Skip to contentSkip to conversation

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

PATCH In review StDenis, Tom 4 participants 7 messages Jun 14, 2019 – Jun 17, 2019 5 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amd/amdgpu: cast mem->num_pages to 64-bits when shifting

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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +5 -3
index c963ad86072e..31895d3c33de 100644
@@ -279,14 +279,16 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
 	uint64_t vis_usage = 0;
 	unsigned i;
 	int r;
+	uint64_t mem_bytes;
 
 	lpfn = place->lpfn;
 	if (!lpfn)
 		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 +310,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;
 	}
 
Signature · 6 lines
2.21.0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

ping?

Quoted 49 lines · Tom

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

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +3 -2
index c963ad86072e..31895d3c33de 100644
@@ -279,14 +279,16 @@ static int amdgpu_vram_mgr_new(struct
 ttm_mem_type_manager *man,
        uint64_t vis_usage = 0;
        unsigned i;
        int r;
+       uint64_t mem_bytes;
 
        lpfn = place->lpfn;
        if (!lpfn)
                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) {

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +2 -2
+               atomic64_sub(mem_bytes, &mgr->usage);
                mem->mm_node = NULL;
                return 0;
        }
@@ -308,7 +310,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;
        }
 
--

2.21.0

Quoted 4 lines

_______________________________________________
amd-gfx mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): CAAzXoRK9vy-qC36mSzJ3aJr6YB8kuWMujxfuNFAqAzbWEk+gTA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

Reviewed-by
#4 lore raw

Am 17.06.19 um 20:21 schrieb Tom St Denis:

Quoted 62 lines

ping?

On Fri, Jun 14, 2019 at 12:51 PM StDenis, Tom <[email protected] <mailto:[email protected]>> wrote:

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

    Signed-off-by: Tom St Denis <[email protected]
    <mailto:[email protected]>>
    ---
     drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 8 +++++---
     1 file changed, 5 insertions(+), 3 deletions(-)

    diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
    b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
    index c963ad86072e..31895d3c33de 100644
    --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
    +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
    @@ -279,14 +279,16 @@ static int amdgpu_vram_mgr_new(struct
    ttm_mem_type_manager *man,
            uint64_t vis_usage = 0;
            unsigned i;
            int r;
    +       uint64_t mem_bytes;

            lpfn = place->lpfn;
            if (!lpfn)
                    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 +310,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;
            }

    -- 
    2.21.0

    _______________________________________________
    amd-gfx mailing list
    [email protected] <mailto:[email protected]>
    https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Quoted 4 lines

_______________________________________________
amd-gfx mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#5 lore raw
In reply to a message not in the archive

Looks good to me. One cosmetic comment inline. With that fixed this patch is Reviewed-by:  Felix Kuehling <[email protected]>

Quoted 13 lines · Tom

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

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -0
index c963ad86072e..31895d3c33de 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man, · line 282

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1 -0
@@ -279,14 +279,16 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
  	uint64_t vis_usage = 0;
  	unsigned i;
  	int r;
+	uint64_t mem_bytes;

Christian likes the "upside-down christmas tree" to help readability of local variable declarations.

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +3 -2
  	lpfn = place->lpfn;
  	if (!lpfn)
  		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;
  	}

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man, · line 316

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1 -1
@@ -308,7 +310,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;
  	}
 

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1d91998e-e97c-229e-e7f6-2cc0228bf213-5C7GfCeVMHo@public.gmane.org

#7 lore raw

Am 17.06.19 um 21:15 schrieb Kuehling, Felix:

Quoted 12 lines

Looks good to me. One cosmetic comment inline. With that fixed this patch is Reviewed-by:  Felix Kuehling <[email protected]>

Quoted 8 lines · Tom

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

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 8 +++++---
   1 file changed, 5 insertions(+), 3 deletions(-)
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man, · line 282

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1 -0
index c963ad86072e..31895d3c33de 100644
@@ -279,14 +279,16 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
   	uint64_t vis_usage = 0;
   	unsigned i;
   	int r;
