Netdev List
 help / color / mirror / Atom feed
From: David Yang <mmyangfl@gmail.com>
To: netdev@vger.kernel.org
Cc: David Yang <mmyangfl@gmail.com>, Andrew Lunn <andrew@lunn.ch>,
	Vladimir Oltean <olteanv@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Subject: [PATCH net-next v3 2/5] net: dsa: motorcomm: Cache port isolation masks
Date: Tue,  1 Dec 2026 16:14:53 +0800	[thread overview]
Message-ID: <20261201081459.2104904-3-mmyangfl@gmail.com> (raw)
In-Reply-To: <20261201081459.2104904-1-mmyangfl@gmail.com>

For a following commit, an accurate snapshot of port isolation masks is
required. However, the bridge core invokes .port_pre_bridge_flags()
synchronously in an atomic context, and userspace already sees such
changes applied before .port_bridge_flags() is called, causing the stale
snapshot to be used before the deferred call.

Calculate first, so that part can be made atomic, then apply later.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/motorcomm/chip.c | 90 ++++++++++++++++++--------------
 drivers/net/dsa/motorcomm/chip.h |  4 ++
 2 files changed, 56 insertions(+), 38 deletions(-)

diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c
index 49c9055c7512..6c6a87207bd5 100644
--- a/drivers/net/dsa/motorcomm/chip.c
+++ b/drivers/net/dsa/motorcomm/chip.c
@@ -1256,12 +1256,14 @@ yt921x_dsa_port_vlan_add(struct dsa_switch *ds, int port,
 
 static int yt921x_userport_standalone(struct yt921x_priv *priv, int port)
 {
+	struct yt921x_port *pp = &priv->ports[port];
 	struct dsa_switch *ds = &priv->ds;
 	u32 mask;
 	u32 ctrl;
 	int res;
 
 	ctrl = ~dsa_cpu_ports(ds);
+	pp->isolation_mask = ctrl;
 	res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), ctrl);
 	if (res)
 		return res;
@@ -1300,35 +1302,22 @@ static int yt921x_userport_bridge(struct yt921x_priv *priv, int port)
 	return 0;
 }
 
-static int yt921x_isolate(struct yt921x_priv *priv, int port)
+static void yt921x_isolation_calc_standalone(struct yt921x_priv *priv, int port)
 {
-	u32 mask;
-	int res;
-
-	mask = BIT(port);
-	for (int i = 0; i < YT921X_PORT_NUM; i++) {
-		if (dsa_is_cpu_port(&priv->ds, i) || i == port)
-			continue;
-
-		res = yt921x_reg_set_bits(priv, YT921X_PORTn_ISOLATION(i),
-					  mask);
-		if (res)
-			return res;
-	}
-
-	return 0;
+	for (int i = 0; i < YT921X_PORT_NUM; i++)
+		priv->ports[i].isolation_mask |= BIT(port);
 }
 
 /* Make sure to include the CPU port in ports_mask, or your bridge will
  * not have it.
  */
-static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask)
+static void
+yt921x_isolation_calc_bridge(struct yt921x_priv *priv, u16 ports_mask)
 {
 	unsigned long targets_mask = ports_mask & ~dsa_cpu_ports(&priv->ds);
 	u32 isolated_mask;
 	u32 ctrl;
 	int port;
-	int res;
 
 	isolated_mask = 0;
 	for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) {
@@ -1353,11 +1342,21 @@ static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask)
 		else
 			ctrl &= ~BIT(port);
 
-		res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port),
-				       ctrl);
-		if (res)
-			return res;
+		pp->isolation_mask = ctrl;
 	}
+}
+
+static int yt921x_isolation_commit(struct yt921x_priv *priv, u16 ports_mask)
+{
+	int res;
+
+	for (int i = 0; i < YT921X_PORT_NUM; i++)
+		if (BIT(i) & ports_mask) {
+			res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(i),
+					       priv->ports[i].isolation_mask);
+			if (res)
+				return res;
+		}
 
 	return 0;
 }
@@ -1370,7 +1369,8 @@ static int yt921x_bridge_leave(struct yt921x_priv *priv, int port)
 	if (res)
 		return res;
 
-	res = yt921x_isolate(priv, port);
+	yt921x_isolation_calc_standalone(priv, port);
+	res = yt921x_isolation_commit(priv, ~0);
 	if (res)
 		return res;
 
@@ -1386,7 +1386,8 @@ yt921x_bridge_join(struct yt921x_priv *priv, int port, u16 ports_mask)
 	if (res)
 		return res;
 
-	res = yt921x_bridge(priv, ports_mask);
+	yt921x_isolation_calc_bridge(priv, ports_mask);
+	res = yt921x_isolation_commit(priv, ports_mask);
 	if (res)
 		return res;
 
