[PATCH 5/5] habanalabs: fix NULL pointer dereference

From: Oded Gabbay
Date: Sun Oct 10 2021 - 04:03:34 EST


From: Dani Liberman <dliberman@xxxxxxxxx>

When polling fences for multi CS, it is possible that fence is
no longer exists (its corresponding CS completed and the fence was
deleted) but we still accessing its parameters, causing NULL pointer
dereference.

Fixed by checking if fence exits before accessing its parameters.

Signed-off-by: Dani Liberman <dliberman@xxxxxxxxx>
Reviewed-by: Oded Gabbay <ogabbay@xxxxxxxxxx>
Signed-off-by: Oded Gabbay <ogabbay@xxxxxxxxxx>
---
drivers/misc/habanalabs/common/command_submission.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/habanalabs/common/command_submission.c b/drivers/misc/habanalabs/common/command_submission.c
index 8e4dc1441fff..a3358cc3c877 100644
--- a/drivers/misc/habanalabs/common/command_submission.c
+++ b/drivers/misc/habanalabs/common/command_submission.c
@@ -2382,7 +2382,15 @@ static int hl_cs_poll_fences(struct multi_cs_data *mcs_data)
break;
}

- mcs_data->stream_master_qid_map |= fence->stream_master_qid_map;
+ /*
+ * It is possible to get an old sequence numbers from user
+ * which related to already completed CSs and their fences
+ * already gone. In this case, no need to consider its QID for
+ * mcs completion.
+ */
+ if (fence)
+ mcs_data->stream_master_qid_map |=
+ fence->stream_master_qid_map;

/*
* Using mcs_handling_done to avoid possibility of mcs_data
@@ -2390,7 +2398,8 @@ static int hl_cs_poll_fences(struct multi_cs_data *mcs_data)
* all of its mcs handling, to avoid race the next time the
* user waits for mcs.
*/
- if (status == CS_WAIT_STATUS_BUSY || !fence->mcs_handling_done)
+ if (status == CS_WAIT_STATUS_BUSY ||
+ (fence && !fence->mcs_handling_done))
continue;

mcs_data->completion_bitmap |= BIT(i);
--
2.25.1