nodemask.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. *
  17. * The available nodemask operations are:
  18. *
  19. * void node_set(node, mask) turn on bit 'node' in mask
  20. * void node_clear(node, mask) turn off bit 'node' in mask
  21. * void nodes_setall(mask) set all bits
  22. * void nodes_clear(mask) clear all bits
  23. * int node_isset(node, mask) true iff bit 'node' set in mask
  24. * int node_test_and_set(node, mask) test and set bit 'node' in mask
  25. *
  26. * void nodes_and(dst, src1, src2) dst = src1 & src2 [intersection]
  27. * void nodes_or(dst, src1, src2) dst = src1 | src2 [union]
  28. * void nodes_xor(dst, src1, src2) dst = src1 ^ src2
  29. * void nodes_andnot(dst, src1, src2) dst = src1 & ~src2
  30. * void nodes_complement(dst, src) dst = ~src
  31. *
  32. * int nodes_equal(mask1, mask2) Does mask1 == mask2?
  33. * int nodes_intersects(mask1, mask2) Do mask1 and mask2 intersect?
  34. * int nodes_subset(mask1, mask2) Is mask1 a subset of mask2?
  35. * int nodes_empty(mask) Is mask empty (no bits sets)?
  36. * int nodes_full(mask) Is mask full (all bits sets)?
  37. * int nodes_weight(mask) Hamming weight - number of set bits
  38. *
  39. * void nodes_shift_right(dst, src, n) Shift right
  40. * void nodes_shift_left(dst, src, n) Shift left
  41. *
  42. * int first_node(mask) Number lowest set bit, or MAX_NUMNODES
  43. * int next_node(node, mask) Next node past 'node', or MAX_NUMNODES
  44. * int first_unset_node(mask) First node not set in mask, or
  45. * MAX_NUMNODES.
  46. *
  47. * nodemask_t nodemask_of_node(node) Return nodemask with bit 'node' set
  48. * NODE_MASK_ALL Initializer - all bits set
  49. * NODE_MASK_NONE Initializer - no bits set
  50. * unsigned long *nodes_addr(mask) Array of unsigned long's in mask
  51. *
  52. * int nodemask_scnprintf(buf, len, mask) Format nodemask for printing
  53. * int nodemask_parse_user(ubuf, ulen, mask) Parse ascii string as nodemask
  54. * int nodelist_scnprintf(buf, len, mask) Format nodemask as list for printing
  55. * int nodelist_parse(buf, map) Parse ascii string as nodelist
  56. * int node_remap(oldbit, old, new) newbit = map(old, new)(oldbit)
  57. * int nodes_remap(dst, src, old, new) *dst = map(old, new)(dst)
  58. *
  59. * for_each_node_mask(node, mask) for-loop node over mask
  60. *
  61. * int num_online_nodes() Number of online Nodes
  62. * int num_possible_nodes() Number of all possible Nodes
  63. *
  64. * int node_online(node) Is some node online?
  65. * int node_possible(node) Is some node possible?
  66. *
  67. * int any_online_node(mask) First online node in mask
  68. *
  69. * node_set_online(node) set bit 'node' in node_online_map
  70. * node_set_offline(node) clear bit 'node' in node_online_map
  71. *
  72. * for_each_node(node) for-loop node over node_possible_map
  73. * for_each_online_node(node) for-loop node over node_online_map
  74. *
  75. * Subtlety:
  76. * 1) The 'type-checked' form of node_isset() causes gcc (3.3.2, anyway)
  77. * to generate slightly worse code. So use a simple one-line #define
  78. * for node_isset(), instead of wrapping an inline inside a macro, the
  79. * way we do the other calls.
  80. */
  81. #include <linux/kernel.h>
  82. #include <linux/threads.h>
  83. #include <linux/bitmap.h>
  84. #include <linux/numa.h>
  85. typedef struct { DECLARE_BITMAP(bits, MAX_NUMNODES); } nodemask_t;
  86. extern nodemask_t _unused_nodemask_arg_;
  87. #define node_set(node, dst) __node_set((node), &(dst))
  88. static inline void __node_set(int node, volatile nodemask_t *dstp)
  89. {
  90. set_bit(node, dstp->bits);
  91. }
  92. #define node_clear(node, dst) __node_clear((node), &(dst))
  93. static inline void __node_clear(int node, volatile nodemask_t *dstp)
  94. {
  95. clear_bit(node, dstp->bits);
  96. }
  97. #define nodes_setall(dst) __nodes_setall(&(dst), MAX_NUMNODES)
  98. static inline void __nodes_setall(nodemask_t *dstp, int nbits)
  99. {
  100. bitmap_fill(dstp->bits, nbits);
  101. }
  102. #define nodes_clear(dst) __nodes_clear(&(dst), MAX_NUMNODES)
  103. static inline void __nodes_clear(nodemask_t *dstp, int nbits)
  104. {
  105. bitmap_zero(dstp->bits, nbits);
  106. }
  107. /* No static inline type checking - see Subtlety (1) above. */
  108. #define node_isset(node, nodemask) test_bit((node), (nodemask).bits)
  109. #define node_test_and_set(node, nodemask) \
  110. __node_test_and_set((node), &(nodemask))
  111. static inline int __node_test_and_set(int node, nodemask_t *addr)
  112. {
  113. return test_and_set_bit(node, addr->bits);
  114. }
  115. #define nodes_and(dst, src1, src2) \
  116. __nodes_and(&(dst), &(src1), &(src2), MAX_NUMNODES)
  117. static inline void __nodes_and(nodemask_t *dstp, const nodemask_t *src1p,
  118. const nodemask_t *src2p, int nbits)
  119. {
  120. bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
  121. }
  122. #define nodes_or(dst, src1, src2) \
  123. __nodes_or(&(dst), &(src1), &(src2), MAX_NUMNODES)
  124. static inline void __nodes_or(nodemask_t *dstp, const nodemask_t *src1p,
  125. const nodemask_t *src2p, int nbits)
  126. {
  127. bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
  128. }
  129. #define nodes_xor(dst, src1, src2) \
  130. __nodes_xor(&(dst), &(src1), &(src2), MAX_NUMNODES)
  131. static inline void __nodes_xor(nodemask_t *dstp, const nodemask_t *src1p,
  132. const nodemask_t *src2p, int nbits)
  133. {
  134. bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
  135. }
  136. #define nodes_andnot(dst, src1, src2) \
  137. __nodes_andnot(&(dst), &(src1), &(src2), MAX_NUMNODES)
  138. static inline void __nodes_andnot(nodemask_t *dstp, const nodemask_t *src1p,
  139. const nodemask_t *src2p, int nbits)
  140. {
  141. bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
  142. }
  143. #define nodes_complement(dst, src) \
  144. __nodes_complement(&(dst), &(src), MAX_NUMNODES)
  145. static inline void __nodes_complement(nodemask_t *dstp,
  146. const nodemask_t *srcp, int nbits)
  147. {
  148. bitmap_complement(dstp->bits, srcp->bits, nbits);
  149. }
  150. #define nodes_equal(src1, src2) \
  151. __nodes_equal(&(src1), &(src2), MAX_NUMNODES)
  152. static inline int __nodes_equal(const nodemask_t *src1p,
  153. const nodemask_t *src2p, int nbits)
  154. {
  155. return bitmap_equal(src1p->bits, src2p->bits, nbits);
  156. }
  157. #define nodes_intersects(src1, src2) \
  158. __nodes_intersects(&(src1), &(src2), MAX_NUMNODES)
  159. static inline int __nodes_intersects(const nodemask_t *src1p,
  160. const nodemask_t *src2p, int nbits)
  161. {
  162. return bitmap_intersects(src1p->bits, src2p->bits, nbits);
  163. }
  164. #define nodes_subset(src1, src2) \
  165. __nodes_subset(&(src1), &(src2), MAX_NUMNODES)
  166. static inline int __nodes_subset(const nodemask_t *src1p,
  167. const nodemask_t *src2p, int nbits)
  168. {
  169. return bitmap_subset(src1p->bits, src2p->bits, nbits);
  170. }
  171. #define nodes_empty(src) __nodes_empty(&(src), MAX_NUMNODES)
  172. static inline int __nodes_empty(const nodemask_t *srcp, int nbits)
  173. {
  174. return bitmap_empty(srcp->bits, nbits);
  175. }
  176. #define nodes_full(nodemask) __nodes_full(&(nodemask), MAX_NUMNODES)
  177. static inline int __nodes_full(const nodemask_t *srcp, int nbits)
  178. {
  179. return bitmap_full(srcp->bits, nbits);
  180. }
  181. #define nodes_weight(nodemask) __nodes_weight(&(nodemask), MAX_NUMNODES)
  182. static inline int __nodes_weight(const nodemask_t *srcp, int nbits)
  183. {
  184. return bitmap_weight(srcp->bits, nbits);
  185. }
  186. #define nodes_shift_right(dst, src, n) \
  187. __nodes_shift_right(&(dst), &(src), (n), MAX_NUMNODES)
  188. static inline void __nodes_shift_right(nodemask_t *dstp,
  189. const nodemask_t *srcp, int n, int nbits)
  190. {
  191. bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
  192. }
  193. #define nodes_shift_left(dst, src, n) \
  194. __nodes_shift_left(&(dst), &(src), (n), MAX_NUMNODES)
  195. static inline void __nodes_shift_left(nodemask_t *dstp,
  196. const nodemask_t *srcp, int n, int nbits)
  197. {
  198. bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
  199. }
  200. /* FIXME: better would be to fix all architectures to never return
  201. > MAX_NUMNODES, then the silly min_ts could be dropped. */
  202. #define first_node(src) __first_node(&(src))
  203. static inline int __first_node(const nodemask_t *srcp)
  204. {
  205. return min_t(int, MAX_NUMNODES, find_first_bit(srcp->bits, MAX_NUMNODES));
  206. }
  207. #define next_node(n, src) __next_node((n), &(src))
  208. static inline int __next_node(int n, const nodemask_t *srcp)
  209. {
  210. return min_t(int,MAX_NUMNODES,find_next_bit(srcp->bits, MAX_NUMNODES, n+1));
  211. }
  212. #define nodemask_of_node(node) \
  213. ({ \
  214. typeof(_unused_nodemask_arg_) m; \
  215. if (sizeof(m) == sizeof(unsigned long)) { \
  216. m.bits[0] = 1UL<<(node); \
  217. } else { \
  218. nodes_clear(m); \
  219. node_set((node), m); \
  220. } \
  221. m; \
  222. })
  223. #define first_unset_node(mask) __first_unset_node(&(mask))
  224. static inline int __first_unset_node(const nodemask_t *maskp)
  225. {
  226. return min_t(int,MAX_NUMNODES,
  227. find_first_zero_bit(maskp->bits, MAX_NUMNODES));
  228. }
  229. #define NODE_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(MAX_NUMNODES)
  230. #if MAX_NUMNODES <= BITS_PER_LONG
  231. #define NODE_MASK_ALL \
  232. ((nodemask_t) { { \
  233. [BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD \
  234. } })
  235. #else
  236. #define NODE_MASK_ALL \
  237. ((nodemask_t) { { \
  238. [0 ... BITS_TO_LONGS(MAX_NUMNODES)-2] = ~0UL, \
  239. [BITS_TO_LONGS(MAX_NUMNODES)-1] = NODE_MASK_LAST_WORD \
  240. } })
  241. #endif
  242. #define NODE_MASK_NONE \
  243. ((nodemask_t) { { \
  244. [0 ... BITS_TO_LONGS(MAX_NUMNODES)-1] = 0UL \
  245. } })
  246. #define nodes_addr(src) ((src).bits)
  247. #define nodemask_scnprintf(buf, len, src) \
  248. __nodemask_scnprintf((buf), (len), &(src), MAX_NUMNODES)
  249. static inline int __nodemask_scnprintf(char *buf, int len,
  250. const nodemask_t *srcp, int nbits)
  251. {
  252. return bitmap_scnprintf(buf, len, srcp->bits, nbits);
  253. }
  254. #define nodemask_parse_user(ubuf, ulen, dst) \
  255. __nodemask_parse_user((ubuf), (ulen), &(dst), MAX_NUMNODES)
  256. static inline int __nodemask_parse_user(const char __user *buf, int len,
  257. nodemask_t *dstp, int nbits)
  258. {
  259. return bitmap_parse_user(buf, len, dstp->bits, nbits);
  260. }
  261. #define nodelist_scnprintf(buf, len, src) \
  262. __nodelist_scnprintf((buf), (len), &(src), MAX_NUMNODES)
  263. static inline int __nodelist_scnprintf(char *buf, int len,
  264. const nodemask_t *srcp, int nbits)
  265. {
  266. return bitmap_scnlistprintf(buf, len, srcp->bits, nbits);
  267. }
  268. #define nodelist_parse(buf, dst) __nodelist_parse((buf), &(dst), MAX_NUMNODES)
  269. static inline int __nodelist_parse(const char *buf, nodemask_t *dstp, int nbits)
  270. {
  271. return bitmap_parselist(buf, dstp->bits, nbits);
  272. }
  273. #define node_remap(oldbit, old, new) \
  274. __node_remap((oldbit), &(old), &(new), MAX_NUMNODES)
  275. static inline int __node_remap(int oldbit,
  276. const nodemask_t *oldp, const nodemask_t *newp, int nbits)
  277. {
  278. return bitmap_bitremap(oldbit, oldp->bits, newp->bits, nbits);
  279. }
  280. #define nodes_remap(dst, src, old, new) \
  281. __nodes_remap(&(dst), &(src), &(old), &(new), MAX_NUMNODES)
  282. static inline void __nodes_remap(nodemask_t *dstp, const nodemask_t *srcp,
  283. const nodemask_t *oldp, const nodemask_t *newp, int nbits)
  284. {
  285. bitmap_remap(dstp->bits, srcp->bits, oldp->bits, newp->bits, nbits);
  286. }
  287. #if MAX_NUMNODES > 1
  288. #define for_each_node_mask(node, mask) \
  289. for ((node) = first_node(mask); \
  290. (node) < MAX_NUMNODES; \
  291. (node) = next_node((node), (mask)))
  292. #else /* MAX_NUMNODES == 1 */
  293. #define for_each_node_mask(node, mask) \
  294. if (!nodes_empty(mask)) \
  295. for ((node) = 0; (node) < 1; (node)++)
  296. #endif /* MAX_NUMNODES */
  297. /*
  298. * The following particular system nodemasks and operations
  299. * on them manage all possible and online nodes.
  300. */
  301. extern nodemask_t node_online_map;
  302. extern nodemask_t node_possible_map;
  303. #if MAX_NUMNODES > 1
  304. #define num_online_nodes() nodes_weight(node_online_map)
  305. #define num_possible_nodes() nodes_weight(node_possible_map)
  306. #define node_online(node) node_isset((node), node_online_map)
  307. #define node_possible(node) node_isset((node), node_possible_map)
  308. #define first_online_node first_node(node_online_map)
  309. #define next_online_node(nid) next_node((nid), node_online_map)
  310. extern int nr_node_ids;
  311. #else
  312. #define num_online_nodes() 1
  313. #define num_possible_nodes() 1
  314. #define node_online(node) ((node) == 0)
  315. #define node_possible(node) ((node) == 0)
  316. #define first_online_node 0
  317. #define next_online_node(nid) (MAX_NUMNODES)
  318. #define nr_node_ids 1
  319. #endif
  320. #define any_online_node(mask) \
  321. ({ \
  322. int node; \
  323. for_each_node_mask(node, (mask)) \
  324. if (node_online(node)) \
  325. break; \
  326. node; \
  327. })
  328. #define node_set_online(node) set_bit((node), node_online_map.bits)
  329. #define node_set_offline(node) clear_bit((node), node_online_map.bits)
  330. #define for_each_node(node) for_each_node_mask((node), node_possible_map)
  331. #define for_each_online_node(node) for_each_node_mask((node), node_online_map)
  332. #endif /* __LINUX_NODEMASK_H */