Skip to contentSkip to conversation

ftrace: Finish removing subops after a hash update failure

PATCH In review Weiming Shi 2 participants 3 messages Nov 22

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ftrace: Finish removing subops after a hash update failure

When removing a non-final graph subop, ftrace_shutdown_subops() first detaches it, then rebuilds and installs the manager hashes. If rebuild_hashes() fails, the subop stays detached with its ENABLED and SUBOP flags and manager pointer intact. If ftrace_update_ops() fails, the subop is added back even though unregister_ftrace_graph() has already removed its graph slot.

In the fprobe path, ftrace_free_filter() then warns and returns, while __fprobe_graph_unregister() marks the shared graph operation unregistered. A later registration follows the stale SUBOP state into an empty manager list. rebuild_hashes() leaves its output pointers uninitialized in that case, and ops_equal() dereferences one of them.

Complete non-final removal on both manager-hash failure paths and initialize empty rebuild outputs to EMPTY_HASH. Keep direct dispatch disabled after an update failure because the old manager hashes may cover functions needed only by the removed subop; normal dispatch still applies the remaining subops' filters.

Completing removal also lets the caller free the subop filter. Split graph unregistration into removal and callback-wait helpers so Tasks RCU waits run before saved_func or the operation can be released. For asynchronous fprobe removal, remove the graph operation immediately and defer the wait and filter release to a worker that is flushed before reuse. Gate return callbacks on fprobe_graph_registered while cleanup is pending.

Oops: general protection fault, probably for non-canonical address 0xdffffc0000000802: 0000 [#1] SMP KASAN NOPTI
KASAN: probably user-memory-access in range [0x0000000000004010-0x0000000000004017]
RIP: ops_equal (kernel/trace/trace.h:1011 kernel/trace/ftrace.c:3416)
Call Trace:
 ftrace_update_ops (kernel/trace/ftrace.c:3464)
 ftrace_hash_move_and_update_subops.isra.0 (kernel/trace/ftrace.c:3795)
 ftrace_hash_move_and_update_ops (kernel/trace/ftrace.c:5095)
 ftrace_set_hash (kernel/trace/ftrace.c:6006)
 ftrace_set_filter_ips (kernel/trace/ftrace.c:6020 kernel/trace/ftrace.c:6769)
 register_fprobe_ips (kernel/trace/fprobe.c:290 kernel/trace/fprobe.c:1020)
 register_fprobe (kernel/trace/fprobe.c:979)
 __register_trace_fprobe.part.0 (kernel/trace/trace_fprobe.c:838)
 enable_trace_fprobe (kernel/trace/trace_fprobe.c:823 kernel/trace/trace_fprobe.c:1483)
 fprobe_register (kernel/trace/trace_fprobe.c:1553)
 __ftrace_event_enable_disable (kernel/trace/trace_events.c:913)
 event_enable_write (kernel/trace/trace_events.c:943 kernel/trace/trace_events.c:1995)
 vfs_write (fs/read_write.c:665)
 ksys_write (fs/read_write.c:719)
 do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89)
 entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
Kernel panic - not syncing: Fatal exception
Fixes 0ae6b8ce200d ("ftrace: Fix accounting of subop hashes")
Assisted-by LLM

---
Changes in v2:
- Merge the dependent teardown and graph-reader fixes.
- Use Tasks RCU waits and defer the fprobe callback wait and filter release to a worker.
- Rebase onto 4405c18c95d2 and decode the reproduced crash trace.
- v1: https://lore.kernel.org/r/[email protected]

 kernel/trace/fgraph.c          | 53 +++++++++++++++++++++++++++++-----
 kernel/trace/fprobe.c          | 21 +++++++++++---
 kernel/trace/ftrace.c          | 22 +++++++++-----
 kernel/trace/ftrace_internal.h |  2 ++
 4 files changed, 79 insertions(+), 19 deletions(-)
kernel/trace/fgraph.c +46 -7
index 6d87143b3..3760795d3 100644
@@ -1406,8 +1406,11 @@ int register_ftrace_graph(struct fgraph_ops *gops)
 	gops->ops.flags |= FTRACE_OPS_FL_GRAPH;
 
 	ret = ftrace_startup_subops(&graph_ops, &gops->ops, command);
