All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] tpm: Add reserved memory event log
@ 2023-01-13 16:10 Eddie James
  2023-01-13 16:10 ` [PATCH v2 1/2] tpm: Use managed allocation for bios " Eddie James
  2023-01-13 16:10 ` [PATCH v2 2/2] tpm: Add reserved memory " Eddie James
  0 siblings, 2 replies; 11+ messages in thread
From: Eddie James @ 2023-01-13 16:10 UTC (permalink / raw)
  To: linux-integrity; +Cc: linux-kernel, jgg, jarkko, peterhuewe, Eddie James

Some platforms may desire to pass the event log up to linux in the
form of a reserved memory region. Add support for this in the TPM
core to find the reserved memory region and map it. Since the memory
is mapped, not allocated, change all the event log allocation
functions to managed allocations so that the memory can be either
automatically freed or unmapped.

Changes since v1:
 - Use managed memory

Eddie James (2):
  tpm: Use managed allocation for bios event log
  tpm: Add reserved memory event log

 drivers/char/tpm/eventlog/acpi.c |  5 ++--
 drivers/char/tpm/eventlog/efi.c  | 13 +++++-----
 drivers/char/tpm/eventlog/of.c   | 41 ++++++++++++++++++++++++++++++--
 drivers/char/tpm/tpm-chip.c      |  1 -
 4 files changed, 49 insertions(+), 11 deletions(-)

-- 
2.31.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/2] tpm: Use managed allocation for bios event log
  2023-01-13 16:10 [PATCH v2 0/2] tpm: Add reserved memory event log Eddie James
@ 2023-01-13 16:10 ` Eddie James
  2023-01-21  0:11   ` Jarkko Sakkinen
  2023-01-13 16:10 ` [PATCH v2 2/2] tpm: Add reserved memory " Eddie James
  1 sibling, 1 reply; 11+ messages in thread
From: Eddie James @ 2023-01-13 16:10 UTC (permalink / raw)
  To: linux-integrity; +Cc: linux-kernel, jgg, jarkko, peterhuewe, Eddie James

Since the bios event log is freed in the device release function,
let devres handle the deallocation. This will allow other memory
allocation/mapping functions to be used for the bios event log.

Signed-off-by: Eddie James <eajames@linux.ibm.com>
---
 drivers/char/tpm/eventlog/acpi.c |  5 +++--
 drivers/char/tpm/eventlog/efi.c  | 13 +++++++------
 drivers/char/tpm/eventlog/of.c   |  3 ++-
 drivers/char/tpm/tpm-chip.c      |  1 -
 4 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/drivers/char/tpm/eventlog/acpi.c b/drivers/char/tpm/eventlog/acpi.c
index 0913d3eb8d51..40360e599bc3 100644
--- a/drivers/char/tpm/eventlog/acpi.c
+++ b/drivers/char/tpm/eventlog/acpi.c
@@ -14,6 +14,7 @@
  * Access to the event log extended by the TCG BIOS of PC platform
  */
 
+#include <linux/device.h>
 #include <linux/seq_file.h>
 #include <linux/fs.h>
 #include <linux/security.h>
@@ -135,7 +136,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)
 	}
 
 	/* malloc EventLog space */
-	log->bios_event_log = kmalloc(len, GFP_KERNEL);
+	log->bios_event_log = devm_kmalloc(&chip->dev, len, GFP_KERNEL);
 	if (!log->bios_event_log)
 		return -ENOMEM;
 
@@ -160,7 +161,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)
 	return format;
 
 err:
-	kfree(log->bios_event_log);
+	devm_kfree(&chip->dev, log->bios_event_log);
 	log->bios_event_log = NULL;
 	return ret;
 }
diff --git a/drivers/char/tpm/eventlog/efi.c b/drivers/char/tpm/eventlog/efi.c
index e6cb9d525e30..4e9d7c2bf32e 100644
--- a/drivers/char/tpm/eventlog/efi.c
+++ b/drivers/char/tpm/eventlog/efi.c
@@ -6,6 +6,7 @@
  *      Thiebaud Weksteen <tweek@google.com>
  */
 
+#include <linux/device.h>
 #include <linux/efi.h>
 #include <linux/tpm_eventlog.h>
 
@@ -55,7 +56,7 @@ int tpm_read_log_efi(struct tpm_chip *chip)
 	}
 
 	/* malloc EventLog space */
-	log->bios_event_log = kmemdup(log_tbl->log, log_size, GFP_KERNEL);
+	log->bios_event_log = devm_kmemdup(&chip->dev, log_tbl->log, log_size, GFP_KERNEL);
 	if (!log->bios_event_log) {
 		ret = -ENOMEM;
 		goto out;
@@ -76,7 +77,7 @@ int tpm_read_log_efi(struct tpm_chip *chip)
 			     MEMREMAP_WB);
 	if (!final_tbl) {
 		pr_err("Could not map UEFI TPM final log\n");
-		kfree(log->bios_event_log);
+		devm_kfree(&chip->dev, log->bios_event_log);
 		ret = -ENOMEM;
 		goto out;
 	}
@@ -91,11 +92,11 @@ int tpm_read_log_efi(struct tpm_chip *chip)
 	 * Allocate memory for the 'combined log' where we will append the
 	 * 'final events log' to.
 	 */
-	tmp = krealloc(log->bios_event_log,
-		       log_size + final_events_log_size,
-		       GFP_KERNEL);
+	tmp = devm_krealloc(&chip->dev, log->bios_event_log,
+			    log_size + final_events_log_size,
+			    GFP_KERNEL);
 	if (!tmp) {
-		kfree(log->bios_event_log);
+		devm_kfree(&chip->dev, log->bios_event_log);
 		ret = -ENOMEM;
 		goto out;
 	}
diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
index a9ce66d09a75..741ab2204b11 100644
--- a/drivers/char/tpm/eventlog/of.c
+++ b/drivers/char/tpm/eventlog/of.c
@@ -10,6 +10,7 @@
  * Read the event log created by the firmware on PPC64
  */
 
+#include <linux/device.h>
 #include <linux/slab.h>
 #include <linux/of.h>
 #include <linux/tpm_eventlog.h>
@@ -65,7 +66,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
 		return -EIO;
 	}
 
-	log->bios_event_log = kmemdup(__va(base), size, GFP_KERNEL);
+	log->bios_event_log = devm_kmemdup(&chip->dev, __va(base), size, GFP_KERNEL);
 	if (!log->bios_event_log)
 		return -ENOMEM;
 
diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index 741d8f3e8fb3..b99f55f2d4fd 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -267,7 +267,6 @@ static void tpm_dev_release(struct device *dev)
 	idr_remove(&dev_nums_idr, chip->dev_num);
 	mutex_unlock(&idr_lock);
 
-	kfree(chip->log.bios_event_log);
 	kfree(chip->work_space.context_buf);
 	kfree(chip->work_space.session_buf);
 	kfree(chip->allocated_banks);
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 2/2] tpm: Add reserved memory event log
  2023-01-13 16:10 [PATCH v2 0/2] tpm: Add reserved memory event log Eddie James
  2023-01-13 16:10 ` [PATCH v2 1/2] tpm: Use managed allocation for bios " Eddie James
