From nobody Thu Nov 26 02:11:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 38.145.34.151 as permitted sender) client-ip=38.145.34.151; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of lists.libvirt.org designates 38.145.34.151 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1790073324; cv=none; d=zohomail.com; s=zohoarc; b=CvSlDqSPYAulkZgkJ0Rs+5b+vJOa1HjRDj/J9yFPRPT/CPaUoxqptngrqkQVjZFm8wxFC6TX2cxrkspJCxO8EOeBeQNPQKQv6ivyeo66411oeQegCfP/9SkjltP7W/evcnDK8M2ADphvN1x7JIA1n0+DUGBZ2NCQvfyrJG7bV1Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1790073324; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=1dF4qYnvjoWLV0HNrrhT5rhBNDkgeO1Nc7PuWyPUWMo=; b=GOE7DGlHqndF29Rl4Mgsvj34mcjYS/Q5P//oCm9m/s0zPbR57u2UWaRtaJk3Vu41ngCyY57nYnsy54IKv/jjzExveHhp9JEH+i8DfsasIzoHDndq1F5HZ3WW/j+pmFMwoLgkiYI6QP4QzfOtVFJd5RwHPIOL0pKDzJjbN8CGb7w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of lists.libvirt.org designates 38.145.34.151 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [38.145.34.151]) by mx.zohomail.com with SMTPS id 1790073324840779.8494512103913; Tue, 22 Sep 2026 03:35:24 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 231823F35B; Tue, 22 Sep 2026 06:35:23 -0400 (EDT) Received: from [172.19.199.16] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id B7E024191B; Tue, 22 Sep 2026 06:34:08 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 197F63F317; Tue, 22 Sep 2026 06:33:59 -0400 (EDT) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 2924E3F308 for ; Tue, 22 Sep 2026 06:33:57 -0400 (EDT) Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa115.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2026 03:32:52 -0700 Received: from liyifan1-desk.bj.intel.com ([10.238.152.46]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2026 03:32:50 -0700 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=4.0.1 X-Greylist: delayed 64 seconds by postgrey-1.37 at lists.libvirt.org; Tue, 22 Sep 2026 06:33:58 EDT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1790073238; x=1821609238; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=uY9SR5qnX7su2YLrn/QXHSy71eK58ALcHSFCSTfZ9Nw=; b=Ryo+zpuCbLGNIfXZU2SHmgXLEBDxBQWb+Je1K0tSf8d262brYStq8hg5 1xuATuUMSabahrLQA2LUUn5m1sPZMr/VY0Hjam6dmWkBDubug4qKT3WUw 63AvkbDgUX+s2MP9eRnUKvPis5JYndYhgAjYWytbn07PlI4h/LiLvCGXx E9MzK9KW1HT15zOhaYfxFs1vFLcCakOmPm8S/IS9sWVyiQEE1I2aVoWpR RdvRSvLR7YEXGk1qOxkm623V2GiwXzV1O/f2EWmhUVZ71bO3y8PZoe3ze DsQZxxByjEaKStQtSud+XMyuzwoJsn6YNqHTKpuLTiN8FAga12yz21AsP Q==; X-CSE-ConnectionGUID: 3nJDX7juQDG47SKvh0v5/g== X-CSE-MsgGUID: C7cjgC8OTvSGmHC5NHq18Q== X-IronPort-AV: E=McAfee;i="6800,10657,11912"; a="1165992" X-IronPort-AV: E=Sophos;i="6.27,116,1787036400"; d="scan'208";a="1165992" X-CSE-ConnectionGUID: nxnCsZT6Qnuy01yROxGlQg== X-CSE-MsgGUID: 8gOZ9W3LTZCZJVXrBHU4NQ== X-ExtLoop1: 1 From: Yifan Li To: devel@lists.libvirt.org Subject: [PATCH] node_device_udev: Don't hold the object lock while joining the udev thread Date: Tue, 22 Sep 2026 18:27:08 +0800 Message-ID: <20260922102708.1509038-1-yifan2.li@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: 5GBVE2K6Q2EXS4IIXGEVBZJZUZH2YKQG X-Message-ID-Hash: 5GBVE2K6Q2EXS4IIXGEVBZJZUZH2YKQG X-MailFrom: yifan2.li@intel.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Yifan Li , Jianfeng Gao , Lili Li X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1790073327350158500 Content-Type: text/plain; charset="utf-8" We found libvirt has a race condition bug and it causes systemd timeout during system shutdown. This issue does not exist on libvirt 10.0.0 with Ubuntu 24.04 but found in Ubuntu 26.04 which uses libvirt 12.0.0. nodeStateShutdownWait() joins the udev event thread while holding @priv's object lock: VIR_WITH_OBJECT_LOCK_GUARD(priv) { if (priv->udevThread) virThreadJoin(priv->udevThread); } but udevEventHandleThread() needs that same lock to make progress. Its only clean exit path is VIR_WITH_OBJECT_LOCK_GUARD(priv) { while (!priv->udevDataReady && !priv->udevThreadQuit) { if (virCondWait(&priv->udevThreadCond, &priv->parent.lock)) ... } if (priv->udevThreadQuit) return; so the thread has to re-acquire the lock - either on return from virCondWait() or to re-enter the guard at the top of its loop - before it can observe @priv->udevThreadQuit and return. nodeStateShutdownPrepare() sets udevThreadQuit, signals the condition and drops the lock. Which thread grabs the lock next then decides the outcome: - the udev thread wins: it sees udevThreadQuit, returns, and the subsequent join completes immediately. - nodeStateShutdownWait() wins: it takes the lock and blocks in virThreadJoin(), so the udev thread can never re-acquire the lock, never observes udevThreadQuit and never returns. Both threads are then stuck for good. Holding the lock across the join serves no purpose - udevThreadQuit has already been published by nodeStateShutdownPrepare(), @priv->udevThread is only assigned in nodeStateInitialize() and released by udevEventDataDispose(), and the udev thread holds a reference to @priv. Join outside the lock guard instead, which also restores the ordering nodeStateCleanup() used before commit e89d39f5b8f3 ("node_device_udev: Introduce and use `stateShutdownPrepare` and `stateShutdownWait`"): signal under the lock, release it, then join. Fixes: e89d39f5b8f30f0b3284ed2bd71cee1a8a3d707d Signed-off-by: Yifan Li Signed-off-by: Jianfeng Gao Signed-off-by: Lili Li Reviewed-by: Michal Privoznik --- How found: shutdown on Ubuntu 26.04 with libvirt 12.0.0(monolithic libvirtd, socket activated), found it was killed by systemd due to timeout. In our testing(repeated S5 cycles), it is about 4% hit rate. State of a hung daemon: tid comm wchan 14039 libvirtd poll_schedule_timeout 14108 udev-event futex_do_wait 14165 daemon-shutdown futex_do_wait Thread "daemon-shutdown" (LWP 14165): #5 __pthread_clockjoin_ex () #6 libvirt_driver_nodedev.so #7 virStateShutdownWait () from libvirt.so.0 Thread "udev-event" (LWP 14108): #1 __GI___lll_lock_wait (futex=3D0x7d55d001cc58) #2 __pthread_mutex_cond_lock (mutex=3D0x7d55d001cc58) #4 ___pthread_cond_wait () #5 virCondWait () from libvirt.so.0 #6 libvirt_driver_nodedev.so (gdb) x/4dw 0x7d55d001cc58 0x7d55d001cc58: 2 0 14165 2 __lock __count __owner __nusers __owner is the daemon-shutdown thread itself, parked in pthread_join() waiting for the very thread that needs that lock in order to exit. src/node_device/node_device_udev.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_devi= ce_udev.c index 104433fb88..ac9be10382 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -2482,10 +2482,17 @@ nodeStateShutdownWait(void) if (!priv) return 0; =20 - VIR_WITH_OBJECT_LOCK_GUARD(priv) { - if (priv->udevThread) - virThreadJoin(priv->udevThread); - } + /* Do not hold @priv's lock while joining the udev thread. + * udevEventHandleThread() must acquire that very lock in order to + * observe @priv->udevThreadQuit - already set by + * nodeStateShutdownPrepare() - and return. Joining while holding the + * lock therefore deadlocks the two threads against each other. + * + * @priv->udevThread is only ever assigned in nodeStateInitialize() and + * released by udevEventDataDispose(), and the udev thread holds a + * reference to @priv, so accessing it here without the lock is safe. = */ + if (priv->udevThread) + virThreadJoin(priv->udevThread); =20 if (priv->workerPool) virThreadPoolDrain(priv->workerPool); --=20 2.48.1