All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] net: drop netif_attrmask_next*()
@ 2022-10-13 23:43 Yury Norov
  2022-10-13 23:43 ` [PATCH v2 1/4] net: move setup code out of mutex in __netif_set_xps_queue() Yury Norov
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Yury Norov @ 2022-10-13 23:43 UTC (permalink / raw)
  To: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Sebastian Andrzej Siewior, Menglong Dong,
	Kuniyuki Iwashima, Petr Machata, Guo Ren, Michael S . Tsirkin
  Cc: Yury Norov, linux-kernel

netif_attrmask_next_and() generates warnings if CONFIG_DEBUG_PER_CPU_MAPS
is enabled. It is used in a single place. netif_attrmask_next() is not
used at all. With some rework of __netif_set_xps_queue(), we can drop
both functions, switch the code to well-tested bitmap API and fix the
warning.

v1: https://lore.kernel.org/netdev/20221002151702.3932770-1-yury.norov@gmail.com/T/
v2: Fix missed bitmap initialization in patch #3.

Yury Norov (4):
  net: move setup code out of mutex in __netif_set_xps_queue()
  net: merge XPS_CPU_DEV_MAPS_SIZE and XPS_RXQ_DEV_MAPS_SIZE macros
  net: initialize online_mask unconditionally in __netif_set_xps_queue()
  net: fix opencoded for_each_and_bit() in __netif_set_xps_queue()

 include/linux/netdevice.h | 53 ++-------------------------------------
 net/core/dev.c            | 35 ++++++++++++++------------
 2 files changed, 21 insertions(+), 67 deletions(-)

-- 
2.34.1


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

* [PATCH v2 1/4] net: move setup code out of mutex in __netif_set_xps_queue()
  2022-10-13 23:43 [PATCH v2 0/4] net: drop netif_attrmask_next*() Yury Norov
@ 2022-10-13 23:43 ` Yury Norov
  2022-10-13 23:43 ` [PATCH v2 2/4] net: merge XPS_CPU_DEV_MAPS_SIZE and XPS_RXQ_DEV_MAPS_SIZE macros Yury Norov
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Yury Norov @ 2022-10-13 23:43 UTC (permalink / raw)
  To: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Sebastian Andrzej Siewior, Menglong Dong,
	Kuniyuki Iwashima, Petr Machata, Guo Ren, Michael S . Tsirkin
  Cc: Yury Norov, linux-kernel

maps_sz, nr_ids and online_mask may be set out of the mutex.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 net/core/dev.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index fa53830d0683..70fa12c6551c 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -2563,9 +2563,6 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 			return -EINVAL;
 	}
 
-	mutex_lock(&xps_map_mutex);
-
-	dev_maps = xmap_dereference(dev->xps_maps[type]);
 	if (type == XPS_RXQS) {
 		maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
 		nr_ids = dev->num_rx_queues;
@@ -2579,6 +2576,10 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 	if (maps_sz < L1_CACHE_BYTES)
 		maps_sz = L1_CACHE_BYTES;
 
+	mutex_lock(&xps_map_mutex);
+
+	dev_maps = xmap_dereference(dev->xps_maps[type]);
+
 	/* The old dev_maps could be larger or smaller than the one we're
 	 * setting up now, as dev->num_tc or nr_ids could have been updated in
 	 * between. We could try to be smart, but let's be safe instead and only
-- 
2.34.1


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

* [PATCH v2 2/4] net: merge XPS_CPU_DEV_MAPS_SIZE and XPS_RXQ_DEV_MAPS_SIZE macros
  2022-10-13 23:43 [PATCH v2 0/4] net: drop netif_attrmask_next*() Yury Norov
  2022-10-13 23:43 ` [PATCH v2 1/4] net: move setup code out of mutex in __netif_set_xps_queue() Yury Norov
@ 2022-10-13 23:43 ` Yury Norov
  2022-10-13 23:43 ` [PATCH v2 3/4] net: initialize online_mask unconditionally in __netif_set_xps_queue() Yury Norov
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Yury Norov @ 2022-10-13 23:43 UTC (permalink / raw)
  To: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Sebastian Andrzej Siewior, Menglong Dong,
	Kuniyuki Iwashima, Petr Machata, Guo Ren, Michael S . Tsirkin
  Cc: Yury Norov, linux-kernel

