linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ming Lin <mlin@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>,
	Hugh Dickins <hughd@google.com>, Simon Ser <contact@emersion.fr>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Ming Lin <mlin@kernel.org>
Subject: [PATCH 2/2] mm: adds NOSIGBUS extension for out-of-band shmem read
Date: Tue,  1 Jun 2021 16:22:33 -0700	[thread overview]
Message-ID: <1622589753-9206-3-git-send-email-mlin@kernel.org> (raw)
In-Reply-To: <1622589753-9206-1-git-send-email-mlin@kernel.org>

Adds new flag MAP_NOSIGBUS of mmap() to specify the behavior of
"don't SIGBUS on read beyond i_size". This flag is only allowed
for read only shmem mapping.

If you use MAP_NOSIGBUS, and you access pages that don't have a backing
store, you will get zero pages, and they will NOT BE SYNCHRONIZED with
the backing store possibly later being updated.

Any user that uses MAP_NOSIGBUS had better just accept that it's not
compatible with expanding the shmem backing store later.

Signed-off-by: Ming Lin <mlin@kernel.org>
---
 include/linux/mm.h                     |  2 ++
 include/linux/mman.h                   |  1 +
 include/uapi/asm-generic/mman-common.h |  1 +
 mm/mmap.c                              |  3 +++
 mm/shmem.c                             | 17 ++++++++++++++++-
 5 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index e9d67bc..5d0e0dc 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -373,6 +373,8 @@ int __add_to_page_cache_locked(struct page *page, struct address_space *mapping,
 # define VM_UFFD_MINOR		VM_NONE
 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
 
+#define VM_NOSIGBUS		VM_FLAGS_BIT(38)	/* Do not SIGBUS on out-of-band shmem read */
+
 /* Bits set in the VMA until the stack is in its final location */
 #define VM_STACK_INCOMPLETE_SETUP	(VM_RAND_READ | VM_SEQ_READ)
 
diff --git a/include/linux/mman.h b/include/linux/mman.h
index b2cbae9..c966b08 100644
--- a/include/linux/mman.h
+++ b/include/linux/mman.h
@@ -154,6 +154,7 @@ static inline bool arch_validate_flags(unsigned long flags)
 	       _calc_vm_trans(flags, MAP_DENYWRITE,  VM_DENYWRITE ) |
 	       _calc_vm_trans(flags, MAP_LOCKED,     VM_LOCKED    ) |
 	       _calc_vm_trans(flags, MAP_SYNC,	     VM_SYNC      ) |
+	       _calc_vm_trans(flags, MAP_NOSIGBUS,   VM_NOSIGBUS  ) |
 	       arch_calc_vm_flag_bits(flags);
 }
 
diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h
index f94f65d..55f4be0 100644
--- a/include/uapi/asm-generic/mman-common.h
+++ b/include/uapi/asm-generic/mman-common.h
@@ -29,6 +29,7 @@
 #define MAP_HUGETLB		0x040000	/* create a huge page mapping */
 #define MAP_SYNC		0x080000 /* perform synchronous page faults for the mapping */
 #define MAP_FIXED_NOREPLACE	0x100000	/* MAP_FIXED which doesn't unmap underlying mapping */
+#define MAP_NOSIGBUS		0x200000	/* do not SIGBUS on out-of-band shmem read */
 
 #define MAP_UNINITIALIZED 0x4000000	/* For anonymous mmap, memory could be
 					 * uninitialized */
diff --git a/mm/mmap.c b/mm/mmap.c
index 096bba4..69cd856 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1419,6 +1419,9 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
 	if (!len)
 		return -EINVAL;
 
+	if ((flags & MAP_NOSIGBUS) && ((prot & PROT_WRITE) || !shmem_file(file)))
+		return -EINVAL;
+
 	/*
 	 * Does the application expect PROT_READ to imply PROT_EXEC?
 	 *
diff --git a/mm/shmem.c b/mm/shmem.c
index 5d46611..5d15b08 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1812,7 +1812,22 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
 repeat:
 	if (sgp <= SGP_CACHE &&
 	    ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
-		return -EINVAL;
+		if (!vma || !(vma->vm_flags & VM_NOSIGBUS))
+			return -EINVAL;
+
+		vma->vm_flags |= VM_MIXEDMAP;
+		/*
+		 * Get zero page for MAP_NOSIGBUS mapping, which isn't
+                 * coherent wrt shmem contents that are expanded and
+		 * filled in later.
+		 */
+		error = vm_insert_page(vma, (unsigned long)vmf->address,
+					ZERO_PAGE(0));
+		if (error)
+			return error;
+
+		*fault_type = VM_FAULT_NOPAGE;
+		return 0;
 	}
 
 	sbinfo = SHMEM_SB(inode->i_sb);
-- 
1.8.3.1


  parent reply	other threads:[~2021-06-01 23:22 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-01 23:22 [PATCH 0/2] mm: adds MAP_NOSIGBUS extension for shmem read Ming Lin
2021-06-01 23:22 ` [PATCH 1/2] mm: make "vm_flags" be an u64 Ming Lin
2021-06-02  1:58   ` kernel test robot
2021-06-02  2:06   ` kernel test robot
2021-06-01 23:22 ` Ming Lin [this message]
2021-06-02  0:16   ` [PATCH 2/2] mm: adds NOSIGBUS extension for out-of-band shmem read Linus Torvalds
2021-06-02  1:06     ` Ming Lin
2021-06-02  2:13     ` Hugh Dickins
2021-06-02  2:02   ` kernel test robot
2021-06-02  3:49   ` Hugh Dickins
2021-06-03  0:05     ` Ming Lin
2021-06-03  0:46       ` Hugh Dickins
2021-06-03 18:25         ` Linus Torvalds
2021-06-03 19:07           ` Hugh Dickins
2021-06-03 19:12             ` Linus Torvalds
2021-06-03 19:15               ` Linus Torvalds
2021-06-03 19:24               ` Andy Lutomirski
2021-06-03 19:35                 ` Simon Ser
2021-06-03 19:57         ` Ming Lin
2021-06-02  9:30   ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1622589753-9206-3-git-send-email-mlin@kernel.org \
    --to=mlin@kernel.org \
    --cc=contact@emersion.fr \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).