linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/1] add support for enum module parameters
@ 2022-04-14 12:30 Jani Nikula
  2022-04-14 12:30 ` [PATCH 1/1] module: add enum module parameter type to map names to values Jani Nikula
  2022-04-14 13:19 ` [PATCH 0/1] add support for enum module parameters Greg Kroah-Hartman
  0 siblings, 2 replies; 9+ messages in thread
From: Jani Nikula @ 2022-04-14 12:30 UTC (permalink / raw)
  To: linux-kernel
  Cc: intel-gfx, dri-devel, jani.nikula, Andrew Morton,
	Greg Kroah-Hartman, Lucas De Marchi

Hey, I've sent this before, ages ago, but haven't really followed
through with it. I still think it would be useful for many scenarios
where a plain number is a clumsy interface for a module param.

Thoughts?


BR,
Jani.

Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Lucas De Marchi <lucas.demarchi@intel.com>

Jani Nikula (1):
  module: add enum module parameter type to map names to values

 include/linux/moduleparam.h | 64 +++++++++++++++++++++++++++++++++++++
 kernel/params.c             | 41 ++++++++++++++++++++++++
 2 files changed, 105 insertions(+)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/1] module: add enum module parameter type to map names to values
  2022-04-14 12:30 [PATCH 0/1] add support for enum module parameters Jani Nikula
@ 2022-04-14 12:30 ` Jani Nikula
  2022-04-14 13:19 ` [PATCH 0/1] add support for enum module parameters Greg Kroah-Hartman
  1 sibling, 0 replies; 9+ messages in thread
From: Jani Nikula @ 2022-04-14 12:30 UTC (permalink / raw)
  To: linux-kernel
  Cc: intel-gfx, dri-devel, jani.nikula, Andrew Morton,
	Greg Kroah-Hartman, Lucas De Marchi

Add enum module parameter type that's internally an int and externally
maps names to values. This makes the userspace interface more intuitive
to use and somewhat easier to document, while also limiting the allowed
values set by userspace to the defined set.

For example, given this code to define a "mode" in a fictional module
"foobar":

	struct module_param_enumeration modes[] = {
		{ "foo", 0 },
		{ "bar", 1 },
		{ "baz", -1 },
	};

	int mode;

	module_param_enum(mode, modes, 0600);

You can probe foobar with "foobar.mode=bar" in the kernel or modprobe
command line to set the mode to 1.

Similarly, you can use the sysfs with the names:

	# echo baz > /sys/module/foobar/parameters/mode
	# cat /sys/module/foobar/parameters/mode
	baz

With checks:

	# echo nope > /sys/module/foobar/parameters/mode
	echo: write error: Invalid argument

Of course, the kernel can still internally set the mode variable
directly to a value that is not defined in the enumerations (obviously
to be avoided), which will result in unknown key:

	# cat /sys/module/foobar/parameters/mode
	(unknown)

Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
---
 include/linux/moduleparam.h | 64 +++++++++++++++++++++++++++++++++++++
 kernel/params.c             | 41 ++++++++++++++++++++++++
 2 files changed, 105 insertions(+)

diff --git a/include/linux/moduleparam.h b/include/linux/moduleparam.h
index 962cd41a2cb5..a11fb8f214e5 100644
--- a/include/linux/moduleparam.h
+++ b/include/linux/moduleparam.h
@@ -77,6 +77,7 @@ struct kernel_param {
 		void *arg;
 		const struct kparam_string *str;
 		const struct kparam_array *arr;
+		const struct kparam_enum *enumeration;
 	};
 };
 
@@ -98,6 +99,19 @@ struct kparam_array
 	void *elem;
 };
 
