Skip to content

Commit

Permalink
module: add per-module param_lock
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Dan Streetman <[email protected]>
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
ddstreet authored and rustyrussell committed Jun 23, 2015
1 parent 5104b7d commit b51d23e
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 93 deletions.
20 changes: 10 additions & 10 deletions arch/um/drivers/hostaudio_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ static int hostaudio_open(struct inode *inode, struct file *file)
int ret;

#ifdef DEBUG
kparam_block_sysfs_write(dsp);
kernel_param_lock(THIS_MODULE);
printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
kparam_unblock_sysfs_write(dsp);
kernel_param_unlock(THIS_MODULE);
#endif

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

kparam_block_sysfs_write(dsp);
kernel_param_lock(THIS_MODULE);
mutex_lock(&hostaudio_mutex);
ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
mutex_unlock(&hostaudio_mutex);
kparam_unblock_sysfs_write(dsp);
kernel_param_unlock(THIS_MODULE);

if (ret < 0) {
kfree(state);
Expand Down Expand Up @@ -260,17 +260,17 @@ static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
if (file->f_mode & FMODE_WRITE)
w = 1;

kparam_block_sysfs_write(mixer);
kernel_param_lock(THIS_MODULE);
mutex_lock(&hostaudio_mutex);
ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
mutex_unlock(&hostaudio_mutex);
kparam_unblock_sysfs_write(mixer);
kernel_param_unlock(THIS_MODULE);

if (ret < 0) {
kparam_block_sysfs_write(dsp);
kernel_param_lock(THIS_MODULE);
printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
"err = %d\n", dsp, -ret);
kparam_unblock_sysfs_write(dsp);
kernel_param_unlock(THIS_MODULE);
kfree(state);
return ret;
}
Expand Down Expand Up @@ -326,10 +326,10 @@ MODULE_LICENSE("GPL");

static int __init hostaudio_init_module(void)
{
__kernel_param_lock();
kernel_param_lock(THIS_MODULE);
printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
dsp, mixer);
__kernel_param_unlock();
kernel_param_unlock(THIS_MODULE);