-	if (!ret)
+	if (!ret) {
 		fgraph_array[i] = gops;
+		if (ftrace_graph_active == 1)
+			ftrace_graph_enable_direct(true, gops);
+	}
 
 error:
 	if (ret) {
@@ -1420,13 +1423,27 @@ int register_ftrace_graph(struct fgraph_ops *gops)
 	return ret;
 }
 
-void unregister_ftrace_graph(struct fgraph_ops *gops)
+/**
+ * unregister_ftrace_graph_start - remove a function graph operation
+ * @gops: graph operation to remove
+ *
+ * Remove @gops from graph dispatch, release its array slot, and update the
+ * graph and subop state. Existing graph callbacks may still be using @gops
+ * after this returns.
+ *
+ * Return: true if @gops was removed. The caller must then invoke
+ * unregister_ftrace_graph_finish() exactly once before releasing @gops or its
+ * filter, even if the subop hash update failed.
+ */
+bool unregister_ftrace_graph_start(struct fgraph_ops *gops)
 {
 	int command = 0;
+	int ret = -EINVAL;
+	bool removed = false;
 
 	if (WARN_ONCE(!(gops->ops.flags & FTRACE_OPS_FL_GRAPH),
 		      "function graph ops unregistered without registering"))
-		return;
+		return false;
 
 	guard(mutex)(&ftrace_lock);
 
@@ -1440,16 +1457,17 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
 	if (fgraph_lru_release_index(gops->idx) < 0)
 		goto out;
 
-	fgraph_array[gops->idx] = &fgraph_stub;
+	WRITE_ONCE(fgraph_array[gops->idx], &fgraph_stub);
+	removed = true;
 
 	ftrace_graph_active--;
 
 	if (!ftrace_graph_active)
 		command = FTRACE_STOP_FUNC_RET;
 
-	ftrace_shutdown_subops(&graph_ops, &gops->ops, command);
+	ret = ftrace_shutdown_subops(&graph_ops, &gops->ops, command);
 
-	if (ftrace_graph_active == 1)
+	if (ftrace_graph_active == 1 && !ret)
 		ftrace_graph_enable_direct(true, NULL);
 	else if (!ftrace_graph_active)
 		ftrace_graph_disable_direct(false);
@@ -1460,7 +1478,28 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
 		unregister_pm_notifier(&ftrace_suspend_notifier);
 		unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
 	}
-	gops->saved_func = NULL;
  out:
 	gops->ops.flags &= ~FTRACE_OPS_FL_GRAPH;
+	return removed;
+}
+
+/**
+ * unregister_ftrace_graph_finish - wait for callbacks using a removed operation
+ * @gops: graph operation previously removed by unregister_ftrace_graph_start()
+ *
+ * Wait for existing graph readers, then clear the saved callback. This may
+ * sleep. The caller may release @gops and its filter after this returns.
+ */
+void unregister_ftrace_graph_finish(struct fgraph_ops *gops)
+{
+	/* Graph callbacks can run where RCU is not watching. */
+	synchronize_rcu_tasks_rude();
+	synchronize_rcu_tasks();
+	gops->saved_func = NULL;
+}
+
+void unregister_ftrace_graph(struct fgraph_ops *gops)
+{
+	if (unregister_ftrace_graph_start(gops))
+		unregister_ftrace_graph_finish(gops);
 }
kernel/trace/fprobe.c +17 -4
index 47d565a1e..29fc2bfc4 100644
@@ -14,9 +14,11 @@
 #include <linux/rhashtable.h>
 #include <linux/slab.h>
 #include <linux/sort.h>
+#include <linux/workqueue.h>
 
 #include <asm/fprobe.h>
 
+#include "ftrace_internal.h"
 #include "trace.h"
 
 #define FPROBE_IP_HASH_BITS 8
