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/inet.h>
16 #include <linux/kthread.h>
17 #include <linux/list.h>
18 #include <linux/radix-tree.h>
19 #include <linux/module.h>
20 #include <linux/semaphore.h>
21 #include <linux/wait.h>
22 #include <net/sock.h>
23 #include <net/inet_common.h>
24 #include <net/inet_connection_sock.h>
25 #include <net/request_sock.h>
26
27 #include <xen/events.h>
28 #include <xen/grant_table.h>
29 #include <xen/xen.h>
30 #include <xen/xenbus.h>
31 #include <xen/interface/io/pvcalls.h>
32
33 #define PVCALLS_VERSIONS "1"
34 #define MAX_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
35
36 struct pvcalls_back_global {
37 struct list_head frontends;
38 struct semaphore frontends_lock;
39 } pvcalls_back_global;
40
41 /*
42 * Per-frontend data structure. It contains pointers to the command
43 * ring, its event channel, a list of active sockets and a tree of
44 * passive sockets.
45 */
46 struct pvcalls_fedata {
47 struct list_head list;
48 struct xenbus_device *dev;
49 struct xen_pvcalls_sring *sring;
50 struct xen_pvcalls_back_ring ring;
51 int irq;
52 struct list_head socket_mappings;
53 struct radix_tree_root socketpass_mappings;
54 struct semaphore socket_lock;
55 };
56
57 struct pvcalls_ioworker {
58 struct work_struct register_work;
59 struct workqueue_struct *wq;
60 };
61
62 struct sock_mapping {
63 struct list_head list;
64 struct pvcalls_fedata *fedata;
65 struct sockpass_mapping *sockpass;
66 struct socket *sock;
67 uint64_t id;
68 grant_ref_t ref;
69 struct pvcalls_data_intf *ring;
70 void *bytes;
71 struct pvcalls_data data;
72 uint32_t ring_order;
73 int irq;
74 atomic_t read;
75 atomic_t write;
76 atomic_t io;
77 atomic_t release;
78 atomic_t eoi;
79 void (*saved_data_ready)(struct sock *sk);
80 struct pvcalls_ioworker ioworker;
81 };
82
83 struct sockpass_mapping {
84 struct list_head list;
85 struct pvcalls_fedata *fedata;
86 struct socket *sock;
87 uint64_t id;
88 struct xen_pvcalls_request reqcopy;
89 spinlock_t copy_lock;
90 struct workqueue_struct *wq;
91 struct work_struct register_work;
92 void (*saved_data_ready)(struct sock *sk);
93 };
94
95 static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map);
96 static int pvcalls_back_release_active(struct xenbus_device *dev,
97 struct pvcalls_fedata *fedata,
98 struct sock_mapping *map);
99
pvcalls_conn_back_read(void * opaque)100 static bool pvcalls_conn_back_read(void *opaque)
101 {
102 struct sock_mapping *map = (struct sock_mapping *)opaque;
103 struct msghdr msg;
104 struct kvec vec[2];
105 RING_IDX cons, prod, size, wanted, array_size, masked_prod, masked_cons;
106 int32_t error;
107 struct pvcalls_data_intf *intf = map->ring;
108 struct pvcalls_data *data = &map->data;
109 unsigned long flags;
110 int ret;
111
112 array_size = XEN_FLEX_RING_SIZE(map->ring_order);
113 cons = intf->in_cons;
114 prod = intf->in_prod;
115 error = intf->in_error;
116 /* read the indexes first, then deal with the data */
117 virt_mb();
118
119 if (error)
120 return false;
121
122 size = pvcalls_queued(prod, cons, array_size);
123 if (size >= array_size)
124 return false;
125 spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags);
126 if (skb_queue_empty(&map->sock->sk->sk_receive_queue)) {
127 atomic_set(&map->read, 0);
128 spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock,
129 flags);
130 return true;
131 }
132 spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags);
133 wanted = array_size - size;
134 masked_prod = pvcalls_mask(prod, array_size);
135 masked_cons = pvcalls_mask(cons, array_size);
136
137 memset(&msg, 0, sizeof(msg));
138 if (masked_prod < masked_cons) {
139 vec[0].iov_base = data->in + masked_prod;
140 vec[0].iov_len = wanted;
141 iov_iter_kvec(&msg.msg_iter, ITER_KVEC|WRITE, vec, 1, wanted);
142 } else {
143 vec[0].iov_base = data->in + masked_prod;
144 vec[0].iov_len = array_size - masked_prod;
145 vec[1].iov_base = data->in;
146 vec[1].iov_len = wanted - vec[0].iov_len;
147 iov_iter_kvec(&msg.msg_iter, ITER_KVEC|WRITE, vec, 2, wanted);
148 }
149
150 atomic_set(&map->read, 0);
151 ret = inet_recvmsg(map->sock, &msg, wanted, MSG_DONTWAIT);
152 WARN_ON(ret > wanted);
153 if (ret == -EAGAIN) /* shouldn't happen */
154 return true;
155 if (!ret)
156 ret = -ENOTCONN;
157 spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags);
158 if (ret > 0 && !skb_queue_empty(&map->sock->sk->sk_receive_queue))
159 atomic_inc(&map->read);
160 spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags);
161
162 /* write the data, then modify the indexes */
163 virt_wmb();
164 if (ret < 0) {
165 atomic_set(&map->read, 0);
166 intf->in_error = ret;
167 } else
168 intf->in_prod = prod + ret;
169 /* update the indexes, then notify the other end */
170 virt_wmb();
171 notify_remote_via_irq(map->irq);
172
173 return true;
174 }
175
pvcalls_conn_back_write(struct sock_mapping * map)176 static bool pvcalls_conn_back_write(struct sock_mapping *map)
177 {
178 struct pvcalls_data_intf *intf = map->ring;
179 struct pvcalls_data *data = &map->data;
180 struct msghdr msg;
181 struct kvec vec[2];
182 RING_IDX cons, prod, size, array_size;
183 int ret;
184
185 cons = intf->out_cons;
186 prod = intf->out_prod;
187 /* read the indexes before dealing with the data */
188 virt_mb();
189
190 array_size = XEN_FLEX_RING_SIZE(map->ring_order);
191 size = pvcalls_queued(prod, cons, array_size);
192 if (size == 0)
193 return false;
194
195 memset(&msg, 0, sizeof(msg));
196 msg.msg_flags |= MSG_DONTWAIT;
197 if (pvcalls_mask(prod, array_size) > pvcalls_mask(cons, array_size)) {
198 vec[0].iov_base = data->out + pvcalls_mask(cons, array_size);
199 vec[0].iov_len = size;
200 iov_iter_kvec(&msg.msg_iter, ITER_KVEC|READ, vec, 1, size);
201 } else {
202 vec[0].iov_base = data->out + pvcalls_mask(cons, array_size);
203 vec[0].iov_len = array_size - pvcalls_mask(cons, array_size);
204 vec[1].iov_base = data->out;
205 vec[1].iov_len = size - vec[0].iov_len;
206 iov_iter_kvec(&msg.msg_iter, ITER_KVEC|READ, vec, 2, size);
207 }
208
209 atomic_set(&map->write, 0);
210 ret = inet_sendmsg(map->sock, &msg, size);
211 if (ret == -EAGAIN) {
212 atomic_inc(&map->write);
213 atomic_inc(&map->io);
214 return true;
215 }
216
217 /* write the data, then update the indexes */
218 virt_wmb();
219 if (ret < 0) {
220 intf->out_error = ret;
221 } else {
222 intf->out_error = 0;
223 intf->out_cons = cons + ret;
224 prod = intf->out_prod;
225 }
226 /* update the indexes, then notify the other end */
227 virt_wmb();
228 if (prod != cons + ret) {
229 atomic_inc(&map->write);
230 atomic_inc(&map->io);
231 }
232 notify_remote_via_irq(map->irq);
233
234 return true;
235 }
236
pvcalls_back_ioworker(struct work_struct * work)237 static void pvcalls_back_ioworker(struct work_struct *work)
238 {
239 struct pvcalls_ioworker *ioworker = container_of(work,
240 struct pvcalls_ioworker, register_work);
241 struct sock_mapping *map = container_of(ioworker, struct sock_mapping,
242 ioworker);
243 unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
244
245 while (atomic_read(&map->io) > 0) {
246 if (atomic_read(&map->release) > 0) {
247 atomic_set(&map->release, 0);
248 return;
249 }
250
251 if (atomic_read(&map->read) > 0 &&
252 pvcalls_conn_back_read(map))
253 eoi_flags = 0;
254 if (atomic_read(&map->write) > 0 &&
255 pvcalls_conn_back_write(map))
256 eoi_flags = 0;
257
258 if (atomic_read(&map->eoi) > 0 && !atomic_read(&map->write)) {
259 atomic_set(&map->eoi, 0);
260 xen_irq_lateeoi(map->irq, eoi_flags);
261 eoi_flags = XEN_EOI_FLAG_SPURIOUS;
262 }
263
264 atomic_dec(&map->io);
265 }
266 }
267
pvcalls_back_socket(struct xenbus_device * dev,struct xen_pvcalls_request * req)268 static int pvcalls_back_socket(struct xenbus_device *dev,
269 struct xen_pvcalls_request *req)
270 {
271 struct pvcalls_fedata *fedata;
272 int ret;
273 struct xen_pvcalls_response *rsp;
274
275 fedata = dev_get_drvdata(&dev->dev);
276
277 if (req->u.socket.domain != AF_INET ||
278 req->u.socket.type != SOCK_STREAM ||
279 (req->u.socket.protocol != IPPROTO_IP &&
280 req->u.socket.protocol != AF_INET))
281 ret = -EAFNOSUPPORT;
282 else
283 ret = 0;
284
285 /* leave the actual socket allocation for later */
286
287 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
288 rsp->req_id = req->req_id;
289 rsp->cmd = req->cmd;
290 rsp->u.socket.id = req->u.socket.id;
291 rsp->ret = ret;
292
293 return 0;
294 }
295
pvcalls_sk_state_change(struct sock * sock)296 static void pvcalls_sk_state_change(struct sock *sock)
297 {
298 struct sock_mapping *map = sock->sk_user_data;
299
300 if (map == NULL)
301 return;
302
303 atomic_inc(&map->read);
304 notify_remote_via_irq(map->irq);
305 }
306
pvcalls_sk_data_ready(struct sock * sock)307 static void pvcalls_sk_data_ready(struct sock *sock)
308 {
309 struct sock_mapping *map = sock->sk_user_data;
310 struct pvcalls_ioworker *iow;
311
312 if (map == NULL)
313 return;
314
315 iow = &map->ioworker;
316 atomic_inc(&map->read);
317 atomic_inc(&map->io);
318 queue_work(iow->wq, &iow->register_work);
319 }
320
pvcalls_new_active_socket(struct pvcalls_fedata * fedata,uint64_t id,grant_ref_t ref,uint32_t evtchn,struct socket * sock)321 static struct sock_mapping *pvcalls_new_active_socket(
322 struct pvcalls_fedata *fedata,
323 uint64_t id,
324 grant_ref_t ref,
325 uint32_t evtchn,
326 struct socket *sock)
327 {
328 int ret;
329 struct sock_mapping *map;
330 void *page;
331
332 map = kzalloc(sizeof(*map), GFP_KERNEL);
333 if (map == NULL) {
334 sock_release(sock);
335 return NULL;
336 }
337
338 map->fedata = fedata;
339 map->sock = sock;
340 map->id = id;
341 map->ref = ref;
342
343 ret = xenbus_map_ring_valloc(fedata->dev, &ref, 1, &page);
344 if (ret < 0)
345 goto out;
346 map->ring = page;
347 map->ring_order = map->ring->ring_order;
348 /* first read the order, then map the data ring */
349 virt_rmb();
350 if (map->ring_order > MAX_RING_ORDER) {
351 pr_warn("%s frontend requested ring_order %u, which is > MAX (%u)\n",
352 __func__, map->ring_order, MAX_RING_ORDER);
353 goto out;
354 }
355 ret = xenbus_map_ring_valloc(fedata->dev, map->ring->ref,
356 (1 << map->ring_order), &page);
357 if (ret < 0)
358 goto out;
359 map->bytes = page;
360
361 ret = bind_interdomain_evtchn_to_irqhandler_lateeoi(
362 fedata->dev->otherend_id, evtchn,
363 pvcalls_back_conn_event, 0, "pvcalls-backend", map);
364 if (ret < 0)
365 goto out;
366 map->irq = ret;
367
368 map->data.in = map->bytes;
369 map->data.out = map->bytes + XEN_FLEX_RING_SIZE(map->ring_order);
370
371 map->ioworker.wq = alloc_workqueue("pvcalls_io", WQ_UNBOUND, 1);
372 if (!map->ioworker.wq)
373 goto out;
374 atomic_set(&map->io, 1);
375 INIT_WORK(&map->ioworker.register_work, pvcalls_back_ioworker);
376
377 down(&fedata->socket_lock);
378 list_add_tail(&map->list, &fedata->socket_mappings);
379 up(&fedata->socket_lock);
380
381 write_lock_bh(&map->sock->sk->sk_callback_lock);
382 map->saved_data_ready = map->sock->sk->sk_data_ready;
383 map->sock->sk->sk_user_data = map;
384 map->sock->sk->sk_data_ready = pvcalls_sk_data_ready;
385 map->sock->sk->sk_state_change = pvcalls_sk_state_change;
386 write_unlock_bh(&map->sock->sk->sk_callback_lock);
387
388 return map;
389 out:
390 down(&fedata->socket_lock);
391 list_del(&map->list);
392 pvcalls_back_release_active(fedata->dev, fedata, map);
393 up(&fedata->socket_lock);
394 return NULL;
395 }
396
pvcalls_back_connect(struct xenbus_device * dev,struct xen_pvcalls_request * req)397 static int pvcalls_back_connect(struct xenbus_device *dev,
398 struct xen_pvcalls_request *req)
399 {
400 struct pvcalls_fedata *fedata;
401 int ret = -EINVAL;
402 struct socket *sock;
403 struct sock_mapping *map;
404 struct xen_pvcalls_response *rsp;
405 struct sockaddr *sa = (struct sockaddr *)&req->u.connect.addr;
406
407 fedata = dev_get_drvdata(&dev->dev);
408
409 if (req->u.connect.len < sizeof(sa->sa_family) ||
410 req->u.connect.len > sizeof(req->u.connect.addr) ||
411 sa->sa_family != AF_INET)
412 goto out;
413
414 ret = sock_create(AF_INET, SOCK_STREAM, 0, &sock);
415 if (ret < 0)
416 goto out;
417 ret = inet_stream_connect(sock, sa, req->u.connect.len, 0);
418 if (ret < 0) {
419 sock_release(sock);
420 goto out;
421 }
422
423 map = pvcalls_new_active_socket(fedata,
424 req->u.connect.id,
425 req->u.connect.ref,
426 req->u.connect.evtchn,
427 sock);
428 if (!map)
429 ret = -EFAULT;
430
431 out:
432 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
433 rsp->req_id = req->req_id;
434 rsp->cmd = req->cmd;
435 rsp->u.connect.id = req->u.connect.id;
436 rsp->ret = ret;
437
438 return 0;
439 }
440
pvcalls_back_release_active(struct xenbus_device * dev,struct pvcalls_fedata * fedata,struct sock_mapping * map)441 static int pvcalls_back_release_active(struct xenbus_device *dev,
442 struct pvcalls_fedata *fedata,
443 struct sock_mapping *map)
444 {
445 disable_irq(map->irq);
446 if (map->sock->sk != NULL) {
447 write_lock_bh(&map->sock->sk->sk_callback_lock);
448 map->sock->sk->sk_user_data = NULL;
449 map->sock->sk->sk_data_ready = map->saved_data_ready;
450 write_unlock_bh(&map->sock->sk->sk_callback_lock);
451 }
452
453 atomic_set(&map->release, 1);
454 flush_work(&map->ioworker.register_work);
455
456 xenbus_unmap_ring_vfree(dev, map->bytes);
457 xenbus_unmap_ring_vfree(dev, (void *)map->ring);
458 unbind_from_irqhandler(map->irq, map);
459
460 sock_release(map->sock);
461 kfree(map);
462
463 return 0;
464 }
465
pvcalls_back_release_passive(struct xenbus_device * dev,struct pvcalls_fedata * fedata,struct sockpass_mapping * mappass)466 static int pvcalls_back_release_passive(struct xenbus_device *dev,
467 struct pvcalls_fedata *fedata,
468 struct sockpass_mapping *mappass)
469 {
470 if (mappass->sock->sk != NULL) {
471 write_lock_bh(&mappass->sock->sk->sk_callback_lock);
472 mappass->sock->sk->sk_user_data = NULL;
473 mappass->sock->sk->sk_data_ready = mappass->saved_data_ready;
474 write_unlock_bh(&mappass->sock->sk->sk_callback_lock);
475 }
476 sock_release(mappass->sock);
477 flush_workqueue(mappass->wq);
478 destroy_workqueue(mappass->wq);
479 kfree(mappass);
480
481 return 0;
482 }
483
pvcalls_back_release(struct xenbus_device * dev,struct xen_pvcalls_request * req)484 static int pvcalls_back_release(struct xenbus_device *dev,
485 struct xen_pvcalls_request *req)
486 {
487 struct pvcalls_fedata *fedata;
488 struct sock_mapping *map, *n;
489 struct sockpass_mapping *mappass;
490 int ret = 0;
491 struct xen_pvcalls_response *rsp;
492
493 fedata = dev_get_drvdata(&dev->dev);
494
495 down(&fedata->socket_lock);
496 list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) {
497 if (map->id == req->u.release.id) {
498 list_del(&map->list);
499 up(&fedata->socket_lock);
500 ret = pvcalls_back_release_active(dev, fedata, map);
501 goto out;
502 }
503 }
504 mappass = radix_tree_lookup(&fedata->socketpass_mappings,
505 req->u.release.id);
506 if (mappass != NULL) {
507 radix_tree_delete(&fedata->socketpass_mappings, mappass->id);
508 up(&fedata->socket_lock);
509 ret = pvcalls_back_release_passive(dev, fedata, mappass);
510 } else
511 up(&fedata->socket_lock);
512
513 out:
514 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
515 rsp->req_id = req->req_id;
516 rsp->u.release.id = req->u.release.id;
517 rsp->cmd = req->cmd;
518 rsp->ret = ret;
519 return 0;
520 }
521
__pvcalls_back_accept(struct work_struct * work)522 static void __pvcalls_back_accept(struct work_struct *work)
523 {
524 struct sockpass_mapping *mappass = container_of(
525 work, struct sockpass_mapping, register_work);
526 struct sock_mapping *map;
527 struct pvcalls_ioworker *iow;
528 struct pvcalls_fedata *fedata;
529 struct socket *sock;
530 struct xen_pvcalls_response *rsp;
531 struct xen_pvcalls_request *req;
532 int notify;
533 int ret = -EINVAL;
534 unsigned long flags;
535
536 fedata = mappass->fedata;
537 /*
538 * __pvcalls_back_accept can race against pvcalls_back_accept.
539 * We only need to check the value of "cmd" on read. It could be
540 * done atomically, but to simplify the code on the write side, we
541 * use a spinlock.
542 */
543 spin_lock_irqsave(&mappass->copy_lock, flags);
544 req = &mappass->reqcopy;
545 if (req->cmd != PVCALLS_ACCEPT) {
546 spin_unlock_irqrestore(&mappass->copy_lock, flags);
547 return;
548 }
549 spin_unlock_irqrestore(&mappass->copy_lock, flags);
550
551 sock = sock_alloc();
552 if (sock == NULL)
553 goto out_error;
554 sock->type = mappass->sock->type;
555 sock->ops = mappass->sock->ops;
556
557 ret = inet_accept(mappass->sock, sock, O_NONBLOCK, true);
558 if (ret == -EAGAIN) {
559 sock_release(sock);
560 return;
561 }
562
563 map = pvcalls_new_active_socket(fedata,
564 req->u.accept.id_new,
565 req->u.accept.ref,
566 req->u.accept.evtchn,
567 sock);
568 if (!map) {
569 ret = -EFAULT;
570 goto out_error;
571 }
572
573 map->sockpass = mappass;
574 iow = &map->ioworker;
575 atomic_inc(&map->read);
576 atomic_inc(&map->io);
577 queue_work(iow->wq, &iow->register_work);
578
579 out_error:
580 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
581 rsp->req_id = req->req_id;
582 rsp->cmd = req->cmd;
583 rsp->u.accept.id = req->u.accept.id;
584 rsp->ret = ret;
585 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify);
586 if (notify)
587 notify_remote_via_irq(fedata->irq);
588
589 mappass->reqcopy.cmd = 0;
590 }
591
pvcalls_pass_sk_data_ready(struct sock * sock)592 static void pvcalls_pass_sk_data_ready(struct sock *sock)
593 {
594 struct sockpass_mapping *mappass = sock->sk_user_data;
595 struct pvcalls_fedata *fedata;
596 struct xen_pvcalls_response *rsp;
597 unsigned long flags;
598 int notify;
599
600 if (mappass == NULL)
601 return;
602
603 fedata = mappass->fedata;
604 spin_lock_irqsave(&mappass->copy_lock, flags);
605 if (mappass->reqcopy.cmd == PVCALLS_POLL) {
606 rsp = RING_GET_RESPONSE(&fedata->ring,
607 fedata->ring.rsp_prod_pvt++);
608 rsp->req_id = mappass->reqcopy.req_id;
609 rsp->u.poll.id = mappass->reqcopy.u.poll.id;
610 rsp->cmd = mappass->reqcopy.cmd;
611 rsp->ret = 0;
612
613 mappass->reqcopy.cmd = 0;
614 spin_unlock_irqrestore(&mappass->copy_lock, flags);
615
616 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify);
617 if (notify)
618 notify_remote_via_irq(mappass->fedata->irq);
619 } else {
620 spin_unlock_irqrestore(&mappass->copy_lock, flags);
621 queue_work(mappass->wq, &mappass->register_work);
622 }
623 }
624
pvcalls_back_bind(struct xenbus_device * dev,struct xen_pvcalls_request * req)625 static int pvcalls_back_bind(struct xenbus_device *dev,
626 struct xen_pvcalls_request *req)
627 {
628 struct pvcalls_fedata *fedata;
629 int ret;
630 struct sockpass_mapping *map;
631 struct xen_pvcalls_response *rsp;
632
633 fedata = dev_get_drvdata(&dev->dev);
634
635 map = kzalloc(sizeof(*map), GFP_KERNEL);
636 if (map == NULL) {
637 ret = -ENOMEM;
638 goto out;
639 }
640
641 INIT_WORK(&map->register_work, __pvcalls_back_accept);
642 spin_lock_init(&map->copy_lock);
643 map->wq = alloc_workqueue("pvcalls_wq", WQ_UNBOUND, 1);
644 if (!map->wq) {
645 ret = -ENOMEM;
646 goto out;
647 }
648
649 ret = sock_create(AF_INET, SOCK_STREAM, 0, &map->sock);
650 if (ret < 0)
651 goto out;
652
653 ret = inet_bind(map->sock, (struct sockaddr *)&req->u.bind.addr,
654 req->u.bind.len);
655 if (ret < 0)
656 goto out;
657
658 map->fedata = fedata;
659 map->id = req->u.bind.id;
660
661 down(&fedata->socket_lock);
662 ret = radix_tree_insert(&fedata->socketpass_mappings, map->id,
663 map);
664 up(&fedata->socket_lock);
665 if (ret)
666 goto out;
667
668 write_lock_bh(&map->sock->sk->sk_callback_lock);
669 map->saved_data_ready = map->sock->sk->sk_data_ready;
670 map->sock->sk->sk_user_data = map;
671 map->sock->sk->sk_data_ready = pvcalls_pass_sk_data_ready;
672 write_unlock_bh(&map->sock->sk->sk_callback_lock);
673
674 out:
675 if (ret) {
676 if (map && map->sock)
677 sock_release(map->sock);
678 if (map && map->wq)
679 destroy_workqueue(map->wq);
680 kfree(map);
681 }
682 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
683 rsp->req_id = req->req_id;
684 rsp->cmd = req->cmd;
685 rsp->u.bind.id = req->u.bind.id;
686 rsp->ret = ret;
687 return 0;
688 }
689
pvcalls_back_listen(struct xenbus_device * dev,struct xen_pvcalls_request * req)690 static int pvcalls_back_listen(struct xenbus_device *dev,
691 struct xen_pvcalls_request *req)
692 {
693 struct pvcalls_fedata *fedata;
694 int ret = -EINVAL;
695 struct sockpass_mapping *map;
696 struct xen_pvcalls_response *rsp;
697
698 fedata = dev_get_drvdata(&dev->dev);
699
700 down(&fedata->socket_lock);
701 map = radix_tree_lookup(&fedata->socketpass_mappings, req->u.listen.id);
702 up(&fedata->socket_lock);
703 if (map == NULL)
704 goto out;
705
706 ret = inet_listen(map->sock, req->u.listen.backlog);
707
708 out:
709 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
710 rsp->req_id = req->req_id;
711 rsp->cmd = req->cmd;
712 rsp->u.listen.id = req->u.listen.id;
713 rsp->ret = ret;
714 return 0;
715 }
716
pvcalls_back_accept(struct xenbus_device * dev,struct xen_pvcalls_request * req)717 static int pvcalls_back_accept(struct xenbus_device *dev,
718 struct xen_pvcalls_request *req)
719 {
720 struct pvcalls_fedata *fedata;
721 struct sockpass_mapping *mappass;
722 int ret = -EINVAL;
723 struct xen_pvcalls_response *rsp;
724 unsigned long flags;
725
726 fedata = dev_get_drvdata(&dev->dev);
727
728 down(&fedata->socket_lock);
729 mappass = radix_tree_lookup(&fedata->socketpass_mappings,
730 req->u.accept.id);
731 up(&fedata->socket_lock);
732 if (mappass == NULL)
733 goto out_error;
734
735 /*
736 * Limitation of the current implementation: only support one
737 * concurrent accept or poll call on one socket.
738 */
739 spin_lock_irqsave(&mappass->copy_lock, flags);
740 if (mappass->reqcopy.cmd != 0) {
741 spin_unlock_irqrestore(&mappass->copy_lock, flags);
742 ret = -EINTR;
743 goto out_error;
744 }
745
746 mappass->reqcopy = *req;
747 spin_unlock_irqrestore(&mappass->copy_lock, flags);
748 queue_work(mappass->wq, &mappass->register_work);
749
750 /* Tell the caller we don't need to send back a notification yet */
751 return -1;
752
753 out_error:
754 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
755 rsp->req_id = req->req_id;
756 rsp->cmd = req->cmd;
757 rsp->u.accept.id = req->u.accept.id;
758 rsp->ret = ret;
759 return 0;
760 }
761
pvcalls_back_poll(struct xenbus_device * dev,struct xen_pvcalls_request * req)762 static int pvcalls_back_poll(struct xenbus_device *dev,
763 struct xen_pvcalls_request *req)
764 {
765 struct pvcalls_fedata *fedata;
766 struct sockpass_mapping *mappass;
767 struct xen_pvcalls_response *rsp;
768 struct inet_connection_sock *icsk;
769 struct request_sock_queue *queue;
770 unsigned long flags;
771 int ret;
772 bool data;
773
774 fedata = dev_get_drvdata(&dev->dev);
775
776 down(&fedata->socket_lock);
777 mappass = radix_tree_lookup(&fedata->socketpass_mappings,
778 req->u.poll.id);
779 up(&fedata->socket_lock);
780 if (mappass == NULL)
781 return -EINVAL;
782
783 /*
784 * Limitation of the current implementation: only support one
785 * concurrent accept or poll call on one socket.
786 */
787 spin_lock_irqsave(&mappass->copy_lock, flags);
788 if (mappass->reqcopy.cmd != 0) {
789 ret = -EINTR;
790 goto out;
791 }
792
793 mappass->reqcopy = *req;
794 icsk = inet_csk(mappass->sock->sk);
795 queue = &icsk->icsk_accept_queue;
796 data = READ_ONCE(queue->rskq_accept_head) != NULL;
797 if (data) {
798 mappass->reqcopy.cmd = 0;
799 ret = 0;
800 goto out;
801 }
802 spin_unlock_irqrestore(&mappass->copy_lock, flags);
803
804 /* Tell the caller we don't need to send back a notification yet */
805 return -1;
806
807 out:
808 spin_unlock_irqrestore(&mappass->copy_lock, flags);
809
810 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
811 rsp->req_id = req->req_id;
812 rsp->cmd = req->cmd;
813 rsp->u.poll.id = req->u.poll.id;
814 rsp->ret = ret;
815 return 0;
816 }
817
pvcalls_back_handle_cmd(struct xenbus_device * dev,struct xen_pvcalls_request * req)818 static int pvcalls_back_handle_cmd(struct xenbus_device *dev,
819 struct xen_pvcalls_request *req)
820 {
821 int ret = 0;
822
823 switch (req->cmd) {
824 case PVCALLS_SOCKET:
825 ret = pvcalls_back_socket(dev, req);
826 break;
827 case PVCALLS_CONNECT:
828 ret = pvcalls_back_connect(dev, req);
829 break;
830 case PVCALLS_RELEASE:
831 ret = pvcalls_back_release(dev, req);
832 break;
833 case PVCALLS_BIND:
834 ret = pvcalls_back_bind(dev, req);
835 break;
836 case PVCALLS_LISTEN:
837 ret = pvcalls_back_listen(dev, req);
838 break;
839 case PVCALLS_ACCEPT:
840 ret = pvcalls_back_accept(dev, req);
841 break;
842 case PVCALLS_POLL:
843 ret = pvcalls_back_poll(dev, req);
844 break;
845 default:
846 {
847 struct pvcalls_fedata *fedata;
848 struct xen_pvcalls_response *rsp;
849
850 fedata = dev_get_drvdata(&dev->dev);
851 rsp = RING_GET_RESPONSE(
852 &fedata->ring, fedata->ring.rsp_prod_pvt++);
853 rsp->req_id = req->req_id;
854 rsp->cmd = req->cmd;
855 rsp->ret = -ENOTSUPP;
856 break;
857 }
858 }
859 return ret;
860 }
861
pvcalls_back_work(struct pvcalls_fedata * fedata)862 static void pvcalls_back_work(struct pvcalls_fedata *fedata)
863 {
864 int notify, notify_all = 0, more = 1;
865 struct xen_pvcalls_request req;
866 struct xenbus_device *dev = fedata->dev;
867
868 while (more) {
869 while (RING_HAS_UNCONSUMED_REQUESTS(&fedata->ring)) {
870 RING_COPY_REQUEST(&fedata->ring,
871 fedata->ring.req_cons++,
872 &req);
873
874 if (!pvcalls_back_handle_cmd(dev, &req)) {
875 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(
876 &fedata->ring, notify);
877 notify_all += notify;
878 }
879 }
880
881 if (notify_all) {
882 notify_remote_via_irq(fedata->irq);
883 notify_all = 0;
884 }
885
886 RING_FINAL_CHECK_FOR_REQUESTS(&fedata->ring, more);
887 }
888 }
889
pvcalls_back_event(int irq,void * dev_id)890 static irqreturn_t pvcalls_back_event(int irq, void *dev_id)
891 {
892 struct xenbus_device *dev = dev_id;
893 struct pvcalls_fedata *fedata = NULL;
894 unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
895
896 if (dev) {
897 fedata = dev_get_drvdata(&dev->dev);
898 if (fedata) {
899 pvcalls_back_work(fedata);
900 eoi_flags = 0;
901 }
902 }
903
904 xen_irq_lateeoi(irq, eoi_flags);
905
906 return IRQ_HANDLED;
907 }
908
pvcalls_back_conn_event(int irq,void * sock_map)909 static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map)
910 {
911 struct sock_mapping *map = sock_map;
912 struct pvcalls_ioworker *iow;
913
914 if (map == NULL || map->sock == NULL || map->sock->sk == NULL ||
915 map->sock->sk->sk_user_data != map) {
916 xen_irq_lateeoi(irq, 0);
917 return IRQ_HANDLED;
918 }
919
920 iow = &map->ioworker;
921
922 atomic_inc(&map->write);
923 atomic_inc(&map->eoi);
924 atomic_inc(&map->io);
925 queue_work(iow->wq, &iow->register_work);
926
927 return IRQ_HANDLED;
928 }
929
backend_connect(struct xenbus_device * dev)930 static int backend_connect(struct xenbus_device *dev)
931 {
932 int err, evtchn;
933 grant_ref_t ring_ref;
934 struct pvcalls_fedata *fedata = NULL;
935
936 fedata = kzalloc(sizeof(struct pvcalls_fedata), GFP_KERNEL);
937 if (!fedata)
938 return -ENOMEM;
939
940 fedata->irq = -1;
941 err = xenbus_scanf(XBT_NIL, dev->otherend, "port", "%u",
942 &evtchn);
943 if (err != 1) {
944 err = -EINVAL;
945 xenbus_dev_fatal(dev, err, "reading %s/event-channel",
946 dev->otherend);
947 goto error;
948 }
949
950 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-ref", "%u", &ring_ref);
951 if (err != 1) {
952 err = -EINVAL;
953 xenbus_dev_fatal(dev, err, "reading %s/ring-ref",
954 dev->otherend);
955 goto error;
956 }
957
958 err = bind_interdomain_evtchn_to_irq_lateeoi(dev->otherend_id, evtchn);
959 if (err < 0)
960 goto error;
961 fedata->irq = err;
962
963 err = request_threaded_irq(fedata->irq, NULL, pvcalls_back_event,
964 IRQF_ONESHOT, "pvcalls-back", dev);
965 if (err < 0)
966 goto error;
967
968 err = xenbus_map_ring_valloc(dev, &ring_ref, 1,
969 (void **)&fedata->sring);
970 if (err < 0)
971 goto error;
972
973 BACK_RING_INIT(&fedata->ring, fedata->sring, XEN_PAGE_SIZE * 1);
974 fedata->dev = dev;
975
976 INIT_LIST_HEAD(&fedata->socket_mappings);
977 INIT_RADIX_TREE(&fedata->socketpass_mappings, GFP_KERNEL);
978 sema_init(&fedata->socket_lock, 1);
979 dev_set_drvdata(&dev->dev, fedata);
980
981 down(&pvcalls_back_global.frontends_lock);
982 list_add_tail(&fedata->list, &pvcalls_back_global.frontends);
983 up(&pvcalls_back_global.frontends_lock);
984
985 return 0;
986
987 error:
988 if (fedata->irq >= 0)
989 unbind_from_irqhandler(fedata->irq, dev);
990 if (fedata->sring != NULL)
991 xenbus_unmap_ring_vfree(dev, fedata->sring);
992 kfree(fedata);
993 return err;
994 }
995
backend_disconnect(struct xenbus_device * dev)996 static int backend_disconnect(struct xenbus_device *dev)
997 {
998 struct pvcalls_fedata *fedata;
999 struct sock_mapping *map, *n;
1000 struct sockpass_mapping *mappass;
1001 struct radix_tree_iter iter;
1002 void **slot;
1003
1004
1005 fedata = dev_get_drvdata(&dev->dev);
1006
1007 down(&fedata->socket_lock);
1008 list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) {
1009 list_del(&map->list);
1010 pvcalls_back_release_active(dev, fedata, map);
1011 }
1012
1013 radix_tree_for_each_slot(slot, &fedata->socketpass_mappings, &iter, 0) {
1014 mappass = radix_tree_deref_slot(slot);
1015 if (!mappass)
1016 continue;
1017 if (radix_tree_exception(mappass)) {
1018 if (radix_tree_deref_retry(mappass))
1019 slot = radix_tree_iter_retry(&iter);
1020 } else {
1021 radix_tree_delete(&fedata->socketpass_mappings,
1022 mappass->id);
1023 pvcalls_back_release_passive(dev, fedata, mappass);
1024 }
1025 }
1026 up(&fedata->socket_lock);
1027
1028 unbind_from_irqhandler(fedata->irq, dev);
1029 xenbus_unmap_ring_vfree(dev, fedata->sring);
1030
1031 list_del(&fedata->list);
1032 kfree(fedata);
1033 dev_set_drvdata(&dev->dev, NULL);
1034
1035 return 0;
1036 }
1037
pvcalls_back_probe(struct xenbus_device * dev,const struct xenbus_device_id * id)1038 static int pvcalls_back_probe(struct xenbus_device *dev,
1039 const struct xenbus_device_id *id)
1040 {
1041 int err, abort;
1042 struct xenbus_transaction xbt;
1043
1044 again:
1045 abort = 1;
1046
1047 err = xenbus_transaction_start(&xbt);
1048 if (err) {
1049 pr_warn("%s cannot create xenstore transaction\n", __func__);
1050 return err;
1051 }
1052
1053 err = xenbus_printf(xbt, dev->nodename, "versions", "%s",
1054 PVCALLS_VERSIONS);
1055 if (err) {
1056 pr_warn("%s write out 'versions' failed\n", __func__);
1057 goto abort;
1058 }
1059
1060 err = xenbus_printf(xbt, dev->nodename, "max-page-order", "%u",
1061 MAX_RING_ORDER);
1062 if (err) {
1063 pr_warn("%s write out 'max-page-order' failed\n", __func__);
1064 goto abort;
1065 }
1066
1067 err = xenbus_printf(xbt, dev->nodename, "function-calls",
1068 XENBUS_FUNCTIONS_CALLS);
1069 if (err) {
1070 pr_warn("%s write out 'function-calls' failed\n", __func__);
1071 goto abort;
1072 }
1073
1074 abort = 0;
1075 abort:
1076 err = xenbus_transaction_end(xbt, abort);
1077 if (err) {
1078 if (err == -EAGAIN && !abort)
1079 goto again;
1080 pr_warn("%s cannot complete xenstore transaction\n", __func__);
1081 return err;
1082 }
1083
1084 if (abort)
1085 return -EFAULT;
1086
1087 xenbus_switch_state(dev, XenbusStateInitWait);
1088
1089 return 0;
1090 }
1091
set_backend_state(struct xenbus_device * dev,enum xenbus_state state)1092 static void set_backend_state(struct xenbus_device *dev,
1093 enum xenbus_state state)
1094 {
1095 while (dev->state != state) {
1096 switch (dev->state) {
1097 case XenbusStateClosed:
1098 switch (state) {
1099 case XenbusStateInitWait:
1100 case XenbusStateConnected:
1101 xenbus_switch_state(dev, XenbusStateInitWait);
1102 break;
1103 case XenbusStateClosing:
1104 xenbus_switch_state(dev, XenbusStateClosing);
1105 break;
1106 default:
1107 WARN_ON(1);
1108 }
1109 break;
1110 case XenbusStateInitWait:
1111 case XenbusStateInitialised:
1112 switch (state) {
1113 case XenbusStateConnected:
1114 if (backend_connect(dev))
1115 return;
1116 xenbus_switch_state(dev, XenbusStateConnected);
1117 break;
1118 case XenbusStateClosing:
1119 case XenbusStateClosed:
1120 xenbus_switch_state(dev, XenbusStateClosing);
1121 break;
1122 default:
1123 WARN_ON(1);
1124 }
1125 break;
1126 case XenbusStateConnected:
1127 switch (state) {
1128 case XenbusStateInitWait:
1129 case XenbusStateClosing:
1130 case XenbusStateClosed:
1131 down(&pvcalls_back_global.frontends_lock);
1132 backend_disconnect(dev);
1133 up(&pvcalls_back_global.frontends_lock);
1134 xenbus_switch_state(dev, XenbusStateClosing);
1135 break;
1136 default:
1137 WARN_ON(1);
1138 }
1139 break;
1140 case XenbusStateClosing:
1141 switch (state) {
1142 case XenbusStateInitWait:
1143 case XenbusStateConnected:
1144 case XenbusStateClosed:
1145 xenbus_switch_state(dev, XenbusStateClosed);
1146 break;
1147 default:
1148 WARN_ON(1);
1149 }
1150 break;
1151 default:
1152 WARN_ON(1);
1153 }
1154 }
1155 }
1156
pvcalls_back_changed(struct xenbus_device * dev,enum xenbus_state frontend_state)1157 static void pvcalls_back_changed(struct xenbus_device *dev,
1158 enum xenbus_state frontend_state)
1159 {
1160 switch (frontend_state) {
1161 case XenbusStateInitialising:
1162 set_backend_state(dev, XenbusStateInitWait);
1163 break;
1164
1165 case XenbusStateInitialised:
1166 case XenbusStateConnected:
1167 set_backend_state(dev, XenbusStateConnected);
1168 break;
1169
1170 case XenbusStateClosing:
1171 set_backend_state(dev, XenbusStateClosing);
1172 break;
1173
1174 case XenbusStateClosed:
1175 set_backend_state(dev, XenbusStateClosed);
1176 if (xenbus_dev_is_online(dev))
1177 break;
1178 device_unregister(&dev->dev);
1179 break;
1180 case XenbusStateUnknown:
1181 set_backend_state(dev, XenbusStateClosed);
1182 device_unregister(&dev->dev);
1183 break;
1184
1185 default:
1186 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1187 frontend_state);
1188 break;
1189 }
1190 }
1191
pvcalls_back_remove(struct xenbus_device * dev)1192 static int pvcalls_back_remove(struct xenbus_device *dev)
1193 {
1194 return 0;
1195 }
1196
pvcalls_back_uevent(struct xenbus_device * xdev,struct kobj_uevent_env * env)1197 static int pvcalls_back_uevent(struct xenbus_device *xdev,
1198 struct kobj_uevent_env *env)
1199 {
1200 return 0;
1201 }
1202
1203 static const struct xenbus_device_id pvcalls_back_ids[] = {
1204 { "pvcalls" },
1205 { "" }
1206 };
1207
1208 static struct xenbus_driver pvcalls_back_driver = {
1209 .ids = pvcalls_back_ids,
1210 .probe = pvcalls_back_probe,
1211 .remove = pvcalls_back_remove,
1212 .uevent = pvcalls_back_uevent,
1213 .otherend_changed = pvcalls_back_changed,
1214 };
1215
pvcalls_back_init(void)1216 static int __init pvcalls_back_init(void)
1217 {
1218 int ret;
1219
1220 if (!xen_domain())
1221 return -ENODEV;
1222
1223 ret = xenbus_register_backend(&pvcalls_back_driver);
1224 if (ret < 0)
1225 return ret;
1226
1227 sema_init(&pvcalls_back_global.frontends_lock, 1);
1228 INIT_LIST_HEAD(&pvcalls_back_global.frontends);
1229 return 0;
1230 }
1231 module_init(pvcalls_back_init);
1232
pvcalls_back_fin(void)1233 static void __exit pvcalls_back_fin(void)
1234 {
1235 struct pvcalls_fedata *fedata, *nfedata;
1236
1237 down(&pvcalls_back_global.frontends_lock);
1238 list_for_each_entry_safe(fedata, nfedata,
1239 &pvcalls_back_global.frontends, list) {
1240 backend_disconnect(fedata->dev);
1241 }
1242 up(&pvcalls_back_global.frontends_lock);
1243
1244 xenbus_unregister_driver(&pvcalls_back_driver);
1245 }
1246
1247 module_exit(pvcalls_back_fin);
1248
1249 MODULE_DESCRIPTION("Xen PV Calls backend driver");
1250 MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
1251 MODULE_LICENSE("GPL");
1252