workqueue: Introduce low-level unbound wq sysfs cpumask v4
cover workqueue: Introduce low-level unbound wq sysfs cpumask v4
This patchset mostly copies from Frederic and split the apply_workqueue_attrs() as TJ's suggest.
This patchset doesn't include the patch "workqueue: Allow changing attributions of ordered workqueues", I hope to reduce the review processing. The handling for the ordered workqueue will be repose after this patchset accepted.
Thanks,
Lai
Frederic Weisbecker (2):
workqueue: Reorder sysfs code
workqueue: Create low-level unbound workqueues cpumask
Lai Jiangshan (2):
workqueue: split apply_workqueue_attrs() into 3 stages
workqueue: Allow modifying low level unbound workqueue cpumask
kernel/workqueue.c | 942 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 539 insertions(+), 403 deletions(-)
Signature · 1 line
2.1.0
PATCH 1/4 workqueue: Reorder sysfs code
From: Frederic Weisbecker <[email protected]>
The sysfs code usually belongs to the botom of the file since it deals with high level objects. In the workqueue code it's misplaced and such that we'll need to work around functions references to allow the sysfs code to call APIs like apply_workqueue_attrs().
Lets move that block further in the file, right above alloc_workqueue_key() which reference it.
--- kernel/workqueue.c | 634 ++++++++++++++++++++++++++--------------------------- 1 file changed, 317 insertions(+), 317 deletions(-)
kernel/workqueue.c +317 -317
@@ -3001,323 +3001,6 @@ int execute_in_process_context(work_func_t fn, struct execute_work *ew) }EXPORT_SYMBOL_GPL (execute_in_process_context); -#ifdef CONFIG_SYSFS -/* -* Workqueues with WQ_SYSFS flag set is visible to userland via -* /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the -* following attributes. -* -* per_cpu RO bool : whether the workqueue is per-cpu or unbound -* max_active RW int : maximum number of in-flight work items -* -* Unbound workqueues have the following extra attributes. -* -* id RO int : the associated pool ID -* nice RW int : nice value of the workers -* cpumask RW mask : bitmask of allowed CPUs for the workers -*/ -struct wq_device { -struct workqueue_struct *wq; -struct device dev; -}; - -static struct workqueue_struct *dev_to_wq (struct device *dev) -{ - struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); - -return wq_dev->wq; -} - -static ssize_t per_cpu_show (struct device *dev,struct device_attribute *attr, -char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); - -return scnprintf (buf,PAGE_SIZE ,"%d\n" , (bool )!(wq->flags &WQ_UNBOUND )); -} -static DEVICE_ATTR_RO (per_cpu); - -static ssize_t max_active_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); - -return scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->saved_max_active); -} - -static ssize_t max_active_store (struct device *dev, -struct device_attribute *attr,const char *buf, -size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int val; - -if (sscanf (buf,"%d" , &val) !=1 || val <=0 ) -return -EINVAL ; - -workqueue_set_max_active (wq, val); -return count; -} -static DEVICE_ATTR_RW (max_active); - -static struct attribute *wq_sysfs_attrs[] = { - &dev_attr_per_cpu.attr, - &dev_attr_max_active.attr, -NULL , -}; -ATTRIBUTE_GROUPS (wq_sysfs); - -static ssize_t wq_pool_ids_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -const char *delim ="" ; -int node, written =0 ; - -rcu_read_lock_sched (); -for_each_node (node) { - written +=scnprintf (buf + written,PAGE_SIZE - written, -"%s%d:%d" , delim, node, -unbound_pwq_by_node (wq, node)->pool->id); - delim =" " ; - } - written +=scnprintf (buf + written,PAGE_SIZE - written,"\n" ); -rcu_read_unlock_sched (); - -return written; -} - -static ssize_t wq_nice_show (struct device *dev,struct device_attribute *attr, -char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int written; - -mutex_lock (&wq->mutex); - written =scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->unbound_attrs->nice); -mutex_unlock (&wq->mutex); - -return written; -} - -/* prepare workqueue_attrs for sysfs store operations */ -static struct workqueue_attrs *wq_sysfs_prep_attrs (struct workqueue_struct *wq) -{ -struct workqueue_attrs *attrs; - - 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; -} - -static ssize_t wq_nice_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -struct workqueue_attrs *attrs; -int ret; - - attrs =wq_sysfs_prep_attrs (wq); -if (!attrs) -return -ENOMEM ; - -if (sscanf (buf,"%d" , &attrs->nice) ==1 && - attrs->nice >=MIN_NICE && attrs->nice <=MAX_NICE ) - ret =apply_workqueue_attrs (wq, attrs); -else - ret = -EINVAL ; - -free_workqueue_attrs (attrs); -return ret ?: count; -} - -static ssize_t wq_cpumask_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int written; - -mutex_lock (&wq->mutex); - written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , -cpumask_pr_args (wq->unbound_attrs->cpumask)); -mutex_unlock (&wq->mutex); -return written; -} - -static ssize_t wq_cpumask_store (struct device *dev, -struct device_attribute *attr, -const char *buf,size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -struct workqueue_attrs *attrs; -int ret; - - attrs =wq_sysfs_prep_attrs (wq); -if (!attrs) -return -ENOMEM ; - - ret =cpumask_parse (buf, attrs->cpumask); -if (!ret) - ret =apply_workqueue_attrs (wq, attrs); - -free_workqueue_attrs (attrs); -return ret ?: count; -} - -static ssize_t wq_numa_show (struct device *dev,struct device_attribute *attr, -char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int written; - -mutex_lock (&wq->mutex); - written =scnprintf (buf,PAGE_SIZE ,"%d\n" , - !wq->unbound_attrs->no_numa); -mutex_unlock (&wq->mutex); - -return written; -} - -static ssize_t wq_numa_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -struct workqueue_attrs *attrs; -int v, ret; - - attrs =wq_sysfs_prep_attrs (wq); -if (!attrs) -return -ENOMEM ; - - ret = -EINVAL ; -if (sscanf (buf,"%d" , &v) ==1 ) { - attrs->no_numa = !v; - ret =apply_workqueue_attrs (wq, attrs); - } - -free_workqueue_attrs (attrs); -return ret ?: count; -} - -static struct device_attribute wq_sysfs_unbound_attrs[] = { -__ATTR (pool_ids,0444 , wq_pool_ids_show,NULL ), -__ATTR (nice,0644 , wq_nice_show, wq_nice_store), -__ATTR (cpumask,0644 , wq_cpumask_show, wq_cpumask_store), -__ATTR (numa,0644 , wq_numa_show, wq_numa_store), - __ATTR_NULL, -}; - -static struct bus_type wq_subsys = { - .name ="workqueue" , - .dev_groups = wq_sysfs_groups, -}; - -static int __init wq_sysfs_init (void ) -{ -return subsys_virtual_register (&wq_subsys,NULL ); -} -core_initcall (wq_sysfs_init); - -static void wq_device_release (struct device *dev) -{ - struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); - -kfree (wq_dev); -} - -/** -* workqueue_sysfs_register - make a workqueue visible in sysfs -* @wq: the workqueue to register -* -* Expose @wq in sysfs under /sys/bus/workqueue/devices. -* alloc_workqueue*() automatically calls this function if WQ_SYSFS is set -* which is the preferred method. -* -* Workqueue user should use this function directly iff it wants to apply -* workqueue_attrs before making the workqueue visible in sysfs; otherwise, -* apply_workqueue_attrs() may race against userland updating the -* attributes. -* -* Return: 0 on success, -errno on failure. -*/ -int workqueue_sysfs_register (struct workqueue_struct *wq) -{ -struct wq_device *wq_dev; -int ret; - -/* -* Adjusting max_active or creating new pwqs by applyting -* attributes breaks ordering guarantee. Disallow exposing ordered -* workqueues. -*/ -if (WARN_ON (wq->flags & __WQ_ORDERED)) -return -EINVAL ; - - wq->wq_dev = wq_dev =kzalloc (sizeof (*wq_dev),GFP_KERNEL ); -if (!wq_dev) -return -ENOMEM ; - - wq_dev->wq = wq; - wq_dev->dev.bus = &wq_subsys; - wq_dev->dev.init_name = wq->name; - wq_dev->dev.release = wq_device_release; - -/* -* unbound_attrs are created separately. Suppress uevent until -* everything is ready. -*/ -dev_set_uevent_suppress (&wq_dev->dev, true); - - ret =device_register (&wq_dev->dev); -if (ret) { -kfree (wq_dev); - wq->wq_dev =NULL ; -return ret; - } - -if (wq->flags &WQ_UNBOUND ) { -struct device_attribute *attr; - -for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { - ret =device_create_file (&wq_dev->dev, attr); -if (ret) { -device_unregister (&wq_dev->dev); - wq->wq_dev =NULL ; -return ret; - } - } - } - -dev_set_uevent_suppress (&wq_dev->dev, false); -kobject_uevent (&wq_dev->dev.kobj,KOBJ_ADD ); -return 0 ; -} - -/** -* workqueue_sysfs_unregister - undo workqueue_sysfs_register() -* @wq: the workqueue to unregister -* -* If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. -*/ -static void workqueue_sysfs_unregister (struct workqueue_struct *wq) -{ -struct wq_device *wq_dev = wq->wq_dev; - -if (!wq->wq_dev) -return ; - - wq->wq_dev =NULL ; -device_unregister (&wq_dev->dev); -} -#else /* CONFIG_SYSFS */ -static void workqueue_sysfs_unregister (struct workqueue_struct *wq) { } -#endif /* CONFIG_SYSFS */ -/** * free_workqueue_attrs - free a workqueue_attrs * @attrs: workqueue_attrs to free
@@ -4029,6 +3712,323 @@ out_unlock:put_pwq_unlocked (old_pwq); } +#ifdef CONFIG_SYSFS +/* +* Workqueues with WQ_SYSFS flag set is visible to userland via +* /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the +* following attributes. +* +* per_cpu RO bool : whether the workqueue is per-cpu or unbound +* max_active RW int : maximum number of in-flight work items +* +* Unbound workqueues have the following extra attributes. +* +* id RO int : the associated pool ID +* nice RW int : nice value of the workers +* cpumask RW mask : bitmask of allowed CPUs for the workers +*/ +struct wq_device { +struct workqueue_struct *wq; +struct device dev; +}; + +static struct workqueue_struct *dev_to_wq (struct device *dev) +{ + struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); + +return wq_dev->wq; +} + +static ssize_t per_cpu_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , (bool )!(wq->flags &WQ_UNBOUND )); +} +static DEVICE_ATTR_RO (per_cpu); + +static ssize_t max_active_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->saved_max_active); +} + +static ssize_t max_active_store (struct device *dev, +struct device_attribute *attr,const char *buf, +size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int val; + +if (sscanf (buf,"%d" , &val) !=1 || val <=0 ) +return -EINVAL ; + +workqueue_set_max_active (wq, val); +return count; +} +static DEVICE_ATTR_RW (max_active); + +static struct attribute *wq_sysfs_attrs[] = { + &dev_attr_per_cpu.attr, + &dev_attr_max_active.attr, +NULL , +}; +ATTRIBUTE_GROUPS (wq_sysfs); + +static ssize_t wq_pool_ids_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +const char *delim ="" ; +int node, written =0 ; + +rcu_read_lock_sched (); +for_each_node (node) { + written +=scnprintf (buf + written,PAGE_SIZE - written, +"%s%d:%d" , delim, node, +unbound_pwq_by_node (wq, node)->pool->id); + delim =" " ; + } + written +=scnprintf (buf + written,PAGE_SIZE - written,"\n" ); +rcu_read_unlock_sched (); + +return written; +} + +static ssize_t wq_nice_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int written; + +mutex_lock (&wq->mutex); + written =scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->unbound_attrs->nice); +mutex_unlock (&wq->mutex); + +return written; +} + +/* prepare workqueue_attrs for sysfs store operations */ +static struct workqueue_attrs *wq_sysfs_prep_attrs (struct workqueue_struct *wq) +{ +struct workqueue_attrs *attrs; + + 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; +} + +static ssize_t wq_nice_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +struct workqueue_attrs *attrs; +int ret; + + attrs =wq_sysfs_prep_attrs (wq); +if (!attrs) +return -ENOMEM ; + +if (sscanf (buf,"%d" , &attrs->nice) ==1 && + attrs->nice >=MIN_NICE && attrs->nice <=MAX_NICE ) + ret =apply_workqueue_attrs (wq, attrs); +else + ret = -EINVAL ; + +free_workqueue_attrs (attrs); +return ret ?: count; +} + +static ssize_t wq_cpumask_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int written; + +mutex_lock (&wq->mutex); + written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , +cpumask_pr_args (wq->unbound_attrs->cpumask)); +mutex_unlock (&wq->mutex); +return written; +} + +static ssize_t wq_cpumask_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +struct workqueue_attrs *attrs; +int ret; + + attrs =wq_sysfs_prep_attrs (wq); +if (!attrs) +return -ENOMEM ; + + ret =cpumask_parse (buf, attrs->cpumask); +if (!ret) + ret =apply_workqueue_attrs (wq, attrs); + +free_workqueue_attrs (attrs); +return ret ?: count; +} + +static ssize_t wq_numa_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int written; + +mutex_lock (&wq->mutex); + written =scnprintf (buf,PAGE_SIZE ,"%d\n" , + !wq->unbound_attrs->no_numa); +mutex_unlock (&wq->mutex); + +return written; +} + +static ssize_t wq_numa_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +struct workqueue_attrs *attrs; +int v, ret; + + attrs =wq_sysfs_prep_attrs (wq); +if (!attrs) +return -ENOMEM ; + + ret = -EINVAL ; +if (sscanf (buf,"%d" , &v) ==1 ) { + attrs->no_numa = !v; + ret =apply_workqueue_attrs (wq, attrs); + } + +free_workqueue_attrs (attrs); +return ret ?: count; +} + +static struct device_attribute wq_sysfs_unbound_attrs[] = { +__ATTR (pool_ids,0444 , wq_pool_ids_show,NULL ), +__ATTR (nice,0644 , wq_nice_show, wq_nice_store), +__ATTR (cpumask,0644 , wq_cpumask_show, wq_cpumask_store), +__ATTR (numa,0644 , wq_numa_show, wq_numa_store), + __ATTR_NULL, +}; + +static struct bus_type wq_subsys = { + .name ="workqueue" , + .dev_groups = wq_sysfs_groups, +}; + +static int __init wq_sysfs_init (void ) +{ +return subsys_virtual_register (&wq_subsys,NULL ); +} +core_initcall (wq_sysfs_init); + +static void wq_device_release (struct device *dev) +{ + struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); + +kfree (wq_dev); +} + +/** +* workqueue_sysfs_register - make a workqueue visible in sysfs +* @wq: the workqueue to register +* +* Expose @wq in sysfs under /sys/bus/workqueue/devices. +* alloc_workqueue*() automatically calls this function if WQ_SYSFS is set +* which is the preferred method. +* +* Workqueue user should use this function directly iff it wants to apply +* workqueue_attrs before making the workqueue visible in sysfs; otherwise, +* apply_workqueue_attrs() may race against userland updating the +* attributes. +* +* Return: 0 on success, -errno on failure. +*/ +int workqueue_sysfs_register (struct workqueue_struct *wq) +{ +struct wq_device *wq_dev; +int ret; + +/* +* Adjusting max_active or creating new pwqs by applyting +* attributes breaks ordering guarantee. Disallow exposing ordered +* workqueues. +*/ +if (WARN_ON (wq->flags & __WQ_ORDERED)) +return -EINVAL ; + + wq->wq_dev = wq_dev =kzalloc (sizeof (*wq_dev),GFP_KERNEL ); +if (!wq_dev) +return -ENOMEM ; + + wq_dev->wq = wq; + wq_dev->dev.bus = &wq_subsys; + wq_dev->dev.init_name = wq->name; + wq_dev->dev.release = wq_device_release; + +/* +* unbound_attrs are created separately. Suppress uevent until +* everything is ready. +*/ +dev_set_uevent_suppress (&wq_dev->dev, true); + + ret =device_register (&wq_dev->dev); +if (ret) { +kfree (wq_dev); + wq->wq_dev =NULL ; +return ret; + } + +if (wq->flags &WQ_UNBOUND ) { +struct device_attribute *attr; + +for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { + ret =device_create_file (&wq_dev->dev, attr); +if (ret) { +device_unregister (&wq_dev->dev); + wq->wq_dev =NULL ; +return ret; + } + } + } + +dev_set_uevent_suppress (&wq_dev->dev, false); +kobject_uevent (&wq_dev->dev.kobj,KOBJ_ADD ); +return 0 ; +} + +/** +* workqueue_sysfs_unregister - undo workqueue_sysfs_register() +* @wq: the workqueue to unregister +* +* If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. +*/ +static void workqueue_sysfs_unregister (struct workqueue_struct *wq) +{ +struct wq_device *wq_dev = wq->wq_dev; + +if (!wq->wq_dev) +return ; + + wq->wq_dev =NULL ; +device_unregister (&wq_dev->dev); +} +#else /* CONFIG_SYSFS */ +static void workqueue_sysfs_unregister (struct workqueue_struct *wq) { } +#endif /* CONFIG_SYSFS */ +static int alloc_and_link_pwqs (struct workqueue_struct *wq) {bool highpri = wq->flags &WQ_HIGHPRI ;
Signature · 1 line
2.1.0
PATCH 2/4 workqueue: split apply_workqueue_attrs() into 3 stages
In reply to Lai JiangshanCurrent apply_workqueue_attrs() includes pwqs-allocation and pwqs-installation, so when we batch multiple apply_workqueue_attrs()s as a transaction, we can't ensure the transaction must succeed or fail as a complete unit.
To solve this, we split apply_workqueue_attrs() into three stages. The first stage does the preparation: allocation memory, pwqs. The second stage does the attrs-installaion and pwqs-installation. The third stage frees the allocated memory and (old or unused) pwqs.
As the result, batching multiple apply_workqueue_attrs()s can succeed or fail as a complete unit:
1) batch do all the first stage for all the workqueues
2) only commit all when all the above succeed.
This patch is a preparation for the next patch ("Allow modifying low level unbound workqueue cpumask") which will do a multiple apply_workqueue_attrs().
The patch doesn't have functionality changed except two minor adjustment:
1) free_unbound_pwq() for the error path is removed, we use the heavier version put_pwq_unlocked() instead since the error path is rare. this adjustment simplifies the code.
2) the memory-allocation is also moved into wq_pool_mutex. this is needed to avoid to do the further splitting.
--- kernel/workqueue.c | 199 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 115 insertions(+), 84 deletions(-)
kernel/workqueue.c +115 -84
@@ -3424,17 +3424,6 @@ static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,return pwq; } -/* undo alloc_unbound_pwq(), used only in the error path */ -static void free_unbound_pwq (struct pool_workqueue *pwq) -{ -lockdep_assert_held (&wq_pool_mutex); - -if (pwq) { -put_unbound_pool (pwq->pool); -kmem_cache_free (pwq_cache, pwq); - } -} -/** * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node * @attrs: the wq_attrs of interest
@@ -3497,42 +3486,46 @@ static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,return old_pwq; } -/** -* 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) +struct wq_unbound_install_ctx { +struct workqueue_struct *wq;/* target to be installed */ +struct workqueue_attrs *attrs;/* attrs for installing */ +struct pool_workqueue *dfl_pwq; +struct pool_workqueue *pwq_tbl[]; +}; + +static void wq_unbound_install_ctx_free (struct wq_unbound_install_ctx *ctx) { +int node; + +if (ctx) { +/* put the pwqs */ +for_each_node (node) +put_pwq_unlocked (ctx->pwq_tbl[node]); +put_pwq_unlocked (ctx->dfl_pwq); + +free_workqueue_attrs (ctx->attrs); + } + +kfree (ctx); +} + +static struct wq_unbound_install_ctx * +wq_unbound_install_ctx_prepare (struct workqueue_struct *wq, +const struct workqueue_attrs *attrs) +{ +struct wq_unbound_install_ctx *ctx;struct workqueue_attrs *new_attrs, *tmp_attrs; -struct pool_workqueue **pwq_tbl, *dfl_pwq; -int node, ret; +int node; -/* only unbound workqueues can change attributes */ -if (WARN_ON (!(wq->flags &WQ_UNBOUND ))) -return -EINVAL ; +lockdep_assert_held (&wq_pool_mutex); -/* creating multiple pwqs breaks ordering guarantee */ -if (WARN_ON ((wq->flags & __WQ_ORDERED) && !list_empty (&wq->pwqs))) -return -EINVAL ; + ctx =kzalloc (sizeof (*ctx) + nr_node_ids *sizeof (ctx->pwq_tbl[0 ]), +GFP_KERNEL ); - pwq_tbl =kzalloc (nr_node_ids *sizeof (pwq_tbl[0 ]),GFP_KERNEL ); new_attrs =alloc_workqueue_attrs (GFP_KERNEL ); tmp_attrs =alloc_workqueue_attrs (GFP_KERNEL ); -if (!pwq_tbl || !new_attrs || !tmp_attrs) - goto enomem; +if (!ctx || !new_attrs || !tmp_attrs) + goto out_free;/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs);
@@ -3546,75 +3539,113 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,copy_workqueue_attrs (tmp_attrs, new_attrs);/* -* 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); - -/* * If something goes wrong during CPU up/down, we'll fall back to * the default pwq covering whole @attrs->cpumask. Always create * it even if we don't use it immediately. */ - dfl_pwq =alloc_unbound_pwq (wq, new_attrs); -if (!dfl_pwq) - goto enomem_pwq; + ctx->dfl_pwq =alloc_unbound_pwq (wq, new_attrs); +if (!ctx->dfl_pwq) + goto out_free;for_each_node (node) {if (wq_calc_node_cpumask (attrs, node,-1 , tmp_attrs->cpumask)) { - pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs); -if (!pwq_tbl[node]) - goto enomem_pwq; + ctx->pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs); +if (!ctx->pwq_tbl[node]) + goto out_free; }else { - dfl_pwq->refcnt++; - pwq_tbl[node] = dfl_pwq; + ctx->dfl_pwq->refcnt++; + ctx->pwq_tbl[node] = ctx->dfl_pwq; } } -mutex_unlock (&wq_pool_mutex); + ctx->wq = wq; + ctx->attrs = new_attrs; + +out_free: +free_workqueue_attrs (tmp_attrs); + +if (!ctx || !ctx->wq) { +kfree (new_attrs); +wq_unbound_install_ctx_free (ctx); +return NULL ; + }else { +return ctx; + } +} + +static void wq_unbound_install_ctx_commit (struct wq_unbound_install_ctx *ctx) +{ +int node;/* all pwqs have been created successfully, let's install'em */ -mutex_lock (&wq->mutex); +mutex_lock (&ctx->wq->mutex); -copy_workqueue_attrs (wq->unbound_attrs, new_attrs); +copy_workqueue_attrs (ctx->wq->unbound_attrs, ctx->attrs);/* save the previous pwq and install the new one */ for_each_node (node) - pwq_tbl[node] =numa_pwq_tbl_install (wq, node, pwq_tbl[node]); + ctx->pwq_tbl[node] =numa_pwq_tbl_install (ctx->wq, node, + ctx->pwq_tbl[node]);/* @dfl_pwq might not have been used, ensure it's linked */ -link_pwq (dfl_pwq); -swap (wq->dfl_pwq, dfl_pwq); +link_pwq (ctx->dfl_pwq); +swap (ctx->wq->dfl_pwq, ctx->dfl_pwq); -mutex_unlock (&wq->mutex); +mutex_unlock (&ctx->wq->mutex); +} -/* put the old pwqs */ -for_each_node (node) -put_pwq_unlocked (pwq_tbl[node]); -put_pwq_unlocked (dfl_pwq); +/** +* 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) +{ +struct wq_unbound_install_ctx *ctx; +int ret = -ENOMEM ; -put_online_cpus (); - ret =0 ; -/* fall through */ -out_free: -free_workqueue_attrs (tmp_attrs); -free_workqueue_attrs (new_attrs); -kfree (pwq_tbl); -return ret; +/* only unbound workqueues can change attributes */ +if (WARN_ON (!(wq->flags &WQ_UNBOUND ))) +return -EINVAL ; -enomem_pwq: -free_unbound_pwq (dfl_pwq); -for_each_node (node) -if (pwq_tbl && pwq_tbl[node] != dfl_pwq) -free_unbound_pwq (pwq_tbl[node]); +/* creating multiple pwqs breaks ordering guarantee */ +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 =wq_unbound_install_ctx_prepare (wq, attrs);mutex_unlock (&wq_pool_mutex); +put_online_cpus (); -enomem: - ret = -ENOMEM ; - goto out_free; + +/* the ctx has been prepared successfully, let's commit it */ +if (ctx) { +wq_unbound_install_ctx_commit (ctx); + ret =0 ; + } + +wq_unbound_install_ctx_free (ctx); + +return ret; }/**
Signature · 1 line
2.1.0
PATCH 3/4 workqueue: Create low-level unbound workqueues cpumask
In reply to Lai JiangshanFrom: Frederic Weisbecker <[email protected]>
Create a cpumask that limit the affinity of all unbound workqueues. This cpumask is controlled though a file at the root of the workqueue sysfs directory.
It works on a lower-level than the per WQ_SYSFS workqueues cpumask files such that the effective cpumask applied for a given unbound workqueue is the intersection of /sys/devices/virtual/workqueue/$WORKQUEUE/cpumask and the new /sys/devices/virtual/workqueue/cpumask_unbounds file.
This patch implements the basic infrastructure and the read interface. cpumask_unbounds is initially set to cpu_possible_mask.
--- kernel/workqueue.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
kernel/workqueue.c +27 -2
@@ -299,6 +299,8 @@ static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */static LIST_HEAD (workqueues);/* PR: list of all workqueues */ static bool workqueue_freezing;/* PL: have wqs started freezing? */ +static cpumask_var_t wq_unbound_cpumask; +/* the per-cpu worker pools */ static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools);
@@ -3529,7 +3531,7 @@ wq_unbound_install_ctx_prepare(struct workqueue_struct *wq,/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask);/* * We may create multiple pwqs with differing cpumasks. Make a
@@ -3959,9 +3961,29 @@ static struct bus_type wq_subsys = { .dev_groups = wq_sysfs_groups, }; +static ssize_t unbounds_cpumask_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +int written; + + written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , +cpumask_pr_args (wq_unbound_cpumask)); + +return written; +} + +static struct device_attribute wq_sysfs_cpumask_attr = +__ATTR (cpumask,0444 , unbounds_cpumask_show,NULL ); +static int __init wq_sysfs_init (void ) { -return subsys_virtual_register (&wq_subsys,NULL ); +int err; + + err =subsys_virtual_register (&wq_subsys,NULL ); +if (err) +return err; + +return device_create_file (wq_subsys.dev_root, &wq_sysfs_cpumask_attr); }core_initcall (wq_sysfs_init);
@@ -5094,6 +5116,9 @@ static int __init init_workqueues(void)WARN_ON (__alignof__(struct pool_workqueue ) < __alignof__(long long )); +BUG_ON (!alloc_cpumask_var (&wq_unbound_cpumask,GFP_KERNEL )); +cpumask_copy (wq_unbound_cpumask, cpu_possible_mask); + pwq_cache =KMEM_CACHE (pool_workqueue,SLAB_PANIC );cpu_notifier (workqueue_cpu_up_callback,CPU_PRI_WORKQUEUE_UP );
Signature · 1 line
2.1.0
Re: [PATCH 3/4] workqueue: Create low-level unbound workqueues cpumask
Quoted 2 lines · Lai Jiangshan
This patch implements the basic infrastructure and the read interface.
cpumask_unboundsis initially set tocpu_possible_mask.
Re: [PATCH 3/4] workqueue: Create low-level unbound workqueues cpumask
In reply to Lai JiangshanQuoted 23 lines · Lai Jiangshan
From: Frederic Weisbecker <[email protected]>
Create a cpumask that limit the affinity of all unbound workqueues. This cpumask is controlled though a file at the root of the workqueue sysfs directory.
It works on a lower-level than the per
WQ_SYSFSworkqueues cpumask files such that the effective cpumask applied for a given unbound workqueue is the intersection of/sys/devices/virtual/workqueue/$WORKQUEUE/cpumaskand the new/sys/devices/virtual/workqueue/cpumask_unboundsfile.This patch implements the basic infrastructure and the read interface.
cpumask_unboundsis initially set tocpu_possible_mask.Cc Christoph Lameter <[email protected]>Cc Kevin Hilman <[email protected]>Cc Lai Jiangshan <[email protected]>Cc Mike Galbraith <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Tejun Heo <[email protected]>Cc Viresh Kumar <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>Signed-off-by Lai Jiangshan <[email protected]>
[...]
kernel/workqueue.c static int __init init_workqueues(void) · line 5121
Quoted 7 lines
kernel/workqueue.c +3
-0@@ -5094,6 +5116,9 @@ static int __init init_workqueues(void)WARN_ON (__alignof__(struct pool_workqueue ) < __alignof__(long long )); +BUG_ON (!alloc_cpumask_var (&wq_unbound_cpumask,GFP_KERNEL )); +cpumask_copy (wq_unbound_cpumask, cpu_possible_mask); +
As I mentioned in an earlier discussion[1], I still think this could default too the housekeeping CPUs in the NO_HZ_FULL case:
#ifdef CONFIG_NO_HZ_FULL cpumask_complement(wq_unbound_cpumask, tick_nohz_full_mask); #else cpumask_copy(wq_unbound_cpumask, cpu_possible_mask);
#endif
But that could also be left to a future optimization as well.
Kevin
Kevin Hilman
Acked-byRe: [PATCH 3/4] workqueue: Create low-level unbound workqueues cpumask
Quoted 39 lines · Kevin Hilman
Quoted 23 lines · Lai Jiangshan
From: Frederic Weisbecker <[email protected]>
Create a cpumask that limit the affinity of all unbound workqueues. This cpumask is controlled though a file at the root of the workqueue sysfs directory.
It works on a lower-level than the per
WQ_SYSFSworkqueues cpumask files such that the effective cpumask applied for a given unbound workqueue is the intersection of/sys/devices/virtual/workqueue/$WORKQUEUE/cpumaskand the new/sys/devices/virtual/workqueue/cpumask_unboundsfile.This patch implements the basic infrastructure and the read interface.
cpumask_unboundsis initially set tocpu_possible_mask.Cc Christoph Lameter <[email protected]>Cc Kevin Hilman <[email protected]>Cc Lai Jiangshan <[email protected]>Cc Mike Galbraith <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Tejun Heo <[email protected]>Cc Viresh Kumar <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>Signed-off-by Lai Jiangshan <[email protected]>[...]
kernel/workqueue.c static int __init init_workqueues(void) · line 5121
Quoted 7 lines
kernel/workqueue.c +3
-0@@ -5094,6 +5116,9 @@ static int __init init_workqueues(void)WARN_ON (__alignof__(struct pool_workqueue ) < __alignof__(long long )); +BUG_ON (!alloc_cpumask_var (&wq_unbound_cpumask,GFP_KERNEL )); +cpumask_copy (wq_unbound_cpumask, cpu_possible_mask); +As I mentioned in an earlier discussion[1], I still think this could default too the housekeeping CPUs in the
NO_HZ_FULLcase:
Quoted 7 lines
#ifdef CONFIG_NO_HZ_FULL cpumask_complement(wq_unbound_cpumask, tick_nohz_full_mask); #else cpumask_copy(wq_unbound_cpumask, cpu_possible_mask);#endif
But that could also be left to a future optimization as well.
I meant to add:
as well, as the NO_HZ_FULL bit could be added as an add-on patch.
Kevin
Re: [PATCH 3/4] workqueue: Create low-level unbound workqueues cpumask
In reply to Kevin HilmanQuoted 39 lines · Kevin Hilman
Quoted 23 lines · Lai Jiangshan
From: Frederic Weisbecker <[email protected]>
Create a cpumask that limit the affinity of all unbound workqueues. This cpumask is controlled though a file at the root of the workqueue sysfs directory.
It works on a lower-level than the per
WQ_SYSFSworkqueues cpumask files such that the effective cpumask applied for a given unbound workqueue is the intersection of/sys/devices/virtual/workqueue/$WORKQUEUE/cpumaskand the new/sys/devices/virtual/workqueue/cpumask_unboundsfile.This patch implements the basic infrastructure and the read interface.
cpumask_unboundsis initially set tocpu_possible_mask.Cc Christoph Lameter <[email protected]>Cc Kevin Hilman <[email protected]>Cc Lai Jiangshan <[email protected]>Cc Mike Galbraith <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Tejun Heo <[email protected]>Cc Viresh Kumar <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>Signed-off-by Lai Jiangshan <[email protected]>[...]
kernel/workqueue.c static int __init init_workqueues(void) · line 5121
Quoted 7 lines
kernel/workqueue.c +3
-0@@ -5094,6 +5116,9 @@ static int __init init_workqueues(void)WARN_ON (__alignof__(struct pool_workqueue ) < __alignof__(long long )); +BUG_ON (!alloc_cpumask_var (&wq_unbound_cpumask,GFP_KERNEL )); +cpumask_copy (wq_unbound_cpumask, cpu_possible_mask); +As I mentioned in an earlier discussion[1], I still think this could default too the housekeeping CPUs in the
NO_HZ_FULLcase:
Quoted 2 lines
#ifdef CONFIG_NO_HZ_FULL cpumask_complement(wq_unbound_cpumask, tick_nohz_full_mask);
No, the default/booted wq_unbound_cpumask should be cpu_possible_mask.
Quoted 11 lines
#else cpumask_copy(wq_unbound_cpumask, cpu_possible_mask);#endif
But that could also be left to a future optimization as well.
Kevin
Re: [PATCH 3/4] workqueue: Create low-level unbound workqueues cpumask
Quoted 40 lines · Lai Jiangshan
Quoted 39 lines · Kevin Hilman
Quoted 23 lines · Lai Jiangshan
From: Frederic Weisbecker <[email protected]>
Create a cpumask that limit the affinity of all unbound workqueues. This cpumask is controlled though a file at the root of the workqueue sysfs directory.
It works on a lower-level than the per
WQ_SYSFSworkqueues cpumask files such that the effective cpumask applied for a given unbound workqueue is the intersection of/sys/devices/virtual/workqueue/$WORKQUEUE/cpumaskand the new/sys/devices/virtual/workqueue/cpumask_unboundsfile.This patch implements the basic infrastructure and the read interface.
cpumask_unboundsis initially set tocpu_possible_mask.Cc Christoph Lameter <[email protected]>Cc Kevin Hilman <[email protected]>Cc Lai Jiangshan <[email protected]>Cc Mike Galbraith <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Tejun Heo <[email protected]>Cc Viresh Kumar <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>Signed-off-by Lai Jiangshan <[email protected]>[...]
kernel/workqueue.c static int __init init_workqueues(void) · line 5121
Quoted 7 lines
kernel/workqueue.c +3
-0@@ -5094,6 +5116,9 @@ static int __init init_workqueues(void)WARN_ON (__alignof__(struct pool_workqueue ) < __alignof__(long long )); +BUG_ON (!alloc_cpumask_var (&wq_unbound_cpumask,GFP_KERNEL )); +cpumask_copy (wq_unbound_cpumask, cpu_possible_mask); +As I mentioned in an earlier discussion[1], I still think this could default too the housekeeping CPUs in the
NO_HZ_FULLcase:
Quoted 6 lines
Quoted 2 lines
#ifdef CONFIG_NO_HZ_FULL cpumask_complement(wq_unbound_cpumask, tick_nohz_full_mask);No, the default/booted
wq_unbound_cpumaskshould becpu_possible_mask.
Even for NO_HZ_FULL?
IMO, for NO_HZ_FULL, we want the unbound workqueues to be on the housekeeping CPU(s).
Kevin
Re: [PATCH 3/4] workqueue: Create low-level unbound workqueues cpumask
Quoted 46 lines · Kevin Hilman
Quoted 44 lines · Lai Jiangshan
Quoted 41 lines · Kevin Hilman
Quoted 23 lines · Lai Jiangshan
From: Frederic Weisbecker <[email protected]>
Create a cpumask that limit the affinity of all unbound workqueues. This cpumask is controlled though a file at the root of the workqueue sysfs directory.
It works on a lower-level than the per
WQ_SYSFSworkqueues cpumask files such that the effective cpumask applied for a given unbound workqueue is the intersection of/sys/devices/virtual/workqueue/$WORKQUEUE/cpumaskand the new/sys/devices/virtual/workqueue/cpumask_unboundsfile.This patch implements the basic infrastructure and the read interface.
cpumask_unboundsis initially set tocpu_possible_mask.Cc Christoph Lameter <[email protected]>Cc Kevin Hilman <[email protected]>Cc Lai Jiangshan <[email protected]>Cc Mike Galbraith <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Tejun Heo <[email protected]>Cc Viresh Kumar <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>Signed-off-by Lai Jiangshan <[email protected]>[...]
kernel/workqueue.c static int __init init_workqueues(void) · line 5121
Quoted 7 lines
kernel/workqueue.c +3
-0@@ -5094,6 +5116,9 @@ static int __init init_workqueues(void)WARN_ON (__alignof__(struct pool_workqueue ) < __alignof__(long long )); +BUG_ON (!alloc_cpumask_var (&wq_unbound_cpumask,GFP_KERNEL )); +cpumask_copy (wq_unbound_cpumask, cpu_possible_mask); +As I mentioned in an earlier discussion[1], I still think this could default too the housekeeping CPUs in the
NO_HZ_FULLcase:#ifdef CONFIG_NO_HZ_FULL cpumask_complement(wq_unbound_cpumask, tick_nohz_full_mask);
Quoted 7 lines
Quoted 2 lines
No, the default/booted
wq_unbound_cpumaskshould becpu_possible_mask.Even for
NO_HZ_FULL?IMO, for
NO_HZ_FULL, we want the unbound workqueues to be on the housekeeping CPU(s).
If it should be the default on NO_HZ_FULL, maybe we should do this from the tick nohz code. Some late or fs initcall that will do the workqueue affinity, timer affinity, etc...
Re: [PATCH 3/4] workqueue: Create low-level unbound workqueues cpumask
Quoted 4 lines · Frederic Weisbecker
Quoted 3 lines · Kevin Hilman
Quoted 1 line · Lai Jiangshan
On 03/14/2015 07:49 AM, Kevin Hilman wrote:
[...]
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
As I mentioned in an earlier discussion[1], I still think this could default too the housekeeping CPUs in the
NO_HZ_FULLcase:#ifdef CONFIG_NO_HZ_FULL cpumask_complement(wq_unbound_cpumask, tick_nohz_full_mask);No, the default/booted
wq_unbound_cpumaskshould becpu_possible_mask.
Quoted 8 lines
Quoted 4 lines
Even for
NO_HZ_FULL?IMO, for
NO_HZ_FULL, we want the unbound workqueues to be on the housekeeping CPU(s).If it should be the default on
NO_HZ_FULL, maybe we should do this from the tick nohz code. Some late or fs initcall that will do the workqueue affinity, timer affinity, etc...
Sure, I'd be fine with that too.
Kevin
PATCH 4/4 workqueue: Allow modifying low level unbound workqueue cpumask
In reply to Lai JiangshanAllow to modify the low-level unbound workqueues cpumask through sysfs. This is performed by traversing the entire workqueue list and calling wq_unbound_install_ctx_prepare() on the unbound workqueues with the low level mask passed in. Only after all the preparation are done, we commit them all together.
The oreder-workquue is ignore from the low level unbound workqueue cpumask, it will be handled in near future.
The per-nodes' pwqs are mandatorily controlled by the low level cpumask, while the default pwq ignores the low level cpumask when (and ONLY when) the cpumask set by the user doesn't overlap with the low level cpumask. In this case, we can't apply the empty cpumask to the default pwq, so we use the user-set cpumask directly.
--- kernel/workqueue.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 8 deletions(-)
kernel/workqueue.c +88 -8
@@ -299,7 +299,7 @@ static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */static LIST_HEAD (workqueues);/* PR: list of all workqueues */ static bool workqueue_freezing;/* PL: have wqs started freezing? */ -static cpumask_var_t wq_unbound_cpumask; +static cpumask_var_t wq_unbound_cpumask;/* PL: low level cpumask for all unbound wqs */ /* the per-cpu worker pools */ static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
@@ -3491,6 +3491,7 @@ static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,struct wq_unbound_install_ctx {struct workqueue_struct *wq;/* target to be installed */ struct workqueue_attrs *attrs;/* attrs for installing */ +struct list_head list;/* queued for batching commit */ struct pool_workqueue *dfl_pwq;struct pool_workqueue *pwq_tbl[]; };
@@ -3513,10 +3514,11 @@ static void wq_unbound_install_ctx_free(struct wq_unbound_install_ctx *ctx)static struct wq_unbound_install_ctx *wq_unbound_install_ctx_prepare (struct workqueue_struct *wq, -const struct workqueue_attrs *attrs) +const struct workqueue_attrs *attrs, +cpumask_var_t unbound_cpumask) {struct wq_unbound_install_ctx *ctx; -struct workqueue_attrs *new_attrs, *tmp_attrs; +struct workqueue_attrs *new_attrs, *pwq_attrs, *tmp_attrs;int node;lockdep_assert_held (&wq_pool_mutex);
@@ -3525,13 +3527,16 @@ wq_unbound_install_ctx_prepare(struct workqueue_struct *wq, GFP_KERNEL); new_attrs =alloc_workqueue_attrs (GFP_KERNEL ); + pwq_attrs =alloc_workqueue_attrs (GFP_KERNEL ); tmp_attrs =alloc_workqueue_attrs (GFP_KERNEL );if (!ctx || !new_attrs || !tmp_attrs) goto out_free;/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); +copy_workqueue_attrs (pwq_attrs, attrs); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (pwq_attrs->cpumask, pwq_attrs->cpumask, unbound_cpumask);/* * We may create multiple pwqs with differing cpumasks. Make a
@@ -3544,13 +3549,21 @@ wq_unbound_install_ctx_prepare(struct workqueue_struct *wq,* If something goes wrong during CPU up/down, we'll fall back to * the default pwq covering whole @attrs->cpumask. Always create * it even if we don't use it immediately. +* +* If the cpumask set by the user doesn't overlap with the global +* wq_unbound_cpumask, we ignore the wq_unbound_cpumask for this wq +* which means all its nodes' pwqs are its default pwq and its default +* pwq's workers' cpumask is totally equals to the user setting. */ - ctx->dfl_pwq =alloc_unbound_pwq (wq, new_attrs); +if (cpumask_empty (pwq_attrs->cpumask)) + ctx->dfl_pwq =alloc_unbound_pwq (wq, new_attrs); +else + ctx->dfl_pwq =alloc_unbound_pwq (wq, pwq_attrs);if (!ctx->dfl_pwq) goto out_free;for_each_node (node) { -if (wq_calc_node_cpumask (attrs, node,-1 , tmp_attrs->cpumask)) { +if (wq_calc_node_cpumask (pwq_attrs, node,-1 , tmp_attrs->cpumask)) { ctx->pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs);if (!ctx->pwq_tbl[node]) goto out_free;
@@ -3564,6 +3577,7 @@ wq_unbound_install_ctx_prepare(struct workqueue_struct *wq, ctx->attrs = new_attrs; out_free: +free_workqueue_attrs (pwq_attrs);free_workqueue_attrs (tmp_attrs);if (!ctx || !ctx->wq) {
@@ -3634,7 +3648,7 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,get_online_cpus ();mutex_lock (&wq_pool_mutex); - ctx =wq_unbound_install_ctx_prepare (wq, attrs); + ctx =wq_unbound_install_ctx_prepare (wq, attrs, wq_unbound_cpumask);mutex_unlock (&wq_pool_mutex);put_online_cpus ();
@@ -3961,19 +3975,85 @@ static struct bus_type wq_subsys = { .dev_groups = wq_sysfs_groups, }; +static int unbounds_cpumask_apply (cpumask_var_t cpumask) +{ +LIST_HEAD (ctxs); +int ret =0 ; +struct workqueue_struct *wq; +struct wq_unbound_install_ctx *ctx, *n; + +lockdep_assert_held (&wq_pool_mutex); + +list_for_each_entry (wq, &workqueues, list) { +if (!(wq->flags &WQ_UNBOUND )) +continue ; +/* creating multiple pwqs breaks ordering guarantee */ +if (wq->flags & __WQ_ORDERED) +continue ; + + ctx =wq_unbound_install_ctx_prepare (wq, wq->unbound_attrs, + cpumask); +if (!ctx) { + ret = -ENOMEM ; +break ; + } + +list_add_tail (&ctx->list, &ctxs); + } + +list_for_each_entry_safe (ctx, n, &ctxs, list) { +if (ret >=0 ) +wq_unbound_install_ctx_commit (ctx); +wq_unbound_install_ctx_free (ctx); + } + +return ret; +} + +static ssize_t unbounds_cpumask_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +cpumask_var_t cpumask; +int ret = -EINVAL ; + +if (!zalloc_cpumask_var (&cpumask,GFP_KERNEL )) +return -ENOMEM ; + + ret =cpumask_parse (buf, cpumask); +if (ret) + goto out; + +get_online_cpus (); +cpumask_and (cpumask, cpumask, cpu_possible_mask); +if (cpumask_intersects (cpumask, cpu_online_mask)) { +mutex_lock (&wq_pool_mutex); + ret =unbounds_cpumask_apply (cpumask); +if (ret >=0 ) +cpumask_copy (wq_unbound_cpumask, cpumask); +mutex_unlock (&wq_pool_mutex); + } +put_online_cpus (); +out: +free_cpumask_var (cpumask); +return ret ? ret : count; +} +static ssize_t unbounds_cpumask_show (struct device *dev,struct device_attribute *attr,char *buf) {int written; +mutex_lock (&wq_pool_mutex); written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" ,cpumask_pr_args (wq_unbound_cpumask)); +mutex_unlock (&wq_pool_mutex);return written; }static struct device_attribute wq_sysfs_cpumask_attr = -__ATTR (cpumask,0444 , unbounds_cpumask_show,NULL ); +__ATTR (cpumask,0644 , unbounds_cpumask_show, unbounds_cpumask_store);static int __init wq_sysfs_init (void ) {
Signature · 1 line
2.1.0
Re: [PATCH 4/4] workqueue: Allow modifying low level unbound workqueue cpumask
Quoted 5 lines · Lai Jiangshan
The per-nodes' pwqs are mandatorily controlled by the low level cpumask, while the default pwq ignores the low level cpumask when (and ONLY when) the cpumask set by the user doesn't overlap with the low level cpumask. In this case, we can't apply the empty cpumask to the default pwq, so we use the user-set cpumask directly.
I am wondering now why we have two cpumasks? A script can just interate through the work queues if we want to set them all right? Then we do not have to deal with the conflict between the settings in the kernel.
Re: [PATCH 4/4] workqueue: Allow modifying low level unbound workqueue cpumask
Quoted 9 lines · Christoph Lameter
Quoted 5 lines · Lai Jiangshan
The per-nodes' pwqs are mandatorily controlled by the low level cpumask, while the default pwq ignores the low level cpumask when (and ONLY when) the cpumask set by the user doesn't overlap with the low level cpumask. In this case, we can't apply the empty cpumask to the default pwq, so we use the user-set cpumask directly.
I am wondering now why we have two cpumasks?
What's your meaning? which two cpumask?
The per-nodes' pwqs' cpumask and the default pwq's cpumask? They refer to different pool, so they have different cpumask.
1)
If the per-nodes' pwqs exist, they were controlled by A
(A = user-set-cpumask & possible-cpus-of-the-node). Now after this patch, they are controlled by B (B = A & the-low-level-cpumak).
if A is empty or B is empty, we used default pwq for the node.
2)
The default pwq is different, it was controlled by C (C = user-set-cpumask), and after this patch, it will be controlled by D
(D = user-set-cpumask(C) & the-low-level-cpumask), But D may be empty, we can't have a default pwq with empty cpumask, so we have
use C instead in this case.
Quoted 3 lines
A script can just interate
through the work queues if we want to set them all right? Then we do not have to deal with the conflict between the settings in the kernel.
wq->unbound_attrs->cpumask and the-low-level-cpumask can be set by users. they may be set different or even non-intersect, the non-intersect case is not real conflict, but it is possible, we have to handle it.
Re: [PATCH 4/4] workqueue: Allow modifying low level unbound workqueue cpumask
In reply to Lai JiangshanQuoted 15 lines · Lai Jiangshan
Allow to modify the low-level unbound workqueues cpumask through sysfs. This is performed by traversing the entire workqueue list and calling
wq_unbound_install_ctx_prepare()on the unbound workqueues with the low level mask passed in. Only after all the preparation are done, we commit them all together.The oreder-workquue is ignore from the low level unbound workqueue cpumask, it will be handled in near future.
The per-nodes' pwqs are mandatorily controlled by the low level cpumask, while the default pwq ignores the low level cpumask when (and ONLY when) the cpumask set by the user doesn't overlap with the low level cpumask. In this case, we can't apply the empty cpumask to the default pwq, so we use the user-set cpumask directly.
Quoted 10 lines
Cc Christoph Lameter <[email protected]>Cc Kevin Hilman <[email protected]>Cc Lai Jiangshan <[email protected]>Cc Mike Galbraith <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Tejun Heo <[email protected]>Cc Viresh Kumar <[email protected]>Cc Frederic Weisbecker <[email protected]>Original-patch-by Frederic Weisbecker <[email protected]>Signed-off-by Lai Jiangshan <[email protected]>
miss a part in wq_update_unbound_numa()
kernel/workqueue.c +3 -0
@@ -3722,6 +3722,9 @@ static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,* wq's, the default pwq should be used. */ if (wq_calc_node_cpumask (wq->unbound_attrs, node, cpu_off, cpumask)) { +cpumask_and (cpumask, cpumask, wq_unbound_cpumask); +if (cpumask_empty (cpumask)) + goto use_dfl_pwq;if (cpumask_equal (cpumask, pwq->pool->attrs->cpumask)) goto out_unlock; }else {
Quoted 186 lines
--- kernel/workqueue.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 8 deletions(-)kernel/workqueue.c +88
-8@@ -299,7 +299,7 @@ static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */static LIST_HEAD (workqueues);/* PR: list of all workqueues */ static bool workqueue_freezing;/* PL: have wqs started freezing? */ -static cpumask_var_t wq_unbound_cpumask; +static cpumask_var_t wq_unbound_cpumask;/* PL: low level cpumask for all unbound wqs */ /* the per-cpu worker pools */ static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],@@ -3491,6 +3491,7 @@ static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,struct wq_unbound_install_ctx {struct workqueue_struct *wq;/* target to be installed */ struct workqueue_attrs *attrs;/* attrs for installing */ +struct list_head list;/* queued for batching commit */ struct pool_workqueue *dfl_pwq;struct pool_workqueue *pwq_tbl[]; };@@ -3513,10 +3514,11 @@ static void wq_unbound_install_ctx_free(struct wq_unbound_install_ctx *ctx)static struct wq_unbound_install_ctx *wq_unbound_install_ctx_prepare (struct workqueue_struct *wq, -const struct workqueue_attrs *attrs) +const struct workqueue_attrs *attrs, +cpumask_var_t unbound_cpumask) {struct wq_unbound_install_ctx *ctx; -struct workqueue_attrs *new_attrs, *tmp_attrs; +struct workqueue_attrs *new_attrs, *pwq_attrs, *tmp_attrs;int node;lockdep_assert_held (&wq_pool_mutex);@@ -3525,13 +3527,16 @@ wq_unbound_install_ctx_prepare(struct workqueue_struct *wq, GFP_KERNEL); new_attrs =alloc_workqueue_attrs (GFP_KERNEL ); + pwq_attrs =alloc_workqueue_attrs (GFP_KERNEL ); tmp_attrs =alloc_workqueue_attrs (GFP_KERNEL );if (!ctx || !new_attrs || !tmp_attrs) goto out_free;/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); +copy_workqueue_attrs (pwq_attrs, attrs); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (pwq_attrs->cpumask, pwq_attrs->cpumask, unbound_cpumask);/* * We may create multiple pwqs with differing cpumasks. Make a @@ -3544,13 +3549,21 @@ wq_unbound_install_ctx_prepare(struct workqueue_struct *wq,* If something goes wrong during CPU up/down, we'll fall back to * the default pwq covering whole @attrs->cpumask. Always create * it even if we don't use it immediately. +* +* If the cpumask set by the user doesn't overlap with the global +* wq_unbound_cpumask, we ignore the wq_unbound_cpumask for this wq +* which means all its nodes' pwqs are its default pwq and its default +* pwq's workers' cpumask is totally equals to the user setting. */ - ctx->dfl_pwq =alloc_unbound_pwq (wq, new_attrs); +if (cpumask_empty (pwq_attrs->cpumask)) + ctx->dfl_pwq =alloc_unbound_pwq (wq, new_attrs); +else + ctx->dfl_pwq =alloc_unbound_pwq (wq, pwq_attrs);if (!ctx->dfl_pwq) goto out_free;for_each_node (node) { -if (wq_calc_node_cpumask (attrs, node,-1 , tmp_attrs->cpumask)) { +if (wq_calc_node_cpumask (pwq_attrs, node,-1 , tmp_attrs->cpumask)) { ctx->pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs);if (!ctx->pwq_tbl[node]) goto out_free;@@ -3564,6 +3577,7 @@ wq_unbound_install_ctx_prepare(struct workqueue_struct *wq, ctx->attrs = new_attrs; out_free: +free_workqueue_attrs (pwq_attrs);free_workqueue_attrs (tmp_attrs);if (!ctx || !ctx->wq) {@@ -3634,7 +3648,7 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,get_online_cpus ();mutex_lock (&wq_pool_mutex); - ctx =wq_unbound_install_ctx_prepare (wq, attrs); + ctx =wq_unbound_install_ctx_prepare (wq, attrs, wq_unbound_cpumask);mutex_unlock (&wq_pool_mutex);put_online_cpus ();@@ -3961,19 +3975,85 @@ static struct bus_type wq_subsys = { .dev_groups = wq_sysfs_groups, }; +static int unbounds_cpumask_apply (cpumask_var_t cpumask) +{ +LIST_HEAD (ctxs); +int ret =0 ; +struct workqueue_struct *wq; +struct wq_unbound_install_ctx *ctx, *n; + +lockdep_assert_held (&wq_pool_mutex); + +list_for_each_entry (wq, &workqueues, list) { +if (!(wq->flags &WQ_UNBOUND )) +continue ; +/* creating multiple pwqs breaks ordering guarantee */ +if (wq->flags & __WQ_ORDERED) +continue ; + + ctx =wq_unbound_install_ctx_prepare (wq, wq->unbound_attrs, + cpumask); +if (!ctx) { + ret = -ENOMEM ; +break ; + } + +list_add_tail (&ctx->list, &ctxs); + } + +list_for_each_entry_safe (ctx, n, &ctxs, list) { +if (ret >=0 ) +wq_unbound_install_ctx_commit (ctx); +wq_unbound_install_ctx_free (ctx); + } + +return ret; +} + +static ssize_t unbounds_cpumask_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +cpumask_var_t cpumask; +int ret = -EINVAL ; + +if (!zalloc_cpumask_var (&cpumask,GFP_KERNEL )) +return -ENOMEM ; + + ret =cpumask_parse (buf, cpumask); +if (ret) + goto out; + +get_online_cpus (); +cpumask_and (cpumask, cpumask, cpu_possible_mask); +if (cpumask_intersects (cpumask, cpu_online_mask)) { +mutex_lock (&wq_pool_mutex); + ret =unbounds_cpumask_apply (cpumask); +if (ret >=0 ) +cpumask_copy (wq_unbound_cpumask, cpumask); +mutex_unlock (&wq_pool_mutex); + } +put_online_cpus (); +out: +free_cpumask_var (cpumask); +return ret ? ret : count; +} +static ssize_t unbounds_cpumask_show (struct device *dev,struct device_attribute *attr,char *buf) {int written; +mutex_lock (&wq_pool_mutex); written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" ,cpumask_pr_args (wq_unbound_cpumask)); +mutex_unlock (&wq_pool_mutex);return written; }static struct device_attribute wq_sysfs_cpumask_attr = -__ATTR (cpumask,0444 , unbounds_cpumask_show,NULL ); +__ATTR (cpumask,0644 , unbounds_cpumask_show, unbounds_cpumask_store);static int __init wq_sysfs_init (void ) {
Quoted 6 lines · Lai Jiangshan
This patchset mostly copies from Frederic and split the
apply_workqueue_attrs()as TJ's suggest.This patchset doesn't include the patch "workqueue: Allow changing attributions of ordered workqueues", I hope to reduce the review processing. The handling for the ordered workqueue will be repose after this patchset accepted.
Ah thanks a lot for taking over this patchset. We strongly need this feature :-)
Thanks.
Quoted 26 lines
Thanks,
LaiFrederic Weisbecker (2):
workqueue: Reorder sysfs code
workqueue: Create low-level unbound workqueues cpumaskLai Jiangshan (2):
workqueue: splitapply_workqueue_attrs()into 3 stages
workqueue: Allow modifying low level unbound workqueue cpumaskCc Christoph Lameter <[email protected]>Cc Kevin Hilman <[email protected]>Cc Lai Jiangshan <[email protected]>Cc Mike Galbraith <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Tejun Heo <[email protected]>Cc Viresh Kumar <[email protected]>Cc Frederic Weisbecker <[email protected]>kernel/workqueue.c | 942 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 539 insertions(+), 403 deletions(-)Signature · 1 line
2.1.0
Quoted 2 lines · Lai Jiangshan
This patchset mostly copies from Frederic and split the
apply_workqueue_attrs()as TJ's suggest.
Cool. I'll take these out for a (-rt) test-drive soonish.
-Mike
v5 cover workqueue: Introduce low-level unbound wq sysfs cpumask v5
In reply to Lai JiangshanThis patchset mostly copies from Frederic and split the apply_workqueue_attrs() as TJ's suggest.
This patchset still doesn't include the patch "workqueue: Allow changing attributions of ordered workqueues", I hope to reduce the review processing. The handling for the ordered workqueue will be repose after this patchset accepted.
Changed from V4
Add workqueue_unbounds_cpumask_set() kernel API and minimally restruct the patch4.
Thanks,
Lai
Frederic Weisbecker (2):
workqueue: Reorder sysfs code
workqueue: Create low-level unbound workqueues cpumask
Lai Jiangshan (2):
workqueue: split apply_workqueue_attrs() into 3 stages
workqueue: Allow modifying low level unbound workqueue cpumask
include/linux/workqueue.h | 1 + kernel/workqueue.c | 971 +++++++++++++++++++++++++++------------------- 2 files changed, 569 insertions(+), 403 deletions(-)
Signature · 1 line
2.1.0
PATCH v5 1/4 workqueue: Reorder sysfs code
From: Frederic Weisbecker <[email protected]>
The sysfs code usually belongs to the botom of the file since it deals with high level objects. In the workqueue code it's misplaced and such that we'll need to work around functions references to allow the sysfs code to call APIs like apply_workqueue_attrs().
Lets move that block further in the file, right above alloc_workqueue_key() which reference it.
--- kernel/workqueue.c | 634 ++++++++++++++++++++++++++--------------------------- 1 file changed, 317 insertions(+), 317 deletions(-)
kernel/workqueue.c +317 -317
@@ -3001,323 +3001,6 @@ int execute_in_process_context(work_func_t fn, struct execute_work *ew) }EXPORT_SYMBOL_GPL (execute_in_process_context); -#ifdef CONFIG_SYSFS -/* -* Workqueues with WQ_SYSFS flag set is visible to userland via -* /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the -* following attributes. -* -* per_cpu RO bool : whether the workqueue is per-cpu or unbound -* max_active RW int : maximum number of in-flight work items -* -* Unbound workqueues have the following extra attributes. -* -* id RO int : the associated pool ID -* nice RW int : nice value of the workers -* cpumask RW mask : bitmask of allowed CPUs for the workers -*/ -struct wq_device { -struct workqueue_struct *wq; -struct device dev; -}; - -static struct workqueue_struct *dev_to_wq (struct device *dev) -{ - struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); - -return wq_dev->wq; -} - -static ssize_t per_cpu_show (struct device *dev,struct device_attribute *attr, -char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); - -return scnprintf (buf,PAGE_SIZE ,"%d\n" , (bool )!(wq->flags &WQ_UNBOUND )); -} -static DEVICE_ATTR_RO (per_cpu); - -static ssize_t max_active_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); - -return scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->saved_max_active); -} - -static ssize_t max_active_store (struct device *dev, -struct device_attribute *attr,const char *buf, -size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int val; - -if (sscanf (buf,"%d" , &val) !=1 || val <=0 ) -return -EINVAL ; - -workqueue_set_max_active (wq, val); -return count; -} -static DEVICE_ATTR_RW (max_active); - -static struct attribute *wq_sysfs_attrs[] = { - &dev_attr_per_cpu.attr, - &dev_attr_max_active.attr, -NULL , -}; -ATTRIBUTE_GROUPS (wq_sysfs); - -static ssize_t wq_pool_ids_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -const char *delim ="" ; -int node, written =0 ; - -rcu_read_lock_sched (); -for_each_node (node) { - written +=scnprintf (buf + written,PAGE_SIZE - written, -"%s%d:%d" , delim, node, -unbound_pwq_by_node (wq, node)->pool->id); - delim =" " ; - } - written +=scnprintf (buf + written,PAGE_SIZE - written,"\n" ); -rcu_read_unlock_sched (); - -return written; -} - -static ssize_t wq_nice_show (struct device *dev,struct device_attribute *attr, -char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int written; - -mutex_lock (&wq->mutex); - written =scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->unbound_attrs->nice); -mutex_unlock (&wq->mutex); - -return written; -} - -/* prepare workqueue_attrs for sysfs store operations */ -static struct workqueue_attrs *wq_sysfs_prep_attrs (struct workqueue_struct *wq) -{ -struct workqueue_attrs *attrs; - - 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; -} - -static ssize_t wq_nice_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -struct workqueue_attrs *attrs; -int ret; - - attrs =wq_sysfs_prep_attrs (wq); -if (!attrs) -return -ENOMEM ; - -if (sscanf (buf,"%d" , &attrs->nice) ==1 && - attrs->nice >=MIN_NICE && attrs->nice <=MAX_NICE ) - ret =apply_workqueue_attrs (wq, attrs); -else - ret = -EINVAL ; - -free_workqueue_attrs (attrs); -return ret ?: count; -} - -static ssize_t wq_cpumask_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int written; - -mutex_lock (&wq->mutex); - written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , -cpumask_pr_args (wq->unbound_attrs->cpumask)); -mutex_unlock (&wq->mutex); -return written; -} - -static ssize_t wq_cpumask_store (struct device *dev, -struct device_attribute *attr, -const char *buf,size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -struct workqueue_attrs *attrs; -int ret; - - attrs =wq_sysfs_prep_attrs (wq); -if (!attrs) -return -ENOMEM ; - - ret =cpumask_parse (buf, attrs->cpumask); -if (!ret) - ret =apply_workqueue_attrs (wq, attrs); - -free_workqueue_attrs (attrs); -return ret ?: count; -} - -static ssize_t wq_numa_show (struct device *dev,struct device_attribute *attr, -char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int written; - -mutex_lock (&wq->mutex); - written =scnprintf (buf,PAGE_SIZE ,"%d\n" , - !wq->unbound_attrs->no_numa); -mutex_unlock (&wq->mutex); - -return written; -} - -static ssize_t wq_numa_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -struct workqueue_attrs *attrs; -int v, ret; - - attrs =wq_sysfs_prep_attrs (wq); -if (!attrs) -return -ENOMEM ; - - ret = -EINVAL ; -if (sscanf (buf,"%d" , &v) ==1 ) { - attrs->no_numa = !v; - ret =apply_workqueue_attrs (wq, attrs); - } - -free_workqueue_attrs (attrs); -return ret ?: count; -} - -static struct device_attribute wq_sysfs_unbound_attrs[] = { -__ATTR (pool_ids,0444 , wq_pool_ids_show,NULL ), -__ATTR (nice,0644 , wq_nice_show, wq_nice_store), -__ATTR (cpumask,0644 , wq_cpumask_show, wq_cpumask_store), -__ATTR (numa,0644 , wq_numa_show, wq_numa_store), - __ATTR_NULL, -}; - -static struct bus_type wq_subsys = { - .name ="workqueue" , - .dev_groups = wq_sysfs_groups, -}; - -static int __init wq_sysfs_init (void ) -{ -return subsys_virtual_register (&wq_subsys,NULL ); -} -core_initcall (wq_sysfs_init); - -static void wq_device_release (struct device *dev) -{ - struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); - -kfree (wq_dev); -} - -/** -* workqueue_sysfs_register - make a workqueue visible in sysfs -* @wq: the workqueue to register -* -* Expose @wq in sysfs under /sys/bus/workqueue/devices. -* alloc_workqueue*() automatically calls this function if WQ_SYSFS is set -* which is the preferred method. -* -* Workqueue user should use this function directly iff it wants to apply -* workqueue_attrs before making the workqueue visible in sysfs; otherwise, -* apply_workqueue_attrs() may race against userland updating the -* attributes. -* -* Return: 0 on success, -errno on failure. -*/ -int workqueue_sysfs_register (struct workqueue_struct *wq) -{ -struct wq_device *wq_dev; -int ret; - -/* -* Adjusting max_active or creating new pwqs by applyting -* attributes breaks ordering guarantee. Disallow exposing ordered -* workqueues. -*/ -if (WARN_ON (wq->flags & __WQ_ORDERED)) -return -EINVAL ; - - wq->wq_dev = wq_dev =kzalloc (sizeof (*wq_dev),GFP_KERNEL ); -if (!wq_dev) -return -ENOMEM ; - - wq_dev->wq = wq; - wq_dev->dev.bus = &wq_subsys; - wq_dev->dev.init_name = wq->name; - wq_dev->dev.release = wq_device_release; - -/* -* unbound_attrs are created separately. Suppress uevent until -* everything is ready. -*/ -dev_set_uevent_suppress (&wq_dev->dev, true); - - ret =device_register (&wq_dev->dev); -if (ret) { -kfree (wq_dev); - wq->wq_dev =NULL ; -return ret; - } - -if (wq->flags &WQ_UNBOUND ) { -struct device_attribute *attr; - -for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { - ret =device_create_file (&wq_dev->dev, attr); -if (ret) { -device_unregister (&wq_dev->dev); - wq->wq_dev =NULL ; -return ret; - } - } - } - -dev_set_uevent_suppress (&wq_dev->dev, false); -kobject_uevent (&wq_dev->dev.kobj,KOBJ_ADD ); -return 0 ; -} - -/** -* workqueue_sysfs_unregister - undo workqueue_sysfs_register() -* @wq: the workqueue to unregister -* -* If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. -*/ -static void workqueue_sysfs_unregister (struct workqueue_struct *wq) -{ -struct wq_device *wq_dev = wq->wq_dev; - -if (!wq->wq_dev) -return ; - - wq->wq_dev =NULL ; -device_unregister (&wq_dev->dev); -} -#else /* CONFIG_SYSFS */ -static void workqueue_sysfs_unregister (struct workqueue_struct *wq) { } -#endif /* CONFIG_SYSFS */ -/** * free_workqueue_attrs - free a workqueue_attrs * @attrs: workqueue_attrs to free
@@ -4029,6 +3712,323 @@ out_unlock:put_pwq_unlocked (old_pwq); } +#ifdef CONFIG_SYSFS +/* +* Workqueues with WQ_SYSFS flag set is visible to userland via +* /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the +* following attributes. +* +* per_cpu RO bool : whether the workqueue is per-cpu or unbound +* max_active RW int : maximum number of in-flight work items +* +* Unbound workqueues have the following extra attributes. +* +* id RO int : the associated pool ID +* nice RW int : nice value of the workers +* cpumask RW mask : bitmask of allowed CPUs for the workers +*/ +struct wq_device { +struct workqueue_struct *wq; +struct device dev; +}; + +static struct workqueue_struct *dev_to_wq (struct device *dev) +{ + struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); + +return wq_dev->wq; +} + +static ssize_t per_cpu_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , (bool )!(wq->flags &WQ_UNBOUND )); +} +static DEVICE_ATTR_RO (per_cpu); + +static ssize_t max_active_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->saved_max_active); +} + +static ssize_t max_active_store (struct device *dev, +struct device_attribute *attr,const char *buf, +size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int val; + +if (sscanf (buf,"%d" , &val) !=1 || val <=0 ) +return -EINVAL ; + +workqueue_set_max_active (wq, val); +return count; +} +static DEVICE_ATTR_RW (max_active); + +static struct attribute *wq_sysfs_attrs[] = { + &dev_attr_per_cpu.attr, + &dev_attr_max_active.attr, +NULL , +}; +ATTRIBUTE_GROUPS (wq_sysfs); + +static ssize_t wq_pool_ids_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +const char *delim ="" ; +int node, written =0 ; + +rcu_read_lock_sched (); +for_each_node (node) { + written +=scnprintf (buf + written,PAGE_SIZE - written, +"%s%d:%d" , delim, node, +unbound_pwq_by_node (wq, node)->pool->id); + delim =" " ; + } + written +=scnprintf (buf + written,PAGE_SIZE - written,"\n" ); +rcu_read_unlock_sched (); + +return written; +} + +static ssize_t wq_nice_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int written; + +mutex_lock (&wq->mutex); + written =scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->unbound_attrs->nice); +mutex_unlock (&wq->mutex); + +return written; +} + +/* prepare workqueue_attrs for sysfs store operations */ +static struct workqueue_attrs *wq_sysfs_prep_attrs (struct workqueue_struct *wq) +{ +struct workqueue_attrs *attrs; + + 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; +} + +static ssize_t wq_nice_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +struct workqueue_attrs *attrs; +int ret; + + attrs =wq_sysfs_prep_attrs (wq); +if (!attrs) +return -ENOMEM ; + +if (sscanf (buf,"%d" , &attrs->nice) ==1 && + attrs->nice >=MIN_NICE && attrs->nice <=MAX_NICE ) + ret =apply_workqueue_attrs (wq, attrs); +else + ret = -EINVAL ; + +free_workqueue_attrs (attrs); +return ret ?: count; +} + +static ssize_t wq_cpumask_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int written; + +mutex_lock (&wq->mutex); + written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , +cpumask_pr_args (wq->unbound_attrs->cpumask)); +mutex_unlock (&wq->mutex); +return written; +} + +static ssize_t wq_cpumask_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +struct workqueue_attrs *attrs; +int ret; + + attrs =wq_sysfs_prep_attrs (wq); +if (!attrs) +return -ENOMEM ; + + ret =cpumask_parse (buf, attrs->cpumask); +if (!ret) + ret =apply_workqueue_attrs (wq, attrs); + +free_workqueue_attrs (attrs); +return ret ?: count; +} + +static ssize_t wq_numa_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int written; + +mutex_lock (&wq->mutex); + written =scnprintf (buf,PAGE_SIZE ,"%d\n" , + !wq->unbound_attrs->no_numa); +mutex_unlock (&wq->mutex); + +return written; +} + +static ssize_t wq_numa_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +struct workqueue_attrs *attrs; +int v, ret; + + attrs =wq_sysfs_prep_attrs (wq); +if (!attrs) +return -ENOMEM ; + + ret = -EINVAL ; +if (sscanf (buf,"%d" , &v) ==1 ) { + attrs->no_numa = !v; + ret =apply_workqueue_attrs (wq, attrs); + } + +free_workqueue_attrs (attrs); +return ret ?: count; +} + +static struct device_attribute wq_sysfs_unbound_attrs[] = { +__ATTR (pool_ids,0444 , wq_pool_ids_show,NULL ), +__ATTR (nice,0644 , wq_nice_show, wq_nice_store), +__ATTR (cpumask,0644 , wq_cpumask_show, wq_cpumask_store), +__ATTR (numa,0644 , wq_numa_show, wq_numa_store), + __ATTR_NULL, +}; + +static struct bus_type wq_subsys = { + .name ="workqueue" , + .dev_groups = wq_sysfs_groups, +}; + +static int __init wq_sysfs_init (void ) +{ +return subsys_virtual_register (&wq_subsys,NULL ); +} +core_initcall (wq_sysfs_init); + +static void wq_device_release (struct device *dev) +{ + struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); + +kfree (wq_dev); +} + +/** +* workqueue_sysfs_register - make a workqueue visible in sysfs +* @wq: the workqueue to register +* +* Expose @wq in sysfs under /sys/bus/workqueue/devices. +* alloc_workqueue*() automatically calls this function if WQ_SYSFS is set +* which is the preferred method. +* +* Workqueue user should use this function directly iff it wants to apply +* workqueue_attrs before making the workqueue visible in sysfs; otherwise, +* apply_workqueue_attrs() may race against userland updating the +* attributes. +* +* Return: 0 on success, -errno on failure. +*/ +int workqueue_sysfs_register (struct workqueue_struct *wq) +{ +struct wq_device *wq_dev; +int ret; + +/* +* Adjusting max_active or creating new pwqs by applyting +* attributes breaks ordering guarantee. Disallow exposing ordered +* workqueues. +*/ +if (WARN_ON (wq->flags & __WQ_ORDERED)) +return -EINVAL ; + + wq->wq_dev = wq_dev =kzalloc (sizeof (*wq_dev),GFP_KERNEL ); +if (!wq_dev) +return -ENOMEM ; + + wq_dev->wq = wq; + wq_dev->dev.bus = &wq_subsys; + wq_dev->dev.init_name = wq->name; + wq_dev->dev.release = wq_device_release; + +/* +* unbound_attrs are created separately. Suppress uevent until +* everything is ready. +*/ +dev_set_uevent_suppress (&wq_dev->dev, true); + + ret =device_register (&wq_dev->dev); +if (ret) { +kfree (wq_dev); + wq->wq_dev =NULL ; +return ret; + } + +if (wq->flags &WQ_UNBOUND ) { +struct device_attribute *attr; + +for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { + ret =device_create_file (&wq_dev->dev, attr); +if (ret) { +device_unregister (&wq_dev->dev); + wq->wq_dev =NULL ; +return ret; + } + } + } + +dev_set_uevent_suppress (&wq_dev->dev, false); +kobject_uevent (&wq_dev->dev.kobj,KOBJ_ADD ); +return 0 ; +} + +/** +* workqueue_sysfs_unregister - undo workqueue_sysfs_register() +* @wq: the workqueue to unregister +* +* If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. +*/ +static void workqueue_sysfs_unregister (struct workqueue_struct *wq) +{ +struct wq_device *wq_dev = wq->wq_dev; + +if (!wq->wq_dev) +return ; + + wq->wq_dev =NULL ; +device_unregister (&wq_dev->dev); +} +#else /* CONFIG_SYSFS */ +static void workqueue_sysfs_unregister (struct workqueue_struct *wq) { } +#endif /* CONFIG_SYSFS */ +static int alloc_and_link_pwqs (struct workqueue_struct *wq) {bool highpri = wq->flags &WQ_HIGHPRI ;
Signature · 1 line
2.1.0
Tejun Heo
maintainerRe: [PATCH 1/4 V5] workqueue: Reorder sysfs code
Hello,
Quoted 5 lines · Lai Jiangshan
kernel/workqueue.c +1
-0@@ -4029,6 +3712,323 @@ out_unlock:put_pwq_unlocked (old_pwq); } +#ifdef CONFIG_SYSFS
...
kernel/workqueue.c out_unlock: · line 4034
Quoted 7 lines
kernel/workqueue.c +4
-0+#else /* CONFIG_SYSFS */ +static void workqueue_sysfs_unregister (struct workqueue_struct *wq) { } +#endif /* CONFIG_SYSFS */ +static int alloc_and_link_pwqs (struct workqueue_struct *wq) {bool highpri = wq->flags &WQ_HIGHPRI ;
Why here? It's still in the middle of core logic implementation. Why not move it further down so that it's either before or after the hotplug code?
Thanks.
Signature · 1 line
tejun
PATCH v5 2/4 workqueue: split apply_workqueue_attrs() into 3 stages
In reply to Lai JiangshanCurrent apply_workqueue_attrs() includes pwqs-allocation and pwqs-installation, so when we batch multiple apply_workqueue_attrs()s as a transaction, we can't ensure the transaction must succeed or fail as a complete unit.
To solve this, we split apply_workqueue_attrs() into three stages. The first stage does the preparation: allocation memory, pwqs. The second stage does the attrs-installaion and pwqs-installation. The third stage frees the allocated memory and (old or unused) pwqs.
As the result, batching multiple apply_workqueue_attrs()s can succeed or fail as a complete unit:
1) batch do all the first stage for all the workqueues
2) only commit all when all the above succeed.
This patch is a preparation for the next patch ("Allow modifying low level unbound workqueue cpumask") which will do a multiple apply_workqueue_attrs().
The patch doesn't have functionality changed except two minor adjustment:
1) free_unbound_pwq() for the error path is removed, we use the heavier version put_pwq_unlocked() instead since the error path is rare. this adjustment simplifies the code.
2) the memory-allocation is also moved into wq_pool_mutex. this is needed to avoid to do the further splitting.
--- kernel/workqueue.c | 203 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 119 insertions(+), 84 deletions(-)
kernel/workqueue.c +119 -84
@@ -3424,17 +3424,6 @@ static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,return pwq; } -/* undo alloc_unbound_pwq(), used only in the error path */ -static void free_unbound_pwq (struct pool_workqueue *pwq) -{ -lockdep_assert_held (&wq_pool_mutex); - -if (pwq) { -put_unbound_pool (pwq->pool); -kmem_cache_free (pwq_cache, pwq); - } -} -/** * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node * @attrs: the wq_attrs of interest
@@ -3497,42 +3486,49 @@ static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,return old_pwq; } -/** -* 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) +/* Context to store the prepared attrs & pwqs before installed */ +struct wq_unbound_install_ctx { +struct workqueue_struct *wq;/* target to be installed */ +struct workqueue_attrs *attrs;/* attrs for installing */ +struct pool_workqueue *dfl_pwq; +struct pool_workqueue *pwq_tbl[]; +}; + +/* Free the resources after success or abort */ +static void wq_unbound_install_ctx_free (struct wq_unbound_install_ctx *ctx) { +int node; + +if (ctx) { +/* put the pwqs */ +for_each_node (node) +put_pwq_unlocked (ctx->pwq_tbl[node]); +put_pwq_unlocked (ctx->dfl_pwq); + +free_workqueue_attrs (ctx->attrs); + } + +kfree (ctx); +} + +/* Allocates the attrs and pwqs for later installment */ +static struct wq_unbound_install_ctx * +wq_unbound_install_ctx_prepare (struct workqueue_struct *wq, +const struct workqueue_attrs *attrs) +{ +struct wq_unbound_install_ctx *ctx;struct workqueue_attrs *new_attrs, *tmp_attrs; -struct pool_workqueue **pwq_tbl, *dfl_pwq; -int node, ret; +int node; -/* only unbound workqueues can change attributes */ -if (WARN_ON (!(wq->flags &WQ_UNBOUND ))) -return -EINVAL ; +lockdep_assert_held (&wq_pool_mutex); -/* creating multiple pwqs breaks ordering guarantee */ -if (WARN_ON ((wq->flags & __WQ_ORDERED) && !list_empty (&wq->pwqs))) -return -EINVAL ; + ctx =kzalloc (sizeof (*ctx) + nr_node_ids *sizeof (ctx->pwq_tbl[0 ]), +GFP_KERNEL ); - pwq_tbl =kzalloc (nr_node_ids *sizeof (pwq_tbl[0 ]),GFP_KERNEL ); new_attrs =alloc_workqueue_attrs (GFP_KERNEL ); tmp_attrs =alloc_workqueue_attrs (GFP_KERNEL ); -if (!pwq_tbl || !new_attrs || !tmp_attrs) - goto enomem; +if (!ctx || !new_attrs || !tmp_attrs) + goto out_free;/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs);
@@ -3546,75 +3542,114 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,copy_workqueue_attrs (tmp_attrs, new_attrs);/* -* 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); - -/* * If something goes wrong during CPU up/down, we'll fall back to * the default pwq covering whole @attrs->cpumask. Always create * it even if we don't use it immediately. */ - dfl_pwq =alloc_unbound_pwq (wq, new_attrs); -if (!dfl_pwq) - goto enomem_pwq; + ctx->dfl_pwq =alloc_unbound_pwq (wq, new_attrs); +if (!ctx->dfl_pwq) + goto out_free;for_each_node (node) {if (wq_calc_node_cpumask (attrs, node,-1 , tmp_attrs->cpumask)) { - pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs); -if (!pwq_tbl[node]) - goto enomem_pwq; + ctx->pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs); +if (!ctx->pwq_tbl[node]) + goto out_free; }else { - dfl_pwq->refcnt++; - pwq_tbl[node] = dfl_pwq; + ctx->dfl_pwq->refcnt++; + ctx->pwq_tbl[node] = ctx->dfl_pwq; } } -mutex_unlock (&wq_pool_mutex); + ctx->wq = wq; + ctx->attrs = new_attrs; + +out_free: +free_workqueue_attrs (tmp_attrs); + +if (!ctx || !ctx->wq) { +kfree (new_attrs); +wq_unbound_install_ctx_free (ctx); +return NULL ; + }else { +return ctx; + } +} + +/* Set the unbound_attr and install the prepared pwqs. Should not fail */ +static void wq_unbound_install_ctx_commit (struct wq_unbound_install_ctx *ctx) +{ +int node;/* all pwqs have been created successfully, let's install'em */ -mutex_lock (&wq->mutex); +mutex_lock (&ctx->wq->mutex); -copy_workqueue_attrs (wq->unbound_attrs, new_attrs); +copy_workqueue_attrs (ctx->wq->unbound_attrs, ctx->attrs);/* save the previous pwq and install the new one */ for_each_node (node) - pwq_tbl[node] =numa_pwq_tbl_install (wq, node, pwq_tbl[node]); + ctx->pwq_tbl[node] =numa_pwq_tbl_install (ctx->wq, node, + ctx->pwq_tbl[node]);/* @dfl_pwq might not have been used, ensure it's linked */ -link_pwq (dfl_pwq); -swap (wq->dfl_pwq, dfl_pwq); +link_pwq (ctx->dfl_pwq); +swap (ctx->wq->dfl_pwq, ctx->dfl_pwq); -mutex_unlock (&wq->mutex); +mutex_unlock (&ctx->wq->mutex); +} -/* put the old pwqs */ -for_each_node (node) -put_pwq_unlocked (pwq_tbl[node]); -put_pwq_unlocked (dfl_pwq); +/** +* 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) +{ +struct wq_unbound_install_ctx *ctx; +int ret = -ENOMEM ; -put_online_cpus (); - ret =0 ; -/* fall through */ -out_free: -free_workqueue_attrs (tmp_attrs); -free_workqueue_attrs (new_attrs); -kfree (pwq_tbl); -return ret; +/* only unbound workqueues can change attributes */ +if (WARN_ON (!(wq->flags &WQ_UNBOUND ))) +return -EINVAL ; -enomem_pwq: -free_unbound_pwq (dfl_pwq); -for_each_node (node) -if (pwq_tbl && pwq_tbl[node] != dfl_pwq) -free_unbound_pwq (pwq_tbl[node]); +/* creating multiple pwqs breaks ordering guarantee */ +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 =wq_unbound_install_ctx_prepare (wq, attrs);mutex_unlock (&wq_pool_mutex); +put_online_cpus (); -enomem: - ret = -ENOMEM ; - goto out_free; + +/* the ctx has been prepared successfully, let's commit it */ +if (ctx) { +wq_unbound_install_ctx_commit (ctx); + ret =0 ; + } + +wq_unbound_install_ctx_free (ctx); + +return ret; }/**
Signature · 1 line
2.1.0
Tejun Heo
maintainerRe: [PATCH 2/4 V5] workqueue: split apply_workqueue_attrs() into 3 stages
Quoted 1 line · Lai Jiangshan
+static void
wq_unbound_install_ctx_free(struct wq_unbound_install_ctx *ctx)
Maybe naminig it more consistently with apply_workqueue_attrs() is better? apply_wqattrs_cleanup()?
Quoted 4 lines
{
kernel/workqueue.c +3
-0+int node; + +if (ctx) {
kernel/workqueue.c static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, · line 3512
Quoted 10 lines
kernel/workqueue.c +10
-0+/* put the pwqs */ +for_each_node (node) +put_pwq_unlocked (ctx->pwq_tbl[node]); +put_pwq_unlocked (ctx->dfl_pwq); + +free_workqueue_attrs (ctx->attrs); + } + +kfree (ctx); +}
Wouldn't the following be better? Or at least put kfree(ctx) together with the rest?
if (!ctx) return; the rest;
kernel/workqueue.c static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, · line 3518
Quoted 6 lines
kernel/workqueue.c +6
-0+ +/* Allocates the attrs and pwqs for later installment */ +static struct wq_unbound_install_ctx * +wq_unbound_install_ctx_prepare (struct workqueue_struct *wq, +const struct workqueue_attrs *attrs) +{
apply_wqattrs_prepare()?
...
kernel/workqueue.c int apply_workqueue_attrs(struct workqueue_struct *wq, · line 3577
Quoted 11 lines
kernel/workqueue.c +11
-0+out_free: +free_workqueue_attrs (tmp_attrs); + +if (!ctx || !ctx->wq) { +kfree (new_attrs); +wq_unbound_install_ctx_free (ctx); +return NULL ; + }else { +return ctx; + } +}
Let's separate out error return path even if that takes another goto or a duplicate free_workqueue_attrs() call.
kernel/workqueue.c int apply_workqueue_attrs(struct workqueue_struct *wq, · line 3580
Quoted 2 lines
kernel/workqueue.c +2
-0+/* Set the unbound_attr and install the prepared pwqs. Should not fail */ +static void wq_unbound_install_ctx_commit (struct wq_unbound_install_ctx *ctx)
apply_wqattrs_commit()?
Thanks.
Signature · 1 line
tejun
PATCH v5 3/4 workqueue: Create low-level unbound workqueues cpumask
In reply to Lai JiangshanFrom: Frederic Weisbecker <[email protected]>
Create a cpumask that limit the affinity of all unbound workqueues. This cpumask is controlled though a file at the root of the workqueue sysfs directory.
It works on a lower-level than the per WQ_SYSFS workqueues cpumask files such that the effective cpumask applied for a given unbound workqueue is the intersection of /sys/devices/virtual/workqueue/$WORKQUEUE/cpumask and the new /sys/devices/virtual/workqueue/cpumask_unbounds file.
This patch implements the basic infrastructure and the read interface. cpumask_unbounds is initially set to cpu_possible_mask.
--- kernel/workqueue.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
kernel/workqueue.c +27 -2
@@ -299,6 +299,8 @@ static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */static LIST_HEAD (workqueues);/* PR: list of all workqueues */ static bool workqueue_freezing;/* PL: have wqs started freezing? */ +static cpumask_var_t wq_unbound_cpumask; +/* the per-cpu worker pools */ static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools);
@@ -3532,7 +3534,7 @@ wq_unbound_install_ctx_prepare(struct workqueue_struct *wq,/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask);/* * We may create multiple pwqs with differing cpumasks. Make a
@@ -3963,9 +3965,29 @@ static struct bus_type wq_subsys = { .dev_groups = wq_sysfs_groups, }; +static ssize_t unbounds_cpumask_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +int written; + + written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , +cpumask_pr_args (wq_unbound_cpumask)); + +return written; +} + +static struct device_attribute wq_sysfs_cpumask_attr = +__ATTR (cpumask,0444 , unbounds_cpumask_show,NULL ); +static int __init wq_sysfs_init (void ) { -return subsys_virtual_register (&wq_subsys,NULL ); +int err; + + err =subsys_virtual_register (&wq_subsys,NULL ); +if (err) +return err; + +return device_create_file (wq_subsys.dev_root, &wq_sysfs_cpumask_attr); }core_initcall (wq_sysfs_init);
@@ -5098,6 +5120,9 @@ static int __init init_workqueues(void)WARN_ON (__alignof__(struct pool_workqueue ) < __alignof__(long long )); +BUG_ON (!alloc_cpumask_var (&wq_unbound_cpumask,GFP_KERNEL )); +cpumask_copy (wq_unbound_cpumask, cpu_possible_mask); + pwq_cache =KMEM_CACHE (pool_workqueue,SLAB_PANIC );cpu_notifier (workqueue_cpu_up_callback,CPU_PRI_WORKQUEUE_UP );
Signature · 1 line
2.1.0
PATCH v5 4/4 workqueue: Allow modifying low level unbound workqueue cpumask
In reply to Lai JiangshanAllow to modify the low-level unbound workqueues cpumask through sysfs. This is performed by traversing the entire workqueue list and calling wq_unbound_install_ctx_prepare() on the unbound workqueues with the low level mask passed in. Only after all the preparation are done, we commit them all together.
The oreder-workquue is ignore from the low level unbound workqueue cpumask, it will be handled in near future.
The per-nodes' pwqs are mandatorily controlled by the low level cpumask, while the default pwq ignores the low level cpumask when (and ONLY when) the cpumask set by the user doesn't overlap with the low level cpumask. In this case, we can't apply the empty cpumask to the default pwq, so we use the user-set cpumask directly.
The default wq_unbound_cpumask is still cpu_possible_mask due to the workqueue subsystem doesn't know what is the best default value for the runtime, the system manager or other subsystem which knows the sufficient information should set it when needed.
--- include/linux/workqueue.h | 1 + kernel/workqueue.c | 121 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 114 insertions(+), 8 deletions(-)
include/linux/workqueue.h +1 -0
@@ -424,6 +424,7 @@ struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask);void free_workqueue_attrs (struct workqueue_attrs *attrs);int apply_workqueue_attrs (struct workqueue_struct *wq,const struct workqueue_attrs *attrs); +int workqueue_unbounds_cpumask_set (cpumask_var_t cpumask);extern bool queue_work_on (int cpu,struct workqueue_struct *wq,struct work_struct *work);
kernel/workqueue.c +113 -8
206 lines not on this page · raw message
Signature · 1 line
2.1.0
Tejun Heo
maintainerRe: [PATCH 4/4 V5] workqueue: Allow modifying low level unbound workqueue cpumask
Quoted 2 lines · Lai Jiangshan
The oreder-workquue is ignore from the low level unbound workqueue cpumask, it will be handled in near future.
Ugh, right, ordered workqueues are tricky. Maybe we should change how ordered workqueues are implemented. Just gate work items at the workqueue layer instead of fiddling with max_active and the number of pwqs.
Quoted 6 lines
static struct
wq_unbound_install_ctx* wq_unbound_install_ctx_prepare(structworkqueue_struct*wq,kernel/workqueue.c +2
-1- const struct workqueue_attrs *attrs) +const struct workqueue_attrs *attrs, + cpumask_var_t unbound_cpumask) {
...
kernel/workqueue.c wq_unbound_install_ctx_prepare(struct workqueue_struct *wq, · line 3542
Quoted 6 lines
kernel/workqueue.c +3
-1/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); +copy_workqueue_attrs (pwq_attrs, attrs); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (pwq_attrs->cpumask, pwq_attrs->cpumask, unbound_cpumask);
Hmmm... we weren't checking whether the intersection becomes null before. Why are we doing it now? Note that this doesn't really make things water-tight as cpu on/offlining can still leave the mask w/o any online cpus. Shouldn't we just let the scheduler handle it as before?
kernel/workqueue.c static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, · line 3731
Quoted 7 lines
kernel/workqueue.c +3
-0@@ -3712,6 +3726,9 @@ static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,* wq's, the default pwq should be used. */ if (wq_calc_node_cpumask (wq->unbound_attrs, node, cpu_off, cpumask)) { +cpumask_and (cpumask, cpumask, wq_unbound_cpumask); +if (cpumask_empty (cpumask)) + goto use_dfl_pwq;
So, this special handling is necessary only because we did special in the above for dfl_pwq. Why do we need these?
Quoted 2 lines
(quoted diff) +2
-0+static int unbounds_cpumask_apply(cpumask_var_t cpumask) +{
..
Review
Quoted 2 lines
(quoted diff) +2
-0+ list_for_each_entry_safe(ctx, n, &ctxs, list) { + if (ret >= 0)
Let's do !ret.
Quoted 3 lines
kernel/workqueue.c +3
-0+wq_unbound_install_ctx_commit (ctx); +wq_unbound_install_ctx_free (ctx); + }
...
Quoted 11 lines
kernel/workqueue.c +11
-0+/** +* workqueue_unbounds_cpumask_set - Set the low-level unbound cpumask +* @cpumask: the cpumask to set +* +* The low-level workqueues cpumask is a global cpumask that limits +* the affinity of all unbound workqueues. This function check the @cpumask +* and apply it to all unbound workqueues and updates all pwqs of them. +* When all succeed, it saves @cpumask to the global low-level unbound +* cpumask. +* +* Retun: 0 - Success
kernel/workqueue.c out_unlock: · line 3824
Quoted 10 lines
kernel/workqueue.c +10
-0+* -EINVAL - No online cpu in the @cpumask +* -ENOMEM - Failed to allocate memory for attrs or pwqs. +*/ +int workqueue_unbounds_cpumask_set (cpumask_var_t cpumask) +{ +int ret = -EINVAL ; + +get_online_cpus (); +cpumask_and (cpumask, cpumask, cpu_possible_mask); +if (cpumask_intersects (cpumask, cpu_online_mask)) {
Does this make sense? We can't prevent cpus going down right after the mask is set. What's the point of preventing empty config if we can't prevent transitions into it and have to handle it anyway?
kernel/workqueue.c static struct bus_type wq_subsys = { · line 4055
Quoted 3 lines
kernel/workqueue.c +3
-0+static ssize_t unbounds_cpumask_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count)
Naming is too confusing. Please pick a name which clearly distinguishes per-wq and global masking.
Thanks.
Signature · 1 line
tejun
Re: [PATCH 4/4 V5] workqueue: Allow modifying low level unbound workqueue cpumask
Quoted 16 lines · Tejun Heo
Quoted 2 lines · Lai Jiangshan
The oreder-workquue is ignore from the low level unbound workqueue cpumask, it will be handled in near future.
Ugh, right, ordered workqueues are tricky. Maybe we should change how ordered workqueues are implemented. Just gate work items at the workqueue layer instead of fiddling with
max_activeand the number of pwqs.Quoted 6 lines
static struct
wq_unbound_install_ctx* wq_unbound_install_ctx_prepare(structworkqueue_struct*wq,kernel/workqueue.c +2
-1- const struct workqueue_attrs *attrs) +const struct workqueue_attrs *attrs, + cpumask_var_t unbound_cpumask) {...
Quoted 9 lines
kernel/workqueue.c wq_unbound_install_ctx_prepare(struct workqueue_struct *wq, · line 3542
Quoted 6 lines
kernel/workqueue.c +3
-1/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); +copy_workqueue_attrs (pwq_attrs, attrs); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (pwq_attrs->cpumask, pwq_attrs->cpumask, unbound_cpumask);Hmmm... we weren't checking whether the intersection becomes null before.
Di you refer to the unquoted following code "cpumask_empty(pwq_attrs->cpumask)"?
It is explained in the changelog and the comments.
Quoted 4 lines
Why are we doing it now? Note that this doesn't really make
things water-tight as cpu on/offlining can still leave the mask w/o any online cpus. Shouldn't we just let the scheduler handle it as before?
Did you refer to "cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);"?
new_attrs will be copied to wq->unbound_attrs, so we hope it is sanity. the same code before this patchset did the same work.
And it maybe be used for default pwq, and it can reduce the pool creation:
cpu_possible_mask = 0-7 wq_unbound_cpumask = 0-3 user1 try to set wq1: attrs->cpumask = 4-9 user2 try to set wq2: attrs->cpumask = 4-11
thus both wq1 and wq2's default pwq's pool is the same pool. (pool's cpumask = 4-7)
Quoted 11 lines
kernel/workqueue.c static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, · line 3731
Quoted 8 lines
kernel/workqueue.c +3
-0@@ -3712,6 +3726,9 @@ static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,* wq's, the default pwq should be used. */ if (wq_calc_node_cpumask (wq->unbound_attrs, node, cpu_off, cpumask)) { +cpumask_and (cpumask, cpumask, wq_unbound_cpumask); +if (cpumask_empty (cpumask)) + goto use_dfl_pwq;So, this special handling is necessary only because we did special in the above for
dfl_pwq. Why do we need these?
wq->unbound_attrs is user setting attrs, its cpumask is not controlled by wq_unbound_cpumask. so we need these cpumask_and().
Another question:
Why wq->unbound_attrs' cpumask is not controlled by wq_unbound_cpumask?
I hope the wq->unbound_attrs is always as the same as the user's last setting, regardless how much times the wq_unbound_cpumask is changed.
Quoted 30 lines
Quoted 3 lines
(quoted diff) +2
-0+static int unbounds_cpumask_apply(cpumask_var_t cpumask) +{..
Review
Quoted 2 lines
(quoted diff) +2
-0+ list_for_each_entry_safe(ctx, n, &ctxs, list) { + if (ret >= 0)Let's do !ret.
Quoted 3 lines
kernel/workqueue.c +3
-0+wq_unbound_install_ctx_commit (ctx); +wq_unbound_install_ctx_free (ctx); + }...
Quoted 17 lines
kernel/workqueue.c +17
-0+/** +* workqueue_unbounds_cpumask_set - Set the low-level unbound cpumask +* @cpumask: the cpumask to set +* +* The low-level workqueues cpumask is a global cpumask that limits +* the affinity of all unbound workqueues. This function check the @cpumask +* and apply it to all unbound workqueues and updates all pwqs of them. +* When all succeed, it saves @cpumask to the global low-level unbound +* cpumask. +* +* Retun: 0 - Success +* -EINVAL - No online cpu in the @cpumask +* -ENOMEM - Failed to allocate memory for attrs or pwqs. +*/ +int workqueue_unbounds_cpumask_set (cpumask_var_t cpumask) +{ +int ret = -EINVAL ;
Quoted 8 lines
kernel/workqueue.c out_unlock: · line 3824
Quoted 4 lines
kernel/workqueue.c +4
-0+ +get_online_cpus (); +cpumask_and (cpumask, cpumask, cpu_possible_mask); +if (cpumask_intersects (cpumask, cpu_online_mask)) {Does this make sense? We can't prevent cpus going down right after the mask is set. What's the point of preventing empty config if we can't prevent transitions into it and have to handle it anyway?
Like set_cpus_allowed_ptr(). The cpumask must be valid when setting, although it can be transited into non-intersection later.
This code is originated from Frederic. Maybe he has some stronger reason.
Quoted 7 lines
kernel/workqueue.c static struct bus_type wq_subsys = { · line 4055
Quoted 4 lines
kernel/workqueue.c +3
-0+static ssize_t unbounds_cpumask_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count)Naming is too confusing. Please pick a name which clearly distinguishes per-wq and global masking.
What about these names?wq_unbound_cpumask ==> wq_unbound_global_cpumask workqueue_unbounds_cpumask_set() ==> workqueue_set_unbound_global_cpumask(). (public API)unbounds_cpumask_store() ==> wq_store_unbound_global_cpumask() (static function for sysfs)
Quoted 3 lines
Thanks.
Re: [PATCH 4/4 V5] workqueue: Allow modifying low level unbound workqueue cpumask
Hi, Frederic, TJ
I considered a special case and forgot to consider an another case.
====
Let @L = the low level unbound workqueue cpumask.
Let @U = the user setting cpumask (wq->unbound_attrs->cpumask).
Thus the pwqs in the specified wq are controlled by @L & @U (& = cpmask_and()). But the per-node pwqs are mandatory controlled by @L & @U, we don't discus it. So this mail only takes focus on the default pwq.
What happens for the default pwq when @L & @U == empty cpumask?
I considered this case in the patch. In my patch, the dfl_pwq directly use the @U. The reasons:
1) it is not a good idea to directly use cpu_possible_mask.
2) and it is not a good idea either to use @L & @U ( = empty), in this case, the scheduler will use cpu_possible_mask.
3) so we has to chose one from @L and @U.
4) I chose @U instead of @L.
@L: it is low level global cpumask and it controls *ALL* wqs. @U: it is set by the *USER*, it controls only one *SPECIFIC* wq.
Frederic, TJ, both you didn't say anything about my this early quick decision. Should this case be handled specially? And if yes, does this decision met your requirements?
======
A comment from TJ reminded me that the final cpumask determined by the scheduler is more important.
Let @O = cpu_online_mask.
The missing case:
(@L & @U) is not empty but (@L & @U @O) is empty.
In my old code (V5 patchset), the dfl_pwq uses (@L & @U), the scheduler will use cpu_possible_mask instead due to there is no cpu onlined among all cpu in (@L & @U). It is bad, the pwq is NOT controlled by @L nor @U now.
I think we may use @U for the dfl_pwq in this case. But it will introduces a problem:
When (@L & @U) has online cpu, the dfl_pwq's cpumaks is (@L & @U). when (@L & @U) has no online cpu, the dfl_pwq's cpumask is @U. It means dfl_pwq may need to be reallocated during the cpuhotplug-add/remove and it means wq_update_unbound_numa() can fail.
Frederic, TJ, any comments about this case?
TJ, would you like to make wq_update_unbound_numa() be failure-able?
thanks
Lai
Quoted 138 lines · Lai Jiangshan
Quoted 16 lines · Tejun Heo
Quoted 2 lines · Lai Jiangshan
The oreder-workquue is ignore from the low level unbound workqueue cpumask, it will be handled in near future.
Ugh, right, ordered workqueues are tricky. Maybe we should change how ordered workqueues are implemented. Just gate work items at the workqueue layer instead of fiddling with
max_activeand the number of pwqs.Quoted 6 lines
static struct
wq_unbound_install_ctx* wq_unbound_install_ctx_prepare(structworkqueue_struct*wq,kernel/workqueue.c +2
-1- const struct workqueue_attrs *attrs) +const struct workqueue_attrs *attrs, + cpumask_var_t unbound_cpumask) {...
Quoted 9 lines
kernel/workqueue.c wq_unbound_install_ctx_prepare(struct workqueue_struct *wq, · line 3542
Quoted 6 lines
kernel/workqueue.c +3
-1/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); +copy_workqueue_attrs (pwq_attrs, attrs); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (pwq_attrs->cpumask, pwq_attrs->cpumask, unbound_cpumask);Hmmm... we weren't checking whether the intersection becomes null before.
Di you refer to the unquoted following code "
cpumask_empty(pwq_attrs->cpumask)"?It is explained in the changelog and the comments.
Quoted 4 lines
Why are we doing it now? Note that this doesn't really make
things water-tight as cpu on/offlining can still leave the mask w/o any online cpus. Shouldn't we just let the scheduler handle it as before?Did you refer to "
cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);"?
new_attrswill be copied towq->unbound_attrs, so we hope it is sanity. the same code before this patchset did the same work.And it maybe be used for default pwq, and it can reduce the pool creation:
cpu_possible_mask = 0-7 wq_unbound_cpumask = 0-3 user1 try to set wq1: attrs->cpumask = 4-9 user2 try to set wq2: attrs->cpumask = 4-11thus both wq1 and wq2's default pwq's pool is the same pool. (pool's cpumask = 4-7)
Quoted 11 lines
kernel/workqueue.c static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, · line 3731
Quoted 8 lines
kernel/workqueue.c +3
-0@@ -3712,6 +3726,9 @@ static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,* wq's, the default pwq should be used. */ if (wq_calc_node_cpumask (wq->unbound_attrs, node, cpu_off, cpumask)) { +cpumask_and (cpumask, cpumask, wq_unbound_cpumask); +if (cpumask_empty (cpumask)) + goto use_dfl_pwq;So, this special handling is necessary only because we did special in the above for
dfl_pwq. Why do we need these?
wq->unbound_attrsis user setting attrs, its cpumask is not controlled bywq_unbound_cpumask. so we need thesecpumask_and().Another question:
Whywq->unbound_attrs' cpumask is not controlled bywq_unbound_cpumask?I hope the
wq->unbound_attrsis always as the same as the user's last setting, regardless how much times thewq_unbound_cpumaskis changed.Quoted 30 lines
Quoted 3 lines
(quoted diff) +2
-0+static int unbounds_cpumask_apply(cpumask_var_t cpumask) +{..
Review
Quoted 2 lines
(quoted diff) +2
-0+ list_for_each_entry_safe(ctx, n, &ctxs, list) { + if (ret >= 0)Let's do !ret.
Quoted 3 lines
kernel/workqueue.c +3
-0+wq_unbound_install_ctx_commit (ctx); +wq_unbound_install_ctx_free (ctx); + }...
Quoted 17 lines
kernel/workqueue.c +17
-0+/** +* workqueue_unbounds_cpumask_set - Set the low-level unbound cpumask +* @cpumask: the cpumask to set +* +* The low-level workqueues cpumask is a global cpumask that limits +* the affinity of all unbound workqueues. This function check the @cpumask +* and apply it to all unbound workqueues and updates all pwqs of them. +* When all succeed, it saves @cpumask to the global low-level unbound +* cpumask. +* +* Retun: 0 - Success +* -EINVAL - No online cpu in the @cpumask +* -ENOMEM - Failed to allocate memory for attrs or pwqs. +*/ +int workqueue_unbounds_cpumask_set (cpumask_var_t cpumask) +{ +int ret = -EINVAL ;Quoted 8 lines
kernel/workqueue.c out_unlock: · line 3824
Quoted 4 lines
kernel/workqueue.c +4
-0+ +get_online_cpus (); +cpumask_and (cpumask, cpumask, cpu_possible_mask); +if (cpumask_intersects (cpumask, cpu_online_mask)) {Does this make sense? We can't prevent cpus going down right after the mask is set. What's the point of preventing empty config if we can't prevent transitions into it and have to handle it anyway?
Like
set_cpus_allowed_ptr(). The cpumask must be valid when setting, although it can be transited into non-intersection later.This code is originated from Frederic. Maybe he has some stronger reason.
Quoted 7 lines
kernel/workqueue.c static struct bus_type wq_subsys = { · line 4055
Quoted 4 lines
kernel/workqueue.c +3
-0+static ssize_t unbounds_cpumask_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count)Naming is too confusing. Please pick a name which clearly distinguishes per-wq and global masking.
What about these names?
wq_unbound_cpumask==>wq_unbound_global_cpumaskworkqueue_unbounds_cpumask_set()==>workqueue_set_unbound_global_cpumask(). (public API)unbounds_cpumask_store()==>wq_store_unbound_global_cpumask()(static function for sysfs)Quoted 3 lines
Thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
.
Tejun Heo
maintainerRe: [PATCH 4/4 V5] workqueue: Allow modifying low level unbound workqueue cpumask
Quoted 10 lines · Lai Jiangshan
The missing case:
(@L & @U) is not empty but (@L & @U @O) is empty.In my old code (V5 patchset), the
dfl_pwquses (@L & @U), the scheduler will usecpu_possible_maskinstead due to there is no cpu onlined among all cpu in (@L & @U). It is bad, the pwq is NOT controlled by @L nor @U now.I think we may use @U for the
dfl_pwqin this case. But it will introduces a problem:
Quoted 7 lines
When (@L & @U) has online cpu, the dfl_pwq's cpumaks is (@L & @U). when (@L & @U) has no online cpu, the dfl_pwq's cpumask is @U. It means
dfl_pwqmay need to be reallocated during the cpuhotplug-add/remove and it meanswq_update_unbound_numa()can fail.Frederic, TJ, any comments about this case?
TJ, would you like to makewq_update_unbound_numa()be failure-able?
The goal is making @L behave as cpu_possible_mask for workqueues, right? If @L & @U is empty, fallback to @L. If @L & @O is empty, leave it to the scheduler.
Thanks.
Signature · 1 line
tejun
Re: [PATCH 0/4 V5] workqueue: Introduce low-level unbound wq sysfs cpumask v5
In reply to Lai JiangshanQuoted 2 lines · Lai Jiangshan
This patchset mostly copies from Frederic and split the
apply_workqueue_attrs()as TJ's suggest.
I plugged these into rt and gave them a go, had no trouble, and no unbound workqueues perturbing 7 isolated sockets (56 cores). Poking at the mask heftily after dropping the shield and giving the box plenty of generic work to do generated no excitement. Nice boring testdrive.
Quoted 2 lines
This patchset still doesn't include the patch "workqueue: Allow changing attributions of ordered workqueues".
After a brief stare, it looked ok to wedge that one in as well. Box didn't tell me I shouldn't have done that.
-Mike
v6 cover workqueue: Introduce low-level unbound wq sysfs cpumask v5
In reply to Lai JiangshanThis patchset mostly copies from Frederic and split the apply_workqueue_attrs() as TJ's suggest.
This patchset still doesn't include the patch "workqueue: Allow changing attributions of ordered workqueues", I hope to reduce the review processing. The handling for the ordered workqueue will be repose after this patchset accepted.
changed from v5:
Apply TJ's comment, rename, error-path .etc.
The default pwq fallback to the low level global cpumask when (and ONLY when) the cpumask set by the user doesn't overlap with the low level cpumask.
Changed from V4
Add workqueue_unbounds_cpumask_set() kernel API and minimally restruct the patch4.
---
Frederic Weisbecker (2):
workqueue: Reorder sysfs code
workqueue: Create low-level unbound workqueues cpumask
Lai Jiangshan (2):
workqueue: split apply_workqueue_attrs() into 3 stages
workqueue: Allow modifying low level unbound workqueue cpumask
include/linux/workqueue.h | 1 + kernel/workqueue.c | 971 +++++++++++++++++++++++++++------------------- 2 files changed, 568 insertions(+), 404 deletions(-)
Signature · 1 line
2.1.0
PATCH v6 1/4 workqueue: Reorder sysfs code
From: Frederic Weisbecker <[email protected]>
The sysfs code usually belongs to the botom of the file since it deals with high level objects. In the workqueue code it's misplaced and such that we'll need to work around functions references to allow the sysfs code to call APIs like apply_workqueue_attrs().
Lets move that block further in the file, almost the botom.
And declare workqueue_sysfs_unregister() just before destroy_workqueue() which reference it.
--- kernel/workqueue.c | 636 +++++++++++++++++++++++++++-------------------------- 1 file changed, 319 insertions(+), 317 deletions(-)
kernel/workqueue.c +319 -317
@@ -3001,323 +3001,6 @@ int execute_in_process_context(work_func_t fn, struct execute_work *ew) }EXPORT_SYMBOL_GPL (execute_in_process_context); -#ifdef CONFIG_SYSFS -/* -* Workqueues with WQ_SYSFS flag set is visible to userland via -* /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the -* following attributes. -* -* per_cpu RO bool : whether the workqueue is per-cpu or unbound -* max_active RW int : maximum number of in-flight work items -* -* Unbound workqueues have the following extra attributes. -* -* id RO int : the associated pool ID -* nice RW int : nice value of the workers -* cpumask RW mask : bitmask of allowed CPUs for the workers -*/ -struct wq_device { -struct workqueue_struct *wq; -struct device dev; -}; - -static struct workqueue_struct *dev_to_wq (struct device *dev) -{ - struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); - -return wq_dev->wq; -} - -static ssize_t per_cpu_show (struct device *dev,struct device_attribute *attr, -char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); - -return scnprintf (buf,PAGE_SIZE ,"%d\n" , (bool )!(wq->flags &WQ_UNBOUND )); -} -static DEVICE_ATTR_RO (per_cpu); - -static ssize_t max_active_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); - -return scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->saved_max_active); -} - -static ssize_t max_active_store (struct device *dev, -struct device_attribute *attr,const char *buf, -size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int val; - -if (sscanf (buf,"%d" , &val) !=1 || val <=0 ) -return -EINVAL ; - -workqueue_set_max_active (wq, val); -return count; -} -static DEVICE_ATTR_RW (max_active); - -static struct attribute *wq_sysfs_attrs[] = { - &dev_attr_per_cpu.attr, - &dev_attr_max_active.attr, -NULL , -}; -ATTRIBUTE_GROUPS (wq_sysfs); - -static ssize_t wq_pool_ids_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -const char *delim ="" ; -int node, written =0 ; - -rcu_read_lock_sched (); -for_each_node (node) { - written +=scnprintf (buf + written,PAGE_SIZE - written, -"%s%d:%d" , delim, node, -unbound_pwq_by_node (wq, node)->pool->id); - delim =" " ; - } - written +=scnprintf (buf + written,PAGE_SIZE - written,"\n" ); -rcu_read_unlock_sched (); - -return written; -} - -static ssize_t wq_nice_show (struct device *dev,struct device_attribute *attr, -char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int written; - -mutex_lock (&wq->mutex); - written =scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->unbound_attrs->nice); -mutex_unlock (&wq->mutex); - -return written; -} - -/* prepare workqueue_attrs for sysfs store operations */ -static struct workqueue_attrs *wq_sysfs_prep_attrs (struct workqueue_struct *wq) -{ -struct workqueue_attrs *attrs; - - 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; -} - -static ssize_t wq_nice_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -struct workqueue_attrs *attrs; -int ret; - - attrs =wq_sysfs_prep_attrs (wq); -if (!attrs) -return -ENOMEM ; - -if (sscanf (buf,"%d" , &attrs->nice) ==1 && - attrs->nice >=MIN_NICE && attrs->nice <=MAX_NICE ) - ret =apply_workqueue_attrs (wq, attrs); -else - ret = -EINVAL ; - -free_workqueue_attrs (attrs); -return ret ?: count; -} - -static ssize_t wq_cpumask_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int written; - -mutex_lock (&wq->mutex); - written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , -cpumask_pr_args (wq->unbound_attrs->cpumask)); -mutex_unlock (&wq->mutex); -return written; -} - -static ssize_t wq_cpumask_store (struct device *dev, -struct device_attribute *attr, -const char *buf,size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -struct workqueue_attrs *attrs; -int ret; - - attrs =wq_sysfs_prep_attrs (wq); -if (!attrs) -return -ENOMEM ; - - ret =cpumask_parse (buf, attrs->cpumask); -if (!ret) - ret =apply_workqueue_attrs (wq, attrs); - -free_workqueue_attrs (attrs); -return ret ?: count; -} - -static ssize_t wq_numa_show (struct device *dev,struct device_attribute *attr, -char *buf) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -int written; - -mutex_lock (&wq->mutex); - written =scnprintf (buf,PAGE_SIZE ,"%d\n" , - !wq->unbound_attrs->no_numa); -mutex_unlock (&wq->mutex); - -return written; -} - -static ssize_t wq_numa_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t count) -{ -struct workqueue_struct *wq =dev_to_wq (dev); -struct workqueue_attrs *attrs; -int v, ret; - - attrs =wq_sysfs_prep_attrs (wq); -if (!attrs) -return -ENOMEM ; - - ret = -EINVAL ; -if (sscanf (buf,"%d" , &v) ==1 ) { - attrs->no_numa = !v; - ret =apply_workqueue_attrs (wq, attrs); - } - -free_workqueue_attrs (attrs); -return ret ?: count; -} - -static struct device_attribute wq_sysfs_unbound_attrs[] = { -__ATTR (pool_ids,0444 , wq_pool_ids_show,NULL ), -__ATTR (nice,0644 , wq_nice_show, wq_nice_store), -__ATTR (cpumask,0644 , wq_cpumask_show, wq_cpumask_store), -__ATTR (numa,0644 , wq_numa_show, wq_numa_store), - __ATTR_NULL, -}; - -static struct bus_type wq_subsys = { - .name ="workqueue" , - .dev_groups = wq_sysfs_groups, -}; - -static int __init wq_sysfs_init (void ) -{ -return subsys_virtual_register (&wq_subsys,NULL ); -} -core_initcall (wq_sysfs_init); - -static void wq_device_release (struct device *dev) -{ - struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); - -kfree (wq_dev); -} - -/** -* workqueue_sysfs_register - make a workqueue visible in sysfs -* @wq: the workqueue to register -* -* Expose @wq in sysfs under /sys/bus/workqueue/devices. -* alloc_workqueue*() automatically calls this function if WQ_SYSFS is set -* which is the preferred method. -* -* Workqueue user should use this function directly iff it wants to apply -* workqueue_attrs before making the workqueue visible in sysfs; otherwise, -* apply_workqueue_attrs() may race against userland updating the -* attributes. -* -* Return: 0 on success, -errno on failure. -*/ -int workqueue_sysfs_register (struct workqueue_struct *wq) -{ -struct wq_device *wq_dev; -int ret; - -/* -* Adjusting max_active or creating new pwqs by applyting -* attributes breaks ordering guarantee. Disallow exposing ordered -* workqueues. -*/ -if (WARN_ON (wq->flags & __WQ_ORDERED)) -return -EINVAL ; - - wq->wq_dev = wq_dev =kzalloc (sizeof (*wq_dev),GFP_KERNEL ); -if (!wq_dev) -return -ENOMEM ; - - wq_dev->wq = wq; - wq_dev->dev.bus = &wq_subsys; - wq_dev->dev.init_name = wq->name; - wq_dev->dev.release = wq_device_release; - -/* -* unbound_attrs are created separately. Suppress uevent until -* everything is ready. -*/ -dev_set_uevent_suppress (&wq_dev->dev, true); - - ret =device_register (&wq_dev->dev); -if (ret) { -kfree (wq_dev); - wq->wq_dev =NULL ; -return ret; - } - -if (wq->flags &WQ_UNBOUND ) { -struct device_attribute *attr; - -for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { - ret =device_create_file (&wq_dev->dev, attr); -if (ret) { -device_unregister (&wq_dev->dev); - wq->wq_dev =NULL ; -return ret; - } - } - } - -dev_set_uevent_suppress (&wq_dev->dev, false); -kobject_uevent (&wq_dev->dev.kobj,KOBJ_ADD ); -return 0 ; -} - -/** -* workqueue_sysfs_unregister - undo workqueue_sysfs_register() -* @wq: the workqueue to unregister -* -* If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. -*/ -static void workqueue_sysfs_unregister (struct workqueue_struct *wq) -{ -struct wq_device *wq_dev = wq->wq_dev; - -if (!wq->wq_dev) -return ; - - wq->wq_dev =NULL ; -device_unregister (&wq_dev->dev); -} -#else /* CONFIG_SYSFS */ -static void workqueue_sysfs_unregister (struct workqueue_struct *wq) { } -#endif /* CONFIG_SYSFS */ -/** * free_workqueue_attrs - free a workqueue_attrs * @attrs: workqueue_attrs to free
@@ -4183,6 +3866,8 @@ err_destroy: }EXPORT_SYMBOL_GPL (__alloc_workqueue_key); +static void workqueue_sysfs_unregister (struct workqueue_struct *wq); +/** * destroy_workqueue - safely terminate a workqueue * @wq: target workqueue
@@ -5014,6 +4699,323 @@ out_unlock: }#endif /* CONFIG_FREEZER */ +#ifdef CONFIG_SYSFS +/* +* Workqueues with WQ_SYSFS flag set is visible to userland via +* /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the +* following attributes. +* +* per_cpu RO bool : whether the workqueue is per-cpu or unbound +* max_active RW int : maximum number of in-flight work items +* +* Unbound workqueues have the following extra attributes. +* +* id RO int : the associated pool ID +* nice RW int : nice value of the workers +* cpumask RW mask : bitmask of allowed CPUs for the workers +*/ +struct wq_device { +struct workqueue_struct *wq; +struct device dev; +}; + +static struct workqueue_struct *dev_to_wq (struct device *dev) +{ + struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); + +return wq_dev->wq; +} + +static ssize_t per_cpu_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , (bool )!(wq->flags &WQ_UNBOUND )); +} +static DEVICE_ATTR_RO (per_cpu); + +static ssize_t max_active_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); + +return scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->saved_max_active); +} + +static ssize_t max_active_store (struct device *dev, +struct device_attribute *attr,const char *buf, +size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int val; + +if (sscanf (buf,"%d" , &val) !=1 || val <=0 ) +return -EINVAL ; + +workqueue_set_max_active (wq, val); +return count; +} +static DEVICE_ATTR_RW (max_active); + +static struct attribute *wq_sysfs_attrs[] = { + &dev_attr_per_cpu.attr, + &dev_attr_max_active.attr, +NULL , +}; +ATTRIBUTE_GROUPS (wq_sysfs); + +static ssize_t wq_pool_ids_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +const char *delim ="" ; +int node, written =0 ; + +rcu_read_lock_sched (); +for_each_node (node) { + written +=scnprintf (buf + written,PAGE_SIZE - written, +"%s%d:%d" , delim, node, +unbound_pwq_by_node (wq, node)->pool->id); + delim =" " ; + } + written +=scnprintf (buf + written,PAGE_SIZE - written,"\n" ); +rcu_read_unlock_sched (); + +return written; +} + +static ssize_t wq_nice_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int written; + +mutex_lock (&wq->mutex); + written =scnprintf (buf,PAGE_SIZE ,"%d\n" , wq->unbound_attrs->nice); +mutex_unlock (&wq->mutex); + +return written; +} + +/* prepare workqueue_attrs for sysfs store operations */ +static struct workqueue_attrs *wq_sysfs_prep_attrs (struct workqueue_struct *wq) +{ +struct workqueue_attrs *attrs; + + 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; +} + +static ssize_t wq_nice_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +struct workqueue_attrs *attrs; +int ret; + + attrs =wq_sysfs_prep_attrs (wq); +if (!attrs) +return -ENOMEM ; + +if (sscanf (buf,"%d" , &attrs->nice) ==1 && + attrs->nice >=MIN_NICE && attrs->nice <=MAX_NICE ) + ret =apply_workqueue_attrs (wq, attrs); +else + ret = -EINVAL ; + +free_workqueue_attrs (attrs); +return ret ?: count; +} + +static ssize_t wq_cpumask_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int written; + +mutex_lock (&wq->mutex); + written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , +cpumask_pr_args (wq->unbound_attrs->cpumask)); +mutex_unlock (&wq->mutex); +return written; +} + +static ssize_t wq_cpumask_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +struct workqueue_attrs *attrs; +int ret; + + attrs =wq_sysfs_prep_attrs (wq); +if (!attrs) +return -ENOMEM ; + + ret =cpumask_parse (buf, attrs->cpumask); +if (!ret) + ret =apply_workqueue_attrs (wq, attrs); + +free_workqueue_attrs (attrs); +return ret ?: count; +} + +static ssize_t wq_numa_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +int written; + +mutex_lock (&wq->mutex); + written =scnprintf (buf,PAGE_SIZE ,"%d\n" , + !wq->unbound_attrs->no_numa); +mutex_unlock (&wq->mutex); + +return written; +} + +static ssize_t wq_numa_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t count) +{ +struct workqueue_struct *wq =dev_to_wq (dev); +struct workqueue_attrs *attrs; +int v, ret; + + attrs =wq_sysfs_prep_attrs (wq); +if (!attrs) +return -ENOMEM ; + + ret = -EINVAL ; +if (sscanf (buf,"%d" , &v) ==1 ) { + attrs->no_numa = !v; + ret =apply_workqueue_attrs (wq, attrs); + } + +free_workqueue_attrs (attrs); +return ret ?: count; +} + +static struct device_attribute wq_sysfs_unbound_attrs[] = { +__ATTR (pool_ids,0444 , wq_pool_ids_show,NULL ), +__ATTR (nice,0644 , wq_nice_show, wq_nice_store), +__ATTR (cpumask,0644 , wq_cpumask_show, wq_cpumask_store), +__ATTR (numa,0644 , wq_numa_show, wq_numa_store), + __ATTR_NULL, +}; + +static struct bus_type wq_subsys = { + .name ="workqueue" , + .dev_groups = wq_sysfs_groups, +}; + +static int __init wq_sysfs_init (void ) +{ +return subsys_virtual_register (&wq_subsys,NULL ); +} +core_initcall (wq_sysfs_init); + +static void wq_device_release (struct device *dev) +{ + struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); + +kfree (wq_dev); +} + +/** +* workqueue_sysfs_register - make a workqueue visible in sysfs +* @wq: the workqueue to register +* +* Expose @wq in sysfs under /sys/bus/workqueue/devices. +* alloc_workqueue*() automatically calls this function if WQ_SYSFS is set +* which is the preferred method. +* +* Workqueue user should use this function directly iff it wants to apply +* workqueue_attrs before making the workqueue visible in sysfs; otherwise, +* apply_workqueue_attrs() may race against userland updating the +* attributes. +* +* Return: 0 on success, -errno on failure. +*/ +int workqueue_sysfs_register (struct workqueue_struct *wq) +{ +struct wq_device *wq_dev; +int ret; + +/* +* Adjusting max_active or creating new pwqs by applyting +* attributes breaks ordering guarantee. Disallow exposing ordered +* workqueues. +*/ +if (WARN_ON (wq->flags & __WQ_ORDERED)) +return -EINVAL ; + + wq->wq_dev = wq_dev =kzalloc (sizeof (*wq_dev),GFP_KERNEL ); +if (!wq_dev) +return -ENOMEM ; + + wq_dev->wq = wq; + wq_dev->dev.bus = &wq_subsys; + wq_dev->dev.init_name = wq->name; + wq_dev->dev.release = wq_device_release; + +/* +* unbound_attrs are created separately. Suppress uevent until +* everything is ready. +*/ +dev_set_uevent_suppress (&wq_dev->dev, true); + + ret =device_register (&wq_dev->dev); +if (ret) { +kfree (wq_dev); + wq->wq_dev =NULL ; +return ret; + } + +if (wq->flags &WQ_UNBOUND ) { +struct device_attribute *attr; + +for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { + ret =device_create_file (&wq_dev->dev, attr); +if (ret) { +device_unregister (&wq_dev->dev); + wq->wq_dev =NULL ; +return ret; + } + } + } + +dev_set_uevent_suppress (&wq_dev->dev, false); +kobject_uevent (&wq_dev->dev.kobj,KOBJ_ADD ); +return 0 ; +} + +/** +* workqueue_sysfs_unregister - undo workqueue_sysfs_register() +* @wq: the workqueue to unregister +* +* If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. +*/ +static void workqueue_sysfs_unregister (struct workqueue_struct *wq) +{ +struct wq_device *wq_dev = wq->wq_dev; + +if (!wq->wq_dev) +return ; + + wq->wq_dev =NULL ; +device_unregister (&wq_dev->dev); +} +#else /* CONFIG_SYSFS */ +static void workqueue_sysfs_unregister (struct workqueue_struct *wq) { } +#endif /* CONFIG_SYSFS */ +static void __init wq_numa_init (void ) {cpumask_var_t *tbl;
Signature · 1 line
2.1.0
Tejun Heo
maintainerRe: [PATCH 1/4 V6] workqueue: Reorder sysfs code
Quoted 12 lines · Lai Jiangshan
From: Frederic Weisbecker <[email protected]>
The sysfs code usually belongs to the botom of the file since it deals with high level objects. In the workqueue code it's misplaced and such that we'll need to work around functions references to allow the sysfs code to call APIs like
apply_workqueue_attrs().Lets move that block further in the file, almost the botom.
And declare
workqueue_sysfs_unregister()just beforedestroy_workqueue()which reference it.
Quoted 10 lines
Suggested-by Tejun Heo <[email protected]>Cc Christoph Lameter <[email protected]>Cc Kevin Hilman <[email protected]>Cc Lai Jiangshan <[email protected]>Cc Mike Galbraith <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Tejun Heo <[email protected]>Cc Viresh Kumar <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>Signed-off-by Lai Jiangshan <[email protected]>
Moved the forward declaration of workqueue_sysfs_unregister() where other forward declarations are and applied to wq/for-4.1.
Thanks.
Signature · 1 line
tejun
PATCH v6 2/4 workqueue: split apply_workqueue_attrs() into 3 stages
In reply to Lai JiangshanCurrent apply_workqueue_attrs() includes pwqs-allocation and pwqs-installation, so when we batch multiple apply_workqueue_attrs()s as a transaction, we can't ensure the transaction must succeed or fail as a complete unit.
To solve this, we split apply_workqueue_attrs() into three stages. The first stage does the preparation: allocation memory, pwqs. The second stage does the attrs-installaion and pwqs-installation. The third stage frees the allocated memory and (old or unused) pwqs.
As the result, batching multiple apply_workqueue_attrs()s can succeed or fail as a complete unit:
1) batch do all the first stage for all the workqueues
2) only commit all when all the above succeed.
This patch is a preparation for the next patch ("Allow modifying low level unbound workqueue cpumask") which will do a multiple apply_workqueue_attrs().
The patch doesn't have functionality changed except two minor adjustment:
1) free_unbound_pwq() for the error path is removed, we use the heavier version put_pwq_unlocked() instead since the error path is rare. this adjustment simplifies the code.
2) the memory-allocation is also moved into wq_pool_mutex. this is needed to avoid to do the further splitting.
--- kernel/workqueue.c | 200 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 116 insertions(+), 84 deletions(-)
kernel/workqueue.c +116 -84
@@ -3424,17 +3424,6 @@ static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,return pwq; } -/* undo alloc_unbound_pwq(), used only in the error path */ -static void free_unbound_pwq (struct pool_workqueue *pwq) -{ -lockdep_assert_held (&wq_pool_mutex); - -if (pwq) { -put_unbound_pool (pwq->pool); -kmem_cache_free (pwq_cache, pwq); - } -} -/** * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node * @attrs: the wq_attrs of interest
@@ -3497,42 +3486,49 @@ static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,return old_pwq; } -/** -* 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) +/* Context to store the prepared attrs & pwqs before installed */ +struct apply_wqattrs_ctx { +struct workqueue_struct *wq;/* target to be installed */ +struct workqueue_attrs *attrs;/* attrs for installing */ +struct pool_workqueue *dfl_pwq; +struct pool_workqueue *pwq_tbl[]; +}; + +/* Free the resources after success or abort */ +static void apply_wqattrs_cleanup (struct apply_wqattrs_ctx *ctx) +{ +if (ctx) { +int node; + +/* put the pwqs */ +for_each_node (node) +put_pwq_unlocked (ctx->pwq_tbl[node]); +put_pwq_unlocked (ctx->dfl_pwq); + +free_workqueue_attrs (ctx->attrs); + +kfree (ctx); + } +} + +/* Allocates the attrs and pwqs for later installment */ +static struct apply_wqattrs_ctx * +apply_wqattrs_prepare (struct workqueue_struct *wq, +const struct workqueue_attrs *attrs) { +struct apply_wqattrs_ctx *ctx;struct workqueue_attrs *new_attrs, *tmp_attrs; -struct pool_workqueue **pwq_tbl, *dfl_pwq; -int node, ret; +int node; -/* only unbound workqueues can change attributes */ -if (WARN_ON (!(wq->flags &WQ_UNBOUND ))) -return -EINVAL ; +lockdep_assert_held (&wq_pool_mutex); -/* creating multiple pwqs breaks ordering guarantee */ -if (WARN_ON ((wq->flags & __WQ_ORDERED) && !list_empty (&wq->pwqs))) -return -EINVAL ; + ctx =kzalloc (sizeof (*ctx) + nr_node_ids *sizeof (ctx->pwq_tbl[0 ]), +GFP_KERNEL ); - pwq_tbl =kzalloc (nr_node_ids *sizeof (pwq_tbl[0 ]),GFP_KERNEL ); new_attrs =alloc_workqueue_attrs (GFP_KERNEL ); tmp_attrs =alloc_workqueue_attrs (GFP_KERNEL ); -if (!pwq_tbl || !new_attrs || !tmp_attrs) - goto enomem; +if (!ctx || !new_attrs || !tmp_attrs) + goto out_free;/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs);
@@ -3546,75 +3542,111 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,copy_workqueue_attrs (tmp_attrs, new_attrs);/* -* 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); - -/* * If something goes wrong during CPU up/down, we'll fall back to * the default pwq covering whole @attrs->cpumask. Always create * it even if we don't use it immediately. */ - dfl_pwq =alloc_unbound_pwq (wq, new_attrs); -if (!dfl_pwq) - goto enomem_pwq; + ctx->dfl_pwq =alloc_unbound_pwq (wq, new_attrs); +if (!ctx->dfl_pwq) + goto out_free;for_each_node (node) {if (wq_calc_node_cpumask (attrs, node,-1 , tmp_attrs->cpumask)) { - pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs); -if (!pwq_tbl[node]) - goto enomem_pwq; + ctx->pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs); +if (!ctx->pwq_tbl[node]) + goto out_free; }else { - dfl_pwq->refcnt++; - pwq_tbl[node] = dfl_pwq; + ctx->dfl_pwq->refcnt++; + ctx->pwq_tbl[node] = ctx->dfl_pwq; } } -mutex_unlock (&wq_pool_mutex); + ctx->wq = wq; + ctx->attrs = new_attrs; +free_workqueue_attrs (tmp_attrs); +return ctx; + +out_free: +free_workqueue_attrs (tmp_attrs); +free_workqueue_attrs (new_attrs); +apply_wqattrs_cleanup (ctx); +return NULL ; +} + +/* Set the unbound_attr and install the prepared pwqs. Should not fail */ +static void apply_wqattrs_commit (struct apply_wqattrs_ctx *ctx) +{ +int node;/* all pwqs have been created successfully, let's install'em */ -mutex_lock (&wq->mutex); +mutex_lock (&ctx->wq->mutex); -copy_workqueue_attrs (wq->unbound_attrs, new_attrs); +copy_workqueue_attrs (ctx->wq->unbound_attrs, ctx->attrs);/* save the previous pwq and install the new one */ for_each_node (node) - pwq_tbl[node] =numa_pwq_tbl_install (wq, node, pwq_tbl[node]); + ctx->pwq_tbl[node] =numa_pwq_tbl_install (ctx->wq, node, + ctx->pwq_tbl[node]);/* @dfl_pwq might not have been used, ensure it's linked */ -link_pwq (dfl_pwq); -swap (wq->dfl_pwq, dfl_pwq); +link_pwq (ctx->dfl_pwq); +swap (ctx->wq->dfl_pwq, ctx->dfl_pwq); -mutex_unlock (&wq->mutex); +mutex_unlock (&ctx->wq->mutex); +} -/* put the old pwqs */ -for_each_node (node) -put_pwq_unlocked (pwq_tbl[node]); -put_pwq_unlocked (dfl_pwq); +/** +* 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) +{ +struct apply_wqattrs_ctx *ctx; +int ret = -ENOMEM ; -put_online_cpus (); - ret =0 ; -/* fall through */ -out_free: -free_workqueue_attrs (tmp_attrs); -free_workqueue_attrs (new_attrs); -kfree (pwq_tbl); -return ret; +/* only unbound workqueues can change attributes */ +if (WARN_ON (!(wq->flags &WQ_UNBOUND ))) +return -EINVAL ; -enomem_pwq: -free_unbound_pwq (dfl_pwq); -for_each_node (node) -if (pwq_tbl && pwq_tbl[node] != dfl_pwq) -free_unbound_pwq (pwq_tbl[node]); +/* creating multiple pwqs breaks ordering guarantee */ +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);mutex_unlock (&wq_pool_mutex); +put_online_cpus (); -enomem: - ret = -ENOMEM ; - goto out_free; + +/* the ctx has been prepared successfully, let's commit it */ +if (ctx) { +apply_wqattrs_commit (ctx); + ret =0 ; + } + +apply_wqattrs_cleanup (ctx); + +return ret; }/**
Signature · 1 line
2.1.0
Tejun Heo
maintainerRe: [PATCH 2/4 V6] workqueue: split apply_workqueue_attrs() into 3 stages
Quoted 6 lines · Lai Jiangshan
The patch doesn't have functionality changed except two minor adjustment:
1)free_unbound_pwq()for the error path is removed, we use the heavier versionput_pwq_unlocked()instead since the error path is rare. this adjustment simplifies the code.
2) the memory-allocation is also moved intowq_pool_mutex. this is needed to avoid to do the further splitting.
And we're dropping online_cpus locking before applying the new pwq's. Is that safe?
Thanks.
Signature · 1 line
tejun
PATCH v6 3/4 workqueue: Create low-level unbound workqueues cpumask
In reply to Lai JiangshanFrom: Frederic Weisbecker <[email protected]>
Create a cpumask that limit the affinity of all unbound workqueues. This cpumask is controlled though a file at the root of the workqueue sysfs directory.
It works on a lower-level than the per WQ_SYSFS workqueues cpumask files such that the effective cpumask applied for a given unbound workqueue is the intersection of /sys/devices/virtual/workqueue/$WORKQUEUE/cpumask and the new /sys/devices/virtual/workqueue/cpumask file.
This patch implements the basic infrastructure and the read interface. wq_unbound_global_cpumask is initially set to cpu_possible_mask.
--- kernel/workqueue.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
kernel/workqueue.c +27 -2
@@ -299,6 +299,8 @@ static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */static LIST_HEAD (workqueues);/* PR: list of all workqueues */ static bool workqueue_freezing;/* PL: have wqs started freezing? */ +static cpumask_var_t wq_unbound_global_cpumask; +/* the per-cpu worker pools */ static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools);
@@ -3532,7 +3534,7 @@ apply_wqattrs_prepare(struct workqueue_struct *wq,/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_global_cpumask);/* * We may create multiple pwqs with differing cpumasks. Make a
@@ -4947,9 +4949,29 @@ static struct bus_type wq_subsys = { .dev_groups = wq_sysfs_groups, }; +static ssize_t wq_unbound_global_cpumask_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +int written; + + written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , +cpumask_pr_args (wq_unbound_global_cpumask)); + +return written; +} + +static struct device_attribute wq_sysfs_cpumask_attr = +__ATTR (cpumask,0444 , wq_unbound_global_cpumask_show,NULL ); +static int __init wq_sysfs_init (void ) { -return subsys_virtual_register (&wq_subsys,NULL ); +int err; + + err =subsys_virtual_register (&wq_subsys,NULL ); +if (err) +return err; + +return device_create_file (wq_subsys.dev_root, &wq_sysfs_cpumask_attr); }core_initcall (wq_sysfs_init);
@@ -5097,6 +5119,9 @@ static int __init init_workqueues(void)WARN_ON (__alignof__(struct pool_workqueue ) < __alignof__(long long )); +BUG_ON (!alloc_cpumask_var (&wq_unbound_global_cpumask,GFP_KERNEL )); +cpumask_copy (wq_unbound_global_cpumask, cpu_possible_mask); + pwq_cache =KMEM_CACHE (pool_workqueue,SLAB_PANIC );cpu_notifier (workqueue_cpu_up_callback,CPU_PRI_WORKQUEUE_UP );
Signature · 1 line
2.1.0
PATCH v6 4/4 workqueue: Allow modifying low level unbound workqueue cpumask
In reply to Lai JiangshanAllow to modify the low-level unbound workqueues cpumask through sysfs. This is performed by traversing the entire workqueue list and calling apply_wqattrs_prepare() on the unbound workqueues with the low level mask passed in. Only after all the preparation are done, we commit them all together.
The oreder-workquue is ignore from the low level unbound workqueue cpumask, it will be handled in near future.
The per-nodes' pwqs are mandatorily controlled by the low level cpumask, while the default pwq fallback to the low level global cpumask when (and ONLY when) the cpumask set by the user doesn't overlap with the low level cpumask.
The default wq_unbound_global_cpumask is still cpu_possible_mask due to the workqueue subsystem doesn't know what is the best default value for the runtime, the system manager or other subsystem which knows the sufficient information should set it when needed.
--- include/linux/workqueue.h | 1 + kernel/workqueue.c | 124 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 115 insertions(+), 10 deletions(-)
include/linux/workqueue.h +1 -0
@@ -424,6 +424,7 @@ struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask);void free_workqueue_attrs (struct workqueue_attrs *attrs);int apply_workqueue_attrs (struct workqueue_struct *wq,const struct workqueue_attrs *attrs); +int workqueue_set_unbound_global_cpumask (cpumask_var_t cpumask);extern bool queue_work_on (int cpu,struct workqueue_struct *wq,struct work_struct *work);
kernel/workqueue.c +114 -10
212 lines not on this page · raw message
Signature · 1 line
2.1.0
Tejun Heo
maintainerRe: [PATCH 4/4 V6] workqueue: Allow modifying low level unbound workqueue cpumask
kernel/workqueue.c apply_wqattrs_prepare(struct workqueue_struct *wq, · line 3542
Quoted 6 lines · Lai Jiangshan
kernel/workqueue.c +3
-1/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_global_cpumask); +copy_workqueue_attrs (pwq_attrs, attrs); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (pwq_attrs->cpumask, pwq_attrs->cpumask, unbound_cpumask);
Hmmm... why do we need to keep track of both cpu_possible_mask and unbound_cpumask? Can't we just make unbound_cpumask replace cpu_possible_mask for unbound workqueues?
Thanks.
Signature · 1 line
tejun
Re: [PATCH 4/4 V6] workqueue: Allow modifying low level unbound workqueue cpumask
Quoted 12 lines · Tejun Heo
kernel/workqueue.c apply_wqattrs_prepare(struct workqueue_struct *wq, · line 3542
Quoted 6 lines · Lai Jiangshan
kernel/workqueue.c +3
-1/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_global_cpumask); +copy_workqueue_attrs (pwq_attrs, attrs); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (pwq_attrs->cpumask, pwq_attrs->cpumask, unbound_cpumask);Hmmm... why do we need to keep track of both
cpu_possible_maskandunbound_cpumask? Can't we just makeunbound_cpumaskreplacecpu_possible_maskfor unbound workqueues?
I want to save the original user-setting cpumask.
When any time the wq_unbound_global_cpumask is changed, the new effective cpumask is the-original-user-setting-cpumask & wq_unbound_global_cpumask instead of
the-last-effective-cpumask & wq_unbound_global_cpumask.
thanks,
Lai
Quoted 2 lines
Thanks.
Tejun Heo
maintainerRe: [PATCH 4/4 V6] workqueue: Allow modifying low level unbound workqueue cpumask
Hello, Lai.
Quoted 14 lines · Lai Jiangshan
Quoted 12 lines · Tejun Heo
kernel/workqueue.c apply_wqattrs_prepare(struct workqueue_struct *wq, · line 3542
Quoted 6 lines · Lai Jiangshan
kernel/workqueue.c +3
-1/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_global_cpumask); +copy_workqueue_attrs (pwq_attrs, attrs); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (pwq_attrs->cpumask, pwq_attrs->cpumask, unbound_cpumask);Hmmm... why do we need to keep track of both
cpu_possible_maskandunbound_cpumask? Can't we just makeunbound_cpumaskreplacecpu_possible_maskfor unbound workqueues?
Quoted 7 lines
I want to save the original user-setting cpumask.
When any time the
wq_unbound_global_cpumaskis changed, the new effective cpumask is the-original-user-setting-cpumask &wq_unbound_global_cpumaskinstead of
the-last-effective-cpumask &wq_unbound_global_cpumask.
Yes, I get that, but that'd require just tracking the original configured value and the unbound_cpumask masked value, no? What am I missing?
Thanks.
Signature · 1 line
tejun
Re: [PATCH 4/4 V6] workqueue: Allow modifying low level unbound workqueue cpumask
Quoted 19 lines · Tejun Heo
Hello, Lai.
Quoted 16 lines · Lai Jiangshan
Quoted 12 lines · Tejun Heo
kernel/workqueue.c apply_wqattrs_prepare(struct workqueue_struct *wq, · line 3542
Quoted 6 lines · Lai Jiangshan
kernel/workqueue.c +3
-1/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_global_cpumask); +copy_workqueue_attrs (pwq_attrs, attrs); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (pwq_attrs->cpumask, pwq_attrs->cpumask, unbound_cpumask);Hmmm... why do we need to keep track of both
cpu_possible_maskandunbound_cpumask? Can't we just makeunbound_cpumaskreplacecpu_possible_maskfor unbound workqueues?I want to save the original user-setting cpumask.
Quoted 7 lines
Quoted 5 lines
When any time the
wq_unbound_global_cpumaskis changed, the new effective cpumask is the-original-user-setting-cpumask &wq_unbound_global_cpumaskinstead of
the-last-effective-cpumask &wq_unbound_global_cpumask.Yes, I get that, but that'd require just tracking the original
wq->unbound_attrs (new_attrs) saves the original configured value and is needed to be keep track of.
For sanity, it needs to be masked with cpu_possible_mask.
+ cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
This code is changed back to the original code (before this patchset).
In the next iterate, I will reduce the number of the local vars to make the code clearer.
Quoted 5 lines
configured value and the
unbound_cpumaskmasked value, no? What am I missing?Thanks.
PATCH v7 1/3 workqueue: split apply_workqueue_attrs() into 3 stages
In reply to Lai JiangshanCurrent apply_workqueue_attrs() includes pwqs-allocation and pwqs-installation, so when we batch multiple apply_workqueue_attrs()s as a transaction, we can't ensure the transaction must succeed or fail as a complete unit.
To solve this, we split apply_workqueue_attrs() into three stages. The first stage does the preparation: allocation memory, pwqs. The second stage does the attrs-installaion and pwqs-installation. The third stage frees the allocated memory and (old or unused) pwqs.
As the result, batching multiple apply_workqueue_attrs()s can succeed or fail as a complete unit:
1) batch do all the first stage for all the workqueues
2) only commit all when all the above succeed.
This patch is a preparation for the next patch ("Allow modifying low level unbound workqueue cpumask") which will do a multiple apply_workqueue_attrs().
The patch doesn't have functionality changed except two minor adjustment:
1) free_unbound_pwq() for the error path is removed, we use the heavier version put_pwq_unlocked() instead since the error path is rare. this adjustment simplifies the code.
2) the memory-allocation is also moved into wq_pool_mutex. this is needed to avoid to do the further splitting.
--- kernel/workqueue.c | 200 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 116 insertions(+), 84 deletions(-)
kernel/workqueue.c +116 -84
@@ -3425,17 +3425,6 @@ static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,return pwq; } -/* undo alloc_unbound_pwq(), used only in the error path */ -static void free_unbound_pwq (struct pool_workqueue *pwq) -{ -lockdep_assert_held (&wq_pool_mutex); - -if (pwq) { -put_unbound_pool (pwq->pool); -kmem_cache_free (pwq_cache, pwq); - } -} -/** * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node * @attrs: the wq_attrs of interest
@@ -3498,42 +3487,49 @@ static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,return old_pwq; } -/** -* 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) +/* Context to store the prepared attrs & pwqs before applied */ +struct apply_wqattrs_ctx { +struct workqueue_struct *wq;/* target to be applied */ +struct workqueue_attrs *attrs;/* configured attrs */ +struct pool_workqueue *dfl_pwq; +struct pool_workqueue *pwq_tbl[]; +}; + +/* Free the resources after success or abort */ +static void apply_wqattrs_cleanup (struct apply_wqattrs_ctx *ctx) +{ +if (ctx) { +int node; + +/* put the pwqs */ +for_each_node (node) +put_pwq_unlocked (ctx->pwq_tbl[node]); +put_pwq_unlocked (ctx->dfl_pwq); + +free_workqueue_attrs (ctx->attrs); + +kfree (ctx); + } +} + +/* Allocates the attrs and pwqs for later installment */ +static struct apply_wqattrs_ctx * +apply_wqattrs_prepare (struct workqueue_struct *wq, +const struct workqueue_attrs *attrs) { +struct apply_wqattrs_ctx *ctx;struct workqueue_attrs *new_attrs, *tmp_attrs; -struct pool_workqueue **pwq_tbl, *dfl_pwq; -int node, ret; +int node; -/* only unbound workqueues can change attributes */ -if (WARN_ON (!(wq->flags &WQ_UNBOUND ))) -return -EINVAL ; +lockdep_assert_held (&wq_pool_mutex); -/* creating multiple pwqs breaks ordering guarantee */ -if (WARN_ON ((wq->flags & __WQ_ORDERED) && !list_empty (&wq->pwqs))) -return -EINVAL ; + ctx =kzalloc (sizeof (*ctx) + nr_node_ids *sizeof (ctx->pwq_tbl[0 ]), +GFP_KERNEL ); - pwq_tbl =kzalloc (nr_node_ids *sizeof (pwq_tbl[0 ]),GFP_KERNEL ); new_attrs =alloc_workqueue_attrs (GFP_KERNEL ); tmp_attrs =alloc_workqueue_attrs (GFP_KERNEL ); -if (!pwq_tbl || !new_attrs || !tmp_attrs) - goto enomem; +if (!ctx || !new_attrs || !tmp_attrs) + goto out_free;/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs);
@@ -3547,75 +3543,111 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,copy_workqueue_attrs (tmp_attrs, new_attrs);/* -* 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); - -/* * If something goes wrong during CPU up/down, we'll fall back to * the default pwq covering whole @attrs->cpumask. Always create * it even if we don't use it immediately. */ - dfl_pwq =alloc_unbound_pwq (wq, new_attrs); -if (!dfl_pwq) - goto enomem_pwq; + ctx->dfl_pwq =alloc_unbound_pwq (wq, new_attrs); +if (!ctx->dfl_pwq) + goto out_free;for_each_node (node) {if (wq_calc_node_cpumask (attrs, node,-1 , tmp_attrs->cpumask)) { - pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs); -if (!pwq_tbl[node]) - goto enomem_pwq; + ctx->pwq_tbl[node] =alloc_unbound_pwq (wq, tmp_attrs); +if (!ctx->pwq_tbl[node]) + goto out_free; }else { - dfl_pwq->refcnt++; - pwq_tbl[node] = dfl_pwq; + ctx->dfl_pwq->refcnt++; + ctx->pwq_tbl[node] = ctx->dfl_pwq; } } -mutex_unlock (&wq_pool_mutex); + ctx->attrs = new_attrs; + ctx->wq = wq; +free_workqueue_attrs (tmp_attrs); +return ctx; + +out_free: +free_workqueue_attrs (tmp_attrs); +free_workqueue_attrs (new_attrs); +apply_wqattrs_cleanup (ctx); +return NULL ; +} + +/* Set the unbound_attr and install the prepared pwqs. Should not fail */ +static void apply_wqattrs_commit (struct apply_wqattrs_ctx *ctx) +{ +int node;/* all pwqs have been created successfully, let's install'em */ -mutex_lock (&wq->mutex); +mutex_lock (&ctx->wq->mutex); -copy_workqueue_attrs (wq->unbound_attrs, new_attrs); +copy_workqueue_attrs (ctx->wq->unbound_attrs, ctx->attrs);/* save the previous pwq and install the new one */ for_each_node (node) - pwq_tbl[node] =numa_pwq_tbl_install (wq, node, pwq_tbl[node]); + ctx->pwq_tbl[node] =numa_pwq_tbl_install (ctx->wq, node, + ctx->pwq_tbl[node]);/* @dfl_pwq might not have been used, ensure it's linked */ -link_pwq (dfl_pwq); -swap (wq->dfl_pwq, dfl_pwq); +link_pwq (ctx->dfl_pwq); +swap (ctx->wq->dfl_pwq, ctx->dfl_pwq); -mutex_unlock (&wq->mutex); +mutex_unlock (&ctx->wq->mutex); +} -/* put the old pwqs */ -for_each_node (node) -put_pwq_unlocked (pwq_tbl[node]); -put_pwq_unlocked (dfl_pwq); +/** +* 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) +{ +struct apply_wqattrs_ctx *ctx; +int ret = -ENOMEM ; -put_online_cpus (); - ret =0 ; -/* fall through */ -out_free: -free_workqueue_attrs (tmp_attrs); -free_workqueue_attrs (new_attrs); -kfree (pwq_tbl); -return ret; +/* only unbound workqueues can change attributes */ +if (WARN_ON (!(wq->flags &WQ_UNBOUND ))) +return -EINVAL ; -enomem_pwq: -free_unbound_pwq (dfl_pwq); -for_each_node (node) -if (pwq_tbl && pwq_tbl[node] != dfl_pwq) -free_unbound_pwq (pwq_tbl[node]); +/* creating multiple pwqs breaks ordering guarantee */ +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);mutex_unlock (&wq_pool_mutex); + +/* the ctx has been prepared successfully, let's commit it */ +if (ctx) { +apply_wqattrs_commit (ctx); + ret =0 ; + } +put_online_cpus (); -enomem: - ret = -ENOMEM ; - goto out_free; + +apply_wqattrs_cleanup (ctx); + +return ret; }/**
Signature · 1 line
2.1.0
PATCH v7 2/3 workqueue: Create low-level unbound workqueues cpumask
From: Frederic Weisbecker <[email protected]>
Create a cpumask that limit the affinity of all unbound workqueues. This cpumask is controlled though a file at the root of the workqueue sysfs directory.
It works on a lower-level than the per WQ_SYSFS workqueues cpumask files such that the effective cpumask applied for a given unbound workqueue is the intersection of /sys/devices/virtual/workqueue/$WORKQUEUE/cpumask and the new /sys/devices/virtual/workqueue/cpumask file.
This patch implements the basic infrastructure and the read interface. wq_unbound_global_cpumask is initially set to cpu_possible_mask.
--- kernel/workqueue.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
kernel/workqueue.c +27 -2
@@ -299,6 +299,8 @@ static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */static LIST_HEAD (workqueues);/* PR: list of all workqueues */ static bool workqueue_freezing;/* PL: have wqs started freezing? */ +static cpumask_var_t wq_unbound_global_cpumask; +/* the per-cpu worker pools */ static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools);
@@ -3533,7 +3535,7 @@ apply_wqattrs_prepare(struct workqueue_struct *wq,/* make a copy of @attrs and sanitize it */ copy_workqueue_attrs (new_attrs, attrs); -cpumask_and (new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); +cpumask_and (new_attrs->cpumask, new_attrs->cpumask, wq_unbound_global_cpumask);/* * We may create multiple pwqs with differing cpumasks. Make a
@@ -4946,9 +4948,29 @@ static struct bus_type wq_subsys = { .dev_groups = wq_sysfs_groups, }; +static ssize_t wq_unbound_global_cpumask_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +int written; + + written =scnprintf (buf,PAGE_SIZE ,"%*pb\n" , +cpumask_pr_args (wq_unbound_global_cpumask)); + +return written; +} + +static struct device_attribute wq_sysfs_cpumask_attr = +__ATTR (cpumask,0444 , wq_unbound_global_cpumask_show,NULL ); +static int __init wq_sysfs_init (void ) { -return subsys_virtual_register (&wq_subsys,NULL ); +int err; + + err =subsys_virtual_register (&wq_subsys,NULL ); +if (err) +return err; + +return device_create_file (wq_subsys.dev_root, &wq_sysfs_cpumask_attr); }core_initcall (wq_sysfs_init);
@@ -5096,6 +5118,9 @@ static int __init init_workqueues(void)WARN_ON (__alignof__(struct pool_workqueue ) < __alignof__(long long )); +BUG_ON (!alloc_cpumask_var (&wq_unbound_global_cpumask,GFP_KERNEL )); +cpumask_copy (wq_unbound_global_cpumask, cpu_possible_mask); + pwq_cache =KMEM_CACHE (pool_workqueue,SLAB_PANIC );cpu_notifier (workqueue_cpu_up_callback,CPU_PRI_WORKQUEUE_UP );
Signature · 1 line
2.1.0
PATCH v7 3/3 workqueue: Allow modifying low level unbound workqueue cpumask
In reply to Lai JiangshanAllow to modify the low-level unbound workqueues cpumask through sysfs. This is performed by traversing the entire workqueue list and calling apply_wqattrs_prepare() on the unbound workqueues with the low level mask passed in. Only after all the preparation are done, we commit them all together.
The oreder-workquue is ignore from the low level unbound workqueue cpumask, it will be handled in near future.
The per-nodes' pwqs are mandatorily controlled by the low level cpumask, while the default pwq fallback to the low level global cpumask when (and ONLY when) the cpumask set by the user doesn't overlap with the low level cpumask.
The default wq_unbound_global_cpumask is still cpu_possible_mask due to the workqueue subsystem doesn't know what is the best default value for the runtime, the system manager or other subsystem which knows the sufficient information should set it when needed.
--- include/linux/workqueue.h | 1 + kernel/workqueue.c | 122 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 116 insertions(+), 7 deletions(-)
include/linux/workqueue.h +1 -0
@@ -424,6 +424,7 @@ struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask);void free_workqueue_attrs (struct workqueue_attrs *attrs);int apply_workqueue_attrs (struct workqueue_struct *wq,const struct workqueue_attrs *attrs); +int workqueue_set_unbound_global_cpumask (cpumask_var_t cpumask);extern bool queue_work_on (int cpu,struct workqueue_struct *wq,struct work_struct *work);
kernel/workqueue.c +115 -7
195 lines not on this page · raw message
Signature · 1 line
2.1.0
Tejun Heo
maintainerRe: [PATCH 3/3 V7] workqueue: Allow modifying low level unbound workqueue cpumask
Hello,
Generally looks good to me. Some minor things below.
kernel/workqueue.c static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ · line 302
Quoted 10 lines · Lai Jiangshan
kernel/workqueue.c +1
-1@@ -299,7 +299,7 @@ static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */static LIST_HEAD (workqueues);/* PR: list of all workqueues */ static bool workqueue_freezing;/* PL: have wqs started freezing? */ -static cpumask_var_t wq_unbound_global_cpumask; +static cpumask_var_t wq_unbound_global_cpumask;/* PL: low level cpumask for all unbound wqs */
Are we set on this variable name? What would we lose by naming it wq_unbound_cpumask or wq_cpu_possible_mask?
kernel/workqueue.c static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, · line 3496
Quoted 5 lines
kernel/workqueue.c +1
-0@@ -3493,6 +3493,7 @@ static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,struct apply_wqattrs_ctx {struct workqueue_struct *wq;/* target to be applied */ struct workqueue_attrs *attrs;/* configured attrs */ +struct list_head list;/* queued for batching commit */
batch commit
kernel/workqueue.c static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) · line 3522
Quoted 10 lines
kernel/workqueue.c +2
-1struct pool_workqueue *dfl_pwq;struct pool_workqueue *pwq_tbl[]; };@@ -3517,7 +3518,8 @@ static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx)/* Allocates the attrs and pwqs for later installment */ static struct apply_wqattrs_ctx *apply_wqattrs_prepare (struct workqueue_struct *wq, -const struct workqueue_attrs *attrs) +const struct workqueue_attrs *attrs, +cpumask_var_t unbound_cpumask)
Why do we need this tho? The global mask is protected by pool mutex, right? The update function can set it to the new value and just call update and revert on failure.
kernel/workqueue.c static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, · line 3731
Quoted 12 lines
kernel/workqueue.c +8
-0@@ -3710,6 +3721,14 @@ static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,* wq's, the default pwq should be used. */ if (wq_calc_node_cpumask (wq->unbound_attrs, node, cpu_off, cpumask)) { +/* +* wq->unbound_attrs is the user configured attrs whose +* cpumask is not masked with wq_unbound_global_cpumask, +* so we make complete it. +*/ +cpumask_and (cpumask, cpumask, wq_unbound_global_cpumask); +if (cpumask_empty (cpumask)) + goto use_dfl_pwq;
Wouldn't it be better to apply the global cpumask before calling wq_calc_node_cpumask()? Or just move it inside wq_calc_node_cpumask?
Thanks.
Signature · 1 line
tejun
Re: [PATCH 3/3 V7] workqueue: Allow modifying low level unbound workqueue cpumask
Quoted 13 lines · Tejun Heo
Hello,
Generally looks good to me. Some minor things below.
Quoted 8 lines · Lai Jiangshan
kernel/workqueue.c +0
-0@@ -299,7 +299,7 @@ static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */static LIST_HEAD (workqueues);/* PR: list of all workqueues */ static bool workqueue_freezing;/* PL: have wqs started freezing? */
Quoted 5 lines
kernel/workqueue.c static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ · line 302
Quoted 2 lines
kernel/workqueue.c +1
-1-static cpumask_var_t wq_unbound_global_cpumask; +static cpumask_var_t wq_unbound_global_cpumask;/* PL: low level cpumask for all unbound wqs */ Are we set on this variable name? What would we lose by naming it
wq_unbound_cpumaskorwq_cpu_possible_mask?
I like wq_unbound_cpumask personally. In fact I like to have "unbound" inside to express what's concerned here. I like wq_cpu_possible_mask too but unfortunately it suggests it's about all workqueues (including per cpu ones) while it's not.
Re: [PATCH 3/3 V7] workqueue: Allow modifying low level unbound workqueue cpumask
FWIW on the testing side, I'm running these in 3.12(ish), 4.0 and 4.1 rt trees with NOHZ_FULL, and have yet to meet a problem.
-Mike
Tejun Heo
maintainerRe: [PATCH 1/3 V7] workqueue: split apply_workqueue_attrs() into 3 stages
In reply to Lai JiangshanQuoted 3 lines · Lai Jiangshan
Current
apply_workqueue_attrs()includes pwqs-allocation and pwqs-installation, so when we batch multiple apply_workqueue_attrs()s as a transaction, we can't ensure the transaction must succeed or fail as a complete unit.
Lai, can you please
* Break out threads when posting new version.
* List the changes since the last version?
Thanks.
Signature · 1 line
tejun
Re: [PATCH 1/3 V7] workqueue: split apply_workqueue_attrs() into 3 stages
Quoted 10 lines · Tejun Heo
Quoted 3 lines · Lai Jiangshan
Current
apply_workqueue_attrs()includes pwqs-allocation and pwqs-installation, so when we batch multiple apply_workqueue_attrs()s as a transaction, we can't ensure the transaction must succeed or fail as a complete unit.Lai, can you please
* Break out threads when posting new version.
* List the changes since the last version?
My bad.
I thought you had only two comments which ware handled in v7. It was considered (with my laziness) too less to be renarrated.
---
In [2/4 V6]:
```quote from TJ: And we're dropping online_cpus locking before applying the new pwq's. Is that safe? ```
It was my fault, I didn't remember when I wrongly moved the code. The patch had been tried my best keep functionality unchanged. It is fixed in [1/3 V7].
----
The changes from [4/4 V6] to [3/3 V7]:ctx->attrs (the original configured value) is not calculated until it is saved into ctx->attrs, and a corresponding local-var is killed.
Could you please consider it as [0/3 V7] this time?
Thx
Lai.
Quoted 3 lines
Thanks.