Skip to contentSkip to conversation

team: fix hang in team_mode_get()

PATCH Superseded Taehee Yoo 2 participants 3 messages Apr 18, 2020 – Apr 20, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Taehee Yoo

author
#0 lore raw

PATCH 1/1 team: fix hang in team_mode_get()

When team mode is changed or set, the team_mode_get() is called to check whether the mode module is inserted or not. If the mode module is not inserted, it calls the request_module().
In the request_module(), it creates a child process, which is the "modprobe" process and waits for the done of the child process. At this point, the following locks were used. down_read(&cb_lock()); by genl_rcv()

    genl_lock(); by genl_rcv_msc()
        rtnl_lock(); by team_nl_cmd_options_set()
            mutex_lock(&team->lock); by team_nl_team_get()

Concurrently, the team module could be removed by rmmod or "modprobe -r" The __exit function of team module is team_module_exit(), which calls team_nl_fini() and it tries to acquire following locks.

down_write(&cb_lock);
    genl_lock();

Because of the genl_lock() and cb_lock, this process can't be finished earlier than request_module() routine.

The problem secenario.
CPU0 CPU1
team_mode_get
request_module()
modprobe -r team_mode_roundrobin
team <--(B)
modprobe team <--(A)
team_mode_roundrobin

By request_module(), the "modprobe team_mode_roundrobin" command will be executed. At this point, the modprobe process will decide that the team module should be inserted before team_mode_roundrobin. Because the team module is being removed.

By the module infrastructure, the same module insert/remove operations can't be executed concurrently.
So, (A) waits for (B) but (B) also waits for (A) because of locks. So that the hang occurs at this point.

Test commands:
while :
do
teamd -d &
killall teamd &
modprobe -rv team_mode_roundrobin &
done

The approach of this patch is to hold the reference count of the team module if the team module is compiled as a module. If the reference count of the team module is not zero while request_module() is being called, the team module will not be removed at that moment.
So that the above scenario could not occur.

Fixes 3d249d4ca7d0 ("net: introduce ethernet teaming device")
---
 drivers/net/team/team.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/net/team/team.c +9 -1
index 4004f98e50d9..21702bc23705 100644
@@ -465,9 +465,15 @@ EXPORT_SYMBOL(team_mode_unregister);
 
 static const struct team_mode *team_mode_get(const char *kind)
 {
-	struct team_mode_item *mitem;
 	const struct team_mode *mode = NULL;
+	struct team_mode_item *mitem;
+	bool put = false;
 
+#if IS_MODULE(CONFIG_NET_TEAM)
+	if (!try_module_get(THIS_MODULE))
+		return NULL;
+	put = true;
+#endif
 	spin_lock(&mode_list_lock);
 	mitem = __find_mode(kind);
 	if (!mitem) {
@@ -483,6 +489,8 @@ static const struct team_mode *team_mode_get(const char *kind)
 	}
 
 	spin_unlock(&mode_list_lock);
+	if (put)
+		module_put(THIS_MODULE);
 	return mode;
 }
 
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 63 lines · Sat, Apr 18, 2020 at 06:17:29PM CEST, [email protected] wrote:

When team mode is changed or set, the team_mode_get() is called to check whether the mode module is inserted or not. If the mode module is not inserted, it calls the request_module().
In the request_module(), it creates a child process, which is the "modprobe" process and waits for the done of the child process. At this point, the following locks were used. down_read(&cb_lock()); by genl_rcv()
genl_lock(); by genl_rcv_msc()

       rtnl_lock(); by team_nl_cmd_options_set()
           mutex_lock(&team->lock); by team_nl_team_get()

Concurrently, the team module could be removed by rmmod or "modprobe -r" The __exit function of team module is team_module_exit(), which calls team_nl_fini() and it tries to acquire following locks.

down_write(&cb_lock);
   genl_lock();

Because of the genl_lock() and cb_lock, this process can't be finished earlier than request_module() routine.

The problem secenario.
CPU0 CPU1
team_mode_get
request_module()
modprobe -r team_mode_roundrobin
team <--(B)
modprobe team <--(A)
team_mode_roundrobin

By request_module(), the "modprobe team_mode_roundrobin" command will be executed. At this point, the modprobe process will decide that the team module should be inserted before team_mode_roundrobin. Because the team module is being removed.

By the module infrastructure, the same module insert/remove operations can't be executed concurrently.
So, (A) waits for (B) but (B) also waits for (A) because of locks. So that the hang occurs at this point.

Test commands:
while :
do
teamd -d &
killall teamd &
modprobe -rv team_mode_roundrobin &
done

The approach of this patch is to hold the reference count of the team module if the team module is compiled as a module. If the reference count of the team module is not zero while request_module() is being called, the team module will not be removed at that moment.
So that the above scenario could not occur.

Fixes 3d249d4ca7d0 ("net: introduce ethernet teaming device")

---
drivers/net/team/team.c | 10 +++++++++-

1 file changed, 9 insertions(+), 1 deletion(-)
drivers/net/team/team.c +0 -0
index 4004f98e50d9..21702bc23705 100644

drivers/net/team/team.c EXPORT_SYMBOL(team_mode_unregister); · line 473

