Skip to content

Commit c23f344

Browse files
htejunmstsirkin
authored andcommitted
vhost: replace vhost_workqueue with per-vhost kthread
Replace vhost_workqueue with per-vhost kthread. Other than callback argument change from struct work_struct * to struct vhost_work *, there's no visible change to vhost_poll_*() interface. This conversion is to make each vhost use a dedicated kthread so that resource control via cgroup can be applied. Partially based on Sridhar Samudrala's patch. * Updated to use sub structure vhost_work instead of directly using vhost_poll at Michael's suggestion. * Added flusher wake_up() optimization at Michael's suggestion. Changes by MST: * Converted atomics/barrier use to a spinlock. * Create thread on SET_OWNER * Fix flushing Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Cc: Sridhar Samudrala <samudrala.sridhar@gmail.com>
1 parent 4cfa580 commit c23f344

3 files changed

Lines changed: 164 additions & 73 deletions

File tree

‎drivers/vhost/net.c‎

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -302,54 +302,58 @@ static void handle_rx(struct vhost_net *net)
302302
unuse_mm(net->dev.mm);
303303
}
304304

305-
static void handle_tx_kick(struct work_struct *work)
305+
static void handle_tx_kick(struct vhost_work *work)
306306
{
307-
struct vhost_virtqueue *vq;
308-
struct vhost_net *net;
309-
vq = container_of(work, struct vhost_virtqueue, poll.work);
310-
net = container_of(vq->dev, struct vhost_net, dev);
307+
struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
308+
poll.work);
309+
struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
310+
311311
handle_tx(net);
312312
}
313313

314-
static void handle_rx_kick(struct work_struct *work)
314+
static void handle_rx_kick(struct vhost_work *work)
315315
{
316-
struct vhost_virtqueue *vq;
317-
struct vhost_net *net;
318-
vq = container_of(work, struct vhost_virtqueue, poll.work);
319-
net = container_of(vq->dev, struct vhost_net, dev);
316+
struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
317+
poll.work);
318+
struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
319+
320320
handle_rx(net);
321321
}
322322

323-
static void handle_tx_net(struct work_struct *work)
323+
static void handle_tx_net(struct vhost_work *work)
324324
{
325-
struct vhost_net *net;
326-
net = container_of(work, struct vhost_net, poll[VHOST_NET_VQ_TX].work);
325+
struct vhost_net *net = container_of(work, struct vhost_net,
326+
poll[VHOST_NET_VQ_TX].work);
327327
handle_tx(net);
328328
}
329329

330-
static void handle_rx_net(struct work_struct *work)
330+
static void handle_rx_net(struct vhost_work *work)
331331
{
332-
struct vhost_net *net;
333-
net = container_of(work, struct vhost_net, poll[VHOST_NET_VQ_RX].work);
332+
struct vhost_net *net = container_of(work, struct vhost_net,
333+
poll[VHOST_NET_VQ_RX].work);
334334
handle_rx(net);
335335
}
336336

337337
static int vhost_net_open(struct inode *inode, struct file *f)
338338
{
339339
struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
340+
struct vhost_dev *dev;
340341
int r;
342+
341343
if (!n)
342344
return -ENOMEM;
345+
346+
dev = &n->dev;
343347
n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
344348
n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
345-
r = vhost_dev_init(&n->dev, n->vqs, VHOST_NET_VQ_MAX);
349+
r = vhost_dev_init(dev, n->vqs, VHOST_NET_VQ_MAX);
346350
if (r < 0) {
347351
kfree(n);
348352
return r;
349353
}
350354

351-
vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT);
352-
vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN);
355+
vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
356+
vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
353357
n->tx_poll_state = VHOST_NET_POLL_DISABLED;
354358