The macros are used in a single place, and merging them
would simplify the code.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 include/linux/netdevice.h | 7 ++-----
 net/core/dev.c            | 3 +--
 2 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index a36edb0ec199..53d738f66159 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -825,11 +825,8 @@ struct xps_dev_maps {
 	struct xps_map __rcu *attr_map[]; /* Either CPUs map or RXQs map */
 };
 
-#define XPS_CPU_DEV_MAPS_SIZE(_tcs) (sizeof(struct xps_dev_maps) +	\
-	(nr_cpu_ids * (_tcs) * sizeof(struct xps_map *)))
-
-#define XPS_RXQ_DEV_MAPS_SIZE(_tcs, _rxqs) (sizeof(struct xps_dev_maps) +\
-	(_rxqs * (_tcs) * sizeof(struct xps_map *)))
+#define XPS_DEV_MAPS_SIZE(_tcs, nr) (sizeof(struct xps_dev_maps) +\
+	((nr) * (_tcs) * sizeof(struct xps_map *)))
 
 #endif /* CONFIG_XPS */
 
diff --git a/net/core/dev.c b/net/core/dev.c
index 70fa12c6551c..9dc6fcb0d48a 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -2564,15 +2564,14 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 	}
 
 	if (type == XPS_RXQS) {
-		maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
 		nr_ids = dev->num_rx_queues;
 	} else {
-		maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
 		if (num_possible_cpus() > 1)
 			online_mask = cpumask_bits(cpu_online_mask);
 		nr_ids = nr_cpu_ids;
 	}
 
+	maps_sz = XPS_DEV_MAPS_SIZE(num_tc, nr_ids);
 	if (maps_sz < L1_CACHE_BYTES)
 		maps_sz = L1_CACHE_BYTES;
 
-- 
2.34.1


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

* [PATCH v2 3/4] net: initialize online_mask unconditionally in __netif_set_xps_queue()
  2022-10-13 23:43 [PATCH v2 0/4] net: drop netif_attrmask_next*() Yury Norov
  2022-10-13 23:43 ` [PATCH v2 1/4] net: move setup code out of mutex in __netif_set_xps_queue() Yury Norov
  2022-10-13 23:43 ` [PATCH v2 2/4] net: merge XPS_CPU_DEV_MAPS_SIZE and XPS_RXQ_DEV_MAPS_SIZE macros Yury Norov
