public inbox for drm-ai-reviews@public-inbox.freedesktop.org
 help / color / mirror / Atom feed
* Claude review: dma-buf: heaps: Clear CMA pages with clear_pages()
  2026-03-04  0:25 [PATCH] dma-buf: heaps: Clear CMA pages with clear_pages() Linus Walleij
  2026-03-05  4:00 ` Claude review: " Claude Code Review Bot
@ 2026-03-05  4:00 ` Claude Code Review Bot
  1 sibling, 0 replies; 12+ messages in thread
From: Claude Code Review Bot @ 2026-03-05  4:00 UTC (permalink / raw)
  To: dri-devel-reviews

Overall Series Review

Subject: dma-buf: heaps: Clear CMA pages with clear_pages()
Author: Linus Walleij <linusw@kernel.org>
Patches: 1
Reviewed: 2026-03-05T14:00:37.741553

---

This is a single-patch series that replaces `memset(page_address(cma_pages), 0, size)` with `clear_pages(page_address(cma_pages), pagecount)` in the CMA heap allocator's non-highmem path. The change leverages the newer `clear_pages()` API (introduced by commit 62a9f5a85b98) which can use architecture-optimized page-clearing instructions.

The conversion is straightforward and functionally correct. However, there is one concern regarding preemption latency that should be addressed.

---
Generated by Claude Code Patch Reviewer

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

* Claude review: dma-buf: heaps: Clear CMA pages with clear_pages()
  2026-03-04  0:25 [PATCH] dma-buf: heaps: Clear CMA pages with clear_pages() Linus Walleij
@ 2026-03-05  4:00 ` Claude Code Review Bot
  2026-03-05  4:00 ` Claude Code Review Bot
  1 sibling, 0 replies; 12+ messages in thread
From: Claude Code Review Bot @ 2026-03-05  4:00 UTC (permalink / raw)
  To: dri-devel-reviews

Patch Review

**Correctness of the API change**: The conversion from `memset(..., 0, size)` to `clear_pages(addr, pagecount)` is semantically correct — both zero out the same memory range. The `page_address()` return value is suitable for `clear_pages()`.

**Preemption latency concern**: The `clear_pages()` documentation in `include/linux/mm.h:4410-4417` explicitly states:

> clear_pages() does not (and on architectures where it reduces to a few long-running instructions, might not be able to) call cond_resched() to check if rescheduling is required.
> ...the caller is expected to limit @npages to no more than PROCESS_PAGES_NON_PREEMPT_BATCH.

Here, `pagecount` is derived directly from the user-supplied `len` parameter at line 303-304:

```c
size_t size = PAGE_ALIGN(len);
pgoff_t pagecount = size >> PAGE_SHIFT;
```

CMA allocations can be quite large (tens or hundreds of MB). On architectures that define `clear_pages`, `PROCESS_PAGES_NON_PREEMPT_BATCH` is `SZ_32M >> PAGE_SHIFT` (8192 pages). If a user requests, say, a 256MB buffer, `pagecount` would be 65536, far exceeding the recommended batch limit.

The original `memset()` had the same theoretical issue (no `cond_resched()`), so this isn't a regression in that sense. But adopting `clear_pages()` while ignoring its documented calling convention is worth noting. Consider batching the clear in a loop with `cond_resched()` checks, similar to how the HighMem path iterates page-by-page with a `fatal_signal_pending()` check. For example:

```c
unsigned long remaining = pagecount;
void *addr = page_address(cma_pages);
while (remaining > 0) {
    unsigned int batch = min_t(unsigned long, remaining,
                               PROCESS_PAGES_NON_PREEMPT_BATCH);
    clear_pages(addr, batch);
    addr += batch * PAGE_SIZE;
    remaining -= batch;
    cond_resched();
    if (fatal_signal_pending(current))
        goto free_cma;
}
```

This would also bring parity with the HighMem path's `fatal_signal_pending()` check, which the non-HighMem path currently lacks (and has always lacked, even before this patch).

