blk-cgroup: two fixes on list corruption
Subsystems (2)
cover blk-cgroup: two fixes on list corruption
Hello,
The 1st patch fixes list corruption when running reset_iostat(cgroup v1).
The 2nd patch fixes potential list corruption when reordering of writting to ->lqueued and reading from iostat instance.
Ming Lei (2):
blk-cgroup: fix list corruption from resetting io stat
blk-cgroup: fix list corruption from reorder of WRITE ->lqueued
block/blk-cgroup.c | 68 ++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 23 deletions(-)
Signature · 1 line
2.44.0
PATCH 1/2 blk-cgroup: fix list corruption from resetting io stat
Since commit 3b8cc6298724 ("blk-cgroup: Optimize blkcg_rstat_flush()"), each iostat instance is added to blkcg percpu list, so blkcg_reset_stats() can't reset the stat instance by memset(), otherwise the llist may be corrupted.
Fix the issue by only resetting the counter part.
--- block/blk-cgroup.c | 58 ++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 23 deletions(-)
block/blk-cgroup.c +35 -23
@@ -619,12 +619,45 @@ static void blkg_destroy_all(struct gendisk *disk)spin_unlock_irq (&q->queue_lock); } +static void blkg_iostat_set (struct blkg_iostat *dst,struct blkg_iostat *src) +{ +int i; + +for (i =0 ; i <BLKG_IOSTAT_NR ; i++) { + dst->bytes[i] = src->bytes[i]; + dst->ios[i] = src->ios[i]; + } +} + +static void __blkg_clear_stat (struct blkg_iostat_set *bis) +{ +struct blkg_iostat cur = {0 }; +unsigned long flags; + + flags =u64_stats_update_begin_irqsave (&bis->sync); +blkg_iostat_set (&bis->cur, &cur); +blkg_iostat_set (&bis->last, &cur); +u64_stats_update_end_irqrestore (&bis->sync, flags); +} + +static void blkg_clear_stat (struct blkcg_gq *blkg) +{ +int cpu; + +for_each_possible_cpu (cpu) { +struct blkg_iostat_set *s =per_cpu_ptr (blkg->iostat_cpu, cpu); + +__blkg_clear_stat (s); + } +__blkg_clear_stat (&blkg->iostat); +} +static int blkcg_reset_stats (struct cgroup_subsys_state *css,struct cftype *cftype,u64 val) {struct blkcg *blkcg =css_to_blkcg (css);struct blkcg_gq *blkg; -int i, cpu; +int i;mutex_lock (&blkcg_pol_mutex);spin_lock_irq (&blkcg->lock);
@@ -635,18 +668,7 @@ static int blkcg_reset_stats(struct cgroup_subsys_state *css,* anyway. If you get hit by a race, retry. */ hlist_for_each_entry (blkg, &blkcg->blkg_list, blkcg_node) { -for_each_possible_cpu (cpu) { -struct blkg_iostat_set *bis = -per_cpu_ptr (blkg->iostat_cpu, cpu); -memset (bis,0 ,sizeof (*bis)); - -/* Re-initialize the cleared blkg_iostat_set */ -u64_stats_init (&bis->sync); - bis->blkg = blkg; - } -memset (&blkg->iostat,0 ,sizeof (blkg->iostat)); -u64_stats_init (&blkg->iostat.sync); - +blkg_clear_stat (blkg);for (i =0 ; i <BLKCG_MAX_POLS ; i++) {struct blkcg_policy *pol = blkcg_policy[i];
@@ -949,16 +971,6 @@ void blkg_conf_exit(struct blkg_conf_ctx *ctx) }EXPORT_SYMBOL_GPL (blkg_conf_exit); -static void blkg_iostat_set (struct blkg_iostat *dst,struct blkg_iostat *src) -{ -int i; - -for (i =0 ; i <BLKG_IOSTAT_NR ; i++) { - dst->bytes[i] = src->bytes[i]; - dst->ios[i] = src->ios[i]; - } -} -static void blkg_iostat_add (struct blkg_iostat *dst,struct blkg_iostat *src) {int i;
Signature · 1 line
2.44.0
Re: [PATCH 1/2] blk-cgroup: fix list corruption from resetting io stat
Quoted 94 lines · Ming Lei
Since commit
3b8cc6298724("blk-cgroup: Optimizeblkcg_rstat_flush()"), each iostat instance is added to blkcg percpu list, soblkcg_reset_stats()can't reset the stat instance bymemset(), otherwise the llist may be corrupted.Fix the issue by only resetting the counter part.
Cc Tejun Heo <[email protected]>Cc Waiman Long <[email protected]>Cc Jay Shin <[email protected]>Fixes 3b8cc6298724 ("blk-cgroup: Optimize blkcg_rstat_flush()")Signed-off-by Ming Lei <[email protected]>--- block/blk-cgroup.c | 58 ++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 23 deletions(-)block/blk-cgroup.c +35
-16@@ -619,12 +619,45 @@ static void blkg_destroy_all(struct gendisk *disk)spin_unlock_irq (&q->queue_lock); } +static void blkg_iostat_set (struct blkg_iostat *dst,struct blkg_iostat *src) +{ +int i; + +for (i =0 ; i <BLKG_IOSTAT_NR ; i++) { + dst->bytes[i] = src->bytes[i]; + dst->ios[i] = src->ios[i]; + } +} + +static void __blkg_clear_stat (struct blkg_iostat_set *bis) +{ +struct blkg_iostat cur = {0 }; +unsigned long flags; + + flags =u64_stats_update_begin_irqsave (&bis->sync); +blkg_iostat_set (&bis->cur, &cur); +blkg_iostat_set (&bis->last, &cur); +u64_stats_update_end_irqrestore (&bis->sync, flags); +} + +static void blkg_clear_stat (struct blkcg_gq *blkg) +{ +int cpu; + +for_each_possible_cpu (cpu) { +struct blkg_iostat_set *s =per_cpu_ptr (blkg->iostat_cpu, cpu); + +__blkg_clear_stat (s); + } +__blkg_clear_stat (&blkg->iostat); +} +static int blkcg_reset_stats (struct cgroup_subsys_state *css,struct cftype *cftype,u64 val) {struct blkcg *blkcg =css_to_blkcg (css);struct blkcg_gq *blkg; -int i, cpu; +int i;mutex_lock (&blkcg_pol_mutex);spin_lock_irq (&blkcg->lock);@@ -635,18 +668,7 @@ static int blkcg_reset_stats(struct cgroup_subsys_state *css,* anyway. If you get hit by a race, retry. */ hlist_for_each_entry (blkg, &blkcg->blkg_list, blkcg_node) { -for_each_possible_cpu (cpu) { -struct blkg_iostat_set *bis = -per_cpu_ptr (blkg->iostat_cpu, cpu); -memset (bis,0 ,sizeof (*bis)); - -/* Re-initialize the cleared blkg_iostat_set */ -u64_stats_init (&bis->sync); - bis->blkg = blkg; - } -memset (&blkg->iostat,0 ,sizeof (blkg->iostat)); -u64_stats_init (&blkg->iostat.sync); - +blkg_clear_stat (blkg);for (i =0 ; i <BLKCG_MAX_POLS ; i++) {struct blkcg_policy *pol = blkcg_policy[i];@@ -949,16 +971,6 @@ void blkg_conf_exit(struct blkg_conf_ctx *ctx) }EXPORT_SYMBOL_GPL (blkg_conf_exit); -static void blkg_iostat_set (struct blkg_iostat *dst,struct blkg_iostat *src) -{ -int i;
block/blk-cgroup.c void blkg_conf_exit(struct blkg_conf_ctx *ctx) · line 976
Quoted 10 lines
block/blk-cgroup.c +0
-7- -for (i =0 ; i <BLKG_IOSTAT_NR ; i++) { - dst->bytes[i] = src->bytes[i]; - dst->ios[i] = src->ios[i]; - } -} -static void blkg_iostat_add (struct blkg_iostat *dst,struct blkg_iostat *src) {int i;
Re: [PATCH 1/2] blk-cgroup: fix list corruption from resetting io stat
In reply to Ming LeiQuoted 5 lines · Ming Lei
Since commit
3b8cc6298724("blk-cgroup: Optimizeblkcg_rstat_flush()"), each iostat instance is added to blkcg percpu list, soblkcg_reset_stats()can't reset the stat instance bymemset(), otherwise the llist may be corrupted.
Quoted 7 lines
Fix the issue by only resetting the counter part.
Cc Tejun Heo <[email protected]>Cc Waiman Long <[email protected]>Cc Jay Shin <[email protected]>Fixes 3b8cc6298724 ("blk-cgroup: Optimize blkcg_rstat_flush()")Signed-off-by Ming Lei <[email protected]>
Thanks.
Signature · 1 line
tejun
PATCH 2/2 blk-cgroup: fix list corruption from reorder of WRITE ->lqueued
In reply to Ming Lei__blkcg_rstat_flush() can be run anytime, especially when blk_cgroup_bio_start is being executed.
If WRITE of ->lqueued is re-ordered with READ of 'bisc->lnode.next' in the loop of __blkcg_rstat_flush(), next_bisc can be assigned with one stat instance being added in blk_cgroup_bio_start(), then the local list in __blkcg_rstat_flush() could be corrupted.
Fix the issue by adding one barrier.
--- block/blk-cgroup.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
block/blk-cgroup.c +10 -0
@@ -1036,6 +1036,16 @@ static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu)struct blkg_iostat cur;unsigned int seq; +/* +* Order assignment of `next_bisc` from `bisc->lnode.next` in +* llist_for_each_entry_safe and clearing `bisc->lqueued` for +* avoiding to assign `next_bisc` with new next pointer added +* in blk_cgroup_bio_start() in case of re-ordering. +* +* The pair barrier is implied in llist_add() in blk_cgroup_bio_start(). +*/ +smp_mb (); +WRITE_ONCE (bisc->lqueued, false);/* fetch the current per-cpu values */
Signature · 1 line
2.44.0
Re: [PATCH 2/2] blk-cgroup: fix list corruption from reorder of WRITE ->lqueued
Quoted 27 lines · Ming Lei
__blkcg_rstat_flush()can be run anytime, especially whenblk_cgroup_bio_startis being executed.If WRITE of
->lqueuedis re-ordered with READ of 'bisc->lnode.next' in the loop of__blkcg_rstat_flush(),next_bisccan be assigned with one stat instance being added inblk_cgroup_bio_start(), then the local list in__blkcg_rstat_flush()could be corrupted.Fix the issue by adding one barrier.
Cc Tejun Heo <[email protected]>Cc Waiman Long <[email protected]>Fixes 3b8cc6298724 ("blk-cgroup: Optimize blkcg_rstat_flush()")Signed-off-by Ming Lei <[email protected]>--- block/blk-cgroup.c | 10 ++++++++++ 1 file changed, 10 insertions(+)block/blk-cgroup.c +1
-0@@ -1036,6 +1036,16 @@ static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu)struct blkg_iostat cur;unsigned int seq; +/*
block/blk-cgroup.c static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu) · line 1049
Quoted 10 lines
block/blk-cgroup.c +9
-0+* Order assignment of `next_bisc` from `bisc->lnode.next` in +* llist_for_each_entry_safe and clearing `bisc->lqueued` for +* avoiding to assign `next_bisc` with new next pointer added +* in blk_cgroup_bio_start() in case of re-ordering. +* +* The pair barrier is implied in llist_add() in blk_cgroup_bio_start(). +*/ +smp_mb (); +WRITE_ONCE (bisc->lqueued, false);
I believe replacing WRITE_ONCE() by smp_store_release() and the READ_ONCE() in blk_cgroup_bio_start() by smp_load_acquire() should provide enough barrier to prevent unexpected reordering as the subsequent u64_stats_fetch_begin() will also provide a read barrier for subsequent read.
Cheers,
Longman
Quoted 2 lines
block/blk-cgroup.c +0
-0/* fetch the current per-cpu values */
Re: [PATCH 2/2] blk-cgroup: fix list corruption from reorder of WRITE ->lqueued
Quoted 34 lines · Waiman Long
Quoted 32 lines · Ming Lei
__blkcg_rstat_flush()can be run anytime, especially whenblk_cgroup_bio_startis being executed.If WRITE of
->lqueuedis re-ordered with READ of 'bisc->lnode.next' in the loop of__blkcg_rstat_flush(),next_bisccan be assigned with one stat instance being added inblk_cgroup_bio_start(), then the local list in__blkcg_rstat_flush()could be corrupted.Fix the issue by adding one barrier.
Cc Tejun Heo <[email protected]>Cc Waiman Long <[email protected]>Fixes 3b8cc6298724 ("blk-cgroup: Optimize blkcg_rstat_flush()")Signed-off-by Ming Lei <[email protected]>---
block/blk-cgroup.c| 10 ++++++++++1 file changed, 10 insertions(+)block/blk-cgroup.c +0
-0@@ -1036,6 +1036,16 @@ static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu) struct blkg_iostat cur; unsigned int seq;+ /*
+ * Order assignment ofnext_biscfrombisc->lnode.nextin + *llist_for_each_entry_safeand clearingbisc->lqueuedfor + * avoiding to assignnext_biscwith new next pointer added + * inblk_cgroup_bio_start()in case of re-ordering. + *
Quoted 8 lines
Quoted 6 lines
+ * The pair barrier is implied in
llist_add()inblk_cgroup_bio_start().
+ */
+smp_mb();
+
WRITE_ONCE(bisc->lqueued, false);I believe replacing
WRITE_ONCE()bysmp_store_release()and theREAD_ONCE()inblk_cgroup_bio_start()bysmp_load_acquire()should provide enough barrier to prevent unexpected reordering as the subsequentu64_stats_fetch_begin()will also provide a read barrier for subsequent read.
We can also use a smp_acquire__after_ctrl_dep() after the READ_ONCE() in blk_cgroup_bio_start() instead of a full smp_load_acquire() to optimize it a bit more.
Cheers,
Longman
Re: [PATCH 2/2] blk-cgroup: fix list corruption from reorder of WRITE ->lqueued
In reply to Waiman LongQuoted 34 lines · Waiman Long
Quoted 32 lines · Ming Lei
__blkcg_rstat_flush()can be run anytime, especially whenblk_cgroup_bio_startis being executed.If WRITE of
->lqueuedis re-ordered with READ of 'bisc->lnode.next' in the loop of__blkcg_rstat_flush(),next_bisccan be assigned with one stat instance being added inblk_cgroup_bio_start(), then the local list in__blkcg_rstat_flush()could be corrupted.Fix the issue by adding one barrier.
Cc Tejun Heo <[email protected]>Cc Waiman Long <[email protected]>Fixes 3b8cc6298724 ("blk-cgroup: Optimize blkcg_rstat_flush()")Signed-off-by Ming Lei <[email protected]>--- block/blk-cgroup.c | 10 ++++++++++ 1 file changed, 10 insertions(+)block/blk-cgroup.c +7
-0@@ -1036,6 +1036,16 @@ static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu)struct blkg_iostat cur;unsigned int seq; +/* +* Order assignment of `next_bisc` from `bisc->lnode.next` in +* llist_for_each_entry_safe and clearing `bisc->lqueued` for +* avoiding to assign `next_bisc` with new next pointer added +* in blk_cgroup_bio_start() in case of re-ordering. +* +* The pair barrier is implied in llist_add() in blk_cgroup_bio_start().
Quoted 8 lines
block/blk-cgroup.c static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu) · line 1049
Quoted 4 lines
block/blk-cgroup.c +3
-0+*/ +smp_mb (); +WRITE_ONCE (bisc->lqueued, false);I believe replacing
WRITE_ONCE()bysmp_store_release()and theREAD_ONCE()inblk_cgroup_bio_start()bysmp_load_acquire()should provide enough barrier to prevent unexpected reordering as
Yeah, smp_load_acquire() and smp_store_release() pair works too, but with one extra cost of smp_mb() around llist_add() which implies barrier already.
So I prefer to this patch.
Quoted 3 lines
the subsequent
u64_stats_fetch_begin()will also provide a read barrier for subsequent read.
u64_stats_fetch_begin() is nop in case of BITS_PER_LONG == 64.
Thanks,
Ming
Quoted 8 lines · Ming Lei
The 1st patch fixes list corruption when running
reset_iostat(cgroup v1).The 2nd patch fixes potential list corruption when reordering of writting to ->lqueued and reading from iostat instance.
[...]
Applied, thanks!
[1/2] blk-cgroup: fix list corruption from resetting io stat
commit: 6da6680632792709cecf2b006f2fe3ca7857e791
[2/2] blk-cgroup: fix list corruption from reorder of WRITE ->lqueued
commit: d0aac2363549e12cc79b8e285f13d5a9f42fd08eBest regards,
Signature · 1 line
Jens Axboe