Skip to contentSkip to conversation

xdp: Handle device unregister for devmap_hash map type

PATCH Superseded Toke Høiland-Jørgensen 2 participants 3 messages Oct 18, 2019 – Oct 19, 2019

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 xdp: Handle device unregister for devmap_hash map type

It seems I forgot to add handling of devmap_hash type maps to the device unregister hook for devmaps. This omission causes devices to not be properly released, which causes hangs.

Fix this by adding the missing handler.

Fixes 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index")

---
v3:
- Use u32 for loop iterator variable
- Since we're holding the lock we can just iterate with hlist_for_each_entry_safe()
v2:
- Grab the update lock while walking the map and removing entries.

 kernel/bpf/devmap.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
kernel/bpf/devmap.c +30 -0
index c0a48f336997..012dbfb0f54b 100644
@@ -719,6 +719,31 @@ const struct bpf_map_ops dev_map_hash_ops = {
 	.map_check_btf = map_check_no_btf,
 };
 
+static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
+				       struct net_device *netdev)
+{
+	unsigned long flags;
+	u32 i;
+
+	spin_lock_irqsave(&dtab->index_lock, flags);
+	for (i = 0; i < dtab->n_buckets; i++) {
+		struct bpf_dtab_netdev *dev;
+		struct hlist_head *head;
+		struct hlist_node *next;
+
+		head = dev_map_index_hash(dtab, i);
+
+		hlist_for_each_entry_safe(dev, next, head, index_hlist) {
+			if (netdev != dev->dev)
+				continue;
+
+			hlist_del_rcu(&dev->index_hlist);
+			call_rcu(&dev->rcu, __dev_map_entry_free);
+		}
+	}
+	spin_unlock_irqrestore(&dtab->index_lock, flags);
+}
+
 static int dev_map_notification(struct notifier_block *notifier,
 				ulong event, void *ptr)
 {
@@ -735,6 +760,11 @@ static int dev_map_notification(struct notifier_block *notifier,
 		 */
 		rcu_read_lock();
 		list_for_each_entry_rcu(dtab, &dev_map_list, list) {
+			if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
+				dev_map_hash_remove_netdev(dtab, netdev);
+				continue;
+			}
+
 			for (i = 0; i < dtab->map.max_entries; i++) {
 				struct bpf_dtab_netdev *dev, *odev;
 
Signature · 1 line
2.23.0
#1 lore raw
Quoted 36 lines · Toke Høiland-Jørgensen

It seems I forgot to add handling of devmap_hash type maps to the device unregister hook for devmaps. This omission causes devices to not be properly released, which causes hangs.

Fix this by adding the missing handler.

Fixes 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index")

---
v3:
- Use u32 for loop iterator variable
- Since we're holding the lock we can just iterate with hlist_for_each_entry_safe()
v2:
- Grab the update lock while walking the map and removing entries.

 kernel/bpf/devmap.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
kernel/bpf/devmap.c +9 -0
index c0a48f336997..012dbfb0f54b 100644
@@ -719,6 +719,31 @@ const struct bpf_map_ops dev_map_hash_ops = {
 	.map_check_btf = map_check_no_btf,
 };
 
+static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
+				       struct net_device *netdev)
+{
+	unsigned long flags;
+	u32 i;
+
+	spin_lock_irqsave(&dtab->index_lock, flags);
+	for (i = 0; i < dtab->n_buckets; i++) {
+		struct bpf_dtab_netdev *dev;

kernel/bpf/devmap.c const struct bpf_map_ops dev_map_hash_ops = { · line 740

Quoted 10 lines
kernel/bpf/devmap.c +10 -0
+		struct hlist_head *head;
+		struct hlist_node *next;
+
+		head = dev_map_index_hash(dtab, i);
+
+		hlist_for_each_entry_safe(dev, next, head, index_hlist) {
+			if (netdev != dev->dev)
+				continue;
+
+			hlist_del_rcu(&dev->index_hlist);

There is another issue...
"dtab->items--;"

Quoted 24 lines
kernel/bpf/devmap.c +11 -0
+			call_rcu(&dev->rcu, __dev_map_entry_free);
+		}
+	}
+	spin_unlock_irqrestore(&dtab->index_lock, flags);
+}
+
 static int dev_map_notification(struct notifier_block *notifier,
 				ulong event, void *ptr)
 {
@@ -735,6 +760,11 @@ static int dev_map_notification(struct notifier_block *notifier,
 		 */
 		rcu_read_lock();
 		list_for_each_entry_rcu(dtab, &dev_map_list, list) {
+			if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
+				dev_map_hash_remove_netdev(dtab, netdev);
+				continue;
+			}
+
 			for (i = 0; i < dtab->map.max_entries; i++) {
 				struct bpf_dtab_netdev *dev, *odev;
 
Signature · 1 line
2.23.0
#2 lore raw
Quoted 41 lines · Martin Lau
Quoted 40 lines · Toke Høiland-Jørgensen

It seems I forgot to add handling of devmap_hash type maps to the device unregister hook for devmaps. This omission causes devices to not be properly released, which causes hangs.

Fix this by adding the missing handler.

Fixes 6f9d451ab1a3 ("xdp: Add devmap_hash map type for looking up devices by hashed index")

---
v3:
- Use u32 for loop iterator variable
- Since we're holding the lock we can just iterate with hlist_for_each_entry_safe()
v2:
- Grab the update lock while walking the map and removing entries.

 kernel/bpf/devmap.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
kernel/bpf/devmap.c +13 -0
index c0a48f336997..012dbfb0f54b 100644
@@ -719,6 +719,31 @@ const struct bpf_map_ops dev_map_hash_ops = {
 	.map_check_btf = map_check_no_btf,
 };
 
+static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
+				       struct net_device *netdev)
+{
+	unsigned long flags;
+	u32 i;
+
+	spin_lock_irqsave(&dtab->index_lock, flags);
+	for (i = 0; i < dtab->n_buckets; i++) {
+		struct bpf_dtab_netdev *dev;
+		struct hlist_head *head;
+		struct hlist_node *next;
+
+		head = dev_map_index_hash(dtab, i);
Quoted 8 lines

kernel/bpf/devmap.c const struct bpf_map_ops dev_map_hash_ops = { · line 740

Quoted 6 lines
kernel/bpf/devmap.c +6 -0
+
+		hlist_for_each_entry_safe(dev, next, head, index_hlist) {
+			if (netdev != dev->dev)
+				continue;
+
+			hlist_del_rcu(&dev->index_hlist);

There is another issue...
"dtab->items--;"

Right, of course. Thanks for your diligence in spotting my silly mistakes! Will send a v4 :)

-Toke