blob: 6c8390a2af52cc025ae5f4926330d5d36e0cb3a7 [file] [log] [blame]
Thomas Gleixner7a338472019-06-04 10:11:15 +02001// SPDX-License-Identifier: GPL-2.0-only
Asias He433fc582016-07-28 15:36:34 +01002/*
3 * vhost 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>
Asias He433fc582016-07-28 15:36:34 +01008 */
9#include <linux/miscdevice.h>
10#include <linux/atomic.h>
11#include <linux/module.h>
12#include <linux/mutex.h>
13#include <linux/vmalloc.h>
14#include <net/sock.h>
15#include <linux/virtio_vsock.h>
16#include <linux/vhost.h>
Stefan Hajnoczi834e7722018-11-05 10:35:47 +000017#include <linux/hashtable.h>
Asias He433fc582016-07-28 15:36:34 +010018
19#include <net/af_vsock.h>
20#include "vhost.h"
21
22#define VHOST_VSOCK_DEFAULT_HOST_CID 2
Jason Wange82b9b02019-05-17 00:29:49 -040023/* Max number of bytes transferred before requeueing the job.
24 * Using this limit prevents one virtqueue from starving others. */
25#define VHOST_VSOCK_WEIGHT 0x80000
26/* Max number of packets transferred before requeueing the job.
27 * Using this limit prevents one virtqueue from starving others with
28 * small pkts.
29 */
30#define VHOST_VSOCK_PKT_WEIGHT 256
Asias He433fc582016-07-28 15:36:34 +010031
32enum {
33 VHOST_VSOCK_FEATURES = VHOST_FEATURES,
34};
35
36/* Used to track all the vhost_vsock instances on the system. */
Stefan Hajnoczi6db3d8d2018-11-05 17:33:22 +000037static DEFINE_MUTEX(vhost_vsock_mutex);
Stefan Hajnoczi834e7722018-11-05 10:35:47 +000038static DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8);
Asias He433fc582016-07-28 15:36:34 +010039
40struct vhost_vsock {
41 struct vhost_dev dev;
42 struct vhost_virtqueue vqs[2];
43
Stefan Hajnoczi6db3d8d2018-11-05 17:33:22 +000044 /* Link to global vhost_vsock_hash, writes use vhost_vsock_mutex */
Stefan Hajnoczi834e7722018-11-05 10:35:47 +000045 struct hlist_node hash;
Asias He433fc582016-07-28 15:36:34 +010046
47 struct vhost_work send_pkt_work;
48 spinlock_t send_pkt_list_lock;
49 struct list_head send_pkt_list; /* host->guest pending packets */
50
51 atomic_t queued_replies;
52
53 u32 guest_cid;
54};
55
56static u32 vhost_transport_get_local_cid(void)
57{
58 return VHOST_VSOCK_DEFAULT_HOST_CID;
59}
60
Stefan Hajnoczi6db3d8d2018-11-05 17:33:22 +000061/* Callers that dereference the return value must hold vhost_vsock_mutex or the
Stefan Hajnoczi834e7722018-11-05 10:35:47 +000062 * RCU read lock.
63 */
64static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)
Asias He433fc582016-07-28 15:36:34 +010065{
66 struct vhost_vsock *vsock;
67
Stefan Hajnoczi834e7722018-11-05 10:35:47 +000068 hash_for_each_possible_rcu(vhost_vsock_hash, vsock, hash, guest_cid) {
Asias He433fc582016-07-28 15:36:34 +010069 u32 other_cid = vsock->guest_cid;
70
71 /* Skip instances that have no CID yet */
72 if (other_cid == 0)
73 continue;
74
Vaibhav Murkuteff3c1b12018-03-09 08:26:03 +053075 if (other_cid == guest_cid)
Asias He433fc582016-07-28 15:36:34 +010076 return vsock;
Vaibhav Murkuteff3c1b12018-03-09 08:26:03 +053077
Asias He433fc582016-07-28 15:36:34 +010078 }
Asias He433fc582016-07-28 15:36:34 +010079
80 return NULL;
81}
82
83static void
84vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
85 struct vhost_virtqueue *vq)
86{
87 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
Jason Wange79b4312019-05-17 00:29:51 -040088 int pkts = 0, total_len = 0;
Asias He433fc582016-07-28 15:36:34 +010089 bool added = false;
90 bool restart_tx = false;
91
92 mutex_lock(&vq->mutex);
93
94 if (!vq->private_data)
95 goto out;
96
97 /* Avoid further vmexits, we're already processing the virtqueue */
98 vhost_disable_notify(&vsock->dev, vq);
99
Jason Wange79b4312019-05-17 00:29:51 -0400100 do {
Asias He433fc582016-07-28 15:36:34 +0100101 struct virtio_vsock_pkt *pkt;
102 struct iov_iter iov_iter;
103 unsigned out, in;
104 size_t nbytes;
105 size_t len;
106 int head;
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 vhost_enable_notify(&vsock->dev, vq);
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 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
121 &out, &in, NULL, NULL);
122 if (head < 0) {
123 spin_lock_bh(&vsock->send_pkt_list_lock);
124 list_add(&pkt->list, &vsock->send_pkt_list);
125 spin_unlock_bh(&vsock->send_pkt_list_lock);
126 break;
127 }
128
129 if (head == vq->num) {
130 spin_lock_bh(&vsock->send_pkt_list_lock);
131 list_add(&pkt->list, &vsock->send_pkt_list);
132 spin_unlock_bh(&vsock->send_pkt_list_lock);
133
134 /* We cannot finish yet if more buffers snuck in while
135 * re-enabling notify.
136 */
137 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
138 vhost_disable_notify(&vsock->dev, vq);
139 continue;
140 }
141 break;
142 }
143
144 if (out) {
145 virtio_transport_free_pkt(pkt);
146 vq_err(vq, "Expected 0 output buffers, got %u\n", out);
147 break;
148 }
149
150 len = iov_length(&vq->iov[out], in);
151 iov_iter_init(&iov_iter, READ, &vq->iov[out], in, len);
152
153 nbytes = copy_to_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
154 if (nbytes != sizeof(pkt->hdr)) {
155 virtio_transport_free_pkt(pkt);
156 vq_err(vq, "Faulted on copying pkt hdr\n");
157 break;
158 }
159
160 nbytes = copy_to_iter(pkt->buf, pkt->len, &iov_iter);
161 if (nbytes != pkt->len) {
162 virtio_transport_free_pkt(pkt);
163 vq_err(vq, "Faulted on copying pkt buf\n");
164 break;
165 }
166
167 vhost_add_used(vq, head, sizeof(pkt->hdr) + pkt->len);
168 added = true;
169
170 if (pkt->reply) {
171 int val;
172
173 val = atomic_dec_return(&vsock->queued_replies);
174
175 /* Do we have resources to resume tx processing? */
176 if (val + 1 == tx_vq->num)
177 restart_tx = true;
178 }
179
Gerard Garcia82dfb5402017-04-21 10:10:46 +0100180 /* Deliver to monitoring devices all correctly transmitted
181 * packets.
182 */
183 virtio_transport_deliver_tap_pkt(pkt);
184
Jason Wange79b4312019-05-17 00:29:51 -0400185 total_len += pkt->len;
Asias He433fc582016-07-28 15:36:34 +0100186 virtio_transport_free_pkt(pkt);
Jason Wange79b4312019-05-17 00:29:51 -0400187 } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
Asias He433fc582016-07-28 15:36:34 +0100188 if (added)
189 vhost_signal(&vsock->dev, vq);
190
191out:
192 mutex_unlock(&vq->mutex);
193
194 if (restart_tx)
195 vhost_poll_queue(&tx_vq->poll);
196}
197
198static void vhost_transport_send_pkt_work(struct vhost_work *work)
199{
200 struct vhost_virtqueue *vq;
201 struct vhost_vsock *vsock;
202
203 vsock = container_of(work, struct vhost_vsock, send_pkt_work);
204 vq = &vsock->vqs[VSOCK_VQ_RX];
205
206 vhost_transport_do_send_pkt(vsock, vq);
207}
208
209static int
210vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
211{
212 struct vhost_vsock *vsock;
Asias He433fc582016-07-28 15:36:34 +0100213 int len = pkt->len;
214
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000215 rcu_read_lock();
216
Asias He433fc582016-07-28 15:36:34 +0100217 /* Find the vhost_vsock according to guest context id */
218 vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
219 if (!vsock) {
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000220 rcu_read_unlock();
Asias He433fc582016-07-28 15:36:34 +0100221 virtio_transport_free_pkt(pkt);
222 return -ENODEV;
223 }
224
Asias He433fc582016-07-28 15:36:34 +0100225 if (pkt->reply)
226 atomic_inc(&vsock->queued_replies);
227
228 spin_lock_bh(&vsock->send_pkt_list_lock);
229 list_add_tail(&pkt->list, &vsock->send_pkt_list);
230 spin_unlock_bh(&vsock->send_pkt_list_lock);
231
232 vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000233
234 rcu_read_unlock();
Asias He433fc582016-07-28 15:36:34 +0100235 return len;
236}
237
Peng Tao16320f32017-03-15 09:32:15 +0800238static int
239vhost_transport_cancel_pkt(struct vsock_sock *vsk)
240{
241 struct vhost_vsock *vsock;
242 struct virtio_vsock_pkt *pkt, *n;
243 int cnt = 0;
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000244 int ret = -ENODEV;
Peng Tao16320f32017-03-15 09:32:15 +0800245 LIST_HEAD(freeme);
246
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000247 rcu_read_lock();
248
Peng Tao16320f32017-03-15 09:32:15 +0800249 /* Find the vhost_vsock according to guest context id */
250 vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
251 if (!vsock)
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000252 goto out;
Peng Tao16320f32017-03-15 09:32:15 +0800253
254 spin_lock_bh(&vsock->send_pkt_list_lock);
255 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
256 if (pkt->vsk != vsk)
257 continue;
258 list_move(&pkt->list, &freeme);
259 }
260 spin_unlock_bh(&vsock->send_pkt_list_lock);
261
262 list_for_each_entry_safe(pkt, n, &freeme, list) {
263 if (pkt->reply)
264 cnt++;
265 list_del(&pkt->list);
266 virtio_transport_free_pkt(pkt);
267 }
268
269 if (cnt) {
270 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
271 int new_cnt;
272
273 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
274 if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
275 vhost_poll_queue(&tx_vq->poll);
276 }
277
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000278 ret = 0;
279out:
280 rcu_read_unlock();
281 return ret;
Peng Tao16320f32017-03-15 09:32:15 +0800282}
283
Asias He433fc582016-07-28 15:36:34 +0100284static struct virtio_vsock_pkt *
285vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
286 unsigned int out, unsigned int in)
287{
288 struct virtio_vsock_pkt *pkt;
289 struct iov_iter iov_iter;
290 size_t nbytes;
291 size_t len;
292
293 if (in != 0) {
294 vq_err(vq, "Expected 0 input buffers, got %u\n", in);
295 return NULL;
296 }
297
298 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
299 if (!pkt)
300 return NULL;
301
302 len = iov_length(vq->iov, out);
303 iov_iter_init(&iov_iter, WRITE, vq->iov, out, len);
304
305 nbytes = copy_from_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
306 if (nbytes != sizeof(pkt->hdr)) {
307 vq_err(vq, "Expected %zu bytes for pkt->hdr, got %zu bytes\n",
308 sizeof(pkt->hdr), nbytes);
309 kfree(pkt);
310 return NULL;
311 }
312
313 if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
314 pkt->len = le32_to_cpu(pkt->hdr.len);
315
316 /* No payload */
317 if (!pkt->len)
318 return pkt;
319
320 /* The pkt is too big */
321 if (pkt->len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
322 kfree(pkt);
323 return NULL;
324 }
325
326 pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
327 if (!pkt->buf) {
328 kfree(pkt);
329 return NULL;
330 }
331
Stefano Garzarella473c7392019-07-30 17:43:30 +0200332 pkt->buf_len = pkt->len;
333
Asias He433fc582016-07-28 15:36:34 +0100334 nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
335 if (nbytes != pkt->len) {
336 vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
337 pkt->len, nbytes);
338 virtio_transport_free_pkt(pkt);
339 return NULL;
340 }
341
342 return pkt;
343}
344
345/* Is there space left for replies to rx packets? */
346static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
347{
348 struct vhost_virtqueue *vq = &vsock->vqs[VSOCK_VQ_TX];
349 int val;
350
351 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
352 val = atomic_read(&vsock->queued_replies);
353
354 return val < vq->num;
355}
356
357static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
358{
359 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
360 poll.work);
361 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
362 dev);
363 struct virtio_vsock_pkt *pkt;
Jason Wange79b4312019-05-17 00:29:51 -0400364 int head, pkts = 0, total_len = 0;
Asias He433fc582016-07-28 15:36:34 +0100365 unsigned int out, in;
366 bool added = false;
367
368 mutex_lock(&vq->mutex);
369
370 if (!vq->private_data)
371 goto out;
372
373 vhost_disable_notify(&vsock->dev, vq);
Jason Wange79b4312019-05-17 00:29:51 -0400374 do {
Stefan Hajnoczi3fda5d62016-08-04 14:52:53 +0100375 u32 len;
376
Asias He433fc582016-07-28 15:36:34 +0100377 if (!vhost_vsock_more_replies(vsock)) {
378 /* Stop tx until the device processes already
379 * pending replies. Leave tx virtqueue
380 * callbacks disabled.
381 */
382 goto no_more_replies;
383 }
384
385 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
386 &out, &in, NULL, NULL);
387 if (head < 0)
388 break;
389
390 if (head == vq->num) {
391 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
392 vhost_disable_notify(&vsock->dev, vq);
393 continue;
394 }
395 break;
396 }
397
398 pkt = vhost_vsock_alloc_pkt(vq, out, in);
399 if (!pkt) {
400 vq_err(vq, "Faulted on pkt\n");
401 continue;
402 }
403
Stefan Hajnoczi3fda5d62016-08-04 14:52:53 +0100404 len = pkt->len;
405
Gerard Garcia82dfb5402017-04-21 10:10:46 +0100406 /* Deliver to monitoring devices all received packets */
407 virtio_transport_deliver_tap_pkt(pkt);
408
Asias He433fc582016-07-28 15:36:34 +0100409 /* Only accept correctly addressed packets */
410 if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid)
411 virtio_transport_recv_pkt(pkt);
412 else
413 virtio_transport_free_pkt(pkt);
414
Jason Wange79b4312019-05-17 00:29:51 -0400415 len += sizeof(pkt->hdr);
416 vhost_add_used(vq, head, len);
417 total_len += len;
Asias He433fc582016-07-28 15:36:34 +0100418 added = true;
Jason Wange79b4312019-05-17 00:29:51 -0400419 } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
Asias He433fc582016-07-28 15:36:34 +0100420
421no_more_replies:
422 if (added)
423 vhost_signal(&vsock->dev, vq);
424
425out:
426 mutex_unlock(&vq->mutex);
427}
428
429static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
430{
431 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
432 poll.work);
433 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
434 dev);
435
436 vhost_transport_do_send_pkt(vsock, vq);
437}
438
439static int vhost_vsock_start(struct vhost_vsock *vsock)
440{
Stefan Hajnoczi0516ffd2017-01-19 10:43:53 +0000441 struct vhost_virtqueue *vq;
Asias He433fc582016-07-28 15:36:34 +0100442 size_t i;
443 int ret;
444
445 mutex_lock(&vsock->dev.mutex);
446
447 ret = vhost_dev_check_owner(&vsock->dev);
448 if (ret)
449 goto err;
450
451 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
Stefan Hajnoczi0516ffd2017-01-19 10:43:53 +0000452 vq = &vsock->vqs[i];
Asias He433fc582016-07-28 15:36:34 +0100453
454 mutex_lock(&vq->mutex);
455
456 if (!vhost_vq_access_ok(vq)) {
457 ret = -EFAULT;
Asias He433fc582016-07-28 15:36:34 +0100458 goto err_vq;
459 }
460
461 if (!vq->private_data) {
462 vq->private_data = vsock;
Stefan Hajnoczi0516ffd2017-01-19 10:43:53 +0000463 ret = vhost_vq_init_access(vq);
464 if (ret)
465 goto err_vq;
Asias He433fc582016-07-28 15:36:34 +0100466 }
467
468 mutex_unlock(&vq->mutex);
469 }
470
471 mutex_unlock(&vsock->dev.mutex);
472 return 0;
473
474err_vq:
Stefan Hajnoczi0516ffd2017-01-19 10:43:53 +0000475 vq->private_data = NULL;
476 mutex_unlock(&vq->mutex);
477
Asias He433fc582016-07-28 15:36:34 +0100478 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
Stefan Hajnoczi0516ffd2017-01-19 10:43:53 +0000479 vq = &vsock->vqs[i];
Asias He433fc582016-07-28 15:36:34 +0100480
481 mutex_lock(&vq->mutex);
482 vq->private_data = NULL;
483 mutex_unlock(&vq->mutex);
484 }
485err:
486 mutex_unlock(&vsock->dev.mutex);
487 return ret;
488}
489
490static int vhost_vsock_stop(struct vhost_vsock *vsock)
491{
492 size_t i;
493 int ret;
494
495 mutex_lock(&vsock->dev.mutex);
496
497 ret = vhost_dev_check_owner(&vsock->dev);
498 if (ret)
499 goto err;
500
501 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
502 struct vhost_virtqueue *vq = &vsock->vqs[i];
503
504 mutex_lock(&vq->mutex);
505 vq->private_data = NULL;
506 mutex_unlock(&vq->mutex);
507 }
508
509err:
510 mutex_unlock(&vsock->dev.mutex);
511 return ret;
512}
513
514static void vhost_vsock_free(struct vhost_vsock *vsock)
515{
Wei Yongjunb226aca2016-08-02 13:50:42 +0000516 kvfree(vsock);
Asias He433fc582016-07-28 15:36:34 +0100517}
518
519static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
520{
521 struct vhost_virtqueue **vqs;
522 struct vhost_vsock *vsock;
523 int ret;
524
525 /* This struct is large and allocation could fail, fall back to vmalloc
526 * if there is no other way.
527 */
Michal Hockodcda9b02017-07-12 14:36:45 -0700528 vsock = kvmalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
Michal Hocko6c5ab652017-05-08 15:57:15 -0700529 if (!vsock)
530 return -ENOMEM;
Asias He433fc582016-07-28 15:36:34 +0100531
532 vqs = kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL);
533 if (!vqs) {
534 ret = -ENOMEM;
535 goto out;
536 }
537
Stefan Hajnoczia72b69d2017-11-09 13:29:10 +0000538 vsock->guest_cid = 0; /* no CID assigned yet */
539
Asias He433fc582016-07-28 15:36:34 +0100540 atomic_set(&vsock->queued_replies, 0);
541
542 vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
543 vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
544 vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
545 vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
546
Jason Wange82b9b02019-05-17 00:29:49 -0400547 vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs),
548 UIO_MAXIOV, VHOST_VSOCK_PKT_WEIGHT,
549 VHOST_VSOCK_WEIGHT);
Asias He433fc582016-07-28 15:36:34 +0100550
551 file->private_data = vsock;
552 spin_lock_init(&vsock->send_pkt_list_lock);
553 INIT_LIST_HEAD(&vsock->send_pkt_list);
554 vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
Asias He433fc582016-07-28 15:36:34 +0100555 return 0;
556
557out:
558 vhost_vsock_free(vsock);
559 return ret;
560}
561
562static void vhost_vsock_flush(struct vhost_vsock *vsock)
563{
564 int i;
565
566 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
567 if (vsock->vqs[i].handle_kick)
568 vhost_poll_flush(&vsock->vqs[i].poll);
569 vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
570}
571
572static void vhost_vsock_reset_orphans(struct sock *sk)
573{
574 struct vsock_sock *vsk = vsock_sk(sk);
575
576 /* vmci_transport.c doesn't take sk_lock here either. At least we're
577 * under vsock_table_lock so the sock cannot disappear while we're
578 * executing.
579 */
580
Stefan Hajnoczic38f57d2018-12-06 19:14:34 +0000581 /* If the peer is still valid, no need to reset connection */
582 if (vhost_vsock_get(vsk->remote_addr.svm_cid))
583 return;
584
585 /* If the close timeout is pending, let it expire. This avoids races
586 * with the timeout callback.
587 */
588 if (vsk->close_work_scheduled)
589 return;
590
591 sock_set_flag(sk, SOCK_DONE);
592 vsk->peer_shutdown = SHUTDOWN_MASK;
593 sk->sk_state = SS_UNCONNECTED;
594 sk->sk_err = ECONNRESET;
595 sk->sk_error_report(sk);
Asias He433fc582016-07-28 15:36:34 +0100596}
597
598static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
599{
600 struct vhost_vsock *vsock = file->private_data;
601
Stefan Hajnoczi6db3d8d2018-11-05 17:33:22 +0000602 mutex_lock(&vhost_vsock_mutex);
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000603 if (vsock->guest_cid)
604 hash_del_rcu(&vsock->hash);
Stefan Hajnoczi6db3d8d2018-11-05 17:33:22 +0000605 mutex_unlock(&vhost_vsock_mutex);
Asias He433fc582016-07-28 15:36:34 +0100606
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000607 /* Wait for other CPUs to finish using vsock */
608 synchronize_rcu();
609
Asias He433fc582016-07-28 15:36:34 +0100610 /* Iterating over all connections for all CIDs to find orphans is
611 * inefficient. Room for improvement here. */
612 vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
613
614 vhost_vsock_stop(vsock);
615 vhost_vsock_flush(vsock);
616 vhost_dev_stop(&vsock->dev);
617
618 spin_lock_bh(&vsock->send_pkt_list_lock);
619 while (!list_empty(&vsock->send_pkt_list)) {
620 struct virtio_vsock_pkt *pkt;
621
622 pkt = list_first_entry(&vsock->send_pkt_list,
623 struct virtio_vsock_pkt, list);
624 list_del_init(&pkt->list);
625 virtio_transport_free_pkt(pkt);
626 }
627 spin_unlock_bh(&vsock->send_pkt_list_lock);
628
夷则(Caspar)f6f93f72017-12-25 00:08:58 +0800629 vhost_dev_cleanup(&vsock->dev);
Asias He433fc582016-07-28 15:36:34 +0100630 kfree(vsock->dev.vqs);
631 vhost_vsock_free(vsock);
632 return 0;
633}
634
635static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
636{
637 struct vhost_vsock *other;
638
639 /* Refuse reserved CIDs */
640 if (guest_cid <= VMADDR_CID_HOST ||
641 guest_cid == U32_MAX)
642 return -EINVAL;
643
644 /* 64-bit CIDs are not yet supported */
645 if (guest_cid > U32_MAX)
646 return -EINVAL;
647
648 /* Refuse if CID is already in use */
Stefan Hajnoczi6db3d8d2018-11-05 17:33:22 +0000649 mutex_lock(&vhost_vsock_mutex);
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000650 other = vhost_vsock_get(guest_cid);
Gao feng6c083c22016-12-14 19:24:36 +0800651 if (other && other != vsock) {
Stefan Hajnoczi6db3d8d2018-11-05 17:33:22 +0000652 mutex_unlock(&vhost_vsock_mutex);
Gao feng6c083c22016-12-14 19:24:36 +0800653 return -EADDRINUSE;
654 }
Stefan Hajnoczi834e7722018-11-05 10:35:47 +0000655
656 if (vsock->guest_cid)
657 hash_del_rcu(&vsock->hash);
658
Asias He433fc582016-07-28 15:36:34 +0100659 vsock->guest_cid = guest_cid;
Zha Bin7fbe0782019-01-08 16:07:03 +0800660 hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
Stefan Hajnoczi6db3d8d2018-11-05 17:33:22 +0000661 mutex_unlock(&vhost_vsock_mutex);
Asias He433fc582016-07-28 15:36:34 +0100662
663 return 0;
664}
665
666static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
667{
668 struct vhost_virtqueue *vq;
669 int i;
670
671 if (features & ~VHOST_VSOCK_FEATURES)
672 return -EOPNOTSUPP;
673
674 mutex_lock(&vsock->dev.mutex);
675 if ((features & (1 << VHOST_F_LOG_ALL)) &&
676 !vhost_log_access_ok(&vsock->dev)) {
677 mutex_unlock(&vsock->dev.mutex);
678 return -EFAULT;
679 }
680
681 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
682 vq = &vsock->vqs[i];
683 mutex_lock(&vq->mutex);
684 vq->acked_features = features;
685 mutex_unlock(&vq->mutex);
686 }
687 mutex_unlock(&vsock->dev.mutex);
688 return 0;
689}
690
691static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
692 unsigned long arg)
693{
694 struct vhost_vsock *vsock = f->private_data;
695 void __user *argp = (void __user *)arg;
696 u64 guest_cid;
697 u64 features;
698 int start;
699 int r;
700
701 switch (ioctl) {
702 case VHOST_VSOCK_SET_GUEST_CID:
703 if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
704 return -EFAULT;
705 return vhost_vsock_set_cid(vsock, guest_cid);
706 case VHOST_VSOCK_SET_RUNNING:
707 if (copy_from_user(&start, argp, sizeof(start)))
708 return -EFAULT;
709 if (start)
710 return vhost_vsock_start(vsock);
711 else
712 return vhost_vsock_stop(vsock);
713 case VHOST_GET_FEATURES:
714 features = VHOST_VSOCK_FEATURES;
715 if (copy_to_user(argp, &features, sizeof(features)))
716 return -EFAULT;
717 return 0;
718 case VHOST_SET_FEATURES:
719 if (copy_from_user(&features, argp, sizeof(features)))
720 return -EFAULT;
721 return vhost_vsock_set_features(vsock, features);
722 default:
723 mutex_lock(&vsock->dev.mutex);
724 r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
725 if (r == -ENOIOCTLCMD)
726 r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
727 else
728 vhost_vsock_flush(vsock);
729 mutex_unlock(&vsock->dev.mutex);
730 return r;
731 }
732}
733
Sonny Raodc32bb62018-03-14 14:36:25 -0700734#ifdef CONFIG_COMPAT
735static long vhost_vsock_dev_compat_ioctl(struct file *f, unsigned int ioctl,
736 unsigned long arg)
737{
738 return vhost_vsock_dev_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
739}
740#endif
741
Asias He433fc582016-07-28 15:36:34 +0100742static const struct file_operations vhost_vsock_fops = {
743 .owner = THIS_MODULE,
744 .open = vhost_vsock_dev_open,
745 .release = vhost_vsock_dev_release,
746 .llseek = noop_llseek,
747 .unlocked_ioctl = vhost_vsock_dev_ioctl,
Sonny Raodc32bb62018-03-14 14:36:25 -0700748#ifdef CONFIG_COMPAT
749 .compat_ioctl = vhost_vsock_dev_compat_ioctl,
750#endif
Asias He433fc582016-07-28 15:36:34 +0100751};
752
753static struct miscdevice vhost_vsock_misc = {
Stefan Hajnoczif4660cc2017-05-10 10:19:18 -0400754 .minor = VHOST_VSOCK_MINOR,
Asias He433fc582016-07-28 15:36:34 +0100755 .name = "vhost-vsock",
756 .fops = &vhost_vsock_fops,
757};
758
759static struct virtio_transport vhost_transport = {
760 .transport = {
761 .get_local_cid = vhost_transport_get_local_cid,
762
763 .init = virtio_transport_do_socket_init,
764 .destruct = virtio_transport_destruct,
765 .release = virtio_transport_release,
766 .connect = virtio_transport_connect,
767 .shutdown = virtio_transport_shutdown,
Peng Tao16320f32017-03-15 09:32:15 +0800768 .cancel_pkt = vhost_transport_cancel_pkt,
Asias He433fc582016-07-28 15:36:34 +0100769
770 .dgram_enqueue = virtio_transport_dgram_enqueue,
771 .dgram_dequeue = virtio_transport_dgram_dequeue,
772 .dgram_bind = virtio_transport_dgram_bind,
773 .dgram_allow = virtio_transport_dgram_allow,
774
775 .stream_enqueue = virtio_transport_stream_enqueue,
776 .stream_dequeue = virtio_transport_stream_dequeue,
777 .stream_has_data = virtio_transport_stream_has_data,
778 .stream_has_space = virtio_transport_stream_has_space,
779 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
780 .stream_is_active = virtio_transport_stream_is_active,
781 .stream_allow = virtio_transport_stream_allow,
782
783 .notify_poll_in = virtio_transport_notify_poll_in,
784 .notify_poll_out = virtio_transport_notify_poll_out,
785 .notify_recv_init = virtio_transport_notify_recv_init,
786 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
787 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
788 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
789 .notify_send_init = virtio_transport_notify_send_init,
790 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
791 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
792 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
793
794 .set_buffer_size = virtio_transport_set_buffer_size,
795 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
796 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
797 .get_buffer_size = virtio_transport_get_buffer_size,
798 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
799 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
800 },
801
802 .send_pkt = vhost_transport_send_pkt,
803};
804
805static int __init vhost_vsock_init(void)
806{
807 int ret;
808
809 ret = vsock_core_init(&vhost_transport.transport);
810 if (ret < 0)
811 return ret;
812 return misc_register(&vhost_vsock_misc);
813};
814
815static void __exit vhost_vsock_exit(void)
816{
817 misc_deregister(&vhost_vsock_misc);
818 vsock_core_exit();
819};
820
821module_init(vhost_vsock_init);
822module_exit(vhost_vsock_exit);
823MODULE_LICENSE("GPL v2");
824MODULE_AUTHOR("Asias He");
825MODULE_DESCRIPTION("vhost transport for vsock ");
Stefan Hajnoczif4660cc2017-05-10 10:19:18 -0400826MODULE_ALIAS_MISCDEV(VHOST_VSOCK_MINOR);
827MODULE_ALIAS("devname:vhost-vsock");