+/* Special ones for enums */
+struct module_param_enumeration {
+	const char *key;
+	int val;
+};
+
+struct kparam_enum
+{
+	const struct module_param_enumeration *enums;
+	unsigned int num_enums;
+	int *val;
+};
+
 /**
  * module_param - typesafe helper for a module/cmdline parameter
  * @name: the variable to alter, and exposed parameter name.
@@ -484,6 +498,11 @@ 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
 
+/* An enumeration */
+extern const struct kernel_param_ops param_ops_enum;
+extern int param_set_enum(const char *key, const struct kernel_param *kp);
+extern int param_get_enum(char *buffer, const struct kernel_param *kp);
+
 /**
  * module_param_array - a parameter which is an array of some type
  * @name: the name of the array variable
@@ -523,6 +542,51 @@ extern int param_set_bint(const char *val, const struct kernel_param *kp);
 			    perm, -1, 0);				\
 	__MODULE_PARM_TYPE(name, "array of " #type)
 
+/**
+ * module_param_enum - a parameter which is an enumeration
+ * @name: the variable to alter, and exposed parameter name
+ * @enumerations: array of struct module_param_enumeration defining the enums
+ * @perm: visibility in sysfs
+ *
+ * The userspace input and output are based on the names defined in the
+ * @enumerations array, which maps the names to values stored in the int
+ * variable defined by @name.
+ *
+ * When initializing or changing the variable @name, ensure the value is defined
+ * in @enumerations. Otherwise, reading the parameter value via sysfs will
+ * output "(unknown)".
+ *
+ * ARRAY_SIZE(@enumerations) is used to determine the number of elements in the
+ * enumerations array, so the definition must be visible.
+ */
+
+#define module_param_enum(name, enumerations, perm)			\
+	module_param_enum_named(name, name, enumerations, perm)
+
+/**
+ * module_param_enum_named - a renamed parameter which is an enumeration
+ * @name: a valid C identifier which is the parameter name
+ * @value: the actual lvalue int variable to alter
+ * @enumerations: array of struct module_param_enumeration defining the enums
+ * @perm: visibility in sysfs
+ *
+ * This exposes a different name than the actual variable name.  See
+ * module_param_named() for why this might be necessary.
+ */
+#define module_param_enum_named(name, value, enumerations, perm)	\
+	param_check_int(name, &(value));				\
+	static const struct kparam_enum __param_arr_##name =		\
+	{								\
+		.enums = enumerations,					\
+		.num_enums = ARRAY_SIZE(enumerations),			\
+		.val = &value						\
+	};								\
+	__module_param_call(MODULE_PARAM_PREFIX, name,			\
+			    &param_ops_enum,				\
+			    .enumeration = &__param_arr_##name,		\
+			    perm, -1, 0);				\
+	__MODULE_PARM_TYPE(name, "enumeration")
+
 enum hwparam_type {
 	hwparam_ioport,		/* Module parameter configures an I/O port */
 	hwparam_iomem,		/* Module parameter configures an I/O mem address */
diff --git a/kernel/params.c b/kernel/params.c
index 5b92310425c5..749fe42b1a44 100644
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -534,6 +534,47 @@ const struct kernel_param_ops param_ops_string = {
 };
 EXPORT_SYMBOL(param_ops_string);
 
+int param_set_enum(const char *key, const struct kernel_param *kp)
+{
+	const struct kparam_enum *e = kp->enumeration;
+	unsigned int i;
+
+	for (i = 0; i < e->num_enums; i++) {
+		if (sysfs_streq(key, e->enums[i].key)) {
+			*(e->val) = e->enums[i].val;
+			return 0;
+		}
+	}
+
+	pr_err("%s: unknown key %s to enum parameter\n", kp->name, key);
+
+	return -EINVAL;
+}
+EXPORT_SYMBOL(param_set_enum);
+
+int param_get_enum(char *buffer, const struct kernel_param *kp)
+{
+	const struct kparam_enum *e = kp->enumeration;
+	unsigned int i;
+
+	for (i = 0; i < e->num_enums; i++) {
+		if (*(e->val) == e->enums[i].val)
+			return sysfs_emit(buffer, "%s\n", e->enums[i].key);
+	}
+
+	pr_err("%s: enum parameter set to unknown value %d\n",
+	       kp->name, *(e->val));
+
+	return sysfs_emit(buffer, "(unknown)\n");
+}
+EXPORT_SYMBOL(param_get_enum);
+
+const struct kernel_param_ops param_ops_enum = {
+	.set = param_set_enum,
+	.get = param_get_enum,
+};
+EXPORT_SYMBOL(param_ops_enum);
+
 /* sysfs output in /sys/modules/XYZ/parameters/ */
 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/1] add support for enum module parameters
  2022-04-14 12:30 [PATCH 0/1] add support for enum module parameters Jani Nikula
  2022-04-14 12:30 ` [PATCH 1/1] module: add enum module parameter type to map names to values Jani Nikula
@ 2022-04-14 13:19 ` Greg Kroah-Hartman
  2022-04-14 14:22   ` Jani Nikula
  1 sibling, 1 reply; 9+ messages in thread
From: Greg Kroah-Hartman @ 2022-04-14 13:19 UTC (permalink / raw)
  To: Jani Nikula
  Cc: linux-kernel, intel-gfx, dri-devel, Andrew Morton, Lucas De Marchi

On Thu, Apr 14, 2022 at 03:30:32PM +0300, Jani Nikula wrote:
> Hey, I've sent this before, ages ago, but haven't really followed
> through with it. I still think it would be useful for many scenarios
> where a plain number is a clumsy interface for a module param.
> 
> Thoughts?

We should not be adding new module parameters anyway (they operate on
code, not data/devices), so what would this be used for?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/1] add support for enum module parameters
  2022-04-14 13:19 ` [PATCH 0/1] add support for enum module parameters Greg Kroah-Hartman
