projects/linux·blob assoc-array-fixes · refs

315 lines · 8.7 KiB

1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_CPUSET_H
3#define _LINUX_CPUSET_H
4/*
5 * cpuset interface
6 *
7 * Copyright (C) 2003 BULL SA
8 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
9 *
10 */
11
12#include <linux/sched.h>
13#include <linux/sched/topology.h>
14#include <linux/sched/task.h>
15#include <linux/cpumask.h>
16#include <linux/nodemask.h>
17#include <linux/mm.h>
18#include <linux/mmu_context.h>
19#include <linux/jump_label.h>
20
21extern bool lockdep_is_cpuset_held(void);
22
23#ifdef CONFIG_CPUSETS
24
25/*
26 * Static branch rewrites can happen in an arbitrary order for a given
27 * key. In code paths where we need to loop with read_mems_allowed_begin() and
28 * read_mems_allowed_retry() to get a consistent view of mems_allowed, we need
29 * to ensure that begin() always gets rewritten before retry() in the
30 * disabled -> enabled transition. If not, then if local irqs are disabled
31 * around the loop, we can deadlock since retry() would always be
32 * comparing the latest value of the mems_allowed seqcount against 0 as
33 * begin() still would see cpusets_enabled() as false. The enabled -> disabled
34 * transition should happen in reverse order for the same reasons (want to stop
35 * looking at real value of mems_allowed.sequence in retry() first).
36 */
37extern struct static_key_false cpusets_pre_enable_key;
38extern struct static_key_false cpusets_enabled_key;
39extern struct static_key_false cpusets_insane_config_key;
40
41static inline bool cpusets_enabled(void)
42{
43 return static_branch_unlikely(&cpusets_enabled_key);
44}
45
46static inline void cpuset_inc(void)
47{
48 static_branch_inc_cpuslocked(&cpusets_pre_enable_key);
49 static_branch_inc_cpuslocked(&cpusets_enabled_key);
50}
51
52static inline void cpuset_dec(void)
53{
54 static_branch_dec_cpuslocked(&cpusets_enabled_key);
55 static_branch_dec_cpuslocked(&cpusets_pre_enable_key);
56}
57
58/*
59 * This will get enabled whenever a cpuset configuration is considered
60 * unsupportable in general. E.g. movable only node which cannot satisfy
61 * any non movable allocations (see update_nodemask). Page allocator
62 * needs to make additional checks for those configurations and this
63 * check is meant to guard those checks without any overhead for sane
64 * configurations.
65 */
66static inline bool cpusets_insane_config(void)
67{
68 return static_branch_unlikely(&cpusets_insane_config_key);
69}
70
71extern int cpuset_init(void);
72extern void cpuset_init_smp(void);
73extern void cpuset_force_rebuild(void);
74extern void cpuset_update_active_cpus(void);
75extern void inc_dl_tasks_cs(struct task_struct *task);
76extern void dec_dl_tasks_cs(struct task_struct *task);
77extern void cpuset_lock(void);
78extern void cpuset_unlock(void);
79extern void lockdep_assert_cpuset_lock_held(void);
80extern void cpuset_cpus_allowed_locked(struct task_struct *p, struct cpumask *mask);
81extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mask);
82extern bool cpuset_cpus_allowed_fallback(struct task_struct *p);
83extern int cpuset_num_cpus(struct cgroup *cgroup);
84extern nodemask_t cpuset_mems_allowed(struct task_struct *p);
85#define cpuset_current_mems_allowed (current->mems_allowed)
86void cpuset_init_current_mems_allowed(void);
87int cpuset_nodemask_valid_mems_allowed(const nodemask_t *nodemask);
88
89extern bool cpuset_current_node_allowed(int node, gfp_t gfp_mask);
90
91static inline bool __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
92{
93 return cpuset_current_node_allowed(zone_to_nid(z), gfp_mask);
94}
95
96static inline bool cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
97{
98 if (cpusets_enabled())
99 return __cpuset_zone_allowed(z, gfp_mask);
100 return true;
101}
102
103extern int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
104 const struct task_struct *tsk2);
105
106#ifdef CONFIG_CPUSETS_V1
107#define cpuset_memory_pressure_bump() \
108 do { \
109 if (cpuset_memory_pressure_enabled) \
110 __cpuset_memory_pressure_bump(); \
111 } while (0)
112extern int cpuset_memory_pressure_enabled;
113extern void __cpuset_memory_pressure_bump(void);
114#else
115static inline void cpuset_memory_pressure_bump(void) { }
116#endif
117
118extern void cpuset_task_status_allowed(struct seq_file *m,
119 struct task_struct *task);
120extern int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
121 struct pid *pid, struct task_struct *tsk);
122
123extern int cpuset_mem_spread_node(void);
124
125static inline int cpuset_do_page_mem_spread(void)
126{
127 return task_spread_page(current);
128}
129
130extern bool current_cpuset_is_being_rebound(void);
131
132extern void dl_rebuild_rd_accounting(void);
133extern void rebuild_sched_domains(void);
134
135extern void cpuset_print_current_mems_allowed(void);
136extern void cpuset_reset_sched_domains(void);
137
138/*
139 * read_mems_allowed_begin is required when making decisions involving
140 * mems_allowed such as during page allocation. mems_allowed can be updated in
141 * parallel and depending on the new value an operation can fail potentially
142 * causing process failure. A retry loop with read_mems_allowed_begin and
143 * read_mems_allowed_retry prevents these artificial failures.
144 */
145static inline unsigned int read_mems_allowed_begin(void)
146{
147 if (!static_branch_unlikely(&cpusets_pre_enable_key))
148 return 0;
149
150 return read_seqcount_begin(&current->mems_allowed_seq);
151}
152
153/*
154 * If this returns true, the operation that took place after
155 * read_mems_allowed_begin may have failed artificially due to a concurrent
156 * update of mems_allowed. It is up to the caller to retry the operation if
157 * appropriate.
158 */
159static inline bool read_mems_allowed_retry(unsigned int seq)
160{
161 if (!static_branch_unlikely(&cpusets_enabled_key))
162 return false;
163
164 return read_seqcount_retry(&current->mems_allowed_seq, seq);
165}
166
167static inline void set_mems_allowed(nodemask_t nodemask)
168{
169 unsigned long flags;
170
171 task_lock(current);
172 local_irq_save(flags);
173 write_seqcount_begin(&current->mems_allowed_seq);
174 current->mems_allowed = nodemask;
175 write_seqcount_end(&current->mems_allowed_seq);
176 local_irq_restore(flags);
177 task_unlock(current);
178}
179
180extern void cpuset_nodes_allowed(struct cgroup *cgroup, nodemask_t *mask);
181#else /* !CONFIG_CPUSETS */
182
183static inline bool cpusets_enabled(void) { return false; }
184
185static inline bool cpusets_insane_config(void) { return false; }
186
187static inline int cpuset_init(void) { return 0; }
188static inline void cpuset_init_smp(void) {}
189
190static inline void cpuset_force_rebuild(void) { }
191
192static inline void cpuset_update_active_cpus(void)
193{
194 partition_sched_domains(1, NULL, NULL);
195}
196
197static inline void inc_dl_tasks_cs(struct task_struct *task) { }
198static inline void dec_dl_tasks_cs(struct task_struct *task) { }
199static inline void cpuset_lock(void) { }
200static inline void cpuset_unlock(void) { }
201static inline void lockdep_assert_cpuset_lock_held(void) { }
202
203static inline void cpuset_cpus_allowed_locked(struct task_struct *p,
204 struct cpumask *mask)
205{
206 cpumask_copy(mask, task_cpu_possible_mask(p));
207}
208
209static inline void cpuset_cpus_allowed(struct task_struct *p,
210 struct cpumask *mask)
211{
212 cpuset_cpus_allowed_locked(p, mask);
213}
214
215static inline bool cpuset_cpus_allowed_fallback(struct task_struct *p)
216{
217 return false;
218}
219
220static inline int cpuset_num_cpus(struct cgroup *cgroup)
221{
222 return num_online_cpus();
223}
224
225static inline nodemask_t cpuset_mems_allowed(struct task_struct *p)
226{
227 return node_possible_map;
228}
229
230#define cpuset_current_mems_allowed (node_states[N_MEMORY])
231static inline void cpuset_init_current_mems_allowed(void) {}
232
233static inline int cpuset_nodemask_valid_mems_allowed(const nodemask_t *nodemask)
234{
235 return 1;
236}
237
238static inline bool __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
239{
240 return true;
241}
242
243static inline bool cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
244{
245 return true;
246}
247
248static inline int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
249 const struct task_struct *tsk2)
250{
251 return 1;
252}
253
254static inline void cpuset_memory_pressure_bump(void) {}
255
256static inline void cpuset_task_status_allowed(struct seq_file *m,
257 struct task_struct *task)
258{
259}
260
261static inline int cpuset_mem_spread_node(void)
262{
263 return 0;
264}
265
266static inline int cpuset_do_page_mem_spread(void)
267{
268 return 0;
269}
270
271static inline bool current_cpuset_is_being_rebound(void)
272{
273 return false;
274}
275
276static inline void dl_rebuild_rd_accounting(void)
277{
278}
279
280static inline void rebuild_sched_domains(void)
281{
282 guard(cpus_read_lock)();
283 partition_sched_domains(1, NULL, NULL);
284}
285
286static inline void cpuset_reset_sched_domains(void)
287{
288 partition_sched_domains(1, NULL, NULL);
289}
290
291static inline void cpuset_print_current_mems_allowed(void)
292{
293}
294
295static inline void set_mems_allowed(nodemask_t nodemask)
296{
297}
298
299static inline unsigned int read_mems_allowed_begin(void)
300{
301 return 0;
302}
303
304static inline bool read_mems_allowed_retry(unsigned int seq)
305{
306 return false;
307}
308
309static inline void cpuset_nodes_allowed(struct cgroup *cgroup, nodemask_t *mask)
310{
311 nodes_copy(*mask, node_states[N_MEMORY]);
312}
313#endif /* !CONFIG_CPUSETS */
314
315#endif /* _LINUX_CPUSET_H */