@@ -1540,7 +1540,7 @@ static void worker_enter_idle(struct worker *worker)
1540
1540
(worker -> hentry .next || worker -> hentry .pprev )))
1541
1541
return ;
1542
1542
1543
- /* can't use worker_set_flags(), also called from start_worker () */
1543
+ /* can't use worker_set_flags(), also called from create_worker () */
1544
1544
worker -> flags |= WORKER_IDLE ;
1545
1545
pool -> nr_idle ++ ;
1546
1546
worker -> last_active = jiffies ;
@@ -1661,8 +1661,7 @@ static void worker_detach_from_pool(struct worker *worker,
1661
1661
* create_worker - create a new workqueue worker
1662
1662
* @pool: pool the new worker will belong to
1663
1663
*
1664
- * Create a new worker which is attached to @pool. The new worker must be
1665
- * started by start_worker().
1664
+ * Create and start a new worker which is attached to @pool.
1666
1665
*
1667
1666
* CONTEXT:
1668
1667
* Might sleep. Does GFP_KERNEL allocations.
@@ -1707,6 +1706,13 @@ static struct worker *create_worker(struct worker_pool *pool)
1707
1706
/* successful, attach the worker to the pool */
1708
1707
worker_attach_to_pool (worker , pool );
1709
1708
1709
+ /* start the newly created worker */
1710
+ spin_lock_irq (& pool -> lock );
1711
+ worker -> pool -> nr_workers ++ ;
1712
+ worker_enter_idle (worker );
1713
+ wake_up_process (worker -> task );
1714
+ spin_unlock_irq (& pool -> lock );
1715
+
1710
1716
return worker ;
1711
1717
1712
1718
fail :
@@ -1716,44 +1722,6 @@ static struct worker *create_worker(struct worker_pool *pool)
1716
1722
return NULL ;
1717
1723
}
1718
1724
1719
- /**
1720
- * start_worker - start a newly created worker
1721
- * @worker: worker to start
1722
- *
1723
- * Make the pool aware of @worker and start it.
1724
- *
1725
- * CONTEXT:
1726
- * spin_lock_irq(pool->lock).
1727
- */
1728
- static void start_worker (struct worker * worker )
1729
- {
1730
- worker -> pool -> nr_workers ++ ;
1731
- worker_enter_idle (worker );
1732
- wake_up_process (worker -> task );
1733
- }
1734
-
1735
- /**
1736
- * create_and_start_worker - create and start a worker for a pool
1737
- * @pool: the target pool
1738
- *
1739
- * Grab the managership of @pool and create and start a new worker for it.
1740
- *
1741
- * Return: 0 on success. A negative error code otherwise.
1742
- */
1743
- static int create_and_start_worker (struct worker_pool * pool )
1744
- {
1745
- struct worker * worker ;
1746
-
1747
- worker = create_worker (pool );
1748
- if (worker ) {
1749
- spin_lock_irq (& pool -> lock );
1750
- start_worker (worker );
1751
- spin_unlock_irq (& pool -> lock );
1752
- }
1753
-
1754
- return worker ? 0 : - ENOMEM ;
1755
- }
1756
-
1757
1725
/**
1758
1726
* destroy_worker - destroy a workqueue worker
1759
1727
* @worker: worker to be destroyed
@@ -1892,19 +1860,7 @@ __acquires(&pool->lock)
1892
1860
mod_timer (& pool -> mayday_timer , jiffies + MAYDAY_INITIAL_TIMEOUT );
1893
1861
1894
1862
while (true) {
1895
- struct worker * worker ;
1896
-
1897
- worker = create_worker (pool );
1898
- if (worker ) {
1899
- del_timer_sync (& pool -> mayday_timer );
1900
- spin_lock_irq (& pool -> lock );
1901
- start_worker (worker );
1902
- if (WARN_ON_ONCE (need_to_create_worker (pool )))
1903
- goto restart ;
1904
- return true;
1905
- }
1906
-
1907
- if (!need_to_create_worker (pool ))
1863
+ if (create_worker (pool ) || !need_to_create_worker (pool ))
1908
1864
break ;
1909
1865
1910
1866
schedule_timeout_interruptible (CREATE_COOLDOWN );
@@ -1915,6 +1871,11 @@ __acquires(&pool->lock)
1915
1871
1916
1872
del_timer_sync (& pool -> mayday_timer );
1917
1873
spin_lock_irq (& pool -> lock );
1874
+ /*
1875
+ * This is necessary even after a new worker was just successfully
1876
+ * created as @pool->lock was dropped and the new worker might have
1877
+ * already become busy.
1878
+ */
1918
1879
if (need_to_create_worker (pool ))
1919
1880
goto restart ;
1920
1881
return true;
@@ -3537,7 +3498,7 @@ static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3537
3498
goto fail ;
3538
3499
3539
3500
/* create and start the initial worker */
3540
- if (create_and_start_worker (pool ) < 0 )
3501
+ if (! create_worker (pool ))
3541
3502
goto fail ;
3542
3503
3543
3504
/* install */
@@ -4611,7 +4572,7 @@ static int workqueue_cpu_up_callback(struct notifier_block *nfb,
4611
4572
for_each_cpu_worker_pool (pool , cpu ) {
4612
4573
if (pool -> nr_workers )
4613
4574
continue ;
4614
- if (create_and_start_worker (pool ) < 0 )
4575
+ if (! create_worker (pool ))
4615
4576
return NOTIFY_BAD ;
4616
4577
}
4617
4578
break ;
@@ -4911,7 +4872,7 @@ static int __init init_workqueues(void)
4911
4872
4912
4873
for_each_cpu_worker_pool (pool , cpu ) {
4913
4874
pool -> flags &= ~POOL_DISASSOCIATED ;
4914
- BUG_ON (create_and_start_worker (pool ) < 0 );
4875
+ BUG_ON (! create_worker (pool ));
4915
4876
}
4916
4877
}
4917
4878
0 commit comments