Skip to contentSkip to conversation

miiphy: define mii_devs with LIST_HEAD()

PATCH In review Weijie Gao 2 participants 3 messages Jan 25, 2025 – Mar 4, 2025 Networking

lore
Share & tools
lore

Weijie Gao

author
#0 lore raw

PATCH 1/1 miiphy: define mii_devs with LIST_HEAD()

When enabling net console and console multiplexing, a boot crash was observed using mtk_eth driver with stdin/stdout set to "serial,nc" in persistent environment:

Quoted 6 lines
CPU:   MediaTek MT7981
Model: OpenWrt One
DRAM:  1 GiB
Core:  35 devices, 15 uclasses, devicetree: separate
spi-nand: spi_nand spi_nand@0: Winbond SPI NAND was found.
spi-nand: spi_nand spi_nand@0: 128 MiB, block size: 128 KiB, page size: 2048, OOB size: 64
Quoted 10 lines

Loading Environment from UBI... SF: Detected w25q128 with page size 256 Bytes, erase size 4 KiB, total 16 MiB
mtd: partition "ubi" extends beyond the end of device "spi-nand0" -- size truncated to 0x7f00000 Read 126976 bytes from volume ubootenv to 000000007f7bf0c0
Read 126976 bytes from volume ubootenv2 to 000000007f7de100
OK
"Synchronous Abort" handler, esr 0x96000004, far 0xeafffffeea000018
elr: 0000000041e63cd4 lr : 0000000041e1b844 (reloc)
elr: 000000007ff9ecd4 lr : 000000007ff56844
x0 : eafffffeea000018 x1 : 000000007fb552e0
x2 : 00000000000000fe x3 : 0000000000000000

The cause is that "serial,nc" forced the console subsystem to initialize the ethernet driver before ethernet subsystem initialization (console_init_r() is called before initr_net()).

During the mtk_eth driver initialization, mdio_register() will be called, and miiphy_get_dev_by_name() will then be called.

The miiphy_get_dev_by_name() will check the list "mii_devs" to see if the passed device name exists. However the mii_devs is defined without initialization:

Quoted 1 line

static struct list_head mii_devs;

and the actual initialization is done in the following chain:
initr_net -> eth_initialize -> eth_common_init -> miiphy_init Since initr_net() hasn't be called, iterating over the mii_devs will access to physical address 0 (mii_devs.next == NULL) and will cause the crash.

The fix is to define mii_devs using:

Quoted 1 line

static LIST_HEAD(mii_devs);

As the "current_mii" is defined as a static variable, it will always be NULL in board_r stage and initializing it will NULL is unnecessary. So the entire miiphy_init() can be remove.

---
 common/miiphyutil.c | 12 +-----------
 include/miiphy.h    |  2 --
 net/eth_common.c    |  5 -----
 3 files changed, 1 insertion(+), 18 deletions(-)
common/miiphyutil.c +1 -11
index 9b8744e5d8..6169ea884a 100644
@@ -30,7 +30,7 @@
 #define debug(fmt, args...)
 #endif /* MII_DEBUG */
 