@ 2023-01-13 16:10 ` Eddie James
  2023-01-18 13:27   ` Stefan Berger
  2023-01-21  0:13   ` Jarkko Sakkinen
  1 sibling, 2 replies; 11+ messages in thread
From: Eddie James @ 2023-01-13 16:10 UTC (permalink / raw)
  To: linux-integrity; +Cc: linux-kernel, jgg, jarkko, peterhuewe, Eddie James

Some platforms may desire to pass the event log up to linux in the
form of a reserved memory region. Add support for this in the TPM
core to find the reserved memory region and map it.

Signed-off-by: Eddie James <eajames@linux.ibm.com>
---
 drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
index 741ab2204b11..c815cadf00a4 100644
--- a/drivers/char/tpm/eventlog/of.c
+++ b/drivers/char/tpm/eventlog/of.c
@@ -12,12 +12,48 @@
 
 #include <linux/device.h>
 #include <linux/slab.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_reserved_mem.h>
 #include <linux/tpm_eventlog.h>
 
 #include "../tpm.h"
 #include "common.h"
 
+static int tpm_read_log_memory_region(struct tpm_chip *chip)
+{
+	struct device_node *node;
+	struct resource res;
+	int rc;
+
+	node = of_parse_phandle(chip->dev.parent->of_node, "memory-region", 0);
+	if (!node) {
+		dev_info(&chip->dev, "no phandle\n");
+		return -ENODEV;
+	}
+
+	rc = of_address_to_resource(node, 0, &res);
+	of_node_put(node);
+	if (rc) {
+		dev_info(&chip->dev, "no mem\n");
+		return rc;
+	}
+
+	chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
+						 MEMREMAP_WB);
+	if (!chip->log.bios_event_log) {
+		dev_info(&chip->dev, "err memremap\n");
+		return -ENOMEM;
+	}
+
+	chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
+
+	return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
+		EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
+}
+
 int tpm_read_log_of(struct tpm_chip *chip)
 {
 	struct device_node *np;
@@ -39,7 +75,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
 	sizep = of_get_property(np, "linux,sml-size", NULL);
 	basep = of_get_property(np, "linux,sml-base", NULL);
 	if (sizep == NULL && basep == NULL)
-		return -ENODEV;
+		return tpm_read_log_memory_region(chip);
 	if (sizep == NULL || basep == NULL)
 		return -EIO;
 
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/2] tpm: Add reserved memory event log
  2023-01-13 16:10 ` [PATCH v2 2/2] tpm: Add reserved memory " Eddie James
