fastmap-wl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012 Linutronix GmbH
  4. * Copyright (c) 2014 sigma star gmbh
  5. * Author: Richard Weinberger <[email protected]>
  6. */
  7. /**
  8. * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
  9. * @wrk: the work description object
  10. */
  11. static void update_fastmap_work_fn(struct work_struct *wrk)
  12. {
  13. struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
  14. ubi_update_fastmap(ubi);
  15. spin_lock(&ubi->wl_lock);
  16. ubi->fm_work_scheduled = 0;
  17. spin_unlock(&ubi->wl_lock);
  18. }
  19. /**
  20. * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
  21. * @root: the RB-tree where to look for
  22. */
  23. static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
  24. {
  25. struct rb_node *p;
  26. struct ubi_wl_entry *e, *victim = NULL;
  27. int max_ec = UBI_MAX_ERASECOUNTER;
  28. ubi_rb_for_each_entry(p, e, root, u.rb) {
  29. if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
  30. victim = e;
  31. max_ec = e->ec;
  32. }
  33. }
  34. return victim;
  35. }
  36. static inline void return_unused_peb(struct ubi_device *ubi,
  37. struct ubi_wl_entry *e)
  38. {
  39. wl_tree_add(e, &ubi->free);
  40. ubi->free_count++;
  41. }
  42. /**
  43. * return_unused_pool_pebs - returns unused PEB to the free tree.
  44. * @ubi: UBI device description object
  45. * @pool: fastmap pool description object
  46. */
  47. static void return_unused_pool_pebs(struct ubi_device *ubi,
  48. struct ubi_fm_pool *pool)
  49. {
  50. int i;
  51. struct ubi_wl_entry *e;
  52. for (i = pool->used; i < pool->size; i++) {
  53. e = ubi->lookuptbl[pool->pebs[i]];
  54. return_unused_peb(ubi, e);
  55. }
  56. }
  57. /**
  58. * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
  59. * @ubi: UBI device description object
  60. * @anchor: This PEB will be used as anchor PEB by fastmap
  61. *
  62. * The function returns a physical erase block with a given maximal number
  63. * and removes it from the wl subsystem.
  64. * Must be called with wl_lock held!
  65. */
  66. struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
  67. {
  68. struct ubi_wl_entry *e = NULL;
  69. if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
  70. goto out;
  71. if (anchor)
  72. e = find_anchor_wl_entry(&ubi->free);
  73. else
  74. e = find_mean_wl_entry(ubi, &ubi->free);
  75. if (!e)
  76. goto out;
  77. self_check_in_wl_tree(ubi, e, &ubi->free);
  78. /* remove it from the free list,
  79. * the wl subsystem does no longer know this erase block */
  80. rb_erase(&e->u.rb, &ubi->free);
  81. ubi->free_count--;
  82. out:
  83. return e;
  84. }
  85. /*
  86. * has_enough_free_count - whether ubi has enough free pebs to fill fm pools
  87. * @ubi: UBI device description object
  88. * @is_wl_pool: whether UBI is filling wear leveling pool
  89. *
  90. * This helper function checks whether there are enough free pebs (deducted
  91. * by fastmap pebs) to fill fm_pool and fm_wl_pool, above rule works after
  92. * there is at least one of free pebs is filled into fm_wl_pool.
  93. * For wear leveling pool, UBI should also reserve free pebs for bad pebs
  94. * handling, because there maybe no enough free pebs for user volumes after
  95. * producing new bad pebs.
  96. */
  97. static bool has_enough_free_count(struct ubi_device *ubi, bool is_wl_pool)
  98. {
  99. int fm_used = 0; // fastmap non anchor pebs.
  100. int beb_rsvd_pebs;
  101. if (!ubi->free.rb_node)
  102. return false;
  103. beb_rsvd_pebs = is_wl_pool ? ubi->beb_rsvd_pebs : 0;
  104. if (ubi->fm_wl_pool.size > 0 && !(ubi->ro_mode || ubi->fm_disabled))
  105. fm_used = ubi->fm_size / ubi->leb_size - 1;
  106. return ubi->free_count - beb_rsvd_pebs > fm_used;
  107. }
  108. /**
  109. * ubi_refill_pools - refills all fastmap PEB pools.
  110. * @ubi: UBI device description object
  111. */
  112. void ubi_refill_pools(struct ubi_device *ubi)
  113. {
  114. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  115. struct ubi_fm_pool *pool = &ubi->fm_pool;
  116. struct ubi_wl_entry *e;
  117. int enough;
  118. spin_lock(&ubi->wl_lock);
  119. return_unused_pool_pebs(ubi, wl_pool);
  120. return_unused_pool_pebs(ubi, pool);
  121. wl_pool->size = 0;
  122. pool->size = 0;
  123. if (ubi->fm_anchor) {
  124. wl_tree_add(ubi->fm_anchor, &ubi->free);
  125. ubi->free_count++;
  126. ubi->fm_anchor = NULL;
  127. }
  128. if (!ubi->fm_disabled)
  129. /*
  130. * All available PEBs are in ubi->free, now is the time to get
  131. * the best anchor PEBs.
  132. */
  133. ubi->fm_anchor = ubi_wl_get_fm_peb(ubi, 1);
  134. for (;;) {
  135. enough = 0;
  136. if (pool->size < pool->max_size) {
  137. if (!has_enough_free_count(ubi, false))
  138. break;
  139. e = wl_get_wle(ubi);
  140. if (!e)
  141. break;
  142. pool->pebs[pool->size] = e->pnum;
  143. pool->size++;
  144. } else
  145. enough++;
  146. if (wl_pool->size < wl_pool->max_size) {
  147. if (!has_enough_free_count(ubi, true))
  148. break;
  149. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  150. self_check_in_wl_tree(ubi, e, &ubi->free);
  151. rb_erase(&e->u.rb, &ubi->free);
  152. ubi->free_count--;
  153. wl_pool->pebs[wl_pool->size] = e->pnum;
  154. wl_pool->size++;
  155. } else
  156. enough++;
  157. if (enough == 2)
  158. break;
  159. }
  160. wl_pool->used = 0;
  161. pool->used = 0;
  162. spin_unlock(&ubi->wl_lock);
  163. }
  164. /**
  165. * produce_free_peb - produce a free physical eraseblock.
  166. * @ubi: UBI device description object
  167. *
  168. * This function tries to make a free PEB by means of synchronous execution of
  169. * pending works. This may be needed if, for example the background thread is
  170. * disabled. Returns zero in case of success and a negative error code in case
  171. * of failure.
  172. */
  173. static int produce_free_peb(struct ubi_device *ubi)
  174. {
  175. int err;
  176. while (!ubi->free.rb_node && ubi->works_count) {
  177. dbg_wl("do one work synchronously");
  178. err = do_work(ubi);
  179. if (err)
  180. return err;
  181. }
  182. return 0;
  183. }
  184. /**
  185. * ubi_wl_get_peb - get a physical eraseblock.
  186. * @ubi: UBI device description object
  187. *
  188. * This function returns a physical eraseblock in case of success and a
  189. * negative error code in case of failure.
  190. * Returns with ubi->fm_eba_sem held in read mode!
  191. */
  192. int ubi_wl_get_peb(struct ubi_device *ubi)
  193. {
  194. int ret, attempts = 0;
  195. struct ubi_fm_pool *pool = &ubi->fm_pool;
  196. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  197. again:
  198. down_read(&ubi->fm_eba_sem);
  199. spin_lock(&ubi->wl_lock);
  200. /* We check here also for the WL pool because at this point we can
  201. * refill the WL pool synchronous. */
  202. if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
  203. spin_unlock(&ubi->wl_lock);
  204. up_read(&ubi->fm_eba_sem);
  205. ret = ubi_update_fastmap(ubi);
  206. if (ret) {
  207. ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
  208. down_read(&ubi->fm_eba_sem);
  209. return -ENOSPC;
  210. }
  211. down_read(&ubi->fm_eba_sem);
  212. spin_lock(&ubi->wl_lock);
  213. }
  214. if (pool->used == pool->size) {
  215. spin_unlock(&ubi->wl_lock);
  216. attempts++;
  217. if (attempts == 10) {
  218. ubi_err(ubi, "Unable to get a free PEB from user WL pool");
  219. ret = -ENOSPC;
  220. goto out;
  221. }
  222. up_read(&ubi->fm_eba_sem);
  223. ret = produce_free_peb(ubi);
  224. if (ret < 0) {
  225. down_read(&ubi->fm_eba_sem);
  226. goto out;
  227. }
  228. goto again;
  229. }
  230. ubi_assert(pool->used < pool->size);
  231. ret = pool->pebs[pool->used++];
  232. prot_queue_add(ubi, ubi->lookuptbl[ret]);
  233. spin_unlock(&ubi->wl_lock);
  234. out:
  235. return ret;
  236. }
  237. /**
  238. * next_peb_for_wl - returns next PEB to be used internally by the
  239. * WL sub-system.
  240. *
  241. * @ubi: UBI device description object
  242. */
  243. static struct ubi_wl_entry *next_peb_for_wl(struct ubi_device *ubi)
  244. {
  245. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  246. int pnum;
  247. if (pool->used == pool->size)
  248. return NULL;
  249. pnum = pool->pebs[pool->used];
  250. return ubi->lookuptbl[pnum];
  251. }
  252. /**
  253. * need_wear_leveling - checks whether to trigger a wear leveling work.
  254. * UBI fetches free PEB from wl_pool, we check free PEBs from both 'wl_pool'
  255. * and 'ubi->free', because free PEB in 'ubi->free' tree maybe moved into
  256. * 'wl_pool' by ubi_refill_pools().
  257. *
  258. * @ubi: UBI device description object
  259. */
  260. static bool need_wear_leveling(struct ubi_device *ubi)
  261. {
  262. int ec;
  263. struct ubi_wl_entry *e;
  264. if (!ubi->used.rb_node)
  265. return false;
  266. e = next_peb_for_wl(ubi);
  267. if (!e) {
  268. if (!ubi->free.rb_node)
  269. return false;
  270. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  271. ec = e->ec;
  272. } else {
  273. ec = e->ec;
  274. if (ubi->free.rb_node) {
  275. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
  276. ec = max(ec, e->ec);
  277. }
  278. }
  279. e = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  280. return ec - e->ec >= UBI_WL_THRESHOLD;
  281. }
  282. /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
  283. *
  284. * @ubi: UBI device description object
  285. */
  286. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  287. {
  288. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  289. int pnum;
  290. ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
  291. if (pool->used == pool->size) {
  292. /* We cannot update the fastmap here because this
  293. * function is called in atomic context.
  294. * Let's fail here and refill/update it as soon as possible. */
  295. if (!ubi->fm_work_scheduled) {
  296. ubi->fm_work_scheduled = 1;
  297. schedule_work(&ubi->fm_work);
  298. }
  299. return NULL;
  300. }
  301. pnum = pool->pebs[pool->used++];
  302. return ubi->lookuptbl[pnum];
  303. }
  304. /**
  305. * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
  306. * @ubi: UBI device description object
  307. */
  308. int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
  309. {
  310. struct ubi_work *wrk;
  311. struct ubi_wl_entry *anchor;
  312. spin_lock(&ubi->wl_lock);
  313. /* Do we already have an anchor? */
  314. if (ubi->fm_anchor) {
  315. spin_unlock(&ubi->wl_lock);
  316. return 0;
  317. }
  318. /* See if we can find an anchor PEB on the list of free PEBs */
  319. anchor = ubi_wl_get_fm_peb(ubi, 1);
  320. if (anchor) {
  321. ubi->fm_anchor = anchor;
  322. spin_unlock(&ubi->wl_lock);
  323. return 0;
  324. }
  325. ubi->fm_do_produce_anchor = 1;
  326. /* No luck, trigger wear leveling to produce a new anchor PEB. */
  327. if (ubi->wl_scheduled) {
  328. spin_unlock(&ubi->wl_lock);
  329. return 0;
  330. }
  331. ubi->wl_scheduled = 1;
  332. spin_unlock(&ubi->wl_lock);
  333. wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
  334. if (!wrk) {
  335. spin_lock(&ubi->wl_lock);
  336. ubi->wl_scheduled = 0;
  337. spin_unlock(&ubi->wl_lock);
  338. return -ENOMEM;
  339. }
  340. wrk->func = &wear_leveling_worker;
  341. __schedule_ubi_work(ubi, wrk);
  342. return 0;
  343. }
  344. /**
  345. * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
  346. * sub-system.
  347. * see: ubi_wl_put_peb()
  348. *
  349. * @ubi: UBI device description object
  350. * @fm_e: physical eraseblock to return
  351. * @lnum: the last used logical eraseblock number for the PEB
  352. * @torture: if this physical eraseblock has to be tortured
  353. */
  354. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
  355. int lnum, int torture)
  356. {
  357. struct ubi_wl_entry *e;
  358. int vol_id, pnum = fm_e->pnum;
  359. dbg_wl("PEB %d", pnum);
  360. ubi_assert(pnum >= 0);
  361. ubi_assert(pnum < ubi->peb_count);
  362. spin_lock(&ubi->wl_lock);
  363. e = ubi->lookuptbl[pnum];
  364. /* This can happen if we recovered from a fastmap the very
  365. * first time and writing now a new one. In this case the wl system
  366. * has never seen any PEB used by the original fastmap.
  367. */
  368. if (!e) {
  369. e = fm_e;
  370. ubi_assert(e->ec >= 0);
  371. ubi->lookuptbl[pnum] = e;
  372. }
  373. spin_unlock(&ubi->wl_lock);
  374. vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
  375. return schedule_erase(ubi, e, vol_id, lnum, torture, true);
  376. }
  377. /**
  378. * ubi_is_erase_work - checks whether a work is erase work.
  379. * @wrk: The work object to be checked
  380. */
  381. int ubi_is_erase_work(struct ubi_work *wrk)
  382. {
  383. return wrk->func == erase_worker;
  384. }
  385. static void ubi_fastmap_close(struct ubi_device *ubi)
  386. {
  387. int i;
  388. return_unused_pool_pebs(ubi, &ubi->fm_pool);
  389. return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
  390. if (ubi->fm_anchor) {
  391. return_unused_peb(ubi, ubi->fm_anchor);
  392. ubi->fm_anchor = NULL;
  393. }
  394. if (ubi->fm) {
  395. for (i = 0; i < ubi->fm->used_blocks; i++)
  396. kfree(ubi->fm->e[i]);
  397. }
  398. kfree(ubi->fm);
  399. }
  400. /**
  401. * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
  402. * See find_mean_wl_entry()
  403. *
  404. * @ubi: UBI device description object
  405. * @e: physical eraseblock to return
  406. * @root: RB tree to test against.
  407. */
  408. static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
  409. struct ubi_wl_entry *e,
  410. struct rb_root *root) {
  411. if (e && !ubi->fm_disabled && !ubi->fm &&
  412. e->pnum < UBI_FM_MAX_START)
  413. e = rb_entry(rb_next(root->rb_node),
  414. struct ubi_wl_entry, u.rb);
  415. return e;
  416. }