@ 2022-10-13 23:43 ` Yury Norov
  2022-10-13 23:43 ` [PATCH v2 4/4] net: fix opencoded for_each_and_bit() " Yury Norov
  2022-10-14 14:20 ` [PATCH v2 0/4] net: drop netif_attrmask_next*() Michael S. Tsirkin
  4 siblings, 0 replies; 7+ messages in thread
From: Yury Norov @ 2022-10-13 23:43 UTC (permalink / raw)
  To: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Sebastian Andrzej Siewior, Menglong Dong,
	Kuniyuki Iwashima, Petr Machata, Guo Ren, Michael S . Tsirkin
  Cc: Yury Norov, linux-kernel

If the mask is initialized unconditionally, it's possible to use bitmap
API to traverse it, which is done in the following patch.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 net/core/dev.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index 9dc6fcb0d48a..8049e2ff11a5 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -2542,7 +2542,7 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 			  u16 index, enum xps_map_type type)
 {
 	struct xps_dev_maps *dev_maps, *new_dev_maps = NULL, *old_dev_maps = NULL;
-	const unsigned long *online_mask = NULL;
+	const unsigned long *online_mask;
 	bool active = false, copy = false;
 	int i, j, tci, numa_node_id = -2;
 	int maps_sz, num_tc = 1, tc = 0;
@@ -2565,9 +2565,12 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 
 	if (type == XPS_RXQS) {
 		nr_ids = dev->num_rx_queues;
+		online_mask = bitmap_alloc(nr_ids, GFP_KERNEL);
+		if (!online_mask)
+			return -ENOMEM;
+		bitmap_fill(online_mask, nr_ids);
 	} else {
-		if (num_possible_cpus() > 1)
-			online_mask = cpumask_bits(cpu_online_mask);
+		online_mask = cpumask_bits(cpu_online_mask);
 		nr_ids = nr_cpu_ids;
 	}
 
@@ -2593,10 +2596,8 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 	     j < nr_ids;) {
 		if (!new_dev_maps) {
 			new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
-			if (!new_dev_maps) {
-				mutex_unlock(&xps_map_mutex);
-				return -ENOMEM;
-			}
+			if (!new_dev_maps)
+				goto err_out;
 
 			new_dev_maps->nr_ids = nr_ids;
 			new_dev_maps->num_tc = num_tc;
@@ -2718,7 +2719,8 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 
 out_no_maps:
 	mutex_unlock(&xps_map_mutex);
-
+	if (type == XPS_RXQS)
+		bitmap_free(online_mask);
 	return 0;
 error:
 	/* remove any maps that we added */
@@ -2733,8 +2735,10 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 		}
 	}
 
+err_out:
 	mutex_unlock(&xps_map_mutex);
-
+	if (type == XPS_RXQS)
+		bitmap_free(online_mask);
 	kfree(new_dev_maps);
 	return -ENOMEM;
 }
-- 
2.34.1


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

* [PATCH v2 4/4] net: fix opencoded for_each_and_bit() in __netif_set_xps_queue()
  2022-10-13 23:43 [PATCH v2 0/4] net: drop netif_attrmask_next*() Yury Norov
                   ` (2 preceding siblings ...)
  2022-10-13 23:43 ` [PATCH v2 3/4] net: initialize online_mask unconditionally in __netif_set_xps_queue() Yury Norov
@ 2022-10-13 23:43 ` Yury Norov
  2022-10-14  9:38   ` Michael S. Tsirkin
  2022-10-14 14:20 ` [PATCH v2 0/4] net: drop netif_attrmask_next*() Michael S. Tsirkin
  4 siblings, 1 reply; 7+ messages in thread
From: Yury Norov @ 2022-10-13 23:43 UTC (permalink / raw)
  To: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Sebastian Andrzej Siewior, Menglong Dong,
	Kuniyuki Iwashima, Petr Machata, Guo Ren, Michael S . Tsirkin
  Cc: Yury Norov, linux-kernel

Replace opencoded bitmap traversing and drop unused
netif_attrmask_next*() functions

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 include/linux/netdevice.h | 46 ---------------------------------------
 net/core/dev.c            |  3 +--
 2 files changed, 1 insertion(+), 48 deletions(-)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 53d738f66159..5db2b6de7308 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -3648,52 +3648,6 @@ static inline bool netif_attr_test_online(unsigned long j,
 
 	return (j < nr_bits);
 }
