Skip to contentSkip to conversation

net: phy: Improved phy_error() with function and error code

PATCH In review Florian Fainelli 2 participants 3 messages Mar 20, 2023 – Mar 22, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: phy: Improved phy_error() with function and error code

When the PHY library calls phy_error() something bad has happened, and we halt the PHY state machine. To facilitate debugging, introduce phy_error_precise() which allows us to provide useful information about the calling function and the error actually returned to facilitate debugging.

---
 drivers/net/phy/phy.c | 21 ++++++++++++++-------
 include/linux/phy.h   |  6 +++++-
 2 files changed, 19 insertions(+), 8 deletions(-)
drivers/net/phy/phy.c +14 -7
index b33e55a7364e..a84f7873700d 100644
@@ -1170,17 +1170,20 @@ void phy_stop_machine(struct phy_device *phydev)
 }
 
 /**
- * phy_error - enter HALTED state for this PHY device
+ * phy_error_precise - enter HALTED state for this PHY device
  * @phydev: target phy_device struct
+ * @func: symbolic function name that returned the error
+ * @err: return code from the function that caused the error
  *
  * Moves the PHY to the HALTED state in response to a read
  * or write error, and tells the controller the link is down.
  * Must not be called from interrupt context, or while the
  * phydev->lock is held.
  */
-void phy_error(struct phy_device *phydev)
+void phy_error_precise(struct phy_device *phydev, const void *func,
+		       int err)
 {
-	WARN_ON(1);
+	WARN(1, "%pS: returned: %d\n", func, err);
 
 	mutex_lock(&phydev->lock);
 	phydev->state = PHY_HALTED;
@@ -1188,7 +1191,7 @@ void phy_error(struct phy_device *phydev)
 
 	phy_trigger_machine(phydev);
 }
-EXPORT_SYMBOL(phy_error);
+EXPORT_SYMBOL(phy_error_precise);
 
 /**
  * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
@@ -1378,6 +1381,7 @@ void phy_state_machine(struct work_struct *work)
 	struct net_device *dev = phydev->attached_dev;
 	bool needs_aneg = false, do_suspend = false;
 	enum phy_state old_state;
+	const void *func = NULL;
 	bool finished = false;
 	int err = 0;
 
@@ -1396,6 +1400,7 @@ void phy_state_machine(struct work_struct *work)
 	case PHY_NOLINK:
 	case PHY_RUNNING:
 		err = phy_check_link_status(phydev);
+		func = &phy_check_link_status;
 		break;
 	case PHY_CABLETEST:
 		err = phydev->drv->cable_test_get_status(phydev, &finished);
@@ -1425,16 +1430,18 @@ void phy_state_machine(struct work_struct *work)
 
 	mutex_unlock(&phydev->lock);
 
-	if (needs_aneg)
+	if (needs_aneg) {
 		err = phy_start_aneg(phydev);
-	else if (do_suspend)
+		func = &phy_start_aneg;
+	} else if (do_suspend) {
 		phy_suspend(phydev);
+	}
 
 	if (err == -ENODEV)
 		return;
 
 	if (err < 0)
-		phy_error(phydev);
+		phy_error_precise(phydev, func, err);
 
 	if (old_state != phydev->state) {
 		phydev_dbg(phydev, "PHY state change %s -> %s\n",
include/linux/phy.h +5 -1
index fefd5091bc24..dc77b09908da 100644
@@ -1793,7 +1793,11 @@ void phy_drivers_unregister(struct phy_driver *drv, int n);
 int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
 int phy_drivers_register(struct phy_driver *new_driver, int n,
 			 struct module *owner);
-void phy_error(struct phy_device *phydev);
+void phy_error_precise(struct phy_device *phydev, const void *func, int err);
+static inline void phy_error(struct phy_device *phydev)
+{
+	phy_error_precise(phydev, (const void *)_RET_IP_, -EIO);
+}
 void phy_state_machine(struct work_struct *work);
 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
 void phy_trigger_machine(struct phy_device *phydev);
Signature · 1 line
2.34.1
maintainer
#1 lore raw

include/linux/phy.h void phy_drivers_unregister(struct phy_driver *drv, int n); · line 1800

Quoted 4 lines · On Mon, 20 Mar 2023 14:34:51 -0700 Florian Fainelli wrote:
include/linux/phy.h +4 -0
+static inline void phy_error(struct phy_device *phydev)
+{
+	phy_error_precise(phydev, (const void *)_RET_IP_, -EIO);
+}

LGTM apart from this _RET_IP_ here. Wouldn't this make @func sometimes the function that returned the error and sometimes the caller? The caller is in the stack trace already, so no need to duplicate. Besides how dependable is using _RET_IP_ inside a static inline?

#2 lore raw
Quoted 11 lines · Jakub Kicinski

include/linux/phy.h void phy_drivers_unregister(struct phy_driver *drv, int n); · line 1800

Quoted 4 lines · On Mon, 20 Mar 2023 14:34:51 -0700 Florian Fainelli wrote:
include/linux/phy.h +4 -0
+static inline void phy_error(struct phy_device *phydev)
+{
+	phy_error_precise(phydev, (const void *)_RET_IP_, -EIO);
+}

LGTM apart from this _RET_IP_ here. Wouldn't this make @func sometimes the function that returned the error and sometimes the caller? The caller is in the stack trace already, so no need to duplicate. Besides how dependable is using _RET_IP_ inside a static inline?

You have a point that the existing phy_error() already has a WARN_ON() that will tell us which function it has been invoked from whenever phy_error() is used outside of the phy_state_machine(), expect a v2 shortly.

Signature · 1 line
Florian