2 * (c) 2017 Stefano Stabellini <stefano@aporeto.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/module.h>
16 #include <linux/net.h>
17 #include <linux/socket.h>
21 #include <xen/events.h>
22 #include <xen/grant_table.h>
24 #include <xen/xenbus.h>
25 #include <xen/interface/io/pvcalls.h>
27 #include "pvcalls-front.h"
29 #define PVCALLS_INVALID_ID UINT_MAX
30 #define PVCALLS_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
31 #define PVCALLS_NR_RSP_PER_RING __CONST_RING_SIZE(xen_pvcalls, XEN_PAGE_SIZE)
32 #define PVCALLS_FRONT_MAX_SPIN 5000
34 struct pvcalls_bedata {
35 struct xen_pvcalls_front_ring ring;
39 struct list_head socket_mappings;
40 spinlock_t socket_lock;
42 wait_queue_head_t inflight_req;
43 struct xen_pvcalls_response rsp[PVCALLS_NR_RSP_PER_RING];
45 /* Only one front/back connection supported. */
46 static struct xenbus_device *pvcalls_front_dev;
47 static atomic_t pvcalls_refcount;
49 /* first increment refcount, then proceed */
50 #define pvcalls_enter() { \
51 atomic_inc(&pvcalls_refcount); \
54 /* first complete other operations, then decrement refcount */
55 #define pvcalls_exit() { \
56 atomic_dec(&pvcalls_refcount); \
61 struct list_head list;
68 struct pvcalls_data_intf *ring;
69 struct pvcalls_data data;
70 struct mutex in_mutex;
71 struct mutex out_mutex;
73 wait_queue_head_t inflight_conn_req;
77 * Socket status, needs to be 64-bit aligned due to the
78 * test_and_* functions which have this requirement on arm64.
80 #define PVCALLS_STATUS_UNINITALIZED 0
81 #define PVCALLS_STATUS_BIND 1
82 #define PVCALLS_STATUS_LISTEN 2
83 uint8_t status __attribute__((aligned(8)));
85 * Internal state-machine flags.
86 * Only one accept operation can be inflight for a socket.
87 * Only one poll operation can be inflight for a given socket.
88 * flags needs to be 64-bit aligned due to the test_and_*
89 * functions which have this requirement on arm64.
91 #define PVCALLS_FLAG_ACCEPT_INFLIGHT 0
92 #define PVCALLS_FLAG_POLL_INFLIGHT 1
93 #define PVCALLS_FLAG_POLL_RET 2
94 uint8_t flags __attribute__((aligned(8)));
95 uint32_t inflight_req_id;
96 struct sock_mapping *accept_map;
97 wait_queue_head_t inflight_accept_req;
102 static inline struct sock_mapping *pvcalls_enter_sock(struct socket *sock)
104 struct sock_mapping *map;
106 if (!pvcalls_front_dev ||
107 dev_get_drvdata(&pvcalls_front_dev->dev) == NULL)
108 return ERR_PTR(-ENOTCONN);
110 map = (struct sock_mapping *)sock->sk->sk_send_head;
112 return ERR_PTR(-ENOTSOCK);
115 atomic_inc(&map->refcount);
119 static inline void pvcalls_exit_sock(struct socket *sock)
121 struct sock_mapping *map;
123 map = (struct sock_mapping *)sock->sk->sk_send_head;
124 atomic_dec(&map->refcount);
128 static inline int get_request(struct pvcalls_bedata *bedata, int *req_id)
130 *req_id = bedata->ring.req_prod_pvt & (RING_SIZE(&bedata->ring) - 1);
131 if (RING_FULL(&bedata->ring) ||
132 bedata->rsp[*req_id].req_id != PVCALLS_INVALID_ID)
137 static bool pvcalls_front_write_todo(struct sock_mapping *map)
139 struct pvcalls_data_intf *intf = map->active.ring;
140 RING_IDX cons, prod, size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
143 error = intf->out_error;
144 if (error == -ENOTCONN)
149 cons = intf->out_cons;
150 prod = intf->out_prod;
151 return !!(size - pvcalls_queued(prod, cons, size));
154 static bool pvcalls_front_read_todo(struct sock_mapping *map)
156 struct pvcalls_data_intf *intf = map->active.ring;
160 cons = intf->in_cons;
161 prod = intf->in_prod;
162 error = intf->in_error;
163 return (error != 0 ||
164 pvcalls_queued(prod, cons,
165 XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER)) != 0);
168 static irqreturn_t pvcalls_front_event_handler(int irq, void *dev_id)
170 struct xenbus_device *dev = dev_id;
171 struct pvcalls_bedata *bedata;
172 struct xen_pvcalls_response *rsp;
174 int req_id = 0, more = 0, done = 0;
180 bedata = dev_get_drvdata(&dev->dev);
181 if (bedata == NULL) {
187 while (RING_HAS_UNCONSUMED_RESPONSES(&bedata->ring)) {
188 rsp = RING_GET_RESPONSE(&bedata->ring, bedata->ring.rsp_cons);
190 req_id = rsp->req_id;
191 if (rsp->cmd == PVCALLS_POLL) {
192 struct sock_mapping *map = (struct sock_mapping *)(uintptr_t)
195 clear_bit(PVCALLS_FLAG_POLL_INFLIGHT,
196 (void *)&map->passive.flags);
198 * clear INFLIGHT, then set RET. It pairs with
199 * the checks at the beginning of
200 * pvcalls_front_poll_passive.
203 set_bit(PVCALLS_FLAG_POLL_RET,
204 (void *)&map->passive.flags);
206 dst = (uint8_t *)&bedata->rsp[req_id] +
208 src = (uint8_t *)rsp + sizeof(rsp->req_id);
209 memcpy(dst, src, sizeof(*rsp) - sizeof(rsp->req_id));
211 * First copy the rest of the data, then req_id. It is
212 * paired with the barrier when accessing bedata->rsp.
215 bedata->rsp[req_id].req_id = req_id;
219 bedata->ring.rsp_cons++;
222 RING_FINAL_CHECK_FOR_RESPONSES(&bedata->ring, more);
226 wake_up(&bedata->inflight_req);
231 static void pvcalls_front_free_map(struct pvcalls_bedata *bedata,
232 struct sock_mapping *map)
236 unbind_from_irqhandler(map->active.irq, map);
238 spin_lock(&bedata->socket_lock);
239 if (!list_empty(&map->list))
240 list_del_init(&map->list);
241 spin_unlock(&bedata->socket_lock);
243 for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
244 gnttab_end_foreign_access(map->active.ring->ref[i], 0, 0);
245 gnttab_end_foreign_access(map->active.ref, 0, 0);
246 free_page((unsigned long)map->active.ring);
251 static irqreturn_t pvcalls_front_conn_handler(int irq, void *sock_map)
253 struct sock_mapping *map = sock_map;
258 wake_up_interruptible(&map->active.inflight_conn_req);
263 int pvcalls_front_socket(struct socket *sock)
265 struct pvcalls_bedata *bedata;
266 struct sock_mapping *map = NULL;
267 struct xen_pvcalls_request *req;
268 int notify, req_id, ret;
271 * PVCalls only supports domain AF_INET,
272 * type SOCK_STREAM and protocol 0 sockets for now.
274 * Check socket type here, AF_INET and protocol checks are done
277 if (sock->type != SOCK_STREAM)
281 if (!pvcalls_front_dev) {
285 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
287 map = kzalloc(sizeof(*map), GFP_KERNEL);
293 spin_lock(&bedata->socket_lock);
295 ret = get_request(bedata, &req_id);
298 spin_unlock(&bedata->socket_lock);
304 * sock->sk->sk_send_head is not used for ip sockets: reuse the
305 * field to store a pointer to the struct sock_mapping
306 * corresponding to the socket. This way, we can easily get the
307 * struct sock_mapping from the struct socket.
309 sock->sk->sk_send_head = (void *)map;
310 list_add_tail(&map->list, &bedata->socket_mappings);
312 req = RING_GET_REQUEST(&bedata->ring, req_id);
313 req->req_id = req_id;
314 req->cmd = PVCALLS_SOCKET;
315 req->u.socket.id = (uintptr_t) map;
316 req->u.socket.domain = AF_INET;
317 req->u.socket.type = SOCK_STREAM;
318 req->u.socket.protocol = IPPROTO_IP;
320 bedata->ring.req_prod_pvt++;
321 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
322 spin_unlock(&bedata->socket_lock);
324 notify_remote_via_irq(bedata->irq);
326 wait_event(bedata->inflight_req,
327 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
329 /* read req_id, then the content */
331 ret = bedata->rsp[req_id].ret;
332 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
338 static int create_active(struct sock_mapping *map, int *evtchn)
341 int ret = -ENOMEM, irq = -1, i;
344 init_waitqueue_head(&map->active.inflight_conn_req);
346 map->active.ring = (struct pvcalls_data_intf *)
347 __get_free_page(GFP_KERNEL | __GFP_ZERO);
348 if (map->active.ring == NULL)
350 map->active.ring->ring_order = PVCALLS_RING_ORDER;
351 bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
355 for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
356 map->active.ring->ref[i] = gnttab_grant_foreign_access(
357 pvcalls_front_dev->otherend_id,
358 pfn_to_gfn(virt_to_pfn(bytes) + i), 0);
360 map->active.ref = gnttab_grant_foreign_access(
361 pvcalls_front_dev->otherend_id,
362 pfn_to_gfn(virt_to_pfn((void *)map->active.ring)), 0);
364 map->active.data.in = bytes;
365 map->active.data.out = bytes +
366 XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
368 ret = xenbus_alloc_evtchn(pvcalls_front_dev, evtchn);
371 irq = bind_evtchn_to_irqhandler(*evtchn, pvcalls_front_conn_handler,
372 0, "pvcalls-frontend", map);
378 map->active.irq = irq;
379 map->active_socket = true;
380 mutex_init(&map->active.in_mutex);
381 mutex_init(&map->active.out_mutex);
387 xenbus_free_evtchn(pvcalls_front_dev, *evtchn);
388 free_pages((unsigned long)map->active.data.in, PVCALLS_RING_ORDER);
389 free_page((unsigned long)map->active.ring);
393 int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
394 int addr_len, int flags)
396 struct pvcalls_bedata *bedata;
397 struct sock_mapping *map = NULL;
398 struct xen_pvcalls_request *req;
399 int notify, req_id, ret, evtchn;
401 if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
404 map = pvcalls_enter_sock(sock);
408 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
410 spin_lock(&bedata->socket_lock);
411 ret = get_request(bedata, &req_id);
413 spin_unlock(&bedata->socket_lock);
414 pvcalls_exit_sock(sock);
417 ret = create_active(map, &evtchn);
419 spin_unlock(&bedata->socket_lock);
420 pvcalls_exit_sock(sock);
424 req = RING_GET_REQUEST(&bedata->ring, req_id);
425 req->req_id = req_id;
426 req->cmd = PVCALLS_CONNECT;
427 req->u.connect.id = (uintptr_t)map;
428 req->u.connect.len = addr_len;
429 req->u.connect.flags = flags;
430 req->u.connect.ref = map->active.ref;
431 req->u.connect.evtchn = evtchn;
432 memcpy(req->u.connect.addr, addr, sizeof(*addr));
436 bedata->ring.req_prod_pvt++;
437 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
438 spin_unlock(&bedata->socket_lock);
441 notify_remote_via_irq(bedata->irq);
443 wait_event(bedata->inflight_req,
444 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
446 /* read req_id, then the content */
448 ret = bedata->rsp[req_id].ret;
449 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
450 pvcalls_exit_sock(sock);
454 static int __write_ring(struct pvcalls_data_intf *intf,
455 struct pvcalls_data *data,
456 struct iov_iter *msg_iter,
459 RING_IDX cons, prod, size, masked_prod, masked_cons;
460 RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
463 error = intf->out_error;
466 cons = intf->out_cons;
467 prod = intf->out_prod;
468 /* read indexes before continuing */
471 size = pvcalls_queued(prod, cons, array_size);
472 if (size >= array_size)
474 if (len > array_size - size)
475 len = array_size - size;
477 masked_prod = pvcalls_mask(prod, array_size);
478 masked_cons = pvcalls_mask(cons, array_size);
480 if (masked_prod < masked_cons) {
481 len = copy_from_iter(data->out + masked_prod, len, msg_iter);
483 if (len > array_size - masked_prod) {
484 int ret = copy_from_iter(data->out + masked_prod,
485 array_size - masked_prod, msg_iter);
486 if (ret != array_size - masked_prod) {
490 len = ret + copy_from_iter(data->out, len - ret, msg_iter);
492 len = copy_from_iter(data->out + masked_prod, len, msg_iter);
496 /* write to ring before updating pointer */
498 intf->out_prod += len;
503 int pvcalls_front_sendmsg(struct socket *sock, struct msghdr *msg,
506 struct pvcalls_bedata *bedata;
507 struct sock_mapping *map;
508 int sent, tot_sent = 0;
509 int count = 0, flags;
511 flags = msg->msg_flags;
512 if (flags & (MSG_CONFIRM|MSG_DONTROUTE|MSG_EOR|MSG_OOB))
515 map = pvcalls_enter_sock(sock);
518 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
520 mutex_lock(&map->active.out_mutex);
521 if ((flags & MSG_DONTWAIT) && !pvcalls_front_write_todo(map)) {
522 mutex_unlock(&map->active.out_mutex);
523 pvcalls_exit_sock(sock);
531 sent = __write_ring(map->active.ring,
532 &map->active.data, &msg->msg_iter,
537 notify_remote_via_irq(map->active.irq);
539 if (sent >= 0 && len > 0 && count < PVCALLS_FRONT_MAX_SPIN)
544 mutex_unlock(&map->active.out_mutex);
545 pvcalls_exit_sock(sock);
549 static int __read_ring(struct pvcalls_data_intf *intf,
550 struct pvcalls_data *data,
551 struct iov_iter *msg_iter,
552 size_t len, int flags)
554 RING_IDX cons, prod, size, masked_prod, masked_cons;
555 RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
558 cons = intf->in_cons;
559 prod = intf->in_prod;
560 error = intf->in_error;
561 /* get pointers before reading from the ring */
564 size = pvcalls_queued(prod, cons, array_size);
565 masked_prod = pvcalls_mask(prod, array_size);
566 masked_cons = pvcalls_mask(cons, array_size);
569 return error ?: size;
574 if (masked_prod > masked_cons) {
575 len = copy_to_iter(data->in + masked_cons, len, msg_iter);
577 if (len > (array_size - masked_cons)) {
578 int ret = copy_to_iter(data->in + masked_cons,
579 array_size - masked_cons, msg_iter);
580 if (ret != array_size - masked_cons) {
584 len = ret + copy_to_iter(data->in, len - ret, msg_iter);
586 len = copy_to_iter(data->in + masked_cons, len, msg_iter);
590 /* read data from the ring before increasing the index */
592 if (!(flags & MSG_PEEK))
593 intf->in_cons += len;
598 int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
601 struct pvcalls_bedata *bedata;
603 struct sock_mapping *map;
605 if (flags & (MSG_CMSG_CLOEXEC|MSG_ERRQUEUE|MSG_OOB|MSG_TRUNC))
608 map = pvcalls_enter_sock(sock);
611 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
613 mutex_lock(&map->active.in_mutex);
614 if (len > XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER))
615 len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
617 while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) {
618 wait_event_interruptible(map->active.inflight_conn_req,
619 pvcalls_front_read_todo(map));
621 ret = __read_ring(map->active.ring, &map->active.data,
622 &msg->msg_iter, len, flags);
625 notify_remote_via_irq(map->active.irq);
627 ret = (flags & MSG_DONTWAIT) ? -EAGAIN : 0;
628 if (ret == -ENOTCONN)
631 mutex_unlock(&map->active.in_mutex);
632 pvcalls_exit_sock(sock);
636 int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
638 struct pvcalls_bedata *bedata;
639 struct sock_mapping *map = NULL;
640 struct xen_pvcalls_request *req;
641 int notify, req_id, ret;
643 if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
646 map = pvcalls_enter_sock(sock);
649 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
651 spin_lock(&bedata->socket_lock);
652 ret = get_request(bedata, &req_id);
654 spin_unlock(&bedata->socket_lock);
655 pvcalls_exit_sock(sock);
658 req = RING_GET_REQUEST(&bedata->ring, req_id);
659 req->req_id = req_id;
661 req->cmd = PVCALLS_BIND;
662 req->u.bind.id = (uintptr_t)map;
663 memcpy(req->u.bind.addr, addr, sizeof(*addr));
664 req->u.bind.len = addr_len;
666 init_waitqueue_head(&map->passive.inflight_accept_req);
668 map->active_socket = false;
670 bedata->ring.req_prod_pvt++;
671 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
672 spin_unlock(&bedata->socket_lock);
674 notify_remote_via_irq(bedata->irq);
676 wait_event(bedata->inflight_req,
677 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
679 /* read req_id, then the content */
681 ret = bedata->rsp[req_id].ret;
682 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
684 map->passive.status = PVCALLS_STATUS_BIND;
685 pvcalls_exit_sock(sock);
689 int pvcalls_front_listen(struct socket *sock, int backlog)
691 struct pvcalls_bedata *bedata;
692 struct sock_mapping *map;
693 struct xen_pvcalls_request *req;
694 int notify, req_id, ret;
696 map = pvcalls_enter_sock(sock);
699 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
701 if (map->passive.status != PVCALLS_STATUS_BIND) {
702 pvcalls_exit_sock(sock);
706 spin_lock(&bedata->socket_lock);
707 ret = get_request(bedata, &req_id);
709 spin_unlock(&bedata->socket_lock);
710 pvcalls_exit_sock(sock);
713 req = RING_GET_REQUEST(&bedata->ring, req_id);
714 req->req_id = req_id;
715 req->cmd = PVCALLS_LISTEN;
716 req->u.listen.id = (uintptr_t) map;
717 req->u.listen.backlog = backlog;
719 bedata->ring.req_prod_pvt++;
720 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
721 spin_unlock(&bedata->socket_lock);
723 notify_remote_via_irq(bedata->irq);
725 wait_event(bedata->inflight_req,
726 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
728 /* read req_id, then the content */
730 ret = bedata->rsp[req_id].ret;
731 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
733 map->passive.status = PVCALLS_STATUS_LISTEN;
734 pvcalls_exit_sock(sock);
738 int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
740 struct pvcalls_bedata *bedata;
741 struct sock_mapping *map;
742 struct sock_mapping *map2 = NULL;
743 struct xen_pvcalls_request *req;
744 int notify, req_id, ret, evtchn, nonblock;
746 map = pvcalls_enter_sock(sock);
749 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
751 if (map->passive.status != PVCALLS_STATUS_LISTEN) {
752 pvcalls_exit_sock(sock);
756 nonblock = flags & SOCK_NONBLOCK;
758 * Backend only supports 1 inflight accept request, will return
759 * errors for the others
761 if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
762 (void *)&map->passive.flags)) {
763 req_id = READ_ONCE(map->passive.inflight_req_id);
764 if (req_id != PVCALLS_INVALID_ID &&
765 READ_ONCE(bedata->rsp[req_id].req_id) == req_id) {
766 map2 = map->passive.accept_map;
770 pvcalls_exit_sock(sock);
773 if (wait_event_interruptible(map->passive.inflight_accept_req,
774 !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
775 (void *)&map->passive.flags))) {
776 pvcalls_exit_sock(sock);
781 spin_lock(&bedata->socket_lock);
782 ret = get_request(bedata, &req_id);
784 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
785 (void *)&map->passive.flags);
786 spin_unlock(&bedata->socket_lock);
787 pvcalls_exit_sock(sock);
790 map2 = kzalloc(sizeof(*map2), GFP_ATOMIC);
792 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
793 (void *)&map->passive.flags);
794 spin_unlock(&bedata->socket_lock);
795 pvcalls_exit_sock(sock);
798 ret = create_active(map2, &evtchn);
801 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
802 (void *)&map->passive.flags);
803 spin_unlock(&bedata->socket_lock);
804 pvcalls_exit_sock(sock);
807 list_add_tail(&map2->list, &bedata->socket_mappings);
809 req = RING_GET_REQUEST(&bedata->ring, req_id);
810 req->req_id = req_id;
811 req->cmd = PVCALLS_ACCEPT;
812 req->u.accept.id = (uintptr_t) map;
813 req->u.accept.ref = map2->active.ref;
814 req->u.accept.id_new = (uintptr_t) map2;
815 req->u.accept.evtchn = evtchn;
816 map->passive.accept_map = map2;
818 bedata->ring.req_prod_pvt++;
819 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
820 spin_unlock(&bedata->socket_lock);
822 notify_remote_via_irq(bedata->irq);
823 /* We could check if we have received a response before returning. */
825 WRITE_ONCE(map->passive.inflight_req_id, req_id);
826 pvcalls_exit_sock(sock);
830 if (wait_event_interruptible(bedata->inflight_req,
831 READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) {
832 pvcalls_exit_sock(sock);
835 /* read req_id, then the content */
839 map2->sock = newsock;
840 newsock->sk = kzalloc(sizeof(*newsock->sk), GFP_KERNEL);
842 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
843 map->passive.inflight_req_id = PVCALLS_INVALID_ID;
844 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
845 (void *)&map->passive.flags);
846 pvcalls_front_free_map(bedata, map2);
847 pvcalls_exit_sock(sock);
850 newsock->sk->sk_send_head = (void *)map2;
852 ret = bedata->rsp[req_id].ret;
853 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
854 map->passive.inflight_req_id = PVCALLS_INVALID_ID;
856 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags);
857 wake_up(&map->passive.inflight_accept_req);
859 pvcalls_exit_sock(sock);
863 static __poll_t pvcalls_front_poll_passive(struct file *file,
864 struct pvcalls_bedata *bedata,
865 struct sock_mapping *map,
868 int notify, req_id, ret;
869 struct xen_pvcalls_request *req;
871 if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
872 (void *)&map->passive.flags)) {
873 uint32_t req_id = READ_ONCE(map->passive.inflight_req_id);
875 if (req_id != PVCALLS_INVALID_ID &&
876 READ_ONCE(bedata->rsp[req_id].req_id) == req_id)
877 return EPOLLIN | EPOLLRDNORM;
879 poll_wait(file, &map->passive.inflight_accept_req, wait);
883 if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET,
884 (void *)&map->passive.flags))
885 return EPOLLIN | EPOLLRDNORM;
888 * First check RET, then INFLIGHT. No barriers necessary to
889 * ensure execution ordering because of the conditional
890 * instructions creating control dependencies.
893 if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT,
894 (void *)&map->passive.flags)) {
895 poll_wait(file, &bedata->inflight_req, wait);
899 spin_lock(&bedata->socket_lock);
900 ret = get_request(bedata, &req_id);
902 spin_unlock(&bedata->socket_lock);
905 req = RING_GET_REQUEST(&bedata->ring, req_id);
906 req->req_id = req_id;
907 req->cmd = PVCALLS_POLL;
908 req->u.poll.id = (uintptr_t) map;
910 bedata->ring.req_prod_pvt++;
911 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
912 spin_unlock(&bedata->socket_lock);
914 notify_remote_via_irq(bedata->irq);
916 poll_wait(file, &bedata->inflight_req, wait);
920 static __poll_t pvcalls_front_poll_active(struct file *file,
921 struct pvcalls_bedata *bedata,
922 struct sock_mapping *map,
926 int32_t in_error, out_error;
927 struct pvcalls_data_intf *intf = map->active.ring;
929 out_error = intf->out_error;
930 in_error = intf->in_error;
932 poll_wait(file, &map->active.inflight_conn_req, wait);
933 if (pvcalls_front_write_todo(map))
934 mask |= EPOLLOUT | EPOLLWRNORM;
935 if (pvcalls_front_read_todo(map))
936 mask |= EPOLLIN | EPOLLRDNORM;
937 if (in_error != 0 || out_error != 0)
943 __poll_t pvcalls_front_poll(struct file *file, struct socket *sock,
946 struct pvcalls_bedata *bedata;
947 struct sock_mapping *map;
950 map = pvcalls_enter_sock(sock);
953 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
955 if (map->active_socket)
956 ret = pvcalls_front_poll_active(file, bedata, map, wait);
958 ret = pvcalls_front_poll_passive(file, bedata, map, wait);
959 pvcalls_exit_sock(sock);
963 int pvcalls_front_release(struct socket *sock)
965 struct pvcalls_bedata *bedata;
966 struct sock_mapping *map;
967 int req_id, notify, ret;
968 struct xen_pvcalls_request *req;
970 if (sock->sk == NULL)
973 map = pvcalls_enter_sock(sock);
975 if (PTR_ERR(map) == -ENOTCONN)
980 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
982 spin_lock(&bedata->socket_lock);
983 ret = get_request(bedata, &req_id);
985 spin_unlock(&bedata->socket_lock);
986 pvcalls_exit_sock(sock);
989 sock->sk->sk_send_head = NULL;
991 req = RING_GET_REQUEST(&bedata->ring, req_id);
992 req->req_id = req_id;
993 req->cmd = PVCALLS_RELEASE;
994 req->u.release.id = (uintptr_t)map;
996 bedata->ring.req_prod_pvt++;
997 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
998 spin_unlock(&bedata->socket_lock);
1000 notify_remote_via_irq(bedata->irq);
1002 wait_event(bedata->inflight_req,
1003 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
1005 if (map->active_socket) {
1007 * Set in_error and wake up inflight_conn_req to force
1008 * recvmsg waiters to exit.
1010 map->active.ring->in_error = -EBADF;
1011 wake_up_interruptible(&map->active.inflight_conn_req);
1014 * We need to make sure that sendmsg/recvmsg on this socket have
1015 * not started before we've cleared sk_send_head here. The
1016 * easiest way to guarantee this is to see that no pvcalls
1017 * (other than us) is in progress on this socket.
1019 while (atomic_read(&map->refcount) > 1)
1022 pvcalls_front_free_map(bedata, map);
1024 wake_up(&bedata->inflight_req);
1025 wake_up(&map->passive.inflight_accept_req);
1027 while (atomic_read(&map->refcount) > 1)
1030 spin_lock(&bedata->socket_lock);
1031 list_del(&map->list);
1032 spin_unlock(&bedata->socket_lock);
1033 if (READ_ONCE(map->passive.inflight_req_id) !=
1034 PVCALLS_INVALID_ID) {
1035 pvcalls_front_free_map(bedata,
1036 map->passive.accept_map);
1040 WRITE_ONCE(bedata->rsp[req_id].req_id, PVCALLS_INVALID_ID);
1046 static const struct xenbus_device_id pvcalls_front_ids[] = {
1051 static int pvcalls_front_remove(struct xenbus_device *dev)
1053 struct pvcalls_bedata *bedata;
1054 struct sock_mapping *map = NULL, *n;
1056 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
1057 dev_set_drvdata(&dev->dev, NULL);
1058 pvcalls_front_dev = NULL;
1059 if (bedata->irq >= 0)
1060 unbind_from_irqhandler(bedata->irq, dev);
1062 list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
1063 map->sock->sk->sk_send_head = NULL;
1064 if (map->active_socket) {
1065 map->active.ring->in_error = -EBADF;
1066 wake_up_interruptible(&map->active.inflight_conn_req);
1071 while (atomic_read(&pvcalls_refcount) > 0)
1073 list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
1074 if (map->active_socket) {
1075 /* No need to lock, refcount is 0 */
1076 pvcalls_front_free_map(bedata, map);
1078 list_del(&map->list);
1082 if (bedata->ref != -1)
1083 gnttab_end_foreign_access(bedata->ref, 0, 0);
1084 kfree(bedata->ring.sring);
1086 xenbus_switch_state(dev, XenbusStateClosed);
1090 static int pvcalls_front_probe(struct xenbus_device *dev,
1091 const struct xenbus_device_id *id)
1093 int ret = -ENOMEM, evtchn, i;
1094 unsigned int max_page_order, function_calls, len;
1096 grant_ref_t gref_head = 0;
1097 struct xenbus_transaction xbt;
1098 struct pvcalls_bedata *bedata = NULL;
1099 struct xen_pvcalls_sring *sring;
1101 if (pvcalls_front_dev != NULL) {
1102 dev_err(&dev->dev, "only one PV Calls connection supported\n");
1106 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
1107 if (IS_ERR(versions))
1108 return PTR_ERR(versions);
1111 if (strcmp(versions, "1")) {
1116 max_page_order = xenbus_read_unsigned(dev->otherend,
1117 "max-page-order", 0);
1118 if (max_page_order < PVCALLS_RING_ORDER)
1120 function_calls = xenbus_read_unsigned(dev->otherend,
1121 "function-calls", 0);
1122 /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
1123 if (function_calls != 1)
1125 pr_info("%s max-page-order is %u\n", __func__, max_page_order);
1127 bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL);
1131 dev_set_drvdata(&dev->dev, bedata);
1132 pvcalls_front_dev = dev;
1133 init_waitqueue_head(&bedata->inflight_req);
1134 INIT_LIST_HEAD(&bedata->socket_mappings);
1135 spin_lock_init(&bedata->socket_lock);
1139 for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++)
1140 bedata->rsp[i].req_id = PVCALLS_INVALID_ID;
1142 sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL |
1146 SHARED_RING_INIT(sring);
1147 FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE);
1149 ret = xenbus_alloc_evtchn(dev, &evtchn);
1153 bedata->irq = bind_evtchn_to_irqhandler(evtchn,
1154 pvcalls_front_event_handler,
1155 0, "pvcalls-frontend", dev);
1156 if (bedata->irq < 0) {
1161 ret = gnttab_alloc_grant_references(1, &gref_head);
1164 ret = gnttab_claim_grant_reference(&gref_head);
1168 gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id,
1169 virt_to_gfn((void *)sring), 0);
1172 ret = xenbus_transaction_start(&xbt);
1174 xenbus_dev_fatal(dev, ret, "starting transaction");
1177 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
1180 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref);
1183 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
1187 ret = xenbus_transaction_end(xbt, 0);
1191 xenbus_dev_fatal(dev, ret, "completing transaction");
1194 xenbus_switch_state(dev, XenbusStateInitialised);
1199 xenbus_transaction_end(xbt, 1);
1200 xenbus_dev_fatal(dev, ret, "writing xenstore");
1202 pvcalls_front_remove(dev);
1206 static void pvcalls_front_changed(struct xenbus_device *dev,
1207 enum xenbus_state backend_state)
1209 switch (backend_state) {
1210 case XenbusStateReconfiguring:
1211 case XenbusStateReconfigured:
1212 case XenbusStateInitialising:
1213 case XenbusStateInitialised:
1214 case XenbusStateUnknown:
1217 case XenbusStateInitWait:
1220 case XenbusStateConnected:
1221 xenbus_switch_state(dev, XenbusStateConnected);
1224 case XenbusStateClosed:
1225 if (dev->state == XenbusStateClosed)
1227 /* Missed the backend's CLOSING state */
1229 case XenbusStateClosing:
1230 xenbus_frontend_closed(dev);
1235 static struct xenbus_driver pvcalls_front_driver = {
1236 .ids = pvcalls_front_ids,
1237 .probe = pvcalls_front_probe,
1238 .remove = pvcalls_front_remove,
1239 .otherend_changed = pvcalls_front_changed,
1242 static int __init pvcalls_frontend_init(void)
1247 pr_info("Initialising Xen pvcalls frontend driver\n");
1249 return xenbus_register_frontend(&pvcalls_front_driver);
1252 module_init(pvcalls_frontend_init);
1254 MODULE_DESCRIPTION("Xen PV Calls frontend driver");
1255 MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
1256 MODULE_LICENSE("GPL");