blob: cba701dba35057d2233f5052b3079f69f7e65909 [file] [log] [blame]
Chris Mason8b712842008-06-11 16:50:36 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kthread.h>
20#include <linux/list.h>
21#include <linux/spinlock.h>
Chris Masonb51912c2009-02-04 09:23:24 -050022#include <linux/freezer.h>
23#include <linux/ftrace.h>
Chris Mason8b712842008-06-11 16:50:36 -040024#include "async-thread.h"
25
Chris Mason4a69a412008-11-06 22:03:00 -050026#define WORK_QUEUED_BIT 0
27#define WORK_DONE_BIT 1
28#define WORK_ORDER_DONE_BIT 2
29
Chris Mason8b712842008-06-11 16:50:36 -040030/*
31 * container for the kthread task pointer and the list of pending work
32 * One of these is allocated per thread.
33 */
34struct btrfs_worker_thread {
Chris Mason35d8ba62008-06-11 20:21:24 -040035 /* pool we belong to */
36 struct btrfs_workers *workers;
37
Chris Mason8b712842008-06-11 16:50:36 -040038 /* list of struct btrfs_work that are waiting for service */
39 struct list_head pending;
40
41 /* list of worker threads from struct btrfs_workers */
42 struct list_head worker_list;
43
44 /* kthread */
45 struct task_struct *task;
46
47 /* number of things on the pending list */
48 atomic_t num_pending;
Chris Mason53863232008-08-15 15:34:18 -040049
Chris Mason4854ddd2008-08-15 15:34:17 -040050 unsigned long sequence;
Chris Mason8b712842008-06-11 16:50:36 -040051
52 /* protects the pending list. */
53 spinlock_t lock;
54
55 /* set to non-zero when this thread is already awake and kicking */
56 int working;
Chris Mason35d8ba62008-06-11 20:21:24 -040057
58 /* are we currently idle */
59 int idle;
Chris Mason8b712842008-06-11 16:50:36 -040060};
61
62/*
Chris Mason35d8ba62008-06-11 20:21:24 -040063 * helper function to move a thread onto the idle list after it
64 * has finished some requests.
65 */
66static void check_idle_worker(struct btrfs_worker_thread *worker)
67{
68 if (!worker->idle && atomic_read(&worker->num_pending) <
69 worker->workers->idle_thresh / 2) {
70 unsigned long flags;
71 spin_lock_irqsave(&worker->workers->lock, flags);
72 worker->idle = 1;
73 list_move(&worker->worker_list, &worker->workers->idle_list);
74 spin_unlock_irqrestore(&worker->workers->lock, flags);
75 }
76}
77
78/*
79 * helper function to move a thread off the idle list after new
80 * pending work is added.
81 */
82static void check_busy_worker(struct btrfs_worker_thread *worker)
83{
84 if (worker->idle && atomic_read(&worker->num_pending) >=
85 worker->workers->idle_thresh) {
86 unsigned long flags;
87 spin_lock_irqsave(&worker->workers->lock, flags);
88 worker->idle = 0;
89 list_move_tail(&worker->worker_list,
90 &worker->workers->worker_list);
91 spin_unlock_irqrestore(&worker->workers->lock, flags);
92 }
93}
94
Chris Mason4a69a412008-11-06 22:03:00 -050095static noinline int run_ordered_completions(struct btrfs_workers *workers,
96 struct btrfs_work *work)
97{
98 unsigned long flags;
99
100 if (!workers->ordered)
101 return 0;
102
103 set_bit(WORK_DONE_BIT, &work->flags);
104
105 spin_lock_irqsave(&workers->lock, flags);
106
Chris Masond3977122009-01-05 21:25:51 -0500107 while (!list_empty(&workers->order_list)) {
Chris Mason4a69a412008-11-06 22:03:00 -0500108 work = list_entry(workers->order_list.next,
109 struct btrfs_work, order_list);
110
111 if (!test_bit(WORK_DONE_BIT, &work->flags))
112 break;
113
114 /* we are going to call the ordered done function, but
115 * we leave the work item on the list as a barrier so
116 * that later work items that are done don't have their
117 * functions called before this one returns
118 */
119 if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
120 break;
121
122 spin_unlock_irqrestore(&workers->lock, flags);
123
124 work->ordered_func(work);
125
126 /* now take the lock again and call the freeing code */
127 spin_lock_irqsave(&workers->lock, flags);
128 list_del(&work->order_list);
129 work->ordered_free(work);
130 }
131
132 spin_unlock_irqrestore(&workers->lock, flags);
133 return 0;
134}
135
Chris Mason35d8ba62008-06-11 20:21:24 -0400136/*
Chris Mason8b712842008-06-11 16:50:36 -0400137 * main loop for servicing work items
138 */
139static int worker_loop(void *arg)
140{
141 struct btrfs_worker_thread *worker = arg;
142 struct list_head *cur;
143 struct btrfs_work *work;
144 do {
145 spin_lock_irq(&worker->lock);
Chris Masonb51912c2009-02-04 09:23:24 -0500146again_locked:
Chris Masond3977122009-01-05 21:25:51 -0500147 while (!list_empty(&worker->pending)) {
Chris Mason8b712842008-06-11 16:50:36 -0400148 cur = worker->pending.next;
149 work = list_entry(cur, struct btrfs_work, list);
150 list_del(&work->list);
Chris Mason4a69a412008-11-06 22:03:00 -0500151 clear_bit(WORK_QUEUED_BIT, &work->flags);
Chris Mason8b712842008-06-11 16:50:36 -0400152
153 work->worker = worker;
154 spin_unlock_irq(&worker->lock);
155
156 work->func(work);
157
158 atomic_dec(&worker->num_pending);
Chris Mason4a69a412008-11-06 22:03:00 -0500159 /*
160 * unless this is an ordered work queue,
161 * 'work' was probably freed by func above.
162 */
163 run_ordered_completions(worker->workers, work);
164
Chris Mason8b712842008-06-11 16:50:36 -0400165 spin_lock_irq(&worker->lock);
Chris Mason35d8ba62008-06-11 20:21:24 -0400166 check_idle_worker(worker);
Chris Mason4a69a412008-11-06 22:03:00 -0500167
Chris Mason8b712842008-06-11 16:50:36 -0400168 }
Chris Mason8b712842008-06-11 16:50:36 -0400169 if (freezing(current)) {
Chris Masonb51912c2009-02-04 09:23:24 -0500170 worker->working = 0;
171 spin_unlock_irq(&worker->lock);
Chris Mason8b712842008-06-11 16:50:36 -0400172 refrigerator();
173 } else {
Chris Mason8b712842008-06-11 16:50:36 -0400174 spin_unlock_irq(&worker->lock);
Chris Masonb51912c2009-02-04 09:23:24 -0500175 if (!kthread_should_stop()) {
176 cpu_relax();
177 /*
178 * we've dropped the lock, did someone else
179 * jump_in?
180 */
181 smp_mb();
182 if (!list_empty(&worker->pending))
183 continue;
184
185 /*
186 * this short schedule allows more work to
187 * come in without the queue functions
188 * needing to go through wake_up_process()
189 *
190 * worker->working is still 1, so nobody
191 * is going to try and wake us up
192 */
193 schedule_timeout(1);
194 smp_mb();
195 if (!list_empty(&worker->pending))
196 continue;
197
Amit Gudb5555f72009-04-02 17:01:27 -0400198 if (kthread_should_stop())
199 break;
200
Chris Masonb51912c2009-02-04 09:23:24 -0500201 /* still no more work?, sleep for real */
202 spin_lock_irq(&worker->lock);
203 set_current_state(TASK_INTERRUPTIBLE);
204 if (!list_empty(&worker->pending))
205 goto again_locked;
206
207 /*
208 * this makes sure we get a wakeup when someone
209 * adds something new to the queue
210 */
211 worker->working = 0;
212 spin_unlock_irq(&worker->lock);
213
Amit Gudb5555f72009-04-02 17:01:27 -0400214 if (!kthread_should_stop())
215 schedule();
Chris Masonb51912c2009-02-04 09:23:24 -0500216 }
Chris Mason8b712842008-06-11 16:50:36 -0400217 __set_current_state(TASK_RUNNING);
218 }
219 } while (!kthread_should_stop());
220 return 0;
221}
222
223/*
224 * this will wait for all the worker threads to shutdown
225 */
226int btrfs_stop_workers(struct btrfs_workers *workers)
227{
228 struct list_head *cur;
229 struct btrfs_worker_thread *worker;
230
Chris Mason35d8ba62008-06-11 20:21:24 -0400231 list_splice_init(&workers->idle_list, &workers->worker_list);
Chris Masond3977122009-01-05 21:25:51 -0500232 while (!list_empty(&workers->worker_list)) {
Chris Mason8b712842008-06-11 16:50:36 -0400233 cur = workers->worker_list.next;
234 worker = list_entry(cur, struct btrfs_worker_thread,
235 worker_list);
236 kthread_stop(worker->task);
237 list_del(&worker->worker_list);
238 kfree(worker);
239 }
240 return 0;
241}
242
243/*
244 * simple init on struct btrfs_workers
245 */
Chris Mason5443be42008-08-15 15:34:16 -0400246void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max)
Chris Mason8b712842008-06-11 16:50:36 -0400247{
248 workers->num_workers = 0;
249 INIT_LIST_HEAD(&workers->worker_list);
Chris Mason35d8ba62008-06-11 20:21:24 -0400250 INIT_LIST_HEAD(&workers->idle_list);
Chris Mason4a69a412008-11-06 22:03:00 -0500251 INIT_LIST_HEAD(&workers->order_list);
Chris Mason8b712842008-06-11 16:50:36 -0400252 spin_lock_init(&workers->lock);
253 workers->max_workers = max;
Chris Mason61b49442008-07-31 15:42:53 -0400254 workers->idle_thresh = 32;
Chris Mason5443be42008-08-15 15:34:16 -0400255 workers->name = name;
Chris Mason4a69a412008-11-06 22:03:00 -0500256 workers->ordered = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400257}
258
259/*
260 * starts new worker threads. This does not enforce the max worker
261 * count in case you need to temporarily go past it.
262 */
263int btrfs_start_workers(struct btrfs_workers *workers, int num_workers)
264{
265 struct btrfs_worker_thread *worker;
266 int ret = 0;
267 int i;
268
269 for (i = 0; i < num_workers; i++) {
270 worker = kzalloc(sizeof(*worker), GFP_NOFS);
271 if (!worker) {
272 ret = -ENOMEM;
273 goto fail;
274 }
275
276 INIT_LIST_HEAD(&worker->pending);
277 INIT_LIST_HEAD(&worker->worker_list);
278 spin_lock_init(&worker->lock);
279 atomic_set(&worker->num_pending, 0);
Chris Mason5443be42008-08-15 15:34:16 -0400280 worker->task = kthread_run(worker_loop, worker,
281 "btrfs-%s-%d", workers->name,
282 workers->num_workers + i);
Chris Mason35d8ba62008-06-11 20:21:24 -0400283 worker->workers = workers;
Chris Mason8b712842008-06-11 16:50:36 -0400284 if (IS_ERR(worker->task)) {
Li Zefan3bf10412008-07-30 09:24:37 -0400285 kfree(worker);
Chris Mason8b712842008-06-11 16:50:36 -0400286 ret = PTR_ERR(worker->task);
287 goto fail;
288 }
289
290 spin_lock_irq(&workers->lock);
Chris Mason35d8ba62008-06-11 20:21:24 -0400291 list_add_tail(&worker->worker_list, &workers->idle_list);
Chris Mason4854ddd2008-08-15 15:34:17 -0400292 worker->idle = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400293 workers->num_workers++;
294 spin_unlock_irq(&workers->lock);
295 }
296 return 0;
297fail:
298 btrfs_stop_workers(workers);
299 return ret;
300}
301
302/*
303 * run through the list and find a worker thread that doesn't have a lot
304 * to do right now. This can return null if we aren't yet at the thread
305 * count limit and all of the threads are busy.
306 */
307static struct btrfs_worker_thread *next_worker(struct btrfs_workers *workers)
308{
309 struct btrfs_worker_thread *worker;
310 struct list_head *next;
Chris Mason8b712842008-06-11 16:50:36 -0400311 int enforce_min = workers->num_workers < workers->max_workers;
312
Chris Mason8b712842008-06-11 16:50:36 -0400313 /*
Chris Mason35d8ba62008-06-11 20:21:24 -0400314 * if we find an idle thread, don't move it to the end of the
315 * idle list. This improves the chance that the next submission
316 * will reuse the same thread, and maybe catch it while it is still
317 * working
Chris Mason8b712842008-06-11 16:50:36 -0400318 */
Chris Mason35d8ba62008-06-11 20:21:24 -0400319 if (!list_empty(&workers->idle_list)) {
320 next = workers->idle_list.next;
Chris Mason8b712842008-06-11 16:50:36 -0400321 worker = list_entry(next, struct btrfs_worker_thread,
322 worker_list);
Chris Mason35d8ba62008-06-11 20:21:24 -0400323 return worker;
Chris Mason8b712842008-06-11 16:50:36 -0400324 }
Chris Mason35d8ba62008-06-11 20:21:24 -0400325 if (enforce_min || list_empty(&workers->worker_list))
326 return NULL;
327
Chris Mason8b712842008-06-11 16:50:36 -0400328 /*
Chris Mason35d8ba62008-06-11 20:21:24 -0400329 * if we pick a busy task, move the task to the end of the list.
Chris Masond352ac62008-09-29 15:18:18 -0400330 * hopefully this will keep things somewhat evenly balanced.
331 * Do the move in batches based on the sequence number. This groups
332 * requests submitted at roughly the same time onto the same worker.
Chris Mason8b712842008-06-11 16:50:36 -0400333 */
Chris Mason35d8ba62008-06-11 20:21:24 -0400334 next = workers->worker_list.next;
335 worker = list_entry(next, struct btrfs_worker_thread, worker_list);
Chris Mason4854ddd2008-08-15 15:34:17 -0400336 atomic_inc(&worker->num_pending);
337 worker->sequence++;
Chris Masond352ac62008-09-29 15:18:18 -0400338
Chris Mason53863232008-08-15 15:34:18 -0400339 if (worker->sequence % workers->idle_thresh == 0)
Chris Mason4854ddd2008-08-15 15:34:17 -0400340 list_move_tail(next, &workers->worker_list);
Chris Mason8b712842008-06-11 16:50:36 -0400341 return worker;
342}
343
Chris Masond352ac62008-09-29 15:18:18 -0400344/*
345 * selects a worker thread to take the next job. This will either find
346 * an idle worker, start a new worker up to the max count, or just return
347 * one of the existing busy workers.
348 */
Chris Mason8b712842008-06-11 16:50:36 -0400349static struct btrfs_worker_thread *find_worker(struct btrfs_workers *workers)
350{
351 struct btrfs_worker_thread *worker;
352 unsigned long flags;
353
354again:
355 spin_lock_irqsave(&workers->lock, flags);
356 worker = next_worker(workers);
357 spin_unlock_irqrestore(&workers->lock, flags);
358
359 if (!worker) {
360 spin_lock_irqsave(&workers->lock, flags);
361 if (workers->num_workers >= workers->max_workers) {
Chris Mason35d8ba62008-06-11 20:21:24 -0400362 struct list_head *fallback = NULL;
Chris Mason8b712842008-06-11 16:50:36 -0400363 /*
364 * we have failed to find any workers, just
365 * return the force one
366 */
Chris Mason35d8ba62008-06-11 20:21:24 -0400367 if (!list_empty(&workers->worker_list))
368 fallback = workers->worker_list.next;
369 if (!list_empty(&workers->idle_list))
370 fallback = workers->idle_list.next;
371 BUG_ON(!fallback);
372 worker = list_entry(fallback,
Chris Mason8b712842008-06-11 16:50:36 -0400373 struct btrfs_worker_thread, worker_list);
374 spin_unlock_irqrestore(&workers->lock, flags);
375 } else {
376 spin_unlock_irqrestore(&workers->lock, flags);
377 /* we're below the limit, start another worker */
378 btrfs_start_workers(workers, 1);
379 goto again;
380 }
381 }
382 return worker;
383}
384
385/*
386 * btrfs_requeue_work just puts the work item back on the tail of the list
387 * it was taken from. It is intended for use with long running work functions
388 * that make some progress and want to give the cpu up for others.
389 */
390int btrfs_requeue_work(struct btrfs_work *work)
391{
392 struct btrfs_worker_thread *worker = work->worker;
393 unsigned long flags;
Chris Masona6837052009-02-04 09:19:41 -0500394 int wake = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400395
Chris Mason4a69a412008-11-06 22:03:00 -0500396 if (test_and_set_bit(WORK_QUEUED_BIT, &work->flags))
Chris Mason8b712842008-06-11 16:50:36 -0400397 goto out;
398
399 spin_lock_irqsave(&worker->lock, flags);
Chris Mason8b712842008-06-11 16:50:36 -0400400 list_add_tail(&work->list, &worker->pending);
Chris Masonb51912c2009-02-04 09:23:24 -0500401 atomic_inc(&worker->num_pending);
Chris Mason75ccf472008-09-30 19:24:06 -0400402
403 /* by definition we're busy, take ourselves off the idle
404 * list
405 */
406 if (worker->idle) {
407 spin_lock_irqsave(&worker->workers->lock, flags);
408 worker->idle = 0;
409 list_move_tail(&worker->worker_list,
410 &worker->workers->worker_list);
411 spin_unlock_irqrestore(&worker->workers->lock, flags);
412 }
Chris Masona6837052009-02-04 09:19:41 -0500413 if (!worker->working) {
414 wake = 1;
415 worker->working = 1;
416 }
Chris Mason75ccf472008-09-30 19:24:06 -0400417
Chris Mason8b712842008-06-11 16:50:36 -0400418 spin_unlock_irqrestore(&worker->lock, flags);
Chris Masona6837052009-02-04 09:19:41 -0500419 if (wake)
420 wake_up_process(worker->task);
Chris Mason8b712842008-06-11 16:50:36 -0400421out:
Chris Masona6837052009-02-04 09:19:41 -0500422
Chris Mason8b712842008-06-11 16:50:36 -0400423 return 0;
424}
425
426/*
427 * places a struct btrfs_work into the pending queue of one of the kthreads
428 */
429int btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work)
430{
431 struct btrfs_worker_thread *worker;
432 unsigned long flags;
433 int wake = 0;
434
435 /* don't requeue something already on a list */
Chris Mason4a69a412008-11-06 22:03:00 -0500436 if (test_and_set_bit(WORK_QUEUED_BIT, &work->flags))
Chris Mason8b712842008-06-11 16:50:36 -0400437 goto out;
438
439 worker = find_worker(workers);
Chris Mason4a69a412008-11-06 22:03:00 -0500440 if (workers->ordered) {
441 spin_lock_irqsave(&workers->lock, flags);
442 list_add_tail(&work->order_list, &workers->order_list);
443 spin_unlock_irqrestore(&workers->lock, flags);
444 } else {
445 INIT_LIST_HEAD(&work->order_list);
446 }
Chris Mason8b712842008-06-11 16:50:36 -0400447
448 spin_lock_irqsave(&worker->lock, flags);
Chris Masona6837052009-02-04 09:19:41 -0500449
Chris Masonb51912c2009-02-04 09:23:24 -0500450 list_add_tail(&work->list, &worker->pending);
Chris Mason8b712842008-06-11 16:50:36 -0400451 atomic_inc(&worker->num_pending);
Chris Mason35d8ba62008-06-11 20:21:24 -0400452 check_busy_worker(worker);
Chris Mason8b712842008-06-11 16:50:36 -0400453
454 /*
455 * avoid calling into wake_up_process if this thread has already
456 * been kicked
457 */
458 if (!worker->working)
459 wake = 1;
460 worker->working = 1;
461
462 spin_unlock_irqrestore(&worker->lock, flags);
463
464 if (wake)
465 wake_up_process(worker->task);
466out:
467 return 0;
468}