From patchwork Sun Sep 29 20:57:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13815239 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 3C83F18EB0; Sun, 29 Sep 2024 20:57:36 +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=1727643457; cv=none; b=ERMIdCgyH4039N0bvKYtzHegMnTkDrGSTGa0dGZ4jOp7IhONWP2CU9R3yflnAOBjs8cITWjBB6Gg3RQBxqoGvTxapZLuApibkyzu0l5tyOmtUFOtyiZAyNDLBcbzAVhJYO/FvYn1oj/4mSQ/sxpQWA7mU6fPbPzR4iuagiGJKqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643457; c=relaxed/simple; bh=UdjtvmBJEuA72j3JoZh2o4f116ut1IgN0CDDTK9Tvss=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RpIYL8S9fqJfQMvqLckgK7bBvFT3+qM9QclWwKX7+HfetF3I+Kzjbj2qqJzOdXSQBy+tl6oSJWQHOoT+4sN4tW71Ii893vudqi5aFdMXNNdDPWzuVCLXFRvJQrVoMIXZdkJp3M3grzRRlqYene0LR8uAo5EuLK27GODyZ+aRHH0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PL8LRKxR; 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="PL8LRKxR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 558CEC4CEC5; Sun, 29 Sep 2024 20:57:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643456; bh=UdjtvmBJEuA72j3JoZh2o4f116ut1IgN0CDDTK9Tvss=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PL8LRKxRfP5BbhdJ0WQWAIHdICrxRZ9p41+RVEhdPZvju8T3X6o8uiqRJ/8SEBX8P l2LJ8oB3pQypc6nFU86tCohLaegvgTTmMEOs9Jw+xdCBoyWLB6EA6PFQYyOz4LPDd6 sMVRZ+ifjEnj+v0DV9XMiK7Yr1RrKKBOZywGNzDLzJUJ5vdi+GzL3/31HBLTllsQml /X+onDiZh2ib9dC5MtvorAIm+uTTXWwKL5OdXdsQXLwOy76s8Vsf/+JmX2sQDgWLa7 ZKBX4SEABdJ4SWJQ5k6d/8i6+39TxUe8V9HX5INd7A+wT6JN24eHYMLNzmzLITkSa3 S7To1heoyjhHA== 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: [PATCHv5 bpf-next 01/13] uprobe: Add data pointer to consumer handlers Date: Sun, 29 Sep 2024 22:57:05 +0200 Message-ID: <20240929205717.3813648-2-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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 data pointer to both entry and exit consumer handlers and all its users. The functionality itself is coming in following change. Signed-off-by: Jiri Olsa Acked-by: Oleg Nesterov Acked-by: Andrii Nakryiko --- include/linux/uprobes.h | 4 ++-- kernel/events/uprobes.c | 4 ++-- kernel/trace/bpf_trace.c | 6 ++++-- kernel/trace/trace_uprobe.c | 12 ++++++++---- .../testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 2 +- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h index 2b294bf1881f..bb265a632b91 100644 --- a/include/linux/uprobes.h +++ b/include/linux/uprobes.h @@ -37,10 +37,10 @@ struct uprobe_consumer { * for the current process. If filter() is omitted or returns true, * UPROBE_HANDLER_REMOVE is effectively ignored. */ - int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs); + int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs, __u64 *data); int (*ret_handler)(struct uprobe_consumer *self, unsigned long func, - struct pt_regs *regs); + struct pt_regs *regs, __u64 *data); bool (*filter)(struct uprobe_consumer *self, struct mm_struct *mm); struct list_head cons_node; diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 2ec796e2f055..2ba93f8a31aa 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -2139,7 +2139,7 @@ static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) int rc = 0; if (uc->handler) { - rc = uc->handler(uc, regs); + rc = uc->handler(uc, regs, NULL); WARN(rc & ~UPROBE_HANDLER_MASK, "bad rc=0x%x from %ps()\n", rc, uc->handler); } @@ -2179,7 +2179,7 @@ handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs) list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node, srcu_read_lock_held(&uprobes_srcu)) { if (uc->ret_handler) - uc->ret_handler(uc, ri->func, regs); + uc->ret_handler(uc, ri->func, regs, NULL); } srcu_read_unlock(&uprobes_srcu, srcu_idx); } diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index a582cd25ca87..fdab7ecd8dfa 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3244,7 +3244,8 @@ uprobe_multi_link_filter(struct uprobe_consumer *con, struct mm_struct *mm) } static int -uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs) +uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs, + __u64 *data) { struct bpf_uprobe *uprobe; @@ -3253,7 +3254,8 @@ uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs) } static int -uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs) +uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, struct pt_regs *regs, + __u64 *data) { struct bpf_uprobe *uprobe; diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c index f7443e996b1b..11103dde897b 100644 --- a/kernel/trace/trace_uprobe.c +++ b/kernel/trace/trace_uprobe.c @@ -88,9 +88,11 @@ static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev) static int register_uprobe_event(struct trace_uprobe *tu); static int unregister_uprobe_event(struct trace_uprobe *tu); -static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs); +static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs, + __u64 *data); static int uretprobe_dispatcher(struct uprobe_consumer *con, - unsigned long func, struct pt_regs *regs); + unsigned long func, struct pt_regs *regs, + __u64 *data); #ifdef CONFIG_STACK_GROWSUP static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) @@ -1500,7 +1502,8 @@ trace_uprobe_register(struct trace_event_call *event, enum trace_reg type, } } -static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) +static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs, + __u64 *data) { struct trace_uprobe *tu; struct uprobe_dispatch_data udd; @@ -1530,7 +1533,8 @@ static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) } static int uretprobe_dispatcher(struct uprobe_consumer *con, - unsigned long func, struct pt_regs *regs) + unsigned long func, struct pt_regs *regs, + __u64 *data) { struct trace_uprobe *tu; struct uprobe_dispatch_data udd; diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index 8835761d9a12..12005e3dc3e4 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -461,7 +461,7 @@ static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = { static int uprobe_ret_handler(struct uprobe_consumer *self, unsigned long func, - struct pt_regs *regs) + struct pt_regs *regs, __u64 *data) { regs->ax = 0x12345678deadbeef; From patchwork Sun Sep 29 20:57:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13815240 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 5D38513C3EE; Sun, 29 Sep 2024 20:57:50 +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=1727643470; cv=none; b=bdYJ2dSJQ3/lPwvS6CA5fxqssmopeht9iD4j3iFknGg6TKnfcQued6LeYJbx5RVjp/K1OT2jCJ6bpVk9F1EAUNu1XWyF/mvcTq7jJAidSv+64f0FuYE9aGMNpo21eIISpVTbl9VAmunFIvajSVTZIzOXmi3CUjlTpsfKOrThP24= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643470; c=relaxed/simple; bh=ubdVoXUcNMJ32NluaZK9m/9+tu736MJgye8617L0U/E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JC/ceMwfoiK3Xn4wvr2oyy62lwNK1RIfmNQihiWlu4J+0+qUbHa7sU0O6cpBJh8bwstr/ffbTXdXT+YFvEUULa5eO7+QXfrCvAey09AttAJ8ZTL7eN+q3R/SKacMS/9Dlt6fZxscP3JSiHH0niS0SQInmjxDPmtzqsPxf3FeS3U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L57En9IH; 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="L57En9IH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35823C4CEC5; Sun, 29 Sep 2024 20:57:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643470; bh=ubdVoXUcNMJ32NluaZK9m/9+tu736MJgye8617L0U/E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L57En9IHYq/YOXo9OrvIrnVyAy8Xf0RJg2QEraitCSqMKehwy3cjycV4vgkJyD8Wi owtmHsOIslRDchFgTQ8jBHqc3aSCDhMRC3+BHGhCe5QgPCIGz5RNLWckt6ZULLWvAB KLhFjwRd9kwZwlYS8w6UAXfepBqhIdLMLKiptqoaTizy68rqNCdm60u51gdvIrQkbn CInony/Fdjn87LoaLIE/tkIZHP0dDDqC0xSFl1kbOQo4j6s9RrhNHxNRqZAoWZeR9H 71w29UVJozRq5nYRxmTLqZSbG3V5fHxh0M3ZbD1sIk3dYBF/AlFevGbXDC47pV4qMT 05BfASbXtEHqQ== 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: [PATCHv5 bpf-next 02/13] uprobe: Add support for session consumer Date: Sun, 29 Sep 2024 22:57:06 +0200 Message-ID: <20240929205717.3813648-3-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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 This change allows the uprobe consumer to behave as session which means that 'handler' and 'ret_handler' callbacks are connected in a way that allows to: - control execution of 'ret_handler' from 'handler' callback - share data between 'handler' and 'ret_handler' 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. To achive this we are adding new return value the uprobe consumer can return from 'handler' callback: UPROBE_HANDLER_IGNORE - Ignore 'ret_handler' callback for this consumer. And store cookie and pass it to 'ret_handler' when consumer has both 'handler' and 'ret_handler' callbacks defined. We store shared data in the return_consumer object array as part of the return_instance object. This way the handle_uretprobe_chain can find related return_consumer and its shared data. We also store entry handler return value, for cases when there are multiple consumers on single uprobe and some of them are ignored and some of them not, in which case the return probe gets installed and we need to have a way to find out which consumer needs to be ignored. The tricky part is when consumer is registered 'after' the uprobe entry handler is hit. In such case this consumer's 'ret_handler' gets executed as well, but it won't have the proper data pointer set, so we can filter it out. Suggested-by: Oleg Nesterov Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- include/linux/uprobes.h | 21 +++++- kernel/events/uprobes.c | 148 +++++++++++++++++++++++++++++++--------- 2 files changed, 137 insertions(+), 32 deletions(-) diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h index bb265a632b91..dbaf04189548 100644 --- a/include/linux/uprobes.h +++ b/include/linux/uprobes.h @@ -23,8 +23,17 @@ struct inode; struct notifier_block; struct page; +/* + * Allowed return values from uprobe consumer's handler callback + * with following meaning: + * + * UPROBE_HANDLER_REMOVE + * - Remove the uprobe breakpoint from current->mm. + * UPROBE_HANDLER_IGNORE + * - Ignore ret_handler callback for this consumer. + */ #define UPROBE_HANDLER_REMOVE 1 -#define UPROBE_HANDLER_MASK 1 +#define UPROBE_HANDLER_IGNORE 2 #define MAX_URETPROBE_DEPTH 64 @@ -44,6 +53,8 @@ struct uprobe_consumer { bool (*filter)(struct uprobe_consumer *self, struct mm_struct *mm); struct list_head cons_node; + + __u64 id; /* set when uprobe_consumer is registered */ }; #ifdef CONFIG_UPROBES @@ -83,14 +94,22 @@ struct uprobe_task { unsigned int depth; }; +struct return_consumer { + __u64 cookie; + __u64 id; +}; + struct return_instance { struct uprobe *uprobe; unsigned long func; unsigned long stack; /* stack pointer */ unsigned long orig_ret_vaddr; /* original return address */ bool chained; /* true, if instance is nested */ + int consumers_cnt; struct return_instance *next; /* keep as stack */ + + struct return_consumer consumers[] __counted_by(consumers_cnt); }; enum rp_check { diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 2ba93f8a31aa..76fe535c9b3c 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -65,7 +65,7 @@ struct uprobe { struct rcu_head rcu; loff_t offset; loff_t ref_ctr_offset; - unsigned long flags; + unsigned long flags; /* "unsigned long" so bitops work */ /* * The generic code assumes that it has two members of unknown type @@ -825,8 +825,11 @@ static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset, static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) { + static atomic64_t id; + down_write(&uprobe->consumer_rwsem); list_add_rcu(&uc->cons_node, &uprobe->consumers); + uc->id = (__u64) atomic64_inc_return(&id); up_write(&uprobe->consumer_rwsem); } @@ -1797,6 +1800,34 @@ static struct uprobe_task *get_utask(void) return current->utask; } +static size_t ri_size(int consumers_cnt) +{ + struct return_instance *ri; + + return sizeof(*ri) + sizeof(ri->consumers[0]) * consumers_cnt; +} + +#define DEF_CNT 4 + +static struct return_instance *alloc_return_instance(void) +{ + struct return_instance *ri; + + ri = kzalloc(ri_size(DEF_CNT), GFP_KERNEL); + if (!ri) + return ZERO_SIZE_PTR; + + ri->consumers_cnt = DEF_CNT; + return ri; +} + +static struct return_instance *dup_return_instance(struct return_instance *old) +{ + size_t size = ri_size(old->consumers_cnt); + + return kmemdup(old, size, GFP_KERNEL); +} + static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) { struct uprobe_task *n_utask; @@ -1809,11 +1840,10 @@ static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) p = &n_utask->return_instances; for (o = o_utask->return_instances; o; o = o->next) { - n = kmalloc(sizeof(struct return_instance), GFP_KERNEL); + n = dup_return_instance(o); if (!n) return -ENOMEM; - *n = *o; /* * uprobe's refcnt has to be positive at this point, kept by * utask->return_instances items; return_instances can't be @@ -1906,39 +1936,35 @@ 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, + struct return_instance *ri) { - struct return_instance *ri; struct uprobe_task *utask; unsigned long orig_ret_vaddr, trampoline_vaddr; bool chained; if (!get_xol_area()) - return; + goto free; utask = get_utask(); if (!utask) - return; + goto free; if (utask->depth >= MAX_URETPROBE_DEPTH) { printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to" " nestedness limit pid/tgid=%d/%d\n", current->pid, current->tgid); - return; + goto free; } /* we need to bump refcount to store uprobe in utask */ if (!try_get_uprobe(uprobe)) - return; - - ri = kmalloc(sizeof(struct return_instance), GFP_KERNEL); - if (!ri) - goto fail; + goto free; trampoline_vaddr = uprobe_get_trampoline_vaddr(); orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs); if (orig_ret_vaddr == -1) - goto fail; + goto put; /* drop the entries invalidated by longjmp() */ chained = (orig_ret_vaddr == trampoline_vaddr); @@ -1956,7 +1982,7 @@ static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) * attack from user-space. */ uprobe_warn(current, "handle tail call"); - goto fail; + goto put; } orig_ret_vaddr = utask->return_instances->orig_ret_vaddr; } @@ -1971,9 +1997,10 @@ static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) utask->return_instances = ri; return; -fail: - kfree(ri); +put: put_uprobe(uprobe); +free: + kfree(ri); } /* Prepare to single-step probed instruction out of line. */ @@ -2125,35 +2152,91 @@ static struct uprobe *find_active_uprobe_rcu(unsigned long bp_vaddr, int *is_swb return uprobe; } +static struct return_instance* +push_consumer(struct return_instance *ri, int idx, __u64 id, __u64 cookie) +{ + if (unlikely(ri == ZERO_SIZE_PTR)) + return ri; + + if (unlikely(idx >= ri->consumers_cnt)) { + struct return_instance *old_ri = ri; + + ri->consumers_cnt += DEF_CNT; + ri = krealloc(old_ri, ri_size(old_ri->consumers_cnt), GFP_KERNEL); + if (!ri) { + kfree(old_ri); + return ZERO_SIZE_PTR; + } + } + + ri->consumers[idx].id = id; + ri->consumers[idx].cookie = cookie; + return ri; +} + +static struct return_consumer * +return_consumer_find(struct return_instance *ri, int *iter, int id) +{ + struct return_consumer *ric; + int idx = *iter; + + for (ric = &ri->consumers[idx]; idx < ri->consumers_cnt; idx++, ric++) { + if (ric->id == id) { + *iter = idx + 1; + return ric; + } + } + return NULL; +} + +static bool ignore_ret_handler(int rc) +{ + return rc == UPROBE_HANDLER_REMOVE || rc == UPROBE_HANDLER_IGNORE; +} + 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 */ - bool has_consumers = false; + bool has_consumers = false, remove = true; + struct return_instance *ri = NULL; + int push_idx = 0; current->utask->auprobe = &uprobe->arch; list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node, srcu_read_lock_held(&uprobes_srcu)) { + bool session = uc->handler && uc->ret_handler; + __u64 cookie = 0; int rc = 0; if (uc->handler) { - rc = uc->handler(uc, regs, NULL); - WARN(rc & ~UPROBE_HANDLER_MASK, + rc = uc->handler(uc, regs, &cookie); + WARN(rc < 0 || rc > 2, "bad rc=0x%x from %ps()\n", rc, uc->handler); } - if (uc->ret_handler) - need_prep = true; - - remove &= rc; + remove &= rc == UPROBE_HANDLER_REMOVE; has_consumers = true; + + if (!uc->ret_handler || ignore_ret_handler(rc)) + continue; + + if (!ri) + ri = alloc_return_instance(); + + if (session) + ri = push_consumer(ri, push_idx++, uc->id, cookie); } current->utask->auprobe = NULL; - if (need_prep && !remove) - prepare_uretprobe(uprobe, regs); /* put bp at return */ + if (!ZERO_OR_NULL_PTR(ri)) { + /* + * The push_idx value has the final number of return consumers, + * and ri->consumers_cnt has number of allocated consumers. + */ + ri->consumers_cnt = push_idx; + prepare_uretprobe(uprobe, regs, ri); + } if (remove && has_consumers) { down_read(&uprobe->register_rwsem); @@ -2172,14 +2255,17 @@ static void handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs) { struct uprobe *uprobe = ri->uprobe; + struct return_consumer *ric; struct uprobe_consumer *uc; - int srcu_idx; + int srcu_idx, ric_idx = 0; srcu_idx = srcu_read_lock(&uprobes_srcu); list_for_each_entry_srcu(uc, &uprobe->consumers, cons_node, srcu_read_lock_held(&uprobes_srcu)) { - if (uc->ret_handler) - uc->ret_handler(uc, ri->func, regs, NULL); + if (uc->ret_handler) { + ric = return_consumer_find(ri, &ric_idx, uc->id); + uc->ret_handler(uc, ri->func, regs, ric ? &ric->cookie : NULL); + } } srcu_read_unlock(&uprobes_srcu, srcu_idx); } From patchwork Sun Sep 29 20:57:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13815241 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 7D0A3152178; Sun, 29 Sep 2024 20:58:02 +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=1727643483; cv=none; b=Y+6fKopsWXh0fZ9KagF/BN8pzVtpOoZUH99Qo64YJ63wakrp263dJdxiui5YL313F5fAWt+65QmxwsJ/rwJZ4L0EQVAj59ubrPzFBJ/J2USLnvIoSz47u11gaVmeQUKXtA30o0vdmsRMkbhQ9wfqlXAu9BI/iWI+oaannbezYbE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643483; c=relaxed/simple; bh=NbIOzuT2nLtZ5vE0O540zbVsWi9LjQpTCYwdefeJy9g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QGRZr/VKce2mPb8f01Y0N6FdbW8gCelWcNxGV7OrP30HdDAPlo9Viw8pNIseY9lUw7zEiJFUX+QErrBcnvb8xnqttawJosMnRSAG20ojv1fG9dHwpQXcd6E/C7CSqo1igw2LA7yTPGnz6V4BHZwFTBvViEY/RAfMGbDuFE710gI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ATdM21FL; 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="ATdM21FL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 865C3C4CEC5; Sun, 29 Sep 2024 20:57:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643482; bh=NbIOzuT2nLtZ5vE0O540zbVsWi9LjQpTCYwdefeJy9g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ATdM21FLOXjZB8F3CnHvNeolQewkP1mTSnabjpcSVBx4KPxWOOhLeWqQDkvwOD2iU 0DWtRzLCpJz3RX5pFO/ZZLkGCjJzGhlreK2IAXEIV418cxUkcGhaWkvqPpqz11C7CZ 2upfdvzXTn1C+QfCyb1sHXGC5O4HEcE5/56epuz21tuBeaqw1xjSYDtWk2E8KMLRcj ANuDThW+7KtOkYSSWPArwdp4WJFAzk2eoAqUqlMtVVm2BNfzSNgX0Q8/BF8SZ3Oz3g jAyz+Lbj3K/BsVwotht3ZhPzal3KGNP5+nP5hiU7T4DHX/MXlJqO/I84eEuwD6xRE3 eWtiLQT2wZOsQ== 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: [PATCHv5 bpf-next 03/13] bpf: Add support for uprobe multi session attach Date: Sun, 29 Sep 2024 22:57:07 +0200 Message-ID: <20240929205717.3813648-4-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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. Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 9 ++++++-- kernel/trace/bpf_trace.c | 39 +++++++++++++++++++++++++++------- tools/include/uapi/linux/bpf.h | 1 + tools/lib/bpf/libbpf.c | 1 + 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 8ab4d8184b9d..77d0bc5fa986 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 a8f1808a1ca5..0cf7617e6cb6 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3983,10 +3983,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: @@ -5239,7 +5243,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 fdab7ecd8dfa..98e940ec184d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1557,6 +1557,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) { @@ -1574,13 +1585,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: @@ -3074,6 +3085,7 @@ struct bpf_uprobe { u64 cookie; struct uprobe *uprobe; struct uprobe_consumer consumer; + bool session; }; struct bpf_uprobe_multi_link { @@ -3248,9 +3260,13 @@ uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs, __u64 *data) { struct bpf_uprobe *uprobe; + int ret; uprobe = container_of(con, struct bpf_uprobe, consumer); - return uprobe_prog_run(uprobe, instruction_pointer(regs), regs); + ret = uprobe_prog_run(uprobe, instruction_pointer(regs), regs); + if (uprobe->session) + return ret ? UPROBE_HANDLER_IGNORE : 0; + return ret; } static int @@ -3260,6 +3276,12 @@ 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); + /* + * There's chance we could get called with NULL data if we registered uprobe + * after it hit entry but before it hit return probe, just ignore it. + */ + if (uprobe->session && !data) + return 0; return uprobe_prog_run(uprobe, func, regs); } @@ -3299,7 +3321,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; @@ -3375,11 +3397,12 @@ 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 + if (!(flags & BPF_F_UPROBE_MULTI_RETURN)) uprobes[i].consumer.handler = uprobe_multi_link_handler; - + if (flags & BPF_F_UPROBE_MULTI_RETURN || is_uprobe_session(prog)) + uprobes[i].consumer.ret_handler = uprobe_multi_link_ret_handler; + if (is_uprobe_session(prog)) + uprobes[i].session = true; 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 7610883c8191..09bdb1867d4a 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 }; diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 712b95e8891b..3587ed7ec359 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 Sun Sep 29 20:57:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13815242 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 61AE813B58C; Sun, 29 Sep 2024 20:58: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=1727643494; cv=none; b=Fr/IgpO9S504TL4/cLsstw/zG+LHDPGsSLTmqMIDVsP05yFB4o5ewWjGF1p797ZNFXCACDHZJ4FGWTOfiZTRqpCgJZIYiWXzQekrQryPVXjEjn7LKzyOZcMXsLmpeXGnFeGt+u0LWikvIr+wIEQRPYWlffY8sGV4txCRXFixpXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643494; c=relaxed/simple; bh=X2c9rNtwRNPonQHvHspo6tt2WiHF3JstxCqP0Vs89JM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ehoiyh+T4awimgpgGwni16qCZXUlvQE7aBSuE3VVAEMh+a+e6CjHxqjjd+l85eARMibyrt4+zF9zUFF6DsOyJgQ0CnEVnXTVP+AEwX7/H4l/5uoCQ4UODFC2B5rsSiyrOG62b9+stRpIfpZ6Sb+UT5Kb8M4oCLKus96LxRJKIkg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=koSi4l1k; 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="koSi4l1k" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 79D06C4CEC5; Sun, 29 Sep 2024 20:58:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643494; bh=X2c9rNtwRNPonQHvHspo6tt2WiHF3JstxCqP0Vs89JM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=koSi4l1kDhnDWh64tDaQZwoRcRoAyTSjSM5xNGW3n0DClO40kgdeZ7n+WzyF+rYC/ oZ1LNbDmT3ZBFxdqWwVDcTbB3zXPfqQlGVKGv/x7gJvXQK7ddGkndm8IBER6xPZjIB q2ABLZ4s8ErnDZ7l4OZWu6v91PfvcOAK20/EHBcYFuNGgcuymV4H95E3fPS7Nn4Xcu Vd3O7UnXJx1iKsfClXw2VXpX7Q7J94xDOGGS0Ci7G2MPmPM9tTVEjSFEX5nzb0dtXl O5RxTdTVa08MKjU0jEjBU1c373d3pPTuVy5Yw9VAbQjff4LMnuJEzYQZcUq424HSui MXdZ0Y/jSmKMQ== 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: [PATCHv5 bpf-next 04/13] bpf: Add support for uprobe multi session context Date: Sun, 29 Sep 2024 22:57:08 +0200 Message-ID: <20240929205717.3813648-5-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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. Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- kernel/trace/bpf_trace.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 98e940ec184d..41f83d504bf6 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3098,7 +3098,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; }; @@ -3211,10 +3211,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, }; @@ -3233,7 +3238,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); @@ -3263,7 +3268,7 @@ uprobe_multi_link_handler(struct uprobe_consumer *con, struct pt_regs *regs, int ret; uprobe = container_of(con, struct bpf_uprobe, consumer); - ret = uprobe_prog_run(uprobe, instruction_pointer(regs), regs); + ret = uprobe_prog_run(uprobe, instruction_pointer(regs), regs, false, data); if (uprobe->session) return ret ? UPROBE_HANDLER_IGNORE : 0; return ret; @@ -3282,14 +3287,15 @@ uprobe_multi_link_ret_handler(struct uprobe_consumer *con, unsigned long func, s */ if (uprobe->session && !data) return 0; - 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; } @@ -3297,7 +3303,8 @@ 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; } @@ -3491,7 +3498,7 @@ 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 Sun Sep 29 20:57:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13815243 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 6D780768E1; Sun, 29 Sep 2024 20:58: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=1727643506; cv=none; b=PvoiJQVcSYFv2kP9Xkt5VjmCIDSd2RWdEllCVnwrjtdEISXi84nBhS38DAwVsJBOLWrgkoaWuCVgYt3FxgWI9KY39XdcFP+nxlUgvUC4a/kMByqlSTDJjjaQYJJ7f4oqdmtgp2Tja0lTjWU4PK+DRzSsqEHKUk7snbY3I5bT0Ss= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643506; c=relaxed/simple; bh=p7mWPSIolWA4rN8j3pRSzYOWFCyb6wl14UbPts/dKbE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bH0JlUhzAQCplgbVKenX9fEfTUMZ/v3KFYtv2uiiOq7xtEKlPIhvPRtoAqoHQ3LjZ6gWnu9F/na0VkxYfaVzxQBQHmHhX8NJeQOsV7OVrsNnVETAICTs/P3ZTeFDSeDH3xGV5TPYvUidguvXcxVJZotoF9Fbuw+qozraMF0PJRA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n0KWE+kM; 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="n0KWE+kM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E894C4CEC5; Sun, 29 Sep 2024 20:58:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643506; bh=p7mWPSIolWA4rN8j3pRSzYOWFCyb6wl14UbPts/dKbE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n0KWE+kMVVW+V2cInudMS02kuTD9KwhsqN8vKJQnCF+M5ECQNH6oeG7jziF3IJLW1 uFBUb4LOCLbmqP2Z3fUynZ3V+YUMsZ7g1OKjVZ+3uZn/evJGebjnlGpIF9ESwzw3yQ B34xvzRYRV9g1NPEoiRO8thz1fuCa8RWZ1q1MoFq8MZj3FH2DioEd4QuVMTqj0PDsv GwLAfYLHtf9UiNr1/7hD3QMxZVtkPdYaGPcsD1dvSlJT+TO1gXrW4IkfFlAmh6Xee7 BPdUZMZQqzjK56eroNJQao1OLuqXHVDlYmohtePKvK1JQzE2snhVY1P70cbYlykchX Y6fJTPzLviJdw== 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: [PATCHv5 bpf-next 05/13] bpf: Allow return values 0 and 1 for uprobe/kprobe session Date: Sun, 29 Sep 2024 22:57:09 +0200 Message-ID: <20240929205717.3813648-6-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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 The uprobe and kprobe session program can return only 0 or 1, instruct verifier to check for that. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- kernel/bpf/verifier.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 7d9b38ffd220..c4d7b7369259 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -15910,6 +15910,16 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char return -ENOTSUPP; } break; + case BPF_PROG_TYPE_KPROBE: + switch (env->prog->expected_attach_type) { + case BPF_TRACE_KPROBE_SESSION: + case BPF_TRACE_UPROBE_SESSION: + range = retval_range(0, 1); + break; + default: + return 0; + } + break; case BPF_PROG_TYPE_SK_LOOKUP: range = retval_range(SK_DROP, SK_PASS); break; From patchwork Sun Sep 29 20:57:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13815244 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 886F07E107; Sun, 29 Sep 2024 20:58: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=1727643518; cv=none; b=bfKHiq4oK9e5jx+hqo7I7vMwXuF4zOX5S38JQVPuz5bsCz+4MmLypPxtMf3Nwyof1uSfALtsyS5ExHJVfYIVKTb1kkj9gkSmZUgY6+NQkJZtUnw7CBQoOfkVsUCMYS0hitmnU+hp0sUZuzfKavAV6/lLt6WXvv+fLyYVRuf0oSk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643518; c=relaxed/simple; bh=YzvMBy86QCWKgZTc3SkXPbTnwplK2vPqUCi9UkcjDiY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n3PvB+lH+P+IhfZvY65y2ZqBjUmKBc//OtGYLVrDhRoHsSLkDFm3Ep9vk58bGfYpxpMZdXfsr/4b39lf2VQYHvoeLrZ2+TOMR3pWPXFkahik9wZOBcSNeil8X7mBN5V0oeYjHyBBlvHEgPvf+TKMbhc9iyTlbbpW5BCtBOimcDU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n4oZPX0M; 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="n4oZPX0M" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5B06CC4CEC5; Sun, 29 Sep 2024 20:58:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643518; bh=YzvMBy86QCWKgZTc3SkXPbTnwplK2vPqUCi9UkcjDiY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n4oZPX0MPPJue7vEOp3TN85zavIKFCCrc/4rbRud57VCKmh29W7bgTRr8weeIGOPs xhkfmMJ5vJJNug/wDSVcdDdEdbrhePOxufA5jgpdCy26VMyVROpKWdHtU3ZJqV2HK/ J4bFvsyU3Cdsr1gENmEi2hD4UFYP2FLq854SWqJB4HbzzyUJEiae6sw6URMYTQANra DcbyQ72l3pY3NjOR4r/ltG6yzW+QYkDlRLrSTUkg1lqQwt9P4rB4NnKkSw3MZEUcDP Gfqo0gwXDpm+oKeTyEhHoExuPZfAGpH3zo4tvOBnNTcfQYDC+malrMwT4dgCzbEUmL xQTmSzPSWj8ww== 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: [PATCHv5 bpf-next 06/13] libbpf: Add support for uprobe multi session attach Date: Sun, 29 Sep 2024 22:57:10 +0200 Message-ID: <20240929205717.3813648-7-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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. Adding sleepable hook (uprobe.session.s) as well. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/libbpf.c | 21 ++++++++++++++++++--- tools/lib/bpf/libbpf.h | 4 +++- 3 files changed, 22 insertions(+), 4 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 3587ed7ec359..563ff5e64269 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9410,8 +9410,10 @@ 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_multi), 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("uprobe.session.s+", KPROBE, BPF_TRACE_UPROBE_SESSION, SEC_SLEEPABLE, attach_uprobe_multi), SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), SEC_DEF("kretsyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall), SEC_DEF("usdt+", KPROBE, 0, SEC_USDT, attach_usdt), @@ -11733,7 +11735,10 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru ret = 0; break; case 3: - opts.retprobe = str_has_pfx(probe_type, "uretprobe.multi"); + if (str_has_pfx(probe_type, "uprobe.session")) + opts.session = true; + else + opts.retprobe = str_has_pfx(probe_type, "uretprobe.multi"); *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts); ret = libbpf_get_error(*link); break; @@ -11982,10 +11987,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; @@ -12056,12 +12063,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(); @@ -12075,7 +12090,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 91484303849c..b2ce3a72b11d 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -577,10 +577,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 Sun Sep 29 20:57:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13815245 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 AC73F3D0D5; Sun, 29 Sep 2024 20:58:50 +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=1727643530; cv=none; b=LILCcjOFMzWaPk/4Yio3JD/D9lSAOUUWC5PcrzswWXpb7AjRVxzoWxjq61AqgYAVog+fPpQYk6Z6ASsPUNUOvySLqcH57uJ9wi9172SsBWQOLLfgv2v4zlsPM+dfOWCrdICvRukpJ8nR7vxFpaiky72FJCOC7V7MCzqiEGIAED4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643530; c=relaxed/simple; bh=6B8ZL07DjOZIflxWhuosy+A/a21635OmFdEDO63GsAc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lxTmWB/fTsZoFi/m5npk1mVrdOzYma9eburBXkp4DKexwx+90Zm/E/AA3tw76f885V/eSK4J+Zj9JWlFzvR9uVWgl7hog18i254HO3TYODhMIJQ+Rr7RrPteYade69IpP5nnXNQbJi66+OTLyGebRYXW2w7WBde30FPufV1QGrw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NlLEILzh; 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="NlLEILzh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88F98C4CEC5; Sun, 29 Sep 2024 20:58:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643530; bh=6B8ZL07DjOZIflxWhuosy+A/a21635OmFdEDO63GsAc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NlLEILzhrjzoQZ94zeKNpEvx+EbA91sKEt4ug2Al5sExNNBg5rFO7TEhMJSOOV6mp EQJ1V/Zvg0KmB80XvmW1jMdCVcE+n9rEvow9xO1q5XaLFXxocQ0zRiueyFngUWPIoD EKOyK7YTyVCrA7vDNQdUppGn+3YIEIjcFpacspeM/pMYCJ9YBfTOqy+NEtIcezB3QT ee3sb6e5zBJ+SjNhhELt8ULehELVdizkGmgJbIPQUdeJwfbX9tE1w06m1L9l+BCSIE 2E23ckYKnjJThrdJj3PS6MPwrsrzKfmROZENkWrEajRPhz8lrKfM0Znl1ApBhdtuMs 2kAws9IkoMWCw== 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: [PATCHv5 bpf-next 07/13] selftests/bpf: Add uprobe session test Date: Sun, 29 Sep 2024 22:57:11 +0200 Message-ID: <20240929205717.3813648-8-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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 | 47 ++++++++++++ .../bpf/progs/uprobe_multi_session.c | 71 +++++++++++++++++++ 2 files changed, 118 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 2c39902b8a09..b10d2dadb462 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -8,6 +8,7 @@ #include "uprobe_multi_usdt.skel.h" #include "uprobe_multi_consumers.skel.h" #include "uprobe_multi_pid_filter.skel.h" +#include "uprobe_multi_session.skel.h" #include "bpf/libbpf_internal.h" #include "testing_helpers.h" #include "../sdt.h" @@ -1015,6 +1016,50 @@ static void test_pid_filter_process(bool clone_vm) uprobe_multi_pid_filter__destroy(skel); } +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, "uprobe_multi_session__open_and_load")) + goto cleanup; + + skel->bss->pid = getpid(); + skel->bss->user_ptr = test_data; + + 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(); + + /* + * We expect 2 for uprobe_multi_func_2 because it runs both entry/return probe, + * uprobe_multi_func_[13] run just the entry probe. All expected numbers are + * doubled, because we run extra test for sleepable session. + */ + ASSERT_EQ(skel->bss->uprobe_session_result[0], 2, "uprobe_multi_func_1_result"); + ASSERT_EQ(skel->bss->uprobe_session_result[1], 4, "uprobe_multi_func_2_result"); + ASSERT_EQ(skel->bss->uprobe_session_result[2], 2, "uprobe_multi_func_3_result"); + + /* We expect increase in 3 entry and 1 return session calls -> 4 */ + ASSERT_EQ(skel->bss->uprobe_multi_sleep_result, 4, "uprobe_multi_sleep_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; @@ -1111,4 +1156,6 @@ void test_uprobe_multi_test(void) test_pid_filter_process(false); if (test__start_subtest("filter_clone_vm")) test_pid_filter_process(true); + 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..30bff90b68dc --- /dev/null +++ b/tools/testing/selftests/bpf/progs/uprobe_multi_session.c @@ -0,0 +1,71 @@ +// 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] = {}; +__u64 uprobe_multi_sleep_result = 0; + +void *user_ptr = 0; +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; + } + } + + /* only uprobe_multi_func_2 executes return probe */ + 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()); +} + +static __always_inline bool verify_sleepable_user_copy(void) +{ + char data[9]; + + bpf_copy_from_user(data, sizeof(data), user_ptr); + return bpf_strncmp(data, sizeof(data), "test_data") == 0; +} + +SEC("uprobe.session.s//proc/self/exe:uprobe_multi_func_*") +int uprobe_sleepable(struct pt_regs *ctx) +{ + if (verify_sleepable_user_copy()) + uprobe_multi_sleep_result++; + return uprobe_multi_check(ctx, bpf_session_is_return()); +} From patchwork Sun Sep 29 20:57: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: 13815246 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 7792018EB0; Sun, 29 Sep 2024 20:59:02 +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=1727643542; cv=none; b=ph/EgyjLaVJIdNJBQ/skPCLKqwgt8x83lMlV08ZyI4R7UYZYa5fBxNkRbYaG/xtMK1b4B1VeRJHwHms61hf9WWcuTqN3/ZnIcXoTwn81f7RhpSPU0HhCx/DIuKXzgoHqZTn04qn9NTHvv9+XY1GhwcFkY9cLsZWjdEBDj+x++S4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643542; c=relaxed/simple; bh=9k6v4VENBPTcpu5P+I+ruL1o1grMLPniGL3MpVpyyCQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cyMFWFKaXwDiCQZDDMhO11uWZTs9etsKn8zQSQTDgJvD8bAcW/nFK1t3HLeDKT5+FdOnDvrofYPydf/R8Nx60nybrd7dQetwbzUeTEYOB4IEb3gt2Nww7A5CG1bBPb9g4DFdqdrY9mY36BsiPAAGFgvA/5e8YzdKLffvGs6lbUM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fv2se9nv; 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="fv2se9nv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0CBAEC4CEC5; Sun, 29 Sep 2024 20:58:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643542; bh=9k6v4VENBPTcpu5P+I+ruL1o1grMLPniGL3MpVpyyCQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fv2se9nvgnBe/iPdT5bkh36fNBgh/5RE3B4GmBN5f2NpO0p9ZMGr/qXqbZA5Dndc6 KlTDREn74GSnHUk4Qe9rPy/2A5Ikb8AxjjyJLJFOcjwC39E4HzY19im6oxXV9qFRrK DaGR982f7GUGLMXURoSVI4KDg4oq00hGvfKxr8VcXf4EtSBMacBTgfqJ9FfnHCfGvw 495soecn+RvcuiwPrqAZ/GZGgV55pRDrrnj53RRCxjVubq5bEwkHOSOVAzmiYY/4tA WSeSPa9I9S4bb7BqoR7puDxL/VqtmjeN08oJW/6x8f1dzsdUv+4sugoPrnWmroFQLu BcTbRo8iBTVyg== 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: [PATCHv5 bpf-next 08/13] selftests/bpf: Add uprobe session cookie test Date: Sun, 29 Sep 2024 22:57:12 +0200 Message-ID: <20240929205717.3813648-9-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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. Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/uprobe_multi_test.c | 31 ++++++++++++ .../bpf/progs/uprobe_multi_session_cookie.c | 48 +++++++++++++++++++ 2 files changed, 79 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 b10d2dadb462..cc9030e86821 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -9,6 +9,7 @@ #include "uprobe_multi_consumers.skel.h" #include "uprobe_multi_pid_filter.skel.h" #include "uprobe_multi_session.skel.h" +#include "uprobe_multi_session_cookie.skel.h" #include "bpf/libbpf_internal.h" #include "testing_helpers.h" #include "../sdt.h" @@ -1060,6 +1061,34 @@ static void test_session_skel_api(void) uprobe_multi_session__destroy(skel); } +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, "uprobe_multi_session_cookie__open_and_load")) + goto cleanup; + + skel->bss->pid = getpid(); + + err = uprobe_multi_session_cookie__attach(skel); + if (!ASSERT_OK(err, "uprobe_multi_session_cookie__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; @@ -1158,4 +1187,6 @@ void test_uprobe_multi_test(void) test_pid_filter_process(true); if (test__start_subtest("session")) test_session_skel_api(); + 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..5befdf944dc6 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/uprobe_multi_session_cookie.c @@ -0,0 +1,48 @@ +// 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) +{ + __u64 *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 Sun Sep 29 20:57: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: 13815247 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 2D2BF768E1; Sun, 29 Sep 2024 20:59: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=1727643555; cv=none; b=Gx+w6tuFDGZsYl+gFTfwywnrHIN5PePbVYiwUiEUl5N1K3Z3iWQFZZcCudWSAO/24oARTYiwwTx1+YcWvonQD1Edf9qqEugrF5uljfJ1m1lUe5BeSizdKBSqMdL/u4OEepDuh/BGnKdqlTyII6hAYPZdcmoHv4v/oXZlr0Qc3qg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643555; c=relaxed/simple; bh=euP1HPXSCDWMwYn6bfEaonzC4gXPjRy70a9i+BzMcT0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y5tZ4a4fBGlaSbwLIAi2IifrwNkfJfrp5az+jI2rJPtRB6IoS3ijy3fLo19tw5IvKtNNOzPE3R4jSZB+vtrqS5muAaJHjcHX4kHS7x5N2GdWib5kA1iKHnt09yI70btDhm93pXtucRrh1FW+Ps1lVnTzAvfEgh50ch0wgxL95IY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PHaCgHat; 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="PHaCgHat" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36261C4CEC5; Sun, 29 Sep 2024 20:59:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643554; bh=euP1HPXSCDWMwYn6bfEaonzC4gXPjRy70a9i+BzMcT0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PHaCgHatH2bMCKJQoOVlg7b1IhoK3oKmKv320de11nC41uiCQfaJcOWq2IjUb8UeI 2AHlIITaVs0fnrxjmxe71QTioPwzfl/9u5+TNrTpeIzltEDpCKDrlGU67ECnWiMhKL ouz7tmwFcyA0Ar7SlI5Vzck/ctsQodkiJ0R3yMSSrlqqZtxcrkNiR7MdLMrWUPXa2Q VZDKPZpVdaad3pvzMciWqPt9vN6NnyiSLtaPQSHdVqUEbTi27gtKImKMSUS2QPj6bR vnZWA4XbbvRvVi4/Nzfy26Wr5bylBPvGeENpWyhGlvo0OZL4Z/qbN4qp5bVGxLAsS0 GzIL9I9Zj7GAw== 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: [PATCHv5 bpf-next 09/13] selftests/bpf: Add uprobe session recursive test Date: Sun, 29 Sep 2024 22:57:13 +0200 Message-ID: <20240929205717.3813648-10-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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. Acked-by: Andrii Nakryiko 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 cc9030e86821..284cd7fce576 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -10,6 +10,7 @@ #include "uprobe_multi_pid_filter.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" #include "../sdt.h" @@ -36,6 +37,12 @@ noinline void usdt_trigger(void) STAP_PROBE(test, pid_filter_usdt); } +noinline void uprobe_session_recursive(int i) +{ + if (i) + uprobe_session_recursive(i - 1); +} + struct child { int go[2]; int c2p[2]; /* child -> parent channel */ @@ -1089,6 +1096,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; @@ -1189,4 +1244,6 @@ void test_uprobe_multi_test(void) test_session_skel_api(); 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..8fbcd69fae22 --- /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) +{ + __u64 *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(); +} From patchwork Sun Sep 29 20:57: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: 13815248 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 077F2178CC8; Sun, 29 Sep 2024 20:59: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=1727643566; cv=none; b=qyzgudQ71gX8J0OTBVere4KfSul3lB7S/JN4hfI6pMRlRYFcQspWWBydXN3+lhun3hGG1OeQwJnyG6zLf9MpLKimgn224ILUUOH1J8x5HlfeF+UBShMfbhcKkmrOTIdcOq1QEhr18hQ7FYoVYWKbtE5fEGZ2S2/XO/X4+7aU5dM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643566; c=relaxed/simple; bh=nDwwKIoORj0HBWMmH82W4I0Tpkb3tviaGj5oODCtCrU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c+yqBf0laC3aFdOFJufXungkjZH09ruw2kIr2FCrS9DoCIQ8uD/9oMxIKA/8SNlr0PrZ5TZAMqk9Z238EMcvN+W0WcRt/lWx3CiP8d9V4oy4fZO10DaSMQVSbW+y+2d5CLFz3SDVSVc1W/pMCUtrutVsTO3hml4YOEqXUqBbjNU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=grzzITYy; 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="grzzITYy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DEEF2C4CEC5; Sun, 29 Sep 2024 20:59:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643565; bh=nDwwKIoORj0HBWMmH82W4I0Tpkb3tviaGj5oODCtCrU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=grzzITYyIgLjmrS1FMMC+i7xZdPZ5JAlLa26JMGu2xptA7ptJoFMFFMLtV/vLKkr7 fbkSkZQuWnV1evV8hNtq3ffr99KSQihyPLCfdsz14SbOm5g7ht3n7tNuVaYJWK/tgN sDnfd09gUx7uTM7X7AOZ17sxAInygFaJ4dhhWHUpPYhYdLHkiED9C/aIT87MdgTodm XncIdM6WGWnkfgc7GHfOc/DyKB/SLpM2E5+Mcoo65DoaX8+7oUNMau4TFGAxnlbyAH z8qG9nEf++8N63H7Cjcxth5CeQpDrVzhTXx2FrlSA8a/TgYYhaia7qE7BsOwpLNflF WGKukmQQQHl4w== 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: [PATCHv5 bpf-next 10/13] selftests/bpf: Add uprobe session verifier test for return value Date: Sun, 29 Sep 2024 22:57:14 +0200 Message-ID: <20240929205717.3813648-11-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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 Making sure uprobe.session program can return only [0,1] values. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/uprobe_multi_test.c | 2 ++ .../bpf/progs/uprobe_multi_verifier.c | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_verifier.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 284cd7fce576..e693eeb1a5a5 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -11,6 +11,7 @@ #include "uprobe_multi_session.skel.h" #include "uprobe_multi_session_cookie.skel.h" #include "uprobe_multi_session_recursive.skel.h" +#include "uprobe_multi_verifier.skel.h" #include "bpf/libbpf_internal.h" #include "testing_helpers.h" #include "../sdt.h" @@ -1246,4 +1247,5 @@ void test_uprobe_multi_test(void) test_session_cookie_skel_api(); if (test__start_subtest("session_cookie_recursive")) test_session_recursive_skel_api(); + RUN_TESTS(uprobe_multi_verifier); } diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi_verifier.c b/tools/testing/selftests/bpf/progs/uprobe_multi_verifier.c new file mode 100644 index 000000000000..fe49f2cb5360 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/uprobe_multi_verifier.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include +#include +#include +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + + +SEC("uprobe.session") +__success +int uprobe_sesison_return_0(struct pt_regs *ctx) +{ + return 0; +} + +SEC("uprobe.session") +__success +int uprobe_sesison_return_1(struct pt_regs *ctx) +{ + return 1; +} + +SEC("uprobe.session") +__failure +__msg("At program exit the register R0 has smin=2 smax=2 should have been in [0, 1]") +int uprobe_sesison_return_2(struct pt_regs *ctx) +{ + return 2; +} From patchwork Sun Sep 29 20:57: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: 13815249 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 A2FE9175D32; Sun, 29 Sep 2024 20:59:37 +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=1727643577; cv=none; b=YVj/JRAVc9qkRdgti3xNGxARA44WmlNSWgDXV1db7jJ9g0n+bMgYRrxjBlfcrfJbEPNQOmCZF9hzMikx8Y/p8WiIuIcB+HoWhQan7aa3PKiloSgfOMRfF1e8ImfX4Xb0t8mDN5KLXeXrR611wGETiut6HIOZh0LtFMGL4xXY/qg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643577; c=relaxed/simple; bh=iytPBV/coYs8uklN481sCB6sr+lO7xPubDoHnHmKQm4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mJX5u9vvROJB1P2X/DzbHz/Uw5Qz7j25cbH1Bdpy7MKDs2LqoLed1ndnSwR1fbHCA8anGIN43Cz7rfcJgIBryYfONuuolcIN3GKJ6YxrALvEKvdrfscYd2GWTPqp7hSsKotHAK1qC8gONbE47mgfaTHpKhcu+rn2OnAIxtk/0v8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dVbrTRuY; 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="dVbrTRuY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0E7A2C4CEC5; Sun, 29 Sep 2024 20:59:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643577; bh=iytPBV/coYs8uklN481sCB6sr+lO7xPubDoHnHmKQm4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dVbrTRuYkpu0qtaqULHFtRDkT4JV3LTuPtF+JA1JO45W02jTV4k3iJ1oyIuuaYWAZ INgOpqb387zxBxke0rhi15UM/CZpRhQg+/qE5BW5U4IykMrmDKoFfF5DCzJrVba9yN vEiIVg5L3KvjDi/yz7rg1FpcqZFPhmAEIk9JyyZp+P9PsfqRPoSrnjd2R5EHugIQ3z jcTVgo9thAqsvNa3SBmjxjwkz+rSZZB6yntPax+IXX8o2blCewHjTrtFU3vAbTLp0I 2TNfCuVwveLqheABw+jUk/KzRLUx46ErNpW+032vW/tfV6Ov6Iqchp6Hmtc78pDKto AHwzlinwqK1wA== 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: [PATCHv5 bpf-next 11/13] selftests/bpf: Add kprobe session verifier test for return value Date: Sun, 29 Sep 2024 22:57:15 +0200 Message-ID: <20240929205717.3813648-12-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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 Making sure kprobe.session program can return only [0,1] values. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/kprobe_multi_test.c | 2 ++ .../bpf/progs/kprobe_multi_verifier.c | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_verifier.c diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c index 960c9323d1e0..66ab1cae923e 100644 --- a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c @@ -6,6 +6,7 @@ #include "kprobe_multi_override.skel.h" #include "kprobe_multi_session.skel.h" #include "kprobe_multi_session_cookie.skel.h" +#include "kprobe_multi_verifier.skel.h" #include "bpf/libbpf_internal.h" #include "bpf/hashmap.h" @@ -764,4 +765,5 @@ void test_kprobe_multi_test(void) test_session_skel_api(); if (test__start_subtest("session_cookie")) test_session_cookie_skel_api(); + RUN_TESTS(kprobe_multi_verifier); } diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_verifier.c b/tools/testing/selftests/bpf/progs/kprobe_multi_verifier.c new file mode 100644 index 000000000000..288577e81deb --- /dev/null +++ b/tools/testing/selftests/bpf/progs/kprobe_multi_verifier.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include +#include +#include +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + + +SEC("kprobe.session") +__success +int kprobe_session_return_0(struct pt_regs *ctx) +{ + return 0; +} + +SEC("kprobe.session") +__success +int kprobe_session_return_1(struct pt_regs *ctx) +{ + return 1; +} + +SEC("kprobe.session") +__failure +__msg("At program exit the register R0 has smin=2 smax=2 should have been in [0, 1]") +int kprobe_session_return_2(struct pt_regs *ctx) +{ + return 2; +} From patchwork Sun Sep 29 20:57: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: 13815250 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 0126F175D32; Sun, 29 Sep 2024 20:59: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=1727643589; cv=none; b=Dcl+dvOkaX9aa8i01tB2wSw+InWTpWtRbn2B8ZgxAQVWR0hU++C8PPh2oOKd92zG0naKYXkNsDUple2M6ECfVOLWPScY0Y/Pg8biEmX7Z3J8ehzCUQsGB/UVZfWbnSz0qL4wFC3rwVi6S1EhFNxUGle4MrYBu/qM8fDhKwPXsJs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643589; c=relaxed/simple; bh=GxXbTFmwtFnji9jVDyodCe68CuD8wpBEa6bjFb/hzu0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qdfNMJ1YCbyZhS3PSL262V7FBDAvxZk4vk467FOj5aqbvVDMxiXm2b/+npOsC1kBvz3TuNwAndDUqMe6NTGaxta2yZGzQKd/p09KGSCFWz/A/TXNlRy0ydrYZm2JZjH74sdWf9uCuz5r2V08V2GDcMvrDUVRZ2Y9/HqCfkBVmkY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=O796d0Pl; 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="O796d0Pl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52416C4CEC5; Sun, 29 Sep 2024 20:59:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643588; bh=GxXbTFmwtFnji9jVDyodCe68CuD8wpBEa6bjFb/hzu0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O796d0PlfQ58k7M/MZmstcbLoJUWLuR+Eaqupi/L2w+cERrq0dFVPhI0UQKlhUJ3O O/hVunCTDSN3Nmgz6XBjMyALDuGQx6knEUboYSnLURvsN2t8fOSNSHhRTrQXs/mg30 gDu1CYMnX9cMz487iEEsI8j5scRONJIYSxD+AwtXQWdhFEApXkNySl5I7RL5pI91Am aagU7t8Xq5ZM1EFbVcTrnoBbhypjBHDdGxytYpp6Nwkau8dJbTv7bXTyxlZGpRi7Ho B5L2Sm9btw7c4fV9bsWn8ZXkLjpqlhp/ZQ0sRvPDKBFVgxcfep32PAAcVC8zbppfy3 ks2UiOsJOS5TQ== 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: [PATCHv5 bpf-next 12/13] selftests/bpf: Add uprobe session single consumer test Date: Sun, 29 Sep 2024 22:57:16 +0200 Message-ID: <20240929205717.3813648-13-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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 Testing that the session ret_handler bypass works on single uprobe with multiple consumers, each with different session ignore return value. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/uprobe_multi_test.c | 33 ++++++++++++++ .../bpf/progs/uprobe_multi_session_single.c | 44 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/uprobe_multi_session_single.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 e693eeb1a5a5..7e0228f8fcfc 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -9,6 +9,7 @@ #include "uprobe_multi_consumers.skel.h" #include "uprobe_multi_pid_filter.skel.h" #include "uprobe_multi_session.skel.h" +#include "uprobe_multi_session_single.skel.h" #include "uprobe_multi_session_cookie.skel.h" #include "uprobe_multi_session_recursive.skel.h" #include "uprobe_multi_verifier.skel.h" @@ -1069,6 +1070,36 @@ static void test_session_skel_api(void) uprobe_multi_session__destroy(skel); } +static void test_session_single_skel_api(void) +{ + struct uprobe_multi_session_single *skel = NULL; + LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); + int err; + + skel = uprobe_multi_session_single__open_and_load(); + if (!ASSERT_OK_PTR(skel, "uprobe_multi_session_single__open_and_load")) + goto cleanup; + + skel->bss->pid = getpid(); + + err = uprobe_multi_session_single__attach(skel); + if (!ASSERT_OK(err, "uprobe_multi_session_single__attach")) + goto cleanup; + + uprobe_multi_func_1(); + + /* + * We expect consumer 0 and 2 to trigger just entry handler (value 1) + * and consumer 1 to hit both (value 2). + */ + ASSERT_EQ(skel->bss->uprobe_session_result[0], 1, "uprobe_session_result_0"); + ASSERT_EQ(skel->bss->uprobe_session_result[1], 2, "uprobe_session_result_1"); + ASSERT_EQ(skel->bss->uprobe_session_result[2], 1, "uprobe_session_result_2"); + +cleanup: + uprobe_multi_session_single__destroy(skel); +} + static void test_session_cookie_skel_api(void) { struct uprobe_multi_session_cookie *skel = NULL; @@ -1243,6 +1274,8 @@ void test_uprobe_multi_test(void) test_pid_filter_process(true); if (test__start_subtest("session")) test_session_skel_api(); + if (test__start_subtest("session_single")) + test_session_single_skel_api(); if (test__start_subtest("session_cookie")) test_session_cookie_skel_api(); if (test__start_subtest("session_cookie_recursive")) diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi_session_single.c b/tools/testing/selftests/bpf/progs/uprobe_multi_session_single.c new file mode 100644 index 000000000000..1fa53d3785f6 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/uprobe_multi_session_single.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"; + +__u64 uprobe_session_result[3] = {}; +int pid = 0; + +static int uprobe_multi_check(void *ctx, bool is_return, int idx) +{ + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 1; + + uprobe_session_result[idx]++; + + /* only consumer 1 executes return probe */ + if (idx == 0 || idx == 2) + return 1; + + return 0; +} + +SEC("uprobe.session//proc/self/exe:uprobe_multi_func_1") +int uprobe_0(struct pt_regs *ctx) +{ + return uprobe_multi_check(ctx, bpf_session_is_return(), 0); +} + +SEC("uprobe.session//proc/self/exe:uprobe_multi_func_1") +int uprobe_1(struct pt_regs *ctx) +{ + return uprobe_multi_check(ctx, bpf_session_is_return(), 1); +} + +SEC("uprobe.session//proc/self/exe:uprobe_multi_func_1") +int uprobe_2(struct pt_regs *ctx) +{ + return uprobe_multi_check(ctx, bpf_session_is_return(), 2); +} From patchwork Sun Sep 29 20:57: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: 13815254 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 D3CE3175D32; Sun, 29 Sep 2024 20:59:59 +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=1727643599; cv=none; b=FFcRUqHH+WGimbe7jSZkMhXjU2M+8xcynCM6wUd38jSVpsyuFX+YDVM9CPJYseWYAR2qceRl+Lj/XdYe7EXA0kFjdoSDgPduT6pZ5Iwa0o8/HuKM1HhDGIMv7JEN7hWCyW8PygOFmb/deFvh6XsTqnmBtHGT63CqztuABAgnKHE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727643599; c=relaxed/simple; bh=Oqo2qdNBfGiUDUhpK1HFYYjwJGAT2CrPMHcWSzbsz2w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gkHNRucOzk1ii2vSHlUERTCDx8YS2OdDYJ8WY1a2CcP4uhfmwsGjUzvU43/gcveucZ894KA7RY9GgdQXubX/JoTal4zyKwXTk/Qd3yNLDPc6GDzrgUh2/oCj2CzK1PGlPxPTHZCLKeXmKk7MoxnJ3ABaoWSiMHrvO/vbGWduFS8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MHloHK0D; 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="MHloHK0D" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 858B1C4CEC5; Sun, 29 Sep 2024 20:59:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727643599; bh=Oqo2qdNBfGiUDUhpK1HFYYjwJGAT2CrPMHcWSzbsz2w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MHloHK0DOhwH4MfFZPaIht+ZdK4lvfs4qkPkmZmEGM91b8n0NN2qmvGmYDnqZcItJ 7v6HkWOo8B2XifqS9QyYWXK516Mgootod7pSgcnmkpZL5zl6EaUK2ciqBrLRMm1a6K tCi6ys0N3FFqtHke5yfyb871zB+UquDyyFMy8k42LRdvblw/ruhHcNL1sjgbCxh3cV dZXPD3ZU4VxFpaeqdeiPpSjuhR7sIZr4q9DLvuK4TXP06CihblGyHxlZE3DA4XWt7a /hmstAHCjsb3avo3RFjbokNgEiKWf1l7Y9sJE8wMRv0+pCcG0FYEKXDNdUSKUL2kze 2lWnpJ+1gsZpw== 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: [PATCHv5 bpf-next 13/13] selftests/bpf: Add uprobe sessions to consumer test Date: Sun, 29 Sep 2024 22:57:17 +0200 Message-ID: <20240929205717.3813648-14-jolsa@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240929205717.3813648-1-jolsa@kernel.org> References: <20240929205717.3813648-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 consumers to the consumer test, so we get the session into the test mix. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/uprobe_multi_test.c | 63 ++++++++++++++----- .../bpf/progs/uprobe_multi_consumers.c | 16 ++++- 2 files changed, 64 insertions(+), 15 deletions(-) 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 7e0228f8fcfc..b43107e87e78 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c @@ -765,6 +765,10 @@ get_program(struct uprobe_multi_consumers *skel, int prog) return skel->progs.uprobe_2; case 3: return skel->progs.uprobe_3; + case 4: + return skel->progs.uprobe_4; + case 5: + return skel->progs.uprobe_5; default: ASSERT_FAIL("get_program"); return NULL; @@ -783,6 +787,10 @@ get_link(struct uprobe_multi_consumers *skel, int link) return &skel->links.uprobe_2; case 3: return &skel->links.uprobe_3; + case 4: + return &skel->links.uprobe_4; + case 5: + return &skel->links.uprobe_5; default: ASSERT_FAIL("get_link"); return NULL; @@ -801,8 +809,10 @@ static int uprobe_attach(struct uprobe_multi_consumers *skel, int idx) /* * bit/prog: 0,1 uprobe entry * bit/prog: 2,3 uprobe return + * bit/prog: 4,5 uprobe session */ opts.retprobe = idx == 2 || idx == 3; + opts.session = idx == 4 || idx == 5; *link = bpf_program__attach_uprobe_multi(prog, 0, "/proc/self/exe", "uprobe_consumer_test", @@ -832,13 +842,13 @@ uprobe_consumer_test(struct uprobe_multi_consumers *skel, int idx; /* detach uprobe for each unset programs in 'before' state ... */ - for (idx = 0; idx < 4; idx++) { + for (idx = 0; idx < 6; idx++) { if (test_bit(idx, before) && !test_bit(idx, after)) uprobe_detach(skel, idx); } /* ... and attach all new programs in 'after' state */ - for (idx = 0; idx < 4; idx++) { + for (idx = 0; idx < 6; idx++) { if (!test_bit(idx, before) && test_bit(idx, after)) { if (!ASSERT_OK(uprobe_attach(skel, idx), "uprobe_attach_after")) return -1; @@ -855,7 +865,7 @@ static int consumer_test(struct uprobe_multi_consumers *skel, printf("consumer_test before %lu after %lu\n", before, after); /* 'before' is each, we attach uprobe for every set idx */ - for (idx = 0; idx < 4; idx++) { + for (idx = 0; idx < 6; idx++) { if (test_bit(idx, before)) { if (!ASSERT_OK(uprobe_attach(skel, idx), "uprobe_attach_before")) goto cleanup; @@ -866,7 +876,7 @@ static int consumer_test(struct uprobe_multi_consumers *skel, if (!ASSERT_EQ(err, 0, "uprobe_consumer_test")) goto cleanup; - for (idx = 0; idx < 4; idx++) { + for (idx = 0; idx < 6; idx++) { const char *fmt = "BUG"; __u64 val = 0; @@ -878,18 +888,38 @@ static int consumer_test(struct uprobe_multi_consumers *skel, if (test_bit(idx, before)) val++; fmt = "prog 0/1: uprobe"; - } else { + } else if (idx < 4) { /* * to trigger uretprobe consumer, the uretprobe needs to be installed, * which means one of the 'return' uprobes was alive when probe was hit: * - * idxs: 2/3 uprobe return in 'installed' mask + * idxs: 2/3/4 uprobe return in 'installed' mask */ - unsigned long had_uretprobes = before & 0b1100; /* is uretprobe installed */ + unsigned long had_uretprobes = before & 0b011100; /* is uretprobe installed */ if (had_uretprobes && test_bit(idx, after)) val++; fmt = "idx 2/3: uretprobe"; + } else if (idx == 4) { + /* + * session with return + * +1 if defined in 'before' + * +1 if defined in 'after' + */ + if (test_bit(idx, before)) { + val++; + if (test_bit(idx, after)) + val++; + } + fmt = "idx 4 : session with return"; + } else if (idx == 5) { + /* + * session without return + * +1 if defined in 'before' + */ + if (test_bit(idx, before)) + val++; + fmt = "idx 5 : session with NO return"; } if (!ASSERT_EQ(skel->bss->uprobe_result[idx], val, fmt)) @@ -900,7 +930,7 @@ static int consumer_test(struct uprobe_multi_consumers *skel, ret = 0; cleanup: - for (idx = 0; idx < 4; idx++) + for (idx = 0; idx < 6; idx++) uprobe_detach(skel, idx); return ret; } @@ -920,40 +950,45 @@ static void test_consumers(void) * * - 2 uprobe entry consumer * - 2 uprobe exit consumers + * - 2 uprobe session consumers * - * The test uses 4 uprobes attached on single function, but that - * translates into single uprobe with 4 consumers in kernel. + * The test uses 6 uprobes attached on single function, but that + * translates into single uprobe with 6 consumers in kernel. * * The before/after values present the state of attached consumers * before and after the probed function: * * bit/prog 0,1 : uprobe entry * bit/prog 2,3 : uprobe return + * bit/prog 4 : uprobe session with return + * bit/prog 5 : uprobe session without return * * For example for: * - * before = 0b0101 - * after = 0b0110 + * before = 0b010101 + * after = 0b000110 * * it means that before we call 'uprobe_consumer_test' we attach * uprobes defined in 'before' value: * * - bit/prog 0: uprobe entry * - bit/prog 2: uprobe return + * - bit/prog 4: uprobe session with return * * uprobe_consumer_test is called and inside it we attach and detach * uprobes based on 'after' value: * * - bit/prog 0: stays untouched * - bit/prog 2: uprobe return is detached + * - bit/prog 4: uprobe session is detached * * uprobe_consumer_test returns and we check counters values increased * by bpf programs on each uprobe to match the expected count based on * before/after bits. */ - for (before = 0; before < 16; before++) { - for (after = 0; after < 16; after++) + for (before = 0; before < 64; before++) { + for (after = 0; after < 64; after++) if (consumer_test(skel, before, after)) goto out; } diff --git a/tools/testing/selftests/bpf/progs/uprobe_multi_consumers.c b/tools/testing/selftests/bpf/progs/uprobe_multi_consumers.c index 7e0fdcbbd242..448b753a4c66 100644 --- a/tools/testing/selftests/bpf/progs/uprobe_multi_consumers.c +++ b/tools/testing/selftests/bpf/progs/uprobe_multi_consumers.c @@ -8,7 +8,7 @@ char _license[] SEC("license") = "GPL"; -__u64 uprobe_result[4]; +__u64 uprobe_result[6]; SEC("uprobe.multi") int uprobe_0(struct pt_regs *ctx) @@ -37,3 +37,17 @@ int uprobe_3(struct pt_regs *ctx) uprobe_result[3]++; return 0; } + +SEC("uprobe.session") +int uprobe_4(struct pt_regs *ctx) +{ + uprobe_result[4]++; + return 0; +} + +SEC("uprobe.session") +int uprobe_5(struct pt_regs *ctx) +{ + uprobe_result[5]++; + return 1; +}