sbitmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright (C) 2016 Facebook
  3. * Copyright (C) 2013-2014 Jens Axboe
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public
  7. * License v2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/sched.h>
  18. #include <linux/random.h>
  19. #include <linux/sbitmap.h>
  20. #include <linux/seq_file.h>
  21. int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
  22. gfp_t flags, int node)
  23. {
  24. unsigned int bits_per_word;
  25. unsigned int i;
  26. if (shift < 0) {
  27. shift = ilog2(BITS_PER_LONG);
  28. /*
  29. * If the bitmap is small, shrink the number of bits per word so
  30. * we spread over a few cachelines, at least. If less than 4
  31. * bits, just forget about it, it's not going to work optimally
  32. * anyway.
  33. */
  34. if (depth >= 4) {
  35. while ((4U << shift) > depth)
  36. shift--;
  37. }
  38. }
  39. bits_per_word = 1U << shift;
  40. if (bits_per_word > BITS_PER_LONG)
  41. return -EINVAL;
  42. sb->shift = shift;
  43. sb->depth = depth;
  44. sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
  45. if (depth == 0) {
  46. sb->map = NULL;
  47. return 0;
  48. }
  49. sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);
  50. if (!sb->map)
  51. return -ENOMEM;
  52. for (i = 0; i < sb->map_nr; i++) {
  53. sb->map[i].depth = min(depth, bits_per_word);
  54. depth -= sb->map[i].depth;
  55. }
  56. return 0;
  57. }
  58. EXPORT_SYMBOL_GPL(sbitmap_init_node);
  59. void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
  60. {
  61. unsigned int bits_per_word = 1U << sb->shift;
  62. unsigned int i;
  63. sb->depth = depth;
  64. sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
  65. for (i = 0; i < sb->map_nr; i++) {
  66. sb->map[i].depth = min(depth, bits_per_word);
  67. depth -= sb->map[i].depth;
  68. }
  69. }
  70. EXPORT_SYMBOL_GPL(sbitmap_resize);
  71. static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
  72. unsigned int hint, bool wrap)
  73. {
  74. unsigned int orig_hint = hint;
  75. int nr;
  76. while (1) {
  77. nr = find_next_zero_bit(word, depth, hint);
  78. if (unlikely(nr >= depth)) {
  79. /*
  80. * We started with an offset, and we didn't reset the
  81. * offset to 0 in a failure case, so start from 0 to
  82. * exhaust the map.
  83. */
  84. if (orig_hint && hint && wrap) {
  85. hint = orig_hint = 0;
  86. continue;
  87. }
  88. return -1;
  89. }
  90. if (!test_and_set_bit_lock(nr, word))
  91. break;
  92. hint = nr + 1;
  93. if (hint >= depth - 1)
  94. hint = 0;
  95. }
  96. return nr;
  97. }
  98. int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
  99. {
  100. unsigned int i, index;
  101. int nr = -1;
  102. index = SB_NR_TO_INDEX(sb, alloc_hint);
  103. for (i = 0; i < sb->map_nr; i++) {
  104. nr = __sbitmap_get_word(&sb->map[index].word,
  105. sb->map[index].depth,
  106. SB_NR_TO_BIT(sb, alloc_hint),
  107. !round_robin);
  108. if (nr != -1) {
  109. nr += index << sb->shift;
  110. break;
  111. }
  112. /* Jump to next index. */
  113. index++;
  114. alloc_hint = index << sb->shift;
  115. if (index >= sb->map_nr) {
  116. index = 0;
  117. alloc_hint = 0;
  118. }
  119. }
  120. return nr;
  121. }
  122. EXPORT_SYMBOL_GPL(sbitmap_get);
  123. int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
  124. unsigned long shallow_depth)
  125. {
  126. unsigned int i, index;
  127. int nr = -1;
  128. index = SB_NR_TO_INDEX(sb, alloc_hint);
  129. for (i = 0; i < sb->map_nr; i++) {
  130. nr = __sbitmap_get_word(&sb->map[index].word,
  131. min(sb->map[index].depth, shallow_depth),
  132. SB_NR_TO_BIT(sb, alloc_hint), true);
  133. if (nr != -1) {
  134. nr += index << sb->shift;
  135. break;
  136. }
  137. /* Jump to next index. */
  138. index++;
  139. alloc_hint = index << sb->shift;
  140. if (index >= sb->map_nr) {
  141. index = 0;
  142. alloc_hint = 0;
  143. }
  144. }
  145. return nr;
  146. }
  147. EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
  148. bool sbitmap_any_bit_set(const struct sbitmap *sb)
  149. {
  150. unsigned int i;
  151. for (i = 0; i < sb->map_nr; i++) {
  152. if (sb->map[i].word)
  153. return true;
  154. }
  155. return false;
  156. }
  157. EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
  158. bool sbitmap_any_bit_clear(const struct sbitmap *sb)
  159. {
  160. unsigned int i;
  161. for (i = 0; i < sb->map_nr; i++) {
  162. const struct sbitmap_word *word = &sb->map[i];
  163. unsigned long ret;
  164. ret = find_first_zero_bit(&word->word, word->depth);
  165. if (ret < word->depth)
  166. return true;
  167. }
  168. return false;
  169. }
  170. EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
  171. unsigned int sbitmap_weight(const struct sbitmap *sb)
  172. {
  173. unsigned int i, weight = 0;
  174. for (i = 0; i < sb->map_nr; i++) {
  175. const struct sbitmap_word *word = &sb->map[i];
  176. weight += bitmap_weight(&word->word, word->depth);
  177. }
  178. return weight;
  179. }
  180. EXPORT_SYMBOL_GPL(sbitmap_weight);
  181. void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
  182. {
  183. seq_printf(m, "depth=%u\n", sb->depth);
  184. seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
  185. seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
  186. seq_printf(m, "map_nr=%u\n", sb->map_nr);
  187. }
  188. EXPORT_SYMBOL_GPL(sbitmap_show);
  189. static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
  190. {
  191. if ((offset & 0xf) == 0) {
  192. if (offset != 0)
  193. seq_putc(m, '\n');
  194. seq_printf(m, "%08x:", offset);
  195. }
  196. if ((offset & 0x1) == 0)
  197. seq_putc(m, ' ');
  198. seq_printf(m, "%02x", byte);
  199. }
  200. void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
  201. {
  202. u8 byte = 0;
  203. unsigned int byte_bits = 0;
  204. unsigned int offset = 0;
  205. int i;
  206. for (i = 0; i < sb->map_nr; i++) {
  207. unsigned long word = READ_ONCE(sb->map[i].word);
  208. unsigned int word_bits = READ_ONCE(sb->map[i].depth);
  209. while (word_bits > 0) {
  210. unsigned int bits = min(8 - byte_bits, word_bits);
  211. byte |= (word & (BIT(bits) - 1)) << byte_bits;
  212. byte_bits += bits;
  213. if (byte_bits == 8) {
  214. emit_byte(m, offset, byte);
  215. byte = 0;
  216. byte_bits = 0;
  217. offset++;
  218. }
  219. word >>= bits;
  220. word_bits -= bits;
  221. }
  222. }
  223. if (byte_bits) {
  224. emit_byte(m, offset, byte);
  225. offset++;
  226. }
  227. if (offset)
  228. seq_putc(m, '\n');
  229. }
  230. EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
  231. static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
  232. unsigned int depth)
  233. {
  234. unsigned int wake_batch;
  235. unsigned int shallow_depth;
  236. /*
  237. * For each batch, we wake up one queue. We need to make sure that our
  238. * batch size is small enough that the full depth of the bitmap,
  239. * potentially limited by a shallow depth, is enough to wake up all of
  240. * the queues.
  241. *
  242. * Each full word of the bitmap has bits_per_word bits, and there might
  243. * be a partial word. There are depth / bits_per_word full words and
  244. * depth % bits_per_word bits left over. In bitwise arithmetic:
  245. *
  246. * bits_per_word = 1 << shift
  247. * depth / bits_per_word = depth >> shift
  248. * depth % bits_per_word = depth & ((1 << shift) - 1)
  249. *
  250. * Each word can be limited to sbq->min_shallow_depth bits.
  251. */
  252. shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
  253. depth = ((depth >> sbq->sb.shift) * shallow_depth +
  254. min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
  255. wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
  256. SBQ_WAKE_BATCH);
  257. return wake_batch;
  258. }
  259. int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
  260. int shift, bool round_robin, gfp_t flags, int node)
  261. {
  262. int ret;
  263. int i;
  264. ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
  265. if (ret)
  266. return ret;
  267. sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
  268. if (!sbq->alloc_hint) {
  269. sbitmap_free(&sbq->sb);
  270. return -ENOMEM;
  271. }
  272. if (depth && !round_robin) {
  273. for_each_possible_cpu(i)
  274. *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
  275. }
  276. sbq->min_shallow_depth = UINT_MAX;
  277. sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
  278. atomic_set(&sbq->wake_index, 0);
  279. sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
  280. if (!sbq->ws) {
  281. free_percpu(sbq->alloc_hint);
  282. sbitmap_free(&sbq->sb);
  283. return -ENOMEM;
  284. }
  285. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  286. init_waitqueue_head(&sbq->ws[i].wait);
  287. atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
  288. }
  289. sbq->round_robin = round_robin;
  290. return 0;
  291. }
  292. EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
  293. static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
  294. unsigned int depth)
  295. {
  296. unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
  297. int i;
  298. if (sbq->wake_batch != wake_batch) {
  299. WRITE_ONCE(sbq->wake_batch, wake_batch);
  300. /*
  301. * Pairs with the memory barrier in sbitmap_queue_wake_up()
  302. * to ensure that the batch size is updated before the wait
  303. * counts.
  304. */
  305. smp_mb();
  306. for (i = 0; i < SBQ_WAIT_QUEUES; i++)
  307. atomic_set(&sbq->ws[i].wait_cnt, 1);
  308. }
  309. }
  310. void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
  311. {
  312. sbitmap_queue_update_wake_batch(sbq, depth);
  313. sbitmap_resize(&sbq->sb, depth);
  314. }
  315. EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
  316. int __sbitmap_queue_get(struct sbitmap_queue *sbq)
  317. {
  318. unsigned int hint, depth;
  319. int nr;
  320. hint = this_cpu_read(*sbq->alloc_hint);
  321. depth = READ_ONCE(sbq->sb.depth);
  322. if (unlikely(hint >= depth)) {
  323. hint = depth ? prandom_u32() % depth : 0;
  324. this_cpu_write(*sbq->alloc_hint, hint);
  325. }
  326. nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
  327. if (nr == -1) {
  328. /* If the map is full, a hint won't do us much good. */
  329. this_cpu_write(*sbq->alloc_hint, 0);
  330. } else if (nr == hint || unlikely(sbq->round_robin)) {
  331. /* Only update the hint if we used it. */
  332. hint = nr + 1;
  333. if (hint >= depth - 1)
  334. hint = 0;
  335. this_cpu_write(*sbq->alloc_hint, hint);
  336. }
  337. return nr;
  338. }
  339. EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
  340. int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
  341. unsigned int shallow_depth)
  342. {
  343. unsigned int hint, depth;
  344. int nr;
  345. WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
  346. hint = this_cpu_read(*sbq->alloc_hint);
  347. depth = READ_ONCE(sbq->sb.depth);
  348. if (unlikely(hint >= depth)) {
  349. hint = depth ? prandom_u32() % depth : 0;
  350. this_cpu_write(*sbq->alloc_hint, hint);
  351. }
  352. nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
  353. if (nr == -1) {
  354. /* If the map is full, a hint won't do us much good. */
  355. this_cpu_write(*sbq->alloc_hint, 0);
  356. } else if (nr == hint || unlikely(sbq->round_robin)) {
  357. /* Only update the hint if we used it. */
  358. hint = nr + 1;
  359. if (hint >= depth - 1)
  360. hint = 0;
  361. this_cpu_write(*sbq->alloc_hint, hint);
  362. }
  363. return nr;
  364. }
  365. EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
  366. void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
  367. unsigned int min_shallow_depth)
  368. {
  369. sbq->min_shallow_depth = min_shallow_depth;
  370. sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
  371. }
  372. EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
  373. static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
  374. {
  375. int i, wake_index;
  376. wake_index = atomic_read(&sbq->wake_index);
  377. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  378. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  379. if (waitqueue_active(&ws->wait)) {
  380. int o = atomic_read(&sbq->wake_index);
  381. if (wake_index != o)
  382. atomic_cmpxchg(&sbq->wake_index, o, wake_index);
  383. return ws;
  384. }
  385. wake_index = sbq_index_inc(wake_index);
  386. }
  387. return NULL;
  388. }
  389. static bool __sbq_wake_up(struct sbitmap_queue *sbq)
  390. {
  391. struct sbq_wait_state *ws;
  392. unsigned int wake_batch;
  393. int wait_cnt;
  394. ws = sbq_wake_ptr(sbq);
  395. if (!ws)
  396. return false;
  397. wait_cnt = atomic_dec_return(&ws->wait_cnt);
  398. if (wait_cnt <= 0) {
  399. int ret;
  400. wake_batch = READ_ONCE(sbq->wake_batch);
  401. /*
  402. * Pairs with the memory barrier in sbitmap_queue_resize() to
  403. * ensure that we see the batch size update before the wait
  404. * count is reset.
  405. */
  406. smp_mb__before_atomic();
  407. /*
  408. * For concurrent callers of this, the one that failed the
  409. * atomic_cmpxhcg() race should call this function again
  410. * to wakeup a new batch on a different 'ws'.
  411. */
  412. ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch);
  413. if (ret == wait_cnt) {
  414. sbq_index_atomic_inc(&sbq->wake_index);
  415. wake_up_nr(&ws->wait, wake_batch);
  416. return false;
  417. }
  418. return true;
  419. }
  420. return false;
  421. }
  422. void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)
  423. {
  424. while (__sbq_wake_up(sbq))
  425. ;
  426. }
  427. EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
  428. void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
  429. unsigned int cpu)
  430. {
  431. sbitmap_clear_bit_unlock(&sbq->sb, nr);
  432. /*
  433. * Pairs with the memory barrier in set_current_state() to ensure the
  434. * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
  435. * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
  436. * waiter. See the comment on waitqueue_active().
  437. */
  438. smp_mb__after_atomic();
  439. sbitmap_queue_wake_up(sbq);
  440. if (likely(!sbq->round_robin && nr < sbq->sb.depth))
  441. *per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
  442. }
  443. EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
  444. void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
  445. {
  446. int i, wake_index;
  447. /*
  448. * Pairs with the memory barrier in set_current_state() like in
  449. * sbitmap_queue_wake_up().
  450. */
  451. smp_mb();
  452. wake_index = atomic_read(&sbq->wake_index);
  453. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  454. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  455. if (waitqueue_active(&ws->wait))
  456. wake_up(&ws->wait);
  457. wake_index = sbq_index_inc(wake_index);
  458. }
  459. }
  460. EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
  461. void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
  462. {
  463. bool first;
  464. int i;
  465. sbitmap_show(&sbq->sb, m);
  466. seq_puts(m, "alloc_hint={");
  467. first = true;
  468. for_each_possible_cpu(i) {
  469. if (!first)
  470. seq_puts(m, ", ");
  471. first = false;
  472. seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
  473. }
  474. seq_puts(m, "}\n");
  475. seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
  476. seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
  477. seq_puts(m, "ws={\n");
  478. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  479. struct sbq_wait_state *ws = &sbq->ws[i];
  480. seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
  481. atomic_read(&ws->wait_cnt),
  482. waitqueue_active(&ws->wait) ? "active" : "inactive");
  483. }
  484. seq_puts(m, "}\n");
  485. seq_printf(m, "round_robin=%d\n", sbq->round_robin);
  486. seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
  487. }
  488. EXPORT_SYMBOL_GPL(sbitmap_queue_show);