module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
if (module_data.dev_audio < 0) {
Expand Down
6 changes: 3 additions & 3 deletions drivers/net/ethernet/myricom/myri10ge/myri10ge.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ MODULE_FIRMWARE("myri10ge_eth_z8e.dat");
MODULE_FIRMWARE("myri10ge_rss_ethp_z8e.dat");
MODULE_FIRMWARE("myri10ge_rss_eth_z8e.dat");

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

kparam_block_sysfs_write(myri10ge_fw_name);
kernel_param_lock(THIS_MODULE);
if (myri10ge_fw_name != NULL) {
char *fw_name = kstrdup(myri10ge_fw_name, GFP_KERNEL);
if (fw_name) {
overridden = 1;
set_fw_name(mgp, fw_name, true);
}
}
kparam_unblock_sysfs_write(myri10ge_fw_name);
kernel_param_unlock(THIS_MODULE);

if (mgp->board_number < MYRI10GE_MAX_BOARDS &&
myri10ge_fw_names[mgp->board_number] != NULL &&
Expand Down
6 changes: 3 additions & 3 deletions drivers/net/wireless/libertas_tf/if_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,15 +821,15 @@ static int if_usb_prog_firmware(struct if_usb_card *cardp)

lbtf_deb_enter(LBTF_DEB_USB);

kparam_block_sysfs_write(fw_name);
kernel_param_lock(THIS_MODULE);
ret = request_firmware(&cardp->fw, lbtf_fw_name, &cardp->udev->dev);
if (ret < 0) {
pr_err("request_firmware() failed with %#x\n", ret);
pr_err("firmware %s not found\n", lbtf_fw_name);
kparam_unblock_sysfs_write(fw_name);
kernel_param_unlock(THIS_MODULE);
goto done;
}
kparam_unblock_sysfs_write(fw_name);
kernel_param_unlock(THIS_MODULE);

if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
goto release_fw;
Expand Down
4 changes: 2 additions & 2 deletions drivers/usb/atm/ueagle-atm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ static void cmvs_file_name(struct uea_softc *sc, char *const cmv_name, int ver)
char file_arr[] = "CMVxy.bin";
char *file;

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

static int request_cmvs_old(struct uea_softc *sc,
Expand Down
4 changes: 2 additions & 2 deletions drivers/video/fbdev/vt8623fb.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,9 @@ static int vt8623_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)

/* Prepare startup mode */

kparam_block_sysfs_write(mode_option);
kernel_param_lock(THIS_MODULE);
rc = fb_find_mode(&(info->var), info, mode_option, NULL, 0, NULL, 8);
kparam_unblock_sysfs_write(mode_option);
kernel_param_unlock(THIS_MODULE);
if (! ((rc == 1) || (rc == 2))) {
rc = -EINVAL;
dev_err(info->device, "mode %s not found\n", mode_option);
Expand Down
1 change: 1 addition & 0 deletions include/linux/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ struct module {
unsigned int num_syms;

/* Kernel parameters. */
struct mutex param_lock;
struct kernel_param *kp;
unsigned int num_kp;

Expand Down
61 changes: 9 additions & 52 deletions include/linux/moduleparam.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ enum {

struct kernel_param {
const char *name;
struct module *mod;
const struct kernel_param_ops *ops;
const u16 perm;
s8 level;
Expand Down Expand Up @@ -108,7 +109,7 @@ struct kparam_array
*
* @perm is 0 if the the variable is not to appear in sysfs, or 0444
* for world-readable, 0644 for root-writable, etc. Note that if it
* is writable, you may need to use kparam_block_sysfs_write() around
* is writable, you may need to use kernel_param_lock() around
* accesses (esp. charp, which can be kfreed when it changes).
*
* The @type is simply pasted to refer to a param_ops_##type and a
Expand Down Expand Up @@ -216,12 +217,12 @@ struct kparam_array
parameters. */
#define __module_param_call(prefix, name, ops, arg, perm, level, flags) \
/* Default value instead of permissions? */ \
static const char __param_str_##name[] = prefix #name; \
static const char __param_str_##name[] = prefix #name; \
static struct kernel_param __moduleparam_const __param_##name \
__used \
__attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
= { __param_str_##name, ops, VERIFY_OCTAL_PERMISSIONS(perm), \
level, flags, { arg } }
= { __param_str_##name, THIS_MODULE, ops, \
VERIFY_OCTAL_PERMISSIONS(perm), level, flags, { arg } }

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

/**
* kparam_block_sysfs_write - make sure a parameter isn't written via sysfs.
* @name: the name of the parameter
*
* There's no point blocking write on a paramter that isn't writable via sysfs!
*/
#define kparam_block_sysfs_write(name) \
do { \
BUG_ON(!(__param_##name.perm & 0222)); \
__kernel_param_lock(); \
} while (0)

/**
* kparam_unblock_sysfs_write - allows sysfs to write to a parameter again.
* @name: the name of the parameter
*/
#define kparam_unblock_sysfs_write(name) \
do { \
BUG_ON(!(__param_##name.perm & 0222)); \
__kernel_param_unlock(); \
} while (0)

/**
* kparam_block_sysfs_read - make sure a parameter isn't read via sysfs.
* @name: the name of the parameter
*
* This also blocks sysfs writes.
*/
#define kparam_block_sysfs_read(name) \
do { \
BUG_ON(!(__param_##name.perm & 0444)); \
__kernel_param_lock(); \
} while (0)

/**
* kparam_unblock_sysfs_read - allows sysfs to read a parameter again.
* @name: the name of the parameter
*/
#define kparam_unblock_sysfs_read(name) \
do { \
BUG_ON(!(__param_##name.perm & 0444)); \
__kernel_param_unlock(); \
} while (0)

#ifdef CONFIG_SYSFS
extern void __kernel_param_lock(void);
extern void __kernel_param_unlock(void);
extern void kernel_param_lock(struct module *mod);
extern void kernel_param_unlock(struct module *mod);
#else
static inline void __kernel_param_lock(void)
static inline void kernel_param_lock(struct module *mod)
{
}
static inline void __kernel_param_unlock(void)
static inline void kernel_param_unlock(struct module *mod)
{
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions kernel/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -3442,6 +3442,8 @@ static int load_module(struct load_info *info, const char __user *uargs,
if (err)
goto unlink_mod;

mutex_init(&mod->param_lock);

/* Now we've got everything in the final locations, we can
* find optional sections. */
err = find_module_sections(mod, info);
Expand Down
Loading

0 comments on commit b51d23e

Please sign in to comment.