From patchwork Fri Dec 23 15:56:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13081014 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 38C42C3DA79 for ; Fri, 23 Dec 2022 15:56:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC6C2900002; Fri, 23 Dec 2022 10:56:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C764D940007; Fri, 23 Dec 2022 10:56:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B16A7900004; Fri, 23 Dec 2022 10:56:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A1D30900002 for ; Fri, 23 Dec 2022 10:56:29 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6ABF6405D5 for ; Fri, 23 Dec 2022 15:56:29 +0000 (UTC) X-FDA: 80274023298.29.C3518E9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id CB86AC0021 for ; Fri, 23 Dec 2022 15:56:27 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=T+G3NW09; spf=pass (imf10.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@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=1671810987; 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=lDRifkePoVbkoc0g7uxXuEvxzJ2tfw2CrPnegwMaAgc=; b=UEzWAyHyf9/lAkDxLl+v+Evi9aQ3ng02qvLa5PEffuhtA85Ux0blX8KWATV7EaHn+k03DB hvPV5oQ7CNynjmFIKDzeLdiP2UovYCSAsA9fM7SgsZeJzmSG1vBEmpw2LSj51wm6LOLKPN qHCoJmbjrhRyygzq6jNL24eQ/IX/p+U= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=T+G3NW09; spf=pass (imf10.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671810987; a=rsa-sha256; cv=none; b=yfxn3g6tqoW4M7tOGA3PeIcPb33A3zRdBsDWhbcjMl4mApWET0+OF1i+dLDMfQZowtfe+h v9I/kgfKFj7MWamHr0MUQKGiVY0/IePtzW/2zyWr/GZlLKQEhNrneaAM35gxETF/sC0z7a zsStfZ+NllV30lgJBDyON1+PFVrEpjk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671810987; 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=lDRifkePoVbkoc0g7uxXuEvxzJ2tfw2CrPnegwMaAgc=; b=T+G3NW09D/l9tXoYJlUS/s8l1kCp1tSYDA788yiiAnDbbgFlMipiA7ODhyYbuJSOcEsJ6m UPQfqKhvmfInQG/1Y7WKiVvUz4aptqxCR6Qn6b5sP20Js90RCZjh1hJpAzUVRntmoSsxOi xD+gBjMeFFe+UFjpi1frtmehKiu+2JE= 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-622-OIt8_fafOky8eoVrOlgPCQ-1; Fri, 23 Dec 2022 10:56:22 -0500 X-MC-Unique: OIt8_fafOky8eoVrOlgPCQ-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 6DC5F80D0E3; Fri, 23 Dec 2022 15:56:22 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.192.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id 77C8340C2064; Fri, 23 Dec 2022 15:56:20 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Peter Xu , Hugh Dickins , Andrea Arcangeli , Nadav Amit Subject: [PATCH v1 1/2] mm/userfaultfd: rely on vma->vm_page_prot in uffd_wp_range() Date: Fri, 23 Dec 2022 16:56:15 +0100 Message-Id: <20221223155616.297723-2-david@redhat.com> In-Reply-To: <20221223155616.297723-1-david@redhat.com> References: <20221223155616.297723-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Stat-Signature: 16f17hs1whsinagnsqogg8q8bt8te375 X-Rspam-User: X-Rspamd-Queue-Id: CB86AC0021 X-Rspamd-Server: rspam06 X-HE-Tag: 1671810987-754183 X-HE-Meta: U2FsdGVkX1/oQbh3S6o7r4Q0SxsTaxpwQqzrTVLwbkQ9iwZ24ICS63fX7CwR9HKMtsSmvqUnQeQBE/cod4E2GvGhZniRTzPO9Hl2LNx4MYv8nAaaZwkiVS7KUIs+MrcwBy0LvO+HYbGRRDzOWOH3VmRICld0CSq8CacurqCRsqjOOVnMRwdyzeUuiD4TVt91YfcPe4umuUE/PwlTwsUQPj0ReY9rQeFN5b0gJ5UVJKIIbNyU6Vt6jomu6Vli6UsPGakTNpKdJRLyTQ3CAPeHKJ0eW8iS4IhF0Je24zIs7UWNFksTP0QE3/oFbufSwohx2PLjzb148xxC9qZJnsxvSUIYy4nnW59dA8notUHcqv2Aie9pPEP19aXwd06auih+oALpKyVywFCAuXxlTM5kguAudSHvX5ahbTqRRaVTeK/CbtUWVYwQqih/LZcumK9ejNHednik0S2rl1uOBs8wWEAiFfrUMg7Pfvw+IwLbKKAILoUm95lqQmbwB4xRuQw3VlQ8i4Xt5Z8l2k4QPOfQLssw7DJraicpGGbUfvogoLLUIHM/s7GTbHO6J3iCOwN8OfD7lw0zMMuDs2DkGP1ePAHEghnna0OM8EJtFIBtD9E9aMFCMMNpOmVp+kQQIgDHs5VclwWQi0ZpeQQivna0IcJAoc8hICroxR2CIXQdZQKIkGQhJc4LqEWgk4Y3Au5pcXtCYkObZGxlr6XQsl5kOamsoQV2lKFnhYOg6q+atYrROijdivblasDR87wfHj/lgJBre1ZzdlE0D1p22jPoGbeSJHkvaThRJqBn4exNpjBf2ZYSJaCH18A77EyC3IyzrlzKMtmo7fZaLz1foTDO+K39HSL7AWMlwkhFzyS+H3/dyTuBbROSDnkjI/uEahysc4jFDr3r+lRvyE8wdRA+G9pznsloMILPOwwE9tMUwQzhPq1ugVmbKxOH5UUe9XJu3P1M/s/RivJGznT38Cn QrvMbfGt 7d1qxqs/R4YyEnNZfjaPhMC85rx9F1PEPBE/WQ6Qk4craVtmhxgEbtazQTQ== 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: uffd_wp_range() currently calculates page protection manually using vm_get_page_prot(). This will ignore any other reason for active writenotify: one mechanism applicable to shmem is softdirty tracking. For example, the following sequence 1) Write to mapped shmem page 2) Clear softdirty 3) Register uffd-wp covering the mapped page 4) Unregister uffd-wp covering the mapped page 5) Write to page again will not set the modified page softdirty, because uffd_wp_range() will ignore that writenotify is required for softdirty tracking and simply map the page writable again using change_protection(). Similarly, instead of unregistering, protecting followed by un-protecting the page using uffd-wp would result in the same situation. Now that we enable writenotify whenever enabling uffd-wp on a VMA, vma->vm_page_prot will already properly reflect our requirements: the default is to write-protect all PTEs. However, for shared mappings we would now not remap the PTEs writable if possible when unprotecting, just like for private mappings (COW). To compensate, set MM_CP_TRY_CHANGE_WRITABLE just like mprotect() does to try mapping individual PTEs writable. For private mappings, this change implies that we will now always try setting PTEs writable when un-protecting, just like when upgrading write permissions using mprotect(), which is an improvement. For shared mappings, we will only set PTEs writable if can_change_pte_writable()/can_change_pmd_writable() indicates that it's ok. For ordinary shmem, this will be the case when PTEs are dirty, which should usually be the case -- otherwise we could special-case shmem in can_change_pte_writable()/can_change_pmd_writable() easily, because shmem itself doesn't require writenotify. Note that hugetlb does not yet implement MM_CP_TRY_CHANGE_WRITABLE, so we won't try setting PTEs writable when unprotecting or when unregistering uffd-wp. This can be added later on top by implementing MM_CP_TRY_CHANGE_WRITABLE. While commit ffd05793963a ("userfaultfd: wp: support write protection for userfault vma range") introduced that code, it should only be applicable to uffd-wp on shared mappings -- shmem (hugetlb does not support softdirty tracking). I don't think this corner cases justifies to cc stable. Let's just handle it correctly and prepare for change_protection() cleanups. Fixes: b1f9e876862d ("mm/uffd: enable write protection for shmem & hugetlbfs") Signed-off-by: David Hildenbrand Signed-off-by: David Hildenbrand --- mm/userfaultfd.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 0499907b6f1a..351e8d6b398b 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -727,17 +727,25 @@ ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long start, void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, unsigned long start, unsigned long len, bool enable_wp) { + unsigned int mm_cp_flags; struct mmu_gather tlb; - pgprot_t newprot; if (enable_wp) - newprot = vm_get_page_prot(dst_vma->vm_flags & ~(VM_WRITE)); + mm_cp_flags = MM_CP_UFFD_WP; else - newprot = vm_get_page_prot(dst_vma->vm_flags); + mm_cp_flags = MM_CP_UFFD_WP_RESOLVE; + /* + * vma->vm_page_prot already reflects that uffd-wp is enabled for this + * VMA (see userfaultfd_set_vm_flags()) and that all PTEs are supposed + * to be write-protected as default whenever protection changes. + * Try upgrading write permissions manually. + */ + if (vma_wants_manual_pte_write_upgrade(dst_vma)) + mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE; tlb_gather_mmu(&tlb, dst_mm); - change_protection(&tlb, dst_vma, start, start + len, newprot, - enable_wp ? MM_CP_UFFD_WP : MM_CP_UFFD_WP_RESOLVE); + change_protection(&tlb, dst_vma, start, start + len, vma->vm_page_prot, + mm_cp_flags); tlb_finish_mmu(&tlb); } From patchwork Fri Dec 23 15:56:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13081015 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 8BA1BC4167B for ; Fri, 23 Dec 2022 15:56:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F11F7940008; Fri, 23 Dec 2022 10:56:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EBF34940007; Fri, 23 Dec 2022 10:56:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3A19940008; Fri, 23 Dec 2022 10:56:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B4CE3940007 for ; Fri, 23 Dec 2022 10:56:30 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6772A8064C for ; Fri, 23 Dec 2022 15:56:30 +0000 (UTC) X-FDA: 80274023340.23.E81A142 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf05.hostedemail.com (Postfix) with ESMTP id E7B1810000C for ; Fri, 23 Dec 2022 15:56:28 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IoMxw28t; spf=pass (imf05.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671810989; a=rsa-sha256; cv=none; b=wPgtFREOWp0ieAERqFwVGTIbiY/oxxZlMc7gPRvlHhAYgJUDIe72v3DOG2/x9oZJF2x54b whklrG8ZQYLDnJIZFZTEvlpVg2j0O+S5tzwYoZTu5mfwTOZjNRMQKaPNis5yV1D0GaR+uv BOqhTPTt20OXYonAoKlVxkj0NmGct6Y= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IoMxw28t; spf=pass (imf05.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@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=1671810989; 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=jBT0ZAHzVTRfbP+Wl+fN0VxUXB1ayM6xOLWbOB1BDws=; b=njDloAPQ1PnZTpOXWozoZx8Dwc5XhJRNwuvUuFAYfWW27l2DyxXgcmzFU3oGleQ58qKbIc rNBu27aSJ+v7ZEVTQgWP3xZUZ3+HdFOe1JdKyUsNBMfZcAVOWpSKY5XpmnGe9dvejFwS7/ rJzWUwPoATy/GoRk4u+qixcbNlkklAA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671810988; 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=jBT0ZAHzVTRfbP+Wl+fN0VxUXB1ayM6xOLWbOB1BDws=; b=IoMxw28t6ioD4Zx3fReQHumtJ5G+6IKfDkN2ugxEg1XP55Swoz+5PumtLVrJ6XiyCuO43E mu7BVhnlify3sf0n0W3bwep6Gv/zYKgV9JDvBnu5IGXTdADzzLNK/kH5YHV/U+T6+If6ai PRderSUd9VoPFMadlvcsmnLv99hZysU= 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-104-A-8EU6qTONi9mcOonSs4-Q-1; Fri, 23 Dec 2022 10:56:24 -0500 X-MC-Unique: A-8EU6qTONi9mcOonSs4-Q-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 7DAA3185A794; Fri, 23 Dec 2022 15:56:24 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.192.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id CE6A240C2064; Fri, 23 Dec 2022 15:56:22 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Peter Xu , Hugh Dickins , Andrea Arcangeli , Nadav Amit Subject: [PATCH v1 2/2] mm/mprotect: drop pgprot_t parameter from change_protection() Date: Fri, 23 Dec 2022 16:56:16 +0100 Message-Id: <20221223155616.297723-3-david@redhat.com> In-Reply-To: <20221223155616.297723-1-david@redhat.com> References: <20221223155616.297723-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Rspam-User: X-Rspamd-Queue-Id: E7B1810000C X-Rspamd-Server: rspam01 X-Stat-Signature: bz18ojkfa1ufq798s13rkn7h6d4q5rmp X-HE-Tag: 1671810988-710371 X-HE-Meta: U2FsdGVkX19ZTuTwzBscTxus3KgtWCLThhU7jnrrp57v/dRLZ19cpMxgAVY45DSqbI1/+BIdZ9Qr531d7qxw8cbte3uAW4G5Db1gtNd5D4yONmK3EzRI/uqksEqmD5Qk24MaQyInGW8brdhihlKeHZtTljH+n5u8U+z505qZRhOH2G9P0LvzuhS4kRif9FjNm5DjncZXb3SVXYOvkaF80Oj//7Tk5tlIMuEzOdo7FR4KSBtvsXOcnz+OslE6OcQnrN3uwsXFN3qk9VDWYoutN4T5pufsbSXnOgZBcA9N+2BusR5dlc5osYq0VwiQkUAiB2XyHxg3Zv3vUhk0H5gCBt/v4UPvxiYlQj89yhAPf6Jb4CW03IVx+JfY5VdeRByxRtHGJy7dSzQa6u73IcSMEfYup8KbjrmOWfoBWLq3NwejS8hMPadbxIBPfd2jIV4NWCHzZSkwndrJ1e5R4EB/HQQhg+W3ma2b1M0Gl73ZAQtwYuAPZTxICFZe77/4/c28JXwuj90RHnCh52VhKJ0HtnsEZpqy97oGN6TeL+JA3OwhLDi8OYBWlpnOVhzS2eo9gZ321QmG9UU8vPYox+d9LvsJUaBvHbfrsPTE9aBe+jne4sSJA6Rq3WUMuDzURBmATPxzBzop6yuuRJXFNOEOdYvge06H1QPBuz90fErNmwDxBLwvBa6Gl3KpjnvrsCQKf64sbHe19Epr+p6pHaWbO0HvPtrOS9vGSa0t89i2NNVBi409pjUMKKdO4V4YdrbusNXFwnA3AmaYP/Jkzxp+1XIWcJ9pLXcnG2MoWueVMH24ONHtyjKiFpE1zACq81Hwp442OaQCm1jcAQNqHaIvchKuNjXemqR1EkooDenj/6S9pH8h9EfP8uIIAqV6XmdzBTspTYXJEQHuW5Mh6izy1aF3q57AP3uQSqGoAB1bJ/F16maYSUQTITMjVDAl/5q/ou9BcC+ETddjYgKMQsj LlFblBzl XzhTuRNqUNVQI+0/ubJnLWEiVRcmRgTNQ90mWRKUz0ClPRFfN/zcyFofUfA== 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: Being able to provide a custom protection opens the door for inconsistencies and BUGs: for example, accidentally allowing for more permissions than desired by other mechanisms (e.g., softdirty tracking). vma->vm_page_prot should be the single source of truth. Only PROT_NUMA is special: there is no way we can erroneously allow for more permissions when removing all permissions. Special-case using the MM_CP_PROT_NUMA flag. Signed-off-by: David Hildenbrand Signed-off-by: David Hildenbrand --- include/linux/mm.h | 3 +-- mm/mempolicy.c | 3 +-- mm/mprotect.c | 14 +++++++++++--- mm/userfaultfd.c | 3 +-- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index f3f196e4d66d..b8be8c33ca20 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2148,8 +2148,7 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, pte_t pte); extern unsigned long change_protection(struct mmu_gather *tlb, struct vm_area_struct *vma, unsigned long start, - unsigned long end, pgprot_t newprot, - unsigned long cp_flags); + unsigned long end, unsigned long cp_flags); extern int mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma, struct vm_area_struct **pprev, unsigned long start, unsigned long end, unsigned long newflags); diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 61aa9aedb728..c3f02703a710 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -635,8 +635,7 @@ unsigned long change_prot_numa(struct vm_area_struct *vma, tlb_gather_mmu(&tlb, vma->vm_mm); - nr_updated = change_protection(&tlb, vma, addr, end, PAGE_NONE, - MM_CP_PROT_NUMA); + nr_updated = change_protection(&tlb, vma, addr, end, MM_CP_PROT_NUMA); if (nr_updated) count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated); diff --git a/mm/mprotect.c b/mm/mprotect.c index 908df12caa26..569cefa668a6 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -503,13 +503,21 @@ static unsigned long change_protection_range(struct mmu_gather *tlb, unsigned long change_protection(struct mmu_gather *tlb, struct vm_area_struct *vma, unsigned long start, - unsigned long end, pgprot_t newprot, - unsigned long cp_flags) + unsigned long end, unsigned long cp_flags) { + pgprot_t newprot = vma->vm_page_prot; unsigned long pages; BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL); + /* + * Ordinary protection updates (mprotect, uffd-wp, softdirty tracking) + * are expected to reflect their requirements via VMA flags such that + * vma_set_page_prot() will adjust vma->vm_page_prot accordingly. + */ + if (cp_flags & MM_CP_PROT_NUMA) + newprot = PAGE_NONE; + if (is_vm_hugetlb_page(vma)) pages = hugetlb_change_protection(vma, start, end, newprot, cp_flags); @@ -638,7 +646,7 @@ mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma, mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE; vma_set_page_prot(vma); - change_protection(tlb, vma, start, end, vma->vm_page_prot, mm_cp_flags); + change_protection(tlb, vma, start, end, mm_cp_flags); /* * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 351e8d6b398b..be7ee9d82e72 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -744,8 +744,7 @@ void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, if (vma_wants_manual_pte_write_upgrade(dst_vma)) mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE; tlb_gather_mmu(&tlb, dst_mm); - change_protection(&tlb, dst_vma, start, start + len, vma->vm_page_prot, - mm_cp_flags); + change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags); tlb_finish_mmu(&tlb); }