Skip to contentSkip to conversation

add missing mutex lock to amdgpu_get_xgmi_hive() (v3)

PATCH In review StDenis, Tom 2 participants 2 messages Jan 7, 2019 1 message not in the archive

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

PATCH 1/1 add missing mutex lock to amdgpu_get_xgmi_hive() (v3)

v2: Move locks around in other functions so that this function can stand on its own. Also only hold the hive specific lock for add/remove device instead of the driver global lock so you can't add/remove devices in parallel from one hive.

v3: add reset_lock

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  6 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c   | 41 ++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.h   |  5 +--
 3 files changed, 33 insertions(+), 19 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -3
index 39d5d058b2c7..1a558dc41ba6 100644
@@ -3525,9 +3525,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 	 * by different nodes. No point also since the one node already executing
 	 * reset will also reset all the other nodes in the hive.
 	 */
-	hive = amdgpu_get_xgmi_hive(adev);
+	hive = amdgpu_get_xgmi_hive(adev, 0);
 	if (hive && adev->gmc.xgmi.num_physical_nodes > 1 &&
-	    !mutex_trylock(&hive->hive_lock))
+	    !mutex_trylock(&hive->reset_lock))
 		return 0;
 
 	/* Start with adev pre asic reset first for soft reset check.*/
@@ -3606,7 +3606,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
 	}
 
 	if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
-		mutex_unlock(&hive->hive_lock);
+		mutex_unlock(&hive->reset_lock);
 
 	if (r)
 		dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c +27 -14

84 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.h +3 -2
index 6151eb9c8ad3..14bc60664159 100644
@@ -29,10 +29,11 @@ struct amdgpu_hive_info {
 	struct list_head	device_list;
 	struct psp_xgmi_topology_info	topology_info;
 	int number_devices;
-	struct mutex hive_lock;
+	struct mutex hive_lock,
+		     reset_lock;
 };
 
-struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev);
+struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock);
 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev);
 int amdgpu_xgmi_add_device(struct amdgpu_device *adev);
 void amdgpu_xgmi_remove_device(struct amdgpu_device *adev);
Signature · 6 lines
2.17.2

_______________________________________________
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]

Reviewed-by Andrey Grodzovsky
#2 lore raw

Andrey

Quoted 140 lines · Tom

v2: Move locks around in other functions so that this function can stand on its own. Also only hold the hive specific lock for add/remove device instead of the driver global lock so you can't add/remove devices in parallel from one hive.

v3: add reset_lock

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  6 ++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c   | 41 ++++++++++++++--------
  drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.h   |  5 +--
  3 files changed, 33 insertions(+), 19 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -3
index 39d5d058b2c7..1a558dc41ba6 100644
@@ -3525,9 +3525,9 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  	 * by different nodes. No point also since the one node already executing
  	 * reset will also reset all the other nodes in the hive.
  	 */
-	hive = amdgpu_get_xgmi_hive(adev);
+	hive = amdgpu_get_xgmi_hive(adev, 0);
  	if (hive && adev->gmc.xgmi.num_physical_nodes > 1 &&
-	    !mutex_trylock(&hive->hive_lock))
+	    !mutex_trylock(&hive->reset_lock))
  		return 0;
 
  	/* Start with adev pre asic reset first for soft reset check.*/
@@ -3606,7 +3606,7 @@ int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  	}
 
  	if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
-		mutex_unlock(&hive->hive_lock);
+		mutex_unlock(&hive->reset_lock);
 
  	if (r)
  		dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c +27 -14
index 8a8bc60cb6b4..9e98ab8b1525 100644
@@ -40,26 +40,40 @@ void *amdgpu_xgmi_hive_try_lock(struct amdgpu_hive_info *hive)
  	return &hive->device_list;
  }
 
-struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev)
+struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock)
  {
  	int i;
  	struct amdgpu_hive_info *tmp;
 
  	if (!adev->gmc.xgmi.hive_id)
  		return NULL;
+
+	mutex_lock(&xgmi_mutex);
+
  	for (i = 0 ; i < hive_count; ++i) {
  		tmp = &xgmi_hives[i];
-		if (tmp->hive_id == adev->gmc.xgmi.hive_id)
+		if (tmp->hive_id == adev->gmc.xgmi.hive_id) {
+			if (lock)
+				mutex_lock(&tmp->hive_lock);
+			mutex_unlock(&xgmi_mutex);
  			return tmp;
+		}
  	}
-	if (i >= AMDGPU_MAX_XGMI_HIVE)
+	if (i >= AMDGPU_MAX_XGMI_HIVE) {
+		mutex_unlock(&xgmi_mutex);
  		return NULL;
+	}
 
  	/* initialize new hive if not exist */
  	tmp = &xgmi_hives[hive_count++];
  	tmp->hive_id = adev->gmc.xgmi.hive_id;
  	INIT_LIST_HEAD(&tmp->device_list);
  	mutex_init(&tmp->hive_lock);
+	mutex_init(&tmp->reset_lock);
+	if (lock)
+		mutex_lock(&tmp->hive_lock);
+
+	mutex_unlock(&xgmi_mutex);
 
  	return tmp;
  }
@@ -111,8 +125,8 @@ int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
  		return ret;
  	}
 
-	mutex_lock(&xgmi_mutex);
-	hive = amdgpu_get_xgmi_hive(adev);
+	/* find hive and take lock */
+	hive = amdgpu_get_xgmi_hive(adev, 1);
  	if (!hive)
  		goto exit;
 
@@ -142,8 +156,8 @@ int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
  			break;
  	}
 
+	mutex_unlock(&hive->hive_lock);
  exit:
-	mutex_unlock(&xgmi_mutex);
  	return ret;
  }
 
@@ -154,15 +168,14 @@ void amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
  	if (!adev->gmc.xgmi.supported)
  		return;
 
-	mutex_lock(&xgmi_mutex);
-
-	hive = amdgpu_get_xgmi_hive(adev);
+	hive = amdgpu_get_xgmi_hive(adev, 1);
  	if (!hive)
-		goto exit;
+		return;
 
-	if (!(hive->number_devices--))
+	if (!(hive->number_devices--)) {
  		mutex_destroy(&hive->hive_lock);
-
-exit:
-	mutex_unlock(&xgmi_mutex);
+		mutex_destroy(&hive->reset_lock);
+	} else {
+		mutex_unlock(&hive->hive_lock);
+	}
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.h +0 -0
index 6151eb9c8ad3..14bc60664159 100644
@@ -29,10 +29,11 @@ struct amdgpu_hive_info {
  	struct list_head	device_list;
  	struct psp_xgmi_topology_info	topology_info;
  	int number_devices;

drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.h struct amdgpu_hive_info { · line 39

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.h +3 -2
-	struct mutex hive_lock;
+	struct mutex hive_lock,
+		     reset_lock;
  };
 
-struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev);
+struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock);
  int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev);
  int amdgpu_xgmi_add_device(struct amdgpu_device *adev);
  void amdgpu_xgmi_remove_device(struct amdgpu_device *adev);

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