Skip to contentSkip to conversation

tpm_tis: Issue a TPM2_Shutdown for TPM2 devices.

PATCH In review Josh Zimmerman 5 participants 25 messages Apr 27, 2017 – May 8, 2017 17 messages not in the archive TPM device driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm_tis: Issue a TPM2_Shutdown for TPM2 devices.

If a TPM2 loses power without a TPM2_Shutdown command being issued, it may lose some state that has yet to be persisted to NVRam, and will increment the DA counter (meaning that after too many disorderly reboots, the TPM will lock the user out).

This is a variant of https://patchwork.kernel.org/patch/9516631/.
It differs in that:
* It only changes behavior on TPM2 devices, to avoid invoking the unbounded-waiting sysfs codepath that was discussed on that patch.
* It modifies tpm_tis rather than tpm_i2c_infineon, so that it can change behavior for all TPM2 devices.

---
 drivers/char/tpm/tpm_tis.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/char/tpm/tpm_tis.c +12 -0
index c7e1384f1b08..bd9c70b305ab 100644
@@ -216,11 +216,23 @@ static void tpm_tis_pnp_remove(struct pnp_dev *dev)
 	tpm_tis_remove(chip);
 }
 
+static void tpm_tis_pnp_shutdown(struct pnp_dev *dev)
+{
+	struct tpm_chip *chip = pnp_get_drvdata(dev);
+	// TPM 2.0 requires that the TPM2_Shutdown() command be issued prior to loss of power.
+	// If it is not, the DA counter will be incremented and, eventually, the user will be
+	// locked out of their TPM.
+	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+		tpm_tis_pnp_remove(dev);
+	}
+}
+
 static struct pnp_driver tis_pnp_driver = {
 	.name = "tpm_tis",
 	.id_table = tpm_pnp_tbl,
 	.probe = tpm_tis_pnp_init,
 	.remove = tpm_tis_pnp_remove,
+	.shutdown = tpm_tis_pnp_shutdown,
 	.driver	= {
 		.pm = &tpm_tis_pm,
 	},
Signature · 6 lines
2.13.0.rc0.306.g87b477812d-goog


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
#2 lore raw
In reply to a message not in the archive
Quoted 6 lines · Josh Zimmerman

If a TPM2 loses power without a TPM2_Shutdown command being issued, it may lose some state that has yet to be persisted to NVRam, and will increment the DA counter (meaning that after too many disorderly reboots, the TPM will lock the user out).

This is a variant of https://patchwork.kernel.org/patch/9516631/.

This has all the same problems of that patch, just now adds the code to tis driver as well..

There is not much reason to accept this and reject the above patch.

Quoted 3 lines

It differs in that:
* It only changes behavior on TPM2 devices, to avoid invoking the unbounded-waiting sysfs codepath that was discussed on that patch.

Hum. If that is a sensible work around for now then lets just do that at the core code level please.

If you do this, then tpm_sysfs_add_device really needs a comment warning that tpm2 can not have sysfs until the problems are fixed with shutdown.

Quoted 2 lines

* It modifies tpm_tis rather than tpm_i2c_infineon, so that it can change behavior for all TPM2 devices.

There are many drivers that support TPM2 beyond tpm_tis, this just fixes a few tis drivers, re-enforcing my point in the original patch that this stuff has no buisness being in the low level drivers and should be in core code.

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#5 lore raw
In reply to a message not in the archive
Quoted 26 lines · Josh Zimmerman

If a TPM2 loses power without a TPM2_Shutdown command being issued, it may lose some state that has yet to be persisted to NVRam, and will increment the DA counter (meaning that after too many disorderly reboots, the TPM will lock the user out).

This is a variant of https://patchwork.kernel.org/patch/9516631/.
It differs in that:
* It only changes behavior on TPM2 devices, to avoid invoking the unbounded-waiting sysfs codepath that was discussed on that patch.
* It modifies tpm_tis rather than tpm_i2c_infineon, so that it can change behavior for all TPM2 devices.

---
 drivers/char/tpm/tpm_tis.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/char/tpm/tpm_tis.c +1 -0
index c7e1384f1b08..bd9c70b305ab 100644
@@ -216,11 +216,23 @@ static void tpm_tis_pnp_remove(struct pnp_dev *dev)
 	tpm_tis_remove(chip);
 }
 
