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     fprintf(stderr, "%s %p\n", __func__, notifier);
34     event_notifier_test_and_clear(notifier);
35 }
36 
37 static bool io_poll_true(void *opaque)
38 {
39     fprintf(stderr, "%s %p\n", __func__, opaque);
40     return true;
41 }
42 
43 static bool io_poll_false(void *opaque)
44 {
45     fprintf(stderr, "%s %p\n", __func__, opaque);
46     return false;
47 }
48 
49 static void io_poll_ready(EventNotifier *notifier)
50 {
51     TestData *td = container_of(notifier, TestData, poll_notifier);
52 
53     fprintf(stderr, "> %s\n", __func__);
54 
55     g_assert(!td->nested);
56     td->nested = true;
57 
58     /* Wake the following nested aio_poll() call */
59     event_notifier_set(&td->dummy_notifier);
60 
61     /* This nested event loop must not call io_poll()/io_poll_ready() */
62     g_assert(aio_poll(td->ctx, true));
63 
64     td->nested = false;
65 
66     fprintf(stderr, "< %s\n", __func__);
67 }
68 
69 /* dummy_notifier never triggers */
70 static void io_poll_never_ready(EventNotifier *notifier)
71 {
72     g_assert_not_reached();
73 }
74 
75 static void test(void)
76 {
77     TestData td = {
78         .ctx = aio_context_new(&error_abort),
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, &error_abort);
85 
86     /*
87      * The GSource is unused but this has the side-effect of changing the fdmon
88      * that AioContext uses.
89      */
90     aio_get_g_source(td.ctx);
91 
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);
96 
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);
101 
102     /* Consume aio_notify() */
103     g_assert(!aio_poll(td.ctx, false));
104 
105     /*
106      * Run the io_read() handler. This has the side-effect of activating
107      * polling in future aio_poll() calls.
108      */
109     g_assert(aio_poll(td.ctx, true));
110 
111     /* The second time around the io_poll()/io_poll_ready() handler runs */
112     g_assert(aio_poll(td.ctx, true));
113 
114     /* Run io_poll()/io_poll_ready() one more time to show it keeps working */
115     g_assert(aio_poll(td.ctx, true));
116 
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);
122 }
123 
124 int main(int argc, char **argv)
125 {
126     g_test_init(&argc, &argv, NULL);
127     g_test_add_func("/nested-aio-poll", test);
128     return g_test_run();
129 }
130