]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/xen/pvcalls-front.c
xen/pvcalls: Remove set but not used variable
[mirror_ubuntu-bionic-kernel.git] / drivers / xen / pvcalls-front.c
CommitLineData
416efba0
SS
1/*
2 * (c) 2017 Stefano Stabellini <stefano@aporeto.com>
3 *
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.
8 *
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.
13 */
14
15#include <linux/module.h>
2195046b
SS
16#include <linux/net.h>
17#include <linux/socket.h>
18
19#include <net/sock.h>
416efba0
SS
20
21#include <xen/events.h>
22#include <xen/grant_table.h>
23#include <xen/xen.h>
24#include <xen/xenbus.h>
25#include <xen/interface/io/pvcalls.h>
26
2195046b
SS
27#include "pvcalls-front.h"
28
aa7ba376
SS
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)
45ddce21 32#define PVCALLS_FRONT_MAX_SPIN 5000
aa7ba376 33
63be5d01
SS
34static struct proto pvcalls_proto = {
35 .name = "PVCalls",
36 .owner = THIS_MODULE,
37 .obj_size = sizeof(struct sock),
38};
39
aa7ba376
SS
40struct pvcalls_bedata {
41 struct xen_pvcalls_front_ring ring;
42 grant_ref_t ref;
43 int irq;
44
45 struct list_head socket_mappings;
46 spinlock_t socket_lock;
47
48 wait_queue_head_t inflight_req;
49 struct xen_pvcalls_response rsp[PVCALLS_NR_RSP_PER_RING];
50};
51/* Only one front/back connection supported. */
52static struct xenbus_device *pvcalls_front_dev;
53static atomic_t pvcalls_refcount;
54
55/* first increment refcount, then proceed */
56#define pvcalls_enter() { \
57 atomic_inc(&pvcalls_refcount); \
58}
59
60/* first complete other operations, then decrement refcount */
61#define pvcalls_exit() { \
62 atomic_dec(&pvcalls_refcount); \
63}
64
65struct sock_mapping {
66 bool active_socket;
67 struct list_head list;
68 struct socket *sock;
cb1c7d9b
SS
69 union {
70 struct {
71 int irq;
72 grant_ref_t ref;
73 struct pvcalls_data_intf *ring;
74 struct pvcalls_data data;
75 struct mutex in_mutex;
76 struct mutex out_mutex;
77
78 wait_queue_head_t inflight_conn_req;
79 } active;
67ea9893
SS
80 struct {
81 /* Socket status */
82#define PVCALLS_STATUS_UNINITALIZED 0
83#define PVCALLS_STATUS_BIND 1
84#define PVCALLS_STATUS_LISTEN 2
85 uint8_t status;
9774c6cc
SS
86 /*
87 * Internal state-machine flags.
88 * Only one accept operation can be inflight for a socket.
89 * Only one poll operation can be inflight for a given socket.
90 */
91#define PVCALLS_FLAG_ACCEPT_INFLIGHT 0
5842c835
SS
92#define PVCALLS_FLAG_POLL_INFLIGHT 1
93#define PVCALLS_FLAG_POLL_RET 2
9774c6cc
SS
94 uint8_t flags;
95 uint32_t inflight_req_id;
96 struct sock_mapping *accept_map;
97 wait_queue_head_t inflight_accept_req;
67ea9893 98 } passive;
cb1c7d9b 99 };
aa7ba376
SS
100};
101
2195046b
SS
102static inline int get_request(struct pvcalls_bedata *bedata, int *req_id)
103{
104 *req_id = bedata->ring.req_prod_pvt & (RING_SIZE(&bedata->ring) - 1);
105 if (RING_FULL(&bedata->ring) ||
106 bedata->rsp[*req_id].req_id != PVCALLS_INVALID_ID)
107 return -EAGAIN;
108 return 0;
109}
110
45ddce21
SS
111static bool pvcalls_front_write_todo(struct sock_mapping *map)
112{
113 struct pvcalls_data_intf *intf = map->active.ring;
114 RING_IDX cons, prod, size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
115 int32_t error;
116
117 error = intf->out_error;
118 if (error == -ENOTCONN)
119 return false;
120 if (error != 0)
121 return true;
122
123 cons = intf->out_cons;
124 prod = intf->out_prod;
125 return !!(size - pvcalls_queued(prod, cons, size));
126}
127
ae0d0405
SS
128static bool pvcalls_front_read_todo(struct sock_mapping *map)
129{
130 struct pvcalls_data_intf *intf = map->active.ring;
131 RING_IDX cons, prod;
132 int32_t error;
133
134 cons = intf->in_cons;
135 prod = intf->in_prod;
136 error = intf->in_error;
137 return (error != 0 ||
138 pvcalls_queued(prod, cons,
139 XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER)) != 0);
140}
141
aa7ba376
SS
142static irqreturn_t pvcalls_front_event_handler(int irq, void *dev_id)
143{
2195046b
SS
144 struct xenbus_device *dev = dev_id;
145 struct pvcalls_bedata *bedata;
146 struct xen_pvcalls_response *rsp;
147 uint8_t *src, *dst;
148 int req_id = 0, more = 0, done = 0;
149
150 if (dev == NULL)
151 return IRQ_HANDLED;
152
153 pvcalls_enter();
154 bedata = dev_get_drvdata(&dev->dev);
155 if (bedata == NULL) {
156 pvcalls_exit();
157 return IRQ_HANDLED;
158 }
159
160again:
161 while (RING_HAS_UNCONSUMED_RESPONSES(&bedata->ring)) {
162 rsp = RING_GET_RESPONSE(&bedata->ring, bedata->ring.rsp_cons);
163
164 req_id = rsp->req_id;
5842c835
SS
165 if (rsp->cmd == PVCALLS_POLL) {
166 struct sock_mapping *map = (struct sock_mapping *)(uintptr_t)
167 rsp->u.poll.id;
168
169 clear_bit(PVCALLS_FLAG_POLL_INFLIGHT,
170 (void *)&map->passive.flags);
171 /*
172 * clear INFLIGHT, then set RET. It pairs with
173 * the checks at the beginning of
174 * pvcalls_front_poll_passive.
175 */
176 smp_wmb();
177 set_bit(PVCALLS_FLAG_POLL_RET,
178 (void *)&map->passive.flags);
179 } else {
180 dst = (uint8_t *)&bedata->rsp[req_id] +
181 sizeof(rsp->req_id);
182 src = (uint8_t *)rsp + sizeof(rsp->req_id);
183 memcpy(dst, src, sizeof(*rsp) - sizeof(rsp->req_id));
184 /*
185 * First copy the rest of the data, then req_id. It is
186 * paired with the barrier when accessing bedata->rsp.
187 */
188 smp_wmb();
189 bedata->rsp[req_id].req_id = req_id;
190 }
2195046b
SS
191
192 done = 1;
193 bedata->ring.rsp_cons++;
194 }
195
196 RING_FINAL_CHECK_FOR_RESPONSES(&bedata->ring, more);
197 if (more)
198 goto again;
199 if (done)
200 wake_up(&bedata->inflight_req);
201 pvcalls_exit();
aa7ba376
SS
202 return IRQ_HANDLED;
203}
204
205static void pvcalls_front_free_map(struct pvcalls_bedata *bedata,
206 struct sock_mapping *map)
207{
235a71c5
SS
208 int i;
209
210 unbind_from_irqhandler(map->active.irq, map);
211
212 spin_lock(&bedata->socket_lock);
213 if (!list_empty(&map->list))
214 list_del_init(&map->list);
215 spin_unlock(&bedata->socket_lock);
216
217 for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
218 gnttab_end_foreign_access(map->active.ring->ref[i], 0, 0);
219 gnttab_end_foreign_access(map->active.ref, 0, 0);
220 free_page((unsigned long)map->active.ring);
221
222 kfree(map);
aa7ba376
SS
223}
224
cb1c7d9b
SS
225static irqreturn_t pvcalls_front_conn_handler(int irq, void *sock_map)
226{
227 struct sock_mapping *map = sock_map;
228
229 if (map == NULL)
230 return IRQ_HANDLED;
231
232 wake_up_interruptible(&map->active.inflight_conn_req);
233
234 return IRQ_HANDLED;
235}
236
2195046b
SS
237int pvcalls_front_socket(struct socket *sock)
238{
239 struct pvcalls_bedata *bedata;
240 struct sock_mapping *map = NULL;
241 struct xen_pvcalls_request *req;
242 int notify, req_id, ret;
243
244 /*
245 * PVCalls only supports domain AF_INET,
246 * type SOCK_STREAM and protocol 0 sockets for now.
247 *
248 * Check socket type here, AF_INET and protocol checks are done
249 * by the caller.
250 */
251 if (sock->type != SOCK_STREAM)
252 return -EOPNOTSUPP;
253
254 pvcalls_enter();
255 if (!pvcalls_front_dev) {
256 pvcalls_exit();
257 return -EACCES;
258 }
259 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
260
261 map = kzalloc(sizeof(*map), GFP_KERNEL);
262 if (map == NULL) {
263 pvcalls_exit();
264 return -ENOMEM;
265 }
266
267 spin_lock(&bedata->socket_lock);
268
269 ret = get_request(bedata, &req_id);
270 if (ret < 0) {
271 kfree(map);
272 spin_unlock(&bedata->socket_lock);
273 pvcalls_exit();
274 return ret;
275 }
276
277 /*
278 * sock->sk->sk_send_head is not used for ip sockets: reuse the
279 * field to store a pointer to the struct sock_mapping
280 * corresponding to the socket. This way, we can easily get the
281 * struct sock_mapping from the struct socket.
282 */
283 sock->sk->sk_send_head = (void *)map;
284 list_add_tail(&map->list, &bedata->socket_mappings);
285
286 req = RING_GET_REQUEST(&bedata->ring, req_id);
287 req->req_id = req_id;
288 req->cmd = PVCALLS_SOCKET;
289 req->u.socket.id = (uintptr_t) map;
290 req->u.socket.domain = AF_INET;
291 req->u.socket.type = SOCK_STREAM;
292 req->u.socket.protocol = IPPROTO_IP;
293
294 bedata->ring.req_prod_pvt++;
295 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
296 spin_unlock(&bedata->socket_lock);
297 if (notify)
298 notify_remote_via_irq(bedata->irq);
299
300 wait_event(bedata->inflight_req,
301 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
302
303 /* read req_id, then the content */
304 smp_rmb();
305 ret = bedata->rsp[req_id].ret;
306 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
307
308 pvcalls_exit();
309 return ret;
310}
311
cb1c7d9b
SS
312static int create_active(struct sock_mapping *map, int *evtchn)
313{
314 void *bytes;
315 int ret = -ENOMEM, irq = -1, i;
316
317 *evtchn = -1;
318 init_waitqueue_head(&map->active.inflight_conn_req);
319
320 map->active.ring = (struct pvcalls_data_intf *)
321 __get_free_page(GFP_KERNEL | __GFP_ZERO);
322 if (map->active.ring == NULL)
323 goto out_error;
324 map->active.ring->ring_order = PVCALLS_RING_ORDER;
325 bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
326 PVCALLS_RING_ORDER);
327 if (bytes == NULL)
328 goto out_error;
329 for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
330 map->active.ring->ref[i] = gnttab_grant_foreign_access(
331 pvcalls_front_dev->otherend_id,
332 pfn_to_gfn(virt_to_pfn(bytes) + i), 0);
333
334 map->active.ref = gnttab_grant_foreign_access(
335 pvcalls_front_dev->otherend_id,
336 pfn_to_gfn(virt_to_pfn((void *)map->active.ring)), 0);
337
338 map->active.data.in = bytes;
339 map->active.data.out = bytes +
340 XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
341
342 ret = xenbus_alloc_evtchn(pvcalls_front_dev, evtchn);
343 if (ret)
344 goto out_error;
345 irq = bind_evtchn_to_irqhandler(*evtchn, pvcalls_front_conn_handler,
346 0, "pvcalls-frontend", map);
347 if (irq < 0) {
348 ret = irq;
349 goto out_error;
350 }
351
352 map->active.irq = irq;
353 map->active_socket = true;
354 mutex_init(&map->active.in_mutex);
355 mutex_init(&map->active.out_mutex);
356
357 return 0;
358
359out_error:
773aaadc 360 if (*evtchn >= 0)
cb1c7d9b 361 xenbus_free_evtchn(pvcalls_front_dev, *evtchn);
e7881bba
PB
362 free_pages((unsigned long)map->active.data.in, PVCALLS_RING_ORDER);
363 free_page((unsigned long)map->active.ring);
cb1c7d9b
SS
364 return ret;
365}
366
367int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
368 int addr_len, int flags)
369{
370 struct pvcalls_bedata *bedata;
371 struct sock_mapping *map = NULL;
372 struct xen_pvcalls_request *req;
373 int notify, req_id, ret, evtchn;
374
375 if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
376 return -EOPNOTSUPP;
377
378 pvcalls_enter();
379 if (!pvcalls_front_dev) {
380 pvcalls_exit();
381 return -ENOTCONN;
382 }
383
384 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
385
386 map = (struct sock_mapping *)sock->sk->sk_send_head;
387 if (!map) {
388 pvcalls_exit();
389 return -ENOTSOCK;
390 }
391
392 spin_lock(&bedata->socket_lock);
393 ret = get_request(bedata, &req_id);
394 if (ret < 0) {
395 spin_unlock(&bedata->socket_lock);
396 pvcalls_exit();
397 return ret;
398 }
399 ret = create_active(map, &evtchn);
400 if (ret < 0) {
401 spin_unlock(&bedata->socket_lock);
402 pvcalls_exit();
403 return ret;
404 }
405
406 req = RING_GET_REQUEST(&bedata->ring, req_id);
407 req->req_id = req_id;
408 req->cmd = PVCALLS_CONNECT;
409 req->u.connect.id = (uintptr_t)map;
410 req->u.connect.len = addr_len;
411 req->u.connect.flags = flags;
412 req->u.connect.ref = map->active.ref;
413 req->u.connect.evtchn = evtchn;
414 memcpy(req->u.connect.addr, addr, sizeof(*addr));
415
416 map->sock = sock;
417
418 bedata->ring.req_prod_pvt++;
419 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
420 spin_unlock(&bedata->socket_lock);
421
422 if (notify)
423 notify_remote_via_irq(bedata->irq);
424
425 wait_event(bedata->inflight_req,
426 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
427
428 /* read req_id, then the content */
429 smp_rmb();
430 ret = bedata->rsp[req_id].ret;
431 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
432 pvcalls_exit();
433 return ret;
434}
435
45ddce21
SS
436static int __write_ring(struct pvcalls_data_intf *intf,
437 struct pvcalls_data *data,
438 struct iov_iter *msg_iter,
439 int len)
440{
441 RING_IDX cons, prod, size, masked_prod, masked_cons;
442 RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
443 int32_t error;
444
445 error = intf->out_error;
446 if (error < 0)
447 return error;
448 cons = intf->out_cons;
449 prod = intf->out_prod;
450 /* read indexes before continuing */
451 virt_mb();
452
453 size = pvcalls_queued(prod, cons, array_size);
454 if (size >= array_size)
455 return -EINVAL;
456 if (len > array_size - size)
457 len = array_size - size;
458
459 masked_prod = pvcalls_mask(prod, array_size);
460 masked_cons = pvcalls_mask(cons, array_size);
461
462 if (masked_prod < masked_cons) {
463 len = copy_from_iter(data->out + masked_prod, len, msg_iter);
464 } else {
465 if (len > array_size - masked_prod) {
466 int ret = copy_from_iter(data->out + masked_prod,
467 array_size - masked_prod, msg_iter);
468 if (ret != array_size - masked_prod) {
469 len = ret;
470 goto out;
471 }
472 len = ret + copy_from_iter(data->out, len - ret, msg_iter);
473 } else {
474 len = copy_from_iter(data->out + masked_prod, len, msg_iter);
475 }
476 }
477out:
478 /* write to ring before updating pointer */
479 virt_wmb();
480 intf->out_prod += len;
481
482 return len;
483}
484
485int pvcalls_front_sendmsg(struct socket *sock, struct msghdr *msg,
486 size_t len)
487{
45ddce21
SS
488 struct sock_mapping *map;
489 int sent, tot_sent = 0;
490 int count = 0, flags;
491
492 flags = msg->msg_flags;
493 if (flags & (MSG_CONFIRM|MSG_DONTROUTE|MSG_EOR|MSG_OOB))
494 return -EOPNOTSUPP;
495
496 pvcalls_enter();
497 if (!pvcalls_front_dev) {
498 pvcalls_exit();
499 return -ENOTCONN;
500 }
45ddce21
SS
501
502 map = (struct sock_mapping *) sock->sk->sk_send_head;
503 if (!map) {
504 pvcalls_exit();
505 return -ENOTSOCK;
506 }
507
508 mutex_lock(&map->active.out_mutex);
509 if ((flags & MSG_DONTWAIT) && !pvcalls_front_write_todo(map)) {
510 mutex_unlock(&map->active.out_mutex);
511 pvcalls_exit();
512 return -EAGAIN;
513 }
514 if (len > INT_MAX)
515 len = INT_MAX;
516
517again:
518 count++;
519 sent = __write_ring(map->active.ring,
520 &map->active.data, &msg->msg_iter,
521 len);
522 if (sent > 0) {
523 len -= sent;
524 tot_sent += sent;
525 notify_remote_via_irq(map->active.irq);
526 }
527 if (sent >= 0 && len > 0 && count < PVCALLS_FRONT_MAX_SPIN)
528 goto again;
529 if (sent < 0)
530 tot_sent = sent;
531
532 mutex_unlock(&map->active.out_mutex);
533 pvcalls_exit();
534 return tot_sent;
535}
536
ae0d0405
SS
537static int __read_ring(struct pvcalls_data_intf *intf,
538 struct pvcalls_data *data,
539 struct iov_iter *msg_iter,
540 size_t len, int flags)
541{
542 RING_IDX cons, prod, size, masked_prod, masked_cons;
543 RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
544 int32_t error;
545
546 cons = intf->in_cons;
547 prod = intf->in_prod;
548 error = intf->in_error;
549 /* get pointers before reading from the ring */
550 virt_rmb();
ae0d0405
SS
551
552 size = pvcalls_queued(prod, cons, array_size);
553 masked_prod = pvcalls_mask(prod, array_size);
554 masked_cons = pvcalls_mask(cons, array_size);
555
556 if (size == 0)
ee88f289 557 return error ?: size;
ae0d0405
SS
558
559 if (len > size)
560 len = size;
561
562 if (masked_prod > masked_cons) {
563 len = copy_to_iter(data->in + masked_cons, len, msg_iter);
564 } else {
565 if (len > (array_size - masked_cons)) {
566 int ret = copy_to_iter(data->in + masked_cons,
567 array_size - masked_cons, msg_iter);
568 if (ret != array_size - masked_cons) {
569 len = ret;
570 goto out;
571 }
572 len = ret + copy_to_iter(data->in, len - ret, msg_iter);
573 } else {
574 len = copy_to_iter(data->in + masked_cons, len, msg_iter);
575 }
576 }
577out:
578 /* read data from the ring before increasing the index */
579 virt_mb();
580 if (!(flags & MSG_PEEK))
581 intf->in_cons += len;
582
583 return len;
584}
585
586int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
587 int flags)
588{
ae0d0405
SS
589 int ret;
590 struct sock_mapping *map;
591
592 if (flags & (MSG_CMSG_CLOEXEC|MSG_ERRQUEUE|MSG_OOB|MSG_TRUNC))
593 return -EOPNOTSUPP;
594
595 pvcalls_enter();
596 if (!pvcalls_front_dev) {
597 pvcalls_exit();
598 return -ENOTCONN;
599 }
ae0d0405
SS
600
601 map = (struct sock_mapping *) sock->sk->sk_send_head;
602 if (!map) {
603 pvcalls_exit();
604 return -ENOTSOCK;
605 }
606
607 mutex_lock(&map->active.in_mutex);
608 if (len > XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER))
609 len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
610
611 while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) {
612 wait_event_interruptible(map->active.inflight_conn_req,
613 pvcalls_front_read_todo(map));
614 }
615 ret = __read_ring(map->active.ring, &map->active.data,
616 &msg->msg_iter, len, flags);
617
618 if (ret > 0)
619 notify_remote_via_irq(map->active.irq);
620 if (ret == 0)
621 ret = (flags & MSG_DONTWAIT) ? -EAGAIN : 0;
622 if (ret == -ENOTCONN)
623 ret = 0;
624
625 mutex_unlock(&map->active.in_mutex);
626 pvcalls_exit();
627 return ret;
628}
629
67ea9893
SS
630int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
631{
632 struct pvcalls_bedata *bedata;
633 struct sock_mapping *map = NULL;
634 struct xen_pvcalls_request *req;
635 int notify, req_id, ret;
636
637 if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
638 return -EOPNOTSUPP;
639
640 pvcalls_enter();
641 if (!pvcalls_front_dev) {
642 pvcalls_exit();
643 return -ENOTCONN;
644 }
645 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
646
647 map = (struct sock_mapping *) sock->sk->sk_send_head;
648 if (map == NULL) {
649 pvcalls_exit();
650 return -ENOTSOCK;
651 }
652
653 spin_lock(&bedata->socket_lock);
654 ret = get_request(bedata, &req_id);
655 if (ret < 0) {
656 spin_unlock(&bedata->socket_lock);
657 pvcalls_exit();
658 return ret;
659 }
660 req = RING_GET_REQUEST(&bedata->ring, req_id);
661 req->req_id = req_id;
662 map->sock = sock;
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;
667
9774c6cc
SS
668 init_waitqueue_head(&map->passive.inflight_accept_req);
669
67ea9893
SS
670 map->active_socket = false;
671
672 bedata->ring.req_prod_pvt++;
673 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
674 spin_unlock(&bedata->socket_lock);
675 if (notify)
676 notify_remote_via_irq(bedata->irq);
677
678 wait_event(bedata->inflight_req,
679 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
680
681 /* read req_id, then the content */
682 smp_rmb();
683 ret = bedata->rsp[req_id].ret;
684 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
685
686 map->passive.status = PVCALLS_STATUS_BIND;
687 pvcalls_exit();
688 return 0;
689}
690
1853f11d
SS
691int pvcalls_front_listen(struct socket *sock, int backlog)
692{
693 struct pvcalls_bedata *bedata;
694 struct sock_mapping *map;
695 struct xen_pvcalls_request *req;
696 int notify, req_id, ret;
697
698 pvcalls_enter();
699 if (!pvcalls_front_dev) {
700 pvcalls_exit();
701 return -ENOTCONN;
702 }
703 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
704
705 map = (struct sock_mapping *) sock->sk->sk_send_head;
706 if (!map) {
707 pvcalls_exit();
708 return -ENOTSOCK;
709 }
710
711 if (map->passive.status != PVCALLS_STATUS_BIND) {
712 pvcalls_exit();
713 return -EOPNOTSUPP;
714 }
715
716 spin_lock(&bedata->socket_lock);
717 ret = get_request(bedata, &req_id);
718 if (ret < 0) {
719 spin_unlock(&bedata->socket_lock);
720 pvcalls_exit();
721 return ret;
722 }
723 req = RING_GET_REQUEST(&bedata->ring, req_id);
724 req->req_id = req_id;
725 req->cmd = PVCALLS_LISTEN;
726 req->u.listen.id = (uintptr_t) map;
727 req->u.listen.backlog = backlog;
728
729 bedata->ring.req_prod_pvt++;
730 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
731 spin_unlock(&bedata->socket_lock);
732 if (notify)
733 notify_remote_via_irq(bedata->irq);
734
735 wait_event(bedata->inflight_req,
736 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
737
738 /* read req_id, then the content */
739 smp_rmb();
740 ret = bedata->rsp[req_id].ret;
741 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
742
743 map->passive.status = PVCALLS_STATUS_LISTEN;
744 pvcalls_exit();
745 return ret;
746}
747
9774c6cc
SS
748int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
749{
750 struct pvcalls_bedata *bedata;
751 struct sock_mapping *map;
752 struct sock_mapping *map2 = NULL;
753 struct xen_pvcalls_request *req;
754 int notify, req_id, ret, evtchn, nonblock;
755
756 pvcalls_enter();
757 if (!pvcalls_front_dev) {
758 pvcalls_exit();
759 return -ENOTCONN;
760 }
761 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
762
763 map = (struct sock_mapping *) sock->sk->sk_send_head;
764 if (!map) {
765 pvcalls_exit();
766 return -ENOTSOCK;
767 }
768
769 if (map->passive.status != PVCALLS_STATUS_LISTEN) {
770 pvcalls_exit();
771 return -EINVAL;
772 }
773
774 nonblock = flags & SOCK_NONBLOCK;
775 /*
776 * Backend only supports 1 inflight accept request, will return
777 * errors for the others
778 */
779 if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
780 (void *)&map->passive.flags)) {
781 req_id = READ_ONCE(map->passive.inflight_req_id);
782 if (req_id != PVCALLS_INVALID_ID &&
783 READ_ONCE(bedata->rsp[req_id].req_id) == req_id) {
784 map2 = map->passive.accept_map;
785 goto received;
786 }
787 if (nonblock) {
788 pvcalls_exit();
789 return -EAGAIN;
790 }
791 if (wait_event_interruptible(map->passive.inflight_accept_req,
792 !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
793 (void *)&map->passive.flags))) {
794 pvcalls_exit();
795 return -EINTR;
796 }
797 }
798
799 spin_lock(&bedata->socket_lock);
800 ret = get_request(bedata, &req_id);
801 if (ret < 0) {
802 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
803 (void *)&map->passive.flags);
804 spin_unlock(&bedata->socket_lock);
805 pvcalls_exit();
806 return ret;
807 }
4aac2caf 808 map2 = kzalloc(sizeof(*map2), GFP_ATOMIC);
9774c6cc
SS
809 if (map2 == NULL) {
810 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
811 (void *)&map->passive.flags);
812 spin_unlock(&bedata->socket_lock);
813 pvcalls_exit();
814 return -ENOMEM;
815 }
816 ret = create_active(map2, &evtchn);
817 if (ret < 0) {
818 kfree(map2);
819 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
820 (void *)&map->passive.flags);
821 spin_unlock(&bedata->socket_lock);
822 pvcalls_exit();
823 return ret;
824 }
825 list_add_tail(&map2->list, &bedata->socket_mappings);
826
827 req = RING_GET_REQUEST(&bedata->ring, req_id);
828 req->req_id = req_id;
829 req->cmd = PVCALLS_ACCEPT;
830 req->u.accept.id = (uintptr_t) map;
831 req->u.accept.ref = map2->active.ref;
832 req->u.accept.id_new = (uintptr_t) map2;
833 req->u.accept.evtchn = evtchn;
834 map->passive.accept_map = map2;
835
836 bedata->ring.req_prod_pvt++;
837 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
838 spin_unlock(&bedata->socket_lock);
839 if (notify)
840 notify_remote_via_irq(bedata->irq);
841 /* We could check if we have received a response before returning. */
842 if (nonblock) {
843 WRITE_ONCE(map->passive.inflight_req_id, req_id);
844 pvcalls_exit();
845 return -EAGAIN;
846 }
847
848 if (wait_event_interruptible(bedata->inflight_req,
849 READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) {
850 pvcalls_exit();
851 return -EINTR;
852 }
853 /* read req_id, then the content */
854 smp_rmb();
855
856received:
857 map2->sock = newsock;
63be5d01 858 newsock->sk = sk_alloc(sock_net(sock->sk), PF_INET, GFP_KERNEL, &pvcalls_proto, false);
9774c6cc
SS
859 if (!newsock->sk) {
860 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
861 map->passive.inflight_req_id = PVCALLS_INVALID_ID;
862 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
863 (void *)&map->passive.flags);
864 pvcalls_front_free_map(bedata, map2);
865 pvcalls_exit();
866 return -ENOMEM;
867 }
868 newsock->sk->sk_send_head = (void *)map2;
869
870 ret = bedata->rsp[req_id].ret;
871 bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
872 map->passive.inflight_req_id = PVCALLS_INVALID_ID;
873
874 clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags);
875 wake_up(&map->passive.inflight_accept_req);
876
877 pvcalls_exit();
878 return ret;
879}
880
5842c835
SS
881static unsigned int pvcalls_front_poll_passive(struct file *file,
882 struct pvcalls_bedata *bedata,
883 struct sock_mapping *map,
884 poll_table *wait)
885{
886 int notify, req_id, ret;
887 struct xen_pvcalls_request *req;
888
889 if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
890 (void *)&map->passive.flags)) {
891 uint32_t req_id = READ_ONCE(map->passive.inflight_req_id);
892
893 if (req_id != PVCALLS_INVALID_ID &&
894 READ_ONCE(bedata->rsp[req_id].req_id) == req_id)
895 return POLLIN | POLLRDNORM;
896
897 poll_wait(file, &map->passive.inflight_accept_req, wait);
898 return 0;
899 }
900
901 if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET,
902 (void *)&map->passive.flags))
903 return POLLIN | POLLRDNORM;
904
905 /*
906 * First check RET, then INFLIGHT. No barriers necessary to
907 * ensure execution ordering because of the conditional
908 * instructions creating control dependencies.
909 */
910
911 if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT,
912 (void *)&map->passive.flags)) {
913 poll_wait(file, &bedata->inflight_req, wait);
914 return 0;
915 }
916
917 spin_lock(&bedata->socket_lock);
918 ret = get_request(bedata, &req_id);
919 if (ret < 0) {
920 spin_unlock(&bedata->socket_lock);
921 return ret;
922 }
923 req = RING_GET_REQUEST(&bedata->ring, req_id);
924 req->req_id = req_id;
925 req->cmd = PVCALLS_POLL;
926 req->u.poll.id = (uintptr_t) map;
927
928 bedata->ring.req_prod_pvt++;
929 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
930 spin_unlock(&bedata->socket_lock);
931 if (notify)
932 notify_remote_via_irq(bedata->irq);
933
934 poll_wait(file, &bedata->inflight_req, wait);
935 return 0;
936}
937
938static unsigned int pvcalls_front_poll_active(struct file *file,
939 struct pvcalls_bedata *bedata,
940 struct sock_mapping *map,
941 poll_table *wait)
942{
943 unsigned int mask = 0;
944 int32_t in_error, out_error;
945 struct pvcalls_data_intf *intf = map->active.ring;
946
947 out_error = intf->out_error;
948 in_error = intf->in_error;
949
950 poll_wait(file, &map->active.inflight_conn_req, wait);
951 if (pvcalls_front_write_todo(map))
952 mask |= POLLOUT | POLLWRNORM;
953 if (pvcalls_front_read_todo(map))
954 mask |= POLLIN | POLLRDNORM;
955 if (in_error != 0 || out_error != 0)
956 mask |= POLLERR;
957
958 return mask;
959}
960
961unsigned int pvcalls_front_poll(struct file *file, struct socket *sock,
962 poll_table *wait)
963{
964 struct pvcalls_bedata *bedata;
965 struct sock_mapping *map;
966 int ret;
967
968 pvcalls_enter();
969 if (!pvcalls_front_dev) {
970 pvcalls_exit();
971 return POLLNVAL;
972 }
973 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
974
975 map = (struct sock_mapping *) sock->sk->sk_send_head;
976 if (!map) {
977 pvcalls_exit();
978 return POLLNVAL;
979 }
980 if (map->active_socket)
981 ret = pvcalls_front_poll_active(file, bedata, map, wait);
982 else
983 ret = pvcalls_front_poll_passive(file, bedata, map, wait);
984 pvcalls_exit();
985 return ret;
986}
987
235a71c5
SS
988int pvcalls_front_release(struct socket *sock)
989{
990 struct pvcalls_bedata *bedata;
991 struct sock_mapping *map;
992 int req_id, notify, ret;
993 struct xen_pvcalls_request *req;
994
995 if (sock->sk == NULL)
996 return 0;
997
998 pvcalls_enter();
999 if (!pvcalls_front_dev) {
1000 pvcalls_exit();
1001 return -EIO;
1002 }
1003
1004 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
1005
1006 map = (struct sock_mapping *) sock->sk->sk_send_head;
1007 if (map == NULL) {
1008 pvcalls_exit();
1009 return 0;
1010 }
1011
1012 spin_lock(&bedata->socket_lock);
1013 ret = get_request(bedata, &req_id);
1014 if (ret < 0) {
1015 spin_unlock(&bedata->socket_lock);
1016 pvcalls_exit();
1017 return ret;
1018 }
1019 sock->sk->sk_send_head = NULL;
1020
1021 req = RING_GET_REQUEST(&bedata->ring, req_id);
1022 req->req_id = req_id;
1023 req->cmd = PVCALLS_RELEASE;
1024 req->u.release.id = (uintptr_t)map;
1025
1026 bedata->ring.req_prod_pvt++;
1027 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
1028 spin_unlock(&bedata->socket_lock);
1029 if (notify)
1030 notify_remote_via_irq(bedata->irq);
1031
1032 wait_event(bedata->inflight_req,
1033 READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
1034
1035 if (map->active_socket) {
1036 /*
1037 * Set in_error and wake up inflight_conn_req to force
1038 * recvmsg waiters to exit.
1039 */
1040 map->active.ring->in_error = -EBADF;
1041 wake_up_interruptible(&map->active.inflight_conn_req);
1042
1043 /*
646d944c
SS
1044 * We need to make sure that sendmsg/recvmsg on this socket have
1045 * not started before we've cleared sk_send_head here. The
1046 * easiest (though not optimal) way to guarantee this is to see
1047 * that no pvcall (other than us) is in progress.
235a71c5 1048 */
646d944c 1049 while (atomic_read(&pvcalls_refcount) > 1)
235a71c5
SS
1050 cpu_relax();
1051
1052 pvcalls_front_free_map(bedata, map);
1053 } else {
1054 spin_lock(&bedata->socket_lock);
1055 list_del(&map->list);
1056 spin_unlock(&bedata->socket_lock);
c2c273f9
SS
1057 if (READ_ONCE(map->passive.inflight_req_id) != PVCALLS_INVALID_ID &&
1058 READ_ONCE(map->passive.inflight_req_id) != 0) {
235a71c5
SS
1059 pvcalls_front_free_map(bedata,
1060 map->passive.accept_map);
1061 }
1062 kfree(map);
1063 }
1064 WRITE_ONCE(bedata->rsp[req_id].req_id, PVCALLS_INVALID_ID);
1065
1066 pvcalls_exit();
1067 return 0;
1068}
1069
416efba0
SS
1070static const struct xenbus_device_id pvcalls_front_ids[] = {
1071 { "pvcalls" },
1072 { "" }
1073};
1074
1075static int pvcalls_front_remove(struct xenbus_device *dev)
1076{
aa7ba376
SS
1077 struct pvcalls_bedata *bedata;
1078 struct sock_mapping *map = NULL, *n;
1079
1080 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
1081 dev_set_drvdata(&dev->dev, NULL);
1082 pvcalls_front_dev = NULL;
1083 if (bedata->irq >= 0)
1084 unbind_from_irqhandler(bedata->irq, dev);
1085
cb1c7d9b
SS
1086 list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
1087 map->sock->sk->sk_send_head = NULL;
1088 if (map->active_socket) {
1089 map->active.ring->in_error = -EBADF;
1090 wake_up_interruptible(&map->active.inflight_conn_req);
1091 }
1092 }
1093
aa7ba376
SS
1094 smp_mb();
1095 while (atomic_read(&pvcalls_refcount) > 0)
1096 cpu_relax();
1097 list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
1098 if (map->active_socket) {
1099 /* No need to lock, refcount is 0 */
1100 pvcalls_front_free_map(bedata, map);
1101 } else {
1102 list_del(&map->list);
1103 kfree(map);
1104 }
1105 }
1ab134ca 1106 if (bedata->ref != -1)
aa7ba376
SS
1107 gnttab_end_foreign_access(bedata->ref, 0, 0);
1108 kfree(bedata->ring.sring);
1109 kfree(bedata);
1110 xenbus_switch_state(dev, XenbusStateClosed);
416efba0
SS
1111 return 0;
1112}
1113
1114static int pvcalls_front_probe(struct xenbus_device *dev,
1115 const struct xenbus_device_id *id)
1116{
21968190
SS
1117 int ret = -ENOMEM, evtchn, i;
1118 unsigned int max_page_order, function_calls, len;
1119 char *versions;
1120 grant_ref_t gref_head = 0;
1121 struct xenbus_transaction xbt;
1122 struct pvcalls_bedata *bedata = NULL;
1123 struct xen_pvcalls_sring *sring;
1124
1125 if (pvcalls_front_dev != NULL) {
1126 dev_err(&dev->dev, "only one PV Calls connection supported\n");
1127 return -EINVAL;
1128 }
1129
1130 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
8c71fa88
DC
1131 if (IS_ERR(versions))
1132 return PTR_ERR(versions);
21968190
SS
1133 if (!len)
1134 return -EINVAL;
1135 if (strcmp(versions, "1")) {
1136 kfree(versions);
1137 return -EINVAL;
1138 }
1139 kfree(versions);
1140 max_page_order = xenbus_read_unsigned(dev->otherend,
1141 "max-page-order", 0);
1142 if (max_page_order < PVCALLS_RING_ORDER)
1143 return -ENODEV;
1144 function_calls = xenbus_read_unsigned(dev->otherend,
1145 "function-calls", 0);
1146 /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
1147 if (function_calls != 1)
1148 return -ENODEV;
1149 pr_info("%s max-page-order is %u\n", __func__, max_page_order);
1150
1151 bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL);
1152 if (!bedata)
1153 return -ENOMEM;
1154
1155 dev_set_drvdata(&dev->dev, bedata);
1156 pvcalls_front_dev = dev;
1157 init_waitqueue_head(&bedata->inflight_req);
1158 INIT_LIST_HEAD(&bedata->socket_mappings);
1159 spin_lock_init(&bedata->socket_lock);
1160 bedata->irq = -1;
1161 bedata->ref = -1;
1162
1163 for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++)
1164 bedata->rsp[i].req_id = PVCALLS_INVALID_ID;
1165
1166 sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL |
1167 __GFP_ZERO);
1168 if (!sring)
1169 goto error;
1170 SHARED_RING_INIT(sring);
1171 FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE);
1172
1173 ret = xenbus_alloc_evtchn(dev, &evtchn);
1174 if (ret)
1175 goto error;
1176
1177 bedata->irq = bind_evtchn_to_irqhandler(evtchn,
1178 pvcalls_front_event_handler,
1179 0, "pvcalls-frontend", dev);
1180 if (bedata->irq < 0) {
1181 ret = bedata->irq;
1182 goto error;
1183 }
1184
1185 ret = gnttab_alloc_grant_references(1, &gref_head);
1186 if (ret < 0)
1187 goto error;
95110ac8
CIK
1188 ret = gnttab_claim_grant_reference(&gref_head);
1189 if (ret < 0)
21968190 1190 goto error;
95110ac8 1191 bedata->ref = ret;
21968190
SS
1192 gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id,
1193 virt_to_gfn((void *)sring), 0);
1194
1195 again:
1196 ret = xenbus_transaction_start(&xbt);
1197 if (ret) {
1198 xenbus_dev_fatal(dev, ret, "starting transaction");
1199 goto error;
1200 }
1201 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
1202 if (ret)
1203 goto error_xenbus;
1204 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref);
1205 if (ret)
1206 goto error_xenbus;
1207 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
1208 evtchn);
1209 if (ret)
1210 goto error_xenbus;
1211 ret = xenbus_transaction_end(xbt, 0);
1212 if (ret) {
1213 if (ret == -EAGAIN)
1214 goto again;
1215 xenbus_dev_fatal(dev, ret, "completing transaction");
1216 goto error;
1217 }
1218 xenbus_switch_state(dev, XenbusStateInitialised);
1219
416efba0 1220 return 0;
21968190
SS
1221
1222 error_xenbus:
1223 xenbus_transaction_end(xbt, 1);
1224 xenbus_dev_fatal(dev, ret, "writing xenstore");
1225 error:
1226 pvcalls_front_remove(dev);
1227 return ret;
416efba0
SS
1228}
1229
1230static void pvcalls_front_changed(struct xenbus_device *dev,
1231 enum xenbus_state backend_state)
1232{
21968190
SS
1233 switch (backend_state) {
1234 case XenbusStateReconfiguring:
1235 case XenbusStateReconfigured:
1236 case XenbusStateInitialising:
1237 case XenbusStateInitialised:
1238 case XenbusStateUnknown:
1239 break;
1240
1241 case XenbusStateInitWait:
1242 break;
1243
1244 case XenbusStateConnected:
1245 xenbus_switch_state(dev, XenbusStateConnected);
1246 break;
1247
1248 case XenbusStateClosed:
1249 if (dev->state == XenbusStateClosed)
1250 break;
3d8765d4
GS
1251 /* Missed the backend's CLOSING state */
1252 /* fall through */
21968190
SS
1253 case XenbusStateClosing:
1254 xenbus_frontend_closed(dev);
1255 break;
1256 }
416efba0
SS
1257}
1258
1259static struct xenbus_driver pvcalls_front_driver = {
1260 .ids = pvcalls_front_ids,
1261 .probe = pvcalls_front_probe,
1262 .remove = pvcalls_front_remove,
1263 .otherend_changed = pvcalls_front_changed,
1264};
1265
1266static int __init pvcalls_frontend_init(void)
1267{
1268 if (!xen_domain())
1269 return -ENODEV;
1270
1271 pr_info("Initialising Xen pvcalls frontend driver\n");
1272
1273 return xenbus_register_frontend(&pvcalls_front_driver);
1274}
1275
1276module_init(pvcalls_frontend_init);
24e7f84d
BO
1277
1278MODULE_DESCRIPTION("Xen PV Calls frontend driver");
1279MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
1280MODULE_LICENSE("GPL");