net: dsa: qca8k: preserve port state across MTU changes
PATCH v7 1/1 net: dsa: qca8k: preserve port state across MTU changes
The MTU update pauses enabled ports 0 and 6 and then unconditionally enables their MACs. This can undo a phylink link-down decision. Phylink callbacks also run without RTNL and can enable a MAC during the update. Changing MAX_FRAME_SIZE with a CPU MAC running can stop the switch from sending packets until reset, as described in commit 386228c694bf.
Serialize the MTU transaction and PORT_STATUS writers with a dedicated mutex. Save and restore the MAC bits, abort before the frame-size write on detected snapshot or pause errors, and restore every touched port. Mask partial status values before passing them to the regmap callback.
Return the result of the frame-size write even if a later port restore
fails: DSA does not roll back the switch's frame size when the CPU MTU callback returns an error. Record successful frame-size writes and skip requests for the same value. DSA invokes the callback for every CPU port, so a second invocation must not reject an already committed global value.
Retain failed status writes and retry them every second, because a fixed-link port may receive no further link-up callback. Merge newer port decisions into the pending target, and recover pending writes on ports 0 and 6 before changing the frame size again. Disable and drain retries for suspend, shutdown and removal, with managed cleanup on probe failure.
---
v7:
- Address Sashiko's review of v6 [1]:
- Mask cached PORT_STATUS values for partial writes. The qca8k regmap callbacks do not mask their value argument.
- Track the successfully committed global frame size and return success for repeated requests. This covers DSA invoking the MTU callback for both CPU ports, including a pending restore failure after the first callback. Initialize the record from the setup write and update it only when a frame-size write succeeds.
- The tests now use the actual Ethernet RMW callback and DSA CPU-port iteration, which the v6 fixtures did not model correctly.
v6: https://lore.kernel.org/netdev/[email protected]/
v5: https://lore.kernel.org/netdev/[email protected]/
This remains the standalone net fix for existing ports 0 and 6. Internal CPU support stays in the separate net-next series. The pre-existing NULL PHY dereference noted in the review is outside this change.
The affected arm64 objects build with W=1, with CONFIG_PM_SLEEP enabled and disabled. A userspace harness compiled the driver, Ethernet RMW and DSA MTU functions with mocked transport, topology, netdev and workqueue boundaries. It covered single and dual CPU ports, both traversal orders, MTU growth and shrink, failed initial writes, duplicate callbacks, later MTU changes, retries, callback interleavings and lifecycle handling. The sent v6 and six deliberately broken variants fail the checks. This revision has not been tested on hardware or with kernel lockdep.
The net base can still swallow MDIO write errors. The retry and committed frame-size record handle only errors reported by the register API; they cannot detect a write that falsely reports success or guarantee recovery from a write whose completion is unknown.
Merge note: net-next has changed qca8k_port_set_status() to use regmap_assign_bits(). This patch replaces that function body with the serialized status-update helper, so that hunk needs manual resolution when propagating the net fix to net-next.
[1] https://lore.kernel.org/netdev/[email protected]/
drivers/net/dsa/qca/qca8k-8xxx.c | 33 ++++++- drivers/net/dsa/qca/qca8k-common.c | 150 ++++++++++++++++++++++++----- drivers/net/dsa/qca/qca8k.h | 15 +++ 3 files changed, 174 insertions(+), 24 deletions(-)
drivers/net/dsa/qca/qca8k-8xxx.c +31 -2
@@ -1483,7 +1483,9 @@ qca8k_phylink_mac_link_up(struct phylink_config *config, reg |=QCA8K_PORT_STATUS_TXMAC |QCA8K_PORT_STATUS_RXMAC ; -qca8k_write (priv,QCA8K_REG_PORT_STATUS (port), reg); +mutex_lock (&priv->port_status_lock); +qca8k_port_status_update (priv, port,U32_MAX , reg); +mutex_unlock (&priv->port_status_lock); }static struct qca8k_pcs *pcs_to_qca8k_pcs (struct phylink_pcs *pcs)
@@ -1995,6 +1997,7 @@ qca8k_setup(struct dsa_switch *ds)/* Setup our port MTUs to match power on defaults */ ret =qca8k_write (priv,QCA8K_MAX_FRAME_SIZE ,ETH_FRAME_LEN +ETH_FCS_LEN ); + priv->max_frame_size = ret ?0 :ETH_FRAME_LEN +ETH_FCS_LEN ;if (ret)dev_warn (priv->dev,"failed setting MTU settings" );
@@ -2055,6 +2058,13 @@ static const struct dsa_switch_ops qca8k_switch_ops = { .connect_tag_protocol = qca8k_connect_tag_protocol, }; +static void qca8k_port_status_stop (void *data) +{ +struct qca8k_priv *priv = data; + +disable_delayed_work_sync (&priv->port_status_work); +} +static int qca8k_sw_probe (struct mdio_device *mdiodev) {
@@ -2116,6 +2126,11 @@ qca8k_sw_probe(struct mdio_device *mdiodev) priv->ds->ops = &qca8k_switch_ops; priv->ds->phylink_mac_ops = &qca8k_phylink_mac_ops;mutex_init (&priv->reg_mutex); +mutex_init (&priv->port_status_lock); +INIT_DELAYED_WORK (&priv->port_status_work, qca8k_port_status_work); + ret =devm_add_action_or_reset (priv->dev, qca8k_port_status_stop, priv); +if (ret) +return ret;dev_set_drvdata (&mdiodev->dev, priv);return dsa_register_switch (priv->ds);
@@ -2130,6 +2145,7 @@ qca8k_sw_remove(struct mdio_device *mdiodev)if (!priv)return ; +qca8k_port_status_stop (priv);for (i =0 ; i <QCA8K_NUM_PORTS ; i++)qca8k_port_set_status (priv, i,0 );
@@ -2143,6 +2159,7 @@ static void qca8k_sw_shutdown(struct mdio_device *mdiodev)if (!priv)return ; +qca8k_port_status_stop (priv);dsa_switch_shutdown (priv->ds);dev_set_drvdata (&mdiodev->dev,NULL );
@@ -2165,13 +2182,24 @@ qca8k_set_pm(struct qca8k_priv *priv, int enable) } } +static void qca8k_port_status_start (struct qca8k_priv *priv) +{ +enable_delayed_work (&priv->port_status_work); +schedule_delayed_work (&priv->port_status_work,0 ); +} +static int qca8k_suspend (struct device *dev) {struct qca8k_priv *priv =dev_get_drvdata (dev); +int ret; +qca8k_port_status_stop (priv);qca8k_set_pm (priv,0 ); + ret =dsa_switch_suspend (priv->ds); +if (ret) +qca8k_port_status_start (priv); -return dsa_switch_suspend (priv->ds); +return ret; }static int qca8k_resume (struct device *dev)
@@ -2179,6 +2207,7 @@ static int qca8k_resume(struct device *dev)struct qca8k_priv *priv =dev_get_drvdata (dev);qca8k_set_pm (priv,1 ); +qca8k_port_status_start (priv);return dsa_switch_resume (priv->ds); }
drivers/net/dsa/qca/qca8k-common.c +128 -22
194 lines not on this page · raw message
drivers/net/dsa/qca/qca8k.h +15 -0
@@ -10,6 +10,7 @@#include <linux/delay.h> #include <linux/regmap.h> +#include <linux/workqueue.h> #include <linux/gpio/consumer.h> #include <linux/leds.h> #include <linux/dsa/tag_qca.h>
@@ -459,6 +460,17 @@ struct qca8k_priv {struct mii_bus *internal_mdio_bus;struct dsa_switch *ds;struct mutex reg_mutex; +/* Serializes writes to QCA8K_REG_PORT_STATUS and the MTU update. Taken +* from phylink MAC callbacks: do not access a PHY or call into phylink +* with it held. +*/ +struct mutex port_status_lock; +/* Committed frame size, 0 if unknown; protected by port_status_lock. */ +u32 max_frame_size; +struct delayed_work port_status_work; +/* Nonzero masks mark pending writes; protected by port_status_lock. */ +u32 port_status_mask[QCA8K_NUM_PORTS ]; +u32 port_status_value[QCA8K_NUM_PORTS ];struct device *dev;struct gpio_desc *reset_gpio;struct net_device *mgmt_conduit;/* Track if mdio/mib Ethernet is available */
@@ -502,6 +514,9 @@ static inline u32 qca8k_port_to_phy(int port)extern const struct qca8k_mib_desc ar8327_mib[];extern const struct regmap_access_table qca8k_readable_table;int qca8k_mib_init (struct qca8k_priv *priv); +void qca8k_port_status_work (struct work_struct *work); +void qca8k_port_status_update (struct qca8k_priv *priv,int port, +u32 mask,u32 val);void qca8k_port_set_status (struct qca8k_priv *priv,int port,int enable);int qca8k_read_switch_id (struct qca8k_priv *priv);
base-commit: 78731b46967e3e7102b5cd5062bef185f3404b5c
Signature · 1 line
2.43.0
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Quoted 3 lines · On Thu, 22 Oct 2026 16:03:36 +0000 [email protected] wrote:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
The original problem was found during development, not in production. While preparing the net-next RFC v2 [1], which extended the CPU MAC pause in qca8k_port_change_mtu() to internal CPU ports, I asked an LLM agent for an adversarial review of that change. The review found that the existing sequence re-enables the port 0 and 6 MACs after phylink has taken the link down, and that phylink callbacks can run during the update.
The problems fixed in v6 and v7 were reported by Sashiko on v5 and v6. I reproduced them, and the original problem with the unpatched code, in a userspace harness that compiles the driver and DSA MTU functions with mocked register access. None of them has been reproduced on hardware, and v7 has not been tested on hardware.
Sashiko's review of v7 is a separate question and is still open.
[1] https://lore.kernel.org/netdev/[email protected]/
Thanks,
Yongzhao Chen
Andrew Lunn
maintainerQuoted 6 lines · Yongzhao Chen
Quoted 3 lines · On Thu, 22 Oct 2026 16:03:36 +0000 [email protected] wrote:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
The original problem was found during development, not in production.
The point of these emails it to try to determine if the patch should go to net or net-next.
Is this a real problem which bothers somebody? If it has not been seen in production, it sounds like it does not bother anybody. So net-next.
Andrew
Hi Andrew,
Quoted 2 lines · Andrew Lunn
Is this a real problem which bothers somebody? If it has not been seen in production, it sounds like it does not bother anybody. So net-next.
It has not been seen in production. Following your suggestion, I will move it to net-next.
v7 needs another revision anyway. Sashiko's review [1] is right: a concurrent PORT_STATUS reader such as the PCS poll can corrupt the MTU snapshot, because the qca8k regmap is created with disable_locking and shares its work_buf. Clearing the MAC bits unconditionally would not be enough, since the restore uses the same snapshot and the MAX_FRAME_SIZE write also goes through work_buf. Stanislaw's patch [2] enables the regmap lock. With it, the v7 code passes a local KUnit test that forces this interleaving against the real regmap core.
I will send v8 for net-next once it reopens and [2] is in net-next, and say in the commit message how the problem was found, as sinfo asked.
[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/
pw-bot: cr
Hi Yongzhao,
Quoted 2 lines · Yongzhao Chen
I will send v8 for net-next once it reopens and [2] is in net-next, and say in the commit message how the problem was found, as sinfo asked.
By the same rule my net series [1] belongs in net-next too: all three were found by the AI review of my MIB statistics series, none in production. So I will send its v2 to net-next once it reopens, with the regmap locking patch first and unchanged, and you can base v8 on it. If yours is ready first, carry that patch in your series instead and I will drop it from mine.
[1] https://lore.kernel.org/netdev/[email protected]/
Stanislaw
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] In qca8k_port_change_mtu() (drivers/net/dsa/qca/qca8k-common.c), the decision to pause a CPU MAC…
--
net: dsa: qca8k: preserve port state across MTU changes
This patch adds a dedicated mutex that serializes the MTU update with all PORT_STATUS writers. During the update, the MAC bits of the enabled CPU ports are saved and restored instead of being forced back on afterwards. Failed status writes are recorded and retried from a delayed work, and the committed MAX_FRAME_SIZE is tracked so a duplicate call for the same value returns early.
Quoted 4 lines
drivers/net/dsa/qca/qca8k-common.c +0
-0
[ ... ]
Quoted 14 lines
drivers/net/dsa/qca/qca8k-common.c +3
-6@@ -782,27 +856,59 @@ int qca8k_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)if (!dsa_is_cpu_port (ds, port))return 0 ; -/* To change the MAX_FRAME_SIZE the cpu ports must be off or -* the switch panics. -* Turn off both cpu ports before applying the new value to prevent -* this. +/* To change MAX_FRAME_SIZE, pause enabled ports 0 and 6. +* Serialize with phylink and restore the saved MAC state. */ -if (priv->port_enabled_map &BIT (0 )) -qca8k_port_set_status (priv,0 ,0 ); +mutex_lock (&priv->port_status_lock);
[ ... ]
drivers/net/dsa/qca/qca8k-common.c int qca8k_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu) · line 891
Quoted 13 lines
drivers/net/dsa/qca/qca8k-common.c +13
-0+ ports &= priv->port_enabled_map; +for_each_set_bit (i, &ports,QCA8K_NUM_PORTS ) { + ret =regmap_read (priv->regmap,QCA8K_REG_PORT_STATUS (i), + &status[i]); +if (ret) + goto unlock; + } + +for_each_set_bit (i, &ports,QCA8K_NUM_PORTS ) { +u32 mask =qca8k_port_status_mask (i); + +if (!(status[i] & mask)) +continue ;
[Severity: Medium]
Can a concurrent register reader corrupt the status[] snapshot here and make this skip the pause?
qca8k_regmap_config in qca8k-8xxx.c has:
.disable_locking = true, /* Locking is handled by qca8k read/write */
So regmap_read() goes through _regmap_bus_read() and takes no regmap lock. The register address is formatted into the shared map->work_buf, and qca8k_bulk_read() reads it back from there:
u32 reg = *(u16 *)reg_buf;
The value is parsed from the shared work_val only after the transport has released mdio_lock:
drivers/base/regmap/regmap.c:_regmap_bus_read() {
...
ret = _regmap_raw_read(map, reg, work_val, map->format.val_bytes, false);
if (ret == 0)
*val = map->format.parse_val(work_val);
...
}port_status_lock does not cover readers such as qca8k_pcs_get_state(). Phylink calls it from its resolve worker without RTNL, and it is polled because qca8k_setup_pcs() sets pcs.poll = true. The LED hw_control_get path is another reader that takes no lock.
For example, with port 0 enabled and port 6 in in-band mode after a link_down:
CPU1 (RTNL held)qca8k_port_change_mtu()
regmap_read(QCA8K_REG_PORT_STATUS(0))
CPU2phylink_resolve()
qca8k_pcs_get_state(port 6)
qca8k_read(priv, QCA8K_REG_PORT_STATUS(port), ®)
Suppose port 6's address or value lands in work_buf before port 0's value is parsed. Then status[0] would have no TXMAC/RXMAC bits, the continue above would skip port 0, and this write would run with the port 0 MAC still enabled:
drivers/net/dsa/qca/qca8k-common.c int qca8k_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu) · line 900
Quoted 10 lines
drivers/net/dsa/qca/qca8k-common.c +7
-2+__set_bit (i, &stopped); + ret =regmap_clear_bits (priv->regmap,QCA8K_REG_PORT_STATUS (i), + mask); +if (ret) + goto restore; + } -if (priv->port_enabled_map &BIT (0 )) -qca8k_port_set_status (priv,0 ,1 ); + ret =qca8k_write (priv,QCA8K_MAX_FRAME_SIZE , frame_size);
Commit 386228c694bf says this state stops the switch from sending packets until reset.
The shared work_buf race itself predates this patch. However, the old pause was an unconditional regmap_clear_bits(), which goes through qca8k_regmap_update_bits() and does not use work_buf. So it could not be skipped this way.
The window is small, but should the pause avoid depending on this unlocked read? One option would be to clear the MAC bits of the enabled CPU ports unconditionally, as before.
Quoted 2 lines
drivers/net/dsa/qca/qca8k-common.c +2
-0+if (!ret) + priv->max_frame_size = frame_size;
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261022155837.760-1-yongzhao.derek%40gmail.com