Skip to content

Commit 22f3787

Browse files
stefanhaRHMiklos Szeredi
authored andcommitted
virtiofs: set up virtio_fs dax_device
Setup a dax device. Use the shm capability to find the cache entry and map it. The DAX window is accessed by the fs/dax.c infrastructure and must have struct pages (at least on x86). Use devm_memremap_pages() to map the DAX window PCI BAR and allocate struct page. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com> Signed-off-by: Vivek Goyal <vgoyal@redhat.com> Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com> Signed-off-by: Liu Bo <bo.liu@linux.alibaba.com> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent f4fd4ae commit 22f3787

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

‎fs/fuse/virtio_fs.c‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
*/
66

77
#include <linux/fs.h>
8+
#include <linux/dax.h>
9+
#include <linux/pci.h>
10+
#include <linux/pfn_t.h>
811
#include <linux/module.h>
912
#include <linux/virtio.h>
1013
#include <linux/virtio_fs.h>
1114
#include <linux/delay.h>
1215
#include <linux/fs_context.h>
1316
#include <linux/highmem.h>
17+
#include <linux/uio.h>
1418
#include "fuse_i.h"
1519

1620
/* List of virtio-fs device instances and a lock for the list. Also provides
@@ -49,6 +53,12 @@ struct virtio_fs {
4953
struct virtio_fs_vq *vqs;
5054
unsigned int nvqs; /* number of virtqueues */
5155
unsigned int num_request_queues; /* number of request queues */
56+
struct dax_device *dax_dev;
57+
58+
/* DAX memory window where file contents are mapped */
59+
void *window_kaddr;
60+
phys_addr_t window_phys_addr;
61+
size_t window_len;
5262
};
5363

