mm: cma: introduce a non-blocking version of cma_release()
Subsystems (3)
cover mm: cma: introduce a non-blocking version of cma_release()
This small patchset introduces a non-blocking version of cma_release() and simplifies the code in hugetlbfs, where previously we had to temporarily drop hugetlb_lock around the cma_release() call.
It should help Zi Yan on his work on 1 GB THPs: splitting a gigantic THP under a memory pressure requires a cma_release() call. If it's a blocking function, it complicates the already complicated code. Because there are at least two use cases like this (hugetlbfs is another example), I believe it's just better to make cma_release() non-blocking.
v1:
- introduce cma_release_nowait() instead of making cma_release() non-blocking, for performance reasons
rfc:
https://lkml.org/lkml/2020/10/16/1050
Roman Gushchin (2):
mm: cma: introduce cma_release_nowait()
mm: hugetlb: don't drop hugetlb_lock around cma_release() call
include/linux/cma.h | 2 + mm/cma.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ mm/cma.h | 5 +++ mm/hugetlb.c | 11 ++---- 4 files changed, 103 insertions(+), 8 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/2 mm: cma: introduce cma_release_nowait()
cma_release() has to lock the cma_lock mutex to clear the cma bitmap. It makes it a blocking function, which complicates its usage from non-blocking contexts. For instance, hugetlbfs code is temporarily dropping the hugetlb_lock spinlock to call cma_release().
This patch introduces a non-blocking cma_release_nowait(), which postpones the cma bitmap clearance. It's done later from a work context. The first page in the cma allocation is used to store the work struct. Because CMA allocations and de-allocations are usually not that frequent, a single global workqueue is used.
To make sure that subsequent cma_alloc() call will pass, cma_alloc() flushes the cma_release_wq workqueue. To avoid a performance regression in the case when only cma_release() is used, gate it by a per-cma area flag, which is set by the first call
of cma_release_nowait().
--- include/linux/cma.h | 2 + mm/cma.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ mm/cma.h | 5 +++ 3 files changed, 100 insertions(+)
include/linux/cma.h +2 -0
@@ -47,6 +47,8 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,extern struct page *cma_alloc (struct cma *cma,size_t count,unsigned int align,bool no_warn);extern bool cma_release (struct cma *cma,const struct page *pages,unsigned int count); +extern bool cma_release_nowait (struct cma *cma,const struct page *pages, +unsigned int count);extern int cma_for_each_area (int (*it)(struct cma *cma,void *data),void *data);#endif
mm/cma.c +93 -0
127 lines not on this page · raw message
mm/cma.h +5 -0
@@ -7,6 +7,7 @@struct cma {unsigned long base_pfn;unsigned long count; +unsigned long flags;unsigned long *bitmap;unsigned int order_per_bit;/* Order of pages represented by one bit */ struct mutex lock;
@@ -18,6 +19,10 @@ struct cma {char name[CMA_MAX_NAME ]; }; +enum cma_flags { +CMA_DELAYED_RELEASE ,/* cma bitmap is cleared asynchronously */ +}; +extern struct cma cma_areas[MAX_CMA_AREAS ];extern unsigned cma_area_count;
Signature · 1 line
2.26.2
Re: [PATCH v1 1/2] mm: cma: introduce cma_release_nowait()
Btw, I think we also need to use this nonblocking version from dma_free_contiguous. dma_free* is defined to not block. In practice callers mostly care if they also did GFP_ATOMIC allocations, which don't dip into CMA, but I think we do have a problem.
Re: [PATCH v1 1/2] mm: cma: introduce cma_release_nowait()
Quoted 4 lines · Christoph Hellwig
Btw, I think we also need to use this nonblocking version from
dma_free_contiguous. dma_free* is defined to not block. In practice callers mostly care if they also didGFP_ATOMICallocations, which don't dip into CMA, but I think we do have a problem.
It's a good point!
Do you mind mastering a patch? I can include it into the patchset.
Thanks!
PATCH 2/2 mm: hugetlb: don't drop hugetlb_lock around cma_release() call
In reply to Roman GushchinReplace blocking cma_release() with a non-blocking cma_release_nowait() call, so there is no more need to temporarily drop hugetlb_lock.
--- mm/hugetlb.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
mm/hugetlb.c +3 -8
@@ -1224,10 +1224,11 @@ static void free_gigantic_page(struct page *page, unsigned int order) {/* * If the page isn't allocated using the cma allocator, -* cma_release() returns false. +* cma_release_nowait() returns false. */ #ifdef CONFIG_CMA -if (cma_release (hugetlb_cma[page_to_nid (page)], page,1 << order)) +if (cma_release_nowait (hugetlb_cma[page_to_nid (page)], page, +1 << order))return ;#endif
@@ -1312,14 +1313,8 @@ static void update_and_free_page(struct hstate *h, struct page *page)set_compound_page_dtor (page,NULL_COMPOUND_DTOR );set_page_refcounted (page);if (hstate_is_gigantic (h)) { -/* -* Temporarily drop the hugetlb_lock, because -* we might block in free_gigantic_page(). -*/ -spin_unlock (&hugetlb_lock);destroy_compound_gigantic_page (page,huge_page_order (h));free_gigantic_page (page,huge_page_order (h)); -spin_lock (&hugetlb_lock); }else {__free_pages (page,huge_page_order (h)); }
Signature · 1 line
2.26.2
Quoted 6 lines · Roman Gushchin
This small patchset introduces a non-blocking version of
cma_release()and simplifies the code in hugetlbfs, where previously we had to temporarily drophugetlb_lockaround thecma_release()call.It should help Zi Yan on his work on 1 GB THPs: splitting a gigantic THP under a memory pressure requires a
cma_release()call. If it's
Thanks for the patch. But during 1GB THP split, we only clear the bitmaps without releasing the pages. Also in cma_release_nowait(), the first page in the allocated CMA region is reused to store struct cma_clear_bitmap_work, but the same method cannot be used during THP split, since the first page is still in-use. We might need to allocate some new memory for struct cma_clear_bitmap_work, which might not be successful under memory pressure. Any suggestion on where to store struct cma_clear_bitmap_work when I only want to clear bitmap without releasing the pages?
Thanks.
—
Best Regards,
Yan Zi
Quoted 9 lines · Zi Yan
Quoted 6 lines · Roman Gushchin
This small patchset introduces a non-blocking version of
cma_release()and simplifies the code in hugetlbfs, where previously we had to temporarily drophugetlb_lockaround thecma_release()call.It should help Zi Yan on his work on 1 GB THPs: splitting a gigantic THP under a memory pressure requires a
cma_release()call. If it's
Quoted 9 lines
Thanks for the patch. But during 1GB THP split, we only clear the bitmaps without releasing the pages. Also in
cma_release_nowait(), the first page in the allocated CMA region is reused to store structcma_clear_bitmap_work, but the same method cannot be used during THP split, since the first page is still in-use. We might need to allocate some new memory for structcma_clear_bitmap_work, which might not be successful under memory pressure. Any suggestion on where to store structcma_clear_bitmap_workwhen I only want to clear bitmap without releasing the pages?
It means we can't use cma_release() there either, because it does clear individual pages. We need to clear the cma bitmap without touching pages.
Can you handle an error there?
If so, we can introduce something like int cma_schedule_bitmap_clearance(), which will allocate a work structure and will be able to return -ENOMEM in the unlikely case of error.
Will it work for you?
Thanks!
Quoted 23 lines · Roman Gushchin
Quoted 9 lines · Zi Yan
Quoted 6 lines · Roman Gushchin
This small patchset introduces a non-blocking version of
cma_release()and simplifies the code in hugetlbfs, where previously we had to temporarily drophugetlb_lockaround thecma_release()call.It should help Zi Yan on his work on 1 GB THPs: splitting a gigantic THP under a memory pressure requires a
cma_release()call. If it'sQuoted 9 lines
Thanks for the patch. But during 1GB THP split, we only clear the bitmaps without releasing the pages. Also in
cma_release_nowait(), the first page in the allocated CMA region is reused to store structcma_clear_bitmap_work, but the same method cannot be used during THP split, since the first page is still in-use. We might need to allocate some new memory for structcma_clear_bitmap_work, which might not be successful under memory pressure. Any suggestion on where to store structcma_clear_bitmap_workwhen I only want to clear bitmap without releasing the pages?It means we can't use
cma_release()there either, because it does clear individual pages. We need to clear the cma bitmap without touching pages.
Quoted 7 lines
Can you handle an error there?
If so, we can introduce something like int
cma_schedule_bitmap_clearance(), which will allocate a work structure and will be able to return-ENOMEMin the unlikely case of error.Will it work for you?
Yes, it works. Thanks.
—
Best Regards,
Yan Zi
Quoted 27 lines · Zi Yan
Quoted 25 lines · Roman Gushchin
Quoted 9 lines · Zi Yan
Quoted 6 lines · Roman Gushchin
This small patchset introduces a non-blocking version of
cma_release()and simplifies the code in hugetlbfs, where previously we had to temporarily drophugetlb_lockaround thecma_release()call.It should help Zi Yan on his work on 1 GB THPs: splitting a gigantic THP under a memory pressure requires a
cma_release()call. If it'sQuoted 9 lines
Thanks for the patch. But during 1GB THP split, we only clear the bitmaps without releasing the pages. Also in
cma_release_nowait(), the first page in the allocated CMA region is reused to store structcma_clear_bitmap_work, but the same method cannot be used during THP split, since the first page is still in-use. We might need to allocate some new memory for structcma_clear_bitmap_work, which might not be successful under memory pressure. Any suggestion on where to store structcma_clear_bitmap_workwhen I only want to clear bitmap without releasing the pages?It means we can't use
cma_release()there either, because it does clear individual pages. We need to clear the cma bitmap without touching pages.Can you handle an error there?
Quoted 7 lines
Quoted 5 lines
If so, we can introduce something like int
cma_schedule_bitmap_clearance(), which will allocate a work structure and will be able to return-ENOMEMin the unlikely case of error.Will it work for you?
Yes, it works. Thanks.
Nice!
It means we can keep these two patches as they are (they do makes sense even without THP, because they simplify the hugetlbfs code).
I'll send a patch implementing cma_schedule_bitmap_clearance() to you, so you'll be able to use it in the patchset.
Thanks!