public inbox for drm-ai-reviews@public-inbox.freedesktop.org
 help / color / mirror / Atom feed
From: Claude Code Review Bot <claude-review@example.com>
To: dri-devel-reviews@example.com
Subject: Claude review: drm/ttm: Use ttm_backup_insert_folio() for zero-copy swapout
Date: Sat, 16 May 2026 13:43:40 +1000	[thread overview]
Message-ID: <review-patch2-20260512110339.6244-3-thomas.hellstrom@linux.intel.com> (raw)
In-Reply-To: <20260512110339.6244-3-thomas.hellstrom@linux.intel.com>

Patch Review

**drivers/gpu/drm/ttm/ttm_backup.c — `ttm_backup_insert_folio()`:**

The new function is a clean wrapper:

```c
s64
ttm_backup_insert_folio(struct file *backup, struct folio *folio,
			unsigned int order, bool writeback, pgoff_t idx,
			gfp_t folio_gfp)
{
	int ret;

	WARN_ON_ONCE(folio_get_private(folio));
	ret = shmem_insert_folio(backup, folio, order, idx, writeback, folio_gfp);
	if (ret)
		return ret;

	return ttm_backup_shmem_idx_to_handle(idx);
}
```

The `WARN_ON_ONCE(folio_get_private(folio))` is good — it catches the case where TTM forgot to clear `page->private` (which stores the pool order) before insertion.

Note that the old `ttm_backup_backup_page()` is completely removed and replaced. The old function is no longer called anywhere. Good — no dead code left behind.

**drivers/gpu/drm/ttm/ttm_pool.c — `ttm_pool_backup()` changes:**

1. **Loop structure change:**

   The loop changes from `for (i = 0; i < num_pages; ++i)` to `for (i = 0; i < num_pages; i += npages)` with `npages` computed from the page order. This is the core of the optimization — instead of splitting every compound page before backup, it attempts to insert the entire compound folio.

2. **New handle check in the initial shrink pass:**

   ```c
   if (unlikely(!page ||
                ttm_backup_page_ptr_is_handle(page))) {
       num_pages = 1;
       continue;
   }
   ```

   The old code only checked `!page`. The addition of `ttm_backup_page_ptr_is_handle(page)` correctly handles re-entry after a partial backup — pages that were already backed up have their `tt->pages[i]` slot replaced with a handle pointer.

3. **Fallback-to-split on insertion failure:**

   ```c
   if (unlikely(handle < 0)) {
       if (order) {
           page->private = order;
           ttm_pool_split_for_swap(pool, page);
           npages = 0;
           continue;
       }
       ret = (int)handle;
       break;
   }
   ```

   When high-order insertion fails, the code restores `page->private = order` (which was cleared to 0 before the insertion attempt) and calls `ttm_pool_split_for_swap()`. Setting `npages = 0` causes `i` not to advance, so the loop retries the same index — but now the page is order-0 after splitting. This is a clever retry mechanism.

   **However**, there's a subtlety: `shmem_insert_folio()` may have called `prep_compound_page()` to promote the non-compound high-order allocation to compound, and then `undo_compound_page()` on failure. After undo, the pages are back to non-compound state. Then `ttm_pool_split_for_swap()` calls `split_page(p, order)`. `split_page()` handles non-compound high-order pages (adjusting refcounts), so this is technically correct, but it means the pages have been through a promote→undo→split cycle where `page[1]` and other tail pages' internal fields (lru, etc.) were modified by the compound promotion and not fully restored by undo. Since the pages are isolated and split_page only cares about refcounts, this works in practice. It would be worth a comment explaining this lifecycle.

4. **NR_GPU_ACTIVE accounting:**

   ```c
   mod_node_page_state(page_pgdat(page), NR_GPU_ACTIVE, -(1 << order));
   folio_put(page_folio(page));
   ```

   The old code used `__free_pages_gpu_account(page, 0, false)` (which doesn't exist in the current drm-next tree, confirming these patches are against a different base). The new code correctly decrements `NR_GPU_ACTIVE` by the full folio size and drops the caller's reference. Since `shmem_insert_folio()` transferred ownership to shmem (which took its own reference via `shmem_add_to_page_cache()`), the `folio_put()` drops the last non-shmem reference.

5. **Handle array population:**

   ```c
   for (j = 0; j < npages; j++)
       tt->pages[i + j] = ttm_backup_handle_to_page_ptr(handle + j);
   ```

   This fills in handles for all sub-pages of the compound folio. The comment in `ttm_backup_insert_folio()`'s docstring says "Handles for sub-pages of a compound folio follow sequentially: handle + j addresses sub-page j." This relies on the shmem page cache index being the handle base, with sequential offsets for each page within the folio. This is consistent with `ttm_backup_shmem_idx_to_handle()`.

6. **Fault injection guard:**

   ```c
   if (unlikely(i + npages > num_pages))
       break;
   ```

   Good defensive check — if `CONFIG_FAULT_INJECTION` truncated `num_pages` mid-compound-page, the code breaks out rather than trying to partially insert a folio.

**include/drm/ttm/ttm_backup.h:**

Clean interface update, just renaming `ttm_backup_backup_page` to `ttm_backup_insert_folio` with the new folio-based signature. The `alloc_gfp` parameter is correctly dropped since shmem pages are no longer allocated.

**Summary of issues requiring attention:**
- The missing `folio_clear_dirty_for_io()` / `folio_set_reclaim()` before `shmem_writeout()` in patch 1 is the most significant concern and should be addressed or explicitly justified.
- `undo_compound_page()` scope and documentation could be tightened.
- The silent EEXIST truncation in `shmem_insert_folio()` should at least emit a debug/warning message.

---
Generated by Claude Code Patch Reviewer

  reply	other threads:[~2026-05-16  3:43 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-12 11:03 [PATCH 0/2] Insert instead of copy pages into shmem when shrinking Thomas Hellström
2026-05-12 11:03 ` [PATCH 1/2] mm/shmem: add shmem_insert_folio() Thomas Hellström
2026-05-12 11:07   ` David Hildenbrand (Arm)
2026-05-12 11:31     ` Thomas Hellström
2026-05-12 20:03       ` David Hildenbrand (Arm)
2026-05-13  7:47         ` Christian König
2026-05-13  8:31           ` Thomas Hellström
2026-05-13  9:30             ` David Hildenbrand (Arm)
2026-05-13  8:37           ` David Hildenbrand (Arm)
2026-05-13  8:51             ` Thomas Hellström
2026-05-13 10:03               ` David Hildenbrand (Arm)
2026-05-13 10:37                 ` Thomas Hellström
2026-05-13 11:36                   ` David Hildenbrand (Arm)
2026-05-13 14:53                     ` Thomas Hellström
2026-05-13 19:35                       ` David Hildenbrand (Arm)
2026-05-14 10:40                         ` Thomas Hellström
2026-05-13 11:54             ` Christian König
2026-05-13 19:43               ` David Hildenbrand (Arm)
2026-05-16  3:43   ` Claude review: " Claude Code Review Bot
2026-05-12 11:03 ` [PATCH 2/2] drm/ttm: Use ttm_backup_insert_folio() for zero-copy swapout Thomas Hellström
2026-05-16  3:43   ` Claude Code Review Bot [this message]
2026-05-16  3:43 ` Claude review: Insert instead of copy pages into shmem when shrinking Claude Code Review Bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=review-patch2-20260512110339.6244-3-thomas.hellstrom@linux.intel.com \
    --to=claude-review@example.com \
    --cc=dri-devel-reviews@example.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox