Skip to contentSkip to conversation

firmware: coreboot: Fix probe and simplify code

PATCH Superseded Stephen Boyd 3 participants 26 messages Aug 9, 2018 – Aug 10, 2018

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover firmware: coreboot: Fix probe and simplify code

This series reworks the coreboot firmware driver a bit to fix some bugs and then simplify the code by changing the design to get rid of the different platform drivers, remap memory with memremap(), and use devm to simplify error paths. There's some risk in changing to memremap() but I think that should work out alright. It will either return memory directly, or fallback to ioremap_cache() as has been done before.

Changes from v2:
* A bunch more patches
* Fix iounmap missing on error path
* Fix bus getting unregistered on driver unbind and never registered again
* Request exclusive access to table region
* Pull out table signature check to driver probe

Changes from v1:
* Split out fixlet for DT based driver from platform driver change

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>

Stephen Boyd (7):
firmware: coreboot: Let OF core populate platform device
firmware: coreboot: Unmap ioregion on failure
firmware: coreboot: Make bus registration symmetric
firmware: coreboot: Collapse platform drivers into bus core
firmware: coreboot: Remap RAM with memremap() instead of ioremap()
firmware: coreboot: Only populate devices in coreboot_table_init()
firmware: coreboot: Request table region for exclusive access

 drivers/firmware/google/Kconfig               |  26 ++--
 drivers/firmware/google/Makefile              |   2 -
 drivers/firmware/google/coreboot_table-acpi.c |  88 ------------
 drivers/firmware/google/coreboot_table-of.c   |  82 -----------
 drivers/firmware/google/coreboot_table.c      | 135 +++++++++++++-----
 drivers/firmware/google/coreboot_table.h      |   6 -
 6 files changed, 107 insertions(+), 232 deletions(-)
 delete mode 100644 drivers/firmware/google/coreboot_table-acpi.c
 delete mode 100644 drivers/firmware/google/coreboot_table-of.c
Signature · 1 line
Sent by a computer through tubes
#1 lore raw

PATCH v3 1/7 firmware: coreboot: Let OF core populate platform device

Now that the /firmware/coreboot node in DT is populated by the core DT platform code with commit 3aa0582fdb82 ("of: platform: populate /firmware/ node from of_platform_default_populate_init()") we should and can remove the platform device creation here. Otherwise, the of_platform_device_create() call will fail, the coreboot of driver won't be registered, and this driver will never bind. At the same time, we should move this driver to use MODULE_DEVICE_TABLE so that module auto-load works properly when the coreboot device is auto-populated and we should drop the of_node handling that was presumably placed here to hold a reference to the DT node created during module init that no longer happens.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
Fixes 3aa0582fdb82 ("of: platform: populate /firmware/ node from of_platform_default_populate_init()")
---
 drivers/firmware/google/coreboot_table-of.c | 28 +++------------------
 1 file changed, 3 insertions(+), 25 deletions(-)
drivers/firmware/google/coreboot_table-of.c +3 -25
index f15bf404c579..9b90c0fa4a0b 100644
@@ -19,7 +19,6 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
-#include <linux/of_platform.h>
 #include <linux/platform_device.h>
 
 #include "coreboot_table.h"
@@ -30,7 +29,6 @@ static int coreboot_table_of_probe(struct platform_device *pdev)
 	void __iomem *ptr;
 
 	ptr = of_iomap(fw_dn, 0);
-	of_node_put(fw_dn);
 	if (!ptr)
 		return -ENOMEM;
 
@@ -44,8 +42,9 @@ static int coreboot_table_of_remove(struct platform_device *pdev)
 
 static const struct of_device_id coreboot_of_match[] = {
 	{ .compatible = "coreboot" },
-	{},
+	{}
 };
+MODULE_DEVICE_TABLE(of, coreboot_of_match);
 
 static struct platform_driver coreboot_table_of_driver = {
 	.probe = coreboot_table_of_probe,
@@ -55,28 +54,7 @@ static struct platform_driver coreboot_table_of_driver = {
 		.of_match_table = coreboot_of_match,
 	},
 };
-
-static int __init platform_coreboot_table_of_init(void)
-{
-	struct platform_device *pdev;
-	struct device_node *of_node;
-
-	/* Limit device creation to the presence of /firmware/coreboot node */
-	of_node = of_find_node_by_path("/firmware/coreboot");
-	if (!of_node)
-		return -ENODEV;
-
-	if (!of_match_node(coreboot_of_match, of_node))
-		return -ENODEV;
-
-	pdev = of_platform_device_create(of_node, "coreboot_table_of", NULL);
-	if (!pdev)
-		return -ENODEV;
-
-	return platform_driver_register(&coreboot_table_of_driver);
-}
-
-module_init(platform_coreboot_table_of_init);
+module_platform_driver(coreboot_table_of_driver);
 
 MODULE_AUTHOR("Google, Inc.");
 MODULE_LICENSE("GPL");
Signature · 1 line
Sent by a computer through tubes
Reviewed-by on v3 1/7
#2 lore raw

Re: [PATCH v3 1/7] firmware: coreboot: Let OF core populate platform device

Hi,

Quoted 11 lines · Stephen Boyd

Now that the /firmware/coreboot node in DT is populated by the core DT platform code with commit 3aa0582fdb82 ("of: platform: populate /firmware/ node from of_platform_default_populate_init()") we should and can remove the platform device creation here. Otherwise, the of_platform_device_create() call will fail, the coreboot of driver won't be registered, and this driver will never bind. At the same time, we should move this driver to use MODULE_DEVICE_TABLE so that module auto-load works properly when the coreboot device is auto-populated and we should drop the of_node handling that was presumably placed here to hold a reference to the DT node created during module init that no longer happens.

Yep, the of_node_put() was to complement of_find_compatible_node() which was originally done in probe(), and then of_find_node_by_path() in __init when it was upstreamed, and now it's no longer needed.

Quoted 11 lines
Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
Fixes 3aa0582fdb82 ("of: platform: populate /firmware/ node from of_platform_default_populate_init()")
---
 drivers/firmware/google/coreboot_table-of.c | 28 +++------------------
 1 file changed, 3 insertions(+), 25 deletions(-)

Change looks good:

Thanks!

Now to go figure out how a simple bugfix managed to blow up into a 7-patch series... If this goes on any longer, patch 1 should probably go by itself, to fix the regression.

Brian

Quoted 64 lines
drivers/firmware/google/coreboot_table-of.c +3 -25
index f15bf404c579..9b90c0fa4a0b 100644
@@ -19,7 +19,6 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
-#include <linux/of_platform.h>
 #include <linux/platform_device.h>
 
 #include "coreboot_table.h"
@@ -30,7 +29,6 @@ static int coreboot_table_of_probe(struct platform_device *pdev)
 	void __iomem *ptr;
 
 	ptr = of_iomap(fw_dn, 0);
-	of_node_put(fw_dn);
 	if (!ptr)
 		return -ENOMEM;
 
@@ -44,8 +42,9 @@ static int coreboot_table_of_remove(struct platform_device *pdev)
 
 static const struct of_device_id coreboot_of_match[] = {
 	{ .compatible = "coreboot" },
-	{},
+	{}
 };
+MODULE_DEVICE_TABLE(of, coreboot_of_match);
 
 static struct platform_driver coreboot_table_of_driver = {
 	.probe = coreboot_table_of_probe,
@@ -55,28 +54,7 @@ static struct platform_driver coreboot_table_of_driver = {
 		.of_match_table = coreboot_of_match,
 	},
 };
