9pfs: v9fs_reclaim_fd() fixes
v2 cover 9pfs: v9fs_reclaim_fd() fixes
Three fixes for 9p server's v9fs_reclaim_fd() function:
* Patch 1 fixes a concurrency issue.
* Patch 2 fixes a file descriptor leak and optimizes overall latency. On a test machine with ~800,000 FIDs, this reduced execution duration of v9fs_reclaim_fd() from 30ms to 1ms.
V2:
- Patch 2: Decrement global variable total_open_fd on main thread, not on fs driver background thread.
- Patch 2: Update commit log about file descriptor leak being fixed.
Christian Schoenebeck (2):
9pfs: fix concurrent v9fs_reclaim_fd() calls 9pfs: fix FD leak and reduce latency of v9fs_reclaim_fd()
hw/9pfs/9p.c | 39 ++++++++++++++++++++++++++++++--------- hw/9pfs/9p.h | 1 + 2 files changed, 31 insertions(+), 9 deletions(-)
Signature · 1 line
2.39.5
PATCH v2 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
PATCH v2 2/2 9pfs: fix FD leak and reduce latency of v9fs_reclaim_fd()
In reply to Christian SchoenebeckThis patch fixes two different bugs in v9fs_reclaim_fd():
1. Reduce latency:
This 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.
2. Fix file descriptor leak:
The existing code called v9fs_co_close() and v9fs_co_closedir() to close file descriptors. Both functions check right at the beginning if the 9p request was cancelled:
if (v9fs_request_cancelled(pdu)) {
return -EINTR;
}So if client sent a 'Tflush' message, v9fs_co_close() / v9fs_co_closedir() returned without having closed the file descriptor and v9fs_reclaim_fd() subsequently freed the FID without its file descriptor being closed, hence leaking those file descriptors.
This 2nd bug is fixed by this patch as well by open coding v9fs_co_close() and v9fs_co_closedir() inside of v9fs_reclaim_fd() and not performing the v9fs_request_cancelled(pdu) check there.
--- hw/9pfs/9p.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)
hw/9pfs/9p.c +20 -9
@@ -434,6 +434,8 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)V9fsFidState *f;GHashTableIter iter;gpointer fid; +int err; +int nclosed =0 ;/* prevent multiple coroutines running this function simultaniously */ if (s->reclaiming) {
@@ -446,10 +448,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 +501,26 @@ 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 must only be mutated on main thread */ + nclosed++; + } + } + }); + total_open_fd -= nclosed; +/* 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
Re: [PATCH v2 2/2] 9pfs: fix FD leak and reduce latency of v9fs_reclaim_fd()
Quoted 78 lines · CET Christian Schoenebeck
This patch fixes two different bugs in
v9fs_reclaim_fd():1. Reduce latency:
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.2. Fix file descriptor leak:
The existing code called
v9fs_co_close()andv9fs_co_closedir()to close file descriptors. Both functions check right at the beginning if the 9p request was cancelled:if (v9fs_request_cancelled(pdu)) { return -EINTR; }So if client sent a 'Tflush' message,
v9fs_co_close()/v9fs_co_closedir()returned without having closed the file descriptor andv9fs_reclaim_fd()subsequently freed the FID without its file descriptor being closed, hence leaking those file descriptors.This 2nd bug is fixed by this patch as well by open coding
v9fs_co_close()andv9fs_co_closedir()inside ofv9fs_reclaim_fd()and not performing thev9fs_request_cancelled(pdu)check there.Fixes 7a46274529c ('hw/9pfs: Add file descriptor reclaim support')Fixes bccacf6c792 ('hw/9pfs: Implement TFLUSH operation')Signed-off-by Christian Schoenebeck <[email protected]>--- hw/9pfs/9p.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)hw/9pfs/9p.c +9
-4@@ -434,6 +434,8 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)V9fsFidState *f;GHashTableIter iter;gpointer fid; +int err; +int nclosed =0 ;/* prevent multiple coroutines running this function simultaniously */ if (s->reclaiming) {@@ -446,10 +448,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 +501,26 @@ 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) {
hw/9pfs/9p.c void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) · line 519
Quoted 10 lines
hw/9pfs/9p.c +10
-0+ 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 must only be mutated on main thread */ + nclosed++; + } + } + }); + total_open_fd -= nclosed;
So here is another thing: looking at 'man 2 close' I would say that decrementing 'total_open_fd' conditionally based on what close() returned is wrong. The man page suggest that the return value of close() should only be used for diagnostic purposes, as an error on close() often indicates just an error on a previous write() and hence the return value should only be used for catching a data loss related to writes.
So this should probably changed here, as well as in v9fs_co_close() / v9fs_co_closedir(), part of a separate patch though, so I haven't addressed it here yet.
Does this make sense?
/Christian
Quoted 13 lines
hw/9pfs/9p.c +1
-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); - }/* * Now drop the fid reference, free it * if clunked.
Re: [PATCH v2 2/2] 9pfs: fix FD leak and reduce latency of v9fs_reclaim_fd()
Quoted 97 lines · On Fri, 07 Mar 2025 10:47:33 +0100 Christian Schoenebeck…
Quoted 78 lines · CET Christian Schoenebeck
This patch fixes two different bugs in
v9fs_reclaim_fd():1. Reduce latency:
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.2. Fix file descriptor leak:
The existing code called
v9fs_co_close()andv9fs_co_closedir()to close file descriptors. Both functions check right at the beginning if the 9p request was cancelled:if (v9fs_request_cancelled(pdu)) { return -EINTR; }So if client sent a 'Tflush' message,
v9fs_co_close()/v9fs_co_closedir()returned without having closed the file descriptor andv9fs_reclaim_fd()subsequently freed the FID without its file descriptor being closed, hence leaking those file descriptors.This 2nd bug is fixed by this patch as well by open coding
v9fs_co_close()andv9fs_co_closedir()inside ofv9fs_reclaim_fd()and not performing thev9fs_request_cancelled(pdu)check there.Fixes 7a46274529c ('hw/9pfs: Add file descriptor reclaim support')Fixes bccacf6c792 ('hw/9pfs: Implement TFLUSH operation')Signed-off-by Christian Schoenebeck <[email protected]>--- hw/9pfs/9p.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)hw/9pfs/9p.c +9
-4@@ -434,6 +434,8 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)V9fsFidState *f;GHashTableIter iter;gpointer fid; +int err; +int nclosed =0 ;/* prevent multiple coroutines running this function simultaniously */ if (s->reclaiming) {@@ -446,10 +448,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 +501,26 @@ 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) {hw/9pfs/9p.c void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) · line 519
Quoted 10 lines
hw/9pfs/9p.c +10
-0+ 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 must only be mutated on main thread */ + nclosed++; + } + } + }); + total_open_fd -= nclosed;So here is another thing: looking at 'man 2 close' I would say that decrementing '
total_open_fd' conditionally based on whatclose()returned is wrong. The man page suggest that the return value ofclose()should only be used for diagnostic purposes, as an error onclose()often indicates just an error on a previouswrite()and hence the return value should only be used for catching a data loss related to writes.
Quoted 6 lines
So this should probably changed here, as well as in
v9fs_co_close()/v9fs_co_closedir(), part of a separate patch though, so I haven't addressed it here yet.Does this make sense?
The handling of the return value of close() seems to be non-trivial indeed. It makes sense to address this in some other series.
Cheers,
--
Greg
Quoted 17 lines
/Christian
Quoted 13 lines
hw/9pfs/9p.c +1
-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); - }/* * Now drop the fid reference, free it * if clunked.
Signature · 1 line
Greg
Re: [PATCH v2 2/2] 9pfs: fix FD leak and reduce latency of v9fs_reclaim_fd()
In reply to Christian SchoenebeckQuoted 29 lines · On Fri, 7 Mar 2025 10:23:02 +0100 Christian Schoenebeck…
This patch fixes two different bugs in
v9fs_reclaim_fd():1. Reduce latency:
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.2. Fix file descriptor leak:
The existing code called
v9fs_co_close()andv9fs_co_closedir()to close file descriptors. Both functions check right at the beginning if the 9p request was cancelled:if (v9fs_request_cancelled(pdu)) { return -EINTR; }So if client sent a 'Tflush' message,
v9fs_co_close()/v9fs_co_closedir()returned without having closed the file descriptor andv9fs_reclaim_fd()subsequently freed the FID without its file descriptor being closed, hence leaking those file descriptors.
Quoted 8 lines
This 2nd bug is fixed by this patch as well by open coding
v9fs_co_close()andv9fs_co_closedir()inside ofv9fs_reclaim_fd()and not performing thev9fs_request_cancelled(pdu)check there.Fixes 7a46274529c ('hw/9pfs: Add file descriptor reclaim support')Fixes bccacf6c792 ('hw/9pfs: Implement TFLUSH operation')Signed-off-by Christian Schoenebeck <[email protected]>---
Quoted 63 lines
hw/9pfs/9p.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)hw/9pfs/9p.c +20
-9@@ -434,6 +434,8 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)V9fsFidState *f;GHashTableIter iter;gpointer fid; +int err; +int nclosed =0 ;/* prevent multiple coroutines running this function simultaniously */ if (s->reclaiming) {@@ -446,10 +448,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 +501,26 @@ 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 must only be mutated on main thread */ + nclosed++; + } + } + }); + total_open_fd -= nclosed; +/* 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
Greg
Quoted 13 lines · CET Christian Schoenebeck
Three fixes for 9p server's
v9fs_reclaim_fd()function:* Patch 1 fixes a concurrency issue.
* Patch 2 fixes a file descriptor leak and optimizes overall latency. On a test machine with ~800,000 FIDs, this reduced execution duration of
v9fs_reclaim_fd()from 30ms to 1ms.V2:
- Patch 2: Decrement global variabletotal_open_fdon main thread, not on fs driver background thread.
- Patch 2: Update commit log about file descriptor leak being fixed.
Quoted 9 lines
Christian Schoenebeck (2):
9pfs: fix concurrentv9fs_reclaim_fd()calls 9pfs: fix FD leak and reduce latency ofv9fs_reclaim_fd()hw/9pfs/9p.c | 39 ++++++++++++++++++++++++++++++--------- hw/9pfs/9p.h | 1 + 2 files changed, 31 insertions(+), 9 deletions(-)
Queued on 9p.next:
https://github.com/cschoenebeck/qemu/commits/9p.next
Thanks!
/Christian