[NET]: Do sysfs registration as part of register_netdevice.

The last step of netdevice registration was being done by a delayed
call, but because it was delayed, it was impossible to return any error
code if the class_device registration failed.

Side effects:
* one state in registration process is unnecessary.
* register_netdevice can sleep inside class_device registration/hotplug
* code in netdev_run_todo only does unregistration so it is simpler.

Signed-off-by: Stephen Hemminger <shemminger@osdl.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by Stephen Hemminger and committed by David S. Miller b17a7c17 a50bb7b9

+29 -39
+1 -2
include/linux/netdevice.h
··· 433 433 434 434 /* register/unregister state machine */ 435 435 enum { NETREG_UNINITIALIZED=0, 436 - NETREG_REGISTERING, /* called register_netdevice */ 437 - NETREG_REGISTERED, /* completed register todo */ 436 + NETREG_REGISTERED, /* completed register_netdevice */ 438 437 NETREG_UNREGISTERING, /* called unregister_netdevice */ 439 438 NETREG_UNREGISTERED, /* completed unregister todo */ 440 439 NETREG_RELEASED, /* called free_netdev */
+28 -37
net/core/dev.c
··· 2777 2777 BUG_ON(dev_boot_phase); 2778 2778 ASSERT_RTNL(); 2779 2779 2780 + might_sleep(); 2781 + 2780 2782 /* When net_device's are persistent, this will be fatal. */ 2781 2783 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); 2782 2784 ··· 2865 2863 if (!dev->rebuild_header) 2866 2864 dev->rebuild_header = default_rebuild_header; 2867 2865 2866 + ret = netdev_register_sysfs(dev); 2867 + if (ret) 2868 + goto out_err; 2869 + dev->reg_state = NETREG_REGISTERED; 2870 + 2868 2871 /* 2869 2872 * Default initial state at registry is that the 2870 2873 * device is present. ··· 2885 2878 hlist_add_head(&dev->name_hlist, head); 2886 2879 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex)); 2887 2880 dev_hold(dev); 2888 - dev->reg_state = NETREG_REGISTERING; 2889 2881 write_unlock_bh(&dev_base_lock); 2890 2882 2891 2883 /* Notify protocols, that a new device appeared. */ 2892 2884 raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev); 2893 2885 2894 - /* Finish registration after unlock */ 2895 - net_set_todo(dev); 2896 2886 ret = 0; 2897 2887 2898 2888 out: ··· 3012 3008 * 3013 3009 * We are invoked by rtnl_unlock() after it drops the semaphore. 3014 3010 * This allows us to deal with problems: 3015 - * 1) We can create/delete sysfs objects which invoke hotplug 3011 + * 1) We can delete sysfs objects which invoke hotplug 3016 3012 * without deadlocking with linkwatch via keventd. 3017 3013 * 2) Since we run with the RTNL semaphore not held, we can sleep 3018 3014 * safely in order to wait for the netdev refcnt to drop to zero. ··· 3021 3017 void netdev_run_todo(void) 3022 3018 { 3023 3019 struct list_head list = LIST_HEAD_INIT(list); 3024 - int err; 3025 - 3026 3020 3027 3021 /* Need to guard against multiple cpu's getting out of order. */ 3028 3022 mutex_lock(&net_todo_run_mutex); ··· 3043 3041 = list_entry(list.next, struct net_device, todo_list); 3044 3042 list_del(&dev->todo_list); 3045 3043 3046 - switch(dev->reg_state) { 3047 - case NETREG_REGISTERING: 3048 - err = netdev_register_sysfs(dev); 3049 - if (err) 3050 - printk(KERN_ERR "%s: failed sysfs registration (%d)\n", 3051 - dev->name, err); 3052 - dev->reg_state = NETREG_REGISTERED; 3053 - break; 3054 - 3055 - case NETREG_UNREGISTERING: 3056 - netdev_unregister_sysfs(dev); 3057 - dev->reg_state = NETREG_UNREGISTERED; 3058 - 3059 - netdev_wait_allrefs(dev); 3060 - 3061 - /* paranoia */ 3062 - BUG_ON(atomic_read(&dev->refcnt)); 3063 - BUG_TRAP(!dev->ip_ptr); 3064 - BUG_TRAP(!dev->ip6_ptr); 3065 - BUG_TRAP(!dev->dn_ptr); 3066 - 3067 - 3068 - /* It must be the very last action, 3069 - * after this 'dev' may point to freed up memory. 3070 - */ 3071 - if (dev->destructor) 3072 - dev->destructor(dev); 3073 - break; 3074 - 3075 - default: 3044 + if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { 3076 3045 printk(KERN_ERR "network todo '%s' but state %d\n", 3077 3046 dev->name, dev->reg_state); 3078 - break; 3047 + dump_stack(); 3048 + continue; 3079 3049 } 3050 + 3051 + netdev_unregister_sysfs(dev); 3052 + dev->reg_state = NETREG_UNREGISTERED; 3053 + 3054 + netdev_wait_allrefs(dev); 3055 + 3056 + /* paranoia */ 3057 + BUG_ON(atomic_read(&dev->refcnt)); 3058 + BUG_TRAP(!dev->ip_ptr); 3059 + BUG_TRAP(!dev->ip6_ptr); 3060 + BUG_TRAP(!dev->dn_ptr); 3061 + 3062 + /* It must be the very last action, 3063 + * after this 'dev' may point to freed up memory. 3064 + */ 3065 + if (dev->destructor) 3066 + dev->destructor(dev); 3080 3067 } 3081 3068 3082 3069 out: