qca8k_bulk_read() is the regmap read op. When the Ethernet management path is not available it falls back to reading the registers one by one over MDIO, but it stores register i at val_buf + i with val_buf a void pointer: the words land one byte apart and overwrite each other, and the last six bytes of a three-register read are never written. Every multi-register read over MDIO is garbage then. qca8k_fdb_read() reads the three ATU registers this way, so the FDB dump shows MAC addresses made of the VID and the aging field, the search reads a wrong status and port mask for the entry it found, and qca8k_port_mdb_add() and qca8k_port_mdb_del() write that port mask back into the ATU. With tag_qca the management frames carry these reads and the fallback is rarely taken; it is taken every time when the management conduit is down, when a management read fails, and on boards whose CPU port is not port 0, where qca8k_conduit_change() never sets up the management conduit. Store each register in its own u32. The bug was found while bringing up a tagger without management frames; an LLM assistant (Claude, claude-fable-5-1) helped write the fix and this description. I reviewed both and tested the fix on a QCA8337 behind an IPQ5018 (GL.iNet GL-B3000), where bridge fdb show returns the right entries with it and garbage without. Fixes: c766e077d927 ("net: dsa: qca8k: convert to regmap read/write API") Signed-off-by: Stanislaw Pal --- drivers/net/dsa/qca/qca8k-8xxx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/net/dsa/qca/qca8k-8xxx.c b/drivers/net/dsa/qca/qca8k-8xxx.c index 382753bcc4..3c8044a4d1 100644 --- a/drivers/net/dsa/qca/qca8k-8xxx.c +++ b/drivers/net/dsa/qca/qca8k-8xxx.c @@ -519,7 +519,7 @@ qca8k_bulk_read(void *ctx, const void *reg_buf, size_t reg_len, /* loop count times and increment reg of 4 */ for (i = 0; i < count; i++, reg += sizeof(u32)) { - ret = qca8k_read_mii(priv, reg, val_buf + i); + ret = qca8k_read_mii(priv, reg, (u32 *)val_buf + i); if (ret < 0) return ret; } -- 2.43.0 qca8k_get_ethtool_stats_eth() points mib_eth_data at the caller's ethtool buffer, asks the switch for an autocast and waits 5 ms for the frames. On a timeout it returns with the request still published, and the caller falls back to register reads; ethtool_get_stats() then frees the buffer. The autocast handler runs from the conduit's receive path without the request's mutex, so a frame that arrives late still writes the requested port's counters through mib_eth_data->data into freed memory, and frames beyond the seven expected ones underflow port_parsed. ETHTOOL_GSTATS does not need CAP_NET_ADMIN. Protect the request with a spinlock that the handler takes, retire it on the way out, and let the handler retire it when the last frame completes it, so that frames arriving after that are dropped without touching data. An autocast frame carries no identity of the request it answers. Once a request has timed out and the next one is published, the switch may still be sending the earlier autocast, and its frames count for the new request. Wait for MIB_BUSY to clear before asking for the next autocast, as qca8k_mib_init() does after a flush, so that at most the frames already on their way to the host can be taken for the new request, and track the ports seen as a bitmap instead of a countdown, so that a second frame of a port cannot complete the request before the other ports have reported. The requested port's counters then come from the newest frame of that port, which is at most one autocast behind - the same as a register read racing the hardware. An LLM assistant (Claude, claude-fable-5-1) helped analyse the race and write the change and this description; I reviewed both. Built with W=1 on arm64; tested on a QCA8337 behind an IPQ5018 (GL.iNet GL-B3000) with tag_qca, with the request timeout forced to 0 to exercise the late frame path. Fixes: 5c957c7ca78c ("net: dsa: qca8k: add support for mib autocast in Ethernet packet") Signed-off-by: Stanislaw Pal --- drivers/net/dsa/qca/qca8k-8xxx.c | 47 ++++++++++++++++++++++++++---- drivers/net/dsa/qca/qca8k-common.c | 2 +- drivers/net/dsa/qca/qca8k.h | 6 ++-- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/drivers/net/dsa/qca/qca8k-8xxx.c b/drivers/net/dsa/qca/qca8k-8xxx.c index 3c8044a4d1..4de7da9c8e 100644 --- a/drivers/net/dsa/qca/qca8k-8xxx.c +++ b/drivers/net/dsa/qca/qca8k-8xxx.c @@ -1676,10 +1676,18 @@ static void qca8k_mib_autocast_handler(struct dsa_switch *ds, struct sk_buff *sk mib_ethhdr = (struct mib_ethhdr *)skb_mac_header(skb); mib_eth_data = &priv->mib_eth_data; + spin_lock(&mib_eth_data->lock); + + /* A late frame of a request that has already returned */ + if (!mib_eth_data->data) + goto unlock; + /* The switch autocast every port. Ignore other packet and * parse only the requested one. */ port = FIELD_GET(QCA_HDR_RECV_SOURCE_PORT, ntohs(mib_ethhdr->hdr)); + if (port >= QCA8K_NUM_PORTS) + goto unlock; if (port != mib_eth_data->req_port) goto exit; @@ -1704,9 +1712,17 @@ static void qca8k_mib_autocast_handler(struct dsa_switch *ds, struct sk_buff *sk } exit: - /* Complete on receiving all the mib packet */ - if (refcount_dec_and_test(&mib_eth_data->port_parsed)) + /* Complete once a frame of every port has arrived. A second frame + * of a port, left over from an earlier autocast, cannot end the + * request early. + */ + mib_eth_data->ports_seen |= BIT(port); + if (mib_eth_data->ports_seen == GENMASK(QCA8K_NUM_PORTS - 1, 0)) { + mib_eth_data->data = NULL; complete(&mib_eth_data->rw_done); + } +unlock: + spin_unlock(&mib_eth_data->lock); } static int @@ -1721,13 +1737,25 @@ qca8k_get_ethtool_stats_eth(struct dsa_switch *ds, int port, u64 *data) mutex_lock(&mib_eth_data->mutex); + mutex_lock(&priv->reg_mutex); + + /* A request that timed out may have left the switch still sending + * its autocast. Let it finish before asking for another one, so + * that as few of its frames as possible are taken for ours. + */ + ret = qca8k_busy_wait(priv, QCA8K_REG_MIB, QCA8K_MIB_BUSY); + if (ret) { + mutex_unlock(&priv->reg_mutex); + goto out; + } + reinit_completion(&mib_eth_data->rw_done); + spin_lock_bh(&mib_eth_data->lock); mib_eth_data->req_port = dp->index; mib_eth_data->data = data; - refcount_set(&mib_eth_data->port_parsed, QCA8K_NUM_PORTS); - - mutex_lock(&priv->reg_mutex); + mib_eth_data->ports_seen = 0; + spin_unlock_bh(&mib_eth_data->lock); /* Send mib autocast request */ ret = regmap_update_bits(priv->regmap, QCA8K_REG_MIB, @@ -1743,6 +1771,14 @@ qca8k_get_ethtool_stats_eth(struct dsa_switch *ds, int port, u64 *data) ret = wait_for_completion_timeout(&mib_eth_data->rw_done, QCA8K_ETHERNET_TIMEOUT); exit: + /* On a timeout the frames can still arrive after the caller has + * freed data: retire the request so that the handler drops them. + */ + spin_lock_bh(&mib_eth_data->lock); + mib_eth_data->data = NULL; + spin_unlock_bh(&mib_eth_data->lock); + +out: mutex_unlock(&mib_eth_data->mutex); return ret; @@ -2120,6 +2156,7 @@ qca8k_sw_probe(struct mdio_device *mdiodev) init_completion(&priv->mgmt_eth_data.rw_done); mutex_init(&priv->mib_eth_data.mutex); + spin_lock_init(&priv->mib_eth_data.lock); init_completion(&priv->mib_eth_data.rw_done); priv->ds->dev = &mdiodev->dev; diff --git a/drivers/net/dsa/qca/qca8k-common.c b/drivers/net/dsa/qca/qca8k-common.c index ab978cc836..f1e7e86f47 100644 --- a/drivers/net/dsa/qca/qca8k-common.c +++ b/drivers/net/dsa/qca/qca8k-common.c @@ -101,7 +101,7 @@ const struct regmap_access_table qca8k_readable_table = { .n_yes_ranges = ARRAY_SIZE(qca8k_readable_ranges), }; -static int qca8k_busy_wait(struct qca8k_priv *priv, u32 reg, u32 mask) +int qca8k_busy_wait(struct qca8k_priv *priv, u32 reg, u32 mask) { u32 val; diff --git a/drivers/net/dsa/qca/qca8k.h b/drivers/net/dsa/qca/qca8k.h index 9563388930..79754755f1 100644 --- a/drivers/net/dsa/qca/qca8k.h +++ b/drivers/net/dsa/qca/qca8k.h @@ -402,9 +402,10 @@ struct qca8k_mgmt_eth_data { struct qca8k_mib_eth_data { struct completion rw_done; struct mutex mutex; /* Process one command at time */ - refcount_t port_parsed; /* Counter to track parsed port */ + spinlock_t lock; /* Protects the request against the rx handler */ + unsigned long ports_seen; /* Ports whose frame arrived, under lock */ u8 req_port; - u64 *data; /* pointer to ethtool data */ + u64 *data; /* pointer to ethtool data, NULL with no request pending */ }; struct qca8k_ports_config { @@ -501,6 +502,7 @@ static inline u32 qca8k_port_to_phy(int port) /* Common setup function */ extern const struct qca8k_mib_desc ar8327_mib[]; extern const struct regmap_access_table qca8k_readable_table; +int qca8k_busy_wait(struct qca8k_priv *priv, u32 reg, u32 mask); int qca8k_mib_init(struct qca8k_priv *priv); void qca8k_port_set_status(struct qca8k_priv *priv, int port, int enable); int qca8k_read_switch_id(struct qca8k_priv *priv); -- 2.43.0 qca_tag_rcv() makes only the 2 byte tag linear before it hands a MIB frame to qca8k_mib_autocast_handler(). The handler then reads the counters that follow the Ethernet header, up to 164 bytes on QCA833x, straight from skb->data, without checking the frame length. A short or non-linear frame makes it read past the packet data. Pull the counters into the linear area before reading them. A frame that is too short is left uncounted, so the request times out and the caller reads the counters from the registers instead. An LLM assistant (Claude, claude-fable-5-1) helped write the change and this description; I reviewed both and built them with W=1 on arm64. Fixes: 5c957c7ca78c ("net: dsa: qca8k: add support for mib autocast in Ethernet packet") Signed-off-by: Stanislaw Pal --- drivers/net/dsa/qca/qca8k-8xxx.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/net/dsa/qca/qca8k-8xxx.c b/drivers/net/dsa/qca/qca8k-8xxx.c index 4de7da9c8e..76c0f7c97b 100644 --- a/drivers/net/dsa/qca/qca8k-8xxx.c +++ b/drivers/net/dsa/qca/qca8k-8xxx.c @@ -1669,6 +1669,7 @@ static void qca8k_mib_autocast_handler(struct dsa_switch *ds, struct sk_buff *sk struct qca8k_priv *priv = ds->priv; const struct qca8k_mib_desc *mib; struct mib_ethhdr *mib_ethhdr; + unsigned int len = 0; __le32 *data2; u8 port; int i; @@ -1691,6 +1692,16 @@ static void qca8k_mib_autocast_handler(struct dsa_switch *ds, struct sk_buff *sk if (port != mib_eth_data->req_port) goto exit; + /* Past the first 3, the counters follow the header. Leave a short + * frame uncounted, so that the request times out and the caller + * reads the registers instead. + */ + for (i = 3; i < priv->info->mib_count; i++) + len += ar8327_mib[i].size * sizeof(u32); + if (!pskb_may_pull(skb, len)) + goto unlock; + + mib_ethhdr = (struct mib_ethhdr *)skb_mac_header(skb); data2 = (__le32 *)skb->data; for (i = 0; i < priv->info->mib_count; i++) { -- 2.43.0 qca8k_rw_reg_ack_handler() answers a management read by copying the data that follows the Ethernet header into mgmt_eth_data->data. It trusts the frame for both the length and the layout: the length field of the frame allows up to 32 bytes while the buffer holds 16, so a frame announcing more than three registers writes past the buffer into the fields that follow it in qca8k_priv; and qca_tag_rcv() made only the 2 byte tag linear, so the copy reads skb->data without checking that the frame has the bytes it announces. The handler runs for every ack frame, whatever its sequence number. Size the buffer to what a management frame can carry, and pull the announced data into the linear area before reading it. The pull can move the head, so take the Ethernet header again after it. A frame that is too short is left unacknowledged, so the read fails and the caller falls back to MDIO. An LLM assistant (Claude, claude-fable-5-1 and claude-opus-5-5) helped write the change and this description; I reviewed both and built them with W=1 on arm64. The management path was exercised on a QCA8337 behind an IPQ5018 (GL.iNet GL-B3000) with tag_qca. Fixes: 90386223f44e ("net: dsa: qca8k: add support for larger read/write size with mgmt Ethernet") Signed-off-by: Stanislaw Pal --- drivers/net/dsa/qca/qca8k-8xxx.c | 24 +++++++++++++++++++----- drivers/net/dsa/qca/qca8k.h | 3 ++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/drivers/net/dsa/qca/qca8k-8xxx.c b/drivers/net/dsa/qca/qca8k-8xxx.c index 76c0f7c97b..9a7e90f48d 100644 --- a/drivers/net/dsa/qca/qca8k-8xxx.c +++ b/drivers/net/dsa/qca/qca8k-8xxx.c @@ -170,6 +170,7 @@ static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb) struct qca8k_mgmt_eth_data *mgmt_eth_data; struct qca8k_priv *priv = ds->priv; struct qca_mgmt_ethhdr *mgmt_ethhdr; + int data_len = 0; u32 command; u8 len, cmd; int i; @@ -190,6 +191,20 @@ static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb) /* We can ignore odd value, we always round up them in the alloc function. */ len *= sizeof(u16); + if (cmd == MDIO_READ && len > QCA_HDR_MGMT_DATA1_LEN) + data_len = min_t(int, QCA_HDR_MGMT_DATA2_LEN, + len - QCA_HDR_MGMT_DATA1_LEN); + + /* The tagger made only the tag linear. Leave a frame that is too + * short for the data it announces unacknowledged: the read fails + * and the caller falls back to MDIO. + */ + if (data_len && !pskb_may_pull(skb, data_len)) + goto complete; + + /* pskb_may_pull() may have moved the head */ + mgmt_ethhdr = (struct qca_mgmt_ethhdr *)skb_mac_header(skb); + /* Make sure the seq match the requested packet */ if (get_unaligned_le32(&mgmt_ethhdr->seq) == mgmt_eth_data->seq) mgmt_eth_data->ack = true; @@ -199,13 +214,11 @@ static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb) *val = get_unaligned_le32(&mgmt_ethhdr->mdio_data); - /* Get the rest of the 12 byte of data. - * The read/write function will extract the requested data. + /* Get the rest of the data. + * The read function extracts what it asked for. */ - if (len > QCA_HDR_MGMT_DATA1_LEN) { + if (data_len) { __le32 *data2 = (__le32 *)skb->data; - int data_len = min_t(int, QCA_HDR_MGMT_DATA2_LEN, - len - QCA_HDR_MGMT_DATA1_LEN); val++; @@ -217,6 +230,7 @@ static void qca8k_rw_reg_ack_handler(struct dsa_switch *ds, struct sk_buff *skb) } } +complete: complete(&mgmt_eth_data->rw_done); } diff --git a/drivers/net/dsa/qca/qca8k.h b/drivers/net/dsa/qca/qca8k.h index 79754755f1..b7aa0d832e 100644 --- a/drivers/net/dsa/qca/qca8k.h +++ b/drivers/net/dsa/qca/qca8k.h @@ -396,7 +396,8 @@ struct qca8k_mgmt_eth_data { struct mutex mutex; /* Enforce one mdio read/write at time */ bool ack; u32 seq; - u32 data[4]; + /* The data a management frame can carry */ + u32 data[(QCA_HDR_MGMT_DATA1_LEN + QCA_HDR_MGMT_DATA2_LEN) / sizeof(u32)]; }; struct qca8k_mib_eth_data { -- 2.43.0