All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
@ 2026-11-25  4:39 Takuo Koguchi
  2026-11-25  4:47 ` 小口琢夫 / KOGUCHI,TAKUO
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Takuo Koguchi @ 2026-11-25  4:39 UTC (permalink / raw)
  To: cip-dev; +Cc: rostedt, Takuo Koguchi

On PREEMPT_RT, drain_local_stock() accesses the per-cpu memcg_stock
via this_cpu_ptr() without disabling preemption.

Accesses to memcg_stock from consume_stock() and refill_stock() are
already serialized using get_cpu_var()/put_cpu_var(). However,
drain_local_stock() still accesses the same per-cpu state without
equivalent protection.

As a result, drain_local_stock() may be preempted while operating on
memcg_stock, allowing another task running on the same CPU to observe
or modify the same per-cpu stock concurrently. This may result in
inconsistent updates of cached charges and stock counts.

Failures were observed as divergence between memcg accounting and
actual memory consumption. The divergence occurred in both positive
and negative directions, indicating corruption of accounting state
rather than a simple charge leak.

Under stress testing, the accounting divergence was reproducible and
was frequently accompanied by oops storms. Protecting
drain_local_stock() with get_cpu_var()/put_cpu_var() eliminated the
accounting divergence and the associated stability issues.

Use get_cpu_var()/put_cpu_var() in drain_local_stock() to serialize
accesses to the per-cpu memcg stock and make its protection consistent
with the other memcg stock users.

Signed-off-by: Takuo Koguchi <takuo.koguchi.sw@hitachi.com>
---
 mm/memcontrol.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index f9d80952723c..9b298ce10a7a 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1933,14 +1933,21 @@ static void drain_stock(struct memcg_stock_pcp *stock)
 }
 
 /*
- * This must be called under preempt disabled or must be called by
- * a thread which is pinned to local cpu.
+ * Drain this cpu's stock.  Called directly by drain_all_stock() for the
+ * local cpu and as a work item for the other cpus.  Preemption is
+ * disabled here because neither caller guarantees it on PREEMPT_RT:
+ * drain_all_stock() only holds get_cpu_light(), which is a plain
+ * barrier() without CONFIG_SMP, and the work item runs in a preemptible
+ * kworker.  consume_stock() and refill_stock() run with preemption
+ * disabled, so this keeps every user of the per-cpu stock serialized.
  */
 static void drain_local_stock(struct work_struct *dummy)
 {
-	struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
+	struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
+
 	drain_stock(stock);
 	clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
+	put_cpu_var(memcg_stock);
 }
 
 /*
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* RE: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
  2026-11-25  4:39 [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var() Takuo Koguchi
@ 2026-11-25  4:47 ` 小口琢夫 / KOGUCHI,TAKUO
  2026-11-25  8:27   ` Jan Kiszka
  2026-11-25  5:17 ` 小口琢夫 / KOGUCHI,TAKUO
  2026-11-25 15:13 ` Harry Yoo
  2 siblings, 1 reply; 9+ messages in thread
From: 小口琢夫 / KOGUCHI,TAKUO @ 2026-11-25  4:47 UTC (permalink / raw)
  To: 'Takuo Koguchi', cip-dev@lists.cip-project.org
  Cc: rostedt@goodmis.org

This patch is meant to be applied for linux-4.4.y-cip(-rt) only.

Takuo 

> -----Original Message-----
> From: Takuo Koguchi <takuo.koguchi@gmail.com>
> Sent: Wednesday, November 25, 2026 1:39 PM
> To: cip-dev@lists.cip-project.org
> Cc: rostedt@goodmis.org; 小口琢夫 / KOGUCHI,TAKUO
> <takuo.koguchi.sw@hitachi.com>
> Subject: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
> 
> On PREEMPT_RT, drain_local_stock() accesses the per-cpu memcg_stock via
> this_cpu_ptr() without disabling preemption.
> 
> Accesses to memcg_stock from consume_stock() and refill_stock() are already
> serialized using get_cpu_var()/put_cpu_var(). However,
> drain_local_stock() still accesses the same per-cpu state without equivalent
> protection.
> 
> As a result, drain_local_stock() may be preempted while operating on
> memcg_stock, allowing another task running on the same CPU to observe or
> modify the same per-cpu stock concurrently. This may result in inconsistent
> updates of cached charges and stock counts.
> 
> Failures were observed as divergence between memcg accounting and actual
> memory consumption. The divergence occurred in both positive and negative
> directions, indicating corruption of accounting state rather than a simple charge
> leak.
> 
> Under stress testing, the accounting divergence was reproducible and was
> frequently accompanied by oops storms. Protecting
> drain_local_stock() with get_cpu_var()/put_cpu_var() eliminated the accounting
> divergence and the associated stability issues.
> 
> Use get_cpu_var()/put_cpu_var() in drain_local_stock() to serialize accesses to
> the per-cpu memcg stock and make its protection consistent with the other
> memcg stock users.
> 
> Signed-off-by: Takuo Koguchi <takuo.koguchi.sw@hitachi.com>
> ---
>  mm/memcontrol.c | 13 ++++++++++---
>  1 file changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c index
> f9d80952723c..9b298ce10a7a 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1933,14 +1933,21 @@ static void drain_stock(struct memcg_stock_pcp
> *stock)  }
> 
>  /*
> - * This must be called under preempt disabled or must be called by
> - * a thread which is pinned to local cpu.
> + * Drain this cpu's stock.  Called directly by drain_all_stock() for
> + the
> + * local cpu and as a work item for the other cpus.  Preemption is
> + * disabled here because neither caller guarantees it on PREEMPT_RT:
> + * drain_all_stock() only holds get_cpu_light(), which is a plain
> + * barrier() without CONFIG_SMP, and the work item runs in a
> + preemptible
> + * kworker.  consume_stock() and refill_stock() run with preemption
> + * disabled, so this keeps every user of the per-cpu stock serialized.
>   */
>  static void drain_local_stock(struct work_struct *dummy)  {
> -	struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
> +	struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
> +
>  	drain_stock(stock);
>  	clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
> +	put_cpu_var(memcg_stock);
>  }
> 
>  /*
> --
> 2.34.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
  2026-11-25  4:39 [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var() Takuo Koguchi
  2026-11-25  4:47 ` 小口琢夫 / KOGUCHI,TAKUO