-
-static int __init platform_coreboot_table_of_init(void)
-{
-	struct platform_device *pdev;
-	struct device_node *of_node;
-
-	/* Limit device creation to the presence of /firmware/coreboot node */
-	of_node = of_find_node_by_path("/firmware/coreboot");
-	if (!of_node)
-		return -ENODEV;
-
-	if (!of_match_node(coreboot_of_match, of_node))
-		return -ENODEV;
-
-	pdev = of_platform_device_create(of_node, "coreboot_table_of", NULL);
-	if (!pdev)
-		return -ENODEV;
-
-	return platform_driver_register(&coreboot_table_of_driver);
-}
-
-module_init(platform_coreboot_table_of_init);
+module_platform_driver(coreboot_table_of_driver);
 
 MODULE_AUTHOR("Google, Inc.");
 MODULE_LICENSE("GPL");
Signature · 1 line
Sent by a computer through tubes
#3 lore raw

PATCH v3 2/7 firmware: coreboot: Unmap ioregion on failure

In reply to Stephen Boyd

Both callers of coreboot_table_init() ioremap the pointer that comes in but they don't unmap the memory on failure. Both of them also fail probe immediately with the return value of coreboot_table_init(), leaking a mapping when it fails. Plug the leak so the mapping isn't left unused.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
Fixes 570d30c2823f ("firmware: coreboot: Expose the coreboot table as a bus")
---
 drivers/firmware/google/coreboot_table.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/firmware/google/coreboot_table.c +3 -0
index 19db5709ae28..0d3e140444ae 100644
@@ -138,6 +138,9 @@ int coreboot_table_init(struct device *dev, void __iomem *ptr)
 		ptr_entry += entry.size;
 	}
 
+	if (ret)
+		iounmap(ptr);
+
 	return ret;
 }
 EXPORT_SYMBOL(coreboot_table_init);
Signature · 1 line
Sent by a computer through tubes
Reviewed-by on v3 2/7
#4 lore raw

Re: [PATCH v3 2/7] firmware: coreboot: Unmap ioregion on failure

Quoted 10 lines · Stephen Boyd

Both callers of coreboot_table_init() ioremap the pointer that comes in but they don't unmap the memory on failure. Both of them also fail probe immediately with the return value of coreboot_table_init(), leaking a mapping when it fails. Plug the leak so the mapping isn't left unused.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
Fixes 570d30c2823f ("firmware: coreboot: Expose the coreboot table as a bus")

I suppose this is fair, since that commit introduced error paths and didn't clean them up. But one warning below:

Quoted 10 lines
---
 drivers/firmware/google/coreboot_table.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/firmware/google/coreboot_table.c +0 -0
index 19db5709ae28..0d3e140444ae 100644

drivers/firmware/google/coreboot_table.c int coreboot_table_init(struct device *dev, void __iomem *ptr) · line 142

Quoted 5 lines
drivers/firmware/google/coreboot_table.c +2 -0
@@ -138,6 +138,9 @@ int coreboot_table_init(struct device *dev, void __iomem *ptr)
 		ptr_entry += entry.size;
 	}
 
+	if (ret)
+		iounmap(ptr);

This works because no sub-driver is using this mapping any more (i.e., because we killed coreboot_table_find()). Otherwise, we'd need to explicitly kill all the sub-devices first. IOW, if this gets backported to older kernels, it would need to go along with this and its other dependencies:

b616cf53aa7a firmware: coreboot: Remove unused coreboot_table_find

But I guess that's a question for -stable. Or, we remove the 'Fixes' tag? Or add another tag, to list other dependencies? Or just ignore it.

But for this change as applied to mainline:

Quoted 7 lines
drivers/firmware/google/coreboot_table.c +1 -0
+
 	return ret;
 }
 EXPORT_SYMBOL(coreboot_table_init);
Signature · 1 line
Sent by a computer through tubes
#5 lore raw

Re: [PATCH v3 2/7] firmware: coreboot: Unmap ioregion on failure

Quoting Brian Norris (2018-08-09 10:49:38)

Quoted 28 lines · Stephen Boyd
Quoted 10 lines

Both callers of coreboot_table_init() ioremap the pointer that comes in but they don't unmap the memory on failure. Both of them also fail probe immediately with the return value of coreboot_table_init(), leaking a mapping when it fails. Plug the leak so the mapping isn't left unused.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
Fixes 570d30c2823f ("firmware: coreboot: Expose the coreboot table as a bus")

I suppose this is fair, since that commit introduced error paths and didn't clean them up. But one warning below:

Quoted 13 lines
---
 drivers/firmware/google/coreboot_table.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/firmware/google/coreboot_table.c +0 -0
index 19db5709ae28..0d3e140444ae 100644
@@ -138,6 +138,9 @@ int coreboot_table_init(struct device *dev, void __iomem *ptr)
              ptr_entry += entry.size;
      }
 
Quoted 8 lines

drivers/firmware/google/coreboot_table.c int coreboot_table_init(struct device *dev, void __iomem *ptr) · line 142

Quoted 2 lines
drivers/firmware/google/coreboot_table.c +2 -0
+     if (ret)
+             iounmap(ptr);

This works because no sub-driver is using this mapping any more (i.e., because we killed coreboot_table_find()). Otherwise, we'd need to explicitly kill all the sub-devices first. IOW, if this gets backported to older kernels, it would need to go along with this and its other dependencies:

The memory is copied out of the table. So do the devices actually use the memory that we remap here? I don't see how it's a problem if we unmap the table after we populate devices.

Quoted 10 lines

b616cf53aa7a firmware: coreboot: Remove unused coreboot_table_find

But I guess that's a question for -stable. Or, we remove the 'Fixes' tag? Or add another tag, to list other dependencies? Or just ignore it.

But for this change as applied to mainline:

Thanks!

#6 lore raw

Re: [PATCH v3 2/7] firmware: coreboot: Unmap ioregion on failure

Hi,

Quoted 38 lines · Stephen Boyd

Quoting Brian Norris (2018-08-09 10:49:38)

Quoted 28 lines · Stephen Boyd
Quoted 10 lines

Both callers of coreboot_table_init() ioremap the pointer that comes in but they don't unmap the memory on failure. Both of them also fail probe immediately with the return value of coreboot_table_init(), leaking a mapping when it fails. Plug the leak so the mapping isn't left unused.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
Fixes 570d30c2823f ("firmware: coreboot: Expose the coreboot table as a bus")

I suppose this is fair, since that commit introduced error paths and didn't clean them up. But one warning below:

Quoted 13 lines
---
 drivers/firmware/google/coreboot_table.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/firmware/google/coreboot_table.c +0 -0
index 19db5709ae28..0d3e140444ae 100644
@@ -138,6 +138,9 @@ int coreboot_table_init(struct device *dev, void __iomem *ptr)
              ptr_entry += entry.size;
      }
 
Quoted 8 lines

drivers/firmware/google/coreboot_table.c int coreboot_table_init(struct device *dev, void __iomem *ptr) · line 142

Quoted 2 lines
drivers/firmware/google/coreboot_table.c +2 -0
+     if (ret)
+             iounmap(ptr);

This works because no sub-driver is using this mapping any more (i.e., because we killed coreboot_table_find()). Otherwise, we'd need to explicitly kill all the sub-devices first. IOW, if this gets backported to older kernels, it would need to go along with this and its other dependencies:

Quoted 3 lines

The memory is copied out of the table. So do the devices actually use the memory that we remap here? I don't see how it's a problem if we unmap the table after we populate devices.

No, the memory is (or was) copied each time. See:

