Skip to contentSkip to conversation

amdgpu: Use uint32_t i in amdgpu_find_bo_by_cpu_mapping

PATCH In review Michel Dänzer 3 participants 8 messages Aug 14, 2018 – Aug 17, 2018 4 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 amdgpu: Use uint32_t i in amdgpu_find_bo_by_cpu_mapping

From: Michel Dänzer <[email protected]>

The compiler points out that an int doesn't work as intended if dev->bo_handles.max_key > INT_MAX:

../../amdgpu/amdgpu_bo.c: In function ‘amdgpu_find_bo_by_cpu_mapping’:
../../amdgpu/amdgpu_bo.c:550:16: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint32_t’ {aka ‘unsigned int’} [-Wsign-compare]

  for (i = 0; i < dev->bo_handles.max_key; i++) {
                ^
../../amdgpu/amdgpu_bo.c:558:8: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint32_t’ {aka ‘unsigned int’} [-Wsign-compare]
  if (i < dev->bo_handles.max_key) {
        ^
Fixes 4d454424e1f2 ("amdgpu: add a function to find bo by cpu mapping
                     (v2)")
---
 amdgpu/amdgpu_bo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
amdgpu/amdgpu_bo.c +1 -1
index b790e9be..86d1c143 100644
@@ -535,7 +535,7 @@ int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
 				  amdgpu_bo_handle *buf_handle,
 				  uint64_t *offset_in_bo)
 {
-	int i;
+	uint32_t i;
 	struct amdgpu_bo *bo;
 
 	if (cpu == NULL || size == 0)
Signature · 6 lines
2.18.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): 20180814095848.10981-1-michel-otUistvHUpPR7s880joybQ@public.gmane.org

#2 lore raw

PATCH 2/2 amdgpu: Use char* pointers in amdgpu_find_bo_by_cpu_mapping

From: Michel Dänzer <[email protected]>

Arithmetic using void* pointers isn't defined by the C standard, only as a GCC extension. Avoids compiler warnings:

../../amdgpu/amdgpu_bo.c: In function ‘amdgpu_find_bo_by_cpu_mapping’:
../../amdgpu/amdgpu_bo.c:554:48: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))

                                                ^
../../amdgpu/amdgpu_bo.c:561:23: warning: pointer of type ‘void *’ used in subtraction [-Wpointer-arith]
   *offset_in_bo = cpu - bo->cpu_ptr;
                       ^
Fixes 4d454424e1f2 ("amdgpu: add a function to find bo by cpu mapping
                     (v2)")
---
 amdgpu/amdgpu.h    | 2 +-
 amdgpu/amdgpu_bo.c | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
amdgpu/amdgpu.h +1 -1
index a8c353c6..f2bdeb95 100644
@@ -695,7 +695,7 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
  *
 */
 int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
-				  void *cpu,
+				  char *cpu,
 				  uint64_t size,
 				  amdgpu_bo_handle *buf_handle,
 				  uint64_t *offset_in_bo);
amdgpu/amdgpu_bo.c +4 -3
index 86d1c143..ef75f1d0 100644
@@ -530,7 +530,7 @@ int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo,
 }
 
 int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
-				  void *cpu,
+				  char *cpu,
 				  uint64_t size,
 				  amdgpu_bo_handle *buf_handle,
 				  uint64_t *offset_in_bo)
@@ -551,14 +551,15 @@ int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
 		bo = handle_table_lookup(&dev->bo_handles, i);
 		if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
 			continue;
-		if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))
+		if (cpu >= (char*)bo->cpu_ptr &&
+		    cpu < ((char*)bo->cpu_ptr + bo->alloc_size))
 			break;
 	}
 
 	if (i < dev->bo_handles.max_key) {
 		atomic_inc(&bo->refcount);
 		*buf_handle = bo;
-		*offset_in_bo = cpu - bo->cpu_ptr;
+		*offset_in_bo = cpu - (char*)bo->cpu_ptr;
 	} else {
 		*buf_handle = NULL;
 		*offset_in_bo = 0;
Signature · 6 lines
2.18.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): 20180814095848.10981-2-michel-otUistvHUpPR7s880joybQ@public.gmane.org

#4 lore raw

Re: [PATCH libdrm 2/2] amdgpu: Use char* pointers in amdgpu_find_bo_by_cpu_mapping

Quoted 26 lines · Michel Dänzer

From: Michel Dänzer <[email protected]>

Arithmetic using void* pointers isn't defined by the C standard, only as a GCC extension. Avoids compiler warnings:

