Skip to content

Commit ce673f6

Browse files
committed
Merge tag 'for-5.18/dm-fixes-2' of git://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm
Pull device mapper fixes from Mike Snitzer: - Fix memory corruption in DM integrity target when tag_size is less than digest size. - Fix DM multipath's historical-service-time path selector to not use sched_clock() and ktime_get_ns(); only use ktime_get_ns(). - Fix dm_io->orig_bio NULL pointer dereference in dm_zone_map_bio() due to 5.18 changes that overlooked DM zone's use of ->orig_bio - Fix for regression that broke the use of dm_accept_partial_bio() for "abnormal" IO (e.g. WRITE ZEROES) that does not need duplicate bios - Fix DM's issuing of empty flush bio so that it's size is 0. * tag 'for-5.18/dm-fixes-2' of git://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm: dm: fix bio length of empty flush dm: allow dm_accept_partial_bio() for dm_io without duplicate bios dm zone: fix NULL pointer dereference in dm_zone_map_bio dm mpath: only use ktime_get_ns() in historical selector dm integrity: fix memory corruption when tag_size is less than digest size
2 parents fb649bd + 92b914e commit ce673f6

4 files changed

Lines changed: 45 additions & 40 deletions

File tree

drivers/md/dm-integrity.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4399,6 +4399,7 @@ static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
43994399
}
44004400

