Commit 23b7b357 authored by 蒋家盛's avatar 蒋家盛 Committed by Li Lingfeng
Browse files

dm stats: check for and propagate alloc_percpu failure

mainline inclusion
from mainline-v6.3-rc4
commit d3aa3e06
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I7I2L8

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v6.4-rc6&id=d3aa3e060c4a80827eb801fc448debc9daa7c46b



--------------------------------

Check alloc_precpu()'s return value and return an error from
dm_stats_init() if it fails. Update alloc_dev() to fail if
dm_stats_init() does.

Otherwise, a NULL pointer dereference will occur in dm_stats_cleanup()
even if dm-stats isn't being actively used.

Fixes: fd2ed4d2 ("dm: add statistics support")
Cc: stable@vger.kernel.org
Signed-off-by: default avatarJiasheng Jiang <jiasheng@iscas.ac.cn>
Signed-off-by: default avatarMike Snitzer <snitzer@kernel.org>

Conflicts:
  drivers/md/dm-stats.c
  drivers/md/dm.c

Signed-off-by: default avatarLi Lingfeng <lilingfeng3@huawei.com>
parent 82c66282
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -188,7 +188,7 @@ static int dm_stat_in_flight(struct dm_stat_shared *shared)
	       atomic_read(&shared->in_flight[WRITE]);
}

void dm_stats_init(struct dm_stats *stats)
int dm_stats_init(struct dm_stats *stats)
{
	int cpu;
	struct dm_stats_last_position *last;
@@ -196,11 +196,16 @@ void dm_stats_init(struct dm_stats *stats)
	mutex_init(&stats->mutex);
	INIT_LIST_HEAD(&stats->list);
	stats->last = alloc_percpu(struct dm_stats_last_position);
	if (!stats->last)
		return -ENOMEM;

	for_each_possible_cpu(cpu) {
		last = per_cpu_ptr(stats->last, cpu);
		last->last_sector = (sector_t)ULLONG_MAX;
		last->last_rw = UINT_MAX;
	}

	return 0;
}

void dm_stats_cleanup(struct dm_stats *stats)
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ struct dm_stats_aux {
	unsigned long long duration_ns;
};

void dm_stats_init(struct dm_stats *st);
int dm_stats_init(struct dm_stats *st);
void dm_stats_cleanup(struct dm_stats *st);

struct mapped_device;
+3 −1
Original line number Diff line number Diff line
@@ -1904,7 +1904,9 @@ static struct mapped_device *alloc_dev(int minor)
	if (!md->bdev)
		goto bad;

	dm_stats_init(&md->stats);
	r = dm_stats_init(&md->stats);
	if (r < 0)
		goto bad;

	/* Populate the mapping, nobody knows we exist yet */
	spin_lock(&_minor_lock);