Skip to contentSkip to conversation

ieee802154: hwsim: free PIB after unregistering hardware

PATCH In review Yousef Alhouseen 3 participants 6 messages Jun 27 – Sep 2

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

PATCH 1/1 ieee802154: hwsim: free PIB after unregistering hardware

hwsim_del() queues the currently published PIB for RCU freeing before unregistering the hardware. The unregister path can still invoke driver callbacks, including set_promiscuous_mode(), after that grace period has started. A callback can consequently dereference the freed PIB.

Unregister the hardware first, then fetch and free the final PIB. This also handles a PIB replacement performed by a callback during unregister.

Fixes 1c9f4a3fce77 ("ieee802154: hwsim: fix rcu handling")
Reported-by [email protected]
---
 drivers/net/ieee802154/mac802154_hwsim.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/net/ieee802154/mac802154_hwsim.c +2 -3
index 6daa0f198..2a2d8a9eb 100644
@@ -1004,12 +1004,11 @@ static void hwsim_del(struct hwsim_phy *phy)
 		list_del_rcu(&e->list);
 		hwsim_free_edge(e);
 	}
-	pib = rcu_dereference(phy->pib);
 	rcu_read_unlock();
 
-	kfree_rcu(pib, rcu);
-
 	ieee802154_unregister_hw(phy->hw);
+	pib = rcu_dereference_protected(phy->pib, 1);
+	kfree_rcu(pib, rcu);
 	ieee802154_free_hw(phy->hw);
 }
 
Signature · 1 line
2.54.0
maintainer
#1 lore raw

Hello Yousef,

Quoted 4 lines
drivers/net/ieee802154/mac802154_hwsim.c +0 -0
@@ -1004,12 +1004,11 @@ static void hwsim_del(struct hwsim_phy *phy)
 		list_del_rcu(&e->list);
 		hwsim_free_edge(e);
 	}

drivers/net/ieee802154/mac802154_hwsim.c static void hwsim_del(struct hwsim_phy *phy) · line 1013

Quoted 10 lines
drivers/net/ieee802154/mac802154_hwsim.c +2 -3
-	pib = rcu_dereference(phy->pib);
 	rcu_read_unlock();
 
-	kfree_rcu(pib, rcu);
-
 	ieee802154_unregister_hw(phy->hw);
+	pib = rcu_dereference_protected(phy->pib, 1);
+	kfree_rcu(pib, rcu);
 	ieee802154_free_hw(phy->hw);
 }

Would you mind justifying the choice for the _protected() version, please?

Thanks,
Miquèl

#2 lore raw

Hello Miquel,

After ieee802154_unregister_hw() returns, the driver callbacks that can replace phy->pib have been quiesced, and hwsim_del() has exclusive ownership of the final teardown. The pointer is no longer being fetched inside an RCU read-side critical section, so rcu_dereference() is not appropriate there.

rcu_dereference_protected(phy->pib, 1) expresses that there can no longer be a concurrent updater at that point; the protection condition is the completed unregister rather than a locally held lock. The value is only fetched so the final object can be passed to kfree_rcu().

rcu_access_pointer() would also be sufficient for that limited use if you prefer it, and I can use that spelling in a v2.

Thanks,
Yousef

Quoted 22 lines · Miquel Raynal

Hello Yousef,

Quoted 4 lines
drivers/net/ieee802154/mac802154_hwsim.c +0 -0
@@ -1004,12 +1004,11 @@ static void hwsim_del(struct hwsim_phy *phy)
 list_del_rcu(&e->list);
 hwsim_free_edge(e);
 }

drivers/net/ieee802154/mac802154_hwsim.c static void hwsim_del(struct hwsim_phy *phy) · line 1013

Quoted 10 lines
drivers/net/ieee802154/mac802154_hwsim.c +2 -3
- pib = rcu_dereference(phy->pib);
 rcu_read_unlock();
 
- kfree_rcu(pib, rcu);
-
 ieee802154_unregister_hw(phy->hw);
+ pib = rcu_dereference_protected(phy->pib, 1);
+ kfree_rcu(pib, rcu);
 ieee802154_free_hw(phy->hw);
 }

