xref: /linux/net/9p/trans_xen.c (revision 0678df8271820bcf8fb4f877129f05d68a237de4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/fs/9p/trans_xen
4  *
5  * Xen transport layer.
6  *
7  * Copyright (C) 2017 by Stefano Stabellini <stefano@aporeto.com>
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>
17 #include <linux/spinlock.h>
18 #include <net/9p/9p.h>
19 #include <net/9p/client.h>
20 #include <net/9p/transport.h>
21 
22 #define XEN_9PFS_NUM_RINGS 2
23 #define XEN_9PFS_RING_ORDER 9
24 #define XEN_9PFS_RING_SIZE(ring)  XEN_FLEX_RING_SIZE(ring->intf->ring_order)
25 
26 struct 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 */
35 struct 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 */
51 struct xen_9pfs_front_priv {
52 	struct list_head list;
53 	struct xenbus_device *dev;
54 	char *tag;
55 	struct p9_client *client;
56 
57 	struct xen_9pfs_dataring *rings;
58 };
59 
60 static LIST_HEAD(xen_9pfs_devs);
61 static DEFINE_RWLOCK(xen_9pfs_lock);
62 
63 /* We don't currently allow canceling of requests */
64 static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req)
65 {
66 	return 1;
67 }
68 
69 static int p9_xen_create(struct p9_client *client, const char *addr, char *args)
70 {
71 	struct xen_9pfs_front_priv *priv;
72 
73 	if (addr == NULL)
74 		return -EINVAL;
75 
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;
86 }
87 
88 static void p9_xen_close(struct p9_client *client)
89 {
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 
103 static 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 
111 	return XEN_9PFS_RING_SIZE(ring) -
112 		xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) >= size;
113 }
114 
115 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req)
116 {
117 	struct xen_9pfs_front_priv *priv;
118 	RING_IDX cons, prod, masked_cons, masked_prod;
119 	unsigned long flags;
120 	u32 size = p9_req->tc.size;
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);
130 	if (list_entry_is_head(priv, &xen_9pfs_devs, list))
131 		return -EINVAL;
132 
133 	num = p9_req->tc.tag % XEN_9PFS_NUM_RINGS;
134 	ring = &priv->rings[num];
135 
136 again:
137 	while (wait_event_killable(ring->wq,
138 				   p9_xen_write_todo(ring, size)) != 0)
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 
146 	if (XEN_9PFS_RING_SIZE(ring) -
147 	    xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) < size) {
148 		spin_unlock_irqrestore(&ring->lock, flags);
149 		goto again;
150 	}
151 
152 	masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring));
153 	masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
154 
155 	xen_9pfs_write_packet(ring->data.out, p9_req->tc.sdata, size,
156 			      &masked_prod, masked_cons,
157 			      XEN_9PFS_RING_SIZE(ring));
158 
159 	WRITE_ONCE(p9_req->status, REQ_STATUS_SENT);
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);
165 	p9_req_put(client, p9_req);
166 
167 	return 0;
168 }
169 
170 static void p9_xen_response(struct work_struct *work)
171 {
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 
187 		if (xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) <
188 		    sizeof(h)) {
189 			notify_remote_via_irq(ring->irq);
190 			return;
191 		}
192 
193 		masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring));
194 		masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
195 
196 		/* First, read just the header */
197 		xen_9pfs_read_packet(&h, ring->data.in, sizeof(h),
198 				     masked_prod, &masked_cons,
199 				     XEN_9PFS_RING_SIZE(ring));
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 
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);
214 			WRITE_ONCE(req->status, REQ_STATUS_ERROR);
215 			goto recv_error;
216 		}
217 
218 		req->rc.size = h.size;
219 		req->rc.id = h.id;
220 		req->rc.tag = h.tag;
221 		req->rc.offset = 0;
222 
223 		masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring));
224 		/* Then, read the whole packet (including the header) */
225 		xen_9pfs_read_packet(req->rc.sdata, ring->data.in, h.size,
226 				     masked_prod, &masked_cons,
227 				     XEN_9PFS_RING_SIZE(ring));
228 
229 recv_error:
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 	}
239 }
240 
241 static 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 
256 static struct p9_trans_module p9_xen_trans = {
257 	.name = "xen",
258 	.maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT - 2),
259 	.pooled_rbuffers = false,
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 
268 static const struct xenbus_device_id xen_9pfs_front_ids[] = {
269 	{ "9pfs" },
270 	{ "" }
271 };
272 
273 static 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 
281 	for (i = 0; i < XEN_9PFS_NUM_RINGS; i++) {
282 		struct xen_9pfs_dataring *ring = &priv->rings[i];
283 
284 		cancel_work_sync(&ring->work);
285 
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) {
291 			for (j = 0;
292 			     j < (1 << priv->rings[i].intf->ring_order);
293 			     j++) {
294 				grant_ref_t ref;
295 
296 				ref = priv->rings[i].intf->ref[j];
297 				gnttab_end_foreign_access(ref, NULL);
298 			}
299 			free_pages_exact(priv->rings[i].data.in,
300 				   1UL << (priv->rings[i].intf->ring_order +
301 					   XEN_PAGE_SHIFT));
302 		}
303 		gnttab_end_foreign_access(priv->rings[i].ref, NULL);
304 		free_page((unsigned long)priv->rings[i].intf);
305 	}
306 	kfree(priv->rings);
307 	kfree(priv->tag);
308 	kfree(priv);
309 }
310 
311 static void xen_9pfs_front_remove(struct xenbus_device *dev)
312 {
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);
317 }
318 
319 static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev,
320 					 struct xen_9pfs_dataring *ring,
321 					 unsigned int order)
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;
339 	bytes = alloc_pages_exact(1UL << (order + XEN_PAGE_SHIFT),
340 				  GFP_KERNEL | __GFP_ZERO);
341 	if (!bytes) {
342 		ret = -ENOMEM;
343 		goto out;
344 	}
345 	for (; i < (1 << order); i++) {
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 	}
352 	ring->intf->ring_order = order;
353 	ring->data.in = bytes;
354 	ring->data.out = bytes + XEN_FLEX_RING_SIZE(order);
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;
367 out:
368 	if (bytes) {
369 		for (i--; i >= 0; i--)
370 			gnttab_end_foreign_access(ring->intf->ref[i], NULL);
371 		free_pages_exact(bytes, 1UL << (order + XEN_PAGE_SHIFT));
372 	}
373 	gnttab_end_foreign_access(ring->ref, NULL);
374 	free_page((unsigned long)ring->intf);
375 	return ret;
376 }
377 
378 static int xen_9pfs_front_init(struct xenbus_device *dev)
379 {
380 	int ret, i;
381 	struct xenbus_transaction xbt;
382 	struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev);
383 	char *versions, *v;
384 	unsigned int max_rings, max_ring_order, len = 0;
385 
386 	versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
387 	if (IS_ERR(versions))
388 		return PTR_ERR(versions);
389 	for (v = versions; *v; v++) {
390 		if (simple_strtoul(v, &v, 10) == 1) {
391 			v = NULL;
392 			break;
393 		}
394 	}
395 	if (v) {
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);
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;
409 
410 	priv->rings = kcalloc(XEN_9PFS_NUM_RINGS, sizeof(*priv->rings),
411 			      GFP_KERNEL);
412 	if (!priv->rings) {
413 		kfree(priv);
414 		return -ENOMEM;
415 	}
416 
417 	for (i = 0; i < XEN_9PFS_NUM_RINGS; i++) {
418 		priv->rings[i].priv = priv;
419 		ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i],
420 						    max_ring_order);
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",
435 			    XEN_9PFS_NUM_RINGS);
436 	if (ret)
437 		goto error_xenbus;
438 
439 	for (i = 0; i < XEN_9PFS_NUM_RINGS; i++) {
440 		char str[16];
441 
442 		BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9);
443 		sprintf(str, "ring-ref%d", i);
444 		ret = xenbus_printf(xbt, dev->nodename, str, "%d",
445 				    priv->rings[i].ref);
446 		if (ret)
447 			goto error_xenbus;
448 
449 		sprintf(str, "event-channel-%d", i);
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);
456 	if (IS_ERR(priv->tag)) {
457 		ret = PTR_ERR(priv->tag);
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 
468 	return 0;
469 
470  error_xenbus:
471 	xenbus_transaction_end(xbt, 1);
472 	xenbus_dev_fatal(dev, ret, "writing xenstore");
473  error:
474 	xen_9pfs_front_free(priv);
475 	return ret;
476 }
477 
478 static 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 
497 static int xen_9pfs_front_resume(struct xenbus_device *dev)
498 {
499 	dev_warn(&dev->dev, "suspend/resume unsupported\n");
500 	return 0;
501 }
502 
503 static void xen_9pfs_front_changed(struct xenbus_device *dev,
504 				   enum xenbus_state backend_state)
505 {
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:
515 		if (!xen_9pfs_front_init(dev))
516 			xenbus_switch_state(dev, XenbusStateInitialised);
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;
526 		fallthrough;	/* Missed the backend's CLOSING state */
527 	case XenbusStateClosing:
528 		xenbus_frontend_closed(dev);
529 		break;
530 	}
531 }
532 
533 static 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 
541 static int __init p9_trans_xen_init(void)
542 {
543 	int rc;
544 
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);
551 	rc = xenbus_register_frontend(&xen_9pfs_front_driver);
552 	if (rc)
553 		v9fs_unregister_trans(&p9_xen_trans);
554 
555 	return rc;
556 }
557 module_init(p9_trans_xen_init);
558 MODULE_ALIAS_9P("xen");
559 
560 static void __exit p9_trans_xen_exit(void)
561 {
562 	v9fs_unregister_trans(&p9_xen_trans);
563 	return xenbus_unregister_driver(&xen_9pfs_front_driver);
564 }
565 module_exit(p9_trans_xen_exit);
566 
567 MODULE_ALIAS("xen:9pfs");
568 MODULE_AUTHOR("Stefano Stabellini <stefano@aporeto.com>");
569 MODULE_DESCRIPTION("Xen Transport for 9P");
570 MODULE_LICENSE("GPL");
571