public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Leonardo Bras <leobras@redhat.com>
To: Tejun Heo <tj@kernel.org>, Lai Jiangshan <jiangshanlai@gmail.com>,
	Marcelo Tosatti <mtosatti@redhat.com>
Cc: Leonardo Bras <leobras@redhat.com>, linux-kernel@vger.kernel.org
Subject: [PATCH v1 1/1] wq: Avoid using isolated cpus' timers on unbounded queue_delayed_work
Date: Thu, 25 Jan 2024 22:03:20 -0300	[thread overview]
Message-ID: <20240126010321.2550286-1-leobras@redhat.com> (raw)

When __queue_delayed_work() is called with WORK_CPU_UNBOUND, it means any
cpu is able to run the work, as well as any cpu timer is able to be used.

This is not good if a system does use CPU isolation, because it can take
away some valuable cpu time to:
1 - deal with the timer interrupt,
2 - schedule-out the desired task,
3 - queue work on a random workqueue, and
4 - schedule the desired task back to the cpu.

So to fix this, during __queue_delayed_work(), if both:
- Work is not cpu-bounded,
- CPU isolation is in place,
then pick a random non-isolated cpu to use both the timer and the
system per-cpu workqueue.

AS an optimization, if the current cpu is not isolated, use it's timer
instead of looking for another candidate.

Signed-off-by: Leonardo Bras <leobras@redhat.com>
---
Changes since RFC:
- Do not use the same cpu from the timer for queueing the work.
- If the current cpu is not isolated, use it's timer instead of
  looking for another candidate.

 kernel/workqueue.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 76e60faed8923..0a85b00bbe52d 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -1958,10 +1958,24 @@ static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
 	dwork->cpu = cpu;
 	timer->expires = jiffies + delay;
 
-	if (unlikely(cpu != WORK_CPU_UNBOUND))
-		add_timer_on(timer, cpu);
-	else
-		add_timer(timer);
+	if (likely(cpu == WORK_CPU_UNBOUND)) {
+		if (!housekeeping_enabled(HK_TYPE_TIMER)) {
+			/* Reuse the same timer */
+			add_timer(timer);
+			return;
+		}
+
+		/*
+		 * If the work is cpu-unbound, and cpu isolation is in place,
+		 * only use timers from housekeeping cpus.
+		 * If the current cpu is a housekeeping cpu, use it instead.
+		 */
+		cpu = smp_processor_id();
+		if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER))
+			cpu = housekeeping_any_cpu(HK_TYPE_TIMER);
+	}
+
+	add_timer_on(timer, cpu);
 }
 
 /**
-- 
2.43.0


             reply	other threads:[~2024-01-26  1:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-26  1:03 Leonardo Bras [this message]
2024-01-26 21:49 ` [PATCH v1 1/1] wq: Avoid using isolated cpus' timers on unbounded queue_delayed_work Tejun Heo
2024-01-26 22:05   ` Leonardo Bras
2024-01-29 18:18     ` Tejun Heo
2024-01-29 19:26       ` Leonardo Bras
2024-01-29 20:51         ` Tejun Heo
2024-01-29 20:54           ` Leonardo Bras
2024-01-29 21:17             ` Leonardo Bras

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=20240126010321.2550286-1-leobras@redhat.com \
    --to=leobras@redhat.com \
    --cc=jiangshanlai@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=tj@kernel.org \
    /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