Skip to contentSkip to conversation

tpm: of: avoid __va() translation for event log address

PATCH In review Ard Biesheuvel 4 participants 10 messages Sep 22, 2020 – Sep 28, 2020

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

PATCH 1/1 tpm: of: avoid __va() translation for event log address

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

So instead, use memremap(), which will reuse the linear mapping if it is valid, or create another mapping otherwise.

Cc Peter Huewe <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
---
 drivers/char/tpm/eventlog/of.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/char/tpm/eventlog/of.c +7 -1
index a9ce66d09a75..9178547589a3 100644
@@ -11,6 +11,7 @@
  */
 
 #include <linux/slab.h>
+#include <linux/io.h>
 #include <linux/of.h>
 #include <linux/tpm_eventlog.h>
 
@@ -25,6 +26,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
 	struct tpm_bios_log *log;
 	u32 size;
 	u64 base;
+	void *p;
 
 	log = &chip->log;
 	if (chip->dev.parent && chip->dev.parent->of_node)
@@ -65,7 +67,11 @@ int tpm_read_log_of(struct tpm_chip *chip)
 		return -EIO;
 	}
 
-	log->bios_event_log = kmemdup(__va(base), size, GFP_KERNEL);
+	p = memremap(base, size, MEMREMAP_WB);
+	if (!p)
+		return -ENOMEM;
+	log->bios_event_log = kmemdup(p, size, GFP_KERNEL);
+	memunmap(p);
 	if (!log->bios_event_log)
 		return -ENOMEM;
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 9 lines · Ard Biesheuvel

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

Makes perfect sense to the level that I wonder if this should have a fixes tag and/or needs to be backported to the stable kernels?

Quoted 24 lines

So instead, use memremap(), which will reuse the linear mapping if it is valid, or create another mapping otherwise.

Cc Peter Huewe <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
---
 drivers/char/tpm/eventlog/of.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/char/tpm/eventlog/of.c +1 -0
index a9ce66d09a75..9178547589a3 100644
@@ -11,6 +11,7 @@
  */
 
 #include <linux/slab.h>
+#include <linux/io.h>
 #include <linux/of.h>
 #include <linux/tpm_eventlog.h>
 

drivers/char/tpm/eventlog/of.c int tpm_read_log_of(struct tpm_chip *chip) · line 29

Quoted 4 lines
drivers/char/tpm/eventlog/of.c +1 -0
@@ -25,6 +26,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
 	struct tpm_bios_log *log;
 	u32 size;
 	u64 base;
+	void *p;

I'd just use 'ptr' for readability sake.

Quoted 18 lines
drivers/char/tpm/eventlog/of.c +5 -1
 	log = &chip->log;
 	if (chip->dev.parent && chip->dev.parent->of_node)
@@ -65,7 +67,11 @@ int tpm_read_log_of(struct tpm_chip *chip)
 		return -EIO;
 	}
 
-	log->bios_event_log = kmemdup(__va(base), size, GFP_KERNEL);
+	p = memremap(base, size, MEMREMAP_WB);
+	if (!p)
+		return -ENOMEM;
+	log->bios_event_log = kmemdup(p, size, GFP_KERNEL);
+	memunmap(p);
 	if (!log->bios_event_log)
 		return -ENOMEM;
 
Signature · 1 line
2.17.1

This is a really great catch!

I'm a bit late of my PR a bit because of SGX upstreaming madness (sending v39 soon). If you can answer to my question above, I can do that nitpick change to patch and get it to my v5.10 PR.

PS. Just so that you know, once I've applied it, it will be available here:

git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd.git

I'll include MAINTAINERS update to that PR.

/Jarkko

Reviewed-by
#2 lore raw
Quoted 70 lines · Jarkko Sakkinen
Quoted 9 lines · Ard Biesheuvel

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

Makes perfect sense to the level that I wonder if this should have a fixes tag and/or needs to be backported to the stable kernels?

Quoted 24 lines

So instead, use memremap(), which will reuse the linear mapping if it is valid, or create another mapping otherwise.

Cc Peter Huewe <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
---
 drivers/char/tpm/eventlog/of.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/char/tpm/eventlog/of.c +1 -0
index a9ce66d09a75..9178547589a3 100644
@@ -11,6 +11,7 @@
  */
 
 #include <linux/slab.h>
+#include <linux/io.h>
 #include <linux/of.h>
 #include <linux/tpm_eventlog.h>
 

drivers/char/tpm/eventlog/of.c int tpm_read_log_of(struct tpm_chip *chip) · line 29

Quoted 4 lines
drivers/char/tpm/eventlog/of.c +1 -0
@@ -25,6 +26,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
 	struct tpm_bios_log *log;
 	u32 size;
 	u64 base;
