Skip to contentSkip to conversation

userfaultfd man page updates

PATCH Superseded Axel Rasmussen 3 participants 32 messages Sep 19, 2023 – Oct 10, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover userfaultfd man page updates

Various updates for userfaultfd man pages. To summarize the changes:

- Correctly / fully describe the two-step feature support handshake process.
- Describe new UFFDIO_POISON ioctl.
- Other small improvements (missing ioctls, error codes, etc).

Axel Rasmussen (10):
userfaultfd.2: briefly mention two-step feature handshake process userfaultfd.2: reword to account for new fault resolution ioctls userfaultfd.2: comment on feature detection in the example program ioctl_userfaultfd.2: fix a few trivial mistakes ioctl_userfaultfd.2: describe two-step feature handshake ioctl_userfaultfd.2: describe missing UFFDIO_API feature flags ioctl_userfaultfd.2: correct and update UFFDIO_API ioctl error codes ioctl_userfaultfd.2: clarify the state of the uffdio_api structure on
error
ioctl_userfaultfd.2: fix / update UFFDIO_REGISTER error code list ioctl_userfaultfd.2: document new UFFDIO_POISON ioctl

 man2/ioctl_userfaultfd.2 | 254 ++++++++++++++++++++++++++++++++-------
 man2/userfaultfd.2       |  15 ++-
 2 files changed, 220 insertions(+), 49 deletions(-)

--
2.42.0.459.ge4e396fd5e-goog

#1 lore raw

PATCH 1/10 userfaultfd.2: briefly mention two-step feature handshake process

This process is critical for programs which depend on extra features, so it's worth mentioning here.

Future commits will much more fully describe it in ioctl_userfaultfd.2.

---
 man2/userfaultfd.2 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
man2/userfaultfd.2 +3 -2
index 40354065c..1b2af22f9 100644
@@ -200,8 +200,9 @@ the application must enable it using the
 .B UFFDIO_API
 .BR ioctl (2)
 operation.
-This operation allows a handshake between the kernel and user space
-to determine the API version and supported features.
+This operation allows a two-step handshake between the kernel and user space
+to determine what API version and features the kernel supports,
+and then to enable those features user space wants.
 This operation must be performed before any of the other
 .BR ioctl (2)
 operations described below (or those operations fail with the
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#2 lore raw

Re: [PATCH 01/10] userfaultfd.2: briefly mention two-step feature handshake process

Quoted 6 lines · Axel Rasmussen

This process is critical for programs which depend on extra features, so it's worth mentioning here.

Future commits will much more fully describe it in ioctl_userfaultfd.2.

Patch applied. Thanks,
Alex

Quoted 23 lines
---
 man2/userfaultfd.2 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
man2/userfaultfd.2 +3 -2
index 40354065c..1b2af22f9 100644
@@ -200,8 +200,9 @@ the application must enable it using the
 .B UFFDIO_API
 .BR ioctl (2)
 operation.
-This operation allows a handshake between the kernel and user space
-to determine the API version and supported features.
+This operation allows a two-step handshake between the kernel and user space
+to determine what API version and features the kernel supports,
+and then to enable those features user space wants.
 This operation must be performed before any of the other
 .BR ioctl (2)
 operations described below (or those operations fail with the
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Reviewed-by on v1 1/10
#3 lore raw

Re: [PATCH 01/10] userfaultfd.2: briefly mention two-step feature handshake process

In reply to Axel Rasmussen
Quoted 6 lines · Axel Rasmussen

This process is critical for programs which depend on extra features, so it's worth mentioning here.

Future commits will much more fully describe it in ioctl_userfaultfd.2.

Quoted 24 lines
---
 man2/userfaultfd.2 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
man2/userfaultfd.2 +3 -2
index 40354065c..1b2af22f9 100644
@@ -200,8 +200,9 @@ the application must enable it using the
 .B UFFDIO_API
 .BR ioctl (2)
 operation.
-This operation allows a handshake between the kernel and user space
-to determine the API version and supported features.
+This operation allows a two-step handshake between the kernel and user space
+to determine what API version and features the kernel supports,
+and then to enable those features user space wants.
 This operation must be performed before any of the other
 .BR ioctl (2)
 operations described below (or those operations fail with the
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
#4 lore raw

PATCH 2/10 userfaultfd.2: reword to account for new fault resolution ioctls

In reply to Axel Rasmussen

Basically, reword the sentence to clarify that this isn't a complete list. I don't believe it's worth maintaining a fully complete list here, instead ioctl_userfaultfd.2 is the place to do that. Let this just be an example.

---
 man2/userfaultfd.2 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man2/userfaultfd.2 +2 -1
index 1b2af22f9..00d94e514 100644
@@ -222,11 +222,12 @@ operation,
 a page fault occurring in the requested memory range, and satisfying
 the mode defined at the registration time, will be forwarded by the kernel to
 the user-space application.
-The application can then use the
+The application can then use various (e.g.
 .B UFFDIO_COPY ,
 .B UFFDIO_ZEROPAGE ,
 or
 .B UFFDIO_CONTINUE
+)
 .BR ioctl (2)
 operations to resolve the page fault.
 .PP
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#5 lore raw

Re: [PATCH 02/10] userfaultfd.2: reword to account for new fault resolution ioctls

Hi Axel,

Quoted 6 lines · Axel Rasmussen

Basically, reword the sentence to clarify that this isn't a complete list. I don't believe it's worth maintaining a fully complete list here, instead ioctl_userfaultfd.2 is the place to do that. Let this just be an example.

Patch applied.

Thanks,
Alex

Quoted 25 lines
---
 man2/userfaultfd.2 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man2/userfaultfd.2 +2 -1
index 1b2af22f9..00d94e514 100644
@@ -222,11 +222,12 @@ operation,
 a page fault occurring in the requested memory range, and satisfying
 the mode defined at the registration time, will be forwarded by the kernel to
 the user-space application.
-The application can then use the
+The application can then use various (e.g.
 .B UFFDIO_COPY ,
 .B UFFDIO_ZEROPAGE ,
 or
 .B UFFDIO_CONTINUE
+)
 .BR ioctl (2)
 operations to resolve the page fault.
 .PP
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Reviewed-by on v1 2/10
#6 lore raw

Re: [PATCH 02/10] userfaultfd.2: reword to account for new fault resolution ioctls

In reply to Axel Rasmussen
Quoted 6 lines · Axel Rasmussen

Basically, reword the sentence to clarify that this isn't a complete list. I don't believe it's worth maintaining a fully complete list here, instead ioctl_userfaultfd.2 is the place to do that. Let this just be an example.

Quoted 26 lines
---
 man2/userfaultfd.2 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
man2/userfaultfd.2 +2 -1
index 1b2af22f9..00d94e514 100644
@@ -222,11 +222,12 @@ operation,
 a page fault occurring in the requested memory range, and satisfying
 the mode defined at the registration time, will be forwarded by the kernel to
 the user-space application.
-The application can then use the
+The application can then use various (e.g.
 .B UFFDIO_COPY ,
 .B UFFDIO_ZEROPAGE ,
 or
 .B UFFDIO_CONTINUE
+)
 .BR ioctl (2)
 operations to resolve the page fault.
 .PP
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
#7 lore raw

PATCH 3/10 userfaultfd.2: comment on feature detection in the example program

In reply to Axel Rasmussen

The example program doesn't depend on any extra features, so it does not make use of the two-step feature handshake process. This is fine, but it might set a bad example for programs which *do* depend on specific features (e.g. they may conclude they don't need to do anything to enable / detect them).

No need to make the example program more complicated: let's just add a comment indicating why we do it the way we do it in the example, and describing briefly what a more complicated program would need to do instead.

The comment is kept rather brief; a full description of this feature will be included in ioctl_userfaultfd.2 instead.

---
 man2/userfaultfd.2 | 7 +++++++
 1 file changed, 7 insertions(+)
man2/userfaultfd.2 +7 -0
index 00d94e514..b2b79f61d 100644
@@ -881,6 +881,13 @@ main(int argc, char *argv[])
     if (uffd == \-1)
         err(EXIT_FAILURE, "userfaultfd");
 \&
+    /* NOTE: Two-step feature handshake is not needed here, since this
+       example doesn't require any specific features.
+
+       Programs that *do* should call UFFDIO_API twice: once with
+       `features = 0` to detect features supported by this kernel, and
+       again with the subset of features the program actually wants to
+       enable. */
     uffdio_api.api = UFFD_API;
     uffdio_api.features = 0;
     if (ioctl(uffd, UFFDIO_API, &uffdio_api) == \-1)
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#8 lore raw

Re: [PATCH 03/10] userfaultfd.2: comment on feature detection in the example program

Hi Axel,

Quoted 11 lines · Axel Rasmussen

The example program doesn't depend on any extra features, so it does not make use of the two-step feature handshake process. This is fine, but it might set a bad example for programs which *do* depend on specific features (e.g. they may conclude they don't need to do anything to enable / detect them).

No need to make the example program more complicated: let's just add a comment indicating why we do it the way we do it in the example, and describing briefly what a more complicated program would need to do instead.

Quoted 4 lines

The comment is kept rather brief; a full description of this feature will be included in ioctl_userfaultfd.2 instead.

Patch applied.

Thanks,
Alex

Quoted 25 lines
---
 man2/userfaultfd.2 | 7 +++++++
 1 file changed, 7 insertions(+)
man2/userfaultfd.2 +7 -0
index 00d94e514..b2b79f61d 100644
@@ -881,6 +881,13 @@ main(int argc, char *argv[])
     if (uffd == \-1)
         err(EXIT_FAILURE, "userfaultfd");
 \&
+    /* NOTE: Two-step feature handshake is not needed here, since this
+       example doesn't require any specific features.
+
+       Programs that *do* should call UFFDIO_API twice: once with
+       `features = 0` to detect features supported by this kernel, and
+       again with the subset of features the program actually wants to
+       enable. */
     uffdio_api.api = UFFD_API;
     uffdio_api.features = 0;
     if (ioctl(uffd, UFFDIO_API, &uffdio_api) == \-1)
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Reviewed-by on v1 3/10
#9 lore raw

Re: [PATCH 03/10] userfaultfd.2: comment on feature detection in the example program

In reply to Axel Rasmussen
Quoted 11 lines · Axel Rasmussen

The example program doesn't depend on any extra features, so it does not make use of the two-step feature handshake process. This is fine, but it might set a bad example for programs which *do* depend on specific features (e.g. they may conclude they don't need to do anything to enable / detect them).

No need to make the example program more complicated: let's just add a comment indicating why we do it the way we do it in the example, and describing briefly what a more complicated program would need to do instead.

Quoted 4 lines

The comment is kept rather brief; a full description of this feature will be included in ioctl_userfaultfd.2 instead.

Quoted 26 lines
---
 man2/userfaultfd.2 | 7 +++++++
 1 file changed, 7 insertions(+)
man2/userfaultfd.2 +7 -0
index 00d94e514..b2b79f61d 100644
@@ -881,6 +881,13 @@ main(int argc, char *argv[])
     if (uffd == \-1)
         err(EXIT_FAILURE, "userfaultfd");
 \&
+    /* NOTE: Two-step feature handshake is not needed here, since this
+       example doesn't require any specific features.
+
+       Programs that *do* should call UFFDIO_API twice: once with
+       `features = 0` to detect features supported by this kernel, and
+       again with the subset of features the program actually wants to
+       enable. */
     uffdio_api.api = UFFD_API;
     uffdio_api.features = 0;
     if (ioctl(uffd, UFFDIO_API, &uffdio_api) == \-1)
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
#10 lore raw

PATCH 4/10 ioctl_userfaultfd.2: fix a few trivial mistakes

In reply to Axel Rasmussen

- Fix missing paragraph tag. The lack of this tag yielded no blank line in the rendere dpage, which is inconsistent with style elsewhere.

- The description of UFFDIO_WRITEPROTECT was a sentence fragment; the last half of the sentence was left out by mistake. Add it in to fix the issue.

- move UFFDIO_WRITEPROTECT 'since' to its own line All other ioctls note the kernel version introduced on a separate line from the ioctl name. Update UFFDIO_WRITEPROTECT to match the existing style.

---
 man2/ioctl_userfaultfd.2 | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
man2/ioctl_userfaultfd.2 +4 -1
index b5281ec4c..339adf8fe 100644
@@ -31,6 +31,7 @@ using calls of the form:
 ioctl(fd, cmd, argp);
 .EE
 .in
+.PP
 In the above,
 .I fd
 is a file descriptor referring to a userfaultfd object,
@@ -351,6 +352,7 @@ operation is supported.
 .B 1 << _UFFDIO_WRITEPROTECT
 The
 .B UFFDIO_WRITEPROTECT
+operation is supported.
 .TP
 .B 1 << _UFFDIO_ZEROPAGE
 The
@@ -693,7 +695,8 @@ field of the
 structure was not a multiple of the system page size; or
 .I len
 was zero; or the specified range was otherwise invalid.
-.SS UFFDIO_WRITEPROTECT (Since Linux 5.7)
+.SS UFFDIO_WRITEPROTECT
+(Since Linux 5.7.)
 Write-protect or write-unprotect a userfaultfd-registered memory range
 registered with mode
 .BR UFFDIO_REGISTER_MODE_WP .
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#11 lore raw

Re: [PATCH 04/10] ioctl_userfaultfd.2: fix a few trivial mistakes

Quoted 7 lines · Axel Rasmussen

- Fix missing paragraph tag. The lack of this tag yielded no blank line in the rendere dpage, which is inconsistent with style elsewhere.

- The description of UFFDIO_WRITEPROTECT was a sentence fragment; the last half of the sentence was left out by mistake. Add it in to fix the issue.

Quoted 6 lines

- move UFFDIO_WRITEPROTECT 'since' to its own line All other ioctls note the kernel version introduced on a separate line from the ioctl name. Update UFFDIO_WRITEPROTECT to match the existing style.

---

Patch applied.

Thanks,
Alex

Quoted 36 lines
 man2/ioctl_userfaultfd.2 | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
man2/ioctl_userfaultfd.2 +4 -1
index b5281ec4c..339adf8fe 100644
@@ -31,6 +31,7 @@ using calls of the form:
 ioctl(fd, cmd, argp);
 .EE
 .in
+.PP
 In the above,
 .I fd
 is a file descriptor referring to a userfaultfd object,
@@ -351,6 +352,7 @@ operation is supported.
 .B 1 << _UFFDIO_WRITEPROTECT
 The
 .B UFFDIO_WRITEPROTECT
+operation is supported.
 .TP
 .B 1 << _UFFDIO_ZEROPAGE
 The
@@ -693,7 +695,8 @@ field of the
 structure was not a multiple of the system page size; or
 .I len
 was zero; or the specified range was otherwise invalid.
-.SS UFFDIO_WRITEPROTECT (Since Linux 5.7)
+.SS UFFDIO_WRITEPROTECT
+(Since Linux 5.7.)
 Write-protect or write-unprotect a userfaultfd-registered memory range
 registered with mode
 .BR UFFDIO_REGISTER_MODE_WP .
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#12 lore raw

PATCH 5/10 ioctl_userfaultfd.2: describe two-step feature handshake

In reply to Axel Rasmussen

Fully describe how UFFDIO_API can be used to perform a two-step feature handshake, and also note the case where this isn't necessary (programs which don't depend on any extra features).

This lets us clean up an old FIXME asking for this to be described.

---
 man2/ioctl_userfaultfd.2 | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)
