/src/qemu/util/fdmon-poll.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
2 | | /* |
3 | | * poll(2) file descriptor monitoring |
4 | | * |
5 | | * Uses ppoll(2) when available, g_poll() otherwise. |
6 | | */ |
7 | | |
8 | | #include "qemu/osdep.h" |
9 | | #include "aio-posix.h" |
10 | | #include "qemu/rcu_queue.h" |
11 | | |
12 | | /* |
13 | | * These thread-local variables are used only in fdmon_poll_wait() around the |
14 | | * call to the poll() system call. In particular they are not used while |
15 | | * aio_poll is performing callbacks, which makes it much easier to think about |
16 | | * reentrancy! |
17 | | * |
18 | | * Stack-allocated arrays would be perfect but they have size limitations; |
19 | | * heap allocation is expensive enough that we want to reuse arrays across |
20 | | * calls to aio_poll(). And because poll() has to be called without holding |
21 | | * any lock, the arrays cannot be stored in AioContext. Thread-local data |
22 | | * has none of the disadvantages of these three options. |
23 | | */ |
24 | | static __thread GPollFD *pollfds; |
25 | | static __thread AioHandler **nodes; |
26 | | static __thread unsigned npfd, nalloc; |
27 | | static __thread Notifier pollfds_cleanup_notifier; |
28 | | |
29 | | static void pollfds_cleanup(Notifier *n, void *unused) |
30 | 0 | { |
31 | 0 | g_assert(npfd == 0); |
32 | 0 | g_free(pollfds); |
33 | 0 | g_free(nodes); |
34 | 0 | nalloc = 0; |
35 | 0 | } |
36 | | |
37 | | static void add_pollfd(AioHandler *node) |
38 | 1.04M | { |
39 | 1.04M | if (npfd == nalloc) { |
40 | 4 | if (nalloc == 0) { |
41 | 3 | pollfds_cleanup_notifier.notify = pollfds_cleanup; |
42 | 3 | qemu_thread_atexit_add(&pollfds_cleanup_notifier); |
43 | 3 | nalloc = 8; |
44 | 3 | } else { |
45 | 1 | g_assert(nalloc <= INT_MAX); |
46 | 1 | nalloc *= 2; |
47 | 1 | } |
48 | 4 | pollfds = g_renew(GPollFD, pollfds, nalloc); |
49 | 4 | nodes = g_renew(AioHandler *, nodes, nalloc); |
50 | 4 | } |
51 | 1.04M | nodes[npfd] = node; |
52 | 1.04M | pollfds[npfd] = (GPollFD) { |
53 | 1.04M | .fd = node->pfd.fd, |
54 | 1.04M | .events = node->pfd.events, |
55 | 1.04M | }; |
56 | 1.04M | npfd++; |
57 | 1.04M | } |
58 | | |
59 | | static int fdmon_poll_wait(AioContext *ctx, AioHandlerList *ready_list, |
60 | | int64_t timeout) |
61 | 114k | { |
62 | 114k | AioHandler *node; |
63 | 114k | int ret; |
64 | | |
65 | 114k | assert(npfd == 0); |
66 | | |
67 | 1.05M | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
68 | 1.05M | if (!QLIST_IS_INSERTED(node, node_deleted) && node->pfd.events) { |
69 | 1.04M | add_pollfd(node); |
70 | 1.04M | } |
71 | 1.05M | } |
72 | | |
73 | | /* epoll(7) is faster above a certain number of fds */ |
74 | 229k | if (fdmon_epoll_try_upgrade(ctx, npfd)) { |
75 | 0 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
76 | 0 | if (!QLIST_IS_INSERTED(node, node_deleted) && node->pfd.events) { |
77 | 0 | g_source_remove_poll(&ctx->source, &node->pfd); |
78 | 0 | } |
79 | 0 | } |
80 | 0 | npfd = 0; /* we won't need pollfds[], reset npfd */ |
81 | 0 | return ctx->fdmon_ops->wait(ctx, ready_list, timeout); |
82 | 0 | } |
83 | | |
84 | 114k | ret = qemu_poll_ns(pollfds, npfd, timeout); |
85 | 114k | if (ret > 0) { |
86 | 17.2k | int i; |
87 | | |
88 | 43.7k | for (i = 0; i < npfd; i++) { |
89 | 26.4k | int revents = pollfds[i].revents; |
90 | | |
91 | 26.4k | if (revents) { |
92 | 19.9k | aio_add_ready_handler(ready_list, nodes[i], revents); |
93 | 19.9k | } |
94 | 26.4k | } |
95 | 17.2k | } |
96 | | |
97 | 114k | npfd = 0; |
98 | 114k | return ret; |
99 | 229k | } |
100 | | |
101 | | static void fdmon_poll_update(AioContext *ctx, |
102 | | AioHandler *old_node, |
103 | | AioHandler *new_node) |
104 | 3.15M | { |
105 | 3.15M | if (old_node) { |
106 | | /* |
107 | | * If the GSource is in the process of being destroyed then |
108 | | * g_source_remove_poll() causes an assertion failure. Skip removal in |
109 | | * that case, because glib cleans up its state during destruction |
110 | | * anyway. |
111 | | */ |
112 | 1.57M | if (!g_source_is_destroyed(&ctx->source)) { |
113 | 1.57M | g_source_remove_poll(&ctx->source, &old_node->pfd); |
114 | 1.57M | } |
115 | 1.57M | } |
116 | | |
117 | 3.15M | if (new_node) { |
118 | 1.57M | g_source_add_poll(&ctx->source, &new_node->pfd); |
119 | 1.57M | } |
120 | 3.15M | } |
121 | | |
122 | | static void fdmon_poll_gsource_prepare(AioContext *ctx) |
123 | 278M | { |
124 | | /* Do nothing */ |
125 | 278M | } |
126 | | |
127 | | static bool fdmon_poll_gsource_check(AioContext *ctx) |
128 | 216M | { |
129 | 216M | AioHandler *node; |
130 | 216M | bool result = false; |
131 | | |
132 | | /* |
133 | | * We have to walk very carefully in case aio_set_fd_handler is |
134 | | * called while we're walking. |
135 | | */ |
136 | 216M | qemu_lockcnt_inc(&ctx->list_lock); |
137 | | |
138 | 512M | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
139 | 512M | int revents = node->pfd.revents & node->pfd.events; |
140 | | |
141 | 512M | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) { |
142 | 240k | result = true; |
143 | 240k | break; |
144 | 240k | } |
145 | 512M | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) { |
146 | 0 | result = true; |
147 | 0 | break; |
148 | 0 | } |
149 | 512M | } |
150 | | |
151 | 216M | qemu_lockcnt_dec(&ctx->list_lock); |
152 | | |
153 | 216M | return result; |
154 | 216M | } |
155 | | |
156 | | static void fdmon_poll_gsource_dispatch(AioContext *ctx, |
157 | | AioHandlerList *ready_list) |
158 | 62.1M | { |
159 | 62.1M | AioHandler *node; |
160 | | |
161 | 65.4M | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
162 | 65.4M | int revents = node->pfd.revents; |
163 | | |
164 | 65.4M | if (revents) { |
165 | 1.19M | aio_add_ready_handler(ready_list, node, revents); |
166 | 1.19M | } |
167 | 65.4M | } |
168 | 62.1M | } |
169 | | |
170 | | const FDMonOps fdmon_poll_ops = { |
171 | | .update = fdmon_poll_update, |
172 | | .wait = fdmon_poll_wait, |
173 | | .need_wait = aio_poll_disabled, |
174 | | .gsource_prepare = fdmon_poll_gsource_prepare, |
175 | | .gsource_check = fdmon_poll_gsource_check, |
176 | | .gsource_dispatch = fdmon_poll_gsource_dispatch, |
177 | | }; |
178 | | |
179 | | void fdmon_poll_downgrade(AioContext *ctx) |
180 | 0 | { |
181 | 0 | AioHandler *node; |
182 | |
|
183 | 0 | ctx->fdmon_ops = &fdmon_poll_ops; |
184 | |
|
185 | 0 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
186 | 0 | if (!QLIST_IS_INSERTED(node, node_deleted) && node->pfd.events) { |
187 | 0 | g_source_add_poll(&ctx->source, &node->pfd); |
188 | 0 | } |
189 | 0 | } |
190 | 0 | } |