From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757180AbdELNaq (ORCPT ); Fri, 12 May 2017 09:30:46 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:52936 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755284AbdELNam (ORCPT ); Fri, 12 May 2017 09:30:42 -0400 Subject: Re: [PATCH v4 1/2] tpm: Refactor tpm_transmit pulling out tpm_transfer function To: tpmdd-devel@lists.sourceforge.net, linux-security-module@vger.kernel.org, jarkko.sakkinen@linux.intel.com References: <1494460462-29022-1-git-send-email-stefanb@linux.vnet.ibm.com> <1494460462-29022-2-git-send-email-stefanb@linux.vnet.ibm.com> Cc: jgunthorpe@obsidianresearch.com, linux-kernel@vger.kernel.org From: Stefan Berger Date: Fri, 12 May 2017 09:30:36 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <1494460462-29022-2-git-send-email-stefanb@linux.vnet.ibm.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 x-cbid: 17051213-2213-0000-0000-000001B62ACE X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007051; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000211; SDB=6.00859520; UDB=6.00426065; IPR=6.00639102; BA=6.00005345; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00015428; XFM=3.00000015; UTC=2017-05-12 13:30:39 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17051213-2214-0000-0000-00005616BE31 Message-Id: <1fd5572e-f816-9cc4-40f4-d8ac18aaacdb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-05-12_06:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1705120254 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/10/2017 07:54 PM, Stefan Berger wrote: > Refactor tpm_transmit and pull out code sending the command > and receiving the response and put this into tpm_transfer. > > Signed-off-by: Stefan Berger > --- > drivers/char/tpm/tpm-interface.c | 121 +++++++++++++++++++++++---------------- > 1 file changed, 73 insertions(+), 48 deletions(-) > > diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c > index 158c1db..263b6d1 100644 > --- a/drivers/char/tpm/tpm-interface.c > +++ b/drivers/char/tpm/tpm-interface.c > @@ -370,67 +370,29 @@ static bool tpm_validate_command(struct tpm_chip *chip, > } > > /** > - * tmp_transmit - Internal kernel interface to transmit TPM commands. > + * tmp_transfer - Send a TPM command to the TPM and receive response > * > * @chip: TPM chip to use > * @buf: TPM command buffer > + * @count: size of the TPM command > * @bufsiz: length of the TPM command buffer > - * @flags: tpm transmit flags - bitmap > * > * Return: > - * 0 when the operation is successful. > + * >0 when the operation is successful; returns response length > * A negative number for system errors (errno). > */ > -ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, > - u8 *buf, size_t bufsiz, unsigned int flags) > +ssize_t tpm_transfer(struct tpm_chip *chip, u8 *buf, u32 count, size_t bufsiz) > { > - struct tpm_output_header *header = (void *)buf; > int rc; > + struct tpm_output_header *header = (void *)buf; > + u32 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); > ssize_t len = 0; > - u32 count, ordinal; > unsigned long stop; > - bool need_locality; > - > - if (!tpm_validate_command(chip, space, buf, bufsiz)) > - return -EINVAL; > - > - if (bufsiz > TPM_BUFSIZE) > - bufsiz = TPM_BUFSIZE; > - > - count = be32_to_cpu(*((__be32 *) (buf + 2))); > - ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); > - if (count == 0) > - return -ENODATA; > - if (count > bufsiz) { > - dev_err(&chip->dev, > - "invalid count value %x %zx\n", count, bufsiz); > - return -E2BIG; > - } > - > - if (!(flags & TPM_TRANSMIT_UNLOCKED)) > - mutex_lock(&chip->tpm_mutex); > - > - if (chip->dev.parent) > - pm_runtime_get_sync(chip->dev.parent); > - > - /* Store the decision as chip->locality will be changed. */ > - need_locality = chip->locality == -1; > - > - if (need_locality && chip->ops->request_locality) { > - rc = chip->ops->request_locality(chip, 0); > - if (rc < 0) > - goto out_no_locality; > - chip->locality = rc; > - } > - > - rc = tpm2_prepare_space(chip, space, ordinal, buf); > - if (rc) > - goto out; > > rc = chip->ops->send(chip, (u8 *) buf, count); > if (rc < 0) { > dev_err(&chip->dev, > - "tpm_transmit: tpm_send: error %d\n", rc); > + "tpm_transfer: tpm_send: error %d\n", rc); > goto out; > } > > @@ -467,18 +429,81 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, missing here: rc = 0; > if (len < 0) { > rc = len; > dev_err(&chip->dev, > - "tpm_transmit: tpm_recv: error %d\n", rc); > + "tpm_transfer: tpm_recv: error %d\n", rc); > goto out; > } else if (len < TPM_HEADER_SIZE) { > rc = -EFAULT; > goto out; > } > > - if (len != be32_to_cpu(header->length)) { > + if (len != be32_to_cpu(header->length)) > rc = -EFAULT; > - goto out; > + > +out: > + return rc ? rc : len; > +} > +EXPORT_SYMBOL_GPL(tpm_transfer); > + > +/** > + * tmp_transmit - Internal kernel interface to transmit TPM commands. > + * > + * @chip: TPM chip to use > + * @buf: TPM command buffer > + * @bufsiz: length of the TPM command buffer > + * @flags: tpm transmit flags - bitmap > + * > + * Return: > + * 0 when the operation is successful. > + * A negative number for system errors (errno). > + */ > +ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, > + u8 *buf, size_t bufsiz, unsigned int flags) > +{ > + int rc; > + ssize_t len = 0; > + u32 count, ordinal; > + bool need_locality; > + > + if (!tpm_validate_command(chip, space, buf, bufsiz)) > + return -EINVAL; > + > + if (bufsiz > TPM_BUFSIZE) > + bufsiz = TPM_BUFSIZE; > + > + count = be32_to_cpu(*((__be32 *) (buf + 2))); > + ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); > + if (count == 0) > + return -ENODATA; > + if (count > bufsiz) { > + dev_err(&chip->dev, > + "invalid count value %x %zx\n", count, bufsiz); > + return -E2BIG; > + } > + > + if (!(flags & TPM_TRANSMIT_UNLOCKED)) > + mutex_lock(&chip->tpm_mutex); > + > + if (chip->dev.parent) > + pm_runtime_get_sync(chip->dev.parent); > + > + /* Store the decision as chip->locality will be changed. */ > + need_locality = chip->locality == -1; > + > + if (need_locality && chip->ops->request_locality) { > + rc = chip->ops->request_locality(chip, 0); > + if (rc < 0) > + goto out_no_locality; > + chip->locality = rc; > } > > + rc = tpm2_prepare_space(chip, space, ordinal, buf); > + if (rc) > + goto out; > + > + len = tpm_transfer(chip, buf, count, bufsiz); > + if (len < 0) > + goto out; > + > rc = tpm2_commit_space(chip, space, ordinal, buf, &len); > > out: