[net-next,v6,7/9] net: dsa: microchip: Add error handling for ksz_switch_macaddr_get()
Message ID | 20231019122850.1199821-8-o.rempel@pengutronix.de |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp346765vqb; Thu, 19 Oct 2023 05:30:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7klncywX5I8zkWLpwP9+vDABhy7uEcLfSjOpeNH3eNVOiUp7FS8o4m70pAcMHY5QB4/hv X-Received: by 2002:a17:902:dac8:b0:1ca:4d35:b2ec with SMTP id q8-20020a170902dac800b001ca4d35b2ecmr2784773plx.15.1697718611603; Thu, 19 Oct 2023 05:30:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697718611; cv=none; d=google.com; s=arc-20160816; b=hdaKjxC6bXc8kylsXP+Rd+/B9reDwy/dFHKipcoH1RoRPhdLHb6uaPZNd8TdmxbBEg IGle7YHGT83Bzo7BNM8MQ+pyvIHbyp28IBLWlVZUzXKOq3zKcNY54jznTy8IKq+4CUqU MJm5unLTnGlHK0ysPEdlC8zZh8w1BXldM9us47HOuVNwtlTh1dZwvKvmu3W+qTXLabgM u6Qj1wP3dCYljRp9i2l3oUZ5aErbnA3An+g/htGsLdFQj6OYrGStD2BTMLJawYBcwzt4 WbcNGhtI7ZwrvVX8ZRUfpEZF6G8gKEOpdD98n3t7HSi+m7P/KZE+bDPJfEdnfinPu8RJ M4Lg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=qg+M8mvk7yxj/VboNGwBKCG34lNWWtwvMc1wkZzmSuw=; fh=oL5u1qxwpthPG0onT/4SNMwjdlMbLicDue2DPzZvioY=; b=w5QA93JsYH++YK45+XVSl6Yo4u9+C9RGSgXUMM8Hjek7CqoUGeBebN3ULmo7cUeJHf TLCsqG5C/pd6BzKaxuUEtrZQVDIIUkkM3vKOePe+isGm4OfI+KN02QhF5yYMA1Z5kHJ7 1aG5dNI45rCWoVexwSj2AX1Mr+FoNd1QYt60AVvPn+C3OYml6LMxtLoedn4bXWxMe94i 4vWq4fe57VG6XYzctm/8Yzgmi5PbDXn1Xb60XhRwrQto0v6+TMcIS3+Vxk1ehUqLbtXb fOdVTtoK5PmByMkSq94HWk7SYDPQNffUIRgIoCMD8OpQBcHqOSr5YXsx3nU5+Y399nQA jdUQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id z15-20020a170903018f00b001c61923a58esi2304975plg.137.2023.10.19.05.30.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 05:30:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 4BCA98229F2C; Thu, 19 Oct 2023 05:30:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345579AbjJSM3T (ORCPT <rfc822;lkml4gm@gmail.com> + 25 others); Thu, 19 Oct 2023 08:29:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345534AbjJSM3R (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 19 Oct 2023 08:29:17 -0400 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [IPv6:2a0a:edc0:2:b01:1d::104]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38861CF for <linux-kernel@vger.kernel.org>; Thu, 19 Oct 2023 05:29:15 -0700 (PDT) Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from <ore@pengutronix.de>) id 1qtS8y-0006Kx-OT; Thu, 19 Oct 2023 14:28:52 +0200 Received: from [2a0a:edc0:0:1101:1d::ac] (helo=dude04.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from <ore@pengutronix.de>) id 1qtS8x-002mrr-Ln; Thu, 19 Oct 2023 14:28:51 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from <ore@pengutronix.de>) id 1qtS8x-00529h-1p; Thu, 19 Oct 2023 14:28:51 +0200 From: Oleksij Rempel <o.rempel@pengutronix.de> To: "David S. Miller" <davem@davemloft.net>, Andrew Lunn <andrew@lunn.ch>, Eric Dumazet <edumazet@google.com>, Florian Fainelli <f.fainelli@gmail.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Vladimir Oltean <olteanv@gmail.com>, Woojung Huh <woojung.huh@microchip.com>, Arun Ramadoss <arun.ramadoss@microchip.com>, Conor Dooley <conor+dt@kernel.org>, Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>, Rob Herring <robh+dt@kernel.org> Cc: Oleksij Rempel <o.rempel@pengutronix.de>, Florian Fainelli <florian.fainelli@broadcom.com>, kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, "Russell King (Oracle)" <linux@armlinux.org.uk>, devicetree@vger.kernel.org Subject: [PATCH net-next v6 7/9] net: dsa: microchip: Add error handling for ksz_switch_macaddr_get() Date: Thu, 19 Oct 2023 14:28:48 +0200 Message-Id: <20231019122850.1199821-8-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231019122850.1199821-1-o.rempel@pengutronix.de> References: <20231019122850.1199821-1-o.rempel@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 19 Oct 2023 05:30:04 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780186991123273044 X-GMAIL-MSGID: 1780186991123273044 |
Series |
net: dsa: microchip: provide Wake on LAN support
|
|
Commit Message
Oleksij Rempel
Oct. 19, 2023, 12:28 p.m. UTC
Enhance the ksz_switch_macaddr_get() function to handle errors that may occur during the call to ksz_write8(). Specifically, this update checks the return value of ksz_write8(), which may fail if regmap ranges validation is not passed and returns the error code. Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> Reviewed-by: Andrew Lunn <andrew@lunn.ch> Reviewed-by: Florian Fainelli <florian.fainelli@broadcom.com> --- drivers/net/dsa/microchip/ksz_common.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
Comments
On Thu, Oct 19, 2023 at 02:28:48PM +0200, Oleksij Rempel wrote: > Enhance the ksz_switch_macaddr_get() function to handle errors that may > occur during the call to ksz_write8(). Specifically, this update checks > the return value of ksz_write8(), which may fail if regmap ranges > validation is not passed and returns the error code. > > Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> > Reviewed-by: Andrew Lunn <andrew@lunn.ch> > Reviewed-by: Florian Fainelli <florian.fainelli@broadcom.com> > --- > drivers/net/dsa/microchip/ksz_common.c | 9 ++++++--- > 1 file changed, 6 insertions(+), 3 deletions(-) > > diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c > index 7b05de6fe987..79052a54880c 100644 > --- a/drivers/net/dsa/microchip/ksz_common.c > +++ b/drivers/net/dsa/microchip/ksz_common.c > @@ -3612,7 +3612,7 @@ int ksz_switch_macaddr_get(struct dsa_switch *ds, int port, > struct ksz_switch_macaddr *switch_macaddr; > struct ksz_device *dev = ds->priv; > const u16 *regs = dev->info->regs; > - int i; > + int i, ret; > > /* Make sure concurrent MAC address changes are blocked */ > ASSERT_RTNL(); > @@ -3639,8 +3639,11 @@ int ksz_switch_macaddr_get(struct dsa_switch *ds, int port, > dev->switch_macaddr = switch_macaddr; > > /* Program the switch MAC address to hardware */ > - for (i = 0; i < ETH_ALEN; i++) > - ksz_write8(dev, regs[REG_SW_MAC_ADDR] + i, addr[i]); > + for (i = 0; i < ETH_ALEN; i++) { > + ret = ksz_write8(dev, regs[REG_SW_MAC_ADDR] + i, addr[i]); > + if (ret) > + return ret; > + } I understand that you intend the error to be fatal, but this leaks memory and a refcount.
On Thu, Oct 19, 2023 at 08:13:08PM +0300, Vladimir Oltean wrote: > On Thu, Oct 19, 2023 at 02:28:48PM +0200, Oleksij Rempel wrote: > > Enhance the ksz_switch_macaddr_get() function to handle errors that may > > occur during the call to ksz_write8(). Specifically, this update checks > > the return value of ksz_write8(), which may fail if regmap ranges > > validation is not passed and returns the error code. > > > > Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> > > Reviewed-by: Andrew Lunn <andrew@lunn.ch> > > Reviewed-by: Florian Fainelli <florian.fainelli@broadcom.com> > > --- > > drivers/net/dsa/microchip/ksz_common.c | 9 ++++++--- > > 1 file changed, 6 insertions(+), 3 deletions(-) > > > > diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c > > index 7b05de6fe987..79052a54880c 100644 > > --- a/drivers/net/dsa/microchip/ksz_common.c > > +++ b/drivers/net/dsa/microchip/ksz_common.c > > @@ -3612,7 +3612,7 @@ int ksz_switch_macaddr_get(struct dsa_switch *ds, int port, > > struct ksz_switch_macaddr *switch_macaddr; > > struct ksz_device *dev = ds->priv; > > const u16 *regs = dev->info->regs; > > - int i; > > + int i, ret; > > > > /* Make sure concurrent MAC address changes are blocked */ > > ASSERT_RTNL(); > > @@ -3639,8 +3639,11 @@ int ksz_switch_macaddr_get(struct dsa_switch *ds, int port, > > dev->switch_macaddr = switch_macaddr; > > > > /* Program the switch MAC address to hardware */ > > - for (i = 0; i < ETH_ALEN; i++) > > - ksz_write8(dev, regs[REG_SW_MAC_ADDR] + i, addr[i]); > > + for (i = 0; i < ETH_ALEN; i++) { > > + ret = ksz_write8(dev, regs[REG_SW_MAC_ADDR] + i, addr[i]); > > + if (ret) > > + return ret; > > + } > > I understand that you intend the error to be fatal, but this leaks memory and a refcount. I didn't estimate correctly what would happen. If ksz_write8() fails as you say, we will leave behind the reference to dev->switch_macaddr, which points to valid memory. Which means that the second time around, the call to ksz_switch_macaddr_get() will succeed, because the driver thinks that the address has been programmed to hardware, and it is unaware of the previous failure. But it will not work. Am I correct? If so, it needs to tear down properly, because if ksz_switch_macaddr_get() fails once to write to hardware, it should fail always.
diff --git a/drivers/net/dsa/microchip/ksz_common.c b/drivers/net/dsa/microchip/ksz_common.c index 7b05de6fe987..79052a54880c 100644 --- a/drivers/net/dsa/microchip/ksz_common.c +++ b/drivers/net/dsa/microchip/ksz_common.c @@ -3612,7 +3612,7 @@ int ksz_switch_macaddr_get(struct dsa_switch *ds, int port, struct ksz_switch_macaddr *switch_macaddr; struct ksz_device *dev = ds->priv; const u16 *regs = dev->info->regs; - int i; + int i, ret; /* Make sure concurrent MAC address changes are blocked */ ASSERT_RTNL(); @@ -3639,8 +3639,11 @@ int ksz_switch_macaddr_get(struct dsa_switch *ds, int port, dev->switch_macaddr = switch_macaddr; /* Program the switch MAC address to hardware */ - for (i = 0; i < ETH_ALEN; i++) - ksz_write8(dev, regs[REG_SW_MAC_ADDR] + i, addr[i]); + for (i = 0; i < ETH_ALEN; i++) { + ret = ksz_write8(dev, regs[REG_SW_MAC_ADDR] + i, addr[i]); + if (ret) + return ret; + } return 0; }