xref: /openbmc/linux/fs/btrfs/async-thread.c (revision 2939e1a8)
18b712842SChris Mason /*
28b712842SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
308a9ff32SQu Wenruo  * Copyright (C) 2014 Fujitsu.  All rights reserved.
48b712842SChris Mason  *
58b712842SChris Mason  * This program is free software; you can redistribute it and/or
68b712842SChris Mason  * modify it under the terms of the GNU General Public
78b712842SChris Mason  * License v2 as published by the Free Software Foundation.
88b712842SChris Mason  *
98b712842SChris Mason  * This program is distributed in the hope that it will be useful,
108b712842SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
118b712842SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
128b712842SChris Mason  * General Public License for more details.
138b712842SChris Mason  *
148b712842SChris Mason  * You should have received a copy of the GNU General Public
158b712842SChris Mason  * License along with this program; if not, write to the
168b712842SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
178b712842SChris Mason  * Boston, MA 021110-1307, USA.
188b712842SChris Mason  */
198b712842SChris Mason 
208b712842SChris Mason #include <linux/kthread.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
228b712842SChris Mason #include <linux/list.h>
238b712842SChris Mason #include <linux/spinlock.h>
248b712842SChris Mason #include <linux/freezer.h>
258b712842SChris Mason #include "async-thread.h"
2652483bc2SQu Wenruo #include "ctree.h"
278b712842SChris Mason 
28a046e9c8SQu Wenruo #define WORK_DONE_BIT 0
29a046e9c8SQu Wenruo #define WORK_ORDER_DONE_BIT 1
30a046e9c8SQu Wenruo #define WORK_HIGH_PRIO_BIT 2
314a69a410SChris Mason 
320bd9289cSQu Wenruo #define NO_THRESHOLD (-1)
330bd9289cSQu Wenruo #define DFT_THRESHOLD (32)
340bd9289cSQu Wenruo 
35d458b054SQu Wenruo struct __btrfs_workqueue {
3608a9ff32SQu Wenruo 	struct workqueue_struct *normal_wq;
37cb001095SJeff Mahoney 
38cb001095SJeff Mahoney 	/* File system this workqueue services */
39cb001095SJeff Mahoney 	struct btrfs_fs_info *fs_info;
40cb001095SJeff Mahoney 
4108a9ff32SQu Wenruo 	/* List head pointing to ordered work list */
4208a9ff32SQu Wenruo 	struct list_head ordered_list;
4308a9ff32SQu Wenruo 
4408a9ff32SQu Wenruo 	/* Spinlock for ordered_list */
4508a9ff32SQu Wenruo 	spinlock_t list_lock;
460bd9289cSQu Wenruo 
470bd9289cSQu Wenruo 	/* Thresholding related variants */
480bd9289cSQu Wenruo 	atomic_t pending;
49c6dd6ea5SQu Wenruo 
50c6dd6ea5SQu Wenruo 	/* Up limit of concurrency workers */
51c6dd6ea5SQu Wenruo 	int limit_active;
52c6dd6ea5SQu Wenruo 
53c6dd6ea5SQu Wenruo 	/* Current number of concurrency workers */
54c6dd6ea5SQu Wenruo 	int current_active;
55c6dd6ea5SQu Wenruo 
56c6dd6ea5SQu Wenruo 	/* Threshold to change current_active */
570bd9289cSQu Wenruo 	int thresh;
580bd9289cSQu Wenruo 	unsigned int count;
590bd9289cSQu Wenruo 	spinlock_t thres_lock;
6008a9ff32SQu Wenruo };
6108a9ff32SQu Wenruo 
62d458b054SQu Wenruo struct btrfs_workqueue {
63d458b054SQu Wenruo 	struct __btrfs_workqueue *normal;
64d458b054SQu Wenruo 	struct __btrfs_workqueue *high;
651ca08976SQu Wenruo };
661ca08976SQu Wenruo 
679e0af237SLiu Bo static void normal_work_helper(struct btrfs_work *work);
689e0af237SLiu Bo 
699e0af237SLiu Bo #define BTRFS_WORK_HELPER(name)					\
709e0af237SLiu Bo void btrfs_##name(struct work_struct *arg)				\
719e0af237SLiu Bo {									\
729e0af237SLiu Bo 	struct btrfs_work *work = container_of(arg, struct btrfs_work,	\
739e0af237SLiu Bo 					       normal_work);		\
749e0af237SLiu Bo 	normal_work_helper(work);					\
759e0af237SLiu Bo }
769e0af237SLiu Bo 
77cb001095SJeff Mahoney struct btrfs_fs_info *
78cb001095SJeff Mahoney btrfs_workqueue_owner(struct __btrfs_workqueue *wq)
79cb001095SJeff Mahoney {
80cb001095SJeff Mahoney 	return wq->fs_info;
81cb001095SJeff Mahoney }
82cb001095SJeff Mahoney 
83cb001095SJeff Mahoney struct btrfs_fs_info *
84cb001095SJeff Mahoney btrfs_work_owner(struct btrfs_work *work)
85cb001095SJeff Mahoney {
86cb001095SJeff Mahoney 	return work->wq->fs_info;
87cb001095SJeff Mahoney }
88cb001095SJeff Mahoney 
892939e1a8SMaxim Patlasov bool btrfs_workqueue_normal_congested(struct btrfs_workqueue *wq)
902939e1a8SMaxim Patlasov {
912939e1a8SMaxim Patlasov 	/*
922939e1a8SMaxim Patlasov 	 * We could compare wq->normal->pending with num_online_cpus()
932939e1a8SMaxim Patlasov 	 * to support "thresh == NO_THRESHOLD" case, but it requires
942939e1a8SMaxim Patlasov 	 * moving up atomic_inc/dec in thresh_queue/exec_hook. Let's
952939e1a8SMaxim Patlasov 	 * postpone it until someone needs the support of that case.
962939e1a8SMaxim Patlasov 	 */
972939e1a8SMaxim Patlasov 	if (wq->normal->thresh == NO_THRESHOLD)
982939e1a8SMaxim Patlasov 		return false;
992939e1a8SMaxim Patlasov 
1002939e1a8SMaxim Patlasov 	return atomic_read(&wq->normal->pending) > wq->normal->thresh * 2;
1012939e1a8SMaxim Patlasov }
1022939e1a8SMaxim Patlasov 
1039e0af237SLiu Bo BTRFS_WORK_HELPER(worker_helper);
1049e0af237SLiu Bo BTRFS_WORK_HELPER(delalloc_helper);
1059e0af237SLiu Bo BTRFS_WORK_HELPER(flush_delalloc_helper);
1069e0af237SLiu Bo BTRFS_WORK_HELPER(cache_helper);
1079e0af237SLiu Bo BTRFS_WORK_HELPER(submit_helper);
1089e0af237SLiu Bo BTRFS_WORK_HELPER(fixup_helper);
1099e0af237SLiu Bo BTRFS_WORK_HELPER(endio_helper);
1109e0af237SLiu Bo BTRFS_WORK_HELPER(endio_meta_helper);
1119e0af237SLiu Bo BTRFS_WORK_HELPER(endio_meta_write_helper);
1129e0af237SLiu Bo BTRFS_WORK_HELPER(endio_raid56_helper);
1138b110e39SMiao Xie BTRFS_WORK_HELPER(endio_repair_helper);
1149e0af237SLiu Bo BTRFS_WORK_HELPER(rmw_helper);
1159e0af237SLiu Bo BTRFS_WORK_HELPER(endio_write_helper);
1169e0af237SLiu Bo BTRFS_WORK_HELPER(freespace_write_helper);
1179e0af237SLiu Bo BTRFS_WORK_HELPER(delayed_meta_helper);
1189e0af237SLiu Bo BTRFS_WORK_HELPER(readahead_helper);
1199e0af237SLiu Bo BTRFS_WORK_HELPER(qgroup_rescan_helper);
1209e0af237SLiu Bo BTRFS_WORK_HELPER(extent_refs_helper);
1219e0af237SLiu Bo BTRFS_WORK_HELPER(scrub_helper);
1229e0af237SLiu Bo BTRFS_WORK_HELPER(scrubwrc_helper);
1239e0af237SLiu Bo BTRFS_WORK_HELPER(scrubnc_helper);
12420b2e302SZhao Lei BTRFS_WORK_HELPER(scrubparity_helper);
1259e0af237SLiu Bo 
1269e0af237SLiu Bo static struct __btrfs_workqueue *
127cb001095SJeff Mahoney __btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info, const char *name,
128cb001095SJeff Mahoney 			unsigned int flags, int limit_active, int thresh)
12908a9ff32SQu Wenruo {
13061dd5ae6SDavid Sterba 	struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_KERNEL);
13108a9ff32SQu Wenruo 
1325d99a998SDavid Sterba 	if (!ret)
13308a9ff32SQu Wenruo 		return NULL;
13408a9ff32SQu Wenruo 
135cb001095SJeff Mahoney 	ret->fs_info = fs_info;
136c6dd6ea5SQu Wenruo 	ret->limit_active = limit_active;
1370bd9289cSQu Wenruo 	atomic_set(&ret->pending, 0);
1380bd9289cSQu Wenruo 	if (thresh == 0)
1390bd9289cSQu Wenruo 		thresh = DFT_THRESHOLD;
1400bd9289cSQu Wenruo 	/* For low threshold, disabling threshold is a better choice */
1410bd9289cSQu Wenruo 	if (thresh < DFT_THRESHOLD) {
142c6dd6ea5SQu Wenruo 		ret->current_active = limit_active;
1430bd9289cSQu Wenruo 		ret->thresh = NO_THRESHOLD;
1440bd9289cSQu Wenruo 	} else {
145c6dd6ea5SQu Wenruo 		/*
146c6dd6ea5SQu Wenruo 		 * For threshold-able wq, let its concurrency grow on demand.
147c6dd6ea5SQu Wenruo 		 * Use minimal max_active at alloc time to reduce resource
148c6dd6ea5SQu Wenruo 		 * usage.
149c6dd6ea5SQu Wenruo 		 */
150c6dd6ea5SQu Wenruo 		ret->current_active = 1;
1510bd9289cSQu Wenruo 		ret->thresh = thresh;
1520bd9289cSQu Wenruo 	}
1530bd9289cSQu Wenruo 
1541ca08976SQu Wenruo 	if (flags & WQ_HIGHPRI)
1551ca08976SQu Wenruo 		ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
156c6dd6ea5SQu Wenruo 						 ret->current_active, "btrfs",
157c6dd6ea5SQu Wenruo 						 name);
1581ca08976SQu Wenruo 	else
1591ca08976SQu Wenruo 		ret->normal_wq = alloc_workqueue("%s-%s", flags,
160c6dd6ea5SQu Wenruo 						 ret->current_active, "btrfs",
1610bd9289cSQu Wenruo 						 name);
1625d99a998SDavid Sterba 	if (!ret->normal_wq) {
16308a9ff32SQu Wenruo 		kfree(ret);
16408a9ff32SQu Wenruo 		return NULL;
16508a9ff32SQu Wenruo 	}
16608a9ff32SQu Wenruo 
16708a9ff32SQu Wenruo 	INIT_LIST_HEAD(&ret->ordered_list);
16808a9ff32SQu Wenruo 	spin_lock_init(&ret->list_lock);
1690bd9289cSQu Wenruo 	spin_lock_init(&ret->thres_lock);
170c3a46891SQu Wenruo 	trace_btrfs_workqueue_alloc(ret, name, flags & WQ_HIGHPRI);
17108a9ff32SQu Wenruo 	return ret;
17208a9ff32SQu Wenruo }
17308a9ff32SQu Wenruo 
1741ca08976SQu Wenruo static inline void
175d458b054SQu Wenruo __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq);
1761ca08976SQu Wenruo 
177cb001095SJeff Mahoney struct btrfs_workqueue *btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info,
178cb001095SJeff Mahoney 					      const char *name,
1796f011058SDavid Sterba 					      unsigned int flags,
180c6dd6ea5SQu Wenruo 					      int limit_active,
1810bd9289cSQu Wenruo 					      int thresh)
1821ca08976SQu Wenruo {
18361dd5ae6SDavid Sterba 	struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_KERNEL);
1841ca08976SQu Wenruo 
1855d99a998SDavid Sterba 	if (!ret)
1861ca08976SQu Wenruo 		return NULL;
1871ca08976SQu Wenruo 
188cb001095SJeff Mahoney 	ret->normal = __btrfs_alloc_workqueue(fs_info, name,
189cb001095SJeff Mahoney 					      flags & ~WQ_HIGHPRI,
190c6dd6ea5SQu Wenruo 					      limit_active, thresh);
1915d99a998SDavid Sterba 	if (!ret->normal) {
1921ca08976SQu Wenruo 		kfree(ret);
1931ca08976SQu Wenruo 		return NULL;
1941ca08976SQu Wenruo 	}
1951ca08976SQu Wenruo 
1961ca08976SQu Wenruo 	if (flags & WQ_HIGHPRI) {
197cb001095SJeff Mahoney 		ret->high = __btrfs_alloc_workqueue(fs_info, name, flags,
198cb001095SJeff Mahoney 						    limit_active, thresh);
1995d99a998SDavid Sterba 		if (!ret->high) {
2001ca08976SQu Wenruo 			__btrfs_destroy_workqueue(ret->normal);
2011ca08976SQu Wenruo 			kfree(ret);
2021ca08976SQu Wenruo 			return NULL;
2031ca08976SQu Wenruo 		}
2041ca08976SQu Wenruo 	}
2051ca08976SQu Wenruo 	return ret;
2061ca08976SQu Wenruo }
2071ca08976SQu Wenruo 
2080bd9289cSQu Wenruo /*
2090bd9289cSQu Wenruo  * Hook for threshold which will be called in btrfs_queue_work.
2100bd9289cSQu Wenruo  * This hook WILL be called in IRQ handler context,
2110bd9289cSQu Wenruo  * so workqueue_set_max_active MUST NOT be called in this hook
2120bd9289cSQu Wenruo  */
213d458b054SQu Wenruo static inline void thresh_queue_hook(struct __btrfs_workqueue *wq)
2140bd9289cSQu Wenruo {
2150bd9289cSQu Wenruo 	if (wq->thresh == NO_THRESHOLD)
2160bd9289cSQu Wenruo 		return;
2170bd9289cSQu Wenruo 	atomic_inc(&wq->pending);
2180bd9289cSQu Wenruo }
2190bd9289cSQu Wenruo 
2200bd9289cSQu Wenruo /*
2210bd9289cSQu Wenruo  * Hook for threshold which will be called before executing the work,
2220bd9289cSQu Wenruo  * This hook is called in kthread content.
2230bd9289cSQu Wenruo  * So workqueue_set_max_active is called here.
2240bd9289cSQu Wenruo  */
225d458b054SQu Wenruo static inline void thresh_exec_hook(struct __btrfs_workqueue *wq)
2260bd9289cSQu Wenruo {
227c6dd6ea5SQu Wenruo 	int new_current_active;
2280bd9289cSQu Wenruo 	long pending;
2290bd9289cSQu Wenruo 	int need_change = 0;
2300bd9289cSQu Wenruo 
2310bd9289cSQu Wenruo 	if (wq->thresh == NO_THRESHOLD)
2320bd9289cSQu Wenruo 		return;
2330bd9289cSQu Wenruo 
2340bd9289cSQu Wenruo 	atomic_dec(&wq->pending);
2350bd9289cSQu Wenruo 	spin_lock(&wq->thres_lock);
2360bd9289cSQu Wenruo 	/*
2370bd9289cSQu Wenruo 	 * Use wq->count to limit the calling frequency of
2380bd9289cSQu Wenruo 	 * workqueue_set_max_active.
2390bd9289cSQu Wenruo 	 */
2400bd9289cSQu Wenruo 	wq->count++;
2410bd9289cSQu Wenruo 	wq->count %= (wq->thresh / 4);
2420bd9289cSQu Wenruo 	if (!wq->count)
2430bd9289cSQu Wenruo 		goto  out;
244c6dd6ea5SQu Wenruo 	new_current_active = wq->current_active;
2450bd9289cSQu Wenruo 
2460bd9289cSQu Wenruo 	/*
2470bd9289cSQu Wenruo 	 * pending may be changed later, but it's OK since we really
2480bd9289cSQu Wenruo 	 * don't need it so accurate to calculate new_max_active.
2490bd9289cSQu Wenruo 	 */
2500bd9289cSQu Wenruo 	pending = atomic_read(&wq->pending);
2510bd9289cSQu Wenruo 	if (pending > wq->thresh)
252c6dd6ea5SQu Wenruo 		new_current_active++;
2530bd9289cSQu Wenruo 	if (pending < wq->thresh / 2)
254c6dd6ea5SQu Wenruo 		new_current_active--;
255c6dd6ea5SQu Wenruo 	new_current_active = clamp_val(new_current_active, 1, wq->limit_active);
256c6dd6ea5SQu Wenruo 	if (new_current_active != wq->current_active)  {
2570bd9289cSQu Wenruo 		need_change = 1;
258c6dd6ea5SQu Wenruo 		wq->current_active = new_current_active;
2590bd9289cSQu Wenruo 	}
2600bd9289cSQu Wenruo out:
2610bd9289cSQu Wenruo 	spin_unlock(&wq->thres_lock);
2620bd9289cSQu Wenruo 
2630bd9289cSQu Wenruo 	if (need_change) {
264c6dd6ea5SQu Wenruo 		workqueue_set_max_active(wq->normal_wq, wq->current_active);
2650bd9289cSQu Wenruo 	}
2660bd9289cSQu Wenruo }
2670bd9289cSQu Wenruo 
268d458b054SQu Wenruo static void run_ordered_work(struct __btrfs_workqueue *wq)
26908a9ff32SQu Wenruo {
27008a9ff32SQu Wenruo 	struct list_head *list = &wq->ordered_list;
271d458b054SQu Wenruo 	struct btrfs_work *work;
27208a9ff32SQu Wenruo 	spinlock_t *lock = &wq->list_lock;
27308a9ff32SQu Wenruo 	unsigned long flags;
27408a9ff32SQu Wenruo 
27508a9ff32SQu Wenruo 	while (1) {
27608a9ff32SQu Wenruo 		spin_lock_irqsave(lock, flags);
27708a9ff32SQu Wenruo 		if (list_empty(list))
27808a9ff32SQu Wenruo 			break;
279d458b054SQu Wenruo 		work = list_entry(list->next, struct btrfs_work,
28008a9ff32SQu Wenruo 				  ordered_list);
28108a9ff32SQu Wenruo 		if (!test_bit(WORK_DONE_BIT, &work->flags))
28208a9ff32SQu Wenruo 			break;
28308a9ff32SQu Wenruo 
28408a9ff32SQu Wenruo 		/*
28508a9ff32SQu Wenruo 		 * we are going to call the ordered done function, but
28608a9ff32SQu Wenruo 		 * we leave the work item on the list as a barrier so
28708a9ff32SQu Wenruo 		 * that later work items that are done don't have their
28808a9ff32SQu Wenruo 		 * functions called before this one returns
28908a9ff32SQu Wenruo 		 */
29008a9ff32SQu Wenruo 		if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
29108a9ff32SQu Wenruo 			break;
29252483bc2SQu Wenruo 		trace_btrfs_ordered_sched(work);
29308a9ff32SQu Wenruo 		spin_unlock_irqrestore(lock, flags);
29408a9ff32SQu Wenruo 		work->ordered_func(work);
29508a9ff32SQu Wenruo 
29608a9ff32SQu Wenruo 		/* now take the lock again and drop our item from the list */
29708a9ff32SQu Wenruo 		spin_lock_irqsave(lock, flags);
29808a9ff32SQu Wenruo 		list_del(&work->ordered_list);
29908a9ff32SQu Wenruo 		spin_unlock_irqrestore(lock, flags);
30008a9ff32SQu Wenruo 
30108a9ff32SQu Wenruo 		/*
30208a9ff32SQu Wenruo 		 * we don't want to call the ordered free functions
30308a9ff32SQu Wenruo 		 * with the lock held though
30408a9ff32SQu Wenruo 		 */
30508a9ff32SQu Wenruo 		work->ordered_free(work);
30652483bc2SQu Wenruo 		trace_btrfs_all_work_done(work);
30708a9ff32SQu Wenruo 	}
30808a9ff32SQu Wenruo 	spin_unlock_irqrestore(lock, flags);
30908a9ff32SQu Wenruo }
31008a9ff32SQu Wenruo 
3119e0af237SLiu Bo static void normal_work_helper(struct btrfs_work *work)
31208a9ff32SQu Wenruo {
313d458b054SQu Wenruo 	struct __btrfs_workqueue *wq;
31408a9ff32SQu Wenruo 	int need_order = 0;
31508a9ff32SQu Wenruo 
31608a9ff32SQu Wenruo 	/*
31708a9ff32SQu Wenruo 	 * We should not touch things inside work in the following cases:
31808a9ff32SQu Wenruo 	 * 1) after work->func() if it has no ordered_free
31908a9ff32SQu Wenruo 	 *    Since the struct is freed in work->func().
32008a9ff32SQu Wenruo 	 * 2) after setting WORK_DONE_BIT
32108a9ff32SQu Wenruo 	 *    The work may be freed in other threads almost instantly.
32208a9ff32SQu Wenruo 	 * So we save the needed things here.
32308a9ff32SQu Wenruo 	 */
32408a9ff32SQu Wenruo 	if (work->ordered_func)
32508a9ff32SQu Wenruo 		need_order = 1;
32608a9ff32SQu Wenruo 	wq = work->wq;
32708a9ff32SQu Wenruo 
32852483bc2SQu Wenruo 	trace_btrfs_work_sched(work);
3290bd9289cSQu Wenruo 	thresh_exec_hook(wq);
33008a9ff32SQu Wenruo 	work->func(work);
33108a9ff32SQu Wenruo 	if (need_order) {
33208a9ff32SQu Wenruo 		set_bit(WORK_DONE_BIT, &work->flags);
33308a9ff32SQu Wenruo 		run_ordered_work(wq);
33408a9ff32SQu Wenruo 	}
33552483bc2SQu Wenruo 	if (!need_order)
33652483bc2SQu Wenruo 		trace_btrfs_all_work_done(work);
33708a9ff32SQu Wenruo }
33808a9ff32SQu Wenruo 
3399e0af237SLiu Bo void btrfs_init_work(struct btrfs_work *work, btrfs_work_func_t uniq_func,
3406db8914fSQu Wenruo 		     btrfs_func_t func,
3416db8914fSQu Wenruo 		     btrfs_func_t ordered_func,
3426db8914fSQu Wenruo 		     btrfs_func_t ordered_free)
34308a9ff32SQu Wenruo {
34408a9ff32SQu Wenruo 	work->func = func;
34508a9ff32SQu Wenruo 	work->ordered_func = ordered_func;
34608a9ff32SQu Wenruo 	work->ordered_free = ordered_free;
3479e0af237SLiu Bo 	INIT_WORK(&work->normal_work, uniq_func);
34808a9ff32SQu Wenruo 	INIT_LIST_HEAD(&work->ordered_list);
34908a9ff32SQu Wenruo 	work->flags = 0;
35008a9ff32SQu Wenruo }
35108a9ff32SQu Wenruo 
352d458b054SQu Wenruo static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq,
353d458b054SQu Wenruo 				      struct btrfs_work *work)
35408a9ff32SQu Wenruo {
35508a9ff32SQu Wenruo 	unsigned long flags;
35608a9ff32SQu Wenruo 
35708a9ff32SQu Wenruo 	work->wq = wq;
3580bd9289cSQu Wenruo 	thresh_queue_hook(wq);
35908a9ff32SQu Wenruo 	if (work->ordered_func) {
36008a9ff32SQu Wenruo 		spin_lock_irqsave(&wq->list_lock, flags);
36108a9ff32SQu Wenruo 		list_add_tail(&work->ordered_list, &wq->ordered_list);
36208a9ff32SQu Wenruo 		spin_unlock_irqrestore(&wq->list_lock, flags);
36308a9ff32SQu Wenruo 	}
36452483bc2SQu Wenruo 	trace_btrfs_work_queued(work);
3650a95b851SQu Wenruo 	queue_work(wq->normal_wq, &work->normal_work);
36608a9ff32SQu Wenruo }
36708a9ff32SQu Wenruo 
368d458b054SQu Wenruo void btrfs_queue_work(struct btrfs_workqueue *wq,
369d458b054SQu Wenruo 		      struct btrfs_work *work)
3701ca08976SQu Wenruo {
371d458b054SQu Wenruo 	struct __btrfs_workqueue *dest_wq;
3721ca08976SQu Wenruo 
3731ca08976SQu Wenruo 	if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags) && wq->high)
3741ca08976SQu Wenruo 		dest_wq = wq->high;
3751ca08976SQu Wenruo 	else
3761ca08976SQu Wenruo 		dest_wq = wq->normal;
3771ca08976SQu Wenruo 	__btrfs_queue_work(dest_wq, work);
3781ca08976SQu Wenruo }
3791ca08976SQu Wenruo 
3801ca08976SQu Wenruo static inline void
381d458b054SQu Wenruo __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq)
38208a9ff32SQu Wenruo {
38308a9ff32SQu Wenruo 	destroy_workqueue(wq->normal_wq);
384c3a46891SQu Wenruo 	trace_btrfs_workqueue_destroy(wq);
38508a9ff32SQu Wenruo 	kfree(wq);
38608a9ff32SQu Wenruo }
38708a9ff32SQu Wenruo 
388d458b054SQu Wenruo void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
3891ca08976SQu Wenruo {
3901ca08976SQu Wenruo 	if (!wq)
3911ca08976SQu Wenruo 		return;
3921ca08976SQu Wenruo 	if (wq->high)
3931ca08976SQu Wenruo 		__btrfs_destroy_workqueue(wq->high);
3941ca08976SQu Wenruo 	__btrfs_destroy_workqueue(wq->normal);
395ef66af10SFilipe Manana 	kfree(wq);
3961ca08976SQu Wenruo }
3971ca08976SQu Wenruo 
398c6dd6ea5SQu Wenruo void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int limit_active)
39908a9ff32SQu Wenruo {
400800ee224SSergei Trofimovich 	if (!wq)
401800ee224SSergei Trofimovich 		return;
402c6dd6ea5SQu Wenruo 	wq->normal->limit_active = limit_active;
4031ca08976SQu Wenruo 	if (wq->high)
404c6dd6ea5SQu Wenruo 		wq->high->limit_active = limit_active;
4051ca08976SQu Wenruo }
4061ca08976SQu Wenruo 
407d458b054SQu Wenruo void btrfs_set_work_high_priority(struct btrfs_work *work)
4081ca08976SQu Wenruo {
4091ca08976SQu Wenruo 	set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
41008a9ff32SQu Wenruo }
411