nodemask.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #ifndef __LINUX_NODEMASK_H
  2. #define __LINUX_NODEMASK_H
  3. /*
  4. * Nodemasks provide a bitmap suitable for representing the
  5. * set of Node's in a system, one bit position per Node number.
  6. *
  7. * See detailed comments in the file linux/bitmap.h describing the
  8. * data type on which these nodemasks are based.
  9. *
  10. * For details of nodemask_scnprintf() and nodemask_parse_user(),
  11. * see bitmap_scnprintf() and bitmap_parse_user() in lib/bitmap.c.
  12. * For details of nodelist_scnprintf() and nodelist_parse(), see
  13. * bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c.
  14. * For details of node_remap(), see bitmap_bitremap in lib/bitmap.c.
  15. * For details of nodes_remap(), see bitmap_remap in lib/bitmap.c.
  16. * For details of nodes_onto(), see bitmap_onto in lib/bitmap.c.
  17. * For details of nodes_fold(), see bitmap_fold in lib/bitmap.c.
  18. *
  19. * The available nodemask operations are:
  20. *
  21. * void node_set(node, mask) turn on bit 'node' in mask
  22. * void node_clear(node, mask) turn off bit 'node' in mask
  23. * void nodes_setall(mask) set all bits
  24. * void nodes_clear(mask) clear all bits
  25. * int node_isset(node, mask) true iff bit 'node' set in mask
  26. * int node_test_and_set(node, mask) test and set bit 'node' in mask
  27. *
  28. * void nodes_and(dst, src1, src2) dst = src1 & src2 [intersection]
  29. * void nodes_or(dst, src1, src2) dst = src1 | src2 [union]
  30. * void nodes_xor(dst, src1, src2) dst = src1 ^ src2
  31. * void nodes_andnot(dst, src1, src2) dst = src1 & ~src2
  32. * void nodes_complement(dst, src) dst = ~src
  33. *
  34. * int nodes_equal(mask1, mask2) Does mask1 == mask2?
  35. * int nodes_intersects(mask1, mask2) Do mask1 and mask2 intersect?
  36. * int nodes_subset(mask1, mask2) Is mask1 a subset of mask2?
  37. * int nodes_empty(mask) Is mask empty (no bits sets)?
  38. * int nodes_full(mask) Is mask full (all bits sets)?
  39. * int nodes_weight(mask) Hamming weight - number of set bits
  40. *
  41. * void nodes_shift_right(dst, src, n) Shift right
  42. * void nodes_shift_left(dst, src, n) Shift left
  43. *
  44. * int first_node(mask) Number lowest set bit, or MAX_NUMNODES
  45. * int next_node(node, mask) Next node past 'node', or MAX_NUMNODES
  46. * 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_scnprintf(buf, len, mask) Format nodemask for printing
  55. * int nodemask_parse_user(ubuf, ulen, mask) Parse ascii string as nodemask
  56. * int nodelist_scnprintf(buf, len, mask) Format nodemask as list for printing
  57. * int nodelist_parse(buf, map) Parse ascii string as nodelist
  58. * int node_remap(oldbit, old, new) newbit = map(old, new)(oldbit)
  59. * void nodes_remap(dst, src, old, new) *dst = map(old, new)(src)
  60. * void nodes_onto(dst, orig, relmap) *dst = orig relative to relmap
  61. * void nodes_fold(dst, orig, sz) dst bits = orig bits mod sz
  62. *
  63. * for_each_node_mask(node, mask) for-loop node over mask
  64. *
  65. * int num_online_nodes() Number of online Nodes
  66. * int num_possible_nodes() Number of all possible Nodes
  67. *
  68. * int node_online(node) Is some node online?
  69. * int node_possible(node) Is some node possible?
  70. *
  71. * int any_online_node(mask) First online node in mask
  72. *
  73. * node_set_online(node) set bit 'node' in node_online_map
  74. * node_set_offline(node) clear bit 'node' in node_online_map
  75. *
  76. * for_each_node(node) for-loop node over node_possible_map
  77. * for_each_online_node(node) for-loop node over node_online_map
  78. *
  79. * Subtlety:
  80. * 1) The 'type-checked' form of node_isset() causes gcc (3.3.2, anyway)
  81. * to generate slightly worse code. So use a simple one-line #define
  82. * for node_isset(), instead of wrapping an inline inside a macro, the
  83. * way we do the other calls.
  84. */
  85. #include <linux/kernel.h>
  86. #include <linux/threads.h>
  87. #include <linux/bitmap.h>
  88. #include <linux/numa.h>
  89. typedef struct { DECLARE_BITMAP(bits, MAX_NUMNODES); } nodemask_t;
  90. extern nodemask_t _unused_nodemask_arg_;
  91. #define node_set(node, dst) __node_set((node), &(dst))
  92. static inline void __node_set(int node, volatile nodemask_t *dstp)
  93. {
  94. set_bit(node, dstp->bits);
  95. }
  96. #define node_clear(node, dst) __node_clear((node), &(dst))
  97. static inline void __node_clear(int node, volatile nodemask_t *dstp)
  98. {
  99. clear_bit(node, dstp->bits);
  100. }
  101. #define nodes_setall(dst) __nodes_setall(&(dst), MAX_NUMNODES)
  102. static inline void __nodes_setall(nodemask_t *dstp, int nbits)
  103. {
  104. bitmap_fill(dstp->bits, nbits);
  105. }
  106. #define nodes_clear(dst) __nodes_clear(&(dst), MAX_NUMNODES)
  107. static inline void __nodes_clear(nodemask_t *dstp, int nbits)
  108. {
  109. bitmap_zero(dstp->bits, nbits);
  110. }
  111. /* No static inline type checking - see Subtlety (1) above. */
  112. #define node_isset(node, nodemask) test_bit((node), (nodemask).bits)
  113. #define node_test_and_set(node, nodemask) \
  114. __node_test_and_set((node), &(nodemask))
  115. static inline int __node_test_and_set(int node, nodemask_t *addr)
  116. {
  117. return test_and_set_bit(node, addr->bits);
  118. }
  119. #define nodes_and(dst, src1, src2) \
  120. __nodes_and(&(dst), &(src1), &(src2), MAX_NUMNODES)
  121. static inline void __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
  122. const nodemask_t *src2p, int nbits)
  123. {
  124. bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
  125. }
  126. #define nodes_or(dst, src1, src2) \
  127. __nodes_or(&(dst), &(src1), &(src2), MAX_NUMNODES)
  128. static inline void __nodes_or(nodemask_t *dstp, const nodemask_t *src1p,
  129. const nodemask_t *src2p, int nbits)
  130. {
  131. bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
  132. }
  133. #define nodes_xor(dst, src1, src2) \
  134. __nodes_xor(&(dst), &(src1), &(src2), MAX_NUMNODES)
  135. static inline void __nodes_xor(nodemask_t *dstp, const nodemask_t *src1p,
  136. const nodemask_t *src2p, int nbits)
  137. {
  138. bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
  139. }
  140. #define nodes_andnot(dst, src1, src2) \
  141. __nodes_andnot(&(dst), &(src1), &(src2), MAX_NUMNODES)
  142. static inline void __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
  143. const nodemask_t *src2p, int nbits)
  144. {
  145. bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
  146. }
  147. #define nodes_complement(dst, src) \
  148. __nodes_complement(&(dst), &(src), MAX_NUMNODES)
  149. static inline void __nodes_complement(nodemask_t *dstp,
  150. const nodemask_t *srcp, int nbits)
  151. {
  152. bitmap_complement(dstp->bits, srcp->bits, nbits);
  153. }
  154. #define nodes_equal(src1, src2) \
  155. __nodes_equal(&(src1), &(src2), MAX_NUMNODES)
  156. static inline int __nodes_equal(const nodemask_t *src1p,
  157. const nodemask_t *src2p, int nbits)
  158. {
  159. return bitmap_equal(src1p->bits, src2p->bits, nbits);
  160. }
  161. #define nodes_intersects(src1, src2) \
  162. __nodes_intersects(&(src1), &(src2), MAX_NUMNODES)
  163. static inline int __nodes_intersects(const nodemask_t *src1p,
  164. const nodemask_t *src2p, int nbits)
  165. {
  166. return bitmap_intersects(src1p->bits, src2p->bits, nbits);
  167. }
  168. #define nodes_subset(src1, src2) \
  169. __nodes_subset(&(src1), &(src2), MAX_NUMNODES)
  170. static inline int __nodes_subset(const nodemask_t *src1p,
  171. const nodemask_t *src2p, int nbits)
  172. {
  173. return bitmap_subset(src1p->bits, src2p->bits, nbits);
  174. }
  175. #define nodes_empty(src) __nodes_empty(&(src), MAX_NUMNODES)
  176. static inline int __nodes_empty(const nodemask_t *srcp, int nbits)
  177. {
  178. return bitmap_empty(srcp->bits, nbits);
  179. }
  180. #define nodes_full(nodemask) __nodes_full(&(nodemask), MAX_NUMNODES)
  181. static inline int __nodes_full(const nodemask_t *srcp, int nbits)
  182. {
  183. return bitmap_full(srcp->bits, nbits);
  184. }
  185. #define nodes_weight(nodemask) __nodes_weight(&(nodemask), MAX_NUMNODES)
  186. static inline int __nodes_weight(const nodemask_t *srcp, int nbits)
  187. {
  188. return bitmap_weight(srcp->bits, nbits);
  189. }
  190. #define nodes_shift_right(dst, src, n) \
  191. __nodes_shift_right(&(dst), &(src), (n), MAX_NUMNODES)
  192. static inline void __nodes_shift_right(nodemask_t *dstp,
  193. const nodemask_t *srcp, int n, int nbits)
  194. {
  195. bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
  196. }
  197. #define nodes_shift_left(dst, src, n) \
  198. __nodes_shift_left(&(dst), &(src), (n), MAX_NUMNODES)
  199. static inline void __nodes_shift_left(nodemask_t *dstp,
  200. const nodemask_t *srcp, int n, int nbits)
  201. {
  202. bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
  203. }
  204. /* FIXME: better would be to fix all architectures to never return
  205. > MAX_NUMNODES, then the silly min_ts could be dropped. */
  206. #define first_node(src) __first_node(&(src))
  207. static inline int __first_node(const nodemask_t *srcp)
  208. {
  209. return min_t(int, MAX_NUMNODES, find_first_bit(srcp->bits, MAX_NUMNODES));
  210. }
  211. #define next_node(n, src) __next_node((n), &(src))
  212. static inline int __next_node(int n, const nodemask_t *srcp)
  213. {
  214. return min_t(int,MAX_NUMNODES,find_next_bit(srcp->bits, MAX_NUMNODES, n+1));
  215. }
  216. #define nodemask_of_node(node) \
  217. ({ \
  218. typeof(_unused_nodemask_arg_) m; \
  219. if (sizeof(m) == sizeof(unsigned long)) { \
  220. m.bits[0] = 1UL<<(node); \
  221. } else { \
  222. nodes_clear(m); \
  223. node_set((node), m); \
  224. } \
  225. m; \
  226. })
  227. #define first_unset_node(mask) __first_unset_node(&(mask))
  228. static inline int __first_unset_node(const nodemask_t *maskp)
  229. {
  230. return min_t(int,MAX_NUMNODES,
  231. find_first_zero_bit(maskp->bits, MAX_NUMNODES));
  232. }
  233. #define NODE_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(MAX_NUMNODES)
  234. #if MAX_NUMNODES <= BITS_PER_LONG
  235. #define NODE_MASK_ALL \
  236. ((nodemask_t) { { \
  237. [BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD \
  238. } })
  239. #else
  240. #define NODE_MASK_ALL \
  241. ((nodemask_t) { { \
  242. [0 ... BITS_TO_LONGS(MAX_NUMNODES)-2] = ~0UL, \
  243. [BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD \
  244. } })
  245. #endif
  246. #define NODE_MASK_NONE \
  247. ((nodemask_t) { { \
  248. [0 ... BITS_TO_LONGS(MAX_NUMNODES)-1] = 0UL \
  249. } })
  250. #define nodes_addr(src) ((src).bits)
  251. #define nodemask_scnprintf(buf, len, src) \
  252. __nodemask_scnprintf((buf), (len), &(src), MAX_NUMNODES)
  253. static inline int __nodemask_scnprintf(char *buf, int len,
  254. const nodemask_t *srcp, int nbits)
  255. {
  256. return bitmap_scnprintf(buf, len, srcp->bits, nbits);
  257. }
  258. #define nodemask_parse_user(ubuf, ulen, dst) \
  259. __nodemask_parse_user((ubuf), (ulen), &(dst), MAX_NUMNODES)
  260. static inline int __nodemask_parse_user(const char __user *buf, int len,
  261. nodemask_t *dstp, int nbits)
  262. {
  263. return bitmap_parse_user(buf, len, dstp->bits, nbits);
  264. }
  265. #define nodelist_scnprintf(buf, len, src) \
  266. __nodelist_scnprintf((buf), (len), &(src), MAX_NUMNODES)
  267. static inline int __nodelist_scnprintf(char *buf, int len,
  268. const nodemask_t *srcp, int nbits)
  269. {
  270. return bitmap_scnlistprintf(buf, len, srcp->bits, nbits);
  271. }
  272. #define nodelist_parse(buf, dst) __nodelist_parse((buf), &(dst), MAX_NUMNODES)
  273. static inline int __nodelist_parse(const char *buf, nodemask_t *dstp, int nbits)
  274. {
  275. return bitmap_parselist(buf, dstp->bits, nbits);
  276. }
  277. #define node_remap(oldbit, old, new) \
  278. __node_remap((oldbit), &(old), &(new), MAX_NUMNODES)
  279. static inline int __node_remap(int oldbit,
  280. const nodemask_t *oldp, const nodemask_t *newp, int nbits)
  281. {
  282. return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
  283. }
  284. #define nodes_remap(dst, src, old, new) \
  285. __nodes_remap(&(dst), &(src), &(old), &(new), MAX_NUMNODES)
  286. static inline void __nodes_remap(nodemask_t *dstp, const nodemask_t *srcp,
  287. const nodemask_t *oldp, const nodemask_t *newp, int nbits)
  288. {
  289. bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
  290. }
  291. #define nodes_onto(dst, orig, relmap) \
  292. __nodes_onto(&(dst), &(orig), &(relmap), MAX_NUMNODES)
  293. static inline void __nodes_onto(nodemask_t *dstp, const nodemask_t *origp,
  294. const nodemask_t *relmapp, int nbits)
  295. {
  296. bitmap_onto(dstp->bits, origp->bits, relmapp->bits, nbits);
  297. }
  298. #define nodes_fold(dst, orig, sz) \
  299. __nodes_fold(&(dst), &(orig), sz, MAX_NUMNODES)
  300. static inline void __nodes_fold(nodemask_t *dstp, const nodemask_t *origp,
  301. int sz, int nbits)
  302. {
  303. bitmap_fold(dstp->bits, origp->bits, sz, nbits);
  304. }
  305. #if MAX_NUMNODES > 1
  306. #define for_each_node_mask(node, mask) \
  307. for ((node) = first_node(mask); \
  308. (node) < MAX_NUMNODES; \
  309. (node) = next_node((node), (mask)))
  310. #else /* MAX_NUMNODES == 1 */
  311. #define for_each_node_mask(node, mask) \
  312. if (!nodes_empty(mask)) \
  313. for ((node) = 0; (node) < 1; (node)++)
  314. #endif /* MAX_NUMNODES */
  315. /*
  316. * Bitmasks that are kept for all the nodes.
  317. */
  318. enum node_states {
  319. N_POSSIBLE, /* The node could become online at some point */
  320. N_ONLINE, /* The node is online */
  321. N_NORMAL_MEMORY, /* The node has regular memory */
  322. #ifdef CONFIG_HIGHMEM
  323. N_HIGH_MEMORY, /* The node has regular or high memory */
  324. #else
  325. N_HIGH_MEMORY = N_NORMAL_MEMORY,
  326. #endif
  327. N_CPU, /* The node has one or more cpus */
  328. NR_NODE_STATES
  329. };
  330. /*
  331. * The following particular system nodemasks and operations
  332. * on them manage all possible and online nodes.
  333. */
  334. extern nodemask_t node_states[NR_NODE_STATES];
  335. #if MAX_NUMNODES > 1
  336. static inline int node_state(int node, enum node_states state)
  337. {
  338. return node_isset(node, node_states[state]);
  339. }
  340. static inline void node_set_state(int node, enum node_states state)
  341. {
  342. __node_set(node, &node_states[state]);
  343. }
  344. static inline void node_clear_state(int node, enum node_states state)
  345. {
  346. __node_clear(node, &node_states[state]);
  347. }
  348. static inline int num_node_state(enum node_states state)
  349. {
  350. return nodes_weight(node_states[state]);
  351. }
  352. #define for_each_node_state(__node, __state) \
  353. for_each_node_mask((__node), node_states[__state])
  354. #define first_online_node first_node(node_states[N_ONLINE])
  355. #define next_online_node(nid) next_node((nid), node_states[N_ONLINE])
  356. extern int nr_node_ids;
  357. #else
  358. static inline int node_state(int node, enum node_states state)
  359. {
  360. return node == 0;
  361. }
  362. static inline void node_set_state(int node, enum node_states state)
  363. {
  364. }
  365. static inline void node_clear_state(int node, enum node_states state)
  366. {
  367. }
  368. static inline int num_node_state(enum node_states state)
  369. {
  370. return 1;
  371. }
  372. #define for_each_node_state(node, __state) \
  373. for ( (node) = 0; (node) == 0; (node) = 1)
  374. #define first_online_node 0
  375. #define next_online_node(nid) (MAX_NUMNODES)
  376. #define nr_node_ids 1
  377. #endif
  378. #define node_online_map node_states[N_ONLINE]
  379. #define node_possible_map node_states[N_POSSIBLE]
  380. #define any_online_node(mask) \
  381. ({ \
  382. int node; \
  383. for_each_node_mask(node, (mask)) \
  384. if (node_online(node)) \
  385. break; \
  386. node; \
  387. })
  388. #define num_online_nodes() num_node_state(N_ONLINE)
  389. #define num_possible_nodes() num_node_state(N_POSSIBLE)
  390. #define node_online(node) node_state((node), N_ONLINE)
  391. #define node_possible(node) node_state((node), N_POSSIBLE)
  392. #define node_set_online(node) node_set_state((node), N_ONLINE)
  393. #define node_set_offline(node) node_clear_state((node), N_ONLINE)
  394. #define for_each_node(node) for_each_node_state(node, N_POSSIBLE)
  395. #define for_each_online_node(node) for_each_node_state(node, N_ONLINE)
  396. #endif /* __LINUX_NODEMASK_H */