Skip to contentSkip to conversation

tpm: unified PPI interface for TPM 1.x/2.0 devices

PATCH In review Jarkko Sakkinen 2 participants 6 messages Apr 1, 2015 – Apr 9, 2015 TPM device driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: unified PPI interface for TPM 1.x/2.0 devices

Added PPI interface to the character device. PPI interface is also kept in the pdev for backwards compatibility.

---
 drivers/char/tpm/tpm-chip.c      |  16 +++---
 drivers/char/tpm/tpm-interface.c |   1 +
 drivers/char/tpm/tpm.h           |   3 +-
 drivers/char/tpm/tpm_ppi.c       | 105 ++++++++++++++++++++++++++++++++-------
 4 files changed, 97 insertions(+), 28 deletions(-)
drivers/char/tpm/tpm-chip.c +7 -9
index 283f00a..44f39be 100644
@@ -181,12 +181,6 @@ static int tpm1_chip_register(struct tpm_chip *chip)
 	if (rc)
 		return rc;
 
-	rc = tpm_add_ppi(chip);
-	if (rc) {
-		tpm_sysfs_del_device(chip);
-		return rc;
-	}
-
 	chip->bios_dir = tpm_bios_log_setup(chip->devname);
 
 	return 0;
@@ -200,8 +194,6 @@ static void tpm1_chip_unregister(struct tpm_chip *chip)
 	if (chip->bios_dir)
 		tpm_bios_log_teardown(chip->bios_dir);
 
-	tpm_remove_ppi(chip);
-
 	tpm_sysfs_del_device(chip);
 }
 
@@ -224,6 +216,10 @@ int tpm_chip_register(struct tpm_chip *chip)
 	if (rc)
 		return rc;
 
+	rc = tpm_add_ppi(chip);
+	if (rc)
+		goto out_err;
+
 	rc = tpm_dev_add_device(chip);
 	if (rc)
 		goto out_err;
@@ -237,6 +233,7 @@ int tpm_chip_register(struct tpm_chip *chip)
 
 	return 0;
 out_err:
+	tpm_remove_ppi(chip);
 	tpm1_chip_unregister(chip);
 	return rc;
 }
@@ -262,7 +259,8 @@ void tpm_chip_unregister(struct tpm_chip *chip)
 	spin_unlock(&driver_lock);
 	synchronize_rcu();
 
-	tpm1_chip_unregister(chip);
 	tpm_dev_del_device(chip);
+	tpm_remove_ppi(chip);
+	tpm1_chip_unregister(chip);
 }
 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
drivers/char/tpm/tpm-interface.c +1 -0
index e85d341..aa813ff 100644
@@ -1038,6 +1038,7 @@ static int __init tpm_init(void)
 		return rc;
 	}
 
+	tpm_ppi_init(tpm_class);
 	return 0;
 }
 
drivers/char/tpm/tpm.h +2 -1
index f8319a0..65872a5 100644
@@ -182,7 +182,7 @@ struct tpm_chip {
 	struct list_head list;
 };
 
