Skip to contentSkip to conversation

blk-iolatency: use spin_lock_irqsave() in iolatency_clear_scaling()

PATCH Superseded Tao Cui 2 participants 3 messages Jul 17

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH 1/1 blk-iolatency: use spin_lock_irqsave() in iolatency_clear_scaling()

From: Tao Cui <[email protected]>

child_lat.lock is acquired with spin_lock_irqsave() in both iolatency_check_latencies() (called from the blkcg_iolatency_done_bio() softirq path) and blkiolatency_timer_fn() (timer softirq). iolatency_clear_scaling() instead uses a plain spin_lock(), which only happens to be safe because both of its callers enter with interrupts already disabled:

* iolatency_set_limit() runs under queue_lock via blkg_conf_prep(),

    which returns with the lock held and interrupts disabled;

* iolatency_pd_offline() runs under queue_lock from blkg_destroy() and blkcg_deactivate_policy(), both of which take it with spin_lock_irq().

Take the lock with spin_lock_irqsave()/spin_unlock_irqrestore() so the locking is self-contained and consistent with the other two sites, instead of relying on an undocumented caller precondition. No functional change.

---
 block/blk-iolatency.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/blk-iolatency.c +3 -2
index cef02b6c5fa9..68b74258cd95 100644
@@ -811,16 +811,17 @@ static void iolatency_clear_scaling(struct blkcg_gq *blkg)
 	if (blkg->parent) {
 		struct iolatency_grp *iolat = blkg_to_lat(blkg->parent);
 		struct child_latency_info *lat_info;
+		unsigned long flags;
 		if (!iolat)
 			return;
 
 		lat_info = &iolat->child_lat;
-		spin_lock(&lat_info->lock);
+		spin_lock_irqsave(&lat_info->lock, flags);
 		atomic_set(&lat_info->scale_cookie, DEFAULT_SCALE_COOKIE);
 		lat_info->last_scale_event = 0;
 		lat_info->scale_grp = NULL;
 		lat_info->scale_lat = 0;
-		spin_unlock(&lat_info->lock);
+		spin_unlock_irqrestore(&lat_info->lock, flags);
 	}
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 28 lines · Tao Cui

From: Tao Cui <[email protected]>

child_lat.lock is acquired with spin_lock_irqsave() in both iolatency_check_latencies() (called from the blkcg_iolatency_done_bio() softirq path) and blkiolatency_timer_fn() (timer softirq). iolatency_clear_scaling() instead uses a plain spin_lock(), which only happens to be safe because both of its callers enter with interrupts already disabled:

* iolatency_set_limit() runs under queue_lock via blkg_conf_prep(),

    which returns with the lock held and interrupts disabled;

* iolatency_pd_offline() runs under queue_lock from blkg_destroy() and blkcg_deactivate_policy(), both of which take it with spin_lock_irq().

Take the lock with spin_lock_irqsave()/spin_unlock_irqrestore() so the locking is self-contained and consistent with the other two sites, instead of relying on an undocumented caller precondition. No functional change.

---
 block/blk-iolatency.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/blk-iolatency.c +0 -0
index cef02b6c5fa9..68b74258cd95 100644

block/blk-iolatency.c static void iolatency_clear_scaling(struct blkcg_gq *blkg) · line 819

Quoted 10 lines
block/blk-iolatency.c +2 -1
@@ -811,16 +811,17 @@ static void iolatency_clear_scaling(struct blkcg_gq *blkg)
 	if (blkg->parent) {
 		struct iolatency_grp *iolat = blkg_to_lat(blkg->parent);
 		struct child_latency_info *lat_info;
+		unsigned long flags;
 		if (!iolat)
 			return;
 
 		lat_info = &iolat->child_lat;
-		spin_lock(&lat_info->lock);
+		spin_lock_irqsave(&lat_info->lock, flags);

Better to use guard(spinlock_irqsave)(&lat_info->lock).

Thanks,
Leon

Quoted 9 lines
block/blk-iolatency.c +1 -1
 		atomic_set(&lat_info->scale_cookie, DEFAULT_SCALE_COOKIE);
 		lat_info->last_scale_event = 0;
 		lat_info->scale_grp = NULL;
 		lat_info->scale_lat = 0;
-		spin_unlock(&lat_info->lock);
+		spin_unlock_irqrestore(&lat_info->lock, flags);
 	}
 }
 
author
#2 lore raw
Quoted 36 lines · Leon Hwang
Quoted 35 lines · Tao Cui

From: Tao Cui <[email protected]>

child_lat.lock is acquired with spin_lock_irqsave() in both iolatency_check_latencies() (called from the blkcg_iolatency_done_bio() softirq path) and blkiolatency_timer_fn() (timer softirq). iolatency_clear_scaling() instead uses a plain spin_lock(), which only happens to be safe because both of its callers enter with interrupts already disabled:

* iolatency_set_limit() runs under queue_lock via blkg_conf_prep(),

    which returns with the lock held and interrupts disabled;

* iolatency_pd_offline() runs under queue_lock from blkg_destroy() and blkcg_deactivate_policy(), both of which take it with spin_lock_irq().

Take the lock with spin_lock_irqsave()/spin_unlock_irqrestore() so the locking is self-contained and consistent with the other two sites, instead of relying on an undocumented caller precondition. No functional change.

---
 block/blk-iolatency.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/blk-iolatency.c +1 -0
index cef02b6c5fa9..68b74258cd95 100644
@@ -811,16 +811,17 @@ static void iolatency_clear_scaling(struct blkcg_gq *blkg)
 	if (blkg->parent) {
 		struct iolatency_grp *iolat = blkg_to_lat(blkg->parent);
 		struct child_latency_info *lat_info;
+		unsigned long flags;
 		if (!iolat)
 			return;
 
Quoted 6 lines

block/blk-iolatency.c static void iolatency_clear_scaling(struct blkcg_gq *blkg) · line 819

Quoted 3 lines
block/blk-iolatency.c +1 -1
 		lat_info = &iolat->child_lat;
-		spin_lock(&lat_info->lock);
+		spin_lock_irqsave(&lat_info->lock, flags);

Better to use guard(spinlock_irqsave)(&lat_info->lock).

Good point, thanks. Will switch to guard(spinlock_irqsave)() in v2.

Thanks,
Tao

Quoted 13 lines

Thanks,
Leon

Quoted 9 lines
block/blk-iolatency.c +1 -1
 		atomic_set(&lat_info->scale_cookie, DEFAULT_SCALE_COOKIE);
 		lat_info->last_scale_event = 0;
 		lat_info->scale_grp = NULL;
 		lat_info->scale_lat = 0;
-		spin_unlock(&lat_info->lock);
+		spin_unlock_irqrestore(&lat_info->lock, flags);
 	}
 }