xref: /openbmc/linux/fs/btrfs/async-thread.c (revision 58e814fc)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
28b712842SChris Mason /*
38b712842SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
408a9ff32SQu Wenruo  * Copyright (C) 2014 Fujitsu.  All rights reserved.
58b712842SChris Mason  */
68b712842SChris Mason 
78b712842SChris Mason #include <linux/kthread.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
98b712842SChris Mason #include <linux/list.h>
108b712842SChris Mason #include <linux/spinlock.h>
118b712842SChris Mason #include <linux/freezer.h>
128b712842SChris Mason #include "async-thread.h"
1352483bc2SQu Wenruo #include "ctree.h"
148b712842SChris Mason 
15f64ce7b8SDavid Sterba enum {
16f64ce7b8SDavid Sterba 	WORK_DONE_BIT,
17f64ce7b8SDavid Sterba 	WORK_ORDER_DONE_BIT,
18f64ce7b8SDavid Sterba };
194a69a410SChris Mason 
200bd9289cSQu Wenruo #define NO_THRESHOLD (-1)
210bd9289cSQu Wenruo #define DFT_THRESHOLD (32)
220bd9289cSQu Wenruo 
23a31b4a43SChristoph Hellwig struct btrfs_workqueue {
2408a9ff32SQu Wenruo 	struct workqueue_struct *normal_wq;
25cb001095SJeff Mahoney 
26cb001095SJeff Mahoney 	/* File system this workqueue services */
27cb001095SJeff Mahoney 	struct btrfs_fs_info *fs_info;
28cb001095SJeff Mahoney 
2908a9ff32SQu Wenruo 	/* List head pointing to ordered work list */
3008a9ff32SQu Wenruo 	struct list_head ordered_list;
3108a9ff32SQu Wenruo 
3208a9ff32SQu Wenruo 	/* Spinlock for ordered_list */
3308a9ff32SQu Wenruo 	spinlock_t list_lock;
340bd9289cSQu Wenruo 
350bd9289cSQu Wenruo 	/* Thresholding related variants */
360bd9289cSQu Wenruo 	atomic_t pending;
37c6dd6ea5SQu Wenruo 
38c6dd6ea5SQu Wenruo 	/* Up limit of concurrency workers */
39c6dd6ea5SQu Wenruo 	int limit_active;
40c6dd6ea5SQu Wenruo 
41c6dd6ea5SQu Wenruo 	/* Current number of concurrency workers */
42c6dd6ea5SQu Wenruo 	int current_active;
43c6dd6ea5SQu Wenruo 
44c6dd6ea5SQu Wenruo 	/* Threshold to change current_active */
450bd9289cSQu Wenruo 	int thresh;
460bd9289cSQu Wenruo 	unsigned int count;
470bd9289cSQu Wenruo 	spinlock_t thres_lock;
4808a9ff32SQu Wenruo };
4908a9ff32SQu Wenruo 
btrfs_workqueue_owner(const struct btrfs_workqueue * wq)50a31b4a43SChristoph Hellwig struct btrfs_fs_info * __pure btrfs_workqueue_owner(const struct btrfs_workqueue *wq)
51cb001095SJeff Mahoney {
52cb001095SJeff Mahoney 	return wq->fs_info;
53cb001095SJeff Mahoney }
54cb001095SJeff Mahoney 
btrfs_work_owner(const struct btrfs_work * work)55e1f60a65SDavid Sterba struct btrfs_fs_info * __pure btrfs_work_owner(const struct btrfs_work *work)
56cb001095SJeff Mahoney {
57cb001095SJeff Mahoney 	return work->wq->fs_info;
58cb001095SJeff Mahoney }
59cb001095SJeff Mahoney 
btrfs_workqueue_normal_congested(const struct btrfs_workqueue * wq)609a35b637SJeff Mahoney bool btrfs_workqueue_normal_congested(const struct btrfs_workqueue *wq)
612939e1a8SMaxim Patlasov {
622939e1a8SMaxim Patlasov 	/*
63a31b4a43SChristoph Hellwig 	 * We could compare wq->pending with num_online_cpus()
642939e1a8SMaxim Patlasov 	 * to support "thresh == NO_THRESHOLD" case, but it requires
652939e1a8SMaxim Patlasov 	 * moving up atomic_inc/dec in thresh_queue/exec_hook. Let's
662939e1a8SMaxim Patlasov 	 * postpone it until someone needs the support of that case.
672939e1a8SMaxim Patlasov 	 */
68a31b4a43SChristoph Hellwig 	if (wq->thresh == NO_THRESHOLD)
692939e1a8SMaxim Patlasov 		return false;
702939e1a8SMaxim Patlasov 
71a31b4a43SChristoph Hellwig 	return atomic_read(&wq->pending) > wq->thresh * 2;
722939e1a8SMaxim Patlasov }
732939e1a8SMaxim Patlasov 
btrfs_init_workqueue(struct btrfs_workqueue * wq,struct btrfs_fs_info * fs_info)74*58e814fcSTejun Heo static void btrfs_init_workqueue(struct btrfs_workqueue *wq,
75*58e814fcSTejun Heo 				 struct btrfs_fs_info *fs_info)
76*58e814fcSTejun Heo {
77*58e814fcSTejun Heo 	wq->fs_info = fs_info;
78*58e814fcSTejun Heo 	atomic_set(&wq->pending, 0);
79*58e814fcSTejun Heo 	INIT_LIST_HEAD(&wq->ordered_list);
80*58e814fcSTejun Heo 	spin_lock_init(&wq->list_lock);
81*58e814fcSTejun Heo 	spin_lock_init(&wq->thres_lock);
82*58e814fcSTejun Heo }
83*58e814fcSTejun Heo 
btrfs_alloc_workqueue(struct btrfs_fs_info * fs_info,const char * name,unsigned int flags,int limit_active,int thresh)84a31b4a43SChristoph Hellwig struct btrfs_workqueue *btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info,
85a31b4a43SChristoph Hellwig 					      const char *name, unsigned int flags,
86a31b4a43SChristoph Hellwig 					      int limit_active, int thresh)
8708a9ff32SQu Wenruo {
88a31b4a43SChristoph Hellwig 	struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_KERNEL);
8908a9ff32SQu Wenruo 
905d99a998SDavid Sterba 	if (!ret)
9108a9ff32SQu Wenruo 		return NULL;
9208a9ff32SQu Wenruo 
93*58e814fcSTejun Heo 	btrfs_init_workqueue(ret, fs_info);
94*58e814fcSTejun Heo 
95c6dd6ea5SQu Wenruo 	ret->limit_active = limit_active;
960bd9289cSQu Wenruo 	if (thresh == 0)
970bd9289cSQu Wenruo 		thresh = DFT_THRESHOLD;
980bd9289cSQu Wenruo 	/* For low threshold, disabling threshold is a better choice */
990bd9289cSQu Wenruo 	if (thresh < DFT_THRESHOLD) {
100c6dd6ea5SQu Wenruo 		ret->current_active = limit_active;
1010bd9289cSQu Wenruo 		ret->thresh = NO_THRESHOLD;
1020bd9289cSQu Wenruo 	} else {
103c6dd6ea5SQu Wenruo 		/*
104c6dd6ea5SQu Wenruo 		 * For threshold-able wq, let its concurrency grow on demand.
105c6dd6ea5SQu Wenruo 		 * Use minimal max_active at alloc time to reduce resource
106c6dd6ea5SQu Wenruo 		 * usage.
107c6dd6ea5SQu Wenruo 		 */
108c6dd6ea5SQu Wenruo 		ret->current_active = 1;
1090bd9289cSQu Wenruo 		ret->thresh = thresh;
1100bd9289cSQu Wenruo 	}
1110bd9289cSQu Wenruo 
112a31b4a43SChristoph Hellwig 	ret->normal_wq = alloc_workqueue("btrfs-%s", flags, ret->current_active,
113a31b4a43SChristoph Hellwig 					 name);
1145d99a998SDavid Sterba 	if (!ret->normal_wq) {
11508a9ff32SQu Wenruo 		kfree(ret);
11608a9ff32SQu Wenruo 		return NULL;
11708a9ff32SQu Wenruo 	}
11808a9ff32SQu Wenruo 
119*58e814fcSTejun Heo 	trace_btrfs_workqueue_alloc(ret, name);
120*58e814fcSTejun Heo 	return ret;
121*58e814fcSTejun Heo }
122*58e814fcSTejun Heo 
btrfs_alloc_ordered_workqueue(struct btrfs_fs_info * fs_info,const char * name,unsigned int flags)123*58e814fcSTejun Heo struct btrfs_workqueue *btrfs_alloc_ordered_workqueue(
124*58e814fcSTejun Heo 				struct btrfs_fs_info *fs_info, const char *name,
125*58e814fcSTejun Heo 				unsigned int flags)
126*58e814fcSTejun Heo {
127*58e814fcSTejun Heo 	struct btrfs_workqueue *ret;
128*58e814fcSTejun Heo 
129*58e814fcSTejun Heo 	ret = kzalloc(sizeof(*ret), GFP_KERNEL);
130*58e814fcSTejun Heo 	if (!ret)
131*58e814fcSTejun Heo 		return NULL;
132*58e814fcSTejun Heo 
133*58e814fcSTejun Heo 	btrfs_init_workqueue(ret, fs_info);
134*58e814fcSTejun Heo 
135*58e814fcSTejun Heo 	/* Ordered workqueues don't allow @max_active adjustments. */
136*58e814fcSTejun Heo 	ret->limit_active = 1;
137*58e814fcSTejun Heo 	ret->current_active = 1;
138*58e814fcSTejun Heo 	ret->thresh = NO_THRESHOLD;
139*58e814fcSTejun Heo 
140*58e814fcSTejun Heo 	ret->normal_wq = alloc_ordered_workqueue("btrfs-%s", flags, name);
141*58e814fcSTejun Heo 	if (!ret->normal_wq) {
142*58e814fcSTejun Heo 		kfree(ret);
143*58e814fcSTejun Heo 		return NULL;
144*58e814fcSTejun Heo 	}
145*58e814fcSTejun Heo 
146a31b4a43SChristoph Hellwig 	trace_btrfs_workqueue_alloc(ret, name);
1471ca08976SQu Wenruo 	return ret;
1481ca08976SQu Wenruo }
1491ca08976SQu Wenruo 
1500bd9289cSQu Wenruo /*
1510bd9289cSQu Wenruo  * Hook for threshold which will be called in btrfs_queue_work.
1520bd9289cSQu Wenruo  * This hook WILL be called in IRQ handler context,
1530bd9289cSQu Wenruo  * so workqueue_set_max_active MUST NOT be called in this hook
1540bd9289cSQu Wenruo  */
thresh_queue_hook(struct btrfs_workqueue * wq)155a31b4a43SChristoph Hellwig static inline void thresh_queue_hook(struct btrfs_workqueue *wq)
1560bd9289cSQu Wenruo {
1570bd9289cSQu Wenruo 	if (wq->thresh == NO_THRESHOLD)
1580bd9289cSQu Wenruo 		return;
1590bd9289cSQu Wenruo 	atomic_inc(&wq->pending);
1600bd9289cSQu Wenruo }
1610bd9289cSQu Wenruo 
1620bd9289cSQu Wenruo /*
1630bd9289cSQu Wenruo  * Hook for threshold which will be called before executing the work,
1640bd9289cSQu Wenruo  * This hook is called in kthread content.
1650bd9289cSQu Wenruo  * So workqueue_set_max_active is called here.
1660bd9289cSQu Wenruo  */
thresh_exec_hook(struct btrfs_workqueue * wq)167a31b4a43SChristoph Hellwig static inline void thresh_exec_hook(struct btrfs_workqueue *wq)
1680bd9289cSQu Wenruo {
169c6dd6ea5SQu Wenruo 	int new_current_active;
1700bd9289cSQu Wenruo 	long pending;
1710bd9289cSQu Wenruo 	int need_change = 0;
1720bd9289cSQu Wenruo 
1730bd9289cSQu Wenruo 	if (wq->thresh == NO_THRESHOLD)
1740bd9289cSQu Wenruo 		return;
1750bd9289cSQu Wenruo 
1760bd9289cSQu Wenruo 	atomic_dec(&wq->pending);
1770bd9289cSQu Wenruo 	spin_lock(&wq->thres_lock);
1780bd9289cSQu Wenruo 	/*
1790bd9289cSQu Wenruo 	 * Use wq->count to limit the calling frequency of
1800bd9289cSQu Wenruo 	 * workqueue_set_max_active.
1810bd9289cSQu Wenruo 	 */
1820bd9289cSQu Wenruo 	wq->count++;
1830bd9289cSQu Wenruo 	wq->count %= (wq->thresh / 4);
1840bd9289cSQu Wenruo 	if (!wq->count)
1850bd9289cSQu Wenruo 		goto  out;
186c6dd6ea5SQu Wenruo 	new_current_active = wq->current_active;
1870bd9289cSQu Wenruo 
1880bd9289cSQu Wenruo 	/*
1890bd9289cSQu Wenruo 	 * pending may be changed later, but it's OK since we really
1900bd9289cSQu Wenruo 	 * don't need it so accurate to calculate new_max_active.
1910bd9289cSQu Wenruo 	 */
1920bd9289cSQu Wenruo 	pending = atomic_read(&wq->pending);
1930bd9289cSQu Wenruo 	if (pending > wq->thresh)
194c6dd6ea5SQu Wenruo 		new_current_active++;
1950bd9289cSQu Wenruo 	if (pending < wq->thresh / 2)
196c6dd6ea5SQu Wenruo 		new_current_active--;
197c6dd6ea5SQu Wenruo 	new_current_active = clamp_val(new_current_active, 1, wq->limit_active);
198c6dd6ea5SQu Wenruo 	if (new_current_active != wq->current_active)  {
1990bd9289cSQu Wenruo 		need_change = 1;
200c6dd6ea5SQu Wenruo 		wq->current_active = new_current_active;
2010bd9289cSQu Wenruo 	}
2020bd9289cSQu Wenruo out:
2030bd9289cSQu Wenruo 	spin_unlock(&wq->thres_lock);
2040bd9289cSQu Wenruo 
2050bd9289cSQu Wenruo 	if (need_change) {
206c6dd6ea5SQu Wenruo 		workqueue_set_max_active(wq->normal_wq, wq->current_active);
2070bd9289cSQu Wenruo 	}
2080bd9289cSQu Wenruo }
2090bd9289cSQu Wenruo 
run_ordered_work(struct btrfs_workqueue * wq,struct btrfs_work * self)210a31b4a43SChristoph Hellwig static void run_ordered_work(struct btrfs_workqueue *wq,
211c495dcd6SOmar Sandoval 			     struct btrfs_work *self)
21208a9ff32SQu Wenruo {
21308a9ff32SQu Wenruo 	struct list_head *list = &wq->ordered_list;
214d458b054SQu Wenruo 	struct btrfs_work *work;
21508a9ff32SQu Wenruo 	spinlock_t *lock = &wq->list_lock;
21608a9ff32SQu Wenruo 	unsigned long flags;
217c495dcd6SOmar Sandoval 	bool free_self = false;
21808a9ff32SQu Wenruo 
21908a9ff32SQu Wenruo 	while (1) {
22008a9ff32SQu Wenruo 		spin_lock_irqsave(lock, flags);
22108a9ff32SQu Wenruo 		if (list_empty(list))
22208a9ff32SQu Wenruo 			break;
223d458b054SQu Wenruo 		work = list_entry(list->next, struct btrfs_work,
22408a9ff32SQu Wenruo 				  ordered_list);
22508a9ff32SQu Wenruo 		if (!test_bit(WORK_DONE_BIT, &work->flags))
22608a9ff32SQu Wenruo 			break;
22745da9c17SNikolay Borisov 		/*
22845da9c17SNikolay Borisov 		 * Orders all subsequent loads after reading WORK_DONE_BIT,
22945da9c17SNikolay Borisov 		 * paired with the smp_mb__before_atomic in btrfs_work_helper
23045da9c17SNikolay Borisov 		 * this guarantees that the ordered function will see all
23145da9c17SNikolay Borisov 		 * updates from ordinary work function.
23245da9c17SNikolay Borisov 		 */
23345da9c17SNikolay Borisov 		smp_rmb();
23408a9ff32SQu Wenruo 
23508a9ff32SQu Wenruo 		/*
23608a9ff32SQu Wenruo 		 * we are going to call the ordered done function, but
23708a9ff32SQu Wenruo 		 * we leave the work item on the list as a barrier so
23808a9ff32SQu Wenruo 		 * that later work items that are done don't have their
23908a9ff32SQu Wenruo 		 * functions called before this one returns
24008a9ff32SQu Wenruo 		 */
24108a9ff32SQu Wenruo 		if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
24208a9ff32SQu Wenruo 			break;
24352483bc2SQu Wenruo 		trace_btrfs_ordered_sched(work);
24408a9ff32SQu Wenruo 		spin_unlock_irqrestore(lock, flags);
24508a9ff32SQu Wenruo 		work->ordered_func(work);
24608a9ff32SQu Wenruo 
24708a9ff32SQu Wenruo 		/* now take the lock again and drop our item from the list */
24808a9ff32SQu Wenruo 		spin_lock_irqsave(lock, flags);
24908a9ff32SQu Wenruo 		list_del(&work->ordered_list);
25008a9ff32SQu Wenruo 		spin_unlock_irqrestore(lock, flags);
25108a9ff32SQu Wenruo 
252c495dcd6SOmar Sandoval 		if (work == self) {
25308a9ff32SQu Wenruo 			/*
254c495dcd6SOmar Sandoval 			 * This is the work item that the worker is currently
255c495dcd6SOmar Sandoval 			 * executing.
256c495dcd6SOmar Sandoval 			 *
257c495dcd6SOmar Sandoval 			 * The kernel workqueue code guarantees non-reentrancy
258c495dcd6SOmar Sandoval 			 * of work items. I.e., if a work item with the same
259c495dcd6SOmar Sandoval 			 * address and work function is queued twice, the second
260c495dcd6SOmar Sandoval 			 * execution is blocked until the first one finishes. A
261c495dcd6SOmar Sandoval 			 * work item may be freed and recycled with the same
262c495dcd6SOmar Sandoval 			 * work function; the workqueue code assumes that the
263c495dcd6SOmar Sandoval 			 * original work item cannot depend on the recycled work
264c495dcd6SOmar Sandoval 			 * item in that case (see find_worker_executing_work()).
265c495dcd6SOmar Sandoval 			 *
266a0cac0ecSOmar Sandoval 			 * Note that different types of Btrfs work can depend on
267a0cac0ecSOmar Sandoval 			 * each other, and one type of work on one Btrfs
268a0cac0ecSOmar Sandoval 			 * filesystem may even depend on the same type of work
269a0cac0ecSOmar Sandoval 			 * on another Btrfs filesystem via, e.g., a loop device.
270a0cac0ecSOmar Sandoval 			 * Therefore, we must not allow the current work item to
271a0cac0ecSOmar Sandoval 			 * be recycled until we are really done, otherwise we
272a0cac0ecSOmar Sandoval 			 * break the above assumption and can deadlock.
273c495dcd6SOmar Sandoval 			 */
274c495dcd6SOmar Sandoval 			free_self = true;
275c495dcd6SOmar Sandoval 		} else {
276c495dcd6SOmar Sandoval 			/*
277c495dcd6SOmar Sandoval 			 * We don't want to call the ordered free functions with
278c9eb55dbSOmar Sandoval 			 * the lock held.
27908a9ff32SQu Wenruo 			 */
28008a9ff32SQu Wenruo 			work->ordered_free(work);
281c9eb55dbSOmar Sandoval 			/* NB: work must not be dereferenced past this point. */
282c9eb55dbSOmar Sandoval 			trace_btrfs_all_work_done(wq->fs_info, work);
28308a9ff32SQu Wenruo 		}
284c495dcd6SOmar Sandoval 	}
28508a9ff32SQu Wenruo 	spin_unlock_irqrestore(lock, flags);
286c495dcd6SOmar Sandoval 
287c495dcd6SOmar Sandoval 	if (free_self) {
288c495dcd6SOmar Sandoval 		self->ordered_free(self);
289c9eb55dbSOmar Sandoval 		/* NB: self must not be dereferenced past this point. */
290c9eb55dbSOmar Sandoval 		trace_btrfs_all_work_done(wq->fs_info, self);
291c495dcd6SOmar Sandoval 	}
29208a9ff32SQu Wenruo }
29308a9ff32SQu Wenruo 
btrfs_work_helper(struct work_struct * normal_work)294a0cac0ecSOmar Sandoval static void btrfs_work_helper(struct work_struct *normal_work)
29508a9ff32SQu Wenruo {
296a0cac0ecSOmar Sandoval 	struct btrfs_work *work = container_of(normal_work, struct btrfs_work,
297a0cac0ecSOmar Sandoval 					       normal_work);
298a31b4a43SChristoph Hellwig 	struct btrfs_workqueue *wq = work->wq;
29908a9ff32SQu Wenruo 	int need_order = 0;
30008a9ff32SQu Wenruo 
30108a9ff32SQu Wenruo 	/*
30208a9ff32SQu Wenruo 	 * We should not touch things inside work in the following cases:
30308a9ff32SQu Wenruo 	 * 1) after work->func() if it has no ordered_free
30408a9ff32SQu Wenruo 	 *    Since the struct is freed in work->func().
30508a9ff32SQu Wenruo 	 * 2) after setting WORK_DONE_BIT
30608a9ff32SQu Wenruo 	 *    The work may be freed in other threads almost instantly.
30708a9ff32SQu Wenruo 	 * So we save the needed things here.
30808a9ff32SQu Wenruo 	 */
30908a9ff32SQu Wenruo 	if (work->ordered_func)
31008a9ff32SQu Wenruo 		need_order = 1;
31108a9ff32SQu Wenruo 
31252483bc2SQu Wenruo 	trace_btrfs_work_sched(work);
3130bd9289cSQu Wenruo 	thresh_exec_hook(wq);
31408a9ff32SQu Wenruo 	work->func(work);
31508a9ff32SQu Wenruo 	if (need_order) {
31645da9c17SNikolay Borisov 		/*
31745da9c17SNikolay Borisov 		 * Ensures all memory accesses done in the work function are
31845da9c17SNikolay Borisov 		 * ordered before setting the WORK_DONE_BIT. Ensuring the thread
31945da9c17SNikolay Borisov 		 * which is going to executed the ordered work sees them.
32045da9c17SNikolay Borisov 		 * Pairs with the smp_rmb in run_ordered_work.
32145da9c17SNikolay Borisov 		 */
32245da9c17SNikolay Borisov 		smp_mb__before_atomic();
32308a9ff32SQu Wenruo 		set_bit(WORK_DONE_BIT, &work->flags);
324c495dcd6SOmar Sandoval 		run_ordered_work(wq, work);
325c9eb55dbSOmar Sandoval 	} else {
326c9eb55dbSOmar Sandoval 		/* NB: work must not be dereferenced past this point. */
327c9eb55dbSOmar Sandoval 		trace_btrfs_all_work_done(wq->fs_info, work);
32808a9ff32SQu Wenruo 	}
32908a9ff32SQu Wenruo }
33008a9ff32SQu Wenruo 
btrfs_init_work(struct btrfs_work * work,btrfs_func_t func,btrfs_func_t ordered_func,btrfs_func_t ordered_free)331a0cac0ecSOmar Sandoval void btrfs_init_work(struct btrfs_work *work, btrfs_func_t func,
332a0cac0ecSOmar Sandoval 		     btrfs_func_t ordered_func, btrfs_func_t ordered_free)
33308a9ff32SQu Wenruo {
33408a9ff32SQu Wenruo 	work->func = func;
33508a9ff32SQu Wenruo 	work->ordered_func = ordered_func;
33608a9ff32SQu Wenruo 	work->ordered_free = ordered_free;
337a0cac0ecSOmar Sandoval 	INIT_WORK(&work->normal_work, btrfs_work_helper);
33808a9ff32SQu Wenruo 	INIT_LIST_HEAD(&work->ordered_list);
33908a9ff32SQu Wenruo 	work->flags = 0;
34008a9ff32SQu Wenruo }
34108a9ff32SQu Wenruo 
btrfs_queue_work(struct btrfs_workqueue * wq,struct btrfs_work * work)342a31b4a43SChristoph Hellwig void btrfs_queue_work(struct btrfs_workqueue *wq, struct btrfs_work *work)
34308a9ff32SQu Wenruo {
34408a9ff32SQu Wenruo 	unsigned long flags;
34508a9ff32SQu Wenruo 
34608a9ff32SQu Wenruo 	work->wq = wq;
3470bd9289cSQu Wenruo 	thresh_queue_hook(wq);
34808a9ff32SQu Wenruo 	if (work->ordered_func) {
34908a9ff32SQu Wenruo 		spin_lock_irqsave(&wq->list_lock, flags);
35008a9ff32SQu Wenruo 		list_add_tail(&work->ordered_list, &wq->ordered_list);
35108a9ff32SQu Wenruo 		spin_unlock_irqrestore(&wq->list_lock, flags);
35208a9ff32SQu Wenruo 	}
35352483bc2SQu Wenruo 	trace_btrfs_work_queued(work);
3540a95b851SQu Wenruo 	queue_work(wq->normal_wq, &work->normal_work);
35508a9ff32SQu Wenruo }
35608a9ff32SQu Wenruo 
btrfs_destroy_workqueue(struct btrfs_workqueue * wq)357a31b4a43SChristoph Hellwig void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
3581ca08976SQu Wenruo {
359a31b4a43SChristoph Hellwig 	if (!wq)
360a31b4a43SChristoph Hellwig 		return;
36108a9ff32SQu Wenruo 	destroy_workqueue(wq->normal_wq);
362c3a46891SQu Wenruo 	trace_btrfs_workqueue_destroy(wq);
36308a9ff32SQu Wenruo 	kfree(wq);
36408a9ff32SQu Wenruo }
36508a9ff32SQu Wenruo 
btrfs_workqueue_set_max(struct btrfs_workqueue * wq,int limit_active)366c6dd6ea5SQu Wenruo void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int limit_active)
36708a9ff32SQu Wenruo {
368a31b4a43SChristoph Hellwig 	if (wq)
369a31b4a43SChristoph Hellwig 		wq->limit_active = limit_active;
37008a9ff32SQu Wenruo }
371f0cc2cd7SFilipe Manana 
btrfs_flush_workqueue(struct btrfs_workqueue * wq)372f0cc2cd7SFilipe Manana void btrfs_flush_workqueue(struct btrfs_workqueue *wq)
373f0cc2cd7SFilipe Manana {
374a31b4a43SChristoph Hellwig 	flush_workqueue(wq->normal_wq);
375f0cc2cd7SFilipe Manana }
376