Skip to contentSkip to conversation

Per-device parameter support (13/16)

RFC/PATCH Tejun Heo 2 participants 6 messages Oct 23, 2004 – Oct 25, 2004

Subsystems (2)
lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

dp_13_devparam.diff

This is the 13rd patch of 16 patches for devparam.

This patch adds needed data fields to module and device structures
and actually implements devparam. This patch doesn't hook devparam into the driver model it's done in the next patch.

Currently, set_devparams_by_args() path isn't used as all parameters
are specified either as boot options or module parameters. set_devparams_by_args() is there to be used when the hotplug facility is expanded to deal driver-device association. However, feel free to #if 0 or cut the code out.

drivers/base/Makefile +2 -1
Index: linux-devparam-export/drivers/base/Makefile
===================================================================
@@ -2,7 +2,8 @@
 
 obj-y			:= core.o sys.o interface.o bus.o \
 			   driver.o class.o class_simple.o platform.o \
-			   cpu.o firmware.o init.o map.o dmapool.o
+			   cpu.o firmware.o init.o map.o dmapool.o \
+			   deviceparam.o
 obj-y			+= power/
 obj-$(CONFIG_FW_LOADER)	+= firmware_class.o
 obj-$(CONFIG_NUMA)	+= node.o
drivers/base/deviceparam.c new file +980 -0

980 lines not on this page · raw message

include/linux/device.h +15 -0
Index: linux-devparam-export/include/linux/device.h
===================================================================
@@ -19,6 +19,8 @@
 #include <linux/types.h>
 #include <linux/module.h>
 #include <linux/pm.h>
+#include <linux/deviceparam.h>
+#include <linux/vector.h>
 #include <asm/semaphore.h>
 #include <asm/atomic.h>
 
