selftests/mm: Split / Refactor userfault test
Subsystems (7)
Peter Xu
reviewercover selftests/mm: Split / Refactor userfault test
[Sorry for the test case bomb]
This patchset splits userfaultfd.c into two tests:
- uffd-stress: the "vanilla", old and powerful stress test
- uffd-unit-tests: all the unit tests will be moved here
This is on my todo list for a long time but I never did it for real. The uffd test is growing into a small and cute monster. I start to notice it's going harder to maintain such a test and make it useful.
A few issues I found when looking at userfaultfd test:
- We have a bunch of unit tests in userfaultfd.c, but they always need to be run only after a stress type. No way to not do it.
- We can only run an unit test for one memory type only, if we want to do a quick smoke test to check regressions, there's no good way. The best to come currently is "bash ./run_vmtests.sh -t userfaultfd" thanks to the most recent changes to run_vmtests.sh on tagging. Still, that needs to run the stress tests always and hard to see what's wrong.
- It's hard to add a new unit test to userfaultfd.c, we don't really know what's happening, not until we mostly read the whole file.
- We did a bunch of useless tests, e.g. we run twice the whole suite of stress test just to verify both syscall and /dev/userfaultfd. They're all using userfaultfd_new() to create the handle, everything should really be the same underneath. One simple unit test should cover that!
- We have tens of global variables in one file but shared with all the tests. Some of them are not suitable to be a global var from maintainance pov. It enforces every unit test to consider how these vars affects the stress test and vice versa, but that's logically not necessary.
- Userfaultfd test is not friendly to old kernels. Mostly it only works on the latest kernel tree. It's preferrable to be run on all kernels and properly report what's missing.
I'll stop here, I feel like I can still list some..
This patchset should resolve all issues above, and actually we can do even more on top. I stopped doing that until I found I already got 29 patches and 2000+ LOC changes. That's already a patchset terrible enough so we should move in small steps.
After the whole set applied, "./run_vmtests.sh -t userfaultfd" looks like this:
===8<===
vm.nr_hugepages = 1024
-------------------------
running ./uffd-unit-tests
-------------------------UFFDIO_API (with syscall)... doneUFFDIO_API (with /dev/userfaultfd)... done
zeropage on anon... done
zeropage on shmem... done
zeropage on shmem-private... done
zeropage-hugetlb on hugetlb... done
zeropage-hugetlb on hugetlb-private... done
pagemap on anon... pagemap on anon... pagemap on anon... done
wp-unpopulated on anon... done
minor on shmem... done
minor on hugetlb... done
minor-wp on shmem... done
minor-wp on hugetlb... done
minor-collapse on shmem... done
sigbus on anon... sigbus on anon... done
sigbus on shmem... sigbus on shmem... done
sigbus on shmem-private... sigbus on shmem-private... done
sigbus on hugetlb... sigbus on hugetlb... done
sigbus on hugetlb-private... sigbus on hugetlb-private... done
sigbus-wp on anon... sigbus-wp on anon... done
sigbus-wp on shmem... sigbus-wp on shmem... done
sigbus-wp on shmem-private... sigbus-wp on shmem-private... done
sigbus-wp on hugetlb... sigbus-wp on hugetlb... done
sigbus-wp on hugetlb-private... sigbus-wp on hugetlb-private... done
events on anon... events on anon... done
events on shmem... events on shmem... done
events on shmem-private... events on shmem-private... done
events on hugetlb... events on hugetlb... done
events on hugetlb-private... events on hugetlb-private... done
events-wp on anon... events-wp on anon... done
events-wp on shmem... events-wp on shmem... done
events-wp on shmem-private... events-wp on shmem-private... done
events-wp on hugetlb... events-wp on hugetlb... done
events-wp on hugetlb-private... events-wp on hugetlb-private... done
Userfaults unit tests: pass=34, skip=0, fail=0 (total=34)
[PASS]
--------------------------------
running ./uffd-stress anon 20 16
--------------------------------nr_pages: 5120, nr_pages_per_cpu: 640
bounces: 15, mode: rnd racing ver poll, userfaults: 345 missing (26+48+61+102+30+12+59+7) 1596 wp (120+139+317+346+215+67+306+86) [...]
[PASS]
------------------------------------
running ./uffd-stress hugetlb 128 32
------------------------------------nr_pages: 64, nr_pages_per_cpu: 8
bounces: 31, mode: rnd racing ver poll, userfaults: 29 missing (6+6+6+5+4+2+0+0) 104 wp (20+19+22+18+7+12+5+1) [...]
[PASS]
--------------------------------------------
running ./uffd-stress hugetlb-private 128 32
--------------------------------------------nr_pages: 64, nr_pages_per_cpu: 8
bounces: 31, mode: rnd racing ver poll, userfaults: 33 missing (12+9+7+0+5+0+0+0) 111 wp (24+25+14+14+11+17+5+1) [...]
[PASS]
---------------------------------
running ./uffd-stress shmem 20 16
---------------------------------nr_pages: 5120, nr_pages_per_cpu: 640
bounces: 15, mode: rnd racing ver poll, userfaults: 247 missing (15+17+34+60+81+37+3+0) 2038 wp (180+114+276+400+381+318+165+204) [...]
[PASS]
-----------------------------------------
running ./uffd-stress shmem-private 20 16
-----------------------------------------nr_pages: 5120, nr_pages_per_cpu: 640
bounces: 15, mode: rnd racing ver poll, userfaults: 235 missing (52+29+55+56+13+9+16+5) 2849 wp (218+406+461+531+328+284+430+191) [...]
[PASS]
SUMMARY: PASS=6 SKIP=0 FAIL=0
===8<===
The output may be different if we miss some features (e.g., hugetlb not allocated, old kernel, less privilege of uffd handle), but they should show up with good reasons. E.g., I tried to run the unit test on my Fedora kernel and it gives me:
===8<===UFFDIO_API (with syscall)... failed [reason: UFFDIO_API should fail with wrong api but didn't] UFFDIO_API (with /dev/userfaultfd)... skipped [reason: cannot open userfaultfd handle] zeropage on anon... done
zeropage on shmem... done
zeropage on shmem-private... done
zeropage-hugetlb on hugetlb... done
zeropage-hugetlb on hugetlb-private... done
pagemap on anon... pagemap on anon... pagemap on anon... done
wp-unpopulated on anon... skipped [reason: feature missing]
minor on shmem... done
minor on hugetlb... done
minor-wp on shmem... skipped [reason: feature missing]
minor-wp on hugetlb... skipped [reason: feature missing]
minor-collapse on shmem... done
sigbus on anon... skipped [reason: possible lack of priviledge]
sigbus on shmem... skipped [reason: possible lack of priviledge]
sigbus on shmem-private... skipped [reason: possible lack of priviledge]
sigbus on hugetlb... skipped [reason: possible lack of priviledge]
sigbus on hugetlb-private... skipped [reason: possible lack of priviledge] sigbus-wp on anon... skipped [reason: possible lack of priviledge]
sigbus-wp on shmem... skipped [reason: possible lack of priviledge]
sigbus-wp on shmem-private... skipped [reason: possible lack of priviledge] sigbus-wp on hugetlb... skipped [reason: possible lack of priviledge]
sigbus-wp on hugetlb-private... skipped [reason: possible lack of priviledge] events on anon... skipped [reason: possible lack of priviledge]
events on shmem... skipped [reason: possible lack of priviledge]
events on shmem-private... skipped [reason: possible lack of priviledge]
events on hugetlb... skipped [reason: possible lack of priviledge]
events on hugetlb-private... skipped [reason: possible lack of priviledge] events-wp on anon... skipped [reason: possible lack of priviledge]
events-wp on shmem... skipped [reason: possible lack of priviledge]
events-wp on shmem-private... skipped [reason: possible lack of priviledge] events-wp on hugetlb... skipped [reason: possible lack of priviledge]
events-wp on hugetlb-private... skipped [reason: possible lack of priviledge] Userfaults unit tests: pass=9, skip=24, fail=1 (total=34)
===8<===
Patch layout:
- Revert "userfaultfd: don't fail on unrecognized features"
Something I found when I got the UFFDIO_API test below. Axel, I still propose to revert it as a whole, but feel free to continue the discussion from the original patch thread.
- selftests/mm: Update .gitignore with two missing tests
- selftests/mm: Dump a summary in run_vmtests.sh
- selftests/mm: Merge util.h into vm_util.h
- selftests/mm: Use TEST_GEN_PROGS where proper
- selftests/mm: Link vm_util.c always
- selftests/mm: Merge default_huge_page_size() into one
- selftests/mm: Use PM_* macros in vm_utils.h
- selftests/mm: Reuse pagemap_get_entry() in vm_util.h
- selftests/mm: Test UFFDIO_ZEROPAGE only when !hugetlb
- selftests/mm: Drop test_uffdio_zeropage_eexist
Until here, all cleanups here and there. I wanted to keep going, but I found that maybe it'll take a few more days to split the test. Hence I did a split starting from the next one, so we have a working thing first.
- selftests/mm: Create uffd-common.[ch]
- selftests/mm: Split uffd tests into uffd-stress and uffd-unit-tests
This did the major brute force split of common codes into uffd-common.[ch]. That'll be the so far common base for stress and unit tests. Then a new unit test is created.
- selftests/mm: uffd_[un]register()
- selftests/mm: uffd_open_{dev|sys}()
- selftests/mm: UFFDIO_API test
This patch hides here to start writting the 1st unit test with UFFDIO_API, also detection of userfaultfd privileges.
- selftests/mm: Drop global mem_fd in uffd tests
- selftests/mm: Drop global hpage_size in uffd tests
- selftests/mm: Let uffd_handle_page_fault() takes wp parameter
- selftests/mm: Allow allocate_area() to fail properly
Some further cleanup that I noticed otherwise hard to move the tests.
- selftests/mm: Add framework for uffd-unit-test
The major patch provides the framework for most of the rest unit tests.
- selftests/mm: Move uffd pagemap test to unit test
- selftests/mm: Move uffd minor test to unit test
- selftests/mm: Move uffd sig/events tests into uffd unit tests
- selftests/mm: Move zeropage test into uffd unit tests
Move unit tests and suite them into the new file.
- selftests/mm: Workaround no way to detect uffd-minor + wp
- selftests/mm: Allow uffd test to skip properly with no privilege
- selftests/mm: Drop sys/dev test in uffd-stress test
- selftests/mm: Add shmem-private test to uffd-stress
A bunch of changes to do better on error reportings, and add shmem-private to the stress test which was long missing.
Please have a look, any comment welcomed.
Thanks,
Peter Xu (29):
Revert "userfaultfd: don't fail on unrecognized features" selftests/mm: Update .gitignore with two missing tests selftests/mm: Dump a summary in run_vmtests.sh selftests/mm: Merge util.h into vm_util.h
selftests/mm: Use TEST_GEN_PROGS where proper selftests/mm: Link vm_util.c always
selftests/mm: Merge default_huge_page_size() into one selftests/mm: Use PM_* macros in vm_utils.h selftests/mm: Reuse pagemap_get_entry() in vm_util.h selftests/mm: Test UFFDIO_ZEROPAGE only when !hugetlb selftests/mm: Drop test_uffdio_zeropage_eexist selftests/mm: Create uffd-common.[ch]
selftests/mm: Split uffd tests into uffd-stress and uffd-unit-tests selftests/mm: uffd_[un]register()
selftests/mm: uffd_open_{dev|sys}()
selftests/mm: UFFDIO_API test
selftests/mm: Drop global mem_fd in uffd tests selftests/mm: Drop global hpage_size in uffd tests selftests/mm: Let uffd_handle_page_fault() takes wp parameter selftests/mm: Allow allocate_area() to fail properly selftests/mm: Add framework for uffd-unit-test selftests/mm: Move uffd pagemap test to unit test selftests/mm: Move uffd minor test to unit test selftests/mm: Move uffd sig/events tests into uffd unit tests selftests/mm: Move zeropage test into uffd unit tests selftests/mm: Workaround no way to detect uffd-minor + wp selftests/mm: Allow uffd test to skip properly with no privilege selftests/mm: Drop sys/dev test in uffd-stress test selftests/mm: Add shmem-private test to uffd-stress
fs/userfaultfd.c | 6 +- tools/testing/selftests/mm/.gitignore | 5 +- tools/testing/selftests/mm/Makefile | 77 +- tools/testing/selftests/mm/gup_test.c | 5 +- tools/testing/selftests/mm/hugepage-mremap.c | 7 +- tools/testing/selftests/mm/hugetlb-madvise.c | 25 +- .../selftests/mm/ksm_functional_tests.c | 6 +- tools/testing/selftests/mm/ksm_tests.c | 2 +- tools/testing/selftests/mm/mrelease_test.c | 11 +- tools/testing/selftests/mm/run_vmtests.sh | 26 +- tools/testing/selftests/mm/thuge-gen.c | 19 +- tools/testing/selftests/mm/transhuge-stress.c | 12 +- tools/testing/selftests/mm/uffd-common.c | 612 ++++++ tools/testing/selftests/mm/uffd-common.h | 117 + tools/testing/selftests/mm/uffd-stress.c | 482 +++++ tools/testing/selftests/mm/uffd-unit-tests.c | 891 ++++++++ tools/testing/selftests/mm/userfaultfd.c | 1903 ----------------- tools/testing/selftests/mm/util.h | 69 - tools/testing/selftests/mm/vm_util.c | 166 +- tools/testing/selftests/mm/vm_util.h | 48 + 20 files changed, 2385 insertions(+), 2104 deletions(-) create mode 100644 tools/testing/selftests/mm/uffd-common.c create mode 100644 tools/testing/selftests/mm/uffd-common.h create mode 100644 tools/testing/selftests/mm/uffd-stress.c create mode 100644 tools/testing/selftests/mm/uffd-unit-tests.c delete mode 100644 tools/testing/selftests/mm/userfaultfd.c delete mode 100644 tools/testing/selftests/mm/util.h
Signature · 1 line
2.39.1
Peter Xu
reviewerPATCH 1/29 Revert "userfaultfd: don't fail on unrecognized features"
This is a proposal to revert commit 914eedcb9ba0ff53c33808.
I found this when writting a simple UFFDIO_API test to be the first unit test in this set. Two things breaks with the commit:
- UFFDIO_API check was lost and missing. According to man page, the kernel should reject ioctl(UFFDIO_API) if uffdio_api.api != 0xaa. This check is needed if the api version will be extended in the future, or user app won't be able to identify which is a new kernel.
- Feature flags checks were removed, which means UFFDIO_API with a feature that does not exist will also succeed. According to the man page, we should (and it makes sense) to reject ioctl(UFFDIO_API) if unknown features passed in.
--- fs/userfaultfd.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/userfaultfd.c +4 -2
@@ -1977,8 +1977,10 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, ret = -EFAULT ;if (copy_from_user (&uffdio_api, buf,sizeof (uffdio_api))) goto out; -/* Ignore unsupported features (userspace built against newer kernel) */ - features = uffdio_api.features &UFFD_API_FEATURES ; + features = uffdio_api.features; + ret = -EINVAL ; +if (uffdio_api.api !=UFFD_API || (features & ~UFFD_API_FEATURES )) + goto err_out; ret = -EPERM ;if ((features &UFFD_FEATURE_EVENT_FORK ) && !capable (CAP_SYS_PTRACE )) goto err_out;
Signature · 1 line
2.39.1
Re: [PATCH 01/29] Revert "userfaultfd: don't fail on unrecognized features"
Quoted 9 lines · Peter Xu
This is a proposal to revert commit
914eedcb9ba0ff53c33808.I found this when writting a simple
UFFDIO_APItest to be the first unit test in this set. Two things breaks with the commit:-
UFFDIO_APIcheck was lost and missing. According to man page, the kernel should rejectioctl(UFFDIO_API)if uffdio_api.api !=0xaa. This check is needed if the api version will be extended in the future, or user app won't be able to identify which is a new kernel.
Agreed.
Quoted 6 lines
- Feature flags checks were removed, which means
UFFDIO_APIwith a feature that does not exist will also succeed. According to the man page, we should (and it makes sense) to rejectioctl(UFFDIO_API)if unknown features passed in.
Agreed.
I understand the motivation of the original commit, but it should not have changed existing checks/functionality. Introducing a different way to enable such functionality on explicit request would be better. But maybe simple feature probing (is X support? is Y supported? is Z supported) might be easier without requiring ABI changes.
I assume we better add
Quoted 25 lines
Cc Axel Rasmussen <[email protected]>Cc linux-stable <[email protected]>Signed-off-by Peter Xu <[email protected]>--- fs/userfaultfd.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)fs/userfaultfd.c +4
-2@@ -1977,8 +1977,10 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, ret = -EFAULT ;if (copy_from_user (&uffdio_api, buf,sizeof (uffdio_api))) goto out; -/* Ignore unsupported features (userspace built against newer kernel) */ - features = uffdio_api.features &UFFD_API_FEATURES ; + features = uffdio_api.features; + ret = -EINVAL ; +if (uffdio_api.api !=UFFD_API || (features & ~UFFD_API_FEATURES )) + goto err_out; ret = -EPERM ;if ((features &UFFD_FEATURE_EVENT_FORK ) && !capable (CAP_SYS_PTRACE )) goto err_out;
Signature · 3 lines
Thanks, David / dhildenb
Peter Xu
reviewerRe: [PATCH 01/29] Revert "userfaultfd: don't fail on unrecognized features"
Quoted 20 lines · David Hildenbrand
Quoted 9 lines · Peter Xu
This is a proposal to revert commit
914eedcb9ba0ff53c33808.I found this when writting a simple
UFFDIO_APItest to be the first unit test in this set. Two things breaks with the commit:-
UFFDIO_APIcheck was lost and missing. According to man page, the kernel should rejectioctl(UFFDIO_API)if uffdio_api.api !=0xaa. This check is needed if the api version will be extended in the future, or user app won't be able to identify which is a new kernel.Agreed.
Quoted 6 lines
- Feature flags checks were removed, which means
UFFDIO_APIwith a feature that does not exist will also succeed. According to the man page, we should (and it makes sense) to rejectioctl(UFFDIO_API)if unknown features passed in.
Quoted 7 lines
Agreed.
I understand the motivation of the original commit, but it should not have changed existing checks/functionality. Introducing a different way to enable such functionality on explicit request would be better. But maybe simple feature probing (is X support? is Y supported? is Z supported) might be easier without requiring ABI changes.
Yes, I mentioned a similar "proposal" of UFFDIO_FEATURES here too, simply returning the feature bitmask before UFFDIO_API:
https://lore.kernel.org/all/ZCSUTSbAcwBINiNk@x1n/
But I think current way is still fine; so maybe we'd just not bother.
Quoted 4 lines
I assume we better add
Fixes 914eedcb9ba0 ("userfaultfd: don't fail on unrecognized features")
Yes I'll add it.
Quoted 1 line
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 01/29] Revert "userfaultfd: don't fail on unrecognized features"
In reply to Peter XuQuoted 10 lines · Peter Xu
This is a proposal to revert commit
914eedcb9ba0ff53c33808.I found this when writting a simple
UFFDIO_APItest to be the first unit test in this set. Two things breaks with the commit:-
UFFDIO_APIcheck was lost and missing. According to man page, the kernel should rejectioctl(UFFDIO_API)if uffdio_api.api !=0xaa. This check is needed if the api version will be extended in the future, or user app won't be able to identify which is a new kernel.
100% agreed, this was a mistake.
Quoted 5 lines
- Feature flags checks were removed, which means
UFFDIO_APIwith a feature that does not exist will also succeed. According to the man page, we should (and it makes sense) to rejectioctl(UFFDIO_API)if unknown features passed in.
I still strongly disagree with reverting this part, my feeling is still that doing so makes things more complicated for no reason.
Re: David's point, it's clearly wrong to change semantics so a thing that used to work now fails. But this instead makes it more permissive
- existing userspace programs continue to work as-is, but *also* one can achieve the same thing more simply (combine probing + configuration into one step). I don't see any problem with that, generally.
But, if David and others don't find my argument convincing, it isn't the end of the world. It just means I have to go update my userspace code to be a bit more complicated. :)
I also still think the man page is incorrect or at least incomplete no matter what we do here; we should be sure to update it as a follow-up.
Quoted 29 lines
Cc Axel Rasmussen <[email protected]>Cc linux-stable <[email protected]>Signed-off-by Peter Xu <[email protected]>--- fs/userfaultfd.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)fs/userfaultfd.c +4
-3@@ -1977,8 +1977,10 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, ret = -EFAULT ;if (copy_from_user (&uffdio_api, buf,sizeof (uffdio_api))) goto out; -/* Ignore unsupported features (userspace built against newer kernel) */ - features = uffdio_api.features &UFFD_API_FEATURES ; + features = uffdio_api.features; + ret = -EINVAL ; +if (uffdio_api.api !=UFFD_API || (features & ~UFFD_API_FEATURES )) + goto err_out; ret = -EPERM ;if ((features &UFFD_FEATURE_EVENT_FORK ) && !capable (CAP_SYS_PTRACE )) goto err_out; --2.39.1
Peter Xu
reviewerRe: [PATCH 01/29] Revert "userfaultfd: don't fail on unrecognized features"
Quoted 30 lines · Axel Rasmussen
Quoted 10 lines · Peter Xu
This is a proposal to revert commit
914eedcb9ba0ff53c33808.I found this when writting a simple
UFFDIO_APItest to be the first unit test in this set. Two things breaks with the commit:-
UFFDIO_APIcheck was lost and missing. According to man page, the kernel should rejectioctl(UFFDIO_API)if uffdio_api.api !=0xaa. This check is needed if the api version will be extended in the future, or user app won't be able to identify which is a new kernel.100% agreed, this was a mistake.
Quoted 5 lines
- Feature flags checks were removed, which means
UFFDIO_APIwith a feature that does not exist will also succeed. According to the man page, we should (and it makes sense) to rejectioctl(UFFDIO_API)if unknown features passed in.I still strongly disagree with reverting this part, my feeling is still that doing so makes things more complicated for no reason.
Re: David's point, it's clearly wrong to change semantics so a thing that used to work now fails. But this instead makes it more permissive
- existing userspace programs continue to work as-is, but *also* one can achieve the same thing more simply (combine probing + configuration into one step). I don't see any problem with that, generally.
Quoted 3 lines
But, if David and others don't find my argument convincing, it isn't the end of the world. It just means I have to go update my userspace code to be a bit more complicated. :)
I'd say it's fine if it's your own program that you can in full control easily. :) Sorry again for not noticing that earlier.
There's one reason that we may consider keeping the behavior. IMHO it is when there're major softwares that uses the "wrong" ABI (let's say so; because it's not following the man pages). If you're aware any such major softwares (especially open sourced) will break due to this revert patch, please shoot.
Quoted 3 lines
I also still think the man page is incorrect or at least incomplete no matter what we do here; we should be sure to update it as a follow-up.
So far it looks still fine if with this revert. Maybe I overlooked somewhere?
I'll add this into my todo, but with low priority. If you have suggestion already on how to improve the man page please do so before me!
Signature · 1 line
Peter Xu
Re: [PATCH 01/29] Revert "userfaultfd: don't fail on unrecognized features"
Quoted 36 lines · Peter Xu
Quoted 30 lines · Axel Rasmussen
Quoted 10 lines · Peter Xu
This is a proposal to revert commit
914eedcb9ba0ff53c33808.I found this when writting a simple
UFFDIO_APItest to be the first unit test in this set. Two things breaks with the commit:-
UFFDIO_APIcheck was lost and missing. According to man page, the kernel should rejectioctl(UFFDIO_API)if uffdio_api.api !=0xaa. This check is needed if the api version will be extended in the future, or user app won't be able to identify which is a new kernel.100% agreed, this was a mistake.
Quoted 5 lines
- Feature flags checks were removed, which means
UFFDIO_APIwith a feature that does not exist will also succeed. According to the man page, we should (and it makes sense) to rejectioctl(UFFDIO_API)if unknown features passed in.I still strongly disagree with reverting this part, my feeling is still that doing so makes things more complicated for no reason.
Re: David's point, it's clearly wrong to change semantics so a thing that used to work now fails. But this instead makes it more permissive
- existing userspace programs continue to work as-is, but *also* one can achieve the same thing more simply (combine probing + configuration into one step). I don't see any problem with that, generally.Quoted 3 lines
But, if David and others don't find my argument convincing, it isn't the end of the world. It just means I have to go update my userspace code to be a bit more complicated. :)
Quoted 8 lines
I'd say it's fine if it's your own program that you can in full control easily. :) Sorry again for not noticing that earlier.
There's one reason that we may consider keeping the behavior. IMHO it is when there're major softwares that uses the "wrong" ABI (let's say so; because it's not following the man pages). If you're aware any such major softwares (especially open sourced) will break due to this revert patch, please shoot.
Well, I did find one example, criu:
https://github.com/checkpoint-restore/criu/blob/criu-dev/criu/uffd.c#L266
It doesn't do the two-step probing process, it looks to me like it does what my patch was intending users to do:
It just asks for the requested features up-front, without any probing. And then it returns the subset of features it *actually* got, ostensibly so the caller can compare that vs. what it requested.
Then again, it looks like the caller doesn't *actually* compare the features it got vs. what it asked for. I don't know enough about criu to know if this is a bug, or if they actually just don't care.
https://github.com/checkpoint-restore/criu/blob/criu-dev/criu/uffd.c#L312
Quoted 10 lines
Quoted 4 lines
I also still think the man page is incorrect or at least incomplete no matter what we do here; we should be sure to update it as a follow-up.
So far it looks still fine if with this revert. Maybe I overlooked somewhere?
I'll add this into my todo, but with low priority. If you have suggestion already on how to improve the man page please do so before me!
My thinking is that it describes the bits and pieces, but doesn't explicitly describe end-to-end how to configure a userfaultfd using the two-step probing approach. (Or state that this is actually *required*, unless you only want to set features=0 in any case.)
Maybe it will be easiest if I just send a patch myself with what I'm thinking, and we can see what folks think. Always easier to just look at a patch vs. talking about it in the abstract. :)
Quoted 4 lines
--
Peter Xu
Re: [PATCH 01/29] Revert "userfaultfd: don't fail on unrecognized features"
Quoted 16 lines · Axel Rasmussen
Quoted 14 lines · Peter Xu
Quoted 12 lines · Axel Rasmussen
Quoted 11 lines · Peter Xu
This is a proposal to revert commit
914eedcb9ba0ff53c33808.I found this when writting a simple
UFFDIO_APItest to be the first unit test in this set. Two things breaks with the commit:-
UFFDIO_APIcheck was lost and missing. According to man page, the kernel should rejectioctl(UFFDIO_API)if uffdio_api.api !=0xaa. This check is needed if the api version will be extended in the future, or user app won't be able to identify which is a new kernel.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
- Feature flags checks were removed, which means
UFFDIO_APIwith a feature that does not exist will also succeed. According to the man page, we should (and it makes sense) to rejectioctl(UFFDIO_API)if unknown features passed in.
If features/flags are not checked in kernel, and the kernel doesn't return an error on
an unknown flag/error, that makes the syscall non-extendable, meaning that adding
any new feature may break existing software, which doesn't sanitize them properly.
https://lwn.net/Articles/588444/
See a bunch of painful exercises from syscalls with numbers in the end:
https://lwn.net/Articles/792628/
To adding an additional setsockopt() because an old one didn't have sanity checks for flags:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8917a777be3b
(not the best example, as the new setsockopt() didn't check flags for sanity as well (sic!),
but that's near the code I work on now)
This is even documented nowadays:
https://www.kernel.org/doc/html/latest/process/adding-syscalls.html#designing-the-api-planning-for-extension
...and everyone knows what happens when you blame userspace for breaking by not doing what you would have expected it to do:
https://lkml.org/lkml/2012/12/23/75
[..]
Quoted 8 lines
Quoted 5 lines
There's one reason that we may consider keeping the behavior. IMHO it is when there're major softwares that uses the "wrong" ABI (let's say so; because it's not following the man pages). If you're aware any such major softwares (especially open sourced) will break due to this revert patch, please shoot.
Well, I did find one example, criu:
https://github.com/checkpoint-restore/criu/blob/criu-dev/criu/uffd.c#L266
Mike can speak better than me about uffd, but AFAICS, CRIU correctly detects features with kerneldat/kdat:
https://github.com/checkpoint-restore/criu/blob/criu-dev/criu/kerndat.c#L1235
So, doing a sane thing in kernel shouldn't break CRIU (at least here).
Thanks,
Dmitry
Re: [PATCH 01/29] Revert "userfaultfd: don't fail on unrecognized features"
Quoted 23 lines · Dmitry Safonov
Quoted 16 lines · Axel Rasmussen
Quoted 14 lines · Peter Xu
Quoted 12 lines · Axel Rasmussen
Quoted 11 lines · Peter Xu
This is a proposal to revert commit
914eedcb9ba0ff53c33808.I found this when writting a simple
UFFDIO_APItest to be the first unit test in this set. Two things breaks with the commit:-
UFFDIO_APIcheck was lost and missing. According to man page, the kernel should rejectioctl(UFFDIO_API)if uffdio_api.api !=0xaa. This check is needed if the api version will be extended in the future, or user app won't be able to identify which is a new kernel.Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
- Feature flags checks were removed, which means
UFFDIO_APIwith a feature that does not exist will also succeed. According to the man page, we should (and it makes sense) to rejectioctl(UFFDIO_API)if unknown features passed in.
Quoted 7 lines
If features/flags are not checked in kernel, and the kernel doesn't return an error on
an unknown flag/error, that makes the syscall non-extendable, meaning that adding
any new feature may break existing software, which doesn't sanitize them properly.
https://lwn.net/Articles/588444/
I don't think the same problem applies here. In the case of syscalls, the problem is the only way the kernel can communicate is by the EINVAL return value. Without the check, if a call succeeds the caller can't tell: was the flag supported + applied, or unrecognized + ignored?
With UFFDIO_API (we aren't talking about userfaultfd(2) itself), when you pass in a set of flags, we return the subset of flags which were enabled, in addition to the return code. So via that mechanism, one is "able to check whether it is running on a kernel where [userfaultfd] supports [the feature]" as the article describes - the only difference is, the caller must check the returned set of features, instead of checking for an error code. I don't think it's exactly *how* userspace can check that's important, but rather *that* it can check.
Another important difference: I have a hard time imagining a case where adding a new feature could break userspace, even with my approach, but let's say for the sake of argument one arises in the future. Unlike normal syscalls, we have the UFFD_API version check, so we have the option of incrementing that to separate users relying on the old behavior, from users willing to deal with the new behavior.
(Syscalls can kind of replicate this by adding a new syscall, like clone() vs clone2(), but I think that's messier than the API version check being built-in to the API.)
Quoted 10 lines
See a bunch of painful exercises from syscalls with numbers in the end:
https://lwn.net/Articles/792628/
To adding an additionalsetsockopt()because an old one didn't have sanity checks for flags:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8917a777be3b
(not the best example, as the newsetsockopt()didn't check flags for sanity as well (sic!),
but that's near the code I work on now)
Quoted 6 lines
This is even documented nowadays:
https://www.kernel.org/doc/html/latest/process/adding-syscalls.html#designing-the-api-planning-for-extension...and everyone knows what happens when you blame userspace for breaking by not doing what you would have expected it to do:
https://lkml.org/lkml/2012/12/23/75
100% agreed. :)
Quoted 8 lines
[..]
Quoted 6 lines
Quoted 5 lines
There's one reason that we may consider keeping the behavior. IMHO it is when there're major softwares that uses the "wrong" ABI (let's say so; because it's not following the man pages). If you're aware any such major softwares (especially open sourced) will break due to this revert patch, please shoot.
Quoted 6 lines
Quoted 2 lines
Well, I did find one example, criu:
https://github.com/checkpoint-restore/criu/blob/criu-dev/criu/uffd.c#L266Mike can speak better than me about uffd, but AFAICS, CRIU correctly detects features with kerneldat/kdat:
https://github.com/checkpoint-restore/criu/blob/criu-dev/criu/kerndat.c#L1235
Ah, right, this is the simplest case where no optional features are asked for. So, it's not a great example; this particular case would look the same regardless of what the kernel does.
Quoted 5 lines
So, doing a sane thing in kernel shouldn't break CRIU (at least here).
Thanks,
Dmitry
Re: [PATCH 01/29] Revert "userfaultfd: don't fail on unrecognized features"
In reply to Axel RasmussenQuoted 30 lines · Axel Rasmussen
Quoted 10 lines · Peter Xu
This is a proposal to revert commit
914eedcb9ba0ff53c33808.I found this when writting a simple
UFFDIO_APItest to be the first unit test in this set. Two things breaks with the commit:-
UFFDIO_APIcheck was lost and missing. According to man page, the kernel should rejectioctl(UFFDIO_API)if uffdio_api.api !=0xaa. This check is needed if the api version will be extended in the future, or user app won't be able to identify which is a new kernel.100% agreed, this was a mistake.
Quoted 5 lines
- Feature flags checks were removed, which means
UFFDIO_APIwith a feature that does not exist will also succeed. According to the man page, we should (and it makes sense) to rejectioctl(UFFDIO_API)if unknown features passed in.I still strongly disagree with reverting this part, my feeling is still that doing so makes things more complicated for no reason.
Re: David's point, it's clearly wrong to change semantics so a thing that used to work now fails. But this instead makes it more permissive
- existing userspace programs continue to work as-is, but *also* one can achieve the same thing more simply (combine probing + configuration into one step). I don't see any problem with that, generally.
Quoted 3 lines
But, if David and others don't find my argument convincing, it isn't the end of the world. It just means I have to go update my userspace code to be a bit more complicated. :)
I'd probably find it more convincing if we'd started out with that approach ;) . User space would have to deal with the behavior of old kernels either way already? IOW, old kernels would reject the new flags, new kernels would not reject them but mask them out. So changing that behavior after the effects is somewhat suboptimal IMHO ... and rather makes things more complicated.
Signature · 3 lines
Thanks, David / dhildenb
Peter Xu
reviewerPATCH 2/29 selftests/mm: Update .gitignore with two missing tests
In reply to Peter Xu--- tools/testing/selftests/mm/.gitignore | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/selftests/mm/.gitignore +2 -0
@@ -36,3 +36,5 @@ split_huge_page_test ksm_tests local_config.h local_config.mk +ksm_functional_tests +mdwe_test
Signature · 1 line
2.39.1
Re: [PATCH 02/29] selftests/mm: Update .gitignore with two missing tests
Quoted 10 lines · Peter Xu
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/.gitignore | 2 ++ 1 file changed, 2 insertions(+)tools/testing/selftests/mm/.gitignore +0
-0
tools/testing/selftests/mm/.gitignore split_huge_page_test · line 40
Quoted 5 lines
tools/testing/selftests/mm/.gitignore +2
-0@@ -36,3 +36,5 @@ split_huge_page_test ksm_tests local_config.h local_config.mk +ksm_functional_tests +mdwe_test
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 02/29] selftests/mm: Update .gitignore with two missing tests
In reply to Peter XuQuoted 1 line · Peter Xu
Signed-off-by Peter Xu <[email protected]>
Quoted 17 lines
--- tools/testing/selftests/mm/.gitignore | 2 ++ 1 file changed, 2 insertions(+)tools/testing/selftests/mm/.gitignore +2
-0@@ -36,3 +36,5 @@ split_huge_page_test ksm_tests local_config.h local_config.mk +ksm_functional_tests +mdwe_testSignature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 3/29 selftests/mm: Dump a summary in run_vmtests.sh
In reply to Peter XuDump a summary after running whatever test specified. Useful for human runners to identify any kind of failures (besides exit code).
--- tools/testing/selftests/mm/run_vmtests.sh | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/testing/selftests/mm/run_vmtests.sh +8 -0
@@ -5,6 +5,9 @@ # Kselftest framework requirement - SKIP code is 4. ksft_skip=4 +count_pass=0 +count_fail=0 +count_skip=0 exitcode=0 usage() {
@@ -149,11 +152,14 @@ run_test() { "$@" local ret=$? if [ $ret -eq 0 ]; then + count_pass=$(( $count_pass + 1 )) echo "[PASS]" elif [ $ret -eq $ksft_skip ]; then + count_skip=$(( $count_skip + 1 )) echo "[SKIP]" exitcode=$ksft_skip else + count_fail=$(( $count_fail + 1 )) echo "[FAIL]" exitcode=1 fi
@@ -279,4 +285,6 @@ CATEGORY="soft_dirty" run_test ./soft-dirty # COW tests CATEGORY="cow" run_test ./cow +echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" + exit $exitcode
Signature · 1 line
2.39.1
Re: [PATCH 03/29] selftests/mm: Dump a summary in run_vmtests.sh
Quoted 24 lines · Peter Xu
Dump a summary after running whatever test specified. Useful for human runners to identify any kind of failures (besides exit code).
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/run_vmtests.sh | 8 ++++++++ 1 file changed, 8 insertions(+)tools/testing/selftests/mm/run_vmtests.sh +3
-0@@ -5,6 +5,9 @@ # Kselftest framework requirement - SKIP code is 4. ksft_skip=4 +count_pass=0 +count_fail=0 +count_skip=0 exitcode=0 usage() {
tools/testing/selftests/mm/run_vmtests.sh run_test() { · line 155
Quoted 4 lines
tools/testing/selftests/mm/run_vmtests.sh +1
-0@@ -149,11 +152,14 @@ run_test() { "$@" local ret=$? if [ $ret -eq 0 ]; then + count_pass=$(( $count_pass + 1 ))
Actually, inside $(( )) there's no need to prefix variable names with
$ too. Running "shellcheck" over the script written this way will generate a warning.
Same applies below.
Quoted 18 lines
tools/testing/selftests/mm/run_vmtests.sh +4
-1echo "[PASS]" elif [ $ret -eq $ksft_skip ]; then + count_skip=$(( $count_skip + 1 )) echo "[SKIP]" exitcode=$ksft_skip else + count_fail=$(( $count_fail + 1 )) echo "[FAIL]" exitcode=1 fi@@ -279,4 +285,6 @@ CATEGORY="soft_dirty" run_test ./soft-dirty # COW tests CATEGORY="cow" run_test ./cow +echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" + exit $exitcode --
Quoted 2 lines
2.39.1
Besides the nitpick:
Peter Xu
reviewerRe: [PATCH 03/29] selftests/mm: Dump a summary in run_vmtests.sh
Quoted 31 lines · Axel Rasmussen
Quoted 24 lines · Peter Xu
Dump a summary after running whatever test specified. Useful for human runners to identify any kind of failures (besides exit code).
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/run_vmtests.sh | 8 ++++++++ 1 file changed, 8 insertions(+)tools/testing/selftests/mm/run_vmtests.sh +3
-0@@ -5,6 +5,9 @@ # Kselftest framework requirement - SKIP code is 4. ksft_skip=4 +count_pass=0 +count_fail=0 +count_skip=0 exitcode=0 usage() {tools/testing/selftests/mm/run_vmtests.sh run_test() { · line 155
Quoted 4 lines
tools/testing/selftests/mm/run_vmtests.sh +1
-0@@ -149,11 +152,14 @@ run_test() { "$@" local ret=$? if [ $ret -eq 0 ]; then + count_pass=$(( $count_pass + 1 ))Actually, inside $(( )) there's no need to prefix variable names with
Quoted 4 lines
$ too. Running "shellcheck" over the script written this way will generate a warning.Same applies below.
Sure, I'll fix.
Quoted 24 lines
Quoted 19 lines
tools/testing/selftests/mm/run_vmtests.sh +4
-1echo "[PASS]" elif [ $ret -eq $ksft_skip ]; then + count_skip=$(( $count_skip + 1 )) echo "[SKIP]" exitcode=$ksft_skip else + count_fail=$(( $count_fail + 1 )) echo "[FAIL]" exitcode=1 fi@@ -279,4 +285,6 @@ CATEGORY="soft_dirty" run_test ./soft-dirty # COW tests CATEGORY="cow" run_test ./cow +echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" + exit $exitcode --Quoted 2 lines
2.39.1
Besides the nitpick:
Quoted 1 line
Reviewed-by Axel Rasmussen <[email protected]>
Thanks!
Signature · 1 line
Peter Xu
David Hildenbrand
Reviewed-byRe: [PATCH 03/29] selftests/mm: Dump a summary in run_vmtests.sh
Quoted 35 lines · Peter Xu
Quoted 34 lines · Axel Rasmussen
Quoted 24 lines · Peter Xu
Dump a summary after running whatever test specified. Useful for human runners to identify any kind of failures (besides exit code).
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/run_vmtests.sh | 8 ++++++++ 1 file changed, 8 insertions(+)tools/testing/selftests/mm/run_vmtests.sh +3
-0@@ -5,6 +5,9 @@ # Kselftest framework requirement - SKIP code is 4. ksft_skip=4 +count_pass=0 +count_fail=0 +count_skip=0 exitcode=0 usage() {tools/testing/selftests/mm/run_vmtests.sh run_test() { · line 155
Quoted 4 lines
tools/testing/selftests/mm/run_vmtests.sh +1
-0@@ -149,11 +152,14 @@ run_test() { "$@" local ret=$? if [ $ret -eq 0 ]; then + count_pass=$(( $count_pass + 1 ))Actually, inside $(( )) there's no need to prefix variable names with
$ too. Running "shellcheck" over the script written this way will generate a warning.
Quoted 3 lines
Quoted 1 line
Same applies below.
Sure, I'll fix.
With that
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 03/29] selftests/mm: Dump a summary in run_vmtests.sh
In reply to Peter XuQuoted 4 lines · Peter Xu
Dump a summary after running whatever test specified. Useful for human runners to identify any kind of failures (besides exit code).
Signed-off-by Peter Xu <[email protected]>
Quoted 43 lines
--- tools/testing/selftests/mm/run_vmtests.sh | 8 ++++++++ 1 file changed, 8 insertions(+)tools/testing/selftests/mm/run_vmtests.sh +8
-0@@ -5,6 +5,9 @@ # Kselftest framework requirement - SKIP code is 4. ksft_skip=4 +count_pass=0 +count_fail=0 +count_skip=0 exitcode=0 usage() {@@ -149,11 +152,14 @@ run_test() { "$@" local ret=$? if [ $ret -eq 0 ]; then + count_pass=$(( $count_pass + 1 )) echo "[PASS]" elif [ $ret -eq $ksft_skip ]; then + count_skip=$(( $count_skip + 1 )) echo "[SKIP]" exitcode=$ksft_skip else + count_fail=$(( $count_fail + 1 )) echo "[FAIL]" exitcode=1 fi@@ -279,4 +285,6 @@ CATEGORY="soft_dirty" run_test ./soft-dirty # COW tests CATEGORY="cow" run_test ./cow +echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" + exit $exitcodeSignature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 4/29 selftests/mm: Merge util.h into vm_util.h
In reply to Peter XuThere're two util headers under mm/ kselftest. Merge one with another. It turns out util.h is the easy one to move.
When merging, drop PAGE_SIZE / PAGE_SHIFT because they're unnecessary wrappers to page_size() / page_shift(), meanwhile rename them to psize() and pshift() so as to not conflict with some existing definitions in some test files that includes vm_util.h.
--- tools/testing/selftests/mm/Makefile | 4 ++ tools/testing/selftests/mm/gup_test.c | 5 +- tools/testing/selftests/mm/ksm_tests.c | 2 +- tools/testing/selftests/mm/mrelease_test.c | 11 ++- tools/testing/selftests/mm/transhuge-stress.c | 12 ++-- tools/testing/selftests/mm/util.h | 69 ------------------- tools/testing/selftests/mm/vm_util.c | 31 +++++++++ tools/testing/selftests/mm/vm_util.h | 31 +++++++++ 8 files changed, 80 insertions(+), 85 deletions(-) delete mode 100644 tools/testing/selftests/mm/util.h
tools/testing/selftests/mm/Makefile +4 -0
@@ -111,6 +111,10 @@ $(OUTPUT)/madv_populate: vm_util.c $(OUTPUT)/soft-dirty: vm_util.c $(OUTPUT)/split_huge_page_test: vm_util.c $(OUTPUT)/userfaultfd: vm_util.c +$(OUTPUT)/gup_test: vm_util.c +$(OUTPUT)/mrelease_test: vm_util.c +$(OUTPUT)/transhuge-stress: vm_util.c +$(OUTPUT)/ksm_tests: vm_util.c ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
tools/testing/selftests/mm/gup_test.c +2 -3
@@ -12,8 +12,7 @@#include <assert.h> #include <mm/gup_test.h> #include "../kselftest.h" - -#include "util.h" +#include "vm_util.h" #define MB (1UL << 20)
@@ -251,7 +250,7 @@ int main(int argc, char **argv)if (touch) { gup.gup_flags |=FOLL_TOUCH ; }else { -for (; (unsigned long )p < gup.addr + size; p +=PAGE_SIZE ) +for (; (unsigned long )p < gup.addr + size; p +=psize ()) p[0 ] =0 ; }
tools/testing/selftests/mm/ksm_tests.c +1 -1
@@ -14,7 +14,7 @@#include "../kselftest.h" #include <include/vdso/time64.h> -#include "util.h" +#include "vm_util.h" #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"#define KSM_FP (s) (KSM_SYSFS_PATH s)
tools/testing/selftests/mm/mrelease_test.c +5 -6
@@ -9,8 +9,7 @@#include <stdlib.h> #include <sys/wait.h> #include <unistd.h> - -#include "util.h" +#include "vm_util.h" #include "../kselftest.h"
@@ -32,7 +31,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd)unsigned long i;char *buf; - buf = (char *)mmap (NULL , nr_pages *PAGE_SIZE ,PROT_READ |PROT_WRITE , + buf = (char *)mmap (NULL , nr_pages *psize (),PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANON ,0 ,0 );if (buf ==MAP_FAILED ) {perror ("mmap failed, halting the test" );
@@ -40,7 +39,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd) }for (i =0 ; i < nr_pages; i++) - *((unsigned long *)(buf + (i *PAGE_SIZE ))) = i; + *((unsigned long *)(buf + (i *psize ()))) = i;/* Signal the parent that the child is ready */ if (write (pipefd,"" ,1 ) <0 ) {
@@ -54,7 +53,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd) timeout--; } -munmap (buf, nr_pages *PAGE_SIZE ); +munmap (buf, nr_pages *psize ());return (timeout >0 ) ?KSFT_PASS :KSFT_FAIL ; }
@@ -87,7 +86,7 @@ static int child_main(int pipefd[], size_t size)/* Allocate and fault-in memory and wait to be killed */ close (pipefd[0 ]); - res =alloc_noexit (MB (size) /PAGE_SIZE , pipefd[1 ]); + res =alloc_noexit (MB (size) /psize (), pipefd[1 ]);close (pipefd[1 ]);return res; }
tools/testing/selftests/mm/transhuge-stress.c +6 -6
@@ -15,7 +15,7 @@#include <fcntl.h> #include <string.h> #include <sys/mman.h> -#include "util.h" +#include "vm_util.h" int backing_fd =-1 ;int mmap_flags =MAP_ANONYMOUS |MAP_NORESERVE |MAP_PRIVATE ;
@@ -34,10 +34,10 @@ int main(int argc, char **argv)int pagemap_fd; ram =sysconf (_SC_PHYS_PAGES); -if (ram >SIZE_MAX /sysconf (_SC_PAGESIZE) /4 ) +if (ram >SIZE_MAX /psize () /4 ) ram =SIZE_MAX /4 ;else - ram *=sysconf (_SC_PAGESIZE); + ram *=psize (); len = ram;while (++i < argc) {
@@ -58,7 +58,7 @@ int main(int argc, char **argv)warnx ("allocate %zd transhuge pages, using %zd MiB virtual memory" " and %zd MiB of ram" , len >>HPAGE_SHIFT , len >>20 , - ram >> (20 +HPAGE_SHIFT -PAGE_SHIFT -1 )); + ram >> (20 +HPAGE_SHIFT -pshift () -1 )); pagemap_fd =open ("/proc/self/pagemap" ,O_RDONLY );if (pagemap_fd <0 )
@@ -92,7 +92,7 @@ int main(int argc, char **argv)if (pfn <0 ) { nr_failed++; }else { -size_t idx = pfn >> (HPAGE_SHIFT -PAGE_SHIFT ); +size_t idx = pfn >> (HPAGE_SHIFT -pshift ()); nr_succeed++;if (idx >= map_len) {
@@ -108,7 +108,7 @@ int main(int argc, char **argv) }/* split transhuge page, keep last page */ -if (madvise (p,HPAGE_SIZE -PAGE_SIZE ,MADV_DONTNEED )) +if (madvise (p,HPAGE_SIZE -psize (),MADV_DONTNEED ))err (2 ,"MADV_DONTNEED" ); }clock_gettime (CLOCK_MONOTONIC , &b);
tools/testing/selftests/mm/util.h deleted +0 -69
69 lines not on this page · raw message
tools/testing/selftests/mm/vm_util.c +31 -0
@@ -8,6 +8,9 @@#define SMAP_FILE_PATH "/proc/self/smaps"#define MAX_LINE_LENGTH 500 +unsigned int __page_size; +unsigned int __page_shift; +uint64_t pagemap_get_entry (int fd,char *start) {const unsigned long pfn = (unsigned long )start /getpagesize ();
@@ -149,3 +152,31 @@ bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size) {return __check_huge (addr,"ShmemPmdMapped:" , nr_hpages, hpage_size); } + +int64_t allocate_transhuge (void *ptr,int pagemap_fd) +{ +uint64_t ent[2 ]; + +/* drop pmd */ +if (mmap (ptr,HPAGE_SIZE ,PROT_READ |PROT_WRITE , +MAP_FIXED |MAP_ANONYMOUS | +MAP_NORESERVE |MAP_PRIVATE ,-1 ,0 ) != ptr) +errx (2 ,"mmap transhuge" ); + +if (madvise (ptr,HPAGE_SIZE ,MADV_HUGEPAGE )) +err (2 ,"MADV_HUGEPAGE" ); + +/* allocate transparent huge page */ + *(volatile void **)ptr = ptr; + +if (pread (pagemap_fd, ent,sizeof (ent), + (uintptr_t )ptr >> (pshift () -3 )) !=sizeof (ent)) +err (2 ,"read pagemap" ); + +if (PAGEMAP_PRESENT (ent[0 ]) &&PAGEMAP_PRESENT (ent[1 ]) && +PAGEMAP_PFN (ent[0 ]) +1 ==PAGEMAP_PFN (ent[1 ]) && + !(PAGEMAP_PFN (ent[0 ]) & ((1 << (HPAGE_SHIFT -pshift ())) -1 ))) +return PAGEMAP_PFN (ent[0 ]); + +return -1 ; +}
tools/testing/selftests/mm/vm_util.h +31 -0
@@ -1,6 +1,27 @@/* SPDX-License-Identifier: GPL-2.0 */ #include <stdint.h> #include <stdbool.h> +#include <sys/mman.h> +#include <err.h> +#include <string.h> /* ffsl() */ +#include <unistd.h> /* _SC_PAGESIZE */ + +extern unsigned int __page_size; +extern unsigned int __page_shift; + +static inline unsigned int psize (void ) +{ +if (!__page_size) + __page_size =sysconf (_SC_PAGESIZE); +return __page_size; +} + +static inline unsigned int pshift (void ) +{ +if (!__page_shift) + __page_shift = (ffsl (psize ()) -1 ); +return __page_shift; +}uint64_t pagemap_get_entry (int fd,char *start);bool pagemap_is_softdirty (int fd,char *start);
@@ -13,3 +34,13 @@ uint64_t read_pmd_pagesize(void);bool check_huge_anon (void *addr,int nr_hpages,uint64_t hpage_size);bool check_huge_file (void *addr,int nr_hpages,uint64_t hpage_size);bool check_huge_shmem (void *addr,int nr_hpages,uint64_t hpage_size); +int64_t allocate_transhuge (void *ptr,int pagemap_fd); + +/* +* On ppc64 this will only work with radix 2M hugepage size +*/ +#define HPAGE_SHIFT 21 +#define HPAGE_SIZE (1 << HPAGE_SHIFT) + +#define PAGEMAP_PRESENT (ent) (((ent) & (1ull << 63)) != 0) +#define PAGEMAP_PFN (ent) ((ent) & ((1ull << 55) - 1))
Signature · 1 line
2.39.1
Re: [PATCH 04/29] selftests/mm: Merge util.h into vm_util.h
Quoted 10 lines · Peter Xu
There're two util headers under
mm/kselftest. Merge one with another. It turns oututil.his the easy one to move.When merging, drop
PAGE_SIZE/PAGE_SHIFTbecause they're unnecessary wrappers topage_size()/page_shift(), meanwhile rename them topsize()andpshift()so as to not conflict with some existing definitions in some test files that includesvm_util.h.Signed-off-by Peter Xu <[email protected]>
Quoted 336 lines
--- tools/testing/selftests/mm/Makefile | 4 ++ tools/testing/selftests/mm/gup_test.c | 5 +- tools/testing/selftests/mm/ksm_tests.c | 2 +- tools/testing/selftests/mm/mrelease_test.c | 11 ++- tools/testing/selftests/mm/transhuge-stress.c | 12 ++-- tools/testing/selftests/mm/util.h | 69 ------------------- tools/testing/selftests/mm/vm_util.c | 31 +++++++++ tools/testing/selftests/mm/vm_util.h | 31 +++++++++ 8 files changed, 80 insertions(+), 85 deletions(-) delete mode 100644 tools/testing/selftests/mm/util.htools/testing/selftests/mm/Makefile +4
-0@@ -111,6 +111,10 @@ $(OUTPUT)/madv_populate: vm_util.c $(OUTPUT)/soft-dirty: vm_util.c $(OUTPUT)/split_huge_page_test: vm_util.c $(OUTPUT)/userfaultfd: vm_util.c +$(OUTPUT)/gup_test: vm_util.c +$(OUTPUT)/mrelease_test: vm_util.c +$(OUTPUT)/transhuge-stress: vm_util.c +$(OUTPUT)/ksm_tests: vm_util.c ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))tools/testing/selftests/mm/gup_test.c +2
-3@@ -12,8 +12,7 @@#include <assert.h> #include <mm/gup_test.h> #include "../kselftest.h" - -#include "util.h" +#include "vm_util.h" #define MB (1UL << 20)@@ -251,7 +250,7 @@ int main(int argc, char **argv)if (touch) { gup.gup_flags |=FOLL_TOUCH ; }else { -for (; (unsigned long )p < gup.addr + size; p +=PAGE_SIZE ) +for (; (unsigned long )p < gup.addr + size; p +=psize ()) p[0 ] =0 ; }tools/testing/selftests/mm/ksm_tests.c +1
-1@@ -14,7 +14,7 @@#include "../kselftest.h" #include <include/vdso/time64.h> -#include "util.h" +#include "vm_util.h" #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"#define KSM_FP (s) (KSM_SYSFS_PATH s)tools/testing/selftests/mm/mrelease_test.c +5
-6@@ -9,8 +9,7 @@#include <stdlib.h> #include <sys/wait.h> #include <unistd.h> - -#include "util.h" +#include "vm_util.h" #include "../kselftest.h" @@ -32,7 +31,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd)unsigned long i;char *buf; - buf = (char *)mmap (NULL , nr_pages *PAGE_SIZE ,PROT_READ |PROT_WRITE , + buf = (char *)mmap (NULL , nr_pages *psize (),PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANON ,0 ,0 );if (buf ==MAP_FAILED ) {perror ("mmap failed, halting the test" );@@ -40,7 +39,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd) }for (i =0 ; i < nr_pages; i++) - *((unsigned long *)(buf + (i *PAGE_SIZE ))) = i; + *((unsigned long *)(buf + (i *psize ()))) = i;/* Signal the parent that the child is ready */ if (write (pipefd,"" ,1 ) <0 ) {@@ -54,7 +53,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd) timeout--; } -munmap (buf, nr_pages *PAGE_SIZE ); +munmap (buf, nr_pages *psize ());return (timeout >0 ) ?KSFT_PASS :KSFT_FAIL ; }@@ -87,7 +86,7 @@ static int child_main(int pipefd[], size_t size)/* Allocate and fault-in memory and wait to be killed */ close (pipefd[0 ]); - res =alloc_noexit (MB (size) /PAGE_SIZE , pipefd[1 ]); + res =alloc_noexit (MB (size) /psize (), pipefd[1 ]);close (pipefd[1 ]);return res; }tools/testing/selftests/mm/transhuge-stress.c +6
-6@@ -15,7 +15,7 @@#include <fcntl.h> #include <string.h> #include <sys/mman.h> -#include "util.h" +#include "vm_util.h" int backing_fd =-1 ;int mmap_flags =MAP_ANONYMOUS |MAP_NORESERVE |MAP_PRIVATE ;@@ -34,10 +34,10 @@ int main(int argc, char **argv)int pagemap_fd; ram =sysconf (_SC_PHYS_PAGES); -if (ram >SIZE_MAX /sysconf (_SC_PAGESIZE) /4 ) +if (ram >SIZE_MAX /psize () /4 ) ram =SIZE_MAX /4 ;else - ram *=sysconf (_SC_PAGESIZE); + ram *=psize (); len = ram;while (++i < argc) {@@ -58,7 +58,7 @@ int main(int argc, char **argv)warnx ("allocate %zd transhuge pages, using %zd MiB virtual memory" " and %zd MiB of ram" , len >>HPAGE_SHIFT , len >>20 , - ram >> (20 +HPAGE_SHIFT -PAGE_SHIFT -1 )); + ram >> (20 +HPAGE_SHIFT -pshift () -1 )); pagemap_fd =open ("/proc/self/pagemap" ,O_RDONLY );if (pagemap_fd <0 )@@ -92,7 +92,7 @@ int main(int argc, char **argv)if (pfn <0 ) { nr_failed++; }else { -size_t idx = pfn >> (HPAGE_SHIFT -PAGE_SHIFT ); +size_t idx = pfn >> (HPAGE_SHIFT -pshift ()); nr_succeed++;if (idx >= map_len) {@@ -108,7 +108,7 @@ int main(int argc, char **argv) }/* split transhuge page, keep last page */ -if (madvise (p,HPAGE_SIZE -PAGE_SIZE ,MADV_DONTNEED )) +if (madvise (p,HPAGE_SIZE -psize (),MADV_DONTNEED ))err (2 ,"MADV_DONTNEED" ); }clock_gettime (CLOCK_MONOTONIC , &b);tools/testing/selftests/mm/util.h deleted +0
-69@@ -1,69 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ - -#ifndef __KSELFTEST_VM_UTIL_H -#define __KSELFTEST_VM_UTIL_H - -#include <stdint.h> -#include <sys/mman.h> -#include <err.h> -#include <string.h> /* ffsl() */ -#include <unistd.h> /* _SC_PAGESIZE */ - -static unsigned int __page_size; -static unsigned int __page_shift; - -static inline unsigned int page_size (void ) -{ -if (!__page_size) - __page_size =sysconf (_SC_PAGESIZE); -return __page_size; -} - -static inline unsigned int page_shift (void ) -{ -if (!__page_shift) - __page_shift = (ffsl (page_size ()) -1 ); -return __page_shift; -} - -#define PAGE_SHIFT (page_shift()) -#define PAGE_SIZE (page_size()) -/* -* On ppc64 this will only work with radix 2M hugepage size -*/ -#define HPAGE_SHIFT 21 -#define HPAGE_SIZE (1 << HPAGE_SHIFT) - -#define PAGEMAP_PRESENT (ent) (((ent) & (1ull << 63)) != 0) -#define PAGEMAP_PFN (ent) ((ent) & ((1ull << 55) - 1)) - - -static inline int64_t allocate_transhuge (void *ptr,int pagemap_fd) -{ -uint64_t ent[2 ]; - -/* drop pmd */ -if (mmap (ptr,HPAGE_SIZE ,PROT_READ |PROT_WRITE , -MAP_FIXED |MAP_ANONYMOUS | -MAP_NORESERVE |MAP_PRIVATE ,-1 ,0 ) != ptr) -errx (2 ,"mmap transhuge" ); - -if (madvise (ptr,HPAGE_SIZE ,MADV_HUGEPAGE )) -err (2 ,"MADV_HUGEPAGE" ); - -/* allocate transparent huge page */ - *(volatile void **)ptr = ptr; - -if (pread (pagemap_fd, ent,sizeof (ent), - (uintptr_t )ptr >> (PAGE_SHIFT -3 )) !=sizeof (ent)) -err (2 ,"read pagemap" ); - -if (PAGEMAP_PRESENT (ent[0 ]) &&PAGEMAP_PRESENT (ent[1 ]) && -PAGEMAP_PFN (ent[0 ]) +1 ==PAGEMAP_PFN (ent[1 ]) && - !(PAGEMAP_PFN (ent[0 ]) & ((1 << (HPAGE_SHIFT -PAGE_SHIFT )) -1 ))) -return PAGEMAP_PFN (ent[0 ]); - -return -1 ; -} - -#endif tools/testing/selftests/mm/vm_util.c +31
-0@@ -8,6 +8,9 @@#define SMAP_FILE_PATH "/proc/self/smaps"#define MAX_LINE_LENGTH 500 +unsigned int __page_size; +unsigned int __page_shift; +uint64_t pagemap_get_entry (int fd,char *start) {const unsigned long pfn = (unsigned long )start /getpagesize ();@@ -149,3 +152,31 @@ bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size) {return __check_huge (addr,"ShmemPmdMapped:" , nr_hpages, hpage_size); } + +int64_t allocate_transhuge (void *ptr,int pagemap_fd) +{ +uint64_t ent[2 ]; + +/* drop pmd */ +if (mmap (ptr,HPAGE_SIZE ,PROT_READ |PROT_WRITE , +MAP_FIXED |MAP_ANONYMOUS | +MAP_NORESERVE |MAP_PRIVATE ,-1 ,0 ) != ptr) +errx (2 ,"mmap transhuge" ); + +if (madvise (ptr,HPAGE_SIZE ,MADV_HUGEPAGE )) +err (2 ,"MADV_HUGEPAGE" ); + +/* allocate transparent huge page */ + *(volatile void **)ptr = ptr; + +if (pread (pagemap_fd, ent,sizeof (ent), + (uintptr_t )ptr >> (pshift () -3 )) !=sizeof (ent)) +err (2 ,"read pagemap" ); + +if (PAGEMAP_PRESENT (ent[0 ]) &&PAGEMAP_PRESENT (ent[1 ]) && +PAGEMAP_PFN (ent[0 ]) +1 ==PAGEMAP_PFN (ent[1 ]) && + !(PAGEMAP_PFN (ent[0 ]) & ((1 << (HPAGE_SHIFT -pshift ())) -1 ))) +return PAGEMAP_PFN (ent[0 ]); + +return -1 ; +}tools/testing/selftests/mm/vm_util.h +31
-1@@ -1,6 +1,27 @@/* SPDX-License-Identifier: GPL-2.0 */ #include <stdint.h> #include <stdbool.h> +#include <sys/mman.h> +#include <err.h> +#include <string.h> /* ffsl() */ +#include <unistd.h> /* _SC_PAGESIZE */ + +extern unsigned int __page_size; +extern unsigned int __page_shift; + +static inline unsigned int psize (void ) +{ +if (!__page_size) + __page_size =sysconf (_SC_PAGESIZE); +return __page_size; +} + +static inline unsigned int pshift (void ) +{ +if (!__page_shift) + __page_shift = (ffsl (psize ()) -1 ); +return __page_shift; +}uint64_t pagemap_get_entry (int fd,char *start);bool pagemap_is_softdirty (int fd,char *start);@@ -13,3 +34,13 @@ uint64_t read_pmd_pagesize(void);bool check_huge_anon (void *addr,int nr_hpages,uint64_t hpage_size);bool check_huge_file (void *addr,int nr_hpages,uint64_t hpage_size);bool check_huge_shmem (void *addr,int nr_hpages,uint64_t hpage_size); +int64_t allocate_transhuge (void *ptr,int pagemap_fd); + +/* +* On ppc64 this will only work with radix 2M hugepage size +*/ +#define HPAGE_SHIFT 21 +#define HPAGE_SIZE (1 << HPAGE_SHIFT) + +#define PAGEMAP_PRESENT (ent) (((ent) & (1ull << 63)) != 0) +#define PAGEMAP_PFN (ent) ((ent) & ((1ull << 55) - 1)) --2.39.1
Re: [PATCH 04/29] selftests/mm: Merge util.h into vm_util.h
In reply to Peter XuQuoted 10 lines · Peter Xu
There're two util headers under
mm/kselftest. Merge one with another. It turns oututil.his the easy one to move.When merging, drop
PAGE_SIZE/PAGE_SHIFTbecause they're unnecessary wrappers topage_size()/page_shift(), meanwhile rename them topsize()andpshift()so as to not conflict with some existing definitions in some test files that includesvm_util.h.Signed-off-by Peter Xu <[email protected]>---
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 04/29] selftests/mm: Merge util.h into vm_util.h
In reply to Peter XuQuoted 7 lines · Peter Xu
There're two util headers under
mm/kselftest. Merge one with another. It turns oututil.his the easy one to move.When merging, drop
PAGE_SIZE/PAGE_SHIFTbecause they're unnecessary wrappers topage_size()/page_shift(), meanwhile rename them topsize()andpshift()so as to not conflict with some existing definitions in some test files that includesvm_util.h.
For me this sounds like two patches, but it's not really important with diffstat that small.
Quoted 1 line
Signed-off-by Peter Xu <[email protected]>
Quoted 336 lines
--- tools/testing/selftests/mm/Makefile | 4 ++ tools/testing/selftests/mm/gup_test.c | 5 +- tools/testing/selftests/mm/ksm_tests.c | 2 +- tools/testing/selftests/mm/mrelease_test.c | 11 ++- tools/testing/selftests/mm/transhuge-stress.c | 12 ++-- tools/testing/selftests/mm/util.h | 69 ------------------- tools/testing/selftests/mm/vm_util.c | 31 +++++++++ tools/testing/selftests/mm/vm_util.h | 31 +++++++++ 8 files changed, 80 insertions(+), 85 deletions(-) delete mode 100644 tools/testing/selftests/mm/util.htools/testing/selftests/mm/Makefile +4
-0@@ -111,6 +111,10 @@ $(OUTPUT)/madv_populate: vm_util.c $(OUTPUT)/soft-dirty: vm_util.c $(OUTPUT)/split_huge_page_test: vm_util.c $(OUTPUT)/userfaultfd: vm_util.c +$(OUTPUT)/gup_test: vm_util.c +$(OUTPUT)/mrelease_test: vm_util.c +$(OUTPUT)/transhuge-stress: vm_util.c +$(OUTPUT)/ksm_tests: vm_util.c ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))tools/testing/selftests/mm/gup_test.c +2
-3@@ -12,8 +12,7 @@#include <assert.h> #include <mm/gup_test.h> #include "../kselftest.h" - -#include "util.h" +#include "vm_util.h" #define MB (1UL << 20)@@ -251,7 +250,7 @@ int main(int argc, char **argv)if (touch) { gup.gup_flags |=FOLL_TOUCH ; }else { -for (; (unsigned long )p < gup.addr + size; p +=PAGE_SIZE ) +for (; (unsigned long )p < gup.addr + size; p +=psize ()) p[0 ] =0 ; }tools/testing/selftests/mm/ksm_tests.c +1
-1@@ -14,7 +14,7 @@#include "../kselftest.h" #include <include/vdso/time64.h> -#include "util.h" +#include "vm_util.h" #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"#define KSM_FP (s) (KSM_SYSFS_PATH s)tools/testing/selftests/mm/mrelease_test.c +5
-6@@ -9,8 +9,7 @@#include <stdlib.h> #include <sys/wait.h> #include <unistd.h> - -#include "util.h" +#include "vm_util.h" #include "../kselftest.h" @@ -32,7 +31,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd)unsigned long i;char *buf; - buf = (char *)mmap (NULL , nr_pages *PAGE_SIZE ,PROT_READ |PROT_WRITE , + buf = (char *)mmap (NULL , nr_pages *psize (),PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANON ,0 ,0 );if (buf ==MAP_FAILED ) {perror ("mmap failed, halting the test" );@@ -40,7 +39,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd) }for (i =0 ; i < nr_pages; i++) - *((unsigned long *)(buf + (i *PAGE_SIZE ))) = i; + *((unsigned long *)(buf + (i *psize ()))) = i;/* Signal the parent that the child is ready */ if (write (pipefd,"" ,1 ) <0 ) {@@ -54,7 +53,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd) timeout--; } -munmap (buf, nr_pages *PAGE_SIZE ); +munmap (buf, nr_pages *psize ());return (timeout >0 ) ?KSFT_PASS :KSFT_FAIL ; }@@ -87,7 +86,7 @@ static int child_main(int pipefd[], size_t size)/* Allocate and fault-in memory and wait to be killed */ close (pipefd[0 ]); - res =alloc_noexit (MB (size) /PAGE_SIZE , pipefd[1 ]); + res =alloc_noexit (MB (size) /psize (), pipefd[1 ]);close (pipefd[1 ]);return res; }tools/testing/selftests/mm/transhuge-stress.c +6
-6@@ -15,7 +15,7 @@#include <fcntl.h> #include <string.h> #include <sys/mman.h> -#include "util.h" +#include "vm_util.h" int backing_fd =-1 ;int mmap_flags =MAP_ANONYMOUS |MAP_NORESERVE |MAP_PRIVATE ;@@ -34,10 +34,10 @@ int main(int argc, char **argv)int pagemap_fd; ram =sysconf (_SC_PHYS_PAGES); -if (ram >SIZE_MAX /sysconf (_SC_PAGESIZE) /4 ) +if (ram >SIZE_MAX /psize () /4 ) ram =SIZE_MAX /4 ;else - ram *=sysconf (_SC_PAGESIZE); + ram *=psize (); len = ram;while (++i < argc) {@@ -58,7 +58,7 @@ int main(int argc, char **argv)warnx ("allocate %zd transhuge pages, using %zd MiB virtual memory" " and %zd MiB of ram" , len >>HPAGE_SHIFT , len >>20 , - ram >> (20 +HPAGE_SHIFT -PAGE_SHIFT -1 )); + ram >> (20 +HPAGE_SHIFT -pshift () -1 )); pagemap_fd =open ("/proc/self/pagemap" ,O_RDONLY );if (pagemap_fd <0 )@@ -92,7 +92,7 @@ int main(int argc, char **argv)if (pfn <0 ) { nr_failed++; }else { -size_t idx = pfn >> (HPAGE_SHIFT -PAGE_SHIFT ); +size_t idx = pfn >> (HPAGE_SHIFT -pshift ()); nr_succeed++;if (idx >= map_len) {@@ -108,7 +108,7 @@ int main(int argc, char **argv) }/* split transhuge page, keep last page */ -if (madvise (p,HPAGE_SIZE -PAGE_SIZE ,MADV_DONTNEED )) +if (madvise (p,HPAGE_SIZE -psize (),MADV_DONTNEED ))err (2 ,"MADV_DONTNEED" ); }clock_gettime (CLOCK_MONOTONIC , &b);tools/testing/selftests/mm/util.h deleted +0
-69@@ -1,69 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ - -#ifndef __KSELFTEST_VM_UTIL_H -#define __KSELFTEST_VM_UTIL_H - -#include <stdint.h> -#include <sys/mman.h> -#include <err.h> -#include <string.h> /* ffsl() */ -#include <unistd.h> /* _SC_PAGESIZE */ - -static unsigned int __page_size; -static unsigned int __page_shift; - -static inline unsigned int page_size (void ) -{ -if (!__page_size) - __page_size =sysconf (_SC_PAGESIZE); -return __page_size; -} - -static inline unsigned int page_shift (void ) -{ -if (!__page_shift) - __page_shift = (ffsl (page_size ()) -1 ); -return __page_shift; -} - -#define PAGE_SHIFT (page_shift()) -#define PAGE_SIZE (page_size()) -/* -* On ppc64 this will only work with radix 2M hugepage size -*/ -#define HPAGE_SHIFT 21 -#define HPAGE_SIZE (1 << HPAGE_SHIFT) - -#define PAGEMAP_PRESENT (ent) (((ent) & (1ull << 63)) != 0) -#define PAGEMAP_PFN (ent) ((ent) & ((1ull << 55) - 1)) - - -static inline int64_t allocate_transhuge (void *ptr,int pagemap_fd) -{ -uint64_t ent[2 ]; - -/* drop pmd */ -if (mmap (ptr,HPAGE_SIZE ,PROT_READ |PROT_WRITE , -MAP_FIXED |MAP_ANONYMOUS | -MAP_NORESERVE |MAP_PRIVATE ,-1 ,0 ) != ptr) -errx (2 ,"mmap transhuge" ); - -if (madvise (ptr,HPAGE_SIZE ,MADV_HUGEPAGE )) -err (2 ,"MADV_HUGEPAGE" ); - -/* allocate transparent huge page */ - *(volatile void **)ptr = ptr; - -if (pread (pagemap_fd, ent,sizeof (ent), - (uintptr_t )ptr >> (PAGE_SHIFT -3 )) !=sizeof (ent)) -err (2 ,"read pagemap" ); - -if (PAGEMAP_PRESENT (ent[0 ]) &&PAGEMAP_PRESENT (ent[1 ]) && -PAGEMAP_PFN (ent[0 ]) +1 ==PAGEMAP_PFN (ent[1 ]) && - !(PAGEMAP_PFN (ent[0 ]) & ((1 << (HPAGE_SHIFT -PAGE_SHIFT )) -1 ))) -return PAGEMAP_PFN (ent[0 ]); - -return -1 ; -} - -#endif tools/testing/selftests/mm/vm_util.c +31
-0@@ -8,6 +8,9 @@#define SMAP_FILE_PATH "/proc/self/smaps"#define MAX_LINE_LENGTH 500 +unsigned int __page_size; +unsigned int __page_shift; +uint64_t pagemap_get_entry (int fd,char *start) {const unsigned long pfn = (unsigned long )start /getpagesize ();@@ -149,3 +152,31 @@ bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size) {return __check_huge (addr,"ShmemPmdMapped:" , nr_hpages, hpage_size); } + +int64_t allocate_transhuge (void *ptr,int pagemap_fd) +{ +uint64_t ent[2 ]; + +/* drop pmd */ +if (mmap (ptr,HPAGE_SIZE ,PROT_READ |PROT_WRITE , +MAP_FIXED |MAP_ANONYMOUS | +MAP_NORESERVE |MAP_PRIVATE ,-1 ,0 ) != ptr) +errx (2 ,"mmap transhuge" ); + +if (madvise (ptr,HPAGE_SIZE ,MADV_HUGEPAGE )) +err (2 ,"MADV_HUGEPAGE" ); + +/* allocate transparent huge page */ + *(volatile void **)ptr = ptr; + +if (pread (pagemap_fd, ent,sizeof (ent), + (uintptr_t )ptr >> (pshift () -3 )) !=sizeof (ent)) +err (2 ,"read pagemap" ); + +if (PAGEMAP_PRESENT (ent[0 ]) &&PAGEMAP_PRESENT (ent[1 ]) && +PAGEMAP_PFN (ent[0 ]) +1 ==PAGEMAP_PFN (ent[1 ]) && + !(PAGEMAP_PFN (ent[0 ]) & ((1 << (HPAGE_SHIFT -pshift ())) -1 ))) +return PAGEMAP_PFN (ent[0 ]); + +return -1 ; +}tools/testing/selftests/mm/vm_util.h +31
-0@@ -1,6 +1,27 @@/* SPDX-License-Identifier: GPL-2.0 */ #include <stdint.h> #include <stdbool.h> +#include <sys/mman.h> +#include <err.h> +#include <string.h> /* ffsl() */ +#include <unistd.h> /* _SC_PAGESIZE */ + +extern unsigned int __page_size; +extern unsigned int __page_shift; + +static inline unsigned int psize (void ) +{ +if (!__page_size) + __page_size =sysconf (_SC_PAGESIZE); +return __page_size; +} + +static inline unsigned int pshift (void ) +{ +if (!__page_shift) + __page_shift = (ffsl (psize ()) -1 ); +return __page_shift; +}uint64_t pagemap_get_entry (int fd,char *start);bool pagemap_is_softdirty (int fd,char *start);@@ -13,3 +34,13 @@ uint64_t read_pmd_pagesize(void);bool check_huge_anon (void *addr,int nr_hpages,uint64_t hpage_size);bool check_huge_file (void *addr,int nr_hpages,uint64_t hpage_size);bool check_huge_shmem (void *addr,int nr_hpages,uint64_t hpage_size); +int64_t allocate_transhuge (void *ptr,int pagemap_fd); + +/* +* On ppc64 this will only work with radix 2M hugepage size +*/ +#define HPAGE_SHIFT 21 +#define HPAGE_SIZE (1 << HPAGE_SHIFT) + +#define PAGEMAP_PRESENT (ent) (((ent) & (1ull << 63)) != 0) +#define PAGEMAP_PFN (ent) ((ent) & ((1ull << 55) - 1))Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 5/29 selftests/mm: Use TEST_GEN_PROGS where proper
In reply to Peter XuTEST_GEN_PROGS and TEST_GEN_FILES are used randomly in the mm/Makefile to specify programs that need to build. Logically all these binaries should all fall into TEST_GEN_PROGS.
Replace those TEST_GEN_FILES with TEST_GEN_PROGS, so that we can reference all the tests easily later.
--- tools/testing/selftests/mm/Makefile | 63 +++++++++++++++-------------- 1 file changed, 32 insertions(+), 31 deletions(-)
tools/testing/selftests/mm/Makefile +32 -31
@@ -31,34 +31,35 @@ MAKEFLAGS += --no-builtin-rules CFLAGS = -Wall -I $(top_srcdir) -I $(top_srcdir)/tools/include/uapi $(EXTRA_CFLAGS) $(KHDR_INCLUDES) LDLIBS = -lrt -lpthread -TEST_GEN_FILES = cow -TEST_GEN_FILES += compaction_test -TEST_GEN_FILES += gup_test -TEST_GEN_FILES += hmm-tests -TEST_GEN_FILES += hugetlb-madvise -TEST_GEN_FILES += hugepage-mmap -TEST_GEN_FILES += hugepage-mremap -TEST_GEN_FILES += hugepage-shm -TEST_GEN_FILES += hugepage-vmemmap -TEST_GEN_FILES += khugepaged + +TEST_GEN_PROGS = cow +TEST_GEN_PROGS += compaction_test +TEST_GEN_PROGS += gup_test +TEST_GEN_PROGS += hmm-tests +TEST_GEN_PROGS += hugetlb-madvise +TEST_GEN_PROGS += hugepage-mmap +TEST_GEN_PROGS += hugepage-mremap +TEST_GEN_PROGS += hugepage-shm +TEST_GEN_PROGS += hugepage-vmemmap +TEST_GEN_PROGS += khugepaged TEST_GEN_PROGS = madv_populate -TEST_GEN_FILES += map_fixed_noreplace -TEST_GEN_FILES += map_hugetlb -TEST_GEN_FILES += map_populate -TEST_GEN_FILES += memfd_secret -TEST_GEN_FILES += migration -TEST_GEN_FILES += mlock-random-test -TEST_GEN_FILES += mlock2-tests -TEST_GEN_FILES += mrelease_test -TEST_GEN_FILES += mremap_dontunmap -TEST_GEN_FILES += mremap_test -TEST_GEN_FILES += on-fault-limit -TEST_GEN_FILES += thuge-gen -TEST_GEN_FILES += transhuge-stress -TEST_GEN_FILES += userfaultfd +TEST_GEN_PROGS += map_fixed_noreplace +TEST_GEN_PROGS += map_hugetlb +TEST_GEN_PROGS += map_populate +TEST_GEN_PROGS += memfd_secret +TEST_GEN_PROGS += migration +TEST_GEN_PROGS += mlock-random-test +TEST_GEN_PROGS += mlock2-tests +TEST_GEN_PROGS += mrelease_test +TEST_GEN_PROGS += mremap_dontunmap +TEST_GEN_PROGS += mremap_test +TEST_GEN_PROGS += on-fault-limit +TEST_GEN_PROGS += thuge-gen +TEST_GEN_PROGS += transhuge-stress +TEST_GEN_PROGS += userfaultfd TEST_GEN_PROGS += soft-dirty TEST_GEN_PROGS += split_huge_page_test -TEST_GEN_FILES += ksm_tests +TEST_GEN_PROGS += ksm_tests TEST_GEN_PROGS += ksm_functional_tests TEST_GEN_PROGS += mdwe_test
@@ -76,24 +77,24 @@ CFLAGS += -no-pie endif ifeq ($(CAN_BUILD_I386),1) -TEST_GEN_FILES += $(BINARIES_32) +TEST_GEN_PROGS += $(BINARIES_32) endif ifeq ($(CAN_BUILD_X86_64),1) -TEST_GEN_FILES += $(BINARIES_64) +TEST_GEN_PROGS += $(BINARIES_64) endif else ifneq (,$(findstring $(MACHINE),ppc64)) -TEST_GEN_FILES += protection_keys +TEST_GEN_PROGS += protection_keys endif endif ifneq (,$(filter $(MACHINE),arm64 ia64 mips64 parisc64 ppc64 riscv64 s390x sh64 sparc64 x86_64)) -TEST_GEN_FILES += va_128TBswitch -TEST_GEN_FILES += virtual_address_range -TEST_GEN_FILES += write_to_hugetlbfs +TEST_GEN_PROGS += va_128TBswitch +TEST_GEN_PROGS += virtual_address_range +TEST_GEN_PROGS += write_to_hugetlbfs endif TEST_PROGS := run_vmtests.sh
Signature · 1 line
2.39.1
Re: [PATCH 05/29] selftests/mm: Use TEST_GEN_PROGS where proper
Quoted 9 lines · Peter Xu
TEST_GEN_PROGSandTEST_GEN_FILESare used randomly in the mm/Makefile to specify programs that need to build. Logically all these binaries should all fall intoTEST_GEN_PROGS.Replace those
TEST_GEN_FILESwithTEST_GEN_PROGS, so that we can reference all the tests easily later.Signed-off-by Peter Xu <[email protected]>---
Yes, makes sense
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 05/29] selftests/mm: Use TEST_GEN_PROGS where proper
In reply to Peter XuQuoted 8 lines · Peter Xu
TEST_GEN_PROGSandTEST_GEN_FILESare used randomly in the mm/Makefile to specify programs that need to build. Logically all these binaries should all fall intoTEST_GEN_PROGS.Replace those
TEST_GEN_FILESwithTEST_GEN_PROGS, so that we can reference all the tests easily later.Signed-off-by Peter Xu <[email protected]>
Quoted 103 lines
--- tools/testing/selftests/mm/Makefile | 63 +++++++++++++++-------------- 1 file changed, 32 insertions(+), 31 deletions(-)tools/testing/selftests/mm/Makefile +32
-31@@ -31,34 +31,35 @@ MAKEFLAGS += --no-builtin-rules CFLAGS = -Wall -I $(top_srcdir) -I $(top_srcdir)/tools/include/uapi $(EXTRA_CFLAGS) $(KHDR_INCLUDES) LDLIBS = -lrt -lpthread -TEST_GEN_FILES = cow -TEST_GEN_FILES += compaction_test -TEST_GEN_FILES += gup_test -TEST_GEN_FILES += hmm-tests -TEST_GEN_FILES += hugetlb-madvise -TEST_GEN_FILES += hugepage-mmap -TEST_GEN_FILES += hugepage-mremap -TEST_GEN_FILES += hugepage-shm -TEST_GEN_FILES += hugepage-vmemmap -TEST_GEN_FILES += khugepaged + +TEST_GEN_PROGS = cow +TEST_GEN_PROGS += compaction_test +TEST_GEN_PROGS += gup_test +TEST_GEN_PROGS += hmm-tests +TEST_GEN_PROGS += hugetlb-madvise +TEST_GEN_PROGS += hugepage-mmap +TEST_GEN_PROGS += hugepage-mremap +TEST_GEN_PROGS += hugepage-shm +TEST_GEN_PROGS += hugepage-vmemmap +TEST_GEN_PROGS += khugepaged TEST_GEN_PROGS = madv_populate -TEST_GEN_FILES += map_fixed_noreplace -TEST_GEN_FILES += map_hugetlb -TEST_GEN_FILES += map_populate -TEST_GEN_FILES += memfd_secret -TEST_GEN_FILES += migration -TEST_GEN_FILES += mlock-random-test -TEST_GEN_FILES += mlock2-tests -TEST_GEN_FILES += mrelease_test -TEST_GEN_FILES += mremap_dontunmap -TEST_GEN_FILES += mremap_test -TEST_GEN_FILES += on-fault-limit -TEST_GEN_FILES += thuge-gen -TEST_GEN_FILES += transhuge-stress -TEST_GEN_FILES += userfaultfd +TEST_GEN_PROGS += map_fixed_noreplace +TEST_GEN_PROGS += map_hugetlb +TEST_GEN_PROGS += map_populate +TEST_GEN_PROGS += memfd_secret +TEST_GEN_PROGS += migration +TEST_GEN_PROGS += mlock-random-test +TEST_GEN_PROGS += mlock2-tests +TEST_GEN_PROGS += mrelease_test +TEST_GEN_PROGS += mremap_dontunmap +TEST_GEN_PROGS += mremap_test +TEST_GEN_PROGS += on-fault-limit +TEST_GEN_PROGS += thuge-gen +TEST_GEN_PROGS += transhuge-stress +TEST_GEN_PROGS += userfaultfd TEST_GEN_PROGS += soft-dirty TEST_GEN_PROGS += split_huge_page_test -TEST_GEN_FILES += ksm_tests +TEST_GEN_PROGS += ksm_tests TEST_GEN_PROGS += ksm_functional_tests TEST_GEN_PROGS += mdwe_test@@ -76,24 +77,24 @@ CFLAGS += -no-pie endif ifeq ($(CAN_BUILD_I386),1) -TEST_GEN_FILES += $(BINARIES_32) +TEST_GEN_PROGS += $(BINARIES_32) endif ifeq ($(CAN_BUILD_X86_64),1) -TEST_GEN_FILES += $(BINARIES_64) +TEST_GEN_PROGS += $(BINARIES_64) endif else ifneq (,$(findstring $(MACHINE),ppc64)) -TEST_GEN_FILES += protection_keys +TEST_GEN_PROGS += protection_keys endif endif ifneq (,$(filter $(MACHINE),arm64 ia64 mips64 parisc64 ppc64 riscv64 s390x sh64 sparc64 x86_64)) -TEST_GEN_FILES += va_128TBswitch -TEST_GEN_FILES += virtual_address_range -TEST_GEN_FILES += write_to_hugetlbfs +TEST_GEN_PROGS += va_128TBswitch +TEST_GEN_PROGS += virtual_address_range +TEST_GEN_PROGS += write_to_hugetlbfs endif TEST_PROGS := run_vmtests.shSignature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 6/29 selftests/mm: Link vm_util.c always
In reply to Peter XuWe do have plenty of files that want to link against vm_util.c. Just make it simple by linking it always.
--- tools/testing/selftests/mm/Makefile | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
tools/testing/selftests/mm/Makefile +1 -11
@@ -105,17 +105,7 @@ TEST_FILES += va_128TBswitch.sh include ../lib.mk -$(OUTPUT)/cow: vm_util.c -$(OUTPUT)/khugepaged: vm_util.c -$(OUTPUT)/ksm_functional_tests: vm_util.c -$(OUTPUT)/madv_populate: vm_util.c -$(OUTPUT)/soft-dirty: vm_util.c -$(OUTPUT)/split_huge_page_test: vm_util.c -$(OUTPUT)/userfaultfd: vm_util.c -$(OUTPUT)/gup_test: vm_util.c -$(OUTPUT)/mrelease_test: vm_util.c -$(OUTPUT)/transhuge-stress: vm_util.c -$(OUTPUT)/ksm_tests: vm_util.c +$(TEST_GEN_PROGS): vm_util.c ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
Signature · 1 line
2.39.1
Re: [PATCH 06/29] selftests/mm: Link vm_util.c always
Quoted 21 lines · Peter Xu
We do have plenty of files that want to link against
vm_util.c. Just make it simple by linking it always.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/Makefile | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)tools/testing/selftests/mm/Makefile +0
-5@@ -105,17 +105,7 @@ TEST_FILES += va_128TBswitch.sh include ../lib.mk -$(OUTPUT)/cow: vm_util.c -$(OUTPUT)/khugepaged: vm_util.c -$(OUTPUT)/ksm_functional_tests: vm_util.c -$(OUTPUT)/madv_populate: vm_util.c -$(OUTPUT)/soft-dirty: vm_util.c
tools/testing/selftests/mm/Makefile TEST_FILES += va_128TBswitch.sh · line 111
Quoted 10 lines
tools/testing/selftests/mm/Makefile +1
-6-$(OUTPUT)/split_huge_page_test: vm_util.c -$(OUTPUT)/userfaultfd: vm_util.c -$(OUTPUT)/gup_test: vm_util.c -$(OUTPUT)/mrelease_test: vm_util.c -$(OUTPUT)/transhuge-stress: vm_util.c -$(OUTPUT)/ksm_tests: vm_util.c +$(TEST_GEN_PROGS): vm_util.c ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 06/29] selftests/mm: Link vm_util.c always
In reply to Peter XuQuoted 4 lines · Peter Xu
We do have plenty of files that want to link against
vm_util.c. Just make it simple by linking it always.Signed-off-by Peter Xu <[email protected]>
Quoted 30 lines
--- tools/testing/selftests/mm/Makefile | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)tools/testing/selftests/mm/Makefile +1
-11@@ -105,17 +105,7 @@ TEST_FILES += va_128TBswitch.sh include ../lib.mk -$(OUTPUT)/cow: vm_util.c -$(OUTPUT)/khugepaged: vm_util.c -$(OUTPUT)/ksm_functional_tests: vm_util.c -$(OUTPUT)/madv_populate: vm_util.c -$(OUTPUT)/soft-dirty: vm_util.c -$(OUTPUT)/split_huge_page_test: vm_util.c -$(OUTPUT)/userfaultfd: vm_util.c -$(OUTPUT)/gup_test: vm_util.c -$(OUTPUT)/mrelease_test: vm_util.c -$(OUTPUT)/transhuge-stress: vm_util.c -$(OUTPUT)/ksm_tests: vm_util.c +$(TEST_GEN_PROGS): vm_util.c ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 7/29 selftests/mm: Merge default_huge_page_size() into one
In reply to Peter XuThere're already 3 same definitions of the three functions. Move it into vm_util.[ch].
--- tools/testing/selftests/mm/hugetlb-madvise.c | 25 +------------------- tools/testing/selftests/mm/thuge-gen.c | 19 +-------------- tools/testing/selftests/mm/userfaultfd.c | 24 ------------------- tools/testing/selftests/mm/vm_util.c | 21 ++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 1 + 5 files changed, 24 insertions(+), 66 deletions(-)
tools/testing/selftests/mm/hugetlb-madvise.c +1 -24
@@ -18,6 +18,7 @@#include <unistd.h> #include <sys/mman.h> #include <fcntl.h> +#include "vm_util.h" #define MIN_FREE_PAGES 20#define NR_HUGE_PAGES 10/* common number of pages to map/allocate */
@@ -35,30 +36,6 @@unsigned long huge_page_size;unsigned long base_page_size; -/* -* default_huge_page_size copied from mlock2-tests.c -*/ -unsigned long default_huge_page_size (void ) -{ -unsigned long hps =0 ; -char *line =NULL ; -size_t linelen =0 ; -FILE *f =fopen ("/proc/meminfo" ,"r" ); - -if (!f) -return 0 ; -while (getline (&line, &linelen, f) >0 ) { -if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { - hps <<=10 ; -break ; - } - } - -free (line); -fclose (f); -return hps; -} -unsigned long get_free_hugepages (void ) {unsigned long fhp =0 ;
tools/testing/selftests/mm/thuge-gen.c +1 -18
@@ -24,6 +24,7 @@#include <unistd.h> #include <stdarg.h> #include <string.h> +#include "vm_util.h" #define err (x) perror(x), exit(1)
@@ -74,24 +75,6 @@ void find_pagesizes(void)globfree (&g); } -unsigned long default_huge_page_size (void ) -{ -unsigned long hps =0 ; -char *line =NULL ; -size_t linelen =0 ; -FILE *f =fopen ("/proc/meminfo" ,"r" ); -if (!f) -return 0 ; -while (getline (&line, &linelen, f) >0 ) { -if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { - hps <<=10 ; -break ; - } - } -free (line); -return hps; -} -void show (unsigned long ps) {char buf[100 ];
tools/testing/selftests/mm/userfaultfd.c +0 -24
@@ -1703,30 +1703,6 @@ static int userfaultfd_stress(void) || userfaultfd_events_test() || userfaultfd_minor_test(); } -/* -* Copied from mlock2-tests.c -*/ -unsigned long default_huge_page_size (void ) -{ -unsigned long hps =0 ; -char *line =NULL ; -size_t linelen =0 ; -FILE *f =fopen ("/proc/meminfo" ,"r" ); - -if (!f) -return 0 ; -while (getline (&line, &linelen, f) >0 ) { -if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { - hps <<=10 ; -break ; - } - } - -free (line); -fclose (f); -return hps; -} -static void set_test_type (const char *type) {if (!strcmp (type,"anon" )) {
tools/testing/selftests/mm/vm_util.c +21 -0
@@ -180,3 +180,24 @@ int64_t allocate_transhuge(void *ptr, int pagemap_fd)return -1 ; } + +unsigned long default_huge_page_size (void ) +{ +unsigned long hps =0 ; +char *line =NULL ; +size_t linelen =0 ; +FILE *f =fopen ("/proc/meminfo" ,"r" ); + +if (!f) +return 0 ; +while (getline (&line, &linelen, f) >0 ) { +if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { + hps <<=10 ; +break ; + } + } + +free (line); +fclose (f); +return hps; +}
tools/testing/selftests/mm/vm_util.h +1 -0
@@ -35,6 +35,7 @@ bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);bool check_huge_file (void *addr,int nr_hpages,uint64_t hpage_size);bool check_huge_shmem (void *addr,int nr_hpages,uint64_t hpage_size);int64_t allocate_transhuge (void *ptr,int pagemap_fd); +unsigned long default_huge_page_size (void );/* * On ppc64 this will only work with radix 2M hugepage size
Signature · 1 line
2.39.1
Re: [PATCH 07/29] selftests/mm: Merge default_huge_page_size() into one
Quoted 5 lines · Peter Xu
There're already 3 same definitions of the three functions. Move it into vm_util.[ch].
Signed-off-by Peter Xu <[email protected]>
Quoted 167 lines
--- tools/testing/selftests/mm/hugetlb-madvise.c | 25 +------------------- tools/testing/selftests/mm/thuge-gen.c | 19 +-------------- tools/testing/selftests/mm/userfaultfd.c | 24 ------------------- tools/testing/selftests/mm/vm_util.c | 21 ++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 1 + 5 files changed, 24 insertions(+), 66 deletions(-)tools/testing/selftests/mm/hugetlb-madvise.c +1
-24@@ -18,6 +18,7 @@#include <unistd.h> #include <sys/mman.h> #include <fcntl.h> +#include "vm_util.h" #define MIN_FREE_PAGES 20#define NR_HUGE_PAGES 10/* common number of pages to map/allocate */ @@ -35,30 +36,6 @@unsigned long huge_page_size;unsigned long base_page_size; -/* -* default_huge_page_size copied from mlock2-tests.c -*/ -unsigned long default_huge_page_size (void ) -{ -unsigned long hps =0 ; -char *line =NULL ; -size_t linelen =0 ; -FILE *f =fopen ("/proc/meminfo" ,"r" ); - -if (!f) -return 0 ; -while (getline (&line, &linelen, f) >0 ) { -if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { - hps <<=10 ; -break ; - } - } - -free (line); -fclose (f); -return hps; -} -unsigned long get_free_hugepages (void ) {unsigned long fhp =0 ;tools/testing/selftests/mm/thuge-gen.c +1
-18@@ -24,6 +24,7 @@#include <unistd.h> #include <stdarg.h> #include <string.h> +#include "vm_util.h" #define err (x) perror(x), exit(1)@@ -74,24 +75,6 @@ void find_pagesizes(void)globfree (&g); } -unsigned long default_huge_page_size (void ) -{ -unsigned long hps =0 ; -char *line =NULL ; -size_t linelen =0 ; -FILE *f =fopen ("/proc/meminfo" ,"r" ); -if (!f) -return 0 ; -while (getline (&line, &linelen, f) >0 ) { -if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { - hps <<=10 ; -break ; - } - } -free (line); -return hps; -} -void show (unsigned long ps) {char buf[100 ];tools/testing/selftests/mm/userfaultfd.c +0
-24@@ -1703,30 +1703,6 @@ static int userfaultfd_stress(void) || userfaultfd_events_test() || userfaultfd_minor_test(); } -/* -* Copied from mlock2-tests.c -*/ -unsigned long default_huge_page_size (void ) -{ -unsigned long hps =0 ; -char *line =NULL ; -size_t linelen =0 ; -FILE *f =fopen ("/proc/meminfo" ,"r" ); - -if (!f) -return 0 ; -while (getline (&line, &linelen, f) >0 ) { -if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { - hps <<=10 ; -break ; - } - } - -free (line); -fclose (f); -return hps; -} -static void set_test_type (const char *type) {if (!strcmp (type,"anon" )) {tools/testing/selftests/mm/vm_util.c +21
-0@@ -180,3 +180,24 @@ int64_t allocate_transhuge(void *ptr, int pagemap_fd)return -1 ; } + +unsigned long default_huge_page_size (void ) +{ +unsigned long hps =0 ; +char *line =NULL ; +size_t linelen =0 ; +FILE *f =fopen ("/proc/meminfo" ,"r" ); + +if (!f) +return 0 ; +while (getline (&line, &linelen, f) >0 ) { +if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { + hps <<=10 ; +break ; + } + } + +free (line); +fclose (f); +return hps; +}tools/testing/selftests/mm/vm_util.h +1
-1@@ -35,6 +35,7 @@ bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);bool check_huge_file (void *addr,int nr_hpages,uint64_t hpage_size);bool check_huge_shmem (void *addr,int nr_hpages,uint64_t hpage_size);int64_t allocate_transhuge (void *ptr,int pagemap_fd); +unsigned long default_huge_page_size (void );/* * On ppc64 this will only work with radix 2M hugepage size -- 2.39.1
Re: [PATCH 07/29] selftests/mm: Merge default_huge_page_size() into one
In reply to Peter XuQuoted 11 lines · Peter Xu
There're already 3 same definitions of the three functions. Move it into vm_util.[ch].
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/hugetlb-madvise.c | 25 +------------------- tools/testing/selftests/mm/thuge-gen.c | 19 +-------------- tools/testing/selftests/mm/userfaultfd.c | 24 ------------------- tools/testing/selftests/mm/vm_util.c | 21 ++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 1 + 5 files changed, 24 insertions(+), 66 deletions(-)
Thanks!
Some of those copies were make by me. Sorry.
BTW - The same code (even with '... copied from mlock2-tests.c) resides in the selftests/memfd directory. I did that as well. :( Suspect it is OK to leave the copy there. But, it does make me wonder why memfd is not in the mm directory?
Signature · 1 line
Mike Kravetz
Peter Xu
reviewerRe: [PATCH 07/29] selftests/mm: Merge default_huge_page_size() into one
Quoted 13 lines · Mike Kravetz
Quoted 11 lines · Peter Xu
There're already 3 same definitions of the three functions. Move it into vm_util.[ch].
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/hugetlb-madvise.c | 25 +------------------- tools/testing/selftests/mm/thuge-gen.c | 19 +-------------- tools/testing/selftests/mm/userfaultfd.c | 24 ------------------- tools/testing/selftests/mm/vm_util.c | 21 ++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 1 + 5 files changed, 24 insertions(+), 66 deletions(-)
Quoted 5 lines
Thanks!
Some of those copies were make by me. Sorry.
Reviewed-by Mike Kravetz <[email protected]>
Thanks!
Quoted 5 lines
BTW - The same code (even with '... copied from
mlock2-tests.c) resides in the selftests/memfd directory. I did that as well. :( Suspect it is OK to leave the copy there. But, it does make me wonder why memfd is not in the mm directory?
I don't know either, I suspect it was just a personal preference when it was firstly introduced in:
commit 4f5ce5e8d7e2da3c714df8a7fa42edb9f992fc52
Author: David Herrmann <[email protected]>
Date: Fri Aug 8 14:25:32 2014 -0700
selftests: add memfd_create() + sealing tests
So I left all those alone for now. Logically they should be merged into mm/ indeed, perhaps in some future cleanups.
Signature · 1 line
Peter Xu
Re: [PATCH 07/29] selftests/mm: Merge default_huge_page_size() into one
In reply to Peter XuQuoted 5 lines · Peter Xu
There're already 3 same definitions of the three functions. Move it into vm_util.[ch].
Signed-off-by Peter Xu <[email protected]>---
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 07/29] selftests/mm: Merge default_huge_page_size() into one
In reply to Peter XuQuoted 4 lines · Peter Xu
There're already 3 same definitions of the three functions. Move it into vm_util.[ch].
Signed-off-by Peter Xu <[email protected]>
Quoted 167 lines
--- tools/testing/selftests/mm/hugetlb-madvise.c | 25 +------------------- tools/testing/selftests/mm/thuge-gen.c | 19 +-------------- tools/testing/selftests/mm/userfaultfd.c | 24 ------------------- tools/testing/selftests/mm/vm_util.c | 21 ++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 1 + 5 files changed, 24 insertions(+), 66 deletions(-)tools/testing/selftests/mm/hugetlb-madvise.c +1
-24@@ -18,6 +18,7 @@#include <unistd.h> #include <sys/mman.h> #include <fcntl.h> +#include "vm_util.h" #define MIN_FREE_PAGES 20#define NR_HUGE_PAGES 10/* common number of pages to map/allocate */ @@ -35,30 +36,6 @@unsigned long huge_page_size;unsigned long base_page_size; -/* -* default_huge_page_size copied from mlock2-tests.c -*/ -unsigned long default_huge_page_size (void ) -{ -unsigned long hps =0 ; -char *line =NULL ; -size_t linelen =0 ; -FILE *f =fopen ("/proc/meminfo" ,"r" ); - -if (!f) -return 0 ; -while (getline (&line, &linelen, f) >0 ) { -if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { - hps <<=10 ; -break ; - } - } - -free (line); -fclose (f); -return hps; -} -unsigned long get_free_hugepages (void ) {unsigned long fhp =0 ;tools/testing/selftests/mm/thuge-gen.c +1
-18@@ -24,6 +24,7 @@#include <unistd.h> #include <stdarg.h> #include <string.h> +#include "vm_util.h" #define err (x) perror(x), exit(1)@@ -74,24 +75,6 @@ void find_pagesizes(void)globfree (&g); } -unsigned long default_huge_page_size (void ) -{ -unsigned long hps =0 ; -char *line =NULL ; -size_t linelen =0 ; -FILE *f =fopen ("/proc/meminfo" ,"r" ); -if (!f) -return 0 ; -while (getline (&line, &linelen, f) >0 ) { -if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { - hps <<=10 ; -break ; - } - } -free (line); -return hps; -} -void show (unsigned long ps) {char buf[100 ];tools/testing/selftests/mm/userfaultfd.c +0
-24@@ -1703,30 +1703,6 @@ static int userfaultfd_stress(void) || userfaultfd_events_test() || userfaultfd_minor_test(); } -/* -* Copied from mlock2-tests.c -*/ -unsigned long default_huge_page_size (void ) -{ -unsigned long hps =0 ; -char *line =NULL ; -size_t linelen =0 ; -FILE *f =fopen ("/proc/meminfo" ,"r" ); - -if (!f) -return 0 ; -while (getline (&line, &linelen, f) >0 ) { -if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { - hps <<=10 ; -break ; - } - } - -free (line); -fclose (f); -return hps; -} -static void set_test_type (const char *type) {if (!strcmp (type,"anon" )) {tools/testing/selftests/mm/vm_util.c +21
-0@@ -180,3 +180,24 @@ int64_t allocate_transhuge(void *ptr, int pagemap_fd)return -1 ; } + +unsigned long default_huge_page_size (void ) +{ +unsigned long hps =0 ; +char *line =NULL ; +size_t linelen =0 ; +FILE *f =fopen ("/proc/meminfo" ,"r" ); + +if (!f) +return 0 ; +while (getline (&line, &linelen, f) >0 ) { +if (sscanf (line,"Hugepagesize: %lu kB" , &hps) ==1 ) { + hps <<=10 ; +break ; + } + } + +free (line); +fclose (f); +return hps; +}tools/testing/selftests/mm/vm_util.h +1
-0@@ -35,6 +35,7 @@ bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);bool check_huge_file (void *addr,int nr_hpages,uint64_t hpage_size);bool check_huge_shmem (void *addr,int nr_hpages,uint64_t hpage_size);int64_t allocate_transhuge (void *ptr,int pagemap_fd); +unsigned long default_huge_page_size (void );/* * On ppc64 this will only work with radix 2M hugepage size Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 8/29 selftests/mm: Use PM_* macros in vm_utils.h
In reply to Peter XuWe've got the macros in uffd-stress.c, move it over and use it in vm_util.h.
--- tools/testing/selftests/mm/userfaultfd.c | 8 -------- tools/testing/selftests/mm/vm_util.c | 16 ++++------------ tools/testing/selftests/mm/vm_util.h | 8 ++++++++ 3 files changed, 12 insertions(+), 20 deletions(-)
tools/testing/selftests/mm/userfaultfd.c +0 -8
@@ -1389,14 +1389,6 @@ static int userfaultfd_minor_test(void)return stats.missing_faults !=0 || stats.minor_faults != nr_pages; } -#define BIT_ULL (nr) (1ULL << (nr)) -#define PM_SOFT_DIRTY BIT_ULL(55) -#define PM_MMAP_EXCLUSIVE BIT_ULL(56) -#define PM_UFFD_WP BIT_ULL(57) -#define PM_FILE BIT_ULL(61) -#define PM_SWAP BIT_ULL(62) -#define PM_PRESENT BIT_ULL(63) -static int pagemap_open (void ) {int fd =open ("/proc/self/pagemap" ,O_RDONLY );
tools/testing/selftests/mm/vm_util.c +4 -12
@@ -25,25 +25,17 @@ uint64_t pagemap_get_entry(int fd, char *start)bool pagemap_is_softdirty (int fd,char *start) { -uint64_t entry =pagemap_get_entry (fd, start); - -// Check if dirty bit (55th bit) is set -return entry &0x0080000000000000ull ; +return pagemap_get_entry (fd, start) &PM_SOFT_DIRTY ; }bool pagemap_is_swapped (int fd,char *start) { -uint64_t entry =pagemap_get_entry (fd, start); - -return entry &0x4000000000000000ull ; +return pagemap_get_entry (fd, start) &PM_SWAP ; }bool pagemap_is_populated (int fd,char *start) { -uint64_t entry =pagemap_get_entry (fd, start); - -/* Present or swapped. */ -return entry &0xc000000000000000ull ; +return pagemap_get_entry (fd, start) & (PM_PRESENT |PM_SWAP ); }unsigned long pagemap_get_pfn (int fd,char *start)
@@ -51,7 +43,7 @@ unsigned long pagemap_get_pfn(int fd, char *start)uint64_t entry =pagemap_get_entry (fd, start);/* If present (63th bit), PFN is at bit 0 -- 54. */ -if (entry &0x8000000000000000ull ) +if (entry &PM_PRESENT )return entry &0x007fffffffffffffull ;return -1ul ; }
tools/testing/selftests/mm/vm_util.h +8 -0
@@ -6,6 +6,14 @@#include <string.h> /* ffsl() */ #include <unistd.h> /* _SC_PAGESIZE */ +#define BIT_ULL (nr) (1ULL << (nr)) +#define PM_SOFT_DIRTY BIT_ULL(55) +#define PM_MMAP_EXCLUSIVE BIT_ULL(56) +#define PM_UFFD_WP BIT_ULL(57) +#define PM_FILE BIT_ULL(61) +#define PM_SWAP BIT_ULL(62) +#define PM_PRESENT BIT_ULL(63) +extern unsigned int __page_size;extern unsigned int __page_shift;
Signature · 1 line
2.39.1
Re: [PATCH 08/29] selftests/mm: Use PM_* macros in vm_utils.h
Quoted 9 lines · Peter Xu
We've got the macros in
uffd-stress.c, move it over and use it invm_util.h.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/userfaultfd.c | 8 -------- tools/testing/selftests/mm/vm_util.c | 16 ++++------------ tools/testing/selftests/mm/vm_util.h | 8 ++++++++ 3 files changed, 12 insertions(+), 20 deletions(-)
Nice, thanks!
Signature · 1 line
Mike Kravetz
Re: [PATCH 08/29] selftests/mm: Use PM_* macros in vm_utils.h
In reply to Peter XuQuoted 5 lines · Peter Xu
We've got the macros in
uffd-stress.c, move it over and use it invm_util.h.Signed-off-by Peter Xu <[email protected]>---
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 08/29] selftests/mm: Use PM_* macros in vm_utils.h
In reply to Peter XuQuoted 4 lines · Peter Xu
We've got the macros in
uffd-stress.c, move it over and use it invm_util.h.Signed-off-by Peter Xu <[email protected]>
Quoted 89 lines
--- tools/testing/selftests/mm/userfaultfd.c | 8 -------- tools/testing/selftests/mm/vm_util.c | 16 ++++------------ tools/testing/selftests/mm/vm_util.h | 8 ++++++++ 3 files changed, 12 insertions(+), 20 deletions(-)tools/testing/selftests/mm/userfaultfd.c +0
-8@@ -1389,14 +1389,6 @@ static int userfaultfd_minor_test(void)return stats.missing_faults !=0 || stats.minor_faults != nr_pages; } -#define BIT_ULL (nr) (1ULL << (nr)) -#define PM_SOFT_DIRTY BIT_ULL(55) -#define PM_MMAP_EXCLUSIVE BIT_ULL(56) -#define PM_UFFD_WP BIT_ULL(57) -#define PM_FILE BIT_ULL(61) -#define PM_SWAP BIT_ULL(62) -#define PM_PRESENT BIT_ULL(63) -static int pagemap_open (void ) {int fd =open ("/proc/self/pagemap" ,O_RDONLY );tools/testing/selftests/mm/vm_util.c +4
-12@@ -25,25 +25,17 @@ uint64_t pagemap_get_entry(int fd, char *start)bool pagemap_is_softdirty (int fd,char *start) { -uint64_t entry =pagemap_get_entry (fd, start); - -// Check if dirty bit (55th bit) is set -return entry &0x0080000000000000ull ; +return pagemap_get_entry (fd, start) &PM_SOFT_DIRTY ; }bool pagemap_is_swapped (int fd,char *start) { -uint64_t entry =pagemap_get_entry (fd, start); - -return entry &0x4000000000000000ull ; +return pagemap_get_entry (fd, start) &PM_SWAP ; }bool pagemap_is_populated (int fd,char *start) { -uint64_t entry =pagemap_get_entry (fd, start); - -/* Present or swapped. */ -return entry &0xc000000000000000ull ; +return pagemap_get_entry (fd, start) & (PM_PRESENT |PM_SWAP ); }unsigned long pagemap_get_pfn (int fd,char *start)@@ -51,7 +43,7 @@ unsigned long pagemap_get_pfn(int fd, char *start)uint64_t entry =pagemap_get_entry (fd, start);/* If present (63th bit), PFN is at bit 0 -- 54. */ -if (entry &0x8000000000000000ull ) +if (entry &PM_PRESENT )return entry &0x007fffffffffffffull ;return -1ul ; }tools/testing/selftests/mm/vm_util.h +8
-0@@ -6,6 +6,14 @@#include <string.h> /* ffsl() */ #include <unistd.h> /* _SC_PAGESIZE */ +#define BIT_ULL (nr) (1ULL << (nr)) +#define PM_SOFT_DIRTY BIT_ULL(55) +#define PM_MMAP_EXCLUSIVE BIT_ULL(56) +#define PM_UFFD_WP BIT_ULL(57) +#define PM_FILE BIT_ULL(61) +#define PM_SWAP BIT_ULL(62) +#define PM_PRESENT BIT_ULL(63) +extern unsigned int __page_size;extern unsigned int __page_shift;Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 9/29 selftests/mm: Reuse pagemap_get_entry() in vm_util.h
In reply to Peter XuMeanwhile drop pagemap_read_vaddr().
--- tools/testing/selftests/mm/userfaultfd.c | 31 +++++++----------------- 1 file changed, 9 insertions(+), 22 deletions(-)
tools/testing/selftests/mm/userfaultfd.c +9 -22
@@ -1399,19 +1399,6 @@ static int pagemap_open(void)return fd; } -static uint64_t pagemap_read_vaddr (int fd,void *vaddr) -{ -uint64_t value; -int ret; - - ret =pread (fd, &value,sizeof (uint64_t ), - ((uint64_t )vaddr >>12 ) *sizeof (uint64_t )); -if (ret !=sizeof (uint64_t )) -err ("pread() on pagemap failed" ); - -return value; -} -/* This macro let __LINE__ works in err() */ #define pagemap_check_wp (value, wp) do { \ if (!!(value & PM_UFFD_WP) != wp) \
@@ -1427,7 +1414,7 @@ static int pagemap_test_fork(bool present)if (!child) {/* Open the pagemap fd of the child itself */ fd =pagemap_open (); - value =pagemap_read_vaddr (fd, area_dst); + value =pagemap_get_entry (fd, area_dst);/* * After fork() uffd-wp bit should be gone as long as we're * without UFFD_FEATURE_EVENT_FORK
@@ -1446,24 +1433,24 @@ static void userfaultfd_wp_unpopulated_test(int pagemap_fd)/* Test applying pte marker to anon unpopulated */ wp_range (uffd, (uint64_t )area_dst, page_size, true); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, true);/* Test unprotect on anon pte marker */ wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Test zap on anon marker */ wp_range (uffd, (uint64_t )area_dst, page_size, true);if (madvise (area_dst, page_size,MADV_DONTNEED ))err ("madvise(MADV_DONTNEED) failed" ); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Test fault in after marker removed */ *area_dst =1 ; - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Drop it to make pte none again */ if (madvise (area_dst, page_size,MADV_DONTNEED ))
@@ -1522,7 +1509,7 @@ static void userfaultfd_pagemap_test(unsigned int test_pgsize)/* Touch the page */ *area_dst =1 ;wp_range (uffd, (uint64_t )area_dst, test_pgsize, true); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, true);/* Make sure uffd-wp bit dropped when fork */ if (pagemap_test_fork (true))
@@ -1536,7 +1523,7 @@ static void userfaultfd_pagemap_test(unsigned int test_pgsize)err ("madvise(MADV_PAGEOUT) failed" );/* Uffd-wp should persist even swapped out */ - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, true);/* Make sure uffd-wp bit dropped when fork */ if (pagemap_test_fork (false))
@@ -1544,12 +1531,12 @@ static void userfaultfd_pagemap_test(unsigned int test_pgsize)/* Unprotect; this tests swap pte modifications */ wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Fault in the page from disk */ *area_dst =2 ; - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);close (pagemap_fd);
Signature · 1 line
2.39.1
Re: [PATCH 09/29] selftests/mm: Reuse pagemap_get_entry() in vm_util.h
Quoted 4 lines · Peter Xu
Meanwhile drop
pagemap_read_vaddr().Signed-off-by Peter Xu <[email protected]>
Quoted 102 lines
--- tools/testing/selftests/mm/userfaultfd.c | 31 +++++++----------------- 1 file changed, 9 insertions(+), 22 deletions(-)tools/testing/selftests/mm/userfaultfd.c +9
-23@@ -1399,19 +1399,6 @@ static int pagemap_open(void)return fd; } -static uint64_t pagemap_read_vaddr (int fd,void *vaddr) -{ -uint64_t value; -int ret; - - ret =pread (fd, &value,sizeof (uint64_t ), - ((uint64_t )vaddr >>12 ) *sizeof (uint64_t )); -if (ret !=sizeof (uint64_t )) -err ("pread() on pagemap failed" ); - -return value; -} -/* This macro let __LINE__ works in err() */ #define pagemap_check_wp (value, wp) do { \ if (!!(value & PM_UFFD_WP) != wp) \@@ -1427,7 +1414,7 @@ static int pagemap_test_fork(bool present)if (!child) {/* Open the pagemap fd of the child itself */ fd =pagemap_open (); - value =pagemap_read_vaddr (fd, area_dst); + value =pagemap_get_entry (fd, area_dst);/* * After fork() uffd-wp bit should be gone as long as we're * without UFFD_FEATURE_EVENT_FORK @@ -1446,24 +1433,24 @@ static void userfaultfd_wp_unpopulated_test(int pagemap_fd)/* Test applying pte marker to anon unpopulated */ wp_range (uffd, (uint64_t )area_dst, page_size, true); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, true);/* Test unprotect on anon pte marker */ wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Test zap on anon marker */ wp_range (uffd, (uint64_t )area_dst, page_size, true);if (madvise (area_dst, page_size,MADV_DONTNEED ))err ("madvise(MADV_DONTNEED) failed" ); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Test fault in after marker removed */ *area_dst =1 ; - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Drop it to make pte none again */ if (madvise (area_dst, page_size,MADV_DONTNEED ))@@ -1522,7 +1509,7 @@ static void userfaultfd_pagemap_test(unsigned int test_pgsize)/* Touch the page */ *area_dst =1 ;wp_range (uffd, (uint64_t )area_dst, test_pgsize, true); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, true);/* Make sure uffd-wp bit dropped when fork */ if (pagemap_test_fork (true))@@ -1536,7 +1523,7 @@ static void userfaultfd_pagemap_test(unsigned int test_pgsize)err ("madvise(MADV_PAGEOUT) failed" );/* Uffd-wp should persist even swapped out */ - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, true);/* Make sure uffd-wp bit dropped when fork */ if (pagemap_test_fork (false))@@ -1544,12 +1531,12 @@ static void userfaultfd_pagemap_test(unsigned int test_pgsize)/* Unprotect; this tests swap pte modifications */ wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Fault in the page from disk */ *area_dst =2 ; - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);close (pagemap_fd); --2.39.1
Re: [PATCH 09/29] selftests/mm: Reuse pagemap_get_entry() in vm_util.h
In reply to Peter XuQuoted 6 lines · Peter Xu
Meanwhile drop
pagemap_read_vaddr().Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/userfaultfd.c | 31 +++++++----------------- 1 file changed, 9 insertions(+), 22 deletions(-)
Thanks,
Signature · 1 line
Mike Kravetz
Re: [PATCH 09/29] selftests/mm: Reuse pagemap_get_entry() in vm_util.h
In reply to Peter XuQuoted 4 lines · Peter Xu
Meanwhile drop
pagemap_read_vaddr().Signed-off-by Peter Xu <[email protected]>---
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 09/29] selftests/mm: Reuse pagemap_get_entry() in vm_util.h
In reply to Peter XuQuoted 1 line · Peter Xu
Subject: selftests/mm: Reuse
pagemap_get_entry()invm_util.h
Nit: selftests/mm/uffd:
Quoted 3 lines
Meanwhile drop
pagemap_read_vaddr().Signed-off-by Peter Xu <[email protected]>
Quoted 102 lines
--- tools/testing/selftests/mm/userfaultfd.c | 31 +++++++----------------- 1 file changed, 9 insertions(+), 22 deletions(-)tools/testing/selftests/mm/userfaultfd.c +9
-22@@ -1399,19 +1399,6 @@ static int pagemap_open(void)return fd; } -static uint64_t pagemap_read_vaddr (int fd,void *vaddr) -{ -uint64_t value; -int ret; - - ret =pread (fd, &value,sizeof (uint64_t ), - ((uint64_t )vaddr >>12 ) *sizeof (uint64_t )); -if (ret !=sizeof (uint64_t )) -err ("pread() on pagemap failed" ); - -return value; -} -/* This macro let __LINE__ works in err() */ #define pagemap_check_wp (value, wp) do { \ if (!!(value & PM_UFFD_WP) != wp) \@@ -1427,7 +1414,7 @@ static int pagemap_test_fork(bool present)if (!child) {/* Open the pagemap fd of the child itself */ fd =pagemap_open (); - value =pagemap_read_vaddr (fd, area_dst); + value =pagemap_get_entry (fd, area_dst);/* * After fork() uffd-wp bit should be gone as long as we're * without UFFD_FEATURE_EVENT_FORK @@ -1446,24 +1433,24 @@ static void userfaultfd_wp_unpopulated_test(int pagemap_fd)/* Test applying pte marker to anon unpopulated */ wp_range (uffd, (uint64_t )area_dst, page_size, true); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, true);/* Test unprotect on anon pte marker */ wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Test zap on anon marker */ wp_range (uffd, (uint64_t )area_dst, page_size, true);if (madvise (area_dst, page_size,MADV_DONTNEED ))err ("madvise(MADV_DONTNEED) failed" ); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Test fault in after marker removed */ *area_dst =1 ; - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Drop it to make pte none again */ if (madvise (area_dst, page_size,MADV_DONTNEED ))@@ -1522,7 +1509,7 @@ static void userfaultfd_pagemap_test(unsigned int test_pgsize)/* Touch the page */ *area_dst =1 ;wp_range (uffd, (uint64_t )area_dst, test_pgsize, true); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, true);/* Make sure uffd-wp bit dropped when fork */ if (pagemap_test_fork (true))@@ -1536,7 +1523,7 @@ static void userfaultfd_pagemap_test(unsigned int test_pgsize)err ("madvise(MADV_PAGEOUT) failed" );/* Uffd-wp should persist even swapped out */ - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, true);/* Make sure uffd-wp bit dropped when fork */ if (pagemap_test_fork (false))@@ -1544,12 +1531,12 @@ static void userfaultfd_pagemap_test(unsigned int test_pgsize)/* Unprotect; this tests swap pte modifications */ wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);/* Fault in the page from disk */ *area_dst =2 ; - value =pagemap_read_vaddr (pagemap_fd, area_dst); + value =pagemap_get_entry (pagemap_fd, area_dst);pagemap_check_wp (value, false);close (pagemap_fd);Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 10/29 selftests/mm: Test UFFDIO_ZEROPAGE only when !hugetlb
In reply to Peter XuMake the check as simple as "test_type == TEST_HUGETLB" because that's the only mem that doesn't support ZEROPAGE.
--- tools/testing/selftests/mm/userfaultfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/userfaultfd.c +1 -1
@@ -1118,7 +1118,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) {struct uffdio_zeropage uffdio_zeropage;int ret; -bool has_zeropage =get_expected_ioctls (0 ) & (1 << _UFFDIO_ZEROPAGE); +bool has_zeropage = !(test_type ==TEST_HUGETLB );__s64 res;if (offset >= nr_pages * page_size)
Signature · 1 line
2.39.1
Re: [PATCH 10/29] selftests/mm: Test UFFDIO_ZEROPAGE only when !hugetlb
Quoted 13 lines · Peter Xu
Make the check as simple as "
test_type==TEST_HUGETLB" because that's the only mem that doesn't support ZEROPAGE.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/userfaultfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/userfaultfd.c +0
-0
tools/testing/selftests/mm/userfaultfd.c static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) · line 1121
Quoted 5 lines
tools/testing/selftests/mm/userfaultfd.c +1
-1@@ -1118,7 +1118,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) {struct uffdio_zeropage uffdio_zeropage;int ret; -bool has_zeropage =get_expected_ioctls (0 ) & (1 << _UFFDIO_ZEROPAGE); +bool has_zeropage = !(test_type ==TEST_HUGETLB );
It is true that hugetlb is the only mem type that does not support zeropage. So, the change is correct.
However, I actually prefer the explicit check that is there today. It seems more like a test of the API. And, is more future proof is code changes.
Just my opinion/thoughts, not a strong objection.
Signature · 1 line
Mike Kravetz
Re: [PATCH 10/29] selftests/mm: Test UFFDIO_ZEROPAGE only when !hugetlb
Quoted 24 lines · Mike Kravetz
Quoted 13 lines · Peter Xu
Make the check as simple as "
test_type==TEST_HUGETLB" because that's the only mem that doesn't support ZEROPAGE.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/userfaultfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/userfaultfd.c +0
-0tools/testing/selftests/mm/userfaultfd.c static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) · line 1121
Quoted 5 lines
tools/testing/selftests/mm/userfaultfd.c +1
-1@@ -1118,7 +1118,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) {struct uffdio_zeropage uffdio_zeropage;int ret; -bool has_zeropage =get_expected_ioctls (0 ) & (1 << _UFFDIO_ZEROPAGE); +bool has_zeropage = !(test_type ==TEST_HUGETLB );It is true that hugetlb is the only mem type that does not support zeropage. So, the change is correct.
Quoted 4 lines
However, I actually prefer the explicit check that is there today. It seems more like a test of the API. And, is more future proof is code changes.
Just my opinion/thoughts, not a strong objection.
I agree. The existing code is more robust to future changes where we might support or stop supporting this ioctl in some cases. It also proves that the ioctl works, any time the API reports that it is supported / ought to work, independent of when the *test* thinks it should be supported.
Then again, I think this is unlikely to change in the future, so I also agree with Mike that it's not the biggest deal.
Quoted 2 lines
--
Mike Kravetz
Re: [PATCH 10/29] selftests/mm: Test UFFDIO_ZEROPAGE only when !hugetlb
Quoted 30 lines · Axel Rasmussen
Quoted 24 lines · Mike Kravetz
Quoted 13 lines · Peter Xu
Make the check as simple as "
test_type==TEST_HUGETLB" because that's the only mem that doesn't support ZEROPAGE.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/userfaultfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/userfaultfd.c +0
-0tools/testing/selftests/mm/userfaultfd.c static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) · line 1121
Quoted 5 lines
tools/testing/selftests/mm/userfaultfd.c +1
-1@@ -1118,7 +1118,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) {struct uffdio_zeropage uffdio_zeropage;int ret; -bool has_zeropage =get_expected_ioctls (0 ) & (1 << _UFFDIO_ZEROPAGE); +bool has_zeropage = !(test_type ==TEST_HUGETLB );It is true that hugetlb is the only mem type that does not support zeropage. So, the change is correct.
Quoted 4 lines
However, I actually prefer the explicit check that is there today. It seems more like a test of the API. And, is more future proof is code changes.
Just my opinion/thoughts, not a strong objection.
Quoted 8 lines
I agree. The existing code is more robust to future changes where we might support or stop supporting this ioctl in some cases. It also proves that the ioctl works, any time the API reports that it is supported / ought to work, independent of when the *test* thinks it should be supported.
Then again, I think this is unlikely to change in the future, so I also agree with Mike that it's not the biggest deal.
As there were already discussions on eventually supporting UFFDIO_ZEROPAGE that doesn't place the shared zeropage but ... a fresh zeropage, it might make sense to keep it as is.
Signature · 3 lines
Thanks, David / dhildenb
Peter Xu
reviewerRe: [PATCH 10/29] selftests/mm: Test UFFDIO_ZEROPAGE only when !hugetlb
Quoted 37 lines · David Hildenbrand
Quoted 36 lines · Axel Rasmussen
Quoted 24 lines · Mike Kravetz
Quoted 13 lines · Peter Xu
Make the check as simple as "
test_type==TEST_HUGETLB" because that's the only mem that doesn't support ZEROPAGE.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/userfaultfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/userfaultfd.c +0
-0tools/testing/selftests/mm/userfaultfd.c static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) · line 1121
Quoted 5 lines
tools/testing/selftests/mm/userfaultfd.c +1
-1@@ -1118,7 +1118,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) {struct uffdio_zeropage uffdio_zeropage;int ret; -bool has_zeropage =get_expected_ioctls (0 ) & (1 << _UFFDIO_ZEROPAGE); +bool has_zeropage = !(test_type ==TEST_HUGETLB );It is true that hugetlb is the only mem type that does not support zeropage. So, the change is correct.
Quoted 4 lines
However, I actually prefer the explicit check that is there today. It seems more like a test of the API. And, is more future proof is code changes.
Just my opinion/thoughts, not a strong objection.
I agree. The existing code is more robust to future changes where we might support or stop supporting this ioctl in some cases. It also proves that the ioctl works, any time the API reports that it is supported / ought to work, independent of when the *test* thinks it should be supported.
Quoted 6 lines
Quoted 2 lines
Then again, I think this is unlikely to change in the future, so I also agree with Mike that it's not the biggest deal.
As there were already discussions on eventually supporting
UFFDIO_ZEROPAGEthat doesn't place the shared zeropage but ... a fresh zeropage, it might make sense to keep it as is.
Thanks everyone.
So here the major goal is to drop get_expected_ioctls(), and I think it's really unwanted here. Besides it's a blocker for split the test in a clean way, a major reason is get_expected_ioctls() fetches "wheter we support zeropage for this mem" from UFFD_API_RANGE_IOCTLS, rather than from the UFFDIO_REGISTER anyway:
uint64_t get_expected_ioctls(uint64_t mode)
{
uint64_t ioctls = UFFD_API_RANGE_IOCTLS;
if (test_type == TEST_HUGETLB)
ioctls &= ~(1 << _UFFDIO_ZEROPAGE);
if (!((mode & UFFDIO_REGISTER_MODE_WP) && test_uffdio_wp))
ioctls &= ~(1 << _UFFDIO_WRITEPROTECT);
if (!((mode & UFFDIO_REGISTER_MODE_MINOR) && test_uffdio_minor))
ioctls &= ~(1 << _UFFDIO_CONTINUE);
return ioctls;
}It means it'll succeed or fail depending on what kernel we run this test on, and also on what headers we compile the test against.
I actually mentioned some of the reasoning in a follow up patch (sorry maybe the split here caused some confusion):
selftests/mm: uffd_[un]register()
Add two helpers to register/unregister to an uffd. Use them to drop duplicate codes.
This patch also drops assert_expected_ioctls_present() and get_expected_ioctls(). Reasons:
- It'll need a lot of effort to pass test_type==HUGETLB into it from the upper, so it's the simplest way to get rid of another global var
- The ioctls returned in UFFDIO_REGISTER is hardly useful at all, because any app can already detect kernel support on any ioctl via its corresponding UFFD_FEATURE_*. The check here is for sanity mostly but it's probably destined no user app will even use it.
- It's not friendly to one future goal of uffd to run on old kernels, the problem is get_expected_ioctls() compiles against UFFD_API_RANGE_IOCTLS, which is a value that can change depending on where the test is compiled, rather than reflecting what the kernel underneath has. It means it'll report false negatives on old kernels so it's against our will.
So let's make our live easier.
But I do agree that it's helpful to keep a test against uffdio_register.ioctls in this case against _UFFDIO_ZEROPAGE, so it can be detected dynamically. IOW, even if we would like to avoid "test != HUGETLB" here, at least we should like to fix that with the UFFDIO_REGISTER results.
Here's my offer below. :)
Could I keep this patch as-is (as part of getting rid of get_expected_ioctls() effort; I can squash this one into "selftests/mm:
uffd_[un]register()" if any of you think proper), meanwhile I'll squash a fixup to the "move zeropage test into uffd-unit-tests" explicitly check uffdio_register.ioctls in the same patchset? IOW, we'll have a few test commits missing this specific ioctl test, but then we'll have a better one dynamically detected from the kernel.
The fixup patch attached. I think it'll automatically work when someone would like to introduce UFFDIO_ZEROPAGE to hugetlb too, another side benefit is I merged the zeropage test into one, which does look better too.
Thanks,
Signature · 1 line
Peter Xu
From 5b06f921cf8420600c697a3072a1459a5cb4956b Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Mon, 3 Apr 2023 11:57:07 -0400
Subject: [PATCH] fixup! selftests/mm: Move zeropage test into uffd unit tests
--- tools/testing/selftests/mm/uffd-unit-tests.c | 62 +++++++++++--------- 1 file changed, 33 insertions(+), 29 deletions(-)
tools/testing/selftests/mm/uffd-unit-tests.c +33 -29
@@ -711,54 +711,58 @@ static bool do_uffdio_zeropage(int ufd, bool has_zeropage)return false; } +/* +* Registers a range with MISSING mode only for zeropage test. Return true +* if UFFDIO_ZEROPAGE supported, false otherwise. Can't use uffd_register() +* because we want to detect .ioctls along the way. +*/ +static bool +uffd_register_detect_zp (int uffd,void *addr,uint64_t len) +{ +struct uffdio_register uffdio_register = {0 }; +uint64_t mode =UFFDIO_REGISTER_MODE_MISSING ; + + uffdio_register.range.start = (unsigned long )addr; + uffdio_register.range.len = len; + uffdio_register.mode = mode; + +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) ==-1 ) +err ("zeropage test register fail" ); + +return uffdio_register.ioctls & (1 << _UFFDIO_ZEROPAGE); +} + +/* exercise UFFDIO_ZEROPAGE */ -static void uffd_zeropage_test_common (bool has_zeropage) +static void uffd_zeropage_test (void ) { -if (uffd_register (uffd, area_dst, page_size, - true, false, false)) -err ("register" ); +bool has_zeropage; +int i; + has_zeropage =uffd_register_detect_zp (uffd, area_dst, page_size);if (area_dst_alias) -if (uffd_register (uffd, area_dst_alias, page_size, - true, false, false)) -err ("register" ); - -if (do_uffdio_zeropage (uffd, has_zeropage)) { -int i; +/* Ignore the retval; we already have it */ +uffd_register_detect_zp (uffd, area_dst_alias, page_size); +if (do_uffdio_zeropage (uffd, has_zeropage))for (i =0 ; i < page_size; i++)if (area_dst[i] !=0 )err ("data non-zero at offset %d\n" , i); - } +if (uffd_unregister (uffd, area_dst, page_size)) +err ("unregister" ); -if (uffd_unregister (uffd, area_dst, page_size * nr_pages)) +if (area_dst_alias &&uffd_unregister (uffd, area_dst_alias, page_size))err ("unregister" );uffd_test_pass (); } -static void uffd_zeropage_test (void ) -{ -uffd_zeropage_test_common (true); -} - -static void uffd_zeropage_hugetlb_test (void ) -{ -uffd_zeropage_test_common (false); -} -uffd_test_case_t uffd_tests[] = { { .name ="zeropage" , .uffd_fn = uffd_zeropage_test, - .mem_targets =MEM_ANON |MEM_SHMEM |MEM_SHMEM_PRIVATE , - .uffd_feature_required =0 , - }, - { - .name ="zeropage-hugetlb" , - .uffd_fn = uffd_zeropage_hugetlb_test, - .mem_targets =MEM_HUGETLB |MEM_HUGETLB_PRIVATE , + .mem_targets =MEM_ALL , .uffd_feature_required =0 , }, {
Signature · 1 line
2.39.1
Re: [PATCH 10/29] selftests/mm: Test UFFDIO_ZEROPAGE only when !hugetlb
Quoted 115 lines · Peter Xu
Quoted 37 lines · David Hildenbrand
Quoted 36 lines · Axel Rasmussen
Quoted 24 lines · Mike Kravetz
Quoted 13 lines · Peter Xu
Make the check as simple as "
test_type==TEST_HUGETLB" because that's the only mem that doesn't support ZEROPAGE.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/userfaultfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/userfaultfd.c +0
-0tools/testing/selftests/mm/userfaultfd.c static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) · line 1121
Quoted 5 lines
tools/testing/selftests/mm/userfaultfd.c +1
-1@@ -1118,7 +1118,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry) {struct uffdio_zeropage uffdio_zeropage;int ret; -bool has_zeropage =get_expected_ioctls (0 ) & (1 << _UFFDIO_ZEROPAGE); +bool has_zeropage = !(test_type ==TEST_HUGETLB );It is true that hugetlb is the only mem type that does not support zeropage. So, the change is correct.
Quoted 4 lines
However, I actually prefer the explicit check that is there today. It seems more like a test of the API. And, is more future proof is code changes.
Just my opinion/thoughts, not a strong objection.
I agree. The existing code is more robust to future changes where we might support or stop supporting this ioctl in some cases. It also proves that the ioctl works, any time the API reports that it is supported / ought to work, independent of when the *test* thinks it should be supported.
Quoted 6 lines
Quoted 2 lines
Then again, I think this is unlikely to change in the future, so I also agree with Mike that it's not the biggest deal.
As there were already discussions on eventually supporting
UFFDIO_ZEROPAGEthat doesn't place the shared zeropage but ... a fresh zeropage, it might make sense to keep it as is.Thanks everyone.
So here the major goal is to drop
get_expected_ioctls(), and I think it's really unwanted here. Besides it's a blocker for split the test in a clean way, a major reason isget_expected_ioctls()fetches "wheter we support zeropage for this mem" fromUFFD_API_RANGE_IOCTLS, rather than from theUFFDIO_REGISTERanyway:
uint64_tget_expected_ioctls(uint64_t mode){ uint64_t ioctls = UFFD_API_RANGE_IOCTLS; if (test_type == TEST_HUGETLB) ioctls &= ~(1 << _UFFDIO_ZEROPAGE); if (!((mode & UFFDIO_REGISTER_MODE_WP) && test_uffdio_wp)) ioctls &= ~(1 << _UFFDIO_WRITEPROTECT); if (!((mode & UFFDIO_REGISTER_MODE_MINOR) && test_uffdio_minor)) ioctls &= ~(1 << _UFFDIO_CONTINUE); return ioctls; }It means it'll succeed or fail depending on what kernel we run this test on, and also on what headers we compile the test against.
I actually mentioned some of the reasoning in a follow up patch (sorry maybe the split here caused some confusion):
selftests/mm: uffd_[un]register()
Add two helpers to register/unregister to an uffd. Use them to drop duplicate codes.
This patch also drops
assert_expected_ioctls_present()andget_expected_ioctls(). Reasons:- It'll need a lot of effort to pass test_type==HUGETLB into it from the upper, so it's the simplest way to get rid of another global var
- The ioctls returned in
UFFDIO_REGISTERis hardly useful at all, because any app can already detect kernel support on any ioctl via its corresponding UFFD_FEATURE_*. The check here is for sanity mostly but it's probably destined no user app will even use it.- It's not friendly to one future goal of uffd to run on old kernels, the problem is
get_expected_ioctls()compiles againstUFFD_API_RANGE_IOCTLS, which is a value that can change depending on where the test is compiled, rather than reflecting what the kernel underneath has. It means it'll report false negatives on old kernels so it's against our will.So let's make our live easier.
But I do agree that it's helpful to keep a test against uffdio_register.ioctls in this case against
_UFFDIO_ZEROPAGE, so it can be detected dynamically. IOW, even if we would like to avoid "test != HUGETLB" here, at least we should like to fix that with theUFFDIO_REGISTERresults.Here's my offer below. :)
Could I keep this patch as-is (as part of getting rid of
get_expected_ioctls()effort; I can squash this one into "selftests/mm:
uffd_[un]register()" if any of you think proper), meanwhile I'll squash a fixup to the "move zeropage test into uffd-unit-tests" explicitly check uffdio_register.ioctls in the same patchset? IOW, we'll have a few test commits missing this specific ioctl test, but then we'll have a better one dynamically detected from the kernel.
Quoted 3 lines
The fixup patch attached. I think it'll automatically work when someone would like to introduce
UFFDIO_ZEROPAGEto hugetlb too, another side benefit is I merged the zeropage test into one, which does look better too.
I agree that it makes sense. A nit below :)
Quoted 20 lines
Thanks,
Signature · 11 lines
Peter Xu From 5b06f921cf8420600c697a3072a1459a5cb4956b Mon Sep 17 00:00:00 2001 From: Peter Xu <[email protected]> Date: Mon, 3 Apr 2023 11:57:07 -0400 Subject: [PATCH] fixup! selftests/mm: Move zeropage test into uffd unit tests Signed-off-by: Peter Xu <[email protected]> --- tools/testing/selftests/mm/uffd-unit-tests.c | 62 +++++++++++--------- 1 file changed, 33 insertions(+), 29 deletions(-)tools/testing/selftests/mm/uffd-unit-tests.c +0
-0
tools/testing/selftests/mm/uffd-unit-tests.c static bool do_uffdio_zeropage(int ufd, bool has_zeropage) · line 720
Quoted 10 lines
tools/testing/selftests/mm/uffd-unit-tests.c +7
-0@@ -711,54 +711,58 @@ static bool do_uffdio_zeropage(int ufd, bool has_zeropage)return false; } +/* +* Registers a range with MISSING mode only for zeropage test. Return true +* if UFFDIO_ZEROPAGE supported, false otherwise. Can't use uffd_register() +* because we want to detect .ioctls along the way. +*/ +static bool +uffd_register_detect_zp (int uffd,void *addr,uint64_t len)
Let's spell out 'zp' as zeropage, what do you say?
Quoted 80 lines
tools/testing/selftests/mm/uffd-unit-tests.c +26
-29+{ +struct uffdio_register uffdio_register = {0 }; +uint64_t mode =UFFDIO_REGISTER_MODE_MISSING ; + + uffdio_register.range.start = (unsigned long )addr; + uffdio_register.range.len = len; + uffdio_register.mode = mode; + +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) ==-1 ) +err ("zeropage test register fail" ); + +return uffdio_register.ioctls & (1 << _UFFDIO_ZEROPAGE); +} + +/* exercise UFFDIO_ZEROPAGE */ -static void uffd_zeropage_test_common (bool has_zeropage) +static void uffd_zeropage_test (void ) { -if (uffd_register (uffd, area_dst, page_size, - true, false, false)) -err ("register" ); +bool has_zeropage; +int i; + has_zeropage =uffd_register_detect_zp (uffd, area_dst, page_size);if (area_dst_alias) -if (uffd_register (uffd, area_dst_alias, page_size, - true, false, false)) -err ("register" ); - -if (do_uffdio_zeropage (uffd, has_zeropage)) { -int i; +/* Ignore the retval; we already have it */ +uffd_register_detect_zp (uffd, area_dst_alias, page_size); +if (do_uffdio_zeropage (uffd, has_zeropage))for (i =0 ; i < page_size; i++)if (area_dst[i] !=0 )err ("data non-zero at offset %d\n" , i); - } +if (uffd_unregister (uffd, area_dst, page_size)) +err ("unregister" ); -if (uffd_unregister (uffd, area_dst, page_size * nr_pages)) +if (area_dst_alias &&uffd_unregister (uffd, area_dst_alias, page_size))err ("unregister" );uffd_test_pass (); } -static void uffd_zeropage_test (void ) -{ -uffd_zeropage_test_common (true); -} - -static void uffd_zeropage_hugetlb_test (void ) -{ -uffd_zeropage_test_common (false); -} -uffd_test_case_t uffd_tests[] = { { .name ="zeropage" , .uffd_fn = uffd_zeropage_test, - .mem_targets =MEM_ANON |MEM_SHMEM |MEM_SHMEM_PRIVATE , - .uffd_feature_required =0 , - }, - { - .name ="zeropage-hugetlb" , - .uffd_fn = uffd_zeropage_hugetlb_test, - .mem_targets =MEM_HUGETLB |MEM_HUGETLB_PRIVATE , + .mem_targets =MEM_ALL , .uffd_feature_required =0 , }, {Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerRe: [PATCH 10/29] selftests/mm: Test UFFDIO_ZEROPAGE only when !hugetlb
Quoted 9 lines · Mike Rapoport
tools/testing/selftests/mm/uffd-unit-tests.c static bool do_uffdio_zeropage(int ufd, bool has_zeropage) · line 720
Quoted 7 lines
tools/testing/selftests/mm/uffd-unit-tests.c +7
-0+/* +* Registers a range with MISSING mode only for zeropage test. Return true +* if UFFDIO_ZEROPAGE supported, false otherwise. Can't use uffd_register() +* because we want to detect .ioctls along the way. +*/ +static bool +uffd_register_detect_zp (int uffd,void *addr,uint64_t len)Let's spell out 'zp' as zeropage, what do you say?
Definitely can do. :)
Thanks,
Signature · 1 line
Peter Xu
Peter Xu
reviewerPATCH 11/29 selftests/mm: Drop test_uffdio_zeropage_eexist
In reply to Peter XuThe idea was trying to flip this var in the alarm handler from time to time to test -EEXIST of UFFDIO_ZEROPAGE, but firstly it's only used in the zeropage test so probably only used once, meanwhile we passed "retry==false" so it'll never got tested anyway.
Drop both sides so we always test UFFDIO_ZEROPAGE retries if has_zeropage is set (!hugetlb).
One more thing to do is doing UFFDIO_REGISTER for the alias buffer too, because otherwise the test won't even pass! We were just lucky that this test never really got ran at all.
--- tools/testing/selftests/mm/userfaultfd.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
tools/testing/selftests/mm/userfaultfd.c +11 -9
@@ -88,7 +88,6 @@ static bool test_dev_userfaultfd;/* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */ #define ALARM_INTERVAL_SECS 10static volatile bool test_uffdio_copy_eexist = true; -static volatile bool test_uffdio_zeropage_eexist = true;/* Whether to test uffd write-protection */ static bool test_uffdio_wp = true;/* Whether to test uffd minor faults */
@@ -1114,7 +1113,7 @@ static void retry_uffdio_zeropage(int ufd, } } -static int __uffdio_zeropage (int ufd,unsigned long offset,bool retry) +static int __uffdio_zeropage (int ufd,unsigned long offset) {struct uffdio_zeropage uffdio_zeropage;int ret;
@@ -1138,11 +1137,8 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)if (res != page_size) {err ("UFFDIO_ZEROPAGE unexpected size" ); }else { -if (test_uffdio_zeropage_eexist && retry) { - test_uffdio_zeropage_eexist = false; -retry_uffdio_zeropage (ufd, &uffdio_zeropage, - offset); - } +retry_uffdio_zeropage (ufd, &uffdio_zeropage, + offset);return 1 ; } }else
@@ -1153,7 +1149,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)static int uffdio_zeropage (int ufd,unsigned long offset) { -return __uffdio_zeropage (ufd, offset, false); +return __uffdio_zeropage (ufd, offset); }/* exercise UFFDIO_ZEROPAGE */
@@ -1177,6 +1173,13 @@ static int userfaultfd_zeropage_test(void)assert_expected_ioctls_present ( uffdio_register.mode, uffdio_register.ioctls); +if (area_dst_alias) { +/* Needed this to test zeropage-retry on shared memory */ + uffdio_register.range.start = (unsigned long ) area_dst_alias; +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) +err ("register failure" ); + } +if (uffdio_zeropage (uffd,0 ))if (my_bcmp (area_dst, zeropage, page_size))err ("zeropage is not zero" );
@@ -1763,7 +1766,6 @@ static void sigalrm(int sig)if (sig !=SIGALRM )abort (); test_uffdio_copy_eexist = true; - test_uffdio_zeropage_eexist = true;alarm (ALARM_INTERVAL_SECS ); }
Signature · 1 line
2.39.1
Re: [PATCH 11/29] selftests/mm: Drop test_uffdio_zeropage_eexist
Quoted 8 lines · Peter Xu
The idea was trying to flip this var in the alarm handler from time to time to test
-EEXISTofUFFDIO_ZEROPAGE, but firstly it's only used in the zeropage test so probably only used once, meanwhile we passed "retry==false" so it'll never got tested anyway.Drop both sides so we always test
UFFDIO_ZEROPAGEretries ifhas_zeropageis set (!hugetlb).
Quoted 8 lines
One more thing to do is doing
UFFDIO_REGISTERfor the alias buffer too, because otherwise the test won't even pass! We were just lucky that this test never really got ran at all.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/userfaultfd.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
Thanks! With only passing "retry==false", that code is indeed dead. I had to read the code again to understand area_dst_alias. Thanks for taking this on as the code is difficult to understand.
Signature · 1 line
Mike Kravetz
Peter Xu
reviewerRe: [PATCH 11/29] selftests/mm: Drop test_uffdio_zeropage_eexist
Quoted 13 lines · Mike Kravetz
Quoted 12 lines · Peter Xu
The idea was trying to flip this var in the alarm handler from time to time to test
-EEXISTofUFFDIO_ZEROPAGE, but firstly it's only used in the zeropage test so probably only used once, meanwhile we passed "retry==false" so it'll never got tested anyway.Drop both sides so we always test
UFFDIO_ZEROPAGEretries ifhas_zeropageis set (!hugetlb).One more thing to do is doing
UFFDIO_REGISTERfor the alias buffer too, because otherwise the test won't even pass! We were just lucky that this test never really got ran at all.
Quoted 8 lines
Quoted 4 lines
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/userfaultfd.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)Thanks! With only passing "retry==false", that code is indeed dead. I had to read the code again to understand
area_dst_alias. Thanks for taking this on as the code is difficult to understand.
Yes it's very confusing. I plan to move on the cleanup to remove all these global variables at least in the unit tests, but I'll first see how this one goes.
Quoted 2 lines
Reviewed-by Mike Kravetz <[email protected]>
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 11/29] selftests/mm: Drop test_uffdio_zeropage_eexist
In reply to Peter XuQuoted 8 lines · Peter Xu
The idea was trying to flip this var in the alarm handler from time to time to test
-EEXISTofUFFDIO_ZEROPAGE, but firstly it's only used in the zeropage test so probably only used once, meanwhile we passed "retry==false" so it'll never got tested anyway.Drop both sides so we always test
UFFDIO_ZEROPAGEretries ifhas_zeropageis set (!hugetlb).
Quoted 6 lines
One more thing to do is doing
UFFDIO_REGISTERfor the alias buffer too, because otherwise the test won't even pass! We were just lucky that this test never really got ran at all.Signed-off-by Peter Xu <[email protected]>---
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 11/29] selftests/mm: Drop test_uffdio_zeropage_eexist
In reply to Peter XuQuoted 5 lines · Peter Xu
The idea was trying to flip this var in the alarm handler from time to time to test
-EEXISTofUFFDIO_ZEROPAGE, but firstly it's only used in the zeropage test so probably only used once, meanwhile we passed "retry==false" so it'll never got tested anyway.
Quoted 8 lines
Drop both sides so we always test
UFFDIO_ZEROPAGEretries ifhas_zeropageis set (!hugetlb).One more thing to do is doing
UFFDIO_REGISTERfor the alias buffer too, because otherwise the test won't even pass! We were just lucky that this test never really got ran at all.Signed-off-by Peter Xu <[email protected]>
Quoted 73 lines
--- tools/testing/selftests/mm/userfaultfd.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)tools/testing/selftests/mm/userfaultfd.c +11
-9@@ -88,7 +88,6 @@ static bool test_dev_userfaultfd;/* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */ #define ALARM_INTERVAL_SECS 10static volatile bool test_uffdio_copy_eexist = true; -static volatile bool test_uffdio_zeropage_eexist = true;/* Whether to test uffd write-protection */ static bool test_uffdio_wp = true;/* Whether to test uffd minor faults */ @@ -1114,7 +1113,7 @@ static void retry_uffdio_zeropage(int ufd, } } -static int __uffdio_zeropage (int ufd,unsigned long offset,bool retry) +static int __uffdio_zeropage (int ufd,unsigned long offset) {struct uffdio_zeropage uffdio_zeropage;int ret;@@ -1138,11 +1137,8 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)if (res != page_size) {err ("UFFDIO_ZEROPAGE unexpected size" ); }else { -if (test_uffdio_zeropage_eexist && retry) { - test_uffdio_zeropage_eexist = false; -retry_uffdio_zeropage (ufd, &uffdio_zeropage, - offset); - } +retry_uffdio_zeropage (ufd, &uffdio_zeropage, + offset);return 1 ; } }else @@ -1153,7 +1149,7 @@ static int __uffdio_zeropage(int ufd, unsigned long offset, bool retry)static int uffdio_zeropage (int ufd,unsigned long offset) { -return __uffdio_zeropage (ufd, offset, false); +return __uffdio_zeropage (ufd, offset); }/* exercise UFFDIO_ZEROPAGE */ @@ -1177,6 +1173,13 @@ static int userfaultfd_zeropage_test(void)assert_expected_ioctls_present ( uffdio_register.mode, uffdio_register.ioctls); +if (area_dst_alias) { +/* Needed this to test zeropage-retry on shared memory */ + uffdio_register.range.start = (unsigned long ) area_dst_alias; +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register)) +err ("register failure" ); + } +if (uffdio_zeropage (uffd,0 ))if (my_bcmp (area_dst, zeropage, page_size))err ("zeropage is not zero" );@@ -1763,7 +1766,6 @@ static void sigalrm(int sig)if (sig !=SIGALRM )abort (); test_uffdio_copy_eexist = true; - test_uffdio_zeropage_eexist = true;alarm (ALARM_INTERVAL_SECS ); }Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 12/29 selftests/mm: Create uffd-common.[ch]
In reply to Peter XuMove common utility functions into uffd-common.[ch] files from the original userfaultfd.c. This prepares for a split of userfaultfd.c into two tests:
one to only cover the old but powerful stress test, the other one covers all the functional tests.
This movement is kind of a brute-force effort for now, with light touch-ups but nothing should really change. There's chances to optimize more, but let's leave that for later.
--- tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/uffd-common.c | 611 ++++++++++++++++++++ tools/testing/selftests/mm/uffd-common.h | 117 ++++ tools/testing/selftests/mm/userfaultfd.c | 694 +---------------------- 4 files changed, 731 insertions(+), 693 deletions(-) create mode 100644 tools/testing/selftests/mm/uffd-common.c create mode 100644 tools/testing/selftests/mm/uffd-common.h
tools/testing/selftests/mm/Makefile +2 -0
@@ -107,6 +107,8 @@ include ../lib.mk $(TEST_GEN_PROGS): vm_util.c +$(OUTPUT)/userfaultfd: uffd-common.c + ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32)) BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))
tools/testing/selftests/mm/uffd-common.c new file +611 -0
611 lines not on this page · raw message
tools/testing/selftests/mm/uffd-common.h new file +117 -0
117 lines not on this page · raw message
tools/testing/selftests/mm/userfaultfd.c +1 -693
748 lines not on this page · raw message
Signature · 1 line
2.39.1
Re: [PATCH 12/29] selftests/mm: Create uffd-common.[ch]
Quoted 10 lines · Peter Xu
Move common utility functions into uffd-common.[ch] files from the original
userfaultfd.c. This prepares for a split ofuserfaultfd.cinto two tests:
one to only cover the old but powerful stress test, the other one covers all the functional tests.This movement is kind of a brute-force effort for now, with light touch-ups but nothing should really change. There's chances to optimize more, but let's leave that for later.
Signed-off-by Peter Xu <[email protected]>
Quoted 9 lines
--- tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/uffd-common.c | 611 ++++++++++++++++++++ tools/testing/selftests/mm/uffd-common.h | 117 ++++ tools/testing/selftests/mm/userfaultfd.c | 694 +---------------------- 4 files changed, 731 insertions(+), 693 deletions(-) create mode 100644 tools/testing/selftests/mm/uffd-common.c create mode 100644 tools/testing/selftests/mm/uffd-common.h
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 13/29 selftests/mm: Split uffd tests into uffd-stress and uffd-unit-tests
In reply to Peter XuIn many ways it's weird and unwanted to keep all the tests in the same userfaultfd.c at least when still in the current way.
For example, it doesn't make much sense to run the stress test for each method we can create an userfaultfd handle (either via syscall or /dev/ node). It's a waste of time running this twice for the whole stress as the stress paths are the same, only the open path is different.
It's also just weird to need to manually specify different types of memory to run all unit tests for the userfaultfd interface. We should be able to just run a single program and that should go through all functional uffd tests without running the stress test at all. The stress test was more for torturing and finding race conditions. We don't want to wait for stress to finish just to regress test a functional test.
When we start to pile up more things on top of the same file and same functions, things start to go a bit chaos and the code is just harder to maintain too with tons of global variables.
This patch creates a new test uffd-unit-tests to keep userfaultfd unit tests in the future, currently empty.
Meanwhile rename the old userfaultfd.c test to uffd-stress.c.
---
tools/testing/selftests/mm/.gitignore | 3 ++-
tools/testing/selftests/mm/Makefile | 8 +++---
tools/testing/selftests/mm/run_vmtests.sh | 10 ++++---
.../mm/{userfaultfd.c => uffd-stress.c} | 0
tools/testing/selftests/mm/uffd-unit-tests.c | 27 +++++++++++++++++++
5 files changed, 40 insertions(+), 8 deletions(-)
rename tools/testing/selftests/mm/{userfaultfd.c => uffd-stress.c} (100%)
create mode 100644 tools/testing/selftests/mm/uffd-unit-tests.ctools/testing/selftests/mm/.gitignore +2 -1
@@ -21,7 +21,8 @@ protection_keys protection_keys_32 protection_keys_64 madv_populate -userfaultfd +uffd-stress +uffd-unit-tests mlock-intersect-test mlock-random-test virtual_address_range
tools/testing/selftests/mm/Makefile +5 -3
@@ -20,7 +20,7 @@ MACHINE ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/' -e 's/ppc64.*/p # Avoid accidental wrong builds, due to built-in rules working just a little # bit too well--but not quite as well as required for our situation here. # -# In other words, "make userfaultfd" is supposed to fail to build at all, +# In other words, "make $SOME_TEST" is supposed to fail to build at all, # because this Makefile only supports either "make" (all), or "make /full/path". # However, the built-in rules, if not suppressed, will pick up CFLAGS and the # initial LDLIBS (but not the target-specific LDLIBS, because those are only
@@ -56,7 +56,8 @@ TEST_GEN_PROGS += mremap_test TEST_GEN_PROGS += on-fault-limit TEST_GEN_PROGS += thuge-gen TEST_GEN_PROGS += transhuge-stress -TEST_GEN_PROGS += userfaultfd +TEST_GEN_PROGS += uffd-stress +TEST_GEN_PROGS += uffd-unit-tests TEST_GEN_PROGS += soft-dirty TEST_GEN_PROGS += split_huge_page_test TEST_GEN_PROGS += ksm_tests
@@ -107,7 +108,8 @@ include ../lib.mk $(TEST_GEN_PROGS): vm_util.c -$(OUTPUT)/userfaultfd: uffd-common.c +$(OUTPUT)/uffd-stress: uffd-common.c +$(OUTPUT)/uffd-unit-tests: uffd-common.c ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
tools/testing/selftests/mm/run_vmtests.sh +6 -4
@@ -196,14 +196,16 @@ CATEGORY="gup_test" run_test ./gup_test -a # Dump pages 0, 19, and 4096, using pin_user_pages: CATEGORY="gup_test" run_test ./gup_test -ct -F 0x1 0 19 0x1000 +CATEGORY="userfaultfd" run_test ./uffd-unit-tests uffd_mods=("" ":dev") +uffd_stress_bin=./uffd-stress for mod in "${uffd_mods[@]}"; do - CATEGORY="userfaultfd" run_test ./userfaultfd anon${mod} 20 16 + CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon${mod} 20 16 # Hugetlb tests require source and destination huge pages. Pass in half # the size ($half_ufd_size_MB), which is used for *each*. - CATEGORY="userfaultfd" run_test ./userfaultfd hugetlb${mod} "$half_ufd_size_MB" 32 - CATEGORY="userfaultfd" run_test ./userfaultfd hugetlb_shared${mod} "$half_ufd_size_MB" 32 - CATEGORY="userfaultfd" run_test ./userfaultfd shmem${mod} 20 16 + CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb${mod} "$half_ufd_size_MB" 32 + CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb_shared${mod} "$half_ufd_size_MB" 32 + CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem${mod} 20 16 done #cleanup
tools/testing/selftests/mm/uffd-stress.c renamed from tools/testing/selftests/mm/userfaultfd.c +0 -0
tools/testing/selftests/mm/uffd-unit-tests.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Userfaultfd unit tests. +* +* Copyright (C) 2015-2023 Red Hat, Inc. +*/ + +#include "uffd-common.h" + +#ifdef __NR_userfaultfd + +int main (int argc,char *argv[]) +{ +return KSFT_PASS ; +} + +#else /* __NR_userfaultfd */ + +#warning "missing __NR_userfaultfd definition" + +int main (void ) +{ +printf ("Skipping %s (missing __NR_userfaultfd)\n" , __file__); +return KSFT_SKIP ; +} + +#endif /* __NR_userfaultfd */
Signature · 1 line
2.39.1
Re: [PATCH 13/29] selftests/mm: Split uffd tests into uffd-stress and uffd-unit-tests
Quoted 19 lines · Peter Xu
In many ways it's weird and unwanted to keep all the tests in the same
userfaultfd.cat least when still in the current way.For example, it doesn't make much sense to run the stress test for each method we can create an userfaultfd handle (either via syscall or
/dev/node). It's a waste of time running this twice for the whole stress as the stress paths are the same, only the open path is different.It's also just weird to need to manually specify different types of memory to run all unit tests for the userfaultfd interface. We should be able to just run a single program and that should go through all functional uffd tests without running the stress test at all. The stress test was more for torturing and finding race conditions. We don't want to wait for stress to finish just to regress test a functional test.
When we start to pile up more things on top of the same file and same functions, things start to go a bit chaos and the code is just harder to maintain too with tons of global variables.
Quoted 6 lines
This patch creates a new test uffd-unit-tests to keep userfaultfd unit tests in the future, currently empty.
Meanwhile rename the old
userfaultfd.ctest touffd-stress.c.Signed-off-by Peter Xu <[email protected]>
Quoted 122 lines
--- tools/testing/selftests/mm/.gitignore | 3 ++- tools/testing/selftests/mm/Makefile | 8 +++--- tools/testing/selftests/mm/run_vmtests.sh | 10 ++++--- .../mm/{userfaultfd.c => uffd-stress.c} | 0 tools/testing/selftests/mm/uffd-unit-tests.c | 27 +++++++++++++++++++ 5 files changed, 40 insertions(+), 8 deletions(-) rename tools/testing/selftests/mm/{userfaultfd.c => uffd-stress.c} (100%) create mode 100644 tools/testing/selftests/mm/uffd-unit-tests.ctools/testing/selftests/mm/.gitignore +2
-1@@ -21,7 +21,8 @@ protection_keys protection_keys_32 protection_keys_64 madv_populate -userfaultfd +uffd-stress +uffd-unit-tests mlock-intersect-test mlock-random-test virtual_address_rangetools/testing/selftests/mm/Makefile +5
-3@@ -20,7 +20,7 @@ MACHINE ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/' -e 's/ppc64.*/p # Avoid accidental wrong builds, due to built-in rules working just a little # bit too well--but not quite as well as required for our situation here. # -# In other words, "make userfaultfd" is supposed to fail to build at all, +# In other words, "make $SOME_TEST" is supposed to fail to build at all, # because this Makefile only supports either "make" (all), or "make /full/path". # However, the built-in rules, if not suppressed, will pick up CFLAGS and the # initial LDLIBS (but not the target-specific LDLIBS, because those are only@@ -56,7 +56,8 @@ TEST_GEN_PROGS += mremap_test TEST_GEN_PROGS += on-fault-limit TEST_GEN_PROGS += thuge-gen TEST_GEN_PROGS += transhuge-stress -TEST_GEN_PROGS += userfaultfd +TEST_GEN_PROGS += uffd-stress +TEST_GEN_PROGS += uffd-unit-tests TEST_GEN_PROGS += soft-dirty TEST_GEN_PROGS += split_huge_page_test TEST_GEN_PROGS += ksm_tests@@ -107,7 +108,8 @@ include ../lib.mk $(TEST_GEN_PROGS): vm_util.c -$(OUTPUT)/userfaultfd: uffd-common.c +$(OUTPUT)/uffd-stress: uffd-common.c +$(OUTPUT)/uffd-unit-tests: uffd-common.c ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))tools/testing/selftests/mm/run_vmtests.sh +6
-4@@ -196,14 +196,16 @@ CATEGORY="gup_test" run_test ./gup_test -a # Dump pages 0, 19, and 4096, using pin_user_pages: CATEGORY="gup_test" run_test ./gup_test -ct -F 0x1 0 19 0x1000 +CATEGORY="userfaultfd" run_test ./uffd-unit-tests uffd_mods=("" ":dev") +uffd_stress_bin=./uffd-stress for mod in "${uffd_mods[@]}"; do - CATEGORY="userfaultfd" run_test ./userfaultfd anon${mod} 20 16 + CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon${mod} 20 16 # Hugetlb tests require source and destination huge pages. Pass in half # the size ($half_ufd_size_MB), which is used for *each*. - CATEGORY="userfaultfd" run_test ./userfaultfd hugetlb${mod} "$half_ufd_size_MB" 32 - CATEGORY="userfaultfd" run_test ./userfaultfd hugetlb_shared${mod} "$half_ufd_size_MB" 32 - CATEGORY="userfaultfd" run_test ./userfaultfd shmem${mod} 20 16 + CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb${mod} "$half_ufd_size_MB" 32 + CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb_shared${mod} "$half_ufd_size_MB" 32 + CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem${mod} 20 16 done #cleanuptools/testing/selftests/mm/uffd-stress.c renamed from tools/testing/selftests/mm/userfaultfd.c +0
-0tools/testing/selftests/mm/uffd-unit-tests.c new file +27
-0@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Userfaultfd unit tests. +* +* Copyright (C) 2015-2023 Red Hat, Inc. +*/ + +#include "uffd-common.h" + +#ifdef __NR_userfaultfd + +int main (int argc,char *argv[]) +{ +return KSFT_PASS ; +} + +#else /* __NR_userfaultfd */ + +#warning "missing __NR_userfaultfd definition" + +int main (void ) +{ +printf ("Skipping %s (missing __NR_userfaultfd)\n" , __file__); +return KSFT_SKIP ; +} + +#endif /* __NR_userfaultfd */ Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 14/29 selftests/mm: uffd_[un]register()
In reply to Peter XuAdd two helpers to register/unregister to an uffd. Use them to drop duplicate codes.
This patch also drops assert_expected_ioctls_present() and get_expected_ioctls(). Reasons:
- It'll need a lot of effort to pass test_type==HUGETLB into it from the upper, so it's the simplest way to get rid of another global var
- The ioctls returned in UFFDIO_REGISTER is hardly useful at all, because any app can already detect kernel support on any ioctl via its corresponding UFFD_FEATURE_*. The check here is for sanity mostly but it's probably destined no user app will even use it.
- It's not friendly to one future goal of uffd to run on old kernels, the problem is get_expected_ioctls() compiles against UFFD_API_RANGE_IOCTLS, which is a value that can change depending on where the test is compiled, rather than reflecting what the kernel underneath has. It means it'll report false negatives on old kernels so it's against our will.
So let's make our live easier.
--- tools/testing/selftests/mm/hugepage-mremap.c | 7 +- .../selftests/mm/ksm_functional_tests.c | 6 +- tools/testing/selftests/mm/uffd-common.c | 28 +------ tools/testing/selftests/mm/uffd-common.h | 2 - tools/testing/selftests/mm/uffd-stress.c | 83 +++++-------------- tools/testing/selftests/mm/vm_util.c | 37 +++++++++ tools/testing/selftests/mm/vm_util.h | 7 ++ 7 files changed, 66 insertions(+), 104 deletions(-)
tools/testing/selftests/mm/hugepage-mremap.c +1 -6
@@ -60,7 +60,6 @@ static void register_region_with_uffd(char *addr, size_t len) {long uffd;/* userfaultfd file descriptor */ struct uffdio_api uffdio_api; -struct uffdio_register uffdio_register;/* Create and enable userfaultfd object. */
@@ -96,11 +95,7 @@ static void register_region_with_uffd(char *addr, size_t len)* handling by the userfaultfd object. In mode, we request to track * missing pages (i.e., pages that have not yet been faulted in). */ - - uffdio_register.range.start = (unsigned long )addr; - uffdio_register.range.len = len; - uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) ==-1 ) { +if (uffd_register (uffd, addr, len, true, false, false)) {perror ("ioctl-UFFDIO_REGISTER" );exit (1 ); }
tools/testing/selftests/mm/ksm_functional_tests.c +1 -5
@@ -178,7 +178,6 @@ static void test_unmerge_discarded(void)static void test_unmerge_uffd_wp (void ) {struct uffdio_writeprotect uffd_writeprotect; -struct uffdio_register uffdio_register;const unsigned int size =2 * MiB;struct uffdio_api uffdio_api;char *map;
@@ -210,10 +209,7 @@ static void test_unmerge_uffd_wp(void) }/* Register UFFD-WP, no need for an actual handler. */ - uffdio_register.range.start = (unsigned long ) map; - uffdio_register.range.len = size; - uffdio_register.mode =UFFDIO_REGISTER_MODE_WP ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) <0 ) { +if (uffd_register (uffd, map, size, false, true, false)) {ksft_test_result_fail ("UFFDIO_REGISTER_MODE_WP failed\n" ); goto close_uffd; }
tools/testing/selftests/mm/uffd-common.c +1 -27
@@ -330,33 +330,6 @@ void uffd_test_ctx_init(uint64_t features)err ("pipe" ); } -uint64_t get_expected_ioctls (uint64_t mode) -{ -uint64_t ioctls =UFFD_API_RANGE_IOCTLS ; - -if (test_type ==TEST_HUGETLB ) - ioctls &= ~(1 << _UFFDIO_ZEROPAGE); - -if (!((mode &UFFDIO_REGISTER_MODE_WP ) && test_uffdio_wp)) - ioctls &= ~(1 << _UFFDIO_WRITEPROTECT); - -if (!((mode &UFFDIO_REGISTER_MODE_MINOR ) && test_uffdio_minor)) - ioctls &= ~(1 << _UFFDIO_CONTINUE); - -return ioctls; -} - -void assert_expected_ioctls_present (uint64_t mode,uint64_t ioctls) -{ -uint64_t expected =get_expected_ioctls (mode); -uint64_t actual = ioctls & expected; - -if (actual != expected) { -err ("missing ioctl(s): expected %" PRIx64" actual: %" PRIx64, - expected, actual); - } -} -void wp_range (int ufd,__u64 start,__u64 len,bool wp) {struct uffdio_writeprotect prms;
@@ -609,3 +582,4 @@ int copy_page(int ufd, unsigned long offset) {return __copy_page (ufd, offset, false); } +
tools/testing/selftests/mm/uffd-common.h +0 -2
@@ -101,8 +101,6 @@ extern uffd_test_ops_t *uffd_test_ops;void uffd_stats_report (struct uffd_stats *stats,int n_cpus);void uffd_test_ctx_init (uint64_t features);void userfaultfd_open (uint64_t *features); -uint64_t get_expected_ioctls (uint64_t mode); -void assert_expected_ioctls_present (uint64_t mode,uint64_t ioctls);int uffd_read_msg (int ufd,struct uffd_msg *msg);void wp_range (int ufd,__u64 start,__u64 len,bool wp);void uffd_handle_page_fault (struct uffd_msg *msg,struct uffd_stats *stats);
tools/testing/selftests/mm/uffd-stress.c +19 -64
176 lines not on this page · raw message
tools/testing/selftests/mm/vm_util.c +37 -0
@@ -1,6 +1,8 @@// SPDX-License-Identifier: GPL-2.0 #include <string.h> #include <fcntl.h> +#include <sys/ioctl.h> +#include <linux/userfaultfd.h> #include "../kselftest.h" #include "vm_util.h"
@@ -193,3 +195,38 @@ unsigned long default_huge_page_size(void)fclose (f);return hps; } + +int uffd_register (int uffd,void *addr,uint64_t len, +bool miss,bool wp,bool minor) +{ +struct uffdio_register uffdio_register = {0 }; +uint64_t mode =0 ; +int ret =0 ; + +if (miss) + mode |=UFFDIO_REGISTER_MODE_MISSING ; +if (wp) + mode |=UFFDIO_REGISTER_MODE_WP ; +if (minor) + mode |=UFFDIO_REGISTER_MODE_MINOR ; + + uffdio_register.range.start = (unsigned long )addr; + uffdio_register.range.len = len; + uffdio_register.mode = mode; + +if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) ==-1 ) + ret = -errno; + +return ret; +} + +int uffd_unregister (int uffd,void *addr,uint64_t len) +{ +struct uffdio_range range = { .start = (uintptr_t )addr, .len = len }; +int ret =0 ; + +if (ioctl (uffd,UFFDIO_UNREGISTER , &range) ==-1 ) + ret = -errno; + +return ret; +}
tools/testing/selftests/mm/vm_util.h +7 -0
@@ -45,6 +45,13 @@ bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);int64_t allocate_transhuge (void *ptr,int pagemap_fd);unsigned long default_huge_page_size (void ); +int uffd_open_dev (unsigned int flags); +int uffd_open_sys (unsigned int flags); +int uffd_open (unsigned int flags); +int uffd_register (int uffd,void *addr,uint64_t len, +bool miss,bool wp,bool minor); +int uffd_unregister (int uffd,void *addr,uint64_t len); +/* * On ppc64 this will only work with radix 2M hugepage size */
Signature · 1 line
2.39.1
Peter Xu
reviewerRe: [PATCH 14/29] selftests/mm: uffd_[un]register()
Quoted 24 lines · Peter Xu
tools/testing/selftests/mm/uffd-common.c +0
-16@@ -330,33 +330,6 @@ void uffd_test_ctx_init(uint64_t features)err ("pipe" ); } -uint64_t get_expected_ioctls (uint64_t mode) -{ -uint64_t ioctls =UFFD_API_RANGE_IOCTLS ; - -if (test_type ==TEST_HUGETLB ) - ioctls &= ~(1 << _UFFDIO_ZEROPAGE); - -if (!((mode &UFFDIO_REGISTER_MODE_WP ) && test_uffdio_wp)) - ioctls &= ~(1 << _UFFDIO_WRITEPROTECT); - -if (!((mode &UFFDIO_REGISTER_MODE_MINOR ) && test_uffdio_minor)) - ioctls &= ~(1 << _UFFDIO_CONTINUE); - -return ioctls; -} -
tools/testing/selftests/mm/uffd-common.c void uffd_test_ctx_init(uint64_t features) · line 358
Quoted 10 lines
tools/testing/selftests/mm/uffd-common.c +0
-10-void assert_expected_ioctls_present (uint64_t mode,uint64_t ioctls) -{ -uint64_t expected =get_expected_ioctls (mode); -uint64_t actual = ioctls & expected; - -if (actual != expected) { -err ("missing ioctl(s): expected %" PRIx64" actual: %" PRIx64, - expected, actual); - } -}
Here I dropped the other reference of get_expected_ioctls(), so I also dropped this test which I think is kind of flawed IMHO - as I replied in the other thread, we should probably not reference UFFD_API_RANGE_IOCTLS.
But I can feel (from the comments in the other patch that removed the other reference of get_expected_ioctls()) that a lot of us would still care about this test.
So I added a new patch / test on top of the series (so it'll have one more patch in the next version at last), just to test all possible combinations of UFFDIO_REGISTER alongside with its returned uffdio_register.ioctls.
This is IMHO better than get_expected_ioctls() because:
- It's much cleaner to have a separate test on this rather than testing it randomly in the code with random values passed in.
- It tests all combinations. It not only includes shmem-private that this series introduced while wasn't there before, but also all combinations of (miss, wp, minor) tuples.
- It doesn't rely on UFFD_API_RANGE_IOCTLS anymore.
It'll be something like this:
===8<===
/*
* Test the returned uffdio_register.ioctls with different register modes.
* Note that _UFFDIO_ZEROPAGE is tested separately in the zeropage test. */
static voiddo_register_ioctls_test(uffd_test_args_t *args, bool miss, bool wp, bool minor)
{
uint64_t ioctls = 0, expected = BIT_ULL(_UFFDIO_WAKE);
mem_type_t *mem_type = args->mem_type;
int ret;
ret = uffd_register_with_ioctls(uffd, area_dst, page_size,
miss, wp, minor, &ioctls);
/*
* Handle special cases of UFFDIO_REGISTER here where it should
* just fail with -EINVAL first..
*
* Case 1: register MINOR on anon
* Case 2: register with no mode selected
*/
if ((minor && (mem_type->mem_flag == MEM_ANON)) ||
(!miss && !wp && !minor)) {
if (ret != -EINVAL)
err("register (miss=%d, wp=%d, minor=%d) failed "
"with wrong errno=%d", miss, wp, minor, ret);
return;
}
/* UFFDIO_REGISTER should succeed, then check ioctls returned */
if (miss)
expected |= BIT_ULL(_UFFDIO_COPY);
if (wp)
expected |= BIT_ULL(_UFFDIO_WRITEPROTECT);
if (minor)
expected |= BIT_ULL(_UFFDIO_CONTINUE);
if ((ioctls & expected) != expected)
err("unexpected uffdio_register.ioctls "
"(miss=%d, wp=%d, minor=%d): expected=0x%"PRIx64", "
"returned=0x%"PRIx64, miss, wp, minor, expected, ioctls);
uffd_unregister(uffd, area_dst, page_size);
}static void uffd_register_ioctls_test(uffd_test_args_t *args)
{
int miss, wp, minor;
for (miss = 0; miss <= 1; miss++)
for (wp = 0; wp <= 1; wp++)
for (minor = 0; minor <= 1; minor++)
do_register_ioctls_test(args, miss, wp, minor);
uffd_test_pass();
}===8<===
Side note: the _UFFDIO_ZEROPAGE test will be left in the specific zeropage test.
I considered moving get_expected_ioctls() rather than dropping in the same patch, but that's just over-complicated when without the unit test frameworks being ready. I hope this addresses the concern here, otherwise please shoot.
I've also attached the two patches that will test uffdio_register.ioctls as a whole, just in case helpful for discussion before I post v2.
Thanks,
Signature · 1 line
Peter Xu
From bea1534894c8edca4d7132c2d2676a9faf94a346 Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Wed, 29 Mar 2023 16:34:15 -0400
Subject: [PATCH] selftests/mm: Move zeropage test into uffd unit tests
Simplifies it a bit along the way, e.g., drop the never used offset field (which was always the 1st page so offset=0).
Introduce uffd_register_with_ioctls() out of uffd_register() to detect uffdio_register.ioctls got returned. Check that automatically when testing UFFDIO_ZEROPAGE on different types of memory (and kernel).
--- tools/testing/selftests/mm/uffd-stress.c | 94 +------------------- tools/testing/selftests/mm/uffd-unit-tests.c | 93 +++++++++++++++++++ tools/testing/selftests/mm/vm_util.c | 14 ++- tools/testing/selftests/mm/vm_util.h | 2 + 4 files changed, 108 insertions(+), 95 deletions(-)
tools/testing/selftests/mm/uffd-stress.c +1 -93
@@ -110,15 +110,6 @@ static inline uint64_t uffd_minor_feature(void)return 0 ; } -static int my_bcmp (char *str1,char *str2,size_t n) -{ -unsigned long i; -for (i =0 ; i < n; i++) -if (str1[i] != str2[i]) -return 1 ; -return 0 ; -} -static void *locking_thread (void *arg) {unsigned long cpu = (unsigned long ) arg;
@@ -274,89 +265,6 @@ static int stress(struct uffd_stats *uffd_stats)return 0 ; } -static void retry_uffdio_zeropage (int ufd, -struct uffdio_zeropage *uffdio_zeropage, -unsigned long offset) -{ - uffd_test_ops->alias_mapping (&uffdio_zeropage->range.start, - uffdio_zeropage->range.len, - offset); -if (ioctl (ufd,UFFDIO_ZEROPAGE , uffdio_zeropage)) { -if (uffdio_zeropage->zeropage != -EEXIST ) -err ("UFFDIO_ZEROPAGE error: %" PRId64, - (int64_t )uffdio_zeropage->zeropage); - }else { -err ("UFFDIO_ZEROPAGE error: %" PRId64, - (int64_t )uffdio_zeropage->zeropage); - } -} - -static int __uffdio_zeropage (int ufd,unsigned long offset) -{ -struct uffdio_zeropage uffdio_zeropage; -int ret; -bool has_zeropage = !(test_type ==TEST_HUGETLB ); -__s64 res; - -if (offset >= nr_pages * page_size) -err ("unexpected offset %lu" , offset); - uffdio_zeropage.range.start = (unsigned long ) area_dst + offset; - uffdio_zeropage.range.len = page_size; - uffdio_zeropage.mode =0 ; - ret =ioctl (ufd,UFFDIO_ZEROPAGE , &uffdio_zeropage); - res = uffdio_zeropage.zeropage; -if (ret) { -/* real retval in ufdio_zeropage.zeropage */ -if (has_zeropage) -err ("UFFDIO_ZEROPAGE error: %" PRId64, (int64_t )res); -else if (res != -EINVAL ) -err ("UFFDIO_ZEROPAGE not -EINVAL" ); - }else if (has_zeropage) { -if (res != page_size) { -err ("UFFDIO_ZEROPAGE unexpected size" ); - }else { -retry_uffdio_zeropage (ufd, &uffdio_zeropage, - offset); -return 1 ; - } - }else -err ("UFFDIO_ZEROPAGE succeeded" ); - -return 0 ; -} - -static int uffdio_zeropage (int ufd,unsigned long offset) -{ -return __uffdio_zeropage (ufd, offset); -} - -/* exercise UFFDIO_ZEROPAGE */ -static int userfaultfd_zeropage_test (void ) -{ -printf ("testing UFFDIO_ZEROPAGE: " ); -fflush (stdout); - -uffd_test_ctx_init (0 ); - -if (uffd_register (uffd, area_dst, nr_pages * page_size, - true, test_uffdio_wp, false)) -err ("register failure" ); - -if (area_dst_alias) { -/* Needed this to test zeropage-retry on shared memory */ -if (uffd_register (uffd, area_dst_alias, nr_pages * page_size, - true, test_uffdio_wp, false)) -err ("register failure" ); - } - -if (uffdio_zeropage (uffd,0 )) -if (my_bcmp (area_dst, zeropage, page_size)) -err ("zeropage is not zero" ); - -printf ("done.\n" ); -return 0 ; -} -static int userfaultfd_stress (void ) {void *area;
@@ -468,7 +376,7 @@ static int userfaultfd_stress(void)uffd_stats_report (uffd_stats, nr_cpus); } -return userfaultfd_zeropage_test (); +return 0 ; }static void set_test_type (const char *type)
tools/testing/selftests/mm/uffd-unit-tests.c +93 -0
100 lines not on this page · raw message
tools/testing/selftests/mm/vm_util.c +12 -2
@@ -198,8 +198,9 @@ unsigned long default_huge_page_size(void)return hps; } -int uffd_register (int uffd,void *addr,uint64_t len, -bool miss,bool wp,bool minor) +/* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */ +int uffd_register_with_ioctls (int uffd,void *addr,uint64_t len, +bool miss,bool wp,bool minor,uint64_t *ioctls) {struct uffdio_register uffdio_register = {0 };uint64_t mode =0 ;
@@ -218,10 +219,19 @@ int uffd_register(int uffd, void *addr, uint64_t len,if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) ==-1 ) ret = -errno; +else if (ioctls) + *ioctls = uffdio_register.ioctls;return ret; } +int uffd_register (int uffd,void *addr,uint64_t len, +bool miss,bool wp,bool minor) +{ +return uffd_register_with_ioctls (uffd, addr, len, + miss, wp, minor,NULL ); +} +int uffd_unregister (int uffd,void *addr,uint64_t len) {struct uffdio_range range = { .start = (uintptr_t )addr, .len = len };
tools/testing/selftests/mm/vm_util.h +2 -0
@@ -52,6 +52,8 @@ int uffd_open_dev(unsigned int flags);int uffd_open_sys (unsigned int flags);int uffd_open (unsigned int flags);int uffd_get_features (uint64_t *features); +int uffd_register_with_ioctls (int uffd,void *addr,uint64_t len, +bool miss,bool wp,bool minor,uint64_t *ioctls);/* * On ppc64 this will only work with radix 2M hugepage size
Signature · 1 line
2.39.1
From f575e3d26dedce19ef9790c2ac0a5e7e17bcf2bd Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Wed, 5 Apr 2023 14:20:18 -0400
Subject: [PATCH] selftests/mm: Add uffdio register ioctls test
This new test tests against the returned ioctls from UFFDIO_REGISTER, where put into uffdio_register.ioctls.
This also tests the expected failure cases of UFFDIO_REGISTER, aka:
- Register with empty mode should fail with -EINVAL
- Register anon without page cache (anon) should fail with -EINVAL
--- tools/testing/selftests/mm/uffd-unit-tests.c | 111 ++++++++++++++++--- 1 file changed, 96 insertions(+), 15 deletions(-)
tools/testing/selftests/mm/uffd-unit-tests.c +96 -15
204 lines not on this page · raw message
Signature · 1 line
2.39.1
Re: [PATCH 14/29] selftests/mm: uffd_[un]register()
In reply to Peter XuQuoted 46 lines · Peter Xu
Add two helpers to register/unregister to an uffd. Use them to drop duplicate codes.
This patch also drops
assert_expected_ioctls_present()andget_expected_ioctls(). Reasons:- It'll need a lot of effort to pass test_type==HUGETLB into it from the upper, so it's the simplest way to get rid of another global var
- The ioctls returned in
UFFDIO_REGISTERis hardly useful at all, because any app can already detect kernel support on any ioctl via its corresponding UFFD_FEATURE_*. The check here is for sanity mostly but it's probably destined no user app will even use it.- It's not friendly to one future goal of uffd to run on old kernels, the problem is
get_expected_ioctls()compiles againstUFFD_API_RANGE_IOCTLS, which is a value that can change depending on where the test is compiled, rather than reflecting what the kernel underneath has. It means it'll report false negatives on old kernels so it's against our will.So let's make our live easier.
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/hugepage-mremap.c | 7 +- .../selftests/mm/ksm_functional_tests.c | 6 +- tools/testing/selftests/mm/uffd-common.c | 28 +------ tools/testing/selftests/mm/uffd-common.h | 2 - tools/testing/selftests/mm/uffd-stress.c | 83 +++++-------------- tools/testing/selftests/mm/vm_util.c | 37 +++++++++ tools/testing/selftests/mm/vm_util.h | 7 ++ 7 files changed, 66 insertions(+), 104 deletions(-)tools/testing/selftests/mm/hugepage-mremap.c +0
-1@@ -60,7 +60,6 @@ static void register_region_with_uffd(char *addr, size_t len) {long uffd;/* userfaultfd file descriptor */ struct uffdio_api uffdio_api; -struct uffdio_register uffdio_register;/* Create and enable userfaultfd object. */
tools/testing/selftests/mm/hugepage-mremap.c static void register_region_with_uffd(char *addr, size_t len) · line 98
Quoted 9 lines
tools/testing/selftests/mm/hugepage-mremap.c +1
-5@@ -96,11 +95,7 @@ static void register_region_with_uffd(char *addr, size_t len)* handling by the userfaultfd object. In mode, we request to track * missing pages (i.e., pages that have not yet been faulted in). */ - - uffdio_register.range.start = (unsigned long )addr; - uffdio_register.range.len = len; - uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) ==-1 ) { +if (uffd_register (uffd, addr, len, true, false, false)) {
I'd replace booleans with a bit flags as it easier to read. Other than that LGTM.
Quoted 3 lines
tools/testing/selftests/mm/hugepage-mremap.c +0
-0perror ("ioctl-UFFDIO_REGISTER" );exit (1 ); }
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerRe: [PATCH 14/29] selftests/mm: uffd_[un]register()
Quoted 5 lines · Mike Rapoport
Quoted 5 lines
tools/testing/selftests/mm/hugepage-mremap.c +0
-1@@ -96,11 +95,7 @@ static void register_region_with_uffd(char *addr, size_t len)* handling by the userfaultfd object. In mode, we request to track * missing pages (i.e., pages that have not yet been faulted in). */ -
Quoted 8 lines
tools/testing/selftests/mm/hugepage-mremap.c static void register_region_with_uffd(char *addr, size_t len) · line 98
Quoted 5 lines
tools/testing/selftests/mm/hugepage-mremap.c +1
-4- uffdio_register.range.start = (unsigned long )addr; - uffdio_register.range.len = len; - uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) ==-1 ) { +if (uffd_register (uffd, addr, len, true, false, false)) {I'd replace booleans with a bit flags as it easier to read. Other than that LGTM.
It was mostly for no need to remember the long names of macros, and easier when conditionally set with some modes. E.g., we have 5 callers have things like:
uffd_register(..., test_uffdio_wp ? UFFDIO_REGISTER_MODE_WP : 0);
The bools simplifes it to:
uffd_register(..., test_uffdio_wp, ...);
But let me know if you still think that's better - I can switch here.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 14/29] selftests/mm: uffd_[un]register()
Quoted 19 lines · Peter Xu
Quoted 5 lines · Mike Rapoport
Quoted 5 lines
tools/testing/selftests/mm/hugepage-mremap.c +0
-1@@ -96,11 +95,7 @@ static void register_region_with_uffd(char *addr, size_t len)* handling by the userfaultfd object. In mode, we request to track * missing pages (i.e., pages that have not yet been faulted in). */ -Quoted 8 lines
tools/testing/selftests/mm/hugepage-mremap.c static void register_region_with_uffd(char *addr, size_t len) · line 98
Quoted 5 lines
tools/testing/selftests/mm/hugepage-mremap.c +1
-4- uffdio_register.range.start = (unsigned long )addr; - uffdio_register.range.len = len; - uffdio_register.mode =UFFDIO_REGISTER_MODE_MISSING ; -if (ioctl (uffd,UFFDIO_REGISTER , &uffdio_register) ==-1 ) { +if (uffd_register (uffd, addr, len, true, false, false)) {I'd replace booleans with a bit flags as it easier to read. Other than that LGTM.
It was mostly for no need to remember the long names of macros, and easier when conditionally set with some modes. E.g., we have 5 callers have things like:
Quoted 7 lines
uffd_register(..., test_uffdio_wp ? UFFDIO_REGISTER_MODE_WP : 0);The bools simplifes it to:
uffd_register(..., test_uffdio_wp, ...);But let me know if you still think that's better - I can switch here.
No strong feelings, however you prefer.
Quoted 5 lines
Thanks,
Signature · 1 line
Peter Xu
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 15/29 selftests/mm: uffd_open_{dev|sys}()
In reply to Peter XuProvide two helpers to open an uffd handle. Drop the error checks around SKIPs because it's inside an errexit() anyway, which IMHO doesn't really help much if the test will not continue.
--- tools/testing/selftests/mm/uffd-common.c | 28 +++++------------------- tools/testing/selftests/mm/vm_util.c | 24 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 23 deletions(-)
tools/testing/selftests/mm/uffd-common.c +5 -23
@@ -192,34 +192,16 @@ void uffd_stats_report(struct uffd_stats *stats, int n_cpus)printf ("\n" ); } -static int __userfaultfd_open_dev (void ) -{ -int fd, _uffd; - - fd =open ("/dev/userfaultfd" ,O_RDWR |O_CLOEXEC ); -if (fd <0 ) -errexit (KSFT_SKIP ,"opening /dev/userfaultfd failed" ); - - _uffd =ioctl (fd,USERFAULTFD_IOC_NEW ,UFFD_FLAGS ); -if (_uffd <0 ) -errexit (errno ==ENOTTY ?KSFT_SKIP :1 , -"creating userfaultfd failed" ); -close (fd); -return _uffd; -} -void userfaultfd_open (uint64_t *features) {struct uffdio_api uffdio_api;if (test_dev_userfaultfd) - uffd =__userfaultfd_open_dev (); -else { - uffd =syscall (__NR_userfaultfd,UFFD_FLAGS ); -if (uffd <0 ) -errexit (errno ==ENOSYS ?KSFT_SKIP :1 , -"creating userfaultfd failed" ); - } + uffd =uffd_open_dev (UFFD_FLAGS ); +else + uffd =uffd_open_sys (UFFD_FLAGS ); +if (uffd <0 ) +err ("uffd open failed (dev=%d)" , test_dev_userfaultfd); uffd_flags =fcntl (uffd,F_GETFD ,NULL ); uffdio_api.api =UFFD_API ;
tools/testing/selftests/mm/vm_util.c +24 -0
@@ -3,6 +3,8 @@#include <fcntl.h> #include <sys/ioctl.h> #include <linux/userfaultfd.h> +#include <sys/syscall.h> +#include <unistd.h> #include "../kselftest.h" #include "vm_util.h"
@@ -230,3 +232,25 @@ int uffd_unregister(int uffd, void *addr, uint64_t len)return ret; } + +int uffd_open_dev (unsigned int flags) +{ +int fd, uffd; + + fd =open ("/dev/userfaultfd" ,O_RDWR |O_CLOEXEC ); +if (fd <0 ) +return fd; + uffd =ioctl (fd,USERFAULTFD_IOC_NEW , flags); +close (fd); + +return uffd; +} + +int uffd_open_sys (unsigned int flags) +{ +#ifdef __NR_userfaultfd +return syscall (__NR_userfaultfd, flags); +#else +return -1 ; +#endif +}
Signature · 1 line
2.39.1
Re: [PATCH 15/29] selftests/mm: uffd_open_{dev|sys}()
Quoted 6 lines · Peter Xu
Provide two helpers to open an uffd handle. Drop the error checks around SKIPs because it's inside an
errexit()anyway, which IMHO doesn't really help much if the test will not continue.Signed-off-by Peter Xu <[email protected]>---
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH 15/29] selftests/mm: uffd_open_{dev|sys}()
In reply to Peter XuQuoted 5 lines · Peter Xu
Provide two helpers to open an uffd handle. Drop the error checks around SKIPs because it's inside an
errexit()anyway, which IMHO doesn't really help much if the test will not continue.Signed-off-by Peter Xu <[email protected]>
Quoted 91 lines
--- tools/testing/selftests/mm/uffd-common.c | 28 +++++------------------- tools/testing/selftests/mm/vm_util.c | 24 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 23 deletions(-)tools/testing/selftests/mm/uffd-common.c +5
-23@@ -192,34 +192,16 @@ void uffd_stats_report(struct uffd_stats *stats, int n_cpus)printf ("\n" ); } -static int __userfaultfd_open_dev (void ) -{ -int fd, _uffd; - - fd =open ("/dev/userfaultfd" ,O_RDWR |O_CLOEXEC ); -if (fd <0 ) -errexit (KSFT_SKIP ,"opening /dev/userfaultfd failed" ); - - _uffd =ioctl (fd,USERFAULTFD_IOC_NEW ,UFFD_FLAGS ); -if (_uffd <0 ) -errexit (errno ==ENOTTY ?KSFT_SKIP :1 , -"creating userfaultfd failed" ); -close (fd); -return _uffd; -} -void userfaultfd_open (uint64_t *features) {struct uffdio_api uffdio_api;if (test_dev_userfaultfd) - uffd =__userfaultfd_open_dev (); -else { - uffd =syscall (__NR_userfaultfd,UFFD_FLAGS ); -if (uffd <0 ) -errexit (errno ==ENOSYS ?KSFT_SKIP :1 , -"creating userfaultfd failed" ); - } + uffd =uffd_open_dev (UFFD_FLAGS ); +else + uffd =uffd_open_sys (UFFD_FLAGS ); +if (uffd <0 ) +err ("uffd open failed (dev=%d)" , test_dev_userfaultfd); uffd_flags =fcntl (uffd,F_GETFD ,NULL ); uffdio_api.api =UFFD_API ;tools/testing/selftests/mm/vm_util.c +24
-0@@ -3,6 +3,8 @@#include <fcntl.h> #include <sys/ioctl.h> #include <linux/userfaultfd.h> +#include <sys/syscall.h> +#include <unistd.h> #include "../kselftest.h" #include "vm_util.h" @@ -230,3 +232,25 @@ int uffd_unregister(int uffd, void *addr, uint64_t len)return ret; } + +int uffd_open_dev (unsigned int flags) +{ +int fd, uffd; + + fd =open ("/dev/userfaultfd" ,O_RDWR |O_CLOEXEC ); +if (fd <0 ) +return fd; + uffd =ioctl (fd,USERFAULTFD_IOC_NEW , flags); +close (fd); + +return uffd; +} + +int uffd_open_sys (unsigned int flags) +{ +#ifdef __NR_userfaultfd +return syscall (__NR_userfaultfd, flags); +#else +return -1 ; +#endif +}Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 16/29 selftests/mm: UFFDIO_API test
In reply to Peter XuAdd one simple test for UFFDIO_API. With that, I also added a bunch of small but handy helpers along the way.
--- tools/testing/selftests/mm/uffd-unit-tests.c | 111 ++++++++++++++++++- tools/testing/selftests/mm/vm_util.c | 10 ++ 2 files changed, 120 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/uffd-unit-tests.c +110 -1
@@ -9,9 +9,118 @@#ifdef __NR_userfaultfd +struct { +unsigned int pass, skip, fail, total; +} uffd_test_acct; + +static void uffd_test_report (void ) +{ +printf ("Userfaults unit tests: pass=%u, skip=%u, fail=%u (total=%u)\n" , + uffd_test_acct.pass, + uffd_test_acct.skip, + uffd_test_acct.fail, + uffd_test_acct.total); +} + +static void uffd_test_pass (void ) +{ +printf ("done\n" ); + uffd_test_acct.pass++; +} + +#define uffd_test_start (...) do { \ + printf(__VA_ARGS__); \ + printf("... "); \ + uffd_test_acct.total++; \ + } while (0) + +#define uffd_test_fail (...) do { \ + printf("failed [reason: "); \ + printf(__VA_ARGS__); \ + printf("]\n"); \ + uffd_test_acct.fail++; \ + } while (0) + +#define uffd_test_skip (...) do { \ + printf("skipped [reason: "); \ + printf(__VA_ARGS__); \ + printf("]\n"); \ + uffd_test_acct.skip++; \ + } while (0) + +/* +* Returns 1 if specific userfaultfd supported, 0 otherwise. Note, we'll +* return 1 even if some test failed as long as uffd supported, because in +* that case we still want to proceed with the rest uffd unit tests. +*/ +static int test_uffd_api (bool use_dev) +{ +struct uffdio_api uffdio_api; +int uffd; + +uffd_test_start ("UFFDIO_API (with %s)" , + use_dev ?"/dev/userfaultfd" :"syscall" ); + +if (use_dev) + uffd =uffd_open_dev (UFFD_FLAGS ); +else + uffd =uffd_open_sys (UFFD_FLAGS ); +if (uffd <0 ) { +uffd_test_skip ("cannot open userfaultfd handle" ); +return 0 ; + } + +/* Test wrong UFFD_API */ + uffdio_api.api =0xab ; + uffdio_api.features =0 ; +if (ioctl (uffd,UFFDIO_API , &uffdio_api) ==0 ) { +uffd_test_fail ("UFFDIO_API should fail with wrong api but didn't" ); + goto out; + } + +/* Test wrong feature bit */ + uffdio_api.api =UFFD_API ; + uffdio_api.features =BIT_ULL (63 ); +if (ioctl (uffd,UFFDIO_API , &uffdio_api) ==0 ) { +uffd_test_fail ("UFFDIO_API should fail with wrong feature but didn't" ); + goto out; + } + +/* Test normal UFFDIO_API */ + uffdio_api.api =UFFD_API ; + uffdio_api.features =0 ; +if (ioctl (uffd,UFFDIO_API , &uffdio_api)) { +uffd_test_fail ("UFFDIO_API should succeed but failed" ); + goto out; + } + +/* Test double requests of UFFDIO_API with a random feature set */ + uffdio_api.features =BIT_ULL (0 ); +if (ioctl (uffd,UFFDIO_API , &uffdio_api) ==0 ) { +uffd_test_fail ("UFFDIO_API should reject initialized uffd" ); + goto out; + } + +uffd_test_pass (); +out: +close (uffd); +/* We have a valid uffd handle */ +return 1 ; +} +int main (int argc,char *argv[]) { -return KSFT_PASS ; +int has_uffd; + + has_uffd =test_uffd_api (false); + has_uffd |=test_uffd_api (true); + +if (!has_uffd) { +printf ("Userfaultfd not supported or unprivileged, skip all tests\n" ); +exit (KSFT_SKIP ); + } +uffd_test_report (); +return uffd_test_acct.fail ?KSFT_FAIL :KSFT_PASS ; }#else /* __NR_userfaultfd */
tools/testing/selftests/mm/vm_util.c +10 -0
@@ -254,3 +254,13 @@ int uffd_open_sys(unsigned int flags)return -1 ;#endif } + +int uffd_open (unsigned int flags) +{ +int uffd =uffd_open_sys (flags); + +if (uffd <0 ) + uffd =uffd_open_dev (flags); + +return uffd; +}
Signature · 1 line
2.39.1
Re: [PATCH 16/29] selftests/mm: UFFDIO_API test
Quoted 46 lines · Peter Xu
Add one simple test for
UFFDIO_API. With that, I also added a bunch of small but handy helpers along the way.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/uffd-unit-tests.c | 111 ++++++++++++++++++- tools/testing/selftests/mm/vm_util.c | 10 ++ 2 files changed, 120 insertions(+), 1 deletion(-)tools/testing/selftests/mm/uffd-unit-tests.c +29
-0@@ -9,9 +9,118 @@#ifdef __NR_userfaultfd +struct { +unsigned int pass, skip, fail, total; +} uffd_test_acct; + +static void uffd_test_report (void ) +{ +printf ("Userfaults unit tests: pass=%u, skip=%u, fail=%u (total=%u)\n" , + uffd_test_acct.pass, + uffd_test_acct.skip, + uffd_test_acct.fail, + uffd_test_acct.total); +} + +static void uffd_test_pass (void ) +{ +printf ("done\n" ); + uffd_test_acct.pass++; +} + +#define uffd_test_start (...) do { \ + printf(__VA_ARGS__); \ + printf("... "); \ + uffd_test_acct.total++; \ + } while (0) + +#define uffd_test_fail (...) do { \ + printf("failed [reason: "); \ + printf(__VA_ARGS__); \ + printf("]\n"); \
tools/testing/selftests/mm/uffd-unit-tests.c line 50
Quoted 10 lines
tools/testing/selftests/mm/uffd-unit-tests.c +10
-0+ uffd_test_acct.fail++; \ + } while (0) + +#define uffd_test_skip (...) do { \ + printf("skipped [reason: "); \ + printf(__VA_ARGS__); \ + printf("]\n"); \ + uffd_test_acct.skip++; \ + } while (0) +
There is ksft_print_msg, ksft_test_result, ksft_test_result_fail, ... do we maybe want to convert properly to ksft while already at it?
Signature · 3 lines
Thanks, David / dhildenb
Peter Xu
reviewerRe: [PATCH 16/29] selftests/mm: UFFDIO_API test
Quoted 2 lines · David Hildenbrand
There is
ksft_print_msg,ksft_test_result,ksft_test_result_fail, ... do we maybe want to convert properly to ksft while already at it?
Yes, I started with trying to use that but found that there're not a lot of things that I can leverage.
Starting with ksft_set_plan() - I think this is something we call first. I want the current unit test to skip everything if UFFD API test failed here, then I need to feed in a dynamic number of "plan" into ksft_set_plan(). But I never know after I ran the 1st test..
I can call ksft_set_plan() later than this, but it misses a few tests which also looks weird.
It also seems to not really help anything at all and not obvious to use. E.g. ksft_finished() will reference ksft_plan then it'll trigger ksft_exit_fail() but here I want to make it SKIP if the 1st test failed simply because the kernel probably doesn't have CONFIG_USERFAULTFD.
Another example: I never figured what does x{fail|pass|skip} meant in the header.. e.g. ksft_inc_xfail_cnt() is used nowhere so I cannot reference either. Then I don't know when I should increase them.
In short, to make the unit test behave as expected, I figured I'll just write these few helpers and that's good enough for this unit test. That takes perhaps 5 min anyway and isn't hugely bad for an unit test.
Then I keep the exit code matching kselftests (KSFT_SKIP, etc.).
What I can do here, though, is at least reuse the counters, e.g:
ksft_inc_pass_cnt() / ksft_inc_fail_cnt()
There's no ksft_inc_skip_cnt() so, maybe, I can just reuse ksft_inc_xskip_cnt() assuming that counts "skip"s?
Let me know if you have better ideas, I'll be happy to switch in that case.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH 16/29] selftests/mm: UFFDIO_API test
Quoted 11 lines · Peter Xu
Quoted 2 lines · David Hildenbrand
There is
ksft_print_msg,ksft_test_result,ksft_test_result_fail, ... do we maybe want to convert properly to ksft while already at it?Yes, I started with trying to use that but found that there're not a lot of things that I can leverage.
Starting with
ksft_set_plan()- I think this is something we call first. I want the current unit test to skip everything if UFFD API test failed here, then I need to feed in a dynamic number of "plan" intoksft_set_plan(). But I never know after I ran the 1st test..
In cow.c I did that. Getting the number of tests right can be challenging indeed.
Basic "feature availability" checks would go first (is uffd even around?), and depending on that you can set the plan.
For everything else, you can skip instead of test, so it will still be accounted towards the plan.
Quoted 3 lines
I can call
ksft_set_plan()later than this, but it misses a few tests which also looks weird.
Yeah, it would be nice to simply make ksft_set_plan() optional. For example, make ksft_print_cnts() skip the comparison if ksft_plan == 0. At least ksft_exit_skip() handles that already in a descend way (below).
Quoted 5 lines
It also seems to not really help anything at all and not obvious to use. E.g.
ksft_finished()will referenceksft_planthen it'll triggerksft_exit_fail()but here I want to make it SKIP if the 1st test failed simply because the kernel probably doesn't haveCONFIG_USERFAULTFD.
You'd simply do that availability check first and then use ksft_exit_skip() in case not available I guess.
Quoted 4 lines
Another example: I never figured what does x{fail|pass|skip} meant in the header.. e.g.
ksft_inc_xfail_cnt()is used nowhere so I cannot reference either. Then I don't know when I should increase them.
In cow.c I have the following flow:
ksft_print_header();ksft_set_plan();
... tests ...
err = ksft_get_fail_cnt();
if (err)
ksft_exit_fail_msg();
return ksft_exit_pass();
That gives me:
# [INFO] detected THP size: 2048 KiB
# [INFO] detected hugetlb size: 2048 KiB
# [INFO] detected hugetlb size: 1048576 KiB
# [INFO] huge zeropage is enabled
TAP version 13
1..190
...
# Totals: pass:87 fail:0 xfail:0 xpass:0 skip:103 error:0
I didn't use xfail or xpass so far, but what I understood is that these are "expected failures" and "expected passes". fail/pass/skip are straight forward. ksft_test_result_fail()/ksft_test_result_pass()/ksft_test_result_skip() are used to set them.
You'd do availability checks before ksft_set_plan() and fail with a ksft_exit_skip() if the kernel doesn't support it. Then, you'd just use ksft_test_result_fail()/ksft_test_result_pass()/ksft_test_result_skip().
Quoted 7 lines
In short, to make the unit test behave as expected, I figured I'll just write these few helpers and that's good enough for this unit test. That takes perhaps 5 min anyway and isn't hugely bad for an unit test.
Then I keep the exit code matching kselftests (
KSFT_SKIP, etc.).
Quoted 8 lines
What I can do here, though, is at least reuse the counters, e.g:
ksft_inc_pass_cnt()/ksft_inc_fail_cnt()There's no
ksft_inc_skip_cnt()so, maybe, I can just reuseksft_inc_xskip_cnt()assuming that counts "skip"s?Let me know if you have better ideas, I'll be happy to switch in that case.
I guess once you start manually increasing/decreasing the cnt, you might be abusing the ksft framework indeed and are better off handling it differently :D
Signature · 3 lines
Thanks, David / dhildenb
Peter Xu
reviewerRe: [PATCH 16/29] selftests/mm: UFFDIO_API test
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Peter Xu
Quoted 2 lines · David Hildenbrand
There is
ksft_print_msg,ksft_test_result,ksft_test_result_fail, ... do we maybe want to convert properly to ksft while already at it?Yes, I started with trying to use that but found that there're not a lot of things that I can leverage.
Quoted 7 lines
Quoted 4 lines
Starting with
ksft_set_plan()- I think this is something we call first. I want the current unit test to skip everything if UFFD API test failed here, then I need to feed in a dynamic number of "plan" intoksft_set_plan(). But I never know after I ran the 1st test..In
cow.cI did that. Getting the number of tests right can be challenging indeed.
IMHO the major thing is not about not easy to set, it's about there's merely no benefit I can see of having that calculated at the start of a test.
There's one thing it can do, that is when calling ksft_finished() it can be used to know whether all tests are run, but sadly here we're calculating everything just to make it match.. so it loses its last purpose.. IMHO.
Quoted 51 lines
Basic "feature availability" checks would go first (is uffd even around?), and depending on that you can set the plan.
For everything else, you can skip instead of test, so it will still be accounted towards the plan.
Quoted 3 lines
I can call
ksft_set_plan()later than this, but it misses a few tests which also looks weird.Yeah, it would be nice to simply make
ksft_set_plan()optional. For example, makeksft_print_cnts()skip the comparison ifksft_plan== 0. At leastksft_exit_skip()handles that already in a descend way (below).Quoted 5 lines
It also seems to not really help anything at all and not obvious to use. E.g.
ksft_finished()will referenceksft_planthen it'll triggerksft_exit_fail()but here I want to make it SKIP if the 1st test failed simply because the kernel probably doesn't haveCONFIG_USERFAULTFD.You'd simply do that availability check first and then use
ksft_exit_skip()in case not available I guess.Quoted 4 lines
Another example: I never figured what does x{fail|pass|skip} meant in the header.. e.g.
ksft_inc_xfail_cnt()is used nowhere so I cannot reference either. Then I don't know when I should increase them.In
cow.cI have the following flow:
ksft_print_header();ksft_set_plan();
... tests ...
err =ksft_get_fail_cnt();
if (err)ksft_exit_fail_msg();return
ksft_exit_pass();That gives me:
# [INFO] detected THP size: 2048 KiB
# [INFO] detected hugetlb size: 2048 KiB
# [INFO] detected hugetlb size: 1048576 KiB
# [INFO] huge zeropage is enabled
TAP version 13
1..190
...
# Totals: pass:87 fail:0 xfail:0 xpass:0 skip:103 error:0
Quoted 2 lines
I didn't use xfail or xpass so far, but what I understood is that these are "expected failures" and "expected passes". fail/pass/skip are straight
Yes, xfail can be expressed that way, but maybe not xpass? Otherwise it's hard to identify what's the difference between xpass and pass, because IIUC pass also means "expected to pass".
Quoted 19 lines
forward. ksft_test_result_fail()/ksft_test_result_pass()/ksft_test_result_skip() are used to set them.
You'd do availability checks before
ksft_set_plan()and fail with aksft_exit_skip()if the kernel doesn't support it. Then, you'd just use ksft_test_result_fail()/ksft_test_result_pass()/ksft_test_result_skip().Quoted 11 lines
In short, to make the unit test behave as expected, I figured I'll just write these few helpers and that's good enough for this unit test. That takes perhaps 5 min anyway and isn't hugely bad for an unit test.
Then I keep the exit code matching kselftests (
KSFT_SKIP, etc.).What I can do here, though, is at least reuse the counters, e.g:
ksft_inc_pass_cnt()/ksft_inc_fail_cnt()
Quoted 8 lines
Quoted 4 lines
There's no
ksft_inc_skip_cnt()so, maybe, I can just reuseksft_inc_xskip_cnt()assuming that counts "skip"s?Let me know if you have better ideas, I'll be happy to switch in that case.
I guess once you start manually increasing/decreasing the cnt, you might be abusing the ksft framework indeed and are better off handling it differently :D
I'm serious considering that to address your comment here, to show that I'm trying my best to use whatever can help in this test case. :) Here reusing it would avoid a few bytes in the bss, which is still beneficial so I can do. At least I'm sure xskip is for skipping now, so I know what to use.
PS: one other reason I didn't use the header is also because I prefer the current output of uffd-self-tests.c. I didn't say anything because I think it's stingy.. So let's keep this in a trivial small PS. After all, the print format is half of what the header provides functional-wise.
I'll see what I can come up with at last in the next version, thanks David.
Signature · 1 line
Peter Xu
Re: [PATCH 16/29] selftests/mm: UFFDIO_API test
Quoted 14 lines · Peter Xu
Quoted 13 lines · David Hildenbrand
Quoted 11 lines · Peter Xu
Quoted 2 lines · David Hildenbrand
There is
ksft_print_msg,ksft_test_result,ksft_test_result_fail, ... do we maybe want to convert properly to ksft while already at it?Yes, I started with trying to use that but found that there're not a lot of things that I can leverage.
Starting with
ksft_set_plan()- I think this is something we call first. I want the current unit test to skip everything if UFFD API test failed here, then I need to feed in a dynamic number of "plan" intoksft_set_plan(). But I never know after I ran the 1st test..
Quoted 6 lines
Quoted 2 lines
In
cow.cI did that. Getting the number of tests right can be challenging indeed.IMHO the major thing is not about not easy to set, it's about there's merely no benefit I can see of having that calculated at the start of a test.
Thinking about it, I believe I figured out why it makes sense. By specifying upfront how many tests you intend to run, the framework can check if you have the right number of pass/fail/skip during that test case execution.
This requires a different way of writing tests: each test case is supposed to trigger the exact same number of pass/fail/skip on each possible path.
If the numbers don't add up, it could indicate a possible bug in your tests. For example, not triggering a fail on some exit path. While your test execution might indicate "success", there is actually a hidden issue in your test.
I started using the framework for all new tests, because I think it's quite nice and at least things will be a bit consistent and possibly tests easier to maintain.
Having that said, I can understand why one might not want to use it. And that there are some things in there that might be improved.
Quoted 56 lines
There's one thing it can do, that is when calling
ksft_finished()it can be used to know whether all tests are run, but sadly here we're calculating everything just to make it match.. so it loses its last purpose.. IMHO.Quoted 51 lines
Basic "feature availability" checks would go first (is uffd even around?), and depending on that you can set the plan.
For everything else, you can skip instead of test, so it will still be accounted towards the plan.
Quoted 3 lines
I can call
ksft_set_plan()later than this, but it misses a few tests which also looks weird.Yeah, it would be nice to simply make
ksft_set_plan()optional. For example, makeksft_print_cnts()skip the comparison ifksft_plan== 0. At leastksft_exit_skip()handles that already in a descend way (below).Quoted 5 lines
It also seems to not really help anything at all and not obvious to use. E.g.
ksft_finished()will referenceksft_planthen it'll triggerksft_exit_fail()but here I want to make it SKIP if the 1st test failed simply because the kernel probably doesn't haveCONFIG_USERFAULTFD.You'd simply do that availability check first and then use
ksft_exit_skip()in case not available I guess.Quoted 4 lines
Another example: I never figured what does x{fail|pass|skip} meant in the header.. e.g.
ksft_inc_xfail_cnt()is used nowhere so I cannot reference either. Then I don't know when I should increase them.In
cow.cI have the following flow:
ksft_print_header();ksft_set_plan();
... tests ...
err =ksft_get_fail_cnt();
if (err)ksft_exit_fail_msg();return
ksft_exit_pass();That gives me:
# [INFO] detected THP size: 2048 KiB
# [INFO] detected hugetlb size: 2048 KiB
# [INFO] detected hugetlb size: 1048576 KiB
# [INFO] huge zeropage is enabled
TAP version 13
1..190
...
# Totals: pass:87 fail:0 xfail:0 xpass:0 skip:103 error:0
Quoted 7 lines
Quoted 2 lines
I didn't use xfail or xpass so far, but what I understood is that these are "expected failures" and "expected passes". fail/pass/skip are straight
Yes, xfail can be expressed that way, but maybe not xpass? Otherwise it's hard to identify what's the difference between xpass and pass, because IIUC pass also means "expected to pass".
I'm a simple man, I use pass/fail/skip in my tests. But let's try figuring that out; a quick internet search (no idea how trustworthy) tells me that I was wrong about xpass:
xfail: expected failure
xpass: unexpected pass
it essentially is:
xfail -> pass
xpass -> fail
but with a slight semantic difference when thinking about a test case:
ret = mmap(0, 0 ...);
if (ret == MAP_FAILED) {
XFAIL();
} else {
XPASS();
}vs.
ret = mmap(0, 0 ...);
if (ret == MAP_FAILED) {
PASS();
} else {
FAIL();
}It's all inherited from other testing frameworks I think. And xpass seems to be completely unused and xfail mostly unused.
So I wouldn't worry about that and simply use pass/fail/skip.
Quoted 22 lines
Quoted 22 lines
forward. ksft_test_result_fail()/ksft_test_result_pass()/ksft_test_result_skip() are used to set them.
You'd do availability checks before
ksft_set_plan()and fail with aksft_exit_skip()if the kernel doesn't support it. Then, you'd just use ksft_test_result_fail()/ksft_test_result_pass()/ksft_test_result_skip().Quoted 14 lines
In short, to make the unit test behave as expected, I figured I'll just write these few helpers and that's good enough for this unit test. That takes perhaps 5 min anyway and isn't hugely bad for an unit test.
Then I keep the exit code matching kselftests (
KSFT_SKIP, etc.).What I can do here, though, is at least reuse the counters, e.g:
ksft_inc_pass_cnt() / ksft_inc_fail_cnt()There's no
ksft_inc_skip_cnt()so, maybe, I can just reuseksft_inc_xskip_cnt()assuming that counts "skip"s?
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Let me know if you have better ideas, I'll be happy to switch in that case.
I guess once you start manually increasing/decreasing the cnt, you might be abusing the ksft framework indeed and are better off handling it differently :D
I'm serious considering that to address your comment here, to show that I'm trying my best to use whatever can help in this test case. :) Here reusing
:) appreciated, but don't let my comments distract you. If you don't think ksft is a good fit (or any good), then don't use it.
Signature · 3 lines
Thanks, David / dhildenb
Peter Xu
reviewerRe: [PATCH 16/29] selftests/mm: UFFDIO_API test
In reply to Peter Xutools/testing/selftests/mm/vm_util.c int uffd_open_sys(unsigned int flags) · line 266
Quoted 9 lines · Peter Xu
tools/testing/selftests/mm/vm_util.c +9
-0+int uffd_open (unsigned int flags) +{ +int uffd =uffd_open_sys (flags); + +if (uffd <0 ) + uffd =uffd_open_dev (flags); + +return uffd; +}
A (benign) accident when rebasing here.. I'll move this function into "selftests/mm: Add framework for uffd-unit-test" which is its first usage.
Signature · 1 line
Peter Xu
Peter Xu
reviewerPATCH 17/29 selftests/mm: Drop global mem_fd in uffd tests
In reply to Peter XuDrop it by creating the memfd dynamically in the tests.
--- tools/testing/selftests/mm/uffd-common.c | 28 +++++++++++++++++++++++- tools/testing/selftests/mm/uffd-common.h | 2 +- tools/testing/selftests/mm/uffd-stress.c | 15 ------------- 3 files changed, 28 insertions(+), 17 deletions(-)
tools/testing/selftests/mm/uffd-common.c +27 -1
@@ -12,12 +12,32 @@volatile bool test_uffdio_copy_eexist = true;unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size, hpage_size;char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap; -int mem_fd, uffd =-1 , uffd_flags, finished, *pipefd, test_type; +int uffd =-1 , uffd_flags, finished, *pipefd, test_type;bool map_shared, test_collapse, test_dev_userfaultfd;bool test_uffdio_wp = true, test_uffdio_minor = false;unsigned long long *count_verify;uffd_test_ops_t *uffd_test_ops; +static int uffd_mem_fd_create (off_t mem_size,bool hugetlb) +{ +unsigned int memfd_flags =0 ; +int mem_fd; + +if (hugetlb) + memfd_flags =MFD_HUGETLB ; + mem_fd =memfd_create ("uffd-test" , memfd_flags); +if (mem_fd <0 ) +err ("memfd_create" ); +if (ftruncate (mem_fd, mem_size)) +err ("ftruncate" ); +if (fallocate (mem_fd, +FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE ,0 , + mem_size)) +err ("fallocate" ); + +return mem_fd; +} +static void anon_release_pages (char *rel_area) {if (madvise (rel_area, nr_pages * page_size,MADV_DONTNEED ))
@@ -51,6 +71,7 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src)off_t offset = is_src ?0 : size;void *area_alias =NULL ;char **alloc_area_alias; +int mem_fd =uffd_mem_fd_create (size *2 , true); *alloc_area = mmap(NULL, size, PROT_READ | PROT_WRITE, (map_shared ? MAP_SHARED : MAP_PRIVATE) |
@@ -73,6 +94,8 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src) }if (area_alias) *alloc_area_alias = area_alias; + +close (mem_fd); }static void hugetlb_alias_mapping (__u64 *start,size_t len,unsigned long offset)
@@ -95,6 +118,7 @@ static void shmem_allocate_area(void **alloc_area, bool is_src)size_t bytes = nr_pages * page_size;unsigned long offset = is_src ?0 : bytes;char *p =NULL , *p_alias =NULL ; +int mem_fd =uffd_mem_fd_create (bytes *2 , false);if (test_collapse) { p =BASE_PMD_ADDR ;
@@ -124,6 +148,8 @@ static void shmem_allocate_area(void **alloc_area, bool is_src) area_src_alias = area_alias; else area_dst_alias = area_alias; + +close (mem_fd); }static void shmem_alias_mapping (__u64 *start,size_t len,unsigned long offset)
tools/testing/selftests/mm/uffd-common.h +1 -1
@@ -87,7 +87,7 @@ typedef struct uffd_test_ops uffd_test_ops_t;extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size, hpage_size;extern char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap; -extern int mem_fd, uffd, uffd_flags, finished, *pipefd, test_type; +extern int uffd, uffd_flags, finished, *pipefd, test_type;extern bool map_shared, test_collapse, test_dev_userfaultfd;extern bool test_uffdio_wp, test_uffdio_minor;extern unsigned long long *count_verify;
tools/testing/selftests/mm/uffd-stress.c +0 -15
@@ -1090,21 +1090,6 @@ int main(int argc, char **argv) } nr_pages = nr_pages_per_cpu * nr_cpus; -if (test_type ==TEST_SHMEM || test_type ==TEST_HUGETLB ) { -unsigned int memfd_flags =0 ; - -if (test_type ==TEST_HUGETLB ) - memfd_flags =MFD_HUGETLB ; - mem_fd =memfd_create (argv[0 ], memfd_flags); -if (mem_fd <0 ) -err ("memfd_create" ); -if (ftruncate (mem_fd, nr_pages * page_size *2 )) -err ("ftruncate" ); -if (fallocate (mem_fd, -FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE ,0 , - nr_pages * page_size *2 )) -err ("fallocate" ); - }printf ("nr_pages: %lu, nr_pages_per_cpu: %lu\n" , nr_pages, nr_pages_per_cpu);return userfaultfd_stress ();
Signature · 1 line
2.39.1
Re: [PATCH 17/29] selftests/mm: Drop global mem_fd in uffd tests
Quoted 3 lines · Peter Xu
Drop it by creating the memfd dynamically in the tests.
Signed-off-by Peter Xu <[email protected]>
Quoted 120 lines
--- tools/testing/selftests/mm/uffd-common.c | 28 +++++++++++++++++++++++- tools/testing/selftests/mm/uffd-common.h | 2 +- tools/testing/selftests/mm/uffd-stress.c | 15 ------------- 3 files changed, 28 insertions(+), 17 deletions(-)tools/testing/selftests/mm/uffd-common.c +27
-1@@ -12,12 +12,32 @@volatile bool test_uffdio_copy_eexist = true;unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size, hpage_size;char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap; -int mem_fd, uffd =-1 , uffd_flags, finished, *pipefd, test_type; +int uffd =-1 , uffd_flags, finished, *pipefd, test_type;bool map_shared, test_collapse, test_dev_userfaultfd;bool test_uffdio_wp = true, test_uffdio_minor = false;unsigned long long *count_verify;uffd_test_ops_t *uffd_test_ops; +static int uffd_mem_fd_create (off_t mem_size,bool hugetlb) +{ +unsigned int memfd_flags =0 ; +int mem_fd; + +if (hugetlb) + memfd_flags =MFD_HUGETLB ; + mem_fd =memfd_create ("uffd-test" , memfd_flags); +if (mem_fd <0 ) +err ("memfd_create" ); +if (ftruncate (mem_fd, mem_size)) +err ("ftruncate" ); +if (fallocate (mem_fd, +FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE ,0 , + mem_size)) +err ("fallocate" ); + +return mem_fd; +} +static void anon_release_pages (char *rel_area) {if (madvise (rel_area, nr_pages * page_size,MADV_DONTNEED ))@@ -51,6 +71,7 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src)off_t offset = is_src ?0 : size;void *area_alias =NULL ;char **alloc_area_alias; +int mem_fd =uffd_mem_fd_create (size *2 , true); *alloc_area = mmap(NULL, size, PROT_READ | PROT_WRITE, (map_shared ? MAP_SHARED : MAP_PRIVATE) |@@ -73,6 +94,8 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src) }if (area_alias) *alloc_area_alias = area_alias; + +close (mem_fd); }static void hugetlb_alias_mapping (__u64 *start,size_t len,unsigned long offset)@@ -95,6 +118,7 @@ static void shmem_allocate_area(void **alloc_area, bool is_src)size_t bytes = nr_pages * page_size;unsigned long offset = is_src ?0 : bytes;char *p =NULL , *p_alias =NULL ; +int mem_fd =uffd_mem_fd_create (bytes *2 , false);if (test_collapse) { p =BASE_PMD_ADDR ;@@ -124,6 +148,8 @@ static void shmem_allocate_area(void **alloc_area, bool is_src) area_src_alias = area_alias; else area_dst_alias = area_alias; + +close (mem_fd); }static void shmem_alias_mapping (__u64 *start,size_t len,unsigned long offset)tools/testing/selftests/mm/uffd-common.h +1
-1@@ -87,7 +87,7 @@ typedef struct uffd_test_ops uffd_test_ops_t;extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size, hpage_size;extern char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap; -extern int mem_fd, uffd, uffd_flags, finished, *pipefd, test_type; +extern int uffd, uffd_flags, finished, *pipefd, test_type;extern bool map_shared, test_collapse, test_dev_userfaultfd;extern bool test_uffdio_wp, test_uffdio_minor;extern unsigned long long *count_verify;tools/testing/selftests/mm/uffd-stress.c +0
-15@@ -1090,21 +1090,6 @@ int main(int argc, char **argv) } nr_pages = nr_pages_per_cpu * nr_cpus; -if (test_type ==TEST_SHMEM || test_type ==TEST_HUGETLB ) { -unsigned int memfd_flags =0 ; - -if (test_type ==TEST_HUGETLB ) - memfd_flags =MFD_HUGETLB ; - mem_fd =memfd_create (argv[0 ], memfd_flags); -if (mem_fd <0 ) -err ("memfd_create" ); -if (ftruncate (mem_fd, nr_pages * page_size *2 )) -err ("ftruncate" ); -if (fallocate (mem_fd, -FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE ,0 , - nr_pages * page_size *2 )) -err ("fallocate" ); - }printf ("nr_pages: %lu, nr_pages_per_cpu: %lu\n" , nr_pages, nr_pages_per_cpu);return userfaultfd_stress ();Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 18/29 selftests/mm: Drop global hpage_size in uffd tests
In reply to Peter Xuhpage_size was wrongly used. Sometimes it means hugetlb default size, sometimes it was used as thp size.
Remove the global variable and use the right one at each place.
--- tools/testing/selftests/mm/uffd-common.c | 7 ++++--- tools/testing/selftests/mm/uffd-common.h | 2 +- tools/testing/selftests/mm/uffd-stress.c | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-)
tools/testing/selftests/mm/uffd-common.c +4 -3
@@ -10,7 +10,7 @@#define BASE_PMD_ADDR ((void *)(1UL << 30))volatile bool test_uffdio_copy_eexist = true; -unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size, hpage_size; +unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;int uffd =-1 , uffd_flags, finished, *pipefd, test_type;bool map_shared, test_collapse, test_dev_userfaultfd;
@@ -115,7 +115,7 @@ static void shmem_release_pages(char *rel_area)static void shmem_allocate_area (void **alloc_area,bool is_src) {void *area_alias =NULL ; -size_t bytes = nr_pages * page_size; +size_t bytes = nr_pages * page_size, hpage_size =read_pmd_pagesize ();unsigned long offset = is_src ?0 : bytes;char *p =NULL , *p_alias =NULL ;int mem_fd =uffd_mem_fd_create (bytes *2 , false);
@@ -159,7 +159,8 @@ static void shmem_alias_mapping(__u64 *start, size_t len, unsigned long offset)static void shmem_check_pmd_mapping (void *p,int expect_nr_hpages) { -if (!check_huge_shmem (area_dst_alias, expect_nr_hpages, hpage_size)) +if (!check_huge_shmem (area_dst_alias, expect_nr_hpages, +read_pmd_pagesize ()))err ("Did not find expected %d number of hugepages" , expect_nr_hpages); }
tools/testing/selftests/mm/uffd-common.h +1 -1
@@ -85,7 +85,7 @@ struct uffd_test_ops { };typedef struct uffd_test_ops uffd_test_ops_t ; -extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size, hpage_size; +extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;extern char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;extern int uffd, uffd_flags, finished, *pipefd, test_type;extern bool map_shared, test_collapse, test_dev_userfaultfd;
tools/testing/selftests/mm/uffd-stress.c +3 -3
@@ -655,7 +655,7 @@ static int userfaultfd_minor_test(void) uffd_test_ops->check_pmd_mapping (area_dst, nr_pages * page_size / - hpage_size); +read_pmd_pagesize ());/* * This won't cause uffd-fault - it purely just makes sure there * was no corruption.
@@ -997,7 +997,7 @@ static void parse_test_type_arg(const char *raw_type)err ("Unsupported test: %s" , raw_type);if (test_type ==TEST_HUGETLB ) - page_size = hpage_size; + page_size =default_huge_page_size ();else page_size =sysconf (_SC_PAGE_SIZE);
@@ -1035,6 +1035,7 @@ static void sigalrm(int sig)int main (int argc,char **argv) {size_t bytes; +size_t hpage_size =read_pmd_pagesize ();if (argc <4 )usage ();
@@ -1043,7 +1044,6 @@ int main(int argc, char **argv)err ("failed to arm SIGALRM" );alarm (ALARM_INTERVAL_SECS ); - hpage_size =default_huge_page_size ();parse_test_type_arg (argv[1 ]); bytes =atol (argv[2 ]) *1024 *1024 ;
Signature · 1 line
2.39.1
Re: [PATCH 18/29] selftests/mm: Drop global hpage_size in uffd tests
Quoted 6 lines · Peter Xu
hpage_sizewas wrongly used. Sometimes it means hugetlb default size, sometimes it was used as thp size.Remove the global variable and use the right one at each place.
Signed-off-by Peter Xu <[email protected]>
Quoted 92 lines
--- tools/testing/selftests/mm/uffd-common.c | 7 ++++--- tools/testing/selftests/mm/uffd-common.h | 2 +- tools/testing/selftests/mm/uffd-stress.c | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-)tools/testing/selftests/mm/uffd-common.c +4
-3@@ -10,7 +10,7 @@#define BASE_PMD_ADDR ((void *)(1UL << 30))volatile bool test_uffdio_copy_eexist = true; -unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size, hpage_size; +unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;int uffd =-1 , uffd_flags, finished, *pipefd, test_type;bool map_shared, test_collapse, test_dev_userfaultfd;@@ -115,7 +115,7 @@ static void shmem_release_pages(char *rel_area)static void shmem_allocate_area (void **alloc_area,bool is_src) {void *area_alias =NULL ; -size_t bytes = nr_pages * page_size; +size_t bytes = nr_pages * page_size, hpage_size =read_pmd_pagesize ();unsigned long offset = is_src ?0 : bytes;char *p =NULL , *p_alias =NULL ;int mem_fd =uffd_mem_fd_create (bytes *2 , false);@@ -159,7 +159,8 @@ static void shmem_alias_mapping(__u64 *start, size_t len, unsigned long offset)static void shmem_check_pmd_mapping (void *p,int expect_nr_hpages) { -if (!check_huge_shmem (area_dst_alias, expect_nr_hpages, hpage_size)) +if (!check_huge_shmem (area_dst_alias, expect_nr_hpages, +read_pmd_pagesize ()))err ("Did not find expected %d number of hugepages" , expect_nr_hpages); }tools/testing/selftests/mm/uffd-common.h +1
-1@@ -85,7 +85,7 @@ struct uffd_test_ops { };typedef struct uffd_test_ops uffd_test_ops_t ; -extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size, hpage_size; +extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;extern char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;extern int uffd, uffd_flags, finished, *pipefd, test_type;extern bool map_shared, test_collapse, test_dev_userfaultfd;tools/testing/selftests/mm/uffd-stress.c +3
-3@@ -655,7 +655,7 @@ static int userfaultfd_minor_test(void) uffd_test_ops->check_pmd_mapping (area_dst, nr_pages * page_size / - hpage_size); +read_pmd_pagesize ());/* * This won't cause uffd-fault - it purely just makes sure there * was no corruption. @@ -997,7 +997,7 @@ static void parse_test_type_arg(const char *raw_type)err ("Unsupported test: %s" , raw_type);if (test_type ==TEST_HUGETLB ) - page_size = hpage_size; + page_size =default_huge_page_size ();else page_size =sysconf (_SC_PAGE_SIZE);@@ -1035,6 +1035,7 @@ static void sigalrm(int sig)int main (int argc,char **argv) {size_t bytes; +size_t hpage_size =read_pmd_pagesize ();if (argc <4 )usage ();@@ -1043,7 +1044,6 @@ int main(int argc, char **argv)err ("failed to arm SIGALRM" );alarm (ALARM_INTERVAL_SECS ); - hpage_size =default_huge_page_size ();parse_test_type_arg (argv[1 ]); bytes =atol (argv[2 ]) *1024 *1024 ;Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 19/29 selftests/mm: Let uffd_handle_page_fault() takes wp parameter
In reply to Peter XuMake the handler optionally apply WP bit when resolving page faults for either missing or minor page faults. This move towards removing global test_uffdio_wp outside of the common code.
For this, slightly abuse uffd_stats to keep one more parameter on whether we'd like to resolve page faults with WP bit set. Note that only the name is abused, it'll be better to be called uffd_args or similar but let's not bother for now.
--- tools/testing/selftests/mm/uffd-common.c | 17 +++++++++-------- tools/testing/selftests/mm/uffd-common.h | 6 ++++-- tools/testing/selftests/mm/uffd-stress.c | 16 ++++++++++------ 3 files changed, 23 insertions(+), 16 deletions(-)
tools/testing/selftests/mm/uffd-common.c +9 -8
@@ -353,7 +353,7 @@ void wp_range(int ufd, __u64 start, __u64 len, bool wp)err ("clear WP failed: address=0x%" PRIx64, (uint64_t )start); } -static void continue_range (int ufd,__u64 start,__u64 len) +static void continue_range (int ufd,__u64 start,__u64 len,bool wp) {struct uffdio_continue req;int ret;
@@ -361,7 +361,7 @@ static void continue_range(int ufd, __u64 start, __u64 len) req.range.start = start; req.range.len = len; req.mode =0 ; -if (test_uffdio_wp) +if (wp) req.mode |=UFFDIO_CONTINUE_MODE_WP ;if (ioctl (ufd,UFFDIO_CONTINUE , &req))
@@ -429,7 +429,8 @@ void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_stats *stats) area_dst_alias));for (b =0 ; b < page_size; ++b) area[b] = ~area[b]; -continue_range (uffd, msg->arg.pagefault.address, page_size); +continue_range (uffd, msg->arg.pagefault.address, page_size, + stats->apply_wp); stats->minor_faults++; }else {/*
@@ -459,7 +460,7 @@ void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_stats *stats) offset = (char *)(unsigned long )msg->arg.pagefault.address - area_dst; offset &= ~(page_size-1 ); -if (copy_page (uffd, offset)) +if (copy_page (uffd, offset, stats->apply_wp)) stats->missing_faults++; } }
@@ -555,7 +556,7 @@ static void wake_range(int ufd, unsigned long addr, unsigned long len) addr), exit(1); } -int __copy_page (int ufd,unsigned long offset,bool retry) +int __copy_page (int ufd,unsigned long offset,bool retry,bool wp) {struct uffdio_copy uffdio_copy;
@@ -564,7 +565,7 @@ int __copy_page(int ufd, unsigned long offset, bool retry) uffdio_copy.dst = (unsigned long ) area_dst + offset; uffdio_copy.src = (unsigned long ) area_src + offset; uffdio_copy.len = page_size; -if (test_uffdio_wp) +if (wp) uffdio_copy.mode =UFFDIO_COPY_MODE_WP ;else uffdio_copy.mode =0 ;
@@ -587,8 +588,8 @@ int __copy_page(int ufd, unsigned long offset, bool retry)return 0 ; } -int copy_page (int ufd,unsigned long offset) +int copy_page (int ufd,unsigned long offset,bool wp) { -return __copy_page (ufd, offset, false); +return __copy_page (ufd, offset, false, wp); }
tools/testing/selftests/mm/uffd-common.h +4 -2
@@ -72,6 +72,8 @@/* Userfaultfd test statistics */ struct uffd_stats {int cpu; +/* Whether apply wr-protects when installing pages */ +bool apply_wp;unsigned long missing_faults;unsigned long wp_faults;unsigned long minor_faults;
@@ -104,8 +106,8 @@ void userfaultfd_open(uint64_t *features);int uffd_read_msg (int ufd,struct uffd_msg *msg);void wp_range (int ufd,__u64 start,__u64 len,bool wp);void uffd_handle_page_fault (struct uffd_msg *msg,struct uffd_stats *stats); -int __copy_page (int ufd,unsigned long offset,bool retry); -int copy_page (int ufd,unsigned long offset); +int __copy_page (int ufd,unsigned long offset,bool retry,bool wp); +int copy_page (int ufd,unsigned long offset,bool wp);void *uffd_poll_thread (void *arg);#define TEST_ANON 1
tools/testing/selftests/mm/uffd-stress.c +10 -6
@@ -97,6 +97,7 @@ static void uffd_stats_reset(struct uffd_stats *uffd_stats,for (i =0 ; i < n_cpus; i++) { uffd_stats[i].cpu = i; + uffd_stats[i].apply_wp = test_uffdio_wp; uffd_stats[i].missing_faults =0 ; uffd_stats[i].wp_faults =0 ; uffd_stats[i].minor_faults =0 ;
@@ -156,7 +157,7 @@ static void *locking_thread(void *arg)static int copy_page_retry (int ufd,unsigned long offset) { -return __copy_page (ufd, offset, true); +return __copy_page (ufd, offset, true, test_uffdio_wp); }pthread_mutex_t uffd_read_mutex =PTHREAD_MUTEX_INITIALIZER ;
@@ -309,7 +310,7 @@ static void sighndl(int sig, siginfo_t *siginfo, void *ptr)* This also tests UFFD_FEATURE_EVENT_FORK event along with the signal * feature. Using monitor thread, verify no userfault events are generated. */ -static int faulting_process (int signal_test) +static int faulting_process (int signal_test,bool wp) {unsigned long nr;unsigned long long count;
@@ -344,7 +345,7 @@ static int faulting_process(int signal_test)if (steps ==1 ) {/* This is a MISSING request */ steps++; -if (copy_page (uffd, offset)) +if (copy_page (uffd, offset, wp)) signalled++; }else {/* This is a WP request */
@@ -508,6 +509,7 @@ static int userfaultfd_events_test(void) true, test_uffdio_wp, false))err ("register failure" ); + stats.apply_wp = test_uffdio_wp;if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats))err ("uffd_poll_thread create" );
@@ -516,7 +518,7 @@ static int userfaultfd_events_test(void)err ("fork" );if (!pid) -exit (faulting_process (0 )); +exit (faulting_process (0 , test_uffdio_wp));waitpid (pid, &err,0 );if (err)
@@ -552,11 +554,12 @@ static int userfaultfd_sig_test(void) true, test_uffdio_wp, false))err ("register failure" ); -if (faulting_process (1 )) +if (faulting_process (1 , test_uffdio_wp))err ("faulting process failed" ); uffd_test_ops->release_pages (area_dst); + stats.apply_wp = test_uffdio_wp;if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats))err ("uffd_poll_thread create" );
@@ -565,7 +568,7 @@ static int userfaultfd_sig_test(void)err ("fork" );if (!pid) -exit (faulting_process (2 )); +exit (faulting_process (2 , test_uffdio_wp));waitpid (pid, &err,0 );if (err)
@@ -629,6 +632,7 @@ static int userfaultfd_minor_test(void) page_size); } + stats.apply_wp = test_uffdio_wp;if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats))err ("uffd_poll_thread create" );
Signature · 1 line
2.39.1
Re: [PATCH 19/29] selftests/mm: Let uffd_handle_page_fault() takes wp parameter
Quoted 1 line · Peter Xu
Subject: selftests/mm: Let
uffd_handle_page_fault()takes wp parameter
Nit: ^ take
Quoted 2 lines
Make the handler optionally apply WP bit when resolving page faults for either missing or minor page faults. This move towards removing global
Nit: ^ moves
Quoted 6 lines
test_uffdio_wpoutside of the common code.For this, slightly abuse
uffd_statsto keep one more parameter on whether we'd like to resolve page faults with WP bit set. Note that only the name is abused, it'll be better to be calleduffd_argsor similar but let's not bother for now.
Maybe one of the first commits in the series should have been s/uffd_stats/uffd_args/g, but I realize that it's PITA, so I won't insist.
Quoted 191 lines
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/uffd-common.c | 17 +++++++++-------- tools/testing/selftests/mm/uffd-common.h | 6 ++++-- tools/testing/selftests/mm/uffd-stress.c | 16 ++++++++++------ 3 files changed, 23 insertions(+), 16 deletions(-)tools/testing/selftests/mm/uffd-common.c +9
-8@@ -353,7 +353,7 @@ void wp_range(int ufd, __u64 start, __u64 len, bool wp)err ("clear WP failed: address=0x%" PRIx64, (uint64_t )start); } -static void continue_range (int ufd,__u64 start,__u64 len) +static void continue_range (int ufd,__u64 start,__u64 len,bool wp) {struct uffdio_continue req;int ret;@@ -361,7 +361,7 @@ static void continue_range(int ufd, __u64 start, __u64 len) req.range.start = start; req.range.len = len; req.mode =0 ; -if (test_uffdio_wp) +if (wp) req.mode |=UFFDIO_CONTINUE_MODE_WP ;if (ioctl (ufd,UFFDIO_CONTINUE , &req))@@ -429,7 +429,8 @@ void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_stats *stats) area_dst_alias));for (b =0 ; b < page_size; ++b) area[b] = ~area[b]; -continue_range (uffd, msg->arg.pagefault.address, page_size); +continue_range (uffd, msg->arg.pagefault.address, page_size, + stats->apply_wp); stats->minor_faults++; }else {/* @@ -459,7 +460,7 @@ void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_stats *stats) offset = (char *)(unsigned long )msg->arg.pagefault.address - area_dst; offset &= ~(page_size-1 ); -if (copy_page (uffd, offset)) +if (copy_page (uffd, offset, stats->apply_wp)) stats->missing_faults++; } }@@ -555,7 +556,7 @@ static void wake_range(int ufd, unsigned long addr, unsigned long len) addr), exit(1); } -int __copy_page (int ufd,unsigned long offset,bool retry) +int __copy_page (int ufd,unsigned long offset,bool retry,bool wp) {struct uffdio_copy uffdio_copy;@@ -564,7 +565,7 @@ int __copy_page(int ufd, unsigned long offset, bool retry) uffdio_copy.dst = (unsigned long ) area_dst + offset; uffdio_copy.src = (unsigned long ) area_src + offset; uffdio_copy.len = page_size; -if (test_uffdio_wp) +if (wp) uffdio_copy.mode =UFFDIO_COPY_MODE_WP ;else uffdio_copy.mode =0 ;@@ -587,8 +588,8 @@ int __copy_page(int ufd, unsigned long offset, bool retry)return 0 ; } -int copy_page (int ufd,unsigned long offset) +int copy_page (int ufd,unsigned long offset,bool wp) { -return __copy_page (ufd, offset, false); +return __copy_page (ufd, offset, false, wp); }tools/testing/selftests/mm/uffd-common.h +4
-2@@ -72,6 +72,8 @@/* Userfaultfd test statistics */ struct uffd_stats {int cpu; +/* Whether apply wr-protects when installing pages */ +bool apply_wp;unsigned long missing_faults;unsigned long wp_faults;unsigned long minor_faults;@@ -104,8 +106,8 @@ void userfaultfd_open(uint64_t *features);int uffd_read_msg (int ufd,struct uffd_msg *msg);void wp_range (int ufd,__u64 start,__u64 len,bool wp);void uffd_handle_page_fault (struct uffd_msg *msg,struct uffd_stats *stats); -int __copy_page (int ufd,unsigned long offset,bool retry); -int copy_page (int ufd,unsigned long offset); +int __copy_page (int ufd,unsigned long offset,bool retry,bool wp); +int copy_page (int ufd,unsigned long offset,bool wp);void *uffd_poll_thread (void *arg);#define TEST_ANON 1tools/testing/selftests/mm/uffd-stress.c +10
-6@@ -97,6 +97,7 @@ static void uffd_stats_reset(struct uffd_stats *uffd_stats,for (i =0 ; i < n_cpus; i++) { uffd_stats[i].cpu = i; + uffd_stats[i].apply_wp = test_uffdio_wp; uffd_stats[i].missing_faults =0 ; uffd_stats[i].wp_faults =0 ; uffd_stats[i].minor_faults =0 ;@@ -156,7 +157,7 @@ static void *locking_thread(void *arg)static int copy_page_retry (int ufd,unsigned long offset) { -return __copy_page (ufd, offset, true); +return __copy_page (ufd, offset, true, test_uffdio_wp); }pthread_mutex_t uffd_read_mutex =PTHREAD_MUTEX_INITIALIZER ;@@ -309,7 +310,7 @@ static void sighndl(int sig, siginfo_t *siginfo, void *ptr)* This also tests UFFD_FEATURE_EVENT_FORK event along with the signal * feature. Using monitor thread, verify no userfault events are generated. */ -static int faulting_process (int signal_test) +static int faulting_process (int signal_test,bool wp) {unsigned long nr;unsigned long long count;@@ -344,7 +345,7 @@ static int faulting_process(int signal_test)if (steps ==1 ) {/* This is a MISSING request */ steps++; -if (copy_page (uffd, offset)) +if (copy_page (uffd, offset, wp)) signalled++; }else {/* This is a WP request */ @@ -508,6 +509,7 @@ static int userfaultfd_events_test(void) true, test_uffdio_wp, false))err ("register failure" ); + stats.apply_wp = test_uffdio_wp;if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats))err ("uffd_poll_thread create" );@@ -516,7 +518,7 @@ static int userfaultfd_events_test(void)err ("fork" );if (!pid) -exit (faulting_process (0 )); +exit (faulting_process (0 , test_uffdio_wp));waitpid (pid, &err,0 );if (err)@@ -552,11 +554,12 @@ static int userfaultfd_sig_test(void) true, test_uffdio_wp, false))err ("register failure" ); -if (faulting_process (1 )) +if (faulting_process (1 , test_uffdio_wp))err ("faulting process failed" ); uffd_test_ops->release_pages (area_dst); + stats.apply_wp = test_uffdio_wp;if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats))err ("uffd_poll_thread create" );@@ -565,7 +568,7 @@ static int userfaultfd_sig_test(void)err ("fork" );if (!pid) -exit (faulting_process (2 )); +exit (faulting_process (2 , test_uffdio_wp));waitpid (pid, &err,0 );if (err)@@ -629,6 +632,7 @@ static int userfaultfd_minor_test(void) page_size); } + stats.apply_wp = test_uffdio_wp;if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats))err ("uffd_poll_thread create" );Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerRe: [PATCH 19/29] selftests/mm: Let uffd_handle_page_fault() takes wp parameter
Quoted 9 lines · Mike Rapoport
Quoted 1 line · Peter Xu
Subject: selftests/mm: Let
uffd_handle_page_fault()takes wp parameterNit: ^ take
Quoted 2 lines
Make the handler optionally apply WP bit when resolving page faults for either missing or minor page faults. This move towards removing global
Nit: ^ moves
Will fix.
Quoted 10 lines
Quoted 7 lines
test_uffdio_wpoutside of the common code.For this, slightly abuse
uffd_statsto keep one more parameter on whether we'd like to resolve page faults with WP bit set. Note that only the name is abused, it'll be better to be calleduffd_argsor similar but let's not bother for now.Maybe one of the first commits in the series should have been s/uffd_stats/uffd_args/g, but I realize that it's PITA, so I won't insist.
It's fine, I'll add one patch. The rebase does take some more minutes, but it's manageable. I'll drop this paragraph then.
Thanks,
Signature · 1 line
Peter Xu
Peter Xu
reviewerPATCH 20/29 selftests/mm: Allow allocate_area() to fail properly
In reply to Peter XuMostly to detect hugetlb allocation errors and skip hugetlb tests when pages are not allocated.
--- tools/testing/selftests/mm/uffd-common.c | 32 +++++++++++++++++------- tools/testing/selftests/mm/uffd-common.h | 4 +-- 2 files changed, 25 insertions(+), 11 deletions(-)
tools/testing/selftests/mm/uffd-common.c +23 -9
@@ -44,10 +44,13 @@ static void anon_release_pages(char *rel_area)err ("madvise(MADV_DONTNEED) failed" ); } -static void anon_allocate_area (void **alloc_area,bool is_src) +static int anon_allocate_area (void **alloc_area,bool is_src) { *alloc_area =mmap (NULL , nr_pages * page_size,PROT_READ |PROT_WRITE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); +if (*alloc_area ==MAP_FAILED ) +err ("ENOMEM" ); +return 0 ; }static void noop_alias_mapping (__u64 *start,size_t len,unsigned long offset)
@@ -65,7 +68,7 @@ static void hugetlb_release_pages(char *rel_area) } } -static void hugetlb_allocate_area (void **alloc_area,bool is_src) +static int hugetlb_allocate_area (void **alloc_area,bool is_src) {off_t size = nr_pages * page_size;off_t offset = is_src ?0 : size;
@@ -77,14 +80,16 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src) (map_shared ?MAP_SHARED :MAP_PRIVATE ) | (is_src ?0 :MAP_NORESERVE ), mem_fd, offset); -if (*alloc_area ==MAP_FAILED ) -err ("mmap of hugetlbfs file failed" ); +if (*alloc_area ==MAP_FAILED ) { + *alloc_area =NULL ; +return -errno; + }if (map_shared) { area_alias =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_SHARED , mem_fd, offset);if (area_alias ==MAP_FAILED ) -err ("mmap of hugetlb file alias failed" ); +return -errno; }if (is_src) {
@@ -96,6 +101,7 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src) *alloc_area_alias = area_alias;close (mem_fd); +return 0 ; }static void hugetlb_alias_mapping (__u64 *start,size_t len,unsigned long offset)
@@ -112,7 +118,7 @@ static void shmem_release_pages(char *rel_area)err ("madvise(MADV_REMOVE) failed" ); } -static void shmem_allocate_area (void **alloc_area,bool is_src) +static int shmem_allocate_area (void **alloc_area,bool is_src) {void *area_alias =NULL ;size_t bytes = nr_pages * page_size, hpage_size =read_pmd_pagesize ();
@@ -150,6 +156,7 @@ static void shmem_allocate_area(void **alloc_area, bool is_src) area_dst_alias = area_alias;close (mem_fd); +return 0 ; }static void shmem_alias_mapping (__u64 *start,size_t len,unsigned long offset)
@@ -282,14 +289,19 @@ static void uffd_test_ctx_clear(void)munmap_area ((void **)&area_remap); } -void uffd_test_ctx_init (uint64_t features) +int uffd_test_ctx_init (uint64_t features) {unsigned long nr, cpu; +int ret;uffd_test_ctx_clear (); - uffd_test_ops->allocate_area ((void **)&area_src, true); - uffd_test_ops->allocate_area ((void **)&area_dst, false); + ret = uffd_test_ops->allocate_area ((void **)&area_src, true); +if (ret) +return ret; + ret = uffd_test_ops->allocate_area ((void **)&area_dst, false); +if (ret) +return ret;userfaultfd_open (&features);
@@ -337,6 +349,8 @@ void uffd_test_ctx_init(uint64_t features)for (cpu =0 ; cpu < nr_cpus; cpu++)if (pipe2 (&pipefd[cpu *2 ],O_CLOEXEC |O_NONBLOCK ))err ("pipe" ); + +return 0 ; }void wp_range (int ufd,__u64 start,__u64 len,bool wp)
tools/testing/selftests/mm/uffd-common.h +2 -2
@@ -80,7 +80,7 @@ struct uffd_stats { };struct uffd_test_ops { -void (*allocate_area)(void **alloc_area,bool is_src); +int (*allocate_area)(void **alloc_area,bool is_src);void (*release_pages)(char *rel_area);void (*alias_mapping)(__u64 *start,size_t len,unsigned long offset);void (*check_pmd_mapping)(void *p,int expect_nr_hpages);
@@ -101,7 +101,7 @@ extern uffd_test_ops_t hugetlb_uffd_test_ops;extern uffd_test_ops_t *uffd_test_ops;void uffd_stats_report (struct uffd_stats *stats,int n_cpus); -void uffd_test_ctx_init (uint64_t features); +int uffd_test_ctx_init (uint64_t features);void userfaultfd_open (uint64_t *features);int uffd_read_msg (int ufd,struct uffd_msg *msg);void wp_range (int ufd,__u64 start,__u64 len,bool wp);
Signature · 1 line
2.39.1
Re: [PATCH 20/29] selftests/mm: Allow allocate_area() to fail properly
Quoted 2 lines · Peter Xu
Mostly to detect hugetlb allocation errors and skip hugetlb tests when pages are not allocated.
Wouldn't we want to skip anon and shmem tests as well for consistency?
Quoted 136 lines
Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/uffd-common.c | 32 +++++++++++++++++------- tools/testing/selftests/mm/uffd-common.h | 4 +-- 2 files changed, 25 insertions(+), 11 deletions(-)tools/testing/selftests/mm/uffd-common.c +23
-9@@ -44,10 +44,13 @@ static void anon_release_pages(char *rel_area)err ("madvise(MADV_DONTNEED) failed" ); } -static void anon_allocate_area (void **alloc_area,bool is_src) +static int anon_allocate_area (void **alloc_area,bool is_src) { *alloc_area =mmap (NULL , nr_pages * page_size,PROT_READ |PROT_WRITE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); +if (*alloc_area ==MAP_FAILED ) +err ("ENOMEM" ); +return 0 ; }static void noop_alias_mapping (__u64 *start,size_t len,unsigned long offset)@@ -65,7 +68,7 @@ static void hugetlb_release_pages(char *rel_area) } } -static void hugetlb_allocate_area (void **alloc_area,bool is_src) +static int hugetlb_allocate_area (void **alloc_area,bool is_src) {off_t size = nr_pages * page_size;off_t offset = is_src ?0 : size;@@ -77,14 +80,16 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src) (map_shared ?MAP_SHARED :MAP_PRIVATE ) | (is_src ?0 :MAP_NORESERVE ), mem_fd, offset); -if (*alloc_area ==MAP_FAILED ) -err ("mmap of hugetlbfs file failed" ); +if (*alloc_area ==MAP_FAILED ) { + *alloc_area =NULL ; +return -errno; + }if (map_shared) { area_alias =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_SHARED , mem_fd, offset);if (area_alias ==MAP_FAILED ) -err ("mmap of hugetlb file alias failed" ); +return -errno; }if (is_src) {@@ -96,6 +101,7 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src) *alloc_area_alias = area_alias;close (mem_fd); +return 0 ; }static void hugetlb_alias_mapping (__u64 *start,size_t len,unsigned long offset)@@ -112,7 +118,7 @@ static void shmem_release_pages(char *rel_area)err ("madvise(MADV_REMOVE) failed" ); } -static void shmem_allocate_area (void **alloc_area,bool is_src) +static int shmem_allocate_area (void **alloc_area,bool is_src) {void *area_alias =NULL ;size_t bytes = nr_pages * page_size, hpage_size =read_pmd_pagesize ();@@ -150,6 +156,7 @@ static void shmem_allocate_area(void **alloc_area, bool is_src) area_dst_alias = area_alias;close (mem_fd); +return 0 ; }static void shmem_alias_mapping (__u64 *start,size_t len,unsigned long offset)@@ -282,14 +289,19 @@ static void uffd_test_ctx_clear(void)munmap_area ((void **)&area_remap); } -void uffd_test_ctx_init (uint64_t features) +int uffd_test_ctx_init (uint64_t features) {unsigned long nr, cpu; +int ret;uffd_test_ctx_clear (); - uffd_test_ops->allocate_area ((void **)&area_src, true); - uffd_test_ops->allocate_area ((void **)&area_dst, false); + ret = uffd_test_ops->allocate_area ((void **)&area_src, true); +if (ret) +return ret; + ret = uffd_test_ops->allocate_area ((void **)&area_dst, false); +if (ret) +return ret;userfaultfd_open (&features);@@ -337,6 +349,8 @@ void uffd_test_ctx_init(uint64_t features)for (cpu =0 ; cpu < nr_cpus; cpu++)if (pipe2 (&pipefd[cpu *2 ],O_CLOEXEC |O_NONBLOCK ))err ("pipe" ); + +return 0 ; }void wp_range (int ufd,__u64 start,__u64 len,bool wp)tools/testing/selftests/mm/uffd-common.h +2
-2@@ -80,7 +80,7 @@ struct uffd_stats { };struct uffd_test_ops { -void (*allocate_area)(void **alloc_area,bool is_src); +int (*allocate_area)(void **alloc_area,bool is_src);void (*release_pages)(char *rel_area);void (*alias_mapping)(__u64 *start,size_t len,unsigned long offset);void (*check_pmd_mapping)(void *p,int expect_nr_hpages);@@ -101,7 +101,7 @@ extern uffd_test_ops_t hugetlb_uffd_test_ops;extern uffd_test_ops_t *uffd_test_ops;void uffd_stats_report (struct uffd_stats *stats,int n_cpus); -void uffd_test_ctx_init (uint64_t features); +int uffd_test_ctx_init (uint64_t features);void userfaultfd_open (uint64_t *features);int uffd_read_msg (int ufd,struct uffd_msg *msg);void wp_range (int ufd,__u64 start,__u64 len,bool wp);Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerRe: [PATCH 20/29] selftests/mm: Allow allocate_area() to fail properly
Quoted 5 lines · Mike Rapoport
Quoted 2 lines · Peter Xu
Mostly to detect hugetlb allocation errors and skip hugetlb tests when pages are not allocated.
Wouldn't we want to skip anon and shmem tests as well for consistency?
Much less possibile (and useful) than hugetlb for sure, but it's indeed more consistent. Will do.
Signature · 1 line
Peter Xu
Peter Xu
reviewerPATCH 21/29 selftests/mm: Add framework for uffd-unit-test
In reply to Peter XuAdd a framework to be prepared to move unit tests from uffd-stress.c into uffd-unit-tests.c. The goal is to allow detection of uffd features for each test, and also loop over specified types of memory that a test support.
--- tools/testing/selftests/mm/uffd-unit-tests.c | 125 +++++++++++++++++++ tools/testing/selftests/mm/vm_util.c | 27 ++++ tools/testing/selftests/mm/vm_util.h | 1 + 3 files changed, 153 insertions(+)
tools/testing/selftests/mm/uffd-unit-tests.c +125 -0
@@ -9,6 +9,66 @@#ifdef __NR_userfaultfd +/* The unit test doesn't need a large or random size, make it 32MB for now */ +#define UFFD_TEST_MEM_SIZE (32UL << 20) + +#define MEM_ANON BIT_ULL(0) +#define MEM_SHMEM BIT_ULL(1) +#define MEM_SHMEM_PRIVATE BIT_ULL(2) +#define MEM_HUGETLB BIT_ULL(3) +#define MEM_HUGETLB_PRIVATE BIT_ULL(4) + +struct mem_type { +const char *name; +unsigned int mem_flag; +uffd_test_ops_t *mem_ops; +bool shared; +}; +typedef struct mem_type mem_type_t ; + +mem_type_t mem_types[] = { + { + .name ="anon" , + .mem_flag =MEM_ANON , + .mem_ops = &anon_uffd_test_ops, + .shared = false, + }, + { + .name ="shmem" , + .mem_flag =MEM_SHMEM , + .mem_ops = &shmem_uffd_test_ops, + .shared = true, + }, + { + .name ="shmem-private" , + .mem_flag =MEM_SHMEM_PRIVATE , + .mem_ops = &shmem_uffd_test_ops, + .shared = false, + }, + { + .name ="hugetlb" , + .mem_flag =MEM_HUGETLB , + .mem_ops = &hugetlb_uffd_test_ops, + .shared = true, + }, + { + .name ="hugetlb-private" , + .mem_flag =MEM_HUGETLB_PRIVATE , + .mem_ops = &hugetlb_uffd_test_ops, + .shared = false, + }, +}; + +/* Returns: UFFD_TEST_* */ +typedef void (*uffd_test_fn )(void ); + +typedef struct { +const char *name; +uffd_test_fn uffd_fn; +unsigned int mem_targets; +uint64_t uffd_feature_required; +}uffd_test_case_t ; +struct {unsigned int pass, skip, fail, total; } uffd_test_acct;
@@ -108,9 +168,50 @@ static int test_uffd_api(bool use_dev)return 1 ; } +/* +* This function initializes the global variables. TODO: remove global +* vars and then remove this. +*/ +static int uffd_setup_environment (uffd_test_case_t *test,mem_type_t *mem_type) +{ + map_shared = mem_type->shared; + uffd_test_ops = mem_type->mem_ops; + +if (mem_type->mem_flag & (MEM_HUGETLB_PRIVATE |MEM_HUGETLB )) + page_size =default_huge_page_size (); +else + page_size =psize (); + + nr_pages =UFFD_TEST_MEM_SIZE / page_size; +/* TODO: remove this global var.. it's so ugly */ + nr_cpus =1 ; + +return uffd_test_ctx_init (test->uffd_feature_required); +} + +static bool uffd_feature_supported (uffd_test_case_t *test) +{ +uint64_t features; + +if (uffd_get_features (&features)) +return false; + +return (features & test->uffd_feature_required) == + test->uffd_feature_required; +} + +uffd_test_case_t uffd_tests[] = { +}; +int main (int argc,char *argv[]) { +int n_tests =sizeof (uffd_tests) /sizeof (uffd_test_case_t); +int n_mems =sizeof (mem_types) /sizeof (mem_type_t); +uffd_test_case_t *test; +mem_type_t *mem_type; +char test_name[128 ];int has_uffd; +int i, j; has_uffd =test_uffd_api (false); has_uffd |=test_uffd_api (true);
@@ -119,7 +220,31 @@ int main(int argc, char *argv[])printf ("Userfaultfd not supported or unprivileged, skip all tests\n" );exit (KSFT_SKIP ); } + +for (i =0 ; i < n_tests; i++) { + test = &uffd_tests[i]; +for (j =0 ; j < n_mems; j++) { + mem_type = &mem_types[j]; +if (!(test->mem_targets & mem_type->mem_flag)) +continue ; +snprintf (test_name,sizeof (test_name), +"%s on %s" , test->name, mem_type->name); + +uffd_test_start (test_name); +if (!uffd_feature_supported (test)) { +uffd_test_skip ("feature missing" ); +continue ; + } +if (uffd_setup_environment (test, mem_type)) { +uffd_test_skip ("memory allocation failed" ); +continue ; + } + test->uffd_fn (); + } + } +uffd_test_report (); +return uffd_test_acct.fail ?KSFT_FAIL :KSFT_PASS ; }
tools/testing/selftests/mm/vm_util.c +27 -0
@@ -264,3 +264,30 @@ int uffd_open(unsigned int flags)return uffd; } + +int uffd_get_features (uint64_t *features) +{ +struct uffdio_api uffdio_api = { .api =UFFD_API , .features =0 }; +/* +* This should by default work in most kernels; the feature list +* will be the same no matter what we pass in here. +*/ +int fd =uffd_open (UFFD_USER_MODE_ONLY ); + +if (fd <0 ) +/* Maybe the kernel is older than user-only mode? */ + fd =uffd_open (0 ); + +if (fd <0 ) +return fd; + +if (ioctl (fd,UFFDIO_API , &uffdio_api)) { +close (fd); +return -errno; + } + + *features = uffdio_api.features; +close (fd); + +return 0 ; +}
tools/testing/selftests/mm/vm_util.h +1 -0
@@ -51,6 +51,7 @@ int uffd_open(unsigned int flags);int uffd_register (int uffd,void *addr,uint64_t len,bool miss,bool wp,bool minor);int uffd_unregister (int uffd,void *addr,uint64_t len); +int uffd_get_features (uint64_t *features);/* * On ppc64 this will only work with radix 2M hugepage size
Signature · 1 line
2.39.1
Re: [PATCH 21/29] selftests/mm: Add framework for uffd-unit-test
Quoted 144 lines · Peter Xu
Add a framework to be prepared to move unit tests from
uffd-stress.cintouffd-unit-tests.c. The goal is to allow detection of uffd features for each test, and also loop over specified types of memory that a test support.Signed-off-by Peter Xu <[email protected]>--- tools/testing/selftests/mm/uffd-unit-tests.c | 125 +++++++++++++++++++ tools/testing/selftests/mm/vm_util.c | 27 ++++ tools/testing/selftests/mm/vm_util.h | 1 + 3 files changed, 153 insertions(+)tools/testing/selftests/mm/uffd-unit-tests.c +108
-0@@ -9,6 +9,66 @@#ifdef __NR_userfaultfd +/* The unit test doesn't need a large or random size, make it 32MB for now */ +#define UFFD_TEST_MEM_SIZE (32UL << 20) + +#define MEM_ANON BIT_ULL(0) +#define MEM_SHMEM BIT_ULL(1) +#define MEM_SHMEM_PRIVATE BIT_ULL(2) +#define MEM_HUGETLB BIT_ULL(3) +#define MEM_HUGETLB_PRIVATE BIT_ULL(4) + +struct mem_type { +const char *name; +unsigned int mem_flag; +uffd_test_ops_t *mem_ops; +bool shared; +}; +typedef struct mem_type mem_type_t ; + +mem_type_t mem_types[] = { + { + .name ="anon" , + .mem_flag =MEM_ANON , + .mem_ops = &anon_uffd_test_ops, + .shared = false, + }, + { + .name ="shmem" , + .mem_flag =MEM_SHMEM , + .mem_ops = &shmem_uffd_test_ops, + .shared = true, + }, + { + .name ="shmem-private" , + .mem_flag =MEM_SHMEM_PRIVATE , + .mem_ops = &shmem_uffd_test_ops, + .shared = false, + }, + { + .name ="hugetlb" , + .mem_flag =MEM_HUGETLB , + .mem_ops = &hugetlb_uffd_test_ops, + .shared = true, + }, + { + .name ="hugetlb-private" , + .mem_flag =MEM_HUGETLB_PRIVATE , + .mem_ops = &hugetlb_uffd_test_ops, + .shared = false, + }, +}; + +/* Returns: UFFD_TEST_* */ +typedef void (*uffd_test_fn )(void ); + +typedef struct { +const char *name; +uffd_test_fn uffd_fn; +unsigned int mem_targets; +uint64_t uffd_feature_required; +}uffd_test_case_t ; +struct {unsigned int pass, skip, fail, total; } uffd_test_acct;@@ -108,9 +168,50 @@ static int test_uffd_api(bool use_dev)return 1 ; } +/* +* This function initializes the global variables. TODO: remove global +* vars and then remove this. +*/ +static int uffd_setup_environment (uffd_test_case_t *test,mem_type_t *mem_type) +{ + map_shared = mem_type->shared; + uffd_test_ops = mem_type->mem_ops; + +if (mem_type->mem_flag & (MEM_HUGETLB_PRIVATE |MEM_HUGETLB )) + page_size =default_huge_page_size (); +else + page_size =psize (); + + nr_pages =UFFD_TEST_MEM_SIZE / page_size; +/* TODO: remove this global var.. it's so ugly */ + nr_cpus =1 ; + +return uffd_test_ctx_init (test->uffd_feature_required); +} + +static bool uffd_feature_supported (uffd_test_case_t *test) +{ +uint64_t features; + +if (uffd_get_features (&features)) +return false; + +return (features & test->uffd_feature_required) == + test->uffd_feature_required; +} + +uffd_test_case_t uffd_tests[] = { +}; +int main (int argc,char *argv[]) { +int n_tests =sizeof (uffd_tests) /sizeof (uffd_test_case_t); +int n_mems =sizeof (mem_types) /sizeof (mem_type_t); +uffd_test_case_t *test; +mem_type_t *mem_type; +char test_name[128 ];int has_uffd; +int i, j; has_uffd =test_uffd_api (false); has_uffd |=test_uffd_api (true);@@ -119,7 +220,31 @@ int main(int argc, char *argv[])printf ("Userfaultfd not supported or unprivileged, skip all tests\n" );exit (KSFT_SKIP ); } + +for (i =0 ; i < n_tests; i++) { + test = &uffd_tests[i]; +for (j =0 ; j < n_mems; j++) { + mem_type = &mem_types[j]; +if (!(test->mem_targets & mem_type->mem_flag)) +continue ;
tools/testing/selftests/mm/uffd-unit-tests.c int main(int argc, char *argv[]) · line 239
Quoted 10 lines
tools/testing/selftests/mm/uffd-unit-tests.c +10
-0+snprintf (test_name,sizeof (test_name), +"%s on %s" , test->name, mem_type->name); + +uffd_test_start (test_name); +if (!uffd_feature_supported (test)) { +uffd_test_skip ("feature missing" ); +continue ; + } +if (uffd_setup_environment (test, mem_type)) { +uffd_test_skip ("memory allocation failed" );
Maybe uffd_test_skip("environment setup failed")?
Quoted 61 lines
tools/testing/selftests/mm/uffd-unit-tests.c +7
-0+continue ; + } + test->uffd_fn (); + } + } +uffd_test_report (); +return uffd_test_acct.fail ?KSFT_FAIL :KSFT_PASS ; }tools/testing/selftests/mm/vm_util.c +27
-0@@ -264,3 +264,30 @@ int uffd_open(unsigned int flags)return uffd; } + +int uffd_get_features (uint64_t *features) +{ +struct uffdio_api uffdio_api = { .api =UFFD_API , .features =0 }; +/* +* This should by default work in most kernels; the feature list +* will be the same no matter what we pass in here. +*/ +int fd =uffd_open (UFFD_USER_MODE_ONLY ); + +if (fd <0 ) +/* Maybe the kernel is older than user-only mode? */ + fd =uffd_open (0 ); + +if (fd <0 ) +return fd; + +if (ioctl (fd,UFFDIO_API , &uffdio_api)) { +close (fd); +return -errno; + } + + *features = uffdio_api.features; +close (fd); + +return 0 ; +}tools/testing/selftests/mm/vm_util.h +1
-0@@ -51,6 +51,7 @@ int uffd_open(unsigned int flags);int uffd_register (int uffd,void *addr,uint64_t len,bool miss,bool wp,bool minor);int uffd_unregister (int uffd,void *addr,uint64_t len); +int uffd_get_features (uint64_t *features);/* * On ppc64 this will only work with radix 2M hugepage size Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerRe: [PATCH 21/29] selftests/mm: Add framework for uffd-unit-test
Quoted 15 lines · Mike Rapoport
Quoted 15 lines
tools/testing/selftests/mm/uffd-unit-tests.c +11
-0@@ -119,7 +220,31 @@ int main(int argc, char *argv[])printf ("Userfaultfd not supported or unprivileged, skip all tests\n" );exit (KSFT_SKIP ); } + +for (i =0 ; i < n_tests; i++) { + test = &uffd_tests[i]; +for (j =0 ; j < n_mems; j++) { + mem_type = &mem_types[j]; +if (!(test->mem_targets & mem_type->mem_flag)) +continue ; +snprintf (test_name,sizeof (test_name), +"%s on %s" , test->name, mem_type->name); + +uffd_test_start (test_name);
Quoted 8 lines
tools/testing/selftests/mm/uffd-unit-tests.c int main(int argc, char *argv[]) · line 239
Quoted 6 lines
tools/testing/selftests/mm/uffd-unit-tests.c +6
-0+if (!uffd_feature_supported (test)) { +uffd_test_skip ("feature missing" ); +continue ; + } +if (uffd_setup_environment (test, mem_type)) { +uffd_test_skip ("memory allocation failed" );Maybe
uffd_test_skip("environment setup failed")?
Sure.
Side note: the line will be changed after "selftests/mm: Allow uffd test to skip properly with no privilege" to contain an errmsg pointer instead, so it won't affect the last result after series applied.
Thanks,
Signature · 1 line
Peter Xu
Peter Xu
reviewerPATCH 22/29 selftests/mm: Move uffd pagemap test to unit test
In reply to Peter XuMove it over and make it split into two tests, one for pagemap and one for the new WP_UNPOPULATED (to be a separate one).
The thp pagemap test wasn't really working (with MADV_HUGEPAGE). Let's just drop it (since it never really worked anyway..) and leave that for later.
--- tools/testing/selftests/mm/uffd-stress.c | 166 ------------------- tools/testing/selftests/mm/uffd-unit-tests.c | 145 ++++++++++++++++ 2 files changed, 145 insertions(+), 166 deletions(-)
tools/testing/selftests/mm/uffd-stress.c +0 -166
@@ -671,157 +671,6 @@ static int userfaultfd_minor_test(void)return stats.missing_faults !=0 || stats.minor_faults != nr_pages; } -static int pagemap_open (void ) -{ -int fd =open ("/proc/self/pagemap" ,O_RDONLY ); - -if (fd <0 ) -err ("open pagemap" ); - -return fd; -} - -/* This macro let __LINE__ works in err() */ -#define pagemap_check_wp (value, wp) do { \ - if (!!(value & PM_UFFD_WP) != wp) \ - err("pagemap uffd-wp bit error: 0x%"PRIx64, value); \ - } while (0) - -static int pagemap_test_fork (bool present) -{ -pid_t child =fork (); -uint64_t value; -int fd, result; - -if (!child) { -/* Open the pagemap fd of the child itself */ - fd =pagemap_open (); - value =pagemap_get_entry (fd, area_dst); -/* -* After fork() uffd-wp bit should be gone as long as we're -* without UFFD_FEATURE_EVENT_FORK -*/ -pagemap_check_wp (value, false); -/* Succeed */ -exit (0 ); - } -waitpid (child, &result,0 ); -return result; -} - -static void userfaultfd_wp_unpopulated_test (int pagemap_fd) -{ -uint64_t value; - -/* Test applying pte marker to anon unpopulated */ -wp_range (uffd, (uint64_t )area_dst, page_size, true); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, true); - -/* Test unprotect on anon pte marker */ -wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); - -/* Test zap on anon marker */ -wp_range (uffd, (uint64_t )area_dst, page_size, true); -if (madvise (area_dst, page_size,MADV_DONTNEED )) -err ("madvise(MADV_DONTNEED) failed" ); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); - -/* Test fault in after marker removed */ - *area_dst =1 ; - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); -/* Drop it to make pte none again */ -if (madvise (area_dst, page_size,MADV_DONTNEED )) -err ("madvise(MADV_DONTNEED) failed" ); - -/* Test read-zero-page upon pte marker */ -wp_range (uffd, (uint64_t )area_dst, page_size, true); - *(volatile char *)area_dst; -/* Drop it to make pte none again */ -if (madvise (area_dst, page_size,MADV_DONTNEED )) -err ("madvise(MADV_DONTNEED) failed" ); -} - -static void userfaultfd_pagemap_test (unsigned int test_pgsize) -{ -int pagemap_fd; -uint64_t value; - -/* Pagemap tests uffd-wp only */ -if (!test_uffdio_wp) -return ; - -/* Not enough memory to test this page size */ -if (test_pgsize > nr_pages * page_size) -return ; - -printf ("testing uffd-wp with pagemap (pgsize=%u): " , test_pgsize); -/* Flush so it doesn't flush twice in parent/child later */ -fflush (stdout); - -uffd_test_ctx_init (UFFD_FEATURE_WP_UNPOPULATED ); - -if (test_pgsize > page_size) { -/* This is a thp test */ -if (madvise (area_dst, nr_pages * page_size,MADV_HUGEPAGE )) -err ("madvise(MADV_HUGEPAGE) failed" ); - }else if (test_pgsize == page_size) { -/* This is normal page test; force no thp */ -if (madvise (area_dst, nr_pages * page_size,MADV_NOHUGEPAGE )) -err ("madvise(MADV_NOHUGEPAGE) failed" ); - } - -if (uffd_register (uffd, area_dst, nr_pages * page_size, - false, true, false)) -err ("register failed" ); - - pagemap_fd =pagemap_open (); - -/* Smoke test WP_UNPOPULATED first when it's still empty */ -if (test_pgsize == page_size) -userfaultfd_wp_unpopulated_test (pagemap_fd); - -/* Touch the page */ - *area_dst =1 ; -wp_range (uffd, (uint64_t )area_dst, test_pgsize, true); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, true); -/* Make sure uffd-wp bit dropped when fork */ -if (pagemap_test_fork (true)) -err ("Detected stall uffd-wp bit in child" ); - -/* Exclusive required or PAGEOUT won't work */ -if (!(value &PM_MMAP_EXCLUSIVE )) -err ("multiple mapping detected: 0x%" PRIx64, value); - -if (madvise (area_dst, test_pgsize,MADV_PAGEOUT )) -err ("madvise(MADV_PAGEOUT) failed" ); - -/* Uffd-wp should persist even swapped out */ - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, true); -/* Make sure uffd-wp bit dropped when fork */ -if (pagemap_test_fork (false)) -err ("Detected stall uffd-wp bit in child" ); - -/* Unprotect; this tests swap pte modifications */ -wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); - -/* Fault in the page from disk */ - *area_dst =2 ; - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); - -close (pagemap_fd); -printf ("done\n" ); -} -static int userfaultfd_stress (void ) {void *area;
@@ -933,21 +782,6 @@ static int userfaultfd_stress(void)uffd_stats_report (uffd_stats, nr_cpus); } -if (test_type ==TEST_ANON ) { -/* -* shmem/hugetlb won't be able to run since they have different -* behavior on fork() (file-backed memory normally drops ptes -* directly when fork), meanwhile the pagemap test will verify -* pgtable entry of fork()ed child. -*/ -userfaultfd_pagemap_test (page_size); -/* -* Hard-code for x86_64 for now for 2M THP, as x86_64 is -* currently the only one that supports uffd-wp -*/ -userfaultfd_pagemap_test (page_size *512 ); - } -return userfaultfd_zeropage_test () ||userfaultfd_sig_test () ||userfaultfd_events_test () ||userfaultfd_minor_test (); }
tools/testing/selftests/mm/uffd-unit-tests.c +145 -0
152 lines not on this page · raw message
Signature · 1 line
2.39.1
Re: [PATCH 22/29] selftests/mm: Move uffd pagemap test to unit test
Quoted 8 lines · Peter Xu
Move it over and make it split into two tests, one for pagemap and one for the new
WP_UNPOPULATED(to be a separate one).The thp pagemap test wasn't really working (with
MADV_HUGEPAGE). Let's just drop it (since it never really worked anyway..) and leave that for later.Signed-off-by Peter Xu <[email protected]>
Quoted 349 lines
--- tools/testing/selftests/mm/uffd-stress.c | 166 ------------------- tools/testing/selftests/mm/uffd-unit-tests.c | 145 ++++++++++++++++ 2 files changed, 145 insertions(+), 166 deletions(-)tools/testing/selftests/mm/uffd-stress.c +0
-166@@ -671,157 +671,6 @@ static int userfaultfd_minor_test(void)return stats.missing_faults !=0 || stats.minor_faults != nr_pages; } -static int pagemap_open (void ) -{ -int fd =open ("/proc/self/pagemap" ,O_RDONLY ); - -if (fd <0 ) -err ("open pagemap" ); - -return fd; -} - -/* This macro let __LINE__ works in err() */ -#define pagemap_check_wp (value, wp) do { \ - if (!!(value & PM_UFFD_WP) != wp) \ - err("pagemap uffd-wp bit error: 0x%"PRIx64, value); \ - } while (0) - -static int pagemap_test_fork (bool present) -{ -pid_t child =fork (); -uint64_t value; -int fd, result; - -if (!child) { -/* Open the pagemap fd of the child itself */ - fd =pagemap_open (); - value =pagemap_get_entry (fd, area_dst); -/* -* After fork() uffd-wp bit should be gone as long as we're -* without UFFD_FEATURE_EVENT_FORK -*/ -pagemap_check_wp (value, false); -/* Succeed */ -exit (0 ); - } -waitpid (child, &result,0 ); -return result; -} - -static void userfaultfd_wp_unpopulated_test (int pagemap_fd) -{ -uint64_t value; - -/* Test applying pte marker to anon unpopulated */ -wp_range (uffd, (uint64_t )area_dst, page_size, true); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, true); - -/* Test unprotect on anon pte marker */ -wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); - -/* Test zap on anon marker */ -wp_range (uffd, (uint64_t )area_dst, page_size, true); -if (madvise (area_dst, page_size,MADV_DONTNEED )) -err ("madvise(MADV_DONTNEED) failed" ); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); - -/* Test fault in after marker removed */ - *area_dst =1 ; - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); -/* Drop it to make pte none again */ -if (madvise (area_dst, page_size,MADV_DONTNEED )) -err ("madvise(MADV_DONTNEED) failed" ); - -/* Test read-zero-page upon pte marker */ -wp_range (uffd, (uint64_t )area_dst, page_size, true); - *(volatile char *)area_dst; -/* Drop it to make pte none again */ -if (madvise (area_dst, page_size,MADV_DONTNEED )) -err ("madvise(MADV_DONTNEED) failed" ); -} - -static void userfaultfd_pagemap_test (unsigned int test_pgsize) -{ -int pagemap_fd; -uint64_t value; - -/* Pagemap tests uffd-wp only */ -if (!test_uffdio_wp) -return ; - -/* Not enough memory to test this page size */ -if (test_pgsize > nr_pages * page_size) -return ; - -printf ("testing uffd-wp with pagemap (pgsize=%u): " , test_pgsize); -/* Flush so it doesn't flush twice in parent/child later */ -fflush (stdout); - -uffd_test_ctx_init (UFFD_FEATURE_WP_UNPOPULATED ); - -if (test_pgsize > page_size) { -/* This is a thp test */ -if (madvise (area_dst, nr_pages * page_size,MADV_HUGEPAGE )) -err ("madvise(MADV_HUGEPAGE) failed" ); - }else if (test_pgsize == page_size) { -/* This is normal page test; force no thp */ -if (madvise (area_dst, nr_pages * page_size,MADV_NOHUGEPAGE )) -err ("madvise(MADV_NOHUGEPAGE) failed" ); - } - -if (uffd_register (uffd, area_dst, nr_pages * page_size, - false, true, false)) -err ("register failed" ); - - pagemap_fd =pagemap_open (); - -/* Smoke test WP_UNPOPULATED first when it's still empty */ -if (test_pgsize == page_size) -userfaultfd_wp_unpopulated_test (pagemap_fd); - -/* Touch the page */ - *area_dst =1 ; -wp_range (uffd, (uint64_t )area_dst, test_pgsize, true); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, true); -/* Make sure uffd-wp bit dropped when fork */ -if (pagemap_test_fork (true)) -err ("Detected stall uffd-wp bit in child" ); - -/* Exclusive required or PAGEOUT won't work */ -if (!(value &PM_MMAP_EXCLUSIVE )) -err ("multiple mapping detected: 0x%" PRIx64, value); - -if (madvise (area_dst, test_pgsize,MADV_PAGEOUT )) -err ("madvise(MADV_PAGEOUT) failed" ); - -/* Uffd-wp should persist even swapped out */ - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, true); -/* Make sure uffd-wp bit dropped when fork */ -if (pagemap_test_fork (false)) -err ("Detected stall uffd-wp bit in child" ); - -/* Unprotect; this tests swap pte modifications */ -wp_range (uffd, (uint64_t )area_dst, page_size, false); - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); - -/* Fault in the page from disk */ - *area_dst =2 ; - value =pagemap_get_entry (pagemap_fd, area_dst); -pagemap_check_wp (value, false); - -close (pagemap_fd); -printf ("done\n" ); -} -static int userfaultfd_stress (void ) {void *area;@@ -933,21 +782,6 @@ static int userfaultfd_stress(void)uffd_stats_report (uffd_stats, nr_cpus); } -if (test_type ==TEST_ANON ) { -/* -* shmem/hugetlb won't be able to run since they have different -* behavior on fork() (file-backed memory normally drops ptes -* directly when fork), meanwhile the pagemap test will verify -* pgtable entry of fork()ed child. -*/ -userfaultfd_pagemap_test (page_size); -/* -* Hard-code for x86_64 for now for 2M THP, as x86_64 is -* currently the only one that supports uffd-wp -*/ -userfaultfd_pagemap_test (page_size *512 ); - } -return userfaultfd_zeropage_test () ||userfaultfd_sig_test () ||userfaultfd_events_test () ||userfaultfd_minor_test (); }tools/testing/selftests/mm/uffd-unit-tests.c +145
-0@@ -200,7 +200,152 @@ static bool uffd_feature_supported(uffd_test_case_t *test) test->uffd_feature_required; } +static int pagemap_open (void ) +{ +int fd =open ("/proc/self/pagemap" ,O_RDONLY ); + +if (fd <0 ) +err ("open pagemap" ); + +return fd; +} + +/* This macro let __LINE__ works in err() */ +#define pagemap_check_wp (value, wp) do { \ + if (!!(value & PM_UFFD_WP) != wp) \ + err("pagemap uffd-wp bit error: 0x%"PRIx64, value); \ + } while (0) + +static int pagemap_test_fork (bool present) +{ +pid_t child =fork (); +uint64_t value; +int fd, result; + +if (!child) { +/* Open the pagemap fd of the child itself */ + fd =pagemap_open (); + value =pagemap_get_entry (fd, area_dst); +/* +* After fork() uffd-wp bit should be gone as long as we're +* without UFFD_FEATURE_EVENT_FORK +*/ +pagemap_check_wp (value, false); +/* Succeed */ +exit (0 ); + } +waitpid (child, &result,0 ); +return result; +} + +static void uffd_wp_unpopulated_test (void ) +{ +uint64_t value; +int pagemap_fd; + +if (uffd_register (uffd, area_dst, nr_pages * page_size, + false, true, false)) +err ("register failed" ); + + pagemap_fd =pagemap_open (); + +/* Test applying pte marker to anon unpopulated */ +wp_range (uffd, (uint64_t )area_dst, page_size, true); + value =pagemap_get_entry (pagemap_fd, area_dst); +pagemap_check_wp (value, true); + +/* Test unprotect on anon pte marker */ +wp_range (uffd, (uint64_t )area_dst, page_size, false); + value =pagemap_get_entry (pagemap_fd, area_dst); +pagemap_check_wp (value, false); + +/* Test zap on anon marker */ +wp_range (uffd, (uint64_t )area_dst, page_size, true); +if (madvise (area_dst, page_size,MADV_DONTNEED )) +err ("madvise(MADV_DONTNEED) failed" ); + value =pagemap_get_entry (pagemap_fd, area_dst); +pagemap_check_wp (value, false); + +/* Test fault in after marker removed */ + *area_dst =1 ; + value =pagemap_get_entry (pagemap_fd, area_dst); +pagemap_check_wp (value, false); +/* Drop it to make pte none again */ +if (madvise (area_dst, page_size,MADV_DONTNEED )) +err ("madvise(MADV_DONTNEED) failed" ); + +/* Test read-zero-page upon pte marker */ +wp_range (uffd, (uint64_t )area_dst, page_size, true); + *(volatile char *)area_dst; +/* Drop it to make pte none again */ +if (madvise (area_dst, page_size,MADV_DONTNEED )) +err ("madvise(MADV_DONTNEED) failed" ); + +uffd_test_pass (); +} + +static void uffd_pagemap_test (void ) +{ +int pagemap_fd; +uint64_t value; + +if (uffd_register (uffd, area_dst, nr_pages * page_size, + false, true, false)) +err ("register failed" ); + + pagemap_fd =pagemap_open (); + +/* Touch the page */ + *area_dst =1 ; +wp_range (uffd, (uint64_t )area_dst, page_size, true); + value =pagemap_get_entry (pagemap_fd, area_dst); +pagemap_check_wp (value, true); +/* Make sure uffd-wp bit dropped when fork */ +if (pagemap_test_fork (true)) +err ("Detected stall uffd-wp bit in child" ); + +/* Exclusive required or PAGEOUT won't work */ +if (!(value &PM_MMAP_EXCLUSIVE )) +err ("multiple mapping detected: 0x%" PRIx64, value); + +if (madvise (area_dst, page_size,MADV_PAGEOUT )) +err ("madvise(MADV_PAGEOUT) failed" ); + +/* Uffd-wp should persist even swapped out */ + value =pagemap_get_entry (pagemap_fd, area_dst); +pagemap_check_wp (value, true); +/* Make sure uffd-wp bit dropped when fork */ +if (pagemap_test_fork (false)) +err ("Detected stall uffd-wp bit in child" ); + +/* Unprotect; this tests swap pte modifications */ +wp_range (uffd, (uint64_t )area_dst, page_size, false); + value =pagemap_get_entry (pagemap_fd, area_dst); +pagemap_check_wp (value, false); + +/* Fault in the page from disk */ + *area_dst =2 ; + value =pagemap_get_entry (pagemap_fd, area_dst); +pagemap_check_wp (value, false); + +close (pagemap_fd); +uffd_test_pass (); +} +uffd_test_case_t uffd_tests[] = { + { + .name ="pagemap" , + .uffd_fn = uffd_pagemap_test, + .mem_targets =MEM_ANON , + .uffd_feature_required =UFFD_FEATURE_PAGEFAULT_FLAG_WP , + }, + { + .name ="wp-unpopulated" , + .uffd_fn = uffd_wp_unpopulated_test, + .mem_targets =MEM_ANON , + .uffd_feature_required = +UFFD_FEATURE_PAGEFAULT_FLAG_WP |UFFD_FEATURE_WP_UNPOPULATED , + }, };int main (int argc,char *argv[])Signature · 1 line
2.39.1
Signature · 2 lines
Sincerely yours, Mike.
Peter Xu
reviewerPATCH 23/29 selftests/mm: Move uffd minor test to unit test
In reply to Peter XuThis moves the minor test to the new unit test.
Rewrite the content check with char* opeartions to avoid fiddling with my_bcmp().
Drop global vars test_uffdio_minor and test_collapse, just assume test them always in common code for now.
OTOH make this single test into five tests:
- minor test on [shmem, hugetlb] with wp=false
- minor test on [shmem, hugetlb] with wp=true
- minor test + collapse on shmem only
One thing to mention that we used to test COLLAPSE+WP but that doesn't sound right at all. It's possible it's silently broken but unnoticed because COLLAPSE is not part of the default test suite.
Make the MADV_COLLAPSE test fail-able (by skip it when failing), because it's not guaranteed to success anyway.
Drop a bunch of useless code after the move, because the unit test always use aligned num of pages and has nothing to do with n_cpus.
--- tools/testing/selftests/mm/uffd-common.c | 25 ++-- tools/testing/selftests/mm/uffd-common.h | 4 +- tools/testing/selftests/mm/uffd-stress.c | 131 +------------------ tools/testing/selftests/mm/uffd-unit-tests.c | 120 +++++++++++++++++ 4 files changed, 135 insertions(+), 145 deletions(-)
tools/testing/selftests/mm/uffd-common.c +12 -13
@@ -13,8 +13,8 @@ volatile bool test_uffdio_copy_eexist = true;unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;int uffd =-1 , uffd_flags, finished, *pipefd, test_type; -bool map_shared, test_collapse, test_dev_userfaultfd; -bool test_uffdio_wp = true, test_uffdio_minor = false; +bool map_shared, test_dev_userfaultfd; +bool test_uffdio_wp = true;unsigned long long *count_verify;uffd_test_ops_t *uffd_test_ops;
@@ -126,28 +126,27 @@ static int shmem_allocate_area(void **alloc_area, bool is_src)char *p =NULL , *p_alias =NULL ;int mem_fd =uffd_mem_fd_create (bytes *2 , false); -if (test_collapse) { - p =BASE_PMD_ADDR ; -if (!is_src) -/* src map + alias + interleaved hpages */ - p +=2 * (bytes + hpage_size); - p_alias = p; - p_alias += bytes; - p_alias += hpage_size;/* Prevent src/dst VMA merge */ - } +/* TODO: clean this up. Use a static addr is ugly */ + p =BASE_PMD_ADDR ; +if (!is_src) +/* src map + alias + interleaved hpages */ + p +=2 * (bytes + hpage_size); + p_alias = p; + p_alias += bytes; + p_alias += hpage_size;/* Prevent src/dst VMA merge */ *alloc_area =mmap (p, bytes,PROT_READ |PROT_WRITE ,MAP_SHARED , mem_fd, offset);if (*alloc_area ==MAP_FAILED )err ("mmap of memfd failed" ); -if (test_collapse && *alloc_area != p) +if (*alloc_area != p)err ("mmap of memfd failed at %p" , p); area_alias =mmap (p_alias, bytes,PROT_READ |PROT_WRITE ,MAP_SHARED , mem_fd, offset);if (area_alias ==MAP_FAILED )err ("mmap of memfd alias failed" ); -if (test_collapse && area_alias != p_alias) +if (area_alias != p_alias)err ("mmap of anonymous memory failed at %p" , p_alias);if (is_src)
tools/testing/selftests/mm/uffd-common.h +2 -2
@@ -90,8 +90,8 @@ typedef struct uffd_test_ops uffd_test_ops_t;extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;extern char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;extern int uffd, uffd_flags, finished, *pipefd, test_type; -extern bool map_shared, test_collapse, test_dev_userfaultfd; -extern bool test_uffdio_wp, test_uffdio_minor; +extern bool map_shared, test_dev_userfaultfd; +extern bool test_uffdio_wp;extern unsigned long long *count_verify;extern volatile bool test_uffdio_copy_eexist;
tools/testing/selftests/mm/uffd-stress.c +1 -130
197 lines not on this page · raw message
tools/testing/selftests/mm/uffd-unit-tests.c +120 -0
132 lines not on this page · raw message
Signature · 1 line
2.39.1
Peter Xu
reviewerPATCH 24/29 selftests/mm: Move uffd sig/events tests into uffd unit tests
In reply to Peter XuMove the two tests into the unit test, and convert it into 20 standalone tests:
- events test on all 5 mem types, with wp on/off
- signal test on all 5 mem types, with wp on/off
Testing sigbus on anon... done
Testing sigbus on shmem... done
Testing sigbus on shmem-private... done
Testing sigbus on hugetlb... done
Testing sigbus on hugetlb-private... done
Testing sigbus-wp on anon... done
Testing sigbus-wp on shmem... done
Testing sigbus-wp on shmem-private... done
Testing sigbus-wp on hugetlb... done
Testing sigbus-wp on hugetlb-private... done
Testing events on anon... done
Testing events on shmem... done
Testing events on shmem-private... done
Testing events on hugetlb... done
Testing events on hugetlb-private... done
Testing events-wp on anon... done
Testing events-wp on shmem... done
Testing events-wp on shmem-private... done
Testing events-wp on hugetlb... done
Testing events-wp on hugetlb-private... done
It'll also remove a lot of global references along the way, e.g. test_uffdio_wp will be replaced with the wp value passed over.
--- tools/testing/selftests/mm/uffd-stress.c | 227 +--------------- tools/testing/selftests/mm/uffd-unit-tests.c | 263 +++++++++++++++++++ 2 files changed, 264 insertions(+), 226 deletions(-)
tools/testing/selftests/mm/uffd-stress.c +1 -226
@@ -274,133 +274,6 @@ static int stress(struct uffd_stats *uffd_stats)return 0 ; } -sigjmp_buf jbuf, *sigbuf; - -static void sighndl (int sig,siginfo_t *siginfo,void *ptr) -{ -if (sig ==SIGBUS ) { -if (sigbuf) -siglongjmp (*sigbuf,1 ); -abort (); - } -} - -/* -* For non-cooperative userfaultfd test we fork() a process that will -* generate pagefaults, will mremap the area monitored by the -* userfaultfd and at last this process will release the monitored -* area. -* For the anonymous and shared memory the area is divided into two -* parts, the first part is accessed before mremap, and the second -* part is accessed after mremap. Since hugetlbfs does not support -* mremap, the entire monitored area is accessed in a single pass for -* HUGETLB_TEST. -* The release of the pages currently generates event for shmem and -* anonymous memory (UFFD_EVENT_REMOVE), hence it is not checked -* for hugetlb. -* For signal test(UFFD_FEATURE_SIGBUS), signal_test = 1, we register -* monitored area, generate pagefaults and test that signal is delivered. -* Use UFFDIO_COPY to allocate missing page and retry. For signal_test = 2 -* test robustness use case - we release monitored area, fork a process -* that will generate pagefaults and verify signal is generated. -* This also tests UFFD_FEATURE_EVENT_FORK event along with the signal -* feature. Using monitor thread, verify no userfault events are generated. -*/ -static int faulting_process (int signal_test,bool wp) -{ -unsigned long nr; -unsigned long long count; -unsigned long split_nr_pages; -unsigned long lastnr; -struct sigaction act; -volatile unsigned long signalled =0 ; - - split_nr_pages = (nr_pages +1 ) /2 ; - -if (signal_test) { - sigbuf = &jbuf; -memset (&act,0 ,sizeof (act)); - act.sa_sigaction = sighndl; - act.sa_flags =SA_SIGINFO ; -if (sigaction (SIGBUS , &act,0 )) -err ("sigaction" ); - lastnr = (unsigned long )-1 ; - } - -for (nr =0 ; nr < split_nr_pages; nr++) { -volatile int steps =1 ; -unsigned long offset = nr * page_size; - -if (signal_test) { -if (sigsetjmp (*sigbuf,1 ) !=0 ) { -if (steps ==1 && nr == lastnr) -err ("Signal repeated" ); - - lastnr = nr; -if (signal_test ==1 ) { -if (steps ==1 ) { -/* This is a MISSING request */ - steps++; -if (copy_page (uffd, offset, wp)) - signalled++; - }else { -/* This is a WP request */ -assert (steps ==2 ); -wp_range (uffd, - (__u64 )area_dst + - offset, - page_size, false); - } - }else { - signalled++; -continue ; - } - } - } - - count = *area_count (area_dst, nr); -if (count != count_verify[nr]) -err ("nr %lu memory corruption %llu %llu\n" , - nr, count, count_verify[nr]); -/* -* Trigger write protection if there is by writing -* the same value back. -*/ - *area_count (area_dst, nr) = count; - } - -if (signal_test) -return signalled != split_nr_pages; - - area_dst =mremap (area_dst, nr_pages * page_size, nr_pages * page_size, -MREMAP_MAYMOVE |MREMAP_FIXED , area_src); -if (area_dst ==MAP_FAILED ) -err ("mremap" ); -/* Reset area_src since we just clobbered it */ - area_src =NULL ; - -for (; nr < nr_pages; nr++) { - count = *area_count (area_dst, nr); -if (count != count_verify[nr]) { -err ("nr %lu memory corruption %llu %llu\n" , - nr, count, count_verify[nr]); - } -/* -* Trigger write protection if there is by writing -* the same value back. -*/ - *area_count (area_dst, nr) = count; - } - - uffd_test_ops->release_pages (area_dst); - -for (nr =0 ; nr < nr_pages; nr++) -if (my_bcmp (area_dst + nr * page_size, zeropage, page_size)) -err ("nr %lu is not zero" , nr); - -return 0 ; -} -static void retry_uffdio_zeropage (int ufd,struct uffdio_zeropage *uffdio_zeropage,unsigned long offset)
@@ -484,103 +357,6 @@ static int userfaultfd_zeropage_test(void)return 0 ; } -static int userfaultfd_events_test (void ) -{ -pthread_t uffd_mon; -int err, features; -pid_t pid; -char c; -struct uffd_stats stats = {0 }; - -printf ("testing events (fork, remap, remove): " ); -fflush (stdout); - - features =UFFD_FEATURE_EVENT_FORK |UFFD_FEATURE_EVENT_REMAP | -UFFD_FEATURE_EVENT_REMOVE ; -uffd_test_ctx_init (features); - -fcntl (uffd,F_SETFL , uffd_flags |O_NONBLOCK ); - -if (uffd_register (uffd, area_dst, nr_pages * page_size, - true, test_uffdio_wp, false)) -err ("register failure" ); - - stats.apply_wp = test_uffdio_wp; -if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats)) -err ("uffd_poll_thread create" ); - - pid =fork (); -if (pid <0 ) -err ("fork" ); - -if (!pid) -exit (faulting_process (0 , test_uffdio_wp)); - -waitpid (pid, &err,0 ); -if (err) -err ("faulting process failed" ); -if (write (pipefd[1 ], &c,sizeof (c)) !=sizeof (c)) -err ("pipe write" ); -if (pthread_join (uffd_mon,NULL )) -return 1 ; - -uffd_stats_report (&stats,1 ); - -return stats.missing_faults != nr_pages; -} - -static int userfaultfd_sig_test (void ) -{ -unsigned long userfaults; -pthread_t uffd_mon; -int err, features; -pid_t pid; -char c; -struct uffd_stats stats = {0 }; - -printf ("testing signal delivery: " ); -fflush (stdout); - - features =UFFD_FEATURE_EVENT_FORK |UFFD_FEATURE_SIGBUS ; -uffd_test_ctx_init (features); - -fcntl (uffd,F_SETFL , uffd_flags |O_NONBLOCK ); - -if (uffd_register (uffd, area_dst, nr_pages * page_size, - true, test_uffdio_wp, false)) -err ("register failure" ); - -if (faulting_process (1 , test_uffdio_wp)) -err ("faulting process failed" ); - - uffd_test_ops->release_pages (area_dst); - - stats.apply_wp = test_uffdio_wp; -if (pthread_create (&uffd_mon, &attr, uffd_poll_thread, &stats)) -err ("uffd_poll_thread create" ); - - pid =fork (); -if (pid <0 ) -err ("fork" ); - -if (!pid) -exit (faulting_process (2 , test_uffdio_wp)); - -waitpid (pid, &err,0 ); -if (err) -err ("faulting process failed" ); -if (write (pipefd[1 ], &c,sizeof (c)) !=sizeof (c)) -err ("pipe write" ); -if (pthread_join (uffd_mon, (void **)&userfaults)) -return 1 ; - -printf ("done.\n" ); -if (userfaults) -err ("Signal test failed, userfaults: %ld" , userfaults); - -return userfaults !=0 ; -} -static int userfaultfd_stress (void ) {void *area;
@@ -692,8 +468,7 @@ static int userfaultfd_stress(void)uffd_stats_report (uffd_stats, nr_cpus); } -return userfaultfd_zeropage_test () ||userfaultfd_sig_test () - ||userfaultfd_events_test (); +return userfaultfd_zeropage_test (); }static void set_test_type (const char *type)
tools/testing/selftests/mm/uffd-unit-tests.c +263 -0
281 lines not on this page · raw message
Signature · 1 line
2.39.1
Peter Xu
reviewerPATCH 25/29 selftests/mm: Move zeropage test into uffd unit tests
In reply to Peter XuSimplifies it a bit along the way, e.g., drop the never used offset field (which was always the 1st page so offset=0).
--- tools/testing/selftests/mm/uffd-stress.c | 94 +------------------- tools/testing/selftests/mm/uffd-unit-tests.c | 94 ++++++++++++++++++++ 2 files changed, 95 insertions(+), 93 deletions(-)
tools/testing/selftests/mm/uffd-stress.c +1 -93
@@ -110,15 +110,6 @@ static inline uint64_t uffd_minor_feature(void)return 0 ; } -static int my_bcmp (char *str1,char *str2,size_t n) -{ -unsigned long i; -for (i =0 ; i < n; i++) -if (str1[i] != str2[i]) -return 1 ; -return 0 ; -} -static void *locking_thread (void *arg) {unsigned long cpu = (unsigned long ) arg;
@@ -274,89 +265,6 @@ static int stress(struct uffd_stats *uffd_stats)return 0 ; } -static void retry_uffdio_zeropage (int ufd, -struct uffdio_zeropage *uffdio_zeropage, -unsigned long offset) -{ - uffd_test_ops->alias_mapping (&uffdio_zeropage->range.start, - uffdio_zeropage->range.len, - offset); -if (ioctl (ufd,UFFDIO_ZEROPAGE , uffdio_zeropage)) { -if (uffdio_zeropage->zeropage != -EEXIST ) -err ("UFFDIO_ZEROPAGE error: %" PRId64, - (int64_t )uffdio_zeropage->zeropage); - }else { -err ("UFFDIO_ZEROPAGE error: %" PRId64, - (int64_t )uffdio_zeropage->zeropage); - } -} - -static int __uffdio_zeropage (int ufd,unsigned long offset) -{ -struct uffdio_zeropage uffdio_zeropage; -int ret; -bool has_zeropage = !(test_type ==TEST_HUGETLB ); -__s64 res; - -if (offset >= nr_pages * page_size) -err ("unexpected offset %lu" , offset); - uffdio_zeropage.range.start = (unsigned long ) area_dst + offset; - uffdio_zeropage.range.len = page_size; - uffdio_zeropage.mode =0 ; - ret =ioctl (ufd,UFFDIO_ZEROPAGE , &uffdio_zeropage); - res = uffdio_zeropage.zeropage; -if (ret) { -/* real retval in ufdio_zeropage.zeropage */ -if (has_zeropage) -err ("UFFDIO_ZEROPAGE error: %" PRId64, (int64_t )res); -else if (res != -EINVAL ) -err ("UFFDIO_ZEROPAGE not -EINVAL" ); - }else if (has_zeropage) { -if (res != page_size) { -err ("UFFDIO_ZEROPAGE unexpected size" ); - }else { -retry_uffdio_zeropage (ufd, &uffdio_zeropage, - offset); -return 1 ; - } - }else -err ("UFFDIO_ZEROPAGE succeeded" ); - -return 0 ; -} - -static int uffdio_zeropage (int ufd,unsigned long offset) -{ -return __uffdio_zeropage (ufd, offset); -} - -/* exercise UFFDIO_ZEROPAGE */ -static int userfaultfd_zeropage_test (void ) -{ -printf ("testing UFFDIO_ZEROPAGE: " ); -fflush (stdout); - -uffd_test_ctx_init (0 ); - -if (uffd_register (uffd, area_dst, nr_pages * page_size, - true, test_uffdio_wp, false)) -err ("register failure" ); - -if (area_dst_alias) { -/* Needed this to test zeropage-retry on shared memory */ -if (uffd_register (uffd, area_dst_alias, nr_pages * page_size, - true, test_uffdio_wp, false)) -err ("register failure" ); - } - -if (uffdio_zeropage (uffd,0 )) -if (my_bcmp (area_dst, zeropage, page_size)) -err ("zeropage is not zero" ); - -printf ("done.\n" ); -return 0 ; -} -static int userfaultfd_stress (void ) {void *area;
@@ -468,7 +376,7 @@ static int userfaultfd_stress(void)uffd_stats_report (uffd_stats, nr_cpus); } -return userfaultfd_zeropage_test (); +return 0 ; }static void set_test_type (const char *type)
tools/testing/selftests/mm/uffd-unit-tests.c +94 -0
101 lines not on this page · raw message
Signature · 1 line
2.39.1
Peter Xu
reviewerPATCH 26/29 selftests/mm: Workaround no way to detect uffd-minor + wp
In reply to Peter XuUserfaultfd minor+wp mode was very recently added. The test will fail on the old kernels at ioctl(UFFDIO_CONTINUE) which is misterious. Unfortunately there's no feature bit to detect for this support.
Add a hack to leverage WP_UNPOPULATED to detect whether that feature existed, since WP_UNPOPULATED was merged right after minor+wp.
--- tools/testing/selftests/mm/uffd-unit-tests.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/uffd-unit-tests.c +7 -1
@@ -784,7 +784,13 @@ uffd_test_case_t uffd_tests[] = { .mem_targets = MEM_SHMEM | MEM_HUGETLB, .uffd_feature_required =UFFD_FEATURE_MINOR_HUGETLBFS |UFFD_FEATURE_MINOR_SHMEM | - UFFD_FEATURE_PAGEFAULT_FLAG_WP, +UFFD_FEATURE_PAGEFAULT_FLAG_WP | +/* +* HACK: here we leveraged WP_UNPOPULATED to detect whether +* minor mode supports wr-protect. There's no feature flag +* for it so this is the best we can test against. +*/ + UFFD_FEATURE_WP_UNPOPULATED, }, { .name = "minor-collapse",
Signature · 1 line
2.39.1
Peter Xu
reviewerPATCH 27/29 selftests/mm: Allow uffd test to skip properly with no privilege
In reply to Peter XuAllow skip a unit test properly due to no privilege (e.g. sigbus and events tests).
--- tools/testing/selftests/mm/uffd-common.c | 27 ++++++++++++-------- tools/testing/selftests/mm/uffd-common.h | 4 +-- tools/testing/selftests/mm/uffd-stress.c | 6 +++-- tools/testing/selftests/mm/uffd-unit-tests.c | 10 +++++--- 4 files changed, 29 insertions(+), 18 deletions(-)
tools/testing/selftests/mm/uffd-common.c +17 -10
@@ -225,7 +225,7 @@ void uffd_stats_report(struct uffd_stats *stats, int n_cpus)printf ("\n" ); } -void userfaultfd_open (uint64_t *features) +int userfaultfd_open (uint64_t *features) {struct uffdio_api uffdio_api;
@@ -234,18 +234,19 @@ void userfaultfd_open(uint64_t *features)else uffd =uffd_open_sys (UFFD_FLAGS );if (uffd <0 ) -err ("uffd open failed (dev=%d)" , test_dev_userfaultfd); +return -1 ; uffd_flags =fcntl (uffd,F_GETFD ,NULL ); uffdio_api.api =UFFD_API ; uffdio_api.features = *features;if (ioctl (uffd,UFFDIO_API , &uffdio_api)) -err ("UFFDIO_API failed.\nPlease make sure to " -"run with either root or ptrace capability." ); +/* Probably lack of CAP_PTRACE? */ +return -1 ;if (uffdio_api.api !=UFFD_API )err ("UFFDIO_API error: %" PRIu64, (uint64_t )uffdio_api.api); *features = uffdio_api.features; +return 0 ; }static inline void munmap_area (void **area)
@@ -288,7 +289,7 @@ static void uffd_test_ctx_clear(void)munmap_area ((void **)&area_remap); } -int uffd_test_ctx_init (uint64_t features) +int uffd_test_ctx_init (uint64_t features,const char **errmsg) {unsigned long nr, cpu;int ret;
@@ -296,13 +297,19 @@ int uffd_test_ctx_init(uint64_t features)uffd_test_ctx_clear (); ret = uffd_test_ops->allocate_area ((void **)&area_src, true); -if (ret) -return ret; - ret = uffd_test_ops->allocate_area ((void **)&area_dst, false); -if (ret) + ret |= uffd_test_ops->allocate_area ((void **)&area_dst, false); +if (ret) { +if (errmsg) + *errmsg ="memory allocation failed" ;return ret; + } -userfaultfd_open (&features); + ret =userfaultfd_open (&features); +if (ret) { +if (errmsg) + *errmsg ="possible lack of priviledge" ; +return ret; + } count_verify =malloc (nr_pages *sizeof (unsigned long long ));if (!count_verify)
tools/testing/selftests/mm/uffd-common.h +2 -2
@@ -101,8 +101,8 @@ extern uffd_test_ops_t hugetlb_uffd_test_ops;extern uffd_test_ops_t *uffd_test_ops;void uffd_stats_report (struct uffd_stats *stats,int n_cpus); -int uffd_test_ctx_init (uint64_t features); -void userfaultfd_open (uint64_t *features); +int uffd_test_ctx_init (uint64_t features,const char **errmsg); +int userfaultfd_open (uint64_t *features);int uffd_read_msg (int ufd,struct uffd_msg *msg);void wp_range (int ufd,__u64 start,__u64 len,bool wp);void uffd_handle_page_fault (struct uffd_msg *msg,struct uffd_stats *stats);
tools/testing/selftests/mm/uffd-stress.c +4 -2
@@ -272,7 +272,8 @@ static int userfaultfd_stress(void)struct uffd_stats uffd_stats[nr_cpus];uint64_t mem_size = nr_pages * page_size; -uffd_test_ctx_init (UFFD_FEATURE_WP_UNPOPULATED ); +if (uffd_test_ctx_init (UFFD_FEATURE_WP_UNPOPULATED ,NULL )) +err ("context init failed" );if (posix_memalign (&area, page_size, page_size))err ("out of memory" );
@@ -436,7 +437,8 @@ static void parse_test_type_arg(const char *raw_type)* feature. */ -userfaultfd_open (&features); +if (userfaultfd_open (&features)) +err ("Userfaultfd open failed" ); test_uffdio_wp = test_uffdio_wp && (features &UFFD_FEATURE_PAGEFAULT_FLAG_WP );
tools/testing/selftests/mm/uffd-unit-tests.c +6 -4
@@ -175,7 +175,8 @@ static int test_uffd_api(bool use_dev)* This function initializes the global variables. TODO: remove global * vars and then remove this. */ -static int uffd_setup_environment (uffd_test_case_t *test,mem_type_t *mem_type) +static int uffd_setup_environment (uffd_test_case_t *test,mem_type_t *mem_type, +const char **errmsg) { map_shared = mem_type->shared; uffd_test_ops = mem_type->mem_ops;
@@ -189,7 +190,7 @@ static int uffd_setup_environment(uffd_test_case_t *test, mem_type_t *mem_type)/* TODO: remove this global var.. it's so ugly */ nr_cpus =1 ; -return uffd_test_ctx_init (test->uffd_feature_required); +return uffd_test_ctx_init (test->uffd_feature_required, errmsg); }static bool uffd_feature_supported (uffd_test_case_t *test)
@@ -838,6 +839,7 @@ int main(int argc, char *argv[])uffd_test_case_t *test;mem_type_t *mem_type;char test_name[128 ]; +const char *errmsg;int has_uffd;int i, j;
@@ -863,8 +865,8 @@ int main(int argc, char *argv[])uffd_test_skip ("feature missing" );continue ; } -if (uffd_setup_environment (test, mem_type)) { -uffd_test_skip ("memory allocation failed" ); +if (uffd_setup_environment (test, mem_type, &errmsg)) { +uffd_test_skip (errmsg);continue ; } test->uffd_fn ();
Signature · 1 line
2.39.1
Peter Xu
reviewerPATCH 28/29 selftests/mm: Drop sys/dev test in uffd-stress test
In reply to Peter XuWith the new uffd unit test covering the /dev/userfaultfd path and syscall path of uffd initializations, we can safely drop the devnode test in the old stress test.
One thing is to avoid duplication of running the stress test twice which is an overkill to only test the /dev/ interface in run_vmtests.sh.
The other benefit is now all uffd tests (that uses userfaultfd_open) can run automatically as long as any type of interface is enabled (either syscall or dev), so it's more likely to succeed rather than fail due to unprivilege.
With this patch lands, we can drop all the "mem_type:XXX" handlings too.
--- tools/testing/selftests/mm/run_vmtests.sh | 15 +++++-------- tools/testing/selftests/mm/uffd-common.c | 7 ++---- tools/testing/selftests/mm/uffd-common.h | 2 +- tools/testing/selftests/mm/uffd-stress.c | 27 ++--------------------- 4 files changed, 11 insertions(+), 40 deletions(-)
tools/testing/selftests/mm/run_vmtests.sh +6 -9
@@ -197,16 +197,13 @@ CATEGORY="gup_test" run_test ./gup_test -a CATEGORY="gup_test" run_test ./gup_test -ct -F 0x1 0 19 0x1000 CATEGORY="userfaultfd" run_test ./uffd-unit-tests -uffd_mods=("" ":dev") uffd_stress_bin=./uffd-stress -for mod in "${uffd_mods[@]}"; do - CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon${mod} 20 16 - # Hugetlb tests require source and destination huge pages. Pass in half - # the size ($half_ufd_size_MB), which is used for *each*. - CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb${mod} "$half_ufd_size_MB" 32 - CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb_shared${mod} "$half_ufd_size_MB" 32 - CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem${mod} 20 16 -done +CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16 +# Hugetlb tests require source and destination huge pages. Pass in half +# the size ($half_ufd_size_MB), which is used for *each*. +CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb "$half_ufd_size_MB" 32 +CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb_shared "$half_ufd_size_MB" 32 +CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem 20 16 #cleanup echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages
tools/testing/selftests/mm/uffd-common.c +2 -5
@@ -13,7 +13,7 @@ volatile bool test_uffdio_copy_eexist = true;unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;int uffd =-1 , uffd_flags, finished, *pipefd, test_type; -bool map_shared, test_dev_userfaultfd; +bool map_shared;bool test_uffdio_wp = true;unsigned long long *count_verify;uffd_test_ops_t *uffd_test_ops;
@@ -229,10 +229,7 @@ int userfaultfd_open(uint64_t *features) {struct uffdio_api uffdio_api; -if (test_dev_userfaultfd) - uffd =uffd_open_dev (UFFD_FLAGS ); -else - uffd =uffd_open_sys (UFFD_FLAGS ); + uffd =uffd_open (UFFD_FLAGS );if (uffd <0 )return -1 ; uffd_flags =fcntl (uffd,F_GETFD ,NULL );
tools/testing/selftests/mm/uffd-common.h +1 -1
@@ -90,7 +90,7 @@ typedef struct uffd_test_ops uffd_test_ops_t;extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;extern char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap;extern int uffd, uffd_flags, finished, *pipefd, test_type; -extern bool map_shared, test_dev_userfaultfd; +extern bool map_shared;extern bool test_uffdio_wp;extern unsigned long long *count_verify;extern volatile bool test_uffdio_copy_eexist;
tools/testing/selftests/mm/uffd-stress.c +2 -25
@@ -55,8 +55,6 @@ pthread_attr_t attr;const char *examples ="# Run anonymous memory test on 100MiB region with 99999 bounces:\n" "./userfaultfd anon 100 99999\n\n" -"# Run the same anonymous memory test, but using /dev/userfaultfd:\n" -"./userfaultfd anon:dev 100 99999\n\n" "# Run share memory test on 1GiB region with 99 bounces:\n" "./userfaultfd shmem 1000 99\n\n" "# Run hugetlb memory test on 256MiB region with 50 bounces:\n"
@@ -69,18 +67,9 @@ const char *examples =static void usage (void ) { -fprintf (stderr,"\nUsage: ./userfaultfd <test type> <MiB> <bounces> " -"[hugetlbfs_file]\n\n" ); +fprintf (stderr,"\nUsage: ./userfaultfd <test type> <MiB> <bounces>\n\n" );fprintf (stderr,"Supported <test type>: anon, hugetlb, " "hugetlb_shared, shmem\n\n" ); -fprintf (stderr,"'Test mods' can be joined to the test type string with a ':'. " -"Supported mods:\n" ); -fprintf (stderr,"\tsyscall - Use userfaultfd(2) (default)\n" ); -fprintf (stderr,"\tdev - Use /dev/userfaultfd instead of userfaultfd(2)\n" ); -fprintf (stderr,"\nExample test mod usage:\n" ); -fprintf (stderr,"# Run anonymous memory test with /dev/userfaultfd:\n" ); -fprintf (stderr,"./userfaultfd anon:dev 100 99999\n\n" ); -fprintf (stderr,"Examples:\n\n" );fprintf (stderr,"%s" , examples);exit (1 );
@@ -401,21 +390,9 @@ static void set_test_type(const char *type)static void parse_test_type_arg (const char *raw_type) { -char *buf =strdup (raw_type);uint64_t features =UFFD_API_FEATURES ; -while (buf) { -const char *token =strsep (&buf,":" ); - -if (!test_type) -set_test_type (token); -else if (!strcmp (token,"dev" )) - test_dev_userfaultfd = true; -else if (!strcmp (token,"syscall" )) - test_dev_userfaultfd = false; -else -err ("unrecognized test mod '%s'" , token); - } +set_test_type (raw_type);if (!test_type)err ("failed to parse test type argument: '%s'" , raw_type);
Signature · 1 line
2.39.1
Peter Xu
reviewerPATCH 29/29 selftests/mm: Add shmem-private test to uffd-stress
In reply to Peter XuThe userfaultfd stress test never tested private shmem, which I think was overlooked long due. Add it so it matches with uffd unit test and it'll cover all memory supported with the three memory types.
Meanwhile, rename the memory types a bit. Considering shared mem is the major use case for both shmem / hugetlbfs, changing from:
anon, hugetlb, hugetlb_shared, shmem
To (with shmem-private added):
anon, hugetlb, hugetlb-private, shmem, shmem-private
Add the shmem-private to run_vmtests.sh too.
--- tools/testing/selftests/mm/run_vmtests.sh | 3 ++- tools/testing/selftests/mm/uffd-stress.c | 11 +++++++---- 2 files changed, 9 insertions(+), 5 deletions(-)
tools/testing/selftests/mm/run_vmtests.sh +2 -1
@@ -202,8 +202,9 @@ CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16 # Hugetlb tests require source and destination huge pages. Pass in half # the size ($half_ufd_size_MB), which is used for *each*. CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb "$half_ufd_size_MB" 32 -CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb_shared "$half_ufd_size_MB" 32 +CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb-private "$half_ufd_size_MB" 32 CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem 20 16 +CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem-private 20 16 #cleanup echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages
tools/testing/selftests/mm/uffd-stress.c +7 -4
@@ -59,8 +59,8 @@ const char *examples ="./userfaultfd shmem 1000 99\n\n" "# Run hugetlb memory test on 256MiB region with 50 bounces:\n" "./userfaultfd hugetlb 256 50\n\n" -"# Run the same hugetlb test but using shared file:\n" -"./userfaultfd hugetlb_shared 256 50\n\n" +"# Run the same hugetlb test but using private file:\n" +"./userfaultfd hugetlb-private 256 50\n\n" "# 10MiB-~6GiB 999 bounces anonymous test, " "continue forever unless an error triggers\n" "while ./userfaultfd anon $[RANDOM % 6000 + 10] 999; do true; done\n\n" ;
@@ -69,7 +69,7 @@ static void usage(void) {fprintf (stderr,"\nUsage: ./userfaultfd <test type> <MiB> <bounces>\n\n" );fprintf (stderr,"Supported <test type>: anon, hugetlb, " -"hugetlb_shared, shmem\n\n" ); +"hugetlb-private, shmem, shmem-private\n\n" );fprintf (stderr,"Examples:\n\n" );fprintf (stderr,"%s" , examples);exit (1 );
@@ -377,14 +377,17 @@ static void set_test_type(const char *type) }else if (!strcmp (type,"hugetlb" )) { test_type =TEST_HUGETLB ; uffd_test_ops = &hugetlb_uffd_test_ops; - }else if (!strcmp (type,"hugetlb_shared" )) { map_shared = true; + }else if (!strcmp (type,"hugetlb-private" )) { test_type =TEST_HUGETLB ; uffd_test_ops = &hugetlb_uffd_test_ops; }else if (!strcmp (type,"shmem" )) { map_shared = true; test_type =TEST_SHMEM ; uffd_test_ops = &shmem_uffd_test_ops; + }else if (!strcmp (type,"shmem-private" )) { + test_type =TEST_SHMEM ; + uffd_test_ops = &shmem_uffd_test_ops; } }
Signature · 1 line
2.39.1
Peter Xu
reviewerQuoted 26 lines · Peter Xu
UFFDIO_API(with syscall)... doneUFFDIO_API(with/dev/userfaultfd)... done
zeropage on anon... done
zeropage on shmem... done
zeropage on shmem-private... done
zeropage-hugetlb on hugetlb... done
zeropage-hugetlb on hugetlb-private... done
pagemap on anon... pagemap on anon... pagemap on anon... done wp-unpopulated on anon... done
minor on shmem... done
minor on hugetlb... done
minor-wp on shmem... done
minor-wp on hugetlb... done
minor-collapse on shmem... done
sigbus on anon... sigbus on anon... done
sigbus on shmem... sigbus on shmem... done
sigbus on shmem-private... sigbus on shmem-private... done sigbus on hugetlb... sigbus on hugetlb... done
sigbus on hugetlb-private... sigbus on hugetlb-private... done sigbus-wp on anon... sigbus-wp on anon... done
sigbus-wp on shmem... sigbus-wp on shmem... done sigbus-wp on shmem-private... sigbus-wp on shmem-private... done sigbus-wp on hugetlb... sigbus-wp on hugetlb... done sigbus-wp on hugetlb-private... sigbus-wp on hugetlb-private... done events on anon... events on anon... done
events on shmem... events on shmem... done
Quoted 8 lines
events on shmem-private... events on shmem-private... done events on hugetlb... events on hugetlb... done
events on hugetlb-private... events on hugetlb-private... done events-wp on anon... events-wp on anon... done
events-wp on shmem... events-wp on shmem... done events-wp on shmem-private... events-wp on shmem-private... done events-wp on hugetlb... events-wp on hugetlb... done events-wp on hugetlb-private... events-wp on hugetlb-private... done
Oops, the sigbus/events test do not look right here.. I think I slightly messed it up right before I post this set.
I'll squash below change into "selftests/mm: UFFDIO_API test" in the next post to fix this up:
===8<===
tools/testing/selftests/mm/uffd-unit-tests.c +2 -0
@@ -92,8 +92,10 @@ static void uffd_test_pass(void) }#define uffd_test_start (...) do { \ + printf("Testing "); \ printf(__VA_ARGS__); \ printf("... "); \ + fflush(stdout); \ uffd_test_acct.total++; \ } while (0)
===8<===
So it should look like:
===8<===
Testing UFFDIO_API (with syscall)... done Testing UFFDIO_API (with /dev/userfaultfd)... done Testing zeropage on anon... done
Testing zeropage on shmem... done
Testing zeropage on shmem-private... done Testing zeropage-hugetlb on hugetlb... done Testing zeropage-hugetlb on hugetlb-private... done Testing pagemap on anon... done
Testing wp-unpopulated on anon... done
Testing minor on shmem... done
Testing minor on hugetlb... done
Testing minor-wp on shmem... done
Testing minor-wp on hugetlb... done
Testing minor-collapse on shmem... done
Testing sigbus on anon... done
Testing sigbus on shmem... done
Testing sigbus on shmem-private... done
Testing sigbus on hugetlb... done
Testing sigbus on hugetlb-private... done Testing sigbus-wp on anon... done
Testing sigbus-wp on shmem... done
Testing sigbus-wp on shmem-private... done Testing sigbus-wp on hugetlb... done
Testing sigbus-wp on hugetlb-private... done Testing events on anon... done
Testing events on shmem... done
Testing events on shmem-private... done
Testing events on hugetlb... done
Testing events on hugetlb-private... done Testing events-wp on anon... done
Testing events-wp on shmem... done
Testing events-wp on shmem-private... done Testing events-wp on hugetlb... done
Testing events-wp on hugetlb-private... done Userfaults unit tests: pass=34, skip=0, fail=0 (total=34) ===8<===
Signature · 1 line
Peter Xu