Skip to contentSkip to conversation

virtio_net: fix missing lock protection on control_buf access

PATCH Applied Heng Qi 4 participants 4 messages May 30, 2024 – Jun 1, 2024

Subsystems (4)
lore
Share & tools
lore

Heng Qi

author
#0 lore raw

PATCH v2 1/1 virtio_net: fix missing lock protection on control_buf access

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")

---
v1->v2:
- Use the ok instead of ret.

 drivers/net/virtio_net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +3 -1
index 4a802c0ea2cb..1ea8e6a24286 100644
@@ -2686,6 +2686,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
+	bool ok;
 	int ret;
 
 	/* Caller should know better */
@@ -2731,8 +2732,9 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 	}
 
 unlock:
+	ok = vi->ctrl->status == VIRTIO_NET_OK;
 	mutex_unlock(&vi->cvq_lock);
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	return ok;
 }
 
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
Signature · 1 line
2.32.0.3.g01195cf9f
Acked-by on v2 1/1
#1 lore raw
Quoted 39 lines · Heng Qi

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")

---
v1->v2:
- Use the ok instead of ret.

 drivers/net/virtio_net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +3 -2
index 4a802c0ea2cb..1ea8e6a24286 100644
@@ -2686,6 +2686,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 {
        struct scatterlist *sgs[5], hdr, stat;
        u32 out_num = 0, tmp, in_num = 0;
+       bool ok;
        int ret;
 
        /* Caller should know better */
@@ -2731,8 +2732,9 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
        }
 
 unlock:
+       ok = vi->ctrl->status == VIRTIO_NET_OK;
        mutex_unlock(&vi->cvq_lock);
-       return vi->ctrl->status == VIRTIO_NET_OK;
+       return ok;
 }
 
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
--
Quoted 1 line

2.32.0.3.g01195cf9f

Thanks

reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Heng Qi
Quoted 7 lines · Heng Qi

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")
Quoted 33 lines

---
v1->v2:
- Use the ok instead of ret.

 drivers/net/virtio_net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +3 -2
index 4a802c0ea2cb..1ea8e6a24286 100644
@@ -2686,6 +2686,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
+	bool ok;
 	int ret;
 
 	/* Caller should know better */
@@ -2731,8 +2732,9 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 	}
 
 unlock:
+	ok = vi->ctrl->status == VIRTIO_NET_OK;
 	mutex_unlock(&vi->cvq_lock);
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	return ok;
 }
 
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
--

2.32.0.3.g01195cf9f

#3 lore raw
In reply to Heng Qi
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Thu, 30 May 2024 11:41:43 +0800 you wrote:

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")

[...]

Here is the summary with links:
- [net,v2] virtio_net: fix missing lock protection on control_buf access

    https://git.kernel.org/netdev/net/c/30636258a7c9

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html