device: use safe_list for device event, fixes some hang issues
authorFelix Fietkau <nbd@openwrt.org>
Mon, 15 Apr 2013 14:20:50 +0000 (16:20 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 15 Apr 2013 14:20:50 +0000 (16:20 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
device.c
device.h
vlan.c

index 9ef444e88a233f615c320fe3f3ec65d7f10d2f0b..1c0736f67ce05260d5c1d3800f6bd52dec4140fd 100644 (file)
--- a/device.c
+++ b/device.c
@@ -180,36 +180,22 @@ static void __init dev_init(void)
        avl_init(&devices, avl_strcmp, true, NULL);
 }
 
-
-static void __device_broadcast_event(struct list_head *head, enum device_event ev)
+static int device_broadcast_cb(void *ctx, struct safe_list *list)
 {
-       struct device_user *dep;
-       static uint8_t idx[__DEV_EVENT_MAX];
-       bool found;
-
-       idx[ev]++;
-       do {
-               found = false;
-
-               list_for_each_entry(dep, head, list) {
-                       if (!dep->cb)
-                               continue;
+       struct device_user *dep = container_of(list, struct device_user, list);
+       int *ev = ctx;
 
-                       if (dep->ev_idx[ev] == idx[ev])
-                               continue;
-
-                       dep->cb(dep, ev);
-                       dep->ev_idx[ev] = idx[ev];
-                       found = true;
-                       break;
-               }
-       } while (found);
+       if (dep->cb)
+               dep->cb(dep, *ev);
+       return 0;
 }
 
 void device_broadcast_event(struct device *dev, enum device_event ev)
 {
-       __device_broadcast_event(&dev->aliases, ev);
-       __device_broadcast_event(&dev->users, ev);
+       int dev_ev = ev;
+
+       safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
+       safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
 }
 
 int device_claim(struct device_user *dep)
@@ -276,8 +262,8 @@ void device_init_virtual(struct device *dev, const struct device_type *type, con
                strncpy(dev->ifname, name, IFNAMSIZ);
 
        D(DEVICE, "Initialize device '%s'\n", dev->ifname);
-       INIT_LIST_HEAD(&dev->users);
-       INIT_LIST_HEAD(&dev->aliases);
+       INIT_SAFE_LIST(&dev->users);
+       INIT_SAFE_LIST(&dev->aliases);
        dev->type = type;
 
        if (!dev->set_state)
@@ -356,19 +342,21 @@ device_delete(struct device *dev)
        dev->avl.key = NULL;
 }
 
-void device_cleanup(struct device *dev)
+static int device_cleanup_cb(void *ctx, struct safe_list *list)
 {
-       struct device_user *dep, *tmp;
-
-       D(DEVICE, "Clean up device '%s'\n", dev->ifname);
-       list_for_each_entry_safe(dep, tmp, &dev->users, list) {
-               if (!dep->cb)
-                       continue;
-
+       struct device_user *dep = container_of(list, struct device_user, list);
+       if (dep->cb)
                dep->cb(dep, DEV_EVENT_REMOVE);
-               device_release(dep);
-       }
 
+       device_release(dep);
+       return 0;
+}
+
+void device_cleanup(struct device *dev)
+{
+       D(DEVICE, "Clean up device '%s'\n", dev->ifname);
+       safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
+       safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
        device_delete(dev);
 }
 
@@ -407,7 +395,10 @@ static int device_refcount(struct device *dev)
        struct list_head *list;
        int count = 0;
 
-       list_for_each(list, &dev->users)
+       list_for_each(list, &dev->users.list)
+               count++;
+
+       list_for_each(list, &dev->aliases.list)
                count++;
 
        return count;
@@ -415,7 +406,7 @@ static int device_refcount(struct device *dev)
 
 void device_add_user(struct device_user *dep, struct device *dev)
 {
-       struct list_head *head;
+       struct safe_list *head;
 
        if (dep->dev)
                device_remove_user(dep);
@@ -429,7 +420,8 @@ void device_add_user(struct device_user *dep, struct device *dev)
                head = &dev->aliases;
        else
                head = &dev->users;
-       list_add_tail(&dep->list, head);
+
+       safe_list_add(&dep->list, head);
        D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
 
        if (dep->cb && dev->present) {
@@ -452,7 +444,9 @@ device_free(struct device *dev)
 static void
 __device_free_unused(struct device *dev)
 {
-       if (!list_empty(&dev->users) || dev->current_config || __devlock)
+       if (!safe_list_empty(&dev->users) ||
+               !safe_list_empty(&dev->aliases) ||
+           dev->current_config || __devlock)
                return;
 
        device_free(dev);
@@ -469,7 +463,7 @@ void device_remove_user(struct device_user *dep)
        if (dep->claimed)
                device_release(dep);
 
-       list_del(&dep->list);
+       safe_list_del(&dep->list);
        dep->dev = NULL;
        D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
        __device_free_unused(dev);
@@ -545,9 +539,10 @@ device_replace(struct device *dev, struct device *odev)
        if (present)
                device_set_present(odev, false);
 
-       list_for_each_entry_safe(dep, tmp, &odev->userslist) {
+       list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
                device_release(dep);
-               list_move_tail(&dep->list, &dev->users);
+               safe_list_del(&dep->list);
+               safe_list_add(&dep->list, &dev->users);
                dep->dev = dev;
        }
        device_free(odev);
index 1f852abe6c46a70ac6262b04e12bef2aa707ac8f..1068a46c9e4a8dd151930c049e29f4c0983a4ccf 100644 (file)
--- a/device.h
+++ b/device.h
@@ -15,6 +15,7 @@
 #define __LL_H
 
 #include <libubox/avl.h>
+#include <libubox/safe_list.h>
 #include <netinet/in.h>
 
 struct device;
@@ -83,7 +84,7 @@ enum device_event {
  * device dependency with callbacks
  */
 struct device_user {
-       struct list_head list;
+       struct safe_list list;
 
        bool claimed;
        bool hotplug;
@@ -110,8 +111,8 @@ struct device {
        const struct device_type *type;
 
        struct avl_node avl;
-       struct list_head users;
-       struct list_head aliases;
+       struct safe_list users;
+       struct safe_list aliases;
 
        char ifname[IFNAMSIZ + 1];
        int ifindex;
diff --git a/vlan.c b/vlan.c
index ee0652fb476ffc6542280f1c5eefbe26ea187b66..45896751d39241d240b2596d85dbeff8b1373303 100644 (file)
--- a/vlan.c
+++ b/vlan.c
@@ -99,7 +99,7 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create)
        struct device_user *dep;
 
        /* look for an existing interface before creating a new one */
-       list_for_each_entry(dep, &dev->userslist) {
+       list_for_each_entry(dep, &dev->users.list, list.list) {
                if (dep->cb != vlan_dev_cb)
                        continue;