Skip to content

Commit e08c0b8

Browse files
author
Fox Snowpatch
committed
1 parent 182544a commit e08c0b8

8 files changed

Lines changed: 152 additions & 95 deletions

File tree

tools/testing/selftests/mm/charge_reserved_hugetlb.sh

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ if [[ $(id -u) -ne 0 ]]; then
1212
fi
1313

1414
nr_hugepgs=$(cat /proc/sys/vm/nr_hugepages)
15+
trap 'echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages' EXIT
1516

1617
fault_limit_file=limit_in_bytes
1718
reservation_limit_file=rsvd.limit_in_bytes
@@ -65,7 +66,6 @@ function cleanup() {
6566
if [[ -e $cgroup_path/hugetlb_cgroup_test2 ]]; then
6667
rmdir $cgroup_path/hugetlb_cgroup_test2
6768
fi
68-
echo 0 >/proc/sys/vm/nr_hugepages
6969
echo CLEANUP DONE
7070
}
7171

@@ -89,6 +89,15 @@ function get_machine_hugepage_size() {
8989
}
9090

9191
MB=$(get_machine_hugepage_size)
92+
if (( MB >= 1024 )); then
93+
# For 1GB hugepages
94+
UNIT="GB"
95+
MB_DISPLAY=$((MB / 1024))
96+
else
97+
# For 2MB hugepages
98+
UNIT="MB"
99+
MB_DISPLAY=$MB
100+
fi
92101