@ 2022-04-14 14:22   ` Jani Nikula
  2022-04-14 14:29     ` Greg Kroah-Hartman
  2022-04-20  5:13     ` Kalle Valo
  0 siblings, 2 replies; 9+ messages in thread
From: Jani Nikula @ 2022-04-14 14:22 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: linux-kernel, intel-gfx, dri-devel, Andrew Morton, Lucas De Marchi

On Thu, 14 Apr 2022, Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
> On Thu, Apr 14, 2022 at 03:30:32PM +0300, Jani Nikula wrote:
>> Hey, I've sent this before, ages ago, but haven't really followed
>> through with it. I still think it would be useful for many scenarios
>> where a plain number is a clumsy interface for a module param.
>> 
>> Thoughts?
>
> We should not be adding new module parameters anyway (they operate on
> code, not data/devices), so what would this be used for?

I think it's just easier to use names than random values, and this also
gives you range check on the input.

I also keep telling people not to add new module parameters, but it's
not like they're going away anytime soon.

If there's a solution to being able to pass device specific debug
parameters at probe time, I'm all ears. At least i915 has a bunch of
things which can't really be changed after probe, when debugfs for the
device is around. Module parameters aren't ideal, but debugfs doesn't
work for this.


BR,
Jani.

-- 
Jani Nikula, Intel Open Source Graphics Center

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/1] add support for enum module parameters
  2022-04-14 14:22   ` Jani Nikula
@ 2022-04-14 14:29     ` Greg Kroah-Hartman
  2022-04-20  5:13     ` Kalle Valo
  1 sibling, 0 replies; 9+ messages in thread
From: Greg Kroah-Hartman @ 2022-04-14 14:29 UTC (permalink / raw)
  To: Jani Nikula
  Cc: linux-kernel, intel-gfx, dri-devel, Andrew Morton, Lucas De Marchi

On Thu, Apr 14, 2022 at 05:22:47PM +0300, Jani Nikula wrote:
> On Thu, 14 Apr 2022, Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
> > On Thu, Apr 14, 2022 at 03:30:32PM +0300, Jani Nikula wrote:
> >> Hey, I've sent this before, ages ago, but haven't really followed
> >> through with it. I still think it would be useful for many scenarios
> >> where a plain number is a clumsy interface for a module param.
> >> 
> >> Thoughts?
> >
> > We should not be adding new module parameters anyway (they operate on
> > code, not data/devices), so what would this be used for?
> 
> I think it's just easier to use names than random values, and this also
> gives you range check on the input.
> 
> I also keep telling people not to add new module parameters, but it's
> not like they're going away anytime soon.

Existing ones can not go away (or change), but we do not have to add new
ones.

> If there's a solution to being able to pass device specific debug
> parameters at probe time, I'm all ears. At least i915 has a bunch of
> things which can't really be changed after probe, when debugfs for the
> device is around. Module parameters aren't ideal, but debugfs doesn't
> work for this.

configfs?


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/1] add support for enum module parameters
  2022-04-14 14:22   ` Jani Nikula
  2022-04-14 14:29     ` Greg Kroah-Hartman
