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 5D8CE606A31A for ; Wed, 2 Dec 2026 00:27:44 +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=oiJszkDR Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1EF5310EAED; Wed, 2 Dec 2026 00:27:39 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7740A10EAED; Wed, 2 Dec 2026 00:27:33 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 2F215429EF; Wed, 2 Dec 2026 00:27:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF68B1F00896; Wed, 2 Dec 2026 00:27:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796171253; bh=YVSmjVMVombMMnwSQR0Ge+1pJ3dxKhJXQgLECneV96E=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=oiJszkDRiWFyL8yjJclamNTua0VDo30pNZdq0HP8Rz5t0k4qoDYGK5HZeUFztRHap DfFJ6mZQsEgeIvQKkjKPl21ndGr+2Z0tBlGfWspIdD32UUB69P+qxnqL7AU5FxdUR/ iS2sN4UgZlU0L99kFJUxuMmeRtJtQP7PeGAgGO0wTOzM5h05MlMu1P6Wzl5mIuptxS 6EkXW8GdbeYUoaltt7ec2J8HBYOZ7Mkv9Zfyxxv69UbVSRTAQ8HFyBxCSt7kAyopFg qUybkXsVufSvnNsjuN/oVKAy1VGXQN/95UNxYle8ZXSqjep2BYXWLgFDSDDdfH18nt sOs1gT2g3BXNA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 07/33] drm/ttm: Harvest beneficial-order pages on defragmentation moves To: "Matthew Brost" , intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org In-Reply-To: <20261201234915.3305331-8-matthew.brost@intel.com> References: <20261201234915.3305331-1-matthew.brost@intel.com> <20261201234915.3305331-8-matthew.brost@intel.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 00:27:32 +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=ham 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 2 potential issues to consider: - [High] drm/ttm: tail-page harvesting on unaligned chunks in __ttm_pool_alloc() - [Low] drm/ttm: missing kernel-doc member descriptions in struct ttm_pool_alloc_iter -- commit b5d32192ac0036b39c488e2913939a83996181d5 Author: Matthew Brost drm/ttm: Harvest beneficial-order pages on defragmentation moves This commit reuses already beneficial-order chunks from the old tt during defragmentation moves and harvests the remaining old pages if allocation falls back below the beneficial order after making partial progress. > diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_poo= l.c > index a117bd9089af6..111d83da5971b 100644 > --- a/drivers/gpu/drm/ttm/ttm_pool.c > +++ b/drivers/gpu/drm/ttm/ttm_pool.c > @@ -808,6 +808,149 @@ static unsigned int ttm_pool_alloc_find_order(unsig= ned int highest, [ ... ] > +static unsigned int ttm_pool_defrag_old_order(struct ttm_pool *pool, > + struct ttm_tt *old_tt, > + pgoff_t off, pgoff_t *cursor) > +{ > + struct page *p; > + > + while (*cursor < off) { > + p =3D old_tt->pages[*cursor]; > + if (!p || ttm_backup_page_ptr_is_handle(p)) > + return 0; > + *cursor +=3D 1UL << ttm_pool_page_order(pool, p); > + } > + > + if (*cursor !=3D off) > + return 0; > + > + p =3D old_tt->pages[off]; > + if (!p || ttm_backup_page_ptr_is_handle(p)) > + return 0; > + > + return ttm_pool_page_order(pool, p); > +} [ ... ] > +static int ttm_pool_harvest_remaining(struct ttm_pool *pool, > + struct ttm_tt *old_tt, pgoff_t off, > + struct ttm_pool_alloc_state *alloc) > +{ > + /* > + * @off is always an old-tt chunk head here, never a tail page, so > + * reading the per-chunk order below is safe. This holds because: > + * - Old chunks are self-aligned: greedy largest-power-of-two fill > + * places an order-k chunk at a 2^k-aligned offset, so any > + * beneficial-order-aligned offset is a chunk boundary. > + * - Fresh defrag allocations are capped at the beneficial order (see > + * ttm_pool_iter_acquire_page()), so they advance @off in beneficial > + * steps and never overshoot into the middle of a larger old chunk; > + * super-beneficial old chunks are always harvested whole by > + * ttm_pool_iter_reuse_old(). > + * - We are only reached once the defrag budget/prealloc is exhausted, > + * which in the capped/prealloc config leaves @off beneficial-aligned > + * (or, in the sub-beneficial tail, already walking old boundaries). > + */ > + while (alloc->remaining_pages) { > + struct page *p =3D old_tt->pages[off]; > + unsigned int order =3D ttm_backup_page_ptr_is_handle(p) ? 0 : > + ttm_pool_page_order(pool, p); > + pgoff_t nr =3D 1UL << order; > + int r; > + > + r =3D ttm_pool_harvest_page(old_tt, order, off, alloc); > + if (r) > + return r; > + > + off +=3D nr; > + } > + > + return 0; > +} [Severity: High] Can ttm_pool_harvest_remaining() and ttm_pool_iter_reuse_old() harvest tail subpages of a multi-order chunk in old_tt as order-0 pages when old_tt has unaligned chunks? If ttm_pool_restore_and_alloc() falls back below beneficial_order (setting TTM_TT_FLAG_BENEFICIAL_ORDER_FAILED, e.g. allocating order 8 at off =3D 0..= 255) and is interrupted (-EINTR in ttm_pool_restore_commit()), resuming it re-enters __ttm_pool_alloc() at off =3D 256 and resets it.order from ttm_pool_alloc_find_order(MAX_PAGE_ORDER, alloc) based only on alloc->remaining_pages, which can place an order-9 (512-page) chunk at unaligned offset 256..767. In addition, __ttm_pool_alloc() starts defrag allocations at MAX_PAGE_ORDER rather than capping at beneficial_order. When that tt is later defragmented and new_tt allocates a fresh order-9 chunk at off =3D 0..511, off =3D 512 lands on a tail page inside old_tt's chunk at 256..767: - In ttm_pool_harvest_remaining(), ttm_pool_page_order(pool, p) reads p->private =3D=3D 0 on the tail page (or dereferences NULL on dma_alloc pools) and harvests pages 512..767 as order-0 pages. - In a sub-beneficial tail where it->order < it->beneficial_order (e.g. a 768-page BO), ttm_pool_defrag_old_order() sees *cursor !=3D off and retur= ns 0, which ttm_pool_iter_reuse_old() treats as a valid order-0 chunk head. When the move commits, ttm_tt_defrag_disown_borrowed(old_tt, new_tt) strides by new_tt's chunk orders (0 -> 512) and clears old_tt->pages[512..7= 67] while leaving the order-9 head at old_tt->pages[256] intact. Unpopulating old_tt then frees pages 256..767 as an order-9 allocation while new_tt still uses pages 512..767 (use-after-free), and unpopulating new_tt later double frees those tail pages as order-0 pages. On the failure unwind path, ttm_tt_defrag_disown_borrowed(new_tt, old_tt) strides by old_tt's chunk orders (0 -> 256 -> 768), sees new_tt->pages[256] !=3D old_tt->pages[256], = and skips clearing new_tt->pages[512..767], freeing tail pages still owned by old_tt. > @@ -821,6 +964,7 @@ struct ttm_pool_alloc_iter { > struct ttm_tt *tt; > const struct ttm_operation_ctx *ctx; > struct ttm_pool_alloc_state *alloc; > + struct ttm_tt *defrag_old_tt; > unsigned int beneficial_order; > gfp_t gfp_flags; > =20 > @@ -829,9 +973,50 @@ struct ttm_pool_alloc_iter { > enum ttm_caching page_caching; > bool allow_pools; > bool fail_beneficial; > + unsigned int alloc_count; > + pgoff_t old_cursor; > struct page *p; > }; [Severity: Low] struct ttm_pool_alloc_iter has a kernel-doc comment block above it, so adding defrag_old_tt, alloc_count, and old_cursor without @member descriptions triggers scripts/kernel-doc warnings under W=3D1. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201234915.3305= 331-1-matthew.brost@intel.com?part=3D7