Merge branch 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[linux-2.6-block.git] / kernel / workqueue.c
index 4a9f65b54ee5f1db96998d598f66356f1712c077..4c4f06176f748616b180254e94eda6bbed7dde25 100644 (file)
@@ -285,12 +285,7 @@ static bool wq_disable_numa;
 module_param_named(disable_numa, wq_disable_numa, bool, 0444);
 
 /* see the comment above the definition of WQ_POWER_EFFICIENT */
-#ifdef CONFIG_WQ_POWER_EFFICIENT_DEFAULT
-static bool wq_power_efficient = true;
-#else
-static bool wq_power_efficient;
-#endif
-
+static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT);
 module_param_named(power_efficient, wq_power_efficient, bool, 0444);
 
 static bool wq_numa_enabled;           /* unbound NUMA affinity enabled */
@@ -337,8 +332,6 @@ struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
 EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
 
 static int worker_thread(void *__worker);
-static void copy_workqueue_attrs(struct workqueue_attrs *to,
-                                const struct workqueue_attrs *from);
 static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
 
 #define CREATE_TRACE_POINTS
@@ -990,7 +983,7 @@ static struct worker *find_worker_executing_work(struct worker_pool *pool,
  * move_linked_works - move linked works to a list
  * @work: start of series of works to be scheduled
  * @head: target list to append @work to
- * @nextp: out paramter for nested worklist walking
+ * @nextp: out parameter for nested worklist walking
  *
  * Schedule linked works starting from @work to @head.  Work series to
  * be scheduled starts at @work and includes any consecutive work with
@@ -2960,36 +2953,6 @@ int schedule_on_each_cpu(work_func_t func)
        return 0;
 }
 
-/**
- * flush_scheduled_work - ensure that any scheduled work has run to completion.
- *
- * Forces execution of the kernel-global workqueue and blocks until its
- * completion.
- *
- * Think twice before calling this function!  It's very easy to get into
- * trouble if you don't take great care.  Either of the following situations
- * will lead to deadlock:
- *
- *     One of the work items currently on the workqueue needs to acquire
- *     a lock held by your code or its caller.
- *
- *     Your code is running in the context of a work routine.
- *
- * They will be detected by lockdep when they occur, but the first might not
- * occur very often.  It depends on what work items are on the workqueue and
- * what locks they need, which you have no control over.
- *
- * In most situations flushing the entire workqueue is overkill; you merely
- * need to know that a particular work item isn't queued and isn't running.
- * In such cases you should use cancel_delayed_work_sync() or
- * cancel_work_sync() instead.
- */
-void flush_scheduled_work(void)
-{
-       flush_workqueue(system_wq);
-}
-EXPORT_SYMBOL(flush_scheduled_work);
-
 /**
  * execute_in_process_context - reliably execute the routine with user context
  * @fn:                the function to execute
@@ -3095,7 +3058,7 @@ static bool wqattrs_equal(const struct workqueue_attrs *a,
  * init_worker_pool - initialize a newly zalloc'd worker_pool
  * @pool: worker_pool to initialize
  *
- * Initiailize a newly zalloc'd @pool.  It also allocates @pool->attrs.
+ * Initialize a newly zalloc'd @pool.  It also allocates @pool->attrs.
  *
  * Return: 0 on success, -errno on failure.  Even on failure, all fields
  * inside @pool proper are initialized and put_unbound_pool() can be called
@@ -3621,24 +3584,21 @@ static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx)
        mutex_unlock(&ctx->wq->mutex);
 }
 
-/**
- * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
- * @wq: the target workqueue
- * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
- *
- * Apply @attrs to an unbound workqueue @wq.  Unless disabled, on NUMA
- * machines, this function maps a separate pwq to each NUMA node with
- * possibles CPUs in @attrs->cpumask so that work items are affine to the
- * NUMA node it was issued on.  Older pwqs are released as in-flight work
- * items finish.  Note that a work item which repeatedly requeues itself
- * back-to-back will stay on its current pwq.
- *
- * Performs GFP_KERNEL allocations.
- *
- * Return: 0 on success and -errno on failure.
- */
-int apply_workqueue_attrs(struct workqueue_struct *wq,
-                         const struct workqueue_attrs *attrs)
+static void apply_wqattrs_lock(void)
+{
+       /* CPUs should stay stable across pwq creations and installations */
+       get_online_cpus();
+       mutex_lock(&wq_pool_mutex);
+}
+
+static void apply_wqattrs_unlock(void)
+{
+       mutex_unlock(&wq_pool_mutex);
+       put_online_cpus();
+}
+
+static int apply_workqueue_attrs_locked(struct workqueue_struct *wq,
+                                       const struct workqueue_attrs *attrs)
 {
        struct apply_wqattrs_ctx *ctx;
        int ret = -ENOMEM;
@@ -3651,14 +3611,6 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,
        if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
                return -EINVAL;
 
-       /*
-        * CPUs should stay stable across pwq creations and installations.
-        * Pin CPUs, determine the target cpumask for each node and create
-        * pwqs accordingly.
-        */
-       get_online_cpus();
-       mutex_lock(&wq_pool_mutex);
-
        ctx = apply_wqattrs_prepare(wq, attrs);
 
        /* the ctx has been prepared successfully, let's commit it */
@@ -3667,14 +3619,39 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,
                ret = 0;
        }
 
-       mutex_unlock(&wq_pool_mutex);
-       put_online_cpus();
-
        apply_wqattrs_cleanup(ctx);
 
        return ret;
 }
 
