async-thread.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. * Copyright (C) 2014 Fujitsu. All rights reserved.
  5. */
  6. #include <linux/kthread.h>
  7. #include <linux/slab.h>
  8. #include <linux/list.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/freezer.h>
  11. #include <trace/events/btrfs.h>
  12. #include "async-thread.h"
  13. enum {
  14. WORK_DONE_BIT,
  15. WORK_ORDER_DONE_BIT,
  16. };
  17. #define NO_THRESHOLD (-1)
  18. #define DEFAULT_THRESHOLD (32)
  19. struct btrfs_workqueue {
  20. struct workqueue_struct *normal_wq;
  21. /* File system this workqueue services */
  22. struct btrfs_fs_info *fs_info;
  23. /* List head pointing to ordered work list */
  24. struct list_head ordered_list;
  25. /* Spinlock for ordered_list */
  26. spinlock_t list_lock;
  27. /* Thresholding related variants */
  28. atomic_t pending;
  29. /* Up limit of concurrency workers */
  30. int limit_active;
  31. /* Current number of concurrency workers */
  32. int current_active;
  33. /* Threshold to change current_active */
  34. int thresh;
  35. unsigned int count;
  36. spinlock_t thres_lock;
  37. };
  38. struct btrfs_fs_info * __pure btrfs_workqueue_owner(const struct btrfs_workqueue *wq)
  39. {
  40. return wq->fs_info;
  41. }
  42. struct btrfs_fs_info * __pure btrfs_work_owner(const struct btrfs_work *work)
  43. {
  44. return work->wq->fs_info;
  45. }
  46. bool btrfs_workqueue_normal_congested(const struct btrfs_workqueue *wq)
  47. {
  48. /*
  49. * We could compare wq->pending with num_online_cpus()
  50. * to support "thresh == NO_THRESHOLD" case, but it requires
  51. * moving up atomic_inc/dec in thresh_queue/exec_hook. Let's
  52. * postpone it until someone needs the support of that case.
  53. */
  54. if (wq->thresh == NO_THRESHOLD)
  55. return false;
  56. return atomic_read(&wq->pending) > wq->thresh * 2;
  57. }
  58. static void btrfs_init_workqueue(struct btrfs_workqueue *wq,
  59. struct btrfs_fs_info *fs_info)
  60. {
  61. wq->fs_info = fs_info;
  62. atomic_set(&wq->pending, 0);
  63. INIT_LIST_HEAD(&wq->ordered_list);
  64. spin_lock_init(&wq->list_lock);
  65. spin_lock_init(&wq->thres_lock);
  66. }
  67. struct btrfs_workqueue *btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info,
  68. const char *name, unsigned int flags,
  69. int limit_active, int thresh)
  70. {
  71. struct btrfs_workqueue *ret = kzalloc_obj(*ret);
  72. if (!ret)
  73. return NULL;
  74. btrfs_init_workqueue(ret, fs_info);
  75. ret->limit_active = limit_active;
  76. if (thresh == 0)
  77. thresh = DEFAULT_THRESHOLD;
  78. /* For low threshold, disabling threshold is a better choice */
  79. if (thresh < DEFAULT_THRESHOLD) {
  80. ret->current_active = limit_active;
  81. ret->thresh = NO_THRESHOLD;
  82. } else {
  83. /*
  84. * For threshold-able wq, let its concurrency grow on demand.
  85. * Use minimal max_active at alloc time to reduce resource
  86. * usage.
  87. */
  88. ret->current_active = 1;
  89. ret->thresh = thresh;
  90. }
  91. ret->normal_wq = alloc_workqueue("btrfs-%s", flags, ret->current_active,
  92. name);
  93. if (!ret->normal_wq) {
  94. kfree(ret);
  95. return NULL;
  96. }
  97. trace_btrfs_workqueue_alloc(ret, name);
  98. return ret;
  99. }
  100. struct btrfs_workqueue *btrfs_alloc_ordered_workqueue(
  101. struct btrfs_fs_info *fs_info, const char *name,
  102. unsigned int flags)
  103. {
  104. struct btrfs_workqueue *ret;
  105. ret = kzalloc_obj(*ret);
  106. if (!ret)
  107. return NULL;
  108. btrfs_init_workqueue(ret, fs_info);
  109. /* Ordered workqueues don't allow @max_active adjustments. */
  110. ret->limit_active = 1;
  111. ret->current_active = 1;
  112. ret->thresh = NO_THRESHOLD;
  113. ret->normal_wq = alloc_ordered_workqueue("btrfs-%s", flags, name);
  114. if (!ret->normal_wq) {
  115. kfree(ret);
  116. return NULL;
  117. }
  118. trace_btrfs_workqueue_alloc(ret, name);
  119. return ret;
  120. }
  121. /*
  122. * Hook for threshold which will be called in btrfs_queue_work.
  123. * This hook WILL be called in IRQ handler context,
  124. * so workqueue_set_max_active MUST NOT be called in this hook
  125. */
  126. static inline void thresh_queue_hook(struct btrfs_workqueue *wq)
  127. {
  128. if (wq->thresh == NO_THRESHOLD)
  129. return;
  130. atomic_inc(&wq->pending);
  131. }
  132. /*
  133. * Hook for threshold which will be called before executing the work,
  134. * This hook is called in kthread content.
  135. * So workqueue_set_max_active is called here.
  136. */
  137. static inline void thresh_exec_hook(struct btrfs_workqueue *wq)
  138. {
  139. int new_current_active;
  140. long pending;
  141. bool need_change = false;
  142. if (wq->thresh == NO_THRESHOLD)
  143. return;
  144. atomic_dec(&wq->pending);
  145. spin_lock(&wq->thres_lock);
  146. /*
  147. * Use wq->count to limit the calling frequency of
  148. * workqueue_set_max_active.
  149. */
  150. wq->count++;
  151. wq->count %= (wq->thresh / 4);
  152. if (!wq->count)
  153. goto out;
  154. new_current_active = wq->current_active;
  155. /*
  156. * pending may be changed later, but it's OK since we really
  157. * don't need it so accurate to calculate new_max_active.
  158. */
  159. pending = atomic_read(&wq->pending);
  160. if (pending > wq->thresh)
  161. new_current_active++;
  162. if (pending < wq->thresh / 2)
  163. new_current_active--;
  164. new_current_active = clamp_val(new_current_active, 1, wq->limit_active);
  165. if (new_current_active != wq->current_active) {
  166. need_change = true;
  167. wq->current_active = new_current_active;
  168. }
  169. out:
  170. spin_unlock(&wq->thres_lock);
  171. if (need_change)
  172. workqueue_set_max_active(wq->normal_wq, wq->current_active);
  173. }
  174. static void run_ordered_work(struct btrfs_workqueue *wq,
  175. struct btrfs_work *self)
  176. {
  177. struct list_head *list = &wq->ordered_list;
  178. struct btrfs_work *work;
  179. spinlock_t *lock = &wq->list_lock;
  180. unsigned long flags;
  181. bool free_self = false;
  182. while (1) {
  183. spin_lock_irqsave(lock, flags);
  184. if (list_empty(list))
  185. break;
  186. work = list_first_entry(list, struct btrfs_work, ordered_list);
  187. if (!test_bit(WORK_DONE_BIT, &work->flags))
  188. break;
  189. /*
  190. * Orders all subsequent loads after reading WORK_DONE_BIT,
  191. * paired with the smp_mb__before_atomic in btrfs_work_helper
  192. * this guarantees that the ordered function will see all
  193. * updates from ordinary work function.
  194. */
  195. smp_rmb();
  196. /*
  197. * we are going to call the ordered done function, but
  198. * we leave the work item on the list as a barrier so
  199. * that later work items that are done don't have their
  200. * functions called before this one returns
  201. */
  202. if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
  203. break;
  204. trace_btrfs_ordered_sched(work);
  205. spin_unlock_irqrestore(lock, flags);
  206. work->ordered_func(work, false);
  207. /* now take the lock again and drop our item from the list */
  208. spin_lock_irqsave(lock, flags);
  209. list_del(&work->ordered_list);
  210. spin_unlock_irqrestore(lock, flags);
  211. if (work == self) {
  212. /*
  213. * This is the work item that the worker is currently
  214. * executing.
  215. *
  216. * The kernel workqueue code guarantees non-reentrancy
  217. * of work items. I.e., if a work item with the same
  218. * address and work function is queued twice, the second
  219. * execution is blocked until the first one finishes. A
  220. * work item may be freed and recycled with the same
  221. * work function; the workqueue code assumes that the
  222. * original work item cannot depend on the recycled work
  223. * item in that case (see find_worker_executing_work()).
  224. *
  225. * Note that different types of Btrfs work can depend on
  226. * each other, and one type of work on one Btrfs
  227. * filesystem may even depend on the same type of work
  228. * on another Btrfs filesystem via, e.g., a loop device.
  229. * Therefore, we must not allow the current work item to
  230. * be recycled until we are really done, otherwise we
  231. * break the above assumption and can deadlock.
  232. */
  233. free_self = true;
  234. } else {
  235. /*
  236. * We don't want to call the ordered free functions with
  237. * the lock held.
  238. */
  239. work->ordered_func(work, true);
  240. /* NB: work must not be dereferenced past this point. */
  241. trace_btrfs_all_work_done(wq->fs_info, work);
  242. }
  243. }
  244. spin_unlock_irqrestore(lock, flags);
  245. if (free_self) {
  246. self->ordered_func(self, true);
  247. /* NB: self must not be dereferenced past this point. */
  248. trace_btrfs_all_work_done(wq->fs_info, self);
  249. }
  250. }
  251. static void btrfs_work_helper(struct work_struct *normal_work)
  252. {
  253. struct btrfs_work *work = container_of(normal_work, struct btrfs_work,
  254. normal_work);
  255. struct btrfs_workqueue *wq = work->wq;
  256. bool need_order = false;
  257. /*
  258. * We should not touch things inside work in the following cases:
  259. * 1) after work->func() if it has no ordered_func(..., true) to free
  260. * Since the struct is freed in work->func().
  261. * 2) after setting WORK_DONE_BIT
  262. * The work may be freed in other threads almost instantly.
  263. * So we save the needed things here.
  264. */
  265. if (work->ordered_func)
  266. need_order = true;
  267. trace_btrfs_work_sched(work);
  268. thresh_exec_hook(wq);
  269. work->func(work);
  270. if (need_order) {
  271. /*
  272. * Ensures all memory accesses done in the work function are
  273. * ordered before setting the WORK_DONE_BIT. Ensuring the thread
  274. * which is going to executed the ordered work sees them.
  275. * Pairs with the smp_rmb in run_ordered_work.
  276. */
  277. smp_mb__before_atomic();
  278. set_bit(WORK_DONE_BIT, &work->flags);
  279. run_ordered_work(wq, work);
  280. } else {
  281. /* NB: work must not be dereferenced past this point. */
  282. trace_btrfs_all_work_done(wq->fs_info, work);
  283. }
  284. }
  285. void btrfs_init_work(struct btrfs_work *work, btrfs_func_t func,
  286. btrfs_ordered_func_t ordered_func)
  287. {
  288. work->func = func;
  289. work->ordered_func = ordered_func;
  290. INIT_WORK(&work->normal_work, btrfs_work_helper);
  291. INIT_LIST_HEAD(&work->ordered_list);
  292. work->flags = 0;
  293. }
  294. void btrfs_queue_work(struct btrfs_workqueue *wq, struct btrfs_work *work)
  295. {
  296. unsigned long flags;
  297. work->wq = wq;
  298. thresh_queue_hook(wq);
  299. if (work->ordered_func) {
  300. spin_lock_irqsave(&wq->list_lock, flags);
  301. list_add_tail(&work->ordered_list, &wq->ordered_list);
  302. spin_unlock_irqrestore(&wq->list_lock, flags);
  303. }
  304. trace_btrfs_work_queued(work);
  305. queue_work(wq->normal_wq, &work->normal_work);
  306. }
  307. void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
  308. {
  309. if (!wq)
  310. return;
  311. destroy_workqueue(wq->normal_wq);
  312. trace_btrfs_workqueue_destroy(wq);
  313. kfree(wq);
  314. }
  315. void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int limit_active)
  316. {
  317. if (wq)
  318. wq->limit_active = limit_active;
  319. }
  320. void btrfs_flush_workqueue(struct btrfs_workqueue *wq)
  321. {
  322. flush_workqueue(wq->normal_wq);
  323. }