Skip to content

Commit a666e5c

Browse files
Mikulas Patockasnitm
authored andcommitted
dm: fix deadlock when swapping to encrypted device
The system would deadlock when swapping to a dm-crypt device. The reason is that for each incoming write bio, dm-crypt allocates memory that holds encrypted data. These excessive allocations exhaust all the memory and the result is either deadlock or OOM trigger. This patch limits the number of in-flight swap bios, so that the memory consumed by dm-crypt is limited. The limit is enforced if the target set the "limit_swap_bios" variable and if the bio has REQ_SWAP set. Non-swap bios are not affected becuase taking the semaphore would cause performance degradation. This is similar to request-based drivers - they will also block when the number of requests is over the limit. Signed-off-by: Mikulas Patocka <[email protected]> Cc: [email protected] Signed-off-by: Mike Snitzer <[email protected]>
1 parent e3290b9 commit a666e5c

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

drivers/md/dm-core.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ struct mapped_device {
103103
/* kobject and completion */
104104
struct dm_kobject_holder kobj_holder;
105105

106+
int swap_bios;
107+
struct semaphore swap_bios_semaphore;
108+
struct mutex swap_bios_lock;
109+
106110
struct dm_stats stats;
107111

108112
/* for blk-mq request-based DM support */

drivers/md/dm-crypt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3342,6 +3342,7 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
33423342
wake_up_process(cc->write_thread);
33433343

33443344
ti->num_flush_bios = 1;
3345+
ti->limit_swap_bios = true;
33453346

33463347
return 0;
33473348

drivers/md/dm.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
153153
#define DM_NUMA_NODE NUMA_NO_NODE
154154
static int dm_numa_node = DM_NUMA_NODE;
155155

156+
#define DEFAULT_SWAP_BIOS (8 * 1048576 / PAGE_SIZE)
157+
static int swap_bios = DEFAULT_SWAP_BIOS;
158+
static int get_swap_bios(void)
159+
{
160+
int latch = READ_ONCE(swap_bios);
161+
if (unlikely(latch <= 0))
162+
latch = DEFAULT_SWAP_BIOS;
163+
return latch;
164+
}
165+
156166
/*
157167
* For mempools pre-allocation at the table loading time.
158168
*/
@@ -974,6 +984,11 @@ void disable_write_zeroes(struct mapped_device *md)
974984
limits->max_write_zeroes_sectors = 0;
975985
}
976986

987+
static bool swap_bios_limit(struct dm_target *ti, struct bio *bio)
988+
{
989+
return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios);
990+
}
991+
977992
static void clone_endio(struct bio *bio)
978993
{
979994
blk_status_t error = bio->bi_status;
@@ -1025,6 +1040,11 @@ static void clone_endio(struct bio *bio)
10251040
}
10261041
}
10271042

1043+
if (unlikely(swap_bios_limit(tio->ti, bio))) {
1044+
struct mapped_device *md = io->md;
1045+
up(&md->swap_bios_semaphore);
1046+
}
1047+
10281048
free_tio(tio);
10291049
dec_pending(io, error);
10301050
}
@@ -1258,6 +1278,22 @@ void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
12581278
}
12591279
EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
12601280

1281+
static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch)
1282+
{
1283+
mutex_lock(&md->swap_bios_lock);
1284+
while (latch < md->swap_bios) {
1285+
cond_resched();
1286+
down(&md->swap_bios_semaphore);
1287+
md->swap_bios--;
1288+
}
1289+
while (latch > md->swap_bios) {
1290+
cond_resched();
1291+
up(&md->swap_bios_semaphore);
1292+
md->swap_bios++;
1293+
}
1294+
mutex_unlock(&md->swap_bios_lock);
1295+
}
1296+
12611297
static blk_qc_t __map_bio(struct dm_target_io *tio)
12621298
{
12631299
int r;
@@ -1277,6 +1313,14 @@ static blk_qc_t __map_bio(struct dm_target_io *tio)
12771313
atomic_inc(&io->io_count);
12781314
sector = clone->bi_iter.bi_sector;
12791315

1316+
if (unlikely(swap_bios_limit(ti, clone))) {
1317+
struct mapped_device *md = io->md;
1318+
int latch = get_swap_bios();
1319+
if (unlikely(latch != md->swap_bios))
1320+
__set_swap_bios_limit(md, latch);
1321+
down(&md->swap_bios_semaphore);
1322+
}
1323+
12801324
r = ti->type->map(ti, clone);
12811325
switch (r) {
12821326
case DM_MAPIO_SUBMITTED:
@@ -1287,10 +1331,18 @@ static blk_qc_t __map_bio(struct dm_target_io *tio)
12871331
ret = submit_bio_noacct(clone);
12881332
break;
12891333
case DM_MAPIO_KILL:
1334+
if (unlikely(swap_bios_limit(ti, clone))) {
1335+
struct mapped_device *md = io->md;
1336+
up(&md->swap_bios_semaphore);
1337+
}
12901338
free_tio(tio);
12911339
dec_pending(io, BLK_STS_IOERR);
12921340
break;
12931341
case DM_MAPIO_REQUEUE:
1342+
if (unlikely(swap_bios_limit(ti, clone))) {
1343+
struct mapped_device *md = io->md;
1344+
up(&md->swap_bios_semaphore);
1345+
}
12941346
free_tio(tio);
12951347
dec_pending(io, BLK_STS_DM_REQUEUE);
12961348
break;
@@ -1767,6 +1819,7 @@ static void cleanup_mapped_device(struct mapped_device *md)
17671819
mutex_destroy(&md->suspend_lock);
17681820
mutex_destroy(&md->type_lock);
17691821
mutex_destroy(&md->table_devices_lock);
1822+
mutex_destroy(&md->swap_bios_lock);
17701823

17711824
dm_mq_cleanup_mapped_device(md);
17721825
}
@@ -1834,6 +1887,10 @@ static struct mapped_device *alloc_dev(int minor)
18341887
init_waitqueue_head(&md->eventq);
18351888
init_completion(&md->kobj_holder.completion);
18361889

1890+
md->swap_bios = get_swap_bios();
1891+
sema_init(&md->swap_bios_semaphore, md->swap_bios);
1892+
mutex_init(&md->swap_bios_lock);
1893+
18371894
md->disk->major = _major;
18381895
md->disk->first_minor = minor;
18391896
md->disk->fops = &dm_blk_dops;
@@ -3117,6 +3174,9 @@ MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
31173174
module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
31183175
MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
31193176

3177+
module_param(swap_bios, int, S_IRUGO | S_IWUSR);
3178+
MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs");
3179+
31203180
MODULE_DESCRIPTION(DM_NAME " driver");
31213181
MODULE_AUTHOR("Joe Thornber <[email protected]>");
31223182
MODULE_LICENSE("GPL");

include/linux/device-mapper.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ struct dm_target {
343343
* whether or not its underlying devices have support.
344344
*/
345345
bool discards_supported:1;
346+
347+
/*
348+
* Set if we need to limit the number of in-flight bios when swapping.
349+
*/
350+
bool limit_swap_bios:1;
346351
};
347352

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

0 commit comments

Comments
 (0)