Search code examples
linuxlinux-kernelaffinityjitter

Unbound workqueue's kthreads CPU affinity


Is there a way to set CPU affinity for unbound workqueue's kthreads (those that named kthread/uXX:y)? Something like cpu mask for regular workqueues. Is it a good idea to set it for each kthread using taskset?


Solution

  • Workqueue subsystem exports sysfs attribute for setting cpu affinity for unbound workers. Code can be found in Workqueue.c:

        5040 static ssize_t wq_unbound_cpumask_store(struct device *dev,
        5041                 struct device_attribute *attr, const char *buf, size_t count)
        5042 {
        5043         cpumask_var_t cpumask;
        5044         int ret;
        5045 
        5046         if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
        5047                 return -ENOMEM;
        5048 
        5049         ret = cpumask_parse(buf, cpumask);
        5050         if (!ret)
        5051                 ret = workqueue_set_unbound_cpumask(cpumask);
        5052 
        5053         free_cpumask_var(cpumask);
        5054         return ret ? ret : count;
        5055 }
        5056 
        5057 static struct device_attribute wq_sysfs_cpumask_attr =
        5058         __ATTR(cpumask, 0644, wq_unbound_cpumask_show,
        5059                wq_unbound_cpumask_store)
    

    So any user space application can write to sysfs descriptor to set unbound workqueue cpu mask.

    I hope this answers your query.