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); }