btrfs: Fix memory barriers usage with device stats counters
PATCH 1/3 btrfs: Fix memory barriers usage with device stats counters
Commit addc3fa74e5b ("Btrfs: Fix the problem that the dirty flag of dev stats is cleared") reworked the way device stats changes are tracked. A new atomic dev_stats_ccnt counter was introduced which is incremented every time any of the device stats counters are changed. This serves as a flag whether there are any pending stats changes. However, this patch only partially implemented the correct memory barriers necessary:
- It only ordered the stores to the counters but not the reads e.g. btrfs_run_dev_stats
- It completely omitted any comments documenting the intended design and how the memory barriers pair with each-other
This patch provides the necessary comments as well as adds a missing smp_rmb in btrfs_run_dev_stats. Furthermore since dev_stats_cnt is only a snapshot at best there was no point in reading the counter twice - once in btrfs_dev_stats_dirty and then again when assigning stats_cnt. Just collapse both reads into 1.
--- fs/btrfs/volumes.c | 16 ++++++++++++++-- fs/btrfs/volumes.h | 12 ++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-)
fs/btrfs/volumes.c +14 -2
@@ -7084,10 +7084,22 @@ int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,mutex_lock (&fs_devices->device_list_mutex);list_for_each_entry (device, &fs_devices->devices, dev_list) { -if (!device->dev_stats_valid || !btrfs_dev_stats_dirty (device)) + stats_cnt =atomic_read (&device->dev_stats_ccnt); +if (!device->dev_stats_valid || stats_cnt ==0 )continue ; - stats_cnt =atomic_read (&device->dev_stats_ccnt); + +/* +* There is a LOAD-LOAD control dependency between the value of +* dev_stats_ccnt and updating the on-disk values which requires +* reading the in-memory counters. Such control dependencies +* require explicit read memory barriers. +* +* This memory barriers pairs with smp_mb__before_atomic in +* btrfs_dev_stat_inc/btrfs_dev_stat_read_and_reset/btrfs_dev_stat_set +*/ +smp_rmb (); + ret =update_dev_stat_item (trans, fs_info, device);if (!ret)atomic_sub (stats_cnt, &device->dev_stats_ccnt);
fs/btrfs/volumes.h +12 -0
@@ -498,6 +498,12 @@ static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, int index) {atomic_inc (dev->dev_stat_values + index); +/* +* This memory barrier orders stores updating statistics before stores +* updating dev_stats_ccnt. +* +* It pairs with smp_rmb() in btrfs_run_dev_stats(). +*/ smp_mb__before_atomic ();atomic_inc (&dev->dev_stats_ccnt); }
@@ -523,6 +529,12 @@ static inline void btrfs_dev_stat_set(struct btrfs_device *dev, int index, unsigned long val) {atomic_set (dev->dev_stat_values + index, val); +/* +* This memory barrier orders stores updating statistics before stores +* updating dev_stats_ccnt. +* +* It pairs with smp_rmb() in btrfs_run_dev_stats(). +*/ smp_mb__before_atomic ();atomic_inc (&dev->dev_stats_ccnt); }
Signature · 1 line
2.7.4
PATCH 2/3 btrfs: Remove redundant memory barrier
As per atomic_t.txt documentation :
- RMW operations that have a return value are fully ordered;
atomic_xchg is one such operation so it already includes everything it needs w.r.t memory ordering and add a comment ot be more explicit about that.
--- fs/btrfs/volumes.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
fs/btrfs/volumes.h +7 -1
@@ -520,7 +520,13 @@ static inline int btrfs_dev_stat_read_and_reset(struct btrfs_device *dev,int ret; ret =atomic_xchg (dev->dev_stat_values + index,0 ); -smp_mb__before_atomic (); +/* +* atomic_xchg implies a full memory barriers as per atomic_t.txt: +* - RMW operations that have a return value are fully ordered; +* +* This implicit memory barriers is paired with the smp_rmb in +* btrfs_run_dev_stats +*/ atomic_inc (&dev->dev_stats_ccnt);return ret; }
Signature · 1 line
2.7.4
PATCH 3/3 btrfs: Remove unused function
In reply to Nikolay BorisovIt's sole callsite was removed in a previous patch so just nuke it for good.
--- fs/btrfs/volumes.h | 5 ----- 1 file changed, 5 deletions(-)
fs/btrfs/volumes.h +0 -5
@@ -489,11 +489,6 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,int btrfs_remove_chunk (struct btrfs_trans_handle *trans,struct btrfs_fs_info *fs_info,u64 chunk_offset); -static inline int btrfs_dev_stats_dirty (struct btrfs_device *dev) -{ -return atomic_read (&dev->dev_stats_ccnt); -} -static inline void btrfs_dev_stat_inc (struct btrfs_device *dev,int index) {
Signature · 1 line
2.7.4
Quoted 40 lines · ----- On Oct 20, 2017, at 11:10 AM, Nikolay Borisov [email protected] wrote:
Commit
addc3fa74e5b("Btrfs: Fix the problem that the dirty flag of dev stats is cleared")
reworked the way device stats changes are tracked. A new atomicdev_stats_ccntcounter was introduced which is incremented every time any of the device stats counters are changed. This serves as a flag whether there are any pending stats changes. However, this patch only partially implemented the correct memory barriers necessary:- It only ordered the stores to the counters but not the reads e.g.
btrfs_run_dev_stats
- It completely omitted any comments documenting the intended design and how the memory barriers pair with each-otherThis patch provides the necessary comments as well as adds a missing
smp_rmbinbtrfs_run_dev_stats. Furthermore sincedev_stats_cntis only a snapshot at best there was no point in reading the counter twice - once inbtrfs_dev_stats_dirtyand then again when assigningstats_cnt. Just collapse both reads into 1.Fixes addc3fa74e5b ("Btrfs: Fix the problem that the dirty flag of dev stats iscleared")
Signed-off-by Nikolay Borisov <[email protected]>---
fs/btrfs/volumes.c| 16 ++++++++++++++--fs/btrfs/volumes.h| 12 ++++++++++++2 files changed, 26 insertions(+), 2 deletions(-)fs/btrfs/volumes.c +2
-1@@ -7084,10 +7084,22 @@ int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,mutex_lock (&fs_devices->device_list_mutex);list_for_each_entry (device, &fs_devices->devices, dev_list) { -if (!device->dev_stats_valid || !btrfs_dev_stats_dirty (device)) + stats_cnt =atomic_read (&device->dev_stats_ccnt); +if (!device->dev_stats_valid || stats_cnt ==0 )continue ;
fs/btrfs/volumes.c int btrfs_run_dev_stats(struct btrfs_trans_handle · line 7100
Quoted 10 lines
fs/btrfs/volumes.c +9
-1- stats_cnt =atomic_read (&device->dev_stats_ccnt); + +/* +* There is a LOAD-LOAD control dependency between the value of +* dev_stats_ccnt and updating the on-disk values which requires +* reading the in-memory counters. Such control dependencies +* require explicit read memory barriers. +* +* This memory barriers pairs with smp_mb__before_atomic in +* btrfs_dev_stat_inc/btrfs_dev_stat_read_and_reset/btrfs_dev_stat_set
should be:
"This memory barriers pairs with smp_mb__before_atomic in btrfs_dev_stat_inc/btrfs_dev_stat_set, and with the full barrier implied by atomic_xchg in btrfs_dev_stat_read_and_reset"
Thanks,
Mathieu
Quoted 40 lines
fs/btrfs/volumes.c +3
-0+*/ +smp_rmb (); + ret =update_dev_stat_item (trans, fs_info, device);if (!ret)atomic_sub(stats_cnt, &device->dev_stats_ccnt);fs/btrfs/volumes.h +6
-0@@ -498,6 +498,12 @@ static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, int index) {atomic_inc (dev->dev_stat_values + index); +/* +* This memory barrier orders stores updating statistics before stores +* updating dev_stats_ccnt. +* +* It pairs with smp_rmb() in btrfs_run_dev_stats(). +*/ smp_mb__before_atomic ();atomic_inc (&dev->dev_stats_ccnt);}
fs/btrfs/volumes.h +6
-0@@ -523,6 +529,12 @@ static inline void btrfs_dev_stat_set(struct btrfs_device *dev, int index, unsigned long val) {atomic_set (dev->dev_stat_values + index, val); +/* +* This memory barrier orders stores updating statistics before stores +* updating dev_stats_ccnt. +* +* It pairs with smp_rmb() in btrfs_run_dev_stats(). +*/ smp_mb__before_atomic ();atomic_inc (&dev->dev_stats_ccnt);}
--
2.7.4
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
PATCH v2 1/1 btrfs: Fix memory barriers usage with device stats counters
In reply to Nikolay BorisovCommit addc3fa74e5b ("Btrfs: Fix the problem that the dirty flag of dev stats is cleared") reworked the way device stats changes are tracked. A new atomic dev_stats_ccnt counter was introduced which is incremented every time any of the device stats counters are changed. This serves as a flag whether there are any pending stats changes. However, this patch only partially implemented the correct memory barriers necessary:
- It only ordered the stores to the counters but not the reads e.g. btrfs_run_dev_stats
- It completely omitted any comments documenting the intended design and how the memory barriers pair with each-other
This patch provides the necessary comments as well as adds a missing smp_rmb in btrfs_run_dev_stats. Furthermore since dev_stats_cnt is only a snapshot at best there was no point in reading the counter twice - once in btrfs_dev_stats_dirty and then again when assigning stats_cnt. Just collapse both reads into 1.
---
V2:
Reworded the comment in btrfs_run_dev_stats according to Mathieu Desnoyers' feedback
fs/btrfs/volumes.c | 18 ++++++++++++++++-- fs/btrfs/volumes.h | 12 ++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-)
fs/btrfs/volumes.c +16 -2
@@ -7084,10 +7084,24 @@ int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,mutex_lock (&fs_devices->device_list_mutex);list_for_each_entry (device, &fs_devices->devices, dev_list) { -if (!device->dev_stats_valid || !btrfs_dev_stats_dirty (device)) + stats_cnt =atomic_read (&device->dev_stats_ccnt); +if (!device->dev_stats_valid || stats_cnt ==0 )continue ; - stats_cnt =atomic_read (&device->dev_stats_ccnt); + +/* +* There is a LOAD-LOAD control dependency between the value of +* dev_stats_ccnt and updating the on-disk values which requires +* reading the in-memory counters. Such control dependencies +* require explicit read memory barriers. +* +* This memory barriers pairs with smp_mb__before_atomic in +* btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full +* barrier implied by atomic_xchg in +* btrfs_dev_stats_read_and_reset +*/ +smp_rmb (); + ret =update_dev_stat_item (trans, fs_info, device);if (!ret)atomic_sub (stats_cnt, &device->dev_stats_ccnt);
fs/btrfs/volumes.h +12 -0
@@ -498,6 +498,12 @@ static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, int index) {atomic_inc (dev->dev_stat_values + index); +/* +* This memory barrier orders stores updating statistics before stores +* updating dev_stats_ccnt. +* +* It pairs with smp_rmb() in btrfs_run_dev_stats(). +*/ smp_mb__before_atomic ();atomic_inc (&dev->dev_stats_ccnt); }
@@ -523,6 +529,12 @@ static inline void btrfs_dev_stat_set(struct btrfs_device *dev, int index, unsigned long val) {atomic_set (dev->dev_stat_values + index, val); +/* +* This memory barrier orders stores updating statistics before stores +* updating dev_stats_ccnt. +* +* It pairs with smp_rmb() in btrfs_run_dev_stats(). +*/ smp_mb__before_atomic ();atomic_inc (&dev->dev_stats_ccnt); }
Signature · 1 line
2.7.4
Quoted 11 lines · ----- Nikolay Borisov
Commit
addc3fa74e5b("Btrfs: Fix the problem that the dirty flag of dev stats is cleared") reworked the way device stats changes are tracked. A new atomicdev_stats_ccntcounter was introduced which is incremented every time any of the device stats counters are changed. This serves as a flag whether there are any pending stats changes. However, this patch only partially implemented the correct memory barriers necessary:- It only ordered the stores to the counters but not the reads e.g.
btrfs_run_dev_stats
- It completely omitted any comments documenting the intended design and how the memory barriers pair with each-other
Quoted 7 lines
This patch provides the necessary comments as well as adds a missing
smp_rmbinbtrfs_run_dev_stats. Furthermore sincedev_stats_cntis only a snapshot at best there was no point in reading the counter twice - once inbtrfs_dev_stats_dirtyand then again when assigningstats_cnt. Just collapse both reads into 1.Fixes addc3fa74e5b ("Btrfs: Fix the problem that the dirty flag of dev stats is cleared")Signed-off-by Nikolay Borisov <[email protected]>
Quoted 74 lines
---
V2:
Reworded the comment inbtrfs_run_dev_statsaccording to Mathieu Desnoyers' feedbackfs/btrfs/volumes.c | 18 ++++++++++++++++-- fs/btrfs/volumes.h | 12 ++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-)fs/btrfs/volumes.c +16
-2@@ -7084,10 +7084,24 @@ int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,mutex_lock (&fs_devices->device_list_mutex);list_for_each_entry (device, &fs_devices->devices, dev_list) { -if (!device->dev_stats_valid || !btrfs_dev_stats_dirty (device)) + stats_cnt =atomic_read (&device->dev_stats_ccnt); +if (!device->dev_stats_valid || stats_cnt ==0 )continue ; - stats_cnt =atomic_read (&device->dev_stats_ccnt); + +/* +* There is a LOAD-LOAD control dependency between the value of +* dev_stats_ccnt and updating the on-disk values which requires +* reading the in-memory counters. Such control dependencies +* require explicit read memory barriers. +* +* This memory barriers pairs with smp_mb__before_atomic in +* btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full +* barrier implied by atomic_xchg in +* btrfs_dev_stats_read_and_reset +*/ +smp_rmb (); + ret =update_dev_stat_item (trans, fs_info, device);if (!ret)atomic_sub (stats_cnt, &device->dev_stats_ccnt);fs/btrfs/volumes.h +12
-0@@ -498,6 +498,12 @@ static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, int index) {atomic_inc (dev->dev_stat_values + index); +/* +* This memory barrier orders stores updating statistics before stores +* updating dev_stats_ccnt. +* +* It pairs with smp_rmb() in btrfs_run_dev_stats(). +*/ smp_mb__before_atomic ();atomic_inc (&dev->dev_stats_ccnt); }@@ -523,6 +529,12 @@ static inline void btrfs_dev_stat_set(struct btrfs_device *dev, int index, unsigned long val) {atomic_set (dev->dev_stat_values + index, val); +/* +* This memory barrier orders stores updating statistics before stores +* updating dev_stats_ccnt. +* +* It pairs with smp_rmb() in btrfs_run_dev_stats(). +*/ smp_mb__before_atomic ();atomic_inc (&dev->dev_stats_ccnt); }Signature · 1 line
2.7.4
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [PATCH 2/3] btrfs: Remove redundant memory barrier
In reply to Nikolay BorisovQuoted 7 lines · Nikolay Borisov
As per atomic_t.txt documentation :
- RMW operations that have a return value are fully ordered;
atomic_xchgis one such operation so it already includes everything it needs w.r.t memory ordering and add a comment ot be more explicit about that.Signed-off-by Nikolay Borisov <[email protected]>