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