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