From patchwork Fri Dec 13 09:54:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriele Monaco X-Patchwork-Id: 13906787 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2E48E7717D for ; Fri, 13 Dec 2024 09:55:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DDD96B0083; Fri, 13 Dec 2024 04:55:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 48DAA6B0085; Fri, 13 Dec 2024 04:55:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32E2B6B0088; Fri, 13 Dec 2024 04:55:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 138C56B0083 for ; Fri, 13 Dec 2024 04:55:18 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B8075AF357 for ; Fri, 13 Dec 2024 09:55:17 +0000 (UTC) X-FDA: 82889476698.03.460E6AC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf14.hostedemail.com (Postfix) with ESMTP id CDE3110000E for ; Fri, 13 Dec 2024 09:54:47 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hhPwhrEm; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf14.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=gmonaco@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734083704; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JAzbIE5lEihVL5VaG4JjJ0SMlqmrPzzCPdmb4qvN7Uk=; b=TjCurqb2YWuz3dHrNfcPU7hQ+c9ywKN6CEoudu+l83TmKlXkXqTE1lD7Q5/8afTUrUV5qW P1XFuGLa2Xy67o66f8F0tFGvueI3N3J8DQH0AIcOP1e19u9+L1XNIWynG9/EXhNKD6hl1o d3b0DEdRm4WF+X1KYsgtqvcOU+hS9V4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734083704; a=rsa-sha256; cv=none; b=yDadj2rbCRRVI72la963PDqlvLsZHPYquJe7yjBQR480e/BqEGyULzs1YgZs85/IgQj2te qqndvDrK8iDolcoo+ukj3GaKiJ8n0KsltXcV61qKyBlJnmQrDcnMmBkQ/DndsiO5nIR7gR pNt0ySFc5K5ipv4P/jGfeLs4sVyIu6k= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hhPwhrEm; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf14.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=gmonaco@redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734083714; 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=JAzbIE5lEihVL5VaG4JjJ0SMlqmrPzzCPdmb4qvN7Uk=; b=hhPwhrEmK1Qjp5HtkRGv2/xpMhnlQITZD/Vt46R1tu7BFgAG6qUlbi8DwcB5IGvO/2uSv2 Qy9qY1wUFXzrYYKZ8AQf/AIJ9O74GLqHuYciDbLwfFlP0f6nqTZgt3LLBjrjj7LuezsXvY yA6f1R2m/i1Cv43fPaexAaSh9tKT4DA= 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-333-bqgCsWERMqW_tJfsr6ogGA-1; Fri, 13 Dec 2024 04:55:11 -0500 X-MC-Unique: bqgCsWERMqW_tJfsr6ogGA-1 X-Mimecast-MFC-AGG-ID: bqgCsWERMqW_tJfsr6ogGA Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 C734C195605E; Fri, 13 Dec 2024 09:55:09 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.39.192.43]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7A17E195395A; Fri, 13 Dec 2024 09:55:05 +0000 (UTC) From: Gabriele Monaco To: Mathieu Desnoyers , Ingo Molnar , Peter Zijlstra , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Juri Lelli , Vincent Guittot , Mel Gorman , Shuah Khan , linux-kselftest@vger.kernel.org, Gabriele Monaco Subject: [PATCH v2 1/4] sched: Move task_mm_cid_work to mm delayed work Date: Fri, 13 Dec 2024 10:54:04 +0100 Message-ID: <20241213095407.271357-2-gmonaco@redhat.com> In-Reply-To: <20241213095407.271357-1-gmonaco@redhat.com> References: <20241213095407.271357-1-gmonaco@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Stat-Signature: p6j6a5seppg9ith9pbdfyqm7pieh4mo1 X-Rspamd-Queue-Id: CDE3110000E X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1734083687-915696 X-HE-Meta: U2FsdGVkX18Zk2jMUFXb72eo72wEBRqxihUVLI757Tk47sso9eUrvxT1qeHxBUiuhxMpyNP1fMaXoOlP4MnZzVMMdh9vAGLwWPJmpRCxKptxm4yWVYx4IpjTDrN5WnYLgn1DSOzWT5ya1oST1jl8ahwyK9Zp9YtXcw4pCTbDMkwNnv/IIfgCn982J/PI6HpolWWykxOpQPCQlQ7KMQqFahSwYgm9X+bBaq0sZKO5mAtqVA6kH/IxzIlfPSOqbjQVSDy0cy3MZlyLC7H7mD+c8DcbX4V42GkcON1DuBRgNUXgew7BV3touSGOvriwwTYJBy0XZ0wFDsjDa+rWkkaR3ejXH07Qjg/J5jdEQsaQG80vQmObBzhXrqFLstSlh3h7LMe9DVTLI3muFq42CDhvm0Eld11pRE6aOAyyik6vaCFaSTtVbfZY+oye53cdZPCBZwuq2sA6EERk0a0uoMNALD64zRxa9q3jbasLwSoYrs1wOXAgQVpwVLbk8V2KB3tyZXbvHXtORsnJfZ/Sl9bO9RwaKm7XVocZyJ6lXC9YxTO9Y/MUQNPntl56Na4hmN7bu24werOHTRWD/+1r5UD6gcTHnJ3hniyDxe2HA8fkRfEsWgMxezxtpxXz7woSMLbxPC/wtwlRd4kzTA5SG7IC7hMgPOUCPyDm6rhNwUjcBhiDZtG/TB9oHy1cDtL65HNzPLbvjFGncH2sQctg+bCgkABxzUo21xhtD+wFieEhNTNOqslTKBdsp5H0851kBKUEJs5uS8kfcXII1ogNWfuZtr2u2YUv6thEsvXTwXgbJ5F0SYz8tvIhv3G3skh+NbQmlpH2VzNdZigbIXNWJwMaffUPWaEdrGdCt0jDETMMvoiaIQJD88GirxX6xe2O1rW0BnhjOFy7yjHOGoYUtfxW01JssJr1KJKP6l5AHPn6JA4Z3Bd8qFKRS777t7JJUPSdEPcJ6XteS87kgBOf7My N+dD2HPt 6uKL+b/H1YUvtvbaO7d2p/XSXmQtzrEvVQllaeyzjXqgQhnViEYUZKvS6ZhGQNpuQzx0804Q/ivIA7cMUiJ9BZ7NtDj0IP13sD2TC3MZbb6aQk+qnMfgZnGLfhqjiNT6x5PoFA5OiU9VOAQvcJ1pMJ0uWCVVUM19E95XzQTmtVllW9BqLWcTiD6Jcpsdqi5mJpnNLVTwL0Sm74qWzxOIu0HDaPbyA9sPLMRQS4nw1N2W5mMpBQH9gKgZnZKeBUP2qX+ylhs1+fKLrhoxnGOeRLNoYw45u0fjydUFVZX7qZJPI9U8EY9DkLFKlZsn1A8Eio5A+lHIbJdomoT58zpR5e8C6ArvWbPEq03VTm54fG08yo3rE+anhBrDTKdLJUefq+J8885riFCvE9AtuO/7sqJBkgg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Currently, the task_mm_cid_work function is called in a task work triggered by a scheduler tick. This can delay the execution of the task for the entire duration of the function, negatively affecting the response of real time tasks. This patch runs the task_mm_cid_work in a new delayed work connected to the mm_struct rather than in the task context before returning to userspace. This delayed work is initialised while allocating the mm and disabled before freeing it, its execution is no longer triggered by scheduler ticks but run periodically based on the defined MM_CID_SCAN_DELAY. 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 could be more predictable in some situations since the delayed work is always scheduled with the same periodicity for each mm. Signed-off-by: Gabriele Monaco --- include/linux/mm_types.h | 11 +++++++++ include/linux/sched.h | 1 - kernel/sched/core.c | 51 ++++++---------------------------------- kernel/sched/sched.h | 7 ------ 4 files changed, 18 insertions(+), 52 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 7361a8f3ab68..92acb827fee4 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -856,6 +856,7 @@ struct mm_struct { * mm nr_cpus_allowed updates. */ raw_spinlock_t cpus_allowed_lock; + struct delayed_work mm_cid_work; #endif #ifdef CONFIG_MMU atomic_long_t pgtables_bytes; /* size of all page tables */ @@ -1144,11 +1145,16 @@ static inline void vma_iter_init(struct vma_iterator *vmi, #ifdef CONFIG_SCHED_MM_CID +#define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */ +#define MM_CID_SCAN_DELAY 100 /* 100ms */ + enum mm_cid_state { MM_CID_UNSET = -1U, /* Unset state has lazy_put flag set. */ MM_CID_LAZY_PUT = (1U << 31), }; +extern void task_mm_cid_work(struct work_struct *work); + static inline bool mm_cid_is_unset(int cid) { return cid == MM_CID_UNSET; @@ -1221,12 +1227,17 @@ static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct * if (!mm->pcpu_cid) return -ENOMEM; mm_init_cid(mm, p); + INIT_DELAYED_WORK(&mm->mm_cid_work, task_mm_cid_work); + mm->mm_cid_next_scan = jiffies + msecs_to_jiffies(MM_CID_SCAN_DELAY); + schedule_delayed_work(&mm->mm_cid_work, + msecs_to_jiffies(MM_CID_SCAN_DELAY)); return 0; } #define mm_alloc_cid(...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__)) static inline void mm_destroy_cid(struct mm_struct *mm) { + disable_delayed_work_sync(&mm->mm_cid_work); free_percpu(mm->pcpu_cid); mm->pcpu_cid = NULL; } diff --git a/include/linux/sched.h b/include/linux/sched.h index d380bffee2ef..5d141c310917 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1374,7 +1374,6 @@ 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; #endif struct tlbflush_unmap_batch tlb_ubc; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index c6d8232ad9ee..e3b27b73301c 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4516,7 +4516,6 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p) p->wake_entry.u_flags = CSD_TYPE_TTWU; p->migration_pending = NULL; #endif - init_sched_mm_cid(p); } DEFINE_STATIC_KEY_FALSE(sched_numa_balancing); @@ -5654,7 +5653,6 @@ void sched_tick(void) resched_latency = cpu_resched_latency(rq); calc_global_load_tick(rq); sched_core_tick(rq); - task_tick_mm_cid(rq, donor); scx_tick(rq); rq_unlock(rq, &rf); @@ -10520,22 +10518,14 @@ static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu, sched_mm_cid_remote_clear(mm, pcpu_cid, cpu); } -static void task_mm_cid_work(struct callback_head *work) +void task_mm_cid_work(struct work_struct *work) { unsigned long now = jiffies, old_scan, next_scan; - struct task_struct *t = current; struct cpumask *cidmask; - struct mm_struct *mm; + struct delayed_work *delayed_work = container_of(work, struct delayed_work, work); + struct mm_struct *mm = container_of(delayed_work, struct mm_struct, mm_cid_work); int weight, cpu; - SCHED_WARN_ON(t != container_of(work, struct task_struct, cid_work)); - - work->next = work; /* Prevent double-add */ - if (t->flags & PF_EXITING) - return; - mm = t->mm; - if (!mm) - return; old_scan = READ_ONCE(mm->mm_cid_next_scan); next_scan = now + msecs_to_jiffies(MM_CID_SCAN_DELAY); if (!old_scan) { @@ -10548,9 +10538,9 @@ static void task_mm_cid_work(struct callback_head *work) old_scan = next_scan; } if (time_before(now, old_scan)) - return; + goto out; if (!try_cmpxchg(&mm->mm_cid_next_scan, &old_scan, next_scan)) - return; + goto out; cidmask = mm_cidmask(mm); /* Clear cids that were not recently used. */ for_each_possible_cpu(cpu) @@ -10562,35 +10552,8 @@ static void task_mm_cid_work(struct callback_head *work) */ for_each_possible_cpu(cpu) sched_mm_cid_remote_clear_weight(mm, cpu, weight); -} - -void init_sched_mm_cid(struct task_struct *t) -{ - struct mm_struct *mm = t->mm; - int mm_users = 0; - - if (mm) { - mm_users = atomic_read(&mm->mm_users); - 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); -} - -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) - 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 | TWAF_NO_ALLOC); +out: + schedule_delayed_work(delayed_work, msecs_to_jiffies(MM_CID_SCAN_DELAY)); } void sched_mm_cid_exit_signals(struct task_struct *t) diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 76f5f53a645f..21be461ff913 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3581,16 +3581,11 @@ extern void sched_dynamic_update(int mode); #ifdef CONFIG_SCHED_MM_CID -#define SCHED_MM_CID_PERIOD_NS (100ULL * 1000000) /* 100ms */ -#define MM_CID_SCAN_DELAY 100 /* 100ms */ - extern raw_spinlock_t cid_lock; extern int use_cid_lock; 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); static inline void __mm_cid_put(struct mm_struct *mm, int cid) { @@ -3839,8 +3834,6 @@ 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 *curr) { } -static inline void init_sched_mm_cid(struct task_struct *t) { } #endif /* !CONFIG_SCHED_MM_CID */ extern u64 avg_vruntime(struct cfs_rq *cfs_rq); From patchwork Fri Dec 13 09:54:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriele Monaco X-Patchwork-Id: 13906788 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2551DE7717D for ; Fri, 13 Dec 2024 09:55:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6D9D6B0088; Fri, 13 Dec 2024 04:55:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B1DB16B0089; Fri, 13 Dec 2024 04:55:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E54D6B008A; Fri, 13 Dec 2024 04:55:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 7EE366B0088 for ; Fri, 13 Dec 2024 04:55:28 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2B282AECAA for ; Fri, 13 Dec 2024 09:55:28 +0000 (UTC) X-FDA: 82889477580.12.E86A2D7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf20.hostedemail.com (Postfix) with ESMTP id B2F511C000A for ; Fri, 13 Dec 2024 09:54:59 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CYZwMRSR; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf20.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=gmonaco@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734083714; a=rsa-sha256; cv=none; b=pG/XJRQ2R/fGUMvfbT1EtpdxJzJyOUeDTT8CfCgkrJWUVQGS8V6/sVTQqreSIwMAMGpYRp PJEDBdmFGYaj9lhKRiYFOsJ80uzP9bh1PkNWDlOalOpuCIlgXp+m/XjdCeeSWnc6vayXp5 BQBd/tu0dH36IEEoXUWh7GeDj5GmkzM= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CYZwMRSR; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf20.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=gmonaco@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734083714; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Zoc1rmo1ZxPdELSIwUBxgusiNNtCrVkbWCsaBTVZCO8=; b=fZvRHisx6fr6tCyQq0rVS7qZKjPKuiShe1q1iiIvKxIOcFDzX1+Th9fszLSICzsciXcuvh Bs4cKC2aO3vEGFvI8p9UwBU8tm+jOvx1YYNVevpF1WJzzlh7OncA1Yn+sUE/ihRrGFWAaR F/Rgc4709Vlc/zNZZdMFB1VgUTspi78= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734083725; 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=Zoc1rmo1ZxPdELSIwUBxgusiNNtCrVkbWCsaBTVZCO8=; b=CYZwMRSRwsqztNd1aIK/aTnQ/SiLVOUDQx5Vc7jlxWH7apg/AXzep+mhZvXa/ReUK/JpWC ADC71xd7TT6MfaVjNXqwQU9InOm4I+Or+gWVoEHYzxgkwt1qSw1W6A7NR9+UILAclkETwm mMMKTu5Rv5f9W1sprdOR8T6eFk+KThc= Received: from mx-prod-mc-01.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-515-uVqnbA6wPtKoeuvb26eCFw-1; Fri, 13 Dec 2024 04:55:22 -0500 X-MC-Unique: uVqnbA6wPtKoeuvb26eCFw-1 X-Mimecast-MFC-AGG-ID: uVqnbA6wPtKoeuvb26eCFw Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C8A721955BFE; Fri, 13 Dec 2024 09:55:20 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.39.192.43]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6B19E1953951; Fri, 13 Dec 2024 09:55:16 +0000 (UTC) From: Gabriele Monaco To: Mathieu Desnoyers , Ingo Molnar , Peter Zijlstra , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Juri Lelli , Vincent Guittot , Mel Gorman , Shuah Khan , linux-kselftest@vger.kernel.org, Gabriele Monaco Subject: [PATCH v2 2/4] sched: Remove mm_cid_next_scan as obsolete Date: Fri, 13 Dec 2024 10:54:05 +0100 Message-ID: <20241213095407.271357-3-gmonaco@redhat.com> In-Reply-To: <20241213095407.271357-1-gmonaco@redhat.com> References: <20241213095407.271357-1-gmonaco@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: B2F511C000A X-Stat-Signature: 6bgqwwhw7em5cdacrg33r9yx6u9c8mam X-HE-Tag: 1734083699-307731 X-HE-Meta: U2FsdGVkX1+fIqJGPIvHwi/duh9WekG8OXBLdvf5au1igDHwcJzldAdxJdzfWL4/GxE6lt9e1jjsjG01lKqw5/SZ9RQRDnp/NnGKoEbs4ixShi1Ewzgb6J8DbfZQhK0w1qhrQ9sZDHWLU7QN983gbCXY5+SQcvIEQqE23ziF4WEhTs/y+EPEMkTANP/ANS9J2E9l4gT+rgXaKdk2BMr6ljf9hqcL9eWbvyBWGCu6uympysaN9q3xMd5yTm9QCIr47wgnca1OAkzkPjiidpF8iZc9OUmEleQ3CioeJTkTGGMX4swP9KJ276Ei71JDwdKd3TKCbh4I9wvZcCr6AAJwY6gO+FqtYi504IYioPFuYrR/nwnC0z6U7xvTXGFv5Hy4f/IqsF1oM9bYebp2oUOP0S1TBRD4SEeYPn9UpZ/ggbz8KFDqMduUUmm5otq5MFOUl1FShl7mQi6Z/pAjuc2mDK+iqeTs8wm++BDatuhHjB/gMx8s8hefnFBsW7XiuD/usADmzfxlbC1xhzPSv8zXr9zhVGC5QOxzQykWiegN394F0DhMCEOm5bQethoIVkEY0wR04rcPN0qQYj4YzMtAzIOmumDkyVwLncxPGQc9HDPyoWEf+1dpDmmrKtrXinvTlSHxpne82cyDA+cyKOLTDXG4b5tIbkUd/6KpLyCMvxl0mAS6oDl6UZx58cC51r5PJq84vMFI7IZW/nSQIegiE/KwCqrfmWQXr/46f+VTeLTIwxnzb/y0bDG4K3Ij3sJaS1cD2N5ZqwaHOW5FhCdRa6WbsBRznfVS3hK73Y2jsrsA5nbP8YsrKI7AkM4dcR24t0nAiAkrBVmOCxFfpaEa5mTApvhYnwMnjJ2uvh7k86WJv6bU+PG21CFxCEL44w/eDVicleawRAfE4x+5EzsDW4jXfHncQ2OQ8vN8YtjOJ5NQ6J87oNh8RRteETNpFpZvkklcZql1EjghL+uRYXP KcQT5YMx yT89NBtCoIR8KCo+bgX89ydVX56bTE2pvn5VhkNjI8eNV2r4zJzW07d6Cg+Mgwi65gtfpVl08kCZzmVnairqvb82CoxsnOJQcvQ67ADn9y//dDRGUTDPo8jqtQM5eLeK6yJT3gC09PReafAuLEqybbY3bczmxUTbbdUffZE7kW3W0ascoX5FtcymPQWu/c0x2dEneKfKixO3eX3I3TnlR7vuAPBp1LYazcoO+UlMjeWnX2o6T4Aj0Q9EGIdr2Ko2Tkzfa6pgG+1AjlNhVvx1Oj/l+R7aykf9hqTLxOyfUmLT27XOr+lHPZcLsjyGvWUo2Um66U8CMd41WjLSiEjxlEM8GK4mKAHUFSg9f25JNERBreSVmT6Y7Oh7xMb2EPu9CjzK9vNuwHUTGrJvQnc8xE6IQcg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The checks for the scan time in task_mm_cid_work are now superfluous since the task runs in a delayed_work and the minimum periodicity is already implied. This patch removes those checks and the field from the mm_struct. Additionally, we include a simple check to quickly terminate the function if we have no work to be done (i.e. no mm_cid is allocated). This is helpful for tasks that sleep for a long time, but also for terminated task. We are no longer following the process' state, hence the function continues to run after a process terminates but before its mm is freed. Signed-off-by: Gabriele Monaco --- include/linux/mm_types.h | 7 ------- kernel/sched/core.c | 19 +++---------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 92acb827fee4..8a76a1c09234 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -829,12 +829,6 @@ struct mm_struct { * runqueue locks. */ struct mm_cid __percpu *pcpu_cid; - /* - * @mm_cid_next_scan: Next mm_cid scan (in jiffies). - * - * When the next mm_cid scan is due (in jiffies). - */ - unsigned long mm_cid_next_scan; /** * @nr_cpus_allowed: Number of CPUs allowed for mm. * @@ -1228,7 +1222,6 @@ static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct * return -ENOMEM; mm_init_cid(mm, p); INIT_DELAYED_WORK(&mm->mm_cid_work, task_mm_cid_work); - mm->mm_cid_next_scan = jiffies + msecs_to_jiffies(MM_CID_SCAN_DELAY); schedule_delayed_work(&mm->mm_cid_work, msecs_to_jiffies(MM_CID_SCAN_DELAY)); return 0; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index e3b27b73301c..30d78fe14eff 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -10520,28 +10520,15 @@ static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu, void task_mm_cid_work(struct work_struct *work) { - unsigned long now = jiffies, old_scan, next_scan; struct cpumask *cidmask; struct delayed_work *delayed_work = container_of(work, struct delayed_work, work); struct mm_struct *mm = container_of(delayed_work, struct mm_struct, mm_cid_work); int weight, cpu; - old_scan = READ_ONCE(mm->mm_cid_next_scan); - next_scan = now + msecs_to_jiffies(MM_CID_SCAN_DELAY); - if (!old_scan) { - unsigned long res; - - res = cmpxchg(&mm->mm_cid_next_scan, old_scan, next_scan); - if (res != old_scan) - old_scan = res; - else - old_scan = next_scan; - } - if (time_before(now, old_scan)) - goto out; - if (!try_cmpxchg(&mm->mm_cid_next_scan, &old_scan, next_scan)) - goto out; cidmask = mm_cidmask(mm); + /* Nothing to clear for now */ + if (cpumask_empty(cidmask)) + goto out; /* Clear cids that were not recently used. */ for_each_possible_cpu(cpu) sched_mm_cid_remote_clear_old(mm, cpu); From patchwork Fri Dec 13 09:54:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriele Monaco X-Patchwork-Id: 13906789 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA6E6E7717F for ; Fri, 13 Dec 2024 09:55:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 812896B008A; Fri, 13 Dec 2024 04:55:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C1EE6B008C; Fri, 13 Dec 2024 04:55:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 689B56B0092; Fri, 13 Dec 2024 04:55:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4A5D96B008A for ; Fri, 13 Dec 2024 04:55:43 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 10516437D4 for ; Fri, 13 Dec 2024 09:55:43 +0000 (UTC) X-FDA: 82889478126.30.BBD99D0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id 279C1C0008 for ; Fri, 13 Dec 2024 09:55:29 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=S8s62Uh3; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf10.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=gmonaco@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734083717; a=rsa-sha256; cv=none; b=iLzX1yjevjZNo6xZHZBWQNQZWXBjuswugDfn2FG0U0in0YETNxqvPN/t9U96xRw96OeeCG 1F7vESedmXMuli4T0JGDYs2a7grkXTRg6nTs4KUDC0vGn2tDOOs/z/PKaoTI87M1n/5cl7 7GsjI0iO6XoGC2ABJBexbCrWuQZ3+w0= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=S8s62Uh3; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf10.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=gmonaco@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734083717; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+blQkZ+8pXZzsTpzFalfD/SItztwEIvwWJSLIJ51tVA=; b=gYtt/RVZU54Z1XONWSI9emriQQ9U/S5nDQ1YNXPrYrJDdlZWl4M/Cf2xnfzmWDbHufv85r ZaawuPXhCDJnuBVyUU7GBu2CsZIQwJZtylgBp+r0Aq6zipASNXfTpjeukSILHVkxN5Sx5w 2zQoOFbgwblYWSBxpt4MPsnmF+tjcP4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734083740; 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=+blQkZ+8pXZzsTpzFalfD/SItztwEIvwWJSLIJ51tVA=; b=S8s62Uh3GQ2T9aJAbkxVncqdfyCinadrEeOCnSJkuCfiDkTico3OwjlF5WmEv2GzcLMkgH iA4fwX0jtC8V5xC3jkJiTDYsfZbQ0RKsUr0Yx8lEhJ8y8Mzd0Qr4Q7tgi1x984EoKyIxe9 N3ZJfZ66ynpshYqRWssXAXcZLreoibE= 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-624-lfytZsNrPn2sf6rsSwqFKA-1; Fri, 13 Dec 2024 04:55:37 -0500 X-MC-Unique: lfytZsNrPn2sf6rsSwqFKA-1 X-Mimecast-MFC-AGG-ID: lfytZsNrPn2sf6rsSwqFKA Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 385041955F2D; Fri, 13 Dec 2024 09:55:35 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.39.192.43]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9C471195394B; Fri, 13 Dec 2024 09:55:30 +0000 (UTC) From: Gabriele Monaco To: Mathieu Desnoyers , Ingo Molnar , Peter Zijlstra , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Juri Lelli , Vincent Guittot , Mel Gorman , Shuah Khan , linux-kselftest@vger.kernel.org, Marco Elver , Ingo Molnar , Gabriele Monaco Subject: [PATCH v2 3/4] sched: Compact RSEQ concurrency IDs with reduced threads and affinity Date: Fri, 13 Dec 2024 10:54:06 +0100 Message-ID: <20241213095407.271357-4-gmonaco@redhat.com> In-Reply-To: <20241213095407.271357-1-gmonaco@redhat.com> References: <20241213095407.271357-1-gmonaco@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 279C1C0008 X-Stat-Signature: 6msd6ao9z48xrmpwfwnnbtjo4tsy1ag1 X-Rspam-User: X-HE-Tag: 1734083729-719833 X-HE-Meta: U2FsdGVkX19f143nHCwPHsNnS0IYCRrJ6A8Hc7jzH+ZiNNGeqRNqLELPJuw7Ve2xM1TYqCCpSggkJsn6E8Pt/IJ9SB9oR3Tu99W0XyV5IPwtcs+1n2edJT76PqJwbkgsD8PfJ4gB/UtVlb8PfdzpKW1jcXeFIbweaG0x9jvNnpwaPTNX4knRWDhkfz6CDXlvKdEH3CIyAATUx8mQ09ikPsOThPgSgCf7UEbYl8IdlALM5g0yMYomxhyT9xUL6XJIMviuhv7vzyRQ/Z0BP+/uC2XAb8cMt2IiRhCRRTyKZs3slBqCRJi9X1GJ/s9BcwsNT5tLoFGLiinuNVEwNOnCqn1UTCzQ6v1fwNvD6Q4NILBHIfHuHPtZM0tkQkd7729sx9wfR4IJ6YTMFk9SQQh/wgTYbqDB7UoRhdfUTGaocTPdeYTEEPaTemE3HWxpV9howYZJ1weKK4cP2YTzV9jHP3BfBin10lEhwR0hi9zylDBTr6n7bb3e8lGiRNwVNUwfYcG+PpBjCFwJrxbqBPeGoc6b2oOd2Tm3SW2WLQodsM3J5jGmpsQkSnyHaiMIOMl++y6OMpYaasKqfN4EcJmCo7fUWLMO4uKODBRtRv2bJCpozS90B+HLEUUIEHIERixknuypJw43RrIksWQrqHLLA/cOFw1M01mLmUDgNSARR5ljYi+4PpQ7b6lg0h7a91z+KujdxM9HsspEfvII2470tILkoDwBMNx9sU+YHuD/gsC2b/2qosmmSb0RJKc7qyOTYPO2gBcjx+7KFQIPTThkQCQM9xsitt3qZaF+5VikqxxCbfz1uH9mj9plF115bwaldND2EPePCYS8okx0r+WVai6QTc4Cq7bvgrU2Eoo6+cNYF+EoDZO0dvI+PEoMtlAKxYneQgNnZdFulnAxo+znPTvl0Y0aXex/MN/MZdUDPzFLR2LRNqsprHz8DCHqmVJQDViT6ogexFiAjA5MBzG IqERyhUU 03wPO7BaQ0/hTMWTVKu+be9Pz0ugo3qtzRsi8rmqrcQqX/ZF2yswgonlynvQDQ4YrWyWgIKinUhllOr942Adg5Al13I5FQDaqtoUixXDPJI840UytEX6WmA7X4Lui+ZMcMBX1XNkE7gjpfnDGOGWjcNd+M38y2oNct3/+aL9XnhsbbocJjv5iyPWiFIE3ckjL2to1woA2IwNXlptSDW0x5MfwnSCBmdLvGgIDRkvLY6mRaD8BLnpdhCvglVRUhO1P8xnyydxD4Wbh8pB3tlsxleOszt/53mQScPNz6RKKrH8bo+Q7oPi6/S0s2+ILPWatkKGTJZRRFmjlmtIn8YMfgpP2JP50ef4LET45+8pVRNCZzZazu9ILj9ZIikScPSOBBsIwlb9vpMGhG84PWUm5rWesacMg5G47AtKrzb6sUDzsBtBj/z2S9nf8BDkW7v+NFR5V3a5vN36kd9C7u+ki4K/7NmSpGIeHYF8U95/gHbCkPRc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Mathieu Desnoyers When a process reduces its number of threads or clears bits in its CPU affinity mask, the mm_cid allocation should eventually converge towards smaller values. However, the change introduced by: commit 7e019dcc470f ("sched: Improve cache locality of RSEQ concurrency IDs for intermittent workloads") adds a per-mm/CPU recent_cid which is never unset unless a thread migrates. This is a tradeoff between: A) Preserving cache locality after a transition from many threads to few threads, or after reducing the hamming weight of the allowed CPU mask. B) Making the mm_cid upper bounds wrt nr threads and allowed CPU mask easy to document and understand. C) Allowing applications to eventually react to mm_cid compaction after reduction of the nr threads or allowed CPU mask, making the tracking of mm_cid compaction easier by shrinking it back towards 0 or not. D) Making sure applications that periodically reduce and then increase again the nr threads or allowed CPU mask still benefit from good cache locality with mm_cid. Introduce the following changes: * After shrinking the number of threads or reducing the number of allowed CPUs, reduce the value of max_nr_cid so expansion of CID allocation will preserve cache locality if the number of threads or allowed CPUs increase again. * Only re-use a recent_cid if it is within the max_nr_cid upper bound, else find the first available CID. Fixes: 7e019dcc470f ("sched: Improve cache locality of RSEQ concurrency IDs for intermittent workloads") Cc: Peter Zijlstra (Intel) Cc: Marco Elver Cc: Ingo Molnar Tested-by: Gabriele Monaco Signed-off-by: Mathieu Desnoyers Signed-off-by: Gabriele Monaco --- include/linux/mm_types.h | 7 ++++--- kernel/sched/sched.h | 25 ++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 8a76a1c09234..16076e70a6b9 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -837,10 +837,11 @@ struct mm_struct { */ unsigned int nr_cpus_allowed; /** - * @max_nr_cid: Maximum number of concurrency IDs allocated. + * @max_nr_cid: Maximum number of allowed concurrency + * IDs allocated. * - * Track the highest number of concurrency IDs allocated for the - * mm. + * Track the highest number of allowed concurrency IDs + * allocated for the mm. */ atomic_t max_nr_cid; /** diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 21be461ff913..f3b0d1d86622 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3652,10 +3652,28 @@ static inline int __mm_cid_try_get(struct task_struct *t, struct mm_struct *mm) { struct cpumask *cidmask = mm_cidmask(mm); struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid; - int cid = __this_cpu_read(pcpu_cid->recent_cid); + int cid, max_nr_cid, allowed_max_nr_cid; + /* + * After shrinking the number of threads or reducing the number + * of allowed cpus, reduce the value of max_nr_cid so expansion + * of cid allocation will preserve cache locality if the number + * of threads or allowed cpus increase again. + */ + max_nr_cid = atomic_read(&mm->max_nr_cid); + while ((allowed_max_nr_cid = min_t(int, READ_ONCE(mm->nr_cpus_allowed), + atomic_read(&mm->mm_users))), + max_nr_cid > allowed_max_nr_cid) { + /* atomic_try_cmpxchg loads previous mm->max_nr_cid into max_nr_cid. */ + if (atomic_try_cmpxchg(&mm->max_nr_cid, &max_nr_cid, allowed_max_nr_cid)) { + max_nr_cid = allowed_max_nr_cid; + break; + } + } /* Try to re-use recent cid. This improves cache locality. */ - if (!mm_cid_is_unset(cid) && !cpumask_test_and_set_cpu(cid, cidmask)) + cid = __this_cpu_read(pcpu_cid->recent_cid); + if (!mm_cid_is_unset(cid) && cid < max_nr_cid && + !cpumask_test_and_set_cpu(cid, cidmask)) return cid; /* * Expand cid allocation if the maximum number of concurrency @@ -3663,8 +3681,9 @@ static inline int __mm_cid_try_get(struct task_struct *t, struct mm_struct *mm) * and number of threads. Expanding cid allocation as much as * possible improves cache locality. */ - cid = atomic_read(&mm->max_nr_cid); + cid = max_nr_cid; while (cid < READ_ONCE(mm->nr_cpus_allowed) && cid < atomic_read(&mm->mm_users)) { + /* atomic_try_cmpxchg loads previous mm->max_nr_cid into cid. */ if (!atomic_try_cmpxchg(&mm->max_nr_cid, &cid, cid + 1)) continue; if (!cpumask_test_and_set_cpu(cid, cidmask)) From patchwork Fri Dec 13 09:54:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriele Monaco X-Patchwork-Id: 13906790 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B72BE77180 for ; Fri, 13 Dec 2024 09:56:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E1F46B0092; Fri, 13 Dec 2024 04:56:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2923A6B0093; Fri, 13 Dec 2024 04:56:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1348F6B0095; Fri, 13 Dec 2024 04:56:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E82846B0092 for ; Fri, 13 Dec 2024 04:56:02 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7790080CBA for ; Fri, 13 Dec 2024 09:56:02 +0000 (UTC) X-FDA: 82889478798.13.7163468 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf02.hostedemail.com (Postfix) with ESMTP id 2B2F98000F for ; Fri, 13 Dec 2024 09:55:12 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aQ+k69NE; spf=pass (imf02.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=gmonaco@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734083733; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4j0XdIbHKHBrG40Ri7lqb7eCCeV+N5WkxyRh1PruNqc=; b=slkrBQtbGrrTFeNNl/YBB6oe8L2YeUaFc8v0fW8wcJg+dhv/2qYF7tzLxfXJZNi67G6Bih Vx3wgnOju78dtivKXk5z4zQWDe3bQHeMsjPZJoKGTTXpsG4kXMN3MxEX0jxB0QrbSGDybe fUUptB1kCuS6ghJa4fdBnl2ye85Rn04= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734083733; a=rsa-sha256; cv=none; b=R9WwmsODIB/yjdEblI1RWiDlXkRFyFqO+hstiEKAiLZmqn3MK2WD6BtjGfRGLAMofyNs9y ufVvQWHtqOx1KjhrU50zE9D2aD7eKHcIo0rJ4CtW29fqG2X3pTPlUEoB8K6oIZkqB29S1b kCWFK7lqjrHSNlSTPglJR5Oyex3FlIk= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aQ+k69NE; spf=pass (imf02.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=gmonaco@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734083759; 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=4j0XdIbHKHBrG40Ri7lqb7eCCeV+N5WkxyRh1PruNqc=; b=aQ+k69NEi9odG+QFTufSS5yYrepbAhqEY2oSQBO1Jd6ncg9/PIEt3tq1ndqp/mKf27xBmw KcvK2LTGm5Sc5FtQh9hWye2mnuMOLEm7VekPH4dxqbj5STOy1Wvy5VZZp+N7RowAx1yj5/ aBAaw1cgInBOjnE7k6F1wJ7OAtCjUwE= 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-503-ljxb3HsdNUafwUf2srUAIw-1; Fri, 13 Dec 2024 04:55:54 -0500 X-MC-Unique: ljxb3HsdNUafwUf2srUAIw-1 X-Mimecast-MFC-AGG-ID: ljxb3HsdNUafwUf2srUAIw Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 1827E1955F25; Fri, 13 Dec 2024 09:55:53 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.39.192.43]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id F02EC195394B; Fri, 13 Dec 2024 09:55:48 +0000 (UTC) From: Gabriele Monaco To: Mathieu Desnoyers , Ingo Molnar , Peter Zijlstra , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Juri Lelli , Vincent Guittot , Mel Gorman , Shuah Khan , linux-kselftest@vger.kernel.org, Gabriele Monaco Subject: [PATCH v2 4/4] rseq/selftests: Add test for mm_cid compaction Date: Fri, 13 Dec 2024 10:54:07 +0100 Message-ID: <20241213095407.271357-5-gmonaco@redhat.com> In-Reply-To: <20241213095407.271357-1-gmonaco@redhat.com> References: <20241213095407.271357-1-gmonaco@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Rspamd-Queue-Id: 2B2F98000F X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: 7i63qh6u3mz8qfft9mnoma87iwzzg3jr X-HE-Tag: 1734083711-363568 X-HE-Meta: U2FsdGVkX1/G3q8M3hn/3DaGfanbpmq7dYZyYIgopfJtnbkj5UW2aX3oYfglrJHKFUQXvIgCWCqGLPfcAclycA3asURNyFwEYuA9iBB6x0QTQQHprJDxdzOarIWKalYKvZeTT8kZRkf3flYv87q2w4YwEjhOppTjqPEpgFk3vZH4/lzzf31qNazrs7t/h4S/tVcPr6zV/UjROYg7lGafjF7h6HGWgXy6Ye25DQCDgwcoi5oTCRvcVBsA3FZRq5GJN9m1tx2TFWat2yt5PxiSWN6bNp/Sw7vC1ybv4fTr60XW7lk0ZCysSEzgwjxe/XfBlg3kjs29dZa+4PCmZ4do31eo5eTVj0KC6GS+D6/ArwCvYJOs4VIgZdQMU76d/eoFnc2EBEpUPY4+TpyUuXO8Njhh7JunnIh86hr/uVqiDXr3TyJvtr6JVKMfWwy/GH6VZ8BezoQ3lAsES2roNAyRovs7vvJuBREgePKmrCr/yy96RpSuRjdNYtP8iCz0EDoc05PZokIpNugHouqR2bmEMY7lTx9CDPsBpNPvxMubZU0kss7O+F3r/KMtl7HBOhKjcv1Tlhu+AO1bhPKwbgMZaZjGm7MFaoJI3M851HQmLdcsEnlaSwrERprmDqvc3MId1yw6c3e5D/KfCN6pZ7kw6CyukBqnhJJs+vJiGERpOWAZuz+WXhXUDlccWZmOxR4z+ccVSFRkf1gNLQscHyIfk7K6uY3RwLS8U/w6n1OBD5zzB/crn19l/k1XjSsrFAmgOXD+7zQxq6chW8CQzOBN/SzrWOh4CnE/S6EZCpzgWwQHdjQIXbxTGh6cnBd1LB5J9n5rhQAfMDQbafjX9senwMp1duuHzFJ/nDYqunzCVN1b+mCv77L+xJIoLpA/yif3CaULkfyofjrAF+d6j2Y5b08nCWQrXPKp0FkrZF5svdvHknKyEKfHSVfWrAUQPWPYefkHDPxM/+ICqiyjWfr nK7jrMX6 acfcZEavzNUDOJGZG3qAT32jQ9dHo3IIkSro9Feazoww5sA1HxGIagqA6NMnfq5VOg/pxGgF0Agy2EvZ7RZ2vX0wCwt6WSwQh+qzgf5fmSL3ATJDkjt41yxg097t99DZPNL3VOmHk0VHDshKLsC5TAOfKWR0v8vDsLYP6gEBp3xRyovFK1SPxoi3ZY93Ekp5Wlyb3aA8jFP0chGSKIcKMyXLgNB/S6m0TvI0RsJ5uJUvNWEODwjjP8I/hOZKfOgJ4LyGphGPHOtpTvrLwgLUuchql0kmfGeYgj/i9QiY55RUej1Uf9s9rPnavFLc7jF//EKODq3U+JZm8ZoRYjQ0jtXhyrVRrYYavKdwZOJxROJcyKA5Q0QUGhHoWHrQYN/uMHpJPHyNhnk9ImKSsvz063Z3NWQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: A task in the kernel (task_mm_cid_work) runs somewhat periodically to compact the mm_cid for each process, this test tries to validate that it runs correctly and timely. The test spawns 1 thread pinned to each CPU, then each thread, including the main one, run 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 (> 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. Signed-off-by: Gabriele Monaco --- tools/testing/selftests/rseq/.gitignore | 1 + tools/testing/selftests/rseq/Makefile | 2 +- .../selftests/rseq/mm_cid_compaction_test.c | 157 ++++++++++++++++++ 3 files changed, 159 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/selftests/rseq/.gitignore index 16496de5f6ce..2c89f97e4f73 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/selftests/rseq/Makefile index 5a3432fceb58..ce1b38f46a35 100644 --- a/tools/testing/selftests/rseq/Makefile +++ b/tools/testing/selftests/rseq/Makefile @@ -16,7 +16,7 @@ OVERRIDE_TARGETS = 1 TEST_GEN_PROGS = basic_test basic_percpu_ops_test basic_percpu_ops_mm_cid_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 + param_test_mm_cid_benchmark param_test_mm_cid_compare_twice mm_cid_compaction_test TEST_GEN_PROGS_EXTENDED = librseq.so 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 000000000000..9bc7310c3cb5 --- /dev/null +++ b/tools/testing/selftests/rseq/mm_cid_compaction_test.c @@ -0,0 +1,157 @@ +// 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_CLEANUP_TIMEOUT 10 + +struct thread_args { + int num_cpus; + pthread_mutex_t token; + pthread_t *tinfo; +}; + +static void *thread_runner(void *arg) +{ + struct thread_args *args = arg; + int i, ret, curr_mm_cid; + + for (i = 0; i < THREAD_RUNS; i++) + usleep(RUNNER_PERIOD); + curr_mm_cid = 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 > args->num_cpus / 2 && + !pthread_mutex_trylock(&args->token)) { + printf_verbose("cpu%d has %d and will be the new leader\n", + sched_getcpu(), curr_mm_cid); + for (i = 0; i < args->num_cpus; i++) { + if (args->tinfo[i] == pthread_self()) + continue; + ret = pthread_join(args->tinfo[i], NULL); + if (ret) { + fprintf(stderr, + "Error: failed to join thread %d (%d): %s\n", + i, ret, strerror(ret)); + assert(ret == 0); + } + } + free(args->tinfo); + + for (i = 0; i < MM_CID_CLEANUP_TIMEOUT; i++) { + curr_mm_cid = rseq_current_mm_cid(); + printf_verbose("run %d: mm_cid %d on cpu%d\n", i, + curr_mm_cid, sched_getcpu()); + if (curr_mm_cid == 0) { + printf_verbose( + "mm_cids successfully compacted, exiting\n"); + pthread_exit(NULL); + } + usleep(RUNNER_PERIOD); + } + assert(false); + } + printf_verbose("cpu%d has %d and is going to terminate\n", + sched_getcpu(), curr_mm_cid); + pthread_exit(NULL); +} + +void test_mm_cid_compaction(void) +{ + cpu_set_t affinity, test_affinity; + int i, j, ret, num_threads; + pthread_t *tinfo; + struct thread_args args = { .token = PTHREAD_MUTEX_INITIALIZER }; + + sched_getaffinity(0, sizeof(affinity), &affinity); + CPU_ZERO(&test_affinity); + num_threads = CPU_COUNT(&affinity); + tinfo = calloc(num_threads, sizeof(*tinfo)); + if (!tinfo) { + fprintf(stderr, "Error: failed to allocate tinfo(%d): %s\n", + errno, strerror(errno)); + assert(ret == 0); + } + args.num_cpus = num_threads; + args.tinfo = tinfo; + if (num_threads == 1) { + printf_verbose( + "Running on a single cpu, cannot test anything\n"); + return; + } + for (i = 0, j = 0; i < CPU_SETSIZE && j < num_threads; i++) { + if (CPU_ISSET(i, &affinity)) { + ret = pthread_create(&tinfo[j], NULL, thread_runner, + &args); + if (ret) { + fprintf(stderr, + "Error: failed to create thread(%d): %s\n", + ret, strerror(ret)); + assert(ret == 0); + } + CPU_SET(i, &test_affinity); + pthread_setaffinity_np(tinfo[j], sizeof(test_affinity), + &test_affinity); + CPU_CLR(i, &test_affinity); + ++j; + } + } + printf_verbose("Started %d threads\n", num_threads); + + /* Also main thread will terminate if it is not selected as leader */ + thread_runner(&args); +} + +int main(int argc, char **argv) +{ + if (rseq_register_current_thread()) { + fprintf(stderr, + "Error: rseq_register_current_thread(...) failed(%d): %s\n", + errno, strerror(errno)); + goto error; + } + if (!rseq_mm_cid_available()) { + fprintf(stderr, "Error: rseq_mm_cid unavailable\n"); + goto error; + } + test_mm_cid_compaction(); + if (rseq_unregister_current_thread()) { + fprintf(stderr, + "Error: rseq_unregister_current_thread(...) failed(%d): %s\n", + errno, strerror(errno)); + goto error; + } + return 0; + +error: + return -1; +}