Skip to contentSkip to conversation

tracing: Updates for 6.13

PATCH In review Steven Rostedt 4 participants 19 messages Nov 1, 2024

Subsystems (5)
Documentation BPF Tracing ftrace +1 more subsystems
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

cover tracing: Updates for 6.13

git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
trace/for-next

Head SHA1: 773180c19386a6ee07368178bb58a3f185e8005d

Jinjie Ruan (1):
tracing: Replace strncpy() with strscpy() when copying comm

Justin Stitt (1):
tracing: Replace multiple deprecated strncpy with memcpy

Mathieu Desnoyers (4):
tracing: Introduce tracepoint extended structure
tracing: Introduce tracepoint_is_faultable()
tracing: Fix syscall tracepoint use-after-free
tracing: Add might_fault() check in __DECLARE_TRACE_SYSCALL

Nir Lichtman (1):
kdb: Remove fallback interpretation of arbitrary numbers as hex

Ryan Roberts (1):
tracing: Make percpu stack trace buffer invariant to PAGE_SIZE

Sebastian Andrzej Siewior (1):
tracing: Remove TRACE_FLAG_IRQS_NOSUPPORT

Yuran Pereira (2):
kdb: Replace the use of simple_strto with safer kstrto in kdb_main
trace: kdb: Replace simple_strtoul with kstrtoul in kdb_ftdump

----

 Documentation/trace/ftrace.rst     |  3 --
 include/linux/trace_events.h       | 13 -------
 include/linux/tracepoint-defs.h    | 10 ++++--
 include/linux/tracepoint.h         | 44 ++++++++++++++++++++----
 include/trace/define_trace.h       |  2 +-
 kernel/debug/kdb/kdb_main.c        | 69 ++++++++++----------------------------
 kernel/trace/trace.c               |  4 +--
 kernel/trace/trace_events_filter.c |  8 ++---
 kernel/trace/trace_events_hist.c   |  4 +--
 kernel/trace/trace_kdb.c           | 13 +++----
 kernel/trace/trace_output.c        |  1 -
 kernel/trace/trace_sched_switch.c  |  2 +-
 kernel/tracepoint.c                | 20 ++++++-----
 13 files changed, 89 insertions(+), 104 deletions(-)
author maintainer
#1 lore raw

PATCH 1/11 tracing: Make percpu stack trace buffer invariant to PAGE_SIZE

From: Ryan Roberts <[email protected]>

Previously the size of "struct ftrace_stacks" depended upon PAGE_SIZE. For the common 4K page size, on a 64-bit system, sizeof(struct ftrace_stacks) was 32K. But for a 64K page size, sizeof(struct ftrace_stacks) was 512K.

But ftrace stack usage requirements should be invariant to page size. So let's redefine FTRACE_KSTACK_ENTRIES so that "struct ftrace_stacks" is always sized at 32K for 64-bit and 16K for 32-bit.

As a side effect, it removes the PAGE_SIZE compile-time constant assumption from this code, which is required to reach the goal of boot-time page size selection.

---
 kernel/trace/trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace.c +1 -1
