From: Tomas Glozar <tglozar@redhat.com>
To: Steven Rostedt <rostedt@goodmis.org>, Tomas Glozar <tglozar@redhat.com>
Cc: John Kacur <jkacur@redhat.com>,
Luis Goncalves <lgoncalv@redhat.com>,
Crystal Wood <crwood@redhat.com>,
Costa Shulyupin <costa.shul@redhat.com>,
Wander Lairson Costa <wander@redhat.com>,
LKML <linux-kernel@vger.kernel.org>,
linux-trace-kernel <linux-trace-kernel@vger.kernel.org>
Subject: [PATCH 3/5] rtla: Abort on nr_cpus mismatch with tracer
Date: Fri, 14 Aug 2026 15:55:09 +0200 [thread overview]
Message-ID: <20260814135511.2207190-4-tglozar@redhat.com> (raw)
In-Reply-To: <20260814135511.2207190-1-tglozar@redhat.com>
In rare situations, sysfs might report a different number of CPUs than
the kernel sees, for example, when /sys is not real sysfs.
In tracefs mode, RTLA will discard any samples that are reported on CPUs
higher than the maximum in sysfs. However, in BPF mode, the incorrectly
sized BPF_MAP_LOOKUP_ELEM syscall on percpu maps will cause a buffer
overflow.
To cover for this corner case, write "all" to osnoise/cpus, and read
back the real kernel-side list of all possible cpus. If it doesn't match
RTLA's nr_cpus (read from sysfs), abort with an error.
Assisted-by: Claude:claude-opus-4-6
Signed-off-by: Tomas Glozar <tglozar@redhat.com>
---
tools/tracing/rtla/src/common.c | 13 +++++++++-
tools/tracing/rtla/src/common.h | 1 +
tools/tracing/rtla/src/osnoise.c | 44 ++++++++++++++++++++++++++++++++
tools/tracing/rtla/src/utils.c | 16 ++++++++++++
tools/tracing/rtla/src/utils.h | 1 +
5 files changed, 74 insertions(+), 1 deletion(-)
diff --git a/tools/tracing/rtla/src/common.c b/tools/tracing/rtla/src/common.c
index 20fae1f19cacf..2a5333270e035 100644
--- a/tools/tracing/rtla/src/common.c
+++ b/tools/tracing/rtla/src/common.c
@@ -62,11 +62,22 @@ static void unset_signals(struct common_params *params)
int
common_apply_config(struct osnoise_tool *tool, struct common_params *params)
{
- int retval, i;
+ int retval, i, tracer_cpus;
if (!params->sleep_time)
params->sleep_time = 1;
+ tracer_cpus = osnoise_get_cpu_count();
+ if (tracer_cpus < 0) {
+ err_msg("Failed to get tracer cpu count from osnoise/cpus\n");
+ goto out_err;
+ }
+ if (tracer_cpus != nr_cpus) {
+ err_msg("Kernel reports %d cpus but rtla sees %d\n",
+ tracer_cpus, nr_cpus);
+ goto out_err;
+ }
+
retval = osnoise_set_cpus(tool->context, params->cpus ? params->cpus : "all");
if (retval) {
err_msg("Failed to apply CPUs config\n");
diff --git a/tools/tracing/rtla/src/common.h b/tools/tracing/rtla/src/common.h
index 051d56182276b..a7a9063c302b5 100644
--- a/tools/tracing/rtla/src/common.h
+++ b/tools/tracing/rtla/src/common.h
@@ -168,6 +168,7 @@ should_continue_tracing(const struct common_params *params)
int
common_threshold_handler(const struct osnoise_tool *tool);
+int osnoise_get_cpu_count(void);
int osnoise_set_cpus(struct osnoise_context *context, char *cpus);
void osnoise_restore_cpus(struct osnoise_context *context);
diff --git a/tools/tracing/rtla/src/osnoise.c b/tools/tracing/rtla/src/osnoise.c
index b9bcbf9ee430c..5a746bc11f03b 100644
--- a/tools/tracing/rtla/src/osnoise.c
+++ b/tools/tracing/rtla/src/osnoise.c
@@ -124,6 +124,50 @@ void osnoise_put_cpus(struct osnoise_context *context)
context->orig_cpus = NULL;
}
+/*
+ * osnoise_get_cpu_count - get the number of CPUs as seen by the osnoise tracer
+ *
+ * Write "all" to osnoise/cpus and read back the real kernel-side list
+ * of all possible cpus. Restore the original value afterwards.
+ *
+ * Returns the number of CPUs, or -1 on error.
+ */
+int osnoise_get_cpu_count(void)
+{
+ char *orig, *readback;
+ int max_cpu;
+
+ orig = tracefs_instance_file_read(NULL, "osnoise/cpus", NULL);
+ if (!orig)
+ return -1;
+
+ if (tracefs_instance_file_write(NULL, "osnoise/cpus", "all\n") < 0) {
+ free(orig);
+ return -1;
+ }
+
+ readback = tracefs_instance_file_read(NULL, "osnoise/cpus", NULL);
+ if (!readback) {
+ tracefs_instance_file_write(NULL, "osnoise/cpus", orig);
+ free(orig);
+ return -1;
+ }
+
+ max_cpu = get_max_cpu_from_list(readback);
+ free(readback);
+
+ if (tracefs_instance_file_write(NULL, "osnoise/cpus", orig) < 0) {
+ free(orig);
+ return -1;
+ }
+ free(orig);
+
+ if (max_cpu < 0)
+ return -1;
+
+ return max_cpu + 1;
+}
+
/*
* osnoise_read_ll_config - read a long long value from a config
*
diff --git a/tools/tracing/rtla/src/utils.c b/tools/tracing/rtla/src/utils.c
index d65de511be9e1..5d91c9151aa4a 100644
--- a/tools/tracing/rtla/src/utils.c
+++ b/tools/tracing/rtla/src/utils.c
@@ -161,6 +161,22 @@ static int max_cpu_callback(int i, void *data)
return 0;
}
+/*
+ * get_max_cpu_from_list - get the maximum CPU in a CPU list
+ *
+ * Returns maximum CPU number, or -1 on error
+ */
+int get_max_cpu_from_list(const char *cpu_list)
+{
+ int max_cpu = -1;
+
+ if (cpu_list_iterate(cpu_list, max_cpu_callback, &max_cpu) < 0)
+ return -1;
+
+ return max_cpu;
+}
+
+
static int tmp_cpu_set_callback(int i, void *data)
{
bool *cpu_set = data;
diff --git a/tools/tracing/rtla/src/utils.h b/tools/tracing/rtla/src/utils.h
index 2579e7fa08be6..44b450645ffb5 100644
--- a/tools/tracing/rtla/src/utils.h
+++ b/tools/tracing/rtla/src/utils.h
@@ -50,6 +50,7 @@ long parse_seconds_duration(char *val);
void get_duration(time_t start_time, char *output, int output_size);
int cpu_list_iterate(const char *cpu_list, int (*callback)(int, void *), void *data);
+int get_max_cpu_from_list(const char *cpu_list);
int get_possible_cpus(void);
long long get_llong_from_str(char *start);
--
2.55.0
next prev parent reply other threads:[~2026-08-14 13:55 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-14 13:55 [PATCH 0/5] rtla: Implement more robust nr_cpus handling Tomas Glozar
2026-08-14 13:55 ` [PATCH 1/5] rtla: Replace get_nprocs_conf() with sysfs possible cpus Tomas Glozar
2026-08-14 13:55 ` [PATCH 2/5] rtla: Discard trace entries with cpu >= nr_cpus Tomas Glozar
2026-08-14 14:02 ` sashiko-bot
2026-08-14 13:55 ` Tomas Glozar [this message]
2026-08-14 13:55 ` [PATCH 4/5] rtla/tests: Add unit test for cpu_list_iterate() Tomas Glozar
2026-08-14 13:55 ` [PATCH 5/5] rtla/tests: Add unit test for get_max_cpu_from_list() Tomas Glozar
2026-08-14 14:30 ` [PATCH 0/5] rtla: Implement more robust nr_cpus handling Steven Rostedt
2026-08-14 17:49 ` Tomas Glozar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260814135511.2207190-4-tglozar@redhat.com \
--to=tglozar@redhat.com \
--cc=costa.shul@redhat.com \
--cc=crwood@redhat.com \
--cc=jkacur@redhat.com \
--cc=lgoncalv@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=wander@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.