Message ID | 1503564371-26090-5-git-send-email-armbru@redhat.com |
---|---|
State | New |
Headers | show |
Hi On Thu, Aug 24, 2017 at 10:45 AM, Markus Armbruster <armbru@redhat.com> wrote: > 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. tpm_ > > 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 <stefanb@us.ibm.com> > Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> > --- > 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); > > 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); > > #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 = > QLIST_HEAD_INITIALIZER(tpm_backends); > > - > -#define TPM_MAX_MODELS 1 > - > static TPMDriverOps const *be_drivers[TPM_TYPE__MAX]; > +static bool tpm_models[TPM_MODEL__MAX]; > > -static enum TpmModel tpm_models[TPM_MAX_MODELS] = { > - TPM_MODEL__MAX, > -}; > - > -int tpm_register_model(enum TpmModel model) > +void tpm_register_model(enum TpmModel model) > { > - int i; > - > - for (i = 0; i < TPM_MAX_MODELS; i++) { > - if (tpm_models[i] == TPM_MODEL__MAX) { > - tpm_models[i] = 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 = 0; i < TPM_MAX_MODELS; i++) { > - if (tpm_models[i] == model) { > - return true; > - } > - } > - return false; > + tpm_models[model] = true; Ah, no need for an assert() here indeed (it took me a minute). > } > > const TPMDriverOps *tpm_get_backend_driver(const char *type) > @@ -270,7 +243,7 @@ TPMInfoList *qmp_query_tpm(Error **errp) > TPMInfoList *info, *head = NULL, *cur_item = NULL; > > QLIST_FOREACH(drv, &tpm_backends, list) { > - if (!tpm_model_is_registered(drv->fe_model)) { > + if (!tpm_models[drv->fe_model]) { > continue; > } > info = g_new0(TPMInfoList, 1); > @@ -317,7 +290,7 @@ TpmModelList *qmp_query_tpm_models(Error **errp) > TpmModelList *head = NULL, *prev = NULL, *cur_item; > > for (i = 0; i < TPM_MODEL__MAX; i++) { > - if (!tpm_model_is_registered(i)) { > + if (!tpm_models[i]) { > continue; > } > cur_item = g_new0(TpmModelList, 1); > -- > 2.7.5 > >
Marc-André Lureau <marcandre.lureau@gmail.com> writes: > Hi > > On Thu, Aug 24, 2017 at 10:45 AM, Markus Armbruster <armbru@redhat.com> wrote: >> 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. > > tpm_ Will fix. >> >> 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 <stefanb@us.ibm.com> >> Signed-off-by: Markus Armbruster <armbru@redhat.com> > > Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Thanks! [...]
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); 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); #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 = QLIST_HEAD_INITIALIZER(tpm_backends); - -#define TPM_MAX_MODELS 1 - static TPMDriverOps const *be_drivers[TPM_TYPE__MAX]; +static bool tpm_models[TPM_MODEL__MAX]; -static enum TpmModel tpm_models[TPM_MAX_MODELS] = { - TPM_MODEL__MAX, -}; - -int tpm_register_model(enum TpmModel model) +void tpm_register_model(enum TpmModel model) { - int i; - - for (i = 0; i < TPM_MAX_MODELS; i++) { - if (tpm_models[i] == TPM_MODEL__MAX) { - tpm_models[i] = 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 = 0; i < TPM_MAX_MODELS; i++) { - if (tpm_models[i] == model) { - return true; - } - } - return false; + tpm_models[model] = true; } const TPMDriverOps *tpm_get_backend_driver(const char *type) @@ -270,7 +243,7 @@ TPMInfoList *qmp_query_tpm(Error **errp) TPMInfoList *info, *head = NULL, *cur_item = NULL; QLIST_FOREACH(drv, &tpm_backends, list) { - if (!tpm_model_is_registered(drv->fe_model)) { + if (!tpm_models[drv->fe_model]) { continue; } info = g_new0(TPMInfoList, 1); @@ -317,7 +290,7 @@ TpmModelList *qmp_query_tpm_models(Error **errp) TpmModelList *head = NULL, *prev = NULL, *cur_item; for (i = 0; i < TPM_MODEL__MAX; i++) { - if (!tpm_model_is_registered(i)) { + if (!tpm_models[i]) { continue; } cur_item = g_new0(TpmModelList, 1);
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 <stefanb@us.ibm.com> Signed-off-by: Markus Armbruster <armbru@redhat.com> --- include/sysemu/tpm_backend.h | 2 +- tpm.c | 37 +++++-------------------------------- 2 files changed, 6 insertions(+), 33 deletions(-)