+static void tpm_tis_pnp_shutdown(struct pnp_dev *dev)

drivers/char/tpm/tpm_tis.c static void tpm_tis_pnp_remove(struct pnp_dev *dev) · line 229

Quoted 10 lines
drivers/char/tpm/tpm_tis.c +10 -0
+{
+	struct tpm_chip *chip = pnp_get_drvdata(dev);
+	// TPM 2.0 requires that the TPM2_Shutdown() command be issued prior to loss of power.
+	// If it is not, the DA counter will be incremented and, eventually, the user will be
+	// locked out of their TPM.
+	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+		tpm_tis_pnp_remove(dev);
+	}
+}
+

What about acpi driver?
Doy you think this should be in stable?

I wonder if we could move in tpm_tis to nowadays common infrastructure where you just have platform_driver and use acpi_match_table.

I'm referring to similar infra as I have in my other project:

http://git.infradead.org/users/jjs/linux-isgx.git/blob/HEAD:/drivers/platform/x86/intel_sgx/sgx_main.c

I guess we have a problem in tis such that some of the devices in some environments are not available through ACPI. There's also of_match_table but I'm wondering if ACPI and OF would cover everything we are using pnp_driver for.

I'm just thinking that since you are fixing the hooks maybe it would make sense to make things right at the same time.

Currently the whole things is a mess.

I talked about this with Rafael a while ago. He said that for some reason it is wrong in the first place to root to ACPI device but I cannot recall the exact reason.

/Jarkko

