@@ -64,6 +64,8 @@ struct btrfs_worker_thread {
6464 int idle ;
6565};
6666
67+ static int __btrfs_start_workers (struct btrfs_workers * workers );
68+
6769/*
6870 * btrfs_start_workers uses kthread_run, which can block waiting for memory
6971 * for a very long time. It will actually throttle on page writeback,
@@ -88,27 +90,10 @@ static void start_new_worker_func(struct btrfs_work *work)
8890{
8991 struct worker_start * start ;
9092 start = container_of (work , struct worker_start , work );
91- btrfs_start_workers (start -> queue , 1 );
93+ __btrfs_start_workers (start -> queue );
9294 kfree (start );
9395}
9496
95- static int start_new_worker (struct btrfs_workers * queue )
96- {
97- struct worker_start * start ;
98- int ret ;
99-
100- start = kzalloc (sizeof (* start ), GFP_NOFS );
101- if (!start )
102- return - ENOMEM ;
103-
104- start -> work .func = start_new_worker_func ;
105- start -> queue = queue ;
106- ret = btrfs_queue_worker (queue -> atomic_worker_start , & start -> work );
107- if (ret )
108- kfree (start );
109- return ret ;
110- }
111-
11297/*
11398 * helper function to move a thread onto the idle list after it
11499 * has finished some requests.
@@ -153,12 +138,20 @@ static void check_busy_worker(struct btrfs_worker_thread *worker)
153138static void check_pending_worker_creates (struct btrfs_worker_thread * worker )
154139{
155140 struct btrfs_workers * workers = worker -> workers ;
141+ struct worker_start * start ;
156142 unsigned long flags ;
157143
158144 rmb ();
159145 if (!workers -> atomic_start_pending )
160146 return ;
161147
148+ start = kzalloc (sizeof (* start ), GFP_NOFS );
149+ if (!start )
150+ return ;
151+
152+ start -> work .func = start_new_worker_func ;
153+ start -> queue = workers ;
154+
162155 spin_lock_irqsave (& workers -> lock , flags );
163156 if (!workers -> atomic_start_pending )
164157 goto out ;
@@ -170,10 +163,11 @@ static void check_pending_worker_creates(struct btrfs_worker_thread *worker)
170163
171164 workers -> num_workers_starting += 1 ;
172165 spin_unlock_irqrestore (& workers -> lock , flags );
173- start_new_worker (workers );
166+ btrfs_queue_worker (workers -> atomic_worker_start , & start -> work );
174167 return ;
175168
176169out :
170+ kfree (start );
177171 spin_unlock_irqrestore (& workers -> lock , flags );
178172}
179173
@@ -462,56 +456,55 @@ void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max,
462456 * starts new worker threads. This does not enforce the max worker
463457 * count in case you need to temporarily go past it.
464458 */
465- static int __btrfs_start_workers (struct btrfs_workers * workers ,
466- int num_workers )
459+ static int __btrfs_start_workers (struct btrfs_workers * workers )
467460{
468461 struct btrfs_worker_thread * worker ;
469462 int ret = 0 ;
470- int i ;
471463
472- for (i = 0 ; i < num_workers ; i ++ ) {
473- worker = kzalloc (sizeof (* worker ), GFP_NOFS );
474- if (!worker ) {
475- ret = - ENOMEM ;
476- goto fail ;
477- }
464+ worker = kzalloc (sizeof (* worker ), GFP_NOFS );
465+ if (!worker ) {
466+ ret = - ENOMEM ;
467+ goto fail ;
468+ }
478469
479- INIT_LIST_HEAD (& worker -> pending );
480- INIT_LIST_HEAD (& worker -> prio_pending );
481- INIT_LIST_HEAD (& worker -> worker_list );
482- spin_lock_init (& worker -> lock );
483-
484- atomic_set (& worker -> num_pending , 0 );
485- atomic_set (& worker -> refs , 1 );
486- worker -> workers = workers ;
487- worker -> task = kthread_run (worker_loop , worker ,
488- "btrfs-%s-%d" , workers -> name ,
489- workers -> num_workers + i );
490- if (IS_ERR (worker -> task )) {
491- ret = PTR_ERR (worker -> task );
492- kfree (worker );
493- goto fail ;
494- }
495- spin_lock_irq (& workers -> lock );
496- list_add_tail (& worker -> worker_list , & workers -> idle_list );
497- worker -> idle = 1 ;
498- workers -> num_workers ++ ;
499- workers -> num_workers_starting -- ;
500- WARN_ON (workers -> num_workers_starting < 0 );
501- spin_unlock_irq (& workers -> lock );
470+ INIT_LIST_HEAD (& worker -> pending );
471+ INIT_LIST_HEAD (& worker -> prio_pending );
472+ INIT_LIST_HEAD (& worker -> worker_list );
473+ spin_lock_init (& worker -> lock );
474+
475+ atomic_set (& worker -> num_pending , 0 );
476+ atomic_set (& worker -> refs , 1 );
477+ worker -> workers = workers ;
478+ worker -> task = kthread_run (worker_loop , worker ,
479+ "btrfs-%s-%d" , workers -> name ,
480+ workers -> num_workers + 1 );
481+ if (IS_ERR (worker -> task )) {
482+ ret = PTR_ERR (worker -> task );
483+ kfree (worker );
484+ goto fail ;
502485 }
486+ spin_lock_irq (& workers -> lock );
487+ list_add_tail (& worker -> worker_list , & workers -> idle_list );
488+ worker -> idle = 1 ;
489+ workers -> num_workers ++ ;
490+ workers -> num_workers_starting -- ;
491+ WARN_ON (workers -> num_workers_starting < 0 );
492+ spin_unlock_irq (& workers -> lock );
493+
503494 return 0 ;
504495fail :
505- btrfs_stop_workers (workers );
496+ spin_lock_irq (& workers -> lock );
497+ workers -> num_workers_starting -- ;
498+ spin_unlock_irq (& workers -> lock );
506499 return ret ;
507500}
508501
509- int btrfs_start_workers (struct btrfs_workers * workers , int num_workers )
502+ int btrfs_start_workers (struct btrfs_workers * workers )
510503{
511504 spin_lock_irq (& workers -> lock );
512- workers -> num_workers_starting += num_workers ;
505+ workers -> num_workers_starting ++ ;
513506 spin_unlock_irq (& workers -> lock );
514- return __btrfs_start_workers (workers , num_workers );
507+ return __btrfs_start_workers (workers );
515508}
516509
517510/*
@@ -568,6 +561,7 @@ static struct btrfs_worker_thread *find_worker(struct btrfs_workers *workers)
568561 struct btrfs_worker_thread * worker ;
569562 unsigned long flags ;
570563 struct list_head * fallback ;
564+ int ret ;
571565
572566again :
573567 spin_lock_irqsave (& workers -> lock , flags );
@@ -584,7 +578,9 @@ static struct btrfs_worker_thread *find_worker(struct btrfs_workers *workers)
584578 workers -> num_workers_starting ++ ;
585579 spin_unlock_irqrestore (& workers -> lock , flags );
586580 /* we're below the limit, start another worker */
587- __btrfs_start_workers (workers , 1 );
581+ ret = __btrfs_start_workers (workers );
582+ if (ret )
583+ goto fallback ;
588584 goto again ;
589585 }
590586 }
@@ -665,15 +661,15 @@ void btrfs_set_work_high_prio(struct btrfs_work *work)
665661/*
666662 * places a struct btrfs_work into the pending queue of one of the kthreads
667663 */
668- int btrfs_queue_worker (struct btrfs_workers * workers , struct btrfs_work * work )
664+ void btrfs_queue_worker (struct btrfs_workers * workers , struct btrfs_work * work )
669665{
670666 struct btrfs_worker_thread * worker ;
671667 unsigned long flags ;
672668 int wake = 0 ;
673669
674670 /* don't requeue something already on a list */
675671 if (test_and_set_bit (WORK_QUEUED_BIT , & work -> flags ))
676- goto out ;
672+ return ;
677673
678674 worker = find_worker (workers );
679675 if (workers -> ordered ) {
@@ -712,7 +708,4 @@ int btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work)
712708 if (wake )
713709 wake_up_process (worker -> task );
714710 spin_unlock_irqrestore (& worker -> lock , flags );
715-
716- out :
717- return 0 ;
718711}
0 commit comments