Skip to contentSkip to conversation

tpm: Move Linux RNG connection to hwrng

PATCH In review Jason Gunthorpe 3 participants 10 messages Oct 24, 2017 – Oct 25, 2017

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: Move Linux RNG connection to hwrng

The tpm-rng.c approach is completely inconsistent with how the kernel handles hotplug. Instead manage a hwrng device for each TPM. This will cause the kernel to read entropy from the TPM when it is plugged in, and allow access to the TPM rng via /dev/hwrng.

---
 drivers/char/hw_random/Kconfig   | 13 -----------
 drivers/char/hw_random/Makefile  |  1 -
 drivers/char/hw_random/tpm-rng.c | 50 ----------------------------------------
 drivers/char/tpm/Kconfig         | 13 +++++++++++
 drivers/char/tpm/tpm-chip.c      | 41 ++++++++++++++++++++++++++++----
 drivers/char/tpm/tpm-interface.c | 37 ++++++++++++++++-------------
 drivers/char/tpm/tpm.h           |  5 ++++
 7 files changed, 76 insertions(+), 84 deletions(-)
 delete mode 100644 drivers/char/hw_random/tpm-rng.c

All,

It is such a good idea, I took PrasannaKumar's patch, reviewed and revised it to the level it could be merged.

This is compile tested only.

drivers/char/hw_random/Kconfig +0 -13
index 95a031e9eced07..a20fed182cbcce 100644
@@ -306,19 +306,6 @@ config HW_RANDOM_POWERNV
 
 	  If unsure, say Y.
 
-config HW_RANDOM_TPM
-	tristate "TPM HW Random Number Generator support"
-	depends on TCG_TPM
-	default HW_RANDOM
-	---help---
-	  This driver provides kernel-side support for the Random Number
-	  Generator in the Trusted Platform Module
-
-	  To compile this driver as a module, choose M here: the
-	  module will be called tpm-rng.
-
-	  If unsure, say Y.
-
 config HW_RANDOM_HISI
 	tristate "Hisilicon Random Number Generator support"
 	depends on HW_RANDOM && ARCH_HISI
drivers/char/hw_random/Makefile +0 -1
index 39a67defac67cb..91cb8e8213e7c1 100644
@@ -26,7 +26,6 @@ obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
 obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
 obj-$(CONFIG_HW_RANDOM_HISI)	+= hisi-rng.o
-obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
 obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
 obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
drivers/char/hw_random/tpm-rng.c deleted +0 -50

50 lines not on this page · raw message

drivers/char/tpm/Kconfig +13 -0
index a30352202f1fdc..a95725fa77789e 100644
@@ -26,6 +26,19 @@ menuconfig TCG_TPM
 
 if TCG_TPM
 
+config HW_RANDOM_TPM
+	tristate "TPM HW Random Number Generator support"
+	depends on TCG_TPM && HW_RANDOM
+	default HW_RANDOM
+	---help---
+	  This driver provides kernel-side support for the Random Number
+	  Generator in the Trusted Platform Module
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called tpm-rng.
+
+	  If unsure, say Y.
+
 config TCG_TIS_CORE
 	tristate
 	---help---
drivers/char/tpm/tpm-chip.c +37 -4

72 lines not on this page · raw message

drivers/char/tpm/tpm-interface.c +21 -16
index 1d6729be4cd637..310de288549818 100644
@@ -1194,9 +1194,8 @@ static const struct tpm_input_header tpm_getrandom_header = {
  *
  * Returns < 0 on error and the number of bytes read on success
  */
-int tpm_get_random(u32 chip_num, u8 *out, size_t max)
+int tpm_get_random_dev(struct tpm_chip *chip, u8 *out, size_t max)
 {
-	struct tpm_chip *chip;
 	struct tpm_cmd_t tpm_cmd;
 	u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength;
 	int err, total = 0, retries = 5;
@@ -1205,15 +1204,8 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
 	if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
 		return -EINVAL;
 
-	chip = tpm_chip_find_get(chip_num);
-	if (chip = NULL)
-		return -ENODEV;
-
-	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
-		err = tpm2_get_random(chip, out, max);
-		tpm_put_ops(chip);
-		return err;
-	}
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		return tpm2_get_random(chip, out, max);
 
 	do {
 		tpm_cmd.header.in = tpm_getrandom_header;
@@ -1231,10 +1223,9 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
 
 		rlength = be32_to_cpu(tpm_cmd.header.out.length);
 		if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
-			      recd) {
-			total = -EFAULT;
-			break;
-		}
+			      recd)
+			return -EFAULT;
+
 		memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
 
 		dest += recd;