@@ -279,6 +281,14 @@ static struct fgraph_ops fprobe_graph_ops = {
 static int nr_fgraph_fprobes;
 /* Is fprobe_graph_ops registered? */
 static bool fprobe_graph_registered;
+static void fprobe_graph_cleanup_workfn(struct work_struct *work);
+static DECLARE_WORK(fprobe_graph_cleanup_work, fprobe_graph_cleanup_workfn);
+
+static void fprobe_graph_cleanup_workfn(struct work_struct *work)
+{
+	unregister_ftrace_graph_finish(&fprobe_graph_ops);
+	ftrace_free_filter(&fprobe_graph_ops.ops);
+}
 
 /* Add @addrs to the ftrace filter and register fgraph if needed. */
 static int fprobe_graph_add_ips(unsigned long *addrs, int num)
@@ -286,6 +296,7 @@ static int fprobe_graph_add_ips(unsigned long *addrs, int num)
 	int ret;
 
 	lockdep_assert_held(&fprobe_mutex);
+	flush_work(&fprobe_graph_cleanup_work);
 
 	ret = ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0);
 	if (ret)
@@ -297,7 +308,7 @@ static int fprobe_graph_add_ips(unsigned long *addrs, int num)
 			ftrace_free_filter(&fprobe_graph_ops.ops);
 			return ret;
 		}
-		fprobe_graph_registered = true;
+		WRITE_ONCE(fprobe_graph_registered, true);
 	}
 	return 0;
 }
@@ -305,9 +316,9 @@ static int fprobe_graph_add_ips(unsigned long *addrs, int num)
 static void __fprobe_graph_unregister(void)
 {
 	if (fprobe_graph_registered) {
-		unregister_ftrace_graph(&fprobe_graph_ops);
-		ftrace_free_filter(&fprobe_graph_ops.ops);
-		fprobe_graph_registered = false;
+		WRITE_ONCE(fprobe_graph_registered, false);
+		if (unregister_ftrace_graph_start(&fprobe_graph_ops))
+			schedule_work(&fprobe_graph_cleanup_work);
 	}
 }
 
