[PATCH net-next v23 01/15] net: phylink: make PCS phylink instance access RCU protected

From: Christian Marangi

Date: Sun Nov 29 2026 - 18:06:33 EST


In the context of phylink_pcs_change(), phylink instance is dereferenced
from the pcs pointer present in the phylink_pcs struct.

There is currently a fragility where this pcs pointer is never set NULL in
phylink_destroy() and would reference invalid memory after phylink instance
is freed.

In this scenario, if a PCS driver calls phylink_pcs_change(), invalid
memory can be dereferenced and phylink_link_changed() is called on freed
memory.

To address this, make the phylink pointer saved in phylink_pcs struct RCU
protected and access it under an RCU read lock. Use rcu_assign_pointer()
to update the pointer in phylink_major_config().

Also correctly reset the phylink instance pointer on phylink_destroy() and
unconditionally synchronize RCU to wait for any in-flight
phylink_pcs_change() calls, even if phylink_major_config() already cleared
the association.

Fixes: 24699cc1ff3e ("net: phylink: add support for PCS link change notifications")
Signed-off-by: Christian Marangi <ansuelsmth@xxxxxxxxx>
---
drivers/net/phy/phylink.c | 22 +++++++++++++++++++---
include/linux/phylink.h | 2 +-
2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index d1f1142a98a4..d736604b86f4 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -17,6 +17,7 @@
#include <linux/phy_link_topology.h>
#include <linux/phy_port.h>
#include <linux/phylink.h>
+#include <linux/rcupdate.h>
#include <linux/rtnetlink.h>
#include <linux/spinlock.h>
#include <linux/timer.h>
@@ -1295,10 +1296,10 @@ static void phylink_major_config(struct phylink *pl, bool restart,
phylink_pcs_disable(pl->pcs);

if (pl->pcs)
- pl->pcs->phylink = NULL;
+ rcu_assign_pointer(pl->pcs->phylink, NULL);

if (pcs)
- pcs->phylink = pl;
+ rcu_assign_pointer(pcs->phylink, pl);

pl->pcs = pcs;
}
@@ -2157,6 +2158,16 @@ void phylink_destroy(struct phylink *pl)
if (pl->link_gpio)
gpiod_put(pl->link_gpio);

+ if (pl->pcs)
+ rcu_assign_pointer(pl->pcs->phylink, NULL);
+
+ /*
+ * Unconditionally wait for readers that may have obtained this
+ * phylink through a PCS while phylink_major_config() was clearing
+ * the association.
+ */
+ synchronize_rcu();
+
cancel_work_sync(&pl->resolve);
kfree(pl);
}
@@ -2621,10 +2632,15 @@ EXPORT_SYMBOL_GPL(phylink_mac_change);
*/
void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
{
- struct phylink *pl = pcs->phylink;
+ struct phylink *pl;

+ rcu_read_lock();
+
+ pl = rcu_dereference(pcs->phylink);
if (pl)
phylink_link_changed(pl, up, "pcs");
+
+ rcu_read_unlock();
}
EXPORT_SYMBOL_GPL(phylink_pcs_change);

diff --git a/include/linux/phylink.h b/include/linux/phylink.h
index a7b6294cc4a0..27515d0bc57e 100644
--- a/include/linux/phylink.h
+++ b/include/linux/phylink.h
@@ -494,7 +494,7 @@ struct phylink_pcs_ops;
struct phylink_pcs {
DECLARE_PHY_INTERFACE_MASK(supported_interfaces);
const struct phylink_pcs_ops *ops;
- struct phylink *phylink;
+ struct phylink __rcu *phylink;
bool poll;
bool rxc_always_on;
};
--
2.55.0