@@ -1242,9 +1233,23 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
 		num_bytes -= recd;
 	} while (retries-- && total < max);
 
-	tpm_put_ops(chip);
 	return total ? total : -EIO;
 }
+
+int tpm_get_random(u32 chip_num, u8 *out, size_t max)
+{
+	int rc;
+	struct tpm_chip *chip;
+
+	chip = tpm_chip_find_get(chip_num);
+	if (chip = NULL)
+		return -ENODEV;
+
+	rc = tpm_get_random_dev(chip, out, max);
+
+	tpm_put_ops(chip);
+	return rc;
+}
 EXPORT_SYMBOL_GPL(tpm_get_random);
 
 /**
drivers/char/tpm/tpm.h +5 -0
index 2d5466a72e40f8..0e41e044d81be2 100644
@@ -26,6 +26,7 @@
 #include <linux/module.h>
 #include <linux/delay.h>
 #include <linux/fs.h>
+#include <linux/hw_random.h>
 #include <linux/mutex.h>
 #include <linux/sched.h>
 #include <linux/platform_device.h>
@@ -210,6 +211,9 @@ struct tpm_chip {
 	int dev_num;		/* /dev/tpm# */
 	unsigned long is_open;	/* only one allowed */
 
+	char tpm_hwrng_name[64];
+	struct hwrng tpm_hwrng;
+
 	struct mutex tpm_mutex;	/* tpm is processing */
 
 	unsigned long timeout_a; /* jiffies */
@@ -548,6 +552,7 @@ void tpm_chip_unregister(struct tpm_chip *chip);
 void tpm_sysfs_add_device(struct tpm_chip *chip);
 
 int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
+int tpm_get_random_dev(struct tpm_chip *chip, u8 *out, size_t max);
 
 #ifdef CONFIG_ACPI
 extern void tpm_add_ppi(struct tpm_chip *chip);
Signature · 1 line
2.7.4
#1 lore raw
Quoted 1 line · Jason Gunthorpe

This is compile tested only.

0day says the kconfig has a problem when randomized, here is the fix I will roll into a v2 in a few days:

drivers/char/tpm/Kconfig +3 -6
index a95725fa77789e..ca89da3e4b2ae9 100644
@@ -27,16 +27,13 @@ menuconfig TCG_TPM
 if TCG_TPM
 
 config HW_RANDOM_TPM
-       tristate "TPM HW Random Number Generator support"
-       depends on TCG_TPM && HW_RANDOM
-       default HW_RANDOM
+       bool "TPM HW Random Number Generator support"
+       depends on TCG_TPM && HW_RANDOM && !(TCG_TPM=y && HW_RANDOM=m)
+       default y
        ---help---
          This driver provides kernel-side support for the Random Number
          Generator in the Trusted Platform Module
 
-         To compile this driver as a module, choose M here: the
-         module will be called tpm-rng.
-
          If unsure, say Y.
 
 config TCG_TIS_CORE
#2 lore raw
Quoted 6 lines · Jason Gunthorpe
Quoted 1 line · Jason Gunthorpe

This is compile tested only.

0day says the kconfig has a problem when randomized, here is the fix I will roll into a v2 in a few days:

I will probably have to postpone the review to next week anyway so take your time :-)

/Jarkko

Quoted 25 lines
drivers/char/tpm/Kconfig +3 -6
index a95725fa77789e..ca89da3e4b2ae9 100644
@@ -27,16 +27,13 @@ menuconfig TCG_TPM
 if TCG_TPM
 
 config HW_RANDOM_TPM
