From patchwork Thu Dec 5 08:31:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriele Monaco X-Patchwork-Id: 13895587 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 746D0E7716C for ; Thu, 5 Dec 2024 15:33:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FBA06B00CA; Thu, 5 Dec 2024 10:19:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 556576B012F; Thu, 5 Dec 2024 10:19:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 557266B0116; Thu, 5 Dec 2024 10:19:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E7A0C6B0088 for ; Thu, 5 Dec 2024 03:31:57 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 963DE160F0C for ; Thu, 5 Dec 2024 08:31:57 +0000 (UTC) X-FDA: 82860236382.20.5B633BA Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 39AAA40009 for ; Thu, 5 Dec 2024 08:31:44 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bRiY3lEq; spf=pass (imf17.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=1733387499; 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:references:dkim-signature; bh=2vM6qUuCJYzyfHeB88GfpKyQAf0dwch2f0TqJnEPdAU=; b=LyFlLjyl0xJQhyw5iFlVauqIcAg3Jhjzz8HzfC7yo5o0WKm06bttWh2dRMdhik5uwKjPtB LJIjmCqxS29g/8mC0D3DcnxLuVIN0eX3aXYiVKUjyac0uAdfZ9nvY2B2XrP1/sG+bth/4H CdTuhHIXwH1T9jIUeUCrXWRtb+lHz4Y= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bRiY3lEq; spf=pass (imf17.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733387499; a=rsa-sha256; cv=none; b=ZOjoNynLhQzke1H/TdApHFWbsg1SKBUM1vridpELSD9WP6o31OSsLm647p+kar6+u4Qfg3 RtMR1ByxSzWUdlqKK6EX3rKeOJrPCdEcSiqS8o4RxiY9nPWnYPbFNWa8qflNkDZa6tUgfq aQssZqZnLYe6VbUVGe/46p4JPgrA/Tg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733387515; 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; bh=2vM6qUuCJYzyfHeB88GfpKyQAf0dwch2f0TqJnEPdAU=; b=bRiY3lEqx2gZuMmibX9vDXTSnl/2iMu9dv7O9La/KTJtdRaWaZlkeqX/bAnt4Rw167nQM4 1/7uqIMvhsh56ThVAbdXScp+fuufVMoro8/sFPSXem21dYMfHdivcf+0NdONxixe0JCG0z JhDDLYecF9eCtAVz0KBF132P2XfiBkA= 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-29-gvnJkFQXN86hWtSsyN7wzQ-1; Thu, 05 Dec 2024 03:31:53 -0500 X-MC-Unique: gvnJkFQXN86hWtSsyN7wzQ-1 X-Mimecast-MFC-AGG-ID: gvnJkFQXN86hWtSsyN7wzQ Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 499F31955DA8; Thu, 5 Dec 2024 08:31:52 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.39.192.112]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CBCB81955E87; Thu, 5 Dec 2024 08:31:42 +0000 (UTC) From: Gabriele Monaco To: Mathieu Desnoyers , Ingo Molnar , Peter Zijlstra , Andrew Morton , Mel Gorman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Juri Lelli , Vincent Guittot , Gabriele Monaco Subject: [PATCH] sched: Move task_mm_cid_work to mm delayed work Date: Thu, 5 Dec 2024 09:31:11 +0100 Message-ID: <20241205083110.180134-2-gmonaco@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 39AAA40009 X-Rspam-User: X-Stat-Signature: 18jgp9reeefauadte1fxthcf8s9rbbkc X-HE-Tag: 1733387504-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DChl5YcNCFoSIwfAC1YLh4aGmlTkK80Lbf1LwiOxtOgqxn+v+T2KMd7w7nbLg3zjF0R0eUtxcJk7CC+3BYLTufjFp16N90lC8hBbdeQogZaRWWLG67nM3hwJIUJ3Ek4cQQ7fCRxIf9/riDwOQ1N02MaO7th7Zc7/WWmrbWExjSQcs4ftkFbIjg18gncKxmVnIZrvaPxUcnm4t+zfm39bhXr6i84dyEGkYhRspkyoXQjMPcZhxqoqv7i5CNMfIV37t3As9XNInGyNKOJNnPhpjVpYr1R+/sE6JyggAENuo6ASPvr66Bz6bdNgPhRZ+26ytkJqFnBMVkyuJScdG+y+pMnYKo5+Zl0z+aB7lfe5yLEvNWTo= 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 running 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. On a busy system, this may mean the function gets called less often, but the current behaviour already doesn't provide guarantees. 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. This deprecate the "sched: improve task_mm_cid_work impact on isolated systems" patches by dropping the first patch and using a workqueue instead of RCU callbacks. Signed-off-by: Gabriele Monaco Link: https://lore.kernel.org/lkml/20241202140735.56368-1-gmonaco@redhat.com/ --- include/linux/mm_types.h | 5 +++++ kernel/sched/core.c | 37 +++++++------------------------------ 2 files changed, 12 insertions(+), 30 deletions(-) base-commit: feffde684ac29a3b7aec82d2df850fbdbdee55e4 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 7361a8f3ab68..38c567f06dce 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 */ @@ -1149,6 +1150,8 @@ enum mm_cid_state { 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 +1224,14 @@ 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); 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(&mm->mm_cid_work); free_percpu(mm->pcpu_cid); mm->pcpu_cid = NULL; } diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 95e40895a519..0c3a778c9cb5 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -5654,7 +5654,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 +10519,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) { @@ -10571,26 +10562,12 @@ void init_sched_mm_cid(struct task_struct *t) if (mm) { mm_users = atomic_read(&mm->mm_users); - if (mm_users == 1) + if (mm_users == 1) { 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)); + } } - 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); } void sched_mm_cid_exit_signals(struct task_struct *t)