* [PATCH 0/2] Add ACR_FLAGS_LAZY and use it in cma_range_alloc()
@ 2026-12-02 15:13 Daniel Scally
2026-12-02 15:13 ` [PATCH 1/2] mm: page_alloc: Add a "lazy" flag for __alloc_contig_migrate_range() Daniel Scally
2026-12-02 15:13 ` [PATCH 2/2] mm: cma: Pass ACR_FLAGS_LAZY to alloc_contig_frozen_range() Daniel Scally
0 siblings, 2 replies; 4+ messages in thread
From: Daniel Scally @ 2026-12-02 15:13 UTC (permalink / raw)
To: Vlastimil Babka, Johannes Weiner, Suren Baghdasaryan,
Michal Hocko, Brendan Jackman, Zi Yan, David Hildenbrand,
Liam R. Howlett, Mike Rapoport, Andrew Morton
Cc: linux-mm, hughd, kieran.bingham, Daniel Scally
Hello all
I feel I should mention at the top that I think that this change is
ok and that it fixes a problem that we've been having, but I won't
pretend any expertise (or even experience) in mm/ so there may be
potential downsides for the change that I have not appreciated.
I'm working with an i.MX8MP board which has a dewarper device to
who's driver userspace queues memory buffers ultimately allocated
by the kernel calling cma_range_alloc(), which itself calls
alloc_contig_frozen_range() for different page ranges until it
manages to allocate the space it needs (or fails).
We're finding that the call to lru_cache_disable() within
__alloc_contig_migrate_range() comes with a severe performance hit on
a PREEMPT_RT kernel, and so we'd like to avoid it. This proposal adds
a new "lazy" flag, which __alloc_contig_migrate_range() will treat as
an instruction to skip the lru_cache_disable() call.
My understanding is that the impact of this would be that an
allocation that could have succeeded if pages within the LRU cache
had been flushed and migrated out of the region, will now fail with
-EBUSY. In an attempt to get the best of both worlds cma_range_alloc()
is changed to run its loop once with the new flag in the hopes of a
quick successful allocation, but if that fails to run again without
the flag. The impact should be that:
1. A successful allocation that did not need to migrate any pages
that are in the LRU cache is faster.
2. A successful allocation that DID need to migrate a page that is in
the LRU cache is slower.
3. A failure to allocate would be slower.
We did also come across Hugh's series [2] which removes the
lru_cache_disable() and lru_cache_enable() calls (and so fixes the
slowness we experience), but the changes are much more extensive, so
we preferred investigating a standalone mechanism to avoid the slow
path, and I think it would still be worth merging if it's correct,
even if Hugh's series will probably replace it when that is merged.
Thanks
Dan
[1] https://www.kernel.org/doc/html/v4.9/media/uapi/v4l/vidioc-reqbufs.html
[2] https://lore.kernel.org/lkml/e28f9a94-4339-f8ac-8301-6be3c9b5b7ce@google.com/
Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
---
Daniel Scally (2):
mm: page_alloc: Add a "lazy" flag for __alloc_contig_migrate_range()
mm: cma: Pass ACR_FLAGS_LAZY to alloc_contig_frozen_range()
include/linux/gfp.h | 1 +
mm/cma.c | 24 ++++++++++++++++++++----
mm/page_alloc.c | 12 ++++++++----
3 files changed, 29 insertions(+), 8 deletions(-)
---
base-commit: 1a7610248a3802e38124ce66b07eef12277be9a0
change-id: 20261202-lazy-cma-allocation-05dabbb8e9ff
Best regards,
--
Daniel Scally <dan.scally@ideasonboard.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] mm: page_alloc: Add a "lazy" flag for __alloc_contig_migrate_range()
2026-12-02 15:13 [PATCH 0/2] Add ACR_FLAGS_LAZY and use it in cma_range_alloc() Daniel Scally
@ 2026-12-02 15:13 ` Daniel Scally
2026-12-02 15:13 ` [PATCH 2/2] mm: cma: Pass ACR_FLAGS_LAZY to alloc_contig_frozen_range() Daniel Scally
1 sibling, 0 replies; 4+ messages in thread
From: Daniel Scally @ 2026-12-02 15:13 UTC (permalink / raw)
To: Vlastimil Babka, Johannes Weiner, Suren Baghdasaryan,
Michal Hocko, Brendan Jackman, Zi Yan, David Hildenbrand,
Liam R. Howlett, Mike Rapoport, Andrew Morton
Cc: linux-mm, hughd, kieran.bingham, Daniel Scally
The lru_cache_disable() call in __alloc_contig_migrate_range() can
cause a performance hit. If we can successfully allocate without it
that would be preferable. Add a new ACR_FLAGS_LAZY to tell functions
to take a lazy path for allocation (I.E. prefer fast-failure over
slow-success), and use it to avoid flushing and disabling the LRU
cache in __alloc_contig_migrate_range(). This will mean that pages
still in the cache are not migrated, and if they're busy will cause
the allocation to fail on __test_page_isolated_in_pageblock() and so
return -EBUSY. The caller of alloc_contig_frozen_range_noprof() would
therefore be expected to re-try without ACR_FLAGS_LAZY.
Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
---
include/linux/gfp.h | 1 +
mm/page_alloc.c | 12 ++++++++----
2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/include/linux/gfp.h b/include/linux/gfp.h
index 63a7654c725fcc8854159130ebc106e611c15b23..b0d53ad7885ec726857379eb8a9b999e2350bdb9 100644
--- a/include/linux/gfp.h
+++ b/include/linux/gfp.h
@@ -386,6 +386,7 @@ extern gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma);
typedef unsigned int __bitwise acr_flags_t;
#define ACR_FLAGS_NONE ((__force acr_flags_t)0) // ordinary allocation request
#define ACR_FLAGS_CMA ((__force acr_flags_t)BIT(0)) // allocate for CMA
+#define ACR_FLAGS_LAZY ((__force acr_flags_t)BIT(1)) // take the lazy path
/* The below functions must be run on a range from a single zone. */
int alloc_contig_frozen_range_noprof(unsigned long start, unsigned long end,
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index aa1ed599c7c6b7ed697e9015b5e024c46b16c28c..fcbe405057d5a5fce816acc706dcb5e5441ac23b 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -7197,7 +7197,8 @@ static void alloc_contig_dump_pages(struct list_head *page_list)
/* [start, end) must belong to a single zone. */
static int __alloc_contig_migrate_range(struct compact_control *cc,
- unsigned long start, unsigned long end)
+ unsigned long start, unsigned long end,
+ acr_flags_t alloc_flags)
{
/* This function is based on compact_zone() from compaction.c. */
unsigned int nr_reclaimed;
@@ -7210,7 +7211,8 @@ static int __alloc_contig_migrate_range(struct compact_control *cc,
.reason = MR_CONTIG_RANGE,
};
- lru_cache_disable();
+ if (!(alloc_flags & ACR_FLAGS_LAZY))
+ lru_cache_disable();
while (pfn < end || !list_empty(&cc->migratepages)) {
if (fatal_signal_pending(current)) {
@@ -7245,7 +7247,9 @@ static int __alloc_contig_migrate_range(struct compact_control *cc,
break;
}
- lru_cache_enable();
+ if (!(alloc_flags & ACR_FLAGS_LAZY))
+ lru_cache_enable();
+
if (ret < 0) {
if (!(cc->gfp_mask & __GFP_NOWARN) && ret == -EBUSY)
alloc_contig_dump_pages(&cc->migratepages);
@@ -7417,7 +7421,7 @@ int alloc_contig_frozen_range_noprof(unsigned long start, unsigned long end,
* allocated. So, if we fall through be sure to clear ret so that
* -EBUSY is not accidentally used or returned to caller.
*/
- ret = __alloc_contig_migrate_range(&cc, start, end);
+ ret = __alloc_contig_migrate_range(&cc, start, end, alloc_flags);
if (ret && ret != -EBUSY)
goto done;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] mm: cma: Pass ACR_FLAGS_LAZY to alloc_contig_frozen_range()
2026-12-02 15:13 [PATCH 0/2] Add ACR_FLAGS_LAZY and use it in cma_range_alloc() Daniel Scally
2026-12-02 15:13 ` [PATCH 1/2] mm: page_alloc: Add a "lazy" flag for __alloc_contig_migrate_range() Daniel Scally
@ 2026-12-02 15:13 ` Daniel Scally
2026-12-03 15:14 ` sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Daniel Scally @ 2026-12-02 15:13 UTC (permalink / raw)
To: Vlastimil Babka, Johannes Weiner, Suren Baghdasaryan,
Michal Hocko, Brendan Jackman, Zi Yan, David Hildenbrand,
Liam R. Howlett, Mike Rapoport, Andrew Morton
Cc: linux-mm, hughd, kieran.bingham, Daniel Scally
Re-work cma_range_alloc() so that two passes are performed; once
with the new ACR_FLAGS_LAZY to try to get a successful allocation
as quickly as possible, and if no free block is found a second pass
without the flag to try the more likely to succeed but slower path.
Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
---
mm/cma.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/mm/cma.c b/mm/cma.c
index 0c97aadcfea4029411171dce3760d68de66047e0..22a9c203cafb9dab38ef4f1441442f99e4e7ba36 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -706,9 +706,10 @@ static void cma_debug_show_areas(struct cma *cma)
spin_unlock_irq(&cma->lock);
}
-static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
- unsigned long count, unsigned int align,
- struct page **pagep, gfp_t gfp)
+static int __cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
+ unsigned long count, unsigned int align,
+ struct page **pagep, gfp_t gfp,
+ acr_flags_t alloc_flags)
{
unsigned long bitmap_maxno, bitmap_no, bitmap_count;
unsigned long start, pfn, mask, offset;
@@ -766,7 +767,7 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
spin_unlock_irq(&cma->lock);
mutex_lock(&cma->alloc_mutex);
- ret = alloc_contig_frozen_range(pfn, pfn + count, ACR_FLAGS_CMA, gfp);
+ ret = alloc_contig_frozen_range(pfn, pfn + count, alloc_flags, gfp);
mutex_unlock(&cma->alloc_mutex);
if (!ret)
break;
@@ -786,6 +787,21 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
return ret;
}
+static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
+ unsigned long count, unsigned int align,
+ struct page **pagep, gfp_t gfp)
+{
+ int ret;
+
+ ret = __cma_range_alloc(cma, cmr, count, align, pagep, gfp,
+ ACR_FLAGS_CMA | ACR_FLAGS_LAZY);
+ if (ret == -EBUSY)
+ ret = __cma_range_alloc(cma, cmr, count, align, pagep, gfp,
+ ACR_FLAGS_CMA);
+
+ return ret;
+}
+
static struct page *__cma_alloc_frozen(struct cma *cma,
unsigned long count, unsigned int align, gfp_t gfp)
{
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 2/2] mm: cma: Pass ACR_FLAGS_LAZY to alloc_contig_frozen_range()
2026-12-02 15:13 ` [PATCH 2/2] mm: cma: Pass ACR_FLAGS_LAZY to alloc_contig_frozen_range() Daniel Scally
@ 2026-12-03 15:14 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-03 15:14 UTC (permalink / raw)
To: Daniel Scally, Vlastimil Babka, Johannes Weiner,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman, Zi Yan,
David Hildenbrand, Liam R. Howlett, Mike Rapoport, Andrew Morton
Cc: linux-mm, hughd, kieran.bingham
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] mm: cma: migration thrashing in cma_range_alloc() on
ACR_FLAGS_LAZY pass
--
commit bff27908fdd0da13045bbc4d2d80382f106a9a63
Author: Daniel Scally <dan.scally@ideasonboard.com>
mm: cma: Pass ACR_FLAGS_LAZY to alloc_contig_frozen_range()
This commit reworks cma_range_alloc() to attempt contiguous allocation across
the CMA range with ACR_FLAGS_LAZY first, falling back to a second pass
without ACR_FLAGS_LAZY if the first pass returns -EBUSY.
> diff --git a/mm/cma.c b/mm/cma.c
> index 0c97aadcfea40..22a9c203cafb9 100644
> --- a/mm/cma.c
> +++ b/mm/cma.c
> @@ -706,9 +706,10 @@ static void cma_debug_show_areas(struct cma *cma)
> spin_unlock_irq(&cma->lock);
> }
>
> -static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
> - unsigned long count, unsigned int align,
> - struct page **pagep, gfp_t gfp)
> +static int __cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
> + unsigned long count, unsigned int align,
> + struct page **pagep, gfp_t gfp,
> + acr_flags_t alloc_flags)
> {
> unsigned long bitmap_maxno, bitmap_no, bitmap_count;
> unsigned long start, pfn, mask, offset;
> @@ -766,7 +767,7 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
> spin_unlock_irq(&cma->lock);
>
> mutex_lock(&cma->alloc_mutex);
> - ret = alloc_contig_frozen_range(pfn, pfn + count, ACR_FLAGS_CMA, gfp);
> + ret = alloc_contig_frozen_range(pfn, pfn + count, alloc_flags, gfp);
> mutex_unlock(&cma->alloc_mutex);
> if (!ret)
> break;
> @@ -786,6 +787,21 @@ static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
> return ret;
> }
>
> +static int cma_range_alloc(struct cma *cma, struct cma_memrange *cmr,
> + unsigned long count, unsigned int align,
> + struct page **pagep, gfp_t gfp)
> +{
> + int ret;
> +
> + ret = __cma_range_alloc(cma, cmr, count, align, pagep, gfp,
> + ACR_FLAGS_CMA | ACR_FLAGS_LAZY);
> + if (ret == -EBUSY)
> + ret = __cma_range_alloc(cma, cmr, count, align, pagep, gfp,
> + ACR_FLAGS_CMA);
> +
> + return ret;
> +}
[Severity: High]
Can the ACR_FLAGS_LAZY pass thrash page migrations across the entire CMA
range instead of failing fast when movable pages sit in per-CPU LRU batches?
In __alloc_contig_migrate_range(), ACR_FLAGS_LAZY only skips
lru_cache_disable() while still running the full pageblock isolation,
reclaim, and 5-try synchronous migrate_pages() loop on every candidate window
in cmr->bitmap:
mm/page_alloc.c:__alloc_contig_migrate_range() {
...
if (!(alloc_flags & ACR_FLAGS_LAZY))
lru_cache_disable();
while (pfn < end || !list_empty(&cc->migratepages)) {
...
}
When a window contains a folio queued in a per-CPU LRU batch,
alloc_contig_frozen_range() still migrates all other LRU folios out of the
window (or retries migration 5 times if the batched folio is file-backed and
already has PG_lru set) before failing with -EBUSY. Because
lru_cache_disabled() is false during the lazy pass, migrate_folio_move() ->
folio_add_lru() and folio_putback_lru() also place newly migrated and
put-back folios into cpu_fbatches.lru_add without PG_lru set. If those
destination folios land in higher unisolated MIGRATE_CMA pageblocks, later
windows in the same lazy scan fail test_pages_isolated() on the folios it
just migrated, and when bitmap_count > mask + 1, advancing start by mask + 1
repeatedly re-isolates and migrates overlapping windows across the same
un-drained folio while holding cma->alloc_mutex.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-lazy-cma-allocation-v1-0-af432f823fbd@ideasonboard.com?part=2
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-03 15:14 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 15:13 [PATCH 0/2] Add ACR_FLAGS_LAZY and use it in cma_range_alloc() Daniel Scally
2026-12-02 15:13 ` [PATCH 1/2] mm: page_alloc: Add a "lazy" flag for __alloc_contig_migrate_range() Daniel Scally
2026-12-02 15:13 ` [PATCH 2/2] mm: cma: Pass ACR_FLAGS_LAZY to alloc_contig_frozen_range() Daniel Scally
2026-12-03 15:14 ` sashiko-bot
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.