Skip to content

Commit 145f820

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 6504661 commit 145f820

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
@@ -3127,7 +3127,8 @@ struct bpf_kfunc_btf_tab {
31273127
static int kfunc_call_imm(struct bpf_verifier_env *env, unsigned long func_addr, u32 func_id,
31283128
s32 *imm);
31293129

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

31323133
static int kfunc_desc_cmp_by_id_off(const void *a, const void *b)
31333134
{
@@ -21898,7 +21899,7 @@ static int kfunc_call_imm(struct bpf_verifier_env *env, unsigned long func_addr,
2189821899
}
2189921900

2190021901
/* replace a generic kfunc with a specialized version if necessary */
21901-
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc)
21902+
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc, int insn_idx)
2190221903
{
2190321904
struct bpf_prog *prog = env->prog;
2190421905
bool seen_direct_write;
@@ -21934,6 +21935,9 @@ static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc
2193421935
} else if (func_id == special_kfunc_list[KF_bpf_remove_dentry_xattr]) {
2193521936
if (bpf_lsm_has_d_inode_locked(prog))
2193621937
addr = (unsigned long)bpf_remove_dentry_xattr_locked;
21938+
} else if (func_id == special_kfunc_list[KF_bpf_dynptr_from_file]) {
21939+
if (!env->insn_aux_data[insn_idx].non_sleepable)
21940+
addr = (unsigned long)bpf_dynptr_from_file_sleepable;
2193721941
}
2193821942

2193921943
if (!addr) /* Nothing to patch with */
@@ -21987,7 +21991,7 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2198721991
return -EFAULT;
2198821992
}
2198921993

21990-
err = specialize_kfunc(env, desc);
21994+
err = specialize_kfunc(env, desc, insn_idx);
2199121995
if (err)
2199221996
return err;
2199321997

0 commit comments

Comments
 (0)