blob: 26a52ea7ec45702c0bd61173c194fe883c958067 [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
67 spin_lock(&line->lock);
68 WARN_ON(line->state != PBLK_LINESTATE_GC);
69 line->state = PBLK_LINESTATE_CLOSED;
Hans Holmbergf2937232018-10-09 13:11:53 +020070 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
71 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020072 move_list = pblk_line_gc_list(pblk, line);
73 spin_unlock(&line->lock);
74
75 if (move_list) {
76 spin_lock(&l_mg->gc_lock);
77 list_add_tail(&line->list, move_list);
78 spin_unlock(&l_mg->gc_lock);
79 }
80}
81
82static void pblk_gc_line_ws(struct work_struct *work)
83{
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020084 struct pblk_line_ws *gc_rq_ws = container_of(work,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020085 struct pblk_line_ws, ws);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020086 struct pblk *pblk = gc_rq_ws->pblk;
Javier González2a19b102017-10-13 14:46:15 +020087 struct nvm_tgt_dev *dev = pblk->dev;
88 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020089 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020090 struct pblk_line *line = gc_rq_ws->line;
91 struct pblk_gc_rq *gc_rq = gc_rq_ws->priv;
Javier González2a19b102017-10-13 14:46:15 +020092 int ret;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020093
94 up(&gc->gc_sem);
95
Kees Cook42bc47b2018-06-12 14:27:11 -070096 gc_rq->data = vmalloc(array_size(gc_rq->nr_secs, geo->csecs));
Javier González2a19b102017-10-13 14:46:15 +020097 if (!gc_rq->data) {
Matias Bjørling4e495a42018-07-13 10:48:42 +020098 pblk_err(pblk, "could not GC line:%d (%d/%d)\n",
Javier González2a19b102017-10-13 14:46:15 +020099 line->id, *line->vsc, gc_rq->nr_secs);
100 goto out;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200101 }
102
Javier González2a19b102017-10-13 14:46:15 +0200103 /* Read from GC victim block */
104 ret = pblk_submit_read_gc(pblk, gc_rq);
105 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200106 pblk_err(pblk, "failed GC read in line:%d (err:%d)\n",
Javier González2a19b102017-10-13 14:46:15 +0200107 line->id, ret);
108 goto out;
109 }
110
111 if (!gc_rq->secs_to_gc)
112 goto out;
113
114retry:
115 spin_lock(&gc->w_lock);
116 if (gc->w_entries >= PBLK_GC_RQ_QD) {
117 spin_unlock(&gc->w_lock);
118 pblk_gc_writer_kick(&pblk->gc);
119 usleep_range(128, 256);
120 goto retry;
121 }
122 gc->w_entries++;
123 list_add_tail(&gc_rq->list, &gc->w_list);
124 spin_unlock(&gc->w_lock);
125
126 pblk_gc_writer_kick(&pblk->gc);
127
128 kfree(gc_rq_ws);
129 return;
130
131out:
132 pblk_gc_free_gc_rq(gc_rq);
133 kref_put(&line->ref, pblk_line_put);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200134 kfree(gc_rq_ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200135}
136
Hans Holmberg48b8d202018-06-01 16:41:06 +0200137static __le64 *get_lba_list_from_emeta(struct pblk *pblk,
138 struct pblk_line *line)
139{
140 struct line_emeta *emeta_buf;
141 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
142 struct pblk_line_meta *lm = &pblk->lm;
143 unsigned int lba_list_size = lm->emeta_len[2];
144 __le64 *lba_list;
145 int ret;
146
147 emeta_buf = pblk_malloc(lm->emeta_len[0],
148 l_mg->emeta_alloc_type, GFP_KERNEL);
149 if (!emeta_buf)
150 return NULL;
151
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200152 ret = pblk_line_emeta_read(pblk, line, emeta_buf);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200153 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200154 pblk_err(pblk, "line %d read emeta failed (%d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200155 line->id, ret);
156 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
157 return NULL;
158 }
159
160 /* If this read fails, it means that emeta is corrupted.
161 * For now, leave the line untouched.
162 * TODO: Implement a recovery routine that scans and moves
163 * all sectors on the line.
164 */
165
166 ret = pblk_recov_check_emeta(pblk, emeta_buf);
167 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200168 pblk_err(pblk, "inconsistent emeta (line %d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200169 line->id);
170 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
171 return NULL;
172 }
173
174 lba_list = pblk_malloc(lba_list_size,
175 l_mg->emeta_alloc_type, GFP_KERNEL);
176 if (lba_list)
177 memcpy(lba_list, emeta_to_lbas(pblk, emeta_buf), lba_list_size);
178
179 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
180
181 return lba_list;
182}
183
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200184static void pblk_gc_line_prepare_ws(struct work_struct *work)
185{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200186 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
187 ws);
188 struct pblk *pblk = line_ws->pblk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200189 struct pblk_line *line = line_ws->line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200190 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
191 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200192 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200193 struct pblk_line_ws *gc_rq_ws;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200194 struct pblk_gc_rq *gc_rq;
195 __le64 *lba_list;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200196 unsigned long *invalid_bitmap;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200197 int sec_left, nr_secs, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200198
Javier Gonzálezd3401212017-10-13 14:46:14 +0200199 invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_KERNEL);
Markus Elfring5da84cf2018-03-30 00:04:48 +0200200 if (!invalid_bitmap)
Javier Gonzálezd3401212017-10-13 14:46:14 +0200201 goto fail_free_ws;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200202
Hans Holmberg48b8d202018-06-01 16:41:06 +0200203 if (line->w_err_gc->has_write_err) {
204 lba_list = line->w_err_gc->lba_list;
205 line->w_err_gc->lba_list = NULL;
206 } else {
207 lba_list = get_lba_list_from_emeta(pblk, line);
208 if (!lba_list) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200209 pblk_err(pblk, "could not interpret emeta (line %d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200210 line->id);
Colin Ian King0ec69372018-06-05 16:14:56 +0100211 goto fail_free_invalid_bitmap;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200212 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200213 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200214
Javier Gonzálezd3401212017-10-13 14:46:14 +0200215 spin_lock(&line->lock);
216 bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200217 sec_left = pblk_line_vsc(line);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200218 spin_unlock(&line->lock);
219
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200220 if (sec_left < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200221 pblk_err(pblk, "corrupted GC line (%d)\n", line->id);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200222 goto fail_free_lba_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200223 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200224
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200225 bit = -1;
226next_rq:
227 gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL);
228 if (!gc_rq)
Hans Holmberg48b8d202018-06-01 16:41:06 +0200229 goto fail_free_lba_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200230
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200231 nr_secs = 0;
232 do {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200233 bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200234 bit + 1);
235 if (bit > line->emeta_ssec)
236 break;
237
Javier Gonzálezd3401212017-10-13 14:46:14 +0200238 gc_rq->paddr_list[nr_secs] = bit;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200239 gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]);
240 } while (nr_secs < pblk->max_write_pgs);
241
242 if (unlikely(!nr_secs)) {
243 kfree(gc_rq);
244 goto out;
245 }
246
247 gc_rq->nr_secs = nr_secs;
248 gc_rq->line = line;
249
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200250 gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
251 if (!gc_rq_ws)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200252 goto fail_free_gc_rq;
253
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
284fail_free_gc_rq:
285 kfree(gc_rq);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200286fail_free_lba_list:
287 pblk_mfree(lba_list, l_mg->emeta_alloc_type);
Colin Ian King0ec69372018-06-05 16:14:56 +0100288fail_free_invalid_bitmap:
Javier Gonzálezd3401212017-10-13 14:46:14 +0200289 kfree(invalid_bitmap);
290fail_free_ws:
291 kfree(line_ws);
292
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200293 pblk_put_line_back(pblk, line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200294 kref_put(&line->ref, pblk_line_put);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200295 atomic_dec(&gc->read_inflight_gc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200296
Matias Bjørling4e495a42018-07-13 10:48:42 +0200297 pblk_err(pblk, "failed to GC line %d\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298}
299
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200300static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200301{
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200302 struct pblk_gc *gc = &pblk->gc;
303 struct pblk_line_ws *line_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200304
Matias Bjørling4e495a42018-07-13 10:48:42 +0200305 pblk_debug(pblk, "line '%d' being reclaimed for GC\n", line->id);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200306
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200307 line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200308 if (!line_ws)
309 return -ENOMEM;
310
311 line_ws->pblk = pblk;
312 line_ws->line = line;
313
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200314 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200315 INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws);
316 queue_work(gc->gc_reader_wq, &line_ws->ws);
317
318 return 0;
319}
320
Javier González8bd40022017-10-13 14:46:44 +0200321static void pblk_gc_reader_kick(struct pblk_gc *gc)
322{
323 wake_up_process(gc->gc_reader_ts);
324}
325
326static void pblk_gc_kick(struct pblk *pblk)
327{
328 struct pblk_gc *gc = &pblk->gc;
329
330 pblk_gc_writer_kick(gc);
331 pblk_gc_reader_kick(gc);
332
333 /* If we're shutting down GC, let's not start it up again */
334 if (gc->gc_enabled) {
335 wake_up_process(gc->gc_ts);
336 mod_timer(&gc->gc_timer,
337 jiffies + msecs_to_jiffies(GC_TIME_MSECS));
338 }
339}
340
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200341static int pblk_gc_read(struct pblk *pblk)
342{
343 struct pblk_gc *gc = &pblk->gc;
344 struct pblk_line *line;
345
346 spin_lock(&gc->r_lock);
347 if (list_empty(&gc->r_list)) {
348 spin_unlock(&gc->r_lock);
349 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200350 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200351
352 line = list_first_entry(&gc->r_list, struct pblk_line, list);
353 list_del(&line->list);
354 spin_unlock(&gc->r_lock);
355
356 pblk_gc_kick(pblk);
357
358 if (pblk_gc_line(pblk, line))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200359 pblk_err(pblk, "failed to GC line %d\n", line->id);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200360
361 return 0;
362}
363
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200364static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk,
365 struct list_head *group_list)
366{
367 struct pblk_line *line, *victim;
Heiner Litz05869422019-02-11 13:25:09 +0100368 unsigned int line_vsc = ~0x0L, victim_vsc = ~0x0L;
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200369
370 victim = list_first_entry(group_list, struct pblk_line, list);
Heiner Litz05869422019-02-11 13:25:09 +0100371
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200372 list_for_each_entry(line, group_list, list) {
Heiner Litz05869422019-02-11 13:25:09 +0100373 if (!atomic_read(&line->sec_to_update))
374 line_vsc = le32_to_cpu(*line->vsc);
375 if (line_vsc < victim_vsc) {
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200376 victim = line;
Heiner Litz05869422019-02-11 13:25:09 +0100377 victim_vsc = le32_to_cpu(*victim->vsc);
378 }
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200379 }
380
Heiner Litz05869422019-02-11 13:25:09 +0100381 if (victim_vsc == ~0x0)
382 return NULL;
383
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200384 return victim;
385}
386
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200387static bool pblk_gc_should_run(struct pblk_gc *gc, struct pblk_rl *rl)
388{
389 unsigned int nr_blocks_free, nr_blocks_need;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200390 unsigned int werr_lines = atomic_read(&rl->werr_lines);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200391
392 nr_blocks_need = pblk_rl_high_thrs(rl);
393 nr_blocks_free = pblk_rl_nr_free_blks(rl);
394
395 /* This is not critical, no need to take lock here */
Hans Holmberg48b8d202018-06-01 16:41:06 +0200396 return ((werr_lines > 0) ||
397 ((gc->gc_active) && (nr_blocks_need > nr_blocks_free)));
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200398}
399
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200400void pblk_gc_free_full_lines(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200401{
402 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200403 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200404 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200405
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200406 do {
407 spin_lock(&l_mg->gc_lock);
408 if (list_empty(&l_mg->gc_full_list)) {
409 spin_unlock(&l_mg->gc_lock);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200410 return;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200411 }
412
413 line = list_first_entry(&l_mg->gc_full_list,
414 struct pblk_line, list);
415
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200416 spin_lock(&line->lock);
417 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
418 line->state = PBLK_LINESTATE_GC;
Hans Holmbergf2937232018-10-09 13:11:53 +0200419 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
420 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200421 spin_unlock(&line->lock);
422
423 list_del(&line->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200424 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200425
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200426 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200427 kref_put(&line->ref, pblk_line_put);
428 } while (1);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200429}
430
431/*
432 * Lines with no valid sectors will be returned to the free list immediately. If
433 * GC is activated - either because the free block count is under the determined
434 * threshold, or because it is being forced from user space - only lines with a
435 * high count of invalid sectors will be recycled.
436 */
437static void pblk_gc_run(struct pblk *pblk)
438{
439 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
440 struct pblk_gc *gc = &pblk->gc;
441 struct pblk_line *line;
442 struct list_head *group_list;
443 bool run_gc;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200444 int read_inflight_gc, gc_group = 0, prev_group = 0;
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200445
446 pblk_gc_free_full_lines(pblk);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200447
448 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200449 if (!run_gc || (atomic_read(&gc->read_inflight_gc) >= PBLK_GC_L_QD))
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200450 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200451
452next_gc_group:
453 group_list = l_mg->gc_lists[gc_group++];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200454
455 do {
456 spin_lock(&l_mg->gc_lock);
Heiner Litz05869422019-02-11 13:25:09 +0100457
458 line = pblk_gc_get_victim_line(pblk, group_list);
459 if (!line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200460 spin_unlock(&l_mg->gc_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200461 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200462 }
463
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200464 spin_lock(&line->lock);
465 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
466 line->state = PBLK_LINESTATE_GC;
Hans Holmbergf2937232018-10-09 13:11:53 +0200467 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
468 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200469 spin_unlock(&line->lock);
470
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200471 list_del(&line->list);
472 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200473
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200474 spin_lock(&gc->r_lock);
475 list_add_tail(&line->list, &gc->r_list);
476 spin_unlock(&gc->r_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200477
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200478 read_inflight_gc = atomic_inc_return(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200479 pblk_gc_reader_kick(gc);
480
481 prev_group = 1;
482
483 /* No need to queue up more GC lines than we can handle */
484 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200485 if (!run_gc || read_inflight_gc >= PBLK_GC_L_QD)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200486 break;
487 } while (1);
488
489 if (!prev_group && pblk->rl.rb_state > gc_group &&
490 gc_group < PBLK_GC_NR_LISTS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200491 goto next_gc_group;
492}
493
Kees Cook87c1d2d2017-10-17 21:10:19 -0700494static void pblk_gc_timer(struct timer_list *t)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200495{
Kees Cook87c1d2d2017-10-17 21:10:19 -0700496 struct pblk *pblk = from_timer(pblk, t, gc.gc_timer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200497
498 pblk_gc_kick(pblk);
499}
500
501static int pblk_gc_ts(void *data)
502{
503 struct pblk *pblk = data;
504
505 while (!kthread_should_stop()) {
506 pblk_gc_run(pblk);
507 set_current_state(TASK_INTERRUPTIBLE);
508 io_schedule();
509 }
510
511 return 0;
512}
513
514static int pblk_gc_writer_ts(void *data)
515{
516 struct pblk *pblk = data;
517
518 while (!kthread_should_stop()) {
519 if (!pblk_gc_write(pblk))
520 continue;
521 set_current_state(TASK_INTERRUPTIBLE);
522 io_schedule();
523 }
524
525 return 0;
526}
527
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200528static int pblk_gc_reader_ts(void *data)
529{
530 struct pblk *pblk = data;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200531 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200532
533 while (!kthread_should_stop()) {
534 if (!pblk_gc_read(pblk))
535 continue;
536 set_current_state(TASK_INTERRUPTIBLE);
537 io_schedule();
538 }
539
Matias Bjørling880eda52018-07-13 10:48:37 +0200540#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +0200541 pblk_info(pblk, "flushing gc pipeline, %d lines left\n",
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200542 atomic_read(&gc->pipeline_gc));
543#endif
544
545 do {
546 if (!atomic_read(&gc->pipeline_gc))
547 break;
548
549 schedule();
550 } while (1);
551
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200552 return 0;
553}
554
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200555static void pblk_gc_start(struct pblk *pblk)
556{
557 pblk->gc.gc_active = 1;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200558 pblk_debug(pblk, "gc start\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200559}
560
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200561void pblk_gc_should_start(struct pblk *pblk)
562{
563 struct pblk_gc *gc = &pblk->gc;
564
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200565 if (gc->gc_enabled && !gc->gc_active) {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200566 pblk_gc_start(pblk);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200567 pblk_gc_kick(pblk);
568 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200569}
570
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200571void pblk_gc_should_stop(struct pblk *pblk)
572{
573 struct pblk_gc *gc = &pblk->gc;
574
575 if (gc->gc_active && !gc->gc_forced)
Hans Holmbergaed49e12018-01-05 14:16:12 +0100576 gc->gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200577}
578
Hans Holmberg03661b52017-10-13 14:46:37 +0200579void pblk_gc_should_kick(struct pblk *pblk)
580{
581 pblk_rl_update_rates(&pblk->rl);
582}
583
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200584void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
585 int *gc_active)
586{
587 struct pblk_gc *gc = &pblk->gc;
588
589 spin_lock(&gc->lock);
590 *gc_enabled = gc->gc_enabled;
591 *gc_active = gc->gc_active;
592 spin_unlock(&gc->lock);
593}
594
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200595int pblk_gc_sysfs_force(struct pblk *pblk, int force)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200596{
597 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200598
599 if (force < 0 || force > 1)
600 return -EINVAL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200601
602 spin_lock(&gc->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200603 gc->gc_forced = force;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200604
605 if (force)
606 gc->gc_enabled = 1;
607 else
608 gc->gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200609 spin_unlock(&gc->lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200610
611 pblk_gc_should_start(pblk);
612
613 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200614}
615
616int pblk_gc_init(struct pblk *pblk)
617{
618 struct pblk_gc *gc = &pblk->gc;
619 int ret;
620
621 gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts");
622 if (IS_ERR(gc->gc_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200623 pblk_err(pblk, "could not allocate GC main kthread\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200624 return PTR_ERR(gc->gc_ts);
625 }
626
627 gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk,
628 "pblk-gc-writer-ts");
629 if (IS_ERR(gc->gc_writer_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200630 pblk_err(pblk, "could not allocate GC writer kthread\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200631 ret = PTR_ERR(gc->gc_writer_ts);
632 goto fail_free_main_kthread;
633 }
634
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200635 gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk,
636 "pblk-gc-reader-ts");
637 if (IS_ERR(gc->gc_reader_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200638 pblk_err(pblk, "could not allocate GC reader kthread\n");
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200639 ret = PTR_ERR(gc->gc_reader_ts);
640 goto fail_free_writer_kthread;
641 }
642
Kees Cook87c1d2d2017-10-17 21:10:19 -0700643 timer_setup(&gc->gc_timer, pblk_gc_timer, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200644 mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS));
645
646 gc->gc_active = 0;
647 gc->gc_forced = 0;
648 gc->gc_enabled = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200649 gc->w_entries = 0;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200650 atomic_set(&gc->read_inflight_gc, 0);
651 atomic_set(&gc->pipeline_gc, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200652
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200653 /* Workqueue that reads valid sectors from a line and submit them to the
654 * GC writer to be recycled.
655 */
656 gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq",
657 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS);
658 if (!gc->gc_line_reader_wq) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200659 pblk_err(pblk, "could not allocate GC line reader workqueue\n");
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200660 ret = -ENOMEM;
661 goto fail_free_reader_kthread;
662 }
663
664 /* Workqueue that prepare lines for GC */
665 gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq",
666 WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200667 if (!gc->gc_reader_wq) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200668 pblk_err(pblk, "could not allocate GC reader workqueue\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200669 ret = -ENOMEM;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200670 goto fail_free_reader_line_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200671 }
672
673 spin_lock_init(&gc->lock);
674 spin_lock_init(&gc->w_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200675 spin_lock_init(&gc->r_lock);
676
Javier González36278962017-10-13 14:46:11 +0200677 sema_init(&gc->gc_sem, PBLK_GC_RQ_QD);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200678
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200679 INIT_LIST_HEAD(&gc->w_list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200680 INIT_LIST_HEAD(&gc->r_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200681
682 return 0;
683
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200684fail_free_reader_line_wq:
685 destroy_workqueue(gc->gc_line_reader_wq);
686fail_free_reader_kthread:
687 kthread_stop(gc->gc_reader_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200688fail_free_writer_kthread:
689 kthread_stop(gc->gc_writer_ts);
Dan Carpenter503ec942017-04-15 20:55:51 +0200690fail_free_main_kthread:
691 kthread_stop(gc->gc_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200692
693 return ret;
694}
695
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200696void pblk_gc_exit(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200697{
698 struct pblk_gc *gc = &pblk->gc;
699
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200700 gc->gc_enabled = 0;
701 del_timer_sync(&gc->gc_timer);
Hans Holmbergaed49e12018-01-05 14:16:12 +0100702 gc->gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200703
704 if (gc->gc_ts)
705 kthread_stop(gc->gc_ts);
706
Hans Holmberg1edebac2017-10-13 14:46:39 +0200707 if (gc->gc_reader_ts)
708 kthread_stop(gc->gc_reader_ts);
709
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200710 if (graceful) {
711 flush_workqueue(gc->gc_reader_wq);
712 flush_workqueue(gc->gc_line_reader_wq);
713 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200714
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200715 destroy_workqueue(gc->gc_reader_wq);
Dan Carpenter5565b0c2018-03-30 00:05:23 +0200716 destroy_workqueue(gc->gc_line_reader_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200717
718 if (gc->gc_writer_ts)
719 kthread_stop(gc->gc_writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200720}