Skip to content

Commit 60c856c

Browse files
Mikulas Patockakergon
authored andcommitted
dm snapshot: fix register_snapshot deadlock
register_snapshot() performs a GFP_KERNEL allocation while holding _origins_lock for write, but that could write out dirty pages onto a device that attempts to acquire _origins_lock for read, resulting in deadlock. So move the allocation up before taking the lock. This path is not performance-critical, so it doesn't matter that we allocate memory and free it if we find that we won't need it. Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Alasdair G Kergon <agk@redhat.com>
1 parent b34578a commit 60c856c

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎drivers/md/dm-snap.c‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,21 @@ static void __insert_origin(struct origin *o)
229229
*/
230230
static int register_snapshot(struct dm_snapshot *snap)
231231
{
232-
struct origin *o;
232+
struct origin *o, *new_o;
233233
struct block_device *bdev = snap->origin->bdev;
234234

235+
new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
236+
if (!new_o)
237+
return -ENOMEM;
238+
235239
down_write(&_origins_lock);
236240
o = __lookup_origin(bdev);
237241

238-
if (!o) {
242+
if (o)
243+
kfree(new_o);
244+
else {
239245
/* New origin */
240-
o = kmalloc(sizeof(*o), GFP_KERNEL);
241-
if (!o) {
242-
up_write(&_origins_lock);
243-
return -ENOMEM;
244-
}
246+
o = new_o;
245247

246248
/* Initialise the struct */
247249
INIT_LIST_HEAD(&o->snapshots);

0 commit comments

Comments
 (0)