1 From 66cb1910df17b38334153462ec8166e48058035f Mon Sep 17 00:00:00 2001
2 From: Joe Thornber <ejt@redhat.com>
3 Date: Wed, 30 Oct 2013 17:11:58 +0000
4 Subject: dm cache: fix a race condition between queuing new migrations and quiescing for a shutdown
6 From: Joe Thornber <ejt@redhat.com>
8 commit 66cb1910df17b38334153462ec8166e48058035f upstream.
10 The code that was trying to do this was inadequate. The postsuspend
11 method (in ioctl context), needs to wait for the worker thread to
12 acknowledge the request to quiesce. Otherwise the migration count may
13 drop to zero temporarily before the worker thread realises we're
14 quiescing. In this case the target will be taken down, but the worker
15 thread may have issued a new migration, which will cause an oops when
18 Signed-off-by: Joe Thornber <ejt@redhat.com>
19 Signed-off-by: Mike Snitzer <snitzer@redhat.com>
20 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
23 drivers/md/dm-cache-target.c | 54 +++++++++++++++++++++++++++++++------------
24 1 file changed, 40 insertions(+), 14 deletions(-)
26 --- a/drivers/md/dm-cache-target.c
27 +++ b/drivers/md/dm-cache-target.c
28 @@ -148,6 +148,9 @@ struct cache {
29 wait_queue_head_t migration_wait;
30 atomic_t nr_migrations;
32 + wait_queue_head_t quiescing_wait;
33 + atomic_t quiescing_ack;
36 * cache_size entries, dirty if set
38 @@ -748,8 +751,9 @@ static void cell_defer(struct cache *cac
40 static void cleanup_migration(struct dm_cache_migration *mg)
42 - dec_nr_migrations(mg->cache);
43 + struct cache *cache = mg->cache;
45 + dec_nr_migrations(cache);
48 static void migration_failure(struct dm_cache_migration *mg)
49 @@ -1346,34 +1350,51 @@ static void writeback_some_dirty_blocks(
50 /*----------------------------------------------------------------
52 *--------------------------------------------------------------*/
53 -static void start_quiescing(struct cache *cache)
54 +static bool is_quiescing(struct cache *cache)
59 spin_lock_irqsave(&cache->lock, flags);
60 - cache->quiescing = 1;
61 + r = cache->quiescing;
62 spin_unlock_irqrestore(&cache->lock, flags);
67 -static void stop_quiescing(struct cache *cache)
68 +static void ack_quiescing(struct cache *cache)
70 + if (is_quiescing(cache)) {
71 + atomic_inc(&cache->quiescing_ack);
72 + wake_up(&cache->quiescing_wait);
76 +static void wait_for_quiescing_ack(struct cache *cache)
78 + wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
81 +static void start_quiescing(struct cache *cache)
85 spin_lock_irqsave(&cache->lock, flags);
86 - cache->quiescing = 0;
87 + cache->quiescing = true;
88 spin_unlock_irqrestore(&cache->lock, flags);
90 + wait_for_quiescing_ack(cache);
93 -static bool is_quiescing(struct cache *cache)
94 +static void stop_quiescing(struct cache *cache)
99 spin_lock_irqsave(&cache->lock, flags);
100 - r = cache->quiescing;
101 + cache->quiescing = false;
102 spin_unlock_irqrestore(&cache->lock, flags);
105 + atomic_set(&cache->quiescing_ack, 0);
108 static void wait_for_migrations(struct cache *cache)
109 @@ -1420,16 +1441,15 @@ static void do_worker(struct work_struct
110 struct cache *cache = container_of(ws, struct cache, worker);
113 - if (!is_quiescing(cache))
114 + if (!is_quiescing(cache)) {
115 + writeback_some_dirty_blocks(cache);
116 + process_deferred_writethrough_bios(cache);
117 process_deferred_bios(cache);
120 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
121 process_migrations(cache, &cache->completed_migrations, complete_migration);
123 - writeback_some_dirty_blocks(cache);
125 - process_deferred_writethrough_bios(cache);
127 if (commit_if_needed(cache)) {
128 process_deferred_flush_bios(cache, false);
130 @@ -1442,6 +1462,9 @@ static void do_worker(struct work_struct
131 process_migrations(cache, &cache->need_commit_migrations,
132 migration_success_post_commit);
135 + ack_quiescing(cache);
137 } while (more_work(cache));
140 @@ -2005,6 +2028,9 @@ static int cache_create(struct cache_arg
141 atomic_set(&cache->nr_migrations, 0);
142 init_waitqueue_head(&cache->migration_wait);
144 + init_waitqueue_head(&cache->quiescing_wait);
145 + atomic_set(&cache->quiescing_ack, 0);
149 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));