Pull request

(Resent due to an email preparation mistake.)
 -----BEGIN PGP SIGNATURE-----
 
 iQEzBAABCAAdFiEEhpWov9P5fNqsNXdanKSrs4Grc8gFAmCro7YACgkQnKSrs4Gr
 c8g3TQgAkdY2B+YgxkAwcsBYyPi6CCHEft46KiBprhb3v0GrdV2dSyFz3Wf/TiOI
 y0IqPVahI2WeEmw55o3I62Xyl9Hgw+z1kJeUJXJdZtfqP3UYgoL3tgkCth7KrRZS
 QUrOd6X6Yem+YTxtfAwo9NBpYiJVeLgUoJNQShP+M8UqBOMX/SFcp6eRvRm1d6uA
 RCPRufPGwhJicqZzQmF4Haepn+yg9TRAaJUO9iWwj4HAeIHCXrbpcaJ59yvP6L4I
 ObJSZAe1ERB2ugLKT8e1BibY2BZUKymp8lk+00yZ/O7PM98m8vPXUaoQlqA4Sv+4
 IiKaVeGlWhyeEF70wRRRDBtqagTxlA==
 =lWeh
 -----END PGP SIGNATURE-----

Merge remote-tracking branch 'remotes/stefanha-gitlab/tags/block-pull-request' into staging

Pull request

(Resent due to an email preparation mistake.)

# gpg: Signature made Mon 24 May 2021 14:01:42 BST
# gpg:                using RSA key 8695A8BFD3F97CDAAC35775A9CA4ABB381AB73C8
# gpg: Good signature from "Stefan Hajnoczi <stefanha@redhat.com>" [full]
# gpg:                 aka "Stefan Hajnoczi <stefanha@gmail.com>" [full]
# Primary key fingerprint: 8695 A8BF D3F9 7CDA AC35  775A 9CA4 ABB3 81AB 73C8

* remotes/stefanha-gitlab/tags/block-pull-request:
  coroutine-sleep: introduce qemu_co_sleep
  coroutine-sleep: replace QemuCoSleepState pointer with struct in the API
  coroutine-sleep: move timer out of QemuCoSleepState
  coroutine-sleep: allow qemu_co_sleep_wake that wakes nothing
  coroutine-sleep: disallow NULL QemuCoSleepState** argument
  coroutine-sleep: use a stack-allocated timer
  bitops.h: Improve find_xxx_bit() documentation
  multi-process: Initialize variables declared with g_auto*

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Peter Maydell 2021-05-24 15:48:08 +01:00
commit 0dab1d36f5
7 changed files with 80 additions and 71 deletions

View file