355359
f->private_data = n;
@@ -656,25 +660,13 @@ static struct miscdevice vhost_net_misc = {
656660

657661
static int vhost_net_init(void)
658662
{
659-
int r = vhost_init();
660-
if (r)
661-
goto err_init;
662-
r = misc_register(&vhost_net_misc);
663-
if (r)
664-
goto err_reg;
665-
return 0;
666-
err_reg:
667-
vhost_cleanup();
668-
err_init:
669-
return r;
670-
663+
return misc_register(&vhost_net_misc);
671664
}
672665
module_init(vhost_net_init);
673666

674667
static void vhost_net_exit(void)
675668
{
676669
misc_deregister(&vhost_net_misc);
677-
vhost_cleanup();
678670
}
679671
module_exit(vhost_net_exit);
680672

‎drivers/vhost/vhost.c‎

Lines changed: 115 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
#include <linux/mm.h>
1818
#include <linux/miscdevice.h>
1919
#include <linux/mutex.h>
20-
#include <linux/workqueue.h>
2120
#include <linux/rcupdate.h>
2221
#include <linux/poll.h>
2322
#include <linux/file.h>
2423
#include <linux/highmem.h>
2524
#include <linux/slab.h>
25+
#include <linux/kthread.h>
2626

2727
#include <linux/net.h>
2828
#include <linux/if_packet.h>
@@ -37,8 +37,6 @@ enum {
3737
VHOST_MEMORY_F_LOG = 0x1,
3838
};
3939

40-
static struct workqueue_struct *vhost_workqueue;
41-
4240
static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
4341
poll_table *pt)
4442
{
@@ -52,23 +50,31 @@ static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
5250
static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
5351
void *key)
5452
{
55-
struct vhost_poll *poll;
56-
poll = container_of(wait, struct vhost_poll, wait);
53+
struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
54+
5755
if (!((unsigned long)key & poll->mask))
5856
return 0;
5957

60-
queue_work(vhost_workqueue, &poll->work);
58+
vhost_poll_queue(poll);
6159
return 0;
6260
}
6361

6462
/* Init poll structure */
65-
void vhost_poll_init(struct vhost_poll *poll, work_func_t func,
66-
unsigned long mask)
63+
void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
64+
unsigned long mask, struct vhost_dev *dev)
6765
{
68-
INIT_WORK(&poll->work, func);
66+
struct vhost_work *work = &poll->work;
67+
6968
init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
7069
init_poll_funcptr(&poll->table, vhost_poll_func);
7170
poll->mask = mask;
71+
poll->dev = dev;
72+
73+
INIT_LIST_HEAD(&work->node);
74+
work->fn = fn;
75+
init_waitqueue_head(&work->done);
76+
work->flushing = 0;
77+
work->queue_seq = work->done_seq = 0;
7278
}
7379

7480
/* Start polling a file. We add ourselves to file's wait queue. The caller must
@@ -92,12 +98,40 @@ void vhost_poll_stop(struct vhost_poll *poll)
9298
* locks that are also used by the callback. */
9399
void vhost_poll_flush(struct vhost_poll *poll)
94100
{
95-
flush_work(&poll->work);
101+
struct vhost_work *work = &poll->work;
102+
unsigned seq;
103+
int left;
104+
int flushing;
105+
106+
spin_lock_irq(&poll->dev->work_lock);
107+
seq = work->queue_seq;
108+
work->flushing++;
109+
spin_unlock_irq(&poll->dev->work_lock);
110+
wait_event(work->done, ({
111+
spin_lock_irq(&poll->dev->work_lock);
112+
left = seq - work->done_seq <= 0;
113+
spin_unlock_irq(&poll->dev->work_lock);
114+
left;
115+
}));
116+
spin_lock_irq(&poll->dev->work_lock);
117+
flushing = --work->flushing;
118+
spin_unlock_irq(&poll->dev->work_lock);
119+
BUG_ON(flushing < 0);
96120
}
97121

98122
void vhost_poll_queue(struct vhost_poll *poll)
99123
{
100-
queue_work(vhost_workqueue, &poll->work);
124+
struct vhost_dev *dev = poll->dev;
125+
struct vhost_work *work = &poll->work;
126+
unsigned long flags;
127+
128+
spin_lock_irqsave(&dev->work_lock, flags);
129+
if (list_empty(&work->node)) {
130+
list_add_tail(&work->node, &dev->work_list);
131+
work->queue_seq++;
132+
wake_up_process(dev->worker);
133+
}
134+
spin_unlock_irqrestore(&dev->work_lock, flags);
101135
}
102136

