1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Test that poll handlers are not re-entrant in nested aio_poll()
7 * Poll handlers are usually level-triggered. That means they continue firing
8 * until the condition is reset (e.g. a virtqueue becomes empty). If a poll
9 * handler calls nested aio_poll() before the condition is reset, then infinite
12 * aio_poll() is supposed to prevent this by disabling poll handlers in nested
13 * aio_poll() calls. This test case checks that this is indeed what happens.
15 #include "qemu/osdep.h"
16 #include "block/aio.h"
17 #include "qapi/error.h"
22 /* This is the EventNotifier that drives the test */
23 EventNotifier poll_notifier
;
25 /* This EventNotifier is only used to wake aio_poll() */
26 EventNotifier dummy_notifier
;
31 static void io_read(EventNotifier
*notifier
)
33 fprintf(stderr
, "%s %p\n", __func__
, notifier
);
34 event_notifier_test_and_clear(notifier
);
37 static bool io_poll_true(void *opaque
)
39 fprintf(stderr
, "%s %p\n", __func__
, opaque
);
43 static bool io_poll_false(void *opaque
)
45 fprintf(stderr
, "%s %p\n", __func__
, opaque
);
49 static void io_poll_ready(EventNotifier
*notifier
)
51 TestData
*td
= container_of(notifier
, TestData
, poll_notifier
);
53 fprintf(stderr
, "> %s\n", __func__
);
55 g_assert(!td
->nested
);
58 /* Wake the following nested aio_poll() call */
59 event_notifier_set(&td
->dummy_notifier
);
61 /* This nested event loop must not call io_poll()/io_poll_ready() */
62 g_assert(aio_poll(td
->ctx
, true));
66 fprintf(stderr
, "< %s\n", __func__
);
69 /* dummy_notifier never triggers */
70 static void io_poll_never_ready(EventNotifier
*notifier
)
72 g_assert_not_reached();
75 static void test(void)
78 .ctx
= aio_context_new(&error_abort
),
81 qemu_set_current_aio_context(td
.ctx
);
84 aio_context_set_poll_params(td
.ctx
, 1000000, 2, 2, &error_abort
);
87 * The GSource is unused but this has the side-effect of changing the fdmon
88 * that AioContext uses.
90 aio_get_g_source(td
.ctx
);
92 /* Make the event notifier active (set) right away */
93 event_notifier_init(&td
.poll_notifier
, 1);
94 aio_set_event_notifier(td
.ctx
, &td
.poll_notifier
,
95 io_read
, io_poll_true
, io_poll_ready
);
97 /* This event notifier will be used later */
98 event_notifier_init(&td
.dummy_notifier
, 0);
99 aio_set_event_notifier(td
.ctx
, &td
.dummy_notifier
,
100 io_read
, io_poll_false
, io_poll_never_ready
);
102 /* Consume aio_notify() */
103 g_assert(!aio_poll(td
.ctx
, false));
106 * Run the io_read() handler. This has the side-effect of activating
107 * polling in future aio_poll() calls.
109 g_assert(aio_poll(td
.ctx
, true));
111 /* The second time around the io_poll()/io_poll_ready() handler runs */
112 g_assert(aio_poll(td
.ctx
, true));
114 /* Run io_poll()/io_poll_ready() one more time to show it keeps working */
115 g_assert(aio_poll(td
.ctx
, true));
117 aio_set_event_notifier(td
.ctx
, &td
.dummy_notifier
, NULL
, NULL
, NULL
);
118 aio_set_event_notifier(td
.ctx
, &td
.poll_notifier
, NULL
, NULL
, NULL
);
119 event_notifier_cleanup(&td
.dummy_notifier
);
120 event_notifier_cleanup(&td
.poll_notifier
);
121 aio_context_unref(td
.ctx
);
124 int main(int argc
, char **argv
)
126 g_test_init(&argc
, &argv
, NULL
);
127 g_test_add_func("/nested-aio-poll", test
);