From nobody Sat Apr 11 18:34:15 2026 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=none dis=none) header.from=linux.alibaba.com ARC-Seal: i=1; a=rsa-sha256; t=1775249507; cv=none; d=zohomail.com; s=zohoarc; b=OTsTQIGgNubs5IMlNw40B0N60JTdpt+Kxbs95d7tw3kegYQwm5GT/WBJ4o0zgYU3pOTmKDlu4gWhJZ6Csbm4z5hUHIGToQ/c2XlmcI1moPsjk71+Lku91qs+FCzipnxZZkUNS54L3yfUmws14gQGbsomQD4A5Q4b/VTBwU6dhgw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1775249507; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=S0gH1RNVZ2+dFXuhhk/p/ALCB/w4E8iigHAamMgF1fA=; b=eOzNum3U54L7f5a8dHz6+4YLPkjGOm+PsytS3iEPPXz4HJXERJ1dyTv4DkN5v+oCK7WTTOZ3kKi+Hzr0BgmXuLECKJgpfMZYk2bqgTTftCX3Ubo6DMLxvx8Fhdp3MQYOra+vqOHcShjmjwhyKsOSWxpKmK59l7PzHbYq5GWwGzk= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17752495074911023.2287302245289; Fri, 3 Apr 2026 13:51:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w8lTc-0001MX-C0; Fri, 03 Apr 2026 16:50:48 -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 ) id 1w8lTa-0001M1-AG for qemu-devel@nongnu.org; Fri, 03 Apr 2026 16:50:46 -0400 Received: from out30-97.freemail.mail.aliyun.com ([115.124.30.97]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w8lTV-000093-11 for qemu-devel@nongnu.org; Fri, 03 Apr 2026 16:50:46 -0400 Received: from localhost(mailfrom:guobin@linux.alibaba.com fp:SMTPD_---0X0L1tvp_1775249411 cluster:ay36) by smtp.aliyun-inc.com; Sat, 04 Apr 2026 04:50:27 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1775249428; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=S0gH1RNVZ2+dFXuhhk/p/ALCB/w4E8iigHAamMgF1fA=; b=KRd3V7Tq7twL5/XZS5am9rGYgrgZSDtflH0IFP+AHm7RxCX/kYzf6ebkI9KIA2sj8eAvZMongoyQfzDCoyqX5xkkj4RRtUNWiCmGEfZL8Sbsx9PyxfAoocv+LQeeX2XpQcfeKotgBdKpIpLH1LEfbZ35NNNDzGKkCExtOPrW0ms= X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R161e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=maildocker-contentspam011083073210; MF=guobin@linux.alibaba.com; NM=1; PH=DS; RN=4; SR=0; TI=SMTPD_---0X0L1tvp_1775249411; From: Bin Guo To: qemu-devel@nongnu.org Cc: mst@redhat.com, richard.henderson@linaro.org, philmd@linaro.org Subject: [PATCH] vhost: return early when memslots overflow in vhost_get_free_memslots() Date: Sat, 4 Apr 2026 04:50:11 +0800 Message-ID: <20260403205011.15962-1-guobin@linux.alibaba.com> X-Mailer: git-send-email 2.50.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=115.124.30.97; envelope-from=guobin@linux.alibaba.com; helo=out30-97.freemail.mail.aliyun.com X-Spam_score_int: -174 X-Spam_score: -17.5 X-Spam_bar: ----------------- X-Spam_report: (-17.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 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: qemu development 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 @linux.alibaba.com) X-ZM-MESSAGEID: 1775249510608158500 Content-Type: text/plain; charset="utf-8" The vhost_get_free_memslots() function continues iterating through all vhost devices even after detecting a memslot overflow, which is unnecessary since the result is already determined to be 0. Use early return to immediately exit the loop when overflow is detected, avoiding redundant iterations and unnecessary arithmetic operations. This optimizes the best case from O(n) to O(1) when the first device overflows, and saves n-k iterations on average where k is the position of the overflowing device. Signed-off-by: Bin Guo --- hw/virtio/vhost.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index b9dc4ed13b..9d1b56fa34 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -70,15 +70,16 @@ unsigned int vhost_get_free_memslots(void) =20 QLIST_FOREACH(hdev, &vhost_devices, entry) { unsigned int r =3D hdev->vhost_ops->vhost_backend_memslots_limit(h= dev); - unsigned int cur_free =3D r - hdev->mem->nregions; + unsigned int cur_free; =20 if (unlikely(r < hdev->mem->nregions)) { warn_report_once("used (%u) vhost backend memory slots exceed" " the device limit (%u).", hdev->mem->nregion= s, r); - free =3D 0; - } else { - free =3D MIN(free, cur_free); + return 0; } + + cur_free =3D r - hdev->mem->nregions; + free =3D MIN(free, cur_free); } return free; } --=20 2.50.1 (Apple Git-155)