@ -50,7 +50,7 @@ typedef struct BlockCopyCallState {
/* State */ /* State */
int ret; int ret;
bool finished; bool finished;
QemuCoSleepState *sleep_state; QemuCoSleep sleep;
bool cancelled; bool cancelled;
/* OUT parameters */ /* OUT parameters */
@ -625,8 +625,8 @@ block_copy_dirty_clusters(BlockCopyCallState *call_state)
if (ns > 0) { if (ns > 0) {
block_copy_task_end(task, -EAGAIN); block_copy_task_end(task, -EAGAIN);
g_free(task); g_free(task);
qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, ns, qemu_co_sleep_ns_wakeable(&call_state->sleep,
&call_state->sleep_state); QEMU_CLOCK_REALTIME, ns);
continue; continue;
} }
} }
@ -674,9 +674,7 @@ out:
void block_copy_kick(BlockCopyCallState *call_state) void block_copy_kick(BlockCopyCallState *call_state)
{ {
if (call_state->sleep_state) { qemu_co_sleep_wake(&call_state->sleep);
qemu_co_sleep_wake(call_state->sleep_state);
}
} }
/* /*

View file

@ -116,7 +116,7 @@ typedef struct BDRVNBDState {
CoQueue free_sema; CoQueue free_sema;
Coroutine *connection_co; Coroutine *connection_co;
Coroutine *teardown_co; Coroutine *teardown_co;
QemuCoSleepState *connection_co_sleep_ns_state; QemuCoSleep reconnect_sleep;
bool drained; bool drained;
bool wait_drained_end; bool wait_drained_end;
int in_flight; int in_flight;
@ -289,9 +289,7 @@ static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs)
BDRVNBDState *s = (BDRVNBDState *)bs->opaque; BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
s->drained = true; s->drained = true;
if (s->connection_co_sleep_ns_state) { qemu_co_sleep_wake(&s->reconnect_sleep);
qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
}
nbd_co_establish_connection_cancel(bs, false); nbd_co_establish_connection_cancel(bs, false);
@ -330,9 +328,7 @@ static void nbd_teardown_connection(BlockDriverState *bs)
s->state = NBD_CLIENT_QUIT; s->state = NBD_CLIENT_QUIT;
if (s->connection_co) { if (s->connection_co) {
if (s->connection_co_sleep_ns_state) { qemu_co_sleep_wake(&s->reconnect_sleep);
qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
}
nbd_co_establish_connection_cancel(bs, true); nbd_co_establish_connection_cancel(bs, true);
} }
if (qemu_in_coroutine()) { if (qemu_in_coroutine()) {
@ -689,8 +685,8 @@ static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s)
} }
bdrv_inc_in_flight(s->bs); bdrv_inc_in_flight(s->bs);
} else { } else {
qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout, qemu_co_sleep_ns_wakeable(&s->reconnect_sleep,
&s->connection_co_sleep_ns_state); QEMU_CLOCK_REALTIME, timeout);
if (s->drained) { if (s->drained) {
continue; continue;
} }

View file

@ -41,10 +41,9 @@ void remote_sysmem_reconfig(MPQemuMsg *msg, Error **errp)
remote_sysmem_reset(); remote_sysmem_reset();
for (region = 0; region < msg->num_fds; region++) { for (region = 0; region < msg->num_fds; region++, suffix++) {
g_autofree char *name; g_autofree char *name = g_strdup_printf("remote-mem-%u", suffix);
subregion = g_new(MemoryRegion, 1); subregion = g_new(MemoryRegion, 1);
name = g_strdup_printf("remote-mem-%u", suffix++);
memory_region_init_ram_from_fd(subregion, NULL, memory_region_init_ram_from_fd(subregion, NULL,
name, sysmem_info->sizes[region], name, sysmem_info->sizes[region],
true, msg->fds[region], true, msg->fds[region],

View file

@ -347,13 +347,12 @@ static void probe_pci_info(PCIDevice *dev, Error **errp)
PCI_BASE_ADDRESS_SPACE_IO : PCI_BASE_ADDRESS_SPACE_MEMORY; PCI_BASE_ADDRESS_SPACE_IO : PCI_BASE_ADDRESS_SPACE_MEMORY;
if (size) { if (size) {
g_autofree char *name; g_autofree char *name = g_strdup_printf("bar-region-%d", i);
pdev->region[i].dev = pdev; pdev->region[i].dev = pdev;
pdev->region[i].present = true; pdev->region[i].present = true;
if (type == PCI_BASE_ADDRESS_SPACE_MEMORY) { if (type == PCI_BASE_ADDRESS_SPACE_MEMORY) {
pdev->region[i].memory = true; pdev->region[i].memory = true;
} }
name = g_strdup_printf("bar-region-%d", i);
memory_region_init_io(&pdev->region[i].mr, OBJECT(pdev), memory_region_init_io(&pdev->region[i].mr, OBJECT(pdev),
&proxy_mr_ops, &pdev->region[i], &proxy_mr_ops, &pdev->region[i],
name, size); name, size);

View file

@ -140,7 +140,8 @@ static inline int test_bit(long nr, const unsigned long *addr)
* @addr: The address to start the search at * @addr: The address to start the search at
* @size: The maximum size to search * @size: The maximum size to search
* *
* Returns the bit number of the first set bit, or size. * Returns the bit number of the last set bit,
* or @size if there is no set bit in the bitmap.
*/ */
unsigned long find_last_bit(const unsigned long *addr, unsigned long find_last_bit(const unsigned long *addr,
unsigned long size); unsigned long size);
@ -150,6 +151,9 @@ unsigned long find_last_bit(const unsigned long *addr,
* @addr: The address to base the search on * @addr: The address to base the search on
* @offset: The bitnumber to start searching at * @offset: The bitnumber to start searching at
* @size: The bitmap size in bits * @size: The bitmap size in bits
*
* Returns the bit number of the next set bit,
* or @size if there are no further set bits in the bitmap.
*/ */
unsigned long find_next_bit(const unsigned long *addr, unsigned long find_next_bit(const unsigned long *addr,
unsigned long size, unsigned long size,
@ -160,6 +164,9 @@ unsigned long find_next_bit(const unsigned long *addr,
* @addr: The address to base the search on * @addr: The address to base the search on
* @offset: The bitnumber to start searching at * @offset: The bitnumber to start searching at
* @size: The bitmap size in bits * @size: The bitmap size in bits
*
* Returns the bit number of the next cleared bit,
* or @size if there are no further clear bits in the bitmap.
*/ */
unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long find_next_zero_bit(const unsigned long *addr,
@ -171,7 +178,8 @@ unsigned long find_next_zero_bit(const unsigned long *addr,
* @addr: The address to start the search at * @addr: The address to start the search at
* @size: The maximum size to search * @size: The maximum size to search
* *
* Returns the bit number of the first set bit. * Returns the bit number of the first set bit,
* or @size if there is no set bit in the bitmap.
*/ */
static inline unsigned long find_first_bit(const unsigned long *addr, static inline unsigned long find_first_bit(const unsigned long *addr,
unsigned long size) unsigned long size)
@ -194,7 +202,8 @@ static inline unsigned long find_first_bit(const unsigned long *addr,
* @addr: The address to start the search at * @addr: The address to start the search at
* @size: The maximum size to search * @size: The maximum size to search
* *
* Returns the bit number of the first cleared bit. * Returns the bit number of the first cleared bit,
* or @size if there is no clear bit in the bitmap.
*/ */
static inline unsigned long find_first_zero_bit(const unsigned long *addr, static inline unsigned long find_first_zero_bit(const unsigned long *addr,
unsigned long size) unsigned long size)

View file

@ -291,20 +291,27 @@ void qemu_co_rwlock_wrlock(CoRwlock *lock);
*/ */
void qemu_co_rwlock_unlock(CoRwlock *lock); void qemu_co_rwlock_unlock(CoRwlock *lock);
typedef struct QemuCoSleepState QemuCoSleepState; typedef struct QemuCoSleep {
Coroutine *to_wake;
} QemuCoSleep;
/** /**
* Yield the coroutine for a given duration. During this yield, @sleep_state * Yield the coroutine for a given duration. Initializes @w so that,
* (if not NULL) is set to an opaque pointer, which may be used for * during this yield, it can be passed to qemu_co_sleep_wake() to
* qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when the * terminate the sleep.
* timer fires. Don't save the obtained value to other variables and don't call
* qemu_co_sleep_wake from another aio context.
*/ */
void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
QemuCoSleepState **sleep_state); QEMUClockType type, int64_t ns);
/**
* Yield the coroutine until the next call to qemu_co_sleep_wake.
*/
void coroutine_fn qemu_co_sleep(QemuCoSleep *w);
static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
{ {
qemu_co_sleep_ns_wakeable(type, ns, NULL); QemuCoSleep w = { 0 };
qemu_co_sleep_ns_wakeable(&w, type, ns);
} }
/** /**
@ -313,7 +320,7 @@ static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
* qemu_co_sleep_ns() and should be checked to be non-NULL before calling * qemu_co_sleep_ns() and should be checked to be non-NULL before calling
* qemu_co_sleep_wake(). * qemu_co_sleep_wake().
*/ */
void qemu_co_sleep_wake(QemuCoSleepState *sleep_state); void qemu_co_sleep_wake(QemuCoSleep *w);
/** /**
* Yield until a file descriptor becomes readable * Yield until a file descriptor becomes readable

View file

@ -19,43 +19,34 @@
static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns"; static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
struct QemuCoSleepState { void qemu_co_sleep_wake(QemuCoSleep *w)
Coroutine *co;
QEMUTimer *ts;
QemuCoSleepState **user_state_pointer;
};
void qemu_co_sleep_wake(QemuCoSleepState *sleep_state)
{ {
/* Write of schedule protected by barrier write in aio_co_schedule */ Coroutine *co;
const char *scheduled = qatomic_cmpxchg(&sleep_state->co->scheduled,
qemu_co_sleep_ns__scheduled, NULL);
assert(scheduled == qemu_co_sleep_ns__scheduled); co = w->to_wake;
if (sleep_state->user_state_pointer) { w->to_wake = NULL;
*sleep_state->user_state_pointer = NULL; if (co) {
/* Write of schedule protected by barrier write in aio_co_schedule */
const char *scheduled = qatomic_cmpxchg(&co->scheduled,
qemu_co_sleep_ns__scheduled, NULL);
assert(scheduled == qemu_co_sleep_ns__scheduled);
aio_co_wake(co);
} }
timer_del(sleep_state->ts);
aio_co_wake(sleep_state->co);
} }
static void co_sleep_cb(void *opaque) static void co_sleep_cb(void *opaque)
{ {
qemu_co_sleep_wake(opaque); QemuCoSleep *w = opaque;
qemu_co_sleep_wake(w);
} }
void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, void coroutine_fn qemu_co_sleep(QemuCoSleep *w)
QemuCoSleepState **sleep_state)
{ {
AioContext *ctx = qemu_get_current_aio_context(); Coroutine *co = qemu_coroutine_self();
QemuCoSleepState state = {
.co = qemu_coroutine_self(),
.ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &state),
.user_state_pointer = sleep_state,
};
const char *scheduled = qatomic_cmpxchg(&state.co->scheduled, NULL, const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
qemu_co_sleep_ns__scheduled); qemu_co_sleep_ns__scheduled);
if (scheduled) { if (scheduled) {
fprintf(stderr, fprintf(stderr,
"%s: Co-routine was already scheduled in '%s'\n", "%s: Co-routine was already scheduled in '%s'\n",
@ -63,17 +54,27 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns,
abort(); abort();
} }
if (sleep_state) { w->to_wake = co;
*sleep_state = &state;
}
timer_mod(state.ts, qemu_clock_get_ns(type) + ns);
qemu_coroutine_yield(); qemu_coroutine_yield();
if (sleep_state) {
/* /* w->to_wake is cleared before resuming this coroutine. */
* Note that *sleep_state is cleared during qemu_co_sleep_wake assert(w->to_wake == NULL);
* before resuming this coroutine. }
*/
assert(*sleep_state == NULL); void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
} QEMUClockType type, int64_t ns)
timer_free(state.ts); {
AioContext *ctx = qemu_get_current_aio_context();
QEMUTimer ts;
aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w);
timer_mod(&ts, qemu_clock_get_ns(type) + ns);
/*
* The timer will fire in the current AiOContext, so the callback
* must happen after qemu_co_sleep yields and there is no race
* between timer_mod and qemu_co_sleep.
*/
qemu_co_sleep(w);
timer_del(&ts);
} }