From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E074C3A59B for ; Fri, 30 Aug 2019 21:54:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E4BB623431 for ; Fri, 30 Aug 2019 21:54:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728308AbfH3Vy3 (ORCPT ); Fri, 30 Aug 2019 17:54:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50053 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728111AbfH3Vy2 (ORCPT ); Fri, 30 Aug 2019 17:54:28 -0400 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E165E54F4C for ; Fri, 30 Aug 2019 21:54:27 +0000 (UTC) Received: by mail-qk1-f197.google.com with SMTP id n14so6416145qkk.22 for ; Fri, 30 Aug 2019 14:54:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:reply-to :mail-followup-to:references:mime-version:content-disposition :in-reply-to:user-agent; bh=l8M5VfLTIyhXhOjqX9IRgwW1sSZJqUoGoJtW856hZPA=; b=rrSi15DL9lsOP1RCQ/pf2p+2Cw5EyHoaiLR1X+EBXx8OpS5LO1xUDvOAjlFO26CaVE IYduGua4502Mz0vKSgT2QxsQ4X2cXMqs+D5ITjvWyut8vi13eTKVY7TrGsPLU5dR5Nfg i+bRxLfjMmOw8oV31CJ7OjjxdJitmAT5RXEAnQQRGpZHSEIJnSW/wNAGnr8WStvVGlEL 6AGOxMtkrXgs9FfBGeUSIZt+GtFs8Q4ccLmG+ym56prXH1wRtwv/wLJOrnGeMqlHVCPe el50xEUZ2hoRVMjyJUSiGUIDhCKjpTjzfC6BqLoDzCi+7Zyd4uNVg55NkEat491fm5h5 EhFg== X-Gm-Message-State: APjAAAWHFR3prXPZvwHP4xEnONd191RAgPH9lqEz4hek85+wy60aafFa /pPtUWy9Cu2dhMbYqPeeuUrxEXbs1BnDgTTyuGioPuODb4IoLpqwdgAV/aJoVty/eI6i9EnewiX ot3Dfx3Cj8cfa40Z40nnrnNIF X-Received: by 2002:ac8:845:: with SMTP id x5mr5146541qth.42.1567202067270; Fri, 30 Aug 2019 14:54:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqw4x7u2LiB6BsuBK15nT/Qv1VARIboK0aXL2heo/OgMEZUiGDq1Iqk3Kl9PkTRNGlpFH3BAIw== X-Received: by 2002:ac8:845:: with SMTP id x5mr5146526qth.42.1567202067039; Fri, 30 Aug 2019 14:54:27 -0700 (PDT) Received: from localhost (ip70-163-223-149.ph.ph.cox.net. [70.163.223.149]) by smtp.gmail.com with ESMTPSA id d25sm3190290qtn.51.2019.08.30.14.54.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2019 14:54:26 -0700 (PDT) Date: Fri, 30 Aug 2019 14:54:24 -0700 From: Jerry Snitselaar To: linux-integrity@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Alexey Klimov , Jarkko Sakkinen , Peter Huewe , Jason Gunthorpe Subject: Re: [PATCH 1/3 v3] tpm: Remove duplicate code from caps_show() in tpm-sysfs.c Message-ID: <20190830215424.4wahtwvbqx4fakso@cantor> Reply-To: Jerry Snitselaar Mail-Followup-To: linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, Alexey Klimov , Jarkko Sakkinen , Peter Huewe , Jason Gunthorpe References: <20190829204947.2591-1-jsnitsel@redhat.com> <20190829204947.2591-2-jsnitsel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20190829204947.2591-2-jsnitsel@redhat.com> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu Aug 29 19, Jerry Snitselaar wrote: >From: Jarkko Sakkinen > >Replace existing TPM 1.x version structs with new structs that consolidate >the common parts into a single struct so that code duplication is no longer >needed in caps_show(). > >Cc: Peter Huewe >Cc: Jason Gunthorpe >Cc: Alexey Klimov >Signed-off-by: Jarkko Sakkinen >Reviewed-by: Jerry Snitselaar >Tested-by: Jerry Snitselaar >--- > drivers/char/tpm/tpm-sysfs.c | 44 ++++++++++++++++++------------------ > drivers/char/tpm/tpm.h | 23 ++++++++----------- > 2 files changed, 32 insertions(+), 35 deletions(-) > >diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c >index d9caedda075b..e0550f0cfd8f 100644 >--- a/drivers/char/tpm/tpm-sysfs.c >+++ b/drivers/char/tpm/tpm-sysfs.c >@@ -217,6 +217,7 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr, > char *buf) > { > struct tpm_chip *chip = to_tpm_chip(dev); >+ struct tpm1_version *version; > ssize_t rc = 0; > char *str = buf; > cap_t cap; >@@ -232,31 +233,30 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr, > str += sprintf(str, "Manufacturer: 0x%x\n", > be32_to_cpu(cap.manufacturer_id)); > >- /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */ >- rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap, >+ /* TPM 1.2 */ >+ if (!tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap, > "attempting to determine the 1.2 version", >- sizeof(cap.tpm_version_1_2)); >- if (!rc) { >- str += sprintf(str, >- "TCG version: %d.%d\nFirmware version: %d.%d\n", >- cap.tpm_version_1_2.Major, >- cap.tpm_version_1_2.Minor, >- cap.tpm_version_1_2.revMajor, >- cap.tpm_version_1_2.revMinor); >- } else { >- /* Otherwise just use TPM_STRUCT_VER */ >- if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap, >- "attempting to determine the 1.1 version", >- sizeof(cap.tpm_version))) >- goto out_ops; >- str += sprintf(str, >- "TCG version: %d.%d\nFirmware version: %d.%d\n", >- cap.tpm_version.Major, >- cap.tpm_version.Minor, >- cap.tpm_version.revMajor, >- cap.tpm_version.revMinor); >+ sizeof(cap.version2))) { >+ version = &cap.version2.version; >+ goto out_print; > } >+ >+ /* TPM 1.1 */ >+ if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap, >+ "attempting to determine the 1.1 version", >+ sizeof(cap.version1))) { >+ version = &cap.version1; >+ goto out_ops; >+ } Jarkko, does the following change to the above block look good? I'll submit a v4. With the current patch it is setting version when the tpm1_getcap fails for 1.1 instead of when it succeeds. I only have 1.2 and 2.0 devices, so I didn't hit it when testing your patch. + /* TPM 1.1 */ + if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap, + "attempting to determine the 1.1 version", + sizeof(cap.version1))) + goto out_ops; + + version = &cap.version1; >+ >+out_print: >+ str += sprintf(str, >+ "TCG version: %d.%d\nFirmware version: %d.%d\n", >+ version->major, version->minor, >+ version->rev_major, version->rev_minor); >+ > rc = str - buf; >+ > out_ops: > tpm_put_ops(chip); > return rc; >diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h >index a7fea3e0ca86..a4f74dd02a35 100644 >--- a/drivers/char/tpm/tpm.h >+++ b/drivers/char/tpm/tpm.h >@@ -186,19 +186,16 @@ struct stclear_flags_t { > u8 bGlobalLock; > } __packed; > >-struct tpm_version_t { >- u8 Major; >- u8 Minor; >- u8 revMajor; >- u8 revMinor; >+struct tpm1_version { >+ u8 major; >+ u8 minor; >+ u8 rev_major; >+ u8 rev_minor; > } __packed; > >-struct tpm_version_1_2_t { >- __be16 tag; >- u8 Major; >- u8 Minor; >- u8 revMajor; >- u8 revMinor; >+struct tpm1_version2 { >+ __be16 tag; >+ struct tpm1_version version; > } __packed; > > struct timeout_t { >@@ -243,8 +240,8 @@ typedef union { > struct stclear_flags_t stclear_flags; > __u8 owned; > __be32 num_pcrs; >- struct tpm_version_t tpm_version; >- struct tpm_version_1_2_t tpm_version_1_2; >+ struct tpm1_version version1; >+ struct tpm1_version2 version2; > __be32 manufacturer_id; > struct timeout_t timeout; > struct duration_t duration; >-- >2.21.0 >