diff mbox series

tpm: tpm_try_transmit() refactor error flow.

Message ID 20181016133716.17842-1-tomas.winkler@intel.com (mailing list archive)
State New, archived
Headers show
Series tpm: tpm_try_transmit() refactor error flow. | expand

Commit Message

Winkler, Tomas Oct. 16, 2018, 1:37 p.m. UTC
First, rename out_no_locality to out_locality for bailing out on
both tpm_cmd_ready() and tpm_request_locality() failure.
Second, ignore the return value of go_to_idle() as  it may override
the return value of the actual tpm operation, the go_to_idle() error
will be caught on any consequent command.
Last, fix the wrong 'goto out', that jumped back instead of forward.

Cc: stable@vger.kernel.org
Fixes: 627448e85c76 ("tpm: separate cmd_ready/go_idle from runtime_pm")
Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
---
 drivers/char/tpm/tpm-interface.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

Comments

Jarkko Sakkinen Oct. 18, 2018, 12:14 a.m. UTC | #1
On Tue, 16 Oct 2018, Tomas Winkler wrote:
> First, rename out_no_locality to out_locality for bailing out on
> both tpm_cmd_ready() and tpm_request_locality() failure.

This is unnecessary change and technically it is not a rename: the
commit message text and the code change do not match. Rename is just a
rename (i.e. change a variable name foo to bar).

/Jarkko
Winkler, Tomas Oct. 18, 2018, 6:06 a.m. UTC | #2
> -----Original Message-----
> From: Jarkko Sakkinen [mailto:jarkko.sakkinen@linux.intel.com]
> Sent: Thursday, October 18, 2018 03:15
> To: Winkler, Tomas <tomas.winkler@intel.com>
> Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>; Jason Gunthorpe
> <jgg@ziepe.ca>; Nayna Jain <nayna@linux.vnet.ibm.com>; Usyskin,
> Alexander <alexander.usyskin@intel.com>; Struk, Tadeusz
> <tadeusz.struk@intel.com>; linux-integrity@vger.kernel.org; linux-security-
> module@vger.kernel.org; linux-kernel@vger.kernel.org;
> stable@vger.kernel.org
> Subject: Re: [PATCH] tpm: tpm_try_transmit() refactor error flow.
> 
> On Tue, 16 Oct 2018, Tomas Winkler wrote:
> > First, rename out_no_locality to out_locality for bailing out on both
> > tpm_cmd_ready() and tpm_request_locality() failure.
> 
> This is unnecessary change and technically it is not a rename: the commit
> message text and the code change do not match. Rename is just a rename
> (i.e. change a variable name foo to bar).

I'm renaming the label because it doesn't match the code flow anymore,
I can change the commit message, but you please review the code.
Tomas
Jarkko Sakkinen Oct. 19, 2018, 11:34 p.m. UTC | #3
On Thu, 18 Oct 2018, Winkler, Tomas wrote:
>> -----Original Message-----
>> From: Jarkko Sakkinen [mailto:jarkko.sakkinen@linux.intel.com]
>> Sent: Thursday, October 18, 2018 03:15
>> To: Winkler, Tomas <tomas.winkler@intel.com>
>> Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>; Jason Gunthorpe
>> <jgg@ziepe.ca>; Nayna Jain <nayna@linux.vnet.ibm.com>; Usyskin,
>> Alexander <alexander.usyskin@intel.com>; Struk, Tadeusz
>> <tadeusz.struk@intel.com>; linux-integrity@vger.kernel.org; linux-security-
>> module@vger.kernel.org; linux-kernel@vger.kernel.org;
>> stable@vger.kernel.org
>> Subject: Re: [PATCH] tpm: tpm_try_transmit() refactor error flow.
>>
>> On Tue, 16 Oct 2018, Tomas Winkler wrote:
>>> First, rename out_no_locality to out_locality for bailing out on both
>>> tpm_cmd_ready() and tpm_request_locality() failure.
>>
>> This is unnecessary change and technically it is not a rename: the commit
>> message text and the code change do not match. Rename is just a rename
>> (i.e. change a variable name foo to bar).
>
> I'm renaming the label because it doesn't match the code flow anymore,
> I can change the commit message, but you please review the code.
> Tomas

The flow change is unnecessary and does not really have anything to do
with the bug fix. Earlier version was better than this and would have
been fine when taking account the remark from Jason.

