From patchwork Mon Dec 16 13:09: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: 13909717 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 BA660E7717F for ; Mon, 16 Dec 2024 13:09:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 416DC6B0093; Mon, 16 Dec 2024 08:09:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C6616B0095; Mon, 16 Dec 2024 08:09:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 240D46B0096; Mon, 16 Dec 2024 08:09:38 -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 067356B0093 for ; Mon, 16 Dec 2024 08:09:38 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7750B120E29 for ; Mon, 16 Dec 2024 13:09:29 +0000 (UTC) X-FDA: 82900853406.14.1CB1377 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf20.hostedemail.com (Postfix) with ESMTP id 0D6D11C0003 for ; Mon, 16 Dec 2024 13:08:56 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eRVuP1y3; spf=pass (imf20.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.133.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=1734354553; 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=U4feDC9Qj04RysuZYkGE0IUB/Cg8J4h0SsH5UXZ7tMw=; b=aTKtFR9BS/42FHp0KdcAA0zkFNasYAPBKGu2xm91jB87xL8p8m4sPkaieOYNHwHdb5WbLs pLoUR2bXq18zyKvL72u30oFoMCNgptYGYD4a+IFKm1rPRROY+LCLRCCBnpMzgeRFVwnl1U HsC0XUy86dxqAcPlAKWhI4mgMc7BerI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734354553; a=rsa-sha256; cv=none; b=B4kXeZDt4aeAahV3SKQIWtB9SwKvb32dIPjwUVC+ltMnX6eMmQv2AMlMzUMAlZqU3E5yj2 tpPN4E0X4Ht0TOMazgkZONTYo+s2OLKnA6WdcX5TmQ4uW2fCVVt2Vco4ZsQ7Ur0n13YMJT Wk57FYuFHOfvkZWiUg4FshaxRXtYpZY= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eRVuP1y3; spf=pass (imf20.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.133.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=1734354567; 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=U4feDC9Qj04RysuZYkGE0IUB/Cg8J4h0SsH5UXZ7tMw=; b=eRVuP1y3kLCFQXst1g88EYUzXE2kVcpMNyFnhHvcIUzHHC2VqB7doryCI39HYPYonjtx8J FxiAi38ClK8g8WQXbcCbrisUf3XNtGWoJ9tq2QrczVOM7m+pXUI6qTB+YtTRCIa7WvGRdu 4xGbIxUkUysvh/hfJMGxldoD7+t7p9Q= 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-454-pOrgOCcPOhqRlv22iGy-0Q-1; Mon, 16 Dec 2024 08:09:24 -0500 X-MC-Unique: pOrgOCcPOhqRlv22iGy-0Q-1 X-Mimecast-MFC-AGG-ID: pOrgOCcPOhqRlv22iGy-0Q 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 90C0D1955F44; Mon, 16 Dec 2024 13:09:20 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.39.193.102]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 665EA1955F43; Mon, 16 Dec 2024 13:09:17 +0000 (UTC) From: Gabriele Monaco To: Mathieu Desnoyers , Peter Zijlstra , Ingo Molnar , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Juri Lelli , Marco Elver , Gabriele Monaco Subject: [PATCH v3 1/3] sched: Compact RSEQ concurrency IDs with reduced threads and affinity Date: Mon, 16 Dec 2024 14:09:07 +0100 Message-ID: <20241216130909.240042-2-gmonaco@redhat.com> In-Reply-To: <20241216130909.240042-1-gmonaco@redhat.com> References: <20241216130909.240042-1-gmonaco@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 0D6D11C0003 X-Stat-Signature: mu7gxbxqiqcigphexhhm6t7kgstyqkab X-Rspam-User: X-HE-Tag: 1734354536-787009 X-HE-Meta: U2FsdGVkX1/UVywk1zNzuRT1RelOS9DJQIlXteDTYeh3CGiuxSeYplySb+f9qQ4EmLWxXh23hSRi5UdkepDtwQ7/48S1vJ6WWFp2mrfIKobQ3gVg1AQEg0Y5PkBwE4xF3Whs51k7XdIPoZ6wRM7D9QGnll2KJbuTsvyMB/XHpZViu1YRVqD+06dpAbS9zNN/dHULBqTtgQK/8CUNqUF/rlkuUPv9dI7Z6ChJNANEXruSb0mYPL3J6Ppl4n4uq5jSGf4oY0K2Vv5JGGFrhhwtu2T+SyMW4heEsu91avugj9oTLNGakvgae9+ifvVW1vSM7GWRwzx8z48XNZZWMDUQ8cDkH103S1EbVnpEgCiz9LgVcKWqWpEpsGEuMx0Sev+gLmFQF/le2LQRahr2R2XC0Mh2+/OUexwtBnZ8WTtq5STM5NzHRgUlQG6fSmM70Ybs6/0mbMsD320gIlZ5DRTlQDCL47Ep9jJ+Jx7eZ7hXlAZpoKXaWMlUGClfEdvr4B01cqBZ5MLGCek6wxdcpqgHiNZkOan2qa3hXF+Gklzy8KEKEupkemkqZInf6RyKYCY+mwX7FxKkCYJKCbSn3Mc2WQphL5rPoz7YMp+U9ewUTSE00XuKpXlvqZY9Ibn08RZVwWpTIH1zknV3TjJoZUEV2KdKxKhfQ9BTx59URBwSj//amhNJ7P1e++4PRPXpovtuzVi3iTYHIJp9v8L59QXbKihvteih7Gs1TgNP7Pw7XkZgCeUS4vW+3k8D19tZg95iPu0MkcD+2SoVUpIFDVzSm4wjtzZ4tLL465DX2S/iE7y95cPd2DLZ5//9Qo0c7it+BtgOEz6FhsQeH+TX8EafcF+blry6+4c+ICo6ayywRiVwfsQVHGSVwpPz+oCbIce4tPb+4WIIRNCBtug735PMg0P93JtJ2xSa8t8O/WV6mcsWFXvuD98kdsy6A5jyPOaS5RwUfxVI4pleq0aMfQ3 JiSqCM0/ vKNGVpZ1VYMJAmw7FTWNXtIrJaPQCJh+YAPvEXCZL2phVwkv2mJEtVLtaRLNdiQMgPlyNxUoljp2tE5ztOqSk4PM069cmV4bNjfhPbbuTzkBc1QLy3Kqjl7PvdwGiQjYlc0p3iKqPQifDwSPwMYWByzOiuOmAToQ2g+4AyA1wmsApmfPRmE+wpyvzYYyLy5PnDTNrOXbsJYN7ed7f0KrrNjZhQLXsFYgB/5o2s5TN8+ooFkhMoC9ieJw2x3OFLyTRWZShgMTRVfEeriWcO9OIlWY+ns/itZR8y3NhTLdd8YbnMI4s1TMwUrMyQyFBsC/No06ZtafVGqJ/o4BPVKOiUYFr4Rqxtss9d5foBpA5oIO+TxES0ZXAKok+P3FWZjaX5Xorh9OnvobeVFktxu/MvaMFHNU6ITtu2Va7BjybEteCOce9Ko9wybsjBb15I0NDubtITj6y/mF/UhigZCis8B7m7lxXTlP3uy7RpMr8lEPR4II= 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 7361a8f3ab68..d56948a74254 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -843,10 +843,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 76f5f53a645f..b50dcd908702 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3657,10 +3657,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 @@ -3668,8 +3686,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 Mon Dec 16 13:09:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriele Monaco X-Patchwork-Id: 13909718 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 546D7E7717F for ; Mon, 16 Dec 2024 13:09:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8D8C6B0096; Mon, 16 Dec 2024 08:09:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D3CDA6B0098; Mon, 16 Dec 2024 08:09:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C04D76B0099; Mon, 16 Dec 2024 08:09:55 -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 9F61A6B0096 for ; Mon, 16 Dec 2024 08:09:55 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 706D9C0EB0 for ; Mon, 16 Dec 2024 13:09:31 +0000 (UTC) X-FDA: 82900853196.04.C066034 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf19.hostedemail.com (Postfix) with ESMTP id BE92D1A0003 for ; Mon, 16 Dec 2024 13:08:58 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XcG70qpV; spf=pass (imf19.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=1734354556; 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=DDvTtsToy10cct7t5ck8viUzORJyYfeA1yptX9MXqDM=; b=yD4ntR1hwaJNIvBtMxJV/mu0E4hyiz3PQw9zGjaxd2G7u3Dit9XSR22CrzVrgrxY1iTMZh v0PitfpwsknnHqqxgb3ORq4rJGLT6jw3fUwXBjbEd+VXim/o0YY8f5F4qvuKkk6vfdHsCx KYVPM8X42DR50cfD47ngwPqd8nhHMZE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XcG70qpV; spf=pass (imf19.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=1734354556; a=rsa-sha256; cv=none; b=OtgAe42Ib75CjOaTJz6Pd31+AxdHRGayyqIYORw5agmWItV7mlKA1nr5mr8OT8BozmRvOx CQ71RvwZjneHEyiWJbV+cHhU7RFU1+I4dOmcY5LF+ceKsxGav8hax/4jKToiLJbbeLi+7r UUHiGWhCnuG+hYefxgSLruqW9hMmOto= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734354568; 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=DDvTtsToy10cct7t5ck8viUzORJyYfeA1yptX9MXqDM=; b=XcG70qpVqmcBIzgiO3oC0cVkyyuqEIpXnQqWolSDlhWSSv9nbGHPHEtP+hiBHhzFJ/QRN6 YdpGaqqjpR4LgtqxTYmdJ3Ltn56JjEsMvR50MKeMeR7VpU7B5cqItCMptqhrb4xuJ/F9oW GBzoB5yNpHahP/wMfygQrC2SaUopImY= 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-637-S-U76gCfN7SzpAq_Rouh5A-1; Mon, 16 Dec 2024 08:09:25 -0500 X-MC-Unique: S-U76gCfN7SzpAq_Rouh5A-1 X-Mimecast-MFC-AGG-ID: S-U76gCfN7SzpAq_Rouh5A 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6A7FC1956059; Mon, 16 Dec 2024 13:09:24 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.39.193.102]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id ED8A51956052; Mon, 16 Dec 2024 13:09:20 +0000 (UTC) From: Gabriele Monaco To: Mathieu Desnoyers , Peter Zijlstra , Ingo Molnar , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Juri Lelli , Gabriele Monaco , Andrew Morton Subject: [PATCH v3 2/3] sched: Move task_mm_cid_work to mm delayed work Date: Mon, 16 Dec 2024 14:09:08 +0100 Message-ID: <20241216130909.240042-3-gmonaco@redhat.com> In-Reply-To: <20241216130909.240042-1-gmonaco@redhat.com> References: <20241216130909.240042-1-gmonaco@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Rspamd-Queue-Id: BE92D1A0003 X-Stat-Signature: s839bwwkcc8dxija5bp9frioystuotwi X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1734354538-738830 X-HE-Meta: U2FsdGVkX1+Mb3/N8NxyIfUu1gKLFs3EVB7Lx9t5KT5PT0Eh7m+EnlUYLCi+8+YO64EhYkMh3sOTmrPV6YxsAdQSF7an2ht4UVS9dtkmAu5vjvCRnHEODfb0Zd6XtBwP3ihT/Y6VYc1DHhvrKEmHQ9Pf46oiXJX2p8rEKXfX9X5ELLJc9ufuqkK3q6dST2ViF3yTAHrojL0mKYL1R43huaff6jD8u5Mbekn9iwhZvqS1duu0c0olbluktu9rPBBcew4ML0MzNjXKQM/AIevrWYMV1c60XiWZEl4yzkZyl/H7Cio8M5714aoEbR321uAd8Z5xDQ1dnlIFHwVwYNtbzbYY/VV3bc4nP1L7kJGN+R/Gpj4obqL6UwOgxzkDDOPcQlM3I7YDTj7GTpKmCF9pj1w4U7hTgPQnQ3nER89UKeXVgQIgbzRBL+YiHNLJgvaORfGq0FZGenBGlik/bNhAUQ6WLwvq6yyxdwYpEPt9Kj82efcrH4YEPD0BaMWzkGqIdtfKvLAj4ozXbGRdmn4fLhPPO8Q2q/nJsZIjXLbBYbnR7ozRcJv1Aiu98WtvWOMIg2X1EQF++73nSRumA7Ut+ZfsNonP2AV8Oa+JCtx1b1pqZUSTiJthh43tpsNooT9X0V5K8F5Gh4Whrec/lBk3uIfE0AAaMk4tFedpkYbfBG8EsZ7Bz0VEdvD5Ds76Xb6KVVlru77RNms5/TnyA/RhVGLKGxOKYHVTBq26JA2bye1iFwMosvMOK/UMCcjuugaHiBnn/D69bIa3ntsjlv7mE0VyMHXjiKy5wI5Qi5X7VWYUvuGzaT0VUehGr7Xtjj6JKs7GJlmQswxy3P61E5oGFMadmvF+eLjMjYtIWTwkiG0YQCQIGwraicORWd7WZjmMeoeJK2GcHsaGgb9ygZR5N/UVBoC+faj2K0orraxta7eYo53bYzSg5qdUDGp0SjpvMffB2e38URsEV2HsbR+ f28IfVpI utBHUezXq9O+pffejQu2dPts0C/oRl1JlzDzSjyOjVqpdoJD7bcsmy5A/WntC073thFT4c5j76NbPf6Cwojk06obKsQZ6wjCEC319QjPZna3PK68PS9gOdJTniz1JkeXP8lslOs+M02T4FzjEa7bpCdx6e76Bbq/1DXezhm5qGD7W0D69oaxJX2261+zQ3FTKMjWyxFAerH4ZhLi2BYzuN8Y/R6gC3jVrmBSTtObNWt5qjbGkekyKVUOFF19IM6LNUQr1Nun8v/NmuLrtfUGEy4tjetnpr15UJ5ut7Lzvotx0o7vBibHZLzl7EY1Pazvdw2YwASIbF9AlzOnr5ieAVnVrz+1kiTLbuzEjsVxK495mOp9BoXaAdaQyaCwojYce1ApJ0r9HTxP7EtnzhvY+yHN6V2SgvV809yddRLkgTUM5kn87MfbRmwEs8pbIhX+NisC6SGMc3fI1xGWz2HKXnMUnkDfZmUtk6FJHGJ8fiD429FsIJ/8LMkODEeM7nozKbZyiXjVH+dmZ9S3hNbyb/89s2pRx62rEnQ3tYhvxS/l5FBqcZG9PDZcgZ6a4Dj8gC+Wnk9wHLoMU5gxMRi/wE8ZnR2Y2qyHIrYiF 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 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. 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 with periodic tasks with short runtime, which may rarely run during a scheduler tick. Now, the work is always scheduled with the same periodicity for each mm (though the periodicity is not guaranteed due to interference from other tasks, but mm_cid compaction is mostly best effort). To avoid excessively increased runtime, we quickly return from 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. Fixes: 223baf9d17f2 ("sched: Fix performance regression introduced by mm_cid") To: Mathieu Desnoyers Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Andrew Morton Signed-off-by: Gabriele Monaco --- include/linux/mm_types.h | 16 ++++++---- include/linux/sched.h | 1 - kernel/sched/core.c | 66 +++++----------------------------------- kernel/sched/sched.h | 7 ----- 4 files changed, 18 insertions(+), 72 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index d56948a74254..16076e70a6b9 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. * @@ -857,6 +851,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 */ @@ -1145,11 +1140,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; @@ -1222,12 +1222,16 @@ 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); + 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..30d78fe14eff 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,38 +10518,17 @@ 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) { - 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)) - return; - if (!try_cmpxchg(&mm->mm_cid_next_scan, &old_scan, next_scan)) - return; 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); @@ -10562,35 +10539,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 b50dcd908702..f3b0d1d86622 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) { @@ -3858,8 +3853,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 Mon Dec 16 13:09:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriele Monaco X-Patchwork-Id: 13909719 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 C0711E7717F for ; Mon, 16 Dec 2024 13:10:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 550976B0099; Mon, 16 Dec 2024 08:10:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FEC76B009A; Mon, 16 Dec 2024 08:10:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39FE96B009B; Mon, 16 Dec 2024 08:10:00 -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 1A2F56B0099 for ; Mon, 16 Dec 2024 08:10:00 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9505DA0ECA for ; Mon, 16 Dec 2024 13:09:34 +0000 (UTC) X-FDA: 82900852146.09.3ECA892 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 04AE140023 for ; Mon, 16 Dec 2024 13:09:05 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fbli0Etb; spf=pass (imf11.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.133.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=1734354559; 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=uZtDN64Xh2i1EouKa8fldcRUrzUUze6qMnOFRIwRqYA=; b=MNLs1esKPl7znnuyjY27WUz/fRlxe3IkV3NT3bzDWkQz7wz6slp9Gs5z0v/dn25Pf5g2fd NcIrrhLWFvTSzU4d6eiIkJyojTqIaBgOhRIa4vu3wQnCpZmodG+9YhWHKii+8U3RgpsUmE fPIP8Cx7LUhBuhD5euRLaZGTQ24/pnM= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fbli0Etb; spf=pass (imf11.hostedemail.com: domain of gmonaco@redhat.com designates 170.10.133.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=1734354559; a=rsa-sha256; cv=none; b=7pd9y4VcWHVplyZS6th0n2uZQiqbBO2mkDb1Tdzty3BlEaei3dOg5isRR+wcNlZbSyaviI sNwKkyGNNoFjBlr7gbO4fUWsNXkrkQt2WwPvYPpPtr/lXooLC4hY69s5vSjOeLgOwZJ1yg 82sragrVPtGIRsqs/G+73p45i3gj1gs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734354572; 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=uZtDN64Xh2i1EouKa8fldcRUrzUUze6qMnOFRIwRqYA=; b=fbli0Etb69xd47RyS7Jvx34l+cvwN+zS0JYqfzeyqmPvxpLa0zJ3IsEnTJ21xAMGC4MGGj UQ6IeuVFaRRDsg2qlG54wsSnH5u4IuecA/a8kTzolY5dZaN6ApksGiTXPR8fODP1Rq4qsi /KwT2t/5h3DudZVg0VARVDvjqZbRfD0= Received: from mx-prod-mc-04.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-70-MiaLR4PnNt-nwN18nbzrrQ-1; Mon, 16 Dec 2024 08:09:29 -0500 X-MC-Unique: MiaLR4PnNt-nwN18nbzrrQ-1 X-Mimecast-MFC-AGG-ID: MiaLR4PnNt-nwN18nbzrrQ 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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 101CD19560B3; Mon, 16 Dec 2024 13:09:28 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.com (unknown [10.39.193.102]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E51BA1956052; Mon, 16 Dec 2024 13:09:24 +0000 (UTC) From: Gabriele Monaco To: Mathieu Desnoyers , Peter Zijlstra , Ingo Molnar , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Juri Lelli , Gabriele Monaco , Shuah Khan Subject: [PATCH v3 3/3] rseq/selftests: Add test for mm_cid compaction Date: Mon, 16 Dec 2024 14:09:09 +0100 Message-ID: <20241216130909.240042-4-gmonaco@redhat.com> In-Reply-To: <20241216130909.240042-1-gmonaco@redhat.com> References: <20241216130909.240042-1-gmonaco@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Rspamd-Queue-Id: 04AE140023 X-Stat-Signature: xfpmmonr6aawf771afzq7a9kkyjshui7 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1734354545-840271 X-HE-Meta: U2FsdGVkX1/b40ZG0pJ0ADuS2AvnBlO46d5vqW1HGEbMHuk0Sn4PgtMT7B7T/ZLPHmzLAuOxda3Pc7GI63kFd2hU5BAbEDYPxTk7PsKWQGOqeaxpKTJq7r8OjfZzG1sp0Zvh/S67zZKsMvsCw8XZHPpmCU+JFqfDfylBBf6/SsMpfRB+mYlsH2ryKy8yeXdt7VBLiHxnRJjoCnPgUq++r72tCIJ/AI1i47s439soKZCFTgJmSXHBkbQsZfovvzz/6sd/X7pmnp0z8TS1X69ZpTrh0ATG4VNCdkw+UaixWdxRx3OsTwTEoC/S12utrkK+ekDJPcW/QFkoSVV9DjTJtOLjydNeaEaA9/OaUEvaAGa12qmmaBoZp6w60qStWQHhI/oNlY5osPd0Q/q03RAnlQiichwQ6he14GQT/ZIoAvySLAJqhjBWHELAfHixTz4+kPiAGxJLtcA1xVAZbcTX/5bESI/M+V4X0aPbdCzt8WHNZEfOe33WJehSEowNfwgVltop+sAsizJCHQHzG1ixyxaeiDi6apg4JGbLv4fQDKN3HkhgH7BRgLfwGM0Ev65jBcAmocTx09tKUjdiZwpQC2/b+Wee6ktQOWrJJC2cRBvtQfg1GJjmElYZpb+EB2suhfB12v4InBhI/zr3DMfviw4Ir5h4j3V6tXQqB9O63UxLJR+KjJwyY4IDC+UHtbQTrepFaQNfi554MXwsljd94ZFVFiVVr5JQVw5DD49yK3PgLaMJu9ea7Br+Fqb2QFVBzBkOoCCNLPAlGSDoicUwF4TS3RRF8kx4zcCw+ufizPAZvAis6yVe9UrgvDHqxa1dX+Z0PJFvOP69HMj//O/sMIFKGawEEQPkIJSjhe65EzyHEV2h547uqAP0Wcd+cvJwTdCPFA4Kk6beYS6OmbX5SkwadCqbo0DrelAYDV3FNi+FLQL/KTmaNjTnYAql1HiWjHCRNoC51yZFlsmsYQ3 PtJu32Tj F8zCcKF++vjVT7VJI4XizJlZZwtilFdsZp6vLWbZwNJcs6dNIpuEfA2lsl0Nq5k+Je946zP3MrOqSQ6upyAoBr6IPDi2jzePUI6y5ugzCkf+Hcj4VtBtXWZNew6h1byKTanVX+FzKs9b9VBrbDYg3H7Rwn+Zg5caltGvOpn9u3pjJD+bM6Fwz4e3glDY4Qe1lryAkDkVjTc5kgCI6ys2DwP1b3vBI+aLmj9VGy8wydyAI+N7RIS1yP+cFnOF4LR3v4AZzgHT/gABrUubLJlqVvLkpyxIDUn1kTUEdI/cOw+am+9SG/pY40m69JajgAvOnqKWVurNqXsAllMQfpibW/WTCWl4tUD766oZbbFc5W+o2M1rTBF64iZ9C1vRVDvEONLcbyO01vdZQKZL3O6j2PPCToQt0T5K1yabIoLLeFd4eVWxnUkKucp+5TFtmNF2L4P0tyvwf1QqOKnkdf1xu5v6b2Kmcyh9VpWq7 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, 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 (>= 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. To: Mathieu Desnoyers Cc: 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 | 190 ++++++++++++++++++ 3 files changed, 192 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..e5557b38a4e9 --- /dev/null +++ b/tools/testing/selftests/rseq/mm_cid_compaction_test.c @@ -0,0 +1,190 @@ +// 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_t *tinfo; + struct thread_args *args_head; +}; + +static void *thread_runner(void *arg) +{ + struct thread_args *args = arg; + int i, ret, curr_mm_cid; + cpu_set_t affinity; + + CPU_ZERO(&affinity); + CPU_SET(args->cpu, &affinity); + ret = pthread_setaffinity_np(pthread_self(), sizeof(affinity), &affinity); + if (ret) { + fprintf(stderr, + "Error: failed to set affinity to thread %d (%d): %s\n", + args->cpu, ret, strerror(ret)); + assert(ret == 0); + } + 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); + free(args->token); + free(args->args_head); + + for (i = 0; i < MM_CID_COMPACT_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; + int i, j, ret, num_threads; + pthread_t *tinfo; + pthread_mutex_t *token; + struct thread_args *args; + + sched_getaffinity(0, sizeof(affinity), &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 = calloc(num_threads, sizeof(*args)); + if (!args) { + fprintf(stderr, "Error: failed to allocate args(%d): %s\n", + errno, strerror(errno)); + assert(ret == 0); + } + token = calloc(num_threads, sizeof(*token)); + if (!token) { + fprintf(stderr, "Error: failed to allocate token(%d): %s\n", + errno, strerror(errno)); + assert(ret == 0); + } + if (num_threads == 1) { + printf_verbose( + "Running on a single cpu, cannot test anything\n"); + return; + } + pthread_mutex_init(token, NULL); + /* The main thread runs on CPU0 */ + for (i = 0, j = 0; i < CPU_SETSIZE && j < num_threads; i++) { + if (CPU_ISSET(i, &affinity)) { + args[j].num_cpus = num_threads; + args[j].tinfo = tinfo; + args[j].token = token; + args[j].cpu = i; + args[j].args_head = args; + if (!j) { + /* The first thread is the main one */ + tinfo[0] = pthread_self(); + ++j; + continue; + } + ret = pthread_create(&tinfo[j], NULL, thread_runner, + &args[j]); + if (ret) { + fprintf(stderr, + "Error: failed to create thread(%d): %s\n", + ret, strerror(ret)); + assert(ret == 0); + } + ++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]); +} + +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; +}