@@ -57,6 +59,7 @@ struct bus_type {
 	struct bus_attribute	* bus_attrs;
 	struct device_attribute	* dev_attrs;
 	struct driver_attribute	* drv_attrs;
+	struct device_paramset_def * bus_paramset_def;
 
 	int		(*match)(struct device * dev, struct device_driver * drv);
 	int		(*hotplug) (struct device *dev, char **envp, 
@@ -105,6 +108,15 @@ struct device_driver {
 	struct semaphore	unload_sem;
 	struct kobject		kobj;
 	struct list_head	devices;
+	struct device_paramset_def * dev_paramset_def;
+	struct device_paramset_def ** aux_paramset_defs;
+
+	/* The following fields are initialized automatically by
+	   devparam_driver_init() when registering the driver */
+	int			nr_aux_paramsets;
+	struct vector		paramset_used_vec;
+	struct vector		bus_paramset_vec, dev_paramset_vec;
+	struct vector		* aux_paramset_vecs;
 
 	struct module 		* owner;
 
@@ -267,6 +279,9 @@ struct device {
 	void		*driver_data;	/* data private to the driver */
 	void		*platform_data;	/* Platform specific data (e.g. ACPI,
 					   BIOS data relevant to device) */
+
+	struct device_params	params;	/* Device params, see deviceparam.h */
+
 	struct dev_pm_info	power;
 	u32		power_state;	/* Current operating state. In
 					   ACPI-speak, this is D0-D3, D0
include/linux/deviceparam.h new file +219 -0

219 lines not on this page · raw message

include/linux/module.h +7 -0
Index: linux-devparam-export/include/linux/module.h
===================================================================
@@ -18,6 +18,7 @@
 #include <linux/stringify.h>
 #include <linux/kobject.h>
 #include <linux/moduleparam.h>
+#include <linux/vector.h>
 #include <asm/local.h>
 
 #include <asm/module.h>
@@ -126,6 +127,9 @@ extern struct module __this_module;
 #define MODULE_PARM_DESC(_parm, desc) \
 	__MODULE_INFO(parm, _parm, #_parm ":" desc)
 
+#define MODULE_DEVPARM_DESC(_parm, desc) \
+	__MODULE_INFO(devparm, _parm, #_parm ":" desc)
+
 #define MODULE_DEVICE_TABLE(type,name)		\
   MODULE_GENERIC_TABLE(type##_device,name)
 
@@ -323,6 +327,9 @@ struct module
 	/* The command line arguments (may be mangled).  People like
 	   keeping pointers to this stuff */
 	char *args;
+
+	/* Module parameter vector, used by deviceparam */
+	struct vector param_vec;
 };
 
 /* FIXME: It'd be nice to isolate modules during init, too, so they
#1 lore raw

drivers/base/deviceparam.c line 512

Quoted 4 lines · Tejun Heo
drivers/base/deviceparam.c new file +4 -0
+static int
+#ifndef CONFIG_MODULES
+__init
+#endif

This looks like a job for "__init_or_module", BTW.

Rusty.

Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman
#2 lore raw
In reply to Tejun Heo
Quoted 33 lines · Tejun Heo

dp_13_devparam.diff

This is the 13rd patch of 16 patches for devparam.

This patch adds needed data fields to module and device structures
and actually implements devparam. This patch doesn't hook devparam into the driver model it's done in the next patch.

drivers/base/deviceparam.c new file +25 -0
+int devparam_unknown_modparam(char *name, char *val, void *arg)
+{
+	struct module *mod = arg;
+	char **param;
+
+	param = vector_elem(&mod->param_vec, vector_len(&mod->param_vec),
+			    GFP_KERNEL);
+
+	if (param == NULL) {
+		printk(KERN_ERR
+		       "Device params: Insufficient memory for `%s'\n", name);
+		return -ENOMEM;
+	}
+
+	param[0] = name;
+	param[1] = val;
+
+	return 0;
+}
+
+void devparam_module_done(struct module *mod)
+{
+	struct vector *vec = &mod->param_vec;
+	int i;
+

drivers/base/deviceparam.c line 499

Quoted 10 lines
drivers/base/deviceparam.c new file +10 -0
+	for (i = 0; i < vector_len(vec); i++) {
+		char **param = vector_elem(vec, i, 0);
+		if (param[0])
+			printk(KERN_ERR
+			       "Device params: Unknown parameter `%s'\n",
+			       param[0]);
+	}
+	
+	vector_destroy(vec);
+}

That seems a strange place to warn... Is that right?

include/linux/module.h struct module · line 332

Quoted 3 lines
include/linux/module.h +3 -0
+
+	/* Module parameter vector, used by deviceparam */
+	struct vector param_vec;

I don't mind the addition of your vector type, but adding infrastructure always results in arguments. Can you think of another place which needs it?

Rusty.

Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman
author
#3 lore raw
Quoted 10 lines · Rusty Russell
Quoted 9 lines · Tejun Heo
drivers/base/deviceparam.c new file +9 -0
+void devparam_module_done(struct module *mod)
+{
+	struct vector *vec = &mod->param_vec;
+	int i;
+
+	for (i = 0; i < vector_len(vec); i++) {
+		char **param = vector_elem(vec, i, 0);
+		if (param[0])
+			printk(KERN_ERR
Quoted 8 lines

drivers/base/deviceparam.c line 499

Quoted 6 lines
drivers/base/deviceparam.c new file +6 -0
+			       "Device params: Unknown parameter `%s'\n",
+			       param[0]);
+	}
+	
+	vector_destroy(vec);
+}

That seems a strange place to warn... Is that right?

Yes, it's right. Because a module may contain more than one drivers,
devparam puts all paramters into an array and mask them off with NULL when they are taken and, after module initialization is done, above devparam_module_done() is called and prints warnings about untaken paramters. The same approach is used for boot options too.

Quoted 7 lines

include/linux/module.h struct module · line 332

Quoted 3 lines
include/linux/module.h +3 -0
+
+	/* Module parameter vector, used by deviceparam */
+	struct vector param_vec;

I don't mind the addition of your vector type, but adding infrastructure always results in arguments. Can you think of another place which needs it?

I just find dynamically expandable arrays very useful when I code
stuff. Expanding/shrinking in the middle is maybe unnecessary but in general, a vector w/ constructor/destructor is handy. I cannot pinpoint any specific code right now but I am pretty positive that there are many places where unncessary fixed limit is employed to avoid dynamic array management (not that fixed limit is always bad but it's better to avoid them when possible without much overhead).

If getting the vector in is difficult, I can merge vector codes into
devparam or change devparam to use list instead. No problem there. But I think that having generic vector around isn't a bad idea. I'll try to find places where vector can be useful if I can find some time.

Thanks.

Signature · 1 line
tejun
#4 lore raw
In reply to Tejun Heo

include/linux/deviceparam.h line 51

Quoted 6 lines · Tejun Heo
include/linux/deviceparam.h new file +6 -0
+/* For some reason, gcc aligns structures which contain other
+   structures to 32-byte boundary even when __alignof__() the strucure
+   is smaller than that.  As we're gonna assemble paramset array
+   manually, we need to set alignment explicitly.  Also, we put all
+   paramset array elements into .data.1 to avoid intervening variables
+   of different types (kparam_string, kparam_array for now). */

You need __attribute__((aligned(sizeof(void *))): see moduleparam.h. The padding between structures is arch-dependent (x86-64 found out the hard way when we changed kernel_param previously). Not sure why you're using a separate section here anyway.

Quoted 4 lines
include/linux/deviceparam.h new file +4 -0
+/* Bit flag */
+#define __DEVICE_PARAM_FLAG(Name, Field, Flag, Dfl, Inv, Perm, Desc)	\
+	param_check_uint(__devparam_data(Name),				\
+			 &(((__devparam_type *)0)->Field));		\

include/linux/deviceparam.h line 136

Quoted 10 lines
include/linux/deviceparam.h new file +10 -0
+	static struct kparam_flag __devparam_data(__param_flag_##Name)	\
+	__devparam_extra_section = { 0, Flag, Inv };			\
+	__DEVICE_PARAM_CALL_RANGED(Name, param_set_flag, param_get_flag,\
+		&__devparam_data(__param_flag_##Name),			\
+		sizeof(struct kparam_flag), 1, 0, Dfl, 0, Perm, Desc,	\
+		offsetof(struct kparam_flag, pflags),			\
+		offsetof(__devparam_type, Field), -1, -1)
+
+#define DEVICE_PARAM_FLAG(Name, Field, Flag, Dfl, Perm, Desc)		\
+	__DEVICE_PARAM_FLAG(Name, Field, Flag, Dfl, 0, Perm, Desc)

Haven't read closely, but why is FLAG special?

I think your macros might be better off always having the range, which would reduce the number of functions.. I like including the description in the macros: that's an improvement.

I always disliked the _NAMED versions (if it's a good name for users, it's usually a good name for the variable and vice versa), but you might want to consider always having it if that's the common case.

Cheers,
Rusty.

Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman
author
#5 lore raw
Quoted 4 lines · Rusty Russell

You need __attribute__((aligned(sizeof(void *))): see moduleparam.h. The padding between structures is arch-dependent (x86-64 found out the hard way when we changed kernel_param previously). Not sure why you're using a separate section here anyway.

As the defs array of struct device_paramset_def is assembled by
putting struct device_params continuosly, there shouldn't be any other intervening data while defining the array. So, the wrapping arguments (struct kparam_array, struct kparam_flag) should stay out of line. That's why they're put in different subsections.

Quoted 8 lines
Quoted 8 lines
include/linux/deviceparam.h new file +8 -0
+/* Bit flag */
+#define __DEVICE_PARAM_FLAG(Name, Field, Flag, Dfl, Inv, Perm, Desc)	\
+	param_check_uint(__devparam_data(Name),				\
+			 &(((__devparam_type *)0)->Field));		\
+	static struct kparam_flag __devparam_data(__param_flag_##Name)	\
+	__devparam_extra_section = { 0, Flag, Inv };			\
+	__DEVICE_PARAM_CALL_RANGED(Name, param_set_flag, param_get_flag,\
+		&__devparam_data(__param_flag_##Name),			\
Quoted 8 lines

include/linux/deviceparam.h line 136

Quoted 6 lines
include/linux/deviceparam.h new file +6 -0
+		sizeof(struct kparam_flag), 1, 0, Dfl, 0, Perm, Desc,	\
+		offsetof(struct kparam_flag, pflags),			\
+		offsetof(__devparam_type, Field), -1, -1)
+
+#define DEVICE_PARAM_FLAG(Name, Field, Flag, Dfl, Perm, Desc)		\
+	__DEVICE_PARAM_FLAG(Name, Field, Flag, Dfl, 0, Perm, Desc)

Haven't read closely, but why is FLAG special?

Because flag requires wrapping struct kparam_flag structure. As I've
explained in another mail, wrapping structures need to be copied and fixed up before being used. So, the sizeof and offsetof's.

Quoted 7 lines

I think your macros might be better off always having the range, which would reduce the number of functions.. I like including the description in the macros: that's an improvement.

I always disliked the _NAMED versions (if it's a good name for users, it's usually a good name for the variable and vice versa), but you might want to consider always having it if that's the common case.

Again, I just think it's better to be a bit rich in module/device
parameter handling and provide convenient interface as it can reduce code in many places. But that's me. :-)

As this is the last mail of this series of replies, I want to add
some more stuff to discuss about.

1. What do you think about merging bus_paramset_def, dev_paramset_def
and aux_paramset_defs into single paramset_defs array. The only functional difference is that, with the change, a driver should explicitly specify the corresponding bus paramset_def and pass the resulting paramset to the bus registration function explicitly if the bus wants to accept its own paramset.

I think it's better because it's simpler and more consistent, and it
gives drivers a chance to peek or mangle bus parameters as needed.

2. What do you think about adding support for user-controllable
device - driver association? We'll be able to tell the kernel to 'associate device A to driver B with these parameters'. With proper user-space programs we'll be able to have fine control over device drivers. Also, user-space tool will be able to set the kernel device name as it likes from the start so discrepancies between kernel device names and user-space device names can be removed (udev).

What I was thinking was

- adding a sysctl variable which enables the feature and when it's enabled the kernel stops automatically associating devices to matching drivers.
- User space tool can write to a driver sysfs node (maybe named attach) to attach a specific device to the driver.
- A device - driver association is represented by a child kobject of the associated driver or device.
- Detaching can be done by deleting the file representing the association. Or writing to a detach sysfs file.

As there's no kobject which represents device - driver association,
devparam currently makes a child kobject of the corresponding device node itself (the params subdirectory).

3. How about adding a mechanism to automatically add min, max and
default value to @Desc. Though I'm not so sure about this one.

Thanks a lot. :-)

Signature · 1 line
tejun