Skip to contentSkip to conversation

params: Add support for static keys

PATCH Superseded Kent Overstreet 3 participants 5 messages May 10, 2025 – May 14, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 params: Add support for static keys

Static keys can now be a module parameter, e.g.

module_param_named(foo, foo.key, static_key_t, 0644)

bcachefs is now using this.

Cc Luis Chamberlain <[email protected]>
Cc Petr Pavlu <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Daniel Gomez <[email protected]>
---
 include/linux/jump_label.h  |  2 ++
 include/linux/moduleparam.h |  6 ++++++
 kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+)
include/linux/jump_label.h +2 -0
index fdb79dd1ebd8..0fc9b71db56f 100644
@@ -107,6 +107,8 @@ struct static_key {
 #endif	/* CONFIG_JUMP_LABEL */
 };
 
+typedef struct static_key static_key_t;
+
 #endif /* __ASSEMBLY__ */
 
 #ifdef CONFIG_JUMP_LABEL
include/linux/moduleparam.h +6 -0
index bfb85fd13e1f..2494e7e67453 100644
@@ -488,6 +488,12 @@ extern int param_set_bint(const char *val, const struct kernel_param *kp);
 #define param_get_bint param_get_int
 #define param_check_bint param_check_int
 
+/* A static key, which can only be set like a bool */
+extern const struct kernel_param_ops param_ops_static_key_t;
+extern int param_set_static_key_t(const char *val, const struct kernel_param *kp);
+extern int param_get_static_key_t(char *buffer, const struct kernel_param *kp);
+#define param_check_static_key_t(name, p) __param_check(name, p, struct static_key)
+
 /**
  * module_param_array - a parameter which is an array of some type
  * @name: the name of the array variable
kernel/params.c +35 -0
index 2509f216c9f3..991f49e138e7 100644
@@ -14,6 +14,7 @@
 #include <linux/overflow.h>
 #include <linux/security.h>
 #include <linux/slab.h>
+#include <linux/static_key.h>
 #include <linux/string.h>
 
 #ifdef CONFIG_SYSFS
@@ -412,6 +413,40 @@ const struct kernel_param_ops param_ops_bint = {
 };
 EXPORT_SYMBOL(param_ops_bint);
 
+int param_set_static_key_t(const char *val, const struct kernel_param *kp)
+{
+	/* Match bool exactly, by re-using it. */
+	struct kernel_param boolkp = *kp;
+	bool v;
+	int ret;
+
+	boolkp.arg = &v;
+
+	ret = param_set_bool(val, &boolkp);
+	if (ret)
+		return ret;
+	if (v)
+		static_key_enable(kp->arg);
+	else
+		static_key_disable(kp->arg);
+	return 0;
+}
+EXPORT_SYMBOL(param_set_static_key_t);
+
+int param_get_static_key_t(char *buffer, const struct kernel_param *kp)
+{
+	struct static_key *key = kp->arg;
+	return sprintf(buffer, "%c\n", static_key_enabled(key) ? 'N' : 'Y');
+}
+EXPORT_SYMBOL(param_get_static_key_t);
+
+const struct kernel_param_ops param_ops_static_key_t = {
+	.flags = KERNEL_PARAM_OPS_FL_NOARG,
+	.set = param_set_static_key_t,
+	.get = param_get_static_key_t,
+};
+EXPORT_SYMBOL(param_ops_static_key_t);
+
 /* We break the rule and mangle the string. */
 static int param_array(struct module *mod,
 		       const char *name,
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 11 lines · Kent Overstreet

Static keys can now be a module parameter, e.g.

module_param_named(foo, foo.key, static_key_t, 0644)

bcachefs is now using this.

Cc Luis Chamberlain <[email protected]>
Cc Petr Pavlu <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Daniel Gomez <[email protected]>

Please Cc also the "STATIC BRANCH/CALL" folks on the next version.

include/linux/moduleparam.h

Quoted 24 lines
---
 include/linux/jump_label.h  |  2 ++
 include/linux/moduleparam.h |  6 ++++++
 kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+)
include/linux/jump_label.h +2 -0
index fdb79dd1ebd8..0fc9b71db56f 100644
@@ -107,6 +107,8 @@ struct static_key {
 #endif	/* CONFIG_JUMP_LABEL */
 };
 
+typedef struct static_key static_key_t;
+
 #endif /* __ASSEMBLY__ */
 
 #ifdef CONFIG_JUMP_LABEL
include/linux/moduleparam.h +0 -0
index bfb85fd13e1f..2494e7e67453 100644

The module_param() macro in this file has a kernel-doc that describes all currently supported standard types:

