Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2433714rwb; Fri, 2 Dec 2022 09:40:09 -0800 (PST) X-Google-Smtp-Source: AA0mqf4MWrGYb3HhWkQcFrQzQxo380TT0fUqb1EcDopsAr332iGOgRVN4H2FxBtECawBP3ZLVBn9 X-Received: by 2002:a17:906:68cd:b0:7ac:f8e3:d547 with SMTP id y13-20020a17090668cd00b007acf8e3d547mr16305141ejr.53.1670002809196; Fri, 02 Dec 2022 09:40:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670002809; cv=none; d=google.com; s=arc-20160816; b=RSDQopZ9ClaWzEqRIyGbDqRnqXJFNDwzws6aooooUmjleF0LAwHyUqM3gMWZk3y3h2 dkUuS5H/A7l7OL0pF+0m6Ydf1LKh8y9zM8EF8f14S0rbpcOhJMyGHiZrMAiOMgwUNg9p EyAQUNdavLOTlCVjpHNlTqUCICok2COMRBpb+PvqcfM7hw/60GyoDokBh+77bWyNhi+2 lmI8OUWmQ6BXQ5pNIMqc2Y5sYBir5b2wkwK+1MYlsUdjSXhCmY0ax1Vruu6hntX3TNwr nIkaWXjGMmQyTbfoqDTeAbjECPR4aNje0xHmREwLLZdpeOp/p1/AE4Zr6KHH7qxJm8dQ NeUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:content-language:references:cc:to:from:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=Usmp2CDAvNfy2M2omAE7j3vT3EFJoDk3WrwAZjTClPA=; b=m1dXOYo09KpLJWSeiElMrMUYjxfbxzWzG0bcNwkeZtDaDauLR+nEcJ+g42c+o+mVes FC6kQQXrdWVu1BtKv7ywSwHRCckLWAm+BeJ6BWItpDy+eYuChR4/xy7SZcQAt7cBzWwd SDgupdfY9a1lfuRRF2h2zmbLnHYom69AhK4YcrhZRwR6d6Q2Y9FP4ABTO8Qf3CCbMu+w sztYr3nm/D0xp/w2IL/cz1N/LgGAPOrrtlDjPkjtYqTOf6b6UuhQvlhGbvtpJFn4twuS dIhd0TV+icaUVXhMqbSf4TOfW0w1wzBvtNYDxiDzXgB/SzMrj4qUHublcdW8/Thw0OmF iG3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HKMZQphs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ca15-20020aa7cd6f000000b00459f9c3d02bsi6405169edb.22.2022.12.02.09.39.48; Fri, 02 Dec 2022 09:40:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HKMZQphs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234080AbiLBRMU (ORCPT + 83 others); Fri, 2 Dec 2022 12:12:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233751AbiLBRMR (ORCPT ); Fri, 2 Dec 2022 12:12:17 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37889E8018 for ; Fri, 2 Dec 2022 09:11:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670001082; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Usmp2CDAvNfy2M2omAE7j3vT3EFJoDk3WrwAZjTClPA=; b=HKMZQphsszwE4Zv8uPwdzuyq6Hvzlg4UA9cmuQC0f0nx0+YdLhHW0E1/+d3uFv3BgciWHo 7joDQ8rljYIOSptt5L17X0lcOUT6vUxqYz+eup1E6cZLrku9oaCW/kXs4enmUxKtKHmVs9 wth1s/uz/U0+/ikXH6cpg5CBjQW9q3E= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-320-BP-5BM5JOwqFkMeCUqxn9A-1; Fri, 02 Dec 2022 12:11:21 -0500 X-MC-Unique: BP-5BM5JOwqFkMeCUqxn9A-1 Received: by mail-wm1-f69.google.com with SMTP id f18-20020a7bcd12000000b003d077f0e3e5so2160846wmj.7 for ; Fri, 02 Dec 2022 09:11:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:content-language :references:cc:to:from:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Usmp2CDAvNfy2M2omAE7j3vT3EFJoDk3WrwAZjTClPA=; b=ZLrNEs/b0qujc1avg4keweE8Z7biP2i/52Lw3H2vDLx48+tRAqSvVDkNj+e/5of+28 A/jykr4DnLIj978ZjswvusbpH7U5C+OOPb96DkNZzf5XpF0W9xZpsIa11kggk8B88rel VvXMHOtn3j+0V3bNr3DiZW2qLxAOslD6w/7XpqF+aIOTwfx9/mIMtiIc74hYEdzCofn7 Id1bP+U9n+GTXJ2XCZ/mWtUVkjheqwUz4vNatz5EbQ8MvbVQ2U3sNjKQBMzLqK8RHHDg nEWbUaHfQ6SJROPEzvvwvNMH7KNsY5fJ/pXI1CxB8YqUpNsoJbuQZGOZp18iZiIL9S87 alyg== X-Gm-Message-State: ANoB5plD0xELx7r3+JVoBJfWWN8QyBGnjLKKE4iSiPC9pSnHFVUSz4FC q1ThjedR4L/lrlYi884SClcGyVg7Lu1nWaejUwNlJTZw0uNv8I/IlR7cJ4+fbJSkulkrpkFtUr6 F79VK5c+P6XoaokM/DcOJ6z61 X-Received: by 2002:a05:600c:4e09:b0:3cf:55bd:4944 with SMTP id b9-20020a05600c4e0900b003cf55bd4944mr55175174wmq.64.1670001079839; Fri, 02 Dec 2022 09:11:19 -0800 (PST) X-Received: by 2002:a05:600c:4e09:b0:3cf:55bd:4944 with SMTP id b9-20020a05600c4e0900b003cf55bd4944mr55175147wmq.64.1670001079507; Fri, 02 Dec 2022 09:11:19 -0800 (PST) Received: from ?IPV6:2003:cb:c703:7a00:852e:72cd:ed76:d72f? (p200300cbc7037a00852e72cded76d72f.dip0.t-ipconnect.de. [2003:cb:c703:7a00:852e:72cd:ed76:d72f]) by smtp.gmail.com with ESMTPSA id e18-20020a5d4e92000000b0024206ed539fsm7307560wru.66.2022.12.02.09.11.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 Dec 2022 09:11:19 -0800 (PST) Message-ID: <19800718-9cb6-9355-da1c-c7961b01e922@redhat.com> Date: Fri, 2 Dec 2022 18:11:17 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH RFC] mm/userfaultfd: enable writenotify while userfaultfd-wp is enabled for a VMA From: David Hildenbrand To: Peter Xu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Ives van Hoorne , stable@vger.kernel.org, Andrew Morton , Hugh Dickins , Alistair Popple , Mike Rapoport , Nadav Amit , Andrea Arcangeli References: <20221202122748.113774-1-david@redhat.com> <690afe0f-c9a0-9631-b365-d11d98fdf56f@redhat.com> Content-Language: en-US Organization: Red Hat In-Reply-To: <690afe0f-c9a0-9631-b365-d11d98fdf56f@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02.12.22 17:56, David Hildenbrand wrote: > On 02.12.22 17:33, Peter Xu wrote: >> On Fri, Dec 02, 2022 at 01:27:48PM +0100, David Hildenbrand wrote: >>> Currently, we don't enable writenotify when enabling userfaultfd-wp on >>> a shared writable mapping (for now we only support SHMEM). The consequence >> >> and hugetlbfs >> >>> is that vma->vm_page_prot will still include write permissions, to be set >>> as default for all PTEs that get remapped (e.g., mprotect(), NUMA hinting, >>> page migration, ...). >> >> The thing is by default I think we want the write bit.. >> >> The simple example is (1) register UFFD_WP on shmem writable, (2) write a >> page. Here we didn't wr-protect anything, so we want the write bit there. >> >> Or the other example is when UFFDIO_COPY with flags==0 even if with >> VM_UFFD_WP. We definitely wants the write bit. >> >> We only doesn't want the write bit when uffd-wp is explicitly set. >> >> I think fundamentally the core is uffd-wp is pte-based, so the information >> resides in pte not vma. I'm not strongly objecting this patch, especially >> you mentioned auto-numa so I need to have a closer look later there. >> However I do think uffd-wp is slightly special because we always need to >> consider pte information anyway, so a per-vma information doesn't hugely >> help, IMHO. > > That's the same as softdirty tracking, IMHO. > > [...] > >>> Running the mprotect() reproducer [1] without this commit: >>> $ ./uffd-wp-mprotect >>> FAIL: uffd-wp did not fire >>> Running the mprotect() reproducer with this commit: >>> $ ./uffd-wp-mprotect >>> PASS: uffd-wp fired >>> >>> [1] https://lore.kernel.org/all/222fc0b2-6ec0-98e7-833f-ea868b248446@redhat.com/T/#u >> >> I still hope for a formal patch (non-rfc) we can have a reproducer outside >> mprotect(). IMHO mprotect() is really ambiguously here being used with >> uffd-wp, so not a good example IMO as I explained in the other thread [1]. > > I took the low hanging fruit to showcase that this is a more generic problem. > The reproducer is IMHO nice because it's simple and race-free. > >> >> I'll need to off-work most of the rest of today, but maybe I can also have >> a look in the weekend or Monday more on the numa paths. Before that, can >> we first reach a consensus that we have the mm/migrate patch there to be >> merged first? These are two issues, IMHO. >> >> I know you're against me for some reason, but until now I sincerely don't >> know why. That patch sololy recovers write bit status (by removing it for >> read-only) for a migration entry and that definitely makes sense to me. As >> I also mentioned in the old version of that thread, we can rework migration >> entries and merge READ|WRITE entries into a GENERIC entry one day if you >> think proper, but that's for later. > > I'm not against you. I'm against changing well-working, common code > when it doesn't make any sense to me to change it. And now we have proof that > mprotect() just behaves exactly the same way, using the basic rules of vma->vm_page_prot. > > Yes, there is broken sparc64 (below), but that shouldn't dictate our implementation. > > > What *would* make sense to me, as I raised, is: > > diff --git a/mm/migrate.c b/mm/migrate.c > index dff333593a8a..9fc181fd3c5a 100644 > --- a/mm/migrate.c > +++ b/mm/migrate.c > @@ -213,8 +213,10 @@ static bool remove_migration_pte(struct folio *folio, > pte = pte_mkdirty(pte); > if (is_writable_migration_entry(entry)) > pte = maybe_mkwrite(pte, vma); > - else if (pte_swp_uffd_wp(*pvmw.pte)) > + else if (pte_swp_uffd_wp(*pvmw.pte)) { > pte = pte_mkuffd_wp(pte); > + pt = pte_wrprotect(pte); > + } > > if (folio_test_anon(folio) && !is_readable_migration_entry(entry)) > rmap_flags |= RMAP_EXCLUSIVE; > > > It still requires patch each and every possible code location, which I dislike as > described in the patch description. The fact that there are still uffd-wp bugs > with your patch makes that hopefully clear. I'd be interested if they can be > reproduced witht his patch. > And if NUMA hinting is indeed the problem, without this patch what would be required would most probably be: diff --git a/mm/memory.c b/mm/memory.c index 8a6d5c823f91..869d35ef0e24 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4808,6 +4808,8 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) pte = pte_mkyoung(pte); if (was_writable) pte = pte_mkwrite(pte); + if (pte_uffd_wp(pte)) + pte = pte_wrprotect(pte); ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); update_mmu_cache(vma, vmf->address, vmf->pte); pte_unmap_unlock(vmf->pte, vmf->ptl); And just to make my point about the migration path clearer: doing it your way would be: diff --git a/mm/memory.c b/mm/memory.c index 8a6d5c823f91..a7c4c1a57f6a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4808,6 +4808,8 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) pte = pte_mkyoung(pte); if (was_writable) pte = pte_mkwrite(pte); + else + pte = pte_wrprotect(pte); ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); update_mmu_cache(vma, vmf->address, vmf->pte); pte_unmap_unlock(vmf->pte, vmf->ptl); And I don't think that's the right approach. -- Thanks, David / dhildenb