2010-06-01 09:36:06

by Tejun Heo

[permalink] [raw]
Subject: [PATCH 3/3] vhost: apply cpumask and cgroup to vhost workers

Apply the cpumask and cgroup of the initializing task to the created
vhost worker.

Based on Sridhar Samudrala's patch. Li Zefan spotted a bug in error
path (twice), fixed (twice).

Signed-off-by: Tejun Heo <[email protected]>
Cc: Michael S. Tsirkin <[email protected]>
Cc: Sridhar Samudrala <[email protected]>
Cc: Li Zefan <[email protected]>
---
drivers/vhost/vhost.c | 34 ++++++++++++++++++++++++++++++----
1 file changed, 30 insertions(+), 4 deletions(-)

Index: work/drivers/vhost/vhost.c
===================================================================
--- work.orig/drivers/vhost/vhost.c
+++ work/drivers/vhost/vhost.c
@@ -23,6 +23,7 @@
#include <linux/highmem.h>
#include <linux/slab.h>
#include <linux/kthread.h>
+#include <linux/cgroup.h>

#include <linux/net.h>
#include <linux/if_packet.h>
@@ -187,11 +188,29 @@ long vhost_dev_init(struct vhost_dev *de
struct vhost_virtqueue *vqs, int nvqs)
{
struct task_struct *worker;
- int i;
+ cpumask_var_t mask;
+ int i, ret = -ENOMEM;
+
+ if (!alloc_cpumask_var(&mask, GFP_KERNEL))
+ goto out_free_mask;

worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
- if (IS_ERR(worker))
- return PTR_ERR(worker);
+ if (IS_ERR(worker)) {
+ ret = PTR_ERR(worker);
+ goto out_free_mask;
+ }
+
+ ret = sched_getaffinity(current->pid, mask);
+ if (ret)
+ goto out_stop_worker;
+
+ ret = sched_setaffinity(worker->pid, mask);
+ if (ret)
+ goto out_stop_worker;
+
+ ret = cgroup_attach_task_current_cg(worker);
+ if (ret)
+ goto out_stop_worker;

dev->vqs = vqs;
dev->nvqs = nvqs;
@@ -214,7 +233,14 @@ long vhost_dev_init(struct vhost_dev *de
}

wake_up_process(worker); /* avoid contributing to loadavg */
- return 0;
+ ret = 0;
+ goto out_free_mask;
+
+out_stop_worker:
+ kthread_stop(worker);
+out_free_mask:
+ free_cpumask_var(mask);
+ return ret;
}

/* Caller should have device mutex */


2010-06-01 10:22:03

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: [PATCH 3/3] vhost: apply cpumask and cgroup to vhost workers

On Tue, Jun 01, 2010 at 11:35:15AM +0200, Tejun Heo wrote:
> Apply the cpumask and cgroup of the initializing task to the created
> vhost worker.
>
> Based on Sridhar Samudrala's patch. Li Zefan spotted a bug in error
> path (twice), fixed (twice).
>
> Signed-off-by: Tejun Heo <[email protected]>
> Cc: Michael S. Tsirkin <[email protected]>
> Cc: Sridhar Samudrala <[email protected]>
> Cc: Li Zefan <[email protected]>

Something that I wanted to figure out - what happens if the
CPU mask limits us to a certain CPU that subsequently goes offline?
Will e.g. flush block forever or until that CPU comes back?
Also, does singlethreaded workqueue behave in the same way?

> ---
> drivers/vhost/vhost.c | 34 ++++++++++++++++++++++++++++++----
> 1 file changed, 30 insertions(+), 4 deletions(-)
>
> Index: work/drivers/vhost/vhost.c
> ===================================================================
> --- work.orig/drivers/vhost/vhost.c
> +++ work/drivers/vhost/vhost.c
> @@ -23,6 +23,7 @@
> #include <linux/highmem.h>
> #include <linux/slab.h>
> #include <linux/kthread.h>
> +#include <linux/cgroup.h>
>
> #include <linux/net.h>
> #include <linux/if_packet.h>
> @@ -187,11 +188,29 @@ long vhost_dev_init(struct vhost_dev *de
> struct vhost_virtqueue *vqs, int nvqs)
> {
> struct task_struct *worker;
> - int i;
> + cpumask_var_t mask;
> + int i, ret = -ENOMEM;
> +
> + if (!alloc_cpumask_var(&mask, GFP_KERNEL))
> + goto out_free_mask;
>
> worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
> - if (IS_ERR(worker))
> - return PTR_ERR(worker);
> + if (IS_ERR(worker)) {
> + ret = PTR_ERR(worker);
> + goto out_free_mask;
> + }
> +
> + ret = sched_getaffinity(current->pid, mask);
> + if (ret)
> + goto out_stop_worker;
> +
> + ret = sched_setaffinity(worker->pid, mask);
> + if (ret)
> + goto out_stop_worker;
> +
> + ret = cgroup_attach_task_current_cg(worker);
> + if (ret)
> + goto out_stop_worker;
>
> dev->vqs = vqs;
> dev->nvqs = nvqs;
> @@ -214,7 +233,14 @@ long vhost_dev_init(struct vhost_dev *de
> }
>
> wake_up_process(worker); /* avoid contributing to loadavg */
> - return 0;
> + ret = 0;
> + goto out_free_mask;
> +
> +out_stop_worker:
> + kthread_stop(worker);
> +out_free_mask:
> + free_cpumask_var(mask);
> + return ret;
> }
>
> /* Caller should have device mutex */