Quoted 12 lines
drivers/char/tpm/tpm_tis.c +1 -0
 static struct pnp_driver tis_pnp_driver = {
 	.name = "tpm_tis",
 	.id_table = tpm_pnp_tbl,
 	.probe = tpm_tis_pnp_init,
 	.remove = tpm_tis_pnp_remove,
+	.shutdown = tpm_tis_pnp_shutdown,
 	.driver	= {
 		.pm = &tpm_tis_pm,
 	},
Signature · 1 line
2.13.0.rc0.306.g87b477812d-goog

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#4 lore raw
In reply to a message not in the archive
Quoted 27 lines · Jason Gunthorpe
Quoted 6 lines · Josh Zimmerman

If a TPM2 loses power without a TPM2_Shutdown command being issued, it may lose some state that has yet to be persisted to NVRam, and will increment the DA counter (meaning that after too many disorderly reboots, the TPM will lock the user out).

This is a variant of https://patchwork.kernel.org/patch/9516631/.

This has all the same problems of that patch, just now adds the code to tis driver as well..

There is not much reason to accept this and reject the above patch.

Quoted 3 lines

It differs in that:
* It only changes behavior on TPM2 devices, to avoid invoking the unbounded-waiting sysfs codepath that was discussed on that patch.

Hum. If that is a sensible work around for now then lets just do that at the core code level please.

If you do this, then tpm_sysfs_add_device really needs a comment warning that tpm2 can not have sysfs until the problems are fixed with shutdown.

Quoted 2 lines

* It modifies tpm_tis rather than tpm_i2c_infineon, so that it can change behavior for all TPM2 devices.

Quoted 6 lines

There are many drivers that support TPM2 beyond tpm_tis, this just fixes a few tis drivers, re-enforcing my point in the original patch that this stuff has no buisness being in the low level drivers and should be in core code.

Jason

Let me note that this does not even cover all of the tpm_tis.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#7 lore raw
In reply to a message not in the archive
Quoted 4 lines · Jarkko Sakkinen

I guess we have a problem in tis such that some of the devices in some environments are not available through ACPI. There's also of_match_table but I'm wondering if ACPI and OF would cover everything we are using pnp_driver for.

How do you recover the acpi handle like pnp_acpi_device does from a platform_device?

If you know how to do that then we should use acpi_match_table and merge the platform and acpi devices..

Does the acpi_match_table encompass all of the pnp subsystem as well, or do we still need to have pnp_driver to handle legacy pre-acpi PNP systems?

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#9 lore raw
In reply to a message not in the archive

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch
* Have a common tpm_shutdown method in tpm-chip.c that device specific drivers call (or that is just registered automatically for all devices?) that nulls out chip->ops (to prevent any commands being issued after tpm2_shutdown) and sends tpm2_shutdown

This would leave further device refactoring for a future patch.

Before I spend too much time on this, does this seem like a reasonable approach? Anything I'm missing?

Thanks,
Josh

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#11 lore raw
In reply to a message not in the archive
Quoted 6 lines · Josh Zimmerman

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch

As I said, if you rely on the fact that sysfs does not exist for TPM2 then this should already be done for the TPM2 case and an incremental later patch could solve this problem in sysfs to turn shutdown on for tpm1. As long as this logic is clearly documented it is probably an OK step for now.

Quoted 4 lines

* Have a common tpm_shutdown method in tpm-chip.c that device specific drivers call (or that is just registered automatically for all devices?) that nulls out chip->ops (to prevent any commands being issued after tpm2_shutdown) and sends tpm2_shutdown

I think the original patch got a bit stuck on exactly how to do this, but yes, somehow the common shutdown method is called. Either from the driver core (preferred, IHMO) or via patching every single TPM driver.

Quoted 2 lines

Before I spend too much time on this, does this seem like a reasonable approach? Anything I'm missing?

Don't think so, but suggest re-reading that huge original thread to be sure.

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#13 lore raw
In reply to a message not in the archive
Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Josh Zimmerman

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

Quoted 8 lines
Quoted 2 lines

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch

As I said, if you rely on the fact that sysfs does not exist for TPM2 then this should already be done for the TPM2 case and an incremental later patch could solve this problem in sysfs to turn shutdown on for tpm1. As long as this logic is clearly documented it is probably an OK step for now.

This I'm not actually sure about: it seems that there are other places than sysfs where chip->ops is referenced without being guarded by tpm_try_get_ops. If we rely on just NULLing out chip->ops this could be dangerous. I'll look at sending a patch for this first.

Quoted 8 lines
Quoted 4 lines

* Have a common tpm_shutdown method in tpm-chip.c that device specific drivers call (or that is just registered automatically for all devices?) that nulls out chip->ops (to prevent any commands being issued after tpm2_shutdown) and sends tpm2_shutdown

I think the original patch got a bit stuck on exactly how to do this, but yes, somehow the common shutdown method is called. Either from the driver core (preferred, IHMO) or via patching every single TPM driver.

I think the original thread was leaning towards (and I agree with) moving it into the core. Just to make sure I'm understanding the code structure correctly, tpm-chip.c is part of that core and a reasonable place for shared functionality, correct?

Quoted 5 lines
Quoted 2 lines

Before I spend too much time on this, does this seem like a reasonable approach? Anything I'm missing?

Don't think so, but suggest re-reading that huge original thread to be sure.

Will do.

Quoted 1 line

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#15 lore raw
In reply to a message not in the archive
Quoted 16 lines · Josh Zimmerman
Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Josh Zimmerman

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

Quoted 8 lines
Quoted 2 lines

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch

As I said, if you rely on the fact that sysfs does not exist for TPM2 then this should already be done for the TPM2 case and an incremental later patch could solve this problem in sysfs to turn shutdown on for tpm1. As long as this logic is clearly documented it is probably an OK step for now.

Quoted 4 lines

This I'm not actually sure about: it seems that there are other places than sysfs where chip->ops is referenced without being guarded by tpm_try_get_ops. If we rely on just NULLing out chip->ops this could be dangerous. I'll look at sending a patch for this first.

If that is the case then we have an existing bug..

The only other user entry point is via the cdev, and those call try_get_ops around all calls down into the tpm code.

Everything coming in via the kapi must call tpm_chip_find_get, which does try_get_ops.

Quoted 8 lines
Quoted 3 lines

I think the original patch got a bit stuck on exactly how to do this, but yes, somehow the common shutdown method is called. Either from the driver core (preferred, IHMO) or via patching every single TPM driver.

I think the original thread was leaning towards (and I agree with) moving it into the core. Just to make sure I'm understanding the code structure correctly, tpm-chip.c is part of that core and a reasonable place for shared functionality, correct?

Yes..

But I went I said driver core, I ment by adding tpm_class->shutdown that is called by drivers/base/... 'or something like that'

The trouble is how to consistently get the machine shutdown callback from the driver core.

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#17 lore raw
In reply to a message not in the archive
Quoted 39 lines · Jason Gunthorpe
Quoted 16 lines · Josh Zimmerman
Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Josh Zimmerman

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

Quoted 8 lines
Quoted 2 lines

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch

As I said, if you rely on the fact that sysfs does not exist for TPM2 then this should already be done for the TPM2 case and an incremental later patch could solve this problem in sysfs to turn shutdown on for tpm1. As long as this logic is clearly documented it is probably an OK step for now.

Quoted 4 lines

This I'm not actually sure about: it seems that there are other places than sysfs where chip->ops is referenced without being guarded by tpm_try_get_ops. If we rely on just NULLing out chip->ops this could be dangerous. I'll look at sending a patch for this first.

If that is the case then we have an existing bug..

The only other user entry point is via the cdev, and those call try_get_ops around all calls down into the tpm code.

Everything coming in via the kapi must call tpm_chip_find_get, which does try_get_ops.

Quoted 8 lines
Quoted 3 lines

I think the original patch got a bit stuck on exactly how to do this, but yes, somehow the common shutdown method is called. Either from the driver core (preferred, IHMO) or via patching every single TPM driver.

I think the original thread was leaning towards (and I agree with) moving it into the core. Just to make sure I'm understanding the code structure correctly, tpm-chip.c is part of that core and a reasonable place for shared functionality, correct?

Quoted 7 lines

Yes..

But I went I said driver core, I ment by adding tpm_class->shutdown that is called by drivers/base/... 'or something like that'

The trouble is how to consistently get the machine shutdown callback from the driver core.

It is not particularly clear what you mean here. Care to elaborate?

Thanks,
Rafael

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#31 lore raw
In reply to a message not in the archive
Quoted 22 lines · Jason Gunthorpe
Quoted 16 lines · Josh Zimmerman
Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Josh Zimmerman

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

Quoted 8 lines
Quoted 2 lines

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch

As I said, if you rely on the fact that sysfs does not exist for TPM2 then this should already be done for the TPM2 case and an incremental later patch could solve this problem in sysfs to turn shutdown on for tpm1. As long as this logic is clearly documented it is probably an OK step for now.

Quoted 4 lines

This I'm not actually sure about: it seems that there are other places than sysfs where chip->ops is referenced without being guarded by tpm_try_get_ops. If we rely on just NULLing out chip->ops this could be dangerous. I'll look at sending a patch for this first.

Quoted 7 lines

If that is the case then we have an existing bug..

The only other user entry point is via the cdev, and those call try_get_ops around all calls down into the tpm code.

Everything coming in via the kapi must call tpm_chip_find_get, which does try_get_ops.

OK, then I'm confused about the problem with sysfs. If everything is already grabbing a ref first, why can't we just grab a ref and NULL out chip->ops? My understanding from reading the other thread was that sysfs (and, possibly, some other places--see the paragraph starting "I went through the places that access chip->ops to see what's" in the original thread) doesn't grab the ref, which was part of the reason this was tricky.

Josh

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#19 lore raw
In reply to a message not in the archive
Quoted 4 lines · Rafael J. Wysocki
Quoted 2 lines

The trouble is how to consistently get the machine shutdown callback from the driver core.

It is not particularly clear what you mean here. Care to elaborate?

The request is to have a way for the generic tpm code to be called before struct device_driver -> shutdown()

Presumably by adding shutdown() to struct class like the suspend/resume PM methods?

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#33 lore raw
In reply to a message not in the archive
Quoted 29 lines · Josh Zimmerman
Quoted 27 lines · Jason Gunthorpe
Quoted 16 lines · Josh Zimmerman
Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Josh Zimmerman

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

Quoted 8 lines
Quoted 2 lines

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch

As I said, if you rely on the fact that sysfs does not exist for TPM2 then this should already be done for the TPM2 case and an incremental later patch could solve this problem in sysfs to turn shutdown on for tpm1. As long as this logic is clearly documented it is probably an OK step for now.

Quoted 4 lines

This I'm not actually sure about: it seems that there are other places than sysfs where chip->ops is referenced without being guarded by tpm_try_get_ops. If we rely on just NULLing out chip->ops this could be dangerous. I'll look at sending a patch for this first.

If that is the case then we have an existing bug..

The only other user entry point is via the cdev, and those call try_get_ops around all calls down into the tpm code.

Quoted 6 lines
Quoted 2 lines

Everything coming in via the kapi must call tpm_chip_find_get, which does try_get_ops.

OK, then I'm confused about the problem with sysfs. If everything is already grabbing a ref first, why can't we just grab a ref and NULL out chip->ops?

sysfs relies on an implicit ref that is held across the duration of the sysfs file's lifetime. That ref is not given up until device_del returns, which serializes everything. See the comment in tpm_sysfs_add_device

This is fine so long as ops is only ever null'd after device_del, but this shutdown work proposes to change that invariant..

To compensate, the ops lock would have to be pushed down into each sysfs method (for tpm1), or just not use sysfs (like tpm2 does)

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#21 lore raw
In reply to a message not in the archive
Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines · Rafael J. Wysocki
Quoted 2 lines

The trouble is how to consistently get the machine shutdown callback from the driver core.

It is not particularly clear what you mean here. Care to elaborate?

The request is to have a way for the generic tpm code to be called before struct device_driver -> shutdown()

To clarify just a bit: I believe TPM2_Shutdown must be the *last* command sent to the TPM, so it's not as simple as "must be called before device-specific shutdown": if the device has a command it needs to send, it must do so before the core sends tpm2_shutdown (or at least must send another tpm2_shutdown when it's done).

