Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp765454rwl; Wed, 5 Apr 2023 07:28:00 -0700 (PDT) X-Google-Smtp-Source: AKy350YL6ftKnx7a12kVe6BowAa+1xJVtO4PuCrqHCRPElSqlHj3G3MOJw+5tasDTfOAI3V5kZ4h X-Received: by 2002:a17:903:745:b0:1a2:75cc:654c with SMTP id kl5-20020a170903074500b001a275cc654cmr2259019plb.16.1680704879823; Wed, 05 Apr 2023 07:27:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680704879; cv=none; d=google.com; s=arc-20160816; b=E3IHcZepyvvXPsT4qPFrWxQXManP7nv3V+GBrWY1ez2guZSwrweyLfcSdMFqh5sGVS 1z859iOJ+ltOpK13tOJTI10+OXA+fu6VK6z+bXFzK0Ggn3G4T02KB0mqTra/P24j6/ln qOlC/lmFxH03+W9V+VOHI8aI0iVE0XVCRIGZAPko3ICYnsKHARbosZtmYemgB1OH+oFD 0ziz5dM4TU3PaQgkjCpgjMFASBDW9fQP3Sb3MUzW9jasIuOAjwrFgzVxBc7zfGBzU8Fn fO/1ID0s9Pyw4jSTiW9Ju+5h41OSR+MdIrp1hl+EIxGrCNZqizxMDHcYXS8Y2FVvnw2c 91vA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wA9vrEua4SxGH0U8touKUeIrkMHHDv7b7I0B36bB+wM=; b=HAJFosPpKe/pQulKfW4yJildkGx5+4pRck8lXl2VQm31pWz5UZheRzJaSNxR17oUYg KAH8UHj/v0XtAmjXV5zc8HAnJn4LOfF9g+9iS/3cqOR3lZoNkN/bHRaMQDa/pQGk7aAm Z0R02b1s2uKxWfKl9AZwRH+70Fk0hehfwzzz0HxNlGYWXMTTjcjcOSVOtGgh/xg2gCRX TqsZFwFNXSLWsoBA4F8OU7osNowlSKq/zco7TR7q0OUo/+dGT2WDlG2+bnDOAOx3+fLm Gx8Ufc6aj5CzSNNFn236eJ46zA2XloBsLqk8kh99BJeodSSwcsvbrAhpyOUXBC7loYEB eeew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PQPE0c3T; 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 i9-20020a17090332c900b0019cdbe4750bsi3184273plr.442.2023.04.05.07.27.48; Wed, 05 Apr 2023 07:27:59 -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=PQPE0c3T; 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 S238583AbjDEO1Z (ORCPT + 99 others); Wed, 5 Apr 2023 10:27:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238477AbjDEO1K (ORCPT ); Wed, 5 Apr 2023 10:27:10 -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 CE60859F2 for ; Wed, 5 Apr 2023 07:26:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680704763; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wA9vrEua4SxGH0U8touKUeIrkMHHDv7b7I0B36bB+wM=; b=PQPE0c3TDhCq4fec6YppK9KCtCujgKjUG2EEtoXnM5hE+t8u4tPRjsOiF/hYDjnnTSf6x+ 8VKErFnLvgMkzcIUVT0b4LIPJNz5x1VjJejw+47kzeMA1DYhU3/470/mh9EzsRh5gO5zEs PjlhLxqvND7C5LpLfvD018WC8iq5U6M= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-343-mYpZFFrRMw-fpnIzPwp4dg-1; Wed, 05 Apr 2023 10:25:42 -0400 X-MC-Unique: mYpZFFrRMw-fpnIzPwp4dg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7A9C68996E4; Wed, 5 Apr 2023 14:25:38 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.195.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id 55334400F57; Wed, 5 Apr 2023 14:25:37 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Peter Xu , Muhammad Usama Anjum , stable@vger.kernel.org Subject: [PATCH v1 1/2] mm/userfaultfd: fix uffd-wp handling for THP migration entries Date: Wed, 5 Apr 2023 16:25:34 +0200 Message-Id: <20230405142535.493854-2-david@redhat.com> In-Reply-To: <20230405142535.493854-1-david@redhat.com> References: <20230405142535.493854-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 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 --- 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); set_pmd_at(mm, address, pvmw->pmd, pmdswp); page_remove_rmap(page, vma, true); put_page(page); -- 2.39.2