-       tristate "TPM HW Random Number Generator support"
-       depends on TCG_TPM && HW_RANDOM
-       default HW_RANDOM
+       bool "TPM HW Random Number Generator support"
+       depends on TCG_TPM && HW_RANDOM && !(TCG_TPM=y && HW_RANDOM=m)
+       default y
        ---help---
          This driver provides kernel-side support for the Random Number
          Generator in the Trusted Platform Module
 
-         To compile this driver as a module, choose M here: the
-         module will be called tpm-rng.
-
          If unsure, say Y.
 
 config TCG_TIS_CORE
#3 lore raw
In reply to Jason Gunthorpe

Hi Jason,

Nice to see this patch. Some minor comments below.

Quoted 19 lines · Jason Gunthorpe

The tpm-rng.c approach is completely inconsistent with how the kernel handles hotplug. Instead manage a hwrng device for each TPM. This will cause the kernel to read entropy from the TPM when it is plugged in, and allow access to the TPM rng via /dev/hwrng.

---
 drivers/char/hw_random/Kconfig   | 13 -----------
 drivers/char/hw_random/Makefile  |  1 -
 drivers/char/hw_random/tpm-rng.c | 50 ----------------------------------------
 drivers/char/tpm/Kconfig         | 13 +++++++++++
 drivers/char/tpm/tpm-chip.c      | 41 ++++++++++++++++++++++++++++----
 drivers/char/tpm/tpm-interface.c | 37 ++++++++++++++++-------------
 drivers/char/tpm/tpm.h           |  5 ++++
 7 files changed, 76 insertions(+), 84 deletions(-)
 delete mode 100644 drivers/char/hw_random/tpm-rng.c
Quoted 4 lines

All,

It is such a good idea, I took PrasannaKumar's patch, reviewed and revised it to the level it could be merged.

Thank you for this.

Quoted 136 lines

This is compile tested only.

drivers/char/hw_random/Kconfig +0 -13
index 95a031e9eced07..a20fed182cbcce 100644
@@ -306,19 +306,6 @@ config HW_RANDOM_POWERNV
 
          If unsure, say Y.
 
-config HW_RANDOM_TPM
-       tristate "TPM HW Random Number Generator support"
-       depends on TCG_TPM
-       default HW_RANDOM
-       ---help---
-         This driver provides kernel-side support for the Random Number
-         Generator in the Trusted Platform Module
-
-         To compile this driver as a module, choose M here: the
-         module will be called tpm-rng.
-
-         If unsure, say Y.
-
 config HW_RANDOM_HISI
        tristate "Hisilicon Random Number Generator support"
        depends on HW_RANDOM && ARCH_HISI
drivers/char/hw_random/Makefile +0 -1
index 39a67defac67cb..91cb8e8213e7c1 100644
@@ -26,7 +26,6 @@ obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
 obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
 obj-$(CONFIG_HW_RANDOM_HISI)   += hisi-rng.o
-obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
 obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
 obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
drivers/char/hw_random/tpm-rng.c deleted +0 -50
deleted file mode 100644
index d6d448266f0752..00000000000000
@@ -1,50 +0,0 @@
-/*
- * Copyright (C) 2012 Kent Yoder IBM Corporation
- *
- * HWRNG interfaces to pull RNG data from a TPM
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
- */
-
-#include <linux/module.h>
-#include <linux/hw_random.h>
-#include <linux/tpm.h>
-
-#define MODULE_NAME "tpm-rng"
-
-static int tpm_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
-{
-       return tpm_get_random(TPM_ANY_NUM, data, max);
-}
-
-static struct hwrng tpm_rng = {
-       .name = MODULE_NAME,
-       .read = tpm_rng_read,
-};
-
-static int __init rng_init(void)
-{
-       return hwrng_register(&tpm_rng);
-}
-module_init(rng_init);
-
-static void __exit rng_exit(void)
-{
-       hwrng_unregister(&tpm_rng);
-}
-module_exit(rng_exit);
-
-MODULE_LICENSE("GPL v2");
-MODULE_AUTHOR("Kent Yoder <[email protected]>");
-MODULE_DESCRIPTION("RNG driver for TPM devices");
drivers/char/tpm/Kconfig +13 -0
index a30352202f1fdc..a95725fa77789e 100644
@@ -26,6 +26,19 @@ menuconfig TCG_TPM
 
 if TCG_TPM
 
