Sleepable BPF programs still create local storage with kmalloc_maylock. Use the program aux metadata from the expanded helper to allocate storage with a sleepable call when available. Signed-off-by: Emil Tsalapatis --- include/linux/bpf.h | 5 +++++ kernel/bpf/bpf_local_storage.c | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5565a735ae7ce..52eee026d0685 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2094,6 +2094,11 @@ static inline bool bpf_prog_runs_sleepable(const struct bpf_prog *prog) return prog->sleepable || prog->aux->sleepable_async_cb; } +static inline bool bpf_prog_can_sleep(const struct bpf_prog *prog) +{ + return prog->sleepable && preemptible() && !rcu_preempt_depth(); +} + struct bpf_array_aux { /* Programs with direct jumps into programs part of this array. */ struct list_head poke_progs; diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index 28eaee5c66ce8..74dfebf180970 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -69,6 +69,10 @@ static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem) static void *bpf_local_storage_alloc_mem(struct bpf_local_storage_map *smap, size_t size, bool sleepable) { + if (sleepable) + return bpf_map_kzalloc(&smap->map, size, + GFP_KERNEL | __GFP_RETRY_MAYFAIL); + return bpf_map_kmalloc_maylock(&smap->map, size, __GFP_ZERO); } @@ -598,7 +602,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, { struct bpf_local_storage_data *old_sdata = NULL; struct bpf_local_storage_elem *alloc_selem, *selem = NULL; - bool sleepable = false; + bool sleepable = aux && bpf_prog_can_sleep(aux->prog); struct bpf_local_storage *local_storage; struct bpf_local_storage_map_bucket *b; HLIST_HEAD(old_selem_free_list); -- 2.52.1