Skip to content
Snippets Groups Projects
  • Stefan Hajnoczi's avatar
    fc879646
    aio-posix: fix spurious ->poll_ready() callbacks in main loop · fc879646
    Stefan Hajnoczi authored
    
    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: 826cc324 ("aio-posix: split poll check from ready handler")
    Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
    Reported-by: default avatarJason Wang <jasowang@redhat.com>
    Tested-by: default avatarJason Wang <jasowang@redhat.com>
    Message-id: 20220223155703.136833-1-stefanha@redhat.com
    Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
    fc879646
    History
    aio-posix: fix spurious ->poll_ready() callbacks in main loop
    Stefan Hajnoczi authored
    
    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: 826cc324 ("aio-posix: split poll check from ready handler")
    Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
    Reported-by: default avatarJason Wang <jasowang@redhat.com>
    Tested-by: default avatarJason Wang <jasowang@redhat.com>
    Message-id: 20220223155703.136833-1-stefanha@redhat.com
    Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>