]>
Commit | Line | Data |
---|---|---|
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 |
34 | static struct proto pvcalls_proto = { |
35 | .name = "PVCalls", | |
36 | .owner = THIS_MODULE, | |
37 | .obj_size = sizeof(struct sock), | |
38 | }; | |
39 | ||
aa7ba376 SS |
40 | struct 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. */ | |
52 | static struct xenbus_device *pvcalls_front_dev; | |
53 | static 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 | ||
65 | struct 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 |
102 | static 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 |
111 | static 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 |
128 | static 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 |
142 | static 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 | ||
160 | again: | |
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 | ||
205 | static 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 |
225 | static 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 |
237 | int 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 |
312 | static 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 | ||
359 | out_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 | ||
367 | int 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 |
436 | static 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 | } | |
477 | out: | |
478 | /* write to ring before updating pointer */ | |
479 | virt_wmb(); | |
480 | intf->out_prod += len; | |
481 | ||
482 | return len; | |
483 | } | |
484 | ||
485 | int pvcalls_front_sendmsg(struct socket *sock, struct msghdr *msg, | |
486 | size_t len) | |
487 | { | |
488 | struct pvcalls_bedata *bedata; | |
489 | struct sock_mapping *map; | |
490 | int sent, tot_sent = 0; | |
491 | int count = 0, flags; | |
492 | ||
493 | flags = msg->msg_flags; | |
494 | if (flags & (MSG_CONFIRM|MSG_DONTROUTE|MSG_EOR|MSG_OOB)) | |
495 | return -EOPNOTSUPP; | |
496 | ||
497 | pvcalls_enter(); | |
498 | if (!pvcalls_front_dev) { | |
499 | pvcalls_exit(); | |
500 | return -ENOTCONN; | |
501 | } | |
502 | bedata = dev_get_drvdata(&pvcalls_front_dev->dev); | |
503 | ||
504 | map = (struct sock_mapping *) sock->sk->sk_send_head; | |
505 | if (!map) { | |
506 | pvcalls_exit(); | |
507 | return -ENOTSOCK; | |
508 | } | |
509 | ||
510 | mutex_lock(&map->active.out_mutex); | |
511 | if ((flags & MSG_DONTWAIT) && !pvcalls_front_write_todo(map)) { | |
512 | mutex_unlock(&map->active.out_mutex); | |
513 | pvcalls_exit(); | |
514 | return -EAGAIN; | |
515 | } | |
516 | if (len > INT_MAX) | |
517 | len = INT_MAX; | |
518 | ||
519 | again: | |
520 | count++; | |
521 | sent = __write_ring(map->active.ring, | |
522 | &map->active.data, &msg->msg_iter, | |
523 | len); | |
524 | if (sent > 0) { | |
525 | len -= sent; | |
526 | tot_sent += sent; | |
527 | notify_remote_via_irq(map->active.irq); | |
528 | } | |
529 | if (sent >= 0 && len > 0 && count < PVCALLS_FRONT_MAX_SPIN) | |
530 | goto again; | |
531 | if (sent < 0) | |
532 | tot_sent = sent; | |
533 | ||
534 | mutex_unlock(&map->active.out_mutex); | |
535 | pvcalls_exit(); | |
536 | return tot_sent; | |
537 | } | |
538 | ||
ae0d0405 SS |
539 | static int __read_ring(struct pvcalls_data_intf *intf, |
540 | struct pvcalls_data *data, | |
541 | struct iov_iter *msg_iter, | |
542 | size_t len, int flags) | |
543 | { | |
544 | RING_IDX cons, prod, size, masked_prod, masked_cons; | |
545 | RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER); | |
546 | int32_t error; | |
547 | ||
548 | cons = intf->in_cons; | |
549 | prod = intf->in_prod; | |
550 | error = intf->in_error; | |
551 | /* get pointers before reading from the ring */ | |
552 | virt_rmb(); | |
ae0d0405 SS |
553 | |
554 | size = pvcalls_queued(prod, cons, array_size); | |
555 | masked_prod = pvcalls_mask(prod, array_size); | |
556 | masked_cons = pvcalls_mask(cons, array_size); | |
557 | ||
558 | if (size == 0) | |
ee88f289 | 559 | return error ?: size; |
ae0d0405 SS |
560 | |
561 | if (len > size) | |
562 | len = size; | |
563 | ||
564 | if (masked_prod > masked_cons) { | |
565 | len = copy_to_iter(data->in + masked_cons, len, msg_iter); | |
566 | } else { | |
567 | if (len > (array_size - masked_cons)) { | |
568 | int ret = copy_to_iter(data->in + masked_cons, | |
569 | array_size - masked_cons, msg_iter); | |
570 | if (ret != array_size - masked_cons) { | |
571 | len = ret; | |
572 | goto out; | |
573 | } | |
574 | len = ret + copy_to_iter(data->in, len - ret, msg_iter); | |
575 | } else { | |
576 | len = copy_to_iter(data->in + masked_cons, len, msg_iter); | |
577 | } | |
578 | } | |
579 | out: | |
580 | /* read data from the ring before increasing the index */ | |
581 | virt_mb(); | |
582 | if (!(flags & MSG_PEEK)) | |
583 | intf->in_cons += len; | |
584 | ||
585 | return len; | |
586 | } | |
587 | ||
588 | int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, | |
589 | int flags) | |
590 | { | |
591 | struct pvcalls_bedata *bedata; | |
592 | int ret; | |
593 | struct sock_mapping *map; | |
594 | ||
595 | if (flags & (MSG_CMSG_CLOEXEC|MSG_ERRQUEUE|MSG_OOB|MSG_TRUNC)) | |
596 | return -EOPNOTSUPP; | |
597 | ||
598 | pvcalls_enter(); | |
599 | if (!pvcalls_front_dev) { | |
600 | pvcalls_exit(); | |
601 | return -ENOTCONN; | |
602 | } | |
603 | bedata = dev_get_drvdata(&pvcalls_front_dev->dev); | |
604 | ||
605 | map = (struct sock_mapping *) sock->sk->sk_send_head; | |
606 | if (!map) { | |
607 | pvcalls_exit(); | |
608 | return -ENOTSOCK; | |
609 | } | |
610 | ||
611 | mutex_lock(&map->active.in_mutex); | |
612 | if (len > XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER)) | |
613 | len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER); | |
614 | ||
615 | while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) { | |
616 | wait_event_interruptible(map->active.inflight_conn_req, | |
617 | pvcalls_front_read_todo(map)); | |
618 | } | |
619 | ret = __read_ring(map->active.ring, &map->active.data, | |
620 | &msg->msg_iter, len, flags); | |
621 | ||
622 | if (ret > 0) | |
623 | notify_remote_via_irq(map->active.irq); | |
624 | if (ret == 0) | |
625 | ret = (flags & MSG_DONTWAIT) ? -EAGAIN : 0; | |
626 | if (ret == -ENOTCONN) | |
627 | ret = 0; | |
628 | ||
629 | mutex_unlock(&map->active.in_mutex); | |
630 | pvcalls_exit(); | |
631 | return ret; | |
632 | } | |
633 | ||
67ea9893 SS |
634 | int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
635 | { | |
636 | struct pvcalls_bedata *bedata; | |
637 | struct sock_mapping *map = NULL; | |
638 | struct xen_pvcalls_request *req; | |
639 | int notify, req_id, ret; | |
640 | ||
641 | if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM) | |
642 | return -EOPNOTSUPP; | |
643 | ||
644 | pvcalls_enter(); | |
645 | if (!pvcalls_front_dev) { | |
646 | pvcalls_exit(); | |
647 | return -ENOTCONN; | |
648 | } | |
649 | bedata = dev_get_drvdata(&pvcalls_front_dev->dev); | |
650 | ||
651 | map = (struct sock_mapping *) sock->sk->sk_send_head; | |
652 | if (map == NULL) { | |
653 | pvcalls_exit(); | |
654 | return -ENOTSOCK; | |
655 | } | |
656 | ||
657 | spin_lock(&bedata->socket_lock); | |
658 | ret = get_request(bedata, &req_id); | |
659 | if (ret < 0) { | |
660 | spin_unlock(&bedata->socket_lock); | |
661 | pvcalls_exit(); | |
662 | return ret; | |
663 | } | |
664 | req = RING_GET_REQUEST(&bedata->ring, req_id); | |
665 | req->req_id = req_id; | |
666 | map->sock = sock; | |
667 | req->cmd = PVCALLS_BIND; | |
668 | req->u.bind.id = (uintptr_t)map; | |
669 | memcpy(req->u.bind.addr, addr, sizeof(*addr)); | |
670 | req->u.bind.len = addr_len; | |
671 | ||
9774c6cc SS |
672 | init_waitqueue_head(&map->passive.inflight_accept_req); |
673 | ||
67ea9893 SS |
674 | map->active_socket = false; |
675 | ||
676 | bedata->ring.req_prod_pvt++; | |
677 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); | |
678 | spin_unlock(&bedata->socket_lock); | |
679 | if (notify) | |
680 | notify_remote_via_irq(bedata->irq); | |
681 | ||
682 | wait_event(bedata->inflight_req, | |
683 | READ_ONCE(bedata->rsp[req_id].req_id) == req_id); | |
684 | ||
685 | /* read req_id, then the content */ | |
686 | smp_rmb(); | |
687 | ret = bedata->rsp[req_id].ret; | |
688 | bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; | |
689 | ||
690 | map->passive.status = PVCALLS_STATUS_BIND; | |
691 | pvcalls_exit(); | |
692 | return 0; | |
693 | } | |
694 | ||
1853f11d SS |
695 | int pvcalls_front_listen(struct socket *sock, int backlog) |
696 | { | |
697 | struct pvcalls_bedata *bedata; | |
698 | struct sock_mapping *map; | |
699 | struct xen_pvcalls_request *req; | |
700 | int notify, req_id, ret; | |
701 | ||
702 | pvcalls_enter(); | |
703 | if (!pvcalls_front_dev) { | |
704 | pvcalls_exit(); | |
705 | return -ENOTCONN; | |
706 | } | |
707 | bedata = dev_get_drvdata(&pvcalls_front_dev->dev); | |
708 | ||
709 | map = (struct sock_mapping *) sock->sk->sk_send_head; | |
710 | if (!map) { | |
711 | pvcalls_exit(); | |
712 | return -ENOTSOCK; | |
713 | } | |
714 | ||
715 | if (map->passive.status != PVCALLS_STATUS_BIND) { | |
716 | pvcalls_exit(); | |
717 | return -EOPNOTSUPP; | |
718 | } | |
719 | ||
720 | spin_lock(&bedata->socket_lock); | |
721 | ret = get_request(bedata, &req_id); | |
722 | if (ret < 0) { | |
723 | spin_unlock(&bedata->socket_lock); | |
724 | pvcalls_exit(); | |
725 | return ret; | |
726 | } | |
727 | req = RING_GET_REQUEST(&bedata->ring, req_id); | |
728 | req->req_id = req_id; | |
729 | req->cmd = PVCALLS_LISTEN; | |
730 | req->u.listen.id = (uintptr_t) map; | |
731 | req->u.listen.backlog = backlog; | |
732 | ||
733 | bedata->ring.req_prod_pvt++; | |
734 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); | |
735 | spin_unlock(&bedata->socket_lock); | |
736 | if (notify) | |
737 | notify_remote_via_irq(bedata->irq); | |
738 | ||
739 | wait_event(bedata->inflight_req, | |
740 | READ_ONCE(bedata->rsp[req_id].req_id) == req_id); | |
741 | ||
742 | /* read req_id, then the content */ | |
743 | smp_rmb(); | |
744 | ret = bedata->rsp[req_id].ret; | |
745 | bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; | |
746 | ||
747 | map->passive.status = PVCALLS_STATUS_LISTEN; | |
748 | pvcalls_exit(); | |
749 | return ret; | |
750 | } | |
751 | ||
9774c6cc SS |
752 | int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags) |
753 | { | |
754 | struct pvcalls_bedata *bedata; | |
755 | struct sock_mapping *map; | |
756 | struct sock_mapping *map2 = NULL; | |
757 | struct xen_pvcalls_request *req; | |
758 | int notify, req_id, ret, evtchn, nonblock; | |
759 | ||
760 | pvcalls_enter(); | |
761 | if (!pvcalls_front_dev) { | |
762 | pvcalls_exit(); | |
763 | return -ENOTCONN; | |
764 | } | |
765 | bedata = dev_get_drvdata(&pvcalls_front_dev->dev); | |
766 | ||
767 | map = (struct sock_mapping *) sock->sk->sk_send_head; | |
768 | if (!map) { | |
769 | pvcalls_exit(); | |
770 | return -ENOTSOCK; | |
771 | } | |
772 | ||
773 | if (map->passive.status != PVCALLS_STATUS_LISTEN) { | |
774 | pvcalls_exit(); | |
775 | return -EINVAL; | |
776 | } | |
777 | ||
778 | nonblock = flags & SOCK_NONBLOCK; | |
779 | /* | |
780 | * Backend only supports 1 inflight accept request, will return | |
781 | * errors for the others | |
782 | */ | |
783 | if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, | |
784 | (void *)&map->passive.flags)) { | |
785 | req_id = READ_ONCE(map->passive.inflight_req_id); | |
786 | if (req_id != PVCALLS_INVALID_ID && | |
787 | READ_ONCE(bedata->rsp[req_id].req_id) == req_id) { | |
788 | map2 = map->passive.accept_map; | |
789 | goto received; | |
790 | } | |
791 | if (nonblock) { | |
792 | pvcalls_exit(); | |
793 | return -EAGAIN; | |
794 | } | |
795 | if (wait_event_interruptible(map->passive.inflight_accept_req, | |
796 | !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, | |
797 | (void *)&map->passive.flags))) { | |
798 | pvcalls_exit(); | |
799 | return -EINTR; | |
800 | } | |
801 | } | |
802 | ||
803 | spin_lock(&bedata->socket_lock); | |
804 | ret = get_request(bedata, &req_id); | |
805 | if (ret < 0) { | |
806 | clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, | |
807 | (void *)&map->passive.flags); | |
808 | spin_unlock(&bedata->socket_lock); | |
809 | pvcalls_exit(); | |
810 | return ret; | |
811 | } | |
4aac2caf | 812 | map2 = kzalloc(sizeof(*map2), GFP_ATOMIC); |
9774c6cc SS |
813 | if (map2 == NULL) { |
814 | clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, | |
815 | (void *)&map->passive.flags); | |
816 | spin_unlock(&bedata->socket_lock); | |
817 | pvcalls_exit(); | |
818 | return -ENOMEM; | |
819 | } | |
820 | ret = create_active(map2, &evtchn); | |
821 | if (ret < 0) { | |
822 | kfree(map2); | |
823 | clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, | |
824 | (void *)&map->passive.flags); | |
825 | spin_unlock(&bedata->socket_lock); | |
826 | pvcalls_exit(); | |
827 | return ret; | |
828 | } | |
829 | list_add_tail(&map2->list, &bedata->socket_mappings); | |
830 | ||
831 | req = RING_GET_REQUEST(&bedata->ring, req_id); | |
832 | req->req_id = req_id; | |
833 | req->cmd = PVCALLS_ACCEPT; | |
834 | req->u.accept.id = (uintptr_t) map; | |
835 | req->u.accept.ref = map2->active.ref; | |
836 | req->u.accept.id_new = (uintptr_t) map2; | |
837 | req->u.accept.evtchn = evtchn; | |
838 | map->passive.accept_map = map2; | |
839 | ||
840 | bedata->ring.req_prod_pvt++; | |
841 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); | |
842 | spin_unlock(&bedata->socket_lock); | |
843 | if (notify) | |
844 | notify_remote_via_irq(bedata->irq); | |
845 | /* We could check if we have received a response before returning. */ | |
846 | if (nonblock) { | |
847 | WRITE_ONCE(map->passive.inflight_req_id, req_id); | |
848 | pvcalls_exit(); | |
849 | return -EAGAIN; | |
850 | } | |
851 | ||
852 | if (wait_event_interruptible(bedata->inflight_req, | |
853 | READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) { | |
854 | pvcalls_exit(); | |
855 | return -EINTR; | |
856 | } | |
857 | /* read req_id, then the content */ | |
858 | smp_rmb(); | |
859 | ||
860 | received: | |
861 | map2->sock = newsock; | |
63be5d01 | 862 | newsock->sk = sk_alloc(sock_net(sock->sk), PF_INET, GFP_KERNEL, &pvcalls_proto, false); |
9774c6cc SS |
863 | if (!newsock->sk) { |
864 | bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; | |
865 | map->passive.inflight_req_id = PVCALLS_INVALID_ID; | |
866 | clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, | |
867 | (void *)&map->passive.flags); | |
868 | pvcalls_front_free_map(bedata, map2); | |
869 | pvcalls_exit(); | |
870 | return -ENOMEM; | |
871 | } | |
872 | newsock->sk->sk_send_head = (void *)map2; | |
873 | ||
874 | ret = bedata->rsp[req_id].ret; | |
875 | bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; | |
876 | map->passive.inflight_req_id = PVCALLS_INVALID_ID; | |
877 | ||
878 | clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags); | |
879 | wake_up(&map->passive.inflight_accept_req); | |
880 | ||
881 | pvcalls_exit(); | |
882 | return ret; | |
883 | } | |
884 | ||
5842c835 SS |
885 | static unsigned int pvcalls_front_poll_passive(struct file *file, |
886 | struct pvcalls_bedata *bedata, | |
887 | struct sock_mapping *map, | |
888 | poll_table *wait) | |
889 | { | |
890 | int notify, req_id, ret; | |
891 | struct xen_pvcalls_request *req; | |
892 | ||
893 | if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, | |
894 | (void *)&map->passive.flags)) { | |
895 | uint32_t req_id = READ_ONCE(map->passive.inflight_req_id); | |
896 | ||
897 | if (req_id != PVCALLS_INVALID_ID && | |
898 | READ_ONCE(bedata->rsp[req_id].req_id) == req_id) | |
899 | return POLLIN | POLLRDNORM; | |
900 | ||
901 | poll_wait(file, &map->passive.inflight_accept_req, wait); | |
902 | return 0; | |
903 | } | |
904 | ||
905 | if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET, | |
906 | (void *)&map->passive.flags)) | |
907 | return POLLIN | POLLRDNORM; | |
908 | ||
909 | /* | |
910 | * First check RET, then INFLIGHT. No barriers necessary to | |
911 | * ensure execution ordering because of the conditional | |
912 | * instructions creating control dependencies. | |
913 | */ | |
914 | ||
915 | if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT, | |
916 | (void *)&map->passive.flags)) { | |
917 | poll_wait(file, &bedata->inflight_req, wait); | |
918 | return 0; | |
919 | } | |
920 | ||
921 | spin_lock(&bedata->socket_lock); | |
922 | ret = get_request(bedata, &req_id); | |
923 | if (ret < 0) { | |
924 | spin_unlock(&bedata->socket_lock); | |
925 | return ret; | |
926 | } | |
927 | req = RING_GET_REQUEST(&bedata->ring, req_id); | |
928 | req->req_id = req_id; | |
929 | req->cmd = PVCALLS_POLL; | |
930 | req->u.poll.id = (uintptr_t) map; | |
931 | ||
932 | bedata->ring.req_prod_pvt++; | |
933 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); | |
934 | spin_unlock(&bedata->socket_lock); | |
935 | if (notify) | |
936 | notify_remote_via_irq(bedata->irq); | |
937 | ||
938 | poll_wait(file, &bedata->inflight_req, wait); | |
939 | return 0; | |
940 | } | |
941 | ||
942 | static unsigned int pvcalls_front_poll_active(struct file *file, | |
943 | struct pvcalls_bedata *bedata, | |
944 | struct sock_mapping *map, | |
945 | poll_table *wait) | |
946 | { | |
947 | unsigned int mask = 0; | |
948 | int32_t in_error, out_error; | |
949 | struct pvcalls_data_intf *intf = map->active.ring; | |
950 | ||
951 | out_error = intf->out_error; | |
952 | in_error = intf->in_error; | |
953 | ||
954 | poll_wait(file, &map->active.inflight_conn_req, wait); | |
955 | if (pvcalls_front_write_todo(map)) | |
956 | mask |= POLLOUT | POLLWRNORM; | |
957 | if (pvcalls_front_read_todo(map)) | |
958 | mask |= POLLIN | POLLRDNORM; | |
959 | if (in_error != 0 || out_error != 0) | |
960 | mask |= POLLERR; | |
961 | ||
962 | return mask; | |
963 | } | |
964 | ||
965 | unsigned int pvcalls_front_poll(struct file *file, struct socket *sock, | |
966 | poll_table *wait) | |
967 | { | |
968 | struct pvcalls_bedata *bedata; | |
969 | struct sock_mapping *map; | |
970 | int ret; | |
971 | ||
972 | pvcalls_enter(); | |
973 | if (!pvcalls_front_dev) { | |
974 | pvcalls_exit(); | |
975 | return POLLNVAL; | |
976 | } | |
977 | bedata = dev_get_drvdata(&pvcalls_front_dev->dev); | |
978 | ||
979 | map = (struct sock_mapping *) sock->sk->sk_send_head; | |
980 | if (!map) { | |
981 | pvcalls_exit(); | |
982 | return POLLNVAL; | |
983 | } | |
984 | if (map->active_socket) | |
985 | ret = pvcalls_front_poll_active(file, bedata, map, wait); | |
986 | else | |
987 | ret = pvcalls_front_poll_passive(file, bedata, map, wait); | |
988 | pvcalls_exit(); | |
989 | return ret; | |
990 | } | |
991 | ||
235a71c5 SS |
992 | int pvcalls_front_release(struct socket *sock) |
993 | { | |
994 | struct pvcalls_bedata *bedata; | |
995 | struct sock_mapping *map; | |
996 | int req_id, notify, ret; | |
997 | struct xen_pvcalls_request *req; | |
998 | ||
999 | if (sock->sk == NULL) | |
1000 | return 0; | |
1001 | ||
1002 | pvcalls_enter(); | |
1003 | if (!pvcalls_front_dev) { | |
1004 | pvcalls_exit(); | |
1005 | return -EIO; | |
1006 | } | |
1007 | ||
1008 | bedata = dev_get_drvdata(&pvcalls_front_dev->dev); | |
1009 | ||
1010 | map = (struct sock_mapping *) sock->sk->sk_send_head; | |
1011 | if (map == NULL) { | |
1012 | pvcalls_exit(); | |
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | spin_lock(&bedata->socket_lock); | |
1017 | ret = get_request(bedata, &req_id); | |
1018 | if (ret < 0) { | |
1019 | spin_unlock(&bedata->socket_lock); | |
1020 | pvcalls_exit(); | |
1021 | return ret; | |
1022 | } | |
1023 | sock->sk->sk_send_head = NULL; | |
1024 | ||
1025 | req = RING_GET_REQUEST(&bedata->ring, req_id); | |
1026 | req->req_id = req_id; | |
1027 | req->cmd = PVCALLS_RELEASE; | |
1028 | req->u.release.id = (uintptr_t)map; | |
1029 | ||
1030 | bedata->ring.req_prod_pvt++; | |
1031 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); | |
1032 | spin_unlock(&bedata->socket_lock); | |
1033 | if (notify) | |
1034 | notify_remote_via_irq(bedata->irq); | |
1035 | ||
1036 | wait_event(bedata->inflight_req, | |
1037 | READ_ONCE(bedata->rsp[req_id].req_id) == req_id); | |
1038 | ||
1039 | if (map->active_socket) { | |
1040 | /* | |
1041 | * Set in_error and wake up inflight_conn_req to force | |
1042 | * recvmsg waiters to exit. | |
1043 | */ | |
1044 | map->active.ring->in_error = -EBADF; | |
1045 | wake_up_interruptible(&map->active.inflight_conn_req); | |
1046 | ||
1047 | /* | |
646d944c SS |
1048 | * We need to make sure that sendmsg/recvmsg on this socket have |
1049 | * not started before we've cleared sk_send_head here. The | |
1050 | * easiest (though not optimal) way to guarantee this is to see | |
1051 | * that no pvcall (other than us) is in progress. | |
235a71c5 | 1052 | */ |
646d944c | 1053 | while (atomic_read(&pvcalls_refcount) > 1) |
235a71c5 SS |
1054 | cpu_relax(); |
1055 | ||
1056 | pvcalls_front_free_map(bedata, map); | |
1057 | } else { | |
1058 | spin_lock(&bedata->socket_lock); | |
1059 | list_del(&map->list); | |
1060 | spin_unlock(&bedata->socket_lock); | |
c2c273f9 SS |
1061 | if (READ_ONCE(map->passive.inflight_req_id) != PVCALLS_INVALID_ID && |
1062 | READ_ONCE(map->passive.inflight_req_id) != 0) { | |
235a71c5 SS |
1063 | pvcalls_front_free_map(bedata, |
1064 | map->passive.accept_map); | |
1065 | } | |
1066 | kfree(map); | |
1067 | } | |
1068 | WRITE_ONCE(bedata->rsp[req_id].req_id, PVCALLS_INVALID_ID); | |
1069 | ||
1070 | pvcalls_exit(); | |
1071 | return 0; | |
1072 | } | |
1073 | ||
416efba0 SS |
1074 | static const struct xenbus_device_id pvcalls_front_ids[] = { |
1075 | { "pvcalls" }, | |
1076 | { "" } | |
1077 | }; | |
1078 | ||
1079 | static int pvcalls_front_remove(struct xenbus_device *dev) | |
1080 | { | |
aa7ba376 SS |
1081 | struct pvcalls_bedata *bedata; |
1082 | struct sock_mapping *map = NULL, *n; | |
1083 | ||
1084 | bedata = dev_get_drvdata(&pvcalls_front_dev->dev); | |
1085 | dev_set_drvdata(&dev->dev, NULL); | |
1086 | pvcalls_front_dev = NULL; | |
1087 | if (bedata->irq >= 0) | |
1088 | unbind_from_irqhandler(bedata->irq, dev); | |
1089 | ||
cb1c7d9b SS |
1090 | list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) { |
1091 | map->sock->sk->sk_send_head = NULL; | |
1092 | if (map->active_socket) { | |
1093 | map->active.ring->in_error = -EBADF; | |
1094 | wake_up_interruptible(&map->active.inflight_conn_req); | |
1095 | } | |
1096 | } | |
1097 | ||
aa7ba376 SS |
1098 | smp_mb(); |
1099 | while (atomic_read(&pvcalls_refcount) > 0) | |
1100 | cpu_relax(); | |
1101 | list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) { | |
1102 | if (map->active_socket) { | |
1103 | /* No need to lock, refcount is 0 */ | |
1104 | pvcalls_front_free_map(bedata, map); | |
1105 | } else { | |
1106 | list_del(&map->list); | |
1107 | kfree(map); | |
1108 | } | |
1109 | } | |
1ab134ca | 1110 | if (bedata->ref != -1) |
aa7ba376 SS |
1111 | gnttab_end_foreign_access(bedata->ref, 0, 0); |
1112 | kfree(bedata->ring.sring); | |
1113 | kfree(bedata); | |
1114 | xenbus_switch_state(dev, XenbusStateClosed); | |
416efba0 SS |
1115 | return 0; |
1116 | } | |
1117 | ||
1118 | static int pvcalls_front_probe(struct xenbus_device *dev, | |
1119 | const struct xenbus_device_id *id) | |
1120 | { | |
21968190 SS |
1121 | int ret = -ENOMEM, evtchn, i; |
1122 | unsigned int max_page_order, function_calls, len; | |
1123 | char *versions; | |
1124 | grant_ref_t gref_head = 0; | |
1125 | struct xenbus_transaction xbt; | |
1126 | struct pvcalls_bedata *bedata = NULL; | |
1127 | struct xen_pvcalls_sring *sring; | |
1128 | ||
1129 | if (pvcalls_front_dev != NULL) { | |
1130 | dev_err(&dev->dev, "only one PV Calls connection supported\n"); | |
1131 | return -EINVAL; | |
1132 | } | |
1133 | ||
1134 | versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len); | |
8c71fa88 DC |
1135 | if (IS_ERR(versions)) |
1136 | return PTR_ERR(versions); | |
21968190 SS |
1137 | if (!len) |
1138 | return -EINVAL; | |
1139 | if (strcmp(versions, "1")) { | |
1140 | kfree(versions); | |
1141 | return -EINVAL; | |
1142 | } | |
1143 | kfree(versions); | |
1144 | max_page_order = xenbus_read_unsigned(dev->otherend, | |
1145 | "max-page-order", 0); | |
1146 | if (max_page_order < PVCALLS_RING_ORDER) | |
1147 | return -ENODEV; | |
1148 | function_calls = xenbus_read_unsigned(dev->otherend, | |
1149 | "function-calls", 0); | |
1150 | /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */ | |
1151 | if (function_calls != 1) | |
1152 | return -ENODEV; | |
1153 | pr_info("%s max-page-order is %u\n", __func__, max_page_order); | |
1154 | ||
1155 | bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL); | |
1156 | if (!bedata) | |
1157 | return -ENOMEM; | |
1158 | ||
1159 | dev_set_drvdata(&dev->dev, bedata); | |
1160 | pvcalls_front_dev = dev; | |
1161 | init_waitqueue_head(&bedata->inflight_req); | |
1162 | INIT_LIST_HEAD(&bedata->socket_mappings); | |
1163 | spin_lock_init(&bedata->socket_lock); | |
1164 | bedata->irq = -1; | |
1165 | bedata->ref = -1; | |
1166 | ||
1167 | for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++) | |
1168 | bedata->rsp[i].req_id = PVCALLS_INVALID_ID; | |
1169 | ||
1170 | sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL | | |
1171 | __GFP_ZERO); | |
1172 | if (!sring) | |
1173 | goto error; | |
1174 | SHARED_RING_INIT(sring); | |
1175 | FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE); | |
1176 | ||
1177 | ret = xenbus_alloc_evtchn(dev, &evtchn); | |
1178 | if (ret) | |
1179 | goto error; | |
1180 | ||
1181 | bedata->irq = bind_evtchn_to_irqhandler(evtchn, | |
1182 | pvcalls_front_event_handler, | |
1183 | 0, "pvcalls-frontend", dev); | |
1184 | if (bedata->irq < 0) { | |
1185 | ret = bedata->irq; | |
1186 | goto error; | |
1187 | } | |
1188 | ||
1189 | ret = gnttab_alloc_grant_references(1, &gref_head); | |
1190 | if (ret < 0) | |
1191 | goto error; | |
95110ac8 CIK |
1192 | ret = gnttab_claim_grant_reference(&gref_head); |
1193 | if (ret < 0) | |
21968190 | 1194 | goto error; |
95110ac8 | 1195 | bedata->ref = ret; |
21968190 SS |
1196 | gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id, |
1197 | virt_to_gfn((void *)sring), 0); | |
1198 | ||
1199 | again: | |
1200 | ret = xenbus_transaction_start(&xbt); | |
1201 | if (ret) { | |
1202 | xenbus_dev_fatal(dev, ret, "starting transaction"); | |
1203 | goto error; | |
1204 | } | |
1205 | ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1); | |
1206 | if (ret) | |
1207 | goto error_xenbus; | |
1208 | ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref); | |
1209 | if (ret) | |
1210 | goto error_xenbus; | |
1211 | ret = xenbus_printf(xbt, dev->nodename, "port", "%u", | |
1212 | evtchn); | |
1213 | if (ret) | |
1214 | goto error_xenbus; | |
1215 | ret = xenbus_transaction_end(xbt, 0); | |
1216 | if (ret) { | |
1217 | if (ret == -EAGAIN) | |
1218 | goto again; | |
1219 | xenbus_dev_fatal(dev, ret, "completing transaction"); | |
1220 | goto error; | |
1221 | } | |
1222 | xenbus_switch_state(dev, XenbusStateInitialised); | |
1223 | ||
416efba0 | 1224 | return 0; |
21968190 SS |
1225 | |
1226 | error_xenbus: | |
1227 | xenbus_transaction_end(xbt, 1); | |
1228 | xenbus_dev_fatal(dev, ret, "writing xenstore"); | |
1229 | error: | |
1230 | pvcalls_front_remove(dev); | |
1231 | return ret; | |
416efba0 SS |
1232 | } |
1233 | ||
1234 | static void pvcalls_front_changed(struct xenbus_device *dev, | |
1235 | enum xenbus_state backend_state) | |
1236 | { | |
21968190 SS |
1237 | switch (backend_state) { |
1238 | case XenbusStateReconfiguring: | |
1239 | case XenbusStateReconfigured: | |
1240 | case XenbusStateInitialising: | |
1241 | case XenbusStateInitialised: | |
1242 | case XenbusStateUnknown: | |
1243 | break; | |
1244 | ||
1245 | case XenbusStateInitWait: | |
1246 | break; | |
1247 | ||
1248 | case XenbusStateConnected: | |
1249 | xenbus_switch_state(dev, XenbusStateConnected); | |
1250 | break; | |
1251 | ||
1252 | case XenbusStateClosed: | |
1253 | if (dev->state == XenbusStateClosed) | |
1254 | break; | |
3d8765d4 GS |
1255 | /* Missed the backend's CLOSING state */ |
1256 | /* fall through */ | |
21968190 SS |
1257 | case XenbusStateClosing: |
1258 | xenbus_frontend_closed(dev); | |
1259 | break; | |
1260 | } | |
416efba0 SS |
1261 | } |
1262 | ||
1263 | static struct xenbus_driver pvcalls_front_driver = { | |
1264 | .ids = pvcalls_front_ids, | |
1265 | .probe = pvcalls_front_probe, | |
1266 | .remove = pvcalls_front_remove, | |
1267 | .otherend_changed = pvcalls_front_changed, | |
1268 | }; | |
1269 | ||
1270 | static int __init pvcalls_frontend_init(void) | |
1271 | { | |
1272 | if (!xen_domain()) | |
1273 | return -ENODEV; | |
1274 | ||
1275 | pr_info("Initialising Xen pvcalls frontend driver\n"); | |
1276 | ||
1277 | return xenbus_register_frontend(&pvcalls_front_driver); | |
1278 | } | |
1279 | ||
1280 | module_init(pvcalls_frontend_init); | |
24e7f84d BO |
1281 | |
1282 | MODULE_DESCRIPTION("Xen PV Calls frontend driver"); | |
1283 | MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>"); | |
1284 | MODULE_LICENSE("GPL"); |