drm/amd/amdgpu: cast mem->num_pages to 64-bits when shifting (v2) [Linux 5.3]

This Linux kernel change "drm/amd/amdgpu: cast mem->num_pages to 64-bits when shifting (v2)" is included in the Linux 5.3 release. This change is authored by Tom St Denis <tom.stdenis [at] amd.com> on Fri Jun 14 11:47:21 2019 -0400. The commit for this change in Linux stable tree is c1d827d (patch).

drm/amd/amdgpu: cast mem->num_pages to 64-bits when shifting (v2)

On 32-bit hosts mem->num_pages is 32-bits and can overflow
when shifted.  Add a cast to avoid this.

(v2): Style fix.

Signed-off-by: Tom St Denis <tom.stdenis@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

There are 9 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
index c963ad8..1150e34 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
@@ -276,7 +276,7 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
    struct drm_mm_node *nodes;
    enum drm_mm_insert_mode mode;
    unsigned long lpfn, num_nodes, pages_per_node, pages_left;
-   uint64_t vis_usage = 0;
+   uint64_t vis_usage = 0, mem_bytes;
    unsigned i;
    int r;

@@ -285,8 +285,9 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
        lpfn = man->size;

    /* bail out quickly if there's likely not enough VRAM for this BO */
-   if (atomic64_add_return(mem->num_pages << PAGE_SHIFT, &mgr->usage) > adev->gmc.mc_vram_size) {
-       atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
+   mem_bytes = (u64)mem->num_pages << PAGE_SHIFT;
+   if (atomic64_add_return(mem_bytes, &mgr->usage) > adev->gmc.mc_vram_size) {
+       atomic64_sub(mem_bytes, &mgr->usage);
        mem->mm_node = NULL;
        return 0;
    }
@@ -308,7 +309,7 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
    nodes = kvmalloc_array((uint32_t)num_nodes, sizeof(*nodes),
                   GFP_KERNEL | __GFP_ZERO);
    if (!nodes) {
-       atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
+       atomic64_sub(mem_bytes, &mgr->usage);
        return -ENOMEM;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *