mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yury Norov <yury.norov@gmail.com>
To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	virtualization@lists.linux.dev,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
	"Eugenio Pérez" <eperezma@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@google.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>
Cc: Yury Norov <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>
Subject: [PATCH 02/14] virtio_net: simplify virtnet_set_affinity()
Date: Sat, 28 Dec 2024 10:49:34 -0800	[thread overview]
Message-ID: <20241228184949.31582-3-yury.norov@gmail.com> (raw)
In-Reply-To: <20241228184949.31582-1-yury.norov@gmail.com>

The inner loop may be replaced with the dedicated for_each_online_cpu_wrap.
It helps to avoid setting the same bits in the @mask more than once, in
case of group_size is greater than number of online CPUs.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 drivers/net/virtio_net.c | 12 +++++++-----
 include/linux/cpumask.h  |  4 ++++
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 7646ddd9bef7..5e266486de1f 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -3826,7 +3826,7 @@ static void virtnet_set_affinity(struct virtnet_info *vi)
 	cpumask_var_t mask;
 	int stragglers;
 	int group_size;
-	int i, j, cpu;
+	int i, start = 0, cpu;
 	int num_cpu;
 	int stride;
 
@@ -3840,16 +3840,18 @@ static void virtnet_set_affinity(struct virtnet_info *vi)
 	stragglers = num_cpu >= vi->curr_queue_pairs ?
 			num_cpu % vi->curr_queue_pairs :
 			0;
-	cpu = cpumask_first(cpu_online_mask);
 
 	for (i = 0; i < vi->curr_queue_pairs; i++) {
 		group_size = stride + (i < stragglers ? 1 : 0);
 
-		for (j = 0; j < group_size; j++) {
+		for_each_online_cpu_wrap(cpu, start) {
+			if (!group_size--)
+				break;
 			cpumask_set_cpu(cpu, mask);
-			cpu = cpumask_next_wrap(cpu, cpu_online_mask,
-						nr_cpu_ids, false);
 		}
+
+		start = cpu < nr_cpu_ids ? cpu + 1 : start;
+
 		virtqueue_set_affinity(vi->rq[i].vq, mask);
 		virtqueue_set_affinity(vi->sq[i].vq, mask);
 		__netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 5cf69a110c1c..30042351f15f 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -1036,6 +1036,8 @@ extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
 
 #define for_each_possible_cpu_wrap(cpu, start)	\
 	for ((void)(start), (cpu) = 0; (cpu) < 1; (cpu)++)
+#define for_each_online_cpu_wrap(cpu, start)	\
+	for ((void)(start), (cpu) = 0; (cpu) < 1; (cpu)++)
 #else
 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
 #define for_each_online_cpu(cpu)   for_each_cpu((cpu), cpu_online_mask)
@@ -1044,6 +1046,8 @@ extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
 
 #define for_each_possible_cpu_wrap(cpu, start)	\
 	for_each_cpu_wrap((cpu), cpu_possible_mask, (start))
+#define for_each_online_cpu_wrap(cpu, start)	\
+	for_each_cpu_wrap((cpu), cpu_online_mask, (start))
 #endif
 
 /* Wrappers for arch boot code to manipulate normally-constant masks */
-- 
2.43.0


  parent reply	other threads:[~2024-12-28 18:49 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-28 18:49 [PATCH 00/14] cpumask: cleanup cpumask_next_wrap() implementation and usage Yury Norov
2024-12-28 18:49 ` [PATCH 01/14] objpool: rework objpool_pop() Yury Norov
2024-12-28 18:49 ` Yury Norov [this message]
2024-12-28 18:49 ` [PATCH 03/14] ibmvnic: simplify ibmvnic_set_queue_affinity() Yury Norov
2025-01-07 22:37   ` Nick Child
2025-01-07 22:42     ` Yury Norov
2025-01-07 23:04       ` Yury Norov
2025-01-08 14:08         ` [PATCH 03/14] ibmvnic: simplify ibmvnic_set_queue_affinity()y Nick Child
2024-12-28 18:49 ` [PATCH 04/14] powerpc/xmon: simplify xmon_batch_next_cpu() Yury Norov
2024-12-28 18:49 ` [PATCH 05/14] cpumask: deprecate cpumask_next_wrap() Yury Norov
2025-01-03 17:39   ` Bjorn Helgaas
2024-12-28 18:49 ` [PATCH 06/14] cpumask: re-introduce cpumask_next{,_and}_wrap() Yury Norov
2025-01-03 17:44   ` Bjorn Helgaas
2025-01-15  3:41     ` Yury Norov
2025-01-07 13:28   ` Alexander Gordeev
2025-01-15  3:38     ` Yury Norov
2024-12-28 18:49 ` [PATCH 07/14] cpumask: use cpumask_next_wrap() where appropriate Yury Norov
2024-12-28 18:49 ` [PATCH 08/14] padata: switch padata_find_next() to using cpumask_next_wrap() Yury Norov
2025-01-04  0:33   ` Herbert Xu
2025-01-07 19:02   ` Daniel Jordan
2024-12-28 18:49 ` [PATCH 09/14] s390: switch stop_machine_yield() " Yury Norov
2024-12-28 18:49 ` [PATCH 10/14] nvme-tcp: switch nvme_tcp_set_queue_io_cpu() " Yury Norov
2024-12-30  8:31   ` Sagi Grimberg
2024-12-28 18:49 ` [PATCH 11/14] scsi: lpfc: switch lpfc_irq_rebalance() " Yury Norov
2024-12-31  1:05   ` Justin Tee
2024-12-28 18:49 ` [PATCH 12/14] scsi: lpfc: rework lpfc_next_{online,present}_cpu() Yury Norov
2024-12-31  1:06   ` Justin Tee
2024-12-28 18:49 ` [PATCH 13/14] PCI: hv: switch hv_compose_multi_msi_req_get_cpu() to using cpumask_next_wrap() Yury Norov
2024-12-29 17:34   ` Michael Kelley
2025-01-03 17:45   ` Bjorn Helgaas
2025-01-03 18:56     ` Yury Norov
2024-12-28 18:49 ` [PATCH 14/14] cpumask: drop cpumask_next_wrap_old() Yury Norov
2025-01-03  7:02 ` [PATCH 00/14] cpumask: cleanup cpumask_next_wrap() implementation and usage Christoph Hellwig
2025-01-03 15:21   ` Yury Norov

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=20241228184949.31582-3-yury.norov@gmail.com \
    --to=yury.norov@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=eperezma@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.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

all inboxes | Powered by JetHome®