btrfs: Cleanup the "_struct" suffix in btrfs_workequeue
[cascardo/linux.git] / fs / btrfs / async-thread.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  * Copyright (C) 2014 Fujitsu.  All rights reserved.
4  *
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>
21 #include <linux/slab.h>
22 #include <linux/list.h>
23 #include <linux/spinlock.h>
24 #include <linux/freezer.h>
25 #include <linux/workqueue.h>
26 #include "async-thread.h"
27
28 #define WORK_DONE_BIT 0
29 #define WORK_ORDER_DONE_BIT 1
30 #define WORK_HIGH_PRIO_BIT 2
31
32 #define NO_THRESHOLD (-1)
33 #define DFT_THRESHOLD (32)
34
35 struct __btrfs_workqueue {
36         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;
42
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;
50 };
51
52 struct btrfs_workqueue {
53         struct __btrfs_workqueue *normal;
54         struct __btrfs_workqueue *high;
55 };
56
57 static inline struct __btrfs_workqueue
58 *__btrfs_alloc_workqueue(char *name, int flags, int max_active, int thresh)
59 {
60         struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
61
62         if (unlikely(!ret))
63                 return NULL;
64
65         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
78         if (flags & WQ_HIGHPRI)
79                 ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
80                                                  ret->max_active,
81                                                  "btrfs", name);
82         else
83                 ret->normal_wq = alloc_workqueue("%s-%s", flags,
84                                                  ret->max_active, "btrfs",
85                                                  name);
86         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);
93         spin_lock_init(&ret->thres_lock);
94         return ret;
95 }
96
97 static inline void
98 __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq);
99
100 struct btrfs_workqueue *btrfs_alloc_workqueue(char *name,
101                                               int flags,
102                                               int max_active,
103                                               int thresh)
104 {
105         struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
106
107         if (unlikely(!ret))
108                 return NULL;
109
110         ret->normal = __btrfs_alloc_workqueue(name, flags & ~WQ_HIGHPRI,
111                                               max_active, thresh);
112         if (unlikely(!ret->normal)) {
113                 kfree(ret);
114                 return NULL;
115         }
116
117         if (flags & WQ_HIGHPRI) {
118                 ret->high = __btrfs_alloc_workqueue(name, flags, max_active,
119                                                     thresh);
120                 if (unlikely(!ret->high)) {
121                         __btrfs_destroy_workqueue(ret->normal);
122                         kfree(ret);
123                         return NULL;
124                 }
125         }
126         return ret;
127 }
128
129 /*
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  */
134 static inline void thresh_queue_hook(struct __btrfs_workqueue *wq)
135 {
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  */
146 static inline void thresh_exec_hook(struct __btrfs_workqueue *wq)
147 {
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         }
181 out:
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
189 static void run_ordered_work(struct __btrfs_workqueue *wq)
190 {
191         struct list_head *list = &wq->ordered_list;
192         struct btrfs_work *work;
193         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;
200                 work = list_entry(list->next, struct btrfs_work,
201                                   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
230 static void normal_work_helper(struct work_struct *arg)
231 {
232         struct btrfs_work *work;
233         struct __btrfs_workqueue *wq;
234         int need_order = 0;
235
236         work = container_of(arg, struct btrfs_work, normal_work);
237         /*
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
249         thresh_exec_hook(wq);
250         work->func(work);
251         if (need_order) {
252                 set_bit(WORK_DONE_BIT, &work->flags);
253                 run_ordered_work(wq);
254         }
255 }
256
257 void btrfs_init_work(struct btrfs_work *work,
258                      void (*func)(struct btrfs_work *),
259                      void (*ordered_func)(struct btrfs_work *),
260                      void (*ordered_free)(struct btrfs_work *))
261 {
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
270 static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq,
271                                       struct btrfs_work *work)
272 {
273         unsigned long flags;
274
275         work->wq = wq;
276         thresh_queue_hook(wq);
277         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
285 void btrfs_queue_work(struct btrfs_workqueue *wq,
286                       struct btrfs_work *work)
287 {
288         struct __btrfs_workqueue *dest_wq;
289
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
297 static inline void
298 __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq)
299 {
300         destroy_workqueue(wq->normal_wq);
301         kfree(wq);
302 }
303
304 void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
305 {
306         if (!wq)
307                 return;
308         if (wq->high)
309                 __btrfs_destroy_workqueue(wq->high);
310         __btrfs_destroy_workqueue(wq->normal);
311 }
312
313 void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int max)
314 {
315         wq->normal->max_active = max;
316         if (wq->high)
317                 wq->high->max_active = max;
318 }
319
320 void btrfs_set_work_high_priority(struct btrfs_work *work)
321 {
322         set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
323 }