linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Edwin Peer <edwin.peer@broadcom.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: devicetree@vger.kernel.org, Felix Fietkau <nbd@openwrt.org>,
	Arnd Bergmann <arnd@arndb.de>,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>,
	netdev@vger.kernel.org, Sean Wang <sean.wang@mediatek.com>,
	linux-kernel@vger.kernel.org, Mark Lee <Mark-MC.Lee@mediatek.com>,
	Fabien Parent <fparent@baylibre.com>,
	Rob Herring <robh+dt@kernel.org>,
	linux-mediatek@lists.infradead.org,
	John Crispin <john@phrozen.org>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	Jakub Kicinski <kuba@kernel.org>,
	"David S . Miller" <davem@davemloft.net>,
	linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH 05/11] net: core: provide devm_register_netdev()
Date: Tue, 5 May 2020 12:25:03 -0700	[thread overview]
Message-ID: <CAKOOJTzcNr7mc9xusQm3nCzkq5P=ha-si3fizeEL2_KJUOC3-Q@mail.gmail.com> (raw)
In-Reply-To: <20200505140231.16600-6-brgl@bgdev.pl>

On Tue, May 5, 2020 at 7:05 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>
> Provide devm_register_netdev() - a device resource managed variant
> of register_netdev(). This new helper will only work for net_device
> structs that have a parent device assigned and are devres managed too.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> ---
>  include/linux/netdevice.h |  4 ++++
>  net/core/dev.c            | 48 +++++++++++++++++++++++++++++++++++++++
>  net/ethernet/eth.c        |  1 +
>  3 files changed, 53 insertions(+)
>
> diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
> index 130a668049ab..433bd5ca2efc 100644
> --- a/include/linux/netdevice.h
> +++ b/include/linux/netdevice.h
> @@ -1515,6 +1515,8 @@ struct net_device_ops {
>   * @IFF_FAILOVER_SLAVE: device is lower dev of a failover master device
>   * @IFF_L3MDEV_RX_HANDLER: only invoke the rx handler of L3 master device
>   * @IFF_LIVE_RENAME_OK: rename is allowed while device is up and running
> + * @IFF_IS_DEVRES: this structure was allocated dynamically and is managed by
> + *     devres
>   */
>  enum netdev_priv_flags {
>         IFF_802_1Q_VLAN                 = 1<<0,
> @@ -1548,6 +1550,7 @@ enum netdev_priv_flags {
>         IFF_FAILOVER_SLAVE              = 1<<28,
>         IFF_L3MDEV_RX_HANDLER           = 1<<29,
>         IFF_LIVE_RENAME_OK              = 1<<30,
> +       IFF_IS_DEVRES                   = 1<<31,
>  };
>
>  #define IFF_802_1Q_VLAN                        IFF_802_1Q_VLAN
> @@ -4206,6 +4209,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
>                          count)
>
>  int register_netdev(struct net_device *dev);
> +int devm_register_netdev(struct net_device *ndev);
>  void unregister_netdev(struct net_device *dev);
>
>  /* General hardware address lists handling functions */
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 522288177bbd..99db537c9468 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -9519,6 +9519,54 @@ int register_netdev(struct net_device *dev)
>  }
>  EXPORT_SYMBOL(register_netdev);
>
> +struct netdevice_devres {
> +       struct net_device *ndev;
> +};
> +
> +static void devm_netdev_release(struct device *dev, void *this)
> +{
> +       struct netdevice_devres *res = this;
> +
> +       unregister_netdev(res->ndev);
> +}
> +
> +/**
> + *     devm_register_netdev - resource managed variant of register_netdev()
> + *     @ndev: device to register
> + *
> + *     This is a devres variant of register_netdev() for which the unregister
> + *     function will be call automatically when the parent device of ndev
> + *     is detached.
> + */
> +int devm_register_netdev(struct net_device *ndev)
> +{
> +       struct netdevice_devres *dr;
> +       int ret;
> +
> +       /* struct net_device itself must be devres managed. */
> +       BUG_ON(!(ndev->priv_flags & IFF_IS_DEVRES));
> +       /* struct net_device must have a parent device - it will be the device
> +        * managing this resource.
> +        */

Catching static programming errors seems like an expensive use of the
last runtime flag in the enum. It would be weird to devres manage the
unregister and not also choose to manage the underlying memory in the
same fashion, so it wouldn't be an obvious mistake to make. If it must
be enforced, one could also iterate over the registered release
functions and check for the presence of devm_free_netdev without
burning the flag.

> +       BUG_ON(!ndev->dev.parent);
> +
> +       dr = devres_alloc(devm_netdev_release, sizeof(*dr), GFP_KERNEL);
> +       if (!dr)
> +               return -ENOMEM;
> +
> +       ret = register_netdev(ndev);
> +       if (ret) {
> +               devres_free(dr);
> +               return ret;
> +       }
> +
> +       dr->ndev = ndev;
> +       devres_add(ndev->dev.parent, dr);
> +
> +       return 0;
> +}
> +EXPORT_SYMBOL(devm_register_netdev);
> +
>  int netdev_refcnt_read(const struct net_device *dev)
>  {
>         int i, refcnt = 0;
> diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c
> index c8b903302ff2..ce9b5e576f20 100644
> --- a/net/ethernet/eth.c
> +++ b/net/ethernet/eth.c
> @@ -423,6 +423,7 @@ struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv,
>
>         *dr = netdev;
>         devres_add(dev, dr);
> +       netdev->priv_flags |= IFF_IS_DEVRES;
>
>         return netdev;
>  }
> --
> 2.25.0
>

