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