Skip to contentSkip to conversation

kprobes: use DEFINE_DEBUGFS_ATTRIBUTE for the enabled knob

PATCH Superseded Bradley Morgan 4 participants 7 messages Aug 18

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kprobes: use DEFINE_DEBUGFS_ATTRIBUTE for the enabled knob

This enabled knob is a disgusting terrible hack. It has rolled its own read/write pair since 2007, writing '1' or '0' into a three byte buffer by hand just to print a single character, with an XXX comment begging debugfs for write callbacks on bool files. DEFINE_DEBUGFS_ATTRIBUTE showed up in 2016 and does exactly that, so the disgusting terrible hack has outlived its excuse for nine years. Kill it, and the stale comment with it.

The behavior does not change, except the write only accepts 0/1 now instead of y/n/on/off, and nothing uses anything else.

---
 kernel/kprobes.c | 43 +++++++++----------------------------------
 1 file changed, 9 insertions(+), 34 deletions(-)
kernel/kprobes.c +9 -34
index bfc89083daa9..044c6b5fd2aa 100644
@@ -3013,47 +3013,22 @@ static int disarm_all_kprobes(void)
 	return ret;
 }
 
-/*
- * XXX: The debugfs bool file interface doesn't allow for callbacks
- * when the bool state is switched. We can reuse that facility when
- * available
- */
-static ssize_t read_enabled_file_bool(struct file *file,
-	       char __user *user_buf, size_t count, loff_t *ppos)
+static int kprobes_enabled_set(void *data, u64 val)
 {
-	char buf[3];
+	if (val)
+		return arm_all_kprobes();
 
-	if (!kprobes_all_disarmed)
-		buf[0] = '1';
-	else
-		buf[0] = '0';
-	buf[1] = '\n';
-	buf[2] = 0x00;
-	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
+	return disarm_all_kprobes();
 }
 
-static ssize_t write_enabled_file_bool(struct file *file,
-	       const char __user *user_buf, size_t count, loff_t *ppos)
+static int kprobes_enabled_get(void *data, u64 *val)
 {
-	bool enable;
-	int ret;
-
-	ret = kstrtobool_from_user(user_buf, count, &enable);
-	if (ret)
-		return ret;
-
-	ret = enable ? arm_all_kprobes() : disarm_all_kprobes();
-	if (ret)
-		return ret;
-
-	return count;
+	*val = !kprobes_all_disarmed;
+	return 0;
 }
 
