Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

blk-throttle: fix access race during throttle policy activation

On repeated cold boots we occasionally hit a NULL pointer crash in
blk_should_throtl() when throttling is consulted before the throttle
policy is fully enabled for the queue. Checking only q->td != NULL is
insufficient during early initialization, so blkg_to_pd() for the
throttle policy can still return NULL and blkg_to_tg() becomes NULL,
which later gets dereferenced.

Unable to handle kernel NULL pointer dereference
at virtual address 0000000000000156
...
pc : submit_bio_noacct+0x14c/0x4c8
lr : submit_bio_noacct+0x48/0x4c8
sp : ffff800087f0b690
x29: ffff800087f0b690 x28: 0000000000005f90 x27: ffff00068af393c0
x26: 0000000000080000 x25: 000000000002fbc0 x24: ffff000684ddcc70
x23: 0000000000000000 x22: 0000000000000000 x21: 0000000000000000
x20: 0000000000080000 x19: ffff000684ddcd08 x18: ffffffffffffffff
x17: 0000000000000000 x16: ffff80008132a550 x15: 0000ffff98020fff
x14: 0000000000000000 x13: 1fffe000d11d7021 x12: ffff000688eb810c
x11: ffff00077ec4bb80 x10: ffff000688dcb720 x9 : ffff80008068ef60
x8 : 00000a6fb8a86e85 x7 : 000000000000111e x6 : 0000000000000002
x5 : 0000000000000246 x4 : 0000000000015cff x3 : 0000000000394500
x2 : ffff000682e35e40 x1 : 0000000000364940 x0 : 000000000000001a
Call trace:
submit_bio_noacct+0x14c/0x4c8
verity_map+0x178/0x2c8
__map_bio+0x228/0x250
dm_submit_bio+0x1c4/0x678
__submit_bio+0x170/0x230
submit_bio_noacct_nocheck+0x16c/0x388
submit_bio_noacct+0x16c/0x4c8
submit_bio+0xb4/0x210
f2fs_submit_read_bio+0x4c/0xf0
f2fs_mpage_readpages+0x3b0/0x5f0
f2fs_readahead+0x90/0xe8

Tighten blk_throtl_activated() to also require that the throttle policy
bit is set on the queue:

return q->td != NULL &&
test_bit(blkcg_policy_throtl.plid, q->blkcg_pols);

This prevents blk_should_throtl() from accessing throttle group state
until policy data has been attached to blkgs.

Fixes: a3166c51702b ("blk-throttle: delay initialization until configuration")
Co-developed-by: Liang Jie <liangjie@lixiang.com>
Signed-off-by: Liang Jie <liangjie@lixiang.com>
Signed-off-by: Han Guangjiang <hanguangjiang@lixiang.com>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Han Guangjiang and committed by
Jens Axboe
bd9fd5be 7942b226

+18 -18
-6
block/blk-cgroup.c
··· 110 110 return task_css(current, io_cgrp_id); 111 111 } 112 112 113 - static bool blkcg_policy_enabled(struct request_queue *q, 114 - const struct blkcg_policy *pol) 115 - { 116 - return pol && test_bit(pol->plid, q->blkcg_pols); 117 - } 118 - 119 113 static void blkg_free_workfn(struct work_struct *work) 120 114 { 121 115 struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
+6
block/blk-cgroup.h
··· 459 459 bio_issue_as_root_blkg(rq->bio) == bio_issue_as_root_blkg(bio); 460 460 } 461 461 462 + static inline bool blkcg_policy_enabled(struct request_queue *q, 463 + const struct blkcg_policy *pol) 464 + { 465 + return pol && test_bit(pol->plid, q->blkcg_pols); 466 + } 467 + 462 468 void blk_cgroup_bio_start(struct bio *bio); 463 469 void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); 464 470 #else /* CONFIG_BLK_CGROUP */
+1 -5
block/blk-throttle.c
··· 1327 1327 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn); 1328 1328 throtl_service_queue_init(&td->service_queue); 1329 1329 1330 - /* 1331 - * Freeze queue before activating policy, to synchronize with IO path, 1332 - * which is protected by 'q_usage_counter'. 1333 - */ 1334 1330 memflags = blk_mq_freeze_queue(disk->queue); 1335 1331 blk_mq_quiesce_queue(disk->queue); 1336 1332 1337 1333 q->td = td; 1338 1334 td->queue = q; 1339 1335 1340 - /* activate policy */ 1336 + /* activate policy, blk_throtl_activated() will return true */ 1341 1337 ret = blkcg_activate_policy(disk, &blkcg_policy_throtl); 1342 1338 if (ret) { 1343 1339 q->td = NULL;
+11 -7
block/blk-throttle.h
··· 156 156 157 157 static inline bool blk_throtl_activated(struct request_queue *q) 158 158 { 159 - return q->td != NULL; 159 + /* 160 + * q->td guarantees that the blk-throttle module is already loaded, 161 + * and the plid of blk-throttle is assigned. 162 + * blkcg_policy_enabled() guarantees that the policy is activated 163 + * in the request_queue. 164 + */ 165 + return q->td != NULL && blkcg_policy_enabled(q, &blkcg_policy_throtl); 160 166 } 161 167 162 168 static inline bool blk_should_throtl(struct bio *bio) ··· 170 164 struct throtl_grp *tg; 171 165 int rw = bio_data_dir(bio); 172 166 173 - /* 174 - * This is called under bio_queue_enter(), and it's synchronized with 175 - * the activation of blk-throtl, which is protected by 176 - * blk_mq_freeze_queue(). 177 - */ 178 167 if (!blk_throtl_activated(bio->bi_bdev->bd_queue)) 179 168 return false; 180 169 ··· 195 194 196 195 static inline bool blk_throtl_bio(struct bio *bio) 197 196 { 198 - 197 + /* 198 + * block throttling takes effect if the policy is activated 199 + * in the bio's request_queue. 200 + */ 199 201 if (!blk_should_throtl(bio)) 200 202 return false; 201 203