@ 2023-01-18 13:27   ` Stefan Berger
  2023-01-18 14:49     ` Eddie James
  2023-01-21  0:13   ` Jarkko Sakkinen
  1 sibling, 1 reply; 11+ messages in thread
From: Stefan Berger @ 2023-01-18 13:27 UTC (permalink / raw)
  To: Eddie James, linux-integrity; +Cc: linux-kernel, jgg, jarkko, peterhuewe



On 1/13/23 11:10, Eddie James wrote:
> Some platforms may desire to pass the event log up to linux in the

Which platforms are these that work like this?

    Stefan

> form of a reserved memory region. Add support for this in the TPM
> core to find the reserved memory region and map it.
> 
> Signed-off-by: Eddie James <eajames@linux.ibm.com>
> ---
>   drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
>   1 file changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
> index 741ab2204b11..c815cadf00a4 100644
> --- a/drivers/char/tpm/eventlog/of.c
> +++ b/drivers/char/tpm/eventlog/of.c
> @@ -12,12 +12,48 @@
>   
>   #include <linux/device.h>
>   #include <linux/slab.h>
> +#include <linux/io.h>
> +#include <linux/ioport.h>
>   #include <linux/of.h>
> +#include <linux/of_address.h>
> +#include <linux/of_reserved_mem.h>
>   #include <linux/tpm_eventlog.h>
>   
>   #include "../tpm.h"
>   #include "common.h"
>   
> +static int tpm_read_log_memory_region(struct tpm_chip *chip)
> +{
> +	struct device_node *node;
> +	struct resource res;
> +	int rc;
> +
> +	node = of_parse_phandle(chip->dev.parent->of_node, "memory-region", 0);
> +	if (!node) {
> +		dev_info(&chip->dev, "no phandle\n");
> +		return -ENODEV;
> +	}
> +
> +	rc = of_address_to_resource(node, 0, &res);
> +	of_node_put(node);
> +	if (rc) {
> +		dev_info(&chip->dev, "no mem\n");
> +		return rc;
> +	}
> +
> +	chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
> +						 MEMREMAP_WB);
> +	if (!chip->log.bios_event_log) {
> +		dev_info(&chip->dev, "err memremap\n");
> +		return -ENOMEM;
> +	}
> +
> +	chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
> +
> +	return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
> +		EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
> +}
> +
>   int tpm_read_log_of(struct tpm_chip *chip)
>   {
>   	struct device_node *np;
> @@ -39,7 +75,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>   	sizep = of_get_property(np, "linux,sml-size", NULL);
>   	basep = of_get_property(np, "linux,sml-base", NULL);
>   	if (sizep == NULL && basep == NULL)
> -		return -ENODEV;
> +		return tpm_read_log_memory_region(chip);
>   	if (sizep == NULL || basep == NULL)
>   		return -EIO;
>   

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/2] tpm: Add reserved memory event log
  2023-01-18 13:27   ` Stefan Berger
@ 2023-01-18 14:49     ` Eddie James
  2023-01-18 15:01       ` Stefan Berger
  0 siblings, 1 reply; 11+ messages in thread
From: Eddie James @ 2023-01-18 14:49 UTC (permalink / raw)
  To: Stefan Berger, linux-integrity; +Cc: linux-kernel, jgg, jarkko, peterhuewe


On 1/18/23 07:27, Stefan Berger wrote:
>
>
> On 1/13/23 11:10, Eddie James wrote:
>> Some platforms may desire to pass the event log up to linux in the
>
> Which platforms are these that work like this?


Platforms booting from U-Boot without EFI. So at the moment, IBM's 
OpenBMC systems hope to use this.

Thanks,

Eddie


