1 | The following changes since commit f6b06fcceef465de0cf2514c9f76fe0192896781: | 1 | The following changes since commit 6c769690ac845fa62642a5f93b4e4bd906adab95: |
---|---|---|---|
2 | 2 | ||
3 | Merge remote-tracking branch 'remotes/kraxel/tags/ui-20190121-pull-request' into staging (2019-01-23 17:57:47 +0000) | 3 | Merge remote-tracking branch 'remotes/vsementsov/tags/pull-simplebench-2021-05-04' into staging (2021-05-21 12:02:34 +0100) |
4 | 4 | ||
5 | are available in the Git repository at: | 5 | are available in the Git repository at: |
6 | 6 | ||
7 | git://github.com/stefanha/qemu.git tags/block-pull-request | 7 | https://gitlab.com/stefanha/qemu.git tags/block-pull-request |
8 | 8 | ||
9 | for you to fetch changes up to 8595685986152334b1ec28c78cb0e5e855d56b54: | 9 | for you to fetch changes up to 0a6f0c76a030710780ce10d6347a70f098024d21: |
10 | 10 | ||
11 | qemu-coroutine-sleep: drop CoSleepCB (2019-01-24 10:05:16 +0000) | 11 | coroutine-sleep: introduce qemu_co_sleep (2021-05-21 18:22:33 +0100) |
12 | 12 | ||
13 | ---------------------------------------------------------------- | 13 | ---------------------------------------------------------------- |
14 | Pull request | 14 | Pull request |
15 | 15 | ||
16 | Changelog: No user-visible changes. | 16 | (Resent due to an email preparation mistake.) |
17 | 17 | ||
18 | ---------------------------------------------------------------- | 18 | ---------------------------------------------------------------- |
19 | 19 | ||
20 | Stefan Hajnoczi (2): | 20 | Paolo Bonzini (6): |
21 | throttle-groups: fix restart coroutine iothread race | 21 | coroutine-sleep: use a stack-allocated timer |
22 | iotests: add 238 for throttling tgm unregister iothread segfault | 22 | coroutine-sleep: disallow NULL QemuCoSleepState** argument |
23 | coroutine-sleep: allow qemu_co_sleep_wake that wakes nothing | ||
24 | coroutine-sleep: move timer out of QemuCoSleepState | ||
25 | coroutine-sleep: replace QemuCoSleepState pointer with struct in the | ||
26 | API | ||
27 | coroutine-sleep: introduce qemu_co_sleep | ||
23 | 28 | ||
24 | Vladimir Sementsov-Ogievskiy (1): | 29 | Philippe Mathieu-Daudé (1): |
25 | qemu-coroutine-sleep: drop CoSleepCB | 30 | bitops.h: Improve find_xxx_bit() documentation |
26 | 31 | ||
27 | include/block/throttle-groups.h | 5 ++++ | 32 | Zenghui Yu (1): |
28 | block/throttle-groups.c | 9 +++++++ | 33 | multi-process: Initialize variables declared with g_auto* |
29 | util/qemu-coroutine-sleep.c | 27 +++++++------------ | 34 | |
30 | tests/qemu-iotests/238 | 47 +++++++++++++++++++++++++++++++++ | 35 | include/qemu/bitops.h | 15 ++++++-- |
31 | tests/qemu-iotests/238.out | 6 +++++ | 36 | include/qemu/coroutine.h | 27 ++++++++----- |
32 | tests/qemu-iotests/group | 1 + | 37 | block/block-copy.c | 10 ++--- |
33 | 6 files changed, 78 insertions(+), 17 deletions(-) | 38 | block/nbd.c | 14 +++---- |
34 | create mode 100755 tests/qemu-iotests/238 | 39 | hw/remote/memory.c | 5 +-- |
35 | create mode 100644 tests/qemu-iotests/238.out | 40 | hw/remote/proxy.c | 3 +- |
41 | util/qemu-coroutine-sleep.c | 75 +++++++++++++++++++------------------ | ||
42 | 7 files changed, 79 insertions(+), 70 deletions(-) | ||
36 | 43 | ||
37 | -- | 44 | -- |
38 | 2.20.1 | 45 | 2.31.1 |
39 | 46 | ||
40 | diff view generated by jsdifflib |
New patch | |||
---|---|---|---|
1 | From: Zenghui Yu <yuzenghui@huawei.com> | ||
1 | 2 | ||
3 | Quote docs/devel/style.rst (section "Automatic memory deallocation"): | ||
4 | |||
5 | * Variables declared with g_auto* MUST always be initialized, | ||
6 | otherwise the cleanup function will use uninitialized stack memory | ||
7 | |||
8 | Initialize @name properly to get rid of the compilation error (using | ||
9 | gcc-7.3.0 on CentOS): | ||
10 | |||
11 | ../hw/remote/proxy.c: In function 'pci_proxy_dev_realize': | ||
12 | /usr/include/glib-2.0/glib/glib-autocleanups.h:28:3: error: 'name' may be used uninitialized in this function [-Werror=maybe-uninitialized] | ||
13 | g_free (*pp); | ||
14 | ^~~~~~~~~~~~ | ||
15 | ../hw/remote/proxy.c:350:30: note: 'name' was declared here | ||
16 | g_autofree char *name; | ||
17 | ^~~~ | ||
18 | |||
19 | Signed-off-by: Zenghui Yu <yuzenghui@huawei.com> | ||
20 | Reviewed-by: Jagannathan Raman <jag.raman@oracle.com> | ||
21 | Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com> | ||
22 | Reviewed-by: Miroslav Rezanina <mrezanin@redhat.com> | ||
23 | Message-id: 20210312112143.1369-1-yuzenghui@huawei.com | ||
24 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | ||
25 | --- | ||
26 | hw/remote/memory.c | 5 ++--- | ||
27 | hw/remote/proxy.c | 3 +-- | ||
28 | 2 files changed, 3 insertions(+), 5 deletions(-) | ||
29 | |||
30 | diff --git a/hw/remote/memory.c b/hw/remote/memory.c | ||
31 | index XXXXXXX..XXXXXXX 100644 | ||
32 | --- a/hw/remote/memory.c | ||
33 | +++ b/hw/remote/memory.c | ||
34 | @@ -XXX,XX +XXX,XX @@ void remote_sysmem_reconfig(MPQemuMsg *msg, Error **errp) | ||
35 | |||
36 | remote_sysmem_reset(); | ||
37 | |||
38 | - for (region = 0; region < msg->num_fds; region++) { | ||
39 | - g_autofree char *name; | ||
40 | + for (region = 0; region < msg->num_fds; region++, suffix++) { | ||
41 | + g_autofree char *name = g_strdup_printf("remote-mem-%u", suffix); | ||
42 | subregion = g_new(MemoryRegion, 1); | ||
43 | - name = g_strdup_printf("remote-mem-%u", suffix++); | ||
44 | memory_region_init_ram_from_fd(subregion, NULL, | ||
45 | name, sysmem_info->sizes[region], | ||
46 | true, msg->fds[region], | ||
47 | diff --git a/hw/remote/proxy.c b/hw/remote/proxy.c | ||
48 | index XXXXXXX..XXXXXXX 100644 | ||
49 | --- a/hw/remote/proxy.c | ||
50 | +++ b/hw/remote/proxy.c | ||
51 | @@ -XXX,XX +XXX,XX @@ static void probe_pci_info(PCIDevice *dev, Error **errp) | ||
52 | PCI_BASE_ADDRESS_SPACE_IO : PCI_BASE_ADDRESS_SPACE_MEMORY; | ||
53 | |||
54 | if (size) { | ||
55 | - g_autofree char *name; | ||
56 | + g_autofree char *name = g_strdup_printf("bar-region-%d", i); | ||
57 | pdev->region[i].dev = pdev; | ||
58 | pdev->region[i].present = true; | ||
59 | if (type == PCI_BASE_ADDRESS_SPACE_MEMORY) { | ||
60 | pdev->region[i].memory = true; | ||
61 | } | ||
62 | - name = g_strdup_printf("bar-region-%d", i); | ||
63 | memory_region_init_io(&pdev->region[i].mr, OBJECT(pdev), | ||
64 | &proxy_mr_ops, &pdev->region[i], | ||
65 | name, size); | ||
66 | -- | ||
67 | 2.31.1 | ||
68 | diff view generated by jsdifflib |
New patch | |||
---|---|---|---|
1 | From: Philippe Mathieu-Daudé <philmd@redhat.com> | ||
1 | 2 | ||
3 | Document the following functions return the bitmap size | ||
4 | if no matching bit is found: | ||
5 | |||
6 | - find_first_bit | ||
7 | - find_next_bit | ||
8 | - find_last_bit | ||
9 | - find_first_zero_bit | ||
10 | - find_next_zero_bit | ||
11 | |||
12 | Reviewed-by: Richard Henderson <richard.henderson@linaro.org> | ||
13 | Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com> | ||
14 | Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> | ||
15 | Message-id: 20210510200758.2623154-2-philmd@redhat.com | ||
16 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | ||
17 | --- | ||
18 | include/qemu/bitops.h | 15 ++++++++++++--- | ||
19 | 1 file changed, 12 insertions(+), 3 deletions(-) | ||
20 | |||
21 | diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h | ||
22 | index XXXXXXX..XXXXXXX 100644 | ||
23 | --- a/include/qemu/bitops.h | ||
24 | +++ b/include/qemu/bitops.h | ||
25 | @@ -XXX,XX +XXX,XX @@ static inline int test_bit(long nr, const unsigned long *addr) | ||
26 | * @addr: The address to start the search at | ||
27 | * @size: The maximum size to search | ||
28 | * | ||
29 | - * Returns the bit number of the first set bit, or size. | ||
30 | + * Returns the bit number of the last set bit, | ||
31 | + * or @size if there is no set bit in the bitmap. | ||
32 | */ | ||
33 | unsigned long find_last_bit(const unsigned long *addr, | ||
34 | unsigned long size); | ||
35 | @@ -XXX,XX +XXX,XX @@ unsigned long find_last_bit(const unsigned long *addr, | ||
36 | * @addr: The address to base the search on | ||
37 | * @offset: The bitnumber to start searching at | ||
38 | * @size: The bitmap size in bits | ||
39 | + * | ||
40 | + * Returns the bit number of the next set bit, | ||
41 | + * or @size if there are no further set bits in the bitmap. | ||
42 | */ | ||
43 | unsigned long find_next_bit(const unsigned long *addr, | ||
44 | unsigned long size, | ||
45 | @@ -XXX,XX +XXX,XX @@ unsigned long find_next_bit(const unsigned long *addr, | ||
46 | * @addr: The address to base the search on | ||
47 | * @offset: The bitnumber to start searching at | ||
48 | * @size: The bitmap size in bits | ||
49 | + * | ||
50 | + * Returns the bit number of the next cleared bit, | ||
51 | + * or @size if there are no further clear bits in the bitmap. | ||
52 | */ | ||
53 | |||
54 | unsigned long find_next_zero_bit(const unsigned long *addr, | ||
55 | @@ -XXX,XX +XXX,XX @@ unsigned long find_next_zero_bit(const unsigned long *addr, | ||
56 | * @addr: The address to start the search at | ||
57 | * @size: The maximum size to search | ||
58 | * | ||
59 | - * Returns the bit number of the first set bit. | ||
60 | + * Returns the bit number of the first set bit, | ||
61 | + * or @size if there is no set bit in the bitmap. | ||
62 | */ | ||
63 | static inline unsigned long find_first_bit(const unsigned long *addr, | ||
64 | unsigned long size) | ||
65 | @@ -XXX,XX +XXX,XX @@ static inline unsigned long find_first_bit(const unsigned long *addr, | ||
66 | * @addr: The address to start the search at | ||
67 | * @size: The maximum size to search | ||
68 | * | ||
69 | - * Returns the bit number of the first cleared bit. | ||
70 | + * Returns the bit number of the first cleared bit, | ||
71 | + * or @size if there is no clear bit in the bitmap. | ||
72 | */ | ||
73 | static inline unsigned long find_first_zero_bit(const unsigned long *addr, | ||
74 | unsigned long size) | ||
75 | -- | ||
76 | 2.31.1 | ||
77 | diff view generated by jsdifflib |
New patch | |||
---|---|---|---|
1 | From: Paolo Bonzini <pbonzini@redhat.com> | ||
1 | 2 | ||
3 | The lifetime of the timer is well-known (it cannot outlive | ||
4 | qemu_co_sleep_ns_wakeable, because it's deleted by the time the | ||
5 | coroutine resumes), so it is not necessary to place it on the heap. | ||
6 | |||
7 | Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> | ||
8 | Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> | ||
9 | Message-id: 20210517100548.28806-2-pbonzini@redhat.com | ||
10 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | ||
11 | --- | ||
12 | util/qemu-coroutine-sleep.c | 9 ++++----- | ||
13 | 1 file changed, 4 insertions(+), 5 deletions(-) | ||
14 | |||
15 | diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c | ||
16 | index XXXXXXX..XXXXXXX 100644 | ||
17 | --- a/util/qemu-coroutine-sleep.c | ||
18 | +++ b/util/qemu-coroutine-sleep.c | ||
19 | @@ -XXX,XX +XXX,XX @@ static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns"; | ||
20 | |||
21 | struct QemuCoSleepState { | ||
22 | Coroutine *co; | ||
23 | - QEMUTimer *ts; | ||
24 | + QEMUTimer ts; | ||
25 | QemuCoSleepState **user_state_pointer; | ||
26 | }; | ||
27 | |||
28 | @@ -XXX,XX +XXX,XX @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) | ||
29 | if (sleep_state->user_state_pointer) { | ||
30 | *sleep_state->user_state_pointer = NULL; | ||
31 | } | ||
32 | - timer_del(sleep_state->ts); | ||
33 | + timer_del(&sleep_state->ts); | ||
34 | aio_co_wake(sleep_state->co); | ||
35 | } | ||
36 | |||
37 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | ||
38 | AioContext *ctx = qemu_get_current_aio_context(); | ||
39 | QemuCoSleepState state = { | ||
40 | .co = qemu_coroutine_self(), | ||
41 | - .ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &state), | ||
42 | .user_state_pointer = sleep_state, | ||
43 | }; | ||
44 | |||
45 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | ||
46 | abort(); | ||
47 | } | ||
48 | |||
49 | + aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state); | ||
50 | if (sleep_state) { | ||
51 | *sleep_state = &state; | ||
52 | } | ||
53 | - timer_mod(state.ts, qemu_clock_get_ns(type) + ns); | ||
54 | + timer_mod(&state.ts, qemu_clock_get_ns(type) + ns); | ||
55 | qemu_coroutine_yield(); | ||
56 | if (sleep_state) { | ||
57 | /* | ||
58 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | ||
59 | */ | ||
60 | assert(*sleep_state == NULL); | ||
61 | } | ||
62 | - timer_free(state.ts); | ||
63 | } | ||
64 | -- | ||
65 | 2.31.1 | ||
66 | diff view generated by jsdifflib |
New patch | |||
---|---|---|---|
1 | From: Paolo Bonzini <pbonzini@redhat.com> | ||
1 | 2 | ||
3 | Simplify the code by removing conditionals. qemu_co_sleep_ns | ||
4 | can simply point the argument to an on-stack temporary. | ||
5 | |||
6 | Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> | ||
7 | Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> | ||
8 | Message-id: 20210517100548.28806-3-pbonzini@redhat.com | ||
9 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | ||
10 | --- | ||
11 | include/qemu/coroutine.h | 5 +++-- | ||
12 | util/qemu-coroutine-sleep.c | 18 +++++------------- | ||
13 | 2 files changed, 8 insertions(+), 15 deletions(-) | ||
14 | |||
15 | diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h | ||
16 | index XXXXXXX..XXXXXXX 100644 | ||
17 | --- a/include/qemu/coroutine.h | ||
18 | +++ b/include/qemu/coroutine.h | ||
19 | @@ -XXX,XX +XXX,XX @@ typedef struct QemuCoSleepState QemuCoSleepState; | ||
20 | |||
21 | /** | ||
22 | * Yield the coroutine for a given duration. During this yield, @sleep_state | ||
23 | - * (if not NULL) is set to an opaque pointer, which may be used for | ||
24 | + * is set to an opaque pointer, which may be used for | ||
25 | * qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when the | ||
26 | * timer fires. Don't save the obtained value to other variables and don't call | ||
27 | * qemu_co_sleep_wake from another aio context. | ||
28 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | ||
29 | QemuCoSleepState **sleep_state); | ||
30 | static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) | ||
31 | { | ||
32 | - qemu_co_sleep_ns_wakeable(type, ns, NULL); | ||
33 | + QemuCoSleepState *unused = NULL; | ||
34 | + qemu_co_sleep_ns_wakeable(type, ns, &unused); | ||
35 | } | ||
36 | |||
37 | /** | ||
38 | diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c | ||
39 | index XXXXXXX..XXXXXXX 100644 | ||
40 | --- a/util/qemu-coroutine-sleep.c | ||
41 | +++ b/util/qemu-coroutine-sleep.c | ||
42 | @@ -XXX,XX +XXX,XX @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) | ||
43 | qemu_co_sleep_ns__scheduled, NULL); | ||
44 | |||
45 | assert(scheduled == qemu_co_sleep_ns__scheduled); | ||
46 | - if (sleep_state->user_state_pointer) { | ||
47 | - *sleep_state->user_state_pointer = NULL; | ||
48 | - } | ||
49 | + *sleep_state->user_state_pointer = NULL; | ||
50 | timer_del(&sleep_state->ts); | ||
51 | aio_co_wake(sleep_state->co); | ||
52 | } | ||
53 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | ||
54 | } | ||
55 | |||
56 | aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state); | ||
57 | - if (sleep_state) { | ||
58 | - *sleep_state = &state; | ||
59 | - } | ||
60 | + *sleep_state = &state; | ||
61 | timer_mod(&state.ts, qemu_clock_get_ns(type) + ns); | ||
62 | qemu_coroutine_yield(); | ||
63 | - if (sleep_state) { | ||
64 | - /* | ||
65 | - * Note that *sleep_state is cleared during qemu_co_sleep_wake | ||
66 | - * before resuming this coroutine. | ||
67 | - */ | ||
68 | - assert(*sleep_state == NULL); | ||
69 | - } | ||
70 | + | ||
71 | + /* qemu_co_sleep_wake clears *sleep_state before resuming this coroutine. */ | ||
72 | + assert(*sleep_state == NULL); | ||
73 | } | ||
74 | -- | ||
75 | 2.31.1 | ||
76 | diff view generated by jsdifflib |
1 | The following QMP command leads to a crash when iothreads are used: | 1 | From: Paolo Bonzini <pbonzini@redhat.com> |
---|---|---|---|
2 | 2 | ||
3 | { 'execute': 'device_del', 'arguments': {'id': 'data'} } | 3 | All callers of qemu_co_sleep_wake are checking whether they are passing |
4 | a NULL argument inside the pointer-to-pointer: do the check in | ||
5 | qemu_co_sleep_wake itself. | ||
4 | 6 | ||
5 | The backtrace involves the queue restart coroutine where | 7 | As a side effect, qemu_co_sleep_wake can be called more than once and |
6 | tgm->throttle_state is a NULL pointer because | 8 | it will only wake the coroutine once; after the first time, the argument |
7 | throttle_group_unregister_tgm() has already been called: | 9 | will be set to NULL via *sleep_state->user_state_pointer. However, this |
10 | would not be safe unless co_sleep_cb keeps using the QemuCoSleepState* | ||
11 | directly, so make it go through the pointer-to-pointer instead. | ||
8 | 12 | ||
9 | (gdb) bt full | 13 | Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> |
10 | #0 0x00005585a7a3b378 in qemu_mutex_lock_impl (mutex=0xffffffffffffffd0, file=0x5585a7bb3d54 "block/throttle-groups.c", line=412) at util/qemu-thread-posix.c:64 | 14 | Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> |
11 | err = <optimized out> | 15 | Message-id: 20210517100548.28806-4-pbonzini@redhat.com |
12 | __PRETTY_FUNCTION__ = "qemu_mutex_lock_impl" | ||
13 | __func__ = "qemu_mutex_lock_impl" | ||
14 | #1 0x00005585a79be074 in throttle_group_restart_queue_entry (opaque=0x5585a9de4eb0) at block/throttle-groups.c:412 | ||
15 | _f = <optimized out> | ||
16 | data = 0x5585a9de4eb0 | ||
17 | tgm = 0x5585a9079440 | ||
18 | ts = 0x0 | ||
19 | tg = 0xffffffffffffff98 | ||
20 | is_write = false | ||
21 | empty_queue = 255 | ||
22 | |||
23 | This coroutine should not execute in the iothread after the throttle | ||
24 | group member has been unregistered! | ||
25 | |||
26 | The root cause is that the device_del code path schedules the restart | ||
27 | coroutine in the iothread while holding the AioContext lock. Therefore | ||
28 | the iothread cannot execute the coroutine until after device_del | ||
29 | releases the lock - by this time it's too late. | ||
30 | |||
31 | This patch adds a reference count to ThrottleGroupMember so we can | ||
32 | synchronously wait for restart coroutines to complete. Once they are | ||
33 | done it is safe to unregister the ThrottleGroupMember. | ||
34 | |||
35 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | ||
36 | Reviewed-by: Alberto Garcia <berto@igalia.com> | ||
37 | Message-id: 20190114133257.30299-2-stefanha@redhat.com | ||
38 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | 16 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> |
39 | --- | 17 | --- |
40 | include/block/throttle-groups.h | 5 +++++ | 18 | block/block-copy.c | 4 +--- |
41 | block/throttle-groups.c | 9 +++++++++ | 19 | block/nbd.c | 8 ++------ |
42 | 2 files changed, 14 insertions(+) | 20 | util/qemu-coroutine-sleep.c | 21 ++++++++++++--------- |
21 | 3 files changed, 15 insertions(+), 18 deletions(-) | ||
43 | 22 | ||
44 | diff --git a/include/block/throttle-groups.h b/include/block/throttle-groups.h | 23 | diff --git a/block/block-copy.c b/block/block-copy.c |
45 | index XXXXXXX..XXXXXXX 100644 | 24 | index XXXXXXX..XXXXXXX 100644 |
46 | --- a/include/block/throttle-groups.h | 25 | --- a/block/block-copy.c |
47 | +++ b/include/block/throttle-groups.h | 26 | +++ b/block/block-copy.c |
48 | @@ -XXX,XX +XXX,XX @@ typedef struct ThrottleGroupMember { | 27 | @@ -XXX,XX +XXX,XX @@ out: |
49 | */ | 28 | |
50 | unsigned int io_limits_disabled; | 29 | void block_copy_kick(BlockCopyCallState *call_state) |
51 | 30 | { | |
52 | + /* Number of pending throttle_group_restart_queue_entry() coroutines. | 31 | - if (call_state->sleep_state) { |
53 | + * Accessed with atomic operations. | 32 | - qemu_co_sleep_wake(call_state->sleep_state); |
54 | + */ | 33 | - } |
55 | + unsigned int restart_pending; | 34 | + qemu_co_sleep_wake(call_state->sleep_state); |
56 | + | 35 | } |
57 | /* The following fields are protected by the ThrottleGroup lock. | 36 | |
58 | * See the ThrottleGroup documentation for details. | 37 | /* |
59 | * throttle_state tells us if I/O limits are configured. */ | 38 | diff --git a/block/nbd.c b/block/nbd.c |
60 | diff --git a/block/throttle-groups.c b/block/throttle-groups.c | ||
61 | index XXXXXXX..XXXXXXX 100644 | 39 | index XXXXXXX..XXXXXXX 100644 |
62 | --- a/block/throttle-groups.c | 40 | --- a/block/nbd.c |
63 | +++ b/block/throttle-groups.c | 41 | +++ b/block/nbd.c |
64 | @@ -XXX,XX +XXX,XX @@ static void coroutine_fn throttle_group_restart_queue_entry(void *opaque) | 42 | @@ -XXX,XX +XXX,XX @@ static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs) |
43 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; | ||
44 | |||
45 | s->drained = true; | ||
46 | - if (s->connection_co_sleep_ns_state) { | ||
47 | - qemu_co_sleep_wake(s->connection_co_sleep_ns_state); | ||
48 | - } | ||
49 | + qemu_co_sleep_wake(s->connection_co_sleep_ns_state); | ||
50 | |||
51 | nbd_co_establish_connection_cancel(bs, false); | ||
52 | |||
53 | @@ -XXX,XX +XXX,XX @@ static void nbd_teardown_connection(BlockDriverState *bs) | ||
54 | |||
55 | s->state = NBD_CLIENT_QUIT; | ||
56 | if (s->connection_co) { | ||
57 | - if (s->connection_co_sleep_ns_state) { | ||
58 | - qemu_co_sleep_wake(s->connection_co_sleep_ns_state); | ||
59 | - } | ||
60 | + qemu_co_sleep_wake(s->connection_co_sleep_ns_state); | ||
61 | nbd_co_establish_connection_cancel(bs, true); | ||
65 | } | 62 | } |
66 | 63 | if (qemu_in_coroutine()) { | |
67 | g_free(data); | 64 | diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c |
68 | + | 65 | index XXXXXXX..XXXXXXX 100644 |
69 | + atomic_dec(&tgm->restart_pending); | 66 | --- a/util/qemu-coroutine-sleep.c |
70 | + aio_wait_kick(); | 67 | +++ b/util/qemu-coroutine-sleep.c |
68 | @@ -XXX,XX +XXX,XX @@ struct QemuCoSleepState { | ||
69 | |||
70 | void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) | ||
71 | { | ||
72 | - /* Write of schedule protected by barrier write in aio_co_schedule */ | ||
73 | - const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled, | ||
74 | - qemu_co_sleep_ns__scheduled, NULL); | ||
75 | + if (sleep_state) { | ||
76 | + /* Write of schedule protected by barrier write in aio_co_schedule */ | ||
77 | + const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled, | ||
78 | + qemu_co_sleep_ns__scheduled, NULL); | ||
79 | |||
80 | - assert(scheduled == qemu_co_sleep_ns__scheduled); | ||
81 | - *sleep_state->user_state_pointer = NULL; | ||
82 | - timer_del(&sleep_state->ts); | ||
83 | - aio_co_wake(sleep_state->co); | ||
84 | + assert(scheduled == qemu_co_sleep_ns__scheduled); | ||
85 | + *sleep_state->user_state_pointer = NULL; | ||
86 | + timer_del(&sleep_state->ts); | ||
87 | + aio_co_wake(sleep_state->co); | ||
88 | + } | ||
71 | } | 89 | } |
72 | 90 | ||
73 | static void throttle_group_restart_queue(ThrottleGroupMember *tgm, bool is_write) | 91 | static void co_sleep_cb(void *opaque) |
74 | @@ -XXX,XX +XXX,XX @@ static void throttle_group_restart_queue(ThrottleGroupMember *tgm, bool is_write | 92 | { |
75 | * be no timer pending on this tgm at this point */ | 93 | - qemu_co_sleep_wake(opaque); |
76 | assert(!timer_pending(tgm->throttle_timers.timers[is_write])); | 94 | + QemuCoSleepState **sleep_state = opaque; |
77 | 95 | + qemu_co_sleep_wake(*sleep_state); | |
78 | + atomic_inc(&tgm->restart_pending); | ||
79 | + | ||
80 | co = qemu_coroutine_create(throttle_group_restart_queue_entry, rd); | ||
81 | aio_co_enter(tgm->aio_context, co); | ||
82 | } | 96 | } |
83 | @@ -XXX,XX +XXX,XX @@ void throttle_group_register_tgm(ThrottleGroupMember *tgm, | 97 | |
84 | 98 | void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | |
85 | tgm->throttle_state = ts; | 99 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, |
86 | tgm->aio_context = ctx; | 100 | abort(); |
87 | + atomic_set(&tgm->restart_pending, 0); | ||
88 | |||
89 | qemu_mutex_lock(&tg->lock); | ||
90 | /* If the ThrottleGroup is new set this ThrottleGroupMember as the token */ | ||
91 | @@ -XXX,XX +XXX,XX @@ void throttle_group_unregister_tgm(ThrottleGroupMember *tgm) | ||
92 | return; | ||
93 | } | 101 | } |
94 | 102 | ||
95 | + /* Wait for throttle_group_restart_queue_entry() coroutines to finish */ | 103 | - aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state); |
96 | + AIO_WAIT_WHILE(tgm->aio_context, atomic_read(&tgm->restart_pending) > 0); | 104 | + aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, sleep_state); |
97 | + | 105 | *sleep_state = &state; |
98 | qemu_mutex_lock(&tg->lock); | 106 | timer_mod(&state.ts, qemu_clock_get_ns(type) + ns); |
99 | for (i = 0; i < 2; i++) { | 107 | qemu_coroutine_yield(); |
100 | assert(tgm->pending_reqs[i] == 0); | ||
101 | -- | 108 | -- |
102 | 2.20.1 | 109 | 2.31.1 |
103 | 110 | ||
104 | diff view generated by jsdifflib |
New patch | |||
---|---|---|---|
1 | From: Paolo Bonzini <pbonzini@redhat.com> | ||
1 | 2 | ||
3 | This simplification is enabled by the previous patch. Now aio_co_wake | ||
4 | will only be called once, therefore we do not care about a spurious | ||
5 | firing of the timer after a qemu_co_sleep_wake. | ||
6 | |||
7 | Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> | ||
8 | Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> | ||
9 | Message-id: 20210517100548.28806-5-pbonzini@redhat.com | ||
10 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | ||
11 | --- | ||
12 | util/qemu-coroutine-sleep.c | 8 ++++---- | ||
13 | 1 file changed, 4 insertions(+), 4 deletions(-) | ||
14 | |||
15 | diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c | ||
16 | index XXXXXXX..XXXXXXX 100644 | ||
17 | --- a/util/qemu-coroutine-sleep.c | ||
18 | +++ b/util/qemu-coroutine-sleep.c | ||
19 | @@ -XXX,XX +XXX,XX @@ static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns"; | ||
20 | |||
21 | struct QemuCoSleepState { | ||
22 | Coroutine *co; | ||
23 | - QEMUTimer ts; | ||
24 | QemuCoSleepState **user_state_pointer; | ||
25 | }; | ||
26 | |||
27 | @@ -XXX,XX +XXX,XX @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) | ||
28 | |||
29 | assert(scheduled == qemu_co_sleep_ns__scheduled); | ||
30 | *sleep_state->user_state_pointer = NULL; | ||
31 | - timer_del(&sleep_state->ts); | ||
32 | aio_co_wake(sleep_state->co); | ||
33 | } | ||
34 | } | ||
35 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | ||
36 | QemuCoSleepState **sleep_state) | ||
37 | { | ||
38 | AioContext *ctx = qemu_get_current_aio_context(); | ||
39 | + QEMUTimer ts; | ||
40 | QemuCoSleepState state = { | ||
41 | .co = qemu_coroutine_self(), | ||
42 | .user_state_pointer = sleep_state, | ||
43 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | ||
44 | abort(); | ||
45 | } | ||
46 | |||
47 | - aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, sleep_state); | ||
48 | + aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, sleep_state); | ||
49 | *sleep_state = &state; | ||
50 | - timer_mod(&state.ts, qemu_clock_get_ns(type) + ns); | ||
51 | + timer_mod(&ts, qemu_clock_get_ns(type) + ns); | ||
52 | qemu_coroutine_yield(); | ||
53 | + timer_del(&ts); | ||
54 | |||
55 | /* qemu_co_sleep_wake clears *sleep_state before resuming this coroutine. */ | ||
56 | assert(*sleep_state == NULL); | ||
57 | -- | ||
58 | 2.31.1 | ||
59 | diff view generated by jsdifflib |
1 | From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> | 1 | From: Paolo Bonzini <pbonzini@redhat.com> |
---|---|---|---|
2 | 2 | ||
3 | Drop CoSleepCB structure. It's actually unused. | 3 | Right now, users of qemu_co_sleep_ns_wakeable are simply passing |
4 | 4 | a pointer to QemuCoSleepState by reference to the function. But | |
5 | Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> | 5 | QemuCoSleepState really is just a Coroutine*; making the |
6 | Message-id: 20190122143113.20331-1-vsementsov@virtuozzo.com | 6 | content of the struct public is just as efficient and lets us |
7 | skip the user_state_pointer indirection. | ||
8 | |||
9 | Since the usage is changed, take the occasion to rename the | ||
10 | struct to QemuCoSleep. | ||
11 | |||
12 | Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> | ||
13 | Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> | ||
14 | Message-id: 20210517100548.28806-6-pbonzini@redhat.com | ||
7 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | 15 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> |
8 | --- | 16 | --- |
9 | util/qemu-coroutine-sleep.c | 27 ++++++++++----------------- | 17 | include/qemu/coroutine.h | 23 +++++++++++---------- |
10 | 1 file changed, 10 insertions(+), 17 deletions(-) | 18 | block/block-copy.c | 8 ++++---- |
11 | 19 | block/nbd.c | 10 ++++----- | |
20 | util/qemu-coroutine-sleep.c | 41 ++++++++++++++++--------------------- | ||
21 | 4 files changed, 39 insertions(+), 43 deletions(-) | ||
22 | |||
23 | diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h | ||
24 | index XXXXXXX..XXXXXXX 100644 | ||
25 | --- a/include/qemu/coroutine.h | ||
26 | +++ b/include/qemu/coroutine.h | ||
27 | @@ -XXX,XX +XXX,XX @@ void qemu_co_rwlock_wrlock(CoRwlock *lock); | ||
28 | */ | ||
29 | void qemu_co_rwlock_unlock(CoRwlock *lock); | ||
30 | |||
31 | -typedef struct QemuCoSleepState QemuCoSleepState; | ||
32 | +typedef struct QemuCoSleep { | ||
33 | + Coroutine *to_wake; | ||
34 | +} QemuCoSleep; | ||
35 | |||
36 | /** | ||
37 | - * Yield the coroutine for a given duration. During this yield, @sleep_state | ||
38 | - * is set to an opaque pointer, which may be used for | ||
39 | - * qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when the | ||
40 | - * timer fires. Don't save the obtained value to other variables and don't call | ||
41 | - * qemu_co_sleep_wake from another aio context. | ||
42 | + * Yield the coroutine for a given duration. Initializes @w so that, | ||
43 | + * during this yield, it can be passed to qemu_co_sleep_wake() to | ||
44 | + * terminate the sleep. | ||
45 | */ | ||
46 | -void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, | ||
47 | - QemuCoSleepState **sleep_state); | ||
48 | +void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, | ||
49 | + QEMUClockType type, int64_t ns); | ||
50 | + | ||
51 | static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) | ||
52 | { | ||
53 | - QemuCoSleepState *unused = NULL; | ||
54 | - qemu_co_sleep_ns_wakeable(type, ns, &unused); | ||
55 | + QemuCoSleep w = { 0 }; | ||
56 | + qemu_co_sleep_ns_wakeable(&w, type, ns); | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | @@ -XXX,XX +XXX,XX @@ static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) | ||
61 | * qemu_co_sleep_ns() and should be checked to be non-NULL before calling | ||
62 | * qemu_co_sleep_wake(). | ||
63 | */ | ||
64 | -void qemu_co_sleep_wake(QemuCoSleepState *sleep_state); | ||
65 | +void qemu_co_sleep_wake(QemuCoSleep *w); | ||
66 | |||
67 | /** | ||
68 | * Yield until a file descriptor becomes readable | ||
69 | diff --git a/block/block-copy.c b/block/block-copy.c | ||
70 | index XXXXXXX..XXXXXXX 100644 | ||
71 | --- a/block/block-copy.c | ||
72 | +++ b/block/block-copy.c | ||
73 | @@ -XXX,XX +XXX,XX @@ typedef struct BlockCopyCallState { | ||
74 | /* State */ | ||
75 | int ret; | ||
76 | bool finished; | ||
77 | - QemuCoSleepState *sleep_state; | ||
78 | + QemuCoSleep sleep; | ||
79 | bool cancelled; | ||
80 | |||
81 | /* OUT parameters */ | ||
82 | @@ -XXX,XX +XXX,XX @@ block_copy_dirty_clusters(BlockCopyCallState *call_state) | ||
83 | if (ns > 0) { | ||
84 | block_copy_task_end(task, -EAGAIN); | ||
85 | g_free(task); | ||
86 | - qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, ns, | ||
87 | - &call_state->sleep_state); | ||
88 | + qemu_co_sleep_ns_wakeable(&call_state->sleep, | ||
89 | + QEMU_CLOCK_REALTIME, ns); | ||
90 | continue; | ||
91 | } | ||
92 | } | ||
93 | @@ -XXX,XX +XXX,XX @@ out: | ||
94 | |||
95 | void block_copy_kick(BlockCopyCallState *call_state) | ||
96 | { | ||
97 | - qemu_co_sleep_wake(call_state->sleep_state); | ||
98 | + qemu_co_sleep_wake(&call_state->sleep); | ||
99 | } | ||
100 | |||
101 | /* | ||
102 | diff --git a/block/nbd.c b/block/nbd.c | ||
103 | index XXXXXXX..XXXXXXX 100644 | ||
104 | --- a/block/nbd.c | ||
105 | +++ b/block/nbd.c | ||
106 | @@ -XXX,XX +XXX,XX @@ typedef struct BDRVNBDState { | ||
107 | CoQueue free_sema; | ||
108 | Coroutine *connection_co; | ||
109 | Coroutine *teardown_co; | ||
110 | - QemuCoSleepState *connection_co_sleep_ns_state; | ||
111 | + QemuCoSleep reconnect_sleep; | ||
112 | bool drained; | ||
113 | bool wait_drained_end; | ||
114 | int in_flight; | ||
115 | @@ -XXX,XX +XXX,XX @@ static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs) | ||
116 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; | ||
117 | |||
118 | s->drained = true; | ||
119 | - qemu_co_sleep_wake(s->connection_co_sleep_ns_state); | ||
120 | + qemu_co_sleep_wake(&s->reconnect_sleep); | ||
121 | |||
122 | nbd_co_establish_connection_cancel(bs, false); | ||
123 | |||
124 | @@ -XXX,XX +XXX,XX @@ static void nbd_teardown_connection(BlockDriverState *bs) | ||
125 | |||
126 | s->state = NBD_CLIENT_QUIT; | ||
127 | if (s->connection_co) { | ||
128 | - qemu_co_sleep_wake(s->connection_co_sleep_ns_state); | ||
129 | + qemu_co_sleep_wake(&s->reconnect_sleep); | ||
130 | nbd_co_establish_connection_cancel(bs, true); | ||
131 | } | ||
132 | if (qemu_in_coroutine()) { | ||
133 | @@ -XXX,XX +XXX,XX @@ static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s) | ||
134 | } | ||
135 | bdrv_inc_in_flight(s->bs); | ||
136 | } else { | ||
137 | - qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout, | ||
138 | - &s->connection_co_sleep_ns_state); | ||
139 | + qemu_co_sleep_ns_wakeable(&s->reconnect_sleep, | ||
140 | + QEMU_CLOCK_REALTIME, timeout); | ||
141 | if (s->drained) { | ||
142 | continue; | ||
143 | } | ||
12 | diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c | 144 | diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c |
13 | index XXXXXXX..XXXXXXX 100644 | 145 | index XXXXXXX..XXXXXXX 100644 |
14 | --- a/util/qemu-coroutine-sleep.c | 146 | --- a/util/qemu-coroutine-sleep.c |
15 | +++ b/util/qemu-coroutine-sleep.c | 147 | +++ b/util/qemu-coroutine-sleep.c |
16 | @@ -XXX,XX +XXX,XX @@ | 148 | @@ -XXX,XX +XXX,XX @@ |
17 | #include "qemu/timer.h" | 149 | |
18 | #include "block/aio.h" | 150 | static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns"; |
19 | 151 | ||
20 | -typedef struct CoSleepCB { | 152 | -struct QemuCoSleepState { |
21 | - QEMUTimer *ts; | 153 | +void qemu_co_sleep_wake(QemuCoSleep *w) |
22 | - Coroutine *co; | 154 | +{ |
23 | -} CoSleepCB; | 155 | Coroutine *co; |
24 | - | 156 | - QemuCoSleepState **user_state_pointer; |
157 | -}; | ||
158 | |||
159 | -void qemu_co_sleep_wake(QemuCoSleepState *sleep_state) | ||
160 | -{ | ||
161 | - if (sleep_state) { | ||
162 | + co = w->to_wake; | ||
163 | + w->to_wake = NULL; | ||
164 | + if (co) { | ||
165 | /* Write of schedule protected by barrier write in aio_co_schedule */ | ||
166 | - const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled, | ||
167 | + const char *scheduled = qatomic_cmpxchg(&co->scheduled, | ||
168 | qemu_co_sleep_ns__scheduled, NULL); | ||
169 | |||
170 | assert(scheduled == qemu_co_sleep_ns__scheduled); | ||
171 | - *sleep_state->user_state_pointer = NULL; | ||
172 | - aio_co_wake(sleep_state->co); | ||
173 | + aio_co_wake(co); | ||
174 | } | ||
175 | } | ||
176 | |||
25 | static void co_sleep_cb(void *opaque) | 177 | static void co_sleep_cb(void *opaque) |
26 | { | 178 | { |
27 | - CoSleepCB *sleep_cb = opaque; | 179 | - QemuCoSleepState **sleep_state = opaque; |
28 | + Coroutine *co = opaque; | 180 | - qemu_co_sleep_wake(*sleep_state); |
29 | 181 | + QemuCoSleep *w = opaque; | |
30 | /* Write of schedule protected by barrier write in aio_co_schedule */ | 182 | + qemu_co_sleep_wake(w); |
31 | - atomic_set(&sleep_cb->co->scheduled, NULL); | 183 | } |
32 | - aio_co_wake(sleep_cb->co); | 184 | |
33 | + atomic_set(&co->scheduled, NULL); | 185 | -void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, |
34 | + aio_co_wake(co); | 186 | - QemuCoSleepState **sleep_state) |
35 | } | 187 | +void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, |
36 | 188 | + QEMUClockType type, int64_t ns) | |
37 | void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) | 189 | { |
38 | { | 190 | + Coroutine *co = qemu_coroutine_self(); |
39 | AioContext *ctx = qemu_get_current_aio_context(); | 191 | AioContext *ctx = qemu_get_current_aio_context(); |
40 | - CoSleepCB sleep_cb = { | 192 | QEMUTimer ts; |
193 | - QemuCoSleepState state = { | ||
41 | - .co = qemu_coroutine_self(), | 194 | - .co = qemu_coroutine_self(), |
195 | - .user_state_pointer = sleep_state, | ||
42 | - }; | 196 | - }; |
43 | + QEMUTimer *ts; | 197 | |
44 | + Coroutine *co = qemu_coroutine_self(); | 198 | - const char *scheduled = qatomic_cmpxchg(&state.co->scheduled, NULL, |
45 | 199 | - qemu_co_sleep_ns__scheduled); | |
46 | - const char *scheduled = atomic_cmpxchg(&sleep_cb.co->scheduled, NULL, | 200 | + const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL, |
47 | - __func__); | 201 | + qemu_co_sleep_ns__scheduled); |
48 | + const char *scheduled = atomic_cmpxchg(&co->scheduled, NULL, __func__); | ||
49 | if (scheduled) { | 202 | if (scheduled) { |
50 | fprintf(stderr, | 203 | fprintf(stderr, |
51 | "%s: Co-routine was already scheduled in '%s'\n", | 204 | "%s: Co-routine was already scheduled in '%s'\n", |
52 | __func__, scheduled); | 205 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, |
53 | abort(); | 206 | abort(); |
54 | } | 207 | } |
55 | - sleep_cb.ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &sleep_cb); | 208 | |
56 | - timer_mod(sleep_cb.ts, qemu_clock_get_ns(type) + ns); | 209 | - aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, sleep_state); |
57 | + ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, co); | 210 | - *sleep_state = &state; |
58 | + timer_mod(ts, qemu_clock_get_ns(type) + ns); | 211 | + w->to_wake = co; |
212 | + aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w), | ||
213 | timer_mod(&ts, qemu_clock_get_ns(type) + ns); | ||
59 | qemu_coroutine_yield(); | 214 | qemu_coroutine_yield(); |
60 | - timer_del(sleep_cb.ts); | 215 | timer_del(&ts); |
61 | - timer_free(sleep_cb.ts); | 216 | |
62 | + timer_del(ts); | 217 | - /* qemu_co_sleep_wake clears *sleep_state before resuming this coroutine. */ |
63 | + timer_free(ts); | 218 | - assert(*sleep_state == NULL); |
219 | + /* w->to_wake is cleared before resuming this coroutine. */ | ||
220 | + assert(w->to_wake == NULL); | ||
64 | } | 221 | } |
65 | -- | 222 | -- |
66 | 2.20.1 | 223 | 2.31.1 |
67 | 224 | ||
68 | diff view generated by jsdifflib |
1 | Hot-unplug a scsi-hd using an iothread. The previous patch fixes a | 1 | From: Paolo Bonzini <pbonzini@redhat.com> |
---|---|---|---|
2 | segfault in this scenario. | ||
3 | 2 | ||
4 | This patch adds a regression test. | 3 | Allow using QemuCoSleep to sleep forever until woken by qemu_co_sleep_wake. |
4 | This makes the logic of qemu_co_sleep_ns_wakeable easy to understand. | ||
5 | 5 | ||
6 | Suggested-by: Alberto Garcia <berto@igalia.com> | 6 | In the future we will introduce an API that can work even if the |
7 | Suggested-by: Kevin Wolf <kwolf@redhat.com> | 7 | sleep and wake happen from different threads. For now, initializing |
8 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | 8 | w->to_wake after timer_mod is fine because the timer can only fire in |
9 | Reviewed-by: Alberto Garcia <berto@igalia.com> | 9 | the same AioContext. |
10 | Message-id: 20190114133257.30299-3-stefanha@redhat.com | 10 | |
11 | Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> | ||
12 | Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> | ||
13 | Message-id: 20210517100548.28806-7-pbonzini@redhat.com | ||
11 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> | 14 | Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> |
12 | --- | 15 | --- |
13 | tests/qemu-iotests/238 | 47 ++++++++++++++++++++++++++++++++++++++ | 16 | include/qemu/coroutine.h | 5 +++++ |
14 | tests/qemu-iotests/238.out | 6 +++++ | 17 | util/qemu-coroutine-sleep.c | 26 +++++++++++++++++++------- |
15 | tests/qemu-iotests/group | 1 + | 18 | 2 files changed, 24 insertions(+), 7 deletions(-) |
16 | 3 files changed, 54 insertions(+) | ||
17 | create mode 100755 tests/qemu-iotests/238 | ||
18 | create mode 100644 tests/qemu-iotests/238.out | ||
19 | 19 | ||
20 | diff --git a/tests/qemu-iotests/238 b/tests/qemu-iotests/238 | 20 | diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h |
21 | new file mode 100755 | 21 | index XXXXXXX..XXXXXXX 100644 |
22 | index XXXXXXX..XXXXXXX | 22 | --- a/include/qemu/coroutine.h |
23 | --- /dev/null | 23 | +++ b/include/qemu/coroutine.h |
24 | +++ b/tests/qemu-iotests/238 | 24 | @@ -XXX,XX +XXX,XX @@ typedef struct QemuCoSleep { |
25 | @@ -XXX,XX +XXX,XX @@ | 25 | void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, |
26 | +#!/usr/bin/env python | 26 | QEMUClockType type, int64_t ns); |
27 | +# | 27 | |
28 | +# Regression test for throttle group member unregister segfault with iothread | 28 | +/** |
29 | +# | 29 | + * Yield the coroutine until the next call to qemu_co_sleep_wake. |
30 | +# Copyright (c) 2019 Red Hat, Inc. | 30 | + */ |
31 | +# | 31 | +void coroutine_fn qemu_co_sleep(QemuCoSleep *w); |
32 | +# This program is free software; you can redistribute it and/or modify | ||
33 | +# it under the terms of the GNU General Public License as published by | ||
34 | +# the Free Software Foundation; either version 2 of the License, or | ||
35 | +# (at your option) any later version. | ||
36 | +# | ||
37 | +# This program is distributed in the hope that it will be useful, | ||
38 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
39 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
40 | +# GNU General Public License for more details. | ||
41 | +# | ||
42 | +# You should have received a copy of the GNU General Public License | ||
43 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
44 | +# | ||
45 | + | 32 | + |
46 | +import sys | 33 | static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) |
47 | +import os | 34 | { |
48 | +import iotests | 35 | QemuCoSleep w = { 0 }; |
49 | +from iotests import log | 36 | diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c |
37 | index XXXXXXX..XXXXXXX 100644 | ||
38 | --- a/util/qemu-coroutine-sleep.c | ||
39 | +++ b/util/qemu-coroutine-sleep.c | ||
40 | @@ -XXX,XX +XXX,XX @@ static void co_sleep_cb(void *opaque) | ||
41 | qemu_co_sleep_wake(w); | ||
42 | } | ||
43 | |||
44 | -void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, | ||
45 | - QEMUClockType type, int64_t ns) | ||
46 | +void coroutine_fn qemu_co_sleep(QemuCoSleep *w) | ||
47 | { | ||
48 | Coroutine *co = qemu_coroutine_self(); | ||
49 | - AioContext *ctx = qemu_get_current_aio_context(); | ||
50 | - QEMUTimer ts; | ||
51 | |||
52 | const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL, | ||
53 | qemu_co_sleep_ns__scheduled); | ||
54 | @@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, | ||
55 | } | ||
56 | |||
57 | w->to_wake = co; | ||
58 | - aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w), | ||
59 | - timer_mod(&ts, qemu_clock_get_ns(type) + ns); | ||
60 | qemu_coroutine_yield(); | ||
61 | - timer_del(&ts); | ||
62 | |||
63 | /* w->to_wake is cleared before resuming this coroutine. */ | ||
64 | assert(w->to_wake == NULL); | ||
65 | } | ||
50 | + | 66 | + |
51 | +sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts')) | 67 | +void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, |
68 | + QEMUClockType type, int64_t ns) | ||
69 | +{ | ||
70 | + AioContext *ctx = qemu_get_current_aio_context(); | ||
71 | + QEMUTimer ts; | ||
52 | + | 72 | + |
53 | +from qemu import QEMUMachine | 73 | + aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w); |
74 | + timer_mod(&ts, qemu_clock_get_ns(type) + ns); | ||
54 | + | 75 | + |
55 | +if iotests.qemu_default_machine == 's390-ccw-virtio': | 76 | + /* |
56 | + virtio_scsi_device = 'virtio-scsi-ccw' | 77 | + * The timer will fire in the current AiOContext, so the callback |
57 | +else: | 78 | + * must happen after qemu_co_sleep yields and there is no race |
58 | + virtio_scsi_device = 'virtio-scsi-pci' | 79 | + * between timer_mod and qemu_co_sleep. |
59 | + | 80 | + */ |
60 | +vm = QEMUMachine(iotests.qemu_prog) | 81 | + qemu_co_sleep(w); |
61 | +vm.add_args('-machine', 'accel=kvm') | 82 | + timer_del(&ts); |
62 | +vm.launch() | 83 | +} |
63 | + | ||
64 | +log(vm.qmp('blockdev-add', node_name='hd0', driver='null-co')) | ||
65 | +log(vm.qmp('object-add', qom_type='iothread', id='iothread0')) | ||
66 | +log(vm.qmp('device_add', id='scsi0', driver=virtio_scsi_device, iothread='iothread0')) | ||
67 | +log(vm.qmp('device_add', id='scsi-hd0', driver='scsi-hd', drive='hd0')) | ||
68 | +log(vm.qmp('block_set_io_throttle', id='scsi-hd0', bps=0, bps_rd=0, bps_wr=0, | ||
69 | + iops=1000, iops_rd=0, iops_wr=0, conv_keys=False)) | ||
70 | +log(vm.qmp('device_del', id='scsi-hd0')) | ||
71 | + | ||
72 | +vm.shutdown() | ||
73 | diff --git a/tests/qemu-iotests/238.out b/tests/qemu-iotests/238.out | ||
74 | new file mode 100644 | ||
75 | index XXXXXXX..XXXXXXX | ||
76 | --- /dev/null | ||
77 | +++ b/tests/qemu-iotests/238.out | ||
78 | @@ -XXX,XX +XXX,XX @@ | ||
79 | +{"return": {}} | ||
80 | +{"return": {}} | ||
81 | +{"return": {}} | ||
82 | +{"return": {}} | ||
83 | +{"return": {}} | ||
84 | +{"return": {}} | ||
85 | diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group | ||
86 | index XXXXXXX..XXXXXXX 100644 | ||
87 | --- a/tests/qemu-iotests/group | ||
88 | +++ b/tests/qemu-iotests/group | ||
89 | @@ -XXX,XX +XXX,XX @@ | ||
90 | 234 auto quick migration | ||
91 | 235 auto quick | ||
92 | 236 auto quick | ||
93 | +238 auto quick | ||
94 | -- | 84 | -- |
95 | 2.20.1 | 85 | 2.31.1 |
96 | 86 | ||
97 | diff view generated by jsdifflib |