linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Harry Yoo <harry.yoo@oracle.com>
To: Yeo Reum Yun <YeoReum.Yun@arm.com>
Cc: "linux-mm@kvack.org" <linux-mm@kvack.org>
Subject: Re: [Question] Question about GFP_ATOMIC allocations on PREEMPT_RT kernel
Date: Sun, 8 Jun 2025 22:56:34 +0900	[thread overview]
Message-ID: <aEWWkij1LMT-Ywhu@hyeyoo> (raw)
In-Reply-To: <GV1PR08MB10521D66F55C6BDBCDC2C088EFB68A@GV1PR08MB10521.eurprd08.prod.outlook.com>

On Sun, Jun 08, 2025 at 01:33:14PM +0000, Yeo Reum Yun wrote:
> Hi mm experts,
> 
> I have a (possibly silly) question regarding the use of memory allocation with GFP_ATOMIC.
> 
> According to the latest documentation in core-api/memory-allocation.html,
> using GFP_ATOMIC is acceptable in interrupt handlers:
> 
> GFP_ATOMIC:
> “(GFP_KERNEL | __GFP_HIGH) & ~__GFP_DIRECT_RECLAIM (aka GFP_ATOMIC) –
> non-sleeping allocation with an expensive fallback,
> so it can access some portion of memory reserves.
> Usually used from interrupt/bottom-half context with an expensive slow path fallback.”
> 
> This makes sense on non-RT kernels, where spin_lock() is non-sleepable.
> 
> However, how does this behave on a PREEMPT_RT kernel?
> 
> From what I see in the buddy allocator, even allocations using GFP_ATOMIC
> can potentially sleep, since some of the locks used (e.g., zone->lock)
> are implemented as sleepable rtmutex under PREEMPT_RT.
> 
> This seems fine for most interrupt handlers, since on PREEMPT_RT,
> they are handled in dedicated IRQ threads — which means they run in a sleepable context.
> 
> But what about interrupt handlers registered with IRQF_NO_THREAD?

You're not supposed allocate (even with GFP_ATOMIC) or free memory on
PREEMPT_RT in non-threaded interrupt context [1].

I think it somwhow implied in [2]? (Ok, probably not that obvious)

"Acquiring a raw_spinlock_t disables preemption and possibly also
interrupts, so the critical section must avoid acquiring a regular
spinlock_t or rwlock_t, for example, the critical section must avoid
allocating memory."

"But this code fails on PREEMPT_RT kernels because the memory allocator
is fully preemptible and therefore cannot be invoked from truly atomic
contexts."

[1] https://lore.kernel.org/linux-mm/YwZSUpYY9l442Lxr@linutronix.de

[2] https://www.kernel.org/doc/html/latest/locking/locktypes.html#raw-spinlock-t-on-rt

> Those handlers run in hard interrupt context, which is non-sleepable.
> If an allocation with GFP_ATOMIC involves a sleepable lock (like zone->lock),
> wouldn’t that be problematic?
> 
> To my knowledge, it’s not clearly documented whether memory allocation functions like
> kmalloc(), alloc_pages(), or folio_alloc() are explicitly forbidden in truly
> non-sleepable contexts under PREEMPT_RT.
> 
> So my question is:
> Is it always safe to call GFP_ATOMIC allocations from a non-sleepable context, regardless of the kernel configuration (i.e., RT vs non-RT)?
> 
> Thanks!
> IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
> 

-- 
Cheers,
Harry / Hyeonggon


  reply	other threads:[~2025-06-08 13:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-08 13:33 Yeo Reum Yun
2025-06-08 13:56 ` Harry Yoo [this message]
2025-06-08 15:15   ` Yeoreum Yun

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=aEWWkij1LMT-Ywhu@hyeyoo \
    --to=harry.yoo@oracle.com \
    --cc=YeoReum.Yun@arm.com \
    --cc=linux-mm@kvack.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox