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

From: Jakub Kicinski

Date: Sat Nov 21 2026 - 18:26:55 EST


This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
---
net: phylink: make PCS phylink instance access RCU protected

This commit makes the phylink back-pointer in struct phylink_pcs RCU
protected and reads it under rcu_read_lock() in phylink_pcs_change().
phylink_destroy() now clears that pointer and calls synchronize_rcu(), so
a concurrent phylink_pcs_change() should not run on a freed phylink
instance.

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index e7734e45376cd..d9d0ace2042e5 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c

[ ... ]

> @@ -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;
> }

[ ... ]

> @@ -2137,6 +2138,11 @@ 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);
> + synchronize_rcu();
> + }
> +
> cancel_work_sync(&pl->resolve);
> kfree(pl);
> }

[Severity: High]
Can phylink_pcs_change() still use pl after kfree(pl) here?

phylink_destroy() only waits for a grace period when pl->pcs is non-NULL.
pl->pcs can be NULL at this point if an earlier phylink_major_config()
switched to a NULL PCS. That happens when mac_select_pcs() returns NULL
for the new interface, for example in the default case of
mv88e639x_pcs_select().

In that case phylink_major_config() cleared old_pcs->phylink and set
pl->pcs to NULL without waiting for a grace period. A sequence like this
looks possible:

PCS driver threaded IRQ handler:
phylink_pcs_change(old_pcs)
rcu_read_lock();
pl = rcu_dereference(pcs->phylink);
<preempted>

Resolve worker:
phylink_major_config()
rcu_assign_pointer(pl->pcs->phylink, NULL);
pl->pcs = pcs; /* pcs == NULL */

Driver teardown:
phylink_stop()
phylink_destroy()
if (pl->pcs) { ... } /* skipped, no synchronize_rcu() */
cancel_work_sync(&pl->resolve);
kfree(pl);

IRQ handler resumes:
phylink_link_changed(pl, up, "pcs");
/* writes pl->link_failed, may queue_work(&pl->resolve) */

phylink_stop() does not seem to close this window. It only handles
phylink's own link_poll timer, link_irq and resolve work, and the
phylink_pcs_disable(pl->pcs) call gets a NULL PCS here. The mv88e6xxx PCS
drivers use request_threaded_irq(), so with CONFIG_PREEMPT_RCU the reader
can be preempted inside its RCU section.

The commit message says the change will "syncronyze RCU to catch an
execution of phylink_pcs_change()". This case still seems to be open at
the end of the series. phylink_detach_pcss() only sets rcu_sync when
pcs_list is non-empty or pl->pcs is non-NULL, and pcs_list stays empty for
mac_select_pcs() drivers. The later READ_ONCE(pl->pcs) == pcs check in
phylink_pcs_change() also dereferences the possibly freed pl.

Would it make sense to call synchronize_rcu() unconditionally in
phylink_destroy() (and later in phylink_detach_pcss())? Another option is
to wait for a grace period whenever phylink_major_config() detaches the
old PCS.