Skip to contentSkip to conversation

blk-wbt: Fix data race and avoid possible false sharing

PATCH In review Marco Elver 2 participants 4 messages Nov 5, 2019 – Nov 23 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 blk-wbt: Fix data race and avoid possible false sharing

The pattern here is to avoid possible false sharing. However, due to compiler optimizations the code may simply collapse to the write if we omit READ_ONCE/WRITE_ONCE:
https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE#it-may-improve-performance

==================================================================

BUG: KCSAN: data-race in wbt_wait / wbt_wait

read to 0xffff88821aa6d140 of 8 bytes by task 10372 on cpu 1:

 wb_timestamp block/blk-wbt.c:88 [inline]
 wb_timestamp block/blk-wbt.c:83 [inline]
 wbt_wait+0x1f9/0x250 block/blk-wbt.c:587
 __rq_qos_throttle+0x47/0x70 block/blk-rq-qos.c:72
 rq_qos_throttle block/blk-rq-qos.h:185 [inline]
 blk_mq_make_request+0x29c/0xf60 block/blk-mq.c:1971
 generic_make_request block/blk-core.c:1064 [inline]
 generic_make_request+0x196/0x740 block/blk-core.c:1006
 submit_bio+0x96/0x3c0 block/blk-core.c:1190
 submit_bh_wbc+0x40f/0x460 fs/buffer.c:3095
 submit_bh fs/buffer.c:3101 [inline]
 __bread_slow fs/buffer.c:1177 [inline]
 __bread_gfp+0xe7/0x1e0 fs/buffer.c:1359
 sb_bread include/linux/buffer_head.h:307 [inline]
 fat__get_entry+0x35e/0x4f0 fs/fat/dir.c:100
 fat_get_entry fs/fat/dir.c:128 [inline]
 fat_get_short_entry+0x103/0x200 fs/fat/dir.c:877
 fat_subdirs+0x6b/0x110 fs/fat/dir.c:943
 fat_read_root fs/fat/inode.c:1416 [inline]
 fat_fill_super+0x1552/0x1f50 fs/fat/inode.c:1862
 vfat_fill_super+0x3b/0x50 fs/fat/namei_vfat.c:1050
 mount_bdev+0x262/0x2d0 fs/super.c:1415
 vfat_mount+0x3e/0x60 fs/fat/namei_vfat.c:1057

write to 0xffff88821aa6d140 of 8 bytes by task 10375 on cpu 0:

 wb_timestamp block/blk-wbt.c:89 [inline]
 wb_timestamp block/blk-wbt.c:83 [inline]
 wbt_wait+0x21e/0x250 block/blk-wbt.c:587
 __rq_qos_throttle+0x47/0x70 block/blk-rq-qos.c:72
 rq_qos_throttle block/blk-rq-qos.h:185 [inline]
 blk_mq_make_request+0x29c/0xf60 block/blk-mq.c:1971
 generic_make_request block/blk-core.c:1064 [inline]
 generic_make_request+0x196/0x740 block/blk-core.c:1006
 submit_bio+0x96/0x3c0 block/blk-core.c:1190
 mpage_bio_submit fs/mpage.c:66 [inline]
 mpage_readpages+0x36c/0x3c0 fs/mpage.c:410
 blkdev_readpages+0x36/0x50 fs/block_dev.c:620
 read_pages+0xa2/0x2d0 mm/readahead.c:126
 __do_page_cache_readahead+0x353/0x390 mm/readahead.c:212
 force_page_cache_readahead+0x13a/0x1f0 mm/readahead.c:243
 page_cache_sync_readahead+0x1cf/0x1e0 mm/readahead.c:522
 generic_file_buffered_read mm/filemap.c:2050 [inline]
 generic_file_read_iter+0xeb6/0x1440 mm/filemap.c:2323
 blkdev_read_iter+0xb2/0xe0 fs/block_dev.c:2010

Reported by Kernel Concurrency Sanitizer on:

CPU: 0 PID: 10375 Comm: blkid Not tainted 5.4.0-rc3+ #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
==================================================================
---
 block/blk-wbt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/blk-wbt.c +2 -2
