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