Skip to content

Commit b24c0b7

Browse files
committed
Merge: selftests/mm: hugetlb_fault_after_madv: use default hugetlb page size
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/6588 JIRA: https://issues.redhat.com/browse/RHEL-60023 Tested: Ran the test on s390x and x86-64, with the expected output. Let's make the test also work as expected on architectures that have a different hugepage size than 2 MiB -- such as s390x. While at it, backport the test output cleanup part of the same upstream series. Signed-off-by: David Hildenbrand <david@redhat.com> Approved-by: Rafael Aquini <raquini@redhat.com> Approved-by: mcasquer <mcasquer@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Augusto Caringi <acaringi@redhat.com>
2 parents 60cc288 + b2fe178 commit b24c0b7

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

tools/testing/selftests/mm/hugetlb_fault_after_madv.c

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,36 @@
55
#include <sys/mman.h>
66
#include <sys/types.h>
77
#include <unistd.h>
8+
#include <setjmp.h>
9+
#include <signal.h>
810

911
#include "vm_util.h"
1012
#include "../kselftest.h"
1113

12-
#define MMAP_SIZE (1 << 21)
1314
#define INLOOP_ITER 100
1415

15-
char *huge_ptr;
16+
static char *huge_ptr;
17+
static size_t huge_page_size;
18+
19+
static sigjmp_buf sigbuf;
20+
static bool sigbus_triggered;
21+
22+
static void signal_handler(int signal)
23+
{
24+
if (signal == SIGBUS) {
25+
sigbus_triggered = true;
26+
siglongjmp(sigbuf, 1);
27+
}
28+
}
1629

1730
/* Touch the memory while it is being madvised() */
1831
void *touch(void *unused)
1932
{
2033
char *ptr = (char *)huge_ptr;
2134

35+
if (sigsetjmp(sigbuf, 1))
36+
return NULL;
37+
2238
for (int i = 0; i < INLOOP_ITER; i++)
2339
ptr[0] = '.';
2440

@@ -30,7 +46,7 @@ void *madv(void *unused)
3046
usleep(rand() % 10);
3147

3248
for (int i = 0; i < INLOOP_ITER; i++)
33-
madvise(huge_ptr, MMAP_SIZE, MADV_DONTNEED);
49+
madvise(huge_ptr, huge_page_size, MADV_DONTNEED);
3450

3551
return NULL;
3652
}
@@ -44,17 +60,31 @@ int main(void)
4460
* interactions
4561
*/
4662
int max = 10000;
63+
int err;
64+
65+
ksft_print_header();
66+
ksft_set_plan(1);
4767

4868
srand(getpid());
4969

70+
if (signal(SIGBUS, signal_handler) == SIG_ERR)
71+
ksft_exit_skip("Could not register signal handler.");
72+
73+
huge_page_size = default_huge_page_size();
74+
if (!huge_page_size)
75+
ksft_exit_skip("Could not detect default hugetlb page size.");
76+
77+
ksft_print_msg("[INFO] detected default hugetlb page size: %zu KiB\n",
78+
huge_page_size / 1024);
79+
5080
free_hugepages = get_free_hugepages();
5181
if (free_hugepages != 1) {
5282
ksft_exit_skip("This test needs one and only one page to execute. Got %lu\n",
5383
free_hugepages);
5484
}
5585

5686
while (max--) {
57-
huge_ptr = mmap(NULL, MMAP_SIZE, PROT_READ | PROT_WRITE,
87+
huge_ptr = mmap(NULL, huge_page_size, PROT_READ | PROT_WRITE,
5888
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
5989
-1, 0);
6090

@@ -66,8 +96,14 @@ int main(void)
6696

6797
pthread_join(thread1, NULL);
6898
pthread_join(thread2, NULL);
69-
munmap(huge_ptr, MMAP_SIZE);
99+
munmap(huge_ptr, huge_page_size);
70100
}
71101

72-
return KSFT_PASS;
102+
ksft_test_result(!sigbus_triggered, "SIGBUS behavior\n");
103+
104+
err = ksft_get_fail_cnt();
105+
if (err)
106+
ksft_exit_fail_msg("%d out of %d tests failed\n",
107+
err, ksft_test_num());
108+
ksft_exit_pass();
73109
}

0 commit comments

Comments
 (0)