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 111D8606A320 for ; Wed, 2 Dec 2026 00:24:42 +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=NdtbK2nG Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C16EA10FE6F; Wed, 2 Dec 2026 00:24:36 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6DA3910FE6A; Wed, 2 Dec 2026 00:24:30 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 42F2B40FF0; Wed, 2 Dec 2026 00:24:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 067211F000C1; Wed, 2 Dec 2026 00:24:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796171070; bh=Q3BZY2gjqsrWuut8RWacdHgBS3O/6aiQR4HmZF6+/Ws=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=NdtbK2nGOm0oI32WhHr86ipPysjn/yYv5y+7F8r5LovY32Jat8NuV2NZ5U2rRDAbb Z3uqzWTY24fprpAIdrZd4wj0wNKY0rxeybh7HnIQraMou/fyT15+vd5i50TGU4GVZ5 OVQhnOSDEK/vo41Bq8X02JSXdIi68jfHD5RLEy8lHHeEhibdobQRExD411feBV6cvh hPw/KO3OJjYkPi1k9+MHcTxLk5Q5nOjF2DuqduQKQQqalkV0qNAG6d0LUbEI4ygoFH HkIRsM+YM6Skjcf7S3TqKmb1QHAvHWNaeIiWYw7R0k+yFytSdHREydBt36HEeZ4DcO UWIUdtnKBznpw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 21/33] drm/xe: Defrag using out-of-lock page preallocation To: "Matthew Brost" , intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org In-Reply-To: <20261201234915.3305331-22-matthew.brost@intel.com> References: <20261201234915.3305331-1-matthew.brost@intel.com> <20261201234915.3305331-22-matthew.brost@intel.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 00:24:29 +0000 X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "Intel-xe" 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 3 potential issues to consider: - [Medium] drm/xe: uninitialized prealloc bag used when want is 0 in xe_bo_defrag_one() - [Medium] drm/xe: TTM WC/UC page pool bypass in xe_bo_defrag_one() preallocation - [Low] drm/xe: xe_bo_defrag_one() over-preallocates 2MB chunk on unaligned or CCS tail -- commit 02f1b3d24f540fb63f990c585582303a91d27ddc Author: Matthew Brost drm/xe: Defrag using out-of-lock page preallocation This commit converts xe_bo_defrag_one() to preallocate beneficial-order pages and apply CPU cache mode changes outside the BO dma-resv lock before validating the buffer object. > diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c > index 9725c79af5f1f..3ea90cfb27e41 100644 > --- a/drivers/gpu/drm/xe/xe_bo.c > +++ b/drivers/gpu/drm/xe/xe_bo.c > @@ -1468,16 +1468,56 @@ static int xe_bo_defrag_one(struct xe_device *xe,= struct xe_bo *bo, > .defrag_bytes_remaining =3D budget, > }; > struct ttm_buffer_object *ttm_bo =3D &bo->ttm; > + struct ttm_pool *pool =3D &ttm_bo->bdev->pool; > + struct ttm_pool_prealloc pp =3D {}; > struct ttm_placement placement; > struct ttm_place place; > + enum ttm_caching tt_caching; > + unsigned int order, want =3D 0; > int ret =3D 0; > =20 > *consumed =3D 0; > *needs_more =3D false; > =20 > + /* > + * Phase 1: under the BO lock, re-check eligibility and estimate how > + * many beneficial-order chunks the move will (re)allocate, bounded by > + * the run's byte budget. Then drop the lock so the high-order, possibly > + * reclaim/compaction stalling allocations happen unlocked. > + */ > + xe_bo_lock(bo, false); > + if (!xe_bo_needs_defrag(bo)) { > + xe_bo_defrag_remove(bo); > + xe_bo_unlock(bo); > + goto out; > + } > + tt_caching =3D bo->ttm.ttm->caching; > + order =3D ttm_pool_prealloc_order(pool); > + if (order && ttm_bo->ttm) { > + u32 suboptimal =3D ttm_tt_suboptimal_pages(ttm_bo->ttm); > + u64 cap =3D min_t(u64, budget, xe_bo_size(bo)) >> PAGE_SHIFT; > + > + /* Prealloc only the beneficial-order chunks the move replaces. */ > + want =3D DIV_ROUND_UP_ULL(min_t(u64, suboptimal, cap), 1UL << order); [Severity: Low] Does DIV_ROUND_UP_ULL() over-preallocate an extra beneficial-order chunk when the BO has an unaligned trailing tail or FlatCCS extra_pages? __ttm_pool_alloc() counts every chunk with it.order < it.beneficial_order in nr_suboptimal_pages, including any trailing pages where the remaining size is smaller than 1 << order. During a defrag move, ttm_pool_iter_reuse_old() always harvests that sub-beneficial tail from defrag_old_tt rather than consuming a preallocated beneficial-order chunk: drivers/gpu/drm/ttm/ttm_pool.c:ttm_pool_iter_reuse_old() { ... if (harvest_order < it->beneficial_order && it->order >=3D it->beneficial_order) return TTM_POOL_ITER_FILL; ... } Rounding suboptimal up by 1 << order allocates and cache-converts an extra 2MB chunk that __ttm_pool_alloc() cannot consume and that ttm_pool_prealloc_fini() immediately restores to write-back and frees. > + } > + xe_bo_unlock(bo); > + > + /* Phase 2: preallocate outside the lock; */ > + if (want) { > + ret =3D ttm_pool_prealloc_fill(pool, tt_caching, &pp, want); > + if (ret || !pp.count) { > + ret =3D ret ?: -ENOMEM; > + xe_gt_stats_incr(xe_root_mmio_gt(xe), > + XE_GT_STATS_ID_DEFRAG_FAILED_COUNT, 1); > + goto out_err; > + } > + } [Severity: Medium] Does preallocating via ttm_pool_prealloc_fill() bypass the TTM write-combined and uncached page pools? Previously, ttm_bo_validate() ran with ctx.prealloc =3D=3D NULL, so ttm_pool_iter_acquire_page() drew pre-cached beneficial-order pages from ttm_pool_select_type() / ttm_pool_type_take() before falling back to ttm_pool_alloc_page(). Unlike ttm_pool_prealloc_fill_full() (which calls ttm_pool_take_page()), ttm_pool_prealloc_fill() calls ttm_pool_alloc_page() directly and then runs ttm_pool_prealloc_apply_caching(), while ttm_pool_prealloc_fini() frees unconsumed pages back to the buddy allocator via ttm_pool_free_page(). For write-combined or uncached BOs on x86, defragmentation now ignores idle beneficial-order pages already sitting in the TTM pool, forcing fresh order-9 buddy allocations and synchronous cross-CPU PAT/TLB shootdowns via set_pages_array_wc() or failing with -ENOMEM when buddy allocation fails. > + > + /* > + * Phase 3: re-take the lock, re-check, and validate using the prealloc. > + */ > xe_bo_lock(bo, false); > =20 > - /* Re-check eligibility under the BO lock. */ > if (!xe_bo_needs_defrag(bo)) { > xe_bo_defrag_remove(bo); > goto unlock; > @@ -1487,6 +1527,7 @@ static int xe_bo_defrag_one(struct xe_device *xe, s= truct xe_bo *bo, > place.flags |=3D TTM_PL_FLAG_TEMPORARY; > placement.num_placement =3D 1; > placement.placement =3D &place; > + ctx.prealloc =3D &pp; [Severity: Medium] If want is 0, ttm_pool_prealloc_fill() is skipped and pp remains zero-initialized with pp.caching =3D=3D ttm_uncached. Won't passing ctx.prealloc =3D &pp to ttm_bo_validate() then trigger WARN_ON(ctx->prealloc && ctx->prealloc->caching !=3D tt->caching) in ttm_pool_alloc() and fail with -EINVAL? A populated TT can have TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED set while ttm_tt_suboptimal_pages() is 0 after a swap-in retry in ttm_pool_restore_and_alloc(), because ttm_pool_restore_commit() snapshots restore->snapshot_alloc before __ttm_pool_alloc() increments alloc->nr_suboptimal_pages and retry does not clear TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED. When this happens on a ttm_cached or ttm_write_combined BO, ttm_bo_validate() fails with -EINVAL and leaves the BO at the head of xe->mem.defrag.list, blocking defragmentation of all BOs behind it and repeating the warning on every worker run. Setting ctx.prealloc =3D want ? &pp : NULL (or initializing pp.caching =3D tt_caching) avoids this. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201234915.3305= 331-1-matthew.brost@intel.com?part=3D21