workqueue: separate pool-attaching code out from create_worker()
authorLai Jiangshan <laijs@cn.fujitsu.com>
Tue, 20 May 2014 09:46:35 +0000 (17:46 +0800)
committerTejun Heo <tj@kernel.org>
Tue, 20 May 2014 14:59:32 +0000 (10:59 -0400)
Currently, the code to attach a new worker to its pool is embedded in
create_worker().  Separating this code out will make the codes clearer
and will allow rescuers to share the code path later.

tj: Description and comment updates.

Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
kernel/workqueue.c

index 38b9ea7c204ce3214ea659790710b0ea60eb9e03..b1de6ac4a0e319c962fdf3e70c9d4a5b068ce3da 100644 (file)
@@ -66,7 +66,7 @@ enum {
         *
         * Note that DISASSOCIATED should be flipped only while holding
         * attach_mutex to avoid changing binding state while
-        * create_worker() is in progress.
+        * worker_attach_to_pool() is in progress.
         */
        POOL_DISASSOCIATED      = 1 << 2,       /* cpu can't serve workers */
        POOL_FREEZING           = 1 << 3,       /* freeze in progress */
@@ -1682,14 +1682,47 @@ static struct worker *alloc_worker(void)
        return worker;
 }
 
+/**
+ * worker_attach_to_pool() - attach a worker to a pool
+ * @worker: worker to be attached
+ * @pool: the target pool
+ *
+ * Attach @worker to @pool.  Once attached, the %WORKER_UNBOUND flag and
+ * cpu-binding of @worker are kept coordinated with the pool across
+ * cpu-[un]hotplugs.
+ */
+static void worker_attach_to_pool(struct worker *worker,
+                                  struct worker_pool *pool)
+{
+       mutex_lock(&pool->attach_mutex);
+
+       /*
+        * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
+        * online CPUs.  It'll be re-applied when any of the CPUs come up.
+        */
+       set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
+
+       /*
+        * The pool->attach_mutex ensures %POOL_DISASSOCIATED remains
+        * stable across this function.  See the comments above the
+        * flag definition for details.
+        */
+       if (pool->flags & POOL_DISASSOCIATED)
+               worker->flags |= WORKER_UNBOUND;
+
+       list_add_tail(&worker->node, &pool->workers);
+
+       mutex_unlock(&pool->attach_mutex);
+}
+
 /**
  * worker_detach_from_pool() - detach a worker from its pool
  * @worker: worker which is attached to its pool
  * @pool: the pool @worker is attached to
  *
- * Undo the attaching which had been done in create_worker().  The caller
- * worker shouldn't access to the pool after detached except it has other
- * reference to the pool.
+ * Undo the attaching which had been done in worker_attach_to_pool().  The
+ * caller worker shouldn't access to the pool after detached except it has
+ * other reference to the pool.
  */
 static void worker_detach_from_pool(struct worker *worker,
                                    struct worker_pool *pool)
@@ -1753,26 +1786,8 @@ static struct worker *create_worker(struct worker_pool *pool)
        /* prevent userland from meddling with cpumask of workqueue workers */
        worker->task->flags |= PF_NO_SETAFFINITY;
 
-       mutex_lock(&pool->attach_mutex);
-
-       /*
-        * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
-        * online CPUs.  It'll be re-applied when any of the CPUs come up.
-        */
-       set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
-
-       /*
-        * The pool->attach_mutex ensures %POOL_DISASSOCIATED
-        * remains stable across this function.  See the comments above the
-        * flag definition for details.
-        */
-       if (pool->flags & POOL_DISASSOCIATED)
-               worker->flags |= WORKER_UNBOUND;
-
        /* successful, attach the worker to the pool */
-       list_add_tail(&worker->node, &pool->workers);
-
-       mutex_unlock(&pool->attach_mutex);
+       worker_attach_to_pool(worker, pool);
 
        return worker;