From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 299BD46F494 for ; Tue, 21 Jul 2026 17:47:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784656053; cv=none; b=sEmsNgN+LUzOJ/Kf+mUQ2OfQVk0MKjMSVbDIsnOx6ys59T/VMAhA9hFO7UiQTtnJyOdzgtxoQyp8Ap8AhGsrDTczunvccDekGqKojOF7g54dhQ5rymyArvlJxQIktC5ACoZJJWc9PFw4VMThsM83Dob2bkeyuMK3PtMzr9GRXpU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784656053; c=relaxed/simple; bh=nKJKT/P+qEgUMo/wDI71pfPxFpDWy8cquVcetFsF/9A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SFSXJEGHqlAw6BfxgZiDtq5D5/gS+oIkBqofRjYqmBtA650wBpPecNgrwNg5maiUq9KkoR0sHiezUfqyWEF88Nl7M9kCj+D2g8qGwOtS0Bx4XFFwdbGX+QXMzz2wImXai8aH+mUd1IAqWper2+genc5sFcyEi/iSflMKkeb0g6k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=UzLzB7By; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="UzLzB7By" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1784656052; x=1816192052; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nKJKT/P+qEgUMo/wDI71pfPxFpDWy8cquVcetFsF/9A=; b=UzLzB7BynR8KaqiAlKPPtVp6QbYw7ZRZ/qaLM4lkoZuNpgbPamX31J3+ T/1GSN9z4DM+dezJAPIshZgROl4m1NjiSLWrx2jWmJJgxmLukvnmz/LcT ZdqG+sFsJ44I6wAm5cZsMjUogQCN5yZnXWhnUIxMb4VOB+tjkpRWM0z9C CYB3eFueKN/LgnNnMzoDIO4jrLJO5N/hj14niNy0UUH9HOCz8YksYP0gS liQBPLiciKq5NFAehNdLGci2pLYVkOrX9rlM9Fkp5SDyjdkU1mfskgK2j BxfH+4G8oG545MthuIU0j+fj1dtJSiZOBKWDNFJ2bzFwK/R2TQJmmgF8g g==; X-CSE-ConnectionGUID: 1cCfsdlIRbSQ3vJBbojWJQ== X-CSE-MsgGUID: wIIXfB57RCqLtsanUgcd7w== X-IronPort-AV: E=McAfee;i="6800,10657,11853"; a="95920599" X-IronPort-AV: E=Sophos;i="6.25,177,1779174000"; d="scan'208";a="95920599" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Jul 2026 10:47:30 -0700 X-CSE-ConnectionGUID: aX85PhGqRZ2+OzCWKl4KjA== X-CSE-MsgGUID: C9cUg2WjRnO17Cple9Z0iw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,177,1779174000"; d="scan'208";a="254418016" Received: from gsse-cloud1.jf.intel.com ([10.54.39.91]) by fmviesa007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Jul 2026 10:47:29 -0700 From: Matthew Brost To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hugh Dickins , Baolin Wang , Andrew Morton , Christian Koenig , Huang Rui , Matthew Auld , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Christoph Hellwig Subject: [PATCH 2/2] drm/ttm: use shmem_backup_folio() for folio backup Date: Tue, 21 Jul 2026 10:47:23 -0700 Message-Id: <20260721174723.1039395-2-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260721174723.1039395-1-matthew.brost@intel.com> References: <20260721174723.1039395-1-matthew.brost@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Cc: Baolin Wang Cc: Andrew Morton Cc: Christian Koenig Cc: Huang Rui Cc: Matthew Auld Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Simona Vetter Cc: dri-devel@lists.freedesktop.org Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org Suggested-by: Christoph Hellwig Signed-off-by: Matthew Brost 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