diff mbox

[ovs-dev,3/8] datapath: Fix inconsistent teardown and release of private netdev state.

Message ID 1500497833-30066-4-git-send-email-gvrose8192@gmail.com
State Superseded
Delegated to: Joe Stringer
Headers show

Commit Message

Gregory Rose July 19, 2017, 8:57 p.m. UTC
Upstream commit:
    commit cf124db566e6b036b8bcbe8decbed740bdfac8c6
    Author: David S. Miller <davem@davemloft.net>
    Date:   Mon May 8 12:52:56 2017 -0400

    net: Fix inconsistent teardown and release of private netdev state.

    Network devices can allocate reasources and private memory using
    netdev_ops->ndo_init().  However, the release of these resources
    can occur in one of two different places.

    Either netdev_ops->ndo_uninit() or netdev->destructor().

    The decision of which operation frees the resources depends upon
    whether it is necessary for all netdev refs to be released before it
    is safe to perform the freeing.

    netdev_ops->ndo_uninit() presumably can occur right after the
    NETDEV_UNREGISTER notifier completes and the unicast and multicast
    address lists are flushed.

    netdev->destructor(), on the other hand, does not run until the
    netdev references all go away.

    Further complicating the situation is that netdev->destructor()
    almost universally does also a free_netdev().

    This creates a problem for the logic in register_netdevice().
    Because all callers of register_netdevice() manage the freeing
    of the netdev, and invoke free_netdev(dev) if register_netdevice()
    fails.

    If netdev_ops->ndo_init() succeeds, but something else fails inside
    of register_netdevice(), it does call ndo_ops->ndo_uninit().  But
    it is not able to invoke netdev->destructor().

    This is because netdev->destructor() will do a free_netdev() and
    then the caller of register_netdevice() will do the same.

    However, this means that the resources that would normally be released
    by netdev->destructor() will not be.

    Over the years drivers have added local hacks to deal with this, by
    invoking their destructor parts by hand when register_netdevice()
    fails.

    Many drivers do not try to deal with this, and instead we have leaks.

    Let's close this hole by formalizing the distinction between what
    private things need to be freed up by netdev->destructor() and whether
    the driver needs unregister_netdevice() to perform the free_netdev().

    netdev->priv_destructor() performs all actions to free up the private
    resources that used to be freed by netdev->destructor(), except for
    free_netdev().

    netdev->needs_free_netdev is a boolean that indicates whether
    free_netdev() should be done at the end of unregister_netdevice().

    Now, register_netdevice() can sanely release all resources after
    ndo_ops->ndo_init() succeeds, by invoking both ndo_ops->ndo_uninit()
    and netdev->priv_destructor().

    And at the end of unregister_netdevice(), we invoke
    netdev->priv_destructor() and optionally call free_netdev().

    Signed-off-by: David S. Miller <davem@davemloft.net>

Applied the portion of the commit applicable to openvswitch.

Signed-off-by: Greg Rose <gvrose8192@gmail.com>
---
 datapath/vport-internal_dev.c | 7 +++++++
 1 file changed, 7 insertions(+)
diff mbox

Patch

diff --git a/datapath/vport-internal_dev.c b/datapath/vport-internal_dev.c
index 2fa9ab9..0aa331a 100644
--- a/datapath/vport-internal_dev.c
+++ b/datapath/vport-internal_dev.c
@@ -114,7 +114,9 @@  static void internal_dev_destructor(struct net_device *dev)
 	struct vport *vport = ovs_internal_dev_get_vport(dev);
 
 	ovs_vport_free(vport);
+#ifndef HAVE_NEEDS_FREE_NETDEV
 	free_netdev(dev);
+#endif
 }
 
 static void
@@ -188,7 +190,12 @@  static void do_setup(struct net_device *netdev)
 	netdev->priv_flags &= ~IFF_TX_SKB_SHARING;
 	netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_OPENVSWITCH |
 			      IFF_PHONY_HEADROOM | IFF_NO_QUEUE;
+#ifndef HAVE_NEEDS_FREE_NETDEV
 	netdev->destructor = internal_dev_destructor;
+#else
+	netdev->needs_free_netdev = true;
+	netdev->priv_destructor = internal_dev_destructor;
+#endif /* HAVE_NEEDS_FREE_NETDEV */
 	netdev->ethtool_ops = &internal_dev_ethtool_ops;
 	netdev->rtnl_link_ops = &internal_dev_link_ops;