-static const struct file_operations fops_kp = {
-	.read =         read_enabled_file_bool,
-	.write =        write_enabled_file_bool,
-	.llseek =	default_llseek,
-};
+DEFINE_DEBUGFS_ATTRIBUTE(fops_kp, kprobes_enabled_get,
+			 kprobes_enabled_set, "%llu\n");
 
 static int __init debugfs_kprobe_init(void)
 {
Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The 'enabled' debugfs file is created using `debugfs_create_file()` instead of…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The 'enabled' debugfs file is created using debugfs_create_file() instead of debugfs_create_file_unsafe() with a DEFINE_DEBUGFS_ATTRIBUTE struct, leading to redundant proxying and overhead. --

--- Patch [1]: [PATCH] kprobes: use DEFINE_DEBUGFS_ATTRIBUTE for the enabled knob --- commit f5babbd146ccf4194f28506d04f5cbfb2c40150a
Author: Bradley Morgan <[email protected]>

kprobes: use DEFINE_DEBUGFS_ATTRIBUTE for the enabled knob

This commit replaces the custom read/write handlers for the debugfs enabled file with the standard DEFINE_DEBUGFS_ATTRIBUTE macro. It simplifies the implementation and accepts integer inputs rather than characters.

Quoted 5 lines
kernel/kprobes.c +0 -0
index ca9d834d0b843..10bcc2a0926a6 100644
@@ -2962,47 +2962,22 @@ static int disarm_all_kprobes(void)

[ ... ]

kernel/kprobes.c static int disarm_all_kprobes(void) · line 3034

Quoted 10 lines
kernel/kprobes.c +2 -5
-static const struct file_operations fops_kp = {
-	.read =         read_enabled_file_bool,
-	.write =        write_enabled_file_bool,
-	.llseek =	default_llseek,
-};
+DEFINE_DEBUGFS_ATTRIBUTE(fops_kp, kprobes_enabled_get,
+			 kprobes_enabled_set, "%llu\n");
 
 static int __init debugfs_kprobe_init(void)
 {

[Severity: Low]
Will this lead to double-proxying for the debugfs file?

Since DEFINE_DEBUGFS_ATTRIBUTE natively provides debugfs_attr_read and debugfs_attr_write operations that already include debugfs_file_get() and debugfs_file_put() protection, passing fops_kp to debugfs_create_file() in debugfs_kprobe_init() will cause the debugfs core to wrap it again:

kernel/kprobes.c:debugfs_kprobe_init() {
    ...
	debugfs_create_file("enabled", 0600, dir, NULL, &fops_kp);
    ...
}

Could this use debugfs_create_file_unsafe() instead?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 3 lines · On 18 August 2026 03:01:18 BST, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider:
- [Low] The 'enabled' debugfs file is created using debugfs_create_file() instead of debugfs_create_file_unsafe() with a DEFINE_DEBUGFS_ATTRIBUTE struct, leading to redundant proxying and overhead.

[email protected]

I hate sashiko so damn much, best to wait for review!

Quoted 40 lines

--

--- Patch [1]: [PATCH] kprobes: use DEFINE_DEBUGFS_ATTRIBUTE for the enabled knob --- commit f5babbd146ccf4194f28506d04f5cbfb2c40150a
Author: Bradley Morgan <[email protected]>

kprobes: use DEFINE_DEBUGFS_ATTRIBUTE for the enabled knob

This commit replaces the custom read/write handlers for the debugfs enabled
file with the standard DEFINE_DEBUGFS_ATTRIBUTE macro. It simplifies the implementation and accepts integer inputs rather than characters.

Quoted 5 lines
kernel/kprobes.c +0 -0
index ca9d834d0b843..10bcc2a0926a6 100644
@@ -2962,47 +2962,22 @@ static int disarm_all_kprobes(void)

[ ... ]

kernel/kprobes.c static int disarm_all_kprobes(void) · line 3034

Quoted 10 lines
kernel/kprobes.c +2 -5
-static const struct file_operations fops_kp = {
-	.read =         read_enabled_file_bool,
-	.write =        write_enabled_file_bool,
-	.llseek =	default_llseek,
-};
+DEFINE_DEBUGFS_ATTRIBUTE(fops_kp, kprobes_enabled_get,
+			 kprobes_enabled_set, "%llu\n");
 
 static int __init debugfs_kprobe_init(void)
 {

[Severity: Low]
Will this lead to double-proxying for the debugfs file?

Since DEFINE_DEBUGFS_ATTRIBUTE natively provides debugfs_attr_read and debugfs_attr_write operations that already include debugfs_file_get() and debugfs_file_put() protection, passing fops_kp to debugfs_create_file() in debugfs_kprobe_init() will cause the debugfs core to wrap it again:

Quoted 9 lines
kernel/kprobes.c:debugfs_kprobe_init() {
   ...
	debugfs_create_file("enabled", 0600, dir, NULL, &fops_kp);
   ...
}

Could this use debugfs_create_file_unsafe() instead?

Thanks!

maintainer
#3 lore raw
Quoted 8 lines · On Tue, 18 Aug 2026 16:17:20 +0100 Bradley Morgan <[email protected]> wrote:
Quoted 3 lines · On 18 August 2026 03:01:18 BST, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider:
- [Low] The 'enabled' debugfs file is created using debugfs_create_file() instead of debugfs_create_file_unsafe() with a DEFINE_DEBUGFS_ATTRIBUTE struct, leading to redundant proxying and overhead.

[email protected]

I hate sashiko so damn much, best to wait for review!

I found sashiko to be extremely useful.

Quoted 10 lines
Quoted 10 lines

[ ... ]

Quoted 8 lines
kernel/kprobes.c +2 -5
-static const struct file_operations fops_kp = {
-	.read =         read_enabled_file_bool,
-	.write =        write_enabled_file_bool,
-	.llseek =	default_llseek,
-};
+DEFINE_DEBUGFS_ATTRIBUTE(fops_kp, kprobes_enabled_get,
+			 kprobes_enabled_set, "%llu\n");
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

static int __init debugfs_kprobe_init(void)
{

[Severity: Low]

It is even saying this is of "low priority". That means it's more of an "FYI".

Quoted 7 lines
Quoted 7 lines

Will this lead to double-proxying for the debugfs file?

Since DEFINE_DEBUGFS_ATTRIBUTE natively provides debugfs_attr_read and debugfs_attr_write operations that already include debugfs_file_get() and debugfs_file_put() protection, passing fops_kp to debugfs_create_file() in debugfs_kprobe_init() will cause the debugfs core to wrap it again:

Quoted 7 lines
Quoted 7 lines
kernel/kprobes.c:debugfs_kprobe_init() {
   ...
	debugfs_create_file("enabled", 0600, dir, NULL, &fops_kp);
   ...
}

Could this use debugfs_create_file_unsafe() instead?

As this isn't a critical path, I don't think we really care here if it's wrapped or not. But it's nice to know that it is.

Anyway, I'll let others review this, but it looks fine to me.

-- Steve

#4 lore raw
Quoted 50 lines · Steven Rostedt
Quoted 4 lines · On Tue, 18 Aug 2026 16:17:20 +0100 Bradley Morgan <[email protected]> wrote:
Quoted 3 lines · On 18 August 2026 03:01:18 BST, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The 'enabled' debugfs file is created using

debugfs_create_file() instead of debugfs_create_file_unsafe() with a DEFINE_DEBUGFS_ATTRIBUTE struct, leading to redundant proxying and overhead.

Quoted 4 lines

[email protected]

I hate sashiko so damn much, best to wait for review!

I found sashiko to be extremely useful.

Quoted 10 lines
Quoted 10 lines

[ ... ]

Quoted 8 lines
kernel/kprobes.c +2 -5
-static const struct file_operations fops_kp = {
-	.read =         read_enabled_file_bool,
-	.write =        write_enabled_file_bool,
-	.llseek =	default_llseek,
-};
+DEFINE_DEBUGFS_ATTRIBUTE(fops_kp, kprobes_enabled_get,
+			 kprobes_enabled_set, "%llu\n");
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

static int __init debugfs_kprobe_init(void)
{

[Severity: Low]

It is even saying this is of "low priority". That means it's more of an "FYI".

Quoted 4 lines
Quoted 4 lines

Will this lead to double-proxying for the debugfs file?

Since DEFINE_DEBUGFS_ATTRIBUTE natively provides debugfs_attr_read and debugfs_attr_write operations that already include debugfs_file_get()

and

Quoted 9 lines
Quoted 9 lines

debugfs_file_put() protection, passing fops_kp to debugfs_create_file() in debugfs_kprobe_init() will cause the debugfs core to wrap it again:

kernel/kprobes.c:debugfs_kprobe_init() {
   ...
	debugfs_create_file("enabled", 0600, dir, NULL, &fops_kp);
   ...
}
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Could this use debugfs_create_file_unsafe() instead?

As this isn't a critical path, I don't think we really care here if it's wrapped or not. But it's nice to know that it is.

Anyway, I'll let others review this, but it looks fine to me.

-- Steve

the fix is a couple of years in the making, heh
Thanks!

#5 lore raw
In reply to Bradley Morgan
Quoted 7 lines · On Tue, 18 Aug 2026 01:49:20 +0000 Bradley Morgan <[email protected]> wrote:

This enabled knob is a disgusting terrible hack. It has rolled its own read/write pair since 2007, writing '1' or '0' into a three byte buffer by hand just to print a single character, with an XXX comment begging debugfs for write callbacks on bool files. DEFINE_DEBUGFS_ATTRIBUTE showed up in 2016 and does exactly that, so the disgusting terrible hack has outlived its excuse for nine years. Kill it, and the stale comment with it.

Hm, OK, but please calm down. Just describe the technical reason here.

Quoted 3 lines

The behavior does not change, except the write only accepts 0/1 now instead of y/n/on/off, and nothing uses anything else.

I think this comment is needed.

Please add Signed-off-by.

The code looks good to me.

Thanks,

Quoted 68 lines
---
 kernel/kprobes.c | 43 +++++++++----------------------------------
 1 file changed, 9 insertions(+), 34 deletions(-)
kernel/kprobes.c +9 -34
index bfc89083daa9..044c6b5fd2aa 100644
@@ -3013,47 +3013,22 @@ static int disarm_all_kprobes(void)
 	return ret;
 }
 
-/*
- * XXX: The debugfs bool file interface doesn't allow for callbacks
- * when the bool state is switched. We can reuse that facility when
- * available
- */
-static ssize_t read_enabled_file_bool(struct file *file,
-	       char __user *user_buf, size_t count, loff_t *ppos)
+static int kprobes_enabled_set(void *data, u64 val)
 {
-	char buf[3];
+	if (val)
+		return arm_all_kprobes();
 
-	if (!kprobes_all_disarmed)
-		buf[0] = '1';
-	else
-		buf[0] = '0';
-	buf[1] = '\n';
-	buf[2] = 0x00;
-	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
+	return disarm_all_kprobes();
 }
 
-static ssize_t write_enabled_file_bool(struct file *file,
-	       const char __user *user_buf, size_t count, loff_t *ppos)
+static int kprobes_enabled_get(void *data, u64 *val)
 {
-	bool enable;
-	int ret;
-
-	ret = kstrtobool_from_user(user_buf, count, &enable);
-	if (ret)
-		return ret;
-
-	ret = enable ? arm_all_kprobes() : disarm_all_kprobes();
-	if (ret)
-		return ret;
-
-	return count;
+	*val = !kprobes_all_disarmed;
+	return 0;
 }
 
-static const struct file_operations fops_kp = {
-	.read =         read_enabled_file_bool,
-	.write =        write_enabled_file_bool,
-	.llseek =	default_llseek,
-};
+DEFINE_DEBUGFS_ATTRIBUTE(fops_kp, kprobes_enabled_get,
+			 kprobes_enabled_set, "%llu\n");
 
 static int __init debugfs_kprobe_init(void)
 {
Signature · 1 line
2.47.3
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#6 lore raw
Quoted 16 lines · Masami Hiramatsu
Quoted 7 lines · On Tue, 18 Aug 2026 01:49:20 +0000 Bradley Morgan <[email protected]> wrote:

This enabled knob is a disgusting terrible hack. It has rolled its own read/write pair since 2007, writing '1' or '0' into a three byte buffer by hand just to print a single character, with an XXX comment begging debugfs for write callbacks on bool files. DEFINE_DEBUGFS_ATTRIBUTE showed up in 2016 and does exactly that, so the disgusting terrible hack has outlived its excuse for nine years. Kill it, and the stale comment with it.

Hm, OK, but please calm down. Just describe the technical reason here.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

The behavior does not change, except the write only accepts 0/1 now instead of y/n/on/off, and nothing uses anything else.

I think this comment is needed.

What do you suggest?

I'm saying

/* Let's kill this hack */

(I love using the word hack!)

Quoted 2 lines

Please add Signed-off-by.

V2 acks it

Quoted 67 lines

The code looks good to me.

Thanks,

Quoted 63 lines
---
 kernel/kprobes.c | 43 +++++++++----------------------------------
 1 file changed, 9 insertions(+), 34 deletions(-)
kernel/kprobes.c +9 -34
index bfc89083daa9..044c6b5fd2aa 100644
@@ -3013,47 +3013,22 @@ static int disarm_all_kprobes(void)
 	return ret;
 }
 
-/*
- * XXX: The debugfs bool file interface doesn't allow for callbacks
- * when the bool state is switched. We can reuse that facility when
- * available
- */
-static ssize_t read_enabled_file_bool(struct file *file,
-	       char __user *user_buf, size_t count, loff_t *ppos)
+static int kprobes_enabled_set(void *data, u64 val)
 {
-	char buf[3];
+	if (val)
+		return arm_all_kprobes();
 
-	if (!kprobes_all_disarmed)
-		buf[0] = '1';
-	else
-		buf[0] = '0';
-	buf[1] = '\n';
-	buf[2] = 0x00;
-	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
+	return disarm_all_kprobes();
 }
 
-static ssize_t write_enabled_file_bool(struct file *file,
-	       const char __user *user_buf, size_t count, loff_t *ppos)
+static int kprobes_enabled_get(void *data, u64 *val)
 {
-	bool enable;
-	int ret;
-
-	ret = kstrtobool_from_user(user_buf, count, &enable);
-	if (ret)
-		return ret;
-
-	ret = enable ? arm_all_kprobes() : disarm_all_kprobes();
-	if (ret)
-		return ret;
-
-	return count;
+	*val = !kprobes_all_disarmed;
+	return 0;
 }
 
-static const struct file_operations fops_kp = {
-	.read =         read_enabled_file_bool,
-	.write =        write_enabled_file_bool,
-	.llseek =	default_llseek,
-};
+DEFINE_DEBUGFS_ATTRIBUTE(fops_kp, kprobes_enabled_get,
+			 kprobes_enabled_set, "%llu\n");
 
Quoted 8 lines
Quoted 5 lines
kernel/kprobes.c +0 -0
 static int __init debugfs_kprobe_init(void)
 {
Signature · 1 line
2.47.3

Thanks!