kernel: net: phy: realtek: fix NULL pointer dereference
authorDaniel Golle <daniel@makrotopia.org>
Sun, 23 Apr 2023 17:46:02 +0000 (18:46 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Mon, 24 Apr 2023 02:16:26 +0000 (03:16 +0100)
The previous attempt to replace an open coded paged read in the RealTek
Ethernet PHY driver was too naive and resulted in breaking the r8169
PCIe Ethernet driver which also makes use of the RealTek Ethernet PHY
driver.
Fix this by instead of using the (not yet populated) paged operations
rather use rtl821x_write_page and protect the whole paged read operation
using the MDIO bus mutex.

Fixes: 998b973157 ("kernel: net: phy: realtek: improve RealTek 2.5G PHY driver")
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
target/linux/generic/pending-5.15/726-net-phy-realtek-make-sure-paged-read-is-protected-by.patch [new file with mode: 0644]
target/linux/generic/pending-5.15/726-net-phy-realtek-use-phy_read_paged-instead-of-open-c.patch [deleted file]
target/linux/generic/pending-5.15/729-net-phy-realtek-introduce-rtl822x_probe.patch

diff --git a/target/linux/generic/pending-5.15/726-net-phy-realtek-make-sure-paged-read-is-protected-by.patch b/target/linux/generic/pending-5.15/726-net-phy-realtek-make-sure-paged-read-is-protected-by.patch
new file mode 100644 (file)
index 0000000..31f0622
--- /dev/null
@@ -0,0 +1,35 @@
+From 4dd2cc9b91ecb25f278a2c55e07e6455e9000e6b Mon Sep 17 00:00:00 2001
+From: Daniel Golle <daniel@makrotopia.org>
+Date: Sat, 22 Apr 2023 01:21:14 +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(-)
+
+--- a/drivers/net/phy/realtek.c
++++ b/drivers/net/phy/realtek.c
+@@ -727,9 +727,11 @@ static bool rtlgen_supports_2_5gbps(stru
+ {
+       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;
+ }
diff --git a/target/linux/generic/pending-5.15/726-net-phy-realtek-use-phy_read_paged-instead-of-open-c.patch b/target/linux/generic/pending-5.15/726-net-phy-realtek-use-phy_read_paged-instead-of-open-c.patch
deleted file mode 100644 (file)
index 250436f..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-From 934cdd67e7cf71f97a2a8aea2892e540af47dcdf Mon Sep 17 00:00:00 2001
-From: Daniel Golle <daniel@makrotopia.org>
-Date: Sat, 22 Apr 2023 01:21:14 +0100
-Subject: [PATCH 1/3] net: phy: realtek: use phy_read_paged instead of open
- coding
-
-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(-)
-
---- a/drivers/net/phy/realtek.c
-+++ b/drivers/net/phy/realtek.c
-@@ -727,9 +727,7 @@ static bool rtlgen_supports_2_5gbps(stru
- {
-       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;
- }
index 4496d6cedf944c99335fb5a643838a3fe78a78b0..87102d38187501003cbb26e9aea4b0ffd6fd366e 100644 (file)
@@ -24,7 +24,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
  #define RTL8366RB_POWER_SAVE                  0x15
  #define RTL8366RB_POWER_SAVE_ON                       BIT(12)
  
-@@ -736,6 +740,25 @@ static int rtl8226_match_phy_device(stru
+@@ -740,6 +744,25 @@ static int rtl8226_match_phy_device(stru
               rtlgen_supports_2_5gbps(phydev);
  }
  
@@ -50,7 +50,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
  static int rtlgen_resume(struct phy_device *phydev)
  {
        int ret = genphy_resume(phydev);
-@@ -1009,6 +1032,7 @@ static struct phy_driver realtek_drvs[]
+@@ -1013,6 +1036,7 @@ static struct phy_driver realtek_drvs[]
                .match_phy_device = rtl8226_match_phy_device,
                .get_features   = rtl822x_get_features,
                .config_aneg    = rtl822x_config_aneg,
@@ -58,7 +58,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
                .read_status    = rtl822x_read_status,
                .suspend        = genphy_suspend,
                .resume         = rtlgen_resume,
-@@ -1022,6 +1046,7 @@ static struct phy_driver realtek_drvs[]
+@@ -1026,6 +1050,7 @@ static struct phy_driver realtek_drvs[]
                .name           = "RTL8226B_RTL8221B 2.5Gbps PHY",
                .get_features   = rtl822x_get_features,
                .config_aneg    = rtl822x_config_aneg,
@@ -66,7 +66,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
                .read_status    = rtl822x_read_status,
                .suspend        = genphy_suspend,
                .resume         = rtlgen_resume,
-@@ -1035,6 +1060,7 @@ static struct phy_driver realtek_drvs[]
+@@ -1039,6 +1064,7 @@ static struct phy_driver realtek_drvs[]
                .name           = "RTL8226-CG 2.5Gbps PHY",
                .get_features   = rtl822x_get_features,
                .config_aneg    = rtl822x_config_aneg,
@@ -74,7 +74,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
                .read_status    = rtl822x_read_status,
                .suspend        = genphy_suspend,
                .resume         = rtlgen_resume,
-@@ -1046,6 +1072,7 @@ static struct phy_driver realtek_drvs[]
+@@ -1050,6 +1076,7 @@ static struct phy_driver realtek_drvs[]
                .name           = "RTL8226B-CG_RTL8221B-CG 2.5Gbps PHY",
                .get_features   = rtl822x_get_features,
                .config_aneg    = rtl822x_config_aneg,
@@ -82,7 +82,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
                .read_status    = rtl822x_read_status,
                .suspend        = genphy_suspend,
                .resume         = rtlgen_resume,
-@@ -1058,6 +1085,7 @@ static struct phy_driver realtek_drvs[]
+@@ -1062,6 +1089,7 @@ static struct phy_driver realtek_drvs[]
                .get_features   = rtl822x_get_features,
                .config_init    = rtl8221b_config_init,
                .config_aneg    = rtl822x_config_aneg,
@@ -90,7 +90,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
                .read_status    = rtl822x_read_status,
                .suspend        = genphy_suspend,
                .resume         = rtlgen_resume,
-@@ -1070,6 +1098,7 @@ static struct phy_driver realtek_drvs[]
+@@ -1074,6 +1102,7 @@ static struct phy_driver realtek_drvs[]
                .get_features   = rtl822x_get_features,
                .config_aneg    = rtl822x_config_aneg,
                .config_init    = rtl8221b_config_init,