1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Richard Palethorpe <richiejp@f-m.fm>
4 */
5
6 /*
7 * Check that accessing a page marked with MADV_HWPOISON results in SIGBUS.
8 *
9 * Test flow: create child process,
10 * map and write to memory,
11 * mark memory with MADV_HWPOISON,
12 * access memory,
13 * if SIGBUS is delivered to child the test passes else it fails
14 *
15 * If the underlying page type of the memory we have mapped does not support
16 * poisoning then the test will fail. We try to map and write to the memory in
17 * such a way that by the time madvise is called the virtual memory address
18 * points to a supported page. However there may be some rare circumstances
19 * where the test produces the wrong result because we have somehow obtained
20 * an unsupported page. In such cases madvise will probably return success,
21 * but no SIGBUS will be produced.
22 *
23 * For more information see <linux source>/Documentation/vm/hwpoison.txt.
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/wait.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <signal.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include "tst_test.h"
36 #include "lapi/mmap.h"
37
run_child(void)38 static void run_child(void)
39 {
40 const size_t msize = getpagesize();
41 void *mem = NULL;
42
43 tst_res(TINFO,
44 "mmap(0, %zu, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)",
45 msize);
46 mem = SAFE_MMAP(NULL,
47 msize,
48 PROT_READ | PROT_WRITE,
49 MAP_ANONYMOUS | MAP_PRIVATE,
50 -1,
51 0);
52 memset(mem, 'L', msize);
53
54 tst_res(TINFO, "madvise(%p, %zu, MADV_HWPOISON)", mem, msize);
55 if (madvise(mem, msize, MADV_HWPOISON) == -1) {
56 if (errno == EINVAL) {
57 tst_res(TCONF | TERRNO,
58 "CONFIG_MEMORY_FAILURE probably not set in kconfig");
59 } else {
60 tst_res(TFAIL | TERRNO, "Could not poison memory");
61 }
62 exit(0);
63 }
64
65 *((char *)mem) = 'd';
66
67 tst_res(TFAIL, "Did not receive SIGBUS on accessing poisoned page");
68 }
69
run(void)70 static void run(void)
71 {
72 int status;
73 pid_t pid;
74
75 pid = SAFE_FORK();
76 if (pid == 0) {
77 run_child();
78 exit(0);
79 }
80
81 SAFE_WAITPID(pid, &status, 0);
82 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGBUS) {
83 tst_res(TPASS, "Received SIGBUS after accessing poisoned page");
84 return;
85 }
86
87 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
88 return;
89
90 tst_res(TFAIL, "Child %s", tst_strstatus(status));
91 }
92
93 static struct tst_test test = {
94 .test_all = run,
95 .min_kver = "2.6.31",
96 .needs_root = 1,
97 .forks_child = 1
98 };
99
100