From: Harry Yoo <harry.yoo@oracle.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
bpf@vger.kernel.org, daniel@iogearbox.net, andrii@kernel.org,
martin.lau@kernel.org, akpm@linux-foundation.org,
peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de,
rostedt@goodmis.org, shakeel.butt@linux.dev, mhocko@suse.com,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH mm] mm/page_alloc: Avoid second trylock of zone->lock
Date: Tue, 1 Apr 2025 12:28:25 +0900 [thread overview]
Message-ID: <Z-tdWXcv1Zqim2ng@harry> (raw)
In-Reply-To: <20250331002809.94758-1-alexei.starovoitov@gmail.com>
On Sun, Mar 30, 2025 at 05:28:09PM -0700, Alexei Starovoitov wrote:
> From: Alexei Starovoitov <ast@kernel.org>
>
> 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.
>
> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> Fixes: 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
Looks good to me,
Reviewed-by: Harry Yoo <harry.yoo@oracle.com>
> mm/page_alloc.c | 15 +++++++++------
> 1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index e3ea5bf5c459..ffbb5678bc2f 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -1268,11 +1268,12 @@ static void free_one_page(struct zone *zone, struct page *page,
> struct llist_head *llhead;
> unsigned long flags;
>
> - if (!spin_trylock_irqsave(&zone->lock, flags)) {
> - if (unlikely(fpi_flags & FPI_TRYLOCK)) {
> + if (unlikely(fpi_flags & FPI_TRYLOCK)) {
> + if (!spin_trylock_irqsave(&zone->lock, flags)) {
> add_page_to_zone_llist(zone, page, order);
> return;
> }
> + } else {
> spin_lock_irqsave(&zone->lock, flags);
> }
>
> @@ -2341,9 +2342,10 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
> unsigned long flags;
> int i;
>
> - if (!spin_trylock_irqsave(&zone->lock, flags)) {
> - if (unlikely(alloc_flags & ALLOC_TRYLOCK))
> + if (unlikely(alloc_flags & ALLOC_TRYLOCK)) {
> + if (!spin_trylock_irqsave(&zone->lock, flags))
> return 0;
> + } else {
> spin_lock_irqsave(&zone->lock, flags);
> }
> for (i = 0; i < count; ++i) {
> @@ -2964,9 +2966,10 @@ struct page *rmqueue_buddy(struct zone *preferred_zone, struct zone *zone,
>
> do {
> page = NULL;
> - if (!spin_trylock_irqsave(&zone->lock, flags)) {
> - if (unlikely(alloc_flags & ALLOC_TRYLOCK))
> + if (unlikely(alloc_flags & ALLOC_TRYLOCK)) {
> + if (!spin_trylock_irqsave(&zone->lock, flags))
> return NULL;
> + } else {
> spin_lock_irqsave(&zone->lock, flags);
> }
> if (alloc_flags & ALLOC_HIGHATOMIC)
> --
> 2.47.1
>
>
--
Cheers,
Harry (formerly known as Hyeonggon)
next prev parent reply other threads:[~2025-04-01 3:29 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-31 0:28 [PATCH mm] mm/page_alloc: Avoid second trylock of zone->lock Alexei Starovoitov
2025-03-31 8:11 ` Sebastian Andrzej Siewior
2025-03-31 10:52 ` Michal Hocko
2025-03-31 12:17 ` Vlastimil Babka
2025-03-31 16:59 ` Alexei Starovoitov
2025-04-01 3:28 ` Harry Yoo [this message]
2025-04-02 4:29 ` Shakeel Butt
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=Z-tdWXcv1Zqim2ng@harry \
--to=harry.yoo@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=martin.lau@kernel.org \
--cc=mhocko@suse.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=shakeel.butt@linux.dev \
--cc=torvalds@linux-foundation.org \
--cc=vbabka@suse.cz \
/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.