Skip to contentSkip to conversation

mm/cma: move put_page_testzero() out of VM_WARN_ON in cma_release()

PATCH Applied Zi Yan 8 participants 9 messages Feb 25 – Mar 12

Subsystems (3)
lore
Share & tools
lore

Zi Yan

author
#0 lore raw

PATCH v3 1/1 mm/cma: move put_page_testzero() out of VM_WARN_ON in cma_release()

When CONFIG_DEBUG_VM is not set, VM_WARN_ON is a NOP. Putting any statement with side effect inside it is incorrect. Collect all !put_page_testzero() results and check the sum using WARN instead after the loop. It restores the same check in free_contig_range() before commit e0c1326779cc ("mm: page_alloc: add alloc_contig_frozen_{range,pages}()"), the commit prior to the Fixes one.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")

---
From V2:
- Collect !put_page_testzero() to get the right result.

From V1:
- Collect all put_page_testzero() results and do a single WARN after the loop.

 mm/cma.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/cma.c +4 -1
index 94b5da468a7d..15cc0ae76c8e 100644
@@ -1013,6 +1013,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
 		 unsigned long count)
 {
 	struct cma_memrange *cmr;
+	unsigned long ret = 0;
 	unsigned long i, pfn;
 
 	cmr = find_cma_memrange(cma, pages, count);
@@ -1021,7 +1022,9 @@ bool cma_release(struct cma *cma, const struct page *pages,
 
 	pfn = page_to_pfn(pages);
 	for (i = 0; i < count; i++, pfn++)
-		VM_WARN_ON(!put_page_testzero(pfn_to_page(pfn)));
+		ret += !put_page_testzero(pfn_to_page(pfn));
+
+	WARN(ret, "%lu pages are still in use!\n", ret);
 
 	__cma_release_frozen(cma, cmr, pages, count);
 
Signature · 1 line
2.51.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 36 lines · Zi Yan

When CONFIG_DEBUG_VM is not set, VM_WARN_ON is a NOP. Putting any statement with side effect inside it is incorrect. Collect all !put_page_testzero() results and check the sum using WARN instead after the loop. It restores the same check in free_contig_range() before commit e0c1326779cc ("mm: page_alloc: add alloc_contig_frozen_{range,pages}()"), the commit prior to the Fixes one.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")

---
From V2:
- Collect !put_page_testzero() to get the right result.

From V1:
- Collect all put_page_testzero() results and do a single WARN after the loop.

 mm/cma.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/cma.c +1 -0
index 94b5da468a7d..15cc0ae76c8e 100644
@@ -1013,6 +1013,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
 		 unsigned long count)
 {
 	struct cma_memrange *cmr;
+	unsigned long ret = 0;
 	unsigned long i, pfn;
 
 	cmr = find_cma_memrange(cma, pages, count);

mm/cma.c bool cma_release(struct cma *cma, const struct page *pages, · line 1030

Quoted 10 lines
mm/cma.c +3 -1
@@ -1021,7 +1022,9 @@ bool cma_release(struct cma *cma, const struct page *pages,
 
 	pfn = page_to_pfn(pages);
 	for (i = 0; i < count; i++, pfn++)
-		VM_WARN_ON(!put_page_testzero(pfn_to_page(pfn)));
+		ret += !put_page_testzero(pfn_to_page(pfn));
+
+	WARN(ret, "%lu pages are still in use!\n", ret);
 
 	__cma_release_frozen(cma, cmr, pages, count);
 

Makes sense to make the above warning unconditional regardless of CONFIG_DEBUG_VM.

Reviewed-by on v3 1/1
#2 lore raw
In reply to Zi Yan
Quoted 12 lines · Zi Yan

When CONFIG_DEBUG_VM is not set, VM_WARN_ON is a NOP. Putting any statement with side effect inside it is incorrect. Collect all !put_page_testzero() results and check the sum using WARN instead after the loop. It restores the same check in free_contig_range() before commit e0c1326779cc ("mm: page_alloc: add alloc_contig_frozen_{range,pages}()"), the commit prior to the Fixes one.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")
Quoted 8 lines

---
From V2:
- Collect !put_page_testzero() to get the right result.

From V1:
- Collect all put_page_testzero() results and do a single WARN after the loop.

Quoted 26 lines
  mm/cma.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/cma.c +4 -1
index 94b5da468a7d..15cc0ae76c8e 100644
@@ -1013,6 +1013,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
  		 unsigned long count)
  {
  	struct cma_memrange *cmr;
+	unsigned long ret = 0;
  	unsigned long i, pfn;
 
  	cmr = find_cma_memrange(cma, pages, count);
@@ -1021,7 +1022,9 @@ bool cma_release(struct cma *cma, const struct page *pages,
 
  	pfn = page_to_pfn(pages);
  	for (i = 0; i < count; i++, pfn++)
-		VM_WARN_ON(!put_page_testzero(pfn_to_page(pfn)));
+		ret += !put_page_testzero(pfn_to_page(pfn));
+
+	WARN(ret, "%lu pages are still in use!\n", ret);
 
  	__cma_release_frozen(cma, cmr, pages, count);
 
Tested-by on v3 1/1
#3 lore raw
In reply to Zi Yan
Quoted 36 lines · Zi Yan

When CONFIG_DEBUG_VM is not set, VM_WARN_ON is a NOP. Putting any statement with side effect inside it is incorrect. Collect all !put_page_testzero() results and check the sum using WARN instead after the loop. It restores the same check in free_contig_range() before commit e0c1326779cc ("mm: page_alloc: add alloc_contig_frozen_{range,pages}()"), the commit prior to the Fixes one.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")

---
From V2:
- Collect !put_page_testzero() to get the right result.

From V1:
- Collect all put_page_testzero() results and do a single WARN after the loop.

  mm/cma.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/cma.c +1 -0
index 94b5da468a7d..15cc0ae76c8e 100644
@@ -1013,6 +1013,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
  		 unsigned long count)
  {
  	struct cma_memrange *cmr;
+	unsigned long ret = 0;
  	unsigned long i, pfn;
 
  	cmr = find_cma_memrange(cma, pages, count);

mm/cma.c bool cma_release(struct cma *cma, const struct page *pages, · line 1029

Quoted 9 lines
mm/cma.c +3 -1
@@ -1021,7 +1022,9 @@ bool cma_release(struct cma *cma, const struct page *pages,
 
  	pfn = page_to_pfn(pages);
  	for (i = 0; i < count; i++, pfn++)
-		VM_WARN_ON(!put_page_testzero(pfn_to_page(pfn)));
+		ret += !put_page_testzero(pfn_to_page(pfn));
+
+	WARN(ret, "%lu pages are still in use!\n", ret);
 
  	__cma_release_frozen(cma, cmr, pages, count);
Tested-by on v3 1/1
#4 lore raw
In reply to Zi Yan
Quoted 36 lines · Zi Yan

When CONFIG_DEBUG_VM is not set, VM_WARN_ON is a NOP. Putting any statement with side effect inside it is incorrect. Collect all !put_page_testzero() results and check the sum using WARN instead after the loop. It restores the same check in free_contig_range() before commit e0c1326779cc ("mm: page_alloc: add alloc_contig_frozen_{range,pages}()"), the commit prior to the Fixes one.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")

---
From V2:
- Collect !put_page_testzero() to get the right result.

From V1:
- Collect all put_page_testzero() results and do a single WARN after the loop.

  mm/cma.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/cma.c +1 -0
index 94b5da468a7d..15cc0ae76c8e 100644
@@ -1013,6 +1013,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
  		 unsigned long count)
  {
  	struct cma_memrange *cmr;
+	unsigned long ret = 0;
  	unsigned long i, pfn;
 
  	cmr = find_cma_memrange(cma, pages, count);

mm/cma.c bool cma_release(struct cma *cma, const struct page *pages, · line 1029

Quoted 9 lines
mm/cma.c +3 -1
@@ -1021,7 +1022,9 @@ bool cma_release(struct cma *cma, const struct page *pages,
 
  	pfn = page_to_pfn(pages);
  	for (i = 0; i < count; i++, pfn++)
-		VM_WARN_ON(!put_page_testzero(pfn_to_page(pfn)));
+		ret += !put_page_testzero(pfn_to_page(pfn));
+
+	WARN(ret, "%lu pages are still in use!\n", ret);
 
  	__cma_release_frozen(cma, cmr, pages, count);
maintainer Acked-by on v3 1/1
#5 lore raw
In reply to Zi Yan
Quoted 36 lines · Zi Yan

When CONFIG_DEBUG_VM is not set, VM_WARN_ON is a NOP. Putting any statement with side effect inside it is incorrect. Collect all !put_page_testzero() results and check the sum using WARN instead after the loop. It restores the same check in free_contig_range() before commit e0c1326779cc ("mm: page_alloc: add alloc_contig_frozen_{range,pages}()"), the commit prior to the Fixes one.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")

---
From V2:
- Collect !put_page_testzero() to get the right result.

From V1:
- Collect all put_page_testzero() results and do a single WARN after the loop.

 mm/cma.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/cma.c +1 -0
index 94b5da468a7d..15cc0ae76c8e 100644
@@ -1013,6 +1013,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
 		 unsigned long count)
 {
 	struct cma_memrange *cmr;
+	unsigned long ret = 0;
 	unsigned long i, pfn;
 
 	cmr = find_cma_memrange(cma, pages, count);

mm/cma.c bool cma_release(struct cma *cma, const struct page *pages, · line 1030

Quoted 10 lines
mm/cma.c +3 -1
@@ -1021,7 +1022,9 @@ bool cma_release(struct cma *cma, const struct page *pages,
 
 	pfn = page_to_pfn(pages);
 	for (i = 0; i < count; i++, pfn++)
-		VM_WARN_ON(!put_page_testzero(pfn_to_page(pfn)));
+		ret += !put_page_testzero(pfn_to_page(pfn));
+
+	WARN(ret, "%lu pages are still in use!\n", ret);
 
 	__cma_release_frozen(cma, cmr, pages, count);
 

Thanks for taking care of the fix.

Signature · 3 lines
Cheers,

David
Reviewed-by on v3 1/1
#6 lore raw
In reply to Zi Yan
Quoted 6 lines · Zi Yan

When CONFIG_DEBUG_VM is not set, VM_WARN_ON is a NOP. Putting any statement with side effect inside it is incorrect. Collect all !put_page_testzero() results and check the sum using WARN instead after the loop. It restores the same check in free_contig_range() before commit e0c1326779cc ("mm: page_alloc: add alloc_contig_frozen_{range,pages}()"), the commit prior to the Fixes one.

LGTM.

Reviewed-by on v3 1/1
#7 lore raw
In reply to Zi Yan
Quoted 7 lines · On Tue, 24 Feb 2026 22:12:31 -0500 Zi Yan <[email protected]> wrote:

When CONFIG_DEBUG_VM is not set, VM_WARN_ON is a NOP. Putting any statement with side effect inside it is incorrect. Collect all !put_page_testzero() results and check the sum using WARN instead after the loop. It restores the same check in free_contig_range() before commit e0c1326779cc ("mm: page_alloc: add alloc_contig_frozen_{range,pages}()"), the commit prior to the Fixes one.

Quoted 5 lines
Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")

Thanks,
SJ

[...]

maintainer Tested-by on v3 1/1
#8 lore raw
In reply to Zi Yan
Quoted 36 lines · Zi Yan

When CONFIG_DEBUG_VM is not set, VM_WARN_ON is a NOP. Putting any statement with side effect inside it is incorrect. Collect all !put_page_testzero() results and check the sum using WARN instead after the loop. It restores the same check in free_contig_range() before commit e0c1326779cc ("mm: page_alloc: add alloc_contig_frozen_{range,pages}()"), the commit prior to the Fixes one.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")

---

Quoted 1 line

From V2:

- Collect !put_page_testzero() to get the right result.

Quoted 1 line

From V1:

- Collect all put_page_testzero() results and do a single WARN after the loop.

  mm/cma.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/cma.c +1 -0
index 94b5da468a7d..15cc0ae76c8e 100644
@@ -1013,6 +1013,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
  		 unsigned long count)
  {
  	struct cma_memrange *cmr;
+	unsigned long ret = 0;
  	unsigned long i, pfn;
 
  	cmr = find_cma_memrange(cma, pages, count);

mm/cma.c bool cma_release(struct cma *cma, const struct page *pages, · line 1030

Quoted 10 lines
mm/cma.c +3 -1
@@ -1021,7 +1022,9 @@ bool cma_release(struct cma *cma, const struct page *pages,
 
  	pfn = page_to_pfn(pages);
  	for (i = 0; i < count; i++, pfn++)
-		VM_WARN_ON(!put_page_testzero(pfn_to_page(pfn)));
+		ret += !put_page_testzero(pfn_to_page(pfn));
+
+	WARN(ret, "%lu pages are still in use!\n", ret);
 
  	__cma_release_frozen(cma, cmr, pages, count);
 

This fixes a regression I was seeing on Tegra. So ...

Thanks!
Jon

Signature · 1 line
nvpublic