Fix userfaultfd_api to return EINVAL as expected
Subsystems (5)
PATCH v3 1/3 Fix userfaultfd_api to return EINVAL as expected
Currently if we request a feature that is not set in the Kernel config we fail silently and return all the available features. However, the man page indicates we should return an EINVAL.
We need to fix this issue since we can end up with a Kernel warning should a program request the feature UFFD_FEATURE_WP_UNPOPULATED on a kernel with the config not set with this feature.
[ 200.812896] WARNING: CPU: 91 PID: 13634 at mm/memory.c:1660 zap_pte_range+0x43d/0x660 [ 200.820738] Modules linked in: [ 200.869387] CPU: 91 PID: 13634 Comm: userfaultfd Kdump: loaded Not tainted 6.9.0-rc5+ #8 [ 200.877477] Hardware name: Dell Inc. PowerEdge R6525/0N7YGH, BIOS 2.7.3 03/30/2022 [ 200.885052] RIP: 0010:zap_pte_range+0x43d/0x660
--- fs/userfaultfd.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/userfaultfd.c +6 -1
@@ -2057,7 +2057,7 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, goto out; features = uffdio_api.features; ret = -EINVAL ; -if (uffdio_api.api !=UFFD_API || (features & ~UFFD_API_FEATURES )) +if (uffdio_api.api !=UFFD_API ) goto err_out; ret = -EPERM ;if ((features &UFFD_FEATURE_EVENT_FORK ) && !capable (CAP_SYS_PTRACE ))
@@ -2081,6 +2081,11 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, uffdio_api.features &= ~UFFD_FEATURE_WP_UNPOPULATED ; uffdio_api.features &= ~UFFD_FEATURE_WP_ASYNC ;#endif + + ret = -EINVAL ; +if (features & ~uffdio_api.features) + goto err_out; + uffdio_api.ioctls =UFFD_API_IOCTLS ; ret = -EFAULT ;if (copy_to_user (buf, &uffdio_api,sizeof (uffdio_api)))
Signature · 1 line
2.44.0
PATCH v3 2/3 Update uffd-stress to handle EINVAL for unset config features
Now that we have updated userfaultfd_api to correctly return EIVAL when a feature is requested but not available, let's fix the uffd-stress test to only set the UFFD_FEATURE_WP_UNPOPULATED feature when the config is set. In addition, still run the test if the CONFIG_PTE_MARKER_UFFD_WP is not set, just dont use the corresponding UFFD_FEATURE_WP_UNPOPULATED feature.
--- tools/testing/selftests/mm/uffd-stress.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
tools/testing/selftests/mm/uffd-stress.c +9 -5
@@ -38,6 +38,8 @@#ifdef __NR_userfaultfd +uint64_t features; +#define BOUNCE_RANDOM (1<<0)#define BOUNCE_RACINGFAULTS (1<<1)#define BOUNCE_VERIFY (1<<2)
@@ -247,10 +249,14 @@ static int userfaultfd_stress(void)unsigned long nr;struct uffd_args args[nr_cpus];uint64_t mem_size = nr_pages * page_size; +int flags =0 ;memset (args,0 ,sizeof (struct uffd_args ) * nr_cpus); -if (uffd_test_ctx_init (UFFD_FEATURE_WP_UNPOPULATED ,NULL )) +if (features &UFFD_FEATURE_WP_UNPOPULATED && test_type ==TEST_ANON ) + flags =UFFD_FEATURE_WP_UNPOPULATED ; + +if (uffd_test_ctx_init (flags,NULL ))err ("context init failed" );if (posix_memalign (&area, page_size, page_size))
@@ -385,8 +391,6 @@ static void set_test_type(const char *type)static void parse_test_type_arg (const char *raw_type) { -uint64_t features =UFFD_API_FEATURES ; -set_test_type (raw_type);if (!test_type)
@@ -409,8 +413,8 @@ static void parse_test_type_arg(const char *raw_type)* feature. */ -if (userfaultfd_open (&features)) -err ("Userfaultfd open failed" ); +if (uffd_get_features (&features)) +err ("failed to get available features" ); test_uffdio_wp = test_uffdio_wp && (features &UFFD_FEATURE_PAGEFAULT_FLAG_WP );
Signature · 1 line
2.44.0
PATCH v3 3/3 Turn off test_uffdio_wp if CONFIG_PTE_MARKER_UFFD_WP is not configured.
In reply to Audra MitchellIf CONFIG_PTE_MARKER_UFFD_WP is disabled, then we turn off three features in userfaultfd_api (UFFD_FEATURE_WP_HUGETLBFS_SHMEM, UFFD_FEATURE_WP_UNPOPULATED, and UFFD_FEATURE_WP_ASYNC). Currently this test always will call uffdio_regsiter with the flag UFFDIO_REGISTER_MODE_WP. However, the kernel ensures in vma_can_userfault that if the feature UFFD_FEATURE_WP_HUGETLBFS_SHMEM is disabled, only allow the VM_UFFD_WP on anonymous vmas, meaning our call to uffdio_regsiter will fail. We still want to be able to run the test even if we have CONFIG_PTE_MARKER_UFFD_WP disabled, so check to see if the feature UFFD_FEATURE_WP_HUGETLBFS_SHMEM has been turned off in the test and if so, disable us from calling uffdio_regsiter with the flag UFFDIO_REGISTER_MODE_WP.
--- tools/testing/selftests/mm/uffd-stress.c | 3 +++ 1 file changed, 3 insertions(+)
tools/testing/selftests/mm/uffd-stress.c +3 -0
@@ -419,6 +419,9 @@ static void parse_test_type_arg(const char *raw_type) test_uffdio_wp = test_uffdio_wp && (features &UFFD_FEATURE_PAGEFAULT_FLAG_WP ); +if (test_type !=TEST_ANON && !(features &UFFD_FEATURE_WP_HUGETLBFS_SHMEM )) + test_uffdio_wp = false; +close (uffd); uffd =-1 ; }
Signature · 1 line
2.44.0
Re: [PATCH v3 3/3] Turn off test_uffdio_wp if CONFIG_PTE_MARKER_UFFD_WP is not configured.
Quoted 13 lines · Audra Mitchell
If
CONFIG_PTE_MARKER_UFFD_WPis disabled, then we turn off three features inuserfaultfd_api(UFFD_FEATURE_WP_HUGETLBFS_SHMEM,UFFD_FEATURE_WP_UNPOPULATED, andUFFD_FEATURE_WP_ASYNC). Currently this test always will calluffdio_regsiterwith the flagUFFDIO_REGISTER_MODE_WP. However, the kernel ensures invma_can_userfaultthat if the featureUFFD_FEATURE_WP_HUGETLBFS_SHMEMis disabled, only allow theVM_UFFD_WPon anonymous vmas, meaning our call touffdio_regsiterwill fail. We still want to be able to run the test even if we haveCONFIG_PTE_MARKER_UFFD_WPdisabled, so check to see if the featureUFFD_FEATURE_WP_HUGETLBFS_SHMEMhas been turned off in the test and if so, disable us from callinguffdio_regsiterwith the flagUFFDIO_REGISTER_MODE_WP.
Quoted 1 line
Signed-off-by Audra Mitchell <[email protected]>
Quoted 21 lines
--- tools/testing/selftests/mm/uffd-stress.c | 3 +++ 1 file changed, 3 insertions(+)tools/testing/selftests/mm/uffd-stress.c +3
-0@@ -419,6 +419,9 @@ static void parse_test_type_arg(const char *raw_type) test_uffdio_wp = test_uffdio_wp && (features &UFFD_FEATURE_PAGEFAULT_FLAG_WP ); +if (test_type !=TEST_ANON && !(features &UFFD_FEATURE_WP_HUGETLBFS_SHMEM )) + test_uffdio_wp = false; +close (uffd); uffd =-1 ; }Signature · 1 line
2.44.0
Signature · 1 line
Peter Xu