mirror of
https://github.com/torvalds/linux.git
synced 2026-11-20 13:03:48 +00:00
mm/cma: don't release CMA pages still in use
When a driver calls dma_free_contiguous() before quiescing DMA, the
page might still have a reference from the device.
put_page_testzero() there returns false, WARN fires, but the code proceeds
to free_contig_frozen_range() putting a live page onto buddy and clearing
the bitmap. A subsequent allocation can hand the same PFN to a new
owner while the original holder still references it.
A concurrent put_page() that drops the last reference between the
testzero loop and free_contig_frozen_range() can double-queue the page
via page->lru, corrupting buddy lists.
Avoid the corruption by not freeing a CMA region if any of the
pages inside are still in use.
Make it explicit in the warning that the driver allowed a leak.
While at it, use page_ref_freeze() to make the semantics clearer and
switch to pr_warn_ratelimited().
Fixes: 9bda131c60 ("mm: cma: add cma_alloc_frozen{_compound}()")
Cc: [email protected]
Signed-off-by: Rik van Riel <[email protected]>
Link: https://patch.msgid.link/20260811132045.5f114772@fangorn
[ david: use page_ref_freeze() + pr_warn_ratelimited() ]
Acked-by: Lorenzo Stoakes (ARM) <[email protected]>
Acked-by: Muchun Song <[email protected]>
Tested-by: Muhammad Usama Anjum <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: David Hildenbrand (Arm) <[email protected]>
This commit is contained in:
committed by
David Hildenbrand (Arm)
parent
b1ee878021
commit
e8abc61d8a
@@ -1020,7 +1020,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
|
||||
unsigned long count)
|
||||
{
|
||||
struct cma_memrange *cmr;
|
||||
unsigned long ret = 0;
|
||||
unsigned long leaked = 0;
|
||||
unsigned long i, pfn;
|
||||
|
||||
cmr = find_cma_memrange(cma, pages, count);
|
||||
@@ -1029,9 +1029,13 @@ bool cma_release(struct cma *cma, const struct page *pages,
|
||||
|
||||
pfn = page_to_pfn(pages);
|
||||
for (i = 0; i < count; i++, pfn++)
|
||||
ret += !put_page_testzero(pfn_to_page(pfn));
|
||||
leaked += !page_ref_freeze(pfn_to_page(pfn), 1);
|
||||
|
||||
WARN(ret, "%lu pages are still in use!\n", ret);
|
||||
if (leaked) {
|
||||
pr_warn_ratelimited("%lu pages are still in use, not freeing CMA region!\n",
|
||||
leaked);
|
||||
return true;
|
||||
}
|
||||
|
||||
__cma_release_frozen(cma, cmr, pages, count);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user