mtd: nand: brcmnand: Check flash #WP pin status before nand erase/program
[linux/fpc-iii.git] / drivers / vhost / vsock.c
blobe3fad302b4fb2ea2ffc70a769b4f35f1a122e2b5
1 /*
2 * vhost transport for vsock
4 * Copyright (C) 2013-2015 Red Hat, Inc.
5 * Author: Asias He <asias@redhat.com>
6 * Stefan Hajnoczi <stefanha@redhat.com>
8 * This work is licensed under the terms of the GNU GPL, version 2.
9 */
10 #include <linux/miscdevice.h>
11 #include <linux/atomic.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/vmalloc.h>
15 #include <net/sock.h>
16 #include <linux/virtio_vsock.h>
17 #include <linux/vhost.h>
19 #include <net/af_vsock.h>
20 #include "vhost.h"
22 #define VHOST_VSOCK_DEFAULT_HOST_CID 2
24 enum {
25 VHOST_VSOCK_FEATURES = VHOST_FEATURES,
28 /* Used to track all the vhost_vsock instances on the system. */
29 static DEFINE_SPINLOCK(vhost_vsock_lock);
30 static LIST_HEAD(vhost_vsock_list);
32 struct vhost_vsock {
33 struct vhost_dev dev;
34 struct vhost_virtqueue vqs[2];
36 /* Link to global vhost_vsock_list, protected by vhost_vsock_lock */
37 struct list_head list;
39 struct vhost_work send_pkt_work;
40 spinlock_t send_pkt_list_lock;
41 struct list_head send_pkt_list; /* host->guest pending packets */
43 atomic_t queued_replies;
45 u32 guest_cid;
48 static u32 vhost_transport_get_local_cid(void)
50 return VHOST_VSOCK_DEFAULT_HOST_CID;
53 static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)
55 struct vhost_vsock *vsock;
57 spin_lock_bh(&vhost_vsock_lock);
58 list_for_each_entry(vsock, &vhost_vsock_list, list) {
59 u32 other_cid = vsock->guest_cid;
61 /* Skip instances that have no CID yet */
62 if (other_cid == 0)
63 continue;
65 if (other_cid == guest_cid) {
66 spin_unlock_bh(&vhost_vsock_lock);
67 return vsock;
70 spin_unlock_bh(&vhost_vsock_lock);
72 return NULL;
75 static void
76 vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
77 struct vhost_virtqueue *vq)
79 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
80 bool added = false;
81 bool restart_tx = false;
83 mutex_lock(&vq->mutex);
85 if (!vq->private_data)
86 goto out;
88 /* Avoid further vmexits, we're already processing the virtqueue */
89 vhost_disable_notify(&vsock->dev, vq);
91 for (;;) {
92 struct virtio_vsock_pkt *pkt;
93 struct iov_iter iov_iter;
94 unsigned out, in;
95 size_t nbytes;
96 size_t len;
97 int head;
99 spin_lock_bh(&vsock->send_pkt_list_lock);
100 if (list_empty(&vsock->send_pkt_list)) {
101 spin_unlock_bh(&vsock->send_pkt_list_lock);
102 vhost_enable_notify(&vsock->dev, vq);
103 break;
106 pkt = list_first_entry(&vsock->send_pkt_list,
107 struct virtio_vsock_pkt, list);
108 list_del_init(&pkt->list);
109 spin_unlock_bh(&vsock->send_pkt_list_lock);
111 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
112 &out, &in, NULL, NULL);
113 if (head < 0) {
114 spin_lock_bh(&vsock->send_pkt_list_lock);
115 list_add(&pkt->list, &vsock->send_pkt_list);
116 spin_unlock_bh(&vsock->send_pkt_list_lock);
117 break;
120 if (head == vq->num) {
121 spin_lock_bh(&vsock->send_pkt_list_lock);
122 list_add(&pkt->list, &vsock->send_pkt_list);
123 spin_unlock_bh(&vsock->send_pkt_list_lock);
125 /* We cannot finish yet if more buffers snuck in while
126 * re-enabling notify.
128 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
129 vhost_disable_notify(&vsock->dev, vq);
130 continue;
132 break;
135 if (out) {
136 virtio_transport_free_pkt(pkt);
137 vq_err(vq, "Expected 0 output buffers, got %u\n", out);
138 break;
141 len = iov_length(&vq->iov[out], in);
142 iov_iter_init(&iov_iter, READ, &vq->iov[out], in, len);
144 nbytes = copy_to_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
145 if (nbytes != sizeof(pkt->hdr)) {
146 virtio_transport_free_pkt(pkt);
147 vq_err(vq, "Faulted on copying pkt hdr\n");
148 break;
151 nbytes = copy_to_iter(pkt->buf, pkt->len, &iov_iter);
152 if (nbytes != pkt->len) {
153 virtio_transport_free_pkt(pkt);
154 vq_err(vq, "Faulted on copying pkt buf\n");
155 break;
158 vhost_add_used(vq, head, sizeof(pkt->hdr) + pkt->len);
159 added = true;
161 if (pkt->reply) {
162 int val;
164 val = atomic_dec_return(&vsock->queued_replies);
166 /* Do we have resources to resume tx processing? */
167 if (val + 1 == tx_vq->num)
168 restart_tx = true;
171 virtio_transport_free_pkt(pkt);
173 if (added)
174 vhost_signal(&vsock->dev, vq);
176 out:
177 mutex_unlock(&vq->mutex);
179 if (restart_tx)
180 vhost_poll_queue(&tx_vq->poll);
183 static void vhost_transport_send_pkt_work(struct vhost_work *work)
185 struct vhost_virtqueue *vq;
186 struct vhost_vsock *vsock;
188 vsock = container_of(work, struct vhost_vsock, send_pkt_work);
189 vq = &vsock->vqs[VSOCK_VQ_RX];
191 vhost_transport_do_send_pkt(vsock, vq);
194 static int
195 vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
197 struct vhost_vsock *vsock;
198 struct vhost_virtqueue *vq;
199 int len = pkt->len;
201 /* Find the vhost_vsock according to guest context id */
202 vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
203 if (!vsock) {
204 virtio_transport_free_pkt(pkt);
205 return -ENODEV;
208 vq = &vsock->vqs[VSOCK_VQ_RX];
210 if (pkt->reply)
211 atomic_inc(&vsock->queued_replies);
213 spin_lock_bh(&vsock->send_pkt_list_lock);
214 list_add_tail(&pkt->list, &vsock->send_pkt_list);
215 spin_unlock_bh(&vsock->send_pkt_list_lock);
217 vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
218 return len;
221 static struct virtio_vsock_pkt *
222 vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
223 unsigned int out, unsigned int in)
225 struct virtio_vsock_pkt *pkt;
226 struct iov_iter iov_iter;
227 size_t nbytes;
228 size_t len;
230 if (in != 0) {
231 vq_err(vq, "Expected 0 input buffers, got %u\n", in);
232 return NULL;
235 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
236 if (!pkt)
237 return NULL;
239 len = iov_length(vq->iov, out);
240 iov_iter_init(&iov_iter, WRITE, vq->iov, out, len);
242 nbytes = copy_from_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
243 if (nbytes != sizeof(pkt->hdr)) {
244 vq_err(vq, "Expected %zu bytes for pkt->hdr, got %zu bytes\n",
245 sizeof(pkt->hdr), nbytes);
246 kfree(pkt);
247 return NULL;
250 if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
251 pkt->len = le32_to_cpu(pkt->hdr.len);
253 /* No payload */
254 if (!pkt->len)
255 return pkt;
257 /* The pkt is too big */
258 if (pkt->len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
259 kfree(pkt);
260 return NULL;
263 pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
264 if (!pkt->buf) {
265 kfree(pkt);
266 return NULL;
269 nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
270 if (nbytes != pkt->len) {
271 vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
272 pkt->len, nbytes);
273 virtio_transport_free_pkt(pkt);
274 return NULL;
277 return pkt;
280 /* Is there space left for replies to rx packets? */
281 static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
283 struct vhost_virtqueue *vq = &vsock->vqs[VSOCK_VQ_TX];
284 int val;
286 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
287 val = atomic_read(&vsock->queued_replies);
289 return val < vq->num;
292 static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
294 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
295 poll.work);
296 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
297 dev);
298 struct virtio_vsock_pkt *pkt;
299 int head;
300 unsigned int out, in;
301 bool added = false;
303 mutex_lock(&vq->mutex);
305 if (!vq->private_data)
306 goto out;
308 vhost_disable_notify(&vsock->dev, vq);
309 for (;;) {
310 u32 len;
312 if (!vhost_vsock_more_replies(vsock)) {
313 /* Stop tx until the device processes already
314 * pending replies. Leave tx virtqueue
315 * callbacks disabled.
317 goto no_more_replies;
320 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
321 &out, &in, NULL, NULL);
322 if (head < 0)
323 break;
325 if (head == vq->num) {
326 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
327 vhost_disable_notify(&vsock->dev, vq);
328 continue;
330 break;
333 pkt = vhost_vsock_alloc_pkt(vq, out, in);
334 if (!pkt) {
335 vq_err(vq, "Faulted on pkt\n");
336 continue;
339 len = pkt->len;
341 /* Only accept correctly addressed packets */
342 if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid)
343 virtio_transport_recv_pkt(pkt);
344 else
345 virtio_transport_free_pkt(pkt);
347 vhost_add_used(vq, head, sizeof(pkt->hdr) + len);
348 added = true;
351 no_more_replies:
352 if (added)
353 vhost_signal(&vsock->dev, vq);
355 out:
356 mutex_unlock(&vq->mutex);
359 static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
361 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
362 poll.work);
363 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
364 dev);
366 vhost_transport_do_send_pkt(vsock, vq);
369 static int vhost_vsock_start(struct vhost_vsock *vsock)
371 struct vhost_virtqueue *vq;
372 size_t i;
373 int ret;
375 mutex_lock(&vsock->dev.mutex);
377 ret = vhost_dev_check_owner(&vsock->dev);
378 if (ret)
379 goto err;
381 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
382 vq = &vsock->vqs[i];
384 mutex_lock(&vq->mutex);
386 if (!vhost_vq_access_ok(vq)) {
387 ret = -EFAULT;
388 goto err_vq;
391 if (!vq->private_data) {
392 vq->private_data = vsock;
393 ret = vhost_vq_init_access(vq);
394 if (ret)
395 goto err_vq;
398 mutex_unlock(&vq->mutex);
401 mutex_unlock(&vsock->dev.mutex);
402 return 0;
404 err_vq:
405 vq->private_data = NULL;
406 mutex_unlock(&vq->mutex);
408 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
409 vq = &vsock->vqs[i];
411 mutex_lock(&vq->mutex);
412 vq->private_data = NULL;
413 mutex_unlock(&vq->mutex);
415 err:
416 mutex_unlock(&vsock->dev.mutex);
417 return ret;
420 static int vhost_vsock_stop(struct vhost_vsock *vsock)
422 size_t i;
423 int ret;
425 mutex_lock(&vsock->dev.mutex);
427 ret = vhost_dev_check_owner(&vsock->dev);
428 if (ret)
429 goto err;
431 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
432 struct vhost_virtqueue *vq = &vsock->vqs[i];
434 mutex_lock(&vq->mutex);
435 vq->private_data = NULL;
436 mutex_unlock(&vq->mutex);
439 err:
440 mutex_unlock(&vsock->dev.mutex);
441 return ret;
444 static void vhost_vsock_free(struct vhost_vsock *vsock)
446 kvfree(vsock);
449 static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
451 struct vhost_virtqueue **vqs;
452 struct vhost_vsock *vsock;
453 int ret;
455 /* This struct is large and allocation could fail, fall back to vmalloc
456 * if there is no other way.
458 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
459 if (!vsock) {
460 vsock = vmalloc(sizeof(*vsock));
461 if (!vsock)
462 return -ENOMEM;
465 vqs = kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL);
466 if (!vqs) {
467 ret = -ENOMEM;
468 goto out;
471 atomic_set(&vsock->queued_replies, 0);
473 vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
474 vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
475 vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
476 vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
478 vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs));
480 file->private_data = vsock;
481 spin_lock_init(&vsock->send_pkt_list_lock);
482 INIT_LIST_HEAD(&vsock->send_pkt_list);
483 vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
485 spin_lock_bh(&vhost_vsock_lock);
486 list_add_tail(&vsock->list, &vhost_vsock_list);
487 spin_unlock_bh(&vhost_vsock_lock);
488 return 0;
490 out:
491 vhost_vsock_free(vsock);
492 return ret;
495 static void vhost_vsock_flush(struct vhost_vsock *vsock)
497 int i;
499 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
500 if (vsock->vqs[i].handle_kick)
501 vhost_poll_flush(&vsock->vqs[i].poll);
502 vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
505 static void vhost_vsock_reset_orphans(struct sock *sk)
507 struct vsock_sock *vsk = vsock_sk(sk);
509 /* vmci_transport.c doesn't take sk_lock here either. At least we're
510 * under vsock_table_lock so the sock cannot disappear while we're
511 * executing.
514 if (!vhost_vsock_get(vsk->remote_addr.svm_cid)) {
515 sock_set_flag(sk, SOCK_DONE);
516 vsk->peer_shutdown = SHUTDOWN_MASK;
517 sk->sk_state = SS_UNCONNECTED;
518 sk->sk_err = ECONNRESET;
519 sk->sk_error_report(sk);
523 static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
525 struct vhost_vsock *vsock = file->private_data;
527 spin_lock_bh(&vhost_vsock_lock);
528 list_del(&vsock->list);
529 spin_unlock_bh(&vhost_vsock_lock);
531 /* Iterating over all connections for all CIDs to find orphans is
532 * inefficient. Room for improvement here. */
533 vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
535 vhost_vsock_stop(vsock);
536 vhost_vsock_flush(vsock);
537 vhost_dev_stop(&vsock->dev);
539 spin_lock_bh(&vsock->send_pkt_list_lock);
540 while (!list_empty(&vsock->send_pkt_list)) {
541 struct virtio_vsock_pkt *pkt;
543 pkt = list_first_entry(&vsock->send_pkt_list,
544 struct virtio_vsock_pkt, list);
545 list_del_init(&pkt->list);
546 virtio_transport_free_pkt(pkt);
548 spin_unlock_bh(&vsock->send_pkt_list_lock);
550 vhost_dev_cleanup(&vsock->dev, false);
551 kfree(vsock->dev.vqs);
552 vhost_vsock_free(vsock);
553 return 0;
556 static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
558 struct vhost_vsock *other;
560 /* Refuse reserved CIDs */
561 if (guest_cid <= VMADDR_CID_HOST ||
562 guest_cid == U32_MAX)
563 return -EINVAL;
565 /* 64-bit CIDs are not yet supported */
566 if (guest_cid > U32_MAX)
567 return -EINVAL;
569 /* Refuse if CID is already in use */
570 other = vhost_vsock_get(guest_cid);
571 if (other && other != vsock)
572 return -EADDRINUSE;
574 spin_lock_bh(&vhost_vsock_lock);
575 vsock->guest_cid = guest_cid;
576 spin_unlock_bh(&vhost_vsock_lock);
578 return 0;
581 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
583 struct vhost_virtqueue *vq;
584 int i;
586 if (features & ~VHOST_VSOCK_FEATURES)
587 return -EOPNOTSUPP;
589 mutex_lock(&vsock->dev.mutex);
590 if ((features & (1 << VHOST_F_LOG_ALL)) &&
591 !vhost_log_access_ok(&vsock->dev)) {
592 mutex_unlock(&vsock->dev.mutex);
593 return -EFAULT;
596 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
597 vq = &vsock->vqs[i];
598 mutex_lock(&vq->mutex);
599 vq->acked_features = features;
600 mutex_unlock(&vq->mutex);
602 mutex_unlock(&vsock->dev.mutex);
603 return 0;
606 static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
607 unsigned long arg)
609 struct vhost_vsock *vsock = f->private_data;
610 void __user *argp = (void __user *)arg;
611 u64 guest_cid;
612 u64 features;
613 int start;
614 int r;
616 switch (ioctl) {
617 case VHOST_VSOCK_SET_GUEST_CID:
618 if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
619 return -EFAULT;
620 return vhost_vsock_set_cid(vsock, guest_cid);
621 case VHOST_VSOCK_SET_RUNNING:
622 if (copy_from_user(&start, argp, sizeof(start)))
623 return -EFAULT;
624 if (start)
625 return vhost_vsock_start(vsock);
626 else
627 return vhost_vsock_stop(vsock);
628 case VHOST_GET_FEATURES:
629 features = VHOST_VSOCK_FEATURES;
630 if (copy_to_user(argp, &features, sizeof(features)))
631 return -EFAULT;
632 return 0;
633 case VHOST_SET_FEATURES:
634 if (copy_from_user(&features, argp, sizeof(features)))
635 return -EFAULT;
636 return vhost_vsock_set_features(vsock, features);
637 default:
638 mutex_lock(&vsock->dev.mutex);
639 r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
640 if (r == -ENOIOCTLCMD)
641 r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
642 else
643 vhost_vsock_flush(vsock);
644 mutex_unlock(&vsock->dev.mutex);
645 return r;
649 static const struct file_operations vhost_vsock_fops = {
650 .owner = THIS_MODULE,
651 .open = vhost_vsock_dev_open,
652 .release = vhost_vsock_dev_release,
653 .llseek = noop_llseek,
654 .unlocked_ioctl = vhost_vsock_dev_ioctl,
657 static struct miscdevice vhost_vsock_misc = {
658 .minor = MISC_DYNAMIC_MINOR,
659 .name = "vhost-vsock",
660 .fops = &vhost_vsock_fops,
663 static struct virtio_transport vhost_transport = {
664 .transport = {
665 .get_local_cid = vhost_transport_get_local_cid,
667 .init = virtio_transport_do_socket_init,
668 .destruct = virtio_transport_destruct,
669 .release = virtio_transport_release,
670 .connect = virtio_transport_connect,
671 .shutdown = virtio_transport_shutdown,
673 .dgram_enqueue = virtio_transport_dgram_enqueue,
674 .dgram_dequeue = virtio_transport_dgram_dequeue,
675 .dgram_bind = virtio_transport_dgram_bind,
676 .dgram_allow = virtio_transport_dgram_allow,
678 .stream_enqueue = virtio_transport_stream_enqueue,
679 .stream_dequeue = virtio_transport_stream_dequeue,
680 .stream_has_data = virtio_transport_stream_has_data,
681 .stream_has_space = virtio_transport_stream_has_space,
682 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
683 .stream_is_active = virtio_transport_stream_is_active,
684 .stream_allow = virtio_transport_stream_allow,
686 .notify_poll_in = virtio_transport_notify_poll_in,
687 .notify_poll_out = virtio_transport_notify_poll_out,
688 .notify_recv_init = virtio_transport_notify_recv_init,
689 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
690 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
691 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
692 .notify_send_init = virtio_transport_notify_send_init,
693 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
694 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
695 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
697 .set_buffer_size = virtio_transport_set_buffer_size,
698 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
699 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
700 .get_buffer_size = virtio_transport_get_buffer_size,
701 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
702 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
705 .send_pkt = vhost_transport_send_pkt,
708 static int __init vhost_vsock_init(void)
710 int ret;
712 ret = vsock_core_init(&vhost_transport.transport);
713 if (ret < 0)
714 return ret;
715 return misc_register(&vhost_vsock_misc);
718 static void __exit vhost_vsock_exit(void)
720 misc_deregister(&vhost_vsock_misc);
721 vsock_core_exit();
724 module_init(vhost_vsock_init);
725 module_exit(vhost_vsock_exit);
726 MODULE_LICENSE("GPL v2");
727 MODULE_AUTHOR("Asias He");
728 MODULE_DESCRIPTION("vhost transport for vsock ");