From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from va-2-46.ptr.blmpb.com (va-2-46.ptr.blmpb.com [209.127.231.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45BE84252BF for ; Mon, 13 Jul 2026 13:25:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.231.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783949151; cv=none; b=HbUdoOmAwq4yEbCDELghesfb7xBpn5IFF2ccPadTq+sO9+UR8T2Lb10BBR3Vcb0KRoL1PQvdQHaU27A8S3LalftuS7Do35tXqK8ybhFIjssjCMpG5KQi0+geTEDBqyBfJlN/zNjNJ5w/j82foj80lkJyB50n0vHZTWd6mbS2Uio= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783949151; c=relaxed/simple; bh=bKV3qeHHUkmIKrLnkIPysgiaGlpuZCK3MjxDhWK7gb4=; h=To:Date:Content-Type:From:Subject:Mime-Version: Content-Disposition:Cc:Message-Id:In-Reply-To:References; b=hDozwOc3+u4ArtuV2cCW0gkEVpR89qmAgapz058uV3JiRq5Wz2ZL3oMNApbDpV1dA2+TM3LwI8vav9er1FP4N+ta6MwM+swtcCt1ejx4KtGKKf5JgRnaTDjNIkQAL2/81RwDHceJ3Aoe3OD+Vrmu8k0tp5nZT8fl8xH8r8IWTT4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=picoheart.com; spf=pass smtp.mailfrom=picoheart.com; dkim=pass (2048-bit key) header.d=picoheart.com header.i=@picoheart.com header.b=D1vjSIWx; arc=none smtp.client-ip=209.127.231.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=picoheart.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=picoheart.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=picoheart.com header.i=@picoheart.com header.b="D1vjSIWx" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=feishu2604151535; d=picoheart.com; t=1783949137; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=UnYFE64SWOzeEjXYMuc4HAEtOUyJzJDVfxKkUKJDhQs=; b=D1vjSIWxI4NlniQABcJhWdyIc5ghCAd7g5S/SQxWEmnkZBKGpfdkv2oMMI2YS7D3ON7lwN /535jmdqW7QUOScHI4YbrGB6No2wqFwPcEHADew7eMnXO3ANfRKwvVf6OGbT6hf7MSQxti bMsFocLL1QNa1IFQMC9dQWMrwcmAu4vCffh9Uiw2ncsqA5fJwd1/Vb3IosboGB6Oc/w8wY ueRNjFNU4vGTFZo7+VsTE6ZWukVWqPUNTPW4+dFGxkqRmenhq4to/FfX6p5qNKJBMpRIB1 J/WAzCyDE7LgecngFP1fexQHbgc5Ry4DdPNI4nfW3xdf99ghHgIOiyu4tLD0lg== To: "Arunpravin Paneer Selvam" Date: Mon, 13 Jul 2026 21:25:33 +0800 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Original-From: Zhaoyu Liu From: "Zhaoyu Liu" Subject: Re: [PATCH] gpu: buddy: avoid repeated builds of root dfs when alloc range Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Disposition: inline Received: from hostpc ([183.129.139.234]) by smtp.feishu.cn with ESMTPS; Mon, 13 Jul 2026 21:25:35 +0800 Cc: "matthew.auld" , "joelagnelf" , "airlied" , "simona" , , "dri-devel" , "linux-kernel" Message-Id: X-Lms-Return-Path: In-Reply-To: <4901dc21-c281-4e60-b2a2-061100a5c8a9@amd.com> References: <4901dc21-c281-4e60-b2a2-061100a5c8a9@amd.com> On Mon, Jul 13, 2026 at 01:32:22PM +0530, Arunpravin Paneer Selvam wrote: >=20 >=20 > On 7/13/2026 12:01 PM, =E5=88=98=E7=85=A7=E7=8E=89 wrote: > > [You don't often get email from liuzhaoyu.zackary@picoheart.com. Learn = why this is important at https://aka.ms/LearnAboutSenderIdentification ] > >=20 > > Please help review this patch. > > Thanks! > I was wondering whether this optimization provides a measurable benefit i= n > practice. Since mm->n_roots is typically quite small, the previous DFS li= st > construction only adds a few list_add_tail() operations per allocation. > On the other hand, the new approach introduces an additional helper call = and > branching during candidate selection. Have you done any profiling or > benchmarking to quantify the improvement? >=20 > Thanks, > Arun. Thank you for your review, Arun. To verify the gain, a KUnit perf case (gpu_test_buddy_range_alloc_perf) was added locally. It sweeps fixed 1MB bias ranges across the whole address space, allocating and freeing each range exactly, repeated 10000 iterations. Two scenarios cover the two extremes of n_roots: - 16MB mm -> n_roots =3D 1 (16 bias ranges) - 1GB-1 mm -> n_roots =3D 18 (1023 bias ranges) Each configuration was measured 3 times and averaged. Three builds were compared: - baseline : unmodified tree - no-inline : patched, __get_candidate_block is static - inline : patched, __get_candidate_block is static inline +----------+-----------------+------------------+------------------+ | mm_size | baseline (ns) | no-inline (ns) | inline (ns) | | | | (change vs base) | (change vs base) | +----------+-----------------+------------------+------------------+ | 16MB | 66877 | 66598 | 64663 | | | | -0.42% | -3.31% | +----------+-----------------+------------------+------------------+ | 1GB-1 | 9333559 | 9193052 | 9251951 | | | | -1.51% | -0.87% | +----------+-----------------+------------------+------------------+ And, I've sent new v2 patch with inline function, link: https://lore.kernel.org/all/alTlW8TmTXLocWKP@hostpc/ Please help review it again. Thanks, zackary > >=20 > > > From: "Zhaoyu Liu" > > > Date: Thu, Jul 9, 2026, 19:20 > > > Subject: [PATCH] gpu: buddy: avoid repeated builds of root dfs when = alloc range > > > To: , , , , > > > Cc: , > > > The buddy allocator previously pre-built a DFS list by inserting all > > > root blocks via list_add_tail() before every allocation call in > > > __alloc_range_bias() and __gpu_buddy_alloc_range(). This is wasteful > > > when the allocation can be satisfied by the first root or by sub-bloc= ks > > > produced from splits, as the remaining roots are never consumed. > > > Introduce __get_candidate_block() which unifies the block acquisition > > > logic for both __alloc_range_bias() and __alloc_range(): it first > > > checks the DFS list (populated by splits during allocation or > > > pre-filled by the caller for trim), and only when the list is exhaust= ed > > > does it lazily fetch the next root block through a cursor. > > > When the caller pre-fills the DFS list (trim scenario), the cursor is > > > initialized to mm->n_roots to disable root iteration entirely, > > > preserving the original behavior. > > > Signed-off-by: Zhaoyu Liu > > > --- > > > drivers/gpu/buddy.c | 61 ++++++++++++++++++++++++++++++------------= --- > > > 1 file changed, 41 insertions(+), 20 deletions(-) > > > diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c > > > index dc81fe0301ce..2cebb87020c7 100644 > > > --- a/drivers/gpu/buddy.c > > > +++ b/drivers/gpu/buddy.c > > > @@ -700,6 +700,41 @@ static void __gpu_buddy_undo_splits(struct gpu_b= uddy *mm, > > > } > > > } > > >=20 > > > +/** > > > + * __get_candidate_block - Get a candidate block for allocation. > > > + * @mm: GPU buddy manager > > > + * @dfs: List of candidate blocks. Populated in two ways: (1) pre-fi= lled by > > > + * the caller before allocation with blocks available for allo= cation, > > > + * and (2) extended during allocation by block splits, which a= dd the > > > + * resulting sub-blocks for subsequent finer-grained allocatio= n. > > > + * @cursor: Pointer to current root index, advanced when iterating r= oots. > > > + * Pass in a value >=3D mm->n_roots to disable root iterati= on entirely, > > > + * restricting block acquisition to @dfs only; this is used= in the > > > + * trim scenario where only pre-split sub-blocks should be = considered. > > > + * > > > + * Return: Pointer to the acquired block on success, ERR_PTR(-ENOSPC= ) when no > > > + * more blocks are available. > > > + */ > > > +static struct gpu_buddy_block* > > > +__get_candidate_block(struct gpu_buddy *mm, struct list_head *dfs, > > > + uint32_t *cursor) > > > +{ > > > + struct gpu_buddy_block *block; > > > + > > > + block =3D list_first_entry_or_null(dfs, > > > + struct gpu_buddy_block, > > > + tmp_link); > > > + if (block) { > > > + list_del(&block->tmp_link); > > > + return block; > > > + } > > > + > > > + if (*cursor >=3D mm->n_roots) > > > + return ERR_PTR(-ENOSPC); > > > + > > > + return mm->roots[(*cursor)++]; > > > +} > > > + > > > static struct gpu_buddy_block * > > > __alloc_range_bias(struct gpu_buddy *mm, > > > u64 start, u64 end, > > > @@ -711,25 +746,18 @@ __alloc_range_bias(struct gpu_buddy *mm, > > > struct gpu_buddy_block *block; > > > LIST_HEAD(dfs); > > > int err; > > > - int i; > > > + uint32_t cursor =3D 0; > > >=20 > > > end =3D end - 1; > > >=20 > > > - for (i =3D 0; i < mm->n_roots; ++i) > > > - list_add_tail(&mm->roots[i]->tmp_link, &dfs); > > > - > > > do { > > > u64 block_start; > > > u64 block_end; > > >=20 > > > - block =3D list_first_entry_or_null(&dfs, > > > - struct gpu_buddy_bl= ock, > > > - tmp_link); > > > - if (!block) > > > + block =3D __get_candidate_block(mm, &dfs, &cursor); > > > + if (IS_ERR(block)) > > > break; > > >=20 > > > - list_del(&block->tmp_link); > > > - > > > if (gpu_buddy_block_order(block) < order) > > > continue; > > >=20 > > > @@ -1023,6 +1051,7 @@ static int __alloc_range(struct gpu_buddy *mm, > > > LIST_HEAD(allocated); > > > u64 end; > > > int err; > > > + uint32_t cursor =3D list_empty(dfs) ? 0 : mm->n_roots; > > >=20 > > > end =3D start + size - 1; > > >=20 > > > @@ -1030,14 +1059,10 @@ static int __alloc_range(struct gpu_buddy *mm= , > > > u64 block_start; > > > u64 block_end; > > >=20 > > > - block =3D list_first_entry_or_null(dfs, > > > - struct gpu_buddy_bl= ock, > > > - tmp_link); > > > - if (!block) > > > + block =3D __get_candidate_block(mm, dfs, &cursor); > > > + if (IS_ERR(block)) > > > break; > > >=20 > > > - list_del(&block->tmp_link); > > > - > > > block_start =3D gpu_buddy_block_offset(block); > > > block_end =3D block_start + gpu_buddy_block_size(mm= , block) - 1; > > >=20 > > > @@ -1109,10 +1134,6 @@ static int __gpu_buddy_alloc_range(struct gpu_= buddy *mm, > > > struct list_head *blocks) > > > { > > > LIST_HEAD(dfs); > > > - int i; > > > - > > > - for (i =3D 0; i < mm->n_roots; ++i) > > > - list_add_tail(&mm->roots[i]->tmp_link, &dfs); > > >=20 > > > return __alloc_range(mm, &dfs, start, size, > > > blocks, total_allocated_on_err); > > > -- > > > 2.34.1 >