+config HW_RANDOM_TPM
+       tristate "TPM HW Random Number Generator support"
+       depends on TCG_TPM && HW_RANDOM
+       default HW_RANDOM
+       ---help---
+         This driver provides kernel-side support for the Random Number
+         Generator in the Trusted Platform Module
+
+         To compile this driver as a module, choose M here: the
+         module will be called tpm-rng.
+
+         If unsure, say Y.
+
 config TCG_TIS_CORE
        tristate
        ---help---
drivers/char/tpm/tpm-chip.c +3 -0
index 0eca20c5a80cf2..f3571406fb2995 100644
@@ -26,6 +26,7 @@
 #include <linux/spinlock.h>
 #include <linux/freezer.h>
 #include <linux/major.h>
+#include <linux/hw_random.h>
 #include "tpm.h"
 #include "tpm_eventlog.h"
 
@@ -387,6 +388,26 @@ static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
 
        return 0;
 }
+
+static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)

drivers/char/tpm/tpm-chip.c static int tpm_add_legacy_sysfs(struct tpm_chip *chip) · line 402

Quoted 10 lines
drivers/char/tpm/tpm-chip.c +10 -0
+{
+       struct tpm_chip *chip = container_of(rng, struct tpm_chip, tpm_hwrng);
+
+       return tpm_get_random_dev(chip, data, max);
+}
+
+static int tpm_add_hwrng(struct tpm_chip *chip)
+{
+       if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+               return 0;

Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if condition can be avoided.

Quoted 138 lines
drivers/char/tpm/tpm-chip.c +24 -4
+
+       snprintf(chip->tpm_hwrng_name, sizeof(chip->tpm_hwrng_name),
+                "tpm-rng-%d", chip->dev_num);
+       chip->tpm_hwrng.name = chip->tpm_hwrng_name;
+       chip->tpm_hwrng.read = tpm_hwrng_read;
+       return hwrng_register(&chip->tpm_hwrng);
+}
+
 /*
  * tpm_chip_register() - create a character device for the TPM chip
  * @chip: TPM chip to use.
@@ -419,11 +440,13 @@ int tpm_chip_register(struct tpm_chip *chip)
 
        tpm_add_ppi(chip);
 
+       rc = tpm_add_hwrng(chip);
+       if (rc)
+               goto out_ppi;
+
        rc = tpm_add_char_device(chip);
-       if (rc) {
-               tpm_bios_log_teardown(chip);
-               return rc;
-       }
+       if (rc)
+               goto out_hwrng;
 
        rc = tpm_add_legacy_sysfs(chip);
        if (rc) {
@@ -432,6 +455,14 @@ int tpm_chip_register(struct tpm_chip *chip)
        }
 
        return 0;
+
+out_hwrng:
+       if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+               hwrng_unregister(&chip->tpm_hwrng);
+out_ppi:
+       tpm_bios_log_teardown(chip);
+
+       return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_chip_register);
 
@@ -451,6 +482,8 @@ EXPORT_SYMBOL_GPL(tpm_chip_register);
 void tpm_chip_unregister(struct tpm_chip *chip)
 {
        tpm_del_legacy_sysfs(chip);
+       if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+               hwrng_unregister(&chip->tpm_hwrng);
        tpm_bios_log_teardown(chip);
        if (chip->flags & TPM_CHIP_FLAG_TPM2)
                cdev_device_del(&chip->cdevs, &chip->devs);
drivers/char/tpm/tpm-interface.c +21 -16
index 1d6729be4cd637..310de288549818 100644
@@ -1194,9 +1194,8 @@ static const struct tpm_input_header tpm_getrandom_header = {
  *
  * Returns < 0 on error and the number of bytes read on success
  */
