From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sphereful.davidgow.net (sphereful.davidgow.net [203.29.242.92]) (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 19A793749F4 for ; Sun, 13 Sep 2026 08:49:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=203.29.242.92 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789289384; cv=none; b=P5fkJuHSDxE4i0w/e+zP2oVb/QFB3zd01HU7PZXdRIKI4FCT3u99Lo7a6Rm6Yh0trBAsPUNz7pnalwG5gIIhMQ/N7NxUKBaiBhgxpv1JoNaP37SC2pNsbdxYtRF8sygk7t4i8JvnDQFhqB19R9+0JoG03qt8tTCMGNDb0cwlZq4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789289384; c=relaxed/simple; bh=K0GBY6e7lZorWrH6FFJzqCfhKE9ipjRdiC9O9SCravs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=lSR4fHrGHA7DVovJkEvE0AXhjlzGoYcLHvkxePLcM5mSiPll+2XJSqwfhpDTig15ZfjsEH97r10zBbxufSydnpELv/8lGMgAKKVvgVuQm4rJQ4yhmhElTNxKVw5ECNCVxT9j9s/pMFdIdpD5Sg6zv0AIjYnujtpj8KvNzWWfFVE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=davidgow.net; spf=pass smtp.mailfrom=davidgow.net; arc=none smtp.client-ip=203.29.242.92 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=davidgow.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=davidgow.net Received: by sphereful.davidgow.net (Postfix, from userid 119) id 742F51EAB35; Sun, 13 Sep 2026 16:49:40 +0800 (AWST) X-Spam-Level: Received: from shelley.lan (unknown [IPv6:2001:8003:8802:7000::41b]) by sphereful.davidgow.net (Postfix) with ESMTPSA id 5F6281EA8D4; Sun, 13 Sep 2026 16:49:38 +0800 (AWST) From: David Gow To: Jim Cromie , "Maciej W . Rozycki" , Andrew Morton , Matthew Auld , Arun Pravin , Joel Fernandes , David Airlie , Simona Vetter Cc: David Gow , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] drm_buddy: fix power-of-2 rounding errs Date: Sun, 13 Sep 2026 16:49:06 +0800 Message-ID: <20260913084907.520877-2-david@davidgow.net> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The standard roundup_pow_of_two() and rounddown_pow_of_two() macros use unsigned long internally, which on 32-bit architectures (like arm32) is a 32-bit type. drm_test_buddy_alloc_exceeds_max_order() uses these on a u64 value, where they silently truncate the 10GB allocation, giving unexpected success in DRM-CI. (see below the snip). Fix this by replacing the those macros with safe 64-bit power-of-two calculations using ilog2(). Fixes: 0a1844bf0b532 ("drm/buddy: Improve contiguous memory allocation") Signed-off-by: Jim Cromie Signed-off-by: David Gow --- This is a straightforward rebase of patch 39 of v12 of this series: https://lore.kernel.org/all/20260326185413.1205870-40-jim.cromie@gmail.com/ The only changes are updating the buddy path (as it's no longer a part of DRM), and adding the Fixes tag. This is a fairly hacky solution: I've got a nicer series which adds clearer helpers and generally cleans up all of these macros on 32-bit systems, but it's much larger (and growing as more things need cleaning up), so I'd rather have this more urgent fix go in immediately, and save the cleanup for a less-urgent follow-up series: https://lore.kernel.org/all/20260830103321.2042968-1-david@davidgow.net/ As noted in the original series, this is causing KUnit test failures on all 32-bit architectures: [09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: EXPECTATION FAILED at drivers/gpu/tests/gpu_buddy_test.c:1429 [09:01:26] Expected err == -22, but [09:01:26] err == 0 (0x0) [09:01:26] WARNING: drivers/gpu/buddy.c:508 at gpu_buddy_fini+0x244/0x2e0, CPU#0: kunit_try_catch/1595 [09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: drivers/gpu/buddy.c:508: gpu_buddy_assert(gpu_buddy_block_is_free(mm->roots[i])) [09:01:26] WARNING: drivers/gpu/buddy.c:516 at gpu_buddy_fini+0x284/0x2e0, CPU#0: kunit_try_catch/1595 [09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: drivers/gpu/buddy.c:508: gpu_buddy_assert(gpu_buddy_block_is_free(mm->roots[i])) [09:01:26] WARNING: drivers/gpu/buddy.c:516 at gpu_buddy_fini+0x284/0x2e0, CPU#0: kunit_try_catch/1595 [09:01:26] # gpu_test_buddy_alloc_exceeds_max_order: drivers/gpu/buddy.c:519: gpu_buddy_assert(!mm->used_scoreboard[i]) [09:01:26] [FAILED] gpu_test_buddy_alloc_exceeds_max_order Cheers, -- David --- drivers/gpu/buddy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c index 26e7a48b55f3..964a5ae78f1b 100644 --- a/drivers/gpu/buddy.c +++ b/drivers/gpu/buddy.c @@ -1188,7 +1188,7 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm, u64 modify_size; int err; - modify_size = rounddown_pow_of_two(size); + modify_size = 1ULL << ilog2(size); order = ilog2(modify_size) - ilog2(mm->chunk_size); if (order == 0) return -ENOSPC; @@ -1435,7 +1435,7 @@ int gpu_buddy_alloc_blocks(struct gpu_buddy *mm, /* Roundup the size to power of 2 */ if (flags & GPU_BUDDY_CONTIGUOUS_ALLOCATION) { - size = roundup_pow_of_two(size); + size = 1ULL << (ilog2(size - 1) + 1); min_block_size = size; /* * Normalize the requested size to min_block_size for regular allocations. -- 2.55.0