Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/mm: hugetlb_fault_after_madv: improve test output

Let's improve the test output. For example, print the proper test result.
Install a SIGBUS handler to catch any SIGBUS instead of crashing the test
on failure.

With unsuitable hugetlb page count:
$ ./hugetlb_fault_after_madv
TAP version 13
1..1
# [INFO] detected default hugetlb page size: 2048 KiB
ok 2 # SKIP This test needs one and only one page to execute. Got 0
# Totals: pass:0 fail:0 xfail:0 xpass:0 skip:1 error:0

On a failure:
$ ./hugetlb_fault_after_madv
TAP version 13
1..1
not ok 1 SIGBUS behavior
Bail out! 1 out of 1 tests failed

On success:
$ ./hugetlb_fault_after_madv
TAP version 13
1..1
# [INFO] detected default hugetlb page size: 2048 KiB
ok 1 SIGBUS behavior
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0

Link: https://lkml.kernel.org/r/20240926152044.2205129-3-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Breno Leitao <leitao@debian.org>
Tested-by: Mario Casquero <mcasquer@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

David Hildenbrand and committed by
Andrew Morton
f33cea94 3b2faed0

+33 -1
+33 -1
tools/testing/selftests/mm/hugetlb_fault_after_madv.c
··· 5 5 #include <sys/mman.h> 6 6 #include <sys/types.h> 7 7 #include <unistd.h> 8 + #include <setjmp.h> 9 + #include <signal.h> 8 10 9 11 #include "vm_util.h" 10 12 #include "../kselftest.h" ··· 16 14 static char *huge_ptr; 17 15 static size_t huge_page_size; 18 16 17 + static sigjmp_buf sigbuf; 18 + static bool sigbus_triggered; 19 + 20 + static void signal_handler(int signal) 21 + { 22 + if (signal == SIGBUS) { 23 + sigbus_triggered = true; 24 + siglongjmp(sigbuf, 1); 25 + } 26 + } 27 + 19 28 /* Touch the memory while it is being madvised() */ 20 29 void *touch(void *unused) 21 30 { 22 31 char *ptr = (char *)huge_ptr; 32 + 33 + if (sigsetjmp(sigbuf, 1)) 34 + return NULL; 23 35 24 36 for (int i = 0; i < INLOOP_ITER; i++) 25 37 ptr[0] = '.'; ··· 60 44 * interactions 61 45 */ 62 46 int max = 10000; 47 + int err; 48 + 49 + ksft_print_header(); 50 + ksft_set_plan(1); 63 51 64 52 srand(getpid()); 53 + 54 + if (signal(SIGBUS, signal_handler) == SIG_ERR) 55 + ksft_exit_skip("Could not register signal handler."); 65 56 66 57 huge_page_size = default_huge_page_size(); 67 58 if (!huge_page_size) 68 59 ksft_exit_skip("Could not detect default hugetlb page size."); 60 + 61 + ksft_print_msg("[INFO] detected default hugetlb page size: %zu KiB\n", 62 + huge_page_size / 1024); 69 63 70 64 free_hugepages = get_free_hugepages(); 71 65 if (free_hugepages != 1) { ··· 99 73 munmap(huge_ptr, huge_page_size); 100 74 } 101 75 102 - return KSFT_PASS; 76 + ksft_test_result(!sigbus_triggered, "SIGBUS behavior\n"); 77 + 78 + err = ksft_get_fail_cnt(); 79 + if (err) 80 + ksft_exit_fail_msg("%d out of %d tests failed\n", 81 + err, ksft_test_num()); 82 + ksft_exit_pass(); 103 83 }