ratelimit: protect with a mutex
PATCH 1/1 ratelimit: protect with a mutex
Right now, rate limiting is protected by the AioContext mutex, which is taken for example both by the block jobs and by qmp_block_job_set_speed (via find_block_job).
We would like to remove the dependency of block layer code on the AioContext mutex, since most drivers and the core I/O code are already not relying on it. However, there is no existing lock that can easily be taken by both ratelimit_set_speed and ratelimit_calculate_delay, especially because the latter might run in coroutine context (and therefore under a CoMutex) but the former will not.
Since concurrent calls to ratelimit_calculate_delay are not possible, one idea could be to use a seqlock to get a snapshot of slice_ns and slice_quota. But for now keep it simple, and just add a mutex to the RateLimit struct; block jobs are generally not performance critical to the point of optimizing the clock cycles spent in synchronization.
This also requires the introduction of init/destroy functions, so add them to the two users of ratelimit.h.
--- block/block-copy.c | 2 ++ blockjob.c | 3 +++ include/qemu/ratelimit.h | 14 ++++++++++++++ 3 files changed, 19 insertions(+)
block/block-copy.c +2 -0
@@ -230,6 +230,7 @@ void block_copy_state_free(BlockCopyState *s)return ; } +ratelimit_destroy (&s->rate_limit);bdrv_release_dirty_bitmap (s->copy_bitmap);shres_destroy (s->mem);g_free (s);
@@ -289,6 +290,7 @@ BlockCopyState *block_copy_state_new(BdrvChild *source, BdrvChild *target, s->copy_size =MAX (s->cluster_size,BLOCK_COPY_MAX_BUFFER ); } +ratelimit_init (&s->rate_limit);QLIST_INIT (&s->tasks);QLIST_INIT (&s->calls);
blockjob.c +3 -0
@@ -87,6 +87,7 @@ void block_job_free(Job *job)block_job_remove_all_bdrv (bjob);blk_unref (bjob->blk); +ratelimit_destroy (&bjob->limit);error_free (bjob->blocker); }
@@ -435,6 +436,8 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,assert (job->job.driver->free == &block_job_free);assert (job->job.driver->user_resume == &block_job_user_resume); +ratelimit_init (&job->limit); + job->blk = blk; job->finalize_cancelled_notifier.notify = block_job_event_cancelled;
include/qemu/ratelimit.h +14 -0
@@ -14,9 +14,11 @@ #ifndef QEMU_RATELIMIT_H #define QEMU_RATELIMIT_H +#include "qemu/lockable.h" #include "qemu/timer.h" typedef struct { + QemuMutex lock; int64_t slice_start_time; int64_t slice_end_time; uint64_t slice_quota;
@@ -40,6 +42,7 @@ static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)int64_t now =qemu_clock_get_ns (QEMU_CLOCK_REALTIME );double delay_slices; +QEMU_LOCK_GUARD (&limit->lock);assert (limit->slice_quota && limit->slice_ns);if (limit->slice_end_time < now) {
@@ -65,9 +68,20 @@ static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)return limit->slice_end_time - now; } +static inline void ratelimit_init (RateLimit *limit) +{ +qemu_mutex_init (&limit->lock); +} + +static inline void ratelimit_destroy (RateLimit *limit) +{ +qemu_mutex_destroy (&limit->lock); +} +static inline void ratelimit_set_speed (RateLimit *limit,uint64_t speed,uint64_t slice_ns) { +QEMU_LOCK_GUARD (&limit->lock); limit->slice_ns = slice_ns; limit->slice_quota =MAX (((double )speed * slice_ns) /1000000000ULL ,1 ); }
Signature · 1 line
2.30.1
Quoted 20 lines · Paolo Bonzini
Right now, rate limiting is protected by the AioContext mutex, which is taken for example both by the block jobs and by
qmp_block_job_set_speed(viafind_block_job).We would like to remove the dependency of block layer code on the AioContext mutex, since most drivers and the core I/O code are already not relying on it. However, there is no existing lock that can easily be taken by both
ratelimit_set_speedandratelimit_calculate_delay, especially because the latter might run in coroutine context (and therefore under a CoMutex) but the former will not.Since concurrent calls to
ratelimit_calculate_delayare not possible, one idea could be to use a seqlock to get a snapshot ofslice_nsandslice_quota. But for now keep it simple, and just add a mutex to the RateLimit struct; block jobs are generally not performance critical to the point of optimizing the clock cycles spent in synchronization.This also requires the introduction of init/destroy functions, so add them to the two users of
ratelimit.h.
Quoted 6 lines
Signed-off-by Paolo Bonzini <[email protected]>--- block/block-copy.c | 2 ++ blockjob.c | 3 +++ include/qemu/ratelimit.h | 14 ++++++++++++++ 3 files changed, 19 insertions(+)
Quoted 104 lines · Paolo Bonzini
Right now, rate limiting is protected by the AioContext mutex, which is taken for example both by the block jobs and by
qmp_block_job_set_speed(viafind_block_job).We would like to remove the dependency of block layer code on the AioContext mutex, since most drivers and the core I/O code are already not relying on it. However, there is no existing lock that can easily be taken by both
ratelimit_set_speedandratelimit_calculate_delay, especially because the latter might run in coroutine context (and therefore under a CoMutex) but the former will not.Since concurrent calls to
ratelimit_calculate_delayare not possible, one idea could be to use a seqlock to get a snapshot ofslice_nsandslice_quota. But for now keep it simple, and just add a mutex to the RateLimit struct; block jobs are generally not performance critical to the point of optimizing the clock cycles spent in synchronization.This also requires the introduction of init/destroy functions, so add them to the two users of
ratelimit.h.Signed-off-by Paolo Bonzini <[email protected]>--- block/block-copy.c | 2 ++ blockjob.c | 3 +++ include/qemu/ratelimit.h | 14 ++++++++++++++ 3 files changed, 19 insertions(+)block/block-copy.c +2
-0@@ -230,6 +230,7 @@ void block_copy_state_free(BlockCopyState *s)return ; } +ratelimit_destroy (&s->rate_limit);bdrv_release_dirty_bitmap (s->copy_bitmap);shres_destroy (s->mem);g_free (s);@@ -289,6 +290,7 @@ BlockCopyState *block_copy_state_new(BdrvChild *source, BdrvChild *target, s->copy_size =MAX (s->cluster_size,BLOCK_COPY_MAX_BUFFER ); } +ratelimit_init (&s->rate_limit);QLIST_INIT (&s->tasks);QLIST_INIT (&s->calls);blockjob.c +3
-0@@ -87,6 +87,7 @@ void block_job_free(Job *job)block_job_remove_all_bdrv (bjob);blk_unref (bjob->blk); +ratelimit_destroy (&bjob->limit);error_free (bjob->blocker); }@@ -435,6 +436,8 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,assert (job->job.driver->free == &block_job_free);assert (job->job.driver->user_resume == &block_job_user_resume); +ratelimit_init (&job->limit); + job->blk = blk; job->finalize_cancelled_notifier.notify = block_job_event_cancelled;include/qemu/ratelimit.h +10
-0@@ -14,9 +14,11 @@ #ifndef QEMU_RATELIMIT_H #define QEMU_RATELIMIT_H +#include "qemu/lockable.h" #include "qemu/timer.h" typedef struct { + QemuMutex lock; int64_t slice_start_time; int64_t slice_end_time; uint64_t slice_quota;@@ -40,6 +42,7 @@ static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)int64_t now =qemu_clock_get_ns (QEMU_CLOCK_REALTIME );double delay_slices; +QEMU_LOCK_GUARD (&limit->lock);assert (limit->slice_quota && limit->slice_ns);if (limit->slice_end_time < now) {@@ -65,9 +68,20 @@ static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)return limit->slice_end_time - now; } +static inline void ratelimit_init (RateLimit *limit) +{ +qemu_mutex_init (&limit->lock); +} + +static inline void ratelimit_destroy (RateLimit *limit) +{
include/qemu/ratelimit.h static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n) · line 87
Quoted 10 lines
include/qemu/ratelimit.h +4
-0+qemu_mutex_destroy (&limit->lock); +} +static inline void ratelimit_set_speed (RateLimit *limit,uint64_t speed,uint64_t slice_ns) { +QEMU_LOCK_GUARD (&limit->lock); limit->slice_ns = slice_ns; limit->slice_quota =MAX (((double )speed * slice_ns) /1000000000ULL ,1 ); }
Quoted 17 lines · Paolo Bonzini
Right now, rate limiting is protected by the AioContext mutex, which is taken for example both by the block jobs and by
qmp_block_job_set_speed(viafind_block_job).We would like to remove the dependency of block layer code on the AioContext mutex, since most drivers and the core I/O code are already not relying on it. However, there is no existing lock that can easily be taken by both
ratelimit_set_speedandratelimit_calculate_delay, especially because the latter might run in coroutine context (and therefore under a CoMutex) but the former will not.Since concurrent calls to
ratelimit_calculate_delayare not possible, one idea could be to use a seqlock to get a snapshot ofslice_nsandslice_quota. But for now keep it simple, and just add a mutex to the RateLimit struct; block jobs are generally not performance critical to the point of optimizing the clock cycles spent in synchronization.
Quoted 4 lines
This also requires the introduction of init/destroy functions, so add them to the two users of
ratelimit.h.Signed-off-by Paolo Bonzini <[email protected]>
Signature · 2 lines
Best regards, Vladimir