pblk-gc.c 16 KB

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