../../amdgpu/amdgpu_bo.c: In function ‘amdgpu_find_bo_by_cpu_mapping’:
../../amdgpu/amdgpu_bo.c:554:48: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]

    if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))
                                                 ^
../../amdgpu/amdgpu_bo.c:561:23: warning: pointer of type ‘void *’ used in subtraction [-Wpointer-arith]
    *offset_in_bo = cpu - bo->cpu_ptr;
                        ^
Fixes 4d454424e1f2 ("amdgpu: add a function to find bo by cpu mapping
                      (v2)")
---
  amdgpu/amdgpu.h    | 2 +-
  amdgpu/amdgpu_bo.c | 7 ++++---
  2 files changed, 5 insertions(+), 4 deletions(-)
amdgpu/amdgpu.h +0 -0
index a8c353c6..f2bdeb95 100644

amdgpu/amdgpu.h int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev, · line 698

Quoted 5 lines
amdgpu/amdgpu.h +1 -1
@@ -695,7 +695,7 @@ int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
   *
  */
  int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
-				  void *cpu,
+				  char *cpu,

Shall we cast the cpu pointer when do arithmetic and keep the arguments as other cpu ponter?

e.g.
@@ -565,14 +565,15 @@ int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,

                bo = handle_table_lookup(&dev->bo_handles, i);
                if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
                        continue;
-               if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))
+               if (cpu >= bo->cpu_ptr &&
+                  (uint64_t)cpu < ((uint64_t)bo->cpu_ptr + bo->alloc_size))
                        break;
        }

        if (i < dev->bo_handles.max_key) {
                atomic_inc(&bo->refcount);
                *buf_handle = bo;
-               *offset_in_bo = cpu - bo->cpu_ptr;
+               *offset_in_bo = (uint64_t)cpu - (uint64_t)bo->cpu_ptr;
        } else {
                *buf_handle = NULL;
                *offset_in_bo = 0;

Regards,
Jerry

Quoted 25 lines
amdgpu/amdgpu.h +0 -0
  				  uint64_t size,
  				  amdgpu_bo_handle *buf_handle,
  				  uint64_t *offset_in_bo);
amdgpu/amdgpu_bo.c +3 -2
index 86d1c143..ef75f1d0 100644
@@ -530,7 +530,7 @@ int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo,
  }
 
  int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
-				  void *cpu,
+				  char *cpu,
  				  uint64_t size,
  				  amdgpu_bo_handle *buf_handle,
  				  uint64_t *offset_in_bo)
@@ -551,14 +551,15 @@ int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
  		bo = handle_table_lookup(&dev->bo_handles, i);
  		if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
  			continue;
-		if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))
+		if (cpu >= (char*)bo->cpu_ptr &&
+		    cpu < ((char*)bo->cpu_ptr + bo->alloc_size))
  			break;

amdgpu/amdgpu_bo.c int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev, · line 565

Quoted 10 lines
amdgpu/amdgpu_bo.c +1 -1
  	}
 
  	if (i < dev->bo_handles.max_key) {
  		atomic_inc(&bo->refcount);
  		*buf_handle = bo;
-		*offset_in_bo = cpu - bo->cpu_ptr;
+		*offset_in_bo = cpu - (char*)bo->cpu_ptr;
  	} else {
  		*buf_handle = NULL;
  		*offset_in_bo = 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]

#6 lore raw

PATCH v2 1/1 amdgpu: Eliminate void* arithmetic in amdgpu_find_bo_by_cpu_mapping

From: Michel Dänzer <[email protected]>

Arithmetic using void* pointers isn't defined by the C standard, only as a GCC extension. Avoids compiler warnings:

../../amdgpu/amdgpu_bo.c: In function ‘amdgpu_find_bo_by_cpu_mapping’:
../../amdgpu/amdgpu_bo.c:554:48: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))

                                                ^
../../amdgpu/amdgpu_bo.c:561:23: warning: pointer of type ‘void *’ used in subtraction [-Wpointer-arith]
   *offset_in_bo = cpu - bo->cpu_ptr;
                       ^

v2: Use uintptr_t instead of char*, don't change function signature
(Junwei Zhang)

Fixes 4d454424e1f2 ("amdgpu: add a function to find bo by cpu mapping
                     (v2)")
---
 amdgpu/amdgpu_bo.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
amdgpu/amdgpu_bo.c +3 -2
index 86d1c143..8efd014e 100644
@@ -551,14 +551,15 @@ int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
 		bo = handle_table_lookup(&dev->bo_handles, i);
 		if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
 			continue;
-		if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))
+		if (cpu >= bo->cpu_ptr &&
+		    cpu < (void*)((uintptr_t)bo->cpu_ptr + bo->alloc_size))
 			break;
 	}
 
 	if (i < dev->bo_handles.max_key) {
 		atomic_inc(&bo->refcount);
 		*buf_handle = bo;
-		*offset_in_bo = cpu - bo->cpu_ptr;
+		*offset_in_bo = (uintptr_t)cpu - (uintptr_t)bo->cpu_ptr;
 	} else {
 		*buf_handle = NULL;
 		*offset_in_bo = 0;
Signature · 6 lines
2.18.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): 20180816135429.3945-1-michel-otUistvHUpPR7s880joybQ@public.gmane.org

