Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/page_alloc: avoid second trylock of zone->lock

spin_trylock followed by spin_lock will cause extra write cache access.
If the lock is contended it may cause unnecessary cache line bouncing and
will execute redundant irq restore/save pair. Therefore, check
alloc/fpi_flags first and use spin_trylock or spin_lock.

Link: https://lkml.kernel.org/r/20250331002809.94758-1-alexei.starovoitov@gmail.com
Fixes: 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Acked-by: Michal Hocko <mhocko@suse.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Harry Yoo <harry.yoo@oracle.com>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Daniel Borkman <daniel@iogearbox.net>
Cc: Martin KaFai Lau <martin.lau@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Alexei Starovoitov and committed by
Andrew Morton
c5bb27e2 a84edd52

+9 -6
+9 -6
mm/page_alloc.c
··· 1400 1400 struct llist_head *llhead; 1401 1401 unsigned long flags; 1402 1402 1403 - if (!spin_trylock_irqsave(&zone->lock, flags)) { 1404 - if (unlikely(fpi_flags & FPI_TRYLOCK)) { 1403 + if (unlikely(fpi_flags & FPI_TRYLOCK)) { 1404 + if (!spin_trylock_irqsave(&zone->lock, flags)) { 1405 1405 add_page_to_zone_llist(zone, page, order); 1406 1406 return; 1407 1407 } 1408 + } else { 1408 1409 spin_lock_irqsave(&zone->lock, flags); 1409 1410 } 1410 1411 ··· 2315 2314 unsigned long flags; 2316 2315 int i; 2317 2316 2318 - if (!spin_trylock_irqsave(&zone->lock, flags)) { 2319 - if (unlikely(alloc_flags & ALLOC_TRYLOCK)) 2317 + if (unlikely(alloc_flags & ALLOC_TRYLOCK)) { 2318 + if (!spin_trylock_irqsave(&zone->lock, flags)) 2320 2319 return 0; 2320 + } else { 2321 2321 spin_lock_irqsave(&zone->lock, flags); 2322 2322 } 2323 2323 for (i = 0; i < count; ++i) { ··· 2939 2937 2940 2938 do { 2941 2939 page = NULL; 2942 - if (!spin_trylock_irqsave(&zone->lock, flags)) { 2943 - if (unlikely(alloc_flags & ALLOC_TRYLOCK)) 2940 + if (unlikely(alloc_flags & ALLOC_TRYLOCK)) { 2941 + if (!spin_trylock_irqsave(&zone->lock, flags)) 2944 2942 return NULL; 2943 + } else { 2945 2944 spin_lock_irqsave(&zone->lock, flags); 2946 2945 } 2947 2946 if (alloc_flags & ALLOC_HIGHATOMIC)