man2/ioctl_userfaultfd.2 +21 -16
index 339adf8fe..e91a1dfc8 100644
@@ -83,7 +83,6 @@ struct uffdio_api {
 The
 .I api
 field denotes the API version requested by the application.
-.PP
 The kernel verifies that it can support the requested API version,
 and sets the
 .I features
@@ -93,6 +92,25 @@ fields to bit masks representing all the available features and the generic
 .BR ioctl (2)
 operations available.
 .PP
+After Linux 4.11,
+applications should use the
+.I features
+field to perform a two-step handshake.
+First,
+.BR UFFDIO_API
+is called with the
+.I features
+field set to zero.
+The kernel responsds by setting all supported feature bits.
+.PP
+Applications which do not require any specific features
+can begin using the userfaultfd immediately.
+Applications which do need specific features
+should call
+.BR UFFDIO_API
+again with a subset of the reported feature bits set
+to enable those features.
+.PP
 Before Linux 4.11, the
 .I features
 field must be initialized to zero before the call to
@@ -102,24 +120,11 @@ and zero (i.e., no feature bits) is placed in the
 field by the kernel upon return from
 .BR ioctl (2).
 .PP
-Starting from Linux 4.11, the
-.I features
-field can be used to ask whether particular features are supported
-and explicitly enable userfaultfd features that are disabled by default.
-The kernel always reports all the available features in the
-.I features
-field.
-.PP
-To enable userfaultfd features the application should set
-a bit corresponding to each feature it wants to enable in the
-.I features
-field.
-If the kernel supports all the requested features it will enable them.
-Otherwise it will zero out the returned
+If the application sets unsupported feature bits,
+the kernel will zero out the returned
 .I uffdio_api
 structure and return
 .BR EINVAL .
-.\" FIXME add more details about feature negotiation and enablement
 .PP
 The following feature bits may be set:
 .TP
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#13 lore raw

Re: [PATCH 05/10] ioctl_userfaultfd.2: describe two-step feature handshake

Hi Axel,

Quoted 24 lines · Axel Rasmussen

Fully describe how UFFDIO_API can be used to perform a two-step feature handshake, and also note the case where this isn't necessary (programs which don't depend on any extra features).

This lets us clean up an old FIXME asking for this to be described.

---
 man2/ioctl_userfaultfd.2 | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)
man2/ioctl_userfaultfd.2 +0 -1
index 339adf8fe..e91a1dfc8 100644
@@ -83,7 +83,6 @@ struct uffdio_api {
 The
 .I api
 field denotes the API version requested by the application.
-.PP
 The kernel verifies that it can support the requested API version,
 and sets the
 .I features

man2/ioctl_userfaultfd.2 fields to bit masks representing all the available features and the generic · line 95

Quoted 4 lines
man2/ioctl_userfaultfd.2 +1 -0
@@ -93,6 +92,25 @@ fields to bit masks representing all the available features and the generic
 .BR ioctl (2)
 operations available.
 .PP
+After Linux 4.11,

"After" to me means that you're not including 4.11. You probably mean "Since", which would be inclusive? Or do you actually mean since 4.12?

In any case, "since" is more commonly used, so I prefer that wording, for consistency.

Thanks,
Alex

Quoted 51 lines
man2/ioctl_userfaultfd.2 +20 -15
+applications should use the
+.I features
+field to perform a two-step handshake.
+First,
+.BR UFFDIO_API
+is called with the
+.I features
+field set to zero.
+The kernel responsds by setting all supported feature bits.
+.PP
+Applications which do not require any specific features
+can begin using the userfaultfd immediately.
+Applications which do need specific features
+should call
+.BR UFFDIO_API
+again with a subset of the reported feature bits set
+to enable those features.
+.PP
 Before Linux 4.11, the
 .I features
 field must be initialized to zero before the call to
@@ -102,24 +120,11 @@ and zero (i.e., no feature bits) is placed in the
 field by the kernel upon return from
 .BR ioctl (2).
 .PP
-Starting from Linux 4.11, the
-.I features
-field can be used to ask whether particular features are supported
-and explicitly enable userfaultfd features that are disabled by default.
-The kernel always reports all the available features in the
-.I features
-field.
-.PP
-To enable userfaultfd features the application should set
-a bit corresponding to each feature it wants to enable in the
-.I features
-field.
-If the kernel supports all the requested features it will enable them.
-Otherwise it will zero out the returned
+If the application sets unsupported feature bits,
+the kernel will zero out the returned
 .I uffdio_api
 structure and return
 .BR EINVAL .
-.\" FIXME add more details about feature negotiation and enablement
 .PP
 The following feature bits may be set:
 .TP
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Reviewed-by on v1 5/10
#14 lore raw

Re: [PATCH 05/10] ioctl_userfaultfd.2: describe two-step feature handshake

In reply to Axel Rasmussen
Quoted 7 lines · Axel Rasmussen

Fully describe how UFFDIO_API can be used to perform a two-step feature handshake, and also note the case where this isn't necessary (programs which don't depend on any extra features).

This lets us clean up an old FIXME asking for this to be described.

Quoted 73 lines
---
 man2/ioctl_userfaultfd.2 | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)
man2/ioctl_userfaultfd.2 +21 -16
index 339adf8fe..e91a1dfc8 100644
@@ -83,7 +83,6 @@ struct uffdio_api {
 The
 .I api
 field denotes the API version requested by the application.
-.PP
 The kernel verifies that it can support the requested API version,
 and sets the
 .I features
@@ -93,6 +92,25 @@ fields to bit masks representing all the available features and the generic
 .BR ioctl (2)
 operations available.
 .PP
+After Linux 4.11,
+applications should use the
+.I features
+field to perform a two-step handshake.
+First,
+.BR UFFDIO_API
+is called with the
+.I features
+field set to zero.
+The kernel responsds by setting all supported feature bits.
+.PP
+Applications which do not require any specific features
+can begin using the userfaultfd immediately.
+Applications which do need specific features
+should call
+.BR UFFDIO_API
+again with a subset of the reported feature bits set
+to enable those features.
+.PP
 Before Linux 4.11, the
 .I features
 field must be initialized to zero before the call to
@@ -102,24 +120,11 @@ and zero (i.e., no feature bits) is placed in the
 field by the kernel upon return from
 .BR ioctl (2).
 .PP
-Starting from Linux 4.11, the
-.I features
-field can be used to ask whether particular features are supported
-and explicitly enable userfaultfd features that are disabled by default.
-The kernel always reports all the available features in the
-.I features
-field.
-.PP
-To enable userfaultfd features the application should set
-a bit corresponding to each feature it wants to enable in the
-.I features
-field.
-If the kernel supports all the requested features it will enable them.
-Otherwise it will zero out the returned
+If the application sets unsupported feature bits,
+the kernel will zero out the returned
 .I uffdio_api
 structure and return
 .BR EINVAL .
-.\" FIXME add more details about feature negotiation and enablement
 .PP
 The following feature bits may be set:
 .TP
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
#15 lore raw

Re: [PATCH 05/10] ioctl_userfaultfd.2: describe two-step feature handshake

Hi Mike,

Quoted 10 lines · Mike Rapoport
Quoted 7 lines · Axel Rasmussen

Fully describe how UFFDIO_API can be used to perform a two-step feature handshake, and also note the case where this isn't necessary (programs which don't depend on any extra features).

This lets us clean up an old FIXME asking for this to be described.

Since v2 is unchanged, I've added this tag. Thanks for the review!

Cheers,
Alex

Quoted 23 lines
Quoted 23 lines
---
 man2/ioctl_userfaultfd.2 | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)
