Skip to content

Commit b51d23e

Browse files
ddstreetrustyrussell
authored andcommitted
module: add per-module param_lock
Add a "param_lock" mutex to each module, and update params.c to use the correct built-in or module mutex while locking kernel params. Remove the kparam_block_sysfs_r/w() macros, replace them with direct calls to kernel_param_[un]lock(module). The kernel param code currently uses a single mutex to protect modification of any and all kernel params. While this generally works, there is one specific problem with it; a module callback function cannot safely load another module, i.e. with request_module() or even with indirect calls such as crypto_has_alg(). If the module to be loaded has any of its params configured (e.g. with a /etc/modprobe.d/* config file), then the attempt will result in a deadlock between the first module param callback waiting for modprobe, and modprobe trying to lock the single kernel param mutex to set the new module's param. This fixes that by using per-module mutexes, so that each individual module is protected against concurrent changes in its own kernel params, but is not blocked by changes to other module params. All built-in modules continue to use the built-in mutex, since they will always be loaded at runtime and references (e.g. request_module(), crypto_has_alg()) to them will never cause load-time param changing. This also simplifies the interface used by modules to block sysfs access to their params; while there are currently functions to block and unblock sysfs param access which are split up by read and write and expect a single kernel param to be passed, their actual operation is identical and applies to all params, not just the one passed to them; they simply lock and unlock the global param mutex. They are replaced with direct calls to kernel_param_[un]lock(THIS_MODULE), which locks THIS_MODULE's param_lock, or if the module is built-in, it locks the built-in mutex. Suggested-by: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: Dan Streetman <ddstreet@ieee.org> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 5104b7d commit b51d23e

10 files changed

Lines changed: 65 additions & 93 deletions

File tree

‎arch/um/drivers/hostaudio_kern.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ static int hostaudio_open(struct inode *inode, struct file *file)
185185
int ret;
186186

187187
#ifdef DEBUG
188-
kparam_block_sysfs_write(dsp);
188+
kernel_param_lock(THIS_MODULE);
189189
printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
190-
kparam_unblock_sysfs_write(dsp);
190+
kernel_param_unlock(THIS_MODULE);
191191
#endif
192192

193193
state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
@@ -199,11 +199,11 @@ static int hostaudio_open(struct inode *inode, struct file *file)
199199
if (file->f_mode & FMODE_WRITE)
200200
w = 1;
201201

202-
kparam_block_sysfs_write(dsp);
202+
kernel_param_lock(THIS_MODULE);
203203
mutex_lock(&hostaudio_mutex);
204204
ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
205205
mutex_unlock(&hostaudio_mutex);
206-
kparam_unblock_sysfs_write(dsp);
206+
kernel_param_unlock(THIS_MODULE);
207207

208208
if (ret < 0) {
209209
kfree(state);
@@ -260,17 +260,17 @@ static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
260260
if (file->f_mode & FMODE_WRITE)
261261
w = 1;
262262

263-
kparam_block_sysfs_write(mixer);
263+
kernel_param_lock(THIS_MODULE);
264264
mutex_lock(&hostaudio_mutex);
265265
ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
266266
mutex_unlock(&hostaudio_mutex);
267-
kparam_unblock_sysfs_write(mixer);
267+
kernel_param_unlock(THIS_MODULE);
268268

269269
if (ret < 0) {
270-
kparam_block_sysfs_write(dsp);
270+
kernel_param_lock(THIS_MODULE);
271271
printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
272272
"err = %d\n", dsp, -ret);
273-
kparam_unblock_sysfs_write(dsp);
273+
kernel_param_unlock(THIS_MODULE);
274274
kfree(state);
275275
return ret;
276276
}
@@ -326,10 +326,10 @@ MODULE_LICENSE("GPL");
326326

327327
static int __init hostaudio_init_module(void)
328328
{
329-
__kernel_param_lock();
329+
kernel_param_lock(THIS_MODULE);
330330
printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
331331
dsp, mixer);
332-
__kernel_param_unlock();
332+
kernel_param_unlock(THIS_MODULE);
333333

334334
module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
335335
if (module_data.dev_audio < 0) {

‎drivers/net/ethernet/myricom/myri10ge/myri10ge.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ MODULE_FIRMWARE("myri10ge_eth_z8e.dat");
279279
MODULE_FIRMWARE("myri10ge_rss_ethp_z8e.dat");
280280
MODULE_FIRMWARE("myri10ge_rss_eth_z8e.dat");
281281

282-
/* Careful: must be accessed under kparam_block_sysfs_write */
282+
/* Careful: must be accessed under kernel_param_lock() */
283283
static char *myri10ge_fw_name = NULL;
284284
module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
285285
MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
@@ -3427,15 +3427,15 @@ static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
34273427
}
34283428
}
34293429

