From nobody Wed Feb 11 07:48:32 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 9C8512741C0; Sun, 8 Feb 2026 03:24:06 +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=1770521046; cv=none; b=f7/sdTy+qtCvv/vxjxpHmOTPlks5lNVOY5ZtWSpplhT0B6ZeneuF8U5n83R291gM/BwVyRwXfyPCO3ckAh1rBsbmJ2XIBcBdE6Bw2xrIEly7m/1GxF/46y075yIMXaPck7tAcV4RVANKiSAGrhnYnin5+HclN0QKsafFT5QgMe4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521046; c=relaxed/simple; bh=P2+u/vNHwEzn7ODM4Pi1LxTEHDnekqxDaeXdxpmwQ2o=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=Kg2Tqx7mi5pHIFjzzDWf9UCeqNriCgpmIdcjanGaUAbUCpB+/x3MkgJoNcZ9A2JGirzAoJfruNgS3qDbMN+AmYUkzKqfqQyLVuL2yym7P9/1TkKlP9IdLPiovjupWYIxhJHxkH8cBk101qxXm4nmyukXIRdWgPSpQPHq0vX4hxU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=W+VwhFqK; 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="W+VwhFqK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4EE06C19424; Sun, 8 Feb 2026 03:24:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521046; bh=P2+u/vNHwEzn7ODM4Pi1LxTEHDnekqxDaeXdxpmwQ2o=; h=Date:From:To:Cc:Subject:References:From; b=W+VwhFqKigWepw8h0hZv0Vs/oeE/5YfMO3/Z4Tj8St94mstZxfsZnvyNHB+Hsc1P9 yIMNp1Zx7w+PmbipRPYLLuJw8fJM56hHeoVRiaE/l2cvWblLnL5iKDWVCan70GcKf3 W8TrKKdZSCoXfuOixqpbRzv+Vx1C0gcnrh0FiCRvizxnW4qRaF5AQv9PxFBJgd9lqM 3hHrOV3rAHY6Ie8WviTdHwy6k/unAKQEm46xNaPjFmxmSaxE0LqqvGQ6ihLFEhDI76 OsP7CZrJOv1c3kNjvjO7w8FakdF72CYgA49xg4vDohgWuzJaC1Utj2zXHZ7rdgawuK JfvgTiy/ppJng== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPl-0000000AObG-2dnE; Sat, 07 Feb 2026 22:24:49 -0500 Message-ID: <20260208032449.483690153@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:18 -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 v2 01/10] tracing: Make tracing_disabled global for tracing system References: <20260208032417.262341179@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 d02c4004c718..1ff40c88e75c 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 @@ -3423,7 +3423,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 */ @@ -4765,11 +4765,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. @@ -7609,7 +7604,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))) @@ -7689,7 +7684,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 31fb137e1c66..433705bef480 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -657,6 +657,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); @@ -668,7 +670,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 89d2740f7bb5..061658518605 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -2083,7 +2083,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 Wed Feb 11 07:48:32 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 95E9F1DFFD; Sun, 8 Feb 2026 03:24:06 +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=1770521046; cv=none; b=gHvc2MWNHGtotCJ71sp5YK6HwrlBZl+l3jqx63aHMws/fU6DYwzyq7JKz9ER0ahDe2BCGcnFBVrfB53qo2+JSlzP8Iry/F+bdlf2vBYcc0xv8sbhIDuK6Shg3HSwaooXX3etX8OzMe/HbuSDtkku+mPSWDtemOzPVi2EOoCPsDU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521046; c=relaxed/simple; bh=WjgPtrJ5wSDCRofEA70oMGklis624z6PXHvh0XLUpVk=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=NtI334XHhg3AHqnQOy1BewCCIrcstdJV7n6u+M/lpd6th6/hM/oqck6YTsbcWlRRJXfAyNZ7krLgj1YMV9HAzscoaqyJSxgO3O+46v1CRBs5QQMQuKXb6HlgJAAZjGUzrQwIvWCg0SCW60h0SfxfZgkMAcMf/sXjiaMLyoCLdXI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hBBeaq0r; 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="hBBeaq0r" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74ABAC19425; Sun, 8 Feb 2026 03:24:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521046; bh=WjgPtrJ5wSDCRofEA70oMGklis624z6PXHvh0XLUpVk=; h=Date:From:To:Cc:Subject:References:From; b=hBBeaq0rDH15psyqB+6u5rVw4R+Wa4+uxeLgTPZ02sqRZbyDOQBjQ65g8Hhfx8A/o OZqUQWbxvxRThcuE8snK+NaryxLXmYdqXmcc9yhzmSNDtMweg4PWD6PxFozc1LuzfG 4zXNN8FGwFDGwWQ2olg0icISFhquhnPErwLoNg/DzNRfBSsFAMYOFDW1bAXMn0cMtP MERruYh8SBWJA1W+cUvf6U2eVOaLqN2eXrdYbsoP95QLnHjcYczak5wIBzufBsgpZw xu4N3lQ2frpsSIUpf3Lxk4rXcAoOYYwtQH4vPjbx2Jy1/9CPWrlQsnrWEuJglk8opK rr6xell56GW5Q== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPl-0000000AObk-3KPP; Sat, 07 Feb 2026 22:24:49 -0500 Message-ID: <20260208032449.648932796@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:19 -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 v2 02/10] tracing: Make tracing_selftest_running global to the tracing subsystem References: <20260208032417.262341179@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 1ff40c88e75c..f040ee4fe101 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 433705bef480..19cffc7b5852 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -863,6 +863,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 @@ -875,6 +876,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 Wed Feb 11 07:48:32 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 BB94F293C4E; Sun, 8 Feb 2026 03:24:06 +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=1770521046; cv=none; b=suzgKSH4PDCizj6Y/nmg2PWDSX0C/1Om4IirMpLwWv8wj67NdJ2y/s22gADkP8bQEkKGymPKcmfjJGNfjIRKRm6GFfRB1FZeg1ASYazqUTNbWr0zbfuIiB2eYBF+fHMHqdcFTC4wPLoBQdVngE6SNjXOjcnGgx9p8lgukUkP3ww= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521046; c=relaxed/simple; bh=Ss/0hHnK8DPO0iCJwrIIKNzPNkD50min9pGCh2rRKFc=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=izkTqeBnyY/qKn/v1kLDXwQkFBrZZNYbn4284U3eAFPZOc78T897QGk0/PwM0HzglB/RsoiMZwCGn8O9Y1jerR9y5vkR6VNpZVztYO366l5gJgSJayP6Qc7QUpAakIsVd8RKTIDSzp20r1u4sheYzCt/jD8rjvhze2mqjafBv0o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QJpV1PcN; 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="QJpV1PcN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 991B6C2BC9E; Sun, 8 Feb 2026 03:24:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521046; bh=Ss/0hHnK8DPO0iCJwrIIKNzPNkD50min9pGCh2rRKFc=; h=Date:From:To:Cc:Subject:References:From; b=QJpV1PcNp7cJZ7SKHk+kw3T7AUcxzikb5uVfE6buEUUs1yL7C4Hr+2bcGBaCIhihV OPKN22zKd9ugsp5ZSIwdXN/O/LE9ClpbMhnoyrA4ELGcpCVvuHljmw9WXidrmoCJbd fANaUGkKTE7K/06DI/A7E6JKL+CtVoLkKvfuAKRBgUwixwOPSUo/EXBp2+kM/eb/oK R+LGeulE7Hm5WcII51QxC2LXQKa4W1kpq2R3TTrhANHcYuPel3adI+hE4uvWPEoz1z 0VmRwOxH085BUQaWExw5j9qjyUufLJ+WgKOTxluP5BdZOYKCB02ndsAKLQ+JhZEO3s 8Zpnr0zi+aVhQ== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPl-0000000AOcE-415G; Sat, 07 Feb 2026 22:24:49 -0500 Message-ID: <20260208032449.813550600@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:20 -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 v2 03/10] tracing: Move __trace_buffer_{un}lock_*() functions to trace.h References: <20260208032417.262341179@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 f040ee4fe101..55cd0c774886 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 19cffc7b5852..c2beabe96952 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -1568,6 +1568,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 Wed Feb 11 07:48:32 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 C90C92BEFEB; Sun, 8 Feb 2026 03:24:06 +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=1770521046; cv=none; b=m1KLvdP2783ziAGYj+sTfuZs+gHBsU04WBoN+t8+oFGXvvYt4adcOIBZcoZfTesfM7ilAGqeO3d1ARIxd8sblrcZTMJX8MWo+N6l+FWapIdafItXnabawsHdKUm2jkMV8q1mSwL7k67GDpkTL5fkroK6i7a3dWtS4FLn4h4rC4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521046; c=relaxed/simple; bh=PkfKtA879ZBp3CTU0oytJ++8l4oi1boK/tfiRxrrp5c=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=lPszXiZnadM8zXJUSasDjRHUe2SkkGVFa7mW7/P1KLDCAkwn9/1RJqZVJ0K3itv9BXRBkAMSMI9rAsMr0sS1ez3ZgrCvzyl9IWVpzgKMAhYjcW43HgtdfK1uf1ehKsEJlWT90skXWzVn16xQrtAuF+6p+hk3GSjpme7l3HI9BpQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rybf3fIr; 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="rybf3fIr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A62F8C116C6; Sun, 8 Feb 2026 03:24:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521046; bh=PkfKtA879ZBp3CTU0oytJ++8l4oi1boK/tfiRxrrp5c=; h=Date:From:To:Cc:Subject:References:From; b=rybf3fIryO8/8Bswj9SGvLm6yps3lXcEgpcr1Git12UwbO87U0VE7LcJ0ahn4Ej92 XT2SbgCOUnmRFLzVRzKSNgNzxkijJVUvMch2NRZcFHCjVNj+3qd1KvgrJz36/4v5kq JRmLd6+fZZS+egIUDseSDVC6eZKB87pcGKJ2PLyfFMxi6MqUwki7Z2ZfiE7AYKONvu Sax352deH+Fz+6RHGseK2U9WVrQ0gVHxeY3MQ5auFUFDPljUITI5OAr4OEKjhQoet9 kKlqnk/CFc/AAv98g5ka6I+A5hDAYwRTxDdH4kl20a0bjg3lpYo5XZFojHKhe2+dEW Q+vwljlqp5MAA== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPm-0000000AOci-0W6H; Sat, 07 Feb 2026 22:24:50 -0500 Message-ID: <20260208032449.974218132@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:21 -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 v2 04/10] tracing: Move ftrace_trace_stack() out of trace.c and into trace.h References: <20260208032417.262341179@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 55cd0c774886..a515b5241391 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) @@ -2964,10 +2938,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; @@ -3050,17 +3024,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 c2beabe96952..605ee23f3262 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -2279,6 +2279,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 Wed Feb 11 07:48:32 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 F096D2C11D1; Sun, 8 Feb 2026 03:24:06 +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=1770521047; cv=none; b=uYXq89k5cYFu5uRLDaO5ax+ExhiJAtK1Yn9ZbtnyROFOUYRGn8IvoD7BDoMnYSgJvG9wHNnFQvw6jpFeOx2JYrP8bNzm2gHU8DB4V10syHYcDt3GSWmhKBpM08bsXCuth3YidH7Vsito1i6B/J476/GcmTmua3bNb6ZYYUfgdxc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521047; c=relaxed/simple; bh=ofZYtIuKZZzoDoktKHjlNYL9T3iTkdESp5qanyFNzIM=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=d6Nd0fOIF9pi3CnK9IE1af2cN0dpLtzgQWzsXepfbaIOP6AlglyP5lbG8uMItK1ZFF9CO6bWL8NNnik+mEEP6kTCGygv1IDhAEagGHHC71voVdbhi6c3F32f7omfbg4pR6wBlan/I+6AdnXkwxvGY6zIe1qZZdvOPfjN+I2gPvU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aE57g/Uv; 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="aE57g/Uv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF069C4CEF7; Sun, 8 Feb 2026 03:24:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521046; bh=ofZYtIuKZZzoDoktKHjlNYL9T3iTkdESp5qanyFNzIM=; h=Date:From:To:Cc:Subject:References:From; b=aE57g/UvywN9NEb3lOuiaZAKvI5p/ut5r7rzvcNLabTLlKagL4t2QoNIynVKNCn+T uXBIgUFnU9HHb1Y6dvnus393za5kupzoTLN5KUlNxcCP/nz4v3/h358NpgCzUk/OyQ BMtOi5UHCDueCji9eDu//DBAjpCNDKLHFFGUWED4HWdUX6dmp3/EbZyFqlCSaSGBdq LZoF8RiPgydV27TbnOtuJODOCGQqzU5VYyq1PIjeCaZp9Ph5N2pmrm6IgtW4HD5DmI p9pDpHZTFDvTA3IKL1/q1Xf+vkpyquDGTE6dlArKkY9YQzZcRsxBH4jHTZOQyxz9Sw MvGoR+gOFaUrQ== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPm-0000000AOdC-1Feg; Sat, 07 Feb 2026 22:24:50 -0500 Message-ID: <20260208032450.144525891@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:22 -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 v2 05/10] tracing: Make printk_trace global for tracing system References: <20260208032417.262341179@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 a515b5241391..4a73822e2603 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 605ee23f3262..921e4daa2825 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -482,6 +482,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 Wed Feb 11 07:48:32 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 4D64B2C3259; Sun, 8 Feb 2026 03:24:07 +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=1770521047; cv=none; b=UoC0PY9MxW2YA/iFvIwvG/Uyan2i+bDZROa4p76FkXlfJX/X6m0pdCfl50oydCzWlleTiitMX8Ri9ghDccxEucNmMpLt5NY1y4q2IONNjJwwDtlFnPVdUhO5l+Tcn+K8nrmWBZnuvoLQmp1IOflce7SSbo6cpXFgx5C57ZYaQt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521047; c=relaxed/simple; bh=3Xwg0vZ1urflHR8mszNbLj+Cni7vlXl8An6HdvjgI7c=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=cuB/IY0GNX1ktxwxet5/YXsE9ZL7Kr6CjryHHkkCZbTMdg4q6nJXCnPtWLoYoMoSgPNWIy7dQOPVuSp3WivQHFxpEe8IuX1sneE7qNrIfJsCDrHBp9QYVD835anJf2aoZw30YYoCom0x/7NE43h4ZDJWFwiFk0/SUU0JszA/pIA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lzGwptbS; 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="lzGwptbS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1DF95C2BCB5; Sun, 8 Feb 2026 03:24:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521047; bh=3Xwg0vZ1urflHR8mszNbLj+Cni7vlXl8An6HdvjgI7c=; h=Date:From:To:Cc:Subject:References:From; b=lzGwptbS7WXnnGwBMxSsDz3Ih++eoFbR+Dia6q2KmRUeapqO8FvzNEq5fF0n/rpSp 7baq3bYnVb1EBBuXuoN8CHLwtMas7ikHPDDSl8RDf2aKpBOrDmOV10OCQl99C7g1Nq Y7tbW8Qo4vXUaeEebF1pjT21+bKDdcoyfC4v+hHTTBN9pab3JQUJ/xIXDWXjwvwkZU lRX9BLoyjic6gXRjGUrB9flz3Rit5pTPV2t3XvB0ed89BxXk0blRZIG682wDeTVVCr 9SOAgs4+rY6GGheSkA/AABqBD4e0qekTIrdKSkBN+1UZ36NVy6knFGnAesvQrNj0TF WiTefVmnmjTfA== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPm-0000000AOdg-1yJe; Sat, 07 Feb 2026 22:24:50 -0500 Message-ID: <20260208032450.318864210@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:23 -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 v2 06/10] tracing: Make tracing_update_buffers() take NULL for global_trace References: <20260208032417.262341179@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 4a73822e2603..601b6f622391 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -3234,7 +3234,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; @@ -6186,6 +6186,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 Wed Feb 11 07:48:32 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 560BA2D0600; Sun, 8 Feb 2026 03:24:07 +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=1770521047; cv=none; b=lQAUyPpPgBzdvt2/rM/EOeoN5jzZ4nMkS76eHCmZHEmSuTwHV1325Lg1zsd5UVUn39qjTqCpSTsZizFBZhe1vf03WKRTnXryZjFyoXBf4OSn9KW1PsSBtVBOOWyuJL5xM3PgfK9DmzMMTKYKbQ4iNvydhSqhNOxhM9kg45IiONc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521047; c=relaxed/simple; bh=i8degb57vUuiSHEjbklu1wiQSbWaGQjUxuSzZzW7ZMo=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=ZdqXlfEo5Yt/ghM71wd1BoDlERfjSonVieubnpZIkZ84gx+71Vm8yvPx46oSaijsmPVAmybqQvDQOx/GrA0MNicnFNrMU7ZwTbJ8ItYinxzLJFp744rrYRtiYb5rOd8c/++5I9KAF/BjzOEY9J7fiH6pieFYf4NCqo0B+FW1eKc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FiE81IZT; 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="FiE81IZT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3051AC2BC87; Sun, 8 Feb 2026 03:24:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521047; bh=i8degb57vUuiSHEjbklu1wiQSbWaGQjUxuSzZzW7ZMo=; h=Date:From:To:Cc:Subject:References:From; b=FiE81IZTR9yzLZcu8h/690/PxxI2CfHBy5u2OZREKOsBLbyV16zX469440jhQWb7A zhOM1NDClqvuwDjsuCDJzQ2XMreGpUeZbZdZ4vn8zQTmQw5A8UgdAGUTR3DUqA69ef H/6QriT55XprrecyDVGGKbPuiHgXwGt5m6fy3NorP3ASwA8WbOCLNE5kCZ9Ph3E0Cn Dz3q4mJuYP2V1DUoC7mzWeCEf9vmMfloIvfolrqiHRG8WYJ/CGmH2N6xSChiLV+6ZT T5ZY6JoFPp7H+VJLqa+KCoulCim79y//rMyFcp5dxKIaRq8dUrEZPyA6A404JSTaqx ITdf+gxpTn/fw== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPm-0000000AOeA-2gO1; Sat, 07 Feb 2026 22:24:50 -0500 Message-ID: <20260208032450.491116245@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:24 -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 v2 07/10] tracing: Have trace_printk functions use flags instead of using global_trace References: <20260208032417.262341179@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 601b6f622391..f4ae80564615 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)) @@ -3386,7 +3387,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); @@ -3422,7 +3423,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))) @@ -3449,7 +3450,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 Wed Feb 11 07:48:32 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 74B702D1911; Sun, 8 Feb 2026 03:24:07 +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=1770521047; cv=none; b=XaJnlBnxhm1l5DwWytaibyGfhSJhcftRLUlh8DhNE1cP2u1b5GWScw0CEcFHaiSF++M7NKhs16lPTL8GOBFx8EPTkktFkTonghJ/+9tMGqid6BeoHiIHoLqBRXhyuik/HwX7SIOMsfj/m+wuUi5fgI1uEDESETDBcbUfmVr03Uk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521047; c=relaxed/simple; bh=T6KjllVEpFn6yHv1jikWRDhzSSQ7e2FnENqxX2C8/ts=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=O6y7GhFOSPpeibu/ELf6srNye9XL4ijNkg2N1/pwe3WEyrw0wWt7vJmI17osZoupJtW71p1JW+rUZT5Vm/AchxuOkuOmAWoQi7hxdA0CyUg5+TB2nTPKF9VIKcK1ulv1qzounkPXMXQ4fzjNIXZrIlum9BTqJBym/jqId5Tdn4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SiYMa96V; 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="SiYMa96V" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F44AC4CEF7; Sun, 8 Feb 2026 03:24:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521047; bh=T6KjllVEpFn6yHv1jikWRDhzSSQ7e2FnENqxX2C8/ts=; h=Date:From:To:Cc:Subject:References:From; b=SiYMa96VaI25XuAQZAwjhwfNbHm1Ujt0aWDYQJr1pf9PijnIz+hSYcPrHGPbdJUKz VRGmzKnapQyUxLfEkYtbJKgZSg8TVSNYtEgHE68A5tjDzk1iKPoJtWD+ZXBrrvHFGR rClTpQoX7m2Pf3p34BT7obq50P4Bo/293repwEC13mFc1iGgcH648KISNbtIHEqadv QpNB31cV6ayi7OJab0aq/UjJTi1L81DfbhFYHUfP4VVlmfaKrPCOplN7UeGLl7HXPb ziYr3ByDrI0cGmEQbpJ+Q12MQ5v+Y0GWMiX67FPxTymOk6hO4+v23zF1uZEV8s+2LW SZ0ek5geA7W/g== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPm-0000000AOeg-3O7X; Sat, 07 Feb 2026 22:24:50 -0500 Message-ID: <20260208032450.660237094@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:25 -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 v2 08/10] tracing: Use system_state in trace_printk_init_buffers() References: <20260208032417.262341179@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 f4ae80564615..4066c33674e7 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -3243,10 +3243,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 Wed Feb 11 07:48:32 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 C88252D5957; Sun, 8 Feb 2026 03:24:07 +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=1770521047; cv=none; b=qEAwGEOv7Ljc5ixHFFK98TtLe7H5e3gwpp/7CzmaeWPcAcfKk0zwTxs8xnpA+wq1hGoUWZzJB/Wq0ubdvjlj7bfqvaWjA7K+P/nPcD9EBli3kQv1G94VW7Hx+xwq2vXQz69qU1mGhj5Y49wmU4+l/aKJN1mUJ+RZ+JE/PQtXaMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521047; c=relaxed/simple; bh=XDF2El33lw0RRIstOCa+Rte3svvG1kIhOVxkHOJNvSU=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=IDurEDTOouGNqMZTPGBukpK0Q1vjbIkSIoS00rP4uh4SdAAK21OGKiR8fSAjgk3SGB5i7yuLz8AsY7XDQKatNBSOc8N7/DkCvp8MfzFWWFHwTp7OnUW7Bk5SkG0HrMYrO5xyEmTM4enxZYZD5nYHXqh7znRMSQqBGeyZUEpbsWE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j1y1UKxf; 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="j1y1UKxf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83B49C116C6; Sun, 8 Feb 2026 03:24:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521047; bh=XDF2El33lw0RRIstOCa+Rte3svvG1kIhOVxkHOJNvSU=; h=Date:From:To:Cc:Subject:References:From; b=j1y1UKxfPAoNbs0zP1dctlBC6C7tVvkrwMx8ga9FM5cgNoRqQkVcle0BWrBCJkjf8 5wqE6pJ2pa4xcHRAm9svpkyoISxXcOCAYuT+tGgCYu2QjC0H5Ztz9e7zf1DCZwT8XN n3H3GCivuHAHykkX4g/20HC0O/MXHuTcZJOlcpD3gIiaCts3tBRWhTpNuWrqeOcnZX GnTOjtCtpRy3CY8BNiM40Cz3aBQP2aWAL9oWeOkVdDq5HucED5uxExLjDFWZVDh4ip n/ZX0/hGW/rKLWUVb/aXZbMbT7U/KyDdM+2zt0o8LQFwTfSvIbPl6JTzDibhhPotRh ahcU2V32mYwqQ== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPm-0000000AOfB-46Ns; Sat, 07 Feb 2026 22:24:50 -0500 Message-ID: <20260208032450.828744197@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:26 -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 v2 09/10] tracing: Move trace_printk functions out of trace.c and into trace_printk.c References: <20260208032417.262341179@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 4066c33674e7..5812b830c1fa 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) @@ -3159,324 +3046,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 921e4daa2825..6b0fedf2f532 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -2131,6 +2131,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 Wed Feb 11 07:48:32 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 E175B2D640D; Sun, 8 Feb 2026 03:24:07 +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=1770521048; cv=none; b=SkgfdPr5dK/vFWv6Jn+nq15OQBOsXi5m3PJF55H+GJrBvYpkok6iWsQjiq3RuYCsn2Mpl+lMpuvw3mN/cURBImB/zA1zu0enmMcgXckiR2/6WSCcb6BDM2iwNVFfCn3zgNr8iPL3aWehi/3phnCCDuZOb6g1VDcsZaS2u/nlhDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770521048; c=relaxed/simple; bh=ZoivyDaiiE9fgmWWluM+fGCmlmzO4kaZmV3OI1IQj5c=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=a49VR9E31+57fGe8HaqTWGqzk9sNriLjVTSeDSJU5PhDq1V/RiKdyYucD1Sp0HfYmZcvfySQ4eJJX/eYBTlYBp43DfSrEanhG70uSLnkTYDOv8DZtpVnkYndsIgbUbGKYaspb5dqJ4nr4JSQ0cXCpqLg9x7YCnb1QOR3JTQb7Y0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BrgROwLL; 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="BrgROwLL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1EBDC4AF09; Sun, 8 Feb 2026 03:24:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770521047; bh=ZoivyDaiiE9fgmWWluM+fGCmlmzO4kaZmV3OI1IQj5c=; h=Date:From:To:Cc:Subject:References:From; b=BrgROwLLQmUSlbk7ODBQ26qxpWXnFC1pC8e72ZNmXJZQeXdQSFB8DqYJ3Sx2XDcPN H0b4U7W4MAaffW8MDVxmbRpjWJ7GcDwfoDubWJME0SojBXnOeDJAG8KgqXRJA8AiNL HyeVjO3c2bu+MbyiNZpgEPhqF7DfYPxVtk+4CPK5a0ToWcRDDZ18uV8nD7fE8NHbUY n5eX5ty0fhhYYCaLzF84F3QjU1dnXgA+Fw9/AhhKUuBBFG523Fqx8OJHwGIhoh9z0N aQLnC8YvB9f4HXq+zfZ/TrgF8xTUdqkvdF6j24Ma0BIdAY9+mKpO8pVq2Mt0T7AB/i 0S8UbWKfTvm/w== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1vovPn-0000000AOfh-0Yy6; Sat, 07 Feb 2026 22:24:51 -0500 Message-ID: <20260208032450.998330662@kernel.org> User-Agent: quilt/0.68 Date: Sat, 07 Feb 2026 22:24:27 -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 v2 10/10] tracing: Move pid filtering into trace_pid.c References: <20260208032417.262341179@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 5812b830c1fa..551a452befa0 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