+	void *p;

I'd just use 'ptr' for readability sake.

Quoted 18 lines
drivers/char/tpm/eventlog/of.c +5 -1
 	log = &chip->log;
 	if (chip->dev.parent && chip->dev.parent->of_node)
@@ -65,7 +67,11 @@ int tpm_read_log_of(struct tpm_chip *chip)
 		return -EIO;
 	}
 
-	log->bios_event_log = kmemdup(__va(base), size, GFP_KERNEL);
+	p = memremap(base, size, MEMREMAP_WB);
+	if (!p)
+		return -ENOMEM;
+	log->bios_event_log = kmemdup(p, size, GFP_KERNEL);
+	memunmap(p);
 	if (!log->bios_event_log)
 		return -ENOMEM;
 
Signature · 1 line
2.17.1

This is a really great catch!

I'm a bit late of my PR a bit because of SGX upstreaming madness (sending v39 soon). If you can answer to my question above, I can do that nitpick change to patch and get it to my v5.10 PR.

Quoted 6 lines

PS. Just so that you know, once I've applied it, it will be available here:

git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd.git

I'll include MAINTAINERS update to that PR.

Forgot this:

/Jarkko

#3 lore raw
In reply to Jarkko Sakkinen
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ard Biesheuvel

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Quoted 9 lines
Quoted 5 lines

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

Makes perfect sense to the level that I wonder if this should have a fixes tag and/or needs to be backported to the stable kernels?

AIUI, the code was written specifically for ppc64, which is a non-highmem, non-EFI architecture. However, when we start reusing this driver for ARM, this issue could pop up.

The code itself has been refactored a couple of times, so I think it will require different versions of the patch for different generations of stable kernels.

So perhaps just add Cc: <[email protected]>, and wait and see how far back it applies cleanly?

Quoted 23 lines
Quoted 23 lines

So instead, use memremap(), which will reuse the linear mapping if it is valid, or create another mapping otherwise.

Cc Peter Huewe <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
---
 drivers/char/tpm/eventlog/of.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/char/tpm/eventlog/of.c +1 -0
index a9ce66d09a75..9178547589a3 100644
@@ -11,6 +11,7 @@
  */
 
 #include <linux/slab.h>
+#include <linux/io.h>
 #include <linux/of.h>
 #include <linux/tpm_eventlog.h>
 
Quoted 8 lines

drivers/char/tpm/eventlog/of.c int tpm_read_log_of(struct tpm_chip *chip) · line 29

Quoted 5 lines
drivers/char/tpm/eventlog/of.c +1 -0
@@ -25,6 +26,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
      struct tpm_bios_log *log;
      u32 size;
      u64 base;
+     void *p;

I'd just use 'ptr' for readability sake.

If you prefer

Quoted 21 lines
Quoted 16 lines
drivers/char/tpm/eventlog/of.c +5 -2
      log = &chip->log;
      if (chip->dev.parent && chip->dev.parent->of_node)
@@ -65,7 +67,11 @@ int tpm_read_log_of(struct tpm_chip *chip)
              return -EIO;
      }
 
-     log->bios_event_log = kmemdup(__va(base), size, GFP_KERNEL);
+     p = memremap(base, size, MEMREMAP_WB);
+     if (!p)
+             return -ENOMEM;
+     log->bios_event_log = kmemdup(p, size, GFP_KERNEL);
+     memunmap(p);
      if (!log->bios_event_log)
              return -ENOMEM;
 
--
Quoted 2 lines

2.17.1

This is a really great catch!

Quoted 4 lines

I'm a bit late of my PR a bit because of SGX upstreaming madness (sending v39 soon). If you can answer to my question above, I can do that nitpick change to patch and get it to my v5.10 PR.

Yes, please.

Quoted 8 lines

PS. Just so that you know, once I've applied it, it will be available here:

git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd.git

I'll include MAINTAINERS update to that PR.

/Jarkko

#4 lore raw
Quoted 22 lines · Ard Biesheuvel
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ard Biesheuvel

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Quoted 9 lines
Quoted 5 lines

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

Makes perfect sense to the level that I wonder if this should have a fixes tag and/or needs to be backported to the stable kernels?

AIUI, the code was written specifically for ppc64, which is a non-highmem, non-EFI architecture. However, when we start reusing this driver for ARM, this issue could pop up.

Quoted 6 lines

The code itself has been refactored a couple of times, so I think it will require different versions of the patch for different generations of stable kernels.

So perhaps just add Cc: <[email protected]>, and wait and see how far back it applies cleanly?

Yeah, I think I'll cc it with some note before the diffstat.

I'm thinking to cap it to only 5.x kernels (at least first) unless it is dead easy to backport below that.

