1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * virtio transport for vsock
4 *
5 * Copyright (C) 2013-2015 Red Hat, Inc.
6 * Author: Asias He <asias@redhat.com>
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
9 * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
10 * early virtio-vsock proof-of-concept bits.
11 */
12 #include <linux/spinlock.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/atomic.h>
16 #include <linux/virtio.h>
17 #include <linux/virtio_ids.h>
18 #include <linux/virtio_config.h>
19 #include <linux/virtio_vsock.h>
20 #include <net/sock.h>
21 #include <linux/mutex.h>
22 #include <net/af_vsock.h>
23
24 static struct workqueue_struct *virtio_vsock_workqueue;
25 static struct virtio_vsock __rcu *the_virtio_vsock;
26 static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */
27
28 struct virtio_vsock {
29 struct virtio_device *vdev;
30 struct virtqueue *vqs[VSOCK_VQ_MAX];
31
32 /* Virtqueue processing is deferred to a workqueue */
33 struct work_struct tx_work;
34 struct work_struct rx_work;
35 struct work_struct event_work;
36
37 /* The following fields are protected by tx_lock. vqs[VSOCK_VQ_TX]
38 * must be accessed with tx_lock held.
39 */
40 struct mutex tx_lock;
41 bool tx_run;
42
43 struct work_struct send_pkt_work;
44 spinlock_t send_pkt_list_lock;
45 struct list_head send_pkt_list;
46
47 atomic_t queued_replies;
48
49 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
50 * must be accessed with rx_lock held.
51 */
52 struct mutex rx_lock;
53 bool rx_run;
54 int rx_buf_nr;
55 int rx_buf_max_nr;
56
57 /* The following fields are protected by event_lock.
58 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
59 */
60 struct mutex event_lock;
61 bool event_run;
62 struct virtio_vsock_event event_list[8];
63
64 u32 guest_cid;
65 };
66
virtio_transport_get_local_cid(void)67 static u32 virtio_transport_get_local_cid(void)
68 {
69 struct virtio_vsock *vsock;
70 u32 ret;
71
72 rcu_read_lock();
73 vsock = rcu_dereference(the_virtio_vsock);
74 if (!vsock) {
75 ret = VMADDR_CID_ANY;
76 goto out_rcu;
77 }
78
79 ret = vsock->guest_cid;
80 out_rcu:
81 rcu_read_unlock();
82 return ret;
83 }
84
85 static void
virtio_transport_send_pkt_work(struct work_struct * work)86 virtio_transport_send_pkt_work(struct work_struct *work)
87 {
88 struct virtio_vsock *vsock =
89 container_of(work, struct virtio_vsock, send_pkt_work);
90 struct virtqueue *vq;
91 bool added = false;
92 bool restart_rx = false;
93
94 mutex_lock(&vsock->tx_lock);
95
96 if (!vsock->tx_run)
97 goto out;
98
99 vq = vsock->vqs[VSOCK_VQ_TX];
100
101 for (;;) {
102 struct virtio_vsock_pkt *pkt;
103 struct scatterlist hdr, buf, *sgs[2];
104 int ret, in_sg = 0, out_sg = 0;
105 bool reply;
106
107 spin_lock_bh(&vsock->send_pkt_list_lock);
108 if (list_empty(&vsock->send_pkt_list)) {
109 spin_unlock_bh(&vsock->send_pkt_list_lock);
110 break;
111 }
112
113 pkt = list_first_entry(&vsock->send_pkt_list,
114 struct virtio_vsock_pkt, list);
115 list_del_init(&pkt->list);
116 spin_unlock_bh(&vsock->send_pkt_list_lock);
117
118 virtio_transport_deliver_tap_pkt(pkt);
119
120 reply = pkt->reply;
121
122 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
123 sgs[out_sg++] = &hdr;
124 if (pkt->buf) {
125 sg_init_one(&buf, pkt->buf, pkt->len);
126 sgs[out_sg++] = &buf;
127 }
128
129 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
130 /* Usually this means that there is no more space available in
131 * the vq
132 */
133 if (ret < 0) {
134 spin_lock_bh(&vsock->send_pkt_list_lock);
135 list_add(&pkt->list, &vsock->send_pkt_list);
136 spin_unlock_bh(&vsock->send_pkt_list_lock);
137 break;
138 }
139
140 if (reply) {
141 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
142 int val;
143
144 val = atomic_dec_return(&vsock->queued_replies);
145
146 /* Do we now have resources to resume rx processing? */
147 if (val + 1 == virtqueue_get_vring_size(rx_vq))
148 restart_rx = true;
149 }
150
151 added = true;
152 }
153
154 if (added)
155 virtqueue_kick(vq);
156
157 out:
158 mutex_unlock(&vsock->tx_lock);
159
160 if (restart_rx)
161 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
162 }
163
164 static int
virtio_transport_send_pkt(struct virtio_vsock_pkt * pkt)165 virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
166 {
167 struct virtio_vsock *vsock;
168 int len = pkt->len;
169
170 rcu_read_lock();
171 vsock = rcu_dereference(the_virtio_vsock);
172 if (!vsock) {
173 virtio_transport_free_pkt(pkt);
174 len = -ENODEV;
175 goto out_rcu;
176 }
177
178 if (le64_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid) {
179 virtio_transport_free_pkt(pkt);
180 len = -ENODEV;
181 goto out_rcu;
182 }
183
184 if (pkt->reply)
185 atomic_inc(&vsock->queued_replies);
186
187 spin_lock_bh(&vsock->send_pkt_list_lock);
188 list_add_tail(&pkt->list, &vsock->send_pkt_list);
189 spin_unlock_bh(&vsock->send_pkt_list_lock);
190
191 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
192
193 out_rcu:
194 rcu_read_unlock();
195 return len;
196 }
197
198 static int
virtio_transport_cancel_pkt(struct vsock_sock * vsk)199 virtio_transport_cancel_pkt(struct vsock_sock *vsk)
200 {
201 struct virtio_vsock *vsock;
202 struct virtio_vsock_pkt *pkt, *n;
203 int cnt = 0, ret;
204 LIST_HEAD(freeme);
205
206 rcu_read_lock();
207 vsock = rcu_dereference(the_virtio_vsock);
208 if (!vsock) {
209 ret = -ENODEV;
210 goto out_rcu;
211 }
212
213 spin_lock_bh(&vsock->send_pkt_list_lock);
214 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
215 if (pkt->vsk != vsk)
216 continue;
217 list_move(&pkt->list, &freeme);
218 }
219 spin_unlock_bh(&vsock->send_pkt_list_lock);
220
221 list_for_each_entry_safe(pkt, n, &freeme, list) {
222 if (pkt->reply)
223 cnt++;
224 list_del(&pkt->list);
225 virtio_transport_free_pkt(pkt);
226 }
227
228 if (cnt) {
229 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
230 int new_cnt;
231
232 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
233 if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
234 new_cnt < virtqueue_get_vring_size(rx_vq))
235 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
236 }
237
238 ret = 0;
239
240 out_rcu:
241 rcu_read_unlock();
242 return ret;
243 }
244
virtio_vsock_rx_fill(struct virtio_vsock * vsock)245 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
246 {
247 int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
248 struct virtio_vsock_pkt *pkt;
249 struct scatterlist hdr, buf, *sgs[2];
250 struct virtqueue *vq;
251 int ret;
252
253 vq = vsock->vqs[VSOCK_VQ_RX];
254
255 do {
256 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
257 if (!pkt)
258 break;
259
260 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
261 if (!pkt->buf) {
262 virtio_transport_free_pkt(pkt);
263 break;
264 }
265
266 pkt->buf_len = buf_len;
267 pkt->len = buf_len;
268
269 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
270 sgs[0] = &hdr;
271
272 sg_init_one(&buf, pkt->buf, buf_len);
273 sgs[1] = &buf;
274 ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
275 if (ret) {
276 virtio_transport_free_pkt(pkt);
277 break;
278 }
279 vsock->rx_buf_nr++;
280 } while (vq->num_free);
281 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
282 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
283 virtqueue_kick(vq);
284 }
285
virtio_transport_tx_work(struct work_struct * work)286 static void virtio_transport_tx_work(struct work_struct *work)
287 {
288 struct virtio_vsock *vsock =
289 container_of(work, struct virtio_vsock, tx_work);
290 struct virtqueue *vq;
291 bool added = false;
292
293 vq = vsock->vqs[VSOCK_VQ_TX];
294 mutex_lock(&vsock->tx_lock);
295
296 if (!vsock->tx_run)
297 goto out;
298
299 do {
300 struct virtio_vsock_pkt *pkt;
301 unsigned int len;
302
303 virtqueue_disable_cb(vq);
304 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
305 virtio_transport_free_pkt(pkt);
306 added = true;
307 }
308 } while (!virtqueue_enable_cb(vq));
309
310 out:
311 mutex_unlock(&vsock->tx_lock);
312
313 if (added)
314 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
315 }
316
317 /* Is there space left for replies to rx packets? */
virtio_transport_more_replies(struct virtio_vsock * vsock)318 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
319 {
320 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
321 int val;
322
323 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
324 val = atomic_read(&vsock->queued_replies);
325
326 return val < virtqueue_get_vring_size(vq);
327 }
328
329 /* event_lock must be held */
virtio_vsock_event_fill_one(struct virtio_vsock * vsock,struct virtio_vsock_event * event)330 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
331 struct virtio_vsock_event *event)
332 {
333 struct scatterlist sg;
334 struct virtqueue *vq;
335
336 vq = vsock->vqs[VSOCK_VQ_EVENT];
337
338 sg_init_one(&sg, event, sizeof(*event));
339
340 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
341 }
342
343 /* event_lock must be held */
virtio_vsock_event_fill(struct virtio_vsock * vsock)344 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
345 {
346 size_t i;
347
348 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
349 struct virtio_vsock_event *event = &vsock->event_list[i];
350
351 virtio_vsock_event_fill_one(vsock, event);
352 }
353
354 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
355 }
356
virtio_vsock_reset_sock(struct sock * sk)357 static void virtio_vsock_reset_sock(struct sock *sk)
358 {
359 /* vmci_transport.c doesn't take sk_lock here either. At least we're
360 * under vsock_table_lock so the sock cannot disappear while we're
361 * executing.
362 */
363
364 sk->sk_state = TCP_CLOSE;
365 sk->sk_err = ECONNRESET;
366 sk->sk_error_report(sk);
367 }
368
virtio_vsock_update_guest_cid(struct virtio_vsock * vsock)369 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
370 {
371 struct virtio_device *vdev = vsock->vdev;
372 __le64 guest_cid;
373
374 vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
375 &guest_cid, sizeof(guest_cid));
376 vsock->guest_cid = le64_to_cpu(guest_cid);
377 }
378
379 /* event_lock must be held */
virtio_vsock_event_handle(struct virtio_vsock * vsock,struct virtio_vsock_event * event)380 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
381 struct virtio_vsock_event *event)
382 {
383 switch (le32_to_cpu(event->id)) {
384 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
385 virtio_vsock_update_guest_cid(vsock);
386 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
387 break;
388 }
389 }
390
virtio_transport_event_work(struct work_struct * work)391 static void virtio_transport_event_work(struct work_struct *work)
392 {
393 struct virtio_vsock *vsock =
394 container_of(work, struct virtio_vsock, event_work);
395 struct virtqueue *vq;
396
397 vq = vsock->vqs[VSOCK_VQ_EVENT];
398
399 mutex_lock(&vsock->event_lock);
400
401 if (!vsock->event_run)
402 goto out;
403
404 do {
405 struct virtio_vsock_event *event;
406 unsigned int len;
407
408 virtqueue_disable_cb(vq);
409 while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
410 if (len == sizeof(*event))
411 virtio_vsock_event_handle(vsock, event);
412
413 virtio_vsock_event_fill_one(vsock, event);
414 }
415 } while (!virtqueue_enable_cb(vq));
416
417 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
418 out:
419 mutex_unlock(&vsock->event_lock);
420 }
421
virtio_vsock_event_done(struct virtqueue * vq)422 static void virtio_vsock_event_done(struct virtqueue *vq)
423 {
424 struct virtio_vsock *vsock = vq->vdev->priv;
425
426 if (!vsock)
427 return;
428 queue_work(virtio_vsock_workqueue, &vsock->event_work);
429 }
430
virtio_vsock_tx_done(struct virtqueue * vq)431 static void virtio_vsock_tx_done(struct virtqueue *vq)
432 {
433 struct virtio_vsock *vsock = vq->vdev->priv;
434
435 if (!vsock)
436 return;
437 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
438 }
439
virtio_vsock_rx_done(struct virtqueue * vq)440 static void virtio_vsock_rx_done(struct virtqueue *vq)
441 {
442 struct virtio_vsock *vsock = vq->vdev->priv;
443
444 if (!vsock)
445 return;
446 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
447 }
448
449 static struct virtio_transport virtio_transport = {
450 .transport = {
451 .module = THIS_MODULE,
452
453 .get_local_cid = virtio_transport_get_local_cid,
454
455 .init = virtio_transport_do_socket_init,
456 .destruct = virtio_transport_destruct,
457 .release = virtio_transport_release,
458 .connect = virtio_transport_connect,
459 .shutdown = virtio_transport_shutdown,
460 .cancel_pkt = virtio_transport_cancel_pkt,
461
462 .dgram_bind = virtio_transport_dgram_bind,
463 .dgram_dequeue = virtio_transport_dgram_dequeue,
464 .dgram_enqueue = virtio_transport_dgram_enqueue,
465 .dgram_allow = virtio_transport_dgram_allow,
466
467 .stream_dequeue = virtio_transport_stream_dequeue,
468 .stream_enqueue = virtio_transport_stream_enqueue,
469 .stream_has_data = virtio_transport_stream_has_data,
470 .stream_has_space = virtio_transport_stream_has_space,
471 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
472 .stream_is_active = virtio_transport_stream_is_active,
473 .stream_allow = virtio_transport_stream_allow,
474
475 .notify_poll_in = virtio_transport_notify_poll_in,
476 .notify_poll_out = virtio_transport_notify_poll_out,
477 .notify_recv_init = virtio_transport_notify_recv_init,
478 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
479 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
480 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
481 .notify_send_init = virtio_transport_notify_send_init,
482 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
483 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
484 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
485 .notify_buffer_size = virtio_transport_notify_buffer_size,
486 },
487
488 .send_pkt = virtio_transport_send_pkt,
489 };
490
virtio_transport_rx_work(struct work_struct * work)491 static void virtio_transport_rx_work(struct work_struct *work)
492 {
493 struct virtio_vsock *vsock =
494 container_of(work, struct virtio_vsock, rx_work);
495 struct virtqueue *vq;
496
497 vq = vsock->vqs[VSOCK_VQ_RX];
498
499 mutex_lock(&vsock->rx_lock);
500
501 if (!vsock->rx_run)
502 goto out;
503
504 do {
505 virtqueue_disable_cb(vq);
506 for (;;) {
507 struct virtio_vsock_pkt *pkt;
508 unsigned int len;
509
510 if (!virtio_transport_more_replies(vsock)) {
511 /* Stop rx until the device processes already
512 * pending replies. Leave rx virtqueue
513 * callbacks disabled.
514 */
515 goto out;
516 }
517
518 pkt = virtqueue_get_buf(vq, &len);
519 if (!pkt) {
520 break;
521 }
522
523 vsock->rx_buf_nr--;
524
525 /* Drop short/long packets */
526 if (unlikely(len < sizeof(pkt->hdr) ||
527 len > sizeof(pkt->hdr) + pkt->len)) {
528 virtio_transport_free_pkt(pkt);
529 continue;
530 }
531
532 pkt->len = len - sizeof(pkt->hdr);
533 virtio_transport_deliver_tap_pkt(pkt);
534 virtio_transport_recv_pkt(&virtio_transport, pkt);
535 }
536 } while (!virtqueue_enable_cb(vq));
537
538 out:
539 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
540 virtio_vsock_rx_fill(vsock);
541 mutex_unlock(&vsock->rx_lock);
542 }
543
virtio_vsock_probe(struct virtio_device * vdev)544 static int virtio_vsock_probe(struct virtio_device *vdev)
545 {
546 vq_callback_t *callbacks[] = {
547 virtio_vsock_rx_done,
548 virtio_vsock_tx_done,
549 virtio_vsock_event_done,
550 };
551 static const char * const names[] = {
552 "rx",
553 "tx",
554 "event",
555 };
556 struct virtio_vsock *vsock = NULL;
557 int ret;
558
559 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
560 if (ret)
561 return ret;
562
563 /* Only one virtio-vsock device per guest is supported */
564 if (rcu_dereference_protected(the_virtio_vsock,
565 lockdep_is_held(&the_virtio_vsock_mutex))) {
566 ret = -EBUSY;
567 goto out;
568 }
569
570 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
571 if (!vsock) {
572 ret = -ENOMEM;
573 goto out;
574 }
575
576 vsock->vdev = vdev;
577
578 ret = virtio_find_vqs(vsock->vdev, VSOCK_VQ_MAX,
579 vsock->vqs, callbacks, names,
580 NULL);
581 if (ret < 0)
582 goto out;
583
584 virtio_vsock_update_guest_cid(vsock);
585
586 vsock->rx_buf_nr = 0;
587 vsock->rx_buf_max_nr = 0;
588 atomic_set(&vsock->queued_replies, 0);
589
590 mutex_init(&vsock->tx_lock);
591 mutex_init(&vsock->rx_lock);
592 mutex_init(&vsock->event_lock);
593 spin_lock_init(&vsock->send_pkt_list_lock);
594 INIT_LIST_HEAD(&vsock->send_pkt_list);
595 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
596 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
597 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
598 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
599
600 mutex_lock(&vsock->tx_lock);
601 vsock->tx_run = true;
602 mutex_unlock(&vsock->tx_lock);
603
604 mutex_lock(&vsock->rx_lock);
605 virtio_vsock_rx_fill(vsock);
606 vsock->rx_run = true;
607 mutex_unlock(&vsock->rx_lock);
608
609 mutex_lock(&vsock->event_lock);
610 virtio_vsock_event_fill(vsock);
611 vsock->event_run = true;
612 mutex_unlock(&vsock->event_lock);
613
614 vdev->priv = vsock;
615 rcu_assign_pointer(the_virtio_vsock, vsock);
616
617 mutex_unlock(&the_virtio_vsock_mutex);
618 return 0;
619
620 out:
621 kfree(vsock);
622 mutex_unlock(&the_virtio_vsock_mutex);
623 return ret;
624 }
625
virtio_vsock_remove(struct virtio_device * vdev)626 static void virtio_vsock_remove(struct virtio_device *vdev)
627 {
628 struct virtio_vsock *vsock = vdev->priv;
629 struct virtio_vsock_pkt *pkt;
630
631 mutex_lock(&the_virtio_vsock_mutex);
632
633 vdev->priv = NULL;
634 rcu_assign_pointer(the_virtio_vsock, NULL);
635 synchronize_rcu();
636
637 /* Reset all connected sockets when the device disappear */
638 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
639
640 /* Stop all work handlers to make sure no one is accessing the device,
641 * so we can safely call vdev->config->reset().
642 */
643 mutex_lock(&vsock->rx_lock);
644 vsock->rx_run = false;
645 mutex_unlock(&vsock->rx_lock);
646
647 mutex_lock(&vsock->tx_lock);
648 vsock->tx_run = false;
649 mutex_unlock(&vsock->tx_lock);
650
651 mutex_lock(&vsock->event_lock);
652 vsock->event_run = false;
653 mutex_unlock(&vsock->event_lock);
654
655 /* Flush all device writes and interrupts, device will not use any
656 * more buffers.
657 */
658 vdev->config->reset(vdev);
659
660 mutex_lock(&vsock->rx_lock);
661 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
662 virtio_transport_free_pkt(pkt);
663 mutex_unlock(&vsock->rx_lock);
664
665 mutex_lock(&vsock->tx_lock);
666 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
667 virtio_transport_free_pkt(pkt);
668 mutex_unlock(&vsock->tx_lock);
669
670 spin_lock_bh(&vsock->send_pkt_list_lock);
671 while (!list_empty(&vsock->send_pkt_list)) {
672 pkt = list_first_entry(&vsock->send_pkt_list,
673 struct virtio_vsock_pkt, list);
674 list_del(&pkt->list);
675 virtio_transport_free_pkt(pkt);
676 }
677 spin_unlock_bh(&vsock->send_pkt_list_lock);
678
679 /* Delete virtqueues and flush outstanding callbacks if any */
680 vdev->config->del_vqs(vdev);
681
682 /* Other works can be queued before 'config->del_vqs()', so we flush
683 * all works before to free the vsock object to avoid use after free.
684 */
685 flush_work(&vsock->rx_work);
686 flush_work(&vsock->tx_work);
687 flush_work(&vsock->event_work);
688 flush_work(&vsock->send_pkt_work);
689
690 mutex_unlock(&the_virtio_vsock_mutex);
691
692 kfree(vsock);
693 }
694
695 static struct virtio_device_id id_table[] = {
696 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
697 { 0 },
698 };
699
700 static unsigned int features[] = {
701 };
702
703 static struct virtio_driver virtio_vsock_driver = {
704 .feature_table = features,
705 .feature_table_size = ARRAY_SIZE(features),
706 .driver.name = KBUILD_MODNAME,
707 .driver.owner = THIS_MODULE,
708 .id_table = id_table,
709 .probe = virtio_vsock_probe,
710 .remove = virtio_vsock_remove,
711 };
712
virtio_vsock_init(void)713 static int __init virtio_vsock_init(void)
714 {
715 int ret;
716
717 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
718 if (!virtio_vsock_workqueue)
719 return -ENOMEM;
720
721 ret = vsock_core_register(&virtio_transport.transport,
722 VSOCK_TRANSPORT_F_G2H);
723 if (ret)
724 goto out_wq;
725
726 ret = register_virtio_driver(&virtio_vsock_driver);
727 if (ret)
728 goto out_vci;
729
730 return 0;
731
732 out_vci:
733 vsock_core_unregister(&virtio_transport.transport);
734 out_wq:
735 destroy_workqueue(virtio_vsock_workqueue);
736 return ret;
737 }
738
virtio_vsock_exit(void)739 static void __exit virtio_vsock_exit(void)
740 {
741 unregister_virtio_driver(&virtio_vsock_driver);
742 vsock_core_unregister(&virtio_transport.transport);
743 destroy_workqueue(virtio_vsock_workqueue);
744 }
745
746 module_init(virtio_vsock_init);
747 module_exit(virtio_vsock_exit);
748 MODULE_LICENSE("GPL v2");
749 MODULE_AUTHOR("Asias He");
750 MODULE_DESCRIPTION("virtio transport for vsock");
751 MODULE_DEVICE_TABLE(virtio, id_table);
752