From patchwork Thu Dec 22 20:55:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13080325 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 22235C4332F for ; Thu, 22 Dec 2022 20:55:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3820940008; Thu, 22 Dec 2022 15:55:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AE8DC940007; Thu, 22 Dec 2022 15:55:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98AA8940008; Thu, 22 Dec 2022 15:55:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 88A5D940007 for ; Thu, 22 Dec 2022 15:55:28 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 61FC11209E1 for ; Thu, 22 Dec 2022 20:55:28 +0000 (UTC) X-FDA: 80271147936.25.0DBE063 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf07.hostedemail.com (Postfix) with ESMTP id CEAD440012 for ; Thu, 22 Dec 2022 20:55:26 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JIQrxSrk; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf07.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671742526; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=F0EDg+aKW31sZt/nNenws8XeVzJOg6ps93oALzUHu5c=; b=r/0docyvns923QmCs1diCQ8dcfQV7chbKt2mj3RQZ1a/Emn3DhDBE3n6KXwpOQgWOjpuny tPR7jnTPJVuHQac8WFuWL/XQNyi0bsVGN5ZerQ+V10UjvP+TfLKr7K4ejWfpb6am244Vo2 CdK2DStBVvwG6dJTV6M+UMutGg58E1o= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JIQrxSrk; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf07.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671742526; a=rsa-sha256; cv=none; b=TM6Ydme+nunW96luZhmEZNY2FEST4rvBH3Xk4amWX3/AHgnEnKyT9h72hum9y4SHEXFqa/ FEJW0oB1Xo2pAjRmUPBXZc90FSIfehO31UDZUpw8aJPgM/Iha6daFW/Hu6sWDvAr2aoTSL SMKBSz2Nv+ZxcQiltIbN+GXlyB4uFDI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671742526; 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=F0EDg+aKW31sZt/nNenws8XeVzJOg6ps93oALzUHu5c=; b=JIQrxSrkwGXqy/QPtUS7sME2JrZyQP15weiFdY1Y5/tzmuNGxr9i922x99NZlSLckKDCn0 HduIJgruVY1mRVpB6EjJtpYa4mINGEvNxB76g/MJuai62E7xfJgEHTn+u5UJhWCQhkg4Sp +8UT9DYvXn4vN+4RGlSlNiFqiO98rsU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-615-Gw7L4NaePVO3xdU5CBBExw-1; Thu, 22 Dec 2022 15:55:20 -0500 X-MC-Unique: Gw7L4NaePVO3xdU5CBBExw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 31C301C05AE6; Thu, 22 Dec 2022 20:55:20 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.193.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 168CE40C2064; Thu, 22 Dec 2022 20:55:17 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Peter Xu , Muchun Song , Miaohe Lin , stable@vger.kernel.org Subject: [PATCH v1 2/2] mm/hugetlb: fix uffd-wp handling for migration entries in hugetlb_change_protection() Date: Thu, 22 Dec 2022 21:55:11 +0100 Message-Id: <20221222205511.675832-3-david@redhat.com> In-Reply-To: <20221222205511.675832-1-david@redhat.com> References: <20221222205511.675832-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: CEAD440012 X-Stat-Signature: cennrx1hgwrah4nqf4grfi53ms738y5k X-HE-Tag: 1671742526-351934 X-HE-Meta: U2FsdGVkX1+G+ZBYl2t9Qn/SvOMYkLT6isxXQOB8ZsCfSJSAtBQ3YkbjY2Y6Pg5cMeWvMLZKX2+o/6MqFVDq0D83eOx5qI3OaQd9+BA/s7SKWYSUdBjZoHu9TzTnZ8Nao/CG0HTeSzidkTneUv0AbVzVyhToTPhvGbi1JmvVB1W0wNatm3thv14GSBtbBdqnQN62gRz5bJARKhKxvMdrDLUyUlhNKS/gtTrBOUnk2mxEbSIaaZBiNc+b0qZ0WZ7Fqj8uDUe8K0bB9Ex3X6qGQzNne5X1VbxZv7UupqtxQEjpPqMRtfnU/ah19gjS+5dgSBt8EuiLAwc46HCnG4muN8KonfgnBPBwFi+7glxAmhDD+JOZ9K6zwpHUIi4bIYFXwLMzVXfgwz5kyYqnsKW6FB61p7zpFMLAxYdRYYKY4sLEbBWfP42+Hk9QPwgmmgu4Qyf7POjdVnOSuvUm+GFXrppL5AvqmU48dhwvBB236/04Xix5s5svl2sgaAXBsfV1f1LIN3pt83EUr/y2V8vY6XiabTdo9EhsSEau72eJmZeI45Ckjb7xn7Uvd/FrTqGtvEyR2YqZecBxeQgS6LVE90h8UEbpyocIr6eIjevdZx3UDHm7ieSTFXlvvCqOFqW6jKVT1flXr3erOkfEY8zvfW+pjRhFfERfDv32LBJlSU+Lo3nFsvs02HP/bBKWKDi/AbQHitvZQc/Kg80jgPra8mahAx4Hw6xVj0YVxTj01wExGAhv/odAvSZ0vDD/C1pRcQmgwW7x0WKfQqZgQ1pDy1d39PQ6lRYWioMX6762UhHdEwVqfHXiCwUHpfBQljF/aC6caZCnJclRT6VbESvAJZSwB3K+EMvmobrFYFhA1xZB/axNd/VD7Z/JP0oOW7gu6qTR/Y3rswTbaaQHING6+ZymPQzcy9M2H1QsNi1sIVASWl1ES3QOfBrF9vbPGyp9f5F2muzO1+205bLpuBu 5kQ== 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: We have to update the uffd-wp SWP PTE bit independent of the type of migration entry. Currently, if we're unlucky and we want to install/clear the uffd-wp bit just while we're migrating a read-only mapped hugetlb page, we would miss to set/clear the uffd-wp bit. Further, if we're processing a readable-exclusive migration entry and neither want to set or clear the uffd-wp bit, we could currently end up losing the uffd-wp bit. Note that the same would hold for writable migrating entries, however, having a writable migration entry with the uffd-wp bit set would already mean that something went wrong. Note that the change from !is_readable_migration_entry -> writable_migration_entry is harmless and actually cleaner, as raised by Miaohe Lin and discussed in [1]. [1] https://lkml.kernel.org/r/90dd6a93-4500-e0de-2bf0-bf522c311b0c@huawei.com Fixes: 60dfaad65aa9 ("mm/hugetlb: allow uffd wr-protect none ptes") Cc: Signed-off-by: David Hildenbrand Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 3a94f519304f..9552a6d1a281 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6516,10 +6516,9 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, } else if (unlikely(is_hugetlb_entry_migration(pte))) { swp_entry_t entry = pte_to_swp_entry(pte); struct page *page = pfn_swap_entry_to_page(entry); + pte_t newpte = pte; - if (!is_readable_migration_entry(entry)) { - pte_t newpte; - + if (is_writable_migration_entry(entry)) { if (PageAnon(page)) entry = make_readable_exclusive_migration_entry( swp_offset(entry)); @@ -6527,13 +6526,15 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, entry = make_readable_migration_entry( swp_offset(entry)); newpte = swp_entry_to_pte(entry); - if (uffd_wp) - newpte = pte_swp_mkuffd_wp(newpte); - else if (uffd_wp_resolve) - newpte = pte_swp_clear_uffd_wp(newpte); - set_huge_pte_at(mm, address, ptep, newpte); pages++; } + + if (uffd_wp) + newpte = pte_swp_mkuffd_wp(newpte); + else if (uffd_wp_resolve) + newpte = pte_swp_clear_uffd_wp(newpte); + if (!pte_same(pte, newpte)) + set_huge_pte_at(mm, address, ptep, newpte); } else if (unlikely(is_pte_marker(pte))) { /* No other markers apply for now. */ WARN_ON_ONCE(!pte_marker_uffd_wp(pte));