diff mbox

[tpmdd-devel,1/2] tpm: add sysfs attributes for tpm2

Message ID 1468547496-16215-2-git-send-email-apronin@chromium.org
State New
Headers show

Commit Message

apronin@chromium.org July 15, 2016, 1:51 a.m. UTC
Break sysfs attributes into common and TPM 1.2/2.0-specific, and
create sysfs groups for TPM2.0.

Signed-off-by: Andrey Pronin <apronin@chromium.org>
---
 drivers/char/tpm/tpm-chip.c  | 48 ++++++++++++++++++++++++++++----------------
 drivers/char/tpm/tpm-sysfs.c | 24 ++++++++++++++++++++--
 2 files changed, 53 insertions(+), 19 deletions(-)

Comments

Jason Gunthorpe July 15, 2016, 3:21 a.m. UTC | #1
On Thu, Jul 14, 2016 at 06:51:35PM -0700, Andrey Pronin wrote:
> -	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
> -
> -	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
> -		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
> +	for (ngrp = 0; ngrp < chip->groups_cnt; ++ngrp) {
> +		if (chip->groups[ngrp]->name) {
> +			sysfs_remove_link(&chip->dev.parent->kobj,
> +					  chip->groups[ngrp]->name);
> +		} else {
> +			for (i = chip->groups[ngrp]->attrs; *i != NULL; ++i)
> +				sysfs_remove_link(&chip->dev.parent->kobj,
> +						  (*i)->name);
> +		}
> +	}

NAK

No new compat symlinks. Only the existing set of links are permitted.

Any new sysfs entries must use only the new location.

> +static struct attribute *tpm2_dev_attrs[] = {
>  	NULL,
>  };

> +static const struct attribute_group tpm2_dev_group = {
> +	.attrs = tpm2_dev_attrs,
> +};

Don't add dead code, add this and related in the patch that requires it.

