blob: 7ad0cfe58a21d977d099dd6749cad6b627981c6c [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
59/*
60 * Responsible for managing all memory related to a gc request. Also in case of
61 * failure
62 */
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020063static int pblk_gc_move_valid_secs(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020064{
65 struct nvm_tgt_dev *dev = pblk->dev;
66 struct nvm_geo *geo = &dev->geo;
67 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020068 struct pblk_line *line = gc_rq->line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020069 void *data;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020070 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020071
Javier Gonzálezde54e702017-06-30 17:56:39 +020072 data = vmalloc(gc_rq->nr_secs * geo->sec_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020073 if (!data) {
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020074 ret = -ENOMEM;
Javier Gonzálezd3401212017-10-13 14:46:14 +020075 goto fail;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020076 }
77
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020078 gc_rq->data = data;
Javier Gonzálezd3401212017-10-13 14:46:14 +020079
80 /* Read from GC victim block */
81 ret = pblk_submit_read_gc(pblk, gc_rq);
82 if (ret)
83 goto fail;
84
85 if (!gc_rq->secs_to_gc)
86 goto fail;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020087
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020088retry:
89 spin_lock(&gc->w_lock);
Javier González36278962017-10-13 14:46:11 +020090 if (gc->w_entries >= PBLK_GC_RQ_QD) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020091 spin_unlock(&gc->w_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +020092 pblk_gc_writer_kick(&pblk->gc);
93 usleep_range(128, 256);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020094 goto retry;
95 }
96 gc->w_entries++;
97 list_add_tail(&gc_rq->list, &gc->w_list);
98 spin_unlock(&gc->w_lock);
99
100 pblk_gc_writer_kick(&pblk->gc);
101
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200102 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200103
Javier Gonzálezd3401212017-10-13 14:46:14 +0200104fail:
105 pblk_gc_free_gc_rq(gc_rq);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200106 kref_put(&line->ref, pblk_line_put);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200107 return ret;
108}
109
110static void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line)
111{
112 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
113 struct list_head *move_list;
114
115 spin_lock(&line->lock);
116 WARN_ON(line->state != PBLK_LINESTATE_GC);
117 line->state = PBLK_LINESTATE_CLOSED;
118 move_list = pblk_line_gc_list(pblk, line);
119 spin_unlock(&line->lock);
120
121 if (move_list) {
122 spin_lock(&l_mg->gc_lock);
123 list_add_tail(&line->list, move_list);
124 spin_unlock(&l_mg->gc_lock);
125 }
126}
127
128static void pblk_gc_line_ws(struct work_struct *work)
129{
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200130 struct pblk_line_ws *gc_rq_ws = container_of(work,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200131 struct pblk_line_ws, ws);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200132 struct pblk *pblk = gc_rq_ws->pblk;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200133 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200134 struct pblk_line *line = gc_rq_ws->line;
135 struct pblk_gc_rq *gc_rq = gc_rq_ws->priv;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200136
137 up(&gc->gc_sem);
138
139 if (pblk_gc_move_valid_secs(pblk, gc_rq)) {
140 pr_err("pblk: could not GC all sectors: line:%d (%d/%d)\n",
141 line->id, *line->vsc,
142 gc_rq->nr_secs);
143 }
144
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200145 kfree(gc_rq_ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200146}
147
148static void pblk_gc_line_prepare_ws(struct work_struct *work)
149{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200150 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
151 ws);
152 struct pblk *pblk = line_ws->pblk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200153 struct pblk_line *line = line_ws->line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200154 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
155 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200156 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200157 struct line_emeta *emeta_buf;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200158 struct pblk_line_ws *gc_rq_ws;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200159 struct pblk_gc_rq *gc_rq;
160 __le64 *lba_list;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200161 unsigned long *invalid_bitmap;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200162 int sec_left, nr_secs, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200163 int ret;
164
Javier Gonzálezd3401212017-10-13 14:46:14 +0200165 invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_KERNEL);
166 if (!invalid_bitmap) {
167 pr_err("pblk: could not allocate GC invalid bitmap\n");
168 goto fail_free_ws;
169 }
170
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200171 emeta_buf = pblk_malloc(lm->emeta_len[0], l_mg->emeta_alloc_type,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200172 GFP_KERNEL);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200173 if (!emeta_buf) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200174 pr_err("pblk: cannot use GC emeta\n");
Javier Gonzálezd3401212017-10-13 14:46:14 +0200175 goto fail_free_bitmap;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200176 }
177
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200178 ret = pblk_line_read_emeta(pblk, line, emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200179 if (ret) {
180 pr_err("pblk: line %d read emeta failed (%d)\n", line->id, ret);
181 goto fail_free_emeta;
182 }
183
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200184 /* If this read fails, it means that emeta is corrupted. For now, leave
185 * the line untouched. TODO: Implement a recovery routine that scans and
186 * moves all sectors on the line.
187 */
188 lba_list = pblk_recov_get_lba_list(pblk, emeta_buf);
189 if (!lba_list) {
190 pr_err("pblk: could not interpret emeta (line %d)\n", line->id);
191 goto fail_free_emeta;
192 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200193
Javier Gonzálezd3401212017-10-13 14:46:14 +0200194 spin_lock(&line->lock);
195 bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200196 sec_left = pblk_line_vsc(line);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200197 spin_unlock(&line->lock);
198
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200199 if (sec_left < 0) {
200 pr_err("pblk: corrupted GC line (%d)\n", line->id);
201 goto fail_free_emeta;
202 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200203
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200204 bit = -1;
205next_rq:
206 gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL);
207 if (!gc_rq)
208 goto fail_free_emeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200209
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200210 nr_secs = 0;
211 do {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200212 bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line,
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200213 bit + 1);
214 if (bit > line->emeta_ssec)
215 break;
216
Javier Gonzálezd3401212017-10-13 14:46:14 +0200217 gc_rq->paddr_list[nr_secs] = bit;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200218 gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]);
219 } while (nr_secs < pblk->max_write_pgs);
220
221 if (unlikely(!nr_secs)) {
222 kfree(gc_rq);
223 goto out;
224 }
225
226 gc_rq->nr_secs = nr_secs;
227 gc_rq->line = line;
228
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200229 gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
230 if (!gc_rq_ws)
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200231 goto fail_free_gc_rq;
232
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200233 gc_rq_ws->pblk = pblk;
234 gc_rq_ws->line = line;
235 gc_rq_ws->priv = gc_rq;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200236
237 down(&gc->gc_sem);
238 kref_get(&line->ref);
239
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200240 INIT_WORK(&gc_rq_ws->ws, pblk_gc_line_ws);
241 queue_work(gc->gc_line_reader_wq, &gc_rq_ws->ws);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200242
243 sec_left -= nr_secs;
244 if (sec_left > 0)
245 goto next_rq;
246
247out:
248 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200249 kfree(line_ws);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200250 kfree(invalid_bitmap);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200251
252 kref_put(&line->ref, pblk_line_put);
253 atomic_dec(&gc->inflight_gc);
254
255 return;
256
257fail_free_gc_rq:
258 kfree(gc_rq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200259fail_free_emeta:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200260 pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200261fail_free_bitmap:
262 kfree(invalid_bitmap);
263fail_free_ws:
264 kfree(line_ws);
265
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200266 pblk_put_line_back(pblk, line);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200267 kref_put(&line->ref, pblk_line_put);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200268 atomic_dec(&gc->inflight_gc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200269
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200270 pr_err("pblk: Failed to GC line %d\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200271}
272
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200273static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200274{
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200275 struct pblk_gc *gc = &pblk->gc;
276 struct pblk_line_ws *line_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200277
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200278 pr_debug("pblk: line '%d' being reclaimed for GC\n", line->id);
279
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200280 line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200281 if (!line_ws)
282 return -ENOMEM;
283
284 line_ws->pblk = pblk;
285 line_ws->line = line;
286
287 INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws);
288 queue_work(gc->gc_reader_wq, &line_ws->ws);
289
290 return 0;
291}
292
293static int pblk_gc_read(struct pblk *pblk)
294{
295 struct pblk_gc *gc = &pblk->gc;
296 struct pblk_line *line;
297
298 spin_lock(&gc->r_lock);
299 if (list_empty(&gc->r_list)) {
300 spin_unlock(&gc->r_lock);
301 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200302 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200303
304 line = list_first_entry(&gc->r_list, struct pblk_line, list);
305 list_del(&line->list);
306 spin_unlock(&gc->r_lock);
307
308 pblk_gc_kick(pblk);
309
310 if (pblk_gc_line(pblk, line))
311 pr_err("pblk: failed to GC line %d\n", line->id);
312
313 return 0;
314}
315
316static void pblk_gc_reader_kick(struct pblk_gc *gc)
317{
318 wake_up_process(gc->gc_reader_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200319}
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
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200349/*
350 * Lines with no valid sectors will be returned to the free list immediately. If
351 * GC is activated - either because the free block count is under the determined
352 * threshold, or because it is being forced from user space - only lines with a
353 * high count of invalid sectors will be recycled.
354 */
355static void pblk_gc_run(struct pblk *pblk)
356{
357 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
358 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200359 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200360 struct list_head *group_list;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200361 bool run_gc;
362 int inflight_gc, gc_group = 0, prev_group = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200363
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200364 do {
365 spin_lock(&l_mg->gc_lock);
366 if (list_empty(&l_mg->gc_full_list)) {
367 spin_unlock(&l_mg->gc_lock);
368 break;
369 }
370
371 line = list_first_entry(&l_mg->gc_full_list,
372 struct pblk_line, list);
373
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200374 spin_lock(&line->lock);
375 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
376 line->state = PBLK_LINESTATE_GC;
377 spin_unlock(&line->lock);
378
379 list_del(&line->list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200380 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200381
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200382 kref_put(&line->ref, pblk_line_put);
383 } while (1);
384
385 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
386 if (!run_gc || (atomic_read(&gc->inflight_gc) >= PBLK_GC_L_QD))
387 return;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200388
389next_gc_group:
390 group_list = l_mg->gc_lists[gc_group++];
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200391
392 do {
393 spin_lock(&l_mg->gc_lock);
394 if (list_empty(group_list)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200395 spin_unlock(&l_mg->gc_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200396 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200397 }
398
Javier Gonzálezd45ebd42017-06-26 11:57:23 +0200399 line = pblk_gc_get_victim_line(pblk, group_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200400
401 spin_lock(&line->lock);
402 WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
403 line->state = PBLK_LINESTATE_GC;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200404 spin_unlock(&line->lock);
405
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200406 list_del(&line->list);
407 spin_unlock(&l_mg->gc_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200408
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200409 spin_lock(&gc->r_lock);
410 list_add_tail(&line->list, &gc->r_list);
411 spin_unlock(&gc->r_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200412
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200413 inflight_gc = atomic_inc_return(&gc->inflight_gc);
414 pblk_gc_reader_kick(gc);
415
416 prev_group = 1;
417
418 /* No need to queue up more GC lines than we can handle */
419 run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
420 if (!run_gc || inflight_gc >= PBLK_GC_L_QD)
421 break;
422 } while (1);
423
424 if (!prev_group && pblk->rl.rb_state > gc_group &&
425 gc_group < PBLK_GC_NR_LISTS)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200426 goto next_gc_group;
427}
428
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200429void pblk_gc_kick(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200430{
431 struct pblk_gc *gc = &pblk->gc;
432
433 wake_up_process(gc->gc_ts);
434 pblk_gc_writer_kick(gc);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200435 pblk_gc_reader_kick(gc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200436 mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS));
437}
438
439static 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;
476
477 while (!kthread_should_stop()) {
478 if (!pblk_gc_read(pblk))
479 continue;
480 set_current_state(TASK_INTERRUPTIBLE);
481 io_schedule();
482 }
483
484 return 0;
485}
486
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200487static void pblk_gc_start(struct pblk *pblk)
488{
489 pblk->gc.gc_active = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200490 pr_debug("pblk: gc start\n");
491}
492
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200493void pblk_gc_should_start(struct pblk *pblk)
494{
495 struct pblk_gc *gc = &pblk->gc;
496
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200497 if (gc->gc_enabled && !gc->gc_active)
498 pblk_gc_start(pblk);
499
500 pblk_gc_kick(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200501}
502
503/*
504 * If flush_wq == 1 then no lock should be held by the caller since
505 * flush_workqueue can sleep
506 */
507static void pblk_gc_stop(struct pblk *pblk, int flush_wq)
508{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200509 pblk->gc.gc_active = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200510 pr_debug("pblk: gc stop\n");
511}
512
513void pblk_gc_should_stop(struct pblk *pblk)
514{
515 struct pblk_gc *gc = &pblk->gc;
516
517 if (gc->gc_active && !gc->gc_forced)
518 pblk_gc_stop(pblk, 0);
519}
520
521void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
522 int *gc_active)
523{
524 struct pblk_gc *gc = &pblk->gc;
525
526 spin_lock(&gc->lock);
527 *gc_enabled = gc->gc_enabled;
528 *gc_active = gc->gc_active;
529 spin_unlock(&gc->lock);
530}
531
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200532int pblk_gc_sysfs_force(struct pblk *pblk, int force)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200533{
534 struct pblk_gc *gc = &pblk->gc;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200535
536 if (force < 0 || force > 1)
537 return -EINVAL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200538
539 spin_lock(&gc->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200540 gc->gc_forced = force;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200541
542 if (force)
543 gc->gc_enabled = 1;
544 else
545 gc->gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200546 spin_unlock(&gc->lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200547
548 pblk_gc_should_start(pblk);
549
550 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200551}
552
553int pblk_gc_init(struct pblk *pblk)
554{
555 struct pblk_gc *gc = &pblk->gc;
556 int ret;
557
558 gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts");
559 if (IS_ERR(gc->gc_ts)) {
560 pr_err("pblk: could not allocate GC main kthread\n");
561 return PTR_ERR(gc->gc_ts);
562 }
563
564 gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk,
565 "pblk-gc-writer-ts");
566 if (IS_ERR(gc->gc_writer_ts)) {
567 pr_err("pblk: could not allocate GC writer kthread\n");
568 ret = PTR_ERR(gc->gc_writer_ts);
569 goto fail_free_main_kthread;
570 }
571
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200572 gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk,
573 "pblk-gc-reader-ts");
574 if (IS_ERR(gc->gc_reader_ts)) {
575 pr_err("pblk: could not allocate GC reader kthread\n");
576 ret = PTR_ERR(gc->gc_reader_ts);
577 goto fail_free_writer_kthread;
578 }
579
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200580 setup_timer(&gc->gc_timer, pblk_gc_timer, (unsigned long)pblk);
581 mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS));
582
583 gc->gc_active = 0;
584 gc->gc_forced = 0;
585 gc->gc_enabled = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200586 gc->w_entries = 0;
587 atomic_set(&gc->inflight_gc, 0);
588
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200589 /* Workqueue that reads valid sectors from a line and submit them to the
590 * GC writer to be recycled.
591 */
592 gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq",
593 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS);
594 if (!gc->gc_line_reader_wq) {
595 pr_err("pblk: could not allocate GC line reader workqueue\n");
596 ret = -ENOMEM;
597 goto fail_free_reader_kthread;
598 }
599
600 /* Workqueue that prepare lines for GC */
601 gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq",
602 WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200603 if (!gc->gc_reader_wq) {
604 pr_err("pblk: could not allocate GC reader workqueue\n");
605 ret = -ENOMEM;
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200606 goto fail_free_reader_line_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607 }
608
609 spin_lock_init(&gc->lock);
610 spin_lock_init(&gc->w_lock);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200611 spin_lock_init(&gc->r_lock);
612
Javier González36278962017-10-13 14:46:11 +0200613 sema_init(&gc->gc_sem, PBLK_GC_RQ_QD);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200614
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200615 INIT_LIST_HEAD(&gc->w_list);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200616 INIT_LIST_HEAD(&gc->r_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200617
618 return 0;
619
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200620fail_free_reader_line_wq:
621 destroy_workqueue(gc->gc_line_reader_wq);
622fail_free_reader_kthread:
623 kthread_stop(gc->gc_reader_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200624fail_free_writer_kthread:
625 kthread_stop(gc->gc_writer_ts);
Dan Carpenter503ec942017-04-15 20:55:51 +0200626fail_free_main_kthread:
627 kthread_stop(gc->gc_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200628
629 return ret;
630}
631
632void pblk_gc_exit(struct pblk *pblk)
633{
634 struct pblk_gc *gc = &pblk->gc;
635
636 flush_workqueue(gc->gc_reader_wq);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200637 flush_workqueue(gc->gc_line_reader_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200638
639 del_timer(&gc->gc_timer);
640 pblk_gc_stop(pblk, 1);
641
642 if (gc->gc_ts)
643 kthread_stop(gc->gc_ts);
644
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200645 if (gc->gc_reader_wq)
646 destroy_workqueue(gc->gc_reader_wq);
647
648 if (gc->gc_line_reader_wq)
649 destroy_workqueue(gc->gc_line_reader_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200650
651 if (gc->gc_writer_ts)
652 kthread_stop(gc->gc_writer_ts);
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200653
654 if (gc->gc_reader_ts)
655 kthread_stop(gc->gc_reader_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200656}