[PATCH 09/12] habanalabs: fix update of is_in_soft_reset

From: Oded Gabbay
Date: Mon Jul 11 2022 - 02:30:48 EST


reset_info.is_in_soft_reset should be updated both before in_reset
and inside the spin lock of the reset info structure.

The reasons are:

- When we are inside soft reset, it implies we are in reset. Therefore,
if someone checks if we are in soft reset, he can deduce we are
in reset, while the opposite is not correct and might be misleading.

- Both these flags are changed together so they must be changed
inside the reset info spinlock.

Signed-off-by: Oded Gabbay <ogabbay@xxxxxxxxxx>
---
drivers/misc/habanalabs/common/device.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/drivers/misc/habanalabs/common/device.c b/drivers/misc/habanalabs/common/device.c
index 4391eb22ddb8..5bc291c11e9b 100644
--- a/drivers/misc/habanalabs/common/device.c
+++ b/drivers/misc/habanalabs/common/device.c
@@ -1346,7 +1346,14 @@ int hl_device_reset(struct hl_device *hdev, u32 flags)
spin_unlock(&hdev->reset_info.lock);
return 0;
}
+
+ /* This still allows the completion of some KDMA ops
+ * Update this before in_reset because is_in_soft_reset implies we are in reset
+ */
+ hdev->reset_info.is_in_soft_reset = !hard_reset;
+
hdev->reset_info.in_reset = 1;
+
spin_unlock(&hdev->reset_info.lock);

if (delay_reset)
@@ -1354,9 +1361,6 @@ int hl_device_reset(struct hl_device *hdev, u32 flags)

handle_reset_trigger(hdev, flags);

- /* This still allows the completion of some KDMA ops */
- hdev->reset_info.is_in_soft_reset = !hard_reset;
-
/* This also blocks future CS/VM/JOB completion operations */
hdev->disabled = true;

@@ -1565,7 +1569,7 @@ int hl_device_reset(struct hl_device *hdev, u32 flags)
}

spin_lock(&hdev->reset_info.lock);
- hdev->reset_info.is_in_soft_reset = false;
+ hdev->reset_info.is_in_soft_reset = 0;

/* Schedule hard reset only if requested and if not already in hard reset.
* We keep 'in_reset' enabled, so no other reset can go in during the hard
@@ -1612,18 +1616,22 @@ int hl_device_reset(struct hl_device *hdev, u32 flags)

out_err:
hdev->disabled = true;
- hdev->reset_info.is_in_soft_reset = false;
+
+ spin_lock(&hdev->reset_info.lock);
+ hdev->reset_info.is_in_soft_reset = 0;

if (hard_reset) {
dev_err(hdev->dev, "Failed to reset! Device is NOT usable\n");
hdev->reset_info.hard_reset_cnt++;
} else if (reset_upon_device_release) {
+ spin_unlock(&hdev->reset_info.lock);
dev_err(hdev->dev, "Failed to reset device after user release\n");
flags |= HL_DRV_RESET_HARD;
flags &= ~HL_DRV_RESET_DEV_RELEASE;
hard_reset = true;
goto again;
} else {
+ spin_unlock(&hdev->reset_info.lock);
dev_err(hdev->dev, "Failed to do soft-reset\n");
hdev->reset_info.soft_reset_cnt++;
flags |= HL_DRV_RESET_HARD;
@@ -1633,6 +1641,8 @@ int hl_device_reset(struct hl_device *hdev, u32 flags)

hdev->reset_info.in_reset = 0;

+ spin_unlock(&hdev->reset_info.lock);
+
return rc;
}

--
2.25.1