xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mincore/mincore02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Copyright (c) International Business Machines  Corp., 2001
5  * Author: Rajeev Tiwari: [email protected]
6  * Copyright (c) 2004 Gernot Payer <[email protected]>
7  * Copyright (c) 2013 Cyril Hrubis <[email protected]>
8  * Copyright (C) 2024 SUSE LLC Andrea Manzini <[email protected]>
9  */
10 
11 /*\
12  * [Description]
13  *
14  * This test case provides a functional validation for mincore system call.
15  * We mmap a file of known size (multiple of page size) and lock it in
16  * memory. Then we obtain page location information via mincore and compare
17  * the result with the expected value.
18  */
19 
20 #include "tst_test.h"
21 
22 #define NUM_PAGES 4
23 
24 static void *addr;
25 static int size;
26 static unsigned char vec[NUM_PAGES];
27 static int fd;
28 
cleanup(void)29 static void cleanup(void)
30 {
31 	if (addr) {
32 		SAFE_MUNLOCK(addr, size);
33 		SAFE_MUNMAP(addr, size);
34 	}
35 	if (fd > 0)
36 		SAFE_CLOSE(fd);
37 }
38 
setup(void)39 static void setup(void)
40 {
41 	size = getpagesize() * NUM_PAGES;
42 	char buf[size];
43 
44 	memset(buf, 42, size);
45 
46 	int fd = SAFE_OPEN("mincore02", O_CREAT | O_RDWR, 0600);
47 
48 	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, size);
49 	addr = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED, fd, 0);
50 
51 	SAFE_MLOCK(addr, size);
52 }
53 
check_mincore(void)54 static void check_mincore(void)
55 {
56 	TST_EXP_PASS(mincore(addr, size, vec));
57 
58 	int locked_pages = 0;
59 
60 	for (int i = 0; i < NUM_PAGES; i++)
61 		locked_pages += (vec[i] & 1);
62 
63 	TST_EXP_EQ_SZ(locked_pages, NUM_PAGES);
64 }
65 
66 static struct tst_test test = {
67 	.setup = setup,
68 	.cleanup = cleanup,
69 	.test_all = check_mincore,
70 	.needs_tmpdir = 1,
71 };
72