diff options
author | Chris Mason <chris.mason@oracle.com> | 2009-10-02 19:11:56 -0400 |
---|---|---|
committer | Chris Mason <chris.mason@oracle.com> | 2009-10-05 09:44:45 -0400 |
commit | 61d92c328c16419fc96dc50dd16f8b8c695409ec (patch) | |
tree | e9cd82eb56ff5f38f64d9f35229d15496e5d53de /fs/btrfs/async-thread.c | |
parent | fbf190874407f23d2891b53ffdf7d3c6be8d47ff (diff) |
Btrfs: fix deadlock on async thread startup
The btrfs async worker threads are used for a wide variety of things,
including processing bio end_io functions. This means that when
the endio threads aren't running, the rest of the FS isn't
able to do the final processing required to clear PageWriteback.
The endio threads also try to exit as they become idle and
start more as the work piles up. The problem is that starting more
threads means kthreadd may need to allocate ram, and that allocation
may wait until the global number of writeback pages on the system is
below a certain limit.
The result of that throttling is that end IO threads wait on
kthreadd, who is waiting on IO to end, which will never happen.
This commit fixes the deadlock by handing off thread startup to a
dedicated thread. It also fixes a bug where the on-demand thread
creation was creating far too many threads because it didn't take into
account threads being started by other procs.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/async-thread.c')
-rw-r--r-- | fs/btrfs/async-thread.c | 81 |
1 files changed, 73 insertions, 8 deletions
diff --git a/fs/btrfs/async-thread.c b/fs/btrfs/async-thread.c index 282ca085c2fb..c0861e781cdb 100644 --- a/fs/btrfs/async-thread.c +++ b/fs/btrfs/async-thread.c | |||
@@ -64,6 +64,51 @@ struct btrfs_worker_thread { | |||
64 | }; | 64 | }; |
65 | 65 | ||
66 | /* | 66 | /* |
67 | * btrfs_start_workers uses kthread_run, which can block waiting for memory | ||
68 | * for a very long time. It will actually throttle on page writeback, | ||
69 | * and so it may not make progress until after our btrfs worker threads | ||
70 | * process all of the pending work structs in their queue | ||
71 | * | ||
72 | * This means we can't use btrfs_start_workers from inside a btrfs worker | ||
73 | * thread that is used as part of cleaning dirty memory, which pretty much | ||
74 | * involves all of the worker threads. | ||
75 | * | ||
76 | * Instead we have a helper queue who never has more than one thread | ||
77 | * where we scheduler thread start operations. This worker_start struct | ||
78 | * is used to contain the work and hold a pointer to the queue that needs | ||
79 | * another worker. | ||
80 | */ | ||
81 | struct worker_start { | ||
82 | struct btrfs_work work; | ||
83 | struct btrfs_workers *queue; | ||
84 | }; | ||
85 | |||
86 | static void start_new_worker_func(struct btrfs_work *work) | ||
87 | { | ||
88 | struct worker_start *start; | ||
89 | start = container_of(work, struct worker_start, work); | ||
90 | btrfs_start_workers(start->queue, 1); | ||
91 | kfree(start); | ||
92 | } | ||
93 | |||
94 | static int start_new_worker(struct btrfs_workers *queue) | ||
95 | { | ||
96 | struct worker_start *start; | ||
97 | int ret; | ||
98 | |||
99 | start = kzalloc(sizeof(*start), GFP_NOFS); | ||
100 | if (!start) | ||
101 | return -ENOMEM; | ||
102 | |||
103 | start->work.func = start_new_worker_func; | ||
104 | start->queue = queue; | ||
105 | ret = btrfs_queue_worker(queue->atomic_worker_start, &start->work); | ||
106 | if (ret) | ||
107 | kfree(start); | ||
108 | return ret; | ||
109 | } | ||
110 | |||
111 | /* | ||
67 | * helper function to move a thread onto the idle list after it | 112 | * helper function to move a thread onto the idle list after it |
68 | * has finished some requests. | 113 | * has finished some requests. |
69 | */ | 114 | */ |
@@ -118,11 +163,13 @@ static void check_pending_worker_creates(struct btrfs_worker_thread *worker) | |||
118 | goto out; | 163 | goto out; |
119 | 164 | ||
120 | workers->atomic_start_pending = 0; | 165 | workers->atomic_start_pending = 0; |
121 | if (workers->num_workers >= workers->max_workers) | 166 | if (workers->num_workers + workers->num_workers_starting >= |
167 | workers->max_workers) | ||
122 | goto out; | 168 | goto out; |
123 | 169 | ||
170 | workers->num_workers_starting += 1; | ||
124 | spin_unlock_irqrestore(&workers->lock, flags); | 171 | spin_unlock_irqrestore(&workers->lock, flags); |
125 | btrfs_start_workers(workers, 1); | 172 | start_new_worker(workers); |
126 | return; | 173 | return; |
127 | 174 | ||
128 | out: | 175 | out: |
@@ -390,9 +437,11 @@ int btrfs_stop_workers(struct btrfs_workers *workers) | |||
390 | /* | 437 | /* |
391 | * simple init on struct btrfs_workers | 438 | * simple init on struct btrfs_workers |
392 | */ | 439 | */ |
393 | void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max) | 440 | void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max, |
441 | struct btrfs_workers *async_helper) | ||
394 | { | 442 | { |
395 | workers->num_workers = 0; | 443 | workers->num_workers = 0; |
444 | workers->num_workers_starting = 0; | ||
396 | INIT_LIST_HEAD(&workers->worker_list); | 445 | INIT_LIST_HEAD(&workers->worker_list); |
397 | INIT_LIST_HEAD(&workers->idle_list); | 446 | INIT_LIST_HEAD(&workers->idle_list); |
398 | INIT_LIST_HEAD(&workers->order_list); | 447 | INIT_LIST_HEAD(&workers->order_list); |
@@ -404,14 +453,15 @@ void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max) | |||
404 | workers->name = name; | 453 | workers->name = name; |
405 | workers->ordered = 0; | 454 | workers->ordered = 0; |
406 | workers->atomic_start_pending = 0; | 455 | workers->atomic_start_pending = 0; |
407 | workers->atomic_worker_start = 0; | 456 | workers->atomic_worker_start = async_helper; |
408 | } | 457 | } |
409 | 458 | ||
410 | /* | 459 | /* |
411 | * starts new worker threads. This does not enforce the max worker | 460 | * starts new worker threads. This does not enforce the max worker |
412 | * count in case you need to temporarily go past it. | 461 | * count in case you need to temporarily go past it. |
413 | */ | 462 | */ |
414 | int btrfs_start_workers(struct btrfs_workers *workers, int num_workers) | 463 | static int __btrfs_start_workers(struct btrfs_workers *workers, |
464 | int num_workers) | ||
415 | { | 465 | { |
416 | struct btrfs_worker_thread *worker; | 466 | struct btrfs_worker_thread *worker; |
417 | int ret = 0; | 467 | int ret = 0; |
@@ -444,6 +494,8 @@ int btrfs_start_workers(struct btrfs_workers *workers, int num_workers) | |||
444 | list_add_tail(&worker->worker_list, &workers->idle_list); | 494 | list_add_tail(&worker->worker_list, &workers->idle_list); |
445 | worker->idle = 1; | 495 | worker->idle = 1; |
446 | workers->num_workers++; | 496 | workers->num_workers++; |
497 | workers->num_workers_starting--; | ||
498 | WARN_ON(workers->num_workers_starting < 0); | ||
447 | spin_unlock_irq(&workers->lock); | 499 | spin_unlock_irq(&workers->lock); |
448 | } | 500 | } |
449 | return 0; | 501 | return 0; |
@@ -452,6 +504,14 @@ fail: | |||
452 | return ret; | 504 | return ret; |
453 | } | 505 | } |
454 | 506 | ||
507 | int btrfs_start_workers(struct btrfs_workers *workers, int num_workers) | ||
508 | { | ||
509 | spin_lock_irq(&workers->lock); | ||
510 | workers->num_workers_starting += num_workers; | ||
511 | spin_unlock_irq(&workers->lock); | ||
512 | return __btrfs_start_workers(workers, num_workers); | ||
513 | } | ||
514 | |||
455 | /* | 515 | /* |
456 | * run through the list and find a worker thread that doesn't have a lot | 516 | * run through the list and find a worker thread that doesn't have a lot |
457 | * to do right now. This can return null if we aren't yet at the thread | 517 | * to do right now. This can return null if we aren't yet at the thread |
@@ -461,7 +521,10 @@ static struct btrfs_worker_thread *next_worker(struct btrfs_workers *workers) | |||
461 | { | 521 | { |
462 | struct btrfs_worker_thread *worker; | 522 | struct btrfs_worker_thread *worker; |
463 | struct list_head *next; | 523 | struct list_head *next; |
464 | int enforce_min = workers->num_workers < workers->max_workers; | 524 | int enforce_min; |
525 | |||
526 | enforce_min = (workers->num_workers + workers->num_workers_starting) < | ||
527 | workers->max_workers; | ||
465 | 528 | ||
466 | /* | 529 | /* |
467 | * if we find an idle thread, don't move it to the end of the | 530 | * if we find an idle thread, don't move it to the end of the |
@@ -509,15 +572,17 @@ again: | |||
509 | worker = next_worker(workers); | 572 | worker = next_worker(workers); |
510 | 573 | ||
511 | if (!worker) { | 574 | if (!worker) { |
512 | if (workers->num_workers >= workers->max_workers) { | 575 | if (workers->num_workers + workers->num_workers_starting >= |
576 | workers->max_workers) { | ||
513 | goto fallback; | 577 | goto fallback; |
514 | } else if (workers->atomic_worker_start) { | 578 | } else if (workers->atomic_worker_start) { |
515 | workers->atomic_start_pending = 1; | 579 | workers->atomic_start_pending = 1; |
516 | goto fallback; | 580 | goto fallback; |
517 | } else { | 581 | } else { |
582 | workers->num_workers_starting++; | ||
518 | spin_unlock_irqrestore(&workers->lock, flags); | 583 | spin_unlock_irqrestore(&workers->lock, flags); |
519 | /* we're below the limit, start another worker */ | 584 | /* we're below the limit, start another worker */ |
520 | btrfs_start_workers(workers, 1); | 585 | __btrfs_start_workers(workers, 1); |
521 | goto again; | 586 | goto again; |
522 | } | 587 | } |
523 | } | 588 | } |