1 // SPDX-License-Identifier: GPL-2.0-only
3 * The Virtio 9p transport driver
5 * This is a block based transport driver based on the lguest block driver
8 * Copyright (C) 2007, 2008 Eric Van Hensbergen, IBM Corporation
10 * Based on virtio console driver
11 * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/module.h>
18 #include <linux/net.h>
19 #include <linux/ipv6.h>
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
23 #include <linux/uaccess.h>
24 #include <linux/inet.h>
25 #include <linux/file.h>
26 #include <linux/highmem.h>
27 #include <linux/slab.h>
28 #include <net/9p/9p.h>
29 #include <linux/parser.h>
30 #include <net/9p/client.h>
31 #include <net/9p/transport.h>
32 #include <linux/scatterlist.h>
33 #include <linux/swap.h>
34 #include <linux/virtio.h>
35 #include <linux/virtio_9p.h>
36 #include "trans_common.h"
38 #define VIRTQUEUE_NUM 128
40 /* a single mutex to manage channel initialization and attachment */
41 static DEFINE_MUTEX(virtio_9p_lock);
42 static DECLARE_WAIT_QUEUE_HEAD(vp_wq);
43 static atomic_t vp_pinned = ATOMIC_INIT(0);
46 * struct virtio_chan - per-instance transport information
47 * @inuse: whether the channel is in use
48 * @lock: protects multiple elements within this structure
49 * @client: client instance
50 * @vdev: virtio dev associated with this channel
51 * @vq: virtio queue associated with this channel
52 * @ring_bufs_avail: flag to indicate there is some available in the ring buf
53 * @vc_wq: wait queue for waiting for thing to be added to ring buf
54 * @p9_max_pages: maximum number of pinned pages
55 * @sg: scatter gather list which is used to pack a request (protected?)
56 * @chan_list: linked list of channels
58 * We keep all per-channel information in a structure.
59 * This structure is allocated within the devices dev->mem space.
60 * A pointer to the structure will get put in the transport private.
69 struct p9_client *client;
70 struct virtio_device *vdev;
73 wait_queue_head_t *vc_wq;
74 /* This is global limit. Since we don't have a global structure,
75 * will be placing it in each channel.
77 unsigned long p9_max_pages;
78 /* Scatterlist: can be too big for stack. */
79 struct scatterlist sg[VIRTQUEUE_NUM];
81 * @tag: name to identify a mount null terminated
85 struct list_head chan_list;
88 static struct list_head virtio_chan_list;
90 /* How many bytes left in this page. */
91 static unsigned int rest_of_page(void *data)
93 return PAGE_SIZE - offset_in_page(data);
97 * p9_virtio_close - reclaim resources of a channel
98 * @client: client instance
100 * This reclaims a channel by freeing its resources and
101 * resetting its inuse flag.
105 static void p9_virtio_close(struct p9_client *client)
107 struct virtio_chan *chan = client->trans;
109 mutex_lock(&virtio_9p_lock);
112 mutex_unlock(&virtio_9p_lock);
116 * req_done - callback which signals activity from the server
117 * @vq: virtio queue activity was received on
119 * This notifies us that the server has triggered some activity
120 * on the virtio channel - most likely a response to request we
121 * sent. Figure out which requests now have responses and wake up
124 * Bugs: could do with some additional sanity checking, but appears to work.
128 static void req_done(struct virtqueue *vq)
130 struct virtio_chan *chan = vq->vdev->priv;
132 struct p9_req_t *req;
133 bool need_wakeup = false;
136 p9_debug(P9_DEBUG_TRANS, ": request done\n");
138 spin_lock_irqsave(&chan->lock, flags);
139 while ((req = virtqueue_get_buf(chan->vq, &len)) != NULL) {
140 if (!chan->ring_bufs_avail) {
141 chan->ring_bufs_avail = 1;
147 p9_client_cb(chan->client, req, REQ_STATUS_RCVD);
150 spin_unlock_irqrestore(&chan->lock, flags);
151 /* Wakeup if anyone waiting for VirtIO ring space. */
153 wake_up(chan->vc_wq);
157 * pack_sg_list - pack a scatter gather list from a linear buffer
158 * @sg: scatter/gather list to pack into
159 * @start: which segment of the sg_list to start at
160 * @limit: maximum segment to pack data to
161 * @data: data to pack into scatter/gather list
162 * @count: amount of data to pack into the scatter/gather list
164 * sg_lists have multiple segments of various sizes. This will pack
165 * arbitrary data into an existing scatter gather list, segmenting the
166 * data as necessary within constraints.
170 static int pack_sg_list(struct scatterlist *sg, int start,
171 int limit, char *data, int count)
177 s = rest_of_page(data);
180 BUG_ON(index >= limit);
181 /* Make sure we don't terminate early. */
182 sg_unmark_end(&sg[index]);
183 sg_set_buf(&sg[index++], data, s);
188 sg_mark_end(&sg[index - 1]);
192 /* We don't currently allow canceling of virtio requests */
193 static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
198 /* Reply won't come, so drop req ref */
199 static int p9_virtio_cancelled(struct p9_client *client, struct p9_req_t *req)
201 p9_req_put(client, req);
206 * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer,
207 * this takes a list of pages.
208 * @sg: scatter/gather list to pack into
209 * @start: which segment of the sg_list to start at
210 * @limit: maximum number of pages in sg list.
211 * @pdata: a list of pages to add into sg.
212 * @nr_pages: number of pages to pack into the scatter/gather list
213 * @offs: amount of data in the beginning of first page _not_ to pack
214 * @count: amount of data to pack into the scatter/gather list
217 pack_sg_list_p(struct scatterlist *sg, int start, int limit,
218 struct page **pdata, int nr_pages, size_t offs, int count)
224 BUG_ON(nr_pages > (limit - start));
226 * if the first page doesn't start at
227 * page boundary find the offset
230 s = PAGE_SIZE - data_off;
233 BUG_ON(index >= limit);
234 /* Make sure we don't terminate early. */
235 sg_unmark_end(&sg[index]);
236 sg_set_page(&sg[index++], pdata[i++], s, data_off);
243 sg_mark_end(&sg[index - 1]);
244 return index - start;
248 * p9_virtio_request - issue a request
249 * @client: client instance issuing the request
250 * @req: request to be issued
255 p9_virtio_request(struct p9_client *client, struct p9_req_t *req)
258 int in, out, out_sgs, in_sgs;
260 struct virtio_chan *chan = client->trans;
261 struct scatterlist *sgs[2];
263 p9_debug(P9_DEBUG_TRANS, "9p debug: virtio request\n");
265 WRITE_ONCE(req->status, REQ_STATUS_SENT);
267 spin_lock_irqsave(&chan->lock, flags);
269 out_sgs = in_sgs = 0;
270 /* Handle out VirtIO ring buffers */
271 out = pack_sg_list(chan->sg, 0,
272 VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
274 sgs[out_sgs++] = chan->sg;
276 in = pack_sg_list(chan->sg, out,
277 VIRTQUEUE_NUM, req->rc.sdata, req->rc.capacity);
279 sgs[out_sgs + in_sgs++] = chan->sg + out;
281 err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
284 if (err == -ENOSPC) {
285 chan->ring_bufs_avail = 0;
286 spin_unlock_irqrestore(&chan->lock, flags);
287 err = wait_event_killable(*chan->vc_wq,
288 chan->ring_bufs_avail);
289 if (err == -ERESTARTSYS)
292 p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
295 spin_unlock_irqrestore(&chan->lock, flags);
296 p9_debug(P9_DEBUG_TRANS,
297 "virtio rpc add_sgs returned failure\n");
301 virtqueue_kick(chan->vq);
302 spin_unlock_irqrestore(&chan->lock, flags);
304 p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
308 static int p9_get_mapped_pages(struct virtio_chan *chan,
309 struct page ***pages,
310 struct iov_iter *data,
318 if (!iov_iter_count(data))
321 if (!iov_iter_is_kvec(data)) {
324 * We allow only p9_max_pages pinned. We wait for the
325 * Other zc request to finish here
327 if (atomic_read(&vp_pinned) >= chan->p9_max_pages) {
328 err = wait_event_killable(vp_wq,
329 (atomic_read(&vp_pinned) < chan->p9_max_pages));
330 if (err == -ERESTARTSYS)
333 n = iov_iter_get_pages_alloc2(data, pages, count, offs);
337 nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
338 atomic_add(nr_pages, &vp_pinned);
341 /* kernel buffer, no need to pin pages */
346 /* we'd already checked that it's non-empty */
348 len = iov_iter_single_seg_count(data);
350 p = data->kvec->iov_base + data->iov_offset;
353 iov_iter_advance(data, 0);
358 nr_pages = DIV_ROUND_UP((unsigned long)p + len, PAGE_SIZE) -
359 (unsigned long)p / PAGE_SIZE;
361 *pages = kmalloc_array(nr_pages, sizeof(struct page *),
367 p -= (*offs = offset_in_page(p));
368 for (index = 0; index < nr_pages; index++) {
369 if (is_vmalloc_addr(p))
370 (*pages)[index] = vmalloc_to_page(p);
372 (*pages)[index] = kmap_to_page(p);
375 iov_iter_advance(data, len);
380 static void handle_rerror(struct p9_req_t *req, int in_hdr_len,
381 size_t offs, struct page **pages)
384 void *to = req->rc.sdata + in_hdr_len;
386 // Fits entirely into the static data? Nothing to do.
387 if (req->rc.size < in_hdr_len || !pages)
390 // Really long error message? Tough, truncate the reply. Might get
391 // rejected (we can't be arsed to adjust the size encoded in header,
392 // or string size for that matter), but it wouldn't be anything valid
394 if (unlikely(req->rc.size > P9_ZC_HDR_SZ))
395 req->rc.size = P9_ZC_HDR_SZ;
397 // data won't span more than two pages
398 size = req->rc.size - in_hdr_len;
399 n = PAGE_SIZE - offs;
401 memcpy_from_page(to, *pages++, offs, n);
406 memcpy_from_page(to, *pages, offs, size);
410 * p9_virtio_zc_request - issue a zero copy request
411 * @client: client instance issuing the request
412 * @req: request to be issued
413 * @uidata: user buffer that should be used for zero copy read
414 * @uodata: user buffer that should be used for zero copy write
415 * @inlen: read buffer size
416 * @outlen: write buffer size
417 * @in_hdr_len: reader header size, This is the size of response protocol data
421 p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
422 struct iov_iter *uidata, struct iov_iter *uodata,
423 int inlen, int outlen, int in_hdr_len)
425 int in, out, err, out_sgs, in_sgs;
427 int in_nr_pages = 0, out_nr_pages = 0;
428 struct page **in_pages = NULL, **out_pages = NULL;
429 struct virtio_chan *chan = client->trans;
430 struct scatterlist *sgs[4];
435 p9_debug(P9_DEBUG_TRANS, "virtio request\n");
439 int n = p9_get_mapped_pages(chan, &out_pages, uodata,
440 outlen, &offs, &need_drop);
445 out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
447 __le32 v = cpu_to_le32(n);
448 memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
451 /* The size field of the message must include the length of the
452 * header and the length of the data. We didn't actually know
453 * the length of the data until this point so add it in now.
455 sz = cpu_to_le32(req->tc.size + outlen);
456 memcpy(&req->tc.sdata[0], &sz, sizeof(sz));
458 int n = p9_get_mapped_pages(chan, &in_pages, uidata,
459 inlen, &offs, &need_drop);
464 in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
466 __le32 v = cpu_to_le32(n);
467 memcpy(&req->tc.sdata[req->tc.size - 4], &v, 4);
471 WRITE_ONCE(req->status, REQ_STATUS_SENT);
473 spin_lock_irqsave(&chan->lock, flags);
475 out_sgs = in_sgs = 0;
478 out = pack_sg_list(chan->sg, 0,
479 VIRTQUEUE_NUM, req->tc.sdata, req->tc.size);
482 sgs[out_sgs++] = chan->sg;
485 sgs[out_sgs++] = chan->sg + out;
486 out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
487 out_pages, out_nr_pages, offs, outlen);
491 * Take care of in data
492 * For example TREAD have 11.
493 * 11 is the read/write header = PDU Header(7) + IO Size (4).
494 * Arrange in such a way that server places header in the
495 * allocated memory and payload onto the user buffer.
497 in = pack_sg_list(chan->sg, out,
498 VIRTQUEUE_NUM, req->rc.sdata, in_hdr_len);
500 sgs[out_sgs + in_sgs++] = chan->sg + out;
503 sgs[out_sgs + in_sgs++] = chan->sg + out + in;
504 pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
505 in_pages, in_nr_pages, offs, inlen);
508 BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
509 err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req,
512 if (err == -ENOSPC) {
513 chan->ring_bufs_avail = 0;
514 spin_unlock_irqrestore(&chan->lock, flags);
515 err = wait_event_killable(*chan->vc_wq,
516 chan->ring_bufs_avail);
517 if (err == -ERESTARTSYS)
520 p9_debug(P9_DEBUG_TRANS, "Retry virtio request\n");
521 goto req_retry_pinned;
523 spin_unlock_irqrestore(&chan->lock, flags);
524 p9_debug(P9_DEBUG_TRANS,
525 "virtio rpc add_sgs returned failure\n");
530 virtqueue_kick(chan->vq);
531 spin_unlock_irqrestore(&chan->lock, flags);
533 p9_debug(P9_DEBUG_TRANS, "virtio request kicked\n");
534 err = wait_event_killable(req->wq,
535 READ_ONCE(req->status) >= REQ_STATUS_RCVD);
536 // RERROR needs reply (== error string) in static data
537 if (READ_ONCE(req->status) == REQ_STATUS_RCVD &&
538 unlikely(req->rc.sdata[4] == P9_RERROR))
539 handle_rerror(req, in_hdr_len, offs, in_pages);
542 * Non kernel buffers are pinned, unpin them
547 p9_release_pages(in_pages, in_nr_pages);
548 atomic_sub(in_nr_pages, &vp_pinned);
551 p9_release_pages(out_pages, out_nr_pages);
552 atomic_sub(out_nr_pages, &vp_pinned);
554 /* wakeup anybody waiting for slots to pin pages */
560 /* reply won't come */
561 p9_req_put(client, req);
566 static ssize_t p9_mount_tag_show(struct device *dev,
567 struct device_attribute *attr, char *buf)
569 struct virtio_chan *chan;
570 struct virtio_device *vdev;
573 vdev = dev_to_virtio(dev);
575 tag_len = strlen(chan->tag);
577 memcpy(buf, chan->tag, tag_len + 1);
582 static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
585 * p9_virtio_probe - probe for existence of 9P virtio channels
586 * @vdev: virtio device to probe
588 * This probes for existing virtio channels.
592 static int p9_virtio_probe(struct virtio_device *vdev)
597 struct virtio_chan *chan;
599 if (!vdev->config->get) {
600 dev_err(&vdev->dev, "%s failure: config access disabled\n",
605 chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL);
607 pr_err("Failed to allocate virtio 9P channel\n");
614 /* We expect one virtqueue, for requests. */
615 chan->vq = virtio_find_single_vq(vdev, req_done, "requests");
616 if (IS_ERR(chan->vq)) {
617 err = PTR_ERR(chan->vq);
620 chan->vq->vdev->priv = chan;
621 spin_lock_init(&chan->lock);
623 sg_init_table(chan->sg, VIRTQUEUE_NUM);
626 if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
627 virtio_cread(vdev, struct virtio_9p_config, tag_len, &tag_len);
632 tag = kzalloc(tag_len + 1, GFP_KERNEL);
638 virtio_cread_bytes(vdev, offsetof(struct virtio_9p_config, tag),
641 err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
645 chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
648 goto out_remove_file;
650 init_waitqueue_head(chan->vc_wq);
651 chan->ring_bufs_avail = 1;
652 /* Ceiling limit to avoid denial of service attacks */
653 chan->p9_max_pages = nr_free_buffer_pages()/4;
655 virtio_device_ready(vdev);
657 mutex_lock(&virtio_9p_lock);
658 list_add_tail(&chan->chan_list, &virtio_chan_list);
659 mutex_unlock(&virtio_9p_lock);
661 /* Let udev rules use the new mount_tag attribute. */
662 kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
667 sysfs_remove_file(&vdev->dev.kobj, &dev_attr_mount_tag.attr);
671 vdev->config->del_vqs(vdev);
680 * p9_virtio_create - allocate a new virtio channel
681 * @client: client instance invoking this transport
682 * @devname: string identifying the channel to connect to (unused)
683 * @args: args passed from sys_mount() for per-transport options (unused)
685 * This sets up a transport channel for 9p communication. Right now
686 * we only match the first available channel, but eventually we could look up
687 * alternate channels by matching devname versus a virtio_config entry.
688 * We use a simple reference count mechanism to ensure that only a single
689 * mount has a channel open at a time.
694 p9_virtio_create(struct p9_client *client, const char *devname, char *args)
696 struct virtio_chan *chan;
703 mutex_lock(&virtio_9p_lock);
704 list_for_each_entry(chan, &virtio_chan_list, chan_list) {
705 if (!strcmp(devname, chan->tag)) {
714 mutex_unlock(&virtio_9p_lock);
717 pr_err("no channels available for device %s\n", devname);
721 client->trans = (void *)chan;
722 client->status = Connected;
723 chan->client = client;
729 * p9_virtio_remove - clean up resources associated with a virtio device
730 * @vdev: virtio device to remove
734 static void p9_virtio_remove(struct virtio_device *vdev)
736 struct virtio_chan *chan = vdev->priv;
737 unsigned long warning_time;
739 mutex_lock(&virtio_9p_lock);
741 /* Remove self from list so we don't get new users. */
742 list_del(&chan->chan_list);
743 warning_time = jiffies;
745 /* Wait for existing users to close. */
746 while (chan->inuse) {
747 mutex_unlock(&virtio_9p_lock);
749 if (time_after(jiffies, warning_time + 10 * HZ)) {
750 dev_emerg(&vdev->dev,
751 "p9_virtio_remove: waiting for device in use.\n");
752 warning_time = jiffies;
754 mutex_lock(&virtio_9p_lock);
757 mutex_unlock(&virtio_9p_lock);
759 virtio_reset_device(vdev);
760 vdev->config->del_vqs(vdev);
762 sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
763 kobject_uevent(&(vdev->dev.kobj), KOBJ_CHANGE);
770 static struct virtio_device_id id_table[] = {
771 { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID },
775 static unsigned int features[] = {
779 /* The standard "struct lguest_driver": */
780 static struct virtio_driver p9_virtio_drv = {
781 .feature_table = features,
782 .feature_table_size = ARRAY_SIZE(features),
783 .driver.name = KBUILD_MODNAME,
784 .driver.owner = THIS_MODULE,
785 .id_table = id_table,
786 .probe = p9_virtio_probe,
787 .remove = p9_virtio_remove,
790 static struct p9_trans_module p9_virtio_trans = {
792 .create = p9_virtio_create,
793 .close = p9_virtio_close,
794 .request = p9_virtio_request,
795 .zc_request = p9_virtio_zc_request,
796 .cancel = p9_virtio_cancel,
797 .cancelled = p9_virtio_cancelled,
799 * We leave one entry for input and one entry for response
800 * headers. We also skip one more entry to accommodate, address
801 * that are not at page boundary, that can result in an extra
804 .maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
805 .pooled_rbuffers = false,
807 .owner = THIS_MODULE,
810 /* The standard init function */
811 static int __init p9_virtio_init(void)
815 INIT_LIST_HEAD(&virtio_chan_list);
817 v9fs_register_trans(&p9_virtio_trans);
818 rc = register_virtio_driver(&p9_virtio_drv);
820 v9fs_unregister_trans(&p9_virtio_trans);
825 static void __exit p9_virtio_cleanup(void)
827 unregister_virtio_driver(&p9_virtio_drv);
828 v9fs_unregister_trans(&p9_virtio_trans);
831 module_init(p9_virtio_init);
832 module_exit(p9_virtio_cleanup);
833 MODULE_ALIAS_9P("virtio");
835 MODULE_DEVICE_TABLE(virtio, id_table);
836 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
837 MODULE_DESCRIPTION("Virtio 9p Transport");
838 MODULE_LICENSE("GPL");