blob: d8c07e5c1f24bbc24c1dcd62ad125d9734849e5d [file] [log] [blame]
Chris Mason8b712842008-06-11 16:50:36 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
Qu Wenruo08a9ff32014-02-28 10:46:03 +08003 * Copyright (C) 2014 Fujitsu. All rights reserved.
Chris Mason8b712842008-06-11 16:50:36 -04004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 021110-1307, USA.
18 */
19
20#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Chris Mason8b712842008-06-11 16:50:36 -040022#include <linux/list.h>
23#include <linux/spinlock.h>
Chris Masonb51912c2009-02-04 09:23:24 -050024#include <linux/freezer.h>
Qu Wenruo08a9ff32014-02-28 10:46:03 +080025#include <linux/workqueue.h>
Chris Mason8b712842008-06-11 16:50:36 -040026#include "async-thread.h"
27
Qu Wenruoa046e9c2014-02-28 10:46:18 +080028#define WORK_DONE_BIT 0
29#define WORK_ORDER_DONE_BIT 1
30#define WORK_HIGH_PRIO_BIT 2
Chris Mason4a69a412008-11-06 22:03:00 -050031
Qu Wenruo0bd92892014-02-28 10:46:05 +080032#define NO_THRESHOLD (-1)
33#define DFT_THRESHOLD (32)
34
Qu Wenruod458b052014-02-28 10:46:19 +080035struct __btrfs_workqueue {
Qu Wenruo08a9ff32014-02-28 10:46:03 +080036 struct workqueue_struct *normal_wq;
37 /* List head pointing to ordered work list */
38 struct list_head ordered_list;
39
40 /* Spinlock for ordered_list */
41 spinlock_t list_lock;
Qu Wenruo0bd92892014-02-28 10:46:05 +080042
43 /* Thresholding related variants */
44 atomic_t pending;
45 int max_active;
46 int current_max;
47 int thresh;
48 unsigned int count;
49 spinlock_t thres_lock;
Qu Wenruo08a9ff32014-02-28 10:46:03 +080050};
51
Qu Wenruod458b052014-02-28 10:46:19 +080052struct btrfs_workqueue {
53 struct __btrfs_workqueue *normal;
54 struct __btrfs_workqueue *high;
Qu Wenruo1ca08972014-02-28 10:46:04 +080055};
56
Qu Wenruod458b052014-02-28 10:46:19 +080057static inline struct __btrfs_workqueue
Qu Wenruo0bd92892014-02-28 10:46:05 +080058*__btrfs_alloc_workqueue(char *name, int flags, int max_active, int thresh)
Qu Wenruo08a9ff32014-02-28 10:46:03 +080059{
Qu Wenruod458b052014-02-28 10:46:19 +080060 struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
Qu Wenruo08a9ff32014-02-28 10:46:03 +080061
62 if (unlikely(!ret))
63 return NULL;
64
Qu Wenruo0bd92892014-02-28 10:46:05 +080065 ret->max_active = max_active;
66 atomic_set(&ret->pending, 0);
67 if (thresh == 0)
68 thresh = DFT_THRESHOLD;
69 /* For low threshold, disabling threshold is a better choice */
70 if (thresh < DFT_THRESHOLD) {
71 ret->current_max = max_active;
72 ret->thresh = NO_THRESHOLD;
73 } else {
74 ret->current_max = 1;
75 ret->thresh = thresh;
76 }
77
Qu Wenruo1ca08972014-02-28 10:46:04 +080078 if (flags & WQ_HIGHPRI)
79 ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
Qu Wenruo0bd92892014-02-28 10:46:05 +080080 ret->max_active,
81 "btrfs", name);
Qu Wenruo1ca08972014-02-28 10:46:04 +080082 else
83 ret->normal_wq = alloc_workqueue("%s-%s", flags,
Qu Wenruo0bd92892014-02-28 10:46:05 +080084 ret->max_active, "btrfs",
85 name);
Qu Wenruo08a9ff32014-02-28 10:46:03 +080086 if (unlikely(!ret->normal_wq)) {
87 kfree(ret);
88 return NULL;
89 }
90
91 INIT_LIST_HEAD(&ret->ordered_list);
92 spin_lock_init(&ret->list_lock);
Qu Wenruo0bd92892014-02-28 10:46:05 +080093 spin_lock_init(&ret->thres_lock);
Qu Wenruo08a9ff32014-02-28 10:46:03 +080094 return ret;
95}
96
Qu Wenruo1ca08972014-02-28 10:46:04 +080097static inline void
Qu Wenruod458b052014-02-28 10:46:19 +080098__btrfs_destroy_workqueue(struct __btrfs_workqueue *wq);
Qu Wenruo1ca08972014-02-28 10:46:04 +080099
Qu Wenruod458b052014-02-28 10:46:19 +0800100struct btrfs_workqueue *btrfs_alloc_workqueue(char *name,
101 int flags,
102 int max_active,
103 int thresh)
Qu Wenruo1ca08972014-02-28 10:46:04 +0800104{
Qu Wenruod458b052014-02-28 10:46:19 +0800105 struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
Qu Wenruo1ca08972014-02-28 10:46:04 +0800106
107 if (unlikely(!ret))
108 return NULL;
109
110 ret->normal = __btrfs_alloc_workqueue(name, flags & ~WQ_HIGHPRI,
Qu Wenruo0bd92892014-02-28 10:46:05 +0800111 max_active, thresh);
Qu Wenruo1ca08972014-02-28 10:46:04 +0800112 if (unlikely(!ret->normal)) {
113 kfree(ret);
114 return NULL;
115 }
116
117 if (flags & WQ_HIGHPRI) {
Qu Wenruo0bd92892014-02-28 10:46:05 +0800118 ret->high = __btrfs_alloc_workqueue(name, flags, max_active,
119 thresh);
Qu Wenruo1ca08972014-02-28 10:46:04 +0800120 if (unlikely(!ret->high)) {
121 __btrfs_destroy_workqueue(ret->normal);
122 kfree(ret);
123 return NULL;
124 }
125 }
126 return ret;
127}
128
Qu Wenruo0bd92892014-02-28 10:46:05 +0800129/*
130 * Hook for threshold which will be called in btrfs_queue_work.
131 * This hook WILL be called in IRQ handler context,
132 * so workqueue_set_max_active MUST NOT be called in this hook
133 */
Qu Wenruod458b052014-02-28 10:46:19 +0800134static inline void thresh_queue_hook(struct __btrfs_workqueue *wq)
Qu Wenruo0bd92892014-02-28 10:46:05 +0800135{
136 if (wq->thresh == NO_THRESHOLD)
137 return;
138 atomic_inc(&wq->pending);
139}
140
141/*
142 * Hook for threshold which will be called before executing the work,
143 * This hook is called in kthread content.
144 * So workqueue_set_max_active is called here.
145 */
Qu Wenruod458b052014-02-28 10:46:19 +0800146static inline void thresh_exec_hook(struct __btrfs_workqueue *wq)
Qu Wenruo0bd92892014-02-28 10:46:05 +0800147{
148 int new_max_active;
149 long pending;
150 int need_change = 0;
151
152 if (wq->thresh == NO_THRESHOLD)
153 return;
154
155 atomic_dec(&wq->pending);
156 spin_lock(&wq->thres_lock);
157 /*
158 * Use wq->count to limit the calling frequency of
159 * workqueue_set_max_active.
160 */
161 wq->count++;
162 wq->count %= (wq->thresh / 4);
163 if (!wq->count)
164 goto out;
165 new_max_active = wq->current_max;
166
167 /*
168 * pending may be changed later, but it's OK since we really
169 * don't need it so accurate to calculate new_max_active.
170 */
171 pending = atomic_read(&wq->pending);
172 if (pending > wq->thresh)
173 new_max_active++;
174 if (pending < wq->thresh / 2)
175 new_max_active--;
176 new_max_active = clamp_val(new_max_active, 1, wq->max_active);
177 if (new_max_active != wq->current_max) {
178 need_change = 1;
179 wq->current_max = new_max_active;
180 }
181out:
182 spin_unlock(&wq->thres_lock);
183
184 if (need_change) {
185 workqueue_set_max_active(wq->normal_wq, wq->current_max);
186 }
187}
188
Qu Wenruod458b052014-02-28 10:46:19 +0800189static void run_ordered_work(struct __btrfs_workqueue *wq)
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800190{
191 struct list_head *list = &wq->ordered_list;
Qu Wenruod458b052014-02-28 10:46:19 +0800192 struct btrfs_work *work;
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800193 spinlock_t *lock = &wq->list_lock;
194 unsigned long flags;
195
196 while (1) {
197 spin_lock_irqsave(lock, flags);
198 if (list_empty(list))
199 break;
Qu Wenruod458b052014-02-28 10:46:19 +0800200 work = list_entry(list->next, struct btrfs_work,
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800201 ordered_list);
202 if (!test_bit(WORK_DONE_BIT, &work->flags))
203 break;
204
205 /*
206 * we are going to call the ordered done function, but
207 * we leave the work item on the list as a barrier so
208 * that later work items that are done don't have their
209 * functions called before this one returns
210 */
211 if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
212 break;
213 spin_unlock_irqrestore(lock, flags);
214 work->ordered_func(work);
215
216 /* now take the lock again and drop our item from the list */
217 spin_lock_irqsave(lock, flags);
218 list_del(&work->ordered_list);
219 spin_unlock_irqrestore(lock, flags);
220
221 /*
222 * we don't want to call the ordered free functions
223 * with the lock held though
224 */
225 work->ordered_free(work);
226 }
227 spin_unlock_irqrestore(lock, flags);
228}
229
230static void normal_work_helper(struct work_struct *arg)
231{
Qu Wenruod458b052014-02-28 10:46:19 +0800232 struct btrfs_work *work;
233 struct __btrfs_workqueue *wq;
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800234 int need_order = 0;
235
Qu Wenruod458b052014-02-28 10:46:19 +0800236 work = container_of(arg, struct btrfs_work, normal_work);
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800237 /*
238 * We should not touch things inside work in the following cases:
239 * 1) after work->func() if it has no ordered_free
240 * Since the struct is freed in work->func().
241 * 2) after setting WORK_DONE_BIT
242 * The work may be freed in other threads almost instantly.
243 * So we save the needed things here.
244 */
245 if (work->ordered_func)
246 need_order = 1;
247 wq = work->wq;
248
Qu Wenruo0bd92892014-02-28 10:46:05 +0800249 thresh_exec_hook(wq);
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800250 work->func(work);
251 if (need_order) {
252 set_bit(WORK_DONE_BIT, &work->flags);
253 run_ordered_work(wq);
254 }
255}
256
Qu Wenruod458b052014-02-28 10:46:19 +0800257void btrfs_init_work(struct btrfs_work *work,
Qu Wenruo6db89142014-03-06 04:19:50 +0000258 btrfs_func_t func,
259 btrfs_func_t ordered_func,
260 btrfs_func_t ordered_free)
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800261{
262 work->func = func;
263 work->ordered_func = ordered_func;
264 work->ordered_free = ordered_free;
265 INIT_WORK(&work->normal_work, normal_work_helper);
266 INIT_LIST_HEAD(&work->ordered_list);
267 work->flags = 0;
268}
269
Qu Wenruod458b052014-02-28 10:46:19 +0800270static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq,
271 struct btrfs_work *work)
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800272{
273 unsigned long flags;
274
275 work->wq = wq;
Qu Wenruo0bd92892014-02-28 10:46:05 +0800276 thresh_queue_hook(wq);
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800277 if (work->ordered_func) {
278 spin_lock_irqsave(&wq->list_lock, flags);
279 list_add_tail(&work->ordered_list, &wq->ordered_list);
280 spin_unlock_irqrestore(&wq->list_lock, flags);
281 }
282 queue_work(wq->normal_wq, &work->normal_work);
283}
284
Qu Wenruod458b052014-02-28 10:46:19 +0800285void btrfs_queue_work(struct btrfs_workqueue *wq,
286 struct btrfs_work *work)
Qu Wenruo1ca08972014-02-28 10:46:04 +0800287{
Qu Wenruod458b052014-02-28 10:46:19 +0800288 struct __btrfs_workqueue *dest_wq;
Qu Wenruo1ca08972014-02-28 10:46:04 +0800289
290 if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags) && wq->high)
291 dest_wq = wq->high;
292 else
293 dest_wq = wq->normal;
294 __btrfs_queue_work(dest_wq, work);
295}
296
297static inline void
Qu Wenruod458b052014-02-28 10:46:19 +0800298__btrfs_destroy_workqueue(struct __btrfs_workqueue *wq)
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800299{
300 destroy_workqueue(wq->normal_wq);
301 kfree(wq);
302}
303
Qu Wenruod458b052014-02-28 10:46:19 +0800304void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
Qu Wenruo1ca08972014-02-28 10:46:04 +0800305{
306 if (!wq)
307 return;
308 if (wq->high)
309 __btrfs_destroy_workqueue(wq->high);
310 __btrfs_destroy_workqueue(wq->normal);
311}
312
Qu Wenruod458b052014-02-28 10:46:19 +0800313void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int max)
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800314{
Qu Wenruo0bd92892014-02-28 10:46:05 +0800315 wq->normal->max_active = max;
Qu Wenruo1ca08972014-02-28 10:46:04 +0800316 if (wq->high)
Qu Wenruo0bd92892014-02-28 10:46:05 +0800317 wq->high->max_active = max;
Qu Wenruo1ca08972014-02-28 10:46:04 +0800318}
319
Qu Wenruod458b052014-02-28 10:46:19 +0800320void btrfs_set_work_high_priority(struct btrfs_work *work)
Qu Wenruo1ca08972014-02-28 10:46:04 +0800321{
322 set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
Qu Wenruo08a9ff32014-02-28 10:46:03 +0800323}