Re: [PATCH 2/2] mm: adds NOSIGBUS extension for out-of-band shmem read

From: Linus Torvalds
Date: Thu Jun 03 2021 - 14:25:42 EST


On Wed, Jun 2, 2021 at 5:46 PM Hugh Dickins <hughd@xxxxxxxxxx> wrote:
>
> Ideally you can simply call do_anonymous_page() from __do_fault()
> in the VM_FAULT_SIGBUS on VM_NOSIGBUS case.

Heh.

We're actually then back to my original patch.

That one doesn't handle shared mappings (even read-only ones), for the
simple reason that do_anonymous_page() refuses to insert anonymous
pages into a shared mapping, and has

/* File mapping without ->vm_ops ? */
if (vma->vm_flags & VM_SHARED)
return VM_FAULT_SIGBUS;

at the very top.

But yes, if we just remove that check, I think my original patch
should actually "JustWork(tm)".

I'm attaching it again, with old name and old commentary (ie that

/* FIXME! We don't have a VM_NOFAULT bit */

should just be replaced with that VM_NOSIGBUS bit instead, and the
#if'ed out region should be enabled.

Oh, and we need to think hard about one more case: mprotect().

In particular, I think the attached patch fails horribly for the case
of a shared mapping that starts out read-only, then inserts a zero
page, then somebody does mprotect(MAP_WRITE), and then writes to the
page. I haven't checked what the write protect fault handler does, but
I think that for a shared mapping it will just make the page dirty and
writable.

Which would be horribly wrong for VM_NOSIGBUS.

So that support infrastructure that adds MAP_NOSIGBUS, and checks that
it is only done on a read-only mapping, also has to make sure that it
clears the VM_MAYWRITE bit when it sets VM_NOSIGBUS.

That way mprotect can't then later make it writable.

Hugh, comments on this approach?

Again: this patch is my *OLD* one, I didn't try to update it to the
new world order. It requires

- Ming's MAP_NOSIGBUS ccode

- removal of that "File mapping without ->vm_ops" case

- that FIXME fixed and name updated

- and that VM_MAYWRITE clearing if VM_NOSIGBUS is set, to avoid the
mprotect issue.

Hmm?

Linus
mm/memory.c | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 550405fc3b5e..bbede6b52f7a 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4312,10 +4312,21 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
}

if (!vmf->pte) {
- if (vma_is_anonymous(vmf->vma))
- return do_anonymous_page(vmf);
- else
- return do_fault(vmf);
+ if (!vma_is_anonymous(vmf->vma)) {
+ vm_fault_t ret = do_fault(vmf);
+ if (ret & VM_FAULT_RETRY)
+ return ret;
+ if (!(ret & VM_FAULT_SIGBUS))
+ return ret;
+/* FIXME! We don't have a VM_NOFAULT bit */
+#if 0
+ /* See if we should turn a SIGBUS into an anonymous page */
+ if (!(vma->vm_flags & VM_NOFAULT))
+ return ret;
+#endif
+/* Fall back on do_anonymous_page() instead of SIGBUS */
+ }
+ return do_anonymous_page(vmf);
}

if (!pte_present(vmf->orig_pte))