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 --- 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 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 --- 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