From patchwork Mon Jan 2 16:08:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13086802 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 8198BC3DA7A for ; Mon, 2 Jan 2023 16:09:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 222318E0005; Mon, 2 Jan 2023 11:09:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D0F08E0001; Mon, 2 Jan 2023 11:09:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 099448E0005; Mon, 2 Jan 2023 11:09:17 -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 F0EDC8E0001 for ; Mon, 2 Jan 2023 11:09:16 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C6165A5FDF for ; Mon, 2 Jan 2023 16:09:16 +0000 (UTC) X-FDA: 80310343512.04.E676B36 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf29.hostedemail.com (Postfix) with ESMTP id 3862D12000F for ; Mon, 2 Jan 2023 16:09:15 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YpAR5ryY; spf=pass (imf29.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1672675755; a=rsa-sha256; cv=none; b=aJo+iyWGAYz9GcDOAsPIYJZdWcmYPQooHHGiMPUS8dDvxU1IzYm71zK4X4l90eNLhBjzm2 fNCy06thSJCGJh/D1uAUqO7oErctwwCgfROhxo6nodd8e3UUMOsZEAXafMe4BqypKiTBUS kA0oFj1/qXtHJMHojIxdCuqlVlTnBTk= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YpAR5ryY; spf=pass (imf29.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1672675755; 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=6DrwTfQPeLhDnawi85eq4Dim4O55bnNWNC/AzEE3jLc=; b=72Viw6WJaj50tNWcls8EdohwmAN9Z6Z38Btl380IXRYIVhJT14TDXilSTInJbBnV4XoPBn BsfcwJzzpbKnytOsIE+WY8vtFa3xK7/Hl01Nh0Sn1Z3qRXOTChnkxyQEk6E4vlK91khhl3 yMSCC5plbSQdyAKygqyjjCsf6Z2t89w= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672675754; 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=6DrwTfQPeLhDnawi85eq4Dim4O55bnNWNC/AzEE3jLc=; b=YpAR5ryY53TjEerXYD0wfCQ5Ochm5yhfXO+dDTLVwoIAGyxlFuig4jpiHgTeagxB5jl1sV Xt1tvZj9Bp6/8SxskffpafahSqg8nCClqVm2dtYfy1JHCt17/mSbzx3id5tLHJP/jHj+XL c3N1rpcW7OWfV65n8XtbjhS4oTtHALo= 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-553-2irx2fcbOIKnZ864F3jF7g-1; Mon, 02 Jan 2023 11:09:04 -0500 X-MC-Unique: 2irx2fcbOIKnZ864F3jF7g-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2F3F618A647B; Mon, 2 Jan 2023 16:09:03 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.193.209]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9677451E5; Mon, 2 Jan 2023 16:09:00 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, David Hildenbrand , Andrew Morton , Arnd Bergmann , Greg Kroah-Hartman , Nicolas Pitre , Jens Axboe , Pavel Begunkov Subject: [PATCH mm-unstable v1 1/3] mm/nommu: factor out check for NOMMU shared mappings into is_nommu_shared_mapping() Date: Mon, 2 Jan 2023 17:08:54 +0100 Message-Id: <20230102160856.500584-2-david@redhat.com> In-Reply-To: <20230102160856.500584-1-david@redhat.com> References: <20230102160856.500584-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspam-User: X-Rspamd-Queue-Id: 3862D12000F X-Rspamd-Server: rspam01 X-Stat-Signature: bspy6kwcj6oa6b5ttk89yatxggcxuufp X-HE-Tag: 1672675755-897556 X-HE-Meta: U2FsdGVkX18KzQCCjYEc3vvaAa/lGDtRXIhbZQLtssU4UHeunZhCxKJZxAeReyk8Pq11NPXV/1spbAv+wTNwY40+z6PYhruYZGF1J/AZidB8hNvFXwGtq2Cc3ZWLUDm0B2VlTUiS+jqeVil0zSjBKJEOXyC8spZOFEuPKCKHl9yx+NAFIzf3K7OUA4b8RBAAJt+Cyav6KLyHr3DP++ODOfyq/T88kjc+zBpdAeVa+dIZrdUQyLgETOPuKPc8ktgBIwkUb+d982mTMakskj4/JQvEJDz04UT++T9wdwnLzq4ajCoxSjrFxat6OtEX4eVM3YTagD+AU6Dv6j/Ve859hsftImFwJWJ70YIqMtkKm7+7xdb+IlH2ZB89gG7mbXts3D1ewVUH/ZNfmXa9JNncAXUHDGJC1GARgraSW1xJwCCQCcv7LhBSx6INi95afHlkhAbmmoa/YiLBoKAz6dLd4ZZr/5vf/+bKrElcBkD6IpmsjoKSKbRzG42BAbmHUGBlIEqGS43MgaFVmfJnxGreH9hRqUR4CR5oXU6rJPCdZDPw/iNy1GkP2fG//a/NifoJEZE7W6Bb77xMOEsX9JTtq0J4peoiInV4SaUeNc5FQoGnErEZmD9WWNR8T8ya6yAn2SmUZBOOlwPoPrDTDG+e/DWifvNMUIDy/LSAqANEA6ok7RYjyvCgsVc2JSCagjIf69xifA7AJL06mJ5pxUXsl7i+Vw059MWQJhZmI1kpGzDtKxXNbTMsEEjsCgWRA+HQ10Oq10gIPSWepoABfdnjr57oqdJ4SLj8fcE1Hq523liffvRNmhoV6QsjY+LVWU3395SMtjN8LvU+Puqu/b9XMz0F/VGL7MjjUeUmTD0CvdFnbg1vAkgI3yUK0zY+GV9mb1L2R1RxbR575e/65K8vAyk+ZyIKK/8yFRC+4EXvIt//0dNnocoot3/tbdyImBrdRSSI3pcZXa/xPIeueq1 u+3zJq/y nMy2f9iwIREnJV/qfDZsYC7Lj7TiLZHQdmY1OhcLaDe7fhwgJFocGK9Xk9BNusCYm+pxs 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 want to stop using VM_MAYSHARE in private mappings to pave the way for clarifying the semantics of VM_MAYSHARE vs. VM_SHARED and reduce the confusion. While CONFIG_MMU uses VM_MAYSHARE to represent MAP_SHARED, !CONFIG_MMU also sets VM_MAYSHARE for selected R/O private file mappings that are an effective overlay of a file mapping. Let's factor out all relevant VM_MAYSHARE checks in !CONFIG_MMU code into is_nommu_shared_mapping() first. Note that whenever VM_SHARED is set, VM_MAYSHARE must be set as well (unless there is a serious BUG). So there is not need to test for VM_SHARED manually. No functional change intended. Signed-off-by: David Hildenbrand --- drivers/char/mem.c | 2 +- fs/cramfs/inode.c | 2 +- fs/proc/task_nommu.c | 2 +- fs/ramfs/file-nommu.c | 2 +- fs/romfs/mmap-nommu.c | 2 +- include/linux/mm.h | 15 +++++++++++++++ io_uring/io_uring.c | 2 +- mm/nommu.c | 11 ++++++----- 8 files changed, 27 insertions(+), 11 deletions(-) diff --git a/drivers/char/mem.c b/drivers/char/mem.c index 83bf2a4dcb57..ffb101d349f0 100644 --- a/drivers/char/mem.c +++ b/drivers/char/mem.c @@ -343,7 +343,7 @@ static unsigned zero_mmap_capabilities(struct file *file) /* can't do an in-place private mapping if there's no MMU */ static inline int private_mapping_ok(struct vm_area_struct *vma) { - return vma->vm_flags & VM_MAYSHARE; + return is_nommu_shared_mapping(vma->vm_flags); } #else diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c index 61ccf7722fc3..50e4e060db68 100644 --- a/fs/cramfs/inode.c +++ b/fs/cramfs/inode.c @@ -437,7 +437,7 @@ static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma) static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma) { - return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS; + return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -ENOSYS; } static unsigned long cramfs_physmem_get_unmapped_area(struct file *file, diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c index 2fd06f52b6a4..0ec35072a8e5 100644 --- a/fs/proc/task_nommu.c +++ b/fs/proc/task_nommu.c @@ -38,7 +38,7 @@ void task_mem(struct seq_file *m, struct mm_struct *mm) } if (atomic_read(&mm->mm_count) > 1 || - vma->vm_flags & VM_MAYSHARE) { + is_nommu_shared_mapping(vma->vm_flags)) { sbytes += size; } else { bytes += size; diff --git a/fs/ramfs/file-nommu.c b/fs/ramfs/file-nommu.c index cb240eac5036..cd4537692751 100644 --- a/fs/ramfs/file-nommu.c +++ b/fs/ramfs/file-nommu.c @@ -264,7 +264,7 @@ static unsigned long ramfs_nommu_get_unmapped_area(struct file *file, */ static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma) { - if (!(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) + if (!is_nommu_shared_mapping(vma->vm_flags)) return -ENOSYS; file_accessed(file); diff --git a/fs/romfs/mmap-nommu.c b/fs/romfs/mmap-nommu.c index 2c4a23113fb5..4578dc45e50a 100644 --- a/fs/romfs/mmap-nommu.c +++ b/fs/romfs/mmap-nommu.c @@ -63,7 +63,7 @@ static unsigned long romfs_get_unmapped_area(struct file *file, */ static int romfs_mmap(struct file *file, struct vm_area_struct *vma) { - return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS; + return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -ENOSYS; } static unsigned romfs_mmap_capabilities(struct file *file) diff --git a/include/linux/mm.h b/include/linux/mm.h index c37f9330f14e..28568a92e5df 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1347,6 +1347,21 @@ static inline bool is_cow_mapping(vm_flags_t flags) return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; } +#ifndef CONFIG_MMU +static inline bool is_nommu_shared_mapping(vm_flags_t flags) +{ + /* + * NOMMU shared mappings are ordinary MAP_SHARED mappings and selected + * R/O MAP_PRIVATE file mappings that are an effective R/O overlay of + * a file mapping. R/O MAP_PRIVATE mappings might still modify + * underlying memory if ptrace is active, so this is only possible if + * ptrace does not apply. Note that there is no mprotect() to upgrade + * write permissions later. + */ + return flags & VM_MAYSHARE; +} +#endif + #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP) #define SECTION_IN_PAGE_FLAGS #endif diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index ff2bbac1a10f..ae7d2a9eb63f 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -3194,7 +3194,7 @@ static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) { - return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; + return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -EINVAL; } static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) diff --git a/mm/nommu.c b/mm/nommu.c index 214c70e1d059..6c4bdc07a776 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -958,9 +958,10 @@ static int do_mmap_private(struct vm_area_struct *vma, */ if (capabilities & NOMMU_MAP_DIRECT) { ret = call_mmap(vma->vm_file, vma); + /* shouldn't return success if we're not sharing */ + if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags))) + ret = -ENOSYS; if (ret == 0) { - /* shouldn't return success if we're not sharing */ - BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); vma->vm_region->vm_top = vma->vm_region->vm_end; return 0; } @@ -1106,7 +1107,7 @@ unsigned long do_mmap(struct file *file, * these cases, sharing is handled in the driver or filesystem rather * than here */ - if (vm_flags & VM_MAYSHARE) { + if (is_nommu_shared_mapping(vm_flags)) { struct vm_region *pregion; unsigned long pglen, rpglen, pgend, rpgend, start; @@ -1116,7 +1117,7 @@ unsigned long do_mmap(struct file *file, for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { pregion = rb_entry(rb, struct vm_region, vm_rb); - if (!(pregion->vm_flags & VM_MAYSHARE)) + if (!is_nommu_shared_mapping(pregion->vm_flags)) continue; /* search for overlapping mappings on the same file */ @@ -1597,7 +1598,7 @@ static unsigned long do_mremap(unsigned long addr, if (vma->vm_end != vma->vm_start + old_len) return (unsigned long) -EFAULT; - if (vma->vm_flags & VM_MAYSHARE) + if (is_nommu_shared_mapping(vma->vm_flags)) return (unsigned long) -EPERM; if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) From patchwork Mon Jan 2 16:08:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13086801 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 85A44C4708D for ; Mon, 2 Jan 2023 16:09:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E42E8E0003; Mon, 2 Jan 2023 11:09:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2947B8E0001; Mon, 2 Jan 2023 11:09:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 15C948E0003; Mon, 2 Jan 2023 11:09:12 -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 08DD98E0001 for ; Mon, 2 Jan 2023 11:09:12 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C3B3A1A098D for ; Mon, 2 Jan 2023 16:09:11 +0000 (UTC) X-FDA: 80310343302.20.5D3B627 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 36BFE2000F for ; Mon, 2 Jan 2023 16:09:10 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=daJhQqnQ; spf=pass (imf03.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=1672675750; 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=/KmBUrtOn1BMOkdcQJmrOKgCHuRXJmWehWSjwC/MXzU=; b=Wy9RpxQlUW2dOhuBhPDcl9Fly3VqmqrBEiqwVHMj8txJp/NKHqkgvoUEoCTXRyXrmFhcR8 CCtLolXxqfulAODyKuT9Y69v1Yow/zGIOnJ/lkCJ+cw+bxTZQbbsbU29prMhdNgEAjptBp 85QaXDBcAK2imapzN+U6mZ6fwmCmgDc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=daJhQqnQ; spf=pass (imf03.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=1672675750; a=rsa-sha256; cv=none; b=XHl4reaP7+SAd+KjyRwA7MIwZF5utLZk6IoVMs4lmj5EbROA3JKPnhKHcU+egnwuBL9qHX HD/e7FYGXiJk1L3mj7xIZFLlOMNNBj5dQ8zeUOjtH8NPLUnTEHY00S8UGKe+Zgk+kUVu4s kYXSfFEZs1icyssZ+vX3c4XDcTeMz7M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672675749; 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=/KmBUrtOn1BMOkdcQJmrOKgCHuRXJmWehWSjwC/MXzU=; b=daJhQqnQ3LcDFF1M+OEvkNH4s4RDWQ1FRAmOlCXMSQgVvAGibXEE0+4lowavQtAwj+LJLi qFsnDmyp3uH6sLRn2ZWZ0cBE8Doz4Y70C1PIFe7REJXY2o9TRvOkbf5i9xqaEvqMmBRPl/ s/WcJU6+K5Frit837hWLr9/27HA6R7s= 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-280-xSeEzPZEPJqZEG66yU6B_Q-1; Mon, 02 Jan 2023 11:09:06 -0500 X-MC-Unique: xSeEzPZEPJqZEG66yU6B_Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CD3461C08792; Mon, 2 Jan 2023 16:09:05 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.193.209]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8F4E751EF; Mon, 2 Jan 2023 16:09:03 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, David Hildenbrand , Andrew Morton , Arnd Bergmann , Greg Kroah-Hartman , Nicolas Pitre , Jens Axboe , Pavel Begunkov Subject: [PATCH mm-unstable v1 2/3] mm/nommu: don't use VM_MAYSHARE for MAP_PRIVATE mappings Date: Mon, 2 Jan 2023 17:08:55 +0100 Message-Id: <20230102160856.500584-3-david@redhat.com> In-Reply-To: <20230102160856.500584-1-david@redhat.com> References: <20230102160856.500584-1-david@redhat.com> MIME-Version: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zNcS2UsetT4wqvFRc1yp2/qObmvbk8zE1X8+z97NMJNwEV5gxz0VLAfcQXYOJF C00Zrdd4 lbVQbGsIJ0BxappHt5vs836UAlFoOIIW7khICG2obFVzisT5jpCDH/d+Dxrl9rOp6yez8 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: Let's stop using VM_MAYSHARE for MAP_PRIVATE mappings and use VM_MAYOVERLAY instead. Rewrite determine_vm_flags() to make the whole logic easier to digest, and to cleanly separate MAP_PRIVATE vs. MAP_SHARED. No functional change intended. Signed-off-by: David Hildenbrand --- include/linux/mm.h | 7 ++++++- mm/nommu.c | 51 +++++++++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 28568a92e5df..510f2e7cccdb 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -276,7 +276,12 @@ extern unsigned int kobjsize(const void *objp); #define VM_MAYSHARE 0x00000080 #define VM_GROWSDOWN 0x00000100 /* general info on the segment */ +#ifdef CONFIG_MMU #define VM_UFFD_MISSING 0x00000200 /* missing pages tracking */ +#else /* CONFIG_MMU */ +#define VM_MAYOVERLAY 0x00000200 /* nommu: R/O MAP_PRIVATE mapping that might overlay a file mapping */ +#define VM_UFFD_MISSING 0 +#endif /* CONFIG_MMU */ #define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */ #define VM_UFFD_WP 0x00001000 /* wrprotect pages tracking */ @@ -1358,7 +1363,7 @@ static inline bool is_nommu_shared_mapping(vm_flags_t flags) * ptrace does not apply. Note that there is no mprotect() to upgrade * write permissions later. */ - return flags & VM_MAYSHARE; + return flags & (VM_MAYSHARE | VM_MAYOVERLAY); } #endif diff --git a/mm/nommu.c b/mm/nommu.c index 6c4bdc07a776..5c628c868648 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -892,29 +892,36 @@ static unsigned long determine_vm_flags(struct file *file, unsigned long vm_flags; vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); - /* vm_flags |= mm->def_flags; */ - if (!(capabilities & NOMMU_MAP_DIRECT)) { - /* attempt to share read-only copies of mapped file chunks */ + if (!file) { + /* + * MAP_ANONYMOUS. MAP_SHARED is mapped to MAP_PRIVATE, because + * there is no fork(). + */ vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; - if (file && !(prot & PROT_WRITE)) - vm_flags |= VM_MAYSHARE; + } else if (flags & MAP_PRIVATE) { + /* MAP_PRIVATE file mapping */ + if (capabilities & NOMMU_MAP_DIRECT) + vm_flags |= (capabilities & NOMMU_VMFLAGS); + else + vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; + + if (!(prot & PROT_WRITE) && !current->ptrace) + /* + * R/O private file mapping which cannot be used to + * modify memory, especially also not via active ptrace + * (e.g., set breakpoints) or later by upgrading + * permissions (no mprotect()). We can try overlaying + * the file mapping, which will work e.g., on chardevs, + * ramfs/tmpfs/shmfs and romfs/cramf. + */ + vm_flags |= VM_MAYOVERLAY; } else { - /* overlay a shareable mapping on the backing device or inode - * if possible - used for chardevs, ramfs/tmpfs/shmfs and - * romfs/cramfs */ - vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS); - if (flags & MAP_SHARED) - vm_flags |= VM_SHARED; + /* MAP_SHARED file mapping: NOMMU_MAP_DIRECT is set. */ + vm_flags |= VM_SHARED | VM_MAYSHARE | + (capabilities & NOMMU_VMFLAGS); } - /* refuse to let anyone share private mappings with this process if - * it's being traced - otherwise breakpoints set in it may interfere - * with another untraced process - */ - if ((flags & MAP_PRIVATE) && current->ptrace) - vm_flags &= ~VM_MAYSHARE; - return vm_flags; } @@ -952,9 +959,11 @@ static int do_mmap_private(struct vm_area_struct *vma, void *base; int ret, order; - /* invoke the file's mapping function so that it can keep track of - * shared mappings on devices or memory - * - VM_MAYSHARE will be set if it may attempt to share + /* + * Invoke the file's mapping function so that it can keep track of + * shared mappings on devices or memory. VM_MAYOVERLAY will be set if + * it may attempt to share, which will make is_nommu_shared_mapping() + * happy. */ if (capabilities & NOMMU_MAP_DIRECT) { ret = call_mmap(vma->vm_file, vma); From patchwork Mon Jan 2 16:08:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13086803 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 584C0C54E76 for ; Mon, 2 Jan 2023 16:09:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2F918E0006; Mon, 2 Jan 2023 11:09:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DE1878E0001; Mon, 2 Jan 2023 11:09:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C7F4D8E0006; Mon, 2 Jan 2023 11:09:17 -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 AD7A58E0001 for ; Mon, 2 Jan 2023 11:09:17 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8B0321A0995 for ; Mon, 2 Jan 2023 16:09:17 +0000 (UTC) X-FDA: 80310343554.27.5050686 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id F2F57A000E for ; Mon, 2 Jan 2023 16:09:15 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="Lxe/2IR6"; spf=pass (imf25.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1672675756; a=rsa-sha256; cv=none; b=BV8xX/JaoG+imqTlP3udJwXcCI0FsHgHY+6AT1NU4sSavSx/XgbBjV5dPynyByyMQzTmPy XzBm2ku/CH3wWDLJ03Qx+829KsRTA4vniaGtoch8WXO+iUC8dqywCcfyzg+pEgqhOG8goK lvE5Pg2ngQryVhq2f2OEAjUCrtoxNJ4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="Lxe/2IR6"; spf=pass (imf25.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1672675756; 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=Sh73hBCrQns8aoevVV+OOnfi6R1HtUp1NLv5DeAjtKc=; b=Di4JCKLOW6LrS4QYwJwxn+fh3fr5LRDscSZL523NsOuTjPC9yelFSGziP6YntxCgrD75e+ KJKAdUiLdTNrPAl05bsBpW3JEJR7jQFKys4mkP0qX+X65VJPap+4C4JaW+2CgSSPU1QxhJ 55bIAgNXChg8CxKyFi2BEeKGnTmhwYs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672675755; 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=Sh73hBCrQns8aoevVV+OOnfi6R1HtUp1NLv5DeAjtKc=; b=Lxe/2IR66UfxphV5VUq8KdTb6pAnT8h4ANievpAIeuIuy41/HsFwOaFVxX+EvYncxbjkcx f4OF6zdFKEnJje/FPV9W65JY2IpJ+9Y6ByeVU2BlDWFdqffT/P6eN7BWeTh+Do9aI7OaWI n3tRyoACbCG0GSKxhfZjbXSXar85O54= 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-517-h6RgIbbbPnKtMqdUN9kSDg-1; Mon, 02 Jan 2023 11:09:09 -0500 X-MC-Unique: h6RgIbbbPnKtMqdUN9kSDg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 48408811E6E; Mon, 2 Jan 2023 16:09:08 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.193.209]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3D18D35455; Mon, 2 Jan 2023 16:09:06 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, David Hildenbrand , Andrew Morton , Arnd Bergmann , Greg Kroah-Hartman , Nicolas Pitre , Jens Axboe , Pavel Begunkov Subject: [PATCH mm-unstable v1 3/3] drivers/misc/open-dice: don't touch VM_MAYSHARE Date: Mon, 2 Jan 2023 17:08:56 +0100 Message-Id: <20230102160856.500584-4-david@redhat.com> In-Reply-To: <20230102160856.500584-1-david@redhat.com> References: <20230102160856.500584-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspam-User: X-Rspamd-Queue-Id: F2F57A000E X-Rspamd-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fa/xScCBllrvpjTLj3YdMytfVkosTDsPGn30KQLyaJk+2NRVe8rK5KCqYt/ 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: A MAP_SHARED mapping always has VM_MAYSHARE set, and writable (VM_MAYWRITE) MAP_SHARED mappings have VM_SHARED set as well. To identify a MAP_SHARED mapping, it's sufficient to look at VM_MAYSHARE. We cannot have VM_MAYSHARE|VM_WRITE mappings without having VM_SHARED set. Consequently, current code will never actually end up clearing VM_MAYSHARE and that code is confusing, because nobody is supposed to mess with VM_MAYWRITE. Let's clean it up and restructure the code. No functional change intended. Cc: Arnd Bergmann Cc: Greg Kroah-Hartman Signed-off-by: David Hildenbrand --- drivers/misc/open-dice.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/misc/open-dice.c b/drivers/misc/open-dice.c index c61be3404c6f..9dda47b3fd70 100644 --- a/drivers/misc/open-dice.c +++ b/drivers/misc/open-dice.c @@ -90,15 +90,13 @@ static int open_dice_mmap(struct file *filp, struct vm_area_struct *vma) { struct open_dice_drvdata *drvdata = to_open_dice_drvdata(filp); - /* Do not allow userspace to modify the underlying data. */ - if ((vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_SHARED)) - return -EPERM; - - /* Ensure userspace cannot acquire VM_WRITE + VM_SHARED later. */ - if (vma->vm_flags & VM_WRITE) - vma->vm_flags &= ~VM_MAYSHARE; - else if (vma->vm_flags & VM_SHARED) + if (vma->vm_flags & VM_MAYSHARE) { + /* Do not allow userspace to modify the underlying data. */ + if (vma->vm_flags & VM_WRITE) + return -EPERM; + /* Ensure userspace cannot acquire VM_WRITE later. */ vma->vm_flags &= ~VM_MAYWRITE; + } /* Create write-combine mapping so all clients observe a wipe. */ vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);