Quoted 8 lines
Quoted 6 lines

This is a really great catch!

I'm a bit late of my PR a bit because of SGX upstreaming madness (sending v39 soon). If you can answer to my question above, I can do that nitpick change to patch and get it to my v5.10 PR.

Yes, please.

Great, will do, thanks again for fixing this issue!

/Jarkko

#5 lore raw
Quoted 27 lines · Jarkko Sakkinen
Quoted 26 lines · Ard Biesheuvel
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ard Biesheuvel

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Quoted 9 lines
Quoted 5 lines

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

Makes perfect sense to the level that I wonder if this should have a fixes tag and/or needs to be backported to the stable kernels?

AIUI, the code was written specifically for ppc64, which is a non-highmem, non-EFI architecture. However, when we start reusing this driver for ARM, this issue could pop up.

The code itself has been refactored a couple of times, so I think it will require different versions of the patch for different generations of stable kernels.

Quoted 7 lines
Quoted 2 lines

So perhaps just add Cc: <[email protected]>, and wait and see how far back it applies cleanly?

Yeah, I think I'll cc it with some note before the diffstat.

I'm thinking to cap it to only 5.x kernels (at least first) unless it is dead easy to backport below that.

I have this vauge recollection of pointing at this before and being told that it had to be __va for some PPC reason?

Do check with the PPC people first, I see none on the CC list.

Jason

#6 lore raw
Quoted 36 lines · Jason Gunthorpe
Quoted 27 lines · Jarkko Sakkinen
Quoted 26 lines · Ard Biesheuvel
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ard Biesheuvel

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Quoted 9 lines
Quoted 5 lines

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

Makes perfect sense to the level that I wonder if this should have a fixes tag and/or needs to be backported to the stable kernels?

AIUI, the code was written specifically for ppc64, which is a non-highmem, non-EFI architecture. However, when we start reusing this driver for ARM, this issue could pop up.

The code itself has been refactored a couple of times, so I think it will require different versions of the patch for different generations of stable kernels.

Quoted 7 lines
Quoted 2 lines

So perhaps just add Cc: <[email protected]>, and wait and see how far back it applies cleanly?

Yeah, I think I'll cc it with some note before the diffstat.

I'm thinking to cap it to only 5.x kernels (at least first) unless it is dead easy to backport below that.

Quoted 6 lines

I have this vauge recollection of pointing at this before and being told that it had to be __va for some PPC reason?

Do check with the PPC people first, I see none on the CC list.

Jason

Thanks, added arch/powerpc maintainers.

/Jarkko

#7 lore raw

Le 28/09/2020 à 01:44, Jarkko Sakkinen a écrit :

Quoted 37 lines · Jason Gunthorpe
Quoted 36 lines · Jarkko Sakkinen
Quoted 34 lines · Ard Biesheuvel
Quoted 22 lines
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ard Biesheuvel

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Quoted 9 lines
Quoted 5 lines

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

Makes perfect sense to the level that I wonder if this should have a fixes tag and/or needs to be backported to the stable kernels?

AIUI, the code was written specifically for ppc64, which is a non-highmem, non-EFI architecture. However, when we start reusing this driver for ARM, this issue could pop up.

Quoted 6 lines

The code itself has been refactored a couple of times, so I think it will require different versions of the patch for different generations of stable kernels.

So perhaps just add Cc: <[email protected]>, and wait and see how far back it applies cleanly?

Yeah, I think I'll cc it with some note before the diffstat.

I'm thinking to cap it to only 5.x kernels (at least first) unless it is dead easy to backport below that.

Quoted 9 lines
Quoted 6 lines

I have this vauge recollection of pointing at this before and being told that it had to be __va for some PPC reason?

Do check with the PPC people first, I see none on the CC list.

Jason

Thanks, added arch/powerpc maintainers.

As far as I can see, memremap() won't work on PPC32 at least:

IIUC, memremap() calls arch_memremap_wb() arch_memremap_wb() calls ioremap_cache()
In case of failure, then ioremap_wt() and ioremap_wc() are tried.

All ioremap calls end up in __ioremap_caller() which will return NULL in case you try to ioremap RAM.

So the statement "So instead, use memremap(), which will reuse the linear mapping if it is valid, or create another mapping otherwise." seems to be wrong, at least for PPC32.

Even for PPC64 which doesn't seem to have the RAM check, I can't see that it will "reuse the linear mapping".

Christophe

#8 lore raw
Quoted 57 lines · Christophe Leroy
Quoted 37 lines · Jarkko Sakkinen
Quoted 36 lines · Jason Gunthorpe
Quoted 34 lines · Jarkko Sakkinen
Quoted 22 lines · Ard Biesheuvel
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ard Biesheuvel

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Quoted 9 lines
Quoted 5 lines

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

