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 kfree(map->active.data.in);
389 kfree(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 */
566 size = pvcalls_queued(prod, cons, array_size);
567 masked_prod = pvcalls_mask(prod, array_size);
568 masked_cons = pvcalls_mask(cons, array_size);
576 if (masked_prod > masked_cons) {
577 len = copy_to_iter(data->in + masked_cons, len, msg_iter);
579 if (len > (array_size - masked_cons)) {
580 int ret = copy_to_iter(data->in + masked_cons,
581 array_size - masked_cons, msg_iter);
582 if (ret != array_size - masked_cons) {
586 len = ret + copy_to_iter(data->in, len - ret, msg_iter);
588 len = copy_to_iter(data->in + masked_cons, len, msg_iter);
592 /* read data from the ring before increasing the index */
594 if (!(flags & MSG_PEEK))
595 intf->in_cons += len;
600 int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
603 struct pvcalls_bedata *bedata;
605 struct sock_mapping *map;
607 if (flags & (MSG_CMSG_CLOEXEC|MSG_ERRQUEUE|MSG_OOB|MSG_TRUNC))
610 map = pvcalls_enter_sock(sock);
613 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
615 mutex_lock(&map->active.in_mutex);
616 if (len > XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER))
617 len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
619 while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) {
620 wait_event_interruptible(map->active.inflight_conn_req,
621 pvcalls_front_read_todo(map));
623 ret = __read_ring(map->active.ring, &map->active.data,
624 &msg->msg_iter, len, flags);
627 notify_remote_via_irq(map->active.irq);
629 ret = (flags & MSG_DONTWAIT) ? -EAGAIN : 0;
630 if (ret == -ENOTCONN)
633 mutex_unlock(&map->active.in_mutex);
634 pvcalls_exit_sock(sock);
638 int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
640 struct pvcalls_bedata *bedata;
641 struct sock_mapping *map = NULL;
642 struct xen_pvcalls_request *req;
643 int notify, req_id, ret;
645 if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
648 map = pvcalls_enter_sock(sock);
651 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
653 spin_lock(&bedata->socket_lock);
654 ret = get_request(bedata, &req_id);
656 spin_unlock(&bedata->socket_lock);
657 pvcalls_exit_sock(sock);
660 req = RING_GET_REQUEST(&bedata->ring, req_id);
661 req->req_id = req_id;
663 req->cmd = PVCALLS_BIND;
664 req->u.bind.id = (uintptr_t)map;
665 memcpy(req->u.bind.addr, addr, sizeof(*addr));
666 req->u.bind.len = addr_len;
668 init_waitqueue_head(&map->passive.inflight_accept_req);
670 map->active_socket = false;
672 bedata->ring.req_prod_pvt++;
673 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
674 spin_unlock(&bedata->socket_lock);
676 notify_remote_via_irq(bedata->irq);
678 wait_event(bedata->inflight_req,
679 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
681 /* read req_id, then the content */
683 ret = bedata->rsp[req_id].ret;
684 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
686 map->passive.status = PVCALLS_STATUS_BIND;
687 pvcalls_exit_sock(sock);
691 int pvcalls_front_listen(struct socket *sock, int backlog)
693 struct pvcalls_bedata *bedata;
694 struct sock_mapping *map;
695 struct xen_pvcalls_request *req;
696 int notify, req_id, ret;
698 map = pvcalls_enter_sock(sock);
701 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
703 if (map->passive.status != PVCALLS_STATUS_BIND) {
704 pvcalls_exit_sock(sock);
708 spin_lock(&bedata->socket_lock);
709 ret = get_request(bedata, &req_id);
711 spin_unlock(&bedata->socket_lock);
712 pvcalls_exit_sock(sock);
715 req = RING_GET_REQUEST(&bedata->ring, req_id);
716 req->req_id = req_id;
717 req->cmd = PVCALLS_LISTEN;
718 req->u.listen.id = (uintptr_t) map;
719 req->u.listen.backlog = backlog;
721 bedata->ring.req_prod_pvt++;
722 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
723 spin_unlock(&bedata->socket_lock);
725 notify_remote_via_irq(bedata->irq);
727 wait_event(bedata->inflight_req,
728 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
730 /* read req_id, then the content */
732 ret = bedata->rsp[req_id].ret;
733 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
735 map->passive.status = PVCALLS_STATUS_LISTEN;
736 pvcalls_exit_sock(sock);
740 int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
742 struct pvcalls_bedata *bedata;
743 struct sock_mapping *map;
744 struct sock_mapping *map2 = NULL;
745 struct xen_pvcalls_request *req;
746 int notify, req_id, ret, evtchn, nonblock;
748 map = pvcalls_enter_sock(sock);
751 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
753 if (map->passive.status != PVCALLS_STATUS_LISTEN) {
754 pvcalls_exit_sock(sock);
758 nonblock = flags & SOCK_NONBLOCK;
760 * Backend only supports 1 inflight accept request, will return
761 * errors for the others
763 if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
764 (void *)&map->passive.flags)) {
765 req_id = READ_ONCE(map->passive.inflight_req_id);
766 if (req_id != PVCALLS_INVALID_ID &&
767 READ_ONCE(bedata->rsp[req_id].req_id) == req_id) {
768 map2 = map->passive.accept_map;
772 pvcalls_exit_sock(sock);
775 if (wait_event_interruptible(map->passive.inflight_accept_req,
776 !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
777 (void *)&map->passive.flags))) {
778 pvcalls_exit_sock(sock);
783 spin_lock(&bedata->socket_lock);
784 ret = get_request(bedata, &req_id);
786 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
787 (void *)&map->passive.flags);
788 spin_unlock(&bedata->socket_lock);
789 pvcalls_exit_sock(sock);
792 map2 = kzalloc(sizeof(*map2), GFP_ATOMIC);
794 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
795 (void *)&map->passive.flags);
796 spin_unlock(&bedata->socket_lock);
797 pvcalls_exit_sock(sock);
800 ret = create_active(map2, &evtchn);
803 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
804 (void *)&map->passive.flags);
805 spin_unlock(&bedata->socket_lock);
806 pvcalls_exit_sock(sock);
809 list_add_tail(&map2->list, &bedata->socket_mappings);
811 req = RING_GET_REQUEST(&bedata->ring, req_id);
812 req->req_id = req_id;
813 req->cmd = PVCALLS_ACCEPT;
814 req->u.accept.id = (uintptr_t) map;
815 req->u.accept.ref = map2->active.ref;
816 req->u.accept.id_new = (uintptr_t) map2;
817 req->u.accept.evtchn = evtchn;
818 map->passive.accept_map = map2;
820 bedata->ring.req_prod_pvt++;
821 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
822 spin_unlock(&bedata->socket_lock);
824 notify_remote_via_irq(bedata->irq);
825 /* We could check if we have received a response before returning. */
827 WRITE_ONCE(map->passive.inflight_req_id, req_id);
828 pvcalls_exit_sock(sock);
832 if (wait_event_interruptible(bedata->inflight_req,
833 READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) {
834 pvcalls_exit_sock(sock);
837 /* read req_id, then the content */
841 map2->sock = newsock;
842 newsock->sk = kzalloc(sizeof(*newsock->sk), GFP_KERNEL);
844 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
845 map->passive.inflight_req_id = PVCALLS_INVALID_ID;
846 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
847 (void *)&map->passive.flags);
848 pvcalls_front_free_map(bedata, map2);
849 pvcalls_exit_sock(sock);
852 newsock->sk->sk_send_head = (void *)map2;
854 ret = bedata->rsp[req_id].ret;
855 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
856 map->passive.inflight_req_id = PVCALLS_INVALID_ID;
858 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags);
859 wake_up(&map->passive.inflight_accept_req);
861 pvcalls_exit_sock(sock);
865 static __poll_t pvcalls_front_poll_passive(struct file *file,
866 struct pvcalls_bedata *bedata,
867 struct sock_mapping *map,
870 int notify, req_id, ret;
871 struct xen_pvcalls_request *req;
873 if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
874 (void *)&map->passive.flags)) {
875 uint32_t req_id = READ_ONCE(map->passive.inflight_req_id);
877 if (req_id != PVCALLS_INVALID_ID &&
878 READ_ONCE(bedata->rsp[req_id].req_id) == req_id)
879 return EPOLLIN | EPOLLRDNORM;
881 poll_wait(file, &map->passive.inflight_accept_req, wait);
885 if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET,
886 (void *)&map->passive.flags))
887 return EPOLLIN | EPOLLRDNORM;
890 * First check RET, then INFLIGHT. No barriers necessary to
891 * ensure execution ordering because of the conditional
892 * instructions creating control dependencies.
895 if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT,
896 (void *)&map->passive.flags)) {
897 poll_wait(file, &bedata->inflight_req, wait);
901 spin_lock(&bedata->socket_lock);
902 ret = get_request(bedata, &req_id);
904 spin_unlock(&bedata->socket_lock);
907 req = RING_GET_REQUEST(&bedata->ring, req_id);
908 req->req_id = req_id;
909 req->cmd = PVCALLS_POLL;
910 req->u.poll.id = (uintptr_t) map;
912 bedata->ring.req_prod_pvt++;
913 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
914 spin_unlock(&bedata->socket_lock);
916 notify_remote_via_irq(bedata->irq);
918 poll_wait(file, &bedata->inflight_req, wait);
922 static __poll_t pvcalls_front_poll_active(struct file *file,
923 struct pvcalls_bedata *bedata,
924 struct sock_mapping *map,
928 int32_t in_error, out_error;
929 struct pvcalls_data_intf *intf = map->active.ring;
931 out_error = intf->out_error;
932 in_error = intf->in_error;
934 poll_wait(file, &map->active.inflight_conn_req, wait);
935 if (pvcalls_front_write_todo(map))
936 mask |= EPOLLOUT | EPOLLWRNORM;
937 if (pvcalls_front_read_todo(map))
938 mask |= EPOLLIN | EPOLLRDNORM;
939 if (in_error != 0 || out_error != 0)
945 __poll_t pvcalls_front_poll(struct file *file, struct socket *sock,
948 struct pvcalls_bedata *bedata;
949 struct sock_mapping *map;
952 map = pvcalls_enter_sock(sock);
955 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
957 if (map->active_socket)
958 ret = pvcalls_front_poll_active(file, bedata, map, wait);
960 ret = pvcalls_front_poll_passive(file, bedata, map, wait);
961 pvcalls_exit_sock(sock);
965 int pvcalls_front_release(struct socket *sock)
967 struct pvcalls_bedata *bedata;
968 struct sock_mapping *map;
969 int req_id, notify, ret;
970 struct xen_pvcalls_request *req;
972 if (sock->sk == NULL)
975 map = pvcalls_enter_sock(sock);
977 if (PTR_ERR(map) == -ENOTCONN)
982 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
984 spin_lock(&bedata->socket_lock);
985 ret = get_request(bedata, &req_id);
987 spin_unlock(&bedata->socket_lock);
988 pvcalls_exit_sock(sock);
991 sock->sk->sk_send_head = NULL;
993 req = RING_GET_REQUEST(&bedata->ring, req_id);
994 req->req_id = req_id;
995 req->cmd = PVCALLS_RELEASE;
996 req->u.release.id = (uintptr_t)map;
998 bedata->ring.req_prod_pvt++;
999 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
1000 spin_unlock(&bedata->socket_lock);
1002 notify_remote_via_irq(bedata->irq);
1004 wait_event(bedata->inflight_req,
1005 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
1007 if (map->active_socket) {
1009 * Set in_error and wake up inflight_conn_req to force
1010 * recvmsg waiters to exit.
1012 map->active.ring->in_error = -EBADF;
1013 wake_up_interruptible(&map->active.inflight_conn_req);
1016 * We need to make sure that sendmsg/recvmsg on this socket have
1017 * not started before we've cleared sk_send_head here. The
1018 * easiest way to guarantee this is to see that no pvcalls
1019 * (other than us) is in progress on this socket.
1021 while (atomic_read(&map->refcount) > 1)
1024 pvcalls_front_free_map(bedata, map);
1026 wake_up(&bedata->inflight_req);
1027 wake_up(&map->passive.inflight_accept_req);
1029 while (atomic_read(&map->refcount) > 1)
1032 spin_lock(&bedata->socket_lock);
1033 list_del(&map->list);
1034 spin_unlock(&bedata->socket_lock);
1035 if (READ_ONCE(map->passive.inflight_req_id) !=
1036 PVCALLS_INVALID_ID) {
1037 pvcalls_front_free_map(bedata,
1038 map->passive.accept_map);
1042 WRITE_ONCE(bedata->rsp[req_id].req_id, PVCALLS_INVALID_ID);
1048 static const struct xenbus_device_id pvcalls_front_ids[] = {
1053 static int pvcalls_front_remove(struct xenbus_device *dev)
1055 struct pvcalls_bedata *bedata;
1056 struct sock_mapping *map = NULL, *n;
1058 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
1059 dev_set_drvdata(&dev->dev, NULL);
1060 pvcalls_front_dev = NULL;
1061 if (bedata->irq >= 0)
1062 unbind_from_irqhandler(bedata->irq, dev);
1064 list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
1065 map->sock->sk->sk_send_head = NULL;
1066 if (map->active_socket) {
1067 map->active.ring->in_error = -EBADF;
1068 wake_up_interruptible(&map->active.inflight_conn_req);
1073 while (atomic_read(&pvcalls_refcount) > 0)
1075 list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
1076 if (map->active_socket) {
1077 /* No need to lock, refcount is 0 */
1078 pvcalls_front_free_map(bedata, map);
1080 list_del(&map->list);
1084 if (bedata->ref != -1)
1085 gnttab_end_foreign_access(bedata->ref, 0, 0);
1086 kfree(bedata->ring.sring);
1088 xenbus_switch_state(dev, XenbusStateClosed);
1092 static int pvcalls_front_probe(struct xenbus_device *dev,
1093 const struct xenbus_device_id *id)
1095 int ret = -ENOMEM, evtchn, i;
1096 unsigned int max_page_order, function_calls, len;
1098 grant_ref_t gref_head = 0;
1099 struct xenbus_transaction xbt;
1100 struct pvcalls_bedata *bedata = NULL;
1101 struct xen_pvcalls_sring *sring;
1103 if (pvcalls_front_dev != NULL) {
1104 dev_err(&dev->dev, "only one PV Calls connection supported\n");
1108 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
1109 if (IS_ERR(versions))
1110 return PTR_ERR(versions);
1113 if (strcmp(versions, "1")) {
1118 max_page_order = xenbus_read_unsigned(dev->otherend,
1119 "max-page-order", 0);
1120 if (max_page_order < PVCALLS_RING_ORDER)
1122 function_calls = xenbus_read_unsigned(dev->otherend,
1123 "function-calls", 0);
1124 /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
1125 if (function_calls != 1)
1127 pr_info("%s max-page-order is %u\n", __func__, max_page_order);
1129 bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL);
1133 dev_set_drvdata(&dev->dev, bedata);
1134 pvcalls_front_dev = dev;
1135 init_waitqueue_head(&bedata->inflight_req);
1136 INIT_LIST_HEAD(&bedata->socket_mappings);
1137 spin_lock_init(&bedata->socket_lock);
1141 for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++)
1142 bedata->rsp[i].req_id = PVCALLS_INVALID_ID;
1144 sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL |
1148 SHARED_RING_INIT(sring);
1149 FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE);
1151 ret = xenbus_alloc_evtchn(dev, &evtchn);
1155 bedata->irq = bind_evtchn_to_irqhandler(evtchn,
1156 pvcalls_front_event_handler,
1157 0, "pvcalls-frontend", dev);
1158 if (bedata->irq < 0) {
1163 ret = gnttab_alloc_grant_references(1, &gref_head);
1166 ret = gnttab_claim_grant_reference(&gref_head);
1170 gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id,
1171 virt_to_gfn((void *)sring), 0);
1174 ret = xenbus_transaction_start(&xbt);
1176 xenbus_dev_fatal(dev, ret, "starting transaction");
1179 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
1182 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref);
1185 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
1189 ret = xenbus_transaction_end(xbt, 0);
1193 xenbus_dev_fatal(dev, ret, "completing transaction");
1196 xenbus_switch_state(dev, XenbusStateInitialised);
1201 xenbus_transaction_end(xbt, 1);
1202 xenbus_dev_fatal(dev, ret, "writing xenstore");
1204 pvcalls_front_remove(dev);
1208 static void pvcalls_front_changed(struct xenbus_device *dev,
1209 enum xenbus_state backend_state)
1211 switch (backend_state) {
1212 case XenbusStateReconfiguring:
1213 case XenbusStateReconfigured:
1214 case XenbusStateInitialising:
1215 case XenbusStateInitialised:
1216 case XenbusStateUnknown:
1219 case XenbusStateInitWait:
1222 case XenbusStateConnected:
1223 xenbus_switch_state(dev, XenbusStateConnected);
1226 case XenbusStateClosed:
1227 if (dev->state == XenbusStateClosed)
1229 /* Missed the backend's CLOSING state */
1231 case XenbusStateClosing:
1232 xenbus_frontend_closed(dev);
1237 static struct xenbus_driver pvcalls_front_driver = {
1238 .ids = pvcalls_front_ids,
1239 .probe = pvcalls_front_probe,
1240 .remove = pvcalls_front_remove,
1241 .otherend_changed = pvcalls_front_changed,
1244 static int __init pvcalls_frontend_init(void)
1249 pr_info("Initialising Xen pvcalls frontend driver\n");
1251 return xenbus_register_frontend(&pvcalls_front_driver);
1254 module_init(pvcalls_frontend_init);
1256 MODULE_DESCRIPTION("Xen PV Calls frontend driver");
1258 MODULE_LICENSE("GPL");