When ->poll() succeeds the AioHandler is placed on the ready list with
revents set to the magic value 0. This magic value causes
aio_dispatch_handler() to invoke ->poll_ready() instead of ->io_read()
for G_IO_IN or ->io_write() for G_IO_OUT.
This magic value 0 hack works for the IOThread where AioHandlers are
placed on ->ready_list and processed by aio_dispatch_ready_handlers().
It does not work for the main loop where all AioHandlers are processed
by aio_dispatch_handlers(), even those that are not ready and have a
revents value of 0.
As a result the main loop invokes ->poll_ready() on AioHandlers that are
not ready. These spurious ->poll_ready() calls waste CPU cycles and
could lead to crashes if the code assumes ->poll() must have succeeded
before ->poll_ready() is called (a reasonable asumption but I haven't
seen it in practice).
Stop using revents to track whether ->poll_ready() will be called on an
AioHandler. Introduce a separate AioHandler->poll_ready field instead.
This eliminates spurious ->poll_ready() calls in the main loop.
Fixes: 826cc32423db2a99d184dbf4f507c737d7e7a4ae ("aio-posix: split poll check from ready handler")
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Reported-by: Jason Wang <jasowang@redhat.com>
Tested-by: Jason Wang <jasowang@redhat.com>
Message-id: 20220223155703.136833-1-stefanha@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
		
	
			
		
			
				
	
	
		
			84 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/*
 | 
						|
 * AioContext POSIX event loop implementation internal APIs
 | 
						|
 *
 | 
						|
 * Copyright IBM, Corp. 2008
 | 
						|
 * Copyright Red Hat, Inc. 2020
 | 
						|
 *
 | 
						|
 * Authors:
 | 
						|
 *  Anthony Liguori   <aliguori@us.ibm.com>
 | 
						|
 *
 | 
						|
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 | 
						|
 * the COPYING file in the top-level directory.
 | 
						|
 *
 | 
						|
 * Contributions after 2012-01-13 are licensed under the terms of the
 | 
						|
 * GNU GPL, version 2 or (at your option) any later version.
 | 
						|
 */
 | 
						|
 | 
						|
#ifndef AIO_POSIX_H
 | 
						|
#define AIO_POSIX_H
 | 
						|
 | 
						|
#include "block/aio.h"
 | 
						|
 | 
						|
struct AioHandler {
 | 
						|
    GPollFD pfd;
 | 
						|
    IOHandler *io_read;
 | 
						|
    IOHandler *io_write;
 | 
						|
    AioPollFn *io_poll;
 | 
						|
    IOHandler *io_poll_ready;
 | 
						|
    IOHandler *io_poll_begin;
 | 
						|
    IOHandler *io_poll_end;
 | 
						|
    void *opaque;
 | 
						|
    QLIST_ENTRY(AioHandler) node;
 | 
						|
    QLIST_ENTRY(AioHandler) node_ready; /* only used during aio_poll() */
 | 
						|
    QLIST_ENTRY(AioHandler) node_deleted;
 | 
						|
    QLIST_ENTRY(AioHandler) node_poll;
 | 
						|
#ifdef CONFIG_LINUX_IO_URING
 | 
						|
    QSLIST_ENTRY(AioHandler) node_submitted;
 | 
						|
    unsigned flags; /* see fdmon-io_uring.c */
 | 
						|
#endif
 | 
						|
    int64_t poll_idle_timeout; /* when to stop userspace polling */
 | 
						|
    bool poll_ready; /* has polling detected an event? */
 | 
						|
    bool is_external;
 | 
						|
};
 | 
						|
 | 
						|
/* Add a handler to a ready list */
 | 
						|
void aio_add_ready_handler(AioHandlerList *ready_list, AioHandler *node,
 | 
						|
                           int revents);
 | 
						|
 | 
						|
extern const FDMonOps fdmon_poll_ops;
 | 
						|
 | 
						|
#ifdef CONFIG_EPOLL_CREATE1
 | 
						|
bool fdmon_epoll_try_upgrade(AioContext *ctx, unsigned npfd);
 | 
						|
void fdmon_epoll_setup(AioContext *ctx);
 | 
						|
void fdmon_epoll_disable(AioContext *ctx);
 | 
						|
#else
 | 
						|
static inline bool fdmon_epoll_try_upgrade(AioContext *ctx, unsigned npfd)
 | 
						|
{
 | 
						|
    return false;
 | 
						|
}
 | 
						|
 | 
						|
static inline void fdmon_epoll_setup(AioContext *ctx)
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
static inline void fdmon_epoll_disable(AioContext *ctx)
 | 
						|
{
 | 
						|
}
 | 
						|
#endif /* !CONFIG_EPOLL_CREATE1 */
 | 
						|
 | 
						|
#ifdef CONFIG_LINUX_IO_URING
 | 
						|
bool fdmon_io_uring_setup(AioContext *ctx);
 | 
						|
void fdmon_io_uring_destroy(AioContext *ctx);
 | 
						|
#else
 | 
						|
static inline bool fdmon_io_uring_setup(AioContext *ctx)
 | 
						|
{
 | 
						|
    return false;
 | 
						|
}
 | 
						|
 | 
						|
static inline void fdmon_io_uring_destroy(AioContext *ctx)
 | 
						|
{
 | 
						|
}
 | 
						|
#endif /* !CONFIG_LINUX_IO_URING */
 | 
						|
 | 
						|
#endif /* AIO_POSIX_H */
 |