-static struct list_head mii_devs;
+static LIST_HEAD(mii_devs);
 static struct mii_dev *current_mii;
 
 /*
@@ -55,16 +55,6 @@ struct mii_dev *miiphy_get_dev_by_name(const char *devname)
 	return NULL;
 }
 
-/*****************************************************************************
- *
- * Initialize global data. Need to be called before any other miiphy routine.
- */
-void miiphy_init(void)
-{
-	INIT_LIST_HEAD(&mii_devs);
-	current_mii = NULL;
-}
-
 struct mii_dev *mdio_alloc(void)
 {
 	struct mii_dev *bus;
include/miiphy.h +0 -2
index 5abffd8fb6..8e4b58c734 100644
@@ -33,8 +33,6 @@ int miiphy_is_1000base_x(const char *devname, unsigned char addr);
 int miiphy_link(const char *devname, unsigned char addr);
 #endif
 
-void miiphy_init(void);
-
 int miiphy_set_current_dev(const char *devname);
 const char *miiphy_get_current_dev(void);
 struct mii_dev *mdio_get_current_dev(void);
net/eth_common.c +0 -5
index 89b5bb3718..ba57d836b0 100644
@@ -31,11 +31,6 @@ int eth_env_set_enetaddr_by_index(const char *base_name, int index,
 void eth_common_init(void)
 {
 	bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
-#if CONFIG_IS_ENABLED(ETH)
-#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
-	miiphy_init();
-#endif
-#endif
 }
 
 int eth_mac_skip(int index)
Signature · 1 line
2.34.1
author
#1 lore raw

Hi,

Is there any update for this patch?

Thanks

Quoted 115 lines · Weijie Gao <[email protected]> 于2025年1月25日周六 23:27写道:

When enabling net console and console multiplexing, a boot crash was observed using mtk_eth driver with stdin/stdout set to "serial,nc" in persistent environment:

Quoted 6 lines
CPU:   MediaTek MT7981
Model: OpenWrt One
DRAM:  1 GiB
Core:  35 devices, 15 uclasses, devicetree: separate
spi-nand: spi_nand spi_nand@0: Winbond SPI NAND was found.
spi-nand: spi_nand spi_nand@0: 128 MiB, block size: 128 KiB, page size: 2048, OOB size: 64
Quoted 10 lines

Loading Environment from UBI... SF: Detected w25q128 with page size 256 Bytes, erase size 4 KiB, total 16 MiB
mtd: partition "ubi" extends beyond the end of device "spi-nand0" -- size truncated to 0x7f00000 Read 126976 bytes from volume ubootenv to 000000007f7bf0c0
Read 126976 bytes from volume ubootenv2 to 000000007f7de100
OK
"Synchronous Abort" handler, esr 0x96000004, far 0xeafffffeea000018
elr: 0000000041e63cd4 lr : 0000000041e1b844 (reloc)
elr: 000000007ff9ecd4 lr : 000000007ff56844
x0 : eafffffeea000018 x1 : 000000007fb552e0
x2 : 00000000000000fe x3 : 0000000000000000

The cause is that "serial,nc" forced the console subsystem to initialize the ethernet driver before ethernet subsystem initialization (console_init_r() is called before initr_net()).

During the mtk_eth driver initialization, mdio_register() will be called, and miiphy_get_dev_by_name() will then be called.

The miiphy_get_dev_by_name() will check the list "mii_devs" to see if the passed device name exists. However the mii_devs is defined without initialization:

Quoted 1 line

static struct list_head mii_devs;

and the actual initialization is done in the following chain:
initr_net -> eth_initialize -> eth_common_init -> miiphy_init Since initr_net() hasn't be called, iterating over the mii_devs will access to physical address 0 (mii_devs.next == NULL) and will cause the crash.

The fix is to define mii_devs using:

Quoted 1 line

static LIST_HEAD(mii_devs);

As the "current_mii" is defined as a static variable, it will always be NULL in board_r stage and initializing it will NULL is unnecessary. So the entire miiphy_init() can be remove.

---
 common/miiphyutil.c | 12 +-----------
 include/miiphy.h    |  2 --
 net/eth_common.c    |  5 -----
 3 files changed, 1 insertion(+), 18 deletions(-)
common/miiphyutil.c +1 -11
index 9b8744e5d8..6169ea884a 100644
@@ -30,7 +30,7 @@
 #define debug(fmt, args...)
 #endif /* MII_DEBUG */
 
-static struct list_head mii_devs;
+static LIST_HEAD(mii_devs);
 static struct mii_dev *current_mii;
 
 /*
@@ -55,16 +55,6 @@ struct mii_dev *miiphy_get_dev_by_name(const char *devname)
        return NULL;
 }
 
-/*****************************************************************************
- *
- * Initialize global data. Need to be called before any other miiphy routine.
- */
-void miiphy_init(void)
-{
-       INIT_LIST_HEAD(&mii_devs);
-       current_mii = NULL;
-}
-
 struct mii_dev *mdio_alloc(void)
 {
        struct mii_dev *bus;
include/miiphy.h +0 -2
index 5abffd8fb6..8e4b58c734 100644
@@ -33,8 +33,6 @@ int miiphy_is_1000base_x(const char *devname, unsigned char addr);
 int miiphy_link(const char *devname, unsigned char addr);
 #endif
 
-void miiphy_init(void);
-
 int miiphy_set_current_dev(const char *devname);
 const char *miiphy_get_current_dev(void);
 struct mii_dev *mdio_get_current_dev(void);
net/eth_common.c +0 -6
index 89b5bb3718..ba57d836b0 100644
@@ -31,11 +31,6 @@ int eth_env_set_enetaddr_by_index(const char *base_name, int index,
 void eth_common_init(void)
 {
        bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
-#if CONFIG_IS_ENABLED(ETH)
-#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
-       miiphy_init();
-#endif
-#endif
 }
 
 int eth_mac_skip(int index)
--

2.34.1

#2 lore raw
In reply to Weijie Gao
Quoted 21 lines · Weijie Gao

When enabling net console and console multiplexing, a boot crash was observed using mtk_eth driver with stdin/stdout set to "serial,nc" in persistent environment:

Quoted 6 lines
CPU:   MediaTek MT7981
Model: OpenWrt One
DRAM:  1 GiB
Core:  35 devices, 15 uclasses, devicetree: separate
spi-nand: spi_nand spi_nand@0: Winbond SPI NAND was found.
spi-nand: spi_nand spi_nand@0: 128 MiB, block size: 128 KiB, page size: 2048, OOB size: 64
Quoted 10 lines

Loading Environment from UBI... SF: Detected w25q128 with page size 256 Bytes, erase size 4 KiB, total 16 MiB
mtd: partition "ubi" extends beyond the end of device "spi-nand0" -- size truncated to 0x7f00000 Read 126976 bytes from volume ubootenv to 000000007f7bf0c0
Read 126976 bytes from volume ubootenv2 to 000000007f7de100
OK
"Synchronous Abort" handler, esr 0x96000004, far 0xeafffffeea000018
elr: 0000000041e63cd4 lr : 0000000041e1b844 (reloc)
elr: 000000007ff9ecd4 lr : 000000007ff56844
x0 : eafffffeea000018 x1 : 000000007fb552e0
x2 : 00000000000000fe x3 : 0000000000000000

Quoted 1 line

[...]

Applied to u-boot/master, thanks!

Signature · 1 line
Tom