[PATCH v2] params: Add support for static keys

Kent Overstreet posted 1 patch 8 months, 4 weeks ago
include/linux/jump_label.h  |  2 ++
include/linux/moduleparam.h |  7 +++++++
kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
3 files changed, 44 insertions(+)
[PATCH v2] params: Add support for static keys
Posted by Kent Overstreet 8 months, 4 weeks ago
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 <mcgrof@kernel.org>
Cc: Petr Pavlu <petr.pavlu@suse.com>
Cc: Sami Tolvanen <samitolvanen@google.com>
Cc: Daniel Gomez <da.gomez@samsung.com>
Cc: linux-modules@vger.kernel.org
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Josh Poimboeuf <jpoimboe@kernel.org>
Cc: Jason Baron <jbaron@akamai.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 include/linux/jump_label.h  |  2 ++
 include/linux/moduleparam.h |  7 +++++++
 kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+)

diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h
index fdb79dd1ebd8..0fc9b71db56f 100644
--- a/include/linux/jump_label.h
+++ b/include/linux/jump_label.h
@@ -107,6 +107,8 @@ struct static_key {
 #endif	/* CONFIG_JUMP_LABEL */
 };
 
+typedef struct static_key static_key_t;
+
 #endif /* __ASSEMBLY__ */
 
 #ifdef CONFIG_JUMP_LABEL
diff --git a/include/linux/moduleparam.h b/include/linux/moduleparam.h
index bfb85fd13e1f..11e8d5c57435 100644
--- a/include/linux/moduleparam.h
+++ b/include/linux/moduleparam.h
@@ -122,6 +122,7 @@ struct kparam_array
  *	charp: a character pointer
  *	bool: a bool, values 0/1, y/n, Y/N.
  *	invbool: the above, only sense-reversed (N = true).
+ *	static_key_t: same as bool, but updates a 'struct static_key'
  */
 #define module_param(name, type, perm)				\
 	module_param_named(name, name, type, perm)
@@ -488,6 +489,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
diff --git a/kernel/params.c b/kernel/params.c
index 2509f216c9f3..dd10ed125826 100644
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -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) ? 'Y' : 'N');
+}
+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,
-- 
2.49.0
Re: [PATCH v2] params: Add support for static keys
Posted by Josh Poimboeuf 8 months, 4 weeks ago
On Tue, May 13, 2025 at 09:07:32AM -0400, Kent Overstreet wrote:
> 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 <mcgrof@kernel.org>
> Cc: Petr Pavlu <petr.pavlu@suse.com>
> Cc: Sami Tolvanen <samitolvanen@google.com>
> Cc: Daniel Gomez <da.gomez@samsung.com>
> Cc: linux-modules@vger.kernel.org
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Josh Poimboeuf <jpoimboe@kernel.org>
> Cc: Jason Baron <jbaron@akamai.com>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
> ---
>  include/linux/jump_label.h  |  2 ++
>  include/linux/moduleparam.h |  7 +++++++
>  kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
>  3 files changed, 44 insertions(+)
> 
> diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h
> index fdb79dd1ebd8..0fc9b71db56f 100644
> --- a/include/linux/jump_label.h
> +++ b/include/linux/jump_label.h
> @@ -107,6 +107,8 @@ struct static_key {
>  #endif	/* CONFIG_JUMP_LABEL */
>  };
>  
> +typedef struct static_key static_key_t;
> +
>  #endif /* __ASSEMBLY__ */
>  
>  #ifdef CONFIG_JUMP_LABEL
> diff --git a/include/linux/moduleparam.h b/include/linux/moduleparam.h
> index bfb85fd13e1f..11e8d5c57435 100644
> --- a/include/linux/moduleparam.h
> +++ b/include/linux/moduleparam.h
> @@ -122,6 +122,7 @@ struct kparam_array
>   *	charp: a character pointer
>   *	bool: a bool, values 0/1, y/n, Y/N.
>   *	invbool: the above, only sense-reversed (N = true).
> + *	static_key_t: same as bool, but updates a 'struct static_key'

The static_key_*() interfaces are deprecated because they're really easy
to use incorrectly.  I don't think we want to propagate that misuse to
modules.

It would be better to have type(s) based on static_key_false and/or
static_key_true, depending on whatever the default is.

