From nobody Tue May 14 06:07:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.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.129.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=1647531066; cv=none; d=zohomail.com; s=zohoarc; b=REdfbqCoVw082z0n/XMLTgf5w6OesXei145U3pdIU79/ujHTEe6m5AcmdF5pdOjo2K7BTQ9eJqinmTpt5b0ei4FEBBGVsBqXoXm4k1yW3FbVYO9/Hw3TBUrhM4WSMbz3pd0XwyNGmjTOyKSJK0FXLtcLmjrTgVl6aoC8OziNMvo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1647531066; 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=YdUwGvbnWPEhzCCCDvewzD/fxueiVQ1XuDJUTLTKh/k=; b=Z3bryjiFmQrZW0RBXWbSpF0iU++4YYv+g2ZixlUUzkZ3gPlKl9STb8gfAIQUkBHw7CnIm3w+6OC8OHTNJoS2wnboRICEYbXQiiwvEUdTMIXY5zzGJSd31Z/SxJVeBvi2kW8wQsaD2LONaSvdKCbLntY3pyXEUA0TxUrWBb99QOA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.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.129.124]) by mx.zohomail.com with SMTPS id 1647531066260642.2776746144197; Thu, 17 Mar 2022 08:31:06 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-656-dfNVp22TPt2fATSmTX-wlA-1; Thu, 17 Mar 2022 11:30:59 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 947873C19859; Thu, 17 Mar 2022 15:30:57 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 26C0040D2820; Thu, 17 Mar 2022 15:30:57 +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 C40DE194036C; Thu, 17 Mar 2022 15:30:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 381401940349 for ; Thu, 17 Mar 2022 15:30:53 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 8B6D640F9D5C; Thu, 17 Mar 2022 15:30:53 +0000 (UTC) Received: from domokun.gsslab.fab.redhat.com (gx270-2.gsslab.fab.redhat.com [10.33.8.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1F41040F9D53; Thu, 17 Mar 2022 15:30:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647531065; 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=YdUwGvbnWPEhzCCCDvewzD/fxueiVQ1XuDJUTLTKh/k=; b=LTccT3O/WdCRRTNDaIYcKGby8LV4ZV7pAlKont2EHeCmEs15RmBKvVdFpG1FE8XK+zxhAJ 6tPks/S9SJRTPClaU0SpiB1ZJHDz3K/pIf7G370mOi1meJnPpVlBVDopHN1VS0vKV41rKt WiNRn+H1z4M48OdTAlRLKuk9OwB2A/k= X-MC-Unique: dfNVp22TPt2fATSmTX-wlA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH v2] nwfilter: merge updateMutex and updateLock Date: Thu, 17 Mar 2022 15:30:52 +0000 Message-Id: <20220317153052.3671141-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 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.84 on 10.11.54.2 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1647531066663100001 The updateLock is a R/W lock held by anything which needs to read or modify the rules associated with an NWFilter. APIs for defining/undefining NW filters rules hold a write lock on updateLock. APIs for creating/deleting NW filter bindings hold a read lock on updateLock, which prevents define/undefine taking place concurrently. The problems arise when we attempt to creating two NW filter bindings in parallel. Thread 1 can acquire the mutex for filter A Thread 2 can acquire the mutex for filter B Consider if filters A and B both reference filters C and D, but in different orders: Filter A -> filter C -> filter D Filter B -> filter D -> filter C Thread 1 will try to acquire locks in order A, C, D while thread 1 will try to acquire in order A, D, C. Deadlock can still occur. Think we can sort the list of filters before acquiring locks on all of them ? Nope, we allow arbitrary recursion: Filter A -> filter C -> filter E -> filter F -> filter H -> filter K -> filter D -> filter G -> filter I So we can't tell from looking at 'A' which filters we're going to need to lock. We can only see the first level of filters references and we need to lock those before we can see the second level of filters, etc. We could probably come up with some cleverness to address this but it isn't worth the time investment. It is simpler to just keep the process of creating NW filter bindings totally serialized. Using two separate locks for this serialization though is pointless. Every code path which gets a read(updateLock) will go on to hold updateMutex. It is simpler to just hold write(updateLock) and get rid of updateMutex. At that point we don't need updateLock to be a R/W lock, it can be a plain mutex. Thus this patch gets rid of the current updateLock and updateMutex and introduces a new top level updateMutex. This has a secondary benefit of introducing fairness into the locking. With a POSIX R/W lock, you get writer starvation if you have lots of readers. IOW, if we call virNWFilterBIndingCreate and virNWFilterBindingDelete in a tight loop from a couple of threads, we can prevent virNWFilterDefine from ever acquiring a write lock. Getting rid of the R/W lock gives us FIFO lock acquisition preventing starvation of any API call servicing. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/conf/nwfilter_conf.c | 33 +--------- src/conf/nwfilter_conf.h | 9 --- src/conf/virnwfilterobj.h | 3 + src/libvirt_private.syms | 3 - src/nwfilter/nwfilter_driver.c | 77 ++++++++++++------------ src/nwfilter/nwfilter_gentech_driver.c | 83 +++----------------------- 6 files changed, 52 insertions(+), 156 deletions(-) diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index 3e1d4f5927..5171145c75 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -151,33 +151,6 @@ static const struct int_map chain_priorities[] =3D { }; =20 =20 -/* - * only one filter update allowed - */ -static virRWLock updateLock; -static bool initialized; - -void -virNWFilterReadLockFilterUpdates(void) -{ - virRWLockRead(&updateLock); -} - - -void -virNWFilterWriteLockFilterUpdates(void) -{ - virRWLockWrite(&updateLock); -} - - -void -virNWFilterUnlockFilterUpdates(void) -{ - virRWLockUnlock(&updateLock); -} - - /* * attribute names for the rules XML */ @@ -3059,6 +3032,7 @@ virNWFilterDefFormat(const virNWFilterDef *def) return virBufferContentAndReset(&buf); } =20 +static bool initialized; static virNWFilterTriggerRebuildCallback rebuildCallback; static void *rebuildOpaque; =20 @@ -3074,9 +3048,6 @@ virNWFilterConfLayerInit(virNWFilterTriggerRebuildCal= lback cb, =20 initialized =3D true; =20 - if (virRWLockInit(&updateLock) < 0) - return -1; - return 0; } =20 @@ -3087,8 +3058,6 @@ virNWFilterConfLayerShutdown(void) if (!initialized) return; =20 - virRWLockDestroy(&updateLock); - initialized =3D false; rebuildCallback =3D NULL; rebuildOpaque =3D NULL; diff --git a/src/conf/nwfilter_conf.h b/src/conf/nwfilter_conf.h index bbe12284a5..46867afc8c 100644 --- a/src/conf/nwfilter_conf.h +++ b/src/conf/nwfilter_conf.h @@ -552,15 +552,6 @@ virNWFilterDefParseString(const char *xml, virNWFilterDef * virNWFilterDefParseFile(const char *filename); =20 -void -virNWFilterWriteLockFilterUpdates(void); - -void -virNWFilterReadLockFilterUpdates(void); - -void -virNWFilterUnlockFilterUpdates(void); - typedef int (*virNWFilterTriggerRebuildCallback)(void *opaque); =20 int diff --git a/src/conf/virnwfilterobj.h b/src/conf/virnwfilterobj.h index 44ba31f732..30a9020097 100644 --- a/src/conf/virnwfilterobj.h +++ b/src/conf/virnwfilterobj.h @@ -43,6 +43,9 @@ struct _virNWFilterDriverState { char *stateDir; char *configDir; char *bindingDir; + + /* Recursive. Hold for filter changes, instantiation or deletion */ + virMutex updateLock; }; =20 virNWFilterDef * diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3a8e16525b..03697d81a8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -949,7 +949,6 @@ virNWFilterDeleteDef; virNWFilterJumpTargetTypeToString; virNWFilterPrintStateMatchFlags; virNWFilterPrintTCPFlags; -virNWFilterReadLockFilterUpdates; virNWFilterRuleActionTypeToString; virNWFilterRuleDirectionTypeToString; virNWFilterRuleIsProtocolEthernet; @@ -958,8 +957,6 @@ virNWFilterRuleIsProtocolIPv6; virNWFilterRuleProtocolTypeToString; virNWFilterSaveConfig; virNWFilterTriggerRebuild; -virNWFilterUnlockFilterUpdates; -virNWFilterWriteLockFilterUpdates; =20 =20 # conf/nwfilter_ipaddrmap.h diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c index a493205c80..732d83e563 100644 --- a/src/nwfilter/nwfilter_driver.c +++ b/src/nwfilter/nwfilter_driver.c @@ -176,6 +176,8 @@ nwfilterStateInitialize(bool privileged, driver->lockFD =3D -1; if (virMutexInit(&driver->lock) < 0) goto err_free_driverstate; + if (virMutexInitRecursive(&driver->updateLock) < 0) + goto err_free_driverstate; =20 driver->privileged =3D privileged; if (!(driver->nwfilters =3D virNWFilterObjListNew())) @@ -293,13 +295,12 @@ nwfilterStateReload(void) virNWFilterLearnThreadsTerminate(true); =20 nwfilterDriverLock(); - virNWFilterWriteLockFilterUpdates(); - - virNWFilterObjListLoadAllConfigs(driver->nwfilters, driver->configDir); =20 - virNWFilterUnlockFilterUpdates(); + VIR_WITH_MUTEX_LOCK_GUARD(&driver->updateLock) { + virNWFilterObjListLoadAllConfigs(driver->nwfilters, driver->config= Dir); =20 - virNWFilterBuildAll(driver, false); + virNWFilterBuildAll(driver, false); + } =20 nwfilterDriverUnlock(); =20 @@ -343,6 +344,7 @@ nwfilterStateCleanup(void) /* free inactive nwfilters */ virNWFilterObjListFree(driver->nwfilters); =20 + virMutexDestroy(&driver->updateLock); virMutexDestroy(&driver->lock); g_clear_pointer(&driver, g_free); =20 @@ -550,7 +552,6 @@ nwfilterDefineXMLFlags(virConnectPtr conn, } =20 nwfilterDriverLock(); - virNWFilterWriteLockFilterUpdates(); =20 if (!(def =3D virNWFilterDefParseString(xml, flags))) goto cleanup; @@ -558,8 +559,10 @@ nwfilterDefineXMLFlags(virConnectPtr conn, if (virNWFilterDefineXMLFlagsEnsureACL(conn, def) < 0) goto cleanup; =20 - if (!(obj =3D virNWFilterObjListAssignDef(driver->nwfilters, def))) - goto cleanup; + VIR_WITH_MUTEX_LOCK_GUARD(&driver->updateLock) { + if (!(obj =3D virNWFilterObjListAssignDef(driver->nwfilters, def))) + goto cleanup; + } def =3D NULL; objdef =3D virNWFilterObjGetDef(obj); =20 @@ -574,8 +577,6 @@ nwfilterDefineXMLFlags(virConnectPtr conn, virNWFilterDefFree(def); if (obj) virNWFilterObjUnlock(obj); - - virNWFilterUnlockFilterUpdates(); nwfilterDriverUnlock(); return nwfilter; } @@ -592,39 +593,39 @@ nwfilterDefineXML(virConnectPtr conn, static int nwfilterUndefine(virNWFilterPtr nwfilter) { - virNWFilterObj *obj; + virNWFilterObj *obj =3D NULL; virNWFilterDef *def; int ret =3D -1; =20 nwfilterDriverLock(); - virNWFilterWriteLockFilterUpdates(); =20 - if (!(obj =3D nwfilterObjFromNWFilter(nwfilter->uuid))) - goto cleanup; - def =3D virNWFilterObjGetDef(obj); + VIR_WITH_MUTEX_LOCK_GUARD(&driver->updateLock) { + if (!(obj =3D nwfilterObjFromNWFilter(nwfilter->uuid))) + goto cleanup; + def =3D virNWFilterObjGetDef(obj); =20 - if (virNWFilterUndefineEnsureACL(nwfilter->conn, def) < 0) - goto cleanup; + if (virNWFilterUndefineEnsureACL(nwfilter->conn, def) < 0) + goto cleanup; =20 - if (virNWFilterObjTestUnassignDef(obj) < 0) { - virReportError(VIR_ERR_OPERATION_INVALID, - "%s", - _("nwfilter is in use")); - goto cleanup; - } + if (virNWFilterObjTestUnassignDef(obj) < 0) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", + _("nwfilter is in use")); + goto cleanup; + } =20 - if (virNWFilterDeleteDef(driver->configDir, def) < 0) - goto cleanup; + if (virNWFilterDeleteDef(driver->configDir, def) < 0) + goto cleanup; =20 - virNWFilterObjListRemove(driver->nwfilters, obj); - obj =3D NULL; + virNWFilterObjListRemove(driver->nwfilters, obj); + obj =3D NULL; + } ret =3D 0; =20 cleanup: if (obj) virNWFilterObjUnlock(obj); =20 - virNWFilterUnlockFilterUpdates(); nwfilterDriverUnlock(); return ret; } @@ -764,14 +765,14 @@ nwfilterBindingCreateXML(virConnectPtr conn, if (!(ret =3D virGetNWFilterBinding(conn, def->portdevname, def->filte= r))) goto cleanup; =20 - virNWFilterReadLockFilterUpdates(); - if (virNWFilterInstantiateFilter(driver, def) < 0) { - virNWFilterUnlockFilterUpdates(); - virNWFilterBindingObjListRemove(driver->bindings, obj); - g_clear_pointer(&ret, virObjectUnref); - goto cleanup; + VIR_WITH_MUTEX_LOCK_GUARD(&driver->updateLock) { + if (virNWFilterInstantiateFilter(driver, def) < 0) { + virNWFilterBindingObjListRemove(driver->bindings, obj); + g_clear_pointer(&ret, virObjectUnref); + goto cleanup; + } } - virNWFilterUnlockFilterUpdates(); + virNWFilterBindingObjSave(obj, driver->bindingDir); =20 cleanup: @@ -808,9 +809,9 @@ nwfilterBindingDelete(virNWFilterBindingPtr binding) if (virNWFilterBindingDeleteEnsureACL(binding->conn, def) < 0) goto cleanup; =20 - virNWFilterReadLockFilterUpdates(); - virNWFilterTeardownFilter(def); - virNWFilterUnlockFilterUpdates(); + VIR_WITH_MUTEX_LOCK_GUARD(&driver->updateLock) { + virNWFilterTeardownFilter(def); + } virNWFilterBindingObjDelete(obj, driver->bindingDir); virNWFilterBindingObjListRemove(driver->bindings, obj); =20 diff --git a/src/nwfilter/nwfilter_gentech_driver.c b/src/nwfilter/nwfilter= _gentech_driver.c index c609405ac0..7e323afb1a 100644 --- a/src/nwfilter/nwfilter_gentech_driver.c +++ b/src/nwfilter/nwfilter_gentech_driver.c @@ -55,56 +55,10 @@ static virNWFilterTechDriver *filter_tech_drivers[] =3D= { NULL }; =20 -/* - * Serializes instantiation of filters. - * - * When instantiating a filter, we need to resolve references - * to other filters and acquire locks on them. - * - * We retain a lock on the referenced filter once found. - * The order in which the locks are acquired depends on - * the order in which filters reference each other. - * - * Filter A: - * Reference Filter C - * Reference Filter D - * - * Filter B: - * Reference Filter D - * Reference Filter C - * - * In one example, we lock A, C, D, in the other example - * we lock A, D, C. - * - * Because C & D are locked in differing orders we are - * once again at risk of deadlocks. - * - * There can be multiple levels of recursion, so it is - * not viable to determine the lock order upfront, it - * has to be done as we traverse the tree. - * - * Thus we serialize any code that needs to traverse - * the filter references. - * - * This covers the following APIs: - * - * virNWFilterDefineXML - * virNWFilterUndefine - * virNWFilterBindingCreate - * virNWFilterBindingDelete - * - * In addition to the asynchronous filter instantiation - * triggered by the IP address learning backends. - */ -static virMutex updateMutex; - int virNWFilterTechDriversInit(bool privileged) { size_t i =3D 0; VIR_DEBUG("Initializing NWFilter technology drivers"); - if (virMutexInitRecursive(&updateMutex) < 0) - return -1; - while (filter_tech_drivers[i]) { if (!(filter_tech_drivers[i]->flags & TECHDRV_FLAG_INITIALIZED)) filter_tech_drivers[i]->init(privileged); @@ -122,7 +76,6 @@ void virNWFilterTechDriversShutdown(void) filter_tech_drivers[i]->shutdown(); i++; } - virMutexDestroy(&updateMutex); } =20 =20 @@ -744,9 +697,6 @@ virNWFilterInstantiateFilterInternal(virNWFilterDriverS= tate *driver, bool *foundNewFilter) { int ifindex; - int rc; - - virMutexLock(&updateMutex); =20 /* after grabbing the filter update lock check for the interface; if it's not there anymore its filters will be or are being removed @@ -756,20 +706,14 @@ virNWFilterInstantiateFilterInternal(virNWFilterDrive= rState *driver, /* interfaces / VMs can disappear during filter instantiation; don't mark it as an error */ virResetLastError(); - rc =3D 0; - goto cleanup; + return 0; } =20 - rc =3D virNWFilterInstantiateFilterUpdate(driver, teardownOld, - binding, - ifindex, - useNewFilter, - false, foundNewFilter); - - cleanup: - virMutexUnlock(&updateMutex); - - return rc; + return virNWFilterInstantiateFilterUpdate(driver, teardownOld, + binding, + ifindex, + useNewFilter, + false, foundNewFilter); } =20 =20 @@ -778,11 +722,9 @@ virNWFilterInstantiateFilterLate(virNWFilterDriverStat= e *driver, virNWFilterBindingDef *binding, int ifindex) { - int rc; + int rc =3D 0; bool foundNewFilter =3D false; - - virNWFilterReadLockFilterUpdates(); - virMutexLock(&updateMutex); + VIR_LOCK_GUARD lock =3D virLockGuardLock(&driver->updateLock); =20 rc =3D virNWFilterInstantiateFilterUpdate(driver, true, binding, ifindex, @@ -798,9 +740,6 @@ virNWFilterInstantiateFilterLate(virNWFilterDriverState= *driver, } } =20 - virNWFilterUnlockFilterUpdates(); - virMutexUnlock(&updateMutex); - return rc; } =20 @@ -921,11 +860,7 @@ _virNWFilterTeardownFilter(const char *ifname) int virNWFilterTeardownFilter(virNWFilterBindingDef *binding) { - int ret; - virMutexLock(&updateMutex); - ret =3D _virNWFilterTeardownFilter(binding->portdevname); - virMutexUnlock(&updateMutex); - return ret; + return _virNWFilterTeardownFilter(binding->portdevname); } =20 enum { --=20 2.35.1