/Jarkko
Winkler, Tomas Oct. 20, 2018, 10:49 p.m. UTC | #4
> 
> On Thu, 18 Oct 2018, Winkler, Tomas wrote:
> >> -----Original Message-----
> >> From: Jarkko Sakkinen [mailto:jarkko.sakkinen@linux.intel.com]
> >> Sent: Thursday, October 18, 2018 03:15
> >> To: Winkler, Tomas <tomas.winkler@intel.com>
> >> Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>; Jason
> >> Gunthorpe <jgg@ziepe.ca>; Nayna Jain <nayna@linux.vnet.ibm.com>;
> >> Usyskin, Alexander <alexander.usyskin@intel.com>; Struk, Tadeusz
> >> <tadeusz.struk@intel.com>; linux-integrity@vger.kernel.org;
> >> linux-security- module@vger.kernel.org; linux-kernel@vger.kernel.org;
> >> stable@vger.kernel.org
> >> Subject: Re: [PATCH] tpm: tpm_try_transmit() refactor error flow.
> >>
> >> On Tue, 16 Oct 2018, Tomas Winkler wrote:
> >>> First, rename out_no_locality to out_locality for bailing out on
> >>> both
> >>> tpm_cmd_ready() and tpm_request_locality() failure.
> >>
> >> This is unnecessary change and technically it is not a rename: the
> >> commit message text and the code change do not match. Rename is just
> >> a rename (i.e. change a variable name foo to bar).
> >
> > I'm renaming the label because it doesn't match the code flow anymore,
> > I can change the commit message, but you please review the code.
> > Tomas
> 
> The flow change is unnecessary and does not really have anything to do with
> the bug fix. 

What I see in the original code is that when 
tpm_cmd_ready()  fails it's jumps to 'out' label and trying to do tpm_go_idle()
but instead it should just undoing the locality, so both cmd_read and go idle had a wrong 
jump. I see both should  be fixed.

Earlier version was better than this and would have been fine
> when taking account the remark from Jason.
Jarkko Sakkinen Oct. 23, 2018, 1:27 p.m. UTC | #5
On Sat, 20 Oct 2018, Winkler, Tomas wrote:

>>
>> On Thu, 18 Oct 2018, Winkler, Tomas wrote:
>>>> -----Original Message-----
>>>> From: Jarkko Sakkinen [mailto:jarkko.sakkinen@linux.intel.com]
>>>> Sent: Thursday, October 18, 2018 03:15
>>>> To: Winkler, Tomas <tomas.winkler@intel.com>
>>>> Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>; Jason
>>>> Gunthorpe <jgg@ziepe.ca>; Nayna Jain <nayna@linux.vnet.ibm.com>;
>>>> Usyskin, Alexander <alexander.usyskin@intel.com>; Struk, Tadeusz
>>>> <tadeusz.struk@intel.com>; linux-integrity@vger.kernel.org;
>>>> linux-security- module@vger.kernel.org; linux-kernel@vger.kernel.org;
>>>> stable@vger.kernel.org
>>>> Subject: Re: [PATCH] tpm: tpm_try_transmit() refactor error flow.
>>>>
>>>> On Tue, 16 Oct 2018, Tomas Winkler wrote:
>>>>> First, rename out_no_locality to out_locality for bailing out on
>>>>> both
>>>>> tpm_cmd_ready() and tpm_request_locality() failure.
>>>>
>>>> This is unnecessary change and technically it is not a rename: the
>>>> commit message text and the code change do not match. Rename is just
>>>> a rename (i.e. change a variable name foo to bar).
>>>
>>> I'm renaming the label because it doesn't match the code flow anymore,
>>> I can change the commit message, but you please review the code.
>>> Tomas
>>
>> The flow change is unnecessary and does not really have anything to do with
>> the bug fix.
>
> What I see in the original code is that when
> tpm_cmd_ready()  fails it's jumps to 'out' label and trying to do tpm_go_idle()
> but instead it should just undoing the locality, so both cmd_read and go idle had a wrong
> jump. I see both should  be fixed.
>
> Earlier version was better than this and would have been fine
>> when taking account the remark from Jason.

So you are doing tpm_cmd_ready() change in order to prevent unnecessary
call to tpm_go_idle() that was previously i.e. before when that function
failed in jumped to out and called tpm_go_idle()?

This should be probably split into two commits if I understood right. You
did not have this updated mentioned in a changelog (there was no 
changelog).

Have you checked that if these were separate commits they would have the
same fixes line? Why not use the label name 'out' as the same label
handles bot 'locality' and 'no_locality'?