Josh

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#38 lore raw
In reply to a message not in the archive
Quoted 10 lines · Jason Gunthorpe
Quoted 4 lines · Jarkko Sakkinen

I guess we have a problem in tis such that some of the devices in some environments are not available through ACPI. There's also of_match_table but I'm wondering if ACPI and OF would cover everything we are using pnp_driver for.

How do you recover the acpi handle like pnp_acpi_device does from a platform_device?

Quoted 8 lines

If you know how to do that then we should use acpi_match_table and merge the platform and acpi devices..

Does the acpi_match_table encompass all of the pnp subsystem as well, or do we still need to have pnp_driver to handle legacy pre-acpi PNP systems?

Jason

AFAIK with ACPI_COMPANION as in

http://lxr.free-electrons.com/source/net/rfkill/rfkill-gpio.c

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#37 lore raw
In reply to a message not in the archive
Quoted 11 lines · Josh Zimmerman

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch
* Have a common tpm_shutdown method in tpm-chip.c that device specific drivers call (or that is just registered automatically for all devices?) that nulls out chip->ops (to prevent any commands being issued after tpm2_shutdown) and sends tpm2_shutdown

Quoted 7 lines

This would leave further device refactoring for a future patch.

Before I spend too much time on this, does this seem like a reasonable approach? Anything I'm missing?

