Skip to contentSkip to conversation

sched/mm: LRU drain flush on nohz_full

PATCH Superseded Frederic Weisbecker 5 participants 14 messages Apr 10, 2025 – Jul 2, 2025 1 message not in the archive

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover sched/mm: LRU drain flush on nohz_full

Hi,

When LRUs are pending, the drain can be triggered remotely, whether the remote CPU is running in userspace in nohz_full mode or not. This kind of noise is expected to be caused by preparatory work before a task runs isolated in userspace. This patchset is a proposal to flush that before the task starts its critical work in userspace.

Changes since v2:

* Add tags (thanks everyone)
* Assume TASK_WORK_DEQUEUED is set before calling task_work_add() (Oleg)
* Return -EINVAL if queued from kthread instead of silently ignoring.
* Queue from a more appropriate place (folio_batch_add()) (Michal)
* Refactor changelog on last patch (Michal)

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
task/work

HEAD: 036bfe5153de18c995653ee5074d5eec463bbde0

Thanks,
Frederic
---

Frederic Weisbecker (6):
task_work: Provide means to check if a work is queued sched/fair: Use task_work_queued() on numa_work
sched: Use task_work_queued() on cid_work
tick/nohz: Move nohz_full related fields out of hot task struct's places sched/isolation: Introduce isolated task work
mm: Drain LRUs upon resume to userspace on nohz_full CPUs

 include/linux/pagevec.h         | 18 ++----------------
 include/linux/sched.h           | 15 +++++++++------
 include/linux/sched/isolation.h | 17 +++++++++++++++++
 include/linux/swap.h            |  1 +
 include/linux/task_work.h       | 12 ++++++++++++
 kernel/sched/core.c             |  6 ++----
 kernel/sched/fair.c             |  5 +----
 kernel/sched/isolation.c        | 34 ++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h            |  1 +
 kernel/task_work.c              |  9 +++++++--
 mm/swap.c                       | 30 +++++++++++++++++++++++++++++-
 11 files changed, 115 insertions(+), 33 deletions(-)
#1 lore raw

PATCH v3 1/6 task_work: Provide means to check if a work is queued

Some task work users implement their own ways to know if a callback is already queued on the current task while fiddling with the callback head internals.

Provide instead a consolidated API to serve this very purpose.

---
 include/linux/task_work.h | 12 ++++++++++++
 kernel/task_work.c        |  9 +++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
include/linux/task_work.h +12 -0
index 0646804860ff..31caf12c1313 100644
@@ -5,12 +5,15 @@
 #include <linux/list.h>
 #include <linux/sched.h>
 
+#define TASK_WORK_DEQUEUED	((void *) -1UL)
+
 typedef void (*task_work_func_t)(struct callback_head *);
 
 static inline void
 init_task_work(struct callback_head *twork, task_work_func_t func)
 {
 	twork->func = func;
+	twork->next = TASK_WORK_DEQUEUED;
 }
 
 enum task_work_notify_mode {
@@ -26,6 +29,15 @@ static inline bool task_work_pending(struct task_struct *task)
 	return READ_ONCE(task->task_works);
 }
 
+/*
+ * Check if a work is queued. Beware: this is inherently racy if the work can
+ * be queued elsewhere than the current task.
+ */
+static inline bool task_work_queued(struct callback_head *twork)
+{
+	return twork->next != TASK_WORK_DEQUEUED;
+}
+
 int task_work_add(struct task_struct *task, struct callback_head *twork,
 			enum task_work_notify_mode mode);
 
kernel/task_work.c +7 -2
index d1efec571a4a..56718cb824d9 100644
@@ -67,8 +67,10 @@ int task_work_add(struct task_struct *task, struct callback_head *work,
 
 	head = READ_ONCE(task->task_works);
 	do {
-		if (unlikely(head == &work_exited))
+		if (unlikely(head == &work_exited)) {
+			work->next = TASK_WORK_DEQUEUED;
 			return -ESRCH;
+		}
 		work->next = head;
 	} while (!try_cmpxchg(&task->task_works, &head, work));
 
@@ -129,8 +131,10 @@ task_work_cancel_match(struct task_struct *task,
 		if (!match(work, data)) {
 			pprev = &work->next;
 			work = READ_ONCE(*pprev);
-		} else if (try_cmpxchg(pprev, &work, work->next))
+		} else if (try_cmpxchg(pprev, &work, work->next)) {
+			work->next = TASK_WORK_DEQUEUED;
 			break;
+		}
 	}
 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
 