-#define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
+#define to_tpm_chip(n) container_of(dev, struct tpm_chip, dev)
 
 static inline void tpm_chip_put(struct tpm_chip *chip)
 {
@@ -411,6 +411,7 @@ void tpm_sysfs_del_device(struct tpm_chip *chip);
 
 int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
 
+void tpm_ppi_init(struct class *tpm_class);
 #ifdef CONFIG_ACPI
 extern int tpm_add_ppi(struct tpm_chip *chip);
 extern void tpm_remove_ppi(struct tpm_chip *chip);
drivers/char/tpm/tpm_ppi.c +87 -18

202 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw
Quoted 2 lines · Jarkko Sakkinen

Added PPI interface to the character device. PPI interface is also kept in the pdev for backwards compatibility.

Could you look at just completely moving the PPI interface to the char dev and then adding a symlink from the pdev? That would be really ideal.

symlinks have the advantage that they actually fully fix the lifetime issues.

This seems doable, if we replace the ppi_attrs group with a bunch of calls to sysfs_create_link it should work ?

drivers/char/tpm/tpm_ppi.c tpm_eval_dsm(acpi_handle ppi_handle, int func, acpi_object_type type, · line 61

Quoted 9 lines
drivers/char/tpm/tpm_ppi.c +9 -0
+static struct tpm_chip *ppi_dev_to_chip(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+
+	if (chip == NULL)
+		chip = to_tpm_chip(chip);
+
+	return chip;
+}

If symlinks don't work out, we should probably just set the drvdata on the tpm_chip itself to avoid this.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!(chip->flags & TPM_CHIP_FLAG_PPI))
+		return -EINVAL;

Hum, I don't think the PPI files should be created if there is no PPI support..

drivers/char/tpm/tpm_ppi.c static ssize_t tpm_show_ppi_tcg_operations(struct device *dev, · line 372

Quoted 4 lines
drivers/char/tpm/tpm_ppi.c +3 -0
+void __init tpm_ppi_init(struct class *tpm_class)
+{
+	tpm_class->dev_groups = tpm_groups;
 }

So this shouldn't be unconditional.

Also, ultimately PPI can't just claim the dev_groups, other parts of the driver will need to add groups too.

I think it makes more sense to do

struct attribute_group *tpm_ppi_get_sysfs(struct tpm_chip *chip) {
}

And take care of building the list in the caller.

And tpm_ppi_get_sysfs should be called after the driver is readied but before adding the device.

Jason

#2 lore raw
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · Jarkko Sakkinen

Added PPI interface to the character device. PPI interface is also kept in the pdev for backwards compatibility.

Could you look at just completely moving the PPI interface to the char dev and then adding a symlink from the pdev? That would be really ideal.

Quoted 5 lines

symlinks have the advantage that they actually fully fix the lifetime issues.

This seems doable, if we replace the ppi_attrs group with a bunch of calls to sysfs_create_link it should work ?

If we follow the pattern in [1] by the book, how would you use sysfs_create_link()? To be more specific, how would you get the driver core to create the symlinks for you?

If we decide not to follow [1] by the book, then this might be doable (thinking off my head, that's the reason why I use *might be* instead of *is*). Wouldn't we get non-racy behavior if sysfs_create_link()'s are executed after device_initialize() and before device_add()?

Quoted 4 lines
Quoted 4 lines
drivers/char/tpm/tpm_ppi.c +4 -0
+static struct tpm_chip *ppi_dev_to_chip(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+
Quoted 8 lines

drivers/char/tpm/tpm_ppi.c tpm_eval_dsm(acpi_handle ppi_handle, int func, acpi_object_type type, · line 61

Quoted 5 lines
drivers/char/tpm/tpm_ppi.c +5 -0
+	if (chip == NULL)
+		chip = to_tpm_chip(chip);
+
+	return chip;
+}

If symlinks don't work out, we should probably just set the drvdata on the tpm_chip itself to avoid this.

I'll experiment with this. Thanks for the comment.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!(chip->flags & TPM_CHIP_FLAG_PPI))
+		return -EINVAL;

Hum, I don't think the PPI files should be created if there is no PPI support..

Again, following [1] by the book. And again, I think we could just as well do our sysfs stuff in-between device_initialize() and device_add() and get the non-racy behavior.

Quoted 12 lines

drivers/char/tpm/tpm_ppi.c static ssize_t tpm_show_ppi_tcg_operations(struct device *dev, · line 372

Quoted 4 lines
drivers/char/tpm/tpm_ppi.c +3 -0
+void __init tpm_ppi_init(struct class *tpm_class)
+{
+	tpm_class->dev_groups = tpm_groups;
 }

So this shouldn't be unconditional.

Also, ultimately PPI can't just claim the dev_groups, other parts of the driver will need to add groups too.

I think it makes more sense to do

Quoted 8 lines

struct attribute_group *tpm_ppi_get_sysfs(struct tpm_chip *chip) {
}

And take care of building the list in the caller.

And tpm_ppi_get_sysfs should be called after the driver is readied but before adding the device.

I don't think this would matter. Things could be refactored when more sysfs attributes are needed.

Quoted 1 line

Jason

/Jarkko

[1] http://kroah.com/log/blog/2013/06/26/how-to-create-a-sysfs-file-correctly/

#3 lore raw

Gave this some rethought :)

Quoted 15 lines · Jarkko Sakkinen
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · Jarkko Sakkinen

Added PPI interface to the character device. PPI interface is also kept in the pdev for backwards compatibility.

Could you look at just completely moving the PPI interface to the char dev and then adding a symlink from the pdev? That would be really ideal.

Quoted 5 lines

symlinks have the advantage that they actually fully fix the lifetime issues.

This seems doable, if we replace the ppi_attrs group with a bunch of calls to sysfs_create_link it should work ?

Quoted 8 lines

If we follow the pattern in [1] by the book, how would you use sysfs_create_link()? To be more specific, how would you get the driver core to create the symlinks for you?

If we decide not to follow [1] by the book, then this might be doable (thinking off my head, that's the reason why I use *might be* instead of *is*). Wouldn't we get non-racy behavior if sysfs_create_link()'s are executed after device_initialize() and before device_add()?

Here I tend to lean towards for creating a separate set of attributes instead. I would keep the legacy stuff completely separated of the sysfs attributes for the character devices and not do any clever things in the sysfs.

Quoted 21 lines
Quoted 4 lines
Quoted 4 lines
drivers/char/tpm/tpm_ppi.c +4 -0
+static struct tpm_chip *ppi_dev_to_chip(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+
Quoted 8 lines

drivers/char/tpm/tpm_ppi.c tpm_eval_dsm(acpi_handle ppi_handle, int func, acpi_object_type type, · line 61

Quoted 5 lines
drivers/char/tpm/tpm_ppi.c +5 -0
+	if (chip == NULL)
+		chip = to_tpm_chip(chip);
+
+	return chip;
+}

If symlinks don't work out, we should probably just set the drvdata on the tpm_chip itself to avoid this.

I'll experiment with this. Thanks for the comment.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!(chip->flags & TPM_CHIP_FLAG_PPI))
+		return -EINVAL;

Hum, I don't think the PPI files should be created if there is no PPI support..

Quoted 3 lines

Again, following [1] by the book. And again, I think we could just as well do our sysfs stuff in-between device_initialize() and device_add() and get the non-racy behavior.

I do not think it would be a bad idea to always create them when the kernel is compiled with CONFIG_ACPI. Maybe it would be abetter idea to return -ENOSYS? Device Model in the Linux kernel seems to recommend through the defaults APIs a flat set of attributes for each device node.

Quoted 16 lines
Quoted 16 lines

drivers/char/tpm/tpm_ppi.c static ssize_t tpm_show_ppi_tcg_operations(struct device *dev, · line 372

Quoted 4 lines
drivers/char/tpm/tpm_ppi.c +3 -0
+void __init tpm_ppi_init(struct class *tpm_class)
+{
+	tpm_class->dev_groups = tpm_groups;
 }

So this shouldn't be unconditional.

Also, ultimately PPI can't just claim the dev_groups, other parts of the driver will need to add groups too.

I think it makes more sense to do

struct attribute_group *tpm_ppi_get_sysfs(struct tpm_chip *chip) {
}

Quoted 9 lines
Quoted 4 lines

And take care of building the list in the caller.

And tpm_ppi_get_sysfs should be called after the driver is readied but before adding the device.

I don't think this would matter. Things could be refactored when more sysfs attributes are needed.

Quoted 1 line

Jason

/Jarkko

#4 lore raw
In reply to Jarkko Sakkinen
Quoted 12 lines · Jarkko Sakkinen
Quoted 11 lines · Jason Gunthorpe
Quoted 2 lines · Jarkko Sakkinen

Added PPI interface to the character device. PPI interface is also kept in the pdev for backwards compatibility.

Could you look at just completely moving the PPI interface to the char dev and then adding a symlink from the pdev? That would be really ideal.

symlinks have the advantage that they actually fully fix the lifetime issues.

Quoted 6 lines
Quoted 2 lines

This seems doable, if we replace the ppi_attrs group with a bunch of calls to sysfs_create_link it should work ?

If we follow the pattern in [1] by the book, how would you use sysfs_create_link()? To be more specific, how would you get the driver core to create the symlinks for you?

The driver core does not create symlinks, it creates the real files, which is the tpm_class->dev_groups part of your patch. That is fine..

The symlinks replace the broken legacy files under the platform_device. These are already racy, and different versions of the kernel have had different kind of races here. It wasn't until your 'tpm: fix call order in tpm-chip.c' that the ordering here started to make any kind of sense.

So, I'm inclined to say the legacy paths don't much matter. They have rarely worked race free so nothing out there can be depending on them.

I'd rather see the legacy paths be turned into symlinkes because that means we can close the use-after-free oops possibility the current code has, and that is a more serious bug than the user space race which has always existed anyhow.

Quoted 4 lines

If we decide not to follow [1] by the book, then this might be doable (thinking off my head, that's the reason why I use *might be* instead of *is*). Wouldn't we get non-racy behavior if sysfs_create_link()'s are executed after device_initialize() and before device_add()?

That would at least preserve the latest semantic that the uevent is created after all the sysfs is in place. It is the best we can do.

Since this seems to address the race problem why do you think this is not worthwhile?

Quoted 9 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!(chip->flags & TPM_CHIP_FLAG_PPI))
+		return -EINVAL;

Hum, I don't think the PPI files should be created if there is no PPI support..

Again, following [1] by the book. And again, I think we could just as well do our sysfs stuff in-between device_initialize() and device_add() and get the non-racy behavior.

Not relying on the class default seems reasonable for ppi to me.

Quoted 3 lines

I do not think it would be a bad idea to always create them when the kernel is compiled with CONFIG_ACPI. Maybe it would be abetter idea to return -ENOSYS?

This is not really consistent with other uses of sysfs, user space tooling has a harder time detecting ENOSYS than it does a file that doesn't exist.

It is also a change from the current PPI behavior, so I don't think we should do this without a very good reason.

Quoted 3 lines

Device Model in the Linux kernel seems to recommend through the defaults APIs a flat set of attributes for each device node.

No, that is just the defaults scheme, there are other ways to create attributes that are conditional based on device capabilities.

Greg notes:

Quoted 2 lines

Sometimes you don't have control over the driver either, or want different sysfs files for different devices controlled by your driver

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

drivers/firewire/core-device.c has an example of this, the config_rom_attributes are pruned to only expose the ones that actually exist using the struct device groups scheme.

Jason

#5 lore raw
Quoted 29 lines · Jason Gunthorpe
Quoted 12 lines · Jarkko Sakkinen
Quoted 11 lines · Jason Gunthorpe
Quoted 2 lines · Jarkko Sakkinen

Added PPI interface to the character device. PPI interface is also kept in the pdev for backwards compatibility.

Could you look at just completely moving the PPI interface to the char dev and then adding a symlink from the pdev? That would be really ideal.

symlinks have the advantage that they actually fully fix the lifetime issues.

Quoted 6 lines
Quoted 2 lines

This seems doable, if we replace the ppi_attrs group with a bunch of calls to sysfs_create_link it should work ?

If we follow the pattern in [1] by the book, how would you use sysfs_create_link()? To be more specific, how would you get the driver core to create the symlinks for you?

The driver core does not create symlinks, it creates the real files, which is the tpm_class->dev_groups part of your patch. That is fine..

The symlinks replace the broken legacy files under the platform_device. These are already racy, and different versions of the kernel have had different kind of races here. It wasn't until your 'tpm: fix call order in tpm-chip.c' that the ordering here started to make any kind of sense.

Quoted 8 lines

So, I'm inclined to say the legacy paths don't much matter. They have rarely worked race free so nothing out there can be depending on them.

I'd rather see the legacy paths be turned into symlinkes because that means we can close the use-after-free oops possibility the current code has, and that is a more serious bug than the user space race which has always existed anyhow.

OK, I'll consider doing this for the next iteration.

Quoted 28 lines
Quoted 4 lines

If we decide not to follow [1] by the book, then this might be doable (thinking off my head, that's the reason why I use *might be* instead of *is*). Wouldn't we get non-racy behavior if sysfs_create_link()'s are executed after device_initialize() and before device_add()?

That would at least preserve the latest semantic that the uevent is created after all the sysfs is in place. It is the best we can do.

Since this seems to address the race problem why do you think this is not worthwhile?

Quoted 9 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!(chip->flags & TPM_CHIP_FLAG_PPI))
+		return -EINVAL;

Hum, I don't think the PPI files should be created if there is no PPI support..

Again, following [1] by the book. And again, I think we could just as well do our sysfs stuff in-between device_initialize() and device_add() and get the non-racy behavior.

Not relying on the class default seems reasonable for ppi to me.

Quoted 3 lines

I do not think it would be a bad idea to always create them when the kernel is compiled with CONFIG_ACPI. Maybe it would be abetter idea to return -ENOSYS?

Quoted 6 lines

This is not really consistent with other uses of sysfs, user space tooling has a harder time detecting ENOSYS than it does a file that doesn't exist.

It is also a change from the current PPI behavior, so I don't think we should do this without a very good reason.

Agreed.

Quoted 9 lines
Quoted 3 lines

Device Model in the Linux kernel seems to recommend through the defaults APIs a flat set of attributes for each device node.

No, that is just the defaults scheme, there are other ways to create attributes that are conditional based on device capabilities.

Greg notes:

Quoted 7 lines
Quoted 2 lines

Sometimes you don't have control over the driver either, or want different sysfs files for different devices controlled by your driver

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

drivers/firewire/core-device.c has an example of this, the config_rom_attributes are pruned to only expose the ones that actually exist using the struct device groups scheme.

Thanks for noting this.

Quoted 1 line

Jason

OK based on this discussion I'll iterate the patch.

/Jarkko