Thanks,
Josh

I think this sounds like a plan.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#36 lore raw
In reply to a message not in the archive
Quoted 22 lines · Jason Gunthorpe
Quoted 16 lines · Josh Zimmerman
Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Josh Zimmerman

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

Quoted 8 lines
Quoted 2 lines

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch

As I said, if you rely on the fact that sysfs does not exist for TPM2 then this should already be done for the TPM2 case and an incremental later patch could solve this problem in sysfs to turn shutdown on for tpm1. As long as this logic is clearly documented it is probably an OK step for now.

Quoted 4 lines

This I'm not actually sure about: it seems that there are other places than sysfs where chip->ops is referenced without being guarded by tpm_try_get_ops. If we rely on just NULLing out chip->ops this could be dangerous. I'll look at sending a patch for this first.

Quoted 7 lines

If that is the case then we have an existing bug..

The only other user entry point is via the cdev, and those call try_get_ops around all calls down into the tpm code.

Everything coming in via the kapi must call tpm_chip_find_get, which does try_get_ops.

See the comment in tpm_sysfs_add_device.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#35 lore raw
In reply to a message not in the archive
Quoted 43 lines · Jason Gunthorpe
Quoted 29 lines · Josh Zimmerman
Quoted 27 lines · Jason Gunthorpe
Quoted 16 lines · Josh Zimmerman
Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Josh Zimmerman

