Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 3 | * Copyright (C) 2014 Fujitsu. All rights reserved. |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 22 | #include <linux/list.h> |
| 23 | #include <linux/spinlock.h> |
Chris Mason | b51912c | 2009-02-04 09:23:24 -0500 | [diff] [blame] | 24 | #include <linux/freezer.h> |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 25 | #include <linux/workqueue.h> |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 26 | #include "async-thread.h" |
| 27 | |
Qu Wenruo | a046e9c | 2014-02-28 10:46:18 +0800 | [diff] [blame] | 28 | #define WORK_DONE_BIT 0 |
| 29 | #define WORK_ORDER_DONE_BIT 1 |
| 30 | #define WORK_HIGH_PRIO_BIT 2 |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 31 | |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 32 | #define NO_THRESHOLD (-1) |
| 33 | #define DFT_THRESHOLD (32) |
| 34 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 35 | struct __btrfs_workqueue { |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 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; |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 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; |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 50 | }; |
| 51 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 52 | struct btrfs_workqueue { |
| 53 | struct __btrfs_workqueue *normal; |
| 54 | struct __btrfs_workqueue *high; |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 55 | }; |
| 56 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 57 | static inline struct __btrfs_workqueue |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 58 | *__btrfs_alloc_workqueue(char *name, int flags, int max_active, int thresh) |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 59 | { |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 60 | struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS); |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 61 | |
| 62 | if (unlikely(!ret)) |
| 63 | return NULL; |
| 64 | |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 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 | |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 78 | if (flags & WQ_HIGHPRI) |
| 79 | ret->normal_wq = alloc_workqueue("%s-%s-high", flags, |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 80 | ret->max_active, |
| 81 | "btrfs", name); |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 82 | else |
| 83 | ret->normal_wq = alloc_workqueue("%s-%s", flags, |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 84 | ret->max_active, "btrfs", |
| 85 | name); |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 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); |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 93 | spin_lock_init(&ret->thres_lock); |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 94 | return ret; |
| 95 | } |
| 96 | |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 97 | static inline void |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 98 | __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq); |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 99 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 100 | struct btrfs_workqueue *btrfs_alloc_workqueue(char *name, |
| 101 | int flags, |
| 102 | int max_active, |
| 103 | int thresh) |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 104 | { |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 105 | struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS); |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 106 | |
| 107 | if (unlikely(!ret)) |
| 108 | return NULL; |
| 109 | |
| 110 | ret->normal = __btrfs_alloc_workqueue(name, flags & ~WQ_HIGHPRI, |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 111 | max_active, thresh); |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 112 | if (unlikely(!ret->normal)) { |
| 113 | kfree(ret); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | if (flags & WQ_HIGHPRI) { |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 118 | ret->high = __btrfs_alloc_workqueue(name, flags, max_active, |
| 119 | thresh); |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 120 | if (unlikely(!ret->high)) { |
| 121 | __btrfs_destroy_workqueue(ret->normal); |
| 122 | kfree(ret); |
| 123 | return NULL; |
| 124 | } |
| 125 | } |
| 126 | return ret; |
| 127 | } |
| 128 | |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 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 | */ |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 134 | static inline void thresh_queue_hook(struct __btrfs_workqueue *wq) |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 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 | */ |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 146 | static inline void thresh_exec_hook(struct __btrfs_workqueue *wq) |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 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 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 189 | static void run_ordered_work(struct __btrfs_workqueue *wq) |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 190 | { |
| 191 | struct list_head *list = &wq->ordered_list; |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 192 | struct btrfs_work *work; |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 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; |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 200 | work = list_entry(list->next, struct btrfs_work, |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 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 | { |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 232 | struct btrfs_work *work; |
| 233 | struct __btrfs_workqueue *wq; |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 234 | int need_order = 0; |
| 235 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 236 | work = container_of(arg, struct btrfs_work, normal_work); |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 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 | |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 249 | thresh_exec_hook(wq); |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 250 | work->func(work); |
| 251 | if (need_order) { |
| 252 | set_bit(WORK_DONE_BIT, &work->flags); |
| 253 | run_ordered_work(wq); |
| 254 | } |
| 255 | } |
| 256 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 257 | void btrfs_init_work(struct btrfs_work *work, |
Qu Wenruo | 6db8914 | 2014-03-06 04:19:50 +0000 | [diff] [blame^] | 258 | btrfs_func_t func, |
| 259 | btrfs_func_t ordered_func, |
| 260 | btrfs_func_t ordered_free) |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 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 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 270 | static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq, |
| 271 | struct btrfs_work *work) |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 272 | { |
| 273 | unsigned long flags; |
| 274 | |
| 275 | work->wq = wq; |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 276 | thresh_queue_hook(wq); |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 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 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 285 | void btrfs_queue_work(struct btrfs_workqueue *wq, |
| 286 | struct btrfs_work *work) |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 287 | { |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 288 | struct __btrfs_workqueue *dest_wq; |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 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 |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 298 | __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq) |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 299 | { |
| 300 | destroy_workqueue(wq->normal_wq); |
| 301 | kfree(wq); |
| 302 | } |
| 303 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 304 | void btrfs_destroy_workqueue(struct btrfs_workqueue *wq) |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 305 | { |
| 306 | if (!wq) |
| 307 | return; |
| 308 | if (wq->high) |
| 309 | __btrfs_destroy_workqueue(wq->high); |
| 310 | __btrfs_destroy_workqueue(wq->normal); |
| 311 | } |
| 312 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 313 | void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int max) |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 314 | { |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 315 | wq->normal->max_active = max; |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 316 | if (wq->high) |
Qu Wenruo | 0bd9289 | 2014-02-28 10:46:05 +0800 | [diff] [blame] | 317 | wq->high->max_active = max; |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 318 | } |
| 319 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 320 | void btrfs_set_work_high_priority(struct btrfs_work *work) |
Qu Wenruo | 1ca0897 | 2014-02-28 10:46:04 +0800 | [diff] [blame] | 321 | { |
| 322 | set_bit(WORK_HIGH_PRIO_BIT, &work->flags); |
Qu Wenruo | 08a9ff3 | 2014-02-28 10:46:03 +0800 | [diff] [blame] | 323 | } |