@ 2026-11-25  5:17 ` 小口琢夫 / KOGUCHI,TAKUO
  2026-11-25 15:13 ` Harry Yoo
  2 siblings, 0 replies; 9+ messages in thread
From: 小口琢夫 / KOGUCHI,TAKUO @ 2026-11-25  5:17 UTC (permalink / raw)
  To: cip-dev@lists.cip-project.org; +Cc: rostedt@goodmis.org

Dear reviews,

(Previous mail looks like blocked.)

This patch is meant to be applied for linux-4.4.y-cip(-rt) only.

Regards,

Takuo Koguchi
> -----Original Message-----
> From: Takuo Koguchi <takuo.koguchi@gmail.com>
> Sent: Wednesday, November 25, 2026 1:39 PM
> To: cip-dev@lists.cip-project.org
> Cc: rostedt@goodmis.org; 小口琢夫 / KOGUCHI,TAKUO
> <takuo.koguchi.sw@hitachi.com>
> Subject: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
> 
> On PREEMPT_RT, drain_local_stock() accesses the per-cpu memcg_stock via
> this_cpu_ptr() without disabling preemption.
> 
> Accesses to memcg_stock from consume_stock() and refill_stock() are already
> serialized using get_cpu_var()/put_cpu_var(). However,
> drain_local_stock() still accesses the same per-cpu state without equivalent
> protection.
> 
> As a result, drain_local_stock() may be preempted while operating on
> memcg_stock, allowing another task running on the same CPU to observe or
> modify the same per-cpu stock concurrently. This may result in inconsistent
> updates of cached charges and stock counts.
> 
> Failures were observed as divergence between memcg accounting and actual
> memory consumption. The divergence occurred in both positive and negative
> directions, indicating corruption of accounting state rather than a simple charge
> leak.
> 
> Under stress testing, the accounting divergence was reproducible and was
> frequently accompanied by oops storms. Protecting
> drain_local_stock() with get_cpu_var()/put_cpu_var() eliminated the accounting
> divergence and the associated stability issues.
> 
> Use get_cpu_var()/put_cpu_var() in drain_local_stock() to serialize accesses to
> the per-cpu memcg stock and make its protection consistent with the other
> memcg stock users.
> 
> Signed-off-by: Takuo Koguchi <takuo.koguchi.sw@hitachi.com>
> ---
>  mm/memcontrol.c | 13 ++++++++++---
>  1 file changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c index
> f9d80952723c..9b298ce10a7a 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1933,14 +1933,21 @@ static void drain_stock(struct memcg_stock_pcp
> *stock)  }
> 
>  /*
> - * This must be called under preempt disabled or must be called by
> - * a thread which is pinned to local cpu.
> + * Drain this cpu's stock.  Called directly by drain_all_stock() for
> + the
> + * local cpu and as a work item for the other cpus.  Preemption is
> + * disabled here because neither caller guarantees it on PREEMPT_RT:
> + * drain_all_stock() only holds get_cpu_light(), which is a plain
> + * barrier() without CONFIG_SMP, and the work item runs in a
> + preemptible
> + * kworker.  consume_stock() and refill_stock() run with preemption
> + * disabled, so this keeps every user of the per-cpu stock serialized.
>   */
>  static void drain_local_stock(struct work_struct *dummy)  {
> -	struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
> +	struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
> +
>  	drain_stock(stock);
>  	clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
> +	put_cpu_var(memcg_stock);
>  }
> 
>  /*
> --
> 2.34.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
  2026-11-25  4:47 ` 小口琢夫 / KOGUCHI,TAKUO
@ 2026-11-25  8:27   ` Jan Kiszka
  2026-11-25  8:40     ` Re: [cip-dev] " 小口琢夫 / KOGUCHI,TAKUO
  0 siblings, 1 reply; 9+ messages in thread
From: Jan Kiszka @ 2026-11-25  8:27 UTC (permalink / raw)
  To: 小口琢夫 / KOGUCHI,TAKUO,
	'Takuo Koguchi', cip-dev@lists.cip-project.org,
	Ulrich Hecht, Pavel Machek
  Cc: rostedt@goodmis.org

On 25.11.26 05:47, å°å£ç¢å¤« / KOGUCHIï¼TAKUO wrote:
> This patch is meant to be applied for linux-4.4.y-cip(-rt) only.
> 

I hope you know that 4.4-cip is very close to its retirement. There will
likely be final releases of vanilla and -rt in December, but then it's
going to be over. IOW: All users should by now be migrated to a newer
kernel, or they will be in troubles in 5 weeks from now.

That said:

> Takuo 
> 
>> -----Original Message-----
>> From: Takuo Koguchi <takuo.koguchi@gmail.com>
>> Sent: Wednesday, November 25, 2026 1:39 PM
>> To: cip-dev@lists.cip-project.org
>> Cc: rostedt@goodmis.org; 小口琢夫 / KOGUCHI,TAKUO
>> <takuo.koguchi.sw@hitachi.com>
>> Subject: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
>>

Is this a backport from 4.19 or newer? If so, please reference to
original commit.

Or is this addressing an issue that was only present in 4.4 and is not
affecting newer RT kernels? Then please explain, why. This information
is actually the most valuable part of this patch because it may help us
to resolve an issue that will live longer than 4.4.

Jan

>> On PREEMPT_RT, drain_local_stock() accesses the per-cpu memcg_stock via
>> this_cpu_ptr() without disabling preemption.
>>
>> Accesses to memcg_stock from consume_stock() and refill_stock() are already
>> serialized using get_cpu_var()/put_cpu_var(). However,
>> drain_local_stock() still accesses the same per-cpu state without equivalent
>> protection.
>>
>> As a result, drain_local_stock() may be preempted while operating on
>> memcg_stock, allowing another task running on the same CPU to observe or
>> modify the same per-cpu stock concurrently. This may result in inconsistent
>> updates of cached charges and stock counts.
>>
>> Failures were observed as divergence between memcg accounting and actual
>> memory consumption. The divergence occurred in both positive and negative
>> directions, indicating corruption of accounting state rather than a simple charge
>> leak.
>>
>> Under stress testing, the accounting divergence was reproducible and was
>> frequently accompanied by oops storms. Protecting
>> drain_local_stock() with get_cpu_var()/put_cpu_var() eliminated the accounting
>> divergence and the associated stability issues.
>>
>> Use get_cpu_var()/put_cpu_var() in drain_local_stock() to serialize accesses to
>> the per-cpu memcg stock and make its protection consistent with the other
>> memcg stock users.
>>
>> Signed-off-by: Takuo Koguchi <takuo.koguchi.sw@hitachi.com>
>> ---
>>  mm/memcontrol.c | 13 ++++++++++---
>>  1 file changed, 10 insertions(+), 3 deletions(-)
>>
>> diff --git a/mm/memcontrol.c b/mm/memcontrol.c index
>> f9d80952723c..9b298ce10a7a 100644
>> --- a/mm/memcontrol.c
>> +++ b/mm/memcontrol.c
>> @@ -1933,14 +1933,21 @@ static void drain_stock(struct memcg_stock_pcp
>> *stock)  }
>>
>>  /*
>> - * This must be called under preempt disabled or must be called by
>> - * a thread which is pinned to local cpu.
>> + * Drain this cpu's stock.  Called directly by drain_all_stock() for
>> + the
>> + * local cpu and as a work item for the other cpus.  Preemption is
>> + * disabled here because neither caller guarantees it on PREEMPT_RT:
>> + * drain_all_stock() only holds get_cpu_light(), which is a plain
>> + * barrier() without CONFIG_SMP, and the work item runs in a
>> + preemptible
>> + * kworker.  consume_stock() and refill_stock() run with preemption
>> + * disabled, so this keeps every user of the per-cpu stock serialized.
>>   */
>>  static void drain_local_stock(struct work_struct *dummy)  {
>> -	struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
>> +	struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
>> +
>>  	drain_stock(stock);
>>  	clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
>> +	put_cpu_var(memcg_stock);
>>  }
>>
>>  /*
>> --
>> 2.34.1
> 

-- 
Siemens AG, Foundational Technologies
Linux Expert Center

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: Re: [cip-dev] [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
  2026-11-25  8:27   ` Jan Kiszka
@ 2026-11-25  8:40     ` 小口琢夫 / KOGUCHI,TAKUO
  0 siblings, 0 replies; 9+ messages in thread
From: 小口琢夫 / KOGUCHI,TAKUO @ 2026-11-25  8:40 UTC (permalink / raw)
  To: 'jan.kiszka@siemens.com', cip-dev@lists.cip-project.org,
	Ulrich Hecht, Pavel Machek
  Cc: rostedt@goodmis.org

Thank you for the comment,

Later LTS, other methods like lock_irqsave/local_irqrestore are used, so this patch is not needed.

I hope this patch is just in time for the last CIP release for linux-4.4.y-cip-rt.

Takuo

> -----Original Message-----
> From: Jan Kiszka via cip-dev <jan.kiszka=siemens.com@lists.cip-project.org>
> Sent: Wednesday, November 25, 2026 5:27 PM
> To: 小口琢夫 / KOGUCHI,TAKUO <takuo.koguchi.sw@hitachi.com>; 'Takuo
> Koguchi' <takuo.koguchi@gmail.com>; cip-dev@lists.cip-project.org; Ulrich
> Hecht <uli@fpond.eu>; Pavel Machek <pavel@nabladev.com>
> Cc: rostedt@goodmis.org
> Subject: [!]Re: [cip-dev] [PATCH] memcontrol: protect drain_local_stock() with
> get_cpu_var()
> 
> On 25.11.26 05:47, å°å£ç¢å¤« / KOGUCHIï¼TAKUO wrote:
> > This patch is meant to be applied for linux-4.4.y-cip(-rt) only.
> >
> 
> I hope you know that 4.4-cip is very close to its retirement. There will likely be
> final releases of vanilla and -rt in December, but then it's going to be over. IOW: All
> users should by now be migrated to a newer kernel, or they will be in troubles in 5
> weeks from now.
> 
> That said:
> 
> > Takuo
> >
> >> -----Original Message-----
> >> From: Takuo Koguchi <takuo.koguchi@gmail.com>
> >> Sent: Wednesday, November 25, 2026 1:39 PM
> >> To: cip-dev@lists.cip-project.org
> >> Cc: rostedt@goodmis.org; 小口琢夫 / KOGUCHI,TAKUO
> >> <takuo.koguchi.sw@hitachi.com>
> >> Subject: [PATCH] memcontrol: protect drain_local_stock() with
> >> get_cpu_var()
> >>
> 
> Is this a backport from 4.19 or newer? If so, please reference to original commit.
> 
> Or is this addressing an issue that was only present in 4.4 and is not affecting
> newer RT kernels? Then please explain, why. This information is actually the most
> valuable part of this patch because it may help us to resolve an issue that will live
> longer than 4.4.
> 
> Jan
> 
> >> On PREEMPT_RT, drain_local_stock() accesses the per-cpu memcg_stock
> >> via
> >> this_cpu_ptr() without disabling preemption.
> >>
> >> Accesses to memcg_stock from consume_stock() and refill_stock() are
> >> already serialized using get_cpu_var()/put_cpu_var(). However,
> >> drain_local_stock() still accesses the same per-cpu state without
> >> equivalent protection.
> >>
> >> As a result, drain_local_stock() may be preempted while operating on
> >> memcg_stock, allowing another task running on the same CPU to observe
> >> or modify the same per-cpu stock concurrently. This may result in
> >> inconsistent updates of cached charges and stock counts.
> >>
> >> Failures were observed as divergence between memcg accounting and
> >> actual memory consumption. The divergence occurred in both positive
> >> and negative directions, indicating corruption of accounting state
> >> rather than a simple charge leak.
> >>
> >> Under stress testing, the accounting divergence was reproducible and
> >> was frequently accompanied by oops storms. Protecting
> >> drain_local_stock() with get_cpu_var()/put_cpu_var() eliminated the
> >> accounting divergence and the associated stability issues.
> >>
> >> Use get_cpu_var()/put_cpu_var() in drain_local_stock() to serialize
> >> accesses to the per-cpu memcg stock and make its protection
> >> consistent with the other memcg stock users.
> >>
> >> Signed-off-by: Takuo Koguchi <takuo.koguchi.sw@hitachi.com>
> >> ---
> >>  mm/memcontrol.c | 13 ++++++++++---
> >>  1 file changed, 10 insertions(+), 3 deletions(-)
> >>
> >> diff --git a/mm/memcontrol.c b/mm/memcontrol.c index
> >> f9d80952723c..9b298ce10a7a 100644
> >> --- a/mm/memcontrol.c
> >> +++ b/mm/memcontrol.c
> >> @@ -1933,14 +1933,21 @@ static void drain_stock(struct
> >> memcg_stock_pcp
> >> *stock)  }
> >>
> >>  /*
> >> - * This must be called under preempt disabled or must be called by
> >> - * a thread which is pinned to local cpu.
> >> + * Drain this cpu's stock.  Called directly by drain_all_stock() for
> >> + the
> >> + * local cpu and as a work item for the other cpus.  Preemption is
> >> + * disabled here because neither caller guarantees it on PREEMPT_RT:
> >> + * drain_all_stock() only holds get_cpu_light(), which is a plain
> >> + * barrier() without CONFIG_SMP, and the work item runs in a
> >> + preemptible
> >> + * kworker.  consume_stock() and refill_stock() run with preemption
> >> + * disabled, so this keeps every user of the per-cpu stock serialized.
> >>   */
> >>  static void drain_local_stock(struct work_struct *dummy)  {
> >> -	struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
> >> +	struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
> >> +
> >>  	drain_stock(stock);
> >>  	clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
> >> +	put_cpu_var(memcg_stock);
> >>  }
> >>
> >>  /*
> >> --
> >> 2.34.1
> >
> 
> --
> Siemens AG, Foundational Technologies
> Linux Expert Center

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
  2026-11-25  4:39 [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var() Takuo Koguchi
  2026-11-25  4:47 ` 小口琢夫 / KOGUCHI,TAKUO
  2026-11-25  5:17 ` 小口琢夫 / KOGUCHI,TAKUO
@ 2026-11-25 15:13 ` Harry Yoo
  2026-11-25 15:19   ` Sebastian Andrzej Siewior
  2026-11-30  2:15   ` Takuo Koguchi
  2 siblings, 2 replies; 9+ messages in thread
From: Harry Yoo @ 2026-11-25 15:13 UTC (permalink / raw)
  To: Takuo Koguchi
  Cc: cip-dev, Steven Rostedt, Takuo Koguchi, Johannes Weiner,
	Michal Hocko, Roman Gushchin, Shakeel Butt, Muchun Song, cgroups,
	linux-mm, Sebastian Andrzej Siewior, Clark Williams,
	linux-rt-devel

[+Cc PREEMPT_RT and MEMCG folks properly ]

This doesn't look like a patch for the latest mainline.
Which kernel version is it based onto?

On Wed, Nov 25, 2026 at 01:39:00PM +0900, Takuo Koguchi wrote:
> On PREEMPT_RT, drain_local_stock() accesses the per-cpu memcg_stock
> via this_cpu_ptr() without disabling preemption.
>
> Accesses to memcg_stock from consume_stock() and refill_stock() are
> already serialized using get_cpu_var()/put_cpu_var(). However,
> drain_local_stock() still accesses the same per-cpu state without
> equivalent protection.
> 
> As a result, drain_local_stock() may be preempted while operating on
> memcg_stock, allowing another task running on the same CPU to observe
> or modify the same per-cpu stock concurrently. This may result in
> inconsistent updates of cached charges and stock counts.
> 
> Failures were observed as divergence between memcg accounting and
> actual memory consumption. The divergence occurred in both positive
> and negative directions, indicating corruption of accounting state
> rather than a simple charge leak.
> 
> Under stress testing, the accounting divergence was reproducible and
> was frequently accompanied by oops storms. Protecting
> drain_local_stock() with get_cpu_var()/put_cpu_var() eliminated the
> accounting divergence and the associated stability issues.
> 
> Use get_cpu_var()/put_cpu_var() in drain_local_stock() to serialize
> accesses to the per-cpu memcg stock and make its protection consistent
> with the other memcg stock users.
> 
> Signed-off-by: Takuo Koguchi <takuo.koguchi.sw@hitachi.com>
> ---
>  mm/memcontrol.c | 13 ++++++++++---
>  1 file changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index f9d80952723c..9b298ce10a7a 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1933,14 +1933,21 @@ static void drain_stock(struct memcg_stock_pcp *stock)
>  }
>  
>  /*
> - * This must be called under preempt disabled or must be called by
> - * a thread which is pinned to local cpu.
> + * Drain this cpu's stock.  Called directly by drain_all_stock() for the
> + * local cpu and as a work item for the other cpus.  Preemption is
> + * disabled here because neither caller guarantees it on PREEMPT_RT:
> + * drain_all_stock() only holds get_cpu_light(), which is a plain
> + * barrier() without CONFIG_SMP, and the work item runs in a preemptible
> + * kworker.  consume_stock() and refill_stock() run with preemption
> + * disabled, so this keeps every user of the per-cpu stock serialized.
>   */
>  static void drain_local_stock(struct work_struct *dummy)
>  {
> -	struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
> +	struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
> +
>  	drain_stock(stock);
>  	clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
> +	put_cpu_var(memcg_stock);
>  }
>  
>  /*
> -- 
> 2.34.1

-- 
Cheers,
Harry / Hyeonggon

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
  2026-11-25 15:13 ` Harry Yoo
@ 2026-11-25 15:19   ` Sebastian Andrzej Siewior
  2026-11-30  2:15     ` Takuo Koguchi
  2026-11-30  2:15   ` Takuo Koguchi
  1 sibling, 1 reply; 9+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-11-25 15:19 UTC (permalink / raw)
  To: Harry Yoo
  Cc: Takuo Koguchi, cip-dev, Steven Rostedt, Takuo Koguchi,
	Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	Muchun Song, cgroups, linux-mm, Clark Williams, linux-rt-devel

On 2026-11-25 15:13:05 [+0000], Harry Yoo wrote:
> > --- a/mm/memcontrol.c
> > +++ b/mm/memcontrol.c
> > @@ -1933,14 +1933,21 @@ static void drain_stock(struct memcg_stock_pcp *stock)
> >  }
> >  
> >  /*
> > - * This must be called under preempt disabled or must be called by
> > - * a thread which is pinned to local cpu.
> > + * Drain this cpu's stock.  Called directly by drain_all_stock() for the
> > + * local cpu and as a work item for the other cpus.  Preemption is
> > + * disabled here because neither caller guarantees it on PREEMPT_RT:
> > + * drain_all_stock() only holds get_cpu_light(), which is a plain
> > + * barrier() without CONFIG_SMP, and the work item runs in a preemptible
> > + * kworker.  consume_stock() and refill_stock() run with preemption
> > + * disabled, so this keeps every user of the per-cpu stock serialized.
> >   */
> >  static void drain_local_stock(struct work_struct *dummy)
> >  {
> > -	struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
> > +	struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
> > +
> >  	drain_stock(stock);
> >  	clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
> > +	put_cpu_var(memcg_stock);
> >  }