Reviewed-by
#8 lore raw

Re: [PATCH v2 libdrm] amdgpu: Eliminate void* arithmetic in amdgpu_find_bo_by_cpu_mapping

Am 16.08.2018 um 15:54 schrieb Michel Dänzer:

Quoted 13 lines

From: Michel Dänzer <[email protected]>

Arithmetic using void* pointers isn't defined by the C standard, only as a GCC extension. Avoids compiler warnings:

../../amdgpu/amdgpu_bo.c: In function ‘amdgpu_find_bo_by_cpu_mapping’:
../../amdgpu/amdgpu_bo.c:554:48: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]

    if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))
                                                 ^
../../amdgpu/amdgpu_bo.c:561:23: warning: pointer of type ‘void *’ used in subtraction [-Wpointer-arith]
    *offset_in_bo = cpu - bo->cpu_ptr;
                        ^
Quoted 6 lines

v2: Use uintptr_t instead of char*, don't change function signature
(Junwei Zhang)

Fixes 4d454424e1f2 ("amdgpu: add a function to find bo by cpu mapping
                      (v2)")
Quoted 16 lines
---
  amdgpu/amdgpu_bo.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
amdgpu/amdgpu_bo.c +2 -1
index 86d1c143..8efd014e 100644
@@ -551,14 +551,15 @@ int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
  		bo = handle_table_lookup(&dev->bo_handles, i);
  		if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
  			continue;
-		if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))
+		if (cpu >= bo->cpu_ptr &&
+		    cpu < (void*)((uintptr_t)bo->cpu_ptr + bo->alloc_size))
  			break;

amdgpu/amdgpu_bo.c int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev, · line 565

Quoted 10 lines
amdgpu/amdgpu_bo.c +1 -1
  	}
 
  	if (i < dev->bo_handles.max_key) {
  		atomic_inc(&bo->refcount);
  		*buf_handle = bo;
-		*offset_in_bo = cpu - bo->cpu_ptr;
+		*offset_in_bo = (uintptr_t)cpu - (uintptr_t)bo->cpu_ptr;
  	} else {
  		*buf_handle = NULL;
  		*offset_in_bo = 0;

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

Reviewed-by Junwei Zhang
#9 lore raw

Re: [PATCH v2 libdrm] amdgpu: Eliminate void* arithmetic in amdgpu_find_bo_by_cpu_mapping

In reply to a message not in the archive

Regards,
Jerry(Junwei Zhang)

________________________________________
From: amd-gfx <[email protected]> on behalf of Michel Dänzer <[email protected]>
Sent: Thursday, August 16, 2018 9:54:29 PM
To: [email protected]
Subject: [PATCH v2 libdrm] amdgpu: Eliminate void* arithmetic in amdgpu_find_bo_by_cpu_mapping

From: Michel Dänzer <[email protected]>

Arithmetic using void* pointers isn't defined by the C standard, only as a GCC extension. Avoids compiler warnings:

../../amdgpu/amdgpu_bo.c: In function ‘amdgpu_find_bo_by_cpu_mapping’:
../../amdgpu/amdgpu_bo.c:554:48: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))

                                                ^
../../amdgpu/amdgpu_bo.c:561:23: warning: pointer of type ‘void *’ used in subtraction [-Wpointer-arith]
   *offset_in_bo = cpu - bo->cpu_ptr;
                       ^

v2: Use uintptr_t instead of char*, don't change function signature
(Junwei Zhang)

