9pfs: fix 'total_open_fd' decrementation
PATCH v2 1/1 9pfs: fix 'total_open_fd' decrementation
According to 'man 2 close' errors returned by close() should only be used for either diagnostic purposes or for catching data loss due to a previous write error, as an error result of close() usually indicates a deferred error of a previous write operation.
Therefore not decrementing 'total_open_fd' on a close() error is wrong and would yield in a higher open file descriptor count than actually the case, leading to 9p server reclaiming open file descriptors too soon.
Based-on: <[email protected]>
---
V2: log a warning message on unexpected close() -> EBADF case
hw/9pfs/9p.c | 10 +++++++++- hw/9pfs/codir.c | 7 ++++++- hw/9pfs/cofile.c | 7 ++++++- 3 files changed, 21 insertions(+), 3 deletions(-)
hw/9pfs/9p.c +9 -1
@@ -510,7 +510,15 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) 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) { + +/* 'man 2 close' suggests to ignore close() errors except of EBADF */ +if (unlikely (err && errno ==EBADF )) { +/* +* unexpected case as FIDs were picked above by having a valid +* file descriptor +*/ +error_report ("9pfs: v9fs_reclaim_fd() WARNING: close() failed with EBADF" ); + }else {/* total_open_fd must only be mutated on main thread */ nclosed++; }
hw/9pfs/codir.c +6 -1
@@ -20,6 +20,7 @@#include "fsdev/qemu-fsdev.h" #include "qemu/thread.h" #include "qemu/main-loop.h" +#include "qemu/error-report.h" #include "coth.h" #include "9p-xattr.h" #include "9p-util.h"
@@ -353,7 +354,11 @@ int coroutine_fn v9fs_co_closedir(V9fsPDU *pdu, V9fsFidOpenState *fs) err = -errno; } }); -if (!err) { +/* 'man 2 close' suggests to ignore close() errors except of EBADF */ +if (unlikely (err && errno ==EBADF )) { +/* unexpected case as we should have checked for a valid file handle */ +error_report ("9pfs: WARNING: v9fs_co_closedir() failed with EBADF" ); + }else { total_open_fd--; }return err;
hw/9pfs/cofile.c +6 -1
@@ -20,6 +20,7 @@#include "fsdev/qemu-fsdev.h" #include "qemu/thread.h" #include "qemu/main-loop.h" +#include "qemu/error-report.h" #include "coth.h" int coroutine_fn v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t st_mode,
@@ -197,7 +198,11 @@ int coroutine_fn v9fs_co_close(V9fsPDU *pdu, V9fsFidOpenState *fs) err = -errno; } }); -if (!err) { +/* 'man 2 close' suggests to ignore close() errors except of EBADF */ +if (unlikely (err && errno ==EBADF )) { +/* unexpected case as we should have checked for a valid file handle */ +error_report ("9pfs: WARNING: v9fs_co_close() failed with EBADF" ); + }else { total_open_fd--; }return err;
Signature · 1 line
2.39.5
Quoted 26 lines · On Thu, 20 Mar 2025 13:16:20 +0100 Christian Schoenebeck…
According to 'man 2 close' errors returned by
close()should only be used for either diagnostic purposes or for catching data loss due to a previous write error, as an error result ofclose()usually indicates a deferred error of a previous write operation.Therefore not decrementing '
total_open_fd' on aclose()error is wrong and would yield in a higher open file descriptor count than actually the case, leading to 9p server reclaiming open file descriptors too soon.Based-on: <[email protected]>
Signed-off-by Christian Schoenebeck <[email protected]>---
V2: log a warning message on unexpectedclose()-> EBADF casehw/9pfs/9p.c | 10 +++++++++- hw/9pfs/codir.c | 7 ++++++- hw/9pfs/cofile.c | 7 ++++++- 3 files changed, 21 insertions(+), 3 deletions(-)hw/9pfs/9p.c +0
-0@@ -510,7 +510,15 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) err = (f->fid_type ==P9_FID_DIR ) ? s->ops->closedir(&s->ctx, &f->fs_reclaim) :
hw/9pfs/9p.c void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) · line 520
Quoted 10 lines
hw/9pfs/9p.c +8
-1s->ops->close (&s->ctx, &f->fs_reclaim); -if (!err) { + +/* 'man 2 close' suggests to ignore close() errors except of EBADF */ +if (unlikely (err && errno ==EBADF )) { +/* +* unexpected case as FIDs were picked above by having a valid +* file descriptor +*/ +error_report ("9pfs: v9fs_reclaim_fd() WARNING: close() failed with EBADF" );
I just hope there isn't a way to reach this 100% or we'll end up saturating the logs. ;-)
Quoted 54 lines
hw/9pfs/9p.c +1
-0+ }else {/* total_open_fd must only be mutated on main thread */ nclosed++; }hw/9pfs/codir.c +6
-1@@ -20,6 +20,7 @@#include "fsdev/qemu-fsdev.h" #include "qemu/thread.h" #include "qemu/main-loop.h" +#include "qemu/error-report.h" #include "coth.h" #include "9p-xattr.h" #include "9p-util.h" @@ -353,7 +354,11 @@ int coroutine_fn v9fs_co_closedir(V9fsPDU *pdu, V9fsFidOpenState *fs) err = -errno; } }); -if (!err) { +/* 'man 2 close' suggests to ignore close() errors except of EBADF */ +if (unlikely (err && errno ==EBADF )) { +/* unexpected case as we should have checked for a valid file handle */ +error_report ("9pfs: WARNING: v9fs_co_closedir() failed with EBADF" ); + }else { total_open_fd--; }return err;hw/9pfs/cofile.c +6
-1@@ -20,6 +20,7 @@#include "fsdev/qemu-fsdev.h" #include "qemu/thread.h" #include "qemu/main-loop.h" +#include "qemu/error-report.h" #include "coth.h" int coroutine_fn v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t st_mode,@@ -197,7 +198,11 @@ int coroutine_fn v9fs_co_close(V9fsPDU *pdu, V9fsFidOpenState *fs) err = -errno; } }); -if (!err) { +/* 'man 2 close' suggests to ignore close() errors except of EBADF */ +if (unlikely (err && errno ==EBADF )) { +/* unexpected case as we should have checked for a valid file handle */ +error_report ("9pfs: WARNING: v9fs_co_close() failed with EBADF" ); + }else { total_open_fd--; }return err;
Signature · 1 line
Greg
Quoted 10 lines · CET Christian Schoenebeck
According to 'man 2 close' errors returned by
close()should only be used for either diagnostic purposes or for catching data loss due to a previous write error, as an error result ofclose()usually indicates a deferred error of a previous write operation.Therefore not decrementing '
total_open_fd' on aclose()error is wrong and would yield in a higher open file descriptor count than actually the case, leading to 9p server reclaiming open file descriptors too soon.Based-on: <[email protected]>
Quoted 8 lines
Signed-off-by Christian Schoenebeck <[email protected]>---
V2: log a warning message on unexpectedclose()-> EBADF casehw/9pfs/9p.c | 10 +++++++++- hw/9pfs/codir.c | 7 ++++++- hw/9pfs/cofile.c | 7 ++++++- 3 files changed, 21 insertions(+), 3 deletions(-)
Queued on 9p.next:
https://github.com/cschoenebeck/qemu/commits/9p.next
Thanks!
/Christian