blob: cec9a56dda146712ef06a917afaf1c326f67385e [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"
19#include <linux/delay.h>
20
21static void pblk_gc_free_gc_rq(struct pblk_gc_rq *gc_rq)
22{
Javier Gonzálezd3401212017-10-13 14:46:14 +020023 if (gc_rq->data)
24 vfree(gc_rq->data);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020025 kfree(gc_rq);
26}
27
28static int pblk_gc_write(struct pblk *pblk)
29{
30 struct pblk_gc *gc = &pblk->gc;
31 struct pblk_gc_rq *gc_rq, *tgc_rq;
32 LIST_HEAD(w_list);
33
34 spin_lock(&gc->w_lock);
35 if (list_empty(&gc->w_list)) {
36 spin_unlock(&gc->w_lock);
37 return 1;
38 }
39
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020040 list_cut_position(&w_list, &gc->w_list, gc->w_list.prev);
41 gc->w_entries = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020042 spin_unlock(&gc->w_lock);
43
44 list_for_each_entry_safe(gc_rq, tgc_rq, &w_list, list) {
Javier Gonzálezd3401212017-10-13 14:46:14 +020045 pblk_write_gc_to_cache(pblk, gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020046 list_del(&gc_rq->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020047 kref_put(&gc_rq->line->ref, pblk_line_put);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020048 pblk_gc_free_gc_rq(gc_rq);
49 }
50
51 return 0;
52}
53
54static void pblk_gc_writer_kick(struct pblk_gc *gc)
55{
56 wake_up_process(gc->gc_writer_ts);
57}
58
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059static void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line)
60{
61 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
62 struct list_head *move_list;
63
64 spin_lock(&line->lock);
65 WARN_ON(line->state != PBLK_LINESTATE_GC);
66 line->state = PBLK_LINESTATE_CLOSED;
67 move_list = pblk_line_gc_list(pblk, line);
68 spin_unlock(&line->lock);
69
70 if (move_list) {
71 spin_lock(&l_mg->gc_lock);
72 list_add_tail(&line->list, move_list);
73 spin_unlock(&l_mg->gc_lock);
74 }
75}
76
77static void pblk_gc_line_ws(struct work_struct *work)
78{
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020079 struct pblk_line_ws *gc_rq_ws = container_of(work,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020080 struct pblk_line_ws, ws);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020081 struct pblk *pblk = gc_rq_ws->pblk;
Javier González2a19b102017-10-13 14:46:15 +020082 struct nvm_tgt_dev *dev = pblk->dev;
83 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020084 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020085 struct pblk_line *line = gc_rq_ws->line;
86 struct pblk_gc_rq *gc_rq = gc_rq_ws->priv;
Javier González2a19b102017-10-13 14:46:15 +020087 int ret;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020088
89 up(&gc->gc_sem);
90
Javier González2a19b102017-10-13 14:46:15 +020091 gc_rq->data = vmalloc(gc_rq->nr_secs * geo->sec_size);
92 if (!gc_rq->data) {
93 pr_err("pblk: could not GC line:%d (%d/%d)\n",
94 line->id, *line->vsc, gc_rq->nr_secs);
95 goto out;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020096 }
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) {
101 pr_err("pblk: failed GC read in line:%d (err:%d)\n",
102 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
132static void pblk_gc_line_prepare_ws(struct work_struct *work)
133{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200134 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
135 ws);
136 struct pblk *pblk = line_ws->pblk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200137 struct pblk_line *line = line_ws->line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200138 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
139 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200140 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200141 struct line_emeta *emeta_buf;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200142 struct pblk_line_ws *gc_rq_ws;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200143 struct pblk_gc_rq *gc_rq;
144 __le64 *lba_list;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200145 unsigned long *invalid_bitmap;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200146 int sec_left, nr_secs, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200147 int ret;
148
Javier Gonzálezd3401212017-10-13 14:46:14 +0200149 invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_KERNEL);
150 if (!invalid_bitmap) {
151 pr_err("pblk: could not allocate GC invalid bitmap\n");
152 goto fail_free_ws;
153 }
154
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200155 emeta_buf = pblk_malloc(lm->emeta_len[0], l_mg->emeta_alloc_type,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200156 GFP_KERNEL);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200157 if (!emeta_buf) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200158 pr_err("pblk: cannot use GC emeta\n");
Javier Gonzálezd3401212017-10-13 14:46:14 +0200159 goto fail_free_bitmap;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200160 }
161
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200162 ret = pblk_line_read_emeta(pblk, line, emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200163 if (ret) {
164 pr_err("pblk: line %d read emeta failed (%d)\n", line->id, ret);
165 goto fail_free_emeta;
166 }
167
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200168 /* If this read fails, it means that emeta is corrupted. For now, leave
169 * the line untouched. TODO: Implement a recovery routine that scans and
170 * moves all sectors on the line.
171 */
Hans Holmberg06bc0722018-01-05 14:16:08 +0100172
173 ret = pblk_recov_check_emeta(pblk, emeta_buf);
174 if (ret) {
175 pr_err("pblk: inconsistent emeta (line %d)\n", line->id);
176 goto fail_free_emeta;
177 }
178
179 lba_list = emeta_to_lbas(pblk, emeta_buf);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200180 if (!lba_list) {
181 pr_err("pblk: could not interpret emeta (line %d)\n", line->id);
182 goto fail_free_emeta;
183 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200184
Javier Gonzálezd3401212017-10-13 14:46:14 +0200185 spin_lock(&line->lock);
186 bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200187 sec_left = pblk_line_vsc(line);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200188 spin_unlock(&line->lock);
189
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200190 if (sec_left < 0) {
191 pr_err("pblk: corrupted GC line (%d)\n", line->id);
192 goto fail_free_emeta;
193 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200194
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200195 bit = -1;
196next_rq:
197 gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL);
198 if (!gc_rq)
199 goto fail_free_emeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200200
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200201 nr_secs = 0;
202 do {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200203 bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200204 bit + 1);
205 if (bit > line->emeta_ssec)
206 break;
207
Javier Gonzálezd3401212017-10-13 14:46:14 +0200208 gc_rq->paddr_list[nr_secs] = bit;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200209 gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]);
210 } while (nr_secs < pblk->max_write_pgs);
211
212 if (unlikely(!nr_secs)) {
213 kfree(gc_rq);
214 goto out;
215 }
216
217 gc_rq->nr_secs = nr_secs;
218 gc_rq->line = line;
219
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200220 gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
221 if (!gc_rq_ws)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200222 goto fail_free_gc_rq;
223
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200224 gc_rq_ws->pblk = pblk;
225 gc_rq_ws->line = line;
226 gc_rq_ws->priv = gc_rq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200227
Javier González8da10cc2017-10-13 14:46:45 +0200228 /* The write GC path can be much slower than the read GC one due to
229 * the budget imposed by the rate-limiter. Balance in case that we get
230 * back pressure from the write GC path.
231 */
232 while (down_timeout(&gc->gc_sem, msecs_to_jiffies(30000)))
233 io_schedule();
234
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200235 kref_get(&line->ref);
236
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200237 INIT_WORK(&gc_rq_ws->ws, pblk_gc_line_ws);
238 queue_work(gc->gc_line_reader_wq, &gc_rq_ws->ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200239
240 sec_left -= nr_secs;
241 if (sec_left > 0)
242 goto next_rq;
243
244out:
245 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200246 kfree(line_ws);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200247 kfree(invalid_bitmap);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200248
249 kref_put(&line->ref, pblk_line_put);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200250 atomic_dec(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200251
252 return;
253
254fail_free_gc_rq:
255 kfree(gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200256fail_free_emeta:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200257 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200258fail_free_bitmap:
259 kfree(invalid_bitmap);
260fail_free_ws:
261 kfree(line_ws);
262
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200263 pblk_put_line_back(pblk, line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200264 kref_put(&line->ref, pblk_line_put);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200265 atomic_dec(&gc->read_inflight_gc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200266
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200267 pr_err("pblk: Failed to GC line %d\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200268}
269
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200270static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200271{
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200272 struct pblk_gc *gc = &pblk->gc;
273 struct pblk_line_ws *line_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200274
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200275 pr_debug("pblk: line '%d' being reclaimed for GC\n", line->id);
276
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200277 line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200278 if (!line_ws)
279 return -ENOMEM;
280
281 line_ws->pblk = pblk;
282 line_ws->line = line;
283
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200284 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200285 INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws);
286 queue_work(gc->gc_reader_wq, &line_ws->ws);
287
288 return 0;
289}
290
Javier González8bd40022017-10-13 14:46:44 +0200291static void pblk_gc_reader_kick(struct pblk_gc *gc)
292{
293 wake_up_process(gc->gc_reader_ts);
294}
295
296static void pblk_gc_kick(struct pblk *pblk)
297{
298 struct pblk_gc *gc = &pblk->gc;
299
300 pblk_gc_writer_kick(gc);
301 pblk_gc_reader_kick(gc);
302
303 /* If we're shutting down GC, let's not start it up again */
304 if (gc->gc_enabled) {
305 wake_up_process(gc->gc_ts);
306 mod_timer(&gc->gc_timer,
307 jiffies + msecs_to_jiffies(GC_TIME_MSECS));
308 }
309}
310
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200311static int pblk_gc_read(struct pblk *pblk)
312{
313 struct pblk_gc *gc = &pblk->gc;
314 struct pblk_line *line;
315
316 spin_lock(&gc->r_lock);
317 if (list_empty(&gc->r_list)) {
318 spin_unlock(&gc->r_lock);
319 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200320 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200321
322 line = list_first_entry(&gc->r_list, struct pblk_line, list);
323 list_del(&line->list);
324 spin_unlock(&gc->r_lock);
325
326 pblk_gc_kick(pblk);
327
328 if (pblk_gc_line(pblk, line))
329 pr_err("pblk: failed to GC line %d\n", line->id);
330
331 return 0;
332}
333
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200334static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk,
335 struct list_head *group_list)
336{
337 struct pblk_line *line, *victim;
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200338 int line_vsc, victim_vsc;
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200339
340 victim = list_first_entry(group_list, struct pblk_line, list);
341 list_for_each_entry(line, group_list, list) {
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200342 line_vsc = le32_to_cpu(*line->vsc);
343 victim_vsc = le32_to_cpu(*victim->vsc);
344 if (line_vsc < victim_vsc)
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200345 victim = line;
346 }
347
348 return victim;
349}
350
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200351static bool pblk_gc_should_run(struct pblk_gc *gc, struct pblk_rl *rl)
352{
353 unsigned int nr_blocks_free, nr_blocks_need;
354
355 nr_blocks_need = pblk_rl_high_thrs(rl);
356 nr_blocks_free = pblk_rl_nr_free_blks(rl);
357
358 /* This is not critical, no need to take lock here */
359 return ((gc->gc_active) && (nr_blocks_need > nr_blocks_free));
360}
361
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200362void pblk_gc_free_full_lines(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200363{
364 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200365 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200366 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200367
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200368 do {
369 spin_lock(&l_mg->gc_lock);
370 if (list_empty(&l_mg->gc_full_list)) {
371 spin_unlock(&l_mg->gc_lock);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200372 return;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200373 }
374
375 line = list_first_entry(&l_mg->gc_full_list,
376 struct pblk_line, list);
377
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200378 spin_lock(&line->lock);
379 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
380 line->state = PBLK_LINESTATE_GC;
381 spin_unlock(&line->lock);
382
383 list_del(&line->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200384 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200385
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200386 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200387 kref_put(&line->ref, pblk_line_put);
388 } while (1);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200389}
390
391/*
392 * Lines with no valid sectors will be returned to the free list immediately. If
393 * GC is activated - either because the free block count is under the determined
394 * threshold, or because it is being forced from user space - only lines with a
395 * high count of invalid sectors will be recycled.
396 */
397static void pblk_gc_run(struct pblk *pblk)
398{
399 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
400 struct pblk_gc *gc = &pblk->gc;
401 struct pblk_line *line;
402 struct list_head *group_list;
403 bool run_gc;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200404 int read_inflight_gc, gc_group = 0, prev_group = 0;
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200405
406 pblk_gc_free_full_lines(pblk);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200407
408 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200409 if (!run_gc || (atomic_read(&gc->read_inflight_gc) >= PBLK_GC_L_QD))
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200410 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200411
412next_gc_group:
413 group_list = l_mg->gc_lists[gc_group++];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200414
415 do {
416 spin_lock(&l_mg->gc_lock);
417 if (list_empty(group_list)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200418 spin_unlock(&l_mg->gc_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200419 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200420 }
421
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200422 line = pblk_gc_get_victim_line(pblk, group_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200423
424 spin_lock(&line->lock);
425 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
426 line->state = PBLK_LINESTATE_GC;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200427 spin_unlock(&line->lock);
428
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200429 list_del(&line->list);
430 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200431
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200432 spin_lock(&gc->r_lock);
433 list_add_tail(&line->list, &gc->r_list);
434 spin_unlock(&gc->r_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200435
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200436 read_inflight_gc = atomic_inc_return(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200437 pblk_gc_reader_kick(gc);
438
439 prev_group = 1;
440
441 /* No need to queue up more GC lines than we can handle */
442 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200443 if (!run_gc || read_inflight_gc >= PBLK_GC_L_QD)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200444 break;
445 } while (1);
446
447 if (!prev_group && pblk->rl.rb_state > gc_group &&
448 gc_group < PBLK_GC_NR_LISTS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200449 goto next_gc_group;
450}
451
Kees Cook87c1d2d2017-10-17 21:10:19 -0700452static void pblk_gc_timer(struct timer_list *t)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200453{
Kees Cook87c1d2d2017-10-17 21:10:19 -0700454 struct pblk *pblk = from_timer(pblk, t, gc.gc_timer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200455
456 pblk_gc_kick(pblk);
457}
458
459static int pblk_gc_ts(void *data)
460{
461 struct pblk *pblk = data;
462
463 while (!kthread_should_stop()) {
464 pblk_gc_run(pblk);
465 set_current_state(TASK_INTERRUPTIBLE);
466 io_schedule();
467 }
468
469 return 0;
470}
471
472static int pblk_gc_writer_ts(void *data)
473{
474 struct pblk *pblk = data;
475
476 while (!kthread_should_stop()) {
477 if (!pblk_gc_write(pblk))
478 continue;
479 set_current_state(TASK_INTERRUPTIBLE);
480 io_schedule();
481 }
482
483 return 0;
484}
485
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200486static int pblk_gc_reader_ts(void *data)
487{
488 struct pblk *pblk = data;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200489 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200490
491 while (!kthread_should_stop()) {
492 if (!pblk_gc_read(pblk))
493 continue;
494 set_current_state(TASK_INTERRUPTIBLE);
495 io_schedule();
496 }
497
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200498#ifdef CONFIG_NVM_DEBUG
499 pr_info("pblk: flushing gc pipeline, %d lines left\n",
500 atomic_read(&gc->pipeline_gc));
501#endif
502
503 do {
504 if (!atomic_read(&gc->pipeline_gc))
505 break;
506
507 schedule();
508 } while (1);
509
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200510 return 0;
511}
512
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200513static void pblk_gc_start(struct pblk *pblk)
514{
515 pblk->gc.gc_active = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200516 pr_debug("pblk: gc start\n");
517}
518
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200519void pblk_gc_should_start(struct pblk *pblk)
520{
521 struct pblk_gc *gc = &pblk->gc;
522
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200523 if (gc->gc_enabled && !gc->gc_active) {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200524 pblk_gc_start(pblk);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200525 pblk_gc_kick(pblk);
526 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200527}
528
529/*
530 * If flush_wq == 1 then no lock should be held by the caller since
531 * flush_workqueue can sleep
532 */
533static void pblk_gc_stop(struct pblk *pblk, int flush_wq)
534{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200535 pblk->gc.gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200536 pr_debug("pblk: gc stop\n");
537}
538
539void pblk_gc_should_stop(struct pblk *pblk)
540{
541 struct pblk_gc *gc = &pblk->gc;
542
543 if (gc->gc_active && !gc->gc_forced)
544 pblk_gc_stop(pblk, 0);
545}
546
Hans Holmberg03661b52017-10-13 14:46:37 +0200547void pblk_gc_should_kick(struct pblk *pblk)
548{
549 pblk_rl_update_rates(&pblk->rl);
550}
551
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200552void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
553 int *gc_active)
554{
555 struct pblk_gc *gc = &pblk->gc;
556
557 spin_lock(&gc->lock);
558 *gc_enabled = gc->gc_enabled;
559 *gc_active = gc->gc_active;
560 spin_unlock(&gc->lock);
561}
562
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200563int pblk_gc_sysfs_force(struct pblk *pblk, int force)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200564{
565 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200566
567 if (force < 0 || force > 1)
568 return -EINVAL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200569
570 spin_lock(&gc->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200571 gc->gc_forced = force;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200572
573 if (force)
574 gc->gc_enabled = 1;
575 else
576 gc->gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200577 spin_unlock(&gc->lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200578
579 pblk_gc_should_start(pblk);
580
581 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582}
583
584int pblk_gc_init(struct pblk *pblk)
585{
586 struct pblk_gc *gc = &pblk->gc;
587 int ret;
588
589 gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts");
590 if (IS_ERR(gc->gc_ts)) {
591 pr_err("pblk: could not allocate GC main kthread\n");
592 return PTR_ERR(gc->gc_ts);
593 }
594
595 gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk,
596 "pblk-gc-writer-ts");
597 if (IS_ERR(gc->gc_writer_ts)) {
598 pr_err("pblk: could not allocate GC writer kthread\n");
599 ret = PTR_ERR(gc->gc_writer_ts);
600 goto fail_free_main_kthread;
601 }
602
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200603 gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk,
604 "pblk-gc-reader-ts");
605 if (IS_ERR(gc->gc_reader_ts)) {
606 pr_err("pblk: could not allocate GC reader kthread\n");
607 ret = PTR_ERR(gc->gc_reader_ts);
608 goto fail_free_writer_kthread;
609 }
610
Kees Cook87c1d2d2017-10-17 21:10:19 -0700611 timer_setup(&gc->gc_timer, pblk_gc_timer, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200612 mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS));
613
614 gc->gc_active = 0;
615 gc->gc_forced = 0;
616 gc->gc_enabled = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200617 gc->w_entries = 0;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200618 atomic_set(&gc->read_inflight_gc, 0);
619 atomic_set(&gc->pipeline_gc, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200620
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200621 /* Workqueue that reads valid sectors from a line and submit them to the
622 * GC writer to be recycled.
623 */
624 gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq",
625 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS);
626 if (!gc->gc_line_reader_wq) {
627 pr_err("pblk: could not allocate GC line reader workqueue\n");
628 ret = -ENOMEM;
629 goto fail_free_reader_kthread;
630 }
631
632 /* Workqueue that prepare lines for GC */
633 gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq",
634 WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200635 if (!gc->gc_reader_wq) {
636 pr_err("pblk: could not allocate GC reader workqueue\n");
637 ret = -ENOMEM;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200638 goto fail_free_reader_line_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200639 }
640
641 spin_lock_init(&gc->lock);
642 spin_lock_init(&gc->w_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200643 spin_lock_init(&gc->r_lock);
644
Javier González36278962017-10-13 14:46:11 +0200645 sema_init(&gc->gc_sem, PBLK_GC_RQ_QD);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200646
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200647 INIT_LIST_HEAD(&gc->w_list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200648 INIT_LIST_HEAD(&gc->r_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200649
650 return 0;
651
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200652fail_free_reader_line_wq:
653 destroy_workqueue(gc->gc_line_reader_wq);
654fail_free_reader_kthread:
655 kthread_stop(gc->gc_reader_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200656fail_free_writer_kthread:
657 kthread_stop(gc->gc_writer_ts);
Dan Carpenter503ec942017-04-15 20:55:51 +0200658fail_free_main_kthread:
659 kthread_stop(gc->gc_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200660
661 return ret;
662}
663
664void pblk_gc_exit(struct pblk *pblk)
665{
666 struct pblk_gc *gc = &pblk->gc;
667
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200668 gc->gc_enabled = 0;
669 del_timer_sync(&gc->gc_timer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200670 pblk_gc_stop(pblk, 1);
671
672 if (gc->gc_ts)
673 kthread_stop(gc->gc_ts);
674
Hans Holmberg1edebac2017-10-13 14:46:39 +0200675 if (gc->gc_reader_ts)
676 kthread_stop(gc->gc_reader_ts);
677
678 flush_workqueue(gc->gc_reader_wq);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200679 if (gc->gc_reader_wq)
680 destroy_workqueue(gc->gc_reader_wq);
681
Hans Holmberg1edebac2017-10-13 14:46:39 +0200682 flush_workqueue(gc->gc_line_reader_wq);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200683 if (gc->gc_line_reader_wq)
684 destroy_workqueue(gc->gc_line_reader_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200685
686 if (gc->gc_writer_ts)
687 kthread_stop(gc->gc_writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200688}