Skip to content

Commit eaa251b

Browse files
mykyta5Kernel Patches Daemon
authored andcommitted
bpf: dispatch to sleepable file dynptr
File dynptr reads may sleep when the requested folios are not in the page cache. To avoid sleeping in non-sleepable contexts while still supporting valid sleepable use, given that dynptrs are non-sleepable by default, enable sleeping only when bpf_dynptr_from_file() is invoked from a sleepable context. This change: * Introduces a sleepable constructor: bpf_dynptr_from_file_sleepable() * Override non-sleepable constructor with sleepable if it's always called in sleepable context Signed-off-by: Mykyta Yatsenko <[email protected]> Acked-by: Eduard Zingerman <[email protected]>
1 parent 9b24a35 commit eaa251b

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

include/linux/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,9 @@ static inline bool bpf_map_has_internal_structs(struct bpf_map *map)
670670

671671
void bpf_map_free_internal_structs(struct bpf_map *map, void *obj);
672672

673+
int bpf_dynptr_from_file_sleepable(struct file *file, u32 flags,
674+
struct bpf_dynptr *ptr__uninit);
675+
673676
extern const struct bpf_map_ops bpf_map_offload_ops;
674677

675678
/* bpf_type_flag contains a set of flags that are applicable to the values of

kernel/bpf/helpers.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4336,6 +4336,11 @@ __bpf_kfunc int bpf_dynptr_from_file(struct file *file, u32 flags, struct bpf_dy
43364336
return make_file_dynptr(file, flags, false, (struct bpf_dynptr_kern *)ptr__uninit);
43374337
}
43384338

4339+
int bpf_dynptr_from_file_sleepable(struct file *file, u32 flags, struct bpf_dynptr *ptr__uninit)
4340+
{
4341+
return make_file_dynptr(file, flags, true, (struct bpf_dynptr_kern *)ptr__uninit);
4342+
}
4343+
43394344
__bpf_kfunc int bpf_dynptr_file_discard(struct bpf_dynptr *dynptr)
43404345
{
43414346
struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)dynptr;

kernel/bpf/verifier.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,7 +3126,8 @@ struct bpf_kfunc_btf_tab {
31263126

31273127
static unsigned long kfunc_call_imm(unsigned long func_addr, u32 func_id);
31283128

3129-
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc);
3129+
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc,
3130+
int insn_idx);
31303131

31313132
static int kfunc_desc_cmp_by_id_off(const void *a, const void *b)
31323133
{
@@ -21879,7 +21880,7 @@ static unsigned long kfunc_call_imm(unsigned long func_addr, u32 func_id)
2187921880
}
2188021881

2188121882
/* replace a generic kfunc with a specialized version if necessary */
21882-
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc)
21883+
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc, int insn_idx)
2188321884
{
2188421885
struct bpf_prog *prog = env->prog;
2188521886
bool seen_direct_write;
@@ -21914,6 +21915,9 @@ static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc
2191421915
} else if (func_id == special_kfunc_list[KF_bpf_remove_dentry_xattr]) {
2191521916
if (bpf_lsm_has_d_inode_locked(prog))
2191621917
addr = (unsigned long)bpf_remove_dentry_xattr_locked;
21918+
} else if (func_id == special_kfunc_list[KF_bpf_dynptr_from_file]) {
21919+
if (!env->insn_aux_data[insn_idx].non_sleepable)
21920+
addr = (unsigned long)bpf_dynptr_from_file_sleepable;
2191721921
}
2191821922

2191921923
set_imm:
@@ -21969,7 +21973,7 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2196921973
return -EFAULT;
2197021974
}
2197121975

21972-
err = specialize_kfunc(env, desc);
21976+
err = specialize_kfunc(env, desc, insn_idx);
2197321977
if (err)
2197421978
return err;
2197521979

0 commit comments

Comments
 (0)