Mention branches and keyring.
[releases.git] / vmw_vsock / virtio_transport.c
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 static struct virtio_transport virtio_transport; /* forward declaration */
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         bool tx_run;
43
44         struct work_struct send_pkt_work;
45         struct sk_buff_head send_pkt_queue;
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         bool seqpacket_allow;
66 };
67
68 static u32 virtio_transport_get_local_cid(void)
69 {
70         struct virtio_vsock *vsock;
71         u32 ret;
72
73         rcu_read_lock();
74         vsock = rcu_dereference(the_virtio_vsock);
75         if (!vsock) {
76                 ret = VMADDR_CID_ANY;
77                 goto out_rcu;
78         }
79
80         ret = vsock->guest_cid;
81 out_rcu:
82         rcu_read_unlock();
83         return ret;
84 }
85
86 static void
87 virtio_transport_send_pkt_work(struct work_struct *work)
88 {
89         struct virtio_vsock *vsock =
90                 container_of(work, struct virtio_vsock, send_pkt_work);
91         struct virtqueue *vq;
92         bool added = false;
93         bool restart_rx = false;
94
95         mutex_lock(&vsock->tx_lock);
96
97         if (!vsock->tx_run)
98                 goto out;
99
100         vq = vsock->vqs[VSOCK_VQ_TX];
101
102         for (;;) {
103                 struct scatterlist hdr, buf, *sgs[2];
104                 int ret, in_sg = 0, out_sg = 0;
105                 struct sk_buff *skb;
106                 bool reply;
107
108                 skb = virtio_vsock_skb_dequeue(&vsock->send_pkt_queue);
109                 if (!skb)
110                         break;
111
112                 reply = virtio_vsock_skb_reply(skb);
113
114                 sg_init_one(&hdr, virtio_vsock_hdr(skb), sizeof(*virtio_vsock_hdr(skb)));
115                 sgs[out_sg++] = &hdr;
116                 if (skb->len > 0) {
117                         sg_init_one(&buf, skb->data, skb->len);
118                         sgs[out_sg++] = &buf;
119                 }
120
121                 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, skb, GFP_KERNEL);
122                 /* Usually this means that there is no more space available in
123                  * the vq
124                  */
125                 if (ret < 0) {
126                         virtio_vsock_skb_queue_head(&vsock->send_pkt_queue, skb);
127                         break;
128                 }
129
130                 virtio_transport_deliver_tap_pkt(skb);
131
132                 if (reply) {
133                         struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
134                         int val;
135
136                         val = atomic_dec_return(&vsock->queued_replies);
137
138                         /* Do we now have resources to resume rx processing? */
139                         if (val + 1 == virtqueue_get_vring_size(rx_vq))
140                                 restart_rx = true;
141                 }
142
143                 added = true;
144         }
145
146         if (added)
147                 virtqueue_kick(vq);
148
149 out:
150         mutex_unlock(&vsock->tx_lock);
151
152         if (restart_rx)
153                 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
154 }
155
156 static int
157 virtio_transport_send_pkt(struct sk_buff *skb)
158 {
159         struct virtio_vsock_hdr *hdr;
160         struct virtio_vsock *vsock;
161         int len = skb->len;
162
163         hdr = virtio_vsock_hdr(skb);
164
165         rcu_read_lock();
166         vsock = rcu_dereference(the_virtio_vsock);
167         if (!vsock) {
168                 kfree_skb(skb);
169                 len = -ENODEV;
170                 goto out_rcu;
171         }
172
173         if (le64_to_cpu(hdr->dst_cid) == vsock->guest_cid) {
174                 kfree_skb(skb);
175                 len = -ENODEV;
176                 goto out_rcu;
177         }
178
179         if (virtio_vsock_skb_reply(skb))
180                 atomic_inc(&vsock->queued_replies);
181
182         virtio_vsock_skb_queue_tail(&vsock->send_pkt_queue, skb);
183         queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
184
185 out_rcu:
186         rcu_read_unlock();
187         return len;
188 }
189
190 static int
191 virtio_transport_cancel_pkt(struct vsock_sock *vsk)
192 {
193         struct virtio_vsock *vsock;
194         int cnt = 0, ret;
195
196         rcu_read_lock();
197         vsock = rcu_dereference(the_virtio_vsock);
198         if (!vsock) {
199                 ret = -ENODEV;
200                 goto out_rcu;
201         }
202
203         cnt = virtio_transport_purge_skbs(vsk, &vsock->send_pkt_queue);
204
205         if (cnt) {
206                 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
207                 int new_cnt;
208
209                 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
210                 if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
211                     new_cnt < virtqueue_get_vring_size(rx_vq))
212                         queue_work(virtio_vsock_workqueue, &vsock->rx_work);
213         }
214
215         ret = 0;
216
217 out_rcu:
218         rcu_read_unlock();
219         return ret;
220 }
221
222 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
223 {
224         int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM;
225         struct scatterlist pkt, *p;
226         struct virtqueue *vq;
227         struct sk_buff *skb;
228         int ret;
229
230         vq = vsock->vqs[VSOCK_VQ_RX];
231
232         do {
233                 skb = virtio_vsock_alloc_skb(total_len, GFP_KERNEL);
234                 if (!skb)
235                         break;
236
237                 memset(skb->head, 0, VIRTIO_VSOCK_SKB_HEADROOM);
238                 sg_init_one(&pkt, virtio_vsock_hdr(skb), total_len);
239                 p = &pkt;
240                 ret = virtqueue_add_sgs(vq, &p, 0, 1, skb, GFP_KERNEL);
241                 if (ret < 0) {
242                         kfree_skb(skb);
243                         break;
244                 }
245
246                 vsock->rx_buf_nr++;
247         } while (vq->num_free);
248         if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
249                 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
250         virtqueue_kick(vq);
251 }
252
253 static void virtio_transport_tx_work(struct work_struct *work)
254 {
255         struct virtio_vsock *vsock =
256                 container_of(work, struct virtio_vsock, tx_work);
257         struct virtqueue *vq;
258         bool added = false;
259
260         vq = vsock->vqs[VSOCK_VQ_TX];
261         mutex_lock(&vsock->tx_lock);
262
263         if (!vsock->tx_run)
264                 goto out;
265
266         do {
267                 struct sk_buff *skb;
268                 unsigned int len;
269
270                 virtqueue_disable_cb(vq);
271                 while ((skb = virtqueue_get_buf(vq, &len)) != NULL) {
272                         consume_skb(skb);
273                         added = true;
274                 }
275         } while (!virtqueue_enable_cb(vq));
276
277 out:
278         mutex_unlock(&vsock->tx_lock);
279
280         if (added)
281                 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
282 }
283
284 /* Is there space left for replies to rx packets? */
285 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
286 {
287         struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
288         int val;
289
290         smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
291         val = atomic_read(&vsock->queued_replies);
292
293         return val < virtqueue_get_vring_size(vq);
294 }
295
296 /* event_lock must be held */
297 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
298                                        struct virtio_vsock_event *event)
299 {
300         struct scatterlist sg;
301         struct virtqueue *vq;
302
303         vq = vsock->vqs[VSOCK_VQ_EVENT];
304
305         sg_init_one(&sg, event, sizeof(*event));
306
307         return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
308 }
309
310 /* event_lock must be held */
311 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
312 {
313         size_t i;
314
315         for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
316                 struct virtio_vsock_event *event = &vsock->event_list[i];
317
318                 virtio_vsock_event_fill_one(vsock, event);
319         }
320
321         virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
322 }
323
324 static void virtio_vsock_reset_sock(struct sock *sk)
325 {
326         /* vmci_transport.c doesn't take sk_lock here either.  At least we're
327          * under vsock_table_lock so the sock cannot disappear while we're
328          * executing.
329          */
330
331         sk->sk_state = TCP_CLOSE;
332         sk->sk_err = ECONNRESET;
333         sk_error_report(sk);
334 }
335
336 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
337 {
338         struct virtio_device *vdev = vsock->vdev;
339         __le64 guest_cid;
340
341         vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
342                           &guest_cid, sizeof(guest_cid));
343         vsock->guest_cid = le64_to_cpu(guest_cid);
344 }
345
346 /* event_lock must be held */
347 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
348                                       struct virtio_vsock_event *event)
349 {
350         switch (le32_to_cpu(event->id)) {
351         case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
352                 virtio_vsock_update_guest_cid(vsock);
353                 vsock_for_each_connected_socket(&virtio_transport.transport,
354                                                 virtio_vsock_reset_sock);
355                 break;
356         }
357 }
358
359 static void virtio_transport_event_work(struct work_struct *work)
360 {
361         struct virtio_vsock *vsock =
362                 container_of(work, struct virtio_vsock, event_work);
363         struct virtqueue *vq;
364
365         vq = vsock->vqs[VSOCK_VQ_EVENT];
366
367         mutex_lock(&vsock->event_lock);
368
369         if (!vsock->event_run)
370                 goto out;
371
372         do {
373                 struct virtio_vsock_event *event;
374                 unsigned int len;
375
376                 virtqueue_disable_cb(vq);
377                 while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
378                         if (len == sizeof(*event))
379                                 virtio_vsock_event_handle(vsock, event);
380
381                         virtio_vsock_event_fill_one(vsock, event);
382                 }
383         } while (!virtqueue_enable_cb(vq));
384
385         virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
386 out:
387         mutex_unlock(&vsock->event_lock);
388 }
389
390 static void virtio_vsock_event_done(struct virtqueue *vq)
391 {
392         struct virtio_vsock *vsock = vq->vdev->priv;
393
394         if (!vsock)
395                 return;
396         queue_work(virtio_vsock_workqueue, &vsock->event_work);
397 }
398
399 static void virtio_vsock_tx_done(struct virtqueue *vq)
400 {
401         struct virtio_vsock *vsock = vq->vdev->priv;
402
403         if (!vsock)
404                 return;
405         queue_work(virtio_vsock_workqueue, &vsock->tx_work);
406 }
407
408 static void virtio_vsock_rx_done(struct virtqueue *vq)
409 {
410         struct virtio_vsock *vsock = vq->vdev->priv;
411
412         if (!vsock)
413                 return;
414         queue_work(virtio_vsock_workqueue, &vsock->rx_work);
415 }
416
417 static bool virtio_transport_seqpacket_allow(u32 remote_cid);
418
419 static struct virtio_transport virtio_transport = {
420         .transport = {
421                 .module                   = THIS_MODULE,
422
423                 .get_local_cid            = virtio_transport_get_local_cid,
424
425                 .init                     = virtio_transport_do_socket_init,
426                 .destruct                 = virtio_transport_destruct,
427                 .release                  = virtio_transport_release,
428                 .connect                  = virtio_transport_connect,
429                 .shutdown                 = virtio_transport_shutdown,
430                 .cancel_pkt               = virtio_transport_cancel_pkt,
431
432                 .dgram_bind               = virtio_transport_dgram_bind,
433                 .dgram_dequeue            = virtio_transport_dgram_dequeue,
434                 .dgram_enqueue            = virtio_transport_dgram_enqueue,
435                 .dgram_allow              = virtio_transport_dgram_allow,
436
437                 .stream_dequeue           = virtio_transport_stream_dequeue,
438                 .stream_enqueue           = virtio_transport_stream_enqueue,
439                 .stream_has_data          = virtio_transport_stream_has_data,
440                 .stream_has_space         = virtio_transport_stream_has_space,
441                 .stream_rcvhiwat          = virtio_transport_stream_rcvhiwat,
442                 .stream_is_active         = virtio_transport_stream_is_active,
443                 .stream_allow             = virtio_transport_stream_allow,
444
445                 .seqpacket_dequeue        = virtio_transport_seqpacket_dequeue,
446                 .seqpacket_enqueue        = virtio_transport_seqpacket_enqueue,
447                 .seqpacket_allow          = virtio_transport_seqpacket_allow,
448                 .seqpacket_has_data       = virtio_transport_seqpacket_has_data,
449
450                 .notify_poll_in           = virtio_transport_notify_poll_in,
451                 .notify_poll_out          = virtio_transport_notify_poll_out,
452                 .notify_recv_init         = virtio_transport_notify_recv_init,
453                 .notify_recv_pre_block    = virtio_transport_notify_recv_pre_block,
454                 .notify_recv_pre_dequeue  = virtio_transport_notify_recv_pre_dequeue,
455                 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
456                 .notify_send_init         = virtio_transport_notify_send_init,
457                 .notify_send_pre_block    = virtio_transport_notify_send_pre_block,
458                 .notify_send_pre_enqueue  = virtio_transport_notify_send_pre_enqueue,
459                 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
460                 .notify_buffer_size       = virtio_transport_notify_buffer_size,
461         },
462
463         .send_pkt = virtio_transport_send_pkt,
464 };
465
466 static bool virtio_transport_seqpacket_allow(u32 remote_cid)
467 {
468         struct virtio_vsock *vsock;
469         bool seqpacket_allow;
470
471         seqpacket_allow = false;
472         rcu_read_lock();
473         vsock = rcu_dereference(the_virtio_vsock);
474         if (vsock)
475                 seqpacket_allow = vsock->seqpacket_allow;
476         rcu_read_unlock();
477
478         return seqpacket_allow;
479 }
480
481 static void virtio_transport_rx_work(struct work_struct *work)
482 {
483         struct virtio_vsock *vsock =
484                 container_of(work, struct virtio_vsock, rx_work);
485         struct virtqueue *vq;
486
487         vq = vsock->vqs[VSOCK_VQ_RX];
488
489         mutex_lock(&vsock->rx_lock);
490
491         if (!vsock->rx_run)
492                 goto out;
493
494         do {
495                 virtqueue_disable_cb(vq);
496                 for (;;) {
497                         struct sk_buff *skb;
498                         unsigned int len;
499
500                         if (!virtio_transport_more_replies(vsock)) {
501                                 /* Stop rx until the device processes already
502                                  * pending replies.  Leave rx virtqueue
503                                  * callbacks disabled.
504                                  */
505                                 goto out;
506                         }
507
508                         skb = virtqueue_get_buf(vq, &len);
509                         if (!skb)
510                                 break;
511
512                         vsock->rx_buf_nr--;
513
514                         /* Drop short/long packets */
515                         if (unlikely(len < sizeof(struct virtio_vsock_hdr) ||
516                                      len > virtio_vsock_skb_len(skb))) {
517                                 kfree_skb(skb);
518                                 continue;
519                         }
520
521                         virtio_vsock_skb_rx_put(skb);
522                         virtio_transport_deliver_tap_pkt(skb);
523                         virtio_transport_recv_pkt(&virtio_transport, skb);
524                 }
525         } while (!virtqueue_enable_cb(vq));
526
527 out:
528         if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
529                 virtio_vsock_rx_fill(vsock);
530         mutex_unlock(&vsock->rx_lock);
531 }
532
533 static int virtio_vsock_vqs_init(struct virtio_vsock *vsock)
534 {
535         struct virtio_device *vdev = vsock->vdev;
536         static const char * const names[] = {
537                 "rx",
538                 "tx",
539                 "event",
540         };
541         vq_callback_t *callbacks[] = {
542                 virtio_vsock_rx_done,
543                 virtio_vsock_tx_done,
544                 virtio_vsock_event_done,
545         };
546         int ret;
547
548         ret = virtio_find_vqs(vdev, VSOCK_VQ_MAX, vsock->vqs, callbacks, names,
549                               NULL);
550         if (ret < 0)
551                 return ret;
552
553         virtio_vsock_update_guest_cid(vsock);
554
555         virtio_device_ready(vdev);
556
557         return 0;
558 }
559
560 static void virtio_vsock_vqs_start(struct virtio_vsock *vsock)
561 {
562         mutex_lock(&vsock->tx_lock);
563         vsock->tx_run = true;
564         mutex_unlock(&vsock->tx_lock);
565
566         mutex_lock(&vsock->rx_lock);
567         virtio_vsock_rx_fill(vsock);
568         vsock->rx_run = true;
569         mutex_unlock(&vsock->rx_lock);
570
571         mutex_lock(&vsock->event_lock);
572         virtio_vsock_event_fill(vsock);
573         vsock->event_run = true;
574         mutex_unlock(&vsock->event_lock);
575
576         /* virtio_transport_send_pkt() can queue packets once
577          * the_virtio_vsock is set, but they won't be processed until
578          * vsock->tx_run is set to true. We queue vsock->send_pkt_work
579          * when initialization finishes to send those packets queued
580          * earlier.
581          * We don't need to queue the other workers (rx, event) because
582          * as long as we don't fill the queues with empty buffers, the
583          * host can't send us any notification.
584          */
585         queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
586 }
587
588 static void virtio_vsock_vqs_del(struct virtio_vsock *vsock)
589 {
590         struct virtio_device *vdev = vsock->vdev;
591         struct sk_buff *skb;
592
593         /* Reset all connected sockets when the VQs disappear */
594         vsock_for_each_connected_socket(&virtio_transport.transport,
595                                         virtio_vsock_reset_sock);
596
597         /* Stop all work handlers to make sure no one is accessing the device,
598          * so we can safely call virtio_reset_device().
599          */
600         mutex_lock(&vsock->rx_lock);
601         vsock->rx_run = false;
602         mutex_unlock(&vsock->rx_lock);
603
604         mutex_lock(&vsock->tx_lock);
605         vsock->tx_run = false;
606         mutex_unlock(&vsock->tx_lock);
607
608         mutex_lock(&vsock->event_lock);
609         vsock->event_run = false;
610         mutex_unlock(&vsock->event_lock);
611
612         /* Flush all device writes and interrupts, device will not use any
613          * more buffers.
614          */
615         virtio_reset_device(vdev);
616
617         mutex_lock(&vsock->rx_lock);
618         while ((skb = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
619                 kfree_skb(skb);
620         mutex_unlock(&vsock->rx_lock);
621
622         mutex_lock(&vsock->tx_lock);
623         while ((skb = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
624                 kfree_skb(skb);
625         mutex_unlock(&vsock->tx_lock);
626
627         virtio_vsock_skb_queue_purge(&vsock->send_pkt_queue);
628
629         /* Delete virtqueues and flush outstanding callbacks if any */
630         vdev->config->del_vqs(vdev);
631 }
632
633 static int virtio_vsock_probe(struct virtio_device *vdev)
634 {
635         struct virtio_vsock *vsock = NULL;
636         int ret;
637
638         ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
639         if (ret)
640                 return ret;
641
642         /* Only one virtio-vsock device per guest is supported */
643         if (rcu_dereference_protected(the_virtio_vsock,
644                                 lockdep_is_held(&the_virtio_vsock_mutex))) {
645                 ret = -EBUSY;
646                 goto out;
647         }
648
649         vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
650         if (!vsock) {
651                 ret = -ENOMEM;
652                 goto out;
653         }
654
655         vsock->vdev = vdev;
656
657         vsock->rx_buf_nr = 0;
658         vsock->rx_buf_max_nr = 0;
659         atomic_set(&vsock->queued_replies, 0);
660
661         mutex_init(&vsock->tx_lock);
662         mutex_init(&vsock->rx_lock);
663         mutex_init(&vsock->event_lock);
664         skb_queue_head_init(&vsock->send_pkt_queue);
665         INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
666         INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
667         INIT_WORK(&vsock->event_work, virtio_transport_event_work);
668         INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
669
670         if (virtio_has_feature(vdev, VIRTIO_VSOCK_F_SEQPACKET))
671                 vsock->seqpacket_allow = true;
672
673         vdev->priv = vsock;
674
675         ret = virtio_vsock_vqs_init(vsock);
676         if (ret < 0)
677                 goto out;
678
679         rcu_assign_pointer(the_virtio_vsock, vsock);
680         virtio_vsock_vqs_start(vsock);
681
682         mutex_unlock(&the_virtio_vsock_mutex);
683
684         return 0;
685
686 out:
687         kfree(vsock);
688         mutex_unlock(&the_virtio_vsock_mutex);
689         return ret;
690 }
691
692 static void virtio_vsock_remove(struct virtio_device *vdev)
693 {
694         struct virtio_vsock *vsock = vdev->priv;
695
696         mutex_lock(&the_virtio_vsock_mutex);
697
698         vdev->priv = NULL;
699         rcu_assign_pointer(the_virtio_vsock, NULL);
700         synchronize_rcu();
701
702         virtio_vsock_vqs_del(vsock);
703
704         /* Other works can be queued before 'config->del_vqs()', so we flush
705          * all works before to free the vsock object to avoid use after free.
706          */
707         flush_work(&vsock->rx_work);
708         flush_work(&vsock->tx_work);
709         flush_work(&vsock->event_work);
710         flush_work(&vsock->send_pkt_work);
711
712         mutex_unlock(&the_virtio_vsock_mutex);
713
714         kfree(vsock);
715 }
716
717 #ifdef CONFIG_PM_SLEEP
718 static int virtio_vsock_freeze(struct virtio_device *vdev)
719 {
720         struct virtio_vsock *vsock = vdev->priv;
721
722         mutex_lock(&the_virtio_vsock_mutex);
723
724         rcu_assign_pointer(the_virtio_vsock, NULL);
725         synchronize_rcu();
726
727         virtio_vsock_vqs_del(vsock);
728
729         mutex_unlock(&the_virtio_vsock_mutex);
730
731         return 0;
732 }
733
734 static int virtio_vsock_restore(struct virtio_device *vdev)
735 {
736         struct virtio_vsock *vsock = vdev->priv;
737         int ret;
738
739         mutex_lock(&the_virtio_vsock_mutex);
740
741         /* Only one virtio-vsock device per guest is supported */
742         if (rcu_dereference_protected(the_virtio_vsock,
743                                 lockdep_is_held(&the_virtio_vsock_mutex))) {
744                 ret = -EBUSY;
745                 goto out;
746         }
747
748         ret = virtio_vsock_vqs_init(vsock);
749         if (ret < 0)
750                 goto out;
751
752         rcu_assign_pointer(the_virtio_vsock, vsock);
753         virtio_vsock_vqs_start(vsock);
754
755 out:
756         mutex_unlock(&the_virtio_vsock_mutex);
757         return ret;
758 }
759 #endif /* CONFIG_PM_SLEEP */
760
761 static struct virtio_device_id id_table[] = {
762         { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
763         { 0 },
764 };
765
766 static unsigned int features[] = {
767         VIRTIO_VSOCK_F_SEQPACKET
768 };
769
770 static struct virtio_driver virtio_vsock_driver = {
771         .feature_table = features,
772         .feature_table_size = ARRAY_SIZE(features),
773         .driver.name = KBUILD_MODNAME,
774         .driver.owner = THIS_MODULE,
775         .id_table = id_table,
776         .probe = virtio_vsock_probe,
777         .remove = virtio_vsock_remove,
778 #ifdef CONFIG_PM_SLEEP
779         .freeze = virtio_vsock_freeze,
780         .restore = virtio_vsock_restore,
781 #endif
782 };
783
784 static int __init virtio_vsock_init(void)
785 {
786         int ret;
787
788         virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
789         if (!virtio_vsock_workqueue)
790                 return -ENOMEM;
791
792         ret = vsock_core_register(&virtio_transport.transport,
793                                   VSOCK_TRANSPORT_F_G2H);
794         if (ret)
795                 goto out_wq;
796
797         ret = register_virtio_driver(&virtio_vsock_driver);
798         if (ret)
799                 goto out_vci;
800
801         return 0;
802
803 out_vci:
804         vsock_core_unregister(&virtio_transport.transport);
805 out_wq:
806         destroy_workqueue(virtio_vsock_workqueue);
807         return ret;
808 }
809
810 static void __exit virtio_vsock_exit(void)
811 {
812         unregister_virtio_driver(&virtio_vsock_driver);
813         vsock_core_unregister(&virtio_transport.transport);
814         destroy_workqueue(virtio_vsock_workqueue);
815 }
816
817 module_init(virtio_vsock_init);
818 module_exit(virtio_vsock_exit);
819 MODULE_LICENSE("GPL v2");
820 MODULE_AUTHOR("Asias He");
821 MODULE_DESCRIPTION("virtio transport for vsock");
822 MODULE_DEVICE_TABLE(virtio, id_table);