-int tpm_get_random(u32 chip_num, u8 *out, size_t max)
+int tpm_get_random_dev(struct tpm_chip *chip, u8 *out, size_t max)
 {
-       struct tpm_chip *chip;
        struct tpm_cmd_t tpm_cmd;
        u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength;
        int err, total = 0, retries = 5;
@@ -1205,15 +1204,8 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
        if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
                return -EINVAL;
 
-       chip = tpm_chip_find_get(chip_num);
-       if (chip == NULL)
-               return -ENODEV;
-
-       if (chip->flags & TPM_CHIP_FLAG_TPM2) {
-               err = tpm2_get_random(chip, out, max);
-               tpm_put_ops(chip);
-               return err;
-       }
+       if (chip->flags & TPM_CHIP_FLAG_TPM2)
+               return tpm2_get_random(chip, out, max);
 
        do {
                tpm_cmd.header.in = tpm_getrandom_header;
@@ -1231,10 +1223,9 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
 
                rlength = be32_to_cpu(tpm_cmd.header.out.length);
                if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
-                             recd) {
-                       total = -EFAULT;
-                       break;
-               }
+                             recd)
+                       return -EFAULT;
+
                memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
 
                dest += recd;
@@ -1242,9 +1233,23 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
                num_bytes -= recd;
        } while (retries-- && total < max);
 
-       tpm_put_ops(chip);
        return total ? total : -EIO;
 }
+
+int tpm_get_random(u32 chip_num, u8 *out, size_t max)
+{
+       int rc;
+       struct tpm_chip *chip;
+
+       chip = tpm_chip_find_get(chip_num);
+       if (chip == NULL)
+               return -ENODEV;
+
+       rc = tpm_get_random_dev(chip, out, max);
+
+       tpm_put_ops(chip);
+       return rc;
+}
 EXPORT_SYMBOL_GPL(tpm_get_random);
 
 /**
drivers/char/tpm/tpm.h +1 -0
index 2d5466a72e40f8..0e41e044d81be2 100644
@@ -26,6 +26,7 @@
 #include <linux/module.h>
 #include <linux/delay.h>
 #include <linux/fs.h>
+#include <linux/hw_random.h>
 #include <linux/mutex.h>
 #include <linux/sched.h>
 #include <linux/platform_device.h>

drivers/char/tpm/tpm.h struct tpm_chip { · line 216

Quoted 6 lines
drivers/char/tpm/tpm.h +3 -0
@@ -210,6 +211,9 @@ struct tpm_chip {
        int dev_num;            /* /dev/tpm# */
        unsigned long is_open;  /* only one allowed */
 
+       char tpm_hwrng_name[64];
+       struct hwrng tpm_hwrng;
+

Can this also be put inside the #ifdef?

Quoted 12 lines
drivers/char/tpm/tpm.h +1 -1
        struct mutex tpm_mutex; /* tpm is processing */
 
        unsigned long timeout_a; /* jiffies */
@@ -548,6 +552,7 @@ void tpm_chip_unregister(struct tpm_chip *chip);
 void tpm_sysfs_add_device(struct tpm_chip *chip);
 
 int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
+int tpm_get_random_dev(struct tpm_chip *chip, u8 *out, size_t max);
 
 #ifdef CONFIG_ACPI
 extern void tpm_add_ppi(struct tpm_chip *chip);
--
Quoted 2 lines

2.7.4

Regards,
PrasannaKumar

#4 lore raw
Quoted 7 lines · PrasannaKumar Muralidharan

drivers/char/tpm/tpm-chip.c static int tpm_add_legacy_sysfs(struct tpm_chip *chip) · line 402

