xref: /OK3568_Linux_fs/kernel/drivers/md/dm-kcopyd.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2002 Sistina Software (UK) Limited.
3  * Copyright (C) 2006 Red Hat GmbH
4  *
5  * This file is released under the GPL.
6  *
7  * Kcopyd provides a simple interface for copying an area of one
8  * block-device to one or more other block-devices, with an asynchronous
9  * completion notification.
10  */
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/of_platform.h>
21 #include <linux/of_reserved_mem.h>
22 #include <linux/pagemap.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/workqueue.h>
26 #include <linux/mutex.h>
27 #include <linux/delay.h>
28 #include <linux/device-mapper.h>
29 #include <linux/dm-kcopyd.h>
30 
31 #include "dm-core.h"
32 
33 #define SPLIT_COUNT	8
34 #define MIN_JOBS	8
35 
36 #define DEFAULT_SUB_JOB_SIZE_KB 512
37 #define MAX_SUB_JOB_SIZE_KB     1024
38 
39 static unsigned kcopyd_subjob_size_kb = DEFAULT_SUB_JOB_SIZE_KB;
40 
41 module_param(kcopyd_subjob_size_kb, uint, S_IRUGO | S_IWUSR);
42 MODULE_PARM_DESC(kcopyd_subjob_size_kb, "Sub-job size for dm-kcopyd clients");
43 
44 static bool rsm_enabled;
45 static phys_addr_t rsm_mem_base, rsm_mem_size;
46 
47 #ifndef MODULE
48 static DEFINE_SPINLOCK(rsm_lock);
49 static int *rsm_mem;
50 static int rsm_page_cnt;
51 static int rsm_tbl_idx;
52 static struct reserved_mem *rmem;
53 
kcopyd_rsm_init(void)54 static void __init kcopyd_rsm_init(void)
55 {
56 	static struct device_node *rsm_node;
57 	int ret = 0;
58 
59 	if (!rsm_enabled)
60 		return;
61 
62 	rsm_node = of_find_compatible_node(NULL, NULL, "mediatek,dm_ota");
63 	if (!rsm_node) {
64 		ret = -ENODEV;
65 		goto out;
66 	}
67 
68 	rmem = of_reserved_mem_lookup(rsm_node);
69 	if (!rmem) {
70 		ret = -EINVAL;
71 		goto out_put_node;
72 	}
73 
74 	rsm_mem_base = rmem->base;
75 	rsm_mem_size = rmem->size;
76 	rsm_page_cnt = rsm_mem_size / PAGE_SIZE;
77 	rsm_mem = kcalloc(rsm_page_cnt, sizeof(int), GFP_KERNEL);
78 	if (!rsm_mem)
79 		ret = -ENOMEM;
80 
81 out_put_node:
82 	of_node_put(rsm_node);
83 out:
84 	if (ret)
85 		pr_warn("kcopyd: failed to init rsm: %d", ret);
86 }
87 
kcopyd_rsm_enable(char * str)88 static int __init kcopyd_rsm_enable(char *str)
89 {
90 	rsm_enabled = true;
91 
92 	return 0;
93 }
94 early_param("mtk_kcopyd_quirk", kcopyd_rsm_enable);
95 
kcopyd_rsm_get_page(struct page ** p)96 static void kcopyd_rsm_get_page(struct page **p)
97 {
98 	int i;
99 	unsigned long flags;
100 
101 	*p = NULL;
102 	spin_lock_irqsave(&rsm_lock, flags);
103 	for (i = 0 ; i < rsm_page_cnt ; i++) {
104 		rsm_tbl_idx = (rsm_tbl_idx + 1 == rsm_page_cnt) ? 0 : rsm_tbl_idx + 1;
105 
106 		if (rsm_mem[rsm_tbl_idx] == 0) {
107 			rsm_mem[rsm_tbl_idx] = 1;
108 			*p = virt_to_page(phys_to_virt(rsm_mem_base + PAGE_SIZE
109 						       * rsm_tbl_idx));
110 			break;
111 		}
112 	}
113 	spin_unlock_irqrestore(&rsm_lock, flags);
114 }
115 
kcopyd_rsm_drop_page(struct page ** p)116 static void kcopyd_rsm_drop_page(struct page **p)
117 {
118 	u64 off;
119 	unsigned long flags;
120 
121 	if (*p) {
122 		off = page_to_phys(*p) - rsm_mem_base;
123 		spin_lock_irqsave(&rsm_lock, flags);
124 		rsm_mem[off >> PAGE_SHIFT] = 0;
125 		spin_unlock_irqrestore(&rsm_lock, flags);
126 		*p = NULL;
127 	}
128 }
129 
kcopyd_rsm_destroy(void)130 static void kcopyd_rsm_destroy(void)
131 {
132 	if (rsm_enabled)
133 		kfree(rsm_mem);
134 }
135 
136 #else
137 #define kcopyd_rsm_destroy(...)
138 #define kcopyd_rsm_drop_page(...)
139 #define kcopyd_rsm_get_page(...)
140 #define kcopyd_rsm_init(...)
141 #endif
142 
dm_get_kcopyd_subjob_size(void)143 static unsigned dm_get_kcopyd_subjob_size(void)
144 {
145 	unsigned sub_job_size_kb;
146 
147 	sub_job_size_kb = __dm_get_module_param(&kcopyd_subjob_size_kb,
148 						DEFAULT_SUB_JOB_SIZE_KB,
149 						MAX_SUB_JOB_SIZE_KB);
150 
151 	return sub_job_size_kb << 1;
152 }
153 
154 /*-----------------------------------------------------------------
155  * Each kcopyd client has its own little pool of preallocated
156  * pages for kcopyd io.
157  *---------------------------------------------------------------*/
158 struct dm_kcopyd_client {
159 	struct page_list *pages;
160 	unsigned nr_reserved_pages;
161 	unsigned nr_free_pages;
162 	unsigned sub_job_size;
163 
164 	struct dm_io_client *io_client;
165 
166 	wait_queue_head_t destroyq;
167 
168 	mempool_t job_pool;
169 
170 	struct workqueue_struct *kcopyd_wq;
171 	struct work_struct kcopyd_work;
172 
173 	struct dm_kcopyd_throttle *throttle;
174 
175 	atomic_t nr_jobs;
176 
177 /*
178  * We maintain four lists of jobs:
179  *
180  * i)   jobs waiting for pages
181  * ii)  jobs that have pages, and are waiting for the io to be issued.
182  * iii) jobs that don't need to do any IO and just run a callback
183  * iv) jobs that have completed.
184  *
185  * All four of these are protected by job_lock.
186  */
187 	spinlock_t job_lock;
188 	struct list_head callback_jobs;
189 	struct list_head complete_jobs;
190 	struct list_head io_jobs;
191 	struct list_head pages_jobs;
192 };
193 
194 static struct page_list zero_page_list;
195 
196 static DEFINE_SPINLOCK(throttle_spinlock);
197 
198 /*
199  * IO/IDLE accounting slowly decays after (1 << ACCOUNT_INTERVAL_SHIFT) period.
200  * When total_period >= (1 << ACCOUNT_INTERVAL_SHIFT) the counters are divided
201  * by 2.
202  */
203 #define ACCOUNT_INTERVAL_SHIFT		SHIFT_HZ
204 
205 /*
206  * Sleep this number of milliseconds.
207  *
208  * The value was decided experimentally.
209  * Smaller values seem to cause an increased copy rate above the limit.
210  * The reason for this is unknown but possibly due to jiffies rounding errors
211  * or read/write cache inside the disk.
212  */
213 #define SLEEP_MSEC			100
214 
215 /*
216  * Maximum number of sleep events. There is a theoretical livelock if more
217  * kcopyd clients do work simultaneously which this limit avoids.
218  */
219 #define MAX_SLEEPS			10
220 
io_job_start(struct dm_kcopyd_throttle * t)221 static void io_job_start(struct dm_kcopyd_throttle *t)
222 {
223 	unsigned throttle, now, difference;
224 	int slept = 0, skew;
225 
226 	if (unlikely(!t))
227 		return;
228 
229 try_again:
230 	spin_lock_irq(&throttle_spinlock);
231 
232 	throttle = READ_ONCE(t->throttle);
233 
234 	if (likely(throttle >= 100))
235 		goto skip_limit;
236 
237 	now = jiffies;
238 	difference = now - t->last_jiffies;
239 	t->last_jiffies = now;
240 	if (t->num_io_jobs)
241 		t->io_period += difference;
242 	t->total_period += difference;
243 
244 	/*
245 	 * Maintain sane values if we got a temporary overflow.
246 	 */
247 	if (unlikely(t->io_period > t->total_period))
248 		t->io_period = t->total_period;
249 
250 	if (unlikely(t->total_period >= (1 << ACCOUNT_INTERVAL_SHIFT))) {
251 		int shift = fls(t->total_period >> ACCOUNT_INTERVAL_SHIFT);
252 		t->total_period >>= shift;
253 		t->io_period >>= shift;
254 	}
255 
256 	skew = t->io_period - throttle * t->total_period / 100;
257 
258 	if (unlikely(skew > 0) && slept < MAX_SLEEPS) {
259 		slept++;
260 		spin_unlock_irq(&throttle_spinlock);
261 		msleep(SLEEP_MSEC);
262 		goto try_again;
263 	}
264 
265 skip_limit:
266 	t->num_io_jobs++;
267 
268 	spin_unlock_irq(&throttle_spinlock);
269 }
270 
io_job_finish(struct dm_kcopyd_throttle * t)271 static void io_job_finish(struct dm_kcopyd_throttle *t)
272 {
273 	unsigned long flags;
274 
275 	if (unlikely(!t))
276 		return;
277 
278 	spin_lock_irqsave(&throttle_spinlock, flags);
279 
280 	t->num_io_jobs--;
281 
282 	if (likely(READ_ONCE(t->throttle) >= 100))
283 		goto skip_limit;
284 
285 	if (!t->num_io_jobs) {
286 		unsigned now, difference;
287 
288 		now = jiffies;
289 		difference = now - t->last_jiffies;
290 		t->last_jiffies = now;
291 
292 		t->io_period += difference;
293 		t->total_period += difference;
294 
295 		/*
296 		 * Maintain sane values if we got a temporary overflow.
297 		 */
298 		if (unlikely(t->io_period > t->total_period))
299 			t->io_period = t->total_period;
300 	}
301 
302 skip_limit:
303 	spin_unlock_irqrestore(&throttle_spinlock, flags);
304 }
305 
306 
wake(struct dm_kcopyd_client * kc)307 static void wake(struct dm_kcopyd_client *kc)
308 {
309 	queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
310 }
311 
312 /*
313  * Obtain one page for the use of kcopyd.
314  */
alloc_pl(gfp_t gfp,unsigned long job_flags)315 static struct page_list *alloc_pl(gfp_t gfp, unsigned long job_flags)
316 {
317 	struct page_list *pl;
318 
319 	pl = kmalloc(sizeof(*pl), gfp);
320 	if (!pl)
321 		return NULL;
322 
323 	if (rsm_enabled && test_bit(DM_KCOPYD_SNAP_MERGE, &job_flags)) {
324 		kcopyd_rsm_get_page(&pl->page);
325 	} else {
326 		pl->page = alloc_page(gfp);
327 	}
328 
329 	if (!pl->page) {
330 		kfree(pl);
331 		return NULL;
332 	}
333 
334 	return pl;
335 }
336 
free_pl(struct page_list * pl)337 static void free_pl(struct page_list *pl)
338 {
339 	struct page *p = pl->page;
340 	phys_addr_t pa = page_to_phys(p);
341 
342 	if (rsm_enabled && pa >= rsm_mem_base && pa < rsm_mem_base + rsm_mem_size)
343 		kcopyd_rsm_drop_page(&pl->page);
344 	else
345 		__free_page(pl->page);
346 
347 	kfree(pl);
348 }
349 
350 /*
351  * Add the provided pages to a client's free page list, releasing
352  * back to the system any beyond the reserved_pages limit.
353  */
kcopyd_put_pages(struct dm_kcopyd_client * kc,struct page_list * pl)354 static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl)
355 {
356 	struct page_list *next;
357 
358 	do {
359 		next = pl->next;
360 
361 		if (kc->nr_free_pages >= kc->nr_reserved_pages)
362 			free_pl(pl);
363 		else {
364 			pl->next = kc->pages;
365 			kc->pages = pl;
366 			kc->nr_free_pages++;
367 		}
368 
369 		pl = next;
370 	} while (pl);
371 }
372 
kcopyd_get_pages(struct dm_kcopyd_client * kc,unsigned int nr,struct page_list ** pages,unsigned long job_flags)373 static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
374 			    unsigned int nr, struct page_list **pages,
375 			    unsigned long job_flags)
376 {
377 	struct page_list *pl;
378 
379 	*pages = NULL;
380 
381 	do {
382 		pl = alloc_pl(__GFP_NOWARN | __GFP_NORETRY | __GFP_KSWAPD_RECLAIM, job_flags);
383 		if (unlikely(!pl)) {
384 			/* Use reserved pages */
385 			pl = kc->pages;
386 			if (unlikely(!pl))
387 				goto out_of_memory;
388 			kc->pages = pl->next;
389 			kc->nr_free_pages--;
390 		}
391 		pl->next = *pages;
392 		*pages = pl;
393 	} while (--nr);
394 
395 	return 0;
396 
397 out_of_memory:
398 	if (*pages)
399 		kcopyd_put_pages(kc, *pages);
400 	return -ENOMEM;
401 }
402 
403 /*
404  * These three functions resize the page pool.
405  */
drop_pages(struct page_list * pl)406 static void drop_pages(struct page_list *pl)
407 {
408 	struct page_list *next;
409 
410 	while (pl) {
411 		next = pl->next;
412 		free_pl(pl);
413 		pl = next;
414 	}
415 }
416 
417 /*
418  * Allocate and reserve nr_pages for the use of a specific client.
419  */
client_reserve_pages(struct dm_kcopyd_client * kc,unsigned nr_pages)420 static int client_reserve_pages(struct dm_kcopyd_client *kc, unsigned nr_pages)
421 {
422 	unsigned i;
423 	struct page_list *pl = NULL, *next;
424 
425 	for (i = 0; i < nr_pages; i++) {
426 		next = alloc_pl(GFP_KERNEL, 0);
427 		if (!next) {
428 			if (pl)
429 				drop_pages(pl);
430 			return -ENOMEM;
431 		}
432 		next->next = pl;
433 		pl = next;
434 	}
435 
436 	kc->nr_reserved_pages += nr_pages;
437 	kcopyd_put_pages(kc, pl);
438 
439 	return 0;
440 }
441 
client_free_pages(struct dm_kcopyd_client * kc)442 static void client_free_pages(struct dm_kcopyd_client *kc)
443 {
444 	BUG_ON(kc->nr_free_pages != kc->nr_reserved_pages);
445 	drop_pages(kc->pages);
446 	kc->pages = NULL;
447 	kc->nr_free_pages = kc->nr_reserved_pages = 0;
448 }
449 
450 /*-----------------------------------------------------------------
451  * kcopyd_jobs need to be allocated by the *clients* of kcopyd,
452  * for this reason we use a mempool to prevent the client from
453  * ever having to do io (which could cause a deadlock).
454  *---------------------------------------------------------------*/
455 struct kcopyd_job {
456 	struct dm_kcopyd_client *kc;
457 	struct list_head list;
458 	unsigned long flags;
459 
460 	/*
461 	 * Error state of the job.
462 	 */
463 	int read_err;
464 	unsigned long write_err;
465 
466 	/*
467 	 * Either READ or WRITE
468 	 */
469 	int rw;
470 	struct dm_io_region source;
471 
472 	/*
473 	 * The destinations for the transfer.
474 	 */
475 	unsigned int num_dests;
476 	struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
477 
478 	struct page_list *pages;
479 
480 	/*
481 	 * Set this to ensure you are notified when the job has
482 	 * completed.  'context' is for callback to use.
483 	 */
484 	dm_kcopyd_notify_fn fn;
485 	void *context;
486 
487 	/*
488 	 * These fields are only used if the job has been split
489 	 * into more manageable parts.
490 	 */
491 	struct mutex lock;
492 	atomic_t sub_jobs;
493 	sector_t progress;
494 	sector_t write_offset;
495 
496 	struct kcopyd_job *master_job;
497 };
498 
499 static struct kmem_cache *_job_cache;
500 
dm_kcopyd_init(void)501 int __init dm_kcopyd_init(void)
502 {
503 	_job_cache = kmem_cache_create("kcopyd_job",
504 				sizeof(struct kcopyd_job) * (SPLIT_COUNT + 1),
505 				__alignof__(struct kcopyd_job), 0, NULL);
506 	if (!_job_cache)
507 		return -ENOMEM;
508 
509 	zero_page_list.next = &zero_page_list;
510 	zero_page_list.page = ZERO_PAGE(0);
511 
512 	kcopyd_rsm_init();
513 
514 	return 0;
515 }
516 
dm_kcopyd_exit(void)517 void dm_kcopyd_exit(void)
518 {
519 	kmem_cache_destroy(_job_cache);
520 	_job_cache = NULL;
521 	kcopyd_rsm_destroy();
522 }
523 
524 /*
525  * Functions to push and pop a job onto the head of a given job
526  * list.
527  */
pop_io_job(struct list_head * jobs,struct dm_kcopyd_client * kc)528 static struct kcopyd_job *pop_io_job(struct list_head *jobs,
529 				     struct dm_kcopyd_client *kc)
530 {
531 	struct kcopyd_job *job;
532 
533 	/*
534 	 * For I/O jobs, pop any read, any write without sequential write
535 	 * constraint and sequential writes that are at the right position.
536 	 */
537 	list_for_each_entry(job, jobs, list) {
538 		if (job->rw == READ || !test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags)) {
539 			list_del(&job->list);
540 			return job;
541 		}
542 
543 		if (job->write_offset == job->master_job->write_offset) {
544 			job->master_job->write_offset += job->source.count;
545 			list_del(&job->list);
546 			return job;
547 		}
548 	}
549 
550 	return NULL;
551 }
552 
pop(struct list_head * jobs,struct dm_kcopyd_client * kc)553 static struct kcopyd_job *pop(struct list_head *jobs,
554 			      struct dm_kcopyd_client *kc)
555 {
556 	struct kcopyd_job *job = NULL;
557 	unsigned long flags;
558 
559 	spin_lock_irqsave(&kc->job_lock, flags);
560 
561 	if (!list_empty(jobs)) {
562 		if (jobs == &kc->io_jobs)
563 			job = pop_io_job(jobs, kc);
564 		else {
565 			job = list_entry(jobs->next, struct kcopyd_job, list);
566 			list_del(&job->list);
567 		}
568 	}
569 	spin_unlock_irqrestore(&kc->job_lock, flags);
570 
571 	return job;
572 }
573 
push(struct list_head * jobs,struct kcopyd_job * job)574 static void push(struct list_head *jobs, struct kcopyd_job *job)
575 {
576 	unsigned long flags;
577 	struct dm_kcopyd_client *kc = job->kc;
578 
579 	spin_lock_irqsave(&kc->job_lock, flags);
580 	list_add_tail(&job->list, jobs);
581 	spin_unlock_irqrestore(&kc->job_lock, flags);
582 }
583 
584 
push_head(struct list_head * jobs,struct kcopyd_job * job)585 static void push_head(struct list_head *jobs, struct kcopyd_job *job)
586 {
587 	unsigned long flags;
588 	struct dm_kcopyd_client *kc = job->kc;
589 
590 	spin_lock_irqsave(&kc->job_lock, flags);
591 	list_add(&job->list, jobs);
592 	spin_unlock_irqrestore(&kc->job_lock, flags);
593 }
594 
595 /*
596  * These three functions process 1 item from the corresponding
597  * job list.
598  *
599  * They return:
600  * < 0: error
601  *   0: success
602  * > 0: can't process yet.
603  */
run_complete_job(struct kcopyd_job * job)604 static int run_complete_job(struct kcopyd_job *job)
605 {
606 	void *context = job->context;
607 	int read_err = job->read_err;
608 	unsigned long write_err = job->write_err;
609 	dm_kcopyd_notify_fn fn = job->fn;
610 	struct dm_kcopyd_client *kc = job->kc;
611 
612 	if (job->pages && job->pages != &zero_page_list)
613 		kcopyd_put_pages(kc, job->pages);
614 	/*
615 	 * If this is the master job, the sub jobs have already
616 	 * completed so we can free everything.
617 	 */
618 	if (job->master_job == job) {
619 		mutex_destroy(&job->lock);
620 		mempool_free(job, &kc->job_pool);
621 	}
622 	fn(read_err, write_err, context);
623 
624 	if (atomic_dec_and_test(&kc->nr_jobs))
625 		wake_up(&kc->destroyq);
626 
627 	cond_resched();
628 
629 	return 0;
630 }
631 
complete_io(unsigned long error,void * context)632 static void complete_io(unsigned long error, void *context)
633 {
634 	struct kcopyd_job *job = (struct kcopyd_job *) context;
635 	struct dm_kcopyd_client *kc = job->kc;
636 
637 	io_job_finish(kc->throttle);
638 
639 	if (error) {
640 		if (op_is_write(job->rw))
641 			job->write_err |= error;
642 		else
643 			job->read_err = 1;
644 
645 		if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
646 			push(&kc->complete_jobs, job);
647 			wake(kc);
648 			return;
649 		}
650 	}
651 
652 	if (op_is_write(job->rw))
653 		push(&kc->complete_jobs, job);
654 
655 	else {
656 		job->rw = WRITE;
657 		push(&kc->io_jobs, job);
658 	}
659 
660 	wake(kc);
661 }
662 
663 /*
664  * Request io on as many buffer heads as we can currently get for
665  * a particular job.
666  */
run_io_job(struct kcopyd_job * job)667 static int run_io_job(struct kcopyd_job *job)
668 {
669 	int r;
670 	struct dm_io_request io_req = {
671 		.bi_op = job->rw,
672 		.bi_op_flags = 0,
673 		.mem.type = DM_IO_PAGE_LIST,
674 		.mem.ptr.pl = job->pages,
675 		.mem.offset = 0,
676 		.notify.fn = complete_io,
677 		.notify.context = job,
678 		.client = job->kc->io_client,
679 	};
680 
681 	/*
682 	 * If we need to write sequentially and some reads or writes failed,
683 	 * no point in continuing.
684 	 */
685 	if (test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags) &&
686 	    job->master_job->write_err) {
687 		job->write_err = job->master_job->write_err;
688 		return -EIO;
689 	}
690 
691 	io_job_start(job->kc->throttle);
692 
693 	if (job->rw == READ)
694 		r = dm_io(&io_req, 1, &job->source, NULL);
695 	else
696 		r = dm_io(&io_req, job->num_dests, job->dests, NULL);
697 
698 	return r;
699 }
700 
run_pages_job(struct kcopyd_job * job)701 static int run_pages_job(struct kcopyd_job *job)
702 {
703 	int r;
704 	unsigned nr_pages = dm_div_up(job->dests[0].count, PAGE_SIZE >> 9);
705 
706 	r = kcopyd_get_pages(job->kc, nr_pages, &job->pages, job->flags);
707 	if (!r) {
708 		/* this job is ready for io */
709 		push(&job->kc->io_jobs, job);
710 		return 0;
711 	}
712 
713 	if (r == -ENOMEM)
714 		/* can't complete now */
715 		return 1;
716 
717 	return r;
718 }
719 
720 /*
721  * Run through a list for as long as possible.  Returns the count
722  * of successful jobs.
723  */
process_jobs(struct list_head * jobs,struct dm_kcopyd_client * kc,int (* fn)(struct kcopyd_job *))724 static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
725 			int (*fn) (struct kcopyd_job *))
726 {
727 	struct kcopyd_job *job;
728 	int r, count = 0;
729 
730 	while ((job = pop(jobs, kc))) {
731 
732 		r = fn(job);
733 
734 		if (r < 0) {
735 			/* error this rogue job */
736 			if (op_is_write(job->rw))
737 				job->write_err = (unsigned long) -1L;
738 			else
739 				job->read_err = 1;
740 			push(&kc->complete_jobs, job);
741 			wake(kc);
742 			break;
743 		}
744 
745 		if (r > 0) {
746 			/*
747 			 * We couldn't service this job ATM, so
748 			 * push this job back onto the list.
749 			 */
750 			push_head(jobs, job);
751 			break;
752 		}
753 
754 		count++;
755 	}
756 
757 	return count;
758 }
759 
760 /*
761  * kcopyd does this every time it's woken up.
762  */
do_work(struct work_struct * work)763 static void do_work(struct work_struct *work)
764 {
765 	struct dm_kcopyd_client *kc = container_of(work,
766 					struct dm_kcopyd_client, kcopyd_work);
767 	struct blk_plug plug;
768 	unsigned long flags;
769 
770 	/*
771 	 * The order that these are called is *very* important.
772 	 * complete jobs can free some pages for pages jobs.
773 	 * Pages jobs when successful will jump onto the io jobs
774 	 * list.  io jobs call wake when they complete and it all
775 	 * starts again.
776 	 */
777 	spin_lock_irqsave(&kc->job_lock, flags);
778 	list_splice_tail_init(&kc->callback_jobs, &kc->complete_jobs);
779 	spin_unlock_irqrestore(&kc->job_lock, flags);
780 
781 	blk_start_plug(&plug);
782 	process_jobs(&kc->complete_jobs, kc, run_complete_job);
783 	process_jobs(&kc->pages_jobs, kc, run_pages_job);
784 	process_jobs(&kc->io_jobs, kc, run_io_job);
785 	blk_finish_plug(&plug);
786 }
787 
788 /*
789  * If we are copying a small region we just dispatch a single job
790  * to do the copy, otherwise the io has to be split up into many
791  * jobs.
792  */
dispatch_job(struct kcopyd_job * job)793 static void dispatch_job(struct kcopyd_job *job)
794 {
795 	struct dm_kcopyd_client *kc = job->kc;
796 	atomic_inc(&kc->nr_jobs);
797 	if (unlikely(!job->source.count))
798 		push(&kc->callback_jobs, job);
799 	else if (job->pages == &zero_page_list)
800 		push(&kc->io_jobs, job);
801 	else
802 		push(&kc->pages_jobs, job);
803 	wake(kc);
804 }
805 
segment_complete(int read_err,unsigned long write_err,void * context)806 static void segment_complete(int read_err, unsigned long write_err,
807 			     void *context)
808 {
809 	/* FIXME: tidy this function */
810 	sector_t progress = 0;
811 	sector_t count = 0;
812 	struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
813 	struct kcopyd_job *job = sub_job->master_job;
814 	struct dm_kcopyd_client *kc = job->kc;
815 
816 	mutex_lock(&job->lock);
817 
818 	/* update the error */
819 	if (read_err)
820 		job->read_err = 1;
821 
822 	if (write_err)
823 		job->write_err |= write_err;
824 
825 	/*
826 	 * Only dispatch more work if there hasn't been an error.
827 	 */
828 	if ((!job->read_err && !job->write_err) ||
829 	    test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
830 		/* get the next chunk of work */
831 		progress = job->progress;
832 		count = job->source.count - progress;
833 		if (count) {
834 			if (count > kc->sub_job_size)
835 				count = kc->sub_job_size;
836 
837 			job->progress += count;
838 		}
839 	}
840 	mutex_unlock(&job->lock);
841 
842 	if (count) {
843 		int i;
844 
845 		*sub_job = *job;
846 		sub_job->write_offset = progress;
847 		sub_job->source.sector += progress;
848 		sub_job->source.count = count;
849 
850 		for (i = 0; i < job->num_dests; i++) {
851 			sub_job->dests[i].sector += progress;
852 			sub_job->dests[i].count = count;
853 		}
854 
855 		sub_job->fn = segment_complete;
856 		sub_job->context = sub_job;
857 		dispatch_job(sub_job);
858 
859 	} else if (atomic_dec_and_test(&job->sub_jobs)) {
860 
861 		/*
862 		 * Queue the completion callback to the kcopyd thread.
863 		 *
864 		 * Some callers assume that all the completions are called
865 		 * from a single thread and don't race with each other.
866 		 *
867 		 * We must not call the callback directly here because this
868 		 * code may not be executing in the thread.
869 		 */
870 		push(&kc->complete_jobs, job);
871 		wake(kc);
872 	}
873 }
874 
875 /*
876  * Create some sub jobs to share the work between them.
877  */
split_job(struct kcopyd_job * master_job)878 static void split_job(struct kcopyd_job *master_job)
879 {
880 	int i;
881 
882 	atomic_inc(&master_job->kc->nr_jobs);
883 
884 	atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
885 	for (i = 0; i < SPLIT_COUNT; i++) {
886 		master_job[i + 1].master_job = master_job;
887 		segment_complete(0, 0u, &master_job[i + 1]);
888 	}
889 }
890 
dm_kcopyd_copy(struct dm_kcopyd_client * kc,struct dm_io_region * from,unsigned int num_dests,struct dm_io_region * dests,unsigned int flags,dm_kcopyd_notify_fn fn,void * context)891 void dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
892 		    unsigned int num_dests, struct dm_io_region *dests,
893 		    unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
894 {
895 	struct kcopyd_job *job;
896 	int i;
897 
898 	/*
899 	 * Allocate an array of jobs consisting of one master job
900 	 * followed by SPLIT_COUNT sub jobs.
901 	 */
902 	job = mempool_alloc(&kc->job_pool, GFP_NOIO);
903 	mutex_init(&job->lock);
904 
905 	/*
906 	 * set up for the read.
907 	 */
908 	job->kc = kc;
909 	job->flags = flags;
910 	job->read_err = 0;
911 	job->write_err = 0;
912 
913 	job->num_dests = num_dests;
914 	memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
915 
916 	/*
917 	 * If one of the destination is a host-managed zoned block device,
918 	 * we need to write sequentially. If one of the destination is a
919 	 * host-aware device, then leave it to the caller to choose what to do.
920 	 */
921 	if (!test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags)) {
922 		for (i = 0; i < job->num_dests; i++) {
923 			if (bdev_zoned_model(dests[i].bdev) == BLK_ZONED_HM) {
924 				set_bit(DM_KCOPYD_WRITE_SEQ, &job->flags);
925 				break;
926 			}
927 		}
928 	}
929 
930 	/*
931 	 * If we need to write sequentially, errors cannot be ignored.
932 	 */
933 	if (test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags) &&
934 	    test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags))
935 		clear_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags);
936 
937 	if (from) {
938 		job->source = *from;
939 		job->pages = NULL;
940 		job->rw = READ;
941 	} else {
942 		memset(&job->source, 0, sizeof job->source);
943 		job->source.count = job->dests[0].count;
944 		job->pages = &zero_page_list;
945 
946 		/*
947 		 * Use WRITE ZEROES to optimize zeroing if all dests support it.
948 		 */
949 		job->rw = REQ_OP_WRITE_ZEROES;
950 		for (i = 0; i < job->num_dests; i++)
951 			if (!bdev_write_zeroes_sectors(job->dests[i].bdev)) {
952 				job->rw = WRITE;
953 				break;
954 			}
955 	}
956 
957 	job->fn = fn;
958 	job->context = context;
959 	job->master_job = job;
960 	job->write_offset = 0;
961 
962 	if (job->source.count <= kc->sub_job_size)
963 		dispatch_job(job);
964 	else {
965 		job->progress = 0;
966 		split_job(job);
967 	}
968 }
969 EXPORT_SYMBOL(dm_kcopyd_copy);
970 
dm_kcopyd_zero(struct dm_kcopyd_client * kc,unsigned num_dests,struct dm_io_region * dests,unsigned flags,dm_kcopyd_notify_fn fn,void * context)971 void dm_kcopyd_zero(struct dm_kcopyd_client *kc,
972 		    unsigned num_dests, struct dm_io_region *dests,
973 		    unsigned flags, dm_kcopyd_notify_fn fn, void *context)
974 {
975 	dm_kcopyd_copy(kc, NULL, num_dests, dests, flags, fn, context);
976 }
977 EXPORT_SYMBOL(dm_kcopyd_zero);
978 
dm_kcopyd_prepare_callback(struct dm_kcopyd_client * kc,dm_kcopyd_notify_fn fn,void * context)979 void *dm_kcopyd_prepare_callback(struct dm_kcopyd_client *kc,
980 				 dm_kcopyd_notify_fn fn, void *context)
981 {
982 	struct kcopyd_job *job;
983 
984 	job = mempool_alloc(&kc->job_pool, GFP_NOIO);
985 
986 	memset(job, 0, sizeof(struct kcopyd_job));
987 	job->kc = kc;
988 	job->fn = fn;
989 	job->context = context;
990 	job->master_job = job;
991 
992 	atomic_inc(&kc->nr_jobs);
993 
994 	return job;
995 }
996 EXPORT_SYMBOL(dm_kcopyd_prepare_callback);
997 
dm_kcopyd_do_callback(void * j,int read_err,unsigned long write_err)998 void dm_kcopyd_do_callback(void *j, int read_err, unsigned long write_err)
999 {
1000 	struct kcopyd_job *job = j;
1001 	struct dm_kcopyd_client *kc = job->kc;
1002 
1003 	job->read_err = read_err;
1004 	job->write_err = write_err;
1005 
1006 	push(&kc->callback_jobs, job);
1007 	wake(kc);
1008 }
1009 EXPORT_SYMBOL(dm_kcopyd_do_callback);
1010 
1011 /*
1012  * Cancels a kcopyd job, eg. someone might be deactivating a
1013  * mirror.
1014  */
1015 #if 0
1016 int kcopyd_cancel(struct kcopyd_job *job, int block)
1017 {
1018 	/* FIXME: finish */
1019 	return -1;
1020 }
1021 #endif  /*  0  */
1022 
1023 /*-----------------------------------------------------------------
1024  * Client setup
1025  *---------------------------------------------------------------*/
dm_kcopyd_client_create(struct dm_kcopyd_throttle * throttle)1026 struct dm_kcopyd_client *dm_kcopyd_client_create(struct dm_kcopyd_throttle *throttle)
1027 {
1028 	int r;
1029 	unsigned reserve_pages;
1030 	struct dm_kcopyd_client *kc;
1031 
1032 	kc = kzalloc(sizeof(*kc), GFP_KERNEL);
1033 	if (!kc)
1034 		return ERR_PTR(-ENOMEM);
1035 
1036 	spin_lock_init(&kc->job_lock);
1037 	INIT_LIST_HEAD(&kc->callback_jobs);
1038 	INIT_LIST_HEAD(&kc->complete_jobs);
1039 	INIT_LIST_HEAD(&kc->io_jobs);
1040 	INIT_LIST_HEAD(&kc->pages_jobs);
1041 	kc->throttle = throttle;
1042 
1043 	r = mempool_init_slab_pool(&kc->job_pool, MIN_JOBS, _job_cache);
1044 	if (r)
1045 		goto bad_slab;
1046 
1047 	INIT_WORK(&kc->kcopyd_work, do_work);
1048 	kc->kcopyd_wq = alloc_workqueue("kcopyd", WQ_MEM_RECLAIM, 0);
1049 	if (!kc->kcopyd_wq) {
1050 		r = -ENOMEM;
1051 		goto bad_workqueue;
1052 	}
1053 
1054 	kc->sub_job_size = dm_get_kcopyd_subjob_size();
1055 	reserve_pages = DIV_ROUND_UP(kc->sub_job_size << SECTOR_SHIFT, PAGE_SIZE);
1056 
1057 	kc->pages = NULL;
1058 	kc->nr_reserved_pages = kc->nr_free_pages = 0;
1059 	r = client_reserve_pages(kc, reserve_pages);
1060 	if (r)
1061 		goto bad_client_pages;
1062 
1063 	kc->io_client = dm_io_client_create();
1064 	if (IS_ERR(kc->io_client)) {
1065 		r = PTR_ERR(kc->io_client);
1066 		goto bad_io_client;
1067 	}
1068 
1069 	init_waitqueue_head(&kc->destroyq);
1070 	atomic_set(&kc->nr_jobs, 0);
1071 
1072 	return kc;
1073 
1074 bad_io_client:
1075 	client_free_pages(kc);
1076 bad_client_pages:
1077 	destroy_workqueue(kc->kcopyd_wq);
1078 bad_workqueue:
1079 	mempool_exit(&kc->job_pool);
1080 bad_slab:
1081 	kfree(kc);
1082 
1083 	return ERR_PTR(r);
1084 }
1085 EXPORT_SYMBOL(dm_kcopyd_client_create);
1086 
dm_kcopyd_client_destroy(struct dm_kcopyd_client * kc)1087 void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
1088 {
1089 	/* Wait for completion of all jobs submitted by this client. */
1090 	wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
1091 
1092 	BUG_ON(!list_empty(&kc->callback_jobs));
1093 	BUG_ON(!list_empty(&kc->complete_jobs));
1094 	BUG_ON(!list_empty(&kc->io_jobs));
1095 	BUG_ON(!list_empty(&kc->pages_jobs));
1096 	destroy_workqueue(kc->kcopyd_wq);
1097 	dm_io_client_destroy(kc->io_client);
1098 	client_free_pages(kc);
1099 	mempool_exit(&kc->job_pool);
1100 	kfree(kc);
1101 }
1102 EXPORT_SYMBOL(dm_kcopyd_client_destroy);
1103