From nobody Fri May 10 07:12:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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 170.10.133.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=1652971781; cv=none; d=zohomail.com; s=zohoarc; b=P6zkx6R/1Fa3oRLke2tEnkmdJtwPAa9vfYjnUMjQcG8TXeg4ZheXU1PFjQXqX422/iP7TnBF1BB8HMH0WwikKuhJj1L9HBUVP0TndiFNrusCYmlZJhoxjcqCRPS2w5ZyHO/W1dmN4rgpm0NsSoED/wAtg6eYj8WFKaAn7Xf5/yg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1652971781; 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=5/BKT9ZlAl/1GwWFjkLz0gPLAY7q2TKUjIsLVjUPXVQ=; b=DYjGlnuLXneUggmxmIS+g7JoEc6+6jt7j6aKLTOeckY0u9F3pBuOaRcMn7X6P9IySvy+MwbFg5vlzPRBgOphCHUn0b/QOzuxEzfMnJVnMLxCQFzPJg2qKROtt8KmUfCZ9nUr/BFJYhBRG/y+eJVf6KE+viuj8ri9gWX0IGAaPHw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1652971781750713.9905075993272; Thu, 19 May 2022 07:49:41 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-62-6glzZfc0M1CLSXEvmuy1CQ-1; Thu, 19 May 2022 10:49:39 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 77F16800882; Thu, 19 May 2022 14:49:37 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 31D831410F36; Thu, 19 May 2022 14:49:37 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 92513194705A; Thu, 19 May 2022 14:49:36 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 0CCC41947042 for ; Thu, 19 May 2022 14:49:35 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id E1B1A7AD5; Thu, 19 May 2022 14:49:34 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.180]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8972B7774 for ; Thu, 19 May 2022 14:49:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652971780; 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=5/BKT9ZlAl/1GwWFjkLz0gPLAY7q2TKUjIsLVjUPXVQ=; b=MBqmawdc0kPN04EvJgTO4Wjh6yyBC2Xza23WqHP/KkXM3qHf7LGwLvn6oj+a4bUAJVEv4g KSEdIdUSzgkG+ZdFzNxS375fd+yUaliaRNv4I/ae+cgapkCI6Moj1rHelxQpmZM01mu108 P9b3ADhUY7Jr/M1C3BBTm/D0tDyEatI= X-MC-Unique: 6glzZfc0M1CLSXEvmuy1CQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2] remote_daemon: Don't run virStateCleanup() if virStateReload() is still running Date: Thu, 19 May 2022 16:49:32 +0200 Message-Id: <56f95c8761bf5257d9bbba1b95190f1ffac69daf.1652971647.git.mprivozn@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 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) X-ZM-MESSAGEID: 1652971782737100001 Content-Type: text/plain; charset="utf-8"; x-default="true" When a SIGHUP is received a thread is spawned that runs virStateReload(). However, if SIGINT is received while the former thread is still running then we may get into problematic situation: the cleanup code in main() sees drivers initialized and thus calls virStateCleanup(). So now we have two threads, one running virStateReload() the other virStateCleanup(). In this situation it's very likely that a race condition occurs and either of threads causes SIGSEGV. To fix this, unmark drivers as initialized in the virStateReload() thread for the time the function runs. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D2075837 Signed-off-by: Michal Privoznik Reviewed-by: Martin Kletzander --- v2 of: https://listman.redhat.com/archives/libvir-list/2022-April/230415.html diff to v1: - reworked how int is set (instead of inc/dec I'm using set(0)/set(1)) so that reload can be attempted again and again if previous attempt failed. src/remote/remote_daemon.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index 26469e0d9f..b8ecc51758 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -77,7 +77,7 @@ virNetSASLContext *saslCtxt =3D NULL; virNetServerProgram *remoteProgram =3D NULL; virNetServerProgram *qemuProgram =3D NULL; =20 -volatile bool driversInitialized =3D false; +volatile gint driversInitialized =3D 0; =20 static void daemonErrorHandler(void *opaque G_GNUC_UNUSED, virErrorPtr err G_GNUC_UNUSED) @@ -453,8 +453,13 @@ static void daemonReloadHandlerThread(void *opaque G_G= NUC_UNUSED) VIR_INFO("Reloading configuration on SIGHUP"); virHookCall(VIR_HOOK_DRIVER_DAEMON, "-", VIR_HOOK_DAEMON_OP_RELOAD, SIGHUP, "SIGHUP", NULL, NULL); - if (virStateReload() < 0) + + if (virStateReload() < 0) { VIR_WARN("Error while reloading drivers"); + } + + /* Drivers are initialized again. */ + g_atomic_int_set(&driversInitialized, 1); } =20 static void daemonReloadHandler(virNetDaemon *dmn G_GNUC_UNUSED, @@ -463,7 +468,7 @@ static void daemonReloadHandler(virNetDaemon *dmn G_GNU= C_UNUSED, { virThread thr; =20 - if (!driversInitialized) { + if (!g_atomic_int_compare_and_exchange(&driversInitialized, 1, 0)) { VIR_WARN("Drivers are not initialized, reload ignored"); return; } @@ -474,6 +479,10 @@ static void daemonReloadHandler(virNetDaemon *dmn G_GN= UC_UNUSED, * Not much we can do on error here except log it. */ VIR_ERROR(_("Failed to create thread to handle daemon restart")); + + /* Drivers were initialized at the beginning, otherwise we wouldn't + * even get here. */ + g_atomic_int_set(&driversInitialized, 1); } } =20 @@ -607,7 +616,7 @@ static void daemonRunStateInit(void *opaque) goto cleanup; } =20 - driversInitialized =3D true; + g_atomic_int_set(&driversInitialized, 1); =20 virNetDaemonSetShutdownCallbacks(dmn, virStateShutdownPrepare, @@ -1212,10 +1221,9 @@ int main(int argc, char **argv) { cleanup: virNetlinkEventServiceStopAll(); =20 - if (driversInitialized) { + if (g_atomic_int_compare_and_exchange(&driversInitialized, 1, 0)) { /* NB: Possible issue with timing window between driversInitialized * setting if virNetlinkEventServerStart fails */ - driversInitialized =3D false; virStateCleanup(); } =20 --=20 2.35.1