fastmap-wl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 <richard@nod.at>
  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)
  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. * wait_free_pebs_for_pool - wait until there enough free pebs
  87. * @ubi: UBI device description object
  88. *
  89. * Wait and execute do_work until there are enough free pebs, fill pool
  90. * as much as we can. This will reduce pool refilling times, which can
  91. * reduce the fastmap updating frequency.
  92. */
  93. static void wait_free_pebs_for_pool(struct ubi_device *ubi)
  94. {
  95. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  96. struct ubi_fm_pool *pool = &ubi->fm_pool;
  97. int free, expect_free, executed;
  98. /*
  99. * There are at least following free pebs which reserved by UBI:
  100. * 1. WL_RESERVED_PEBS[1]
  101. * 2. EBA_RESERVED_PEBS[1]
  102. * 3. fm pebs - 1: Twice fastmap size deducted by fastmap and fm_anchor
  103. * 4. beb_rsvd_pebs: This value should be get under lock ubi->wl_lock
  104. */
  105. int reserved = WL_RESERVED_PEBS + EBA_RESERVED_PEBS +
  106. ubi->fm_size / ubi->leb_size - 1 + ubi->fm_pool_rsv_cnt;
  107. do {
  108. spin_lock(&ubi->wl_lock);
  109. free = ubi->free_count;
  110. free += pool->size - pool->used + wl_pool->size - wl_pool->used;
  111. expect_free = reserved + ubi->beb_rsvd_pebs;
  112. spin_unlock(&ubi->wl_lock);
  113. /*
  114. * Break out if there are no works or work is executed failure,
  115. * given the fact that erase_worker will schedule itself when
  116. * -EBUSY is returned from mtd layer caused by system shutdown.
  117. */
  118. if (do_work(ubi, &executed) || !executed)
  119. break;
  120. } while (free < expect_free);
  121. }
  122. /*
  123. * left_free_count - returns the number of free pebs to fill fm pools
  124. * @ubi: UBI device description object
  125. *
  126. * This helper function returns the number of free pebs (deducted
  127. * by fastmap pebs) to fill fm_pool and fm_wl_pool.
  128. */
  129. static int left_free_count(struct ubi_device *ubi)
  130. {
  131. int fm_used = 0; // fastmap non anchor pebs.
  132. if (!ubi->free.rb_node)
  133. return 0;
  134. if (!ubi->ro_mode && !ubi->fm_disabled)
  135. fm_used = ubi->fm_size / ubi->leb_size - 1;
  136. return ubi->free_count - fm_used;
  137. }
  138. /*
  139. * can_fill_pools - whether free PEBs will be left after filling pools
  140. * @ubi: UBI device description object
  141. * @free: current number of free PEBs
  142. *
  143. * Return %1 if there are still left free PEBs after filling pools,
  144. * otherwise %0 is returned.
  145. */
  146. static int can_fill_pools(struct ubi_device *ubi, int free)
  147. {
  148. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  149. struct ubi_fm_pool *pool = &ubi->fm_pool;
  150. int pool_need = pool->max_size - pool->size +
  151. wl_pool->max_size - wl_pool->size;
  152. if (free - pool_need < 1)
  153. return 0;
  154. return 1;
  155. }
  156. /**
  157. * ubi_refill_pools_and_lock - refills all fastmap PEB pools and takes fm locks.
  158. * @ubi: UBI device description object
  159. */
  160. void ubi_refill_pools_and_lock(struct ubi_device *ubi)
  161. {
  162. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  163. struct ubi_fm_pool *pool = &ubi->fm_pool;
  164. struct ubi_wl_entry *e;
  165. int enough;
  166. if (!ubi->ro_mode && !ubi->fm_disabled)
  167. wait_free_pebs_for_pool(ubi);
  168. down_write(&ubi->fm_protect);
  169. down_write(&ubi->work_sem);
  170. down_write(&ubi->fm_eba_sem);
  171. spin_lock(&ubi->wl_lock);
  172. return_unused_pool_pebs(ubi, wl_pool);
  173. return_unused_pool_pebs(ubi, pool);
  174. wl_pool->size = 0;
  175. pool->size = 0;
  176. if (ubi->fm_anchor) {
  177. wl_tree_add(ubi->fm_anchor, &ubi->free);
  178. ubi->free_count++;
  179. ubi->fm_anchor = NULL;
  180. }
  181. if (!ubi->fm_disabled)
  182. /*
  183. * All available PEBs are in ubi->free, now is the time to get
  184. * the best anchor PEBs.
  185. */
  186. ubi->fm_anchor = ubi_wl_get_fm_peb(ubi, 1);
  187. for (;;) {
  188. enough = 0;
  189. if (pool->size < pool->max_size) {
  190. if (left_free_count(ubi) <= 0)
  191. break;
  192. e = wl_get_wle(ubi);
  193. if (!e)
  194. break;
  195. pool->pebs[pool->size] = e->pnum;
  196. pool->size++;
  197. } else
  198. enough++;
  199. if (wl_pool->size < wl_pool->max_size) {
  200. int left_free = left_free_count(ubi);
  201. if (left_free <= 0)
  202. break;
  203. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF,
  204. !can_fill_pools(ubi, left_free));
  205. self_check_in_wl_tree(ubi, e, &ubi->free);
  206. rb_erase(&e->u.rb, &ubi->free);
  207. ubi->free_count--;
  208. wl_pool->pebs[wl_pool->size] = e->pnum;
  209. wl_pool->size++;
  210. } else
  211. enough++;
  212. if (enough == 2)
  213. break;
  214. }
  215. wl_pool->used = 0;
  216. pool->used = 0;
  217. spin_unlock(&ubi->wl_lock);
  218. }
  219. /**
  220. * produce_free_peb - produce a free physical eraseblock.
  221. * @ubi: UBI device description object
  222. *
  223. * This function tries to make a free PEB by means of synchronous execution of
  224. * pending works. This may be needed if, for example the background thread is
  225. * disabled. Returns zero in case of success and a negative error code in case
  226. * of failure.
  227. */
  228. static int produce_free_peb(struct ubi_device *ubi)
  229. {
  230. int err;
  231. while (!ubi->free.rb_node && ubi->works_count) {
  232. dbg_wl("do one work synchronously");
  233. err = do_work(ubi, NULL);
  234. if (err)
  235. return err;
  236. }
  237. return 0;
  238. }
  239. /**
  240. * ubi_wl_get_peb - get a physical eraseblock.
  241. * @ubi: UBI device description object
  242. *
  243. * This function returns a physical eraseblock in case of success and a
  244. * negative error code in case of failure.
  245. * Returns with ubi->fm_eba_sem held in read mode!
  246. */
  247. int ubi_wl_get_peb(struct ubi_device *ubi)
  248. {
  249. int ret, attempts = 0;
  250. struct ubi_fm_pool *pool = &ubi->fm_pool;
  251. struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
  252. again:
  253. down_read(&ubi->fm_eba_sem);
  254. spin_lock(&ubi->wl_lock);
  255. /* We check here also for the WL pool because at this point we can
  256. * refill the WL pool synchronous. */
  257. if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
  258. spin_unlock(&ubi->wl_lock);
  259. up_read(&ubi->fm_eba_sem);
  260. ret = ubi_update_fastmap(ubi);
  261. if (ret) {
  262. ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
  263. down_read(&ubi->fm_eba_sem);
  264. return -ENOSPC;
  265. }
  266. down_read(&ubi->fm_eba_sem);
  267. spin_lock(&ubi->wl_lock);
  268. }
  269. if (pool->used == pool->size) {
  270. spin_unlock(&ubi->wl_lock);
  271. attempts++;
  272. if (attempts == 10) {
  273. ubi_err(ubi, "Unable to get a free PEB from user WL pool");
  274. ret = -ENOSPC;
  275. goto out;
  276. }
  277. up_read(&ubi->fm_eba_sem);
  278. ret = produce_free_peb(ubi);
  279. if (ret < 0) {
  280. down_read(&ubi->fm_eba_sem);
  281. goto out;
  282. }
  283. goto again;
  284. }
  285. ubi_assert(pool->used < pool->size);
  286. ret = pool->pebs[pool->used++];
  287. prot_queue_add(ubi, ubi->lookuptbl[ret]);
  288. spin_unlock(&ubi->wl_lock);
  289. out:
  290. return ret;
  291. }
  292. /**
  293. * next_peb_for_wl - returns next PEB to be used internally by the
  294. * WL sub-system.
  295. *
  296. * @ubi: UBI device description object
  297. * @need_fill: whether to fill wear-leveling pool when no PEBs are found
  298. */
  299. static struct ubi_wl_entry *next_peb_for_wl(struct ubi_device *ubi,
  300. bool need_fill)
  301. {
  302. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  303. int pnum;
  304. if (pool->used == pool->size) {
  305. if (need_fill && !ubi->fm_work_scheduled) {
  306. /*
  307. * We cannot update the fastmap here because this
  308. * function is called in atomic context.
  309. * Let's fail here and refill/update it as soon as
  310. * possible.
  311. */
  312. ubi->fm_work_scheduled = 1;
  313. schedule_work(&ubi->fm_work);
  314. }
  315. return NULL;
  316. }
  317. pnum = pool->pebs[pool->used];
  318. return ubi->lookuptbl[pnum];
  319. }
  320. /**
  321. * need_wear_leveling - checks whether to trigger a wear leveling work.
  322. * UBI fetches free PEB from wl_pool, we check free PEBs from both 'wl_pool'
  323. * and 'ubi->free', because free PEB in 'ubi->free' tree maybe moved into
  324. * 'wl_pool' by ubi_refill_pools().
  325. *
  326. * @ubi: UBI device description object
  327. */
  328. static bool need_wear_leveling(struct ubi_device *ubi)
  329. {
  330. int ec;
  331. struct ubi_wl_entry *e;
  332. if (!ubi->used.rb_node)
  333. return false;
  334. e = next_peb_for_wl(ubi, false);
  335. if (!e) {
  336. if (!ubi->free.rb_node)
  337. return false;
  338. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
  339. ec = e->ec;
  340. } else {
  341. ec = e->ec;
  342. if (ubi->free.rb_node) {
  343. e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
  344. ec = max(ec, e->ec);
  345. }
  346. }
  347. e = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
  348. return ec - e->ec >= UBI_WL_THRESHOLD;
  349. }
  350. /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
  351. *
  352. * @ubi: UBI device description object
  353. */
  354. static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
  355. {
  356. struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
  357. int pnum;
  358. ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
  359. if (pool->used == pool->size) {
  360. /* We cannot update the fastmap here because this
  361. * function is called in atomic context.
  362. * Let's fail here and refill/update it as soon as possible. */
  363. if (!ubi->fm_work_scheduled) {
  364. ubi->fm_work_scheduled = 1;
  365. schedule_work(&ubi->fm_work);
  366. }
  367. return NULL;
  368. }
  369. pnum = pool->pebs[pool->used++];
  370. return ubi->lookuptbl[pnum];
  371. }
  372. /**
  373. * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
  374. * @ubi: UBI device description object
  375. */
  376. int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
  377. {
  378. struct ubi_work *wrk;
  379. struct ubi_wl_entry *anchor;
  380. spin_lock(&ubi->wl_lock);
  381. /* Do we already have an anchor? */
  382. if (ubi->fm_anchor) {
  383. spin_unlock(&ubi->wl_lock);
  384. return 0;
  385. }
  386. /* See if we can find an anchor PEB on the list of free PEBs */
  387. anchor = ubi_wl_get_fm_peb(ubi, 1);
  388. if (anchor) {
  389. ubi->fm_anchor = anchor;
  390. spin_unlock(&ubi->wl_lock);
  391. return 0;
  392. }
  393. ubi->fm_do_produce_anchor = 1;
  394. /* No luck, trigger wear leveling to produce a new anchor PEB. */
  395. if (ubi->wl_scheduled) {
  396. spin_unlock(&ubi->wl_lock);
  397. return 0;
  398. }
  399. ubi->wl_scheduled = 1;
  400. spin_unlock(&ubi->wl_lock);
  401. wrk = kmalloc_obj(struct ubi_work, GFP_NOFS);
  402. if (!wrk) {
  403. spin_lock(&ubi->wl_lock);
  404. ubi->wl_scheduled = 0;
  405. spin_unlock(&ubi->wl_lock);
  406. return -ENOMEM;
  407. }
  408. wrk->func = &wear_leveling_worker;
  409. __schedule_ubi_work(ubi, wrk);
  410. return 0;
  411. }
  412. /**
  413. * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
  414. * sub-system.
  415. * see: ubi_wl_put_peb()
  416. *
  417. * @ubi: UBI device description object
  418. * @fm_e: physical eraseblock to return
  419. * @lnum: the last used logical eraseblock number for the PEB
  420. * @torture: if this physical eraseblock has to be tortured
  421. */
  422. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
  423. int lnum, int torture)
  424. {
  425. struct ubi_wl_entry *e;
  426. int vol_id, pnum = fm_e->pnum;
  427. dbg_wl("PEB %d", pnum);
  428. ubi_assert(pnum >= 0);
  429. ubi_assert(pnum < ubi->peb_count);
  430. spin_lock(&ubi->wl_lock);
  431. e = ubi->lookuptbl[pnum];
  432. /* This can happen if we recovered from a fastmap the very
  433. * first time and writing now a new one. In this case the wl system
  434. * has never seen any PEB used by the original fastmap.
  435. */
  436. if (!e) {
  437. e = fm_e;
  438. ubi_assert(e->ec >= 0);
  439. ubi->lookuptbl[pnum] = e;
  440. }
  441. spin_unlock(&ubi->wl_lock);
  442. vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
  443. return schedule_erase(ubi, e, vol_id, lnum, torture, true);
  444. }
  445. /**
  446. * ubi_is_erase_work - checks whether a work is erase work.
  447. * @wrk: The work object to be checked
  448. */
  449. int ubi_is_erase_work(struct ubi_work *wrk)
  450. {
  451. return wrk->func == erase_worker;
  452. }
  453. static void ubi_fastmap_close(struct ubi_device *ubi)
  454. {
  455. return_unused_pool_pebs(ubi, &ubi->fm_pool);
  456. return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
  457. if (ubi->fm_anchor) {
  458. return_unused_peb(ubi, ubi->fm_anchor);
  459. ubi->fm_anchor = NULL;
  460. }
  461. ubi_free_fastmap(ubi);
  462. }
  463. /**
  464. * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
  465. * See find_mean_wl_entry()
  466. *
  467. * @ubi: UBI device description object
  468. * @e: physical eraseblock to return
  469. * @root: RB tree to test against.
  470. */
  471. static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
  472. struct ubi_wl_entry *e,
  473. struct rb_root *root) {
  474. if (e && !ubi->fm_disabled && !ubi->fm && !ubi->fm_anchor &&
  475. e->pnum < UBI_FM_MAX_START)
  476. e = rb_entry(rb_next(root->rb_node),
  477. struct ubi_wl_entry, u.rb);
  478. return e;
  479. }