index bdb776e6ceb9..f1d613d924e9 100644
@@ -2898,7 +2898,7 @@ trace_function(struct trace_array *tr, unsigned long ip, unsigned long
 /* Allow 4 levels of nesting: normal, softirq, irq, NMI */
 #define FTRACE_KSTACK_NESTING	4
 
-#define FTRACE_KSTACK_ENTRIES	(PAGE_SIZE / FTRACE_KSTACK_NESTING)
+#define FTRACE_KSTACK_ENTRIES	(SZ_4K / FTRACE_KSTACK_NESTING)
 
 struct ftrace_stack {
 	unsigned long		calls[FTRACE_KSTACK_ENTRIES];
Signature · 1 line
2.45.2
author maintainer
#2 lore raw

PATCH 2/11 tracing: Replace multiple deprecated strncpy with memcpy

In reply to Steven Rostedt

From: Justin Stitt <[email protected]>

strncpy() is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces.

String copy operations involving manual pointer offset and length calculations followed by explicit NUL-byte assignments are best changed to either strscpy or memcpy.

strscpy is not a drop-in replacement as @len would need a one subtracted from it to avoid truncating the source string.

To not sabotage readability of the current code, use memcpy (retaining the manual NUL assignment) as this unambiguously describes the desired behavior.

---
 kernel/trace/trace_events_filter.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/trace/trace_events_filter.c +4 -4
index 0c611b281a5b..78051de581e7 100644
@@ -1616,7 +1616,7 @@ static int parse_pred(const char *str, void *data,
 				goto err_free;
 			}
 
-			strncpy(num_buf, str + s, len);
+			memcpy(num_buf, str + s, len);
 			num_buf[len] = 0;
 
 			ret = kstrtoul(num_buf, 0, &ip);
@@ -1694,7 +1694,7 @@ static int parse_pred(const char *str, void *data,
 		if (!pred->regex)
 			goto err_mem;
 		pred->regex->len = len;
-		strncpy(pred->regex->pattern, str + s, len);
+		memcpy(pred->regex->pattern, str + s, len);
 		pred->regex->pattern[len] = 0;
 
 	} else if (!strncmp(str + i, "CPUS", 4)) {
@@ -1859,7 +1859,7 @@ static int parse_pred(const char *str, void *data,
 		if (!pred->regex)
 			goto err_mem;
 		pred->regex->len = len;
-		strncpy(pred->regex->pattern, str + s, len);
+		memcpy(pred->regex->pattern, str + s, len);
 		pred->regex->pattern[len] = 0;
 
 		filter_build_regex(pred);
@@ -1919,7 +1919,7 @@ static int parse_pred(const char *str, void *data,
 			goto err_free;
 		}
 
-		strncpy(num_buf, str + s, len);
+		memcpy(num_buf, str + s, len);
 		num_buf[len] = 0;
 
 		/* Make sure it is a value */
Signature · 1 line
2.45.2
author maintainer
#3 lore raw

PATCH 3/11 kdb: Replace the use of simple_strto with safer kstrto in kdb_main

In reply to Steven Rostedt

From: Yuran Pereira <[email protected]>

The simple_str* family of functions perform no error checking in scenarios where the input value overflows the intended output variable. This results in these functions successfully returning even when the output does not match the input string.

Or as it was mentioned [1], "...simple_strtol(), simple_strtoll(), simple_strtoul(), and simple_strtoull() functions explicitly ignore overflows, which may lead to unexpected results in callers." Hence, the use of those functions is discouraged.

This patch replaces all uses of the simple_strto* series of functions with their safer kstrto* alternatives.

Side effects of this patch:
- Every string to long or long long conversion using kstrto* is now checked for failure.
- kstrto* errors are handled with appropriate KDB_BADINT wherever applicable.
- A good side effect is that we end up saving a few lines of code since unlike in simple_strto* functions, kstrto functions do not need an additional "end pointer" variable, and the return values of the latter can be directly checked in an "if" statement without the need to define additional ret or err variables. This, of course, results in cleaner, yet still easy to understand code.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull

[nir: addressed review comments by fixing styling, invalid conversion and a missing error return]
---
 kernel/debug/kdb/kdb_main.c | 69 +++++++++++--------------------------
 1 file changed, 21 insertions(+), 48 deletions(-)
kernel/debug/kdb/kdb_main.c +21 -48
index f5f7d7fb5936..f8703ab760d9 100644
@@ -306,8 +306,8 @@ static int kdbgetulenv(const char *match, unsigned long *value)
 		return KDB_NOTENV;
 	if (strlen(ep) == 0)
 		return KDB_NOENVVALUE;
-
-	*value = simple_strtoul(ep, NULL, 0);
+	if (kstrtoul(ep, 0, value))
+		return KDB_BADINT;
 
 	return 0;
 }
@@ -402,42 +402,23 @@ static void kdb_printenv(void)
  */
 int kdbgetularg(const char *arg, unsigned long *value)
 {
-	char *endp;
-	unsigned long val;
-
-	val = simple_strtoul(arg, &endp, 0);
-
-	if (endp == arg) {
-		/*
-		 * Also try base 16, for us folks too lazy to type the
-		 * leading 0x...
-		 */
-		val = simple_strtoul(arg, &endp, 16);
-		if (endp == arg)
+	/*
+	 * If the first fails, also try base 16, for us
+	 * folks too lazy to type the leading 0x...
+	 */
+	if (kstrtoul(arg, 0, value)) {
+		if (kstrtoul(arg, 16, value))
 			return KDB_BADINT;
 	}
-
-	*value = val;
-
 	return 0;
 }
 
 int kdbgetu64arg(const char *arg, u64 *value)
 {
-	char *endp;
-	u64 val;
-
-	val = simple_strtoull(arg, &endp, 0);
-
-	if (endp == arg) {
-
-		val = simple_strtoull(arg, &endp, 16);
-		if (endp == arg)
+	if (kstrtou64(arg, 0, value)) {
+		if (kstrtou64(arg, 16, value))
 			return KDB_BADINT;
 	}
-
-	*value = val;
-
 	return 0;
 }
 
@@ -473,10 +454,10 @@ int kdb_set(int argc, const char **argv)
 	 */
 	if (strcmp(argv[1], "KDBDEBUG") == 0) {
 		unsigned int debugflags;
-		char *cp;
+		int ret;
 
-		debugflags = simple_strtoul(argv[2], &cp, 0);
-		if (cp == argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) {
+		ret = kstrtouint(argv[2], 0, &debugflags);
+		if (ret || debugflags & ~KDB_DEBUG_FLAG_MASK) {
 			kdb_printf("kdb: illegal debug flags '%s'\n",
 				    argv[2]);
 			return 0;
@@ -1619,10 +1600,10 @@ static int kdb_md(int argc, const char **argv)
 		if (!argv[0][3])
 			valid = 1;
 		else if (argv[0][3] == 'c' && argv[0][4]) {
-			char *p;
-			repeat = simple_strtoul(argv[0] + 4, &p, 10);
+			if (kstrtouint(argv[0] + 4, 10, &repeat))
+				return KDB_BADINT;
 			mdcount = ((repeat * bytesperword) + 15) / 16;
-			valid = !*p;
+			valid = 1;
 		}
 		last_repeat = repeat;
 	} else if (strcmp(argv[0], "md") == 0)
@@ -2083,15 +2064,10 @@ static int kdb_dmesg(int argc, const char **argv)
 	if (argc > 2)
 		return KDB_ARGCOUNT;
 	if (argc) {
-		char *cp;
-		lines = simple_strtol(argv[1], &cp, 0);
-		if (*cp)
+		if (kstrtoint(argv[1], 0, &lines))
 			lines = 0;
-		if (argc > 1) {
-			adjust = simple_strtoul(argv[2], &cp, 0);
-			if (*cp || adjust < 0)
-				adjust = 0;
-		}
+		if (argc > 1 && (kstrtoint(argv[2], 0, &adjust) || adjust < 0))
+			adjust = 0;
 	}
 
 	/* disable LOGGING if set */
@@ -2428,14 +2404,12 @@ static int kdb_help(int argc, const char **argv)
 static int kdb_kill(int argc, const char **argv)
 {
 	long sig, pid;
-	char *endp;
 	struct task_struct *p;
 
 	if (argc != 2)
 		return KDB_ARGCOUNT;
 
-	sig = simple_strtol(argv[1], &endp, 0);
-	if (*endp)
+	if (kstrtol(argv[1], 0, &sig))
 		return KDB_BADINT;
 	if ((sig >= 0) || !valid_signal(-sig)) {
 		kdb_printf("Invalid signal parameter.<-signal>\n");
@@ -2443,8 +2417,7 @@ static int kdb_kill(int argc, const char **argv)
 	}
 	sig = -sig;
 
-	pid = simple_strtol(argv[2], &endp, 0);
-	if (*endp)
+	if (kstrtol(argv[2], 0, &pid))
 		return KDB_BADINT;
 	if (pid <= 0) {
 		kdb_printf("Process ID must be large than 0.\n");
Signature · 1 line
2.45.2
author maintainer
#11 lore raw

PATCH 4/11 trace: kdb: Replace simple_strtoul with kstrtoul in kdb_ftdump

In reply to Steven Rostedt

From: Yuran Pereira <[email protected]>

The function simple_strtoul performs no error checking in scenarios where the input value overflows the intended output variable. This results in this function successfully returning, even when the output does not match the input string (aka the function returns successfully even when the result is wrong).

Or as it was mentioned [1], "...simple_strtol(), simple_strtoll(), simple_strtoul(), and simple_strtoull() functions explicitly ignore overflows, which may lead to unexpected results in callers." Hence, the use of those functions is discouraged.

This patch replaces all uses of the simple_strtoul with the safer alternatives kstrtoint and kstrtol.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull

---
 kernel/trace/trace_kdb.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
kernel/trace/trace_kdb.c +5 -8
index 59857a1ee44c..1e72d20b3c2f 100644
@@ -96,22 +96,19 @@ static int kdb_ftdump(int argc, const char **argv)
 {
 	int skip_entries = 0;
 	long cpu_file;
-	char *cp;
+	int err;
 	int cnt;
 	int cpu;
 
 	if (argc > 2)
 		return KDB_ARGCOUNT;
 
-	if (argc) {
-		skip_entries = simple_strtol(argv[1], &cp, 0);
-		if (*cp)
-			skip_entries = 0;
-	}
+	if (argc && kstrtoint(argv[1], 0, &skip_entries))
+		return KDB_BADINT;
 
 	if (argc == 2) {
-		cpu_file = simple_strtol(argv[2], &cp, 0);
-		if (*cp || cpu_file >= NR_CPUS || cpu_file < 0 ||
+		err = kstrtol(argv[2], 0, &cpu_file);
+		if (err || cpu_file >= NR_CPUS || cpu_file < 0 ||
 		    !cpu_online(cpu_file))
 			return KDB_BADINT;
 	} else {
Signature · 1 line
2.45.2
author maintainer
#12 lore raw

PATCH 5/11 kdb: Remove fallback interpretation of arbitrary numbers as hex

In reply to Steven Rostedt

From: Nir Lichtman <[email protected]>

Remove logic that enables a fallback of interpreting numbers supplied in KDB CLI to be interpreted as hex without explicit "0x" prefix as this can be confusing for the end users.

---
 kernel/debug/kdb/kdb_main.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
kernel/debug/kdb/kdb_main.c +4 -12
index f8703ab760d9..5f4be507d79f 100644
@@ -402,23 +402,15 @@ static void kdb_printenv(void)
  */
 int kdbgetularg(const char *arg, unsigned long *value)
 {
-	/*
-	 * If the first fails, also try base 16, for us
-	 * folks too lazy to type the leading 0x...
-	 */
-	if (kstrtoul(arg, 0, value)) {
-		if (kstrtoul(arg, 16, value))
-			return KDB_BADINT;
-	}
+	if (kstrtoul(arg, 0, value))
+		return KDB_BADINT;
 	return 0;
 }
 
 int kdbgetu64arg(const char *arg, u64 *value)
 {
-	if (kstrtou64(arg, 0, value)) {
-		if (kstrtou64(arg, 16, value))
-			return KDB_BADINT;
-	}
+	if (kstrtou64(arg, 0, value))
+		return KDB_BADINT;
 	return 0;
 }
 
Signature · 1 line
2.45.2
author maintainer
#13 lore raw

PATCH 6/11 tracing: Remove TRACE_FLAG_IRQS_NOSUPPORT

In reply to Steven Rostedt

From: Sebastian Andrzej Siewior <[email protected]>

It was possible to enable tracing with no IRQ tracing support. The tracing infrastructure would then record TRACE_FLAG_IRQS_NOSUPPORT as the only tracing flag and show an 'X' in the output.

The last user of this feature was PPC32 which managed to implement it during PowerPC merge in 2009. Since then, it was unused and the PPC32 dependency was finally removed in commit 0ea5ee035133a ("tracing: Remove PPC32 wart from config TRACING_SUPPORT").
Since the PowerPC merge the code behind !CONFIG_TRACE_IRQFLAGS_SUPPORT with TRACING enabled can no longer be selected used and the 'X' is not displayed or recorded.

Remove the CONFIG_TRACE_IRQFLAGS_SUPPORT from the tracing code. Remove TRACE_FLAG_IRQS_NOSUPPORT.

---
 Documentation/trace/ftrace.rst |  3 ---
 include/linux/trace_events.h   | 13 -------------
 kernel/trace/trace_output.c    |  1 -
 3 files changed, 17 deletions(-)
Documentation/trace/ftrace.rst +0 -3
index 4073ca48af4a..74d5bd801b1a 100644
@@ -1031,9 +1031,6 @@ explains which is which.
   CPU#: The CPU which the process was running on.
 
   irqs-off: 'd' interrupts are disabled. '.' otherwise.
-	.. caution:: If the architecture does not support a way to
-		read the irq flags variable, an 'X' will always
-		be printed here.
 
   need-resched:
 	- 'N' both TIF_NEED_RESCHED and PREEMPT_NEED_RESCHED is set,
include/linux/trace_events.h +0 -13
index f8f2e52653df..016b29a56c87 100644
@@ -184,7 +184,6 @@ unsigned int tracing_gen_ctx_irq_test(unsigned int irqs_status);
 
 enum trace_flag_type {
 	TRACE_FLAG_IRQS_OFF		= 0x01,
-	TRACE_FLAG_IRQS_NOSUPPORT	= 0x02,
 	TRACE_FLAG_NEED_RESCHED		= 0x04,
 	TRACE_FLAG_HARDIRQ		= 0x08,
 	TRACE_FLAG_SOFTIRQ		= 0x10,
@@ -193,7 +192,6 @@ enum trace_flag_type {
 	TRACE_FLAG_BH_OFF		= 0x80,
 };
 
-#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
 static inline unsigned int tracing_gen_ctx_flags(unsigned long irqflags)
 {
 	unsigned int irq_status = irqs_disabled_flags(irqflags) ?
@@ -207,17 +205,6 @@ static inline unsigned int tracing_gen_ctx(void)
 	local_save_flags(irqflags);
 	return tracing_gen_ctx_flags(irqflags);
 }
-#else
-
-static inline unsigned int tracing_gen_ctx_flags(unsigned long irqflags)
-{
-	return tracing_gen_ctx_irq_test(TRACE_FLAG_IRQS_NOSUPPORT);
-}
-static inline unsigned int tracing_gen_ctx(void)
-{
-	return tracing_gen_ctx_irq_test(TRACE_FLAG_IRQS_NOSUPPORT);
-}
-#endif
 
 static inline unsigned int tracing_gen_ctx_dec(void)
 {
kernel/trace/trace_output.c +0 -1
index 868f2f912f28..2ee6613dce6d 100644
@@ -460,7 +460,6 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
 		(entry->flags & TRACE_FLAG_IRQS_OFF && bh_off) ? 'D' :
 		(entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
 		bh_off ? 'b' :
-		(entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
 		'.';
 
 	switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
Signature · 1 line
2.45.2
author maintainer
#14 lore raw

PATCH 7/11 tracing: Introduce tracepoint extended structure

In reply to Steven Rostedt

From: Mathieu Desnoyers <[email protected]>

Shrink the struct tracepoint size from 80 bytes to 72 bytes on x86-64 by moving the (typically NULL) regfunc/unregfunc pointers to an extended structure.

Cc Michael Jeanson <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Jordan Rife <[email protected]>
---
 include/linux/tracepoint-defs.h |  8 ++++++--
 include/linux/tracepoint.h      | 19 +++++++++++++------
 kernel/tracepoint.c             |  9 ++++-----
 3 files changed, 23 insertions(+), 13 deletions(-)
include/linux/tracepoint-defs.h +6 -2
index 60a6e8314d4c..967c08d9da84 100644
@@ -29,6 +29,11 @@ struct tracepoint_func {
 	int prio;
 };
 
+struct tracepoint_ext {
+	int (*regfunc)(void);
+	void (*unregfunc)(void);
+};
+
 struct tracepoint {
 	const char *name;		/* Tracepoint name */
 	struct static_key_false key;
@@ -36,9 +41,8 @@ struct tracepoint {
 	void *static_call_tramp;
 	void *iterator;
 	void *probestub;
-	int (*regfunc)(void);
-	void (*unregfunc)(void);
 	struct tracepoint_func __rcu *funcs;
+	struct tracepoint_ext *ext;
 };
 
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
include/linux/tracepoint.h +13 -6
index 0dc67fad706c..862ab49177a4 100644
@@ -302,7 +302,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
  * structures, so we create an array of pointers that will be used for iteration
  * on the tracepoints.
  */
-#define DEFINE_TRACE_FN(_name, _reg, _unreg, proto, args)		\
+#define __DEFINE_TRACE_EXT(_name, _ext, proto, args)			\
 	static const char __tpstrtab_##_name[]				\
 	__section("__tracepoints_strings") = #_name;			\
 	extern struct static_call_key STATIC_CALL_KEY(tp_func_##_name);	\
@@ -316,9 +316,9 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
 		.static_call_tramp = STATIC_CALL_TRAMP_ADDR(tp_func_##_name), \
 		.iterator = &__traceiter_##_name,			\
 		.probestub = &__probestub_##_name,			\
-		.regfunc = _reg,					\
-		.unregfunc = _unreg,					\
-		.funcs = NULL };					\
+		.funcs = NULL,						\
+		.ext = _ext,						\
+	};								\
 	__TRACEPOINT_ENTRY(_name);					\
 	int __traceiter_##_name(void *__data, proto)			\
 	{								\
@@ -341,8 +341,15 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
 	}								\
 	DEFINE_STATIC_CALL(tp_func_##_name, __traceiter_##_name);
 
-#define DEFINE_TRACE(name, proto, args)		\
-	DEFINE_TRACE_FN(name, NULL, NULL, PARAMS(proto), PARAMS(args));
+#define DEFINE_TRACE_FN(_name, _reg, _unreg, _proto, _args)		\
+	static struct tracepoint_ext __tracepoint_ext_##_name = {	\
+		.regfunc = _reg,					\
+		.unregfunc = _unreg,					\
+	};								\
+	__DEFINE_TRACE_EXT(_name, &__tracepoint_ext_##_name, PARAMS(_proto), PARAMS(_args));
+
+#define DEFINE_TRACE(_name, _proto, _args)				\
+	__DEFINE_TRACE_EXT(_name, NULL, PARAMS(_proto), PARAMS(_args));
 
 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)				\
 	EXPORT_SYMBOL_GPL(__tracepoint_##name);				\
kernel/tracepoint.c +4 -5
index 6474e2cf22c9..5658dc92f5b5 100644
@@ -278,8 +278,8 @@ static int tracepoint_add_func(struct tracepoint *tp,
 	struct tracepoint_func *old, *tp_funcs;
 	int ret;
 
-	if (tp->regfunc && !static_key_enabled(&tp->key)) {
-		ret = tp->regfunc();
+	if (tp->ext && tp->ext->regfunc && !static_key_enabled(&tp->key)) {
+		ret = tp->ext->regfunc();
 		if (ret < 0)
 			return ret;
 	}
@@ -362,9 +362,8 @@ static int tracepoint_remove_func(struct tracepoint *tp,
 	switch (nr_func_state(tp_funcs)) {
 	case TP_FUNC_0:		/* 1->0 */
 		/* Removed last function */
-		if (tp->unregfunc && static_key_enabled(&tp->key))
-			tp->unregfunc();
-
+		if (tp->ext && tp->ext->unregfunc && static_key_enabled(&tp->key))
+			tp->ext->unregfunc();
 		static_branch_disable(&tp->key);
 		/* Set iterator static call */
 		tracepoint_update_call(tp, tp_funcs);
Signature · 1 line
2.45.2
author maintainer
#15 lore raw

PATCH 8/11 tracing: Introduce tracepoint_is_faultable()

In reply to Steven Rostedt

From: Mathieu Desnoyers <[email protected]>

Introduce a "faultable" flag within the extended structure to know whether a tracepoint needs rcu tasks trace grace period before reclaim. This can be queried using tracepoint_is_faultable().

Cc Michael Jeanson <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Jordan Rife <[email protected]>
---
 include/linux/tracepoint-defs.h |  2 ++
 include/linux/tracepoint.h      | 24 ++++++++++++++++++++++++
 include/trace/define_trace.h    |  2 +-
 3 files changed, 27 insertions(+), 1 deletion(-)
include/linux/tracepoint-defs.h +2 -0
index 967c08d9da84..aebf0571c736 100644
@@ -32,6 +32,8 @@ struct tracepoint_func {
 struct tracepoint_ext {
 	int (*regfunc)(void);
 	void (*unregfunc)(void);
+	/* Flags. */
+	unsigned int faultable:1;
 };
 
 struct tracepoint {
include/linux/tracepoint.h +24 -0
index 862ab49177a4..906f3091d23d 100644
@@ -104,6 +104,12 @@ void for_each_tracepoint_in_module(struct module *mod,
  * tracepoint_synchronize_unregister must be called between the last tracepoint
  * probe unregistration and the end of module exit to make sure there is no
  * caller executing a probe when it is freed.
+ *
+ * An alternative is to use the following for batch reclaim associated
+ * with a given tracepoint:
+ *
+ * - tracepoint_is_faultable() == false: call_rcu()
+ * - tracepoint_is_faultable() == true:  call_rcu_tasks_trace()
  */
 #ifdef CONFIG_TRACEPOINTS
 static inline void tracepoint_synchronize_unregister(void)
@@ -111,9 +117,17 @@ static inline void tracepoint_synchronize_unregister(void)
 	synchronize_rcu_tasks_trace();
 	synchronize_rcu();
 }
+static inline bool tracepoint_is_faultable(struct tracepoint *tp)
+{
+	return tp->ext && tp->ext->faultable;
+}
 #else
 static inline void tracepoint_synchronize_unregister(void)
 { }
+static inline bool tracepoint_is_faultable(struct tracepoint *tp)
+{
+	return false;
+}
 #endif
 
 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
@@ -345,6 +359,15 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
 	static struct tracepoint_ext __tracepoint_ext_##_name = {	\
 		.regfunc = _reg,					\
 		.unregfunc = _unreg,					\
+		.faultable = false,					\
+	};								\
+	__DEFINE_TRACE_EXT(_name, &__tracepoint_ext_##_name, PARAMS(_proto), PARAMS(_args));
+
+#define DEFINE_TRACE_SYSCALL(_name, _reg, _unreg, _proto, _args)	\
+	static struct tracepoint_ext __tracepoint_ext_##_name = {	\
+		.regfunc = _reg,					\
+		.unregfunc = _unreg,					\
+		.faultable = true,					\
 	};								\
 	__DEFINE_TRACE_EXT(_name, &__tracepoint_ext_##_name, PARAMS(_proto), PARAMS(_args));
 
@@ -389,6 +412,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
 #define __DECLARE_TRACE_SYSCALL	__DECLARE_TRACE
 
 #define DEFINE_TRACE_FN(name, reg, unreg, proto, args)
+#define DEFINE_TRACE_SYSCALL(name, reg, unreg, proto, args)
 #define DEFINE_TRACE(name, proto, args)
 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
 #define EXPORT_TRACEPOINT_SYMBOL(name)
include/trace/define_trace.h +1 -1
index ff5fa17a6259..63fea2218afa 100644
@@ -48,7 +48,7 @@
 
 #undef TRACE_EVENT_SYSCALL
 #define TRACE_EVENT_SYSCALL(name, proto, args, struct, assign, print, reg, unreg) \
-	DEFINE_TRACE_FN(name, reg, unreg, PARAMS(proto), PARAMS(args))
+	DEFINE_TRACE_SYSCALL(name, reg, unreg, PARAMS(proto), PARAMS(args))
 
 #undef TRACE_EVENT_NOP
 #define TRACE_EVENT_NOP(name, proto, args, struct, assign, print)
Signature · 1 line
2.45.2
author maintainer
#16 lore raw

PATCH 9/11 tracing: Fix syscall tracepoint use-after-free

In reply to Steven Rostedt

From: Mathieu Desnoyers <[email protected]>

The grace period used internally within tracepoint.c:release_probes() uses call_rcu() to batch waiting for quiescence of old probe arrays, rather than using the tracepoint_synchronize_unregister() which blocks while waiting for quiescence.

With the introduction of faultable syscall tracepoints, this causes use-after-free issues reproduced with syzkaller.

Fix this by using the appropriate call_rcu() or call_rcu_tasks_trace() before invoking the rcu_free_old_probes callback. This can be chosen using the tracepoint_is_faultable() API.

A similar issue exists in bpf use of call_rcu(). Fixing this is left to a separate change.

Reported-by [email protected]
Fixes a363d27cdbc2 ("tracing: Allow system call tracepoints to handle page faults")
Cc Michael Jeanson <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Jordan Rife <[email protected]>
---
 kernel/tracepoint.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
kernel/tracepoint.c +7 -4
index 5658dc92f5b5..1848ce7e2976 100644
@@ -106,13 +106,16 @@ static void rcu_free_old_probes(struct rcu_head *head)
 	kfree(container_of(head, struct tp_probes, rcu));
 }
 
-static inline void release_probes(struct tracepoint_func *old)
+static inline void release_probes(struct tracepoint *tp, struct tracepoint_func *old)
 {
 	if (old) {
 		struct tp_probes *tp_probes = container_of(old,
 			struct tp_probes, probes[0]);
 
-		call_rcu(&tp_probes->rcu, rcu_free_old_probes);
+		if (tracepoint_is_faultable(tp))
+			call_rcu_tasks_trace(&tp_probes->rcu, rcu_free_old_probes);
+		else
+			call_rcu(&tp_probes->rcu, rcu_free_old_probes);
 	}
 }
 
@@ -334,7 +337,7 @@ static int tracepoint_add_func(struct tracepoint *tp,
 		break;
 	}
 
-	release_probes(old);
+	release_probes(tp, old);
 	return 0;
 }
 
@@ -405,7 +408,7 @@ static int tracepoint_remove_func(struct tracepoint *tp,
 		WARN_ON_ONCE(1);
 		break;
 	}
-	release_probes(old);
+	release_probes(tp, old);
 	return 0;
 }
 
Signature · 1 line
2.45.2
author maintainer
#17 lore raw

PATCH 10/11 tracing: Add might_fault() check in __DECLARE_TRACE_SYSCALL

In reply to Steven Rostedt

From: Mathieu Desnoyers <[email protected]>

Catch incorrect use of syscall tracepoints even if no probes are registered by adding a might_fault() check in trace_##name() emitted by __DECLARE_TRACE_SYSCALL.

Cc Thomas Gleixner <[email protected]>
Cc Michael Jeanson <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Yonghong Song <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Jordan Rife <[email protected]>
---
 include/linux/tracepoint.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/tracepoint.h +1 -0
index 906f3091d23d..425123e921ac 100644
@@ -301,6 +301,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)
 	__DECLARE_TRACE_COMMON(name, PARAMS(proto), PARAMS(args), cond, PARAMS(data_proto)) \
 	static inline void trace_##name(proto)				\
 	{								\
+		might_fault();						\
 		if (static_branch_unlikely(&__tracepoint_##name.key))	\
 			__DO_TRACE(name,				\
 				TP_ARGS(args),				\
Signature · 1 line
2.45.2
author maintainer
#18 lore raw

PATCH 11/11 tracing: Replace strncpy() with strscpy() when copying comm

In reply to Steven Rostedt

From: Jinjie Ruan <[email protected]>

Replace the depreciated[1] strncpy() calls with strscpy() when copying comm.

---
 kernel/trace/trace.c              | 2 +-
 kernel/trace/trace_events_hist.c  | 4 ++--
 kernel/trace/trace_sched_switch.c | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
kernel/trace/trace.c +1 -1
index f1d613d924e9..a587fd7d7447 100644
@@ -1921,7 +1921,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
 	max_data->critical_start = data->critical_start;
 	max_data->critical_end = data->critical_end;
 
-	strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
+	strscpy(max_data->comm, tsk->comm);
 	max_data->pid = tsk->pid;
 	/*
 	 * If tsk == current, then use current_uid(), as that does not use
kernel/trace/trace_events_hist.c +2 -2
index cc2924ad32a3..c288b92fc4df 100644
@@ -1599,7 +1599,7 @@ static inline void save_comm(char *comm, struct task_struct *task)
 		return;
 	}
 
-	strncpy(comm, task->comm, TASK_COMM_LEN);
+	strscpy(comm, task->comm, TASK_COMM_LEN);
 }
 
 static void hist_elt_data_free(struct hist_elt_data *elt_data)
@@ -3405,7 +3405,7 @@ static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
 	elt_data = context->elt->private_data;
 	track_elt_data = track_data->elt.private_data;
 	if (elt_data->comm)
-		strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
+		strscpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
 
 	track_data->updated = true;
 
kernel/trace/trace_sched_switch.c +1 -1
index 8a407adb0e1c..573b5d8e8a28 100644
@@ -187,7 +187,7 @@ static inline char *get_saved_cmdlines(int idx)
 
 static inline void set_cmdline(int idx, const char *cmdline)
 {
-	strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
+	strscpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
 }
 
 static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
Signature · 1 line
2.45.2
reviewer
#4 lore raw

Re: [for-next][PATCH 03/11] kdb: Replace the use of simple_strto with safer kstrto in kdb_main

In reply to Steven Rostedt

Hi,

Quoted 37 lines · Steven Rostedt

From: Yuran Pereira <[email protected]>

The simple_str* family of functions perform no error checking in scenarios where the input value overflows the intended output variable. This results in these functions successfully returning even when the output does not match the input string.

Or as it was mentioned [1], "...simple_strtol(), simple_strtoll(), simple_strtoul(), and simple_strtoull() functions explicitly ignore overflows, which may lead to unexpected results in callers." Hence, the use of those functions is discouraged.

This patch replaces all uses of the simple_strto* series of functions with their safer kstrto* alternatives.

Side effects of this patch:
- Every string to long or long long conversion using kstrto* is now checked for failure.
- kstrto* errors are handled with appropriate KDB_BADINT wherever applicable.
- A good side effect is that we end up saving a few lines of code since unlike in simple_strto* functions, kstrto functions do not need an additional "end pointer" variable, and the return values of the latter can be directly checked in an "if" statement without the need to define additional ret or err variables. This, of course, results in cleaner, yet still easy to understand code.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull

[nir: addressed review comments by fixing styling, invalid conversion and a missing error return]
Quoted 3 lines
---
 kernel/debug/kdb/kdb_main.c | 69 +++++++++++--------------------------
 1 file changed, 21 insertions(+), 48 deletions(-)

FWIW, I personally have no objection to this patch and patch #3/3 in Nir's series (#5/11 in your email thread) going through the ftrace tree, I'm not actually the maintainer of kdb/kgdb. I'm a reviewer and I try my best to help, but officially you should probably have Daniel Thompson's Ack for them. ...or at least make sure he's CCed here saying that you've picked them up.

I've added him to the conversation here.

-Doug

author maintainer
#5 lore raw

Re: [for-next][PATCH 03/11] kdb: Replace the use of simple_strto with safer kstrto in kdb_main

Quoted 8 lines · On Fri, 1 Nov 2024 07:21:05 -0700 Doug Anderson <[email protected]> wrote:

FWIW, I personally have no objection to this patch and patch #3/3 in Nir's series (#5/11 in your email thread) going through the ftrace tree, I'm not actually the maintainer of kdb/kgdb. I'm a reviewer and I try my best to help, but officially you should probably have Daniel Thompson's Ack for them. ...or at least make sure he's CCed here saying that you've picked them up.

I've added him to the conversation here.

Sure, I can even drop this patch if need be. Thanks for adding Daniel to the Cc. I probably should have run these patches through get maintainers to make sure everyone was accounted for.

-- Steve

#6 lore raw

Re: [for-next][PATCH 03/11] kdb: Replace the use of simple_strto with safer kstrto in kdb_main

Quoted 10 lines · Steven Rostedt
Quoted 7 lines · On Fri, 1 Nov 2024 07:21:05 -0700 Doug Anderson <[email protected]> wrote:

FWIW, I personally have no objection to this patch and patch #3/3 in Nir's series (#5/11 in your email thread) going through the ftrace tree, I'm not actually the maintainer of kdb/kgdb. I'm a reviewer and I try my best to help, but officially you should probably have Daniel Thompson's Ack for them. ...or at least make sure he's CCed here saying that you've picked them up.

Quoted 5 lines
Quoted 1 line

I've added him to the conversation here.

Sure, I can even drop this patch if need be. Thanks for adding Daniel to the Cc. I probably should have run these patches through get maintainers to make sure everyone was accounted for.

I haven't had a chance to review or hoover up the kdb/kgdb patches for this cycle yet (mixture of travel and other things) but they are queued up in my TODO list for next week.

I presume the tracing tree is involved because one of them changes the kdb ftrace command? Are there dependencies between that and other patches in the seriesm?

Daniel.

author maintainer Acked-by
#7 lore raw

Re: [for-next][PATCH 03/11] kdb: Replace the use of simple_strto with safer kstrto in kdb_main

Quoted 3 lines · On Fri, 1 Nov 2024 18:22:04 +0000 Daniel Thompson <[email protected]>…

I presume the tracing tree is involved because one of them changes the kdb ftrace command? Are there dependencies between that and other patches in the seriesm?

Nope, I'll drop them from my queue.

You can add to the tracing patch:

-- Steve

#8 lore raw

Re: [for-next][PATCH 03/11] kdb: Replace the use of simple_strto with safer kstrto in kdb_main

In reply to Daniel Thompson
Quoted 13 lines · Daniel Thompson
Quoted 12 lines · Steven Rostedt
Quoted 8 lines · On Fri, 1 Nov 2024 07:21:05 -0700 Doug Anderson <[email protected]> wrote:

FWIW, I personally have no objection to this patch and patch #3/3 in Nir's series (#5/11 in your email thread) going through the ftrace tree, I'm not actually the maintainer of kdb/kgdb. I'm a reviewer and I try my best to help, but officially you should probably have Daniel Thompson's Ack for them. ...or at least make sure he's CCed here saying that you've picked them up.

I've added him to the conversation here.

Quoted 8 lines
Quoted 3 lines

Sure, I can even drop this patch if need be. Thanks for adding Daniel to the Cc. I probably should have run these patches through get maintainers to make sure everyone was accounted for.

I presume the tracing tree is involved because one of them changes the kdb ftrace command? Are there dependencies between that and other patches in the seriesm?

I assume that is the reason, I just used the same recipient list that the original author of the patch series used a couple of months ago. The patch series is mostly around migrating to usage of better string to int conversion functions, so technically each change is not really dependant on the others.

BTW, Thanks for the reviews Doug and for applying Steven.

Nir.

Quoted 3 lines

Daniel.

author maintainer
#9 lore raw

Re: [for-next][PATCH 03/11] kdb: Replace the use of simple_strto with safer kstrto in kdb_main

Quoted 7 lines · On Fri, 1 Nov 2024 18:42:28 +0000 Nir Lichtman <[email protected]> wrote:

I assume that is the reason, I just used the same recipient list that the original author of the patch series used a couple of months ago. The patch series is mostly around migrating to usage of better string to int conversion functions, so technically each change is not really dependant on the others.

BTW, Thanks for the reviews Doug and for applying Steven.

Note, it's now in Daniel's court. I dropped the patches from linux-next.

I took Doug's Reviewed-by tags as saying it can go through my tree. But that was jumping the gun.

Sorry for the confusion.

-- Steve

#10 lore raw

Re: [for-next][PATCH 03/11] kdb: Replace the use of simple_strto with safer kstrto in kdb_main

Quoted 11 lines · Steven Rostedt
Quoted 1 line · On Fri, 1 Nov 2024 18:42:28 +0000 Nir Lichtman <[email protected]> wrote:

BTW, Thanks for the reviews Doug and for applying Steven.

Note, it's now in Daniel's court. I dropped the patches from linux-next.

I took Doug's Reviewed-by tags as saying it can go through my tree. But that was jumping the gun.

Sorry for the confusion.

Yah just saw that after sending the e-mail,
no worries, still thanks :)

Nir

Quoted 2 lines

-- Steve