103137
static void vhost_vq_reset(struct vhost_dev *dev,
@@ -125,27 +159,71 @@ static void vhost_vq_reset(struct vhost_dev *dev,
125159
vq->log_ctx = NULL;
126160
}
127161

162+
static int vhost_worker(void *data)
163+
{
164+
struct vhost_dev *dev = data;
165+
struct vhost_work *work = NULL;
166+
unsigned uninitialized_var(seq);
167+
168+
for (;;) {
169+
/* mb paired w/ kthread_stop */
170+
set_current_state(TASK_INTERRUPTIBLE);
171+
172+
spin_lock_irq(&dev->work_lock);
173+
if (work) {
174+
work->done_seq = seq;
175+
if (work->flushing)
176+
wake_up_all(&work->done);
177+
}
178+
179+
if (kthread_should_stop()) {
180+
spin_unlock_irq(&dev->work_lock);
181+
__set_current_state(TASK_RUNNING);
182+
return 0;
183+
}
184+
if (!list_empty(&dev->work_list)) {
185+
work = list_first_entry(&dev->work_list,
186+
struct vhost_work, node);
187+
list_del_init(&work->node);
188+
seq = work->queue_seq;
189+
} else
190+
work = NULL;
191+
spin_unlock_irq(&dev->work_lock);
192+
193+
if (work) {
194+
__set_current_state(TASK_RUNNING);
195+
work->fn(work);
196+
} else
197+
schedule();
198+
199+
}
200+
}
201+
128202
long vhost_dev_init(struct vhost_dev *dev,
129203
struct vhost_virtqueue *vqs, int nvqs)
130204
{
131205
int i;
206+
132207
dev->vqs = vqs;
133208
dev->nvqs = nvqs;
134209
mutex_init(&dev->mutex);
135210
dev->log_ctx = NULL;
136211
dev->log_file = NULL;
137212
dev->memory = NULL;
138213
dev->mm = NULL;
214+
spin_lock_init(&dev->work_lock);
215+
INIT_LIST_HEAD(&dev->work_list);
216+
dev->worker = NULL;
139217

140218
for (i = 0; i < dev->nvqs; ++i) {
141219
dev->vqs[i].dev = dev;
142220
mutex_init(&dev->vqs[i].mutex);
143221
vhost_vq_reset(dev, dev->vqs + i);
144222
if (dev->vqs[i].handle_kick)
145223
vhost_poll_init(&dev->vqs[i].poll,
146-
dev->vqs[i].handle_kick,
147-
POLLIN);
224+
dev->vqs[i].handle_kick, POLLIN, dev);
148225
}
226+
149227
return 0;
150228
}
151229

@@ -159,12 +237,31 @@ long vhost_dev_check_owner(struct vhost_dev *dev)
159237
/* Caller should have device mutex */
160238
static long vhost_dev_set_owner(struct vhost_dev *dev)
161239
{
240+
struct task_struct *worker;
241+
int err;
162242
/* Is there an owner already? */
163-
if (dev->mm)
164-
return -EBUSY;
243+
if (dev->mm) {
244+
err = -EBUSY;
245+
goto err_mm;
246+
}
165247
/* No owner, become one */
166248
dev->mm = get_task_mm(current);
249+
worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
250+
if (IS_ERR(worker)) {
251+
err = PTR_ERR(worker);
252+
goto err_worker;
253+
}
254+
255+
dev->worker = worker;
256+
wake_up_process(worker); /* avoid contributing to loadavg */
257+
167258
return 0;
259+
err_worker:
260+
if (dev->mm)
261+
mmput(dev->mm);
262+
dev->mm = NULL;
263+
err_mm:
264+
return err;
168265
}
169266

170267
/* Caller should have device mutex */
@@ -217,6 +314,9 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
217314
if (dev->mm)
218315
mmput(dev->mm);
219316
dev->mm = NULL;
317+
318+
WARN_ON(!list_empty(&dev->work_list));
319+
kthread_stop(dev->worker);
220320
}
221321

222322
static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
@@ -1115,16 +1215,3 @@ void vhost_disable_notify(struct vhost_virtqueue *vq)
11151215
vq_err(vq, "Failed to enable notification at %p: %d\n",
11161216
&vq->used->flags, r);
11171217
}
1118-
1119-
int vhost_init(void)
1120-
{
1121-
vhost_workqueue = create_singlethread_workqueue("vhost");
1122-
if (!vhost_workqueue)
1123-
return -ENOMEM;
1124-
return 0;
1125-
}
1126-
1127-
void vhost_cleanup(void)
1128-
{
1129-
destroy_workqueue(vhost_workqueue);
1130-
}

0 commit comments

Comments
 (0)