Patch "btrfs: Fix memory barriers usage with device stats counters" has been added to the 4.15-stable tree
Subsystems (2)
This is a note to let you know that I've just added the patch titled
btrfs: Fix memory barriers usage with device stats counters
to the 4.15-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
The filename of the patch is:
btrfs-fix-memory-barriers-usage-with-device-stats-counters.patch
and it can be found in the queue-4.15 subdirectory.
If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.
Quoted 1 line
From
9deae9689231964972a94bb56a79b669f9d47ac1Mon Sep 17 00:00:00 2001
From: Nikolay Borisov <[email protected]>
Date: Tue, 24 Oct 2017 13:47:37 +0300
Subject: btrfs: Fix memory barriers usage with device stats counters
From: Nikolay Borisov <[email protected]>
commit 9deae9689231964972a94bb56a79b669f9d47ac1 upstream.
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 | 18 ++++++++++++++++-- fs/btrfs/volumes.h | 12 ++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-)
fs/btrfs/volumes.c +16 -2
@@ -7093,10 +7093,24 @@ int btrfs_run_dev_stats(struct btrfs_tramutex_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(st 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(st 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); }
Patches currently in stable-queue which might be from [email protected] are
queue-4.15/btrfs-fix-memory-barriers-usage-with-device-stats-counters.patch queue-4.15/btrfs-fix-use-after-free-when-cleaning-up-fs_devs-with-a-single-stale-device.patch