@@ -679,6 +690,8 @@ static void fprobe_return(struct ftrace_graph_ret *trace,
 	ret_ip = ftrace_regs_get_instruction_pointer(fregs);
 
 	guard(rcu_sched_notrace)();
+	if (!READ_ONCE(fprobe_graph_registered))
+		return;
 
 	curr = 0;
 	while (size_words > curr) {
kernel/trace/ftrace.c +14 -8
index c7d74b33d..7c712d99f 100644
@@ -3672,6 +3672,8 @@ static int rebuild_hashes(struct ftrace_hash **filter_hash, struct ftrace_hash *
 	bool first = true;
 	int ret;
 
+	*filter_hash = EMPTY_HASH;
+	*notrace_hash = EMPTY_HASH;
 	temp_hash.filter_hash = EMPTY_HASH;
 	temp_hash.notrace_hash = EMPTY_HASH;
 
@@ -3713,7 +3715,13 @@ static int rebuild_hashes(struct ftrace_hash **filter_hash, struct ftrace_hash *
  * will not affect functions that are being traced by other subops that
  * still exist in @ops.
  *
- * If the last subops is removed from @ops, then @ops is shutdown normally.
+ * If @subops is the last subop, @ops is shut down normally. Otherwise,
+ * @subops is detached even if the manager hash update fails. The manager
+ * hashes may be partially updated, but still cover all functions required by
+ * the remaining subops.
+ *
+ * Return: 0 on success, or a negative error code on shutdown or hash update
+ * failure.
  */
 int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command)
 {
@@ -3753,15 +3761,13 @@ int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, in
 	/* Rebuild the hashes without subops */
 	ret = rebuild_hashes(&filter_hash, &notrace_hash, ops);
 	if (ret < 0)
-		return ret;
+		goto out;
 
 	ret = ftrace_update_ops(ops, filter_hash, notrace_hash);
-	if (ret < 0) {
-		list_add(&subops->list, &ops->subop_list);
-	} else {
-		subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP);
-		subops->managed = NULL;
-	}
+
+out:
+	subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP);
+	subops->managed = NULL;
 	free_ftrace_hash(filter_hash);
 	free_ftrace_hash(notrace_hash);
 	return ret;
kernel/trace/ftrace_internal.h +2 -0
index 3235470e6..abb7776a2 100644
@@ -52,6 +52,8 @@ static inline int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_o
 
 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
 extern int ftrace_graph_active;
+bool unregister_ftrace_graph_start(struct fgraph_ops *gops);
+void unregister_ftrace_graph_finish(struct fgraph_ops *gops);
 # ifdef CONFIG_DYNAMIC_FTRACE
 extern void fgraph_update_pid_func(void);
 # else
Signature · 1 line
2.56.0
#1 lore raw

Hi,

Here is the exact reproducer and the commands used for the crash in the commit message.

The tested kernel was commit 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd with:

```
CONFIG_SMP=y
CONFIG_DEBUG_INFO=y
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_FS_ALLOW_ALL=y
CONFIG_KASAN=y
CONFIG_KASAN_GENERIC=y
CONFIG_KASAN_INLINE=y
CONFIG_FUNCTION_TRACER=y
CONFIG_FUNCTION_GRAPH_TRACER=y
CONFIG_DYNAMIC_FTRACE=y
CONFIG_FPROBE=y
CONFIG_FAULT_INJECTION=y
CONFIG_FAILSLAB=y
CONFIG_FAULT_INJECTION_DEBUG_FS=y
CONFIG_FAULT_INJECTION_STACKTRACE_FILTER=y
CONFIG_FTRACE_STARTUP_TEST=y
CONFIG_EVENT_TRACE_STARTUP_TEST=y
```

KASAN and DEBUG_INFO produce and decode the report. FAILSLAB and its stacktrace filter make the allocation failure deterministic. The reproducer does not need a race or any kernel source modification. It must run as root because it mounts tracefs and debugfs and configures failslab.

The test kernel also had the ftrace startup tests enabled. The ftrace=nop command-line option below skips those boot-time tests; it does not disable ftrace, and the reproducer selects the function_graph tracer itself. The option is not required when the startup tests are disabled in the kernel configuration.

Host command:

```
qemu-system-x86_64 \
    -accel kvm -cpu host -smp 4 -m 4096 \
    -no-reboot -nographic \
    -kernel arch/x86/boot/bzImage \
    -initrd initramfs.cpio.gz \
    -append "console=ttyS0 panic=1 oops=panic panic_on_warn=0 nokaslr net.ifnames=0 ftrace=nop"
```

The tested initramfs runs /poc from /init. To build and run it in an existing guest instead:

```
gcc -O2 -Wall -Wextra -o poc poc.c
sudo ./poc
```

The reproducer does the following:

1. It enables the function_graph tracer with tcp_v4_connect as a nonempty filter.
2. It creates and enables a return fprobe on udp_sendmsg.
3. It scopes failslab to allocations below ftrace_shutdown_subops() and fails one allocation while disabling the fprobe. This makes rebuild_hashes() return -ENOMEM.
4. It updates the remaining graph filter, verifies that the fprobe operation is stuck with -EBUSY, and disables the function_graph tracer.
5. It enables the fprobe again. The stale SUBOP state sends the filter update through an empty manager list, and ops_equal() dereferences an uninitialized hash pointer.

With the command above, the failure was reproduced on the first run. The relevant output was:

```
[*] removing subops B (fprobe) with rebuild_hashes() forced to -ENOMEM
FAULT_INJECTION: forcing a failure.
[*] updating the remaining graph filter after the failed teardown
[*] probing the bricked state (expect EBUSY + WARNs)
[-] write(/tmp/poctrace/events/fprobes/poc/enable, "1"): Device or resource busy
[!] CONFIRMED: fprobe_graph_ops is bricked (-EBUSY)
[*] removing subops A -> graph_ops->subop_list is now empty
[*] re-registering the fprobe: expect a wild deref in ops_equal()/ftrace_hash_empty()
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000802: 0000 [#1] SMP KASAN NOPTI
KASAN: probably user-memory-access in range [0x0000000000004010-0x0000000000004017]
RIP: 0010:ops_equal+0x41/0x600
Call Trace:
 ftrace_update_ops+0x64/0x140
 ftrace_hash_move_and_update_subops.isra.0+0x180/0x360
 ftrace_hash_move_and_update_ops+0x271/0x350
 ftrace_set_hash+0x38e/0x860
 ftrace_set_filter_ips+0x88/0x270
 register_fprobe_ips+0x395/0x14d0
 register_fprobe+0x204/0x260
 enable_trace_fprobe+0x1ef/0x4b0
 fprobe_register+0xa3/0xd0
```

Complete PoC:

```c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <stdarg.h>

#define TR  "/tmp/poctrace"
#define FS  "/sys/kernel/debug/failslab"

static int kmsg_fd = -1;

static void say(const char *fmt, ...)
{
	char buf[512];
	va_list ap;
	int n;

	va_start(ap, fmt);
	n = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
	va_end(ap);
	if (n < 0)
		return;
	if (kmsg_fd >= 0)
		write(kmsg_fd, buf, n);
	else {
		fwrite(buf, 1, n, stdout);
		fflush(stdout);
	}
}

static int wr(const char *path, const char *val, int flags)
{
	int fd, n, e;

	fd = open(path, O_WRONLY | flags);
	if (fd < 0) {
		say("[-] open(%s): %s\n", path, strerror(errno));
		return -1;
	}
	n = write(fd, val, strlen(val));
	e = errno;
	close(fd);
	if (n < 0) {
		say("[-] write(%s, \"%s\"): %s\n", path, val, strerror(e));
		return -e;
	}
	return 0;
}

static int w(const char *path, const char *val)  { return wr(path, val, O_TRUNC); }
static int wa(const char *path, const char *val) { return wr(path, val, O_APPEND); }

static int wl(const char *path, unsigned long v)
{
	char buf[64];
	snprintf(buf, sizeof(buf), "%lu", v);
	return w(path, buf);
}

static int wx(const char *path, unsigned long v)
{
	char buf[64];
	snprintf(buf, sizeof(buf), "0x%lx", v);
	return w(path, buf);
}

static int sym_range(const char *name, unsigned long *start, unsigned long *end)
{
	FILE *f = fopen("/proc/kallsyms", "r");
	char line[512], nm[256], type;
	unsigned long addr;
	int found = 0;

	if (!f) {
		say("[-] /proc/kallsyms: %s\n", strerror(errno));
		return -1;
	}
	*start = *end = 0;
	while (fgets(line, sizeof(line), f)) {
		if (sscanf(line, "%lx %c %255s", &addr, &type, nm) != 3)
			continue;
		if (found) {
			if (addr > *start) {
				*end = addr;
				break;
			}
			continue;
		}
		if (!strcmp(nm, name)) {
			*start = addr;
			found = 1;
		}
	}
	fclose(f);
	if (!found)
		return -1;
	if (!*end)
		*end = *start + 0x400;
	return 0;
}

int main(void)
{
	unsigned long s = 0, e = 0;

	setvbuf(stdout, NULL, _IONBF, 0);
	kmsg_fd = open("/dev/kmsg", O_WRONLY);
	w("/proc/sys/kernel/printk_devkmsg", "on\n");
	w("/proc/sys/kernel/printk", "8 4 1 7");

	mkdir("/sys/kernel/debug", 0755);
	if (mount("none", "/sys/kernel/debug", "debugfs", 0, NULL) && errno != EBUSY)
		say("[-] mount debugfs: %s\n", strerror(errno));
	mkdir("/tmp", 0755);
	mkdir(TR, 0755);
	if (mount("none", TR, "tracefs", 0, NULL) && errno != EBUSY) {
		say("[-] mount tracefs: %s\n", strerror(errno));
		return 1;
	}

	if (sym_range("ftrace_shutdown_subops", &s, &e)) {
		say("[-] ftrace_shutdown_subops not in kallsyms\n");
		return 1;
	}
	say("[*] ftrace_shutdown_subops = [0x%lx, 0x%lx)\n", s, e);

	w(TR "/current_tracer", "nop");
	w(TR "/set_ftrace_filter", "tcp_v4_connect");
	if (w(TR "/current_tracer", "function_graph")) {
		say("[-] cannot enable function_graph\n");
		return 1;
	}

	wa(TR "/dynamic_events", "f:poc udp_sendmsg%return");
	if (w(TR "/events/fprobes/poc/enable", "1")) {
		say("[-] cannot enable the fprobe event\n");
		return 1;
	}

	w(FS "/times", "0");
	wl(FS "/probability", 0);
	wl(FS "/interval", 1);
	w(FS "/space", "0");
	wl(FS "/verbose", 2);
	w(FS "/task-filter", "N");
	w(FS "/ignore-gfp-wait", "N");
	wl(FS "/stacktrace-depth", 32);
	wx(FS "/require-start", s);
	wx(FS "/require-end", e);
	wl(FS "/probability", 100);
	w(FS "/times", "1");

	say("[*] removing subops B (fprobe) with rebuild_hashes() forced to -ENOMEM\n");
	w(TR "/events/fprobes/poc/enable", "0");

	wl(FS "/probability", 0);
	w(FS "/times", "0");
	wx(FS "/require-start", 0);
	wx(FS "/require-end", ~0UL);

	say("[*] updating the remaining graph filter after the failed teardown\n");
	if (w(TR "/set_ftrace_filter", "udp_sendmsg")) {
		say("[-] cannot update the remaining graph filter\n");
		return 1;
	}

	say("[*] probing the bricked state (expect EBUSY + WARNs)\n");
	{
		int r = w(TR "/events/fprobes/poc/enable", "1");
		if (r == -EBUSY)
			say("[!] CONFIRMED: fprobe_graph_ops is bricked (-EBUSY)\n");
		else
			say("[?] re-enable returned %d (expected -EBUSY=-16)\n", r);
		w(TR "/events/fprobes/poc/enable", "0");
	}

	say("[*] removing subops A -> graph_ops->subop_list is now empty\n");
	w(TR "/current_tracer", "nop");

	say("[*] re-registering the fprobe: expect a wild deref in "
	    "ops_equal()/ftrace_hash_empty()\n");
	w(TR "/events/fprobes/poc/enable", "1");

	say("[*] survived - no crash this round\n");
	w(TR "/events/fprobes/poc/enable", "0");
	wa(TR "/dynamic_events", "-:fprobes/poc");
	return 0;
}
```

Thanks,
Weiming

#2 lore raw
In reply to Weiming Shi
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] fgraph: unnecessary Tasks RCU waits in unregister_ftrace_graph_finish() - [Low] ftrace: commit…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] fgraph: unnecessary Tasks RCU waits in unregister_ftrace_graph_finish()
- [Low] ftrace: commit message omits reproduction commands requested on v1
--