>
>    Stefan
>
>> form of a reserved memory region. Add support for this in the TPM
>> core to find the reserved memory region and map it.
>>
>> Signed-off-by: Eddie James <eajames@linux.ibm.com>
>> ---
>>   drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
>>   1 file changed, 37 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/char/tpm/eventlog/of.c 
>> b/drivers/char/tpm/eventlog/of.c
>> index 741ab2204b11..c815cadf00a4 100644
>> --- a/drivers/char/tpm/eventlog/of.c
>> +++ b/drivers/char/tpm/eventlog/of.c
>> @@ -12,12 +12,48 @@
>>     #include <linux/device.h>
>>   #include <linux/slab.h>
>> +#include <linux/io.h>
>> +#include <linux/ioport.h>
>>   #include <linux/of.h>
>> +#include <linux/of_address.h>
>> +#include <linux/of_reserved_mem.h>
>>   #include <linux/tpm_eventlog.h>
>>     #include "../tpm.h"
>>   #include "common.h"
>>   +static int tpm_read_log_memory_region(struct tpm_chip *chip)
>> +{
>> +    struct device_node *node;
>> +    struct resource res;
>> +    int rc;
>> +
>> +    node = of_parse_phandle(chip->dev.parent->of_node, 
>> "memory-region", 0);
>> +    if (!node) {
>> +        dev_info(&chip->dev, "no phandle\n");
>> +        return -ENODEV;
>> +    }
>> +
>> +    rc = of_address_to_resource(node, 0, &res);
>> +    of_node_put(node);
>> +    if (rc) {
>> +        dev_info(&chip->dev, "no mem\n");
>> +        return rc;
>> +    }
>> +
>> +    chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, 
>> resource_size(&res),
>> +                         MEMREMAP_WB);
>> +    if (!chip->log.bios_event_log) {
>> +        dev_info(&chip->dev, "err memremap\n");
>> +        return -ENOMEM;
>> +    }
>> +
>> +    chip->log.bios_event_log_end = chip->log.bios_event_log + 
>> resource_size(&res);
>> +
>> +    return chip->flags & TPM_CHIP_FLAG_TPM2 ? 
>> EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
>> +        EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
>> +}
>> +
>>   int tpm_read_log_of(struct tpm_chip *chip)
>>   {
>>       struct device_node *np;
>> @@ -39,7 +75,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>>       sizep = of_get_property(np, "linux,sml-size", NULL);
>>       basep = of_get_property(np, "linux,sml-base", NULL);
>>       if (sizep == NULL && basep == NULL)
>> -        return -ENODEV;
>> +        return tpm_read_log_memory_region(chip);
>>       if (sizep == NULL || basep == NULL)
>>           return -EIO;

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/2] tpm: Add reserved memory event log
  2023-01-18 14:49     ` Eddie James
@ 2023-01-18 15:01       ` Stefan Berger
  2023-01-21  3:58         ` Jarkko Sakkinen
  0 siblings, 1 reply; 11+ messages in thread
From: Stefan Berger @ 2023-01-18 15:01 UTC (permalink / raw)
  To: Eddie James, linux-integrity; +Cc: linux-kernel, jgg, jarkko, peterhuewe



On 1/18/23 09:49, Eddie James wrote:
> 
> On 1/18/23 07:27, Stefan Berger wrote:
>>
>>
>> On 1/13/23 11:10, Eddie James wrote:
>>> Some platforms may desire to pass the event log up to linux in the
>>
>> Which platforms are these that work like this?
> 
> 
> Platforms booting from U-Boot without EFI. So at the moment, IBM's OpenBMC systems hope to use this.

It may be worth to mention this in the patch description in case someone wonders which
systems would take that path.

    Stefan
> 
> Thanks,
> 
> Eddie
> 
> 
>>
>>    Stefan
>>
>>> form of a reserved memory region. Add support for this in the TPM
>>> core to find the reserved memory region and map it.
>>>
>>> Signed-off-by: Eddie James <eajames@linux.ibm.com>
>>> ---
>>>   drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
>>>   1 file changed, 37 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
>>> index 741ab2204b11..c815cadf00a4 100644
>>> --- a/drivers/char/tpm/eventlog/of.c
>>> +++ b/drivers/char/tpm/eventlog/of.c
>>> @@ -12,12 +12,48 @@
>>>     #include <linux/device.h>
>>>   #include <linux/slab.h>
>>> +#include <linux/io.h>
>>> +#include <linux/ioport.h>
>>>   #include <linux/of.h>
>>> +#include <linux/of_address.h>
>>> +#include <linux/of_reserved_mem.h>
>>>   #include <linux/tpm_eventlog.h>
>>>     #include "../tpm.h"
>>>   #include "common.h"
>>>   +static int tpm_read_log_memory_region(struct tpm_chip *chip)
>>> +{
>>> +    struct device_node *node;
>>> +    struct resource res;
>>> +    int rc;
>>> +
>>> +    node = of_parse_phandle(chip->dev.parent->of_node, "memory-region", 0);
>>> +    if (!node) {
>>> +        dev_info(&chip->dev, "no phandle\n");
>>> +        return -ENODEV;
>>> +    }
>>> +
>>> +    rc = of_address_to_resource(node, 0, &res);
>>> +    of_node_put(node);
>>> +    if (rc) {
>>> +        dev_info(&chip->dev, "no mem\n");
>>> +        return rc;
>>> +    }
>>> +
>>> +    chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
>>> +                         MEMREMAP_WB);
>>> +    if (!chip->log.bios_event_log) {
>>> +        dev_info(&chip->dev, "err memremap\n");
>>> +        return -ENOMEM;
>>> +    }
>>> +
>>> +    chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
>>> +
>>> +    return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
>>> +        EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
>>> +}
>>> +
>>>   int tpm_read_log_of(struct tpm_chip *chip)
>>>   {
>>>       struct device_node *np;
>>> @@ -39,7 +75,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>>>       sizep = of_get_property(np, "linux,sml-size", NULL);
>>>       basep = of_get_property(np, "linux,sml-base", NULL);
>>>       if (sizep == NULL && basep == NULL)
>>> -        return -ENODEV;
>>> +        return tpm_read_log_memory_region(chip);
>>>       if (sizep == NULL || basep == NULL)
>>>           return -EIO;

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] tpm: Use managed allocation for bios event log
  2023-01-13 16:10 ` [PATCH v2 1/2] tpm: Use managed allocation for bios " Eddie James
@ 2023-01-21  0:11   ` Jarkko Sakkinen
  2023-01-25 19:06     ` Eddie James
  0 siblings, 1 reply; 11+ messages in thread
From: Jarkko Sakkinen @ 2023-01-21  0:11 UTC (permalink / raw)
  To: Eddie James; +Cc: linux-integrity, linux-kernel, jgg, peterhuewe

