From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EFEAA41685F for ; Thu, 23 Jul 2026 16:54:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784825690; cv=none; b=Y8opNJG4d6owiQJLdmOznIPqx9pH2u2l/CCXz5I3OAz382iKI+t4jh6laqLxn5K822yfD61YYHZjmtsVdPp132/tNA2QGVrlBY0ecOnYOogQmeAcwRawcmNcNZ242aKT0b+L3vEqPB7xHOa3ou2L6YJ0/IVzV0mZ3U3/KiwNquI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784825690; c=relaxed/simple; bh=azjOaYpwHZQRTWkRcdw/e5ylfoMSG2GNHkBOrO6MM6w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=b3MgJf5vL8NS6+bcOz35nt6J5KIgM2CU5vPxoUdgcvYsHMSCQb1sB7BXeX3EOOOzs/i0DYLNCmYEMurGCob8a41H0n3ZJB7skfBgPqEyzyvYHVGPeSrWtVnJmtzlEdg7eIW7q6phPcjN3HxapcFtn3JFhEoNQnj6B/jSB52Hrw4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lx2F+H3i; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lx2F+H3i" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E8DDE1F00A3D; Thu, 23 Jul 2026 16:54:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784825688; bh=u9l1yGqdo5dZgKuUfjqs9Ti6h5CaGV5h74nhNBEYm1o=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lx2F+H3iz5akTHIhz8H/me5cF+oOJN/58wP3P04JMqXYpP+rOs2PbDebL1w7GAcsU Lnyp3v/xiFtc+Eltal+xSYnm/Em60ag6F/NNZhs+lxC9LQOJEjksowh9BVMii1rdNH it2faC/uinsVYTjlJ+YqCoXQMckQem4unFt6Gfhr5BK8rKNLxwgZwjGAUzlPKv9cmm IHYIUCLCNuEja5EoADLE+LNs8Vn0shpSPBeHppZVBxFS8Spm1fFFzw6EQHElCP9m0t M9g4SjXoU1madPWUommpGzt2RxKItOdzLMqw8piKZCYaFLJt/zeKdWkVOzHHwBvcsC of4i5gymsPKxA== From: Sasha Levin To: stable@vger.kernel.org Cc: Arunpravin Paneer Selvam , =?UTF-8?q?Christian=20K=C3=B6nig?= , Matthew Auld , =?UTF-8?q?Timur=20Krist=C3=B3f?= , John Olender , Sasha Levin Subject: [PATCH 6.12.y 2/2] gpu/buddy: bail out of try_harder when alignment cannot be honoured Date: Thu, 23 Jul 2026 12:54:43 -0400 Message-ID: <20260723165444.3392251-2-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260723165444.3392251-1-sashal@kernel.org> References: <2026072017-endless-bagging-0d2e@gregkh> <20260723165444.3392251-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Arunpravin Paneer Selvam [ Upstream commit 56bc6384314fb9ae98975fb2af8b143097ede3dc ] The try_harder contiguous fallback could return a range whose start offset did not match the caller's min_block_size. When a candidate's start is misaligned, realign it: free the misaligned run and reallocate exactly @size at the next lower min_block_size boundary. This keeps the returned size unchanged with no surplus to trim, and rejects the request only when no aligned candidate fits. v2: align misaligned candidates down to min_block_size instead of bailing out, for both the RHS and LHS paths (Matthew). Fixes: 0a1844bf0b53 ("drm/buddy: Improve contiguous memory allocation") Suggested-by: Christian König Cc: Matthew Auld Cc: Christian König Cc: Timur Kristóf Cc: stable@vger.kernel.org Reviewed-by: Matthew Auld Tested-by: John Olender Signed-off-by: Arunpravin Paneer Selvam Link: https://patch.msgid.link/20260709131050.1022759-1-Arunpravin.PaneerSelvam@amd.com Signed-off-by: Sasha Levin --- drivers/gpu/buddy.c | 63 +++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c index 603c59a2013a00..bf5920bd225803 100644 --- a/drivers/gpu/buddy.c +++ b/drivers/gpu/buddy.c @@ -901,22 +901,30 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm, blocks, total_allocated_on_err); } +static int __alloc_contig_aligned_retry(struct gpu_buddy *mm, + u64 unaligned_offset, + u64 size, + u64 min_block_size, + struct list_head *blocks) +{ + u64 aligned_offset = round_down(unaligned_offset, min_block_size); + + return __gpu_buddy_alloc_range(mm, aligned_offset, size, NULL, blocks); +} + static int __alloc_contig_try_harder(struct gpu_buddy *mm, u64 size, u64 min_block_size, struct list_head *blocks) { - u64 rhs_offset, lhs_offset, lhs_size, filled; + u64 rhs_offset, lhs_offset, filled; struct gpu_buddy_block *block; unsigned int tree, order; - LIST_HEAD(blocks_lhs); - unsigned long pages; u64 modify_size; int err; modify_size = rounddown_pow_of_two(size); - pages = modify_size >> ilog2(mm->chunk_size); - order = fls(pages) - 1; + order = ilog2(modify_size) - ilog2(mm->chunk_size); if (order == 0) return -ENOSPC; @@ -932,31 +940,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm, while (iter) { block = rbtree_get_free_block(iter); - /* Allocate blocks traversing RHS */ rhs_offset = gpu_buddy_block_offset(block); + + /* Allocate blocks traversing RHS */ err = __gpu_buddy_alloc_range(mm, rhs_offset, size, &filled, blocks); - if (!err || err != -ENOSPC) + if (err && err != -ENOSPC) return err; + if (!err && IS_ALIGNED(rhs_offset, min_block_size)) + return 0; + if (!err) { + /* Allocate the unaligned RHS offset using round_down */ + gpu_buddy_free_list_internal(mm, blocks); + err = __alloc_contig_aligned_retry(mm, rhs_offset, + size, + min_block_size, + blocks); + if (!err) + return 0; + if (err != -ENOSPC) { + gpu_buddy_free_list_internal(mm, blocks); + return err; + } + goto next; + } - lhs_size = max((size - filled), min_block_size); - if (!IS_ALIGNED(lhs_size, min_block_size)) - lhs_size = round_up(lhs_size, min_block_size); + if (size - filled > rhs_offset) + goto next; - /* Allocate blocks traversing LHS */ - lhs_offset = gpu_buddy_block_offset(block) - lhs_size; - err = __gpu_buddy_alloc_range(mm, lhs_offset, lhs_size, - NULL, &blocks_lhs); - if (!err) { - list_splice(&blocks_lhs, blocks); + lhs_offset = rhs_offset - (size - filled); + + /* Allocate the unaligned LHS offset using round_down */ + gpu_buddy_free_list_internal(mm, blocks); + err = __alloc_contig_aligned_retry(mm, lhs_offset, size, + min_block_size, blocks); + if (!err) return 0; - } else if (err != -ENOSPC) { + if (err != -ENOSPC) { gpu_buddy_free_list_internal(mm, blocks); return err; } - /* Free blocks for the next iteration */ +next: gpu_buddy_free_list_internal(mm, blocks); - iter = rb_prev(iter); } } -- 2.53.0