@@ -224,6 +228,7 @@ void task_work_run(void)
 
 		do {
 			next = work->next;
+			work->next = TASK_WORK_DEQUEUED;
 			work->func(work);
 			work = next;
 			cond_resched();
Signature · 1 line
2.48.1
#2 lore raw

PATCH v3 2/6 sched/fair: Use task_work_queued() on numa_work

In reply to Frederic Weisbecker

Remove the ad-hoc implementation of task_work_queued().

---
 kernel/sched/fair.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/fair.c +1 -4
index e43993a4e580..c6ffa2fdbbd6 100644
@@ -3317,7 +3317,6 @@ static void task_numa_work(struct callback_head *work)
 
 	WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
 
-	work->next = work;
 	/*
 	 * Who cares about NUMA placement when they're dying.
 	 *
@@ -3565,8 +3564,6 @@ void init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
 	p->numa_scan_seq		= mm ? mm->numa_scan_seq : 0;
 	p->numa_scan_period		= sysctl_numa_balancing_scan_delay;
 	p->numa_migrate_retry		= 0;
-	/* Protect against double add, see task_tick_numa and task_numa_work */
-	p->numa_work.next		= &p->numa_work;
 	p->numa_faults			= NULL;
 	p->numa_pages_migrated		= 0;
 	p->total_numa_faults		= 0;
@@ -3607,7 +3604,7 @@ static void task_tick_numa(struct rq *rq, struct task_struct *curr)
 	/*
 	 * We don't care about NUMA placement if we don't have memory.
 	 */
-	if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || work->next != work)
+	if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || task_work_queued(work))
 		return;
 
 	/*
Signature · 1 line
2.48.1
#3 lore raw

PATCH v3 3/6 sched: Use task_work_queued() on cid_work

In reply to Frederic Weisbecker

Remove the ad-hoc implementation of task_work_queued()

---
 kernel/sched/core.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/core.c +1 -4
index cfaca3040b2f..add41254b6e5 100644
@@ -10576,7 +10576,6 @@ static void task_mm_cid_work(struct callback_head *work)
 
 	WARN_ON_ONCE(t != container_of(work, struct task_struct, cid_work));
 
-	work->next = work;	/* Prevent double-add */
 	if (t->flags & PF_EXITING)
 		return;
 	mm = t->mm;
@@ -10620,7 +10619,6 @@ void init_sched_mm_cid(struct task_struct *t)
 		if (mm_users == 1)
 			mm->mm_cid_next_scan = jiffies + msecs_to_jiffies(MM_CID_SCAN_DELAY);
 	}
-	t->cid_work.next = &t->cid_work;	/* Protect against double add */
 	init_task_work(&t->cid_work, task_mm_cid_work);
 }
 
@@ -10629,8 +10627,7 @@ void task_tick_mm_cid(struct rq *rq, struct task_struct *curr)
 	struct callback_head *work = &curr->cid_work;
 	unsigned long now = jiffies;
 
-	if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) ||
-	    work->next != work)
+	if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || task_work_queued(work))
 		return;
 	if (time_before(now, READ_ONCE(curr->mm->mm_cid_next_scan)))
 		return;
Signature · 1 line
2.48.1
#4 lore raw

PATCH v3 4/6 tick/nohz: Move nohz_full related fields out of hot task struct's places

In reply to Frederic Weisbecker

nohz_full is a feature that only fits into rare and very corner cases. Yet distros enable it by default and therefore the related fields are always reserved in the task struct.

Those task fields are stored in the middle of cacheline hot places such as cputime accounting and context switch counting, which doesn't make any sense for a feature that is disabled most of the time.

Move the nohz_full storage to colder places.

---
 include/linux/sched.h | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