On Fri, Jan 13, 2023 at 10:10:16AM -0600, Eddie James wrote:
> Since the bios event log is freed in the device release function,
> let devres handle the deallocation. This will allow other memory
> allocation/mapping functions to be used for the bios event log.
> 
> Signed-off-by: Eddie James <eajames@linux.ibm.com>
> ---
>  drivers/char/tpm/eventlog/acpi.c |  5 +++--
>  drivers/char/tpm/eventlog/efi.c  | 13 +++++++------
>  drivers/char/tpm/eventlog/of.c   |  3 ++-
>  drivers/char/tpm/tpm-chip.c      |  1 -
>  4 files changed, 12 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/char/tpm/eventlog/acpi.c b/drivers/char/tpm/eventlog/acpi.c
> index 0913d3eb8d51..40360e599bc3 100644
> --- a/drivers/char/tpm/eventlog/acpi.c
> +++ b/drivers/char/tpm/eventlog/acpi.c
> @@ -14,6 +14,7 @@
>   * Access to the event log extended by the TCG BIOS of PC platform
>   */
>  
> +#include <linux/device.h>
>  #include <linux/seq_file.h>
>  #include <linux/fs.h>
>  #include <linux/security.h>
> @@ -135,7 +136,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)
>  	}
>  
>  	/* malloc EventLog space */
> -	log->bios_event_log = kmalloc(len, GFP_KERNEL);
> +	log->bios_event_log = devm_kmalloc(&chip->dev, len, GFP_KERNEL);
>  	if (!log->bios_event_log)
>  		return -ENOMEM;
>  
> @@ -160,7 +161,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)
>  	return format;
>  
>  err:
> -	kfree(log->bios_event_log);
> +	devm_kfree(&chip->dev, log->bios_event_log);

I wonder do we want to do devm_kfree's at all as the memory is freed during
detach, i.e. taken care by devres.

>  	log->bios_event_log = NULL;
>  	return ret;
>  }
> diff --git a/drivers/char/tpm/eventlog/efi.c b/drivers/char/tpm/eventlog/efi.c
> index e6cb9d525e30..4e9d7c2bf32e 100644
> --- a/drivers/char/tpm/eventlog/efi.c
> +++ b/drivers/char/tpm/eventlog/efi.c
> @@ -6,6 +6,7 @@
>   *      Thiebaud Weksteen <tweek@google.com>
>   */
>  
> +#include <linux/device.h>
>  #include <linux/efi.h>
>  #include <linux/tpm_eventlog.h>
>  
> @@ -55,7 +56,7 @@ int tpm_read_log_efi(struct tpm_chip *chip)
>  	}
>  
>  	/* malloc EventLog space */
> -	log->bios_event_log = kmemdup(log_tbl->log, log_size, GFP_KERNEL);
> +	log->bios_event_log = devm_kmemdup(&chip->dev, log_tbl->log, log_size, GFP_KERNEL);
>  	if (!log->bios_event_log) {
>  		ret = -ENOMEM;
>  		goto out;
> @@ -76,7 +77,7 @@ int tpm_read_log_efi(struct tpm_chip *chip)
>  			     MEMREMAP_WB);
>  	if (!final_tbl) {
>  		pr_err("Could not map UEFI TPM final log\n");
> -		kfree(log->bios_event_log);
> +		devm_kfree(&chip->dev, log->bios_event_log);
>  		ret = -ENOMEM;
>  		goto out;
>  	}
> @@ -91,11 +92,11 @@ int tpm_read_log_efi(struct tpm_chip *chip)
>  	 * Allocate memory for the 'combined log' where we will append the
>  	 * 'final events log' to.
>  	 */
> -	tmp = krealloc(log->bios_event_log,
> -		       log_size + final_events_log_size,
> -		       GFP_KERNEL);
> +	tmp = devm_krealloc(&chip->dev, log->bios_event_log,
> +			    log_size + final_events_log_size,
> +			    GFP_KERNEL);
>  	if (!tmp) {
> -		kfree(log->bios_event_log);
> +		devm_kfree(&chip->dev, log->bios_event_log);
>  		ret = -ENOMEM;
>  		goto out;
>  	}
> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
> index a9ce66d09a75..741ab2204b11 100644
> --- a/drivers/char/tpm/eventlog/of.c
> +++ b/drivers/char/tpm/eventlog/of.c
> @@ -10,6 +10,7 @@
>   * Read the event log created by the firmware on PPC64
>   */
>  
> +#include <linux/device.h>
>  #include <linux/slab.h>
>  #include <linux/of.h>
>  #include <linux/tpm_eventlog.h>
> @@ -65,7 +66,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>  		return -EIO;
>  	}
>  
> -	log->bios_event_log = kmemdup(__va(base), size, GFP_KERNEL);
> +	log->bios_event_log = devm_kmemdup(&chip->dev, __va(base), size, GFP_KERNEL);
>  	if (!log->bios_event_log)
>  		return -ENOMEM;
>  
> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
> index 741d8f3e8fb3..b99f55f2d4fd 100644
> --- a/drivers/char/tpm/tpm-chip.c
> +++ b/drivers/char/tpm/tpm-chip.c
> @@ -267,7 +267,6 @@ static void tpm_dev_release(struct device *dev)
>  	idr_remove(&dev_nums_idr, chip->dev_num);
>  	mutex_unlock(&idr_lock);
>  
> -	kfree(chip->log.bios_event_log);
>  	kfree(chip->work_space.context_buf);
>  	kfree(chip->work_space.session_buf);
>  	kfree(chip->allocated_banks);
> -- 
> 2.31.1
> 

