From nobody Sat Feb 7 22:07:16 2026 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 707A92D9ECA; Fri, 6 Feb 2026 19:58:56 +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=1770407936; cv=none; b=S8b9l5wPWHLTSmlkjBgLDZNWDFVvqmSNjpBv4EaDwI23g3yILZbdXyglLFgvmr7dmBEFNYVdR8WgpTHSNjvlUF+q2OYfluUuWIIjY1kEAL5rAXrAYTppZT5C5dLXClO8stYL2u/xmUYgBT9InMR88GB3jJHmIZJvpzWtBLsuSDQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407936; c=relaxed/simple; bh=TtEiHaNcCXpnjiMUEzl5E1qGp0IhB1/OfPUdEfw+OnA=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=XBt0/gWB2wyUr6zcsdA/OjoAJDph4yp9Hb7w/DAsXCgkA0WU7Wc7kaeLxuo8Vw4zaS1IBS613a+Rpu5Y6E8QEOJiSX4Hq9dRDGW8qi1r36lhbBQCu1MERMDENdbbwQjDtYXgWRJP/DRSacv3mFCgLIgc5y6J4Eis/6QKLqprHXU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Z9F/lfVz; 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="Z9F/lfVz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 16F35C19423; Fri, 6 Feb 2026 19:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407936; bh=TtEiHaNcCXpnjiMUEzl5E1qGp0IhB1/OfPUdEfw+OnA=; h=Date:From:To:Cc:Subject:References:From; b=Z9F/lfVzGrH/9J8LmK8WMJbSh6YyhhcoXfv8rC0B1/dFzdyShWdkUzQrO/quBtGN9 yrx9f0xOIFa/Zdqeb4Vlp18Fm7DFGIhvbHHknVLMGmrSB5lzZys3a8fcQrXs1d4BMR BXbRoPRNwRCSKoH2zP3RI0YunQkU2UbpgWvIU8bCNf673mYpuTITcgopYSl7mf5cOS 0tK/7kho+63pERspr/gDMKn5Gwiat5repKibW2rfG0pkYbiaTq1WPXBhu/qvax2fjO WiKFNWjDKQ2HP9amfGdJ98wRigtGEWsRu8pvIfr4DjdiZaVtT2Tbo5dgY6K5pREwbh 7AIcSizw3A+lw== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzL-0000000A6mI-0SDz; Fri, 06 Feb 2026 14:59:35 -0500 Message-ID: <20260206195934.961147222@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:42 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 01/12] tracing: Make tracing_disabled global for tracing system References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The tracing_disabled variable is set to one on boot up to prevent some parts of tracing to access the tracing infrastructure before it is set up. It also can be set after boot if an anomaly is discovered. It is currently a static variable in trace.c and can be accessed via a function call trace_is_disabled(). There's really no reason to use a function call as the tracing subsystem should be able to access it directly. By making the variable accessed directly, code can be moved out of trace.c without adding overhead of a function call to see if tracing is disabled or not. Make tracing_disabled global and remove the tracing_is_disabled() helper function. Also add some "unlikely()"s around tracing_disabled where it's checked in hot paths. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 13 ++++--------- kernel/trace/trace.h | 3 ++- kernel/trace/trace_events.c | 2 +- kernel/trace/trace_kprobe.c | 2 +- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 19b5a347a8fc..45972d089061 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -114,7 +114,7 @@ DEFINE_PER_CPU(bool, trace_taskinfo_save); * of the tracer is successful. But that is the only place that sets * this back to zero. */ -static int tracing_disabled =3D 1; +int tracing_disabled =3D 1; =20 cpumask_var_t __read_mostly tracing_buffer_mask; =20 @@ -3425,7 +3425,7 @@ int __trace_array_vprintk(struct trace_buffer *buffer, unsigned int trace_ctx; char *tbuffer; =20 - if (tracing_disabled) + if (unlikely(tracing_disabled)) return 0; =20 /* Don't pollute graph traces with trace_vprintk internals */ @@ -4767,11 +4767,6 @@ int tracing_open_generic(struct inode *inode, struct= file *filp) return 0; } =20 -bool tracing_is_disabled(void) -{ - return (tracing_disabled) ? true: false; -} - /* * Open and update trace_array ref count. * Must have the current trace_array passed to it. @@ -7611,7 +7606,7 @@ tracing_mark_write(struct file *filp, const char __us= er *ubuf, unsigned long ip; char *buf; =20 - if (tracing_disabled) + if (unlikely(tracing_disabled)) return -EINVAL; =20 if (!(tr->trace_flags & TRACE_ITER(MARKERS))) @@ -7691,7 +7686,7 @@ tracing_mark_raw_write(struct file *filp, const char = __user *ubuf, ssize_t written =3D -ENODEV; char *buf; =20 - if (tracing_disabled) + if (unlikely(tracing_disabled)) return -EINVAL; =20 if (!(tr->trace_flags & TRACE_ITER(MARKERS))) diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index f1466e57ffb2..fda87c9f84bc 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -662,6 +662,8 @@ trace_buffer_iter(struct trace_iterator *iter, int cpu) return iter->buffer_iter ? iter->buffer_iter[cpu] : NULL; } =20 +extern int tracing_disabled; + int tracer_init(struct tracer *t, struct trace_array *tr); int tracing_is_enabled(void); void tracing_reset_online_cpus(struct array_buffer *buf); @@ -673,7 +675,6 @@ int tracing_release_generic_tr(struct inode *inode, str= uct file *file); int tracing_open_file_tr(struct inode *inode, struct file *filp); int tracing_release_file_tr(struct inode *inode, struct file *filp); int tracing_single_release_file_tr(struct inode *inode, struct file *filp); -bool tracing_is_disabled(void); bool tracer_tracing_is_on(struct trace_array *tr); void tracer_tracing_on(struct trace_array *tr); void tracer_tracing_off(struct trace_array *tr); diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c index af6d1fe5cab7..61fe01dce7a6 100644 --- a/kernel/trace/trace_events.c +++ b/kernel/trace/trace_events.c @@ -2268,7 +2268,7 @@ static int subsystem_open(struct inode *inode, struct= file *filp) struct event_subsystem *system =3D NULL; int ret; =20 - if (tracing_is_disabled()) + if (unlikely(tracing_disabled)) return -ENODEV; =20 /* Make sure the system still exists */ diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 9953506370a5..48311300205d 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -2079,7 +2079,7 @@ static __init int kprobe_trace_self_tests_init(void) struct trace_kprobe *tk; struct trace_event_file *file; =20 - if (tracing_is_disabled()) + if (unlikely(tracing_disabled)) return -ENODEV; =20 if (tracing_selftest_disabled) --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 707312848B2; Fri, 6 Feb 2026 19:58:56 +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=1770407936; cv=none; b=N3zf0DcU1drFcnOamNoSKc/YRaRdfaifyRkivB6Cp7HtVF3NGLwa6Z2JsjLao1VH/bEEPqEzSfg1kIlKefbj6Pyrm9orKdX9Seugp2+bjhtok9Kkq3MXWEtvdkTe284KmJ+3PDqWmS/xLj7kL1LvwA4MBbEU87YnZx2/8z97b9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407936; c=relaxed/simple; bh=gc3vlVl9zWzfzqzsNtyK2LNgYikkI/ztgXn4fvwCRAk=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=hDwh6vsEDrinfET+EyjmIDK3io2y3GFXtL6mambAxcChqVubdKTgqUdB7oNAMWeJ8g+lj1p2q5INVf6mRNnv2PW0iRTrRQ6i4oYqAsgf6EoM9P1ff5QP3n49GtwekfzZ0k7mIoSFkyYIuR6EywkMFHcWJHag9CcwmcxDn/yuUCI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZLb4LkZf; 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="ZLb4LkZf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25BB7C19422; Fri, 6 Feb 2026 19:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407936; bh=gc3vlVl9zWzfzqzsNtyK2LNgYikkI/ztgXn4fvwCRAk=; h=Date:From:To:Cc:Subject:References:From; b=ZLb4LkZfqAbjrVD5poj0HirUZUWTMgB+yrccRj6buOIzZl5C5cOk462UUI1E/O76q CyTn+ETa36kC3e8/rxklCeC/DOzkTm4FQPbftgl+J2J6s66se3ddmkEGBKvsqzTVE+ ftRlOreH+GJ4YMnv2XifhhXISm/0utz4ppLZKsNcnrZ3meNMPNUtCgysGvu1GQC5N8 i16F6ksnqIJokNsRIE9M3cwqwRvRNDsOu3WprEb9anw4HQd55vIob6g+MPys77QzKx 3CuhW8dyUKGjBKNDltpe6E01oyRFhDDsIwzTqUW7Rd/TkELS6Ac5p1hmpjlMiXMT3J q+HSwj1kw1kZA== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzL-0000000A6mm-18IP; Fri, 06 Feb 2026 14:59:35 -0500 Message-ID: <20260206195935.127453448@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:43 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 02/12] tracing: Make tracing_selftest_running global to the tracing subsystem References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The file trace.c has become a catchall for most things tracing. Start making it smaller by breaking out various aspects into their own files. Make the variable tracing_selftest_running global so that it can be used by other files in the tracing subsystem and trace.c can be split up. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 3 +-- kernel/trace/trace.h | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 45972d089061..dafcdd31570b 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -67,7 +67,7 @@ * insertions into the ring-buffer such as trace_printk could occurred * at the same time, giving false positive or negative results. */ -static bool __read_mostly tracing_selftest_running; +bool __read_mostly tracing_selftest_running; =20 /* * If boot-time tracing including tracers/events via kernel cmdline @@ -83,7 +83,6 @@ void __init disable_tracing_selftest(const char *reason) } } #else -#define tracing_selftest_running 0 #define tracing_selftest_disabled 0 #endif =20 diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index fda87c9f84bc..ccfaa9b4d2bf 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -867,6 +867,7 @@ extern int trace_selftest_startup_nop(struct tracer *tr= ace, struct trace_array *tr); extern int trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr); +extern bool __read_mostly tracing_selftest_running; /* * Tracer data references selftest functions that only occur * on boot up. These can be __init functions. Thus, when selftests @@ -879,6 +880,7 @@ static inline void __init disable_tracing_selftest(cons= t char *reason) } /* Tracers are seldom changed. Optimize when selftests are disabled. */ #define __tracer_data __read_mostly +#define tracing_selftest_running 0 #endif /* CONFIG_FTRACE_STARTUP_TEST */ =20 extern void *head_page(struct trace_array_cpu *data); --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 777C632AAAE; Fri, 6 Feb 2026 19:58:56 +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=1770407936; cv=none; b=mmR9eHpTxLXniqwpQYW+sPsGMyYaBL3HdJjBB9Ff8QP2OKbCRR9NLyilnnLw+t0xJPkLAjBJ8K00KmHrdMKkb/cWA3+V+ZeVz/pE7VD1aDgN76GcbXPVLiQdCJe6jqqDzaxOUuugePyp322hpbclzea3kSLCbQWYyjiMK1Zs9V0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407936; c=relaxed/simple; bh=hc6PrYVuC/Wg7y0SBTDSMwFVfst74rco+Jjkpoa75B8=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=GHsWzhqV6y+92RDkKsh7yy1+pBapBdrRqGmwEbEfSzNOlbOh/BrlNrQ6cYsiXq0EzMZsEOSatXSQ570Na55xKFvBKV43Qpi+FBd5ZwhQarvcfUcZjoX21KCb5XOLj5+l3ElMRMR7q5ow/UcEKG3svYf2Fg8bcKU9aHJu6g6CzS0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nf1ybEIy; 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="nf1ybEIy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 558A9C2BC87; Fri, 6 Feb 2026 19:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407936; bh=hc6PrYVuC/Wg7y0SBTDSMwFVfst74rco+Jjkpoa75B8=; h=Date:From:To:Cc:Subject:References:From; b=nf1ybEIyyxUzuUwoIrdbgZuv6Xo0HSvILPv9cc1jDgg+qPnVgiuZaMcEfaJsv5C4J YKp6cgeiNoBkr4r9X6Zen0bE1ZHPJjj41JRQDBjq8wFYKasXuHAv0nziykfxdZFKHH /8pRd9bVIFH8U6RtxeabTD0S7h9KmjpIoJyXKWtskw8xgjrBXq4cYcuCb1whxGxkhE syT5z5KGleteIVyC6lr88Oq1lx7MZrmmoizec9vSGiq7oaR9xNAhnO3o/rA9P5apqY lbmZHYNcrRHV1WPYSdlX2+O0X5KDRdkKIAYUzOD1dLlPb9DO1EsA7Aqj6PSjipreqY xEtwZe3F+EaQw== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzL-0000000A6nG-1poz; Fri, 06 Feb 2026 14:59:35 -0500 Message-ID: <20260206195935.293094674@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:44 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 03/12] tracing: Move __trace_buffer_{un}lock_*() functions to trace.h References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The file trace.c has become a catchall for most things tracing. Start making it smaller by breaking out various aspects into their own files. Move the __always_inline functions __trace_buffer_lock_reserve(), __trace_buffer_unlock_commit() and trace_event_setup() into trace.h. The trace.c file will be split up and these functions will be used in more than one of these files. As they are already __always_inline they can easily be moved into the trace.h header file. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 42 ------------------------------------------ kernel/trace/trace.h | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index dafcdd31570b..42b7ad09b5d9 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1058,30 +1058,6 @@ static inline void ftrace_trace_stack(struct trace_a= rray *tr, =20 #endif =20 -static __always_inline void -trace_event_setup(struct ring_buffer_event *event, - int type, unsigned int trace_ctx) -{ - struct trace_entry *ent =3D ring_buffer_event_data(event); - - tracing_generic_entry_update(ent, type, trace_ctx); -} - -static __always_inline struct ring_buffer_event * -__trace_buffer_lock_reserve(struct trace_buffer *buffer, - int type, - unsigned long len, - unsigned int trace_ctx) -{ - struct ring_buffer_event *event; - - event =3D ring_buffer_lock_reserve(buffer, len); - if (event !=3D NULL) - trace_event_setup(event, type, trace_ctx); - - return event; -} - void tracer_tracing_on(struct trace_array *tr) { if (tr->array_buffer.buffer) @@ -1109,24 +1085,6 @@ void tracing_on(void) } EXPORT_SYMBOL_GPL(tracing_on); =20 - -static __always_inline void -__buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_eve= nt *event) -{ - __this_cpu_write(trace_taskinfo_save, true); - - /* If this is the temp buffer, we need to commit fully */ - if (this_cpu_read(trace_buffered_event) =3D=3D event) { - /* Length is in event->array[0] */ - ring_buffer_write(buffer, event->array[0], &event->array[1]); - /* Release the temp buffer */ - this_cpu_dec(trace_buffered_event_cnt); - /* ring_buffer_unlock_commit() enables preemption */ - preempt_enable_notrace(); - } else - ring_buffer_unlock_commit(buffer); -} - int __trace_array_puts(struct trace_array *tr, unsigned long ip, const char *str, int size) { diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index ccfaa9b4d2bf..d22d761f8ebd 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -1572,6 +1572,47 @@ char *trace_user_fault_read(struct trace_user_buf_in= fo *tinfo, const char __user *ptr, size_t size, trace_user_buf_copy copy_func, void *data); =20 +static __always_inline void +trace_event_setup(struct ring_buffer_event *event, + int type, unsigned int trace_ctx) +{ + struct trace_entry *ent =3D ring_buffer_event_data(event); + + tracing_generic_entry_update(ent, type, trace_ctx); +} + +static __always_inline struct ring_buffer_event * +__trace_buffer_lock_reserve(struct trace_buffer *buffer, + int type, + unsigned long len, + unsigned int trace_ctx) +{ + struct ring_buffer_event *event; + + event =3D ring_buffer_lock_reserve(buffer, len); + if (event !=3D NULL) + trace_event_setup(event, type, trace_ctx); + + return event; +} + +static __always_inline void +__buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_eve= nt *event) +{ + __this_cpu_write(trace_taskinfo_save, true); + + /* If this is the temp buffer, we need to commit fully */ + if (this_cpu_read(trace_buffered_event) =3D=3D event) { + /* Length is in event->array[0] */ + ring_buffer_write(buffer, event->array[0], &event->array[1]); + /* Release the temp buffer */ + this_cpu_dec(trace_buffered_event_cnt); + /* ring_buffer_unlock_commit() enables preemption */ + preempt_enable_notrace(); + } else + ring_buffer_unlock_commit(buffer); +} + static inline void __trace_event_discard_commit(struct trace_buffer *buffer, struct ring_buffer_event *event) --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 A02A53376A0; Fri, 6 Feb 2026 19:58:56 +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=1770407936; cv=none; b=CMc5OPgnMdJImH0hldCFPAlvktZxgLL/kJcXtOYWXwPlckqXgH2rPFxQEYYVoByfLYAD9yQEOFeRZrxco9gxUkb7CmnoPzUiCcltoUrl/q5aVhZX3++OX/XfUM9vfWuH51AU14yEE13sUBjfBNShQg889JMaUwgm3BihCUp0ORQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407936; c=relaxed/simple; bh=AE+4adVyJFCbyi7b/Eh27pv6ER2+eOGn0BxhQCPrptw=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=rfzAg0Xg4Jq+dWpttPnOAWe6UqTD1ACFHmn/NsnzrkSIotc34ktep9bGj49YWSyptSRgxCL/xAVxl3l71jOCsaoi6RygMoxZCnSwnPA9EIM2iUULoBEG6kfVHhkkGVRiUT007aG+w8/ky8/1y+ztIBqojQCbaced0brGMA8XX5M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UFpecw1F; 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="UFpecw1F" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7BD22C19425; Fri, 6 Feb 2026 19:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407936; bh=AE+4adVyJFCbyi7b/Eh27pv6ER2+eOGn0BxhQCPrptw=; h=Date:From:To:Cc:Subject:References:From; b=UFpecw1Fy+N2l9nsd+GXJABUA+UmAUky97AYtBBXsEyIAbSDiRSJg9EnQgb6u2WQB OCyl0lxOzttvrFQx1I0a1Hq6E2/hK2SYBytU4bWAPrxpTfK5y3mRx2lKjM/v3D98TG PxbGzrVgLxglbJoF6m5R0/CGU34hhOwHEk/gJwwrKKt3K26HZGM6UmQODXRAlaGTIX FoMYoYcrhuURqWZRppXWWz5fRJi8zEZu6kBrNapWE3GxQOHyYO/P7g0tC0PDANS4IX SC+i9V2jVJGPFz80CzeCXeLbi57Kvc1KOv/BT28JiMwbZx/dqyKHQWiTRjdl3bl8hg IlrDgdM8WSf/Q== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzL-0000000A6nk-2Y9o; Fri, 06 Feb 2026 14:59:35 -0500 Message-ID: <20260206195935.463072570@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:45 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 04/12] tracing: Move ftrace_trace_stack() out of trace.c and into trace.h References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The file trace.c has become a catchall for most things tracing. Start making it smaller by breaking out various aspects into their own files. Make ftrace_trace_stack() into a static inline that tests if stack tracing is enabled and if so to call __ftrace_trace_stack() to do the stack trace. This keeps the test inlined in the fast paths and only does the function call if stack tracing is enabled. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 45 ++++---------------------------------------- kernel/trace/trace.h | 31 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 42b7ad09b5d9..061c154c0618 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1032,32 +1032,6 @@ static inline void trace_access_lock_init(void) =20 #endif =20 -#ifdef CONFIG_STACKTRACE -static void __ftrace_trace_stack(struct trace_array *tr, - struct trace_buffer *buffer, - unsigned int trace_ctx, - int skip, struct pt_regs *regs); -static inline void ftrace_trace_stack(struct trace_array *tr, - struct trace_buffer *buffer, - unsigned int trace_ctx, - int skip, struct pt_regs *regs); - -#else -static inline void __ftrace_trace_stack(struct trace_array *tr, - struct trace_buffer *buffer, - unsigned int trace_ctx, - int skip, struct pt_regs *regs) -{ -} -static inline void ftrace_trace_stack(struct trace_array *tr, - struct trace_buffer *buffer, - unsigned long trace_ctx, - int skip, struct pt_regs *regs) -{ -} - -#endif - void tracer_tracing_on(struct trace_array *tr) { if (tr->array_buffer.buffer) @@ -2966,10 +2940,10 @@ struct ftrace_stacks { static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks); static DEFINE_PER_CPU(int, ftrace_stack_reserve); =20 -static void __ftrace_trace_stack(struct trace_array *tr, - struct trace_buffer *buffer, - unsigned int trace_ctx, - int skip, struct pt_regs *regs) +void __ftrace_trace_stack(struct trace_array *tr, + struct trace_buffer *buffer, + unsigned int trace_ctx, + int skip, struct pt_regs *regs) { struct ring_buffer_event *event; unsigned int size, nr_entries; @@ -3052,17 +3026,6 @@ static void __ftrace_trace_stack(struct trace_array = *tr, trace_clear_recursion(bit); } =20 -static inline void ftrace_trace_stack(struct trace_array *tr, - struct trace_buffer *buffer, - unsigned int trace_ctx, - int skip, struct pt_regs *regs) -{ - if (!(tr->trace_flags & TRACE_ITER(STACKTRACE))) - return; - - __ftrace_trace_stack(tr, buffer, trace_ctx, skip, regs); -} - void __trace_stack(struct trace_array *tr, unsigned int trace_ctx, int skip) { diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index d22d761f8ebd..63260595ac8f 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -2283,6 +2283,37 @@ static inline void sanitize_event_name(char *name) *name =3D '_'; } =20 +#ifdef CONFIG_STACKTRACE +void __ftrace_trace_stack(struct trace_array *tr, + struct trace_buffer *buffer, + unsigned int trace_ctx, + int skip, struct pt_regs *regs); + +static __always_inline void ftrace_trace_stack(struct trace_array *tr, + struct trace_buffer *buffer, + unsigned int trace_ctx, + int skip, struct pt_regs *regs) +{ + if (!(tr->trace_flags & TRACE_ITER(STACKTRACE))) + return; + + __ftrace_trace_stack(tr, buffer, trace_ctx, skip, regs); +} +#else +static inline void __ftrace_trace_stack(struct trace_array *tr, + struct trace_buffer *buffer, + unsigned int trace_ctx, + int skip, struct pt_regs *regs) +{ +} +static inline void ftrace_trace_stack(struct trace_array *tr, + struct trace_buffer *buffer, + unsigned long trace_ctx, + int skip, struct pt_regs *regs) +{ +} +#endif + /* * This is a generic way to read and write a u64 value from a file in trac= efs. * --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 B2D773382DA; Fri, 6 Feb 2026 19:58:56 +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=1770407936; cv=none; b=nopS7Vw9Gz7id0W2VA0XaqDfeDSb+YW4RlUDnWFrPoCAHULS4GxaYMBr/vQsPEzOV5Ei0ofRN+W4NwmX6erKeOapYm2zt4oqtXuBeD9wlaNqdyXHt/o7Hv/M63zcyqDasJwW3QuWZBe5E0FFPYu9rRIYfK1iT+UP0ddMCZGTyeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407936; c=relaxed/simple; bh=s29KvyR1cTcgp9eIV9JmO39WhdYvRZ0cDxzveXQC3lw=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=Hj+vt5Zi/ygcoX6rOyxOawKHtPbJMQqqJAHEfUJvE+a5n5rEYLs8ZG9VrhCsetkFcdekNkCJQqtql54t0qRfL4manWyqWSTb1gDXuXYeZn+57bk+LJIeQL9s08HkDuULMkoqBUCX2LHmw67N3l4fQXeDb1RhtSmkLlREGrT43RQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EdY+g/6P; 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="EdY+g/6P" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 92F63C116C6; Fri, 6 Feb 2026 19:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407936; bh=s29KvyR1cTcgp9eIV9JmO39WhdYvRZ0cDxzveXQC3lw=; h=Date:From:To:Cc:Subject:References:From; b=EdY+g/6PzFU6A6qmAUCcnFS+VXEGVjxoAtZW8fMP9g+lVe97I/LIpzndhPDhStjiY x7o7y/1ic74t2bT5NWbbp/4W7ePF6Q8KtBHmWKogt03v5Fcdm1SoNffJo/sbDYvWRm dH/NoeNsZlDdTZu2vz65/y4kbM+Ym+KTR/h5/5HPmOjIsOydbNupeTRAMu49y1cXbo JJyV4enGt/9GWH27S51e/O89zWgJbu+HnmW24U02uVgU33NMZMVS7wQGOLseED1VaA JifoQuvnvf9KOJosFq2OAfG443f+kwy4+IDoV1aEDs4k3qH+Jme0Qros49p5Yme8cW sxOpfv03xI89Q== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzL-0000000A6oE-3EiB; Fri, 06 Feb 2026 14:59:35 -0500 Message-ID: <20260206195935.625485805@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:46 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 05/12] tracing: Make printk_trace global for tracing system References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The printk_trace is used to determine which trace_array trace_printk() writes to. By making it a global variable among the tracing subsystem it will allow the trace_printk functions to be moved out of trace.c and still have direct access to that variable. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 2 +- kernel/trace/trace.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 061c154c0618..1170fcf686a6 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -534,7 +534,7 @@ static struct trace_array global_trace =3D { .trace_flags =3D TRACE_DEFAULT_FLAGS, }; =20 -static struct trace_array *printk_trace =3D &global_trace; +struct trace_array *printk_trace =3D &global_trace; =20 /* List of trace_arrays interested in the top level trace_marker */ static LIST_HEAD(marker_copies); diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 63260595ac8f..73a2ab195114 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -487,6 +487,8 @@ extern bool trace_clock_in_ns(struct trace_array *tr); =20 extern unsigned long trace_adjust_address(struct trace_array *tr, unsigned= long addr); =20 +extern struct trace_array *printk_trace; + /* * The global tracer (top) should be the first trace array added, * but we check the flag anyway. --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 E2ACA338F26; Fri, 6 Feb 2026 19:58:56 +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=1770407937; cv=none; b=oBFWvkxISp+IaiIT7w9lsIKbGRO5zrryzGRCPRvShSkHLVBBZDtzI3F/LhFV2FcLRXM/gKBfjdjVdQGHHHQpfscpQNygN3Br8dODIN9uKOEZGbPdn6zBIrbmy8C38466KHSOEuDhXfCLgOt1ficqJWAm9TwuF1CaWG1OQKkB+n4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407937; c=relaxed/simple; bh=v7tx0fb8mN8IPP3PDF9fvTs6sEupxknIdsg9SHi8S4s=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=pT7hNl35m7SLQnb+Np9ty5HdWRWvlS6LS8u0DpTLw6GmioKcY8QVwHN7XvIpeBZuGR9AFvvelAED67Rj6+UMnGDOv3rsNNJn4Fn3fMrmt6lWckl9LhGykhM5gRU2rZkweIaP51asIy8xJnK9XnskUlQCspd28YZaTm7GR7+Sj6M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mr+RBO/G; 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="Mr+RBO/G" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B96B9C19424; Fri, 6 Feb 2026 19:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407936; bh=v7tx0fb8mN8IPP3PDF9fvTs6sEupxknIdsg9SHi8S4s=; h=Date:From:To:Cc:Subject:References:From; b=Mr+RBO/GiZkNnYqVwI+jC5uXYnMQKyhzb14ajjazViUfOICHIUeYn64qwIj5fLc/Q HhaMRicu/uiEFm6moZbZPW7GJ5DEbdRKw6nKJUIsvlR0oqbuRT1YfQk94rgZXvzhwF Ym3yE5Tfg1rD2T/JW7QgSbUvdhuXZYFyPpoK84F+ChIGu+Dq23TEtcpGuSgqTpdbIL sLFR3ICBhLN63u3XhRMa1SGWzgjMEjEjdp9sh8xGDPJyXWZ9DfUmyLg89shmmh9peL CdPnalnAor3oSCsXXhZqRjnpOiXHZ5Mg/t5if2hnpsYv/sDqRFA1vI9cHeOpfs9ZtF cyXVRJe+j9ScQ== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzL-0000000A6oi-3ufd; Fri, 06 Feb 2026 14:59:35 -0500 Message-ID: <20260206195935.791669128@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:47 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 06/12] tracing: Make tracing_update_buffers() take NULL for global_trace References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The trace.c file has become a dumping ground for all tracing code and has become quite large. In order to move the trace_printk functions out of it these functions can not access global_trace directly, as that is something that needs to stay static in trace.c. Have tracing_update_buffers() take NULL for its trace_array to denote it should work on the global_trace top level trace_array allows that function to be used outside of trace.c and still update the global_trace trace_array. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 1170fcf686a6..a68948ac8bf9 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -3236,7 +3236,7 @@ void trace_printk_init_buffers(void) pr_warn("**********************************************************\n"); =20 /* Expand the buffers to set size */ - if (tracing_update_buffers(&global_trace) < 0) + if (tracing_update_buffers(NULL) < 0) pr_err("Failed to expand tracing buffers for trace_printk() calls\n"); else buffers_allocated =3D 1; @@ -6188,6 +6188,9 @@ int tracing_update_buffers(struct trace_array *tr) { int ret =3D 0; =20 + if (!tr) + tr =3D &global_trace; + guard(mutex)(&trace_types_lock); =20 update_last_data(tr); --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 1546A33A01E; Fri, 6 Feb 2026 19:58:57 +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=1770407937; cv=none; b=lsjggLYrJpaHmzPoM08zAW0jabV+Z81gkS7RmcKZyYwCf5CeO7zGEeiznllVDVMLFXH7l/8bOL8+8oHSD/qgMpiKFWFrhVzLu2rGoDzl6FAVRhxLeVjk+g1G+A+6R1xf3b54u1QNU7lBWnvIXijxfaSTq9xlXMXHcOCNQ1ZSqgw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407937; c=relaxed/simple; bh=SYZ1Rq8DSyU94U+FTXKwm9x7vluarbRusnyLeVZ0CUY=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=LisjGLNbsw9/Wdo7slthkP8oWZbfIErpXrezrAPUacCvynWxfGLRvDtqVad/fRoRWLN1bCXAdf8Cu/kVzNo/U+5Sx8Zdq/H2yu31q4trvpIjzfzeP4Iosoyv/0LD4G2yjOClskrX6ZhUgN+baQUeH77aXA8oPuuv3z4q+I6GS6M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=q1T1D5vh; 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="q1T1D5vh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D8A7DC2BCB3; Fri, 6 Feb 2026 19:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407936; bh=SYZ1Rq8DSyU94U+FTXKwm9x7vluarbRusnyLeVZ0CUY=; h=Date:From:To:Cc:Subject:References:From; b=q1T1D5vh0Rv65qzVNnYrU9hbK4faB8CHLdRlemizd6XrVCWwRorCR8BaeOC06PWoY 3lrVSwv5F/VHc5VPIKO0PTdma6X2yxuOUmdO2hsNR8KU6QNAZdyHDruxgCL3KwmvaY mReUaovtbUkh/Ujzy2MGOc8wDyDhXMAjmmkio0Jz22z50FqBu9zxRbf0OvF864oc26 2QINTkkHB87DMgNXBWhDSzftyoibfy8bAbtwWt+DXd4GXWwm2pnlyeapChAX39fQMF Hvh0RJSPal7RBf3uYDtcrkN0vBKi7zQBJp2Bzl4ABwa338L5GDu0D505RZJA98Xgr4 WJQ8qfNfarD7A== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzM-0000000A6pC-0Pgu; Fri, 06 Feb 2026 14:59:36 -0500 Message-ID: <20260206195935.952012814@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:48 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 07/12] tracing: Have trace_printk functions use flags instead of using global_trace References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The trace.c file has become a dumping ground for all tracing code and has become quite large. In order to move the trace_printk functions out of it these functions can not access global_trace directly, as that is something that needs to stay static in trace.c. Instead of testing the trace_array tr pointer to &global_trace, test the tr->flags to see if TRACE_ARRAY_FL_GLOBAL set. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index a68948ac8bf9..db36d4aebc15 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1071,7 +1071,8 @@ int __trace_array_puts(struct trace_array *tr, unsign= ed long ip, if (!(tr->trace_flags & TRACE_ITER(PRINTK))) return 0; =20 - if (unlikely(tracing_selftest_running && tr =3D=3D &global_trace)) + if (unlikely(tracing_selftest_running && + (tr->flags & TRACE_ARRAY_FL_GLOBAL))) return 0; =20 if (unlikely(tracing_disabled)) @@ -3388,7 +3389,7 @@ int __trace_array_vprintk(struct trace_buffer *buffer, int trace_array_vprintk(struct trace_array *tr, unsigned long ip, const char *fmt, va_list args) { - if (tracing_selftest_running && tr =3D=3D &global_trace) + if (tracing_selftest_running && (tr->flags & TRACE_ARRAY_FL_GLOBAL)) return 0; =20 return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args); @@ -3424,7 +3425,7 @@ int trace_array_printk(struct trace_array *tr, return -ENOENT; =20 /* This is only allowed for created instances */ - if (tr =3D=3D &global_trace) + if (tr->flags & TRACE_ARRAY_FL_GLOBAL) return 0; =20 if (!(tr->trace_flags & TRACE_ITER(PRINTK))) @@ -3451,7 +3452,7 @@ int trace_array_init_printk(struct trace_array *tr) return -ENOENT; =20 /* This is only allowed for created instances */ - if (tr =3D=3D &global_trace) + if (tr->flags & TRACE_ARRAY_FL_GLOBAL) return -EINVAL; =20 return alloc_percpu_trace_buffer(); --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 260C633A6F7; Fri, 6 Feb 2026 19:58:57 +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=1770407937; cv=none; b=TQ1BPaPfy2oq6Z/RUTrEIKL87a2sew520vipc/EDWKcFpwesX1PHiasDJGEOXTuZZHsX3CB9EyQRswodPT1RjiqKCM8pAhtyI5AgImXE24ttRELjiCHPdxAARW+4WholiGg21u6STd00S41aVP8Qni//y/nAv/ZOvejJ6BjCTro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407937; c=relaxed/simple; bh=ay4wjzfNs4Vg2jkQLNy4bi/QQ7cVserI/t7ElR3WQy4=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=MGRDENR9WiAFcQ/B0LBtmBZv0rde7Y1SKO2rnQ/SY24knLXcUQiN69zsnV+OslTZfKGPItqRsTouO3AFlBcRgUT7dX/x6D6w6KCgPOISy+BBf7h07DjWci+7jtPs02l24pz9qI/dVDHqB9+WvrdZ+6JxU2LH730INQSCJs6/Ggg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kyQYJURD; 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="kyQYJURD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 06BF3C19425; Fri, 6 Feb 2026 19:58:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407937; bh=ay4wjzfNs4Vg2jkQLNy4bi/QQ7cVserI/t7ElR3WQy4=; h=Date:From:To:Cc:Subject:References:From; b=kyQYJURDPGmAyX3E5TvR2pDNjJEVmmwLTb5i/EfjpkoRIVnJENveQKWUzAO9p1eB6 hRPDS4Q0/sj8VAe2PuvQvX18SSTT01odu8kyQCoyiaIn3w/sjOkYh+vpwwgAUixwfc QEyZt2rj4yepSyr1VTAl/zNyTSIs33czonuetrqn9KIxDsI0zp4KJiipL7ly5B2F3e YWc8w6EpeHgf3RD0MLqHf3WphkCO27NvgiPazU5h4r8iSjP3wQpXz/fHwZYeFprked YNkrYxCSkM951QZV0ryboM0PqqgcEPlvdvxQ+MdLumVJ9baD5te2k6zpT2rdLW755R 3qIC1pOEoTi0A== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzM-0000000A6ph-17Em; Fri, 06 Feb 2026 14:59:36 -0500 Message-ID: <20260206195936.118522327@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:49 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 08/12] tracing: Use system_state in trace_printk_init_buffers() References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The function trace_printk_init_buffers() is used to expand tha trace_printk buffers when trace_printk() is used within the kernel or in modules. On kernel boot up, it holds off from starting the sched switch cmdline recorder, but will start it immediately when it is added by a module. Currently it uses a trick to see if the global_trace buffer has been allocated or not to know if it was called by module load or not. But this is more of a hack, and can not be used when this code is moved out of trace.c. Instead simply look at the system_state and if it is running then it is know that it could only be called by module load. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index db36d4aebc15..3b7fa2699301 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -3245,10 +3245,9 @@ void trace_printk_init_buffers(void) /* * trace_printk_init_buffers() can be called by modules. * If that happens, then we need to start cmdline recording - * directly here. If the global_trace.buffer is already - * allocated here, then this was called by module code. + * directly here. */ - if (global_trace.array_buffer.buffer) + if (system_state =3D=3D SYSTEM_RUNNING) tracing_start_cmdline_record(); } EXPORT_SYMBOL_GPL(trace_printk_init_buffers); --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 843DD33A9F8; Fri, 6 Feb 2026 19:58:57 +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=1770407937; cv=none; b=nzzRCrJ3f5F0CMTUFpozNmJy3zYTNKjxmfOdc2TF6dOgW+JoHGGuX2YYOHmwpGlot+qk/pBgAvt0CgRpxRJTtkW+ZrKVX200BB1sfdIXekqDsnVN4owV+lEvI88M+3CdTdKVysGehK1pQe5UMG+C/M9qNu94nOnlpEtg9nHdDIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407937; c=relaxed/simple; bh=dTLbo3RCDZzbjTHdEQUNHZ7jQ+T8xshgGjebdPECQ7U=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=TmhY4qAb6i1qurnkW/Ni1cWJCB6M9dpzCL5ydSqLvC2EHRoUJlRYiCe5a0eN+Avfm7CMgPGht1fCtTaVJ9SKdHy7EKvANjnWwJMYU9Ad6IR5FzxxqqmQR855lKYkI7gy7a7CokGDhBMGBx72KG28swT3Olmxice9bhiCKorRy00= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e9A+cEww; 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="e9A+cEww" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 435DEC116C6; Fri, 6 Feb 2026 19:58:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407937; bh=dTLbo3RCDZzbjTHdEQUNHZ7jQ+T8xshgGjebdPECQ7U=; h=Date:From:To:Cc:Subject:References:From; b=e9A+cEww6NZ5TWEIC/Mgf90PbqD3Wzb54vwE4ZUNUHL6FOy08s3+eC3uf0Ice1Zop T2Sd1enYWxl9dOBN+VV067wX0LDTSI3PXvpITSKVro872F/cP5/QTqJImX/HBjfJm9 h7I4ji8iH1nDJg0MAphFD0vJhDTmASS3BkCUjZ+J59Qai21g+fHUOl9wDwxeW3RTun RdBlDV+Nnf2pJaNHPaSpBcPPL795yVP+Ti5XI0xxx6K+TdwU+DbGG/M+GMEbEaARrO kuPxHX8MwBe/kV6vASKEeGW6AlUea0yShJKJQtItHRoQIshcR5RynxucbVST6V4ukD tWZqVV75kkvOg== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzM-0000000A6qE-1oHg; Fri, 06 Feb 2026 14:59:36 -0500 Message-ID: <20260206195936.285697009@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:50 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 09/12] tracing: Move trace_printk functions out of trace.c and into trace_printk.c References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The file trace.c has become a catchall for most things tracing. Start making it smaller by breaking out various aspects into their own files. Move the functions associated to the trace_printk operations out of trace.c= and into trace_printk.c. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 431 ------------------------------------ kernel/trace/trace.h | 1 + kernel/trace/trace_printk.c | 431 ++++++++++++++++++++++++++++++++++++ 3 files changed, 432 insertions(+), 431 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 3b7fa2699301..f62bbe7d093f 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -539,17 +539,6 @@ struct trace_array *printk_trace =3D &global_trace; /* List of trace_arrays interested in the top level trace_marker */ static LIST_HEAD(marker_copies); =20 -static __always_inline bool printk_binsafe(struct trace_array *tr) -{ - /* - * The binary format of traceprintk can cause a crash if used - * by a buffer from another boot. Force the use of the - * non binary version of trace_printk if the trace_printk - * buffer is a boot mapped ring buffer. - */ - return !(tr->flags & TRACE_ARRAY_FL_BOOT); -} - static void update_printk_trace(struct trace_array *tr) { if (printk_trace =3D=3D tr) @@ -1059,108 +1048,6 @@ void tracing_on(void) } EXPORT_SYMBOL_GPL(tracing_on); =20 -int __trace_array_puts(struct trace_array *tr, unsigned long ip, - const char *str, int size) -{ - struct ring_buffer_event *event; - struct trace_buffer *buffer; - struct print_entry *entry; - unsigned int trace_ctx; - int alloc; - - if (!(tr->trace_flags & TRACE_ITER(PRINTK))) - return 0; - - if (unlikely(tracing_selftest_running && - (tr->flags & TRACE_ARRAY_FL_GLOBAL))) - return 0; - - if (unlikely(tracing_disabled)) - return 0; - - alloc =3D sizeof(*entry) + size + 2; /* possible \n added */ - - trace_ctx =3D tracing_gen_ctx(); - buffer =3D tr->array_buffer.buffer; - guard(ring_buffer_nest)(buffer); - event =3D __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, - trace_ctx); - if (!event) - return 0; - - entry =3D ring_buffer_event_data(event); - entry->ip =3D ip; - - memcpy(&entry->buf, str, size); - - /* Add a newline if necessary */ - if (entry->buf[size - 1] !=3D '\n') { - entry->buf[size] =3D '\n'; - entry->buf[size + 1] =3D '\0'; - } else - entry->buf[size] =3D '\0'; - - __buffer_unlock_commit(buffer, event); - ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL); - return size; -} -EXPORT_SYMBOL_GPL(__trace_array_puts); - -/** - * __trace_puts - write a constant string into the trace buffer. - * @ip: The address of the caller - * @str: The constant string to write - * @size: The size of the string. - */ -int __trace_puts(unsigned long ip, const char *str, int size) -{ - return __trace_array_puts(printk_trace, ip, str, size); -} -EXPORT_SYMBOL_GPL(__trace_puts); - -/** - * __trace_bputs - write the pointer to a constant string into trace buffer - * @ip: The address of the caller - * @str: The constant string to write to the buffer to - */ -int __trace_bputs(unsigned long ip, const char *str) -{ - struct trace_array *tr =3D READ_ONCE(printk_trace); - struct ring_buffer_event *event; - struct trace_buffer *buffer; - struct bputs_entry *entry; - unsigned int trace_ctx; - int size =3D sizeof(struct bputs_entry); - - if (!printk_binsafe(tr)) - return __trace_puts(ip, str, strlen(str)); - - if (!(tr->trace_flags & TRACE_ITER(PRINTK))) - return 0; - - if (unlikely(tracing_selftest_running || tracing_disabled)) - return 0; - - trace_ctx =3D tracing_gen_ctx(); - buffer =3D tr->array_buffer.buffer; - - guard(ring_buffer_nest)(buffer); - event =3D __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size, - trace_ctx); - if (!event) - return 0; - - entry =3D ring_buffer_event_data(event); - entry->ip =3D ip; - entry->str =3D str; - - __buffer_unlock_commit(buffer, event); - ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL); - - return 1; -} -EXPORT_SYMBOL_GPL(__trace_bputs); - #ifdef CONFIG_TRACER_SNAPSHOT static void tracing_snapshot_instance_cond(struct trace_array *tr, void *cond_data) @@ -3161,324 +3048,6 @@ void trace_last_func_repeats(struct trace_array *tr, __buffer_unlock_commit(buffer, event); } =20 -/* created for use with alloc_percpu */ -struct trace_buffer_struct { - int nesting; - char buffer[4][TRACE_BUF_SIZE]; -}; - -static struct trace_buffer_struct __percpu *trace_percpu_buffer; - -/* - * This allows for lockless recording. If we're nested too deeply, then - * this returns NULL. - */ -static char *get_trace_buf(void) -{ - struct trace_buffer_struct *buffer =3D this_cpu_ptr(trace_percpu_buffer); - - if (!trace_percpu_buffer || buffer->nesting >=3D 4) - return NULL; - - buffer->nesting++; - - /* Interrupts must see nesting incremented before we use the buffer */ - barrier(); - return &buffer->buffer[buffer->nesting - 1][0]; -} - -static void put_trace_buf(void) -{ - /* Don't let the decrement of nesting leak before this */ - barrier(); - this_cpu_dec(trace_percpu_buffer->nesting); -} - -static int alloc_percpu_trace_buffer(void) -{ - struct trace_buffer_struct __percpu *buffers; - - if (trace_percpu_buffer) - return 0; - - buffers =3D alloc_percpu(struct trace_buffer_struct); - if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer")) - return -ENOMEM; - - trace_percpu_buffer =3D buffers; - return 0; -} - -static int buffers_allocated; - -void trace_printk_init_buffers(void) -{ - if (buffers_allocated) - return; - - if (alloc_percpu_trace_buffer()) - return; - - /* trace_printk() is for debug use only. Don't use it in production. */ - - pr_warn("\n"); - pr_warn("**********************************************************\n"); - pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); - pr_warn("** **\n"); - pr_warn("** trace_printk() being used. Allocating extra memory. **\n"); - pr_warn("** **\n"); - pr_warn("** This means that this is a DEBUG kernel and it is **\n"); - pr_warn("** unsafe for production use. **\n"); - pr_warn("** **\n"); - pr_warn("** If you see this message and you are not debugging **\n"); - pr_warn("** the kernel, report this immediately to your vendor! **\n"); - pr_warn("** **\n"); - pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); - pr_warn("**********************************************************\n"); - - /* Expand the buffers to set size */ - if (tracing_update_buffers(NULL) < 0) - pr_err("Failed to expand tracing buffers for trace_printk() calls\n"); - else - buffers_allocated =3D 1; - - /* - * trace_printk_init_buffers() can be called by modules. - * If that happens, then we need to start cmdline recording - * directly here. - */ - if (system_state =3D=3D SYSTEM_RUNNING) - tracing_start_cmdline_record(); -} -EXPORT_SYMBOL_GPL(trace_printk_init_buffers); - -void trace_printk_start_comm(void) -{ - /* Start tracing comms if trace printk is set */ - if (!buffers_allocated) - return; - tracing_start_cmdline_record(); -} - -static void trace_printk_start_stop_comm(int enabled) -{ - if (!buffers_allocated) - return; - - if (enabled) - tracing_start_cmdline_record(); - else - tracing_stop_cmdline_record(); -} - -/** - * trace_vbprintk - write binary msg to tracing buffer - * @ip: The address of the caller - * @fmt: The string format to write to the buffer - * @args: Arguments for @fmt - */ -int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) -{ - struct ring_buffer_event *event; - struct trace_buffer *buffer; - struct trace_array *tr =3D READ_ONCE(printk_trace); - struct bprint_entry *entry; - unsigned int trace_ctx; - char *tbuffer; - int len =3D 0, size; - - if (!printk_binsafe(tr)) - return trace_vprintk(ip, fmt, args); - - if (unlikely(tracing_selftest_running || tracing_disabled)) - return 0; - - /* Don't pollute graph traces with trace_vprintk internals */ - pause_graph_tracing(); - - trace_ctx =3D tracing_gen_ctx(); - guard(preempt_notrace)(); - - tbuffer =3D get_trace_buf(); - if (!tbuffer) { - len =3D 0; - goto out_nobuffer; - } - - len =3D vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args= ); - - if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0) - goto out_put; - - size =3D sizeof(*entry) + sizeof(u32) * len; - buffer =3D tr->array_buffer.buffer; - scoped_guard(ring_buffer_nest, buffer) { - event =3D __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, - trace_ctx); - if (!event) - goto out_put; - entry =3D ring_buffer_event_data(event); - entry->ip =3D ip; - entry->fmt =3D fmt; - - memcpy(entry->buf, tbuffer, sizeof(u32) * len); - __buffer_unlock_commit(buffer, event); - ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL); - } -out_put: - put_trace_buf(); - -out_nobuffer: - unpause_graph_tracing(); - - return len; -} -EXPORT_SYMBOL_GPL(trace_vbprintk); - -static __printf(3, 0) -int __trace_array_vprintk(struct trace_buffer *buffer, - unsigned long ip, const char *fmt, va_list args) -{ - struct ring_buffer_event *event; - int len =3D 0, size; - struct print_entry *entry; - unsigned int trace_ctx; - char *tbuffer; - - if (unlikely(tracing_disabled)) - return 0; - - /* Don't pollute graph traces with trace_vprintk internals */ - pause_graph_tracing(); - - trace_ctx =3D tracing_gen_ctx(); - guard(preempt_notrace)(); - - - tbuffer =3D get_trace_buf(); - if (!tbuffer) { - len =3D 0; - goto out_nobuffer; - } - - len =3D vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args); - - size =3D sizeof(*entry) + len + 1; - scoped_guard(ring_buffer_nest, buffer) { - event =3D __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, - trace_ctx); - if (!event) - goto out; - entry =3D ring_buffer_event_data(event); - entry->ip =3D ip; - - memcpy(&entry->buf, tbuffer, len + 1); - __buffer_unlock_commit(buffer, event); - ftrace_trace_stack(printk_trace, buffer, trace_ctx, 6, NULL); - } -out: - put_trace_buf(); - -out_nobuffer: - unpause_graph_tracing(); - - return len; -} - -int trace_array_vprintk(struct trace_array *tr, - unsigned long ip, const char *fmt, va_list args) -{ - if (tracing_selftest_running && (tr->flags & TRACE_ARRAY_FL_GLOBAL)) - return 0; - - return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args); -} - -/** - * trace_array_printk - Print a message to a specific instance - * @tr: The instance trace_array descriptor - * @ip: The instruction pointer that this is called from. - * @fmt: The format to print (printf format) - * - * If a subsystem sets up its own instance, they have the right to - * printk strings into their tracing instance buffer using this - * function. Note, this function will not write into the top level - * buffer (use trace_printk() for that), as writing into the top level - * buffer should only have events that can be individually disabled. - * trace_printk() is only used for debugging a kernel, and should not - * be ever incorporated in normal use. - * - * trace_array_printk() can be used, as it will not add noise to the - * top level tracing buffer. - * - * Note, trace_array_init_printk() must be called on @tr before this - * can be used. - */ -int trace_array_printk(struct trace_array *tr, - unsigned long ip, const char *fmt, ...) -{ - int ret; - va_list ap; - - if (!tr) - return -ENOENT; - - /* This is only allowed for created instances */ - if (tr->flags & TRACE_ARRAY_FL_GLOBAL) - return 0; - - if (!(tr->trace_flags & TRACE_ITER(PRINTK))) - return 0; - - va_start(ap, fmt); - ret =3D trace_array_vprintk(tr, ip, fmt, ap); - va_end(ap); - return ret; -} -EXPORT_SYMBOL_GPL(trace_array_printk); - -/** - * trace_array_init_printk - Initialize buffers for trace_array_printk() - * @tr: The trace array to initialize the buffers for - * - * As trace_array_printk() only writes into instances, they are OK to - * have in the kernel (unlike trace_printk()). This needs to be called - * before trace_array_printk() can be used on a trace_array. - */ -int trace_array_init_printk(struct trace_array *tr) -{ - if (!tr) - return -ENOENT; - - /* This is only allowed for created instances */ - if (tr->flags & TRACE_ARRAY_FL_GLOBAL) - return -EINVAL; - - return alloc_percpu_trace_buffer(); -} -EXPORT_SYMBOL_GPL(trace_array_init_printk); - -int trace_array_printk_buf(struct trace_buffer *buffer, - unsigned long ip, const char *fmt, ...) -{ - int ret; - va_list ap; - - if (!(printk_trace->trace_flags & TRACE_ITER(PRINTK))) - return 0; - - va_start(ap, fmt); - ret =3D __trace_array_vprintk(buffer, ip, fmt, ap); - va_end(ap); - return ret; -} - -int trace_vprintk(unsigned long ip, const char *fmt, va_list args) -{ - return trace_array_vprintk(printk_trace, ip, fmt, args); -} -EXPORT_SYMBOL_GPL(trace_vprintk); - static void trace_iterator_increment(struct trace_iterator *iter) { struct ring_buffer_iter *buf_iter =3D trace_buffer_iter(iter, iter->cpu); diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 73a2ab195114..f85c94f2e2d9 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -2135,6 +2135,7 @@ extern const char *__stop___tracepoint_str[]; =20 void trace_printk_control(bool enabled); void trace_printk_start_comm(void); +void trace_printk_start_stop_comm(int enabled); int trace_keep_overwrite(struct tracer *tracer, u64 mask, int set); int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled); =20 diff --git a/kernel/trace/trace_printk.c b/kernel/trace/trace_printk.c index 29f6e95439b6..c9cb74a33b3c 100644 --- a/kernel/trace/trace_printk.c +++ b/kernel/trace/trace_printk.c @@ -376,6 +376,437 @@ static const struct file_operations ftrace_formats_fo= ps =3D { .release =3D seq_release, }; =20 +static __always_inline bool printk_binsafe(struct trace_array *tr) +{ + /* + * The binary format of traceprintk can cause a crash if used + * by a buffer from another boot. Force the use of the + * non binary version of trace_printk if the trace_printk + * buffer is a boot mapped ring buffer. + */ + return !(tr->flags & TRACE_ARRAY_FL_BOOT); +} + +int __trace_array_puts(struct trace_array *tr, unsigned long ip, + const char *str, int size) +{ + struct ring_buffer_event *event; + struct trace_buffer *buffer; + struct print_entry *entry; + unsigned int trace_ctx; + int alloc; + + if (!(tr->trace_flags & TRACE_ITER(PRINTK))) + return 0; + + if (unlikely(tracing_selftest_running && + (tr->flags & TRACE_ARRAY_FL_GLOBAL))) + return 0; + + if (unlikely(tracing_disabled)) + return 0; + + alloc =3D sizeof(*entry) + size + 2; /* possible \n added */ + + trace_ctx =3D tracing_gen_ctx(); + buffer =3D tr->array_buffer.buffer; + guard(ring_buffer_nest)(buffer); + event =3D __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, + trace_ctx); + if (!event) + return 0; + + entry =3D ring_buffer_event_data(event); + entry->ip =3D ip; + + memcpy(&entry->buf, str, size); + + /* Add a newline if necessary */ + if (entry->buf[size - 1] !=3D '\n') { + entry->buf[size] =3D '\n'; + entry->buf[size + 1] =3D '\0'; + } else + entry->buf[size] =3D '\0'; + + __buffer_unlock_commit(buffer, event); + ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL); + return size; +} +EXPORT_SYMBOL_GPL(__trace_array_puts); + +/** + * __trace_puts - write a constant string into the trace buffer. + * @ip: The address of the caller + * @str: The constant string to write + * @size: The size of the string. + */ +int __trace_puts(unsigned long ip, const char *str, int size) +{ + return __trace_array_puts(printk_trace, ip, str, size); +} +EXPORT_SYMBOL_GPL(__trace_puts); + +/** + * __trace_bputs - write the pointer to a constant string into trace buffer + * @ip: The address of the caller + * @str: The constant string to write to the buffer to + */ +int __trace_bputs(unsigned long ip, const char *str) +{ + struct trace_array *tr =3D READ_ONCE(printk_trace); + struct ring_buffer_event *event; + struct trace_buffer *buffer; + struct bputs_entry *entry; + unsigned int trace_ctx; + int size =3D sizeof(struct bputs_entry); + + if (!printk_binsafe(tr)) + return __trace_puts(ip, str, strlen(str)); + + if (!(tr->trace_flags & TRACE_ITER(PRINTK))) + return 0; + + if (unlikely(tracing_selftest_running || tracing_disabled)) + return 0; + + trace_ctx =3D tracing_gen_ctx(); + buffer =3D tr->array_buffer.buffer; + + guard(ring_buffer_nest)(buffer); + event =3D __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size, + trace_ctx); + if (!event) + return 0; + + entry =3D ring_buffer_event_data(event); + entry->ip =3D ip; + entry->str =3D str; + + __buffer_unlock_commit(buffer, event); + ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL); + + return 1; +} +EXPORT_SYMBOL_GPL(__trace_bputs); + +/* created for use with alloc_percpu */ +struct trace_buffer_struct { + int nesting; + char buffer[4][TRACE_BUF_SIZE]; +}; + +static struct trace_buffer_struct __percpu *trace_percpu_buffer; + +/* + * This allows for lockless recording. If we're nested too deeply, then + * this returns NULL. + */ +static char *get_trace_buf(void) +{ + struct trace_buffer_struct *buffer =3D this_cpu_ptr(trace_percpu_buffer); + + if (!trace_percpu_buffer || buffer->nesting >=3D 4) + return NULL; + + buffer->nesting++; + + /* Interrupts must see nesting incremented before we use the buffer */ + barrier(); + return &buffer->buffer[buffer->nesting - 1][0]; +} + +static void put_trace_buf(void) +{ + /* Don't let the decrement of nesting leak before this */ + barrier(); + this_cpu_dec(trace_percpu_buffer->nesting); +} + +static int alloc_percpu_trace_buffer(void) +{ + struct trace_buffer_struct __percpu *buffers; + + if (trace_percpu_buffer) + return 0; + + buffers =3D alloc_percpu(struct trace_buffer_struct); + if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer")) + return -ENOMEM; + + trace_percpu_buffer =3D buffers; + return 0; +} + +static int buffers_allocated; + +void trace_printk_init_buffers(void) +{ + if (buffers_allocated) + return; + + if (alloc_percpu_trace_buffer()) + return; + + /* trace_printk() is for debug use only. Don't use it in production. */ + + pr_warn("\n"); + pr_warn("**********************************************************\n"); + pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); + pr_warn("** **\n"); + pr_warn("** trace_printk() being used. Allocating extra memory. **\n"); + pr_warn("** **\n"); + pr_warn("** This means that this is a DEBUG kernel and it is **\n"); + pr_warn("** unsafe for production use. **\n"); + pr_warn("** **\n"); + pr_warn("** If you see this message and you are not debugging **\n"); + pr_warn("** the kernel, report this immediately to your vendor! **\n"); + pr_warn("** **\n"); + pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); + pr_warn("**********************************************************\n"); + + /* Expand the buffers to set size */ + if (tracing_update_buffers(NULL) < 0) + pr_err("Failed to expand tracing buffers for trace_printk() calls\n"); + else + buffers_allocated =3D 1; + + /* + * trace_printk_init_buffers() can be called by modules. + * If that happens, then we need to start cmdline recording + * directly here. + */ + if (system_state =3D=3D SYSTEM_RUNNING) + tracing_start_cmdline_record(); +} +EXPORT_SYMBOL_GPL(trace_printk_init_buffers); + +void trace_printk_start_comm(void) +{ + /* Start tracing comms if trace printk is set */ + if (!buffers_allocated) + return; + tracing_start_cmdline_record(); +} + +void trace_printk_start_stop_comm(int enabled) +{ + if (!buffers_allocated) + return; + + if (enabled) + tracing_start_cmdline_record(); + else + tracing_stop_cmdline_record(); +} + +/** + * trace_vbprintk - write binary msg to tracing buffer + * @ip: The address of the caller + * @fmt: The string format to write to the buffer + * @args: Arguments for @fmt + */ +int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) +{ + struct ring_buffer_event *event; + struct trace_buffer *buffer; + struct trace_array *tr =3D READ_ONCE(printk_trace); + struct bprint_entry *entry; + unsigned int trace_ctx; + char *tbuffer; + int len =3D 0, size; + + if (!printk_binsafe(tr)) + return trace_vprintk(ip, fmt, args); + + if (unlikely(tracing_selftest_running || tracing_disabled)) + return 0; + + /* Don't pollute graph traces with trace_vprintk internals */ + pause_graph_tracing(); + + trace_ctx =3D tracing_gen_ctx(); + guard(preempt_notrace)(); + + tbuffer =3D get_trace_buf(); + if (!tbuffer) { + len =3D 0; + goto out_nobuffer; + } + + len =3D vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args= ); + + if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0) + goto out_put; + + size =3D sizeof(*entry) + sizeof(u32) * len; + buffer =3D tr->array_buffer.buffer; + scoped_guard(ring_buffer_nest, buffer) { + event =3D __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, + trace_ctx); + if (!event) + goto out_put; + entry =3D ring_buffer_event_data(event); + entry->ip =3D ip; + entry->fmt =3D fmt; + + memcpy(entry->buf, tbuffer, sizeof(u32) * len); + __buffer_unlock_commit(buffer, event); + ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL); + } +out_put: + put_trace_buf(); + +out_nobuffer: + unpause_graph_tracing(); + + return len; +} +EXPORT_SYMBOL_GPL(trace_vbprintk); + +static __printf(3, 0) +int __trace_array_vprintk(struct trace_buffer *buffer, + unsigned long ip, const char *fmt, va_list args) +{ + struct ring_buffer_event *event; + int len =3D 0, size; + struct print_entry *entry; + unsigned int trace_ctx; + char *tbuffer; + + if (unlikely(tracing_disabled)) + return 0; + + /* Don't pollute graph traces with trace_vprintk internals */ + pause_graph_tracing(); + + trace_ctx =3D tracing_gen_ctx(); + guard(preempt_notrace)(); + + + tbuffer =3D get_trace_buf(); + if (!tbuffer) { + len =3D 0; + goto out_nobuffer; + } + + len =3D vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args); + + size =3D sizeof(*entry) + len + 1; + scoped_guard(ring_buffer_nest, buffer) { + event =3D __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, + trace_ctx); + if (!event) + goto out; + entry =3D ring_buffer_event_data(event); + entry->ip =3D ip; + + memcpy(&entry->buf, tbuffer, len + 1); + __buffer_unlock_commit(buffer, event); + ftrace_trace_stack(printk_trace, buffer, trace_ctx, 6, NULL); + } +out: + put_trace_buf(); + +out_nobuffer: + unpause_graph_tracing(); + + return len; +} + +int trace_array_vprintk(struct trace_array *tr, + unsigned long ip, const char *fmt, va_list args) +{ + if (tracing_selftest_running && (tr->flags & TRACE_ARRAY_FL_GLOBAL)) + return 0; + + return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args); +} + +/** + * trace_array_printk - Print a message to a specific instance + * @tr: The instance trace_array descriptor + * @ip: The instruction pointer that this is called from. + * @fmt: The format to print (printf format) + * + * If a subsystem sets up its own instance, they have the right to + * printk strings into their tracing instance buffer using this + * function. Note, this function will not write into the top level + * buffer (use trace_printk() for that), as writing into the top level + * buffer should only have events that can be individually disabled. + * trace_printk() is only used for debugging a kernel, and should not + * be ever incorporated in normal use. + * + * trace_array_printk() can be used, as it will not add noise to the + * top level tracing buffer. + * + * Note, trace_array_init_printk() must be called on @tr before this + * can be used. + */ +int trace_array_printk(struct trace_array *tr, + unsigned long ip, const char *fmt, ...) +{ + int ret; + va_list ap; + + if (!tr) + return -ENOENT; + + /* This is only allowed for created instances */ + if (tr->flags & TRACE_ARRAY_FL_GLOBAL) + return 0; + + if (!(tr->trace_flags & TRACE_ITER(PRINTK))) + return 0; + + va_start(ap, fmt); + ret =3D trace_array_vprintk(tr, ip, fmt, ap); + va_end(ap); + return ret; +} +EXPORT_SYMBOL_GPL(trace_array_printk); + +/** + * trace_array_init_printk - Initialize buffers for trace_array_printk() + * @tr: The trace array to initialize the buffers for + * + * As trace_array_printk() only writes into instances, they are OK to + * have in the kernel (unlike trace_printk()). This needs to be called + * before trace_array_printk() can be used on a trace_array. + */ +int trace_array_init_printk(struct trace_array *tr) +{ + if (!tr) + return -ENOENT; + + /* This is only allowed for created instances */ + if (tr->flags & TRACE_ARRAY_FL_GLOBAL) + return -EINVAL; + + return alloc_percpu_trace_buffer(); +} +EXPORT_SYMBOL_GPL(trace_array_init_printk); + +int trace_array_printk_buf(struct trace_buffer *buffer, + unsigned long ip, const char *fmt, ...) +{ + int ret; + va_list ap; + + if (!(printk_trace->trace_flags & TRACE_ITER(PRINTK))) + return 0; + + va_start(ap, fmt); + ret =3D __trace_array_vprintk(buffer, ip, fmt, ap); + va_end(ap); + return ret; +} + +int trace_vprintk(unsigned long ip, const char *fmt, va_list args) +{ + return trace_array_vprintk(printk_trace, ip, fmt, args); +} +EXPORT_SYMBOL_GPL(trace_vprintk); + static __init int init_trace_printk_function_export(void) { int ret; --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 B88BD33ADBC; Fri, 6 Feb 2026 19:58:57 +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=1770407937; cv=none; b=sd4PoFgSsC/M7aC727Bu4GMH4cFZD+0vap4GDcjb5zl/bduchKZO82dVVMI3Sosto/gB2pLXculzKal2AOan6z4bDlzjY2K6NzxoecP5FnyAHyysHhX9otnpgLWf221mvS0d1E7xj+rQKEzovXMgFav/IPI9UbsjsqVdbtUwXlQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407937; c=relaxed/simple; bh=hSgbbJT6hyzYJ+D57J2CLYyG0AzYk0jawhezmkHSop4=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=MZ0KlOYizrtXvPD13F21f1N6gxYZ57/IpUpHEAIKdgmfyhTpLVBM6ftmsW3tOoUllv/rY2IZANWYEirjP3Ey/mPBlqgkPMXynjam4DsgJSy/kiXQlTKt1JhuqxQu57wXf0oS0rB8j7iGK6DLszGkdlvaZMo37JZF3NhJuMsqdNQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=g9ZRwR6+; 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="g9ZRwR6+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D062C19423; Fri, 6 Feb 2026 19:58:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407937; bh=hSgbbJT6hyzYJ+D57J2CLYyG0AzYk0jawhezmkHSop4=; h=Date:From:To:Cc:Subject:References:From; b=g9ZRwR6+8Yky3Y3ikYJoOtmOUCExH1sxGNW3tgjSR3E4HOTThfOgn3kBXFU3zw8XM mx4AGjtDLCAZHrLf49eNDSada3RgChV+rzW4LDH35y0RVlGUAM89DlD6EUso7Nj2uz mTCer/5KhX3sINYqyQyQBZlxll6xvxwMR8mFjh9MBKkxToRE4u0b1NN+SrHTmA1aX8 5MIBNp757llf+VBjpMEYQfOMJGiWkBg44JW3HSGUxJYDbyvVGNb1EShpNS4GAEgWz4 OTTpdUeitcFN5y43AFoP/+h1oFbgEMm3j25PvqETrH9CVSUVHMLqxt2wS8oKWclwBR l6/g987PWIaug== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzM-0000000A6qj-2VAh; Fri, 06 Feb 2026 14:59:36 -0500 Message-ID: <20260206195936.452815257@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:51 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 10/12] tracing: Move pid filtering into trace_pid.c References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The trace.c file was a dumping ground for most tracing code. Start organizing it better by moving various functions out into their own files. Move the PID filtering functions from trace.c into its own trace_pid.c file. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/Makefile | 1 + kernel/trace/trace.c | 242 -------------------------------------- kernel/trace/trace_pid.c | 246 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 247 insertions(+), 242 deletions(-) create mode 100644 kernel/trace/trace_pid.c diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile index fc5dcc888e13..04096c21d06b 100644 --- a/kernel/trace/Makefile +++ b/kernel/trace/Makefile @@ -68,6 +68,7 @@ obj-$(CONFIG_TRACING) +=3D trace_output.o obj-$(CONFIG_TRACING) +=3D trace_seq.o obj-$(CONFIG_TRACING) +=3D trace_stat.o obj-$(CONFIG_TRACING) +=3D trace_printk.o +obj-$(CONFIG_TRACING) +=3D trace_pid.o obj-$(CONFIG_TRACING) +=3D pid_list.o obj-$(CONFIG_TRACING_MAP) +=3D tracing_map.o obj-$(CONFIG_PREEMPTIRQ_DELAY_TEST) +=3D preemptirq_delay_test.o diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index f62bbe7d093f..6a0cea7e63b0 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -637,248 +637,6 @@ int tracing_check_open_get_tr(struct trace_array *tr) return 0; } =20 -/** - * trace_find_filtered_pid - check if a pid exists in a filtered_pid list - * @filtered_pids: The list of pids to check - * @search_pid: The PID to find in @filtered_pids - * - * Returns true if @search_pid is found in @filtered_pids, and false other= wise. - */ -bool -trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search= _pid) -{ - return trace_pid_list_is_set(filtered_pids, search_pid); -} - -/** - * trace_ignore_this_task - should a task be ignored for tracing - * @filtered_pids: The list of pids to check - * @filtered_no_pids: The list of pids not to be traced - * @task: The task that should be ignored if not filtered - * - * Checks if @task should be traced or not from @filtered_pids. - * Returns true if @task should *NOT* be traced. - * Returns false if @task should be traced. - */ -bool -trace_ignore_this_task(struct trace_pid_list *filtered_pids, - struct trace_pid_list *filtered_no_pids, - struct task_struct *task) -{ - /* - * If filtered_no_pids is not empty, and the task's pid is listed - * in filtered_no_pids, then return true. - * Otherwise, if filtered_pids is empty, that means we can - * trace all tasks. If it has content, then only trace pids - * within filtered_pids. - */ - - return (filtered_pids && - !trace_find_filtered_pid(filtered_pids, task->pid)) || - (filtered_no_pids && - trace_find_filtered_pid(filtered_no_pids, task->pid)); -} - -/** - * trace_filter_add_remove_task - Add or remove a task from a pid_list - * @pid_list: The list to modify - * @self: The current task for fork or NULL for exit - * @task: The task to add or remove - * - * If adding a task, if @self is defined, the task is only added if @self - * is also included in @pid_list. This happens on fork and tasks should - * only be added when the parent is listed. If @self is NULL, then the - * @task pid will be removed from the list, which would happen on exit - * of a task. - */ -void trace_filter_add_remove_task(struct trace_pid_list *pid_list, - struct task_struct *self, - struct task_struct *task) -{ - if (!pid_list) - return; - - /* For forks, we only add if the forking task is listed */ - if (self) { - if (!trace_find_filtered_pid(pid_list, self->pid)) - return; - } - - /* "self" is set for forks, and NULL for exits */ - if (self) - trace_pid_list_set(pid_list, task->pid); - else - trace_pid_list_clear(pid_list, task->pid); -} - -/** - * trace_pid_next - Used for seq_file to get to the next pid of a pid_list - * @pid_list: The pid list to show - * @v: The last pid that was shown (+1 the actual pid to let zero be displ= ayed) - * @pos: The position of the file - * - * This is used by the seq_file "next" operation to iterate the pids - * listed in a trace_pid_list structure. - * - * Returns the pid+1 as we want to display pid of zero, but NULL would - * stop the iteration. - */ -void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos) -{ - long pid =3D (unsigned long)v; - unsigned int next; - - (*pos)++; - - /* pid already is +1 of the actual previous bit */ - if (trace_pid_list_next(pid_list, pid, &next) < 0) - return NULL; - - pid =3D next; - - /* Return pid + 1 to allow zero to be represented */ - return (void *)(pid + 1); -} - -/** - * trace_pid_start - Used for seq_file to start reading pid lists - * @pid_list: The pid list to show - * @pos: The position of the file - * - * This is used by seq_file "start" operation to start the iteration - * of listing pids. - * - * Returns the pid+1 as we want to display pid of zero, but NULL would - * stop the iteration. - */ -void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos) -{ - unsigned long pid; - unsigned int first; - loff_t l =3D 0; - - if (trace_pid_list_first(pid_list, &first) < 0) - return NULL; - - pid =3D first; - - /* Return pid + 1 so that zero can be the exit value */ - for (pid++; pid && l < *pos; - pid =3D (unsigned long)trace_pid_next(pid_list, (void *)pid, &l)) - ; - return (void *)pid; -} - -/** - * trace_pid_show - show the current pid in seq_file processing - * @m: The seq_file structure to write into - * @v: A void pointer of the pid (+1) value to display - * - * Can be directly used by seq_file operations to display the current - * pid value. - */ -int trace_pid_show(struct seq_file *m, void *v) -{ - unsigned long pid =3D (unsigned long)v - 1; - - seq_printf(m, "%lu\n", pid); - return 0; -} - -/* 128 should be much more than enough */ -#define PID_BUF_SIZE 127 - -int trace_pid_write(struct trace_pid_list *filtered_pids, - struct trace_pid_list **new_pid_list, - const char __user *ubuf, size_t cnt) -{ - struct trace_pid_list *pid_list; - struct trace_parser parser; - unsigned long val; - int nr_pids =3D 0; - ssize_t read =3D 0; - ssize_t ret; - loff_t pos; - pid_t pid; - - if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1)) - return -ENOMEM; - - /* - * Always recreate a new array. The write is an all or nothing - * operation. Always create a new array when adding new pids by - * the user. If the operation fails, then the current list is - * not modified. - */ - pid_list =3D trace_pid_list_alloc(); - if (!pid_list) { - trace_parser_put(&parser); - return -ENOMEM; - } - - if (filtered_pids) { - /* copy the current bits to the new max */ - ret =3D trace_pid_list_first(filtered_pids, &pid); - while (!ret) { - ret =3D trace_pid_list_set(pid_list, pid); - if (ret < 0) - goto out; - - ret =3D trace_pid_list_next(filtered_pids, pid + 1, &pid); - nr_pids++; - } - } - - ret =3D 0; - while (cnt > 0) { - - pos =3D 0; - - ret =3D trace_get_user(&parser, ubuf, cnt, &pos); - if (ret < 0) - break; - - read +=3D ret; - ubuf +=3D ret; - cnt -=3D ret; - - if (!trace_parser_loaded(&parser)) - break; - - ret =3D -EINVAL; - if (kstrtoul(parser.buffer, 0, &val)) - break; - - pid =3D (pid_t)val; - - if (trace_pid_list_set(pid_list, pid) < 0) { - ret =3D -1; - break; - } - nr_pids++; - - trace_parser_clear(&parser); - ret =3D 0; - } - out: - trace_parser_put(&parser); - - if (ret < 0) { - trace_pid_list_free(pid_list); - return ret; - } - - if (!nr_pids) { - /* Cleared the list of pids */ - trace_pid_list_free(pid_list); - pid_list =3D NULL; - } - - *new_pid_list =3D pid_list; - - return read; -} - static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu) { u64 ts; diff --git a/kernel/trace/trace_pid.c b/kernel/trace/trace_pid.c new file mode 100644 index 000000000000..7127c8de4174 --- /dev/null +++ b/kernel/trace/trace_pid.c @@ -0,0 +1,246 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "trace.h" + +/** + * trace_find_filtered_pid - check if a pid exists in a filtered_pid list + * @filtered_pids: The list of pids to check + * @search_pid: The PID to find in @filtered_pids + * + * Returns true if @search_pid is found in @filtered_pids, and false other= wise. + */ +bool +trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search= _pid) +{ + return trace_pid_list_is_set(filtered_pids, search_pid); +} + +/** + * trace_ignore_this_task - should a task be ignored for tracing + * @filtered_pids: The list of pids to check + * @filtered_no_pids: The list of pids not to be traced + * @task: The task that should be ignored if not filtered + * + * Checks if @task should be traced or not from @filtered_pids. + * Returns true if @task should *NOT* be traced. + * Returns false if @task should be traced. + */ +bool +trace_ignore_this_task(struct trace_pid_list *filtered_pids, + struct trace_pid_list *filtered_no_pids, + struct task_struct *task) +{ + /* + * If filtered_no_pids is not empty, and the task's pid is listed + * in filtered_no_pids, then return true. + * Otherwise, if filtered_pids is empty, that means we can + * trace all tasks. If it has content, then only trace pids + * within filtered_pids. + */ + + return (filtered_pids && + !trace_find_filtered_pid(filtered_pids, task->pid)) || + (filtered_no_pids && + trace_find_filtered_pid(filtered_no_pids, task->pid)); +} + +/** + * trace_filter_add_remove_task - Add or remove a task from a pid_list + * @pid_list: The list to modify + * @self: The current task for fork or NULL for exit + * @task: The task to add or remove + * + * If adding a task, if @self is defined, the task is only added if @self + * is also included in @pid_list. This happens on fork and tasks should + * only be added when the parent is listed. If @self is NULL, then the + * @task pid will be removed from the list, which would happen on exit + * of a task. + */ +void trace_filter_add_remove_task(struct trace_pid_list *pid_list, + struct task_struct *self, + struct task_struct *task) +{ + if (!pid_list) + return; + + /* For forks, we only add if the forking task is listed */ + if (self) { + if (!trace_find_filtered_pid(pid_list, self->pid)) + return; + } + + /* "self" is set for forks, and NULL for exits */ + if (self) + trace_pid_list_set(pid_list, task->pid); + else + trace_pid_list_clear(pid_list, task->pid); +} + +/** + * trace_pid_next - Used for seq_file to get to the next pid of a pid_list + * @pid_list: The pid list to show + * @v: The last pid that was shown (+1 the actual pid to let zero be displ= ayed) + * @pos: The position of the file + * + * This is used by the seq_file "next" operation to iterate the pids + * listed in a trace_pid_list structure. + * + * Returns the pid+1 as we want to display pid of zero, but NULL would + * stop the iteration. + */ +void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos) +{ + long pid =3D (unsigned long)v; + unsigned int next; + + (*pos)++; + + /* pid already is +1 of the actual previous bit */ + if (trace_pid_list_next(pid_list, pid, &next) < 0) + return NULL; + + pid =3D next; + + /* Return pid + 1 to allow zero to be represented */ + return (void *)(pid + 1); +} + +/** + * trace_pid_start - Used for seq_file to start reading pid lists + * @pid_list: The pid list to show + * @pos: The position of the file + * + * This is used by seq_file "start" operation to start the iteration + * of listing pids. + * + * Returns the pid+1 as we want to display pid of zero, but NULL would + * stop the iteration. + */ +void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos) +{ + unsigned long pid; + unsigned int first; + loff_t l =3D 0; + + if (trace_pid_list_first(pid_list, &first) < 0) + return NULL; + + pid =3D first; + + /* Return pid + 1 so that zero can be the exit value */ + for (pid++; pid && l < *pos; + pid =3D (unsigned long)trace_pid_next(pid_list, (void *)pid, &l)) + ; + return (void *)pid; +} + +/** + * trace_pid_show - show the current pid in seq_file processing + * @m: The seq_file structure to write into + * @v: A void pointer of the pid (+1) value to display + * + * Can be directly used by seq_file operations to display the current + * pid value. + */ +int trace_pid_show(struct seq_file *m, void *v) +{ + unsigned long pid =3D (unsigned long)v - 1; + + seq_printf(m, "%lu\n", pid); + return 0; +} + +/* 128 should be much more than enough */ +#define PID_BUF_SIZE 127 + +int trace_pid_write(struct trace_pid_list *filtered_pids, + struct trace_pid_list **new_pid_list, + const char __user *ubuf, size_t cnt) +{ + struct trace_pid_list *pid_list; + struct trace_parser parser; + unsigned long val; + int nr_pids =3D 0; + ssize_t read =3D 0; + ssize_t ret; + loff_t pos; + pid_t pid; + + if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1)) + return -ENOMEM; + + /* + * Always recreate a new array. The write is an all or nothing + * operation. Always create a new array when adding new pids by + * the user. If the operation fails, then the current list is + * not modified. + */ + pid_list =3D trace_pid_list_alloc(); + if (!pid_list) { + trace_parser_put(&parser); + return -ENOMEM; + } + + if (filtered_pids) { + /* copy the current bits to the new max */ + ret =3D trace_pid_list_first(filtered_pids, &pid); + while (!ret) { + ret =3D trace_pid_list_set(pid_list, pid); + if (ret < 0) + goto out; + + ret =3D trace_pid_list_next(filtered_pids, pid + 1, &pid); + nr_pids++; + } + } + + ret =3D 0; + while (cnt > 0) { + + pos =3D 0; + + ret =3D trace_get_user(&parser, ubuf, cnt, &pos); + if (ret < 0) + break; + + read +=3D ret; + ubuf +=3D ret; + cnt -=3D ret; + + if (!trace_parser_loaded(&parser)) + break; + + ret =3D -EINVAL; + if (kstrtoul(parser.buffer, 0, &val)) + break; + + pid =3D (pid_t)val; + + if (trace_pid_list_set(pid_list, pid) < 0) { + ret =3D -1; + break; + } + nr_pids++; + + trace_parser_clear(&parser); + ret =3D 0; + } + out: + trace_parser_put(&parser); + + if (ret < 0) { + trace_pid_list_free(pid_list); + return ret; + } + + if (!nr_pids) { + /* Cleared the list of pids */ + trace_pid_list_free(pid_list); + pid_list =3D NULL; + } + + *new_pid_list =3D pid_list; + + return read; +} + --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 B6EC333ADB4; Fri, 6 Feb 2026 19:58:57 +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=1770407937; cv=none; b=FwAzrXfRKog/S14gHfDYxrffKoNJn8RGXO2O08Yvn7LCkw0JiK1IEFqXrm35hpdpUt+PXQzjgF3cnbiNGnhwdR8B7OLSbJCNX5YS7fTmSqpXKaLb6M4BqhZrLquJ/CPbjSE5ekpQyLrSYdyHOx0/+2rL7zHp/fb8e5FXm3V0sks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407937; c=relaxed/simple; bh=ZUcEKZAtlFAIbpjaZX6mY8OMNPSAtHWZIBIZL+GAaZc=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=ClsIy6wr85yysydqz/GzgpsjWT+XGNwqBsYGrxPA2WLsjiU3a+M5+TqZK+NWTMgTWzFv9SARjQl1CMEU5womNCNMDbWIzrZCPzAsCTsq+2VN8vrXMpolQlJAmr/+pI97mwYKPs2XMF1JoZqArK09Mhfkfp4NAytFZVebR7RiV74= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MYQHsmu7; 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="MYQHsmu7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98377C19422; Fri, 6 Feb 2026 19:58:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407937; bh=ZUcEKZAtlFAIbpjaZX6mY8OMNPSAtHWZIBIZL+GAaZc=; h=Date:From:To:Cc:Subject:References:From; b=MYQHsmu7EaLuBPy6H8cnXjX8FepyGTq+eKCdkafHDW1DH9CBZ5k/PNabcFsbENRuw osDxYfsyHH32NjrLGKDVHx8UjtylPtLCIIha5VJWfkJnXxtJPbOfhId3XqEx8NwxDc OgeF0gsxAsISTRFx3jZm8aanKn9OoMFi64IAR5rbseQ1y/uTzXhnMv9u+e84NFokXH 4sfoO9VstRYzQ8RyIFxFgK+DuCb6WnxrsxSM8pWUuGWW6whJ100Qo+gnYAE0ojwXfb 0sgcaG3Kz+JfFczj2hPL8cfOmsn86N1eFz2kyk84MSxUkKx5giA7S/FYx06YBq07fA PjgMSW1MLJ2/w== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzM-0000000A6rE-3Bum; Fri, 06 Feb 2026 14:59:36 -0500 Message-ID: <20260206195936.617080218@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:52 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 11/12] tracing: Move tracing_set_filter_buffering() into trace_events_hist.c References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The function tracing_set_filter_buffering() is only used in trace_events_hist.c. Move it to that file and make it static. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 20 -------------------- kernel/trace/trace.h | 1 - kernel/trace/trace_events_hist.c | 20 ++++++++++++++++++++ 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 6a0cea7e63b0..5bc22d966834 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -7134,26 +7134,6 @@ u64 tracing_event_time_stamp(struct trace_buffer *bu= ffer, struct ring_buffer_eve return ring_buffer_event_time_stamp(buffer, rbe); } =20 -/* - * Set or disable using the per CPU trace_buffer_event when possible. - */ -int tracing_set_filter_buffering(struct trace_array *tr, bool set) -{ - guard(mutex)(&trace_types_lock); - - if (set && tr->no_filter_buffering_ref++) - return 0; - - if (!set) { - if (WARN_ON_ONCE(!tr->no_filter_buffering_ref)) - return -EINVAL; - - --tr->no_filter_buffering_ref; - } - - return 0; -} - struct ftrace_buffer_info { struct trace_iterator iter; void *spare; diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index f85c94f2e2d9..374b7c392f0a 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -480,7 +480,6 @@ extern struct trace_array *trace_array_find(const char = *instance); extern struct trace_array *trace_array_find_get(const char *instance); =20 extern u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ri= ng_buffer_event *rbe); -extern int tracing_set_filter_buffering(struct trace_array *tr, bool set); extern int tracing_set_clock(struct trace_array *tr, const char *clockstr); =20 extern bool trace_clock_in_ns(struct trace_array *tr); diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_h= ist.c index 0fc641461be5..e6f449f53afc 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -6531,6 +6531,26 @@ static bool existing_hist_update_only(char *glob, return updated; } =20 +/* + * Set or disable using the per CPU trace_buffer_event when possible. + */ +static int tracing_set_filter_buffering(struct trace_array *tr, bool set) +{ + guard(mutex)(&trace_types_lock); + + if (set && tr->no_filter_buffering_ref++) + return 0; + + if (!set) { + if (WARN_ON_ONCE(!tr->no_filter_buffering_ref)) + return -EINVAL; + + --tr->no_filter_buffering_ref; + } + + return 0; +} + static int hist_register_trigger(char *glob, struct event_trigger_data *data, struct trace_event_file *file) --=20 2.51.0 From nobody Sat Feb 7 22:07:16 2026 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 2543833B975; Fri, 6 Feb 2026 19:58:58 +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=1770407938; cv=none; b=SjaMdS7PuDLcDpJnh5pmhWaU0S/msyLFQqjOVrI+AACFhVdCTPc4CVB++ycv8fbXsvOhu/8rLHyFF9MXm0pukeGo4paXY4Y+3dcr+gGUxCUsKlpTTgwd+exVefcKWCmC0ZIw9XUeX8tR4aCJn67sWKxyIbJOUoj+QzkeNxTztpc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770407938; c=relaxed/simple; bh=75BARh5bpqgqR88rVL22CBs1IJMcdong8pMSYlVHGjY=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=nSgGyYJNSJfFMwpmsFdaco7L2A0fYrgz1Gs332S6qh7Sig/YE92iNxESZUDWWIGv0jYXJABE/j47tm//yyVBJP92E643wp+JAG2z4Hf4sDnm9o3DK36mWJE0m8H65ZgXN79IcB89A/hbx/Con9rljwmFD6GK1wA13QpI6yUgsR4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SQ+qcSbD; 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="SQ+qcSbD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BFB7DC19424; Fri, 6 Feb 2026 19:58:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770407938; bh=75BARh5bpqgqR88rVL22CBs1IJMcdong8pMSYlVHGjY=; h=Date:From:To:Cc:Subject:References:From; b=SQ+qcSbDlO7wZlYErpbBylaYvsOw/2a4hp/Yb6BlJi8rN95XD/lq/ybzAsoh/x2bh cqA7NPCQ7qItEfeazFcsglo5DsKsPwim4nanTWKhl3mRzbwRo7NcdpB6okU5cioRNJ ViFP3EcTyU7hOO+XlfodXYBRTmtuP5qSSLO/1hXQsrtdEqIQzAi+bNDqNgnYXloDDx 4lA9LOXXUd6q3fH5S/nsEWGHco/OFhHRFob6lSKDU/NplnlqyqnJTowiZS1VaMTk8q J9Ni9ZHMZSL7rQSNwOsvn6pgGGFPSqpf9RquypYuRY0Nqfp4v9x/ajNghPg28Oz8lr rAHxscwzfbLyA== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1voRzM-0000000A6rk-3ytK; Fri, 06 Feb 2026 14:59:36 -0500 Message-ID: <20260206195936.803146337@kernel.org> User-Agent: quilt/0.68 Date: Fri, 06 Feb 2026 14:37:53 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton Subject: [PATCH 12/12] tracing: Move snapshot code out of trace.c and into trace_snapshot.c References: <20260206193741.767171392@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The trace.c file was a dumping ground for most tracing code. Start organizing it better by moving various functions out into their own files. Move all the snapshot code, including the max trace code into its own trace_snapshot.c file. Signed-off-by: Steven Rostedt (Google) --- include/linux/ftrace.h | 2 +- kernel/trace/Makefile | 1 + kernel/trace/trace.c | 1273 ++------------------------------- kernel/trace/trace.h | 107 ++- kernel/trace/trace_snapshot.c | 1068 +++++++++++++++++++++++++++ 5 files changed, 1233 insertions(+), 1218 deletions(-) create mode 100644 kernel/trace/trace_snapshot.c diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index a3a8989e3268..9fa999110b5e 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -31,7 +31,7 @@ #define ARCH_SUPPORTS_FTRACE_OPS 0 #endif =20 -#ifdef CONFIG_TRACING +#ifdef CONFIG_TRACER_SNAPSHOT extern void ftrace_boot_snapshot(void); #else static inline void ftrace_boot_snapshot(void) { } diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile index 04096c21d06b..83aeb5c77008 100644 --- a/kernel/trace/Makefile +++ b/kernel/trace/Makefile @@ -69,6 +69,7 @@ obj-$(CONFIG_TRACING) +=3D trace_seq.o obj-$(CONFIG_TRACING) +=3D trace_stat.o obj-$(CONFIG_TRACING) +=3D trace_printk.o obj-$(CONFIG_TRACING) +=3D trace_pid.o +obj-$(CONFIG_TRACER_SNAPSHOT) +=3D trace_snapshot.o obj-$(CONFIG_TRACING) +=3D pid_list.o obj-$(CONFIG_TRACING_MAP) +=3D tracing_map.o obj-$(CONFIG_PREEMPTIRQ_DELAY_TEST) +=3D preemptirq_delay_test.o diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 5bc22d966834..534ab502e5d7 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -47,7 +47,6 @@ #include #include #include -#include #include #include #include @@ -219,15 +218,9 @@ static void ftrace_trace_userstack(struct trace_array = *tr, static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata; static char *default_bootup_tracer; =20 -static bool allocate_snapshot; -static bool snapshot_at_boot; - static char boot_instance_info[COMMAND_LINE_SIZE] __initdata; static int boot_instance_index; =20 -static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata; -static int boot_snapshot_index; - static int __init set_cmdline_ftrace(char *str) { strscpy(bootup_tracer_buf, str, MAX_TRACER_SIZE); @@ -276,38 +269,6 @@ static int __init stop_trace_on_warning(char *str) } __setup("traceoff_on_warning", stop_trace_on_warning); =20 -static int __init boot_alloc_snapshot(char *str) -{ - char *slot =3D boot_snapshot_info + boot_snapshot_index; - int left =3D sizeof(boot_snapshot_info) - boot_snapshot_index; - int ret; - - if (str[0] =3D=3D '=3D') { - str++; - if (strlen(str) >=3D left) - return -1; - - ret =3D snprintf(slot, left, "%s\t", str); - boot_snapshot_index +=3D ret; - } else { - allocate_snapshot =3D true; - /* We also need the main ring buffer expanded */ - trace_set_ring_buffer_expanded(NULL); - } - return 1; -} -__setup("alloc_snapshot", boot_alloc_snapshot); - - -static int __init boot_snapshot(char *str) -{ - snapshot_at_boot =3D true; - boot_alloc_snapshot(str); - return 1; -} -__setup("ftrace_boot_snapshot", boot_snapshot); - - static int __init boot_instance(char *str) { char *slot =3D boot_instance_info + boot_instance_index; @@ -807,48 +768,6 @@ void tracing_on(void) EXPORT_SYMBOL_GPL(tracing_on); =20 #ifdef CONFIG_TRACER_SNAPSHOT -static void tracing_snapshot_instance_cond(struct trace_array *tr, - void *cond_data) -{ - struct tracer *tracer =3D tr->current_trace; - unsigned long flags; - - if (in_nmi()) { - trace_array_puts(tr, "*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n"); - trace_array_puts(tr, "*** snapshot is being ignored ***\n"); - return; - } - - if (!tr->allocated_snapshot) { - trace_array_puts(tr, "*** SNAPSHOT NOT ALLOCATED ***\n"); - trace_array_puts(tr, "*** stopping trace here! ***\n"); - tracer_tracing_off(tr); - return; - } - - /* Note, snapshot can not be used when the tracer uses it */ - if (tracer->use_max_tr) { - trace_array_puts(tr, "*** LATENCY TRACER ACTIVE ***\n"); - trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n"); - return; - } - - if (tr->mapped) { - trace_array_puts(tr, "*** BUFFER MEMORY MAPPED ***\n"); - trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n"); - return; - } - - local_irq_save(flags); - update_max_tr(tr, current, smp_processor_id(), cond_data); - local_irq_restore(flags); -} - -void tracing_snapshot_instance(struct trace_array *tr) -{ - tracing_snapshot_instance_cond(tr, NULL); -} - /** * tracing_snapshot - take a snapshot of the current buffer. * @@ -871,138 +790,6 @@ void tracing_snapshot(void) } EXPORT_SYMBOL_GPL(tracing_snapshot); =20 -/** - * tracing_snapshot_cond - conditionally take a snapshot of the current bu= ffer. - * @tr: The tracing instance to snapshot - * @cond_data: The data to be tested conditionally, and possibly saved - * - * This is the same as tracing_snapshot() except that the snapshot is - * conditional - the snapshot will only happen if the - * cond_snapshot.update() implementation receiving the cond_data - * returns true, which means that the trace array's cond_snapshot - * update() operation used the cond_data to determine whether the - * snapshot should be taken, and if it was, presumably saved it along - * with the snapshot. - */ -void tracing_snapshot_cond(struct trace_array *tr, void *cond_data) -{ - tracing_snapshot_instance_cond(tr, cond_data); -} -EXPORT_SYMBOL_GPL(tracing_snapshot_cond); - -/** - * tracing_cond_snapshot_data - get the user data associated with a snapsh= ot - * @tr: The tracing instance - * - * When the user enables a conditional snapshot using - * tracing_snapshot_cond_enable(), the user-defined cond_data is saved - * with the snapshot. This accessor is used to retrieve it. - * - * Should not be called from cond_snapshot.update(), since it takes - * the tr->max_lock lock, which the code calling - * cond_snapshot.update() has already done. - * - * Returns the cond_data associated with the trace array's snapshot. - */ -void *tracing_cond_snapshot_data(struct trace_array *tr) -{ - void *cond_data =3D NULL; - - local_irq_disable(); - arch_spin_lock(&tr->max_lock); - - if (tr->cond_snapshot) - cond_data =3D tr->cond_snapshot->cond_data; - - arch_spin_unlock(&tr->max_lock); - local_irq_enable(); - - return cond_data; -} -EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data); - -static int resize_buffer_duplicate_size(struct array_buffer *trace_buf, - struct array_buffer *size_buf, int cpu_id); -static void set_buffer_entries(struct array_buffer *buf, unsigned long val= ); - -int tracing_alloc_snapshot_instance(struct trace_array *tr) -{ - int order; - int ret; - - if (!tr->allocated_snapshot) { - - /* Make the snapshot buffer have the same order as main buffer */ - order =3D ring_buffer_subbuf_order_get(tr->array_buffer.buffer); - ret =3D ring_buffer_subbuf_order_set(tr->snapshot_buffer.buffer, order); - if (ret < 0) - return ret; - - /* allocate spare buffer */ - ret =3D resize_buffer_duplicate_size(&tr->snapshot_buffer, - &tr->array_buffer, RING_BUFFER_ALL_CPUS); - if (ret < 0) - return ret; - - tr->allocated_snapshot =3D true; - } - - return 0; -} - -static void free_snapshot(struct trace_array *tr) -{ - /* - * We don't free the ring buffer. instead, resize it because - * The max_tr ring buffer has some state (e.g. ring->clock) and - * we want preserve it. - */ - ring_buffer_subbuf_order_set(tr->snapshot_buffer.buffer, 0); - ring_buffer_resize(tr->snapshot_buffer.buffer, 1, RING_BUFFER_ALL_CPUS); - set_buffer_entries(&tr->snapshot_buffer, 1); - tracing_reset_online_cpus(&tr->snapshot_buffer); - tr->allocated_snapshot =3D false; -} - -static int tracing_arm_snapshot_locked(struct trace_array *tr) -{ - int ret; - - lockdep_assert_held(&trace_types_lock); - - spin_lock(&tr->snapshot_trigger_lock); - if (tr->snapshot =3D=3D UINT_MAX || tr->mapped) { - spin_unlock(&tr->snapshot_trigger_lock); - return -EBUSY; - } - - tr->snapshot++; - spin_unlock(&tr->snapshot_trigger_lock); - - ret =3D tracing_alloc_snapshot_instance(tr); - if (ret) { - spin_lock(&tr->snapshot_trigger_lock); - tr->snapshot--; - spin_unlock(&tr->snapshot_trigger_lock); - } - - return ret; -} - -int tracing_arm_snapshot(struct trace_array *tr) -{ - guard(mutex)(&trace_types_lock); - return tracing_arm_snapshot_locked(tr); -} - -void tracing_disarm_snapshot(struct trace_array *tr) -{ - spin_lock(&tr->snapshot_trigger_lock); - if (!WARN_ON(!tr->snapshot)) - tr->snapshot--; - spin_unlock(&tr->snapshot_trigger_lock); -} - /** * tracing_alloc_snapshot - allocate snapshot buffer. * @@ -1024,129 +811,12 @@ int tracing_alloc_snapshot(void) return ret; } EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); - -/** - * tracing_snapshot_alloc - allocate and take a snapshot of the current bu= ffer. - * - * This is similar to tracing_snapshot(), but it will allocate the - * snapshot buffer if it isn't already allocated. Use this only - * where it is safe to sleep, as the allocation may sleep. - * - * This causes a swap between the snapshot buffer and the current live - * tracing buffer. You can use this to take snapshots of the live - * trace when some condition is triggered, but continue to trace. - */ -void tracing_snapshot_alloc(void) -{ - int ret; - - ret =3D tracing_alloc_snapshot(); - if (ret < 0) - return; - - tracing_snapshot(); -} -EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); - -/** - * tracing_snapshot_cond_enable - enable conditional snapshot for an insta= nce - * @tr: The tracing instance - * @cond_data: User data to associate with the snapshot - * @update: Implementation of the cond_snapshot update function - * - * Check whether the conditional snapshot for the given instance has - * already been enabled, or if the current tracer is already using a - * snapshot; if so, return -EBUSY, else create a cond_snapshot and - * save the cond_data and update function inside. - * - * Returns 0 if successful, error otherwise. - */ -int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, - cond_update_fn_t update) -{ - struct cond_snapshot *cond_snapshot __free(kfree) =3D - kzalloc(sizeof(*cond_snapshot), GFP_KERNEL); - int ret; - - if (!cond_snapshot) - return -ENOMEM; - - cond_snapshot->cond_data =3D cond_data; - cond_snapshot->update =3D update; - - guard(mutex)(&trace_types_lock); - - if (tr->current_trace->use_max_tr) - return -EBUSY; - - /* - * The cond_snapshot can only change to NULL without the - * trace_types_lock. We don't care if we race with it going - * to NULL, but we want to make sure that it's not set to - * something other than NULL when we get here, which we can - * do safely with only holding the trace_types_lock and not - * having to take the max_lock. - */ - if (tr->cond_snapshot) - return -EBUSY; - - ret =3D tracing_arm_snapshot_locked(tr); - if (ret) - return ret; - - local_irq_disable(); - arch_spin_lock(&tr->max_lock); - tr->cond_snapshot =3D no_free_ptr(cond_snapshot); - arch_spin_unlock(&tr->max_lock); - local_irq_enable(); - - return 0; -} -EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable); - -/** - * tracing_snapshot_cond_disable - disable conditional snapshot for an ins= tance - * @tr: The tracing instance - * - * Check whether the conditional snapshot for the given instance is - * enabled; if so, free the cond_snapshot associated with it, - * otherwise return -EINVAL. - * - * Returns 0 if successful, error otherwise. - */ -int tracing_snapshot_cond_disable(struct trace_array *tr) -{ - int ret =3D 0; - - local_irq_disable(); - arch_spin_lock(&tr->max_lock); - - if (!tr->cond_snapshot) - ret =3D -EINVAL; - else { - kfree(tr->cond_snapshot); - tr->cond_snapshot =3D NULL; - } - - arch_spin_unlock(&tr->max_lock); - local_irq_enable(); - - tracing_disarm_snapshot(tr); - - return ret; -} -EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); #else void tracing_snapshot(void) { WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used"); } EXPORT_SYMBOL_GPL(tracing_snapshot); -void tracing_snapshot_cond(struct trace_array *tr, void *cond_data) -{ - WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snap= shot used"); -} -EXPORT_SYMBOL_GPL(tracing_snapshot_cond); int tracing_alloc_snapshot(void) { WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used"= ); @@ -1159,23 +829,6 @@ void tracing_snapshot_alloc(void) tracing_snapshot(); } EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); -void *tracing_cond_snapshot_data(struct trace_array *tr) -{ - return NULL; -} -EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data); -int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, = cond_update_fn_t update) -{ - return -ENODEV; -} -EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable); -int tracing_snapshot_cond_disable(struct trace_array *tr) -{ - return false; -} -EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); -#define free_snapshot(tr) do { } while (0) -#define tracing_arm_snapshot_locked(tr) ({ -EBUSY; }) #endif /* CONFIG_TRACER_SNAPSHOT */ =20 void tracer_tracing_off(struct trace_array *tr) @@ -1488,205 +1141,6 @@ static ssize_t trace_seq_to_buffer(struct trace_seq= *s, void *buf, size_t cnt) =20 unsigned long __read_mostly tracing_thresh; =20 -#ifdef CONFIG_TRACER_MAX_TRACE -static const struct file_operations tracing_max_lat_fops; - -#ifdef LATENCY_FS_NOTIFY - -static struct workqueue_struct *fsnotify_wq; - -static void latency_fsnotify_workfn(struct work_struct *work) -{ - struct trace_array *tr =3D container_of(work, struct trace_array, - fsnotify_work); - fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY); -} - -static void latency_fsnotify_workfn_irq(struct irq_work *iwork) -{ - struct trace_array *tr =3D container_of(iwork, struct trace_array, - fsnotify_irqwork); - queue_work(fsnotify_wq, &tr->fsnotify_work); -} - -static void trace_create_maxlat_file(struct trace_array *tr, - struct dentry *d_tracer) -{ - INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn); - init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq); - tr->d_max_latency =3D trace_create_file("tracing_max_latency", - TRACE_MODE_WRITE, - d_tracer, tr, - &tracing_max_lat_fops); -} - -__init static int latency_fsnotify_init(void) -{ - fsnotify_wq =3D alloc_workqueue("tr_max_lat_wq", - WQ_UNBOUND | WQ_HIGHPRI, 0); - if (!fsnotify_wq) { - pr_err("Unable to allocate tr_max_lat_wq\n"); - return -ENOMEM; - } - return 0; -} - -late_initcall_sync(latency_fsnotify_init); - -void latency_fsnotify(struct trace_array *tr) -{ - if (!fsnotify_wq) - return; - /* - * We cannot call queue_work(&tr->fsnotify_work) from here because it's - * possible that we are called from __schedule() or do_idle(), which - * could cause a deadlock. - */ - irq_work_queue(&tr->fsnotify_irqwork); -} - -#else /* !LATENCY_FS_NOTIFY */ - -static inline void trace_create_maxlat_file(struct trace_array *tr, - struct dentry *d_tracer) { } -#endif - -/* - * Copy the new maximum trace into the separate maximum-trace - * structure. (this way the maximum trace is permanently saved, - * for later retrieval via /sys/kernel/tracing/tracing_max_latency) - */ -static void -__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) -{ - struct array_buffer *trace_buf =3D &tr->array_buffer; - struct array_buffer *max_buf =3D &tr->snapshot_buffer; - struct trace_array_cpu *data =3D per_cpu_ptr(trace_buf->data, cpu); - struct trace_array_cpu *max_data =3D per_cpu_ptr(max_buf->data, cpu); - - max_buf->cpu =3D cpu; - max_buf->time_start =3D data->preempt_timestamp; - - max_data->saved_latency =3D tr->max_latency; - max_data->critical_start =3D data->critical_start; - max_data->critical_end =3D data->critical_end; - - strscpy(max_data->comm, tsk->comm); - max_data->pid =3D tsk->pid; - /* - * If tsk =3D=3D current, then use current_uid(), as that does not use - * RCU. The irq tracer can be called out of RCU scope. - */ - if (tsk =3D=3D current) - max_data->uid =3D current_uid(); - else - max_data->uid =3D task_uid(tsk); - - max_data->nice =3D tsk->static_prio - 20 - MAX_RT_PRIO; - max_data->policy =3D tsk->policy; - max_data->rt_priority =3D tsk->rt_priority; - - /* record this tasks comm */ - tracing_record_cmdline(tsk); - latency_fsnotify(tr); -} - -/** - * update_max_tr - snapshot all trace buffers from global_trace to max_tr - * @tr: tracer - * @tsk: the task with the latency - * @cpu: The cpu that initiated the trace. - * @cond_data: User data associated with a conditional snapshot - * - * Flip the buffers between the @tr and the max_tr and record information - * about which task was the cause of this latency. - */ -void -update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu, - void *cond_data) -{ - if (tr->stop_count) - return; - - WARN_ON_ONCE(!irqs_disabled()); - - if (!tr->allocated_snapshot) { - /* Only the nop tracer should hit this when disabling */ - WARN_ON_ONCE(tr->current_trace !=3D &nop_trace); - return; - } - - arch_spin_lock(&tr->max_lock); - - /* Inherit the recordable setting from array_buffer */ - if (ring_buffer_record_is_set_on(tr->array_buffer.buffer)) - ring_buffer_record_on(tr->snapshot_buffer.buffer); - else - ring_buffer_record_off(tr->snapshot_buffer.buffer); - -#ifdef CONFIG_TRACER_SNAPSHOT - if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data)) { - arch_spin_unlock(&tr->max_lock); - return; - } -#endif - swap(tr->array_buffer.buffer, tr->snapshot_buffer.buffer); - - __update_max_tr(tr, tsk, cpu); - - arch_spin_unlock(&tr->max_lock); - - /* Any waiters on the old snapshot buffer need to wake up */ - ring_buffer_wake_waiters(tr->array_buffer.buffer, RING_BUFFER_ALL_CPUS); -} - -/** - * update_max_tr_single - only copy one trace over, and reset the rest - * @tr: tracer - * @tsk: task with the latency - * @cpu: the cpu of the buffer to copy. - * - * Flip the trace of a single CPU buffer between the @tr and the max_tr. - */ -void -update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int = cpu) -{ - int ret; - - if (tr->stop_count) - return; - - WARN_ON_ONCE(!irqs_disabled()); - if (!tr->allocated_snapshot) { - /* Only the nop tracer should hit this when disabling */ - WARN_ON_ONCE(tr->current_trace !=3D &nop_trace); - return; - } - - arch_spin_lock(&tr->max_lock); - - ret =3D ring_buffer_swap_cpu(tr->snapshot_buffer.buffer, tr->array_buffer= .buffer, cpu); - - if (ret =3D=3D -EBUSY) { - /* - * We failed to swap the buffer due to a commit taking - * place on this CPU. We fail to record, but we reset - * the max trace buffer (no one writes directly to it) - * and flag that it failed. - * Another reason is resize is in progress. - */ - trace_array_printk_buf(tr->snapshot_buffer.buffer, _THIS_IP_, - "Failed to swap buffers due to commit or resize in progress\n"); - } - - WARN_ON_ONCE(ret && ret !=3D -EAGAIN && ret !=3D -EBUSY); - - __update_max_tr(tr, tsk, cpu); - arch_spin_unlock(&tr->max_lock); -} - -#endif /* CONFIG_TRACER_MAX_TRACE */ - struct pipe_wait { struct trace_iterator *iter; int wait_index; @@ -1995,7 +1449,7 @@ int __init register_tracer(struct tracer *type) return 0; } =20 -static void tracing_reset_cpu(struct array_buffer *buf, int cpu) +void tracing_reset_cpu(struct array_buffer *buf, int cpu) { struct trace_buffer *buffer =3D buf->buffer; =20 @@ -3764,50 +3218,6 @@ static void test_ftrace_alive(struct seq_file *m) "# MAY BE MISSING FUNCTION EVENTS\n"); } =20 -#ifdef CONFIG_TRACER_SNAPSHOT -static void show_snapshot_main_help(struct seq_file *m) -{ - seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n" - "# echo 1 > snapshot : Allocates snapshot buffer, if not already all= ocated.\n" - "# Takes a snapshot of the main buffer.\n" - "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate= or free)\n" - "# (Doesn't have to be '2' works with any numbe= r that\n" - "# is not a '0' or '1')\n"); -} - -static void show_snapshot_percpu_help(struct seq_file *m) -{ - seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n"); -#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP - seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not alre= ady allocated.\n" - "# Takes a snapshot of the main buffer for this= cpu.\n"); -#else - seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n" - "# Must use main snapshot file to allocate.\n"); -#endif - seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but= does not allocate)\n" - "# (Doesn't have to be '2' works with any numbe= r that\n" - "# is not a '0' or '1')\n"); -} - -static void print_snapshot_help(struct seq_file *m, struct trace_iterator = *iter) -{ - if (iter->tr->allocated_snapshot) - seq_puts(m, "#\n# * Snapshot is allocated *\n#\n"); - else - seq_puts(m, "#\n# * Snapshot is freed *\n#\n"); - - seq_puts(m, "# Snapshot commands:\n"); - if (iter->cpu_file =3D=3D RING_BUFFER_ALL_CPUS) - show_snapshot_main_help(m); - else - show_snapshot_percpu_help(m); -} -#else -/* Should never be called */ -static inline void print_snapshot_help(struct seq_file *m, struct trace_it= erator *iter) { } -#endif - static int s_show(struct seq_file *m, void *v) { struct trace_iterator *iter =3D v; @@ -3856,17 +3266,6 @@ static int s_show(struct seq_file *m, void *v) return 0; } =20 -/* - * Should be used after trace_array_get(), trace_types_lock - * ensures that i_cdev was already initialized. - */ -static inline int tracing_get_cpu(struct inode *inode) -{ - if (inode->i_cdev) /* See trace_create_cpu_file() */ - return (long)inode->i_cdev - 1; - return RING_BUFFER_ALL_CPUS; -} - static const struct seq_operations tracer_seq_ops =3D { .start =3D s_start, .next =3D s_next, @@ -3893,7 +3292,7 @@ static void free_trace_iter_content(struct trace_iter= ator *iter) free_cpumask_var(iter->started); } =20 -static struct trace_iterator * +struct trace_iterator * __tracing_open(struct inode *inode, struct file *file, bool snapshot) { struct trace_array *tr =3D inode->i_private; @@ -4076,7 +3475,7 @@ int tracing_single_release_file_tr(struct inode *inod= e, struct file *filp) return single_release(inode, filp); } =20 -static int tracing_release(struct inode *inode, struct file *file) +int tracing_release(struct inode *inode, struct file *file) { struct trace_array *tr =3D inode->i_private; struct seq_file *m =3D file->private_data; @@ -5224,7 +4623,7 @@ int tracer_init(struct tracer *t, struct trace_array = *tr) return t->init(tr); } =20 -static void set_buffer_entries(struct array_buffer *buf, unsigned long val) +void trace_set_buffer_entries(struct array_buffer *buf, unsigned long val) { int cpu; =20 @@ -5235,40 +4634,12 @@ static void set_buffer_entries(struct array_buffer = *buf, unsigned long val) static void update_buffer_entries(struct array_buffer *buf, int cpu) { if (cpu =3D=3D RING_BUFFER_ALL_CPUS) { - set_buffer_entries(buf, ring_buffer_size(buf->buffer, 0)); + trace_set_buffer_entries(buf, ring_buffer_size(buf->buffer, 0)); } else { per_cpu_ptr(buf->data, cpu)->entries =3D ring_buffer_size(buf->buffer, c= pu); } } =20 -#ifdef CONFIG_TRACER_SNAPSHOT -/* resize @tr's buffer to the size of @size_tr's entries */ -static int resize_buffer_duplicate_size(struct array_buffer *trace_buf, - struct array_buffer *size_buf, int cpu_id) -{ - int cpu, ret =3D 0; - - if (cpu_id =3D=3D RING_BUFFER_ALL_CPUS) { - for_each_tracing_cpu(cpu) { - ret =3D ring_buffer_resize(trace_buf->buffer, - per_cpu_ptr(size_buf->data, cpu)->entries, cpu); - if (ret < 0) - break; - per_cpu_ptr(trace_buf->data, cpu)->entries =3D - per_cpu_ptr(size_buf->data, cpu)->entries; - } - } else { - ret =3D ring_buffer_resize(trace_buf->buffer, - per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id); - if (ret =3D=3D 0) - per_cpu_ptr(trace_buf->data, cpu_id)->entries =3D - per_cpu_ptr(size_buf->data, cpu_id)->entries; - } - - return ret; -} -#endif /* CONFIG_TRACER_SNAPSHOT */ - static int __tracing_resize_ring_buffer(struct trace_array *tr, unsigned long size, int cpu) { @@ -5695,9 +5066,8 @@ tracing_set_trace_write(struct file *filp, const char= __user *ubuf, return ret; } =20 -static ssize_t -tracing_nsecs_read(unsigned long *ptr, char __user *ubuf, - size_t cnt, loff_t *ppos) +ssize_t tracing_nsecs_read(unsigned long *ptr, char __user *ubuf, + size_t cnt, loff_t *ppos) { char buf[64]; int r; @@ -5709,9 +5079,8 @@ tracing_nsecs_read(unsigned long *ptr, char __user *u= buf, return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); } =20 -static ssize_t -tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf, - size_t cnt, loff_t *ppos) +ssize_t tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf, + size_t cnt, loff_t *ppos) { unsigned long val; int ret; @@ -5753,28 +5122,6 @@ tracing_thresh_write(struct file *filp, const char _= _user *ubuf, return cnt; } =20 -#ifdef CONFIG_TRACER_MAX_TRACE - -static ssize_t -tracing_max_lat_read(struct file *filp, char __user *ubuf, - size_t cnt, loff_t *ppos) -{ - struct trace_array *tr =3D filp->private_data; - - return tracing_nsecs_read(&tr->max_latency, ubuf, cnt, ppos); -} - -static ssize_t -tracing_max_lat_write(struct file *filp, const char __user *ubuf, - size_t cnt, loff_t *ppos) -{ - struct trace_array *tr =3D filp->private_data; - - return tracing_nsecs_write(&tr->max_latency, ubuf, cnt, ppos); -} - -#endif - static int open_pipe_on_cpu(struct trace_array *tr, int cpu) { if (cpu =3D=3D RING_BUFFER_ALL_CPUS) { @@ -7061,266 +6408,78 @@ static ssize_t tracing_clock_write(struct file *fi= lp, const char __user *ubuf, const char *clockstr; int ret; =20 - if (cnt >=3D sizeof(buf)) - return -EINVAL; - - if (copy_from_user(buf, ubuf, cnt)) - return -EFAULT; - - buf[cnt] =3D 0; - - clockstr =3D strstrip(buf); - - ret =3D tracing_set_clock(tr, clockstr); - if (ret) - return ret; - - *fpos +=3D cnt; - - return cnt; -} - -static int tracing_clock_open(struct inode *inode, struct file *file) -{ - struct trace_array *tr =3D inode->i_private; - int ret; - - ret =3D tracing_check_open_get_tr(tr); - if (ret) - return ret; - - ret =3D single_open(file, tracing_clock_show, inode->i_private); - if (ret < 0) - trace_array_put(tr); - - return ret; -} - -static int tracing_time_stamp_mode_show(struct seq_file *m, void *v) -{ - struct trace_array *tr =3D m->private; - - guard(mutex)(&trace_types_lock); - - if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer)) - seq_puts(m, "delta [absolute]\n"); - else - seq_puts(m, "[delta] absolute\n"); - - return 0; -} - -static int tracing_time_stamp_mode_open(struct inode *inode, struct file *= file) -{ - struct trace_array *tr =3D inode->i_private; - int ret; - - ret =3D tracing_check_open_get_tr(tr); - if (ret) - return ret; - - ret =3D single_open(file, tracing_time_stamp_mode_show, inode->i_private); - if (ret < 0) - trace_array_put(tr); - - return ret; -} - -u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buff= er_event *rbe) -{ - if (rbe =3D=3D this_cpu_read(trace_buffered_event)) - return ring_buffer_time_stamp(buffer); - - return ring_buffer_event_time_stamp(buffer, rbe); -} - -struct ftrace_buffer_info { - struct trace_iterator iter; - void *spare; - unsigned int spare_cpu; - unsigned int spare_size; - unsigned int read; -}; - -#ifdef CONFIG_TRACER_SNAPSHOT -static int tracing_snapshot_open(struct inode *inode, struct file *file) -{ - struct trace_array *tr =3D inode->i_private; - struct trace_iterator *iter; - struct seq_file *m; - int ret; - - ret =3D tracing_check_open_get_tr(tr); - if (ret) - return ret; - - if (file->f_mode & FMODE_READ) { - iter =3D __tracing_open(inode, file, true); - if (IS_ERR(iter)) - ret =3D PTR_ERR(iter); - } else { - /* Writes still need the seq_file to hold the private data */ - ret =3D -ENOMEM; - m =3D kzalloc(sizeof(*m), GFP_KERNEL); - if (!m) - goto out; - iter =3D kzalloc(sizeof(*iter), GFP_KERNEL); - if (!iter) { - kfree(m); - goto out; - } - ret =3D 0; - - iter->tr =3D tr; - iter->array_buffer =3D &tr->snapshot_buffer; - iter->cpu_file =3D tracing_get_cpu(inode); - m->private =3D iter; - file->private_data =3D m; - } -out: - if (ret < 0) - trace_array_put(tr); - - return ret; -} - -static void tracing_swap_cpu_buffer(void *tr) -{ - update_max_tr_single((struct trace_array *)tr, current, smp_processor_id(= )); -} - -static ssize_t -tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t = cnt, - loff_t *ppos) -{ - struct seq_file *m =3D filp->private_data; - struct trace_iterator *iter =3D m->private; - struct trace_array *tr =3D iter->tr; - unsigned long val; - int ret; - - ret =3D tracing_update_buffers(tr); - if (ret < 0) - return ret; + if (cnt >=3D sizeof(buf)) + return -EINVAL; =20 - ret =3D kstrtoul_from_user(ubuf, cnt, 10, &val); - if (ret) - return ret; + if (copy_from_user(buf, ubuf, cnt)) + return -EFAULT; =20 - guard(mutex)(&trace_types_lock); + buf[cnt] =3D 0; =20 - if (tr->current_trace->use_max_tr) - return -EBUSY; + clockstr =3D strstrip(buf); =20 - local_irq_disable(); - arch_spin_lock(&tr->max_lock); - if (tr->cond_snapshot) - ret =3D -EBUSY; - arch_spin_unlock(&tr->max_lock); - local_irq_enable(); + ret =3D tracing_set_clock(tr, clockstr); if (ret) return ret; =20 - switch (val) { - case 0: - if (iter->cpu_file !=3D RING_BUFFER_ALL_CPUS) - return -EINVAL; - if (tr->allocated_snapshot) - free_snapshot(tr); - break; - case 1: -/* Only allow per-cpu swap if the ring buffer supports it */ -#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP - if (iter->cpu_file !=3D RING_BUFFER_ALL_CPUS) - return -EINVAL; -#endif - if (tr->allocated_snapshot) - ret =3D resize_buffer_duplicate_size(&tr->snapshot_buffer, - &tr->array_buffer, iter->cpu_file); + *fpos +=3D cnt; =20 - ret =3D tracing_arm_snapshot_locked(tr); - if (ret) - return ret; + return cnt; +} =20 - /* Now, we're going to swap */ - if (iter->cpu_file =3D=3D RING_BUFFER_ALL_CPUS) { - local_irq_disable(); - update_max_tr(tr, current, smp_processor_id(), NULL); - local_irq_enable(); - } else { - smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer, - (void *)tr, 1); - } - tracing_disarm_snapshot(tr); - break; - default: - if (tr->allocated_snapshot) { - if (iter->cpu_file =3D=3D RING_BUFFER_ALL_CPUS) - tracing_reset_online_cpus(&tr->snapshot_buffer); - else - tracing_reset_cpu(&tr->snapshot_buffer, iter->cpu_file); - } - break; - } +static int tracing_clock_open(struct inode *inode, struct file *file) +{ + struct trace_array *tr =3D inode->i_private; + int ret; =20 - if (ret >=3D 0) { - *ppos +=3D cnt; - ret =3D cnt; - } + ret =3D tracing_check_open_get_tr(tr); + if (ret) + return ret; + + ret =3D single_open(file, tracing_clock_show, inode->i_private); + if (ret < 0) + trace_array_put(tr); =20 return ret; } =20 -static int tracing_snapshot_release(struct inode *inode, struct file *file) +static int tracing_time_stamp_mode_show(struct seq_file *m, void *v) { - struct seq_file *m =3D file->private_data; - int ret; - - ret =3D tracing_release(inode, file); + struct trace_array *tr =3D m->private; =20 - if (file->f_mode & FMODE_READ) - return ret; + guard(mutex)(&trace_types_lock); =20 - /* If write only, the seq_file is just a stub */ - if (m) - kfree(m->private); - kfree(m); + if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer)) + seq_puts(m, "delta [absolute]\n"); + else + seq_puts(m, "[delta] absolute\n"); =20 return 0; } =20 -static int tracing_buffers_open(struct inode *inode, struct file *filp); -static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, - size_t count, loff_t *ppos); -static int tracing_buffers_release(struct inode *inode, struct file *file); -static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, - struct pipe_inode_info *pipe, size_t len, unsigned int flags); - -static int snapshot_raw_open(struct inode *inode, struct file *filp) +static int tracing_time_stamp_mode_open(struct inode *inode, struct file *= file) { - struct ftrace_buffer_info *info; + struct trace_array *tr =3D inode->i_private; int ret; =20 - /* The following checks for tracefs lockdown */ - ret =3D tracing_buffers_open(inode, filp); - if (ret < 0) + ret =3D tracing_check_open_get_tr(tr); + if (ret) return ret; =20 - info =3D filp->private_data; - - if (info->iter.trace->use_max_tr) { - tracing_buffers_release(inode, filp); - return -EBUSY; - } - - info->iter.snapshot =3D true; - info->iter.array_buffer =3D &info->iter.tr->snapshot_buffer; + ret =3D single_open(file, tracing_time_stamp_mode_show, inode->i_private); + if (ret < 0) + trace_array_put(tr); =20 return ret; } =20 -#endif /* CONFIG_TRACER_SNAPSHOT */ +u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buff= er_event *rbe) +{ + if (rbe =3D=3D this_cpu_read(trace_buffered_event)) + return ring_buffer_time_stamp(buffer); =20 + return ring_buffer_event_time_stamp(buffer, rbe); +} =20 static const struct file_operations tracing_thresh_fops =3D { .open =3D tracing_open_generic, @@ -7329,16 +6488,6 @@ static const struct file_operations tracing_thresh_f= ops =3D { .llseek =3D generic_file_llseek, }; =20 -#ifdef CONFIG_TRACER_MAX_TRACE -static const struct file_operations tracing_max_lat_fops =3D { - .open =3D tracing_open_generic_tr, - .read =3D tracing_max_lat_read, - .write =3D tracing_max_lat_write, - .llseek =3D generic_file_llseek, - .release =3D tracing_release_generic_tr, -}; -#endif - static const struct file_operations set_tracer_fops =3D { .open =3D tracing_open_generic_tr, .read =3D tracing_set_trace_read, @@ -7425,24 +6574,6 @@ static const struct file_operations last_boot_fops = =3D { .release =3D tracing_seq_release, }; =20 -#ifdef CONFIG_TRACER_SNAPSHOT -static const struct file_operations snapshot_fops =3D { - .open =3D tracing_snapshot_open, - .read =3D seq_read, - .write =3D tracing_snapshot_write, - .llseek =3D tracing_lseek, - .release =3D tracing_snapshot_release, -}; - -static const struct file_operations snapshot_raw_fops =3D { - .open =3D snapshot_raw_open, - .read =3D tracing_buffers_read, - .release =3D tracing_buffers_release, - .splice_read =3D tracing_buffers_splice_read, -}; - -#endif /* CONFIG_TRACER_SNAPSHOT */ - /* * trace_min_max_write - Write a u64 value to a trace_min_max_param struct * @filp: The active open file structure @@ -7802,7 +6933,7 @@ static const struct file_operations tracing_err_log_f= ops =3D { .release =3D tracing_err_log_release, }; =20 -static int tracing_buffers_open(struct inode *inode, struct file *filp) +int tracing_buffers_open(struct inode *inode, struct file *filp) { struct trace_array *tr =3D inode->i_private; struct ftrace_buffer_info *info; @@ -7850,9 +6981,8 @@ tracing_buffers_poll(struct file *filp, poll_table *p= oll_table) return trace_poll(iter, filp, poll_table); } =20 -static ssize_t -tracing_buffers_read(struct file *filp, char __user *ubuf, - size_t count, loff_t *ppos) +ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, + size_t count, loff_t *ppos) { struct ftrace_buffer_info *info =3D filp->private_data; struct trace_iterator *iter =3D &info->iter; @@ -7955,7 +7085,7 @@ static int tracing_buffers_flush(struct file *file, f= l_owner_t id) return 0; } =20 -static int tracing_buffers_release(struct inode *inode, struct file *file) +int tracing_buffers_release(struct inode *inode, struct file *file) { struct ftrace_buffer_info *info =3D file->private_data; struct trace_iterator *iter =3D &info->iter; @@ -8029,10 +7159,9 @@ static void buffer_spd_release(struct splice_pipe_de= sc *spd, unsigned int i) spd->partial[i].private =3D 0; } =20 -static ssize_t -tracing_buffers_splice_read(struct file *file, loff_t *ppos, - struct pipe_inode_info *pipe, size_t len, - unsigned int flags) +ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, + struct pipe_inode_info *pipe, size_t len, + unsigned int flags) { struct ftrace_buffer_info *info =3D file->private_data; struct trace_iterator *iter =3D &info->iter; @@ -8188,44 +7317,6 @@ static long tracing_buffers_ioctl(struct file *file,= unsigned int cmd, unsigned return 0; } =20 -#ifdef CONFIG_TRACER_SNAPSHOT -static int get_snapshot_map(struct trace_array *tr) -{ - int err =3D 0; - - /* - * Called with mmap_lock held. lockdep would be unhappy if we would now - * take trace_types_lock. Instead use the specific - * snapshot_trigger_lock. - */ - spin_lock(&tr->snapshot_trigger_lock); - - if (tr->snapshot || tr->mapped =3D=3D UINT_MAX) - err =3D -EBUSY; - else - tr->mapped++; - - spin_unlock(&tr->snapshot_trigger_lock); - - /* Wait for update_max_tr() to observe iter->tr->mapped */ - if (tr->mapped =3D=3D 1) - synchronize_rcu(); - - return err; - -} -static void put_snapshot_map(struct trace_array *tr) -{ - spin_lock(&tr->snapshot_trigger_lock); - if (!WARN_ON(!tr->mapped)) - tr->mapped--; - spin_unlock(&tr->snapshot_trigger_lock); -} -#else -static inline int get_snapshot_map(struct trace_array *tr) { return 0; } -static inline void put_snapshot_map(struct trace_array *tr) { } -#endif - static void tracing_buffers_mmap_close(struct vm_area_struct *vma) { struct ftrace_buffer_info *info =3D vma->vm_file->private_data; @@ -8393,170 +7484,6 @@ static const struct file_operations tracing_dyn_inf= o_fops =3D { }; #endif /* CONFIG_DYNAMIC_FTRACE */ =20 -#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) -static void -ftrace_snapshot(unsigned long ip, unsigned long parent_ip, - struct trace_array *tr, struct ftrace_probe_ops *ops, - void *data) -{ - tracing_snapshot_instance(tr); -} - -static void -ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, - struct trace_array *tr, struct ftrace_probe_ops *ops, - void *data) -{ - struct ftrace_func_mapper *mapper =3D data; - long *count =3D NULL; - - if (mapper) - count =3D (long *)ftrace_func_mapper_find_ip(mapper, ip); - - if (count) { - - if (*count <=3D 0) - return; - - (*count)--; - } - - tracing_snapshot_instance(tr); -} - -static int -ftrace_snapshot_print(struct seq_file *m, unsigned long ip, - struct ftrace_probe_ops *ops, void *data) -{ - struct ftrace_func_mapper *mapper =3D data; - long *count =3D NULL; - - seq_printf(m, "%ps:", (void *)ip); - - seq_puts(m, "snapshot"); - - if (mapper) - count =3D (long *)ftrace_func_mapper_find_ip(mapper, ip); - - if (count) - seq_printf(m, ":count=3D%ld\n", *count); - else - seq_puts(m, ":unlimited\n"); - - return 0; -} - -static int -ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr, - unsigned long ip, void *init_data, void **data) -{ - struct ftrace_func_mapper *mapper =3D *data; - - if (!mapper) { - mapper =3D allocate_ftrace_func_mapper(); - if (!mapper) - return -ENOMEM; - *data =3D mapper; - } - - return ftrace_func_mapper_add_ip(mapper, ip, init_data); -} - -static void -ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr, - unsigned long ip, void *data) -{ - struct ftrace_func_mapper *mapper =3D data; - - if (!ip) { - if (!mapper) - return; - free_ftrace_func_mapper(mapper, NULL); - return; - } - - ftrace_func_mapper_remove_ip(mapper, ip); -} - -static struct ftrace_probe_ops snapshot_probe_ops =3D { - .func =3D ftrace_snapshot, - .print =3D ftrace_snapshot_print, -}; - -static struct ftrace_probe_ops snapshot_count_probe_ops =3D { - .func =3D ftrace_count_snapshot, - .print =3D ftrace_snapshot_print, - .init =3D ftrace_snapshot_init, - .free =3D ftrace_snapshot_free, -}; - -static int -ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash = *hash, - char *glob, char *cmd, char *param, int enable) -{ - struct ftrace_probe_ops *ops; - void *count =3D (void *)-1; - char *number; - int ret; - - if (!tr) - return -ENODEV; - - /* hash funcs only work with set_ftrace_filter */ - if (!enable) - return -EINVAL; - - ops =3D param ? &snapshot_count_probe_ops : &snapshot_probe_ops; - - if (glob[0] =3D=3D '!') { - ret =3D unregister_ftrace_function_probe_func(glob+1, tr, ops); - if (!ret) - tracing_disarm_snapshot(tr); - - return ret; - } - - if (!param) - goto out_reg; - - number =3D strsep(¶m, ":"); - - if (!strlen(number)) - goto out_reg; - - /* - * We use the callback data field (which is a pointer) - * as our counter. - */ - ret =3D kstrtoul(number, 0, (unsigned long *)&count); - if (ret) - return ret; - - out_reg: - ret =3D tracing_arm_snapshot(tr); - if (ret < 0) - return ret; - - ret =3D register_ftrace_function_probe(glob, tr, ops, count); - if (ret < 0) - tracing_disarm_snapshot(tr); - - return ret < 0 ? ret : 0; -} - -static struct ftrace_func_command ftrace_snapshot_cmd =3D { - .name =3D "snapshot", - .func =3D ftrace_trace_snapshot_callback, -}; - -static __init int register_snapshot_cmd(void) -{ - return register_ftrace_command(&ftrace_snapshot_cmd); -} -#else -static inline __init int register_snapshot_cmd(void) { return 0; } -#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE= ) */ - static struct dentry *tracing_get_dentry(struct trace_array *tr) { /* Top directory uses NULL as the parent */ @@ -9349,7 +8276,7 @@ static void setup_trace_scratch(struct trace_array *t= r, memset(tscratch, 0, size); } =20 -static int +int allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, in= t size) { enum ring_buffer_flags rb_flags; @@ -9389,8 +8316,8 @@ allocate_trace_buffer(struct trace_array *tr, struct = array_buffer *buf, int size } =20 /* Allocate the first page for all buffers */ - set_buffer_entries(&tr->array_buffer, - ring_buffer_size(tr->array_buffer.buffer, 0)); + trace_set_buffer_entries(&tr->array_buffer, + ring_buffer_size(tr->array_buffer.buffer, 0)); =20 return 0; } @@ -9413,23 +8340,11 @@ static int allocate_trace_buffers(struct trace_arra= y *tr, int size) if (ret) return ret; =20 -#ifdef CONFIG_TRACER_SNAPSHOT - /* Fix mapped buffer trace arrays do not have snapshot buffers */ - if (tr->range_addr_start) - return 0; - - ret =3D allocate_trace_buffer(tr, &tr->snapshot_buffer, - allocate_snapshot ? size : 1); - if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) { + ret =3D trace_allocate_snapshot(tr, size); + if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) free_trace_buffer(&tr->array_buffer); - return -ENOMEM; - } - tr->allocated_snapshot =3D allocate_snapshot; - - allocate_snapshot =3D false; -#endif =20 - return 0; + return ret; } =20 static void free_trace_buffers(struct trace_array *tr) @@ -10536,47 +9451,6 @@ ssize_t trace_parse_run_command(struct file *file, = const char __user *buffer, return done; } =20 -#ifdef CONFIG_TRACER_SNAPSHOT -__init static bool tr_needs_alloc_snapshot(const char *name) -{ - char *test; - int len =3D strlen(name); - bool ret; - - if (!boot_snapshot_index) - return false; - - if (strncmp(name, boot_snapshot_info, len) =3D=3D 0 && - boot_snapshot_info[len] =3D=3D '\t') - return true; - - test =3D kmalloc(strlen(name) + 3, GFP_KERNEL); - if (!test) - return false; - - sprintf(test, "\t%s\t", name); - ret =3D strstr(boot_snapshot_info, test) =3D=3D NULL; - kfree(test); - return ret; -} - -__init static void do_allocate_snapshot(const char *name) -{ - if (!tr_needs_alloc_snapshot(name)) - return; - - /* - * When allocate_snapshot is set, the next call to - * allocate_trace_buffers() (called by trace_array_get_by_name()) - * will allocate the snapshot buffer. That will also clear - * this flag. - */ - allocate_snapshot =3D true; -} -#else -static inline void do_allocate_snapshot(const char *name) { } -#endif - __init static int backup_instance_area(const char *backup, unsigned long *addr, phys_addr_t *size) { @@ -10726,8 +9600,7 @@ __init static void enable_instances(void) } } else { /* Only non mapped buffers have snapshot buffers */ - if (IS_ENABLED(CONFIG_TRACER_SNAPSHOT)) - do_allocate_snapshot(name); + do_allocate_snapshot(name); } =20 tr =3D trace_array_create_systems(name, NULL, addr, size); @@ -10919,24 +9792,6 @@ struct trace_array *trace_get_global_array(void) } #endif =20 -void __init ftrace_boot_snapshot(void) -{ -#ifdef CONFIG_TRACER_SNAPSHOT - struct trace_array *tr; - - if (!snapshot_at_boot) - return; - - list_for_each_entry(tr, &ftrace_trace_arrays, list) { - if (!tr->allocated_snapshot) - continue; - - tracing_snapshot_instance(tr); - trace_array_puts(tr, "** Boot snapshot taken **\n"); - } -#endif -} - void __init early_trace_init(void) { if (tracepoint_printk) { diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 374b7c392f0a..7bf7fb81a360 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -261,6 +261,7 @@ static inline bool still_need_pid_events(int type, stru= ct trace_pid_list *pid_li =20 typedef bool (*cond_update_fn_t)(struct trace_array *tr, void *cond_data); =20 +#ifdef CONFIG_TRACER_SNAPSHOT /** * struct cond_snapshot - conditional snapshot data and callback * @@ -303,6 +304,7 @@ struct cond_snapshot { void *cond_data; cond_update_fn_t update; }; +#endif /* CONFIG_TRACER_SNAPSHOT */ =20 /* * struct trace_func_repeats - used to keep track of the consecutive @@ -672,6 +674,7 @@ void tracing_reset_all_online_cpus(void); void tracing_reset_all_online_cpus_unlocked(void); int tracing_open_generic(struct inode *inode, struct file *filp); int tracing_open_generic_tr(struct inode *inode, struct file *filp); +int tracing_release(struct inode *inode, struct file *file); int tracing_release_generic_tr(struct inode *inode, struct file *file); int tracing_open_file_tr(struct inode *inode, struct file *filp); int tracing_release_file_tr(struct inode *inode, struct file *filp); @@ -681,12 +684,48 @@ void tracer_tracing_on(struct trace_array *tr); void tracer_tracing_off(struct trace_array *tr); void tracer_tracing_disable(struct trace_array *tr); void tracer_tracing_enable(struct trace_array *tr); +int allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf= , int size); struct dentry *trace_create_file(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops); =20 +struct trace_iterator *__tracing_open(struct inode *inode, struct file *fi= le, + bool snapshot); +int tracing_buffers_open(struct inode *inode, struct file *filp); +ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf, + size_t count, loff_t *ppos); +int tracing_buffers_release(struct inode *inode, struct file *file); +ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos, + struct pipe_inode_info *pipe, size_t len, unsigned int flags); + +ssize_t tracing_nsecs_read(unsigned long *ptr, char __user *ubuf, + size_t cnt, loff_t *ppos); +ssize_t tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf, + size_t cnt, loff_t *ppos); + +void trace_set_buffer_entries(struct array_buffer *buf, unsigned long val); + +/* + * Should be used after trace_array_get(), trace_types_lock + * ensures that i_cdev was already initialized. + */ +static inline int tracing_get_cpu(struct inode *inode) +{ + if (inode->i_cdev) /* See trace_create_cpu_file() */ + return (long)inode->i_cdev - 1; + return RING_BUFFER_ALL_CPUS; +} +void tracing_reset_cpu(struct array_buffer *buf, int cpu); + +struct ftrace_buffer_info { + struct trace_iterator iter; + void *spare; + unsigned int spare_cpu; + unsigned int spare_size; + unsigned int read; +}; =20 /** * tracer_tracing_is_on_cpu - show real state of ring buffer enabled on fo= r a cpu @@ -798,10 +837,15 @@ void update_max_tr(struct trace_array *tr, struct tas= k_struct *tsk, int cpu, void *cond_data); void update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu); +void trace_create_maxlat_file(struct trace_array *tr, + struct dentry *d_tracer); =20 #ifdef CONFIG_FSNOTIFY #define LATENCY_FS_NOTIFY #endif +#else /* CONFIG_TRACER_MAX_TRACE */ +static inline void trace_create_maxlat_file(struct trace_array *tr, + struct dentry *d_tracer) { } #endif /* CONFIG_TRACER_MAX_TRACE */ =20 #ifdef LATENCY_FS_NOTIFY @@ -2119,12 +2163,6 @@ static inline bool event_command_needs_rec(struct ev= ent_command *cmd_ops) =20 extern int trace_event_enable_disable(struct trace_event_file *file, int enable, int soft_disable); -extern int tracing_alloc_snapshot(void); -extern void tracing_snapshot_cond(struct trace_array *tr, void *cond_data); -extern int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond= _data, cond_update_fn_t update); - -extern int tracing_snapshot_cond_disable(struct trace_array *tr); -extern void *tracing_cond_snapshot_data(struct trace_array *tr); =20 extern const char *__start___trace_bprintk_fmt[]; extern const char *__stop___trace_bprintk_fmt[]; @@ -2212,19 +2250,72 @@ static inline void trace_event_update_all(struct tr= ace_eval_map **map, int len) #endif =20 #ifdef CONFIG_TRACER_SNAPSHOT +extern const struct file_operations snapshot_fops; +extern const struct file_operations snapshot_raw_fops; + +/* Used when creating instances */ +int trace_allocate_snapshot(struct trace_array *tr, int size); + +int tracing_alloc_snapshot(void); +void tracing_snapshot_cond(struct trace_array *tr, void *cond_data); +int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, = cond_update_fn_t update); +int tracing_snapshot_cond_disable(struct trace_array *tr); +void *tracing_cond_snapshot_data(struct trace_array *tr); void tracing_snapshot_instance(struct trace_array *tr); int tracing_alloc_snapshot_instance(struct trace_array *tr); +int tracing_arm_snapshot_locked(struct trace_array *tr); int tracing_arm_snapshot(struct trace_array *tr); void tracing_disarm_snapshot(struct trace_array *tr); -#else +void free_snapshot(struct trace_array *tr); +void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter); +int get_snapshot_map(struct trace_array *tr); +void put_snapshot_map(struct trace_array *tr); +int resize_buffer_duplicate_size(struct array_buffer *trace_buf, + struct array_buffer *size_buf, int cpu_id); +__init void do_allocate_snapshot(const char *name); +__init int register_snapshot_cmd(void); +# ifdef CONFIG_DYNAMIC_FTRACE +__init int register_snapshot_cmd(void); +# else +static inline int register_snapshot_cmd(void) { return 0; } +# endif +#else /* !CONFIG_TRACER_SNAPSHOT */ +static int trace_allocate_snapshot(struct trace_array *tr, int size) { ret= urn 0; } static inline void tracing_snapshot_instance(struct trace_array *tr) { } static inline int tracing_alloc_snapshot_instance(struct trace_array *tr) { return 0; } +static inline int tracing_arm_snapshot_locked(struct trace_array *tr) { re= turn -EBUSY; } static inline int tracing_arm_snapshot(struct trace_array *tr) { return 0;= } static inline void tracing_disarm_snapshot(struct trace_array *tr) { } -#endif +static inline free_snapshot(struct trace_array *tr) {} +static inline void tracing_snapshot_cond(struct trace_array *tr, void *con= d_data) +{ + WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snap= shot used"); +} +static inline void *tracing_cond_snapshot_data(struct trace_array *tr) +{ + return NULL; +} +static inline int tracing_snapshot_cond_enable(struct trace_array *tr, voi= d *cond_data, cond_update_fn_t update) +{ + return -ENODEV; +} +static inline int tracing_snapshot_cond_disable(struct trace_array *tr) +{ + return false; +} +static inline void print_snapshot_help(struct seq_file *m, struct trace_it= erator *iter) +{ + /* Should never be called */ + WARN_ONCE(1, "Snapshot print function called without snapshot configured"= ); +} +static inline int get_snapshot_map(struct trace_array *tr) { return 0; } +static inline void put_snapshot_map(struct trace_array *tr) { } +static inline void do_allocate_snapshot(const char *name) { } +static inline int register_snapshot_cmd(void) { return 0; } +#endif /* CONFIG_TRACER_SNAPSHOT */ =20 #ifdef CONFIG_PREEMPT_TRACER void tracer_preempt_on(unsigned long a0, unsigned long a1); diff --git a/kernel/trace/trace_snapshot.c b/kernel/trace/trace_snapshot.c new file mode 100644 index 000000000000..bda614db5ce1 --- /dev/null +++ b/kernel/trace/trace_snapshot.c @@ -0,0 +1,1068 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +#include /* COMMAND_LINE_SIZE */ + +#include "trace.h" + +/* Used if snapshot allocated at boot */ +static bool allocate_snapshot; +static bool snapshot_at_boot; + +static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata; +static int boot_snapshot_index; + +static int __init boot_alloc_snapshot(char *str) +{ + char *slot =3D boot_snapshot_info + boot_snapshot_index; + int left =3D sizeof(boot_snapshot_info) - boot_snapshot_index; + int ret; + + if (str[0] =3D=3D '=3D') { + str++; + if (strlen(str) >=3D left) + return -1; + + ret =3D snprintf(slot, left, "%s\t", str); + boot_snapshot_index +=3D ret; + } else { + allocate_snapshot =3D true; + /* We also need the main ring buffer expanded */ + trace_set_ring_buffer_expanded(NULL); + } + return 1; +} +__setup("alloc_snapshot", boot_alloc_snapshot); + + +static int __init boot_snapshot(char *str) +{ + snapshot_at_boot =3D true; + boot_alloc_snapshot(str); + return 1; +} +__setup("ftrace_boot_snapshot", boot_snapshot); + +static void tracing_snapshot_instance_cond(struct trace_array *tr, + void *cond_data) +{ + struct tracer *tracer =3D tr->current_trace; + unsigned long flags; + + if (in_nmi()) { + trace_array_puts(tr, "*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n"); + trace_array_puts(tr, "*** snapshot is being ignored ***\n"); + return; + } + + if (!tr->allocated_snapshot) { + trace_array_puts(tr, "*** SNAPSHOT NOT ALLOCATED ***\n"); + trace_array_puts(tr, "*** stopping trace here! ***\n"); + tracer_tracing_off(tr); + return; + } + + /* Note, snapshot can not be used when the tracer uses it */ + if (tracer->use_max_tr) { + trace_array_puts(tr, "*** LATENCY TRACER ACTIVE ***\n"); + trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n"); + return; + } + + if (tr->mapped) { + trace_array_puts(tr, "*** BUFFER MEMORY MAPPED ***\n"); + trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n"); + return; + } + + local_irq_save(flags); + update_max_tr(tr, current, smp_processor_id(), cond_data); + local_irq_restore(flags); +} + +void tracing_snapshot_instance(struct trace_array *tr) +{ + tracing_snapshot_instance_cond(tr, NULL); +} + +/** + * tracing_snapshot_cond - conditionally take a snapshot of the current bu= ffer. + * @tr: The tracing instance to snapshot + * @cond_data: The data to be tested conditionally, and possibly saved + * + * This is the same as tracing_snapshot() except that the snapshot is + * conditional - the snapshot will only happen if the + * cond_snapshot.update() implementation receiving the cond_data + * returns true, which means that the trace array's cond_snapshot + * update() operation used the cond_data to determine whether the + * snapshot should be taken, and if it was, presumably saved it along + * with the snapshot. + */ +void tracing_snapshot_cond(struct trace_array *tr, void *cond_data) +{ + tracing_snapshot_instance_cond(tr, cond_data); +} +EXPORT_SYMBOL_GPL(tracing_snapshot_cond); + +/** + * tracing_cond_snapshot_data - get the user data associated with a snapsh= ot + * @tr: The tracing instance + * + * When the user enables a conditional snapshot using + * tracing_snapshot_cond_enable(), the user-defined cond_data is saved + * with the snapshot. This accessor is used to retrieve it. + * + * Should not be called from cond_snapshot.update(), since it takes + * the tr->max_lock lock, which the code calling + * cond_snapshot.update() has already done. + * + * Returns the cond_data associated with the trace array's snapshot. + */ +void *tracing_cond_snapshot_data(struct trace_array *tr) +{ + void *cond_data =3D NULL; + + local_irq_disable(); + arch_spin_lock(&tr->max_lock); + + if (tr->cond_snapshot) + cond_data =3D tr->cond_snapshot->cond_data; + + arch_spin_unlock(&tr->max_lock); + local_irq_enable(); + + return cond_data; +} +EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data); + +/* resize @tr's buffer to the size of @size_tr's entries */ +int resize_buffer_duplicate_size(struct array_buffer *trace_buf, + struct array_buffer *size_buf, int cpu_id) +{ + int cpu, ret =3D 0; + + if (cpu_id =3D=3D RING_BUFFER_ALL_CPUS) { + for_each_tracing_cpu(cpu) { + ret =3D ring_buffer_resize(trace_buf->buffer, + per_cpu_ptr(size_buf->data, cpu)->entries, cpu); + if (ret < 0) + break; + per_cpu_ptr(trace_buf->data, cpu)->entries =3D + per_cpu_ptr(size_buf->data, cpu)->entries; + } + } else { + ret =3D ring_buffer_resize(trace_buf->buffer, + per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id); + if (ret =3D=3D 0) + per_cpu_ptr(trace_buf->data, cpu_id)->entries =3D + per_cpu_ptr(size_buf->data, cpu_id)->entries; + } + + return ret; +} + +int tracing_alloc_snapshot_instance(struct trace_array *tr) +{ + int order; + int ret; + + if (!tr->allocated_snapshot) { + + /* Make the snapshot buffer have the same order as main buffer */ + order =3D ring_buffer_subbuf_order_get(tr->array_buffer.buffer); + ret =3D ring_buffer_subbuf_order_set(tr->snapshot_buffer.buffer, order); + if (ret < 0) + return ret; + + /* allocate spare buffer */ + ret =3D resize_buffer_duplicate_size(&tr->snapshot_buffer, + &tr->array_buffer, RING_BUFFER_ALL_CPUS); + if (ret < 0) + return ret; + + tr->allocated_snapshot =3D true; + } + + return 0; +} + +void free_snapshot(struct trace_array *tr) +{ + /* + * We don't free the ring buffer. instead, resize it because + * The max_tr ring buffer has some state (e.g. ring->clock) and + * we want preserve it. + */ + ring_buffer_subbuf_order_set(tr->snapshot_buffer.buffer, 0); + ring_buffer_resize(tr->snapshot_buffer.buffer, 1, RING_BUFFER_ALL_CPUS); + trace_set_buffer_entries(&tr->snapshot_buffer, 1); + tracing_reset_online_cpus(&tr->snapshot_buffer); + tr->allocated_snapshot =3D false; +} + +int tracing_arm_snapshot_locked(struct trace_array *tr) +{ + int ret; + + lockdep_assert_held(&trace_types_lock); + + spin_lock(&tr->snapshot_trigger_lock); + if (tr->snapshot =3D=3D UINT_MAX || tr->mapped) { + spin_unlock(&tr->snapshot_trigger_lock); + return -EBUSY; + } + + tr->snapshot++; + spin_unlock(&tr->snapshot_trigger_lock); + + ret =3D tracing_alloc_snapshot_instance(tr); + if (ret) { + spin_lock(&tr->snapshot_trigger_lock); + tr->snapshot--; + spin_unlock(&tr->snapshot_trigger_lock); + } + + return ret; +} + +int tracing_arm_snapshot(struct trace_array *tr) +{ + guard(mutex)(&trace_types_lock); + return tracing_arm_snapshot_locked(tr); +} + +void tracing_disarm_snapshot(struct trace_array *tr) +{ + spin_lock(&tr->snapshot_trigger_lock); + if (!WARN_ON(!tr->snapshot)) + tr->snapshot--; + spin_unlock(&tr->snapshot_trigger_lock); +} + +/** + * tracing_snapshot_alloc - allocate and take a snapshot of the current bu= ffer. + * + * This is similar to tracing_snapshot(), but it will allocate the + * snapshot buffer if it isn't already allocated. Use this only + * where it is safe to sleep, as the allocation may sleep. + * + * This causes a swap between the snapshot buffer and the current live + * tracing buffer. You can use this to take snapshots of the live + * trace when some condition is triggered, but continue to trace. + */ +void tracing_snapshot_alloc(void) +{ + int ret; + + ret =3D tracing_alloc_snapshot(); + if (ret < 0) + return; + + tracing_snapshot(); +} +EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); + +/** + * tracing_snapshot_cond_enable - enable conditional snapshot for an insta= nce + * @tr: The tracing instance + * @cond_data: User data to associate with the snapshot + * @update: Implementation of the cond_snapshot update function + * + * Check whether the conditional snapshot for the given instance has + * already been enabled, or if the current tracer is already using a + * snapshot; if so, return -EBUSY, else create a cond_snapshot and + * save the cond_data and update function inside. + * + * Returns 0 if successful, error otherwise. + */ +int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, + cond_update_fn_t update) +{ + struct cond_snapshot *cond_snapshot __free(kfree) =3D + kzalloc(sizeof(*cond_snapshot), GFP_KERNEL); + int ret; + + if (!cond_snapshot) + return -ENOMEM; + + cond_snapshot->cond_data =3D cond_data; + cond_snapshot->update =3D update; + + guard(mutex)(&trace_types_lock); + + if (tr->current_trace->use_max_tr) + return -EBUSY; + + /* + * The cond_snapshot can only change to NULL without the + * trace_types_lock. We don't care if we race with it going + * to NULL, but we want to make sure that it's not set to + * something other than NULL when we get here, which we can + * do safely with only holding the trace_types_lock and not + * having to take the max_lock. + */ + if (tr->cond_snapshot) + return -EBUSY; + + ret =3D tracing_arm_snapshot_locked(tr); + if (ret) + return ret; + + local_irq_disable(); + arch_spin_lock(&tr->max_lock); + tr->cond_snapshot =3D no_free_ptr(cond_snapshot); + arch_spin_unlock(&tr->max_lock); + local_irq_enable(); + + return 0; +} +EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable); + +/** + * tracing_snapshot_cond_disable - disable conditional snapshot for an ins= tance + * @tr: The tracing instance + * + * Check whether the conditional snapshot for the given instance is + * enabled; if so, free the cond_snapshot associated with it, + * otherwise return -EINVAL. + * + * Returns 0 if successful, error otherwise. + */ +int tracing_snapshot_cond_disable(struct trace_array *tr) +{ + int ret =3D 0; + + local_irq_disable(); + arch_spin_lock(&tr->max_lock); + + if (!tr->cond_snapshot) + ret =3D -EINVAL; + else { + kfree(tr->cond_snapshot); + tr->cond_snapshot =3D NULL; + } + + arch_spin_unlock(&tr->max_lock); + local_irq_enable(); + + tracing_disarm_snapshot(tr); + + return ret; +} +EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); + +#ifdef CONFIG_TRACER_MAX_TRACE +static const struct file_operations tracing_max_lat_fops; + +#ifdef LATENCY_FS_NOTIFY + +static struct workqueue_struct *fsnotify_wq; + +static void latency_fsnotify_workfn(struct work_struct *work) +{ + struct trace_array *tr =3D container_of(work, struct trace_array, + fsnotify_work); + fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY); +} + +static void latency_fsnotify_workfn_irq(struct irq_work *iwork) +{ + struct trace_array *tr =3D container_of(iwork, struct trace_array, + fsnotify_irqwork); + queue_work(fsnotify_wq, &tr->fsnotify_work); +} + +void trace_create_maxlat_file(struct trace_array *tr, + struct dentry *d_tracer) +{ + INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn); + init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq); + tr->d_max_latency =3D trace_create_file("tracing_max_latency", + TRACE_MODE_WRITE, + d_tracer, tr, + &tracing_max_lat_fops); +} + +__init static int latency_fsnotify_init(void) +{ + fsnotify_wq =3D alloc_workqueue("tr_max_lat_wq", + WQ_UNBOUND | WQ_HIGHPRI, 0); + if (!fsnotify_wq) { + pr_err("Unable to allocate tr_max_lat_wq\n"); + return -ENOMEM; + } + return 0; +} + +late_initcall_sync(latency_fsnotify_init); + +void latency_fsnotify(struct trace_array *tr) +{ + if (!fsnotify_wq) + return; + /* + * We cannot call queue_work(&tr->fsnotify_work) from here because it's + * possible that we are called from __schedule() or do_idle(), which + * could cause a deadlock. + */ + irq_work_queue(&tr->fsnotify_irqwork); +} + +#else /* !LATENCY_FS_NOTIFY */ + +#endif + +/* + * Copy the new maximum trace into the separate maximum-trace + * structure. (this way the maximum trace is permanently saved, + * for later retrieval via /sys/kernel/tracing/tracing_max_latency) + */ +static void +__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) +{ + struct array_buffer *trace_buf =3D &tr->array_buffer; + struct array_buffer *max_buf =3D &tr->snapshot_buffer; + struct trace_array_cpu *data =3D per_cpu_ptr(trace_buf->data, cpu); + struct trace_array_cpu *max_data =3D per_cpu_ptr(max_buf->data, cpu); + + max_buf->cpu =3D cpu; + max_buf->time_start =3D data->preempt_timestamp; + + max_data->saved_latency =3D tr->max_latency; + max_data->critical_start =3D data->critical_start; + max_data->critical_end =3D data->critical_end; + + strscpy(max_data->comm, tsk->comm); + max_data->pid =3D tsk->pid; + /* + * If tsk =3D=3D current, then use current_uid(), as that does not use + * RCU. The irq tracer can be called out of RCU scope. + */ + if (tsk =3D=3D current) + max_data->uid =3D current_uid(); + else + max_data->uid =3D task_uid(tsk); + + max_data->nice =3D tsk->static_prio - 20 - MAX_RT_PRIO; + max_data->policy =3D tsk->policy; + max_data->rt_priority =3D tsk->rt_priority; + + /* record this tasks comm */ + tracing_record_cmdline(tsk); + latency_fsnotify(tr); +} + +/** + * update_max_tr - snapshot all trace buffers from global_trace to max_tr + * @tr: tracer + * @tsk: the task with the latency + * @cpu: The cpu that initiated the trace. + * @cond_data: User data associated with a conditional snapshot + * + * Flip the buffers between the @tr and the max_tr and record information + * about which task was the cause of this latency. + */ +void +update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu, + void *cond_data) +{ + if (tr->stop_count) + return; + + WARN_ON_ONCE(!irqs_disabled()); + + if (!tr->allocated_snapshot) { + /* Only the nop tracer should hit this when disabling */ + WARN_ON_ONCE(tr->current_trace !=3D &nop_trace); + return; + } + + arch_spin_lock(&tr->max_lock); + + /* Inherit the recordable setting from array_buffer */ + if (ring_buffer_record_is_set_on(tr->array_buffer.buffer)) + ring_buffer_record_on(tr->snapshot_buffer.buffer); + else + ring_buffer_record_off(tr->snapshot_buffer.buffer); + + if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data)) { + arch_spin_unlock(&tr->max_lock); + return; + } + + swap(tr->array_buffer.buffer, tr->snapshot_buffer.buffer); + + __update_max_tr(tr, tsk, cpu); + + arch_spin_unlock(&tr->max_lock); + + /* Any waiters on the old snapshot buffer need to wake up */ + ring_buffer_wake_waiters(tr->array_buffer.buffer, RING_BUFFER_ALL_CPUS); +} + +/** + * update_max_tr_single - only copy one trace over, and reset the rest + * @tr: tracer + * @tsk: task with the latency + * @cpu: the cpu of the buffer to copy. + * + * Flip the trace of a single CPU buffer between the @tr and the max_tr. + */ +void +update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int = cpu) +{ + int ret; + + if (tr->stop_count) + return; + + WARN_ON_ONCE(!irqs_disabled()); + if (!tr->allocated_snapshot) { + /* Only the nop tracer should hit this when disabling */ + WARN_ON_ONCE(tr->current_trace !=3D &nop_trace); + return; + } + + arch_spin_lock(&tr->max_lock); + + ret =3D ring_buffer_swap_cpu(tr->snapshot_buffer.buffer, tr->array_buffer= .buffer, cpu); + + if (ret =3D=3D -EBUSY) { + /* + * We failed to swap the buffer due to a commit taking + * place on this CPU. We fail to record, but we reset + * the max trace buffer (no one writes directly to it) + * and flag that it failed. + * Another reason is resize is in progress. + */ + trace_array_printk_buf(tr->snapshot_buffer.buffer, _THIS_IP_, + "Failed to swap buffers due to commit or resize in progress\n"); + } + + WARN_ON_ONCE(ret && ret !=3D -EAGAIN && ret !=3D -EBUSY); + + __update_max_tr(tr, tsk, cpu); + arch_spin_unlock(&tr->max_lock); +} + +#endif /* CONFIG_TRACER_MAX_TRACE */ + +static void show_snapshot_main_help(struct seq_file *m) +{ + seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n" + "# echo 1 > snapshot : Allocates snapshot buffer, if not already all= ocated.\n" + "# Takes a snapshot of the main buffer.\n" + "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate= or free)\n" + "# (Doesn't have to be '2' works with any numbe= r that\n" + "# is not a '0' or '1')\n"); +} + +static void show_snapshot_percpu_help(struct seq_file *m) +{ + seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n"); +#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP + seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not alre= ady allocated.\n" + "# Takes a snapshot of the main buffer for this= cpu.\n"); +#else + seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n" + "# Must use main snapshot file to allocate.\n"); +#endif + seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but= does not allocate)\n" + "# (Doesn't have to be '2' works with any numbe= r that\n" + "# is not a '0' or '1')\n"); +} + +void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) +{ + if (iter->tr->allocated_snapshot) + seq_puts(m, "#\n# * Snapshot is allocated *\n#\n"); + else + seq_puts(m, "#\n# * Snapshot is freed *\n#\n"); + + seq_puts(m, "# Snapshot commands:\n"); + if (iter->cpu_file =3D=3D RING_BUFFER_ALL_CPUS) + show_snapshot_main_help(m); + else + show_snapshot_percpu_help(m); +} + +static int tracing_snapshot_open(struct inode *inode, struct file *file) +{ + struct trace_array *tr =3D inode->i_private; + struct trace_iterator *iter; + struct seq_file *m; + int ret; + + ret =3D tracing_check_open_get_tr(tr); + if (ret) + return ret; + + if (file->f_mode & FMODE_READ) { + iter =3D __tracing_open(inode, file, true); + if (IS_ERR(iter)) + ret =3D PTR_ERR(iter); + } else { + /* Writes still need the seq_file to hold the private data */ + ret =3D -ENOMEM; + m =3D kzalloc(sizeof(*m), GFP_KERNEL); + if (!m) + goto out; + iter =3D kzalloc(sizeof(*iter), GFP_KERNEL); + if (!iter) { + kfree(m); + goto out; + } + ret =3D 0; + + iter->tr =3D tr; + iter->array_buffer =3D &tr->snapshot_buffer; + iter->cpu_file =3D tracing_get_cpu(inode); + m->private =3D iter; + file->private_data =3D m; + } +out: + if (ret < 0) + trace_array_put(tr); + + return ret; +} + +static void tracing_swap_cpu_buffer(void *tr) +{ + update_max_tr_single((struct trace_array *)tr, current, smp_processor_id(= )); +} + +static ssize_t +tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t = cnt, + loff_t *ppos) +{ + struct seq_file *m =3D filp->private_data; + struct trace_iterator *iter =3D m->private; + struct trace_array *tr =3D iter->tr; + unsigned long val; + int ret; + + ret =3D tracing_update_buffers(tr); + if (ret < 0) + return ret; + + ret =3D kstrtoul_from_user(ubuf, cnt, 10, &val); + if (ret) + return ret; + + guard(mutex)(&trace_types_lock); + + if (tr->current_trace->use_max_tr) + return -EBUSY; + + local_irq_disable(); + arch_spin_lock(&tr->max_lock); + if (tr->cond_snapshot) + ret =3D -EBUSY; + arch_spin_unlock(&tr->max_lock); + local_irq_enable(); + if (ret) + return ret; + + switch (val) { + case 0: + if (iter->cpu_file !=3D RING_BUFFER_ALL_CPUS) + return -EINVAL; + if (tr->allocated_snapshot) + free_snapshot(tr); + break; + case 1: +/* Only allow per-cpu swap if the ring buffer supports it */ +#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP + if (iter->cpu_file !=3D RING_BUFFER_ALL_CPUS) + return -EINVAL; +#endif + if (tr->allocated_snapshot) + ret =3D resize_buffer_duplicate_size(&tr->snapshot_buffer, + &tr->array_buffer, iter->cpu_file); + + ret =3D tracing_arm_snapshot_locked(tr); + if (ret) + return ret; + + /* Now, we're going to swap */ + if (iter->cpu_file =3D=3D RING_BUFFER_ALL_CPUS) { + local_irq_disable(); + update_max_tr(tr, current, smp_processor_id(), NULL); + local_irq_enable(); + } else { + smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer, + (void *)tr, 1); + } + tracing_disarm_snapshot(tr); + break; + default: + if (tr->allocated_snapshot) { + if (iter->cpu_file =3D=3D RING_BUFFER_ALL_CPUS) + tracing_reset_online_cpus(&tr->snapshot_buffer); + else + tracing_reset_cpu(&tr->snapshot_buffer, iter->cpu_file); + } + break; + } + + if (ret >=3D 0) { + *ppos +=3D cnt; + ret =3D cnt; + } + + return ret; +} + +static int tracing_snapshot_release(struct inode *inode, struct file *file) +{ + struct seq_file *m =3D file->private_data; + int ret; + + ret =3D tracing_release(inode, file); + + if (file->f_mode & FMODE_READ) + return ret; + + /* If write only, the seq_file is just a stub */ + if (m) + kfree(m->private); + kfree(m); + + return 0; +} + +static int snapshot_raw_open(struct inode *inode, struct file *filp) +{ + struct ftrace_buffer_info *info; + int ret; + + /* The following checks for tracefs lockdown */ + ret =3D tracing_buffers_open(inode, filp); + if (ret < 0) + return ret; + + info =3D filp->private_data; + + if (info->iter.trace->use_max_tr) { + tracing_buffers_release(inode, filp); + return -EBUSY; + } + + info->iter.snapshot =3D true; + info->iter.array_buffer =3D &info->iter.tr->snapshot_buffer; + + return ret; +} + +const struct file_operations snapshot_fops =3D { + .open =3D tracing_snapshot_open, + .read =3D seq_read, + .write =3D tracing_snapshot_write, + .llseek =3D tracing_lseek, + .release =3D tracing_snapshot_release, +}; + +const struct file_operations snapshot_raw_fops =3D { + .open =3D snapshot_raw_open, + .read =3D tracing_buffers_read, + .release =3D tracing_buffers_release, + .splice_read =3D tracing_buffers_splice_read, +}; + +#ifdef CONFIG_TRACER_MAX_TRACE +static ssize_t +tracing_max_lat_read(struct file *filp, char __user *ubuf, + size_t cnt, loff_t *ppos) +{ + struct trace_array *tr =3D filp->private_data; + + return tracing_nsecs_read(&tr->max_latency, ubuf, cnt, ppos); +} + +static ssize_t +tracing_max_lat_write(struct file *filp, const char __user *ubuf, + size_t cnt, loff_t *ppos) +{ + struct trace_array *tr =3D filp->private_data; + + return tracing_nsecs_write(&tr->max_latency, ubuf, cnt, ppos); +} + +static const struct file_operations tracing_max_lat_fops =3D { + .open =3D tracing_open_generic_tr, + .read =3D tracing_max_lat_read, + .write =3D tracing_max_lat_write, + .llseek =3D generic_file_llseek, + .release =3D tracing_release_generic_tr, +}; +#endif /* CONFIG_TRACER_MAX_TRACE */ + +int get_snapshot_map(struct trace_array *tr) +{ + int err =3D 0; + + /* + * Called with mmap_lock held. lockdep would be unhappy if we would now + * take trace_types_lock. Instead use the specific + * snapshot_trigger_lock. + */ + spin_lock(&tr->snapshot_trigger_lock); + + if (tr->snapshot || tr->mapped =3D=3D UINT_MAX) + err =3D -EBUSY; + else + tr->mapped++; + + spin_unlock(&tr->snapshot_trigger_lock); + + /* Wait for update_max_tr() to observe iter->tr->mapped */ + if (tr->mapped =3D=3D 1) + synchronize_rcu(); + + return err; + +} +void put_snapshot_map(struct trace_array *tr) +{ + spin_lock(&tr->snapshot_trigger_lock); + if (!WARN_ON(!tr->mapped)) + tr->mapped--; + spin_unlock(&tr->snapshot_trigger_lock); +} + +#ifdef CONFIG_DYNAMIC_FTRACE +static void +ftrace_snapshot(unsigned long ip, unsigned long parent_ip, + struct trace_array *tr, struct ftrace_probe_ops *ops, + void *data) +{ + tracing_snapshot_instance(tr); +} + +static void +ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, + struct trace_array *tr, struct ftrace_probe_ops *ops, + void *data) +{ + struct ftrace_func_mapper *mapper =3D data; + long *count =3D NULL; + + if (mapper) + count =3D (long *)ftrace_func_mapper_find_ip(mapper, ip); + + if (count) { + + if (*count <=3D 0) + return; + + (*count)--; + } + + tracing_snapshot_instance(tr); +} + +static int +ftrace_snapshot_print(struct seq_file *m, unsigned long ip, + struct ftrace_probe_ops *ops, void *data) +{ + struct ftrace_func_mapper *mapper =3D data; + long *count =3D NULL; + + seq_printf(m, "%ps:", (void *)ip); + + seq_puts(m, "snapshot"); + + if (mapper) + count =3D (long *)ftrace_func_mapper_find_ip(mapper, ip); + + if (count) + seq_printf(m, ":count=3D%ld\n", *count); + else + seq_puts(m, ":unlimited\n"); + + return 0; +} + +static int +ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr, + unsigned long ip, void *init_data, void **data) +{ + struct ftrace_func_mapper *mapper =3D *data; + + if (!mapper) { + mapper =3D allocate_ftrace_func_mapper(); + if (!mapper) + return -ENOMEM; + *data =3D mapper; + } + + return ftrace_func_mapper_add_ip(mapper, ip, init_data); +} + +static void +ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr, + unsigned long ip, void *data) +{ + struct ftrace_func_mapper *mapper =3D data; + + if (!ip) { + if (!mapper) + return; + free_ftrace_func_mapper(mapper, NULL); + return; + } + + ftrace_func_mapper_remove_ip(mapper, ip); +} + +static struct ftrace_probe_ops snapshot_probe_ops =3D { + .func =3D ftrace_snapshot, + .print =3D ftrace_snapshot_print, +}; + +static struct ftrace_probe_ops snapshot_count_probe_ops =3D { + .func =3D ftrace_count_snapshot, + .print =3D ftrace_snapshot_print, + .init =3D ftrace_snapshot_init, + .free =3D ftrace_snapshot_free, +}; + +static int +ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash = *hash, + char *glob, char *cmd, char *param, int enable) +{ + struct ftrace_probe_ops *ops; + void *count =3D (void *)-1; + char *number; + int ret; + + if (!tr) + return -ENODEV; + + /* hash funcs only work with set_ftrace_filter */ + if (!enable) + return -EINVAL; + + ops =3D param ? &snapshot_count_probe_ops : &snapshot_probe_ops; + + if (glob[0] =3D=3D '!') { + ret =3D unregister_ftrace_function_probe_func(glob+1, tr, ops); + if (!ret) + tracing_disarm_snapshot(tr); + + return ret; + } + + if (!param) + goto out_reg; + + number =3D strsep(¶m, ":"); + + if (!strlen(number)) + goto out_reg; + + /* + * We use the callback data field (which is a pointer) + * as our counter. + */ + ret =3D kstrtoul(number, 0, (unsigned long *)&count); + if (ret) + return ret; + + out_reg: + ret =3D tracing_arm_snapshot(tr); + if (ret < 0) + return ret; + + ret =3D register_ftrace_function_probe(glob, tr, ops, count); + if (ret < 0) + tracing_disarm_snapshot(tr); + + return ret < 0 ? ret : 0; +} + +static struct ftrace_func_command ftrace_snapshot_cmd =3D { + .name =3D "snapshot", + .func =3D ftrace_trace_snapshot_callback, +}; + +__init int register_snapshot_cmd(void) +{ + return register_ftrace_command(&ftrace_snapshot_cmd); +} +#endif /* CONFIG_DYNAMIC_FTRACE */ + +int trace_allocate_snapshot(struct trace_array *tr, int size) +{ + int ret; + + /* Fix mapped buffer trace arrays do not have snapshot buffers */ + if (tr->range_addr_start) + return 0; + + /* allocate_snapshot can only be true during system boot */ + ret =3D allocate_trace_buffer(tr, &tr->snapshot_buffer, + allocate_snapshot ? size : 1); + if (ret < 0) + return -ENOMEM; + + tr->allocated_snapshot =3D allocate_snapshot; + + allocate_snapshot =3D false; + return 0; +} + +__init static bool tr_needs_alloc_snapshot(const char *name) +{ + char *test; + int len =3D strlen(name); + bool ret; + + if (!boot_snapshot_index) + return false; + + if (strncmp(name, boot_snapshot_info, len) =3D=3D 0 && + boot_snapshot_info[len] =3D=3D '\t') + return true; + + test =3D kmalloc(strlen(name) + 3, GFP_KERNEL); + if (!test) + return false; + + sprintf(test, "\t%s\t", name); + ret =3D strstr(boot_snapshot_info, test) =3D=3D NULL; + kfree(test); + return ret; +} + +__init void do_allocate_snapshot(const char *name) +{ + if (!tr_needs_alloc_snapshot(name)) + return; + + /* + * When allocate_snapshot is set, the next call to + * allocate_trace_buffers() (called by trace_array_get_by_name()) + * will allocate the snapshot buffer. That will also clear + * this flag. + */ + allocate_snapshot =3D true; +} + +void __init ftrace_boot_snapshot(void) +{ + struct trace_array *tr; + + if (!snapshot_at_boot) + return; + + list_for_each_entry(tr, &ftrace_trace_arrays, list) { + if (!tr->allocated_snapshot) + continue; + + tracing_snapshot_instance(tr); + trace_array_puts(tr, "** Boot snapshot taken **\n"); + } +} + --=20 2.51.0