Skip to content

Commit 9dd1cd3

Browse files
author
Mike Snitzer
committed
dm: fix dm-raid crash if md_handle_request() splits bio
Commit ca52248 ("dm: pass NULL bdev to bio_alloc_clone") introduced the optimization to _not_ perform bio_associate_blkg()'s relatively costly work when DM core clones its bio. But in doing so it exposed the possibility for DM's cloned bio to alter DM target behavior (e.g. crash) if a target were to issue IO without first calling bio_set_dev(). The DM raid target can trigger an MD crash due to its need to split the DM bio that is passed to md_handle_request(). The split will recurse to submit_bio_noacct() using a bio with an uninitialized ->bi_blkg. This NULL bio->bi_blkg causes blk_throtl_bio() to dereference a NULL blkg_to_tg(bio->bi_blkg). Fix this in DM core by adding a new 'needs_bio_set_dev' target flag that will make alloc_tio() call bio_set_dev() on behalf of the target. dm-raid is the only target that requires this flag. bio_set_dev() initializes the DM cloned bio's ->bi_blkg, using bio_associate_blkg, before passing the bio to md_handle_request(). Long-term fix would be to audit and refactor MD code to rely on DM to split its bio, using dm_accept_partial_bio(), but there are MD raid personalities (e.g. raid1 and raid10) whose implementation are tightly coupled to handling the bio splitting inline. Fixes: ca52248 ("dm: pass NULL bdev to bio_alloc_clone") Cc: [email protected] Signed-off-by: Mike Snitzer <[email protected]>
1 parent 7dad24d commit 9dd1cd3

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

drivers/md/dm-raid.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,6 +3095,7 @@ static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv)
30953095
INIT_WORK(&rs->md.event_work, do_table_event);
30963096
ti->private = rs;
30973097
ti->num_flush_bios = 1;
3098+
ti->needs_bio_set_dev = true;
30983099

30993100
/* Restore any requested new layout for conversion decision */
31003101
rs_config_restore(rs, &rs_layout);

drivers/md/dm.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,6 @@ static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
574574
struct bio *clone;
575575

576576
clone = bio_alloc_clone(NULL, bio, GFP_NOIO, &md->mempools->io_bs);
577-
/* Set default bdev, but target must bio_set_dev() before issuing IO */
578-
clone->bi_bdev = md->disk->part0;
579-
580577
tio = clone_to_tio(clone);
581578
tio->flags = 0;
582579
dm_tio_set_flag(tio, DM_TIO_INSIDE_DM_IO);
@@ -609,6 +606,7 @@ static void free_io(struct dm_io *io)
609606
static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti,
610607
unsigned target_bio_nr, unsigned *len, gfp_t gfp_mask)
611608
{
609+
struct mapped_device *md = ci->io->md;
612610
struct dm_target_io *tio;
613611
struct bio *clone;
614612

@@ -618,14 +616,10 @@ static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti,
618616
/* alloc_io() already initialized embedded clone */
619617
clone = &tio->clone;
620618
} else {
621-
struct mapped_device *md = ci->io->md;
622-
623619
clone = bio_alloc_clone(NULL, ci->bio, gfp_mask,
624620
&md->mempools->bs);
625621
if (!clone)
626622
return NULL;
627-
/* Set default bdev, but target must bio_set_dev() before issuing IO */
628-
clone->bi_bdev = md->disk->part0;
629623

630624
/* REQ_DM_POLL_LIST shouldn't be inherited */
631625
clone->bi_opf &= ~REQ_DM_POLL_LIST;
@@ -641,6 +635,11 @@ static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti,
641635
tio->len_ptr = len;
642636
tio->old_sector = 0;
643637

638+
/* Set default bdev, but target must bio_set_dev() before issuing IO */
639+
clone->bi_bdev = md->disk->part0;
640+
if (unlikely(ti->needs_bio_set_dev))
641+
bio_set_dev(clone, md->disk->part0);
642+
644643
if (len) {
645644
clone->bi_iter.bi_size = to_bytes(*len);
646645
if (bio_integrity(clone))

include/linux/device-mapper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ struct dm_target {
373373
* after returning DM_MAPIO_SUBMITTED from its map function.
374374
*/
375375
bool accounts_remapped_io:1;
376+
377+
/*
378+
* Set if the target will submit the DM bio without first calling
379+
* bio_set_dev(). NOTE: ideally a target should _not_ need this.
380+
*/
381+
bool needs_bio_set_dev:1;
376382
};
377383

378384
void *dm_per_bio_data(struct bio *bio, size_t data_size);

include/uapi/linux/dm-ioctl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ enum {
286286
#define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, DM_DEV_SET_GEOMETRY_CMD, struct dm_ioctl)
287287

288288
#define DM_VERSION_MAJOR 4
289-
#define DM_VERSION_MINOR 46
289+
#define DM_VERSION_MINOR 47
290290
#define DM_VERSION_PATCHLEVEL 0
291-
#define DM_VERSION_EXTRA "-ioctl (2022-02-22)"
291+
#define DM_VERSION_EXTRA "-ioctl (2022-07-28)"
292292

293293
/* Status bits */
294294
#define DM_READONLY_FLAG (1 << 0) /* In/Out */

0 commit comments

Comments
 (0)