From: Matthew Brost <matthew.brost@intel.com>
To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: Hugh Dickins <hughd@google.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Andrew Morton <akpm@linux-foundation.org>,
Christian Koenig <christian.koenig@amd.com>,
Huang Rui <ray.huang@amd.com>,
Matthew Auld <matthew.auld@intel.com>,
Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
Thomas Zimmermann <tzimmermann@suse.de>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
Christoph Hellwig <hch@lst.de>
Subject: [PATCH 2/2] drm/ttm: use shmem_backup_folio() for folio backup
Date: Tue, 21 Jul 2026 10:47:23 -0700 [thread overview]
Message-ID: <20260721174723.1039395-2-matthew.brost@intel.com> (raw)
In-Reply-To: <20260721174723.1039395-1-matthew.brost@intel.com>
Replace the open-coded shmem-backup loop in ttm_backup_backup_folio()
with a thin wrapper around the new shmem_backup_folio() helper. The
wrapper maps the helper's (int errno, *nr_pages_backed) contract onto
TTM's existing (s64 handle) return convention:
- 0 -> base handle
- -ENOMEM with *nr_pages_backed > 0 -> base handle (short backup;
partial progress kept)
- any other error -> that error verbatim
The mid-compound -ENOMEM fault-injection point that used to live
inside the per-subpage loop moves up to the wrapper and takes a new
shape: instead of synthesizing an error partway through the loop, the
wrapper passes order-1 to shmem_backup_folio() when
ttm_backup_fault_inject_folio() fires. shmem_backup_folio() then
backs up the first half of the compound normally, returns 0 with
*nr_pages_backed = (1 << (order - 1)), and TTM's caller sees the same
"short backup, nr_backed < (1 << order)" it would see on a real
mid-compound OOM and drives its reactive-split path unchanged.
Injection is skipped for order == 0 since there is nothing to shrink;
the previous "inject only on i > 0" guard mapped onto the same
observable subset of runs.
No functional change intended for non-injection paths.
Cc: Hugh Dickins <hughd@google.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Christian Koenig <christian.koenig@amd.com>
Cc: Huang Rui <ray.huang@amd.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: David Airlie <airlied@gmail.com>
Cc: Simona Vetter <simona@ffwll.ch>
Cc: dri-devel@lists.freedesktop.org
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
Assisted-by: GitHub-Copilot:claude-sonnet-5
---
The patch is based on drm-tip rather than the core MM branches to
facilitate Intel CI testing and initial review. It can be rebased onto
the core MM branches in a subsequent revision.
---
drivers/gpu/drm/ttm/ttm_backup.c | 91 ++++++++------------------------
1 file changed, 21 insertions(+), 70 deletions(-)
diff --git a/drivers/gpu/drm/ttm/ttm_backup.c b/drivers/gpu/drm/ttm/ttm_backup.c
index 3c067aadc52d..a6c4e6cb643b 100644
--- a/drivers/gpu/drm/ttm/ttm_backup.c
+++ b/drivers/gpu/drm/ttm/ttm_backup.c
@@ -105,76 +105,27 @@ ttm_backup_backup_folio(struct file *backup, struct folio *folio,
gfp_t folio_gfp, gfp_t alloc_gfp,
pgoff_t *nr_pages_backed)
{
- struct address_space *mapping = backup->f_mapping;
- int nr_pages = 1 << order;
- struct folio *to_folio;
- int ret, i;
-
- *nr_pages_backed = 0;
-
- for (i = 0; i < nr_pages; ) {
- int to_nr, j;
-
- /*
- * Only inject past the first subpage so *nr_pages_backed is
- * always > 0 here, matching a genuine mid-compound -ENOMEM
- * and driving the caller's reactive split fallback instead
- * of an early, no-progress failure.
- */
- if (IS_ENABLED(CONFIG_FAULT_INJECTION) && i &&
- ttm_backup_fault_inject_folio())
- to_folio = ERR_PTR(-ENOMEM);
- else
- to_folio = shmem_read_folio_gfp(mapping, idx + i, alloc_gfp);
- if (IS_ERR(to_folio)) {
- int err = PTR_ERR(to_folio);
-
- if (err == -ENOMEM && *nr_pages_backed)
- return ttm_backup_shmem_idx_to_handle(idx);
-
- if (*nr_pages_backed) {
- shmem_truncate_range(file_inode(backup),
- (loff_t)idx << PAGE_SHIFT,
- ((loff_t)(idx + i) << PAGE_SHIFT) - 1);
- /*
- * The pages just truncated are no longer
- * backed up; don't let the caller mistake
- * them for valid handles.
- */
- *nr_pages_backed = 0;
- }
- return err;
- }
-
- to_nr = min_t(int, nr_pages - i,
- folio_next_index(to_folio) - (idx + i));
-
- folio_mark_accessed(to_folio);
- folio_lock(to_folio);
- folio_mark_dirty(to_folio);
-
- for (j = 0; j < to_nr; j++)
- copy_highpage(folio_file_page(to_folio, idx + i + j),
- folio_page(folio, i + j));
-
- if (writeback && !folio_mapped(to_folio) &&
- folio_clear_dirty_for_io(to_folio)) {
- folio_set_reclaim(to_folio);
- ret = shmem_writeout(to_folio, NULL, NULL);
- if (!folio_test_writeback(to_folio))
- folio_clear_reclaim(to_folio);
- if (ret == AOP_WRITEPAGE_ACTIVATE)
- folio_unlock(to_folio);
- } else {
- folio_unlock(to_folio);
- }
-
- folio_put(to_folio);
- i += to_nr;
- *nr_pages_backed = i;
- }
-
- return ttm_backup_shmem_idx_to_handle(idx);
+ unsigned int backup_order = order;
+ int err;
+
+ /*
+ * Fault injection: back up only the first half of the folio to
+ * simulate a mid-compound OOM. The caller sees *nr_pages_backed
+ * < (1 << order) on success and drives its reactive-split path
+ * exactly as it would on a real short return. order == 0 cannot
+ * be shrunk further, so injection is skipped in that case.
+ */
+ if (IS_ENABLED(CONFIG_FAULT_INJECTION) && order &&
+ ttm_backup_fault_inject_folio())
+ backup_order = order - 1;
+
+ err = shmem_backup_folio(folio, backup, idx, alloc_gfp, backup_order,
+ nr_pages_backed, writeback);
+
+ if (!err || (err == -ENOMEM && *nr_pages_backed))
+ return ttm_backup_shmem_idx_to_handle(idx);
+
+ return err;
}
/**
--
2.34.1
next prev parent reply other threads:[~2026-07-21 17:47 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-21 17:47 [PATCH 1/2] mm/shmem: add shmem_backup_folio() helper Matthew Brost
2026-07-21 17:47 ` Matthew Brost [this message]
2026-07-21 17:54 ` ✓ CI.KUnit: success for series starting with [1/2] " Patchwork
2026-07-21 18:43 ` ✓ Xe.CI.BAT: " Patchwork
2026-07-22 5:11 ` ✓ Xe.CI.FULL: " Patchwork
2026-07-22 9:43 ` [PATCH 1/2] " Christoph Hellwig
2026-07-22 20:11 ` Matthew Brost
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=20260721174723.1039395-2-matthew.brost@intel.com \
--to=matthew.brost@intel.com \
--cc=airlied@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=hch@lst.de \
--cc=hughd@google.com \
--cc=intel-xe@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=matthew.auld@intel.com \
--cc=mripard@kernel.org \
--cc=ray.huang@amd.com \
--cc=simona@ffwll.ch \
--cc=tzimmermann@suse.de \
/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