Netvsc: Call hv_unmap_memory() in the netvsc_device_remove()
PATCH 1/1 Netvsc: Call hv_unmap_memory() in the netvsc_device_remove()
From: Tianyu Lan <[email protected]>
netvsc_device_remove() calls vunmap() inside which should not be called in the interrupt context. Current code calls hv_unmap_memory() in the free_netvsc_device() which is rcu callback and maybe called in the interrupt context. This will trigger BUG_ON(in_interrupt()) in the vunmap(). Fix it via moving hv_unmap_memory() to netvsc_device_ remove().
--- drivers/net/hyperv/netvsc.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/net/hyperv/netvsc.c +10 -8
@@ -154,19 +154,15 @@ static void free_netvsc_device(struct rcu_head *head)kfree (nvdev->extension); -if (nvdev->recv_original_buf) { -hv_unmap_memory (nvdev->recv_buf); +if (nvdev->recv_original_buf)vfree (nvdev->recv_original_buf); - }else { +else vfree (nvdev->recv_buf); - } -if (nvdev->send_original_buf) { -hv_unmap_memory (nvdev->send_buf); +if (nvdev->send_original_buf)vfree (nvdev->send_original_buf); - }else { +else vfree (nvdev->send_buf); - }bitmap_free (nvdev->send_section_map);
@@ -765,6 +761,12 @@ void netvsc_device_remove(struct hv_device *device)netvsc_teardown_send_gpadl (device, net_device, ndev); } +if (net_device->recv_original_buf) +hv_unmap_memory (net_device->recv_buf); + +if (net_device->send_original_buf) +hv_unmap_memory (net_device->send_buf); +/* Release all resources */ free_netvsc_device_rcu (net_device); }
Signature · 1 line
2.25.1
Quoted 61 lines
-----Original Message-----
From: Tianyu Lan <[email protected]>
Sent: Tuesday, February 1, 2022 11:32 AM
To: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; Dexuan Cui <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Michael Kelley (LINUX) <[email protected]>
Cc: Tianyu Lan <[email protected]>; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: [PATCH] Netvsc: Callhv_unmap_memory()in thenetvsc_device_remove()From: Tianyu Lan <[email protected]>
netvsc_device_remove()callsvunmap()inside which should not be called in the interrupt context. Current code callshv_unmap_memory()in thefree_netvsc_device()which is rcu callback and maybe called in the interrupt context. This will trigger BUG_ON(in_interrupt()) in thevunmap(). Fix it via movinghv_unmap_memory()tonetvsc_device_remove().Fixes 846da38de0e8 ("net: netvsc: Add Isolation VM support for netvsc driver")Signed-off-by Tianyu Lan <[email protected]>--- drivers/net/hyperv/netvsc.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)drivers/net/hyperv/netvsc.c +4
-8@@ -154,19 +154,15 @@ static void free_netvsc_device(struct rcu_head *head)kfree (nvdev->extension); -if (nvdev->recv_original_buf) { -hv_unmap_memory (nvdev->recv_buf); +if (nvdev->recv_original_buf)vfree (nvdev->recv_original_buf); - }else { +else vfree (nvdev->recv_buf); - } -if (nvdev->send_original_buf) { -hv_unmap_memory (nvdev->send_buf); +if (nvdev->send_original_buf)vfree (nvdev->send_original_buf); - }else { +else vfree (nvdev->send_buf); - }bitmap_free (nvdev->send_section_map);@@ -765,6 +761,12 @@ void netvsc_device_remove(struct hv_device *device)netvsc_teardown_send_gpadl (device, net_device, ndev); }
drivers/net/hyperv/netvsc.c void netvsc_device_remove(struct hv_device *device) · line 772
Quoted 10 lines
drivers/net/hyperv/netvsc.c +6
-0+if (net_device->recv_original_buf) +hv_unmap_memory (net_device->recv_buf); + +if (net_device->send_original_buf) +hv_unmap_memory (net_device->send_buf); +/* Release all resources */ free_netvsc_device_rcu (net_device); }
From: Tianyu Lan <[email protected]> Sent: Tuesday, February 1, 2022 8:32 AM
Quoted 7 lines
netvsc_device_remove()callsvunmap()inside which should not be called in the interrupt context. Current code callshv_unmap_memory()in thefree_netvsc_device()which is rcu callback and maybe called in the interrupt context. This will trigger BUG_ON(in_interrupt()) in thevunmap(). Fix it via movinghv_unmap_memory()tonetvsc_device_remove().
I think this change can fail to call hv_unmap_memory() in an error case.
If netvsc_init_buf() fails after hv_map_memory() succeeds for the receive buffer or for the send buffer, no corresponding hv_unmap_memory() will be done. The failure in netvsc_init_buf() will cause netvsc_connect_vsp() to fail, so netvsc_add_device() will "goto close" where free_netvsc_device() will be called. But free_netvsc_device() no longer calls hv_unmap_memory(),
so it won't ever happen. netvsc_device_remove() is never called in this case
because netvsc_add_device() failed.
Michael
Quoted 50 lines
Fixes 846da38de0e8 ("net: netvsc: Add Isolation VM support for netvsc driver")Signed-off-by Tianyu Lan <[email protected]>--- drivers/net/hyperv/netvsc.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)drivers/net/hyperv/netvsc.c +10
-9@@ -154,19 +154,15 @@ static void free_netvsc_device(struct rcu_head *head)kfree (nvdev->extension); -if (nvdev->recv_original_buf) { -hv_unmap_memory (nvdev->recv_buf); +if (nvdev->recv_original_buf)vfree (nvdev->recv_original_buf); - }else { +else vfree (nvdev->recv_buf); - } -if (nvdev->send_original_buf) { -hv_unmap_memory (nvdev->send_buf); +if (nvdev->send_original_buf)vfree (nvdev->send_original_buf); - }else { +else vfree (nvdev->send_buf); - }bitmap_free (nvdev->send_section_map);@@ -765,6 +761,12 @@ void netvsc_device_remove(struct hv_device *device)netvsc_teardown_send_gpadl (device, net_device, ndev); } +if (net_device->recv_original_buf) +hv_unmap_memory (net_device->recv_buf); + +if (net_device->send_original_buf) +hv_unmap_memory (net_device->send_buf); +/* Release all resources */ free_netvsc_device_rcu (net_device); } --2.25.1
Quoted 9 lines · Michael Kelley (LINUX)
From: Tianyu Lan<[email protected]> Sent: Tuesday, February 1, 2022 8:32 AM
Quoted 6 lines
netvsc_device_remove()callsvunmap()inside which should not be called in the interrupt context. Current code callshv_unmap_memory()in thefree_netvsc_device()which is rcu callback and maybe called in the interrupt context. This will trigger BUG_ON(in_interrupt()) in thevunmap(). Fix it via movinghv_unmap_memory()tonetvsc_device_remove().I think this change can fail to call
hv_unmap_memory()in an error case.
Quoted 8 lines
If
netvsc_init_buf()fails afterhv_map_memory()succeeds for the receive buffer or for the send buffer, no correspondinghv_unmap_memory()will be done. The failure innetvsc_init_buf()will causenetvsc_connect_vsp()to fail, sonetvsc_add_device()will "goto close" wherefree_netvsc_device()will be called. Butfree_netvsc_device()no longer callshv_unmap_memory(),
so it won't ever happen.netvsc_device_remove()is never called in this case
becausenetvsc_add_device()failed.
Hi Michael:
Thanks for your review. Nice catch and will fix in the next
version.