xref: /aosp_15_r20/external/ltp/include/tst_memutils.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /* SPDX-License-Identifier: GPL-2.0-or-later */
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2020 SUSE LLC <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker #ifndef TST_MEMUTILS_H__
7*49cdfc7eSAndroid Build Coastguard Worker #define TST_MEMUTILS_H__
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*
10*49cdfc7eSAndroid Build Coastguard Worker  * Fill up to maxsize physical memory with fillchar, then free it for reuse.
11*49cdfc7eSAndroid Build Coastguard Worker  * If maxsize is zero, fill as much memory as possible. This function is
12*49cdfc7eSAndroid Build Coastguard Worker  * intended for data disclosure vulnerability tests to reduce the probability
13*49cdfc7eSAndroid Build Coastguard Worker  * that a vulnerable kernel will leak a block of memory that was full of
14*49cdfc7eSAndroid Build Coastguard Worker  * zeroes by chance.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * The function keeps a safety margin to avoid invoking OOM killer and
17*49cdfc7eSAndroid Build Coastguard Worker  * respects the limitations of available address space. (Less than 3GB can be
18*49cdfc7eSAndroid Build Coastguard Worker  * polluted on a 32bit system regardless of available physical RAM.)
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker void tst_pollute_memory(size_t maxsize, int fillchar);
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker /*
23*49cdfc7eSAndroid Build Coastguard Worker  * Read the value of MemAvailable from /proc/meminfo, if no support on
24*49cdfc7eSAndroid Build Coastguard Worker  * older kernels, return 'MemFree + Cached' for instead.
25*49cdfc7eSAndroid Build Coastguard Worker  */
26*49cdfc7eSAndroid Build Coastguard Worker long long tst_available_mem(void);
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker /*
29*49cdfc7eSAndroid Build Coastguard Worker  * Read the value of SwapFree from /proc/meminfo.
30*49cdfc7eSAndroid Build Coastguard Worker  */
31*49cdfc7eSAndroid Build Coastguard Worker long long tst_available_swap(void);
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker /*
34*49cdfc7eSAndroid Build Coastguard Worker  * Enable OOM protection to prevent process($PID) being killed by OOM Killer.
35*49cdfc7eSAndroid Build Coastguard Worker  *   echo -1000 >/proc/$PID/oom_score_adj
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  * If the pid is 0 which means it will set on current(self) process.
38*49cdfc7eSAndroid Build Coastguard Worker  *
39*49cdfc7eSAndroid Build Coastguard Worker  * Unless the process has CAP_SYS_RESOURCE this call will be no-op because
40*49cdfc7eSAndroid Build Coastguard Worker  * setting adj value < 0 requires it.
41*49cdfc7eSAndroid Build Coastguard Worker  *
42*49cdfc7eSAndroid Build Coastguard Worker  * CAP_SYS_RESOURCE:
43*49cdfc7eSAndroid Build Coastguard Worker  *   set /proc/[pid]/oom_score_adj to a value lower than the value last set
44*49cdfc7eSAndroid Build Coastguard Worker  *   by a process with CAP_SYS_RESOURCE.
45*49cdfc7eSAndroid Build Coastguard Worker  *
46*49cdfc7eSAndroid Build Coastguard Worker  * Note:
47*49cdfc7eSAndroid Build Coastguard Worker  *  This exported tst_enable_oom_protection function can be used at anywhere
48*49cdfc7eSAndroid Build Coastguard Worker  *  you want to protect, but please remember that if you do enable protection
49*49cdfc7eSAndroid Build Coastguard Worker  *  on a process($PID) that all the children will inherit its score and be
50*49cdfc7eSAndroid Build Coastguard Worker  *  ignored by OOM Killer as well. So that's why tst_disable_oom_protection()
51*49cdfc7eSAndroid Build Coastguard Worker  *  to be used in combination.
52*49cdfc7eSAndroid Build Coastguard Worker  */
53*49cdfc7eSAndroid Build Coastguard Worker void tst_enable_oom_protection(pid_t pid);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker /*
56*49cdfc7eSAndroid Build Coastguard Worker  * Disable the OOM protection for the process($PID).
57*49cdfc7eSAndroid Build Coastguard Worker  *   echo 0 >/proc/$PID/oom_score_adj
58*49cdfc7eSAndroid Build Coastguard Worker  */
59*49cdfc7eSAndroid Build Coastguard Worker void tst_disable_oom_protection(pid_t pid);
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker #define TST_PRINT_MEMINFO() safe_print_file(__FILE__, __LINE__, "/proc/meminfo")
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker #endif /* TST_MEMUTILS_H__ */
64