Skip to contentSkip to conversation

amd/amdgpu_ctx: Use struct_size() helper and kmalloc()

PATCH Applied Gustavo A. R. Silva 3 participants 6 messages Oct 8, 2020 – Oct 9, 2020

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

PATCH 1/1 amd/amdgpu_ctx: Use struct_size() helper and kmalloc()

Make use of the new struct_size() helper instead of the offsetof() idiom. Also, use kmalloc() instead of kcalloc().

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
index c80d8339f58c..5be125f3b92a 100644
@@ -100,7 +100,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
 	enum drm_sched_priority priority;
 	int r;
 
-	entity = kcalloc(1, offsetof(typeof(*entity), fences[amdgpu_sched_jobs]),
+	entity = kmalloc(struct_size(entity, fences, amdgpu_sched_jobs),
 			 GFP_KERNEL);
 	if (!entity)
 		return  -ENOMEM;
Signature · 1 line
2.27.0
Applied
#1 lore raw

Applied. Thanks!

Alex

Quoted 29 lines · Gustavo A. R. Silva

Make use of the new struct_size() helper instead of the offsetof() idiom. Also, use kmalloc() instead of kcalloc().

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -2
index c80d8339f58c..5be125f3b92a 100644
@@ -100,7 +100,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
        enum drm_sched_priority priority;
        int r;
 
-       entity = kcalloc(1, offsetof(typeof(*entity), fences[amdgpu_sched_jobs]),
+       entity = kmalloc(struct_size(entity, fences, amdgpu_sched_jobs),
                         GFP_KERNEL);
        if (!entity)
                return  -ENOMEM;
--

2.27.0

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

maintainer
#2 lore raw
In reply to Gustavo A. R. Silva

Am 08.10.20 um 16:34 schrieb Gustavo A. R. Silva:

Quoted 13 lines

Make use of the new struct_size() helper instead of the offsetof() idiom. Also, use kmalloc() instead of kcalloc().

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
index c80d8339f58c..5be125f3b92a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 103

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
@@ -100,7 +100,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
  	enum drm_sched_priority priority;
  	int r;
 
-	entity = kcalloc(1, offsetof(typeof(*entity), fences[amdgpu_sched_jobs]),
+	entity = kmalloc(struct_size(entity, fences, amdgpu_sched_jobs),

NAK. You could use kzalloc() here, but kmalloc won't zero initialize the memory which could result in unforeseen consequences.

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
  			 GFP_KERNEL);
  	if (!entity)
  		return  -ENOMEM;
#3 lore raw
Quoted 9 lines · Christian König

Am 08.10.20 um 16:34 schrieb Gustavo A. R. Silva:

Quoted 8 lines

Make use of the new struct_size() helper instead of the offsetof() idiom. Also, use kmalloc() instead of kcalloc().

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 102

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
index c80d8339f58c..5be125f3b92a 100644
@@ -100,7 +100,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
  	enum drm_sched_priority priority;
  	int r;
-	entity = kcalloc(1, offsetof(typeof(*entity), fences[amdgpu_sched_jobs]),
+	entity = kmalloc(struct_size(entity, fences, amdgpu_sched_jobs),

NAK. You could use kzalloc() here, but kmalloc won't zero initialize the memory which could result in unforeseen consequences.

Oh I see.. I certainly didn't take that into account.

I'll fix that up and respin.

Thanks
--
Gustavo

maintainer
#4 lore raw

Am 09.10.20 um 15:54 schrieb Gustavo A. R. Silva:

Quoted 21 lines · Christian König
Quoted 9 lines

Am 08.10.20 um 16:34 schrieb Gustavo A. R. Silva:

Quoted 8 lines

Make use of the new struct_size() helper instead of the offsetof() idiom. Also, use kmalloc() instead of kcalloc().

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 102

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
index c80d8339f58c..5be125f3b92a 100644
@@ -100,7 +100,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
   	enum drm_sched_priority priority;
   	int r;
-	entity = kcalloc(1, offsetof(typeof(*entity), fences[amdgpu_sched_jobs]),
+	entity = kmalloc(struct_size(entity, fences, amdgpu_sched_jobs),

NAK. You could use kzalloc() here, but kmalloc won't zero initialize the memory which could result in unforeseen consequences.

Quoted 3 lines

Oh I see.. I certainly didn't take that into account.

I'll fix that up and respin.

Shit happens, we already have a fix for this. Alex merged it and it immediately broke our testing systems.

So one of our engineers came up with a fix which should already have been applied.

Christian.

Quoted 4 lines

Thanks
--
Gustavo

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

#5 lore raw
Quoted 10 lines · Christian König
Quoted 7 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 103

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
-	entity = kcalloc(1, offsetof(typeof(*entity), fences[amdgpu_sched_jobs]),
+	entity = kmalloc(struct_size(entity, fences, amdgpu_sched_jobs),

NAK. You could use kzalloc() here, but kmalloc won't zero initialize the memory which could result in unforeseen consequences.

Oh I see.. I certainly didn't take that into account.

I'll fix that up and respin.

Shit happens, we already have a fix for this. Alex merged it and it immediately broke our testing systems.

:/

Quoted 2 lines

So one of our engineers came up with a fix which should already have been applied.

Great. Good to know it's already fixed! :)

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