Skip to content

Commit 391145b

Browse files
davemarchevskyAlexei Starovoitov
authored andcommitted
bpf: Add __bpf_kfunc_{start,end}_defs macros
BPF kfuncs are meant to be called from BPF programs. Accordingly, most kfuncs are not called from anywhere in the kernel, which the -Wmissing-prototypes warning is unhappy about. We've peppered __diag_ignore_all("-Wmissing-prototypes", ... everywhere kfuncs are defined in the codebase to suppress this warning. This patch adds two macros meant to bound one or many kfunc definitions. All existing kfunc definitions which use these __diag calls to suppress -Wmissing-prototypes are migrated to use the newly-introduced macros. A new __diag_ignore_all - for "-Wmissing-declarations" - is added to the __bpf_kfunc_start_defs macro based on feedback from Andrii on an earlier version of this patch [0] and another recent mailing list thread [1]. In the future we might need to ignore different warnings or do other kfunc-specific things. This change will make it easier to make such modifications for all kfunc defs. [0]: https://lore.kernel.org/bpf/CAEf4BzaE5dRWtK6RPLnjTW-MW9sx9K3Fn6uwqCTChK2Dcb1Xig@mail.gmail.com/ [1]: https://lore.kernel.org/bpf/ZT+2qCc%2FaXep0%2FLf@krava/ Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com> Suggested-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Cc: Jiri Olsa <olsajiri@gmail.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Acked-by: David Vernet <void@manifault.com> Acked-by: Yafang Shao <laoar.shao@gmail.com> Link: https://lore.kernel.org/r/20231031215625.2343848-1-davemarchevsky@fb.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent cd60f41 commit 391145b

File tree

16 files changed

+46
-73
lines changed

16 files changed

+46
-73
lines changed

Documentation/bpf/kfuncs.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,14 @@ prototype in a header for the wrapper kfunc.
3737
An example is given below::
3838

3939
/* Disables missing prototype warnings */
40-
__diag_push();
41-
__diag_ignore_all("-Wmissing-prototypes",
42-
"Global kfuncs as their definitions will be in BTF");
40+
__bpf_kfunc_start_defs();
4341

4442
__bpf_kfunc struct task_struct *bpf_find_get_task_by_vpid(pid_t nr)
4543
{
4644
return find_get_task_by_vpid(nr);
4745
}
4846

49-
__diag_pop();
47+
__bpf_kfunc_end_defs();
5048

5149
A wrapper kfunc is often needed when we need to annotate parameters of the
5250
kfunc. Otherwise one may directly make the kfunc visible to the BPF program by

include/linux/btf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@
8484
*/
8585
#define __bpf_kfunc __used noinline
8686

87+
#define __bpf_kfunc_start_defs() \
88+
__diag_push(); \
89+
__diag_ignore_all("-Wmissing-declarations", \
90+
"Global kfuncs as their definitions will be in BTF");\
91+
__diag_ignore_all("-Wmissing-prototypes", \
92+
"Global kfuncs as their definitions will be in BTF")
93+
94+
#define __bpf_kfunc_end_defs() __diag_pop()
95+
8796
/*
8897
* Return the name of the passed struct, if exists, or halt the build if for
8998
* example the structure gets renamed. In this way, developers have to revisit

kernel/bpf/bpf_iter.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,7 @@ struct bpf_iter_num_kern {
782782
int end; /* final value, exclusive */
783783
} __aligned(8);
784784

785-
__diag_push();
786-
__diag_ignore_all("-Wmissing-prototypes",
787-
"Global functions as their definitions will be in vmlinux BTF");
785+
__bpf_kfunc_start_defs();
788786

789787
__bpf_kfunc int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end)
790788
{
@@ -843,4 +841,4 @@ __bpf_kfunc void bpf_iter_num_destroy(struct bpf_iter_num *it)
843841
s->cur = s->end = 0;
844842
}
845843

846-
__diag_pop();
844+
__bpf_kfunc_end_defs();

kernel/bpf/cgroup_iter.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,7 @@ struct bpf_iter_css_kern {
305305
unsigned int flags;
306306
} __attribute__((aligned(8)));
307307

308-
__diag_push();
309-
__diag_ignore_all("-Wmissing-prototypes",
310-
"Global functions as their definitions will be in vmlinux BTF");
308+
__bpf_kfunc_start_defs();
311309

312310
__bpf_kfunc int bpf_iter_css_new(struct bpf_iter_css *it,
313311
struct cgroup_subsys_state *start, unsigned int flags)
@@ -358,4 +356,4 @@ __bpf_kfunc void bpf_iter_css_destroy(struct bpf_iter_css *it)
358356
{
359357
}
360358

361-
__diag_pop();
359+
__bpf_kfunc_end_defs();

kernel/bpf/cpumask.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ static bool cpu_valid(u32 cpu)
3434
return cpu < nr_cpu_ids;
3535
}
3636

37-
__diag_push();
38-
__diag_ignore_all("-Wmissing-prototypes",
39-
"Global kfuncs as their definitions will be in BTF");
37+
__bpf_kfunc_start_defs();
4038