-
-/**
- *	netif_attrmask_next - get the next CPU/Rx queue in a cpu/Rx queues mask
- *	@n: CPU/Rx queue index
- *	@srcp: the cpumask/Rx queue mask pointer
- *	@nr_bits: number of bits in the bitmask
- *
- * Returns >= nr_bits if no further CPUs/Rx queues set.
- */
-static inline unsigned int netif_attrmask_next(int n, const unsigned long *srcp,
-					       unsigned int nr_bits)
-{
-	/* n is a prior cpu */
-	cpu_max_bits_warn(n + 1, nr_bits);
-
-	if (srcp)
-		return find_next_bit(srcp, nr_bits, n + 1);
-
-	return n + 1;
-}
-
-/**
- *	netif_attrmask_next_and - get the next CPU/Rx queue in \*src1p & \*src2p
- *	@n: CPU/Rx queue index
- *	@src1p: the first CPUs/Rx queues mask pointer
- *	@src2p: the second CPUs/Rx queues mask pointer
- *	@nr_bits: number of bits in the bitmask
- *
- * Returns >= nr_bits if no further CPUs/Rx queues set in both.
- */
-static inline int netif_attrmask_next_and(int n, const unsigned long *src1p,
-					  const unsigned long *src2p,
-					  unsigned int nr_bits)
-{
-	/* n is a prior cpu */
-	cpu_max_bits_warn(n + 1, nr_bits);
-
-	if (src1p && src2p)
-		return find_next_and_bit(src1p, src2p, nr_bits, n + 1);
-	else if (src1p)
-		return find_next_bit(src1p, nr_bits, n + 1);
-	else if (src2p)
-		return find_next_bit(src2p, nr_bits, n + 1);
-
-	return n + 1;
-}
 #else
 static inline int netif_set_xps_queue(struct net_device *dev,
 				      const struct cpumask *mask,
diff --git a/net/core/dev.c b/net/core/dev.c
index 8049e2ff11a5..b0fb592d51da 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -2592,8 +2592,7 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
 		copy = true;
 
 	/* allocate memory for queue storage */
-	for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
-	     j < nr_ids;) {
+	for_each_and_bit(j, online_mask, mask ? : online_mask, nr_ids) {
 		if (!new_dev_maps) {
 			new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
 			if (!new_dev_maps)
-- 
2.34.1


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

* Re: [PATCH v2 4/4] net: fix opencoded for_each_and_bit() in __netif_set_xps_queue()
  2022-10-13 23:43 ` [PATCH v2 4/4] net: fix opencoded for_each_and_bit() " Yury Norov
@ 2022-10-14  9:38   ` Michael S. Tsirkin
  0 siblings, 0 replies; 7+ messages in thread
From: Michael S. Tsirkin @ 2022-10-14  9:38 UTC (permalink / raw)
  To: Yury Norov
  Cc: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Sebastian Andrzej Siewior, Menglong Dong,
	Kuniyuki Iwashima, Petr Machata, Guo Ren, linux-kernel

On Thu, Oct 13, 2022 at 04:43:48PM -0700, Yury Norov wrote:
> Replace opencoded bitmap traversing and drop unused
> netif_attrmask_next*() functions
> 
> Signed-off-by: Yury Norov <yury.norov@gmail.com>

I think you want to document, here, that this fixes a warning.
Additionally, add a Fixes: tag.


> ---
>  include/linux/netdevice.h | 46 ---------------------------------------
>  net/core/dev.c            |  3 +--
>  2 files changed, 1 insertion(+), 48 deletions(-)
> 
> diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
> index 53d738f66159..5db2b6de7308 100644
> --- a/include/linux/netdevice.h
> +++ b/include/linux/netdevice.h
> @@ -3648,52 +3648,6 @@ static inline bool netif_attr_test_online(unsigned long j,
>  
>  	return (j < nr_bits);
>  }
> -
> -/**
> - *	netif_attrmask_next - get the next CPU/Rx queue in a cpu/Rx queues mask
> - *	@n: CPU/Rx queue index
> - *	@srcp: the cpumask/Rx queue mask pointer
> - *	@nr_bits: number of bits in the bitmask
> - *
> - * Returns >= nr_bits if no further CPUs/Rx queues set.
> - */
> -static inline unsigned int netif_attrmask_next(int n, const unsigned long *srcp,
> -					       unsigned int nr_bits)
> -{
> -	/* n is a prior cpu */
> -	cpu_max_bits_warn(n + 1, nr_bits);
> -
> -	if (srcp)
> -		return find_next_bit(srcp, nr_bits, n + 1);
> -
> -	return n + 1;
> -}
> -
> -/**
> - *	netif_attrmask_next_and - get the next CPU/Rx queue in \*src1p & \*src2p
> - *	@n: CPU/Rx queue index
> - *	@src1p: the first CPUs/Rx queues mask pointer
> - *	@src2p: the second CPUs/Rx queues mask pointer
> - *	@nr_bits: number of bits in the bitmask
> - *
> - * Returns >= nr_bits if no further CPUs/Rx queues set in both.
> - */
> -static inline int netif_attrmask_next_and(int n, const unsigned long *src1p,
> -					  const unsigned long *src2p,
> -					  unsigned int nr_bits)
> -{
> -	/* n is a prior cpu */
> -	cpu_max_bits_warn(n + 1, nr_bits);
> -
> -	if (src1p && src2p)
> -		return find_next_and_bit(src1p, src2p, nr_bits, n + 1);
> -	else if (src1p)
> -		return find_next_bit(src1p, nr_bits, n + 1);
> -	else if (src2p)
> -		return find_next_bit(src2p, nr_bits, n + 1);
> -
> -	return n + 1;
> -}
>  #else
>  static inline int netif_set_xps_queue(struct net_device *dev,
>  				      const struct cpumask *mask,
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 8049e2ff11a5..b0fb592d51da 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -2592,8 +2592,7 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
>  		copy = true;
>  
>  	/* allocate memory for queue storage */
> -	for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
> -	     j < nr_ids;) {
> +	for_each_and_bit(j, online_mask, mask ? : online_mask, nr_ids) {
>  		if (!new_dev_maps) {
>  			new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
>  			if (!new_dev_maps)
> -- 
> 2.34.1


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

* Re: [PATCH v2 0/4] net: drop netif_attrmask_next*()
  2022-10-13 23:43 [PATCH v2 0/4] net: drop netif_attrmask_next*() Yury Norov
                   ` (3 preceding siblings ...)
  2022-10-13 23:43 ` [PATCH v2 4/4] net: fix opencoded for_each_and_bit() " Yury Norov
@ 2022-10-14 14:20 ` Michael S. Tsirkin
  4 siblings, 0 replies; 7+ messages in thread
From: Michael S. Tsirkin @ 2022-10-14 14:20 UTC (permalink / raw)
  To: Yury Norov
  Cc: netdev, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Sebastian Andrzej Siewior, Menglong Dong,
	Kuniyuki Iwashima, Petr Machata, Guo Ren, linux-kernel

On Thu, Oct 13, 2022 at 04:43:44PM -0700, Yury Norov wrote:
> netif_attrmask_next_and() generates warnings if CONFIG_DEBUG_PER_CPU_MAPS
> is enabled. It is used in a single place. netif_attrmask_next() is not
> used at all. With some rework of __netif_set_xps_queue(), we can drop
> both functions, switch the code to well-tested bitmap API and fix the
> warning.
> 
> v1: https://lore.kernel.org/netdev/20221002151702.3932770-1-yury.norov@gmail.com/T/
> v2: Fix missed bitmap initialization in patch #3.


does not seem to fix the warning
https://lore.kernel.org/r/0000000000001d91e205eafc3d01%40google.com

is it supposed to fix it?

> Yury Norov (4):
>   net: move setup code out of mutex in __netif_set_xps_queue()
>   net: merge XPS_CPU_DEV_MAPS_SIZE and XPS_RXQ_DEV_MAPS_SIZE macros
>   net: initialize online_mask unconditionally in __netif_set_xps_queue()
>   net: fix opencoded for_each_and_bit() in __netif_set_xps_queue()
> 
>  include/linux/netdevice.h | 53 ++-------------------------------------
>  net/core/dev.c            | 35 ++++++++++++++------------
>  2 files changed, 21 insertions(+), 67 deletions(-)
> 
> -- 
> 2.34.1


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

end of thread, other threads:[~2022-10-14 14:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-13 23:43 [PATCH v2 0/4] net: drop netif_attrmask_next*() Yury Norov
2022-10-13 23:43 ` [PATCH v2 1/4] net: move setup code out of mutex in __netif_set_xps_queue() Yury Norov
2022-10-13 23:43 ` [PATCH v2 2/4] net: merge XPS_CPU_DEV_MAPS_SIZE and XPS_RXQ_DEV_MAPS_SIZE macros Yury Norov
2022-10-13 23:43 ` [PATCH v2 3/4] net: initialize online_mask unconditionally in __netif_set_xps_queue() Yury Norov
2022-10-13 23:43 ` [PATCH v2 4/4] net: fix opencoded for_each_and_bit() " Yury Norov
2022-10-14  9:38   ` Michael S. Tsirkin
2022-10-14 14:20 ` [PATCH v2 0/4] net: drop netif_attrmask_next*() Michael S. Tsirkin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.