dm-kcopyd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2002 Sistina Software (UK) Limited.
  4. * Copyright (C) 2006 Red Hat GmbH
  5. *
  6. * This file is released under the GPL.
  7. *
  8. * Kcopyd provides a simple interface for copying an area of one
  9. * block-device to one or more other block-devices, with an asynchronous
  10. * completion notification.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/atomic.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/fs.h>
  16. #include <linux/init.h>
  17. #include <linux/list.h>
  18. #include <linux/mempool.h>
  19. #include <linux/module.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/slab.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/mutex.h>
  25. #include <linux/delay.h>
  26. #include <linux/device-mapper.h>
  27. #include <linux/dm-kcopyd.h>
  28. #include "dm-core.h"
  29. #define SPLIT_COUNT 8
  30. #define MIN_JOBS 8
  31. #define DEFAULT_SUB_JOB_SIZE_KB 512
  32. #define MAX_SUB_JOB_SIZE_KB 1024
  33. static unsigned int kcopyd_subjob_size_kb = DEFAULT_SUB_JOB_SIZE_KB;
  34. module_param(kcopyd_subjob_size_kb, uint, 0644);
  35. MODULE_PARM_DESC(kcopyd_subjob_size_kb, "Sub-job size for dm-kcopyd clients");
  36. static unsigned int dm_get_kcopyd_subjob_size(void)
  37. {
  38. unsigned int sub_job_size_kb;
  39. sub_job_size_kb = __dm_get_module_param(&kcopyd_subjob_size_kb,
  40. DEFAULT_SUB_JOB_SIZE_KB,
  41. MAX_SUB_JOB_SIZE_KB);
  42. return sub_job_size_kb << 1;
  43. }
  44. /*
  45. *----------------------------------------------------------------
  46. * Each kcopyd client has its own little pool of preallocated
  47. * pages for kcopyd io.
  48. *---------------------------------------------------------------
  49. */
  50. struct dm_kcopyd_client {
  51. struct page_list *pages;
  52. unsigned int nr_reserved_pages;
  53. unsigned int nr_free_pages;
  54. unsigned int sub_job_size;
  55. struct dm_io_client *io_client;
  56. wait_queue_head_t destroyq;
  57. mempool_t job_pool;
  58. struct workqueue_struct *kcopyd_wq;
  59. struct work_struct kcopyd_work;
  60. struct dm_kcopyd_throttle *throttle;
  61. atomic_t nr_jobs;
  62. /*
  63. * We maintain four lists of jobs:
  64. *
  65. * i) jobs waiting for pages
  66. * ii) jobs that have pages, and are waiting for the io to be issued.
  67. * iii) jobs that don't need to do any IO and just run a callback
  68. * iv) jobs that have completed.
  69. *
  70. * All four of these are protected by job_lock.
  71. */
  72. spinlock_t job_lock;
  73. struct list_head callback_jobs;
  74. struct list_head complete_jobs;
  75. struct list_head io_jobs;
  76. struct list_head pages_jobs;
  77. };
  78. static struct page_list zero_page_list;
  79. static DEFINE_SPINLOCK(throttle_spinlock);
  80. /*
  81. * IO/IDLE accounting slowly decays after (1 << ACCOUNT_INTERVAL_SHIFT) period.
  82. * When total_period >= (1 << ACCOUNT_INTERVAL_SHIFT) the counters are divided
  83. * by 2.
  84. */
  85. #define ACCOUNT_INTERVAL_SHIFT SHIFT_HZ
  86. /*
  87. * Sleep this number of milliseconds.
  88. *
  89. * The value was decided experimentally.
  90. * Smaller values seem to cause an increased copy rate above the limit.
  91. * The reason for this is unknown but possibly due to jiffies rounding errors
  92. * or read/write cache inside the disk.
  93. */
  94. #define SLEEP_USEC 100000
  95. /*
  96. * Maximum number of sleep events. There is a theoretical livelock if more
  97. * kcopyd clients do work simultaneously which this limit avoids.
  98. */
  99. #define MAX_SLEEPS 10
  100. static void io_job_start(struct dm_kcopyd_throttle *t)
  101. {
  102. unsigned int throttle, now, difference;
  103. int slept = 0, skew;
  104. if (unlikely(!t))
  105. return;
  106. try_again:
  107. spin_lock_irq(&throttle_spinlock);
  108. throttle = READ_ONCE(t->throttle);
  109. if (likely(throttle >= 100))
  110. goto skip_limit;
  111. now = jiffies;
  112. difference = now - t->last_jiffies;
  113. t->last_jiffies = now;
  114. if (t->num_io_jobs)
  115. t->io_period += difference;
  116. t->total_period += difference;
  117. /*
  118. * Maintain sane values if we got a temporary overflow.
  119. */
  120. if (unlikely(t->io_period > t->total_period))
  121. t->io_period = t->total_period;
  122. if (unlikely(t->total_period >= (1 << ACCOUNT_INTERVAL_SHIFT))) {
  123. int shift = fls(t->total_period >> ACCOUNT_INTERVAL_SHIFT);
  124. t->total_period >>= shift;
  125. t->io_period >>= shift;
  126. }
  127. skew = t->io_period - throttle * t->total_period / 100;
  128. if (unlikely(skew > 0) && slept < MAX_SLEEPS) {
  129. slept++;
  130. spin_unlock_irq(&throttle_spinlock);
  131. fsleep(SLEEP_USEC);
  132. goto try_again;
  133. }
  134. skip_limit:
  135. t->num_io_jobs++;
  136. spin_unlock_irq(&throttle_spinlock);
  137. }
  138. static void io_job_finish(struct dm_kcopyd_throttle *t)
  139. {
  140. unsigned long flags;
  141. if (unlikely(!t))
  142. return;
  143. spin_lock_irqsave(&throttle_spinlock, flags);
  144. t->num_io_jobs--;
  145. if (likely(READ_ONCE(t->throttle) >= 100))
  146. goto skip_limit;
  147. if (!t->num_io_jobs) {
  148. unsigned int now, difference;
  149. now = jiffies;
  150. difference = now - t->last_jiffies;
  151. t->last_jiffies = now;
  152. t->io_period += difference;
  153. t->total_period += difference;
  154. /*
  155. * Maintain sane values if we got a temporary overflow.
  156. */
  157. if (unlikely(t->io_period > t->total_period))
  158. t->io_period = t->total_period;
  159. }
  160. skip_limit:
  161. spin_unlock_irqrestore(&throttle_spinlock, flags);
  162. }
  163. static void wake(struct dm_kcopyd_client *kc)
  164. {
  165. queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
  166. }
  167. /*
  168. * Obtain one page for the use of kcopyd.
  169. */
  170. static struct page_list *alloc_pl(gfp_t gfp)
  171. {
  172. struct page_list *pl;
  173. pl = kmalloc(sizeof(*pl), gfp);
  174. if (!pl)
  175. return NULL;
  176. pl->page = alloc_page(gfp | __GFP_HIGHMEM);
  177. if (!pl->page) {
  178. kfree(pl);
  179. return NULL;
  180. }
  181. return pl;
  182. }
  183. static void free_pl(struct page_list *pl)
  184. {
  185. __free_page(pl->page);
  186. kfree(pl);
  187. }
  188. /*
  189. * Add the provided pages to a client's free page list, releasing
  190. * back to the system any beyond the reserved_pages limit.
  191. */
  192. static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl)
  193. {
  194. struct page_list *next;
  195. do {
  196. next = pl->next;
  197. if (kc->nr_free_pages >= kc->nr_reserved_pages)
  198. free_pl(pl);
  199. else {
  200. pl->next = kc->pages;
  201. kc->pages = pl;
  202. kc->nr_free_pages++;
  203. }
  204. pl = next;
  205. } while (pl);
  206. }
  207. static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
  208. unsigned int nr, struct page_list **pages)
  209. {
  210. struct page_list *pl;
  211. *pages = NULL;
  212. do {
  213. pl = alloc_pl(__GFP_NOWARN | __GFP_NORETRY | __GFP_KSWAPD_RECLAIM);
  214. if (unlikely(!pl)) {
  215. /* Use reserved pages */
  216. pl = kc->pages;
  217. if (unlikely(!pl))
  218. goto out_of_memory;
  219. kc->pages = pl->next;
  220. kc->nr_free_pages--;
  221. }
  222. pl->next = *pages;
  223. *pages = pl;
  224. } while (--nr);
  225. return 0;
  226. out_of_memory:
  227. if (*pages)
  228. kcopyd_put_pages(kc, *pages);
  229. return -ENOMEM;
  230. }
  231. /*
  232. * These three functions resize the page pool.
  233. */
  234. static void drop_pages(struct page_list *pl)
  235. {
  236. struct page_list *next;
  237. while (pl) {
  238. next = pl->next;
  239. free_pl(pl);
  240. pl = next;
  241. }
  242. }
  243. /*
  244. * Allocate and reserve nr_pages for the use of a specific client.
  245. */
  246. static int client_reserve_pages(struct dm_kcopyd_client *kc, unsigned int nr_pages)
  247. {
  248. unsigned int i;
  249. struct page_list *pl = NULL, *next;
  250. for (i = 0; i < nr_pages; i++) {
  251. next = alloc_pl(GFP_KERNEL);
  252. if (!next) {
  253. if (pl)
  254. drop_pages(pl);
  255. return -ENOMEM;
  256. }
  257. next->next = pl;
  258. pl = next;
  259. }
  260. kc->nr_reserved_pages += nr_pages;
  261. kcopyd_put_pages(kc, pl);
  262. return 0;
  263. }
  264. static void client_free_pages(struct dm_kcopyd_client *kc)
  265. {
  266. BUG_ON(kc->nr_free_pages != kc->nr_reserved_pages);
  267. drop_pages(kc->pages);
  268. kc->pages = NULL;
  269. kc->nr_free_pages = kc->nr_reserved_pages = 0;
  270. }
  271. /*
  272. *---------------------------------------------------------------
  273. * kcopyd_jobs need to be allocated by the *clients* of kcopyd,
  274. * for this reason we use a mempool to prevent the client from
  275. * ever having to do io (which could cause a deadlock).
  276. *---------------------------------------------------------------
  277. */
  278. struct kcopyd_job {
  279. struct dm_kcopyd_client *kc;
  280. struct list_head list;
  281. unsigned int flags;
  282. /*
  283. * Error state of the job.
  284. */
  285. int read_err;
  286. unsigned long write_err;
  287. /*
  288. * REQ_OP_READ, REQ_OP_WRITE or REQ_OP_WRITE_ZEROES.
  289. */
  290. enum req_op op;
  291. struct dm_io_region source;
  292. /*
  293. * The destinations for the transfer.
  294. */
  295. unsigned int num_dests;
  296. struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
  297. struct page_list *pages;
  298. /*
  299. * Set this to ensure you are notified when the job has
  300. * completed. 'context' is for callback to use.
  301. */
  302. dm_kcopyd_notify_fn fn;
  303. void *context;
  304. /*
  305. * These fields are only used if the job has been split
  306. * into more manageable parts.
  307. */
  308. struct mutex lock;
  309. atomic_t sub_jobs;
  310. sector_t progress;
  311. sector_t write_offset;
  312. struct kcopyd_job *master_job;
  313. };
  314. static struct kmem_cache *_job_cache;
  315. int __init dm_kcopyd_init(void)
  316. {
  317. _job_cache = kmem_cache_create("kcopyd_job",
  318. sizeof(struct kcopyd_job) * (SPLIT_COUNT + 1),
  319. __alignof__(struct kcopyd_job), 0, NULL);
  320. if (!_job_cache)
  321. return -ENOMEM;
  322. zero_page_list.next = &zero_page_list;
  323. zero_page_list.page = ZERO_PAGE(0);
  324. return 0;
  325. }
  326. void dm_kcopyd_exit(void)
  327. {
  328. kmem_cache_destroy(_job_cache);
  329. _job_cache = NULL;
  330. }
  331. /*
  332. * Functions to push and pop a job onto the head of a given job
  333. * list.
  334. */
  335. static struct kcopyd_job *pop_io_job(struct list_head *jobs,
  336. struct dm_kcopyd_client *kc)
  337. {
  338. struct kcopyd_job *job;
  339. /*
  340. * For I/O jobs, pop any read, any write without sequential write
  341. * constraint and sequential writes that are at the right position.
  342. */
  343. list_for_each_entry(job, jobs, list) {
  344. if (job->op == REQ_OP_READ ||
  345. !(job->flags & BIT(DM_KCOPYD_WRITE_SEQ))) {
  346. list_del(&job->list);
  347. return job;
  348. }
  349. if (job->write_offset == job->master_job->write_offset) {
  350. job->master_job->write_offset += job->source.count;
  351. list_del(&job->list);
  352. return job;
  353. }
  354. }
  355. return NULL;
  356. }
  357. static struct kcopyd_job *pop(struct list_head *jobs,
  358. struct dm_kcopyd_client *kc)
  359. {
  360. struct kcopyd_job *job = NULL;
  361. spin_lock_irq(&kc->job_lock);
  362. if (!list_empty(jobs)) {
  363. if (jobs == &kc->io_jobs)
  364. job = pop_io_job(jobs, kc);
  365. else {
  366. job = list_entry(jobs->next, struct kcopyd_job, list);
  367. list_del(&job->list);
  368. }
  369. }
  370. spin_unlock_irq(&kc->job_lock);
  371. return job;
  372. }
  373. static void push(struct list_head *jobs, struct kcopyd_job *job)
  374. {
  375. unsigned long flags;
  376. struct dm_kcopyd_client *kc = job->kc;
  377. spin_lock_irqsave(&kc->job_lock, flags);
  378. list_add_tail(&job->list, jobs);
  379. spin_unlock_irqrestore(&kc->job_lock, flags);
  380. }
  381. static void push_head(struct list_head *jobs, struct kcopyd_job *job)
  382. {
  383. struct dm_kcopyd_client *kc = job->kc;
  384. spin_lock_irq(&kc->job_lock);
  385. list_add(&job->list, jobs);
  386. spin_unlock_irq(&kc->job_lock);
  387. }
  388. /*
  389. * These three functions process 1 item from the corresponding
  390. * job list.
  391. *
  392. * They return:
  393. * < 0: error
  394. * 0: success
  395. * > 0: can't process yet.
  396. */
  397. static int run_complete_job(struct kcopyd_job *job)
  398. {
  399. void *context = job->context;
  400. int read_err = job->read_err;
  401. unsigned long write_err = job->write_err;
  402. dm_kcopyd_notify_fn fn = job->fn;
  403. struct dm_kcopyd_client *kc = job->kc;
  404. if (job->pages && job->pages != &zero_page_list)
  405. kcopyd_put_pages(kc, job->pages);
  406. /*
  407. * If this is the master job, the sub jobs have already
  408. * completed so we can free everything.
  409. */
  410. if (job->master_job == job) {
  411. mutex_destroy(&job->lock);
  412. mempool_free(job, &kc->job_pool);
  413. }
  414. fn(read_err, write_err, context);
  415. if (atomic_dec_and_test(&kc->nr_jobs))
  416. wake_up(&kc->destroyq);
  417. cond_resched();
  418. return 0;
  419. }
  420. static void complete_io(unsigned long error, void *context)
  421. {
  422. struct kcopyd_job *job = context;
  423. struct dm_kcopyd_client *kc = job->kc;
  424. io_job_finish(kc->throttle);
  425. if (error) {
  426. if (op_is_write(job->op))
  427. job->write_err |= error;
  428. else
  429. job->read_err = 1;
  430. if (!(job->flags & BIT(DM_KCOPYD_IGNORE_ERROR))) {
  431. push(&kc->complete_jobs, job);
  432. wake(kc);
  433. return;
  434. }
  435. }
  436. if (op_is_write(job->op))
  437. push(&kc->complete_jobs, job);
  438. else {
  439. job->op = REQ_OP_WRITE;
  440. push(&kc->io_jobs, job);
  441. }
  442. wake(kc);
  443. }
  444. /*
  445. * Request io on as many buffer heads as we can currently get for
  446. * a particular job.
  447. */
  448. static int run_io_job(struct kcopyd_job *job)
  449. {
  450. int r;
  451. struct dm_io_request io_req = {
  452. .bi_opf = job->op,
  453. .mem.type = DM_IO_PAGE_LIST,
  454. .mem.ptr.pl = job->pages,
  455. .mem.offset = 0,
  456. .notify.fn = complete_io,
  457. .notify.context = job,
  458. .client = job->kc->io_client,
  459. };
  460. /*
  461. * If we need to write sequentially and some reads or writes failed,
  462. * no point in continuing.
  463. */
  464. if (job->flags & BIT(DM_KCOPYD_WRITE_SEQ) &&
  465. job->master_job->write_err) {
  466. job->write_err = job->master_job->write_err;
  467. return -EIO;
  468. }
  469. io_job_start(job->kc->throttle);
  470. if (job->op == REQ_OP_READ)
  471. r = dm_io(&io_req, 1, &job->source, NULL, IOPRIO_DEFAULT);
  472. else
  473. r = dm_io(&io_req, job->num_dests, job->dests, NULL, IOPRIO_DEFAULT);
  474. return r;
  475. }
  476. static int run_pages_job(struct kcopyd_job *job)
  477. {
  478. int r;
  479. unsigned int nr_pages = dm_div_up(job->dests[0].count, PAGE_SIZE >> 9);
  480. r = kcopyd_get_pages(job->kc, nr_pages, &job->pages);
  481. if (!r) {
  482. /* this job is ready for io */
  483. push(&job->kc->io_jobs, job);
  484. return 0;
  485. }
  486. if (r == -ENOMEM)
  487. /* can't complete now */
  488. return 1;
  489. return r;
  490. }
  491. /*
  492. * Run through a list for as long as possible. Returns the count
  493. * of successful jobs.
  494. */
  495. static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
  496. int (*fn)(struct kcopyd_job *))
  497. {
  498. struct kcopyd_job *job;
  499. int r, count = 0;
  500. while ((job = pop(jobs, kc))) {
  501. r = fn(job);
  502. if (r < 0) {
  503. /* error this rogue job */
  504. if (op_is_write(job->op))
  505. job->write_err = (unsigned long) -1L;
  506. else
  507. job->read_err = 1;
  508. push(&kc->complete_jobs, job);
  509. wake(kc);
  510. break;
  511. }
  512. if (r > 0) {
  513. /*
  514. * We couldn't service this job ATM, so
  515. * push this job back onto the list.
  516. */
  517. push_head(jobs, job);
  518. break;
  519. }
  520. count++;
  521. }
  522. return count;
  523. }
  524. /*
  525. * kcopyd does this every time it's woken up.
  526. */
  527. static void do_work(struct work_struct *work)
  528. {
  529. struct dm_kcopyd_client *kc = container_of(work,
  530. struct dm_kcopyd_client, kcopyd_work);
  531. struct blk_plug plug;
  532. /*
  533. * The order that these are called is *very* important.
  534. * complete jobs can free some pages for pages jobs.
  535. * Pages jobs when successful will jump onto the io jobs
  536. * list. io jobs call wake when they complete and it all
  537. * starts again.
  538. */
  539. spin_lock_irq(&kc->job_lock);
  540. list_splice_tail_init(&kc->callback_jobs, &kc->complete_jobs);
  541. spin_unlock_irq(&kc->job_lock);
  542. blk_start_plug(&plug);
  543. process_jobs(&kc->complete_jobs, kc, run_complete_job);
  544. process_jobs(&kc->pages_jobs, kc, run_pages_job);
  545. process_jobs(&kc->io_jobs, kc, run_io_job);
  546. blk_finish_plug(&plug);
  547. }
  548. /*
  549. * If we are copying a small region we just dispatch a single job
  550. * to do the copy, otherwise the io has to be split up into many
  551. * jobs.
  552. */
  553. static void dispatch_job(struct kcopyd_job *job)
  554. {
  555. struct dm_kcopyd_client *kc = job->kc;
  556. atomic_inc(&kc->nr_jobs);
  557. if (unlikely(!job->source.count))
  558. push(&kc->callback_jobs, job);
  559. else if (job->pages == &zero_page_list)
  560. push(&kc->io_jobs, job);
  561. else
  562. push(&kc->pages_jobs, job);
  563. wake(kc);
  564. }
  565. static void segment_complete(int read_err, unsigned long write_err,
  566. void *context)
  567. {
  568. /* FIXME: tidy this function */
  569. sector_t progress = 0;
  570. sector_t count = 0;
  571. struct kcopyd_job *sub_job = context;
  572. struct kcopyd_job *job = sub_job->master_job;
  573. struct dm_kcopyd_client *kc = job->kc;
  574. mutex_lock(&job->lock);
  575. /* update the error */
  576. if (read_err)
  577. job->read_err = 1;
  578. if (write_err)
  579. job->write_err |= write_err;
  580. /*
  581. * Only dispatch more work if there hasn't been an error.
  582. */
  583. if ((!job->read_err && !job->write_err) ||
  584. job->flags & BIT(DM_KCOPYD_IGNORE_ERROR)) {
  585. /* get the next chunk of work */
  586. progress = job->progress;
  587. count = job->source.count - progress;
  588. if (count) {
  589. if (count > kc->sub_job_size)
  590. count = kc->sub_job_size;
  591. job->progress += count;
  592. }
  593. }
  594. mutex_unlock(&job->lock);
  595. if (count) {
  596. int i;
  597. *sub_job = *job;
  598. sub_job->write_offset = progress;
  599. sub_job->source.sector += progress;
  600. sub_job->source.count = count;
  601. for (i = 0; i < job->num_dests; i++) {
  602. sub_job->dests[i].sector += progress;
  603. sub_job->dests[i].count = count;
  604. }
  605. sub_job->fn = segment_complete;
  606. sub_job->context = sub_job;
  607. dispatch_job(sub_job);
  608. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  609. /*
  610. * Queue the completion callback to the kcopyd thread.
  611. *
  612. * Some callers assume that all the completions are called
  613. * from a single thread and don't race with each other.
  614. *
  615. * We must not call the callback directly here because this
  616. * code may not be executing in the thread.
  617. */
  618. push(&kc->complete_jobs, job);
  619. wake(kc);
  620. }
  621. }
  622. /*
  623. * Create some sub jobs to share the work between them.
  624. */
  625. static void split_job(struct kcopyd_job *master_job)
  626. {
  627. int i;
  628. atomic_inc(&master_job->kc->nr_jobs);
  629. atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
  630. for (i = 0; i < SPLIT_COUNT; i++) {
  631. master_job[i + 1].master_job = master_job;
  632. segment_complete(0, 0u, &master_job[i + 1]);
  633. }
  634. }
  635. void dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
  636. unsigned int num_dests, struct dm_io_region *dests,
  637. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  638. {
  639. struct kcopyd_job *job;
  640. int i;
  641. /*
  642. * Allocate an array of jobs consisting of one master job
  643. * followed by SPLIT_COUNT sub jobs.
  644. */
  645. job = mempool_alloc(&kc->job_pool, GFP_NOIO);
  646. mutex_init(&job->lock);
  647. /*
  648. * set up for the read.
  649. */
  650. job->kc = kc;
  651. job->flags = flags;
  652. job->read_err = 0;
  653. job->write_err = 0;
  654. job->num_dests = num_dests;
  655. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  656. /*
  657. * If one of the destination is a host-managed zoned block device,
  658. * we need to write sequentially. If one of the destination is a
  659. * host-aware device, then leave it to the caller to choose what to do.
  660. */
  661. if (!(job->flags & BIT(DM_KCOPYD_WRITE_SEQ))) {
  662. for (i = 0; i < job->num_dests; i++) {
  663. if (bdev_is_zoned(dests[i].bdev)) {
  664. job->flags |= BIT(DM_KCOPYD_WRITE_SEQ);
  665. break;
  666. }
  667. }
  668. }
  669. /*
  670. * If we need to write sequentially, errors cannot be ignored.
  671. */
  672. if (job->flags & BIT(DM_KCOPYD_WRITE_SEQ) &&
  673. job->flags & BIT(DM_KCOPYD_IGNORE_ERROR))
  674. job->flags &= ~BIT(DM_KCOPYD_IGNORE_ERROR);
  675. if (from) {
  676. job->source = *from;
  677. job->pages = NULL;
  678. job->op = REQ_OP_READ;
  679. } else {
  680. memset(&job->source, 0, sizeof(job->source));
  681. job->source.count = job->dests[0].count;
  682. job->pages = &zero_page_list;
  683. /*
  684. * Use WRITE ZEROES to optimize zeroing if all dests support it.
  685. */
  686. job->op = REQ_OP_WRITE_ZEROES;
  687. for (i = 0; i < job->num_dests; i++)
  688. if (!bdev_write_zeroes_sectors(job->dests[i].bdev)) {
  689. job->op = REQ_OP_WRITE;
  690. break;
  691. }
  692. }
  693. job->fn = fn;
  694. job->context = context;
  695. job->master_job = job;
  696. job->write_offset = 0;
  697. if (job->source.count <= kc->sub_job_size)
  698. dispatch_job(job);
  699. else {
  700. job->progress = 0;
  701. split_job(job);
  702. }
  703. }
  704. EXPORT_SYMBOL(dm_kcopyd_copy);
  705. void dm_kcopyd_zero(struct dm_kcopyd_client *kc,
  706. unsigned int num_dests, struct dm_io_region *dests,
  707. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  708. {
  709. dm_kcopyd_copy(kc, NULL, num_dests, dests, flags, fn, context);
  710. }
  711. EXPORT_SYMBOL(dm_kcopyd_zero);
  712. void *dm_kcopyd_prepare_callback(struct dm_kcopyd_client *kc,
  713. dm_kcopyd_notify_fn fn, void *context)
  714. {
  715. struct kcopyd_job *job;
  716. job = mempool_alloc(&kc->job_pool, GFP_NOIO);
  717. memset(job, 0, sizeof(struct kcopyd_job));
  718. job->kc = kc;
  719. job->fn = fn;
  720. job->context = context;
  721. job->master_job = job;
  722. atomic_inc(&kc->nr_jobs);
  723. return job;
  724. }
  725. EXPORT_SYMBOL(dm_kcopyd_prepare_callback);
  726. void dm_kcopyd_do_callback(void *j, int read_err, unsigned long write_err)
  727. {
  728. struct kcopyd_job *job = j;
  729. struct dm_kcopyd_client *kc = job->kc;
  730. job->read_err = read_err;
  731. job->write_err = write_err;
  732. push(&kc->callback_jobs, job);
  733. wake(kc);
  734. }
  735. EXPORT_SYMBOL(dm_kcopyd_do_callback);
  736. /*
  737. * Cancels a kcopyd job, eg. someone might be deactivating a
  738. * mirror.
  739. */
  740. #if 0
  741. int kcopyd_cancel(struct kcopyd_job *job, int block)
  742. {
  743. /* FIXME: finish */
  744. return -1;
  745. }
  746. #endif /* 0 */
  747. /*
  748. *---------------------------------------------------------------
  749. * Client setup
  750. *---------------------------------------------------------------
  751. */
  752. struct dm_kcopyd_client *dm_kcopyd_client_create(struct dm_kcopyd_throttle *throttle)
  753. {
  754. int r;
  755. unsigned int reserve_pages;
  756. struct dm_kcopyd_client *kc;
  757. kc = kzalloc(sizeof(*kc), GFP_KERNEL);
  758. if (!kc)
  759. return ERR_PTR(-ENOMEM);
  760. spin_lock_init(&kc->job_lock);
  761. INIT_LIST_HEAD(&kc->callback_jobs);
  762. INIT_LIST_HEAD(&kc->complete_jobs);
  763. INIT_LIST_HEAD(&kc->io_jobs);
  764. INIT_LIST_HEAD(&kc->pages_jobs);
  765. kc->throttle = throttle;
  766. r = mempool_init_slab_pool(&kc->job_pool, MIN_JOBS, _job_cache);
  767. if (r)
  768. goto bad_slab;
  769. INIT_WORK(&kc->kcopyd_work, do_work);
  770. kc->kcopyd_wq = alloc_workqueue("kcopyd", WQ_MEM_RECLAIM, 0);
  771. if (!kc->kcopyd_wq) {
  772. r = -ENOMEM;
  773. goto bad_workqueue;
  774. }
  775. kc->sub_job_size = dm_get_kcopyd_subjob_size();
  776. reserve_pages = DIV_ROUND_UP(kc->sub_job_size << SECTOR_SHIFT, PAGE_SIZE);
  777. kc->pages = NULL;
  778. kc->nr_reserved_pages = kc->nr_free_pages = 0;
  779. r = client_reserve_pages(kc, reserve_pages);
  780. if (r)
  781. goto bad_client_pages;
  782. kc->io_client = dm_io_client_create();
  783. if (IS_ERR(kc->io_client)) {
  784. r = PTR_ERR(kc->io_client);
  785. goto bad_io_client;
  786. }
  787. init_waitqueue_head(&kc->destroyq);
  788. atomic_set(&kc->nr_jobs, 0);
  789. return kc;
  790. bad_io_client:
  791. client_free_pages(kc);
  792. bad_client_pages:
  793. destroy_workqueue(kc->kcopyd_wq);
  794. bad_workqueue:
  795. mempool_exit(&kc->job_pool);
  796. bad_slab:
  797. kfree(kc);
  798. return ERR_PTR(r);
  799. }
  800. EXPORT_SYMBOL(dm_kcopyd_client_create);
  801. void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
  802. {
  803. /* Wait for completion of all jobs submitted by this client. */
  804. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  805. BUG_ON(!list_empty(&kc->callback_jobs));
  806. BUG_ON(!list_empty(&kc->complete_jobs));
  807. BUG_ON(!list_empty(&kc->io_jobs));
  808. BUG_ON(!list_empty(&kc->pages_jobs));
  809. destroy_workqueue(kc->kcopyd_wq);
  810. dm_io_client_destroy(kc->io_client);
  811. client_free_pages(kc);
  812. mempool_exit(&kc->job_pool);
  813. kfree(kc);
  814. }
  815. EXPORT_SYMBOL(dm_kcopyd_client_destroy);
  816. void dm_kcopyd_client_flush(struct dm_kcopyd_client *kc)
  817. {
  818. flush_workqueue(kc->kcopyd_wq);
  819. }
  820. EXPORT_SYMBOL(dm_kcopyd_client_flush);