1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Zilogic Systems Pvt. Ltd., 2020
4 * Email: code@zilogic.com
5 */
6
7 /*
8 * mincore03
9 * Testcase 1: Test shows that pages mapped as anonymous and
10 * not faulted, are reported as not resident in memory by mincore().
11 * Testcase 2: Test shows that pages mapped as anonymous and faulted,
12 * are reported as resident in memory by mincore().
13 */
14
15 #include <stdbool.h>
16 #include <unistd.h>
17 #include <sys/mman.h>
18 #include "tst_test.h"
19
20 #define NUM_PAGES 3
21
22 static struct tcase {
23 bool mlock;
24 int expected_pages;
25 char *desc;
26 } tcases[] = {
27 { false, 0, "untouched pages are not resident"},
28 { true, NUM_PAGES, "locked pages are resident"},
29 };
30
31 static int size, page_size;
32 static void *ptr;
33
cleanup(void)34 static void cleanup(void)
35 {
36 if (ptr)
37 SAFE_MUNMAP(ptr, size);
38 }
39
setup(void)40 static void setup(void)
41 {
42 page_size = getpagesize();
43 size = page_size * NUM_PAGES;
44 }
45
test_mincore(unsigned int test_nr)46 static void test_mincore(unsigned int test_nr)
47 {
48 const struct tcase *tc = &tcases[test_nr];
49 unsigned char vec[NUM_PAGES];
50 int locked_pages;
51 int count, mincore_ret;
52
53 ptr = SAFE_MMAP(NULL, size, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
54 if (tc->mlock)
55 SAFE_MLOCK(ptr, size);
56
57 mincore_ret = mincore(ptr, size, vec);
58 if (mincore_ret == -1)
59 tst_brk(TBROK | TERRNO, "mincore failed");
60 locked_pages = 0;
61 for (count = 0; count < NUM_PAGES; count++)
62 if (vec[count] & 1)
63 locked_pages++;
64
65 if (locked_pages == tc->expected_pages)
66 tst_res(TPASS, "mincore() reports %s", tc->desc);
67 else
68 tst_res(TFAIL, "mincore reports resident pages as %d, but expected %d",
69 locked_pages, tc->expected_pages);
70
71 if (tc->mlock)
72 SAFE_MUNLOCK(ptr, size);
73 SAFE_MUNMAP(ptr, size);
74 ptr = NULL;
75 }
76
77 static struct tst_test test = {
78 .tcnt = ARRAY_SIZE(tcases),
79 .setup = setup,
80 .cleanup = cleanup,
81 .test = test_mincore,
82 };
83
84