44014401
if (ic->internal_hash) {
4402+
size_t recalc_tags_size;
44024403
ic->recalc_wq = alloc_workqueue("dm-integrity-recalc", WQ_MEM_RECLAIM, 1);
44034404
if (!ic->recalc_wq ) {
44044405
ti->error = "Cannot allocate workqueue";
@@ -4412,8 +4413,10 @@ static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
44124413
r = -ENOMEM;
44134414
goto bad;
44144415
}
4415-
ic->recalc_tags = kvmalloc_array(RECALC_SECTORS >> ic->sb->log2_sectors_per_block,
4416-
ic->tag_size, GFP_KERNEL);
4416+
recalc_tags_size = (RECALC_SECTORS >> ic->sb->log2_sectors_per_block) * ic->tag_size;
4417+
if (crypto_shash_digestsize(ic->internal_hash) > ic->tag_size)
4418+
recalc_tags_size += crypto_shash_digestsize(ic->internal_hash) - ic->tag_size;
4419+
ic->recalc_tags = kvmalloc(recalc_tags_size, GFP_KERNEL);
44174420
if (!ic->recalc_tags) {
44184421
ti->error = "Cannot allocate tags for recalculating";
44194422
r = -ENOMEM;

drivers/md/dm-ps-historical-service-time.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <linux/blkdev.h>
2828
#include <linux/slab.h>
2929
#include <linux/module.h>
30-
#include <linux/sched/clock.h>
3130

3231

3332
#define DM_MSG_PREFIX "multipath historical-service-time"
@@ -433,7 +432,7 @@ static struct dm_path *hst_select_path(struct path_selector *ps,
433432
{
434433
struct selector *s = ps->context;
435434
struct path_info *pi = NULL, *best = NULL;
436-
u64 time_now = sched_clock();
435+
u64 time_now = ktime_get_ns();
437436
struct dm_path *ret = NULL;
438437
unsigned long flags;
439438

@@ -474,7 +473,7 @@ static int hst_start_io(struct path_selector *ps, struct dm_path *path,
474473

475474
static u64 path_service_time(struct path_info *pi, u64 start_time)
476475
{
477-
u64 sched_now = ktime_get_ns();
476+
u64 now = ktime_get_ns();
478477

479478
/* if a previous disk request has finished after this IO was
480479
* sent to the hardware, pretend the submission happened
@@ -483,11 +482,11 @@ static u64 path_service_time(struct path_info *pi, u64 start_time)
483482
if (time_after64(pi->last_finish, start_time))
484483
start_time = pi->last_finish;
485484

486-
pi->last_finish = sched_now;
487-
if (time_before64(sched_now, start_time))
485+
pi->last_finish = now;
486+
if (time_before64(now, start_time))
488487
return 0;
489488

490-
return sched_now - start_time;
489+
return now - start_time;
491490
}
492491

493492
static int hst_end_io(struct path_selector *ps, struct dm_path *path,

drivers/md/dm-zone.c

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,20 @@ static int dm_update_zone_wp_offset(struct mapped_device *md, unsigned int zno,
360360
return 0;
361361
}
362362

363+
struct orig_bio_details {
364+
unsigned int op;
365+
unsigned int nr_sectors;
366+
};
367+
363368
/*
364369
* First phase of BIO mapping for targets with zone append emulation:
365370
* check all BIO that change a zone writer pointer and change zone
366371
* append operations into regular write operations.
367372
*/
368373
static bool dm_zone_map_bio_begin(struct mapped_device *md,
369-
struct bio *orig_bio, struct bio *clone)
374+
unsigned int zno, struct bio *clone)
370375
{
371376
sector_t zsectors = blk_queue_zone_sectors(md->queue);
372-
unsigned int zno = bio_zone_no(orig_bio);
373377
unsigned int zwp_offset = READ_ONCE(md->zwp_offset[zno]);
374378

375379
/*
@@ -384,7 +388,7 @@ static bool dm_zone_map_bio_begin(struct mapped_device *md,
384388
WRITE_ONCE(md->zwp_offset[zno], zwp_offset);
385389
}
386390

387-
switch (bio_op(orig_bio)) {
391+
switch (bio_op(clone)) {
388392
case REQ_OP_ZONE_RESET:
389393
case REQ_OP_ZONE_FINISH:
390394
return true;
@@ -401,9 +405,8 @@ static bool dm_zone_map_bio_begin(struct mapped_device *md,
401405
* target zone.
402406
*/
403407
clone->bi_opf = REQ_OP_WRITE | REQ_NOMERGE |
404-
(orig_bio->bi_opf & (~REQ_OP_MASK));
405-
clone->bi_iter.bi_sector =
406-
orig_bio->bi_iter.bi_sector + zwp_offset;
408+
(clone->bi_opf & (~REQ_OP_MASK));
409+
clone->bi_iter.bi_sector += zwp_offset;
407410
break;
408411
default:
409412
DMWARN_LIMIT("Invalid BIO operation");
@@ -423,19 +426,18 @@ static bool dm_zone_map_bio_begin(struct mapped_device *md,
423426
* data written to a zone. Note that at this point, the remapped clone BIO
424427
* may already have completed, so we do not touch it.
425428
*/
426-
static blk_status_t dm_zone_map_bio_end(struct mapped_device *md,
427-
struct bio *orig_bio,
429+
static blk_status_t dm_zone_map_bio_end(struct mapped_device *md, unsigned int zno,
430+
struct orig_bio_details *orig_bio_details,
428431
unsigned int nr_sectors)
429432
{
430-
unsigned int zno = bio_zone_no(orig_bio);
431433
unsigned int zwp_offset = READ_ONCE(md->zwp_offset[zno]);
432434

433435
/* The clone BIO may already have been completed and failed */
434436
if (zwp_offset == DM_ZONE_INVALID_WP_OFST)
435437
return BLK_STS_IOERR;
436438

437439
/* Update the zone wp offset */
438-
switch (bio_op(orig_bio)) {
440+
switch (orig_bio_details->op) {
439441
case REQ_OP_ZONE_RESET:
440442
WRITE_ONCE(md->zwp_offset[zno], 0);
441443
return BLK_STS_OK;
@@ -452,7 +454,7 @@ static blk_status_t dm_zone_map_bio_end(struct mapped_device *md,
452454
* Check that the target did not truncate the write operation
453455
* emulating a zone append.
454456
*/
455-
if (nr_sectors != bio_sectors(orig_bio)) {
457+
if (nr_sectors != orig_bio_details->nr_sectors) {
456458
DMWARN_LIMIT("Truncated write for zone append");
457459
return BLK_STS_IOERR;
458460
}
@@ -488,23 +490,23 @@ static inline void dm_zone_unlock(struct request_queue *q,
488490
bio_clear_flag(clone, BIO_ZONE_WRITE_LOCKED);
489491
}
490492

491-
static bool dm_need_zone_wp_tracking(struct bio *orig_bio)
493+
static bool dm_need_zone_wp_tracking(struct bio *bio)
492494
{
493495
/*
494496
* Special processing is not needed for operations that do not need the
495497
* zone write lock, that is, all operations that target conventional
496498
* zones and all operations that do not modify directly a sequential
497499
* zone write pointer.
498500
*/
499-
if (op_is_flush(orig_bio->bi_opf) && !bio_sectors(orig_bio))
501+
if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
500502
return false;
501-
switch (bio_op(orig_bio)) {
503+
switch (bio_op(bio)) {
502504
case REQ_OP_WRITE_ZEROES:
503505
case REQ_OP_WRITE:
504506
case REQ_OP_ZONE_RESET:
505507
case REQ_OP_ZONE_FINISH:
506508
case REQ_OP_ZONE_APPEND:
507-
return bio_zone_is_seq(orig_bio);
509+
return bio_zone_is_seq(bio);
508510
default:
509511
return false;
510512
}
@@ -519,8 +521,8 @@ int dm_zone_map_bio(struct dm_target_io *tio)
519521
struct dm_target *ti = tio->ti;
520522
struct mapped_device *md = io->md;
521523
struct request_queue *q = md->queue;
522-
struct bio *orig_bio = io->orig_bio;
523524
struct bio *clone = &tio->clone;
525+
struct orig_bio_details orig_bio_details;
524526
unsigned int zno;
525527
blk_status_t sts;
526528
int r;
@@ -529,18 +531,21 @@ int dm_zone_map_bio(struct dm_target_io *tio)
529531
* IOs that do not change a zone write pointer do not need
530532
* any additional special processing.
531533
*/
532-
if (!dm_need_zone_wp_tracking(orig_bio))
534+
if (!dm_need_zone_wp_tracking(clone))
533535
return ti->type->map(ti, clone);
534536

535537
/* Lock the target zone */
536-
zno = bio_zone_no(orig_bio);
538+
zno = bio_zone_no(clone);
537539
dm_zone_lock(q, zno, clone);
538540

541+
orig_bio_details.nr_sectors = bio_sectors(clone);
542+
orig_bio_details.op = bio_op(clone);
543+
539544
/*
540545
* Check that the bio and the target zone write pointer offset are
541546
* both valid, and if the bio is a zone append, remap it to a write.
542547
*/
543-
if (!dm_zone_map_bio_begin(md, orig_bio, clone)) {
548+
if (!dm_zone_map_bio_begin(md, zno, clone)) {
544549
dm_zone_unlock(q, zno, clone);
545550
return DM_MAPIO_KILL;
546551
}
@@ -560,15 +565,17 @@ int dm_zone_map_bio(struct dm_target_io *tio)
560565
* The target submitted the clone BIO. The target zone will
561566
* be unlocked on completion of the clone.
562567
*/
563-
sts = dm_zone_map_bio_end(md, orig_bio, *tio->len_ptr);
568+
sts = dm_zone_map_bio_end(md, zno, &orig_bio_details,
569+
*tio->len_ptr);
564570
break;
565571
case DM_MAPIO_REMAPPED:
566572
/*
567573
* The target only remapped the clone BIO. In case of error,
568574
* unlock the target zone here as the clone will not be
569575
* submitted.
570576
*/
571-
sts = dm_zone_map_bio_end(md, orig_bio, *tio->len_ptr);
577+
sts = dm_zone_map_bio_end(md, zno, &orig_bio_details,
578+
*tio->len_ptr);
572579
if (sts != BLK_STS_OK)
573580
dm_zone_unlock(q, zno, clone);
574581
break;

drivers/md/dm.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,8 +1323,7 @@ static void __map_bio(struct bio *clone)
13231323
}
13241324

13251325
static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1326-
struct dm_target *ti, unsigned num_bios,
1327-
unsigned *len)
1326+
struct dm_target *ti, unsigned num_bios)
13281327
{
13291328
struct bio *bio;
13301329
int try;
@@ -1335,7 +1334,7 @@ static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
13351334
if (try)
13361335
mutex_lock(&ci->io->md->table_devices_lock);
13371336
for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1338-
bio = alloc_tio(ci, ti, bio_nr, len,
1337+
bio = alloc_tio(ci, ti, bio_nr, NULL,
13391338
try ? GFP_NOIO : GFP_NOWAIT);
13401339
if (!bio)
13411340
break;
@@ -1363,11 +1362,11 @@ static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
13631362
break;
13641363
case 1:
13651364
clone = alloc_tio(ci, ti, 0, len, GFP_NOIO);
1366-
dm_tio_set_flag(clone_to_tio(clone), DM_TIO_IS_DUPLICATE_BIO);
13671365
__map_bio(clone);
13681366
break;
13691367
default:
1370-
alloc_multiple_bios(&blist, ci, ti, num_bios, len);
1368+
/* dm_accept_partial_bio() is not supported with shared tio->len_ptr */
1369+
alloc_multiple_bios(&blist, ci, ti, num_bios);
13711370
while ((clone = bio_list_pop(&blist))) {
13721371
dm_tio_set_flag(clone_to_tio(clone), DM_TIO_IS_DUPLICATE_BIO);
13731372
__map_bio(clone);
@@ -1392,6 +1391,7 @@ static void __send_empty_flush(struct clone_info *ci)
13921391

13931392
ci->bio = &flush_bio;
13941393
ci->sector_count = 0;
1394+
ci->io->tio.clone.bi_iter.bi_size = 0;
13951395

13961396
while ((ti = dm_table_get_target(ci->map, target_nr++)))
13971397
__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
@@ -1407,14 +1407,10 @@ static void __send_changing_extent_only(struct clone_info *ci, struct dm_target
14071407
len = min_t(sector_t, ci->sector_count,
14081408
max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector)));
14091409

1410-
/*
1411-
* dm_accept_partial_bio cannot be used with duplicate bios,
1412-
* so update clone_info cursor before __send_duplicate_bios().
1413-
*/
1410+
__send_duplicate_bios(ci, ti, num_bios, &len);
1411+
14141412
ci->sector += len;
14151413
ci->sector_count -= len;
1416-
1417-
__send_duplicate_bios(ci, ti, num_bios, &len);
14181414
}
14191415

14201416
static bool is_abnormal_io(struct bio *bio)

0 commit comments

Comments
 (0)