1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (c) 2017 Stefano Stabellini <stefano@aporeto.com>
6 #include <linux/module.h>
8 #include <linux/socket.h>
12 #include <xen/events.h>
13 #include <xen/grant_table.h>
15 #include <xen/xenbus.h>
16 #include <xen/interface/io/pvcalls.h>
18 #include "pvcalls-front.h"
20 #define PVCALLS_INVALID_ID UINT_MAX
21 #define PVCALLS_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
22 #define PVCALLS_NR_RSP_PER_RING __CONST_RING_SIZE(xen_pvcalls, XEN_PAGE_SIZE)
23 #define PVCALLS_FRONT_MAX_SPIN 5000
25 static struct proto pvcalls_proto
= {
28 .obj_size
= sizeof(struct sock
),
31 struct pvcalls_bedata
{
32 struct xen_pvcalls_front_ring ring
;
36 struct list_head socket_mappings
;
37 spinlock_t socket_lock
;
39 wait_queue_head_t inflight_req
;
40 struct xen_pvcalls_response rsp
[PVCALLS_NR_RSP_PER_RING
];
42 /* Only one front/back connection supported. */
43 static struct xenbus_device
*pvcalls_front_dev
;
44 static atomic_t pvcalls_refcount
;
46 /* first increment refcount, then proceed */
47 #define pvcalls_enter() { \
48 atomic_inc(&pvcalls_refcount); \
51 /* first complete other operations, then decrement refcount */
52 #define pvcalls_exit() { \
53 atomic_dec(&pvcalls_refcount); \
58 struct list_head list
;
65 struct pvcalls_data_intf
*ring
;
66 struct pvcalls_data data
;
67 struct mutex in_mutex
;
68 struct mutex out_mutex
;
70 wait_queue_head_t inflight_conn_req
;
74 * Socket status, needs to be 64-bit aligned due to the
75 * test_and_* functions which have this requirement on arm64.
77 #define PVCALLS_STATUS_UNINITALIZED 0
78 #define PVCALLS_STATUS_BIND 1
79 #define PVCALLS_STATUS_LISTEN 2
80 uint8_t status
__attribute__((aligned(8)));
82 * Internal state-machine flags.
83 * Only one accept operation can be inflight for a socket.
84 * Only one poll operation can be inflight for a given socket.
85 * flags needs to be 64-bit aligned due to the test_and_*
86 * functions which have this requirement on arm64.
88 #define PVCALLS_FLAG_ACCEPT_INFLIGHT 0
89 #define PVCALLS_FLAG_POLL_INFLIGHT 1
90 #define PVCALLS_FLAG_POLL_RET 2
91 uint8_t flags
__attribute__((aligned(8)));
92 uint32_t inflight_req_id
;
93 struct sock_mapping
*accept_map
;
94 wait_queue_head_t inflight_accept_req
;
99 static inline struct sock_mapping
*pvcalls_enter_sock(struct socket
*sock
)
101 struct sock_mapping
*map
;
103 if (!pvcalls_front_dev
||
104 dev_get_drvdata(&pvcalls_front_dev
->dev
) == NULL
)
105 return ERR_PTR(-ENOTCONN
);
107 map
= (struct sock_mapping
*)sock
->sk
->sk_send_head
;
109 return ERR_PTR(-ENOTSOCK
);
112 atomic_inc(&map
->refcount
);
116 static inline void pvcalls_exit_sock(struct socket
*sock
)
118 struct sock_mapping
*map
;
120 map
= (struct sock_mapping
*)sock
->sk
->sk_send_head
;
121 atomic_dec(&map
->refcount
);
125 static inline int get_request(struct pvcalls_bedata
*bedata
, int *req_id
)
127 *req_id
= bedata
->ring
.req_prod_pvt
& (RING_SIZE(&bedata
->ring
) - 1);
128 if (RING_FULL(&bedata
->ring
) ||
129 bedata
->rsp
[*req_id
].req_id
!= PVCALLS_INVALID_ID
)
134 static bool pvcalls_front_write_todo(struct sock_mapping
*map
)
136 struct pvcalls_data_intf
*intf
= map
->active
.ring
;
137 RING_IDX cons
, prod
, size
= XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER
);
140 error
= intf
->out_error
;
141 if (error
== -ENOTCONN
)
146 cons
= intf
->out_cons
;
147 prod
= intf
->out_prod
;
148 return !!(size
- pvcalls_queued(prod
, cons
, size
));
151 static bool pvcalls_front_read_todo(struct sock_mapping
*map
)
153 struct pvcalls_data_intf
*intf
= map
->active
.ring
;
157 cons
= intf
->in_cons
;
158 prod
= intf
->in_prod
;
159 error
= intf
->in_error
;
160 return (error
!= 0 ||
161 pvcalls_queued(prod
, cons
,
162 XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER
)) != 0);
165 static irqreturn_t
pvcalls_front_event_handler(int irq
, void *dev_id
)
167 struct xenbus_device
*dev
= dev_id
;
168 struct pvcalls_bedata
*bedata
;
169 struct xen_pvcalls_response
*rsp
;
171 int req_id
= 0, more
= 0, done
= 0;
177 bedata
= dev_get_drvdata(&dev
->dev
);
178 if (bedata
== NULL
) {
184 while (RING_HAS_UNCONSUMED_RESPONSES(&bedata
->ring
)) {
185 rsp
= RING_GET_RESPONSE(&bedata
->ring
, bedata
->ring
.rsp_cons
);
187 req_id
= rsp
->req_id
;
188 if (rsp
->cmd
== PVCALLS_POLL
) {
189 struct sock_mapping
*map
= (struct sock_mapping
*)(uintptr_t)
192 clear_bit(PVCALLS_FLAG_POLL_INFLIGHT
,
193 (void *)&map
->passive
.flags
);
195 * clear INFLIGHT, then set RET. It pairs with
196 * the checks at the beginning of
197 * pvcalls_front_poll_passive.
200 set_bit(PVCALLS_FLAG_POLL_RET
,
201 (void *)&map
->passive
.flags
);
203 dst
= (uint8_t *)&bedata
->rsp
[req_id
] +
205 src
= (uint8_t *)rsp
+ sizeof(rsp
->req_id
);
206 memcpy(dst
, src
, sizeof(*rsp
) - sizeof(rsp
->req_id
));
208 * First copy the rest of the data, then req_id. It is
209 * paired with the barrier when accessing bedata->rsp.
212 bedata
->rsp
[req_id
].req_id
= req_id
;
216 bedata
->ring
.rsp_cons
++;
219 RING_FINAL_CHECK_FOR_RESPONSES(&bedata
->ring
, more
);
223 wake_up(&bedata
->inflight_req
);
228 static void pvcalls_front_free_map(struct pvcalls_bedata
*bedata
,
229 struct sock_mapping
*map
)
233 unbind_from_irqhandler(map
->active
.irq
, map
);
235 spin_lock(&bedata
->socket_lock
);
236 if (!list_empty(&map
->list
))
237 list_del_init(&map
->list
);
238 spin_unlock(&bedata
->socket_lock
);
240 for (i
= 0; i
< (1 << PVCALLS_RING_ORDER
); i
++)
241 gnttab_end_foreign_access(map
->active
.ring
->ref
[i
], 0, 0);
242 gnttab_end_foreign_access(map
->active
.ref
, 0, 0);
243 free_page((unsigned long)map
->active
.ring
);
248 static irqreturn_t
pvcalls_front_conn_handler(int irq
, void *sock_map
)
250 struct sock_mapping
*map
= sock_map
;
255 wake_up_interruptible(&map
->active
.inflight_conn_req
);
260 int pvcalls_front_socket(struct socket
*sock
)
262 struct pvcalls_bedata
*bedata
;
263 struct sock_mapping
*map
= NULL
;
264 struct xen_pvcalls_request
*req
;
265 int notify
, req_id
, ret
;
268 * PVCalls only supports domain AF_INET,
269 * type SOCK_STREAM and protocol 0 sockets for now.
271 * Check socket type here, AF_INET and protocol checks are done
274 if (sock
->type
!= SOCK_STREAM
)
278 if (!pvcalls_front_dev
) {
282 bedata
= dev_get_drvdata(&pvcalls_front_dev
->dev
);
284 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
290 spin_lock(&bedata
->socket_lock
);
292 ret
= get_request(bedata
, &req_id
);
295 spin_unlock(&bedata
->socket_lock
);
301 * sock->sk->sk_send_head is not used for ip sockets: reuse the
302 * field to store a pointer to the struct sock_mapping
303 * corresponding to the socket. This way, we can easily get the
304 * struct sock_mapping from the struct socket.
306 sock
->sk
->sk_send_head
= (void *)map
;
307 list_add_tail(&map
->list
, &bedata
->socket_mappings
);
309 req
= RING_GET_REQUEST(&bedata
->ring
, req_id
);
310 req
->req_id
= req_id
;
311 req
->cmd
= PVCALLS_SOCKET
;
312 req
->u
.socket
.id
= (uintptr_t) map
;
313 req
->u
.socket
.domain
= AF_INET
;
314 req
->u
.socket
.type
= SOCK_STREAM
;
315 req
->u
.socket
.protocol
= IPPROTO_IP
;
317 bedata
->ring
.req_prod_pvt
++;
318 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata
->ring
, notify
);
319 spin_unlock(&bedata
->socket_lock
);
321 notify_remote_via_irq(bedata
->irq
);
323 wait_event(bedata
->inflight_req
,
324 READ_ONCE(bedata
->rsp
[req_id
].req_id
) == req_id
);
326 /* read req_id, then the content */
328 ret
= bedata
->rsp
[req_id
].ret
;
329 bedata
->rsp
[req_id
].req_id
= PVCALLS_INVALID_ID
;
335 static void free_active_ring(struct sock_mapping
*map
)
337 if (!map
->active
.ring
)
340 free_pages((unsigned long)map
->active
.data
.in
,
341 map
->active
.ring
->ring_order
);
342 free_page((unsigned long)map
->active
.ring
);
345 static int alloc_active_ring(struct sock_mapping
*map
)
349 map
->active
.ring
= (struct pvcalls_data_intf
*)
350 get_zeroed_page(GFP_KERNEL
);
351 if (!map
->active
.ring
)
354 map
->active
.ring
->ring_order
= PVCALLS_RING_ORDER
;
355 bytes
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
360 map
->active
.data
.in
= bytes
;
361 map
->active
.data
.out
= bytes
+
362 XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER
);
367 free_active_ring(map
);
371 static int create_active(struct sock_mapping
*map
, evtchn_port_t
*evtchn
)
374 int ret
= -ENOMEM
, irq
= -1, i
;
377 init_waitqueue_head(&map
->active
.inflight_conn_req
);
379 bytes
= map
->active
.data
.in
;
380 for (i
= 0; i
< (1 << PVCALLS_RING_ORDER
); i
++)
381 map
->active
.ring
->ref
[i
] = gnttab_grant_foreign_access(
382 pvcalls_front_dev
->otherend_id
,
383 pfn_to_gfn(virt_to_pfn(bytes
) + i
), 0);
385 map
->active
.ref
= gnttab_grant_foreign_access(
386 pvcalls_front_dev
->otherend_id
,
387 pfn_to_gfn(virt_to_pfn((void *)map
->active
.ring
)), 0);
389 ret
= xenbus_alloc_evtchn(pvcalls_front_dev
, evtchn
);
392 irq
= bind_evtchn_to_irqhandler(*evtchn
, pvcalls_front_conn_handler
,
393 0, "pvcalls-frontend", map
);
399 map
->active
.irq
= irq
;
400 map
->active_socket
= true;
401 mutex_init(&map
->active
.in_mutex
);
402 mutex_init(&map
->active
.out_mutex
);
408 xenbus_free_evtchn(pvcalls_front_dev
, *evtchn
);
412 int pvcalls_front_connect(struct socket
*sock
, struct sockaddr
*addr
,
413 int addr_len
, int flags
)
415 struct pvcalls_bedata
*bedata
;
416 struct sock_mapping
*map
= NULL
;
417 struct xen_pvcalls_request
*req
;
418 int notify
, req_id
, ret
;
419 evtchn_port_t evtchn
;
421 if (addr
->sa_family
!= AF_INET
|| sock
->type
!= SOCK_STREAM
)
424 map
= pvcalls_enter_sock(sock
);
428 bedata
= dev_get_drvdata(&pvcalls_front_dev
->dev
);
429 ret
= alloc_active_ring(map
);
431 pvcalls_exit_sock(sock
);
435 spin_lock(&bedata
->socket_lock
);
436 ret
= get_request(bedata
, &req_id
);
438 spin_unlock(&bedata
->socket_lock
);
439 free_active_ring(map
);
440 pvcalls_exit_sock(sock
);
443 ret
= create_active(map
, &evtchn
);
445 spin_unlock(&bedata
->socket_lock
);
446 free_active_ring(map
);
447 pvcalls_exit_sock(sock
);
451 req
= RING_GET_REQUEST(&bedata
->ring
, req_id
);
452 req
->req_id
= req_id
;
453 req
->cmd
= PVCALLS_CONNECT
;
454 req
->u
.connect
.id
= (uintptr_t)map
;
455 req
->u
.connect
.len
= addr_len
;
456 req
->u
.connect
.flags
= flags
;
457 req
->u
.connect
.ref
= map
->active
.ref
;
458 req
->u
.connect
.evtchn
= evtchn
;
459 memcpy(req
->u
.connect
.addr
, addr
, sizeof(*addr
));
463 bedata
->ring
.req_prod_pvt
++;
464 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata
->ring
, notify
);
465 spin_unlock(&bedata
->socket_lock
);
468 notify_remote_via_irq(bedata
->irq
);
470 wait_event(bedata
->inflight_req
,
471 READ_ONCE(bedata
->rsp
[req_id
].req_id
) == req_id
);
473 /* read req_id, then the content */
475 ret
= bedata
->rsp
[req_id
].ret
;
476 bedata
->rsp
[req_id
].req_id
= PVCALLS_INVALID_ID
;
477 pvcalls_exit_sock(sock
);
481 static int __write_ring(struct pvcalls_data_intf
*intf
,
482 struct pvcalls_data
*data
,
483 struct iov_iter
*msg_iter
,
486 RING_IDX cons
, prod
, size
, masked_prod
, masked_cons
;
487 RING_IDX array_size
= XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER
);
490 error
= intf
->out_error
;
493 cons
= intf
->out_cons
;
494 prod
= intf
->out_prod
;
495 /* read indexes before continuing */
498 size
= pvcalls_queued(prod
, cons
, array_size
);
499 if (size
> array_size
)
501 if (size
== array_size
)
503 if (len
> array_size
- size
)
504 len
= array_size
- size
;
506 masked_prod
= pvcalls_mask(prod
, array_size
);
507 masked_cons
= pvcalls_mask(cons
, array_size
);
509 if (masked_prod
< masked_cons
) {
510 len
= copy_from_iter(data
->out
+ masked_prod
, len
, msg_iter
);
512 if (len
> array_size
- masked_prod
) {
513 int ret
= copy_from_iter(data
->out
+ masked_prod
,
514 array_size
- masked_prod
, msg_iter
);
515 if (ret
!= array_size
- masked_prod
) {
519 len
= ret
+ copy_from_iter(data
->out
, len
- ret
, msg_iter
);
521 len
= copy_from_iter(data
->out
+ masked_prod
, len
, msg_iter
);
525 /* write to ring before updating pointer */
527 intf
->out_prod
+= len
;
532 int pvcalls_front_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
535 struct sock_mapping
*map
;
536 int sent
, tot_sent
= 0;
537 int count
= 0, flags
;
539 flags
= msg
->msg_flags
;
540 if (flags
& (MSG_CONFIRM
|MSG_DONTROUTE
|MSG_EOR
|MSG_OOB
))
543 map
= pvcalls_enter_sock(sock
);
547 mutex_lock(&map
->active
.out_mutex
);
548 if ((flags
& MSG_DONTWAIT
) && !pvcalls_front_write_todo(map
)) {
549 mutex_unlock(&map
->active
.out_mutex
);
550 pvcalls_exit_sock(sock
);
558 sent
= __write_ring(map
->active
.ring
,
559 &map
->active
.data
, &msg
->msg_iter
,
564 notify_remote_via_irq(map
->active
.irq
);
566 if (sent
>= 0 && len
> 0 && count
< PVCALLS_FRONT_MAX_SPIN
)
571 mutex_unlock(&map
->active
.out_mutex
);
572 pvcalls_exit_sock(sock
);
576 static int __read_ring(struct pvcalls_data_intf
*intf
,
577 struct pvcalls_data
*data
,
578 struct iov_iter
*msg_iter
,
579 size_t len
, int flags
)
581 RING_IDX cons
, prod
, size
, masked_prod
, masked_cons
;
582 RING_IDX array_size
= XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER
);
585 cons
= intf
->in_cons
;
586 prod
= intf
->in_prod
;
587 error
= intf
->in_error
;
588 /* get pointers before reading from the ring */
591 size
= pvcalls_queued(prod
, cons
, array_size
);
592 masked_prod
= pvcalls_mask(prod
, array_size
);
593 masked_cons
= pvcalls_mask(cons
, array_size
);
596 return error
?: size
;
601 if (masked_prod
> masked_cons
) {
602 len
= copy_to_iter(data
->in
+ masked_cons
, len
, msg_iter
);
604 if (len
> (array_size
- masked_cons
)) {
605 int ret
= copy_to_iter(data
->in
+ masked_cons
,
606 array_size
- masked_cons
, msg_iter
);
607 if (ret
!= array_size
- masked_cons
) {
611 len
= ret
+ copy_to_iter(data
->in
, len
- ret
, msg_iter
);
613 len
= copy_to_iter(data
->in
+ masked_cons
, len
, msg_iter
);
617 /* read data from the ring before increasing the index */
619 if (!(flags
& MSG_PEEK
))
620 intf
->in_cons
+= len
;
625 int pvcalls_front_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
629 struct sock_mapping
*map
;
631 if (flags
& (MSG_CMSG_CLOEXEC
|MSG_ERRQUEUE
|MSG_OOB
|MSG_TRUNC
))
634 map
= pvcalls_enter_sock(sock
);
638 mutex_lock(&map
->active
.in_mutex
);
639 if (len
> XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER
))
640 len
= XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER
);
642 while (!(flags
& MSG_DONTWAIT
) && !pvcalls_front_read_todo(map
)) {
643 wait_event_interruptible(map
->active
.inflight_conn_req
,
644 pvcalls_front_read_todo(map
));
646 ret
= __read_ring(map
->active
.ring
, &map
->active
.data
,
647 &msg
->msg_iter
, len
, flags
);
650 notify_remote_via_irq(map
->active
.irq
);
652 ret
= (flags
& MSG_DONTWAIT
) ? -EAGAIN
: 0;
653 if (ret
== -ENOTCONN
)
656 mutex_unlock(&map
->active
.in_mutex
);
657 pvcalls_exit_sock(sock
);
661 int pvcalls_front_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
663 struct pvcalls_bedata
*bedata
;
664 struct sock_mapping
*map
= NULL
;
665 struct xen_pvcalls_request
*req
;
666 int notify
, req_id
, ret
;
668 if (addr
->sa_family
!= AF_INET
|| sock
->type
!= SOCK_STREAM
)
671 map
= pvcalls_enter_sock(sock
);
674 bedata
= dev_get_drvdata(&pvcalls_front_dev
->dev
);
676 spin_lock(&bedata
->socket_lock
);
677 ret
= get_request(bedata
, &req_id
);
679 spin_unlock(&bedata
->socket_lock
);
680 pvcalls_exit_sock(sock
);
683 req
= RING_GET_REQUEST(&bedata
->ring
, req_id
);
684 req
->req_id
= req_id
;
686 req
->cmd
= PVCALLS_BIND
;
687 req
->u
.bind
.id
= (uintptr_t)map
;
688 memcpy(req
->u
.bind
.addr
, addr
, sizeof(*addr
));
689 req
->u
.bind
.len
= addr_len
;
691 init_waitqueue_head(&map
->passive
.inflight_accept_req
);
693 map
->active_socket
= false;
695 bedata
->ring
.req_prod_pvt
++;
696 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata
->ring
, notify
);
697 spin_unlock(&bedata
->socket_lock
);
699 notify_remote_via_irq(bedata
->irq
);
701 wait_event(bedata
->inflight_req
,
702 READ_ONCE(bedata
->rsp
[req_id
].req_id
) == req_id
);
704 /* read req_id, then the content */
706 ret
= bedata
->rsp
[req_id
].ret
;
707 bedata
->rsp
[req_id
].req_id
= PVCALLS_INVALID_ID
;
709 map
->passive
.status
= PVCALLS_STATUS_BIND
;
710 pvcalls_exit_sock(sock
);
714 int pvcalls_front_listen(struct socket
*sock
, int backlog
)
716 struct pvcalls_bedata
*bedata
;
717 struct sock_mapping
*map
;
718 struct xen_pvcalls_request
*req
;
719 int notify
, req_id
, ret
;
721 map
= pvcalls_enter_sock(sock
);
724 bedata
= dev_get_drvdata(&pvcalls_front_dev
->dev
);
726 if (map
->passive
.status
!= PVCALLS_STATUS_BIND
) {
727 pvcalls_exit_sock(sock
);
731 spin_lock(&bedata
->socket_lock
);
732 ret
= get_request(bedata
, &req_id
);
734 spin_unlock(&bedata
->socket_lock
);
735 pvcalls_exit_sock(sock
);
738 req
= RING_GET_REQUEST(&bedata
->ring
, req_id
);
739 req
->req_id
= req_id
;
740 req
->cmd
= PVCALLS_LISTEN
;
741 req
->u
.listen
.id
= (uintptr_t) map
;
742 req
->u
.listen
.backlog
= backlog
;
744 bedata
->ring
.req_prod_pvt
++;
745 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata
->ring
, notify
);
746 spin_unlock(&bedata
->socket_lock
);
748 notify_remote_via_irq(bedata
->irq
);
750 wait_event(bedata
->inflight_req
,
751 READ_ONCE(bedata
->rsp
[req_id
].req_id
) == req_id
);
753 /* read req_id, then the content */
755 ret
= bedata
->rsp
[req_id
].ret
;
756 bedata
->rsp
[req_id
].req_id
= PVCALLS_INVALID_ID
;
758 map
->passive
.status
= PVCALLS_STATUS_LISTEN
;
759 pvcalls_exit_sock(sock
);
763 int pvcalls_front_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
765 struct pvcalls_bedata
*bedata
;
766 struct sock_mapping
*map
;
767 struct sock_mapping
*map2
= NULL
;
768 struct xen_pvcalls_request
*req
;
769 int notify
, req_id
, ret
, nonblock
;
770 evtchn_port_t evtchn
;
772 map
= pvcalls_enter_sock(sock
);
775 bedata
= dev_get_drvdata(&pvcalls_front_dev
->dev
);
777 if (map
->passive
.status
!= PVCALLS_STATUS_LISTEN
) {
778 pvcalls_exit_sock(sock
);
782 nonblock
= flags
& SOCK_NONBLOCK
;
784 * Backend only supports 1 inflight accept request, will return
785 * errors for the others
787 if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT
,
788 (void *)&map
->passive
.flags
)) {
789 req_id
= READ_ONCE(map
->passive
.inflight_req_id
);
790 if (req_id
!= PVCALLS_INVALID_ID
&&
791 READ_ONCE(bedata
->rsp
[req_id
].req_id
) == req_id
) {
792 map2
= map
->passive
.accept_map
;
796 pvcalls_exit_sock(sock
);
799 if (wait_event_interruptible(map
->passive
.inflight_accept_req
,
800 !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT
,
801 (void *)&map
->passive
.flags
))) {
802 pvcalls_exit_sock(sock
);
807 map2
= kzalloc(sizeof(*map2
), GFP_KERNEL
);
809 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT
,
810 (void *)&map
->passive
.flags
);
811 pvcalls_exit_sock(sock
);
814 ret
= alloc_active_ring(map2
);
816 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT
,
817 (void *)&map
->passive
.flags
);
819 pvcalls_exit_sock(sock
);
822 spin_lock(&bedata
->socket_lock
);
823 ret
= get_request(bedata
, &req_id
);
825 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT
,
826 (void *)&map
->passive
.flags
);
827 spin_unlock(&bedata
->socket_lock
);
828 free_active_ring(map2
);
830 pvcalls_exit_sock(sock
);
834 ret
= create_active(map2
, &evtchn
);
836 free_active_ring(map2
);
838 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT
,
839 (void *)&map
->passive
.flags
);
840 spin_unlock(&bedata
->socket_lock
);
841 pvcalls_exit_sock(sock
);
844 list_add_tail(&map2
->list
, &bedata
->socket_mappings
);
846 req
= RING_GET_REQUEST(&bedata
->ring
, req_id
);
847 req
->req_id
= req_id
;
848 req
->cmd
= PVCALLS_ACCEPT
;
849 req
->u
.accept
.id
= (uintptr_t) map
;
850 req
->u
.accept
.ref
= map2
->active
.ref
;
851 req
->u
.accept
.id_new
= (uintptr_t) map2
;
852 req
->u
.accept
.evtchn
= evtchn
;
853 map
->passive
.accept_map
= map2
;
855 bedata
->ring
.req_prod_pvt
++;
856 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata
->ring
, notify
);
857 spin_unlock(&bedata
->socket_lock
);
859 notify_remote_via_irq(bedata
->irq
);
860 /* We could check if we have received a response before returning. */
862 WRITE_ONCE(map
->passive
.inflight_req_id
, req_id
);
863 pvcalls_exit_sock(sock
);
867 if (wait_event_interruptible(bedata
->inflight_req
,
868 READ_ONCE(bedata
->rsp
[req_id
].req_id
) == req_id
)) {
869 pvcalls_exit_sock(sock
);
872 /* read req_id, then the content */
876 map2
->sock
= newsock
;
877 newsock
->sk
= sk_alloc(sock_net(sock
->sk
), PF_INET
, GFP_KERNEL
, &pvcalls_proto
, false);
879 bedata
->rsp
[req_id
].req_id
= PVCALLS_INVALID_ID
;
880 map
->passive
.inflight_req_id
= PVCALLS_INVALID_ID
;
881 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT
,
882 (void *)&map
->passive
.flags
);
883 pvcalls_front_free_map(bedata
, map2
);
884 pvcalls_exit_sock(sock
);
887 newsock
->sk
->sk_send_head
= (void *)map2
;
889 ret
= bedata
->rsp
[req_id
].ret
;
890 bedata
->rsp
[req_id
].req_id
= PVCALLS_INVALID_ID
;
891 map
->passive
.inflight_req_id
= PVCALLS_INVALID_ID
;
893 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT
, (void *)&map
->passive
.flags
);
894 wake_up(&map
->passive
.inflight_accept_req
);
896 pvcalls_exit_sock(sock
);
900 static __poll_t
pvcalls_front_poll_passive(struct file
*file
,
901 struct pvcalls_bedata
*bedata
,
902 struct sock_mapping
*map
,
905 int notify
, req_id
, ret
;
906 struct xen_pvcalls_request
*req
;
908 if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT
,
909 (void *)&map
->passive
.flags
)) {
910 uint32_t req_id
= READ_ONCE(map
->passive
.inflight_req_id
);
912 if (req_id
!= PVCALLS_INVALID_ID
&&
913 READ_ONCE(bedata
->rsp
[req_id
].req_id
) == req_id
)
914 return EPOLLIN
| EPOLLRDNORM
;
916 poll_wait(file
, &map
->passive
.inflight_accept_req
, wait
);
920 if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET
,
921 (void *)&map
->passive
.flags
))
922 return EPOLLIN
| EPOLLRDNORM
;
925 * First check RET, then INFLIGHT. No barriers necessary to
926 * ensure execution ordering because of the conditional
927 * instructions creating control dependencies.
930 if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT
,
931 (void *)&map
->passive
.flags
)) {
932 poll_wait(file
, &bedata
->inflight_req
, wait
);
936 spin_lock(&bedata
->socket_lock
);
937 ret
= get_request(bedata
, &req_id
);
939 spin_unlock(&bedata
->socket_lock
);
942 req
= RING_GET_REQUEST(&bedata
->ring
, req_id
);
943 req
->req_id
= req_id
;
944 req
->cmd
= PVCALLS_POLL
;
945 req
->u
.poll
.id
= (uintptr_t) map
;
947 bedata
->ring
.req_prod_pvt
++;
948 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata
->ring
, notify
);
949 spin_unlock(&bedata
->socket_lock
);
951 notify_remote_via_irq(bedata
->irq
);
953 poll_wait(file
, &bedata
->inflight_req
, wait
);
957 static __poll_t
pvcalls_front_poll_active(struct file
*file
,
958 struct pvcalls_bedata
*bedata
,
959 struct sock_mapping
*map
,
963 int32_t in_error
, out_error
;
964 struct pvcalls_data_intf
*intf
= map
->active
.ring
;
966 out_error
= intf
->out_error
;
967 in_error
= intf
->in_error
;
969 poll_wait(file
, &map
->active
.inflight_conn_req
, wait
);
970 if (pvcalls_front_write_todo(map
))
971 mask
|= EPOLLOUT
| EPOLLWRNORM
;
972 if (pvcalls_front_read_todo(map
))
973 mask
|= EPOLLIN
| EPOLLRDNORM
;
974 if (in_error
!= 0 || out_error
!= 0)
980 __poll_t
pvcalls_front_poll(struct file
*file
, struct socket
*sock
,
983 struct pvcalls_bedata
*bedata
;
984 struct sock_mapping
*map
;
987 map
= pvcalls_enter_sock(sock
);
990 bedata
= dev_get_drvdata(&pvcalls_front_dev
->dev
);
992 if (map
->active_socket
)
993 ret
= pvcalls_front_poll_active(file
, bedata
, map
, wait
);
995 ret
= pvcalls_front_poll_passive(file
, bedata
, map
, wait
);
996 pvcalls_exit_sock(sock
);
1000 int pvcalls_front_release(struct socket
*sock
)
1002 struct pvcalls_bedata
*bedata
;
1003 struct sock_mapping
*map
;
1004 int req_id
, notify
, ret
;
1005 struct xen_pvcalls_request
*req
;
1007 if (sock
->sk
== NULL
)
1010 map
= pvcalls_enter_sock(sock
);
1012 if (PTR_ERR(map
) == -ENOTCONN
)
1017 bedata
= dev_get_drvdata(&pvcalls_front_dev
->dev
);
1019 spin_lock(&bedata
->socket_lock
);
1020 ret
= get_request(bedata
, &req_id
);
1022 spin_unlock(&bedata
->socket_lock
);
1023 pvcalls_exit_sock(sock
);
1026 sock
->sk
->sk_send_head
= NULL
;
1028 req
= RING_GET_REQUEST(&bedata
->ring
, req_id
);
1029 req
->req_id
= req_id
;
1030 req
->cmd
= PVCALLS_RELEASE
;
1031 req
->u
.release
.id
= (uintptr_t)map
;
1033 bedata
->ring
.req_prod_pvt
++;
1034 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata
->ring
, notify
);
1035 spin_unlock(&bedata
->socket_lock
);
1037 notify_remote_via_irq(bedata
->irq
);
1039 wait_event(bedata
->inflight_req
,
1040 READ_ONCE(bedata
->rsp
[req_id
].req_id
) == req_id
);
1042 if (map
->active_socket
) {
1044 * Set in_error and wake up inflight_conn_req to force
1045 * recvmsg waiters to exit.
1047 map
->active
.ring
->in_error
= -EBADF
;
1048 wake_up_interruptible(&map
->active
.inflight_conn_req
);
1051 * We need to make sure that sendmsg/recvmsg on this socket have
1052 * not started before we've cleared sk_send_head here. The
1053 * easiest way to guarantee this is to see that no pvcalls
1054 * (other than us) is in progress on this socket.
1056 while (atomic_read(&map
->refcount
) > 1)
1059 pvcalls_front_free_map(bedata
, map
);
1061 wake_up(&bedata
->inflight_req
);
1062 wake_up(&map
->passive
.inflight_accept_req
);
1064 while (atomic_read(&map
->refcount
) > 1)
1067 spin_lock(&bedata
->socket_lock
);
1068 list_del(&map
->list
);
1069 spin_unlock(&bedata
->socket_lock
);
1070 if (READ_ONCE(map
->passive
.inflight_req_id
) != PVCALLS_INVALID_ID
&&
1071 READ_ONCE(map
->passive
.inflight_req_id
) != 0) {
1072 pvcalls_front_free_map(bedata
,
1073 map
->passive
.accept_map
);
1077 WRITE_ONCE(bedata
->rsp
[req_id
].req_id
, PVCALLS_INVALID_ID
);
1083 static const struct xenbus_device_id pvcalls_front_ids
[] = {
1088 static int pvcalls_front_remove(struct xenbus_device
*dev
)
1090 struct pvcalls_bedata
*bedata
;
1091 struct sock_mapping
*map
= NULL
, *n
;
1093 bedata
= dev_get_drvdata(&pvcalls_front_dev
->dev
);
1094 dev_set_drvdata(&dev
->dev
, NULL
);
1095 pvcalls_front_dev
= NULL
;
1096 if (bedata
->irq
>= 0)
1097 unbind_from_irqhandler(bedata
->irq
, dev
);
1099 list_for_each_entry_safe(map
, n
, &bedata
->socket_mappings
, list
) {
1100 map
->sock
->sk
->sk_send_head
= NULL
;
1101 if (map
->active_socket
) {
1102 map
->active
.ring
->in_error
= -EBADF
;
1103 wake_up_interruptible(&map
->active
.inflight_conn_req
);
1108 while (atomic_read(&pvcalls_refcount
) > 0)
1110 list_for_each_entry_safe(map
, n
, &bedata
->socket_mappings
, list
) {
1111 if (map
->active_socket
) {
1112 /* No need to lock, refcount is 0 */
1113 pvcalls_front_free_map(bedata
, map
);
1115 list_del(&map
->list
);
1119 if (bedata
->ref
!= -1)
1120 gnttab_end_foreign_access(bedata
->ref
, 0, 0);
1121 kfree(bedata
->ring
.sring
);
1123 xenbus_switch_state(dev
, XenbusStateClosed
);
1127 static int pvcalls_front_probe(struct xenbus_device
*dev
,
1128 const struct xenbus_device_id
*id
)
1130 int ret
= -ENOMEM
, i
;
1131 evtchn_port_t evtchn
;
1132 unsigned int max_page_order
, function_calls
, len
;
1134 grant_ref_t gref_head
= 0;
1135 struct xenbus_transaction xbt
;
1136 struct pvcalls_bedata
*bedata
= NULL
;
1137 struct xen_pvcalls_sring
*sring
;
1139 if (pvcalls_front_dev
!= NULL
) {
1140 dev_err(&dev
->dev
, "only one PV Calls connection supported\n");
1144 versions
= xenbus_read(XBT_NIL
, dev
->otherend
, "versions", &len
);
1145 if (IS_ERR(versions
))
1146 return PTR_ERR(versions
);
1149 if (strcmp(versions
, "1")) {
1154 max_page_order
= xenbus_read_unsigned(dev
->otherend
,
1155 "max-page-order", 0);
1156 if (max_page_order
< PVCALLS_RING_ORDER
)
1158 function_calls
= xenbus_read_unsigned(dev
->otherend
,
1159 "function-calls", 0);
1160 /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
1161 if (function_calls
!= 1)
1163 pr_info("%s max-page-order is %u\n", __func__
, max_page_order
);
1165 bedata
= kzalloc(sizeof(struct pvcalls_bedata
), GFP_KERNEL
);
1169 dev_set_drvdata(&dev
->dev
, bedata
);
1170 pvcalls_front_dev
= dev
;
1171 init_waitqueue_head(&bedata
->inflight_req
);
1172 INIT_LIST_HEAD(&bedata
->socket_mappings
);
1173 spin_lock_init(&bedata
->socket_lock
);
1177 for (i
= 0; i
< PVCALLS_NR_RSP_PER_RING
; i
++)
1178 bedata
->rsp
[i
].req_id
= PVCALLS_INVALID_ID
;
1180 sring
= (struct xen_pvcalls_sring
*) __get_free_page(GFP_KERNEL
|
1184 SHARED_RING_INIT(sring
);
1185 FRONT_RING_INIT(&bedata
->ring
, sring
, XEN_PAGE_SIZE
);
1187 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
1191 bedata
->irq
= bind_evtchn_to_irqhandler(evtchn
,
1192 pvcalls_front_event_handler
,
1193 0, "pvcalls-frontend", dev
);
1194 if (bedata
->irq
< 0) {
1199 ret
= gnttab_alloc_grant_references(1, &gref_head
);
1202 ret
= gnttab_claim_grant_reference(&gref_head
);
1206 gnttab_grant_foreign_access_ref(bedata
->ref
, dev
->otherend_id
,
1207 virt_to_gfn((void *)sring
), 0);
1210 ret
= xenbus_transaction_start(&xbt
);
1212 xenbus_dev_fatal(dev
, ret
, "starting transaction");
1215 ret
= xenbus_printf(xbt
, dev
->nodename
, "version", "%u", 1);
1218 ret
= xenbus_printf(xbt
, dev
->nodename
, "ring-ref", "%d", bedata
->ref
);
1221 ret
= xenbus_printf(xbt
, dev
->nodename
, "port", "%u",
1225 ret
= xenbus_transaction_end(xbt
, 0);
1229 xenbus_dev_fatal(dev
, ret
, "completing transaction");
1232 xenbus_switch_state(dev
, XenbusStateInitialised
);
1237 xenbus_transaction_end(xbt
, 1);
1238 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
1240 pvcalls_front_remove(dev
);
1244 static void pvcalls_front_changed(struct xenbus_device
*dev
,
1245 enum xenbus_state backend_state
)
1247 switch (backend_state
) {
1248 case XenbusStateReconfiguring
:
1249 case XenbusStateReconfigured
:
1250 case XenbusStateInitialising
:
1251 case XenbusStateInitialised
:
1252 case XenbusStateUnknown
:
1255 case XenbusStateInitWait
:
1258 case XenbusStateConnected
:
1259 xenbus_switch_state(dev
, XenbusStateConnected
);
1262 case XenbusStateClosed
:
1263 if (dev
->state
== XenbusStateClosed
)
1265 /* Missed the backend's CLOSING state */
1267 case XenbusStateClosing
:
1268 xenbus_frontend_closed(dev
);
1273 static struct xenbus_driver pvcalls_front_driver
= {
1274 .ids
= pvcalls_front_ids
,
1275 .probe
= pvcalls_front_probe
,
1276 .remove
= pvcalls_front_remove
,
1277 .otherend_changed
= pvcalls_front_changed
,
1280 static int __init
pvcalls_frontend_init(void)
1285 pr_info("Initialising Xen pvcalls frontend driver\n");
1287 return xenbus_register_frontend(&pvcalls_front_driver
);
1290 module_init(pvcalls_frontend_init
);
1292 MODULE_DESCRIPTION("Xen PV Calls frontend driver");
1293 MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
1294 MODULE_LICENSE("GPL");