From nobody Wed Apr 2 13:41:36 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=crudebyte.com ARC-Seal: i=1; a=rsa-sha256; t=1742473595; cv=none; d=zohomail.com; s=zohoarc; b=QBaq4ujrSJeiLGRlK/eYnIaYLqd5HFguHHINVe+DXfWu0i7xB3ubkaKDGMQfQvFyx/KXSMCaQCWmJBo5JP/S5D01I/H+1iikTFC9YBqXKdi2WL550D5vPbYpopn8K92bt05V9vZN1FtfSvMa6UYpLw4JLd/QhcaM/cItUv6eyNY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1742473595; h=Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=U1CJbSQ4ojuRj9btueVTKiBMg7jOOtRxixjKO+xArJc=; b=c2XYDBjL31O4aZH0HcBE6gkl8/qI2dhVXhirkY2avD8hYUvxEyZjQ+a9q81SuW/1Qs7APs3sD2e+5TyAhULv5GMmJuojxF/hAL5pqlfkBDmPjqS5aCH4dxutROrgl1vNmp7J6HBF3a5CZwlmC2tuKInUBl3IPQP+iHoora1Dzlo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1742473595651376.1946728619307; Thu, 20 Mar 2025 05:26:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tvEyV-00069y-1S; Thu, 20 Mar 2025 08:26:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <81bcefde27e7450ea4061acf0f63320ac0f8f95d@kylie.crudebyte.com>) id 1tvEyT-00069f-GL for qemu-devel@nongnu.org; Thu, 20 Mar 2025 08:26:13 -0400 Received: from kylie.crudebyte.com ([5.189.157.229]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <81bcefde27e7450ea4061acf0f63320ac0f8f95d@kylie.crudebyte.com>) id 1tvEyQ-0006sR-Kc for qemu-devel@nongnu.org; Thu, 20 Mar 2025 08:26:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=kylie; h=Message-Id:Cc:To:Subject:Date:From:Content-Type: Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Content-ID: Content-Description; bh=U1CJbSQ4ojuRj9btueVTKiBMg7jOOtRxixjKO+xArJc=; b=MLS4O nrAwjUi8+yZpQ6bA7C57sFM7QNNG7mmnc5U9DTMb388pU9VFybrYttCZt8kucOZQvMUXjKs0bK2n6 VoHMe6yVF6hdxm7SWQIXBWTDus9/stAhxw0S+2uXNTKstvegqU3JFfwpLuvmIqvU829y7gsgI74am BLzSopwWT1g5P05f4Wfw56Q7f68egwwOVzh+QTeNQZD83UsLLnCID/Kr+ORL1du4iME1WaMH4CO6B Lso1AR83pxD4BA0BSCY9YGFzb7TpfniAUZjIszZHXgFRU8Z/6Y6wXWEHFd/6O4BfKjd/mJ/5LwTGD RlBgSW43CpzzGDEEyaVihJV7yoLCWANhpIK8U8Pcy6GeWFA8B/O6LdR2GQ9mB3IDeR+bCwlx/UCUX QZAeEtiBC9RXj68P47DjWGQRI0vLDdfXRK86WFvNAYqZJyjAfusKSh9YvnrkPhDt5spQbBsljcWvP 1ZZ8gJAO3UcdddMFaMjMCWnOgmAC5T0m6Z10O38KP5G7E8Vl49j+RcKcQmPrpvZipVh0aPXIJ0cbP f25xLBcZHb9JQlDkzQzlgNfNoyuJDpOmMHpYU25PtPjkNI7jqmU4ZW2K/a4FxOvIHlnSgaGWBqi44 nak1GE/oEYKc3+tUkk0f95r0WpWVzttjGkI43x9nFegQ8iRZWEJiplMxKHbPWk=; From: Christian Schoenebeck Date: Thu, 20 Mar 2025 13:16:20 +0100 Subject: [PATCH v2] 9pfs: fix 'total_open_fd' decrementation To: qemu-devel@nongnu.org Cc: Greg Kurz Message-Id: Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=5.189.157.229; envelope-from=81bcefde27e7450ea4061acf0f63320ac0f8f95d@kylie.crudebyte.com; helo=kylie.crudebyte.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @crudebyte.com) X-ZM-MESSAGEID: 1742473596607019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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: <20250312152933.383967-7-groug@kaod.org> Signed-off-by: Christian Schoenebeck Reviewed-by: Greg Kurz --- 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(-) diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index b22df3aa2b..8b001b9112 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -510,7 +510,15 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) err =3D (f->fid_type =3D=3D 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 E= BADF */ + if (unlikely(err && errno =3D=3D EBADF)) { + /* + * unexpected case as FIDs were picked above by having a v= alid + * file descriptor + */ + error_report("9pfs: v9fs_reclaim_fd() WARNING: close() fai= led with EBADF"); + } else { /* total_open_fd must only be mutated on main thread */ nclosed++; } diff --git a/hw/9pfs/codir.c b/hw/9pfs/codir.c index 2068a4779d..bce7dd96e9 100644 --- a/hw/9pfs/codir.c +++ b/hw/9pfs/codir.c @@ -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, V9fsFi= dOpenState *fs) err =3D -errno; } }); - if (!err) { + /* 'man 2 close' suggests to ignore close() errors except of EBADF */ + if (unlikely(err && errno =3D=3D EBADF)) { + /* unexpected case as we should have checked for a valid file hand= le */ + error_report("9pfs: WARNING: v9fs_co_closedir() failed with EBADF"= ); + } else { total_open_fd--; } return err; diff --git a/hw/9pfs/cofile.c b/hw/9pfs/cofile.c index 71174c3e4a..6e775c8e41 100644 --- a/hw/9pfs/cofile.c +++ b/hw/9pfs/cofile.c @@ -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" =20 int coroutine_fn v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t st_mo= de, @@ -197,7 +198,11 @@ int coroutine_fn v9fs_co_close(V9fsPDU *pdu, V9fsFidOp= enState *fs) err =3D -errno; } }); - if (!err) { + /* 'man 2 close' suggests to ignore close() errors except of EBADF */ + if (unlikely(err && errno =3D=3D EBADF)) { + /* unexpected case as we should have checked for a valid file hand= le */ + error_report("9pfs: WARNING: v9fs_co_close() failed with EBADF"); + } else { total_open_fd--; } return err; --=20 2.39.5