1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
4 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5 */
6 /*
7 * Description:
8 * Check the basic functionality of the mlock2(2) since kernel v2.6.9:
9 * 1) When we use mlock2() without MLOCK_ONFAULT to lock memory in the
10 * specified range that is multiples of page size or not, we can
11 * show correct size of locked memory by VmLck from /proc/PID/status
12 * and lock all pages including non-present.
13 * 2) When we use mlock2() with MLOCK_ONFAULT to lock memory in the
14 * specified range that is multiples of page size or not, we can
15 * show correct size of locked memory by VmLck from /proc/PID/status
16 * and just lock present pages.
17 */
18 #include <errno.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <sys/mman.h>
23 #include <linux/mman.h>
24
25 #include "tst_test.h"
26 #include "lapi/syscalls.h"
27 #include "lapi/mlock2.h"
28
29 #define PAGES 8
30 #define HPAGES (PAGES / 2)
31
32 static size_t pgsz;
33 static unsigned char vec[PAGES+1];
34
35 static struct tcase {
36 size_t populate_pages;
37 size_t lock_pages;
38 size_t offset;
39 size_t exp_vmlcks;
40 size_t exp_present_pages;
41 int flag;
42 } tcases[] = {
43 /* lock single page, expect it to be locked and present */
44 {0, 1, 0, 1, 1, 0},
45
46 /* lock all pages, expect all to be locked and present */
47 {0, PAGES, 0, PAGES, PAGES, 0},
48
49 /* mlock2() locks 3 pages if the specified
50 * range is little more than 2 pages.
51 */
52 {0, 2, 1, 3, 3, 0},
53
54 /* mlock2() locks 2 pages if the specified
55 * range is little less than 2 pages.
56 */
57 {0, 2, -1, 2, 2, 0},
58
59 /* mlock2() with MLOCK_ONFAULT just lock present
60 * pages populated by data.
61 */
62 {0, 1, 0, 1, 0, MLOCK_ONFAULT},
63
64 /* fault-in half of pages, lock all with MLOCK_ONFAULT */
65 {HPAGES, PAGES, 0, PAGES, HPAGES, MLOCK_ONFAULT},
66
67 /* fault-in 1 page, lock half of pages */
68 {1, HPAGES, 1, HPAGES + 1, 1, MLOCK_ONFAULT},
69
70 /* fault-in half, lock little less than 2 pages */
71 {HPAGES, HPAGES, -1, HPAGES, HPAGES, MLOCK_ONFAULT},
72 };
73
check_locked_pages(char * addr,size_t len,size_t num_pgs)74 static size_t check_locked_pages(char *addr, size_t len, size_t num_pgs)
75 {
76 size_t n;
77 size_t act_pages = 0;
78
79 SAFE_MINCORE(addr, len, vec);
80
81 for (n = 0; n < num_pgs; n++) {
82 if (vec[n] & 1)
83 act_pages++;
84 }
85
86 return act_pages;
87 }
88
verify_mlock2(unsigned int n)89 static void verify_mlock2(unsigned int n)
90 {
91 struct tcase *tc = &tcases[n];
92 size_t bsize, asize, act_vmlcks, act_pgs;
93 char *addr;
94
95 addr = SAFE_MMAP(NULL, PAGES * pgsz, PROT_WRITE,
96 MAP_SHARED | MAP_ANONYMOUS, 0, 0);
97
98 if (tc->populate_pages)
99 memset(addr, 0, tc->populate_pages * pgsz);
100
101 SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &bsize);
102
103 TEST(tst_syscall(__NR_mlock2, addr, tc->lock_pages * pgsz + tc->offset,
104 tc->flag));
105 SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &asize);
106
107 if (TST_RET != 0) {
108 if (tc->flag && TST_ERR == EINVAL)
109 tst_res(TCONF, "mlock2() didn't support MLOCK_ONFAULT");
110 else
111 tst_res(TFAIL | TTERRNO, "mlock2() failed");
112 goto end2;
113 }
114
115 act_vmlcks = (asize - bsize) * 1024 / pgsz;
116 if (tc->exp_vmlcks != act_vmlcks) {
117 tst_res(TFAIL, "VmLck showed wrong %ld pages, expected %ld",
118 act_vmlcks, tc->exp_vmlcks);
119 goto end1;
120 }
121
122 act_pgs = check_locked_pages(addr, PAGES * pgsz, PAGES);
123 if (act_pgs != tc->exp_present_pages) {
124 tst_res(TFAIL, "mlock2(%d) locked %ld pages, expected %ld",
125 tc->flag, act_pgs, tc->exp_present_pages);
126 } else {
127 tst_res(TPASS, "mlock2(%d) succeeded in locking %ld pages",
128 tc->flag, tc->exp_present_pages);
129 }
130
131 end1:
132 SAFE_MUNLOCK(addr, tc->lock_pages * pgsz + tc->offset);
133 end2:
134 SAFE_MUNMAP(addr, PAGES * pgsz);
135 }
136
setup(void)137 static void setup(void)
138 {
139 pgsz = getpagesize();
140 }
141
142 static struct tst_test test = {
143 .tcnt = ARRAY_SIZE(tcases),
144 .test = verify_mlock2,
145 .setup = setup,
146 .needs_root = 1,
147 .min_kver = "2.6.9",
148 };
149