Re: [PATCH net v4 00/12] Fix possbile memleaks when fail to register_netdevice
From: David Miller <davem@davemloft.net>
Date: 2017-05-02 19:30:32
From: gfree.wind@foxmail.com Date: Tue, 2 May 2017 13:58:42 +0800
These following drivers allocate kinds of resources in its ndo_init func, free some of them or all in the destructor func. Then there is one memleak that some errors happen after register_netdevice invokes the ndo_init callback. Because only the ndo_uninit callback is invoked in the error handler of register_netdevice, but destructor not. In my original approach, I tried to free the resources in the newlink func when fail to register_netdevice, like destructor did except not free the net_dev. This method is not good when destructor is changed, and the memleak could be not fixed when there is no newlink callback. Now create one new func used to free the resources in the destructor, and the ndo_uninit func also could invokes it when fail to register the net_device by comparing the dev->reg_state with NETREG_UNINITIALIZED. If there is no existing ndo_uninit, just add one. This solution doesn't only make sure free all resources in any case, but also follows the original desgin that some resources could be kept until the destructor executes normally after register the device successfully.
I want to think about this some more. It is really unfortunate that resources are allocated strictly from the ndo_init() yet released in two different callbacks which are invoked only in certain (different) situations. Just the fact that we have to make an internal netdev state test in the ndo_uninit callback to get this right is a big red flag to me.