From mboxrd@z Thu Jan 1 00:00:00 1970 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 BEE02CD4F3C for ; Wed, 4 Sep 2024 21:39:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A2B96B00A5; Wed, 4 Sep 2024 17:39:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 34EC16B00DA; Wed, 4 Sep 2024 17:39:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F1D06B00BE; Wed, 4 Sep 2024 17:39:17 -0400 (EDT) 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 EA4CA6B0292 for ; Wed, 4 Sep 2024 17:39:16 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 77024A8DCD for ; Wed, 4 Sep 2024 21:39:16 +0000 (UTC) X-FDA: 82528371912.13.0D49738 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 807E540010 for ; Wed, 4 Sep 2024 21:39:14 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YkSTikQP; spf=pass (imf01.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@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=1725485906; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Y7sVZRygMt8WVBp9EDmrb53irfdcUMzJKYJx8kYK3v4=; b=B0xEgAs56dJDrQV5Tn3XtGt7eyj9PfJxGQcaOFyYKyFjPHaFAwYTJ76jpq8hzizbFyOPaB rN5BCbSuFNLnsTk7ybzs7Kk8obsg7qVzfa6LAjaIIEXCw+OnIkLjZ2MeGsLiuh94Nesdrv jG8JhFCKBAKiUoXSEzN0Uu72N+C7hro= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YkSTikQP; spf=pass (imf01.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725485906; a=rsa-sha256; cv=none; b=EMoHV74bSolyAm4ZIEfpMTaZXLIsKjqXlUZDEZUNbA2FAFnRggcBP6KeGZLeJPf82qO+oM xwXrN25D9XVyFinzReC0hOspbTCjLmzetvpAbcFmLPnl2ZaW7n4MWGzzIfe8XgzCpJ1wEG FDO8qK5pLYHSapi1AijLp76UVt0gobQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725485953; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y7sVZRygMt8WVBp9EDmrb53irfdcUMzJKYJx8kYK3v4=; b=YkSTikQP5mN58zcIzcpSo4LYRkTPxDI/qTMdYLg9mdKvbYFZf6f6ww/EwquFz9UF6vsweY uajiptZqIxItuV4g8Llp5as9NByUDRJEEU689UdiY+h2+4rUXyRoMZDpOtFbD5dBoNBhiZ IQg56ydU95Sm0djHOQF1S4T0Gk/WPUU= 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-671-X8Ry8Vj1N_27BdOOg6-sbQ-1; Wed, 04 Sep 2024 17:39:10 -0400 X-MC-Unique: X8Ry8Vj1N_27BdOOg6-sbQ-1 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 CEF601955F65; Wed, 4 Sep 2024 21:39:06 +0000 (UTC) Received: from [10.2.16.172] (unknown [10.2.16.172]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7B88F1956052; Wed, 4 Sep 2024 21:39:02 +0000 (UTC) Message-ID: Date: Wed, 4 Sep 2024 17:39:01 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v1 1/4] Introducing qpw_lock() and per-cpu queue & flush work To: Leonardo Bras , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Thomas Gleixner , Marcelo Tosatti Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org References: <20240622035815.569665-1-leobras@redhat.com> <20240622035815.569665-2-leobras@redhat.com> Content-Language: en-US From: Waiman Long In-Reply-To: <20240622035815.569665-2-leobras@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 807E540010 X-Stat-Signature: esosdqmgj4ue8dzap631i1qywijc3zs7 X-HE-Tag: 1725485954-806937 X-HE-Meta: U2FsdGVkX1+OVQS6X7UId1xvS/3w4RMfEAjqf82tUwDHlBOBmU8bP2r2J+c2ePYrLFwHNGn9IlrD7TEtZnIsPaEPSinK3cXhoBlgagcXm10HGVLBm2/gSY39obimD6hfVS7/xxC1dFs0T5LNuESxMAZdslO0ywgDO27wpshUJWk1Ab3NWCWRkWdj9d+DAZ0UrbZPNutQVT5ahMoKM/IxSkG5gLOZJPmoVk6GL/C39pgws8iu5b90MXUe1AurATePRQz9HHEa9sGoxQIITeq9oyG1AO5+T6+RGH7Ks+flULwjzs27ZBGnIgkIEKN7BhJhp9DpQDCaDBc+V8G7+8vNuFfePnMYkRRlyNDN4dJcbT3UHyKF8whAqBcOfaxwIS9ihyyt9pqUDuMbfbq4sv4r3A78BtSKVJD6Njr+M4xUcpGGYXeFPty5kKk/+LVAfJg/t/QWBTUMM9RjQXhSFXWoI2zi0A60cv0KaXWA8WhW5vWKuqE9M/4YTHSyzcNJxjKzSy1ajZHIOTq4CO6vnHfao69O7cTb8vYccAi4n0pUyQxd1ROITQgmGSDez0mrjo6+VCIGHzpt8KPB/jhgc0JIgySbxu38NHmPQf0IBWH8TA5Rnw/Muxdql+4VwtO9ekx1nWKIC0m7JbHXfnZyEGlYkJMxGwKKhGAEsZra33DjGJQqHt7ub27Pvl47KYVMHCgsDmzpzKvEZIdpmBDr9IkDeljT5ukVWd7c2bu2oPSEZqLVrrHlW7eClzBUKDZaWEVSs4kqnSFBDYqTt6OGl9VOCoeK6+khn2e6wVFdwqKl0lLN53MIwZcd7U4QYxthSxp27hcwXlAj51fePJQNcfgB0IxsUH/uLAllkYx5aTfbSDPkGUB/DXAygUu2Ws4YXyYA9vFSWLpBAnMEOIT55vls4DOsnoGdF0TViMJ83mqqDc+Mza8ibrgoya9N2cJmRpkxcG7ZvcuYH85Mv70nXMD fHktXMgi 0f0EIQxiOLJRT2Zdmow26ApLhriDXlmy7DcNdnabE5wi4WzW6+0MPLq0bAbVDQ1kiWkLw/AI+cI04Uc1jqvPpkWgO/3lFDl5ZVnzmypCutJK4cd1djTwvpUfGD5KvL7tsxiZdyOPzOYGlyleCGhD0k9XkGHV8IJn7rOdWg38Eoyoizgqz53XanrM6TDl6Oa/io/X6O7Alx+sGWDrbHlJb1I4cULSU8zrJBUde0Qtpz4N9ff0OXmB1DypBibxo8/wh1ThiIXMDx+w2u4kLXbpJWHuMyJ3sh26l6r2LbGPGHsCkJmuimi3xqroEpK6lLAIMzpDX4+PbDWrJznCNpP8LCvKKcBHMx1NYPIMOkDVrBiFxTP5MJwjXdyw2TrO9bLRSo6D3RG2Jnimmrve3Ss+o2uyUeAaodYbNJ3eQehz9xs2/ws+NqO0H0AlkhVIz95GOtYfQUGmc/hoPJ0A= 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: On 6/21/24 23:58, Leonardo Bras wrote: > Some places in the kernel implement a parallel programming strategy > consisting on local_locks() for most of the work, and some rare remote > operations are scheduled on target cpu. This keeps cache bouncing low since > cacheline tends to be mostly local, and avoids the cost of locks in non-RT > kernels, even though the very few remote operations will be expensive due > to scheduling overhead. > > On the other hand, for RT workloads this can represent a problem: getting > an important workload scheduled out to deal with some unrelated task is > sure to introduce unexpected deadline misses. > > It's interesting, though, that local_lock()s in RT kernels become > spinlock(). We can make use of those to avoid scheduling work on a remote > cpu by directly updating another cpu's per_cpu structure, while holding > it's spinlock(). > > In order to do that, it's necessary to introduce a new set of functions to > make it possible to get another cpu's per-cpu "local" lock (qpw_{un,}lock*) > and also the corresponding queue_percpu_work_on() and flush_percpu_work() > helpers to run the remote work. > > On non-RT kernels, no changes are expected, as every one of the introduced > helpers work the exactly same as the current implementation: > qpw_{un,}lock*() -> local_{un,}lock*() (ignores cpu parameter) > queue_percpu_work_on() -> queue_work_on() > flush_percpu_work() -> flush_work() > > For RT kernels, though, qpw_{un,}lock*() will use the extra cpu parameter > to select the correct per-cpu structure to work on, and acquire the > spinlock for that cpu. > > queue_percpu_work_on() will just call the requested function in the current > cpu, which will operate in another cpu's per-cpu object. Since the > local_locks() become spinlock()s in PREEMPT_RT, we are safe doing that. > > flush_percpu_work() then becomes a no-op since no work is actually > scheduled on a remote cpu. > > Some minimal code rework is needed in order to make this mechanism work: > The calls for local_{un,}lock*() on the functions that are currently > scheduled on remote cpus need to be replaced by qpw_{un,}lock_n*(), so in > RT kernels they can reference a different cpu. It's also necessary to use a > qpw_struct instead of a work_struct, but it just contains a work struct > and, in PREEMPT_RT, the target cpu. > > This should have almost no impact on non-RT kernels: few this_cpu_ptr() > will become per_cpu_ptr(,smp_processor_id()). > > On RT kernels, this should improve performance and reduce latency by > removing scheduling noise. > > Signed-off-by: Leonardo Bras > --- > include/linux/qpw.h | 88 +++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 88 insertions(+) > create mode 100644 include/linux/qpw.h > > diff --git a/include/linux/qpw.h b/include/linux/qpw.h > new file mode 100644 > index 000000000000..ea2686a01e5e > --- /dev/null > +++ b/include/linux/qpw.h > @@ -0,0 +1,88 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef _LINUX_QPW_H > +#define _LINUX_QPW_H > + > +#include "linux/local_lock.h" > +#include "linux/workqueue.h" > + > +#ifndef CONFIG_PREEMPT_RT > + > +struct qpw_struct { > + struct work_struct work; > +}; > + > +#define qpw_lock(lock, cpu) \ > + local_lock(lock) > + > +#define qpw_unlock(lock, cpu) \ > + local_unlock(lock) > + > +#define qpw_lock_irqsave(lock, flags, cpu) \ > + local_lock_irqsave(lock, flags) > + > +#define qpw_unlock_irqrestore(lock, flags, cpu) \ > + local_unlock_irqrestore(lock, flags) > + > +#define queue_percpu_work_on(c, wq, qpw) \ > + queue_work_on(c, wq, &(qpw)->work) > + > +#define flush_percpu_work(qpw) \ > + flush_work(&(qpw)->work) > + > +#define qpw_get_cpu(qpw) \ > + smp_processor_id() > + > +#define INIT_QPW(qpw, func, c) \ > + INIT_WORK(&(qpw)->work, (func)) > + > +#else /* !CONFIG_PREEMPT_RT */ > + > +struct qpw_struct { > + struct work_struct work; > + int cpu; > +}; > + > +#define qpw_lock(__lock, cpu) \ > + do { \ > + migrate_disable(); \ > + spin_lock(per_cpu_ptr((__lock), cpu)); \ > + } while (0) > + > +#define qpw_unlock(__lock, cpu) \ > + do { \ > + spin_unlock(per_cpu_ptr((__lock), cpu)); \ > + migrate_enable(); \ > + } while (0) Why there is a migrate_disable/enable() call in qpw_lock/unlock()? The rt_spin_lock/unlock() calls have already include a migrate_disable/enable() pair. > + > +#define qpw_lock_irqsave(lock, flags, cpu) \ > + do { \ > + typecheck(unsigned long, flags); \ > + flags = 0; \ > + qpw_lock(lock, cpu); \ > + } while (0) > + > +#define qpw_unlock_irqrestore(lock, flags, cpu) \ > + qpw_unlock(lock, cpu) > + > +#define queue_percpu_work_on(c, wq, qpw) \ > + do { \ > + struct qpw_struct *__qpw = (qpw); \ > + WARN_ON((c) != __qpw->cpu); \ > + __qpw->work.func(&__qpw->work); \ > + } while (0) > + > +#define flush_percpu_work(qpw) \ > + do {} while (0) > + > +#define qpw_get_cpu(w) \ > + container_of((w), struct qpw_struct, work)->cpu > + > +#define INIT_QPW(qpw, func, c) \ > + do { \ > + struct qpw_struct *__qpw = (qpw); \ > + INIT_WORK(&__qpw->work, (func)); \ > + __qpw->cpu = (c); \ > + } while (0) > + > +#endif /* CONFIG_PREEMPT_RT */ > +#endif /* LINUX_QPW_H */ You may also consider adding a documentation file about the qpw_lock/unlock() calls. Cheers, Longman