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 * Enable OOM protection to prevent process($PID) being killed by OOM Killer. 30 * echo -1000 >/proc/$PID/oom_score_adj 31 * 32 * If the pid is 0 which means it will set on current(self) process. 33 * 34 * Unless the process has CAP_SYS_RESOURCE this call will be no-op because 35 * setting adj value < 0 requires it. 36 * 37 * CAP_SYS_RESOURCE: 38 * set /proc/[pid]/oom_score_adj to a value lower than the value last set 39 * by a process with CAP_SYS_RESOURCE. 40 * 41 * Note: 42 * This exported tst_enable_oom_protection function can be used at anywhere 43 * you want to protect, but please remember that if you do enable protection 44 * on a process($PID) that all the children will inherit its score and be 45 * ignored by OOM Killer as well. So that's why tst_disable_oom_protection() 46 * to be used in combination. 47 */ 48 void tst_enable_oom_protection(pid_t pid); 49 50 /* 51 * Disable the OOM protection for the process($PID). 52 * echo 0 >/proc/$PID/oom_score_adj 53 */ 54 void tst_disable_oom_protection(pid_t pid); 55 56 #endif /* TST_MEMUTILS_H__ */ 57