Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp5600345ima; Tue, 5 Feb 2019 14:49:20 -0800 (PST) X-Google-Smtp-Source: AHgI3Ib6PDKW/a/UDkpSZkPV/6ETRv8cSYj2MJKy+Xw5KnpGmnSxYwKhk3cfjszdZmt1B8SDaqeC X-Received: by 2002:a63:9c1a:: with SMTP id f26mr6705145pge.381.1549406960216; Tue, 05 Feb 2019 14:49:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549406960; cv=none; d=google.com; s=arc-20160816; b=eC1YRx93mxcfjZuq9J61q+jJwM6mOkqBTiHr/T3AV1HcKcAqMgBse4D8EgTR3ZFT2U 7Uhg/yY3yHA6xb/aA7AYUvAcRyRog/glNxdQ8pNJL/9YuXs4F1baHTOyL0ute/H2d1vq lrb7SL0oDgAu7JdhbqEpx2TaDGaHSlWyZfR/w/d3pdBSe7ixlQxJF5lF0om+HfxsFmj+ 2pIosKq9LzCioi1pG8dITSjj2VdbGu10IqVlGkGvHY6xoTH6XKXTgibfxnGLlZA/VST6 s3+sAeAzwPrwwzEvFFQuAiBMQLX0+0A3SR/QGpXXk5yjfz7ggzcgql/6fUitOF93LewB pPWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=UmYDYfFlfOTX1/YOx1VYv8LYGv6yXRfTpfug24ZKfBI=; b=kZpoBvG93jJHmi0lafQY4VpM01ff8eF/ccZQYnJ5eBVGMyLiOIHiPAkAOykA992+Fw lGMnBvGesKJmkwDyXIX2BP2OIQcuJdp0u9yo9IbgWF8Lt1TvNUZqXuqHG8W6g7VddRLD z4txzGMGb7NEaxLE4ib7F39/cpvDcDTg4MBRxzMeePWGl/ips3m5MIvzyBW0JAWn00lS w7hRFfCGYWl8V4dAXc/5Na51nxHvxcEMkm05CvGLT+iSACymSAv0ah4BQBMalEczj6iA tqDpti4YQDFrxZOPFG585+Hq9iV+T+wSm/O5DWBaqZKTBiHkPA+A8OLe+RlLUy0TUbMO iGSw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y66si598227pfa.40.2019.02.05.14.49.05; Tue, 05 Feb 2019 14:49:20 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730820AbfBEWsd (ORCPT + 99 others); Tue, 5 Feb 2019 17:48:33 -0500 Received: from mga06.intel.com ([134.134.136.31]:62937 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729902AbfBEWsb (ORCPT ); Tue, 5 Feb 2019 17:48:31 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Feb 2019 14:48:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,337,1544515200"; d="scan'208";a="113982193" Received: from ibanaga-mobl1.ger.corp.intel.com (HELO localhost) ([10.249.254.171]) by orsmga006.jf.intel.com with ESMTP; 05 Feb 2019 14:48:23 -0800 From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, Peter Huewe , Jason Gunthorpe , Tomas Winkler , Tadeusz Struk , Stefan Berger , Nayna Jain , Jarkko Sakkinen Subject: [PATCH v11 08/16] tpm: clean up tpm_try_transmit() error handling flow Date: Wed, 6 Feb 2019 00:47:15 +0200 Message-Id: <20190205224723.19671-9-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190205224723.19671-1-jarkko.sakkinen@linux.intel.com> References: <20190205224723.19671-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Move locking, locality handling and power management to tpm_transmit() in order to simplify the flow. Signed-off-by: Jarkko Sakkinen Reviewed-by: Stefan Berger Tested-by: Stefan Berger Reviewed-by: Jerry Snitselaar Reviewed-by: James Bottomley --- drivers/char/tpm/tpm-interface.c | 94 +++++++++++++++----------------- drivers/char/tpm/tpm.h | 1 + drivers/char/tpm/tpm2-space.c | 2 +- 3 files changed, 45 insertions(+), 52 deletions(-) diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c index c28ffef92f1a..f5f5224f68b0 100644 --- a/drivers/char/tpm/tpm-interface.c +++ b/drivers/char/tpm/tpm-interface.c @@ -167,7 +167,6 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space, ssize_t len = 0; u32 count, ordinal; unsigned long stop; - bool need_locality; rc = tpm_validate_command(chip, space, buf, bufsiz); if (rc == -EINVAL) @@ -197,37 +196,16 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space, return -E2BIG; } - if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED)) - mutex_lock(&chip->tpm_mutex); - - if (chip->ops->clk_enable != NULL) - chip->ops->clk_enable(chip, true); - - /* Store the decision as chip->locality will be changed. */ - need_locality = chip->locality == -1; - - if (need_locality) { - rc = tpm_request_locality(chip, flags); - if (rc < 0) { - need_locality = false; - goto out_locality; - } - } - - rc = tpm_cmd_ready(chip, flags); - if (rc) - goto out_locality; - rc = tpm2_prepare_space(chip, space, ordinal, buf); if (rc) - goto out; + return rc; rc = chip->ops->send(chip, buf, count); if (rc < 0) { if (rc != -EPIPE) dev_err(&chip->dev, "%s: tpm_send: error %d\n", __func__, rc); - goto out; + goto out_rc; } if (chip->flags & TPM_CHIP_FLAG_IRQ) @@ -243,7 +221,7 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space, if (chip->ops->req_canceled(chip, status)) { dev_err(&chip->dev, "Operation Canceled\n"); rc = -ECANCELED; - goto out; + goto out_rc; } tpm_msleep(TPM_TIMEOUT_POLL); @@ -253,40 +231,20 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space, chip->ops->cancel(chip); dev_err(&chip->dev, "Operation Timed out\n"); rc = -ETIME; - goto out; + goto out_rc; out_recv: len = chip->ops->recv(chip, buf, bufsiz); if (len < 0) { rc = len; - dev_err(&chip->dev, - "tpm_transmit: tpm_recv: error %d\n", rc); - goto out; - } else if (len < TPM_HEADER_SIZE) { + dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc); + } else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length)) rc = -EFAULT; - goto out; - } - if (len != be32_to_cpu(header->length)) { - rc = -EFAULT; - goto out; - } - - rc = tpm2_commit_space(chip, space, ordinal, buf, &len); +out_rc: + if (!rc) + rc = tpm2_commit_space(chip, space, ordinal, buf, &len); -out: - /* may fail but do not override previous error value in rc */ - tpm_go_idle(chip, flags); - -out_locality: - if (need_locality) - tpm_relinquish_locality(chip, flags); - - if (chip->ops->clk_enable != NULL) - chip->ops->clk_enable(chip, false); - - if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED)) - mutex_unlock(&chip->tpm_mutex); return rc ? rc : len; } @@ -316,6 +274,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, /* space for header and handles */ u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)]; unsigned int delay_msec = TPM2_DURATION_SHORT; + bool has_locality = false; u32 rc = 0; ssize_t ret; const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE, @@ -331,7 +290,40 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, memcpy(save, buf, save_size); for (;;) { + if (!(flags & TPM_TRANSMIT_UNLOCKED) && + !(flags & TPM_TRANSMIT_NESTED)) + mutex_lock(&chip->tpm_mutex); + + if (chip->ops->clk_enable != NULL) + chip->ops->clk_enable(chip, true); + + if (chip->locality == -1) { + ret = tpm_request_locality(chip, flags); + if (ret) + goto out_locality; + has_locality = true; + } + + ret = tpm_cmd_ready(chip, flags); + if (ret) + goto out_locality; + ret = tpm_try_transmit(chip, space, buf, bufsiz, flags); + + /* This may fail but do not override ret. */ + tpm_go_idle(chip, flags); + +out_locality: + if (has_locality) + tpm_relinquish_locality(chip, flags); + + if (chip->ops->clk_enable != NULL) + chip->ops->clk_enable(chip, false); + + if (!(flags & TPM_TRANSMIT_UNLOCKED) && + !(flags & TPM_TRANSMIT_NESTED)) + mutex_unlock(&chip->tpm_mutex); + if (ret < 0) break; rc = be32_to_cpu(header->return_code); diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h index 1454ef19d2f4..6eb67ccad2a3 100644 --- a/drivers/char/tpm/tpm.h +++ b/drivers/char/tpm/tpm.h @@ -576,6 +576,7 @@ int tpm2_probe(struct tpm_chip *chip); int tpm2_find_cc(struct tpm_chip *chip, u32 cc); int tpm2_init_space(struct tpm_space *space); void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space); +void tpm2_flush_space(struct tpm_chip *chip); int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc, u8 *cmd); int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c index 39cb3915771e..5d6487575074 100644 --- a/drivers/char/tpm/tpm2-space.c +++ b/drivers/char/tpm/tpm2-space.c @@ -162,7 +162,7 @@ static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf, return 0; } -static void tpm2_flush_space(struct tpm_chip *chip) +void tpm2_flush_space(struct tpm_chip *chip) { struct tpm_space *space = &chip->work_space; int i; -- 2.19.1