netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next] ax25: exit linked-list searches earlier
@ 2023-04-07 14:20 Peter Lafreniere
  2023-04-07 15:14 ` Dan Carpenter
  2023-04-14 14:33 ` [PATCH v2 " Peter Lafreniere
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Lafreniere @ 2023-04-07 14:20 UTC (permalink / raw)
  To: linux-hams; +Cc: Peter Lafreniere, netdev

There's no need to loop until the end of the list if we have a result.

Device callsigns are unique, so there can only be one dev returned from
ax25_addr_ax25dev(). If not, there would be inconsistencies based on
order of insertion, and refcount leaks.

Same reasoning for ax25_get_route() as above.

Signed-off-by: Peter Lafreniere <peter@n8pjl.ca>
---
 net/ax25/ax25_dev.c   | 4 +++-
 net/ax25/ax25_route.c | 3 +++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/net/ax25/ax25_dev.c b/net/ax25/ax25_dev.c
index c5462486dbca..8186faea6b0d 100644
--- a/net/ax25/ax25_dev.c
+++ b/net/ax25/ax25_dev.c
@@ -34,11 +34,13 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
 	ax25_dev *ax25_dev, *res = NULL;
 
 	spin_lock_bh(&ax25_dev_lock);
-	for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
+	for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next) {
 		if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
 			res = ax25_dev;
 			ax25_dev_hold(ax25_dev);
+			break;
 		}
+	}
 	spin_unlock_bh(&ax25_dev_lock);
 
 	return res;
diff --git a/net/ax25/ax25_route.c b/net/ax25/ax25_route.c
index b7c4d656a94b..ed2cab200589 100644
--- a/net/ax25/ax25_route.c
+++ b/net/ax25/ax25_route.c
@@ -364,6 +364,9 @@ ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
 			if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
 				ax25_def_rt = ax25_rt;
 		}
+
+		if (ax25_spe_rt != NULL)
+			break;
 	}
 
 	ax25_rt = ax25_def_rt;
-- 
2.40.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net-next] ax25: exit linked-list searches earlier
  2023-04-07 14:20 [PATCH net-next] ax25: exit linked-list searches earlier Peter Lafreniere
@ 2023-04-07 15:14 ` Dan Carpenter
  2023-04-14 14:33 ` [PATCH v2 " Peter Lafreniere
  1 sibling, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2023-04-07 15:14 UTC (permalink / raw)
  To: Peter Lafreniere; +Cc: linux-hams, netdev

On Fri, Apr 07, 2023 at 10:20:42AM -0400, Peter Lafreniere wrote:
> diff --git a/net/ax25/ax25_route.c b/net/ax25/ax25_route.c
> index b7c4d656a94b..ed2cab200589 100644
> --- a/net/ax25/ax25_route.c
> +++ b/net/ax25/ax25_route.c
> @@ -364,6 +364,9 @@ ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
>  			if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
>  				ax25_def_rt = ax25_rt;
>  		}
> +
> +		if (ax25_spe_rt != NULL)
> +			break;

Better to just return directly.

diff --git a/net/ax25/ax25_route.c b/net/ax25/ax25_route.c
index b7c4d656a94b..a8a3ab8c92f6 100644
--- a/net/ax25/ax25_route.c
+++ b/net/ax25/ax25_route.c
@@ -364,13 +364,12 @@ ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
 			if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
 				ax25_def_rt = ax25_rt;
 		}
-	}
 
-	ax25_rt = ax25_def_rt;
-	if (ax25_spe_rt != NULL)
-		ax25_rt = ax25_spe_rt;
+		if (ax25_spe_rt)
+			return ax25_spe_rt;
+	}
 