commit 74af33bd06338337070f4988ba978b843a4c1bea
Author: Weiming Shi <[email protected]>

Quoted 50 lines

ftrace: Finish removing subops after a hash update failure

When removing a non-final graph subop, ftrace_shutdown_subops() first detaches it, then rebuilds and installs the manager hashes. If rebuild_hashes() fails, the subop stays detached with its ENABLED and SUBOP flags and manager pointer intact. If ftrace_update_ops() fails, the subop is added back even though unregister_ftrace_graph() has already removed its graph slot.

In the fprobe path, ftrace_free_filter() then warns and returns, while __fprobe_graph_unregister() marks the shared graph operation unregistered. A later registration follows the stale SUBOP state into an empty manager list. rebuild_hashes() leaves its output pointers uninitialized in that case, and ops_equal() dereferences one of them.

Complete non-final removal on both manager-hash failure paths and initialize empty rebuild outputs to EMPTY_HASH. Keep direct dispatch disabled after an update failure because the old manager hashes may cover functions needed only by the removed subop; normal dispatch still applies the remaining subops' filters.

Completing removal also lets the caller free the subop filter. Split graph unregistration into removal and callback-wait helpers so Tasks RCU waits run before saved_func or the operation can be released. For asynchronous fprobe removal, remove the graph operation immediately and defer the wait and filter release to a worker that is flushed before reuse. Gate return callbacks on fprobe_graph_registered while cleanup is pending.

