net: make for_each_netdev_dump() a little more bug-proof
Subsystems (3)
PATCH 1/1 net: make for_each_netdev_dump() a little more bug-proof
From: Jakub Kicinski <[email protected]>
commit f22b4b55edb507a2b30981e133b66b642be4d13f upstream.
I find the behavior of xa_for_each_start() slightly counter-intuitive. It doesn't end the iteration by making the index point after the last element. IOW calling xa_for_each_start() again after it "finished" will run the body of the loop for the last valid element, instead of doing nothing.
This works fine for netlink dumps if they terminate correctly (i.e. coalesce or carefully handle NLM_DONE), but as we keep getting reminded legacy dumps are unlikely to go away.
Fixing this generically at the xa_for_each_start() level seems hard - there is no index reserved for "end of iteration". ifindexes are 31b wide, tho, and iterator is ulong so for for_each_netdev_dump() it's safe to go to the next element.
---
The mctp RTM_GETADDR rework backport of acab78ae12c7 ("net: mctp: Don't access ifa_index when missing") pulled 2d45eeb7d5d7 ("mctp: no longer rely on net->dev_index_head[]") as a dependency. However, that change relies on this backport for correct behaviour of for_each_netdev_dump().
Jakub mentions[1] that nothing should be relying on the old behaviour of for_each_netdev_dump(), hence the backport.
[1]: https://lore.kernel.org/netdev/[email protected]/
This backport is only applicable to 6.6.y; the change hit upstream in 6.10.
--- include/linux/netdevice.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/netdevice.h +2 -1
@@ -3036,7 +3036,8 @@ extern rwlock_t dev_base_lock; /* Device list lock */#define net_device_entry (lh) list_entry(lh, struct net_device, dev_list)#define for_each_netdev_dump (net, d, ifindex) \ - xa_for_each_start(&(net)->dev_by_index, (ifindex), (d), (ifindex)) + for (; (d = xa_find(&(net)->dev_by_index, &ifindex, \ + ULONG_MAX, XA_PRESENT)); ifindex++)static inline struct net_device *next_net_device (struct net_device *dev) {
---
base-commit: c2603c511feb427b2b09f74b57816a81272932a1
change-id: 20250610-nl-dump-618700905d4f
Best regards,
Signature · 1 line
Jeremy Kerr <[email protected]>
[ Sasha's backport helper bot ]
Hi,
✅ All tests passed successfully. No issues detected. No action required from the submitter.
The upstream commit SHA1 provided is correct: f22b4b55edb507a2b30981e133b66b642be4d13f
WARNING: Author mismatch between patch and upstream commit:
Backport author: Jeremy Kerr<[email protected]> Commit author: Jakub Kicinski<[email protected]>
Status in newer kernel trees:
6.15.y | Present (exact SHA1) 6.14.y | Present (exact SHA1) 6.12.y | Present (exact SHA1)
Note: The patch differs from the upstream commit:
---
1: f22b4b55edb50 ! 1: 9d419b60cbfdf net: make for_each_netdev_dump() a little more bug-proof
@@ Metadata
## Commit message ##
net: make for_each_netdev_dump() a little more bug-proof
+ commit f22b4b55edb507a2b30981e133b66b642be4d13f upstream.
+
I find the behavior of xa_for_each_start() slightly counter-intuitive. It doesn't end the iteration by making the index point after the last element. IOW calling xa_for_each_start() again after it "finished"
@@ Commit message
+ Signed-off-by: Jeremy Kerr <[email protected]>
## include/linux/netdevice.h ##
-@@ include/linux/netdevice.h: int call_netdevice_notifiers_info(unsigned long val,
+@@ include/linux/netdevice.h: extern rwlock_t dev_base_lock; /* Device list lock */
#define net_device_entry(lh) list_entry(lh, struct net_device, dev_list)
#define for_each_netdev_dump(net, d, ifindex) \
---
Results of testing on various branches:
| Branch | Patch Apply | Build Test | |---------------------------|-------------|------------| | stable/linux-6.6.y | Success | Success |