93102
function setup_cgroup() {
94103
local name="$1"
@@ -98,11 +107,12 @@ function setup_cgroup() {
98107
mkdir $cgroup_path/$name
99108

100109
echo writing cgroup limit: "$cgroup_limit"
101-
echo "$cgroup_limit" >$cgroup_path/$name/hugetlb.${MB}MB.$fault_limit_file
110+
echo "$cgroup_limit" > \
111+
$cgroup_path/$name/hugetlb.${MB_DISPLAY}${UNIT}.$fault_limit_file
102112

103113
echo writing reservation limit: "$reservation_limit"
104114
echo "$reservation_limit" > \
105-
$cgroup_path/$name/hugetlb.${MB}MB.$reservation_limit_file
115+
$cgroup_path/$name/hugetlb.${MB_DISPLAY}${UNIT}.$reservation_limit_file
106116

107117
if [ -e "$cgroup_path/$name/cpuset.cpus" ]; then
108118
echo 0 >$cgroup_path/$name/cpuset.cpus
@@ -137,23 +147,23 @@ function wait_for_file_value() {
137147

138148
function wait_for_hugetlb_memory_to_get_depleted() {
139149
local cgroup="$1"
140-
local path="$cgroup_path/$cgroup/hugetlb.${MB}MB.$reservation_usage_file"
150+
local path="$cgroup_path/$cgroup/hugetlb.${MB_DISPLAY}${UNIT}.$reservation_usage_file"
141151

142152
wait_for_file_value "$path" "0"
143153
}
144154

145155
function wait_for_hugetlb_memory_to_get_reserved() {
146156
local cgroup="$1"
147157
local size="$2"
148-
local path="$cgroup_path/$cgroup/hugetlb.${MB}MB.$reservation_usage_file"
158+
local path="$cgroup_path/$cgroup/hugetlb.${MB_DISPLAY}${UNIT}.$reservation_usage_file"
149159

150160
wait_for_file_value "$path" "$size"
151161
}
152162

153163
function wait_for_hugetlb_memory_to_get_written() {
154164
local cgroup="$1"
155165
local size="$2"
156-
local path="$cgroup_path/$cgroup/hugetlb.${MB}MB.$fault_usage_file"
166+
local path="$cgroup_path/$cgroup/hugetlb.${MB_DISPLAY}${UNIT}.$fault_usage_file"
157167

158168
wait_for_file_value "$path" "$size"
159169
}
@@ -175,8 +185,8 @@ function write_hugetlbfs_and_get_usage() {
175185
hugetlb_difference=0
176186
reserved_difference=0
177187

178-
local hugetlb_usage=$cgroup_path/$cgroup/hugetlb.${MB}MB.$fault_usage_file
179-
local reserved_usage=$cgroup_path/$cgroup/hugetlb.${MB}MB.$reservation_usage_file
188+
local hugetlb_usage=$cgroup_path/$cgroup/hugetlb.${MB_DISPLAY}${UNIT}.$fault_usage_file
189+
local reserved_usage=$cgroup_path/$cgroup/hugetlb.${MB_DISPLAY}${UNIT}.$reservation_usage_file
180190

181191
local hugetlb_before=$(cat $hugetlb_usage)
182192
local reserved_before=$(cat $reserved_usage)
@@ -307,8 +317,10 @@ function run_test() {
307317

308318
cleanup_hugetlb_memory "hugetlb_cgroup_test"
309319

310-
local final_hugetlb=$(cat $cgroup_path/hugetlb_cgroup_test/hugetlb.${MB}MB.$fault_usage_file)
311-
local final_reservation=$(cat $cgroup_path/hugetlb_cgroup_test/hugetlb.${MB}MB.$reservation_usage_file)
320+
local final_hugetlb=$(cat \
321+
$cgroup_path/hugetlb_cgroup_test/hugetlb.${MB_DISPLAY}${UNIT}.$fault_usage_file)
322+
local final_reservation=$(cat \
323+
$cgroup_path/hugetlb_cgroup_test/hugetlb.${MB_DISPLAY}${UNIT}.$reservation_usage_file)
312324

313325
echo $hugetlb_difference
314326
echo $reserved_difference
@@ -364,10 +376,14 @@ function run_multiple_cgroup_test() {
364376
reservation_failed1=$reservation_failed
365377
oom_killed1=$oom_killed
366378

367-
local cgroup1_hugetlb_usage=$cgroup_path/hugetlb_cgroup_test1/hugetlb.${MB}MB.$fault_usage_file
368-
local cgroup1_reservation_usage=$cgroup_path/hugetlb_cgroup_test1/hugetlb.${MB}MB.$reservation_usage_file
369-
local cgroup2_hugetlb_usage=$cgroup_path/hugetlb_cgroup_test2/hugetlb.${MB}MB.$fault_usage_file
370-
local cgroup2_reservation_usage=$cgroup_path/hugetlb_cgroup_test2/hugetlb.${MB}MB.$reservation_usage_file
379+
local cgroup1_hugetlb_usage=\
380+
$cgroup_path/hugetlb_cgroup_test1/hugetlb.${MB_DISPLAY}${UNIT}.$fault_usage_file
381+
local cgroup1_reservation_usage=\
382+
$cgroup_path/hugetlb_cgroup_test1/hugetlb.${MB_DISPLAY}${UNIT}.$reservation_usage_file
383+
local cgroup2_hugetlb_usage=\
384+
$cgroup_path/hugetlb_cgroup_test2/hugetlb.${MB_DISPLAY}${UNIT}.$fault_usage_file
385+
local cgroup2_reservation_usage=\
386+
$cgroup_path/hugetlb_cgroup_test2/hugetlb.${MB_DISPLAY}${UNIT}.$reservation_usage_file
371387

372388
local usage_before_second_write=$(cat $cgroup1_hugetlb_usage)
373389
local reservation_usage_before_second_write=$(cat $cgroup1_reservation_usage)
@@ -594,4 +610,3 @@ if [[ $do_umount ]]; then
594610
rmdir $cgroup_path
595611
fi
596612

597-
echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages

tools/testing/selftests/mm/compaction_test.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ int main(int argc, char **argv)
261261
mem_fragmentable_MB -= MAP_SIZE_MB;
262262
}
263263

264+
/* Unmap every other entry in the list to create fragmentation with
265+
* locked pages before invoking check_compaction().
266+
*/
264267
for (entry = list; entry != NULL; entry = entry->next) {
265268
munmap(entry->map, MAP_SIZE);
266269
if (!entry->next)

tools/testing/selftests/mm/hugepage-mremap.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#define MB_TO_BYTES(x) (x * 1024 * 1024)
3232

3333
#define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC)
34-
#define FLAGS (MAP_SHARED | MAP_ANONYMOUS)
34+
#define FLAGS (MAP_HUGETLB | MAP_SHARED)
35+
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
3536

3637
static void check_bytes(char *addr)
3738
{
@@ -85,31 +86,21 @@ static void register_region_with_uffd(char *addr, size_t len)
8586
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
8687
ksft_exit_fail_msg("ioctl-UFFDIO_API: %s\n", strerror(errno));
8788

88-
/* Create a private anonymous mapping. The memory will be
89-
* demand-zero paged--that is, not yet allocated. When we
90-
* actually touch the memory, it will be allocated via
91-
* the userfaultfd.
92-
*/
93-
94-
addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
95-
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
96-
if (addr == MAP_FAILED)
97-
ksft_exit_fail_msg("mmap: %s\n", strerror(errno));
98-
99-
ksft_print_msg("Address returned by mmap() = %p\n", addr);
100-
101-
/* Register the memory range of the mapping we just created for
102-
* handling by the userfaultfd object. In mode, we request to track
103-
* missing pages (i.e., pages that have not yet been faulted in).
89+
/* Register the passed memory range for handling by the userfaultfd object.
90+
* In mode, we request to track missing pages
91+
* (i.e., pages that have not yet been faulted in).
10492
*/
10593
if (uffd_register(uffd, addr, len, true, false, false))
10694
ksft_exit_fail_msg("ioctl-UFFDIO_REGISTER: %s\n", strerror(errno));
95+
96+
ksft_print_msg("Registered memory at address %p with userfaultfd\n", addr);
10797
}
10898

10999
int main(int argc, char *argv[])
110100
{
111101
size_t length = 0;
112102
int ret = 0, fd;
103+
size_t hpage_size;
113104

114105
ksft_print_header();
115106
ksft_set_plan(1);
@@ -126,29 +117,34 @@ int main(int argc, char *argv[])
126117
length = DEFAULT_LENGTH_MB;
127118

128119
length = MB_TO_BYTES(length);
120+
121+
hpage_size = default_huge_page_size();
122+
if (!hpage_size)
123+
ksft_exit_skip("Unable to determine huge page size\n");
124+
125+
/* Ensure length is hugepage aligned */
126+
length = ALIGN(length, hpage_size);
127+
129128
fd = memfd_create(argv[0], MFD_HUGETLB);
130129
if (fd < 0)
131130
ksft_exit_fail_msg("Open failed: %s\n", strerror(errno));
132131

133132
/* mmap to a PUD aligned address to hopefully trigger pmd sharing. */
134133
unsigned long suggested_addr = 0x7eaa40000000;
135-
void *haddr = mmap((void *)suggested_addr, length, PROTECTION,
136-
MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
134+
void *haddr = mmap((void *)suggested_addr, length, PROTECTION, FLAGS, fd, 0);
137135
ksft_print_msg("Map haddr: Returned address is %p\n", haddr);
138136
if (haddr == MAP_FAILED)
139137
ksft_exit_fail_msg("mmap1: %s\n", strerror(errno));
140138

141139
/* mmap again to a dummy address to hopefully trigger pmd sharing. */
142140
suggested_addr = 0x7daa40000000;
143-
void *daddr = mmap((void *)suggested_addr, length, PROTECTION,
144-
MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
141+
void *daddr = mmap((void *)suggested_addr, length, PROTECTION, FLAGS, fd, 0);
145142
ksft_print_msg("Map daddr: Returned address is %p\n", daddr);
146143
if (daddr == MAP_FAILED)
147144
ksft_exit_fail_msg("mmap3: %s\n", strerror(errno));
148145

149146
suggested_addr = 0x7faa40000000;
150-
void *vaddr =
151-
mmap((void *)suggested_addr, length, PROTECTION, FLAGS, -1, 0);
147+
void *vaddr = mmap((void *)suggested_addr, length, PROTECTION, FLAGS, fd, 0);
152148
ksft_print_msg("Map vaddr: Returned address is %p\n", vaddr);
153149
if (vaddr == MAP_FAILED)
154150
ksft_exit_fail_msg("mmap2: %s\n", strerror(errno));

tools/testing/selftests/mm/hugetlb_reparenting_test.sh

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ if [[ $(id -u) -ne 0 ]]; then
1212
fi
1313

1414
nr_hugepgs=$(cat /proc/sys/vm/nr_hugepages)
15+
trap 'echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages' EXIT
16+
1517
usage_file=usage_in_bytes
1618

1719
if [[ "$1" == "-cgroup-v2" ]]; then
@@ -46,6 +48,13 @@ function get_machine_hugepage_size() {
4648
}
4749

4850
MB=$(get_machine_hugepage_size)
51+
if (( MB >= 1024 )); then
52+
UNIT="GB"
53+
MB_DISPLAY=$((MB / 1024))
54+
else
55+
UNIT="MB"
56+
MB_DISPLAY=$MB
57+
fi
4958

5059
function cleanup() {
5160
echo cleanup
@@ -56,7 +65,6 @@ function cleanup() {
5665
rmdir "$CGROUP_ROOT"/a/b 2>/dev/null
5766
rmdir "$CGROUP_ROOT"/a 2>/dev/null
5867
rmdir "$CGROUP_ROOT"/test1 2>/dev/null
59-
echo $nr_hugepgs >/proc/sys/vm/nr_hugepages
6068
set -e
6169
}
6270

@@ -87,6 +95,7 @@ function assert_with_retry() {
8795
if [[ $elapsed -ge $timeout ]]; then
8896
echo "actual = $((${actual%% *} / 1024 / 1024)) MB"
8997
echo "expected = $((${expected%% *} / 1024 / 1024)) MB"
98+
echo FAIL
9099
cleanup
91100
exit 1
92101
fi
@@ -96,22 +105,19 @@ function assert_with_retry() {
96105
}
97106

98107
function assert_state() {
99-
local expected_a="$1"
100-
local expected_a_hugetlb="$2"
101-
local expected_b=""
108+
local expected_a_hugetlb="$1"
102109
local expected_b_hugetlb=""
103110

104-
if [ ! -z ${3:-} ] && [ ! -z ${4:-} ]; then
105-
expected_b="$3"
106-
expected_b_hugetlb="$4"
111+
if [ ! -z ${2:-} ]; then
112+
expected_b_hugetlb="$2"
107113
fi
108114

109-
assert_with_retry "$CGROUP_ROOT/a/memory.$usage_file" "$expected_a"
110-
assert_with_retry "$CGROUP_ROOT/a/hugetlb.${MB}MB.$usage_file" "$expected_a_hugetlb"
115+
assert_with_retry \
116+
"$CGROUP_ROOT/a/hugetlb.${MB_DISPLAY}${UNIT}.$usage_file" "$expected_a_hugetlb"
111117

112-
if [[ -n "$expected_b" && -n "$expected_b_hugetlb" ]]; then
113-
assert_with_retry "$CGROUP_ROOT/a/b/memory.$usage_file" "$expected_b"
114-
assert_with_retry "$CGROUP_ROOT/a/b/hugetlb.${MB}MB.$usage_file" "$expected_b_hugetlb"
118+
if [[ -n "$expected_b_hugetlb" ]]; then
119+
assert_with_retry \
120+
"$CGROUP_ROOT/a/b/hugetlb.${MB_DISPLAY}${UNIT}.$usage_file" "$expected_b_hugetlb"
115121
fi
116122
}
117123

@@ -143,18 +149,16 @@ write_hugetlbfs() {
143149
local size="$3"
144150

145151
if [[ $cgroup2 ]]; then
146-
echo $$ >$CGROUP_ROOT/$cgroup/cgroup.procs
152+
cg_file="$CGROUP_ROOT/$cgroup/cgroup.procs"
147153
else
148154
echo 0 >$CGROUP_ROOT/$cgroup/cpuset.mems
149155
echo 0 >$CGROUP_ROOT/$cgroup/cpuset.cpus
150-
echo $$ >"$CGROUP_ROOT/$cgroup/tasks"
151-
fi
152-
./write_to_hugetlbfs -p "$path" -s "$size" -m 0 -o
153-
if [[ $cgroup2 ]]; then
154-
echo $$ >$CGROUP_ROOT/cgroup.procs
155-
else
156-
echo $$ >"$CGROUP_ROOT/tasks"
156+
cg_file="$CGROUP_ROOT/$cgroup/tasks"
157157
fi
158+
159+
# Spawn helper to join cgroup before exec to ensure correct cgroup accounting
160+
bash -c "echo \$\$ > '$cg_file'; exec ./write_to_hugetlbfs -p '$path' -s '$size' -m 0 -o" & pid=$!
161+
wait "$pid"
158162
echo
159163
}
160164

@@ -192,21 +196,21 @@ if [[ ! $cgroup2 ]]; then
192196
write_hugetlbfs a "$MNT"/test $size
193197

194198
echo Assert memory charged correctly for parent use.
195-
assert_state 0 $size 0 0
199+
assert_state $size 0
196200

197201
write_hugetlbfs a/b "$MNT"/test2 $size
198202

199203
echo Assert memory charged correctly for child use.
200-
assert_state 0 $(($size * 2)) 0 $size
204+
assert_state $(($size * 2)) $size
201205

202206
rmdir "$CGROUP_ROOT"/a/b
203207
echo Assert memory reparent correctly.
204-
assert_state 0 $(($size * 2))
208+
assert_state $(($size * 2))
205209

206210
rm -rf "$MNT"/*
207211
umount "$MNT"
208212
echo Assert memory uncharged correctly.
209-
assert_state 0 0
213+
assert_state 0
210214

211215
cleanup
212216
fi
@@ -220,16 +224,16 @@ echo write
220224
write_hugetlbfs a/b "$MNT"/test2 $size
221225

222226
echo Assert memory charged correctly for child only use.
223-
assert_state 0 $(($size)) 0 $size
227+
assert_state $(($size)) $size
224228

225229
rmdir "$CGROUP_ROOT"/a/b
226230
echo Assert memory reparent correctly.
227-
assert_state 0 $size
231+
assert_state $size
228232

229233
rm -rf "$MNT"/*
230234
umount "$MNT"
231235
echo Assert memory uncharged correctly.
232-
assert_state 0 0
236+
assert_state 0
233237

234238
cleanup
235239

@@ -240,4 +244,3 @@ if [[ $do_umount ]]; then
240244
rm -rf $CGROUP_ROOT
241245
fi
242246

243-
echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages

0 commit comments

Comments
 (0)