It sounds like there are several intertwined issues here. I'd like to keep this patch relatively minimal, so here are my current thoughts on a path forward:

Quoted 8 lines
Quoted 2 lines

* Refactor references to chip->ops to go through tpm_try_get_ops, as mentioned in the previous patch

As I said, if you rely on the fact that sysfs does not exist for TPM2 then this should already be done for the TPM2 case and an incremental later patch could solve this problem in sysfs to turn shutdown on for tpm1. As long as this logic is clearly documented it is probably an OK step for now.

Quoted 4 lines

This I'm not actually sure about: it seems that there are other places than sysfs where chip->ops is referenced without being guarded by tpm_try_get_ops. If we rely on just NULLing out chip->ops this could be dangerous. I'll look at sending a patch for this first.

If that is the case then we have an existing bug..

The only other user entry point is via the cdev, and those call try_get_ops around all calls down into the tpm code.

Quoted 6 lines
Quoted 2 lines

Everything coming in via the kapi must call tpm_chip_find_get, which does try_get_ops.

OK, then I'm confused about the problem with sysfs. If everything is already grabbing a ref first, why can't we just grab a ref and NULL out chip->ops?

sysfs relies on an implicit ref that is held across the duration of the sysfs file's lifetime. That ref is not given up until device_del returns, which serializes everything. See the comment in tpm_sysfs_add_device

Quoted 7 lines

This is fine so long as ops is only ever null'd after device_del, but this shutdown work proposes to change that invariant..

To compensate, the ops lock would have to be pushed down into each sysfs method (for tpm1), or just not use sysfs (like tpm2 does)

Jason

Ah. Ignore my last response.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#40 lore raw
In reply to a message not in the archive
Quoted 18 lines · Jarkko Sakkinen
Quoted 17 lines · Jason Gunthorpe
Quoted 4 lines · Jarkko Sakkinen

I guess we have a problem in tis such that some of the devices in some environments are not available through ACPI. There's also of_match_table but I'm wondering if ACPI and OF would cover everything we are using pnp_driver for.

How do you recover the acpi handle like pnp_acpi_device does from a platform_device?

If you know how to do that then we should use acpi_match_table and merge the platform and acpi devices..

Does the acpi_match_table encompass all of the pnp subsystem as well, or do we still need to have pnp_driver to handle legacy pre-acpi PNP systems?

Quoted 7 lines
Quoted 1 line

Jason

AFAIK with ACPI_COMPANION as in

http://lxr.free-electrons.com/source/net/rfkill/rfkill-gpio.c

/Jarkko