Quoted 4 lines
drivers/char/tpm/tpm-chip.c +4 -0
+static int tpm_add_hwrng(struct tpm_chip *chip)
+{
+       if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+               return 0;

Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if condition can be avoided.

Generally speaking IS_ENABLED is prefered over #ifdef as it reduces the set of compilation combinations.

Jason

#5 lore raw

Hi Jason,

Quoted 11 lines · Jason Gunthorpe
Quoted 7 lines · PrasannaKumar Muralidharan

drivers/char/tpm/tpm-chip.c static int tpm_add_legacy_sysfs(struct tpm_chip *chip) · line 402

Quoted 4 lines
drivers/char/tpm/tpm-chip.c +4 -0
+static int tpm_add_hwrng(struct tpm_chip *chip)
+{
+       if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+               return 0;

Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if condition can be avoided.

Quoted 2 lines

Generally speaking IS_ENABLED is prefered over #ifdef as it reduces the set of compilation combinations.

Oh okay. No issues then.

Regards,
PrasannaKumar

#6 lore raw
In reply to PrasannaKumar Muralidharan
Quoted 5 lines · PrasannaKumar Muralidharan

drivers/char/tpm/tpm-chip.c static int tpm_add_legacy_sysfs(struct tpm_chip *chip) · line 402

Quoted 2 lines
drivers/char/tpm/tpm-chip.c +2 -0
+       if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+               return 0;

Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if condition can be avoided.

Nope. There is no reason to avoid the if-condition. Compiler will take care of it. IS_ENABLED() macro is available just for the purpose Jason is using it.

Quoted 5 lines

drivers/char/tpm/tpm.h struct tpm_chip { · line 216

Quoted 3 lines
drivers/char/tpm/tpm.h +3 -0
+       char tpm_hwrng_name[64];
+       struct hwrng tpm_hwrng;
+

Can this also be put inside the #ifdef?

Yes. It should be inside #ifdef.

/Jarkko

#7 lore raw
Quoted 11 lines · Jarkko Sakkinen
Quoted 5 lines · PrasannaKumar Muralidharan

drivers/char/tpm/tpm-chip.c static int tpm_add_legacy_sysfs(struct tpm_chip *chip) · line 402

Quoted 2 lines
drivers/char/tpm/tpm-chip.c +2 -0
+       if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+               return 0;

Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if condition can be avoided.

Nope. There is no reason to avoid the if-condition. Compiler will take care of it. IS_ENABLED() macro is available just for the purpose Jason is using it.

Quoted 7 lines
Quoted 5 lines

drivers/char/tpm/tpm.h struct tpm_chip { · line 216

Quoted 3 lines
drivers/char/tpm/tpm.h +3 -0
+       char tpm_hwrng_name[64];
+       struct hwrng tpm_hwrng;
+

Can this also be put inside the #ifdef?

Yes. It should be inside #ifdef.

Then we need #idefs in the .c code, IS_ENABLED is not enough :\ I don't think the few bytes matters enough to bother.

Jason

#8 lore raw
Quoted 16 lines · Jason Gunthorpe
Quoted 15 lines · Jarkko Sakkinen
Quoted 5 lines · PrasannaKumar Muralidharan

drivers/char/tpm/tpm-chip.c static int tpm_add_legacy_sysfs(struct tpm_chip *chip) · line 402

Quoted 2 lines
drivers/char/tpm/tpm-chip.c +2 -0
+       if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+               return 0;

Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if condition can be avoided.

Nope. There is no reason to avoid the if-condition. Compiler will take care of it. IS_ENABLED() macro is available just for the purpose Jason is using it.

Quoted 4 lines
Quoted 3 lines
drivers/char/tpm/tpm.h +3 -0
+       char tpm_hwrng_name[64];
+       struct hwrng tpm_hwrng;
+
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Can this also be put inside the #ifdef?

Yes. It should be inside #ifdef.

Then we need #idefs in the .c code, IS_ENABLED is not enough :\ I don't think the few bytes matters enough to bother.

Jason

I'll buy that!

A minor tidbit: could the tpm_ prefix removed from those fields?

/Jarkko

#9 lore raw
Quoted 1 line · Jarkko Sakkinen

A minor tidbit: could the tpm_ prefix removed from those fields?

Yes, in v2

I will send v2 when you land your rework patch as this will depend on it.

Jason