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