| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * Test that poll handlers are not re-entrant in nested aio_poll() |
| 4 | * |
| 5 | * Copyright Red Hat |
| 6 | * |
| 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 |
| 10 | * recursion occurs. |
| 11 | * |
| 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. |
| 14 | */ |
| 15 | #include "qemu/osdep.h" |
| 16 | #include "qemu/aio.h" |
| 17 | #include "qapi/error.h" |
| 18 | #include "util/aio-posix.h" |
| 19 | |
| 20 | typedef struct { |
| 21 | AioContext *ctx; |
| 22 | |
| 23 | /* This is the EventNotifier that drives the test */ |
| 24 | EventNotifier poll_notifier; |
| 25 | |
| 26 | /* This EventNotifier is only used to wake aio_poll() */ |
| 27 | EventNotifier dummy_notifier; |
| 28 | |
| 29 | bool nested; |
| 30 | } TestData; |
| 31 | |
| 32 | static void io_read(EventNotifier *notifier) |
| 33 | { |
| 34 | event_notifier_test_and_clear(notifier); |
| 35 | } |
| 36 | |
| 37 | static bool io_poll_true(void *opaque) |
| 38 | { |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | static bool io_poll_false(void *opaque) |
| 43 | { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | static void io_poll_ready(EventNotifier *notifier) |
| 48 | { |
| 49 | TestData *td = container_of(notifier, TestData, poll_notifier); |
| 50 | |
| 51 | g_assert(!td->nested); |
| 52 | td->nested = true; |
| 53 | |
| 54 | /* Wake the following nested aio_poll() call */ |
| 55 | event_notifier_set(&td->dummy_notifier); |
| 56 | |
| 57 | /* This nested event loop must not call io_poll()/io_poll_ready() */ |
| 58 | g_assert(aio_poll(td->ctx, true)); |
| 59 | |
| 60 | td->nested = false; |
| 61 | } |
| 62 | |
| 63 | /* dummy_notifier never triggers */ |
| 64 | static void io_poll_never_ready(EventNotifier *notifier) |
| 65 | { |
| 66 | g_assert_not_reached(); |
| 67 | } |
| 68 | |
| 69 | static void test(void) |
| 70 | { |
| 71 | TestData td = { |
| 72 | .ctx = aio_context_new(&error_abort), |
| 73 | }; |
| 74 | |
| 75 | if (td.ctx->fdmon_ops != &fdmon_poll_ops) { |
| 76 | /* This test is tied to fdmon-poll.c */ |
| 77 | g_test_skip("fdmon_poll_ops not in use"); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | qemu_set_current_aio_context(td.ctx); |
| 82 | |
| 83 | /* Enable polling */ |
| 84 | aio_context_set_poll_params(td.ctx, 1000000, 2, 2, 3, &error_abort); |
| 85 | |
| 86 | /* Make the event notifier active (set) right away */ |
| 87 | event_notifier_init(&td.poll_notifier, 1); |
| 88 | aio_set_event_notifier(td.ctx, &td.poll_notifier, |
| 89 | io_read, io_poll_true, io_poll_ready); |
| 90 | |
| 91 | /* This event notifier will be used later */ |
| 92 | event_notifier_init(&td.dummy_notifier, 0); |
| 93 | aio_set_event_notifier(td.ctx, &td.dummy_notifier, |
| 94 | io_read, io_poll_false, io_poll_never_ready); |
| 95 | |
| 96 | /* Consume aio_notify() */ |
| 97 | g_assert(!aio_poll(td.ctx, false)); |
| 98 | |
| 99 | /* |
| 100 | * Run the io_read() handler. This has the side-effect of activating |
| 101 | * polling in future aio_poll() calls. |
| 102 | */ |
| 103 | g_assert(aio_poll(td.ctx, true)); |
| 104 | |
| 105 | /* The second time around the io_poll()/io_poll_ready() handler runs */ |
| 106 | g_assert(aio_poll(td.ctx, true)); |
| 107 | |
| 108 | /* Run io_poll()/io_poll_ready() one more time to show it keeps working */ |
| 109 | g_assert(aio_poll(td.ctx, true)); |
| 110 | |
| 111 | aio_set_event_notifier(td.ctx, &td.dummy_notifier, NULL, NULL, NULL); |
| 112 | aio_set_event_notifier(td.ctx, &td.poll_notifier, NULL, NULL, NULL); |
| 113 | event_notifier_cleanup(&td.dummy_notifier); |
| 114 | event_notifier_cleanup(&td.poll_notifier); |
| 115 | aio_context_unref(td.ctx); |
| 116 | } |
| 117 | |
| 118 | int main(int argc, char **argv) |
| 119 | { |
| 120 | g_test_init(&argc, &argv, NULL); |
| 121 | g_test_add_func("/nested-aio-poll", test); |
| 122 | return g_test_run(); |
| 123 | } |