pblk-gc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 CNEX Labs
  4. * Initial release: Javier Gonzalez <javier@cnexlabs.com>
  5. * Matias Bjorling <matias@cnexlabs.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * pblk-gc.c - pblk's garbage collector
  17. */
  18. #include "pblk.h"
  19. #include "pblk-trace.h"
  20. #include <linux/delay.h>
  21. static void pblk_gc_free_gc_rq(struct pblk_gc_rq *gc_rq)
  22. {
  23. if (gc_rq->data)
  24. vfree(gc_rq->data);
  25. kfree(gc_rq);
  26. }
  27. static int pblk_gc_write(struct pblk *pblk)
  28. {
  29. struct pblk_gc *gc = &pblk->gc;
  30. struct pblk_gc_rq *gc_rq, *tgc_rq;
  31. LIST_HEAD(w_list);
  32. spin_lock(&gc->w_lock);
  33. if (list_empty(&gc->w_list)) {
  34. spin_unlock(&gc->w_lock);
  35. return 1;
  36. }
  37. list_cut_position(&w_list, &gc->w_list, gc->w_list.prev);
  38. gc->w_entries = 0;
  39. spin_unlock(&gc->w_lock);
  40. list_for_each_entry_safe(gc_rq, tgc_rq, &w_list, list) {
  41. pblk_write_gc_to_cache(pblk, gc_rq);
  42. list_del(&gc_rq->list);
  43. kref_put(&gc_rq->line->ref, pblk_line_put);
  44. pblk_gc_free_gc_rq(gc_rq);
  45. }
  46. return 0;
  47. }
  48. static void pblk_gc_writer_kick(struct pblk_gc *gc)
  49. {
  50. wake_up_process(gc->gc_writer_ts);
  51. }
  52. static void pblk_put_line_back(struct pblk *pblk, struct pblk_line *line)
  53. {
  54. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  55. struct list_head *move_list;
  56. spin_lock(&line->lock);
  57. WARN_ON(line->state != PBLK_LINESTATE_GC);
  58. line->state = PBLK_LINESTATE_CLOSED;
  59. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  60. line->state);
  61. move_list = pblk_line_gc_list(pblk, line);
  62. spin_unlock(&line->lock);
  63. if (move_list) {
  64. spin_lock(&l_mg->gc_lock);
  65. list_add_tail(&line->list, move_list);
  66. spin_unlock(&l_mg->gc_lock);
  67. }
  68. }
  69. static void pblk_gc_line_ws(struct work_struct *work)
  70. {
  71. struct pblk_line_ws *gc_rq_ws = container_of(work,
  72. struct pblk_line_ws, ws);
  73. struct pblk *pblk = gc_rq_ws->pblk;
  74. struct nvm_tgt_dev *dev = pblk->dev;
  75. struct nvm_geo *geo = &dev->geo;
  76. struct pblk_gc *gc = &pblk->gc;
  77. struct pblk_line *line = gc_rq_ws->line;
  78. struct pblk_gc_rq *gc_rq = gc_rq_ws->priv;
  79. int ret;
  80. up(&gc->gc_sem);
  81. gc_rq->data = vmalloc(array_size(gc_rq->nr_secs, geo->csecs));
  82. if (!gc_rq->data) {
  83. pblk_err(pblk, "could not GC line:%d (%d/%d)\n",
  84. line->id, *line->vsc, gc_rq->nr_secs);
  85. goto out;
  86. }
  87. /* Read from GC victim block */
  88. ret = pblk_submit_read_gc(pblk, gc_rq);
  89. if (ret) {
  90. pblk_err(pblk, "failed GC read in line:%d (err:%d)\n",
  91. line->id, ret);
  92. goto out;
  93. }
  94. if (!gc_rq->secs_to_gc)
  95. goto out;
  96. retry:
  97. spin_lock(&gc->w_lock);
  98. if (gc->w_entries >= PBLK_GC_RQ_QD) {
  99. spin_unlock(&gc->w_lock);
  100. pblk_gc_writer_kick(&pblk->gc);
  101. usleep_range(128, 256);
  102. goto retry;
  103. }
  104. gc->w_entries++;
  105. list_add_tail(&gc_rq->list, &gc->w_list);
  106. spin_unlock(&gc->w_lock);
  107. pblk_gc_writer_kick(&pblk->gc);
  108. kfree(gc_rq_ws);
  109. return;
  110. out:
  111. pblk_gc_free_gc_rq(gc_rq);
  112. kref_put(&line->ref, pblk_line_put);
  113. kfree(gc_rq_ws);
  114. }
  115. static __le64 *get_lba_list_from_emeta(struct pblk *pblk,
  116. struct pblk_line *line)
  117. {
  118. struct line_emeta *emeta_buf;
  119. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  120. struct pblk_line_meta *lm = &pblk->lm;
  121. unsigned int lba_list_size = lm->emeta_len[2];
  122. __le64 *lba_list;
  123. int ret;
  124. emeta_buf = pblk_malloc(lm->emeta_len[0],
  125. l_mg->emeta_alloc_type, GFP_KERNEL);
  126. if (!emeta_buf)
  127. return NULL;
  128. ret = pblk_line_emeta_read(pblk, line, emeta_buf);
  129. if (ret) {
  130. pblk_err(pblk, "line %d read emeta failed (%d)\n",
  131. line->id, ret);
  132. pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
  133. return NULL;
  134. }
  135. /* If this read fails, it means that emeta is corrupted.
  136. * For now, leave the line untouched.
  137. * TODO: Implement a recovery routine that scans and moves
  138. * all sectors on the line.
  139. */
  140. ret = pblk_recov_check_emeta(pblk, emeta_buf);
  141. if (ret) {
  142. pblk_err(pblk, "inconsistent emeta (line %d)\n",
  143. line->id);
  144. pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
  145. return NULL;
  146. }
  147. lba_list = pblk_malloc(lba_list_size,
  148. l_mg->emeta_alloc_type, GFP_KERNEL);
  149. if (lba_list)
  150. memcpy(lba_list, emeta_to_lbas(pblk, emeta_buf), lba_list_size);
  151. pblk_mfree(emeta_buf, l_mg->emeta_alloc_type);
  152. return lba_list;
  153. }
  154. static void pblk_gc_line_prepare_ws(struct work_struct *work)
  155. {
  156. struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
  157. ws);
  158. struct pblk *pblk = line_ws->pblk;
  159. struct pblk_line *line = line_ws->line;
  160. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  161. struct pblk_line_meta *lm = &pblk->lm;
  162. struct pblk_gc *gc = &pblk->gc;
  163. struct pblk_line_ws *gc_rq_ws;
  164. struct pblk_gc_rq *gc_rq;
  165. __le64 *lba_list;
  166. unsigned long *invalid_bitmap;
  167. int sec_left, nr_secs, bit;
  168. invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_KERNEL);
  169. if (!invalid_bitmap)
  170. goto fail_free_ws;
  171. if (line->w_err_gc->has_write_err) {
  172. lba_list = line->w_err_gc->lba_list;
  173. line->w_err_gc->lba_list = NULL;
  174. } else {
  175. lba_list = get_lba_list_from_emeta(pblk, line);
  176. if (!lba_list) {
  177. pblk_err(pblk, "could not interpret emeta (line %d)\n",
  178. line->id);
  179. goto fail_free_invalid_bitmap;
  180. }
  181. }
  182. spin_lock(&line->lock);
  183. bitmap_copy(invalid_bitmap, line->invalid_bitmap, lm->sec_per_line);
  184. sec_left = pblk_line_vsc(line);
  185. spin_unlock(&line->lock);
  186. if (sec_left < 0) {
  187. pblk_err(pblk, "corrupted GC line (%d)\n", line->id);
  188. goto fail_free_lba_list;
  189. }
  190. bit = -1;
  191. next_rq:
  192. gc_rq = kmalloc(sizeof(struct pblk_gc_rq), GFP_KERNEL);
  193. if (!gc_rq)
  194. goto fail_free_lba_list;
  195. nr_secs = 0;
  196. do {
  197. bit = find_next_zero_bit(invalid_bitmap, lm->sec_per_line,
  198. bit + 1);
  199. if (bit > line->emeta_ssec)
  200. break;
  201. gc_rq->paddr_list[nr_secs] = bit;
  202. gc_rq->lba_list[nr_secs++] = le64_to_cpu(lba_list[bit]);
  203. } while (nr_secs < pblk->max_write_pgs);
  204. if (unlikely(!nr_secs)) {
  205. kfree(gc_rq);
  206. goto out;
  207. }
  208. gc_rq->nr_secs = nr_secs;
  209. gc_rq->line = line;
  210. gc_rq_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
  211. if (!gc_rq_ws)
  212. goto fail_free_gc_rq;
  213. gc_rq_ws->pblk = pblk;
  214. gc_rq_ws->line = line;
  215. gc_rq_ws->priv = gc_rq;
  216. /* The write GC path can be much slower than the read GC one due to
  217. * the budget imposed by the rate-limiter. Balance in case that we get
  218. * back pressure from the write GC path.
  219. */
  220. while (down_timeout(&gc->gc_sem, msecs_to_jiffies(30000)))
  221. io_schedule();
  222. kref_get(&line->ref);
  223. INIT_WORK(&gc_rq_ws->ws, pblk_gc_line_ws);
  224. queue_work(gc->gc_line_reader_wq, &gc_rq_ws->ws);
  225. sec_left -= nr_secs;
  226. if (sec_left > 0)
  227. goto next_rq;
  228. out:
  229. pblk_mfree(lba_list, l_mg->emeta_alloc_type);
  230. kfree(line_ws);
  231. kfree(invalid_bitmap);
  232. kref_put(&line->ref, pblk_line_put);
  233. atomic_dec(&gc->read_inflight_gc);
  234. return;
  235. fail_free_gc_rq:
  236. kfree(gc_rq);
  237. fail_free_lba_list:
  238. pblk_mfree(lba_list, l_mg->emeta_alloc_type);
  239. fail_free_invalid_bitmap:
  240. kfree(invalid_bitmap);
  241. fail_free_ws:
  242. kfree(line_ws);
  243. pblk_put_line_back(pblk, line);
  244. kref_put(&line->ref, pblk_line_put);
  245. atomic_dec(&gc->read_inflight_gc);
  246. pblk_err(pblk, "failed to GC line %d\n", line->id);
  247. }
  248. static int pblk_gc_line(struct pblk *pblk, struct pblk_line *line)
  249. {
  250. struct pblk_gc *gc = &pblk->gc;
  251. struct pblk_line_ws *line_ws;
  252. pblk_debug(pblk, "line '%d' being reclaimed for GC\n", line->id);
  253. line_ws = kmalloc(sizeof(struct pblk_line_ws), GFP_KERNEL);
  254. if (!line_ws)
  255. return -ENOMEM;
  256. line_ws->pblk = pblk;
  257. line_ws->line = line;
  258. atomic_inc(&gc->pipeline_gc);
  259. INIT_WORK(&line_ws->ws, pblk_gc_line_prepare_ws);
  260. queue_work(gc->gc_reader_wq, &line_ws->ws);
  261. return 0;
  262. }
  263. static void pblk_gc_reader_kick(struct pblk_gc *gc)
  264. {
  265. wake_up_process(gc->gc_reader_ts);
  266. }
  267. static void pblk_gc_kick(struct pblk *pblk)
  268. {
  269. struct pblk_gc *gc = &pblk->gc;
  270. pblk_gc_writer_kick(gc);
  271. pblk_gc_reader_kick(gc);
  272. /* If we're shutting down GC, let's not start it up again */
  273. if (gc->gc_enabled) {
  274. wake_up_process(gc->gc_ts);
  275. mod_timer(&gc->gc_timer,
  276. jiffies + msecs_to_jiffies(GC_TIME_MSECS));
  277. }
  278. }
  279. static int pblk_gc_read(struct pblk *pblk)
  280. {
  281. struct pblk_gc *gc = &pblk->gc;
  282. struct pblk_line *line;
  283. spin_lock(&gc->r_lock);
  284. if (list_empty(&gc->r_list)) {
  285. spin_unlock(&gc->r_lock);
  286. return 1;
  287. }
  288. line = list_first_entry(&gc->r_list, struct pblk_line, list);
  289. list_del(&line->list);
  290. spin_unlock(&gc->r_lock);
  291. pblk_gc_kick(pblk);
  292. if (pblk_gc_line(pblk, line))
  293. pblk_err(pblk, "failed to GC line %d\n", line->id);
  294. return 0;
  295. }
  296. static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk,
  297. struct list_head *group_list)
  298. {
  299. struct pblk_line *line, *victim;
  300. int line_vsc, victim_vsc;
  301. victim = list_first_entry(group_list, struct pblk_line, list);
  302. list_for_each_entry(line, group_list, list) {
  303. line_vsc = le32_to_cpu(*line->vsc);
  304. victim_vsc = le32_to_cpu(*victim->vsc);
  305. if (line_vsc < victim_vsc)
  306. victim = line;
  307. }
  308. return victim;
  309. }
  310. static bool pblk_gc_should_run(struct pblk_gc *gc, struct pblk_rl *rl)
  311. {
  312. unsigned int nr_blocks_free, nr_blocks_need;
  313. unsigned int werr_lines = atomic_read(&rl->werr_lines);
  314. nr_blocks_need = pblk_rl_high_thrs(rl);
  315. nr_blocks_free = pblk_rl_nr_free_blks(rl);
  316. /* This is not critical, no need to take lock here */
  317. return ((werr_lines > 0) ||
  318. ((gc->gc_active) && (nr_blocks_need > nr_blocks_free)));
  319. }
  320. void pblk_gc_free_full_lines(struct pblk *pblk)
  321. {
  322. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  323. struct pblk_gc *gc = &pblk->gc;
  324. struct pblk_line *line;
  325. do {
  326. spin_lock(&l_mg->gc_lock);
  327. if (list_empty(&l_mg->gc_full_list)) {
  328. spin_unlock(&l_mg->gc_lock);
  329. return;
  330. }
  331. line = list_first_entry(&l_mg->gc_full_list,
  332. struct pblk_line, list);
  333. spin_lock(&line->lock);
  334. WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
  335. line->state = PBLK_LINESTATE_GC;
  336. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  337. line->state);
  338. spin_unlock(&line->lock);
  339. list_del(&line->list);
  340. spin_unlock(&l_mg->gc_lock);
  341. atomic_inc(&gc->pipeline_gc);
  342. kref_put(&line->ref, pblk_line_put);
  343. } while (1);
  344. }
  345. /*
  346. * Lines with no valid sectors will be returned to the free list immediately. If
  347. * GC is activated - either because the free block count is under the determined
  348. * threshold, or because it is being forced from user space - only lines with a
  349. * high count of invalid sectors will be recycled.
  350. */
  351. static void pblk_gc_run(struct pblk *pblk)
  352. {
  353. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  354. struct pblk_gc *gc = &pblk->gc;
  355. struct pblk_line *line;
  356. struct list_head *group_list;
  357. bool run_gc;
  358. int read_inflight_gc, gc_group = 0, prev_group = 0;
  359. pblk_gc_free_full_lines(pblk);
  360. run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
  361. if (!run_gc || (atomic_read(&gc->read_inflight_gc) >= PBLK_GC_L_QD))
  362. return;
  363. next_gc_group:
  364. group_list = l_mg->gc_lists[gc_group++];
  365. do {
  366. spin_lock(&l_mg->gc_lock);
  367. if (list_empty(group_list)) {
  368. spin_unlock(&l_mg->gc_lock);
  369. break;
  370. }
  371. line = pblk_gc_get_victim_line(pblk, group_list);
  372. spin_lock(&line->lock);
  373. WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
  374. line->state = PBLK_LINESTATE_GC;
  375. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  376. line->state);
  377. spin_unlock(&line->lock);
  378. list_del(&line->list);
  379. spin_unlock(&l_mg->gc_lock);
  380. spin_lock(&gc->r_lock);
  381. list_add_tail(&line->list, &gc->r_list);
  382. spin_unlock(&gc->r_lock);
  383. read_inflight_gc = atomic_inc_return(&gc->read_inflight_gc);
  384. pblk_gc_reader_kick(gc);
  385. prev_group = 1;
  386. /* No need to queue up more GC lines than we can handle */
  387. run_gc = pblk_gc_should_run(&pblk->gc, &pblk->rl);
  388. if (!run_gc || read_inflight_gc >= PBLK_GC_L_QD)
  389. break;
  390. } while (1);
  391. if (!prev_group && pblk->rl.rb_state > gc_group &&
  392. gc_group < PBLK_GC_NR_LISTS)
  393. goto next_gc_group;
  394. }
  395. static void pblk_gc_timer(struct timer_list *t)
  396. {
  397. struct pblk *pblk = from_timer(pblk, t, gc.gc_timer);
  398. pblk_gc_kick(pblk);
  399. }
  400. static int pblk_gc_ts(void *data)
  401. {
  402. struct pblk *pblk = data;
  403. while (!kthread_should_stop()) {
  404. pblk_gc_run(pblk);
  405. set_current_state(TASK_INTERRUPTIBLE);
  406. io_schedule();
  407. }
  408. return 0;
  409. }
  410. static int pblk_gc_writer_ts(void *data)
  411. {
  412. struct pblk *pblk = data;
  413. while (!kthread_should_stop()) {
  414. if (!pblk_gc_write(pblk))
  415. continue;
  416. set_current_state(TASK_INTERRUPTIBLE);
  417. io_schedule();
  418. }
  419. return 0;
  420. }
  421. static int pblk_gc_reader_ts(void *data)
  422. {
  423. struct pblk *pblk = data;
  424. struct pblk_gc *gc = &pblk->gc;
  425. while (!kthread_should_stop()) {
  426. if (!pblk_gc_read(pblk))
  427. continue;
  428. set_current_state(TASK_INTERRUPTIBLE);
  429. io_schedule();
  430. }
  431. #ifdef CONFIG_NVM_PBLK_DEBUG
  432. pblk_info(pblk, "flushing gc pipeline, %d lines left\n",
  433. atomic_read(&gc->pipeline_gc));
  434. #endif
  435. do {
  436. if (!atomic_read(&gc->pipeline_gc))
  437. break;
  438. schedule();
  439. } while (1);
  440. return 0;
  441. }
  442. static void pblk_gc_start(struct pblk *pblk)
  443. {
  444. pblk->gc.gc_active = 1;
  445. pblk_debug(pblk, "gc start\n");
  446. }
  447. void pblk_gc_should_start(struct pblk *pblk)
  448. {
  449. struct pblk_gc *gc = &pblk->gc;
  450. if (gc->gc_enabled && !gc->gc_active) {
  451. pblk_gc_start(pblk);
  452. pblk_gc_kick(pblk);
  453. }
  454. }
  455. void pblk_gc_should_stop(struct pblk *pblk)
  456. {
  457. struct pblk_gc *gc = &pblk->gc;
  458. if (gc->gc_active && !gc->gc_forced)
  459. gc->gc_active = 0;
  460. }
  461. void pblk_gc_should_kick(struct pblk *pblk)
  462. {
  463. pblk_rl_update_rates(&pblk->rl);
  464. }
  465. void pblk_gc_sysfs_state_show(struct pblk *pblk, int *gc_enabled,
  466. int *gc_active)
  467. {
  468. struct pblk_gc *gc = &pblk->gc;
  469. spin_lock(&gc->lock);
  470. *gc_enabled = gc->gc_enabled;
  471. *gc_active = gc->gc_active;
  472. spin_unlock(&gc->lock);
  473. }
  474. int pblk_gc_sysfs_force(struct pblk *pblk, int force)
  475. {
  476. struct pblk_gc *gc = &pblk->gc;
  477. if (force < 0 || force > 1)
  478. return -EINVAL;
  479. spin_lock(&gc->lock);
  480. gc->gc_forced = force;
  481. if (force)
  482. gc->gc_enabled = 1;
  483. else
  484. gc->gc_enabled = 0;
  485. spin_unlock(&gc->lock);
  486. pblk_gc_should_start(pblk);
  487. return 0;
  488. }
  489. int pblk_gc_init(struct pblk *pblk)
  490. {
  491. struct pblk_gc *gc = &pblk->gc;
  492. int ret;
  493. gc->gc_ts = kthread_create(pblk_gc_ts, pblk, "pblk-gc-ts");
  494. if (IS_ERR(gc->gc_ts)) {
  495. pblk_err(pblk, "could not allocate GC main kthread\n");
  496. return PTR_ERR(gc->gc_ts);
  497. }
  498. gc->gc_writer_ts = kthread_create(pblk_gc_writer_ts, pblk,
  499. "pblk-gc-writer-ts");
  500. if (IS_ERR(gc->gc_writer_ts)) {
  501. pblk_err(pblk, "could not allocate GC writer kthread\n");
  502. ret = PTR_ERR(gc->gc_writer_ts);
  503. goto fail_free_main_kthread;
  504. }
  505. gc->gc_reader_ts = kthread_create(pblk_gc_reader_ts, pblk,
  506. "pblk-gc-reader-ts");
  507. if (IS_ERR(gc->gc_reader_ts)) {
  508. pblk_err(pblk, "could not allocate GC reader kthread\n");
  509. ret = PTR_ERR(gc->gc_reader_ts);
  510. goto fail_free_writer_kthread;
  511. }
  512. timer_setup(&gc->gc_timer, pblk_gc_timer, 0);
  513. mod_timer(&gc->gc_timer, jiffies + msecs_to_jiffies(GC_TIME_MSECS));
  514. gc->gc_active = 0;
  515. gc->gc_forced = 0;
  516. gc->gc_enabled = 1;
  517. gc->w_entries = 0;
  518. atomic_set(&gc->read_inflight_gc, 0);
  519. atomic_set(&gc->pipeline_gc, 0);
  520. /* Workqueue that reads valid sectors from a line and submit them to the
  521. * GC writer to be recycled.
  522. */
  523. gc->gc_line_reader_wq = alloc_workqueue("pblk-gc-line-reader-wq",
  524. WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_GC_MAX_READERS);
  525. if (!gc->gc_line_reader_wq) {
  526. pblk_err(pblk, "could not allocate GC line reader workqueue\n");
  527. ret = -ENOMEM;
  528. goto fail_free_reader_kthread;
  529. }
  530. /* Workqueue that prepare lines for GC */
  531. gc->gc_reader_wq = alloc_workqueue("pblk-gc-line_wq",
  532. WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  533. if (!gc->gc_reader_wq) {
  534. pblk_err(pblk, "could not allocate GC reader workqueue\n");
  535. ret = -ENOMEM;
  536. goto fail_free_reader_line_wq;
  537. }
  538. spin_lock_init(&gc->lock);
  539. spin_lock_init(&gc->w_lock);
  540. spin_lock_init(&gc->r_lock);
  541. sema_init(&gc->gc_sem, PBLK_GC_RQ_QD);
  542. INIT_LIST_HEAD(&gc->w_list);
  543. INIT_LIST_HEAD(&gc->r_list);
  544. return 0;
  545. fail_free_reader_line_wq:
  546. destroy_workqueue(gc->gc_line_reader_wq);
  547. fail_free_reader_kthread:
  548. kthread_stop(gc->gc_reader_ts);
  549. fail_free_writer_kthread:
  550. kthread_stop(gc->gc_writer_ts);
  551. fail_free_main_kthread:
  552. kthread_stop(gc->gc_ts);
  553. return ret;
  554. }
  555. void pblk_gc_exit(struct pblk *pblk, bool graceful)
  556. {
  557. struct pblk_gc *gc = &pblk->gc;
  558. gc->gc_enabled = 0;
  559. del_timer_sync(&gc->gc_timer);
  560. gc->gc_active = 0;
  561. if (gc->gc_ts)
  562. kthread_stop(gc->gc_ts);
  563. if (gc->gc_reader_ts)
  564. kthread_stop(gc->gc_reader_ts);
  565. if (graceful) {
  566. flush_workqueue(gc->gc_reader_wq);
  567. flush_workqueue(gc->gc_line_reader_wq);
  568. }
  569. destroy_workqueue(gc->gc_reader_wq);
  570. destroy_workqueue(gc->gc_line_reader_wq);
  571. if (gc->gc_writer_ts)
  572. kthread_stop(gc->gc_writer_ts);
  573. }