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 542C5606A32D for ; Fri, 4 Dec 2026 13:39:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=pixelcluster.dev header.i=@pixelcluster.dev header.a=rsa-sha256 header.s=ovhmo-selector-1 header.b=jZklcyvK Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 17B1D10E762; Fri, 4 Dec 2026 13:39:29 +0000 (UTC) Received: from smtpout5.mo539.mail-out.ovh.net (smtpout5.mo539.mail-out.ovh.net [51.210.91.44]) by gabe.freedesktop.org (Postfix) with ESMTPS id E971D10E762 for ; Fri, 4 Dec 2026 13:39:22 +0000 (UTC) Received: from director4.derp.mail-out.ovh.net (director4.derp.mail-out.ovh.net [79.137.60.37]) by mo539.mail-out.ovh.net (Postfix) with ESMTPS id 4jgszX4hD0z465f; Fri, 4 Dec 2026 13:39:20 +0000 (UTC) Received: from director4.derp.mail-out.ovh.net (director4.derp.mail-out.ovh.net. [127.0.0.1]) by director4.derp.mail-out.ovh.net (inspect_sender_mail_agent) with SMTP for ; Fri, 4 Dec 2026 13:39:20 +0000 (UTC) Received: from mta11.priv.ovhmail-u1.ea.mail.ovh.net (unknown [10.110.0.230]) by director4.derp.mail-out.ovh.net (Postfix) with ESMTPS id 4jgszX3xrtz1xx6; Fri, 4 Dec 2026 13:39:20 +0000 (UTC) Received: from pixelcluster.dev (unknown [10.1.6.3]) (Authenticated sender: nat@pixelcluster.dev) by mta11.priv.ovhmail-u1.ea.mail.ovh.net (Postfix) with ESMTPSA id CA33B9A1A8C; Fri, 4 Dec 2026 13:39:19 +0000 (UTC) Authentication-Results: garm.ovh; auth=pass (GARM-114S008b1899610-6431-4a39-9db1-d2e93cebde13, C72A8C269379148255D259EF506AD3C5C1B61040) smtp.auth=nat@pixelcluster.dev X-OVh-ClientIp: 141.95.41.139 From: Natalie Vock To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org, Matthew Brost Cc: Carlos Santa , Ryan Neph , Arun Pravin , Matthew Auld , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , linux-kernel@vger.kernel.org, Thomas =?UTF-8?B?SGVsbHN0csO2bQ==?= , Niranjana Vishwanathapura Subject: Re: [PATCH v6 04/35] drm/ttm: Introduce ttm_pool_alloc_iter for __ttm_pool_alloc() Date: Fri, 04 Dec 2026 14:39:19 +0100 Message-ID: In-Reply-To: <20261203202352.4153159-5-matthew.brost@intel.com> References: <20261203202352.4153159-1-matthew.brost@intel.com> <20261203202352.4153159-5-matthew.brost@intel.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" x-ovh-tracer-id: 14017453842198585660 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: dmFkZTE+5qt0Gx0CMjEYqE/iuwa5jDu9ByvMj6syKSS8L7TJF/QZAJFJQ/HEHBech8nutlfrJ975x0bvyAyIlmxNmHgzFv+hMtJblI43xsAGnIAckMB8tNBBo2mcWmK/WrHDaNtKzy7i9x6ZpCI4eQTiPLR0JJkjT49t+tUiRe848CC3l3jSAjaenvFjLgfEjMLzSxTZ6uzGL6qBdM9i8bYAE204Uhwuni+rKsN8N3A0e46VQuH4jicyQyyHwIRzVNCQ2yARH8aJNhAaJARy7fwXezl0e6F1jDeYq6hFKM2S3kxSDPeMsDOWEbSM6mDD1+QqQvvXCITEU3aa8//+Q17IokmxVWBnj1BM89idmHSWIBnepKX/9mLPqhafseco0ZznvVnfZVnc28EYcsQWkvx1kNwtWdQCZ77aSS+VBEGUzKffs6loxMghtvKxdcWrx7rxJiaAWe+1Iif2h4Td2EkfS3eRWmBPSJDgRcmUoTXNJaSGAM8X8h9+Od6jso4m4WKcsTDBOVx71IXpzIYbY7zZPdq8qcfodEJaEQvCLm2p805drTpYyE7Rdf+kK62q/dLFmcK1tFx8cE7sxWLLzA4gAcoRrZIb7wcrFifzOPbumBMJKHFKBB/byVuHJI1EURLAGxxACcL/rKuwY9sdRyTrX3WlCYc4fbk1VrFEgN4T3oCRCA DKIM-Signature: a=rsa-sha256; bh=Qmj3TZLXhNY4tah2DjgRWuKxS8Hr/qqdTiszS2kJH+k=; c=relaxed/relaxed; d=pixelcluster.dev; h=From; s=ovhmo-selector-1; t=1796391561; v=1; b=jZklcyvKA04VeXKDdWW4gtUbWUheo9uw0PgAoQ4Wev72hVX0v3ipoB0qAwLFufLGRPt8eSwo H0CKwSyT+VD4Bn13PKW707ZXNF04fUsOPEehtvQMQ/z3w+Yy6+L446iiTawrRO1EF7Zv9YEiDj2 TrPOgPPUCoFHfolB0Pq9f7b0sIKLyqjBOQqs0ULsyaZphgXYYU/52qbr7Evm2s2CR0kbgzcejYI IhomhUzIGX0G5GHfRKgUGt0cV1zdpsBYCZBSdZppvhPdCnsETCui/0GGzg+Am0ZrZ9j4BCXrnGg CZ+B9grl0rshwaYZ9eHQ0pN0O3Xk0FbMo3BapdbOYMpHQ== 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: , Sender: "Intel-xe" X-Spam-Status: No, score=-16.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,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 On Thursday, 3 December 2026 21:23 Matthew Brost wrote: > __ttm_pool_alloc() walks the page orders in a single loop whose body is > about to grow several defragmentation phases (reusing old pages, a move > budget, out-of-lock preallocation and fault injection). Left inline these > interleave into one deeply nested block of gotos and combinatorial > conditionals that is hard to follow and extend. >=20 > Factor the loop's working state into a struct ttm_pool_alloc_iter and > pull the per-order page acquisition and order-fallback into small phase > helpers, so the main loop reads as a short sequence of named steps. This > is a pure refactor with no functional change; subsequent patches extend > the iterator with their defrag phases rather than nesting more logic into > the loop. >=20 > v4: > - Drop restore field from iterator (Niranjana) > v5: > - Drop kernel style comment (Sashiko) >=20 > Cc: Carlos Santa > Cc: Ryan Neph > Cc: Natalie Vock > Cc: Arun Pravin > 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-kernel@vger.kernel.org > Cc: Thomas Hellstr=C3=B6m > Signed-off-by: Matthew Brost > Reviewed-by: Niranjana Vishwanathapura > --- > drivers/gpu/drm/ttm/ttm_pool.c | 157 ++++++++++++++++++++++----------- > 1 file changed, 104 insertions(+), 53 deletions(-) >=20 > diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_poo= l.c > index e06bd3cf6855..a053d78191f6 100644 > --- a/drivers/gpu/drm/ttm/ttm_pool.c > +++ b/drivers/gpu/drm/ttm/ttm_pool.c > @@ -807,18 +807,100 @@ static unsigned int ttm_pool_alloc_find_order(unsi= gned int highest, > return min_t(unsigned int, highest, __fls(alloc->remaining_pages)); > } > =20 > +/* > + * struct ttm_pool_alloc_iter - Working state for the __ttm_pool_alloc()= loop > + * > + * Bundles the immutable inputs and the mutable per-order state that the > + * allocation phase helpers share, so the main loop reads as a short seq= uence > + * of named phases rather than one deeply nested block. > + */ > +struct ttm_pool_alloc_iter { > + /* Immutable for the duration of the allocation. */ > + struct ttm_pool *pool; > + struct ttm_tt *tt; > + const struct ttm_operation_ctx *ctx; > + struct ttm_pool_alloc_state *alloc; > + unsigned int beneficial_order; > + gfp_t gfp_flags; > + > + /* Mutated as the loop walks the orders. */ > + unsigned int order; > + enum ttm_caching page_caching; > + bool allow_pools; > + struct page *p; Bit nitpicky, but I'd leave the struct page out of the iterator. Its value is temporary for an iteration, and each iteration overwrites the value with a new page. Then ttm_pool_iter_acquire_page returns the page directly, which we can use= to make the control flow a bit clearer. > +}; > + > +/* > + * Acquire a single page for the current order, leaving it in @it->p (NU= LL on > + * failure). Tries a same-order pool page, then a fresh system allocatio= n. > + */ > +static void ttm_pool_iter_acquire_page(struct ttm_pool_alloc_iter *it) static struct page *ttm_pool_iter_acquire_page(struct ttm_pool_alloc_iter *= it) > +{ > + struct ttm_pool_type *pt; struct page *p; > + > + /* First, try to allocate a page from a pool if one exists. */ > + it->p =3D NULL; > + pt =3D ttm_pool_select_type(it->pool, it->page_caching, it->order); > + if (!it->p && pt && it->allow_pools) > + it->p =3D ttm_pool_type_take(pt, ttm_pool_nid(it->pool)); if (pt && it->allow_pools) { p =3D ttm_pool_type_take(pt, ttm_pool_nid(it->pool)); if (p) return p; } > + > + /* > + * If that fails or previously failed, allocate from system. Note that > + * this also disallows additional pool allocations using write-back > + * cached pools of the same order. > + */ > + if (!it->p) { > + it->page_caching =3D ttm_cached; > + it->allow_pools =3D false; > + it->p =3D ttm_pool_alloc_page(it->pool, it->gfp_flags, it->order, > + it->ctx->beneficial_reclaim_backoff); > + } it->page_caching =3D ttm_cached; it->allow_pools =3D false; return ttm_pool_alloc_page(it->pool, it->gfp_flags, it->order, it->ctx->beneficial_reclaim_backoff); Natalie > +} > + > +/* > + * The current order could not be satisfied. Lower it and retry, recordi= ng a > + * sub-optimal backing (so the driver can later re-defrag) when we drop = below > + * the beneficial order. > + * > + * Returns 0 to retry the (lowered) order, or a negative errno. > + */ > +static int ttm_pool_iter_lower_order(struct ttm_pool_alloc_iter *it) > +{ > + if (!it->order) > + return -ENOMEM; > + > + /* > + * Failing to allocate at the device's beneficial order means we are > + * about to back this object with a sub-optimal (smaller order) set of > + * pages. Record it so the driver can later try to defragment the object > + * back to beneficial order. > + */ > + if (it->beneficial_order && it->order =3D=3D it->beneficial_order) > + it->tt->page_flags |=3D TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED; > + > + it->order--; > + it->page_caching =3D it->tt->caching; > + it->allow_pools =3D true; > + > + return 0; > +} > + > static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, > const struct ttm_operation_ctx *ctx, > struct ttm_pool_alloc_state *alloc, > struct ttm_pool_tt_restore *restore) > { > - const unsigned int beneficial_order =3D ttm_pool_beneficial_order(pool); > - enum ttm_caching page_caching; > + struct ttm_pool_alloc_iter it =3D { > + .pool =3D pool, > + .tt =3D tt, > + .ctx =3D ctx, > + .alloc =3D alloc, > + .beneficial_order =3D ttm_pool_beneficial_order(pool), > + .page_caching =3D tt->caching, > + .allow_pools =3D true, > + }; > gfp_t gfp_flags =3D GFP_USER; > pgoff_t caching_divide; > - unsigned int order; > - bool allow_pools; > - struct page *p; > int r; > =20 > WARN_ON(!alloc->remaining_pages || ttm_tt_is_populated(tt)); > @@ -835,59 +917,28 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, = struct ttm_tt *tt, > else > gfp_flags |=3D GFP_HIGHUSER; > =20 > - page_caching =3D tt->caching; > - allow_pools =3D true; > - for (order =3D ttm_pool_alloc_find_order(MAX_PAGE_ORDER, alloc); > - alloc->remaining_pages; > - order =3D ttm_pool_alloc_find_order(order, alloc)) { > - struct ttm_pool_type *pt; > - > - /* First, try to allocate a page from a pool if one exists. */ > - p =3D NULL; > - pt =3D ttm_pool_select_type(pool, page_caching, order); > - if (pt && allow_pools) > - p =3D ttm_pool_type_take(pt, ttm_pool_nid(pool)); > + it.gfp_flags =3D gfp_flags; > =20 > - /* > - * If that fails or previously failed, allocate from system. > - * Note that this also disallows additional pool allocations using > - * write-back cached pools of the same order. Consider removing > - * that behaviour. > - */ > - if (!p) { > - page_caching =3D ttm_cached; > - allow_pools =3D false; > - p =3D ttm_pool_alloc_page(pool, gfp_flags, order, > - ctx->beneficial_reclaim_backoff); > - } > - /* If that fails, lower the order if possible and retry. */ > - if (!p) { > - if (order) { > - /* > - * Failing to allocate at the device's beneficial > - * order means we are about to back this object > - * with a sub-optimal (smaller order) set of > - * pages. Record it so the driver can later try to > - * defragment the object back to beneficial order. > - */ > - if (beneficial_order && order =3D=3D beneficial_order) > - tt->page_flags |=3D > - TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED; > - --order; > - page_caching =3D tt->caching; > - allow_pools =3D true; > - continue; > - } > - r =3D -ENOMEM; > - goto error_free_all; > + for (it.order =3D ttm_pool_alloc_find_order(MAX_PAGE_ORDER, alloc); > + alloc->remaining_pages; > + it.order =3D ttm_pool_alloc_find_order(it.order, alloc)) { > + /* Acquire a page (pool / system) for this order. */ > + ttm_pool_iter_acquire_page(&it); > + if (!it.p) { > + r =3D ttm_pool_iter_lower_order(&it); > + if (r) > + goto error_free_all; > + continue; > } > - r =3D ttm_pool_page_allocated(pool, order, p, page_caching, alloc, > - restore); > + > + r =3D ttm_pool_page_allocated(pool, it.order, it.p, > + it.page_caching, alloc, restore); > if (r) > goto error_free_page; > =20 > if (ttm_pool_restore_valid(restore)) { > - r =3D ttm_pool_restore_commit(restore, tt->backup, ctx, alloc); > + r =3D ttm_pool_restore_commit(restore, tt->backup, ctx, > + alloc); > if (r) > goto error_free_all; > } > @@ -903,7 +954,7 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, st= ruct ttm_tt *tt, > return 0; > =20 > error_free_page: > - ttm_pool_free_page(pool, page_caching, order, p, false); > + ttm_pool_free_page(pool, it.page_caching, it.order, it.p, false); > =20 > error_free_all: > if (tt->restore) >=20