Skip to content

Commit 879129d

Browse files
Mikulas Patockakergon
authored andcommitted
dm snapshot: wait for chunks in destructor
If there are several snapshots sharing an origin and one is removed while the origin is being written to, the snapshot's mempool may get deleted while elements are still referenced. Prior to dm-snapshot-use-per-device-mempools.patch the pending exceptions may still have been referenced after the snapshot was destroyed, but this was not a problem because the shared mempool was still there. This patch fixes the problem by tracking the number of mempool elements in use. The scenario: - You have an origin and two snapshots 1 and 2. - Someone writes to the origin. - It creates two exceptions in the snapshots, snapshot 1 will be primary exception, snapshot 2's pending_exception->primary_pe will point to the exception in snapshot 1. - The exceptions are being relocated, relocation of exception 1 finishes (but it's pending_exception is still allocated, because it is referenced by an exception from snapshot 2) - The user lvremoves snapshot 1 --- it calls just suspend (does nothing) and destructor. md->pending is zero (there is no I/O submitted to the snapshot by md layer), so it won't help us. - The destructor waits for kcopyd jobs to finish on snapshot 1 --- but there are none. - The destructor on snapshot 1 cleans up everything. - The relocation of exception on snapshot 2 finishes, it drops reference on primary_pe. This frees its primary_pe pointer. Primary_pe points to pending exception created for snapshot 1. So it frees memory into non-existing mempool. Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Alasdair G Kergon <agk@redhat.com>
1 parent 60c856c commit 879129d

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎drivers/md/dm-snap.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,19 @@ static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snaps
370370
struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
371371
GFP_NOIO);
372372

373+
atomic_inc(&s->pending_exceptions_count);
373374
pe->snap = s;
374375

375376
return pe;
376377
}
377378

378379
static void free_pending_exception(struct dm_snap_pending_exception *pe)
379380
{
380-
mempool_free(pe, pe->snap->pending_pool);
381+
struct dm_snapshot *s = pe->snap;
382+
383+
mempool_free(pe, s->pending_pool);
384+
smp_mb__before_atomic_dec();
385+
atomic_dec(&s->pending_exceptions_count);
381386
}
382387

383388
static void insert_completed_exception(struct dm_snapshot *s,
@@ -602,6 +607,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
602607

603608
s->valid = 1;
604609
s->active = 0;
610+
atomic_set(&s->pending_exceptions_count, 0);
605611
init_rwsem(&s->lock);
606612
spin_lock_init(&s->pe_lock);
607613
s->ti = ti;
@@ -728,6 +734,14 @@ static void snapshot_dtr(struct dm_target *ti)
728734
/* After this returns there can be no new kcopyd jobs. */
729735
unregister_snapshot(s);
730736

737+
while (atomic_read(&s->pending_exceptions_count))
738+
yield();
739+
/*
740+
* Ensure instructions in mempool_destroy aren't reordered
741+
* before atomic_read.
742+
*/
743+
smp_mb();
744+
731745
#ifdef CONFIG_DM_DEBUG
732746
for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
733747
BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));

‎drivers/md/dm-snap.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ struct dm_snapshot {
160160

161161
mempool_t *pending_pool;
162162

163+
atomic_t pending_exceptions_count;
164+
163165
struct exception_table pending;
164166
struct exception_table complete;
165167

0 commit comments

Comments
 (0)