xref: /linux-6.15/include/linux/oom.h (revision f2f48408)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
28ac773b4SAlexey Dobriyan #ifndef __INCLUDE_LINUX_OOM_H
38ac773b4SAlexey Dobriyan #define __INCLUDE_LINUX_OOM_H
48ac773b4SAlexey Dobriyan 
55a3135c2SDavid Rientjes 
63f07c014SIngo Molnar #include <linux/sched/signal.h>
7172acf60SDavid Rientjes #include <linux/types.h>
84365a567SKAMEZAWA Hiroyuki #include <linux/nodemask.h>
9607ca46eSDavid Howells #include <uapi/linux/oom.h>
106b31d595SMichal Hocko #include <linux/mm.h> /* VM_FAULT* */
11172acf60SDavid Rientjes 
12172acf60SDavid Rientjes struct zonelist;
13172acf60SDavid Rientjes struct notifier_block;
1474bcbf40SAndrew Morton struct mem_cgroup;
1574bcbf40SAndrew Morton struct task_struct;
16172acf60SDavid Rientjes 
17ef8444eaSyuzhoujian enum oom_constraint {
18ef8444eaSyuzhoujian 	CONSTRAINT_NONE,
19ef8444eaSyuzhoujian 	CONSTRAINT_CPUSET,
20ef8444eaSyuzhoujian 	CONSTRAINT_MEMORY_POLICY,
21ef8444eaSyuzhoujian 	CONSTRAINT_MEMCG,
22ef8444eaSyuzhoujian };
23ef8444eaSyuzhoujian 
248989e4c7SDavid Rientjes /*
258989e4c7SDavid Rientjes  * Details of the page allocation that triggered the oom killer that are used to
268989e4c7SDavid Rientjes  * determine what should be killed.
278989e4c7SDavid Rientjes  */
286e0fc46dSDavid Rientjes struct oom_control {
298989e4c7SDavid Rientjes 	/* Used to determine cpuset */
306e0fc46dSDavid Rientjes 	struct zonelist *zonelist;
318989e4c7SDavid Rientjes 
328989e4c7SDavid Rientjes 	/* Used to determine mempolicy */
336e0fc46dSDavid Rientjes 	nodemask_t *nodemask;
348989e4c7SDavid Rientjes 
352a966b77SVladimir Davydov 	/* Memory cgroup in which oom is invoked, or NULL for global oom */
362a966b77SVladimir Davydov 	struct mem_cgroup *memcg;
372a966b77SVladimir Davydov 
388989e4c7SDavid Rientjes 	/* Used to determine cpuset and node locality requirement */
398989e4c7SDavid Rientjes 	const gfp_t gfp_mask;
408989e4c7SDavid Rientjes 
418989e4c7SDavid Rientjes 	/*
428989e4c7SDavid Rientjes 	 * order == -1 means the oom kill is required by sysrq, otherwise only
438989e4c7SDavid Rientjes 	 * for display purposes.
448989e4c7SDavid Rientjes 	 */
458989e4c7SDavid Rientjes 	const int order;
466e0fc46dSDavid Rientjes 
477c5f64f8SVladimir Davydov 	/* Used by oom implementation, do not set */
487c5f64f8SVladimir Davydov 	unsigned long totalpages;
497c5f64f8SVladimir Davydov 	struct task_struct *chosen;
509066e5cfSYafang Shao 	long chosen_points;
51ef8444eaSyuzhoujian 
52ef8444eaSyuzhoujian 	/* Used to print the constraint info. */
53ef8444eaSyuzhoujian 	enum oom_constraint constraint;
549cbb78bbSDavid Rientjes };
559cbb78bbSDavid Rientjes 
56dc56401fSJohannes Weiner extern struct mutex oom_lock;
57*67197a4fSSuren Baghdasaryan extern struct mutex oom_adj_mutex;
58dc56401fSJohannes Weiner 
set_current_oom_origin(void)59e1e12d2fSDavid Rientjes static inline void set_current_oom_origin(void)
60e1e12d2fSDavid Rientjes {
61c96fc2d8STetsuo Handa 	current->signal->oom_flag_origin = true;
62e1e12d2fSDavid Rientjes }
63e1e12d2fSDavid Rientjes 
clear_current_oom_origin(void)64e1e12d2fSDavid Rientjes static inline void clear_current_oom_origin(void)
65e1e12d2fSDavid Rientjes {
66c96fc2d8STetsuo Handa 	current->signal->oom_flag_origin = false;
67e1e12d2fSDavid Rientjes }
68e1e12d2fSDavid Rientjes 
oom_task_origin(const struct task_struct * p)69e1e12d2fSDavid Rientjes static inline bool oom_task_origin(const struct task_struct *p)
70e1e12d2fSDavid Rientjes {
71c96fc2d8STetsuo Handa 	return p->signal->oom_flag_origin;
72e1e12d2fSDavid Rientjes }
7372788c38SDavid Rientjes 
tsk_is_oom_victim(struct task_struct * tsk)74862e3073SMichal Hocko static inline bool tsk_is_oom_victim(struct task_struct * tsk)
75862e3073SMichal Hocko {
76862e3073SMichal Hocko 	return tsk->signal->oom_mm;
77862e3073SMichal Hocko }
78862e3073SMichal Hocko 
796b31d595SMichal Hocko /*
806b31d595SMichal Hocko  * Checks whether a page fault on the given mm is still reliable.
816b31d595SMichal Hocko  * This is no longer true if the oom reaper started to reap the
826b31d595SMichal Hocko  * address space which is reflected by MMF_UNSTABLE flag set in
836b31d595SMichal Hocko  * the mm. At that moment any !shared mapping would lose the content
846b31d595SMichal Hocko  * and could cause a memory corruption (zero pages instead of the
856b31d595SMichal Hocko  * original content).
866b31d595SMichal Hocko  *
876b31d595SMichal Hocko  * User should call this before establishing a page table entry for
886b31d595SMichal Hocko  * a !shared mapping and under the proper page table lock.
896b31d595SMichal Hocko  *
906b31d595SMichal Hocko  * Return 0 when the PF is safe VM_FAULT_SIGBUS otherwise.
916b31d595SMichal Hocko  */
check_stable_address_space(struct mm_struct * mm)922b740303SSouptick Joarder static inline vm_fault_t check_stable_address_space(struct mm_struct *mm)
936b31d595SMichal Hocko {
946b31d595SMichal Hocko 	if (unlikely(test_bit(MMF_UNSTABLE, &mm->flags)))
956b31d595SMichal Hocko 		return VM_FAULT_SIGBUS;
966b31d595SMichal Hocko 	return 0;
976b31d595SMichal Hocko }
986b31d595SMichal Hocko 
999066e5cfSYafang Shao long oom_badness(struct task_struct *p,
100a7f638f9SDavid Rientjes 		unsigned long totalpages);
1015695be14SMichal Hocko 
1026e0fc46dSDavid Rientjes extern bool out_of_memory(struct oom_control *oc);
10316e95196SJohannes Weiner 
10438531201STetsuo Handa extern void exit_oom_victim(void);
10516e95196SJohannes Weiner 
1065a3135c2SDavid Rientjes extern int register_oom_notifier(struct notifier_block *nb);
1075a3135c2SDavid Rientjes extern int unregister_oom_notifier(struct notifier_block *nb);
1085a3135c2SDavid Rientjes 
1097d2e7a22SMichal Hocko extern bool oom_killer_disable(signed long timeout);
110c32b3cbeSMichal Hocko extern void oom_killer_enable(void);
1118e4228e1SDavid Rientjes 
112158e0a2dSKAMEZAWA Hiroyuki extern struct task_struct *find_lock_task_mm(struct task_struct *p);
113158e0a2dSKAMEZAWA Hiroyuki 
1145a3135c2SDavid Rientjes #endif /* _INCLUDE_LINUX_OOM_H */
115