From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 498DAC43334 for ; Wed, 20 Jul 2022 19:48:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7A7E76B0072; Wed, 20 Jul 2022 15:48:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 751916B0073; Wed, 20 Jul 2022 15:48:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 619B66B0074; Wed, 20 Jul 2022 15:48:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 536556B0072 for ; Wed, 20 Jul 2022 15:48:33 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 1B0B5A06E0 for ; Wed, 20 Jul 2022 19:48:33 +0000 (UTC) X-FDA: 79708515306.20.1F5DF61 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf28.hostedemail.com (Postfix) with ESMTP id 2F856C0095 for ; Wed, 20 Jul 2022 19:48:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658346511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=on26dLEQi6M3Nk1WhKYkITpAvq0MqN0cNhOtwo2yh7c=; b=ZrAUY2gnjh+F3bqPiewufd/Ul076St3+vgM7NYPSZAp4mecXXZescKRpEa7wFbgiFb9264 B4QZbobOa3tNqKOfh6maW+L2sKh+X/VKqLaaMUWvqu6pw6FCGdVoxB+4YHcmeXy8wPEFIl lY9CqgKAQkVUG7JHNU2JA5LNcwPjWTo= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-588-AS0fdidPO1K7bUNQQO31XA-1; Wed, 20 Jul 2022 15:48:30 -0400 X-MC-Unique: AS0fdidPO1K7bUNQQO31XA-1 Received: by mail-qk1-f198.google.com with SMTP id j16-20020a05620a411000b006b5fb8e5d95so5254520qko.19 for ; Wed, 20 Jul 2022 12:48:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=on26dLEQi6M3Nk1WhKYkITpAvq0MqN0cNhOtwo2yh7c=; b=UcH9x2gABKi8Mw9kNo8Yv5xsdB6/jj5Pg2zvvkJvd9Jql1DmlnYyRLaY6dvkTvOCC9 QMhLj+qwvcX2N1c41Wt3NrybwbccYENNq02Y5BsoBwRk3GraAsJPJtNhSA3yQBcHO+06 1lwjqWEQQiwWqcXhXPiIzxv7W3P9cUuqRFG0DQ7gl/o+imzukjN53vb8JcOTmkd2ObY0 cO376tBX5pViO9TIVgzVqt2KrY8YHz22y2FqDjTOfPgaczghRmHao7D2lYXIRrUzWciL YBer59qMHF5Z/JsaIFaz93h5ZaPhhVBt6tNdwBCTzdQxJhjQU5ZM5ldJAycHOkbxy5Ux Y3EQ== X-Gm-Message-State: AJIora8W5ljykrDfrBkOH8NjfLcIUZssl0aQRfzKOGheviaiO4tS9HDk jQLtK78N+f6rnB+Y2groimFD3Ips73t4bbLOvsgH1hQ42DGespLznoxOlQpUVrNogqdEY5pC1Qh MgL6XqH3Xfjc= X-Received: by 2002:ae9:e715:0:b0:6b5:db75:150e with SMTP id m21-20020ae9e715000000b006b5db75150emr14737795qka.23.1658346509559; Wed, 20 Jul 2022 12:48:29 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v1J22U+4m8EquygNdHZsNC7vr51EIQIxFHy04K/OZ+27q4fpiyHX84PcXY6BbfETK07AFr9g== X-Received: by 2002:ae9:e715:0:b0:6b5:db75:150e with SMTP id m21-20020ae9e715000000b006b5db75150emr14737766qka.23.1658346509217; Wed, 20 Jul 2022 12:48:29 -0700 (PDT) Received: from xz-m1.local (bras-base-aurron9127w-grc-37-74-12-30-48.dsl.bell.ca. [74.12.30.48]) by smtp.gmail.com with ESMTPSA id x9-20020a05620a448900b006b614fe291bsm56937qkp.28.2022.07.20.12.48.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jul 2022 12:48:28 -0700 (PDT) Date: Wed, 20 Jul 2022 15:48:27 -0400 From: Peter Xu To: David Hildenbrand Cc: Nadav Amit , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Mike Rapoport , Axel Rasmussen , Nadav Amit , Andrea Arcangeli , Andrew Cooper , Andy Lutomirski , Dave Hansen , Peter Zijlstra , Thomas Gleixner , Will Deacon , Yu Zhao , Nick Piggin Subject: Re: [RFC PATCH 01/14] userfaultfd: set dirty and young on writeprotect Message-ID: References: <20220718120212.3180-1-namit@vmware.com> <20220718120212.3180-2-namit@vmware.com> <017facf0-7ef8-3faf-138d-3013a20b37db@redhat.com> <2b4393ce-95c9-dd3e-8495-058a139e771e@redhat.com> <69022bad-d6f1-d830-224d-eb8e5c90d5c7@redhat.com> MIME-Version: 1.0 In-Reply-To: <69022bad-d6f1-d830-224d-eb8e5c90d5c7@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658346512; a=rsa-sha256; cv=none; b=eqEEx1OED1LEooh6Fhn+at09j32QDNx3d41ESTmemAPxzCLX2LpjfNcEwL+4n3gWFsfVkY XDUoCXTeE0MmXCYQ8BYtwncI7Nk6SZbznsekPS8VT1V8+kbSXKAevLXt+yd6+rfRg/6a/B O6cW404kO2lHOh4Rn4AA+d3OZ2hO7R8= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZrAUY2gn; spf=none (imf28.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658346512; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=on26dLEQi6M3Nk1WhKYkITpAvq0MqN0cNhOtwo2yh7c=; b=gyS1Bx3KJ1Ucp2XFFaBBvQ6668M1ZletQhNDXsB/3nRRt7jVOIClzKoK6d3LtM7jUkONtq EMpfWGZR0MSiBG+ZQ2j8jqjJAm7UgqqAe+Yj3Fd0hY6F3Hj0/tQy1fZ2fPf0May/KpYsSS yskTzLASvKAeWjT0joLjZ5jbiMrHEh8= X-Stat-Signature: ernowz5tp48cx7jsoax4uu69t1egiicg X-Rspamd-Queue-Id: 2F856C0095 Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZrAUY2gn; spf=none (imf28.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1658346512-492448 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Jul 20, 2022 at 09:33:35PM +0200, David Hildenbrand wrote: > On 20.07.22 21:15, Peter Xu wrote: > > On Wed, Jul 20, 2022 at 05:10:37PM +0200, David Hildenbrand wrote: > >> For pagecache pages it may as well be *plain wrong* to bypass the write > >> fault handler and simply mark pages dirty+map them writable. > > > > Could you elaborate? > > Write-fault handling for some filesystems (that even require this > "slow path") is a bit special. > > For example, do_shared_fault() might have to call page_mkwrite(). > > AFAIK file systems use that for lazy allocation of disk blocks. > If you simply go ahead and map a !dirty pagecache page writable > and mark it dirty, it will not trigger page_mkwrite() and you might > end up corrupting data. > > That's why we the old change_pte_range() code never touched > anything if the pte wasn't already dirty. I don't think that pte_dirty() check was for the pagecache code. For any fs that has page_mkwrite() defined, it'll already have vma_wants_writenotify() return 1, so we'll never try to add write bit, hence we'll never even try to check pte_dirty(). > Because as long as it's not writable, > the FS might have to be informed about the write-unprotect. > > And we end up in the case here for VM_SHARED with vma_wants_writenotify(). > Where we, for example, check > > /* The backer wishes to know when pages are first written to? * > if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite))$ > return 1; > > > Long story short, we should be really careful with write-fault handler bypasses, > especially when deciding to set dirty bits. For pagecache pages, we have to be > especially careful. Since you mentioned page_mkwrite, IMHO it's really the write bit not dirty bit that matters here (and IMHO that's why it's called page_mkwrite() not page_mkdirty()). Here Nadav's patch added pte_mkdirty() only if pte_mkwrite() happens. So I'm a bit confused on what's your worry, and what you're against doing. Say, even if with my original proposal to set dirty unconditionally, it'll be still be after the pte_mkwrite(). I never see how that could affect page_mkwrite not to mention it'll not even reach there. > > For exclusive anon pages it's mostly ok, because wp_page_reuse() > doesn't really contain that much magic. The only thing to consider for ordinary > mprotect() is that there is -- IMHO -- absolutely no guarantee that someone will > write to a specific write-unprotected page soon. For uffd-wp-unprotect it might be > easier to guess, especially, if we un-protect only a single page. Yeh, as mentioned I think that's a valid point - looks good to me to attach the dirty bit only when with a hint. Thanks, -- Peter Xu