From nobody Mon Feb 9 16:45:18 2026 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=1646823848; cv=none; d=zohomail.com; s=zohoarc; b=lYxknlvQs8i/7DcEo3h3grkvoaXe1EYdtPZc0Z0urFik+AhuGqTBd4WGuWw98YdzPar7w8HvdHUGHKksg1ZrymkhNZxLJsVizJpAvT+POoefAoFLxYDgtBUsBXv+EbWKEZ7K7BysvINRbnzsQRxD0lRjIqRhseYkjX6u9VLCaoM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646823848; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7ZkuVDYsKtG+bTW0jpwUfnNLsoHpLUc4uroanm/atAc=; b=T1hW/By/FuGm9sKzF0XCNr03L4EdFNQU2r7LgX5KMCTu3Eyr/ERyO0HVCTGiz+WxZQHDE3c3TD5YqMViaVoxuZHF0wwrsYibKYHCkgm1sxp02gfZfICes7bWssIFVHGBvn6LOfj6ASNyB883FetGbXaLctP9OgVY73daWOtE+4c= 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 1646823848505406.8209737259067; Wed, 9 Mar 2022 03:04:08 -0800 (PST) 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-441-W64_MID-MW6dSiVUHQNwdw-1; Wed, 09 Mar 2022 06:04:04 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1EF7E296A612; Wed, 9 Mar 2022 11:04:02 +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 05210C080AA; Wed, 9 Mar 2022 11:04:02 +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 A3F52196BB9E; Wed, 9 Mar 2022 11:04:01 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id B35B4196A5DB for ; Wed, 9 Mar 2022 11:03:58 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 80A511059172; Wed, 9 Mar 2022 11:03:58 +0000 (UTC) Received: from work.redhat.com (unknown [10.39.195.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id C01D31059176; Wed, 9 Mar 2022 11:03:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646823847; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=7ZkuVDYsKtG+bTW0jpwUfnNLsoHpLUc4uroanm/atAc=; b=M/uAW9CfOuWZ4hLIK56aj9tTlz1b0hnopABwm7PFr41bFYrXDYCKmStW2LLh//40QImjiY 7kAxBllGdm2qwLXHQ1fKU4DyQxMAdjkAMN8PUAn8lwNNQnC0YiN++ipYnQWZKosZqSAa7o 7/IwsIPggOYuuXGBYVWTEIcCK8KSICU= X-MC-Unique: W64_MID-MW6dSiVUHQNwdw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH 10/12] virnetlink: Use automatic memory management Date: Wed, 9 Mar 2022 12:02:28 +0100 Message-Id: <20220309110230.24213-11-twiederh@redhat.com> In-Reply-To: <20220309110230.24213-1-twiederh@redhat.com> References: <20220309110230.24213-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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: , Cc: Tim Wiederhake Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.8 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: 1646823849728100001 Content-Type: text/plain; charset="utf-8" Signed-off-by: Tim Wiederhake --- src/util/virnetlink.c | 101 ++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 62 deletions(-) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index 3216765492..f15bb68b02 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -799,18 +799,6 @@ virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned= int recvbuflen) } =20 =20 -static void -virNetlinkEventServerLock(virNetlinkEventSrvPrivate *driver) -{ - virMutexLock(&driver->lock); -} - -static void -virNetlinkEventServerUnlock(virNetlinkEventSrvPrivate *driver) -{ - virMutexUnlock(&driver->lock); -} - /** * virNetlinkEventRemoveClientPrimitive: * @@ -857,6 +845,7 @@ virNetlinkEventCallback(int watch, int length; bool handled =3D false; g_autofree struct nlmsghdr *msg =3D NULL; + VIR_LOCK_GUARD lock =3D { NULL }; =20 length =3D nl_recv(srv->netlinknh, &peer, (unsigned char **)&msg, &creds); @@ -869,7 +858,7 @@ virNetlinkEventCallback(int watch, return; } =20 - virNetlinkEventServerLock(srv); + lock =3D virLockGuardLock(&srv->lock); =20 VIR_DEBUG("dispatching to max %d clients, called from event watch %d", (int)srv->handlesCount, watch); @@ -886,8 +875,6 @@ virNetlinkEventCallback(int watch, =20 if (!handled) VIR_DEBUG("event not handled."); - - virNetlinkEventServerUnlock(srv); } =20 /** @@ -916,20 +903,20 @@ virNetlinkEventServiceStop(unsigned int protocol) if (!server[protocol]) return 0; =20 - virNetlinkEventServerLock(srv); - nl_close(srv->netlinknh); - virNetlinkFree(srv->netlinknh); - virEventRemoveHandle(srv->eventwatch); + VIR_WITH_MUTEX_LOCK_GUARD(&srv->lock) { + nl_close(srv->netlinknh); + virNetlinkFree(srv->netlinknh); + virEventRemoveHandle(srv->eventwatch); =20 - /* free any remaining clients on the list */ - for (i =3D 0; i < srv->handlesCount; i++) { - if (srv->handles[i].deleted =3D=3D VIR_NETLINK_HANDLE_VALID) - virNetlinkEventRemoveClientPrimitive(i, protocol); - } + /* free any remaining clients on the list */ + for (i =3D 0; i < srv->handlesCount; i++) { + if (srv->handles[i].deleted =3D=3D VIR_NETLINK_HANDLE_VALID) + virNetlinkEventRemoveClientPrimitive(i, protocol); + } =20 - server[protocol] =3D NULL; - VIR_FREE(srv->handles); - virNetlinkEventServerUnlock(srv); + server[protocol] =3D NULL; + VIR_FREE(srv->handles); + } =20 virMutexDestroy(&srv->lock); VIR_FREE(srv); @@ -1014,9 +1001,9 @@ int virNetlinkEventServiceLocalPid(unsigned int proto= col) int virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups) { - virNetlinkEventSrvPrivate *srv; + g_autofree virNetlinkEventSrvPrivate *srv =3D NULL; + VIR_LOCK_GUARD lock =3D { NULL }; int fd; - int ret =3D -1; =20 if (protocol >=3D MAX_LINKS) { virReportSystemError(EINVAL, @@ -1031,34 +1018,32 @@ virNetlinkEventServiceStart(unsigned int protocol, = unsigned int groups) =20 srv =3D g_new0(virNetlinkEventSrvPrivate, 1); =20 - if (virMutexInit(&srv->lock) < 0) { - VIR_FREE(srv); + if (virMutexInit(&srv->lock) < 0) return -1; - } =20 - virNetlinkEventServerLock(srv); + lock =3D virLockGuardLock(&srv->lock); =20 /* Allocate a new socket and get fd */ if (!(srv->netlinknh =3D virNetlinkCreateSocket(protocol))) - goto error_locked; + goto error; =20 fd =3D nl_socket_get_fd(srv->netlinknh); if (fd < 0) { virReportSystemError(errno, "%s", _("cannot get netlink socket fd")); - goto error_server; + goto error; } =20 if (groups && nl_socket_add_membership(srv->netlinknh, groups) < 0) { virReportSystemError(errno, "%s", _("cannot add netlink membership")); - goto error_server; + goto error; } =20 if (nl_socket_set_nonblocking(srv->netlinknh)) { virReportSystemError(errno, "%s", _("cannot set netlink socket nonblocking")); - goto error_server; + goto error; } =20 if ((srv->eventwatch =3D virEventAddHandle(fd, @@ -1067,27 +1052,24 @@ virNetlinkEventServiceStart(unsigned int protocol, = unsigned int groups) srv, NULL)) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Failed to add netlink event handle watch")); - goto error_server; + goto error; } =20 srv->netlinkfd =3D fd; VIR_DEBUG("netlink event listener on fd: %i running", fd); =20 - ret =3D 0; - server[protocol] =3D srv; + server[protocol] =3D g_steal_pointer(&srv); + return 0; =20 - error_server: - if (ret < 0) { + error: + if (srv->netlinknh) { nl_close(srv->netlinknh); virNetlinkFree(srv->netlinknh); } - error_locked: - virNetlinkEventServerUnlock(srv); - if (ret < 0) { - virMutexDestroy(&srv->lock); - VIR_FREE(srv); - } - return ret; + + virLockGuardUnlock(&lock); + virMutexDestroy(&srv->lock); + return -1; } =20 /** @@ -1114,8 +1096,9 @@ virNetlinkEventAddClient(virNetlinkEventHandleCallbac= k handleCB, unsigned int protocol) { size_t i; - int r, ret =3D -1; + int r; virNetlinkEventSrvPrivate *srv =3D NULL; + VIR_LOCK_GUARD lock =3D { NULL }; =20 if (protocol >=3D MAX_LINKS) return -EINVAL; @@ -1128,7 +1111,7 @@ virNetlinkEventAddClient(virNetlinkEventHandleCallbac= k handleCB, return -1; } =20 - virNetlinkEventServerLock(srv); + lock =3D virLockGuardLock(&srv->lock); =20 VIR_DEBUG("adding client: %d.", nextWatch); =20 @@ -1163,10 +1146,7 @@ virNetlinkEventAddClient(virNetlinkEventHandleCallba= ck handleCB, =20 VIR_DEBUG("added client to loop slot: %d. with macaddr ptr=3D%p", r, m= acaddr); =20 - ret =3D nextWatch++; - - virNetlinkEventServerUnlock(srv); - return ret; + return nextWatch++; } =20 /** @@ -1187,8 +1167,8 @@ virNetlinkEventRemoveClient(int watch, const virMacAd= dr *macaddr, unsigned int protocol) { size_t i; - int ret =3D -1; virNetlinkEventSrvPrivate *srv =3D NULL; + VIR_LOCK_GUARD lock =3D { NULL }; =20 if (protocol >=3D MAX_LINKS) return -EINVAL; @@ -1202,7 +1182,7 @@ virNetlinkEventRemoveClient(int watch, const virMacAd= dr *macaddr, return -1; } =20 - virNetlinkEventServerLock(srv); + lock =3D virLockGuardLock(&srv->lock); =20 for (i =3D 0; i < srv->handlesCount; i++) { if (srv->handles[i].deleted !=3D VIR_NETLINK_HANDLE_VALID) @@ -1215,15 +1195,12 @@ virNetlinkEventRemoveClient(int watch, const virMac= Addr *macaddr, VIR_DEBUG("removed client: %d by %s.", srv->handles[i].watch, watch ? "index" : "mac"); virNetlinkEventRemoveClientPrimitive(i, protocol); - ret =3D 0; - goto cleanup; + return 0; } } VIR_DEBUG("no client found to remove."); =20 - cleanup: - virNetlinkEventServerUnlock(srv); - return ret; + return -1; } =20 #else --=20 2.31.1