1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Linux Test Project, 2014
4 */
5 /*
6 * This is a regression test for madvise(2) system call. It tests kernel
7 * for NULL ptr deref Oops fixed by:
8 * commit ee53664bda169f519ce3c6a22d378f0b946c8178
9 * Author: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
10 * Date: Fri Dec 20 15:10:03 2013 +0200
11 * mm: Fix NULL pointer dereference in madvise(MADV_WILLNEED) support
12 *
13 * On buggy kernel with CONFIG_TRANSPARENT_HUGEPAGE=y CONFIG_DEBUG_LOCK_ALLOC=y
14 * this testcase should produce Oops and/or be killed. On fixed/good kernel
15 * this testcase runs to completion (retcode is 0)
16 */
17
18 #include <sys/mman.h>
19 #include <errno.h>
20 #include "tst_test.h"
21
22 #define ALLOC_SIZE (32 * 1024 * 1024)
23
verify_madvise(void)24 static void verify_madvise(void)
25 {
26 void *p;
27
28 p = SAFE_MMAP(NULL, ALLOC_SIZE, PROT_READ,
29 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
30
31 TEST(mprotect(p, ALLOC_SIZE, PROT_NONE));
32 if (TST_RET == -1)
33 tst_brk(TBROK | TTERRNO, "mprotect failed");
34 TEST(madvise(p, ALLOC_SIZE, MADV_WILLNEED));
35 SAFE_MUNMAP(p, ALLOC_SIZE);
36
37 if (TST_RET == 0) {
38 tst_res(TPASS, "issue has not been reproduced");
39 return;
40 }
41
42 if (TST_ERR == EBADF)
43 tst_brk(TCONF, "CONFIG_SWAP=n");
44 else
45 tst_brk(TBROK | TTERRNO, "madvise failed");
46 }
47
48 static struct tst_test test = {
49 .test_all = verify_madvise,
50 .tags = (const struct tst_tag[]) {
51 {"linux-git", "ee53664bda16"},
52 {}
53 }
54 };
55