From nobody Mon Nov 25 15:46:35 2024 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 2BB4584D29; Sun, 10 Nov 2024 15:50:28 +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=1731253829; cv=none; b=c5E91HgGe9G5SCK64ibXMjm1QFOhCemjUMO44y1gWAwW4xznljLhygMTkdIl7R88oiyeLkPr0Tp8MrKiWAhU3wZZDP9mPR1KBXjtCaKY4eqRorOLcZLGpffBQ6YnfWGY2MUOhgpgPUj4MkWXqbGwTv4L0FSTk+A0uKRJCQy84Rk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731253829; c=relaxed/simple; bh=XOmB0ULOHupz4kFjwpmxxMW5DZpDImZiDmErO/2b6yk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DBMNoCzeRE2MDn65Ly9lP0t6oipB9PlehM3ip2Mab3BHRp2h3Q/2Wa/c1BCCVtNkOPGQGqVCAZZ9gL+Pr/vWMRjYrvWjZam4jrJClYhZfRsb4JACKoOHuFeJlAYXWG6HJXKvlPvTbrnIRwBUDJ46Nk4vvhUzTnCcoX92P5Nz3tw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T3s0Su9A; 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="T3s0Su9A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4A7CC4CECD; Sun, 10 Nov 2024 15:50:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731253828; bh=XOmB0ULOHupz4kFjwpmxxMW5DZpDImZiDmErO/2b6yk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T3s0Su9AcrW+LllZNCjEP4hBnsqGyJPq1eiMYMywsAvjn9OSGEhpaT24K/MIx+8cV iOR8jatFtDQQZSt2eH+I5FnDuBUb7+7h7uO4mpH6Hnw/+ooK4RYDI+Gdpa8QzCzgge inPOTIJJiFlQ4ZtOqYBewgfp1eY/1vV8CZBmlhh40X7ahpQwYM1FuCTQUnOubMGRBz tT5ZmWWRAcvmm0Jc0+ptmJQLN4+Wzh2+4+aNla4c+JhlIRpQy0rbj39bM4S4VPlVul U8pE2Qcb9hzd4N/gDHeB60PcODD7hGpxU3yeuZH+CCVVfxUojES4BcG7Dm7OMw5pHq qApazzqt48pVA== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Alexei Starovoitov , Jiri Olsa , Alan Maguire , Mark Rutland , linux-arch@vger.kernel.org Subject: [PATCH v19 08/19] tracing/fprobe: Enable fprobe events with CONFIG_DYNAMIC_FTRACE_WITH_ARGS Date: Mon, 11 Nov 2024 00:50:23 +0900 Message-ID: <173125382335.172790.9364683419794043855.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173125372214.172790.6929368952404083802.stgit@devnote2> References: <173125372214.172790.6929368952404083802.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Allow fprobe events to be enabled with CONFIG_DYNAMIC_FTRACE_WITH_ARGS. With this change, fprobe events mostly use ftrace_regs instead of pt_regs. Note that if the arch doesn't enable HAVE_FTRACE_REGS_HAVING_PT_REGS, fprobe events will not be able to be used from perf. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v9: - Copy store_trace_entry_data() as store_fprobe_entry_data() for fprobe. Chagnes in v3: - Use ftrace_regs_get_return_value(). Changes in v2: - Define ftrace_regs_get_kernel_stack_nth() for !CONFIG_HAVE_REGS_AND_STACK_ACCESS_API. Changes from previous series: Update against the new series. --- include/linux/ftrace.h | 17 ++++++ kernel/trace/Kconfig | 1=20 kernel/trace/trace_fprobe.c | 107 +++++++++++++++++++++++++----------= ---- kernel/trace/trace_probe_tmpl.h | 2 - 4 files changed, 86 insertions(+), 41 deletions(-) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 6d29c640697c..4c553fe9c026 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -250,6 +250,23 @@ static __always_inline bool ftrace_regs_has_args(struc= t ftrace_regs *fregs) return ftrace_get_regs(fregs) !=3D NULL; } =20 +#ifdef CONFIG_HAVE_REGS_AND_STACK_ACCESS_API +static __always_inline unsigned long +ftrace_regs_get_kernel_stack_nth(struct ftrace_regs *fregs, unsigned int n= th) +{ + unsigned long *stackp; + + stackp =3D (unsigned long *)ftrace_regs_get_stack_pointer(fregs); + if (((unsigned long)(stackp + nth) & ~(THREAD_SIZE - 1)) =3D=3D + ((unsigned long)stackp & ~(THREAD_SIZE - 1))) + return *(stackp + nth); + + return 0; +} +#else /* !CONFIG_HAVE_REGS_AND_STACK_ACCESS_API */ +#define ftrace_regs_get_kernel_stack_nth(fregs, nth) (0L) +#endif /* CONFIG_HAVE_REGS_AND_STACK_ACCESS_API */ + typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs); =20 diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index 7f8165f2049a..82654bbfad9a 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -689,7 +689,6 @@ config FPROBE_EVENTS select TRACING select PROBE_EVENTS select DYNAMIC_EVENTS - depends on DYNAMIC_FTRACE_WITH_REGS default y help This allows user to add tracing events on the function entry and diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c index b68d0aa26097..41595e38fea5 100644 --- a/kernel/trace/trace_fprobe.c +++ b/kernel/trace/trace_fprobe.c @@ -134,7 +134,7 @@ static int process_fetch_insn(struct fetch_insn *code, void *rec, void *edata, void *dest, void *base) { - struct pt_regs *regs =3D rec; + struct ftrace_regs *fregs =3D rec; unsigned long val; int ret; =20 @@ -142,17 +142,17 @@ process_fetch_insn(struct fetch_insn *code, void *rec= , void *edata, /* 1st stage: get value from context */ switch (code->op) { case FETCH_OP_STACK: - val =3D regs_get_kernel_stack_nth(regs, code->param); + val =3D ftrace_regs_get_kernel_stack_nth(fregs, code->param); break; case FETCH_OP_STACKP: - val =3D kernel_stack_pointer(regs); + val =3D ftrace_regs_get_stack_pointer(fregs); break; case FETCH_OP_RETVAL: - val =3D regs_return_value(regs); + val =3D ftrace_regs_get_return_value(fregs); break; #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API case FETCH_OP_ARG: - val =3D regs_get_kernel_argument(regs, code->param); + val =3D ftrace_regs_get_argument(fregs, code->param); break; case FETCH_OP_EDATA: val =3D *(unsigned long *)((unsigned long)edata + code->offset); @@ -175,7 +175,7 @@ NOKPROBE_SYMBOL(process_fetch_insn) /* function entry handler */ static nokprobe_inline void __fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, - struct pt_regs *regs, + struct ftrace_regs *fregs, struct trace_event_file *trace_file) { struct fentry_trace_entry_head *entry; @@ -189,41 +189,71 @@ __fentry_trace_func(struct trace_fprobe *tf, unsigned= long entry_ip, if (trace_trigger_soft_disabled(trace_file)) return; =20 - dsize =3D __get_data_size(&tf->tp, regs, NULL); + dsize =3D __get_data_size(&tf->tp, fregs, NULL); =20 entry =3D trace_event_buffer_reserve(&fbuffer, trace_file, sizeof(*entry) + tf->tp.size + dsize); if (!entry) return; =20 - fbuffer.regs =3D regs; + fbuffer.regs =3D ftrace_get_regs(fregs); entry =3D fbuffer.entry =3D ring_buffer_event_data(fbuffer.event); entry->ip =3D entry_ip; - store_trace_args(&entry[1], &tf->tp, regs, NULL, sizeof(*entry), dsize); + store_trace_args(&entry[1], &tf->tp, fregs, NULL, sizeof(*entry), dsize); =20 trace_event_buffer_commit(&fbuffer); } =20 static void fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, - struct pt_regs *regs) + struct ftrace_regs *fregs) { struct event_file_link *link; =20 trace_probe_for_each_link_rcu(link, &tf->tp) - __fentry_trace_func(tf, entry_ip, regs, link->file); + __fentry_trace_func(tf, entry_ip, fregs, link->file); } NOKPROBE_SYMBOL(fentry_trace_func); =20 +static nokprobe_inline +void store_fprobe_entry_data(void *edata, struct trace_probe *tp, struct f= trace_regs *fregs) +{ + struct probe_entry_arg *earg =3D tp->entry_arg; + unsigned long val =3D 0; + int i; + + if (!earg) + return; + + for (i =3D 0; i < earg->size; i++) { + struct fetch_insn *code =3D &earg->code[i]; + + switch (code->op) { + case FETCH_OP_ARG: + val =3D ftrace_regs_get_argument(fregs, code->param); + break; + case FETCH_OP_ST_EDATA: + *(unsigned long *)((unsigned long)edata + code->offset) =3D val; + break; + case FETCH_OP_END: + goto end; + default: + break; + } + } +end: + return; +} + /* function exit handler */ static int trace_fprobe_entry_handler(struct fprobe *fp, unsigned long ent= ry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data) { struct trace_fprobe *tf =3D container_of(fp, struct trace_fprobe, fp); =20 if (tf->tp.entry_arg) - store_trace_entry_data(entry_data, &tf->tp, regs); + store_fprobe_entry_data(entry_data, &tf->tp, fregs); =20 return 0; } @@ -231,7 +261,7 @@ NOKPROBE_SYMBOL(trace_fprobe_entry_handler) =20 static nokprobe_inline void __fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data, struct trace_event_file *trace_file) { struct fexit_trace_entry_head *entry; @@ -245,60 +275,63 @@ __fexit_trace_func(struct trace_fprobe *tf, unsigned = long entry_ip, if (trace_trigger_soft_disabled(trace_file)) return; =20 - dsize =3D __get_data_size(&tf->tp, regs, entry_data); + dsize =3D __get_data_size(&tf->tp, fregs, entry_data); =20 entry =3D trace_event_buffer_reserve(&fbuffer, trace_file, sizeof(*entry) + tf->tp.size + dsize); if (!entry) return; =20 - fbuffer.regs =3D regs; + fbuffer.regs =3D ftrace_get_regs(fregs); entry =3D fbuffer.entry =3D ring_buffer_event_data(fbuffer.event); entry->func =3D entry_ip; entry->ret_ip =3D ret_ip; - store_trace_args(&entry[1], &tf->tp, regs, entry_data, sizeof(*entry), ds= ize); + store_trace_args(&entry[1], &tf->tp, fregs, entry_data, sizeof(*entry), d= size); =20 trace_event_buffer_commit(&fbuffer); } =20 static void fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, void *entry_data) + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data) { struct event_file_link *link; =20 trace_probe_for_each_link_rcu(link, &tf->tp) - __fexit_trace_func(tf, entry_ip, ret_ip, regs, entry_data, link->file); + __fexit_trace_func(tf, entry_ip, ret_ip, fregs, entry_data, link->file); } NOKPROBE_SYMBOL(fexit_trace_func); =20 #ifdef CONFIG_PERF_EVENTS =20 static int fentry_perf_func(struct trace_fprobe *tf, unsigned long entry_i= p, - struct pt_regs *regs) + struct ftrace_regs *fregs) { struct trace_event_call *call =3D trace_probe_event_call(&tf->tp); struct fentry_trace_entry_head *entry; struct hlist_head *head; int size, __size, dsize; + struct pt_regs *regs; int rctx; =20 head =3D this_cpu_ptr(call->perf_events); if (hlist_empty(head)) return 0; =20 - dsize =3D __get_data_size(&tf->tp, regs, NULL); + dsize =3D __get_data_size(&tf->tp, fregs, NULL); __size =3D sizeof(*entry) + tf->tp.size + dsize; size =3D ALIGN(__size + sizeof(u32), sizeof(u64)); size -=3D sizeof(u32); =20 - entry =3D perf_trace_buf_alloc(size, NULL, &rctx); + entry =3D perf_trace_buf_alloc(size, ®s, &rctx); if (!entry) return 0; =20 + regs =3D ftrace_fill_perf_regs(fregs, regs); + entry->ip =3D entry_ip; memset(&entry[1], 0, dsize); - store_trace_args(&entry[1], &tf->tp, regs, NULL, sizeof(*entry), dsize); + store_trace_args(&entry[1], &tf->tp, fregs, NULL, sizeof(*entry), dsize); perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, head, NULL); return 0; @@ -307,31 +340,34 @@ NOKPROBE_SYMBOL(fentry_perf_func); =20 static void fexit_perf_func(struct trace_fprobe *tf, unsigned long entry_ip, - unsigned long ret_ip, struct pt_regs *regs, + unsigned long ret_ip, struct ftrace_regs *fregs, void *entry_data) { struct trace_event_call *call =3D trace_probe_event_call(&tf->tp); struct fexit_trace_entry_head *entry; struct hlist_head *head; int size, __size, dsize; + struct pt_regs *regs; int rctx; =20 head =3D this_cpu_ptr(call->perf_events); if (hlist_empty(head)) return; =20 - dsize =3D __get_data_size(&tf->tp, regs, entry_data); + dsize =3D __get_data_size(&tf->tp, fregs, entry_data); __size =3D sizeof(*entry) + tf->tp.size + dsize; size =3D ALIGN(__size + sizeof(u32), sizeof(u64)); size -=3D sizeof(u32); =20 - entry =3D perf_trace_buf_alloc(size, NULL, &rctx); + entry =3D perf_trace_buf_alloc(size, ®s, &rctx); if (!entry) return; =20 + regs =3D ftrace_fill_perf_regs(fregs, regs); + entry->func =3D entry_ip; entry->ret_ip =3D ret_ip; - store_trace_args(&entry[1], &tf->tp, regs, entry_data, sizeof(*entry), ds= ize); + store_trace_args(&entry[1], &tf->tp, fregs, entry_data, sizeof(*entry), d= size); perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, head, NULL); } @@ -343,17 +379,14 @@ static int fentry_dispatcher(struct fprobe *fp, unsig= ned long entry_ip, void *entry_data) { struct trace_fprobe *tf =3D container_of(fp, struct trace_fprobe, fp); - struct pt_regs *regs =3D ftrace_get_regs(fregs); int ret =3D 0; =20 - if (!regs) - return 0; - if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE)) - fentry_trace_func(tf, entry_ip, regs); + fentry_trace_func(tf, entry_ip, fregs); + #ifdef CONFIG_PERF_EVENTS if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE)) - ret =3D fentry_perf_func(tf, entry_ip, regs); + ret =3D fentry_perf_func(tf, entry_ip, fregs); #endif return ret; } @@ -364,16 +397,12 @@ static void fexit_dispatcher(struct fprobe *fp, unsig= ned long entry_ip, void *entry_data) { struct trace_fprobe *tf =3D container_of(fp, struct trace_fprobe, fp); - struct pt_regs *regs =3D ftrace_get_regs(fregs); - - if (!regs) - return; =20 if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE)) - fexit_trace_func(tf, entry_ip, ret_ip, regs, entry_data); + fexit_trace_func(tf, entry_ip, ret_ip, fregs, entry_data); #ifdef CONFIG_PERF_EVENTS if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE)) - fexit_perf_func(tf, entry_ip, ret_ip, regs, entry_data); + fexit_perf_func(tf, entry_ip, ret_ip, fregs, entry_data); #endif } NOKPROBE_SYMBOL(fexit_dispatcher); diff --git a/kernel/trace/trace_probe_tmpl.h b/kernel/trace/trace_probe_tmp= l.h index 2caf0d2afb32..f39b37fcdb3b 100644 --- a/kernel/trace/trace_probe_tmpl.h +++ b/kernel/trace/trace_probe_tmpl.h @@ -232,7 +232,7 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsi= gned long val, =20 /* Sum up total data length for dynamic arrays (strings) */ static nokprobe_inline int -__get_data_size(struct trace_probe *tp, struct pt_regs *regs, void *edata) +__get_data_size(struct trace_probe *tp, void *regs, void *edata) { struct probe_arg *arg; int i, len, ret =3D 0;