Skip to content

Commit 17ff39e

Browse files
Kent OverstreetKPD
authored andcommitted
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 <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>
1 parent b788ac1 commit 17ff39e

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

include/linux/jump_label.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ struct static_key {
107107
#endif /* CONFIG_JUMP_LABEL */
108108
};
109109

110+
typedef struct static_key static_key_t;
111+
110112
#endif /* __ASSEMBLY__ */
111113

112114
#ifdef CONFIG_JUMP_LABEL

include/linux/moduleparam.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ struct kparam_array
122122
* charp: a character pointer
123123
* bool: a bool, values 0/1, y/n, Y/N.
124124
* invbool: the above, only sense-reversed (N = true).
125+
* static_key_t: same as bool, but updates a 'struct static_key'
125126
*/
126127
#define module_param(name, type, perm) \
127128
module_param_named(name, name, type, perm)
@@ -488,6 +489,12 @@ extern int param_set_bint(const char *val, const struct kernel_param *kp);
488489
#define param_get_bint param_get_int
489490
#define param_check_bint param_check_int
490491

492+
/* A static key, which can only be set like a bool */
493+
extern const struct kernel_param_ops param_ops_static_key_t;
494+
extern int param_set_static_key_t(const char *val, const struct kernel_param *kp);
495+
extern int param_get_static_key_t(char *buffer, const struct kernel_param *kp);
496+
#define param_check_static_key_t(name, p) __param_check(name, p, struct static_key)
497+
491498
/**
492499
* module_param_array - a parameter which is an array of some type
493500
* @name: the name of the array variable

kernel/params.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/overflow.h>
1515
#include <linux/security.h>
1616
#include <linux/slab.h>
17+
#include <linux/static_key.h>
1718
#include <linux/string.h>
1819

1920
#ifdef CONFIG_SYSFS
@@ -412,6 +413,40 @@ const struct kernel_param_ops param_ops_bint = {
412413
};
413414
EXPORT_SYMBOL(param_ops_bint);
414415

416+
int param_set_static_key_t(const char *val, const struct kernel_param *kp)
417+
{
418+
/* Match bool exactly, by re-using it. */
419+
struct kernel_param boolkp = *kp;
420+
bool v;
421+
int ret;
422+
423+
boolkp.arg = &v;
424+
425+
ret = param_set_bool(val, &boolkp);
426+
if (ret)
427+
return ret;
428+
if (v)
429+
static_key_enable(kp->arg);
430+
else
431+
static_key_disable(kp->arg);
432+
return 0;
433+
}
434+
EXPORT_SYMBOL(param_set_static_key_t);
435+
436+
int param_get_static_key_t(char *buffer, const struct kernel_param *kp)
437+
{
438+
struct static_key *key = kp->arg;
439+
return sprintf(buffer, "%c\n", static_key_enabled(key) ? 'Y' : 'N');
440+
}
441+
EXPORT_SYMBOL(param_get_static_key_t);
442+
443+
const struct kernel_param_ops param_ops_static_key_t = {
444+
.flags = KERNEL_PARAM_OPS_FL_NOARG,
445+
.set = param_set_static_key_t,
446+
.get = param_get_static_key_t,
447+
};
448+
EXPORT_SYMBOL(param_ops_static_key_t);
449+
415450
/* We break the rule and mangle the string. */
416451
static int param_array(struct module *mod,
417452
const char *name,

0 commit comments

Comments
 (0)