Merged patches from others
cover Merged patches from others
Hi Michael,
These are the patches I have merged from others.
Cheers,
Alex
Alyssa Ross (1):
scanf.3: clarify that %n supports type modifiers
Peter Xu (4):
userfaultfd.2: Add UFFD_FEATURE_THREAD_ID docs userfaultfd.2: Add write-protect mode ioctl_userfaultfd.2: Add UFFD_FEATURE_THREAD_ID docs ioctl_userfaultfd.2: Add write-protect mode docs
man2/ioctl_userfaultfd.2 | 91 ++++++++++++++++++++++++++++- man2/userfaultfd.2 | 121 +++++++++++++++++++++++++++++++++++++-- man3/scanf.3 | 4 +- 3 files changed, 208 insertions(+), 8 deletions(-)
Signature · 1 line
2.31.0
PATCH 1/5 scanf.3: clarify that %n supports type modifiers
From: Alyssa Ross <[email protected]>
My initial reading of this was that type modifiers were probably not supported. But they are, and this is actually documented further up, in the type modifiers documentation. But to make it clearer, let's copy the language that printf(3) has in its %n section.
--- man3/scanf.3 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
man3/scanf.3 +3 -1
@@ -526,7 +526,9 @@ the next pointer must be a pointer to a pointer to Nothing is expected; instead, the number of characters consumed thus far from the input is stored through the next pointer, which must be a pointer to -.IR int . +.IR int , +or variant whose size matches the (optionally) +supplied integer length modifier. This is .I not a conversion and does
Signature · 1 line
2.31.0
Re: [PATCH 1/5] scanf.3: clarify that %n supports type modifiers
Hello Alex and Alyssa,
Quoted 18 lines · Alejandro Colomar
From: Alyssa Ross <[email protected]>
My initial reading of this was that type modifiers were probably not supported. But they are, and this is actually documented further up, in the type modifiers documentation. But to make it clearer, let's copy the language that printf(3) has in its %n section.
Signed-off-by Alyssa Ross <[email protected]>Signed-off-by Alejandro Colomar <[email protected]>--- man3/scanf.3 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)man3/scanf.3 +0
-0
man3/scanf.3 the next pointer must be a pointer to a pointer to · line 534
Quoted 10 lines
man3/scanf.3 +3
-1@@ -526,7 +526,9 @@ the next pointer must be a pointer to a pointer to Nothing is expected; instead, the number of characters consumed thus far from the input is stored through the next pointer, which must be a pointer to -.IR int . +.IR int , +or variant whose size matches the (optionally) +supplied integer length modifier. This is .I not a conversion and does
Thanks. Patch applied.
Cheers,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 2/5 userfaultfd.2: Add UFFD_FEATURE_THREAD_ID docs
In reply to Alejandro ColomarFrom: Peter Xu <[email protected]>
UFFD_FEATURE_THREAD_ID is supported since Linux 4.14.
--- man2/userfaultfd.2 | 13 +++++++++++++ 1 file changed, 13 insertions(+)
man2/userfaultfd.2 +13 -0
@@ -77,6 +77,13 @@ When the last file descriptor referring to a userfaultfd object is closed, all memory ranges that were registered with the object are unregistered and unread events are flushed. .\" +.PP +Since Linux 4.14, userfaultfd page fault message can selectively embed +faulting thread ID information into the fault message. +One needs to enable this feature explicitly using the +.B UFFD_FEATURE_THREAD_ID +feature bit when initializing the userfaultfd context. +By default, thread ID reporting is disabled. .SS Usage The userfaultfd mechanism is designed to allow a thread in a multithreaded program to perform user-space paging for the other threads in the process.
@@ -229,6 +236,9 @@ struct uffd_msg { struct { __u64 flags; /* Flags describing fault */ __u64 address; /* Faulting address */ + union { + __u32 ptid; /* Thread ID of the fault */ + } feat; } pagefault; struct { /* Since Linux 4.11 */
@@ -358,6 +368,9 @@ otherwise it is a read fault. .\" UFFD_PAGEFAULT_FLAG_WP is not yet supported. .RE .TP +.I pagefault.feat.pid +The thread ID that triggered the page fault. +.TP .I fork.ufd The file descriptor associated with the userfault object created for the child created by
Signature · 1 line
2.31.0
Re: [PATCH 2/5] userfaultfd.2: Add UFFD_FEATURE_THREAD_ID docs
Hello Alex, and Peter,
Please take note below.
Quoted 41 lines · Alejandro Colomar
From: Peter Xu <[email protected]>
UFFD_FEATURE_THREAD_IDis supported since Linux 4.14.Acked-by Mike Rapoport <[email protected]>Signed-off-by Peter Xu <[email protected]>[alx: srcfix]Signed-off-by Alejandro Colomar <[email protected]>--- man2/userfaultfd.2 | 13 +++++++++++++ 1 file changed, 13 insertions(+)man2/userfaultfd.2 +10
-0@@ -77,6 +77,13 @@ When the last file descriptor referring to a userfaultfd object is closed, all memory ranges that were registered with the object are unregistered and unread events are flushed. .\" +.PP +Since Linux 4.14, userfaultfd page fault message can selectively embed +faulting thread ID information into the fault message. +One needs to enable this feature explicitly using the +.B UFFD_FEATURE_THREAD_ID +feature bit when initializing the userfaultfd context. +By default, thread ID reporting is disabled. .SS Usage The userfaultfd mechanism is designed to allow a thread in a multithreaded program to perform user-space paging for the other threads in the process.@@ -229,6 +236,9 @@ struct uffd_msg { struct { __u64 flags; /* Flags describing fault */ __u64 address; /* Faulting address */ + union { + __u32 ptid; /* Thread ID of the fault */ + } feat; } pagefault; struct { /* Since Linux 4.11 */
man2/userfaultfd.2 otherwise it is a read fault. · line 376
Quoted 9 lines
man2/userfaultfd.2 +3
-0@@ -358,6 +368,9 @@ otherwise it is a read fault. .\" UFFD_PAGEFAULT_FLAG_WP is not yet supported. .RE .TP +.I pagefault.feat.pid +The thread ID that triggered the page fault. +.TP .I fork.ufd The file descriptor associated with the userfault object created for the child created by
Thanks. I've applied the patch, but I made a few tweaks afterwards, as per the patch below. Notably, I changed "message" to "notification", since that seems more consistent with the language in the rest of the page. Is it okay, Peter?
Cheers,
Michael
man2/userfaultfd.2 +4 -2
@@ -78,8 +78,10 @@ all memory ranges that were registered with the object are unregistered and unread events are flushed. .\" .PP -Since Linux 4.14, userfaultfd page fault message can selectively embed -faulting thread ID information into the fault message. +Since Linux 4.14, +.\" commit 9d4ac934829ac58c5109c49e6dfe677300e5e652 +a userfaultfd page fault notification can selectively embed the +faulting thread ID information in the notification. One needs to enable this feature explicitly using the .B UFFD_FEATURE_THREAD_ID feature bit when initializing the userfaultfd context.
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH 2/5] userfaultfd.2: Add UFFD_FEATURE_THREAD_ID docs
Quoted 4 lines
Thanks. I've applied the patch, but I made a few tweaks afterwards, as per the patch below. Notably, I changed "message" to "notification", since that seems more consistent with the language in the rest of the page. Is it okay, Peter?
Hmmm -- ignore this. For now, I reverted the patch below, since it causes the following patch to break.
Cheers,
Michael
Quoted 19 lines
man2/userfaultfd.2 +4
-2@@ -78,8 +78,10 @@ all memory ranges that were registered with the object are unregistered and unread events are flushed. .\" .PP -Since Linux 4.14, userfaultfd page fault message can selectively embed -faulting thread ID information into the fault message. +Since Linux 4.14, +.\" commit 9d4ac934829ac58c5109c49e6dfe677300e5e652 +a userfaultfd page fault notification can selectively embed the +faulting thread ID information in the notification. One needs to enable this feature explicitly using the .B UFFD_FEATURE_THREAD_ID feature bit when initializing the userfaultfd context.
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 3/5 userfaultfd.2: Add write-protect mode
In reply to Alejandro ColomarFrom: Peter Xu <[email protected]>
Write-protect mode is supported starting from Linux 5.7.
--- man2/userfaultfd.2 | 108 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-)
man2/userfaultfd.2 +104 -4
@@ -78,6 +78,32 @@ all memory ranges that were registered with the object are unregistered and unread events are flushed. .\" .PP +Userfaultfd supports two modes of registration: +.TP +.BR UFFDIO_REGISTER_MODE_MISSING " (since 4.10)" +When registered with +.B UFFDIO_REGISTER_MODE_MISSING +mode, the userspace will receive a page fault message +when a missing page is accessed. +The faulted thread will be stopped from execution until the page fault is +resolved from the userspace by either an +.B UFFDIO_COPY +or an +.B UFFDIO_ZEROPAGE +ioctl. +.TP +.BR UFFDIO_REGISTER_MODE_WP " (since 5.7)" +When registered with +.B UFFDIO_REGISTER_MODE_WP +mode, the userspace will receive a page fault message +when a write-protected page is written. +The faulted thread will be stopped from execution +until the userspace write-unprotect the page using an +.B UFFDIO_WRITEPROTECT +ioctl. +.PP +Multiple modes can be enabled at the same time for the same memory range. +.PP Since Linux 4.14, userfaultfd page fault message can selectively embed faulting thread ID information into the fault message. One needs to enable this feature explicitly using the
@@ -107,7 +133,7 @@ the process that monitors userfaultfd and handles page faults needs to be aware of the changes in the virtual memory layout of the faulting process to avoid memory corruption. .PP -Starting from Linux 4.11, +Since Linux 4.11, userfaultfd can also notify the fault-handling threads about changes in the virtual memory layout of the faulting process. In addition, if the faulting process invokes
@@ -144,6 +170,17 @@ single threaded non-cooperative userfaultfd manager implementations. .\" and limitations remaining in 4.11 .\" Maybe it's worth adding a dedicated sub-section... .\" +.PP +Since Linux 5.7, userfaultfd is able to do +synchronous page dirty tracking using the new write-protect register mode. +One should check against the feature bit +.B UFFD_FEATURE_PAGEFAULT_FLAG_WP +before using this feature. +Similar to the original userfaultfd missing mode, the write-protect mode will +generate an userfaultfd message when the protected page is written. +The user needs to resolve the page fault by unprotecting the faulted page and +kick the faulted thread to continue. +For more information, please refer to "Userfaultfd write-protect mode" section. .SS Userfaultfd operation After the userfaultfd object is created with .BR userfaultfd (),
@@ -179,7 +216,7 @@ or .BR ioctl (2) operations to resolve the page fault. .PP -Starting from Linux 4.14, if the application sets the +Since Linux 4.14, if the application sets the .B UFFD_FEATURE_SIGBUS feature bit using the .B UFFDIO_API
@@ -219,6 +256,65 @@ userfaultfd can be used only with anonymous private memory mappings. Since Linux 4.11, userfaultfd can be also used with hugetlbfs and shared memory mappings. .\" +.SS Userfaultfd write-protect mode (since 5.7) +Since Linux 5.7, userfaultfd supports write-protect mode. +The user needs to first check availability of this feature using +.B UFFDIO_API +ioctl against the feature bit +.B UFFD_FEATURE_PAGEFAULT_FLAG_WP +before using this feature. +.PP +To register with userfaultfd write-protect mode, the user needs to initiate the +.B UFFDIO_REGISTER +ioctl with mode +.B UFFDIO_REGISTER_MODE_WP +set. +Note that it's legal to monitor the same memory range with multiple modes. +For example, the user can do +.B UFFDIO_REGISTER +with the mode set to +.BR "UFFDIO_REGISTER_MODE_MISSING | UFFDIO_REGISTER_MODE_WP" . +When there is only +.B UFFDIO_REGISTER_MODE_WP +registered, the userspace will +.I not +receive any message when a missing page is written. +Instead, the userspace will only receive a write-protect page fault message +when an existing but write-protected page got written. +.PP +After the +.B UFFDIO_REGISTER +ioctl completed with +.B UFFDIO_REGISTER_MODE_WP +mode set, +the user can write-protect any existing memory within the range using the ioctl +.B UFFDIO_WRITEPROTECT +where +.I uffdio_writeprotect.mode +should be set to +.BR UFFDIO_WRITEPROTECT_MODE_WP . +.PP +When a write-protect event happens, +the userspace will receive a page fault message whose +.I uffd_msg.pagefault.flags +will be with +.B UFFD_PAGEFAULT_FLAG_WP +flag set. +Note: since only writes can trigger such kind of fault, +write-protect messages will always be with +.B UFFD_PAGEFAULT_FLAG_WRITE +bit set too along with bit +.BR UFFD_PAGEFAULT_FLAG_WP . +.PP +To resolve a write-protection page fault, the user should initiate another +.B UFFDIO_WRITEPROTECT +ioctl, whose +.I uffd_msg.pagefault.flags +should have the flag +.B UFFDIO_WRITEPROTECT_MODE_WP +cleared upon the faulted page or range. +.PP +Write-protect mode only supports private anonymous memory. .SS Reading from the userfaultfd structure Each .BR read (2)
@@ -364,8 +460,12 @@ flag (see .BR ioctl_userfaultfd (2)) and this flag is set, this a write fault; otherwise it is a read fault. -.\" -.\" UFFD_PAGEFAULT_FLAG_WP is not yet supported. +.TP +.B UFFD_PAGEFAULT_FLAG_WP +If the address is in a range that was registered with the +.B UFFDIO_REGISTER_MODE_WP +flag, when this bit is set it means it's a write-protect fault. +Otherwise it's a page missing fault. .RE .TP .I pagefault.feat.pid
Signature · 1 line
2.31.0
Re: [PATCH 3/5] userfaultfd.2: Add write-protect mode
Hello Alex, Peter,
I've applied the patch below, but I have some questions.
Quoted 60 lines · Alejandro Colomar
From: Peter Xu <[email protected]>
Write-protect mode is supported starting from Linux 5.7.
Acked-by Mike Rapoport <[email protected]>Signed-off-by Peter Xu <[email protected]>Signed-off-by Alejandro Colomar <[email protected]>--- man2/userfaultfd.2 | 108 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-)man2/userfaultfd.2 +27
-1@@ -78,6 +78,32 @@ all memory ranges that were registered with the object are unregistered and unread events are flushed. .\" .PP +Userfaultfd supports two modes of registration: +.TP +.BR UFFDIO_REGISTER_MODE_MISSING " (since 4.10)" +When registered with +.B UFFDIO_REGISTER_MODE_MISSING +mode, the userspace will receive a page fault message +when a missing page is accessed. +The faulted thread will be stopped from execution until the page fault is +resolved from the userspace by either an +.B UFFDIO_COPY +or an +.B UFFDIO_ZEROPAGE +ioctl. +.TP +.BR UFFDIO_REGISTER_MODE_WP " (since 5.7)" +When registered with +.B UFFDIO_REGISTER_MODE_WP +mode, the userspace will receive a page fault message +when a write-protected page is written. +The faulted thread will be stopped from execution +until the userspace write-unprotect the page using an +.B UFFDIO_WRITEPROTECT +ioctl. +.PP +Multiple modes can be enabled at the same time for the same memory range. +.PP Since Linux 4.14, userfaultfd page fault message can selectively embed faulting thread ID information into the fault message. One needs to enable this feature explicitly using the@@ -107,7 +133,7 @@ the process that monitors userfaultfd and handles page faults needs to be aware of the changes in the virtual memory layout of the faulting process to avoid memory corruption. .PP -Starting from Linux 4.11, +Since Linux 4.11, userfaultfd can also notify the fault-handling threads about changes in the virtual memory layout of the faulting process. In addition, if the faulting process invokes@@ -144,6 +170,17 @@ single threaded non-cooperative userfaultfd manager implementations. .\" and limitations remaining in 4.11 .\" Maybe it's worth adding a dedicated sub-section...
man2/userfaultfd.2 single threaded non-cooperative userfaultfd manager implementations. · line 181
Quoted 10 lines
man2/userfaultfd.2 +9
-0.\" +.PP +Since Linux 5.7, userfaultfd is able to do +synchronous page dirty tracking using the new write-protect register mode. +One should check against the feature bit +.B UFFD_FEATURE_PAGEFAULT_FLAG_WP +before using this feature. +Similar to the original userfaultfd missing mode, the write-protect mode will +generate an userfaultfd message when the protected page is written. +The user needs to resolve the page fault by unprotecting the faulted page and
You use "user-space" elsewhere, but "the user" here. What is the difference?
More generally, What is "the user" in this context? I think you really mean something like "the trap-handling thread"" or something like that? The same statement applies for various pieces below.
man2/userfaultfd.2 single threaded non-cooperative userfaultfd manager implementations. · line 182
Quoted 1 line
man2/userfaultfd.2 +1
-0+kick the faulted thread to continue.
What does "kick" mean here. This should be explained.
Quoted 16 lines
man2/userfaultfd.2 +2
-1+For more information, please refer to "Userfaultfd write-protect mode" section. .SS Userfaultfd operation After the userfaultfd object is created with .BR userfaultfd (),@@ -179,7 +216,7 @@ or .BR ioctl (2) operations to resolve the page fault. .PP -Starting from Linux 4.14, if the application sets the +Since Linux 4.14, if the application sets the .B UFFD_FEATURE_SIGBUS feature bit using the .B UFFDIO_API@@ -219,6 +256,65 @@ userfaultfd can be used only with anonymous private memory mappings. Since Linux 4.11, userfaultfd can be also used with hugetlbfs and shared memory mappings.
man2/userfaultfd.2 userfaultfd can be used only with anonymous private memory mappings. · line 267
Quoted 10 lines
man2/userfaultfd.2 +9
-0.\" +.SS Userfaultfd write-protect mode (since 5.7) +Since Linux 5.7, userfaultfd supports write-protect mode. +The user needs to first check availability of this feature using +.B UFFDIO_API +ioctl against the feature bit +.B UFFD_FEATURE_PAGEFAULT_FLAG_WP +before using this feature. +.PP +To register with userfaultfd write-protect mode, the user needs to initiate the
You use "user-space" elsewhere, but "the user" here. What is the difference?
man2/userfaultfd.2 userfaultfd can be used only with anonymous private memory mappings. · line 273
Quoted 6 lines
man2/userfaultfd.2 +6
-0+.B UFFDIO_REGISTER +ioctl with mode +.B UFFDIO_REGISTER_MODE_WP +set. +Note that it's legal to monitor the same memory range with multiple modes. +For example, the user can do
You use "user-space" elsewhere, but "the user" here. What is the difference?
man2/userfaultfd.2 userfaultfd can be used only with anonymous private memory mappings. · line 283
Quoted 10 lines
man2/userfaultfd.2 +10
-0+.B UFFDIO_REGISTER +with the mode set to +.BR "UFFDIO_REGISTER_MODE_MISSING | UFFDIO_REGISTER_MODE_WP" . +When there is only +.B UFFDIO_REGISTER_MODE_WP +registered, the userspace will +.I not +receive any message when a missing page is written. +Instead, the userspace will only receive a write-protect page fault message +when an existing but write-protected page got written.
The wording here is a bit off. What does it mean: "an existing but write-protected page"?
man2/userfaultfd.2 userfaultfd can be used only with anonymous private memory mappings. · line 290
Quoted 7 lines
man2/userfaultfd.2 +7
-0+.PP +After the +.B UFFDIO_REGISTER +ioctl completed with +.B UFFDIO_REGISTER_MODE_WP +mode set, +the user can write-protect any existing memory within the range using the ioctl
You use "user-space" elsewhere, but "the user" here. What is the difference?
Quoted 9 lines
man2/userfaultfd.2 +9
-0+.B UFFDIO_WRITEPROTECT +where +.I uffdio_writeprotect.mode +should be set to +.BR UFFDIO_WRITEPROTECT_MODE_WP . +.PP +When a write-protect event happens, +the userspace will receive a page fault message whose +.I uffd_msg.pagefault.flags
man2/userfaultfd.2 userfaultfd can be used only with anonymous private memory mappings. · line 309
Quoted 10 lines
man2/userfaultfd.2 +10
-0+will be with +.B UFFD_PAGEFAULT_FLAG_WP +flag set. +Note: since only writes can trigger such kind of fault, +write-protect messages will always be with +.B UFFD_PAGEFAULT_FLAG_WRITE +bit set too along with bit +.BR UFFD_PAGEFAULT_FLAG_WP . +.PP +To resolve a write-protection page fault, the user should initiate another
You use "user-space" elsewhere, but "the user" here. What is the difference?
Quoted 16 lines
man2/userfaultfd.2 +8
-1+.B UFFDIO_WRITEPROTECT +ioctl, whose +.I uffd_msg.pagefault.flags +should have the flag +.B UFFDIO_WRITEPROTECT_MODE_WP +cleared upon the faulted page or range. +.PP +Write-protect mode only supports private anonymous memory. .SS Reading from the userfaultfd structure Each .BR read (2)@@ -364,8 +460,12 @@ flag (see .BR ioctl_userfaultfd (2)) and this flag is set, this a write fault; otherwise it is a read fault. -.\"
man2/userfaultfd.2 flag (see · line 471
Quoted 10 lines
man2/userfaultfd.2 +6
-1-.\" UFFD_PAGEFAULT_FLAG_WP is not yet supported. +.TP +.B UFFD_PAGEFAULT_FLAG_WP +If the address is in a range that was registered with the +.B UFFDIO_REGISTER_MODE_WP +flag, when this bit is set it means it's a write-protect fault. +Otherwise it's a page missing fault. .RE .TP .I pagefault.feat.pid
Thanks,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH 3/5] userfaultfd.2: Add write-protect mode
Quoted 1 line · Michael Kerrisk (man-pages)
Hello Alex, Peter,
Hi, Michael,
[...]
Quoted 7 lines
Quoted 7 lines
man2/userfaultfd.2 +3
-0@@ -144,6 +170,17 @@ single threaded non-cooperative userfaultfd manager implementations. .\" and limitations remaining in 4.11 .\" Maybe it's worth adding a dedicated sub-section... .\" +.PP +Since Linux 5.7, userfaultfd is able to do +synchronous page dirty tracking using the new write-protect register mode.
Quoted 8 lines
man2/userfaultfd.2 single threaded non-cooperative userfaultfd manager implementations. · line 181
Quoted 6 lines
man2/userfaultfd.2 +6
-0+One should check against the feature bit +.B UFFD_FEATURE_PAGEFAULT_FLAG_WP +before using this feature. +Similar to the original userfaultfd missing mode, the write-protect mode will +generate an userfaultfd message when the protected page is written. +The user needs to resolve the page fault by unprotecting the faulted page andYou use "user-space" elsewhere, but "the user" here. What is the difference?
I wanted to mean the same idea of user-space indeed.
Quoted 4 lines
More generally, What is "the user" in this context? I think you really mean something like "the trap-handling thread"" or something like that? The same statement applies for various pieces below.
Yes, it's just a more general wording since the trap-handling thread in the userfaultfd world belongs to the user, or the user-space.
Quoted 4 lines
man2/userfaultfd.2 single threaded non-cooperative userfaultfd manager implementations. · line 182
Quoted 2 lines
man2/userfaultfd.2 +1
-0+kick the faulted thread to continue.What does "kick" mean here. This should be explained.
Sorry about that, "kick" is a very frequently used wording for developers but probably confusing for manpage readers, it means "wake up".
Quoted 21 lines
Quoted 21 lines
man2/userfaultfd.2 +5
-1+For more information, please refer to "Userfaultfd write-protect mode" section. .SS Userfaultfd operation After the userfaultfd object is created with .BR userfaultfd (),@@ -179,7 +216,7 @@ or .BR ioctl (2) operations to resolve the page fault. .PP -Starting from Linux 4.14, if the application sets the +Since Linux 4.14, if the application sets the .B UFFD_FEATURE_SIGBUS feature bit using the .B UFFDIO_API@@ -219,6 +256,65 @@ userfaultfd can be used only with anonymous private memory mappings. Since Linux 4.11, userfaultfd can be also used with hugetlbfs and shared memory mappings. .\" +.SS Userfaultfd write-protect mode (since 5.7) +Since Linux 5.7, userfaultfd supports write-protect mode. +The user needs to first check availability of this feature using
Quoted 8 lines
man2/userfaultfd.2 userfaultfd can be used only with anonymous private memory mappings. · line 267
Quoted 6 lines
man2/userfaultfd.2 +6
-0+.B UFFDIO_API +ioctl against the feature bit +.B UFFD_FEATURE_PAGEFAULT_FLAG_WP +before using this feature. +.PP +To register with userfaultfd write-protect mode, the user needs to initiate theYou use "user-space" elsewhere, but "the user" here. What is the difference?
(I'm skipping same questions)
[...]
Quoted 5 lines
Quoted 5 lines
man2/userfaultfd.2 +5
-0+.B UFFDIO_REGISTER +with the mode set to +.BR "UFFDIO_REGISTER_MODE_MISSING | UFFDIO_REGISTER_MODE_WP" . +When there is only +.B UFFDIO_REGISTER_MODE_WP
Quoted 8 lines
man2/userfaultfd.2 userfaultfd can be used only with anonymous private memory mappings. · line 283
Quoted 5 lines
man2/userfaultfd.2 +5
-0+registered, the userspace will +.I not +receive any message when a missing page is written. +Instead, the userspace will only receive a write-protect page fault message +when an existing but write-protected page got written.The wording here is a bit off. What does it mean: "an existing but write-protected page"?
I wanted to emphasize the fact that this write-protect message won't be generated if the page does not exist at all. That belongs to the missing page fault even if a memory write triggerred the page fault.
Thanks,
Signature · 1 line
Peter Xu
PATCH 4/5 ioctl_userfaultfd.2: Add UFFD_FEATURE_THREAD_ID docs
In reply to Alejandro ColomarFrom: Peter Xu <[email protected]>
UFFD_FEATURE_THREAD_ID is supported in Linux 4.14.
--- man2/ioctl_userfaultfd.2 | 5 +++++ 1 file changed, 5 insertions(+)
man2/ioctl_userfaultfd.2 +5 -0
@@ -208,6 +208,11 @@ signal will be sent to the faulting process. Applications using this feature will not require the use of a userfaultfd monitor for processing memory accesses to the regions registered with userfaultfd. +.TP +.BR UFFD_FEATURE_THREAD_ID " (since Linux 4.14)" +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. .PP The returned .I ioctls
Signature · 1 line
2.31.0
Re: [PATCH 4/5] ioctl_userfaultfd.2: Add UFFD_FEATURE_THREAD_ID docs
Hello Alex, Peter.
Quoted 7 lines · Alejandro Colomar
From: Peter Xu <[email protected]>
UFFD_FEATURE_THREAD_IDis supported in Linux 4.14.Acked-by Mike Rapoport <[email protected]>Signed-off-by Peter Xu <[email protected]>Signed-off-by Alejandro Colomar <[email protected]>
Patch applied.
Thanks,
Michael
Quoted 21 lines
--- man2/ioctl_userfaultfd.2 | 5 +++++ 1 file changed, 5 insertions(+)man2/ioctl_userfaultfd.2 +5
-0@@ -208,6 +208,11 @@ signal will be sent to the faulting process. Applications using this feature will not require the use of a userfaultfd monitor for processing memory accesses to the regions registered with userfaultfd. +.TP +.BR UFFD_FEATURE_THREAD_ID " (since Linux 4.14)" +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. .PP The returned .I ioctls
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 5/5 ioctl_userfaultfd.2: Add write-protect mode docs
In reply to Alejandro ColomarFrom: Peter Xu <[email protected]>
Userfaultfd write-protect mode is supported starting from Linux 5.7.
--- man2/ioctl_userfaultfd.2 | 86 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-)
man2/ioctl_userfaultfd.2 +83 -3
@@ -234,6 +234,11 @@ operation is supported. The .B UFFDIO_UNREGISTER operation is supported. +.TP +.B 1 << _UFFDIO_WRITEPROTECT +The +.B UFFDIO_WRITEPROTECT +operation is supported. .PP This .BR ioctl (2)
@@ -322,9 +327,6 @@ Track page faults on missing pages. .B UFFDIO_REGISTER_MODE_WP Track page faults on write-protected pages. .PP -Currently, the only supported mode is -.BR UFFDIO_REGISTER_MODE_MISSING . -.PP If the operation is successful, the kernel modifies the .I ioctls bit-mask field to indicate which
@@ -443,6 +445,16 @@ operation: .TP .B UFFDIO_COPY_MODE_DONTWAKE Do not wake up the thread that waits for page-fault resolution +.TP +.B UFFDIO_COPY_MODE_WP +Copy the page with read-only permission. +This allows the user to trap the next write to the page, +which will block and generate another write-protect userfault message. +This is only used when both +.B UFFDIO_REGISTER_MODE_MISSING +and +.B UFFDIO_REGISTER_MODE_WP +modes are enabled for the registered range. .PP The .I copy
@@ -654,6 +666,74 @@ 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) +Write-protect or write-unprotect an userfaultfd registered memory range +registered with mode +.BR UFFDIO_REGISTER_MODE_WP . +.PP +The +.I argp +argument is a pointer to a +.I uffdio_range +structure as shown below: +.PP +.in +4n +.EX +struct uffdio_writeprotect { + struct uffdio_range range; /* Range to change write permission*/ + __u64 mode; /* Mode to change write permission */ +}; +.EE +.in +.PP +There're two mode bits that are supported in this structure: +.TP +.B UFFDIO_WRITEPROTECT_MODE_WP +When this mode bit is set, +the ioctl will be a write-protect operation upon the memory range specified by +.IR range . +Otherwise it'll be a write-unprotect operation upon the specified range, +which can be used to resolve an userfaultfd write-protect page fault. +.TP +.B UFFDIO_WRITEPROTECT_MODE_DONTWAKE +When this mode bit is set, +do not wake up any thread that waits for +page-fault resolution after the operation. +This could only be specified if +.B UFFDIO_WRITEPROTECT_MODE_WP +is not specified. +.PP +This +.BR ioctl (2) +operation returns 0 on success. +On error, \-1 is returned and +.I errno +is set to indicate the error. +Possible errors include: +.TP +.B EINVAL +The +.I start +or the +.I len +field of the +.I ufdio_range +structure was not a multiple of the system page size; or +.I len +was zero; or the specified range was otherwise invalid. +.TP +.B EAGAIN +The process was interrupted; retry this call. +.TP +.B ENOENT +The range specified in +.I range +is not valid. +For example, the virtual address does not exist, +or not registered with userfaultfd write-protect mode. +.TP +.B EFAULT +Encountered a generic fault during processing. .SH RETURN VALUE See descriptions of the individual operations, above. .SH ERRORS
Signature · 1 line
2.31.0
Re: [PATCH 5/5] ioctl_userfaultfd.2: Add write-protect mode docs
Hello Alex, Peter,
I've applied the patch below, but I have some questions.
Quoted 39 lines · Alejandro Colomar
From: Peter Xu <[email protected]>
Userfaultfd write-protect mode is supported starting from Linux 5.7.
Acked-by Mike Rapoport <[email protected]>Signed-off-by Peter Xu <[email protected]>[alx: ffix + srcfix]Signed-off-by Alejandro Colomar <[email protected]>--- man2/ioctl_userfaultfd.2 | 86 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-)man2/ioctl_userfaultfd.2 +5
-3@@ -234,6 +234,11 @@ operation is supported. The .B UFFDIO_UNREGISTER operation is supported. +.TP +.B 1 << _UFFDIO_WRITEPROTECT +The +.B UFFDIO_WRITEPROTECT +operation is supported. .PP This .BR ioctl (2)@@ -322,9 +327,6 @@ Track page faults on missing pages. .B UFFDIO_REGISTER_MODE_WP Track page faults on write-protected pages. .PP -Currently, the only supported mode is -.BR UFFDIO_REGISTER_MODE_MISSING . -.PP If the operation is successful, the kernel modifies the .I ioctls bit-mask field to indicate which
man2/ioctl_userfaultfd.2 operation: · line 451
Quoted 7 lines
man2/ioctl_userfaultfd.2 +4
-0@@ -443,6 +445,16 @@ operation: .TP .B UFFDIO_COPY_MODE_DONTWAKE Do not wake up the thread that waits for page-fault resolution +.TP +.B UFFDIO_COPY_MODE_WP +Copy the page with read-only permission. +This allows the user to trap the next write to the page,
What is "the user" in this context? I think you really mean something like "the fault-handling thread"" or something like that?
Quoted 74 lines
man2/ioctl_userfaultfd.2 +67
-0+which will block and generate another write-protect userfault message. +This is only used when both +.B UFFDIO_REGISTER_MODE_MISSING +and +.B UFFDIO_REGISTER_MODE_WP +modes are enabled for the registered range. .PP The .I copy@@ -654,6 +666,74 @@ 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) +Write-protect or write-unprotect an userfaultfd registered memory range +registered with mode +.BR UFFDIO_REGISTER_MODE_WP . +.PP +The +.I argp +argument is a pointer to a +.I uffdio_range +structure as shown below: +.PP +.in +4n +.EX +struct uffdio_writeprotect { + struct uffdio_range range; /* Range to change write permission*/ + __u64 mode; /* Mode to change write permission */ +}; +.EE +.in +.PP +There're two mode bits that are supported in this structure: +.TP +.B UFFDIO_WRITEPROTECT_MODE_WP +When this mode bit is set, +the ioctl will be a write-protect operation upon the memory range specified by +.IR range . +Otherwise it'll be a write-unprotect operation upon the specified range, +which can be used to resolve an userfaultfd write-protect page fault. +.TP +.B UFFDIO_WRITEPROTECT_MODE_DONTWAKE +When this mode bit is set, +do not wake up any thread that waits for +page-fault resolution after the operation. +This could only be specified if +.B UFFDIO_WRITEPROTECT_MODE_WP +is not specified. +.PP +This +.BR ioctl (2) +operation returns 0 on success. +On error, \-1 is returned and +.I errno +is set to indicate the error. +Possible errors include: +.TP +.B EINVAL +The +.I start +or the +.I len +field of the +.I ufdio_range +structure was not a multiple of the system page size; or +.I len +was zero; or the specified range was otherwise invalid. +.TP +.B EAGAIN +The process was interrupted; retry this call. +.TP +.B ENOENT +The range specified in
man2/ioctl_userfaultfd.2 field of the · line 739
Quoted 10 lines
man2/ioctl_userfaultfd.2 +7
-0+.I range +is not valid. +For example, the virtual address does not exist, +or not registered with userfaultfd write-protect mode. +.TP +.B EFAULT +Encountered a generic fault during processing. .SH RETURN VALUE See descriptions of the individual operations, above. .SH ERRORS
Thanks,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Quoted 4 lines · Alejandro Colomar
Hi Michael,
These are the patches I have merged from others.
Thanks, Alex! All merged and pushed now.
Cheers,
Michael
Quoted 17 lines
Alyssa Ross (1):
scanf.3: clarify that %n supports type modifiersPeter Xu (4):
userfaultfd.2: AddUFFD_FEATURE_THREAD_IDdocs userfaultfd.2: Add write-protect mode ioctl_userfaultfd.2: AddUFFD_FEATURE_THREAD_IDdocs ioctl_userfaultfd.2: Add write-protect mode docsman2/ioctl_userfaultfd.2 | 91 ++++++++++++++++++++++++++++- man2/userfaultfd.2 | 121 +++++++++++++++++++++++++++++++++++++-- man3/scanf.3 | 4 +- 3 files changed, 208 insertions(+), 8 deletions(-)--
2.31.0
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/