public inbox for linux-wireless@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns()
@ 2025-04-07 12:53 Ivan Abramov
  2025-04-07 12:53 ` [PATCH v2 1/3] cfg80211: Restore initial state on failed device_rename() " Ivan Abramov
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Ivan Abramov @ 2025-04-07 12:53 UTC (permalink / raw)
  To: Johannes Berg; +Cc: Ivan Abramov, linux-wireless, linux-kernel, lvc-project

This series is inspired by similar series in ieee802154. As far as I know,
there were no WARN_ON triggers in this code so far, but since it's almost
identical to cfg802154_* counterparts, same changes are proposed.

Link to ieee802154 series: https://lore.kernel.org/netdev/20250403101935.991385-1-i.abramov@mt-integration.ru/T/

v2: Add 2 patches. Also make sure to commit against latest
netdev/net.

Ivan Abramov (3):
  cfg80211: Restore initial state on failed device_rename() in
    cfg80211_switch_netns()
  cfg80211: Avoid calling WARN_ON() on -ENOMEM in
    cfg80211_switch_netns()
  cfg80211: Remove WARN_ON() in cfg80211_pernet_exit()

 net/wireless/core.c | 53 +++++++++++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 23 deletions(-)

-- 
2.39.5


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/3] cfg80211: Restore initial state on failed device_rename() in cfg80211_switch_netns()
  2025-04-07 12:53 [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns() Ivan Abramov
@ 2025-04-07 12:53 ` Ivan Abramov
  2025-04-23 15:44   ` Johannes Berg
  2025-04-07 12:53 ` [PATCH v2 2/3] cfg80211: Avoid calling WARN_ON() on -ENOMEM " Ivan Abramov
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Ivan Abramov @ 2025-04-07 12:53 UTC (permalink / raw)
  To: Johannes Berg
  Cc: Ivan Abramov, Kuniyuki Iwashima, linux-wireless, linux-kernel,
	lvc-project

Currently, the return value of device_rename() is not acted upon.

To avoid an inconsistent state in case of failure, roll back the changes
made before the device_rename() call.

Found by Linux Verification Center (linuxtesting.org).

Fixes: 04600794958f ("cfg80211: support sysfs namespaces")
Suggested-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Ivan Abramov <i.abramov@mt-integration.ru>
---
 net/wireless/core.c | 47 +++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 21 deletions(-)

diff --git a/net/wireless/core.c b/net/wireless/core.c
index 9e6b31903121..e4d353ec9436 100644
--- a/net/wireless/core.c
+++ b/net/wireless/core.c
@@ -169,27 +169,17 @@ int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
 		wdev->netdev->netns_immutable = true;
 	}
 
-	if (err) {
-		/* failed -- clean up to old netns */
-		net = wiphy_net(&rdev->wiphy);
-
-		list_for_each_entry_continue_reverse(wdev,
-						     &rdev->wiphy.wdev_list,
-						     list) {
-			if (!wdev->netdev)
-				continue;
-			wdev->netdev->netns_immutable = false;
-			err = dev_change_net_namespace(wdev->netdev, net,
-							"wlan%d");
-			WARN_ON(err);
-			wdev->netdev->netns_immutable = true;
-		}
-
-		return err;
-	}
+	if (err)
+		goto errout;
 
 	guard(wiphy)(&rdev->wiphy);
 
+	err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
+	WARN_ON(err);
+
+	if (err)
+		goto errout;
+
 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
 		if (!wdev->netdev)
 			continue;
@@ -200,9 +190,6 @@ int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
 
 	wiphy_net_set(&rdev->wiphy, net);
 
-	err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
-	WARN_ON(err);
-
 	nl80211_notify_wiphy(rdev, NL80211_CMD_NEW_WIPHY);
 
 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
@@ -212,6 +199,24 @@ int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
 	}
 
 	return 0;
+
+errout:
+	/* failed -- clean up to old netns */
+	net = wiphy_net(&rdev->wiphy);
+
+	list_for_each_entry_continue_reverse(wdev,
+					     &rdev->wiphy.wdev_list,
+					     list) {
+		if (!wdev->netdev)
+			continue;
+		wdev->netdev->netns_immutable = false;
+		err = dev_change_net_namespace(wdev->netdev, net,
+					       "wlan%d");
+		WARN_ON(err);
+		wdev->netdev->netns_immutable = true;
+	}
+
+	return err;
 }
 
 static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/3] cfg80211: Avoid calling WARN_ON() on -ENOMEM in cfg80211_switch_netns()
  2025-04-07 12:53 [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns() Ivan Abramov
  2025-04-07 12:53 ` [PATCH v2 1/3] cfg80211: Restore initial state on failed device_rename() " Ivan Abramov
@ 2025-04-07 12:53 ` Ivan Abramov
  2025-04-07 12:53 ` [PATCH v2 3/3] cfg80211: Remove WARN_ON() in cfg80211_pernet_exit() Ivan Abramov
  2025-04-07 17:29 ` [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns() Kuniyuki Iwashima
  3 siblings, 0 replies; 7+ messages in thread
From: Ivan Abramov @ 2025-04-07 12:53 UTC (permalink / raw)
  To: Johannes Berg
  Cc: Ivan Abramov, Kuniyuki Iwashima, linux-wireless, linux-kernel,
	lvc-project

It's pointless to call WARN_ON() in case of an allocation failure in
dev_change_net_namespace() and device_rename(), since it only leads to
useless splats caused by deliberate fault injections, so avoid it.

Found by Linux Verification Center (linuxtesting.org).

Fixes: 04600794958f ("cfg80211: support sysfs namespaces")
Fixes: 463d01832385 ("cfg80211: make aware of net namespaces")
Suggested-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Ivan Abramov <i.abramov@mt-integration.ru>
---
 net/wireless/core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/net/wireless/core.c b/net/wireless/core.c
index e4d353ec9436..6f95aad18d47 100644
--- a/net/wireless/core.c
+++ b/net/wireless/core.c
@@ -164,8 +164,10 @@ int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
 			continue;
 		wdev->netdev->netns_immutable = false;
 		err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
-		if (err)
+		if (err) {
+			WARN_ON(err && err != -ENOMEM);
 			break;
+		}
 		wdev->netdev->netns_immutable = true;
 	}
 
@@ -175,7 +177,7 @@ int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
 	guard(wiphy)(&rdev->wiphy);
 
 	err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
-	WARN_ON(err);
+	WARN_ON(err && err != -ENOMEM);
 
 	if (err)
 		goto errout;
@@ -212,7 +214,7 @@ int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
 		wdev->netdev->netns_immutable = false;
 		err = dev_change_net_namespace(wdev->netdev, net,
 						"wlan%d");
-		WARN_ON(err);
+		WARN_ON(err && err != -ENOMEM);
 		wdev->netdev->netns_immutable = true;
 	}
 
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/3] cfg80211: Remove WARN_ON() in cfg80211_pernet_exit()
  2025-04-07 12:53 [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns() Ivan Abramov
  2025-04-07 12:53 ` [PATCH v2 1/3] cfg80211: Restore initial state on failed device_rename() " Ivan Abramov
  2025-04-07 12:53 ` [PATCH v2 2/3] cfg80211: Avoid calling WARN_ON() on -ENOMEM " Ivan Abramov
@ 2025-04-07 12:53 ` Ivan Abramov
  2025-04-07 17:29 ` [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns() Kuniyuki Iwashima
  3 siblings, 0 replies; 7+ messages in thread
From: Ivan Abramov @ 2025-04-07 12:53 UTC (permalink / raw)
  To: Johannes Berg
  Cc: Ivan Abramov, Kuniyuki Iwashima, linux-wireless, linux-kernel,
	lvc-project

There's no need to call WARN_ON() in cfg80211_pernet_exit(), since
every point of failure in cfg80211_switch_netns() is covered with
WARN_ON(), so remove it.

Found by Linux Verification Center (linuxtesting.org).

Fixes: 463d01832385 ("cfg80211: make aware of net namespaces")
Suggested-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Ivan Abramov <i.abramov@mt-integration.ru>
---
 net/wireless/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/wireless/core.c b/net/wireless/core.c
index 6f95aad18d47..b77f4ed8f2c0 100644
--- a/net/wireless/core.c
+++ b/net/wireless/core.c
@@ -1656,7 +1656,7 @@ static void __net_exit cfg80211_pernet_exit(struct net *net)
 	rtnl_lock();
 	for_each_rdev(rdev) {
 		if (net_eq(wiphy_net(&rdev->wiphy), net))
-			WARN_ON(cfg80211_switch_netns(rdev, &init_net));
+			cfg80211_switch_netns(rdev, &init_net);
 	}
 	rtnl_unlock();
 }
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns()
  2025-04-07 12:53 [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns() Ivan Abramov
                   ` (2 preceding siblings ...)
  2025-04-07 12:53 ` [PATCH v2 3/3] cfg80211: Remove WARN_ON() in cfg80211_pernet_exit() Ivan Abramov
@ 2025-04-07 17:29 ` Kuniyuki Iwashima
  3 siblings, 0 replies; 7+ messages in thread
From: Kuniyuki Iwashima @ 2025-04-07 17:29 UTC (permalink / raw)
  To: i.abramov; +Cc: johannes, linux-kernel, linux-wireless, lvc-project, netdev

From: Ivan Abramov <i.abramov@mt-integration.ru>
Date: Mon, 7 Apr 2025 15:53:41 +0300
> This series is inspired by similar series in ieee802154. As far as I know,
> there were no WARN_ON triggers in this code so far, but since it's almost
> identical to cfg802154_* counterparts, same changes are proposed.
> 
> Link to ieee802154 series: https://lore.kernel.org/netdev/20250403101935.991385-1-i.abramov@mt-integration.ru/T/
> 
> v2: Add 2 patches. Also make sure to commit against latest
> netdev/net.
> 
> Ivan Abramov (3):
>   cfg80211: Restore initial state on failed device_rename() in
>     cfg80211_switch_netns()
>   cfg80211: Avoid calling WARN_ON() on -ENOMEM in
>     cfg80211_switch_netns()
>   cfg80211: Remove WARN_ON() in cfg80211_pernet_exit()

Let's see how this thread goes first.
https://lore.kernel.org/netdev/CANn89i+UQQ6GqhWisHQEL0ECNFoQqVrO+2Ee3oDzysdR7dh=Ag@mail.gmail.com/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/3] cfg80211: Restore initial state on failed device_rename() in cfg80211_switch_netns()
  2025-04-07 12:53 ` [PATCH v2 1/3] cfg80211: Restore initial state on failed device_rename() " Ivan Abramov
@ 2025-04-23 15:44   ` Johannes Berg
  2025-04-23 18:16     ` Kuniyuki Iwashima
  0 siblings, 1 reply; 7+ messages in thread
From: Johannes Berg @ 2025-04-23 15:44 UTC (permalink / raw)
  To: Ivan Abramov
  Cc: Kuniyuki Iwashima, linux-wireless, linux-kernel, lvc-project,
	netdev

On Mon, 2025-04-07 at 15:53 +0300, Ivan Abramov wrote:
> Currently, the return value of device_rename() is not acted upon.
> 
> To avoid an inconsistent state in case of failure, roll back the changes
> made before the device_rename() call.

This kind of seems complicated for something that ought to not happen
...

And also (+netdev), what do we do in case this is called from
cfg80211_pernet_exit() - leak the whole network namespace because we
couldn't allocate memory for the name? That seems counterproductive.

I'm really not convinced of this whole patchset.

johannes


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/3] cfg80211: Restore initial state on failed device_rename() in cfg80211_switch_netns()
  2025-04-23 15:44   ` Johannes Berg
@ 2025-04-23 18:16     ` Kuniyuki Iwashima
  0 siblings, 0 replies; 7+ messages in thread
From: Kuniyuki Iwashima @ 2025-04-23 18:16 UTC (permalink / raw)
  To: johannes
  Cc: i.abramov, kuniyu, linux-kernel, linux-wireless, lvc-project,
	netdev

From: Johannes Berg <johannes@sipsolutions.net>
Date: Wed, 23 Apr 2025 17:44:45 +0200
> On Mon, 2025-04-07 at 15:53 +0300, Ivan Abramov wrote:
> > Currently, the return value of device_rename() is not acted upon.
> > 
> > To avoid an inconsistent state in case of failure, roll back the changes
> > made before the device_rename() call.
> 
> This kind of seems complicated for something that ought to not happen
> ...
> 
> And also (+netdev), what do we do in case this is called from
> cfg80211_pernet_exit() - leak the whole network namespace because we
> couldn't allocate memory for the name? That seems counterproductive.

default_device_exit_net() does BUG() in such a case, it doens't
assume -ENOMEM as we are freeing memory in the netns dismantle.


static void __net_exit default_device_exit_net(struct net *net)
{
...
	for_each_netdev_safe(net, dev, aux) {
...
		err = dev_change_net_namespace(dev, &init_net, fb_name);
		if (err) {
			pr_emerg("%s: failed to move %s to init_net: %d\n",
				 __func__, dev->name, err);
			BUG();
		}
	}
}

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-04-23 18:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-07 12:53 [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns() Ivan Abramov
2025-04-07 12:53 ` [PATCH v2 1/3] cfg80211: Restore initial state on failed device_rename() " Ivan Abramov
2025-04-23 15:44   ` Johannes Berg
2025-04-23 18:16     ` Kuniyuki Iwashima
2025-04-07 12:53 ` [PATCH v2 2/3] cfg80211: Avoid calling WARN_ON() on -ENOMEM " Ivan Abramov
2025-04-07 12:53 ` [PATCH v2 3/3] cfg80211: Remove WARN_ON() in cfg80211_pernet_exit() Ivan Abramov
2025-04-07 17:29 ` [PATCH v2 0/3] Avoid calling WARN_ON() on allocation failure in cfg80211_switch_netns() Kuniyuki Iwashima

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox