Skip to contentSkip to conversation

block/blk-iocost: read ioc_pd_stat params inside ioc->lock

PATCH In review Tao Cui 2 participants 3 messages Aug 3 – Aug 4

Subsystems (3)
lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH v2 1/1 block/blk-iocost: read ioc_pd_stat params inside ioc->lock

From: Tao Cui <[email protected]>

ioc_pd_stat() reads ioc->enabled, ioc->vtime_base_rate, and iocg->last_stat without holding ioc->lock, which trips KCSAN since ioc_adjust_base_vrate() and iocg_flush_stat_upward() write those fields under ioc->lock.

Commit 35198e323001 ("blk-iocost: read params inside lock in sysfs apis") fixed the same issue in ioc_qos_prfill() and ioc_cost_model_prfill(), but ioc_pd_stat() was missed.

Add spin_lock_irqsave/irqrestore(&ioc->lock) around the reads. Use irqsave/irqrestore instead of the irq variant used by ioc_qos_prfill() because ioc_pd_stat() is called from blkcg_print_stat() which already disables IRQs via guard(spinlock_irq)(&blkcg->lock); an unconditional spin_unlock_irq would wrongly re-enable them.

Fixes 35198e323001 ("blk-iocost: read params inside lock in sysfs apis")

---

Changes in v2:
- Use spin_lock_irqsave/irqrestore instead of spin_lock_irq/irq, since the caller (blkcg_print_stat) may already have IRQs disabled. (Sashiko review)

---
 block/blk-iocost.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
block/blk-iocost.c +6 -1
index 8b2aeba2e1e3..977fe9ecff69 100644
@@ -3092,9 +3092,12 @@ static void ioc_pd_stat(struct blkg_policy_data *pd, struct seq_file *s)
 {
 	struct ioc_gq *iocg = pd_to_iocg(pd);
 	struct ioc *ioc = iocg->ioc;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ioc->lock, flags);
 
 	if (!ioc->enabled)
-		return;
+		goto out;
 
 	if (iocg->level == 0) {
 		unsigned vp10k = DIV64_U64_ROUND_CLOSEST(
@@ -3110,6 +3113,8 @@ static void ioc_pd_stat(struct blkg_policy_data *pd, struct seq_file *s)
 			iocg->last_stat.wait_us,
 			iocg->last_stat.indebt_us,
 			iocg->last_stat.indelay_us);
+out:
+	spin_unlock_irqrestore(&ioc->lock, flags);
 }
 
 static u64 ioc_weight_prfill(struct seq_file *sf, struct blkg_policy_data *pd,
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 15 lines · Tao Cui
block/blk-iocost.c +4 -1
@@ -3092,9 +3092,12 @@ static void ioc_pd_stat(struct blkg_policy_data *pd, struct seq_file *s)
 {
 	struct ioc_gq *iocg = pd_to_iocg(pd);
 	struct ioc *ioc = iocg->ioc;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ioc->lock, flags);
 
 	if (!ioc->enabled)
-		return;
+		goto out;
 
 	if (iocg->level == 0) {
 		unsigned vp10k = DIV64_U64_ROUND_CLOSEST(

block/blk-iocost.c static void ioc_pd_stat(struct blkg_policy_data *pd, struct seq_file *s) · line 3118

Quoted 6 lines
block/blk-iocost.c +2 -0
@@ -3110,6 +3113,8 @@ static void ioc_pd_stat(struct blkg_policy_data *pd, struct seq_file *s)
 			iocg->last_stat.wait_us,
 			iocg->last_stat.indebt_us,
 			iocg->last_stat.indelay_us);
+out:
+	spin_unlock_irqrestore(&ioc->lock, flags);
 }

There's nothing in this function that requires synchronized reads. Might as well just annotate them with data_race(). Otherwise, this can lead to a lot of lock operations when there are a lot of cgroups and high frequency stat reads.

Thanks.

Signature · 1 line
tejun
author
#2 lore raw
Quoted 13 lines · Tejun Heo
Quoted 12 lines · Tao Cui
block/blk-iocost.c +4 -1
@@ -3092,9 +3092,12 @@ static void ioc_pd_stat(struct blkg_policy_data *pd, struct seq_file *s)
 {
 	struct ioc_gq *iocg = pd_to_iocg(pd);
 	struct ioc *ioc = iocg->ioc;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ioc->lock, flags);
 
 	if (!ioc->enabled)
-		return;
+		goto out;
 
Quoted 15 lines

block/blk-iocost.c static void ioc_pd_stat(struct blkg_policy_data *pd, struct seq_file *s) · line 3118

Quoted 9 lines
block/blk-iocost.c +2 -0
 	if (iocg->level == 0) {
 		unsigned vp10k = DIV64_U64_ROUND_CLOSEST(
@@ -3110,6 +3113,8 @@ static void ioc_pd_stat(struct blkg_policy_data *pd, struct seq_file *s)
 			iocg->last_stat.wait_us,
 			iocg->last_stat.indebt_us,
 			iocg->last_stat.indelay_us);
+out:
+	spin_unlock_irqrestore(&ioc->lock, flags);
 }

There's nothing in this function that requires synchronized reads. Might as well just annotate them with data_race(). Otherwise, this can lead to a lot of lock operations when there are a lot of cgroups and high frequency stat reads.

Agreed, there's nothing in this function that requires synchronized reads, these are all stat values where stale reads are harmless. Will switch to data_race() in v3.

Thanks,
Tao

Quoted 2 lines

Thanks.