From nobody Wed May 8 17:38:35 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1615933665; cv=none; d=zohomail.com; s=zohoarc; b=PAgqsaYGEuMjVBjJYV15Nveh7QoMcks1C/QA9qYUlxqBUkGU1g5eRLwYApU+3kBMZwzVSjy9nk9Y0jxjtv8MaKiQnh4KFRBDMGtuRbaJJe/qC3RzLUXYzJzUUM9syDhZQASsFZu2/8j+qCVN7/yU1xrluZHVP7uPl2j4xDacOoA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615933665; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=8HFAYYTysJLzpgH9ZxZ8tmn17rNkN+7J0A0jnzZBncA=; b=atFQ2ugIUqKsBmb4xjW8shak18O87sTKundcPea6DOSaw4DHbAaLGCCxK0v94xjp036PlHChcIMStrOJ1aSTyBNhCLy4ItqjvAjauWfMKY9duj+dJoETZCzFYPgTIC6HHHZ6YxqUiSyoD688j6I42aUSsGGSB/ZXdO0IeG4GEg4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 161593366567182.99223535123122; Tue, 16 Mar 2021 15:27:45 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-528-k2kY-PS-OZGPbDPsIkxFsA-1; Tue, 16 Mar 2021 18:27:42 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 23FA4100C662; Tue, 16 Mar 2021 22:27:35 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 677F75B4A8; Tue, 16 Mar 2021 22:27:34 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 7BF6B1800657; Tue, 16 Mar 2021 22:27:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 12GMRT9c014879 for ; Tue, 16 Mar 2021 18:27:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id 10A201017CF5; Tue, 16 Mar 2021 22:27:29 +0000 (UTC) Received: from himantopus.redhat.com (ovpn-114-228.phx2.redhat.com [10.3.114.228]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D892510027A5 for ; Tue, 16 Mar 2021 22:27:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615933664; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=8HFAYYTysJLzpgH9ZxZ8tmn17rNkN+7J0A0jnzZBncA=; b=XbcB/f6uKsijyMq5eY532h74+qzC2Fly9caOMI2iBrUjcRHW72V64MMUPO2Y+vqdZibN5A O2YMHlzzg33igb7tJTeKVYlZ7Skbj5V7jaiqjjh+WDtebScKz4pWJxtIMAKUVGW26Ad7MZ 7NEHRJ9w2ARqhdGfvj8hSdo0rwDUDHo= X-MC-Unique: k2kY-PS-OZGPbDPsIkxFsA-1 From: Jonathon Jongsma To: libvir-list@redhat.com Subject: [libvirt PATCH] nodedev: Don't crash when exiting before init is done Date: Tue, 16 Mar 2021 17:27:25 -0500 Message-Id: <20210316222725.998020-1-jjongsma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" If libvirtd is terminated before the node driver finishes initialization, it can crash with a backtrace similar to the following: Stack trace of thread 1922933: #0 0x00007f8515178774 g_hash_table_find (libglib-2.0.so.0) #1 0x00007f851593ea98 virHashSearch (libvirt.so.0) #2 0x00007f8515a1dd83 virNodeDeviceObjListSearch (libvirt.so.0) #3 0x00007f84cceb40a1 udevAddOneDevice (libvirt_driver_nodedev.so) #4 0x00007f84cceb5fae nodeStateInitializeEnumerate (libvirt_driver_nod= edev.so) #5 0x00007f85159840cb virThreadHelper (libvirt.so.0) #6 0x00007f8511c7d14a start_thread (libpthread.so.0) #7 0x00007f851442bdb3 __clone (libc.so.6) Stack trace of thread 1922863: #0 0x00007f851442651d syscall (libc.so.6) #1 0x00007f85159842d4 virThreadSelfID (libvirt.so.0) #2 0x00007f851594e240 virLogFormatString (libvirt.so.0) #3 0x00007f851596635d vir_object_finalize (libvirt.so.0) #4 0x00007f8514efe8e9 g_object_unref (libgobject-2.0.so.0) #5 0x00007f85159667f8 virObjectUnref (libvirt.so.0) #6 0x00007f851517755f g_hash_table_remove_all_nodes.part.0 (libglib-2.= 0.so.0) #7 0x00007f8515177e62 g_hash_table_unref (libglib-2.0.so.0) #8 0x00007f851596637e vir_object_finalize (libvirt.so.0) #9 0x00007f8514efe8e9 g_object_unref (libgobject-2.0.so.0) #10 0x00007f85159667f8 virObjectUnref (libvirt.so.0) #11 0x00007f84cceb2b42 nodeStateCleanup (libvirt_driver_nodedev.so) #12 0x00007f8515b37950 virStateCleanup (libvirt.so.0) #13 0x00005648085348e8 main (libvirtd) #14 0x00007f8514352493 __libc_start_main (libc.so.6) #15 0x00005648085350fe _start (libvirtd) This is because the initial population of the device list is done in a separate initialization thread. If we attempt to exit libvirtd before this init thread has completed, we'll try to free the device list while accessing it from the other thread. In order to guarantee that this init thread is not accessing the device list when we're cleaning up the nodedev driver, make it joinable and wait for it to finish before proceding with the cleanup. This is similar to how we handle the udev event handler thread. The separate initialization thread was added in commit 9f0ae0b1. https://bugzilla.redhat.com/show_bug.cgi?id=3D1933590 Signed-off-by: Jonathon Jongsma Reviewed-by: Michal Privoznik --- src/node_device/node_device_udev.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_devi= ce_udev.c index fe25f8235e..010ebf4e74 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -66,6 +66,9 @@ struct _udevEventData { virCond threadCond; bool threadQuit; bool dataReady; + + /* init thread */ + virThread initThread; }; =20 static virClassPtr udevEventDataClass; @@ -1660,6 +1663,7 @@ nodeStateCleanup(void) priv->threadQuit =3D true; virCondSignal(&priv->threadCond); virObjectUnlock(priv); + virThreadJoin(&priv->initThread); virThreadJoin(&priv->th); } =20 @@ -1995,7 +1999,6 @@ nodeStateInitialize(bool privileged, { udevEventDataPtr priv =3D NULL; struct udev *udev =3D NULL; - virThread enumThread; =20 if (root !=3D NULL) { virReportError(VIR_ERR_INVALID_ARG, "%s", @@ -2103,7 +2106,7 @@ nodeStateInitialize(bool privileged, if (udevSetupSystemDev() !=3D 0) goto cleanup; =20 - if (virThreadCreateFull(&enumThread, false, nodeStateInitializeEnumera= te, + if (virThreadCreateFull(&priv->initThread, true, nodeStateInitializeEn= umerate, "nodedev-init", false, udev) < 0) { virReportSystemError(errno, "%s", _("failed to create udev enumerate thread")); --=20 2.26.2