Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp485739imn; Wed, 27 Jul 2022 11:31:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sbveAE0v3Scd7qjNR63iIcKdn1OxiVwL6dgvRYeW7fQaUg3dN0oCofxDjgffvaXY1lnYg7 X-Received: by 2002:a63:5653:0:b0:419:ffb6:3ec6 with SMTP id g19-20020a635653000000b00419ffb63ec6mr19989868pgm.149.1658946700917; Wed, 27 Jul 2022 11:31:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658946700; cv=none; d=google.com; s=arc-20160816; b=bpacamQ981CtQMMNrk+gD7wfrneinr2KVTwCJao8FhlIE5Z5KmAD9MhHew41TeEr3y 0l5cSXMhYDyf0kKoabcZ1KXWoTBkNMmkmrGz80pmbQf6B2e0MNhfzMm0CQw4WDTMYx7v AUXpDf3elt51vAYYyrh59jU1iXaY+B30sKLkGER4+ioN6iXCnBsitOBX/XorgFcHeO5P 3y1Dny1sga4l6PxxSML3vS4HK9FgqGpb+nQo6KwEevHlJN+7x4oLjGoBcbz+kEgGh8uU Ah1qqihMOF5+s5okk6XGXatnofBZMbzBfoDwzGfcG67RsMZ2tvG+B0/5JX1bbOlEqQds eCeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=B9pmVgFgf48uM32czqix218aG1IC8I8hGzvkUPoWmXw=; b=gjl9Fdw39rilmvQz6peqHCtZi9yYhYVly20xrGPaipegfBKcT6GBIZzja/aatMrgj3 YBu8RGSlWwo8DF7cGfREErP9n+S8hSniugpz1AMbdrkvL4McLYANaY6q7Fvur1j4navx 2jRyH+O6nKcYTLXYWU4zPIrdyKVEv3XNLBlc8nS8gn+Azd7K/Eqjh0LxDMERZU8WoLr9 QS+PsWU8WXdDqsg9ZHGezs2rrCP8q7JQdPwj3VF1GMA07wMG8zbWmJFN2Dni/We0e/Gm vSDT3pPHdER9+22FswUz3bB5WyX6tgqd2BpfICP/H2wXfXO8Antj+24+c+aH5UZLBVSA bLFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lRtD+JLo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j3-20020a170903028300b0016a65a43611si21684690plr.149.2022.07.27.11.31.25; Wed, 27 Jul 2022 11:31:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lRtD+JLo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240598AbiG0QuE (ORCPT + 99 others); Wed, 27 Jul 2022 12:50:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240689AbiG0QtO (ORCPT ); Wed, 27 Jul 2022 12:49:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDFC661729; Wed, 27 Jul 2022 09:32:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 13157B8217D; Wed, 27 Jul 2022 16:32:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72286C433D7; Wed, 27 Jul 2022 16:32:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658939561; bh=ylf/3orbTSfk1Z147r4ddLb5MGB2Vm2oLSsvWTP+Gzc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lRtD+JLoUt5Cxlt+ESM/3L0L2ECcJZsBeZSTAIXq+Oz8uhgASx/hbtycnzfAbcNkC kPVTuA+VfeGWoCRQsJKsmV+mkFxI70FExpFho3h5ayLxABPnF/ljBmqDZ+4l2QMQ/a +KZ8g7SEcbImNXTAh5LtEcd77DbbEpYMiB6c0uwY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jakub Kicinski , Fedor Pchelkin Subject: [PATCH 5.10 009/105] docs: net: explain struct net_device lifetime Date: Wed, 27 Jul 2022 18:09:55 +0200 Message-Id: <20220727161012.444477836@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220727161012.056867467@linuxfoundation.org> References: <20220727161012.056867467@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Fedor Pchelkin From: Jakub Kicinski commit 2b446e650b418f9a9e75f99852e2f2560cabfa17 upstream. Explain the two basic flows of struct net_device's operation. Signed-off-by: Jakub Kicinski Signed-off-by: Fedor Pchelkin Signed-off-by: Greg Kroah-Hartman --- Documentation/networking/netdevices.rst | 171 ++++++++++++++++++++++++++++++-- net/core/rtnetlink.c | 2 2 files changed, 166 insertions(+), 7 deletions(-) --- a/Documentation/networking/netdevices.rst +++ b/Documentation/networking/netdevices.rst @@ -10,18 +10,177 @@ Introduction The following is a random collection of documentation regarding network devices. -struct net_device allocation rules -================================== +struct net_device lifetime rules +================================ Network device structures need to persist even after module is unloaded and must be allocated with alloc_netdev_mqs() and friends. If device has registered successfully, it will be freed on last use -by free_netdev(). This is required to handle the pathologic case cleanly -(example: rmmod mydriver needs_free_netdev = true; + } + + static void my_destructor(struct net_device *dev) + { + some_obj_destroy(priv->obj); + some_uninit(priv); + } + + int create_link() + { + struct my_device_priv *priv; + int err; + + ASSERT_RTNL(); + + dev = alloc_netdev(sizeof(*priv), "net%d", NET_NAME_UNKNOWN, my_setup); + if (!dev) + return -ENOMEM; + priv = netdev_priv(dev); + + /* Implicit constructor */ + err = some_init(priv); + if (err) + goto err_free_dev; + + priv->obj = some_obj_create(); + if (!priv->obj) { + err = -ENOMEM; + goto err_some_uninit; + } + /* End of constructor, set the destructor: */ + dev->priv_destructor = my_destructor; + + err = register_netdevice(dev); + if (err) + /* register_netdevice() calls destructor on failure */ + goto err_free_dev; + + /* If anything fails now unregister_netdevice() (or unregister_netdev()) + * will take care of calling my_destructor and free_netdev(). + */ + + return 0; + + err_some_uninit: + some_uninit(priv); + err_free_dev: + free_netdev(dev); + return err; + } + +If struct net_device.priv_destructor is set it will be called by the core +some time after unregister_netdevice(), it will also be called if +register_netdevice() fails. The callback may be invoked with or without +``rtnl_lock`` held. + +There is no explicit constructor callback, driver "constructs" the private +netdev state after allocating it and before registration. + +Setting struct net_device.needs_free_netdev makes core call free_netdevice() +automatically after unregister_netdevice() when all references to the device +are gone. It only takes effect after a successful call to register_netdevice() +so if register_netdevice() fails driver is responsible for calling +free_netdev(). + +free_netdev() is safe to call on error paths right after unregister_netdevice() +or when register_netdevice() fails. Parts of netdev (de)registration process +happen after ``rtnl_lock`` is released, therefore in those cases free_netdev() +will defer some of the processing until ``rtnl_lock`` is released. + +Devices spawned from struct rtnl_link_ops should never free the +struct net_device directly. + +.ndo_init and .ndo_uninit +~~~~~~~~~~~~~~~~~~~~~~~~~ + +``.ndo_init`` and ``.ndo_uninit`` callbacks are called during net_device +registration and de-registration, under ``rtnl_lock``. Drivers can use +those e.g. when parts of their init process need to run under ``rtnl_lock``. + +``.ndo_init`` runs before device is visible in the system, ``.ndo_uninit`` +runs during de-registering after device is closed but other subsystems +may still have outstanding references to the netdevice. MTU === --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -3444,7 +3444,7 @@ replay: if (ops->newlink) { err = ops->newlink(link_net ? : net, dev, tb, data, extack); - /* Drivers should call free_netdev() in ->destructor + /* Drivers should set dev->needs_free_netdev * and unregister it on failure after registration * so that device could be finally freed in rtnl_unlock. */