Skip to content

Commit 7235acd

Browse files
jasowangmstsirkin
authored andcommitted
vhost: simplify work flushing
We used to implement the work flushing through tracking queued seq, done seq, and the number of flushing. This patch simplify this by just implement work flushing through another kind of vhost work with completion. This will be used by lockless enqueuing patch. Signed-off-by: Jason Wang <jasowang@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 523d939 commit 7235acd

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

drivers/vhost/vhost.c

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ static void vhost_reset_is_le(struct vhost_virtqueue *vq)
131131
vq->is_le = virtio_legacy_is_little_endian();
132132
}
133133

134+
struct vhost_flush_struct {
135+
struct vhost_work work;
136+
struct completion wait_event;
137+
};
138+
139+
static void vhost_flush_work(struct vhost_work *work)
140+
{
141+
struct vhost_flush_struct *s;
142+
143+
s = container_of(work, struct vhost_flush_struct, work);
144+
complete(&s->wait_event);
145+
}
146+
134147
static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
135148
poll_table *pt)
136149
{
@@ -158,8 +171,6 @@ void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
158171
INIT_LIST_HEAD(&work->node);
159172
work->fn = fn;
160173
init_waitqueue_head(&work->done);
161-
work->flushing = 0;
162-
work->queue_seq = work->done_seq = 0;
163174
}
164175
EXPORT_SYMBOL_GPL(vhost_work_init);
165176

@@ -211,31 +222,17 @@ void vhost_poll_stop(struct vhost_poll *poll)
211222
}
212223
EXPORT_SYMBOL_GPL(vhost_poll_stop);
213224

214-
static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
215-
unsigned seq)
216-
{
217-
int left;
218-
219-
spin_lock_irq(&dev->work_lock);
220-
left = seq - work->done_seq;
221-
spin_unlock_irq(&dev->work_lock);
222-
return left <= 0;
223-
}
224-
225225
void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
226226
{
227-
unsigned seq;
228-
int flushing;
227+
struct vhost_flush_struct flush;
228+
229+
if (dev->worker) {
230+
init_completion(&flush.wait_event);
231+
vhost_work_init(&flush.work, vhost_flush_work);
229232

230-
spin_lock_irq(&dev->work_lock);
231-
seq = work->queue_seq;
232-
work->flushing++;
233-
spin_unlock_irq(&dev->work_lock);
234-
wait_event(work->done, vhost_work_seq_done(dev, work, seq));
235-
spin_lock_irq(&dev->work_lock);
236-
flushing = --work->flushing;
237-
spin_unlock_irq(&dev->work_lock);
238-
BUG_ON(flushing < 0);
233+
vhost_work_queue(dev, &flush.work);
234+
wait_for_completion(&flush.wait_event);
235+
}
239236
}
240237
EXPORT_SYMBOL_GPL(vhost_work_flush);
241238

@@ -254,7 +251,6 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
254251
spin_lock_irqsave(&dev->work_lock, flags);
255252
if (list_empty(&work->node)) {
256253
list_add_tail(&work->node, &dev->work_list);
257-
work->queue_seq++;
258254
spin_unlock_irqrestore(&dev->work_lock, flags);
259255
wake_up_process(dev->worker);
260256
} else {
@@ -310,7 +306,6 @@ static int vhost_worker(void *data)
310306
{
311307
struct vhost_dev *dev = data;
312308
struct vhost_work *work = NULL;
313-
unsigned uninitialized_var(seq);
314309
mm_segment_t oldfs = get_fs();
315310

316311
set_fs(USER_DS);
@@ -321,11 +316,6 @@ static int vhost_worker(void *data)
321316
set_current_state(TASK_INTERRUPTIBLE);
322317

323318
spin_lock_irq(&dev->work_lock);
324-
if (work) {
325-
work->done_seq = seq;
326-
if (work->flushing)
327-
wake_up_all(&work->done);
328-
}
329319

330320
if (kthread_should_stop()) {
331321
spin_unlock_irq(&dev->work_lock);
@@ -336,7 +326,6 @@ static int vhost_worker(void *data)
336326
work = list_first_entry(&dev->work_list,
337327
struct vhost_work, node);
338328
list_del_init(&work->node);
339-
seq = work->queue_seq;
340329
} else
341330
work = NULL;
342331
spin_unlock_irq(&dev->work_lock);

0 commit comments

Comments
 (0)