net: dsa: motorcomm: MIB fixup
David Yang
maintainerv4 cover net: dsa: motorcomm: MIB fixup
Fix (theoretical) MIB inconsistency and prepare for new devices. No new features are introduced.
v4:
- reuse port_to_priv helper
- re-enable delayed_work on port_setup
- disable preemption for u64_stats_set
v3: https://lore.kernel.org/r/[email protected]
- fix build errors
- fix logic error on MIB buffer creation
v2: https://lore.kernel.org/r/[email protected]
- split patches
v1: https://lore.kernel.org/r/[email protected]
David Yang (7):
net: dsa: motorcomm: Hoist port_to_priv helper into chip.h
net: dsa: motorcomm: Rename MIB stuff
net: dsa: motorcomm: Split MIB buffers
net: dsa: motorcomm: Split MIB module
net: dsa: motorcomm: Use u64_stats_t for MIB stats
net: dsa: motorcomm: Fix MIB synchronization
net: dsa: motorcomm: Use safe 64-bit counter reader
drivers/net/dsa/motorcomm/Makefile | 1 + drivers/net/dsa/motorcomm/chip.c | 413 +++------------------------ drivers/net/dsa/motorcomm/chip.h | 125 +-------- drivers/net/dsa/motorcomm/leds.c | 14 +- drivers/net/dsa/motorcomm/mib.c | 431 +++++++++++++++++++++++++++++ drivers/net/dsa/motorcomm/mib.h | 167 +++++++++++ drivers/net/dsa/motorcomm/smi.c | 30 ++ drivers/net/dsa/motorcomm/smi.h | 2 + 8 files changed, 683 insertions(+), 500 deletions(-) create mode 100644 drivers/net/dsa/motorcomm/mib.c create mode 100644 drivers/net/dsa/motorcomm/mib.h
Signature · 1 line
2.53.0
David Yang
maintainerPATCH v4 1/7 net: dsa: motorcomm: Hoist port_to_priv helper into chip.h
yt921x_poll_mib() open-codes the same container_of_const() expression that leds.c defines as to_yt921x_priv(). Move the macro to chip.h as yt921x_port_to_priv() so both files use one definition.
--- drivers/net/dsa/motorcomm/chip.c | 3 +-- drivers/net/dsa/motorcomm/chip.h | 3 +++ drivers/net/dsa/motorcomm/leds.c | 14 ++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-)
drivers/net/dsa/motorcomm/chip.c +1 -2
@@ -608,9 +608,8 @@ static void yt921x_poll_mib(struct work_struct *work) { struct yt921x_port *pp = container_of_const(work, struct yt921x_port, mib_read.work); -struct yt921x_priv *priv = (void *)(pp - pp->index) - - offsetof(struct yt921x_priv , ports);unsigned long delay =YT921X_STATS_INTERVAL_JIFFIES ; +struct yt921x_priv *priv =yt921x_port_to_priv (pp);int port = pp->index;int res;
drivers/net/dsa/motorcomm/chip.h +3 -0
@@ -989,4 +989,7 @@ struct yt921x_priv { struct yt921x_acl_blk *acl_blks[YT921X_ACL_BLK_NUM]; }; +#define yt921x_port_to_priv (pp) \ + container_of_const((pp), struct yt921x_priv, ports[(pp)->index]) +#endif
drivers/net/dsa/motorcomm/leds.c +6 -8
@@ -12,8 +12,6 @@#define to_yt921x_led (led_cdev) \ container_of_const((led_cdev), struct yt921x_led, cdev)#define to_yt921x_port (led) ((led)->port) -#define to_yt921x_priv (pp) \ - container_of_const((pp), struct yt921x_priv, ports[(pp)->index])#define to_device (priv) ((priv)->ds.dev)static u32 yt921x_led_regaddr (struct yt921x_priv *priv,int port,int group)
@@ -321,7 +319,7 @@ yt921x_cled_brightness_set_blocking(struct led_classdev *led_cdev, {struct yt921x_led *led =to_yt921x_led (led_cdev);struct yt921x_port *pp =to_yt921x_port (led); -struct yt921x_priv *priv =to_yt921x_priv (pp); +struct yt921x_priv *priv =yt921x_port_to_priv (pp);int res;mutex_lock (&priv->reg_lock);
@@ -337,7 +335,7 @@ yt921x_cled_blink_set(struct led_classdev *led_cdev, unsigned long *delay_on, {struct yt921x_led *led =to_yt921x_led (led_cdev);struct yt921x_port *pp =to_yt921x_port (led); -struct yt921x_priv *priv =to_yt921x_priv (pp); +struct yt921x_priv *priv =yt921x_port_to_priv (pp);int res;mutex_lock (&priv->reg_lock);
@@ -353,7 +351,7 @@ yt921x_cled_hw_control_get_device(struct led_classdev *led_cdev) {struct yt921x_led *led =to_yt921x_led (led_cdev);struct yt921x_port *pp =to_yt921x_port (led); -struct yt921x_priv *priv =to_yt921x_priv (pp); +struct yt921x_priv *priv =yt921x_port_to_priv (pp);struct dsa_port *dp; dp =dsa_to_port (&priv->ds, pp->index);
@@ -374,7 +372,7 @@ yt921x_cled_hw_control_is_supported(struct led_classdev *led_cdev, {struct yt921x_led *led =to_yt921x_led (led_cdev);struct yt921x_port *pp =to_yt921x_port (led); -struct yt921x_priv *priv =to_yt921x_priv (pp); +struct yt921x_priv *priv =yt921x_port_to_priv (pp);if (yt921x_led_trigger_is_supported (priv, pp->index, led->group, flags))return 0 ;
@@ -386,7 +384,7 @@ yt921x_cled_hw_control_get(struct led_classdev *led_cdev, unsigned long *flagsp) {struct yt921x_led *led =to_yt921x_led (led_cdev);struct yt921x_port *pp =to_yt921x_port (led); -struct yt921x_priv *priv =to_yt921x_priv (pp); +struct yt921x_priv *priv =yt921x_port_to_priv (pp);int res;mutex_lock (&priv->reg_lock);
@@ -401,7 +399,7 @@ yt921x_cled_hw_control_set(struct led_classdev *led_cdev, unsigned long flags) {struct yt921x_led *led =to_yt921x_led (led_cdev);struct yt921x_port *pp =to_yt921x_port (led); -struct yt921x_priv *priv =to_yt921x_priv (pp); +struct yt921x_priv *priv =yt921x_port_to_priv (pp);int res;mutex_lock (&priv->reg_lock);
Signature · 1 line
2.53.0
Re: [PATCH net-next v4 1/7] net: dsa: motorcomm: Hoist port_to_priv helper into chip.h
Quoted 5 lines · David Yang
yt921x_poll_mib()open-codes the samecontainer_of_const()expression thatleds.cdefines asto_yt921x_priv(). Move the macro tochip.hasyt921x_port_to_priv()so both files use one definition.Signed-off-by David Yang <[email protected]>
Andrew
[email protected]
AI reviewRe: [PATCH net-next v4 1/7] net: dsa: motorcomm: Hoist port_to_priv helper into chip.h
In reply to David YangThank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The helper promoted into the driver-wide header, yt921x_port_to_priv(pp) in drivers/net/dsa/motorcomm/chip.h:992-993,…
--
net: dsa: motorcomm: Hoist port_to_priv helper into chip.h
The duplicated container_of_const() expression used to get from a struct yt921x_port back to the enclosing struct yt921x_priv is moved out of leds.c into chip.h as yt921x_port_to_priv(), and yt921x_poll_mib() in chip.c is converted to use it instead of its open-coded pointer arithmetic.
Quoted 20 lines
drivers/net/dsa/motorcomm/chip.c +1
-2@@ -608,9 +608,8 @@ static void yt921x_poll_mib(struct work_struct *work) { struct yt921x_port *pp = container_of_const(work, struct yt921x_port, mib_read.work); -struct yt921x_priv *priv = (void *)(pp - pp->index) - - offsetof(struct yt921x_priv , ports);unsigned long delay =YT921X_STATS_INTERVAL_JIFFIES ; +struct yt921x_priv *priv =yt921x_port_to_priv (pp);int port = pp->index;int res;drivers/net/dsa/motorcomm/chip.h +0
-0
drivers/net/dsa/motorcomm/chip.h struct yt921x_priv { · line 994
Quoted 6 lines
drivers/net/dsa/motorcomm/chip.h +3
-0@@ -989,4 +989,7 @@ struct yt921x_priv {struct yt921x_acl_blk *acl_blks[YT921X_ACL_BLK_NUM ]; }; +#define yt921x_port_to_priv (pp) \ + container_of_const((pp), struct yt921x_priv, ports[(pp)->index]) +
[Severity: Low]
This isn't a bug today, but now that this helper lives in chip.h and is visible to every file that includes it, is the double expansion of the macro argument worth avoiding?
The argument pp appears twice: once as the container_of_const() base pointer, and again inside the offsetof() member designator:
container_of_const((pp), struct yt921x_priv, ports[(pp)->index])
container_of_const() only caches the first occurrence in its internal __mptr, so the second (pp)->index is a separate textual evaluation of the caller's expression. A caller passing something with side effects or a function call, say:
priv = yt921x_port_to_priv(&other->ports[i++]); priv = yt921x_port_to_priv(get_port(ds, i));
would compile cleanly but compute the container address from a different port than the base pointer.
Would a static inline be preferable here?
static inline struct yt921x_priv *yt921x_port_to_priv(struct yt921x_port *pp)
That gets single evaluation and type checking. All six users (yt921x_poll_mib() in chip.c, and yt921x_cled_brightness_set_blocking(), yt921x_cled_blink_set(), yt921x_cled_hw_control_get_device(), yt921x_cled_hw_control_is_supported(), yt921x_cled_hw_control_get() and yt921x_cled_hw_control_set() in leds.c) assign the result to a plain struct yt921x_priv *, so the const-preserving behaviour of container_of_const() does not appear to be needed.
Failing that, could a short comment be added stating that the argument must be free of side effects?
drivers/net/dsa/motorcomm/chip.h struct yt921x_priv { · line 995
Quoted 1 line
drivers/net/dsa/motorcomm/chip.h +0
-0#endif
[ ... ]
The macro is still present verbatim at the end of the series, so nothing later in the series changes this.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921203040.1203911-1-mmyangfl%40gmail.com
David Yang
maintainerPATCH v4 2/7 net: dsa: motorcomm: Rename MIB stuff
In reply to David YangRename them in preparation of major MIB refactor.
--- drivers/net/dsa/motorcomm/chip.c | 34 ++++++++++++++++---------------- drivers/net/dsa/motorcomm/chip.h | 4 ++-- 2 files changed, 19 insertions(+), 19 deletions(-)
drivers/net/dsa/motorcomm/chip.c +17 -17
@@ -38,7 +38,7 @@ struct yt921x_mib_desc {#define MIB_DESC (_size, _offset, _name) \ {_size, _offset, _name} -/* Must agree with yt921x_mib +/* Must agree with yt921x_mib_stats * * Unstructured fields (name != NULL) will appear in get_ethtool_stats(), * structured go to their *_stats() methods, but we need their sizes and offsets
@@ -545,11 +545,11 @@ yt921x_mbus_ext_init(struct yt921x_priv *priv, struct device_node *mnp) }/* Read and handle overflow of 32bit MIBs. MIB buffer must be zeroed before. */ -static int yt921x_read_mib (struct yt921x_priv *priv,int port) +static int yt921x_mib_read (struct yt921x_priv *priv,int port) {struct yt921x_port *pp = &priv->ports[port];struct device *dev =to_device (priv); -struct yt921x_mib *mib = &pp->mib; +struct yt921x_mib_stats *mib = &pp->mib;int res =0 ;/* Reading of yt921x_port::mib is not protected by a lock and it's vain
@@ -604,7 +604,7 @@ static int yt921x_read_mib(struct yt921x_priv *priv, int port)return res; } -static void yt921x_poll_mib (struct work_struct *work) +static void yt921x_mib_poll (struct work_struct *work) { struct yt921x_port *pp = container_of_const(work, struct yt921x_port, mib_read.work);
@@ -614,7 +614,7 @@ static void yt921x_poll_mib(struct work_struct *work)int res;mutex_lock (&priv->reg_lock); - res =yt921x_read_mib (priv, port); + res =yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock);if (res) delay *=4 ;
@@ -642,11 +642,11 @@ yt921x_dsa_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data) {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib *mib = &pp->mib; +struct yt921x_mib_stats *mib = &pp->mib;size_t j;mutex_lock (&priv->reg_lock); -yt921x_read_mib (priv, port); +yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock); j =0 ;
@@ -684,10 +684,10 @@ yt921x_dsa_get_eth_mac_stats(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib *mib = &pp->mib; +struct yt921x_mib_stats *mib = &pp->mib;mutex_lock (&priv->reg_lock); -yt921x_read_mib (priv, port); +yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock); mac_stats->FramesTransmittedOK = pp->tx_frames;
@@ -720,10 +720,10 @@ yt921x_dsa_get_eth_ctrl_stats(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib *mib = &pp->mib; +struct yt921x_mib_stats *mib = &pp->mib;mutex_lock (&priv->reg_lock); -yt921x_read_mib (priv, port); +yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock); ctrl_stats->MACControlFramesTransmitted = mib->tx_pause;
@@ -749,10 +749,10 @@ yt921x_dsa_get_rmon_stats(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib *mib = &pp->mib; +struct yt921x_mib_stats *mib = &pp->mib;mutex_lock (&priv->reg_lock); -yt921x_read_mib (priv, port); +yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock); *ranges = yt921x_rmon_ranges;
@@ -785,7 +785,7 @@ yt921x_dsa_get_stats64(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib *mib = &pp->mib; +struct yt921x_mib_stats *mib = &pp->mib; stats->rx_length_errors = mib->rx_undersize_errors + mib->rx_fragment_errors;
@@ -821,10 +821,10 @@ yt921x_dsa_get_pause_stats(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib *mib = &pp->mib; +struct yt921x_mib_stats *mib = &pp->mib;mutex_lock (&priv->reg_lock); -yt921x_read_mib (priv, port); +yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock); pause_stats->tx_pause_frames = mib->tx_pause;
@@ -4748,7 +4748,7 @@ static int yt921x_mdio_probe(struct mdio_device *mdiodev)struct yt921x_port *pp = &priv->ports[i]; pp->index = i; -INIT_DELAYED_WORK (&pp->mib_read, yt921x_poll_mib); +INIT_DELAYED_WORK (&pp->mib_read, yt921x_mib_poll); } ds = &priv->ds;
drivers/net/dsa/motorcomm/chip.h +2 -2
@@ -861,7 +861,7 @@ enum yt921x_fdb_entry_status {#define yt921x_port_is_internal (port) ((port) < 8)#define yt921x_port_is_external (port) ((port) == 8 || (port) == 9) -struct yt921x_mib { +struct yt921x_mib_stats {u64 rx_broadcast;u64 rx_pause;u64 rx_multicast;
@@ -940,7 +940,7 @@ struct yt921x_port {bool isolated;struct delayed_work mib_read; -struct yt921x_mib mib; +struct yt921x_mib_stats mib;u64 rx_frames;u64 tx_frames;
Signature · 1 line
2.53.0
David Yang
maintainerPATCH v4 3/7 net: dsa: motorcomm: Split MIB buffers
In reply to David YangWith MIB improvement introduced later, struct yt921x_priv will be over 20k which is not very good for a single kmalloc(). Dynamically allocate the MIB structures to reduce the size of struct yt921x_priv, and do not waste memory on dummy ports.
--- drivers/net/dsa/motorcomm/chip.c | 115 ++++++++++++++++++++++++------- drivers/net/dsa/motorcomm/chip.h | 16 +++-- 2 files changed, 100 insertions(+), 31 deletions(-)
drivers/net/dsa/motorcomm/chip.c +89 -26
@@ -549,10 +549,13 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port) {struct yt921x_port *pp = &priv->ports[port];struct device *dev =to_device (priv); -struct yt921x_mib_stats *mib = &pp->mib; +struct yt921x_mib *pm = pp->mib; +struct yt921x_mib_stats *mib;int res =0 ; -/* Reading of yt921x_port::mib is not protected by a lock and it's vain + mib = &pm->stats; + +/* Reading of yt921x_mib_stats is not protected by a lock and it's vain * to keep its consistency, since we have to read registers one by one * and there is no way to make a snapshot of MIB stats. *
@@ -589,11 +592,11 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)WRITE_ONCE (*valp, val); } - pp->rx_frames = mib->rx_64byte + mib->rx_65_127byte + + pm->rx_frames = mib->rx_64byte + mib->rx_65_127byte + mib->rx_128_255byte + mib->rx_256_511byte + mib->rx_512_1023byte + mib->rx_1024_1518byte + mib->rx_jumbo; - pp->tx_frames = mib->tx_64byte + mib->tx_65_127byte + + pm->tx_frames = mib->tx_64byte + mib->tx_65_127byte + mib->tx_128_255byte + mib->tx_256_511byte + mib->tx_512_1023byte + mib->tx_1024_1518byte + mib->tx_jumbo;
@@ -606,8 +609,9 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)static void yt921x_mib_poll (struct work_struct *work) { - struct yt921x_port *pp = container_of_const(work, struct yt921x_port, - mib_read.work); + struct yt921x_mib *pm = container_of_const(work, struct yt921x_mib, + work.work); +struct yt921x_port *pp = pm->port;unsigned long delay =YT921X_STATS_INTERVAL_JIFFIES ;struct yt921x_priv *priv =yt921x_port_to_priv (pp);int port = pp->index;
@@ -619,7 +623,7 @@ static void yt921x_mib_poll(struct work_struct *work)if (res) delay *=4 ; -schedule_delayed_work (&pp->mib_read, delay); +schedule_delayed_work (&pm->work, delay); } static void
@@ -642,9 +646,14 @@ yt921x_dsa_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data) {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib_stats *mib = &pp->mib; +struct yt921x_mib *pm = pp->mib; +struct yt921x_mib_stats *mib;size_t j; +if (!pm) +return ; + mib = &pm->stats; +mutex_lock (&priv->reg_lock);yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock);
@@ -684,16 +693,21 @@ yt921x_dsa_get_eth_mac_stats(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib_stats *mib = &pp->mib; +struct yt921x_mib *pm = pp->mib; +struct yt921x_mib_stats *mib; + +if (!pm) +return ; + mib = &pm->stats;mutex_lock (&priv->reg_lock);yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock); - mac_stats->FramesTransmittedOK = pp->tx_frames; + mac_stats->FramesTransmittedOK = pm->tx_frames; mac_stats->SingleCollisionFrames = mib->tx_single_collisions; mac_stats->MultipleCollisionFrames = mib->tx_multiple_collisions; - mac_stats->FramesReceivedOK = pp->rx_frames; + mac_stats->FramesReceivedOK = pm->rx_frames; mac_stats->FrameCheckSequenceErrors = mib->rx_crc_errors; mac_stats->AlignmentErrors = mib->rx_alignment_errors; mac_stats->OctetsTransmittedOK = mib->tx_good_bytes;
@@ -720,7 +734,12 @@ yt921x_dsa_get_eth_ctrl_stats(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib_stats *mib = &pp->mib; +struct yt921x_mib *pm = pp->mib; +struct yt921x_mib_stats *mib; + +if (!pm) +return ; + mib = &pm->stats;mutex_lock (&priv->reg_lock);yt921x_mib_read (priv, port);
@@ -749,7 +768,12 @@ yt921x_dsa_get_rmon_stats(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib_stats *mib = &pp->mib; +struct yt921x_mib *pm = pp->mib; +struct yt921x_mib_stats *mib; + +if (!pm) +return ; + mib = &pm->stats;mutex_lock (&priv->reg_lock);yt921x_mib_read (priv, port);
@@ -785,7 +809,12 @@ yt921x_dsa_get_stats64(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib_stats *mib = &pp->mib; +struct yt921x_mib *pm = pp->mib; +struct yt921x_mib_stats *mib; + +if (!pm) +return ; + mib = &pm->stats; stats->rx_length_errors = mib->rx_undersize_errors + mib->rx_fragment_errors;
@@ -801,8 +830,8 @@ yt921x_dsa_get_stats64(struct dsa_switch *ds, int port,/* stats->tx_heartbeat_errors */ stats->tx_window_errors = mib->tx_late_collisions; - stats->rx_packets = pp->rx_frames; - stats->tx_packets = pp->tx_frames; + stats->rx_packets = pm->rx_frames; + stats->tx_packets = pm->tx_frames; stats->rx_bytes = mib->rx_good_bytes -ETH_FCS_LEN * stats->rx_packets; stats->tx_bytes = mib->tx_good_bytes -ETH_FCS_LEN * stats->tx_packets; stats->rx_errors = stats->rx_length_errors + stats->rx_over_errors +
@@ -821,7 +850,12 @@ yt921x_dsa_get_pause_stats(struct dsa_switch *ds, int port, {struct yt921x_priv *priv =to_yt921x_priv (ds);struct yt921x_port *pp = &priv->ports[port]; -struct yt921x_mib_stats *mib = &pp->mib; +struct yt921x_mib *pm = pp->mib; +struct yt921x_mib_stats *mib; + +if (!pm) +return ; + mib = &pm->stats;mutex_lock (&priv->reg_lock);yt921x_mib_read (priv, port);
@@ -3952,11 +3986,14 @@ yt921x_phylink_mac_link_down(struct phylink_config *config, unsigned int mode, {struct dsa_port *dp =dsa_phylink_to_port (config);struct yt921x_priv *priv =to_yt921x_priv (dp->ds); +struct yt921x_mib *pm;int port = dp->index;int res;/* No need to sync; port control block is hold until device remove */ -cancel_delayed_work (&priv->ports[port].mib_read); + pm = priv->ports[port].mib; +if (pm) +cancel_delayed_work (&pm->work);mutex_lock (&priv->reg_lock); res =yt921x_port_down (priv, port);
@@ -3975,6 +4012,7 @@ yt921x_phylink_mac_link_up(struct phylink_config *config, {struct dsa_port *dp =dsa_phylink_to_port (config);struct yt921x_priv *priv =to_yt921x_priv (dp->ds); +struct yt921x_mib *pm;int port = dp->index;int res;
@@ -3987,7 +4025,9 @@ yt921x_phylink_mac_link_up(struct phylink_config *config,dev_err (dp->ds->dev,"Failed to %s port %d: %i\n" ,"bring up" , port, res); -schedule_delayed_work (&priv->ports[port].mib_read,0 ); + pm = priv->ports[port].mib; +if (pm) +schedule_delayed_work (&pm->work,0 ); } static void
@@ -4107,11 +4147,40 @@ yt921x_dsa_get_tag_protocol(struct dsa_switch *ds, int port,return DSA_TAG_PROTO_YT921X ; } +static void yt921x_dsa_port_teardown (struct dsa_switch *ds,int port) +{ +struct yt921x_priv *priv =to_yt921x_priv (ds); +struct yt921x_port *pp = &priv->ports[port]; +struct yt921x_mib *pm = pp->mib; + +/* get_stats64() might not be synchronized with port_teardown(), hold +* the MIB block till the device teardown +*/ +if (pm) +disable_delayed_work_sync (&pm->work); +} +static int yt921x_dsa_port_setup (struct dsa_switch *ds,int port) {struct yt921x_priv *priv =to_yt921x_priv (ds); +struct yt921x_port *pp = &priv->ports[port]; +struct device *dev =to_device (priv); +struct yt921x_mib *pm = pp->mib;int res; +if (pm) { +enable_delayed_work (&pm->work); + }else if (BIT (port) & (priv->info->internal_mask | + priv->info->external_mask)) { + pm =devm_kzalloc (dev,sizeof (*pm),GFP_KERNEL ); +if (!pm) +return -ENOMEM ; + pp->mib = pm; + + pm->port = pp; +INIT_DELAYED_WORK (&pm->work, yt921x_mib_poll); + } +mutex_lock (&priv->reg_lock); res =yt921x_port_setup (priv, port);mutex_unlock (&priv->reg_lock);
@@ -4666,6 +4735,7 @@ static const struct dsa_switch_ops yt921x_dsa_switch_ops = {/* port */ .get_tag_protocol = yt921x_dsa_get_tag_protocol, .phylink_get_caps = yt921x_dsa_phylink_get_caps, + .port_teardown = yt921x_dsa_port_teardown, .port_setup = yt921x_dsa_port_setup,#if IS_ENABLED (CONFIG_DCB )/* dscp */
@@ -4695,12 +4765,6 @@ static void yt921x_mdio_remove(struct mdio_device *mdiodev)if (!priv)return ; -for (size_t i =ARRAY_SIZE (priv->ports); i-- >0 ; ) { -struct yt921x_port *pp = &priv->ports[i]; - -disable_delayed_work_sync (&pp->mib_read); - } -dsa_unregister_switch (&priv->ds);for (unsigned int i =0 ; i <ARRAY_SIZE (priv->acl_blks); i++) {
@@ -4748,7 +4812,6 @@ static int yt921x_mdio_probe(struct mdio_device *mdiodev)struct yt921x_port *pp = &priv->ports[i]; pp->index = i; -INIT_DELAYED_WORK (&pp->mib_read, yt921x_mib_poll); } ds = &priv->ds;
drivers/net/dsa/motorcomm/chip.h +11 -5
@@ -913,6 +913,15 @@ struct yt921x_mib_stats {u64 tx_oam; }; +struct yt921x_mib { +struct yt921x_port *port; + +struct delayed_work work; +struct yt921x_mib_stats stats; +u64 rx_frames; +u64 tx_frames; +}; +struct yt921x_acl_entry {u32 key[2 ];u32 mask[2 ];
@@ -939,11 +948,6 @@ struct yt921x_port {bool hairpin;bool isolated; -struct delayed_work mib_read; -struct yt921x_mib_stats mib; -u64 rx_frames; -u64 tx_frames; -#if IS_ENABLED (CONFIG_NET_DSA_YT921X_LEDS )unsigned char led_duty;unsigned short led_cycle;
@@ -953,6 +957,8 @@ struct yt921x_port {struct yt921x_led *leds[YT921X_LED_GROUP_NUM ];#endif + +struct yt921x_mib *mib; };struct yt921x_reg_ops {
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH net-next v4 3/7] net: dsa: motorcomm: Split MIB buffers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
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] Unbalanced enable_delayed_work() in yt921x_dsa_port_setup() (drivers/net/dsa/motorcomm/chip.c:4171-4172)…
- [Low] Commit message describes only a memory-layout optimisation, but the patch also reworks the MIB work lifecycle: it adds a new…
--
net: dsa: motorcomm: Split MIB buffers
The MIB statistics buffers and the polling work are moved out of struct yt921x_port into a separate struct yt921x_mib that is allocated per port with devm_kzalloc(). Allocation happens in the DSA .port_setup callback, and a new .port_teardown callback is added.
Quoted 4 lines
With MIB improvement introduced later, struct
yt921x_privwill be over 20k which is not very good for a singlekmalloc(). Dynamically allocate the MIB structures to reduce the size of structyt921x_priv, and do not waste memory on dummy ports.
[Severity: Low]
The changelog only mentions the memory layout change, but the patch also reworks the MIB work lifecycle. Could the description also cover that:
- a new .port_teardown op is added to yt921x_dsa_switch_ops,
- the unconditional per-port disable_delayed_work_sync() loop in yt921x_mdio_remove() is removed, so cancellation now depends on DSA calling yt921x_dsa_port_teardown(),
- enable_delayed_work() / disable_delayed_work_sync() become a counted pair across DSA port setup/teardown cycles,
- the six stats callbacks (yt921x_dsa_get_ethtool_stats, yt921x_dsa_get_eth_mac_stats, yt921x_dsa_get_eth_ctrl_stats, yt921x_dsa_get_rmon_stats, yt921x_dsa_get_stats64, yt921x_dsa_get_pause_stats) now return silently for any port outside info->internal_mask | info->external_mask?
Quoted 4 lines
drivers/net/dsa/motorcomm/chip.c +0
-0
[ ... ]
Quoted 31 lines
drivers/net/dsa/motorcomm/chip.c +22
-0@@ -4107,11 +4147,40 @@ yt921x_dsa_get_tag_protocol(struct dsa_switch *ds, int port,return DSA_TAG_PROTO_YT921X ; } +static void yt921x_dsa_port_teardown (struct dsa_switch *ds,int port) +{ +struct yt921x_priv *priv =to_yt921x_priv (ds); +struct yt921x_port *pp = &priv->ports[port]; +struct yt921x_mib *pm = pp->mib; + +/* get_stats64() might not be synchronized with port_teardown(), hold +* the MIB block till the device teardown +*/ +if (pm) +disable_delayed_work_sync (&pm->work); +} +static int yt921x_dsa_port_setup (struct dsa_switch *ds,int port) {struct yt921x_priv *priv =to_yt921x_priv (ds); +struct yt921x_port *pp = &priv->ports[port]; +struct device *dev =to_device (priv); +struct yt921x_mib *pm = pp->mib;int res; +if (pm) { +enable_delayed_work (&pm->work); + }else if (BIT (port) & (priv->info->internal_mask | + priv->info->external_mask)) { + pm =devm_kzalloc (dev,sizeof (*pm),GFP_KERNEL ); +if (!pm)
drivers/net/dsa/motorcomm/chip.c yt921x_dsa_get_tag_protocol(struct dsa_switch *ds, int port, · line 4186
Quoted 10 lines
drivers/net/dsa/motorcomm/chip.c +7
-0+return -ENOMEM ; + pp->mib = pm; + + pm->port = pp; +INIT_DELAYED_WORK (&pm->work, yt921x_mib_poll); + } +mutex_lock (&priv->reg_lock); res =yt921x_port_setup (priv, port);mutex_unlock (&priv->reg_lock);
[Severity: Medium]
Can this enable_delayed_work() run without a matching disable_delayed_work_sync() and trip the workqueue disable count underflow warning?
The first .port_setup for a port allocates pm, runs INIT_DELAYED_WORK() (disable count 0) and publishes pp->mib before the fallible yt921x_port_setup() call. If yt921x_port_setup() fails on a register access, .port_setup returns an error and DSA does not call .port_teardown for that port:
net/dsa/devlink.c:dsa_port_devlink_setup() {
if (ds->ops->port_setup) {
err = ds->ops->port_setup(ds, dp->index);
if (err)
return err;
}
}net/dsa/dsa.c:dsa_port_setup() {
err = dsa_port_devlink_setup(dp);
if (err)
return err;
}dp->setup stays false, and dsa_tree_setup_ports() then retries the same port:
net/dsa/dsa.c:dsa_tree_setup_ports() {
err = dsa_port_setup(dp);
if (err) {
err = dsa_port_setup_as_unused(dp);
}On the retry pp->mib is non-NULL, so enable_delayed_work() runs with the disable count still at 0:
kernel/workqueue.c:work_offqd_enable() {
if (likely(offqd->disable > 0))
offqd->disable--;
else
WARN_ONCE(true, "workqueue: work disable count underflowed\n");
}Would it be better to only call enable_delayed_work() when a previous teardown actually disabled the work, or to undo the enable/init state on the yt921x_port_setup() failure path?
Quoted 19 lines
drivers/net/dsa/motorcomm/chip.c +1
-6@@ -4666,6 +4735,7 @@ static const struct dsa_switch_ops yt921x_dsa_switch_ops = {/* port */ .get_tag_protocol = yt921x_dsa_get_tag_protocol, .phylink_get_caps = yt921x_dsa_phylink_get_caps, + .port_teardown = yt921x_dsa_port_teardown, .port_setup = yt921x_dsa_port_setup,#if IS_ENABLED (CONFIG_DCB )/* dscp */ @@ -4695,12 +4765,6 @@ static void yt921x_mdio_remove(struct mdio_device *mdiodev)if (!priv)return ; -for (size_t i =ARRAY_SIZE (priv->ports); i-- >0 ; ) { -struct yt921x_port *pp = &priv->ports[i]; - -disable_delayed_work_sync (&pp->mib_read); - } -dsa_unregister_switch (&priv->ds);
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921203040.1203911-1-mmyangfl%40gmail.com
David Yang
maintainerPATCH v4 4/7 net: dsa: motorcomm: Split MIB module
In reply to David Yangyt922x is going to use a different MIB scheme, split it to reduce the length of chip.c.
--- drivers/net/dsa/motorcomm/Makefile | 1 + drivers/net/dsa/motorcomm/chip.c | 397 +--------------------------- drivers/net/dsa/motorcomm/chip.h | 124 --------- drivers/net/dsa/motorcomm/mib.c | 407 +++++++++++++++++++++++++++++ drivers/net/dsa/motorcomm/mib.h | 159 +++++++++++ 5 files changed, 568 insertions(+), 520 deletions(-) create mode 100644 drivers/net/dsa/motorcomm/mib.c create mode 100644 drivers/net/dsa/motorcomm/mib.h
drivers/net/dsa/motorcomm/Makefile +1 -0
@@ -2,4 +2,5 @@ obj-$(CONFIG_NET_DSA_YT921X) += yt921x.o yt921x-objs := chip.o yt921x-$(CONFIG_NET_DSA_YT921X_LEDS) += leds.o +yt921x-objs += mib.o yt921x-objs += smi.o
drivers/net/dsa/motorcomm/chip.c +1 -396
417 lines not on this page · raw message
drivers/net/dsa/motorcomm/chip.h +0 -124
136 lines not on this page · raw message
drivers/net/dsa/motorcomm/mib.c new file +407 -0
407 lines not on this page · raw message
drivers/net/dsa/motorcomm/mib.h new file +159 -0
159 lines not on this page · raw message
Signature · 1 line
2.53.0
David Yang
maintainerPATCH v4 5/7 net: dsa: motorcomm: Use u64_stats_t for MIB stats
In reply to David Yang64-bit variables might not be atomic on 32-bit architectures, and could lead to load/store tearing.
This is a verbatim change that replace every u64 statistics into u64_stats_t. u64_stats_sync will be equipped in the next commit.
--- drivers/net/dsa/motorcomm/mib.c | 152 ++++++++++++++++---------------- drivers/net/dsa/motorcomm/mib.h | 104 +++++++++++----------- 2 files changed, 131 insertions(+), 125 deletions(-)
drivers/net/dsa/motorcomm/mib.c +78 -74
@@ -92,22 +92,16 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)struct device *dev =to_device (priv);struct yt921x_mib *pm = pp->mib;struct yt921x_mib_stats *mib; +u64 rx_frames; +u64 tx_frames;int res =0 ; mib = &pm->stats; -/* Reading of yt921x_mib_stats is not protected by a lock and it's vain -* to keep its consistency, since we have to read registers one by one -* and there is no way to make a snapshot of MIB stats. -* -* Writing (by this function only) is and should be protected by -* reg_lock. -*/ -for (size_t i =0 ; i <ARRAY_SIZE (yt921x_mib_descs); i++) {const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];u32 reg =YT921X_MIBn_DATA0 (port) + desc->offset; -u64 *valp = &((u64 *)mib)[i]; +u64_stats_t *valp = &((u64_stats_t *)mib)[i];u32 val0;u64 val;
@@ -116,7 +110,7 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)break ;if (desc->size <=1 ) { -u64 old_val = *valp; +u64 old_val =u64_stats_read (valp); val = (old_val & ~(u64 )U32_MAX ) | val0;if (val < old_val)
@@ -130,17 +124,25 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port) val = ((u64 )val1 <<32 ) | val0; } -WRITE_ONCE (*valp, val); +u64_stats_set (valp, val); } - pm->rx_frames = mib->rx_64byte + mib->rx_65_127byte + - mib->rx_128_255byte + mib->rx_256_511byte + - mib->rx_512_1023byte + mib->rx_1024_1518byte + - mib->rx_jumbo; - pm->tx_frames = mib->tx_64byte + mib->tx_65_127byte + - mib->tx_128_255byte + mib->tx_256_511byte + - mib->tx_512_1023byte + mib->tx_1024_1518byte + - mib->tx_jumbo; + rx_frames =u64_stats_read (&mib->rx_64byte) + +u64_stats_read (&mib->rx_65_127byte) + +u64_stats_read (&mib->rx_128_255byte) + +u64_stats_read (&mib->rx_256_511byte) + +u64_stats_read (&mib->rx_512_1023byte) + +u64_stats_read (&mib->rx_1024_1518byte) + +u64_stats_read (&mib->rx_jumbo); + tx_frames =u64_stats_read (&mib->tx_64byte) + +u64_stats_read (&mib->tx_65_127byte) + +u64_stats_read (&mib->tx_128_255byte) + +u64_stats_read (&mib->tx_256_511byte) + +u64_stats_read (&mib->tx_512_1023byte) + +u64_stats_read (&mib->tx_1024_1518byte) + +u64_stats_read (&mib->tx_jumbo); +u64_stats_set (&pm->rx_frames, rx_frames); +u64_stats_set (&pm->tx_frames, tx_frames);if (res) dev_err(dev, "Failed to %s port %d: %i\n", "read stats for",
@@ -206,7 +208,7 @@ yt921x_dsa_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)if (!desc->name)continue ; - data[j] = ((u64 *)mib)[i]; + data[j] =u64_stats_read (&((u64_stats_t *)mib)[i]); j++; } }
@@ -245,28 +247,28 @@ yt921x_dsa_get_eth_mac_stats(struct dsa_switch *ds, int port,yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock); - mac_stats->FramesTransmittedOK = pm->tx_frames; - mac_stats->SingleCollisionFrames = mib->tx_single_collisions; - mac_stats->MultipleCollisionFrames = mib->tx_multiple_collisions; - mac_stats->FramesReceivedOK = pm->rx_frames; - mac_stats->FrameCheckSequenceErrors = mib->rx_crc_errors; - mac_stats->AlignmentErrors = mib->rx_alignment_errors; - mac_stats->OctetsTransmittedOK = mib->tx_good_bytes; - mac_stats->FramesWithDeferredXmissions = mib->tx_deferred; - mac_stats->LateCollisions = mib->tx_late_collisions; - mac_stats->FramesAbortedDueToXSColls = mib->tx_aborted_errors; + mac_stats->FramesTransmittedOK =u64_stats_read (&pm->tx_frames); + mac_stats->SingleCollisionFrames =u64_stats_read (&mib->tx_single_collisions); + mac_stats->MultipleCollisionFrames =u64_stats_read (&mib->tx_multiple_collisions); + mac_stats->FramesReceivedOK =u64_stats_read (&pm->rx_frames); + mac_stats->FrameCheckSequenceErrors =u64_stats_read (&mib->rx_crc_errors); + mac_stats->AlignmentErrors =u64_stats_read (&mib->rx_alignment_errors); + mac_stats->OctetsTransmittedOK =u64_stats_read (&mib->tx_good_bytes); + mac_stats->FramesWithDeferredXmissions =u64_stats_read (&mib->tx_deferred); + mac_stats->LateCollisions =u64_stats_read (&mib->tx_late_collisions); + mac_stats->FramesAbortedDueToXSColls =u64_stats_read (&mib->tx_aborted_errors);/* mac_stats->FramesLostDueToIntMACXmitError */ /* mac_stats->CarrierSenseErrors */ - mac_stats->OctetsReceivedOK = mib->rx_good_bytes; + mac_stats->OctetsReceivedOK =u64_stats_read (&mib->rx_good_bytes);/* mac_stats->FramesLostDueToIntMACRcvError */ - mac_stats->MulticastFramesXmittedOK = mib->tx_multicast; - mac_stats->BroadcastFramesXmittedOK = mib->tx_broadcast; + mac_stats->MulticastFramesXmittedOK =u64_stats_read (&mib->tx_multicast); + mac_stats->BroadcastFramesXmittedOK =u64_stats_read (&mib->tx_broadcast);/* mac_stats->FramesWithExcessiveDeferral */ - mac_stats->MulticastFramesReceivedOK = mib->rx_multicast; - mac_stats->BroadcastFramesReceivedOK = mib->rx_broadcast; + mac_stats->MulticastFramesReceivedOK =u64_stats_read (&mib->rx_multicast); + mac_stats->BroadcastFramesReceivedOK =u64_stats_read (&mib->rx_broadcast);/* mac_stats->InRangeLengthErrors */ /* mac_stats->OutOfRangeLengthField */ - mac_stats->FrameTooLongErrors = mib->rx_oversize_errors; + mac_stats->FrameTooLongErrors =u64_stats_read (&mib->rx_oversize_errors); }void
@@ -286,8 +288,8 @@ yt921x_dsa_get_eth_ctrl_stats(struct dsa_switch *ds, int port,yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock); - ctrl_stats->MACControlFramesTransmitted = mib->tx_pause; - ctrl_stats->MACControlFramesReceived = mib->rx_pause; + ctrl_stats->MACControlFramesTransmitted =u64_stats_read (&mib->tx_pause); + ctrl_stats->MACControlFramesReceived =u64_stats_read (&mib->rx_pause);/* ctrl_stats->UnsupportedOpcodesReceived */ }
@@ -322,26 +324,26 @@ yt921x_dsa_get_rmon_stats(struct dsa_switch *ds, int port, *ranges = yt921x_rmon_ranges; - rmon_stats->undersize_pkts = mib->rx_undersize_errors; - rmon_stats->oversize_pkts = mib->rx_oversize_errors; - rmon_stats->fragments = mib->rx_alignment_errors; + rmon_stats->undersize_pkts =u64_stats_read (&mib->rx_undersize_errors); + rmon_stats->oversize_pkts =u64_stats_read (&mib->rx_oversize_errors); + rmon_stats->fragments =u64_stats_read (&mib->rx_alignment_errors);/* rmon_stats->jabbers */ - rmon_stats->hist[0 ] = mib->rx_64byte; - rmon_stats->hist[1 ] = mib->rx_65_127byte; - rmon_stats->hist[2 ] = mib->rx_128_255byte; - rmon_stats->hist[3 ] = mib->rx_256_511byte; - rmon_stats->hist[4 ] = mib->rx_512_1023byte; - rmon_stats->hist[5 ] = mib->rx_1024_1518byte; - rmon_stats->hist[6 ] = mib->rx_jumbo; - - rmon_stats->hist_tx[0 ] = mib->tx_64byte; - rmon_stats->hist_tx[1 ] = mib->tx_65_127byte; - rmon_stats->hist_tx[2 ] = mib->tx_128_255byte; - rmon_stats->hist_tx[3 ] = mib->tx_256_511byte; - rmon_stats->hist_tx[4 ] = mib->tx_512_1023byte; - rmon_stats->hist_tx[5 ] = mib->tx_1024_1518byte; - rmon_stats->hist_tx[6 ] = mib->tx_jumbo; + rmon_stats->hist[0 ] =u64_stats_read (&mib->rx_64byte); + rmon_stats->hist[1 ] =u64_stats_read (&mib->rx_65_127byte); + rmon_stats->hist[2 ] =u64_stats_read (&mib->rx_128_255byte); + rmon_stats->hist[3 ] =u64_stats_read (&mib->rx_256_511byte); + rmon_stats->hist[4 ] =u64_stats_read (&mib->rx_512_1023byte); + rmon_stats->hist[5 ] =u64_stats_read (&mib->rx_1024_1518byte); + rmon_stats->hist[6 ] =u64_stats_read (&mib->rx_jumbo); + + rmon_stats->hist_tx[0 ] =u64_stats_read (&mib->tx_64byte); + rmon_stats->hist_tx[1 ] =u64_stats_read (&mib->tx_65_127byte); + rmon_stats->hist_tx[2 ] =u64_stats_read (&mib->tx_128_255byte); + rmon_stats->hist_tx[3 ] =u64_stats_read (&mib->tx_256_511byte); + rmon_stats->hist_tx[4 ] =u64_stats_read (&mib->tx_512_1023byte); + rmon_stats->hist_tx[5 ] =u64_stats_read (&mib->tx_1024_1518byte); + rmon_stats->hist_tx[6 ] =u64_stats_read (&mib->tx_jumbo); }void
@@ -357,32 +359,34 @@ yt921x_dsa_get_stats64(struct dsa_switch *ds, int port,return ; mib = &pm->stats; - stats->rx_length_errors = mib->rx_undersize_errors + - mib->rx_fragment_errors; - stats->rx_over_errors = mib->rx_oversize_errors; - stats->rx_crc_errors = mib->rx_crc_errors; - stats->rx_frame_errors = mib->rx_alignment_errors; + stats->rx_length_errors =u64_stats_read (&mib->rx_undersize_errors) + +u64_stats_read (&mib->rx_fragment_errors); + stats->rx_over_errors =u64_stats_read (&mib->rx_oversize_errors); + stats->rx_crc_errors =u64_stats_read (&mib->rx_crc_errors); + stats->rx_frame_errors =u64_stats_read (&mib->rx_alignment_errors);/* stats->rx_fifo_errors */ /* stats->rx_missed_errors */ - stats->tx_aborted_errors = mib->tx_aborted_errors; + stats->tx_aborted_errors =u64_stats_read (&mib->tx_aborted_errors);/* stats->tx_carrier_errors */ - stats->tx_fifo_errors = mib->tx_undersize_errors; + stats->tx_fifo_errors =u64_stats_read (&mib->tx_undersize_errors);/* stats->tx_heartbeat_errors */ - stats->tx_window_errors = mib->tx_late_collisions; - - stats->rx_packets = pm->rx_frames; - stats->tx_packets = pm->tx_frames; - stats->rx_bytes = mib->rx_good_bytes -ETH_FCS_LEN * stats->rx_packets; - stats->tx_bytes = mib->tx_good_bytes -ETH_FCS_LEN * stats->tx_packets; + stats->tx_window_errors =u64_stats_read (&mib->tx_late_collisions); + + stats->rx_packets =u64_stats_read (&pm->rx_frames); + stats->tx_packets =u64_stats_read (&pm->tx_frames); + stats->rx_bytes =u64_stats_read (&mib->rx_good_bytes) - +ETH_FCS_LEN * stats->rx_packets; + stats->tx_bytes =u64_stats_read (&mib->tx_good_bytes) - +ETH_FCS_LEN * stats->tx_packets; stats->rx_errors = stats->rx_length_errors + stats->rx_over_errors + stats->rx_crc_errors + stats->rx_frame_errors; stats->tx_errors = stats->tx_aborted_errors + stats->tx_fifo_errors + stats->tx_window_errors; - stats->rx_dropped = mib->rx_dropped; + stats->rx_dropped =u64_stats_read (&mib->rx_dropped);/* stats->tx_dropped */ - stats->multicast = mib->rx_multicast; - stats->collisions = mib->tx_collisions; + stats->multicast =u64_stats_read (&mib->rx_multicast); + stats->collisions =u64_stats_read (&mib->tx_collisions); }void
@@ -402,6 +406,6 @@ yt921x_dsa_get_pause_stats(struct dsa_switch *ds, int port,yt921x_mib_read (priv, port);mutex_unlock (&priv->reg_lock); - pause_stats->tx_pause_frames = mib->tx_pause; - pause_stats->rx_pause_frames = mib->rx_pause; + pause_stats->tx_pause_frames =u64_stats_read (&mib->tx_pause); + pause_stats->rx_pause_frames =u64_stats_read (&mib->rx_pause); }
drivers/net/dsa/motorcomm/mib.h +53 -51
122 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH net-next v4 5/7] net: dsa: motorcomm: Use u64_stats_t for MIB stats
Thank 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 (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] Interim 32-bit tearing window: this commit retypes all MIB counters to u64_stats_t and replaces the publication store…
Pre-existing issues:
- [Medium] yt921x_dsa_get_rmon_stats() reports rmon_stats->fragments from mib->rx_alignment_errors (register…
--
net: dsa: motorcomm: Use u64_stats_t for MIB stats
This converts every MIB counter in the motorcomm DSA driver from plain u64 to u64_stats_t, and updates all producers and consumers to use u64_stats_set() / u64_stats_read(). The commit message states that u64_stats_sync will be added in the following commit.
A couple of questions below.
Quoted 30 lines
drivers/net/dsa/motorcomm/mib.c +3
-9@@ -92,22 +92,16 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)struct device *dev =to_device (priv);struct yt921x_mib *pm = pp->mib;struct yt921x_mib_stats *mib; +u64 rx_frames; +u64 tx_frames;int res =0 ; mib = &pm->stats; -/* Reading of yt921x_mib_stats is not protected by a lock and it's vain -* to keep its consistency, since we have to read registers one by one -* and there is no way to make a snapshot of MIB stats. -* -* Writing (by this function only) is and should be protected by -* reg_lock. -*/ -for (size_t i =0 ; i <ARRAY_SIZE (yt921x_mib_descs); i++) {const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];u32 reg =YT921X_MIBn_DATA0 (port) + desc->offset; -u64 *valp = &((u64 *)mib)[i]; +u64_stats_t *valp = &((u64_stats_t *)mib)[i];u32 val0;u64 val;
[ ... ]
drivers/net/dsa/motorcomm/mib.c static int yt921x_mib_read(struct yt921x_priv *priv, int port) · line 128
Quoted 7 lines
drivers/net/dsa/motorcomm/mib.c +1
-1@@ -130,17 +124,25 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port) val = ((u64 )val1 <<32 ) | val0; } -WRITE_ONCE (*valp, val); +u64_stats_set (valp, val); }
[Severity: Medium]
Is this intermediate state a step backwards on 32-bit for one commit?
On BITS_PER_LONG == 32, u64_stats_t is a bare struct and the accessors are plain loads and stores:
include/linux/u64_stats_sync.h:
static inline void u64_stats_set(u64_stats_t *p, u64 val)
{
p->v = val;
}
static inline u64 u64_stats_read(const u64_stats_t *p)
{
return p->v;
}So this hunk replaces the only write-side annotation, WRITE_ONCE(*valp, val), with an unannotated 64-bit store, while struct yt921x_mib in this commit still has no struct u64_stats_sync to bracket it with. The readers in yt921x_dsa_get_stats64() run with no lock at all, and the other getters (yt921x_dsa_get_eth_mac_stats(), _eth_ctrl_stats(), _rmon_stats(), _pause_stats(), _ethtool_stats()) copy the counters out after mutex_unlock(&priv->reg_lock), so they can race with the yt921x_mib_poll() -> yt921x_mib_read() delayed work.
The commit message says "u64_stats_sync will be equipped in the next commit", and the follow-up commit does add struct u64_stats_sync syncp to struct yt921x_mib, wraps the publication in u64_stats_update_begin_irqsave() / u64_stats_update_end_irqrestore(), converts yt921x_dsa_get_stats64() to a u64_stats_fetch_begin() / u64_stats_fetch_retry() loop, and moves the remaining getters' reads back under priv->reg_lock, so the window only exists between the two commits.
Would it be preferable to squash the two commits, or at least keep WRITE_ONCE() here until syncp lands, so that no single commit in the series leaves the 32-bit tearing window wider than before?
[ ... ]
drivers/net/dsa/motorcomm/mib.c yt921x_dsa_get_rmon_stats(struct dsa_switch *ds, int port, · line 330
Quoted 11 lines
drivers/net/dsa/motorcomm/mib.c +3
-3@@ -322,26 +324,26 @@ yt921x_dsa_get_rmon_stats(struct dsa_switch *ds, int port, *ranges = yt921x_rmon_ranges; - rmon_stats->undersize_pkts = mib->rx_undersize_errors; - rmon_stats->oversize_pkts = mib->rx_oversize_errors; - rmon_stats->fragments = mib->rx_alignment_errors; + rmon_stats->undersize_pkts =u64_stats_read (&mib->rx_undersize_errors); + rmon_stats->oversize_pkts =u64_stats_read (&mib->rx_oversize_errors); + rmon_stats->fragments =u64_stats_read (&mib->rx_alignment_errors);/* rmon_stats->jabbers */
[Severity: Medium]
This isn't a bug introduced by this patch, it is pre-existing and only the right hand side was rewritten here, but should rmon_stats->fragments come from mib->rx_fragment_errors instead of mib->rx_alignment_errors?
The driver reads both registers into separate fields:
drivers/net/dsa/motorcomm/mib.c:yt921x_mib_descs[] = {
...
MIB_DESC(1, YT921X_MIB_DATA_RX_ALIGN_ERR, NULL),
MIB_DESC(1, YT921X_MIB_DATA_RX_UNDERSIZE_ERR, NULL),
MIB_DESC(1, YT921X_MIB_DATA_RX_FRAG_ERR, NULL),
...
}mib->rx_alignment_errors is already exported twice, as mac_stats->AlignmentErrors and as stats->rx_frame_errors, while mib->rx_fragment_errors is only folded into stats->rx_length_errors and is never reported through the RMON group. The mapping is unchanged at the end of this series as well.
[ ... ]
Quoted 14 lines
drivers/net/dsa/motorcomm/mib.c +5
-5@@ -357,32 +359,34 @@ yt921x_dsa_get_stats64(struct dsa_switch *ds, int port,return ; mib = &pm->stats; - stats->rx_length_errors = mib->rx_undersize_errors + - mib->rx_fragment_errors; - stats->rx_over_errors = mib->rx_oversize_errors; - stats->rx_crc_errors = mib->rx_crc_errors; - stats->rx_frame_errors = mib->rx_alignment_errors; + stats->rx_length_errors =u64_stats_read (&mib->rx_undersize_errors) + +u64_stats_read (&mib->rx_fragment_errors); + stats->rx_over_errors =u64_stats_read (&mib->rx_oversize_errors); + stats->rx_crc_errors =u64_stats_read (&mib->rx_crc_errors); + stats->rx_frame_errors =u64_stats_read (&mib->rx_alignment_errors);
[ ... ]
Quoted 10 lines
drivers/net/dsa/motorcomm/mib.c +6
-4- stats->rx_packets = pm->rx_frames; - stats->tx_packets = pm->tx_frames; - stats->rx_bytes = mib->rx_good_bytes -ETH_FCS_LEN * stats->rx_packets; - stats->tx_bytes = mib->tx_good_bytes -ETH_FCS_LEN * stats->tx_packets; + stats->rx_packets =u64_stats_read (&pm->rx_frames); + stats->tx_packets =u64_stats_read (&pm->tx_frames); + stats->rx_bytes =u64_stats_read (&mib->rx_good_bytes) - +ETH_FCS_LEN * stats->rx_packets; + stats->tx_bytes =u64_stats_read (&mib->tx_good_bytes) - +ETH_FCS_LEN * stats->tx_packets;
[ ... ]
Quoted 4 lines
drivers/net/dsa/motorcomm/mib.h +0
-0
[ ... ]
Quoted 9 lines
drivers/net/dsa/motorcomm/mib.h +2
-2@@ -128,8 +130,8 @@ struct yt921x_mib {struct delayed_work work;struct yt921x_mib_stats stats; -u64 rx_frames; -u64 tx_frames; +u64_stats_t rx_frames; +u64_stats_t tx_frames; };
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921203040.1203911-1-mmyangfl%40gmail.com
David Yang
maintainerPATCH v4 6/7 net: dsa: motorcomm: Fix MIB synchronization
In reply to David YangThe previous MIB sync was based on the false assumption that 64-bit variables are always atomic. To fix that:
* .get_stats64() uses u64_stats_sync, together with u64_stats_t;
* others take the free mutex lock if possible.
To minimize time of the write side of u64_stats_sync, a per-port buffer is used so that all the stat can be written back altogether after IO operations.
--- drivers/net/dsa/motorcomm/chip.c | 2 + drivers/net/dsa/motorcomm/mib.c | 93 ++++++++++++++++++++------------ drivers/net/dsa/motorcomm/mib.h | 6 +++ 3 files changed, 66 insertions(+), 35 deletions(-)
drivers/net/dsa/motorcomm/chip.c +2 -0
@@ -19,6 +19,7 @@#include <linux/of_mdio.h> #include <linux/of_net.h> #include <linux/sort.h> +#include <linux/u64_stats_sync.h> #include <net/dsa.h> #include <net/dscp.h>
@@ -3783,6 +3784,7 @@ static int yt921x_dsa_port_setup(struct dsa_switch *ds, int port) pp->mib = pm; pm->port = pp; +u64_stats_init (&pm->syncp);INIT_DELAYED_WORK (&pm->work, yt921x_mib_poll); }
drivers/net/dsa/motorcomm/mib.c +58 -35
212 lines not on this page · raw message
drivers/net/dsa/motorcomm/mib.h +6 -0
@@ -125,13 +125,19 @@ struct yt921x_mib_stats {u64_stats_t tx_oam; }; +#define YT921X_MIB_STAT_NUM (sizeof(struct yt921x_mib_stats) / sizeof(u64_stats_t)) +struct yt921x_mib {struct yt921x_port *port;struct delayed_work work; +struct u64_stats_sync syncp; +/* protected by syncp OR priv->reg_lock */ struct yt921x_mib_stats stats;u64_stats_t rx_frames;u64_stats_t tx_frames; +/* protected by priv->reg_lock */ +u64 data[YT921X_MIB_STAT_NUM ]; };void yt921x_mib_poll (struct work_struct *work);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH net-next v4 6/7] net: dsa: motorcomm: Fix MIB synchronization
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit is titled "net: dsa: motorcomm: Fix MIB synchronization" and fixes a real defect in already-merged code (the…
- [Low] The new lock annotation in struct yt921x_mib (mib.h) says stats/rx_frames/tx_frames are "protected by syncp OR priv->reg_lock".
Pre-existing issues:
- [Medium] The driver's standard statistics interfaces disagree about frame-too-long errors.
- [Medium] yt921x_mib_read() publishes a partially refreshed snapshot when a register read fails.
--
net: dsa: motorcomm: Fix MIB synchronization
This converts the yt921x MIB accounting to u64_stats_sync, with .get_stats64() using a seqcount retry loop and the other statistics callbacks holding priv->reg_lock across both the register read and the copy-out. A new per-port u64 buffer collects the values from the bus before they are published to the u64_stats_t counters in one short write-side section.
Quoted 2 lines
The previous MIB sync was based on the false assumption that 64-bit variables are always atomic. To fix that:
[Severity: Low]
The changelog describes a functional fix to already merged code, but the trailer block only carries Signed-off-by and Reviewed-by. Should there be a Fixes: tag here?
The torn-counter behaviour being fixed comes from the initial driver submission (drivers/net/dsa/motorcomm was added by 9c472a0d7c38), and the phrase "The previous MIB sync" does not name a commit, so the fix cannot be traced back or picked up for stable by tooling.
Related to that, the patch is not self-contained against the merged
driver: it relies on the u64_stats_t conversion from "net: dsa: motorcomm:
Use u64_stats_t for MIB stats" and on mib.c/mib.h created by "net: dsa:
motorcomm: Split MIB module" earlier in the series. Could the changelog mention that dependency?
Quoted 4 lines
drivers/net/dsa/motorcomm/mib.c +0
-0
[ ... ]
Quoted 27 lines
drivers/net/dsa/motorcomm/mib.c +3
-2@@ -92,6 +94,8 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)struct device *dev =to_device (priv);struct yt921x_mib *pm = pp->mib;struct yt921x_mib_stats *mib; +u64 *buf = pm->data; +unsigned long flags;u64 rx_frames;u64 tx_frames;int res =0 ;@@ -101,7 +105,6 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)for (size_t i =0 ; i <ARRAY_SIZE (yt921x_mib_descs); i++) {const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];u32 reg =YT921X_MIBn_DATA0 (port) + desc->offset; -u64_stats_t *valp = &((u64_stats_t *)mib)[i];u32 val0;u64 val;@@ -110,7 +113,7 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)break ;if (desc->size <=1 ) { -u64 old_val =u64_stats_read (valp); +u64 old_val = buf[i]; val = (old_val & ~(u64 )U32_MAX ) | val0;if (val < old_val)
drivers/net/dsa/motorcomm/mib.c static int yt921x_mib_read(struct yt921x_priv *priv, int port) · line 135
Quoted 10 lines
drivers/net/dsa/motorcomm/mib.c +4
-1@@ -124,9 +127,12 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port) val = ((u64 )val1 <<32 ) | val0; } -u64_stats_set (valp, val); + buf[i] = val; } + flags =u64_stats_update_begin_irqsave (&pm->syncp); +for (size_t i =0 ; i <ARRAY_SIZE (yt921x_mib_descs); i++) +u64_stats_set (&((u64_stats_t *)mib)[i], buf[i]);
[Severity: Medium]
This isn't a bug introduced by this patch, but should the publication be skipped when res is set?
The collection loop above leaves the loop on the first read error:
res = yt921x_reg_read(priv, reg, &val0); if (res) break;
so pm->data[] holds a mix of freshly read entries and entries left over from the previous poll. The block below then copies all of buf[] into pm->stats and recomputes pm->rx_frames/pm->tx_frames from that mixed snapshot without checking res.
If the RX size histogram registers are read but the following RX_GOOD_BYTES read fails, the published state has a nonzero rx_frames with a stale (or zero) rx_good_bytes, and yt921x_dsa_get_stats64() then computes:
stats->rx_bytes = u64_stats_read(&mib->rx_good_bytes) - ETH_FCS_LEN * stats->rx_packets;
which underflows and reports ~1.8e19 bytes to userspace until the next successful poll. The TX path does the same with tx_good_bytes. yt921x_mib_poll() only lengthens the poll interval after the snapshot has already been published.
The parent commit 84a3b42eff3c stored each value directly into pm->stats inside the same break-on-error loop and recomputed the frame totals unconditionally, so the mixed snapshot predates this change.
Quoted 11 lines
drivers/net/dsa/motorcomm/mib.c +1
-0rx_frames =u64_stats_read (&mib->rx_64byte) +u64_stats_read (&mib->rx_65_127byte) + u64_stats_read(&mib->rx_128_255byte) +@@ -143,6 +149,7 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)u64_stats_read (&mib->tx_jumbo);u64_stats_set (&pm->rx_frames, rx_frames);u64_stats_set (&pm->tx_frames, tx_frames); +u64_stats_update_end_irqrestore (&pm->syncp, flags);if (res) dev_err(dev, "Failed to %s port %d: %i\n", "read stats for",
[ ... ]
Quoted 7 lines
drivers/net/dsa/motorcomm/mib.c +2
-0@@ -269,6 +278,8 @@ yt921x_dsa_get_eth_mac_stats(struct dsa_switch *ds, int port,/* mac_stats->InRangeLengthErrors */ /* mac_stats->OutOfRangeLengthField */ mac_stats->FrameTooLongErrors =u64_stats_read (&mib->rx_oversize_errors); + +mutex_unlock (&priv->reg_lock); }
[ ... ]
Quoted 26 lines
drivers/net/dsa/motorcomm/mib.c +1
-16@@ -354,39 +369,45 @@ yt921x_dsa_get_stats64(struct dsa_switch *ds, int port,struct yt921x_port *pp = &priv->ports[port];struct yt921x_mib *pm = pp->mib;struct yt921x_mib_stats *mib; +unsigned int start;if (!pm)return ; mib = &pm->stats; - stats->rx_length_errors =u64_stats_read (&mib->rx_undersize_errors) + -u64_stats_read (&mib->rx_fragment_errors); - stats->rx_over_errors =u64_stats_read (&mib->rx_oversize_errors); - stats->rx_crc_errors =u64_stats_read (&mib->rx_crc_errors); - stats->rx_frame_errors =u64_stats_read (&mib->rx_alignment_errors); -/* stats->rx_fifo_errors */ -/* stats->rx_missed_errors */ - - stats->tx_aborted_errors =u64_stats_read (&mib->tx_aborted_errors); -/* stats->tx_carrier_errors */ - stats->tx_fifo_errors =u64_stats_read (&mib->tx_undersize_errors); -/* stats->tx_heartbeat_errors */ - stats->tx_window_errors =u64_stats_read (&mib->tx_late_collisions); - - stats->rx_packets =u64_stats_read (&pm->rx_frames); - stats->tx_packets =u64_stats_read (&pm->tx_frames);
drivers/net/dsa/motorcomm/mib.c yt921x_dsa_get_stats64(struct dsa_switch *ds, int port, · line 383
Quoted 10 lines
drivers/net/dsa/motorcomm/mib.c +6
-4- stats->rx_bytes =u64_stats_read (&mib->rx_good_bytes) - -ETH_FCS_LEN * stats->rx_packets; - stats->tx_bytes =u64_stats_read (&mib->tx_good_bytes) - -ETH_FCS_LEN * stats->tx_packets; + do { + start = u64_stats_fetch_begin(&pm->syncp); + + stats->rx_length_errors = u64_stats_read(&mib->rx_undersize_errors) + + u64_stats_read(&mib->rx_fragment_errors); + stats->rx_over_errors = u64_stats_read(&mib->rx_oversize_errors);
[Severity: Medium]
This is a pre-existing mapping and not something this patch changes, but do the two standard interfaces disagree about oversized frames?
yt921x_dsa_get_eth_mac_stats() reports the same hardware counter as the IEEE attribute:
mac_stats->FrameTooLongErrors = u64_stats_read(&mib->rx_oversize_errors);
while here rx_oversize_errors goes to rx_over_errors and is left out of rx_length_errors, which only sums rx_undersize_errors and rx_fragment_errors.
include/uapi/linux/if_link.h documents rx_length_errors as "aInRangeLengthErrors + aOutOfRangeLengthField + aFrameTooLongErrors" and rx_over_errors as a "Receiver FIFO overflow event counter". With the current mapping, oversized frames are missing from rx_length_errors and show up as receive overruns in ip -s link. The same assignment exists in the parent commit 84a3b42eff3c.
Quoted 22 lines
drivers/net/dsa/motorcomm/mib.c +22
-0+ stats->rx_crc_errors =u64_stats_read (&mib->rx_crc_errors); + stats->rx_frame_errors =u64_stats_read (&mib->rx_alignment_errors); +/* stats->rx_fifo_errors */ +/* stats->rx_missed_errors */ + + stats->tx_aborted_errors =u64_stats_read (&mib->tx_aborted_errors); +/* stats->tx_carrier_errors */ + stats->tx_fifo_errors =u64_stats_read (&mib->tx_undersize_errors); +/* stats->tx_heartbeat_errors */ + stats->tx_window_errors =u64_stats_read (&mib->tx_late_collisions); + + stats->rx_packets =u64_stats_read (&pm->rx_frames); + stats->tx_packets =u64_stats_read (&pm->tx_frames); + stats->rx_bytes =u64_stats_read (&mib->rx_good_bytes) - +ETH_FCS_LEN * stats->rx_packets; + stats->tx_bytes =u64_stats_read (&mib->tx_good_bytes) - +ETH_FCS_LEN * stats->tx_packets; + stats->rx_dropped =u64_stats_read (&mib->rx_dropped); +/* stats->tx_dropped */ + stats->multicast =u64_stats_read (&mib->rx_multicast); + stats->collisions =u64_stats_read (&mib->tx_collisions); + }while (u64_stats_fetch_retry (&pm->syncp, start));
[ ... ]
Quoted 6 lines
drivers/net/dsa/motorcomm/mib.h +0
-0@@ -125,13 +125,19 @@ struct yt921x_mib_stats {u64_stats_t tx_oam;
drivers/net/dsa/motorcomm/mib.h struct yt921x_mib_stats { · line 135
Quoted 10 lines
drivers/net/dsa/motorcomm/mib.h +4
-0}; +#define YT921X_MIB_STAT_NUM (sizeof(struct yt921x_mib_stats) / sizeof(u64_stats_t)) +struct yt921x_mib {struct yt921x_port *port;struct delayed_work work; +struct u64_stats_sync syncp; +/* protected by syncp OR priv->reg_lock */
[Severity: Low]
Is the "OR" here accurate for writers? It holds for readers, but struct u64_stats_sync provides no mutual exclusion on its own:
include/linux/u64_stats_sync.h:
static inline void u64_stats_init(struct u64_stats_sync *syncp) { }and u64_stats_update_begin_irqsave()/u64_stats_update_end_irqrestore() compile away on 64-bit, so a writer holding only syncp would be unsynchronized.
The single writer, yt921x_mib_read(), actually needs both: its callee yt921x_reg_read() has lockdep_assert_held_once(&priv->reg_lock), and the publication is additionally wrapped in syncp.
Would something like "writers: priv->reg_lock + syncp; readers:priv->reg_lock or syncp" describe the rule more precisely?
Quoted 6 lines
drivers/net/dsa/motorcomm/mib.h +2
-0struct yt921x_mib_stats stats;u64_stats_t rx_frames;u64_stats_t tx_frames; +/* protected by priv->reg_lock */ +u64 data[YT921X_MIB_STAT_NUM ]; };
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921203040.1203911-1-mmyangfl%40gmail.com
David Yang
maintainerPATCH v4 7/7 net: dsa: motorcomm: Use safe 64-bit counter reader
In reply to David Yang64-bit counter registers are read as two separate 32-bit transactions. If the low word wraps from about 0xffffffff to a small value while the high word increments between the two transactions, the recombined value becomes ((new_high << 32) | old_low), producing a spurious ~4 GiB jump.
Use a safe counter reader for this.
--- drivers/net/dsa/motorcomm/mib.c | 21 +++++++++------------ drivers/net/dsa/motorcomm/smi.c | 30 ++++++++++++++++++++++++++++++ drivers/net/dsa/motorcomm/smi.h | 2 ++ 3 files changed, 41 insertions(+), 12 deletions(-)
drivers/net/dsa/motorcomm/mib.c +9 -12
@@ -105,29 +105,26 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)for (size_t i =0 ; i <ARRAY_SIZE (yt921x_mib_descs); i++) {const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];u32 reg =YT921X_MIBn_DATA0 (port) + desc->offset; -u32 val0; -u64 val; - - res =yt921x_reg_read (priv, reg, &val0); -if (res) -break ;if (desc->size <=1 ) {u64 old_val = buf[i]; +u32 val0; +u64 val; + + res =yt921x_reg_read (priv, reg, &val0); +if (res) +break ; val = (old_val & ~(u64 )U32_MAX ) | val0;if (val < old_val) val +=1ull <<32 ; - }else { -u32 val1; - res =yt921x_reg_read (priv, reg +4 , &val1); + buf[i] = val; + }else { + res =yt921x_counter_read (priv, reg, &buf[i]);if (res)break ; - val = ((u64 )val1 <<32 ) | val0; } - - buf[i] = val; } flags =u64_stats_update_begin_irqsave (&pm->syncp);
drivers/net/dsa/motorcomm/smi.c +30 -0
@@ -63,6 +63,36 @@ int yt921x_reg_update_bits(struct yt921x_priv *priv, u32 reg, u32 mask, u32 val)return yt921x_reg_write (priv, reg, u); } +/* Reliably read a 64bit counter */ +int yt921x_counter_read (struct yt921x_priv *priv,u32 reg,u64 *valp) +{ +u32 old_lo; +int res; +u32 hi; +u32 lo; + + res =yt921x_reg_read (priv, reg, &old_lo); +if (res) +return res; + +for (int i =0 ; i <16 ; i++) { + res =yt921x_reg_read (priv, reg +4 , &hi); +if (res) +return res; + res =yt921x_reg_read (priv, reg, &lo); +if (res) +return res; + +if (lo >= old_lo) { + *valp = ((u64 )hi <<32 ) | lo; +return 0 ; + } + old_lo = lo; + } + +return -ETIMEDOUT ; +} +static int yt921x_regs_read (struct yt921x_priv *priv,u32 reg,u32 *vals,unsigned int num_regs)
drivers/net/dsa/motorcomm/smi.h +2 -0
@@ -35,6 +35,8 @@ yt921x_reg_toggle_bits(struct yt921x_priv *priv, u32 reg, u32 mask, bool set)return yt921x_reg_update_bits (priv, reg, mask, !set ?0 : mask); } +int yt921x_counter_read (struct yt921x_priv *priv,u32 reg,u64 *valp); +/* Some multi-word registers, like VLANn_CTRL, should be treated as a single * long register. More specifically, writes to parts of its words won't become * visible, until the last word is written.
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH net-next v4 7/7] net: dsa: motorcomm: Use safe 64-bit counter reader
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit fixes a real, user-visible statistics bug (spurious ~4 GiB jumps in the 64-bit MIB byte counters…
- [Low] The new exported helper yt921x_counter_read() (declared in smi.h:38) documents nothing beyond "/* Reliably read a 64bit counter…
Pre-existing issues:
- [Medium] yt921x_mib_read() publishes a mixed stats snapshot on register-read failure.
- [Medium] The software MIB shadow buffer pm->data[] is zeroed only once, at devm_kzalloc() time in yt921x_dsa_port_setup() (chip.c:3781), while…
--
net: dsa: motorcomm: Use safe 64-bit counter reader
This adds yt921x_counter_read() to smi.c and switches the 64-bit MIB descriptors in yt921x_mib_read() over to it, so the low word is re-read after the high word and retried until it no longer goes backwards. The previous code read low then high once and recombined them.
A few questions below.
Quoted 9 lines
net: dsa: motorcomm: Use safe 64-bit counter reader
64-bit counter registers are read as two separate 32-bit transactions. If the low word wraps from about
0xffffffffto a small value while the high word increments between the two transactions, the recombined value becomes ((new_high<< 32) |old_low), producing a spurious ~4 GiB jump.Signed-off-by David Yang <[email protected]>Reviewed-by Andrew Lunn <[email protected]>
[Severity: Low]
The changelog describes a user-visible defect: the 64-bit byte counters (rx_good_bytes, rx_bad_bytes, tx_good_bytes) reported through .get_stats64, .get_eth_mac_stats and ethtool -S can jump by about 4 GiB.
The unsafe two-transaction read being replaced here came in with 186623f4aa72 ("net: dsa: yt921x: Add support for Motorcomm YT921x"), which is in released kernels. At the baseline the pre-series form was:
drivers/net/dsa/motorcomm/chip.c:
res = yt921x_reg_read(priv, reg + 4, &val1); ... val = ((u64)val1 << 32) | val0;
Should this carry a Fixes: 186623f4aa72 tag so it gets picked up for stable? As written, the subject reads as a cleanup rather than a correctness fix. Other fixes in this driver already use the tag, for example cc2d273893db carries Fixes: 9af9a8168213.
Quoted 17 lines
drivers/net/dsa/motorcomm/mib.c +0
-6@@ -105,29 +105,26 @@ static int yt921x_mib_read(struct yt921x_priv *priv, int port)for (size_t i =0 ; i <ARRAY_SIZE (yt921x_mib_descs); i++) {const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];u32 reg =YT921X_MIBn_DATA0 (port) + desc->offset; -u32 val0; -u64 val; - - res =yt921x_reg_read (priv, reg, &val0); -if (res) -break ;if (desc->size <=1 ) {u64 old_val = buf[i];
drivers/net/dsa/motorcomm/mib.c static int yt921x_mib_read(struct yt921x_priv *priv, int port) · line 120
Quoted 10 lines
drivers/net/dsa/motorcomm/mib.c +6
-0+u32 val0; +u64 val; + + res =yt921x_reg_read (priv, reg, &val0); +if (res) +break ; val = (old_val & ~(u64 )U32_MAX ) | val0;if (val < old_val) val +=1ull <<32 ;
[Severity: Medium]
This isn't a bug introduced by this patch, but while the ~4 GiB jump class is being addressed, does the same artifact remain reachable here through the shadow buffer?
The wrap extension above assumes pm->data[] still mirrors the hardware counters. pm->data[] is zeroed exactly once, at devm_kzalloc() time in yt921x_dsa_port_setup(), but the hardware MIB block is cleared on every ds->setup, in yt921x_chip_setup():
/* Clear MIB */ ctrl = YT921X_MIB_CTRL_CLEAN | YT921X_MIB_CTRL_ALL_PORT; res = yt921x_reg_write(priv, YT921X_MIB_CTRL, ctrl);
ds->setup can run twice for one device. Unbinding another switch in the same tree goes through dsa_switch_remove() -> dsa_tree_teardown(dst), and a later dsa_switch_probe() -> dsa_tree_setup() calls yt921x_dsa_setup() again on the surviving chip. yt921x_dsa_port_setup() already anticipates that path:
if (pm) {
enable_delayed_work(&pm->work);
} else if (...) {
pm = devm_kzalloc(dev, sizeof(*pm), GFP_KERNEL);Nothing on that path re-zeroes pm->data[], which seems to contradict the "MIB buffer must be zeroed before" contract comment. On the first poll after re-setup, val0 is small while old_val is large, so
"if (val < old_val) val += 1ull << 32" credits a phantom 2^32 to every 32-bit MIB of every port, and the 64-bit counters read through yt921x_counter_read() go backwards. Would zeroing pm->data[] on the re-setup path close that?
Quoted 6 lines
drivers/net/dsa/motorcomm/mib.c +2
-3- }else { -u32 val1; - res =yt921x_reg_read (priv, reg +4 , &val1); + buf[i] = val; + }else {
drivers/net/dsa/motorcomm/mib.c static int yt921x_mib_read(struct yt921x_priv *priv, int port) · line 130
Quoted 10 lines
drivers/net/dsa/motorcomm/mib.c +1
-3+ res =yt921x_counter_read (priv, reg, &buf[i]);if (res)break ; - val = ((u64 )val1 <<32 ) | val0; } - - buf[i] = val; } flags =u64_stats_update_begin_irqsave (&pm->syncp);
[Severity: Medium]
This is a pre-existing issue, but the new helper interacts with it, so it may be worth a look: what is published when the loop breaks early?
On error the loop does break, so buf[i..N-1] keep their previous values. yt921x_counter_read() only assigns *valp in the success branch:
if (lo >= old_lo) {
*valp = ((u64)hi << 32) | lo;
return 0;
}The publication block right after the loop runs unconditionally, with no "if (res)" bail-out:
flags = u64_stats_update_begin_irqsave(&pm->syncp); for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) u64_stats_set(&((u64_stats_t *)mib)[i], buf[i]);
So a fresh rx frame-size count (which feeds pm->rx_frames) can be committed together with a stale rx_good_bytes if the failure lands on the rx_good_bytes descriptor. yt921x_dsa_get_stats64() then does:
stats->rx_bytes = u64_stats_read(&mib->rx_good_bytes) - ETH_FCS_LEN * stats->rx_packets;
with no underflow guard. On the first poll after probe, pm->data is still zeroed, so rx_bytes reported by ip -s link would be around 1.8e19. yt921x_reg_read() reaches __mdiobus_read() via yt921x_reg_mdio_read(), which can return a negative errno on a transient bus error, so the path looks reachable. Would skipping the publish loop when res is set avoid the mixed snapshot?
Quoted 11 lines
drivers/net/dsa/motorcomm/smi.c +3
-0@@ -63,6 +63,36 @@ int yt921x_reg_update_bits(struct yt921x_priv *priv, u32 reg, u32 mask, u32 val)return yt921x_reg_write (priv, reg, u); } +/* Reliably read a 64bit counter */ +int yt921x_counter_read (struct yt921x_priv *priv,u32 reg,u64 *valp) +{
[ ... ]
Quoted 2 lines
drivers/net/dsa/motorcomm/smi.c +2
-0+for (int i =0 ; i <16 ; i++) { + res =yt921x_reg_read (priv, reg +4 , &hi);
[ ... ]
drivers/net/dsa/motorcomm/smi.c int yt921x_reg_update_bits(struct yt921x_priv *priv, u32 reg, u32 mask, u32 val) · line 94
Quoted 2 lines
drivers/net/dsa/motorcomm/smi.c +2
-0+return -ETIMEDOUT ; +}
[Severity: Low]
This isn't a bug, but the helper's contract is only visible from its body. It hardcodes low word at reg, high word at reg + 4, exactly two 32-bit words, and a counter that is free running, monotonically non-decreasing and not cleared by reads. Could the comment spell those out, given the prototype is exported driver-wide in smi.h with no width parameter?
The call site in yt921x_mib_read() routes every descriptor with desc->size > 1 here without passing desc->size, so the descriptor table's declared width and the helper's fixed two-word layout are two independent sources of truth. Today the table only contains MIB_DESC(2, ...) entries, so nothing is misread, but a size == 3 descriptor would be silently truncated.
Also, where does the retry bound of 16 come from? Exhausting it returns -ETIMEDOUT, which yt921x_mib_read() treats as a hard I/O error (it breaks out of the descriptor loop) and yt921x_mib_poll() turns into a 4x poll backoff:
if (res) delay *= 4;
The resulting 12 s interval stays well under the ~100 s overflow bound named in the comment in mib.c, so this looks harmless, but a word on why 16 is enough would help.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921203040.1203911-1-mmyangfl%40gmail.com