staging: most: block module removal while having active configfs items
authorChristian Gromm <christian.gromm@microchip.com>
Fri, 8 Nov 2019 16:21:08 +0000 (17:21 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 11 Nov 2019 15:41:00 +0000 (16:41 +0100)
This patch avoids that core component modules are being unloaded
while the related configfs interface has active items in its directories.
It is needed to prevent the situation where the core module cannot
be unloaded anymore, because the reference count 'used by' indicates that
the module is still being used and the usage count cannot be decreased by
calling rmdir, as the configfs directory has already been removed.

Signed-off-by: Christian Gromm <christian.gromm@microchip.com>
Link: https://lore.kernel.org/r/1573230068-27658-3-git-send-email-christian.gromm@microchip.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/most/cdev/cdev.c
drivers/staging/most/configfs.c
drivers/staging/most/core.h
drivers/staging/most/net/net.c
drivers/staging/most/sound/sound.c
drivers/staging/most/video/video.c

index 724d098aeef040df908ba315bb928a41ede27f1b..f880147c82fd5b02e8f3e1ee735c4998063deccc 100644 (file)
@@ -494,6 +494,7 @@ err_remove_ida:
 
 static struct cdev_component comp = {
        .cc = {
+               .mod = THIS_MODULE,
                .name = "cdev",
                .probe_channel = comp_probe,
                .disconnect_channel = comp_disconnect_channel,
index c292dd306bb875ca6cdb8d87c4e1ce7773ea8d7e..34a9fb53985c1e2cef9ddc0c48837a637e00e9cd 100644 (file)
@@ -395,6 +395,7 @@ static const struct config_item_type mdev_link_type = {
 
 struct most_common {
        struct config_group group;
+       struct module *mod;
        struct configfs_subsystem subsys;
 };
 
@@ -407,11 +408,16 @@ static struct config_item *most_common_make_item(struct config_group *group,
                                                 const char *name)
 {
        struct mdev_link *mdev_link;
+       struct most_common *mc = to_most_common(group->cg_subsys);
 
        mdev_link = kzalloc(sizeof(*mdev_link), GFP_KERNEL);
        if (!mdev_link)
                return ERR_PTR(-ENOMEM);
 
+       if (!try_module_get(mc->mod)) {
+               kfree(mdev_link);
+               return ERR_PTR(-ENOLCK);
+       }
        config_item_init_type_name(&mdev_link->item, name,
                                   &mdev_link_type);
 
@@ -436,8 +442,17 @@ static struct configfs_item_operations most_common_item_ops = {
        .release        = most_common_release,
 };
 
+static void most_common_disconnect(struct config_group *group,
+                                  struct config_item *item)
+{
+       struct most_common *mc = to_most_common(group->cg_subsys);
+
+       module_put(mc->mod);
+}
+
 static struct configfs_group_operations most_common_group_ops = {
        .make_item      = most_common_make_item,
+       .disconnect_notify = most_common_disconnect,
 };
 
 static const struct config_item_type most_common_type = {
@@ -558,13 +573,14 @@ static const struct config_item_type most_snd_grp_type = {
 struct most_sound {
        struct configfs_subsystem subsys;
        struct list_head soundcard_list;
+       struct module *mod;
 };
 
 static struct config_group *most_sound_make_group(struct config_group *group,
                                                  const char *name)
 {
        struct most_snd_grp *most;
-       struct most_sound *ms = container_of(to_configfs_subsystem(group),
+       struct most_sound *ms = container_of(group->cg_subsys,
                                             struct most_sound, subsys);
 
        list_for_each_entry(most, &ms->soundcard_list, list) {
@@ -573,17 +589,29 @@ static struct config_group *most_sound_make_group(struct config_group *group,
                        return ERR_PTR(-EPROTO);
                }
        }
+       if (!try_module_get(ms->mod))
+               return ERR_PTR(-ENOLCK);
        most = kzalloc(sizeof(*most), GFP_KERNEL);
-       if (!most)
+       if (!most) {
+               module_put(ms->mod);
                return ERR_PTR(-ENOMEM);
-
+       }
        config_group_init_type_name(&most->group, name, &most_snd_grp_type);
        list_add_tail(&most->list, &ms->soundcard_list);
        return &most->group;
 }
 
+static void most_sound_disconnect(struct config_group *group,
+                                 struct config_item *item)
+{
+       struct most_sound *ms = container_of(group->cg_subsys,
+                                            struct most_sound, subsys);
+       module_put(ms->mod);
+}
+
 static struct configfs_group_operations most_sound_group_ops = {
        .make_group     = most_sound_make_group,
+       .disconnect_notify = most_sound_disconnect,
 };
 
 static const struct config_item_type most_sound_type = {
@@ -607,12 +635,16 @@ int most_register_configfs_subsys(struct core_component *c)
        int ret;
 
        if (!strcmp(c->name, "cdev")) {
+               most_cdev.mod = c->mod;
                ret = configfs_register_subsystem(&most_cdev.subsys);
        } else if (!strcmp(c->name, "net")) {
+               most_net.mod = c->mod;
                ret = configfs_register_subsystem(&most_net.subsys);
        } else if (!strcmp(c->name, "video")) {
+               most_video.mod = c->mod;
                ret = configfs_register_subsystem(&most_video.subsys);
        } else if (!strcmp(c->name, "sound")) {
+               most_sound_subsys.mod = c->mod;
                ret = configfs_register_subsystem(&most_sound_subsys.subsys);
        } else {
                return -ENODEV;
index 652aaa7710291040455911d0c712aa0fc8c1e7a2..49859aef98df0508cdde5b6bfd6f51a73a568902 100644 (file)
@@ -265,6 +265,7 @@ struct most_interface {
 struct core_component {
        struct list_head list;
        const char *name;
+       struct module *mod;
        int (*probe_channel)(struct most_interface *iface, int channel_idx,
                             struct most_channel_config *cfg, char *name,
                             char *param);
index 26a31854c6366909b86cdb62d7eccd24eedce5b2..6cab1bb8956e3979746dd3203ef0d14260644fe8 100644 (file)
@@ -498,6 +498,7 @@ put_nd:
 }
 
 static struct core_component comp = {
+       .mod = THIS_MODULE,
        .name = "net",
        .probe_channel = comp_probe_channel,
        .disconnect_channel = comp_disconnect_channel,
index 79817061fcfa41cb85318a6373655a7f796c00e4..1359f289ffd513147ce309a5ed1a54713231f09a 100644 (file)
@@ -782,6 +782,7 @@ static int audio_tx_completion(struct most_interface *iface, int channel_id)
  * Initialization of the struct core_component
  */
 static struct core_component comp = {
+       .mod = THIS_MODULE,
        .name = DRIVER_NAME,
        .probe_channel = audio_probe_channel,
        .disconnect_channel = audio_disconnect_channel,
index 250af9fb704dfa2a909369c53a0e7b18478ffaa3..10c1ef7e3a3ede3b7da89d2cce969bb280412f5a 100644 (file)
@@ -528,6 +528,7 @@ static int comp_disconnect_channel(struct most_interface *iface,
 }
 
 static struct core_component comp = {
+       .mod = THIS_MODULE,
        .name = "video",
        .probe_channel = comp_probe_channel,
        .disconnect_channel = comp_disconnect_channel,