Quoted 10 lines
drivers/net/team/team.c +4 -1
@@ -465,9 +465,15 @@ EXPORT_SYMBOL(team_mode_unregister);
 
 static const struct team_mode *team_mode_get(const char *kind)
 {
-	struct team_mode_item *mitem;
 	const struct team_mode *mode = NULL;
+	struct team_mode_item *mitem;
+	bool put = false;
 
+#if IS_MODULE(CONFIG_NET_TEAM)
+	if (!try_module_get(THIS_MODULE))

Can't you call this in case this is not a module? Wouldn't THIS_MODULE be NULL then? try_module_get() handles that correctly.

drivers/net/team/team.c static const struct team_mode *team_mode_get(const char *kind) · line 493

Quoted 12 lines
drivers/net/team/team.c +5 -0
+		return NULL;
+	put = true;
+#endif
 	spin_lock(&mode_list_lock);
 	mitem = __find_mode(kind);
 	if (!mitem) {
@@ -483,6 +489,8 @@ static const struct team_mode *team_mode_get(const char *kind)
 	}
 
 	spin_unlock(&mode_list_lock);
+	if (put)
+		module_put(THIS_MODULE);

Can't you just put this under the same "if IS_MODULE" statement and avoid the "put" variable? Or in case the statement is not needed, just do plain module_put call.

Otherwise, the patch looks fine.

Quoted 6 lines
drivers/net/team/team.c +0 -0
 	return mode;
 }
 
Signature · 1 line
2.17.1
author
#2 lore raw
Quoted 1 line · Jiri Pirko

Hi Jiri,
Thank you for your review!

Quoted 96 lines
Quoted 63 lines · Sat, Apr 18, 2020 at 06:17:29PM CEST, [email protected] wrote:

When team mode is changed or set, the team_mode_get() is called to check whether the mode module is inserted or not. If the mode module is not inserted, it calls the request_module().
In the request_module(), it creates a child process, which is the "modprobe" process and waits for the done of the child process. At this point, the following locks were used. down_read(&cb_lock()); by genl_rcv()
genl_lock(); by genl_rcv_msc()

       rtnl_lock(); by team_nl_cmd_options_set()
           mutex_lock(&team->lock); by team_nl_team_get()

Concurrently, the team module could be removed by rmmod or "modprobe -r" The __exit function of team module is team_module_exit(), which calls team_nl_fini() and it tries to acquire following locks.

down_write(&cb_lock);
   genl_lock();

Because of the genl_lock() and cb_lock, this process can't be finished earlier than request_module() routine.

The problem secenario.
CPU0 CPU1
team_mode_get
request_module()
modprobe -r team_mode_roundrobin
team <--(B)
modprobe team <--(A)
team_mode_roundrobin

By request_module(), the "modprobe team_mode_roundrobin" command will be executed. At this point, the modprobe process will decide that the team module should be inserted before team_mode_roundrobin. Because the team module is being removed.

By the module infrastructure, the same module insert/remove operations can't be executed concurrently.
So, (A) waits for (B) but (B) also waits for (A) because of locks. So that the hang occurs at this point.

Test commands:
while :
do
teamd -d &
killall teamd &
modprobe -rv team_mode_roundrobin &
done

The approach of this patch is to hold the reference count of the team module if the team module is compiled as a module. If the reference count of the team module is not zero while request_module() is being called, the team module will not be removed at that moment.
So that the above scenario could not occur.

Fixes 3d249d4ca7d0 ("net: introduce ethernet teaming device")

---
drivers/net/team/team.c | 10 +++++++++-

1 file changed, 9 insertions(+), 1 deletion(-)
drivers/net/team/team.c +0 -0
index 4004f98e50d9..21702bc23705 100644

drivers/net/team/team.c EXPORT_SYMBOL(team_mode_unregister); · line 473

Quoted 10 lines
drivers/net/team/team.c +4 -1
@@ -465,9 +465,15 @@ EXPORT_SYMBOL(team_mode_unregister);
 
 static const struct team_mode *team_mode_get(const char *kind)
 {
-      struct team_mode_item *mitem;
      const struct team_mode *mode = NULL;
+      struct team_mode_item *mitem;
+      bool put = false;
 
+#if IS_MODULE(CONFIG_NET_TEAM)
+      if (!try_module_get(THIS_MODULE))

Can't you call this in case this is not a module? Wouldn't THIS_MODULE be NULL then? try_module_get() handles that correctly.

drivers/net/team/team.c static const struct team_mode *team_mode_get(const char *kind) · line 493

Quoted 12 lines
drivers/net/team/team.c +5 -0
+              return NULL;
+      put = true;
+#endif
      spin_lock(&mode_list_lock);
      mitem = __find_mode(kind);
      if (!mitem) {
@@ -483,6 +489,8 @@ static const struct team_mode *team_mode_get(const char *kind)
      }
 
      spin_unlock(&mode_list_lock);
+      if (put)
+              module_put(THIS_MODULE);

Can't you just put this under the same "if IS_MODULE" statement and avoid the "put" variable? Or in case the statement is not needed, just do plain module_put call.

Quoted 2 lines

Otherwise, the patch looks fine.

You're right, If the team is not a module, THIS_MODULE will be null. try_module_get() and module_put() deal with NULL well. So, I will send a v2 patch that will remove the #if statement and the put variable.

Thanks a lot!
Taehee Yoo