From nobody Sat Apr 11 18:38:40 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1775833585; cv=none; d=zohomail.com; s=zohoarc; b=ThrunLFwG0MakLIKn+VfDwdWvfx+QvD9dBZFTmpMI32sP6fy9lQnEt//OZe1cjNGU+dPPkM+pt+CHoibJtoeLYRTLG//oecDOPZYk3nn/uBGqvHeH4cV1DCb4//HTvGY1IxMXJtS7D8KcRP3ivy6CRz8vvJ1JcFxGYNfc+iAbNs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1775833585; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zbuB4mhESeHjR1sC4osJPiNIB/gMNHKxCFvIHqQzq6k=; b=jRWxCUa3Mx2alSMCvE1S4JCpwA5Q7z0UH0QUOLAece77GdRiB6qIPwVlZZ7VARkRERFfPFKLVpOwf1kO+L76sRP5rSNKffb6vL95Q3xxeP9MzGlfOco2zCY+BwWIxq5ZTgP8i1EbQhVX19pja4ZzlXnn37oEkN0TiErWb/iIitE= 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 (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1775833585867526.3745254022442; Fri, 10 Apr 2026 08:06:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wBDQF-0006ad-7j; Fri, 10 Apr 2026 11:05:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wBDQ8-0006Rr-7v for qemu-devel@nongnu.org; Fri, 10 Apr 2026 11:05:20 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wBDQ6-00022W-Dy for qemu-devel@nongnu.org; Fri, 10 Apr 2026 11:05:19 -0400 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-35691a231a7so1549744a91.3 for ; Fri, 10 Apr 2026 08:05:18 -0700 (PDT) Received: from ZBMac-LWN10QXKQ0.taila0cd24.ts.net ([104.168.88.121]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35e4131cfacsm3565785a91.10.2026.04.10.08.05.12 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 10 Apr 2026 08:05:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775833516; x=1776438316; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zbuB4mhESeHjR1sC4osJPiNIB/gMNHKxCFvIHqQzq6k=; b=jqNpYybiH6sQEYeJOcR7uPoVureRgkhNyZU/t9ZQ/+GpnTWJthtkV113zVCrsEfntR 9NcRSxd/+4aUjwaqK2KVmvOuHcXJgeQZ40TGsu0fmoKqa94KoEiY9m0FBu48xHGbKOVa xahZLxSgAGj3XEun6xY70IapwjDN/9agABqHnl5bwPXvt2y4GW7t4tYroctqRMxLvleP XpBuJ1zTL6CaTNuxSgQMdXmAD1/t7DioBefJ0zq5N0UTrQRBhWgpKJrkpgL8/zf3advt yT+pkdc0Gvawx9Bptk8KJG46hoAE4jInCNJWd5f8H8xjXY/HbP6TWq3NIFRWyZZluscX sLuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775833516; x=1776438316; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=zbuB4mhESeHjR1sC4osJPiNIB/gMNHKxCFvIHqQzq6k=; b=goKJOIQu8/ZW61hlMfUuCKUuHZ90Wp/8vXzjojv7gkKBumPt4KcAZfvzgwf6Frqksm 1Z6JopVw0Fm/qU5hDlDgoC5+pQSqh20AoiGF6g6mFlc9eroDbEIObtdh9wBgKW8PnrDe kDPSCo727ySgXFlDU04YFwQLv48pv8ZJ+GBs2fnCCgTULq2jrX0028UAhlV9+La+60dl TzeAKR01tA/slUwwbwgvMruOwp2YjROlwoQ9RbLN88tKhFJ3HYlXsOruRKw3eNe9K/KL BJ5QG0itBGJVX8qmg2kfGjPJD3+tr9lO1mGf7LXcf3IAkANQUXXdXUrjUnjXCoSyiK9N TfNQ== X-Gm-Message-State: AOJu0YydJYe0x8xdHGTISpETKxVR4hSQOdPV6bxE4eaq3p3XaIki04jC 9D1ikfm6mgyZDA7aYSs664MH8BY7n+3pKvBRLqwi1TjniC/GdXGFz3NgQslLhSuS X-Gm-Gg: AeBDiet4bEhd7XI6h2hfjywPnUsa/o0vpTDb6A9X2cofmeND7QL7ug+XcCZLtQ5jMxc VB1KxlkByXjzgQfs6s+uJTI8KzQu8eCmCbbmExYrjeCoAz5AU+kFWMuKvcPHL3DnU2LWZ28FBWk 88nts/NpNcZb9Qla8ch55tyz0EkAcjX0VmaI9C9iE3jJPBLk6CdTNskE9DJcogfwCSUtvib7+z7 YayT+IxTRx2i7wvyVy3OeAK35s7yWIX0hsPl2AmLDD7gyXWuDQ5eqPGbhe+qKk8DW6DiqzA/HOl ko9/28sCzuc9T/gzFUGju09eCHIFafGmOfc/5Z1IT5Q2m/yEUFpaSYiQcKb7UwxfwYGPRPiTSJk AV6FOSr0C5wNWXl7kF0P9NuHnS+wTjTd5cNn095nlYPUREThsjCcG9E1UvgVOjt8/uukjXt3YhG gWVopI5S7ZVpiSmK5CGpFo4eiePsf8DFcN+D8tf7lirncTNAQcmHpg X-Received: by 2002:a17:90b:4a04:b0:35b:e550:e68a with SMTP id 98e67ed59e1d1-35e4254fb13mr3695835a91.3.1775833516088; Fri, 10 Apr 2026 08:05:16 -0700 (PDT) From: Zhang Chen To: qemu-devel , "Dr . David Alan Gilbert" , Eric Blake , Markus Armbruster , "Michael S . Tsirkin" , Stefan Hajnoczi Cc: Zhang Chen Subject: [PATCH V6 02/14] iothread: introduce iothread_ref/unref to track attached devices Date: Fri, 10 Apr 2026 23:04:45 +0800 Message-ID: <20260410150457.85190-3-zhangckid@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20260410150457.85190-1-zhangckid@gmail.com> References: <20260410150457.85190-1-zhangckid@gmail.com> 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=2607:f8b0:4864:20::1031; envelope-from=zhangckid@gmail.com; helo=mail-pj1-x1031.google.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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: 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 @gmail.com) X-ZM-MESSAGEID: 1775833587785154101 Content-Type: text/plain; charset="utf-8" Currently, IOThreads do not maintain a record of which devices are associated with them. This makes it difficult to monitor the workload distribution of IOThreads, especially in complex hotplug scenarios involving multiple virtio-blk or virtio-scsi devices. This patch introduces a reference counting and tracking mechanism within the IOThread object: - iothread_ref(): Prepends the device's QOM path to a list. - iothread_unref(): Searches for the device path using a custom string comparison (g_strcmp0), releases the associated memory upon a successful match. - holders: A GList storing the QOM paths of attached devices for runtime introspection. This infrastructure allows management tools and QMP commands to query the attachment status of IOThreads. Signed-off-by: Zhang Chen --- include/system/iothread.h | 1 + iothread.c | 56 +++++++++++++++++++++++++++++++++++++++ qapi/misc.json | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/include/system/iothread.h b/include/system/iothread.h index e26d13c6c7..21a76bd70d 100644 --- a/include/system/iothread.h +++ b/include/system/iothread.h @@ -33,6 +33,7 @@ struct IOThread { bool stopping; /* has iothread_stop() been called? */ bool running; /* should iothread_run() continue? */ int thread_id; + GList *holders; /* an array of QOM paths for attached devi= ces */ =20 /* AioContext poll parameters */ int64_t poll_max_ns; diff --git a/iothread.c b/iothread.c index caf68e0764..c43266b191 100644 --- a/iothread.c +++ b/iothread.c @@ -36,6 +36,55 @@ #define IOTHREAD_POLL_MAX_NS_DEFAULT 0ULL #endif =20 +/* + * Add holder device path to the list. + */ +static void iothread_ref(IOThread *iothread, const char *holder) +{ + IoThreadHolder *h =3D g_new0(IoThreadHolder, 1); + + if (holder && holder[0] =3D=3D '/') { + h->type =3D IO_THREAD_HOLDER_KIND_QOM_OBJECT; + h->u.qom_object.data =3D g_strdup(holder); + } else { + h->type =3D IO_THREAD_HOLDER_KIND_BLOCK_NODE; + h->u.block_node.data =3D g_strdup(holder ? holder : "unknown"); + } + + iothread->holders =3D g_list_prepend(iothread->holders, h); +} + +static int iothread_holder_compare(gconstpointer a, gconstpointer b) +{ + const IoThreadHolder *holder_node =3D a; + const char *target_name =3D b; + const char *current_name =3D NULL; + + if (holder_node->type =3D=3D IO_THREAD_HOLDER_KIND_QOM_OBJECT) { + current_name =3D holder_node->u.qom_object.data; + } else if (holder_node->type =3D=3D IO_THREAD_HOLDER_KIND_BLOCK_NODE) { + current_name =3D holder_node->u.block_node.data; + } + + return g_strcmp0(current_name, target_name); +} + +/* + * Delete holder device path from the list. + */ +static void iothread_unref(IOThread *iothread, const char *holder) +{ + GList *link =3D g_list_find_custom(iothread->holders, holder, + (GCompareFunc)iothread_holder_compare= ); + + /* We don't support unref without a link */ + assert(link); + + IoThreadHolder *h =3D (IoThreadHolder *)link->data; + qapi_free_IoThreadHolder(h); + iothread->holders =3D g_list_delete_link(iothread->holders, link); +} + static void *iothread_run(void *opaque) { IOThread *iothread =3D opaque; @@ -115,6 +164,9 @@ static void iothread_instance_finalize(Object *obj) =20 iothread_stop(iothread); =20 + /* We don't support finalize without holders */ + assert(iothread->holders =3D=3D NULL); + /* * Before glib2 2.33.10, there is a glib2 bug that GSource context * pointer may not be cleared even if the context has already been @@ -336,6 +388,10 @@ char *iothread_get_id(IOThread *iothread) =20 AioContext *iothread_get_aio_context(IOThread *iothread) { + /* Remove in next patch for build */ + iothread_ref(iothread, "tmp"); + iothread_unref(iothread, "tmp"); + return iothread->ctx; } =20 diff --git a/qapi/misc.json b/qapi/misc.json index 1f5062df2a..d65d8012b2 100644 --- a/qapi/misc.json +++ b/qapi/misc.json @@ -67,6 +67,54 @@ ## { 'command': 'query-name', 'returns': 'NameInfo', 'allow-preconfig': true } =20 + +## +# @IoThreadHolderBlockNode: +# +# @data: Block node name. +# +# Since: 11.0 +# +## +{ 'struct': 'IoThreadHolderBlockNode', + 'data': { 'data': 'str' } } + +## +# @IoThreadHolderQomObject: +# +# @data: QOM path. +# +# Since: 11.0 +# +## +{ 'struct': 'IoThreadHolderQomObject', + 'data': { 'data': 'str' } } + +## +# @IoThreadHolderKind: +# +# @block-node: Block node name. +# @qom-object: Standard QOM path. +# +# Since: 11.0 +## +{ 'enum': 'IoThreadHolderKind', + 'data': [ 'block-node', 'qom-object' ] } + +## +# @IoThreadHolder: +# +# @type: Current IoThread holder type support QOM path and Block node. +# +# Since: 11.0 +## +{ 'union': 'IoThreadHolder', + 'base': { 'type': 'IoThreadHolderKind' }, + 'discriminator': 'type', + 'data': { + 'block-node': 'IoThreadHolderBlockNode', + 'qom-object': 'IoThreadHolderQomObject' } } + ## # @IOThreadInfo: # --=20 2.49.0