Fix a race condition triggered by submit_bio()
Subsystems (2)
v2 cover Fix a race condition triggered by submit_bio()
Hi Jens,
One of the consequences of the switch to blk-mq is that generic_make_request() calls, a function called by submit_bio(), must be protected by a blk_queue_enter() / blk_queue_exit() pair to avoid that the block-cgroup functions called by generic_make_request() trigger a race condition. This patch series makes the kernel report a warning if that race condition is hit and also adds the necessary protection in submit_bio(). Please consider these patches for kernel v5.4.
Thanks,
Bart.
Changes compared to v1:
- Use the full syzbot ID in patch 2/2 instead of abbreviating it.
Bart Van Assche (2):
block: Verify whether blk_queue_enter() is used when necessary
block: Fix a race condition in submit_bio()
block/blk-cgroup.c | 2 ++ block/blk-core.c | 34 +++++++++++++++++++++++++++++++++- include/linux/blk-cgroup.h | 2 ++ include/linux/blkdev.h | 8 ++++++++ 4 files changed, 45 insertions(+), 1 deletion(-)
Signature · 1 line
2.22.0.709.g102302147b-goog
PATCH v2 1/2 block: Verify whether blk_queue_enter() is used when necessary
It is required to protect blkg_lookup() calls with a blk_queue_enter() / blk_queue_exit() pair. Since it is nontrivial to verify whether this is the case, verify this at runtime. Only perform this verification if CONFIG_LOCKDEP=y to avoid that unnecessary runtime overhead is added.
Note: using lock_acquire()/lock_release() to verify whether blkg_lookup() is protected correctly is not possible since lock_acquire() and lock_release() must be called from the same task and since blk_queue_enter() and blk_queue_exit() can be called from different tasks.
--- block/blk-cgroup.c | 2 ++ block/blk-core.c | 21 +++++++++++++++++++++ include/linux/blk-cgroup.h | 2 ++ include/linux/blkdev.h | 8 ++++++++ 4 files changed, 33 insertions(+)
block/blk-cgroup.c +2 -0
@@ -196,6 +196,8 @@ struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, {struct blkcg_gq *blkg; +WARN_ON_ONCE (!blk_entered_queue (q)); +/* * Hint didn't match. Look up from the radix tree. Note that the * hint can only be updated under queue_lock as otherwise @blkg
block/blk-core.c +21 -0
@@ -389,6 +389,25 @@ struct request_queue *blk_alloc_queue(gfp_t gfp_mask) }EXPORT_SYMBOL (blk_alloc_queue); +#ifdef CONFIG_PROVE_LOCKING +/** +* blk_entered_queue() - whether or not it is safe to access cgroup information +* @q: request queue pointer +* +* In order to avoid races between accessing cgroup information and the cgroup +* information removal from inside __blk_release_queue(), any code that accesses +* cgroup information must be protected by a blk_queue_enter()/blk_queue_exit() +* pair or must be called after queue cleanup progressed to a stage in which +* only the cleanup code accesses the queue. +*/ +bool blk_entered_queue (struct request_queue *q) +{ +return percpu_ref_is_dying (&q->q_usage_counter) || + !percpu_ref_is_zero (&q->q_usage_counter); +} +EXPORT_SYMBOL (blk_entered_queue); +#endif +/** * blk_queue_enter() - try to increase q->q_usage_counter * @q: request queue pointer
@@ -878,6 +897,8 @@ generic_make_request_checks(struct bio *bio) goto end_io; } +WARN_ON_ONCE (!blk_entered_queue (q)); +/* * For a REQ_NOWAIT based request, return -EOPNOTSUPP * if queue is not a request based queue.
include/linux/blk-cgroup.h +2 -0
@@ -358,6 +358,8 @@ static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, {struct blkcg_gq *blkg; +WARN_ON_ONCE (!blk_entered_queue (q)); +if (blkcg == &blkcg_root)return q->root_blkg;
include/linux/blkdev.h +8 -0
@@ -856,6 +856,14 @@ extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,extern int blk_queue_enter (struct request_queue *q,blk_mq_req_flags_t flags);extern void blk_queue_exit (struct request_queue *q); +#ifdef CONFIG_PROVE_LOCKING +extern bool blk_entered_queue (struct request_queue *q); +#else +static inline bool blk_entered_queue (struct request_queue *q) +{ +return true; +} +#endif extern void blk_sync_queue (struct request_queue *q); extern int blk_rq_map_user(struct request_queue *, struct request *, struct rq_map_data *, void __user *, unsigned long,
Signature · 1 line
2.22.0.709.g102302147b-goog
PATCH v2 2/2 block: Fix a race condition in submit_bio()
In reply to Bart Van Asschegeneric_make_request_checks() needs to be protected by a blk_queue_enter() / blk_queue_exit() pair because it calls blkcg_bio_issue_check() and because that last function calls blkg_lookup().
This patch fixes https://syzkaller.appspot.com/bug?id=ff9ab4a23afa7553fb79f745a92be87ba4144508.
This patch also fixes the following kernel warning, triggered by blktests:
WARNING: CPU: 5 PID: 10706 at block/blk-core.c:903 generic_make_request_checks+0x9c6/0xe60 RIP: 0010:generic_make_request_checks+0x9c6/0xe60 Call Trace: generic_make_request+0x7a/0x5c0 submit_bio+0x92/0x280 mpage_readpages+0x2b1/0x300 blkdev_readpages+0x1d/0x20 read_pages+0xd9/0x2c0 __do_page_cache_readahead+0x2e0/0x310 force_page_cache_readahead+0xfb/0x170 page_cache_sync_readahead+0x28d/0x2a0 generic_file_read_iter+0xc13/0x1530 blkdev_read_iter+0x7d/0x90 new_sync_read+0x2c5/0x3d0 __vfs_read+0x7b/0x90 vfs_read+0xc6/0x1f0 ksys_read+0xc3/0x160 __x64_sys_read+0x43/0x50 do_syscall_64+0x71/0x270 entry_SYSCALL_64_after_hwframe+0x49/0xbe
--- block/blk-core.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
block/blk-core.c +12 -1
@@ -1150,6 +1150,9 @@ EXPORT_SYMBOL_GPL(direct_make_request);*/ blk_qc_t submit_bio (struct bio *bio) { +struct request_queue *q = bio->bi_disk->queue; +blk_qc_t ret; +if (blkcg_punt_bio_submit (bio))return BLK_QC_T_NONE ;
@@ -1182,7 +1185,15 @@ blk_qc_t submit_bio(struct bio *bio) } } -return generic_make_request (bio); +if (unlikely (blk_queue_enter (q,0 ) <0 )) { + bio->bi_status =BLK_STS_IOERR ; + bio->bi_end_io (bio); +return BLK_QC_T_NONE ; + } + ret =generic_make_request (bio); +blk_queue_exit (q); + +return ret; }EXPORT_SYMBOL (submit_bio);
Signature · 1 line
2.22.0.709.g102302147b-goog