From: Russell King - ARM Linux <linux@armlinux.org.uk>
To: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Bartosz Golaszewski <brgl@bgdev.pl>, Sekhar Nori <nsekhar@ti.com>,
Kevin Hilman <khilman@kernel.org>,
Grygorii Strashko <grygorii.strashko@ti.com>,
"David S . Miller" <davem@davemloft.net>,
Srinivas Kandagatla <srinivas.kandagatla@linaro.org>,
Lukas Wunner <lukas@wunner.de>, Rob Herring <robh@kernel.org>,
Florian Fainelli <f.fainelli@gmail.com>,
Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>,
David Lechner <david@lechnology.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Andrew Lunn <andrew@lunn.ch>,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-omap@vger.kernel.org,
netdev@vger.kernel.org,
Bartosz Golaszewski <bgolaszewski@baylibre.com>
Subject: Re: [PATCH v2 2/3] net: add support for nvmem to eth_platform_get_mac_address()
Date: Thu, 19 Jul 2018 13:23:46 +0100 [thread overview]
Message-ID: <20180719122346.GJ6920@n2100.armlinux.org.uk> (raw)
In-Reply-To: <20180719084503.tfv6jllsukk2zv3f@mwanda>
On Thu, Jul 19, 2018 at 11:48:37AM +0300, Dan Carpenter wrote:
> On Thu, Jul 19, 2018 at 10:20:27AM +0200, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> >
> > Many non-DT platforms read the MAC address from EEPROM. Usually it's
> > either done with callbacks defined in board files or from SoC-specific
> > ethernet drivers.
> >
> > In order to generalize this, try to read the MAC from nvmem in
> > eth_platform_get_mac_address() using a standard lookup name:
> > "mac-address".
> >
> > Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> > ---
> > net/ethernet/eth.c | 27 +++++++++++++++++++++++++++
> > 1 file changed, 27 insertions(+)
> >
> > diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c
> > index 39af03894598..af3b4b1b77eb 100644
> > --- a/net/ethernet/eth.c
> > +++ b/net/ethernet/eth.c
> > @@ -54,6 +54,7 @@
> > #include <linux/if_ether.h>
> > #include <linux/of_net.h>
> > #include <linux/pci.h>
> > +#include <linux/nvmem-consumer.h>
> > #include <net/dst.h>
> > #include <net/arp.h>
> > #include <net/sock.h>
> > @@ -527,8 +528,11 @@ unsigned char * __weak arch_get_platform_mac_address(void)
> >
> > int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
> > {
> > + unsigned char addrbuf[ETH_ALEN];
> > const unsigned char *addr;
> > + struct nvmem_cell *nvmem;
> > struct device_node *dp;
> > + size_t alen;
> >
> > if (dev_is_pci(dev))
> > dp = pci_device_to_OF_node(to_pci_dev(dev));
> > @@ -541,6 +545,29 @@ int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
> > if (!addr)
> > addr = arch_get_platform_mac_address();
> >
> > + if (!addr) {
> > + nvmem = nvmem_cell_get(dev, "mac-address");
> > + if (IS_ERR(nvmem) && PTR_ERR(nvmem) == -EPROBE_DEFER)
> > + /* We may have a lookup registered for MAC address but
> > + * the corresponding nvmem provider hasn't been
> > + * registered yet.
> > + */
> > + return -EPROBE_DEFER;
> > +
> > + if (!IS_ERR(nvmem)) {
> > + addr = nvmem_cell_read(nvmem, &alen);
> > + if (!IS_ERR(addr)) {
> ^^^^
> Never do success handling. Always error handling. Otherwise the code
> is indent a lot and the error handling is far from the call.
>
> > + if (alen == ETH_ALEN)
> > + ether_addr_copy(addrbuf, addr);
> > +
> > + kfree(addr);
> > + addr = alen == ETH_ALEN ? addrbuf : NULL;
> > + }
> > +
> > + nvmem_cell_put(nvmem);
> > + }
> > + }
> > +
> > if (!addr || !is_valid_ether_addr(addr))
> ^^^^
> Instead of handling the error we dereference the error pointer here.
>
> *frowny face*
>
> > return -ENODEV;
> >
> > --
>
> Maybe this?
>
> if (!addr) {
> nvmem = nvmem_cell_get(dev, "mac-address");
> if (PTR_ERR(nvmem) == -EPROBE_DEFER)
> return -EPROBE_DEFER;
> if (IS_ERR(nvmem))
> return -ENODEV;
> addr = nvmem_cell_read(nvmem, &alen);
> if (IS_ERR(addr))
> return PTR_ERR(addr);
The problem with doing it this way is... error handling is Hard(tm).
You missed the call to nvmem_cell_put() here.
> if (alen != ETH_ALEN) {
> kfree(addr);
and again here.
> return -ENODEV;
> }
> ether_addr_copy(addrbuf, addr);
> kfree(addr);
> addr = addrbuf;
and here.
> }
> if (!is_valid_ether_addr(addr))
> return -ENODEV;
> ether_addr_copy(mac_addr, addr);
> return 0;
Without checking the semantics, a possible solution to that could be:
if (!addr) {
nvmem = nvmem_cell_get(dev, "mac-address");
if (PTR_ERR(nvmem) == -EPROBE_DEFER)
return -EPROBE_DEFER;
if (IS_ERR(nvmem))
return -ENODEV;
addr = nvmem_cell_read(nvmem, &alen);
+ nvmem_cell_put(nvmem);
if (IS_ERR(addr))
return PTR_ERR(addr);
if (alen != ETH_ALEN) {
kfree(addr);
return -ENODEV;
}
ether_addr_copy(addrbuf, addr);
kfree(addr);
addr = addrbuf;
}
if (!is_valid_ether_addr(addr))
return -ENODEV;
ether_addr_copy(mac_addr, addr);
return 0;
A potential better solution would be to put this code in a separate
function, and then do:
if (!addr)
addr = eth_platform_get_nvmem_mac_address(dev, addrbuf);
which returns either NULL or addrbuf depending on whether it failed or
succeeded, which would probably end up with cleaner code.
--
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 13.8Mbps down 630kbps up
According to speedtest.net: 13Mbps down 490kbps up
next prev parent reply other threads:[~2018-07-19 12:24 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-19 8:20 [PATCH v2 0/3] net: extend eth_platform_get_mac_address() Bartosz Golaszewski
2018-07-19 8:20 ` [PATCH v2 1/3] net: fortify eth_platform_get_mac_address() Bartosz Golaszewski
2018-07-19 8:20 ` [PATCH v2 2/3] net: add support for nvmem to eth_platform_get_mac_address() Bartosz Golaszewski
2018-07-19 8:48 ` Dan Carpenter
2018-07-19 8:57 ` Bartosz Golaszewski
2018-07-19 9:09 ` Dan Carpenter
2018-07-19 10:06 ` Bartosz Golaszewski
2018-07-19 12:37 ` Dan Carpenter
2018-07-19 15:35 ` Bartosz Golaszewski
2018-07-19 12:23 ` Russell King - ARM Linux [this message]
2018-07-19 8:20 ` [PATCH v2 3/3] net: add MTD support " 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=20180719122346.GJ6920@n2100.armlinux.org.uk \
--to=linux@armlinux.org.uk \
--cc=andrew@lunn.ch \
--cc=bgolaszewski@baylibre.com \
--cc=brgl@bgdev.pl \
--cc=dan.carpenter@oracle.com \
--cc=davem@davemloft.net \
--cc=david@lechnology.com \
--cc=f.fainelli@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=grygorii.strashko@ti.com \
--cc=ivan.khoronzhuk@linaro.org \
--cc=khilman@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-omap@vger.kernel.org \
--cc=lukas@wunner.de \
--cc=netdev@vger.kernel.org \
--cc=nsekhar@ti.com \
--cc=robh@kernel.org \
--cc=srinivas.kandagatla@linaro.org \
/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
all inboxes | Powered by JetHome®