flush.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #include "flush.h"
  6. #include <linux/mempool.h>
  7. #include <linux/spinlock.h>
  8. #include "logger.h"
  9. #include "memory-alloc.h"
  10. #include "permassert.h"
  11. #include "admin-state.h"
  12. #include "completion.h"
  13. #include "io-submitter.h"
  14. #include "logical-zone.h"
  15. #include "slab-depot.h"
  16. #include "types.h"
  17. #include "vdo.h"
  18. struct flusher {
  19. struct vdo_completion completion;
  20. /* The vdo to which this flusher belongs */
  21. struct vdo *vdo;
  22. /* The administrative state of the flusher */
  23. struct admin_state state;
  24. /* The current flush generation of the vdo */
  25. sequence_number_t flush_generation;
  26. /* The first unacknowledged flush generation */
  27. sequence_number_t first_unacknowledged_generation;
  28. /* The queue of flush requests waiting to notify other threads */
  29. struct vdo_wait_queue notifiers;
  30. /* The queue of flush requests waiting for VIOs to complete */
  31. struct vdo_wait_queue pending_flushes;
  32. /* The flush generation for which notifications are being sent */
  33. sequence_number_t notify_generation;
  34. /* The logical zone to notify next */
  35. struct logical_zone *logical_zone_to_notify;
  36. /* The ID of the thread on which flush requests should be made */
  37. thread_id_t thread_id;
  38. /* The pool of flush requests */
  39. mempool_t *flush_pool;
  40. /* Bios waiting for a flush request to become available */
  41. struct bio_list waiting_flush_bios;
  42. /* The lock to protect the previous fields */
  43. spinlock_t lock;
  44. /* The rotor for selecting the bio queue for submitting flush bios */
  45. zone_count_t bio_queue_rotor;
  46. /* The number of flushes submitted to the current bio queue */
  47. int flush_count;
  48. };
  49. /**
  50. * assert_on_flusher_thread() - Check that we are on the flusher thread.
  51. * @flusher: The flusher.
  52. * @caller: The function which is asserting.
  53. */
  54. static inline void assert_on_flusher_thread(struct flusher *flusher, const char *caller)
  55. {
  56. VDO_ASSERT_LOG_ONLY((vdo_get_callback_thread_id() == flusher->thread_id),
  57. "%s() called from flusher thread", caller);
  58. }
  59. /**
  60. * as_flusher() - Convert a generic vdo_completion to a flusher.
  61. * @completion: The completion to convert.
  62. *
  63. * Return: The completion as a flusher.
  64. */
  65. static struct flusher *as_flusher(struct vdo_completion *completion)
  66. {
  67. vdo_assert_completion_type(completion, VDO_FLUSH_NOTIFICATION_COMPLETION);
  68. return container_of(completion, struct flusher, completion);
  69. }
  70. /**
  71. * completion_as_vdo_flush() - Convert a generic vdo_completion to a vdo_flush.
  72. * @completion: The completion to convert.
  73. *
  74. * Return: The completion as a vdo_flush.
  75. */
  76. static inline struct vdo_flush *completion_as_vdo_flush(struct vdo_completion *completion)
  77. {
  78. vdo_assert_completion_type(completion, VDO_FLUSH_COMPLETION);
  79. return container_of(completion, struct vdo_flush, completion);
  80. }
  81. /**
  82. * vdo_waiter_as_flush() - Convert a vdo_flush's generic wait queue entry back to the vdo_flush.
  83. * @waiter: The wait queue entry to convert.
  84. *
  85. * Return: The wait queue entry as a vdo_flush.
  86. */
  87. static struct vdo_flush *vdo_waiter_as_flush(struct vdo_waiter *waiter)
  88. {
  89. return container_of(waiter, struct vdo_flush, waiter);
  90. }
  91. static void *allocate_flush(gfp_t gfp_mask, void *pool_data)
  92. {
  93. struct vdo_flush *flush = NULL;
  94. if ((gfp_mask & GFP_NOWAIT) == GFP_NOWAIT) {
  95. flush = vdo_allocate_memory_nowait(sizeof(struct vdo_flush), __func__);
  96. } else {
  97. int result = vdo_allocate(1, struct vdo_flush, __func__, &flush);
  98. if (result != VDO_SUCCESS)
  99. vdo_log_error_strerror(result, "failed to allocate spare flush");
  100. }
  101. if (flush != NULL) {
  102. struct flusher *flusher = pool_data;
  103. vdo_initialize_completion(&flush->completion, flusher->vdo,
  104. VDO_FLUSH_COMPLETION);
  105. }
  106. return flush;
  107. }
  108. static void free_flush(void *element, void *pool_data __always_unused)
  109. {
  110. vdo_free(element);
  111. }
  112. /**
  113. * vdo_make_flusher() - Make a flusher for a vdo.
  114. * @vdo: The vdo which owns the flusher.
  115. *
  116. * Return: VDO_SUCCESS or an error.
  117. */
  118. int vdo_make_flusher(struct vdo *vdo)
  119. {
  120. int result = vdo_allocate(1, struct flusher, __func__, &vdo->flusher);
  121. if (result != VDO_SUCCESS)
  122. return result;
  123. vdo->flusher->vdo = vdo;
  124. vdo->flusher->thread_id = vdo->thread_config.packer_thread;
  125. vdo_set_admin_state_code(&vdo->flusher->state, VDO_ADMIN_STATE_NORMAL_OPERATION);
  126. vdo_initialize_completion(&vdo->flusher->completion, vdo,
  127. VDO_FLUSH_NOTIFICATION_COMPLETION);
  128. spin_lock_init(&vdo->flusher->lock);
  129. bio_list_init(&vdo->flusher->waiting_flush_bios);
  130. vdo->flusher->flush_pool = mempool_create(1, allocate_flush, free_flush,
  131. vdo->flusher);
  132. return ((vdo->flusher->flush_pool == NULL) ? -ENOMEM : VDO_SUCCESS);
  133. }
  134. /**
  135. * vdo_free_flusher() - Free a flusher.
  136. * @flusher: The flusher to free.
  137. */
  138. void vdo_free_flusher(struct flusher *flusher)
  139. {
  140. if (flusher == NULL)
  141. return;
  142. if (flusher->flush_pool != NULL)
  143. mempool_destroy(vdo_forget(flusher->flush_pool));
  144. vdo_free(flusher);
  145. }
  146. /**
  147. * vdo_get_flusher_thread_id() - Get the ID of the thread on which flusher functions should be
  148. * called.
  149. * @flusher: The flusher to query.
  150. *
  151. * Return: The ID of the thread which handles the flusher.
  152. */
  153. thread_id_t vdo_get_flusher_thread_id(struct flusher *flusher)
  154. {
  155. return flusher->thread_id;
  156. }
  157. static void notify_flush(struct flusher *flusher);
  158. static void vdo_complete_flush(struct vdo_flush *flush);
  159. /**
  160. * finish_notification() - Finish the notification process.
  161. * @completion: The flusher completion.
  162. *
  163. * Finishes the notification process by checking if any flushes have completed and then starting
  164. * the notification of the next flush request if one came in while the current notification was in
  165. * progress. This callback is registered in flush_packer_callback().
  166. */
  167. static void finish_notification(struct vdo_completion *completion)
  168. {
  169. struct flusher *flusher = as_flusher(completion);
  170. assert_on_flusher_thread(flusher, __func__);
  171. vdo_waitq_enqueue_waiter(&flusher->pending_flushes,
  172. vdo_waitq_dequeue_waiter(&flusher->notifiers));
  173. vdo_complete_flushes(flusher);
  174. if (vdo_waitq_has_waiters(&flusher->notifiers))
  175. notify_flush(flusher);
  176. }
  177. /**
  178. * flush_packer_callback() - Flush the packer.
  179. * @completion: The flusher completion.
  180. *
  181. * Flushes the packer now that all of the logical and physical zones have been notified of the new
  182. * flush request. This callback is registered in increment_generation().
  183. */
  184. static void flush_packer_callback(struct vdo_completion *completion)
  185. {
  186. struct flusher *flusher = as_flusher(completion);
  187. vdo_increment_packer_flush_generation(flusher->vdo->packer);
  188. vdo_launch_completion_callback(completion, finish_notification,
  189. flusher->thread_id);
  190. }
  191. /**
  192. * increment_generation() - Increment the flush generation in a logical zone.
  193. * @completion: The flusher as a completion.
  194. *
  195. * If there are more logical zones, go on to the next one, otherwise, prepare the physical zones.
  196. * This callback is registered both in notify_flush() and in itself.
  197. */
  198. static void increment_generation(struct vdo_completion *completion)
  199. {
  200. struct flusher *flusher = as_flusher(completion);
  201. struct logical_zone *zone = flusher->logical_zone_to_notify;
  202. vdo_increment_logical_zone_flush_generation(zone, flusher->notify_generation);
  203. if (zone->next == NULL) {
  204. vdo_launch_completion_callback(completion, flush_packer_callback,
  205. flusher->thread_id);
  206. return;
  207. }
  208. flusher->logical_zone_to_notify = zone->next;
  209. vdo_launch_completion_callback(completion, increment_generation,
  210. flusher->logical_zone_to_notify->thread_id);
  211. }
  212. /**
  213. * notify_flush() - Launch a flush notification.
  214. * @flusher: The flusher doing the notification.
  215. */
  216. static void notify_flush(struct flusher *flusher)
  217. {
  218. struct vdo_flush *flush =
  219. vdo_waiter_as_flush(vdo_waitq_get_first_waiter(&flusher->notifiers));
  220. flusher->notify_generation = flush->flush_generation;
  221. flusher->logical_zone_to_notify = &flusher->vdo->logical_zones->zones[0];
  222. flusher->completion.requeue = true;
  223. vdo_launch_completion_callback(&flusher->completion, increment_generation,
  224. flusher->logical_zone_to_notify->thread_id);
  225. }
  226. /**
  227. * flush_vdo() - Start processing a flush request.
  228. * @completion: A flush request (as a vdo_completion)
  229. *
  230. * This callback is registered in launch_flush().
  231. */
  232. static void flush_vdo(struct vdo_completion *completion)
  233. {
  234. struct vdo_flush *flush = completion_as_vdo_flush(completion);
  235. struct flusher *flusher = completion->vdo->flusher;
  236. bool may_notify;
  237. int result;
  238. assert_on_flusher_thread(flusher, __func__);
  239. result = VDO_ASSERT(vdo_is_state_normal(&flusher->state),
  240. "flusher is in normal operation");
  241. if (result != VDO_SUCCESS) {
  242. vdo_enter_read_only_mode(flusher->vdo, result);
  243. vdo_complete_flush(flush);
  244. return;
  245. }
  246. flush->flush_generation = flusher->flush_generation++;
  247. may_notify = !vdo_waitq_has_waiters(&flusher->notifiers);
  248. vdo_waitq_enqueue_waiter(&flusher->notifiers, &flush->waiter);
  249. if (may_notify)
  250. notify_flush(flusher);
  251. }
  252. /**
  253. * check_for_drain_complete() - Check whether the flusher has drained.
  254. * @flusher: The flusher.
  255. */
  256. static void check_for_drain_complete(struct flusher *flusher)
  257. {
  258. bool drained;
  259. if (!vdo_is_state_draining(&flusher->state) ||
  260. vdo_waitq_has_waiters(&flusher->pending_flushes))
  261. return;
  262. spin_lock(&flusher->lock);
  263. drained = bio_list_empty(&flusher->waiting_flush_bios);
  264. spin_unlock(&flusher->lock);
  265. if (drained)
  266. vdo_finish_draining(&flusher->state);
  267. }
  268. /**
  269. * vdo_complete_flushes() - Attempt to complete any flushes which might have finished.
  270. * @flusher: The flusher.
  271. */
  272. void vdo_complete_flushes(struct flusher *flusher)
  273. {
  274. sequence_number_t oldest_active_generation = U64_MAX;
  275. struct logical_zone *zone;
  276. assert_on_flusher_thread(flusher, __func__);
  277. for (zone = &flusher->vdo->logical_zones->zones[0]; zone != NULL; zone = zone->next)
  278. oldest_active_generation =
  279. min(oldest_active_generation,
  280. READ_ONCE(zone->oldest_active_generation));
  281. while (vdo_waitq_has_waiters(&flusher->pending_flushes)) {
  282. struct vdo_flush *flush =
  283. vdo_waiter_as_flush(vdo_waitq_get_first_waiter(&flusher->pending_flushes));
  284. if (flush->flush_generation >= oldest_active_generation)
  285. return;
  286. VDO_ASSERT_LOG_ONLY((flush->flush_generation ==
  287. flusher->first_unacknowledged_generation),
  288. "acknowledged next expected flush, %llu, was: %llu",
  289. (unsigned long long) flusher->first_unacknowledged_generation,
  290. (unsigned long long) flush->flush_generation);
  291. vdo_waitq_dequeue_waiter(&flusher->pending_flushes);
  292. vdo_complete_flush(flush);
  293. flusher->first_unacknowledged_generation++;
  294. }
  295. check_for_drain_complete(flusher);
  296. }
  297. /**
  298. * vdo_dump_flusher() - Dump the flusher, in a thread-unsafe fashion.
  299. * @flusher: The flusher.
  300. */
  301. void vdo_dump_flusher(const struct flusher *flusher)
  302. {
  303. vdo_log_info("struct flusher");
  304. vdo_log_info(" flush_generation=%llu first_unacknowledged_generation=%llu",
  305. (unsigned long long) flusher->flush_generation,
  306. (unsigned long long) flusher->first_unacknowledged_generation);
  307. vdo_log_info(" notifiers queue is %s; pending_flushes queue is %s",
  308. (vdo_waitq_has_waiters(&flusher->notifiers) ? "not empty" : "empty"),
  309. (vdo_waitq_has_waiters(&flusher->pending_flushes) ? "not empty" : "empty"));
  310. }
  311. /**
  312. * initialize_flush() - Initialize a vdo_flush structure.
  313. * @flush: The flush to initialize.
  314. * @vdo: The vdo being flushed.
  315. *
  316. * Initializes a vdo_flush structure, transferring all the bios in the flusher's waiting_flush_bios
  317. * list to it. The caller MUST already hold the lock.
  318. */
  319. static void initialize_flush(struct vdo_flush *flush, struct vdo *vdo)
  320. {
  321. bio_list_init(&flush->bios);
  322. bio_list_merge_init(&flush->bios, &vdo->flusher->waiting_flush_bios);
  323. }
  324. static void launch_flush(struct vdo_flush *flush)
  325. {
  326. struct vdo_completion *completion = &flush->completion;
  327. vdo_prepare_completion(completion, flush_vdo, flush_vdo,
  328. completion->vdo->thread_config.packer_thread, NULL);
  329. vdo_enqueue_completion(completion, VDO_DEFAULT_Q_FLUSH_PRIORITY);
  330. }
  331. /**
  332. * vdo_launch_flush() - Function called to start processing a flush request.
  333. * @vdo: The vdo.
  334. * @bio: The bio containing an empty flush request.
  335. *
  336. * This is called when we receive an empty flush bio from the block layer, and before acknowledging
  337. * a non-empty bio with the FUA flag set.
  338. */
  339. void vdo_launch_flush(struct vdo *vdo, struct bio *bio)
  340. {
  341. /*
  342. * Try to allocate a vdo_flush to represent the flush request. If the allocation fails,
  343. * we'll deal with it later.
  344. */
  345. struct vdo_flush *flush = mempool_alloc(vdo->flusher->flush_pool, GFP_NOWAIT);
  346. struct flusher *flusher = vdo->flusher;
  347. const struct admin_state_code *code = vdo_get_admin_state_code(&flusher->state);
  348. VDO_ASSERT_LOG_ONLY(!code->quiescent, "Flushing not allowed in state %s",
  349. code->name);
  350. spin_lock(&flusher->lock);
  351. /* We have a new bio to start. Add it to the list. */
  352. bio_list_add(&flusher->waiting_flush_bios, bio);
  353. if (flush == NULL) {
  354. spin_unlock(&flusher->lock);
  355. return;
  356. }
  357. /* We have flushes to start. Capture them in the vdo_flush structure. */
  358. initialize_flush(flush, vdo);
  359. spin_unlock(&flusher->lock);
  360. /* Finish launching the flushes. */
  361. launch_flush(flush);
  362. }
  363. /**
  364. * release_flush() - Release a vdo_flush structure that has completed its work.
  365. * @flush: The completed flush structure to re-use or free.
  366. *
  367. * If there are any pending flush requests whose vdo_flush allocation failed, they will be launched
  368. * by immediately re-using the released vdo_flush. If there is no spare vdo_flush, the released
  369. * structure will become the spare. Otherwise, the vdo_flush will be freed.
  370. */
  371. static void release_flush(struct vdo_flush *flush)
  372. {
  373. bool relaunch_flush;
  374. struct flusher *flusher = flush->completion.vdo->flusher;
  375. spin_lock(&flusher->lock);
  376. if (bio_list_empty(&flusher->waiting_flush_bios)) {
  377. relaunch_flush = false;
  378. } else {
  379. /* We have flushes to start. Capture them in a flush request. */
  380. initialize_flush(flush, flusher->vdo);
  381. relaunch_flush = true;
  382. }
  383. spin_unlock(&flusher->lock);
  384. if (relaunch_flush) {
  385. /* Finish launching the flushes. */
  386. launch_flush(flush);
  387. return;
  388. }
  389. mempool_free(flush, flusher->flush_pool);
  390. }
  391. /**
  392. * vdo_complete_flush_callback() - Function called to complete and free a flush request, registered
  393. * in vdo_complete_flush().
  394. * @completion: The flush request.
  395. */
  396. static void vdo_complete_flush_callback(struct vdo_completion *completion)
  397. {
  398. struct vdo_flush *flush = completion_as_vdo_flush(completion);
  399. struct vdo *vdo = completion->vdo;
  400. struct bio *bio;
  401. while ((bio = bio_list_pop(&flush->bios)) != NULL) {
  402. /*
  403. * We're not acknowledging this bio now, but we'll never touch it again, so this is
  404. * the last chance to account for it.
  405. */
  406. vdo_count_bios(&vdo->stats.bios_acknowledged, bio);
  407. /* Update the device, and send it on down... */
  408. bio_set_dev(bio, vdo_get_backing_device(vdo));
  409. atomic64_inc(&vdo->stats.flush_out);
  410. submit_bio_noacct(bio);
  411. }
  412. /*
  413. * Release the flush structure, freeing it, re-using it as the spare, or using it to launch
  414. * any flushes that had to wait when allocations failed.
  415. */
  416. release_flush(flush);
  417. }
  418. /**
  419. * select_bio_queue() - Select the bio queue on which to finish a flush request.
  420. * @flusher: The flusher finishing the request.
  421. */
  422. static thread_id_t select_bio_queue(struct flusher *flusher)
  423. {
  424. struct vdo *vdo = flusher->vdo;
  425. zone_count_t bio_threads = flusher->vdo->thread_config.bio_thread_count;
  426. int interval;
  427. if (bio_threads == 1)
  428. return vdo->thread_config.bio_threads[0];
  429. interval = vdo->device_config->thread_counts.bio_rotation_interval;
  430. if (flusher->flush_count == interval) {
  431. flusher->flush_count = 1;
  432. flusher->bio_queue_rotor = ((flusher->bio_queue_rotor + 1) % bio_threads);
  433. } else {
  434. flusher->flush_count++;
  435. }
  436. return vdo->thread_config.bio_threads[flusher->bio_queue_rotor];
  437. }
  438. /**
  439. * vdo_complete_flush() - Complete and free a vdo flush request.
  440. * @flush: The flush request.
  441. */
  442. static void vdo_complete_flush(struct vdo_flush *flush)
  443. {
  444. struct vdo_completion *completion = &flush->completion;
  445. vdo_prepare_completion(completion, vdo_complete_flush_callback,
  446. vdo_complete_flush_callback,
  447. select_bio_queue(completion->vdo->flusher), NULL);
  448. vdo_enqueue_completion(completion, BIO_Q_FLUSH_PRIORITY);
  449. }
  450. /**
  451. * initiate_drain() - Initiate a drain.
  452. *
  453. * Implements vdo_admin_initiator_fn.
  454. */
  455. static void initiate_drain(struct admin_state *state)
  456. {
  457. check_for_drain_complete(container_of(state, struct flusher, state));
  458. }
  459. /**
  460. * vdo_drain_flusher() - Drain the flusher.
  461. * @flusher: The flusher to drain.
  462. * @completion: The completion to finish when the flusher has drained.
  463. *
  464. * Drains the flusher by preventing any more VIOs from entering the flusher and then flushing. The
  465. * flusher will be left in the suspended state.
  466. */
  467. void vdo_drain_flusher(struct flusher *flusher, struct vdo_completion *completion)
  468. {
  469. assert_on_flusher_thread(flusher, __func__);
  470. vdo_start_draining(&flusher->state, VDO_ADMIN_STATE_SUSPENDING, completion,
  471. initiate_drain);
  472. }
  473. /**
  474. * vdo_resume_flusher() - Resume a flusher which has been suspended.
  475. * @flusher: The flusher to resume.
  476. * @parent: The completion to finish when the flusher has resumed.
  477. */
  478. void vdo_resume_flusher(struct flusher *flusher, struct vdo_completion *parent)
  479. {
  480. assert_on_flusher_thread(flusher, __func__);
  481. vdo_continue_completion(parent, vdo_resume_if_quiescent(&flusher->state));
  482. }