Skip to content

Commit 3a103b5

Browse files
donettom-1akpm00
authored andcommitted
selftest: mm: Test if hugepage does not get leaked during __bio_release_pages()
Commit 1b151e2 ("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 38b4353 ("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 [donettom@linux.ibm.com: address review comments from Muhammad] Link: https://lkml.kernel.org/r/20240604132801.23377-1-donettom@linux.ibm.com [donettom@linux.ibm.com: add this test to run_vmtests.sh] Link: https://lkml.kernel.org/r/20240607182000.6494-1-donettom@linux.ibm.com Link: https://lkml.kernel.org/r/20240523063905.3173-1-donettom@linux.ibm.com Fixes: 38b4353 ("block: Fix page refcounts for unaligned buffers in __bio_release_pages()") Signed-off-by: Donet Tom <donettom@linux.ibm.com> Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Co-developed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com> Cc: David Hildenbrand <david@redhat.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Mike Rapoport (IBM) <rppt@kernel.org> Cc: Muchun Song <songmuchun@bytedance.com> Cc: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Tony Battersby <tonyb@cybernetics.com> Cc: Jens Axboe <axboe@kernel.dk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent a831896 commit 3a103b5

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

tools/testing/selftests/mm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ TEST_GEN_FILES += ksm_functional_tests
7373
TEST_GEN_FILES += mdwe_test
7474
TEST_GEN_FILES += hugetlb_fault_after_madv
7575
TEST_GEN_FILES += hugetlb_madv_vs_map
76+
TEST_GEN_FILES += hugetlb_dio
7677

7778
ifneq ($(ARCH),arm64)
7879
TEST_GEN_FILES += soft-dirty
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* This program tests for hugepage leaks after DIO writes to a file using a
4+
* hugepage as the user buffer. During DIO, the user buffer is pinned and
5+
* should be properly unpinned upon completion. This patch verifies that the
6+
* kernel correctly unpins the buffer at DIO completion for both aligned and
7+
* unaligned user buffer offsets (w.r.t page boundary), ensuring the hugepage
8+
* is freed upon unmapping.
9+
*/
10+
11+
#define _GNU_SOURCE
12+
#include <stdio.h>
13+
#include <sys/stat.h>
14+
#include <stdlib.h>
15+
#include <fcntl.h>
16+
#include <stdint.h>
17+
#include <unistd.h>
18+
#include <string.h>
19+
#include <sys/mman.h>
20+
#include "vm_util.h"
21+
#include "../kselftest.h"
22+
23+
void run_dio_using_hugetlb(unsigned int start_off, unsigned int end_off)
24+
{
25+
int fd;
26+
char *buffer = NULL;
27+
char *orig_buffer = NULL;
28+
size_t h_pagesize = 0;
29+
size_t writesize;
30+
int free_hpage_b = 0;
31+
int free_hpage_a = 0;
32+
const int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB;
33+
const int mmap_prot = PROT_READ | PROT_WRITE;
34+
35+
writesize = end_off - start_off;
36+
37+
/* Get the default huge page size */
38+
h_pagesize = default_huge_page_size();
39+
if (!h_pagesize)
40+
ksft_exit_fail_msg("Unable to determine huge page size\n");
41+
42+
/* Open the file to DIO */
43+
fd = open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT, 0664);
44+
if (fd < 0)
45+
ksft_exit_fail_perror("Error opening file\n");
46+
47+
/* Get the free huge pages before allocation */
48+
free_hpage_b = get_free_hugepages();
49+
if (free_hpage_b == 0) {
50+
close(fd);
51+
ksft_exit_skip("No free hugepage, exiting!\n");
52+
}
53+
54+
/* Allocate a hugetlb page */
55+
orig_buffer = mmap(NULL, h_pagesize, mmap_prot, mmap_flags, -1, 0);
56+
if (orig_buffer == MAP_FAILED) {
57+
close(fd);
58+
ksft_exit_fail_perror("Error mapping memory\n");
59+
}
60+
buffer = orig_buffer;
61+
buffer += start_off;
62+
63+
memset(buffer, 'A', writesize);
64+
65+
/* Write the buffer to the file */
66+
if (write(fd, buffer, writesize) != (writesize)) {
67+
munmap(orig_buffer, h_pagesize);
68+
close(fd);
69+
ksft_exit_fail_perror("Error writing to file\n");
70+
}
71+
72+
/* unmap the huge page */
73+
munmap(orig_buffer, h_pagesize);
74+
close(fd);
75+
76+
/* Get the free huge pages after unmap*/
77+
free_hpage_a = get_free_hugepages();
78+
79+
/*
80+
* If the no. of free hugepages before allocation and after unmap does
81+
* not match - that means there could still be a page which is pinned.
82+
*/
83+
if (free_hpage_a != free_hpage_b) {
84+
ksft_print_msg("No. Free pages before allocation : %d\n", free_hpage_b);
85+
ksft_print_msg("No. Free pages after munmap : %d\n", free_hpage_a);
86+
ksft_test_result_fail(": Huge pages not freed!\n");
87+
} else {
88+
ksft_print_msg("No. Free pages before allocation : %d\n", free_hpage_b);
89+
ksft_print_msg("No. Free pages after munmap : %d\n", free_hpage_a);
90+
ksft_test_result_pass(": Huge pages freed successfully !\n");
91+
}
92+
}
93+
94+
int main(void)
95+
{
96+
size_t pagesize = 0;
97+
98+
ksft_print_header();
99+
ksft_set_plan(4);
100+
101+
/* Get base page size */
102+
pagesize = psize();
103+
104+
/* start and end is aligned to pagesize */
105+
run_dio_using_hugetlb(0, (pagesize * 3));
106+
107+
/* start is aligned but end is not aligned */
108+
run_dio_using_hugetlb(0, (pagesize * 3) - (pagesize / 2));
109+
110+
/* start is unaligned and end is aligned */
111+
run_dio_using_hugetlb(pagesize / 2, (pagesize * 3));
112+
113+
/* both start and end are unaligned */
114+
run_dio_using_hugetlb(pagesize / 2, (pagesize * 3) + (pagesize / 2));
115+
116+
ksft_finished();
117+
}

tools/testing/selftests/mm/run_vmtests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ CATEGORY="hugetlb" run_test ./map_hugetlb
265265
CATEGORY="hugetlb" run_test ./hugepage-mremap
266266
CATEGORY="hugetlb" run_test ./hugepage-vmemmap
267267
CATEGORY="hugetlb" run_test ./hugetlb-madvise
268+
CATEGORY="hugetlb" run_test ./hugetlb_dio
268269

269270
nr_hugepages_tmp=$(cat /proc/sys/vm/nr_hugepages)
270271
# For this test, we need one and just one huge page

0 commit comments

Comments
 (0)