blob: 63ee205b41c477f9ebc70374f6cf208915e6c4f1 [file] [log] [blame]
Javier González02a15202018-10-09 13:12:06 +02001// SPDX-License-Identifier: GPL-2.0
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002/*
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 Holmbergf2937232018-10-09 13:11:53 +020020#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020021#include <linux/delay.h>
22
Hans Holmbergf2937232018-10-09 13:11:53 +020023
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020024static void pblk_gc_free_gc_rq(struct pblk_gc_rq *gc_rq)
25{
Javier Gonzálezd3401212017-10-13 14:46:14 +020026 if (gc_rq->data)
27 vfree(gc_rq->data);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020028 kfree(gc_rq);
29}
30
31static 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álezb20ba1b2017-06-26 11:57:27 +020043 list_cut_position(&w_list, &gc->w_list, gc->w_list.prev);
44 gc->w_entries = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020045 spin_unlock(&gc->w_lock);
46
47 list_for_each_entry_safe(gc_rq, tgc_rq, &w_list, list) {
Javier Gonzálezd3401212017-10-13 14:46:14 +020048 pblk_write_gc_to_cache(pblk, gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020049 list_del(&gc_rq->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020050 kref_put(&gc_rq->line->ref, pblk_line_put);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020051 pblk_gc_free_gc_rq(gc_rq);
52 }
53
54 return 0;
55}
56
57static void pblk_gc_writer_kick(struct pblk_gc *gc)
58{
59 wake_up_process(gc->gc_writer_ts);
60}
61
Igor Konopkof2e02452019-05-04 20:38:05 +020062void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020063{
64 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
65 struct list_head *move_list;
66
Igor Konopko4ca88522019-05-04 20:37:51 +020067 spin_lock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020068 spin_lock(&line->lock);
69 WARN_ON(line->state != PBLK_LINESTATE_GC);
70 line->state = PBLK_LINESTATE_CLOSED;
Hans Holmbergf2937232018-10-09 13:11:53 +020071 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
72 line->state);
Igor Konopko4ca88522019-05-04 20:37:51 +020073
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áleza4bd2172017-04-15 20:55:50 +020080 move_list = pblk_line_gc_list(pblk, line);
81 spin_unlock(&line->lock);
Igor Konopko4ca88522019-05-04 20:37:51 +020082 list_add_tail(&line->list, move_list);
83 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020084}
85
86static void pblk_gc_line_ws(struct work_struct *work)
87{
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020088 struct pblk_line_ws *gc_rq_ws = container_of(work,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020089 struct pblk_line_ws, ws);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020090 struct pblk *pblk = gc_rq_ws->pblk;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020091 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020092 struct pblk_line *line = gc_rq_ws->line;
93 struct pblk_gc_rq *gc_rq = gc_rq_ws->priv;
Javier González2a19b102017-10-13 14:46:15 +020094 int ret;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020095
96 up(&gc->gc_sem);
97
Javier González2a19b102017-10-13 14:46:15 +020098 /* Read from GC victim block */
99 ret = pblk_submit_read_gc(pblk, gc_rq);
100 if (ret) {
Igor Konopkof2e02452019-05-04 20:38:05 +0200101 line->w_err_gc->has_gc_err = 1;
Javier González2a19b102017-10-13 14:46:15 +0200102 goto out;
103 }
104
105 if (!gc_rq->secs_to_gc)
106 goto out;
107
108retry:
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
125out:
126 pblk_gc_free_gc_rq(gc_rq);
127 kref_put(&line->ref, pblk_line_put);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200128 kfree(gc_rq_ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200129}
130
Hans Holmberg48b8d202018-06-01 16:41:06 +0200131static __le64 *get_lba_list_from_emeta(struct pblk *pblk,
132 struct pblk_line *line)
133{
134 struct line_emeta *emeta_buf;
135 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
136 struct pblk_line_meta *lm = &pblk->lm;
137 unsigned int lba_list_size = lm->emeta_len[2];
138 __le64 *lba_list;
139 int ret;
140
141 emeta_buf = pblk_malloc(lm->emeta_len[0],
142 l_mg->emeta_alloc_type, GFP_KERNEL);
143 if (!emeta_buf)
144 return NULL;
145
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200146 ret = pblk_line_emeta_read(pblk, line, emeta_buf);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200147 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200148 pblk_err(pblk, "line %d read emeta failed (%d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200149 line->id, ret);
150 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
151 return NULL;
152 }
153
154 /* If this read fails, it means that emeta is corrupted.
155 * For now, leave the line untouched.
156 * TODO: Implement a recovery routine that scans and moves
157 * all sectors on the line.
158 */
159
160 ret = pblk_recov_check_emeta(pblk, emeta_buf);
161 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200162 pblk_err(pblk, "inconsistent emeta (line %d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200163 line->id);
164 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
165 return NULL;
166 }
167
168 lba_list = pblk_malloc(lba_list_size,
169 l_mg->emeta_alloc_type, GFP_KERNEL);
170 if (lba_list)
171 memcpy(lba_list, emeta_to_lbas(pblk, emeta_buf), lba_list_size);
172
173 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
174
175 return lba_list;
176}
177
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200178static void pblk_gc_line_prepare_ws(struct work_struct *work)
179{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200180 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
181 ws);
182 struct pblk *pblk = line_ws->pblk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200183 struct pblk_line *line = line_ws->line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200184 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
185 struct pblk_line_meta *lm = &pblk->lm;
Igor Konopkod3785612019-05-04 20:37:50 +0200186 struct nvm_tgt_dev *dev = pblk->dev;
187 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200188 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200189 struct pblk_line_ws *gc_rq_ws;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200190 struct pblk_gc_rq *gc_rq;
191 __le64 *lba_list;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200192 unsigned long *invalid_bitmap;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200193 int sec_left, nr_secs, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200194
Javier Gonzálezd3401212017-10-13 14:46:14 +0200195 invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_KERNEL);
Markus Elfring5da84cf2018-03-30 00:04:48 +0200196 if (!invalid_bitmap)
Javier Gonzálezd3401212017-10-13 14:46:14 +0200197 goto fail_free_ws;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200198
Hans Holmberg48b8d202018-06-01 16:41:06 +0200199 if (line->w_err_gc->has_write_err) {
200 lba_list = line->w_err_gc->lba_list;
201 line->w_err_gc->lba_list = NULL;
202 } else {
203 lba_list = get_lba_list_from_emeta(pblk, line);
204 if (!lba_list) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200205 pblk_err(pblk, "could not interpret emeta (line %d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200206 line->id);
Colin Ian King0ec69372018-06-05 16:14:56 +0100207 goto fail_free_invalid_bitmap;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200208 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200209 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200210
Javier Gonzálezd3401212017-10-13 14:46:14 +0200211 spin_lock(&line->lock);
212 bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200213 sec_left = pblk_line_vsc(line);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200214 spin_unlock(&line->lock);
215
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200216 if (sec_left < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200217 pblk_err(pblk, "corrupted GC line (%d)\n", line->id);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200218 goto fail_free_lba_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200219 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200220
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200221 bit = -1;
222next_rq:
223 gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL);
224 if (!gc_rq)
Hans Holmberg48b8d202018-06-01 16:41:06 +0200225 goto fail_free_lba_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200226
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200227 nr_secs = 0;
228 do {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200229 bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200230 bit + 1);
231 if (bit > line->emeta_ssec)
232 break;
233
Javier Gonzálezd3401212017-10-13 14:46:14 +0200234 gc_rq->paddr_list[nr_secs] = bit;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200235 gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]);
236 } while (nr_secs < pblk->max_write_pgs);
237
238 if (unlikely(!nr_secs)) {
239 kfree(gc_rq);
240 goto out;
241 }
242
243 gc_rq->nr_secs = nr_secs;
244 gc_rq->line = line;
245
Igor Konopkod3785612019-05-04 20:37:50 +0200246 gc_rq->data = vmalloc(array_size(gc_rq->nr_secs, geo->csecs));
247 if (!gc_rq->data)
248 goto fail_free_gc_rq;
249
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200250 gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
251 if (!gc_rq_ws)
Igor Konopkod3785612019-05-04 20:37:50 +0200252 goto fail_free_gc_data;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200253
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200254 gc_rq_ws->pblk = pblk;
255 gc_rq_ws->line = line;
256 gc_rq_ws->priv = gc_rq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200257
Javier González8da10cc2017-10-13 14:46:45 +0200258 /* 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álezb20ba1b2017-06-26 11:57:27 +0200265 kref_get(&line->ref);
266
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200267 INIT_WORK(&gc_rq_ws->ws, pblk_gc_line_ws);
268 queue_work(gc->gc_line_reader_wq, &gc_rq_ws->ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200269
270 sec_left -= nr_secs;
271 if (sec_left > 0)
272 goto next_rq;
273
274out:
Hans Holmberg48b8d202018-06-01 16:41:06 +0200275 pblk_mfree(lba_list, l_mg->emeta_alloc_type);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200276 kfree(line_ws);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200277 kfree(invalid_bitmap);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200278
279 kref_put(&line->ref, pblk_line_put);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200280 atomic_dec(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200281
282 return;
283
Igor Konopkod3785612019-05-04 20:37:50 +0200284fail_free_gc_data:
285 vfree(gc_rq->data);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200286fail_free_gc_rq:
287 kfree(gc_rq);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200288fail_free_lba_list:
289 pblk_mfree(lba_list, l_mg->emeta_alloc_type);
Colin Ian King0ec69372018-06-05 16:14:56 +0100290fail_free_invalid_bitmap:
Javier Gonzálezd3401212017-10-13 14:46:14 +0200291 kfree(invalid_bitmap);
292fail_free_ws:
293 kfree(line_ws);
294
Igor Konopko7e5434e2019-05-04 20:37:46 +0200295 /* Line goes back to closed state, so we cannot release additional
296 * reference for line, since we do that only when we want to do
297 * gc to free line state transition.
298 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200299 pblk_put_line_back(pblk, line);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200300 atomic_dec(&gc->read_inflight_gc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200301
Matias Bjørling4e495a42018-07-13 10:48:42 +0200302 pblk_err(pblk, "failed to GC line %d\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200303}
304
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200305static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200306{
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200307 struct pblk_gc *gc = &pblk->gc;
308 struct pblk_line_ws *line_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200309
Matias Bjørling4e495a42018-07-13 10:48:42 +0200310 pblk_debug(pblk, "line '%d' being reclaimed for GC\n", line->id);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200311
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200312 line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200313 if (!line_ws)
314 return -ENOMEM;
315
316 line_ws->pblk = pblk;
317 line_ws->line = line;
318
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200319 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200320 INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws);
321 queue_work(gc->gc_reader_wq, &line_ws->ws);
322
323 return 0;
324}
325
Javier González8bd40022017-10-13 14:46:44 +0200326static void pblk_gc_reader_kick(struct pblk_gc *gc)
327{
328 wake_up_process(gc->gc_reader_ts);
329}
330
331static void pblk_gc_kick(struct pblk *pblk)
332{
333 struct pblk_gc *gc = &pblk->gc;
334
335 pblk_gc_writer_kick(gc);
336 pblk_gc_reader_kick(gc);
337
338 /* If we're shutting down GC, let's not start it up again */
339 if (gc->gc_enabled) {
340 wake_up_process(gc->gc_ts);
341 mod_timer(&gc->gc_timer,
342 jiffies + msecs_to_jiffies(GC_TIME_MSECS));
343 }
344}
345
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200346static int pblk_gc_read(struct pblk *pblk)
347{
348 struct pblk_gc *gc = &pblk->gc;
349 struct pblk_line *line;
350
351 spin_lock(&gc->r_lock);
352 if (list_empty(&gc->r_list)) {
353 spin_unlock(&gc->r_lock);
354 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200355 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200356
357 line = list_first_entry(&gc->r_list, struct pblk_line, list);
358 list_del(&line->list);
359 spin_unlock(&gc->r_lock);
360
361 pblk_gc_kick(pblk);
362
Igor Konopko8935ebf2019-05-04 20:37:47 +0200363 if (pblk_gc_line(pblk, line)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200364 pblk_err(pblk, "failed to GC line %d\n", line->id);
Igor Konopko8935ebf2019-05-04 20:37:47 +0200365 /* rollback */
366 spin_lock(&gc->r_lock);
367 list_add_tail(&line->list, &gc->r_list);
368 spin_unlock(&gc->r_lock);
369 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200370
371 return 0;
372}
373
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200374static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk,
375 struct list_head *group_list)
376{
377 struct pblk_line *line, *victim;
Heiner Litz05869422019-02-11 13:25:09 +0100378 unsigned int line_vsc = ~0x0L, victim_vsc = ~0x0L;
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200379
380 victim = list_first_entry(group_list, struct pblk_line, list);
Heiner Litz05869422019-02-11 13:25:09 +0100381
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200382 list_for_each_entry(line, group_list, list) {
Heiner Litz05869422019-02-11 13:25:09 +0100383 if (!atomic_read(&line->sec_to_update))
384 line_vsc = le32_to_cpu(*line->vsc);
385 if (line_vsc < victim_vsc) {
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200386 victim = line;
Heiner Litz05869422019-02-11 13:25:09 +0100387 victim_vsc = le32_to_cpu(*victim->vsc);
388 }
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200389 }
390
Heiner Litz05869422019-02-11 13:25:09 +0100391 if (victim_vsc == ~0x0)
392 return NULL;
393
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200394 return victim;
395}
396
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200397static bool pblk_gc_should_run(struct pblk_gc *gc, struct pblk_rl *rl)
398{
399 unsigned int nr_blocks_free, nr_blocks_need;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200400 unsigned int werr_lines = atomic_read(&rl->werr_lines);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200401
402 nr_blocks_need = pblk_rl_high_thrs(rl);
403 nr_blocks_free = pblk_rl_nr_free_blks(rl);
404
405 /* This is not critical, no need to take lock here */
Hans Holmberg48b8d202018-06-01 16:41:06 +0200406 return ((werr_lines > 0) ||
407 ((gc->gc_active) && (nr_blocks_need > nr_blocks_free)));
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200408}
409
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200410void pblk_gc_free_full_lines(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200411{
412 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200413 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200414 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200415
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200416 do {
417 spin_lock(&l_mg->gc_lock);
418 if (list_empty(&l_mg->gc_full_list)) {
419 spin_unlock(&l_mg->gc_lock);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200420 return;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200421 }
422
423 line = list_first_entry(&l_mg->gc_full_list,
424 struct pblk_line, list);
425
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200426 spin_lock(&line->lock);
427 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
428 line->state = PBLK_LINESTATE_GC;
Hans Holmbergf2937232018-10-09 13:11:53 +0200429 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
430 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200431 spin_unlock(&line->lock);
432
433 list_del(&line->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200434 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200435
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200436 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200437 kref_put(&line->ref, pblk_line_put);
438 } while (1);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200439}
440
441/*
442 * Lines with no valid sectors will be returned to the free list immediately. If
443 * GC is activated - either because the free block count is under the determined
444 * threshold, or because it is being forced from user space - only lines with a
445 * high count of invalid sectors will be recycled.
446 */
447static void pblk_gc_run(struct pblk *pblk)
448{
449 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
450 struct pblk_gc *gc = &pblk->gc;
451 struct pblk_line *line;
452 struct list_head *group_list;
453 bool run_gc;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200454 int read_inflight_gc, gc_group = 0, prev_group = 0;
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200455
456 pblk_gc_free_full_lines(pblk);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200457
458 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200459 if (!run_gc || (atomic_read(&gc->read_inflight_gc) >= PBLK_GC_L_QD))
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200460 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200461
462next_gc_group:
463 group_list = l_mg->gc_lists[gc_group++];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200464
465 do {
466 spin_lock(&l_mg->gc_lock);
Heiner Litz05869422019-02-11 13:25:09 +0100467
468 line = pblk_gc_get_victim_line(pblk, group_list);
469 if (!line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200470 spin_unlock(&l_mg->gc_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200471 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200472 }
473
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200474 spin_lock(&line->lock);
475 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
476 line->state = PBLK_LINESTATE_GC;
Hans Holmbergf2937232018-10-09 13:11:53 +0200477 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
478 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200479 spin_unlock(&line->lock);
480
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200481 list_del(&line->list);
482 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200483
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200484 spin_lock(&gc->r_lock);
485 list_add_tail(&line->list, &gc->r_list);
486 spin_unlock(&gc->r_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200487
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200488 read_inflight_gc = atomic_inc_return(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200489 pblk_gc_reader_kick(gc);
490
491 prev_group = 1;
492
493 /* No need to queue up more GC lines than we can handle */
494 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200495 if (!run_gc || read_inflight_gc >= PBLK_GC_L_QD)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200496 break;
497 } while (1);
498
499 if (!prev_group && pblk->rl.rb_state > gc_group &&
500 gc_group < PBLK_GC_NR_LISTS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200501 goto next_gc_group;
502}
503
Kees Cook87c1d2d2017-10-17 21:10:19 -0700504static void pblk_gc_timer(struct timer_list *t)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200505{
Kees Cook87c1d2d2017-10-17 21:10:19 -0700506 struct pblk *pblk = from_timer(pblk, t, gc.gc_timer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200507
508 pblk_gc_kick(pblk);
509}
510
511static int pblk_gc_ts(void *data)
512{
513 struct pblk *pblk = data;
514
515 while (!kthread_should_stop()) {
516 pblk_gc_run(pblk);
517 set_current_state(TASK_INTERRUPTIBLE);
518 io_schedule();
519 }
520
521 return 0;
522}
523
524static int pblk_gc_writer_ts(void *data)
525{
526 struct pblk *pblk = data;
527
528 while (!kthread_should_stop()) {
529 if (!pblk_gc_write(pblk))
530 continue;
531 set_current_state(TASK_INTERRUPTIBLE);
532 io_schedule();
533 }
534
535 return 0;
536}
537
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200538static int pblk_gc_reader_ts(void *data)
539{
540 struct pblk *pblk = data;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200541 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200542
543 while (!kthread_should_stop()) {
544 if (!pblk_gc_read(pblk))
545 continue;
546 set_current_state(TASK_INTERRUPTIBLE);
547 io_schedule();
548 }
549
Matias Bjørling880eda52018-07-13 10:48:37 +0200550#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +0200551 pblk_info(pblk, "flushing gc pipeline, %d lines left\n",
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200552 atomic_read(&gc->pipeline_gc));
553#endif
554
555 do {
556 if (!atomic_read(&gc->pipeline_gc))
557 break;
558
559 schedule();
560 } while (1);
561
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200562 return 0;
563}
564
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200565static void pblk_gc_start(struct pblk *pblk)
566{
567 pblk->gc.gc_active = 1;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200568 pblk_debug(pblk, "gc start\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200569}
570
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200571void pblk_gc_should_start(struct pblk *pblk)
572{
573 struct pblk_gc *gc = &pblk->gc;
574
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200575 if (gc->gc_enabled && !gc->gc_active) {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200576 pblk_gc_start(pblk);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200577 pblk_gc_kick(pblk);
578 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200579}
580
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200581void pblk_gc_should_stop(struct pblk *pblk)
582{
583 struct pblk_gc *gc = &pblk->gc;
584
585 if (gc->gc_active && !gc->gc_forced)
Hans Holmbergaed49e12018-01-05 14:16:12 +0100586 gc->gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200587}
588
Hans Holmberg03661b52017-10-13 14:46:37 +0200589void pblk_gc_should_kick(struct pblk *pblk)
590{
591 pblk_rl_update_rates(&pblk->rl);
592}
593
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200594void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
595 int *gc_active)
596{
597 struct pblk_gc *gc = &pblk->gc;
598
599 spin_lock(&gc->lock);
600 *gc_enabled = gc->gc_enabled;
601 *gc_active = gc->gc_active;
602 spin_unlock(&gc->lock);
603}
604
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200605int pblk_gc_sysfs_force(struct pblk *pblk, int force)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200606{
607 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200608
609 if (force < 0 || force > 1)
610 return -EINVAL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200611
612 spin_lock(&gc->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200613 gc->gc_forced = force;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200614
615 if (force)
616 gc->gc_enabled = 1;
617 else
618 gc->gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200619 spin_unlock(&gc->lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200620
621 pblk_gc_should_start(pblk);
622
623 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200624}
625
626int pblk_gc_init(struct pblk *pblk)
627{
628 struct pblk_gc *gc = &pblk->gc;
629 int ret;
630
631 gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts");
632 if (IS_ERR(gc->gc_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200633 pblk_err(pblk, "could not allocate GC main kthread\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200634 return PTR_ERR(gc->gc_ts);
635 }
636
637 gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk,
638 "pblk-gc-writer-ts");
639 if (IS_ERR(gc->gc_writer_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200640 pblk_err(pblk, "could not allocate GC writer kthread\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200641 ret = PTR_ERR(gc->gc_writer_ts);
642 goto fail_free_main_kthread;
643 }
644
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200645 gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk,
646 "pblk-gc-reader-ts");
647 if (IS_ERR(gc->gc_reader_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200648 pblk_err(pblk, "could not allocate GC reader kthread\n");
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200649 ret = PTR_ERR(gc->gc_reader_ts);
650 goto fail_free_writer_kthread;
651 }
652
Kees Cook87c1d2d2017-10-17 21:10:19 -0700653 timer_setup(&gc->gc_timer, pblk_gc_timer, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200654 mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS));
655
656 gc->gc_active = 0;
657 gc->gc_forced = 0;
658 gc->gc_enabled = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200659 gc->w_entries = 0;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200660 atomic_set(&gc->read_inflight_gc, 0);
661 atomic_set(&gc->pipeline_gc, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200662
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200663 /* Workqueue that reads valid sectors from a line and submit them to the
664 * GC writer to be recycled.
665 */
666 gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq",
667 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS);
668 if (!gc->gc_line_reader_wq) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200669 pblk_err(pblk, "could not allocate GC line reader workqueue\n");
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200670 ret = -ENOMEM;
671 goto fail_free_reader_kthread;
672 }
673
674 /* Workqueue that prepare lines for GC */
675 gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq",
676 WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200677 if (!gc->gc_reader_wq) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200678 pblk_err(pblk, "could not allocate GC reader workqueue\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200679 ret = -ENOMEM;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200680 goto fail_free_reader_line_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200681 }
682
683 spin_lock_init(&gc->lock);
684 spin_lock_init(&gc->w_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200685 spin_lock_init(&gc->r_lock);
686
Javier González36278962017-10-13 14:46:11 +0200687 sema_init(&gc->gc_sem, PBLK_GC_RQ_QD);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200688
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200689 INIT_LIST_HEAD(&gc->w_list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200690 INIT_LIST_HEAD(&gc->r_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200691
692 return 0;
693
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200694fail_free_reader_line_wq:
695 destroy_workqueue(gc->gc_line_reader_wq);
696fail_free_reader_kthread:
697 kthread_stop(gc->gc_reader_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200698fail_free_writer_kthread:
699 kthread_stop(gc->gc_writer_ts);
Dan Carpenter503ec942017-04-15 20:55:51 +0200700fail_free_main_kthread:
701 kthread_stop(gc->gc_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200702
703 return ret;
704}
705
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200706void pblk_gc_exit(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200707{
708 struct pblk_gc *gc = &pblk->gc;
709
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200710 gc->gc_enabled = 0;
711 del_timer_sync(&gc->gc_timer);
Hans Holmbergaed49e12018-01-05 14:16:12 +0100712 gc->gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200713
714 if (gc->gc_ts)
715 kthread_stop(gc->gc_ts);
716
Hans Holmberg1edebac2017-10-13 14:46:39 +0200717 if (gc->gc_reader_ts)
718 kthread_stop(gc->gc_reader_ts);
719
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200720 if (graceful) {
721 flush_workqueue(gc->gc_reader_wq);
722 flush_workqueue(gc->gc_line_reader_wq);
723 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200724
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200725 destroy_workqueue(gc->gc_reader_wq);
Dan Carpenter5565b0c2018-03-30 00:05:23 +0200726 destroy_workqueue(gc->gc_line_reader_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200727
728 if (gc->gc_writer_ts)
729 kthread_stop(gc->gc_writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200730}