Skip to contentSkip to conversation

net: intel: ice: Modernize ice_vsi_free() with scope-based guard(mutex)

PATCH In review Ayush Yaduvanshi 4 participants 4 messages Oct 14 – Oct 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: intel: ice: Modernize ice_vsi_free() with scope-based guard(mutex)

---
 drivers/net/ethernet/intel/ice/ice_lib.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ethernet/intel/ice/ice_lib.c +1 -2
index 9e08db376d3d..c695df442822 100644
@@ -457,7 +457,7 @@ void ice_vsi_free(struct ice_vsi *vsi)
 		return;
 	}
 
-	mutex_lock(&pf->sw_mutex);
+	guard(mutex)(&pf->sw_mutex);
 	/* updates the PF for this cleared VSI */
 
 	pf->vsi[vsi->idx] = NULL;
@@ -466,7 +466,6 @@ void ice_vsi_free(struct ice_vsi *vsi)
 	ice_vsi_free_stats(vsi);
 	ice_vsi_free_arrays(vsi);
 	mutex_destroy(&vsi->xdp_state_lock);
-	mutex_unlock(&pf->sw_mutex);
 	devm_kfree(dev, vsi);
 }
 
Signature · 1 line
2.53.0
#1 lore raw
Quoted 1 line · Ayush Yaduvanshi

The commit message is missing, and this patch seems to be a clean-up patch, which is discouraged in Documentation/process/maintainer-netdev.rst. Also, this document says:
Use of ``guard()`` is discouraged within any function longer than 20 lines

Quoted 18 lines
---
 drivers/net/ethernet/intel/ice/ice_lib.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ethernet/intel/ice/ice_lib.c +1 -1
index 9e08db376d3d..c695df442822 100644
@@ -457,7 +457,7 @@ void ice_vsi_free(struct ice_vsi *vsi)
 		return;
 	}
 
-	mutex_lock(&pf->sw_mutex);
+	guard(mutex)(&pf->sw_mutex);
 	/* updates the PF for this cleared VSI */
 
 	pf->vsi[vsi->idx] = NULL;

drivers/net/ethernet/intel/ice/ice_lib.c void ice_vsi_free(struct ice_vsi *vsi) · line 469

Quoted 5 lines
drivers/net/ethernet/intel/ice/ice_lib.c +0 -1
@@ -466,7 +466,6 @@ void ice_vsi_free(struct ice_vsi *vsi)
 	ice_vsi_free_stats(vsi);
 	ice_vsi_free_arrays(vsi);
 	mutex_destroy(&vsi->xdp_state_lock);
-	mutex_unlock(&pf->sw_mutex);
 	devm_kfree(dev, vsi);

This also extends the critical section, as devm_kfree() is now called while holding pf->sw_mutex.

Could you please clarify why this change is necessary?

Thanks,
Kohei

Quoted 5 lines
drivers/net/ethernet/intel/ice/ice_lib.c +0 -0
 }
 
Signature · 1 line
2.53.0
#2 lore raw
Quoted 24 lines · Kohei Enju
Quoted 1 line · Ayush Yaduvanshi

The commit message is missing, and this patch seems to be a clean-up patch, which is discouraged in Documentation/process/maintainer-netdev.rst. Also, this document says:
Use of ``guard()`` is discouraged within any function longer than 20 lines

Quoted 16 lines
---
 drivers/net/ethernet/intel/ice/ice_lib.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ethernet/intel/ice/ice_lib.c +1 -1
index 9e08db376d3d..c695df442822 100644
@@ -457,7 +457,7 @@ void ice_vsi_free(struct ice_vsi *vsi)
 		return;
 	}
 
-	mutex_lock(&pf->sw_mutex);
+	guard(mutex)(&pf->sw_mutex);
 	/* updates the PF for this cleared VSI */
 
Quoted 10 lines

drivers/net/ethernet/intel/ice/ice_lib.c void ice_vsi_free(struct ice_vsi *vsi) · line 469

Quoted 7 lines
drivers/net/ethernet/intel/ice/ice_lib.c +0 -1
 	pf->vsi[vsi->idx] = NULL;
@@ -466,7 +466,6 @@ void ice_vsi_free(struct ice_vsi *vsi)
 	ice_vsi_free_stats(vsi);
 	ice_vsi_free_arrays(vsi);
 	mutex_destroy(&vsi->xdp_state_lock);
-	mutex_unlock(&pf->sw_mutex);
 	devm_kfree(dev, vsi);

This also extends the critical section, as devm_kfree() is now called while holding pf->sw_mutex.

This is exactly why guard() is bad and should not be used.

Andrew

#3 lore raw
In reply to Ayush Yaduvanshi
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] net: ice: TOCTOU race in ice_vsi_free() leads to UAF and double-free --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: ice: TOCTOU race in ice_vsi_free() leads to UAF and double-free --

commit e281bf0eed51082082d6b00b9ef0752bccc1ae10
Author: Ayush Yaduvanshi <[email protected]>

net: intel: ice: Modernize ice_vsi_free() with scope-based guard(mutex)

This commit updates ice_vsi_free() to use a scope-based guard for pf->sw_mutex instead of explicit mutex_lock() and mutex_unlock() calls.

drivers/net/ethernet/intel/ice/ice_lib.c void ice_vsi_free(struct ice_vsi *vsi) · line 460

Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_lib.c +1 -1
index 9e08db376d3d0..c695df4428223 100644
@@ -457,7 +457,7 @@ void ice_vsi_free(struct ice_vsi *vsi)
 		return;
 	}
 
-	mutex_lock(&pf->sw_mutex);
+	guard(mutex)(&pf->sw_mutex);

[Severity: High]
Can this sequence lead to a use-after-free and double-free?

The validity check just before this lock is performed outside the mutex:

    if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
        ...
        return;
    }

Does this create a race condition where two threads can bypass the check concurrently, serialize on pf->sw_mutex, and cause the second thread to dereference and double-free the VSI after the first thread clears it?

Quoted 3 lines
drivers/net/ethernet/intel/ice/ice_lib.c +0 -0
 	/* updates the PF for this cleared VSI */
 
 	pf->vsi[vsi->idx] = NULL;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1