1
The following changes since commit f58d9620aa4a514b1227074ff56eefd1334a6225:
1
The following changes since commit 6c769690ac845fa62642a5f93b4e4bd906adab95:
2
2
3
Merge remote-tracking branch 'remotes/rth/tags/pull-dt-20180326' into staging (2018-03-27 10:27:34 +0100)
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 f5a53faad4bfbf1b86012a13055d2a1a774a42b6:
9
for you to fetch changes up to 0a6f0c76a030710780ce10d6347a70f098024d21:
10
10
11
MAINTAINERS: add include/block/aio-wait.h (2018-03-27 13:05:48 +0100)
11
coroutine-sleep: introduce qemu_co_sleep (2021-05-21 18:22:33 +0100)
12
13
----------------------------------------------------------------
14
Pull request
15
16
(Resent due to an email preparation mistake.)
12
17
13
----------------------------------------------------------------
18
----------------------------------------------------------------
14
19
15
----------------------------------------------------------------
20
Paolo Bonzini (6):
21
coroutine-sleep: use a stack-allocated timer
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
16
28
17
Stefan Hajnoczi (4):
29
Philippe Mathieu-Daudé (1):
18
queue: add QSIMPLEQ_PREPEND()
30
bitops.h: Improve find_xxx_bit() documentation
19
coroutine: avoid co_queue_wakeup recursion
20
coroutine: add test-aio coroutine queue chaining test case
21
MAINTAINERS: add include/block/aio-wait.h
22
31
23
MAINTAINERS | 1 +
32
Zenghui Yu (1):
24
include/qemu/coroutine_int.h | 1 -
33
multi-process: Initialize variables declared with g_auto*
25
include/qemu/queue.h | 8 ++++
34
26
block/io.c | 3 +-
35
include/qemu/bitops.h | 15 ++++++--
27
tests/test-aio.c | 65 ++++++++++++++++++++-----
36
include/qemu/coroutine.h | 27 ++++++++-----
28
util/qemu-coroutine-lock.c | 34 -------------
37
block/block-copy.c | 10 ++---
29
util/qemu-coroutine.c | 110 +++++++++++++++++++++++--------------------
38
block/nbd.c | 14 +++----
30
7 files changed, 121 insertions(+), 101 deletions(-)
39
hw/remote/memory.c | 5 +--
40
hw/remote/proxy.c | 3 +-
41
util/qemu-coroutine-sleep.c | 75 +++++++++++++++++++------------------
42
7 files changed, 79 insertions(+), 70 deletions(-)
31
43
32
--
44
--
33
2.14.3
45
2.31.1
34
46
35
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 include/block/aio-wait.h header file was added by commit
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
7719f3c968c59e1bcda7e177679dc765b59e578f ("block: extract
3
AIO_WAIT_WHILE() from BlockDriverState") without updating MAINTAINERS.
4
2
5
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
3
All callers of qemu_co_sleep_wake are checking whether they are passing
6
Reviewed-by: Eric Blake <eblake@redhat.com>
4
a NULL argument inside the pointer-to-pointer: do the check in
7
Message-id: 20180312132204.23683-1-stefanha@redhat.com
5
qemu_co_sleep_wake itself.
6
7
As a side effect, qemu_co_sleep_wake can be called more than once and
8
it will only wake the coroutine once; after the first time, the argument
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.
12
13
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
14
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
15
Message-id: 20210517100548.28806-4-pbonzini@redhat.com
8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
16
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9
---
17
---
10
MAINTAINERS | 1 +
18
block/block-copy.c | 4 +---
11
1 file changed, 1 insertion(+)
19
block/nbd.c | 8 ++------
20
util/qemu-coroutine-sleep.c | 21 ++++++++++++---------
21
3 files changed, 15 insertions(+), 18 deletions(-)
12
22
13
diff --git a/MAINTAINERS b/MAINTAINERS
23
diff --git a/block/block-copy.c b/block/block-copy.c
14
index XXXXXXX..XXXXXXX 100644
24
index XXXXXXX..XXXXXXX 100644
15
--- a/MAINTAINERS
25
--- a/block/block-copy.c
16
+++ b/MAINTAINERS
26
+++ b/block/block-copy.c
17
@@ -XXX,XX +XXX,XX @@ F: util/aio-*.c
27
@@ -XXX,XX +XXX,XX @@ out:
18
F: block/io.c
28
19
F: migration/block*
29
void block_copy_kick(BlockCopyCallState *call_state)
20
F: include/block/aio.h
30
{
21
+F: include/block/aio-wait.h
31
- if (call_state->sleep_state) {
22
F: scripts/qemugdb/aio.py
32
- qemu_co_sleep_wake(call_state->sleep_state);
23
T: git git://github.com/stefanha/qemu.git block
33
- }
24
34
+ qemu_co_sleep_wake(call_state->sleep_state);
35
}
36
37
/*
38
diff --git a/block/nbd.c b/block/nbd.c
39
index XXXXXXX..XXXXXXX 100644
40
--- a/block/nbd.c
41
+++ b/block/nbd.c
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);
62
}
63
if (qemu_in_coroutine()) {
64
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
65
index XXXXXXX..XXXXXXX 100644
66
--- a/util/qemu-coroutine-sleep.c
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
+ }
89
}
90
91
static void co_sleep_cb(void *opaque)
92
{
93
- qemu_co_sleep_wake(opaque);
94
+ QemuCoSleepState **sleep_state = opaque;
95
+ qemu_co_sleep_wake(*sleep_state);
96
}
97
98
void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
99
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
100
abort();
101
}
102
103
- aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, &state);
104
+ aio_timer_init(ctx, &state.ts, type, SCALE_NS, co_sleep_cb, sleep_state);
105
*sleep_state = &state;
106
timer_mod(&state.ts, qemu_clock_get_ns(type) + ns);
107
qemu_coroutine_yield();
25
--
108
--
26
2.14.3
109
2.31.1
27
110
28
diff view generated by jsdifflib
1
QSIMPLEQ_CONCAT(a, b) joins a = a + b. The new QSIMPLEQ_PREPEND(a, b)
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
API joins a = b + a.
3
2
4
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
3
This simplification is enabled by the previous patch. Now aio_co_wake
5
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
4
will only be called once, therefore we do not care about a spurious
6
Message-id: 20180322152834.12656-2-stefanha@redhat.com
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
7
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
10
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
8
---
11
---
9
include/qemu/queue.h | 8 ++++++++
12
util/qemu-coroutine-sleep.c | 8 ++++----
10
1 file changed, 8 insertions(+)
13
1 file changed, 4 insertions(+), 4 deletions(-)
11
14
12
diff --git a/include/qemu/queue.h b/include/qemu/queue.h
15
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
13
index XXXXXXX..XXXXXXX 100644
16
index XXXXXXX..XXXXXXX 100644
14
--- a/include/qemu/queue.h
17
--- a/util/qemu-coroutine-sleep.c
15
+++ b/include/qemu/queue.h
18
+++ b/util/qemu-coroutine-sleep.c
16
@@ -XXX,XX +XXX,XX @@ struct { \
19
@@ -XXX,XX +XXX,XX @@ static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
17
} \
20
18
} while (/*CONSTCOND*/0)
21
struct QemuCoSleepState {
19
22
Coroutine *co;
20
+#define QSIMPLEQ_PREPEND(head1, head2) do { \
23
- QEMUTimer ts;
21
+ if (!QSIMPLEQ_EMPTY((head2))) { \
24
QemuCoSleepState **user_state_pointer;
22
+ *(head2)->sqh_last = (head1)->sqh_first; \
25
};
23
+ (head1)->sqh_first = (head2)->sqh_first; \
26
24
+ QSIMPLEQ_INIT((head2)); \
27
@@ -XXX,XX +XXX,XX @@ void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
25
+ } \
28
26
+} while (/*CONSTCOND*/0)
29
assert(scheduled == qemu_co_sleep_ns__scheduled);
27
+
30
*sleep_state->user_state_pointer = NULL;
28
#define QSIMPLEQ_LAST(head, type, field) \
31
- timer_del(&sleep_state->ts);
29
(QSIMPLEQ_EMPTY((head)) ? \
32
aio_co_wake(sleep_state->co);
30
NULL : \
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);
31
--
57
--
32
2.14.3
58
2.31.1
33
59
34
diff view generated by jsdifflib
1
qemu_aio_coroutine_enter() is (indirectly) called recursively when
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
processing co_queue_wakeup. This can lead to stack exhaustion.
2
3
3
Right now, users of qemu_co_sleep_ns_wakeable are simply passing
4
This patch rewrites co_queue_wakeup in an iterative fashion (instead of
4
a pointer to QemuCoSleepState by reference to the function. But
5
recursive) with bounded memory usage to prevent stack exhaustion.
5
QemuCoSleepState really is just a Coroutine*; making the
6
6
content of the struct public is just as efficient and lets us
7
qemu_co_queue_run_restart() is inlined into qemu_aio_coroutine_enter()
7
skip the user_state_pointer indirection.
8
and the qemu_coroutine_enter() call is turned into a loop to avoid
8
9
recursion.
9
Since the usage is changed, take the occasion to rename the
10
10
struct to QemuCoSleep.
11
There is one change that is worth mentioning: Previously, when
11
12
coroutine A queued coroutine B, qemu_co_queue_run_restart() entered
12
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
13
coroutine B from coroutine A. If A was terminating then it would still
13
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
14
stay alive until B yielded. After this patch B is entered by A's parent
14
Message-id: 20210517100548.28806-6-pbonzini@redhat.com
15
so that a A can be deleted immediately if it is terminating.
16
17
It is safe to make this change since B could never interact with A if it
18
was terminating anyway.
19
20
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
21
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
22
Message-id: 20180322152834.12656-3-stefanha@redhat.com
23
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
15
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
24
---
16
---
25
include/qemu/coroutine_int.h | 1 -
17
include/qemu/coroutine.h | 23 +++++++++++----------
26
block/io.c | 3 +-
18
block/block-copy.c | 8 ++++----
27
util/qemu-coroutine-lock.c | 34 -------------
19
block/nbd.c | 10 ++++-----
28
util/qemu-coroutine.c | 110 +++++++++++++++++++++++--------------------
20
util/qemu-coroutine-sleep.c | 41 ++++++++++++++++---------------------
29
4 files changed, 60 insertions(+), 88 deletions(-)
21
4 files changed, 39 insertions(+), 43 deletions(-)
30
22
31
diff --git a/include/qemu/coroutine_int.h b/include/qemu/coroutine_int.h
23
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
32
index XXXXXXX..XXXXXXX 100644
24
index XXXXXXX..XXXXXXX 100644
33
--- a/include/qemu/coroutine_int.h
25
--- a/include/qemu/coroutine.h
34
+++ b/include/qemu/coroutine_int.h
26
+++ b/include/qemu/coroutine.h
35
@@ -XXX,XX +XXX,XX @@ Coroutine *qemu_coroutine_new(void);
27
@@ -XXX,XX +XXX,XX @@ void qemu_co_rwlock_wrlock(CoRwlock *lock);
36
void qemu_coroutine_delete(Coroutine *co);
28
*/
37
CoroutineAction qemu_coroutine_switch(Coroutine *from, Coroutine *to,
29
void qemu_co_rwlock_unlock(CoRwlock *lock);
38
CoroutineAction action);
30
39
-void coroutine_fn qemu_co_queue_run_restart(Coroutine *co);
31
-typedef struct QemuCoSleepState QemuCoSleepState;
40
32
+typedef struct QemuCoSleep {
41
#endif
33
+ Coroutine *to_wake;
42
diff --git a/block/io.c b/block/io.c
34
+} QemuCoSleep;
43
index XXXXXXX..XXXXXXX 100644
35
44
--- a/block/io.c
36
/**
45
+++ b/block/io.c
37
- * Yield the coroutine for a given duration. During this yield, @sleep_state
46
@@ -XXX,XX +XXX,XX @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
38
- * is set to an opaque pointer, which may be used for
47
BdrvCoDrainData data;
39
- * qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when the
48
40
- * timer fires. Don't save the obtained value to other variables and don't call
49
/* Calling bdrv_drain() from a BH ensures the current coroutine yields and
41
- * qemu_co_sleep_wake from another aio context.
50
- * other coroutines run if they were queued from
42
+ * Yield the coroutine for a given duration. Initializes @w so that,
51
- * qemu_co_queue_run_restart(). */
43
+ * during this yield, it can be passed to qemu_co_sleep_wake() to
52
+ * other coroutines run if they were queued by aio_co_enter(). */
44
+ * terminate the sleep.
53
45
*/
54
assert(qemu_in_coroutine());
46
-void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
55
data = (BdrvCoDrainData) {
47
- QemuCoSleepState **sleep_state);
56
diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c
48
+void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
57
index XXXXXXX..XXXXXXX 100644
49
+ QEMUClockType type, int64_t ns);
58
--- a/util/qemu-coroutine-lock.c
50
+
59
+++ b/util/qemu-coroutine-lock.c
51
static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
60
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock)
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);
61
}
131
}
62
}
132
if (qemu_in_coroutine()) {
63
133
@@ -XXX,XX +XXX,XX @@ static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s)
64
-/**
134
}
65
- * qemu_co_queue_run_restart:
135
bdrv_inc_in_flight(s->bs);
66
- *
136
} else {
67
- * Enter each coroutine that was previously marked for restart by
137
- qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout,
68
- * qemu_co_queue_next() or qemu_co_queue_restart_all(). This function is
138
- &s->connection_co_sleep_ns_state);
69
- * invoked by the core coroutine code when the current coroutine yields or
139
+ qemu_co_sleep_ns_wakeable(&s->reconnect_sleep,
70
- * terminates.
140
+ QEMU_CLOCK_REALTIME, timeout);
71
- */
141
if (s->drained) {
72
-void qemu_co_queue_run_restart(Coroutine *co)
142
continue;
143
}
144
diff --git a/util/qemu-coroutine-sleep.c b/util/qemu-coroutine-sleep.c
145
index XXXXXXX..XXXXXXX 100644
146
--- a/util/qemu-coroutine-sleep.c
147
+++ b/util/qemu-coroutine-sleep.c
148
@@ -XXX,XX +XXX,XX @@
149
150
static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
151
152
-struct QemuCoSleepState {
153
+void qemu_co_sleep_wake(QemuCoSleep *w)
154
+{
155
Coroutine *co;
156
- QemuCoSleepState **user_state_pointer;
157
-};
158
159
-void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
73
-{
160
-{
74
- Coroutine *next;
161
- if (sleep_state) {
75
- QSIMPLEQ_HEAD(, Coroutine) tmp_queue_wakeup =
162
+ co = w->to_wake;
76
- QSIMPLEQ_HEAD_INITIALIZER(tmp_queue_wakeup);
163
+ w->to_wake = NULL;
77
-
164
+ if (co) {
78
- trace_qemu_co_queue_run_restart(co);
165
/* Write of schedule protected by barrier write in aio_co_schedule */
79
-
166
- const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled,
80
- /* Because "co" has yielded, any coroutine that we wakeup can resume it.
167
+ const char *scheduled = qatomic_cmpxchg(&co->scheduled,
81
- * If this happens and "co" terminates, co->co_queue_wakeup becomes
168
qemu_co_sleep_ns__scheduled, NULL);
82
- * invalid memory. Therefore, use a temporary queue and do not touch
169
83
- * the "co" coroutine as soon as you enter another one.
170
assert(scheduled == qemu_co_sleep_ns__scheduled);
84
- *
171
- *sleep_state->user_state_pointer = NULL;
85
- * In its turn resumed "co" can populate "co_queue_wakeup" queue with
172
- aio_co_wake(sleep_state->co);
86
- * new coroutines to be woken up. The caller, who has resumed "co",
173
+ aio_co_wake(co);
87
- * will be responsible for traversing the same queue, which may cause
88
- * a different wakeup order but not any missing wakeups.
89
- */
90
- QSIMPLEQ_CONCAT(&tmp_queue_wakeup, &co->co_queue_wakeup);
91
-
92
- while ((next = QSIMPLEQ_FIRST(&tmp_queue_wakeup))) {
93
- QSIMPLEQ_REMOVE_HEAD(&tmp_queue_wakeup, co_queue_next);
94
- qemu_coroutine_enter(next);
95
- }
96
-}
97
-
98
static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
99
{
100
Coroutine *next;
101
diff --git a/util/qemu-coroutine.c b/util/qemu-coroutine.c
102
index XXXXXXX..XXXXXXX 100644
103
--- a/util/qemu-coroutine.c
104
+++ b/util/qemu-coroutine.c
105
@@ -XXX,XX +XXX,XX @@ static void coroutine_delete(Coroutine *co)
106
107
void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co)
108
{
109
- Coroutine *self = qemu_coroutine_self();
110
- CoroutineAction ret;
111
-
112
- /* Cannot rely on the read barrier for co in aio_co_wake(), as there are
113
- * callers outside of aio_co_wake() */
114
- const char *scheduled = atomic_mb_read(&co->scheduled);
115
-
116
- trace_qemu_aio_coroutine_enter(ctx, self, co, co->entry_arg);
117
-
118
- /* if the Coroutine has already been scheduled, entering it again will
119
- * cause us to enter it twice, potentially even after the coroutine has
120
- * been deleted */
121
- if (scheduled) {
122
- fprintf(stderr,
123
- "%s: Co-routine was already scheduled in '%s'\n",
124
- __func__, scheduled);
125
- abort();
126
- }
127
-
128
- if (co->caller) {
129
- fprintf(stderr, "Co-routine re-entered recursively\n");
130
- abort();
131
- }
132
-
133
- co->caller = self;
134
- co->ctx = ctx;
135
-
136
- /* Store co->ctx before anything that stores co. Matches
137
- * barrier in aio_co_wake and qemu_co_mutex_wake.
138
- */
139
- smp_wmb();
140
-
141
- ret = qemu_coroutine_switch(self, co, COROUTINE_ENTER);
142
-
143
- qemu_co_queue_run_restart(co);
144
-
145
- /* Beware, if ret == COROUTINE_YIELD and qemu_co_queue_run_restart()
146
- * has started any other coroutine, "co" might have been reentered
147
- * and even freed by now! So be careful and do not touch it.
148
- */
149
-
150
- switch (ret) {
151
- case COROUTINE_YIELD:
152
- return;
153
- case COROUTINE_TERMINATE:
154
- assert(!co->locks_held);
155
- trace_qemu_coroutine_terminate(co);
156
- coroutine_delete(co);
157
- return;
158
- default:
159
- abort();
160
+ QSIMPLEQ_HEAD(, Coroutine) pending = QSIMPLEQ_HEAD_INITIALIZER(pending);
161
+ Coroutine *from = qemu_coroutine_self();
162
+
163
+ QSIMPLEQ_INSERT_TAIL(&pending, co, co_queue_next);
164
+
165
+ /* Run co and any queued coroutines */
166
+ while (!QSIMPLEQ_EMPTY(&pending)) {
167
+ Coroutine *to = QSIMPLEQ_FIRST(&pending);
168
+ CoroutineAction ret;
169
+
170
+ /* Cannot rely on the read barrier for to in aio_co_wake(), as there are
171
+ * callers outside of aio_co_wake() */
172
+ const char *scheduled = atomic_mb_read(&to->scheduled);
173
+
174
+ QSIMPLEQ_REMOVE_HEAD(&pending, co_queue_next);
175
+
176
+ trace_qemu_aio_coroutine_enter(ctx, from, to, to->entry_arg);
177
+
178
+ /* if the Coroutine has already been scheduled, entering it again will
179
+ * cause us to enter it twice, potentially even after the coroutine has
180
+ * been deleted */
181
+ if (scheduled) {
182
+ fprintf(stderr,
183
+ "%s: Co-routine was already scheduled in '%s'\n",
184
+ __func__, scheduled);
185
+ abort();
186
+ }
187
+
188
+ if (to->caller) {
189
+ fprintf(stderr, "Co-routine re-entered recursively\n");
190
+ abort();
191
+ }
192
+
193
+ to->caller = from;
194
+ to->ctx = ctx;
195
+
196
+ /* Store to->ctx before anything that stores to. Matches
197
+ * barrier in aio_co_wake and qemu_co_mutex_wake.
198
+ */
199
+ smp_wmb();
200
+
201
+ ret = qemu_coroutine_switch(from, to, COROUTINE_ENTER);
202
+
203
+ /* Queued coroutines are run depth-first; previously pending coroutines
204
+ * run after those queued more recently.
205
+ */
206
+ QSIMPLEQ_PREPEND(&pending, &to->co_queue_wakeup);
207
+
208
+ switch (ret) {
209
+ case COROUTINE_YIELD:
210
+ break;
211
+ case COROUTINE_TERMINATE:
212
+ assert(!to->locks_held);
213
+ trace_qemu_coroutine_terminate(to);
214
+ coroutine_delete(to);
215
+ break;
216
+ default:
217
+ abort();
218
+ }
219
}
174
}
220
}
175
}
221
176
177
static void co_sleep_cb(void *opaque)
178
{
179
- QemuCoSleepState **sleep_state = opaque;
180
- qemu_co_sleep_wake(*sleep_state);
181
+ QemuCoSleep *w = opaque;
182
+ qemu_co_sleep_wake(w);
183
}
184
185
-void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
186
- QemuCoSleepState **sleep_state)
187
+void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
188
+ QEMUClockType type, int64_t ns)
189
{
190
+ Coroutine *co = qemu_coroutine_self();
191
AioContext *ctx = qemu_get_current_aio_context();
192
QEMUTimer ts;
193
- QemuCoSleepState state = {
194
- .co = qemu_coroutine_self(),
195
- .user_state_pointer = sleep_state,
196
- };
197
198
- const char *scheduled = qatomic_cmpxchg(&state.co->scheduled, NULL,
199
- qemu_co_sleep_ns__scheduled);
200
+ const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
201
+ qemu_co_sleep_ns__scheduled);
202
if (scheduled) {
203
fprintf(stderr,
204
"%s: Co-routine was already scheduled in '%s'\n",
205
@@ -XXX,XX +XXX,XX @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
206
abort();
207
}
208
209
- aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, sleep_state);
210
- *sleep_state = &state;
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);
214
qemu_coroutine_yield();
215
timer_del(&ts);
216
217
- /* qemu_co_sleep_wake clears *sleep_state before resuming this coroutine. */
218
- assert(*sleep_state == NULL);
219
+ /* w->to_wake is cleared before resuming this coroutine. */
220
+ assert(w->to_wake == NULL);
221
}
222
--
222
--
223
2.14.3
223
2.31.1
224
224
225
diff view generated by jsdifflib
1
Check that two coroutines can queue each other repeatedly without
1
From: Paolo Bonzini <pbonzini@redhat.com>
2
hitting stack exhaustion.
3
2
4
Switch to qemu_init_main_loop() in main() because coroutines use
3
Allow using QemuCoSleep to sleep forever until woken by qemu_co_sleep_wake.
5
qemu_get_aio_context() - they don't know about test-aio's ctx variable.
4
This makes the logic of qemu_co_sleep_ns_wakeable easy to understand.
6
5
7
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
6
In the future we will introduce an API that can work even if the
8
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
7
sleep and wake happen from different threads. For now, initializing
9
Message-id: 20180322152834.12656-4-stefanha@redhat.com
8
w->to_wake after timer_mod is fine because the timer can only fire in
9
the same AioContext.
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
10
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
14
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
11
---
15
---
12
tests/test-aio.c | 65 ++++++++++++++++++++++++++++++++++++++++++++------------
16
include/qemu/coroutine.h | 5 +++++
13
1 file changed, 52 insertions(+), 13 deletions(-)
17
util/qemu-coroutine-sleep.c | 26 +++++++++++++++++++-------
18
2 files changed, 24 insertions(+), 7 deletions(-)
14
19
15
diff --git a/tests/test-aio.c b/tests/test-aio.c
20
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
16
index XXXXXXX..XXXXXXX 100644
21
index XXXXXXX..XXXXXXX 100644
17
--- a/tests/test-aio.c
22
--- a/include/qemu/coroutine.h
18
+++ b/tests/test-aio.c
23
+++ b/include/qemu/coroutine.h
19
@@ -XXX,XX +XXX,XX @@
24
@@ -XXX,XX +XXX,XX @@ typedef struct QemuCoSleep {
20
#include "qemu/timer.h"
25
void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
21
#include "qemu/sockets.h"
26
QEMUClockType type, int64_t ns);
22
#include "qemu/error-report.h"
27
23
+#include "qemu/coroutine.h"
28
+/**
24
+#include "qemu/main-loop.h"
29
+ * Yield the coroutine until the next call to qemu_co_sleep_wake.
25
30
+ */
26
static AioContext *ctx;
31
+void coroutine_fn qemu_co_sleep(QemuCoSleep *w);
27
32
+
28
@@ -XXX,XX +XXX,XX @@ static void test_source_timer_schedule(void)
33
static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
29
timer_del(&data.timer);
34
{
35
QemuCoSleep w = { 0 };
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);
30
}
42
}
31
43
32
+/*
44
-void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
33
+ * Check that aio_co_enter() can chain many times
45
- QEMUClockType type, int64_t ns)
34
+ *
46
+void coroutine_fn qemu_co_sleep(QemuCoSleep *w)
35
+ * Two coroutines should be able to invoke each other via aio_co_enter() many
47
{
36
+ * times without hitting a limit like stack exhaustion. In other words, the
48
Coroutine *co = qemu_coroutine_self();
37
+ * calls should be chained instead of nested.
49
- AioContext *ctx = qemu_get_current_aio_context();
38
+ */
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
}
39
+
66
+
40
+typedef struct {
67
+void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
41
+ Coroutine *other;
68
+ QEMUClockType type, int64_t ns)
42
+ unsigned i;
69
+{
43
+ unsigned max;
70
+ AioContext *ctx = qemu_get_current_aio_context();
44
+} ChainData;
71
+ QEMUTimer ts;
45
+
72
+
46
+static void coroutine_fn chain(void *opaque)
73
+ aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w);
47
+{
74
+ timer_mod(&ts, qemu_clock_get_ns(type) + ns);
48
+ ChainData *data = opaque;
49
+
75
+
50
+ for (data->i = 0; data->i < data->max; data->i++) {
76
+ /*
51
+ /* Queue up the other coroutine... */
77
+ * The timer will fire in the current AiOContext, so the callback
52
+ aio_co_enter(ctx, data->other);
78
+ * must happen after qemu_co_sleep yields and there is no race
53
+
79
+ * between timer_mod and qemu_co_sleep.
54
+ /* ...and give control to it */
80
+ */
55
+ qemu_coroutine_yield();
81
+ qemu_co_sleep(w);
56
+ }
82
+ timer_del(&ts);
57
+}
83
+}
58
+
59
+static void test_queue_chaining(void)
60
+{
61
+ /* This number of iterations hit stack exhaustion in the past: */
62
+ ChainData data_a = { .max = 25000 };
63
+ ChainData data_b = { .max = 25000 };
64
+
65
+ data_b.other = qemu_coroutine_create(chain, &data_a);
66
+ data_a.other = qemu_coroutine_create(chain, &data_b);
67
+
68
+ qemu_coroutine_enter(data_b.other);
69
+
70
+ g_assert_cmpint(data_a.i, ==, data_a.max);
71
+ g_assert_cmpint(data_b.i, ==, data_b.max - 1);
72
+
73
+ /* Allow the second coroutine to terminate */
74
+ qemu_coroutine_enter(data_a.other);
75
+
76
+ g_assert_cmpint(data_b.i, ==, data_b.max);
77
+}
78
79
/* End of tests. */
80
81
int main(int argc, char **argv)
82
{
83
- Error *local_error = NULL;
84
- GSource *src;
85
-
86
- init_clocks(NULL);
87
-
88
- ctx = aio_context_new(&local_error);
89
- if (!ctx) {
90
- error_reportf_err(local_error, "Failed to create AIO Context: ");
91
- exit(1);
92
- }
93
- src = aio_get_g_source(ctx);
94
- g_source_attach(src, NULL);
95
- g_source_unref(src);
96
+ qemu_init_main_loop(&error_fatal);
97
+ ctx = qemu_get_aio_context();
98
99
while (g_main_context_iteration(NULL, false));
100
101
@@ -XXX,XX +XXX,XX @@ int main(int argc, char **argv)
102
g_test_add_func("/aio/external-client", test_aio_external_client);
103
g_test_add_func("/aio/timer/schedule", test_timer_schedule);
104
105
+ g_test_add_func("/aio/coroutine/queue-chaining", test_queue_chaining);
106
+
107
g_test_add_func("/aio-gsource/flush", test_source_flush);
108
g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
109
g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
110
--
84
--
111
2.14.3
85
2.31.1
112
86
113
diff view generated by jsdifflib