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 A6EB4C433F5 for ; Mon, 14 Feb 2022 16:46:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B19F6B0075; Mon, 14 Feb 2022 11:46:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 261D86B007B; Mon, 14 Feb 2022 11:46:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 129136B007D; Mon, 14 Feb 2022 11:46:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0142.hostedemail.com [216.40.44.142]) by kanga.kvack.org (Postfix) with ESMTP id 068466B0075 for ; Mon, 14 Feb 2022 11:46:03 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id BB8EA181AC9C6 for ; Mon, 14 Feb 2022 16:46:02 +0000 (UTC) X-FDA: 79141962564.30.255F834 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) by imf16.hostedemail.com (Postfix) with ESMTP id 0B6D2180005 for ; Mon, 14 Feb 2022 16:46:01 +0000 (UTC) Received: by mail-qk1-f178.google.com with SMTP id b22so14840209qkk.12 for ; Mon, 14 Feb 2022 08:46:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=QeA+W5UTyGJnefg3/KfG82NxGUq6cPYrEgrwO7WmFzM=; b=5yIaMy4IIema7swz7szhTLQYzc7X1qKWeHs6ELx7V9WGKbrNr1ofviMV6o/sHSGbrc uLfdlz7wGzHoatYQxr5nUEpWoHnlhNVKoTx2HouziKIlrkfigiEHFIAPKTW2HrjBuAlH 4XFgAruRhogInTYSVI6dL1oi1lzUBa6OiLOPwPpiwEJ+mjzCzdvNHNo4W/69YIyRh4ry AcHDh7S5iqjBNQRDfnJKvNm47fbVELJEri4BTe97JKH0o/AeRL4VFBF96C7RsZLgoXYX tr9X6J3+CGE9dz+4WrXcat40YjfIU8NbRMCgfq79U/Ui5WuIUUBNYdkLypy9vVxwo7LK hDZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=QeA+W5UTyGJnefg3/KfG82NxGUq6cPYrEgrwO7WmFzM=; b=Fn888ia8arJe7N6LxbRe61n1BHEtnzS+yaLoXOnI44JAR3x2yiEvq2WYbhOgge0H0h ZsOWUdvnjJtKwQdlb48n8f48B9wKt1iXi4UVrbBrD8tp/dozJ7X9htL5KBLAa2ELWPEj B2zNwdE66Ys7BVvyslp189b04eidiVM9c7BM3yRSlFi/eD2/PU/KbjEzLleG5pa+VHcF eiVN8L4qk1jaM1Guhf+2M80+K9jB+/HTSYBUukqX3L3RLvVC7ujEIfiMEKR51e28iQfW u0uayVC1o5S8VUzl0/HLzdajNqmC60AyoZjlxuXo9bckBBZP+Out4mgM/BQwY/wI0AfU Ah4Q== X-Gm-Message-State: AOAM533kyx2nmiqFl8kQY/E//7kX9WNUq7x+yA3acHji5vzscc/EyLbx tAOJycqrHasiNBuAlI235K7Q2g== X-Google-Smtp-Source: ABdhPJxaRpxsUPimstp80kDcnPMfUAmF6zMUOF3LhZJ+Y50hwjdMTKk9P406wXzEc4+JKmL68OY1zw== X-Received: by 2002:a05:620a:4546:: with SMTP id u6mr308674qkp.598.1644857161326; Mon, 14 Feb 2022 08:46:01 -0800 (PST) Received: from localhost (cpe-98-15-154-102.hvc.res.rr.com. [98.15.154.102]) by smtp.gmail.com with ESMTPSA id q22sm18799175qtw.52.2022.02.14.08.46.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 08:46:01 -0800 (PST) Date: Mon, 14 Feb 2022 11:46:00 -0500 From: Johannes Weiner To: Sebastian Andrzej Siewior Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Michal Hocko , Michal =?iso-8859-1?Q?Koutn=FD?= , Peter Zijlstra , Thomas Gleixner , Vladimir Davydov , Waiman Long Subject: Re: [PATCH v2 3/4] mm/memcg: Protect per-CPU counter by disabling preemption on PREEMPT_RT where needed. Message-ID: References: <20220211223537.2175879-1-bigeasy@linutronix.de> <20220211223537.2175879-4-bigeasy@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220211223537.2175879-4-bigeasy@linutronix.de> Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=5yIaMy4I; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf16.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.222.178 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 0B6D2180005 X-Stat-Signature: 13y9uzezwf7pnpqsin4xurkfociig8my X-HE-Tag: 1644857161-543852 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: On Fri, Feb 11, 2022 at 11:35:36PM +0100, Sebastian Andrzej Siewior wrote: > The per-CPU counter are modified with the non-atomic modifier. The > consistency is ensured by disabling interrupts for the update. > On non PREEMPT_RT configuration this works because acquiring a > spinlock_t typed lock with the _irq() suffix disables interrupts. On > PREEMPT_RT configurations the RMW operation can be interrupted. > > Another problem is that mem_cgroup_swapout() expects to be invoked with > disabled interrupts because the caller has to acquire a spinlock_t which > is acquired with disabled interrupts. Since spinlock_t never disables > interrupts on PREEMPT_RT the interrupts are never disabled at this > point. > > The code is never called from in_irq() context on PREEMPT_RT therefore > disabling preemption during the update is sufficient on PREEMPT_RT. > The sections which explicitly disable interrupts can remain on > PREEMPT_RT because the sections remain short and they don't involve > sleeping locks (memcg_check_events() is doing nothing on PREEMPT_RT). > > Disable preemption during update of the per-CPU variables which do not > explicitly disable interrupts. > > Signed-off-by: Sebastian Andrzej Siewior > --- > mm/memcontrol.c | 21 +++++++++++++++++++-- > 1 file changed, 19 insertions(+), 2 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index c1caa662946dc..466466f285cea 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -705,6 +705,8 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, > pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); > memcg = pn->memcg; > > + if (IS_ENABLED(CONFIG_PREEMPT_RT)) > + preempt_disable(); > /* Update memcg */ > __this_cpu_add(memcg->vmstats_percpu->state[idx], val); > > @@ -712,6 +714,8 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, > __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val); > > memcg_rstat_updated(memcg, val); > + if (IS_ENABLED(CONFIG_PREEMPT_RT)) > + preempt_enable(); > } I notice you didn't annoate __mod_memcg_state(). I suppose that is because it's called with explicit local_irq_disable(), and that disables preemption on rt? And you only need another preempt_disable() for stacks that rely on coming from spin_lock_irq(save)? That makes sense, but it's difficult to maintain. It'll easily break if somebody adds more memory accounting sites that may also rely on an irq-disabled spinlock somewhere. So better to make this an unconditional locking protocol: static void memcg_stats_lock(void) { #ifdef CONFIG_PREEMPT_RT preempt_disable(); #else VM_BUG_ON(!irqs_disabled()); #endif } static void memcg_stats_unlock(void) { #ifdef CONFIG_PREEMPT_RT preempt_enable(); #endif } and always use these around the counter updates.