bpf_arena_import_memfd() covers the host side of a host/guest shared arena: the host imports a memfd and hands the same file to the VMM. The guest has no such file. The host memory can be exposed to the guest as a virtio-pmem or NVDIMM device, which the guest turns into a device-dax device, /dev/daxX.Y. Add a sleepable kfunc for syscall programs that imports the device-dax pages mapped at an address of the calling task: int bpf_arena_import_dax(void *map, void __arena *addr, u32 page_cnt, u64 src); The pages are pinned with pin_user_pages(FOLL_LONGTERM) and mapped like memfd imports. Only a device-dax mapping is accepted, checked under mmap_lock together with the pin. A pin doesn't keep the dev_pagemap alive, so the import holds a reference on it until the pages are released. Unbinding the device blocks until then: the dax core doesn't notify its users, so the pages can't be revoked. To not race with an unbind before it takes that reference, the import pins the pages under the dax SRCU and checks that the device is alive, like the device-dax fault path. This needs the dax core built in, otherwise the kfunc returns -EOPNOTSUPP. Like bpf_arena_import_memfd(), the kfunc fails in async callbacks and is marked KF_PERFMON. This works with QEMU and Firecracker today, without changes to the VMM. It may be deprecated once a paravirt transport, such as pvsched, can supply guest pages through bpf_arena_map_ext_pages(). Signed-off-by: Andrea Righi --- kernel/bpf/arena.c | 139 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 4c3a28d33546a..5faac67e54095 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -7,7 +7,10 @@ #include #include #include +#include #include +#include +#include #include "linux/filter.h" #include #include @@ -1583,6 +1586,102 @@ static int arena_import_memfd(struct bpf_arena *arena, long uaddr, u32 page_cnt, return arena_import_file(arena, uaddr, page_cnt, fd_file(f), off); } +/* + * A device-dax file maps the inode of its dax_device, which lives on the dax + * pseudo filesystem, whatever CONFIG_FS_DAX says (S_DAX is 0 without it). + */ +static bool vma_is_devdax(struct vm_area_struct *vma) +{ + struct file *file = vma->vm_file; + + return file && file->f_mapping->host->i_sb->s_magic == DAXFS_MAGIC; +} + +static void arena_release_dax_pages(struct page **pages, u32 page_cnt, void *priv) +{ + unpin_user_pages_dirty_lock(pages, page_cnt, true); + /* After the pins: the memmap of the pages goes away with it */ + put_dev_pagemap(priv); + kvfree(pages); +} + +/* + * Import the device-dax pages mapped at [src, src + page_cnt * PAGE_SIZE) in + * the calling task. Only device-dax is accepted: other drivers hand out + * MEMORY_DEVICE_GENERIC pages too (e.g. Xen grants). + */ +static int arena_import_dax(struct bpf_arena *arena, long uaddr, u32 page_cnt, u64 src) +{ + struct mm_struct *mm = current->mm; + struct dev_pagemap *pgmap = NULL; + struct vm_area_struct *vma; + long i, pinned = 0; + struct page **pages; + int ret = -EINVAL; + int id; + + /* The dax SRCU, below, can't be reached from here if the dax core is a module */ + if (!IS_BUILTIN(CONFIG_DAX)) + return -EOPNOTSUPP; + /* Kernel code may run the program without an address space */ + src = untagged_addr(src); + if (!mm || (src & ~PAGE_MASK)) + return -EINVAL; + + pages = kvmalloc_array(page_cnt, sizeof(*pages), GFP_KERNEL_ACCOUNT); + if (!pages) + return -ENOMEM; + + mmap_read_lock(mm); + /* + * A pin doesn't keep the memmap of device pages: an unbind can free it + * until a dev_pagemap reference is held. It kills the dax device first, + * then waits for the readers of the dax SRCU: the memmap stays if the + * device is alive here, as in the fault path. Pages that are mapped + * already are pinned without a fault, so check it here too. + */ + id = dax_read_lock(); + vma = vma_lookup(mm, src); + if (!vma || !vma_is_devdax(vma) || + ((unsigned long)page_cnt << PAGE_SHIFT) > vma->vm_end - src) + goto out_unlock; + if (!dax_alive(inode_dax(vma->vm_file->f_mapping->host))) { + ret = -ENXIO; + goto out_unlock; + } + pinned = pin_user_pages(src, page_cnt, FOLL_WRITE | FOLL_LONGTERM, pages); + if (pinned < 0) + ret = pinned; + else if (pinned == page_cnt && is_zone_device_page(pages[0])) + pgmap = get_dev_pagemap(page_to_pfn(pages[0])); + if (pgmap && pgmap->type == MEMORY_DEVICE_GENERIC) { + ret = 0; + /* All of the pages are on the device of the mapping */ + for (i = 1; i < pinned; i++) + if (!is_zone_device_page(pages[i]) || page_pgmap(pages[i]) != pgmap) + ret = -EINVAL; + } + /* Still under the lock: there may be no dev_pagemap reference */ + if (ret && pinned > 0) { + unpin_user_pages(pages, pinned); + pinned = 0; + } +out_unlock: + dax_read_unlock(id); + mmap_read_unlock(mm); + + if (!ret) + ret = arena_map_ext_pages(arena, uaddr, pages, page_cnt, arena_release_dax_pages, + pgmap); + if (ret) { + if (pinned > 0) + unpin_user_pages(pages, pinned); + put_dev_pagemap(pgmap); + kvfree(pages); + } + return ret; +} + static void arena_free_worker(struct work_struct *work) { struct bpf_arena *arena = container_of(work, struct bpf_arena, free_work); @@ -1810,6 +1909,45 @@ __bpf_kfunc int bpf_arena_import_memfd(void *p__map, void *addr__ign, u32 page_c return -EINVAL; return arena_import_memfd(arena, (long)addr__ign, page_cnt, fd, off); } + +/** + * bpf_arena_import_dax - map device-dax pages of the calling task into an arena + * @p__map: the arena + * @addr__ign: page-aligned arena address where the pages are mapped + * @page_cnt: number of pages to import + * @src: page-aligned address of a writable device-dax mapping in the calling task + * + * Pin the pages backing @src and map them at @addr__ign, like + * bpf_arena_import_memfd() does with the pages of a file. The whole range must + * be in one mapping of a device-dax device, e.g. the /dev/daxX.Y that a guest + * gets from a virtio-pmem or NVDIMM device backed by host memory, which the + * host imports by file. User space keeps accessing the pages through @src. + * + * Imported pages keep the device in use: unbinding or removing it blocks until + * the arena releases them, for as long as a map fd or a bpffs pin keeps the + * arena alive. + * + * BPF programs have no machine check recovery: on a real NVDIMM, the supplier + * has to keep bad pages out of the arena. + * + * The kfunc uses the address space of the current task, so it fails in async + * callbacks, which may run in another one. + * + * Return: 0 on success, -EPERM in an async callback, -EOPNOTSUPP if the dax core + * is a module, negative error code otherwise. + */ +__bpf_kfunc int bpf_arena_import_dax(void *p__map, void *addr__ign, u32 page_cnt, u64 src) +{ + struct bpf_map *map = p__map; + struct bpf_arena *arena = container_of(map, struct bpf_arena, map); + + /* Only the task that runs the program can import pages */ + if (bpf_in_async_cb()) + return -EPERM; + if (map->map_type != BPF_MAP_TYPE_ARENA || !page_cnt || page_cnt > map->max_entries) + return -EINVAL; + return arena_import_dax(arena, (long)addr__ign, page_cnt, src); +} __bpf_kfunc_end_defs(); BTF_KFUNCS_START(arena_kfuncs) @@ -1826,6 +1964,7 @@ static const struct btf_kfunc_id_set common_kfunc_set = { BTF_KFUNCS_START(arena_syscall_kfuncs) BTF_ID_FLAGS(func, bpf_arena_import_memfd, KF_ARENA_ARG2 | KF_SLEEPABLE | KF_PERFMON) +BTF_ID_FLAGS(func, bpf_arena_import_dax, KF_ARENA_ARG2 | KF_SLEEPABLE | KF_PERFMON) BTF_KFUNCS_END(arena_syscall_kfuncs) static const struct btf_kfunc_id_set syscall_kfunc_set = { -- 2.56.1