5464
struct virtio_fs_forget_req {
@@ -686,6 +696,130 @@ static void virtio_fs_cleanup_vqs(struct virtio_device *vdev,
686696
vdev->config->del_vqs(vdev);
687697
}
688698

699+
/* Map a window offset to a page frame number. The window offset will have
700+
* been produced by .iomap_begin(), which maps a file offset to a window
701+
* offset.
702+
*/
703+
static long virtio_fs_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
704+
long nr_pages, void **kaddr, pfn_t *pfn)
705+
{
706+
struct virtio_fs *fs = dax_get_private(dax_dev);
707+
phys_addr_t offset = PFN_PHYS(pgoff);
708+
size_t max_nr_pages = fs->window_len/PAGE_SIZE - pgoff;
709+
710+
if (kaddr)
711+
*kaddr = fs->window_kaddr + offset;
712+
if (pfn)
713+
*pfn = phys_to_pfn_t(fs->window_phys_addr + offset,
714+
PFN_DEV | PFN_MAP);
715+
return nr_pages > max_nr_pages ? max_nr_pages : nr_pages;
716+
}
717+
718+
static size_t virtio_fs_copy_from_iter(struct dax_device *dax_dev,
719+
pgoff_t pgoff, void *addr,
720+
size_t bytes, struct iov_iter *i)
721+
{
722+
return copy_from_iter(addr, bytes, i);
723+
}
724+
725+
static size_t virtio_fs_copy_to_iter(struct dax_device *dax_dev,
726+
pgoff_t pgoff, void *addr,
727+
size_t bytes, struct iov_iter *i)
728+
{
729+
return copy_to_iter(addr, bytes, i);
730+
}
731+
732+
static int virtio_fs_zero_page_range(struct dax_device *dax_dev,
733+
pgoff_t pgoff, size_t nr_pages)
734+
{
735+
long rc;
736+
void *kaddr;
737+
738+
rc = dax_direct_access(dax_dev, pgoff, nr_pages, &kaddr, NULL);
739+
if (rc < 0)
740+
return rc;
741+
memset(kaddr, 0, nr_pages << PAGE_SHIFT);
742+
dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
743+
return 0;
744+
}
745+
746+
static const struct dax_operations virtio_fs_dax_ops = {
747+
.direct_access = virtio_fs_direct_access,
748+
.copy_from_iter = virtio_fs_copy_from_iter,
749+
.copy_to_iter = virtio_fs_copy_to_iter,
750+
.zero_page_range = virtio_fs_zero_page_range,
751+
};
752+
753+
static void virtio_fs_cleanup_dax(void *data)
754+
{
755+
struct dax_device *dax_dev = data;
756+
757+
kill_dax(dax_dev);
758+
put_dax(dax_dev);
759+
}
760+
761+
static int virtio_fs_setup_dax(struct virtio_device *vdev, struct virtio_fs *fs)
762+
{
763+
struct virtio_shm_region cache_reg;
764+
struct dev_pagemap *pgmap;
765+
bool have_cache;
766+
767+
if (!IS_ENABLED(CONFIG_FUSE_DAX))
768+
return 0;
769+
770+
/* Get cache region */
771+
have_cache = virtio_get_shm_region(vdev, &cache_reg,
772+
(u8)VIRTIO_FS_SHMCAP_ID_CACHE);
773+
if (!have_cache) {
774+
dev_notice(&vdev->dev, "%s: No cache capability\n", __func__);
775+
return 0;
776+
}
777+
778+
if (!devm_request_mem_region(&vdev->dev, cache_reg.addr, cache_reg.len,
779+
dev_name(&vdev->dev))) {
780+
dev_warn(&vdev->dev, "could not reserve region addr=0x%llx len=0x%llx\n",
781+
cache_reg.addr, cache_reg.len);
782+
return -EBUSY;
783+
}
784+
785+
dev_notice(&vdev->dev, "Cache len: 0x%llx @ 0x%llx\n", cache_reg.len,
786+
cache_reg.addr);
787+
788+
pgmap = devm_kzalloc(&vdev->dev, sizeof(*pgmap), GFP_KERNEL);
789+
if (!pgmap)
790+
return -ENOMEM;
791+
792+
pgmap->type = MEMORY_DEVICE_FS_DAX;
793+
794+
/* Ideally we would directly use the PCI BAR resource but
795+
* devm_memremap_pages() wants its own copy in pgmap. So
796+
* initialize a struct resource from scratch (only the start
797+
* and end fields will be used).
798+
*/
799+
pgmap->res = (struct resource){
800+
.name = "virtio-fs dax window",
801+
.start = (phys_addr_t) cache_reg.addr,
802+
.end = (phys_addr_t) cache_reg.addr + cache_reg.len - 1,
803+
};
804+
805+
fs->window_kaddr = devm_memremap_pages(&vdev->dev, pgmap);
806+
if (IS_ERR(fs->window_kaddr))
807+
return PTR_ERR(fs->window_kaddr);
808+
809+
fs->window_phys_addr = (phys_addr_t) cache_reg.addr;
810+
fs->window_len = (phys_addr_t) cache_reg.len;
811+
812+
dev_dbg(&vdev->dev, "%s: window kaddr 0x%px phys_addr 0x%llx len 0x%llx\n",
813+
__func__, fs->window_kaddr, cache_reg.addr, cache_reg.len);
814+
815+
fs->dax_dev = alloc_dax(fs, NULL, &virtio_fs_dax_ops, 0);
816+
if (IS_ERR(fs->dax_dev))
817+
return PTR_ERR(fs->dax_dev);
818+
819+
return devm_add_action_or_reset(&vdev->dev, virtio_fs_cleanup_dax,
820+
fs->dax_dev);
821+
}
822+
689823
static int virtio_fs_probe(struct virtio_device *vdev)
690824
{
691825
struct virtio_fs *fs;
@@ -707,6 +841,10 @@ static int virtio_fs_probe(struct virtio_device *vdev)
707841

708842
/* TODO vq affinity */
709843

844+
ret = virtio_fs_setup_dax(vdev, fs);
845+
if (ret < 0)
846+
goto out_vqs;
847+
710848
/* Bring the device online in case the filesystem is mounted and
711849
* requests need to be sent before we return.
712850
*/

‎include/uapi/linux/virtio_fs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ struct virtio_fs_config {
1616
__le32 num_request_queues;
1717
} __attribute__((packed));
1818

19+
/* For the id field in virtio_pci_shm_cap */
20+
#define VIRTIO_FS_SHMCAP_ID_CACHE 0
21+
1922
#endif /* _UAPI_LINUX_VIRTIO_FS_H */

0 commit comments

Comments
 (0)