Merge tag 'seccomp-v6.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees...
[linux-2.6-block.git] / net / 9p / trans_xen.c
CommitLineData
024b7d6a 1// SPDX-License-Identifier: GPL-2.0-only
868eb122
SS
2/*
3 * linux/fs/9p/trans_xen
4 *
5 * Xen transport layer.
6 *
7 * Copyright (C) 2017 by Stefano Stabellini <stefano@aporeto.com>
868eb122
SS
8 */
9
10#include <xen/events.h>
11#include <xen/grant_table.h>
12#include <xen/xen.h>
13#include <xen/xenbus.h>
14#include <xen/interface/io/9pfs.h>
15
16#include <linux/module.h>
71ebd719 17#include <linux/spinlock.h>
868eb122
SS
18#include <net/9p/9p.h>
19#include <net/9p/client.h>
20#include <net/9p/transport.h>
21
71ebd719 22#define XEN_9PFS_NUM_RINGS 2
36f99675
SS
23#define XEN_9PFS_RING_ORDER 9
24#define XEN_9PFS_RING_SIZE(ring) XEN_FLEX_RING_SIZE(ring->intf->ring_order)
71ebd719
SS
25
26struct xen_9pfs_header {
27 uint32_t size;
28 uint8_t id;
29 uint16_t tag;
30
31 /* uint8_t sdata[]; */
32} __attribute__((packed));
33
34/* One per ring, more than one per 9pfs share */
35struct xen_9pfs_dataring {
36 struct xen_9pfs_front_priv *priv;
37
38 struct xen_9pfs_data_intf *intf;
39 grant_ref_t ref;
40 int evtchn;
41 int irq;
42 /* protect a ring from concurrent accesses */
43 spinlock_t lock;
44
45 struct xen_9pfs_data data;
46 wait_queue_head_t wq;
47 struct work_struct work;
48};
49
50/* One per 9pfs share */
51struct xen_9pfs_front_priv {
52 struct list_head list;
53 struct xenbus_device *dev;
54 char *tag;
55 struct p9_client *client;
56
71ebd719
SS
57 struct xen_9pfs_dataring *rings;
58};
59
60static LIST_HEAD(xen_9pfs_devs);
61static DEFINE_RWLOCK(xen_9pfs_lock);
62
f023f18d 63/* We don't currently allow canceling of requests */
868eb122
SS
64static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req)
65{
f023f18d 66 return 1;
868eb122
SS
67}
68
69static int p9_xen_create(struct p9_client *client, const char *addr, char *args)
70{
f023f18d
SS
71 struct xen_9pfs_front_priv *priv;
72
10aa1452
TB
73 if (addr == NULL)
74 return -EINVAL;
75
f023f18d
SS
76 read_lock(&xen_9pfs_lock);
77 list_for_each_entry(priv, &xen_9pfs_devs, list) {
78 if (!strcmp(priv->tag, addr)) {
79 priv->client = client;
80 read_unlock(&xen_9pfs_lock);
81 return 0;
82 }
83 }
84 read_unlock(&xen_9pfs_lock);
85 return -EINVAL;
868eb122
SS
86}
87
88static void p9_xen_close(struct p9_client *client)
89{
f023f18d
SS
90 struct xen_9pfs_front_priv *priv;
91
92 read_lock(&xen_9pfs_lock);
93 list_for_each_entry(priv, &xen_9pfs_devs, list) {
94 if (priv->client == client) {
95 priv->client = NULL;
96 read_unlock(&xen_9pfs_lock);
97 return;
98 }
99 }
100 read_unlock(&xen_9pfs_lock);
101}
102
103static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size)
104{
105 RING_IDX cons, prod;
106
107 cons = ring->intf->out_cons;
108 prod = ring->intf->out_prod;
109 virt_mb();
110
36f99675
SS
111 return XEN_9PFS_RING_SIZE(ring) -
112 xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) >= size;
868eb122
SS
113}
114
115static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
116{
732b33d0 117 struct xen_9pfs_front_priv *priv;
f023f18d
SS
118 RING_IDX cons, prod, masked_cons, masked_prod;
119 unsigned long flags;
523adb6c 120 u32 size = p9_req->tc.size;
f023f18d
SS
121 struct xen_9pfs_dataring *ring;
122 int num;
123
124 read_lock(&xen_9pfs_lock);
125 list_for_each_entry(priv, &xen_9pfs_devs, list) {
126 if (priv->client == client)
127 break;
128 }
129 read_unlock(&xen_9pfs_lock);
732b33d0 130 if (list_entry_is_head(priv, &xen_9pfs_devs, list))
f023f18d
SS
131 return -EINVAL;
132
39763480 133 num = p9_req->tc.tag % XEN_9PFS_NUM_RINGS;
f023f18d
SS
134 ring = &priv->rings[num];
135
136again:
9523feac
TT
137 while (wait_event_killable(ring->wq,
138 p9_xen_write_todo(ring, size)) != 0)
f023f18d
SS
139 ;
140
141 spin_lock_irqsave(&ring->lock, flags);
142 cons = ring->intf->out_cons;
143 prod = ring->intf->out_prod;
144 virt_mb();
145
36f99675
SS
146 if (XEN_9PFS_RING_SIZE(ring) -
147 xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) < size) {
f023f18d
SS
148 spin_unlock_irqrestore(&ring->lock, flags);
149 goto again;
150 }
151
36f99675
SS
152 masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring));
153 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
f023f18d 154
523adb6c 155 xen_9pfs_write_packet(ring->data.out, p9_req->tc.sdata, size,
36f99675
SS
156 &masked_prod, masked_cons,
157 XEN_9PFS_RING_SIZE(ring));
f023f18d 158
1a4f69ef 159 WRITE_ONCE(p9_req->status, REQ_STATUS_SENT);
f023f18d
SS
160 virt_wmb(); /* write ring before updating pointer */
161 prod += size;
162 ring->intf->out_prod = prod;
163 spin_unlock_irqrestore(&ring->lock, flags);
164 notify_remote_via_irq(ring->irq);
8b11ff09 165 p9_req_put(client, p9_req);
f023f18d 166
868eb122
SS
167 return 0;
168}
169
71ebd719
SS
170static void p9_xen_response(struct work_struct *work)
171{
f66c72be
SS
172 struct xen_9pfs_front_priv *priv;
173 struct xen_9pfs_dataring *ring;
174 RING_IDX cons, prod, masked_cons, masked_prod;
175 struct xen_9pfs_header h;
176 struct p9_req_t *req;
177 int status;
178
179 ring = container_of(work, struct xen_9pfs_dataring, work);
180 priv = ring->priv;
181
182 while (1) {
183 cons = ring->intf->in_cons;
184 prod = ring->intf->in_prod;
185 virt_rmb();
186
36f99675 187 if (xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) <
f66c72be
SS
188 sizeof(h)) {
189 notify_remote_via_irq(ring->irq);
190 return;
191 }
192
36f99675
SS
193 masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring));
194 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
f66c72be
SS
195
196 /* First, read just the header */
197 xen_9pfs_read_packet(&h, ring->data.in, sizeof(h),
198 masked_prod, &masked_cons,
36f99675 199 XEN_9PFS_RING_SIZE(ring));
f66c72be
SS
200
201 req = p9_tag_lookup(priv->client, h.tag);
202 if (!req || req->status != REQ_STATUS_SENT) {
203 dev_warn(&priv->dev->dev, "Wrong req tag=%x\n", h.tag);
204 cons += h.size;
205 virt_mb();
206 ring->intf->in_cons = cons;
207 continue;
208 }
209
391c18cf
DM
210 if (h.size > req->rc.capacity) {
211 dev_warn(&priv->dev->dev,
212 "requested packet size too big: %d for tag %d with capacity %zd\n",
213 h.size, h.tag, req->rc.capacity);
1a4f69ef 214 WRITE_ONCE(req->status, REQ_STATUS_ERROR);
391c18cf
DM
215 goto recv_error;
216 }
217
f15e006b
DM
218 req->rc.size = h.size;
219 req->rc.id = h.id;
220 req->rc.tag = h.tag;
523adb6c 221 req->rc.offset = 0;
f66c72be 222
36f99675 223 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
f66c72be 224 /* Then, read the whole packet (including the header) */
523adb6c 225 xen_9pfs_read_packet(req->rc.sdata, ring->data.in, h.size,
f66c72be 226 masked_prod, &masked_cons,
36f99675 227 XEN_9PFS_RING_SIZE(ring));
f66c72be 228
391c18cf 229recv_error:
f66c72be
SS
230 virt_mb();
231 cons += h.size;
232 ring->intf->in_cons = cons;
233
234 status = (req->status != REQ_STATUS_ERROR) ?
235 REQ_STATUS_RCVD : REQ_STATUS_ERROR;
236
237 p9_client_cb(priv->client, req, status);
238 }
71ebd719
SS
239}
240
241static irqreturn_t xen_9pfs_front_event_handler(int irq, void *r)
242{
243 struct xen_9pfs_dataring *ring = r;
244
245 if (!ring || !ring->priv->client) {
246 /* ignore spurious interrupt */
247 return IRQ_HANDLED;
248 }
249
250 wake_up_interruptible(&ring->wq);
251 schedule_work(&ring->work);
252
253 return IRQ_HANDLED;
254}
255
868eb122
SS
256static struct p9_trans_module p9_xen_trans = {
257 .name = "xen",
36f99675 258 .maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT - 2),
01d205d9 259 .pooled_rbuffers = false,
868eb122
SS
260 .def = 1,
261 .create = p9_xen_create,
262 .close = p9_xen_close,
263 .request = p9_xen_request,
264 .cancel = p9_xen_cancel,
265 .owner = THIS_MODULE,
266};
267
268static const struct xenbus_device_id xen_9pfs_front_ids[] = {
269 { "9pfs" },
270 { "" }
271};
272
71ebd719
SS
273static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv)
274{
275 int i, j;
276
277 write_lock(&xen_9pfs_lock);
278 list_del(&priv->list);
279 write_unlock(&xen_9pfs_lock);
280
39763480 281 for (i = 0; i < XEN_9PFS_NUM_RINGS; i++) {
ea4f1009
ZW
282 struct xen_9pfs_dataring *ring = &priv->rings[i];
283
284 cancel_work_sync(&ring->work);
285
71ebd719
SS
286 if (!priv->rings[i].intf)
287 break;
288 if (priv->rings[i].irq > 0)
289 unbind_from_irqhandler(priv->rings[i].irq, priv->dev);
290 if (priv->rings[i].data.in) {
36f99675
SS
291 for (j = 0;
292 j < (1 << priv->rings[i].intf->ring_order);
293 j++) {
71ebd719
SS
294 grant_ref_t ref;
295
296 ref = priv->rings[i].intf->ref[j];
49f8b459 297 gnttab_end_foreign_access(ref, NULL);
71ebd719 298 }
5cadd4bb
JG
299 free_pages_exact(priv->rings[i].data.in,
300 1UL << (priv->rings[i].intf->ring_order +
301 XEN_PAGE_SHIFT));
71ebd719 302 }
49f8b459 303 gnttab_end_foreign_access(priv->rings[i].ref, NULL);
71ebd719
SS
304 free_page((unsigned long)priv->rings[i].intf);
305 }
306 kfree(priv->rings);
307 kfree(priv->tag);
308 kfree(priv);
309}
310
7cffcade 311static void xen_9pfs_front_remove(struct xenbus_device *dev)
868eb122 312{
71ebd719
SS
313 struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
314
315 dev_set_drvdata(&dev->dev, NULL);
316 xen_9pfs_front_free(priv);
868eb122
SS
317}
318
71ebd719 319static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev,
36f99675
SS
320 struct xen_9pfs_dataring *ring,
321 unsigned int order)
71ebd719
SS
322{
323 int i = 0;
324 int ret = -ENOMEM;
325 void *bytes = NULL;
326
327 init_waitqueue_head(&ring->wq);
328 spin_lock_init(&ring->lock);
329 INIT_WORK(&ring->work, p9_xen_response);
330
331 ring->intf = (struct xen_9pfs_data_intf *)get_zeroed_page(GFP_KERNEL);
332 if (!ring->intf)
333 return ret;
334 ret = gnttab_grant_foreign_access(dev->otherend_id,
335 virt_to_gfn(ring->intf), 0);
336 if (ret < 0)
337 goto out;
338 ring->ref = ret;
5cadd4bb
JG
339 bytes = alloc_pages_exact(1UL << (order + XEN_PAGE_SHIFT),
340 GFP_KERNEL | __GFP_ZERO);
71ebd719
SS
341 if (!bytes) {
342 ret = -ENOMEM;
343 goto out;
344 }
36f99675 345 for (; i < (1 << order); i++) {
71ebd719
SS
346 ret = gnttab_grant_foreign_access(
347 dev->otherend_id, virt_to_gfn(bytes) + i, 0);
348 if (ret < 0)
349 goto out;
350 ring->intf->ref[i] = ret;
351 }
36f99675 352 ring->intf->ring_order = order;
71ebd719 353 ring->data.in = bytes;
36f99675 354 ring->data.out = bytes + XEN_FLEX_RING_SIZE(order);
71ebd719
SS
355
356 ret = xenbus_alloc_evtchn(dev, &ring->evtchn);
357 if (ret)
358 goto out;
359 ring->irq = bind_evtchn_to_irqhandler(ring->evtchn,
360 xen_9pfs_front_event_handler,
361 0, "xen_9pfs-frontend", ring);
362 if (ring->irq >= 0)
363 return 0;
364
365 xenbus_free_evtchn(dev, ring->evtchn);
366 ret = ring->irq;
367out:
368 if (bytes) {
369 for (i--; i >= 0; i--)
49f8b459 370 gnttab_end_foreign_access(ring->intf->ref[i], NULL);
5cadd4bb 371 free_pages_exact(bytes, 1UL << (order + XEN_PAGE_SHIFT));
71ebd719 372 }
49f8b459 373 gnttab_end_foreign_access(ring->ref, NULL);
71ebd719
SS
374 free_page((unsigned long)ring->intf);
375 return ret;
376}
377
c15fe55d 378static int xen_9pfs_front_init(struct xenbus_device *dev)
868eb122 379{
71ebd719
SS
380 int ret, i;
381 struct xenbus_transaction xbt;
c15fe55d 382 struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
f1956f4e 383 char *versions, *v;
31d47266 384 unsigned int max_rings, max_ring_order, len = 0;
71ebd719
SS
385
386 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
2f9ad0ac
DM
387 if (IS_ERR(versions))
388 return PTR_ERR(versions);
f1956f4e
JG
389 for (v = versions; *v; v++) {
390 if (simple_strtoul(v, &v, 10) == 1) {
391 v = NULL;
392 break;
393 }
394 }
395 if (v) {
71ebd719
SS
396 kfree(versions);
397 return -EINVAL;
398 }
399 kfree(versions);
400 max_rings = xenbus_read_unsigned(dev->otherend, "max-rings", 0);
401 if (max_rings < XEN_9PFS_NUM_RINGS)
402 return -EINVAL;
403 max_ring_order = xenbus_read_unsigned(dev->otherend,
404 "max-ring-page-order", 0);
36f99675
SS
405 if (max_ring_order > XEN_9PFS_RING_ORDER)
406 max_ring_order = XEN_9PFS_RING_ORDER;
407 if (p9_xen_trans.maxsize > XEN_FLEX_RING_SIZE(max_ring_order))
408 p9_xen_trans.maxsize = XEN_FLEX_RING_SIZE(max_ring_order) / 2;
71ebd719 409
39763480 410 priv->rings = kcalloc(XEN_9PFS_NUM_RINGS, sizeof(*priv->rings),
71ebd719
SS
411 GFP_KERNEL);
412 if (!priv->rings) {
413 kfree(priv);
414 return -ENOMEM;
415 }
416
39763480 417 for (i = 0; i < XEN_9PFS_NUM_RINGS; i++) {
71ebd719 418 priv->rings[i].priv = priv;
36f99675
SS
419 ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i],
420 max_ring_order);
71ebd719
SS
421 if (ret < 0)
422 goto error;
423 }
424
425 again:
426 ret = xenbus_transaction_start(&xbt);
427 if (ret) {
428 xenbus_dev_fatal(dev, ret, "starting transaction");
429 goto error;
430 }
431 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
432 if (ret)
433 goto error_xenbus;
434 ret = xenbus_printf(xbt, dev->nodename, "num-rings", "%u",
39763480 435 XEN_9PFS_NUM_RINGS);
71ebd719
SS
436 if (ret)
437 goto error_xenbus;
39763480
DM
438
439 for (i = 0; i < XEN_9PFS_NUM_RINGS; i++) {
71ebd719
SS
440 char str[16];
441
442 BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9);
316a1bef 443 sprintf(str, "ring-ref%d", i);
71ebd719
SS
444 ret = xenbus_printf(xbt, dev->nodename, str, "%d",
445 priv->rings[i].ref);
446 if (ret)
447 goto error_xenbus;
448
316a1bef 449 sprintf(str, "event-channel-%d", i);
71ebd719
SS
450 ret = xenbus_printf(xbt, dev->nodename, str, "%u",
451 priv->rings[i].evtchn);
452 if (ret)
453 goto error_xenbus;
454 }
455 priv->tag = xenbus_read(xbt, dev->nodename, "tag", NULL);
14e3995e
WY
456 if (IS_ERR(priv->tag)) {
457 ret = PTR_ERR(priv->tag);
71ebd719
SS
458 goto error_xenbus;
459 }
460 ret = xenbus_transaction_end(xbt, 0);
461 if (ret) {
462 if (ret == -EAGAIN)
463 goto again;
464 xenbus_dev_fatal(dev, ret, "completing transaction");
465 goto error;
466 }
467
868eb122 468 return 0;
71ebd719
SS
469
470 error_xenbus:
471 xenbus_transaction_end(xbt, 1);
472 xenbus_dev_fatal(dev, ret, "writing xenstore");
473 error:
71ebd719
SS
474 xen_9pfs_front_free(priv);
475 return ret;
868eb122
SS
476}
477
c15fe55d
JG
478static int xen_9pfs_front_probe(struct xenbus_device *dev,
479 const struct xenbus_device_id *id)
480{
481 struct xen_9pfs_front_priv *priv = NULL;
482
483 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
484 if (!priv)
485 return -ENOMEM;
486
487 priv->dev = dev;
488 dev_set_drvdata(&dev->dev, priv);
489
490 write_lock(&xen_9pfs_lock);
491 list_add_tail(&priv->list, &xen_9pfs_devs);
492 write_unlock(&xen_9pfs_lock);
493
494 return 0;
495}
496
868eb122
SS
497static int xen_9pfs_front_resume(struct xenbus_device *dev)
498{
680a2846 499 dev_warn(&dev->dev, "suspend/resume unsupported\n");
868eb122
SS
500 return 0;
501}
502
503static void xen_9pfs_front_changed(struct xenbus_device *dev,
504 enum xenbus_state backend_state)
505{
71ebd719
SS
506 switch (backend_state) {
507 case XenbusStateReconfiguring:
508 case XenbusStateReconfigured:
509 case XenbusStateInitialising:
510 case XenbusStateInitialised:
511 case XenbusStateUnknown:
512 break;
513
514 case XenbusStateInitWait:
c15fe55d
JG
515 if (!xen_9pfs_front_init(dev))
516 xenbus_switch_state(dev, XenbusStateInitialised);
71ebd719
SS
517 break;
518
519 case XenbusStateConnected:
520 xenbus_switch_state(dev, XenbusStateConnected);
521 break;
522
523 case XenbusStateClosed:
524 if (dev->state == XenbusStateClosed)
525 break;
df561f66 526 fallthrough; /* Missed the backend's CLOSING state */
71ebd719
SS
527 case XenbusStateClosing:
528 xenbus_frontend_closed(dev);
529 break;
530 }
868eb122
SS
531}
532
533static struct xenbus_driver xen_9pfs_front_driver = {
534 .ids = xen_9pfs_front_ids,
535 .probe = xen_9pfs_front_probe,
536 .remove = xen_9pfs_front_remove,
537 .resume = xen_9pfs_front_resume,
538 .otherend_changed = xen_9pfs_front_changed,
539};
540
0664c63a 541static int __init p9_trans_xen_init(void)
868eb122 542{
80a316ff
Y
543 int rc;
544
868eb122
SS
545 if (!xen_domain())
546 return -ENODEV;
547
548 pr_info("Initialising Xen transport for 9pfs\n");
549
550 v9fs_register_trans(&p9_xen_trans);
80a316ff
Y
551 rc = xenbus_register_frontend(&xen_9pfs_front_driver);
552 if (rc)
553 v9fs_unregister_trans(&p9_xen_trans);
554
555 return rc;
868eb122
SS
556}
557module_init(p9_trans_xen_init);
4cd82a5b 558MODULE_ALIAS_9P("xen");
868eb122 559
0664c63a 560static void __exit p9_trans_xen_exit(void)
868eb122
SS
561{
562 v9fs_unregister_trans(&p9_xen_trans);
563 return xenbus_unregister_driver(&xen_9pfs_front_driver);
564}
565module_exit(p9_trans_xen_exit);
d542296a 566
99aa673e 567MODULE_ALIAS("xen:9pfs");
d542296a
SH
568MODULE_AUTHOR("Stefano Stabellini <stefano@aporeto.com>");
569MODULE_DESCRIPTION("Xen Transport for 9P");
570MODULE_LICENSE("GPL");