]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools, qcow: Fix locking issues
authorPekka Enberg <penberg@kernel.org>
Sat, 9 Jul 2011 11:04:12 +0000 (14:04 +0300)
committerPekka Enberg <penberg@kernel.org>
Sun, 10 Jul 2011 12:23:22 +0000 (15:23 +0300)
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 <asias.hejun@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Prasad Joshi <prasadjoshi124@gmail.com>
Cc: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/disk/qcow.c
tools/kvm/include/kvm/mutex.h
tools/kvm/include/kvm/qcow.h

index a1f6ef3ee8fa7645f52bc81ad7212a1273bc6cc3..939bc61b5668cdaa2ee25afe8bfdbea397ae092a 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "kvm/disk-image.h"
 #include "kvm/read-write.h"
+#include "kvm/mutex.h"
 #include "kvm/util.h"
 
 #include <sys/types.h>
@@ -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);
index bd765c4989dbc64bfee97b0255421f9131aae131..3286cead3d6d2e82f7c5eba4286fa398f6d70d24 100644 (file)
 
 #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)
index 12247e095718a46a9283c19798af4b3a6305766c..d44c64ada85001b9c6472a1dda0c8ae25b949df5 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef KVM__QCOW_H
 #define KVM__QCOW_H
 
+#include "kvm/mutex.h"
+
 #include <linux/types.h>
 #include <stdbool.h>
 #include <linux/rbtree.h>
@@ -34,6 +36,7 @@ struct qcow_table {
 };
 
 struct qcow {
+       pthread_mutex_t         mutex;
        void                    *header;
        struct qcow_table       table;
        int                     fd;