@ 2022-04-20  5:13     ` Kalle Valo
  2022-04-20  6:38       ` Greg Kroah-Hartman
                         ` (2 more replies)
  1 sibling, 3 replies; 9+ messages in thread
From: Kalle Valo @ 2022-04-20  5:13 UTC (permalink / raw)
  To: Jani Nikula
  Cc: Greg Kroah-Hartman, linux-kernel, intel-gfx, dri-devel,
	Andrew Morton, Lucas De Marchi, linux-wireless, netdev

+ linux-wireless, netdev

Jani Nikula <jani.nikula@intel.com> writes:

> On Thu, 14 Apr 2022, Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
>> On Thu, Apr 14, 2022 at 03:30:32PM +0300, Jani Nikula wrote:
>>> Hey, I've sent this before, ages ago, but haven't really followed
>>> through with it. I still think it would be useful for many scenarios
>>> where a plain number is a clumsy interface for a module param.
>>> 
>>> Thoughts?
>>
>> We should not be adding new module parameters anyway (they operate on
>> code, not data/devices), so what would this be used for?
>
> I think it's just easier to use names than random values, and this also
> gives you range check on the input.
>
> I also keep telling people not to add new module parameters, but it's
> not like they're going away anytime soon.
>
> If there's a solution to being able to pass device specific debug
> parameters at probe time, I'm all ears. At least i915 has a bunch of
> things which can't really be changed after probe, when debugfs for the
> device is around. Module parameters aren't ideal, but debugfs doesn't
> work for this.

Wireless drivers would also desperately need to pass device specific
parameters at (or before) probe time. And not only debug parameters but
also configuration parameters, for example firmware memory allocations
schemes (optimise for features vs number of clients etc) and whatnot.

Any ideas how to implement that? Is there any prior work for anything
like this? This is pretty hard limiting usability of upstream wireless
drivers and I really want to find a proper solution.


-- 
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/1] add support for enum module parameters
  2022-04-20  5:13     ` Kalle Valo
@ 2022-04-20  6:38       ` Greg Kroah-Hartman
  2022-04-20 15:35       ` Ben Greear
  2022-04-22 20:44       ` Jakub Kicinski
  2 siblings, 0 replies; 9+ messages in thread
From: Greg Kroah-Hartman @ 2022-04-20  6:38 UTC (permalink / raw)
  To: Kalle Valo
  Cc: Jani Nikula, linux-kernel, intel-gfx, dri-devel, Andrew Morton,
	Lucas De Marchi, linux-wireless, netdev

On Wed, Apr 20, 2022 at 08:13:47AM +0300, Kalle Valo wrote:
> + linux-wireless, netdev
> 
> Jani Nikula <jani.nikula@intel.com> writes:
> 
> > On Thu, 14 Apr 2022, Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
> >> On Thu, Apr 14, 2022 at 03:30:32PM +0300, Jani Nikula wrote:
> >>> Hey, I've sent this before, ages ago, but haven't really followed
> >>> through with it. I still think it would be useful for many scenarios
> >>> where a plain number is a clumsy interface for a module param.
> >>> 
> >>> Thoughts?
> >>
> >> We should not be adding new module parameters anyway (they operate on
> >> code, not data/devices), so what would this be used for?
> >
> > I think it's just easier to use names than random values, and this also
> > gives you range check on the input.
> >
> > I also keep telling people not to add new module parameters, but it's
> > not like they're going away anytime soon.
> >
> > If there's a solution to being able to pass device specific debug
> > parameters at probe time, I'm all ears. At least i915 has a bunch of
> > things which can't really be changed after probe, when debugfs for the
> > device is around. Module parameters aren't ideal, but debugfs doesn't
> > work for this.
> 
> Wireless drivers would also desperately need to pass device specific
> parameters at (or before) probe time. And not only debug parameters but
> also configuration parameters, for example firmware memory allocations
> schemes (optimise for features vs number of clients etc) and whatnot.
> 
> Any ideas how to implement that? Is there any prior work for anything
> like this? This is pretty hard limiting usability of upstream wireless
> drivers and I really want to find a proper solution.

Again, configfs?  That should be what that subsystem was designed for...

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/1] add support for enum module parameters
  2022-04-20  5:13     ` Kalle Valo
  2022-04-20  6:38       ` Greg Kroah-Hartman
@ 2022-04-20 15:35       ` Ben Greear
  2022-04-22 20:44       ` Jakub Kicinski
  2 siblings, 0 replies; 9+ messages in thread
From: Ben Greear @ 2022-04-20 15:35 UTC (permalink / raw)
  To: Kalle Valo, Jani Nikula
  Cc: Greg Kroah-Hartman, linux-kernel, intel-gfx, dri-devel,
	Andrew Morton, Lucas De Marchi, linux-wireless, netdev

On 4/19/22 10:13 PM, Kalle Valo wrote:
> + linux-wireless, netdev
> 
> Jani Nikula <jani.nikula@intel.com> writes:
> 
>> On Thu, 14 Apr 2022, Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
>>> On Thu, Apr 14, 2022 at 03:30:32PM +0300, Jani Nikula wrote:
>>>> Hey, I've sent this before, ages ago, but haven't really followed
>>>> through with it. I still think it would be useful for many scenarios
>>>> where a plain number is a clumsy interface for a module param.
>>>>
>>>> Thoughts?
>>>
>>> We should not be adding new module parameters anyway (they operate on
>>> code, not data/devices), so what would this be used for?
>>
>> I think it's just easier to use names than random values, and this also
>> gives you range check on the input.
>>
>> I also keep telling people not to add new module parameters, but it's
>> not like they're going away anytime soon.
>>
>> If there's a solution to being able to pass device specific debug
>> parameters at probe time, I'm all ears. At least i915 has a bunch of
>> things which can't really be changed after probe, when debugfs for the
>> device is around. Module parameters aren't ideal, but debugfs doesn't
>> work for this.
> 
> Wireless drivers would also desperately need to pass device specific
> parameters at (or before) probe time. And not only debug parameters but
> also configuration parameters, for example firmware memory allocations
> schemes (optimise for features vs number of clients etc) and whatnot.
> 
> Any ideas how to implement that? Is there any prior work for anything
> like this? This is pretty hard limiting usability of upstream wireless
> drivers and I really want to find a proper solution.

I used a 'fwcfg' file that is loaded during ath10k initialization, from
same general location as the firmware.  Name is with pci-id or other unique
identifier like board files sometimes are named, and you get per radio
configuration at device load time.  I'm sure I posted a patch on this
some years ago, but I can point you to my current tree if you prefer.

Thanks,
Ben

-- 
Ben Greear <greearb@candelatech.com>
Candela Technologies Inc  http://www.candelatech.com


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/1] add support for enum module parameters
  2022-04-20  5:13     ` Kalle Valo
  2022-04-20  6:38       ` Greg Kroah-Hartman
  2022-04-20 15:35       ` Ben Greear