3430-
kparam_block_sysfs_write(myri10ge_fw_name);
3430+
kernel_param_lock(THIS_MODULE);
34313431
if (myri10ge_fw_name != NULL) {
34323432
char *fw_name = kstrdup(myri10ge_fw_name, GFP_KERNEL);
34333433
if (fw_name) {
34343434
overridden = 1;
34353435
set_fw_name(mgp, fw_name, true);
34363436
}
34373437
}
3438-
kparam_unblock_sysfs_write(myri10ge_fw_name);
3438+
kernel_param_unlock(THIS_MODULE);
34393439

34403440
if (mgp->board_number < MYRI10GE_MAX_BOARDS &&
34413441
myri10ge_fw_names[mgp->board_number] != NULL &&

‎drivers/net/wireless/libertas_tf/if_usb.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -821,15 +821,15 @@ static int if_usb_prog_firmware(struct if_usb_card *cardp)
821821

822822
lbtf_deb_enter(LBTF_DEB_USB);
823823

824-
kparam_block_sysfs_write(fw_name);
824+
kernel_param_lock(THIS_MODULE);
825825
ret = request_firmware(&cardp->fw, lbtf_fw_name, &cardp->udev->dev);
826826
if (ret < 0) {
827827
pr_err("request_firmware() failed with %#x\n", ret);
828828
pr_err("firmware %s not found\n", lbtf_fw_name);
829-
kparam_unblock_sysfs_write(fw_name);
829+
kernel_param_unlock(THIS_MODULE);
830830
goto done;
831831
}
832-
kparam_unblock_sysfs_write(fw_name);
832+
kernel_param_unlock(THIS_MODULE);
833833

834834
if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
835835
goto release_fw;

‎drivers/usb/atm/ueagle-atm.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ static void cmvs_file_name(struct uea_softc *sc, char *const cmv_name, int ver)
15991599
char file_arr[] = "CMVxy.bin";
16001600
char *file;
16011601

1602-
kparam_block_sysfs_write(cmv_file);
1602+
kernel_param_lock(THIS_MODULE);
16031603
/* set proper name corresponding modem version and line type */
16041604
if (cmv_file[sc->modem_index] == NULL) {
16051605
if (UEA_CHIP_VERSION(sc) == ADI930)
@@ -1618,7 +1618,7 @@ static void cmvs_file_name(struct uea_softc *sc, char *const cmv_name, int ver)
16181618
strlcat(cmv_name, file, UEA_FW_NAME_MAX);
16191619
if (ver == 2)
16201620
strlcat(cmv_name, ".v2", UEA_FW_NAME_MAX);
1621-
kparam_unblock_sysfs_write(cmv_file);
1621+
kernel_param_unlock(THIS_MODULE);
16221622
}
16231623

16241624
static int request_cmvs_old(struct uea_softc *sc,

‎drivers/video/fbdev/vt8623fb.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,9 @@ static int vt8623_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
754754

755755
/* Prepare startup mode */
756756

757-
kparam_block_sysfs_write(mode_option);
757+
kernel_param_lock(THIS_MODULE);
758758
rc = fb_find_mode(&(info->var), info, mode_option, NULL, 0, NULL, 8);
759-
kparam_unblock_sysfs_write(mode_option);
759+
kernel_param_unlock(THIS_MODULE);
760760
if (! ((rc == 1) || (rc == 2))) {
761761
rc = -EINVAL;
762762
dev_err(info->device, "mode %s not found\n", mode_option);

‎include/linux/module.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ struct module {
240240
unsigned int num_syms;
241241

242242
/* Kernel parameters. */
243+
struct mutex param_lock;
243244
struct kernel_param *kp;
244245
unsigned int num_kp;
245246

‎include/linux/moduleparam.h‎

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ enum {
6767

6868
struct kernel_param {
6969
const char *name;
70+
struct module *mod;
7071
const struct kernel_param_ops *ops;
7172
const u16 perm;
7273
s8 level;
@@ -108,7 +109,7 @@ struct kparam_array
108109
*
109110
* @perm is 0 if the the variable is not to appear in sysfs, or 0444
110111
* for world-readable, 0644 for root-writable, etc. Note that if it
111-
* is writable, you may need to use kparam_block_sysfs_write() around
112+
* is writable, you may need to use kernel_param_lock() around
112113
* accesses (esp. charp, which can be kfreed when it changes).
113114
*
114115
* The @type is simply pasted to refer to a param_ops_##type and a
@@ -216,12 +217,12 @@ struct kparam_array
216217
parameters. */
217218
#define __module_param_call(prefix, name, ops, arg, perm, level, flags) \
218219
/* Default value instead of permissions? */ \
219-
static const char __param_str_##name[] = prefix #name; \
220+
static const char __param_str_##name[] = prefix #name; \
220221
static struct kernel_param __moduleparam_const __param_##name \
221222
__used \
222223
__attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
223-
= { __param_str_##name, ops, VERIFY_OCTAL_PERMISSIONS(perm), \
224-
level, flags, { arg } }
224+
= { __param_str_##name, THIS_MODULE, ops, \
225+
VERIFY_OCTAL_PERMISSIONS(perm), level, flags, { arg } }
225226

226227
/* Obsolete - use module_param_cb() */
227228
#define module_param_call(name, set, get, arg, perm) \
@@ -238,58 +239,14 @@ __check_old_set_param(int (*oldset)(const char *, struct kernel_param *))
238239
return 0;
239240
}
240241

241-
/**
242-
* kparam_block_sysfs_write - make sure a parameter isn't written via sysfs.
243-
* @name: the name of the parameter
244-
*
245-
* There's no point blocking write on a paramter that isn't writable via sysfs!
246-
*/
247-
#define kparam_block_sysfs_write(name) \
248-
do { \
249-
BUG_ON(!(__param_##name.perm & 0222)); \
250-
__kernel_param_lock(); \
251-
} while (0)
252-
253-
/**
254-
* kparam_unblock_sysfs_write - allows sysfs to write to a parameter again.
255-
* @name: the name of the parameter
256-
*/
257-
#define kparam_unblock_sysfs_write(name) \
258-
do { \
259-
BUG_ON(!(__param_##name.perm & 0222)); \
260-
__kernel_param_unlock(); \
261-
} while (0)
262-
263-
/**
264-
* kparam_block_sysfs_read - make sure a parameter isn't read via sysfs.
265-
* @name: the name of the parameter
266-
*
267-
* This also blocks sysfs writes.
268-
*/
269-
#define kparam_block_sysfs_read(name) \
270-
do { \
271-
BUG_ON(!(__param_##name.perm & 0444)); \
272-
__kernel_param_lock(); \
273-
} while (0)
274-
275-
/**
276-
* kparam_unblock_sysfs_read - allows sysfs to read a parameter again.
277-
* @name: the name of the parameter
278-
*/
279-
#define kparam_unblock_sysfs_read(name) \
280-
do { \
281-
BUG_ON(!(__param_##name.perm & 0444)); \
282-
__kernel_param_unlock(); \
283-
} while (0)
284-
285242
#ifdef CONFIG_SYSFS
286-
extern void __kernel_param_lock(void);
287-
extern void __kernel_param_unlock(void);
243+
extern void kernel_param_lock(struct module *mod);
244+
extern void kernel_param_unlock(struct module *mod);
288245
#else
289-
static inline void __kernel_param_lock(void)
246+
static inline void kernel_param_lock(struct module *mod)
290247
{
291248
}
292-
static inline void __kernel_param_unlock(void)
249+
static inline void kernel_param_unlock(struct module *mod)
293250
{
294251
}
295252
#endif

‎kernel/module.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,6 +3442,8 @@ static int load_module(struct load_info *info, const char __user *uargs,
34423442
if (err)
34433443
goto unlink_mod;
34443444

3445+
mutex_init(&mod->param_lock);
3446+
34453447
/* Now we've got everything in the final locations, we can
34463448
* find optional sections. */
34473449
err = find_module_sections(mod, info);

0 commit comments

Comments
 (0)