**Missing include**: Verify that `cma_heap.c` pulls in the `clear_pages()` declaration. It's defined in `<linux/mm.h>`, which is likely already included transitively, but it's worth confirming.

**Overall**: The patch is a reasonable micro-optimization. The main suggestion is to respect the `PROCESS_PAGES_NON_PREEMPT_BATCH` limit documented in the `clear_pages()` API, and optionally add the `fatal_signal_pending()` check that the HighMem path already has.

---
Generated by Claude Code Patch Reviewer

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

* [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers
@ 2026-03-10  8:53 Linus Walleij
  2026-03-10  8:53 ` [PATCH v2 1/2] dma-buf: heaps: Clear CMA pages with clear_pages() Linus Walleij
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Linus Walleij @ 2026-03-10  8:53 UTC (permalink / raw)
  To: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	T.J. Mercier, Christian König
  Cc: linux-media, dri-devel, linaro-mm-sig, Linus Walleij

Use clear_pages() and clear_highpage() properly in the
DMA heap allocator.

Signed-off-by: Linus Walleij <linusw@kernel.org>
---
Changes in v2:
- Added a second patch to use the clear_highpage() helper.
- Link to v1: https://lore.kernel.org/r/20260304-cma-heap-clear-pages-v1-1-6ff59da716d3@kernel.org

---
Linus Walleij (2):
      dma-buf: heaps: Clear CMA pages with clear_pages()
      dma-buf: heaps: Clear CMA highages using helper

 drivers/dma-buf/heaps/cma_heap.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
---
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20260303-cma-heap-clear-pages-540f3ac9f734

Best regards,
-- 
Linus Walleij <linusw@kernel.org>


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

* [PATCH v2 1/2] dma-buf: heaps: Clear CMA pages with clear_pages()
  2026-03-10  8:53 [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers Linus Walleij
@ 2026-03-10  8:53 ` Linus Walleij
  2026-03-10 15:29   ` T.J. Mercier
  2026-03-11  3:29   ` Claude review: " Claude Code Review Bot
  2026-03-10  8:53 ` [PATCH v2 2/2] dma-buf: heaps: Clear CMA highages using helper Linus Walleij
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 12+ messages in thread
From: Linus Walleij @ 2026-03-10  8:53 UTC (permalink / raw)
  To: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	T.J. Mercier, Christian König
  Cc: linux-media, dri-devel, linaro-mm-sig, Linus Walleij

As of commit 62a9f5a85b98
"mm: introduce clear_pages() and clear_user_pages()" we can
clear a range of pages with a potentially assembly-optimized
call.

Instead of using a memset, use this helper to clear the whole
range of pages from the CMA allocation.

Signed-off-by: Linus Walleij <linusw@kernel.org>
---
 drivers/dma-buf/heaps/cma_heap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c
index bd3370b9a3f6..f0bacf25ed9d 100644
--- a/drivers/dma-buf/heaps/cma_heap.c
+++ b/drivers/dma-buf/heaps/cma_heap.c
@@ -343,7 +343,7 @@ static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,
 			nr_clear_pages--;
 		}
 	} else {
-		memset(page_address(cma_pages), 0, size);
+		clear_pages(page_address(cma_pages), pagecount);
 	}
 
 	buffer->pages = kmalloc_objs(*buffer->pages, pagecount);

-- 
2.53.0


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

* [PATCH v2 2/2] dma-buf: heaps: Clear CMA highages using helper
  2026-03-10  8:53 [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers Linus Walleij
  2026-03-10  8:53 ` [PATCH v2 1/2] dma-buf: heaps: Clear CMA pages with clear_pages() Linus Walleij
@ 2026-03-10  8:53 ` Linus Walleij
  2026-03-10  8:55   ` Christian König
                     ` (2 more replies)
  2026-03-10 14:30 ` [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers Maxime Ripard
  2026-03-11  3:29 ` Claude review: " Claude Code Review Bot
  3 siblings, 3 replies; 12+ messages in thread
From: Linus Walleij @ 2026-03-10  8:53 UTC (permalink / raw)
  To: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	T.J. Mercier, Christian König
  Cc: linux-media, dri-devel, linaro-mm-sig, Linus Walleij

Currently the CMA allocator clears highmem pages using
kmap()->clear_page()->kunmap(), but there is a helper
static inline in <linux/highmem.h> that does the same for
us so use clear_highpage() instead of open coding this.

Suggested-by: T.J. Mercier <tjmercier@google.com>
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
 drivers/dma-buf/heaps/cma_heap.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c
index f0bacf25ed9d..92865786cfc9 100644
--- a/drivers/dma-buf/heaps/cma_heap.c
+++ b/drivers/dma-buf/heaps/cma_heap.c
@@ -329,10 +329,7 @@ static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,
 		struct page *page = cma_pages;
 
 		while (nr_clear_pages > 0) {
-			void *vaddr = kmap_local_page(page);
-
-			clear_page(vaddr);
-			kunmap_local(vaddr);
+			clear_highpage(page);
 			/*
 			 * Avoid wasting time zeroing memory if the process
 			 * has been killed by SIGKILL.

-- 
2.53.0


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

* Re: [PATCH v2 2/2] dma-buf: heaps: Clear CMA highages using helper
  2026-03-10  8:53 ` [PATCH v2 2/2] dma-buf: heaps: Clear CMA highages using helper Linus Walleij
@ 2026-03-10  8:55   ` Christian König
  2026-03-10 15:29   ` T.J. Mercier
  2026-03-11  3:29   ` Claude review: " Claude Code Review Bot
  2 siblings, 0 replies; 12+ messages in thread
From: Christian König @ 2026-03-10  8:55 UTC (permalink / raw)
  To: Linus Walleij, Sumit Semwal, Benjamin Gaignard, Brian Starkey,
	John Stultz, T.J. Mercier
  Cc: linux-media, dri-devel, linaro-mm-sig

On 3/10/26 09:53, Linus Walleij wrote:
> Currently the CMA allocator clears highmem pages using
> kmap()->clear_page()->kunmap(), but there is a helper
> static inline in <linux/highmem.h> that does the same for
> us so use clear_highpage() instead of open coding this.
> 
> Suggested-by: T.J. Mercier <tjmercier@google.com>
> Signed-off-by: Linus Walleij <linusw@kernel.org>

Ah yes, somebody pointed that out to me before but I never found time to write a patch to clean it up.

Reviewed-by: Christian König <christian.koenig@amd.com>

> ---
>  drivers/dma-buf/heaps/cma_heap.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c
> index f0bacf25ed9d..92865786cfc9 100644
> --- a/drivers/dma-buf/heaps/cma_heap.c
> +++ b/drivers/dma-buf/heaps/cma_heap.c
> @@ -329,10 +329,7 @@ static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,
>                 struct page *page = cma_pages;
> 
>                 while (nr_clear_pages > 0) {
> -                       void *vaddr = kmap_local_page(page);
> -
> -                       clear_page(vaddr);
> -                       kunmap_local(vaddr);
> +                       clear_highpage(page);
>                         /*
>                          * Avoid wasting time zeroing memory if the process
>                          * has been killed by SIGKILL.
> 
> --
> 2.53.0
> 


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

* Re: [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers
  2026-03-10  8:53 [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers Linus Walleij
  2026-03-10  8:53 ` [PATCH v2 1/2] dma-buf: heaps: Clear CMA pages with clear_pages() Linus Walleij
  2026-03-10  8:53 ` [PATCH v2 2/2] dma-buf: heaps: Clear CMA highages using helper Linus Walleij
@ 2026-03-10 14:30 ` Maxime Ripard
  2026-03-11  3:29 ` Claude review: " Claude Code Review Bot
  3 siblings, 0 replies; 12+ messages in thread
From: Maxime Ripard @ 2026-03-10 14:30 UTC (permalink / raw)
  To: Linus Walleij
  Cc: dri-devel, linaro-mm-sig, linux-media, Benjamin Gaignard,
	Brian Starkey, Christian König, John Stultz, Maxime Ripard,
	Sumit Semwal, T.J. Mercier

On Tue, 10 Mar 2026 09:53:10 +0100, Linus Walleij wrote:
> Use clear_pages() and clear_highpage() properly in the
> DMA heap allocator.
> 
> Signed-off-by: Linus Walleij <linusw@kernel.org>

Reviewed-by: Maxime Ripard <mripard@kernel.org>

Thanks!
Maxime

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

* Re: [PATCH v2 2/2] dma-buf: heaps: Clear CMA highages using helper
  2026-03-10  8:53 ` [PATCH v2 2/2] dma-buf: heaps: Clear CMA highages using helper Linus Walleij
  2026-03-10  8:55   ` Christian König
@ 2026-03-10 15:29   ` T.J. Mercier
  2026-03-11  3:29   ` Claude review: " Claude Code Review Bot
  2 siblings, 0 replies; 12+ messages in thread
From: T.J. Mercier @ 2026-03-10 15:29 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	Christian König, linux-media, dri-devel, linaro-mm-sig

On Tue, Mar 10, 2026 at 1:53 AM Linus Walleij <linusw@kernel.org> wrote:
>
> Currently the CMA allocator clears highmem pages using
> kmap()->clear_page()->kunmap(), but there is a helper
> static inline in <linux/highmem.h> that does the same for
> us so use clear_highpage() instead of open coding this.
>
> Suggested-by: T.J. Mercier <tjmercier@google.com>
> Signed-off-by: Linus Walleij <linusw@kernel.org>

Reviewed-by: T.J. Mercier <tjmercier@google.com>

Thanks!

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

* Re: [PATCH v2 1/2] dma-buf: heaps: Clear CMA pages with clear_pages()
  2026-03-10  8:53 ` [PATCH v2 1/2] dma-buf: heaps: Clear CMA pages with clear_pages() Linus Walleij
@ 2026-03-10 15:29   ` T.J. Mercier
  2026-03-11  3:29   ` Claude review: " Claude Code Review Bot
  1 sibling, 0 replies; 12+ messages in thread
From: T.J. Mercier @ 2026-03-10 15:29 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Sumit Semwal, Benjamin Gaignard, Brian Starkey, John Stultz,
	Christian König, linux-media, dri-devel, linaro-mm-sig

On Tue, Mar 10, 2026 at 1:53 AM Linus Walleij <linusw@kernel.org> wrote:
>
> As of commit 62a9f5a85b98
> "mm: introduce clear_pages() and clear_user_pages()" we can
> clear a range of pages with a potentially assembly-optimized
> call.
>
> Instead of using a memset, use this helper to clear the whole
> range of pages from the CMA allocation.
>
> Signed-off-by: Linus Walleij <linusw@kernel.org>

Reviewed-by: T.J. Mercier <tjmercier@google.com>

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

* Claude review: dma-buf: heaps: Use page clearing helpers
  2026-03-10  8:53 [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers Linus Walleij
                   ` (2 preceding siblings ...)
  2026-03-10 14:30 ` [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers Maxime Ripard
@ 2026-03-11  3:29 ` Claude Code Review Bot
  3 siblings, 0 replies; 12+ messages in thread
From: Claude Code Review Bot @ 2026-03-11  3:29 UTC (permalink / raw)
  To: dri-devel-reviews

Overall Series Review

Subject: dma-buf: heaps: Use page clearing helpers
Author: Linus Walleij <linusw@kernel.org>
Patches: 7
Reviewed: 2026-03-11T13:29:32.577102

---

This is a straightforward 2-patch cleanup series from Linus Walleij that replaces open-coded page clearing in the CMA heap allocator with the appropriate kernel helpers: `clear_pages()` for the non-highmem path and `clear_highpage()` for the highmem path. The changes are small, correct, and improve code readability by using standard APIs. No functional concerns.

---
Generated by Claude Code Patch Reviewer

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

* Claude review: dma-buf: heaps: Clear CMA pages with clear_pages()
  2026-03-10  8:53 ` [PATCH v2 1/2] dma-buf: heaps: Clear CMA pages with clear_pages() Linus Walleij
  2026-03-10 15:29   ` T.J. Mercier
@ 2026-03-11  3:29   ` Claude Code Review Bot
  1 sibling, 0 replies; 12+ messages in thread
From: Claude Code Review Bot @ 2026-03-11  3:29 UTC (permalink / raw)
  To: dri-devel-reviews

Patch Review

**Change:** Replaces `memset(page_address(cma_pages), 0, size)` with `clear_pages(page_address(cma_pages), pagecount)`.

This is a clean substitution. The `clear_pages()` helper (introduced by commit 62a9f5a85b98) can use architecture-optimized assembly for zeroing, which is a potential performance win over generic `memset`.

**One minor observation:** The original code used `size` (byte count) with `memset`, while `clear_pages()` takes a page count. This is correct — `pagecount` is already computed earlier in the function and represents the same range. However, there's a subtle difference: `size` is `PAGE_ALIGN(len)` which is the aligned allocation size, and `pagecount = size >> PAGE_SHIFT`. So `pagecount * PAGE_SIZE == size`, making this exactly equivalent. No issue here.

Looks good.

---
Generated by Claude Code Patch Reviewer

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

* Claude review: dma-buf: heaps: Clear CMA highages using helper
  2026-03-10  8:53 ` [PATCH v2 2/2] dma-buf: heaps: Clear CMA highages using helper Linus Walleij
  2026-03-10  8:55   ` Christian König
  2026-03-10 15:29   ` T.J. Mercier
@ 2026-03-11  3:29   ` Claude Code Review Bot
  2 siblings, 0 replies; 12+ messages in thread
From: Claude Code Review Bot @ 2026-03-11  3:29 UTC (permalink / raw)
  To: dri-devel-reviews

Patch Review

**Typo in subject:** "highages" should be "highpages". This appears in both the cover letter (`Clear CMA highages using helper`) and the patch subject. Worth fixing in the next revision.

**Change:** Replaces the open-coded `kmap_local_page()` / `clear_page()` / `kunmap_local()` sequence with a single `clear_highpage(page)` call. Looking at the existing code at `cma_heap.c:331-335`:

```c
void *vaddr = kmap_local_page(page);
clear_page(vaddr);
kunmap_local(vaddr);
```

This is exactly what `clear_highpage()` does, so the substitution is correct and simplifies the code nicely. The surrounding loop logic (fatal signal check, page increment, counter decrement) is properly preserved.

**Overall:** Both patches are simple, correct cleanups. The only actionable item is the "highages" typo in the patch 2 subject line — should be "highpages".

---
Generated by Claude Code Patch Reviewer

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

end of thread, other threads:[~2026-03-11  3:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-10  8:53 [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers Linus Walleij
2026-03-10  8:53 ` [PATCH v2 1/2] dma-buf: heaps: Clear CMA pages with clear_pages() Linus Walleij
2026-03-10 15:29   ` T.J. Mercier
2026-03-11  3:29   ` Claude review: " Claude Code Review Bot
2026-03-10  8:53 ` [PATCH v2 2/2] dma-buf: heaps: Clear CMA highages using helper Linus Walleij
2026-03-10  8:55   ` Christian König
2026-03-10 15:29   ` T.J. Mercier
2026-03-11  3:29   ` Claude review: " Claude Code Review Bot
2026-03-10 14:30 ` [PATCH v2 0/2] dma-buf: heaps: Use page clearing helpers Maxime Ripard
2026-03-11  3:29 ` Claude review: " Claude Code Review Bot
  -- strict thread matches above, loose matches on Subject: below --
2026-03-04  0:25 [PATCH] dma-buf: heaps: Clear CMA pages with clear_pages() Linus Walleij
2026-03-05  4:00 ` Claude review: " Claude Code Review Bot
2026-03-05  4:00 ` Claude Code Review Bot

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