+	uint64_t mem_bytes;

Christian likes the "upside-down christmas tree" to help readability of local variable declarations.

Ah, yeah. Thanks for the note.

I actually don't care about that much, but I always get rejects from upstream subsystem maintainers if I don't do this.

It's most likely not even documented somewhere,
Christian.

Quoted 21 lines
Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +3 -3
   	lpfn = place->lpfn;
   	if (!lpfn)
   		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 +310,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);
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man, · line 316

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1 -0
+		atomic64_sub(mem_bytes, &mgr->usage);
   		return -ENOMEM;
   	}
 

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 2dcdc076-baee-d321-38d5-626b740eae59-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#9 lore raw
Quoted 27 lines · Christian König

Am 17.06.19 um 21:15 schrieb Kuehling, Felix:

Quoted 12 lines

Looks good to me. One cosmetic comment inline. With that fixed this patch is Reviewed-by:  Felix Kuehling <[email protected]>

Quoted 8 lines · Tom

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

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 8 +++++---

   1 file changed, 5 insertions(+), 3 deletions(-)
Quoted 13 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c static int amdgpu_vram_mgr_new(struct · line 283

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1 -0
index c963ad86072e..31895d3c33de 100644
@@ -279,14 +279,16 @@ static int amdgpu_vram_mgr_new(struct 
 ttm_mem_type_manager *man,
        uint64_t vis_usage = 0;
        unsigned i;
        int r;
+    uint64_t mem_bytes;

Christian likes the "upside-down christmas tree" to help readability of local variable declarations.

Quoted 6 lines

Ah, yeah. Thanks for the note.

I actually don't care about that much, but I always get rejects from upstream subsystem maintainers if I don't do this.

It's most likely not even documented somewhere,

Right. The fact that I even noticed it probably means that I have a mild case of OCD myself. If it was a strict requirement, it should be caught by checkpatch.pl.

Cheers,
  Felix

Quoted 33 lines

Christian.

Quoted 30 lines
Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -0
           lpfn = place->lpfn;
        if (!lpfn)
            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;
       }

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c static int amdgpu_vram_mgr_new(struct · line 316

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1 -1
@@ -308,7 +310,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;
        }

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 84969444-1d5a-d9ea-c4eb-96ad0c0fabca-5C7GfCeVMHo@public.gmane.org

#11 lore raw

Ok no worries I'll fix it up and push it later today.

Btw I didn't use an inline cast because the macro kept breaking. I tried variants of parentheses and nothing worked.... Odd...

Thanks,
Tom

On June 17, 2019 3:32:03 PM EDT, "Kuehling, Felix" <[email protected]> wrote:

On 2019-06-17 3:28 p.m., Christian König wrote:
Am 17.06.19 um 21:15 schrieb Kuehling, Felix:
Looks good to me. One cosmetic comment inline. With that fixed this patch is Reviewed-by: Felix Kuehling <[email protected]>

On 2019-06-14 12:51 p.m., StDenis, Tom wrote:
On 32-bit hosts mem->num_pages is 32-bits and can overflow when shifted. Add a cast to avoid this.

________________________________

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

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c index c963ad86072e..31895d3c33de 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c @@ -279,14 +279,16 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,

        uint64_t vis_usage = 0;
        unsigned i;
        int r;

+ uint64_t mem_bytes;
Christian likes the "upside-down christmas tree" to help readability of local variable declarations.

Ah, yeah. Thanks for the note.

I actually don't care about that much, but I always get rejects from upstream subsystem maintainers if I don't do this.

It's most likely not even documented somewhere,

Right. The fact that I even noticed it probably means that I have a mild case of OCD myself. If it was a strict requirement, it should be caught by checkpatch.pl.

Cheers,
Felix

Christian.

          lpfn = place->lpfn;
       if (!lpfn)
           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 +310,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;
       }

________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx