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
has_caps(void)98 static int has_caps(void)
99 {
100 struct tst_cap_user_header hdr = {
101 .version = 0x20080522,
102 .pid = tst_syscall(__NR_gettid),
103 };
104
105 struct tst_cap_user_data caps[2];
106
107 if (tst_capget(&hdr, caps))
108 tst_brk(TBROK | TERRNO, "tst_capget()");
109
110 if (caps[0].effective & (1U << CAP_SYS_RESOURCE))
111 return 1;
112
113 return 0;
114 }
115
write_score(const char * path,int score)116 static int write_score(const char *path, int score)
117 {
118 FILE *f;
119
120 f = fopen(path, "w");
121 if (!f)
122 return 1;
123
124 if (fprintf(f, "%d", score) <= 0) {
125 fclose(f);
126 return 1;
127 }
128
129 if (fclose(f))
130 return 1;
131
132 return 0;
133 }
134
set_oom_score_adj(pid_t pid,int value)135 static void set_oom_score_adj(pid_t pid, int value)
136 {
137 int val;
138 char score_path[64];
139
140 if (access("/proc/self/oom_score_adj", F_OK) == -1) {
141 tst_res(TINFO, "oom_score_adj does not exist, skipping the adjustment");
142 return;
143 }
144
145 if (pid == 0) {
146 sprintf(score_path, "/proc/self/oom_score_adj");
147 } else {
148 sprintf(score_path, "/proc/%d/oom_score_adj", pid);
149 if (access(score_path, F_OK) == -1)
150 tst_brk(TBROK, "%s does not exist, please check if PID is valid", score_path);
151 }
152
153 if (write_score(score_path, value)) {
154 if (!has_caps())
155 return;
156
157 tst_res(TWARN, "Can't adjust score, even with capabilities!?");
158 return;
159 }
160
161 FILE_SCANF(score_path, "%d", &val);
162
163 if (val != value)
164 tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value);
165 }
166
tst_enable_oom_protection(pid_t pid)167 void tst_enable_oom_protection(pid_t pid)
168 {
169 set_oom_score_adj(pid, -1000);
170 }
171
tst_disable_oom_protection(pid_t pid)172 void tst_disable_oom_protection(pid_t pid)
173 {
174 set_oom_score_adj(pid, 0);
175 }
176