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