>  void tpm_sysfs_add_device(struct tpm_chip *chip)
>  {
>  	/* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
> @@ -290,4 +306,8 @@ void tpm_sysfs_add_device(struct tpm_chip *chip)
>  	 */
>  	WARN_ON(chip->groups_cnt != 0);
>  	chip->groups[chip->groups_cnt++] = &tpm_dev_group;
> +	if (chip->flags & TPM_CHIP_FLAG_TPM2)
> +		chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
> +	else
> +		chip->groups[chip->groups_cnt++] = &tpm1_dev_group;

.. and this can't really happen either..

To make things simple you can just have tpm2 not ever create any links
for any files by never using groups[0]. There is no need to try and
create a shared 'tpm_dev_group'.

Jason

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
apronin@chromium.org July 15, 2016, 3:32 a.m. UTC | #2
On Thu, Jul 14, 2016 at 09:21:45PM -0600, Jason Gunthorpe wrote:
> On Thu, Jul 14, 2016 at 06:51:35PM -0700, Andrey Pronin wrote:
> > -	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
> > -
> > -	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
> > -		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
> > +	for (ngrp = 0; ngrp < chip->groups_cnt; ++ngrp) {
> > +		if (chip->groups[ngrp]->name) {
> > +			sysfs_remove_link(&chip->dev.parent->kobj,
> > +					  chip->groups[ngrp]->name);
> > +		} else {
> > +			for (i = chip->groups[ngrp]->attrs; *i != NULL; ++i)
> > +				sysfs_remove_link(&chip->dev.parent->kobj,
> > +						  (*i)->name);
> > +		}
> > +	}
> 
> NAK
> 
> No new compat symlinks. Only the existing set of links are permitted.
> 
> Any new sysfs entries must use only the new location.
> 
> > +static struct attribute *tpm2_dev_attrs[] = {
> >  	NULL,
> >  };
> 
> > +static const struct attribute_group tpm2_dev_group = {
> > +	.attrs = tpm2_dev_attrs,
> > +};
> 
> Don't add dead code, add this and related in the patch that requires it.
> 
> >  void tpm_sysfs_add_device(struct tpm_chip *chip)
> >  {
> >  	/* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
> > @@ -290,4 +306,8 @@ void tpm_sysfs_add_device(struct tpm_chip *chip)
> >  	 */
> >  	WARN_ON(chip->groups_cnt != 0);
> >  	chip->groups[chip->groups_cnt++] = &tpm_dev_group;
> > +	if (chip->flags & TPM_CHIP_FLAG_TPM2)
> > +		chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
> > +	else
> > +		chip->groups[chip->groups_cnt++] = &tpm1_dev_group;
> 
> .. and this can't really happen either..
> 
> To make things simple you can just have tpm2 not ever create any links
> for any files by never using groups[0]. There is no need to try and
> create a shared 'tpm_dev_group'.
> 
> Jason

Hi Jason,

Mostly understood. One question.

tpm2 shares some of the attributes with tpm1 (e.g. timeouts). Do I still
just add those separately for tpm2 to groups[1] and keep groups[0] empty?

Andrey


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
Jason Gunthorpe July 15, 2016, 3:34 a.m. UTC | #3
On Thu, Jul 14, 2016 at 08:32:01PM -0700, Andrey Pronin wrote:

> tpm2 shares some of the attributes with tpm1 (e.g. timeouts). Do I still
> just add those separately for tpm2 to groups[1] and keep groups[0] empty?

I think so. Since the file never exists for tpm2, nothing coded for
tpm2 will ever look in the old location.

Jason

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
apronin@chromium.org July 15, 2016, 4:56 p.m. UTC | #4
On Thu, Jul 14, 2016 at 09:34:39PM -0600, Jason Gunthorpe wrote:
> On Thu, Jul 14, 2016 at 08:32:01PM -0700, Andrey Pronin wrote:
> 
> > tpm2 shares some of the attributes with tpm1 (e.g. timeouts). Do I still
> > just add those separately for tpm2 to groups[1] and keep groups[0] empty?

Just realized that if we keep tpm_add_legacy_sysfs() intact, it doesn't
create symlinks for tpm2 case for any of the groups, including groups[0].
So, in tpm_sysfs_add_device() can just do smth like:

if (chip->flags & TPM_CHIP_FLAG_TPM2)
	chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
else
	chip->groups[chip->groups_cnt++] = &tpm_dev_group;

Is that acceptable? Will submit the next version along those lines.

> 
> I think so. Since the file never exists for tpm2, nothing coded for
> tpm2 will ever look in the old location.
> 

There can be a common code working with tpm1.2 that will just continue
working with tpm2 if the common attributes are in the same place. So, some
part of code can be re-used as is, but I agree that there are not too
many common attributes. Will drop for now. Can be addressed later if
the clear need arises.


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
Jason Gunthorpe July 15, 2016, 5:09 p.m. UTC | #5
On Fri, Jul 15, 2016 at 09:56:58AM -0700, Andrey Pronin wrote:
> On Thu, Jul 14, 2016 at 09:34:39PM -0600, Jason Gunthorpe wrote:
> > On Thu, Jul 14, 2016 at 08:32:01PM -0700, Andrey Pronin wrote:
> > 
> > > tpm2 shares some of the attributes with tpm1 (e.g. timeouts). Do I still
> > > just add those separately for tpm2 to groups[1] and keep groups[0] empty?
> 
> Just realized that if we keep tpm_add_legacy_sysfs() intact, it doesn't
> create symlinks for tpm2 case for any of the groups, including groups[0].
> So, in tpm_sysfs_add_device() can just do smth like:
> 
> if (chip->flags & TPM_CHIP_FLAG_TPM2)
> 	chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
> else
> 	chip->groups[chip->groups_cnt++] = &tpm_dev_group;
> 
> Is that acceptable? Will submit the next version along those lines.

Sure, as long as there are no links for TPM2 that sounds fine.

> > I think so. Since the file never exists for tpm2, nothing coded for
> > tpm2 will ever look in the old location.

> There can be a common code working with tpm1.2 that will just
> continue working with tpm2 if the common attributes are in the same
> place. So, some part of code can be re-used as is, but I agree that
> there are not too many common attributes. Will drop for now. Can be
> addressed later if the clear need arises.

Well, we expect people to test & migrate the common code when testing
new tpm2 deployments..

Jason

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
Jarkko Sakkinen July 18, 2016, 7:16 p.m. UTC | #6
On Thu, Jul 14, 2016 at 06:51:35PM -0700, Andrey Pronin wrote:
> Break sysfs attributes into common and TPM 1.2/2.0-specific, and
> create sysfs groups for TPM2.0.

I agree with Jasons comments and wait for revised version.

/Jarkko

> Signed-off-by: Andrey Pronin <apronin@chromium.org>
> ---
>  drivers/char/tpm/tpm-chip.c  | 48 ++++++++++++++++++++++++++++----------------
>  drivers/char/tpm/tpm-sysfs.c | 24 ++++++++++++++++++++--
>  2 files changed, 53 insertions(+), 19 deletions(-)
> 
> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
> index 5a2f043..6c72671 100644
> --- a/drivers/char/tpm/tpm-chip.c
> +++ b/drivers/char/tpm/tpm-chip.c
> @@ -281,8 +281,6 @@ static int tpm1_chip_register(struct tpm_chip *chip)
>  	if (chip->flags & TPM_CHIP_FLAG_TPM2)
>  		return 0;
>  
> -	tpm_sysfs_add_device(chip);
> -
>  	chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
>  
>  	return 0;
> @@ -300,14 +298,21 @@ static void tpm1_chip_unregister(struct tpm_chip *chip)
>  static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
>  {
>  	struct attribute **i;
> +	int ngrp;
>  
> -	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
> +	if (chip->flags & TPM_CHIP_FLAG_VIRTUAL)
>  		return;
>  
> -	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
> -
> -	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
> -		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
> +	for (ngrp = 0; ngrp < chip->groups_cnt; ++ngrp) {
> +		if (chip->groups[ngrp]->name) {
> +			sysfs_remove_link(&chip->dev.parent->kobj,
> +					  chip->groups[ngrp]->name);
> +		} else {
> +			for (i = chip->groups[ngrp]->attrs; *i != NULL; ++i)
> +				sysfs_remove_link(&chip->dev.parent->kobj,
> +						  (*i)->name);
> +		}
> +	}
>  }
>  
>  /* For compatibility with legacy sysfs paths we provide symlinks from the
> @@ -317,20 +322,27 @@ static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
>  static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
>  {
>  	struct attribute **i;
> -	int rc;
> +	int rc = 0;
> +	int ngrp;
>  
> -	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
> +	if (chip->flags & TPM_CHIP_FLAG_VIRTUAL)
>  		return 0;
>  
> -	rc = __compat_only_sysfs_link_entry_to_kobj(
> -		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
> -	if (rc && rc != -ENOENT)
> -		return rc;
> -
>  	/* All the names from tpm-sysfs */
> -	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
> -		rc = __compat_only_sysfs_link_entry_to_kobj(
> -		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
> +	for (ngrp = 0; ngrp < chip->groups_cnt; ++ngrp) {
> +		if (chip->groups[ngrp]->name) {
> +			rc = __compat_only_sysfs_link_entry_to_kobj(
> +				&chip->dev.parent->kobj, &chip->dev.kobj,
> +				chip->groups[ngrp]->name);
> +		} else {
> +			for (i = chip->groups[ngrp]->attrs;
> +			     (*i != NULL) && !rc;
> +			     ++i)
> +				rc = __compat_only_sysfs_link_entry_to_kobj(
> +					&chip->dev.parent->kobj,
> +					&chip->dev.kobj,
> +					(*i)->name);
> +		}
>  		if (rc) {
>  			tpm_del_legacy_sysfs(chip);
>  			return rc;
> @@ -354,6 +366,8 @@ int tpm_chip_register(struct tpm_chip *chip)
>  {
>  	int rc;
>  
> +	tpm_sysfs_add_device(chip);
> +
>  	rc = tpm1_chip_register(chip);
>  	if (rc)
>  		return rc;
> diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c
> index b46cf70..95ce90d 100644
> --- a/drivers/char/tpm/tpm-sysfs.c
> +++ b/drivers/char/tpm/tpm-sysfs.c
> @@ -265,6 +265,12 @@ static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
>  static DEVICE_ATTR_RO(timeouts);
>  
>  static struct attribute *tpm_dev_attrs[] = {
> +	&dev_attr_durations.attr,
> +	&dev_attr_timeouts.attr,
> +	NULL,
> +};
> +
> +static struct attribute *tpm1_dev_attrs[] = {
>  	&dev_attr_pubek.attr,
>  	&dev_attr_pcrs.attr,
>  	&dev_attr_enabled.attr,
> @@ -273,8 +279,10 @@ static struct attribute *tpm_dev_attrs[] = {
>  	&dev_attr_temp_deactivated.attr,
>  	&dev_attr_caps.attr,
>  	&dev_attr_cancel.attr,
> -	&dev_attr_durations.attr,
> -	&dev_attr_timeouts.attr,
> +	NULL,
> +};
> +
> +static struct attribute *tpm2_dev_attrs[] = {
>  	NULL,
>  };
>  
> @@ -282,6 +290,14 @@ static const struct attribute_group tpm_dev_group = {
>  	.attrs = tpm_dev_attrs,
>  };
>  
> +static const struct attribute_group tpm1_dev_group = {
> +	.attrs = tpm1_dev_attrs,
> +};
> +
> +static const struct attribute_group tpm2_dev_group = {
> +	.attrs = tpm2_dev_attrs,
> +};
> +
>  void tpm_sysfs_add_device(struct tpm_chip *chip)
>  {
>  	/* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
> @@ -290,4 +306,8 @@ void tpm_sysfs_add_device(struct tpm_chip *chip)
>  	 */
>  	WARN_ON(chip->groups_cnt != 0);
>  	chip->groups[chip->groups_cnt++] = &tpm_dev_group;
> +	if (chip->flags & TPM_CHIP_FLAG_TPM2)
> +		chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
> +	else
> +		chip->groups[chip->groups_cnt++] = &tpm1_dev_group;
>  }
> -- 
> 2.6.6
> 

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
diff mbox

Patch

diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index 5a2f043..6c72671 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -281,8 +281,6 @@  static int tpm1_chip_register(struct tpm_chip *chip)
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
 		return 0;
 
-	tpm_sysfs_add_device(chip);
-
 	chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
 
 	return 0;
@@ -300,14 +298,21 @@  static void tpm1_chip_unregister(struct tpm_chip *chip)
 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
 {
 	struct attribute **i;
+	int ngrp;
 
-	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
+	if (chip->flags & TPM_CHIP_FLAG_VIRTUAL)
 		return;
 
-	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
-
-	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
-		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
+	for (ngrp = 0; ngrp < chip->groups_cnt; ++ngrp) {
+		if (chip->groups[ngrp]->name) {
+			sysfs_remove_link(&chip->dev.parent->kobj,
+					  chip->groups[ngrp]->name);
+		} else {
+			for (i = chip->groups[ngrp]->attrs; *i != NULL; ++i)
+				sysfs_remove_link(&chip->dev.parent->kobj,
+						  (*i)->name);
+		}
+	}
 }
 
 /* For compatibility with legacy sysfs paths we provide symlinks from the
@@ -317,20 +322,27 @@  static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
 {
 	struct attribute **i;
-	int rc;
+	int rc = 0;
+	int ngrp;
 
-	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
+	if (chip->flags & TPM_CHIP_FLAG_VIRTUAL)
 		return 0;
 
-	rc = __compat_only_sysfs_link_entry_to_kobj(
-		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
-	if (rc && rc != -ENOENT)
-		return rc;
-
 	/* All the names from tpm-sysfs */
-	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
-		rc = __compat_only_sysfs_link_entry_to_kobj(
-		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
+	for (ngrp = 0; ngrp < chip->groups_cnt; ++ngrp) {
+		if (chip->groups[ngrp]->name) {
+			rc = __compat_only_sysfs_link_entry_to_kobj(
+				&chip->dev.parent->kobj, &chip->dev.kobj,
+				chip->groups[ngrp]->name);
+		} else {
+			for (i = chip->groups[ngrp]->attrs;
+			     (*i != NULL) && !rc;
+			     ++i)
+				rc = __compat_only_sysfs_link_entry_to_kobj(
+					&chip->dev.parent->kobj,
+					&chip->dev.kobj,
+					(*i)->name);
+		}
 		if (rc) {
 			tpm_del_legacy_sysfs(chip);
 			return rc;
@@ -354,6 +366,8 @@  int tpm_chip_register(struct tpm_chip *chip)
 {
 	int rc;
 
+	tpm_sysfs_add_device(chip);
+
 	rc = tpm1_chip_register(chip);
 	if (rc)
 		return rc;
diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c
index b46cf70..95ce90d 100644
--- a/drivers/char/tpm/tpm-sysfs.c
+++ b/drivers/char/tpm/tpm-sysfs.c
@@ -265,6 +265,12 @@  static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
 static DEVICE_ATTR_RO(timeouts);
 
 static struct attribute *tpm_dev_attrs[] = {
+	&dev_attr_durations.attr,
+	&dev_attr_timeouts.attr,
+	NULL,
+};
+
+static struct attribute *tpm1_dev_attrs[] = {
 	&dev_attr_pubek.attr,
 	&dev_attr_pcrs.attr,
 	&dev_attr_enabled.attr,
@@ -273,8 +279,10 @@  static struct attribute *tpm_dev_attrs[] = {
 	&dev_attr_temp_deactivated.attr,
 	&dev_attr_caps.attr,
 	&dev_attr_cancel.attr,
-	&dev_attr_durations.attr,
-	&dev_attr_timeouts.attr,
+	NULL,
+};
+
+static struct attribute *tpm2_dev_attrs[] = {
 	NULL,
 };
 
@@ -282,6 +290,14 @@  static const struct attribute_group tpm_dev_group = {
 	.attrs = tpm_dev_attrs,
 };
 
+static const struct attribute_group tpm1_dev_group = {
+	.attrs = tpm1_dev_attrs,
+};
+
+static const struct attribute_group tpm2_dev_group = {
+	.attrs = tpm2_dev_attrs,
+};
+
 void tpm_sysfs_add_device(struct tpm_chip *chip)
 {
 	/* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
@@ -290,4 +306,8 @@  void tpm_sysfs_add_device(struct tpm_chip *chip)
 	 */
 	WARN_ON(chip->groups_cnt != 0);
 	chip->groups[chip->groups_cnt++] = &tpm_dev_group;
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
+	else
+		chip->groups[chip->groups_cnt++] = &tpm1_dev_group;
 }