All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Vlastimil Babka (SUSE)" <vbabka@kernel.org>
To: Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Shakeel Butt <shakeel.butt@linux.dev>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Muchun Song <muchun.song@linux.dev>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Thomas Gleixner <tglx@linutronix.de>
Subject: Re: [PATCH v2] memcg: simple cleanup of stats update functions
Date: Tue, 28 May 2024 10:10:50 +0200	[thread overview]
Message-ID: <69c0e008-ddde-4a38-b856-b765dc2b9745@kernel.org> (raw)
In-Reply-To: <20240528075623.oFcU1JLj@linutronix.de>

On 5/28/24 9:56 AM, Sebastian Andrzej Siewior wrote:
> On 2024-05-27 22:16:41 [-0700], Shakeel Butt wrote:
>> On Mon, May 27, 2024 at 06:34:24PM GMT, Vlastimil Babka (SUSE) wrote:
>> > On 5/27/24 5:22 PM, Sebastian Andrzej Siewior wrote:
>> > > On 2024-04-20 16:25:05 [-0700], Shakeel Butt wrote:
>> > >> mod_memcg_lruvec_state() is never called from outside of memcontrol.c
>> > >> and with always irq disabled. So, replace it with the irq disabled
>> > >> version and add an assert that irq is disabled in the caller.
>> > > 
>> > > unless PREEMPT_RT is enabled. In that case IRQs are not disabled as part
>> > > of local_lock_irqsave(&memcg_stock.stock_lock, …) leading to:
>> 
>> Sorry about that and thanks for the report.
> 
> no worries.
> 
>> > 
>> > But then the "interrupts are handled by a kernel thread that can sleep" part
>> > of RT also means it's ok to just have the stock_lock taken with no
>> > interrupts disabled as no actual raw interrupt handler will interrupt the
>> > holder and deadlock, right?
> 
> I *don't* know why the interrupts-disabled check is here. The
> memcg_stock.stock_lock is acquired on RT with interrupts enabled and
> never disables interrupts. The lock is never acquired in an hard
> interrupt (not threaded interrupt) context so there is never a deadlock.
> 
> Originally the interrupts were disabled in mod_memcg_lruvec_state()
> because the counter, it operates on, is per-CPU and relies on disabled
> interrupts because the operation is not atomic and the code can be run
> in interrupts context (on !RT). The __mod_memcg_lruvec_state() variant
> of it relied on interrupts being disabled by the caller. This "rely on"
> was part of a spinlock_t lock (or invoked from an interrupt handler, the
> memory is fading slowly away) which does not disable interrupts on
> PREEMPT_RT.
> So for that reason we ended up with __memcg_stats_lock() which disables
> preemption only on PREEMPT_RT to achieve the same level of "atomic"
> update.
> 
>> Thanks Vlastimil for jolting my memory on RT reasoning.
>> 
>> > > suggestions?
>> > 
>> > So in that case the appropriate thing would be to replace the assert with
>> > lockdep_assert_held(&memcg_stock.stock_lock);
>> > ?
>> > 
>> > It seems all the code paths leading here have that one.
>> > 
>> 
>> Yeah this seems right and reasonable. Should I send a fix or you want to
>> send it?
> 
> I don't mind sending a patch. I'm just not sure if the lock is the right
> thing to do. However it should ensure that interrupts are disabled on
> !RT for the sake of the counter update (if observed in IRQ context).

Looks like some places there use VM_WARN_ON_IRQS_ENABLED() that's turned off
for PREEMPT_RT, so maybe that's what should replace the current
lockdep_assert, perhaps together with
lockdep_assert_held(this_cpu_ptr(&memcg_stock.stock_lock));

But also __mod_memcg_lruvec_state() already has that VM_WARN_ON.

> Yeah, let me prepare something.
> 
> Sebastian



  reply	other threads:[~2024-05-28  8:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-20 23:25 [PATCH v2] memcg: simple cleanup of stats update functions Shakeel Butt
2024-04-21  2:12 ` T.J. Mercier
2024-05-27 15:22 ` Sebastian Andrzej Siewior
2024-05-27 16:34   ` Vlastimil Babka (SUSE)
2024-05-28  5:16     ` Shakeel Butt
2024-05-28  7:56       ` Sebastian Andrzej Siewior
2024-05-28  8:10         ` Vlastimil Babka (SUSE) [this message]
2024-05-28 10:19           ` Sebastian Andrzej Siewior
2024-05-28 12:17             ` Vlastimil Babka (SUSE)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=69c0e008-ddde-4a38-b856-b765dc2b9745@kernel.org \
    --to=vbabka@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.