From nobody Tue Nov 4 16:50:21 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 150356467193235.86882632381048; Thu, 24 Aug 2017 01:51:11 -0700 (PDT) Received: from localhost ([::1]:47293 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dknr4-0007BA-OK for importer@patchew.org; Thu, 24 Aug 2017 04:51:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37148) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dknmO-0003PM-Iu for qemu-devel@nongnu.org; Thu, 24 Aug 2017 04:46:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dknmK-0000MV-IS for qemu-devel@nongnu.org; Thu, 24 Aug 2017 04:46:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44256) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dknmK-0000Lv-8a for qemu-devel@nongnu.org; Thu, 24 Aug 2017 04:46:16 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 22CB5806A2; Thu, 24 Aug 2017 08:46:15 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-60.ams2.redhat.com [10.36.116.60]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2A42071C4E; Thu, 24 Aug 2017 08:46:13 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id A6B8B11386C7; Thu, 24 Aug 2017 10:46:11 +0200 (CEST) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 22CB5806A2 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=armbru@redhat.com From: Markus Armbruster To: qemu-devel@nongnu.org Date: Thu, 24 Aug 2017 10:45:59 +0200 Message-Id: <1503564371-26090-5-git-send-email-armbru@redhat.com> In-Reply-To: <1503564371-26090-1-git-send-email-armbru@redhat.com> References: <1503564371-26090-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 24 Aug 2017 08:46:15 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 04/16] tpm: Clean up model registration & lookup X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: marcandre.lureau@redhat.com, Stefan Berger , mdroth@linux.vnet.ibm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We have a strict separation between enum TpmModel and tpm_models[]: * TpmModel may have any number of members. It just happens to have one. * tpm_register_model() uses the first empty slot in tmp_models[]. If you register more than tpm_models[] has space, tpn_register_model() fails. Its caller silently ignores the failure. Register the same TpmModel more than once has no effect other than wasting tpm_models[] slots: tpm_model_is_registered() is happy with the first one it finds. Since we only ever register one model, and tpm_models[] has space for just that one, this contraption even works. Turn tpm_models[] into a straight map from enum TpmType to bool. Much simpler. Cc: Stefan Berger Signed-off-by: Markus Armbruster Reviewed-by: Marc-Andr=C3=A9 Lureau --- include/sysemu/tpm_backend.h | 2 +- tpm.c | 37 +++++-------------------------------- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/include/sysemu/tpm_backend.h b/include/sysemu/tpm_backend.h index 1d21c6b..b0a9731 100644 --- a/include/sysemu/tpm_backend.h +++ b/include/sysemu/tpm_backend.h @@ -226,7 +226,7 @@ TPMVersion tpm_backend_get_tpm_version(TPMBackend *s); TPMBackend *qemu_find_tpm(const char *id); =20 const TPMDriverOps *tpm_get_backend_driver(const char *type); -int tpm_register_model(enum TpmModel model); +void tpm_register_model(enum TpmModel model); void tpm_register_driver(const TPMDriverOps *tdo); =20 #endif diff --git a/tpm.c b/tpm.c index 6f39ec9..7635fc7 100644 --- a/tpm.c +++ b/tpm.c @@ -24,39 +24,12 @@ static QLIST_HEAD(, TPMBackend) tpm_backends =3D QLIST_HEAD_INITIALIZER(tpm_backends); =20 - -#define TPM_MAX_MODELS 1 - static TPMDriverOps const *be_drivers[TPM_TYPE__MAX]; +static bool tpm_models[TPM_MODEL__MAX]; =20 -static enum TpmModel tpm_models[TPM_MAX_MODELS] =3D { - TPM_MODEL__MAX, -}; - -int tpm_register_model(enum TpmModel model) +void tpm_register_model(enum TpmModel model) { - int i; - - for (i =3D 0; i < TPM_MAX_MODELS; i++) { - if (tpm_models[i] =3D=3D TPM_MODEL__MAX) { - tpm_models[i] =3D model; - return 0; - } - } - error_report("Could not register TPM model"); - return 1; -} - -static bool tpm_model_is_registered(enum TpmModel model) -{ - int i; - - for (i =3D 0; i < TPM_MAX_MODELS; i++) { - if (tpm_models[i] =3D=3D model) { - return true; - } - } - return false; + tpm_models[model] =3D true; } =20 const TPMDriverOps *tpm_get_backend_driver(const char *type) @@ -270,7 +243,7 @@ TPMInfoList *qmp_query_tpm(Error **errp) TPMInfoList *info, *head =3D NULL, *cur_item =3D NULL; =20 QLIST_FOREACH(drv, &tpm_backends, list) { - if (!tpm_model_is_registered(drv->fe_model)) { + if (!tpm_models[drv->fe_model]) { continue; } info =3D g_new0(TPMInfoList, 1); @@ -317,7 +290,7 @@ TpmModelList *qmp_query_tpm_models(Error **errp) TpmModelList *head =3D NULL, *prev =3D NULL, *cur_item; =20 for (i =3D 0; i < TPM_MODEL__MAX; i++) { - if (!tpm_model_is_registered(i)) { + if (!tpm_models[i]) { continue; } cur_item =3D g_new0(TpmModelList, 1); --=20 2.7.5