selftest: mm: Test if hugepage does not get leaked during __bio_release_pages()
Subsystems (3)
PATCH v2 1/1 selftest: mm: Test if hugepage does not get leaked during __bio_release_pages()
Commit 1b151e2435fc ("block: Remove special-casing of compound pages") caused a change in behaviour when releasing the pages if the buffer does not start at the beginning of the page. This was because the calculation of the number of pages to release was incorrect.
This was fixed by commit 38b43539d64b ("block: Fix page refcounts for unaligned buffers in __bio_release_pages()").
We pin the user buffer during direct I/O writes. If this buffer is a hugepage, bio_release_page() will unpin it and decrement all references and pin counts at ->bi_end_io. However, if any references to the hugepage remain post-I/O, the hugepage will not be freed upon unmap, leading to a memory leak.
This patch verifies that a hugepage, used as a user buffer for DIO operations, is correctly freed upon unmapping, regardless of whether the offsets are aligned or unaligned w.r.t page boundary.
Test Result Fail Scenario (Without the fix)
--------------------------------------------------------
[]# ./hugetlb_dio
TAP version 13
1..4
No. Free pages before allocation : 7
No. Free pages after munmap : 7
ok 1 : Huge pages freed successfully !
No. Free pages before allocation : 7
No. Free pages after munmap : 7
ok 2 : Huge pages freed successfully !
No. Free pages before allocation : 7
No. Free pages after munmap : 7
ok 3 : Huge pages freed successfully !
No. Free pages before allocation : 7
No. Free pages after munmap : 6
not ok 4 : Huge pages not freed!
Totals: pass:3 fail:1 xfail:0 xpass:0 skip:0 error:0
Test Result PASS Scenario (With the fix)
---------------------------------------------------------
[]#./hugetlb_dio
TAP version 13
1..4
No. Free pages before allocation : 7
No. Free pages after munmap : 7
ok 1 : Huge pages freed successfully !
No. Free pages before allocation : 7
No. Free pages after munmap : 7
ok 2 : Huge pages freed successfully !
No. Free pages before allocation : 7
No. Free pages after munmap : 7
ok 3 : Huge pages freed successfully !
No. Free pages before allocation : 7
No. Free pages after munmap : 7
ok 4 : Huge pages freed successfully !
Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0
V2:
- Addressed all review commets from Muhammad Usama Anjum V1:
https://lore.kernel.org/all/[email protected]/#t
--- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/hugetlb_dio.c | 118 +++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 tools/testing/selftests/mm/hugetlb_dio.c
tools/testing/selftests/mm/Makefile +1 -0
@@ -73,6 +73,7 @@ TEST_GEN_FILES += ksm_functional_tests TEST_GEN_FILES += mdwe_test TEST_GEN_FILES += hugetlb_fault_after_madv TEST_GEN_FILES += hugetlb_madv_vs_map +TEST_GEN_FILES += hugetlb_dio ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty
tools/testing/selftests/mm/hugetlb_dio.c new file +118 -0
118 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 10 lines
tools/testing/selftests/mm/hugetlb_dio.c new file +9
-0+void run_dio_using_hugetlb (unsigned int start_off,unsigned int end_off) +{ +int fd; +char *buffer =NULL ; +char *orig_buffer =NULL ; +size_t h_pagesize =0 ; +size_t writesize; +int free_hpage_b =0 ; +int free_hpage_a =0 ;
tools/testing/selftests/mm/hugetlb_dio.c line 43
Quoted 10 lines
tools/testing/selftests/mm/hugetlb_dio.c new file +10
-0+ + writesize = end_off - start_off; + +/* Get the default huge page size */ + h_pagesize =default_huge_page_size (); +if (!h_pagesize) +ksft_exit_fail_msg ("Unable to determine huge page size\n" ); + +/* Open the file to DIO */ + fd =open ("/tmp" ,O_TMPFILE |O_RDWR |O_DIRECT );
I encountered a build error as follows in NixOS:
In file included from /nix/store/fwh4fxd747m0py3ib3s5abamia9nrf90-glibc-2.39-52-dev/include/fcntl.h:342,
from hugetlb_dio.c:15:
In function ‘open’,
inlined from ‘run_dio_using_hugetlb’ at hugetlb_dio.c:41:7:
/nix/store/fwh4fxd747m0py3ib3s5abamia9nrf90-glibc-2.39-52-dev/include/bits/fcntl2.h:50:11: error: call to ‘__open_missing_mode’ declared with attribute error: open with O_CREAT or O_TMPFILE in second argument needs 3 arguments
50 | __open_missing_mode ();
I saw a commit that fixed similar issues with open syscall before:8b65ef5ad486 ("selftests/mm: Fix build with _FORTIFY_SOURCE")
So something like this should fix the issue?
- fd = open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT);
+ fd = open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT, 0664);Quoted 3 lines
tools/testing/selftests/mm/hugetlb_dio.c new file +3
-0+if (fd <0 ) +ksft_exit_fail_msg ("Error opening file" ); +
Quoted 34 lines · Pankaj Raghav
Quoted 9 lines
tools/testing/selftests/mm/hugetlb_dio.c new file +9
-0+void run_dio_using_hugetlb (unsigned int start_off,unsigned int end_off) +{ +int fd; +char *buffer =NULL ; +char *orig_buffer =NULL ; +size_t h_pagesize =0 ; +size_t writesize; +int free_hpage_b =0 ; +int free_hpage_a =0 ;tools/testing/selftests/mm/hugetlb_dio.c line 43
Quoted 10 lines
tools/testing/selftests/mm/hugetlb_dio.c new file +10
-0+ + writesize = end_off - start_off; + +/* Get the default huge page size */ + h_pagesize =default_huge_page_size (); +if (!h_pagesize) +ksft_exit_fail_msg ("Unable to determine huge page size\n" ); + +/* Open the file to DIO */ + fd =open ("/tmp" ,O_TMPFILE |O_RDWR |O_DIRECT );I encountered a build error as follows in NixOS:
In file included from /nix/store/fwh4fxd747m0py3ib3s5abamia9nrf90-glibc-2.39-52-dev/include/fcntl.h:342,
from hugetlb_dio.c:15: In function ‘open’, inlined from ‘run_dio_using_hugetlb’ at hugetlb_dio.c:41:7: /nix/store/fwh4fxd747m0py3ib3s5abamia9nrf90-glibc-2.39-52-dev/include/bits/fcntl2.h:50:11: error: call to ‘__open_missing_mode’ declared with attribute error: open with O_CREAT or O_TMPFILE in second argument needs 3 arguments 50 | __open_missing_mode ();I saw a commit that fixed similar issues with open syscall before:
8b65ef5ad486("selftests/mm: Fix build with_FORTIFY_SOURCE")So something like this should fix the issue?
Review
Quoted 2 lines
(quoted diff) +1
-1- fd = open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT); + fd = open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT, 0664);
Thank you Pankaj.
I am able to reproduce this error with "-D_FORTIFY_SOURCE=2 -O2". I will post v3 with this fix.
-donet
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/mm/hugetlb_dio.c new file +3
-0+if (fd <0 ) +ksft_exit_fail_msg ("Error opening file" ); +