man2/ioctl_userfaultfd.2 +2 -1
index 339adf8fe..e91a1dfc8 100644
@@ -83,7 +83,6 @@ struct uffdio_api {
 The
 .I api
 field denotes the API version requested by the application.
-.PP
 The kernel verifies that it can support the requested API version,
 and sets the
 .I features
@@ -93,6 +92,25 @@ fields to bit masks representing all the available features and the generic
 .BR ioctl (2)
 operations available.
 .PP
+After Linux 4.11,
+applications should use the
Quoted 8 lines

man2/ioctl_userfaultfd.2 fields to bit masks representing all the available features and the generic · line 104

Quoted 8 lines
man2/ioctl_userfaultfd.2 +8 -0
+.I features
+field to perform a two-step handshake.
+First,
+.BR UFFDIO_API
+is called with the
+.I features
+field set to zero.
+The kernel responsds by setting all supported feature bits.

s/responsds/responds/ amended.

Quoted 47 lines
Quoted 43 lines
man2/ioctl_userfaultfd.2 +11 -15
+.PP
+Applications which do not require any specific features
+can begin using the userfaultfd immediately.
+Applications which do need specific features
+should call
+.BR UFFDIO_API
+again with a subset of the reported feature bits set
+to enable those features.
+.PP
 Before Linux 4.11, the
 .I features
 field must be initialized to zero before the call to
@@ -102,24 +120,11 @@ and zero (i.e., no feature bits) is placed in the
 field by the kernel upon return from
 .BR ioctl (2).
 .PP
-Starting from Linux 4.11, the
-.I features
-field can be used to ask whether particular features are supported
-and explicitly enable userfaultfd features that are disabled by default.
-The kernel always reports all the available features in the
-.I features
-field.
-.PP
-To enable userfaultfd features the application should set
-a bit corresponding to each feature it wants to enable in the
-.I features
-field.
-If the kernel supports all the requested features it will enable them.
-Otherwise it will zero out the returned
+If the application sets unsupported feature bits,
+the kernel will zero out the returned
 .I uffdio_api
 structure and return
 .BR EINVAL .
-.\" FIXME add more details about feature negotiation and enablement
 .PP
 The following feature bits may be set:
 .TP
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 1 line
<https://www.alejandro-colomar.es/>
#16 lore raw

PATCH 6/10 ioctl_userfaultfd.2: describe missing UFFDIO_API feature flags

In reply to Axel Rasmussen

Several new features have been added to the kernel recently, and the man page wasn't updated to describe these new features. So, add in descriptions of any missing features.

---
 man2/ioctl_userfaultfd.2 | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
man2/ioctl_userfaultfd.2 +23 -0
index e91a1dfc8..53b1f473f 100644
@@ -204,6 +204,13 @@ If this feature bit is set,
 .I uffd_msg.pagefault.feat.ptid
 will be set to the faulted thread ID for each page-fault message.
 .TP
+.BR UFFD_FEATURE_PAGEFAULT_FLAG_WP " (since Linux 5.10)"
+If this feature bit is set,
+userfaultfd supports write-protect faults
+for anonymous memory.
+(Note that shmem / hugetlbfs support
+is indicated by a separate feature.)
+.TP
 .BR UFFD_FEATURE_MINOR_HUGETLBFS " (since Linux 5.13)"
 If this feature bit is set,
 the kernel supports registering userfaultfd ranges
@@ -221,6 +228,22 @@ will be set to the exact page-fault address that was reported by the hardware,
 and will not mask the offset within the page.
 Note that old Linux versions might indicate the exact address as well,
 even though the feature bit is not set.
+.TP
+.BR UFFD_FEATURE_WP_HUGETLBFS_SHMEM " (since Linux 5.19)"
+If this feature bit is set,
+userfaultfd supports write-protect faults
+for hugetlbfs and shmem / tmpfs memory.
+.TP
+.BR UFFD_FEATURE_WP_UNPOPULATED " (since Linux 6.4)"
+If this feature bit is set,
+the kernel will handle anonymous memory the same way as file memory,
+by allowing the user to write-protect unpopulated ptes.
+.TP
+.BR UFFD_FEATURE_POISON " (since Linux 6.6)"
+If this feature bit is set,
+the kernel supports resolving faults with the
+.B UFFDIO_POISON
+ioctl.
 .PP
 The returned
 .I ioctls
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#17 lore raw

Re: [PATCH 06/10] ioctl_userfaultfd.2: describe missing UFFDIO_API feature flags

Quoted 6 lines · Axel Rasmussen

Several new features have been added to the kernel recently, and the man page wasn't updated to describe these new features. So, add in descriptions of any missing features.

---

Patch applied.

Thanks,
Alex

Quoted 47 lines
 man2/ioctl_userfaultfd.2 | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
man2/ioctl_userfaultfd.2 +23 -0
index e91a1dfc8..53b1f473f 100644
@@ -204,6 +204,13 @@ If this feature bit is set,
 .I uffd_msg.pagefault.feat.ptid
 will be set to the faulted thread ID for each page-fault message.
 .TP
+.BR UFFD_FEATURE_PAGEFAULT_FLAG_WP " (since Linux 5.10)"
+If this feature bit is set,
+userfaultfd supports write-protect faults
+for anonymous memory.
+(Note that shmem / hugetlbfs support
+is indicated by a separate feature.)
+.TP
 .BR UFFD_FEATURE_MINOR_HUGETLBFS " (since Linux 5.13)"
 If this feature bit is set,
 the kernel supports registering userfaultfd ranges
@@ -221,6 +228,22 @@ will be set to the exact page-fault address that was reported by the hardware,
 and will not mask the offset within the page.
 Note that old Linux versions might indicate the exact address as well,
 even though the feature bit is not set.
+.TP
+.BR UFFD_FEATURE_WP_HUGETLBFS_SHMEM " (since Linux 5.19)"
+If this feature bit is set,
+userfaultfd supports write-protect faults
+for hugetlbfs and shmem / tmpfs memory.
+.TP
+.BR UFFD_FEATURE_WP_UNPOPULATED " (since Linux 6.4)"
+If this feature bit is set,
+the kernel will handle anonymous memory the same way as file memory,
+by allowing the user to write-protect unpopulated ptes.
+.TP
+.BR UFFD_FEATURE_POISON " (since Linux 6.6)"
+If this feature bit is set,
+the kernel supports resolving faults with the
+.B UFFDIO_POISON
+ioctl.
 .PP
 The returned
 .I ioctls
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Reviewed-by on v1 6/10
#18 lore raw

Re: [PATCH 06/10] ioctl_userfaultfd.2: describe missing UFFDIO_API feature flags

In reply to Axel Rasmussen
Quoted 5 lines · Axel Rasmussen

Several new features have been added to the kernel recently, and the man page wasn't updated to describe these new features. So, add in descriptions of any missing features.

with a small nit below

Quoted 26 lines
---
 man2/ioctl_userfaultfd.2 | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
man2/ioctl_userfaultfd.2 +7 -0
index e91a1dfc8..53b1f473f 100644
@@ -204,6 +204,13 @@ If this feature bit is set,
 .I uffd_msg.pagefault.feat.ptid
 will be set to the faulted thread ID for each page-fault message.
 .TP
+.BR UFFD_FEATURE_PAGEFAULT_FLAG_WP " (since Linux 5.10)"
+If this feature bit is set,
+userfaultfd supports write-protect faults
+for anonymous memory.
+(Note that shmem / hugetlbfs support
+is indicated by a separate feature.)
+.TP
 .BR UFFD_FEATURE_MINOR_HUGETLBFS " (since Linux 5.13)"
 If this feature bit is set,
 the kernel supports registering userfaultfd ranges
@@ -221,6 +228,22 @@ will be set to the exact page-fault address that was reported by the hardware,
 and will not mask the offset within the page.
 Note that old Linux versions might indicate the exact address as well,
 even though the feature bit is not set.

man2/ioctl_userfaultfd.2 will be set to the exact page-fault address that was reported by the hardware, · line 240

Quoted 10 lines
man2/ioctl_userfaultfd.2 +10 -0
+.TP
+.BR UFFD_FEATURE_WP_HUGETLBFS_SHMEM " (since Linux 5.19)"
+If this feature bit is set,
+userfaultfd supports write-protect faults
+for hugetlbfs and shmem / tmpfs memory.
+.TP
+.BR UFFD_FEATURE_WP_UNPOPULATED " (since Linux 6.4)"
+If this feature bit is set,
+the kernel will handle anonymous memory the same way as file memory,
+by allowing the user to write-protect unpopulated ptes.

Nit: s/ptes/page table entries/

Quoted 13 lines
man2/ioctl_userfaultfd.2 +6 -0
+.TP
+.BR UFFD_FEATURE_POISON " (since Linux 6.6)"
+If this feature bit is set,
+the kernel supports resolving faults with the
+.B UFFDIO_POISON
+ioctl.
 .PP
 The returned
 .I ioctls
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
#19 lore raw

Re: [PATCH 06/10] ioctl_userfaultfd.2: describe missing UFFDIO_API feature flags

Hi Mike,

Quoted 10 lines · Mike Rapoport
Quoted 5 lines · Axel Rasmussen

Several new features have been added to the kernel recently, and the man page wasn't updated to describe these new features. So, add in descriptions of any missing features.

with a small nit below

Thanks for the reviews!

Quoted 2 lines
Quoted 2 lines

---

[...]

Quoted 3 lines
Quoted 1 line

+by allowing the user to write-protect unpopulated ptes.

Nit: s/ptes/page table entries/

I've applied the following patch:
<https://www.alejandro-colomar.es/src/alx/linux/man-pages/man-pages.git/commit/?h=contrib&id=2afbc25a7a3b1b68b638d7542a6bead7a1960a7d>

Cheers,
Alex

Signature · 1 line
<https://www.alejandro-colomar.es/>
#20 lore raw

PATCH 7/10 ioctl_userfaultfd.2: correct and update UFFDIO_API ioctl error codes

In reply to Axel Rasmussen

First, it is not correct that repeated UFFDIO_API calls result in EINVAL. This is true *if both calls enable features*, but in the case where we're doing a two-step feature detection handshake, the kernel explicitly expects 2 calls (one with no features set). So, correct this description.

Then, some new error cases have been added to the kernel recently, and the man page wasn't updated to note these. So, add in descriptions of these new error cases.

---
 man2/ioctl_userfaultfd.2 | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
man2/ioctl_userfaultfd.2 +19 -5
index 53b1f473f..1aa9654be 100644
@@ -280,17 +280,31 @@ refers to an address that is outside the calling process's
 accessible address space.
 .TP
 .B EINVAL
-The userfaultfd has already been enabled by a previous
-.B UFFDIO_API
-operation.
-.TP
-.B EINVAL
 The API version requested in the
 .I api
 field is not supported by this kernel, or the
 .I features
 field passed to the kernel includes feature bits that are not supported
 by the current kernel version.
+.TP
+.B EPERM
+The
+.B UFFD_FEATURE_EVENT_FORK
+feature was enabled,
+but the calling process doesn't have the
+.B CAP_SYS_PTRACE
+capability.
+.TP
+.B EINVAL
+A previous
+.B UFFDIO_API
+call already enabled one or more features for this userfaultfd.
+Calling
+.B UFFDIO_API
+twice,
+the first time with no features set,
+is explicitly allowed
+as per the two-step feature detection handshake.
 .\" FIXME In the above error case, the returned 'uffdio_api' structure is
 .\" zeroed out. Why is this done? This should be explained in the manual page.
 .\"
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#21 lore raw

Re: [PATCH 07/10] ioctl_userfaultfd.2: correct and update UFFDIO_API ioctl error codes

Hi Axel,

Quoted 26 lines · Axel Rasmussen

First, it is not correct that repeated UFFDIO_API calls result in EINVAL. This is true *if both calls enable features*, but in the case where we're doing a two-step feature detection handshake, the kernel explicitly expects 2 calls (one with no features set). So, correct this description.

Then, some new error cases have been added to the kernel recently, and the man page wasn't updated to note these. So, add in descriptions of these new error cases.

---
 man2/ioctl_userfaultfd.2 | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
man2/ioctl_userfaultfd.2 +0 -3
index 53b1f473f..1aa9654be 100644
@@ -280,17 +280,31 @@ refers to an address that is outside the calling process's
 accessible address space.
 .TP
 .B EINVAL
-The userfaultfd has already been enabled by a previous
-.B UFFDIO_API
-operation.

man2/ioctl_userfaultfd.2 refers to an address that is outside the calling process's · line 290

Quoted 10 lines
man2/ioctl_userfaultfd.2 +2 -2
-.TP
-.B EINVAL
 The API version requested in the
 .I api
 field is not supported by this kernel, or the
 .I features
 field passed to the kernel includes feature bits that are not supported
 by the current kernel version.
+.TP
+.B EPERM

This EPERM should probably be at the end. Unless you have a good reason to break alphabetic order.

Thanks,
Alex

Quoted 23 lines
man2/ioctl_userfaultfd.2 +17 -0
+The
+.B UFFD_FEATURE_EVENT_FORK
+feature was enabled,
+but the calling process doesn't have the
+.B CAP_SYS_PTRACE
+capability.
+.TP
+.B EINVAL
+A previous
+.B UFFDIO_API
+call already enabled one or more features for this userfaultfd.
+Calling
+.B UFFDIO_API
+twice,
+the first time with no features set,
+is explicitly allowed
+as per the two-step feature detection handshake.
 .\" FIXME In the above error case, the returned 'uffdio_api' structure is
 .\" zeroed out. Why is this done? This should be explained in the manual page.
 .\"
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Reviewed-by
#22 lore raw

Re: [PATCH 07/10] ioctl_userfaultfd.2: correct and update UFFDIO_API ioctl error codes

Quoted 34 lines · Alejandro Colomar

Hi Axel,

Quoted 31 lines · Axel Rasmussen

First, it is not correct that repeated UFFDIO_API calls result in EINVAL. This is true *if both calls enable features*, but in the case where we're doing a two-step feature detection handshake, the kernel explicitly expects 2 calls (one with no features set). So, correct this description.

Then, some new error cases have been added to the kernel recently, and the man page wasn't updated to note these. So, add in descriptions of these new error cases.

---
 man2/ioctl_userfaultfd.2 | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
man2/ioctl_userfaultfd.2 +0 -5
index 53b1f473f..1aa9654be 100644
@@ -280,17 +280,31 @@ refers to an address that is outside the calling process's
 accessible address space.
 .TP
 .B EINVAL
-The userfaultfd has already been enabled by a previous
-.B UFFDIO_API
-operation.
-.TP
-.B EINVAL
 The API version requested in the
 .I api
 field is not supported by this kernel, or the
Quoted 8 lines

man2/ioctl_userfaultfd.2 refers to an address that is outside the calling process's · line 290

Quoted 5 lines
man2/ioctl_userfaultfd.2 +2 -0
 .I features
 field passed to the kernel includes feature bits that are not supported
 by the current kernel version.
+.TP
+.B EPERM

This EPERM should probably be at the end. Unless you have a good reason to break alphabetic order.

I agree with Alex here, other than that feel free to add

Quoted 26 lines

Thanks,
Alex

Quoted 23 lines
man2/ioctl_userfaultfd.2 +17 -0
+The
+.B UFFD_FEATURE_EVENT_FORK
+feature was enabled,
+but the calling process doesn't have the
+.B CAP_SYS_PTRACE
+capability.
+.TP
+.B EINVAL
+A previous
+.B UFFDIO_API
+call already enabled one or more features for this userfaultfd.
+Calling
+.B UFFDIO_API
+twice,
+the first time with no features set,
+is explicitly allowed
+as per the two-step feature detection handshake.
 .\" FIXME In the above error case, the returned 'uffdio_api' structure is
 .\" zeroed out. Why is this done? This should be explained in the manual page.
 .\"
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
#23 lore raw

Re: [PATCH 07/10] ioctl_userfaultfd.2: correct and update UFFDIO_API ioctl error codes

Hi Mike,

Quoted 44 lines · Mike Rapoport
Quoted 34 lines · Alejandro Colomar

Hi Axel,

Quoted 31 lines · Axel Rasmussen

First, it is not correct that repeated UFFDIO_API calls result in EINVAL. This is true *if both calls enable features*, but in the case where we're doing a two-step feature detection handshake, the kernel explicitly expects 2 calls (one with no features set). So, correct this description.

Then, some new error cases have been added to the kernel recently, and the man page wasn't updated to note these. So, add in descriptions of these new error cases.

---
 man2/ioctl_userfaultfd.2 | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
man2/ioctl_userfaultfd.2 +0 -5
index 53b1f473f..1aa9654be 100644
@@ -280,17 +280,31 @@ refers to an address that is outside the calling process's
 accessible address space.
 .TP
 .B EINVAL
-The userfaultfd has already been enabled by a previous
-.B UFFDIO_API
-operation.
-.TP
-.B EINVAL
 The API version requested in the
 .I api
 field is not supported by this kernel, or the
Quoted 8 lines

man2/ioctl_userfaultfd.2 refers to an address that is outside the calling process's · line 290

Quoted 5 lines
man2/ioctl_userfaultfd.2 +2 -0
 .I features
 field passed to the kernel includes feature bits that are not supported
 by the current kernel version.
+.TP
+.B EPERM

This EPERM should probably be at the end. Unless you have a good reason to break alphabetic order.

Quoted 3 lines

I agree with Alex here, other than that feel free to add

Thanks. Since v2 only reorders these, I've added your tag.

Cheers,
Alex

Quoted 34 lines
Quoted 28 lines

Thanks,
Alex

Quoted 23 lines
man2/ioctl_userfaultfd.2 +17 -0
+The
+.B UFFD_FEATURE_EVENT_FORK
+feature was enabled,
+but the calling process doesn't have the
+.B CAP_SYS_PTRACE
+capability.
+.TP
+.B EINVAL
+A previous
+.B UFFDIO_API
+call already enabled one or more features for this userfaultfd.
+Calling
+.B UFFDIO_API
+twice,
+the first time with no features set,
+is explicitly allowed
+as per the two-step feature detection handshake.
 .\" FIXME In the above error case, the returned 'uffdio_api' structure is
 .\" zeroed out. Why is this done? This should be explained in the manual page.
 .\"
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 1 line
<https://www.alejandro-colomar.es/>
#24 lore raw

PATCH 8/10 ioctl_userfaultfd.2: clarify the state of the uffdio_api structure on error

In reply to Axel Rasmussen

The old FIXME noted that the zeroing was done to differentiate the two EINVAL cases. It's possible something like this was true historically, but in current Linux we zero it in *both* EINVAL cases, so this is at least no longer true.

After reading the code, I can't determine any clear reason why we zero it in some cases but not in others. So, some simple advice we can give userspace is: if an error occurs, treat the contents of the structure as unspecified. Just re-initialize it before retrying UFFDIO_API again.

---
 man2/ioctl_userfaultfd.2 | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
man2/ioctl_userfaultfd.2 +8 -8
index 1aa9654be..29dca1f6b 100644
@@ -272,6 +272,14 @@ operation returns 0 on success.
 On error, \-1 is returned and
 .I errno
 is set to indicate the error.
+If an error occurs,
+the kernel may zero the provided
+.I uffdio_api
+structure.
+The caller should treat its contents as unspecified,
+and reinitialize it before re-attempting another
+.B UFFDIO_API
+call.
 Possible errors include:
 .TP
 .B EFAULT
@@ -305,14 +313,6 @@ twice,
 the first time with no features set,
 is explicitly allowed
 as per the two-step feature detection handshake.
-.\" FIXME In the above error case, the returned 'uffdio_api' structure is
-.\" zeroed out. Why is this done? This should be explained in the manual page.
-.\"
-.\" Mike Rapoport:
-.\"     In my understanding the uffdio_api
-.\"     structure is zeroed to allow the caller
-.\"     to distinguish the reasons for -EINVAL.
-.\"
 .SS UFFDIO_REGISTER
 (Since Linux 4.3.)
 Register a memory address range with the userfaultfd object.
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#25 lore raw

Re: [PATCH 08/10] ioctl_userfaultfd.2: clarify the state of the uffdio_api structure on error

Hi Axel,

Quoted 11 lines · Axel Rasmussen

The old FIXME noted that the zeroing was done to differentiate the two EINVAL cases. It's possible something like this was true historically, but in current Linux we zero it in *both* EINVAL cases, so this is at least no longer true.

After reading the code, I can't determine any clear reason why we zero it in some cases but not in others. So, some simple advice we can give userspace is: if an error occurs, treat the contents of the structure as unspecified. Just re-initialize it before retrying UFFDIO_API again.

I can't apply this patch due to conflicts (due to not having applied two of the previous ones). Please resend all remaining patches in following revisions of the patch set.

The applied ones are here:

<https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/log/?h=contrib>

It's kind of like Linux's 'next' branch.

Cheers,
Alex

Quoted 41 lines
---
 man2/ioctl_userfaultfd.2 | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
man2/ioctl_userfaultfd.2 +8 -8
index 1aa9654be..29dca1f6b 100644
@@ -272,6 +272,14 @@ operation returns 0 on success.
 On error, \-1 is returned and
 .I errno
 is set to indicate the error.
+If an error occurs,
+the kernel may zero the provided
+.I uffdio_api
+structure.
+The caller should treat its contents as unspecified,
+and reinitialize it before re-attempting another
+.B UFFDIO_API
+call.
 Possible errors include:
 .TP
 .B EFAULT
@@ -305,14 +313,6 @@ twice,
 the first time with no features set,
 is explicitly allowed
 as per the two-step feature detection handshake.
-.\" FIXME In the above error case, the returned 'uffdio_api' structure is
-.\" zeroed out. Why is this done? This should be explained in the manual page.
-.\"
-.\" Mike Rapoport:
-.\"     In my understanding the uffdio_api
-.\"     structure is zeroed to allow the caller
-.\"     to distinguish the reasons for -EINVAL.
-.\"
 .SS UFFDIO_REGISTER
 (Since Linux 4.3.)
 Register a memory address range with the userfaultfd object.
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#26 lore raw

Re: [PATCH 08/10] ioctl_userfaultfd.2: clarify the state of the uffdio_api structure on error

Quoted 20 lines · Alejandro Colomar

Hi Axel,

Quoted 11 lines · Axel Rasmussen

The old FIXME noted that the zeroing was done to differentiate the two EINVAL cases. It's possible something like this was true historically, but in current Linux we zero it in *both* EINVAL cases, so this is at least no longer true.

After reading the code, I can't determine any clear reason why we zero it in some cases but not in others. So, some simple advice we can give userspace is: if an error occurs, treat the contents of the structure as unspecified. Just re-initialize it before retrying UFFDIO_API again.

I can't apply this patch due to conflicts (due to not having applied two of the previous ones). Please resend all remaining patches in following revisions of the patch set.

Quoted 5 lines

The applied ones are here:

<https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/log/?h=contrib>

It's kind of like Linux's 'next' branch.

Thanks for the review Alex! I'll fix up the issues noted and send the remaining few patches this week. :)

Quoted 45 lines

Cheers,
Alex

Quoted 41 lines
---
 man2/ioctl_userfaultfd.2 | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
man2/ioctl_userfaultfd.2 +8 -9
index 1aa9654be..29dca1f6b 100644
@@ -272,6 +272,14 @@ operation returns 0 on success.
 On error, \-1 is returned and
 .I errno
 is set to indicate the error.
+If an error occurs,
+the kernel may zero the provided
+.I uffdio_api
+structure.
+The caller should treat its contents as unspecified,
+and reinitialize it before re-attempting another
+.B UFFDIO_API
+call.
 Possible errors include:
 .TP
 .B EFAULT
@@ -305,14 +313,6 @@ twice,
 the first time with no features set,
 is explicitly allowed
 as per the two-step feature detection handshake.
-.\" FIXME In the above error case, the returned 'uffdio_api' structure is
-.\" zeroed out. Why is this done? This should be explained in the manual page.
-.\"
-.\" Mike Rapoport:
-.\"     In my understanding the uffdio_api
-.\"     structure is zeroed to allow the caller
-.\"     to distinguish the reasons for -EINVAL.
-.\"
 .SS UFFDIO_REGISTER
 (Since Linux 4.3.)
 Register a memory address range with the userfaultfd object.
--

2.42.0.459.ge4e396fd5e-goog

Reviewed-by on v1 8/10
#27 lore raw

Re: [PATCH 08/10] ioctl_userfaultfd.2: clarify the state of the uffdio_api structure on error

In reply to Axel Rasmussen
Quoted 9 lines · Axel Rasmussen

The old FIXME noted that the zeroing was done to differentiate the two EINVAL cases. It's possible something like this was true historically, but in current Linux we zero it in *both* EINVAL cases, so this is at least no longer true.

After reading the code, I can't determine any clear reason why we zero it in some cases but not in others. So, some simple advice we can give userspace is: if an error occurs, treat the contents of the structure as unspecified. Just re-initialize it before retrying UFFDIO_API again.

In old kernels (e.g. 4.20 and I didn't go to check when this changed) we had two -EINVALS: one when UFFDIO_API was called when

	state != UFFD_STATE_WAIT_API

and another for API version or features mismatch and we zeroed uffd_api struct only in the second case.

In the current code the first case does not exits anymore.

Quoted 1 line
Quoted 42 lines
---
 man2/ioctl_userfaultfd.2 | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
man2/ioctl_userfaultfd.2 +8 -8
index 1aa9654be..29dca1f6b 100644
@@ -272,6 +272,14 @@ operation returns 0 on success.
 On error, \-1 is returned and
 .I errno
 is set to indicate the error.
+If an error occurs,
+the kernel may zero the provided
+.I uffdio_api
+structure.
+The caller should treat its contents as unspecified,
+and reinitialize it before re-attempting another
+.B UFFDIO_API
+call.
 Possible errors include:
 .TP
 .B EFAULT
@@ -305,14 +313,6 @@ twice,
 the first time with no features set,
 is explicitly allowed
 as per the two-step feature detection handshake.
-.\" FIXME In the above error case, the returned 'uffdio_api' structure is
-.\" zeroed out. Why is this done? This should be explained in the manual page.
-.\"
-.\" Mike Rapoport:
-.\"     In my understanding the uffdio_api
-.\"     structure is zeroed to allow the caller
-.\"     to distinguish the reasons for -EINVAL.
-.\"
 .SS UFFDIO_REGISTER
 (Since Linux 4.3.)
 Register a memory address range with the userfaultfd object.
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
#28 lore raw

PATCH 9/10 ioctl_userfaultfd.2: fix / update UFFDIO_REGISTER error code list

In reply to Axel Rasmussen

The list of error codes in the man page was out of date with respect to the current state of the kernel. Some errors were partially / incorrectly described.

Update the error code listing, so it matches the current state of the kernel, and correctly describes all the errors.

---
 man2/ioctl_userfaultfd.2 | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)
man2/ioctl_userfaultfd.2 +21 -16
index 29dca1f6b..afe3caffc 100644
@@ -413,12 +413,6 @@ On error, \-1 is returned and
 .I errno
 is set to indicate the error.
 Possible errors include:
-.\" FIXME Is the following error list correct?
-.\"
-.TP
-.B EBUSY
-A mapping in the specified range is registered with another
-userfaultfd object.
 .TP
 .B EFAULT
 .I argp
@@ -433,21 +427,32 @@ field; or the
 field was zero.
 .TP
 .B EINVAL
-There is no mapping in the specified address range.
-.TP
-.B EINVAL
+The specified address range was invalid.
+More specifically,
+no mapping exists in the given range,
+or the mapping that exists there is invalid
+(e.g. unsupported type of memory),
+or the range values (
 .I range.start
 or
 .I range.len
-is not a multiple of the system page size; or,
+) are not multiples of the relevant page size,
+or
 .I range.len
-is zero; or these fields are otherwise invalid.
+is zero.
 .TP
-.B EINVAL
-There as an incompatible mapping in the specified address range.
-.\" Mike Rapoport:
-.\" ENOMEM if the process is exiting and the
-.\" mm_struct has gone by the time userfault grabs it.
+.B ENOMEM
+The process is exiting,
+and its address space has already been torn down
+when userfaultfd attempts to reference it.
+.TP
+.B EPERM
+The userfaultfd would allow writing to a file backing the mapping,
+but the calling process lacks such write permissions.
+.TP
+.B EBUSY
+A mapping in the specified range is registered with another
+userfaultfd object.
 .SS UFFDIO_UNREGISTER
 (Since Linux 4.3.)
 Unregister a memory address range from userfaultfd.
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#29 lore raw

PATCH 10/10 ioctl_userfaultfd.2: document new UFFDIO_POISON ioctl

In reply to Axel Rasmussen

This is a new feature recently added to the kernel. So, document the new ioctl the same way we do other UFFDIO_* ioctls.

Also note the corresponding new ioctl flag we can return in reponse to a UFFDIO_REGISTER call.

---
 man2/ioctl_userfaultfd.2 | 112 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
man2/ioctl_userfaultfd.2 +112 -0
index afe3caffc..1282f63e1 100644
@@ -405,6 +405,11 @@ operation is supported.
 The
 .B UFFDIO_CONTINUE
 operation is supported.
+.TP
+.B 1 << _UFFDIO_POISON
+The
+.B UFFDIO_POISON
+operation is supported.
 .PP
 This
 .BR ioctl (2)
@@ -916,6 +921,113 @@ The faulting process has exited at the time of a
 .B UFFDIO_CONTINUE
 operation.
 .\"
+.SS UFFDIO_POISON
+(Since Linux 6.6.)
+Mark an address range as "poisoned".
+Future accesses to these addresses will raise a
+.B SIGBUS
+signal.
+Unlike
+.B MADV_HWPOISON
+this works by installing page table entries,
+rather than "really" poisoning the underlying physical pages.
+This means it only affects this particular address space.
+.PP
+The
+.I argp
+argument is a pointer to a
+.I uffdio_continue
+structure as shown below:
+.PP
+.in +4n
+.EX
+struct uffdio_poison {
+	struct uffdio_range range;
+	                /* Range to install poison PTE markers in */
+	__u64 mode;     /* Flags controlling the behavior of poison */
+	__s64 updated;  /* Number of bytes poisoned, or negated error */
+};
+.EE
+.in
+.PP
+The following value may be bitwise ORed in
+.I mode
+to change the behavior of the
+.B UFFDIO_POISON
+operation:
+.TP
+.B UFFDIO_POISON_MODE_DONTWAKE
+Do not wake up the thread that waits for page-fault resolution.
+.PP
+The
+.I updated
+field is used by the kernel
+to return the number of bytes that were actually poisoned,
+or an error in the same manner as
+.BR UFFDIO_COPY .
+If the value returned in the
+.I updated
+field doesn't match the value that was specified in
+.IR range.len ,
+the operation fails with the error
+.BR EAGAIN .
+The
+.I updated
+field is output-only;
+it is not read by the
+.B UFFDIO_POISON
+operation.
+.PP
+This
+.BR ioctl (2)
+operation returns 0 on success.
+In this case,
+the entire area was poisoned.
+On error, \-1 is returned and
+.I errno
+is set to indicate the error.
+Possible errors include:
+.TP
+.B EAGAIN
+The number of bytes mapped
+(i.e., the value returned in the
+.I updated
+field)
+does not equal the value that was specified in the
+.I range.len
+field.
+.TP
+.B EINVAL
+Either
+.I range.start
+or
+.I range.len
+was not a multiple of the system page size; or
+.I range.len
+was zero; or the range specified was invalid.
+.TP
+.B EINVAL
+An invalid bit was specified in the
+.I mode
+field.
+.TP
+.B EEXIST
+One or more pages were already mapped in the given range.
+.TP
+.B ENOENT
+The faulting process has changed its virtual memory layout simultaneously with
+an outstanding
+.B UFFDIO_POISON
+operation.
+.TP
+.B ENOMEM
+Allocating memory for page table entries failed.
+.TP
+.B ESRCH
+The faulting process has exited at the time of a
+.B UFFDIO_POISON
+operation.
+.\"
 .SH RETURN VALUE
 See descriptions of the individual operations, above.
 .SH ERRORS
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Reviewed-by on v1 10/10
#30 lore raw

Re: [PATCH 10/10] ioctl_userfaultfd.2: document new UFFDIO_POISON ioctl

Quoted 7 lines · Axel Rasmussen

This is a new feature recently added to the kernel. So, document the new ioctl the same way we do other UFFDIO_* ioctls.

Also note the corresponding new ioctl flag we can return in reponse to a UFFDIO_REGISTER call.

With a small correction

Quoted 30 lines
---
 man2/ioctl_userfaultfd.2 | 112 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
man2/ioctl_userfaultfd.2 +11 -0
index afe3caffc..1282f63e1 100644
@@ -405,6 +405,11 @@ operation is supported.
 The
 .B UFFDIO_CONTINUE
 operation is supported.
+.TP
+.B 1 << _UFFDIO_POISON
+The
+.B UFFDIO_POISON
+operation is supported.
 .PP
 This
 .BR ioctl (2)
@@ -916,6 +921,113 @@ The faulting process has exited at the time of a
 .B UFFDIO_CONTINUE
 operation.
 .\"
+.SS UFFDIO_POISON
+(Since Linux 6.6.)
+Mark an address range as "poisoned".
+Future accesses to these addresses will raise a
+.B SIGBUS
+signal.

man2/ioctl_userfaultfd.2 The faulting process has exited at the time of a · line 939

Quoted 10 lines
man2/ioctl_userfaultfd.2 +10 -0
+Unlike
+.B MADV_HWPOISON
+this works by installing page table entries,
+rather than "really" poisoning the underlying physical pages.
+This means it only affects this particular address space.
+.PP
+The
+.I argp
+argument is a pointer to a
+.I uffdio_continue

Did you mean uffdio_poison?

Quoted 98 lines
man2/ioctl_userfaultfd.2 +91 -0
+structure as shown below:
+.PP
+.in +4n
+.EX
+struct uffdio_poison {
+	struct uffdio_range range;
+	                /* Range to install poison PTE markers in */
+	__u64 mode;     /* Flags controlling the behavior of poison */
+	__s64 updated;  /* Number of bytes poisoned, or negated error */
+};
+.EE
+.in
+.PP
+The following value may be bitwise ORed in
+.I mode
+to change the behavior of the
+.B UFFDIO_POISON
+operation:
+.TP
+.B UFFDIO_POISON_MODE_DONTWAKE
+Do not wake up the thread that waits for page-fault resolution.
+.PP
+The
+.I updated
+field is used by the kernel
+to return the number of bytes that were actually poisoned,
+or an error in the same manner as
+.BR UFFDIO_COPY .
+If the value returned in the
+.I updated
+field doesn't match the value that was specified in
+.IR range.len ,
+the operation fails with the error
+.BR EAGAIN .
+The
+.I updated
+field is output-only;
+it is not read by the
+.B UFFDIO_POISON
+operation.
+.PP
+This
+.BR ioctl (2)
+operation returns 0 on success.
+In this case,
+the entire area was poisoned.
+On error, \-1 is returned and
+.I errno
+is set to indicate the error.
+Possible errors include:
+.TP
+.B EAGAIN
+The number of bytes mapped
+(i.e., the value returned in the
+.I updated
+field)
+does not equal the value that was specified in the
+.I range.len
+field.
+.TP
+.B EINVAL
+Either
+.I range.start
+or
+.I range.len
+was not a multiple of the system page size; or
+.I range.len
+was zero; or the range specified was invalid.
+.TP
+.B EINVAL
+An invalid bit was specified in the
+.I mode
+field.
+.TP
+.B EEXIST
+One or more pages were already mapped in the given range.
+.TP
+.B ENOENT
+The faulting process has changed its virtual memory layout simultaneously with
+an outstanding
+.B UFFDIO_POISON
+operation.
+.TP
+.B ENOMEM
+Allocating memory for page table entries failed.
+.TP
+.B ESRCH
+The faulting process has exited at the time of a
+.B UFFDIO_POISON
+operation.
+.\"
 .SH RETURN VALUE
 See descriptions of the individual operations, above.
 .SH ERRORS
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
Signature · 2 lines
Sincerely yours,
Mike.
#31 lore raw

Re: [PATCH 10/10] ioctl_userfaultfd.2: document new UFFDIO_POISON ioctl

Quoted 48 lines · Mike Rapoport
Quoted 7 lines · Axel Rasmussen

This is a new feature recently added to the kernel. So, document the new ioctl the same way we do other UFFDIO_* ioctls.

Also note the corresponding new ioctl flag we can return in reponse to a UFFDIO_REGISTER call.

With a small correction

Quoted 34 lines
---
 man2/ioctl_userfaultfd.2 | 112 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
man2/ioctl_userfaultfd.2 +15 -0
index afe3caffc..1282f63e1 100644
@@ -405,6 +405,11 @@ operation is supported.
 The
 .B UFFDIO_CONTINUE
 operation is supported.
+.TP
+.B 1 << _UFFDIO_POISON
+The
+.B UFFDIO_POISON
+operation is supported.
 .PP
 This
 .BR ioctl (2)
@@ -916,6 +921,113 @@ The faulting process has exited at the time of a
 .B UFFDIO_CONTINUE
 operation.
 .\"
+.SS UFFDIO_POISON
+(Since Linux 6.6.)
+Mark an address range as "poisoned".
+Future accesses to these addresses will raise a
+.B SIGBUS
+signal.
+Unlike
+.B MADV_HWPOISON
+this works by installing page table entries,
+rather than "really" poisoning the underlying physical pages.
Quoted 8 lines

man2/ioctl_userfaultfd.2 The faulting process has exited at the time of a · line 939

Quoted 6 lines
man2/ioctl_userfaultfd.2 +6 -0
+This means it only affects this particular address space.
+.PP
+The
+.I argp
+argument is a pointer to a
+.I uffdio_continue

Did you mean uffdio_poison?

Ah, yes. :) Should have copy/pasted more carefully. I can send a v3 with this small correction.

