diff mbox series

net: bridge: Slightly optimize br_stp_change_bridge_id()

Message ID 73f674075ae5279e3d2fa07d61a0a75bc50790f3.1644659879.git.christophe.jaillet@wanadoo.fr (mailing list archive)
State Rejected
Delegated to: Netdev Maintainers
Headers show
Series net: bridge: Slightly optimize br_stp_change_bridge_id() | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 5 this patch: 5
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang success Errors and warnings before: 18 this patch: 18
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 10 this patch: 10
netdev/checkpatch warning WARNING: networking block comments don't use an empty /* line, use /* Comment...
netdev/kdoc success Errors and warnings before: 3 this patch: 3
netdev/source_inline success Was 0 now: 0

Commit Message

Christophe JAILLET Feb. 12, 2022, 9:58 a.m. UTC
ether_addr_equal_64bits() can easy be used in place of ether_addr_equal()
here.
Padding in the 'net_bridge_port' structure is already there because it is a
huge structure and the required fields are not at the end.
'oldaddr' is local to the function. So add the required padding
explicitly and simplify its definition.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
This patch is more a POC for me.

I'm unsure that using ether_addr_equal_64bits() is really useful. The
speedup should be mostly un-noticeable.

To make sure that we have the required padding, we either need to waste
some space or rely on the fact that the address is embedded in a large
enough structure. (which is the case here)

So, it looks fragile to me and not future-proof.

Feed-back highly appreciated to see if such patches are welcome and if I
should spend some time on it.
---
 net/bridge/br_private.h |  5 +++++
 net/bridge/br_stp_if.c  | 12 +++++++-----
 2 files changed, 12 insertions(+), 5 deletions(-)

Comments

Nikolay Aleksandrov Feb. 13, 2022, 10:13 a.m. UTC | #1
On 12/02/2022 11:58, Christophe JAILLET wrote:
> ether_addr_equal_64bits() can easy be used in place of ether_addr_equal()
> here.
> Padding in the 'net_bridge_port' structure is already there because it is a
> huge structure and the required fields are not at the end.
> 'oldaddr' is local to the function. So add the required padding
> explicitly and simplify its definition.
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> This patch is more a POC for me.
> 
> I'm unsure that using ether_addr_equal_64bits() is really useful. The
> speedup should be mostly un-noticeable.
> 
> To make sure that we have the required padding, we either need to waste
> some space or rely on the fact that the address is embedded in a large
> enough structure. (which is the case here)
> 
> So, it looks fragile to me and not future-proof.
> 
> Feed-back highly appreciated to see if such patches are welcome and if I
> should spend some time on it.
> ---

This is slow path, more so as you've noted above the change is fragile and someone
can easily miss it, I appreciate the comments but for this case I'd prefer to
leave the code as-is to keep it obviously correct and avoid future problems.

Thanks,
 Nik

>  net/bridge/br_private.h |  5 +++++
>  net/bridge/br_stp_if.c  | 12 +++++++-----
>  2 files changed, 12 insertions(+), 5 deletions(-)
> 
> diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h
> index 2661dda1a92b..2f78090574c9 100644
> --- a/net/bridge/br_private.h
> +++ b/net/bridge/br_private.h
> @@ -363,6 +363,11 @@ struct net_bridge_port {
>  	unsigned char			config_pending;
>  	port_id				port_id;
>  	port_id				designated_port;
> +	/*
> +	 * designated_root and designated_bridge must NOT be at the end of the
> +	 * structure because ether_addr_equal_64bits() requires 2 bytes of
> +	 * padding.
> +	 */
>  	bridge_id			designated_root;
>  	bridge_id			designated_bridge;
>  	u32				path_cost;
> diff --git a/net/bridge/br_stp_if.c b/net/bridge/br_stp_if.c
> index 75204d36d7f9..1bf0aaf29e5e 100644
> --- a/net/bridge/br_stp_if.c
> +++ b/net/bridge/br_stp_if.c
> @@ -221,9 +221,11 @@ int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
>  /* called under bridge lock */
>  void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *addr)
>  {
> -	/* should be aligned on 2 bytes for ether_addr_equal() */
> -	unsigned short oldaddr_aligned[ETH_ALEN >> 1];
> -	unsigned char *oldaddr = (unsigned char *)oldaddr_aligned;
> +	/*
> +	 * should be aligned on 2 bytes and have 2 bytes of padding for
> +	 * ether_addr_equal_64bits()
> +	 */
> +	unsigned char oldaddr[ETH_ALEN + 2] __aligned(2);
>  	struct net_bridge_port *p;
>  	int wasroot;
>  
> @@ -236,10 +238,10 @@ void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *addr)
>  	eth_hw_addr_set(br->dev, addr);
>  
>  	list_for_each_entry(p, &br->port_list, list) {
> -		if (ether_addr_equal(p->designated_bridge.addr, oldaddr))
> +		if (ether_addr_equal_64bits(p->designated_bridge.addr, oldaddr))
>  			memcpy(p->designated_bridge.addr, addr, ETH_ALEN);
>  
> -		if (ether_addr_equal(p->designated_root.addr, oldaddr))
> +		if (ether_addr_equal_64bits(p->designated_root.addr, oldaddr))
>  			memcpy(p->designated_root.addr, addr, ETH_ALEN);
>  	}
>
diff mbox series

Patch

diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h
index 2661dda1a92b..2f78090574c9 100644
--- a/net/bridge/br_private.h
+++ b/net/bridge/br_private.h
@@ -363,6 +363,11 @@  struct net_bridge_port {
 	unsigned char			config_pending;
 	port_id				port_id;
 	port_id				designated_port;
+	/*
+	 * designated_root and designated_bridge must NOT be at the end of the
+	 * structure because ether_addr_equal_64bits() requires 2 bytes of
+	 * padding.
+	 */
 	bridge_id			designated_root;
 	bridge_id			designated_bridge;
 	u32				path_cost;
diff --git a/net/bridge/br_stp_if.c b/net/bridge/br_stp_if.c
index 75204d36d7f9..1bf0aaf29e5e 100644
--- a/net/bridge/br_stp_if.c
+++ b/net/bridge/br_stp_if.c
@@ -221,9 +221,11 @@  int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
 /* called under bridge lock */
 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *addr)
 {
-	/* should be aligned on 2 bytes for ether_addr_equal() */
-	unsigned short oldaddr_aligned[ETH_ALEN >> 1];
-	unsigned char *oldaddr = (unsigned char *)oldaddr_aligned;
+	/*
+	 * should be aligned on 2 bytes and have 2 bytes of padding for
+	 * ether_addr_equal_64bits()
+	 */
+	unsigned char oldaddr[ETH_ALEN + 2] __aligned(2);
 	struct net_bridge_port *p;
 	int wasroot;
 
@@ -236,10 +238,10 @@  void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *addr)
 	eth_hw_addr_set(br->dev, addr);
 
 	list_for_each_entry(p, &br->port_list, list) {
-		if (ether_addr_equal(p->designated_bridge.addr, oldaddr))
+		if (ether_addr_equal_64bits(p->designated_bridge.addr, oldaddr))
 			memcpy(p->designated_bridge.addr, addr, ETH_ALEN);
 
-		if (ether_addr_equal(p->designated_root.addr, oldaddr))
+		if (ether_addr_equal_64bits(p->designated_root.addr, oldaddr))
 			memcpy(p->designated_root.addr, addr, ETH_ALEN);
 	}