-- 
Josh
Re: [PATCH v2] params: Add support for static keys
Posted by Kent Overstreet 8 months, 4 weeks ago
On Tue, May 13, 2025 at 02:24:46PM -0700, Josh Poimboeuf wrote:
> On Tue, May 13, 2025 at 09:07:32AM -0400, Kent Overstreet wrote:
> > 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 <mcgrof@kernel.org>
> > Cc: Petr Pavlu <petr.pavlu@suse.com>
> > Cc: Sami Tolvanen <samitolvanen@google.com>
> > Cc: Daniel Gomez <da.gomez@samsung.com>
> > Cc: linux-modules@vger.kernel.org
> > Cc: Peter Zijlstra <peterz@infradead.org>
> > Cc: Josh Poimboeuf <jpoimboe@kernel.org>
> > Cc: Jason Baron <jbaron@akamai.com>
> > Cc: Steven Rostedt <rostedt@goodmis.org>
> > Cc: Ard Biesheuvel <ardb@kernel.org>
> > Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
> > ---
> >  include/linux/jump_label.h  |  2 ++
> >  include/linux/moduleparam.h |  7 +++++++
> >  kernel/params.c             | 35 +++++++++++++++++++++++++++++++++++
> >  3 files changed, 44 insertions(+)
> > 
> > diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h
> > index fdb79dd1ebd8..0fc9b71db56f 100644
> > --- a/include/linux/jump_label.h
> > +++ b/include/linux/jump_label.h
> > @@ -107,6 +107,8 @@ struct static_key {
> >  #endif	/* CONFIG_JUMP_LABEL */
> >  };
> >  
> > +typedef struct static_key static_key_t;
> > +
> >  #endif /* __ASSEMBLY__ */
> >  
> >  #ifdef CONFIG_JUMP_LABEL
> > diff --git a/include/linux/moduleparam.h b/include/linux/moduleparam.h
> > index bfb85fd13e1f..11e8d5c57435 100644
> > --- a/include/linux/moduleparam.h
> > +++ b/include/linux/moduleparam.h
> > @@ -122,6 +122,7 @@ struct kparam_array
> >   *	charp: a character pointer
> >   *	bool: a bool, values 0/1, y/n, Y/N.
> >   *	invbool: the above, only sense-reversed (N = true).
> > + *	static_key_t: same as bool, but updates a 'struct static_key'
> 
> The static_key_*() interfaces are deprecated because they're really easy
> to use incorrectly.  I don't think we want to propagate that misuse to
> modules.
> 
> It would be better to have type(s) based on static_key_false and/or
> static_key_true, depending on whatever the default is.

Except those are just wrappers around struct static_key, so why does
that matter here?
Re: [PATCH v2] params: Add support for static keys
Posted by Josh Poimboeuf 8 months, 4 weeks ago
On Tue, May 13, 2025 at 07:34:59PM -0400, Kent Overstreet wrote:
> On Tue, May 13, 2025 at 02:24:46PM -0700, Josh Poimboeuf wrote:
> > > +++ b/include/linux/moduleparam.h
> > > @@ -122,6 +122,7 @@ struct kparam_array
> > >   *	charp: a character pointer
> > >   *	bool: a bool, values 0/1, y/n, Y/N.
> > >   *	invbool: the above, only sense-reversed (N = true).
> > > + *	static_key_t: same as bool, but updates a 'struct static_key'
> > 
> > The static_key_*() interfaces are deprecated because they're really easy
> > to use incorrectly.  I don't think we want to propagate that misuse to
> > modules.
> > 
> > It would be better to have type(s) based on static_key_false and/or
> > static_key_true, depending on whatever the default is.
> 
> Except those are just wrappers around struct static_key, so why does
> that matter here?

Those struct wrappers are needed to work with static_branch_likely() and
static_branch_unlikely().

We really need to get rid of old static_key_true() and
static_key_false(), they don't have many users left at this point.

-- 
Josh
Re: [PATCH v2] params: Add support for static keys
Posted by Kent Overstreet 8 months, 4 weeks ago
On Tue, May 13, 2025 at 05:37:11PM -0700, Josh Poimboeuf wrote:
> On Tue, May 13, 2025 at 07:34:59PM -0400, Kent Overstreet wrote:
> > On Tue, May 13, 2025 at 02:24:46PM -0700, Josh Poimboeuf wrote:
> > > > +++ b/include/linux/moduleparam.h
> > > > @@ -122,6 +122,7 @@ struct kparam_array
> > > >   *	charp: a character pointer
> > > >   *	bool: a bool, values 0/1, y/n, Y/N.
> > > >   *	invbool: the above, only sense-reversed (N = true).
> > > > + *	static_key_t: same as bool, but updates a 'struct static_key'
> > > 
> > > The static_key_*() interfaces are deprecated because they're really easy
> > > to use incorrectly.  I don't think we want to propagate that misuse to
> > > modules.
> > > 
> > > It would be better to have type(s) based on static_key_false and/or
> > > static_key_true, depending on whatever the default is.
> > 
> > Except those are just wrappers around struct static_key, so why does
> > that matter here?
> 
> Those struct wrappers are needed to work with static_branch_likely() and
> static_branch_unlikely().

