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