BR, Jarkko

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/2] tpm: Add reserved memory event log
  2023-01-13 16:10 ` [PATCH v2 2/2] tpm: Add reserved memory " Eddie James
  2023-01-18 13:27   ` Stefan Berger
@ 2023-01-21  0:13   ` Jarkko Sakkinen
  1 sibling, 0 replies; 11+ messages in thread
From: Jarkko Sakkinen @ 2023-01-21  0:13 UTC (permalink / raw)
  To: Eddie James; +Cc: linux-integrity, linux-kernel, jgg, peterhuewe

On Fri, Jan 13, 2023 at 10:10:17AM -0600, Eddie James wrote:
> Some platforms may desire to pass the event log up to linux in the
> form of a reserved memory region. Add support for this in the TPM
> core to find the reserved memory region and map it.
> 
> Signed-off-by: Eddie James <eajames@linux.ibm.com>
> ---
>  drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
>  1 file changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
> index 741ab2204b11..c815cadf00a4 100644
> --- a/drivers/char/tpm/eventlog/of.c
> +++ b/drivers/char/tpm/eventlog/of.c
> @@ -12,12 +12,48 @@
>  
>  #include <linux/device.h>
>  #include <linux/slab.h>
> +#include <linux/io.h>
> +#include <linux/ioport.h>
>  #include <linux/of.h>
> +#include <linux/of_address.h>
> +#include <linux/of_reserved_mem.h>
>  #include <linux/tpm_eventlog.h>
>  
>  #include "../tpm.h"
>  #include "common.h"
>  
> +static int tpm_read_log_memory_region(struct tpm_chip *chip)
> +{
> +	struct device_node *node;
> +	struct resource res;
> +	int rc;
> +
> +	node = of_parse_phandle(chip->dev.parent->of_node, "memory-region", 0);
> +	if (!node) {
> +		dev_info(&chip->dev, "no phandle\n");
> +		return -ENODEV;
> +	}
> +
> +	rc = of_address_to_resource(node, 0, &res);
> +	of_node_put(node);
> +	if (rc) {
> +		dev_info(&chip->dev, "no mem\n");
> +		return rc;
> +	}
> +
> +	chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
> +						 MEMREMAP_WB);
> +	if (!chip->log.bios_event_log) {
> +		dev_info(&chip->dev, "err memremap\n");
> +		return -ENOMEM;
> +	}
> +
> +	chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
> +
> +	return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
> +		EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
> +}
> +
>  int tpm_read_log_of(struct tpm_chip *chip)
>  {
>  	struct device_node *np;
> @@ -39,7 +75,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>  	sizep = of_get_property(np, "linux,sml-size", NULL);
>  	basep = of_get_property(np, "linux,sml-base", NULL);
>  	if (sizep == NULL && basep == NULL)
> -		return -ENODEV;
> +		return tpm_read_log_memory_region(chip);
>  	if (sizep == NULL || basep == NULL)
>  		return -EIO;
>  
> -- 
> 2.31.1
> 

I would CC this to linux-devicetree.

BR, Jarkko

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/2] tpm: Add reserved memory event log
  2023-01-18 15:01       ` Stefan Berger
@ 2023-01-21  3:58         ` Jarkko Sakkinen
  0 siblings, 0 replies; 11+ messages in thread
From: Jarkko Sakkinen @ 2023-01-21  3:58 UTC (permalink / raw)
  To: Stefan Berger; +Cc: Eddie James, linux-integrity, linux-kernel, jgg, peterhuewe

On Wed, Jan 18, 2023 at 10:01:16AM -0500, Stefan Berger wrote:
> 
> 
> On 1/18/23 09:49, Eddie James wrote:
> > 
> > On 1/18/23 07:27, Stefan Berger wrote:
> > > 
> > > 
> > > On 1/13/23 11:10, Eddie James wrote:
> > > > Some platforms may desire to pass the event log up to linux in the
> > > 
> > > Which platforms are these that work like this?
> > 
> > 
> > Platforms booting from U-Boot without EFI. So at the moment, IBM's OpenBMC systems hope to use this.
> 
> It may be worth to mention this in the patch description in case someone wonders which
> systems would take that path.

+1

BR, Jarkko

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] tpm: Use managed allocation for bios event log
  2023-01-21  0:11   ` Jarkko Sakkinen
@ 2023-01-25 19:06     ` Eddie James
  2023-01-26 17:31       ` Jarkko Sakkinen
  0 siblings, 1 reply; 11+ messages in thread
From: Eddie James @ 2023-01-25 19:06 UTC (permalink / raw)
  To: Jarkko Sakkinen; +Cc: linux-integrity, linux-kernel, jgg, peterhuewe


On 1/20/23 18:11, Jarkko Sakkinen wrote:
> On Fri, Jan 13, 2023 at 10:10:16AM -0600, Eddie James wrote:
>> Since the bios event log is freed in the device release function,
>> let devres handle the deallocation. This will allow other memory
>> allocation/mapping functions to be used for the bios event log.
>>
>> Signed-off-by: Eddie James <eajames@linux.ibm.com>
>> ---
>>   drivers/char/tpm/eventlog/acpi.c |  5 +++--
>>   drivers/char/tpm/eventlog/efi.c  | 13 +++++++------
>>   drivers/char/tpm/eventlog/of.c   |  3 ++-
>>   drivers/char/tpm/tpm-chip.c      |  1 -
>>   4 files changed, 12 insertions(+), 10 deletions(-)
>>
>> diff --git a/drivers/char/tpm/eventlog/acpi.c b/drivers/char/tpm/eventlog/acpi.c
>> index 0913d3eb8d51..40360e599bc3 100644
>> --- a/drivers/char/tpm/eventlog/acpi.c
>> +++ b/drivers/char/tpm/eventlog/acpi.c
>> @@ -14,6 +14,7 @@
>>    * Access to the event log extended by the TCG BIOS of PC platform
>>    */
>>   
>> +#include <linux/device.h>
>>   #include <linux/seq_file.h>
>>   #include <linux/fs.h>
>>   #include <linux/security.h>
>> @@ -135,7 +136,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)
>>   	}
>>   
>>   	/* malloc EventLog space */
>> -	log->bios_event_log = kmalloc(len, GFP_KERNEL);
>> +	log->bios_event_log = devm_kmalloc(&chip->dev, len, GFP_KERNEL);
>>   	if (!log->bios_event_log)
>>   		return -ENOMEM;
>>   
>> @@ -160,7 +161,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)
>>   	return format;
>>   
>>   err:
>> -	kfree(log->bios_event_log);
>> +	devm_kfree(&chip->dev, log->bios_event_log);
> I wonder do we want to do devm_kfree's at all as the memory is freed during
> detach, i.e. taken care by devres.


I think we should since the chip/tpm driver will continue to probe 
without the bios event log. Therefore that memory will be wasted if 
there is some error during bios log setup.


Thanks,

Eddie


>
>>   	log->bios_event_log = NULL;
>>   	return ret;
>>   }
>> diff --git a/drivers/char/tpm/eventlog/efi.c b/drivers/char/tpm/eventlog/efi.c
>> index e6cb9d525e30..4e9d7c2bf32e 100644
>> --- a/drivers/char/tpm/eventlog/efi.c
>> +++ b/drivers/char/tpm/eventlog/efi.c
>> @@ -6,6 +6,7 @@
>>    *      Thiebaud Weksteen <tweek@google.com>
>>    */
>>   
>> +#include <linux/device.h>
>>   #include <linux/efi.h>
>>   #include <linux/tpm_eventlog.h>
>>   
>> @@ -55,7 +56,7 @@ int tpm_read_log_efi(struct tpm_chip *chip)
>>   	}
>>   
>>   	/* malloc EventLog space */
>> -	log->bios_event_log = kmemdup(log_tbl->log, log_size, GFP_KERNEL);
>> +	log->bios_event_log = devm_kmemdup(&chip->dev, log_tbl->log, log_size, GFP_KERNEL);
>>   	if (!log->bios_event_log) {
>>   		ret = -ENOMEM;
>>   		goto out;
>> @@ -76,7 +77,7 @@ int tpm_read_log_efi(struct tpm_chip *chip)
>>   			     MEMREMAP_WB);
>>   	if (!final_tbl) {
>>   		pr_err("Could not map UEFI TPM final log\n");
>> -		kfree(log->bios_event_log);
>> +		devm_kfree(&chip->dev, log->bios_event_log);
>>   		ret = -ENOMEM;
>>   		goto out;
>>   	}
>> @@ -91,11 +92,11 @@ int tpm_read_log_efi(struct tpm_chip *chip)
>>   	 * Allocate memory for the 'combined log' where we will append the
>>   	 * 'final events log' to.
>>   	 */
>> -	tmp = krealloc(log->bios_event_log,
>> -		       log_size + final_events_log_size,
>> -		       GFP_KERNEL);
>> +	tmp = devm_krealloc(&chip->dev, log->bios_event_log,
>> +			    log_size + final_events_log_size,
>> +			    GFP_KERNEL);
>>   	if (!tmp) {
>> -		kfree(log->bios_event_log);
>> +		devm_kfree(&chip->dev, log->bios_event_log);
>>   		ret = -ENOMEM;
>>   		goto out;
>>   	}
>> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
>> index a9ce66d09a75..741ab2204b11 100644
>> --- a/drivers/char/tpm/eventlog/of.c
>> +++ b/drivers/char/tpm/eventlog/of.c
>> @@ -10,6 +10,7 @@
>>    * Read the event log created by the firmware on PPC64
>>    */
>>   
>> +#include <linux/device.h>
>>   #include <linux/slab.h>
>>   #include <linux/of.h>
>>   #include <linux/tpm_eventlog.h>
>> @@ -65,7 +66,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>>   		return -EIO;
>>   	}
>>   
>> -	log->bios_event_log = kmemdup(__va(base), size, GFP_KERNEL);
>> +	log->bios_event_log = devm_kmemdup(&chip->dev, __va(base), size, GFP_KERNEL);
>>   	if (!log->bios_event_log)
>>   		return -ENOMEM;
>>   
>> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
>> index 741d8f3e8fb3..b99f55f2d4fd 100644
>> --- a/drivers/char/tpm/tpm-chip.c
>> +++ b/drivers/char/tpm/tpm-chip.c
>> @@ -267,7 +267,6 @@ static void tpm_dev_release(struct device *dev)
>>   	idr_remove(&dev_nums_idr, chip->dev_num);
>>   	mutex_unlock(&idr_lock);
>>   
>> -	kfree(chip->log.bios_event_log);
>>   	kfree(chip->work_space.context_buf);
>>   	kfree(chip->work_space.session_buf);
>>   	kfree(chip->allocated_banks);
>> -- 
>> 2.31.1
>>
> BR, Jarkko

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] tpm: Use managed allocation for bios event log
  2023-01-25 19:06     ` Eddie James
@ 2023-01-26 17:31       ` Jarkko Sakkinen
  0 siblings, 0 replies; 11+ messages in thread
