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);