/Jarkko
Winkler, Tomas Oct. 23, 2018, 2:16 p.m. UTC | #6
> 
> 
> On Sat, 20 Oct 2018, Winkler, Tomas wrote:
> 
> >>
> >> On Thu, 18 Oct 2018, Winkler, Tomas wrote:
> >>>> -----Original Message-----
> >>>> From: Jarkko Sakkinen [mailto:jarkko.sakkinen@linux.intel.com]
> >>>> Sent: Thursday, October 18, 2018 03:15
> >>>> To: Winkler, Tomas <tomas.winkler@intel.com>
> >>>> Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>; Jason
> >>>> Gunthorpe <jgg@ziepe.ca>; Nayna Jain <nayna@linux.vnet.ibm.com>;
> >>>> Usyskin, Alexander <alexander.usyskin@intel.com>; Struk, Tadeusz
> >>>> <tadeusz.struk@intel.com>; linux-integrity@vger.kernel.org;
> >>>> linux-security- module@vger.kernel.org;
> >>>> linux-kernel@vger.kernel.org; stable@vger.kernel.org
> >>>> Subject: Re: [PATCH] tpm: tpm_try_transmit() refactor error flow.
> >>>>
bb> >>>> On Tue, 16 Oct 2018, Tomas Winkler wrote:
> >>>>> First, rename out_no_locality to out_locality for bailing out on
> >>>>> both
> >>>>> tpm_cmd_ready() and tpm_request_locality() failure.
> >>>>
> >>>> This is unnecessary change and technically it is not a rename: the
> >>>> commit message text and the code change do not match. Rename is
> >>>> just a rename (i.e. change a variable name foo to bar).
> >>>
> >>> I'm renaming the label because it doesn't match the code flow
> >>> anymore, I can change the commit message, but you please review the
> code.
> >>> Tomas
> >>
> >> The flow change is unnecessary and does not really have anything to
> >> do with the bug fix.
> >
> > What I see in the original code is that when
> > tpm_cmd_ready()  fails it's jumps to 'out' label and trying to do
> > tpm_go_idle() but instead it should just undoing the locality, so both
> > cmd_read and go idle had a wrong jump. I see both should  be fixed.
> >
> > Earlier version was better than this and would have been fine
> >> when taking account the remark from Jason.
> 
> So you are doing tpm_cmd_ready() change in order to prevent unnecessary
> call to tpm_go_idle() that was previously i.e. before when that function
> failed in jumped to out and called tpm_go_idle()?

> 
> This should be probably split into two commits if I understood right. You did
> not have this updated mentioned in a changelog (there was no changelog).

I'm not sure why to do it in two steps, this addresses the same issue.

> Have you checked that if these were separate commits they would have the
> same fixes line? Why not use the label name 'out' as the same label handles
> bot 'locality' and 'no_locality'?

To  the  out  label we jump after we are  done with locality and cmd read() before we jump to locality 'locality'.
We will need to add another variable to check If cmd_ready() was called or not in order to get rid of the extran label,
it's not internally tracked so far.
I'm not sure what is better, I prefer this short fix, as the transmit() has to be rewritten anyway at some point, the recursion what is there is not helthy. 

Thanks
Tomas
Jarkko Sakkinen Oct. 24, 2018, 8:59 a.m. UTC | #7
On Tue, 23 Oct 2018, Winkler, Tomas wrote:
> To  the  out  label we jump after we are  done with locality and cmd
> read() before we jump to locality 'locality'.  We will need to add
> another variable to check If cmd_ready() was called or not in order to
> get rid of the extran label, it's not internally tracked so far.

I think this is fine.

Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>

I tested this by assigning rc error code instead of requesting locality
in addition of testig unchanged code.

/Jarkko
Winkler, Tomas Oct. 24, 2018, 9:01 a.m. UTC | #8
> 
> On Tue, 23 Oct 2018, Winkler, Tomas wrote:
> > To  the  out  label we jump after we are  done with locality and cmd
> > read() before we jump to locality 'locality'.  We will need to add
> > another variable to check If cmd_ready() was called or not in order to
> > get rid of the extran label, it's not internally tracked so far.
> 
> I think this is fine.
> 
> Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> 
> I tested this by assigning rc error code instead of requesting locality in
> addition of testig unchanged code.
> 
Great
Thanks
Tomas
diff mbox series

Patch

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 129f640424b7..95db630dd722 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -477,13 +477,15 @@  static ssize_t tpm_try_transmit(struct tpm_chip *chip,
 
 	if (need_locality) {
 		rc = tpm_request_locality(chip, flags);
-		if (rc < 0)
-			goto out_no_locality;
+		if (rc < 0) {
+			need_locality = false;
+			goto out_locality;
+		}
 	}
 
 	rc = tpm_cmd_ready(chip, flags);
 	if (rc)
-		goto out;
+		goto out_locality;
 
 	rc = tpm2_prepare_space(chip, space, ordinal, buf);
 	if (rc)
@@ -547,14 +549,13 @@  static ssize_t tpm_try_transmit(struct tpm_chip *chip,
 		dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc);
 
 out:
-	rc = tpm_go_idle(chip, flags);
-	if (rc)
-		goto 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);
 
-out_no_locality:
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);