include/linux/sched.h +8 -6
index f96ac1982893..b5ce76db6d75 100644
@@ -1110,13 +1110,7 @@ struct task_struct {
 #endif
 	u64				gtime;
 	struct prev_cputime		prev_cputime;
-#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
-	struct vtime			vtime;
-#endif
 
-#ifdef CONFIG_NO_HZ_FULL
-	atomic_t			tick_dep_mask;
-#endif
 	/* Context switch counts: */
 	unsigned long			nvcsw;
 	unsigned long			nivcsw;
@@ -1438,6 +1432,14 @@ struct task_struct {
 	struct task_delay_info		*delays;
 #endif
 
+#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
+	struct vtime			vtime;
+#endif
+
+#ifdef CONFIG_NO_HZ_FULL
+	atomic_t			tick_dep_mask;
+#endif
+
 #ifdef CONFIG_FAULT_INJECTION
 	int				make_it_fail;
 	unsigned int			fail_nth;
Signature · 1 line
2.48.1
#7 lore raw

PATCH v3 5/6 sched/isolation: Introduce isolated task work

In reply to Frederic Weisbecker

Some asynchronous kernel work may be pending upon resume to userspace and execute later on. On isolated workload this becomes problematic once the process is done with preparatory work involving syscalls and wants to run in userspace without being interrupted.

Provide an infrastructure to queue a work to be executed from the current isolated task context right before resuming to userspace. This goes with the assumption that isolated tasks are pinned to a single nohz_full CPU.

---
 include/linux/sched.h           |  1 +
 include/linux/sched/isolation.h | 17 +++++++++++++++++
 kernel/sched/core.c             |  1 +
 kernel/sched/isolation.c        | 31 +++++++++++++++++++++++++++++++
 kernel/sched/sched.h            |  1 +
 5 files changed, 51 insertions(+)
include/linux/sched.h +1 -0
index b5ce76db6d75..4d764eb96e3e 100644
@@ -1437,6 +1437,7 @@ struct task_struct {
 #endif
 
 #ifdef CONFIG_NO_HZ_FULL
+	struct callback_head		nohz_full_work;
 	atomic_t			tick_dep_mask;
 #endif
 
include/linux/sched/isolation.h +17 -0
index d8501f4709b5..74da4324b984 100644
@@ -77,4 +77,21 @@ static inline bool cpu_is_isolated(int cpu)
 	       cpuset_cpu_is_isolated(cpu);
 }
 
+#if defined(CONFIG_NO_HZ_FULL)
+extern int __isolated_task_work_queue(void);
+
+static inline int isolated_task_work_queue(void)
+{
+	if (!housekeeping_cpu(raw_smp_processor_id(), HK_TYPE_KERNEL_NOISE))
+		return -ENOTSUPP;
+
+	return __isolated_task_work_queue();
+}
+
+extern void isolated_task_work_init(struct task_struct *tsk);
+#else
+static inline int isolated_task_work_queue(void) { return -ENOTSUPP; }
+static inline void isolated_task_work_init(struct task_struct *tsk) { }
+#endif /* CONFIG_NO_HZ_FULL */
+
 #endif /* _LINUX_SCHED_ISOLATION_H */
kernel/sched/core.c +1 -0
index add41254b6e5..c8b8b61ac3a6 100644
@@ -4524,6 +4524,7 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
 	p->migration_pending = NULL;
 #endif
 	init_sched_mm_cid(p);
+	isolated_task_work_init(p);
 }
 
 DEFINE_STATIC_KEY_FALSE(sched_numa_balancing);
kernel/sched/isolation.c +31 -0
index 81bc8b329ef1..e246287de9fa 100644
@@ -249,3 +249,34 @@ static int __init housekeeping_isolcpus_setup(char *str)
 	return housekeeping_setup(str, flags);
 }
 __setup("isolcpus=", housekeeping_isolcpus_setup);