Oops: general protection fault, probably for non-canonical address 0xdffffc0000000802: 0000 [#1] SMP KASAN NOPTI
KASAN: probably user-memory-access in range [0x0000000000004010-0x0000000000004017]
RIP: ops_equal (kernel/trace/trace.h:1011 kernel/trace/ftrace.c:3416)
Call Trace:
 ftrace_update_ops (kernel/trace/ftrace.c:3464)
 ftrace_hash_move_and_update_subops.isra.0 (kernel/trace/ftrace.c:3795)
 ftrace_hash_move_and_update_ops (kernel/trace/ftrace.c:5095)
 ftrace_set_hash (kernel/trace/ftrace.c:6006)
 ftrace_set_filter_ips (kernel/trace/ftrace.c:6020 kernel/trace/ftrace.c:6769)
 register_fprobe_ips (kernel/trace/fprobe.c:290 kernel/trace/fprobe.c:1020)
 register_fprobe (kernel/trace/fprobe.c:979)
 __register_trace_fprobe.part.0 (kernel/trace/trace_fprobe.c:838)
 enable_trace_fprobe (kernel/trace/trace_fprobe.c:823 kernel/trace/trace_fprobe.c:1483)
 fprobe_register (kernel/trace/trace_fprobe.c:1553)
 __ftrace_event_enable_disable (kernel/trace/trace_events.c:913)
 event_enable_write (kernel/trace/trace_events.c:943 kernel/trace/trace_events.c:1995)
 vfs_write (fs/read_write.c:665)
 ksys_write (fs/read_write.c:719)
 do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89)
 entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
