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