Skip to contentSkip to conversation

userfaultfd: wake on unregister for minor faults as well as missing

PATCH In review Axel Rasmussen 3 participants 4 messages Oct 25, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 userfaultfd: wake on unregister for minor faults as well as missing

This was an overlooked edge case when minor faults were added. In general, minor faults have the same rough edge here as missing faults:
if we unregister while there are waiting threads, they will just remain waiting forever, as there is no way for userspace to wake them after unregistration. To work around this, userspace needs to carefully wake everything before unregistering.

So, wake for minor faults just like we already do for missing faults as part of the unregistration process.

Fixes 7677f7fd8be7 ("userfaultfd: add minor fault registration mode")
---
 fs/userfaultfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/userfaultfd.c +1 -1
index 07c81ab3fd4d..7daee4b9481c 100644
@@ -1606,7 +1606,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
 			start = vma->vm_start;
 		vma_end = min(end, vma->vm_end);
 
-		if (userfaultfd_missing(vma)) {
+		if (userfaultfd_missing(vma) || userfaultfd_minor(vma)) {
 			/*
 			 * Wake any concurrent pending userfault while
 			 * we unregister, so they will not hang
Signature · 1 line
2.38.0.135.g90850a2211-goog
#1 lore raw
Quoted 33 lines · Axel Rasmussen

This was an overlooked edge case when minor faults were added. In general, minor faults have the same rough edge here as missing faults:
if we unregister while there are waiting threads, they will just remain waiting forever, as there is no way for userspace to wake them after unregistration. To work around this, userspace needs to carefully wake everything before unregistering.

So, wake for minor faults just like we already do for missing faults as part of the unregistration process.

Fixes 7677f7fd8be7 ("userfaultfd: add minor fault registration mode")
---
 fs/userfaultfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/userfaultfd.c +1 -1
index 07c81ab3fd4d..7daee4b9481c 100644
@@ -1606,7 +1606,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
 			start = vma->vm_start;
 		vma_end = min(end, vma->vm_end);
 
-		if (userfaultfd_missing(vma)) {
+		if (userfaultfd_missing(vma) || userfaultfd_minor(vma)) {
 			/*
 			 * Wake any concurrent pending userfault while
 			 * we unregister, so they will not hang
Signature · 1 line
2.38.0.135.g90850a2211-goog

Thanks, Axel. Is wr-protect mode also prone to this? Would a test case help too?

Signature · 1 line
Peter Xu
#2 lore raw
Quoted 28 lines · Peter Xu
Quoted 26 lines · Axel Rasmussen

This was an overlooked edge case when minor faults were added. In general, minor faults have the same rough edge here as missing faults:
if we unregister while there are waiting threads, they will just remain waiting forever, as there is no way for userspace to wake them after unregistration. To work around this, userspace needs to carefully wake everything before unregistering.

So, wake for minor faults just like we already do for missing faults as part of the unregistration process.

Fixes 7677f7fd8be7 ("userfaultfd: add minor fault registration mode")
---
 fs/userfaultfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/userfaultfd.c +0 -0
index 07c81ab3fd4d..7daee4b9481c 100644
@@ -1606,7 +1606,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
                      start = vma->vm_start;
              vma_end = min(end, vma->vm_end);
 
Quoted 10 lines
Quoted 7 lines
fs/userfaultfd.c +1 -2
-             if (userfaultfd_missing(vma)) {
+             if (userfaultfd_missing(vma) || userfaultfd_minor(vma)) {
                      /*
                       * Wake any concurrent pending userfault while
                       * we unregister, so they will not hang
--

2.38.0.135.g90850a2211-goog

Thanks, Axel. Is wr-protect mode also prone to this? Would a test case help too?

I'm not quite as familiar with uffd-wp, but I think so? At minimum, it seems like waking can't *hurt*, and it would simplify the check slightly -- if (userfaultfd_armed(vma)) {}

It would also mean if we add yet another registration mode in the future, we wouldn't forget to update this.

I'll send a v2 to address both points.

Quoted 4 lines

--
Peter Xu

#3 lore raw
In reply to Axel Rasmussen

Thanks for fixing this issue.

Quoted 7 lines · Axel Rasmussen

This was an overlooked edge case when minor faults were added. In general, minor faults have the same rough edge here as missing faults:
if we unregister while there are waiting threads, they will just remain waiting forever, as there is no way for userspace to wake them after unregistration. To work around this, userspace needs to carefully wake everything before unregistering.

Actually, WAKE ioctl doesn't check if the provided range is registered with userfaultfd or not. So, it's possible to wake the waiting threads after unregisteration.

But, in this context, missing faults are the same as minor faults (and wp too?). Therefore, waking the waiting threads as part of unregistration is expected.

Quoted 28 lines

So, wake for minor faults just like we already do for missing faults as part of the unregistration process.

Fixes 7677f7fd8be7 ("userfaultfd: add minor fault registration mode")
---
 fs/userfaultfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/userfaultfd.c +1 -2
index 07c81ab3fd4d..7daee4b9481c 100644
@@ -1606,7 +1606,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
                        start = vma->vm_start;
                vma_end = min(end, vma->vm_end);
 
-               if (userfaultfd_missing(vma)) {
+               if (userfaultfd_missing(vma) || userfaultfd_minor(vma)) {
                        /*
                         * Wake any concurrent pending userfault while
                         * we unregister, so they will not hang
--

2.38.0.135.g90850a2211-goog