linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* Re: [PATCH 2/2] mm/page_alloc: Fix race condition in unaccepted memory handling
       [not found] ` <20250506112509.905147-3-kirill.shutemov@linux.intel.com>
@ 2025-05-06 12:43   ` Borislav Petkov
  0 siblings, 0 replies; 10+ messages in thread
From: Borislav Petkov @ 2025-05-06 12:43 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: akpm, vbabka, surenb, mhocko, jackmanb, hannes, tglx, david, ast,
	linux-mm, linux-coco, linux-kernel, stable

On Tue, May 06, 2025 at 02:25:09PM +0300, Kirill A. Shutemov wrote:
> The page allocator tracks the number of zones that have unaccepted
> memory using static_branch_enc/dec() and uses that static branch in hot
> paths to determine if it needs to deal with unaccepted memory.
> 
> Borisal and Thomas pointed out that the tracking is racy operations on

Boris or Borislav would be nice.

> static_branch are not serialized against adding/removing unaccepted pages
> to/from the zone.

Also, that sentence needs massaging.

> The effect of this static_branch optimization is only visible on
> microbenchmark.
> 
> Instead of adding more complexity around it, remove it altogether.

Yah, good idea.

> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> Fixes: dcdfdd40fa82 ("mm: Add support for unaccepted memory")
> Link: https://lore.kernel.org/all/20250506092445.GBaBnVXXyvnazly6iF@fat_crate.local
> Reported-by: Borislav Petkov <bp@alien8.de>

Tested-by: Borislav Petkov (AMD) <bp@alien8.de>

Thx for the quick fix.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette


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

* Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
       [not found] ` <20250506112509.905147-2-kirill.shutemov@linux.intel.com>
@ 2025-05-06 13:20   ` Brendan Jackman
  2025-05-06 13:34     ` Kirill A. Shutemov
  2025-05-06 17:18   ` Alexei Starovoitov
  2025-05-07  0:00   ` Andrew Morton
  2 siblings, 1 reply; 10+ messages in thread
From: Brendan Jackman @ 2025-05-06 13:20 UTC (permalink / raw)
  To: Kirill A. Shutemov, akpm, vbabka, surenb, mhocko, hannes, bp,
	tglx, david
  Cc: ast, linux-mm, linux-coco, linux-kernel

On Tue May 6, 2025 at 11:25 AM UTC, Kirill A. Shutemov wrote:
> +	/* Bailout, since try_to_accept_memory_one() needs to take a lock */
> +	if (alloc_flags & ALLOC_TRYLOCK)
> +		return false;
> +

Quick lazy question: why don't we just trylock it like we do for the zone
lock?


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

* Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
  2025-05-06 13:20   ` [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory Brendan Jackman
@ 2025-05-06 13:34     ` Kirill A. Shutemov
  2025-05-07 15:27       ` Brendan Jackman
  0 siblings, 1 reply; 10+ messages in thread
From: Kirill A. Shutemov @ 2025-05-06 13:34 UTC (permalink / raw)
  To: Brendan Jackman
  Cc: akpm, vbabka, surenb, mhocko, hannes, bp, tglx, david, ast,
	linux-mm, linux-coco, linux-kernel

On Tue, May 06, 2025 at 01:20:25PM +0000, Brendan Jackman wrote:
> On Tue May 6, 2025 at 11:25 AM UTC, Kirill A. Shutemov wrote:
> > +	/* Bailout, since try_to_accept_memory_one() needs to take a lock */
> > +	if (alloc_flags & ALLOC_TRYLOCK)
> > +		return false;
> > +
> 
> Quick lazy question: why don't we just trylock it like we do for the zone
> lock?

It is not only zone lock. There's also unaccepted_memory_lock inside
accept_memory().

-- 
  Kiryl Shutsemau / Kirill A. Shutemov


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

* Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
       [not found] ` <20250506112509.905147-2-kirill.shutemov@linux.intel.com>
  2025-05-06 13:20   ` [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory Brendan Jackman
@ 2025-05-06 17:18   ` Alexei Starovoitov
  2025-05-06 19:00     ` Kirill A. Shutemov
  2025-05-07  0:00   ` Andrew Morton
  2 siblings, 1 reply; 10+ messages in thread
From: Alexei Starovoitov @ 2025-05-06 17:18 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: Andrew Morton, Vlastimil Babka, Suren Baghdasaryan, Michal Hocko,
	Brendan Jackman, Johannes Weiner, Borislav Petkov,
	Thomas Gleixner, David Hildenbrand, Alexei Starovoitov, linux-mm,
	linux-coco, LKML

On Tue, May 6, 2025 at 4:25 AM Kirill A. Shutemov
<kirill.shutemov@linux.intel.com> wrote:
>
> try_alloc_pages() will not attempt to allocate memory if the system has
> *any* unaccepted memory. Memory is accepted as needed and can remain in
> the system indefinitely, causing the interface to always fail.
>
> Rather than immediately giving up, attempt to use already accepted
> memory on free lists.
>
> Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory
> for ALLOC_TRYLOCK requests.
>
> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> Fixes: 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")

Thanks for working on this, but the fixes tag is overkill.
This limitation is not causing any issues in our setups.
Improving it is certainly better, of course.
Acked-by: Alexei Starovoitov <ast@kernel.org>


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

* Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
  2025-05-06 17:18   ` Alexei Starovoitov
@ 2025-05-06 19:00     ` Kirill A. Shutemov
  2025-05-06 22:05       ` Alexei Starovoitov
  0 siblings, 1 reply; 10+ messages in thread
From: Kirill A. Shutemov @ 2025-05-06 19:00 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Andrew Morton, Vlastimil Babka, Suren Baghdasaryan, Michal Hocko,
	Brendan Jackman, Johannes Weiner, Borislav Petkov,
	Thomas Gleixner, David Hildenbrand, Alexei Starovoitov, linux-mm,
	linux-coco, LKML

On Tue, May 06, 2025 at 10:18:21AM -0700, Alexei Starovoitov wrote:
> On Tue, May 6, 2025 at 4:25 AM Kirill A. Shutemov
> <kirill.shutemov@linux.intel.com> wrote:
> >
> > try_alloc_pages() will not attempt to allocate memory if the system has
> > *any* unaccepted memory. Memory is accepted as needed and can remain in
> > the system indefinitely, causing the interface to always fail.
> >
> > Rather than immediately giving up, attempt to use already accepted
> > memory on free lists.
> >
> > Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory
> > for ALLOC_TRYLOCK requests.
> >
> > Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> > Fixes: 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")
> 
> Thanks for working on this, but the fixes tag is overkill.
> This limitation is not causing any issues in our setups.

Have you had chance to test it on any platform with unaccepted memory?
So far it is only Intel TDX and AMD SEV guests.

> Improving it is certainly better, of course.
> Acked-by: Alexei Starovoitov <ast@kernel.org>

Thanks!

-- 
  Kiryl Shutsemau / Kirill A. Shutemov


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

* Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
  2025-05-06 19:00     ` Kirill A. Shutemov
@ 2025-05-06 22:05       ` Alexei Starovoitov
  2025-05-07 11:13         ` Kirill A. Shutemov
  0 siblings, 1 reply; 10+ messages in thread
From: Alexei Starovoitov @ 2025-05-06 22:05 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: Andrew Morton, Vlastimil Babka, Suren Baghdasaryan, Michal Hocko,
	Brendan Jackman, Johannes Weiner, Borislav Petkov,
	Thomas Gleixner, David Hildenbrand, Alexei Starovoitov, linux-mm,
	linux-coco, LKML

On Tue, May 6, 2025 at 12:00 PM Kirill A. Shutemov
<kirill.shutemov@linux.intel.com> wrote:
>
> On Tue, May 06, 2025 at 10:18:21AM -0700, Alexei Starovoitov wrote:
> > On Tue, May 6, 2025 at 4:25 AM Kirill A. Shutemov
> > <kirill.shutemov@linux.intel.com> wrote:
> > >
> > > try_alloc_pages() will not attempt to allocate memory if the system has
> > > *any* unaccepted memory. Memory is accepted as needed and can remain in
> > > the system indefinitely, causing the interface to always fail.
> > >
> > > Rather than immediately giving up, attempt to use already accepted
> > > memory on free lists.
> > >
> > > Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory
> > > for ALLOC_TRYLOCK requests.
> > >
> > > Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> > > Fixes: 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")
> >
> > Thanks for working on this, but the fixes tag is overkill.
> > This limitation is not causing any issues in our setups.
>
> Have you had chance to test it on any platform with unaccepted memory?
> So far it is only Intel TDX and AMD SEV guests.

We don't use them, and my understanding is that such
unaccepted memory will be there only during boot time.


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

* Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
       [not found] ` <20250506112509.905147-2-kirill.shutemov@linux.intel.com>
  2025-05-06 13:20   ` [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory Brendan Jackman
  2025-05-06 17:18   ` Alexei Starovoitov
@ 2025-05-07  0:00   ` Andrew Morton
  2025-05-07 11:31     ` Kirill A. Shutemov
  2 siblings, 1 reply; 10+ messages in thread
From: Andrew Morton @ 2025-05-07  0:00 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: vbabka, surenb, mhocko, jackmanb, hannes, bp, tglx, david, ast,
	linux-mm, linux-coco, linux-kernel

On Tue,  6 May 2025 14:25:08 +0300 "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> wrote:

> try_alloc_pages() will not attempt to allocate memory if the system has
> *any* unaccepted memory. Memory is accepted as needed and can remain in
> the system indefinitely, causing the interface to always fail.
> 
> Rather than immediately giving up, attempt to use already accepted
> memory on free lists.
> 
> Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory
> for ALLOC_TRYLOCK requests.

What are the userspace-visible effects, please?

Was the omission of cc:stable intentional?  I cannot locally determine
this without the above info.

If the cc:stable omission was indeed intentional then it would be better
if this series was presented as two standalone patches.


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

* Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
  2025-05-06 22:05       ` Alexei Starovoitov
@ 2025-05-07 11:13         ` Kirill A. Shutemov
  0 siblings, 0 replies; 10+ messages in thread
From: Kirill A. Shutemov @ 2025-05-07 11:13 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Andrew Morton, Vlastimil Babka, Suren Baghdasaryan, Michal Hocko,
	Brendan Jackman, Johannes Weiner, Borislav Petkov,
	Thomas Gleixner, David Hildenbrand, Alexei Starovoitov, linux-mm,
	linux-coco, LKML

On Tue, May 06, 2025 at 03:05:31PM -0700, Alexei Starovoitov wrote:
> On Tue, May 6, 2025 at 12:00 PM Kirill A. Shutemov
> <kirill.shutemov@linux.intel.com> wrote:
> >
> > On Tue, May 06, 2025 at 10:18:21AM -0700, Alexei Starovoitov wrote:
> > > On Tue, May 6, 2025 at 4:25 AM Kirill A. Shutemov
> > > <kirill.shutemov@linux.intel.com> wrote:
> > > >
> > > > try_alloc_pages() will not attempt to allocate memory if the system has
> > > > *any* unaccepted memory. Memory is accepted as needed and can remain in
> > > > the system indefinitely, causing the interface to always fail.
> > > >
> > > > Rather than immediately giving up, attempt to use already accepted
> > > > memory on free lists.
> > > >
> > > > Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory
> > > > for ALLOC_TRYLOCK requests.
> > > >
> > > > Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> > > > Fixes: 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")
> > >
> > > Thanks for working on this, but the fixes tag is overkill.
> > > This limitation is not causing any issues in our setups.
> >
> > Have you had chance to test it on any platform with unaccepted memory?
> > So far it is only Intel TDX and AMD SEV guests.
> 
> We don't use them, and my understanding is that such
> unaccepted memory will be there only during boot time.

That's false. Unaccepted memory can be there indefinitely after boot. It
only gets accepted on demand.

-- 
  Kiryl Shutsemau / Kirill A. Shutemov


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

* Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
  2025-05-07  0:00   ` Andrew Morton
@ 2025-05-07 11:31     ` Kirill A. Shutemov
  0 siblings, 0 replies; 10+ messages in thread
From: Kirill A. Shutemov @ 2025-05-07 11:31 UTC (permalink / raw)
  To: Andrew Morton
  Cc: vbabka, surenb, mhocko, jackmanb, hannes, bp, tglx, david, ast,
	linux-mm, linux-coco, linux-kernel

On Tue, May 06, 2025 at 05:00:34PM -0700, Andrew Morton wrote:
> On Tue,  6 May 2025 14:25:08 +0300 "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> wrote:
> 
> > try_alloc_pages() will not attempt to allocate memory if the system has
> > *any* unaccepted memory. Memory is accepted as needed and can remain in
> > the system indefinitely, causing the interface to always fail.
> > 
> > Rather than immediately giving up, attempt to use already accepted
> > memory on free lists.
> > 
> > Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory
> > for ALLOC_TRYLOCK requests.
> 
> What are the userspace-visible effects, please?

I cannot say I fully understand the implications.

The interface obviously allows failure, but the caller might expect
eventual success on retry.

So far, only BPF uses the interface. Maybe Alexei can comment on what will
happen if the function always fails.

I noticed the issue by code analysis because the second patch removes
has_unaccepted_memory().

> Was the omission of cc:stable intentional?  I cannot locally determine
> this without the above info.
> 
> If the cc:stable omission was indeed intentional then it would be better
> if this series was presented as two standalone patches.

Given that the second patch cannot be applied to current Linus' tree
without this one, it is better to add stable@.

-- 
  Kiryl Shutsemau / Kirill A. Shutemov


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

* Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
  2025-05-06 13:34     ` Kirill A. Shutemov
@ 2025-05-07 15:27       ` Brendan Jackman
  0 siblings, 0 replies; 10+ messages in thread
From: Brendan Jackman @ 2025-05-07 15:27 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: akpm, vbabka, surenb, mhocko, hannes, bp, tglx, david, ast,
	linux-mm, linux-coco, linux-kernel

On Tue May 6, 2025 at 1:34 PM UTC, Kirill A. Shutemov wrote:
> On Tue, May 06, 2025 at 01:20:25PM +0000, Brendan Jackman wrote:
>> On Tue May 6, 2025 at 11:25 AM UTC, Kirill A. Shutemov wrote:
>> > +	/* Bailout, since try_to_accept_memory_one() needs to take a lock */
>> > +	if (alloc_flags & ALLOC_TRYLOCK)
>> > +		return false;
>> > +
>> 
>> Quick lazy question: why don't we just trylock it like we do for the zone
>> lock?
>
> It is not only zone lock. There's also unaccepted_memory_lock inside
> accept_memory().

Right, but my lazy question was why can't we "just" trylock that too?

But anyway, that's no use because if we win the trylock we'd still have
to do __free_pages_ok().


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

end of thread, other threads:[~2025-05-07 15:27 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20250506112509.905147-1-kirill.shutemov@linux.intel.com>
     [not found] ` <20250506112509.905147-3-kirill.shutemov@linux.intel.com>
2025-05-06 12:43   ` [PATCH 2/2] mm/page_alloc: Fix race condition in unaccepted memory handling Borislav Petkov
     [not found] ` <20250506112509.905147-2-kirill.shutemov@linux.intel.com>
2025-05-06 13:20   ` [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory Brendan Jackman
2025-05-06 13:34     ` Kirill A. Shutemov
2025-05-07 15:27       ` Brendan Jackman
2025-05-06 17:18   ` Alexei Starovoitov
2025-05-06 19:00     ` Kirill A. Shutemov
2025-05-06 22:05       ` Alexei Starovoitov
2025-05-07 11:13         ` Kirill A. Shutemov
2025-05-07  0:00   ` Andrew Morton
2025-05-07 11:31     ` Kirill A. Shutemov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox