Re: [PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-25 Thread Jason Gunthorpe
On Wed, Oct 25, 2017 at 10:00:30PM +0200, Jarkko Sakkinen wrote:
> 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


Re: [PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-25 Thread Jarkko Sakkinen
On Wed, Oct 25, 2017 at 01:37:07PM -0600, Jason Gunthorpe wrote:
> On Wed, Oct 25, 2017 at 08:58:17PM +0200, Jarkko Sakkinen wrote:
> > On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan wrote:
> > > > +   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.
> > 
> > > > +   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

I'll buy that!

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

/Jarkko


Re: [PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-25 Thread Jason Gunthorpe
On Wed, Oct 25, 2017 at 08:58:17PM +0200, Jarkko Sakkinen wrote:
> On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan wrote:
> > > +   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.
> 
> > > +   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


Re: [PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-25 Thread Jarkko Sakkinen
On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan wrote:
> > +   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.

> > +   char tpm_hwrng_name[64];
> > +   struct hwrng tpm_hwrng;
> > +
> 
> Can this also be put inside the #ifdef?

Yes. It should be inside #ifdef.

/Jarkko


Re: [PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-25 Thread PrasannaKumar Muralidharan
Hi Jason,

On 25 October 2017 at 20:48, Jason Gunthorpe
 wrote:
> On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan
> wrote:
>
>> > +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.

Oh okay. No issues then.

Regards,
PrasannaKumar


Re: [PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-25 Thread Jason Gunthorpe
On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan
wrote:

> > +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


Re: [PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-25 Thread PrasannaKumar Muralidharan
Hi Jason,

Nice to see this patch. Some minor comments below.

On 25 October 2017 at 00:12, Jason Gunthorpe
 wrote:
> 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.
>
> Signed-off-by: PrasannaKumar Muralidharan 
> Signed-off-by: Jason Gunthorpe 
> Reviewed-by: Jason Gunthorpe 
> ---
>  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.

Thank you for this.

> This is compile tested only.
>
> diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
> index 95a031e9eced07..a20fed182cbcce 100644
> --- a/drivers/char/hw_random/Kconfig
> +++ b/drivers/char/hw_random/Kconfig
> @@ -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
> diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
> index 39a67defac67cb..91cb8e8213e7c1 100644
> --- a/drivers/char/hw_random/Makefile
> +++ b/drivers/char/hw_random/Makefile
> @@ -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
> diff --git a/drivers/char/hw_random/tpm-rng.c 
> b/drivers/char/hw_random/tpm-rng.c
> deleted file mode 100644
> index d6d448266f0752..00
> --- a/drivers/char/hw_random/tpm-rng.c
> +++ /dev/null
> @@ -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 
> -#include 
> -#include 
> -
> -#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(_rng);
> -}
> -module_init(rng_init);
> -
> -static void __exit rng_exit(void)
> -{
> -   hwrng_unregister(_rng);
> -}
> -module_exit(rng_exit);
> -
> -MODULE_LICENSE("GPL v2");
> -MODULE_AUTHOR("Kent Yoder ");
> -MODULE_DESCRIPTION("RNG driver for TPM devices");
> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
> index a30352202f1fdc..a95725fa77789e 100644
> --- a/drivers/char/tpm/Kconfig
> +++ b/drivers/char/tpm/Kconfig
> @@ -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---
> +   

Re: [PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-24 Thread Jarkko Sakkinen
On Tue, Oct 24, 2017 at 03:34:49PM -0600, Jason Gunthorpe wrote:
> On Tue, Oct 24, 2017 at 12:42:35PM -0600, Jason Gunthorpe wrote:
> 
> > 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

> 
> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
> index a95725fa77789e..ca89da3e4b2ae9 100644
> --- a/drivers/char/tpm/Kconfig
> +++ b/drivers/char/tpm/Kconfig
> @@ -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


Re: [PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-24 Thread Jason Gunthorpe
On Tue, Oct 24, 2017 at 12:42:35PM -0600, Jason Gunthorpe wrote:

> 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:

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index a95725fa77789e..ca89da3e4b2ae9 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -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


[PATCH] tpm: Move Linux RNG connection to hwrng

2017-10-24 Thread 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.

Signed-off-by: PrasannaKumar Muralidharan 
Signed-off-by: Jason Gunthorpe 
Reviewed-by: Jason Gunthorpe 
---
 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.

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 95a031e9eced07..a20fed182cbcce 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -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
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 39a67defac67cb..91cb8e8213e7c1 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -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
diff --git a/drivers/char/hw_random/tpm-rng.c b/drivers/char/hw_random/tpm-rng.c
deleted file mode 100644
index d6d448266f0752..00
--- a/drivers/char/hw_random/tpm-rng.c
+++ /dev/null
@@ -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 
-#include 
-#include 
-
-#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(_rng);
-}
-module_init(rng_init);
-
-static void __exit rng_exit(void)
-{
-   hwrng_unregister(_rng);
-}
-module_exit(rng_exit);
-
-MODULE_LICENSE("GPL v2");
-MODULE_AUTHOR("Kent Yoder ");
-MODULE_DESCRIPTION("RNG driver for TPM devices");
diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index a30352202f1fdc..a95725fa77789e 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -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---
diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index 0eca20c5a80cf2..f3571406fb2995