int coreboot_table_find(int tag, void *data, size_t data_size) {
...

	memcpy_fromio(&header, ptr_header, sizeof(header));

...

(where ptr_header is an alias for 'ptr')

So before commit b616cf53aa7a and friends, this patch is a bad idea.

Just to reiterate/clarify: none of this is a criticism of this patch as applied to mainline. It's just a criticism of what might happen with the 'Fixes' tag if we aren't careful.

Brian

Quoted 1 line
Quoted 1 line

b616cf53aa7a firmware: coreboot: Remove unused coreboot_table_find

#7 lore raw

Re: [PATCH v3 2/7] firmware: coreboot: Unmap ioregion on failure

Quoting Brian Norris (2018-08-09 12:52:13)

Quoted 53 lines

Hi,

Quoted 38 lines · Stephen Boyd

Quoting Brian Norris (2018-08-09 10:49:38)

Quoted 28 lines · Stephen Boyd
Quoted 10 lines

Both callers of coreboot_table_init() ioremap the pointer that comes in but they don't unmap the memory on failure. Both of them also fail probe immediately with the return value of coreboot_table_init(), leaking a mapping when it fails. Plug the leak so the mapping isn't left unused.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
Fixes 570d30c2823f ("firmware: coreboot: Expose the coreboot table as a bus")

I suppose this is fair, since that commit introduced error paths and didn't clean them up. But one warning below:

Quoted 13 lines
---
 drivers/firmware/google/coreboot_table.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/firmware/google/coreboot_table.c +0 -0
index 19db5709ae28..0d3e140444ae 100644
@@ -138,6 +138,9 @@ int coreboot_table_init(struct device *dev, void __iomem *ptr)
              ptr_entry += entry.size;
      }
 
Quoted 8 lines

drivers/firmware/google/coreboot_table.c int coreboot_table_init(struct device *dev, void __iomem *ptr) · line 142

Quoted 2 lines
drivers/firmware/google/coreboot_table.c +2 -0
+     if (ret)
+             iounmap(ptr);

This works because no sub-driver is using this mapping any more (i.e., because we killed coreboot_table_find()). Otherwise, we'd need to explicitly kill all the sub-devices first. IOW, if this gets backported to older kernels, it would need to go along with this and its other dependencies:

Quoted 3 lines

The memory is copied out of the table. So do the devices actually use the memory that we remap here? I don't see how it's a problem if we unmap the table after we populate devices.

No, the memory is (or was) copied each time. See:

int coreboot_table_find(int tag, void *data, size_t data_size) {
...

        memcpy_fromio(&header, ptr_header, sizeof(header));

...

Quoted 8 lines

(where ptr_header is an alias for 'ptr')

So before commit b616cf53aa7a and friends, this patch is a bad idea.

Just to reiterate/clarify: none of this is a criticism of this patch as applied to mainline. It's just a criticism of what might happen with the 'Fixes' tag if we aren't careful.

Ok. I misread your email. Either way, both of these commits we're talking about here are only in v4.18-rc series, so backporting for stable will be fine either way.

#8 lore raw

PATCH v3 3/7 firmware: coreboot: Make bus registration symmetric

In reply to Stephen Boyd

The bus is registered in module_init() but is unregistered when the platform driver remove() function calls coreboot_table_exit(). That isn't symmetric and it causes the bus to appear on systems that compile this code in, even when there isn't any coreboot firmware on the device. Let's move the registration to the coreboot_table_init() function so that it matches the exit path.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
---
 drivers/firmware/google/coreboot_table.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/firmware/google/coreboot_table.c +7 -7
index 0d3e140444ae..0e461d776ce6 100644
@@ -70,12 +70,6 @@ static struct bus_type coreboot_bus_type = {
 	.remove		= coreboot_bus_remove,
 };
 
-static int __init coreboot_bus_init(void)
-{
-	return bus_register(&coreboot_bus_type);
-}
-module_init(coreboot_bus_init);
-
 static void coreboot_device_release(struct device *dev)
 {
 	struct coreboot_device *device = CB_DEV(dev);
@@ -113,6 +107,10 @@ int coreboot_table_init(struct device *dev, void __iomem *ptr)
 		return -ENODEV;
 	}
 
+	ret = bus_register(&coreboot_bus_type);
+	if (ret)
+		return ret;
+
 	ptr_entry = (void *)ptr_header + header.header_bytes;
 	for (i = 0; i < header.table_entries; i++) {
 		memcpy_fromio(&entry, ptr_entry, sizeof(entry));
@@ -138,8 +136,10 @@ int coreboot_table_init(struct device *dev, void __iomem *ptr)
 		ptr_entry += entry.size;
 	}
 
-	if (ret)
+	if (ret) {
+		bus_unregister(&coreboot_bus_type);
 		iounmap(ptr);
+	}
 
 	return ret;
 }
Signature · 1 line
Sent by a computer through tubes
#9 lore raw

Re: [PATCH v3 3/7] firmware: coreboot: Make bus registration symmetric

drivers/firmware/google/coreboot_table.c int coreboot_table_init(struct device *dev, void __iomem *ptr) · line 142

Quoted 9 lines
drivers/firmware/google/coreboot_table.c +3 -1
@@ -138,8 +136,10 @@ int coreboot_table_init(struct device *dev, void __iomem *ptr)
                ptr_entry += entry.size;
        }
 
-       if (ret)
+       if (ret) {
+               bus_unregister(&coreboot_bus_type);
                iounmap(ptr);
+       }

nit: maybe cleaner to just do if (ret) coreboot_table_exit(); here? You're essentially writing the same code again.

#10 lore raw

Re: [PATCH v3 3/7] firmware: coreboot: Make bus registration symmetric

Quoting Julius Werner (2018-08-09 11:10:42)

Quoted 4 lines
Quoted 4 lines
drivers/firmware/google/coreboot_table.c +0 -0
@@ -138,8 +136,10 @@ int coreboot_table_init(struct device *dev, void __iomem *ptr)
                ptr_entry += entry.size;
        }
 
Quoted 8 lines

drivers/firmware/google/coreboot_table.c int coreboot_table_init(struct device *dev, void __iomem *ptr) · line 142

Quoted 5 lines
drivers/firmware/google/coreboot_table.c +3 -1
-       if (ret)
+       if (ret) {
+               bus_unregister(&coreboot_bus_type);
                iounmap(ptr);
+       }

nit: maybe cleaner to just do if (ret) coreboot_table_exit(); here? You're essentially writing the same code again.

Ok.

#11 lore raw

PATCH v3 4/7 firmware: coreboot: Collapse platform drivers into bus core

In reply to Stephen Boyd

The DT based and ACPI based platform drivers here do the same thing; map some memory and hand it over to the coreboot bus to populate devices. The only major difference is that the DT based driver doesn't map the coreboot table header to figure out how large of a region to map for the whole coreboot table and it uses of_iomap() instead of ioremap_cache(). A cached or non-cached mapping shouldn't matter here and mapping some smaller region first before mapping the whole table is just more work but should be OK. In the end, we can remove two files and combine the code all in one place making it easier to reason about things.

We leave the old Kconfigs in place for a little while longer but make them hidden and select the previously hidden config option. This way users can upgrade without having to know to reselect this config in the future. Later on we can remove the old hidden configs.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
---
 drivers/firmware/google/Kconfig               | 26 +++---
 drivers/firmware/google/Makefile              |  2 -
 drivers/firmware/google/coreboot_table-acpi.c | 88 -------------------
 drivers/firmware/google/coreboot_table-of.c   | 60 -------------
 drivers/firmware/google/coreboot_table.c      | 66 +++++++++++++-
 drivers/firmware/google/coreboot_table.h      |  6 --
 6 files changed, 72 insertions(+), 176 deletions(-)
 delete mode 100644 drivers/firmware/google/coreboot_table-acpi.c
 delete mode 100644 drivers/firmware/google/coreboot_table-of.c
drivers/firmware/google/Kconfig +10 -16
index a456a000048b..16564cfa47ef 100644
@@ -19,28 +19,22 @@ config GOOGLE_SMI
 	  variables.
 
 config GOOGLE_COREBOOT_TABLE
