Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | static void pblk_gc_free_gc_rq(struct pblk_gc_rq *gc_rq) |
| 22 | { |
| 23 | kfree(gc_rq->data); |
| 24 | kfree(gc_rq->lba_list); |
| 25 | kfree(gc_rq); |
| 26 | } |
| 27 | |
| 28 | static 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 | |
| 40 | list_for_each_entry_safe(gc_rq, tgc_rq, &gc->w_list, list) { |
| 41 | list_move_tail(&gc_rq->list, &w_list); |
| 42 | gc->w_entries--; |
| 43 | } |
| 44 | spin_unlock(&gc->w_lock); |
| 45 | |
| 46 | list_for_each_entry_safe(gc_rq, tgc_rq, &w_list, list) { |
| 47 | pblk_write_gc_to_cache(pblk, gc_rq->data, gc_rq->lba_list, |
| 48 | gc_rq->nr_secs, gc_rq->secs_to_gc, |
| 49 | gc_rq->line, PBLK_IOTYPE_GC); |
| 50 | |
| 51 | kref_put(&gc_rq->line->ref, pblk_line_put); |
| 52 | |
| 53 | list_del(&gc_rq->list); |
| 54 | pblk_gc_free_gc_rq(gc_rq); |
| 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static void pblk_gc_writer_kick(struct pblk_gc *gc) |
| 61 | { |
| 62 | wake_up_process(gc->gc_writer_ts); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Responsible for managing all memory related to a gc request. Also in case of |
| 67 | * failure |
| 68 | */ |
| 69 | static int pblk_gc_move_valid_secs(struct pblk *pblk, struct pblk_line *line, |
| 70 | u64 *lba_list, unsigned int nr_secs) |
| 71 | { |
| 72 | struct nvm_tgt_dev *dev = pblk->dev; |
| 73 | struct nvm_geo *geo = &dev->geo; |
| 74 | struct pblk_gc *gc = &pblk->gc; |
| 75 | struct pblk_gc_rq *gc_rq; |
| 76 | void *data; |
| 77 | unsigned int secs_to_gc; |
| 78 | int ret = NVM_IO_OK; |
| 79 | |
| 80 | data = kmalloc(nr_secs * geo->sec_size, GFP_KERNEL); |
| 81 | if (!data) { |
| 82 | ret = NVM_IO_ERR; |
| 83 | goto free_lba_list; |
| 84 | } |
| 85 | |
| 86 | /* Read from GC victim block */ |
| 87 | if (pblk_submit_read_gc(pblk, lba_list, data, nr_secs, |
| 88 | &secs_to_gc, line)) { |
| 89 | ret = NVM_IO_ERR; |
| 90 | goto free_data; |
| 91 | } |
| 92 | |
| 93 | if (!secs_to_gc) |
| 94 | goto free_data; |
| 95 | |
| 96 | gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL); |
| 97 | if (!gc_rq) { |
| 98 | ret = NVM_IO_ERR; |
| 99 | goto free_data; |
| 100 | } |
| 101 | |
| 102 | gc_rq->line = line; |
| 103 | gc_rq->data = data; |
| 104 | gc_rq->lba_list = lba_list; |
| 105 | gc_rq->nr_secs = nr_secs; |
| 106 | gc_rq->secs_to_gc = secs_to_gc; |
| 107 | |
| 108 | kref_get(&line->ref); |
| 109 | |
| 110 | retry: |
| 111 | spin_lock(&gc->w_lock); |
| 112 | if (gc->w_entries > 256) { |
| 113 | spin_unlock(&gc->w_lock); |
| 114 | usleep_range(256, 1024); |
| 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 | return NVM_IO_OK; |
| 124 | |
| 125 | free_data: |
| 126 | kfree(data); |
| 127 | free_lba_list: |
| 128 | kfree(lba_list); |
| 129 | |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | static void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line) |
| 134 | { |
| 135 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 136 | struct list_head *move_list; |
| 137 | |
| 138 | spin_lock(&line->lock); |
| 139 | WARN_ON(line->state != PBLK_LINESTATE_GC); |
| 140 | line->state = PBLK_LINESTATE_CLOSED; |
| 141 | move_list = pblk_line_gc_list(pblk, line); |
| 142 | spin_unlock(&line->lock); |
| 143 | |
| 144 | if (move_list) { |
| 145 | spin_lock(&l_mg->gc_lock); |
| 146 | list_add_tail(&line->list, move_list); |
| 147 | spin_unlock(&l_mg->gc_lock); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static void pblk_gc_line_ws(struct work_struct *work) |
| 152 | { |
| 153 | struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws, |
| 154 | ws); |
| 155 | struct pblk *pblk = line_ws->pblk; |
| 156 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 157 | struct pblk_line *line = line_ws->line; |
| 158 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 159 | struct line_emeta *emeta_buf = line_ws->priv; |
| 160 | __le64 *lba_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 161 | u64 *gc_list; |
| 162 | int sec_left; |
| 163 | int nr_ppas, bit; |
| 164 | int put_line = 1; |
| 165 | |
| 166 | pr_debug("pblk: line '%d' being reclaimed for GC\n", line->id); |
| 167 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 168 | /* 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 out; |
| 176 | } |
| 177 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 178 | spin_lock(&line->lock); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 179 | sec_left = le32_to_cpu(*line->vsc); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 180 | if (!sec_left) { |
| 181 | /* Lines are erased before being used (l_mg->data_/log_next) */ |
| 182 | spin_unlock(&line->lock); |
| 183 | goto out; |
| 184 | } |
| 185 | spin_unlock(&line->lock); |
| 186 | |
| 187 | if (sec_left < 0) { |
| 188 | pr_err("pblk: corrupted GC line (%d)\n", line->id); |
| 189 | put_line = 0; |
| 190 | pblk_put_line_back(pblk, line); |
| 191 | goto out; |
| 192 | } |
| 193 | |
| 194 | bit = -1; |
| 195 | next_rq: |
| 196 | gc_list = kmalloc_array(pblk->max_write_pgs, sizeof(u64), GFP_KERNEL); |
| 197 | if (!gc_list) { |
| 198 | put_line = 0; |
| 199 | pblk_put_line_back(pblk, line); |
| 200 | goto out; |
| 201 | } |
| 202 | |
| 203 | nr_ppas = 0; |
| 204 | do { |
| 205 | bit = find_next_zero_bit(line->invalid_bitmap, lm->sec_per_line, |
| 206 | bit + 1); |
| 207 | if (bit > line->emeta_ssec) |
| 208 | break; |
| 209 | |
| 210 | gc_list[nr_ppas++] = le64_to_cpu(lba_list[bit]); |
| 211 | } while (nr_ppas < pblk->max_write_pgs); |
| 212 | |
| 213 | if (unlikely(!nr_ppas)) { |
| 214 | kfree(gc_list); |
| 215 | goto out; |
| 216 | } |
| 217 | |
| 218 | if (pblk_gc_move_valid_secs(pblk, line, gc_list, nr_ppas)) { |
| 219 | pr_err("pblk: could not GC all sectors: line:%d (%d/%d/%d)\n", |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 220 | line->id, *line->vsc, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 221 | nr_ppas, nr_ppas); |
| 222 | put_line = 0; |
| 223 | pblk_put_line_back(pblk, line); |
| 224 | goto out; |
| 225 | } |
| 226 | |
| 227 | sec_left -= nr_ppas; |
| 228 | if (sec_left > 0) |
| 229 | goto next_rq; |
| 230 | |
| 231 | out: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 232 | pblk_mfree(emeta_buf, l_mg->emeta_alloc_type); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 233 | mempool_free(line_ws, pblk->line_ws_pool); |
| 234 | atomic_dec(&pblk->gc.inflight_gc); |
| 235 | if (put_line) |
| 236 | kref_put(&line->ref, pblk_line_put); |
| 237 | } |
| 238 | |
| 239 | static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line) |
| 240 | { |
| 241 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 242 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 243 | struct line_emeta *emeta_buf; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 244 | struct pblk_line_ws *line_ws; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 245 | int ret; |
| 246 | |
| 247 | line_ws = mempool_alloc(pblk->line_ws_pool, GFP_KERNEL); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 248 | emeta_buf = pblk_malloc(lm->emeta_len[0], l_mg->emeta_alloc_type, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 249 | GFP_KERNEL); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 250 | if (!emeta_buf) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 251 | pr_err("pblk: cannot use GC emeta\n"); |
| 252 | goto fail_free_ws; |
| 253 | } |
| 254 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 255 | ret = pblk_line_read_emeta(pblk, line, emeta_buf); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 256 | if (ret) { |
| 257 | pr_err("pblk: line %d read emeta failed (%d)\n", line->id, ret); |
| 258 | goto fail_free_emeta; |
| 259 | } |
| 260 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 261 | line_ws->pblk = pblk; |
| 262 | line_ws->line = line; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 263 | line_ws->priv = emeta_buf; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 264 | |
| 265 | INIT_WORK(&line_ws->ws, pblk_gc_line_ws); |
| 266 | queue_work(pblk->gc.gc_reader_wq, &line_ws->ws); |
| 267 | |
| 268 | return 0; |
| 269 | |
| 270 | fail_free_emeta: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 271 | pblk_mfree(emeta_buf, l_mg->emeta_alloc_type); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 272 | fail_free_ws: |
| 273 | mempool_free(line_ws, pblk->line_ws_pool); |
| 274 | pblk_put_line_back(pblk, line); |
| 275 | |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | static void pblk_gc_lines(struct pblk *pblk, struct list_head *gc_list) |
| 280 | { |
| 281 | struct pblk_line *line, *tline; |
| 282 | |
| 283 | list_for_each_entry_safe(line, tline, gc_list, list) { |
| 284 | if (pblk_gc_line(pblk, line)) |
| 285 | pr_err("pblk: failed to GC line %d\n", line->id); |
| 286 | list_del(&line->list); |
| 287 | } |
| 288 | } |
| 289 | |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame^] | 290 | static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk, |
| 291 | struct list_head *group_list) |
| 292 | { |
| 293 | struct pblk_line *line, *victim; |
| 294 | |
| 295 | victim = list_first_entry(group_list, struct pblk_line, list); |
| 296 | list_for_each_entry(line, group_list, list) { |
| 297 | if (*line->vsc < *victim->vsc) |
| 298 | victim = line; |
| 299 | } |
| 300 | |
| 301 | return victim; |
| 302 | } |
| 303 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 304 | /* |
| 305 | * Lines with no valid sectors will be returned to the free list immediately. If |
| 306 | * GC is activated - either because the free block count is under the determined |
| 307 | * threshold, or because it is being forced from user space - only lines with a |
| 308 | * high count of invalid sectors will be recycled. |
| 309 | */ |
| 310 | static void pblk_gc_run(struct pblk *pblk) |
| 311 | { |
| 312 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 313 | struct pblk_gc *gc = &pblk->gc; |
| 314 | struct pblk_line *line, *tline; |
| 315 | unsigned int nr_blocks_free, nr_blocks_need; |
| 316 | struct list_head *group_list; |
| 317 | int run_gc, gc_group = 0; |
| 318 | int prev_gc = 0; |
| 319 | int inflight_gc = atomic_read(&gc->inflight_gc); |
| 320 | LIST_HEAD(gc_list); |
| 321 | |
| 322 | spin_lock(&l_mg->gc_lock); |
| 323 | list_for_each_entry_safe(line, tline, &l_mg->gc_full_list, list) { |
| 324 | spin_lock(&line->lock); |
| 325 | WARN_ON(line->state != PBLK_LINESTATE_CLOSED); |
| 326 | line->state = PBLK_LINESTATE_GC; |
| 327 | spin_unlock(&line->lock); |
| 328 | |
| 329 | list_del(&line->list); |
| 330 | kref_put(&line->ref, pblk_line_put); |
| 331 | } |
| 332 | spin_unlock(&l_mg->gc_lock); |
| 333 | |
| 334 | nr_blocks_need = pblk_rl_gc_thrs(&pblk->rl); |
| 335 | nr_blocks_free = pblk_rl_nr_free_blks(&pblk->rl); |
| 336 | run_gc = (nr_blocks_need > nr_blocks_free || gc->gc_forced); |
| 337 | |
| 338 | next_gc_group: |
| 339 | group_list = l_mg->gc_lists[gc_group++]; |
| 340 | spin_lock(&l_mg->gc_lock); |
| 341 | while (run_gc && !list_empty(group_list)) { |
| 342 | /* No need to queue up more GC lines than we can handle */ |
| 343 | if (!run_gc || inflight_gc > gc->gc_jobs_active) { |
| 344 | spin_unlock(&l_mg->gc_lock); |
| 345 | pblk_gc_lines(pblk, &gc_list); |
| 346 | return; |
| 347 | } |
| 348 | |
Javier González | d45ebd4 | 2017-06-26 11:57:23 +0200 | [diff] [blame^] | 349 | line = pblk_gc_get_victim_line(pblk, group_list); |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 350 | nr_blocks_free += atomic_read(&line->blk_in_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 351 | |
| 352 | spin_lock(&line->lock); |
| 353 | WARN_ON(line->state != PBLK_LINESTATE_CLOSED); |
| 354 | line->state = PBLK_LINESTATE_GC; |
| 355 | list_move_tail(&line->list, &gc_list); |
| 356 | atomic_inc(&gc->inflight_gc); |
| 357 | inflight_gc++; |
| 358 | spin_unlock(&line->lock); |
| 359 | |
| 360 | prev_gc = 1; |
| 361 | run_gc = (nr_blocks_need > nr_blocks_free || gc->gc_forced); |
| 362 | } |
| 363 | spin_unlock(&l_mg->gc_lock); |
| 364 | |
| 365 | pblk_gc_lines(pblk, &gc_list); |
| 366 | |
| 367 | if (!prev_gc && pblk->rl.rb_state > gc_group && |
| 368 | gc_group < PBLK_NR_GC_LISTS) |
| 369 | goto next_gc_group; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | static void pblk_gc_kick(struct pblk *pblk) |
| 374 | { |
| 375 | struct pblk_gc *gc = &pblk->gc; |
| 376 | |
| 377 | wake_up_process(gc->gc_ts); |
| 378 | pblk_gc_writer_kick(gc); |
| 379 | mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS)); |
| 380 | } |
| 381 | |
| 382 | static void pblk_gc_timer(unsigned long data) |
| 383 | { |
| 384 | struct pblk *pblk = (struct pblk *)data; |
| 385 | |
| 386 | pblk_gc_kick(pblk); |
| 387 | } |
| 388 | |
| 389 | static int pblk_gc_ts(void *data) |
| 390 | { |
| 391 | struct pblk *pblk = data; |
| 392 | |
| 393 | while (!kthread_should_stop()) { |
| 394 | pblk_gc_run(pblk); |
| 395 | set_current_state(TASK_INTERRUPTIBLE); |
| 396 | io_schedule(); |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int pblk_gc_writer_ts(void *data) |
| 403 | { |
| 404 | struct pblk *pblk = data; |
| 405 | |
| 406 | while (!kthread_should_stop()) { |
| 407 | if (!pblk_gc_write(pblk)) |
| 408 | continue; |
| 409 | set_current_state(TASK_INTERRUPTIBLE); |
| 410 | io_schedule(); |
| 411 | } |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static void pblk_gc_start(struct pblk *pblk) |
| 417 | { |
| 418 | pblk->gc.gc_active = 1; |
| 419 | |
| 420 | pr_debug("pblk: gc start\n"); |
| 421 | } |
| 422 | |
| 423 | int pblk_gc_status(struct pblk *pblk) |
| 424 | { |
| 425 | struct pblk_gc *gc = &pblk->gc; |
| 426 | int ret; |
| 427 | |
| 428 | spin_lock(&gc->lock); |
| 429 | ret = gc->gc_active; |
| 430 | spin_unlock(&gc->lock); |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static void __pblk_gc_should_start(struct pblk *pblk) |
| 436 | { |
| 437 | struct pblk_gc *gc = &pblk->gc; |
| 438 | |
| 439 | lockdep_assert_held(&gc->lock); |
| 440 | |
| 441 | if (gc->gc_enabled && !gc->gc_active) |
| 442 | pblk_gc_start(pblk); |
| 443 | } |
| 444 | |
| 445 | void pblk_gc_should_start(struct pblk *pblk) |
| 446 | { |
| 447 | struct pblk_gc *gc = &pblk->gc; |
| 448 | |
| 449 | spin_lock(&gc->lock); |
| 450 | __pblk_gc_should_start(pblk); |
| 451 | spin_unlock(&gc->lock); |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * If flush_wq == 1 then no lock should be held by the caller since |
| 456 | * flush_workqueue can sleep |
| 457 | */ |
| 458 | static void pblk_gc_stop(struct pblk *pblk, int flush_wq) |
| 459 | { |
| 460 | spin_lock(&pblk->gc.lock); |
| 461 | pblk->gc.gc_active = 0; |
| 462 | spin_unlock(&pblk->gc.lock); |
| 463 | |
| 464 | pr_debug("pblk: gc stop\n"); |
| 465 | } |
| 466 | |
| 467 | void pblk_gc_should_stop(struct pblk *pblk) |
| 468 | { |
| 469 | struct pblk_gc *gc = &pblk->gc; |
| 470 | |
| 471 | if (gc->gc_active && !gc->gc_forced) |
| 472 | pblk_gc_stop(pblk, 0); |
| 473 | } |
| 474 | |
| 475 | void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled, |
| 476 | int *gc_active) |
| 477 | { |
| 478 | struct pblk_gc *gc = &pblk->gc; |
| 479 | |
| 480 | spin_lock(&gc->lock); |
| 481 | *gc_enabled = gc->gc_enabled; |
| 482 | *gc_active = gc->gc_active; |
| 483 | spin_unlock(&gc->lock); |
| 484 | } |
| 485 | |
| 486 | void pblk_gc_sysfs_force(struct pblk *pblk, int force) |
| 487 | { |
| 488 | struct pblk_gc *gc = &pblk->gc; |
| 489 | int rsv = 0; |
| 490 | |
| 491 | spin_lock(&gc->lock); |
| 492 | if (force) { |
| 493 | gc->gc_enabled = 1; |
| 494 | rsv = 64; |
| 495 | } |
| 496 | pblk_rl_set_gc_rsc(&pblk->rl, rsv); |
| 497 | gc->gc_forced = force; |
| 498 | __pblk_gc_should_start(pblk); |
| 499 | spin_unlock(&gc->lock); |
| 500 | } |
| 501 | |
| 502 | int pblk_gc_init(struct pblk *pblk) |
| 503 | { |
| 504 | struct pblk_gc *gc = &pblk->gc; |
| 505 | int ret; |
| 506 | |
| 507 | gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts"); |
| 508 | if (IS_ERR(gc->gc_ts)) { |
| 509 | pr_err("pblk: could not allocate GC main kthread\n"); |
| 510 | return PTR_ERR(gc->gc_ts); |
| 511 | } |
| 512 | |
| 513 | gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk, |
| 514 | "pblk-gc-writer-ts"); |
| 515 | if (IS_ERR(gc->gc_writer_ts)) { |
| 516 | pr_err("pblk: could not allocate GC writer kthread\n"); |
| 517 | ret = PTR_ERR(gc->gc_writer_ts); |
| 518 | goto fail_free_main_kthread; |
| 519 | } |
| 520 | |
| 521 | setup_timer(&gc->gc_timer, pblk_gc_timer, (unsigned long)pblk); |
| 522 | mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS)); |
| 523 | |
| 524 | gc->gc_active = 0; |
| 525 | gc->gc_forced = 0; |
| 526 | gc->gc_enabled = 1; |
| 527 | gc->gc_jobs_active = 8; |
| 528 | gc->w_entries = 0; |
| 529 | atomic_set(&gc->inflight_gc, 0); |
| 530 | |
| 531 | gc->gc_reader_wq = alloc_workqueue("pblk-gc-reader-wq", |
| 532 | WQ_MEM_RECLAIM | WQ_UNBOUND, gc->gc_jobs_active); |
| 533 | if (!gc->gc_reader_wq) { |
| 534 | pr_err("pblk: could not allocate GC reader workqueue\n"); |
| 535 | ret = -ENOMEM; |
| 536 | goto fail_free_writer_kthread; |
| 537 | } |
| 538 | |
| 539 | spin_lock_init(&gc->lock); |
| 540 | spin_lock_init(&gc->w_lock); |
| 541 | INIT_LIST_HEAD(&gc->w_list); |
| 542 | |
| 543 | return 0; |
| 544 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 545 | fail_free_writer_kthread: |
| 546 | kthread_stop(gc->gc_writer_ts); |
Dan Carpenter | 503ec94 | 2017-04-15 20:55:51 +0200 | [diff] [blame] | 547 | fail_free_main_kthread: |
| 548 | kthread_stop(gc->gc_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 549 | |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | void pblk_gc_exit(struct pblk *pblk) |
| 554 | { |
| 555 | struct pblk_gc *gc = &pblk->gc; |
| 556 | |
| 557 | flush_workqueue(gc->gc_reader_wq); |
| 558 | |
| 559 | del_timer(&gc->gc_timer); |
| 560 | pblk_gc_stop(pblk, 1); |
| 561 | |
| 562 | if (gc->gc_ts) |
| 563 | kthread_stop(gc->gc_ts); |
| 564 | |
| 565 | if (pblk->gc.gc_reader_wq) |
| 566 | destroy_workqueue(pblk->gc.gc_reader_wq); |
| 567 | |
| 568 | if (gc->gc_writer_ts) |
| 569 | kthread_stop(gc->gc_writer_ts); |
| 570 | } |