@@ -1397,8 +1398,6 @@ static int
 yt921x_bridge_flags(struct yt921x_priv *priv, int port,
 		    struct switchdev_brport_flags flags)
 {
-	struct yt921x_port *pp = &priv->ports[port];
-	bool do_flush;
 	u32 mask;
 	int res;
 
@@ -1418,16 +1417,7 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port,
 
 	/* BR_BCAST_FLOOD: we can filter bcast, but cannot trap them */
 
-	do_flush = false;
-	if (flags.mask & BR_HAIRPIN_MODE) {
-		pp->hairpin = flags.val & BR_HAIRPIN_MODE;
-		do_flush = true;
-	}
-	if (flags.mask & BR_ISOLATED) {
-		pp->isolated = flags.val & BR_ISOLATED;
-		do_flush = true;
-	}
-	if (do_flush) {
+	if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) {
 		struct dsa_switch *ds = &priv->ds;
 		struct dsa_port *dp = dsa_to_port(ds, port);
 		struct net_device *bdev;
@@ -1437,8 +1427,7 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port,
 			u32 ports_mask;
 
 			ports_mask = dsa_bridge_ports(ds, bdev);
-			ports_mask |= dsa_cpu_ports(ds);
-			res = yt921x_bridge(priv, ports_mask);
+			res = yt921x_isolation_commit(priv, ports_mask);
 			if (res)
 				return res;
 		}
@@ -1452,9 +1441,32 @@ yt921x_dsa_port_pre_bridge_flags(struct dsa_switch *ds, int port,
 				 struct switchdev_brport_flags flags,
 				 struct netlink_ext_ack *extack)
 {
+	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
+	struct yt921x_port *pp = &priv->ports[port];
+
 	if (flags.mask & ~(BR_HAIRPIN_MODE | BR_LEARNING | BR_FLOOD |
 			   BR_MCAST_FLOOD | BR_ISOLATED))
 		return -EINVAL;
+
+	if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) {
+		struct dsa_port *dp = dsa_to_port(ds, port);
+		struct net_device *bdev;
+
+		if (flags.mask & BR_HAIRPIN_MODE)
+			pp->hairpin = flags.val & BR_HAIRPIN_MODE;
+		if (flags.mask & BR_ISOLATED)
+			pp->isolated = flags.val & BR_ISOLATED;
+
+		bdev = dsa_port_bridge_dev_get(dp);
+		if (bdev) {
+			u32 ports_mask;
+
+			ports_mask = dsa_bridge_ports(ds, bdev);
+			ports_mask |= dsa_cpu_ports(ds);
+			yt921x_isolation_calc_bridge(priv, ports_mask);
+		}
+	}
+
 	return 0;
 }
 
@@ -2046,6 +2058,7 @@ yt921x_dsa_phylink_get_caps(struct dsa_switch *ds, int port,
 
 static int yt921x_port_setup(struct yt921x_priv *priv, int port)
 {
+	struct yt921x_port *pp = &priv->ports[port];
 	struct dsa_switch *ds = &priv->ds;
 	u32 ctrl;
 	int res;
@@ -2067,6 +2080,7 @@ static int yt921x_port_setup(struct yt921x_priv *priv, int port)
 		 * without tag).
 		 */
 		ctrl = ~(u32)0;
+		pp->isolation_mask = ctrl;
 		res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port),
 				       ctrl);
 		if (res)
diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h
index d5ed580f8a78..203371888c45 100644
--- a/drivers/net/dsa/motorcomm/chip.h
+++ b/drivers/net/dsa/motorcomm/chip.h
@@ -457,11 +457,15 @@ struct yt921x_port {
 	bool serdes:1;
 	/* Link from in-band status (PHYLINK_PCS_NEG_INBAND) */
 	bool inband:1;
+	bool:0;
+
 	/* BR_HAIRPIN_MODE */
 	bool hairpin:1;
 	/* BR_ISOLATED */
 	bool isolated:1;
 
+	u16 isolation_mask;
+
 	unsigned short acl_cnt;
 
 #if IS_ENABLED(CONFIG_NET_DSA_YT921X_LEDS)
-- 
2.53.0


  parent reply	other threads:[~2026-12-01  8:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  8:14 [PATCH net-next v3 0/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
2026-12-01  8:14 ` [PATCH net-next v3 1/5] net: dsa: motorcomm: Remove local cpu_ports_mask David Yang
2026-12-01  8:14 ` David Yang [this message]
2026-12-01  8:14 ` [PATCH net-next v3 3/5] net: dsa: motorcomm: Reject isolated redirect targets David Yang
2026-12-01  8:14 ` [PATCH net-next v3 4/5] net: dsa: motorcomm: Reject bridge flags that break redirect rules David Yang
2026-12-01  8:14 ` [PATCH net-next v3 5/5] net: dsa: motorcomm: Recompute redirect rules on bridge changes David Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201081459.2104904-3-mmyangfl@gmail.com \
    --to=mmyangfl@gmail.com \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox