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 85726C3ABD8 for ; Fri, 16 May 2025 18:21:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA7EE6B0253; Fri, 16 May 2025 14:21:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A567C6B0254; Fri, 16 May 2025 14:21:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 946BF6B0255; Fri, 16 May 2025 14:21:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 758686B0253 for ; Fri, 16 May 2025 14:21:04 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6A25D120FE7 for ; Fri, 16 May 2025 18:21:04 +0000 (UTC) X-FDA: 83449587648.16.4C4F0C2 Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) by imf02.hostedemail.com (Postfix) with ESMTP id 6931280007 for ; Fri, 16 May 2025 18:21:02 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mVIMsqdU; spf=pass (imf02.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.179 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=1747419662; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Aijqc4SJocxuCvVY9CsEnm5XULlfxcjJW9An8m2JIBY=; b=ns+Jg92OBeQVFv3G65zFxiyNvqveGE45Hr3GpRhc/+3XTIVakroILkKh/MbFA4X2v2sEbp Eyqw1E39SuI3jF1SpwixlseVbFv4BOqjIwLUaREx+5MGQ0aIsAjpQr7Jfu39l7LzwVdBuK wnp5kNqRM/ltoycCoxsD9exhMMqwdgs= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mVIMsqdU; spf=pass (imf02.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.179 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=1747419662; a=rsa-sha256; cv=none; b=Scrx/HYj33e273ARZdU+0279m+EiP65VuOJLMjR3hU2k9Hugu2f3gK7+L36EgD6Lhz/l3V +eplCs6gfHVcsz+ZwgNdfewu1UoEmlB2iS01U8hHaYjr2bRjO0lr9BZ1F8WmYupbDFnVox DE04PSR1YOc0Ejs6lpcbCr6yzsZtpHI= Date: Fri, 16 May 2025 11:20:54 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1747419660; 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: in-reply-to:in-reply-to:references:references; bh=Aijqc4SJocxuCvVY9CsEnm5XULlfxcjJW9An8m2JIBY=; b=mVIMsqdUT72lHBzpWvauLnHJ4jd4fkdk/1nUNgjk0VS+Ur+CB2Pz7/FcHSFYjcjp0ZftQ0 NEhL6o0e4Ec55ODNiv2k4zNXmBxUiR39LBdclyfurcbNK7DKL1Dz6LZ8nCDmw9+/MzyJCc YzeANwabOAeuUt2hyY1khgtOzJjRdAw= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Shakeel Butt To: Vlastimil Babka Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Alexei Starovoitov , Sebastian Andrzej Siewior , Harry Yoo , Yosry Ahmed , Peter Zijlstra , Mathieu Desnoyers , bpf@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Meta kernel team Subject: Re: [PATCH 1/5] memcg: disable kmem charging in nmi for unsupported arch Message-ID: <5utpywqjf47slmygpyfwmveabp65kzhq3sqf52j53hxs5owmxg@67ccuh4pmqsh> References: <20250516064912.1515065-1-shakeel.butt@linux.dev> <20250516064912.1515065-2-shakeel.butt@linux.dev> <050484a9-c08c-40d2-b431-76903a639222@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 6931280007 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: ynbhmn85twn4aii7okbs76oaj76ayyex X-HE-Tag: 1747419662-413820 X-HE-Meta: U2FsdGVkX1/FppKPHwTUQprhQkTpaR9ck6QHvfO/oxx1EdrejFz1MJYjiTP6pNJk4IkeZWoQKNEOoQ5MAo3zwxk5ogbjQEXhrEtOWkNrcz5wlqyfP0CKLjCmnTm2Ul/Yl+Gud283XAPemTpeKgphoiFSjJOoYtJwWMi7TFnSK13uHciY/4sAh2wEilfTl1LvehMFLBR7UePgGnH0xHHud2bNsYMPj/U1rW/Ex6OhWHiyQgj7YrhbI2f2IyYQ6AExiXZOEXj8MsN+oSSOhOJP1o5ugmm+Q4PrfV7oEE8WS+UdHvZmrGtVehzxGfCCYWNyq+JPcaPuu5/XHzy7eNhWWD/sH5OknEU/mviqiw9G5MSBMKj72rDF9zFdtVPY6v8YxIVzYa0UcVxIsgGyfwUWQimzyzhpxP6LoRLQjpvpUgGX6H+lp0BY1/HEXOEufrR3muYIyj7tMOGCtcRsuRtp7JY+tQFqEKVorWomu3Esker2ta1K83xIYTQu4qTPHeFL0/eDpe6pN1CHLo4BjG0yKfftPQDmouhb+U/KKNy+pbCi3ctYVCqMClhWMz/OgWoPY7TPZzyV/1xBwwUEBdllCETCMEDmxYbMoWkBIa+s0Ach9GDSmb9yTOFygKP6eVpDHl6RFr7meXhwxrl78JwiRHE0A+5BVssaa6whYDUi7z8MAuuIO1KzLZG9qfHbfuTxCZ08hcbhamUaMIL4pDbKqtOV3RvjHa3hYIkMuGXr4Yh7p+RZGNDnilwYvGY/6aXkiBB4+OmpepI3w7B0+HtgQKFHutluYf39H+iaiC1AyMzix9kg10LIQl5C2n2YWAJWn0CV/UGbdYGx7PhKrJYArEpEfz5watlKRHadqgIrruoPjQOuaANWcO2qpSFWVT+5p29IZWK0XuDEcF07bitI402PkBhuHPStWgwwRWg2SRBJrtBTRaRlw1l0ULqsi9/KvbMFU/dUTiW4aZicqw0 sjVjl8vw Wm/78toZ/Ox1YL42Rr0Mbc8L+X/d21Kwjd1xJX4ScoxNw6NXkcGDvHJCUiaG/W8W3FbcnC3JvuVe62+SbwIE6fPAncfz62P9Ur4k1ziRUim16POZF3OEAOJ+VCp9smgdQQYMuib37Hn41mL4TB/r8ZbNBgve/ON879YfkQ2TgZkIENP3Wj+Rjjxordg== 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 Fri, May 16, 2025 at 08:37:23AM -0700, Shakeel Butt wrote: > On Fri, May 16, 2025 at 11:30:17AM +0200, Vlastimil Babka wrote: > > On 5/16/25 08:49, Shakeel Butt wrote: > > > The memcg accounting and stats uses this_cpu* and atomic* ops. There are > > > archs which define CONFIG_HAVE_NMI but does not define > > > CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS and ARCH_HAVE_NMI_SAFE_CMPXCHG, so > > > memcg accounting for such archs in nmi context is not possible to > > > support. Let's just disable memcg accounting in nmi context for such > > > archs. > > > > > > Signed-off-by: Shakeel Butt > > > --- > > > include/linux/memcontrol.h | 5 +++++ > > > mm/memcontrol.c | 15 +++++++++++++++ > > > 2 files changed, 20 insertions(+) > > > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > > index f7848f73f41c..53920528821f 100644 > > > --- a/include/linux/memcontrol.h > > > +++ b/include/linux/memcontrol.h > > > @@ -62,6 +62,11 @@ struct mem_cgroup_reclaim_cookie { > > > > > > #ifdef CONFIG_MEMCG > > > > > > +#if defined(CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS) || \ > > > + !defined(CONFIG_HAVE_NMI) || defined(ARCH_HAVE_NMI_SAFE_CMPXCHG) > > > +#define MEMCG_SUPPORTS_NMI_CHARGING > > > +#endif > > > + > > > #define MEM_CGROUP_ID_SHIFT 16 > > > > > > struct mem_cgroup_id { > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > > index e17b698f6243..dface07f69bb 100644 > > > --- a/mm/memcontrol.c > > > +++ b/mm/memcontrol.c > > > @@ -2647,11 +2647,26 @@ static struct obj_cgroup *current_objcg_update(void) > > > return objcg; > > > } > > > > > > +#ifdef MEMCG_SUPPORTS_NMI_CHARGING > > > +static inline bool nmi_charging_allowed(void) > > > +{ > > > + return true; > > > +} > > > +#else > > > +static inline bool nmi_charging_allowed(void) > > > +{ > > > + return false; > > > +} > > > +#endif > > > + > > > __always_inline struct obj_cgroup *current_obj_cgroup(void) > > > { > > > struct mem_cgroup *memcg; > > > struct obj_cgroup *objcg; > > > > > > + if (in_nmi() && !nmi_charging_allowed()) > > > > Exchange the two as the latter is compile-time constant, so it can shortcut > > the in_nmi() check away in all the good cases? > > > > Oh I thought compiler would figure that out but now that I think about > it, it can only do so if the first condition does not have any > side-effects and though in_nmi() does not, I am not sure if compiler can > extract that information. > > I will fix this and make sure that compiler is doing the right thing. So, gcc 11.5 generates the same code irrespective of checking in_nmi() first or second i.e. avoid in_nmi() check altogether on x86_64. I will still rearrange the checks to not leave this optimization to compilers.