-	return ax25_rt;
+	return ax25_def_rt;
 }
 
 /*

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 net-next] ax25: exit linked-list searches earlier
  2023-04-07 14:20 [PATCH net-next] ax25: exit linked-list searches earlier Peter Lafreniere
  2023-04-07 15:14 ` Dan Carpenter
@ 2023-04-14 14:33 ` Peter Lafreniere
  2023-04-18  7:15   ` Paolo Abeni
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Lafreniere @ 2023-04-14 14:33 UTC (permalink / raw)
  To: linux-hams; +Cc: Peter Lafreniere, netdev, error27

There's no need to loop until the end of the list if we have a result.

Device callsigns are unique, so there can only be one dev returned from
ax25_addr_ax25dev(). If not, there would be inconsistencies based on
order of insertion, and refcount leaks.

We follow the same reasoning in ax25_get_route(), and additionally
reorder conditions to skip calling ax25cmp() whenever possible. 

Signed-off-by: Peter Lafreniere <peter@n8pjl.ca>
---
v1 -> v2
 - Make ax25_get_route() return directly
 - Reorder calls to ax25cmp() in ax25_get_route()
 - Skip searching for default route once found in ax25_get_route()

 net/ax25/ax25_dev.c   |  4 +++-
 net/ax25/ax25_route.c | 25 +++++++++++++------------
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/net/ax25/ax25_dev.c b/net/ax25/ax25_dev.c
index c5462486dbca..8186faea6b0d 100644
--- a/net/ax25/ax25_dev.c
+++ b/net/ax25/ax25_dev.c
@@ -34,11 +34,13 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
 	ax25_dev *ax25_dev, *res = NULL;
 
 	spin_lock_bh(&ax25_dev_lock);
-	for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
+	for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next) {
 		if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
 			res = ax25_dev;
 			ax25_dev_hold(ax25_dev);
+			break;
 		}
+	}
 	spin_unlock_bh(&ax25_dev_lock);
 
 	return res;
diff --git a/net/ax25/ax25_route.c b/net/ax25/ax25_route.c
index b7c4d656a94b..ebef46c38e80 100644
--- a/net/ax25/ax25_route.c
+++ b/net/ax25/ax25_route.c
@@ -344,7 +344,6 @@ const struct seq_operations ax25_rt_seqops = {
  */
 ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
 {
-	ax25_route *ax25_spe_rt = NULL;
 	ax25_route *ax25_def_rt = NULL;
 	ax25_route *ax25_rt;
 
@@ -354,23 +353,25 @@ ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
 	 */
 	for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
 		if (dev == NULL) {
-			if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
-				ax25_spe_rt = ax25_rt;
-			if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
+			if (ax25_rt->dev != NULL && ax25cmp(&ax25_rt->callsign, addr) == 0)
+				return ax25_rt;
+
+			if (ax25_def_rt != NULL &&
+			    ax25_rt->dev != NULL &&
+			    ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
 				ax25_def_rt = ax25_rt;
 		} else {
-			if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
-				ax25_spe_rt = ax25_rt;
-			if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
+			if (ax25_rt->dev == dev && ax25cmp(&ax25_rt->callsign, addr) == 0)
+				return ax25_rt;
+
+			if (ax25_def_rt != NULL &&
+			    ax25_rt->dev == dev &&
+			    ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
 				ax25_def_rt = ax25_rt;
 		}
 	}
 
-	ax25_rt = ax25_def_rt;
-	if (ax25_spe_rt != NULL)
-		ax25_rt = ax25_spe_rt;
-
-	return ax25_rt;
+	return ax25_def_rt;
 }
 
 /*
-- 
2.40.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 net-next] ax25: exit linked-list searches earlier
  2023-04-14 14:33 ` [PATCH v2 " Peter Lafreniere
@ 2023-04-18  7:15   ` Paolo Abeni
  0 siblings, 0 replies; 4+ messages in thread
From: Paolo Abeni @ 2023-04-18  7:15 UTC (permalink / raw)
  To: Peter Lafreniere, linux-hams; +Cc: netdev, error27

On Fri, 2023-04-14 at 10:33 -0400, Peter Lafreniere wrote:
> There's no need to loop until the end of the list if we have a result.
> 
> Device callsigns are unique, so there can only be one dev returned from
> ax25_addr_ax25dev(). If not, there would be inconsistencies based on
> order of insertion, and refcount leaks.
> 
> We follow the same reasoning in ax25_get_route(), and additionally
> reorder conditions to skip calling ax25cmp() whenever possible. 
> 
> Signed-off-by: Peter Lafreniere <peter@n8pjl.ca>
> ---
> v1 -> v2
>  - Make ax25_get_route() return directly
>  - Reorder calls to ax25cmp() in ax25_get_route()
>  - Skip searching for default route once found in ax25_get_route()
> 
>  net/ax25/ax25_dev.c   |  4 +++-
>  net/ax25/ax25_route.c | 25 +++++++++++++------------
>  2 files changed, 16 insertions(+), 13 deletions(-)
> 
> diff --git a/net/ax25/ax25_dev.c b/net/ax25/ax25_dev.c
> index c5462486dbca..8186faea6b0d 100644
> --- a/net/ax25/ax25_dev.c
> +++ b/net/ax25/ax25_dev.c
> @@ -34,11 +34,13 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
>  	ax25_dev *ax25_dev, *res = NULL;
>  
>  	spin_lock_bh(&ax25_dev_lock);
> -	for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
> +	for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next) {
>  		if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
>  			res = ax25_dev;
>  			ax25_dev_hold(ax25_dev);
> +			break;
>  		}
> +	}
>  	spin_unlock_bh(&ax25_dev_lock);
>  
>  	return res;
> diff --git a/net/ax25/ax25_route.c b/net/ax25/ax25_route.c
> index b7c4d656a94b..ebef46c38e80 100644
> --- a/net/ax25/ax25_route.c
> +++ b/net/ax25/ax25_route.c
> @@ -344,7 +344,6 @@ const struct seq_operations ax25_rt_seqops = {
>   */
>  ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
>  {
> -	ax25_route *ax25_spe_rt = NULL;
>  	ax25_route *ax25_def_rt = NULL;
>  	ax25_route *ax25_rt;
>  
> @@ -354,23 +353,25 @@ ax25_route *ax25_get_route(ax25_address *addr, struct net_device *dev)
>  	 */
>  	for (ax25_rt = ax25_route_list; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
>  		if (dev == NULL) {
> -			if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev != NULL)
> -				ax25_spe_rt = ax25_rt;
> -			if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev != NULL)
> +			if (ax25_rt->dev != NULL && ax25cmp(&ax25_rt->callsign, addr) == 0)
> +				return ax25_rt;
> +
> +			if (ax25_def_rt != NULL &&
> +			    ax25_rt->dev != NULL &&
> +			    ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
>  				ax25_def_rt = ax25_rt;
>  		} else {
> -			if (ax25cmp(&ax25_rt->callsign, addr) == 0 && ax25_rt->dev == dev)
> -				ax25_spe_rt = ax25_rt;
> -			if (ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0 && ax25_rt->dev == dev)
> +			if (ax25_rt->dev == dev && ax25cmp(&ax25_rt->callsign, addr) == 0)
> +				return ax25_rt;
> +
> +			if (ax25_def_rt != NULL &&
> +			    ax25_rt->dev == dev &&
> +			    ax25cmp(&ax25_rt->callsign, &null_ax25_address) == 0)
>  				ax25_def_rt = ax25_rt;

If I read correctly, multiple routes can legitly match the null
callsign and the above chunk introduces a behavioral change: before the
kernel selected the last of such routes, now the first one.

What about dropping the new condition 'ax25_def_rt != NULL' ?

If so, this patch could target the -net tree - with a suitable fixes
tag.

Thanks!

Paolo


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-04-18  7:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-07 14:20 [PATCH net-next] ax25: exit linked-list searches earlier Peter Lafreniere
2023-04-07 15:14 ` Dan Carpenter
2023-04-14 14:33 ` [PATCH v2 " Peter Lafreniere
2023-04-18  7:15   ` Paolo Abeni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).