From db6283385cb708b9d589e5b57e96eab4afd0269e Mon Sep 17 00:00:00 2001 From: Alberto Garcia Date: Mon, 8 Jun 2015 18:17:45 +0200 Subject: throttle: acquire the ThrottleGroup lock in bdrv_swap() bdrv_swap() touches the fields of a BlockDriverState that are protected by the ThrottleGroup lock. Although those fields end up in their original place, they are temporarily swapped in the process, so there's a chance that an operation on a member of the same group happening on a different thread can try to use them. Signed-off-by: Alberto Garcia Reviewed-by: Stefan Hajnoczi Message-id: d92dc40d7c4f1fc5cda5cbbf4ffb7a4670b79d17.1433779731.git.berto@igalia.com Signed-off-by: Stefan Hajnoczi --- block/throttle-groups.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'block') diff --git a/block/throttle-groups.c b/block/throttle-groups.c index da8c70c..efc462f 100644 --- a/block/throttle-groups.c +++ b/block/throttle-groups.c @@ -33,7 +33,8 @@ * its own locking. * * This locking is however handled internally in this file, so it's - * transparent to outside users. + * mostly transparent to outside users (but see the documentation in + * throttle_groups_lock()). * * The whole ThrottleGroup structure is private and invisible to * outside users, that only use it through its ThrottleState. @@ -459,6 +460,34 @@ void throttle_group_unregister_bs(BlockDriverState *bs) bs->throttle_state = NULL; } +/* Acquire the lock of this throttling group. + * + * You won't normally need to use this. None of the functions from the + * ThrottleGroup API require you to acquire the lock since all of them + * deal with it internally. + * + * This should only be used in exceptional cases when you want to + * access the protected fields of a BlockDriverState directly + * (e.g. bdrv_swap()). + * + * @bs: a BlockDriverState that is member of the group + */ +void throttle_group_lock(BlockDriverState *bs) +{ + ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts); + qemu_mutex_lock(&tg->lock); +} + +/* Release the lock of this throttling group. + * + * See the comments in throttle_group_lock(). + */ +void throttle_group_unlock(BlockDriverState *bs) +{ + ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts); + qemu_mutex_unlock(&tg->lock); +} + static void throttle_groups_init(void) { qemu_mutex_init(&throttle_groups_lock); -- cgit v1.1