+/**
+ * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
+ * @wq: the target workqueue
+ * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
+ *
+ * Apply @attrs to an unbound workqueue @wq.  Unless disabled, on NUMA
+ * machines, this function maps a separate pwq to each NUMA node with
+ * possibles CPUs in @attrs->cpumask so that work items are affine to the
+ * NUMA node it was issued on.  Older pwqs are released as in-flight work
+ * items finish.  Note that a work item which repeatedly requeues itself
+ * back-to-back will stay on its current pwq.
+ *
+ * Performs GFP_KERNEL allocations.
+ *
+ * Return: 0 on success and -errno on failure.
+ */
+int apply_workqueue_attrs(struct workqueue_struct *wq,
+                         const struct workqueue_attrs *attrs)
+{
+       int ret;
+
+       apply_wqattrs_lock();
+       ret = apply_workqueue_attrs_locked(wq, attrs);
+       apply_wqattrs_unlock();
+
+       return ret;
+}
+
 /**
  * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
  * @wq: the target workqueue
@@ -4432,7 +4409,7 @@ static void rebind_workers(struct worker_pool *pool)
        /*
         * Restore CPU affinity of all workers.  As all idle workers should
         * be on the run-queue of the associated CPU before any local
-        * wake-ups for concurrency management happen, restore CPU affinty
+        * wake-ups for concurrency management happen, restore CPU affinity
         * of all workers first and then clear UNBOUND.  As we're called
         * from CPU_ONLINE, the following shouldn't fail.
         */
@@ -4799,10 +4776,9 @@ int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
        if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL))
                return -ENOMEM;
 
-       get_online_cpus();
        cpumask_and(cpumask, cpumask, cpu_possible_mask);
        if (!cpumask_empty(cpumask)) {
-               mutex_lock(&wq_pool_mutex);
+               apply_wqattrs_lock();
 
                /* save the old wq_unbound_cpumask. */
                cpumask_copy(saved_cpumask, wq_unbound_cpumask);
@@ -4815,9 +4791,8 @@ int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
                if (ret < 0)
                        cpumask_copy(wq_unbound_cpumask, saved_cpumask);
 
-               mutex_unlock(&wq_pool_mutex);
+               apply_wqattrs_unlock();
        }
-       put_online_cpus();
 
        free_cpumask_var(saved_cpumask);
        return ret;
@@ -4927,13 +4902,13 @@ static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
 {
        struct workqueue_attrs *attrs;
 
+       lockdep_assert_held(&wq_pool_mutex);
+
        attrs = alloc_workqueue_attrs(GFP_KERNEL);
        if (!attrs)
                return NULL;
 
-       mutex_lock(&wq->mutex);
        copy_workqueue_attrs(attrs, wq->unbound_attrs);
-       mutex_unlock(&wq->mutex);
        return attrs;
 }
 
@@ -4942,18 +4917,22 @@ static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
 {
        struct workqueue_struct *wq = dev_to_wq(dev);
        struct workqueue_attrs *attrs;
-       int ret;
+       int ret = -ENOMEM;
+
+       apply_wqattrs_lock();
 
        attrs = wq_sysfs_prep_attrs(wq);
        if (!attrs)
-               return -ENOMEM;
+               goto out_unlock;
 
        if (sscanf(buf, "%d", &attrs->nice) == 1 &&
            attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE)
-               ret = apply_workqueue_attrs(wq, attrs);
+               ret = apply_workqueue_attrs_locked(wq, attrs);
        else
                ret = -EINVAL;
 
+out_unlock:
+       apply_wqattrs_unlock();
        free_workqueue_attrs(attrs);
        return ret ?: count;
 }
@@ -4977,16 +4956,20 @@ static ssize_t wq_cpumask_store(struct device *dev,
 {
        struct workqueue_struct *wq = dev_to_wq(dev);
        struct workqueue_attrs *attrs;
-       int ret;
+       int ret = -ENOMEM;
+
+       apply_wqattrs_lock();
 
        attrs = wq_sysfs_prep_attrs(wq);
        if (!attrs)
-               return -ENOMEM;
+               goto out_unlock;
 
        ret = cpumask_parse(buf, attrs->cpumask);
        if (!ret)
-               ret = apply_workqueue_attrs(wq, attrs);
+               ret = apply_workqueue_attrs_locked(wq, attrs);
 
+out_unlock:
+       apply_wqattrs_unlock();
        free_workqueue_attrs(attrs);
        return ret ?: count;
 }
@@ -5010,18 +4993,22 @@ static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
 {
        struct workqueue_struct *wq = dev_to_wq(dev);
        struct workqueue_attrs *attrs;
-       int v, ret;
+       int v, ret = -ENOMEM;
+
+       apply_wqattrs_lock();
 
        attrs = wq_sysfs_prep_attrs(wq);
        if (!attrs)
-               return -ENOMEM;
+               goto out_unlock;
 
        ret = -EINVAL;
        if (sscanf(buf, "%d", &v) == 1) {
                attrs->no_numa = !v;
-               ret = apply_workqueue_attrs(wq, attrs);
+               ret = apply_workqueue_attrs_locked(wq, attrs);
        }
 
+out_unlock:
+       apply_wqattrs_unlock();
        free_workqueue_attrs(attrs);
        return ret ?: count;
 }
@@ -5113,7 +5100,7 @@ int workqueue_sysfs_register(struct workqueue_struct *wq)
        int ret;
 
        /*
-        * Adjusting max_active or creating new pwqs by applyting
+        * Adjusting max_active or creating new pwqs by applying
         * attributes breaks ordering guarantee.  Disallow exposing ordered
         * workqueues.
         */