Regards,
Edwin Peer

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2020-05-05 19:25 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-05 14:02 [PATCH 00/11] mediatek: add support for MediaTek Ethernet MAC Bartosz Golaszewski
2020-05-05 14:02 ` [PATCH 01/11] dt-bindings: add a binding document for MediaTek PERICFG controller Bartosz Golaszewski
2020-05-13  2:38   ` Rob Herring
2020-05-13  8:09     ` Bartosz Golaszewski
2020-05-05 14:02 ` [PATCH 02/11] dt-bindings: new: add yaml bindings for MediaTek Ethernet MAC Bartosz Golaszewski
2020-05-13  2:41   ` Rob Herring
2020-05-05 14:02 ` [PATCH 03/11] net: ethernet: mediatek: rename Kconfig prompt Bartosz Golaszewski
2020-05-05 14:02 ` [PATCH 04/11] net: ethernet: mediatek: remove unnecessary spaces from Makefile Bartosz Golaszewski
2020-05-05 14:02 ` [PATCH 05/11] net: core: provide devm_register_netdev() Bartosz Golaszewski
2020-05-05 17:31   ` Jakub Kicinski
2020-05-06  6:39     ` Bartosz Golaszewski
2020-05-06 17:12       ` Jakub Kicinski
2020-05-07  9:25         ` Bartosz Golaszewski
2020-05-07 16:53           ` Jakub Kicinski
2020-05-07 17:03             ` Bartosz Golaszewski
2020-05-07 22:56               ` Jakub Kicinski
2020-05-08  5:54                 ` Heiner Kallweit
2020-05-08 18:39                   ` Bartosz Golaszewski
2020-05-05 19:25   ` Edwin Peer [this message]
2020-05-06  6:46     ` Bartosz Golaszewski
2020-05-06 18:20       ` Edwin Peer
2020-05-05 14:02 ` [PATCH 06/11] net: ethernet: mtk-eth-mac: new driver Bartosz Golaszewski
2020-05-05 17:47   ` Andrew Lunn
2020-05-06  7:02     ` Bartosz Golaszewski
2020-05-05 18:04   ` Jakub Kicinski
2020-05-06  7:09     ` Bartosz Golaszewski
2020-05-06 17:19       ` Jakub Kicinski
2020-05-06 19:16   ` Leon Romanovsky
2020-05-06 19:23     ` Jakub Kicinski
2020-05-07  5:55       ` Leon Romanovsky
2020-05-07 22:50         ` Jakub Kicinski
2020-05-06 19:24     ` Joe Perches
2020-05-07  9:46   ` Mark-MC.Lee
2020-05-07 10:50     ` Bartosz Golaszewski
2020-05-07 13:16       ` Andrew Lunn
2020-05-07 17:01         ` Bartosz Golaszewski
2020-05-05 14:02 ` [PATCH 07/11] ARM64: dts: mediatek: add pericfg syscon to mt8516.dtsi Bartosz Golaszewski
2020-05-05 14:02 ` [PATCH 08/11] ARM64: dts: mediatek: add the ethernet node " Bartosz Golaszewski
2020-05-05 14:02 ` [PATCH 09/11] ARM64: dts: mediatek: add an alias for ethernet0 for pumpkin boards Bartosz Golaszewski
2020-05-05 14:02 ` [PATCH 10/11] ARM64: dts: mediatek: add ethernet pins " Bartosz Golaszewski
2020-05-05 14:02 ` [PATCH 11/11] ARM64: dts: mediatek: enable ethernet on " Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKOOJTzcNr7mc9xusQm3nCzkq5P=ha-si3fizeEL2_KJUOC3-Q@mail.gmail.com' \
    --to=edwin.peer@broadcom.com \
    --cc=Mark-MC.Lee@mediatek.com \
    --cc=arnd@arndb.de \
    --cc=bgolaszewski@baylibre.com \
    --cc=brgl@bgdev.pl \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=fparent@baylibre.com \
    --cc=john@phrozen.org \
    --cc=kuba@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=nbd@openwrt.org \
    --cc=netdev@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=sean.wang@mediatek.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).