-	tristate
-	depends on GOOGLE_COREBOOT_TABLE_ACPI || GOOGLE_COREBOOT_TABLE_OF
-
-config GOOGLE_COREBOOT_TABLE_ACPI
-	tristate "Coreboot Table Access - ACPI"
-	depends on ACPI
-	select GOOGLE_COREBOOT_TABLE
+	tristate "Coreboot Table Access"
+	depends on ACPI || OF
 	help
 	  This option enables the coreboot_table module, which provides other
-	  firmware modules to access to the coreboot table. The coreboot table
-	  pointer is accessed through the ACPI "GOOGCB00" object.
+	  firmware modules access to the coreboot table. The coreboot table
+	  pointer is accessed through the ACPI "GOOGCB00" object or the
+	  device tree node /firmware/coreboot.
 	  If unsure say N.
 
+config GOOGLE_COREBOOT_TABLE_ACPI
+	tristate
+	select GOOGLE_COREBOOT_TABLE
+
 config GOOGLE_COREBOOT_TABLE_OF
-	tristate "Coreboot Table Access - Device Tree"
-	depends on OF
+	tristate
 	select GOOGLE_COREBOOT_TABLE
-	help
-	  This option enable the coreboot_table module, which provide other
-	  firmware modules to access coreboot table. The coreboot table pointer
-	  is accessed through the device tree node /firmware/coreboot.
-	  If unsure say N.
 
 config GOOGLE_MEMCONSOLE
 	tristate
drivers/firmware/google/Makefile +0 -2
index d0b3fba96194..d17caded5d88 100644
@@ -2,8 +2,6 @@
 
 obj-$(CONFIG_GOOGLE_SMI)		+= gsmi.o
 obj-$(CONFIG_GOOGLE_COREBOOT_TABLE)        += coreboot_table.o
-obj-$(CONFIG_GOOGLE_COREBOOT_TABLE_ACPI)   += coreboot_table-acpi.o
-obj-$(CONFIG_GOOGLE_COREBOOT_TABLE_OF)     += coreboot_table-of.o
 obj-$(CONFIG_GOOGLE_FRAMEBUFFER_COREBOOT)  += framebuffer-coreboot.o
 obj-$(CONFIG_GOOGLE_MEMCONSOLE)            += memconsole.o
 obj-$(CONFIG_GOOGLE_MEMCONSOLE_COREBOOT)   += memconsole-coreboot.o
drivers/firmware/google/coreboot_table-acpi.c deleted +0 -88

88 lines not on this page · raw message

drivers/firmware/google/coreboot_table-of.c deleted +0 -60

60 lines not on this page · raw message

drivers/firmware/google/coreboot_table.c +62 -4

97 lines not on this page · raw message

drivers/firmware/google/coreboot_table.h +0 -6
index 8ad95a94481b..71a9de6b15fa 100644
@@ -91,10 +91,4 @@ int coreboot_driver_register(struct coreboot_driver *driver);
 /* Unregister a driver that uses the data from a coreboot table. */
 void coreboot_driver_unregister(struct coreboot_driver *driver);
 
-/* Initialize coreboot table module given a pointer to iomem */
-int coreboot_table_init(struct device *dev, void __iomem *ptr);
-
-/* Cleanup coreboot table module */
-int coreboot_table_exit(void);
-
 #endif /* __COREBOOT_TABLE_H */
Signature · 1 line
Sent by a computer through tubes
#12 lore raw

PATCH v3 5/7 firmware: coreboot: Remap RAM with memremap() instead of ioremap()

In reply to Stephen Boyd

This is all system memory, so we shouldn't be mapping this all with ioremap() as these aren't I/O regions. Instead, they're memory regions so we should use memremap(). Pick MEMREMAP_WB so we can map memory from RAM directly if that's possible, otherwise it falls back to ioremap_cache() like is being done here already. This also nicely silences the sparse warnings in this code and reduces the need to copy anything around anymore.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
---
 drivers/firmware/google/coreboot_table.c | 42 +++++++++++-------------
 1 file changed, 20 insertions(+), 22 deletions(-)
drivers/firmware/google/coreboot_table.c +20 -22
index feb31502f64b..f343dbe86448 100644
@@ -32,7 +32,7 @@
 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
 
-static struct coreboot_table_header __iomem *ptr_header;
+static struct coreboot_table_header *ptr_header;
 
 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
 {
@@ -94,18 +94,18 @@ void coreboot_driver_unregister(struct coreboot_driver *driver)
 }
 EXPORT_SYMBOL(coreboot_driver_unregister);
 
