Change get_modules_for_addrs to add modules to struct modules_array object provided by the caller instead of using its own, and return just zero or error. On error all the modules in the array are released. It will be used to store modules with inlined instances together with modules of the functions attached with fprobe in following change. There's no functional change intended. Signed-off-by: Jiri Olsa --- kernel/trace/bpf_trace.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index a3ac65f2e00c..bd413d45ef2c 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2872,9 +2872,12 @@ static bool has_module(struct modules_array *arr, struct module *mod) return false; } -static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u32 addrs_cnt) +/* + * Add modules for @addrs to @arr. On error all the modules in @arr + * are released. + */ +static int get_modules_for_addrs(struct modules_array *arr, unsigned long *addrs, u32 addrs_cnt) { - struct modules_array arr = {}; u32 i, err = 0; for (i = 0; i < addrs_cnt; i++) { @@ -2884,7 +2887,7 @@ static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u3 scoped_guard(rcu) { mod = __module_address(addrs[i]); /* Either no module or it's already stored */ - if (!mod || has_module(&arr, mod)) { + if (!mod || has_module(arr, mod)) { skip_add = true; break; /* scoped_guard */ } @@ -2895,23 +2898,19 @@ static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u3 continue; if (err) break; - err = add_module(&arr, mod); + err = add_module(arr, mod); if (err) { module_put(mod); break; } } - /* We return either err < 0 in case of error, ... */ if (err) { - kprobe_multi_put_modules(arr.mods, arr.mods_cnt); - kfree(arr.mods); - return err; + kprobe_multi_put_modules(arr->mods, arr->mods_cnt); + kfree(arr->mods); + memset(arr, 0, sizeof(*arr)); } - - /* or number of modules found if everything is ok. */ - *mods = arr.mods; - return arr.mods_cnt; + return err; } static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt) @@ -3188,6 +3187,7 @@ static int inline_sites_register(struct bpf_kprobe_multi_link *link) int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) { struct inline_sites inlines = {}; + struct modules_array mods = {}; struct bpf_kprobe_multi_link *link = NULL; struct bpf_link_primer link_primer; void __user *ucookies; @@ -3359,10 +3359,11 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr link); } - err = get_modules_for_addrs(&link->mods, addrs, cnt); - if (err < 0) + err = get_modules_for_addrs(&mods, addrs, cnt); + if (err) goto error_cleanup; - link->mods_cnt = err; + link->mods = mods.mods; + link->mods_cnt = mods.mods_cnt; if (inlines.sites_cnt) { err = inline_sites_register(link); @@ -3388,6 +3389,8 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr return err; error: + kprobe_multi_put_modules(mods.mods, mods.mods_cnt); + kfree(mods.mods); kfree(link); kvfree(addrs); kvfree(cookies); -- 2.56.1