From: Jarkko Sakkinen @ 2023-01-26 17:31 UTC (permalink / raw)
  To: Eddie James; +Cc: linux-integrity, linux-kernel, jgg, peterhuewe

On Wed, Jan 25, 2023 at 01:06:31PM -0600, Eddie James wrote:
> 
> On 1/20/23 18:11, Jarkko Sakkinen wrote:
> > On Fri, Jan 13, 2023 at 10:10:16AM -0600, Eddie James wrote:
> > > Since the bios event log is freed in the device release function,
> > > let devres handle the deallocation. This will allow other memory
> > > allocation/mapping functions to be used for the bios event log.
> > > 
> > > Signed-off-by: Eddie James <eajames@linux.ibm.com>
> > > ---
> > >   drivers/char/tpm/eventlog/acpi.c |  5 +++--
> > >   drivers/char/tpm/eventlog/efi.c  | 13 +++++++------
> > >   drivers/char/tpm/eventlog/of.c   |  3 ++-
> > >   drivers/char/tpm/tpm-chip.c      |  1 -
> > >   4 files changed, 12 insertions(+), 10 deletions(-)
> > > 
> > > diff --git a/drivers/char/tpm/eventlog/acpi.c b/drivers/char/tpm/eventlog/acpi.c
> > > index 0913d3eb8d51..40360e599bc3 100644
> > > --- a/drivers/char/tpm/eventlog/acpi.c
> > > +++ b/drivers/char/tpm/eventlog/acpi.c
> > > @@ -14,6 +14,7 @@
> > >    * Access to the event log extended by the TCG BIOS of PC platform
> > >    */
> > > +#include <linux/device.h>
> > >   #include <linux/seq_file.h>
> > >   #include <linux/fs.h>
> > >   #include <linux/security.h>
> > > @@ -135,7 +136,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)
> > >   	}
> > >   	/* malloc EventLog space */
> > > -	log->bios_event_log = kmalloc(len, GFP_KERNEL);
> > > +	log->bios_event_log = devm_kmalloc(&chip->dev, len, GFP_KERNEL);
> > >   	if (!log->bios_event_log)
> > >   		return -ENOMEM;
> > > @@ -160,7 +161,7 @@ int tpm_read_log_acpi(struct tpm_chip *chip)
> > >   	return format;
> > >   err:
> > > -	kfree(log->bios_event_log);
> > > +	devm_kfree(&chip->dev, log->bios_event_log);
> > I wonder do we want to do devm_kfree's at all as the memory is freed during
> > detach, i.e. taken care by devres.
> 
> 
> I think we should since the chip/tpm driver will continue to probe without
> the bios event log. Therefore that memory will be wasted if there is some
> error during bios log setup.

OK, I buy this!

For this patch:

Tested-by: Jarkko Sakkinen <jarkko@kernel.org>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

If you could refine the description for the 2nd, then these would be ready
to be picked.

> Thanks,
> 
> Eddie

BR, Jarkko

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-01-26 17:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-13 16:10 [PATCH v2 0/2] tpm: Add reserved memory event log Eddie James
2023-01-13 16:10 ` [PATCH v2 1/2] tpm: Use managed allocation for bios " Eddie James
2023-01-21  0:11   ` Jarkko Sakkinen
2023-01-25 19:06     ` Eddie James
2023-01-26 17:31       ` Jarkko Sakkinen
2023-01-13 16:10 ` [PATCH v2 2/2] tpm: Add reserved memory " Eddie James
2023-01-18 13:27   ` Stefan Berger
2023-01-18 14:49     ` Eddie James
2023-01-18 15:01       ` Stefan Berger
2023-01-21  3:58         ` Jarkko Sakkinen
2023-01-21  0:13   ` Jarkko Sakkinen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.