Skip to content

Commit 754c5fc

Browse files
snitmkergon
authored andcommitted
dm: calculate queue limits during resume not load
Currently, device-mapper maintains a separate instance of 'struct queue_limits' for each table of each device. When the configuration of a device is to be changed, first its table is loaded and this structure is populated, then the device is 'resumed' and the calculated queue_limits are applied. This places restrictions on how userspace may process related devices, where it is often advantageous to 'load' tables for several devices at once before 'resuming' them together. As the new queue_limits only take effect after the 'resume', if they are changing and one device uses another, the latter must be 'resumed' before the former may be 'loaded'. This patch moves the calculation of these queue_limits out of the 'load' operation into 'resume'. Since we are no longer pre-calculating this struct, we no longer need to maintain copies within our dm structs. dm_set_device_limits() now passes the 'start' of the device's data area (aka pe_start) as the 'offset' to blk_stack_limits(). init_valid_queue_limits() is replaced by blk_set_default_limits(). Signed-off-by: Mike Snitzer <snitzer@redhat.com> Cc: martin.petersen@oracle.com Signed-off-by: Alasdair G Kergon <agk@redhat.com>
1 parent 18d8594 commit 754c5fc

4 files changed

Lines changed: 117 additions & 95 deletions

File tree

‎drivers/md/dm-table.c‎

