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