blob: b841d84c4342748cbf7f531415bca7166345937c [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * pblk-gc.c - pblk's garbage collector
16 */
17
18#include "pblk.h"
Hans Holmbergf2937232018-10-09 13:11:53 +020019#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020020#include <linux/delay.h>
21
Hans Holmbergf2937232018-10-09 13:11:53 +020022
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020023static void pblk_gc_free_gc_rq(struct pblk_gc_rq *gc_rq)
24{
Javier Gonzálezd3401212017-10-13 14:46:14 +020025 if (gc_rq->data)
26 vfree(gc_rq->data);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020027 kfree(gc_rq);
28}
29
30static int pblk_gc_write(struct pblk *pblk)
31{
32 struct pblk_gc *gc = &pblk->gc;
33 struct pblk_gc_rq *gc_rq, *tgc_rq;
34 LIST_HEAD(w_list);
35
36 spin_lock(&gc->w_lock);
37 if (list_empty(&gc->w_list)) {
38 spin_unlock(&gc->w_lock);
39 return 1;
40 }
41
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020042 list_cut_position(&w_list, &gc->w_list, gc->w_list.prev);
43 gc->w_entries = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020044 spin_unlock(&gc->w_lock);
45
46 list_for_each_entry_safe(gc_rq, tgc_rq, &w_list, list) {
Javier Gonzálezd3401212017-10-13 14:46:14 +020047 pblk_write_gc_to_cache(pblk, gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020048 list_del(&gc_rq->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020049 kref_put(&gc_rq->line->ref, pblk_line_put);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020050 pblk_gc_free_gc_rq(gc_rq);
51 }
52
53 return 0;
54}
55
56static void pblk_gc_writer_kick(struct pblk_gc *gc)
57{
58 wake_up_process(gc->gc_writer_ts);
59}
60
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020061static void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line)
62{
63 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
64 struct list_head *move_list;
65
66 spin_lock(&line->lock);
67 WARN_ON(line->state != PBLK_LINESTATE_GC);
68 line->state = PBLK_LINESTATE_CLOSED;
Hans Holmbergf2937232018-10-09 13:11:53 +020069 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
70 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020071 move_list = pblk_line_gc_list(pblk, line);
72 spin_unlock(&line->lock);
73
74 if (move_list) {
75 spin_lock(&l_mg->gc_lock);
76 list_add_tail(&line->list, move_list);
77 spin_unlock(&l_mg->gc_lock);
78 }
79}
80
81static void pblk_gc_line_ws(struct work_struct *work)
82{
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020083 struct pblk_line_ws *gc_rq_ws = container_of(work,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020084 struct pblk_line_ws, ws);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020085 struct pblk *pblk = gc_rq_ws->pblk;
Javier González2a19b102017-10-13 14:46:15 +020086 struct nvm_tgt_dev *dev = pblk->dev;
87 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020088 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020089 struct pblk_line *line = gc_rq_ws->line;
90 struct pblk_gc_rq *gc_rq = gc_rq_ws->priv;
Javier González2a19b102017-10-13 14:46:15 +020091 int ret;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020092
93 up(&gc->gc_sem);
94
Kees Cook42bc47b2018-06-12 14:27:11 -070095 gc_rq->data = vmalloc(array_size(gc_rq->nr_secs, geo->csecs));
Javier González2a19b102017-10-13 14:46:15 +020096 if (!gc_rq->data) {
Matias Bjørling4e495a42018-07-13 10:48:42 +020097 pblk_err(pblk, "could not GC line:%d (%d/%d)\n",
Javier González2a19b102017-10-13 14:46:15 +020098 line->id, *line->vsc, gc_rq->nr_secs);
99 goto out;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200100 }
101
Javier González2a19b102017-10-13 14:46:15 +0200102 /* Read from GC victim block */
103 ret = pblk_submit_read_gc(pblk, gc_rq);
104 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200105 pblk_err(pblk, "failed GC read in line:%d (err:%d)\n",
Javier González2a19b102017-10-13 14:46:15 +0200106 line->id, ret);
107 goto out;
108 }
109
110 if (!gc_rq->secs_to_gc)
111 goto out;
112
113retry:
114 spin_lock(&gc->w_lock);
115 if (gc->w_entries >= PBLK_GC_RQ_QD) {
116 spin_unlock(&gc->w_lock);
117 pblk_gc_writer_kick(&pblk->gc);
118 usleep_range(128, 256);
119 goto retry;
120 }
121 gc->w_entries++;
122 list_add_tail(&gc_rq->list, &gc->w_list);
123 spin_unlock(&gc->w_lock);
124
125 pblk_gc_writer_kick(&pblk->gc);
126
127 kfree(gc_rq_ws);
128 return;
129
130out:
131 pblk_gc_free_gc_rq(gc_rq);
132 kref_put(&line->ref, pblk_line_put);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200133 kfree(gc_rq_ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200134}
135
Hans Holmberg48b8d202018-06-01 16:41:06 +0200136static __le64 *get_lba_list_from_emeta(struct pblk *pblk,
137 struct pblk_line *line)
138{
139 struct line_emeta *emeta_buf;
140 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
141 struct pblk_line_meta *lm = &pblk->lm;
142 unsigned int lba_list_size = lm->emeta_len[2];
143 __le64 *lba_list;
144 int ret;
145
146 emeta_buf = pblk_malloc(lm->emeta_len[0],
147 l_mg->emeta_alloc_type, GFP_KERNEL);
148 if (!emeta_buf)
149 return NULL;
150
151 ret = pblk_line_read_emeta(pblk, line, emeta_buf);
152 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200153 pblk_err(pblk, "line %d read emeta failed (%d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200154 line->id, ret);
155 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
156 return NULL;
157 }
158
159 /* If this read fails, it means that emeta is corrupted.
160 * For now, leave the line untouched.
161 * TODO: Implement a recovery routine that scans and moves
162 * all sectors on the line.
163 */
164
165 ret = pblk_recov_check_emeta(pblk, emeta_buf);
166 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200167 pblk_err(pblk, "inconsistent emeta (line %d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200168 line->id);
169 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
170 return NULL;
171 }
172
173 lba_list = pblk_malloc(lba_list_size,
174 l_mg->emeta_alloc_type, GFP_KERNEL);
175 if (lba_list)
176 memcpy(lba_list, emeta_to_lbas(pblk, emeta_buf), lba_list_size);
177
178 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
179
180 return lba_list;
181}
182
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200183static void pblk_gc_line_prepare_ws(struct work_struct *work)
184{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200185 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
186 ws);
187 struct pblk *pblk = line_ws->pblk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200188 struct pblk_line *line = line_ws->line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200189 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
190 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200191 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200192 struct pblk_line_ws *gc_rq_ws;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200193 struct pblk_gc_rq *gc_rq;
194 __le64 *lba_list;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200195 unsigned long *invalid_bitmap;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200196 int sec_left, nr_secs, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200197
Javier Gonzálezd3401212017-10-13 14:46:14 +0200198 invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_KERNEL);
Markus Elfring5da84cf2018-03-30 00:04:48 +0200199 if (!invalid_bitmap)
Javier Gonzálezd3401212017-10-13 14:46:14 +0200200 goto fail_free_ws;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200201
Hans Holmberg48b8d202018-06-01 16:41:06 +0200202 if (line->w_err_gc->has_write_err) {
203 lba_list = line->w_err_gc->lba_list;
204 line->w_err_gc->lba_list = NULL;
205 } else {
206 lba_list = get_lba_list_from_emeta(pblk, line);
207 if (!lba_list) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200208 pblk_err(pblk, "could not interpret emeta (line %d)\n",
Hans Holmberg48b8d202018-06-01 16:41:06 +0200209 line->id);
Colin Ian King0ec69372018-06-05 16:14:56 +0100210 goto fail_free_invalid_bitmap;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200211 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200212 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200213
Javier Gonzálezd3401212017-10-13 14:46:14 +0200214 spin_lock(&line->lock);
215 bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200216 sec_left = pblk_line_vsc(line);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200217 spin_unlock(&line->lock);
218
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200219 if (sec_left < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200220 pblk_err(pblk, "corrupted GC line (%d)\n", line->id);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200221 goto fail_free_lba_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200222 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200223
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200224 bit = -1;
225next_rq:
226 gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL);
227 if (!gc_rq)
Hans Holmberg48b8d202018-06-01 16:41:06 +0200228 goto fail_free_lba_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200229
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200230 nr_secs = 0;
231 do {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200232 bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200233 bit + 1);
234 if (bit > line->emeta_ssec)
235 break;
236
Javier Gonzálezd3401212017-10-13 14:46:14 +0200237 gc_rq->paddr_list[nr_secs] = bit;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200238 gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]);
239 } while (nr_secs < pblk->max_write_pgs);
240
241 if (unlikely(!nr_secs)) {
242 kfree(gc_rq);
243 goto out;
244 }
245
246 gc_rq->nr_secs = nr_secs;
247 gc_rq->line = line;
248
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200249 gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
250 if (!gc_rq_ws)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200251 goto fail_free_gc_rq;
252
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200253 gc_rq_ws->pblk = pblk;
254 gc_rq_ws->line = line;
255 gc_rq_ws->priv = gc_rq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200256
Javier González8da10cc2017-10-13 14:46:45 +0200257 /* The write GC path can be much slower than the read GC one due to
258 * the budget imposed by the rate-limiter. Balance in case that we get
259 * back pressure from the write GC path.
260 */
261 while (down_timeout(&gc->gc_sem, msecs_to_jiffies(30000)))
262 io_schedule();
263
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200264 kref_get(&line->ref);
265
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200266 INIT_WORK(&gc_rq_ws->ws, pblk_gc_line_ws);
267 queue_work(gc->gc_line_reader_wq, &gc_rq_ws->ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200268
269 sec_left -= nr_secs;
270 if (sec_left > 0)
271 goto next_rq;
272
273out:
Hans Holmberg48b8d202018-06-01 16:41:06 +0200274 pblk_mfree(lba_list, l_mg->emeta_alloc_type);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200275 kfree(line_ws);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200276 kfree(invalid_bitmap);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200277
278 kref_put(&line->ref, pblk_line_put);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200279 atomic_dec(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200280
281 return;
282
283fail_free_gc_rq:
284 kfree(gc_rq);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200285fail_free_lba_list:
286 pblk_mfree(lba_list, l_mg->emeta_alloc_type);
Colin Ian King0ec69372018-06-05 16:14:56 +0100287fail_free_invalid_bitmap:
Javier Gonzálezd3401212017-10-13 14:46:14 +0200288 kfree(invalid_bitmap);
289fail_free_ws:
290 kfree(line_ws);
291
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200292 pblk_put_line_back(pblk, line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200293 kref_put(&line->ref, pblk_line_put);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200294 atomic_dec(&gc->read_inflight_gc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200295
Matias Bjørling4e495a42018-07-13 10:48:42 +0200296 pblk_err(pblk, "failed to GC line %d\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200297}
298
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200299static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300{
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200301 struct pblk_gc *gc = &pblk->gc;
302 struct pblk_line_ws *line_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200303
Matias Bjørling4e495a42018-07-13 10:48:42 +0200304 pblk_debug(pblk, "line '%d' being reclaimed for GC\n", line->id);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200305
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200306 line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200307 if (!line_ws)
308 return -ENOMEM;
309
310 line_ws->pblk = pblk;
311 line_ws->line = line;
312
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200313 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200314 INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws);
315 queue_work(gc->gc_reader_wq, &line_ws->ws);
316
317 return 0;
318}
319
Javier González8bd40022017-10-13 14:46:44 +0200320static void pblk_gc_reader_kick(struct pblk_gc *gc)
321{
322 wake_up_process(gc->gc_reader_ts);
323}
324
325static void pblk_gc_kick(struct pblk *pblk)
326{
327 struct pblk_gc *gc = &pblk->gc;
328
329 pblk_gc_writer_kick(gc);
330 pblk_gc_reader_kick(gc);
331
332 /* If we're shutting down GC, let's not start it up again */
333 if (gc->gc_enabled) {
334 wake_up_process(gc->gc_ts);
335 mod_timer(&gc->gc_timer,
336 jiffies + msecs_to_jiffies(GC_TIME_MSECS));
337 }
338}
339
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200340static int pblk_gc_read(struct pblk *pblk)
341{
342 struct pblk_gc *gc = &pblk->gc;
343 struct pblk_line *line;
344
345 spin_lock(&gc->r_lock);
346 if (list_empty(&gc->r_list)) {
347 spin_unlock(&gc->r_lock);
348 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200349 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200350
351 line = list_first_entry(&gc->r_list, struct pblk_line, list);
352 list_del(&line->list);
353 spin_unlock(&gc->r_lock);
354
355 pblk_gc_kick(pblk);
356
357 if (pblk_gc_line(pblk, line))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200358 pblk_err(pblk, "failed to GC line %d\n", line->id);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200359
360 return 0;
361}
362
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200363static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk,
364 struct list_head *group_list)
365{
366 struct pblk_line *line, *victim;
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200367 int line_vsc, victim_vsc;
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200368
369 victim = list_first_entry(group_list, struct pblk_line, list);
370 list_for_each_entry(line, group_list, list) {
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200371 line_vsc = le32_to_cpu(*line->vsc);
372 victim_vsc = le32_to_cpu(*victim->vsc);
373 if (line_vsc < victim_vsc)
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200374 victim = line;
375 }
376
377 return victim;
378}
379
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200380static bool pblk_gc_should_run(struct pblk_gc *gc, struct pblk_rl *rl)
381{
382 unsigned int nr_blocks_free, nr_blocks_need;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200383 unsigned int werr_lines = atomic_read(&rl->werr_lines);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200384
385 nr_blocks_need = pblk_rl_high_thrs(rl);
386 nr_blocks_free = pblk_rl_nr_free_blks(rl);
387
388 /* This is not critical, no need to take lock here */
Hans Holmberg48b8d202018-06-01 16:41:06 +0200389 return ((werr_lines > 0) ||
390 ((gc->gc_active) && (nr_blocks_need > nr_blocks_free)));
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200391}
392
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200393void pblk_gc_free_full_lines(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200394{
395 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200396 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200397 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200398
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200399 do {
400 spin_lock(&l_mg->gc_lock);
401 if (list_empty(&l_mg->gc_full_list)) {
402 spin_unlock(&l_mg->gc_lock);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200403 return;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200404 }
405
406 line = list_first_entry(&l_mg->gc_full_list,
407 struct pblk_line, list);
408
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200409 spin_lock(&line->lock);
410 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
411 line->state = PBLK_LINESTATE_GC;
Hans Holmbergf2937232018-10-09 13:11:53 +0200412 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
413 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200414 spin_unlock(&line->lock);
415
416 list_del(&line->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200417 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200418
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200419 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200420 kref_put(&line->ref, pblk_line_put);
421 } while (1);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200422}
423
424/*
425 * Lines with no valid sectors will be returned to the free list immediately. If
426 * GC is activated - either because the free block count is under the determined
427 * threshold, or because it is being forced from user space - only lines with a
428 * high count of invalid sectors will be recycled.
429 */
430static void pblk_gc_run(struct pblk *pblk)
431{
432 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
433 struct pblk_gc *gc = &pblk->gc;
434 struct pblk_line *line;
435 struct list_head *group_list;
436 bool run_gc;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200437 int read_inflight_gc, gc_group = 0, prev_group = 0;
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200438
439 pblk_gc_free_full_lines(pblk);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200440
441 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200442 if (!run_gc || (atomic_read(&gc->read_inflight_gc) >= PBLK_GC_L_QD))
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200443 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200444
445next_gc_group:
446 group_list = l_mg->gc_lists[gc_group++];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200447
448 do {
449 spin_lock(&l_mg->gc_lock);
450 if (list_empty(group_list)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200451 spin_unlock(&l_mg->gc_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200452 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200453 }
454
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200455 line = pblk_gc_get_victim_line(pblk, group_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200456
457 spin_lock(&line->lock);
458 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
459 line->state = PBLK_LINESTATE_GC;
Hans Holmbergf2937232018-10-09 13:11:53 +0200460 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
461 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200462 spin_unlock(&line->lock);
463
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200464 list_del(&line->list);
465 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200466
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200467 spin_lock(&gc->r_lock);
468 list_add_tail(&line->list, &gc->r_list);
469 spin_unlock(&gc->r_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200470
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200471 read_inflight_gc = atomic_inc_return(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200472 pblk_gc_reader_kick(gc);
473
474 prev_group = 1;
475
476 /* No need to queue up more GC lines than we can handle */
477 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200478 if (!run_gc || read_inflight_gc >= PBLK_GC_L_QD)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200479 break;
480 } while (1);
481
482 if (!prev_group && pblk->rl.rb_state > gc_group &&
483 gc_group < PBLK_GC_NR_LISTS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484 goto next_gc_group;
485}
486
Kees Cook87c1d2d2017-10-17 21:10:19 -0700487static void pblk_gc_timer(struct timer_list *t)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200488{
Kees Cook87c1d2d2017-10-17 21:10:19 -0700489 struct pblk *pblk = from_timer(pblk, t, gc.gc_timer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200490
491 pblk_gc_kick(pblk);
492}
493
494static int pblk_gc_ts(void *data)
495{
496 struct pblk *pblk = data;
497
498 while (!kthread_should_stop()) {
499 pblk_gc_run(pblk);
500 set_current_state(TASK_INTERRUPTIBLE);
501 io_schedule();
502 }
503
504 return 0;
505}
506
507static int pblk_gc_writer_ts(void *data)
508{
509 struct pblk *pblk = data;
510
511 while (!kthread_should_stop()) {
512 if (!pblk_gc_write(pblk))
513 continue;
514 set_current_state(TASK_INTERRUPTIBLE);
515 io_schedule();
516 }
517
518 return 0;
519}
520
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200521static int pblk_gc_reader_ts(void *data)
522{
523 struct pblk *pblk = data;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200524 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200525
526 while (!kthread_should_stop()) {
527 if (!pblk_gc_read(pblk))
528 continue;
529 set_current_state(TASK_INTERRUPTIBLE);
530 io_schedule();
531 }
532
Matias Bjørling880eda52018-07-13 10:48:37 +0200533#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +0200534 pblk_info(pblk, "flushing gc pipeline, %d lines left\n",
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200535 atomic_read(&gc->pipeline_gc));
536#endif
537
538 do {
539 if (!atomic_read(&gc->pipeline_gc))
540 break;
541
542 schedule();
543 } while (1);
544
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200545 return 0;
546}
547
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200548static void pblk_gc_start(struct pblk *pblk)
549{
550 pblk->gc.gc_active = 1;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200551 pblk_debug(pblk, "gc start\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200552}
553
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200554void pblk_gc_should_start(struct pblk *pblk)
555{
556 struct pblk_gc *gc = &pblk->gc;
557
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200558 if (gc->gc_enabled && !gc->gc_active) {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200559 pblk_gc_start(pblk);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200560 pblk_gc_kick(pblk);
561 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200562}
563
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200564void pblk_gc_should_stop(struct pblk *pblk)
565{
566 struct pblk_gc *gc = &pblk->gc;
567
568 if (gc->gc_active && !gc->gc_forced)
Hans Holmbergaed49e12018-01-05 14:16:12 +0100569 gc->gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200570}
571
Hans Holmberg03661b52017-10-13 14:46:37 +0200572void pblk_gc_should_kick(struct pblk *pblk)
573{
574 pblk_rl_update_rates(&pblk->rl);
575}
576
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200577void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
578 int *gc_active)
579{
580 struct pblk_gc *gc = &pblk->gc;
581
582 spin_lock(&gc->lock);
583 *gc_enabled = gc->gc_enabled;
584 *gc_active = gc->gc_active;
585 spin_unlock(&gc->lock);
586}
587
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200588int pblk_gc_sysfs_force(struct pblk *pblk, int force)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200589{
590 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200591
592 if (force < 0 || force > 1)
593 return -EINVAL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200594
595 spin_lock(&gc->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200596 gc->gc_forced = force;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200597
598 if (force)
599 gc->gc_enabled = 1;
600 else
601 gc->gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200602 spin_unlock(&gc->lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200603
604 pblk_gc_should_start(pblk);
605
606 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607}
608
609int pblk_gc_init(struct pblk *pblk)
610{
611 struct pblk_gc *gc = &pblk->gc;
612 int ret;
613
614 gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts");
615 if (IS_ERR(gc->gc_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200616 pblk_err(pblk, "could not allocate GC main kthread\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200617 return PTR_ERR(gc->gc_ts);
618 }
619
620 gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk,
621 "pblk-gc-writer-ts");
622 if (IS_ERR(gc->gc_writer_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200623 pblk_err(pblk, "could not allocate GC writer kthread\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200624 ret = PTR_ERR(gc->gc_writer_ts);
625 goto fail_free_main_kthread;
626 }
627
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200628 gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk,
629 "pblk-gc-reader-ts");
630 if (IS_ERR(gc->gc_reader_ts)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200631 pblk_err(pblk, "could not allocate GC reader kthread\n");
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200632 ret = PTR_ERR(gc->gc_reader_ts);
633 goto fail_free_writer_kthread;
634 }
635
Kees Cook87c1d2d2017-10-17 21:10:19 -0700636 timer_setup(&gc->gc_timer, pblk_gc_timer, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200637 mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS));
638
639 gc->gc_active = 0;
640 gc->gc_forced = 0;
641 gc->gc_enabled = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200642 gc->w_entries = 0;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200643 atomic_set(&gc->read_inflight_gc, 0);
644 atomic_set(&gc->pipeline_gc, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200645
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200646 /* Workqueue that reads valid sectors from a line and submit them to the
647 * GC writer to be recycled.
648 */
649 gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq",
650 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS);
651 if (!gc->gc_line_reader_wq) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200652 pblk_err(pblk, "could not allocate GC line reader workqueue\n");
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200653 ret = -ENOMEM;
654 goto fail_free_reader_kthread;
655 }
656
657 /* Workqueue that prepare lines for GC */
658 gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq",
659 WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200660 if (!gc->gc_reader_wq) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200661 pblk_err(pblk, "could not allocate GC reader workqueue\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200662 ret = -ENOMEM;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200663 goto fail_free_reader_line_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200664 }
665
666 spin_lock_init(&gc->lock);
667 spin_lock_init(&gc->w_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200668 spin_lock_init(&gc->r_lock);
669
Javier González36278962017-10-13 14:46:11 +0200670 sema_init(&gc->gc_sem, PBLK_GC_RQ_QD);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200671
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200672 INIT_LIST_HEAD(&gc->w_list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200673 INIT_LIST_HEAD(&gc->r_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200674
675 return 0;
676
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200677fail_free_reader_line_wq:
678 destroy_workqueue(gc->gc_line_reader_wq);
679fail_free_reader_kthread:
680 kthread_stop(gc->gc_reader_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200681fail_free_writer_kthread:
682 kthread_stop(gc->gc_writer_ts);
Dan Carpenter503ec942017-04-15 20:55:51 +0200683fail_free_main_kthread:
684 kthread_stop(gc->gc_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200685
686 return ret;
687}
688
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200689void pblk_gc_exit(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200690{
691 struct pblk_gc *gc = &pblk->gc;
692
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200693 gc->gc_enabled = 0;
694 del_timer_sync(&gc->gc_timer);
Hans Holmbergaed49e12018-01-05 14:16:12 +0100695 gc->gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200696
697 if (gc->gc_ts)
698 kthread_stop(gc->gc_ts);
699
Hans Holmberg1edebac2017-10-13 14:46:39 +0200700 if (gc->gc_reader_ts)
701 kthread_stop(gc->gc_reader_ts);
702
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200703 if (graceful) {
704 flush_workqueue(gc->gc_reader_wq);
705 flush_workqueue(gc->gc_line_reader_wq);
706 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200707
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200708 destroy_workqueue(gc->gc_reader_wq);
Dan Carpenter5565b0c2018-03-30 00:05:23 +0200709 destroy_workqueue(gc->gc_line_reader_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200710
711 if (gc->gc_writer_ts)
712 kthread_stop(gc->gc_writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200713}