* Standard types are:
* byte, hexint, short, ushort, int, uint, long, ulong
* charp: a character pointer
* bool: a bool, values 0/1, y/n, Y/N.
* invbool: the above, only sense-reversed (N = true).

The static_key_t should be added to this list.

Quoted 43 lines
include/linux/moduleparam.h +6 -0
@@ -488,6 +488,12 @@ extern int param_set_bint(const char *val, const struct kernel_param *kp);
 #define param_get_bint param_get_int
 #define param_check_bint param_check_int
 
+/* A static key, which can only be set like a bool */
+extern const struct kernel_param_ops param_ops_static_key_t;
+extern int param_set_static_key_t(const char *val, const struct kernel_param *kp);
+extern int param_get_static_key_t(char *buffer, const struct kernel_param *kp);
+#define param_check_static_key_t(name, p) __param_check(name, p, struct static_key)
+
 /**
  * module_param_array - a parameter which is an array of some type
  * @name: the name of the array variable
kernel/params.c +15 -0
index 2509f216c9f3..991f49e138e7 100644
@@ -14,6 +14,7 @@
 #include <linux/overflow.h>
 #include <linux/security.h>
 #include <linux/slab.h>
+#include <linux/static_key.h>
 #include <linux/string.h>
 
 #ifdef CONFIG_SYSFS
@@ -412,6 +413,40 @@ const struct kernel_param_ops param_ops_bint = {
 };
 EXPORT_SYMBOL(param_ops_bint);
 
+int param_set_static_key_t(const char *val, const struct kernel_param *kp)
+{
+	/* Match bool exactly, by re-using it. */
+	struct kernel_param boolkp = *kp;
+	bool v;
+	int ret;
+
+	boolkp.arg = &v;
+
+	ret = param_set_bool(val, &boolkp);
+	if (ret)
+		return ret;
+	if (v)
+		static_key_enable(kp->arg);

kernel/params.c const struct kernel_param_ops param_ops_bint = { · line 439

Quoted 10 lines
kernel/params.c +10 -0
+	else
+		static_key_disable(kp->arg);
+	return 0;
+}
+EXPORT_SYMBOL(param_set_static_key_t);
+
+int param_get_static_key_t(char *buffer, const struct kernel_param *kp)
+{
+	struct static_key *key = kp->arg;
+	return sprintf(buffer, "%c\n", static_key_enabled(key) ? 'N' : 'Y');

The 'N'/'Y' values are the other way around.

Quoted 13 lines
kernel/params.c +10 -0
+}
+EXPORT_SYMBOL(param_get_static_key_t);
+
+const struct kernel_param_ops param_ops_static_key_t = {
+	.flags = KERNEL_PARAM_OPS_FL_NOARG,
+	.set = param_set_static_key_t,
+	.get = param_get_static_key_t,
+};
+EXPORT_SYMBOL(param_ops_static_key_t);
+
 /* We break the rule and mangle the string. */
 static int param_array(struct module *mod,
 		       const char *name,
Signature · 2 lines
Thanks
Petr
#2 lore raw
Quoted 98 lines · Petr Pavlu
Quoted 11 lines · Kent Overstreet

Static keys can now be a module parameter, e.g.

module_param_named(foo, foo.key, static_key_t, 0644)

bcachefs is now using this.

Cc Luis Chamberlain <[email protected]>
Cc Petr Pavlu <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Daniel Gomez <[email protected]>

Please Cc also the "STATIC BRANCH/CALL" folks on the next version.

include/linux/moduleparam.h

Quoted 24 lines
---
 include/linux/jump_label.h  |  2 ++
 include/linux/moduleparam.h |  6 ++++++
 kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+)
include/linux/jump_label.h +2 -0
index fdb79dd1ebd8..0fc9b71db56f 100644
@@ -107,6 +107,8 @@ struct static_key {
 #endif	/* CONFIG_JUMP_LABEL */
 };
 
+typedef struct static_key static_key_t;
+
 #endif /* __ASSEMBLY__ */
 
 #ifdef CONFIG_JUMP_LABEL
include/linux/moduleparam.h +0 -0
index bfb85fd13e1f..2494e7e67453 100644

The module_param() macro in this file has a kernel-doc that describes all currently supported standard types:

* Standard types are:
* byte, hexint, short, ushort, int, uint, long, ulong
* charp: a character pointer
* bool: a bool, values 0/1, y/n, Y/N.
* invbool: the above, only sense-reversed (N = true).

The static_key_t should be added to this list.

Quoted 47 lines
include/linux/moduleparam.h +6 -0
@@ -488,6 +488,12 @@ extern int param_set_bint(const char *val, const struct kernel_param *kp);
 #define param_get_bint param_get_int
 #define param_check_bint param_check_int
 
+/* A static key, which can only be set like a bool */
+extern const struct kernel_param_ops param_ops_static_key_t;
+extern int param_set_static_key_t(const char *val, const struct kernel_param *kp);
+extern int param_get_static_key_t(char *buffer, const struct kernel_param *kp);
+#define param_check_static_key_t(name, p) __param_check(name, p, struct static_key)
+
 /**
  * module_param_array - a parameter which is an array of some type
  * @name: the name of the array variable
kernel/params.c +19 -0
index 2509f216c9f3..991f49e138e7 100644
@@ -14,6 +14,7 @@
 #include <linux/overflow.h>
 #include <linux/security.h>
 #include <linux/slab.h>
+#include <linux/static_key.h>
 #include <linux/string.h>
 
 #ifdef CONFIG_SYSFS
@@ -412,6 +413,40 @@ const struct kernel_param_ops param_ops_bint = {
 };
 EXPORT_SYMBOL(param_ops_bint);
 
+int param_set_static_key_t(const char *val, const struct kernel_param *kp)
+{
+	/* Match bool exactly, by re-using it. */
+	struct kernel_param boolkp = *kp;
+	bool v;
+	int ret;
+
+	boolkp.arg = &v;
+
+	ret = param_set_bool(val, &boolkp);
+	if (ret)
+		return ret;
+	if (v)
+		static_key_enable(kp->arg);
+	else
+		static_key_disable(kp->arg);
+	return 0;
+}
Quoted 8 lines

kernel/params.c const struct kernel_param_ops param_ops_bint = { · line 439

Quoted 6 lines
kernel/params.c +6 -0
+EXPORT_SYMBOL(param_set_static_key_t);
+
+int param_get_static_key_t(char *buffer, const struct kernel_param *kp)
+{
+	struct static_key *key = kp->arg;
+	return sprintf(buffer, "%c\n", static_key_enabled(key) ? 'N' : 'Y');

The 'N'/'Y' values are the other way around.

Thanks, fixed these

#3 lore raw
In reply to Kent Overstreet

Le 10/05/2025 à 23:01, Kent Overstreet a écrit :

Quoted 23 lines

Static keys can now be a module parameter, e.g.

module_param_named(foo, foo.key, static_key_t, 0644)

bcachefs is now using this.

Cc Luis Chamberlain <[email protected]>
Cc Petr Pavlu <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Daniel Gomez <[email protected]>
---
  include/linux/jump_label.h  |  2 ++
  include/linux/moduleparam.h |  6 ++++++
  kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
  3 files changed, 43 insertions(+)
include/linux/jump_label.h +0 -0
index fdb79dd1ebd8..0fc9b71db56f 100644

include/linux/jump_label.h struct static_key { · line 111

Quoted 5 lines
include/linux/jump_label.h +2 -0
@@ -107,6 +107,8 @@ struct static_key {
  #endif	/* CONFIG_JUMP_LABEL */
  };
 
+typedef struct static_key static_key_t;
+

Please read https://docs.kernel.org/process/coding-style.html#typedefs

I can't see the reason for a new typedef here. If there is a valid reason please explain in the commit message.

Christophe

#4 lore raw
Quoted 33 lines · Christophe Leroy

Le 10/05/2025 à 23:01, Kent Overstreet a écrit :

Quoted 23 lines

Static keys can now be a module parameter, e.g.

module_param_named(foo, foo.key, static_key_t, 0644)

bcachefs is now using this.

Cc Luis Chamberlain <[email protected]>
Cc Petr Pavlu <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Daniel Gomez <[email protected]>
---
  include/linux/jump_label.h  |  2 ++
  include/linux/moduleparam.h |  6 ++++++
  kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
  3 files changed, 43 insertions(+)
include/linux/jump_label.h +0 -0
index fdb79dd1ebd8..0fc9b71db56f 100644

include/linux/jump_label.h struct static_key { · line 110

Quoted 4 lines
include/linux/jump_label.h +2 -0
@@ -107,6 +107,8 @@ struct static_key {
  #endif	/* CONFIG_JUMP_LABEL */
  };
+typedef struct static_key static_key_t;
+

Please read https://docs.kernel.org/process/coding-style.html#typedefs

Quoted 2 lines

I can't see the reason for a new typedef here. If there is a valid reason please explain in the commit message.

You can't token paste with a type that's two tokens - that's basic C.