+
+#if defined(CONFIG_NO_HZ_FULL)
+static void isolated_task_work(struct callback_head *head)
+{
+}
+
+int __isolated_task_work_queue(void)
+{
+	unsigned long flags;
+	int ret;
+
+	if (current->flags & PF_KTHREAD)
+		return -EINVAL;
+
+	local_irq_save(flags);
+	if (task_work_queued(&current->nohz_full_work)) {
+		ret = 0;
+		goto out;
+	}
+
+	ret = task_work_add(current, &current->nohz_full_work, TWA_RESUME);
+out:
+	local_irq_restore(flags);
+	return ret;
+}
+
+void isolated_task_work_init(struct task_struct *tsk)
+{
+	init_task_work(&tsk->nohz_full_work, isolated_task_work);
+}
+#endif /* CONFIG_NO_HZ_FULL */
kernel/sched/sched.h +1 -0
index 47972f34ea70..e7dc4ae5ccc1 100644
@@ -60,6 +60,7 @@
 #include <linux/stop_machine.h>
 #include <linux/syscalls_api.h>
 #include <linux/syscalls.h>
+#include <linux/task_work.h>
 #include <linux/tick.h>
 #include <linux/topology.h>
 #include <linux/types.h>
Signature · 1 line
2.48.1
#11 lore raw

PATCH v3 6/6 mm: Drain LRUs upon resume to userspace on nohz_full CPUs

In reply to Frederic Weisbecker

LRU batching can be source of disturbances for isolated workloads running in the userspace because it requires kernel worker to handle that and that would preempt the said task. The primary source for such disruption would be __lru_add_drain_all which could be triggered from non-isolated CPUs.

Why would an isolated CPU have anything on the pcp cache? Many syscalls allocate pages that might end there. A typical and unavoidable one would be fork/exec leaving pages on the cache behind just waiting for somebody to drain.

Address the problem by noting a batch has been added to the cache and schedule draining upon return to userspace so the work is done while the syscall is still executing and there are no suprises while the task runs in the userspace where it doesn't want to be preempted.

---
 include/linux/pagevec.h  | 18 ++----------------
 include/linux/swap.h     |  1 +
 kernel/sched/isolation.c |  3 +++
 mm/swap.c                | 30 +++++++++++++++++++++++++++++-
 4 files changed, 35 insertions(+), 17 deletions(-)
include/linux/pagevec.h +2 -16
index 5d3a0cccc6bf..7e647b8df4c7 100644
@@ -61,22 +61,8 @@ static inline unsigned int folio_batch_space(struct folio_batch *fbatch)
 	return PAGEVEC_SIZE - fbatch->nr;
 }
 
-/**
- * folio_batch_add() - Add a folio to a batch.
- * @fbatch: The folio batch.
- * @folio: The folio to add.
- *
- * The folio is added to the end of the batch.
- * The batch must have previously been initialised using folio_batch_init().
- *
- * Return: The number of slots still available.
- */
-static inline unsigned folio_batch_add(struct folio_batch *fbatch,
-		struct folio *folio)
-{
-	fbatch->folios[fbatch->nr++] = folio;
-	return folio_batch_space(fbatch);
-}
+unsigned int folio_batch_add(struct folio_batch *fbatch,
+			     struct folio *folio);
 
 /**
  * folio_batch_next - Return the next folio to process.
include/linux/swap.h +1 -0
index db46b25a65ae..8244475c2efe 100644
@@ -401,6 +401,7 @@ extern void lru_add_drain(void);
 extern void lru_add_drain_cpu(int cpu);
 extern void lru_add_drain_cpu_zone(struct zone *zone);
 extern void lru_add_drain_all(void);
+extern void lru_add_and_bh_lrus_drain(void);
 void folio_deactivate(struct folio *folio);
 void folio_mark_lazyfree(struct folio *folio);
 extern void swap_setup(void);
kernel/sched/isolation.c +3 -0
index e246287de9fa..553889f4e9be 100644
@@ -8,6 +8,8 @@
  *
  */
 