Would you mind justifying the choice for the _protected() version, please?

Thanks,
Miquèl

maintainer
#3 lore raw

Hello Yousef,

Quoted 7 lines · Yousef Alhouseen

Hello Miquel,

After ieee802154_unregister_hw() returns, the driver callbacks that can replace phy->pib have been quiesced, and hwsim_del() has exclusive ownership of the final teardown. The pointer is no longer being fetched inside an RCU read-side critical section, so rcu_dereference() is not appropriate there.

That's right.

Quoted 4 lines

rcu_dereference_protected(phy->pib, 1) expresses that there can no longer be a concurrent updater at that point; the protection condition is the completed unregister rather than a locally held lock. The value is only fetched so the final object can be passed to kfree_rcu().

While I believe this is indeed true, it actually feels slightly overkill since there is no updater anymore and, as far as I understand, the only thing that we need here is to get the pointer value for freeing, right?

Quoted 2 lines

rcu_access_pointer() would also be sufficient for that limited use if you prefer it, and I can use that spelling in a v2.

If you don't mind, I feel like rcu_access_pointer() is exactly what we need here. It is also more understandable, even though I agree in practice there are no differences in this case.

Thanks,
Miquèl

#4 lore raw

Agreed. I'll use rcu_access_pointer() in v2; it expresses the final pointer fetch more directly once unregister has quiesced the updaters.

Thanks,
Yousef

Quoted 32 lines · Miquel Raynal

Hello Yousef,

Quoted 7 lines · Yousef Alhouseen

Hello Miquel,

After ieee802154_unregister_hw() returns, the driver callbacks that can replace phy->pib have been quiesced, and hwsim_del() has exclusive ownership of the final teardown. The pointer is no longer being fetched inside an RCU read-side critical section, so rcu_dereference() is not appropriate there.

That's right.

Quoted 4 lines

rcu_dereference_protected(phy->pib, 1) expresses that there can no longer be a concurrent updater at that point; the protection condition is the completed unregister rather than a locally held lock. The value is only fetched so the final object can be passed to kfree_rcu().

While I believe this is indeed true, it actually feels slightly overkill since there is no updater anymore and, as far as I understand, the only thing that we need here is to get the pointer value for freeing, right?

Quoted 2 lines

rcu_access_pointer() would also be sufficient for that limited use if you prefer it, and I can use that spelling in a v2.

If you don't mind, I feel like rcu_access_pointer() is exactly what we need here. It is also more understandable, even though I agree in practice there are no differences in this case.

Thanks,
Miquèl

maintainer
#5 lore raw

Hello Yousef,

Quoted 2 lines · Yousef Alhouseen

Agreed. I'll use rcu_access_pointer() in v2; it expresses the final pointer fetch more directly once unregister has quiesced the updaters.

Did you send a v2? Did I miss it?

regards
Stefan Schmidt

Quoted 37 lines

Thanks,
Yousef

Quoted 32 lines · Miquel Raynal

Hello Yousef,

Quoted 7 lines · Yousef Alhouseen

Hello Miquel,

After ieee802154_unregister_hw() returns, the driver callbacks that can replace phy->pib have been quiesced, and hwsim_del() has exclusive ownership of the final teardown. The pointer is no longer being fetched inside an RCU read-side critical section, so rcu_dereference() is not appropriate there.

That's right.

Quoted 4 lines

rcu_dereference_protected(phy->pib, 1) expresses that there can no longer be a concurrent updater at that point; the protection condition is the completed unregister rather than a locally held lock. The value is only fetched so the final object can be passed to kfree_rcu().

While I believe this is indeed true, it actually feels slightly overkill since there is no updater anymore and, as far as I understand, the only thing that we need here is to get the pointer value for freeing, right?

Quoted 2 lines

rcu_access_pointer() would also be sufficient for that limited use if you prefer it, and I can use that spelling in a v2.

If you don't mind, I feel like rcu_access_pointer() is exactly what we need here. It is also more understandable, even though I agree in practice there are no differences in this case.

Thanks,
Miquèl