This breaks PREEMPT_RT and is nonsense. this_cpu_ptr() should not create
any warning here and if it is then something else is broken.
There is no need to disable preemption here, this is invoked from a
per-CPU worker.

Sebastian

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
  2026-11-25 15:13 ` Harry Yoo
  2026-11-25 15:19   ` Sebastian Andrzej Siewior
@ 2026-11-30  2:15   ` Takuo Koguchi
  1 sibling, 0 replies; 9+ messages in thread
From: Takuo Koguchi @ 2026-11-30  2:15 UTC (permalink / raw)
  To: Harry Yoo
  Cc: cip-dev, linux-rt-devel, linux-mm, cgroups, Steven Rostedt,
	Clark Williams, Sebastian Andrzej Siewior

Hi Harry,

> This doesn't look like a patch for the latest mainline.
> Which kernel version is it based onto?

Sorry, I should have put that in the subject. It is against
linux-4.4.y-cip-rt, v4.4.302-cip115-rt63 (4550c560d01f).

Mainline and v4.19-rt are not affected: there drain_local_stock() runs
with interrupts disabled since db2ba40c277d (v4.8). In 4.4-rt only
get_cpu_light() protects it (6026be2f750c), which disables migration
but not preemption (on !CONFIG_SMP it is just barrier()). We reproduced
the problem on 4.4.62-rt73, v4.4.302-rt232 and v4.4.302-cip115-rt63
(ARM, !SMP, PREEMPT_RT_FULL). With the patch, our reproducer shows no
drift on v4.4.302-cip115-rt63.

I will send a v2 tagged [PATCH 4.4.y-cip-rt] with a changelog that
explains the race, and answer Sebastian in that thread.

Thanks,
Takuo

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var()
  2026-11-25 15:19   ` Sebastian Andrzej Siewior
