blob: e23b1923b7738a3fd4ea6334de4055f42967cb2b [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
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020062static void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line)
63{
64 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
65 struct list_head *move_list;
66
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) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200101 pblk_err(pblk, "failed GC read in line:%d (err:%d)\n",
Javier González2a19b102017-10-13 14:46:15 +0200102 line->id, ret);
103 goto out;
104 }
105
106 if (!gc_rq->secs_to_gc)
107 goto out;
108
109retry:
110 spin_lock(&gc->w_lock);
111 if (gc->w_entries >= PBLK_GC_RQ_QD) {
112 spin_unlock(&gc->w_lock);
113 pblk_gc_writer_kick(&pblk->gc);
114 usleep_range(128, 256);
115 goto retry;
116 }
117 gc->w_entries++;
118 list_add_tail(&gc_rq->list, &gc->w_list);
119 spin_unlock(&gc->w_lock);
120
121 pblk_gc_writer_kick(&pblk->gc);
122
123 kfree(gc_rq_ws);
124 return;
125
126out:
127 pblk_gc_free_gc_rq(gc_rq);
128 kref_put(&line->ref, pblk_line_put);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200129 kfree(gc_rq_ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200130}
131
Hans Holmberg48b8d202018-06-01 16:41:06 +0200132static __le64 *get_lba_list_from_emeta(struct pblk *pblk,
133 struct pblk_line *line)
134{
135 struct line_emeta *emeta_buf;
136 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
137 struct pblk_line_meta *lm = &pblk->lm;
138 unsigned int lba_list_size = lm->emeta_len[2];
139 __le64 *lba_list;
140 int ret;
141
142 emeta_buf = pblk_malloc(lm->emeta_len[0],
143 l_mg->emeta_alloc_type, GFP_KERNEL);
144 if (!emeta_buf)
145 return NULL;
146
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200147 ret = pblk_line_emeta_read(pblk, line, emeta_buf);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200148 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200149 pblk_err(pblk, "line %d read emeta failed (%d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200150 line->id, ret);
151 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
152 return NULL;
153 }
154
155 /* If this read fails, it means that emeta is corrupted.
156 * For now, leave the line untouched.
157 * TODO: Implement a recovery routine that scans and moves
158 * all sectors on the line.
159 */
160
161 ret = pblk_recov_check_emeta(pblk, emeta_buf);
162 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200163 pblk_err(pblk, "inconsistent emeta (line %d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200164 line->id);
165 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
166 return NULL;
167 }
168
169 lba_list = pblk_malloc(lba_list_size,
170 l_mg->emeta_alloc_type, GFP_KERNEL);
171 if (lba_list)
172 memcpy(lba_list, emeta_to_lbas(pblk, emeta_buf), lba_list_size);
173
174 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
175
176 return lba_list;
177}
178
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200179static void pblk_gc_line_prepare_ws(struct work_struct *work)
180{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200181 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
182 ws);
183 struct pblk *pblk = line_ws->pblk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200184 struct pblk_line *line = line_ws->line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200185 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
186 struct pblk_line_meta *lm = &pblk->lm;
Igor Konopkod3785612019-05-04 20:37:50 +0200187 struct nvm_tgt_dev *dev = pblk->dev;
188 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200189 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200190 struct pblk_line_ws *gc_rq_ws;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200191 struct pblk_gc_rq *gc_rq;
192 __le64 *lba_list;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200193 unsigned long *invalid_bitmap;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200194 int sec_left, nr_secs, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200195
Javier Gonzálezd3401212017-10-13 14:46:14 +0200196 invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_KERNEL);
Markus Elfring5da84cf2018-03-30 00:04:48 +0200197 if (!invalid_bitmap)
Javier Gonzálezd3401212017-10-13 14:46:14 +0200198 goto fail_free_ws;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200199
Hans Holmberg48b8d202018-06-01 16:41:06 +0200200 if (line->w_err_gc->has_write_err) {
201 lba_list = line->w_err_gc->lba_list;
202 line->w_err_gc->lba_list = NULL;
203 } else {
204 lba_list = get_lba_list_from_emeta(pblk, line);
205 if (!lba_list) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200206 pblk_err(pblk, "could not interpret emeta (line %d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200207 line->id);
Colin Ian King0ec69372018-06-05 16:14:56 +0100208 goto fail_free_invalid_bitmap;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200209 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200210 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200211
Javier Gonzálezd3401212017-10-13 14:46:14 +0200212 spin_lock(&line->lock);
213 bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200214 sec_left = pblk_line_vsc(line);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200215 spin_unlock(&line->lock);
216
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200217 if (sec_left < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200218 pblk_err(pblk, "corrupted GC line (%d)\n", line->id);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200219 goto fail_free_lba_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200220 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200222 bit = -1;
223next_rq:
224 gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL);
225 if (!gc_rq)
Hans Holmberg48b8d202018-06-01 16:41:06 +0200226 goto fail_free_lba_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200227
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200228 nr_secs = 0;
229 do {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200230 bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200231 bit + 1);
232 if (bit > line->emeta_ssec)
233 break;
234
Javier Gonzálezd3401212017-10-13 14:46:14 +0200235 gc_rq->paddr_list[nr_secs] = bit;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200236 gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]);
237 } while (nr_secs < pblk->max_write_pgs);
238
239 if (unlikely(!nr_secs)) {
240 kfree(gc_rq);
241 goto out;
242 }
243
244 gc_rq->nr_secs = nr_secs;
245 gc_rq->line = line;
246
Igor Konopkod3785612019-05-04 20:37:50 +0200247 gc_rq->data = vmalloc(array_size(gc_rq->nr_secs, geo->csecs));
248 if (!gc_rq->data)
249 goto fail_free_gc_rq;
250
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200251 gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
252 if (!gc_rq_ws)
Igor Konopkod3785612019-05-04 20:37:50 +0200253 goto fail_free_gc_data;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200254
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200255 gc_rq_ws->pblk = pblk;
256 gc_rq_ws->line = line;
257 gc_rq_ws->priv = gc_rq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200258
Javier González8da10cc2017-10-13 14:46:45 +0200259 /* The write GC path can be much slower than the read GC one due to
260 * the budget imposed by the rate-limiter. Balance in case that we get
261 * back pressure from the write GC path.
262 */
263 while (down_timeout(&gc->gc_sem, msecs_to_jiffies(30000)))
264 io_schedule();
265
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200266 kref_get(&line->ref);
267
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200268 INIT_WORK(&gc_rq_ws->ws, pblk_gc_line_ws);
269 queue_work(gc->gc_line_reader_wq, &gc_rq_ws->ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200270
271 sec_left -= nr_secs;
272 if (sec_left > 0)
273 goto next_rq;
274
275out:
Hans Holmberg48b8d202018-06-01 16:41:06 +0200276 pblk_mfree(lba_list, l_mg->emeta_alloc_type);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200277 kfree(line_ws);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200278 kfree(invalid_bitmap);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200279
280 kref_put(&line->ref, pblk_line_put);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200281 atomic_dec(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200282
283 return;
284
Igor Konopkod3785612019-05-04 20:37:50 +0200285fail_free_gc_data:
286 vfree(gc_rq->data);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200287fail_free_gc_rq:
288 kfree(gc_rq);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200289fail_free_lba_list:
290 pblk_mfree(lba_list, l_mg->emeta_alloc_type);
Colin Ian King0ec69372018-06-05 16:14:56 +0100291fail_free_invalid_bitmap:
Javier Gonzálezd3401212017-10-13 14:46:14 +0200292 kfree(invalid_bitmap);
293fail_free_ws:
294 kfree(line_ws);
295
Igor Konopko7e5434e2019-05-04 20:37:46 +0200296 /* Line goes back to closed state, so we cannot release additional
297 * reference for line, since we do that only when we want to do
298 * gc to free line state transition.
299 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300 pblk_put_line_back(pblk, line);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200301 atomic_dec(&gc->read_inflight_gc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200302
Matias Bjørling4e495a42018-07-13 10:48:42 +0200303 pblk_err(pblk, "failed to GC line %d\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200304}
305
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200306static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200307{
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200308 struct pblk_gc *gc = &pblk->gc;
309 struct pblk_line_ws *line_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200310
Matias Bjørling4e495a42018-07-13 10:48:42 +0200311 pblk_debug(pblk, "line '%d' being reclaimed for GC\n", line->id);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200312
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200313 line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200314 if (!line_ws)
315 return -ENOMEM;
316
317 line_ws->pblk = pblk;
318 line_ws->line = line;
319
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200320 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200321 INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws);
322 queue_work(gc->gc_reader_wq, &line_ws->ws);
323
324 return 0;
325}
326
Javier González8bd40022017-10-13 14:46:44 +0200327static void pblk_gc_reader_kick(struct pblk_gc *gc)
328{
329 wake_up_process(gc->gc_reader_ts);
330}
331
332static void pblk_gc_kick(struct pblk *pblk)
333{
334 struct pblk_gc *gc = &pblk->gc;
335
336 pblk_gc_writer_kick(gc);
337 pblk_gc_reader_kick(gc);
338
339 /* If we're shutting down GC, let's not start it up again */
340 if (gc->gc_enabled) {
341 wake_up_process(gc->gc_ts);
342 mod_timer(&gc->gc_timer,
343 jiffies + msecs_to_jiffies(GC_TIME_MSECS));
344 }
345}
346
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200347static int pblk_gc_read(struct pblk *pblk)
348{
349 struct pblk_gc *gc = &pblk->gc;
350 struct pblk_line *line;
351
352 spin_lock(&gc->r_lock);
353 if (list_empty(&gc->r_list)) {
354 spin_unlock(&gc->r_lock);
355 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200356 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200357
358 line = list_first_entry(&gc->r_list, struct pblk_line, list);
359 list_del(&line->list);
360 spin_unlock(&gc->r_lock);
361
362 pblk_gc_kick(pblk);
363
Igor Konopko8935ebf2019-05-04 20:37:47 +0200364 if (pblk_gc_line(pblk, line)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200365 pblk_err(pblk, "failed to GC line %d\n", line->id);
Igor Konopko8935ebf2019-05-04 20:37:47 +0200366 /* rollback */
367 spin_lock(&gc->r_lock);
368 list_add_tail(&line->list, &gc->r_list);
369 spin_unlock(&gc->r_lock);
370 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200371
372 return 0;
373}
374
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200375static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk,
376 struct list_head *group_list)
377{
378 struct pblk_line *line, *victim;
Heiner Litz05869422019-02-11 13:25:09 +0100379 unsigned int line_vsc = ~0x0L, victim_vsc = ~0x0L;
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200380
381 victim = list_first_entry(group_list, struct pblk_line, list);
Heiner Litz05869422019-02-11 13:25:09 +0100382
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200383 list_for_each_entry(line, group_list, list) {
Heiner Litz05869422019-02-11 13:25:09 +0100384 if (!atomic_read(&line->sec_to_update))
385 line_vsc = le32_to_cpu(*line->vsc);
386 if (line_vsc < victim_vsc) {
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200387 victim = line;
Heiner Litz05869422019-02-11 13:25:09 +0100388 victim_vsc = le32_to_cpu(*victim->vsc);
389 }
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200390 }
391
Heiner Litz05869422019-02-11 13:25:09 +0100392 if (victim_vsc == ~0x0)
393 return NULL;
394
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200395 return victim;
396}
397
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200398static bool pblk_gc_should_run(struct pblk_gc *gc, struct pblk_rl *rl)
399{
400 unsigned int nr_blocks_free, nr_blocks_need;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200401 unsigned int werr_lines = atomic_read(&rl->werr_lines);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200402
403 nr_blocks_need = pblk_rl_high_thrs(rl);
404 nr_blocks_free = pblk_rl_nr_free_blks(rl);
405
406 /* This is not critical, no need to take lock here */
Hans Holmberg48b8d202018-06-01 16:41:06 +0200407 return ((werr_lines > 0) ||
408 ((gc->gc_active) && (nr_blocks_need > nr_blocks_free)));
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200409}
410
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200411void pblk_gc_free_full_lines(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200412{
413 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200414 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200415 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200416
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200417 do {
418 spin_lock(&l_mg->gc_lock);
419 if (list_empty(&l_mg->gc_full_list)) {
420 spin_unlock(&l_mg->gc_lock);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200421 return;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200422 }
423
424 line = list_first_entry(&l_mg->gc_full_list,
425 struct pblk_line, list);
426
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200427 spin_lock(&line->lock);
428 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
429 line->state = PBLK_LINESTATE_GC;
Hans Holmbergf2937232018-10-09 13:11:53 +0200430 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
431 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200432 spin_unlock(&line->lock);
433
434 list_del(&line->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200435 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200436
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200437 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200438 kref_put(&line->ref, pblk_line_put);
439 } while (1);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200440}
441
442/*
443 * Lines with no valid sectors will be returned to the free list immediately. If
444 * GC is activated - either because the free block count is under the determined
445 * threshold, or because it is being forced from user space - only lines with a
446 * high count of invalid sectors will be recycled.
447 */
448static void pblk_gc_run(struct pblk *pblk)
449{
450 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
451 struct pblk_gc *gc = &pblk->gc;
452 struct pblk_line *line;
453 struct list_head *group_list;
454 bool run_gc;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200455 int read_inflight_gc, gc_group = 0, prev_group = 0;
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200456
457 pblk_gc_free_full_lines(pblk);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200458
459 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200460 if (!run_gc || (atomic_read(&gc->read_inflight_gc) >= PBLK_GC_L_QD))
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200461 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200462
463next_gc_group:
464 group_list = l_mg->gc_lists[gc_group++];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200465
466 do {
467 spin_lock(&l_mg->gc_lock);
Heiner Litz05869422019-02-11 13:25:09 +0100468
469 line = pblk_gc_get_victim_line(pblk, group_list);
470 if (!line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200471 spin_unlock(&l_mg->gc_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200472 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200473 }
474
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200475 spin_lock(&line->lock);
476 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
477 line->state = PBLK_LINESTATE_GC;
Hans Holmbergf2937232018-10-09 13:11:53 +0200478 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
479 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200480 spin_unlock(&line->lock);
481
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200482 list_del(&line->list);
483 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200485 spin_lock(&gc->r_lock);
486 list_add_tail(&line->list, &gc->r_list);
487 spin_unlock(&gc->r_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200488
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200489 read_inflight_gc = atomic_inc_return(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200490 pblk_gc_reader_kick(gc);
491
492 prev_group = 1;
493
494 /* No need to queue up more GC lines than we can handle */
495 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200496 if (!run_gc || read_inflight_gc >= PBLK_GC_L_QD)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200497 break;
498 } while (1);
499
500 if (!prev_group && pblk->rl.rb_state > gc_group &&
501 gc_group < PBLK_GC_NR_LISTS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200502 goto next_gc_group;
503}
504
Kees Cook87c1d2d2017-10-17 21:10:19 -0700505static void pblk_gc_timer(struct timer_list *t)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200506{
Kees Cook87c1d2d2017-10-17 21:10:19 -0700507 struct pblk *pblk = from_timer(pblk, t, gc.gc_timer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200508
509 pblk_gc_kick(pblk);
510}
511
512static int pblk_gc_ts(void *data)
513{
514 struct pblk *pblk = data;
515
516 while (!kthread_should_stop()) {
517 pblk_gc_run(pblk);
518 set_current_state(TASK_INTERRUPTIBLE);
519 io_schedule();
520 }
521
522 return 0;
523}
524
525static int pblk_gc_writer_ts(void *data)
526{
527 struct pblk *pblk = data;
528
529 while (!kthread_should_stop()) {
530 if (!pblk_gc_write(pblk))
531 continue;
532 set_current_state(TASK_INTERRUPTIBLE);
533 io_schedule();
534 }
535
536 return 0;
537}
538
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200539static int pblk_gc_reader_ts(void *data)
540{
541 struct pblk *pblk = data;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200542 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200543
544 while (!kthread_should_stop()) {
545 if (!pblk_gc_read(pblk))
546 continue;
547 set_current_state(TASK_INTERRUPTIBLE);
548 io_schedule();
549 }
550
Matias Bjørling880eda52018-07-13 10:48:37 +0200551#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +0200552 pblk_info(pblk, "flushing gc pipeline, %d lines left\n",
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200553 atomic_read(&gc->pipeline_gc));
554#endif
555
556 do {
557 if (!atomic_read(&gc->pipeline_gc))
558 break;
559
560 schedule();
561 } while (1);
562
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200563 return 0;
564}
565
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200566static void pblk_gc_start(struct pblk *pblk)
567{
568 pblk->gc.gc_active = 1;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200569 pblk_debug(pblk, "gc start\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200570}
571
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200572void pblk_gc_should_start(struct pblk *pblk)
573{
574 struct pblk_gc *gc = &pblk->gc;
575
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200576 if (gc->gc_enabled && !gc->gc_active) {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200577 pblk_gc_start(pblk);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200578 pblk_gc_kick(pblk);
579 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200580}
581
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582void pblk_gc_should_stop(struct pblk *pblk)
583{
584 struct pblk_gc *gc = &pblk->gc;
585
586 if (gc->gc_active && !gc->gc_forced)
Hans Holmbergaed49e12018-01-05 14:16:12 +0100587 gc->gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200588}
589
Hans Holmberg03661b52017-10-13 14:46:37 +0200590void pblk_gc_should_kick(struct pblk *pblk)
591{
592 pblk_rl_update_rates(&pblk->rl);
593}
594
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200595void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
596 int *gc_active)
597{
598 struct pblk_gc *gc = &pblk->gc;
599
600 spin_lock(&gc->lock);
601 *gc_enabled = gc->gc_enabled;
602 *gc_active = gc->gc_active;
603 spin_unlock(&gc->lock);
604}
605
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200606int pblk_gc_sysfs_force(struct pblk *pblk, int force)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607{
608 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200609
610 if (force < 0 || force > 1)
611 return -EINVAL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200612
613 spin_lock(&gc->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200614 gc->gc_forced = force;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200615
616 if (force)
617 gc->gc_enabled = 1;
618 else
619 gc->gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200620 spin_unlock(&gc->lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200621
622 pblk_gc_should_start(pblk);
623
624 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200625}
626
627int pblk_gc_init(struct pblk *pblk)
628{
629 struct pblk_gc *gc = &pblk->gc;
630 int ret;
631
632 gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts");
633 if (IS_ERR(gc->gc_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200634 pblk_err(pblk, "could not allocate GC main kthread\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200635 return PTR_ERR(gc->gc_ts);
636 }
637
638 gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk,
639 "pblk-gc-writer-ts");
640 if (IS_ERR(gc->gc_writer_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200641 pblk_err(pblk, "could not allocate GC writer kthread\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200642 ret = PTR_ERR(gc->gc_writer_ts);
643 goto fail_free_main_kthread;
644 }
645
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200646 gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk,
647 "pblk-gc-reader-ts");
648 if (IS_ERR(gc->gc_reader_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200649 pblk_err(pblk, "could not allocate GC reader kthread\n");
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200650 ret = PTR_ERR(gc->gc_reader_ts);
651 goto fail_free_writer_kthread;
652 }
653
Kees Cook87c1d2d2017-10-17 21:10:19 -0700654 timer_setup(&gc->gc_timer, pblk_gc_timer, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200655 mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS));
656
657 gc->gc_active = 0;
658 gc->gc_forced = 0;
659 gc->gc_enabled = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200660 gc->w_entries = 0;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200661 atomic_set(&gc->read_inflight_gc, 0);
662 atomic_set(&gc->pipeline_gc, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200663
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200664 /* Workqueue that reads valid sectors from a line and submit them to the
665 * GC writer to be recycled.
666 */
667 gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq",
668 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS);
669 if (!gc->gc_line_reader_wq) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200670 pblk_err(pblk, "could not allocate GC line reader workqueue\n");
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200671 ret = -ENOMEM;
672 goto fail_free_reader_kthread;
673 }
674
675 /* Workqueue that prepare lines for GC */
676 gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq",
677 WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200678 if (!gc->gc_reader_wq) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200679 pblk_err(pblk, "could not allocate GC reader workqueue\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200680 ret = -ENOMEM;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200681 goto fail_free_reader_line_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200682 }
683
684 spin_lock_init(&gc->lock);
685 spin_lock_init(&gc->w_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200686 spin_lock_init(&gc->r_lock);
687
Javier González36278962017-10-13 14:46:11 +0200688 sema_init(&gc->gc_sem, PBLK_GC_RQ_QD);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200689
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200690 INIT_LIST_HEAD(&gc->w_list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200691 INIT_LIST_HEAD(&gc->r_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200692
693 return 0;
694
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200695fail_free_reader_line_wq:
696 destroy_workqueue(gc->gc_line_reader_wq);
697fail_free_reader_kthread:
698 kthread_stop(gc->gc_reader_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200699fail_free_writer_kthread:
700 kthread_stop(gc->gc_writer_ts);
Dan Carpenter503ec942017-04-15 20:55:51 +0200701fail_free_main_kthread:
702 kthread_stop(gc->gc_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200703
704 return ret;
705}
706
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200707void pblk_gc_exit(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200708{
709 struct pblk_gc *gc = &pblk->gc;
710
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200711 gc->gc_enabled = 0;
712 del_timer_sync(&gc->gc_timer);
Hans Holmbergaed49e12018-01-05 14:16:12 +0100713 gc->gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200714
715 if (gc->gc_ts)
716 kthread_stop(gc->gc_ts);
717
Hans Holmberg1edebac2017-10-13 14:46:39 +0200718 if (gc->gc_reader_ts)
719 kthread_stop(gc->gc_reader_ts);
720
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200721 if (graceful) {
722 flush_workqueue(gc->gc_reader_wq);
723 flush_workqueue(gc->gc_line_reader_wq);
724 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200725
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200726 destroy_workqueue(gc->gc_reader_wq);
Dan Carpenter5565b0c2018-03-30 00:05:23 +0200727 destroy_workqueue(gc->gc_line_reader_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200728
729 if (gc->gc_writer_ts)
730 kthread_stop(gc->gc_writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200731}