9pfs: v9fs_reclaim_fd() fixes
PATCH 2/2 9pfs: reduce latency of v9fs_reclaim_fd()
In reply to Christian SchoenebeckThis function calls v9fs_co_close() and v9fs_co_closedir() in a loop. Each one of the calls adds two thread hops (between main thread and a fs driver background thread). Each thread hop adds latency, which sums up in function's loop to a significant duration.
Reduce overall latency by open coding what v9fs_co_close() and v9fs_co_closedir() do, executing those and the loop itself altogether in only one background thread block, hence reducing the total amount of thread hops to only two.
--- hw/9pfs/9p.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
hw/9pfs/9p.c +17 -9
@@ -434,6 +434,7 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)V9fsFidState *f;GHashTableIter iter;gpointer fid; +int err;/* prevent multiple coroutines running this function simultaniously */ if (s->reclaiming) {
@@ -446,10 +447,10 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) QSLIST_HEAD(, V9fsFidState) reclaim_list =QSLIST_HEAD_INITIALIZER (reclaim_list); +/* Pick FIDs to be closed, collect them on reclaim_list. */ while (g_hash_table_iter_next (&iter, &fid, (gpointer *) &f)) {/* -* Unlink fids cannot be reclaimed. Check -* for them and skip them. Also skip fids +* Unlinked fids cannot be reclaimed, skip those, and also skip fids * currently being operated on. */ if (f->ref || f->flags &FID_NON_RECLAIMABLE ) {
@@ -499,17 +500,24 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) } }/* -* Now close the fid in reclaim list. Free them if they -* are already clunked. +* Close the picked FIDs altogether on a background I/O driver thread. Do +* this all at once to keep latency (i.e. amount of thread hops between main +* thread <-> fs driver background thread) as low as possible. */ +v9fs_co_run_in_worker ({ + QSLIST_FOREACH(f, &reclaim_list, reclaim_next) { + err = (f->fid_type ==P9_FID_DIR ) ? + s->ops->closedir (&s->ctx, &f->fs_reclaim) : + s->ops->close (&s->ctx, &f->fs_reclaim); +if (!err) { + total_open_fd--; + } + } + }); +/* Free the closed FIDs. */ while (!QSLIST_EMPTY (&reclaim_list)) { f =QSLIST_FIRST (&reclaim_list);QSLIST_REMOVE (&reclaim_list, f, V9fsFidState, reclaim_next); -if (f->fid_type ==P9_FID_FILE ) { -v9fs_co_close (pdu, &f->fs_reclaim); - }else if (f->fid_type ==P9_FID_DIR ) { -v9fs_co_closedir (pdu, &f->fs_reclaim); - }/* * Now drop the fid reference, free it * if clunked.
Signature · 1 line
2.39.5
cover 9pfs: v9fs_reclaim_fd() fixes
Two fixes for 9p server's v9fs_reclaim_fd() function:
* Patch 1 fixes a concurrency issue.
* Patch 2 optimizes overall latency. On a test machine with ~800,000 FIDs, this reduced execution duration of v9fs_reclaim_fd() from 30ms to 1ms.
Christian Schoenebeck (2):
9pfs: fix concurrent v9fs_reclaim_fd() calls
9pfs: reduce latency of v9fs_reclaim_fd()
hw/9pfs/9p.c | 36 +++++++++++++++++++++++++++--------- hw/9pfs/9p.h | 1 + 2 files changed, 28 insertions(+), 9 deletions(-)
Signature · 1 line
2.39.5
PATCH 1/2 9pfs: fix concurrent v9fs_reclaim_fd() calls
Even though this function is serialized to be always called from main thread, v9fs_reclaim_fd() is dispatching the coroutine to a worker thread in between via its v9fs_co_*() calls, hence leading to the situation where v9fs_reclaim_fd() is effectively executed multiple times simultaniously, which renders its LRU algorithm useless and causes high latency.
Fix this by adding a simple boolean variable to ensure this function is only called once at a time. No synchronization needed for this boolean variable as this function is only entered and returned on main thread.
--- hw/9pfs/9p.c | 10 ++++++++++ hw/9pfs/9p.h | 1 + 2 files changed, 11 insertions(+)
hw/9pfs/9p.c +10 -0
@@ -435,6 +435,12 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)GHashTableIter iter;gpointer fid; +/* prevent multiple coroutines running this function simultaniously */ +if (s->reclaiming) { +return ; + } + s->reclaiming = true; +g_hash_table_iter_init (&iter, s->fids); QSLIST_HEAD(, V9fsFidState) reclaim_list =
@@ -510,6 +516,8 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)*/ put_fid (pdu, f); } + + s->reclaiming = false; }/*
@@ -4324,6 +4332,8 @@ int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t, s->ctx.fst = &fse->fst;fsdev_throttle_init (s->ctx.fst); + s->reclaiming = false; + rc =0 ; out:if (rc) {
hw/9pfs/9p.h +1 -0
@@ -362,6 +362,7 @@ struct V9fsState {uint64_t qp_ndevices;/* Amount of entries in qpd_table. */ uint16_t qp_affix_next;uint64_t qp_fullpath_next; +bool reclaiming; };/* 9p2000.L open flags */
Signature · 1 line
2.39.5
Re: [PATCH 1/2] 9pfs: fix concurrent v9fs_reclaim_fd() calls
Hi Christian !
Quoted 6 lines · On Tue, 4 Mar 2025 16:15:57 +0100 Christian Schoenebeck…
Even though this function is serialized to be always called from main thread,
v9fs_reclaim_fd()is dispatching the coroutine to a worker thread in between via its v9fs_co_*() calls, hence leading to the situation wherev9fs_reclaim_fd()is effectively executed multiple times simultaniously, which renders its LRU algorithm useless and causes high latency.
Quoted 7 lines
Fix this by adding a simple boolean variable to ensure this function is only called once at a time. No synchronization needed for this boolean variable as this function is only entered and returned on main thread.
Fixes 7a46274529c ('hw/9pfs: Add file descriptor reclaim support')Signed-off-by Christian Schoenebeck <[email protected]>---
Another long long standing bug bites the dust ! Good catch !
Quoted 51 lines
hw/9pfs/9p.c | 10 ++++++++++ hw/9pfs/9p.h | 1 + 2 files changed, 11 insertions(+)hw/9pfs/9p.c +10
-0@@ -435,6 +435,12 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)GHashTableIter iter;gpointer fid; +/* prevent multiple coroutines running this function simultaniously */ +if (s->reclaiming) { +return ; + } + s->reclaiming = true; +g_hash_table_iter_init (&iter, s->fids); QSLIST_HEAD(, V9fsFidState) reclaim_list =@@ -510,6 +516,8 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)*/ put_fid (pdu, f); } + + s->reclaiming = false; }/* @@ -4324,6 +4332,8 @@ int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t, s->ctx.fst = &fse->fst;fsdev_throttle_init (s->ctx.fst); + s->reclaiming = false; + rc =0 ; out:if (rc) {hw/9pfs/9p.h +1
-0@@ -362,6 +362,7 @@ struct V9fsState {uint64_t qp_ndevices;/* Amount of entries in qpd_table. */ uint16_t qp_affix_next;uint64_t qp_fullpath_next; +bool reclaiming; };/* 9p2000.L open flags */
Signature · 1 line
Greg
Re: [PATCH 2/2] 9pfs: reduce latency of v9fs_reclaim_fd()
In reply to Christian SchoenebeckQuoted 10 lines · On Tue, 4 Mar 2025 16:16:05 +0100 Christian Schoenebeck…
This function calls
v9fs_co_close()andv9fs_co_closedir()in a loop. Each one of the calls adds two thread hops (between main thread and a fs driver background thread). Each thread hop adds latency, which sums up in function's loop to a significant duration.Reduce overall latency by open coding what
v9fs_co_close()andv9fs_co_closedir()do, executing those and the loop itself altogether in only one background thread block, hence reducing the total amount of thread hops to only two.
I'm afraid it is a bit trickier but I might have found a subtle bug in the current code :-)
Quoted 37 lines
Signed-off-by Christian Schoenebeck <[email protected]>--- hw/9pfs/9p.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)hw/9pfs/9p.c +4
-4@@ -434,6 +434,7 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)V9fsFidState *f;GHashTableIter iter;gpointer fid; +int err;/* prevent multiple coroutines running this function simultaniously */ if (s->reclaiming) {@@ -446,10 +447,10 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) QSLIST_HEAD(, V9fsFidState) reclaim_list =QSLIST_HEAD_INITIALIZER (reclaim_list); +/* Pick FIDs to be closed, collect them on reclaim_list. */ while (g_hash_table_iter_next (&iter, &fid, (gpointer *) &f)) {/* -* Unlink fids cannot be reclaimed. Check -* for them and skip them. Also skip fids +* Unlinked fids cannot be reclaimed, skip those, and also skip fids * currently being operated on. */ if (f->ref || f->flags &FID_NON_RECLAIMABLE ) {@@ -499,17 +500,24 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) } }/* -* Now close the fid in reclaim list. Free them if they -* are already clunked. +* Close the picked FIDs altogether on a background I/O driver thread. Do
hw/9pfs/9p.c void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) · line 513
Quoted 10 lines
hw/9pfs/9p.c +9
-0+* this all at once to keep latency (i.e. amount of thread hops between main +* thread <-> fs driver background thread) as low as possible. */ + v9fs_co_run_in_worker({ + QSLIST_FOREACH(f, &reclaim_list, reclaim_next) { + err = (f->fid_type == P9_FID_DIR) ? + s->ops->closedir(&s->ctx, &f->fs_reclaim) : + s->ops->close(&s->ctx, &f->fs_reclaim); + if (!err) { + total_open_fd--;
Hmm... total_open_fd is supposed to be accessed by the main thread only. Maybe turn total_open_fd into an atomic ? Or have this block to return the count of closed fds and propagate that to total_open_fd in the main thread ?
hw/9pfs/9p.c void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) · line 512
Quoted 12 lines
hw/9pfs/9p.c +4
-5+ } + } + }); +/* Free the closed FIDs. */ while (!QSLIST_EMPTY (&reclaim_list)) { f =QSLIST_FIRST (&reclaim_list);QSLIST_REMOVE (&reclaim_list, f, V9fsFidState, reclaim_next); -if (f->fid_type ==P9_FID_FILE ) { -v9fs_co_close (pdu, &f->fs_reclaim); - }else if (f->fid_type ==P9_FID_DIR ) { -v9fs_co_closedir (pdu, &f->fs_reclaim); - }
An now for the subtle bug in the existing code :
v9fs_co_close() and v9fs_co_closedir() honor TFLUSH with the following check that skips the actual closing :
if (v9fs_request_cancelled(pdu)) {
return -EINTR;
}If the client cancels the request that needs fd reclaim, some fds will be cleared from the fids without being closed. This causes fd leaks and probably ruins the overall reclaim logic.
This is kinda fixed by the current patch since all the fds in the reclaim list are closed altogether atomically in the same v9fs_co_run_in_worker().
hw/9pfs/9p.c void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) · line 523
Quoted 3 lines
hw/9pfs/9p.c +0
-0/* * Now drop the fid reference, free it * if clunked.
Cheers,
Signature · 1 line
Greg
Re: [PATCH 2/2] 9pfs: reduce latency of v9fs_reclaim_fd()
Quoted 61 lines · CET Greg Kurz
Quoted 10 lines · On Tue, 4 Mar 2025 16:16:05 +0100 Christian Schoenebeck…
This function calls
v9fs_co_close()andv9fs_co_closedir()in a loop. Each one of the calls adds two thread hops (between main thread and a fs driver background thread). Each thread hop adds latency, which sums up in function's loop to a significant duration.Reduce overall latency by open coding what
v9fs_co_close()andv9fs_co_closedir()do, executing those and the loop itself altogether in only one background thread block, hence reducing the total amount of thread hops to only two.I'm afraid it is a bit trickier but I might have found a subtle bug in the current code :-)
Quoted 44 lines
Signed-off-by Christian Schoenebeck <[email protected]>--- hw/9pfs/9p.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)hw/9pfs/9p.c +10
-4@@ -434,6 +434,7 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)V9fsFidState *f;GHashTableIter iter;gpointer fid; +int err;/* prevent multiple coroutines running this function simultaniously */ if (s->reclaiming) {@@ -446,10 +447,10 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) QSLIST_HEAD(, V9fsFidState) reclaim_list =QSLIST_HEAD_INITIALIZER (reclaim_list); +/* Pick FIDs to be closed, collect them on reclaim_list. */ while (g_hash_table_iter_next (&iter, &fid, (gpointer *) &f)) {/* -* Unlink fids cannot be reclaimed. Check -* for them and skip them. Also skip fids +* Unlinked fids cannot be reclaimed, skip those, and also skip fids * currently being operated on. */ if (f->ref || f->flags &FID_NON_RECLAIMABLE ) {@@ -499,17 +500,24 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) } }/* -* Now close the fid in reclaim list. Free them if they -* are already clunked. +* Close the picked FIDs altogether on a background I/O driver thread. Do +* this all at once to keep latency (i.e. amount of thread hops between main +* thread <-> fs driver background thread) as low as possible. */ + v9fs_co_run_in_worker({ + QSLIST_FOREACH(f, &reclaim_list, reclaim_next) { + err = (f->fid_type == P9_FID_DIR) ? + s->ops->closedir(&s->ctx, &f->fs_reclaim) :
Quoted 8 lines
hw/9pfs/9p.c void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) · line 513
Quoted 3 lines
hw/9pfs/9p.c +3
-0+ s->ops->close (&s->ctx, &f->fs_reclaim); +if (!err) { + total_open_fd--;Hmm...
total_open_fdis supposed to be accessed by the main thread only. Maybe turntotal_open_fdinto an atomic ? Or have this block to return the count of closed fds and propagate that tototal_open_fdin the main thread ?
Right! I'll change that to the latter, as it involves far less changes.
Quoted 22 lines
hw/9pfs/9p.c void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) · line 512
Quoted 12 lines
hw/9pfs/9p.c +4
-5+ } + } + }); +/* Free the closed FIDs. */ while (!QSLIST_EMPTY (&reclaim_list)) { f =QSLIST_FIRST (&reclaim_list);QSLIST_REMOVE (&reclaim_list, f, V9fsFidState, reclaim_next); -if (f->fid_type ==P9_FID_FILE ) { -v9fs_co_close (pdu, &f->fs_reclaim); - }else if (f->fid_type ==P9_FID_DIR ) { -v9fs_co_closedir (pdu, &f->fs_reclaim); - }An now for the subtle bug in the existing code :
v9fs_co_close()andv9fs_co_closedir()honor TFLUSH with the following check that skips the actual closing :if (v9fs_request_cancelled(pdu)) { return -EINTR; }
Quoted 7 lines
If the client cancels the request that needs fd reclaim, some fds will be cleared from the fids without being closed. This causes fd leaks and probably ruins the overall reclaim logic.
This is kinda fixed by the current patch since all the fds in the reclaim list are closed altogether atomically in the same
v9fs_co_run_in_worker().
Hu, that makes this 2nd patch indeed more than just an optimization fix.
I'll send a v2 and CC qemu-stable on the 2nd patch as well.
Good catches, thanks Greg!
/Christian