Sure, but this has no bearing on that - unless I've missed them, there
aren't separate methods for just setting and checking the value, which
is all we're doing here.
Re: [PATCH v2] params: Add support for static keys
Posted by Josh Poimboeuf 8 months, 4 weeks ago
On Tue, May 13, 2025 at 08:44:49PM -0400, Kent Overstreet wrote:
> On Tue, May 13, 2025 at 05:37:11PM -0700, Josh Poimboeuf wrote:
> > On Tue, May 13, 2025 at 07:34:59PM -0400, Kent Overstreet wrote:
> > > On Tue, May 13, 2025 at 02:24:46PM -0700, Josh Poimboeuf wrote:
> > > > > +++ b/include/linux/moduleparam.h
> > > > > @@ -122,6 +122,7 @@ struct kparam_array
> > > > >   *	charp: a character pointer
> > > > >   *	bool: a bool, values 0/1, y/n, Y/N.
> > > > >   *	invbool: the above, only sense-reversed (N = true).
> > > > > + *	static_key_t: same as bool, but updates a 'struct static_key'
> > > > 
> > > > The static_key_*() interfaces are deprecated because they're really easy
> > > > to use incorrectly.  I don't think we want to propagate that misuse to
> > > > modules.
> > > > 
> > > > It would be better to have type(s) based on static_key_false and/or
> > > > static_key_true, depending on whatever the default is.
> > > 
> > > Except those are just wrappers around struct static_key, so why does
> > > that matter here?
> > 
> > Those struct wrappers are needed to work with static_branch_likely() and
> > static_branch_unlikely().
> 
> Sure, but this has no bearing on that - unless I've missed them, there
> aren't separate methods for just setting and checking the value, which
> is all we're doing here.

To make use of this feature, wouldn't the module need to use
static_key_false() or so to actually insert the static branch to check
the value?  Otherwise what's the point of using static keys here?

-- 
Josh
Re: [PATCH v2] params: Add support for static keys
Posted by Kent Overstreet 8 months, 4 weeks ago
On Tue, May 13, 2025 at 08:38:57PM -0700, Josh Poimboeuf wrote:
> On Tue, May 13, 2025 at 08:44:49PM -0400, Kent Overstreet wrote:
> > On Tue, May 13, 2025 at 05:37:11PM -0700, Josh Poimboeuf wrote:
> > > On Tue, May 13, 2025 at 07:34:59PM -0400, Kent Overstreet wrote:
> > > > On Tue, May 13, 2025 at 02:24:46PM -0700, Josh Poimboeuf wrote:
> > > > > > +++ b/include/linux/moduleparam.h
> > > > > > @@ -122,6 +122,7 @@ struct kparam_array
> > > > > >   *	charp: a character pointer
> > > > > >   *	bool: a bool, values 0/1, y/n, Y/N.
> > > > > >   *	invbool: the above, only sense-reversed (N = true).
> > > > > > + *	static_key_t: same as bool, but updates a 'struct static_key'
> > > > > 
> > > > > The static_key_*() interfaces are deprecated because they're really easy
> > > > > to use incorrectly.  I don't think we want to propagate that misuse to
> > > > > modules.
> > > > > 
> > > > > It would be better to have type(s) based on static_key_false and/or
> > > > > static_key_true, depending on whatever the default is.
> > > > 
> > > > Except those are just wrappers around struct static_key, so why does
> > > > that matter here?
> > > 
> > > Those struct wrappers are needed to work with static_branch_likely() and
> > > static_branch_unlikely().
> > 
> > Sure, but this has no bearing on that - unless I've missed them, there
> > aren't separate methods for just setting and checking the value, which
> > is all we're doing here.
> 
> To make use of this feature, wouldn't the module need to use
> static_key_false() or so to actually insert the static branch to check
> the value?  Otherwise what's the point of using static keys here?

I'm not sure I follow?

