Skip to contentSkip to conversation

drm/amdkfd: Fix UBSAN shift-out-of-bounds warning

PATCH In review Anson Jacob 3 participants 6 messages Mar 4, 2021 – Mar 15, 2021

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

PATCH 1/1 drm/amdkfd: Fix UBSAN shift-out-of-bounds warning

If get_num_sdma_queues or get_num_xgmi_sdma_queues is 0, we end up doing a shift operation where the number of bits shifted equals number of bits in the operand. This behaviour is undefined.

Set num_sdma_queues or num_xgmi_sdma_queues to ULLONG_MAX, if the count is >= number of bits in the operand.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c   | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +15 -2
index c37e9c4b1fb4..e7a3c496237f 100644
@@ -1128,6 +1128,9 @@ static int set_sched_resources(struct device_queue_manager *dqm)
 
 static int initialize_cpsch(struct device_queue_manager *dqm)
 {
+	uint64_t num_sdma_queues;
+	uint64_t num_xgmi_sdma_queues;
+
 	pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
 
 	mutex_init(&dqm->lock_hidden);
@@ -1136,8 +1139,18 @@ static int initialize_cpsch(struct device_queue_manager *dqm)
 	dqm->active_cp_queue_count = 0;
 	dqm->gws_queue_count = 0;
 	dqm->active_runlist = false;
-	dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm));
-	dqm->xgmi_sdma_bitmap = ~0ULL >> (64 - get_num_xgmi_sdma_queues(dqm));
+
+	num_sdma_queues = get_num_sdma_queues(dqm);
+	if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap))
+		dqm->sdma_bitmap = ULLONG_MAX;
+	else
+		dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1);
+
+	num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm);
+	if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap))
+		dqm->xgmi_sdma_bitmap = ULLONG_MAX;
+	else
+		dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1);
 
 	INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);
 
Signature · 6 lines
2.25.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Reviewed-by Alex Deucher Reviewed-by Felix Kuehling on v1 1/1
#1 lore raw

[AMD Public Use]

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Anson Jacob
Sent: Friday, March 5, 2021 1:39 AM
To: [email protected]
Cc: Jacob, Anson <[email protected]>; Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Subject: [PATCH] drm/amdkfd: Fix UBSAN shift-out-of-bounds warning

If get_num_sdma_queues or get_num_xgmi_sdma_queues is 0, we end up doing a shift operation where the number of bits shifted equals number of bits in the operand. This behaviour is undefined.

Set num_sdma_queues or num_xgmi_sdma_queues to ULLONG_MAX, if the count is >= number of bits in the operand.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c   | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +6 -2
index c37e9c4b1fb4..e7a3c496237f 100644
@@ -1128,6 +1128,9 @@ static int set_sched_resources(struct device_queue_manager *dqm)
 
 static int initialize_cpsch(struct device_queue_manager *dqm)  {
+	uint64_t num_sdma_queues;
+	uint64_t num_xgmi_sdma_queues;
+
 	pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
 
 	mutex_init(&dqm->lock_hidden);
@@ -1136,8 +1139,18 @@ static int initialize_cpsch(struct device_queue_manager *dqm)
 	dqm->active_cp_queue_count = 0;
 	dqm->gws_queue_count = 0;
 	dqm->active_runlist = false;
-	dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm));
-	dqm->xgmi_sdma_bitmap = ~0ULL >> (64 - get_num_xgmi_sdma_queues(dqm));
+
+	num_sdma_queues = get_num_sdma_queues(dqm);
+	if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap))
 

< > No assumption about bitmap size here

+ dqm->sdma_bitmap = ULLONG_MAX;

<> This assumes/fixes the max size. In that case why not make the earlier check also consistent?

+ else
+ dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1);
+
+ num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm);
+ if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap))
+ dqm->xgmi_sdma_bitmap = ULLONG_MAX;
+ else
+ dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1);

INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);

--
2.25.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7Clijo.lazar%40amd.com%7Cc731ee10b10b4728138808d8df496648%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637504853648191471%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=EpuPBBf03EMa0S7rOqI8JieOmcT3GvsnsQMaYujGgeY%3D&amp;reserved=0
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reviewed-by Alex Deucher Reviewed-by Felix Kuehling
#2 lore raw

[AMD Public Use]

Hi Anson,

Please ignore the earlier comment.

Thanks,
Lijo

-----Original Message-----
From: Lazar, Lijo
Sent: Friday, March 5, 2021 11:05 AM
To: Anson Jacob <[email protected]>; [email protected]
Cc: Jacob, Anson <[email protected]>; Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [PATCH] drm/amdkfd: Fix UBSAN shift-out-of-bounds warning

[AMD Public Use]

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Anson Jacob
Sent: Friday, March 5, 2021 1:39 AM
To: [email protected]
Cc: Jacob, Anson <[email protected]>; Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Subject: [PATCH] drm/amdkfd: Fix UBSAN shift-out-of-bounds warning

