Skip to content

Commit 46a072e

Browse files
Dev Jaingregkh
authored andcommitted
selftests/mm: compaction_test: fix bogus test success on Aarch64
[ Upstream commit d4202e6 ] Patch series "Fixes for compaction_test", v2. The compaction_test memory selftest introduces fragmentation in memory and then tries to allocate as many hugepages as possible. This series addresses some problems. On Aarch64, if nr_hugepages == 0, then the test trivially succeeds since compaction_index becomes 0, which is less than 3, due to no division by zero exception being raised. We fix that by checking for division by zero. Secondly, correctly set the number of hugepages to zero before trying to set a large number of them. Now, consider a situation in which, at the start of the test, a non-zero number of hugepages have been already set (while running the entire selftests/mm suite, or manually by the admin). The test operates on 80% of memory to avoid OOM-killer invocation, and because some memory is already blocked by hugepages, it would increase the chance of OOM-killing. Also, since mem_free used in check_compaction() is the value before we set nr_hugepages to zero, the chance that the compaction_index will be small is very high if the preset nr_hugepages was high, leading to a bogus test success. This patch (of 3): Currently, if at runtime we are not able to allocate a huge page, the test will trivially pass on Aarch64 due to no exception being raised on division by zero while computing compaction_index. Fix that by checking for nr_hugepages == 0. Anyways, in general, avoid a division by zero by exiting the program beforehand. While at it, fix a typo, and handle the case where the number of hugepages may overflow an integer. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: bd67d5c ("Test compaction of mlocked memory") Signed-off-by: Dev Jain <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Sri Jayaramappa <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 264b8a7 commit 46a072e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

tools/testing/selftests/vm/compaction_test.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ int prereq(void)
8282
return -1;
8383
}
8484

85-
int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
85+
int check_compaction(unsigned long mem_free, unsigned long hugepage_size)
8686
{
87+
unsigned long nr_hugepages_ul;
8788
int fd, ret = -1;
8889
int compaction_index = 0;
89-
char initial_nr_hugepages[10] = {0};
90-
char nr_hugepages[10] = {0};
90+
char initial_nr_hugepages[20] = {0};
91+
char nr_hugepages[20] = {0};
9192

9293
/* We want to test with 80% of available memory. Else, OOM killer comes
9394
in to play */
@@ -136,7 +137,12 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
136137

137138
/* We should have been able to request at least 1/3 rd of the memory in
138139
huge pages */
139-
compaction_index = mem_free/(atoi(nr_hugepages) * hugepage_size);
140+
nr_hugepages_ul = strtoul(nr_hugepages, NULL, 10);
141+
if (!nr_hugepages_ul) {
142+
ksft_print_msg("ERROR: No memory is available as huge pages\n");
143+
goto close_fd;
144+
}
145+
compaction_index = mem_free/(nr_hugepages_ul * hugepage_size);
140146

141147
lseek(fd, 0, SEEK_SET);
142148

@@ -147,11 +153,11 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
147153
goto close_fd;
148154
}
149155

150-
ksft_print_msg("Number of huge pages allocated = %d\n",
151-
atoi(nr_hugepages));
156+
ksft_print_msg("Number of huge pages allocated = %lu\n",
157+
nr_hugepages_ul);
152158

153159
if (compaction_index > 3) {
154-
ksft_print_msg("ERROR: Less that 1/%d of memory is available\n"
160+
ksft_print_msg("ERROR: Less than 1/%d of memory is available\n"
155161
"as huge pages\n", compaction_index);
156162
goto close_fd;
157163
}

0 commit comments

Comments
 (0)