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