Fixes 4d454424e1f2 ("amdgpu: add a function to find bo by cpu mapping
                     (v2)")
---
 amdgpu/amdgpu_bo.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
amdgpu/amdgpu_bo.c +3 -2
index 86d1c143..8efd014e 100644
@@ -551,14 +551,15 @@ int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
                bo = handle_table_lookup(&dev->bo_handles, i);
                if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
                        continue;
-               if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size))
+               if (cpu >= bo->cpu_ptr &&
+                   cpu < (void*)((uintptr_t)bo->cpu_ptr + bo->alloc_size))
                        break;
        }
 
        if (i < dev->bo_handles.max_key) {
                atomic_inc(&bo->refcount);
                *buf_handle = bo;
-               *offset_in_bo = cpu - bo->cpu_ptr;
+               *offset_in_bo = (uintptr_t)cpu - (uintptr_t)bo->cpu_ptr;
        } else {
                *buf_handle = NULL;
                *offset_in_bo = 0;

--
2.18.0

_______________________________________________
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

#10 lore raw

Re: [PATCH libdrm 2/2] amdgpu: Use char* pointers in amdgpu_find_bo_by_cpu_mapping

In reply to a message not in the archive
Quoted 58 lines · Jerry (Junwei)
Quoted 28 lines · Michel Dänzer

From: Michel Dänzer <[email protected]>

Arithmetic using void* pointers isn't defined by the C standard, only as a GCC extension. Avoids compiler warnings:

../../amdgpu/amdgpu_bo.c: In function ‘amdgpu_find_bo_by_cpu_mapping’:
../../amdgpu/amdgpu_bo.c:554:48: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
    if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr + bo->alloc_size)) ^ ../../amdgpu/amdgpu_bo.c:561:23: warning: pointer of type ‘void *’ used in subtraction [-Wpointer-arith]
    *offset_in_bo = cpu - bo->cpu_ptr;
                        ^

Fixes 4d454424e1f2 ("amdgpu: add a function to find bo by cpu mapping

                      (v2)")

---
  amdgpu/amdgpu.h    | 2 +-
  amdgpu/amdgpu_bo.c | 7 ++++---

  2 files changed, 5 insertions(+), 4 deletions(-)
amdgpu/amdgpu.h +0 -0
index a8c353c6..f2bdeb95 100644

amdgpu/amdgpu.h int · line 699

Quoted 6 lines
amdgpu/amdgpu.h +1 -1
@@ -695,7 +695,7 @@ int
 amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
    *
   */
   int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
-                  void *cpu,
+                  char *cpu,

Shall we cast the cpu pointer when do arithmetic and keep the arguments as other cpu ponter?

e.g.

amdgpu/amdgpu_bo.c +3 -2
@@ -565,14 +565,15 @@ int
 amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
                 bo = handle_table_lookup(&dev->bo_handles, i);
                 if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
                         continue;
-               if (cpu >= bo->cpu_ptr && cpu < (bo->cpu_ptr +
 bo->alloc_size))
+               if (cpu >= bo->cpu_ptr &&
+                  (uint64_t)cpu < ((uint64_t)bo->cpu_ptr +
 bo->alloc_size))
                         break;
         }
 
         if (i < dev->bo_handles.max_key) {
                 atomic_inc(&bo->refcount);
                 *buf_handle = bo;
-               *offset_in_bo = cpu - bo->cpu_ptr;
+               *offset_in_bo = (uint64_t)cpu - (uint64_t)bo->cpu_ptr;
Quoted 3 lines

        } else {
                *buf_handle = NULL;
                *offset_in_bo = 0;

Thanks for the suggestion, I sent out a v2 patch along those lines (though slightly different).

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Reviewed-by Junwei Zhang
#11 lore raw
In reply to a message not in the archive
Quoted 13 lines · Michel Dänzer

From: Michel Dänzer <[email protected]>

The compiler points out that an int doesn't work as intended if dev->bo_handles.max_key > INT_MAX:

../../amdgpu/amdgpu_bo.c: In function ‘amdgpu_find_bo_by_cpu_mapping’:
../../amdgpu/amdgpu_bo.c:550:16: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint32_t’ {aka ‘unsigned int’} [-Wsign-compare]

   for (i = 0; i < dev->bo_handles.max_key; i++) {
                 ^
../../amdgpu/amdgpu_bo.c:558:8: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint32_t’ {aka ‘unsigned int’} [-Wsign-compare]
   if (i < dev->bo_handles.max_key) {
         ^
Quoted 3 lines
Fixes 4d454424e1f2 ("amdgpu: add a function to find bo by cpu mapping
                      (v2)")

Thanks.

Quoted 10 lines
---
  amdgpu/amdgpu_bo.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
amdgpu/amdgpu_bo.c +0 -0
index b790e9be..86d1c143 100644

amdgpu/amdgpu_bo.c int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev, · line 541

Quoted 8 lines
amdgpu/amdgpu_bo.c +1 -1
@@ -535,7 +535,7 @@ int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
  				  amdgpu_bo_handle *buf_handle,
  				  uint64_t *offset_in_bo)
  {
-	int i;
+	uint32_t i;
  	struct amdgpu_bo *bo;
 
  	if (cpu == NULL || size == 0)

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