You just pass the inner static_key to the modparam, you still use
static_key_true or static_key_false as normal.
Re: [PATCH v2] params: Add support for static keys
Posted by Josh Poimboeuf 8 months, 4 weeks ago
On Tue, May 13, 2025 at 11:40:05PM -0400, Kent Overstreet wrote:
> On Tue, May 13, 2025 at 08:38:57PM -0700, Josh Poimboeuf wrote:
> > On Tue, May 13, 2025 at 08:44:49PM -0400, Kent Overstreet wrote:
> > > On Tue, May 13, 2025 at 05:37:11PM -0700, Josh Poimboeuf wrote:
> > > > On Tue, May 13, 2025 at 07:34:59PM -0400, Kent Overstreet wrote:
> > > > > On Tue, May 13, 2025 at 02:24:46PM -0700, Josh Poimboeuf wrote:
> > > > > > > +++ b/include/linux/moduleparam.h
> > > > > > > @@ -122,6 +122,7 @@ struct kparam_array
> > > > > > >   *	charp: a character pointer
> > > > > > >   *	bool: a bool, values 0/1, y/n, Y/N.
> > > > > > >   *	invbool: the above, only sense-reversed (N = true).
> > > > > > > + *	static_key_t: same as bool, but updates a 'struct static_key'
> > > > > > 
> > > > > > The static_key_*() interfaces are deprecated because they're really easy
> > > > > > to use incorrectly.  I don't think we want to propagate that misuse to
> > > > > > modules.
> > > > > > 
> > > > > > It would be better to have type(s) based on static_key_false and/or
> > > > > > static_key_true, depending on whatever the default is.
> > > > > 
> > > > > Except those are just wrappers around struct static_key, so why does
> > > > > that matter here?
> > > > 
> > > > Those struct wrappers are needed to work with static_branch_likely() and
> > > > static_branch_unlikely().
> > > 
> > > Sure, but this has no bearing on that - unless I've missed them, there
> > > aren't separate methods for just setting and checking the value, which
> > > is all we're doing here.
> > 
> > To make use of this feature, wouldn't the module need to use
> > static_key_false() or so to actually insert the static branch to check
> > the value?  Otherwise what's the point of using static keys here?
> 
> I'm not sure I follow?
> 
> You just pass the inner static_key to the modparam, you still use
> static_key_true or static_key_false as normal.

Ok, so the caller does something like so?

  DEFINE_STATIC_KEY_FALSE(foo);
  module_param_named(foo, foo.key, static_key_t, 0);

I guess that works, but 'foo.key' is awkward in that it's nonobvious and
breaks the abstraction.  Driver people might end up allocating
static_key directly and using the deprecated interfaces again.

My preference would be to stick to the supported interfaces, e.g.:

  DEFINE_STATIC_KEY_FALSE(foo);
  module_param_named(foo, foo, static_key_false_t, 0);

-- 
Josh
Re: [PATCH v2] params: Add support for static keys
Posted by Kent Overstreet 8 months, 4 weeks ago
On Wed, May 14, 2025 at 12:06:44PM -0700, Josh Poimboeuf wrote:
> On Tue, May 13, 2025 at 11:40:05PM -0400, Kent Overstreet wrote:
> > On Tue, May 13, 2025 at 08:38:57PM -0700, Josh Poimboeuf wrote:
> > > On Tue, May 13, 2025 at 08:44:49PM -0400, Kent Overstreet wrote:
> > > > On Tue, May 13, 2025 at 05:37:11PM -0700, Josh Poimboeuf wrote:
> > > > > On Tue, May 13, 2025 at 07:34:59PM -0400, Kent Overstreet wrote:
> > > > > > On Tue, May 13, 2025 at 02:24:46PM -0700, Josh Poimboeuf wrote:
> > > > > > > > +++ b/include/linux/moduleparam.h
> > > > > > > > @@ -122,6 +122,7 @@ struct kparam_array
> > > > > > > >   *	charp: a character pointer
> > > > > > > >   *	bool: a bool, values 0/1, y/n, Y/N.
> > > > > > > >   *	invbool: the above, only sense-reversed (N = true).
> > > > > > > > + *	static_key_t: same as bool, but updates a 'struct static_key'
> > > > > > > 
> > > > > > > The static_key_*() interfaces are deprecated because they're really easy
> > > > > > > to use incorrectly.  I don't think we want to propagate that misuse to
> > > > > > > modules.
> > > > > > > 
> > > > > > > It would be better to have type(s) based on static_key_false and/or
> > > > > > > static_key_true, depending on whatever the default is.
> > > > > > 
> > > > > > Except those are just wrappers around struct static_key, so why does
> > > > > > that matter here?
> > > > > 
> > > > > Those struct wrappers are needed to work with static_branch_likely() and
> > > > > static_branch_unlikely().
> > > > 
> > > > Sure, but this has no bearing on that - unless I've missed them, there
> > > > aren't separate methods for just setting and checking the value, which
> > > > is all we're doing here.
> > > 
> > > To make use of this feature, wouldn't the module need to use
> > > static_key_false() or so to actually insert the static branch to check
> > > the value?  Otherwise what's the point of using static keys here?
> > 
> > I'm not sure I follow?
> > 
> > You just pass the inner static_key to the modparam, you still use
> > static_key_true or static_key_false as normal.
> 
> Ok, so the caller does something like so?
> 
>   DEFINE_STATIC_KEY_FALSE(foo);
>   module_param_named(foo, foo.key, static_key_t, 0);
> 
> I guess that works, but 'foo.key' is awkward in that it's nonobvious and
> breaks the abstraction.  Driver people might end up allocating
> static_key directly and using the deprecated interfaces again.
> 
> My preference would be to stick to the supported interfaces, e.g.:
> 
>   DEFINE_STATIC_KEY_FALSE(foo);
>   module_param_named(foo, foo, static_key_false_t, 0);

Yeah, that looks reasonable