-static int coreboot_table_init(struct device *dev, void __iomem *ptr)
+static int coreboot_table_init(struct device *dev, void *ptr)
 {
 	int i, ret;
 	void *ptr_entry;
 	struct coreboot_device *device;
-	struct coreboot_table_entry entry;
-	struct coreboot_table_header header;
+	struct coreboot_table_entry *entry;
+	struct coreboot_table_header *header;
 
 	ptr_header = ptr;
-	memcpy_fromio(&header, ptr_header, sizeof(header));
+	header = ptr;
 
-	if (strncmp(header.signature, "LBIO", sizeof(header.signature))) {
+	if (strncmp(header->signature, "LBIO", sizeof(header->signature))) {
 		pr_warn("coreboot_table: coreboot table missing or corrupt!\n");
 		return -ENODEV;
 	}
@@ -114,11 +114,11 @@ static int coreboot_table_init(struct device *dev, void __iomem *ptr)
 	if (ret)
 		return ret;
 
-	ptr_entry = (void *)ptr_header + header.header_bytes;
-	for (i = 0; i < header.table_entries; i++) {
-		memcpy_fromio(&entry, ptr_entry, sizeof(entry));
+	ptr_entry = ptr_header + header->header_bytes;
+	for (i = 0; i < header->table_entries; i++) {
+		entry = ptr_entry;
 
-		device = kzalloc(sizeof(struct device) + entry.size, GFP_KERNEL);
+		device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
 		if (!device) {
 			ret = -ENOMEM;
 			break;
@@ -128,7 +128,7 @@ static int coreboot_table_init(struct device *dev, void __iomem *ptr)
 		device->dev.parent = dev;
 		device->dev.bus = &coreboot_bus_type;
 		device->dev.release = coreboot_device_release;
-		memcpy_fromio(&device->entry, ptr_entry, entry.size);
+		memcpy(&device->entry, ptr_entry, entry->size);
 
 		ret = device_register(&device->dev);
 		if (ret) {
@@ -136,12 +136,12 @@ static int coreboot_table_init(struct device *dev, void __iomem *ptr)
 			break;
 		}
 
-		ptr_entry += entry.size;
+		ptr_entry += entry->size;
 	}
 
 	if (ret) {
 		bus_unregister(&coreboot_bus_type);
-		iounmap(ptr);
+		memunmap(ptr);
 	}
 
 	return ret;
@@ -149,11 +149,10 @@ static int coreboot_table_init(struct device *dev, void __iomem *ptr)
 
 static int coreboot_table_probe(struct platform_device *pdev)
 {
-	phys_addr_t phyaddr;
 	resource_size_t len;
-	struct coreboot_table_header __iomem *header = NULL;
+	struct coreboot_table_header *header;
 	struct resource *res;
-	void __iomem *ptr = NULL;
+	void *ptr;
 
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	if (!res)
@@ -163,14 +162,13 @@ static int coreboot_table_probe(struct platform_device *pdev)
 	if (!res->start || !len)
 		return -EINVAL;
 
-	phyaddr = res->start;
-	header = ioremap_cache(phyaddr, sizeof(*header));
+	header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
 	if (header == NULL)
 		return -ENOMEM;
 
-	ptr = ioremap_cache(phyaddr,
-			    header->header_bytes + header->table_bytes);
-	iounmap(header);
+	ptr = memremap(res->start, header->header_bytes + header->table_bytes,
+		       MEMREMAP_WB);
+	memunmap(header);
 	if (!ptr)
 		return -ENOMEM;
 
@@ -181,7 +179,7 @@ static int coreboot_table_remove(struct platform_device *pdev)
 {
 	if (ptr_header) {
 		bus_unregister(&coreboot_bus_type);
-		iounmap(ptr_header);
+		memunmap(ptr_header);
 		ptr_header = NULL;
 	}
 
Signature · 1 line
Sent by a computer through tubes
#13 lore raw

Re: [PATCH v3 5/7] firmware: coreboot: Remap RAM with memremap() instead of ioremap()

One thing to note is that we still want this space to be mappable by userspace applications via /dev/mem, so we need to make sure that there's no weird memory type mismatch that causes problems with that. Adding Aaron to see if he has any concerns here, since I think he's seen something like that in the past (not sure if it was related to what this kernel driver does).

Can you please test this on an x86 Chromebook and run the 'cbmem' userspace utility, make sure it doesn't fail after this?

Also, stupid question after taking a step back and looking at this
again: why do we keep a mapping alive for the lifetime of the driver at all? It used to be necessary when this driver was find-entry-on-demand, but nowadays it just goes through all entries once at probe time and immediately memcpy_fromio()s out all the relevant information into (struct coreboot_device)s. After that we're done accessing the "real" coreboot table, forever. Why not just unmap it again at the end of coreboot_table_init()?

Quoted 140 lines · Stephen Boyd

This is all system memory, so we shouldn't be mapping this all with ioremap() as these aren't I/O regions. Instead, they're memory regions so we should use memremap(). Pick MEMREMAP_WB so we can map memory from RAM directly if that's possible, otherwise it falls back to ioremap_cache() like is being done here already. This also nicely silences the sparse warnings in this code and reduces the need to copy anything around anymore.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
---
 drivers/firmware/google/coreboot_table.c | 42 +++++++++++-------------
 1 file changed, 20 insertions(+), 22 deletions(-)
drivers/firmware/google/coreboot_table.c +20 -23
index feb31502f64b..f343dbe86448 100644
@@ -32,7 +32,7 @@
 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
 
-static struct coreboot_table_header __iomem *ptr_header;
+static struct coreboot_table_header *ptr_header;
 
 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
 {
@@ -94,18 +94,18 @@ void coreboot_driver_unregister(struct coreboot_driver *driver)
 }
 EXPORT_SYMBOL(coreboot_driver_unregister);
 
-static int coreboot_table_init(struct device *dev, void __iomem *ptr)
+static int coreboot_table_init(struct device *dev, void *ptr)
 {
        int i, ret;
        void *ptr_entry;
        struct coreboot_device *device;
-       struct coreboot_table_entry entry;
-       struct coreboot_table_header header;
+       struct coreboot_table_entry *entry;
+       struct coreboot_table_header *header;
 
        ptr_header = ptr;
-       memcpy_fromio(&header, ptr_header, sizeof(header));
+       header = ptr;
 
-       if (strncmp(header.signature, "LBIO", sizeof(header.signature))) {
+       if (strncmp(header->signature, "LBIO", sizeof(header->signature))) {
                pr_warn("coreboot_table: coreboot table missing or corrupt!\n");
                return -ENODEV;
        }
@@ -114,11 +114,11 @@ static int coreboot_table_init(struct device *dev, void __iomem *ptr)
        if (ret)
                return ret;
 
-       ptr_entry = (void *)ptr_header + header.header_bytes;
-       for (i = 0; i < header.table_entries; i++) {
-               memcpy_fromio(&entry, ptr_entry, sizeof(entry));
+       ptr_entry = ptr_header + header->header_bytes;
+       for (i = 0; i < header->table_entries; i++) {
+               entry = ptr_entry;
 
-               device = kzalloc(sizeof(struct device) + entry.size, GFP_KERNEL);
+               device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
                if (!device) {
                        ret = -ENOMEM;
                        break;
@@ -128,7 +128,7 @@ static int coreboot_table_init(struct device *dev, void __iomem *ptr)
                device->dev.parent = dev;
                device->dev.bus = &coreboot_bus_type;
                device->dev.release = coreboot_device_release;
-               memcpy_fromio(&device->entry, ptr_entry, entry.size);
+               memcpy(&device->entry, ptr_entry, entry->size);
 
                ret = device_register(&device->dev);
                if (ret) {
@@ -136,12 +136,12 @@ static int coreboot_table_init(struct device *dev, void __iomem *ptr)
                        break;
                }
 
-               ptr_entry += entry.size;
+               ptr_entry += entry->size;
        }
 
        if (ret) {
                bus_unregister(&coreboot_bus_type);
-               iounmap(ptr);
+               memunmap(ptr);
        }
 
        return ret;
@@ -149,11 +149,10 @@ static int coreboot_table_init(struct device *dev, void __iomem *ptr)
 
 static int coreboot_table_probe(struct platform_device *pdev)
 {
-       phys_addr_t phyaddr;
        resource_size_t len;
-       struct coreboot_table_header __iomem *header = NULL;
+       struct coreboot_table_header *header;
        struct resource *res;
-       void __iomem *ptr = NULL;
+       void *ptr;
 
        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
        if (!res)
@@ -163,14 +162,13 @@ static int coreboot_table_probe(struct platform_device *pdev)
        if (!res->start || !len)
                return -EINVAL;
 
-       phyaddr = res->start;
-       header = ioremap_cache(phyaddr, sizeof(*header));
+       header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
        if (header == NULL)
                return -ENOMEM;
 
-       ptr = ioremap_cache(phyaddr,
-                           header->header_bytes + header->table_bytes);
-       iounmap(header);
+       ptr = memremap(res->start, header->header_bytes + header->table_bytes,
+                      MEMREMAP_WB);
+       memunmap(header);
        if (!ptr)
                return -ENOMEM;
 
@@ -181,7 +179,7 @@ static int coreboot_table_remove(struct platform_device *pdev)
 {
        if (ptr_header) {
                bus_unregister(&coreboot_bus_type);
-               iounmap(ptr_header);
+               memunmap(ptr_header);
                ptr_header = NULL;
        }
 
--

Sent by a computer through tubes

#14 lore raw

Re: [PATCH v3 5/7] firmware: coreboot: Remap RAM with memremap() instead of ioremap()

Quoting Julius Werner (2018-08-09 11:24:29)

Quoted 9 lines

One thing to note is that we still want this space to be mappable by userspace applications via /dev/mem, so we need to make sure that there's no weird memory type mismatch that causes problems with that. Adding Aaron to see if he has any concerns here, since I think he's seen something like that in the past (not sure if it was related to what this kernel driver does).

Can you please test this on an x86 Chromebook and run the 'cbmem' userspace utility, make sure it doesn't fail after this?

Sure!

Quoted 9 lines

Also, stupid question after taking a step back and looking at this
again: why do we keep a mapping alive for the lifetime of the driver at all? It used to be necessary when this driver was find-entry-on-demand, but nowadays it just goes through all entries once at probe time and immediately memcpy_fromio()s out all the relevant information into (struct coreboot_device)s. After that we're done accessing the "real" coreboot table, forever. Why not just unmap it again at the end of coreboot_table_init()?

Yes, we just copy everything over so it makes it simpler to just unmap at the end of coreboot_table_init(). Then userspace is free to make a questionable mapping into system RAM to find the table itself.

I'll make this change.

#15 lore raw

PATCH v3 6/7 firmware: coreboot: Only populate devices in coreboot_table_init()

In reply to Stephen Boyd

This function checks the header for sanity, registers a bus, and populates devices for each coreboot table entry. Let's just populate devices here and pull the other bits up into the caller so that this function can be repurposed for pure device creation and registration. We can devm()ify the memory mapping at the same time to keep error paths simpler.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
---
 drivers/firmware/google/coreboot_table.c | 66 +++++++++++-------------
 1 file changed, 29 insertions(+), 37 deletions(-)
drivers/firmware/google/coreboot_table.c +29 -37
index f343dbe86448..814913606d22 100644
@@ -32,8 +32,6 @@
 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
 
-static struct coreboot_table_header *ptr_header;
-
 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
 {
 	struct coreboot_device *device = CB_DEV(dev);
@@ -94,35 +92,21 @@ void coreboot_driver_unregister(struct coreboot_driver *driver)
 }
 EXPORT_SYMBOL(coreboot_driver_unregister);
 
-static int coreboot_table_init(struct device *dev, void *ptr)
+static int coreboot_table_populate(struct device *dev, void *ptr)
 {
 	int i, ret;
 	void *ptr_entry;
 	struct coreboot_device *device;
 	struct coreboot_table_entry *entry;
-	struct coreboot_table_header *header;
-
-	ptr_header = ptr;
-	header = ptr;
-
-	if (strncmp(header->signature, "LBIO", sizeof(header->signature))) {
-		pr_warn("coreboot_table: coreboot table missing or corrupt!\n");
-		return -ENODEV;
-	}
-
-	ret = bus_register(&coreboot_bus_type);
-	if (ret)
-		return ret;
+	struct coreboot_table_header *header = ptr;
 
-	ptr_entry = ptr_header + header->header_bytes;
+	ptr_entry = ptr + header->header_bytes;
 	for (i = 0; i < header->table_entries; i++) {
 		entry = ptr_entry;
 
 		device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
-		if (!device) {
-			ret = -ENOMEM;
-			break;
-		}
+		if (!device)
+			return -ENOMEM;
 
 		dev_set_name(&device->dev, "coreboot%d", i);
 		device->dev.parent = dev;
@@ -133,18 +117,13 @@ static int coreboot_table_init(struct device *dev, void *ptr)
 		ret = device_register(&device->dev);
 		if (ret) {
 			put_device(&device->dev);
-			break;
+			return ret;
 		}
 
 		ptr_entry += entry->size;
 	}
 
-	if (ret) {
-		bus_unregister(&coreboot_bus_type);
-		memunmap(ptr);
-	}
-
-	return ret;
+	return 0;
 }
 
 static int coreboot_table_probe(struct platform_device *pdev)
@@ -152,7 +131,9 @@ static int coreboot_table_probe(struct platform_device *pdev)
 	resource_size_t len;
 	struct coreboot_table_header *header;
 	struct resource *res;
+	struct device *dev = &pdev->dev;
 	void *ptr;
+	int ret;
 
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	if (!res)
@@ -162,26 +143,37 @@ static int coreboot_table_probe(struct platform_device *pdev)
 	if (!res->start || !len)
 		return -EINVAL;
 
+	/* Map and check just the header first to make sure things are sane */
 	header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
-	if (header == NULL)
+	if (!header)
 		return -ENOMEM;
 
-	ptr = memremap(res->start, header->header_bytes + header->table_bytes,
-		       MEMREMAP_WB);
+	if (strncmp(header->signature, "LBIO", sizeof(header->signature))) {
+		dev_warn(dev, "coreboot table missing or corrupt!\n");
+		return -ENODEV;
+	}
+
+	ptr = devm_memremap(dev, res->start,
+			    header->header_bytes + header->table_bytes,
+			    MEMREMAP_WB);
 	memunmap(header);
 	if (!ptr)
 		return -ENOMEM;
 
-	return coreboot_table_init(&pdev->dev, ptr);
+	ret = bus_register(&coreboot_bus_type);
+	if (ret)
+		return ret;
+
+	ret = coreboot_table_populate(dev, ptr);
+	if (ret)
+		bus_unregister(&coreboot_bus_type);
+
+	return ret;
 }
 
 static int coreboot_table_remove(struct platform_device *pdev)
 {
-	if (ptr_header) {
-		bus_unregister(&coreboot_bus_type);
-		memunmap(ptr_header);
-		ptr_header = NULL;
-	}
+	bus_unregister(&coreboot_bus_type);
 
 	return 0;
 }
Signature · 1 line
Sent by a computer through tubes
#16 lore raw

Re: [PATCH v3 6/7] firmware: coreboot: Only populate devices in coreboot_table_init()

Quoted 108 lines · Stephen Boyd

This function checks the header for sanity, registers a bus, and populates devices for each coreboot table entry. Let's just populate devices here and pull the other bits up into the caller so that this function can be repurposed for pure device creation and registration. We can devm()ify the memory mapping at the same time to keep error paths simpler.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
---
 drivers/firmware/google/coreboot_table.c | 66 +++++++++++-------------
 1 file changed, 29 insertions(+), 37 deletions(-)
drivers/firmware/google/coreboot_table.c +10 -28
index f343dbe86448..814913606d22 100644
@@ -32,8 +32,6 @@
 #define CB_DEV(d) container_of(d, struct coreboot_device, dev)
 #define CB_DRV(d) container_of(d, struct coreboot_driver, drv)
 
-static struct coreboot_table_header *ptr_header;
-
 static int coreboot_bus_match(struct device *dev, struct device_driver *drv)
 {
        struct coreboot_device *device = CB_DEV(dev);
@@ -94,35 +92,21 @@ void coreboot_driver_unregister(struct coreboot_driver *driver)
 }
 EXPORT_SYMBOL(coreboot_driver_unregister);
 
-static int coreboot_table_init(struct device *dev, void *ptr)
+static int coreboot_table_populate(struct device *dev, void *ptr)
 {
        int i, ret;
        void *ptr_entry;
        struct coreboot_device *device;
        struct coreboot_table_entry *entry;
-       struct coreboot_table_header *header;
-
-       ptr_header = ptr;
-       header = ptr;
-
-       if (strncmp(header->signature, "LBIO", sizeof(header->signature))) {
-               pr_warn("coreboot_table: coreboot table missing or corrupt!\n");
-               return -ENODEV;
-       }
-
-       ret = bus_register(&coreboot_bus_type);
-       if (ret)
-               return ret;
+       struct coreboot_table_header *header = ptr;
 
-       ptr_entry = ptr_header + header->header_bytes;
+       ptr_entry = ptr + header->header_bytes;
        for (i = 0; i < header->table_entries; i++) {
                entry = ptr_entry;
 
                device = kzalloc(sizeof(struct device) + entry->size, GFP_KERNEL);
-               if (!device) {
-                       ret = -ENOMEM;
-                       break;
-               }
+               if (!device)
+                       return -ENOMEM;
 
                dev_set_name(&device->dev, "coreboot%d", i);
                device->dev.parent = dev;
@@ -133,18 +117,13 @@ static int coreboot_table_init(struct device *dev, void *ptr)
                ret = device_register(&device->dev);
                if (ret) {
                        put_device(&device->dev);
-                       break;
+                       return ret;
                }
 
                ptr_entry += entry->size;
        }
 
-       if (ret) {
-               bus_unregister(&coreboot_bus_type);
-               memunmap(ptr);
-       }
-
-       return ret;
+       return 0;
 }
 
 static int coreboot_table_probe(struct platform_device *pdev)
@@ -152,7 +131,9 @@ static int coreboot_table_probe(struct platform_device *pdev)
        resource_size_t len;
        struct coreboot_table_header *header;
        struct resource *res;
+       struct device *dev = &pdev->dev;
        void *ptr;
+       int ret;
 
        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
        if (!res)
@@ -162,26 +143,37 @@ static int coreboot_table_probe(struct platform_device *pdev)
        if (!res->start || !len)
                return -EINVAL;
 
+       /* Map and check just the header first to make sure things are sane */

drivers/firmware/google/coreboot_table.c static int coreboot_table_probe(struct platform_device *pdev) · line 153

Quoted 10 lines
drivers/firmware/google/coreboot_table.c +4 -3
        header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
-       if (header == NULL)
+       if (!header)
                return -ENOMEM;
 
-       ptr = memremap(res->start, header->header_bytes + header->table_bytes,
-                      MEMREMAP_WB);
+       if (strncmp(header->signature, "LBIO", sizeof(header->signature))) {
+               dev_warn(dev, "coreboot table missing or corrupt!\n");
+               return -ENODEV;

Leaking the mapping here.

Quoted 35 lines
drivers/firmware/google/coreboot_table.c +15 -7
+       }
+
+       ptr = devm_memremap(dev, res->start,
+                           header->header_bytes + header->table_bytes,
+                           MEMREMAP_WB);
        memunmap(header);
        if (!ptr)
                return -ENOMEM;
 
-       return coreboot_table_init(&pdev->dev, ptr);
+       ret = bus_register(&coreboot_bus_type);
+       if (ret)
+               return ret;
+
+       ret = coreboot_table_populate(dev, ptr);
+       if (ret)
+               bus_unregister(&coreboot_bus_type);
+
+       return ret;
 }
 
 static int coreboot_table_remove(struct platform_device *pdev)
 {
-       if (ptr_header) {
-               bus_unregister(&coreboot_bus_type);
-               memunmap(ptr_header);
-               ptr_header = NULL;
-       }
+       bus_unregister(&coreboot_bus_type);
 
        return 0;
 }
--

Sent by a computer through tubes

#17 lore raw

Re: [PATCH v3 6/7] firmware: coreboot: Only populate devices in coreboot_table_init()

Quoting Julius Werner (2018-08-09 14:02:53)

Quoted 11 lines · Stephen Boyd
Quoted 10 lines
drivers/firmware/google/coreboot_table.c +2 -1
@@ -162,26 +143,37 @@ static int coreboot_table_probe(struct platform_device *pdev)
        if (!res->start || !len)
                return -EINVAL;
 
+       /* Map and check just the header first to make sure things are sane */
        header = memremap(res->start, sizeof(*header), MEMREMAP_WB);
-       if (header == NULL)
+       if (!header)
                return -ENOMEM;
 
Quoted 7 lines

drivers/firmware/google/coreboot_table.c static int coreboot_table_probe(struct platform_device *pdev) · line 153

Quoted 5 lines
drivers/firmware/google/coreboot_table.c +3 -2
-       ptr = memremap(res->start, header->header_bytes + header->table_bytes,
-                      MEMREMAP_WB);
+       if (strncmp(header->signature, "LBIO", sizeof(header->signature))) {
+               dev_warn(dev, "coreboot table missing or corrupt!\n");
+               return -ENODEV;

Leaking the mapping here.

Thanks. Fixed.

#18 lore raw

PATCH v3 7/7 firmware: coreboot: Request table region for exclusive access

In reply to Stephen Boyd

Call request_mem_region() on the entire coreboot table to make sure other devices don't attempt to map the coreboot table in their drivers. If drivers need that support, it would be better to provide bus APIs they can use to do that through the mapping created in this file.

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
---
 drivers/firmware/google/coreboot_table.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/firmware/google/coreboot_table.c +11 -3
index 814913606d22..94c41d814ea5 100644
@@ -132,6 +132,7 @@ static int coreboot_table_probe(struct platform_device *pdev)
 	struct coreboot_table_header *header;
 	struct resource *res;
 	struct device *dev = &pdev->dev;
+	const char *name;
 	void *ptr;
 	int ret;
 
@@ -153,10 +154,17 @@ static int coreboot_table_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
-	ptr = devm_memremap(dev, res->start,
-			    header->header_bytes + header->table_bytes,
-			    MEMREMAP_WB);
+	len = header->header_bytes + header->table_bytes;
+	res->end = res->start + len - 1;
+	name = res->name ?: dev_name(dev);
 	memunmap(header);
+
+	if (!devm_request_mem_region(dev, res->start, len, name)) {
+		dev_err(dev, "can't request region for resource %pR\n", res);
+		return -EBUSY;
+	}
+
+	ptr = devm_memremap(dev, res->start, len, MEMREMAP_WB);
 	if (!ptr)
 		return -ENOMEM;
 
Signature · 1 line
Sent by a computer through tubes
#19 lore raw

Re: [PATCH v3 7/7] firmware: coreboot: Request table region for exclusive access

Quoted 6 lines · Stephen Boyd

Call request_mem_region() on the entire coreboot table to make sure other devices don't attempt to map the coreboot table in their drivers. If drivers need that support, it would be better to provide bus APIs they can use to do that through the mapping created in this file.

Does this prevent userspace from mapping this region via /dev/mem? If so, let's please not do it to not break compatibility with existing tools. (I guess an alternative would be to rewrite 'cbmem' to use /sys/bus/coreboot/devices if available to get its coreboot table information. But we'd still need to maintain the old path for backwards compatibility anyway, so that would really just make it more complicated.)

#20 lore raw

Re: [PATCH v3 7/7] firmware: coreboot: Request table region for exclusive access

Quoting Julius Werner (2018-08-09 14:07:31)

Quoted 11 lines · Stephen Boyd
Quoted 6 lines

Call request_mem_region() on the entire coreboot table to make sure other devices don't attempt to map the coreboot table in their drivers. If drivers need that support, it would be better to provide bus APIs they can use to do that through the mapping created in this file.

Does this prevent userspace from mapping this region via /dev/mem? If so, let's please not do it to not break compatibility with existing tools.

No it doesn't break. I can still read the memory here with /dev/mem (and cbmem). But that seems to be because of a couple reasons. First, I have CONFIG_STRICT_DEVMEM=y but CONFIG_IO_STRICT_DEVMEM=n. This allows me to map memory that isn't system ram. If CONFIG_IO_STRICT_DEVMEM=y then mapping this I/O region through /dev/mem here would be denied . Furthermore, I see that my system RAM excludes this coreboot table so it doesn't fall into the bucket that CONFIG_STRICT_DEVMEM would find.

Quoted 5 lines

(I guess an alternative would be to rewrite 'cbmem' to use /sys/bus/coreboot/devices if available to get its coreboot table information. But we'd still need to maintain the old path for backwards compatibility anyway, so that would really just make it more complicated.)

This sounds like a good idea. Userspace reaching into /dev/mem is not good from a kernel hardening perspective. That's why those strict devmem configs exist. Can cbmem be updated to query information from device drivers instead, so that we can enable CONFIG_IO_STRICT_DEVMEM as well?

#21 lore raw

Re: [PATCH v3 7/7] firmware: coreboot: Request table region for exclusive access

Quoted 2 lines

Furthermore, I see that my system RAM excludes this coreboot table so it doesn't fall into the bucket that CONFIG_STRICT_DEVMEM would find.

Yes, that is intentional. We don't want the kernel to try to use that memory for anything else (since we want those tables to survive), so we mark them as reserved in the e820 map.

Quoted 10 lines
Quoted 5 lines

(I guess an alternative would be to rewrite 'cbmem' to use /sys/bus/coreboot/devices if available to get its coreboot table information. But we'd still need to maintain the old path for backwards compatibility anyway, so that would really just make it more complicated.)

This sounds like a good idea. Userspace reaching into /dev/mem is not good from a kernel hardening perspective. That's why those strict devmem configs exist. Can cbmem be updated to query information from device drivers instead, so that we can enable CONFIG_IO_STRICT_DEVMEM as well?

Well... problem is that cbmem doesn't just access the coreboot tables, it accesses more stuff. There is actually a larger memory region called CBMEM (that's what the utility is named after) which contains all sorts of random memory allocations that coreboot wanted to survive for the lifetime of the system. The coreboot table is one section in there, and it sort of serves as a directory for some of the others (although there's also just a general CBMEM directory... there's some redundancy there). But cbmem can also print some of the other CBMEM sections which it finds by querying the coreboot table, such as the firmware log or the boot timestamps.

So the question is how we can get to that content if /dev/mem isn't available anymore. One option would be to just write separate kernel drivers to completely replace the cbmem utility (we already have one for the log, for example), but I think Linux generally doesn't want to have too much logic and parsing and stuff in kernel drivers. Another option is to add a driver that just exposes a sysfs file through which you could read (we don't need to write) the CBMEM area... but then we'd essentially want that to take absolute addresses because that's what the coreboot table pointers contain, so we would've just built /dev/mem by another name (for a restricted range).

The nicest thing, really, would be if there was a way for a kernel driver to mark specific regions as "allowed" by /dev/mem. I don't suppose we'd be willing to introduce a mechanism like that?

#22 lore raw

Re: [PATCH v3 7/7] firmware: coreboot: Request table region for exclusive access

Actually, looking at what IO_STRICT_DEVMEM really does, would it really prevent userspace accesses to these areas? Because it seems that it only prevents accesses to areas marked as IORESOURCE_BUSY, and while I can't fully follow how the kernel assigns that, comments suggest that this is only set when "Driver has marked this resource busy".

So after you make the change to the other patch where we immediately unmap the coreboot table again at the end of the probe() function, shouldn't it become available to userspace again even with IO_STRICT_DEVMEM set?

Quoted 44 lines · Julius Werner
Quoted 3 lines

Furthermore, I see that my system RAM excludes this coreboot table so it doesn't fall into the bucket that CONFIG_STRICT_DEVMEM would find.

Yes, that is intentional. We don't want the kernel to try to use that memory for anything else (since we want those tables to survive), so we mark them as reserved in the e820 map.

Quoted 10 lines
Quoted 5 lines

(I guess an alternative would be to rewrite 'cbmem' to use /sys/bus/coreboot/devices if available to get its coreboot table information. But we'd still need to maintain the old path for backwards compatibility anyway, so that would really just make it more complicated.)

This sounds like a good idea. Userspace reaching into /dev/mem is not good from a kernel hardening perspective. That's why those strict devmem configs exist. Can cbmem be updated to query information from device drivers instead, so that we can enable CONFIG_IO_STRICT_DEVMEM as well?

Well... problem is that cbmem doesn't just access the coreboot tables, it accesses more stuff. There is actually a larger memory region called CBMEM (that's what the utility is named after) which contains all sorts of random memory allocations that coreboot wanted to survive for the lifetime of the system. The coreboot table is one section in there, and it sort of serves as a directory for some of the others (although there's also just a general CBMEM directory... there's some redundancy there). But cbmem can also print some of the other CBMEM sections which it finds by querying the coreboot table, such as the firmware log or the boot timestamps.

So the question is how we can get to that content if /dev/mem isn't available anymore. One option would be to just write separate kernel drivers to completely replace the cbmem utility (we already have one for the log, for example), but I think Linux generally doesn't want to have too much logic and parsing and stuff in kernel drivers. Another option is to add a driver that just exposes a sysfs file through which you could read (we don't need to write) the CBMEM area... but then we'd essentially want that to take absolute addresses because that's what the coreboot table pointers contain, so we would've just built /dev/mem by another name (for a restricted range).

The nicest thing, really, would be if there was a way for a kernel driver to mark specific regions as "allowed" by /dev/mem. I don't suppose we'd be willing to introduce a mechanism like that?

#23 lore raw

Re: [PATCH v3 7/7] firmware: coreboot: Request table region for exclusive access

What's with the top posting? ;-)

Quoting Julius Werner (2018-08-09 16:44:43)

Quoted 6 lines

Actually, looking at what IO_STRICT_DEVMEM really does, would it really prevent userspace accesses to these areas? Because it seems that it only prevents accesses to areas marked as IORESOURCE_BUSY, and while I can't fully follow how the kernel assigns that, comments suggest that this is only set when "Driver has marked this resource busy".

Requesting the memory region as is done in this patch marks it as busy.

Quoted 5 lines

So after you make the change to the other patch where we immediately unmap the coreboot table again at the end of the probe() function, shouldn't it become available to userspace again even with IO_STRICT_DEVMEM set?

Yes, if we unmap the region immediately after devices are populated then this whole point is moot with the assumption that this code isn't running at the same time as the cbmem utility. I've done this already and it is working on arm. I still need to build/boot/test on an x86 platform which I should be able to do tomorrow.

#24 lore raw

Re: [PATCH v3 7/7] firmware: coreboot: Request table region for exclusive access

Quoting Stephen Boyd (2018-08-09 19:54:27)

Quoted 18 lines

What's with the top posting? ;-)

Quoting Julius Werner (2018-08-09 16:44:43)

Quoted 6 lines

Actually, looking at what IO_STRICT_DEVMEM really does, would it really prevent userspace accesses to these areas? Because it seems that it only prevents accesses to areas marked as IORESOURCE_BUSY, and while I can't fully follow how the kernel assigns that, comments suggest that this is only set when "Driver has marked this resource busy".

Requesting the memory region as is done in this patch marks it as busy.

Quoted 5 lines

So after you make the change to the other patch where we immediately unmap the coreboot table again at the end of the probe() function, shouldn't it become available to userspace again even with IO_STRICT_DEVMEM set?

Quoted 6 lines

Yes, if we unmap the region immediately after devices are populated then this whole point is moot with the assumption that this code isn't running at the same time as the cbmem utility. I've done this already and it is working on arm. I still need to build/boot/test on an x86 platform which I should be able to do tomorrow.

I tried my latest version of the patches (unplubished so far) and those work on x86 with cbmem. So things look good and we don't need to keep the mapping around.

Reviewed-by on the v3 series
#25 lore raw
In reply to Stephen Boyd

Hi Stepehen,

Quoted 23 lines · Stephen Boyd

This series reworks the coreboot firmware driver a bit to fix some bugs and then simplify the code by changing the design to get rid of the different platform drivers, remap memory with memremap(), and use devm to simplify error paths. There's some risk in changing to memremap() but I think that should work out alright. It will either return memory directly, or fallback to ioremap_cache() as has been done before.

Changes from v2:
* A bunch more patches
* Fix iounmap missing on error path
* Fix bus getting unregistered on driver unbind and never registered again
* Request exclusive access to table region
* Pull out table signature check to driver probe

Changes from v1:
* Split out fixlet for DT based driver from platform driver change

Cc Wei-Ning Huang <[email protected]>
Cc Julius Werner <[email protected]>
Cc Brian Norris <[email protected]>
Cc Samuel Holland <[email protected]>
Quoted 8 lines

Stephen Boyd (7):
firmware: coreboot: Let OF core populate platform device
firmware: coreboot: Unmap ioregion on failure
firmware: coreboot: Make bus registration symmetric
firmware: coreboot: Collapse platform drivers into bus core
firmware: coreboot: Remap RAM with memremap() instead of ioremap()
firmware: coreboot: Only populate devices in coreboot_table_init()
firmware: coreboot: Request table region for exclusive access

I gave patches 1 and 2 closer review (and individual replies); the remaining patches look good to me too, so for the whole series:

Quoted 13 lines
 drivers/firmware/google/Kconfig               |  26 ++--
 drivers/firmware/google/Makefile              |   2 -
 drivers/firmware/google/coreboot_table-acpi.c |  88 ------------
 drivers/firmware/google/coreboot_table-of.c   |  82 -----------
 drivers/firmware/google/coreboot_table.c      | 135 +++++++++++++-----
 drivers/firmware/google/coreboot_table.h      |   6 -
 6 files changed, 107 insertions(+), 232 deletions(-)
 delete mode 100644 drivers/firmware/google/coreboot_table-acpi.c
 delete mode 100644 drivers/firmware/google/coreboot_table-of.c
Signature · 1 line
Sent by a computer through tubes