@ 2026-11-30  2:15     ` Takuo Koguchi
  0 siblings, 0 replies; 9+ messages in thread
From: Takuo Koguchi @ 2026-11-30  2:15 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: cip-dev, linux-rt-devel, linux-mm, cgroups, Steven Rostedt,
	Clark Williams, Harry Yoo

Hi Sebastian,

> This breaks PREEMPT_RT and is nonsense. this_cpu_ptr() should not create
> any warning here and if it is then something else is broken.

Sorry, my changelog was misleading. There is no warning. The problem is
a race on the per-CPU stock that corrupts the memcg counters.

> There is no need to disable preemption here, this is invoked from a
> per-CPU worker.

On v4.4-rt it is also called directly, from drain_all_stock() for the
local CPU:

	curcpu = get_cpu_light();
	for_each_online_cpu(cpu) {
		...
			if (cpu == curcpu)
				drain_local_stock(&stock->work);

This runs in the task that hit the limit in try_charge(). Since
6026be2f750c only get_cpu_light() is held, which does not disable
preemption (on our !SMP board it is just barrier()). The per-CPU worker
is pinned but preemptible too.

So drain_stock() can be preempted while another task on the same CPU
uses the stock through consume_stock() or refill_stock(). drain_stock()
reads stock->nr_pages separately for the memory and memsw uncharges and
clears nr_pages and cached in separate steps, so the counters drift
apart and the stock can be left as {NULL, N}. On our 4.4.62-rt73 based
kernel that ends in a NULL pointer oops in
page_counter_uncharge() called from drain_stock(), on every charge until
a reboot. On v4.4.302-cip115-rt63 our reproducer shows the drift within
an hour; with the patch, it shows none.

As for RT: refill_stock() already calls drain_stock() with preemption
disabled since 8f3f54d23507, page_counter_uncharge() is lockless and
css_put*() releases through swork. With CONFIG_DEBUG_ATOMIC_SLEEP=y and
CONFIG_DEBUG_PREEMPT=y I saw no splats. Is there a sleeping lock on this
path that I am missing?

I will send a v2 with a changelog that explains the race and a Fixes:
tag. If you prefer a local lock covering all the stock users, I can do
that instead.

Thanks,
Takuo

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-30  1:53 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  4:39 [PATCH] memcontrol: protect drain_local_stock() with get_cpu_var() Takuo Koguchi
2026-11-25  4:47 ` 小口琢夫 / KOGUCHI,TAKUO
2026-11-25  8:27   ` Jan Kiszka
2026-11-25  8:40     ` Re: [cip-dev] " 小口琢夫 / KOGUCHI,TAKUO
2026-11-25  5:17 ` 小口琢夫 / KOGUCHI,TAKUO
2026-11-25 15:13 ` Harry Yoo
2026-11-25 15:19   ` Sebastian Andrzej Siewior
2026-11-30  2:15     ` Takuo Koguchi
2026-11-30  2:15   ` Takuo Koguchi

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.