Skip to content

Commit fb9293b

Browse files
Dev Jainakpm00
Dev Jain
authored andcommitted
selftests/mm: compaction_test: fix bogus test success and reduce probability of OOM-killer invocation
Reset nr_hugepages to zero before the start of the test. If a non-zero number of hugepages is already set before the start of the test, the following problems arise: - The probability of the test getting OOM-killed increases. Proof: The test wants to run on 80% of available memory to prevent OOM-killing (see original code comments). Let the value of mem_free at the start of the test, when nr_hugepages = 0, be x. In the other case, when nr_hugepages > 0, let the memory consumed by hugepages be y. In the former case, the test operates on 0.8 * x of memory. In the latter, the test operates on 0.8 * (x - y) of memory, with y already filled, hence, memory consumed is y + 0.8 * (x - y) = 0.8 * x + 0.2 * y > 0.8 * x. Q.E.D - The probability of a bogus test success increases. Proof: Let the memory consumed by hugepages be greater than 25% of x, with x and y defined as above. The definition of compaction_index is c_index = (x - y)/z where z is the memory consumed by hugepages after trying to increase them again. In check_compaction(), we set the number of hugepages to zero, and then increase them back; the probability that they will be set back to consume at least y amount of memory again is very high (since there is not much delay between the two attempts of changing nr_hugepages). Hence, z >= y > (x/4) (by the 25% assumption). Therefore, c_index = (x - y)/z <= (x - y)/y = x/y - 1 < 4 - 1 = 3 hence, c_index can always be forced to be less than 3, thereby the test succeeding always. Q.E.D Link: https://lkml.kernel.org/r/20240521074358.675031-4-dev.jain@arm.com Fixes: bd67d5c ("Test compaction of mlocked memory") Signed-off-by: Dev Jain <dev.jain@arm.com> Cc: <stable@vger.kernel.org> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Sri Jayaramappa <sjayaram@akamai.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 9ad665e commit fb9293b

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

tools/testing/selftests/mm/compaction_test.c

+49-22
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@ int prereq(void)
8282
return -1;
8383
}
8484

85-
int check_compaction(unsigned long mem_free, unsigned long hugepage_size)
85+
int check_compaction(unsigned long mem_free, unsigned long hugepage_size,
86+
unsigned long initial_nr_hugepages)
8687
{
8788
unsigned long nr_hugepages_ul;
8889
int fd, ret = -1;
8990
int compaction_index = 0;
90-
char initial_nr_hugepages[20] = {0};
9191
char nr_hugepages[20] = {0};
92+
char init_nr_hugepages[20] = {0};
93+
94+
sprintf(init_nr_hugepages, "%lu", initial_nr_hugepages);
9295

9396
/* We want to test with 80% of available memory. Else, OOM killer comes
9497
in to play */
@@ -102,23 +105,6 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size)
102105
goto out;
103106
}
104107

105-
if (read(fd, initial_nr_hugepages, sizeof(initial_nr_hugepages)) <= 0) {
106-
ksft_print_msg("Failed to read from /proc/sys/vm/nr_hugepages: %s\n",
107-
strerror(errno));
108-
goto close_fd;
109-
}
110-
111-
lseek(fd, 0, SEEK_SET);
112-
113-
/* Start with the initial condition of 0 huge pages*/
114-
if (write(fd, "0", sizeof(char)) != sizeof(char)) {
115-
ksft_print_msg("Failed to write 0 to /proc/sys/vm/nr_hugepages: %s\n",
116-
strerror(errno));
117-
goto close_fd;
118-
}
119-
120-
lseek(fd, 0, SEEK_SET);
121-
122108
/* Request a large number of huge pages. The Kernel will allocate
123109
as much as it can */
124110
if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) {
@@ -146,8 +132,8 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size)
146132

147133
lseek(fd, 0, SEEK_SET);
148134

149-
if (write(fd, initial_nr_hugepages, strlen(initial_nr_hugepages))
150-
!= strlen(initial_nr_hugepages)) {
135+
if (write(fd, init_nr_hugepages, strlen(init_nr_hugepages))
136+
!= strlen(init_nr_hugepages)) {
151137
ksft_print_msg("Failed to write value to /proc/sys/vm/nr_hugepages: %s\n",
152138
strerror(errno));
153139
goto close_fd;
@@ -171,6 +157,41 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size)
171157
return ret;
172158
}
173159

160+
int set_zero_hugepages(unsigned long *initial_nr_hugepages)
161+
{
162+
int fd, ret = -1;
163+
char nr_hugepages[20] = {0};
164+
165+
fd = open("/proc/sys/vm/nr_hugepages", O_RDWR | O_NONBLOCK);
166+
if (fd < 0) {
167+
ksft_print_msg("Failed to open /proc/sys/vm/nr_hugepages: %s\n",
168+
strerror(errno));
169+
goto out;
170+
}
171+
if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
172+
ksft_print_msg("Failed to read from /proc/sys/vm/nr_hugepages: %s\n",
173+
strerror(errno));
174+
goto close_fd;
175+
}
176+
177+
lseek(fd, 0, SEEK_SET);
178+
179+
/* Start with the initial condition of 0 huge pages */
180+
if (write(fd, "0", sizeof(char)) != sizeof(char)) {
181+
ksft_print_msg("Failed to write 0 to /proc/sys/vm/nr_hugepages: %s\n",
182+
strerror(errno));
183+
goto close_fd;
184+
}
185+
186+
*initial_nr_hugepages = strtoul(nr_hugepages, NULL, 10);
187+
ret = 0;
188+
189+
close_fd:
190+
close(fd);
191+
192+
out:
193+
return ret;
194+
}
174195

175196
int main(int argc, char **argv)
176197
{
@@ -181,6 +202,7 @@ int main(int argc, char **argv)
181202
unsigned long mem_free = 0;
182203
unsigned long hugepage_size = 0;
183204
long mem_fragmentable_MB = 0;
205+
unsigned long initial_nr_hugepages;
184206

185207
ksft_print_header();
186208

@@ -189,6 +211,10 @@ int main(int argc, char **argv)
189211

190212
ksft_set_plan(1);
191213

214+
/* Start the test without hugepages reducing mem_free */
215+
if (set_zero_hugepages(&initial_nr_hugepages))
216+
ksft_exit_fail();
217+
192218
lim.rlim_cur = RLIM_INFINITY;
193219
lim.rlim_max = RLIM_INFINITY;
194220
if (setrlimit(RLIMIT_MEMLOCK, &lim))
@@ -232,7 +258,8 @@ int main(int argc, char **argv)
232258
entry = entry->next;
233259
}
234260

235-
if (check_compaction(mem_free, hugepage_size) == 0)
261+
if (check_compaction(mem_free, hugepage_size,
262+
initial_nr_hugepages) == 0)
236263
ksft_exit_pass();
237264

238265
ksft_exit_fail();

0 commit comments

Comments
 (0)