2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002 Sistina Software (UK) Limited.
|
2007-05-09 11:33:02 +02:00
|
|
|
* Copyright (C) 2006 Red Hat GmbH
|
2005-04-17 00:20:36 +02:00
|
|
|
*
|
|
|
|
* This file is released under the GPL.
|
|
|
|
*
|
|
|
|
* Kcopyd provides a simple interface for copying an area of one
|
|
|
|
* block-device to one or more other block-devices, with an asynchronous
|
|
|
|
* completion notification.
|
|
|
|
*/
|
|
|
|
|
2008-04-24 22:43:19 +02:00
|
|
|
#include <linux/types.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <asm/atomic.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/mempool.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/workqueue.h>
|
2006-03-27 11:18:20 +02:00
|
|
|
#include <linux/mutex.h>
|
2008-04-24 23:02:01 +02:00
|
|
|
#include <linux/dm-kcopyd.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-04-24 22:43:17 +02:00
|
|
|
#include "dm.h"
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------
|
|
|
|
* Each kcopyd client has its own little pool of preallocated
|
|
|
|
* pages for kcopyd io.
|
|
|
|
*---------------------------------------------------------------*/
|
2008-04-24 22:43:19 +02:00
|
|
|
struct dm_kcopyd_client {
|
2005-04-17 00:20:36 +02:00
|
|
|
spinlock_t lock;
|
|
|
|
struct page_list *pages;
|
|
|
|
unsigned int nr_pages;
|
|
|
|
unsigned int nr_free_pages;
|
2006-03-27 11:17:50 +02:00
|
|
|
|
2007-05-09 11:33:02 +02:00
|
|
|
struct dm_io_client *io_client;
|
|
|
|
|
2006-03-27 11:17:50 +02:00
|
|
|
wait_queue_head_t destroyq;
|
|
|
|
atomic_t nr_jobs;
|
2008-04-24 22:43:44 +02:00
|
|
|
|
2008-04-24 22:43:46 +02:00
|
|
|
mempool_t *job_pool;
|
|
|
|
|
2008-04-24 22:43:44 +02:00
|
|
|
struct workqueue_struct *kcopyd_wq;
|
|
|
|
struct work_struct kcopyd_work;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We maintain three lists of jobs:
|
|
|
|
*
|
|
|
|
* i) jobs waiting for pages
|
|
|
|
* ii) jobs that have pages, and are waiting for the io to be issued.
|
|
|
|
* iii) jobs that have completed.
|
|
|
|
*
|
|
|
|
* All three of these are protected by job_lock.
|
|
|
|
*/
|
|
|
|
spinlock_t job_lock;
|
|
|
|
struct list_head complete_jobs;
|
|
|
|
struct list_head io_jobs;
|
|
|
|
struct list_head pages_jobs;
|
2005-04-17 00:20:36 +02:00
|
|
|
};
|
|
|
|
|
2008-04-24 22:43:44 +02:00
|
|
|
static void wake(struct dm_kcopyd_client *kc)
|
|
|
|
{
|
|
|
|
queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
|
|
|
|
}
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
static struct page_list *alloc_pl(void)
|
|
|
|
{
|
|
|
|
struct page_list *pl;
|
|
|
|
|
|
|
|
pl = kmalloc(sizeof(*pl), GFP_KERNEL);
|
|
|
|
if (!pl)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pl->page = alloc_page(GFP_KERNEL);
|
|
|
|
if (!pl->page) {
|
|
|
|
kfree(pl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_pl(struct page_list *pl)
|
|
|
|
{
|
|
|
|
__free_page(pl->page);
|
|
|
|
kfree(pl);
|
|
|
|
}
|
|
|
|
|
2008-04-24 22:43:19 +02:00
|
|
|
static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
|
2005-04-17 00:20:36 +02:00
|
|
|
unsigned int nr, struct page_list **pages)
|
|
|
|
{
|
|
|
|
struct page_list *pl;
|
|
|
|
|
|
|
|
spin_lock(&kc->lock);
|
|
|
|
if (kc->nr_free_pages < nr) {
|
|
|
|
spin_unlock(&kc->lock);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
kc->nr_free_pages -= nr;
|
|
|
|
for (*pages = pl = kc->pages; --nr; pl = pl->next)
|
|
|
|
;
|
|
|
|
|
|
|
|
kc->pages = pl->next;
|
|
|
|
pl->next = NULL;
|
|
|
|
|
|
|
|
spin_unlock(&kc->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-24 22:43:19 +02:00
|
|
|
static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct page_list *cursor;
|
|
|
|
|
|
|
|
spin_lock(&kc->lock);
|
|
|
|
for (cursor = pl; cursor->next; cursor = cursor->next)
|
|
|
|
kc->nr_free_pages++;
|
|
|
|
|
|
|
|
kc->nr_free_pages++;
|
|
|
|
cursor->next = kc->pages;
|
|
|
|
kc->pages = pl;
|
|
|
|
spin_unlock(&kc->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These three functions resize the page pool.
|
|
|
|
*/
|
|
|
|
static void drop_pages(struct page_list *pl)
|
|
|
|
{
|
|
|
|
struct page_list *next;
|
|
|
|
|
|
|
|
while (pl) {
|
|
|
|
next = pl->next;
|
|
|
|
free_pl(pl);
|
|
|
|
pl = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-24 22:43:19 +02:00
|
|
|
static int client_alloc_pages(struct dm_kcopyd_client *kc, unsigned int nr)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
struct page_list *pl = NULL, *next;
|
|
|
|
|
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
next = alloc_pl();
|
|
|
|
if (!next) {
|
|
|
|
if (pl)
|
|
|
|
drop_pages(pl);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
next->next = pl;
|
|
|
|
pl = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
kcopyd_put_pages(kc, pl);
|
|
|
|
kc->nr_pages += nr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-24 22:43:19 +02:00
|
|
|
static void client_free_pages(struct dm_kcopyd_client *kc)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
BUG_ON(kc->nr_free_pages != kc->nr_pages);
|
|
|
|
drop_pages(kc->pages);
|
|
|
|
kc->pages = NULL;
|
|
|
|
kc->nr_free_pages = kc->nr_pages = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------
|
|
|
|
* kcopyd_jobs need to be allocated by the *clients* of kcopyd,
|
|
|
|
* for this reason we use a mempool to prevent the client from
|
|
|
|
* ever having to do io (which could cause a deadlock).
|
|
|
|
*---------------------------------------------------------------*/
|
|
|
|
struct kcopyd_job {
|
2008-04-24 22:43:19 +02:00
|
|
|
struct dm_kcopyd_client *kc;
|
2005-04-17 00:20:36 +02:00
|
|
|
struct list_head list;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Error state of the job.
|
|
|
|
*/
|
|
|
|
int read_err;
|
2008-03-28 22:16:10 +01:00
|
|
|
unsigned long write_err;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Either READ or WRITE
|
|
|
|
*/
|
|
|
|
int rw;
|
2008-04-24 22:43:17 +02:00
|
|
|
struct dm_io_region source;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The destinations for the transfer.
|
|
|
|
*/
|
|
|
|
unsigned int num_dests;
|
2008-04-24 22:43:19 +02:00
|
|
|
struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
sector_t offset;
|
|
|
|
unsigned int nr_pages;
|
|
|
|
struct page_list *pages;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set this to ensure you are notified when the job has
|
|
|
|
* completed. 'context' is for callback to use.
|
|
|
|
*/
|
2008-04-24 22:43:19 +02:00
|
|
|
dm_kcopyd_notify_fn fn;
|
2005-04-17 00:20:36 +02:00
|
|
|
void *context;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These fields are only used if the job has been split
|
|
|
|
* into more manageable parts.
|
|
|
|
*/
|
2007-10-19 23:38:52 +02:00
|
|
|
struct mutex lock;
|
2005-04-17 00:20:36 +02:00
|
|
|
atomic_t sub_jobs;
|
|
|
|
sector_t progress;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* FIXME: this should scale with the number of pages */
|
|
|
|
#define MIN_JOBS 512
|
|
|
|
|
2006-12-07 05:33:20 +01:00
|
|
|
static struct kmem_cache *_job_cache;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-04-24 22:43:49 +02:00
|
|
|
int __init dm_kcopyd_init(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2007-07-12 18:26:32 +02:00
|
|
|
_job_cache = KMEM_CACHE(kcopyd_job, 0);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!_job_cache)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-24 22:43:49 +02:00
|
|
|
void dm_kcopyd_exit(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
kmem_cache_destroy(_job_cache);
|
|
|
|
_job_cache = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions to push and pop a job onto the head of a given job
|
|
|
|
* list.
|
|
|
|
*/
|
2008-04-24 22:43:44 +02:00
|
|
|
static struct kcopyd_job *pop(struct list_head *jobs,
|
|
|
|
struct dm_kcopyd_client *kc)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct kcopyd_job *job = NULL;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2008-04-24 22:43:44 +02:00
|
|
|
spin_lock_irqsave(&kc->job_lock, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (!list_empty(jobs)) {
|
|
|
|
job = list_entry(jobs->next, struct kcopyd_job, list);
|
|
|
|
list_del(&job->list);
|
|
|
|
}
|
2008-04-24 22:43:44 +02:00
|
|
|
spin_unlock_irqrestore(&kc->job_lock, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
2007-07-12 18:26:32 +02:00
|
|
|
static void push(struct list_head *jobs, struct kcopyd_job *job)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2008-04-24 22:43:44 +02:00
|
|
|
struct dm_kcopyd_client *kc = job->kc;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-04-24 22:43:44 +02:00
|
|
|
spin_lock_irqsave(&kc->job_lock, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
list_add_tail(&job->list, jobs);
|
2008-04-24 22:43:44 +02:00
|
|
|
spin_unlock_irqrestore(&kc->job_lock, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
dm kcopyd: avoid queue shuffle
Write throughput to LVM snapshot origin volume is an order
of magnitude slower than those to LV without snapshots or
snapshot target volumes, especially in the case of sequential
writes with O_SYNC on.
The following patch originally written by Kevin Jamieson and
Jan Blunck and slightly modified for the current RCs by myself
tries to improve the performance by modifying the behaviour
of kcopyd, so that it pushes back an I/O job to the head of
the job queue instead of the tail as process_jobs() currently
does when it has to wait for free pages. This way, write
requests aren't shuffled to cause extra seeks.
I tested the patch against 2.6.27-rc5 and got the following results.
The test is a dd command writing to snapshot origin followed by fsync
to the file just created/updated. A couple of filesystem benchmarks
gave me similar results in case of sequential writes, while random
writes didn't suffer much.
dd if=/dev/zero of=<somewhere on snapshot origin> bs=4096 count=...
[conv=notrunc when updating]
1) linux 2.6.27-rc5 without the patch, write to snapshot origin,
average throughput (MB/s)
10M 100M 1000M
create,dd 511.46 610.72 11.81
create,dd+fsync 7.10 6.77 8.13
update,dd 431.63 917.41 12.75
update,dd+fsync 7.79 7.43 8.12
compared with write throughput to LV without any snapshots,
all dd+fsync and 1000 MiB writes perform very poorly.
10M 100M 1000M
create,dd 555.03 608.98 123.29
create,dd+fsync 114.27 72.78 76.65
update,dd 152.34 1267.27 124.04
update,dd+fsync 130.56 77.81 77.84
2) linux 2.6.27-rc5 with the patch, write to snapshot origin,
average throughput (MB/s)
10M 100M 1000M
create,dd 537.06 589.44 46.21
create,dd+fsync 31.63 29.19 29.23
update,dd 487.59 897.65 37.76
update,dd+fsync 34.12 30.07 26.85
Although still not on par with plain LV performance -
cannot be avoided because it's copy on write anyway -
this simple patch successfully improves throughtput
of dd+fsync while not affecting the rest.
Signed-off-by: Jan Blunck <jblunck@suse.de>
Signed-off-by: Kazuo Ito <ito.kazuo@oss.ntt.co.jp>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
Cc: stable@kernel.org
2008-10-21 18:44:50 +02:00
|
|
|
|
|
|
|
static void push_head(struct list_head *jobs, struct kcopyd_job *job)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct dm_kcopyd_client *kc = job->kc;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&kc->job_lock, flags);
|
|
|
|
list_add(&job->list, jobs);
|
|
|
|
spin_unlock_irqrestore(&kc->job_lock, flags);
|
|
|
|
}
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* These three functions process 1 item from the corresponding
|
|
|
|
* job list.
|
|
|
|
*
|
|
|
|
* They return:
|
|
|
|
* < 0: error
|
|
|
|
* 0: success
|
|
|
|
* > 0: can't process yet.
|
|
|
|
*/
|
|
|
|
static int run_complete_job(struct kcopyd_job *job)
|
|
|
|
{
|
|
|
|
void *context = job->context;
|
|
|
|
int read_err = job->read_err;
|
2008-03-28 22:16:10 +01:00
|
|
|
unsigned long write_err = job->write_err;
|
2008-04-24 22:43:19 +02:00
|
|
|
dm_kcopyd_notify_fn fn = job->fn;
|
|
|
|
struct dm_kcopyd_client *kc = job->kc;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-03-27 11:17:50 +02:00
|
|
|
kcopyd_put_pages(kc, job->pages);
|
2008-04-24 22:43:46 +02:00
|
|
|
mempool_free(job, kc->job_pool);
|
2005-04-17 00:20:36 +02:00
|
|
|
fn(read_err, write_err, context);
|
2006-03-27 11:17:50 +02:00
|
|
|
|
|
|
|
if (atomic_dec_and_test(&kc->nr_jobs))
|
|
|
|
wake_up(&kc->destroyq);
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void complete_io(unsigned long error, void *context)
|
|
|
|
{
|
|
|
|
struct kcopyd_job *job = (struct kcopyd_job *) context;
|
2008-04-24 22:43:44 +02:00
|
|
|
struct dm_kcopyd_client *kc = job->kc;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
if (job->rw == WRITE)
|
2006-06-26 09:27:30 +02:00
|
|
|
job->write_err |= error;
|
2005-04-17 00:20:36 +02:00
|
|
|
else
|
|
|
|
job->read_err = 1;
|
|
|
|
|
2008-04-24 22:43:19 +02:00
|
|
|
if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
|
2008-04-24 22:43:44 +02:00
|
|
|
push(&kc->complete_jobs, job);
|
|
|
|
wake(kc);
|
2005-04-17 00:20:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job->rw == WRITE)
|
2008-04-24 22:43:44 +02:00
|
|
|
push(&kc->complete_jobs, job);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
else {
|
|
|
|
job->rw = WRITE;
|
2008-04-24 22:43:44 +02:00
|
|
|
push(&kc->io_jobs, job);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 22:43:44 +02:00
|
|
|
wake(kc);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request io on as many buffer heads as we can currently get for
|
|
|
|
* a particular job.
|
|
|
|
*/
|
|
|
|
static int run_io_job(struct kcopyd_job *job)
|
|
|
|
{
|
|
|
|
int r;
|
2007-05-09 11:33:02 +02:00
|
|
|
struct dm_io_request io_req = {
|
dm: unplug queues in threads
Remove an avoidable 3ms delay on some dm-raid1 and kcopyd I/O.
It is specified that any submitted bio without BIO_RW_SYNC flag may plug the
queue (i.e. block the requests from being dispatched to the physical device).
The queue is unplugged when the caller calls blk_unplug() function. Usually, the
sequence is that someone calls submit_bh to submit IO on a buffer. The IO plugs
the queue and waits (to be possibly joined with other adjacent bios). Then, when
the caller calls wait_on_buffer(), it unplugs the queue and submits the IOs to
the disk.
This was happenning:
When doing O_SYNC writes, function fsync_buffers_list() submits a list of
bios to dm_raid1, the bios are added to dm_raid1 write queue and kmirrord is
woken up.
fsync_buffers_list() calls wait_on_buffer(). That unplugs the queue, but
there are no bios on the device queue as they are still in the dm_raid1 queue.
wait_on_buffer() starts waiting until the IO is finished.
kmirrord is scheduled, kmirrord takes bios and submits them to the devices.
The submitted bio plugs the harddisk queue but there is no one to unplug it.
(The process that called wait_on_buffer() is already sleeping.)
So there is a 3ms timeout, after which the queues on the harddisks are
unplugged and requests are processed.
This 3ms timeout meant that in certain workloads (e.g. O_SYNC, 8kb writes),
dm-raid1 is 10 times slower than md raid1.
Every time we submit something asynchronously via dm_io, we must unplug the
queue actually to send the request to the device.
This patch adds an unplug call to kmirrord - while processing requests, it keeps
the queue plugged (so that adjacent bios can be merged); when it finishes
processing all the bios, it unplugs the queue to submit the bios.
It also fixes kcopyd which has the same potential problem. All kcopyd requests
are submitted with BIO_RW_SYNC.
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
Acked-by: Jens Axboe <jens.axboe@oracle.com>
2008-04-24 23:10:47 +02:00
|
|
|
.bi_rw = job->rw | (1 << BIO_RW_SYNC),
|
2007-05-09 11:33:02 +02:00
|
|
|
.mem.type = DM_IO_PAGE_LIST,
|
|
|
|
.mem.ptr.pl = job->pages,
|
|
|
|
.mem.offset = job->offset,
|
|
|
|
.notify.fn = complete_io,
|
|
|
|
.notify.context = job,
|
|
|
|
.client = job->kc->io_client,
|
|
|
|
};
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (job->rw == READ)
|
2007-05-09 11:33:02 +02:00
|
|
|
r = dm_io(&io_req, 1, &job->source, NULL);
|
2005-04-17 00:20:36 +02:00
|
|
|
else
|
2007-05-09 11:33:02 +02:00
|
|
|
r = dm_io(&io_req, job->num_dests, job->dests, NULL);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_pages_job(struct kcopyd_job *job)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
job->nr_pages = dm_div_up(job->dests[0].count + job->offset,
|
|
|
|
PAGE_SIZE >> 9);
|
|
|
|
r = kcopyd_get_pages(job->kc, job->nr_pages, &job->pages);
|
|
|
|
if (!r) {
|
|
|
|
/* this job is ready for io */
|
2008-04-24 22:43:44 +02:00
|
|
|
push(&job->kc->io_jobs, job);
|
2005-04-17 00:20:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == -ENOMEM)
|
|
|
|
/* can't complete now */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run through a list for as long as possible. Returns the count
|
|
|
|
* of successful jobs.
|
|
|
|
*/
|
2008-04-24 22:43:44 +02:00
|
|
|
static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
|
|
|
|
int (*fn) (struct kcopyd_job *))
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct kcopyd_job *job;
|
|
|
|
int r, count = 0;
|
|
|
|
|
2008-04-24 22:43:44 +02:00
|
|
|
while ((job = pop(jobs, kc))) {
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
r = fn(job);
|
|
|
|
|
|
|
|
if (r < 0) {
|
|
|
|
/* error this rogue job */
|
|
|
|
if (job->rw == WRITE)
|
2008-03-28 22:16:10 +01:00
|
|
|
job->write_err = (unsigned long) -1L;
|
2005-04-17 00:20:36 +02:00
|
|
|
else
|
|
|
|
job->read_err = 1;
|
2008-04-24 22:43:44 +02:00
|
|
|
push(&kc->complete_jobs, job);
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r > 0) {
|
|
|
|
/*
|
|
|
|
* We couldn't service this job ATM, so
|
|
|
|
* push this job back onto the list.
|
|
|
|
*/
|
dm kcopyd: avoid queue shuffle
Write throughput to LVM snapshot origin volume is an order
of magnitude slower than those to LV without snapshots or
snapshot target volumes, especially in the case of sequential
writes with O_SYNC on.
The following patch originally written by Kevin Jamieson and
Jan Blunck and slightly modified for the current RCs by myself
tries to improve the performance by modifying the behaviour
of kcopyd, so that it pushes back an I/O job to the head of
the job queue instead of the tail as process_jobs() currently
does when it has to wait for free pages. This way, write
requests aren't shuffled to cause extra seeks.
I tested the patch against 2.6.27-rc5 and got the following results.
The test is a dd command writing to snapshot origin followed by fsync
to the file just created/updated. A couple of filesystem benchmarks
gave me similar results in case of sequential writes, while random
writes didn't suffer much.
dd if=/dev/zero of=<somewhere on snapshot origin> bs=4096 count=...
[conv=notrunc when updating]
1) linux 2.6.27-rc5 without the patch, write to snapshot origin,
average throughput (MB/s)
10M 100M 1000M
create,dd 511.46 610.72 11.81
create,dd+fsync 7.10 6.77 8.13
update,dd 431.63 917.41 12.75
update,dd+fsync 7.79 7.43 8.12
compared with write throughput to LV without any snapshots,
all dd+fsync and 1000 MiB writes perform very poorly.
10M 100M 1000M
create,dd 555.03 608.98 123.29
create,dd+fsync 114.27 72.78 76.65
update,dd 152.34 1267.27 124.04
update,dd+fsync 130.56 77.81 77.84
2) linux 2.6.27-rc5 with the patch, write to snapshot origin,
average throughput (MB/s)
10M 100M 1000M
create,dd 537.06 589.44 46.21
create,dd+fsync 31.63 29.19 29.23
update,dd 487.59 897.65 37.76
update,dd+fsync 34.12 30.07 26.85
Although still not on par with plain LV performance -
cannot be avoided because it's copy on write anyway -
this simple patch successfully improves throughtput
of dd+fsync while not affecting the rest.
Signed-off-by: Jan Blunck <jblunck@suse.de>
Signed-off-by: Kazuo Ito <ito.kazuo@oss.ntt.co.jp>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
Cc: stable@kernel.org
2008-10-21 18:44:50 +02:00
|
|
|
push_head(jobs, job);
|
2005-04-17 00:20:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* kcopyd does this every time it's woken up.
|
|
|
|
*/
|
2008-04-24 22:43:44 +02:00
|
|
|
static void do_work(struct work_struct *work)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-04-24 22:43:44 +02:00
|
|
|
struct dm_kcopyd_client *kc = container_of(work,
|
|
|
|
struct dm_kcopyd_client, kcopyd_work);
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* The order that these are called is *very* important.
|
|
|
|
* complete jobs can free some pages for pages jobs.
|
|
|
|
* Pages jobs when successful will jump onto the io jobs
|
|
|
|
* list. io jobs call wake when they complete and it all
|
|
|
|
* starts again.
|
|
|
|
*/
|
2008-04-24 22:43:44 +02:00
|
|
|
process_jobs(&kc->complete_jobs, kc, run_complete_job);
|
|
|
|
process_jobs(&kc->pages_jobs, kc, run_pages_job);
|
|
|
|
process_jobs(&kc->io_jobs, kc, run_io_job);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are copying a small region we just dispatch a single job
|
|
|
|
* to do the copy, otherwise the io has to be split up into many
|
|
|
|
* jobs.
|
|
|
|
*/
|
|
|
|
static void dispatch_job(struct kcopyd_job *job)
|
|
|
|
{
|
2008-04-24 22:43:44 +02:00
|
|
|
struct dm_kcopyd_client *kc = job->kc;
|
|
|
|
atomic_inc(&kc->nr_jobs);
|
|
|
|
push(&kc->pages_jobs, job);
|
|
|
|
wake(kc);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SUB_JOB_SIZE 128
|
2008-03-28 22:16:10 +01:00
|
|
|
static void segment_complete(int read_err, unsigned long write_err,
|
|
|
|
void *context)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
/* FIXME: tidy this function */
|
|
|
|
sector_t progress = 0;
|
|
|
|
sector_t count = 0;
|
|
|
|
struct kcopyd_job *job = (struct kcopyd_job *) context;
|
|
|
|
|
2007-10-19 23:38:52 +02:00
|
|
|
mutex_lock(&job->lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* update the error */
|
|
|
|
if (read_err)
|
|
|
|
job->read_err = 1;
|
|
|
|
|
|
|
|
if (write_err)
|
2006-06-26 09:27:30 +02:00
|
|
|
job->write_err |= write_err;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only dispatch more work if there hasn't been an error.
|
|
|
|
*/
|
|
|
|
if ((!job->read_err && !job->write_err) ||
|
2008-04-24 22:43:19 +02:00
|
|
|
test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
|
2005-04-17 00:20:36 +02:00
|
|
|
/* get the next chunk of work */
|
|
|
|
progress = job->progress;
|
|
|
|
count = job->source.count - progress;
|
|
|
|
if (count) {
|
|
|
|
if (count > SUB_JOB_SIZE)
|
|
|
|
count = SUB_JOB_SIZE;
|
|
|
|
|
|
|
|
job->progress += count;
|
|
|
|
}
|
|
|
|
}
|
2007-10-19 23:38:52 +02:00
|
|
|
mutex_unlock(&job->lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (count) {
|
|
|
|
int i;
|
2008-04-24 22:43:46 +02:00
|
|
|
struct kcopyd_job *sub_job = mempool_alloc(job->kc->job_pool,
|
|
|
|
GFP_NOIO);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
*sub_job = *job;
|
|
|
|
sub_job->source.sector += progress;
|
|
|
|
sub_job->source.count = count;
|
|
|
|
|
|
|
|
for (i = 0; i < job->num_dests; i++) {
|
|
|
|
sub_job->dests[i].sector += progress;
|
|
|
|
sub_job->dests[i].count = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_job->fn = segment_complete;
|
|
|
|
sub_job->context = job;
|
|
|
|
dispatch_job(sub_job);
|
|
|
|
|
|
|
|
} else if (atomic_dec_and_test(&job->sub_jobs)) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To avoid a race we must keep the job around
|
|
|
|
* until after the notify function has completed.
|
|
|
|
* Otherwise the client may try and stop the job
|
|
|
|
* after we've completed.
|
|
|
|
*/
|
|
|
|
job->fn(read_err, write_err, job->context);
|
2008-04-24 22:43:46 +02:00
|
|
|
mempool_free(job, job->kc->job_pool);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create some little jobs that will do the move between
|
|
|
|
* them.
|
|
|
|
*/
|
|
|
|
#define SPLIT_COUNT 8
|
|
|
|
static void split_job(struct kcopyd_job *job)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
atomic_set(&job->sub_jobs, SPLIT_COUNT);
|
|
|
|
for (i = 0; i < SPLIT_COUNT; i++)
|
|
|
|
segment_complete(0, 0u, job);
|
|
|
|
}
|
|
|
|
|
2008-04-24 22:43:19 +02:00
|
|
|
int 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)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct kcopyd_job *job;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a new job.
|
|
|
|
*/
|
2008-04-24 22:43:46 +02:00
|
|
|
job = mempool_alloc(kc->job_pool, GFP_NOIO);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* set up for the read.
|
|
|
|
*/
|
|
|
|
job->kc = kc;
|
|
|
|
job->flags = flags;
|
|
|
|
job->read_err = 0;
|
|
|
|
job->write_err = 0;
|
|
|
|
job->rw = READ;
|
|
|
|
|
|
|
|
job->source = *from;
|
|
|
|
|
|
|
|
job->num_dests = num_dests;
|
|
|
|
memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
|
|
|
|
|
|
|
|
job->offset = 0;
|
|
|
|
job->nr_pages = 0;
|
|
|
|
job->pages = NULL;
|
|
|
|
|
|
|
|
job->fn = fn;
|
|
|
|
job->context = context;
|
|
|
|
|
|
|
|
if (job->source.count < SUB_JOB_SIZE)
|
|
|
|
dispatch_job(job);
|
|
|
|
|
|
|
|
else {
|
2007-10-19 23:38:52 +02:00
|
|
|
mutex_init(&job->lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
job->progress = 0;
|
|
|
|
split_job(job);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-04-24 22:43:19 +02:00
|
|
|
EXPORT_SYMBOL(dm_kcopyd_copy);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Cancels a kcopyd job, eg. someone might be deactivating a
|
|
|
|
* mirror.
|
|
|
|
*/
|
2006-01-06 09:20:08 +01:00
|
|
|
#if 0
|
2005-04-17 00:20:36 +02:00
|
|
|
int kcopyd_cancel(struct kcopyd_job *job, int block)
|
|
|
|
{
|
|
|
|
/* FIXME: finish */
|
|
|
|
return -1;
|
|
|
|
}
|
2006-01-06 09:20:08 +01:00
|
|
|
#endif /* 0 */
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------
|
2008-04-24 22:43:49 +02:00
|
|
|
* Client setup
|
2005-04-17 00:20:36 +02:00
|
|
|
*---------------------------------------------------------------*/
|
2008-04-24 22:43:19 +02:00
|
|
|
int dm_kcopyd_client_create(unsigned int nr_pages,
|
|
|
|
struct dm_kcopyd_client **result)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2008-04-24 22:43:49 +02:00
|
|
|
int r = -ENOMEM;
|
2008-04-24 22:43:19 +02:00
|
|
|
struct dm_kcopyd_client *kc;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
kc = kmalloc(sizeof(*kc), GFP_KERNEL);
|
2008-04-24 22:43:49 +02:00
|
|
|
if (!kc)
|
|
|
|
return -ENOMEM;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
spin_lock_init(&kc->lock);
|
2008-04-24 22:43:44 +02:00
|
|
|
spin_lock_init(&kc->job_lock);
|
|
|
|
INIT_LIST_HEAD(&kc->complete_jobs);
|
|
|
|
INIT_LIST_HEAD(&kc->io_jobs);
|
|
|
|
INIT_LIST_HEAD(&kc->pages_jobs);
|
|
|
|
|
2008-04-24 22:43:46 +02:00
|
|
|
kc->job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
|
2008-04-24 22:43:49 +02:00
|
|
|
if (!kc->job_pool)
|
|
|
|
goto bad_slab;
|
2008-04-24 22:43:46 +02:00
|
|
|
|
2008-04-24 22:43:44 +02:00
|
|
|
INIT_WORK(&kc->kcopyd_work, do_work);
|
|
|
|
kc->kcopyd_wq = create_singlethread_workqueue("kcopyd");
|
2008-04-24 22:43:49 +02:00
|
|
|
if (!kc->kcopyd_wq)
|
|
|
|
goto bad_workqueue;
|
2008-04-24 22:43:44 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
kc->pages = NULL;
|
|
|
|
kc->nr_pages = kc->nr_free_pages = 0;
|
|
|
|
r = client_alloc_pages(kc, nr_pages);
|
2008-04-24 22:43:49 +02:00
|
|
|
if (r)
|
|
|
|
goto bad_client_pages;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-05-09 11:33:02 +02:00
|
|
|
kc->io_client = dm_io_client_create(nr_pages);
|
|
|
|
if (IS_ERR(kc->io_client)) {
|
|
|
|
r = PTR_ERR(kc->io_client);
|
2008-04-24 22:43:49 +02:00
|
|
|
goto bad_io_client;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2006-03-27 11:17:50 +02:00
|
|
|
init_waitqueue_head(&kc->destroyq);
|
|
|
|
atomic_set(&kc->nr_jobs, 0);
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
*result = kc;
|
|
|
|
return 0;
|
2008-04-24 22:43:49 +02:00
|
|
|
|
|
|
|
bad_io_client:
|
|
|
|
client_free_pages(kc);
|
|
|
|
bad_client_pages:
|
|
|
|
destroy_workqueue(kc->kcopyd_wq);
|
|
|
|
bad_workqueue:
|
|
|
|
mempool_destroy(kc->job_pool);
|
|
|
|
bad_slab:
|
|
|
|
kfree(kc);
|
|
|
|
|
|
|
|
return r;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2008-04-24 22:43:19 +02:00
|
|
|
EXPORT_SYMBOL(dm_kcopyd_client_create);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-04-24 22:43:19 +02:00
|
|
|
void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-03-27 11:17:50 +02:00
|
|
|
/* Wait for completion of all jobs submitted by this client. */
|
|
|
|
wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
|
|
|
|
|
2008-04-24 22:43:44 +02:00
|
|
|
BUG_ON(!list_empty(&kc->complete_jobs));
|
|
|
|
BUG_ON(!list_empty(&kc->io_jobs));
|
|
|
|
BUG_ON(!list_empty(&kc->pages_jobs));
|
|
|
|
destroy_workqueue(kc->kcopyd_wq);
|
2007-05-09 11:33:02 +02:00
|
|
|
dm_io_client_destroy(kc->io_client);
|
2005-04-17 00:20:36 +02:00
|
|
|
client_free_pages(kc);
|
2008-04-24 22:43:46 +02:00
|
|
|
mempool_destroy(kc->job_pool);
|
2005-04-17 00:20:36 +02:00
|
|
|
kfree(kc);
|
|
|
|
}
|
2008-04-24 22:43:19 +02:00
|
|
|
EXPORT_SYMBOL(dm_kcopyd_client_destroy);
|