From nobody Tue Oct 7 18:22:22 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 362BA2DC355 for ; Mon, 7 Jul 2025 14:48:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751899723; cv=none; b=fnqwtJ0oYTMzcox4iQ6HxWw1Mi0VC3GgX51RHh+UmTeD5GWxyBCtT+GX0aTxUJEQLlXlGOKMFy8AKBK3yLE8sNHzzVgf7Hn27R6zHqMmMdb6sD6FYYSWtPSHeGj6DAyfa2HIxeP2+9MjafSVztumAoW9+5Ix3+eNYsXIUZk8yxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751899723; c=relaxed/simple; bh=XOo/KqeoUFr4ZoV+7SA2c0dUkyLoUkEZgvTIoppRoo4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M0C0Sd+/aeMAKWCnwJSTEwlWMqcaijnM1dVD4gUM/QBG4w5m4x5duSgsjlhYO+EXJxJ9KMC323oY9lV/pDisYK/EFhM5zu8gZ0euToFUo0QybIL/H+pHIbiWqMVRK98Pbwp8SZ/HvSRD+FFU/Vm87B0thIWJLJAAKKfVqqNbEE8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=DLSzxoRi; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DLSzxoRi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751899721; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=p7oIatGosEglmz7e/TgETTEwaxMtMboDfPP4HMOfw2I=; b=DLSzxoRi0APiq85ikqNLN22pEkvaKhQca7b50+HJ+km9kUi6plaieysev4CqCNvX+lyMkf MEfpNgj+SpbJ8M/Ko61p1GEHA4TUdTXrH9fVh4mw75Z4h//Bw43725JoPVMCgEQewK/FJa woYh1NVIYktCmeevL7eQWpBi+jHauvE= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-338-9VK7nEcINiidUlGI8xicbg-1; Mon, 07 Jul 2025 10:48:39 -0400 X-MC-Unique: 9VK7nEcINiidUlGI8xicbg-1 X-Mimecast-MFC-AGG-ID: 9VK7nEcINiidUlGI8xicbg_1751899718 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BEADB1955EC1; Mon, 7 Jul 2025 14:48:37 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.45.226.23]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3FF79180045B; Mon, 7 Jul 2025 14:48:33 +0000 (UTC) From: Gabriele Monaco To: linux-kernel@vger.kernel.org, Ingo Molnar , Peter Zijlstra , sched-ext@lists.linux.dev Cc: Gabriele Monaco , Mathieu Desnoyers , Ingo Molnar Subject: [PATCH v14 1/3] sched: Add prev_sum_exec_runtime support for RT, DL and SCX classes Date: Mon, 7 Jul 2025 16:48:22 +0200 Message-ID: <20250707144824.117014-2-gmonaco@redhat.com> In-Reply-To: <20250707144824.117014-1-gmonaco@redhat.com> References: <20250707144824.117014-1-gmonaco@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" The fair scheduling class relies on prev_sum_exec_runtime to compute the duration of the task's runtime since it was last scheduled. This value is currently not required by other scheduling classes but can be useful to understand long running tasks and take certain actions (e.g. during a scheduler tick). Add support for prev_sum_exec_runtime to the RT, deadline and sched_ext classes by simply assigning the sum_exec_runtime at each set_next_task. Reviewed-by: Mathieu Desnoyers Signed-off-by: Gabriele Monaco --- kernel/sched/deadline.c | 1 + kernel/sched/ext.c | 1 + kernel/sched/rt.c | 1 + 3 files changed, 3 insertions(+) diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index 89019a1408264..65ecd86bae37d 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -2389,6 +2389,7 @@ static void set_next_task_dl(struct rq *rq, struct ta= sk_struct *p, bool first) p->se.exec_start =3D rq_clock_task(rq); if (on_dl_rq(&p->dl)) update_stats_wait_end_dl(dl_rq, dl_se); + p->se.prev_sum_exec_runtime =3D p->se.sum_exec_runtime; =20 /* You can't push away the running task */ dequeue_pushable_dl_task(rq, p); diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c index b498d867ba210..a4ac4386b9795 100644 --- a/kernel/sched/ext.c +++ b/kernel/sched/ext.c @@ -3255,6 +3255,7 @@ static void set_next_task_scx(struct rq *rq, struct t= ask_struct *p, bool first) } =20 p->se.exec_start =3D rq_clock_task(rq); + p->se.prev_sum_exec_runtime =3D p->se.sum_exec_runtime; =20 /* see dequeue_task_scx() on why we skip when !QUEUED */ if (SCX_HAS_OP(sch, running) && (p->scx.flags & SCX_TASK_QUEUED)) diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c index e40422c370335..2c70ff2042ee9 100644 --- a/kernel/sched/rt.c +++ b/kernel/sched/rt.c @@ -1693,6 +1693,7 @@ static inline void set_next_task_rt(struct rq *rq, st= ruct task_struct *p, bool f p->se.exec_start =3D rq_clock_task(rq); if (on_rt_rq(&p->rt)) update_stats_wait_end_rt(rt_rq, rt_se); + p->se.prev_sum_exec_runtime =3D p->se.sum_exec_runtime; =20 /* The running task is never eligible for pushing */ dequeue_pushable_task(rq, p); --=20 2.50.0 From nobody Tue Oct 7 18:22:22 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0EC87288506 for ; Mon, 7 Jul 2025 14:48:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751899732; cv=none; b=TEqD5ZF/sBPYYFfvnHxCkGeh5emB5dt/JKfYfuBdSEL5RMwi7YyBh6nijKVprrwliQ6Ff3NBh7SAsdcXXmuiAJogMt6QqSSNrktaXSqGNCmlASw1gZOjAlJ9WfbQq5ChK3Ad1cZ3RNQTythbMmRoqhxVRcciBx6ZIFq2Q4V0BGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751899732; c=relaxed/simple; bh=OT85XUTCiK/u9HH8EcciN5jyQveiKYRgfcBGYDbsKLs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EkwIftRF6QmT09aKicpP00KgOj/MXfNqaI1SKy2rtWSj3ubBOfPPNnUAKPDjF1Kiv70V/NtUxhpCb457NJscoeGyHAE+kBXkrGCKd2jH6BN5Jd1ETrkngZ+c/bo/5J3NnALrl46CTLMZ8ZJJ7wW+7MZ4+hxVilF/jAw/CRzvG1Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WRvgGpRp; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WRvgGpRp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751899728; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+3YnxnC/FgU308ft+qHvN/C9UDnmZxA2+M7xo4oEPhY=; b=WRvgGpRpSS9wFxhFQJkBCzIvuFDo+e8dNX+QbUX01wJBefApBnC/oCIX7cKpLsSEiWpY8q sUmJ7itDaWN4v9cwa7FZdfHduZ5BUzMgqM9rXN31sWonpErryX/n8/fg+luJWMwW45wdB3 1KjaHdWNTb25TqqLzrBQoIicpfwuqxE= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-638-aRKoOQhAP125QnKMmBrPTA-1; Mon, 07 Jul 2025 10:48:45 -0400 X-MC-Unique: aRKoOQhAP125QnKMmBrPTA-1 X-Mimecast-MFC-AGG-ID: aRKoOQhAP125QnKMmBrPTA_1751899724 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 086981944A83; Mon, 7 Jul 2025 14:48:44 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.45.226.23]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 455F3180035E; Mon, 7 Jul 2025 14:48:38 +0000 (UTC) From: Gabriele Monaco To: linux-kernel@vger.kernel.org, Andrew Morton , David Hildenbrand , Ingo Molnar , Peter Zijlstra , Mathieu Desnoyers , "Paul E. McKenney" , linux-mm@kvack.org Cc: Gabriele Monaco , Ingo Molnar Subject: [PATCH v14 2/3] sched: Move task_mm_cid_work to mm timer Date: Mon, 7 Jul 2025 16:48:23 +0200 Message-ID: <20250707144824.117014-3-gmonaco@redhat.com> In-Reply-To: <20250707144824.117014-1-gmonaco@redhat.com> References: <20250707144824.117014-1-gmonaco@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" Currently, the task_mm_cid_work function is called in a task work triggered by a scheduler tick to frequently compact the mm_cids of each process. This can delay the execution of the corresponding thread for the entire duration of the function, negatively affecting the response in case of real time tasks. In practice, we observe task_mm_cid_work increasing the latency of 30-35us on a 128 cores system, this order of magnitude is meaningful under PREEMPT_RT. Run the task_mm_cid_work in a new timer connected to the mm_struct rather than in the task context before returning to userspace. This timer is initialised with the mm and disabled before freeing it. The timer is armed while returning to userspace in __rseq_handle_notify_resume, with an expiration of MM_CID_SCAN_DELAY. To make sure this happens predictably also on long running tasks, trigger a call to __rseq_handle_notify_resume also from the scheduler tick if the runtime exceeded a 100ms threshold. The main advantage of this change is that the function can be offloaded to a different CPU and even preempted by RT tasks. Moreover, this new behaviour is more predictable with periodic tasks with short runtime, which may rarely run during a scheduler tick. Now, the timer is always scheduled when the task returns to userspace. The timer is disabled during mmdrop, since the function cannot sleep in all kernel configurations, we cannot wait for a possibly running timer to terminate. Make sure the mm is valid in case the task is terminating by reserving it with mmgrab/mmdrop, returning prematurely if the timer handler is really the last user while it gets to run. This situation is unlikely since the timer is not armed for exiting tasks, but it cannot be ruled out. Fixes: 223baf9d17f2 ("sched: Fix performance regression introduced by mm_ci= d") Signed-off-by: Gabriele Monaco --- include/linux/mm_types.h | 23 +++++++-- include/linux/sched.h | 8 ++- kernel/rseq.c | 2 + kernel/sched/core.c | 103 +++++++++++++++++++++------------------ kernel/sched/sched.h | 8 +-- 5 files changed, 88 insertions(+), 56 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index d6b91e8a66d6d..9c159cf70a16c 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -990,11 +990,11 @@ struct mm_struct { */ struct mm_cid __percpu *pcpu_cid; /* - * @mm_cid_next_scan: Next mm_cid scan (in jiffies). + * @mm_cid_next_scan: Last mm_cid scan (in jiffies). * - * When the next mm_cid scan is due (in jiffies). + * When the last mm_cid scan occurred (in jiffies). */ - unsigned long mm_cid_next_scan; + unsigned long mm_cid_last_scan; /** * @nr_cpus_allowed: Number of CPUs allowed for mm. * @@ -1017,6 +1017,10 @@ struct mm_struct { * mm nr_cpus_allowed updates. */ raw_spinlock_t cpus_allowed_lock; + /* + * @cid_timer: Timer to run the mm_cid scan. + */ + struct timer_list cid_timer; #endif #ifdef CONFIG_MMU atomic_long_t pgtables_bytes; /* size of all page tables */ @@ -1321,6 +1325,8 @@ enum mm_cid_state { MM_CID_LAZY_PUT =3D (1U << 31), }; =20 +extern void task_mm_cid_scan(struct timer_list *timer); + static inline bool mm_cid_is_unset(int cid) { return cid =3D=3D MM_CID_UNSET; @@ -1393,12 +1399,14 @@ static inline int mm_alloc_cid_noprof(struct mm_str= uct *mm, struct task_struct * if (!mm->pcpu_cid) return -ENOMEM; mm_init_cid(mm, p); + timer_setup(&mm->cid_timer, task_mm_cid_scan, TIMER_DEFERRABLE); return 0; } #define mm_alloc_cid(...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__)) =20 static inline void mm_destroy_cid(struct mm_struct *mm) { + timer_shutdown(&mm->cid_timer); free_percpu(mm->pcpu_cid); mm->pcpu_cid =3D NULL; } @@ -1420,6 +1428,11 @@ static inline void mm_set_cpus_allowed(struct mm_str= uct *mm, const struct cpumas WRITE_ONCE(mm->nr_cpus_allowed, cpumask_weight(mm_allowed)); raw_spin_unlock(&mm->cpus_allowed_lock); } + +static inline bool mm_cid_scan_pending(struct mm_struct *mm) +{ + return mm && timer_pending(&mm->cid_timer); +} #else /* CONFIG_SCHED_MM_CID */ static inline void mm_init_cid(struct mm_struct *mm, struct task_struct *p= ) { } static inline int mm_alloc_cid(struct mm_struct *mm, struct task_struct *p= ) { return 0; } @@ -1430,6 +1443,10 @@ static inline unsigned int mm_cid_size(void) return 0; } static inline void mm_set_cpus_allowed(struct mm_struct *mm, const struct = cpumask *cpumask) { } +static inline bool mm_cid_scan_pending(struct mm_struct *mm) +{ + return false; +} #endif /* CONFIG_SCHED_MM_CID */ =20 struct mmu_gather; diff --git a/include/linux/sched.h b/include/linux/sched.h index 4f78a64beb52c..e90bc52dece3e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1432,7 +1432,7 @@ struct task_struct { int last_mm_cid; /* Most recent cid in mm */ int migrate_from_cpu; int mm_cid_active; /* Whether cid bitmap is active */ - struct callback_head cid_work; + unsigned long last_cid_reset; /* Time of last reset in jiffies */ #endif =20 struct tlbflush_unmap_batch tlb_ubc; @@ -2277,4 +2277,10 @@ static __always_inline void alloc_tag_restore(struct= alloc_tag *tag, struct allo #define alloc_tag_restore(_tag, _old) do {} while (0) #endif =20 +#ifdef CONFIG_SCHED_MM_CID +extern void task_queue_mm_cid(struct task_struct *curr); +#else +static inline void task_queue_mm_cid(struct task_struct *curr) { } +#endif + #endif diff --git a/kernel/rseq.c b/kernel/rseq.c index b7a1ec327e811..9ce0f79e35bfb 100644 --- a/kernel/rseq.c +++ b/kernel/rseq.c @@ -441,6 +441,8 @@ void __rseq_handle_notify_resume(struct ksignal *ksig, = struct pt_regs *regs) } if (unlikely(rseq_update_cpu_node_id(t))) goto error; + if (!mm_cid_scan_pending(t->mm)) + task_queue_mm_cid(t); return; =20 error: diff --git a/kernel/sched/core.c b/kernel/sched/core.c index ec68fc686bd74..ed316f0a31d9d 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4542,7 +4542,6 @@ static void __sched_fork(unsigned long clone_flags, s= truct task_struct *p) p->wake_entry.u_flags =3D CSD_TYPE_TTWU; p->migration_pending =3D NULL; #endif - init_sched_mm_cid(p); } =20 DEFINE_STATIC_KEY_FALSE(sched_numa_balancing); @@ -10594,37 +10593,15 @@ static void sched_mm_cid_remote_clear_weight(stru= ct mm_struct *mm, int cpu, sched_mm_cid_remote_clear(mm, pcpu_cid, cpu); } =20 -static void task_mm_cid_work(struct callback_head *work) +void task_mm_cid_scan(struct timer_list *timer) { - unsigned long now =3D jiffies, old_scan, next_scan; - struct task_struct *t =3D current; struct cpumask *cidmask; - struct mm_struct *mm; + struct mm_struct *mm =3D container_of(timer, struct mm_struct, cid_timer); int weight, cpu; =20 - WARN_ON_ONCE(t !=3D container_of(work, struct task_struct, cid_work)); - - work->next =3D work; /* Prevent double-add */ - if (t->flags & PF_EXITING) - return; - mm =3D t->mm; - if (!mm) - return; - old_scan =3D READ_ONCE(mm->mm_cid_next_scan); - next_scan =3D now + msecs_to_jiffies(MM_CID_SCAN_DELAY); - if (!old_scan) { - unsigned long res; - - res =3D cmpxchg(&mm->mm_cid_next_scan, old_scan, next_scan); - if (res !=3D old_scan) - old_scan =3D res; - else - old_scan =3D next_scan; - } - if (time_before(now, old_scan)) - return; - if (!try_cmpxchg(&mm->mm_cid_next_scan, &old_scan, next_scan)) - return; + /* We are the last user, process already terminated. */ + if (atomic_read(&mm->mm_count) =3D=3D 1) + goto out_drop; cidmask =3D mm_cidmask(mm); /* Clear cids that were not recently used. */ for_each_possible_cpu(cpu) @@ -10636,35 +10613,65 @@ static void task_mm_cid_work(struct callback_head= *work) */ for_each_possible_cpu(cpu) sched_mm_cid_remote_clear_weight(mm, cpu, weight); + WRITE_ONCE(mm->mm_cid_last_scan, jiffies); +out_drop: + mmdrop(mm); } =20 -void init_sched_mm_cid(struct task_struct *t) +void task_tick_mm_cid(struct rq *rq, struct task_struct *t) { - struct mm_struct *mm =3D t->mm; - int mm_users =3D 0; + u64 rtime =3D t->se.sum_exec_runtime - t->se.prev_sum_exec_runtime; =20 - if (mm) { - mm_users =3D atomic_read(&mm->mm_users); - if (mm_users =3D=3D 1) - mm->mm_cid_next_scan =3D jiffies + msecs_to_jiffies(MM_CID_SCAN_DELAY); + /* + * If a task is running unpreempted for a long time, it won't get its + * mm_cid compacted and won't update its mm_cid value after a + * compaction occurs. + * For such a task, this function does two things: + * A) trigger the mm_cid recompaction, + * B) trigger an update of the task's rseq->mm_cid field at some point + * after recompaction, so it can get a mm_cid value closer to 0. + * A change in the mm_cid triggers an rseq_preempt. + * + * B occurs once after the compaction work completes, both A and B + * don't run as long as the compaction work is pending. + */ + if (!t->mm || (t->flags & (PF_EXITING | PF_KTHREAD)) || + mm_cid_scan_pending(t->mm)) + return; + if (rtime < RSEQ_UNPREEMPTED_THRESHOLD) + return; + if (time_after(t->mm->mm_cid_last_scan, t->last_cid_reset)) { + /* Update mm_cid field */ + int old_cid =3D t->mm_cid; + + if (!t->mm_cid_active) + return; + mm_cid_snapshot_time(rq, t->mm); + mm_cid_put_lazy(t); + t->last_mm_cid =3D t->mm_cid =3D mm_cid_get(rq, t, t->mm); + if (old_cid !=3D t->mm_cid) + rseq_preempt(t); + } else { + /* Trigger mm_cid recompaction */ + rseq_set_notify_resume(t); } - t->cid_work.next =3D &t->cid_work; /* Protect against double add */ - init_task_work(&t->cid_work, task_mm_cid_work); } =20 -void task_tick_mm_cid(struct rq *rq, struct task_struct *curr) +void task_queue_mm_cid(struct task_struct *curr) { - struct callback_head *work =3D &curr->cid_work; - unsigned long now =3D jiffies; + int requeued; =20 - if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || - work->next !=3D work) - return; - if (time_before(now, READ_ONCE(curr->mm->mm_cid_next_scan))) - return; - - /* No page allocation under rq lock */ - task_work_add(curr, work, TWA_RESUME); + /* + * @curr must be a user thread and the timer must not be pending. + * Access to this timer is not serialised across threads sharing the + * same mm: ensure racing threads don't postpone enqueued timers and + * don't mmgrab() if they didn't enqueue the timer themselves. + * mmgrab() is necessary to ensure the mm exists until the timer runs. + */ + requeued =3D timer_reduce(&curr->mm->cid_timer, + jiffies + msecs_to_jiffies(MM_CID_SCAN_DELAY)); + if (!requeued && timer_pending(&curr->mm->cid_timer)) + mmgrab(curr->mm); } =20 void sched_mm_cid_exit_signals(struct task_struct *t) diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 475bb5998295e..3e72323fbde06 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3606,14 +3606,14 @@ extern const char *preempt_modes[]; =20 #define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */ #define MM_CID_SCAN_DELAY 100 /* 100ms */ +#define RSEQ_UNPREEMPTED_THRESHOLD SCHED_MM_CID_PERIOD_NS =20 extern raw_spinlock_t cid_lock; extern int use_cid_lock; =20 extern void sched_mm_cid_migrate_from(struct task_struct *t); extern void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct = *t); -extern void task_tick_mm_cid(struct rq *rq, struct task_struct *curr); -extern void init_sched_mm_cid(struct task_struct *t); +extern void task_tick_mm_cid(struct rq *rq, struct task_struct *t); =20 static inline void __mm_cid_put(struct mm_struct *mm, int cid) { @@ -3809,6 +3809,7 @@ static inline int mm_cid_get(struct rq *rq, struct ta= sk_struct *t, int cid; =20 lockdep_assert_rq_held(rq); + t->last_cid_reset =3D jiffies; cpumask =3D mm_cidmask(mm); cid =3D __this_cpu_read(pcpu_cid->cid); if (mm_cid_is_valid(cid)) { @@ -3881,8 +3882,7 @@ static inline void switch_mm_cid(struct rq *rq, static inline void switch_mm_cid(struct rq *rq, struct task_struct *prev, = struct task_struct *next) { } static inline void sched_mm_cid_migrate_from(struct task_struct *t) { } static inline void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_= struct *t) { } -static inline void task_tick_mm_cid(struct rq *rq, struct task_struct *cur= r) { } -static inline void init_sched_mm_cid(struct task_struct *t) { } +static inline void task_tick_mm_cid(struct rq *rq, struct task_struct *t) = { } #endif /* !CONFIG_SCHED_MM_CID */ =20 extern u64 avg_vruntime(struct cfs_rq *cfs_rq); --=20 2.50.0 From nobody Tue Oct 7 18:22:22 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D0BB2E1745 for ; Mon, 7 Jul 2025 14:48:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751899738; cv=none; b=Asa4YRSbTbTBTmx/rwksWg57vrw2L/YYmDlWZcdHsB1rpRR1FskkHY5JPYptz0INzNVrf49oP80HNT4Wa/zl2vo6cdH/RJULiZpQjwpyrptZyDH3KFtOdRGNOs7Au6uKcLiLNusEelYrwsR8XYNp5ZhzaPjq/2EnurZEPR9zkvY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751899738; c=relaxed/simple; bh=uku4aOONR85zre0CdqjyAno8gdmf/zWiLAsZg7LF+bs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fCJJU8XHnepvjAXTk+W2VBrjqvTJpkhmRQtVyCqBA3z8fQWiGhFUYQ14BdIoTxjjJKWU141eSu1ho+Zz/cXEdmh8sQuKJgE9e8Ry1OXiyY9Ju4T+wM/Qtrd693/RmDMEtZB3F42Xet835AFn8Kan/iDSpgkUcr0TBMW7Ox5DxFs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=CEdOJTnv; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="CEdOJTnv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751899735; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DSX0JgSswZ8GN8DVqCddIesIoG7h6EG+2FOdxnmUnrg=; b=CEdOJTnvU7kOVMYxw+ljqd0H6hGCE6ZuN+mMiNfJXXDBhVmIN9yJTyWN7vUlFKNP8bx4P6 9GSB1/Iy44ZAeO/jAd2u7ETSy7p+eZoxdoLRykwu2HbVS01u8ZYg7v5u705A2oOZOXatrs iMIftN36wECQ3cX8plv3ezLC4X6Dh/w= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-591-kAefEtVoMyukDh37PeJ4IA-1; Mon, 07 Jul 2025 10:48:50 -0400 X-MC-Unique: kAefEtVoMyukDh37PeJ4IA-1 X-Mimecast-MFC-AGG-ID: kAefEtVoMyukDh37PeJ4IA_1751899729 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8D8DF18DA5CB; Mon, 7 Jul 2025 14:48:49 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.45.226.23]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3EBE1180045B; Mon, 7 Jul 2025 14:48:44 +0000 (UTC) From: Gabriele Monaco To: linux-kernel@vger.kernel.org, Mathieu Desnoyers , Peter Zijlstra , "Paul E. McKenney" , Shuah Khan , linux-kselftest@vger.kernel.org Cc: Gabriele Monaco , Shuah Khan , Ingo Molnar Subject: [PATCH v14 3/3] selftests/rseq: Add test for mm_cid compaction Date: Mon, 7 Jul 2025 16:48:24 +0200 Message-ID: <20250707144824.117014-4-gmonaco@redhat.com> In-Reply-To: <20250707144824.117014-1-gmonaco@redhat.com> References: <20250707144824.117014-1-gmonaco@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" A task in the kernel (task_mm_cid_work) runs somewhat periodically to compact the mm_cid for each process. Add a test to validate that it runs correctly and timely. The test spawns 1 thread pinned to each CPU, then each thread, including the main one, runs in short bursts for some time. During this period, the mm_cids should be spanning all numbers between 0 and nproc. At the end of this phase, a thread with high enough mm_cid (>=3D nproc/2) is selected to be the new leader, all other threads terminate. After some time, the only running thread should see 0 as mm_cid, if that doesn't happen, the compaction mechanism didn't work and the test fails. The test never fails if only 1 core is available, in which case, we cannot test anything as the only available mm_cid is 0. Reviewed-by: Mathieu Desnoyers Acked-by: Shuah Khan Signed-off-by: Gabriele Monaco --- tools/testing/selftests/rseq/.gitignore | 1 + tools/testing/selftests/rseq/Makefile | 2 +- .../selftests/rseq/mm_cid_compaction_test.c | 200 ++++++++++++++++++ 3 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/rseq/mm_cid_compaction_test.c diff --git a/tools/testing/selftests/rseq/.gitignore b/tools/testing/selfte= sts/rseq/.gitignore index 0fda241fa62b0..b3920c59bf401 100644 --- a/tools/testing/selftests/rseq/.gitignore +++ b/tools/testing/selftests/rseq/.gitignore @@ -3,6 +3,7 @@ basic_percpu_ops_test basic_percpu_ops_mm_cid_test basic_test basic_rseq_op_test +mm_cid_compaction_test param_test param_test_benchmark param_test_compare_twice diff --git a/tools/testing/selftests/rseq/Makefile b/tools/testing/selftest= s/rseq/Makefile index 0d0a5fae59547..bc4d940f66d40 100644 --- a/tools/testing/selftests/rseq/Makefile +++ b/tools/testing/selftests/rseq/Makefile @@ -17,7 +17,7 @@ OVERRIDE_TARGETS =3D 1 TEST_GEN_PROGS =3D basic_test basic_percpu_ops_test basic_percpu_ops_mm_ci= d_test param_test \ param_test_benchmark param_test_compare_twice param_test_mm_cid \ param_test_mm_cid_benchmark param_test_mm_cid_compare_twice \ - syscall_errors_test + syscall_errors_test mm_cid_compaction_test =20 TEST_GEN_PROGS_EXTENDED =3D librseq.so =20 diff --git a/tools/testing/selftests/rseq/mm_cid_compaction_test.c b/tools/= testing/selftests/rseq/mm_cid_compaction_test.c new file mode 100644 index 0000000000000..7ddde3b657dd6 --- /dev/null +++ b/tools/testing/selftests/rseq/mm_cid_compaction_test.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: LGPL-2.1 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" +#include "rseq.h" + +#define VERBOSE 0 +#define printf_verbose(fmt, ...) \ + do { \ + if (VERBOSE) \ + printf(fmt, ##__VA_ARGS__); \ + } while (0) + +/* 0.5 s */ +#define RUNNER_PERIOD 500000 +/* Number of runs before we terminate or get the token */ +#define THREAD_RUNS 5 + +/* + * Number of times we check that the mm_cid were compacted. + * Checks are repeated every RUNNER_PERIOD. + */ +#define MM_CID_COMPACT_TIMEOUT 10 + +struct thread_args { + int cpu; + int num_cpus; + pthread_mutex_t *token; + pthread_barrier_t *barrier; + pthread_t *tinfo; + struct thread_args *args_head; +}; + +static void __noreturn *thread_runner(void *arg) +{ + struct thread_args *args =3D arg; + int i, ret, curr_mm_cid; + cpu_set_t cpumask; + + CPU_ZERO(&cpumask); + CPU_SET(args->cpu, &cpumask); + ret =3D pthread_setaffinity_np(pthread_self(), sizeof(cpumask), &cpumask); + if (ret) { + errno =3D ret; + perror("Error: failed to set affinity"); + abort(); + } + pthread_barrier_wait(args->barrier); + + for (i =3D 0; i < THREAD_RUNS; i++) + usleep(RUNNER_PERIOD); + curr_mm_cid =3D rseq_current_mm_cid(); + /* + * We select one thread with high enough mm_cid to be the new leader. + * All other threads (including the main thread) will terminate. + * After some time, the mm_cid of the only remaining thread should + * converge to 0, if not, the test fails. + */ + if (curr_mm_cid >=3D args->num_cpus / 2 && + !pthread_mutex_trylock(args->token)) { + printf_verbose( + "cpu%d has mm_cid=3D%d and will be the new leader.\n", + sched_getcpu(), curr_mm_cid); + for (i =3D 0; i < args->num_cpus; i++) { + if (args->tinfo[i] =3D=3D pthread_self()) + continue; + ret =3D pthread_join(args->tinfo[i], NULL); + if (ret) { + errno =3D ret; + perror("Error: failed to join thread"); + abort(); + } + } + pthread_barrier_destroy(args->barrier); + free(args->tinfo); + free(args->token); + free(args->barrier); + free(args->args_head); + + for (i =3D 0; i < MM_CID_COMPACT_TIMEOUT; i++) { + curr_mm_cid =3D rseq_current_mm_cid(); + printf_verbose("run %d: mm_cid=3D%d on cpu%d.\n", i, + curr_mm_cid, sched_getcpu()); + if (curr_mm_cid =3D=3D 0) + exit(EXIT_SUCCESS); + usleep(RUNNER_PERIOD); + } + exit(EXIT_FAILURE); + } + printf_verbose("cpu%d has mm_cid=3D%d and is going to terminate.\n", + sched_getcpu(), curr_mm_cid); + pthread_exit(NULL); +} + +int test_mm_cid_compaction(void) +{ + cpu_set_t affinity; + int i, j, ret =3D 0, num_threads; + pthread_t *tinfo; + pthread_mutex_t *token; + pthread_barrier_t *barrier; + struct thread_args *args; + + sched_getaffinity(0, sizeof(affinity), &affinity); + num_threads =3D CPU_COUNT(&affinity); + tinfo =3D calloc(num_threads, sizeof(*tinfo)); + if (!tinfo) { + perror("Error: failed to allocate tinfo"); + return -1; + } + args =3D calloc(num_threads, sizeof(*args)); + if (!args) { + perror("Error: failed to allocate args"); + ret =3D -1; + goto out_free_tinfo; + } + token =3D malloc(sizeof(*token)); + if (!token) { + perror("Error: failed to allocate token"); + ret =3D -1; + goto out_free_args; + } + barrier =3D malloc(sizeof(*barrier)); + if (!barrier) { + perror("Error: failed to allocate barrier"); + ret =3D -1; + goto out_free_token; + } + if (num_threads =3D=3D 1) { + fprintf(stderr, "Cannot test on a single cpu. " + "Skipping mm_cid_compaction test.\n"); + /* only skipping the test, this is not a failure */ + goto out_free_barrier; + } + pthread_mutex_init(token, NULL); + ret =3D pthread_barrier_init(barrier, NULL, num_threads); + if (ret) { + errno =3D ret; + perror("Error: failed to initialise barrier"); + goto out_free_barrier; + } + for (i =3D 0, j =3D 0; i < CPU_SETSIZE && j < num_threads; i++) { + if (!CPU_ISSET(i, &affinity)) + continue; + args[j].num_cpus =3D num_threads; + args[j].tinfo =3D tinfo; + args[j].token =3D token; + args[j].barrier =3D barrier; + args[j].cpu =3D i; + args[j].args_head =3D args; + if (!j) { + /* The first thread is the main one */ + tinfo[0] =3D pthread_self(); + ++j; + continue; + } + ret =3D pthread_create(&tinfo[j], NULL, thread_runner, &args[j]); + if (ret) { + errno =3D ret; + perror("Error: failed to create thread"); + abort(); + } + ++j; + } + printf_verbose("Started %d threads.\n", num_threads); + + /* Also main thread will terminate if it is not selected as leader */ + thread_runner(&args[0]); + + /* only reached in case of errors */ +out_free_barrier: + free(barrier); +out_free_token: + free(token); +out_free_args: + free(args); +out_free_tinfo: + free(tinfo); + + return ret; +} + +int main(int argc, char **argv) +{ + if (!rseq_mm_cid_available()) { + fprintf(stderr, "Error: rseq_mm_cid unavailable\n"); + return -1; + } + if (test_mm_cid_compaction()) + return -1; + return 0; +} --=20 2.50.0