From: Pekka Enberg Date: Sat, 9 Jul 2011 11:04:12 +0000 (+0300) Subject: kvm tools, qcow: Fix locking issues X-Git-Tag: next-20110824~3^2~121 X-Git-Url: https://git.karo-electronics.de/?a=commitdiff_plain;h=b973dfba85bf3cc3e4861ba192c4a76e358e649c;p=karo-tx-linux.git kvm tools, qcow: Fix locking issues The virtio_blk_do_io() function can enter the QCOW code through disk_image__{read,write,flush}() from multiple threads because it uses a thread pool for I/O requests. Thus, use locking to make the QCOW2 code thread-safe. Cc: Asias He Cc: Cyrill Gorcunov Cc: Ingo Molnar Cc: Prasad Joshi Cc: Sasha Levin Signed-off-by: Pekka Enberg --- diff --git a/tools/kvm/disk/qcow.c b/tools/kvm/disk/qcow.c index a1f6ef3ee8fa..939bc61b5668 100644 --- a/tools/kvm/disk/qcow.c +++ b/tools/kvm/disk/qcow.c @@ -2,6 +2,7 @@ #include "kvm/disk-image.h" #include "kvm/read-write.h" +#include "kvm/mutex.h" #include "kvm/util.h" #include @@ -232,16 +233,17 @@ static ssize_t qcow_read_cluster(struct qcow *q, u64 offset, void *dst, u32 dst_ l1_idx = get_l1_index(q, offset); if (l1_idx >= table->table_size) - goto out_error; + return -1; clust_offset = get_cluster_offset(q, offset); if (clust_offset >= cluster_size) - goto out_error; + return -1; length = cluster_size - clust_offset; if (length > dst_len) length = dst_len; + mutex_lock(&q->mutex); l2_table_offset = table->l1_table[l1_idx] & ~header->oflag_mask; if (!l2_table_offset) goto zero_cluster; @@ -261,19 +263,22 @@ static ssize_t qcow_read_cluster(struct qcow *q, u64 offset, void *dst, u32 dst_ if (!clust_start) goto zero_cluster; + mutex_unlock(&q->mutex); + if (pread_in_full(q->fd, dst, length, clust_start + clust_offset) < 0) - goto out_error; + return -1; -out: return length; zero_cluster: + mutex_unlock(&q->mutex); memset(dst, 0, length); - goto out; + return length; out_error: + mutex_unlock(&q->mutex); length = -1; - goto out; + return -1; } static ssize_t qcow_read_sector(struct disk_image *disk, u64 sector, void *dst, u32 dst_len) @@ -379,20 +384,22 @@ static ssize_t qcow_write_cluster(struct qcow *q, u64 offset, void *buf, u32 src l1t_idx = get_l1_index(q, offset); if (l1t_idx >= table->table_size) - goto error; + return -1; l2t_idx = get_l2_index(q, offset); if (l2t_idx >= l2t_sz) - goto error; + return -1; clust_off = get_cluster_offset(q, offset); if (clust_off >= clust_sz) - goto error; + return -1; len = clust_sz - clust_off; if (len > src_len) len = src_len; + mutex_lock(&q->mutex); + l2t_off = table->l1_table[l1t_idx] & ~header->oflag_mask; if (l2t_off) { /* read and cache l2 table */ @@ -466,11 +473,14 @@ static ssize_t qcow_write_cluster(struct qcow *q, u64 offset, void *buf, u32 src l2t->table[l2t_idx] = clust_start; } + mutex_unlock(&q->mutex); + return len; free_cache: free(l2t); error: + mutex_unlock(&q->mutex); return -1; } @@ -611,6 +621,7 @@ static struct disk_image *qcow2_probe(int fd, bool readonly) if (!q) goto error; + mutex_init(&q->mutex); q->fd = fd; q->root = RB_ROOT; INIT_LIST_HEAD(&q->lru_list); @@ -710,6 +721,7 @@ static struct disk_image *qcow1_probe(int fd, bool readonly) if (!q) goto error; + mutex_init(&q->mutex); q->fd = fd; q->root = RB_ROOT; INIT_LIST_HEAD(&q->lru_list); diff --git a/tools/kvm/include/kvm/mutex.h b/tools/kvm/include/kvm/mutex.h index bd765c4989db..3286cead3d6d 100644 --- a/tools/kvm/include/kvm/mutex.h +++ b/tools/kvm/include/kvm/mutex.h @@ -12,6 +12,12 @@ #define DEFINE_MUTEX(mutex) pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER +static inline void mutex_init(pthread_mutex_t *mutex) +{ + if (pthread_mutex_init(mutex, NULL) != 0) + die("unexpected pthread_mutex_init() failure!"); +} + static inline void mutex_lock(pthread_mutex_t *mutex) { if (pthread_mutex_lock(mutex) != 0) diff --git a/tools/kvm/include/kvm/qcow.h b/tools/kvm/include/kvm/qcow.h index 12247e095718..d44c64ada850 100644 --- a/tools/kvm/include/kvm/qcow.h +++ b/tools/kvm/include/kvm/qcow.h @@ -1,6 +1,8 @@ #ifndef KVM__QCOW_H #define KVM__QCOW_H +#include "kvm/mutex.h" + #include #include #include @@ -34,6 +36,7 @@ struct qcow_table { }; struct qcow { + pthread_mutex_t mutex; void *header; struct qcow_table table; int fd;