async-thread.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 "async-thread.h"
  12. #include "ctree.h"
  13. #define WORK_DONE_BIT 0
  14. #define WORK_ORDER_DONE_BIT 1
  15. #define WORK_HIGH_PRIO_BIT 2
  16. #define NO_THRESHOLD (-1)
  17. #define DFT_THRESHOLD (32)
  18. struct __btrfs_workqueue {
  19. struct workqueue_struct *normal_wq;
  20. /* File system this workqueue services */
  21. struct btrfs_fs_info *fs_info;
  22. /* List head pointing to ordered work list */
  23. struct list_head ordered_list;
  24. /* Spinlock for ordered_list */
  25. spinlock_t list_lock;
  26. /* Thresholding related variants */
  27. atomic_t pending;
  28. /* Up limit of concurrency workers */
  29. int limit_active;
  30. /* Current number of concurrency workers */
  31. int current_active;
  32. /* Threshold to change current_active */
  33. int thresh;
  34. unsigned int count;
  35. spinlock_t thres_lock;
  36. };
  37. struct btrfs_workqueue {
  38. struct __btrfs_workqueue *normal;
  39. struct __btrfs_workqueue *high;
  40. };
  41. static void normal_work_helper(struct btrfs_work *work);
  42. #define BTRFS_WORK_HELPER(name) \
  43. noinline_for_stack void btrfs_##name(struct work_struct *arg) \
  44. { \
  45. struct btrfs_work *work = container_of(arg, struct btrfs_work, \
  46. normal_work); \
  47. normal_work_helper(work); \
  48. }
  49. struct btrfs_fs_info *
  50. btrfs_workqueue_owner(const struct __btrfs_workqueue *wq)
  51. {
  52. return wq->fs_info;
  53. }
  54. struct btrfs_fs_info *
  55. btrfs_work_owner(const struct btrfs_work *work)
  56. {
  57. return work->wq->fs_info;
  58. }
  59. bool btrfs_workqueue_normal_congested(const struct btrfs_workqueue *wq)
  60. {
  61. /*
  62. * We could compare wq->normal->pending with num_online_cpus()
  63. * to support "thresh == NO_THRESHOLD" case, but it requires
  64. * moving up atomic_inc/dec in thresh_queue/exec_hook. Let's
  65. * postpone it until someone needs the support of that case.
  66. */
  67. if (wq->normal->thresh == NO_THRESHOLD)
  68. return false;
  69. return atomic_read(&wq->normal->pending) > wq->normal->thresh * 2;
  70. }
  71. BTRFS_WORK_HELPER(worker_helper);
  72. BTRFS_WORK_HELPER(delalloc_helper);
  73. BTRFS_WORK_HELPER(flush_delalloc_helper);
  74. BTRFS_WORK_HELPER(cache_helper);
  75. BTRFS_WORK_HELPER(submit_helper);
  76. BTRFS_WORK_HELPER(fixup_helper);
  77. BTRFS_WORK_HELPER(endio_helper);
  78. BTRFS_WORK_HELPER(endio_meta_helper);
  79. BTRFS_WORK_HELPER(endio_meta_write_helper);
  80. BTRFS_WORK_HELPER(endio_raid56_helper);
  81. BTRFS_WORK_HELPER(endio_repair_helper);
  82. BTRFS_WORK_HELPER(rmw_helper);
  83. BTRFS_WORK_HELPER(endio_write_helper);
  84. BTRFS_WORK_HELPER(freespace_write_helper);
  85. BTRFS_WORK_HELPER(delayed_meta_helper);
  86. BTRFS_WORK_HELPER(readahead_helper);
  87. BTRFS_WORK_HELPER(qgroup_rescan_helper);
  88. BTRFS_WORK_HELPER(extent_refs_helper);
  89. BTRFS_WORK_HELPER(scrub_helper);
  90. BTRFS_WORK_HELPER(scrubwrc_helper);
  91. BTRFS_WORK_HELPER(scrubnc_helper);
  92. BTRFS_WORK_HELPER(scrubparity_helper);
  93. static struct __btrfs_workqueue *
  94. __btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info, const char *name,
  95. unsigned int flags, int limit_active, int thresh)
  96. {
  97. struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_KERNEL);
  98. if (!ret)
  99. return NULL;
  100. ret->fs_info = fs_info;
  101. ret->limit_active = limit_active;
  102. atomic_set(&ret->pending, 0);
  103. if (thresh == 0)
  104. thresh = DFT_THRESHOLD;
  105. /* For low threshold, disabling threshold is a better choice */
  106. if (thresh < DFT_THRESHOLD) {
  107. ret->current_active = limit_active;
  108. ret->thresh = NO_THRESHOLD;
  109. } else {
  110. /*
  111. * For threshold-able wq, let its concurrency grow on demand.
  112. * Use minimal max_active at alloc time to reduce resource
  113. * usage.
  114. */
  115. ret->current_active = 1;
  116. ret->thresh = thresh;
  117. }
  118. if (flags & WQ_HIGHPRI)
  119. ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
  120. ret->current_active, "btrfs",
  121. name);
  122. else
  123. ret->normal_wq = alloc_workqueue("%s-%s", flags,
  124. ret->current_active, "btrfs",
  125. name);
  126. if (!ret->normal_wq) {
  127. kfree(ret);
  128. return NULL;
  129. }
  130. INIT_LIST_HEAD(&ret->ordered_list);
  131. spin_lock_init(&ret->list_lock);
  132. spin_lock_init(&ret->thres_lock);
  133. trace_btrfs_workqueue_alloc(ret, name, flags & WQ_HIGHPRI);
  134. return ret;
  135. }
  136. static inline void
  137. __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq);
  138. struct btrfs_workqueue *btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info,
  139. const char *name,
  140. unsigned int flags,
  141. int limit_active,
  142. int thresh)
  143. {
  144. struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_KERNEL);
  145. if (!ret)
  146. return NULL;
  147. ret->normal = __btrfs_alloc_workqueue(fs_info, name,
  148. flags & ~WQ_HIGHPRI,
  149. limit_active, thresh);
  150. if (!ret->normal) {
  151. kfree(ret);
  152. return NULL;
  153. }
  154. if (flags & WQ_HIGHPRI) {
  155. ret->high = __btrfs_alloc_workqueue(fs_info, name, flags,
  156. limit_active, thresh);
  157. if (!ret->high) {
  158. __btrfs_destroy_workqueue(ret->normal);
  159. kfree(ret);
  160. return NULL;
  161. }
  162. }
  163. return ret;
  164. }
  165. /*
  166. * Hook for threshold which will be called in btrfs_queue_work.
  167. * This hook WILL be called in IRQ handler context,
  168. * so workqueue_set_max_active MUST NOT be called in this hook
  169. */
  170. static inline void thresh_queue_hook(struct __btrfs_workqueue *wq)
  171. {
  172. if (wq->thresh == NO_THRESHOLD)
  173. return;
  174. atomic_inc(&wq->pending);
  175. }
  176. /*
  177. * Hook for threshold which will be called before executing the work,
  178. * This hook is called in kthread content.
  179. * So workqueue_set_max_active is called here.
  180. */
  181. static inline void thresh_exec_hook(struct __btrfs_workqueue *wq)
  182. {
  183. int new_current_active;
  184. long pending;
  185. int need_change = 0;
  186. if (wq->thresh == NO_THRESHOLD)
  187. return;
  188. atomic_dec(&wq->pending);
  189. spin_lock(&wq->thres_lock);
  190. /*
  191. * Use wq->count to limit the calling frequency of
  192. * workqueue_set_max_active.
  193. */
  194. wq->count++;
  195. wq->count %= (wq->thresh / 4);
  196. if (!wq->count)
  197. goto out;
  198. new_current_active = wq->current_active;
  199. /*
  200. * pending may be changed later, but it's OK since we really
  201. * don't need it so accurate to calculate new_max_active.
  202. */
  203. pending = atomic_read(&wq->pending);
  204. if (pending > wq->thresh)
  205. new_current_active++;
  206. if (pending < wq->thresh / 2)
  207. new_current_active--;
  208. new_current_active = clamp_val(new_current_active, 1, wq->limit_active);
  209. if (new_current_active != wq->current_active) {
  210. need_change = 1;
  211. wq->current_active = new_current_active;
  212. }
  213. out:
  214. spin_unlock(&wq->thres_lock);
  215. if (need_change) {
  216. workqueue_set_max_active(wq->normal_wq, wq->current_active);
  217. }
  218. }
  219. static void run_ordered_work(struct __btrfs_workqueue *wq,
  220. struct btrfs_work *self)
  221. {
  222. struct list_head *list = &wq->ordered_list;
  223. struct btrfs_work *work;
  224. spinlock_t *lock = &wq->list_lock;
  225. unsigned long flags;
  226. void *wtag;
  227. bool free_self = false;
  228. while (1) {
  229. spin_lock_irqsave(lock, flags);
  230. if (list_empty(list))
  231. break;
  232. work = list_entry(list->next, struct btrfs_work,
  233. ordered_list);
  234. if (!test_bit(WORK_DONE_BIT, &work->flags))
  235. break;
  236. /*
  237. * we are going to call the ordered done function, but
  238. * we leave the work item on the list as a barrier so
  239. * that later work items that are done don't have their
  240. * functions called before this one returns
  241. */
  242. if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
  243. break;
  244. trace_btrfs_ordered_sched(work);
  245. spin_unlock_irqrestore(lock, flags);
  246. work->ordered_func(work);
  247. /* now take the lock again and drop our item from the list */
  248. spin_lock_irqsave(lock, flags);
  249. list_del(&work->ordered_list);
  250. spin_unlock_irqrestore(lock, flags);
  251. if (work == self) {
  252. /*
  253. * This is the work item that the worker is currently
  254. * executing.
  255. *
  256. * The kernel workqueue code guarantees non-reentrancy
  257. * of work items. I.e., if a work item with the same
  258. * address and work function is queued twice, the second
  259. * execution is blocked until the first one finishes. A
  260. * work item may be freed and recycled with the same
  261. * work function; the workqueue code assumes that the
  262. * original work item cannot depend on the recycled work
  263. * item in that case (see find_worker_executing_work()).
  264. *
  265. * Note that the work of one Btrfs filesystem may depend
  266. * on the work of another Btrfs filesystem via, e.g., a
  267. * loop device. Therefore, we must not allow the current
  268. * work item to be recycled until we are really done,
  269. * otherwise we break the above assumption and can
  270. * deadlock.
  271. */
  272. free_self = true;
  273. } else {
  274. /*
  275. * We don't want to call the ordered free functions with
  276. * the lock held though. Save the work as tag for the
  277. * trace event, because the callback could free the
  278. * structure.
  279. */
  280. wtag = work;
  281. work->ordered_free(work);
  282. trace_btrfs_all_work_done(wq->fs_info, wtag);
  283. }
  284. }
  285. spin_unlock_irqrestore(lock, flags);
  286. if (free_self) {
  287. wtag = self;
  288. self->ordered_free(self);
  289. trace_btrfs_all_work_done(wq->fs_info, wtag);
  290. }
  291. }
  292. static void normal_work_helper(struct btrfs_work *work)
  293. {
  294. struct __btrfs_workqueue *wq;
  295. void *wtag;
  296. int need_order = 0;
  297. /*
  298. * We should not touch things inside work in the following cases:
  299. * 1) after work->func() if it has no ordered_free
  300. * Since the struct is freed in work->func().
  301. * 2) after setting WORK_DONE_BIT
  302. * The work may be freed in other threads almost instantly.
  303. * So we save the needed things here.
  304. */
  305. if (work->ordered_func)
  306. need_order = 1;
  307. wq = work->wq;
  308. /* Safe for tracepoints in case work gets freed by the callback */
  309. wtag = work;
  310. trace_btrfs_work_sched(work);
  311. thresh_exec_hook(wq);
  312. work->func(work);
  313. if (need_order) {
  314. set_bit(WORK_DONE_BIT, &work->flags);
  315. run_ordered_work(wq, work);
  316. }
  317. if (!need_order)
  318. trace_btrfs_all_work_done(wq->fs_info, wtag);
  319. }
  320. void btrfs_init_work(struct btrfs_work *work, btrfs_work_func_t uniq_func,
  321. btrfs_func_t func,
  322. btrfs_func_t ordered_func,
  323. btrfs_func_t ordered_free)
  324. {
  325. work->func = func;
  326. work->ordered_func = ordered_func;
  327. work->ordered_free = ordered_free;
  328. INIT_WORK(&work->normal_work, uniq_func);
  329. INIT_LIST_HEAD(&work->ordered_list);
  330. work->flags = 0;
  331. }
  332. static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq,
  333. struct btrfs_work *work)
  334. {
  335. unsigned long flags;
  336. work->wq = wq;
  337. thresh_queue_hook(wq);
  338. if (work->ordered_func) {
  339. spin_lock_irqsave(&wq->list_lock, flags);
  340. list_add_tail(&work->ordered_list, &wq->ordered_list);
  341. spin_unlock_irqrestore(&wq->list_lock, flags);
  342. }
  343. trace_btrfs_work_queued(work);
  344. queue_work(wq->normal_wq, &work->normal_work);
  345. }
  346. void btrfs_queue_work(struct btrfs_workqueue *wq,
  347. struct btrfs_work *work)
  348. {
  349. struct __btrfs_workqueue *dest_wq;
  350. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags) && wq->high)
  351. dest_wq = wq->high;
  352. else
  353. dest_wq = wq->normal;
  354. __btrfs_queue_work(dest_wq, work);
  355. }
  356. static inline void
  357. __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq)
  358. {
  359. destroy_workqueue(wq->normal_wq);
  360. trace_btrfs_workqueue_destroy(wq);
  361. kfree(wq);
  362. }
  363. void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
  364. {
  365. if (!wq)
  366. return;
  367. if (wq->high)
  368. __btrfs_destroy_workqueue(wq->high);
  369. __btrfs_destroy_workqueue(wq->normal);
  370. kfree(wq);
  371. }
  372. void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int limit_active)
  373. {
  374. if (!wq)
  375. return;
  376. wq->normal->limit_active = limit_active;
  377. if (wq->high)
  378. wq->high->limit_active = limit_active;
  379. }
  380. void btrfs_set_work_high_priority(struct btrfs_work *work)
  381. {
  382. set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
  383. }
  384. void btrfs_flush_workqueue(struct btrfs_workqueue *wq)
  385. {
  386. if (wq->high)
  387. flush_workqueue(wq->high->normal_wq);
  388. flush_workqueue(wq->normal->normal_wq);
  389. }