@ 2022-04-22 20:44       ` Jakub Kicinski
  2 siblings, 0 replies; 9+ messages in thread
From: Jakub Kicinski @ 2022-04-22 20:44 UTC (permalink / raw)
  To: Kalle Valo
  Cc: Jani Nikula, Greg Kroah-Hartman, linux-kernel, intel-gfx,
	dri-devel, Andrew Morton, Lucas De Marchi, linux-wireless,
	netdev

On Wed, 20 Apr 2022 08:13:47 +0300 Kalle Valo wrote:
> Wireless drivers would also desperately need to pass device specific
> parameters at (or before) probe time. And not only debug parameters but
> also configuration parameters, for example firmware memory allocations
> schemes (optimise for features vs number of clients etc) and whatnot.
> 
> Any ideas how to implement that? Is there any prior work for anything
> like this? This is pretty hard limiting usability of upstream wireless
> drivers and I really want to find a proper solution.

In netdev we have devlink which is used for all sort of device
configuration. devlink-resource sounds like what you need,
but it'd have to be extended to support configuration which requires
reload/re-probe. Currently only devlink-params support that but params
were a mistake so don't use that.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-04-22 21:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-14 12:30 [PATCH 0/1] add support for enum module parameters Jani Nikula
2022-04-14 12:30 ` [PATCH 1/1] module: add enum module parameter type to map names to values Jani Nikula
2022-04-14 13:19 ` [PATCH 0/1] add support for enum module parameters Greg Kroah-Hartman
2022-04-14 14:22   ` Jani Nikula
2022-04-14 14:29     ` Greg Kroah-Hartman
2022-04-20  5:13     ` Kalle Valo
2022-04-20  6:38       ` Greg Kroah-Hartman
2022-04-20 15:35       ` Ben Greear
2022-04-22 20:44       ` Jakub Kicinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).