2010-06-01 10:57:18

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH 3/3] vhost: apply cpumask and cgroup to vhost workers

Hello,

On 06/01/2010 12:17 PM, Michael S. Tsirkin wrote:
> Something that I wanted to figure out - what happens if the
> CPU mask limits us to a certain CPU that subsequently goes offline?

The thread gets unbound during the last steps of cpu offlining.

> Will e.g. flush block forever or until that CPU comes back?
> Also, does singlethreaded workqueue behave in the same way?

So, things will proceed as usual although the thread will lose its
affinity. Singlethread wqs don't bind their workers (and they
shouldn't! :-). MT ones explicitly manage workers according to cpu
up/down events.

Thanks.

--
tejun

2010-06-01 17:19:24

by Sridhar Samudrala

[permalink] [raw]
Subject: Re: [PATCH 3/3] vhost: apply cpumask and cgroup to vhost workers

On Tue, 2010-06-01 at 11:35 +0200, Tejun Heo wrote:
> Apply the cpumask and cgroup of the initializing task to the created
> vhost worker.
>
> Based on Sridhar Samudrala's patch. Li Zefan spotted a bug in error
> path (twice), fixed (twice).
>
> Signed-off-by: Tejun Heo <[email protected]>
> Cc: Michael S. Tsirkin <[email protected]>
> Cc: Sridhar Samudrala <[email protected]>
> Cc: Li Zefan <[email protected]>
> ---
> drivers/vhost/vhost.c | 34 ++++++++++++++++++++++++++++++----
> 1 file changed, 30 insertions(+), 4 deletions(-)
>
> Index: work/drivers/vhost/vhost.c
> ===================================================================
> --- work.orig/drivers/vhost/vhost.c
> +++ work/drivers/vhost/vhost.c
> @@ -23,6 +23,7 @@
> #include <linux/highmem.h>
> #include <linux/slab.h>
> #include <linux/kthread.h>
> +#include <linux/cgroup.h>
>
> #include <linux/net.h>
> #include <linux/if_packet.h>
> @@ -187,11 +188,29 @@ long vhost_dev_init(struct vhost_dev *de
> struct vhost_virtqueue *vqs, int nvqs)
> {
> struct task_struct *worker;
> - int i;
> + cpumask_var_t mask;
> + int i, ret = -ENOMEM;
> +
> + if (!alloc_cpumask_var(&mask, GFP_KERNEL))
> + goto out_free_mask;

I think this is another bug in the error path. You should simply
do a return instead of a goto here when aloc_cpu_mask fails.

Thanks
Sridhar
>
> worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
> - if (IS_ERR(worker))
> - return PTR_ERR(worker);
> + if (IS_ERR(worker)) {
> + ret = PTR_ERR(worker);
> + goto out_free_mask;
> + }
> +
> + ret = sched_getaffinity(current->pid, mask);
> + if (ret)
> + goto out_stop_worker;
> +
> + ret = sched_setaffinity(worker->pid, mask);
> + if (ret)
> + goto out_stop_worker;
> +
> + ret = cgroup_attach_task_current_cg(worker);
> + if (ret)
> + goto out_stop_worker;
>
> dev->vqs = vqs;
> dev->nvqs = nvqs;
> @@ -214,7 +233,14 @@ long vhost_dev_init(struct vhost_dev *de
> }
>
> wake_up_process(worker); /* avoid contributing to loadavg */
> - return 0;
> + ret = 0;
> + goto out_free_mask;
> +
> +out_stop_worker:
> + kthread_stop(worker);
> +out_free_mask:
> + free_cpumask_var(mask);
> + return ret;
> }
>
> /* Caller should have device mutex */
> --
> To unsubscribe from this list: send the line "unsubscribe netdev" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html

2010-06-02 00:00:50

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH 3/3] vhost: apply cpumask and cgroup to vhost workers

On 06/01/2010 07:19 PM, Sridhar Samudrala wrote:
>> - int i;
>> + cpumask_var_t mask;
>> + int i, ret = -ENOMEM;
>> +
>> + if (!alloc_cpumask_var(&mask, GFP_KERNEL))
>> + goto out_free_mask;
>
> I think this is another bug in the error path. You should simply
> do a return instead of a goto here when aloc_cpu_mask fails.

Oh... it's always safe to call free_cpumask_var() after failed
alloc_cpumask_var(), so that part isn't broken.

Thanks.

--
tejun