]> git.proxmox.com Git - mirror_qemu.git/commit
nbd/server: only traverse NBDExport->clients from main loop thread
authorStefan Hajnoczi <stefanha@redhat.com>
Thu, 21 Dec 2023 19:24:51 +0000 (14:24 -0500)
committerKevin Wolf <kwolf@redhat.com>
Thu, 21 Dec 2023 20:59:07 +0000 (21:59 +0100)
commitf816310d0c32c8482e56807ea0f9faa8d1b5f696
tree850d6fcfbb4b8a05d232f1004a6077e0f230a146
parentefade66d5872e3bfb906944105145007ff2e26a3
nbd/server: only traverse NBDExport->clients from main loop thread

The NBD clients list is currently accessed from both the export
AioContext and the main loop thread. When the AioContext lock is removed
there will be nothing protecting the clients list.

Adding a lock around the clients list is tricky because NBDClient
structs are refcounted and may be freed from the export AioContext or
the main loop thread. nbd_export_request_shutdown() -> client_close() ->
nbd_client_put() is also tricky because the list lock would be held
while indirectly dropping references to NDBClients.

A simpler approach is to only allow nbd_client_put() and client_close()
calls from the main loop thread. Then the NBD clients list is only
accessed from the main loop thread and no fancy locking is needed.

nbd_trip() just needs to reschedule itself in the main loop AioContext
before calling nbd_client_put() and client_close(). This costs more CPU
cycles per NBD request so add nbd_client_put_nonzero() to optimize the
common case where more references to NBDClient remain.

Note that nbd_client_get() can still be called from either thread, so
make NBDClient->refcount atomic.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-ID: <20231221192452.1785567-6-stefanha@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
nbd/server.c