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 26DD0C3ABDA for ; Wed, 14 May 2025 18:42:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFF846B0088; Wed, 14 May 2025 14:42:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CAFEA6B0089; Wed, 14 May 2025 14:42:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B522B6B008A; Wed, 14 May 2025 14:42:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 943176B0088 for ; Wed, 14 May 2025 14:42:22 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2B2101A0FF8 for ; Wed, 14 May 2025 18:42:23 +0000 (UTC) X-FDA: 83442383766.04.F643146 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) by imf03.hostedemail.com (Postfix) with ESMTP id 5845620004 for ; Wed, 14 May 2025 18:42:21 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=L9zMuLin; spf=pass (imf03.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1747248141; 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=I1JZROuIroIvEzIUKiaMX+NW2Feg9mUr1VdRm7ju+Rc=; b=79RJsqgxySp8GnuY/OuNYFN6Kc7RnWbGolnUH5UDA8qTcuunZ4CuNcmX4AV8KtjIuUEJxG 0Ho2ikfF2zeaMDTgEg0BSKGT7LzDJm5r2DNql+kJe3FvNvKnE696Gwe+UDjQf9Cpr1e1zz D508VAnlM1VrvnW6xg4tlISPhIhyqrY= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=L9zMuLin; spf=pass (imf03.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747248141; a=rsa-sha256; cv=none; b=O2lKCivuJHqEiiw8JRjUUZMlaBGmUVceqmglvVPSlYt/fQWEinvU7qo76cvTtJ6rRUhjah Wv2piEDHenOmc0oTAhETbTslj8z4FjDse3n5owgFrP5VILDpINoDEtfXtantkn2O+NzY99 VmJ/rw5hFv9SZAyyUYn68GzXWNcwwkk= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1747248139; 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=I1JZROuIroIvEzIUKiaMX+NW2Feg9mUr1VdRm7ju+Rc=; b=L9zMuLin+KdSVhzEN0SGMJKsSLDXu2kWmM+sbQ2yv1PrTje8O/eF0Gy1pQg2iugldzHZ+T M7psiGTwbuAYe7PTX0V1VJcEOxef1/R4hP0pUoImfGXbfRl58KPC0yIkyYMzoNlqhdjxFf e8EZzcrUZShSAZ0loYLOk0N7rzBcYl8= From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Vlastimil Babka , Alexei Starovoitov , Sebastian Andrzej Siewior , Harry Yoo , Yosry Ahmed , bpf@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Meta kernel team Subject: [PATCH v2 1/7] memcg: memcg_rstat_updated re-entrant safe against irqs Date: Wed, 14 May 2025 11:41:52 -0700 Message-ID: <20250514184158.3471331-2-shakeel.butt@linux.dev> In-Reply-To: <20250514184158.3471331-1-shakeel.butt@linux.dev> References: <20250514184158.3471331-1-shakeel.butt@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Queue-Id: 5845620004 X-Rspamd-Server: rspam09 X-Stat-Signature: k6ichmpgkf9kej4t7x91pxc6b5iajrwp X-HE-Tag: 1747248141-254300 X-HE-Meta: U2FsdGVkX18lKPEg1QW6ymPc+Egbyy3cRdkKFFt4EQ4zCBxiqHCbAXLQP6mzsZHiE7C3sUTshhbqZjPyRWtGVKXgBhua3TiAzHRJ1TQAp08JVE0M4/oJ5q56wQoTRsbf2ULtjJpunhRjFoVZXABsf2/p9JwteaUqe3Kj9oQ4/nXXBd6MQ+Iwglo/BeDyEsaxulHkVo+Sz5IkPFnqIKoweepz+4M06CITTCo61DyKGKhKaSCXj9ehQS57ujxYQHrokcnJeKcyx/awCdfryfXz9dkW1gzRi1s6E5YEWogqcZdH+yIGo/KyZBNbuWX+4GuLokPluvVO+rdB6yMjdpUJuguexCU02hkJ5eMz3+7LwkjwSjlGRKjuSHTc+Wl8n9Y9wR3z+ZGAa/iAt7Xbgnf6DhewjUf1BwazXf64id/vsZ46LgthrG1Jh4WFehVWI3jY0LjtXY+YVbkQL0H+O4uG8eV15p9efMTXzoiNxYsWWvpa27Wwuoh5qSu9dUzsy0MhdV6rhDEdv1aKmAwSz6by9oE1SKr4HGKSxwIvdiVJOQ3bfTrctCgCYDxo+nUNyMMeO5zVaj07bT6v7VfznWwaXynPkZI1xrfRWGF98AgE1k44vJneK0NwWmVF0gnisS2dEvD8zPP4+AjQzT6U17Neg30+2+73koLRix4YWL5evWus83O6VZoWiv+VTkaRtsm2dwV0KCCsWnayPRntHfSCkCgRg3N4ZnrPOMdyQ1t5w5gr0DKzEeo3Pa/aG7gnQrHlHQSw1+u3lF1qlErqpddfscGdj3DuISSCcs7yQiROPCuI+V0LIHlA2uE7Ff7VlAcmD3Hb/KBt+UGktXbaevnr5BEQQtI+TYtzprp4KVCbEgslsc2bMeKa0znmVc/pIQAbPg14Kak8qe5KtKZNwn08sKAZD0AL2UiYrS2dNqHZkxW8gnjLWOTTuZswBBdVU904egKKTYWJJdCWJ5Wxe6l qEFjYcTx fW99T/qjg9uYqWI2xckj71R9CPCQ8MdIr08nW6EZk2j6RS1qxjph5M9WGg92o9y00zBRnHvLKKDJXXZDUzocnDvGuMJ4UpGUCrfQiqXl2Sutc8sS8SC0eonjN+qgo1Q9qZphHtPcEPdbUSrj0jD0CRDdCHEz7QDrR1b4Mt9SZCGjKNRr40Eb3OK1n10bQRQcEeifNIhvTuDkeXa0oRQ3LVZE3q7fbZcSlXu1S 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 function memcg_rstat_updated() is used to track the memcg stats updates for optimizing the flushes. At the moment, it is not re-entrant safe and the callers disabled irqs before calling. However to achieve the goal of updating memcg stats without irqs, memcg_rstat_updated() needs to be re-entrant safe against irqs. This patch makes memcg_rstat_updated() re-entrant safe using this_cpu_* ops. On archs with CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS, this patch is also making memcg_rstat_updated() nmi safe. Signed-off-by: Shakeel Butt Reviewed-by: Vlastimil Babka --- mm/memcontrol.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 89476a71a18d..2464a58fbf17 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -505,8 +505,8 @@ struct memcg_vmstats_percpu { unsigned int stats_updates; /* Cached pointers for fast iteration in memcg_rstat_updated() */ - struct memcg_vmstats_percpu *parent; - struct memcg_vmstats *vmstats; + struct memcg_vmstats_percpu __percpu *parent_pcpu; + struct memcg_vmstats *vmstats; /* The above should fit a single cacheline for memcg_rstat_updated() */ @@ -588,16 +588,21 @@ static bool memcg_vmstats_needs_flush(struct memcg_vmstats *vmstats) static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) { + struct memcg_vmstats_percpu __percpu *statc_pcpu; struct memcg_vmstats_percpu *statc; - int cpu = smp_processor_id(); + int cpu; unsigned int stats_updates; if (!val) return; + /* Don't assume callers have preemption disabled. */ + cpu = get_cpu(); + cgroup_rstat_updated(memcg->css.cgroup, cpu); - statc = this_cpu_ptr(memcg->vmstats_percpu); - for (; statc; statc = statc->parent) { + statc_pcpu = memcg->vmstats_percpu; + for (; statc_pcpu; statc_pcpu = statc->parent_pcpu) { + statc = this_cpu_ptr(statc_pcpu); /* * If @memcg is already flushable then all its ancestors are * flushable as well and also there is no need to increase @@ -606,14 +611,15 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) if (memcg_vmstats_needs_flush(statc->vmstats)) break; - stats_updates = READ_ONCE(statc->stats_updates) + abs(val); - WRITE_ONCE(statc->stats_updates, stats_updates); + stats_updates = this_cpu_add_return(statc_pcpu->stats_updates, + abs(val)); if (stats_updates < MEMCG_CHARGE_BATCH) continue; + stats_updates = this_cpu_xchg(statc_pcpu->stats_updates, 0); atomic64_add(stats_updates, &statc->vmstats->stats_updates); - WRITE_ONCE(statc->stats_updates, 0); } + put_cpu(); } static void __mem_cgroup_flush_stats(struct mem_cgroup *memcg, bool force) @@ -3691,7 +3697,7 @@ static void mem_cgroup_free(struct mem_cgroup *memcg) static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent) { - struct memcg_vmstats_percpu *statc, *pstatc; + struct memcg_vmstats_percpu *statc, __percpu *pstatc_pcpu; struct mem_cgroup *memcg; int node, cpu; int __maybe_unused i; @@ -3722,9 +3728,9 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent) for_each_possible_cpu(cpu) { if (parent) - pstatc = per_cpu_ptr(parent->vmstats_percpu, cpu); + pstatc_pcpu = parent->vmstats_percpu; statc = per_cpu_ptr(memcg->vmstats_percpu, cpu); - statc->parent = parent ? pstatc : NULL; + statc->parent_pcpu = parent ? pstatc_pcpu : NULL; statc->vmstats = memcg->vmstats; } -- 2.47.1