Message ID | 85eb0791bd614ccfdeccdc6fe39be55e602c521c.1682163424.git.daniel@makrotopia.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1657595vqo; Sat, 22 Apr 2023 05:03:58 -0700 (PDT) X-Google-Smtp-Source: AKy350ZomboBW2AN8ECwDjwO/ecsGQppK8VU6wwRFYRZqxFYgRqdtctrYAJaoJSFzU04ufWOCBxo X-Received: by 2002:a05:6a00:1818:b0:634:10a8:538c with SMTP id y24-20020a056a00181800b0063410a8538cmr11452603pfa.12.1682165037705; Sat, 22 Apr 2023 05:03:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682165037; cv=none; d=google.com; s=arc-20160816; b=jBy5MNU1Br/XRXYQHf/vzf+HvSQxZKgFEwvvu16lTnBrqGn/nz3ZJ36ysGZV2Hp3Ks L3UzS+K0mF5SaZCVvaFgkxYFQyRQLZm4FTPlOrqUT4Zzli9L+fONcBDTD7F0ixSqA1Dw beJbZDb5ivFZuX8IkiRUYs5XglEuLsmZVievnKnLfVYaceTPUxL12SmssZowtCuK70IT pqolRdVUHeM1Ndj7otcoP3XVXyFbc8uf6b0/z9i0BUDtkgNRdggykhtmW/rUkbVudK52 rn5XSRdBJBT6vtGLiPqNt/Dyb4jmQ/Ytc66MYk6s22JU/3E0VsbBpJJQosnx3/oweWJN vZtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=Y9hKCZiGJTzdHH6bTMqKZ4wDLV8ZHMWepftXwHd2ktg=; b=OKqZfBfay+dYhLWj0vkPwP64X7CSFSvX0fYZ/leNus299626EJYRYXsroYJsD82vBM zuBoqbXV0Oq7JaXv/hMubOeCxKMzeqMFmUiz7Ax36sFmxQxYGd5xIWnnLzYipNhk7RKN hGenkm46lDEK/tSRVGacy9uu0f9TR9Ml3k5neMyZxeYLnU6kbNLX1z4CkUjF5vJ6HWnQ yKSPq8Kla47hLYY0xJScfUzwOSGagXfa0lQIhALiZd8gs1djb+fn0wPejrsP9v++iqvh UDhzyyZ4Go3CKjetMeBhdgZ5fvWdtjPjzNBXX+/nn6FISan9k1HRY0aOzYK0FgKS2slD sA3A== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g16-20020a63dd50000000b004dccf388f93si6193918pgj.522.2023.04.22.05.03.41; Sat, 22 Apr 2023 05:03:57 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229812AbjDVLta (ORCPT <rfc822;cjcooper78@gmail.com> + 99 others); Sat, 22 Apr 2023 07:49:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229791AbjDVLtX (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 22 Apr 2023 07:49:23 -0400 Received: from fudo.makrotopia.org (fudo.makrotopia.org [IPv6:2a07:2ec0:3002::71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D3461FC8; Sat, 22 Apr 2023 04:49:05 -0700 (PDT) Received: from local by fudo.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from <daniel@makrotopia.org>) id 1pqBjj-00085Z-1c; Sat, 22 Apr 2023 13:49:03 +0200 Date: Sat, 22 Apr 2023 12:48:59 +0100 From: Daniel Golle <daniel@makrotopia.org> To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Andrew Lunn <andrew@lunn.ch>, Heiner Kallweit <hkallweit1@gmail.com>, Russell King <linux@armlinux.org.uk>, "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com> Cc: Chen Minqiang <ptpt52@gmail.com>, Chukun Pan <amadeus@jmu.edu.cn>, Yevhen Kolomeiko <jarvis2709@gmail.com>, Alexander Couzens <lynxis@fe80.eu> Subject: [RFC PATCH net-next 5/8] net: phy: realtek: use phy_read_paged instead of open coding Message-ID: <85eb0791bd614ccfdeccdc6fe39be55e602c521c.1682163424.git.daniel@makrotopia.org> References: <cover.1682163424.git.daniel@makrotopia.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <cover.1682163424.git.daniel@makrotopia.org> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763877886526270368?= X-GMAIL-MSGID: =?utf-8?q?1763877886526270368?= |
Series |
Improvements for RealTek 2.5G Ethernet PHYs
|
|
Commit Message
Daniel Golle
April 22, 2023, 11:48 a.m. UTC
Instead of open coding a paged read, use the phy_read_paged function
in rtlgen_supports_2_5gbps.
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
---
drivers/net/phy/realtek.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
Comments
On 22.04.2023 13:48, Daniel Golle wrote: > Instead of open coding a paged read, use the phy_read_paged function > in rtlgen_supports_2_5gbps. > > Signed-off-by: Daniel Golle <daniel@makrotopia.org> > --- > drivers/net/phy/realtek.c | 4 +--- > 1 file changed, 1 insertion(+), 3 deletions(-) > > diff --git a/drivers/net/phy/realtek.c b/drivers/net/phy/realtek.c > index f97b5e49fae58..62fb965b6d338 100644 > --- a/drivers/net/phy/realtek.c > +++ b/drivers/net/phy/realtek.c > @@ -735,9 +735,7 @@ static bool rtlgen_supports_2_5gbps(struct phy_device *phydev) > { > int val; > > - phy_write(phydev, RTL821x_PAGE_SELECT, 0xa61); > - val = phy_read(phydev, 0x13); > - phy_write(phydev, RTL821x_PAGE_SELECT, 0); > + val = phy_read_paged(phydev, 0xa61, 0x13); > > return val >= 0 && val & RTL_SUPPORTS_2500FULL; > } I remember I had a reason to open-code it, it took me some minutes to recall it. phy_read_paged() calls __phy_read_page() that relies on phydev->drv being set. phydev->drv is set in phy_probe(). And probing is done after matching. __phy_read_paged() should have given you a warning. Did you test this patch? If yes and you didn't get the warning, then apparently I miss something.
On Sat, Apr 22, 2023 at 05:11:57PM +0200, Heiner Kallweit wrote: > On 22.04.2023 13:48, Daniel Golle wrote: > > Instead of open coding a paged read, use the phy_read_paged function > > in rtlgen_supports_2_5gbps. > > > > Signed-off-by: Daniel Golle <daniel@makrotopia.org> > > --- > > drivers/net/phy/realtek.c | 4 +--- > > 1 file changed, 1 insertion(+), 3 deletions(-) > > > > diff --git a/drivers/net/phy/realtek.c b/drivers/net/phy/realtek.c > > index f97b5e49fae58..62fb965b6d338 100644 > > --- a/drivers/net/phy/realtek.c > > +++ b/drivers/net/phy/realtek.c > > @@ -735,9 +735,7 @@ static bool rtlgen_supports_2_5gbps(struct phy_device *phydev) > > { > > int val; > > > > - phy_write(phydev, RTL821x_PAGE_SELECT, 0xa61); > > - val = phy_read(phydev, 0x13); > > - phy_write(phydev, RTL821x_PAGE_SELECT, 0); > > + val = phy_read_paged(phydev, 0xa61, 0x13); > > > > return val >= 0 && val & RTL_SUPPORTS_2500FULL; > > } > > I remember I had a reason to open-code it, it took me some minutes > to recall it. > phy_read_paged() calls __phy_read_page() that relies on phydev->drv > being set. phydev->drv is set in phy_probe(). And probing is done > after matching. __phy_read_paged() should have given you a warning. > Did you test this patch? If yes and you didn't get the warning, > then apparently I miss something. > Yes, you are right, this change was a bit too naive and causes a NULL pointer dereference e.g. for the r8169 driver which also uses the RealTek Ethernet PHY driver. My main concern and original motivation was the lack of mutex protection for the paged read operation. I suggest to rather make this change instead: From 4dd2cc9b91ecb25f278a2c55e07e6455e9000e6b Mon Sep 17 00:00:00 2001 From: Daniel Golle <daniel@makrotopia.org> Date: Sun, 23 Apr 2023 18:47:45 +0100 Subject: [PATCH] net: phy: realtek: make sure paged read is protected by mutex As we cannot rely on phy_read_paged function before the PHY is identified, the paged read in rtlgen_supports_2_5gbps needs to be open coded as it is being called by the match_phy_device function, ie. before .read_page and .write_page have been populated. Make sure it is also protected by the MDIO bus mutex and use rtl821x_write_page instead of 3 individually locked MDIO bus operations. Signed-off-by: Daniel Golle <daniel@makrotopia.org> --- drivers/net/phy/realtek.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/net/phy/realtek.c b/drivers/net/phy/realtek.c index f97b5e49fae5..c27ec4e99fc2 100644 --- a/drivers/net/phy/realtek.c +++ b/drivers/net/phy/realtek.c @@ -735,9 +735,11 @@ static bool rtlgen_supports_2_5gbps(struct phy_device *phydev) { int val; - phy_write(phydev, RTL821x_PAGE_SELECT, 0xa61); - val = phy_read(phydev, 0x13); - phy_write(phydev, RTL821x_PAGE_SELECT, 0); + mutex_lock(&phydev->mdio.bus->mdio_lock); + rtl821x_write_page(phydev, 0xa61); + val = __phy_read(phydev, 0x13); + rtl821x_write_page(phydev, 0); + mutex_unlock(&phydev->mdio.bus->mdio_lock); return val >= 0 && val & RTL_SUPPORTS_2500FULL; }
On 23.04.2023 20:01, Daniel Golle wrote: > On Sat, Apr 22, 2023 at 05:11:57PM +0200, Heiner Kallweit wrote: >> On 22.04.2023 13:48, Daniel Golle wrote: >>> Instead of open coding a paged read, use the phy_read_paged function >>> in rtlgen_supports_2_5gbps. >>> >>> Signed-off-by: Daniel Golle <daniel@makrotopia.org> >>> --- >>> drivers/net/phy/realtek.c | 4 +--- >>> 1 file changed, 1 insertion(+), 3 deletions(-) >>> >>> diff --git a/drivers/net/phy/realtek.c b/drivers/net/phy/realtek.c >>> index f97b5e49fae58..62fb965b6d338 100644 >>> --- a/drivers/net/phy/realtek.c >>> +++ b/drivers/net/phy/realtek.c >>> @@ -735,9 +735,7 @@ static bool rtlgen_supports_2_5gbps(struct phy_device *phydev) >>> { >>> int val; >>> >>> - phy_write(phydev, RTL821x_PAGE_SELECT, 0xa61); >>> - val = phy_read(phydev, 0x13); >>> - phy_write(phydev, RTL821x_PAGE_SELECT, 0); >>> + val = phy_read_paged(phydev, 0xa61, 0x13); >>> >>> return val >= 0 && val & RTL_SUPPORTS_2500FULL; >>> } >> >> I remember I had a reason to open-code it, it took me some minutes >> to recall it. >> phy_read_paged() calls __phy_read_page() that relies on phydev->drv >> being set. phydev->drv is set in phy_probe(). And probing is done >> after matching. __phy_read_paged() should have given you a warning. >> Did you test this patch? If yes and you didn't get the warning, >> then apparently I miss something. >> > > Yes, you are right, this change was a bit too naive and causes a > NULL pointer dereference e.g. for the r8169 driver which also uses > the RealTek Ethernet PHY driver. > My main concern and original motivation was the lack of mutex protection > for the paged read operation. I suggest to rather make this change > instead: > >>From 4dd2cc9b91ecb25f278a2c55e07e6455e9000e6b Mon Sep 17 00:00:00 2001 > From: Daniel Golle <daniel@makrotopia.org> > Date: Sun, 23 Apr 2023 18:47:45 +0100 > Subject: [PATCH] net: phy: realtek: make sure paged read is protected by mutex > > As we cannot rely on phy_read_paged function before the PHY is > identified, the paged read in rtlgen_supports_2_5gbps needs to be open > coded as it is being called by the match_phy_device function, ie. before > .read_page and .write_page have been populated. > > Make sure it is also protected by the MDIO bus mutex and use > rtl821x_write_page instead of 3 individually locked MDIO bus operations. > > Signed-off-by: Daniel Golle <daniel@makrotopia.org> > --- > drivers/net/phy/realtek.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) > > diff --git a/drivers/net/phy/realtek.c b/drivers/net/phy/realtek.c > index f97b5e49fae5..c27ec4e99fc2 100644 > --- a/drivers/net/phy/realtek.c > +++ b/drivers/net/phy/realtek.c > @@ -735,9 +735,11 @@ static bool rtlgen_supports_2_5gbps(struct phy_device *phydev) > { > int val; > > - phy_write(phydev, RTL821x_PAGE_SELECT, 0xa61); > - val = phy_read(phydev, 0x13); > - phy_write(phydev, RTL821x_PAGE_SELECT, 0); > + mutex_lock(&phydev->mdio.bus->mdio_lock); We have helpers phy_(un)lock_mdio_bus() for this. Apart from that: LGTM > + rtl821x_write_page(phydev, 0xa61); > + val = __phy_read(phydev, 0x13); > + rtl821x_write_page(phydev, 0); > + mutex_unlock(&phydev->mdio.bus->mdio_lock); > > return val >= 0 && val & RTL_SUPPORTS_2500FULL; > }
diff --git a/drivers/net/phy/realtek.c b/drivers/net/phy/realtek.c index f97b5e49fae58..62fb965b6d338 100644 --- a/drivers/net/phy/realtek.c +++ b/drivers/net/phy/realtek.c @@ -735,9 +735,7 @@ static bool rtlgen_supports_2_5gbps(struct phy_device *phydev) { int val; - phy_write(phydev, RTL821x_PAGE_SELECT, 0xa61); - val = phy_read(phydev, 0x13); - phy_write(phydev, RTL821x_PAGE_SELECT, 0); + val = phy_read_paged(phydev, 0xa61, 0x13); return val >= 0 && val & RTL_SUPPORTS_2500FULL; }