Skip to content

Commit 15ecce6

Browse files
committed
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 53f5092 commit 15ecce6

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
{
@@ -21880,7 +21881,7 @@ static int kfunc_call_imm(struct bpf_verifier_env *env, unsigned long func_addr,
2188021881
}
2188121882

2188221883
/* replace a generic kfunc with a specialized version if necessary */
21883-
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc)
21884+
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc, int insn_idx)
2188421885
{
2188521886
struct bpf_prog *prog = env->prog;
2188621887
bool seen_direct_write;
@@ -21916,6 +21917,9 @@ static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc
2191621917
} else if (func_id == special_kfunc_list[KF_bpf_remove_dentry_xattr]) {
2191721918
if (bpf_lsm_has_d_inode_locked(prog))
2191821919
addr = (unsigned long)bpf_remove_dentry_xattr_locked;
21920+
} else if (func_id == special_kfunc_list[KF_bpf_dynptr_from_file]) {
21921+
if (!env->insn_aux_data[insn_idx].non_sleepable)
21922+
addr = (unsigned long)bpf_dynptr_from_file_sleepable;
2191921923
}
2192021924

2192121925
if (!addr) /* Nothing to patch with */
@@ -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)