4139
/**
4240
* bpf_cpumask_create() - Create a mutable BPF cpumask.
@@ -407,7 +405,7 @@ __bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
407405
return cpumask_any_and_distribute(src1, src2);
408406
}
409407

410-
__diag_pop();
408+
__bpf_kfunc_end_defs();
411409

412410
BTF_SET8_START(cpumask_kfunc_btf_ids)
413411
BTF_ID_FLAGS(func, bpf_cpumask_create, KF_ACQUIRE | KF_RET_NULL)

kernel/bpf/helpers.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,9 +1886,7 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
18861886
}
18871887
}
18881888

1889-
__diag_push();
1890-
__diag_ignore_all("-Wmissing-prototypes",
1891-
"Global functions as their definitions will be in vmlinux BTF");
1889+
__bpf_kfunc_start_defs();
18921890

18931891
__bpf_kfunc void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign)
18941892
{
@@ -2505,7 +2503,7 @@ __bpf_kfunc void bpf_throw(u64 cookie)
25052503
WARN(1, "A call to BPF exception callback should never return\n");
25062504
}
25072505

2508-
__diag_pop();
2506+
__bpf_kfunc_end_defs();
25092507

25102508
BTF_SET8_START(generic_btf_ids)
25112509
#ifdef CONFIG_KEXEC_CORE

kernel/bpf/map_iter.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ static int __init bpf_map_iter_init(void)
193193

194194
late_initcall(bpf_map_iter_init);
195195

196-
__diag_push();
197-
__diag_ignore_all("-Wmissing-prototypes",
198-
"Global functions as their definitions will be in vmlinux BTF");
196+
__bpf_kfunc_start_defs();
199197

200198
__bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map)
201199
{
@@ -213,7 +211,7 @@ __bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map)
213211
return ret;
214212
}
215213

216-
__diag_pop();
214+
__bpf_kfunc_end_defs();
217215

218216
BTF_SET8_START(bpf_map_iter_kfunc_ids)
219217
BTF_ID_FLAGS(func, bpf_map_sum_elem_count, KF_TRUSTED_ARGS)

kernel/bpf/task_iter.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,7 @@ struct bpf_iter_task_vma_kern {
822822
struct bpf_iter_task_vma_kern_data *data;
823823
} __attribute__((aligned(8)));
824824

825-
__diag_push();
826-
__diag_ignore_all("-Wmissing-prototypes",
827-
"Global functions as their definitions will be in vmlinux BTF");
825+
__bpf_kfunc_start_defs();
828826

829827
__bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
830828
struct task_struct *task, u64 addr)
@@ -890,7 +888,7 @@ __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it)
890888
}
891889
}
892890

893-
__diag_pop();
891+
__bpf_kfunc_end_defs();
894892

895893
#ifdef CONFIG_CGROUPS
896894

@@ -902,9 +900,7 @@ struct bpf_iter_css_task_kern {
902900
struct css_task_iter *css_it;
903901
} __attribute__((aligned(8)));
904902

905-
__diag_push();
906-
__diag_ignore_all("-Wmissing-prototypes",
907-
"Global functions as their definitions will be in vmlinux BTF");
903+
__bpf_kfunc_start_defs();
908904

909905
__bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it,
910906
struct cgroup_subsys_state *css, unsigned int flags)
@@ -950,7 +946,7 @@ __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it)
950946
bpf_mem_free(&bpf_global_ma, kit->css_it);
951947
}
952948

953-
__diag_pop();
949+
__bpf_kfunc_end_defs();
954950

955951
#endif /* CONFIG_CGROUPS */
956952

@@ -973,9 +969,7 @@ enum {
973969
BPF_TASK_ITER_PROC_THREADS
974970
};
975971

976-
__diag_push();
977-
__diag_ignore_all("-Wmissing-prototypes",
978-
"Global functions as their definitions will be in vmlinux BTF");
972+
__bpf_kfunc_start_defs();
979973

980974
__bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
981975
struct task_struct *task__nullable, unsigned int flags)
@@ -1045,7 +1039,7 @@ __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it)
10451039
{
10461040
}
10471041

1048-
__diag_pop();
1042+
__bpf_kfunc_end_defs();
10491043

10501044
DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
10511045

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,9 +1252,7 @@ static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
12521252
};
12531253

12541254
#ifdef CONFIG_KEYS
1255-
__diag_push();
1256-
__diag_ignore_all("-Wmissing-prototypes",
1257-
"kfuncs which will be used in BPF programs");
1255+
__bpf_kfunc_start_defs();
12581256

12591257
/**
12601258
* bpf_lookup_user_key - lookup a key by its serial
@@ -1404,7 +1402,7 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
14041402
}
14051403
#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
14061404

1407-
__diag_pop();
1405+
__bpf_kfunc_end_defs();
14081406

14091407
BTF_SET8_START(key_sig_kfunc_set)
14101408
BTF_ID_FLAGS(func, bpf_lookup_user_key, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)

net/bpf/test_run.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,8 @@ static int bpf_test_finish(const union bpf_attr *kattr,
503503
* architecture dependent calling conventions. 7+ can be supported in the
504504
* future.
505505
*/
506-
__diag_push();
507-
__diag_ignore_all("-Wmissing-prototypes",
508-
"Global functions as their definitions will be in vmlinux BTF");
506+
__bpf_kfunc_start_defs();
507+
509508
__bpf_kfunc int bpf_fentry_test1(int a)
510509
{
511510
return a + 1;
@@ -605,7 +604,7 @@ __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p)
605604
{
606605
}
607606

608-
__diag_pop();
607+
__bpf_kfunc_end_defs();
609608

610609
BTF_SET8_START(bpf_test_modify_return_ids)
611610
BTF_ID_FLAGS(func, bpf_modify_return_test)

0 commit comments

Comments
 (0)