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