index 8641ba9793c5..ce281a9007a6 100644
@@ -85,8 +85,8 @@ static void wb_timestamp(struct rq_wb *rwb, unsigned long *var)
 	if (rwb_enabled(rwb)) {
 		const unsigned long cur = jiffies;
 
-		if (cur != *var)
-			*var = cur;
+		if (cur != READ_ONCE(*var))
+			WRITE_ONCE(*var, cur);
 	}
 }
 
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
#1 lore raw

While looking at Bugzilla reports I noticed that the fix I would have written for https://bugzilla.kernel.org/show_bug.cgi?id=220759, is the same as the fix Marco Elver proposed,
https://lore.kernel.org/all/[email protected]/
long before the bugzilla report. I could not find public comments or an accepted patch.

I tested the patch applied to Axboe's Tree with KCSAN and a mixed read/write workload on null_blk. The patch fixed the KCSAN reports, in my test runs.

Was there a reason this did not get applied?
If there needs to be testing done or something else I would be happy to do it.

Best regards,
Jenson

#2 lore raw
Quoted 9 lines · Jenson Johnathon Reggentin

While looking at Bugzilla reports I noticed that the fix I would have written for https://bugzilla.kernel.org/show_bug.cgi?id=220759, is the same as the fix Marco Elver proposed,
https://lore.kernel.org/all/[email protected]/
long before the bugzilla report. I could not find public comments or an accepted patch.

Quoted 5 lines

I tested the patch applied to Axboe's Tree with KCSAN and a mixed read/write workload on null_blk. The patch fixed the KCSAN reports, in my test runs.

Was there a reason this did not get applied?

Probably was just forgotten or it wasn't serious enough.

Quoted 2 lines

If there needs to be testing done or something else I would be happy to do it.

While data races can cause all sorts of problems, or are the symptom of a deeper problem, the kernel has historically been more lax in tolerating "benign" data races; the C language says they cause UB, the kernel's own memory model (LKMM) is more relaxed about data races:
https://docs.kernel.org/dev-tools/lkmm/docs/access-marking.html [1]

While we should be marking these accesses per [1] all over the kernel, doing so blindly would paper over more serious race-condition bugs, or cases where we need proper *ONCE/atomic_t use. In many cases, nobody has had the time to investigate.

For the "wbt_wait / wbt_wait" case, I think marking with READ_ONCE/WRITE_ONCE is appropriate, otherwise the compiler may optimize away the load-compare.

Thanks,
-- Marco

#3 lore raw

PATCH v2 1/1 blk-wbt: Fix data race and avoid possible false sharing

From: Marco Elver <[email protected]>

The pattern here is to avoid possible false sharing. However, due to compiler optimizations the code may simply collapse to the write if we omit READ_ONCE/WRITE_ONCE:
https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE#it-may-improve-performance

---
v2: Pick up Marco Elver's 2019 patch with updated testing and an updated Fixes Tag.

Tested with KCSAN, HZ=1000 and null_blk in a 4-vCPU, 2-GiB virtme-ng VM. The 30 second fio workload used two direct 4-KiB random readers and one buffered 64-KiB writer.

The baseline produced two wbt_wait/wbt_wait and three wbt_done/wbt_done reports.
Neither signature was observed with this patch. All fio jobs completed without errors.

I also did an AI Code Review, it really wanted to add READ_ONCE instructions for the reads inside the close_io function. These changes did not impact any of the KCSAN reports, in my testing, though.
So I consider them outside of the scope, but I thought I'd mention it for completeness sake.

 block/blk-wbt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/blk-wbt.c +2 -2
index 953d400fd013..a60df3524b98 100644
@@ -156,8 +156,8 @@ static void wb_timestamp(struct rq_wb *rwb, unsigned long *var)
 	if (rwb_enabled(rwb)) {
 		const unsigned long cur = jiffies;
 
-		if (cur != *var)
-			*var = cur;
+		if (cur != READ_ONCE(*var))
+			WRITE_ONCE(*var, cur);
 	}
 }
 

base-commit: 77cc1587b17a356f1638f09ade33872a919a82ab