xref: /aosp_15_r20/external/ltp/lib/tst_memutils.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 SUSE LLC <[email protected]>
4  * Copyright (c) Linux Test Project, 2021-2023
5  */
6 
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <sys/sysinfo.h>
11 #include <stdlib.h>
12 
13 #define TST_NO_DEFAULT_MAIN
14 #include "tst_test.h"
15 #include "tst_memutils.h"
16 #include "tst_capability.h"
17 #include "lapi/syscalls.h"
18 
19 #define BLOCKSIZE (16 * 1024 * 1024)
20 
tst_pollute_memory(size_t maxsize,int fillchar)21 void tst_pollute_memory(size_t maxsize, int fillchar)
22 {
23 	size_t i, map_count = 0, safety = 0, blocksize = BLOCKSIZE;
24 	unsigned long long freeram;
25 	size_t min_free;
26 	void **map_blocks;
27 	struct sysinfo info;
28 
29 	SAFE_FILE_SCANF("/proc/sys/vm/min_free_kbytes", "%zi", &min_free);
30 	min_free *= 1024;
31 	/* Apply a margin because we cannot get below "min" watermark */
32 	min_free += min_free / 10;
33 
34 	SAFE_SYSINFO(&info);
35 	safety = MAX(4096 * SAFE_SYSCONF(_SC_PAGESIZE), 128L * 1024 * 1024);
36 	safety = MAX(safety, min_free);
37 	safety /= info.mem_unit;
38 
39 	if (info.freeswap > safety)
40 		safety = 0;
41 
42 	/*
43 	 * MemFree usually is lower than MemAvailable, although when setting
44 	 * sysctl vm.lowmem_reserve_ratio, this could reverse.
45 	 *
46 	 * Use the lower value of both for pollutable memory. Usually this
47 	 * means we will not evict any caches.
48 	 */
49 	freeram = MIN((long long)info.freeram, (tst_available_mem() * 1024));
50 
51 	/* Not enough free memory to avoid invoking OOM killer */
52 	if (freeram <= safety)
53 		return;
54 
55 	if (!maxsize)
56 		maxsize = SIZE_MAX;
57 
58 	if (freeram - safety < maxsize / info.mem_unit)
59 		maxsize = (freeram - safety) * info.mem_unit;
60 
61 	blocksize = MIN(maxsize, blocksize);
62 	map_count = maxsize / blocksize;
63 	map_blocks = SAFE_MALLOC(map_count * sizeof(void *));
64 
65 	/*
66 	 * Keep allocating until the first failure. The address space may be
67 	 * too fragmented or just smaller than maxsize.
68 	 */
69 	for (i = 0; i < map_count; i++) {
70 		map_blocks[i] = mmap(NULL, blocksize, PROT_READ | PROT_WRITE,
71 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
72 
73 		if (map_blocks[i] == MAP_FAILED) {
74 			map_count = i;
75 			break;
76 		}
77 
78 		memset(map_blocks[i], fillchar, blocksize);
79 	}
80 
81 	for (i = 0; i < map_count; i++)
82 		SAFE_MUNMAP(map_blocks[i], blocksize);
83 
84 	free(map_blocks);
85 }
86 
tst_available_mem(void)87 long long tst_available_mem(void)
88 {
89 	unsigned long long mem_available = 0;
90 
91 	if (FILE_LINES_SCANF("/proc/meminfo", "MemAvailable: %llu",
92 		&mem_available)) {
93 		mem_available = SAFE_READ_MEMINFO("MemFree:")
94 			+ SAFE_READ_MEMINFO("Cached:");
95 	}
96 
97 	return mem_available;
98 }
99 
tst_available_swap(void)100 long long tst_available_swap(void)
101 {
102 	unsigned long long swap_available = 0;
103 
104 	FILE_LINES_SCANF("/proc/meminfo", "SwapFree: %llu", &swap_available);
105 
106 	return swap_available;
107 }
108 
has_caps(void)109 static int has_caps(void)
110 {
111 	struct tst_cap_user_header hdr = {
112 		.version = 0x20080522,
113 		.pid = tst_syscall(__NR_gettid),
114 	};
115 
116 	struct tst_cap_user_data caps[2];
117 
118 	if (tst_capget(&hdr, caps))
119 		tst_brk(TBROK | TERRNO, "tst_capget()");
120 
121 	if (caps[0].effective & (1U << CAP_SYS_RESOURCE))
122 		return 1;
123 
124 	return 0;
125 }
126 
write_score(const char * path,int score)127 static int write_score(const char *path, int score)
128 {
129 	FILE *f;
130 
131 	f = fopen(path, "w");
132 	if (!f)
133 		return 1;
134 
135 	if (fprintf(f, "%d", score) <= 0) {
136 		fclose(f);
137 		return 1;
138 	}
139 
140 	if (fclose(f))
141 		return 1;
142 
143 	return 0;
144 }
145 
set_oom_score_adj(pid_t pid,int value)146 static void set_oom_score_adj(pid_t pid, int value)
147 {
148 	int val;
149 	char score_path[64];
150 
151 	if (access("/proc/self/oom_score_adj", F_OK) == -1) {
152 		tst_res(TINFO, "oom_score_adj does not exist, skipping the adjustment");
153 		return;
154 	}
155 
156 	if (pid == 0) {
157 		sprintf(score_path, "/proc/self/oom_score_adj");
158 	} else {
159 		sprintf(score_path, "/proc/%d/oom_score_adj", pid);
160 		if (access(score_path, F_OK) == -1)
161 			tst_brk(TBROK, "%s does not exist, please check if PID is valid", score_path);
162 	}
163 
164 	if (write_score(score_path, value)) {
165 		if (!has_caps())
166 			return;
167 
168 		tst_res(TWARN, "Can't adjust score, even with capabilities!?");
169 		return;
170 	}
171 
172 	FILE_SCANF(score_path, "%d", &val);
173 
174 	if (val != value)
175 		tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value);
176 }
177 
tst_enable_oom_protection(pid_t pid)178 void tst_enable_oom_protection(pid_t pid)
179 {
180 	set_oom_score_adj(pid, -1000);
181 }
182 
tst_disable_oom_protection(pid_t pid)183 void tst_disable_oom_protection(pid_t pid)
184 {
185 	set_oom_score_adj(pid, 0);
186 }
187