Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH v3] mm/cma: don't release CMA pages still in use
@ 2026-08-11 17:20 Rik van Riel
  2026-08-20 18:16 ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 2+ messages in thread
From: Rik van Riel @ 2026-08-11 17:20 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Andrew Morton, Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, linux-mm,
	linux-kernel

When a driver calls dma_free_contiguous() before quiescing DMA, the
page still has 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.

Fixes: 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")
Link: https://lore.kernel.org/linux-mm/20260809210608.06b5ccb9@fangorn/ [v1]
Link: https://lore.kernel.org/linux-mm/20260810122737.030f8452@fangorn/ [v2]
Cc: stable@vger.kernel.org
Signed-off-by: Rik van Riel <riel@surriel.com>
---
v3:
 - simplify things by simply leaking the CMA range, drivers should not
   call cma_release() while the space is still in use (David Hildenbrand)

 mm/cma.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/mm/cma.c b/mm/cma.c
index a13ce4999b39..c1425f25c39b 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -1018,7 +1018,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);
@@ -1027,9 +1027,12 @@ 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 += !put_page_testzero(pfn_to_page(pfn));
 
-	WARN(ret, "%lu pages are still in use!\n", ret);
+	if (leaked) {
+		WARN(1, "%lu pages are still in use, not freeing CMA region!\n", leaked);
+		return true;
+	}
 
 	__cma_release_frozen(cma, cmr, pages, count);
 
-- 
2.55.0




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [RFC PATCH v3] mm/cma: don't release CMA pages still in use
  2026-08-11 17:20 [RFC PATCH v3] mm/cma: don't release CMA pages still in use Rik van Riel
@ 2026-08-20 18:16 ` David Hildenbrand (Arm)
  0 siblings, 0 replies; 2+ messages in thread
From: David Hildenbrand (Arm) @ 2026-08-20 18:16 UTC (permalink / raw)
  To: Rik van Riel
  Cc: Andrew Morton, Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, linux-mm,
	linux-kernel

On 8/11/26 19:20, Rik van Riel wrote:
> When a driver calls dma_free_contiguous() before quiescing DMA, the
> page still has 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.
> 
> Fixes: 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")
> Link: https://lore.kernel.org/linux-mm/20260809210608.06b5ccb9@fangorn/ [v1]
> Link: https://lore.kernel.org/linux-mm/20260810122737.030f8452@fangorn/ [v2]
> Cc: stable@vger.kernel.org

As discussed, I don't like stable here. Fixes is debatable (code was more robust
before 9bda131c6093).

Code that actually triggers this is the problematic code, this here is rather
making the code more robust for buggy drivers.

> Signed-off-by: Rik van Riel <riel@surriel.com>
> ---
> v3:
>  - simplify things by simply leaking the CMA range, drivers should not
>    call cma_release() while the space is still in use (David Hildenbrand)
> 
>  mm/cma.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/cma.c b/mm/cma.c
> index a13ce4999b39..c1425f25c39b 100644
> --- a/mm/cma.c
> +++ b/mm/cma.c
> @@ -1018,7 +1018,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);
> @@ -1027,9 +1027,12 @@ 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 += !put_page_testzero(pfn_to_page(pfn));
>  
> -	WARN(ret, "%lu pages are still in use!\n", ret);
> +	if (leaked) {
> +		WARN(1, "%lu pages are still in use, not freeing CMA region!\n", leaked);
> +		return true;
> +	}
>  
>  	__cma_release_frozen(cma, cmr, pages, count);
>  

Okay, that will leave the frozen and the CMA bits remains set.

It's a bit inconsistent, because pages the buggy driver will release later will
end up in the buddy, whereby pages we just freed will not end up in the buddy.

We could consider the following instead:

	leaked += !page_ref_freeze(pfn_to_page(pfn), 1);

(I hope I got that conditional right)

Then we will just consistently leak all pages (keeping them allocated with a
refcount of 1).

Oh, and it nicely matches the "__cma_release_frozen" terminology later ;)

(on a related note, the code in general looks a bit shaky regarding speculative
references, but we saw no reports so far ... it will all get clearer once more
code is converted to use frozen pages so we won't even run into this)

-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-08-20 18:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-11 17:20 [RFC PATCH v3] mm/cma: don't release CMA pages still in use Rik van Riel
2026-08-20 18:16 ` David Hildenbrand (Arm)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox