Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp879374rwl; Wed, 5 Apr 2023 08:52:55 -0700 (PDT) X-Google-Smtp-Source: AKy350bFJPFE6m+yJV9+o1iZSId+mox+3lx2DrdEUagmh5cOL1mitbhJJ9ARZJZy2fYITfERsKtb X-Received: by 2002:a17:90b:4c92:b0:23f:635e:51e9 with SMTP id my18-20020a17090b4c9200b0023f635e51e9mr7386964pjb.8.1680709975643; Wed, 05 Apr 2023 08:52:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680709975; cv=none; d=google.com; s=arc-20160816; b=CjB5laEIKC0sxWuKIUyu27c8Z45TxHgeA0+hJmliLChT3VXVms5CFvTxVg/AL8+gqb 4iTEgPxZ0IbFA7oUerOG5RX2qc+P+4kXyHjBbF2Y17cFHcU4i1KQH41RkZXG3zvbGJ8Z yGtPLD/cHmhAfgGtRi7sA4vVZpFwqYunZE0IzMA27fFxXmPrd7RVwJxAgHvJUCWN6Ihp oE0uriWiBtsr/gVBgFN2e0l2dnlHEDL9lKXntEfmRCO0yQ3/DAHhzv36PKNw8zzz70vo o/YrIkNfykc2FO66WPP7L3mYNjYY0XT24q2bJk3RGkCl/prT2DKvlaObgy2Tjqg3v6Ns ec3w== 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:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=zwcds+UtbfQIbiuF3/Xn2nxiwljEp+1pjIA6S8P9CNE=; b=NrY9GuZTHTMeWi3Q9b/8MX9aG6+l+49O9uE0j9gXUtp8ZtT4q4oCWu2mQ4a3jFd4x+ G3fYGUC+l2Kz0dY8pWMnMBWKG1Y5w8RQ2DKaIkWsLA/AueCDDcQP5qRIL5ukSpbACapG wu+0SMGksT4VNooGFhkstMGDQiQmZgWiqD0urzfv6GO6UDRohyMcnf7dbJGqRj/RseZ3 o3mcroXQkNpZ+2xpPGOv6+Fdewu4BpAEFvi0g6LRQ10cEPxhh2zNniwOD3laaMYmHphk 3aIPC4onKfKgjELUVskN4g0kn9BOuBfVEZ83qmKA5fNakvP/i0p+VedM2wFKlTcznb6R b81g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JIjze3aR; 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 me9-20020a17090b17c900b0023d2e945af7si1569731pjb.95.2023.04.05.08.52.42; Wed, 05 Apr 2023 08:52:55 -0700 (PDT) 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=JIjze3aR; 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 S239032AbjDEPwT (ORCPT + 99 others); Wed, 5 Apr 2023 11:52:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239150AbjDEPwE (ORCPT ); Wed, 5 Apr 2023 11:52:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EEAA86182 for ; Wed, 5 Apr 2023 08:51:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680709875; 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=zwcds+UtbfQIbiuF3/Xn2nxiwljEp+1pjIA6S8P9CNE=; b=JIjze3aRO+x1WK50rC2z1wTducNGlS6MdNGP7eXHqkg1Yv9KweoqtsA/FBbedcS9P9JDSY uvxbs1OJKBA+NSJjM1DCZQ6bLd7SjT/n1Omhl74TeRg+sJrFKnPYJ8DAstJgFNEXQvBLWW 6CrAH2Tw0wCZrC7+8K9li19M5pmJ7PM= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-313-MQknS5hnMESyFm9554DKyQ-1; Wed, 05 Apr 2023 11:51:13 -0400 X-MC-Unique: MQknS5hnMESyFm9554DKyQ-1 Received: by mail-wr1-f71.google.com with SMTP id d5-20020adfa345000000b002d716fc080fso4555055wrb.22 for ; Wed, 05 Apr 2023 08:51:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680709871; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zwcds+UtbfQIbiuF3/Xn2nxiwljEp+1pjIA6S8P9CNE=; b=IWpBrrN0Nz8Pc6tJut4EMBiuxsgLHF5fFQr5ONU7E+v/dPe2ER6L3wN0u/OyLGVik9 y7Weno4gaUexjdRbo1uTg5lyd/B/f3/Cbjh4GwNFYhdqvOSNYrkh8Ee3W2smA5HdFTxg Y2StDI6L6chsKeZ0e4AkCf+EoRCPS4QzIowcFKhLqYxmvaC+ibPuYdgppNeIf2Sovlf+ f6Io7mtk4Q+wy6S7+rTxEkGlNlGCCCyJE4lndTLvnxDSzeVCy50eZLUogbQqO4+UFr90 rrg9kOB7+2Glh2ufGEmLbKX8zPRPgWR3aVuAWDJPedTwNP9GfgXKLJEES5zaCSZ8Jxyg ksDA== X-Gm-Message-State: AAQBX9eAZI/8UOnRD7c6a/VvIDLk5oJKdS2zU5THXesI/J6gDMImU/sz 7iGe21v3tRTGjsx+OsiQmKXZ+53wPa9s0G1pK/gi6JdEfevHW6bqv5HxQulUhLPovUJToWDyxae V6zVZRHL2vPpsG5yzxlJGNSWQ X-Received: by 2002:a5d:460c:0:b0:2cf:feac:1ba4 with SMTP id t12-20020a5d460c000000b002cffeac1ba4mr4489144wrq.52.1680709871734; Wed, 05 Apr 2023 08:51:11 -0700 (PDT) X-Received: by 2002:a5d:460c:0:b0:2cf:feac:1ba4 with SMTP id t12-20020a5d460c000000b002cffeac1ba4mr4489129wrq.52.1680709871360; Wed, 05 Apr 2023 08:51:11 -0700 (PDT) Received: from ?IPV6:2003:cb:c703:d00:ca74:d9ea:11e0:dfb? (p200300cbc7030d00ca74d9ea11e00dfb.dip0.t-ipconnect.de. [2003:cb:c703:d00:ca74:d9ea:11e0:dfb]) by smtp.gmail.com with ESMTPSA id x9-20020a5d4449000000b002e40d124460sm15350496wrr.97.2023.04.05.08.51.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Apr 2023 08:51:10 -0700 (PDT) Message-ID: <062cd239-179b-d146-8f5d-735e672b3237@redhat.com> Date: Wed, 5 Apr 2023 17:51:10 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH v1 1/2] mm/userfaultfd: fix uffd-wp handling for THP migration entries Content-Language: en-US To: Peter Xu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Muhammad Usama Anjum , stable@vger.kernel.org References: <20230405142535.493854-1-david@redhat.com> <20230405142535.493854-2-david@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.6 required=5.0 tests=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 05.04.23 17:43, Peter Xu wrote: > On Wed, Apr 05, 2023 at 05:17:31PM +0200, David Hildenbrand wrote: >> On 05.04.23 17:12, Peter Xu wrote: >>> On Wed, Apr 05, 2023 at 04:25:34PM +0200, David Hildenbrand wrote: >>>> Looks like what we fixed for hugetlb in commit 44f86392bdd1 ("mm/hugetlb: >>>> fix uffd-wp handling for migration entries in hugetlb_change_protection()") >>>> similarly applies to THP. >>>> >>>> Setting/clearing uffd-wp on THP migration entries is not implemented >>>> properly. Further, while removing migration PMDs considers the uffd-wp >>>> bit, inserting migration PMDs does not consider the uffd-wp bit. >>>> >>>> We have to set/clear independently of the migration entry type in >>>> change_huge_pmd() and properly copy the uffd-wp bit in >>>> set_pmd_migration_entry(). >>>> >>>> Verified using a simple reproducer that triggers migration of a THP, that >>>> the set_pmd_migration_entry() no longer loses the uffd-wp bit. >>>> >>>> Fixes: f45ec5ff16a7 ("userfaultfd: wp: support swap and page migration") >>>> Cc: stable@vger.kernel.org >>>> Signed-off-by: David Hildenbrand >>> >>> Reviewed-by: Peter Xu >>> >>> Thanks, one trivial nitpick: >>> >>>> --- >>>> mm/huge_memory.c | 14 ++++++++++++-- >>>> 1 file changed, 12 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c >>>> index 032fb0ef9cd1..bdda4f426d58 100644 >>>> --- a/mm/huge_memory.c >>>> +++ b/mm/huge_memory.c >>>> @@ -1838,10 +1838,10 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, >>>> if (is_swap_pmd(*pmd)) { >>>> swp_entry_t entry = pmd_to_swp_entry(*pmd); >>>> struct page *page = pfn_swap_entry_to_page(entry); >>>> + pmd_t newpmd; >>>> VM_BUG_ON(!is_pmd_migration_entry(*pmd)); >>>> if (is_writable_migration_entry(entry)) { >>>> - pmd_t newpmd; >>>> /* >>>> * A protection check is difficult so >>>> * just be safe and disable write >>>> @@ -1855,8 +1855,16 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, >>>> newpmd = pmd_swp_mksoft_dirty(newpmd); >>>> if (pmd_swp_uffd_wp(*pmd)) >>>> newpmd = pmd_swp_mkuffd_wp(newpmd); >>>> - set_pmd_at(mm, addr, pmd, newpmd); >>>> + } else { >>>> + newpmd = *pmd; >>>> } >>>> + >>>> + if (uffd_wp) >>>> + newpmd = pmd_swp_mkuffd_wp(newpmd); >>>> + else if (uffd_wp_resolve) >>>> + newpmd = pmd_swp_clear_uffd_wp(newpmd); >>>> + if (!pmd_same(*pmd, newpmd)) >>>> + set_pmd_at(mm, addr, pmd, newpmd); >>>> goto unlock; >>>> } >>>> #endif >>>> @@ -3251,6 +3259,8 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw, >>>> pmdswp = swp_entry_to_pmd(entry); >>>> if (pmd_soft_dirty(pmdval)) >>>> pmdswp = pmd_swp_mksoft_dirty(pmdswp); >>>> + if (pmd_swp_uffd_wp(*pvmw->pmd)) >>>> + pmdswp = pmd_swp_mkuffd_wp(pmdswp); >>> >>> I think it's fine to use *pmd, but maybe still better to use pmdval? I >>> worry pmdp_invalidate()) can be something else in the future that may >>> affect the bit. >> >> Wondering how I ended up with that, I realized that it's actually >> wrong and might have worked by chance for my reproducer on x86. >> >> That should make it work: >> >> diff --git a/mm/huge_memory.c b/mm/huge_memory.c >> index f977c965fdad..fffc953fa6ea 100644 >> --- a/mm/huge_memory.c >> +++ b/mm/huge_memory.c >> @@ -3257,7 +3257,7 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw, >> pmdswp = swp_entry_to_pmd(entry); >> if (pmd_soft_dirty(pmdval)) >> pmdswp = pmd_swp_mksoft_dirty(pmdswp); >> - if (pmd_swp_uffd_wp(*pvmw->pmd)) >> + if (pmd_uffd_wp(pmdval)) >> pmdswp = pmd_swp_mkuffd_wp(pmdswp); >> set_pmd_at(mm, address, pvmw->pmd, pmdswp); >> page_remove_rmap(page, vma, true); > > I guess pmd_swp_uffd_wp() just reads the _USER bit 2 which is also set for > a present pte, but then it sets swp uffd-wp always even if it was not set. > Yes. I modified the reproducer to migrate without uffd-wp first and we suddenly gain a uffd-wp bit. > Yes the change must be squashed in to be correct, with that, my R-b keeps. Thanks, I will resend later. -- Thanks, David / dhildenb