Quoted 103 lines
Quoted 96 lines
man2/ioctl_userfaultfd.2 +91 -1
+structure as shown below:
+.PP
+.in +4n
+.EX
+struct uffdio_poison {
+     struct uffdio_range range;
+                     /* Range to install poison PTE markers in */
+     __u64 mode;     /* Flags controlling the behavior of poison */
+     __s64 updated;  /* Number of bytes poisoned, or negated error */
+};
+.EE
+.in
+.PP
+The following value may be bitwise ORed in
+.I mode
+to change the behavior of the
+.B UFFDIO_POISON
+operation:
+.TP
+.B UFFDIO_POISON_MODE_DONTWAKE
+Do not wake up the thread that waits for page-fault resolution.
+.PP
+The
+.I updated
+field is used by the kernel
+to return the number of bytes that were actually poisoned,
+or an error in the same manner as
+.BR UFFDIO_COPY .
+If the value returned in the
+.I updated
+field doesn't match the value that was specified in
+.IR range.len ,
+the operation fails with the error
+.BR EAGAIN .
+The
+.I updated
+field is output-only;
+it is not read by the
+.B UFFDIO_POISON
+operation.
+.PP
+This
+.BR ioctl (2)
+operation returns 0 on success.
+In this case,
+the entire area was poisoned.
+On error, \-1 is returned and
+.I errno
+is set to indicate the error.
+Possible errors include:
+.TP
+.B EAGAIN
+The number of bytes mapped
+(i.e., the value returned in the
+.I updated
+field)
+does not equal the value that was specified in the
+.I range.len
+field.
+.TP
+.B EINVAL
+Either
+.I range.start
+or
+.I range.len
+was not a multiple of the system page size; or
+.I range.len
+was zero; or the range specified was invalid.
+.TP
+.B EINVAL
+An invalid bit was specified in the
+.I mode
+field.
+.TP
+.B EEXIST
+One or more pages were already mapped in the given range.
+.TP
+.B ENOENT
+The faulting process has changed its virtual memory layout simultaneously with
+an outstanding
+.B UFFDIO_POISON
+operation.
+.TP
+.B ENOMEM
+Allocating memory for page table entries failed.
+.TP
+.B ESRCH
+The faulting process has exited at the time of a
+.B UFFDIO_POISON
+operation.
+.\"
 .SH RETURN VALUE
 See descriptions of the individual operations, above.
 .SH ERRORS
--
Quoted 3 lines

2.42.0.459.ge4e396fd5e-goog

--
Sincerely yours,
Mike.