blob: 1b8e41597ef536724fed06fbbea74e9ac6a7c501 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_CPUSET_H
3#define _LINUX_CPUSET_H
4/*
5 * cpuset interface
6 *
7 * Copyright (C) 2003 BULL SA
Paul Jackson825a46a2006-03-24 03:16:03 -08008 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 */
11
12#include <linux/sched.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010013#include <linux/sched/topology.h>
Ingo Molnarf719ff92017-02-06 10:57:33 +010014#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/cpumask.h>
16#include <linux/nodemask.h>
David Rientjesa1bc5a42009-04-02 16:57:54 -070017#include <linux/mm.h>
Mel Gorman664eedd2014-06-04 16:10:08 -070018#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#ifdef CONFIG_CPUSETS
21
Dima Zavin89affbf2017-08-02 13:32:18 -070022/*
23 * Static branch rewrites can happen in an arbitrary order for a given
24 * key. In code paths where we need to loop with read_mems_allowed_begin() and
25 * read_mems_allowed_retry() to get a consistent view of mems_allowed, we need
26 * to ensure that begin() always gets rewritten before retry() in the
27 * disabled -> enabled transition. If not, then if local irqs are disabled
28 * around the loop, we can deadlock since retry() would always be
29 * comparing the latest value of the mems_allowed seqcount against 0 as
30 * begin() still would see cpusets_enabled() as false. The enabled -> disabled
31 * transition should happen in reverse order for the same reasons (want to stop
32 * looking at real value of mems_allowed.sequence in retry() first).
33 */
34extern struct static_key_false cpusets_pre_enable_key;
Vlastimil Babka002f2902016-05-19 17:14:30 -070035extern struct static_key_false cpusets_enabled_key;
Mel Gorman664eedd2014-06-04 16:10:08 -070036static inline bool cpusets_enabled(void)
37{
Vlastimil Babka002f2902016-05-19 17:14:30 -070038 return static_branch_unlikely(&cpusets_enabled_key);
Mel Gorman664eedd2014-06-04 16:10:08 -070039}
40
Mel Gorman664eedd2014-06-04 16:10:08 -070041static inline void cpuset_inc(void)
42{
Dima Zavin89affbf2017-08-02 13:32:18 -070043 static_branch_inc(&cpusets_pre_enable_key);
Vlastimil Babka002f2902016-05-19 17:14:30 -070044 static_branch_inc(&cpusets_enabled_key);
Mel Gorman664eedd2014-06-04 16:10:08 -070045}
46
47static inline void cpuset_dec(void)
48{
Vlastimil Babka002f2902016-05-19 17:14:30 -070049 static_branch_dec(&cpusets_enabled_key);
Dima Zavin89affbf2017-08-02 13:32:18 -070050 static_branch_dec(&cpusets_pre_enable_key);
Mel Gorman664eedd2014-06-04 16:10:08 -070051}
Paul Jackson202f72d2006-01-08 01:01:57 -080052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053extern int cpuset_init(void);
54extern void cpuset_init_smp(void);
Peter Zijlstra50e76632017-09-07 11:13:38 +020055extern void cpuset_force_rebuild(void);
Rakib Mullick30e03ac2017-04-09 07:36:14 +060056extern void cpuset_update_active_cpus(void);
Peter Zijlstra50e76632017-09-07 11:13:38 +020057extern void cpuset_wait_for_hotplug(void);
Li Zefan6af866a2009-01-07 18:08:45 -080058extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mask);
Peter Zijlstra2baab4e2012-03-20 15:57:01 +010059extern void cpuset_cpus_allowed_fallback(struct task_struct *p);
Paul Jackson909d75a2006-01-08 01:01:55 -080060extern nodemask_t cpuset_mems_allowed(struct task_struct *p);
Paul Jackson9276b1bc2006-12-06 20:31:48 -080061#define cpuset_current_mems_allowed (current->mems_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062void cpuset_init_current_mems_allowed(void);
Mel Gorman19770b32008-04-28 02:12:18 -070063int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask);
Paul Jackson202f72d2006-01-08 01:01:57 -080064
Vlastimil Babka002f2902016-05-19 17:14:30 -070065extern bool __cpuset_node_allowed(int node, gfp_t gfp_mask);
Paul Jackson02a0e532006-12-13 00:34:25 -080066
Vlastimil Babka002f2902016-05-19 17:14:30 -070067static inline bool cpuset_node_allowed(int node, gfp_t gfp_mask)
Paul Jackson202f72d2006-01-08 01:01:57 -080068{
Vlastimil Babka002f2902016-05-19 17:14:30 -070069 if (cpusets_enabled())
70 return __cpuset_node_allowed(node, gfp_mask);
71 return true;
Paul Jackson02a0e532006-12-13 00:34:25 -080072}
73
Vlastimil Babka002f2902016-05-19 17:14:30 -070074static inline bool __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
Paul Jackson02a0e532006-12-13 00:34:25 -080075{
Vlastimil Babka002f2902016-05-19 17:14:30 -070076 return __cpuset_node_allowed(zone_to_nid(z), gfp_mask);
77}
78
79static inline bool cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
80{
81 if (cpusets_enabled())
82 return __cpuset_zone_allowed(z, gfp_mask);
83 return true;
Paul Jackson202f72d2006-01-08 01:01:57 -080084}
85
David Rientjesbbe373f2007-10-16 23:25:58 -070086extern int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
87 const struct task_struct *tsk2);
Paul Jackson3e0d98b2006-01-08 01:01:49 -080088
89#define cpuset_memory_pressure_bump() \
90 do { \
91 if (cpuset_memory_pressure_enabled) \
92 __cpuset_memory_pressure_bump(); \
93 } while (0)
94extern int cpuset_memory_pressure_enabled;
95extern void __cpuset_memory_pressure_bump(void);
96
Eric W. Biedermandf5f8312008-02-08 04:18:33 -080097extern void cpuset_task_status_allowed(struct seq_file *m,
98 struct task_struct *task);
Zefan Li52de4772014-09-18 16:03:36 +080099extern int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
100 struct pid *pid, struct task_struct *tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Paul Jackson825a46a2006-03-24 03:16:03 -0800102extern int cpuset_mem_spread_node(void);
Jack Steiner6adef3e2010-05-26 14:42:49 -0700103extern int cpuset_slab_spread_node(void);
Paul Jackson825a46a2006-03-24 03:16:03 -0800104
105static inline int cpuset_do_page_mem_spread(void)
106{
Zefan Li2ad654b2014-09-25 09:41:02 +0800107 return task_spread_page(current);
Paul Jackson825a46a2006-03-24 03:16:03 -0800108}
109
110static inline int cpuset_do_slab_mem_spread(void)
111{
Zefan Li2ad654b2014-09-25 09:41:02 +0800112 return task_spread_slab(current);
Paul Jackson825a46a2006-03-24 03:16:03 -0800113}
114
Paul Menage8793d852007-10-18 23:39:39 -0700115extern int current_cpuset_is_being_rebound(void);
116
Max Krasnyanskye761b772008-07-15 04:43:49 -0700117extern void rebuild_sched_domains(void);
118
David Rientjesda39da32015-11-05 18:48:05 -0800119extern void cpuset_print_current_mems_allowed(void);
David Rientjes75aa1992009-01-06 14:39:01 -0800120
Miao Xiec0ff7452010-05-24 14:32:08 -0700121/*
Mel Gormand26914d2014-04-03 14:47:24 -0700122 * read_mems_allowed_begin is required when making decisions involving
123 * mems_allowed such as during page allocation. mems_allowed can be updated in
124 * parallel and depending on the new value an operation can fail potentially
125 * causing process failure. A retry loop with read_mems_allowed_begin and
126 * read_mems_allowed_retry prevents these artificial failures.
Miao Xiec0ff7452010-05-24 14:32:08 -0700127 */
Mel Gormand26914d2014-04-03 14:47:24 -0700128static inline unsigned int read_mems_allowed_begin(void)
Miao Xiec0ff7452010-05-24 14:32:08 -0700129{
Dima Zavin89affbf2017-08-02 13:32:18 -0700130 if (!static_branch_unlikely(&cpusets_pre_enable_key))
Mel Gorman46e700ab2015-11-06 16:28:15 -0800131 return 0;
132
Mel Gormancc9a6c82012-03-21 16:34:11 -0700133 return read_seqcount_begin(&current->mems_allowed_seq);
Miao Xiec0ff7452010-05-24 14:32:08 -0700134}
135
Mel Gormancc9a6c82012-03-21 16:34:11 -0700136/*
Mel Gormand26914d2014-04-03 14:47:24 -0700137 * If this returns true, the operation that took place after
138 * read_mems_allowed_begin may have failed artificially due to a concurrent
139 * update of mems_allowed. It is up to the caller to retry the operation if
Mel Gormancc9a6c82012-03-21 16:34:11 -0700140 * appropriate.
141 */
Mel Gormand26914d2014-04-03 14:47:24 -0700142static inline bool read_mems_allowed_retry(unsigned int seq)
Miao Xiec0ff7452010-05-24 14:32:08 -0700143{
Dima Zavin89affbf2017-08-02 13:32:18 -0700144 if (!static_branch_unlikely(&cpusets_enabled_key))
Mel Gorman46e700ab2015-11-06 16:28:15 -0800145 return false;
146
Mel Gormand26914d2014-04-03 14:47:24 -0700147 return read_seqcount_retry(&current->mems_allowed_seq, seq);
Miao Xiec0ff7452010-05-24 14:32:08 -0700148}
149
Miao Xie58568d22009-06-16 15:31:49 -0700150static inline void set_mems_allowed(nodemask_t nodemask)
151{
John Stultzdb751fe2013-10-07 15:52:00 -0700152 unsigned long flags;
153
Miao Xiec0ff7452010-05-24 14:32:08 -0700154 task_lock(current);
John Stultzdb751fe2013-10-07 15:52:00 -0700155 local_irq_save(flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -0700156 write_seqcount_begin(&current->mems_allowed_seq);
Miao Xie58568d22009-06-16 15:31:49 -0700157 current->mems_allowed = nodemask;
Mel Gormancc9a6c82012-03-21 16:34:11 -0700158 write_seqcount_end(&current->mems_allowed_seq);
John Stultzdb751fe2013-10-07 15:52:00 -0700159 local_irq_restore(flags);
Miao Xiec0ff7452010-05-24 14:32:08 -0700160 task_unlock(current);
Miao Xie58568d22009-06-16 15:31:49 -0700161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#else /* !CONFIG_CPUSETS */
164
Mel Gorman664eedd2014-06-04 16:10:08 -0700165static inline bool cpusets_enabled(void) { return false; }
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static inline int cpuset_init(void) { return 0; }
168static inline void cpuset_init_smp(void) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Peter Zijlstra50e76632017-09-07 11:13:38 +0200170static inline void cpuset_force_rebuild(void) { }
171
Rakib Mullick30e03ac2017-04-09 07:36:14 +0600172static inline void cpuset_update_active_cpus(void)
Tejun Heo3a101d02010-06-08 21:40:36 +0200173{
174 partition_sched_domains(1, NULL, NULL);
175}
176
Peter Zijlstra50e76632017-09-07 11:13:38 +0200177static inline void cpuset_wait_for_hotplug(void) { }
178
Li Zefan6af866a2009-01-07 18:08:45 -0800179static inline void cpuset_cpus_allowed(struct task_struct *p,
180 struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Rusty Russellaa85ea52009-03-30 22:05:15 -0600182 cpumask_copy(mask, cpu_possible_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Peter Zijlstra2baab4e2012-03-20 15:57:01 +0100185static inline void cpuset_cpus_allowed_fallback(struct task_struct *p)
Oleg Nesterov9084bb82010-03-15 10:10:27 +0100186{
Oleg Nesterov9084bb82010-03-15 10:10:27 +0100187}
188
Paul Jackson909d75a2006-01-08 01:01:55 -0800189static inline nodemask_t cpuset_mems_allowed(struct task_struct *p)
190{
191 return node_possible_map;
192}
193
Lai Jiangshan38d7bee2012-12-12 13:51:24 -0800194#define cpuset_current_mems_allowed (node_states[N_MEMORY])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static inline void cpuset_init_current_mems_allowed(void) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Mel Gorman19770b32008-04-28 02:12:18 -0700197static inline int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 return 1;
200}
201
Vlastimil Babka002f2902016-05-19 17:14:30 -0700202static inline bool cpuset_node_allowed(int node, gfp_t gfp_mask)
David Rientjesa1bc5a42009-04-02 16:57:54 -0700203{
Vlastimil Babka002f2902016-05-19 17:14:30 -0700204 return true;
David Rientjesa1bc5a42009-04-02 16:57:54 -0700205}
206
Vlastimil Babka002f2902016-05-19 17:14:30 -0700207static inline bool __cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Vlastimil Babka002f2902016-05-19 17:14:30 -0700209 return true;
210}
211
212static inline bool cpuset_zone_allowed(struct zone *z, gfp_t gfp_mask)
213{
214 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
David Rientjesbbe373f2007-10-16 23:25:58 -0700217static inline int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
218 const struct task_struct *tsk2)
Paul Jacksonef08e3b2005-09-06 15:18:13 -0700219{
220 return 1;
221}
222
Paul Jackson3e0d98b2006-01-08 01:01:49 -0800223static inline void cpuset_memory_pressure_bump(void) {}
224
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800225static inline void cpuset_task_status_allowed(struct seq_file *m,
226 struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Paul Jackson825a46a2006-03-24 03:16:03 -0800230static inline int cpuset_mem_spread_node(void)
231{
232 return 0;
233}
234
Jack Steiner6adef3e2010-05-26 14:42:49 -0700235static inline int cpuset_slab_spread_node(void)
236{
237 return 0;
238}
239
Paul Jackson825a46a2006-03-24 03:16:03 -0800240static inline int cpuset_do_page_mem_spread(void)
241{
242 return 0;
243}
244
245static inline int cpuset_do_slab_mem_spread(void)
246{
247 return 0;
248}
249
Paul Menage8793d852007-10-18 23:39:39 -0700250static inline int current_cpuset_is_being_rebound(void)
251{
252 return 0;
253}
254
Max Krasnyanskye761b772008-07-15 04:43:49 -0700255static inline void rebuild_sched_domains(void)
256{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -0700257 partition_sched_domains(1, NULL, NULL);
Max Krasnyanskye761b772008-07-15 04:43:49 -0700258}
259
David Rientjesda39da32015-11-05 18:48:05 -0800260static inline void cpuset_print_current_mems_allowed(void)
David Rientjes75aa1992009-01-06 14:39:01 -0800261{
262}
263
Miao Xie58568d22009-06-16 15:31:49 -0700264static inline void set_mems_allowed(nodemask_t nodemask)
265{
266}
267
Mel Gormand26914d2014-04-03 14:47:24 -0700268static inline unsigned int read_mems_allowed_begin(void)
Miao Xiec0ff7452010-05-24 14:32:08 -0700269{
Mel Gormancc9a6c82012-03-21 16:34:11 -0700270 return 0;
Miao Xiec0ff7452010-05-24 14:32:08 -0700271}
272
Mel Gormand26914d2014-04-03 14:47:24 -0700273static inline bool read_mems_allowed_retry(unsigned int seq)
Miao Xiec0ff7452010-05-24 14:32:08 -0700274{
Mel Gormand26914d2014-04-03 14:47:24 -0700275 return false;
Miao Xiec0ff7452010-05-24 14:32:08 -0700276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#endif /* !CONFIG_CPUSETS */
279
280#endif /* _LINUX_CPUSET_H */