1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2012-2023 Linux Test Project
4 * Copyright (c) 2012-2017 Red Hat, Inc.
5 *
6 * There are two tunables overcommit_memory and overcommit_ratio under
7 * /proc/sys/vm/, which can control memory overcommitment.
8 *
9 * The overcommit_memory contains a flag that enables memory
10 * overcommitment, it has three values:
11 * - When this flag is 0, the kernel attempts to estimate the amount
12 * of free memory left when userspace requests more memory.
13 * - When this flag is 1, the kernel pretends there is always enough
14 * memory until it actually runs out.
15 * - When this flag is 2, the kernel uses a "never overcommit" policy
16 * that attempts to prevent any overcommit of memory.
17 *
18 * The overcommit_ratio tunable defines the amount by which the kernel
19 * overextends its memory resources in the event that overcommit_memory
20 * is set to the value of 2. The value in this file represents a
21 * percentage added to the amount of actual RAM in a system when
22 * considering whether to grant a particular memory request.
23 * The general formula for this tunable is:
24 * CommitLimit = SwapTotal + MemTotal * overcommit_ratio
25 * CommitLimit, SwapTotal and MemTotal can read from /proc/meminfo.
26 *
27 * The program is designed to test the two tunables:
28 *
29 * When overcommit_memory = 0, allocatable memory can't overextend
30 * the amount of total memory:
31 * a. less than free_total: free_total / 2, alloc should pass.
32 * b. greater than sum_total: sum_total * 2, alloc should fail.
33 *
34 * When overcommit_memory = 1, it can alloc enough much memory, I
35 * choose the three cases:
36 * a. less than sum_total: sum_total / 2, alloc should pass
37 * b. equal to sum_total: sum_total, alloc should pass
38 * c. greater than sum_total: sum_total * 2, alloc should pass
39 * *note: sum_total = SwapTotal + MemTotal
40 *
41 * When overcommit_memory = 2, the total virtual address space on
42 * the system is limited to CommitLimit(Swap+RAM*overcommit_ratio)
43 * commit_left(allocatable memory) = CommitLimit - Committed_AS
44 * a. less than commit_left: commit_left / 2, alloc should pass
45 * b. overcommit limit: CommitLimit + TotalBatchSize, should fail
46 * c. greater than commit_left: commit_left * 2, alloc should fail
47 * *note: CommitLimit is the current overcommit limit.
48 * Committed_AS is the amount of memory that system has used.
49 * it couldn't choose 'equal to commit_left' as a case, because
50 * commit_left rely on Committed_AS, but the Committed_AS is not stable.
51 * *note2: TotalBatchSize is the total number of bytes, that can be
52 * accounted for in the per cpu counters for the vm_committed_as
53 * counter. Since the check used by malloc only looks at the
54 * global counter of vm_committed_as, it can overallocate a bit.
55 *
56 * References:
57 * - Documentation/sysctl/vm.txt
58 * - Documentation/vm/overcommit-accounting
59 */
60
61 #include <errno.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <limits.h>
65 #include "mem.h"
66
67 #define DEFAULT_OVER_RATIO 50L
68 #define EXPECT_PASS 0
69 #define EXPECT_FAIL 1
70
71 static char *R_opt;
72 static long old_overcommit_ratio = -1;
73 static long overcommit_ratio;
74 static long sum_total;
75 static long free_total;
76 static long commit_limit;
77 static long commit_left;
78 static long total_batch_size;
79
80 static int heavy_malloc(long size);
81 static void alloc_and_check(long size, int expect_result);
82 static void update_mem(void);
83 static void update_mem_commit(void);
84 static void calculate_total_batch_size(void);
85
setup(void)86 static void setup(void)
87 {
88 long mem_total, swap_total;
89 struct rlimit lim;
90
91 if (R_opt)
92 overcommit_ratio = SAFE_STRTOL(R_opt, 0, LONG_MAX);
93 else
94 overcommit_ratio = DEFAULT_OVER_RATIO;
95
96 old_overcommit_ratio = get_sys_tune("overcommit_ratio");
97
98 mem_total = SAFE_READ_MEMINFO("MemTotal:");
99 tst_res(TINFO, "MemTotal is %ld kB", mem_total);
100 swap_total = SAFE_READ_MEMINFO("SwapTotal:");
101 tst_res(TINFO, "SwapTotal is %ld kB", swap_total);
102 sum_total = mem_total + swap_total;
103
104 commit_limit = SAFE_READ_MEMINFO("CommitLimit:");
105 tst_res(TINFO, "CommitLimit is %ld kB", commit_limit);
106
107 SAFE_GETRLIMIT(RLIMIT_AS, &lim);
108
109 if (lim.rlim_cur != RLIM_INFINITY) {
110 lim.rlim_cur = RLIM_INFINITY;
111 lim.rlim_max = RLIM_INFINITY;
112
113 tst_res(TINFO, "Increasing RLIM_AS to INFINITY");
114
115 SAFE_SETRLIMIT(RLIMIT_AS, &lim);
116 }
117
118 set_sys_tune("overcommit_ratio", overcommit_ratio, 1);
119
120 calculate_total_batch_size();
121 tst_res(TINFO, "TotalBatchSize is %ld kB", total_batch_size);
122 }
123
overcommit_memory_test(void)124 static void overcommit_memory_test(void)
125 {
126 /* start to test overcommit_memory=2 */
127 set_sys_tune("overcommit_memory", 2, 1);
128
129 update_mem_commit();
130 alloc_and_check(commit_left * 2, EXPECT_FAIL);
131 alloc_and_check(commit_limit + total_batch_size, EXPECT_FAIL);
132 update_mem_commit();
133 alloc_and_check(commit_left / 2, EXPECT_PASS);
134
135 /* start to test overcommit_memory=0 */
136 set_sys_tune("overcommit_memory", 0, 1);
137
138 update_mem();
139 alloc_and_check(free_total / 2, EXPECT_PASS);
140 alloc_and_check(sum_total * 2, EXPECT_FAIL);
141
142 /* start to test overcommit_memory=1 */
143 set_sys_tune("overcommit_memory", 1, 1);
144
145 alloc_and_check(sum_total / 2, EXPECT_PASS);
146 alloc_and_check(sum_total, EXPECT_PASS);
147 alloc_and_check(sum_total * 2, EXPECT_PASS);
148
149 }
150
heavy_malloc(long size)151 static int heavy_malloc(long size)
152 {
153 char *p;
154
155 p = malloc(size * KB);
156 if (p != NULL) {
157 tst_res(TINFO, "malloc %ld kB successfully", size);
158 free(p);
159 return 0;
160 } else {
161 tst_res(TINFO, "malloc %ld kB failed", size);
162 return 1;
163 }
164 }
165
alloc_and_check(long size,int expect_result)166 static void alloc_and_check(long size, int expect_result)
167 {
168 int result;
169
170 /* try to alloc size kB memory */
171 result = heavy_malloc(size);
172
173 switch (expect_result) {
174 case EXPECT_PASS:
175 if (result == 0)
176 tst_res(TPASS, "alloc passed as expected");
177 else
178 tst_res(TFAIL, "alloc failed, expected to pass");
179 break;
180 case EXPECT_FAIL:
181 if (result != 0)
182 tst_res(TPASS, "alloc failed as expected");
183 else
184 tst_res(TFAIL, "alloc passed, expected to fail");
185 break;
186 default:
187 tst_brk(TBROK, "Invalid number parameter: %d",
188 expect_result);
189 }
190 }
191
update_mem(void)192 static void update_mem(void)
193 {
194 long mem_free, swap_free;
195
196 mem_free = SAFE_READ_MEMINFO("MemFree:");
197 swap_free = SAFE_READ_MEMINFO("SwapFree:");
198 free_total = mem_free + swap_free;
199 }
200
update_mem_commit(void)201 static void update_mem_commit(void)
202 {
203 long committed;
204
205 commit_limit = SAFE_READ_MEMINFO("CommitLimit:");
206 committed = SAFE_READ_MEMINFO("Committed_AS:");
207 commit_left = commit_limit - committed;
208
209 if (commit_left < 0) {
210 tst_res(TINFO, "CommitLimit is %ld, Committed_AS is %ld",
211 commit_limit, committed);
212
213 if (overcommit_ratio > old_overcommit_ratio) {
214 tst_brk(TBROK, "Unexpected error: "
215 "CommitLimit < Committed_AS");
216 }
217
218 tst_brk(TCONF, "Specified overcommit_ratio %ld <= default %ld, "
219 "so it's possible for CommitLimit < Committed_AS and skip test",
220 overcommit_ratio, old_overcommit_ratio);
221 }
222 }
223
calculate_total_batch_size(void)224 static void calculate_total_batch_size(void)
225 {
226 struct sysinfo info;
227 long ncpus = tst_ncpus_conf();
228 long pagesize = getpagesize();
229 SAFE_SYSINFO(&info);
230
231 /* see linux source mm/mm_init.c mm_compute_batch() (This is in pages) */
232 long batch_size = MAX(ncpus * 2L,
233 MAX(32L,
234 MIN((long)INT32_MAX,
235 (long)(info.totalram / pagesize) / ncpus / 256
236 )
237 )
238 );
239
240 /* there are ncpu separate counters, that can all grow up to
241 * batch_size. So the maximum error for __vm_enough_memory is
242 * batch_size * ncpus. */
243 total_batch_size = (batch_size * ncpus * pagesize) / KB;
244 }
245
246 static struct tst_test test = {
247 .needs_root = 1,
248 .options = (struct tst_option[]) {
249 {"R:", &R_opt, "Percentage of overcommitting memory"},
250 {}
251 },
252 .setup = setup,
253 .test_all = overcommit_memory_test,
254 .skip_in_compat = 1,
255 .save_restore = (const struct tst_path_val[]) {
256 {"/proc/sys/vm/overcommit_memory", NULL, TST_SR_TBROK},
257 {"/proc/sys/vm/overcommit_ratio", NULL, TST_SR_TBROK},
258 {}
259 },
260 };
261