xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mlock/mlock05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright Red Hat
4  * Author: Filippo Storniolo <[email protected]>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Verify mlock() causes pre-faulting of PTEs and prevent memory to be swapped out.
11  *
12  * Find the new mapping in /proc/$pid/smaps and check Rss and Locked fields after
13  * mlock syscall:
14  * Rss and Locked size should be equal to the size of the memory allocation
15  */
16 
17 #include "tst_test.h"
18 #include "tst_safe_stdio.h"
19 
20 #define MMAPLEN			(1UL<<20)
21 #define LINELEN			256
22 
get_proc_smaps_info(unsigned long desired_mapping_address,unsigned long * Rss,unsigned long * Locked)23 static void get_proc_smaps_info(unsigned long desired_mapping_address, unsigned long *Rss, unsigned long *Locked)
24 {
25 	bool mapping_found = false;
26 	bool Locked_found = false;
27 	bool Rss_found = false;
28 	char buffer[LINELEN];
29 	FILE *fp;
30 	int ret;
31 
32 	fp = SAFE_FOPEN("/proc/self/smaps", "r");
33 
34 	while (fgets(buffer, LINELEN, fp) != NULL) {
35 		unsigned long mapping_address;
36 
37 		ret = sscanf(buffer, "%lx[^-]", &mapping_address);
38 		if ((ret == 1) && (mapping_address == desired_mapping_address)) {
39 			mapping_found = true;
40 			break;
41 		}
42 	}
43 
44 	if (!mapping_found) {
45 		SAFE_FCLOSE(fp);
46 		tst_brk(TBROK, "Mapping %lx not found in /proc/self/smaps", desired_mapping_address);
47 		return;
48 	}
49 
50 	while (fgets(buffer, LINELEN, fp) != NULL) {
51 		unsigned long possible_starting_mapping;
52 		unsigned long possible_ending_mapping;
53 
54 		ret = sscanf(buffer, "%lx-%lx", &possible_starting_mapping, &possible_ending_mapping);
55 		if (ret == 2)
56 			break;
57 
58 		if (strncmp(buffer, "Rss", strlen("Rss")) == 0) {
59 			ret = sscanf(buffer, "%*[^:]:%lu kB", Rss);
60 			if (ret != 1) {
61 				SAFE_FCLOSE(fp);
62 				tst_brk(TBROK, "failure occurred while reading field Rss");
63 				return;
64 			}
65 
66 			Rss_found = true;
67 		}
68 
69 		if (strncmp(buffer, "Locked", strlen("Locked")) == 0) {
70 			ret = sscanf(buffer, "%*[^:]:%lu kB", Locked);
71 			if (ret != 1) {
72 				SAFE_FCLOSE(fp);
73 				tst_brk(TBROK, "failure occurred while reading field Locked");
74 				return;
75 			}
76 
77 			Locked_found =  true;
78 		}
79 
80 		if (Rss_found && Locked_found) {
81 			SAFE_FCLOSE(fp);
82 			return;
83 		}
84 	}
85 
86 	SAFE_FCLOSE(fp);
87 	tst_brk(TBROK, "cannot find both Rss and Locked in mapping %lx", desired_mapping_address);
88 }
89 
verify_mlock(void)90 static void verify_mlock(void)
91 {
92 	unsigned long Locked;
93 	unsigned long Rss;
94 	char *buf;
95 
96 	buf = SAFE_MMAP(NULL, MMAPLEN, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
97 	SAFE_MLOCK(buf, MMAPLEN);
98 
99 	get_proc_smaps_info((unsigned long)buf, &Rss, &Locked);
100 
101 	// Convertion from KiB to B
102 	Rss *= 1024;
103 	Locked *= 1024;
104 
105 	TST_EXP_EQ_LU(Rss, MMAPLEN);
106 	TST_EXP_EQ_LU(Locked, MMAPLEN);
107 
108 	SAFE_MUNLOCK(buf, MMAPLEN);
109 	SAFE_MUNMAP(buf, MMAPLEN);
110 }
111 
112 static struct tst_test test = {
113 	.test_all = verify_mlock,
114 };
115