Skip to contentSkip to conversation

ftrace: deprecate the ftrace_enabled disable switch

RFC PATCH Superseded Andrey Grodzovsky 4 participants 7 messages Jul 31 – Aug 6

Subsystems (7)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover ftrace: deprecate the ftrace_enabled disable switch

This adresses a long-standing issue: kernel.ftrace_enabled=0 silently disables BPF trampolines (fentry/fexit) and ftrace-based kprobes/kretprobes. The write succeeds, the hook stops firing with no error, and re-enabling silently restores it.

The solution chosen is to deny setting this knob to 0 from userspace, thus preventing this case in the first place. Steven mentioned that the switch became effectively useless and doesn't serve any meaningful purpose anymore, and only creates problems for systems that rely on ftrace, such as Livepatching and eBPF. Any attempt to set it to 0 will fail with -EOPNOTSUPP. Reading and writing 1 remain unchanged.

Patch 1: the sysctl change plus a doc note.
Patch 2: updates the one selftest that relied on the old disable behavior.

Changes since v1 :
- test-ftrace.sh: keep the full original disable/reload scenario on kernels where kernel.ftrace_enabled=0 still works, and only fall back to the simple "write is refused" check on kernels that deprecate the knob.(Steven)

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

Andrey Grodzovsky (2):
ftrace: deprecate disabling via ftrace_enabled sysctl selftests/livepatch: update test-ftrace.sh for deprecated
ftrace_enabled

 Documentation/trace/ftrace.rst                |  5 +++
 kernel/trace/ftrace.c                         | 19 ++++----
 .../testing/selftests/livepatch/functions.sh  | 13 ++++++
 .../selftests/livepatch/test-ftrace.sh        | 45 ++++++++++++-------
 4 files changed, 54 insertions(+), 28 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH v2 1/2 ftrace: deprecate disabling via ftrace_enabled sysctl

Writing 0 to kernel.ftrace_enabled has not reliably disabled ftrace for years (FTRACE_OPS_FL_PERMANENT users already block it, and more callers rely on ftrace always being on). Refuse the write instead of leaving it in an inconsistent "disables some, not all" state: return -EOPNOTSUPP and log a message. Reads and enabling (writing 1) are unaffected.

Update the docs to note the deprecation up front.

---
 Documentation/trace/ftrace.rst |  5 +++++
 kernel/trace/ftrace.c          | 19 ++++++++-----------
 2 files changed, 13 insertions(+), 11 deletions(-)
Documentation/trace/ftrace.rst +5 -0
index 84f06bf0da9b..7261f25f8b4b 100644
@@ -3313,6 +3313,11 @@ this special filter via::
 ftrace_enabled
 --------------
 
+.. note::
+   Disabling ftrace via this switch is deprecated. Writing 0 is refused
+   with -EOPNOTSUPP and logs a warning; writing 1 and reading the value
+   are unaffected.
+
 Note, the proc sysctl ftrace_enable is a big on/off switch for the
 function tracer. By default it is enabled (when function tracing is
 enabled in the kernel). If it is disabled, all function tracing is
kernel/trace/ftrace.c +8 -11
index f93e34dd2328..82bb7356ccce 100644
@@ -9357,7 +9357,7 @@ static void ftrace_startup_sysctl(void)
 	}
 }
 
-static void ftrace_shutdown_sysctl(void)
+static void __maybe_unused ftrace_shutdown_sysctl(void)
 {
 	int command;
 
@@ -9377,7 +9377,7 @@ static void ftrace_shutdown_sysctl(void)
 # define ftrace_shutdown_sysctl()      do { } while (0)
 #endif /* CONFIG_DYNAMIC_FTRACE */
 
-static bool is_permanent_ops_registered(void)
+static bool __maybe_unused is_permanent_ops_registered(void)
 {
 	struct ftrace_ops *op;
 
@@ -9415,15 +9415,12 @@ ftrace_enable_sysctl(const struct ctl_table *table, int write,
 		ftrace_startup_sysctl();
 
 	} else {
-		if (is_permanent_ops_registered()) {
-			ftrace_enabled = true;
-			return -EBUSY;
-		}
-
-		/* stopping ftrace calls (just send to ftrace_stub) */
-		ftrace_trace_function = ftrace_stub;
-
-		ftrace_shutdown_sysctl();
+		/*
+		 * Disabling ftrace at runtime via this knob is deprecated.
+		 */
+		ftrace_enabled = true;
+		pr_warn_once("The ftrace_enabled file is deprecated and no longer disables ftrace\n");
+		return -EOPNOTSUPP;
 	}
 
 	last_ftrace_enabled = !!ftrace_enabled;
Signature · 1 line
2.34.1
#2 lore raw

Re: [RFC PATCH bpf-next v2 1/2] ftrace: deprecate disabling via ftrace_enabled sysctl

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The patch breaks livepatch kselftests by deprecating and rejecting writes to…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The patch breaks livepatch kselftests by deprecating and rejecting writes to kernel.ftrace_enabled without updating the corresponding tests.
- [Low] Unused static functions ftrace_shutdown_sysctl and is_permanent_ops_registered were retained and marked with __maybe_unused instead of being properly removed. --

--- Patch [1]: [RFC PATCH bpf-next v2 1/2] ftrace: deprecate disabling via ftrace_enabled sysctl --- commit 6675b51e1176bc76210101b0a6a9da0c7a32dc59
Author: Andrey Grodzovsky <[email protected]>

ftrace: deprecate disabling via ftrace_enabled sysctl

This commit deprecates disabling ftrace via the kernel.ftrace_enabled sysctl. It refuses writes of 0 by returning -EOPNOTSUPP and logging a warning, rather than leaving the system in an inconsistent state.

kernel/trace/ftrace.c static void ftrace_startup_sysctl(void) · line 9360

Quoted 10 lines
kernel/trace/ftrace.c +1 -1
index f93e34dd23288..82bb7356ccce5 100644
@@ -9357,7 +9357,7 @@ static void ftrace_startup_sysctl(void)
 	}
 }
 