If get_num_sdma_queues or get_num_xgmi_sdma_queues is 0, we end up doing a shift operation where the number of bits shifted equals number of bits in the operand. This behaviour is undefined.

Set num_sdma_queues or num_xgmi_sdma_queues to ULLONG_MAX, if the count is >= number of bits in the operand.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c   | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +6 -2
index c37e9c4b1fb4..e7a3c496237f 100644
@@ -1128,6 +1128,9 @@ static int set_sched_resources(struct device_queue_manager *dqm)
 
 static int initialize_cpsch(struct device_queue_manager *dqm)  {
+	uint64_t num_sdma_queues;
+	uint64_t num_xgmi_sdma_queues;
+
 	pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
 
 	mutex_init(&dqm->lock_hidden);
@@ -1136,8 +1139,18 @@ static int initialize_cpsch(struct device_queue_manager *dqm)
 	dqm->active_cp_queue_count = 0;
 	dqm->gws_queue_count = 0;
 	dqm->active_runlist = false;
-	dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm));
-	dqm->xgmi_sdma_bitmap = ~0ULL >> (64 - get_num_xgmi_sdma_queues(dqm));
+
+	num_sdma_queues = get_num_sdma_queues(dqm);
+	if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap))
 

< > No assumption about bitmap size here

+ dqm->sdma_bitmap = ULLONG_MAX;

<> This assumes/fixes the max size. In that case why not make the earlier check also consistent?

+ else
+ dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1);
+
+ num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm);
+ if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap))
+ dqm->xgmi_sdma_bitmap = ULLONG_MAX;
+ else
+ dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1);

INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);

--
2.25.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7Clijo.lazar%40amd.com%7Cc731ee10b10b4728138808d8df496648%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637504853648191471%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=EpuPBBf03EMa0S7rOqI8JieOmcT3GvsnsQMaYujGgeY%3D&amp;reserved=0
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Tested-by on v1 1/1
#3 lore raw
In reply to Anson Jacob

That just leaves the KASAN error from read_indirect_azalia_reg, thanks for the fix!

Quoted 54 lines · Anson Jacob

If get_num_sdma_queues or get_num_xgmi_sdma_queues is 0, we end up doing a shift operation where the number of bits shifted equals number of bits in the operand. This behaviour is undefined.

Set num_sdma_queues or num_xgmi_sdma_queues to ULLONG_MAX, if the count is >= number of bits in the operand.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c   | 17 +++++++++++++++--

 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0 -0
index c37e9c4b1fb4..e7a3c496237f 100644
@@ -1128,6 +1128,9 @@ static int set_sched_resources(struct
 device_queue_manager *dqm)


 static int initialize_cpsch(struct device_queue_manager *dqm) {
+       uint64_t num_sdma_queues;
+       uint64_t num_xgmi_sdma_queues;
+
        pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
        mutex_init(&dqm->lock_hidden);

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +12 -2
@@ -1136,8 +1139,18 @@ static int initialize_cpsch(struct device_queue_manager
 *dqm)
         dqm->active_cp_queue_count = 0;
         dqm->gws_queue_count = 0;
         dqm->active_runlist = false;
-       dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm));
-       dqm->xgmi_sdma_bitmap = ~0ULL >> (64 - get_num_xgmi_sdma_queues(dqm));
+
+       num_sdma_queues = get_num_sdma_queues(dqm);
+       if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap))
+               dqm->sdma_bitmap = ULLONG_MAX;
+       else
+               dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1);
+
+       num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm);
+       if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap))
+               dqm->xgmi_sdma_bitmap = ULLONG_MAX;
+       else
+               dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1);


        INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);

Signature · 13 lines
Sincerely,
   Lyude Paul (she/her)
   Software Engineer at Red Hat
   
Note: I deal with a lot of emails and have a lot of bugs on my plate. If you've
asked me a question, are waiting for a review/merge on a patch, etc. and I
haven't responded in a while, please feel free to send me another email to check
on my status. I don't bite!

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
author Tested-by Lyude Paul
#4 lore raw

[AMD Public Use]

Thanks Lyude for testing the patch.

Are you referring to this issue [1] ?

Is it reproducible after applying this patch as well ?

[1] https://gitlab.freedesktop.org/drm/amd/-/issues/1473

-- Anson
________________________________
From: Lyude Paul <[email protected]>
Sent: Friday, March 5, 2021 6:08 PM
To: Jacob, Anson <[email protected]>; [email protected] <[email protected]>
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH] drm/amdkfd: Fix UBSAN shift-out-of-bounds warning

That just leaves the KASAN error from read_indirect_azalia_reg, thanks for the fix!

Quoted 44 lines · Anson Jacob

If get_num_sdma_queues or get_num_xgmi_sdma_queues is 0, we end up doing a shift operation where the number of bits shifted equals number of bits in the operand. This behaviour is undefined.

