blob: b8323e34d1bcc06b49442b5509630949443ecf38 [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 */
172 lba_list = pblk_recov_get_lba_list(pblk, emeta_buf);
173 if (!lba_list) {
174 pr_err("pblk: could not interpret emeta (line %d)\n", line->id);
175 goto fail_free_emeta;
176 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200177
Javier Gonzálezd3401212017-10-13 14:46:14 +0200178 spin_lock(&line->lock);
179 bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200180 sec_left = pblk_line_vsc(line);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200181 spin_unlock(&line->lock);
182
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200183 if (sec_left < 0) {
184 pr_err("pblk: corrupted GC line (%d)\n", line->id);
185 goto fail_free_emeta;
186 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200187
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200188 bit = -1;
189next_rq:
190 gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL);
191 if (!gc_rq)
192 goto fail_free_emeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200193
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200194 nr_secs = 0;
195 do {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200196 bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200197 bit + 1);
198 if (bit > line->emeta_ssec)
199 break;
200
Javier Gonzálezd3401212017-10-13 14:46:14 +0200201 gc_rq->paddr_list[nr_secs] = bit;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200202 gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]);
203 } while (nr_secs < pblk->max_write_pgs);
204
205 if (unlikely(!nr_secs)) {
206 kfree(gc_rq);
207 goto out;
208 }
209
210 gc_rq->nr_secs = nr_secs;
211 gc_rq->line = line;
212
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200213 gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
214 if (!gc_rq_ws)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200215 goto fail_free_gc_rq;
216
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200217 gc_rq_ws->pblk = pblk;
218 gc_rq_ws->line = line;
219 gc_rq_ws->priv = gc_rq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200220
221 down(&gc->gc_sem);
222 kref_get(&line->ref);
223
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200224 INIT_WORK(&gc_rq_ws->ws, pblk_gc_line_ws);
225 queue_work(gc->gc_line_reader_wq, &gc_rq_ws->ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200226
227 sec_left -= nr_secs;
228 if (sec_left > 0)
229 goto next_rq;
230
231out:
232 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200233 kfree(line_ws);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200234 kfree(invalid_bitmap);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200235
236 kref_put(&line->ref, pblk_line_put);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200237 atomic_dec(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200238
239 return;
240
241fail_free_gc_rq:
242 kfree(gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200243fail_free_emeta:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200244 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200245fail_free_bitmap:
246 kfree(invalid_bitmap);
247fail_free_ws:
248 kfree(line_ws);
249
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200250 pblk_put_line_back(pblk, line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200251 kref_put(&line->ref, pblk_line_put);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200252 atomic_dec(&gc->read_inflight_gc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200253
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200254 pr_err("pblk: Failed to GC line %d\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200255}
256
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200257static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200258{
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200259 struct pblk_gc *gc = &pblk->gc;
260 struct pblk_line_ws *line_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200261
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200262 pr_debug("pblk: line '%d' being reclaimed for GC\n", line->id);
263
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200264 line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200265 if (!line_ws)
266 return -ENOMEM;
267
268 line_ws->pblk = pblk;
269 line_ws->line = line;
270
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200271 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200272 INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws);
273 queue_work(gc->gc_reader_wq, &line_ws->ws);
274
275 return 0;
276}
277
Javier González8bd40022017-10-13 14:46:44 +0200278static void pblk_gc_reader_kick(struct pblk_gc *gc)
279{
280 wake_up_process(gc->gc_reader_ts);
281}
282
283static void pblk_gc_kick(struct pblk *pblk)
284{
285 struct pblk_gc *gc = &pblk->gc;
286
287 pblk_gc_writer_kick(gc);
288 pblk_gc_reader_kick(gc);
289
290 /* If we're shutting down GC, let's not start it up again */
291 if (gc->gc_enabled) {
292 wake_up_process(gc->gc_ts);
293 mod_timer(&gc->gc_timer,
294 jiffies + msecs_to_jiffies(GC_TIME_MSECS));
295 }
296}
297
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200298static int pblk_gc_read(struct pblk *pblk)
299{
300 struct pblk_gc *gc = &pblk->gc;
301 struct pblk_line *line;
302
303 spin_lock(&gc->r_lock);
304 if (list_empty(&gc->r_list)) {
305 spin_unlock(&gc->r_lock);
306 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200307 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200308
309 line = list_first_entry(&gc->r_list, struct pblk_line, list);
310 list_del(&line->list);
311 spin_unlock(&gc->r_lock);
312
313 pblk_gc_kick(pblk);
314
315 if (pblk_gc_line(pblk, line))
316 pr_err("pblk: failed to GC line %d\n", line->id);
317
318 return 0;
319}
320
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200321static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk,
322 struct list_head *group_list)
323{
324 struct pblk_line *line, *victim;
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200325 int line_vsc, victim_vsc;
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200326
327 victim = list_first_entry(group_list, struct pblk_line, list);
328 list_for_each_entry(line, group_list, list) {
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200329 line_vsc = le32_to_cpu(*line->vsc);
330 victim_vsc = le32_to_cpu(*victim->vsc);
331 if (line_vsc < victim_vsc)
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200332 victim = line;
333 }
334
335 return victim;
336}
337
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200338static bool pblk_gc_should_run(struct pblk_gc *gc, struct pblk_rl *rl)
339{
340 unsigned int nr_blocks_free, nr_blocks_need;
341
342 nr_blocks_need = pblk_rl_high_thrs(rl);
343 nr_blocks_free = pblk_rl_nr_free_blks(rl);
344
345 /* This is not critical, no need to take lock here */
346 return ((gc->gc_active) && (nr_blocks_need > nr_blocks_free));
347}
348
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200349void pblk_gc_free_full_lines(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200350{
351 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200352 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200353 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200354
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200355 do {
356 spin_lock(&l_mg->gc_lock);
357 if (list_empty(&l_mg->gc_full_list)) {
358 spin_unlock(&l_mg->gc_lock);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200359 return;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200360 }
361
362 line = list_first_entry(&l_mg->gc_full_list,
363 struct pblk_line, list);
364
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200365 spin_lock(&line->lock);
366 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
367 line->state = PBLK_LINESTATE_GC;
368 spin_unlock(&line->lock);
369
370 list_del(&line->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200371 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200372
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200373 atomic_inc(&gc->pipeline_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200374 kref_put(&line->ref, pblk_line_put);
375 } while (1);
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200376}
377
378/*
379 * Lines with no valid sectors will be returned to the free list immediately. If
380 * GC is activated - either because the free block count is under the determined
381 * threshold, or because it is being forced from user space - only lines with a
382 * high count of invalid sectors will be recycled.
383 */
384static void pblk_gc_run(struct pblk *pblk)
385{
386 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
387 struct pblk_gc *gc = &pblk->gc;
388 struct pblk_line *line;
389 struct list_head *group_list;
390 bool run_gc;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200391 int read_inflight_gc, gc_group = 0, prev_group = 0;
Hans Holmberg37ce33d2017-10-13 14:46:36 +0200392
393 pblk_gc_free_full_lines(pblk);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200394
395 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200396 if (!run_gc || (atomic_read(&gc->read_inflight_gc) >= PBLK_GC_L_QD))
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200397 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200398
399next_gc_group:
400 group_list = l_mg->gc_lists[gc_group++];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200401
402 do {
403 spin_lock(&l_mg->gc_lock);
404 if (list_empty(group_list)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200405 spin_unlock(&l_mg->gc_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200406 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200407 }
408
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200409 line = pblk_gc_get_victim_line(pblk, group_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200410
411 spin_lock(&line->lock);
412 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
413 line->state = PBLK_LINESTATE_GC;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200414 spin_unlock(&line->lock);
415
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200416 list_del(&line->list);
417 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200418
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200419 spin_lock(&gc->r_lock);
420 list_add_tail(&line->list, &gc->r_list);
421 spin_unlock(&gc->r_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200422
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200423 read_inflight_gc = atomic_inc_return(&gc->read_inflight_gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200424 pblk_gc_reader_kick(gc);
425
426 prev_group = 1;
427
428 /* No need to queue up more GC lines than we can handle */
429 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200430 if (!run_gc || read_inflight_gc >= PBLK_GC_L_QD)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200431 break;
432 } while (1);
433
434 if (!prev_group && pblk->rl.rb_state > gc_group &&
435 gc_group < PBLK_GC_NR_LISTS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200436 goto next_gc_group;
437}
438
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200439static void pblk_gc_timer(unsigned long data)
440{
441 struct pblk *pblk = (struct pblk *)data;
442
443 pblk_gc_kick(pblk);
444}
445
446static int pblk_gc_ts(void *data)
447{
448 struct pblk *pblk = data;
449
450 while (!kthread_should_stop()) {
451 pblk_gc_run(pblk);
452 set_current_state(TASK_INTERRUPTIBLE);
453 io_schedule();
454 }
455
456 return 0;
457}
458
459static int pblk_gc_writer_ts(void *data)
460{
461 struct pblk *pblk = data;
462
463 while (!kthread_should_stop()) {
464 if (!pblk_gc_write(pblk))
465 continue;
466 set_current_state(TASK_INTERRUPTIBLE);
467 io_schedule();
468 }
469
470 return 0;
471}
472
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200473static int pblk_gc_reader_ts(void *data)
474{
475 struct pblk *pblk = data;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200476 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200477
478 while (!kthread_should_stop()) {
479 if (!pblk_gc_read(pblk))
480 continue;
481 set_current_state(TASK_INTERRUPTIBLE);
482 io_schedule();
483 }
484
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200485#ifdef CONFIG_NVM_DEBUG
486 pr_info("pblk: flushing gc pipeline, %d lines left\n",
487 atomic_read(&gc->pipeline_gc));
488#endif
489
490 do {
491 if (!atomic_read(&gc->pipeline_gc))
492 break;
493
494 schedule();
495 } while (1);
496
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200497 return 0;
498}
499
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200500static void pblk_gc_start(struct pblk *pblk)
501{
502 pblk->gc.gc_active = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200503 pr_debug("pblk: gc start\n");
504}
505
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200506void pblk_gc_should_start(struct pblk *pblk)
507{
508 struct pblk_gc *gc = &pblk->gc;
509
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200510 if (gc->gc_enabled && !gc->gc_active) {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200511 pblk_gc_start(pblk);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200512 pblk_gc_kick(pblk);
513 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200514}
515
516/*
517 * If flush_wq == 1 then no lock should be held by the caller since
518 * flush_workqueue can sleep
519 */
520static void pblk_gc_stop(struct pblk *pblk, int flush_wq)
521{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200522 pblk->gc.gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200523 pr_debug("pblk: gc stop\n");
524}
525
526void pblk_gc_should_stop(struct pblk *pblk)
527{
528 struct pblk_gc *gc = &pblk->gc;
529
530 if (gc->gc_active && !gc->gc_forced)
531 pblk_gc_stop(pblk, 0);
532}
533
Hans Holmberg03661b52017-10-13 14:46:37 +0200534void pblk_gc_should_kick(struct pblk *pblk)
535{
536 pblk_rl_update_rates(&pblk->rl);
537}
538
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200539void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
540 int *gc_active)
541{
542 struct pblk_gc *gc = &pblk->gc;
543
544 spin_lock(&gc->lock);
545 *gc_enabled = gc->gc_enabled;
546 *gc_active = gc->gc_active;
547 spin_unlock(&gc->lock);
548}
549
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200550int pblk_gc_sysfs_force(struct pblk *pblk, int force)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200551{
552 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200553
554 if (force < 0 || force > 1)
555 return -EINVAL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200556
557 spin_lock(&gc->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200558 gc->gc_forced = force;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200559
560 if (force)
561 gc->gc_enabled = 1;
562 else
563 gc->gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200564 spin_unlock(&gc->lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200565
566 pblk_gc_should_start(pblk);
567
568 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200569}
570
571int pblk_gc_init(struct pblk *pblk)
572{
573 struct pblk_gc *gc = &pblk->gc;
574 int ret;
575
576 gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts");
577 if (IS_ERR(gc->gc_ts)) {
578 pr_err("pblk: could not allocate GC main kthread\n");
579 return PTR_ERR(gc->gc_ts);
580 }
581
582 gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk,
583 "pblk-gc-writer-ts");
584 if (IS_ERR(gc->gc_writer_ts)) {
585 pr_err("pblk: could not allocate GC writer kthread\n");
586 ret = PTR_ERR(gc->gc_writer_ts);
587 goto fail_free_main_kthread;
588 }
589
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200590 gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk,
591 "pblk-gc-reader-ts");
592 if (IS_ERR(gc->gc_reader_ts)) {
593 pr_err("pblk: could not allocate GC reader kthread\n");
594 ret = PTR_ERR(gc->gc_reader_ts);
595 goto fail_free_writer_kthread;
596 }
597
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200598 setup_timer(&gc->gc_timer, pblk_gc_timer, (unsigned long)pblk);
599 mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS));
600
601 gc->gc_active = 0;
602 gc->gc_forced = 0;
603 gc->gc_enabled = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200604 gc->w_entries = 0;
Hans Holmbergd6b992f2017-10-13 14:46:41 +0200605 atomic_set(&gc->read_inflight_gc, 0);
606 atomic_set(&gc->pipeline_gc, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200608 /* Workqueue that reads valid sectors from a line and submit them to the
609 * GC writer to be recycled.
610 */
611 gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq",
612 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS);
613 if (!gc->gc_line_reader_wq) {
614 pr_err("pblk: could not allocate GC line reader workqueue\n");
615 ret = -ENOMEM;
616 goto fail_free_reader_kthread;
617 }
618
619 /* Workqueue that prepare lines for GC */
620 gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq",
621 WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200622 if (!gc->gc_reader_wq) {
623 pr_err("pblk: could not allocate GC reader workqueue\n");
624 ret = -ENOMEM;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200625 goto fail_free_reader_line_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200626 }
627
628 spin_lock_init(&gc->lock);
629 spin_lock_init(&gc->w_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200630 spin_lock_init(&gc->r_lock);
631
Javier González36278962017-10-13 14:46:11 +0200632 sema_init(&gc->gc_sem, PBLK_GC_RQ_QD);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200633
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200634 INIT_LIST_HEAD(&gc->w_list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200635 INIT_LIST_HEAD(&gc->r_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200636
637 return 0;
638
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200639fail_free_reader_line_wq:
640 destroy_workqueue(gc->gc_line_reader_wq);
641fail_free_reader_kthread:
642 kthread_stop(gc->gc_reader_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200643fail_free_writer_kthread:
644 kthread_stop(gc->gc_writer_ts);
Dan Carpenter503ec942017-04-15 20:55:51 +0200645fail_free_main_kthread:
646 kthread_stop(gc->gc_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200647
648 return ret;
649}
650
651void pblk_gc_exit(struct pblk *pblk)
652{
653 struct pblk_gc *gc = &pblk->gc;
654
Hans Holmberg3e3a5b82017-10-13 14:46:34 +0200655 gc->gc_enabled = 0;
656 del_timer_sync(&gc->gc_timer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200657 pblk_gc_stop(pblk, 1);
658
659 if (gc->gc_ts)
660 kthread_stop(gc->gc_ts);
661
Hans Holmberg1edebac2017-10-13 14:46:39 +0200662 if (gc->gc_reader_ts)
663 kthread_stop(gc->gc_reader_ts);
664
665 flush_workqueue(gc->gc_reader_wq);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200666 if (gc->gc_reader_wq)
667 destroy_workqueue(gc->gc_reader_wq);
668
Hans Holmberg1edebac2017-10-13 14:46:39 +0200669 flush_workqueue(gc->gc_line_reader_wq);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200670 if (gc->gc_line_reader_wq)
671 destroy_workqueue(gc->gc_line_reader_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200672
673 if (gc->gc_writer_ts)
674 kthread_stop(gc->gc_writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200675}