A paravirt transport, such as pvsched, can share guest memory with the host without a file: the guest registers ordinary pages with a device, and the host driver pins them. Such a driver needs to map the pages into an arena, and to revoke them synchronously when the guest detaches or the VM goes away, without waiting for BPF programs to free them. Add a kernel API around the imports of the arena: int bpf_arena_map_ext_pages(struct bpf_map *map, u64 addr, struct page **pages, u32 page_cnt, void (*release)(struct page **pages, u32 page_cnt, void *priv), void *priv); int bpf_arena_unmap_ext_pages(struct bpf_map *map, u64 addr, void *priv); The pages are mapped like the ones of a memfd and stay the caller's: the arena calls release once it no longer maps them, whether bpf_arena_unmap_ext_pages() revoked them, a BPF program freed them or the arena was destroyed. bpf_arena_unmap_ext_pages() returns after the release, so the caller can free the pages then. BPF accesses to revoked pages fault like the ones to free arena pages. Suggested-by: Vineeth Pillai Signed-off-by: Andrea Righi --- include/linux/bpf.h | 21 ++++++++++ kernel/bpf/arena.c | 96 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 0fef8e2f43cc0..28872d5c854e4 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -787,6 +787,27 @@ static inline void *bpf_arena_alloc_pages_sleepable(void *p__map, void *addr__ig } #endif +#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_MMU) && defined(CONFIG_64BIT) +int bpf_arena_map_ext_pages(struct bpf_map *map, u64 addr, struct page **pages, u32 page_cnt, + void (*release)(struct page **pages, u32 page_cnt, void *priv), + void *priv); +int bpf_arena_unmap_ext_pages(struct bpf_map *map, u64 addr, void *priv); +#else +static inline int bpf_arena_map_ext_pages(struct bpf_map *map, u64 addr, struct page **pages, + u32 page_cnt, + void (*release)(struct page **pages, u32 page_cnt, + void *priv), + void *priv) +{ + return -EOPNOTSUPP; +} + +static inline int bpf_arena_unmap_ext_pages(struct bpf_map *map, u64 addr, void *priv) +{ + return -EOPNOTSUPP; +} +#endif + extern const struct bpf_map_ops bpf_map_offload_ops; /* bpf_type_flag contains a set of flags that are applicable to the values of diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 019261fb190d4..4c3a28d33546a 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -1108,7 +1108,7 @@ static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt) * from vmalloc area and from all user space vma-s, so that nobody can * populate it while stale TLB entries or user ptes are still around. */ -static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt) +static int __arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt) { u64 full_uaddr = clear_lo32(arena->user_vm_start) + uaddr; long kaddr = bpf_arena_get_kern_vm_start(arena) + uaddr; @@ -1124,7 +1124,7 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt) int ret; /* An overlapping free must not release the range before this one is done */ - guard(mutex)(&arena->zap_mutex); + lockdep_assert_held(&arena->zap_mutex); ret = raw_res_spin_lock_irqsave(&arena->spinlock, flags); if (ret) @@ -1199,6 +1199,12 @@ static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt) return 0; } +static int arena_free_range(struct bpf_arena *arena, long uaddr, long page_cnt) +{ + guard(mutex)(&arena->zap_mutex); + return __arena_free_range(arena, uaddr, page_cnt); +} + static void arena_free_pages(struct bpf_arena *arena, long uaddr, long page_cnt, bool sleepable) { /* The first mmap() may be setting the user range right now */ @@ -1603,6 +1609,92 @@ static void arena_free_irq(struct irq_work *iw) schedule_work(&arena->free_work); } +/** + * bpf_arena_map_ext_pages - map pages that a kernel supplier keeps into an arena + * @map: the arena, which the caller holds a reference to + * @addr: page-aligned arena address where the pages are mapped, as BPF + * programs see it + * @pages: the pages, kept by the caller until @release + * @page_cnt: number of pages + * @release: called once the pages are no longer mapped + * @priv: passed to @release, identifies the pages for bpf_arena_unmap_ext_pages() + * + * Map @pages at @addr, which must be free, for a supplier such as a + * paravirt transport. They are imported like the pages of a memfd, see + * bpf_arena_import_memfd(), until bpf_arena_unmap_ext_pages() revokes them, + * bpf_arena_free_pages() frees any of them or the arena is destroyed. Then + * the arena unmaps them and calls @release, which may sleep: BPF programs + * no longer access them. They write the pages through a kernel mapping, + * which doesn't mark them dirty. + * + * @release runs under a mutex of the arena: it must not take a lock that is + * held across bpf_arena_unmap_ext_pages(). + * + * Context: process context, may sleep. + * Return: 0 on success, negative error code otherwise. + */ +int bpf_arena_map_ext_pages(struct bpf_map *map, u64 addr, struct page **pages, u32 page_cnt, + void (*release)(struct page **pages, u32 page_cnt, void *priv), + void *priv) +{ + struct bpf_arena *arena = container_of(map, struct bpf_arena, map); + + if (map->map_type != BPF_MAP_TYPE_ARENA || !page_cnt || !release) + return -EINVAL; + return arena_map_ext_pages(arena, addr, pages, page_cnt, release, priv); +} +EXPORT_SYMBOL_GPL(bpf_arena_map_ext_pages); + +/** + * bpf_arena_unmap_ext_pages - revoke pages mapped by bpf_arena_map_ext_pages() + * @map: the arena, which the caller holds a reference to + * @addr: the address the pages were mapped at + * @priv: the @priv they were mapped with + * + * Unmap the pages and call their release callback before returning, unless + * the arena released them already. Either way, BPF programs no longer access + * them when this returns: their accesses there fault like the ones to a free + * page of the arena. + * + * Context: process context, may sleep. + * Return: 0 if the pages were released here, -ENOENT if they were already. + */ +int bpf_arena_unmap_ext_pages(struct bpf_map *map, u64 addr, void *priv) +{ + struct bpf_arena *arena = container_of(map, struct bpf_arena, map); + struct mem_cgroup *new_memcg, *old_memcg; + struct arena_import *imp; + long pgoff, page_cnt = 0; + unsigned long flags; + + if (map->map_type != BPF_MAP_TYPE_ARENA || (addr & ~PAGE_MASK)) + return -EINVAL; + /* Pages were mapped: the user range is set */ + if (!READ_ONCE(arena->user_vm_start)) + return -ENOENT; + pgoff = compute_pgoff(arena, addr); + + /* A concurrent free releases the pages before this finds them gone */ + guard(mutex)(&arena->zap_mutex); + + /* No other rqspinlock is held, so this one can only time out */ + while (raw_res_spin_lock_irqsave(&arena->spinlock, flags)) + cond_resched(); + imp = arena_find_import(arena, pgoff); + if (imp && IMPORT_START(imp) == pgoff && imp->priv == priv) + page_cnt = imp->page_cnt; + raw_res_spin_unlock_irqrestore(&arena->spinlock, flags); + if (!page_cnt) + return -ENOENT; + + bpf_map_memcg_enter(&arena->map, &old_memcg, &new_memcg); + while (__arena_free_range(arena, (u32)addr, page_cnt)) + cond_resched(); + bpf_map_memcg_exit(old_memcg, new_memcg); + return 0; +} +EXPORT_SYMBOL_GPL(bpf_arena_unmap_ext_pages); + __bpf_kfunc_start_defs(); __bpf_kfunc void *bpf_arena_alloc_pages(void *p__map, void *addr__ign, u32 page_cnt, -- 2.56.1