cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dm-delay.c (8597B)


      1/*
      2 * Copyright (C) 2005-2007 Red Hat GmbH
      3 *
      4 * A target that delays reads and/or writes and can send
      5 * them to different devices.
      6 *
      7 * This file is released under the GPL.
      8 */
      9
     10#include <linux/module.h>
     11#include <linux/init.h>
     12#include <linux/blkdev.h>
     13#include <linux/bio.h>
     14#include <linux/slab.h>
     15
     16#include <linux/device-mapper.h>
     17
     18#define DM_MSG_PREFIX "delay"
     19
     20struct delay_class {
     21	struct dm_dev *dev;
     22	sector_t start;
     23	unsigned delay;
     24	unsigned ops;
     25};
     26
     27struct delay_c {
     28	struct timer_list delay_timer;
     29	struct mutex timer_lock;
     30	struct workqueue_struct *kdelayd_wq;
     31	struct work_struct flush_expired_bios;
     32	struct list_head delayed_bios;
     33	atomic_t may_delay;
     34
     35	struct delay_class read;
     36	struct delay_class write;
     37	struct delay_class flush;
     38
     39	int argc;
     40};
     41
     42struct dm_delay_info {
     43	struct delay_c *context;
     44	struct delay_class *class;
     45	struct list_head list;
     46	unsigned long expires;
     47};
     48
     49static DEFINE_MUTEX(delayed_bios_lock);
     50
     51static void handle_delayed_timer(struct timer_list *t)
     52{
     53	struct delay_c *dc = from_timer(dc, t, delay_timer);
     54
     55	queue_work(dc->kdelayd_wq, &dc->flush_expired_bios);
     56}
     57
     58static void queue_timeout(struct delay_c *dc, unsigned long expires)
     59{
     60	mutex_lock(&dc->timer_lock);
     61
     62	if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
     63		mod_timer(&dc->delay_timer, expires);
     64
     65	mutex_unlock(&dc->timer_lock);
     66}
     67
     68static void flush_bios(struct bio *bio)
     69{
     70	struct bio *n;
     71
     72	while (bio) {
     73		n = bio->bi_next;
     74		bio->bi_next = NULL;
     75		dm_submit_bio_remap(bio, NULL);
     76		bio = n;
     77	}
     78}
     79
     80static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
     81{
     82	struct dm_delay_info *delayed, *next;
     83	unsigned long next_expires = 0;
     84	unsigned long start_timer = 0;
     85	struct bio_list flush_bios = { };
     86
     87	mutex_lock(&delayed_bios_lock);
     88	list_for_each_entry_safe(delayed, next, &dc->delayed_bios, list) {
     89		if (flush_all || time_after_eq(jiffies, delayed->expires)) {
     90			struct bio *bio = dm_bio_from_per_bio_data(delayed,
     91						sizeof(struct dm_delay_info));
     92			list_del(&delayed->list);
     93			bio_list_add(&flush_bios, bio);
     94			delayed->class->ops--;
     95			continue;
     96		}
     97
     98		if (!start_timer) {
     99			start_timer = 1;
    100			next_expires = delayed->expires;
    101		} else
    102			next_expires = min(next_expires, delayed->expires);
    103	}
    104	mutex_unlock(&delayed_bios_lock);
    105
    106	if (start_timer)
    107		queue_timeout(dc, next_expires);
    108
    109	return bio_list_get(&flush_bios);
    110}
    111
    112static void flush_expired_bios(struct work_struct *work)
    113{
    114	struct delay_c *dc;
    115
    116	dc = container_of(work, struct delay_c, flush_expired_bios);
    117	flush_bios(flush_delayed_bios(dc, 0));
    118}
    119
    120static void delay_dtr(struct dm_target *ti)
    121{
    122	struct delay_c *dc = ti->private;
    123
    124	if (dc->kdelayd_wq)
    125		destroy_workqueue(dc->kdelayd_wq);
    126
    127	if (dc->read.dev)
    128		dm_put_device(ti, dc->read.dev);
    129	if (dc->write.dev)
    130		dm_put_device(ti, dc->write.dev);
    131	if (dc->flush.dev)
    132		dm_put_device(ti, dc->flush.dev);
    133
    134	mutex_destroy(&dc->timer_lock);
    135
    136	kfree(dc);
    137}
    138
    139static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **argv)
    140{
    141	int ret;
    142	unsigned long long tmpll;
    143	char dummy;
    144
    145	if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
    146		ti->error = "Invalid device sector";
    147		return -EINVAL;
    148	}
    149	c->start = tmpll;
    150
    151	if (sscanf(argv[2], "%u%c", &c->delay, &dummy) != 1) {
    152		ti->error = "Invalid delay";
    153		return -EINVAL;
    154	}
    155
    156	ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &c->dev);
    157	if (ret) {
    158		ti->error = "Device lookup failed";
    159		return ret;
    160	}
    161
    162	return 0;
    163}
    164
    165/*
    166 * Mapping parameters:
    167 *    <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
    168 *
    169 * With separate write parameters, the first set is only used for reads.
    170 * Offsets are specified in sectors.
    171 * Delays are specified in milliseconds.
    172 */
    173static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
    174{
    175	struct delay_c *dc;
    176	int ret;
    177
    178	if (argc != 3 && argc != 6 && argc != 9) {
    179		ti->error = "Requires exactly 3, 6 or 9 arguments";
    180		return -EINVAL;
    181	}
    182
    183	dc = kzalloc(sizeof(*dc), GFP_KERNEL);
    184	if (!dc) {
    185		ti->error = "Cannot allocate context";
    186		return -ENOMEM;
    187	}
    188
    189	ti->private = dc;
    190	timer_setup(&dc->delay_timer, handle_delayed_timer, 0);
    191	INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
    192	INIT_LIST_HEAD(&dc->delayed_bios);
    193	mutex_init(&dc->timer_lock);
    194	atomic_set(&dc->may_delay, 1);
    195	dc->argc = argc;
    196
    197	ret = delay_class_ctr(ti, &dc->read, argv);
    198	if (ret)
    199		goto bad;
    200
    201	if (argc == 3) {
    202		ret = delay_class_ctr(ti, &dc->write, argv);
    203		if (ret)
    204			goto bad;
    205		ret = delay_class_ctr(ti, &dc->flush, argv);
    206		if (ret)
    207			goto bad;
    208		goto out;
    209	}
    210
    211	ret = delay_class_ctr(ti, &dc->write, argv + 3);
    212	if (ret)
    213		goto bad;
    214	if (argc == 6) {
    215		ret = delay_class_ctr(ti, &dc->flush, argv + 3);
    216		if (ret)
    217			goto bad;
    218		goto out;
    219	}
    220
    221	ret = delay_class_ctr(ti, &dc->flush, argv + 6);
    222	if (ret)
    223		goto bad;
    224
    225out:
    226	dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
    227	if (!dc->kdelayd_wq) {
    228		ret = -EINVAL;
    229		DMERR("Couldn't start kdelayd");
    230		goto bad;
    231	}
    232
    233	ti->num_flush_bios = 1;
    234	ti->num_discard_bios = 1;
    235	ti->accounts_remapped_io = true;
    236	ti->per_io_data_size = sizeof(struct dm_delay_info);
    237	return 0;
    238
    239bad:
    240	delay_dtr(ti);
    241	return ret;
    242}
    243
    244static int delay_bio(struct delay_c *dc, struct delay_class *c, struct bio *bio)
    245{
    246	struct dm_delay_info *delayed;
    247	unsigned long expires = 0;
    248
    249	if (!c->delay || !atomic_read(&dc->may_delay))
    250		return DM_MAPIO_REMAPPED;
    251
    252	delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
    253
    254	delayed->context = dc;
    255	delayed->expires = expires = jiffies + msecs_to_jiffies(c->delay);
    256
    257	mutex_lock(&delayed_bios_lock);
    258	c->ops++;
    259	list_add_tail(&delayed->list, &dc->delayed_bios);
    260	mutex_unlock(&delayed_bios_lock);
    261
    262	queue_timeout(dc, expires);
    263
    264	return DM_MAPIO_SUBMITTED;
    265}
    266
    267static void delay_presuspend(struct dm_target *ti)
    268{
    269	struct delay_c *dc = ti->private;
    270
    271	atomic_set(&dc->may_delay, 0);
    272	del_timer_sync(&dc->delay_timer);
    273	flush_bios(flush_delayed_bios(dc, 1));
    274}
    275
    276static void delay_resume(struct dm_target *ti)
    277{
    278	struct delay_c *dc = ti->private;
    279
    280	atomic_set(&dc->may_delay, 1);
    281}
    282
    283static int delay_map(struct dm_target *ti, struct bio *bio)
    284{
    285	struct delay_c *dc = ti->private;
    286	struct delay_class *c;
    287	struct dm_delay_info *delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
    288
    289	if (bio_data_dir(bio) == WRITE) {
    290		if (unlikely(bio->bi_opf & REQ_PREFLUSH))
    291			c = &dc->flush;
    292		else
    293			c = &dc->write;
    294	} else {
    295		c = &dc->read;
    296	}
    297	delayed->class = c;
    298	bio_set_dev(bio, c->dev->bdev);
    299	bio->bi_iter.bi_sector = c->start + dm_target_offset(ti, bio->bi_iter.bi_sector);
    300
    301	return delay_bio(dc, c, bio);
    302}
    303
    304#define DMEMIT_DELAY_CLASS(c) \
    305	DMEMIT("%s %llu %u", (c)->dev->name, (unsigned long long)(c)->start, (c)->delay)
    306
    307static void delay_status(struct dm_target *ti, status_type_t type,
    308			 unsigned status_flags, char *result, unsigned maxlen)
    309{
    310	struct delay_c *dc = ti->private;
    311	int sz = 0;
    312
    313	switch (type) {
    314	case STATUSTYPE_INFO:
    315		DMEMIT("%u %u %u", dc->read.ops, dc->write.ops, dc->flush.ops);
    316		break;
    317
    318	case STATUSTYPE_TABLE:
    319		DMEMIT_DELAY_CLASS(&dc->read);
    320		if (dc->argc >= 6) {
    321			DMEMIT(" ");
    322			DMEMIT_DELAY_CLASS(&dc->write);
    323		}
    324		if (dc->argc >= 9) {
    325			DMEMIT(" ");
    326			DMEMIT_DELAY_CLASS(&dc->flush);
    327		}
    328		break;
    329
    330	case STATUSTYPE_IMA:
    331		*result = '\0';
    332		break;
    333	}
    334}
    335
    336static int delay_iterate_devices(struct dm_target *ti,
    337				 iterate_devices_callout_fn fn, void *data)
    338{
    339	struct delay_c *dc = ti->private;
    340	int ret = 0;
    341
    342	ret = fn(ti, dc->read.dev, dc->read.start, ti->len, data);
    343	if (ret)
    344		goto out;
    345	ret = fn(ti, dc->write.dev, dc->write.start, ti->len, data);
    346	if (ret)
    347		goto out;
    348	ret = fn(ti, dc->flush.dev, dc->flush.start, ti->len, data);
    349	if (ret)
    350		goto out;
    351
    352out:
    353	return ret;
    354}
    355
    356static struct target_type delay_target = {
    357	.name	     = "delay",
    358	.version     = {1, 3, 0},
    359	.features    = DM_TARGET_PASSES_INTEGRITY,
    360	.module      = THIS_MODULE,
    361	.ctr	     = delay_ctr,
    362	.dtr	     = delay_dtr,
    363	.map	     = delay_map,
    364	.presuspend  = delay_presuspend,
    365	.resume	     = delay_resume,
    366	.status	     = delay_status,
    367	.iterate_devices = delay_iterate_devices,
    368};
    369
    370static int __init dm_delay_init(void)
    371{
    372	int r;
    373
    374	r = dm_register_target(&delay_target);
    375	if (r < 0) {
    376		DMERR("register failed %d", r);
    377		goto bad_register;
    378	}
    379
    380	return 0;
    381
    382bad_register:
    383	return r;
    384}
    385
    386static void __exit dm_delay_exit(void)
    387{
    388	dm_unregister_target(&delay_target);
    389}
    390
    391/* Module hooks */
    392module_init(dm_delay_init);
    393module_exit(dm_delay_exit);
    394
    395MODULE_DESCRIPTION(DM_NAME " delay target");
    396MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
    397MODULE_LICENSE("GPL");