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" |
| 19 | #include <linux/delay.h> |
| 20 | |
| 21 | static void pblk_gc_free_gc_rq(struct pblk_gc_rq *gc_rq) |
| 22 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 23 | if (gc_rq->data) |
| 24 | vfree(gc_rq->data); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 25 | kfree(gc_rq); |
| 26 | } |
| 27 | |
| 28 | static int pblk_gc_write(struct pblk *pblk) |
| 29 | { |
| 30 | struct pblk_gc *gc = &pblk->gc; |
| 31 | struct pblk_gc_rq *gc_rq, *tgc_rq; |
| 32 | LIST_HEAD(w_list); |
| 33 | |
| 34 | spin_lock(&gc->w_lock); |
| 35 | if (list_empty(&gc->w_list)) { |
| 36 | spin_unlock(&gc->w_lock); |
| 37 | return 1; |
| 38 | } |
| 39 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 40 | list_cut_position(&w_list, &gc->w_list, gc->w_list.prev); |
| 41 | gc->w_entries = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 42 | spin_unlock(&gc->w_lock); |
| 43 | |
| 44 | 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^] | 45 | pblk_write_gc_to_cache(pblk, gc_rq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 46 | list_del(&gc_rq->list); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 47 | kref_put(&gc_rq->line->ref, pblk_line_put); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 48 | pblk_gc_free_gc_rq(gc_rq); |
| 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static void pblk_gc_writer_kick(struct pblk_gc *gc) |
| 55 | { |
| 56 | wake_up_process(gc->gc_writer_ts); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Responsible for managing all memory related to a gc request. Also in case of |
| 61 | * failure |
| 62 | */ |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 63 | static int pblk_gc_move_valid_secs(struct pblk *pblk, struct pblk_gc_rq *gc_rq) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 64 | { |
| 65 | struct nvm_tgt_dev *dev = pblk->dev; |
| 66 | struct nvm_geo *geo = &dev->geo; |
| 67 | struct pblk_gc *gc = &pblk->gc; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 68 | struct pblk_line *line = gc_rq->line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 69 | void *data; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 70 | int ret = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 71 | |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 72 | data = vmalloc(gc_rq->nr_secs * geo->sec_size); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 73 | if (!data) { |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 74 | ret = -ENOMEM; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 75 | goto fail; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 78 | gc_rq->data = data; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 79 | |
| 80 | /* Read from GC victim block */ |
| 81 | ret = pblk_submit_read_gc(pblk, gc_rq); |
| 82 | if (ret) |
| 83 | goto fail; |
| 84 | |
| 85 | if (!gc_rq->secs_to_gc) |
| 86 | goto fail; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 87 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 88 | retry: |
| 89 | spin_lock(&gc->w_lock); |
Javier González | 3627896 | 2017-10-13 14:46:11 +0200 | [diff] [blame] | 90 | if (gc->w_entries >= PBLK_GC_RQ_QD) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 91 | spin_unlock(&gc->w_lock); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 92 | pblk_gc_writer_kick(&pblk->gc); |
| 93 | usleep_range(128, 256); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 94 | goto retry; |
| 95 | } |
| 96 | gc->w_entries++; |
| 97 | list_add_tail(&gc_rq->list, &gc->w_list); |
| 98 | spin_unlock(&gc->w_lock); |
| 99 | |
| 100 | pblk_gc_writer_kick(&pblk->gc); |
| 101 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 102 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 103 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 104 | fail: |
| 105 | pblk_gc_free_gc_rq(gc_rq); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 106 | kref_put(&line->ref, pblk_line_put); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | static void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line) |
| 111 | { |
| 112 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 113 | struct list_head *move_list; |
| 114 | |
| 115 | spin_lock(&line->lock); |
| 116 | WARN_ON(line->state != PBLK_LINESTATE_GC); |
| 117 | line->state = PBLK_LINESTATE_CLOSED; |
| 118 | move_list = pblk_line_gc_list(pblk, line); |
| 119 | spin_unlock(&line->lock); |
| 120 | |
| 121 | if (move_list) { |
| 122 | spin_lock(&l_mg->gc_lock); |
| 123 | list_add_tail(&line->list, move_list); |
| 124 | spin_unlock(&l_mg->gc_lock); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void pblk_gc_line_ws(struct work_struct *work) |
| 129 | { |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 130 | struct pblk_line_ws *gc_rq_ws = container_of(work, |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 131 | struct pblk_line_ws, ws); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 132 | struct pblk *pblk = gc_rq_ws->pblk; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 133 | struct pblk_gc *gc = &pblk->gc; |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 134 | struct pblk_line *line = gc_rq_ws->line; |
| 135 | struct pblk_gc_rq *gc_rq = gc_rq_ws->priv; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 136 | |
| 137 | up(&gc->gc_sem); |
| 138 | |
| 139 | if (pblk_gc_move_valid_secs(pblk, gc_rq)) { |
| 140 | pr_err("pblk: could not GC all sectors: line:%d (%d/%d)\n", |
| 141 | line->id, *line->vsc, |
| 142 | gc_rq->nr_secs); |
| 143 | } |
| 144 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 145 | kfree(gc_rq_ws); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static void pblk_gc_line_prepare_ws(struct work_struct *work) |
| 149 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 150 | struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws, |
| 151 | ws); |
| 152 | struct pblk *pblk = line_ws->pblk; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 153 | struct pblk_line *line = line_ws->line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 154 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 155 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 156 | struct pblk_gc *gc = &pblk->gc; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 157 | struct line_emeta *emeta_buf; |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 158 | struct pblk_line_ws *gc_rq_ws; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 159 | struct pblk_gc_rq *gc_rq; |
| 160 | __le64 *lba_list; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 161 | unsigned long *invalid_bitmap; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 162 | int sec_left, nr_secs, bit; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 163 | int ret; |
| 164 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 165 | invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_KERNEL); |
| 166 | if (!invalid_bitmap) { |
| 167 | pr_err("pblk: could not allocate GC invalid bitmap\n"); |
| 168 | goto fail_free_ws; |
| 169 | } |
| 170 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 171 | emeta_buf = pblk_malloc(lm->emeta_len[0], l_mg->emeta_alloc_type, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 172 | GFP_KERNEL); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 173 | if (!emeta_buf) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 174 | pr_err("pblk: cannot use GC emeta\n"); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 175 | goto fail_free_bitmap; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 178 | ret = pblk_line_read_emeta(pblk, line, emeta_buf); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 179 | if (ret) { |
| 180 | pr_err("pblk: line %d read emeta failed (%d)\n", line->id, ret); |
| 181 | goto fail_free_emeta; |
| 182 | } |
| 183 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 184 | /* If this read fails, it means that emeta is corrupted. For now, leave |
| 185 | * the line untouched. TODO: Implement a recovery routine that scans and |
| 186 | * moves all sectors on the line. |
| 187 | */ |
| 188 | lba_list = pblk_recov_get_lba_list(pblk, emeta_buf); |
| 189 | if (!lba_list) { |
| 190 | pr_err("pblk: could not interpret emeta (line %d)\n", line->id); |
| 191 | goto fail_free_emeta; |
| 192 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 193 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 194 | spin_lock(&line->lock); |
| 195 | bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 196 | sec_left = pblk_line_vsc(line); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 197 | spin_unlock(&line->lock); |
| 198 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 199 | if (sec_left < 0) { |
| 200 | pr_err("pblk: corrupted GC line (%d)\n", line->id); |
| 201 | goto fail_free_emeta; |
| 202 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 203 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 204 | bit = -1; |
| 205 | next_rq: |
| 206 | gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL); |
| 207 | if (!gc_rq) |
| 208 | goto fail_free_emeta; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 209 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 210 | nr_secs = 0; |
| 211 | do { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 212 | bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line, |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 213 | bit + 1); |
| 214 | if (bit > line->emeta_ssec) |
| 215 | break; |
| 216 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 217 | gc_rq->paddr_list[nr_secs] = bit; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 218 | gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]); |
| 219 | } while (nr_secs < pblk->max_write_pgs); |
| 220 | |
| 221 | if (unlikely(!nr_secs)) { |
| 222 | kfree(gc_rq); |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | gc_rq->nr_secs = nr_secs; |
| 227 | gc_rq->line = line; |
| 228 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 229 | gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL); |
| 230 | if (!gc_rq_ws) |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 231 | goto fail_free_gc_rq; |
| 232 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 233 | gc_rq_ws->pblk = pblk; |
| 234 | gc_rq_ws->line = line; |
| 235 | gc_rq_ws->priv = gc_rq; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 236 | |
| 237 | down(&gc->gc_sem); |
| 238 | kref_get(&line->ref); |
| 239 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 240 | INIT_WORK(&gc_rq_ws->ws, pblk_gc_line_ws); |
| 241 | queue_work(gc->gc_line_reader_wq, &gc_rq_ws->ws); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 242 | |
| 243 | sec_left -= nr_secs; |
| 244 | if (sec_left > 0) |
| 245 | goto next_rq; |
| 246 | |
| 247 | out: |
| 248 | pblk_mfree(emeta_buf, l_mg->emeta_alloc_type); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 249 | kfree(line_ws); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 250 | kfree(invalid_bitmap); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 251 | |
| 252 | kref_put(&line->ref, pblk_line_put); |
| 253 | atomic_dec(&gc->inflight_gc); |
| 254 | |
| 255 | return; |
| 256 | |
| 257 | fail_free_gc_rq: |
| 258 | kfree(gc_rq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 259 | fail_free_emeta: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 260 | pblk_mfree(emeta_buf, l_mg->emeta_alloc_type); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame^] | 261 | fail_free_bitmap: |
| 262 | kfree(invalid_bitmap); |
| 263 | fail_free_ws: |
| 264 | kfree(line_ws); |
| 265 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 266 | pblk_put_line_back(pblk, line); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 267 | kref_put(&line->ref, pblk_line_put); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 268 | atomic_dec(&gc->inflight_gc); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 269 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 270 | pr_err("pblk: Failed to GC line %d\n", line->id); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 273 | 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] | 274 | { |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 275 | struct pblk_gc *gc = &pblk->gc; |
| 276 | struct pblk_line_ws *line_ws; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 277 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 278 | pr_debug("pblk: line '%d' being reclaimed for GC\n", line->id); |
| 279 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 280 | line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 281 | if (!line_ws) |
| 282 | return -ENOMEM; |
| 283 | |
| 284 | line_ws->pblk = pblk; |
| 285 | line_ws->line = line; |
| 286 | |
| 287 | INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws); |
| 288 | queue_work(gc->gc_reader_wq, &line_ws->ws); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static int pblk_gc_read(struct pblk *pblk) |
| 294 | { |
| 295 | struct pblk_gc *gc = &pblk->gc; |
| 296 | struct pblk_line *line; |
| 297 | |
| 298 | spin_lock(&gc->r_lock); |
| 299 | if (list_empty(&gc->r_list)) { |
| 300 | spin_unlock(&gc->r_lock); |
| 301 | return 1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 302 | } |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 303 | |
| 304 | line = list_first_entry(&gc->r_list, struct pblk_line, list); |
| 305 | list_del(&line->list); |
| 306 | spin_unlock(&gc->r_lock); |
| 307 | |
| 308 | pblk_gc_kick(pblk); |
| 309 | |
| 310 | if (pblk_gc_line(pblk, line)) |
| 311 | pr_err("pblk: failed to GC line %d\n", line->id); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static void pblk_gc_reader_kick(struct pblk_gc *gc) |
| 317 | { |
| 318 | wake_up_process(gc->gc_reader_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame] | 321 | static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk, |
| 322 | struct list_head *group_list) |
| 323 | { |
| 324 | struct pblk_line *line, *victim; |
Javier González | f417aa0 | 2017-06-30 17:56:34 +0200 | [diff] [blame] | 325 | int line_vsc, victim_vsc; |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame] | 326 | |
| 327 | victim = list_first_entry(group_list, struct pblk_line, list); |
| 328 | list_for_each_entry(line, group_list, list) { |
Javier González | f417aa0 | 2017-06-30 17:56:34 +0200 | [diff] [blame] | 329 | line_vsc = le32_to_cpu(*line->vsc); |
| 330 | victim_vsc = le32_to_cpu(*victim->vsc); |
| 331 | if (line_vsc < victim_vsc) |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame] | 332 | victim = line; |
| 333 | } |
| 334 | |
| 335 | return victim; |
| 336 | } |
| 337 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 338 | static bool pblk_gc_should_run(struct pblk_gc *gc, struct pblk_rl *rl) |
| 339 | { |
| 340 | unsigned int nr_blocks_free, nr_blocks_need; |
| 341 | |
| 342 | nr_blocks_need = pblk_rl_high_thrs(rl); |
| 343 | nr_blocks_free = pblk_rl_nr_free_blks(rl); |
| 344 | |
| 345 | /* This is not critical, no need to take lock here */ |
| 346 | return ((gc->gc_active) && (nr_blocks_need > nr_blocks_free)); |
| 347 | } |
| 348 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 349 | /* |
| 350 | * Lines with no valid sectors will be returned to the free list immediately. If |
| 351 | * GC is activated - either because the free block count is under the determined |
| 352 | * threshold, or because it is being forced from user space - only lines with a |
| 353 | * high count of invalid sectors will be recycled. |
| 354 | */ |
| 355 | static void pblk_gc_run(struct pblk *pblk) |
| 356 | { |
| 357 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 358 | struct pblk_gc *gc = &pblk->gc; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 359 | struct pblk_line *line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 360 | struct list_head *group_list; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 361 | bool run_gc; |
| 362 | int inflight_gc, gc_group = 0, prev_group = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 363 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 364 | do { |
| 365 | spin_lock(&l_mg->gc_lock); |
| 366 | if (list_empty(&l_mg->gc_full_list)) { |
| 367 | spin_unlock(&l_mg->gc_lock); |
| 368 | break; |
| 369 | } |
| 370 | |
| 371 | line = list_first_entry(&l_mg->gc_full_list, |
| 372 | struct pblk_line, list); |
| 373 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 374 | spin_lock(&line->lock); |
| 375 | WARN_ON(line->state != PBLK_LINESTATE_CLOSED); |
| 376 | line->state = PBLK_LINESTATE_GC; |
| 377 | spin_unlock(&line->lock); |
| 378 | |
| 379 | list_del(&line->list); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 380 | spin_unlock(&l_mg->gc_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 381 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 382 | kref_put(&line->ref, pblk_line_put); |
| 383 | } while (1); |
| 384 | |
| 385 | run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl); |
| 386 | if (!run_gc || (atomic_read(&gc->inflight_gc) >= PBLK_GC_L_QD)) |
| 387 | return; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 388 | |
| 389 | next_gc_group: |
| 390 | group_list = l_mg->gc_lists[gc_group++]; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 391 | |
| 392 | do { |
| 393 | spin_lock(&l_mg->gc_lock); |
| 394 | if (list_empty(group_list)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 395 | spin_unlock(&l_mg->gc_lock); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 396 | break; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame] | 399 | line = pblk_gc_get_victim_line(pblk, group_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 400 | |
| 401 | spin_lock(&line->lock); |
| 402 | WARN_ON(line->state != PBLK_LINESTATE_CLOSED); |
| 403 | line->state = PBLK_LINESTATE_GC; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 404 | spin_unlock(&line->lock); |
| 405 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 406 | list_del(&line->list); |
| 407 | spin_unlock(&l_mg->gc_lock); |
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 | spin_lock(&gc->r_lock); |
| 410 | list_add_tail(&line->list, &gc->r_list); |
| 411 | spin_unlock(&gc->r_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 412 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 413 | inflight_gc = atomic_inc_return(&gc->inflight_gc); |
| 414 | pblk_gc_reader_kick(gc); |
| 415 | |
| 416 | prev_group = 1; |
| 417 | |
| 418 | /* No need to queue up more GC lines than we can handle */ |
| 419 | run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl); |
| 420 | if (!run_gc || inflight_gc >= PBLK_GC_L_QD) |
| 421 | break; |
| 422 | } while (1); |
| 423 | |
| 424 | if (!prev_group && pblk->rl.rb_state > gc_group && |
| 425 | gc_group < PBLK_GC_NR_LISTS) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 426 | goto next_gc_group; |
| 427 | } |
| 428 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 429 | void pblk_gc_kick(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 430 | { |
| 431 | struct pblk_gc *gc = &pblk->gc; |
| 432 | |
| 433 | wake_up_process(gc->gc_ts); |
| 434 | pblk_gc_writer_kick(gc); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 435 | pblk_gc_reader_kick(gc); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 436 | mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS)); |
| 437 | } |
| 438 | |
| 439 | static void pblk_gc_timer(unsigned long data) |
| 440 | { |
| 441 | struct pblk *pblk = (struct pblk *)data; |
| 442 | |
| 443 | pblk_gc_kick(pblk); |
| 444 | } |
| 445 | |
| 446 | static int pblk_gc_ts(void *data) |
| 447 | { |
| 448 | struct pblk *pblk = data; |
| 449 | |
| 450 | while (!kthread_should_stop()) { |
| 451 | pblk_gc_run(pblk); |
| 452 | set_current_state(TASK_INTERRUPTIBLE); |
| 453 | io_schedule(); |
| 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int pblk_gc_writer_ts(void *data) |
| 460 | { |
| 461 | struct pblk *pblk = data; |
| 462 | |
| 463 | while (!kthread_should_stop()) { |
| 464 | if (!pblk_gc_write(pblk)) |
| 465 | continue; |
| 466 | set_current_state(TASK_INTERRUPTIBLE); |
| 467 | io_schedule(); |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 473 | static int pblk_gc_reader_ts(void *data) |
| 474 | { |
| 475 | struct pblk *pblk = data; |
| 476 | |
| 477 | while (!kthread_should_stop()) { |
| 478 | if (!pblk_gc_read(pblk)) |
| 479 | continue; |
| 480 | set_current_state(TASK_INTERRUPTIBLE); |
| 481 | io_schedule(); |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 487 | static void pblk_gc_start(struct pblk *pblk) |
| 488 | { |
| 489 | pblk->gc.gc_active = 1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 490 | pr_debug("pblk: gc start\n"); |
| 491 | } |
| 492 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 493 | void pblk_gc_should_start(struct pblk *pblk) |
| 494 | { |
| 495 | struct pblk_gc *gc = &pblk->gc; |
| 496 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 497 | if (gc->gc_enabled && !gc->gc_active) |
| 498 | pblk_gc_start(pblk); |
| 499 | |
| 500 | pblk_gc_kick(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* |
| 504 | * If flush_wq == 1 then no lock should be held by the caller since |
| 505 | * flush_workqueue can sleep |
| 506 | */ |
| 507 | static void pblk_gc_stop(struct pblk *pblk, int flush_wq) |
| 508 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 509 | pblk->gc.gc_active = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 510 | pr_debug("pblk: gc stop\n"); |
| 511 | } |
| 512 | |
| 513 | void pblk_gc_should_stop(struct pblk *pblk) |
| 514 | { |
| 515 | struct pblk_gc *gc = &pblk->gc; |
| 516 | |
| 517 | if (gc->gc_active && !gc->gc_forced) |
| 518 | pblk_gc_stop(pblk, 0); |
| 519 | } |
| 520 | |
| 521 | void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled, |
| 522 | int *gc_active) |
| 523 | { |
| 524 | struct pblk_gc *gc = &pblk->gc; |
| 525 | |
| 526 | spin_lock(&gc->lock); |
| 527 | *gc_enabled = gc->gc_enabled; |
| 528 | *gc_active = gc->gc_active; |
| 529 | spin_unlock(&gc->lock); |
| 530 | } |
| 531 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 532 | int pblk_gc_sysfs_force(struct pblk *pblk, int force) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 533 | { |
| 534 | struct pblk_gc *gc = &pblk->gc; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 535 | |
| 536 | if (force < 0 || force > 1) |
| 537 | return -EINVAL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 538 | |
| 539 | spin_lock(&gc->lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 540 | gc->gc_forced = force; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 541 | |
| 542 | if (force) |
| 543 | gc->gc_enabled = 1; |
| 544 | else |
| 545 | gc->gc_enabled = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 546 | spin_unlock(&gc->lock); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 547 | |
| 548 | pblk_gc_should_start(pblk); |
| 549 | |
| 550 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | int pblk_gc_init(struct pblk *pblk) |
| 554 | { |
| 555 | struct pblk_gc *gc = &pblk->gc; |
| 556 | int ret; |
| 557 | |
| 558 | gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts"); |
| 559 | if (IS_ERR(gc->gc_ts)) { |
| 560 | pr_err("pblk: could not allocate GC main kthread\n"); |
| 561 | return PTR_ERR(gc->gc_ts); |
| 562 | } |
| 563 | |
| 564 | gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk, |
| 565 | "pblk-gc-writer-ts"); |
| 566 | if (IS_ERR(gc->gc_writer_ts)) { |
| 567 | pr_err("pblk: could not allocate GC writer kthread\n"); |
| 568 | ret = PTR_ERR(gc->gc_writer_ts); |
| 569 | goto fail_free_main_kthread; |
| 570 | } |
| 571 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 572 | gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk, |
| 573 | "pblk-gc-reader-ts"); |
| 574 | if (IS_ERR(gc->gc_reader_ts)) { |
| 575 | pr_err("pblk: could not allocate GC reader kthread\n"); |
| 576 | ret = PTR_ERR(gc->gc_reader_ts); |
| 577 | goto fail_free_writer_kthread; |
| 578 | } |
| 579 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 580 | setup_timer(&gc->gc_timer, pblk_gc_timer, (unsigned long)pblk); |
| 581 | mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS)); |
| 582 | |
| 583 | gc->gc_active = 0; |
| 584 | gc->gc_forced = 0; |
| 585 | gc->gc_enabled = 1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 586 | gc->w_entries = 0; |
| 587 | atomic_set(&gc->inflight_gc, 0); |
| 588 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 589 | /* Workqueue that reads valid sectors from a line and submit them to the |
| 590 | * GC writer to be recycled. |
| 591 | */ |
| 592 | gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq", |
| 593 | WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS); |
| 594 | if (!gc->gc_line_reader_wq) { |
| 595 | pr_err("pblk: could not allocate GC line reader workqueue\n"); |
| 596 | ret = -ENOMEM; |
| 597 | goto fail_free_reader_kthread; |
| 598 | } |
| 599 | |
| 600 | /* Workqueue that prepare lines for GC */ |
| 601 | gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq", |
| 602 | WQ_MEM_RECLAIM | WQ_UNBOUND, 1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 603 | if (!gc->gc_reader_wq) { |
| 604 | pr_err("pblk: could not allocate GC reader workqueue\n"); |
| 605 | ret = -ENOMEM; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 606 | goto fail_free_reader_line_wq; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | spin_lock_init(&gc->lock); |
| 610 | spin_lock_init(&gc->w_lock); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 611 | spin_lock_init(&gc->r_lock); |
| 612 | |
Javier González | 3627896 | 2017-10-13 14:46:11 +0200 | [diff] [blame] | 613 | sema_init(&gc->gc_sem, PBLK_GC_RQ_QD); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 614 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 615 | INIT_LIST_HEAD(&gc->w_list); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 616 | INIT_LIST_HEAD(&gc->r_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 617 | |
| 618 | return 0; |
| 619 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 620 | fail_free_reader_line_wq: |
| 621 | destroy_workqueue(gc->gc_line_reader_wq); |
| 622 | fail_free_reader_kthread: |
| 623 | kthread_stop(gc->gc_reader_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 624 | fail_free_writer_kthread: |
| 625 | kthread_stop(gc->gc_writer_ts); |
Dan Carpenter | 503ec94 | 2017-04-15 20:55:51 +0200 | [diff] [blame] | 626 | fail_free_main_kthread: |
| 627 | kthread_stop(gc->gc_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | void pblk_gc_exit(struct pblk *pblk) |
| 633 | { |
| 634 | struct pblk_gc *gc = &pblk->gc; |
| 635 | |
| 636 | flush_workqueue(gc->gc_reader_wq); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 637 | flush_workqueue(gc->gc_line_reader_wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 638 | |
| 639 | del_timer(&gc->gc_timer); |
| 640 | pblk_gc_stop(pblk, 1); |
| 641 | |
| 642 | if (gc->gc_ts) |
| 643 | kthread_stop(gc->gc_ts); |
| 644 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 645 | if (gc->gc_reader_wq) |
| 646 | destroy_workqueue(gc->gc_reader_wq); |
| 647 | |
| 648 | if (gc->gc_line_reader_wq) |
| 649 | destroy_workqueue(gc->gc_line_reader_wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 650 | |
| 651 | if (gc->gc_writer_ts) |
| 652 | kthread_stop(gc->gc_writer_ts); |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 653 | |
| 654 | if (gc->gc_reader_ts) |
| 655 | kthread_stop(gc->gc_reader_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 656 | } |