]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
drm/buddy: Prevent BUG_ON by validating rounded allocation
authorSanjay Yadav <sanjay.kumar.yadav@intel.com>
Thu, 8 Jan 2026 11:32:29 +0000 (17:02 +0530)
committerSasha Levin <sashal@kernel.org>
Wed, 4 Mar 2026 12:21:10 +0000 (07:21 -0500)
[ Upstream commit 5488a29596cdba93a60a79398dc9b69d5bdadf92 ]

When DRM_BUDDY_CONTIGUOUS_ALLOCATION is set, the requested size is
rounded up to the next power-of-two via roundup_pow_of_two().
Similarly, for non-contiguous allocations with large min_block_size,
the size is aligned up via round_up(). Both operations can produce a
rounded size that exceeds mm->size, which later triggers
BUG_ON(order > mm->max_order).

Example scenarios:
- 9G CONTIGUOUS allocation on 10G VRAM memory:
  roundup_pow_of_two(9G) = 16G > 10G
- 9G allocation with 8G min_block_size on 10G VRAM memory:
  round_up(9G, 8G) = 16G > 10G

Fix this by checking the rounded size against mm->size. For
non-contiguous or range allocations where size > mm->size is invalid,
return -EINVAL immediately. For contiguous allocations without range
restrictions, allow the request to fall through to the existing
__alloc_contig_try_harder() fallback.

This ensures invalid user input returns an error or uses the fallback
path instead of hitting BUG_ON.

v2: (Matt A)
- Add Fixes, Cc stable, and Closes tags for context

Closes: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/6712
Fixes: 0a1844bf0b53 ("drm/buddy: Improve contiguous memory allocation")
Cc: <stable@vger.kernel.org> # v6.7+
Cc: Christian König <christian.koenig@amd.com>
Cc: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
Suggested-by: Matthew Auld <matthew.auld@intel.com>
Signed-off-by: Sanjay Yadav <sanjay.kumar.yadav@intel.com>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Reviewed-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
Link: https://patch.msgid.link/20260108113227.2101872-5-sanjay.kumar.yadav@intel.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/gpu/drm/drm_buddy.c

index 3f1a9892f2a392b68f0c7f39a3dffb890fbe7c88..640d93070bb7c44871602b8f7cf7dbb6885f6c70 100644 (file)
@@ -1155,6 +1155,15 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm,
        order = fls(pages) - 1;
        min_order = ilog2(min_block_size) - ilog2(mm->chunk_size);
 
+       if (order > mm->max_order || size > mm->size) {
+               if ((flags & DRM_BUDDY_CONTIGUOUS_ALLOCATION) &&
+                   !(flags & DRM_BUDDY_RANGE_ALLOCATION))
+                       return __alloc_contig_try_harder(mm, original_size,
+                                                        original_min_size, blocks);
+
+               return -EINVAL;
+       }
+
        do {
                order = min(order, (unsigned int)fls(pages) - 1);
                BUG_ON(order > mm->max_order);