Makes perfect sense to the level that I wonder if this should have a fixes tag and/or needs to be backported to the stable kernels?

AIUI, the code was written specifically for ppc64, which is a non-highmem, non-EFI architecture. However, when we start reusing this driver for ARM, this issue could pop up.

Quoted 6 lines

The code itself has been refactored a couple of times, so I think it will require different versions of the patch for different generations of stable kernels.

So perhaps just add Cc: <[email protected]>, and wait and see how far back it applies cleanly?

Yeah, I think I'll cc it with some note before the diffstat.

I'm thinking to cap it to only 5.x kernels (at least first) unless it is dead easy to backport below that.

Quoted 9 lines
Quoted 6 lines

I have this vauge recollection of pointing at this before and being told that it had to be __va for some PPC reason?

Do check with the PPC people first, I see none on the CC list.

Jason

Thanks, added arch/powerpc maintainers.

As far as I can see, memremap() won't work on PPC32 at least:

IIUC, memremap() calls arch_memremap_wb() arch_memremap_wb() calls ioremap_cache()
In case of failure, then ioremap_wt() and ioremap_wc() are tried.

Quoted 8 lines

All ioremap calls end up in __ioremap_caller() which will return NULL in case you try to ioremap RAM.

So the statement "So instead, use memremap(), which will reuse the linear mapping if it is valid, or create another mapping otherwise." seems to be wrong, at least for PPC32.

Even for PPC64 which doesn't seem to have the RAM check, I can't see that it will "reuse the linear mapping".

It is there, please look again. Before any of the above happens, memremap() will call try_ram_remap() for regions that are covered by a IORESOURCE_SYSTEM_RAM, and map it using __va() if its PFN is valid and it is not highmem.

So as far as I can tell, this change has no effect on PPC at all unless its RAM is not described as IORESOURCE_SYSTEM_RAM.

#9 lore raw
Quoted 68 lines · Ard Biesheuvel
Quoted 57 lines · Christophe Leroy
Quoted 37 lines · Jarkko Sakkinen
Quoted 36 lines · Jason Gunthorpe
Quoted 34 lines · Jarkko Sakkinen
Quoted 22 lines · Ard Biesheuvel
Quoted 6 lines · Jarkko Sakkinen
Quoted 4 lines · Ard Biesheuvel

The TPM event log is provided to the OS by the firmware, by loading it into an area in memory and passing the physical address via a node in the device tree.

Quoted 9 lines
Quoted 5 lines

Currently, we use __va() to access the memory via the kernel's linear
map: however, it is not guaranteed that the linear map covers this particular address, as we may be running under HIGHMEM on a 32-bit architecture, or running firmware that uses a memory type for the event log that is omitted from the linear map (such as EfiReserved).

Makes perfect sense to the level that I wonder if this should have a fixes tag and/or needs to be backported to the stable kernels?

AIUI, the code was written specifically for ppc64, which is a non-highmem, non-EFI architecture. However, when we start reusing this driver for ARM, this issue could pop up.

Quoted 6 lines

The code itself has been refactored a couple of times, so I think it will require different versions of the patch for different generations of stable kernels.

So perhaps just add Cc: <[email protected]>, and wait and see how far back it applies cleanly?

Yeah, I think I'll cc it with some note before the diffstat.

I'm thinking to cap it to only 5.x kernels (at least first) unless it is dead easy to backport below that.

Quoted 9 lines
Quoted 6 lines

I have this vauge recollection of pointing at this before and being told that it had to be __va for some PPC reason?

Do check with the PPC people first, I see none on the CC list.

Jason

Thanks, added arch/powerpc maintainers.

As far as I can see, memremap() won't work on PPC32 at least:

IIUC, memremap() calls arch_memremap_wb() arch_memremap_wb() calls ioremap_cache()
In case of failure, then ioremap_wt() and ioremap_wc() are tried.

Quoted 8 lines

All ioremap calls end up in __ioremap_caller() which will return NULL in case you try to ioremap RAM.

So the statement "So instead, use memremap(), which will reuse the linear mapping if it is valid, or create another mapping otherwise." seems to be wrong, at least for PPC32.

Even for PPC64 which doesn't seem to have the RAM check, I can't see that it will "reuse the linear mapping".

Quoted 7 lines

It is there, please look again. Before any of the above happens, memremap() will call try_ram_remap() for regions that are covered by a IORESOURCE_SYSTEM_RAM, and map it using __va() if its PFN is valid and it is not highmem.

So as far as I can tell, this change has no effect on PPC at all unless its RAM is not described as IORESOURCE_SYSTEM_RAM.

Any chance for someone to test this on PPC32?

/Jarkko