Skip to contentSkip to conversation

drm/sched: Lock drm_sched_entity_is_idle()

PATCH Applied Philipp Stanner 3 participants 3 messages Aug 13 – Aug 18

Subsystems (3)
lore
Share & tools
lore

Philipp Stanner

author maintainer
#0 lore raw

PATCH 1/1 drm/sched: Lock drm_sched_entity_is_idle()

drm_sched_entity_is_idle() contains a badly documented memory barrier and an invalid lockless access to entity->stopped. Moreover, it also accesses entity->list which is at all other places protected by entity->lock.

This function is in no way performance critical, so it is safer, more readable and more maintainable to take the spinlock.

Add locking to drm_sched_entity_is_idle().

---
 drivers/gpu/drm/scheduler/sched_entity.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +7 -3
index c51101ec70c1..2d941b2b2861 100644
@@ -179,14 +179,18 @@ EXPORT_SYMBOL(drm_sched_entity_modify_sched);
 
 static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity)
 {
-	rmb(); /* for list_empty to work without lock */
+	bool idle = false;
+
+	spin_lock(&entity->lock);
 
 	if (list_empty(&entity->list) ||
 	    spsc_queue_count(&entity->job_queue) == 0 ||
 	    entity->stopped)
-		return true;
+		idle = true;
 
-	return false;
+	spin_unlock(&entity->lock);
+
+	return idle;
 }
 
 /**

base-commit: c9a2c8ddcd65e96e7f095a87c0cc0b3211f740ea

Signature · 1 line
2.55.0
#1 lore raw
Quoted 42 lines · Philipp Stanner

drm_sched_entity_is_idle() contains a badly documented memory barrier and an invalid lockless access to entity->stopped. Moreover, it also accesses entity->list which is at all other places protected by entity->lock.

This function is in no way performance critical, so it is safer, more readable and more maintainable to take the spinlock.

Add locking to drm_sched_entity_is_idle().

---
  drivers/gpu/drm/scheduler/sched_entity.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +7 -3
index c51101ec70c1..2d941b2b2861 100644
@@ -179,14 +179,18 @@ EXPORT_SYMBOL(drm_sched_entity_modify_sched);
 
  static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity)
  {
-	rmb(); /* for list_empty to work without lock */
+	bool idle = false;
+
+	spin_lock(&entity->lock);
 
  	if (list_empty(&entity->list) ||
  	    spsc_queue_count(&entity->job_queue) == 0 ||
  	    entity->stopped)
-		return true;
+		idle = true;
 
-	return false;
+	spin_unlock(&entity->lock);
+
+	return idle;
  }
 
  /**
Quoted 1 line

base-commit: c9a2c8ddcd65e96e7f095a87c0cc0b3211f740ea

Spotted by chance on the mailing list but I believe I gave you r-b for this one already.

Regards,

Tvrtko

#2 lore raw
Quoted 13 lines · Tvrtko Ursulin
Quoted 10 lines · Philipp Stanner

drm_sched_entity_is_idle() contains a badly documented memory barrier and an invalid lockless access to entity->stopped. Moreover, it also accesses entity->list which is at all other places protected by entity->lock.

This function is in no way performance critical, so it is safer, more readable and more maintainable to take the spinlock.

Add locking to drm_sched_entity_is_idle().

[…]

Quoted 2 lines

Spotted by chance on the mailing list but I believe I gave you r-b for this one already.

Applied to misc-next with your r-b.

Thx