-static void ftrace_shutdown_sysctl(void)
+static void __maybe_unused ftrace_shutdown_sysctl(void)

[Severity: Low]
Since the only caller of ftrace_shutdown_sysctl() has been removed by this patch, should this function be deleted entirely rather than kept around with a __maybe_unused annotation?

Quoted 2 lines
kernel/trace/ftrace.c +0 -0
 {
 	int command;

[ ... ]

kernel/trace/ftrace.c static void ftrace_shutdown_sysctl(void) · line 9380

Quoted 6 lines
kernel/trace/ftrace.c +1 -1
@@ -9377,7 +9377,7 @@ static void ftrace_shutdown_sysctl(void)
 # define ftrace_shutdown_sysctl()      do { } while (0)
 #endif /* CONFIG_DYNAMIC_FTRACE */
 
-static bool is_permanent_ops_registered(void)
+static bool __maybe_unused is_permanent_ops_registered(void)

[Severity: Low]
Similarly, is_permanent_ops_registered() appears to be completely dead code now. Can this be safely removed instead of marking it __maybe_unused?

Quoted 2 lines
kernel/trace/ftrace.c +0 -0
 {
 	struct ftrace_ops *op;

[ ... ]

Quoted 9 lines
kernel/trace/ftrace.c +0 -5
@@ -9415,15 +9415,12 @@ ftrace_enable_sysctl(const struct ctl_table *table, int write,
 		ftrace_startup_sysctl();
 
 	} else {
-		if (is_permanent_ops_registered()) {
-			ftrace_enabled = true;
-			return -EBUSY;
-		}
-

kernel/trace/ftrace.c ftrace_enable_sysctl(const struct ctl_table *table, int write, · line 9423

Quoted 10 lines
kernel/trace/ftrace.c +6 -4
-		/* stopping ftrace calls (just send to ftrace_stub) */
-		ftrace_trace_function = ftrace_stub;
-
-		ftrace_shutdown_sysctl();
+		/*
+		 * Disabling ftrace at runtime via this knob is deprecated.
+		 */
+		ftrace_enabled = true;
+		pr_warn_once("The ftrace_enabled file is deprecated and no longer disables ftrace\n");
+		return -EOPNOTSUPP;

[Severity: Medium]
Will rejecting writes of 0 here break the livepatch kselftests?

Looking at tools/testing/selftests/livepatch/test-ftrace.sh, the scripts explicitly toggle this sysctl and expect either success or an -EBUSY error when a livepatch is active:

tools/testing/selftests/livepatch/test-ftrace.sh:

    set_ftrace_enabled 0
    load_failing_mod $MOD_LIVEPATCH
    ...
    # Check that ftrace could not get disabled when a livepatch is enabled
    set_ftrace_enabled --fail 0

Now that this returns -EOPNOTSUPP unconditionally, it looks like these tests will fail or be skipped. Do the livepatch kselftests need to be updated alongside this change?

Quoted 3 lines
kernel/trace/ftrace.c +0 -0
 	}
 
 	last_ftrace_enabled = !!ftrace_enabled;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

Re: [RFC PATCH bpf-next v2 1/2] ftrace: deprecate disabling via ftrace_enabled sysctl

In reply to Andrey Grodzovsky
Quoted 7 lines · On Fri, 31 Jul 2026 13:53:57 -0400 Andrey Grodzovsky…
kernel/trace/ftrace.c +1 -1
-static void ftrace_shutdown_sysctl(void)
+static void __maybe_unused ftrace_shutdown_sysctl(void)
 {
 	int command;

kernel/trace/ftrace.c static void ftrace_shutdown_sysctl(void) · line 9382

Quoted 7 lines
kernel/trace/ftrace.c +1 -1
@@ -9377,7 +9377,7 @@ static void ftrace_shutdown_sysctl(void)
 # define ftrace_shutdown_sysctl()      do { } while (0)
 #endif /* CONFIG_DYNAMIC_FTRACE */
 
-static bool is_permanent_ops_registered(void)
+static bool __maybe_unused is_permanent_ops_registered(void)
 {
 	struct ftrace_ops *op;

I agree with Sashiko: https://sashiko.dev/#/patchset/20260731175358.3542156-3-andrey.grodzovsky%40crowdstrike.com

Instead of marking with "__maybe_unused", just delete them. If we need to revent, the revert will add them back.

-- Steve

#4 lore raw

RFC PATCH v2 2/2 selftests/livepatch: update test-ftrace.sh for deprecated ftrace_enabled

In reply to Andrey Grodzovsky

kernel.ftrace_enabled=0 is now refused on kernels that deprecate the knob, so the old disable/reload flow no longer applies there. Probe for this with ftrace_disable_supported() and keep the full original scenario (disable, fail to load a livepatch, re-enable, load, confirm disable is refused while loaded) on kernels where it still works; otherwise just confirm the write is refused.

Assisted-by Claude:claude-sonnet-5
---
 .../testing/selftests/livepatch/functions.sh  | 13 ++++++
 .../selftests/livepatch/test-ftrace.sh        | 45 ++++++++++++-------
 2 files changed, 41 insertions(+), 17 deletions(-)
tools/testing/selftests/livepatch/functions.sh +13 -0
index 30dc677b2f45..7fa4bda70221 100644
@@ -126,6 +126,19 @@ function set_ftrace_enabled() {
 	echo "livepatch: kernel.ftrace_enabled = $result" > /dev/kmsg
 }
 
+# ftrace_disable_supported() - probe whether kernel.ftrace_enabled=0
+#	can still disable ftrace on this kernel. Newer kernels deprecate
+#	the knob and always refuse the write with -EOPNOTSUPP.
+function ftrace_disable_supported() {
+	local result
+
+	sysctl -q kernel.ftrace_enabled=0 &> /dev/null
+	result=$(sysctl --values kernel.ftrace_enabled)
+	sysctl -q kernel.ftrace_enabled=1 &> /dev/null
+
+	[[ "$result" == "0" ]]
+}
+
 function cleanup() {
 	pop_config
 }
tools/testing/selftests/livepatch/test-ftrace.sh +28 -17

63 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#5 lore raw

Re: [RFC PATCH bpf-next v2 2/2] selftests/livepatch: update test-ftrace.sh for deprecated ftrace_enabled

Quoted 26 lines · Andrey Grodzovsky

kernel.ftrace_enabled=0 is now refused on kernels that deprecate the knob, so the old disable/reload flow no longer applies there. Probe for this with ftrace_disable_supported() and keep the full original scenario (disable, fail to load a livepatch, re-enable, load, confirm disable is refused while loaded) on kernels where it still works; otherwise just confirm the write is refused.

Assisted-by Claude:claude-sonnet-5
---
 .../testing/selftests/livepatch/functions.sh  | 13 ++++++
 .../selftests/livepatch/test-ftrace.sh        | 45 ++++++++++++-------
 2 files changed, 41 insertions(+), 17 deletions(-)
tools/testing/selftests/livepatch/functions.sh +2 -0
index 30dc677b2f45..7fa4bda70221 100644
@@ -126,6 +126,19 @@ function set_ftrace_enabled() {
 	echo "livepatch: kernel.ftrace_enabled = $result" > /dev/kmsg
 }
 
+# ftrace_disable_supported() - probe whether kernel.ftrace_enabled=0
+#	can still disable ftrace on this kernel. Newer kernels deprecate

tools/testing/selftests/livepatch/functions.sh function set_ftrace_enabled() { · line 140

Quoted 10 lines
tools/testing/selftests/livepatch/functions.sh +10 -0
+#	the knob and always refuse the write with -EOPNOTSUPP.
+function ftrace_disable_supported() {
+	local result
+
+	sysctl -q kernel.ftrace_enabled=0 &> /dev/null
+	result=$(sysctl --values kernel.ftrace_enabled)
+	sysctl -q kernel.ftrace_enabled=1 &> /dev/null
+
+	[[ "$result" == "0" ]]
+}

Small nit: AFAICT, it doesn't really affect the test pattern, but it would be a bit cleaner if this function restored the original value instead of always turning it on, like (untested):

	orig=$(sysctl --values kernel.ftrace_enabled)
	sysctl -q kernel.ftrace_enabled=0 &> /dev/null
	result=$(sysctl --values kernel.ftrace_enabled)
	sysctl -q "kernel.ftrace_enabled=$orig" &> /dev/null

	[[ "$result" == "0" ]]

--
Joe

#6 lore raw

Re: [RFC PATCH bpf-next v2 2/2] selftests/livepatch: update test-ftrace.sh for deprecated ftrace_enabled

Thank you Joe and Steven for your comments; I sent V3.

Andrey

Quoted 53 lines · Joe Lawrence
Quoted 26 lines · Andrey Grodzovsky

kernel.ftrace_enabled=0 is now refused on kernels that deprecate the knob, so the old disable/reload flow no longer applies there. Probe for this with ftrace_disable_supported() and keep the full original scenario (disable, fail to load a livepatch, re-enable, load, confirm disable is refused while loaded) on kernels where it still works; otherwise just confirm the write is refused.

Assisted-by Claude:claude-sonnet-5
---
 .../testing/selftests/livepatch/https://urldefense.com/v3/__http://functions.sh__;!!BmdzS3_lV9HdKG8!zXXcFqHACaJ7PyoFMFh-_8DFTwWSotPltPcR5ezYC87VaVkoNZcqFkVTtLbYUQp4DKEh9Z-0WLN8c5sMeyug2BCCuslAibAtHJo$   | 13 ++++++
 .../selftests/livepatch/https://urldefense.com/v3/__http://test-ftrace.sh__;!!BmdzS3_lV9HdKG8!zXXcFqHACaJ7PyoFMFh-_8DFTwWSotPltPcR5ezYC87VaVkoNZcqFkVTtLbYUQp4DKEh9Z-0WLN8c5sMeyug2BCCuslAJf9tNxM$         | 45 ++++++++++++-------
 2 files changed, 41 insertions(+), 17 deletions(-)
tools/testing/selftests/livepatch/https://urldefense.com/v3/__http://functions.sh__;!!BmdzS3_lV9HdKG8!zXXcFqHACaJ7PyoFMFh-_8DFTwWSotPltPcR5ezYC87VaVkoNZcqFkVTtLbYUQp4DKEh9Z-0WLN8c5sMeyug2BCCuslAibAtHJo$ +2 -0
index 30dc677b2f45..7fa4bda70221 100644
@@ -126,6 +126,19 @@ function set_ftrace_enabled() {
      echo "livepatch: kernel.ftrace_enabled = $result" > /dev/kmsg
 }
 
+# ftrace_disable_supported() - probe whether kernel.ftrace_enabled=0
+#    can still disable ftrace on this kernel. Newer kernels deprecate

tools/testing/selftests/livepatch/https://urldefense.com/v3/__http://functions.sh__;!!BmdzS3_lV9HdKG8!zXXcFqHACaJ7PyoFMFh-_8DFTwWSotPltPcR5ezYC87VaVkoNZcqFkVTtLbYUQp4DKEh9Z-0WLN8c5sMeyug2BCCuslAibAtHJo$ function set_ftrace_enabled() { · line 140

Quoted 10 lines
tools/testing/selftests/livepatch/https://urldefense.com/v3/__http://functions.sh__;!!BmdzS3_lV9HdKG8!zXXcFqHACaJ7PyoFMFh-_8DFTwWSotPltPcR5ezYC87VaVkoNZcqFkVTtLbYUQp4DKEh9Z-0WLN8c5sMeyug2BCCuslAibAtHJo$ +10 -0
+#    the knob and always refuse the write with -EOPNOTSUPP.
+function ftrace_disable_supported() {
+     local result
+
+     sysctl -q kernel.ftrace_enabled=0 &> /dev/null
+     result=$(sysctl --values kernel.ftrace_enabled)
+     sysctl -q kernel.ftrace_enabled=1 &> /dev/null
+
+     [[ "$result" == "0" ]]
+}

Small nit: AFAICT, it doesn't really affect the test pattern, but it would be a bit cleaner if this function restored the original value instead of always turning it on, like (untested):

        orig=$(sysctl --values kernel.ftrace_enabled)
        sysctl -q kernel.ftrace_enabled=0 &> /dev/null
        result=$(sysctl --values kernel.ftrace_enabled)
        sysctl -q "kernel.ftrace_enabled=$orig" &> /dev/null

        [[ "$result" == "0" ]]

--
Joe