+#include <linux/swap.h>
+
 enum hk_flags {
 	HK_FLAG_DOMAIN		= BIT(HK_TYPE_DOMAIN),
 	HK_FLAG_MANAGED_IRQ	= BIT(HK_TYPE_MANAGED_IRQ),
@@ -253,6 +255,7 @@ __setup("isolcpus=", housekeeping_isolcpus_setup);
 #if defined(CONFIG_NO_HZ_FULL)
 static void isolated_task_work(struct callback_head *head)
 {
+	lru_add_and_bh_lrus_drain();
 }
 
 int __isolated_task_work_queue(void)
mm/swap.c +29 -1
index 77b2d5997873..99a1b7b81e86 100644
@@ -37,6 +37,7 @@
 #include <linux/page_idle.h>
 #include <linux/local_lock.h>
 #include <linux/buffer_head.h>
+#include <linux/sched/isolation.h>
 
 #include "internal.h"
 
@@ -155,6 +156,29 @@ static void lru_add(struct lruvec *lruvec, struct folio *folio)
 	trace_mm_lru_insertion(folio);
 }
 
+/**
+ * folio_batch_add() - Add a folio to a batch.
+ * @fbatch: The folio batch.
+ * @folio: The folio to add.
+ *
+ * The folio is added to the end of the batch.
+ * The batch must have previously been initialised using folio_batch_init().
+ *
+ * Return: The number of slots still available.
+ */
+unsigned int folio_batch_add(struct folio_batch *fbatch,
+			     struct folio *folio)
+{
+	unsigned int ret;
+
+	fbatch->folios[fbatch->nr++] = folio;
+	ret = folio_batch_space(fbatch);
+	isolated_task_work_queue();
+
+	return ret;
+}
+EXPORT_SYMBOL(folio_batch_add);
+
 static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)
 {
 	int i;
@@ -738,7 +762,7 @@ void lru_add_drain(void)
  * the same cpu. It shouldn't be a problem in !SMP case since
  * the core is only one and the locks will disable preemption.
  */
-static void lru_add_and_bh_lrus_drain(void)
+void lru_add_and_bh_lrus_drain(void)
 {
 	local_lock(&cpu_fbatches.lock);
 	lru_add_drain_cpu(smp_processor_id());
@@ -864,6 +888,10 @@ static inline void __lru_add_drain_all(bool force_all_cpus)
 	for_each_online_cpu(cpu) {
 		struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
 
+		/* Isolated CPUs handle their cache upon return to userspace */
+		if (!housekeeping_cpu(cpu, HK_TYPE_KERNEL_NOISE))
+			continue;
+
 		if (cpu_needs_drain(cpu)) {
 			INIT_WORK(work, lru_add_drain_per_cpu);
 			queue_work_on(cpu, mm_percpu_wq, work);
Signature · 1 line
2.48.1
#8 lore raw

Re: [PATCH 5/6] sched/isolation: Introduce isolated task work

In reply to Frederic Weisbecker

I know nothing about this code so I can't review, but let me ask anyway...

kernel/sched/isolation.c static int __init housekeeping_isolcpus_setup(char *str) · line 264

Quoted 8 lines · Frederic Weisbecker
kernel/sched/isolation.c +7 -0
+int __isolated_task_work_queue(void)
+{
+	unsigned long flags;
+	int ret;
+
+	if (current->flags & PF_KTHREAD)
+		return -EINVAL;

What about PF_USER_WORKER's ? IIUC, these (in fact kernel) threads never return to userspace and never call task_work_run().

Or PF_IO_WORKER's, they too run only in kernel mode... But iirc they do call task_work_run().

kernel/sched/isolation.c static int __init housekeeping_isolcpus_setup(char *str) · line 275

Quoted 10 lines
kernel/sched/isolation.c +10 -0
+	local_irq_save(flags);
+	if (task_work_queued(&current->nohz_full_work)) {
+		ret = 0;
+		goto out;
+	}
+
+	ret = task_work_add(current, &current->nohz_full_work, TWA_RESUME);
+out:
+	local_irq_restore(flags);
+	return ret;

Hmm, why not

	local_irq_save(flags);
	if (task_work_queued(...))
		ret = 0;
	else
		ret = task_work_add(...);

?

Oleg.

#9 lore raw

Re: [PATCH 5/6] sched/isolation: Introduce isolated task work

Quoted 5 lines · Oleg Nesterov

I know nothing about this code so I can't review, but let me ask anyway...

Quoted 1 line · Frederic Weisbecker
Quoted 10 lines

kernel/sched/isolation.c static int __init housekeeping_isolcpus_setup(char *str) · line 264

Quoted 7 lines
kernel/sched/isolation.c +7 -0
+int __isolated_task_work_queue(void)
+{
+	unsigned long flags;
+	int ret;
+
+	if (current->flags & PF_KTHREAD)
+		return -EINVAL;

What about PF_USER_WORKER's ? IIUC, these (in fact kernel) threads never return to userspace and never call task_work_run().

Ah good catch! (though I'm having a hard time finding out what this is about)...

Quoted 3 lines

Or PF_IO_WORKER's, they too run only in kernel mode... But iirc they do call task_work_run().

At least I see a lot of task_work usage in io_uring, and there are some explicit calls to task_work_run() there...

Quoted 14 lines

kernel/sched/isolation.c static int __init housekeeping_isolcpus_setup(char *str) · line 275

Quoted 11 lines
kernel/sched/isolation.c +10 -0
+	local_irq_save(flags);
+	if (task_work_queued(&current->nohz_full_work)) {
+		ret = 0;
+		goto out;
+	}
+
+	ret = task_work_add(current, &current->nohz_full_work, TWA_RESUME);
+out:
+	local_irq_restore(flags);
+	return ret;

Hmm, why not

Quoted 5 lines
	local_irq_save(flags);
	if (task_work_queued(...))
		ret = 0;
	else
		ret = task_work_add(...);

Hehe, yes indeed!

Thanks!

Signature · 2 lines
Frederic Weisbecker
SUSE Labs
#10 lore raw

Re: [PATCH 5/6] sched/isolation: Introduce isolated task work

In reply to Oleg Nesterov
Quoted 14 lines · Oleg Nesterov

kernel/sched/isolation.c static int __init housekeeping_isolcpus_setup(char *str) · line 275

Quoted 11 lines
kernel/sched/isolation.c +10 -0
+	local_irq_save(flags);
+	if (task_work_queued(&current->nohz_full_work)) {
+		ret = 0;
+		goto out;
+	}
+
+	ret = task_work_add(current, &current->nohz_full_work, TWA_RESUME);
+out:
+	local_irq_restore(flags);
+	return ret;

Hmm, why not

Quoted 7 lines
	local_irq_save(flags);
	if (task_work_queued(...))
		ret = 0;
	else
		ret = task_work_add(...);

?

Or use guard() sand save on flags and ret:

	guard(irqsave)();

	if (task_work_queued(...))
		return 0;

	return task_work_add(...);
Signature · 2 lines
Thanks and Regards,
Prateek
Quoted 4 lines

Oleg.

#5 lore raw

Re: [PATCH 4/6] tick/nohz: Move nohz_full related fields out of hot task struct's places

In reply to Frederic Weisbecker
Quoted 39 lines · Frederic Weisbecker

nohz_full is a feature that only fits into rare and very corner cases. Yet distros enable it by default and therefore the related fields are always reserved in the task struct.

Those task fields are stored in the middle of cacheline hot places such as cputime accounting and context switch counting, which doesn't make any sense for a feature that is disabled most of the time.

Move the nohz_full storage to colder places.

---
  include/linux/sched.h | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
include/linux/sched.h +1 -6
index f96ac1982893..b5ce76db6d75 100644
@@ -1110,13 +1110,7 @@ struct task_struct {
  #endif
  	u64				gtime;
  	struct prev_cputime		prev_cputime;
-#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
-	struct vtime			vtime;
-#endif
 
-#ifdef CONFIG_NO_HZ_FULL
-	atomic_t			tick_dep_mask;
-#endif
  	/* Context switch counts: */
  	unsigned long			nvcsw;
  	unsigned long			nivcsw;
@@ -1438,6 +1432,14 @@ struct task_struct {
  	struct task_delay_info		*delays;
  #endif
 
+#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN

include/linux/sched.h struct task_struct { · line 1445

Quoted 10 lines
include/linux/sched.h +7 -0
+	struct vtime			vtime;
+#endif
+
+#ifdef CONFIG_NO_HZ_FULL
+	atomic_t			tick_dep_mask;
+#endif
+
  #ifdef CONFIG_FAULT_INJECTION
  	int				make_it_fail;
  	unsigned int			fail_nth;

Hi Frederic.

maybe move these nohz related fields into their own cacheline instead?

on PowerPC where we have 128byte cache instead, i see these fields are crossing a cache line boundary.

without patch:
/* XXX last struct has 4 bytes of padding */

struct vtime vtime; /* 2360 48 */
atomic_t tick_dep_mask; /* 2408 4 */
/* XXX 4 bytes hole, try to pack */

long unsigned int nvcsw; /* 2416 8 */
long unsigned int nivcsw; /* 2424 8 */
/* --- cacheline 19 boundary (2432 bytes) --- */

With patch:

	struct vtime               vtime;                /*  3272    48 */
	struct callback_head       nohz_full_work;       /*  3320    16 */
	/* --- cacheline 26 boundary (3328 bytes) was 8 bytes ago --- */
	atomic_t                   tick_dep_mask;        /*  3336     4 */
#6 lore raw

Re: [PATCH 4/6] tick/nohz: Move nohz_full related fields out of hot task struct's places

Quoted 71 lines · Shrikanth Hegde
Quoted 37 lines · Frederic Weisbecker

nohz_full is a feature that only fits into rare and very corner cases. Yet distros enable it by default and therefore the related fields are always reserved in the task struct.

Those task fields are stored in the middle of cacheline hot places such as cputime accounting and context switch counting, which doesn't make any sense for a feature that is disabled most of the time.

Move the nohz_full storage to colder places.

---
  include/linux/sched.h | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
include/linux/sched.h +2 -6
index f96ac1982893..b5ce76db6d75 100644
@@ -1110,13 +1110,7 @@ struct task_struct {
  #endif
  	u64				gtime;
  	struct prev_cputime		prev_cputime;
-#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
-	struct vtime			vtime;
-#endif
-#ifdef CONFIG_NO_HZ_FULL
-	atomic_t			tick_dep_mask;
-#endif
  	/* Context switch counts: */
  	unsigned long			nvcsw;
  	unsigned long			nivcsw;
@@ -1438,6 +1432,14 @@ struct task_struct {
  	struct task_delay_info		*delays;
  #endif
+#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
+	struct vtime			vtime;

include/linux/sched.h struct task_struct { · line 1445

Quoted 10 lines
include/linux/sched.h +6 -0
+#endif
+
+#ifdef CONFIG_NO_HZ_FULL
+	atomic_t			tick_dep_mask;
+#endif
+
  #ifdef CONFIG_FAULT_INJECTION
  	int				make_it_fail;
  	unsigned int			fail_nth;
 

Hi Frederic.

maybe move these nohz related fields into their own cacheline instead?

on PowerPC where we have 128byte cache instead, i see these fields are crossing a cache line boundary.

without patch:
/* XXX last struct has 4 bytes of padding */

struct vtime vtime; /* 2360 48 */
atomic_t tick_dep_mask; /* 2408 4 */
/* XXX 4 bytes hole, try to pack */

long unsigned int nvcsw; /* 2416 8 */
long unsigned int nivcsw; /* 2424 8 */
/* --- cacheline 19 boundary (2432 bytes) --- */

Quoted 6 lines

With patch:

	struct vtime               vtime;                /*  3272    48 */
	struct callback_head       nohz_full_work;       /*  3320    16 */
	/* --- cacheline 26 boundary (3328 bytes) was 8 bytes ago --- */
	atomic_t                   tick_dep_mask;        /*  3336     4 */

It's not much a big deal because those fields shouldn't be accessed much closely in time. Also such a cache alignement is hard to maintain everywhere when there are so many ifdefferies in that structure.

Thanks.

Signature · 2 lines
Frederic Weisbecker
SUSE Labs
#13 lore raw
In reply to a message not in the archive
Quoted 9 lines · Hillf Danton

On Thu, 10 Apr 2025 17:23:21 +0200 Frederic Weisbecker wrote

Quoted 8 lines

Hi,

When LRUs are pending, the drain can be triggered remotely, whether the remote CPU is running in userspace in nohz_full mode or not. This kind of noise is expected to be caused by preparatory work before a task runs isolated in userspace. This patchset is a proposal to flush that before the task starts its critical work in userspace.

Quoted 5 lines

Alternatively add a syscall for workloads on isolated CPUs to flush this xxx and prepare that yyy before entering the critical work, instead of adding random (nice) patches today and next month. Even prctl can do lru_add_and_bh_lrus_drain(), and prctl looks more preferable over adding a syscall.

In an ideal world, this is indeed what we should do now. And I still wish we can do this in the future.

The problem is that this has been tried by the past and the work was never finished because that syscall eventually didn't meet the need for the people working on it.

I would volunteer to start a simple prctl to do such a flush, something that can be extended in the future, should the need arise, but such a new ABI must be thought through along with the CPU isolation community.

Unfortunately there is no such stable CPU isolation community. Many developers in this area contribute changes and then switch to other things. As for the CPU isolation users, they are usually very quiet.

I can't assume all the possible usecases myself and therefore I would easily do it wrong.

In the meantime, the patchset here is a proposal that hopefully should work for many usecases.

Thanks.

Signature · 2 lines
Frederic Weisbecker
SUSE Labs
#14 lore raw

On Tue, 1 Jul 2025 14:36:49 +0200 Frederic Weisbecker wrote

Quoted 23 lines

Le Sat, Apr 12, 2025 at 10:58:22AM +0800, Hillf Danton a �crit :

Quoted 9 lines

On Thu, 10 Apr 2025 17:23:21 +0200 Frederic Weisbecker wrote

Quoted 8 lines

Hi,

When LRUs are pending, the drain can be triggered remotely, whether the remote CPU is running in userspace in nohz_full mode or not. This kind of noise is expected to be caused by preparatory work before a task runs isolated in userspace. This patchset is a proposal to flush that before the task starts its critical work in userspace.

Quoted 5 lines

Alternatively add a syscall for workloads on isolated CPUs to flush this xxx and prepare that yyy before entering the critical work, instead of adding random (nice) patches today and next month. Even prctl can do lru_add_and_bh_lrus_drain(), and prctl looks more preferable over adding a syscall.

In an ideal world, this is indeed what we should do now. And I still wish we can do this in the future.

The problem is that this has been tried by the past and the work was never finished because that syscall eventually didn't meet the need for the people working on it.

Quoted 8 lines

I would volunteer to start a simple prctl to do such a flush, something that can be extended in the future, should the need arise, but such a new ABI must be thought through along with the CPU isolation community.

Unfortunately there is no such stable CPU isolation community. Many developers in this area contribute changes and then switch to other things. As for the CPU isolation users, they are usually very quiet.

Given your footprint in the MAINTAINERS file,

HIGH-RESOLUTION TIMERS, TIMER WHEEL, CLOCKEVENTS
M: Anna-Maria Behnsen <[email protected]>
M: Frederic Weisbecker <[email protected]>
M: Thomas Gleixner <[email protected]>

NOHZ, DYNTICKS SUPPORT
M: Anna-Maria Behnsen <[email protected]>
M: Frederic Weisbecker <[email protected]>
M: Ingo Molnar <[email protected]>
M: Thomas Gleixner <[email protected]>

POSIX CLOCKS and TIMERS
M: Anna-Maria Behnsen <[email protected]>
M: Frederic Weisbecker <[email protected]>
M: Thomas Gleixner <[email protected]>

READ-COPY UPDATE (RCU)
M: "Paul E. McKenney" <[email protected]>
M: Frederic Weisbecker <[email protected]> (kernel/rcu/tree_nocb.h)
M: Neeraj Upadhyay <[email protected]> (kernel/rcu/tasks.h)
M: Joel Fernandes <[email protected]>
M: Josh Triplett <[email protected]>
M: Boqun Feng <[email protected]>
M: Uladzislau Rezki <[email protected]>

I am happy to close the chance for you to deliver patch like a starter.

Quoted 11 lines

I can't assume all the possible usecases myself and therefore I would easily do it wrong.

In the meantime, the patchset here is a proposal that hopefully should work for many usecases.

Thanks.

Signature · 2 lines
Frederic Weisbecker
SUSE Labs