Skip to content

Commit 3dc40cf

Browse files
committed
selftests: KVM: skip hugetlb tests if huge pages are not available
Right now, if KVM memory stress tests are run with hugetlb sources but hugetlb is not available (either in the kernel or because /proc/sys/vm/nr_hugepages is 0) the test will fail with a memory allocation error. This makes it impossible to add tests that default to hugetlb-backed memory, because on a machine with a default configuration they will fail. Therefore, check HugePages_Total as well and, if zero, direct the user to enable hugepages in procfs. Furthermore, return KSFT_SKIP whenever hugetlb is not available. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 53293cb commit 3dc40cf

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

tools/testing/selftests/kvm/lib/test_util.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,33 @@ size_t get_trans_hugepagesz(void)
165165
size_t get_def_hugetlb_pagesz(void)
166166
{
167167
char buf[64];
168-
const char *tag = "Hugepagesize:";
168+
const char *hugepagesize = "Hugepagesize:";
169+
const char *hugepages_total = "HugePages_Total:";
169170
FILE *f;
170171

171172
f = fopen("/proc/meminfo", "r");
172173
TEST_ASSERT(f != NULL, "Error in opening /proc/meminfo");
173174

174175
while (fgets(buf, sizeof(buf), f) != NULL) {
175-
if (strstr(buf, tag) == buf) {
176+
if (strstr(buf, hugepages_total) == buf) {
177+
unsigned long long total = strtoull(buf + strlen(hugepages_total), NULL, 10);
178+
if (!total) {
179+
fprintf(stderr, "HUGETLB is not enabled in /proc/sys/vm/nr_hugepages\n");
180+
exit(KSFT_SKIP);
181+
}
182+
}
183+
if (strstr(buf, hugepagesize) == buf) {
176184
fclose(f);
177-
return strtoull(buf + strlen(tag), NULL, 10) << 10;
185+
return strtoull(buf + strlen(hugepagesize), NULL, 10) << 10;
178186
}
179187
}
180188

181-
if (feof(f))
182-
TEST_FAIL("HUGETLB is not configured in host kernel");
183-
else
184-
TEST_FAIL("Error in reading /proc/meminfo");
189+
if (feof(f)) {
190+
fprintf(stderr, "HUGETLB is not configured in host kernel");
191+
exit(KSFT_SKIP);
192+
}
185193

186-
fclose(f);
187-
return 0;
194+
TEST_FAIL("Error in reading /proc/meminfo");
188195
}
189196

190197
#define ANON_FLAGS (MAP_PRIVATE | MAP_ANONYMOUS)

0 commit comments

Comments
 (0)