Kernel panic - not syncing: Fatal exception
Quoted 6 lines
Fixes 0ae6b8ce200d ("ftrace: Fix accounting of subop hashes")
Assisted-by LLM

[Severity: Low]
As reported by Steven Rostedt on v1 [1], could the commit message include the command lines that trigger the stale SUBOP state and the ops_equal() crash?

Quoted 4 lines
kernel/trace/fgraph.c +0 -0
index ed455b53513be..92186e7f5d698 100644

[ ... ]

Quoted 14 lines
kernel/trace/fgraph.c +7 -1
@@ -1460,7 +1478,28 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
 		unregister_pm_notifier(&ftrace_suspend_notifier);
 		unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
 	}
-	gops->saved_func = NULL;
  out:
 	gops->ops.flags &= ~FTRACE_OPS_FL_GRAPH;
+	return removed;
+}
+
+/**
+ * unregister_ftrace_graph_finish - wait for callbacks using a removed operation
+ * @gops: graph operation previously removed by unregister_ftrace_graph_start()
+ *

kernel/trace/fgraph.c void unregister_ftrace_graph(struct fgraph_ops *gops) · line 1499

Quoted 10 lines
kernel/trace/fgraph.c +10 -0
+ * Wait for existing graph readers, then clear the saved callback. This may
+ * sleep. The caller may release @gops and its filter after this returns.
+ */
+void unregister_ftrace_graph_finish(struct fgraph_ops *gops)
+{
+	/* Graph callbacks can run where RCU is not watching. */
+	synchronize_rcu_tasks_rude();
+	synchronize_rcu_tasks();
+	gops->saved_func = NULL;
+}

[Severity: Low]
As reported by Steven Rostedt on v1 [2], does unregister_ftrace_graph_finish() need synchronize_rcu_tasks_rude() and synchronize_rcu_tasks() instead of synchronize_rcu()?

Both function_graph_enter_regs() and __ftrace_return_to_handler() enter via ftrace_test_recursion_trylock() -> trace_test_and_set_recursion(), which checks that RCU is watching via trace_warn_on_no_rcu() and disables preemption across all graph callbacks. Using Tasks RCU here is inaccurate and stalls graph unregistration and fprobe cleanup waiting for voluntary context switches across all runnable tasks.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1