Skip to content

Commit 23da464

Browse files
kkdwvdAlexei Starovoitov
authored andcommitted
bpf: Allow specifying volatile type modifier for kptrs
This is useful in particular to mark the pointer as volatile, so that compiler treats each load and store to the field as a volatile access. The alternative is having to define and use READ_ONCE and WRITE_ONCE in the BPF program. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Acked-by: David Vernet <void@manifault.com> Link: https://lore.kernel.org/r/20221103191013.1236066-3-memxor@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 9805af8 commit 23da464

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

include/linux/btf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ static inline bool btf_type_is_typedef(const struct btf_type *t)
288288
return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;
289289
}
290290

291+
static inline bool btf_type_is_volatile(const struct btf_type *t)
292+
{
293+
return BTF_INFO_KIND(t->info) == BTF_KIND_VOLATILE;
294+
}
295+
291296
static inline bool btf_type_is_func(const struct btf_type *t)
292297
{
293298
return BTF_INFO_KIND(t->info) == BTF_KIND_FUNC;

kernel/bpf/btf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,6 +3225,9 @@ static int btf_find_kptr(const struct btf *btf, const struct btf_type *t,
32253225
enum bpf_kptr_type type;
32263226
u32 res_id;
32273227

3228+
/* Permit modifiers on the pointer itself */
3229+
if (btf_type_is_volatile(t))
3230+
t = btf_type_by_id(btf, t->type);
32283231
/* For PTR, sz is always == 8 */
32293232
if (!btf_type_is_ptr(t))
32303233
return BTF_FIELD_IGNORE;

0 commit comments

Comments
 (0)