Lines changed: 102 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ struct dm_table {
6262
/* a list of devices used by this table */
6363
struct list_head devices;
6464

65-
/*
66-
* These are optimistic limits taken from all the
67-
* targets, some targets will need smaller limits.
68-
*/
69-
struct queue_limits limits;
70-
7165
/* events get handed up using this callback */
7266
void (*event_fn)(void *);
7367
void *event_context;
@@ -346,18 +340,21 @@ static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
346340
/*
347341
* If possible, this checks an area of a destination device is valid.
348342
*/
349-
static int device_area_is_valid(struct dm_target *ti, struct block_device *bdev,
350-
sector_t start, sector_t len)
343+
static int device_area_is_valid(struct dm_target *ti, struct dm_dev *dev,
344+
sector_t start, void *data)
351345
{
352-
sector_t dev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
346+
struct queue_limits *limits = data;
347+
struct block_device *bdev = dev->bdev;
348+
sector_t dev_size =
349+
i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
353350
unsigned short logical_block_size_sectors =
354-
ti->limits.logical_block_size >> SECTOR_SHIFT;
351+
limits->logical_block_size >> SECTOR_SHIFT;
355352
char b[BDEVNAME_SIZE];
356353

357354
if (!dev_size)
358355
return 1;
359356

360-
if ((start >= dev_size) || (start + len > dev_size)) {
357+
if ((start >= dev_size) || (start + ti->len > dev_size)) {
361358
DMWARN("%s: %s too small for target",
362359
dm_device_name(ti->table->md), bdevname(bdev, b));
363360
return 0;
@@ -371,16 +368,16 @@ static int device_area_is_valid(struct dm_target *ti, struct block_device *bdev,
371368
"logical block size %hu of %s",
372369
dm_device_name(ti->table->md),
373370
(unsigned long long)start,
374-
ti->limits.logical_block_size, bdevname(bdev, b));
371+
limits->logical_block_size, bdevname(bdev, b));
375372
return 0;
376373
}
377374

378-
if (len & (logical_block_size_sectors - 1)) {
375+
if (ti->len & (logical_block_size_sectors - 1)) {
379376
DMWARN("%s: len=%llu not aligned to h/w "
380377
"logical block size %hu of %s",
381378
dm_device_name(ti->table->md),
382-
(unsigned long long)len,
383-
ti->limits.logical_block_size, bdevname(bdev, b));
379+
(unsigned long long)ti->len,
380+
limits->logical_block_size, bdevname(bdev, b));
384381
return 0;
385382
}
386383

@@ -479,18 +476,21 @@ static int __table_get_device(struct dm_table *t, struct dm_target *ti,
479476
*/
480477
#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
481478

482-
void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
479+
int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
480+
sector_t start, void *data)
483481
{
482+
struct queue_limits *limits = data;
483+
struct block_device *bdev = dev->bdev;
484484
struct request_queue *q = bdev_get_queue(bdev);
485485
char b[BDEVNAME_SIZE];
486486

487487
if (unlikely(!q)) {
488488
DMWARN("%s: Cannot set limits for nonexistent device %s",
489489
dm_device_name(ti->table->md), bdevname(bdev, b));
490-
return;
490+
return 0;
491491
}
492492

493-
if (blk_stack_limits(&ti->limits, &q->limits, 0) < 0)
493+
if (blk_stack_limits(limits, &q->limits, start) < 0)
494494
DMWARN("%s: target device %s is misaligned",
495495
dm_device_name(ti->table->md), bdevname(bdev, b));
496496

@@ -501,32 +501,21 @@ void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
501501
*/
502502

503503
if (q->merge_bvec_fn && !ti->type->merge)
504-
ti->limits.max_sectors =
505-
min_not_zero(ti->limits.max_sectors,
504+
limits->max_sectors =
505+
min_not_zero(limits->max_sectors,
506506
(unsigned int) (PAGE_SIZE >> 9));
507+
return 0;
507508
}
508509
EXPORT_SYMBOL_GPL(dm_set_device_limits);
509510

510511
int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
511512
sector_t len, fmode_t mode, struct dm_dev **result)
512513
{
513-
int r = __table_get_device(ti->table, ti, path,
514-
start, len, mode, result);
515-
516-
if (r)
517-
return r;
518-
519-
dm_set_device_limits(ti, (*result)->bdev);
520-
521-
if (!device_area_is_valid(ti, (*result)->bdev, start, len)) {
522-
dm_put_device(ti, *result);
523-
*result = NULL;
524-
return -EINVAL;
525-
}
526-
527-
return r;
514+
return __table_get_device(ti->table, ti, path,
515+
start, len, mode, result);
528516
}
529517

518+
530519
/*
531520
* Decrement a devices use count and remove it if necessary.
532521
*/
@@ -641,49 +630,22 @@ int dm_split_args(int *argc, char ***argvp, char *input)
641630
return 0;
642631
}
643632

644-
static void init_valid_queue_limits(struct queue_limits *limits)
645-
{
646-
if (!limits->max_sectors)
647-
limits->max_sectors = SAFE_MAX_SECTORS;
648-
if (!limits->max_hw_sectors)
649-
limits->max_hw_sectors = SAFE_MAX_SECTORS;
650-
if (!limits->max_phys_segments)
651-
limits->max_phys_segments = MAX_PHYS_SEGMENTS;
652-
if (!limits->max_hw_segments)
653-
limits->max_hw_segments = MAX_HW_SEGMENTS;
654-
if (!limits->logical_block_size)
655-
limits->logical_block_size = 1 << SECTOR_SHIFT;
656-
if (!limits->physical_block_size)
657-
limits->physical_block_size = 1 << SECTOR_SHIFT;
658-
if (!limits->io_min)
659-
limits->io_min = 1 << SECTOR_SHIFT;
660-
if (!limits->max_segment_size)
661-
limits->max_segment_size = MAX_SEGMENT_SIZE;
662-
if (!limits->seg_boundary_mask)
663-
limits->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
664-
if (!limits->bounce_pfn)
665-
limits->bounce_pfn = -1;
666-
/*
667-
* The other fields (alignment_offset, io_opt, misaligned)
668-
* hold 0 from the kzalloc().
669-
*/
670-
}
671-
672633
/*
673634
* Impose necessary and sufficient conditions on a devices's table such
674635
* that any incoming bio which respects its logical_block_size can be
675636
* processed successfully. If it falls across the boundary between
676637
* two or more targets, the size of each piece it gets split into must
677638
* be compatible with the logical_block_size of the target processing it.
678639
*/
679-
static int validate_hardware_logical_block_alignment(struct dm_table *table)
640+
static int validate_hardware_logical_block_alignment(struct dm_table *table,
641+
struct queue_limits *limits)
680642
{
681643
/*
682644
* This function uses arithmetic modulo the logical_block_size
683645
* (in units of 512-byte sectors).
684646
*/
685647
unsigned short device_logical_block_size_sects =
686-
table->limits.logical_block_size >> SECTOR_SHIFT;
648+
limits->logical_block_size >> SECTOR_SHIFT;
687649

688650
/*
689651
* Offset of the start of the next table entry, mod logical_block_size.
@@ -697,6 +659,7 @@ static int validate_hardware_logical_block_alignment(struct dm_table *table)
697659
unsigned short remaining = 0;
698660

699661
struct dm_target *uninitialized_var(ti);
662+
struct queue_limits ti_limits;
700663
unsigned i = 0;
701664

702665
/*
@@ -705,12 +668,19 @@ static int validate_hardware_logical_block_alignment(struct dm_table *table)
705668
while (i < dm_table_get_num_targets(table)) {
706669
ti = dm_table_get_target(table, i++);
707670

671+
blk_set_default_limits(&ti_limits);
672+
673+
/* combine all target devices' limits */
674+
if (ti->type->iterate_devices)
675+
ti->type->iterate_devices(ti, dm_set_device_limits,
676+
&ti_limits);
677+
708678
/*
709679
* If the remaining sectors fall entirely within this
710680
* table entry are they compatible with its logical_block_size?
711681
*/
712682
if (remaining < ti->len &&
713-
remaining & ((ti->limits.logical_block_size >>
683+
remaining & ((ti_limits.logical_block_size >>
714684
SECTOR_SHIFT) - 1))
715685
break; /* Error */
716686

@@ -723,11 +693,11 @@ static int validate_hardware_logical_block_alignment(struct dm_table *table)
723693

724694
if (remaining) {
725695
DMWARN("%s: table line %u (start sect %llu len %llu) "
726-
"not aligned to hardware logical block size %hu",
696+
"not aligned to h/w logical block size %hu",
727697
dm_device_name(table->md), i,
728698
(unsigned long long) ti->begin,
729699
(unsigned long long) ti->len,
730-
table->limits.logical_block_size);
700+
limits->logical_block_size);
731701
return -EINVAL;
732702
}
733703

@@ -786,12 +756,6 @@ int dm_table_add_target(struct dm_table *t, const char *type,
786756

787757
t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
788758

789-
if (blk_stack_limits(&t->limits, &tgt->limits, 0) < 0)
790-
DMWARN("%s: target device (start sect %llu len %llu) "
791-
"is misaligned",
792-
dm_device_name(t->md),
793-
(unsigned long long) tgt->begin,
794-
(unsigned long long) tgt->len);
795759
return 0;
796760

797761
bad:
@@ -834,12 +798,6 @@ int dm_table_complete(struct dm_table *t)
834798
int r = 0;
835799
unsigned int leaf_nodes;
836800

837-
init_valid_queue_limits(&t->limits);
838-
839-
r = validate_hardware_logical_block_alignment(t);
840-
if (r)
841-
return r;
842-
843801
/* how many indexes will the btree have ? */
844802
leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
845803
t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
@@ -914,6 +872,57 @@ struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
914872
return &t->targets[(KEYS_PER_NODE * n) + k];
915873
}
916874

875+
/*
876+
* Establish the new table's queue_limits and validate them.
877+
*/
878+
int dm_calculate_queue_limits(struct dm_table *table,
879+
struct queue_limits *limits)
880+
{
881+
struct dm_target *uninitialized_var(ti);
882+
struct queue_limits ti_limits;
883+
unsigned i = 0;
884+
885+
blk_set_default_limits(limits);
886+
887+
while (i < dm_table_get_num_targets(table)) {
888+
blk_set_default_limits(&ti_limits);
889+
890+
ti = dm_table_get_target(table, i++);
891+
892+
if (!ti->type->iterate_devices)
893+
goto combine_limits;
894+
895+
/*
896+
* Combine queue limits of all the devices this target uses.
897+
*/
898+
ti->type->iterate_devices(ti, dm_set_device_limits,
899+
&ti_limits);
900+
901+
/*
902+
* Check each device area is consistent with the target's
903+
* overall queue limits.
904+
*/
905+
if (!ti->type->iterate_devices(ti, device_area_is_valid,
906+
&ti_limits))
907+
return -EINVAL;
908+
909+
combine_limits:
910+
/*
911+
* Merge this target's queue limits into the overall limits
912+
* for the table.
913+
*/
914+
if (blk_stack_limits(limits, &ti_limits, 0) < 0)
915+
DMWARN("%s: target device "
916+
"(start sect %llu len %llu) "
917+
"is misaligned",
918+
dm_device_name(table->md),
919+
(unsigned long long) ti->begin,
920+
(unsigned long long) ti->len);
921+
}
922+
923+
return validate_hardware_logical_block_alignment(table, limits);
924+
}
925+
917926
/*
918927
* Set the integrity profile for this device if all devices used have
919928
* matching profiles.
@@ -953,14 +962,24 @@ static void dm_table_set_integrity(struct dm_table *t)
953962
return;
954963
}
955964

956-
void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
965+
void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
966+
struct queue_limits *limits)
957967
{
968+
/*
969+
* Each target device in the table has a data area that should normally
970+
* be aligned such that the DM device's alignment_offset is 0.
971+
* FIXME: Propagate alignment_offsets up the stack and warn of
972+
* sub-optimal or inconsistent settings.
973+
*/
974+
limits->alignment_offset = 0;
975+
limits->misaligned = 0;
976+
958977
/*
959978
* Copy table's limits to the DM device's request_queue
960979
*/
961-
q->limits = t->limits;
980+
q->limits = *limits;
962981

963-
if (t->limits.no_cluster)
982+
if (limits->no_cluster)
964983
queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
965984
else
966985
queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);

‎drivers/md/dm.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,8 @@ static void __set_size(struct mapped_device *md, sector_t size)
13131313
mutex_unlock(&md->bdev->bd_inode->i_mutex);
13141314
}
13151315

1316-
static int __bind(struct mapped_device *md, struct dm_table *t)
1316+
static int __bind(struct mapped_device *md, struct dm_table *t,
1317+
struct queue_limits *limits)
13171318
{
13181319
struct request_queue *q = md->queue;
13191320
sector_t size;
@@ -1337,7 +1338,7 @@ static int __bind(struct mapped_device *md, struct dm_table *t)
13371338

13381339
write_lock(&md->map_lock);
13391340
md->map = t;
1340-
dm_table_set_restrictions(t, q);
1341+
dm_table_set_restrictions(t, q, limits);
13411342
write_unlock(&md->map_lock);
13421343

13431344
return 0;
@@ -1562,6 +1563,7 @@ static void dm_queue_flush(struct mapped_device *md)
15621563
*/
15631564
int dm_swap_table(struct mapped_device *md, struct dm_table *table)
15641565
{
1566+
struct queue_limits limits;
15651567
int r = -EINVAL;
15661568

15671569
mutex_lock(&md->suspend_lock);
@@ -1570,8 +1572,12 @@ int dm_swap_table(struct mapped_device *md, struct dm_table *table)
15701572
if (!dm_suspended(md))
15711573
goto out;
15721574

1575+
r = dm_calculate_queue_limits(table, &limits);
1576+
if (r)
1577+
goto out;
1578+
15731579
__unbind(md);
1574-
r = __bind(md, table);
1580+
r = __bind(md, table, &limits);
15751581

15761582
out:
15771583
mutex_unlock(&md->suspend_lock);

‎drivers/md/dm.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ void dm_table_event_callback(struct dm_table *t,
4141
void (*fn)(void *), void *context);
4242
struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index);
4343
struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector);
44-
void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q);
44+
int dm_calculate_queue_limits(struct dm_table *table,
45+
struct queue_limits *limits);
46+
void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
47+
struct queue_limits *limits);
4548
struct list_head *dm_table_get_devices(struct dm_table *t);
4649
void dm_table_presuspend_targets(struct dm_table *t);
4750
void dm_table_postsuspend_targets(struct dm_table *t);

0 commit comments

Comments
 (0)