xref: /linux-6.15/include/linux/oom.h (revision 98748bd7)
1 #ifndef __INCLUDE_LINUX_OOM_H
2 #define __INCLUDE_LINUX_OOM_H
3 
4 
5 #include <linux/sched.h>
6 #include <linux/types.h>
7 #include <linux/nodemask.h>
8 #include <uapi/linux/oom.h>
9 
10 struct zonelist;
11 struct notifier_block;
12 struct mem_cgroup;
13 struct task_struct;
14 
15 /*
16  * Details of the page allocation that triggered the oom killer that are used to
17  * determine what should be killed.
18  */
19 struct oom_control {
20 	/* Used to determine cpuset */
21 	struct zonelist *zonelist;
22 
23 	/* Used to determine mempolicy */
24 	nodemask_t *nodemask;
25 
26 	/* Used to determine cpuset and node locality requirement */
27 	const gfp_t gfp_mask;
28 
29 	/*
30 	 * order == -1 means the oom kill is required by sysrq, otherwise only
31 	 * for display purposes.
32 	 */
33 	const int order;
34 };
35 
36 /*
37  * Types of limitations to the nodes from which allocations may occur
38  */
39 enum oom_constraint {
40 	CONSTRAINT_NONE,
41 	CONSTRAINT_CPUSET,
42 	CONSTRAINT_MEMORY_POLICY,
43 	CONSTRAINT_MEMCG,
44 };
45 
46 enum oom_scan_t {
47 	OOM_SCAN_OK,		/* scan thread and find its badness */
48 	OOM_SCAN_CONTINUE,	/* do not consider thread for oom kill */
49 	OOM_SCAN_ABORT,		/* abort the iteration and return */
50 	OOM_SCAN_SELECT,	/* always select this thread first */
51 };
52 
53 /* Thread is the potential origin of an oom condition; kill first on oom */
54 #define OOM_FLAG_ORIGIN		((__force oom_flags_t)0x1)
55 
56 extern struct mutex oom_lock;
57 
58 static inline void set_current_oom_origin(void)
59 {
60 	current->signal->oom_flags |= OOM_FLAG_ORIGIN;
61 }
62 
63 static inline void clear_current_oom_origin(void)
64 {
65 	current->signal->oom_flags &= ~OOM_FLAG_ORIGIN;
66 }
67 
68 static inline bool oom_task_origin(const struct task_struct *p)
69 {
70 	return !!(p->signal->oom_flags & OOM_FLAG_ORIGIN);
71 }
72 
73 extern void mark_oom_victim(struct task_struct *tsk);
74 
75 #ifdef CONFIG_MMU
76 extern void try_oom_reaper(struct task_struct *tsk);
77 #else
78 static inline void try_oom_reaper(struct task_struct *tsk)
79 {
80 }
81 #endif
82 
83 extern unsigned long oom_badness(struct task_struct *p,
84 		struct mem_cgroup *memcg, const nodemask_t *nodemask,
85 		unsigned long totalpages);
86 
87 extern void oom_kill_process(struct oom_control *oc, struct task_struct *p,
88 			     unsigned int points, unsigned long totalpages,
89 			     struct mem_cgroup *memcg, const char *message);
90 
91 extern void check_panic_on_oom(struct oom_control *oc,
92 			       enum oom_constraint constraint,
93 			       struct mem_cgroup *memcg);
94 
95 extern enum oom_scan_t oom_scan_process_thread(struct oom_control *oc,
96 		struct task_struct *task, unsigned long totalpages);
97 
98 extern bool out_of_memory(struct oom_control *oc);
99 
100 extern void exit_oom_victim(struct task_struct *tsk);
101 
102 extern int register_oom_notifier(struct notifier_block *nb);
103 extern int unregister_oom_notifier(struct notifier_block *nb);
104 
105 extern bool oom_killer_disabled;
106 extern bool oom_killer_disable(void);
107 extern void oom_killer_enable(void);
108 
109 extern struct task_struct *find_lock_task_mm(struct task_struct *p);
110 
111 static inline bool task_will_free_mem(struct task_struct *task)
112 {
113 	struct signal_struct *sig = task->signal;
114 
115 	/*
116 	 * A coredumping process may sleep for an extended period in exit_mm(),
117 	 * so the oom killer cannot assume that the process will promptly exit
118 	 * and release memory.
119 	 */
120 	if (sig->flags & SIGNAL_GROUP_COREDUMP)
121 		return false;
122 
123 	if (!(task->flags & PF_EXITING))
124 		return false;
125 
126 	/* Make sure that the whole thread group is going down */
127 	if (!thread_group_empty(task) && !(sig->flags & SIGNAL_GROUP_EXIT))
128 		return false;
129 
130 	return true;
131 }
132 
133 /* sysctls */
134 extern int sysctl_oom_dump_tasks;
135 extern int sysctl_oom_kill_allocating_task;
136 extern int sysctl_panic_on_oom;
137 #endif /* _INCLUDE_LINUX_OOM_H */
138