1 // SPDX-License-Identifier: GPL-2.0-only
3 * virtio transport for vsock
5 * Copyright (C) 2013-2015 Red Hat, Inc.
6 * Author: Asias He <asias@redhat.com>
7 * Stefan Hajnoczi <stefanha@redhat.com>
9 * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
10 * early virtio-vsock proof-of-concept bits.
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>
21 #include <linux/mutex.h>
22 #include <net/af_vsock.h>
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 */
30 struct virtio_device
*vdev
;
31 struct virtqueue
*vqs
[VSOCK_VQ_MAX
];
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
;
38 /* The following fields are protected by tx_lock. vqs[VSOCK_VQ_TX]
39 * must be accessed with tx_lock held.
44 struct work_struct send_pkt_work
;
45 struct sk_buff_head send_pkt_queue
;
47 atomic_t queued_replies
;
49 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
50 * must be accessed with rx_lock held.
57 /* The following fields are protected by event_lock.
58 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
60 struct mutex event_lock
;
62 struct virtio_vsock_event event_list
[8];
67 /* These fields are used only in tx path in function
68 * 'virtio_transport_send_pkt_work()', so to save
69 * stack space in it, place both of them here. Each
70 * pointer from 'out_sgs' points to the corresponding
71 * element in 'out_bufs' - this is initialized in
72 * 'virtio_vsock_probe()'. Both fields are protected
73 * by 'tx_lock'. +1 is needed for packet header.
75 struct scatterlist
*out_sgs
[MAX_SKB_FRAGS
+ 1];
76 struct scatterlist out_bufs
[MAX_SKB_FRAGS
+ 1];
79 static u32
virtio_transport_get_local_cid(void)
81 struct virtio_vsock
*vsock
;
85 vsock
= rcu_dereference(the_virtio_vsock
);
91 ret
= vsock
->guest_cid
;
97 /* Caller need to hold vsock->tx_lock on vq */
98 static int virtio_transport_send_skb(struct sk_buff
*skb
, struct virtqueue
*vq
,
99 struct virtio_vsock
*vsock
, gfp_t gfp
)
101 int ret
, in_sg
= 0, out_sg
= 0;
102 struct scatterlist
**sgs
;
104 sgs
= vsock
->out_sgs
;
105 sg_init_one(sgs
[out_sg
], virtio_vsock_hdr(skb
),
106 sizeof(*virtio_vsock_hdr(skb
)));
109 if (!skb_is_nonlinear(skb
)) {
111 sg_init_one(sgs
[out_sg
], skb
->data
, skb
->len
);
115 struct skb_shared_info
*si
;
118 /* If skb is nonlinear, then its buffer must contain
119 * only header and nothing more. Data is stored in
122 WARN_ON_ONCE(skb_headroom(skb
) != sizeof(*virtio_vsock_hdr(skb
)));
124 si
= skb_shinfo(skb
);
126 for (i
= 0; i
< si
->nr_frags
; i
++) {
127 skb_frag_t
*skb_frag
= &si
->frags
[i
];
130 /* We will use 'page_to_virt()' for the userspace page
131 * here, because virtio or dma-mapping layers will call
132 * 'virt_to_phys()' later to fill the buffer descriptor.
133 * We don't touch memory at "virtual" address of this page.
135 va
= page_to_virt(skb_frag_page(skb_frag
));
136 sg_init_one(sgs
[out_sg
],
137 va
+ skb_frag_off(skb_frag
),
138 skb_frag_size(skb_frag
));
143 ret
= virtqueue_add_sgs(vq
, sgs
, out_sg
, in_sg
, skb
, gfp
);
144 /* Usually this means that there is no more space available in
150 virtio_transport_deliver_tap_pkt(skb
);
155 virtio_transport_send_pkt_work(struct work_struct
*work
)
157 struct virtio_vsock
*vsock
=
158 container_of(work
, struct virtio_vsock
, send_pkt_work
);
159 struct virtqueue
*vq
;
161 bool restart_rx
= false;
163 mutex_lock(&vsock
->tx_lock
);
168 vq
= vsock
->vqs
[VSOCK_VQ_TX
];
175 skb
= virtio_vsock_skb_dequeue(&vsock
->send_pkt_queue
);
179 reply
= virtio_vsock_skb_reply(skb
);
181 ret
= virtio_transport_send_skb(skb
, vq
, vsock
, GFP_KERNEL
);
183 virtio_vsock_skb_queue_head(&vsock
->send_pkt_queue
, skb
);
188 struct virtqueue
*rx_vq
= vsock
->vqs
[VSOCK_VQ_RX
];
191 val
= atomic_dec_return(&vsock
->queued_replies
);
193 /* Do we now have resources to resume rx processing? */
194 if (val
+ 1 == virtqueue_get_vring_size(rx_vq
))
205 mutex_unlock(&vsock
->tx_lock
);
208 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
211 /* Caller need to hold RCU for vsock.
212 * Returns 0 if the packet is successfully put on the vq.
214 static int virtio_transport_send_skb_fast_path(struct virtio_vsock
*vsock
, struct sk_buff
*skb
)
216 struct virtqueue
*vq
= vsock
->vqs
[VSOCK_VQ_TX
];
219 /* Inside RCU, can't sleep! */
220 ret
= mutex_trylock(&vsock
->tx_lock
);
221 if (unlikely(ret
== 0))
224 ret
= virtio_transport_send_skb(skb
, vq
, vsock
, GFP_ATOMIC
);
228 mutex_unlock(&vsock
->tx_lock
);
234 virtio_transport_send_pkt(struct sk_buff
*skb
)
236 struct virtio_vsock_hdr
*hdr
;
237 struct virtio_vsock
*vsock
;
240 hdr
= virtio_vsock_hdr(skb
);
243 vsock
= rcu_dereference(the_virtio_vsock
);
250 if (le64_to_cpu(hdr
->dst_cid
) == vsock
->guest_cid
) {
256 /* If send_pkt_queue is empty, we can safely bypass this queue
257 * because packet order is maintained and (try) to put the packet
258 * on the virtqueue using virtio_transport_send_skb_fast_path.
259 * If this fails we simply put the packet on the intermediate
260 * queue and schedule the worker.
262 if (!skb_queue_empty_lockless(&vsock
->send_pkt_queue
) ||
263 virtio_transport_send_skb_fast_path(vsock
, skb
)) {
264 if (virtio_vsock_skb_reply(skb
))
265 atomic_inc(&vsock
->queued_replies
);
267 virtio_vsock_skb_queue_tail(&vsock
->send_pkt_queue
, skb
);
268 queue_work(virtio_vsock_workqueue
, &vsock
->send_pkt_work
);
277 virtio_transport_cancel_pkt(struct vsock_sock
*vsk
)
279 struct virtio_vsock
*vsock
;
283 vsock
= rcu_dereference(the_virtio_vsock
);
289 cnt
= virtio_transport_purge_skbs(vsk
, &vsock
->send_pkt_queue
);
292 struct virtqueue
*rx_vq
= vsock
->vqs
[VSOCK_VQ_RX
];
295 new_cnt
= atomic_sub_return(cnt
, &vsock
->queued_replies
);
296 if (new_cnt
+ cnt
>= virtqueue_get_vring_size(rx_vq
) &&
297 new_cnt
< virtqueue_get_vring_size(rx_vq
))
298 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
308 static void virtio_vsock_rx_fill(struct virtio_vsock
*vsock
)
310 int total_len
= VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE
+ VIRTIO_VSOCK_SKB_HEADROOM
;
311 struct scatterlist pkt
, *p
;
312 struct virtqueue
*vq
;
316 vq
= vsock
->vqs
[VSOCK_VQ_RX
];
319 skb
= virtio_vsock_alloc_skb(total_len
, GFP_KERNEL
);
323 memset(skb
->head
, 0, VIRTIO_VSOCK_SKB_HEADROOM
);
324 sg_init_one(&pkt
, virtio_vsock_hdr(skb
), total_len
);
326 ret
= virtqueue_add_sgs(vq
, &p
, 0, 1, skb
, GFP_KERNEL
);
333 } while (vq
->num_free
);
334 if (vsock
->rx_buf_nr
> vsock
->rx_buf_max_nr
)
335 vsock
->rx_buf_max_nr
= vsock
->rx_buf_nr
;
339 static void virtio_transport_tx_work(struct work_struct
*work
)
341 struct virtio_vsock
*vsock
=
342 container_of(work
, struct virtio_vsock
, tx_work
);
343 struct virtqueue
*vq
;
346 vq
= vsock
->vqs
[VSOCK_VQ_TX
];
347 mutex_lock(&vsock
->tx_lock
);
356 virtqueue_disable_cb(vq
);
357 while ((skb
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
358 virtio_transport_consume_skb_sent(skb
, true);
361 } while (!virtqueue_enable_cb(vq
));
364 mutex_unlock(&vsock
->tx_lock
);
367 queue_work(virtio_vsock_workqueue
, &vsock
->send_pkt_work
);
370 /* Is there space left for replies to rx packets? */
371 static bool virtio_transport_more_replies(struct virtio_vsock
*vsock
)
373 struct virtqueue
*vq
= vsock
->vqs
[VSOCK_VQ_RX
];
376 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
377 val
= atomic_read(&vsock
->queued_replies
);
379 return val
< virtqueue_get_vring_size(vq
);
382 /* event_lock must be held */
383 static int virtio_vsock_event_fill_one(struct virtio_vsock
*vsock
,
384 struct virtio_vsock_event
*event
)
386 struct scatterlist sg
;
387 struct virtqueue
*vq
;
389 vq
= vsock
->vqs
[VSOCK_VQ_EVENT
];
391 sg_init_one(&sg
, event
, sizeof(*event
));
393 return virtqueue_add_inbuf(vq
, &sg
, 1, event
, GFP_KERNEL
);
396 /* event_lock must be held */
397 static void virtio_vsock_event_fill(struct virtio_vsock
*vsock
)
401 for (i
= 0; i
< ARRAY_SIZE(vsock
->event_list
); i
++) {
402 struct virtio_vsock_event
*event
= &vsock
->event_list
[i
];
404 virtio_vsock_event_fill_one(vsock
, event
);
407 virtqueue_kick(vsock
->vqs
[VSOCK_VQ_EVENT
]);
410 static void virtio_vsock_reset_sock(struct sock
*sk
)
412 /* vmci_transport.c doesn't take sk_lock here either. At least we're
413 * under vsock_table_lock so the sock cannot disappear while we're
417 sk
->sk_state
= TCP_CLOSE
;
418 sk
->sk_err
= ECONNRESET
;
422 static void virtio_vsock_update_guest_cid(struct virtio_vsock
*vsock
)
424 struct virtio_device
*vdev
= vsock
->vdev
;
427 vdev
->config
->get(vdev
, offsetof(struct virtio_vsock_config
, guest_cid
),
428 &guest_cid
, sizeof(guest_cid
));
429 vsock
->guest_cid
= le64_to_cpu(guest_cid
);
432 /* event_lock must be held */
433 static void virtio_vsock_event_handle(struct virtio_vsock
*vsock
,
434 struct virtio_vsock_event
*event
)
436 switch (le32_to_cpu(event
->id
)) {
437 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET
:
438 virtio_vsock_update_guest_cid(vsock
);
439 vsock_for_each_connected_socket(&virtio_transport
.transport
,
440 virtio_vsock_reset_sock
);
445 static void virtio_transport_event_work(struct work_struct
*work
)
447 struct virtio_vsock
*vsock
=
448 container_of(work
, struct virtio_vsock
, event_work
);
449 struct virtqueue
*vq
;
451 vq
= vsock
->vqs
[VSOCK_VQ_EVENT
];
453 mutex_lock(&vsock
->event_lock
);
455 if (!vsock
->event_run
)
459 struct virtio_vsock_event
*event
;
462 virtqueue_disable_cb(vq
);
463 while ((event
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
464 if (len
== sizeof(*event
))
465 virtio_vsock_event_handle(vsock
, event
);
467 virtio_vsock_event_fill_one(vsock
, event
);
469 } while (!virtqueue_enable_cb(vq
));
471 virtqueue_kick(vsock
->vqs
[VSOCK_VQ_EVENT
]);
473 mutex_unlock(&vsock
->event_lock
);
476 static void virtio_vsock_event_done(struct virtqueue
*vq
)
478 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
482 queue_work(virtio_vsock_workqueue
, &vsock
->event_work
);
485 static void virtio_vsock_tx_done(struct virtqueue
*vq
)
487 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
491 queue_work(virtio_vsock_workqueue
, &vsock
->tx_work
);
494 static void virtio_vsock_rx_done(struct virtqueue
*vq
)
496 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
500 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
503 static bool virtio_transport_can_msgzerocopy(int bufs_num
)
505 struct virtio_vsock
*vsock
;
510 vsock
= rcu_dereference(the_virtio_vsock
);
512 struct virtqueue
*vq
= vsock
->vqs
[VSOCK_VQ_TX
];
514 /* Check that tx queue is large enough to keep whole
515 * data to send. This is needed, because when there is
516 * not enough free space in the queue, current skb to
517 * send will be reinserted to the head of tx list of
518 * the socket to retry transmission later, so if skb
519 * is bigger than whole queue, it will be reinserted
520 * again and again, thus blocking other skbs to be sent.
521 * Each page of the user provided buffer will be added
522 * as a single buffer to the tx virtqueue, so compare
523 * number of pages against maximum capacity of the queue.
525 if (bufs_num
<= vq
->num_max
)
534 static bool virtio_transport_msgzerocopy_allow(void)
539 static bool virtio_transport_seqpacket_allow(u32 remote_cid
);
541 static struct virtio_transport virtio_transport
= {
543 .module
= THIS_MODULE
,
545 .get_local_cid
= virtio_transport_get_local_cid
,
547 .init
= virtio_transport_do_socket_init
,
548 .destruct
= virtio_transport_destruct
,
549 .release
= virtio_transport_release
,
550 .connect
= virtio_transport_connect
,
551 .shutdown
= virtio_transport_shutdown
,
552 .cancel_pkt
= virtio_transport_cancel_pkt
,
554 .dgram_bind
= virtio_transport_dgram_bind
,
555 .dgram_dequeue
= virtio_transport_dgram_dequeue
,
556 .dgram_enqueue
= virtio_transport_dgram_enqueue
,
557 .dgram_allow
= virtio_transport_dgram_allow
,
559 .stream_dequeue
= virtio_transport_stream_dequeue
,
560 .stream_enqueue
= virtio_transport_stream_enqueue
,
561 .stream_has_data
= virtio_transport_stream_has_data
,
562 .stream_has_space
= virtio_transport_stream_has_space
,
563 .stream_rcvhiwat
= virtio_transport_stream_rcvhiwat
,
564 .stream_is_active
= virtio_transport_stream_is_active
,
565 .stream_allow
= virtio_transport_stream_allow
,
567 .seqpacket_dequeue
= virtio_transport_seqpacket_dequeue
,
568 .seqpacket_enqueue
= virtio_transport_seqpacket_enqueue
,
569 .seqpacket_allow
= virtio_transport_seqpacket_allow
,
570 .seqpacket_has_data
= virtio_transport_seqpacket_has_data
,
572 .msgzerocopy_allow
= virtio_transport_msgzerocopy_allow
,
574 .notify_poll_in
= virtio_transport_notify_poll_in
,
575 .notify_poll_out
= virtio_transport_notify_poll_out
,
576 .notify_recv_init
= virtio_transport_notify_recv_init
,
577 .notify_recv_pre_block
= virtio_transport_notify_recv_pre_block
,
578 .notify_recv_pre_dequeue
= virtio_transport_notify_recv_pre_dequeue
,
579 .notify_recv_post_dequeue
= virtio_transport_notify_recv_post_dequeue
,
580 .notify_send_init
= virtio_transport_notify_send_init
,
581 .notify_send_pre_block
= virtio_transport_notify_send_pre_block
,
582 .notify_send_pre_enqueue
= virtio_transport_notify_send_pre_enqueue
,
583 .notify_send_post_enqueue
= virtio_transport_notify_send_post_enqueue
,
584 .notify_buffer_size
= virtio_transport_notify_buffer_size
,
585 .notify_set_rcvlowat
= virtio_transport_notify_set_rcvlowat
,
587 .unsent_bytes
= virtio_transport_unsent_bytes
,
589 .read_skb
= virtio_transport_read_skb
,
592 .send_pkt
= virtio_transport_send_pkt
,
593 .can_msgzerocopy
= virtio_transport_can_msgzerocopy
,
596 static bool virtio_transport_seqpacket_allow(u32 remote_cid
)
598 struct virtio_vsock
*vsock
;
599 bool seqpacket_allow
;
601 seqpacket_allow
= false;
603 vsock
= rcu_dereference(the_virtio_vsock
);
605 seqpacket_allow
= vsock
->seqpacket_allow
;
608 return seqpacket_allow
;
611 static void virtio_transport_rx_work(struct work_struct
*work
)
613 struct virtio_vsock
*vsock
=
614 container_of(work
, struct virtio_vsock
, rx_work
);
615 struct virtqueue
*vq
;
617 vq
= vsock
->vqs
[VSOCK_VQ_RX
];
619 mutex_lock(&vsock
->rx_lock
);
625 virtqueue_disable_cb(vq
);
630 if (!virtio_transport_more_replies(vsock
)) {
631 /* Stop rx until the device processes already
632 * pending replies. Leave rx virtqueue
633 * callbacks disabled.
638 skb
= virtqueue_get_buf(vq
, &len
);
644 /* Drop short/long packets */
645 if (unlikely(len
< sizeof(struct virtio_vsock_hdr
) ||
646 len
> virtio_vsock_skb_len(skb
))) {
651 virtio_vsock_skb_rx_put(skb
);
652 virtio_transport_deliver_tap_pkt(skb
);
653 virtio_transport_recv_pkt(&virtio_transport
, skb
);
655 } while (!virtqueue_enable_cb(vq
));
658 if (vsock
->rx_buf_nr
< vsock
->rx_buf_max_nr
/ 2)
659 virtio_vsock_rx_fill(vsock
);
660 mutex_unlock(&vsock
->rx_lock
);
663 static int virtio_vsock_vqs_init(struct virtio_vsock
*vsock
)
665 struct virtio_device
*vdev
= vsock
->vdev
;
666 struct virtqueue_info vqs_info
[] = {
667 { "rx", virtio_vsock_rx_done
},
668 { "tx", virtio_vsock_tx_done
},
669 { "event", virtio_vsock_event_done
},
673 ret
= virtio_find_vqs(vdev
, VSOCK_VQ_MAX
, vsock
->vqs
, vqs_info
, NULL
);
677 virtio_vsock_update_guest_cid(vsock
);
679 virtio_device_ready(vdev
);
684 static void virtio_vsock_vqs_start(struct virtio_vsock
*vsock
)
686 mutex_lock(&vsock
->tx_lock
);
687 vsock
->tx_run
= true;
688 mutex_unlock(&vsock
->tx_lock
);
690 mutex_lock(&vsock
->rx_lock
);
691 virtio_vsock_rx_fill(vsock
);
692 vsock
->rx_run
= true;
693 mutex_unlock(&vsock
->rx_lock
);
695 mutex_lock(&vsock
->event_lock
);
696 virtio_vsock_event_fill(vsock
);
697 vsock
->event_run
= true;
698 mutex_unlock(&vsock
->event_lock
);
700 /* virtio_transport_send_pkt() can queue packets once
701 * the_virtio_vsock is set, but they won't be processed until
702 * vsock->tx_run is set to true. We queue vsock->send_pkt_work
703 * when initialization finishes to send those packets queued
705 * We don't need to queue the other workers (rx, event) because
706 * as long as we don't fill the queues with empty buffers, the
707 * host can't send us any notification.
709 queue_work(virtio_vsock_workqueue
, &vsock
->send_pkt_work
);
712 static void virtio_vsock_vqs_del(struct virtio_vsock
*vsock
)
714 struct virtio_device
*vdev
= vsock
->vdev
;
717 /* Reset all connected sockets when the VQs disappear */
718 vsock_for_each_connected_socket(&virtio_transport
.transport
,
719 virtio_vsock_reset_sock
);
721 /* Stop all work handlers to make sure no one is accessing the device,
722 * so we can safely call virtio_reset_device().
724 mutex_lock(&vsock
->rx_lock
);
725 vsock
->rx_run
= false;
726 mutex_unlock(&vsock
->rx_lock
);
728 mutex_lock(&vsock
->tx_lock
);
729 vsock
->tx_run
= false;
730 mutex_unlock(&vsock
->tx_lock
);
732 mutex_lock(&vsock
->event_lock
);
733 vsock
->event_run
= false;
734 mutex_unlock(&vsock
->event_lock
);
736 /* Flush all device writes and interrupts, device will not use any
739 virtio_reset_device(vdev
);
741 mutex_lock(&vsock
->rx_lock
);
742 while ((skb
= virtqueue_detach_unused_buf(vsock
->vqs
[VSOCK_VQ_RX
])))
744 mutex_unlock(&vsock
->rx_lock
);
746 mutex_lock(&vsock
->tx_lock
);
747 while ((skb
= virtqueue_detach_unused_buf(vsock
->vqs
[VSOCK_VQ_TX
])))
749 mutex_unlock(&vsock
->tx_lock
);
751 virtio_vsock_skb_queue_purge(&vsock
->send_pkt_queue
);
753 /* Delete virtqueues and flush outstanding callbacks if any */
754 vdev
->config
->del_vqs(vdev
);
757 static int virtio_vsock_probe(struct virtio_device
*vdev
)
759 struct virtio_vsock
*vsock
= NULL
;
763 ret
= mutex_lock_interruptible(&the_virtio_vsock_mutex
);
767 /* Only one virtio-vsock device per guest is supported */
768 if (rcu_dereference_protected(the_virtio_vsock
,
769 lockdep_is_held(&the_virtio_vsock_mutex
))) {
774 vsock
= kzalloc(sizeof(*vsock
), GFP_KERNEL
);
782 vsock
->rx_buf_nr
= 0;
783 vsock
->rx_buf_max_nr
= 0;
784 atomic_set(&vsock
->queued_replies
, 0);
786 mutex_init(&vsock
->tx_lock
);
787 mutex_init(&vsock
->rx_lock
);
788 mutex_init(&vsock
->event_lock
);
789 skb_queue_head_init(&vsock
->send_pkt_queue
);
790 INIT_WORK(&vsock
->rx_work
, virtio_transport_rx_work
);
791 INIT_WORK(&vsock
->tx_work
, virtio_transport_tx_work
);
792 INIT_WORK(&vsock
->event_work
, virtio_transport_event_work
);
793 INIT_WORK(&vsock
->send_pkt_work
, virtio_transport_send_pkt_work
);
795 if (virtio_has_feature(vdev
, VIRTIO_VSOCK_F_SEQPACKET
))
796 vsock
->seqpacket_allow
= true;
800 ret
= virtio_vsock_vqs_init(vsock
);
804 for (i
= 0; i
< ARRAY_SIZE(vsock
->out_sgs
); i
++)
805 vsock
->out_sgs
[i
] = &vsock
->out_bufs
[i
];
807 rcu_assign_pointer(the_virtio_vsock
, vsock
);
808 virtio_vsock_vqs_start(vsock
);
810 mutex_unlock(&the_virtio_vsock_mutex
);
816 mutex_unlock(&the_virtio_vsock_mutex
);
820 static void virtio_vsock_remove(struct virtio_device
*vdev
)
822 struct virtio_vsock
*vsock
= vdev
->priv
;
824 mutex_lock(&the_virtio_vsock_mutex
);
827 rcu_assign_pointer(the_virtio_vsock
, NULL
);
830 virtio_vsock_vqs_del(vsock
);
832 /* Other works can be queued before 'config->del_vqs()', so we flush
833 * all works before to free the vsock object to avoid use after free.
835 flush_work(&vsock
->rx_work
);
836 flush_work(&vsock
->tx_work
);
837 flush_work(&vsock
->event_work
);
838 flush_work(&vsock
->send_pkt_work
);
840 mutex_unlock(&the_virtio_vsock_mutex
);
845 #ifdef CONFIG_PM_SLEEP
846 static int virtio_vsock_freeze(struct virtio_device
*vdev
)
848 struct virtio_vsock
*vsock
= vdev
->priv
;
850 mutex_lock(&the_virtio_vsock_mutex
);
852 rcu_assign_pointer(the_virtio_vsock
, NULL
);
855 virtio_vsock_vqs_del(vsock
);
857 mutex_unlock(&the_virtio_vsock_mutex
);
862 static int virtio_vsock_restore(struct virtio_device
*vdev
)
864 struct virtio_vsock
*vsock
= vdev
->priv
;
867 mutex_lock(&the_virtio_vsock_mutex
);
869 /* Only one virtio-vsock device per guest is supported */
870 if (rcu_dereference_protected(the_virtio_vsock
,
871 lockdep_is_held(&the_virtio_vsock_mutex
))) {
876 ret
= virtio_vsock_vqs_init(vsock
);
880 rcu_assign_pointer(the_virtio_vsock
, vsock
);
881 virtio_vsock_vqs_start(vsock
);
884 mutex_unlock(&the_virtio_vsock_mutex
);
887 #endif /* CONFIG_PM_SLEEP */
889 static struct virtio_device_id id_table
[] = {
890 { VIRTIO_ID_VSOCK
, VIRTIO_DEV_ANY_ID
},
894 static unsigned int features
[] = {
895 VIRTIO_VSOCK_F_SEQPACKET
898 static struct virtio_driver virtio_vsock_driver
= {
899 .feature_table
= features
,
900 .feature_table_size
= ARRAY_SIZE(features
),
901 .driver
.name
= KBUILD_MODNAME
,
902 .id_table
= id_table
,
903 .probe
= virtio_vsock_probe
,
904 .remove
= virtio_vsock_remove
,
905 #ifdef CONFIG_PM_SLEEP
906 .freeze
= virtio_vsock_freeze
,
907 .restore
= virtio_vsock_restore
,
911 static int __init
virtio_vsock_init(void)
915 virtio_vsock_workqueue
= alloc_workqueue("virtio_vsock", 0, 0);
916 if (!virtio_vsock_workqueue
)
919 ret
= vsock_core_register(&virtio_transport
.transport
,
920 VSOCK_TRANSPORT_F_G2H
);
924 ret
= register_virtio_driver(&virtio_vsock_driver
);
931 vsock_core_unregister(&virtio_transport
.transport
);
933 destroy_workqueue(virtio_vsock_workqueue
);
937 static void __exit
virtio_vsock_exit(void)
939 unregister_virtio_driver(&virtio_vsock_driver
);
940 vsock_core_unregister(&virtio_transport
.transport
);
941 destroy_workqueue(virtio_vsock_workqueue
);
944 module_init(virtio_vsock_init
);
945 module_exit(virtio_vsock_exit
);
946 MODULE_LICENSE("GPL v2");
947 MODULE_AUTHOR("Asias He");
948 MODULE_DESCRIPTION("virtio transport for vsock");
949 MODULE_DEVICE_TABLE(virtio
, id_table
);