What about ACPI_HANDLE like is done in thie bit of sdhci_acpi_probe()?

  static int sdhci_acpi_probe(struct platform_device *pdev)
  {
          struct device *dev = &pdev->dev;
          acpi_handle handle = ACPI_HANDLE(dev);
Quoted 8 lines

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
tpmdd-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/tpmdd-devel

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#41 lore raw
Quoted 25 lines · Jerry Snitselaar
Quoted 24 lines · Jarkko Sakkinen
Quoted 10 lines · Jason Gunthorpe
Quoted 4 lines · Jarkko Sakkinen

I guess we have a problem in tis such that some of the devices in some environments are not available through ACPI. There's also of_match_table but I'm wondering if ACPI and OF would cover everything we are using pnp_driver for.

How do you recover the acpi handle like pnp_acpi_device does from a platform_device?

Quoted 8 lines

If you know how to do that then we should use acpi_match_table and merge the platform and acpi devices..

Does the acpi_match_table encompass all of the pnp subsystem as well, or do we still need to have pnp_driver to handle legacy pre-acpi PNP systems?

Jason

AFAIK with ACPI_COMPANION as in

http://lxr.free-electrons.com/source/net/rfkill/rfkill-gpio.c

Quoted 8 lines
Quoted 1 line

/Jarkko

What about ACPI_HANDLE like is done in thie bit of sdhci_acpi_probe()?

static int sdhci_acpi_probe(struct platform_device *pdev)

 {
         struct device *dev = &pdev->dev;
         acpi_handle handle = ACPI_HANDLE(dev);

Yes. That can be used for getting the handle. ACPI_HANDLE just wraps ACPI_COMPANION in a way that it extracts the handle from struct acpi_device.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#22 lore raw
In reply to a message not in the archive
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines · Rafael J. Wysocki
Quoted 2 lines

The trouble is how to consistently get the machine shutdown callback from the driver core.

Quoted 7 lines
Quoted 1 line

It is not particularly clear what you mean here. Care to elaborate?

The request is to have a way for the generic tpm code to be called before struct device_driver -> shutdown()

Presumably by adding shutdown() to struct class like the suspend/resume PM methods?

I've seen some reference to the existing suspend method as being legacy. e.g., drivers/base/power/main.c:__device_suspend:

        if (dev->class) {
                if (dev->class->pm) {
                        info = "class ";
                        callback = pm_op(dev->class->pm, state);
                        goto Run;
                } else if (dev->class->suspend) {
                        pm_dev_dbg(dev, state, "legacy class ");
                        error = legacy_suspend(dev, state, dev->class->suspend,
                                                "legacy class ");
                        goto End;
                }
        }

In addition, it's not clear to me that the tpm codebase is using class->suspend right now; it looks like for the most part each device uses STATIC_DEV_PM_OPS and sets up the common suspend function to be called independently. (this is based on grepping the tpm driver for "suspend" so it's possible I'm missing some clever macro hacks, but I did dig in somewhat thoroughly and don't believe I'm missing anything like that)

Is there some reason a similar approach of having each driver type register its own shutdown function pointer (that might just all be the same function, much like tpm_pm_suspend) would not be acceptable?

Josh

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#24 lore raw
In reply to a message not in the archive
Quoted 2 lines · Josh Zimmerman

I've seen some reference to the existing suspend method as being legacy. e.g., drivers/base/power/main.c:__device_suspend:

I think this is because class->pm is the modern way to do class->suspend, not because class methods are legacy.

Quoted 3 lines

In addition, it's not clear to me that the tpm codebase is using class->suspend right now; it looks like for the most part each device

Yes, but that doesn't mean this is the right way to do things, it just means this old code hasn't really been updated. The tpm subsystem was a total mess just a few years ago, not everything is cleaned up yet..

eg it would probably make much more sense to hook class->pm to do tpm_pm_suspend rather than have each driver include the same STATIC_DEV_PM_OPS.

Quoted 3 lines

Is there some reason a similar approach of having each driver type register its own shutdown function pointer (that might just all be the same function, much like tpm_pm_suspend) would not be acceptable?

Now that we have so many tpm drivers it is a very good idea not to introduce boilerplate into every driver, we should be going the other way :)

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#26 lore raw
In reply to a message not in the archive

Hm, according to include/linux/pm.h,

* It is allowed to unregister devices while the above callbacks are being
* executed. However, a callback routine MUST NOT try to
unregister the device
* it was called for, although it may unregister children of that device (for
* example, if it detects that a child was unplugged while the system was
* asleep).

So, it seems if we want to add shutdown to class->pm, we'll need to do the refactoring for sysfs now to avoid the implicit lock in order to safely NULL out chip->ops. (Otherwise, I believe we'd need to unregister.)\\