Set num_sdma_queues or num_xgmi_sdma_queues to ULLONG_MAX, if the count is >= number of bits in the operand.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c   | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3 -0
index c37e9c4b1fb4..e7a3c496237f 100644
@@ -1128,6 +1128,9 @@ static int set_sched_resources(struct
 device_queue_manager *dqm)
 
 static int initialize_cpsch(struct device_queue_manager *dqm)
 {
+       uint64_t num_sdma_queues;
+       uint64_t num_xgmi_sdma_queues;
+
        pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
 
        mutex_init(&dqm->lock_hidden);
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4 -2
@@ -1136,8 +1139,18 @@ static int initialize_cpsch(struct device_queue_manager
 *dqm)
        dqm->active_cp_queue_count = 0;
        dqm->gws_queue_count = 0;
        dqm->active_runlist = false;
-       dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm));
-       dqm->xgmi_sdma_bitmap = ~0ULL >> (64 - get_num_xgmi_sdma_queues(dqm));
+
+       num_sdma_queues = get_num_sdma_queues(dqm);
+       if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap))
+               dqm->sdma_bitmap = ULLONG_MAX;

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static int initialize_cpsch(struct device_queue_manager · line 1156

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +8 -0
+       else
+               dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1);
+
+       num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm);
+       if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap))
+               dqm->xgmi_sdma_bitmap = ULLONG_MAX;
+       else
+               dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1);
 
        INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);

--
Sincerely,
Lyude Paul (she/her)
Software Engineer at Red Hat

Note: I deal with a lot of emails and have a lot of bugs on my plate. If you've asked me a question, are waiting for a review/merge on a patch, etc. and I haven't responded in a while, please feel free to send me another email to check on my status. I don't bite!

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

#5 lore raw
Quoted 7 lines · Anson

[AMD Public Use]

Thanks Lyude for testing the patch.

Are you referring to this issue [1] ?

Is it reproducible after applying this patch as well ?

Yes I am - and yeah, if you're talking about the patch you originally asked me to try then yes- I'm still able to reproduce it with that patch applied

Quoted 75 lines

[1] https://gitlab.freedesktop.org/drm/amd/-/issues/1473

-- Anson
From: Lyude Paul <[email protected]>
Sent: Friday, March 5, 2021 6:08 PM
To: Jacob, Anson <[email protected]>; [email protected] <amd- [email protected]>
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Subject: Re: [PATCH] drm/amdkfd: Fix UBSAN shift-out-of-bounds warning

That just leaves the KASAN error from read_indirect_azalia_reg, thanks for the fix!

Quoted 8 lines · Anson Jacob

If get_num_sdma_queues or get_num_xgmi_sdma_queues is 0, we end up doing a shift operation where the number of bits shifted equals number of bits in the operand. This behaviour is undefined.

Set num_sdma_queues or num_xgmi_sdma_queues to ULLONG_MAX, if the count is >= number of bits in the operand.

Bug:

https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.freedesktop.org%2Fdrm%2Famd%2F-%2Fissues%2F1472&amp;data=04%7C01%7Canson.jacob%40amd.com%7Ce9ea7130bcd945a194aa08d8e02ba9f8%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637505826204096667%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=GEyObPt%2FnEeU9Y7K2I30RS1TBMGbbUneQ6hWkR7pJLM%3D&amp;reserved=0

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static int initialize_cpsch(struct

Quoted 25 lines

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c   | 17 +++++++++++++++--

 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0 -0
index c37e9c4b1fb4..e7a3c496237f 100644
@@ -1128,6 +1128,9 @@ static int set_sched_resources(struct
 device_queue_manager *dqm)


 static int initialize_cpsch(struct device_queue_manager *dqm) {
+       uint64_t num_sdma_queues;
+       uint64_t num_xgmi_sdma_queues;
+
        pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
        mutex_init(&dqm->lock_hidden);

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0 -0
@@ -1136,8 +1139,18 @@ static int initialize_cpsch(struct

device_queue_manager

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static int initialize_cpsch(struct device_queue_manager *dqm)

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0 -2
 *dqm)
         dqm->active_cp_queue_count = 0;
         dqm->gws_queue_count = 0;
         dqm->active_runlist = false;
-       dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm));
-       dqm->xgmi_sdma_bitmap = ~0ULL >> (64 -

get_num_xgmi_sdma_queues(dqm));

Quoted 15 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +12 -0
+
+       num_sdma_queues = get_num_sdma_queues(dqm);
+       if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap))
+               dqm->sdma_bitmap = ULLONG_MAX;
+       else
+               dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1);
+
+       num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm);
+       if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap))
+               dqm->xgmi_sdma_bitmap = ULLONG_MAX;
+       else
+               dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1);


        INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);

Signature · 8 lines
Sincerely,
Lyude Paul (she/her)
Software Engineer at Red Hat

Note: I deal with a lot of emails and have a lot of bugs on my plate. If you've
asked me a question, are waiting for a review/merge on a patch, etc. and I
haven't responded in a while, please feel free to send me another email to check
on my status. I don't bite!

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