Add ability to attach bpf programs to a tracepoint inside a cgroup
Subsystems (9)
cover Add ability to attach bpf programs to a tracepoint inside a cgroup
Per an earlier discussion last year[1], I have been looking for a mechanism to a) collect resource usages for devices (GPU for now but there could be other device type in the future) and b) possibly enforce some of the resource usages. An obvious mechanism was to use cgroup but there are too much diversity in GPU hardware architecture to have a common cgroup interface at this point. An alternative is to leverage tracepoint with a bpf program inside a cgroup hierarchy for usage collection and enforcement (via writable tracepoint.)
This is a prototype for such idea. It is incomplete but I would like to solicit some feedback before continuing to make sure I am going down the right path. This prototype is built based on my understanding of the followings:
- tracepoint (and kprobe, uprobe) is associated with perf event
- perf events/tracepoint can be a hook for bpf progs but those bpf progs are not part of the cgroup hierarchy
- bpf progs can be attached to the cgroup hierarchy with cgroup local storage and other benefits
- separately, perf subsystem has a cgroup controller (perf cgroup) that allow perf event to be triggered with a cgroup filter
So the key idea of this RFC is to leverage hierarchical organization of bpf-cgroup for the purpose of perf event/tracepoints.
==Known unresolved topics (feedback very much welcome)==
Storage:
I came across the idea of "preallocated" memory for bpf hash map/storage to avoid deadlock[2] but I don't have a good understanding about it currently. If existing bpf_cgroup_storage_type are not considered pre-allocated then I am thinking we can introduce a new type but I am not sure if this is needed yet.
Scalability:
Scalability concern has been raised about perf cgroup [3] and there seems to be a solution to it recently with bperf [4]. This RFC does not change the status quo on the scalability question but if I understand the bperf idea correctly, this RFC may have some similarity.
[1] https://lore.kernel.org/netdev/YJXRHXIykyEBdnTF-NiLfg/[email protected]/T/#m52bc26bbbf16131c48e6b34d875c87660943c452
[2] https://lwn.net/Articles/679074/
[3] https://www.linuxplumbersconf.org/event/4/contributions/291/attachments/313/528/Linux_Plumbers_Conference_2019.pdf
[4] https://linuxplumbersconf.org/event/11/contributions/899/
Kenny Ho (4):
cgroup, perf: Add ability to connect to perf cgroup from other cgroup
controller
bpf, perf: add ability to attach complete array of bpf prog to perf
event
bpf,cgroup,tracing: add new BPF_PROG_TYPE_CGROUP_TRACEPOINT bpf,cgroup,perf: extend bpf-cgroup to support tracepoint attachment
include/linux/bpf-cgroup.h | 17 +++++-- include/linux/bpf_types.h | 4 ++ include/linux/cgroup.h | 2 + include/linux/perf_event.h | 6 +++ include/linux/trace_events.h | 9 ++++ include/uapi/linux/bpf.h | 2 + kernel/bpf/cgroup.c | 96 +++++++++++++++++++++++++++++------- kernel/bpf/syscall.c | 4 ++ kernel/cgroup/cgroup.c | 13 ++--- kernel/events/core.c | 62 +++++++++++++++++++++++ kernel/trace/bpf_trace.c | 36 ++++++++++++++ 11 files changed, 222 insertions(+), 29 deletions(-)
Signature · 1 line
2.25.1
RFC PATCH 1/4 cgroup, perf: Add ability to connect to perf cgroup from other cgroup controller
This provides the ability to allocate cgroup specific perf_event by bpf-cgroup in later patch
--- include/linux/cgroup.h | 2 ++ include/linux/perf_event.h | 2 ++ kernel/cgroup/cgroup.c | 4 ++-- kernel/events/core.c | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-)
include/linux/cgroup.h +2 -0
@@ -97,6 +97,8 @@ extern struct css_set init_css_set;bool css_has_online_children (struct cgroup_subsys_state *css);struct cgroup_subsys_state *css_from_id (int id,struct cgroup_subsys *ss); +struct cgroup_subsys_state *cgroup_tryget_css (struct cgroup *cgroup, +struct cgroup_subsys *ss);struct cgroup_subsys_state *cgroup_e_css (struct cgroup *cgroup,struct cgroup_subsys *ss); struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
include/linux/perf_event.h +2 -0
@@ -926,6 +926,8 @@ struct perf_cgroup {struct perf_cgroup_info __percpu *info; }; +extern struct perf_cgroup *cgroup_tryget_perf_cgroup (struct cgroup *cgrp); +/* * Must ensure cgroup is pinned (css_get) before calling * this function. In other words, we cannot call this function
kernel/cgroup/cgroup.c +2 -2
@@ -495,8 +495,8 @@ static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,* Find and get @cgrp's css associated with @ss. If the css doesn't exist * or is offline, %NULL is returned. */ -static struct cgroup_subsys_state *cgroup_tryget_css (struct cgroup *cgrp, -struct cgroup_subsys *ss) +struct cgroup_subsys_state *cgroup_tryget_css (struct cgroup *cgrp, +struct cgroup_subsys *ss) {struct cgroup_subsys_state *css;
kernel/events/core.c +17 -0
@@ -941,6 +941,18 @@ static int perf_cgroup_ensure_storage(struct perf_event *event,return ret; } +struct perf_cgroup *cgroup_tryget_perf_cgroup (struct cgroup *cgrp) +{ +struct cgroup_subsys_state *css; + + css =cgroup_tryget_css (cgrp, &perf_event_cgrp_subsys); + +if (!css) +return NULL ; + + return container_of(css, struct perf_cgroup, css); +} +static inline int perf_cgroup_connect (int fd,struct perf_event *event,struct perf_event_attr *attr,struct perf_event *group_leader)
@@ -1080,6 +1092,11 @@ static inline void perf_cgroup_sched_in(struct task_struct *prev, { } +struct perf_cgroup *cgroup_tryget_perf_cgroup (struct cgroup *cgrp) +{ +return NULL ; +} +static inline int perf_cgroup_connect (pid_t pid,struct perf_event *event,struct perf_event_attr *attr,struct perf_event *group_leader)
Signature · 1 line
2.25.1
kernel test robot
build botRe: [PATCH RFC 1/4] cgroup, perf: Add ability to connect to perf cgroup from other cgroup controller
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Kenny,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on v5.15]
[also build test WARNING on next-20211126]
[cannot apply to bpf-next/master bpf/master tip/perf/core v5.16-rc1] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Kenny-Ho/Add-ability-to-attach-bpf-programs-to-a-tracepoint-inside-a-cgroup/20211119-043050 base: 8bb7eca972ad531c9b149c0a51ab43a417385813 config: hexagon-randconfig-r041-20211118 (https://download.01.org/0day-ci/archive/20211127/202111272015.KZlJ9xBN-lkp(a)intel.com/config)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/6946ea4a7228dd06a9b6624702bcec1cfc7865ec
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kenny-Ho/Add-ability-to-attach-bpf-programs-to-a-tracepoint-inside-a-cgroup/20211119-043050 git checkout 6946ea4a7228dd06a9b6624702bcec1cfc7865ec
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash ipc/ kernel/events/ kernel/time/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> kernel/events/core.c:1095:21: warning: no previous prototype for function 'cgroup_tryget_perf_cgroup' [-Wmissing-prototypes]
struct perf_cgroup *cgroup_tryget_perf_cgroup(struct cgroup *cgrp)
^ kernel/events/core.c:1095:1: note: declare 'static' if the function is not intended to be used outside of this translation unit struct perf_cgroup *cgroup_tryget_perf_cgroup(struct cgroup *cgrp)
^
static
kernel/events/core.c:1114:1: warning: unused function 'perf_cgroup_switch' [-Wunused-function] perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
^
2 warnings generated.
vim +/cgroup_tryget_perf_cgroup +1095 kernel/events/core.c
1094
> 1095 struct perf_cgroup *cgroup_tryget_perf_cgroup(struct cgroup *cgrp)
1096 {
1097 return NULL;
1098 }
1099
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [PATCH RFC 1/4] cgroup, perf: Add ability to connect to perf cgroup from other cgroup controller
In reply to Kenny Ho[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Kenny,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on v5.15]
[also build test WARNING on next-20211126]
[cannot apply to bpf-next/master bpf/master tip/perf/core v5.16-rc1] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Kenny-Ho/Add-ability-to-attach-bpf-programs-to-a-tracepoint-inside-a-cgroup/20211119-043050 base: 8bb7eca972ad531c9b149c0a51ab43a417385813 config: s390-randconfig-r044-20211118 (https://download.01.org/0day-ci/archive/20211127/202111272042.XuzEpxWN-lkp(a)intel.com/config) compiler: s390-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/6946ea4a7228dd06a9b6624702bcec1cfc7865ec
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kenny-Ho/Add-ability-to-attach-bpf-programs-to-a-tracepoint-inside-a-cgroup/20211119-043050 git checkout 6946ea4a7228dd06a9b6624702bcec1cfc7865ec
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=s390 SHELL=/bin/bash kernel/events/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> kernel/events/core.c:1095:21: warning: no previous prototype for 'cgroup_tryget_perf_cgroup' [-Wmissing-prototypes]
1095 | struct perf_cgroup *cgroup_tryget_perf_cgroup(struct cgroup *cgrp)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
vim +/cgroup_tryget_perf_cgroup +1095 kernel/events/core.c
1094
> 1095 struct perf_cgroup *cgroup_tryget_perf_cgroup(struct cgroup *cgrp)
1096 {
1097 return NULL;
1098 }
1099
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
RFC PATCH 2/4 bpf, perf: add ability to attach complete array of bpf prog to perf event
In reply to Kenny Ho--- include/linux/trace_events.h | 9 +++++++++ kernel/trace/bpf_trace.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+)
include/linux/trace_events.h +9 -0
@@ -725,6 +725,8 @@ trace_trigger_soft_disabled(struct trace_event_file *file)#ifdef CONFIG_BPF_EVENTS unsigned int trace_call_bpf (struct trace_event_call *call,void *ctx); +int perf_event_attach_bpf_prog_array (struct perf_event *event, +struct bpf_prog_array *new_array);int perf_event_attach_bpf_prog (struct perf_event *event,struct bpf_prog *prog,u64 bpf_cookie);void perf_event_detach_bpf_prog (struct perf_event *event);int perf_event_query_prog_array (struct perf_event *event,void __user *info);
@@ -741,6 +743,13 @@ static inline unsigned int trace_call_bpf(struct trace_event_call *call, void *creturn 1 ; } +static inline int +int perf_event_attach_bpf_prog_array(struct perf_event *event, +struct bpf_prog_array *new_array) +{ +return -EOPNOTSUPP ; +} +static inline int perf_event_attach_bpf_prog (struct perf_event *event,struct bpf_prog *prog,u64 bpf_cookie) {
kernel/trace/bpf_trace.c +28 -0
@@ -1802,6 +1802,34 @@ static DEFINE_MUTEX(bpf_event_mutex);#define BPF_TRACE_MAX_PROGS 64 +int perf_event_attach_bpf_prog_array (struct perf_event *event, +struct bpf_prog_array *new_array) +{ +struct bpf_prog_array_item *item; +struct bpf_prog_array *old_array; + +if (!new_array) +return -EINVAL ; + +if (bpf_prog_array_length (new_array) >=BPF_TRACE_MAX_PROGS ) +return -E2BIG ; + +if (!trace_kprobe_on_func_entry (event->tp_event) || + !trace_kprobe_error_injectable (event->tp_event)) +for (item = new_array->items; item->prog; item++) +if (item->prog->kprobe_override) +return -EINVAL ; + +mutex_lock (&bpf_event_mutex); + + old_array =bpf_event_rcu_dereference (event->tp_event->prog_array); +rcu_assign_pointer (event->tp_event->prog_array, new_array); +bpf_prog_array_free (old_array); + +mutex_unlock (&bpf_event_mutex); +return 0 ; +} +int perf_event_attach_bpf_prog (struct perf_event *event,struct bpf_prog *prog,u64 bpf_cookie)
Signature · 1 line
2.25.1
kernel test robot
build botRe: [PATCH RFC 2/4] bpf, perf: add ability to attach complete array of bpf prog to perf event
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Kenny,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on v5.15]
[also build test WARNING on next-20211126]
[cannot apply to bpf-next/master bpf/master tip/perf/core v5.16-rc1] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Kenny-Ho/Add-ability-to-attach-bpf-programs-to-a-tracepoint-inside-a-cgroup/20211119-043050 base: 8bb7eca972ad531c9b149c0a51ab43a417385813 config: hexagon-randconfig-r045-20211118 (https://download.01.org/0day-ci/archive/20211127/202111272020.wypD6U2V-lkp(a)intel.com/config)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/9de19275a9d70c47811096791fae34fedbdeeb94
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kenny-Ho/Add-ability-to-attach-bpf-programs-to-a-tracepoint-inside-a-cgroup/20211119-043050 git checkout 9de19275a9d70c47811096791fae34fedbdeeb94
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash drivers/scsi/ufs/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
In file included from drivers/scsi/ufs/ufshcd.c:32:
In file included from include/trace/events/ufs.h:396:
In file included from include/trace/define_trace.h:102:
In file included from include/trace/trace_events.h:21:
include/linux/trace_events.h:747:1: error: cannot combine with previous 'int' declaration specifier int perf_event_attach_bpf_prog_array(struct perf_event *event,
^
>> drivers/scsi/ufs/ufshcd.c:9415:44: warning: shift count >= width of type [-Wshift-count-overflow]
if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
^~~~~~~~~~~~~~~~
include/linux/dma-mapping.h:76:54: note: expanded from macro 'DMA_BIT_MASK'
#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
^ ~~~1 warning and 1 error generated.
vim +9415 drivers/scsi/ufs/ufshcd.c
47555a5c8a11a4 Yaniv Gardi 2015-10-28 9404
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9405 /**
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9406 * ufshcd_set_dma_mask - Set dma mask based on the controller
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9407 * addressing capability
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9408 * @hba: per adapter instance
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9409 *
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9410 * Returns 0 for success, non-zero for failure
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9411 */
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9412 static int ufshcd_set_dma_mask(struct ufs_hba *hba)
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9413 {
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9414 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 @9415 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9416 return 0;
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9417 }
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9418 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9419 }ca3d7bf9c646e9 Akinobu Mita 2014-07-13 9420
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [PATCH RFC 2/4] bpf, perf: add ability to attach complete array of bpf prog to perf event
In reply to Kenny Ho[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Kenny,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on v5.15]
[also build test ERROR on next-20211126]
[cannot apply to bpf-next/master bpf/master tip/perf/core v5.16-rc1] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Kenny-Ho/Add-ability-to-attach-bpf-programs-to-a-tracepoint-inside-a-cgroup/20211119-043050 base: 8bb7eca972ad531c9b149c0a51ab43a417385813 config: x86_64-defconfig (https://download.01.org/0day-ci/archive/20211127/202111272031.gglKL3hJ-lkp(a)intel.com/config) compiler: gcc-9 (Debian 9.3.0-22) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/9de19275a9d70c47811096791fae34fedbdeeb94 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Kenny-Ho/Add-ability-to-attach-bpf-programs-to-a-tracepoint-inside-a-cgroup/20211119-043050 git checkout 9de19275a9d70c47811096791fae34fedbdeeb94 # save the config file to linux build tree mkdir build_dir make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from fs/quota/quota.c:16:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers 747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
--
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from drivers/char/random.c:335:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers 747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
drivers/char/random.c:2272:6: warning: no previous prototype for 'add_hwgenerator_randomness' [-Wmissing-prototypes]
2272 | void add_hwgenerator_randomness(const char *buffer, size_t count,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
--
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from init/main.c:21:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers
747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
init/main.c:789:20: warning: no previous prototype for 'mem_encrypt_init' [-Wmissing-prototypes]
789 | void __init __weak mem_encrypt_init(void) { }
| ^~~~~~~~~~~~~~~~
--
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from include/linux/entry-common.h:7,
from arch/x86/entry/common.c:13:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers 747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
arch/x86/entry/common.c:238:24: warning: no previous prototype for 'do_SYSENTER_32' [-Wmissing-prototypes]
238 | __visible noinstr long do_SYSENTER_32(struct pt_regs *regs)
| ^~~~~~~~~~~~~~
--
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from include/linux/entry-common.h:7,
from arch/x86/include/asm/idtentry.h:9,
from arch/x86/include/asm/traps.h:9,
from arch/x86/mm/extable.c:9:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers 747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
arch/x86/mm/extable.c:27:16: warning: no previous prototype for 'ex_handler_default' [-Wmissing-prototypes]
27 | __visible bool ex_handler_default(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:37:16: warning: no previous prototype for 'ex_handler_fault' [-Wmissing-prototypes]
37 | __visible bool ex_handler_fault(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:58:16: warning: no previous prototype for 'ex_handler_fprestore' [-Wmissing-prototypes]
58 | __visible bool ex_handler_fprestore(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:73:16: warning: no previous prototype for 'ex_handler_uaccess' [-Wmissing-prototypes]
73 | __visible bool ex_handler_uaccess(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:84:16: warning: no previous prototype for 'ex_handler_copy' [-Wmissing-prototypes]
84 | __visible bool ex_handler_copy(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~
arch/x86/mm/extable.c:96:16: warning: no previous prototype for 'ex_handler_rdmsr_unsafe' [-Wmissing-prototypes]
96 | __visible bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:113:16: warning: no previous prototype for 'ex_handler_wrmsr_unsafe' [-Wmissing-prototypes]
113 | __visible bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~~~~~~
arch/x86/mm/extable.c:129:16: warning: no previous prototype for 'ex_handler_clear_fs' [-Wmissing-prototypes]
129 | __visible bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
| ^~~~~~~~~~~~~~~~~~~
--
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from kernel/audit.c:44:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers
747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
kernel/audit.c: In function 'audit_log_vformat':
kernel/audit.c:1926:2: warning: function 'audit_log_vformat' might be a candidate for 'gnu_printf' format attribute [-Wsuggest-attribute=format]
1926 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
| ^~~
kernel/audit.c:1935:3: warning: function 'audit_log_vformat' might be a candidate for 'gnu_printf' format attribute [-Wsuggest-attribute=format]
1935 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
| ^~~
--
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from kernel/exit.c:42:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers
747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
kernel/exit.c:1810:13: warning: no previous prototype for 'abort' [-Wmissing-prototypes]
1810 | __weak void abort(void)
| ^~~~~
--
In file included from include/trace/trace_events.h:21,
from include/trace/define_trace.h:102,
from include/trace/events/vmscan.h:460,
from mm/vmscan.c:63:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers
747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
mm/vmscan.c: In function 'demote_page_list':
mm/vmscan.c:1340:6: warning: variable 'err' set but not used [-Wunused-but-set-variable]
1340 | int err;
| ^~~
--
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from fs/io_uring.c:45:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers 747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
fs/io_uring.c: In function 'io_queue_async_work':
fs/io_uring.c:1443:61: warning: parameter 'locked' set but not used [-Wunused-but-set-parameter]
1443 | static void io_queue_async_work(struct io_kiocb *req, bool *locked)
| ~~~~~~^~~~~~
--
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from fs/pipe.c:24:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers 747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
fs/pipe.c:755:15: warning: no previous prototype for 'account_pipe_buffers' [-Wmissing-prototypes]
755 | unsigned long account_pipe_buffers(struct user_struct *user,
| ^~~~~~~~~~~~~~~~~~~~
fs/pipe.c:761:6: warning: no previous prototype for 'too_many_pipe_buffers_soft' [-Wmissing-prototypes]
761 | bool too_many_pipe_buffers_soft(unsigned long user_bufs)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
fs/pipe.c:768:6: warning: no previous prototype for 'too_many_pipe_buffers_hard' [-Wmissing-prototypes]
768 | bool too_many_pipe_buffers_hard(unsigned long user_bufs)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
fs/pipe.c:775:6: warning: no previous prototype for 'pipe_is_unprivileged_user' [-Wmissing-prototypes]
775 | bool pipe_is_unprivileged_user(void)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
fs/pipe.c:1245:5: warning: no previous prototype for 'pipe_resize_ring' [-Wmissing-prototypes]
1245 | int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
| ^~~~~~~~~~~~~~~~
--
In file included from include/trace/syscall.h:7,
from include/linux/syscalls.h:87,
from fs/d_path.c:2:
>> include/linux/trace_events.h:747:1: error: two or more data types in declaration specifiers 747 | int perf_event_attach_bpf_prog_array(struct perf_event *event,
| ^~~
fs/d_path.c:320:7: warning: no previous prototype for 'simple_dname' [-Wmissing-prototypes]
320 | char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
| ^~~~~~~~~~~~
..
vim +747 include/linux/trace_events.h
745
746 static inline int
> 747 int perf_event_attach_bpf_prog_array(struct perf_event *event,
748 struct bpf_prog_array *new_array)
749 {
750 return -EOPNOTSUPP;
751 }
752
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
RFC PATCH 3/4 bpf,cgroup,tracing: add new BPF_PROG_TYPE_CGROUP_TRACEPOINT
In reply to Kenny Ho--- include/linux/bpf-cgroup.h | 2 ++ include/linux/bpf_types.h | 4 ++++ include/uapi/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 4 ++++ kernel/trace/bpf_trace.c | 8 ++++++++ 5 files changed, 20 insertions(+)
include/linux/bpf-cgroup.h +2 -0
@@ -48,6 +48,7 @@ enum cgroup_bpf_attach_type {CGROUP_INET4_GETSOCKNAME ,CGROUP_INET6_GETSOCKNAME ,CGROUP_INET_SOCK_RELEASE , +CGROUP_TRACEPOINT , MAX_CGROUP_BPF_ATTACH_TYPE };
@@ -81,6 +82,7 @@ to_cgroup_bpf_attach_type(enum bpf_attach_type attach_type)CGROUP_ATYPE (CGROUP_INET4_GETSOCKNAME );CGROUP_ATYPE (CGROUP_INET6_GETSOCKNAME );CGROUP_ATYPE (CGROUP_INET_SOCK_RELEASE ); +CGROUP_ATYPE (CGROUP_TRACEPOINT );default :return CGROUP_BPF_ATTACH_TYPE_INVALID ; }
include/linux/bpf_types.h +4 -0
@@ -57,6 +57,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SYSCTL, cg_sysctl,BPF_PROG_TYPE (BPF_PROG_TYPE_CGROUP_SOCKOPT , cg_sockopt, struct bpf_sockopt, struct bpf_sockopt_kern)#endif +#if defined (CONFIG_BPF_EVENTS ) && defined (CONFIG_CGROUP_BPF ) +BPF_PROG_TYPE (BPF_PROG_TYPE_CGROUP_TRACEPOINT , cg_tracepoint, + __u64, u64) +#endif #ifdef CONFIG_BPF_LIRC_MODE2 BPF_PROG_TYPE (BPF_PROG_TYPE_LIRC_MODE2 , lirc_mode2, __u32, u32)
include/uapi/linux/bpf.h +2 -0
@@ -949,6 +949,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_LSM ,BPF_PROG_TYPE_SK_LOOKUP ,BPF_PROG_TYPE_SYSCALL ,/* a program that can execute syscalls */ + BPF_PROG_TYPE_CGROUP_TRACEPOINT, }; enum bpf_attach_type {
@@ -994,6 +995,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT ,BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT , +BPF_CGROUP_TRACEPOINT , __MAX_BPF_ATTACH_TYPE };
kernel/bpf/syscall.c +4 -0
@@ -2149,6 +2149,7 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)case BPF_PROG_TYPE_LSM :case BPF_PROG_TYPE_STRUCT_OPS :/* has access to struct sock */ case BPF_PROG_TYPE_EXT :/* extends any prog */ +case BPF_PROG_TYPE_CGROUP_TRACEPOINT :return true;default :return false;
@@ -3137,6 +3138,8 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)return BPF_PROG_TYPE_SK_LOOKUP ;case BPF_XDP :return BPF_PROG_TYPE_XDP ; +case BPF_CGROUP_TRACEPOINT : +return BPF_PROG_TYPE_CGROUP_TRACEPOINT ;default :return BPF_PROG_TYPE_UNSPEC ; }
@@ -3189,6 +3192,7 @@ static int bpf_prog_attach(const union bpf_attr *attr)case BPF_PROG_TYPE_CGROUP_SOCK_ADDR :case BPF_PROG_TYPE_CGROUP_SOCKOPT :case BPF_PROG_TYPE_CGROUP_SYSCTL : +case BPF_PROG_TYPE_CGROUP_TRACEPOINT :case BPF_PROG_TYPE_SOCK_OPS : ret =cgroup_bpf_prog_attach (attr, ptype, prog);break ;
kernel/trace/bpf_trace.c +8 -0
@@ -1798,6 +1798,14 @@ const struct bpf_verifier_ops perf_event_verifier_ops = {const struct bpf_prog_ops perf_event_prog_ops = { }; +const struct bpf_verifier_ops cg_tracepoint_verifier_ops = { + .get_func_proto = tp_prog_func_proto, + .is_valid_access = tp_prog_is_valid_access, +}; + +const struct bpf_prog_ops cg_tracepoint_prog_ops = { +}; +static DEFINE_MUTEX (bpf_event_mutex);#define BPF_TRACE_MAX_PROGS 64
Signature · 1 line
2.25.1
RFC PATCH 4/4 bpf,cgroup,perf: extend bpf-cgroup to support tracepoint attachment
In reply to Kenny Hobpf progs are attached to cgroups as usual with the idea of effective progs remain the same. The perf event / tracepoint's fd is defined as attachment 'subtype'. The 'subtype' is passed along during attachment via bpf_attr, reusing replace_bpf_fd field.
After the effective progs are calculated, perf_event is allocated using the 'subtype'/'fd' value for all cpus filtering on the perf cgroup that corresponds to the bpf-cgroup (with assumption of a unified hierarchy.) The effective bpf prog array is then attached to each newly allocated perf_event and subsequently enabled by activate_effective_progs.
--- include/linux/bpf-cgroup.h | 15 ++++-- include/linux/perf_event.h | 4 ++ kernel/bpf/cgroup.c | 96 +++++++++++++++++++++++++++++++------- kernel/cgroup/cgroup.c | 9 ++-- kernel/events/core.c | 45 ++++++++++++++++++ 5 files changed, 142 insertions(+), 27 deletions(-)
include/linux/bpf-cgroup.h +10 -5
@@ -154,6 +154,11 @@ struct cgroup_bpf {/* cgroup_bpf is released using a work queue */ struct work_struct release_work; + +/* list of perf events (per child cgroups) for tracepoint/kprobe/uprobe bpf attachment to cgroup */ +/* TODO: array of tp type with array of events for each cgroup +* currently only one tp type supported at a time */ +struct list_head per_cg_events; };int cgroup_bpf_inherit (struct cgroup *cgrp);
@@ -161,21 +166,21 @@ void cgroup_bpf_offline(struct cgroup *cgrp);int __cgroup_bpf_attach (struct cgroup *cgrp,struct bpf_prog *prog,struct bpf_prog *replace_prog, -struct bpf_cgroup_link *link, +struct bpf_cgroup_link *link,int bpf_attach_subtype,enum bpf_attach_type type,u32 flags);int __cgroup_bpf_detach (struct cgroup *cgrp,struct bpf_prog *prog,struct bpf_cgroup_link *link, -enum bpf_attach_type type); +enum bpf_attach_type type,int bpf_attach_subtype);int __cgroup_bpf_query (struct cgroup *cgrp,const union bpf_attr *attr,union bpf_attr __user *uattr);/* Wrapper for __cgroup_bpf_*() protected by cgroup_mutex */ int cgroup_bpf_attach (struct cgroup *cgrp,struct bpf_prog *prog,struct bpf_prog *replace_prog, -struct bpf_cgroup_link *link,enum bpf_attach_type type, -u32 flags); +struct bpf_cgroup_link *link,int bpf_attach_subtype, +enum bpf_attach_type type,u32 flags);int cgroup_bpf_detach (struct cgroup *cgrp,struct bpf_prog *prog, -enum bpf_attach_type type); +enum bpf_attach_type type,int bpf_attach_subtype);int cgroup_bpf_query (struct cgroup *cgrp,const union bpf_attr *attr,union bpf_attr __user *uattr);
include/linux/perf_event.h +4 -0
@@ -776,6 +776,7 @@ struct perf_event {#ifdef CONFIG_CGROUP_PERF struct perf_cgroup *cgrp;/* cgroup event is attach to */ +struct list_head bpf_cg_list;#endif #ifdef CONFIG_SECURITY
@@ -982,6 +983,9 @@ extern void perf_pmu_resched(struct pmu *pmu);extern int perf_event_refresh (struct perf_event *event,int refresh);extern void perf_event_update_userpage (struct perf_event *event);extern int perf_event_release_kernel (struct perf_event *event); +extern int perf_event_create_for_all_cpus (struct perf_event_attr *attr, +struct cgroup *cgroup, +struct list_head *entries);extern struct perf_event * perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
kernel/bpf/cgroup.c +78 -18
226 lines not on this page · raw message
kernel/cgroup/cgroup.c +5 -4
@@ -6626,25 +6626,26 @@ void cgroup_sk_free(struct sock_cgroup_data *skcd)#ifdef CONFIG_CGROUP_BPF int cgroup_bpf_attach (struct cgroup *cgrp,struct bpf_prog *prog,struct bpf_prog *replace_prog, -struct bpf_cgroup_link *link, +struct bpf_cgroup_link *link,int bpf_attach_subtype,enum bpf_attach_type type,u32 flags) {int ret;mutex_lock (&cgroup_mutex); - ret =__cgroup_bpf_attach (cgrp, prog, replace_prog, link, type, flags); + ret =__cgroup_bpf_attach (cgrp, prog, replace_prog, link, + bpf_attach_subtype, type, flags);mutex_unlock (&cgroup_mutex);return ret; }int cgroup_bpf_detach (struct cgroup *cgrp,struct bpf_prog *prog, -enum bpf_attach_type type) +enum bpf_attach_type type,int bpf_attach_subtype) {int ret;mutex_lock (&cgroup_mutex); - ret =__cgroup_bpf_detach (cgrp, prog,NULL , type); + ret =__cgroup_bpf_detach (cgrp, prog,NULL , type, bpf_attach_subtype);mutex_unlock (&cgroup_mutex);return ret; }
kernel/events/core.c +45 -0
51 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH RFC 4/4] bpf,cgroup,perf: extend bpf-cgroup to support tracepoint attachment
Quoted 18 lines · Kenny Ho
kernel/bpf/cgroup.c +14
-0@@ -245,6 +256,21 @@ static int compute_effective_progs(struct cgroup *cgrp,if (!progs)return -ENOMEM ; +if (atype ==CGROUP_TRACEPOINT ) { +/* TODO: only create event for cgroup that can have process */ + + attr.config = bpf_attach_subtype; + attr.type =PERF_TYPE_TRACEPOINT ; + attr.sample_type =PERF_SAMPLE_RAW ; + attr.sample_period =1 ; + attr.wakeup_events =1 ; + + rc =perf_event_create_for_all_cpus (&attr, cgrp, + &cgrp->bpf.per_cg_events); +if (rc) + goto err; + }
...
Quoted 7 lines
kernel/events/core.c +7
-0+int perf_event_create_for_all_cpus (struct perf_event_attr *attr, +struct cgroup *cgroup, +struct list_head *entries) +{ +struct perf_event **events; +struct perf_cgroup *perf_cgrp; +int cpu, i =0 ;
kernel/events/core.c perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, · line 12530
Quoted 10 lines
kernel/events/core.c +10
-0+ + events =kzalloc (sizeof (struct perf_event *) *num_possible_cpus (), +GFP_KERNEL ); + +if (!events) +return -ENOMEM ; + +for_each_possible_cpu (cpu) { +/* allocate first, connect the cgroup later */ + events[i] =perf_event_create_kernel_counter (attr, cpu,NULL ,NULL ,NULL );
This is a very heavy hammer for this task.
There is really no need for perf_event to be created.
Did you consider using raw_tp approach instead?
It doesn't need this heavy stuff.
Also I suspect in follow up you'd be adding tracepoints to GPU code? Did you consider just leaving few __weak global functions in GPU code and let bpf progs attach to them as fentry?
I suspect the true hierarchical nature of bpf-cgroup framework isn't necessary. The bpf program itself can filter for given cgroup.
We have bpf_current_task_under_cgroup() and friends.
I suggest to sprinkle __weak empty funcs in GPU and see what
you can do with it with fentry and bpf_current_task_under_cgroup. There is also bpf_get_current_ancestor_cgroup_id().
Re: [PATCH RFC 4/4] bpf,cgroup,perf: extend bpf-cgroup to support tracepoint attachment
Quoted 9 lines · Alexei Starovoitov
kernel/events/core.c perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, · line 12530
Quoted 3 lines · Kenny Ho
kernel/events/core.c +3
-0+for_each_possible_cpu (cpu) { +/* allocate first, connect the cgroup later */ + events[i] =perf_event_create_kernel_counter (attr, cpu,NULL ,NULL ,NULL );This is a very heavy hammer for this task. There is really no need for
perf_eventto be created. Did you consider usingraw_tpapproach instead?
I came across raw_tp but I don't have a good understanding of it yet. Initially I was hoping perf event/tracepoint is a stepping stone to raw tp but that doesn't seem to be the case (and unfortunately I picked the perf event/tracepoint route to dive in first because I saw cgroup usage.) Can you confirm if the following statements are true?
- is raw_tp related to writable tracepoint
- are perf_event/tracepoint/kprobe/uprobe and fentry/fexit/raw_tp considered two separate 'things' (even though both of their purpose is tracing)?
Quoted 4 lines
It doesn't need this heavy stuff.
Also I suspect in follow up you'd be adding tracepoints to GPU code? Did you consider just leaving few__weakglobal functions in GPU code and let bpf progs attach to them as fentry?
There are already tracepoints in the GPU code. And I do like fentry way of doing things more but my head was very much focused on cgroup, and tracepoint/kprobe path seems to have something for it. I suspected this would be a bit too heavy after seeing the scalability discussion but I wasn't sure so I whip this up quickly to get some feedback (while learning more about perf/bpf/cgroup.)
Quoted 3 lines
I suspect the true hierarchical nature of bpf-cgroup framework isn't necessary. The bpf program itself can filter for given cgroup.
We havebpf_current_task_under_cgroup()and friends.
Is there a way to access cgroup local storage from a prog that is not attached to a bpf-cgroup? Although, I guess I can just store/read things using a map with the cg id as key. And with the bpf_get_current_ancestor_cgroup_id below I can just simulate the values being propagated if the hierarchy ends up being relevant. Then again, is there a way to atomically update multiple elements of a map?
I am trying to figure out how to support a multi-user multi-app
sharing use case (like user A given quota X and user B given quota Y with app 1 and 2 each having a quota assigned by A and app 8 and 9 each having quota assigned by B.) Is there some kind of 'lock' mechanism for me to keep quota 1,2,X in sync? (Same for 8,9,Y.)
Quoted 3 lines
I suggest to sprinkle
__weakempty funcs in GPU and see what you can do with it with fentry andbpf_current_task_under_cgroup. There is alsobpf_get_current_ancestor_cgroup_id().