I'll start work on that, but I wanted to send this email first to double-check that my understanding was correct and I wasn't missing an easier path.

Thanks,
Josh

Quoted 24 lines · Jason Gunthorpe
Quoted 2 lines · Josh Zimmerman

I've seen some reference to the existing suspend method as being legacy. e.g., drivers/base/power/main.c:__device_suspend:

I think this is because class->pm is the modern way to do class->suspend, not because class methods are legacy.

Quoted 3 lines

In addition, it's not clear to me that the tpm codebase is using class->suspend right now; it looks like for the most part each device

Yes, but that doesn't mean this is the right way to do things, it just means this old code hasn't really been updated. The tpm subsystem was a total mess just a few years ago, not everything is cleaned up yet..

eg it would probably make much more sense to hook class->pm to do tpm_pm_suspend rather than have each driver include the same STATIC_DEV_PM_OPS.

Quoted 3 lines

Is there some reason a similar approach of having each driver type register its own shutdown function pointer (that might just all be the same function, much like tpm_pm_suspend) would not be acceptable?

Quoted 5 lines

Now that we have so many tpm drivers it is a very good idea not to introduce boilerplate into every driver, we should be going the other way :)

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#28 lore raw
In reply to a message not in the archive
Quoted 9 lines · Josh Zimmerman

Hm, according to include/linux/pm.h,

* It is allowed to unregister devices while the above callbacks are being
* executed. However, a callback routine MUST NOT try to
unregister the device
* it was called for, although it may unregister children of that device (for
* example, if it detects that a child was unplugged while the system was
* asleep).

Quoted 8 lines

So, it seems if we want to add shutdown to class->pm, we'll need to do the refactoring for sysfs now to avoid the implicit lock in order to safely NULL out chip->ops. (Otherwise, I believe we'd need to unregister.)\\

I'll start work on that, but I wanted to send this email first to double-check that my understanding was correct and I wasn't missing an easier path.

Like I said, if you guard shutdown with a 'if TPM2' then the sysfs case cannot occur..

It eventually needs to be fixed for TPM1, but a TPM2 only first step would be OK too.

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#30 lore raw
In reply to a message not in the archive

Ah, yes, right. Too early on Monday still :)
Josh

Quoted 19 lines · Jason Gunthorpe
Quoted 9 lines · Josh Zimmerman

Hm, according to include/linux/pm.h,

* It is allowed to unregister devices while the above callbacks are being
* executed. However, a callback routine MUST NOT try to
unregister the device
* it was called for, although it may unregister children of that device (for
* example, if it detects that a child was unplugged while the system was
* asleep).

Quoted 8 lines

So, it seems if we want to add shutdown to class->pm, we'll need to do the refactoring for sysfs now to avoid the implicit lock in order to safely NULL out chip->ops. (Otherwise, I believe we'd need to unregister.)\\

I'll start work on that, but I wanted to send this email first to double-check that my understanding was correct and I wasn't missing an easier path.

Quoted 7 lines

Like I said, if you guard shutdown with a 'if TPM2' then the sysfs case cannot occur..

It eventually needs to be fixed for TPM1, but a TPM2 only first step would be OK too.

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot