From patchwork Tue Jun 4 20:02:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685838 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E66A14A0AD; Tue, 4 Jun 2024 20:02:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531368; cv=none; b=rtPQkkupOM5fj6YCIiYuei4D8NEv/EbYFp987YAR7SvfIK+H9bogLtLpGPHWqAsG2Rj+BgJJVZcYSVAFqZ7osbWFyTzMIhktscxuyxpBieUiZj5EFs2IZ5bphljMrDE9+niNaNUyFvx/piaBZPMHM3hCq9axh2BYrr62/NMR9K4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531368; c=relaxed/simple; bh=DhP26xmiCYD8Xa0bAOGtxlXv9XJ/IAF50228gAf7JVk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QDgZYpVYeSjuuFBQBXNHp8FN8GrGO2tRbFZ2igrJ75JE0qfqB34omwis8iC1bW0WVkr8HVIGJ/WVjJqadRif1hZCZkvC9Gj5bZexkFQKGsh6nSFdk8Okt3OZ6rnsL7h5c5x9cgSQ67X/KNDBHfu/z9d2QPgntlOrDduNwfbWZy8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HbfnaHg1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HbfnaHg1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C91D1C2BBFC; Tue, 4 Jun 2024 20:02:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531368; bh=DhP26xmiCYD8Xa0bAOGtxlXv9XJ/IAF50228gAf7JVk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HbfnaHg1p545hbvJbRJ0bfSqI36OKWuf/E+KolY5pCHfMofgY5CgLouZ4izkgDfAz CSLW5Fu1ziUyA7ySapYWU5ErtvL5Acfv8n8yYLnIskSxlEjJt3BEOPfVPVlG9IgDaI bjlcfpIFiVhr7hlNAwEYlavevUbdcoYuhtE9z1mDyYj6bATIyxes8F+oX9v4ALE0Ny dIyYQiZl1U2bQksmzS3au+IktCaSXUrt4xxZPzThdWKwBYbuuslLRjZMyXITrRhFDB KyMnu43r3sZxgBF8m7O+ST9VYyMW3GrUlOCJkcKA6qNVXdJiCVUIuzQyqxFwCeic2n brN4aV7VHqBIg== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 01/10] uprobe: Add session callbacks to uprobe_consumer Date: Tue, 4 Jun 2024 22:02:12 +0200 Message-ID: <20240604200221.377848-2-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adding new set of callbacks that are triggered on entry and return uprobe execution for the attached function. The session means that those callbacks are 'connected' in a way that allows to: - control execution of return callback from entry callback - share data between entry and return callbacks The session concept fits to our common use case where we do filtering on entry uprobe and based on the result we decide to run the return uprobe (or not). It's also convenient to share the data between session callbacks. The control of return uprobe execution is done via return value of the entry session callback, where 0 means to install and execute return uprobe, 1 means to not install. Current implementation has a restriction that allows to register only single consumer with session callbacks for a uprobe and also restricting standard callbacks consumers. Which means that there can be only single user of a uprobe (inode + offset) when session consumer is registered to it. This is because all registered consumers are executed when uprobe or return uprobe is hit and wihout additional layer (like fgraph's shadow stack) that would keep the state of the return callback, we have no way to find out which consumer should be executed. I'm not sure how big limitation this is for people, our current use case seems to be ok with that. Fixing this would be more complex/bigger change to uprobes, thoughts? Hence sending this as RFC to gather more opinions and feedback. Signed-off-by: Jiri Olsa --- include/linux/uprobes.h | 18 +++++++++++ kernel/events/uprobes.c | 69 +++++++++++++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 9 deletions(-) diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h index f46e0ca0169c..a2f2d5ac3cee 100644 --- a/include/linux/uprobes.h +++ b/include/linux/uprobes.h @@ -34,6 +34,12 @@ enum uprobe_filter_ctx { }; struct uprobe_consumer { + /* + * The handler callback return value controls removal of the uprobe. + * 0 on success, uprobe stays + * 1 on failure, remove the uprobe + * console warning for anything else + */ int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs); int (*ret_handler)(struct uprobe_consumer *self, unsigned long func, @@ -42,6 +48,17 @@ struct uprobe_consumer { enum uprobe_filter_ctx ctx, struct mm_struct *mm); + /* The handler_session callback return value controls execution of + * the return uprobe and ret_handler_session callback. + * 0 on success + * 1 on failure, DO NOT install/execute the return uprobe + * console warning for anything else + */ + int (*handler_session)(struct uprobe_consumer *self, struct pt_regs *regs, + unsigned long *data); + int (*ret_handler_session)(struct uprobe_consumer *self, unsigned long func, + struct pt_regs *regs, unsigned long *data); + struct uprobe_consumer *next; }; @@ -85,6 +102,7 @@ struct return_instance { unsigned long func; unsigned long stack; /* stack pointer */ unsigned long orig_ret_vaddr; /* original return address */ + unsigned long data; bool chained; /* true, if instance is nested */ struct return_instance *next; /* keep as stack */ diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 2c83ba776fc7..17b0771272a6 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -750,12 +750,32 @@ static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset, return uprobe; } -static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) +/* + * Make sure all the uprobe consumers have only one type of entry + * callback registered (either handler or handler_session) due to + * different return value actions. + */ +static int consumer_check(struct uprobe_consumer *curr, struct uprobe_consumer *uc) +{ + if (!curr) + return 0; + if (curr->handler_session || uc->handler_session) + return -EBUSY; + return 0; +} + +static int consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) { + int err; + down_write(&uprobe->consumer_rwsem); - uc->next = uprobe->consumers; - uprobe->consumers = uc; + err = consumer_check(uprobe->consumers, uc); + if (!err) { + uc->next = uprobe->consumers; + uprobe->consumers = uc; + } up_write(&uprobe->consumer_rwsem); + return err; } /* @@ -1114,6 +1134,21 @@ void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consume } EXPORT_SYMBOL_GPL(uprobe_unregister); +static int check_handler(struct uprobe_consumer *uc) +{ + /* Uprobe must have at least one set consumer. */ + if (!uc->handler && !uc->ret_handler && + !uc->handler_session && !uc->ret_handler_session) + return -1; + /* Session consumer is exclusive. */ + if (uc->handler && uc->handler_session) + return -1; + /* Session consumer must have both entry and return handler. */ + if (!!uc->handler_session != !!uc->ret_handler_session) + return -1; + return 0; +} + /* * __uprobe_register - register a probe * @inode: the file in which the probe has to be placed. @@ -1138,8 +1173,7 @@ static int __uprobe_register(struct inode *inode, loff_t offset, struct uprobe *uprobe; int ret; - /* Uprobe must have at least one set consumer */ - if (!uc->handler && !uc->ret_handler) + if (check_handler(uc)) return -EINVAL; /* copy_insn() uses read_mapping_page() or shmem_read_mapping_page() */ @@ -1173,11 +1207,14 @@ static int __uprobe_register(struct inode *inode, loff_t offset, down_write(&uprobe->register_rwsem); ret = -EAGAIN; if (likely(uprobe_is_active(uprobe))) { - consumer_add(uprobe, uc); + ret = consumer_add(uprobe, uc); + if (ret) + goto fail; ret = register_for_each_vma(uprobe, uc); if (ret) __uprobe_unregister(uprobe, uc); } + fail: up_write(&uprobe->register_rwsem); put_uprobe(uprobe); @@ -1853,7 +1890,7 @@ static void cleanup_return_instances(struct uprobe_task *utask, bool chained, utask->return_instances = ri; } -static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) +static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs, unsigned long data) { struct return_instance *ri; struct uprobe_task *utask; @@ -1909,6 +1946,7 @@ static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) ri->stack = user_stack_pointer(regs); ri->orig_ret_vaddr = orig_ret_vaddr; ri->chained = chained; + ri->data = data; utask->depth++; ri->next = utask->return_instances; @@ -2070,6 +2108,7 @@ static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) struct uprobe_consumer *uc; int remove = UPROBE_HANDLER_REMOVE; bool need_prep = false; /* prepare return uprobe, when needed */ + unsigned long data = 0; down_read(&uprobe->register_rwsem); for (uc = uprobe->consumers; uc; uc = uc->next) { @@ -2081,14 +2120,24 @@ static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) "bad rc=0x%x from %ps()\n", rc, uc->handler); } - if (uc->ret_handler) + if (uc->handler_session) { + rc = uc->handler_session(uc, regs, &data); + WARN(rc & ~UPROBE_HANDLER_MASK, + "bad rc=0x%x from %ps()\n", rc, uc->handler_session); + } + + if (uc->ret_handler || uc->ret_handler_session) need_prep = true; remove &= rc; } if (need_prep && !remove) - prepare_uretprobe(uprobe, regs); /* put bp at return */ + prepare_uretprobe(uprobe, regs, data); /* put bp at return */ + + /* remove uprobe only for non-session consumers */ + if (uprobe->consumers && remove) + remove &= !!uprobe->consumers->handler; if (remove && uprobe->consumers) { WARN_ON(!uprobe_is_active(uprobe)); @@ -2107,6 +2156,8 @@ handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs) for (uc = uprobe->consumers; uc; uc = uc->next) { if (uc->ret_handler) uc->ret_handler(uc, ri->func, regs); + if (uc->ret_handler_session) + uc->ret_handler_session(uc, ri->func, regs, &ri->data); } up_read(&uprobe->register_rwsem); } From patchwork Tue Jun 4 20:02:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685839 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D507114A0AD; Tue, 4 Jun 2024 20:03:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531382; cv=none; b=UgZyr8E28z839uLRXYn3GWSe+sMX4mrI5hwh3ROJH4O+1et/ILm6MV90BqCXWNdSoWQ1wTF1yUAqtsjAtnr8y++ti+9jo2wI0SQAOAvRN1ZHOYGMy/DGvp9SWxy6qfoB4PjEXWFT36UArWusyCRa3hJ01s/Fjl/J+ucFuoPqGOw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531382; c=relaxed/simple; bh=VWxqp+tYVmK0gZXdn9ueD+SAmOvJYQ6Mldxj9MWQ6BM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jvKOojpxzMCVf434ZADVsrUut6DyMxC5DQt5E3QtB7b0+1ZbTv4qutfQ6FeM8Pm7VsXbnTLvSuNY9PF3OI3Xf1INs7mN6fcyE4qO69rPBFgiPGcpUZQbs2YQvXzcpEVgB8D7oJaUu9ALCdST5OgYWwr9FCGCVuSGCZJnJxBZ/NA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FeKh8kvg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FeKh8kvg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A69A5C2BBFC; Tue, 4 Jun 2024 20:02:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531381; bh=VWxqp+tYVmK0gZXdn9ueD+SAmOvJYQ6Mldxj9MWQ6BM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FeKh8kvgKafkCCYct4p963mpZBZJN/mWofov6bmRzqlEKsGPdRyDlT+gYY9yGaO94 fP/Hn0eRIGs+p5g0rUbx9U5UC54AF3BJNulRaWY3eQZyXhJ6jEGW4C6LESqos0oI+E 6vYNYnZtrfTGpKg1N5Y6p2Ci0uMTMlH11yDXhCh61hiEbnQv6dULRHAwTD6x/a9HkK WK2Xz9UqDweSA993WD7ug1HX8zuUmk7K9qtgAtgBVn88Vb2Gd9FmhaJss2wj2lT3nA tJz1bx5COay+eYqbCf605I56ZhA9NjaD68u/Rx/qNWxXT2Q3whQMtHVhwZD6UcaqUx gZq9deTDA8H6Q== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 02/10] bpf: Add support for uprobe multi session attach Date: Tue, 4 Jun 2024 22:02:13 +0200 Message-ID: <20240604200221.377848-3-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adding support to attach bpf program for entry and return probe of the same function. This is common use case which at the moment requires to create two uprobe multi links. Adding new BPF_TRACE_UPROBE_SESSION attach type that instructs kernel to attach single link program to both entry and exit probe. It's possible to control execution of the bpf program on return probe simply by returning zero or non zero from the entry bpf program execution to execute or not the bpf program on return probe respectively. Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 9 ++++-- kernel/trace/bpf_trace.c | 50 +++++++++++++++++++++++++++++----- tools/include/uapi/linux/bpf.h | 1 + 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 25ea393cf084..b400f50e2c3c 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1116,6 +1116,7 @@ enum bpf_attach_type { BPF_NETKIT_PRIMARY, BPF_NETKIT_PEER, BPF_TRACE_KPROBE_SESSION, + BPF_TRACE_UPROBE_SESSION, __MAX_BPF_ATTACH_TYPE }; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 5070fa20d05c..71d279907a0c 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -4048,10 +4048,14 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI && attach_type != BPF_TRACE_UPROBE_MULTI) return -EINVAL; + if (prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION && + attach_type != BPF_TRACE_UPROBE_SESSION) + return -EINVAL; if (attach_type != BPF_PERF_EVENT && attach_type != BPF_TRACE_KPROBE_MULTI && attach_type != BPF_TRACE_KPROBE_SESSION && - attach_type != BPF_TRACE_UPROBE_MULTI) + attach_type != BPF_TRACE_UPROBE_MULTI && + attach_type != BPF_TRACE_UPROBE_SESSION) return -EINVAL; return 0; case BPF_PROG_TYPE_SCHED_CLS: @@ -5314,7 +5318,8 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr) else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI || attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION) ret = bpf_kprobe_multi_link_attach(attr, prog); - else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI) + else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI || + attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION) ret = bpf_uprobe_multi_link_attach(attr, prog); break; default: diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index f5154c051d2c..53b111c8e887 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1642,6 +1642,17 @@ static inline bool is_kprobe_session(const struct bpf_prog *prog) return prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION; } +static inline bool is_uprobe_multi(const struct bpf_prog *prog) +{ + return prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI || + prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION; +} + +static inline bool is_uprobe_session(const struct bpf_prog *prog) +{ + return prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION; +} + static const struct bpf_func_proto * kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1659,13 +1670,13 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_get_func_ip: if (is_kprobe_multi(prog)) return &bpf_get_func_ip_proto_kprobe_multi; - if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI) + if (is_uprobe_multi(prog)) return &bpf_get_func_ip_proto_uprobe_multi; return &bpf_get_func_ip_proto_kprobe; case BPF_FUNC_get_attach_cookie: if (is_kprobe_multi(prog)) return &bpf_get_attach_cookie_proto_kmulti; - if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI) + if (is_uprobe_multi(prog)) return &bpf_get_attach_cookie_proto_umulti; return &bpf_get_attach_cookie_proto_trace; default: @@ -3346,6 +3357,26 @@ uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, s return uprobe_prog_run(uprobe, func, regs); } +static int +uprobe_multi_link_handler_session(struct uprobe_consumer *con, struct pt_regs *regs, + unsigned long *data) +{ + struct bpf_uprobe *uprobe; + + uprobe = container_of(con, struct bpf_uprobe, consumer); + return uprobe_prog_run(uprobe, instruction_pointer(regs), regs); +} + +static int +uprobe_multi_link_ret_handler_session(struct uprobe_consumer *con, unsigned long func, + struct pt_regs *regs, unsigned long *data) +{ + struct bpf_uprobe *uprobe; + + uprobe = container_of(con, struct bpf_uprobe, consumer); + return uprobe_prog_run(uprobe, func, regs); +} + static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx) { struct bpf_uprobe_multi_run_ctx *run_ctx; @@ -3382,7 +3413,7 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr if (sizeof(u64) != sizeof(void *)) return -EOPNOTSUPP; - if (prog->expected_attach_type != BPF_TRACE_UPROBE_MULTI) + if (!is_uprobe_multi(prog)) return -EINVAL; flags = attr->link_create.uprobe_multi.flags; @@ -3460,10 +3491,15 @@ int bpf_uprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr uprobes[i].link = link; - if (flags & BPF_F_UPROBE_MULTI_RETURN) - uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler; - else - uprobes[i].consumer.handler = uprobe_multi_link_handler; + if (is_uprobe_session(prog)) { + uprobes[i].consumer.handler_session = uprobe_multi_link_handler_session; + uprobes[i].consumer.ret_handler_session = uprobe_multi_link_ret_handler_session; + } else { + if (flags & BPF_F_UPROBE_MULTI_RETURN) + uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler; + else + uprobes[i].consumer.handler = uprobe_multi_link_handler; + } if (pid) uprobes[i].consumer.filter = uprobe_multi_link_filter; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 25ea393cf084..b400f50e2c3c 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1116,6 +1116,7 @@ enum bpf_attach_type { BPF_NETKIT_PRIMARY, BPF_NETKIT_PEER, BPF_TRACE_KPROBE_SESSION, + BPF_TRACE_UPROBE_SESSION, __MAX_BPF_ATTACH_TYPE }; From patchwork Tue Jun 4 20:02:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685840 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE214149E08; Tue, 4 Jun 2024 20:03:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531395; cv=none; b=bGAhEURi20ayJrNvOc6MsGDd3xg9DJOKfJA3HzKe2L9v/4Exsc0fxyLJbKhO8d4xn6cHhWq+zsBS+f7mQPIGpDloeeXTFL7PFUUmvVraniMbBGgsoUTUlkTKwIHw5V/BGi7ce1huu2Aq4vvDKpPgmHzW2h1gVUN83VRiKvbV+do= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531395; c=relaxed/simple; bh=E3gFFQnUuz21l8S5zyXAsshddo/NRWyVXSgE9gUIth0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DyK0JVoldzMaKteVWJl0HGfPa67L9YVGKTvtLo59b3hLvyuiRIbjr2OtB/sWGco1y+b1YieS0aBvkXQ8ut0E14JXiof6X82RQsJdLrwBl+WA9EnEfnM6ArQYp/yYPMl+g6Q/yaluJ1ryj+3yMPFHXWM5W+G1E/CiYOro2MQq6Bg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RHXZ/hjq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RHXZ/hjq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60D40C2BBFC; Tue, 4 Jun 2024 20:03:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531394; bh=E3gFFQnUuz21l8S5zyXAsshddo/NRWyVXSgE9gUIth0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RHXZ/hjqnq8YzJ7xKFngyTERrSzaC73vAZ2Oixb/Z88JRX7kzp6a6iNY2OQFPLjsV QfoDjEd+XoBV78YiCxav4VMZ3OE2Vphx5jcm5OagIeNoJq1C+/tvLfslSWKtI5QCJo Qc385ThLJ3muo77HFi2MoZ7qv6Ay9OuwmfX+GTUCL3RU1s7RFv0B9q/R8FoxekdwCf aqbojtaKmMbs8Q26jT5UdnNzUo+1BZIUPhzu7ni0QID+27KwKzAE1N2kkmQ6f/Ea7Z NlCOTOZQhjZbYDSSswHNe4KdYtiq3TTpFjUIKrgTFGVMbKki3wAYurAsOly+InONXQ +z0CBa0dHYgiw== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 03/10] bpf: Add support for uprobe multi session context Date: Tue, 4 Jun 2024 22:02:14 +0200 Message-ID: <20240604200221.377848-4-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Placing bpf_session_run_ctx layer in between bpf_run_ctx and bpf_uprobe_multi_run_ctx, so the session data can be retrieved from uprobe_multi link. Plus granting session kfuncs access to uprobe session programs. Signed-off-by: Jiri Olsa --- kernel/trace/bpf_trace.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 53b111c8e887..4392807ee8d9 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3181,7 +3181,7 @@ struct bpf_uprobe_multi_link { }; struct bpf_uprobe_multi_run_ctx { - struct bpf_run_ctx run_ctx; + struct bpf_session_run_ctx session_ctx; unsigned long entry_ip; struct bpf_uprobe *uprobe; }; @@ -3294,10 +3294,15 @@ static const struct bpf_link_ops bpf_uprobe_multi_link_lops = { static int uprobe_prog_run(struct bpf_uprobe *uprobe, unsigned long entry_ip, - struct pt_regs *regs) + struct pt_regs *regs, + bool is_return, void *data) { struct bpf_uprobe_multi_link *link = uprobe->link; struct bpf_uprobe_multi_run_ctx run_ctx = { + .session_ctx = { + .is_return = is_return, + .data = data, + }, .entry_ip = entry_ip, .uprobe = uprobe, }; @@ -3316,7 +3321,7 @@ static int uprobe_prog_run(struct bpf_uprobe *uprobe, migrate_disable(); - old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); + old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx); err = bpf_prog_run(link->link.prog, regs); bpf_reset_run_ctx(old_run_ctx); @@ -3345,7 +3350,7 @@ uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs) struct bpf_uprobe *uprobe; uprobe = container_of(con, struct bpf_uprobe, consumer); - return uprobe_prog_run(uprobe, instruction_pointer(regs), regs); + return uprobe_prog_run(uprobe, instruction_pointer(regs), regs, false, NULL); } static int @@ -3354,7 +3359,7 @@ uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, s struct bpf_uprobe *uprobe; uprobe = container_of(con, struct bpf_uprobe, consumer); - return uprobe_prog_run(uprobe, func, regs); + return uprobe_prog_run(uprobe, func, regs, true, NULL); } static int @@ -3364,7 +3369,7 @@ uprobe_multi_link_handler_session(struct uprobe_consumer *con, struct pt_regs *r struct bpf_uprobe *uprobe; uprobe = container_of(con, struct bpf_uprobe, consumer); - return uprobe_prog_run(uprobe, instruction_pointer(regs), regs); + return uprobe_prog_run(uprobe, instruction_pointer(regs), regs, false, data); } static int @@ -3374,14 +3379,14 @@ uprobe_multi_link_ret_handler_session(struct uprobe_consumer *con, unsigned long struct bpf_uprobe *uprobe; uprobe = container_of(con, struct bpf_uprobe, consumer); - return uprobe_prog_run(uprobe, func, regs); + return uprobe_prog_run(uprobe, func, regs, true, data); } static u64 bpf_uprobe_multi_entry_ip(struct bpf_run_ctx *ctx) { struct bpf_uprobe_multi_run_ctx *run_ctx; - run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx); + run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, session_ctx.run_ctx); return run_ctx->entry_ip; } @@ -3389,7 +3394,7 @@ static u64 bpf_uprobe_multi_cookie(struct bpf_run_ctx *ctx) { struct bpf_uprobe_multi_run_ctx *run_ctx; - run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, run_ctx); + run_ctx = container_of(current->bpf_ctx, struct bpf_uprobe_multi_run_ctx, session_ctx.run_ctx); return run_ctx->uprobe->cookie; } @@ -3586,7 +3591,8 @@ static int bpf_kprobe_multi_filter(const struct bpf_prog *prog, u32 kfunc_id) if (!btf_id_set8_contains(&kprobe_multi_kfunc_set_ids, kfunc_id)) return 0; - if (!is_kprobe_session(prog)) + if (!is_kprobe_session(prog) && + !is_uprobe_session(prog)) return -EACCES; return 0; From patchwork Tue Jun 4 20:02:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685841 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC45A146D78; Tue, 4 Jun 2024 20:03:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531407; cv=none; b=XQG8YT2IdSsPg6iow458VgNZIhp9PrDd4kqA+IJZ2Y8HJORGqOdM8gLK5s5kxlO4qG7adaSNJ0iEvyDeFuvgMZSVmwg+Ysf+ZF2XHFUGZYQRWplbPg+IIbKzhqQFCZmKGiTlHokIYVzEGrFDZuYV3/zCkGiASeIdApWMBCMpGW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531407; c=relaxed/simple; bh=DggXebc+5AYH4oM8/iWPJWB6VFy1ERgOM6IPvJ6rIjs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GdapsL4AjmEWo/TDMK5A4lliHBFjtvhG/d0tp0zMl0HlmQWrzuwHYB/xskIklrNQazwQIexhzvuIt89JU1xkZZ4ARFXK2TQf2C5N5j1nF9rRPbd5EElaf5r2pIH468ZWK0eugQoXaJaJojajPYN501kihdioqmdPPG/KU6hPvIE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ILEvU0gT; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ILEvU0gT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D32F7C2BBFC; Tue, 4 Jun 2024 20:03:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531406; bh=DggXebc+5AYH4oM8/iWPJWB6VFy1ERgOM6IPvJ6rIjs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ILEvU0gTFvCYyzXJcUBLPA4oft6W+BmxISq3P/cmEVVan/6LPmsmuTLbkAhJbgWQn gIAeCrHBeykLM1cNbVWZcK6qKgTNNnWyCpcRTkYuPTRED6Ne6vcG0Zv1ao/BqxcG/2 E1eT/T6CMjmllRxsluhBxZr/DlnGkIiWqYUGMe7WwGO0gRlbuzGduycAPxnhXvZmsG Q6YyReZzxwZuMK+gGS9ZV26ycoQtJmQQp8m6VDqVRSVAbyUCGWcJOlv3WViGAymALC ALMHNn+nkghG0wl/CozwcDe3HDiPtGjvwTHHmCvqVgbyfN8h4Il36CGDp8PumztyMJ Jh5YUsNhjUKoA== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 04/10] libbpf: Add support for uprobe multi session attach Date: Tue, 4 Jun 2024 22:02:15 +0200 Message-ID: <20240604200221.377848-5-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adding support to attach program in uprobe session mode with bpf_program__attach_uprobe_multi function. Adding session bool to bpf_uprobe_multi_opts struct that allows to load and attach the bpf program via uprobe session. the attachment to create uprobe multi session. Also adding new program loader section that allows: SEC("uprobe.session/bpf_fentry_test*") and loads/attaches uprobe program as uprobe session. Signed-off-by: Jiri Olsa --- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/libbpf.c | 50 ++++++++++++++++++++++++++++++++++++++++-- tools/lib/bpf/libbpf.h | 4 +++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 2a4c71501a17..becdfa701c75 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -776,6 +776,7 @@ int bpf_link_create(int prog_fd, int target_fd, return libbpf_err(-EINVAL); break; case BPF_TRACE_UPROBE_MULTI: + case BPF_TRACE_UPROBE_SESSION: attr.link_create.uprobe_multi.flags = OPTS_GET(opts, uprobe_multi.flags, 0); attr.link_create.uprobe_multi.cnt = OPTS_GET(opts, uprobe_multi.cnt, 0); attr.link_create.uprobe_multi.path = ptr_to_u64(OPTS_GET(opts, uprobe_multi.path, 0)); diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index d1627a2ca30b..a0044448a708 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9328,6 +9328,7 @@ static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_ static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); static int attach_kprobe_session(const struct bpf_program *prog, long cookie, struct bpf_link **link); static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); +static int attach_uprobe_session(const struct bpf_program *prog, long cookie, struct bpf_link **link); static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link); static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link); @@ -9346,6 +9347,7 @@ static const struct bpf_sec_def section_defs[] = { SEC_DEF("kprobe.session+", KPROBE, BPF_TRACE_KPROBE_SESSION, SEC_NONE, attach_kprobe_session), SEC_DEF("uprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi), SEC_DEF("uretprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi), + SEC_DEF("uprobe.session+", KPROBE, BPF_TRACE_UPROBE_SESSION, SEC_NONE, attach_uprobe_session), SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi), SEC_DEF("uretprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi), SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), @@ -11682,6 +11684,40 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru return ret; } +static int attach_uprobe_session(const struct bpf_program *prog, long cookie, struct bpf_link **link) +{ + char *binary_path = NULL, *func_name = NULL; + LIBBPF_OPTS(bpf_uprobe_multi_opts, opts, + .session = true, + ); + int n, ret = -EINVAL; + const char *spec; + + *link = NULL; + + spec = prog->sec_name + sizeof("uprobe.session/") - 1; + n = sscanf(spec, "%m[^:]:%m[^\n]", + &binary_path, &func_name); + + switch (n) { + case 1: + /* but auto-attach is impossible. */ + ret = 0; + break; + case 2: + *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts); + ret = *link ? 0 : -errno; + break; + default: + pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name, + prog->sec_name); + break; + } + free(binary_path); + free(func_name); + return ret; +} + static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz, const char *binary_path, uint64_t offset) { @@ -11916,10 +11952,12 @@ bpf_program__attach_uprobe_multi(const struct bpf_program *prog, const unsigned long *ref_ctr_offsets = NULL, *offsets = NULL; LIBBPF_OPTS(bpf_link_create_opts, lopts); unsigned long *resolved_offsets = NULL; + enum bpf_attach_type attach_type; int err = 0, link_fd, prog_fd; struct bpf_link *link = NULL; char errmsg[STRERR_BUFSIZE]; char full_path[PATH_MAX]; + bool retprobe, session; const __u64 *cookies; const char **syms; size_t cnt; @@ -11990,12 +12028,20 @@ bpf_program__attach_uprobe_multi(const struct bpf_program *prog, offsets = resolved_offsets; } + retprobe = OPTS_GET(opts, retprobe, false); + session = OPTS_GET(opts, session, false); + + if (retprobe && session) + return libbpf_err_ptr(-EINVAL); + + attach_type = session ? BPF_TRACE_UPROBE_SESSION : BPF_TRACE_UPROBE_MULTI; + lopts.uprobe_multi.path = path; lopts.uprobe_multi.offsets = offsets; lopts.uprobe_multi.ref_ctr_offsets = ref_ctr_offsets; lopts.uprobe_multi.cookies = cookies; lopts.uprobe_multi.cnt = cnt; - lopts.uprobe_multi.flags = OPTS_GET(opts, retprobe, false) ? BPF_F_UPROBE_MULTI_RETURN : 0; + lopts.uprobe_multi.flags = retprobe ? BPF_F_UPROBE_MULTI_RETURN : 0; if (pid == 0) pid = getpid(); @@ -12009,7 +12055,7 @@ bpf_program__attach_uprobe_multi(const struct bpf_program *prog, } link->detach = &bpf_link__detach_fd; - link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &lopts); + link_fd = bpf_link_create(prog_fd, 0, attach_type, &lopts); if (link_fd < 0) { err = -errno; pr_warn("prog '%s': failed to attach multi-uprobe: %s\n", diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 26e4e35528c5..04cdb38f527f 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -569,10 +569,12 @@ struct bpf_uprobe_multi_opts { size_t cnt; /* create return uprobes */ bool retprobe; + /* create session kprobes */ + bool session; size_t :0; }; -#define bpf_uprobe_multi_opts__last_field retprobe +#define bpf_uprobe_multi_opts__last_field session /** * @brief **bpf_program__attach_uprobe_multi()** attaches a BPF program From patchwork Tue Jun 4 20:02:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685842 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 527A114B96B; Tue, 4 Jun 2024 20:03:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531418; cv=none; b=L7ZR5ajpmpFGXVC6xqSUPGXYTUqSnkjJZ/1KtRuMzclujppFz2YKPlthfNWBOn/5HPn/h+yHEBgbNIP+EVdLwxbVVW37BLk3//LF73EV5bcvGYrlbMf9Hg/ZG/a/O4NO9/UceQaetTOGETXnH7vo1oYUFPGWItp+Y+I7PAmHQj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531418; c=relaxed/simple; bh=OrDWdwFwBLOPijaiiAiolWQlzUYYleCq1cewcCwIhJ8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IVcH3UxN5XId6uRKbBO4yG3IBpZqFe5+j43rBqYOWGKob0W2UGnn+PU/eyIkrJZFVLhtYua6W19Y99WTAbnXFxFXs48kb0YN3XDB6+AtgwqMtFmCyFsUelyyWVQGSVd5pNAjPS8ed27ni1cke4PEwPU8An3kTs/JVrJmNxWDnJg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ehjPsO2l; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ehjPsO2l" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C42BC3277B; Tue, 4 Jun 2024 20:03:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531418; bh=OrDWdwFwBLOPijaiiAiolWQlzUYYleCq1cewcCwIhJ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ehjPsO2lhV7tnUjWUmMCssQDh6/Fd4Z+C5Rih1oLZ3VJTIrkxNTfS4Zh1qOM3bhWd Y3deZO9smgwc3vN/jjZps8X27DP548QFpTNLvR7nKP6r92QyyIAHJptVp3nZQuUOQ2 rH54R4xrmVsyGqC79kj5Yp2hK1QJGlef1lYesQrfmqIExc5tR5pGkwmAQQq8bAWReS uwqetWOXUF4uKTTmRyR5qzeeY5UTQqQiQ2Yt2QT42Ai9FnTeX34w9fSoIiTf6utmZy LjksAUZghB0aJkoij86zg2lACWW/vjVkgV2/EHrXJqblLvhMN5CMhdYNx520YTNXhk RaBvVS7TAWhKw== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 05/10] libbpf: Add uprobe session attach type names to attach_type_name Date: Tue, 4 Jun 2024 22:02:16 +0200 Message-ID: <20240604200221.377848-6-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adding uprobe session attach type name to attach_type_name, so libbpf_bpf_attach_type_str returns proper string name for BPF_TRACE_UPROBE_SESSION attach type. Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index a0044448a708..702c2fb7e4df 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -133,6 +133,7 @@ static const char * const attach_type_name[] = { [BPF_NETKIT_PRIMARY] = "netkit_primary", [BPF_NETKIT_PEER] = "netkit_peer", [BPF_TRACE_KPROBE_SESSION] = "trace_kprobe_session", + [BPF_TRACE_UPROBE_SESSION] = "trace_uprobe_session", }; static const char * const link_type_name[] = { From patchwork Tue Jun 4 20:02:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685843 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC08614B06E; Tue, 4 Jun 2024 20:03:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531429; cv=none; b=TN2DzwkbOPdcGRTF+KM5yt7MClldqo2EyrfcR9HBtOteCI8NOqm2tSOgKgRwAeEmJDiFeLWFxdmnebxtur7f+MVyQD6ulX9IE9L6SpF57nrut3IDWE2dOWzk0rU7YrwYMWM/OoJ2FYdlByRVGc58XfMAS6ftQ9W5t8Qumqhu+To= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531429; c=relaxed/simple; bh=LXAp5T3JbHohtYrWakMTM5QJ0Ikay0nc5iEdg8MoeQY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hKKOeNdOCTeEDznlXnA4fE6Yw6LiwN7iX/hPA4px62Yi43LzVE0qh72z+IeVvc3ZpFrOZtN7SXI1BufofGnSFj8Lkqpc4xlmOircXFDVA0zHOz8dk7Hgc/pHg3kQM7e8n/mHIM13kwaMrGa23xErGFDPrDYb9oe2GNeWqLGeevM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TZO19Vys; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TZO19Vys" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AF64C2BBFC; Tue, 4 Jun 2024 20:03:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531429; bh=LXAp5T3JbHohtYrWakMTM5QJ0Ikay0nc5iEdg8MoeQY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TZO19VysQh+6U9XFtHA9E63IW1p+AOA8KRqquXdTghTqUCfE5MI2/JTnnBxKD/cCi ICdaHEkOL0QXMVWLqpxaw4jPXLaw5KToWzjokUIS/B3ofBV9e6dOFqbv3y/jduECLF 5N+CXEaVJKhqx0Z+treF6rPg/uOyod4+2Szb/WmyqGDy73e2cu8iEufZePoVaAO6Z4 EAzoZDvQzult+ALOiGklEN0QBjLRLt9zZcwRYgmWwqeFVe5Id5KBmZRycDAxDqfybU iGmNCimljDP3s0UfG/ud16hkfkjoyX2rFmjsI5Ysw6ifkZdQ50Mkj2+0jPJZVC8/NY txvtf92sBqL8Q== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 06/10] selftests/bpf: Move ARRAY_SIZE to bpf_misc.h Date: Tue, 4 Jun 2024 22:02:17 +0200 Message-ID: <20240604200221.377848-7-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 ARRAY_SIZE is used on multiple places, move its definition in bpf_misc.h header. Signed-off-by: Jiri Olsa --- tools/testing/selftests/bpf/progs/bpf_misc.h | 2 ++ tools/testing/selftests/bpf/progs/iters.c | 2 -- tools/testing/selftests/bpf/progs/kprobe_multi_session.c | 3 +-- tools/testing/selftests/bpf/progs/linked_list.c | 5 +---- tools/testing/selftests/bpf/progs/netif_receive_skb.c | 5 +---- tools/testing/selftests/bpf/progs/profiler.inc.h | 5 +---- tools/testing/selftests/bpf/progs/setget_sockopt.c | 5 +---- tools/testing/selftests/bpf/progs/test_bpf_ma.c | 4 ---- tools/testing/selftests/bpf/progs/test_sysctl_loop1.c | 5 +---- tools/testing/selftests/bpf/progs/test_sysctl_loop2.c | 5 +---- tools/testing/selftests/bpf/progs/test_sysctl_prog.c | 5 +---- .../testing/selftests/bpf/progs/test_tcp_custom_syncookie.c | 1 + .../testing/selftests/bpf/progs/test_tcp_custom_syncookie.h | 2 -- .../testing/selftests/bpf/progs/verifier_subprog_precision.c | 2 -- 14 files changed, 11 insertions(+), 40 deletions(-) diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h index fb2f5513e29e..70f56af94513 100644 --- a/tools/testing/selftests/bpf/progs/bpf_misc.h +++ b/tools/testing/selftests/bpf/progs/bpf_misc.h @@ -135,4 +135,6 @@ /* make it look to compiler like value is read and written */ #define __sink(expr) asm volatile("" : "+g"(expr)) +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + #endif diff --git a/tools/testing/selftests/bpf/progs/iters.c b/tools/testing/selftests/bpf/progs/iters.c index fe65e0952a1e..16bdc3e25591 100644 --- a/tools/testing/selftests/bpf/progs/iters.c +++ b/tools/testing/selftests/bpf/progs/iters.c @@ -7,8 +7,6 @@ #include "bpf_misc.h" #include "bpf_compiler.h" -#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0])) - static volatile int zero = 0; int my_pid; diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session.c index bbba9eb46551..bd8b7fb7061e 100644 --- a/tools/testing/selftests/bpf/progs/kprobe_multi_session.c +++ b/tools/testing/selftests/bpf/progs/kprobe_multi_session.c @@ -4,8 +4,7 @@ #include #include #include "bpf_kfuncs.h" - -#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0])) +#include "bpf_misc.h" char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/progs/linked_list.c b/tools/testing/selftests/bpf/progs/linked_list.c index f69bf3e30321..421f40835acd 100644 --- a/tools/testing/selftests/bpf/progs/linked_list.c +++ b/tools/testing/selftests/bpf/progs/linked_list.c @@ -4,10 +4,7 @@ #include #include #include "bpf_experimental.h" - -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0])) -#endif +#include "bpf_misc.h" #include "linked_list.h" diff --git a/tools/testing/selftests/bpf/progs/netif_receive_skb.c b/tools/testing/selftests/bpf/progs/netif_receive_skb.c index c0062645fc68..9e067dcbf607 100644 --- a/tools/testing/selftests/bpf/progs/netif_receive_skb.c +++ b/tools/testing/selftests/bpf/progs/netif_receive_skb.c @@ -5,6 +5,7 @@ #include #include #include +#include "bpf_misc.h" #include @@ -23,10 +24,6 @@ bool skip = false; #define BADPTR 0 #endif -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -#endif - struct { __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); __uint(max_entries, 1); diff --git a/tools/testing/selftests/bpf/progs/profiler.inc.h b/tools/testing/selftests/bpf/progs/profiler.inc.h index 6957d9f2805e..8bd1ebd7d6af 100644 --- a/tools/testing/selftests/bpf/progs/profiler.inc.h +++ b/tools/testing/selftests/bpf/progs/profiler.inc.h @@ -9,6 +9,7 @@ #include "err.h" #include "bpf_experimental.h" #include "bpf_compiler.h" +#include "bpf_misc.h" #ifndef NULL #define NULL 0 @@ -133,10 +134,6 @@ struct { __uint(max_entries, 16); } disallowed_exec_inodes SEC(".maps"); -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof(arr[0])) -#endif - static INLINE bool IS_ERR(const void* ptr) { return IS_ERR_VALUE((unsigned long)ptr); diff --git a/tools/testing/selftests/bpf/progs/setget_sockopt.c b/tools/testing/selftests/bpf/progs/setget_sockopt.c index 7a438600ae98..60518aed1ffc 100644 --- a/tools/testing/selftests/bpf/progs/setget_sockopt.c +++ b/tools/testing/selftests/bpf/progs/setget_sockopt.c @@ -6,10 +6,7 @@ #include #include #include - -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -#endif +#include "bpf_misc.h" extern unsigned long CONFIG_HZ __kconfig; diff --git a/tools/testing/selftests/bpf/progs/test_bpf_ma.c b/tools/testing/selftests/bpf/progs/test_bpf_ma.c index 3494ca30fa7f..4a4e0b8d9b72 100644 --- a/tools/testing/selftests/bpf/progs/test_bpf_ma.c +++ b/tools/testing/selftests/bpf/progs/test_bpf_ma.c @@ -7,10 +7,6 @@ #include "bpf_experimental.h" #include "bpf_misc.h" -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -#endif - struct generic_map_value { void *data; }; diff --git a/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c b/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c index 7f74077d6622..548660e299a5 100644 --- a/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c +++ b/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c @@ -10,10 +10,7 @@ #include #include "bpf_compiler.h" - -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -#endif +#include "bpf_misc.h" /* tcp_mem sysctl has only 3 ints, but this test is doing TCP_MEM_LOOPS */ #define TCP_MEM_LOOPS 28 /* because 30 doesn't fit into 512 bytes of stack */ diff --git a/tools/testing/selftests/bpf/progs/test_sysctl_loop2.c b/tools/testing/selftests/bpf/progs/test_sysctl_loop2.c index 68a75436e8af..81249d119a8b 100644 --- a/tools/testing/selftests/bpf/progs/test_sysctl_loop2.c +++ b/tools/testing/selftests/bpf/progs/test_sysctl_loop2.c @@ -10,10 +10,7 @@ #include #include "bpf_compiler.h" - -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -#endif +#include "bpf_misc.h" /* tcp_mem sysctl has only 3 ints, but this test is doing TCP_MEM_LOOPS */ #define TCP_MEM_LOOPS 20 /* because 30 doesn't fit into 512 bytes of stack */ diff --git a/tools/testing/selftests/bpf/progs/test_sysctl_prog.c b/tools/testing/selftests/bpf/progs/test_sysctl_prog.c index efc3c61f7852..bbdd08764789 100644 --- a/tools/testing/selftests/bpf/progs/test_sysctl_prog.c +++ b/tools/testing/selftests/bpf/progs/test_sysctl_prog.c @@ -10,6 +10,7 @@ #include #include "bpf_compiler.h" +#include "bpf_misc.h" /* Max supported length of a string with unsigned long in base 10 (pow2 - 1). */ #define MAX_ULONG_STR_LEN 0xF @@ -17,10 +18,6 @@ /* Max supported length of sysctl value string (pow2). */ #define MAX_VALUE_STR_LEN 0x40 -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -#endif - const char tcp_mem_name[] = "net/ipv4/tcp_mem"; static __always_inline int is_tcp_mem(struct bpf_sysctl *ctx) { diff --git a/tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c b/tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c index c8e4553648bf..44ee0d037f95 100644 --- a/tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c +++ b/tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.c @@ -9,6 +9,7 @@ #include "bpf_kfuncs.h" #include "test_siphash.h" #include "test_tcp_custom_syncookie.h" +#include "bpf_misc.h" #define MAX_PACKET_OFF 0xffff diff --git a/tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h b/tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h index 29a6a53cf229..f8b1b7e68d2e 100644 --- a/tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h +++ b/tools/testing/selftests/bpf/progs/test_tcp_custom_syncookie.h @@ -7,8 +7,6 @@ #define __packed __attribute__((__packed__)) #define __force -#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) - #define swap(a, b) \ do { \ typeof(a) __tmp = (a); \ diff --git a/tools/testing/selftests/bpf/progs/verifier_subprog_precision.c b/tools/testing/selftests/bpf/progs/verifier_subprog_precision.c index 4a58e0398e72..6a6fad625f7e 100644 --- a/tools/testing/selftests/bpf/progs/verifier_subprog_precision.c +++ b/tools/testing/selftests/bpf/progs/verifier_subprog_precision.c @@ -8,8 +8,6 @@ #include "bpf_misc.h" #include <../../../tools/include/linux/filter.h> -#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) - int vals[] SEC(".data.vals") = {1, 2, 3, 4}; __naked __noinline __used From patchwork Tue Jun 4 20:02:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685844 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50E2D14B06E; Tue, 4 Jun 2024 20:04:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531441; cv=none; b=m78UTjjKC/OpN9zT3aiAdb9cYGILMfqohqEclZf6j7sUmGKuE7zIFPjiLPxQOB99WxwRlEvjyBKqzx6rjoIQrQQo/7aqUH5ADxmO4DsY0n8hjEqJSi/vLSJ9ik4mP36r0s0Nf78IW2XJ7J6/ipp+Vh0OqDOCV9P7IQ2ReF5uP1s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531441; c=relaxed/simple; bh=CmzoveDdgtuX9uW3EQCl2u70qoS5RT28wNQKn8Oddbs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=esTouQr6zdRQEErTkrio8GYj7kRPnHrIGJ9IIRgxJaSjmxS9ACyY+2M2L0TJAdHhrnwb5lQZYQIk+ptR+OUBc2pji37huUT3Ww93jbBwV+dk1Sfk3whzVF/6gbJrrkUdUsS09zq8KA+Bfua5eOj4eliE2oWOjKPayBHprZxTpJ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kGPFjltm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kGPFjltm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF06DC2BBFC; Tue, 4 Jun 2024 20:03:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531440; bh=CmzoveDdgtuX9uW3EQCl2u70qoS5RT28wNQKn8Oddbs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kGPFjltm7U/AN2d6nJR0mn/WkRcDPgVCPvnW00sd6+QQEmdbOX9jqGhDUopK9Bv5s 7RVRzJ1k+rFItowXnYQCRGzSAO44sjTrjcQghu2jeP4IWGqBSy4tE5hNoFslc7snKj w3ENm7ftW62vIXXaca0Ir6S5uWyYjrmGJuj9XNTX+Wbb0FlVoI7gIdGPWunI8PqXI2 azq2pZPuyMwL4fbxjAuDuG+mJ24VCdDNYsuggd5pAeKb6Rih1r71fCUvdWQOnNxW3y UETSHNmPf0dz0SfIEz1O369NRn/8WHyoHWIpQe84LrYkI3tGWYFK5gtVWYjPOl/s8c f7AsQ/5qqFSjw== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 07/10] selftests/bpf: Add uprobe session test Date: Tue, 4 Jun 2024 22:02:18 +0200 Message-ID: <20240604200221.377848-8-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adding uprobe session test and testing that the entry program return value controls execution of the return probe program. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/uprobe_multi_test.c | 38 ++++++++++++++ .../bpf/progs/uprobe_multi_session.c | 52 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_session.c diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c index 8269cdee33ae..fddca2597818 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -5,6 +5,7 @@ #include "uprobe_multi.skel.h" #include "uprobe_multi_bench.skel.h" #include "uprobe_multi_usdt.skel.h" +#include "uprobe_multi_session.skel.h" #include "bpf/libbpf_internal.h" #include "testing_helpers.h" @@ -497,6 +498,41 @@ static void test_link_api(void) __test_link_api(child); } +static void test_session_skel_api(void) +{ + struct uprobe_multi_session *skel = NULL; + LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); + struct bpf_link *link = NULL; + int err; + + skel = uprobe_multi_session__open_and_load(); + if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load")) + goto cleanup; + + skel->bss->pid = getpid(); + + err = uprobe_multi_session__attach(skel); + if (!ASSERT_OK(err, " uprobe_multi_session__attach")) + goto cleanup; + + /* trigger all probes */ + skel->bss->uprobe_multi_func_1_addr = (__u64) uprobe_multi_func_1; + skel->bss->uprobe_multi_func_2_addr = (__u64) uprobe_multi_func_2; + skel->bss->uprobe_multi_func_3_addr = (__u64) uprobe_multi_func_3; + + uprobe_multi_func_1(); + uprobe_multi_func_2(); + uprobe_multi_func_3(); + + ASSERT_EQ(skel->bss->uprobe_session_result[0], 1, "uprobe_multi_func_1_result"); + ASSERT_EQ(skel->bss->uprobe_session_result[1], 2, "uprobe_multi_func_1_result"); + ASSERT_EQ(skel->bss->uprobe_session_result[2], 1, "uprobe_multi_func_1_result"); + +cleanup: + bpf_link__destroy(link); + uprobe_multi_session__destroy(skel); +} + static void test_bench_attach_uprobe(void) { long attach_start_ns = 0, attach_end_ns = 0; @@ -585,4 +621,6 @@ void test_uprobe_multi_test(void) test_bench_attach_usdt(); if (test__start_subtest("attach_api_fails")) test_attach_api_fails(); + if (test__start_subtest("session")) + test_session_skel_api(); } diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi_session.c b/tools/testing/selftests/bpf/progs/uprobe_multi_session.c new file mode 100644 index 000000000000..b382d7d29475 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/uprobe_multi_session.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include "bpf_kfuncs.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +__u64 uprobe_multi_func_1_addr = 0; +__u64 uprobe_multi_func_2_addr = 0; +__u64 uprobe_multi_func_3_addr = 0; + +__u64 uprobe_session_result[3]; + +int pid = 0; + +static int uprobe_multi_check(void *ctx, bool is_return) +{ + const __u64 funcs[] = { + uprobe_multi_func_1_addr, + uprobe_multi_func_2_addr, + uprobe_multi_func_3_addr, + }; + unsigned int i; + __u64 addr; + + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 1; + + addr = bpf_get_func_ip(ctx); + + for (i = 0; i < ARRAY_SIZE(funcs); i++) { + if (funcs[i] == addr) { + uprobe_session_result[i]++; + break; + } + } + + if ((addr == uprobe_multi_func_1_addr) || + (addr == uprobe_multi_func_3_addr)) + return 1; + + return 0; +} + +SEC("uprobe.session//proc/self/exe:uprobe_multi_func_*") +int uprobe(struct pt_regs *ctx) +{ + return uprobe_multi_check(ctx, bpf_session_is_return()); +} From patchwork Tue Jun 4 20:02:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685845 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFE6214C591; Tue, 4 Jun 2024 20:04:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531452; cv=none; b=skASpARdZ/hka4NBss1X8O7MCQUdYOhzmDLgPKCjejrphncDnx/q2CHGFHUf7zG49o5At22SZcuW+mf4FIk9iV4ePCyB2iBeKAe80ShRA9ZEp/pFrc2QzmE60f1CXSnuuU9efi17x70iAT3ea5dUroILp5pTuPBi/luwHWZuRWY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531452; c=relaxed/simple; bh=Eyepo33ZgEJx5F82DIOiokqpWa7VYfHyqJCUfqfW9s4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TGQfO7WIscjK1MrMk6niQIkdtGZPvNeKiN87F8JKDQkTfAaBS2WiG64gJ5UDNbZoWlMpBpMljnS/uxLmfUzSpEYgxyTTw2qzXl3EhLNRqzhuu17dXIXkXdGpo3TvnVOi1p2Yxn+7gCFqEgPhhuDTVqtS5k/hp9YbhaV4FCSkOLk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=p/+697IZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="p/+697IZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5706AC2BBFC; Tue, 4 Jun 2024 20:04:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531452; bh=Eyepo33ZgEJx5F82DIOiokqpWa7VYfHyqJCUfqfW9s4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p/+697IZS5yZlx4HkhHOFsu+OT/bcBT6vV/4RBLyz0iaGRrXJGZpr9qJUsPnnnI5m 5//PVnlh6+yuA5/wq5GGAL0oaiRaMYFZEYTzHyRw14Kng85lq4gVuIRpj+zre0UOsF ke//8s7g7KG4w7xLhfvdqXp6J72HK3pQ4za6MelrVWjau/LbK0MT3LpJDLgPlge6sv Rc5vt+I0fIsrYEkh9qWAi76inXpFKGlvBqM2P4vUwJuLjPDeckpFe0mQZis13Pa6z2 02qgRTkXk9g8IzYPGHs0Bgi2a2A4/E6Cjs3y7DfGpKQ1U8szk24d8qysZXNY8a3FiG Z/duCKPOhVGGg== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 08/10] selftests/bpf: Add uprobe session errors test Date: Tue, 4 Jun 2024 22:02:19 +0200 Message-ID: <20240604200221.377848-9-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adding uprobe session test to check that just single session instance is allowed or single uprobe. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/uprobe_multi_test.c | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c index fddca2597818..4bff681f0d7d 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -533,6 +533,31 @@ static void test_session_skel_api(void) uprobe_multi_session__destroy(skel); } +static void test_session_error_multiple_instances(void) +{ + struct uprobe_multi_session *skel_1 = NULL, *skel_2 = NULL; + int err; + + skel_1 = uprobe_multi_session__open_and_load(); + if (!ASSERT_OK_PTR(skel_1, "fentry_raw_skel_load")) + goto cleanup; + + err = uprobe_multi_session__attach(skel_1); + if (!ASSERT_OK(err, " kprobe_multi_session__attach")) + goto cleanup; + + skel_2 = uprobe_multi_session__open_and_load(); + if (!ASSERT_OK_PTR(skel_2, "fentry_raw_skel_load")) + goto cleanup; + + err = uprobe_multi_session__attach(skel_2); + ASSERT_EQ(err, -EBUSY, " kprobe_multi_session__attach"); + +cleanup: + uprobe_multi_session__destroy(skel_1); + uprobe_multi_session__destroy(skel_2); +} + static void test_bench_attach_uprobe(void) { long attach_start_ns = 0, attach_end_ns = 0; @@ -623,4 +648,6 @@ void test_uprobe_multi_test(void) test_attach_api_fails(); if (test__start_subtest("session")) test_session_skel_api(); + if (test__start_subtest("session_errors")) + test_session_error_multiple_instances(); } From patchwork Tue Jun 4 20:02:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685846 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6215F14B097; Tue, 4 Jun 2024 20:04:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531464; cv=none; b=fMfI+hvjk62mi4Wc+JnL2WJ+RVYJaSUTxrL8O83lqpCyqcbtSPsnoq5vPSzb8eS3tAlehnbivdg/BUX/24vbV4lXP92C0qRAPp/epkPzVXddMbpvPMpcHLFif4TYsf/SKWuq1BcJ9UzVC44Pm//+lEhRu3pW3QalWnWhDCmgv3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531464; c=relaxed/simple; bh=hu7KQGmdhNivcXLEk2nPk2e4zfC0G+cAgMVE0tYtsfg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rOyK4XD4aAQFg+DkpjPIhQVDgS5hoTL7chLYWXzrVhRvyxhWMebQi+zCfkrRjv5kYYK5TJr56iDWCZqcJyvcYPHQPAV/9pYOdpQsGGssjaJFfzp1VElf0U3Z0Fw4KIacZG6K94JsCtURuaVgacV8UcpF0ztOu1jGLSDyrTeF3H4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A27RG7p0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="A27RG7p0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2463C3277B; Tue, 4 Jun 2024 20:04:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531464; bh=hu7KQGmdhNivcXLEk2nPk2e4zfC0G+cAgMVE0tYtsfg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A27RG7p0Ut4hS8633QUIShMZWre3yWrMVdDj32CeBfvfHNHTChdE0xeZyeftqNNlS svhEuye941yg/qlaFWkXL7i+gDayzw5dJH4l4qKJPHsHv4GJdAx9Y1Bl97d+pkrALT waFQQ5O0nZQRdSzXRtSDFkJgdpbagwZ6q/URHeSCzys/g5lzeVA/3QmLNRvCI7wHQl /UzBPVqP6PL48UoDYKrU4tPLjnKVIfJEHFu0uSVpJH/ZR9QUe7Xhnt7VyYa1tnZFWD UPid2GcHQG6PfXrAf1Ugay5LwIdhIXOSOamr5kErSz/P/MfFLC+FhrRgCJMNshKRbs R7h8QjVN3X7MQ== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 09/10] selftests/bpf: Add uprobe session cookie test Date: Tue, 4 Jun 2024 22:02:20 +0200 Message-ID: <20240604200221.377848-10-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adding uprobe session test that verifies the cookie value get properly propagated from entry to return program. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/uprobe_multi_test.c | 31 ++++++++++++ .../bpf/progs/uprobe_multi_session_cookie.c | 50 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_session_cookie.c diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c index 4bff681f0d7d..34671253e130 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -6,6 +6,7 @@ #include "uprobe_multi_bench.skel.h" #include "uprobe_multi_usdt.skel.h" #include "uprobe_multi_session.skel.h" +#include "uprobe_multi_session_cookie.skel.h" #include "bpf/libbpf_internal.h" #include "testing_helpers.h" @@ -558,6 +559,34 @@ static void test_session_error_multiple_instances(void) uprobe_multi_session__destroy(skel_2); } +static void test_session_cookie_skel_api(void) +{ + struct uprobe_multi_session_cookie *skel = NULL; + int err; + + skel = uprobe_multi_session_cookie__open_and_load(); + if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load")) + goto cleanup; + + skel->bss->pid = getpid(); + + err = uprobe_multi_session_cookie__attach(skel); + if (!ASSERT_OK(err, " kprobe_multi_session__attach")) + goto cleanup; + + /* trigger all probes */ + uprobe_multi_func_1(); + uprobe_multi_func_2(); + uprobe_multi_func_3(); + + ASSERT_EQ(skel->bss->test_uprobe_1_result, 1, "test_uprobe_1_result"); + ASSERT_EQ(skel->bss->test_uprobe_2_result, 2, "test_uprobe_2_result"); + ASSERT_EQ(skel->bss->test_uprobe_3_result, 3, "test_uprobe_3_result"); + +cleanup: + uprobe_multi_session_cookie__destroy(skel); +} + static void test_bench_attach_uprobe(void) { long attach_start_ns = 0, attach_end_ns = 0; @@ -650,4 +679,6 @@ void test_uprobe_multi_test(void) test_session_skel_api(); if (test__start_subtest("session_errors")) test_session_error_multiple_instances(); + if (test__start_subtest("session_cookie")) + test_session_cookie_skel_api(); } diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi_session_cookie.c b/tools/testing/selftests/bpf/progs/uprobe_multi_session_cookie.c new file mode 100644 index 000000000000..ea41503fad18 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/uprobe_multi_session_cookie.c @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include "bpf_kfuncs.h" + +char _license[] SEC("license") = "GPL"; + +int pid = 0; + +__u64 test_uprobe_1_result = 0; +__u64 test_uprobe_2_result = 0; +__u64 test_uprobe_3_result = 0; + +static int check_cookie(__u64 val, __u64 *result) +{ + long *cookie; + + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 1; + + cookie = bpf_session_cookie(); + + if (bpf_session_is_return()) { + *result = *cookie == val ? val : 0; + } else { + *cookie = val; + } + + return 0; +} + +SEC("uprobe.session//proc/self/exe:uprobe_multi_func_1") +int uprobe_1(struct pt_regs *ctx) +{ + return check_cookie(1, &test_uprobe_1_result); +} + +SEC("uprobe.session//proc/self/exe:uprobe_multi_func_2") +int uprobe_2(struct pt_regs *ctx) +{ + return check_cookie(2, &test_uprobe_2_result); +} + +SEC("uprobe.session//proc/self/exe:uprobe_multi_func_3") +int uprobe_3(struct pt_regs *ctx) +{ + return check_cookie(3, &test_uprobe_3_result); +} From patchwork Tue Jun 4 20:02:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13685847 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21E7414B061; Tue, 4 Jun 2024 20:04:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531476; cv=none; b=kY02hPtxJml6LlbqsSxG7aK5o6g/rg7YniFRqNlg8HH0QneLD9NoF2Xh8QB2GZMwXCJXQDByk+qd4XPU9NbD1gbpKDD1WOHS/dkjpTVorNm7TNdaDewf86qt6Mb/FYOqNtUfViWdssgxT8OnxGYNcKOT2utQ/nE8ucozgkRku+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717531476; c=relaxed/simple; bh=TMgn7zTEGzLC10FmODgIHZC1q/TA7cHJbeHkzDcrUJo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MoH/Fve0KqJGfJXLXrDuBy0tUBCZvSfKYQNt6cqOzkxWLO+ytW7lUgrNVnx8NRxYtWWe2m1R+obglCbdffppq6ZmCXdqK1SH+oETCPZhyOjRjBXgJb7VO67wgyNwYwd9h7YyPFuY3/0wYbBzlYq7HUQMIugvEMMi05C0xk8i11k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=szzlgn//; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="szzlgn//" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EA1BC2BBFC; Tue, 4 Jun 2024 20:04:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717531475; bh=TMgn7zTEGzLC10FmODgIHZC1q/TA7cHJbeHkzDcrUJo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=szzlgn//rhriiVwGMiESgp+pG34SMvZuLTTEd5+ZaU29XJbvqzIQfMtQJqJiPC8sG kqEa8C0w4mcHqyMpeGnJH5JHkLch2S9p+Ms4lavBg2MRvp7sp59qQyW+pAwo5G1vyA auD/2RQAJfbEpUSlwmw5saLZ7v/H9W4uKX5poF2luHUwxzswQ3bYmH2MmePdGxUrpc q4ywXjUuWBmHayDnO0tdTM+iIvHznUnHdXQkoFqRKpeFG39f5ooEk0ujQhHebvEGtV jkUHrYvaVld2NXxwqm6Wu3gI66X9hzE/cRzKpJHijPL9xM4zK2349CS6MuyD0Ip61h JFZeaJxfBiWCA== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RFC bpf-next 10/10] selftests/bpf: Add uprobe session recursive test Date: Tue, 4 Jun 2024 22:02:21 +0200 Message-ID: <20240604200221.377848-11-jolsa@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240604200221.377848-1-jolsa@kernel.org> References: <20240604200221.377848-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adding uprobe session test that verifies the cookie value is stored properly when single uprobe-ed function is executed recursively. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/uprobe_multi_test.c | 57 +++++++++++++++++++ .../progs/uprobe_multi_session_recursive.c | 44 ++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c index 34671253e130..cdd7c327e16d 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -7,6 +7,7 @@ #include "uprobe_multi_usdt.skel.h" #include "uprobe_multi_session.skel.h" #include "uprobe_multi_session_cookie.skel.h" +#include "uprobe_multi_session_recursive.skel.h" #include "bpf/libbpf_internal.h" #include "testing_helpers.h" @@ -27,6 +28,12 @@ noinline void uprobe_multi_func_3(void) asm volatile (""); } +noinline void uprobe_session_recursive(int i) +{ + if (i) + uprobe_session_recursive(i - 1); +} + struct child { int go[2]; int pid; @@ -587,6 +594,54 @@ static void test_session_cookie_skel_api(void) uprobe_multi_session_cookie__destroy(skel); } +static void test_session_recursive_skel_api(void) +{ + struct uprobe_multi_session_recursive *skel = NULL; + int i, err; + + skel = uprobe_multi_session_recursive__open_and_load(); + if (!ASSERT_OK_PTR(skel, "uprobe_multi_session_recursive__open_and_load")) + goto cleanup; + + skel->bss->pid = getpid(); + + err = uprobe_multi_session_recursive__attach(skel); + if (!ASSERT_OK(err, "uprobe_multi_session_recursive__attach")) + goto cleanup; + + for (i = 0; i < ARRAY_SIZE(skel->bss->test_uprobe_cookie_entry); i++) + skel->bss->test_uprobe_cookie_entry[i] = i + 1; + + uprobe_session_recursive(5); + + /* + * entry uprobe: + * uprobe_session_recursive(5) { *cookie = 1, return 0 + * uprobe_session_recursive(4) { *cookie = 2, return 1 + * uprobe_session_recursive(3) { *cookie = 3, return 0 + * uprobe_session_recursive(2) { *cookie = 4, return 1 + * uprobe_session_recursive(1) { *cookie = 5, return 0 + * uprobe_session_recursive(0) { *cookie = 6, return 1 + * return uprobe: + * } i = 0 not executed + * } i = 1 test_uprobe_cookie_return[0] = 5 + * } i = 2 not executed + * } i = 3 test_uprobe_cookie_return[1] = 3 + * } i = 4 not executed + * } i = 5 test_uprobe_cookie_return[2] = 1 + */ + + ASSERT_EQ(skel->bss->idx_entry, 6, "idx_entry"); + ASSERT_EQ(skel->bss->idx_return, 3, "idx_return"); + + ASSERT_EQ(skel->bss->test_uprobe_cookie_return[0], 5, "test_uprobe_cookie_return[0]"); + ASSERT_EQ(skel->bss->test_uprobe_cookie_return[1], 3, "test_uprobe_cookie_return[1]"); + ASSERT_EQ(skel->bss->test_uprobe_cookie_return[2], 1, "test_uprobe_cookie_return[2]"); + +cleanup: + uprobe_multi_session_recursive__destroy(skel); +} + static void test_bench_attach_uprobe(void) { long attach_start_ns = 0, attach_end_ns = 0; @@ -681,4 +736,6 @@ void test_uprobe_multi_test(void) test_session_error_multiple_instances(); if (test__start_subtest("session_cookie")) test_session_cookie_skel_api(); + if (test__start_subtest("session_cookie_recursive")) + test_session_recursive_skel_api(); } diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c b/tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c new file mode 100644 index 000000000000..7babc180c28f --- /dev/null +++ b/tools/testing/selftests/bpf/progs/uprobe_multi_session_recursive.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include "bpf_kfuncs.h" +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +int pid = 0; + +int idx_entry = 0; +int idx_return = 0; + +__u64 test_uprobe_cookie_entry[6]; +__u64 test_uprobe_cookie_return[3]; + +static int check_cookie(void) +{ + long *cookie = bpf_session_cookie(); + + if (bpf_session_is_return()) { + if (idx_return >= ARRAY_SIZE(test_uprobe_cookie_return)) + return 1; + test_uprobe_cookie_return[idx_return++] = *cookie; + return 0; + } + + if (idx_entry >= ARRAY_SIZE(test_uprobe_cookie_entry)) + return 1; + *cookie = test_uprobe_cookie_entry[idx_entry]; + return idx_entry++ % 2; +} + + +SEC("uprobe.session//proc/self/exe:uprobe_session_recursive") +int uprobe_recursive(struct pt_regs *ctx) +{ + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 1; + + return check_cookie(); +}