Skip to content

Commit 2c52e89

Browse files
mykyta5Alexei Starovoitov
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]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent d869d56 commit 2c52e89

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
@@ -3124,7 +3124,8 @@ struct bpf_kfunc_btf_tab {
31243124
u32 nr_descs;
31253125
};
31263126

3127-
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc);
3127+
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc,
3128+
int insn_idx);
31283129

31293130
static int kfunc_desc_cmp_by_id_off(const void *a, const void *b)
31303131
{
@@ -21869,7 +21870,7 @@ static int fixup_call_args(struct bpf_verifier_env *env)
2186921870
}
2187021871

2187121872
/* replace a generic kfunc with a specialized version if necessary */
21872-
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc)
21873+
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc, int insn_idx)
2187321874
{
2187421875
struct bpf_prog *prog = env->prog;
2187521876
bool seen_direct_write;
@@ -21904,6 +21905,9 @@ static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc
2190421905
} else if (func_id == special_kfunc_list[KF_bpf_remove_dentry_xattr]) {
2190521906
if (bpf_lsm_has_d_inode_locked(prog))
2190621907
addr = (unsigned long)bpf_remove_dentry_xattr_locked;
21908+
} else if (func_id == special_kfunc_list[KF_bpf_dynptr_from_file]) {
21909+
if (!env->insn_aux_data[insn_idx].non_sleepable)
21910+
addr = (unsigned long)bpf_dynptr_from_file_sleepable;
2190721911
}
2190821912

2190921913
set_imm:
@@ -21963,7 +21967,7 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2196321967
return -EFAULT;
2196421968
}
2196521969

21966-
err = specialize_kfunc(env, desc);
21970+
err = specialize_kfunc(env, desc, insn_idx);
2196721971
if (err)
2196821972
return err;
2196921973

0 commit comments

Comments
 (0)