Skip to content

Commit d3aa3e0

Browse files
JiangJiasMike Snitzer
authored andcommitted
dm stats: check for and propagate alloc_percpu failure
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: fd2ed4d ("dm: add statistics support") Cc: [email protected] Signed-off-by: Jiasheng Jiang <[email protected]> Signed-off-by: Mike Snitzer <[email protected]>
1 parent d9a02e0 commit d3aa3e0

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

drivers/md/dm-stats.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static int dm_stat_in_flight(struct dm_stat_shared *shared)
188188
atomic_read(&shared->in_flight[WRITE]);
189189
}
190190

191-
void dm_stats_init(struct dm_stats *stats)
191+
int dm_stats_init(struct dm_stats *stats)
192192
{
193193
int cpu;
194194
struct dm_stats_last_position *last;
@@ -197,11 +197,16 @@ void dm_stats_init(struct dm_stats *stats)
197197
INIT_LIST_HEAD(&stats->list);
198198
stats->precise_timestamps = false;
199199
stats->last = alloc_percpu(struct dm_stats_last_position);
200+
if (!stats->last)
201+
return -ENOMEM;
202+
200203
for_each_possible_cpu(cpu) {
201204
last = per_cpu_ptr(stats->last, cpu);
202205
last->last_sector = (sector_t)ULLONG_MAX;
203206
last->last_rw = UINT_MAX;
204207
}
208+
209+
return 0;
205210
}
206211

207212
void dm_stats_cleanup(struct dm_stats *stats)

drivers/md/dm-stats.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct dm_stats_aux {
2121
unsigned long long duration_ns;
2222
};
2323

24-
void dm_stats_init(struct dm_stats *st);
24+
int dm_stats_init(struct dm_stats *st);
2525
void dm_stats_cleanup(struct dm_stats *st);
2626

2727
struct mapped_device;

drivers/md/dm.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,9 @@ static struct mapped_device *alloc_dev(int minor)
20972097
if (!md->pending_io)
20982098
goto bad;
20992099

2100-
dm_stats_init(&md->stats);
2100+
r = dm_stats_init(&md->stats);
2101+
if (r < 0)
2102+
goto bad;
21012103

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

0 commit comments

Comments
 (0)