From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 451F9606A320 for ; Wed, 2 Dec 2026 00:23:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Vxik1oym Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7E0F210FE57; Wed, 2 Dec 2026 00:23:17 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6FDF510FE26; Wed, 2 Dec 2026 00:23:08 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id E7A7145363; Wed, 2 Dec 2026 00:23:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9616F1F0089B; Wed, 2 Dec 2026 00:23:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796170987; bh=IWlaht6Ruam1+wOYLCX2swkO7i5M7DjBsbIUxcrifYg=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=Vxik1oymolBUBvqG3qY08dXcQiIu+R76TKuVU9MhRuJqdtZjcVoCogQ3Jn67yQCsS PTgJ8TDt+NXVXXEwdUAkxDHnL5URtOI2nVV3wp3+MdY0cQj9D+c0vLs1PvO2YGsPQa UHQw/aWDsR/fJ4igLv/ebAUP7JYmzL+Obr2io6RPZl9qk1mZDQqOZT5c/ZzXwW4JDa UEmjVfknLkEkOlszkZAcoEGvla76PKh47fLQxhz0XReOpKyh6FCXrngUXH28VRyHv/ MXSPsfCtQ6yyh4TBMOn19cWrA9ov1r0bVBd5UKZnArci+RHaO0a+bX0MOp25YPGVOI 1GKECnG2FS0Bw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 09/33] drm/ttm: Preallocate beneficial-order defrag pages outside the lock To: "Matthew Brost" , intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org In-Reply-To: <20261201234915.3305331-10-matthew.brost@intel.com> References: <20261201234915.3305331-1-matthew.brost@intel.com> <20261201234915.3305331-10-matthew.brost@intel.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 00:23:07 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "dri-devel" X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] drm/ttm: lost nr_suboptimal_pages in restore->snapshot_alloc on restore retry - [Medium] drm/ttm: nr_suboptimal_pages counts unupgradable unaligned tail chunks - [Medium] drm/ttm: ttm_pool_iter_harvest_rest() succeeds with zero allocated pages - [Medium] drm/ttm: ttm_pool_prealloc_fill() and fini() bypass WC/UC page pools -- commit 9332f88fafed52e6cb7db307cefb6601ea48bda1 Author: Matthew Brost drm/ttm: Preallocate beneficial-order defrag pages outside the lock This commit adds out-of-lock preallocation of beneficial-order pages with bulk CPU caching updates for TTM defragmentation moves, and tracks the number of sub-optimal pages on each ttm_tt to size the preallocation. > diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_poo= l.c > index 9e95e368ff829..7dcc79458b5ee 100644 > --- a/drivers/gpu/drm/ttm/ttm_pool.c > +++ b/drivers/gpu/drm/ttm/ttm_pool.c [ ... ] > @@ -903,6 +909,9 @@ static int ttm_pool_harvest_page(struct ttm_tt *old_t= t, unsigned int order, > =20 > alloc->caching_divide =3D alloc->pages; > =20 > + if (order < beneficial_order) > + alloc->nr_suboptimal_pages +=3D 0x1 << order; > + > return 0; > } [Severity: Medium] Does counting every chunk with order < beneficial_order here (and in __ttm_pool_alloc() and ttm_pool_restore_and_alloc()) include the trailing unaligned tail of a BO whose size is not a multiple of 1 << beneficial_order? Chunks in the final partial region beyond round_down(tt->num_pages, 1UL << beneficial_order) can never be upgraded to beneficial_order because ttm_pool_alloc_find_order() caps the order to __fls(alloc->remaining_pages). Including those tail pages in tt->nr_suboptimal_pages causes callers that round ttm_tt_suboptimal_pages() up to beneficial-order chunks to preallocate, remap caching on, and then tear down one extra beneficial-order chunk on every defrag pass that __ttm_pool_alloc() can never consume. [ ... ] > @@ -961,6 +971,173 @@ static int ttm_pool_harvest_remaining(struct ttm_po= ol *pool, > return 0; > } > =20 [ ... ] > + while (pp->count < count) { > + struct page *p =3D ttm_pool_alloc_page(pool, gfp, order, false); > + > + if (!p) > + break; > + > + pp->pages[pp->count++] =3D p; > + if (cpages) > + ttm_pool_prealloc_stage_caching(cpages, &ncpages, p, > + order); > + } > + > + /* > + * Apply the requested caching to every collected page in one shot. On > + * failure the pages' caching is indeterminate, so drop the whole bag > + * (freeing restores write-back) and let the consumer allocate in-line. > + */ > + r =3D ttm_pool_prealloc_apply_caching(tt_caching, cpages, ncpages); > + kvfree(cpages); > + if (r) { > + unsigned int i; > + > + for (i =3D 0; i < pp->count; i++) > + ttm_pool_free_page(pool, tt_caching, order, > + pp->pages[i], false); > + pp->count =3D 0; > + } > + > + return 0; > +} > +EXPORT_SYMBOL(ttm_pool_prealloc_fill); > + > +/** > + * ttm_pool_prealloc_fini() - Release unconsumed preallocated pages > + * @pool: The pool the pages came from. > + * @pp: Prealloc bag to drain. Consumed pages (< @used) are now owned by= the tt. > + */ > +void ttm_pool_prealloc_fini(struct ttm_pool *pool, struct ttm_pool_preal= loc *pp) > +{ > + unsigned int i; > + > + for (i =3D pp->used; i < pp->count; ++i) > + ttm_pool_free_page(pool, pp->caching, pp->order, pp->pages[i], > + false); > + kvfree(pp->pages); > + pp->pages =3D NULL; > + pp->count =3D 0; > + pp->used =3D 0; > +} > +EXPORT_SYMBOL(ttm_pool_prealloc_fini); [Severity: Medium] Do ttm_pool_prealloc_fill() and ttm_pool_prealloc_fini() intentionally bypass the TTM page pools for write-combined and uncached allocations? Unlike ttm_pool_iter_acquire_page(), ttm_pool_prealloc_fill() allocates fresh write-back pages from the buddy allocator via ttm_pool_alloc_page() and runs set_pages_array_wc() or set_pages_array_uc() instead of first taking already-cached beneficial-order pages from ttm_pool_select_type() / ttm_pool_type_take(). In addition, when the prealloc bag is short or exhausted, ttm_pool_iter_defrag_exhausted() stops the loop before __ttm_pool_alloc() can take available beneficial-order pages from the pool. On teardown, ttm_pool_prealloc_fini() frees unconsumed chunks through ttm_pool_free_page(), which calls set_pages_wb() individually per chunk on x86 rather than returning them to the pool via ttm_pool_type_give(). [ ... ] > @@ -1006,16 +1184,20 @@ struct ttm_pool_alloc_iter { > }; > =20 > /* > - * Defrag move budget exhausted: the upgrade can make no further progres= s this > - * pass. Snapshot @defrag_capped is set only when a byte budget was in f= orce at > - * entry. > + * Defrag move budget exhausted, or the out-of-lock prealloc bag ran dry= : the > + * upgrade can make no further progress this pass. Snapshot @defrag_capp= ed is > + * set only when a byte budget was in force at entry. > */ > static bool ttm_pool_iter_defrag_exhausted(const struct ttm_pool_alloc_i= ter *it) > { > + const struct ttm_pool_prealloc *pp =3D it->prealloc; > + > if (!it->defrag_old_tt) > return false; > + if (it->defrag_capped && it->alloc->defrag_bytes_remaining <=3D 0) > + return true; > =20 > - return it->defrag_capped && it->alloc->defrag_bytes_remaining <=3D 0; > + return pp && pp->used >=3D pp->count; > } [Severity: Medium] If ctx->prealloc is passed with pp->count =3D=3D 0 (or is exhausted before = any new chunk is allocated, so it.alloc_count =3D=3D 0), does ttm_pool_iter_defrag_exhausted() cause __ttm_pool_alloc() to succeed after borrowing 100% of defrag_old_tt? When ttm_pool_iter_defrag_exhausted() returns true on the first iteration, __ttm_pool_alloc() calls ttm_pool_iter_harvest_rest() and returns 0 with it.alloc_count =3D=3D 0, unlike ttm_pool_iter_lower_order() which returns -ENOMEM when it->alloc_count =3D=3D 0. Committing a zero-progress defrag m= ove still unmaps userspace mappings, runs driver move/rebind callbacks, and re-flags TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED. This also contradicts the ttm_pool_prealloc_fill() and ctx->prealloc comments stating that the pool falls back to in-line allocation when a fill falls short. [ ... ] > @@ -1262,6 +1459,8 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, = struct ttm_tt *tt, > alloc->defrag_bytes_remaining -=3D > (s64)(1UL << it.order) << PAGE_SHIFT; > =20 > + if (it.order < it.beneficial_order) > + alloc->nr_suboptimal_pages +=3D 0x1 << it.order; > it.alloc_count++; > } > =20 > @@ -1326,6 +1525,9 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct tt= m_tt *tt, > /* Report the unused defrag budget back to the caller. */ > ctx->defrag_bytes_remaining =3D alloc.defrag_bytes_remaining; > =20 > + if (!ret) > + tt->nr_suboptimal_pages =3D alloc.nr_suboptimal_pages; > + > return ret; > } > EXPORT_SYMBOL(ttm_pool_alloc); > @@ -1378,6 +1580,15 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *po= ol, struct ttm_tt *tt, > =20 > if (ret) > return ret; > + > + /* > + * __ttm_pool_alloc() counts each freshly (re)allocated > + * chunk against nr_suboptimal_pages, but a chunk whose > + * backup copy was interrupted and is finished here > + * resumes past that loop, so account for it directly. > + */ > + if (restore->order < ttm_pool_beneficial_order(pool)) > + alloc.nr_suboptimal_pages +=3D 1U << restore->order; > } > if (!alloc.remaining_pages) { > ret =3D ttm_pool_apply_caching(&alloc); [Severity: Medium] Does updating alloc.nr_suboptimal_pages after ttm_pool_restore_commit() in __ttm_pool_alloc() and ttm_pool_restore_and_alloc() lose that count from restore->snapshot_alloc if a later step in the restore fails? ttm_pool_restore_commit() saves the post-commit state before returning: drivers/gpu/drm/ttm/ttm_pool.c:ttm_pool_restore_commit() { ... ttm_pool_allocated_page_commit(restore->alloced_page, restore->first_dma, alloc, nr); if (restore->page_caching =3D=3D alloc->tt_caching || PageHighMem(restore-= >alloced_page)) alloc->caching_divide =3D alloc->pages; restore->snapshot_alloc =3D *alloc; restore->alloced_pages +=3D nr; ... } Because both callers increment alloc.nr_suboptimal_pages only after ttm_pool_restore_commit() returns, restore->snapshot_alloc retains the old nr_suboptimal_pages value. If a subsequent chunk fails allocation (or is interrupted on its first page in ttm_pool_restore_commit()), retrying ttm_pool_restore_and_alloc() reloads alloc =3D restore->snapshot_alloc and drops the committed chunk's suboptimal page count, which can leave tt->nr_suboptimal_pages undercounted or 0 even when TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED is set. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201234915.3305= 331-1-matthew.brost@intel.com?part=3D9