Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 CNEX Labs |
| 3 | * Initial release: Javier Gonzalez <javier@cnexlabs.com> |
| 4 | * Matias Bjorling <matias@cnexlabs.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License version |
| 8 | * 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * pblk-gc.c - pblk's garbage collector |
| 16 | */ |
| 17 | |
| 18 | #include "pblk.h" |
Hans Holmberg | f293723 | 2018-10-09 13:11:53 +0200 | [diff] [blame^] | 19 | #include "pblk-trace.h" |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 20 | #include <linux/delay.h> |
| 21 | |
Hans Holmberg | f293723 | 2018-10-09 13:11:53 +0200 | [diff] [blame^] | 22 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 23 | static void pblk_gc_free_gc_rq(struct pblk_gc_rq *gc_rq) |
| 24 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 25 | if (gc_rq->data) |
| 26 | vfree(gc_rq->data); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 27 | kfree(gc_rq); |
| 28 | } |
| 29 | |
| 30 | static int pblk_gc_write(struct pblk *pblk) |
| 31 | { |
| 32 | struct pblk_gc *gc = &pblk->gc; |
| 33 | struct pblk_gc_rq *gc_rq, *tgc_rq; |
| 34 | LIST_HEAD(w_list); |
| 35 | |
| 36 | spin_lock(&gc->w_lock); |
| 37 | if (list_empty(&gc->w_list)) { |
| 38 | spin_unlock(&gc->w_lock); |
| 39 | return 1; |
| 40 | } |
| 41 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 42 | list_cut_position(&w_list, &gc->w_list, gc->w_list.prev); |
| 43 | gc->w_entries = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 44 | spin_unlock(&gc->w_lock); |
| 45 | |
| 46 | list_for_each_entry_safe(gc_rq, tgc_rq, &w_list, list) { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 47 | pblk_write_gc_to_cache(pblk, gc_rq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 48 | list_del(&gc_rq->list); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 49 | kref_put(&gc_rq->line->ref, pblk_line_put); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 50 | pblk_gc_free_gc_rq(gc_rq); |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static void pblk_gc_writer_kick(struct pblk_gc *gc) |
| 57 | { |
| 58 | wake_up_process(gc->gc_writer_ts); |
| 59 | } |
| 60 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 61 | static void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line) |
| 62 | { |
| 63 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 64 | struct list_head *move_list; |
| 65 | |
| 66 | spin_lock(&line->lock); |
| 67 | WARN_ON(line->state != PBLK_LINESTATE_GC); |
| 68 | line->state = PBLK_LINESTATE_CLOSED; |
Hans Holmberg | f293723 | 2018-10-09 13:11:53 +0200 | [diff] [blame^] | 69 | trace_pblk_line_state(pblk_disk_name(pblk), line->id, |
| 70 | line->state); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 71 | move_list = pblk_line_gc_list(pblk, line); |
| 72 | spin_unlock(&line->lock); |
| 73 | |
| 74 | if (move_list) { |
| 75 | spin_lock(&l_mg->gc_lock); |
| 76 | list_add_tail(&line->list, move_list); |
| 77 | spin_unlock(&l_mg->gc_lock); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void pblk_gc_line_ws(struct work_struct *work) |
| 82 | { |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 83 | struct pblk_line_ws *gc_rq_ws = container_of(work, |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 84 | struct pblk_line_ws, ws); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 85 | struct pblk *pblk = gc_rq_ws->pblk; |
Javier González | 2a19b10 | 2017-10-13 14:46:15 +0200 | [diff] [blame] | 86 | struct nvm_tgt_dev *dev = pblk->dev; |
| 87 | struct nvm_geo *geo = &dev->geo; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 88 | struct pblk_gc *gc = &pblk->gc; |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 89 | struct pblk_line *line = gc_rq_ws->line; |
| 90 | struct pblk_gc_rq *gc_rq = gc_rq_ws->priv; |
Javier González | 2a19b10 | 2017-10-13 14:46:15 +0200 | [diff] [blame] | 91 | int ret; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 92 | |
| 93 | up(&gc->gc_sem); |
| 94 | |
Kees Cook | 42bc47b | 2018-06-12 14:27:11 -0700 | [diff] [blame] | 95 | gc_rq->data = vmalloc(array_size(gc_rq->nr_secs, geo->csecs)); |
Javier González | 2a19b10 | 2017-10-13 14:46:15 +0200 | [diff] [blame] | 96 | if (!gc_rq->data) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 97 | pblk_err(pblk, "could not GC line:%d (%d/%d)\n", |
Javier González | 2a19b10 | 2017-10-13 14:46:15 +0200 | [diff] [blame] | 98 | line->id, *line->vsc, gc_rq->nr_secs); |
| 99 | goto out; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Javier González | 2a19b10 | 2017-10-13 14:46:15 +0200 | [diff] [blame] | 102 | /* Read from GC victim block */ |
| 103 | ret = pblk_submit_read_gc(pblk, gc_rq); |
| 104 | if (ret) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 105 | pblk_err(pblk, "failed GC read in line:%d (err:%d)\n", |
Javier González | 2a19b10 | 2017-10-13 14:46:15 +0200 | [diff] [blame] | 106 | line->id, ret); |
| 107 | goto out; |
| 108 | } |
| 109 | |
| 110 | if (!gc_rq->secs_to_gc) |
| 111 | goto out; |
| 112 | |
| 113 | retry: |
| 114 | spin_lock(&gc->w_lock); |
| 115 | if (gc->w_entries >= PBLK_GC_RQ_QD) { |
| 116 | spin_unlock(&gc->w_lock); |
| 117 | pblk_gc_writer_kick(&pblk->gc); |
| 118 | usleep_range(128, 256); |
| 119 | goto retry; |
| 120 | } |
| 121 | gc->w_entries++; |
| 122 | list_add_tail(&gc_rq->list, &gc->w_list); |
| 123 | spin_unlock(&gc->w_lock); |
| 124 | |
| 125 | pblk_gc_writer_kick(&pblk->gc); |
| 126 | |
| 127 | kfree(gc_rq_ws); |
| 128 | return; |
| 129 | |
| 130 | out: |
| 131 | pblk_gc_free_gc_rq(gc_rq); |
| 132 | kref_put(&line->ref, pblk_line_put); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 133 | kfree(gc_rq_ws); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 136 | static __le64 *get_lba_list_from_emeta(struct pblk *pblk, |
| 137 | struct pblk_line *line) |
| 138 | { |
| 139 | struct line_emeta *emeta_buf; |
| 140 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 141 | struct pblk_line_meta *lm = &pblk->lm; |
| 142 | unsigned int lba_list_size = lm->emeta_len[2]; |
| 143 | __le64 *lba_list; |
| 144 | int ret; |
| 145 | |
| 146 | emeta_buf = pblk_malloc(lm->emeta_len[0], |
| 147 | l_mg->emeta_alloc_type, GFP_KERNEL); |
| 148 | if (!emeta_buf) |
| 149 | return NULL; |
| 150 | |
| 151 | ret = pblk_line_read_emeta(pblk, line, emeta_buf); |
| 152 | if (ret) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 153 | pblk_err(pblk, "line %d read emeta failed (%d)\n", |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 154 | line->id, ret); |
| 155 | pblk_mfree(emeta_buf, l_mg->emeta_alloc_type); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | /* If this read fails, it means that emeta is corrupted. |
| 160 | * For now, leave the line untouched. |
| 161 | * TODO: Implement a recovery routine that scans and moves |
| 162 | * all sectors on the line. |
| 163 | */ |
| 164 | |
| 165 | ret = pblk_recov_check_emeta(pblk, emeta_buf); |
| 166 | if (ret) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 167 | pblk_err(pblk, "inconsistent emeta (line %d)\n", |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 168 | line->id); |
| 169 | pblk_mfree(emeta_buf, l_mg->emeta_alloc_type); |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | lba_list = pblk_malloc(lba_list_size, |
| 174 | l_mg->emeta_alloc_type, GFP_KERNEL); |
| 175 | if (lba_list) |
| 176 | memcpy(lba_list, emeta_to_lbas(pblk, emeta_buf), lba_list_size); |
| 177 | |
| 178 | pblk_mfree(emeta_buf, l_mg->emeta_alloc_type); |
| 179 | |
| 180 | return lba_list; |
| 181 | } |
| 182 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 183 | static void pblk_gc_line_prepare_ws(struct work_struct *work) |
| 184 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 185 | struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws, |
| 186 | ws); |
| 187 | struct pblk *pblk = line_ws->pblk; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 188 | struct pblk_line *line = line_ws->line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 189 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 190 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 191 | struct pblk_gc *gc = &pblk->gc; |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 192 | struct pblk_line_ws *gc_rq_ws; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 193 | struct pblk_gc_rq *gc_rq; |
| 194 | __le64 *lba_list; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 195 | unsigned long *invalid_bitmap; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 196 | int sec_left, nr_secs, bit; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 197 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 198 | invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_KERNEL); |
Markus Elfring | 5da84cf | 2018-03-30 00:04:48 +0200 | [diff] [blame] | 199 | if (!invalid_bitmap) |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 200 | goto fail_free_ws; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 201 | |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 202 | if (line->w_err_gc->has_write_err) { |
| 203 | lba_list = line->w_err_gc->lba_list; |
| 204 | line->w_err_gc->lba_list = NULL; |
| 205 | } else { |
| 206 | lba_list = get_lba_list_from_emeta(pblk, line); |
| 207 | if (!lba_list) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 208 | pblk_err(pblk, "could not interpret emeta (line %d)\n", |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 209 | line->id); |
Colin Ian King | 0ec6937 | 2018-06-05 16:14:56 +0100 | [diff] [blame] | 210 | goto fail_free_invalid_bitmap; |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 211 | } |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 212 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 213 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 214 | spin_lock(&line->lock); |
| 215 | bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 216 | sec_left = pblk_line_vsc(line); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 217 | spin_unlock(&line->lock); |
| 218 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 219 | if (sec_left < 0) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 220 | pblk_err(pblk, "corrupted GC line (%d)\n", line->id); |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 221 | goto fail_free_lba_list; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 222 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 223 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 224 | bit = -1; |
| 225 | next_rq: |
| 226 | gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL); |
| 227 | if (!gc_rq) |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 228 | goto fail_free_lba_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 229 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 230 | nr_secs = 0; |
| 231 | do { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 232 | bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line, |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 233 | bit + 1); |
| 234 | if (bit > line->emeta_ssec) |
| 235 | break; |
| 236 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 237 | gc_rq->paddr_list[nr_secs] = bit; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 238 | gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]); |
| 239 | } while (nr_secs < pblk->max_write_pgs); |
| 240 | |
| 241 | if (unlikely(!nr_secs)) { |
| 242 | kfree(gc_rq); |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | gc_rq->nr_secs = nr_secs; |
| 247 | gc_rq->line = line; |
| 248 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 249 | gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL); |
| 250 | if (!gc_rq_ws) |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 251 | goto fail_free_gc_rq; |
| 252 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 253 | gc_rq_ws->pblk = pblk; |
| 254 | gc_rq_ws->line = line; |
| 255 | gc_rq_ws->priv = gc_rq; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 256 | |
Javier González | 8da10cc | 2017-10-13 14:46:45 +0200 | [diff] [blame] | 257 | /* The write GC path can be much slower than the read GC one due to |
| 258 | * the budget imposed by the rate-limiter. Balance in case that we get |
| 259 | * back pressure from the write GC path. |
| 260 | */ |
| 261 | while (down_timeout(&gc->gc_sem, msecs_to_jiffies(30000))) |
| 262 | io_schedule(); |
| 263 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 264 | kref_get(&line->ref); |
| 265 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 266 | INIT_WORK(&gc_rq_ws->ws, pblk_gc_line_ws); |
| 267 | queue_work(gc->gc_line_reader_wq, &gc_rq_ws->ws); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 268 | |
| 269 | sec_left -= nr_secs; |
| 270 | if (sec_left > 0) |
| 271 | goto next_rq; |
| 272 | |
| 273 | out: |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 274 | pblk_mfree(lba_list, l_mg->emeta_alloc_type); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 275 | kfree(line_ws); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 276 | kfree(invalid_bitmap); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 277 | |
| 278 | kref_put(&line->ref, pblk_line_put); |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 279 | atomic_dec(&gc->read_inflight_gc); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 280 | |
| 281 | return; |
| 282 | |
| 283 | fail_free_gc_rq: |
| 284 | kfree(gc_rq); |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 285 | fail_free_lba_list: |
| 286 | pblk_mfree(lba_list, l_mg->emeta_alloc_type); |
Colin Ian King | 0ec6937 | 2018-06-05 16:14:56 +0100 | [diff] [blame] | 287 | fail_free_invalid_bitmap: |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 288 | kfree(invalid_bitmap); |
| 289 | fail_free_ws: |
| 290 | kfree(line_ws); |
| 291 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 292 | pblk_put_line_back(pblk, line); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 293 | kref_put(&line->ref, pblk_line_put); |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 294 | atomic_dec(&gc->read_inflight_gc); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 295 | |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 296 | pblk_err(pblk, "failed to GC line %d\n", line->id); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 299 | static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 300 | { |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 301 | struct pblk_gc *gc = &pblk->gc; |
| 302 | struct pblk_line_ws *line_ws; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 303 | |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 304 | pblk_debug(pblk, "line '%d' being reclaimed for GC\n", line->id); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 305 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 306 | line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 307 | if (!line_ws) |
| 308 | return -ENOMEM; |
| 309 | |
| 310 | line_ws->pblk = pblk; |
| 311 | line_ws->line = line; |
| 312 | |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 313 | atomic_inc(&gc->pipeline_gc); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 314 | INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws); |
| 315 | queue_work(gc->gc_reader_wq, &line_ws->ws); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Javier González | 8bd4002 | 2017-10-13 14:46:44 +0200 | [diff] [blame] | 320 | static void pblk_gc_reader_kick(struct pblk_gc *gc) |
| 321 | { |
| 322 | wake_up_process(gc->gc_reader_ts); |
| 323 | } |
| 324 | |
| 325 | static void pblk_gc_kick(struct pblk *pblk) |
| 326 | { |
| 327 | struct pblk_gc *gc = &pblk->gc; |
| 328 | |
| 329 | pblk_gc_writer_kick(gc); |
| 330 | pblk_gc_reader_kick(gc); |
| 331 | |
| 332 | /* If we're shutting down GC, let's not start it up again */ |
| 333 | if (gc->gc_enabled) { |
| 334 | wake_up_process(gc->gc_ts); |
| 335 | mod_timer(&gc->gc_timer, |
| 336 | jiffies + msecs_to_jiffies(GC_TIME_MSECS)); |
| 337 | } |
| 338 | } |
| 339 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 340 | static int pblk_gc_read(struct pblk *pblk) |
| 341 | { |
| 342 | struct pblk_gc *gc = &pblk->gc; |
| 343 | struct pblk_line *line; |
| 344 | |
| 345 | spin_lock(&gc->r_lock); |
| 346 | if (list_empty(&gc->r_list)) { |
| 347 | spin_unlock(&gc->r_lock); |
| 348 | return 1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 349 | } |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 350 | |
| 351 | line = list_first_entry(&gc->r_list, struct pblk_line, list); |
| 352 | list_del(&line->list); |
| 353 | spin_unlock(&gc->r_lock); |
| 354 | |
| 355 | pblk_gc_kick(pblk); |
| 356 | |
| 357 | if (pblk_gc_line(pblk, line)) |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 358 | pblk_err(pblk, "failed to GC line %d\n", line->id); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame] | 363 | static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk, |
| 364 | struct list_head *group_list) |
| 365 | { |
| 366 | struct pblk_line *line, *victim; |
Javier González | f417aa0 | 2017-06-30 17:56:34 +0200 | [diff] [blame] | 367 | int line_vsc, victim_vsc; |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame] | 368 | |
| 369 | victim = list_first_entry(group_list, struct pblk_line, list); |
| 370 | list_for_each_entry(line, group_list, list) { |
Javier González | f417aa0 | 2017-06-30 17:56:34 +0200 | [diff] [blame] | 371 | line_vsc = le32_to_cpu(*line->vsc); |
| 372 | victim_vsc = le32_to_cpu(*victim->vsc); |
| 373 | if (line_vsc < victim_vsc) |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame] | 374 | victim = line; |
| 375 | } |
| 376 | |
| 377 | return victim; |
| 378 | } |
| 379 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 380 | static bool pblk_gc_should_run(struct pblk_gc *gc, struct pblk_rl *rl) |
| 381 | { |
| 382 | unsigned int nr_blocks_free, nr_blocks_need; |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 383 | unsigned int werr_lines = atomic_read(&rl->werr_lines); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 384 | |
| 385 | nr_blocks_need = pblk_rl_high_thrs(rl); |
| 386 | nr_blocks_free = pblk_rl_nr_free_blks(rl); |
| 387 | |
| 388 | /* This is not critical, no need to take lock here */ |
Hans Holmberg | 48b8d20 | 2018-06-01 16:41:06 +0200 | [diff] [blame] | 389 | return ((werr_lines > 0) || |
| 390 | ((gc->gc_active) && (nr_blocks_need > nr_blocks_free))); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Hans Holmberg | 37ce33d | 2017-10-13 14:46:36 +0200 | [diff] [blame] | 393 | void pblk_gc_free_full_lines(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 394 | { |
| 395 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 396 | struct pblk_gc *gc = &pblk->gc; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 397 | struct pblk_line *line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 398 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 399 | do { |
| 400 | spin_lock(&l_mg->gc_lock); |
| 401 | if (list_empty(&l_mg->gc_full_list)) { |
| 402 | spin_unlock(&l_mg->gc_lock); |
Hans Holmberg | 37ce33d | 2017-10-13 14:46:36 +0200 | [diff] [blame] | 403 | return; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | line = list_first_entry(&l_mg->gc_full_list, |
| 407 | struct pblk_line, list); |
| 408 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 409 | spin_lock(&line->lock); |
| 410 | WARN_ON(line->state != PBLK_LINESTATE_CLOSED); |
| 411 | line->state = PBLK_LINESTATE_GC; |
Hans Holmberg | f293723 | 2018-10-09 13:11:53 +0200 | [diff] [blame^] | 412 | trace_pblk_line_state(pblk_disk_name(pblk), line->id, |
| 413 | line->state); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 414 | spin_unlock(&line->lock); |
| 415 | |
| 416 | list_del(&line->list); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 417 | spin_unlock(&l_mg->gc_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 418 | |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 419 | atomic_inc(&gc->pipeline_gc); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 420 | kref_put(&line->ref, pblk_line_put); |
| 421 | } while (1); |
Hans Holmberg | 37ce33d | 2017-10-13 14:46:36 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Lines with no valid sectors will be returned to the free list immediately. If |
| 426 | * GC is activated - either because the free block count is under the determined |
| 427 | * threshold, or because it is being forced from user space - only lines with a |
| 428 | * high count of invalid sectors will be recycled. |
| 429 | */ |
| 430 | static void pblk_gc_run(struct pblk *pblk) |
| 431 | { |
| 432 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 433 | struct pblk_gc *gc = &pblk->gc; |
| 434 | struct pblk_line *line; |
| 435 | struct list_head *group_list; |
| 436 | bool run_gc; |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 437 | int read_inflight_gc, gc_group = 0, prev_group = 0; |
Hans Holmberg | 37ce33d | 2017-10-13 14:46:36 +0200 | [diff] [blame] | 438 | |
| 439 | pblk_gc_free_full_lines(pblk); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 440 | |
| 441 | run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl); |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 442 | if (!run_gc || (atomic_read(&gc->read_inflight_gc) >= PBLK_GC_L_QD)) |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 443 | return; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 444 | |
| 445 | next_gc_group: |
| 446 | group_list = l_mg->gc_lists[gc_group++]; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 447 | |
| 448 | do { |
| 449 | spin_lock(&l_mg->gc_lock); |
| 450 | if (list_empty(group_list)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 451 | spin_unlock(&l_mg->gc_lock); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 452 | break; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame] | 455 | line = pblk_gc_get_victim_line(pblk, group_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 456 | |
| 457 | spin_lock(&line->lock); |
| 458 | WARN_ON(line->state != PBLK_LINESTATE_CLOSED); |
| 459 | line->state = PBLK_LINESTATE_GC; |
Hans Holmberg | f293723 | 2018-10-09 13:11:53 +0200 | [diff] [blame^] | 460 | trace_pblk_line_state(pblk_disk_name(pblk), line->id, |
| 461 | line->state); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 462 | spin_unlock(&line->lock); |
| 463 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 464 | list_del(&line->list); |
| 465 | spin_unlock(&l_mg->gc_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 466 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 467 | spin_lock(&gc->r_lock); |
| 468 | list_add_tail(&line->list, &gc->r_list); |
| 469 | spin_unlock(&gc->r_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 470 | |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 471 | read_inflight_gc = atomic_inc_return(&gc->read_inflight_gc); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 472 | pblk_gc_reader_kick(gc); |
| 473 | |
| 474 | prev_group = 1; |
| 475 | |
| 476 | /* No need to queue up more GC lines than we can handle */ |
| 477 | run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl); |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 478 | if (!run_gc || read_inflight_gc >= PBLK_GC_L_QD) |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 479 | break; |
| 480 | } while (1); |
| 481 | |
| 482 | if (!prev_group && pblk->rl.rb_state > gc_group && |
| 483 | gc_group < PBLK_GC_NR_LISTS) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 484 | goto next_gc_group; |
| 485 | } |
| 486 | |
Kees Cook | 87c1d2d | 2017-10-17 21:10:19 -0700 | [diff] [blame] | 487 | static void pblk_gc_timer(struct timer_list *t) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 488 | { |
Kees Cook | 87c1d2d | 2017-10-17 21:10:19 -0700 | [diff] [blame] | 489 | struct pblk *pblk = from_timer(pblk, t, gc.gc_timer); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 490 | |
| 491 | pblk_gc_kick(pblk); |
| 492 | } |
| 493 | |
| 494 | static int pblk_gc_ts(void *data) |
| 495 | { |
| 496 | struct pblk *pblk = data; |
| 497 | |
| 498 | while (!kthread_should_stop()) { |
| 499 | pblk_gc_run(pblk); |
| 500 | set_current_state(TASK_INTERRUPTIBLE); |
| 501 | io_schedule(); |
| 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int pblk_gc_writer_ts(void *data) |
| 508 | { |
| 509 | struct pblk *pblk = data; |
| 510 | |
| 511 | while (!kthread_should_stop()) { |
| 512 | if (!pblk_gc_write(pblk)) |
| 513 | continue; |
| 514 | set_current_state(TASK_INTERRUPTIBLE); |
| 515 | io_schedule(); |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 521 | static int pblk_gc_reader_ts(void *data) |
| 522 | { |
| 523 | struct pblk *pblk = data; |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 524 | struct pblk_gc *gc = &pblk->gc; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 525 | |
| 526 | while (!kthread_should_stop()) { |
| 527 | if (!pblk_gc_read(pblk)) |
| 528 | continue; |
| 529 | set_current_state(TASK_INTERRUPTIBLE); |
| 530 | io_schedule(); |
| 531 | } |
| 532 | |
Matias Bjørling | 880eda5 | 2018-07-13 10:48:37 +0200 | [diff] [blame] | 533 | #ifdef CONFIG_NVM_PBLK_DEBUG |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 534 | pblk_info(pblk, "flushing gc pipeline, %d lines left\n", |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 535 | atomic_read(&gc->pipeline_gc)); |
| 536 | #endif |
| 537 | |
| 538 | do { |
| 539 | if (!atomic_read(&gc->pipeline_gc)) |
| 540 | break; |
| 541 | |
| 542 | schedule(); |
| 543 | } while (1); |
| 544 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 548 | static void pblk_gc_start(struct pblk *pblk) |
| 549 | { |
| 550 | pblk->gc.gc_active = 1; |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 551 | pblk_debug(pblk, "gc start\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 552 | } |
| 553 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 554 | void pblk_gc_should_start(struct pblk *pblk) |
| 555 | { |
| 556 | struct pblk_gc *gc = &pblk->gc; |
| 557 | |
Hans Holmberg | 3e3a5b8 | 2017-10-13 14:46:34 +0200 | [diff] [blame] | 558 | if (gc->gc_enabled && !gc->gc_active) { |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 559 | pblk_gc_start(pblk); |
Hans Holmberg | 3e3a5b8 | 2017-10-13 14:46:34 +0200 | [diff] [blame] | 560 | pblk_gc_kick(pblk); |
| 561 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 562 | } |
| 563 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 564 | void pblk_gc_should_stop(struct pblk *pblk) |
| 565 | { |
| 566 | struct pblk_gc *gc = &pblk->gc; |
| 567 | |
| 568 | if (gc->gc_active && !gc->gc_forced) |
Hans Holmberg | aed49e1 | 2018-01-05 14:16:12 +0100 | [diff] [blame] | 569 | gc->gc_active = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Hans Holmberg | 03661b5 | 2017-10-13 14:46:37 +0200 | [diff] [blame] | 572 | void pblk_gc_should_kick(struct pblk *pblk) |
| 573 | { |
| 574 | pblk_rl_update_rates(&pblk->rl); |
| 575 | } |
| 576 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 577 | void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled, |
| 578 | int *gc_active) |
| 579 | { |
| 580 | struct pblk_gc *gc = &pblk->gc; |
| 581 | |
| 582 | spin_lock(&gc->lock); |
| 583 | *gc_enabled = gc->gc_enabled; |
| 584 | *gc_active = gc->gc_active; |
| 585 | spin_unlock(&gc->lock); |
| 586 | } |
| 587 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 588 | int pblk_gc_sysfs_force(struct pblk *pblk, int force) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 589 | { |
| 590 | struct pblk_gc *gc = &pblk->gc; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 591 | |
| 592 | if (force < 0 || force > 1) |
| 593 | return -EINVAL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 594 | |
| 595 | spin_lock(&gc->lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 596 | gc->gc_forced = force; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 597 | |
| 598 | if (force) |
| 599 | gc->gc_enabled = 1; |
| 600 | else |
| 601 | gc->gc_enabled = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 602 | spin_unlock(&gc->lock); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 603 | |
| 604 | pblk_gc_should_start(pblk); |
| 605 | |
| 606 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | int pblk_gc_init(struct pblk *pblk) |
| 610 | { |
| 611 | struct pblk_gc *gc = &pblk->gc; |
| 612 | int ret; |
| 613 | |
| 614 | gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts"); |
| 615 | if (IS_ERR(gc->gc_ts)) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 616 | pblk_err(pblk, "could not allocate GC main kthread\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 617 | return PTR_ERR(gc->gc_ts); |
| 618 | } |
| 619 | |
| 620 | gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk, |
| 621 | "pblk-gc-writer-ts"); |
| 622 | if (IS_ERR(gc->gc_writer_ts)) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 623 | pblk_err(pblk, "could not allocate GC writer kthread\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 624 | ret = PTR_ERR(gc->gc_writer_ts); |
| 625 | goto fail_free_main_kthread; |
| 626 | } |
| 627 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 628 | gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk, |
| 629 | "pblk-gc-reader-ts"); |
| 630 | if (IS_ERR(gc->gc_reader_ts)) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 631 | pblk_err(pblk, "could not allocate GC reader kthread\n"); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 632 | ret = PTR_ERR(gc->gc_reader_ts); |
| 633 | goto fail_free_writer_kthread; |
| 634 | } |
| 635 | |
Kees Cook | 87c1d2d | 2017-10-17 21:10:19 -0700 | [diff] [blame] | 636 | timer_setup(&gc->gc_timer, pblk_gc_timer, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 637 | mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS)); |
| 638 | |
| 639 | gc->gc_active = 0; |
| 640 | gc->gc_forced = 0; |
| 641 | gc->gc_enabled = 1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 642 | gc->w_entries = 0; |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 643 | atomic_set(&gc->read_inflight_gc, 0); |
| 644 | atomic_set(&gc->pipeline_gc, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 645 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 646 | /* Workqueue that reads valid sectors from a line and submit them to the |
| 647 | * GC writer to be recycled. |
| 648 | */ |
| 649 | gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq", |
| 650 | WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS); |
| 651 | if (!gc->gc_line_reader_wq) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 652 | pblk_err(pblk, "could not allocate GC line reader workqueue\n"); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 653 | ret = -ENOMEM; |
| 654 | goto fail_free_reader_kthread; |
| 655 | } |
| 656 | |
| 657 | /* Workqueue that prepare lines for GC */ |
| 658 | gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq", |
| 659 | WQ_MEM_RECLAIM | WQ_UNBOUND, 1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 660 | if (!gc->gc_reader_wq) { |
Matias Bjørling | 4e495a4 | 2018-07-13 10:48:42 +0200 | [diff] [blame] | 661 | pblk_err(pblk, "could not allocate GC reader workqueue\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 662 | ret = -ENOMEM; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 663 | goto fail_free_reader_line_wq; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | spin_lock_init(&gc->lock); |
| 667 | spin_lock_init(&gc->w_lock); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 668 | spin_lock_init(&gc->r_lock); |
| 669 | |
Javier González | 3627896 | 2017-10-13 14:46:11 +0200 | [diff] [blame] | 670 | sema_init(&gc->gc_sem, PBLK_GC_RQ_QD); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 671 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 672 | INIT_LIST_HEAD(&gc->w_list); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 673 | INIT_LIST_HEAD(&gc->r_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 674 | |
| 675 | return 0; |
| 676 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 677 | fail_free_reader_line_wq: |
| 678 | destroy_workqueue(gc->gc_line_reader_wq); |
| 679 | fail_free_reader_kthread: |
| 680 | kthread_stop(gc->gc_reader_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 681 | fail_free_writer_kthread: |
| 682 | kthread_stop(gc->gc_writer_ts); |
Dan Carpenter | 503ec94 | 2017-04-15 20:55:51 +0200 | [diff] [blame] | 683 | fail_free_main_kthread: |
| 684 | kthread_stop(gc->gc_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 685 | |
| 686 | return ret; |
| 687 | } |
| 688 | |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 689 | void pblk_gc_exit(struct pblk *pblk, bool graceful) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 690 | { |
| 691 | struct pblk_gc *gc = &pblk->gc; |
| 692 | |
Hans Holmberg | 3e3a5b8 | 2017-10-13 14:46:34 +0200 | [diff] [blame] | 693 | gc->gc_enabled = 0; |
| 694 | del_timer_sync(&gc->gc_timer); |
Hans Holmberg | aed49e1 | 2018-01-05 14:16:12 +0100 | [diff] [blame] | 695 | gc->gc_active = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 696 | |
| 697 | if (gc->gc_ts) |
| 698 | kthread_stop(gc->gc_ts); |
| 699 | |
Hans Holmberg | 1edebac | 2017-10-13 14:46:39 +0200 | [diff] [blame] | 700 | if (gc->gc_reader_ts) |
| 701 | kthread_stop(gc->gc_reader_ts); |
| 702 | |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 703 | if (graceful) { |
| 704 | flush_workqueue(gc->gc_reader_wq); |
| 705 | flush_workqueue(gc->gc_line_reader_wq); |
| 706 | } |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 707 | |
Javier González | a7c9e91 | 2018-06-01 15:04:24 +0200 | [diff] [blame] | 708 | destroy_workqueue(gc->gc_reader_wq); |
Dan Carpenter | 5565b0c | 2018-03-30 00:05:23 +0200 | [diff] [blame] | 709 | destroy_workqueue(gc->gc_line_reader_wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 710 | |
| 711 | if (gc->gc_writer_ts) |
| 712 | kthread_stop(gc->gc_writer_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 713 | } |