From nobody Fri May 16 03:47:32 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501259961529520.8676776258937; Fri, 28 Jul 2017 09:39:21 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4BE0263772; Fri, 28 Jul 2017 16:39:18 +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 EE6DB7E2F4; Fri, 28 Jul 2017 16:39:17 +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 9BF02180B467; Fri, 28 Jul 2017 16:39:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v6SGd6Yq010336 for ; Fri, 28 Jul 2017 12:39:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3D4E25D747; Fri, 28 Jul 2017 16:39:06 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-46.phx2.redhat.com [10.3.117.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 063EC5E254 for ; Fri, 28 Jul 2017 16:39:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4BE0263772 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Fri, 28 Jul 2017 12:38:56 -0400 Message-Id: <20170728163901.21444-3-jferlan@redhat.com> In-Reply-To: <20170728163901.21444-1-jferlan@redhat.com> References: <20170728163901.21444-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/7] util: Introduce and use virObjectLockWrite 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 28 Jul 2017 16:39:19 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Instead of making virObjectLock be the entry point for two different types of locks, let's create a virObjectLockWrite API which will be able to return status and force the (new) consumers of the RWLock to make sure the lock is really obtained when the "right" object type is passed. Signed-off-by: John Ferlan --- src/conf/virdomainobjlist.c | 18 ++++++++++++++---- src/libvirt_private.syms | 1 + src/util/virobject.c | 32 ++++++++++++++++++++++++++++++++ src/util/virobject.h | 4 ++++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c index fed4029..08a51cc 100644 --- a/src/conf/virdomainobjlist.c +++ b/src/conf/virdomainobjlist.c @@ -330,7 +330,8 @@ virDomainObjPtr virDomainObjListAdd(virDomainObjListPtr= doms, { virDomainObjPtr ret; =20 - virObjectLock(doms); + if (virObjectLockWrite(doms) < 0) + return NULL; ret =3D virDomainObjListAddLocked(doms, def, xmlopt, flags, oldDef); virObjectUnlock(doms); return ret; @@ -352,7 +353,9 @@ void virDomainObjListRemove(virDomainObjListPtr doms, virObjectRef(dom); virObjectUnlock(dom); =20 - virObjectLock(doms); + /* We really shouldn't ignore this, + * but that ship sailed a long time ago */ + ignore_value(virObjectLockWrite(doms)); virObjectLock(dom); virHashRemoveEntry(doms->objs, uuidstr); virHashRemoveEntry(doms->objsName, dom->def->name); @@ -397,9 +400,13 @@ virDomainObjListRename(virDomainObjListPtr doms, * hold a lock on dom but not refcount it. */ virObjectRef(dom); virObjectUnlock(dom); - virObjectLock(doms); + rc =3D virObjectLockWrite(doms); virObjectLock(dom); virObjectUnref(dom); + if (rc < 0) { + VIR_FREE(old_name); + return ret; + } =20 if (virHashLookup(doms->objsName, new_name) !=3D NULL) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -576,7 +583,10 @@ virDomainObjListLoadAllConfigs(virDomainObjListPtr dom= s, if ((rc =3D virDirOpenIfExists(&dir, configDir)) <=3D 0) return rc; =20 - virObjectLock(doms); + if (virObjectLockWrite(doms) < 0) { + VIR_DIR_CLOSE(dir); + return -1; + } =20 while ((ret =3D virDirRead(dir, &entry, configDir)) > 0) { virDomainObjPtr dom; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 37b815c..f1a6510 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2306,6 +2306,7 @@ virObjectListFreeCount; virObjectLock; virObjectLockableNew; virObjectLockRead; +virObjectLockWrite; virObjectNew; virObjectRef; virObjectRWLockableNew; diff --git a/src/util/virobject.c b/src/util/virobject.c index 73de4d3..c48f88c 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -429,6 +429,38 @@ virObjectLockRead(void *anyobj) =20 =20 /** + * virObjectLockWrite: + * @anyobj: any instance of virObjectRWLockable + * + * Acquire a write lock on @anyobj. The lock must be + * released by virObjectUnlock. + * + * The caller is expected to have acquired a reference + * on the object before locking it (eg virObjectRef). + * The object must be unlocked before releasing this + * reference. + * + * Returns 0 on success, -1 on failure + */ +int +virObjectLockWrite(void *anyobj) +{ + if (virObjectIsClass(anyobj, virObjectRWLockableClass)) { + virObjectRWLockablePtr obj =3D anyobj; + virRWLockWrite(&obj->lock); + return 0; + } else { + virObjectPtr obj =3D anyobj; + VIR_WARN("Object %p (%s) is not a virObjectRWLockable instance", + anyobj, obj ? obj->klass->name : "(unknown)"); + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unable to obtain rwlock for object=3D%p"), anyob= j); + } + return -1; +} + + +/** * virObjectUnlock: * @anyobj: any instance of virObjectLockable or virObjectRWLockable * diff --git a/src/util/virobject.h b/src/util/virobject.h index 99910e0..f0d1f97 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -128,6 +128,10 @@ int virObjectLockRead(void *lockableobj) ATTRIBUTE_NONNULL(1); =20 +int +virObjectLockWrite(void *lockableobj) + ATTRIBUTE_NONNULL(1); + void virObjectUnlock(void *lockableobj) ATTRIBUTE_NONNULL(1); --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list