1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_NODEMASK_H
3#define __LINUX_NODEMASK_H
4
5/*
6 * Nodemasks provide a bitmap suitable for representing the
7 * set of Node's in a system, one bit position per Node number.
8 *
9 * See detailed comments in the file linux/bitmap.h describing the
10 * data type on which these nodemasks are based.
11 *
12 * For details of nodemask_parse_user(), see bitmap_parse_user() in
13 * lib/bitmap.c. For details of nodelist_parse(), see bitmap_parselist(),
14 * also in bitmap.c. For details of node_remap(), see bitmap_bitremap in
15 * lib/bitmap.c. For details of nodes_remap(), see bitmap_remap in
16 * lib/bitmap.c. For details of nodes_onto(), see bitmap_onto in
17 * lib/bitmap.c. For details of nodes_fold(), see bitmap_fold in
18 * lib/bitmap.c.
19 *
20 * The available nodemask operations are:
21 *
22 * void node_set(node, mask) turn on bit 'node' in mask
23 * void node_clear(node, mask) turn off bit 'node' in mask
24 * void nodes_setall(mask) set all bits
25 * void nodes_clear(mask) clear all bits
26 * int node_isset(node, mask) true iff bit 'node' set in mask
27 * int node_test_and_set(node, mask) test and set bit 'node' in mask
28 *
29 * void nodes_and(dst, src1, src2) dst = src1 & src2 [intersection]
30 * void nodes_or(dst, src1, src2) dst = src1 | src2 [union]
31 * void nodes_xor(dst, src1, src2) dst = src1 ^ src2
32 * void nodes_andnot(dst, src1, src2) dst = src1 & ~src2
33 * void nodes_complement(dst, src) dst = ~src
34 *
35 * int nodes_equal(mask1, mask2) Does mask1 == mask2?
36 * int nodes_intersects(mask1, mask2) Do mask1 and mask2 intersect?
37 * int nodes_subset(mask1, mask2) Is mask1 a subset of mask2?
38 * int nodes_empty(mask) Is mask empty (no bits sets)?
39 * int nodes_full(mask) Is mask full (all bits sets)?
40 * int nodes_weight(mask) Hamming weight - number of set bits
41 *
42 * unsigned int first_node(mask) Number lowest set bit, or MAX_NUMNODES
43 * unsigend int next_node(node, mask) Next node past 'node', or MAX_NUMNODES
44 * unsigned int next_node_in(node, mask) Next node past 'node', or wrap to first,
45 * or MAX_NUMNODES
46 * unsigned int first_unset_node(mask) First node not set in mask, or
47 * MAX_NUMNODES
48 *
49 * nodemask_t nodemask_of_node(node) Return nodemask with bit 'node' set
50 * NODE_MASK_ALL Initializer - all bits set
51 * NODE_MASK_NONE Initializer - no bits set
52 * unsigned long *nodes_addr(mask) Array of unsigned long's in mask
53 *
54 * int nodemask_parse_user(ubuf, ulen, mask) Parse ascii string as nodemask
55 * int nodelist_parse(buf, map) Parse ascii string as nodelist
56 * int node_remap(oldbit, old, new) newbit = map(old, new)(oldbit)
57 * void nodes_remap(dst, src, old, new) *dst = map(old, new)(src)
58 * void nodes_onto(dst, orig, relmap) *dst = orig relative to relmap
59 * void nodes_fold(dst, orig, sz) dst bits = orig bits mod sz
60 *
61 * for_each_node_mask(node, mask) for-loop node over mask
62 *
63 * int num_online_nodes() Number of online Nodes
64 * int num_possible_nodes() Number of all possible Nodes
65 *
66 * int node_random(mask) Random node with set bit in mask
67 *
68 * int node_online(node) Is some node online?
69 * int node_possible(node) Is some node possible?
70 *
71 * node_set_online(node) set bit 'node' in node_online_map
72 * node_set_offline(node) clear bit 'node' in node_online_map
73 *
74 * for_each_node(node) for-loop node over node_possible_map
75 * for_each_online_node(node) for-loop node over node_online_map
76 *
77 * Subtlety:
78 * 1) The 'type-checked' form of node_isset() causes gcc (3.3.2, anyway)
79 * to generate slightly worse code. So use a simple one-line #define
80 * for node_isset(), instead of wrapping an inline inside a macro, the
81 * way we do the other calls.
82 *
83 * NODEMASK_SCRATCH
84 * When doing above logical AND, OR, XOR, Remap operations the callers tend to
85 * need temporary nodemask_t's on the stack. But if NODES_SHIFT is large,
86 * nodemask_t's consume too much stack space. NODEMASK_SCRATCH is a helper
87 * for such situations. See below and CPUMASK_ALLOC also.
88 */
89
90#include <linux/threads.h>
91#include <linux/bitmap.h>
92#include <linux/minmax.h>
93#include <linux/nodemask_types.h>
94#include <linux/random.h>
95
96extern nodemask_t _unused_nodemask_arg_;
97
98/**
99 * nodemask_pr_args - printf args to output a nodemask
100 * @maskp: nodemask to be printed
101 *
102 * Can be used to provide arguments for '%*pb[l]' when printing a nodemask.
103 */
104#define nodemask_pr_args(maskp) __nodemask_pr_numnodes(maskp), \
105 __nodemask_pr_bits(maskp)
106static __always_inline unsigned int __nodemask_pr_numnodes(const nodemask_t *m)
107{
108 return m ? MAX_NUMNODES : 0;
109}
110static __always_inline const unsigned long *__nodemask_pr_bits(const nodemask_t *m)
111{
112 return m ? m->bits : NULL;
113}
114
115/*
116 * The inline keyword gives the compiler room to decide to inline, or
117 * not inline a function as it sees best. However, as these functions
118 * are called in both __init and non-__init functions, if they are not
119 * inlined we will end up with a section mismatch error (of the type of
120 * freeable items not being freed). So we must use __always_inline here
121 * to fix the problem. If other functions in the future also end up in
122 * this situation they will also need to be annotated as __always_inline
123 */
124#define node_set(node, dst) __node_set((node), &(dst))
125static __always_inline void __node_set(int node, volatile nodemask_t *dstp)
126{
127 set_bit(nr: node, addr: dstp->bits);
128}
129
130#define node_clear(node, dst) __node_clear((node), &(dst))
131static __always_inline void __node_clear(int node, volatile nodemask_t *dstp)
132{
133 clear_bit(nr: node, addr: dstp->bits);
134}
135
136#define nodes_setall(dst) __nodes_setall(&(dst), MAX_NUMNODES)
137static __always_inline void __nodes_setall(nodemask_t *dstp, unsigned int nbits)
138{
139 bitmap_fill(dst: dstp->bits, nbits);
140}
141
142#define nodes_clear(dst) __nodes_clear(&(dst), MAX_NUMNODES)
143static __always_inline void __nodes_clear(nodemask_t *dstp, unsigned int nbits)
144{
145 bitmap_zero(dst: dstp->bits, nbits);
146}
147
148/* No static inline type checking - see Subtlety (1) above. */
149#define node_isset(node, nodemask) test_bit((node), (nodemask).bits)
150
151#define node_test_and_set(node, nodemask) \
152 __node_test_and_set((node), &(nodemask))
153static __always_inline bool __node_test_and_set(int node, nodemask_t *addr)
154{
155 return test_and_set_bit(nr: node, addr: addr->bits);
156}
157
158#define nodes_and(dst, src1, src2) \
159 __nodes_and(&(dst), &(src1), &(src2), MAX_NUMNODES)
160static __always_inline void __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
161 const nodemask_t *src2p, unsigned int nbits)
162{
163 bitmap_and(dst: dstp->bits, src1: src1p->bits, src2: src2p->bits, nbits);
164}
165
166#define nodes_or(dst, src1, src2) \
167 __nodes_or(&(dst), &(src1), &(src2), MAX_NUMNODES)
168static __always_inline void __nodes_or(nodemask_t *dstp, const nodemask_t *src1p,
169 const nodemask_t *src2p, unsigned int nbits)
170{
171 bitmap_or(dst: dstp->bits, src1: src1p->bits, src2: src2p->bits, nbits);
172}
173
174#define nodes_xor(dst, src1, src2) \
175 __nodes_xor(&(dst), &(src1), &(src2), MAX_NUMNODES)
176static __always_inline void __nodes_xor(nodemask_t *dstp, const nodemask_t *src1p,
177 const nodemask_t *src2p, unsigned int nbits)
178{
179 bitmap_xor(dst: dstp->bits, src1: src1p->bits, src2: src2p->bits, nbits);
180}
181
182#define nodes_andnot(dst, src1, src2) \
183 __nodes_andnot(&(dst), &(src1), &(src2), MAX_NUMNODES)
184static __always_inline void __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
185 const nodemask_t *src2p, unsigned int nbits)
186{
187 bitmap_andnot(dst: dstp->bits, src1: src1p->bits, src2: src2p->bits, nbits);
188}
189
190#define nodes_copy(dst, src) __nodes_copy(&(dst), &(src), MAX_NUMNODES)
191static __always_inline void __nodes_copy(nodemask_t *dstp,
192 const nodemask_t *srcp, unsigned int nbits)
193{
194 bitmap_copy(dst: dstp->bits, src: srcp->bits, nbits);
195}
196
197#define nodes_complement(dst, src) \
198 __nodes_complement(&(dst), &(src), MAX_NUMNODES)
199static __always_inline void __nodes_complement(nodemask_t *dstp,
200 const nodemask_t *srcp, unsigned int nbits)
201{
202 bitmap_complement(dst: dstp->bits, src: srcp->bits, nbits);
203}
204
205#define nodes_equal(src1, src2) \
206 __nodes_equal(&(src1), &(src2), MAX_NUMNODES)
207static __always_inline bool __nodes_equal(const nodemask_t *src1p,
208 const nodemask_t *src2p, unsigned int nbits)
209{
210 return bitmap_equal(src1: src1p->bits, src2: src2p->bits, nbits);
211}
212
213#define nodes_intersects(src1, src2) \
214 __nodes_intersects(&(src1), &(src2), MAX_NUMNODES)
215static __always_inline bool __nodes_intersects(const nodemask_t *src1p,
216 const nodemask_t *src2p, unsigned int nbits)
217{
218 return bitmap_intersects(src1: src1p->bits, src2: src2p->bits, nbits);
219}
220
221#define nodes_subset(src1, src2) \
222 __nodes_subset(&(src1), &(src2), MAX_NUMNODES)
223static __always_inline bool __nodes_subset(const nodemask_t *src1p,
224 const nodemask_t *src2p, unsigned int nbits)
225{
226 return bitmap_subset(src1: src1p->bits, src2: src2p->bits, nbits);
227}
228
229#define nodes_empty(src) __nodes_empty(&(src), MAX_NUMNODES)
230static __always_inline bool __nodes_empty(const nodemask_t *srcp, unsigned int nbits)
231{
232 return bitmap_empty(src: srcp->bits, nbits);
233}
234
235#define nodes_full(nodemask) __nodes_full(&(nodemask), MAX_NUMNODES)
236static __always_inline bool __nodes_full(const nodemask_t *srcp, unsigned int nbits)
237{
238 return bitmap_full(src: srcp->bits, nbits);
239}
240
241#define nodes_weight(nodemask) __nodes_weight(&(nodemask), MAX_NUMNODES)
242static __always_inline int __nodes_weight(const nodemask_t *srcp, unsigned int nbits)
243{
244 return bitmap_weight(src: srcp->bits, nbits);
245}
246
247/* FIXME: better would be to fix all architectures to never return
248 > MAX_NUMNODES, then the silly min_ts could be dropped. */
249
250#define first_node(src) __first_node(&(src))
251static __always_inline unsigned int __first_node(const nodemask_t *srcp)
252{
253 return min_t(unsigned int, MAX_NUMNODES, find_first_bit(srcp->bits, MAX_NUMNODES));
254}
255
256#define next_node(n, src) __next_node((n), &(src))
257static __always_inline unsigned int __next_node(int n, const nodemask_t *srcp)
258{
259 return min_t(unsigned int, MAX_NUMNODES, find_next_bit(srcp->bits, MAX_NUMNODES, n+1));
260}
261
262/*
263 * Find the next present node in src, starting after node n, wrapping around to
264 * the first node in src if needed. Returns MAX_NUMNODES if src is empty.
265 */
266#define next_node_in(n, src) __next_node_in((n), &(src))
267static __always_inline unsigned int __next_node_in(int node, const nodemask_t *srcp)
268{
269 unsigned int ret = __next_node(n: node, srcp);
270
271 if (ret == MAX_NUMNODES)
272 ret = __first_node(srcp);
273 return ret;
274}
275
276static __always_inline void init_nodemask_of_node(nodemask_t *mask, int node)
277{
278 nodes_clear(*mask);
279 node_set(node, *mask);
280}
281
282#define nodemask_of_node(node) \
283({ \
284 typeof(_unused_nodemask_arg_) m; \
285 if (sizeof(m) == sizeof(unsigned long)) { \
286 m.bits[0] = 1UL << (node); \
287 } else { \
288 init_nodemask_of_node(&m, (node)); \
289 } \
290 m; \
291})
292
293#define first_unset_node(mask) __first_unset_node(&(mask))
294static __always_inline unsigned int __first_unset_node(const nodemask_t *maskp)
295{
296 return min_t(unsigned int, MAX_NUMNODES,
297 find_first_zero_bit(maskp->bits, MAX_NUMNODES));
298}
299
300#define NODE_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(MAX_NUMNODES)
301
302#if MAX_NUMNODES <= BITS_PER_LONG
303
304#define NODE_MASK_ALL \
305((nodemask_t) { { \
306 [BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD \
307} })
308
309#else
310
311#define NODE_MASK_ALL \
312((nodemask_t) { { \
313 [0 ... BITS_TO_LONGS(MAX_NUMNODES)-2] = ~0UL, \
314 [BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD \
315} })
316
317#endif
318
319#define NODE_MASK_NONE \
320((nodemask_t) { { \
321 [0 ... BITS_TO_LONGS(MAX_NUMNODES)-1] = 0UL \
322} })
323
324#define nodes_addr(src) ((src).bits)
325
326#define nodemask_parse_user(ubuf, ulen, dst) \
327 __nodemask_parse_user((ubuf), (ulen), &(dst), MAX_NUMNODES)
328static __always_inline int __nodemask_parse_user(const char __user *buf, int len,
329 nodemask_t *dstp, int nbits)
330{
331 return bitmap_parse_user(ubuf: buf, ulen: len, dst: dstp->bits, nbits);
332}
333
334#define nodelist_parse(buf, dst) __nodelist_parse((buf), &(dst), MAX_NUMNODES)
335static __always_inline int __nodelist_parse(const char *buf, nodemask_t *dstp, int nbits)
336{
337 return bitmap_parselist(buf, maskp: dstp->bits, nmaskbits: nbits);
338}
339
340#define node_remap(oldbit, old, new) \
341 __node_remap((oldbit), &(old), &(new), MAX_NUMNODES)
342static __always_inline int __node_remap(int oldbit,
343 const nodemask_t *oldp, const nodemask_t *newp, int nbits)
344{
345 return bitmap_bitremap(oldbit, old: oldp->bits, new: newp->bits, bits: nbits);
346}
347
348#define nodes_remap(dst, src, old, new) \
349 __nodes_remap(&(dst), &(src), &(old), &(new), MAX_NUMNODES)
350static __always_inline void __nodes_remap(nodemask_t *dstp, const nodemask_t *srcp,
351 const nodemask_t *oldp, const nodemask_t *newp, int nbits)
352{
353 bitmap_remap(dst: dstp->bits, src: srcp->bits, old: oldp->bits, new: newp->bits, nbits);
354}
355
356#define nodes_onto(dst, orig, relmap) \
357 __nodes_onto(&(dst), &(orig), &(relmap), MAX_NUMNODES)
358static __always_inline void __nodes_onto(nodemask_t *dstp, const nodemask_t *origp,
359 const nodemask_t *relmapp, int nbits)
360{
361 bitmap_onto(dst: dstp->bits, orig: origp->bits, relmap: relmapp->bits, bits: nbits);
362}
363
364#define nodes_fold(dst, orig, sz) \
365 __nodes_fold(&(dst), &(orig), sz, MAX_NUMNODES)
366static __always_inline void __nodes_fold(nodemask_t *dstp, const nodemask_t *origp,
367 int sz, int nbits)
368{
369 bitmap_fold(dst: dstp->bits, orig: origp->bits, sz, nbits);
370}
371
372#if MAX_NUMNODES > 1
373#define for_each_node_mask(node, mask) \
374 for ((node) = first_node(mask); \
375 (node) < MAX_NUMNODES; \
376 (node) = next_node((node), (mask)))
377#else /* MAX_NUMNODES == 1 */
378#define for_each_node_mask(node, mask) \
379 for ((node) = 0; (node) < 1 && !nodes_empty(mask); (node)++)
380#endif /* MAX_NUMNODES */
381
382/*
383 * Bitmasks that are kept for all the nodes.
384 */
385enum node_states {
386 N_POSSIBLE, /* The node could become online at some point */
387 N_ONLINE, /* The node is online */
388 N_NORMAL_MEMORY, /* The node has regular memory */
389#ifdef CONFIG_HIGHMEM
390 N_HIGH_MEMORY, /* The node has regular or high memory */
391#else
392 N_HIGH_MEMORY = N_NORMAL_MEMORY,
393#endif
394 N_MEMORY, /* The node has memory(regular, high, movable) */
395 N_CPU, /* The node has one or more cpus */
396 N_GENERIC_INITIATOR, /* The node has one or more Generic Initiators */
397 NR_NODE_STATES
398};
399
400/*
401 * The following particular system nodemasks and operations
402 * on them manage all possible and online nodes.
403 */
404
405extern nodemask_t node_states[NR_NODE_STATES];
406
407#if MAX_NUMNODES > 1
408static __always_inline int node_state(int node, enum node_states state)
409{
410 return node_isset(node, node_states[state]);
411}
412
413static __always_inline void node_set_state(int node, enum node_states state)
414{
415 __node_set(node, dstp: &node_states[state]);
416}
417
418static __always_inline void node_clear_state(int node, enum node_states state)
419{
420 __node_clear(node, dstp: &node_states[state]);
421}
422
423static __always_inline int num_node_state(enum node_states state)
424{
425 return nodes_weight(node_states[state]);
426}
427
428#define for_each_node_state(__node, __state) \
429 for_each_node_mask((__node), node_states[__state])
430
431#define first_online_node first_node(node_states[N_ONLINE])
432#define first_memory_node first_node(node_states[N_MEMORY])
433static __always_inline unsigned int next_online_node(int nid)
434{
435 return next_node(nid, node_states[N_ONLINE]);
436}
437static __always_inline unsigned int next_memory_node(int nid)
438{
439 return next_node(nid, node_states[N_MEMORY]);
440}
441
442extern unsigned int nr_node_ids;
443extern unsigned int nr_online_nodes;
444
445static __always_inline void node_set_online(int nid)
446{
447 node_set_state(node: nid, state: N_ONLINE);
448 nr_online_nodes = num_node_state(state: N_ONLINE);
449}
450
451static __always_inline void node_set_offline(int nid)
452{
453 node_clear_state(node: nid, state: N_ONLINE);
454 nr_online_nodes = num_node_state(state: N_ONLINE);
455}
456
457#else
458
459static __always_inline int node_state(int node, enum node_states state)
460{
461 return node == 0;
462}
463
464static __always_inline void node_set_state(int node, enum node_states state)
465{
466}
467
468static __always_inline void node_clear_state(int node, enum node_states state)
469{
470}
471
472static __always_inline int num_node_state(enum node_states state)
473{
474 return 1;
475}
476
477#define for_each_node_state(node, __state) \
478 for ( (node) = 0; (node) == 0; (node) = 1)
479
480#define first_online_node 0
481#define first_memory_node 0
482#define next_online_node(nid) (MAX_NUMNODES)
483#define next_memory_node(nid) (MAX_NUMNODES)
484#define nr_node_ids 1U
485#define nr_online_nodes 1U
486
487#define node_set_online(node) node_set_state((node), N_ONLINE)
488#define node_set_offline(node) node_clear_state((node), N_ONLINE)
489
490#endif
491
492static __always_inline int node_random(const nodemask_t *maskp)
493{
494#if defined(CONFIG_NUMA) && (MAX_NUMNODES > 1)
495 int w, bit;
496
497 w = nodes_weight(*maskp);
498 switch (w) {
499 case 0:
500 bit = NUMA_NO_NODE;
501 break;
502 case 1:
503 bit = first_node(*maskp);
504 break;
505 default:
506 bit = find_nth_bit(addr: maskp->bits, MAX_NUMNODES, n: get_random_u32_below(ceil: w));
507 break;
508 }
509 return bit;
510#else
511 return 0;
512#endif
513}
514
515#define node_online_map node_states[N_ONLINE]
516#define node_possible_map node_states[N_POSSIBLE]
517
518#define num_online_nodes() num_node_state(N_ONLINE)
519#define num_possible_nodes() num_node_state(N_POSSIBLE)
520#define node_online(node) node_state((node), N_ONLINE)
521#define node_possible(node) node_state((node), N_POSSIBLE)
522
523#define for_each_node(node) for_each_node_state(node, N_POSSIBLE)
524#define for_each_online_node(node) for_each_node_state(node, N_ONLINE)
525#define for_each_node_with_cpus(node) for_each_node_state(node, N_CPU)
526
527/*
528 * For nodemask scratch area.
529 * NODEMASK_ALLOC(type, name) allocates an object with a specified type and
530 * name.
531 */
532#if NODES_SHIFT > 8 /* nodemask_t > 32 bytes */
533#define NODEMASK_ALLOC(type, name, gfp_flags) \
534 type *name = kmalloc(sizeof(*name), gfp_flags)
535#define NODEMASK_FREE(m) kfree(m)
536#else
537#define NODEMASK_ALLOC(type, name, gfp_flags) type _##name, *name = &_##name
538#define NODEMASK_FREE(m) do {} while (0)
539#endif
540
541/* Example structure for using NODEMASK_ALLOC, used in mempolicy. */
542struct nodemask_scratch {
543 nodemask_t mask1;
544 nodemask_t mask2;
545};
546
547#define NODEMASK_SCRATCH(x) \
548 NODEMASK_ALLOC(struct nodemask_scratch, x, \
549 GFP_KERNEL | __GFP_NORETRY)
550#define NODEMASK_SCRATCH_FREE(x) NODEMASK_FREE(x)
551
552
553#endif /* __LINUX_NODEMASK_H */
554

source code of linux/include/linux/nodemask.h