* [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity
@ 2024-07-09 14:43 Breno Leitao
2024-07-09 14:43 ` [PATCH net-next 1/3] net: netconsole: Remove unnecessary cast from bool Breno Leitao
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Breno Leitao @ 2024-07-09 14:43 UTC (permalink / raw)
To: davem, edumazet, pabeni; +Cc: thepacketgeek, riel, horms, netdev, linux-kernel
This patchset addresses a potential bug in netconsole where the netconsole
target is cleaned up before it is disabled. This sequence could lead to a
situation where an enabled target has an uninitialized netpoll structure,
potentially causing undefined behavior.
The main goals of this patchset are:
1. Correct the order of operations:
- First, disable the netconsole target
- Then, clean up the netpoll structure
2. Improve code readability:
- Remove unnecessary casts
- Eliminate redundant operations
These changes aim to enhance the reliability of netconsole by
eliminating the potential race condition and improve maintainability by
making the code more straightforward to understand and modify.
Breno Leitao (3):
net: netconsole: Remove unnecessary cast from bool
net: netconsole: Eliminate redundant setting of enabled field
net: netconsole: Disable target before netpoll cleanup
drivers/net/netconsole.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH net-next 1/3] net: netconsole: Remove unnecessary cast from bool
2024-07-09 14:43 [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity Breno Leitao
@ 2024-07-09 14:43 ` Breno Leitao
2024-07-09 14:44 ` [PATCH net-next 2/3] net: netconsole: Eliminate redundant setting of enabled field Breno Leitao
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Breno Leitao @ 2024-07-09 14:43 UTC (permalink / raw)
To: davem, edumazet, pabeni, Jakub Kicinski
Cc: thepacketgeek, riel, horms, netdev, linux-kernel
The 'enabled' variable is already a bool, so casting it to its value
is redundant.
Remove the superfluous cast, improving code clarity without changing
functionality.
Signed-off-by: Breno Leitao <leitao@debian.org>
---
drivers/net/netconsole.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index ab8a0623b1a1..de0d89e4e4e2 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -344,7 +344,7 @@ static ssize_t enabled_store(struct config_item *item,
goto out_unlock;
err = -EINVAL;
- if ((bool)enabled == nt->enabled) {
+ if (enabled == nt->enabled) {
pr_info("network logging has already %s\n",
nt->enabled ? "started" : "stopped");
goto out_unlock;
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH net-next 2/3] net: netconsole: Eliminate redundant setting of enabled field
2024-07-09 14:43 [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity Breno Leitao
2024-07-09 14:43 ` [PATCH net-next 1/3] net: netconsole: Remove unnecessary cast from bool Breno Leitao
@ 2024-07-09 14:44 ` Breno Leitao
2024-07-09 14:44 ` [PATCH net-next 3/3] net: netconsole: Disable target before netpoll cleanup Breno Leitao
2024-07-12 2:00 ` [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity patchwork-bot+netdevbpf
3 siblings, 0 replies; 6+ messages in thread
From: Breno Leitao @ 2024-07-09 14:44 UTC (permalink / raw)
To: davem, edumazet, pabeni, Jakub Kicinski
Cc: thepacketgeek, riel, horms, netdev, linux-kernel
When disabling a netconsole target, enabled_store() is called with
enabled=false. Currently, this results in updating the nt->enabled
field twice:
1. Inside the if/else block, with the target_list_lock spinlock held
2. Later, without the target_list_lock
This patch eliminates the redundancy by setting the field only once,
improving efficiency and reducing potential race conditions.
Signed-off-by: Breno Leitao <leitao@debian.org>
---
drivers/net/netconsole.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index de0d89e4e4e2..5ef680cf994a 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -369,6 +369,7 @@ static ssize_t enabled_store(struct config_item *item,
if (err)
goto out_unlock;
+ nt->enabled = true;
pr_info("network logging started\n");
} else { /* false */
/* We need to disable the netconsole before cleaning it up
@@ -381,8 +382,6 @@ static ssize_t enabled_store(struct config_item *item,
netpoll_cleanup(&nt->np);
}
- nt->enabled = enabled;
-
mutex_unlock(&dynamic_netconsole_mutex);
return strnlen(buf, count);
out_unlock:
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH net-next 3/3] net: netconsole: Disable target before netpoll cleanup
2024-07-09 14:43 [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity Breno Leitao
2024-07-09 14:43 ` [PATCH net-next 1/3] net: netconsole: Remove unnecessary cast from bool Breno Leitao
2024-07-09 14:44 ` [PATCH net-next 2/3] net: netconsole: Eliminate redundant setting of enabled field Breno Leitao
@ 2024-07-09 14:44 ` Breno Leitao
2024-07-12 1:32 ` Jakub Kicinski
2024-07-12 2:00 ` [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity patchwork-bot+netdevbpf
3 siblings, 1 reply; 6+ messages in thread
From: Breno Leitao @ 2024-07-09 14:44 UTC (permalink / raw)
To: davem, edumazet, pabeni, Jakub Kicinski
Cc: thepacketgeek, riel, horms, netdev, linux-kernel
Currently, netconsole cleans up the netpoll structure before disabling
the target. This approach can lead to race conditions, as message
senders (write_ext_msg() and write_msg()) check if the target is
enabled before using netpoll.
This patch reverses the order of operations:
1. Disable the target
2. Clean up the netpoll structure
This change eliminates the potential race condition, ensuring that
no messages are sent through a partially cleaned-up netpoll structure.
Signed-off-by: Breno Leitao <leitao@debian.org>
---
drivers/net/netconsole.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index 5ef680cf994a..9c09293b5258 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -973,6 +973,7 @@ static int netconsole_netdev_event(struct notifier_block *this,
/* rtnl_lock already held
* we might sleep in __netpoll_cleanup()
*/
+ nt->enabled = false;
spin_unlock_irqrestore(&target_list_lock, flags);
__netpoll_cleanup(&nt->np);
@@ -980,7 +981,6 @@ static int netconsole_netdev_event(struct notifier_block *this,
spin_lock_irqsave(&target_list_lock, flags);
netdev_put(nt->np.dev, &nt->np.dev_tracker);
nt->np.dev = NULL;
- nt->enabled = false;
stopped = true;
netconsole_target_put(nt);
goto restart;
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH net-next 3/3] net: netconsole: Disable target before netpoll cleanup
2024-07-09 14:44 ` [PATCH net-next 3/3] net: netconsole: Disable target before netpoll cleanup Breno Leitao
@ 2024-07-12 1:32 ` Jakub Kicinski
0 siblings, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2024-07-12 1:32 UTC (permalink / raw)
To: Breno Leitao
Cc: davem, edumazet, pabeni, thepacketgeek, riel, horms, netdev,
linux-kernel
On Tue, 9 Jul 2024 07:44:01 -0700 Breno Leitao wrote:
> Currently, netconsole cleans up the netpoll structure before disabling
> the target. This approach can lead to race conditions, as message
> senders (write_ext_msg() and write_msg()) check if the target is
> enabled before using netpoll.
>
> This patch reverses the order of operations:
> 1. Disable the target
> 2. Clean up the netpoll structure
>
> This change eliminates the potential race condition, ensuring that
> no messages are sent through a partially cleaned-up netpoll structure.
I think this is a legit fix, please add a Fixes tag and resend for net.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity
2024-07-09 14:43 [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity Breno Leitao
` (2 preceding siblings ...)
2024-07-09 14:44 ` [PATCH net-next 3/3] net: netconsole: Disable target before netpoll cleanup Breno Leitao
@ 2024-07-12 2:00 ` patchwork-bot+netdevbpf
3 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-07-12 2:00 UTC (permalink / raw)
To: Breno Leitao
Cc: davem, edumazet, pabeni, thepacketgeek, riel, horms, netdev,
linux-kernel
Hello:
This series was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Tue, 9 Jul 2024 07:43:58 -0700 you wrote:
> This patchset addresses a potential bug in netconsole where the netconsole
> target is cleaned up before it is disabled. This sequence could lead to a
> situation where an enabled target has an uninitialized netpoll structure,
> potentially causing undefined behavior.
>
> The main goals of this patchset are:
>
> [...]
Here is the summary with links:
- [net-next,1/3] net: netconsole: Remove unnecessary cast from bool
https://git.kernel.org/netdev/net-next/c/a9359e8b0065
- [net-next,2/3] net: netconsole: Eliminate redundant setting of enabled field
https://git.kernel.org/netdev/net-next/c/0066623d4008
- [net-next,3/3] net: netconsole: Disable target before netpoll cleanup
(no matching commit)
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-07-12 2:00 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-09 14:43 [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity Breno Leitao
2024-07-09 14:43 ` [PATCH net-next 1/3] net: netconsole: Remove unnecessary cast from bool Breno Leitao
2024-07-09 14:44 ` [PATCH net-next 2/3] net: netconsole: Eliminate redundant setting of enabled field Breno Leitao
2024-07-09 14:44 ` [PATCH net-next 3/3] net: netconsole: Disable target before netpoll cleanup Breno Leitao
2024-07-12 1:32 ` Jakub Kicinski
2024-07-12 2:00 ` [PATCH net-next 0/3] netconsole: Fix potential race condition and improve code clarity patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).