sbitmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Facebook
  4. * Copyright (C) 2013-2014 Jens Axboe
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/random.h>
  8. #include <linux/sbitmap.h>
  9. #include <linux/seq_file.h>
  10. static int init_alloc_hint(struct sbitmap *sb, gfp_t flags)
  11. {
  12. unsigned depth = sb->depth;
  13. sb->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
  14. if (!sb->alloc_hint)
  15. return -ENOMEM;
  16. if (depth && !sb->round_robin) {
  17. int i;
  18. for_each_possible_cpu(i)
  19. *per_cpu_ptr(sb->alloc_hint, i) = get_random_u32_below(depth);
  20. }
  21. return 0;
  22. }
  23. static inline unsigned update_alloc_hint_before_get(struct sbitmap *sb,
  24. unsigned int depth)
  25. {
  26. unsigned hint;
  27. hint = this_cpu_read(*sb->alloc_hint);
  28. if (unlikely(hint >= depth)) {
  29. hint = depth ? get_random_u32_below(depth) : 0;
  30. this_cpu_write(*sb->alloc_hint, hint);
  31. }
  32. return hint;
  33. }
  34. static inline void update_alloc_hint_after_get(struct sbitmap *sb,
  35. unsigned int depth,
  36. unsigned int hint,
  37. unsigned int nr)
  38. {
  39. if (nr == -1) {
  40. /* If the map is full, a hint won't do us much good. */
  41. this_cpu_write(*sb->alloc_hint, 0);
  42. } else if (nr == hint || unlikely(sb->round_robin)) {
  43. /* Only update the hint if we used it. */
  44. hint = nr + 1;
  45. if (hint >= depth - 1)
  46. hint = 0;
  47. this_cpu_write(*sb->alloc_hint, hint);
  48. }
  49. }
  50. /*
  51. * See if we have deferred clears that we can batch move
  52. */
  53. static inline bool sbitmap_deferred_clear(struct sbitmap_word *map,
  54. unsigned int depth, unsigned int alloc_hint, bool wrap)
  55. {
  56. unsigned long mask, word_mask;
  57. guard(raw_spinlock_irqsave)(&map->swap_lock);
  58. if (!map->cleared) {
  59. if (depth == 0)
  60. return false;
  61. word_mask = (~0UL) >> (BITS_PER_LONG - depth);
  62. /*
  63. * The current behavior is to always retry after moving
  64. * ->cleared to word, and we change it to retry in case
  65. * of any free bits. To avoid an infinite loop, we need
  66. * to take wrap & alloc_hint into account, otherwise a
  67. * soft lockup may occur.
  68. */
  69. if (!wrap && alloc_hint)
  70. word_mask &= ~((1UL << alloc_hint) - 1);
  71. return (READ_ONCE(map->word) & word_mask) != word_mask;
  72. }
  73. /*
  74. * First get a stable cleared mask, setting the old mask to 0.
  75. */
  76. mask = xchg(&map->cleared, 0);
  77. /*
  78. * Now clear the masked bits in our free word
  79. */
  80. atomic_long_andnot(mask, (atomic_long_t *)&map->word);
  81. BUILD_BUG_ON(sizeof(atomic_long_t) != sizeof(map->word));
  82. return true;
  83. }
  84. int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
  85. gfp_t flags, int node, bool round_robin,
  86. bool alloc_hint)
  87. {
  88. unsigned int bits_per_word;
  89. int i;
  90. if (shift < 0)
  91. shift = sbitmap_calculate_shift(depth);
  92. bits_per_word = 1U << shift;
  93. if (bits_per_word > BITS_PER_LONG)
  94. return -EINVAL;
  95. sb->shift = shift;
  96. sb->depth = depth;
  97. sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
  98. sb->round_robin = round_robin;
  99. if (depth == 0) {
  100. sb->map = NULL;
  101. return 0;
  102. }
  103. if (alloc_hint) {
  104. if (init_alloc_hint(sb, flags))
  105. return -ENOMEM;
  106. } else {
  107. sb->alloc_hint = NULL;
  108. }
  109. sb->map = kvzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
  110. if (!sb->map) {
  111. free_percpu(sb->alloc_hint);
  112. return -ENOMEM;
  113. }
  114. for (i = 0; i < sb->map_nr; i++)
  115. raw_spin_lock_init(&sb->map[i].swap_lock);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL_GPL(sbitmap_init_node);
  119. void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
  120. {
  121. unsigned int bits_per_word = 1U << sb->shift;
  122. unsigned int i;
  123. for (i = 0; i < sb->map_nr; i++)
  124. sbitmap_deferred_clear(&sb->map[i], 0, 0, 0);
  125. sb->depth = depth;
  126. sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
  127. }
  128. EXPORT_SYMBOL_GPL(sbitmap_resize);
  129. static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
  130. unsigned int hint, bool wrap)
  131. {
  132. int nr;
  133. /* don't wrap if starting from 0 */
  134. wrap = wrap && hint;
  135. while (1) {
  136. nr = find_next_zero_bit(word, depth, hint);
  137. if (unlikely(nr >= depth)) {
  138. /*
  139. * We started with an offset, and we didn't reset the
  140. * offset to 0 in a failure case, so start from 0 to
  141. * exhaust the map.
  142. */
  143. if (hint && wrap) {
  144. hint = 0;
  145. continue;
  146. }
  147. return -1;
  148. }
  149. if (!test_and_set_bit_lock(nr, word))
  150. break;
  151. hint = nr + 1;
  152. if (hint >= depth - 1)
  153. hint = 0;
  154. }
  155. return nr;
  156. }
  157. static int sbitmap_find_bit_in_word(struct sbitmap_word *map,
  158. unsigned int depth,
  159. unsigned int alloc_hint,
  160. bool wrap)
  161. {
  162. int nr;
  163. do {
  164. nr = __sbitmap_get_word(&map->word, depth,
  165. alloc_hint, wrap);
  166. if (nr != -1)
  167. break;
  168. if (!sbitmap_deferred_clear(map, depth, alloc_hint, wrap))
  169. break;
  170. } while (1);
  171. return nr;
  172. }
  173. static unsigned int __map_depth_with_shallow(const struct sbitmap *sb,
  174. int index,
  175. unsigned int shallow_depth)
  176. {
  177. u64 shallow_word_depth;
  178. unsigned int word_depth, reminder;
  179. word_depth = __map_depth(sb, index);
  180. if (shallow_depth >= sb->depth)
  181. return word_depth;
  182. shallow_word_depth = word_depth * shallow_depth;
  183. reminder = do_div(shallow_word_depth, sb->depth);
  184. if (reminder >= (index + 1) * word_depth)
  185. shallow_word_depth++;
  186. return (unsigned int)shallow_word_depth;
  187. }
  188. static int sbitmap_find_bit(struct sbitmap *sb,
  189. unsigned int shallow_depth,
  190. unsigned int index,
  191. unsigned int alloc_hint,
  192. bool wrap)
  193. {
  194. unsigned int i;
  195. int nr = -1;
  196. for (i = 0; i < sb->map_nr; i++) {
  197. unsigned int depth = __map_depth_with_shallow(sb, index,
  198. shallow_depth);
  199. if (depth)
  200. nr = sbitmap_find_bit_in_word(&sb->map[index], depth,
  201. alloc_hint, wrap);
  202. if (nr != -1) {
  203. nr += index << sb->shift;
  204. break;
  205. }
  206. /* Jump to next index. */
  207. alloc_hint = 0;
  208. if (++index >= sb->map_nr)
  209. index = 0;
  210. }
  211. return nr;
  212. }
  213. static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint)
  214. {
  215. unsigned int index;
  216. index = SB_NR_TO_INDEX(sb, alloc_hint);
  217. /*
  218. * Unless we're doing round robin tag allocation, just use the
  219. * alloc_hint to find the right word index. No point in looping
  220. * twice in find_next_zero_bit() for that case.
  221. */
  222. if (sb->round_robin)
  223. alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
  224. else
  225. alloc_hint = 0;
  226. return sbitmap_find_bit(sb, UINT_MAX, index, alloc_hint,
  227. !sb->round_robin);
  228. }
  229. int sbitmap_get(struct sbitmap *sb)
  230. {
  231. int nr;
  232. unsigned int hint, depth;
  233. if (WARN_ON_ONCE(unlikely(!sb->alloc_hint)))
  234. return -1;
  235. depth = READ_ONCE(sb->depth);
  236. hint = update_alloc_hint_before_get(sb, depth);
  237. nr = __sbitmap_get(sb, hint);
  238. update_alloc_hint_after_get(sb, depth, hint, nr);
  239. return nr;
  240. }
  241. EXPORT_SYMBOL_GPL(sbitmap_get);
  242. static int __sbitmap_get_shallow(struct sbitmap *sb,
  243. unsigned int alloc_hint,
  244. unsigned long shallow_depth)
  245. {
  246. unsigned int index;
  247. index = SB_NR_TO_INDEX(sb, alloc_hint);
  248. alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
  249. return sbitmap_find_bit(sb, shallow_depth, index, alloc_hint, true);
  250. }
  251. /**
  252. * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap,
  253. * limiting the depth used from each word.
  254. * @sb: Bitmap to allocate from.
  255. * @shallow_depth: The maximum number of bits to allocate from the bitmap.
  256. *
  257. * This rather specific operation allows for having multiple users with
  258. * different allocation limits. E.g., there can be a high-priority class that
  259. * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow()
  260. * with a @shallow_depth of (sb->depth >> 1). Then, the low-priority
  261. * class can only allocate half of the total bits in the bitmap, preventing it
  262. * from starving out the high-priority class.
  263. *
  264. * Return: Non-negative allocated bit number if successful, -1 otherwise.
  265. */
  266. static int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth)
  267. {
  268. int nr;
  269. unsigned int hint, depth;
  270. if (WARN_ON_ONCE(unlikely(!sb->alloc_hint)))
  271. return -1;
  272. depth = READ_ONCE(sb->depth);
  273. hint = update_alloc_hint_before_get(sb, depth);
  274. nr = __sbitmap_get_shallow(sb, hint, shallow_depth);
  275. update_alloc_hint_after_get(sb, depth, hint, nr);
  276. return nr;
  277. }
  278. bool sbitmap_any_bit_set(const struct sbitmap *sb)
  279. {
  280. unsigned int i;
  281. for (i = 0; i < sb->map_nr; i++) {
  282. if (sb->map[i].word & ~sb->map[i].cleared)
  283. return true;
  284. }
  285. return false;
  286. }
  287. EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
  288. static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
  289. {
  290. unsigned int i, weight = 0;
  291. for (i = 0; i < sb->map_nr; i++) {
  292. const struct sbitmap_word *word = &sb->map[i];
  293. unsigned int word_depth = __map_depth(sb, i);
  294. if (set)
  295. weight += bitmap_weight(&word->word, word_depth);
  296. else
  297. weight += bitmap_weight(&word->cleared, word_depth);
  298. }
  299. return weight;
  300. }
  301. static unsigned int sbitmap_cleared(const struct sbitmap *sb)
  302. {
  303. return __sbitmap_weight(sb, false);
  304. }
  305. unsigned int sbitmap_weight(const struct sbitmap *sb)
  306. {
  307. return __sbitmap_weight(sb, true) - sbitmap_cleared(sb);
  308. }
  309. EXPORT_SYMBOL_GPL(sbitmap_weight);
  310. void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
  311. {
  312. seq_printf(m, "depth=%u\n", sb->depth);
  313. seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
  314. seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
  315. seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
  316. seq_printf(m, "map_nr=%u\n", sb->map_nr);
  317. }
  318. EXPORT_SYMBOL_GPL(sbitmap_show);
  319. static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
  320. {
  321. if ((offset & 0xf) == 0) {
  322. if (offset != 0)
  323. seq_putc(m, '\n');
  324. seq_printf(m, "%08x:", offset);
  325. }
  326. if ((offset & 0x1) == 0)
  327. seq_putc(m, ' ');
  328. seq_printf(m, "%02x", byte);
  329. }
  330. void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
  331. {
  332. u8 byte = 0;
  333. unsigned int byte_bits = 0;
  334. unsigned int offset = 0;
  335. int i;
  336. for (i = 0; i < sb->map_nr; i++) {
  337. unsigned long word = READ_ONCE(sb->map[i].word);
  338. unsigned long cleared = READ_ONCE(sb->map[i].cleared);
  339. unsigned int word_bits = __map_depth(sb, i);
  340. word &= ~cleared;
  341. while (word_bits > 0) {
  342. unsigned int bits = min(8 - byte_bits, word_bits);
  343. byte |= (word & (BIT(bits) - 1)) << byte_bits;
  344. byte_bits += bits;
  345. if (byte_bits == 8) {
  346. emit_byte(m, offset, byte);
  347. byte = 0;
  348. byte_bits = 0;
  349. offset++;
  350. }
  351. word >>= bits;
  352. word_bits -= bits;
  353. }
  354. }
  355. if (byte_bits) {
  356. emit_byte(m, offset, byte);
  357. offset++;
  358. }
  359. if (offset)
  360. seq_putc(m, '\n');
  361. }
  362. EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
  363. static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
  364. unsigned int depth)
  365. {
  366. return clamp_t(unsigned int,
  367. min(depth, sbq->min_shallow_depth) / SBQ_WAIT_QUEUES,
  368. 1, SBQ_WAKE_BATCH);
  369. }
  370. int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
  371. int shift, bool round_robin, gfp_t flags, int node)
  372. {
  373. int ret;
  374. int i;
  375. ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node,
  376. round_robin, true);
  377. if (ret)
  378. return ret;
  379. sbq->min_shallow_depth = UINT_MAX;
  380. sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
  381. atomic_set(&sbq->wake_index, 0);
  382. atomic_set(&sbq->ws_active, 0);
  383. atomic_set(&sbq->completion_cnt, 0);
  384. atomic_set(&sbq->wakeup_cnt, 0);
  385. sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
  386. if (!sbq->ws) {
  387. sbitmap_free(&sbq->sb);
  388. return -ENOMEM;
  389. }
  390. for (i = 0; i < SBQ_WAIT_QUEUES; i++)
  391. init_waitqueue_head(&sbq->ws[i].wait);
  392. return 0;
  393. }
  394. EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
  395. static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
  396. unsigned int depth)
  397. {
  398. unsigned int wake_batch;
  399. wake_batch = sbq_calc_wake_batch(sbq, depth);
  400. if (sbq->wake_batch != wake_batch)
  401. WRITE_ONCE(sbq->wake_batch, wake_batch);
  402. }
  403. void sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue *sbq,
  404. unsigned int users)
  405. {
  406. unsigned int wake_batch;
  407. unsigned int depth = (sbq->sb.depth + users - 1) / users;
  408. wake_batch = clamp_val(depth / SBQ_WAIT_QUEUES,
  409. 1, SBQ_WAKE_BATCH);
  410. WRITE_ONCE(sbq->wake_batch, wake_batch);
  411. }
  412. EXPORT_SYMBOL_GPL(sbitmap_queue_recalculate_wake_batch);
  413. void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
  414. {
  415. sbitmap_queue_update_wake_batch(sbq, depth);
  416. sbitmap_resize(&sbq->sb, depth);
  417. }
  418. EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
  419. int __sbitmap_queue_get(struct sbitmap_queue *sbq)
  420. {
  421. return sbitmap_get(&sbq->sb);
  422. }
  423. EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
  424. unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
  425. unsigned int *offset)
  426. {
  427. struct sbitmap *sb = &sbq->sb;
  428. unsigned int hint, depth;
  429. unsigned long index, nr;
  430. int i;
  431. if (unlikely(sb->round_robin))
  432. return 0;
  433. depth = READ_ONCE(sb->depth);
  434. hint = update_alloc_hint_before_get(sb, depth);
  435. index = SB_NR_TO_INDEX(sb, hint);
  436. for (i = 0; i < sb->map_nr; i++) {
  437. struct sbitmap_word *map = &sb->map[index];
  438. unsigned long get_mask;
  439. unsigned int map_depth = __map_depth(sb, index);
  440. unsigned long val;
  441. sbitmap_deferred_clear(map, 0, 0, 0);
  442. val = READ_ONCE(map->word);
  443. if (val == (1UL << (map_depth - 1)) - 1)
  444. goto next;
  445. nr = find_first_zero_bit(&val, map_depth);
  446. if (nr + nr_tags <= map_depth) {
  447. atomic_long_t *ptr = (atomic_long_t *) &map->word;
  448. get_mask = ((1UL << nr_tags) - 1) << nr;
  449. while (!atomic_long_try_cmpxchg(ptr, &val,
  450. get_mask | val))
  451. ;
  452. get_mask = (get_mask & ~val) >> nr;
  453. if (get_mask) {
  454. *offset = nr + (index << sb->shift);
  455. update_alloc_hint_after_get(sb, depth, hint,
  456. *offset + nr_tags - 1);
  457. return get_mask;
  458. }
  459. }
  460. next:
  461. /* Jump to next index. */
  462. if (++index >= sb->map_nr)
  463. index = 0;
  464. }
  465. return 0;
  466. }
  467. int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
  468. unsigned int shallow_depth)
  469. {
  470. WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
  471. return sbitmap_get_shallow(&sbq->sb, shallow_depth);
  472. }
  473. EXPORT_SYMBOL_GPL(sbitmap_queue_get_shallow);
  474. void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
  475. unsigned int min_shallow_depth)
  476. {
  477. sbq->min_shallow_depth = min_shallow_depth;
  478. sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
  479. }
  480. EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
  481. static void __sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
  482. {
  483. int i, wake_index, woken;
  484. if (!atomic_read(&sbq->ws_active))
  485. return;
  486. wake_index = atomic_read(&sbq->wake_index);
  487. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  488. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  489. /*
  490. * Advance the index before checking the current queue.
  491. * It improves fairness, by ensuring the queue doesn't
  492. * need to be fully emptied before trying to wake up
  493. * from the next one.
  494. */
  495. wake_index = sbq_index_inc(wake_index);
  496. if (waitqueue_active(&ws->wait)) {
  497. woken = wake_up_nr(&ws->wait, nr);
  498. if (woken == nr)
  499. break;
  500. nr -= woken;
  501. }
  502. }
  503. if (wake_index != atomic_read(&sbq->wake_index))
  504. atomic_set(&sbq->wake_index, wake_index);
  505. }
  506. void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
  507. {
  508. unsigned int wake_batch = READ_ONCE(sbq->wake_batch);
  509. unsigned int wakeups;
  510. if (!atomic_read(&sbq->ws_active))
  511. return;
  512. atomic_add(nr, &sbq->completion_cnt);
  513. wakeups = atomic_read(&sbq->wakeup_cnt);
  514. do {
  515. if (atomic_read(&sbq->completion_cnt) - wakeups < wake_batch)
  516. return;
  517. } while (!atomic_try_cmpxchg(&sbq->wakeup_cnt,
  518. &wakeups, wakeups + wake_batch));
  519. __sbitmap_queue_wake_up(sbq, wake_batch);
  520. }
  521. EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
  522. static inline void sbitmap_update_cpu_hint(struct sbitmap *sb, int cpu, int tag)
  523. {
  524. if (likely(!sb->round_robin && tag < sb->depth))
  525. data_race(*per_cpu_ptr(sb->alloc_hint, cpu) = tag);
  526. }
  527. void sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, int offset,
  528. int *tags, int nr_tags)
  529. {
  530. struct sbitmap *sb = &sbq->sb;
  531. unsigned long *addr = NULL;
  532. unsigned long mask = 0;
  533. int i;
  534. smp_mb__before_atomic();
  535. for (i = 0; i < nr_tags; i++) {
  536. const int tag = tags[i] - offset;
  537. unsigned long *this_addr;
  538. /* since we're clearing a batch, skip the deferred map */
  539. this_addr = &sb->map[SB_NR_TO_INDEX(sb, tag)].word;
  540. if (!addr) {
  541. addr = this_addr;
  542. } else if (addr != this_addr) {
  543. atomic_long_andnot(mask, (atomic_long_t *) addr);
  544. mask = 0;
  545. addr = this_addr;
  546. }
  547. mask |= (1UL << SB_NR_TO_BIT(sb, tag));
  548. }
  549. if (mask)
  550. atomic_long_andnot(mask, (atomic_long_t *) addr);
  551. smp_mb__after_atomic();
  552. sbitmap_queue_wake_up(sbq, nr_tags);
  553. sbitmap_update_cpu_hint(&sbq->sb, raw_smp_processor_id(),
  554. tags[nr_tags - 1] - offset);
  555. }
  556. void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
  557. unsigned int cpu)
  558. {
  559. /*
  560. * Once the clear bit is set, the bit may be allocated out.
  561. *
  562. * Orders READ/WRITE on the associated instance(such as request
  563. * of blk_mq) by this bit for avoiding race with re-allocation,
  564. * and its pair is the memory barrier implied in __sbitmap_get_word.
  565. *
  566. * One invariant is that the clear bit has to be zero when the bit
  567. * is in use.
  568. */
  569. smp_mb__before_atomic();
  570. sbitmap_deferred_clear_bit(&sbq->sb, nr);
  571. /*
  572. * Pairs with the memory barrier in set_current_state() to ensure the
  573. * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
  574. * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
  575. * waiter. See the comment on waitqueue_active().
  576. */
  577. smp_mb__after_atomic();
  578. sbitmap_queue_wake_up(sbq, 1);
  579. sbitmap_update_cpu_hint(&sbq->sb, cpu, nr);
  580. }
  581. EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
  582. void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
  583. {
  584. int i, wake_index;
  585. /*
  586. * Pairs with the memory barrier in set_current_state() like in
  587. * sbitmap_queue_wake_up().
  588. */
  589. smp_mb();
  590. wake_index = atomic_read(&sbq->wake_index);
  591. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  592. struct sbq_wait_state *ws = &sbq->ws[wake_index];
  593. if (waitqueue_active(&ws->wait))
  594. wake_up(&ws->wait);
  595. wake_index = sbq_index_inc(wake_index);
  596. }
  597. }
  598. EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
  599. void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
  600. {
  601. bool first;
  602. int i;
  603. sbitmap_show(&sbq->sb, m);
  604. seq_puts(m, "alloc_hint={");
  605. first = true;
  606. for_each_possible_cpu(i) {
  607. if (!first)
  608. seq_puts(m, ", ");
  609. first = false;
  610. seq_printf(m, "%u", *per_cpu_ptr(sbq->sb.alloc_hint, i));
  611. }
  612. seq_puts(m, "}\n");
  613. seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
  614. seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
  615. seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
  616. seq_puts(m, "ws={\n");
  617. for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
  618. struct sbq_wait_state *ws = &sbq->ws[i];
  619. seq_printf(m, "\t{.wait=%s},\n",
  620. waitqueue_active(&ws->wait) ? "active" : "inactive");
  621. }
  622. seq_puts(m, "}\n");
  623. seq_printf(m, "round_robin=%d\n", sbq->sb.round_robin);
  624. seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
  625. }
  626. EXPORT_SYMBOL_GPL(sbitmap_queue_show);
  627. void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
  628. struct sbq_wait_state *ws,
  629. struct sbq_wait *sbq_wait)
  630. {
  631. if (!sbq_wait->sbq) {
  632. sbq_wait->sbq = sbq;
  633. atomic_inc(&sbq->ws_active);
  634. add_wait_queue(&ws->wait, &sbq_wait->wait);
  635. }
  636. }
  637. EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
  638. void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
  639. {
  640. list_del_init(&sbq_wait->wait.entry);
  641. if (sbq_wait->sbq) {
  642. atomic_dec(&sbq_wait->sbq->ws_active);
  643. sbq_wait->sbq = NULL;
  644. }
  645. }
  646. EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
  647. void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
  648. struct sbq_wait_state *ws,
  649. struct sbq_wait *sbq_wait, int state)
  650. {
  651. if (!sbq_wait->sbq) {
  652. atomic_inc(&sbq->ws_active);
  653. sbq_wait->sbq = sbq;
  654. }
  655. prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
  656. }
  657. EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
  658. void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
  659. struct sbq_wait *sbq_wait)
  660. {
  661. finish_wait(&ws->wait, &sbq_wait->wait);
  662. if (sbq_wait->sbq) {
  663. atomic_dec(&sbq->ws_active);
  664. sbq_wait->sbq = NULL;
  665. }
  666. }
  667. EXPORT_SYMBOL_GPL(sbitmap_finish_wait);