Skip to contentSkip to conversation

Read-only memremap()

PATCH Superseded Stephen Boyd 2 participants 8 messages Jun 14, 2019 – Jul 18, 2019

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Read-only memremap()

This patch series implements a read-only version of memremap() via a new MEMREMAP_RO flag. If this is passed in the mapping call, we'll try to map the memory region as read-only if it doesn't intersect with an existing mapping. Otherwise, we'll try to fallback to other flags to try to map the memory that way.

The main use case I have is to map the command-db memory region on Qualcomm devices with a read-only mapping. It's already a const marked pointer and the API returns const pointers as well, so this series makes sure that even stray writes can't modify the memory. To get there we introduce a devm version of memremap() for a reserved memory region, add a memremap() flag, and implement support for that flag on arm64.

Changes from v1:
* Picked up tags and rebased to v5.2-rc3

Cc Evan Green <[email protected]>
Cc Rob Herring <[email protected]>
Cc Bjorn Andersson <[email protected]>
Cc Andy Gross <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>

Stephen Boyd (5):
reserved_mem: Add a devm_memremap_reserved_mem() API
soc: qcom: cmd-db: Migrate to devm_memremap_reserved_mem()
memremap: Add support for read-only memory mappings
arm64: Add support for arch_memremap_ro()
soc: qcom: cmd-db: Map with read-only mappings

 arch/arm64/include/asm/io.h     |  1 +
 drivers/of/of_reserved_mem.c    | 45 +++++++++++++++++++++++++++++++++
 drivers/soc/qcom/cmd-db.c       | 14 +++-------
 include/linux/io.h              |  1 +
 include/linux/of_reserved_mem.h |  6 +++++
 kernel/iomem.c                  | 15 +++++++++--
 6 files changed, 70 insertions(+), 12 deletions(-)

base-commit: f2c7c76c5d0a443053e94adb9f0918fa2fb85c3a

Signature · 1 line
Sent by a computer through tubes
#1 lore raw

PATCH v2 1/5 reserved_mem: Add a devm_memremap_reserved_mem() API

We have a few drivers that need to get a reserved memory region, request the region, and map the reserved memory with memremap(). Add an API to do this all in one function call.

Cc Evan Green <[email protected]>
Cc Rob Herring <[email protected]>
Cc Bjorn Andersson <[email protected]>
Cc Andy Gross <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dan Williams <[email protected]>
---
 drivers/of/of_reserved_mem.c    | 45 +++++++++++++++++++++++++++++++++
 include/linux/of_reserved_mem.h |  6 +++++
 2 files changed, 51 insertions(+)
drivers/of/of_reserved_mem.c +45 -0
index 89e190e94af7..bff932a3b80a 100644
@@ -12,6 +12,7 @@
 #define pr_fmt(fmt)	"OF: reserved mem: " fmt
 
 #include <linux/err.h>
+#include <linux/io.h>
 #include <linux/of.h>
 #include <linux/of_fdt.h>
 #include <linux/of_platform.h>
@@ -407,3 +408,47 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
 	return NULL;
 }
 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
+
+/**
+ * devm_memremap_reserved_mem() - acquire reserved_mem from a device node,
+ *                                request and memremap it
+ * @dev: device with node pointer of the desired reserved-memory region
+ * @flags: flags to pass to memremap()
+ *
+ * This function allows drivers to acquire a reference to the reserved_mem
+ * struct based on the device's device_node handle, request it and then
+ * memremap() it.
+ *
+ * Returns: A remapped reserved memory region, or an error pointer on failure.
+ */
+void *devm_memremap_reserved_mem(struct device *dev, unsigned long flags)
+{
+	void *dest_ptr;
+	struct reserved_mem *rmem;
+	struct resource *res;
+	const char *name;
+
+	rmem = of_reserved_mem_lookup(dev->of_node);
+	if (!rmem) {
+		dev_err(dev, "failed to acquire memory region\n");
+		return ERR_PTR(-ENODEV);
+	}
+
+	name = rmem->name ? : dev_name(dev);
+
+	res = devm_request_mem_region(dev, rmem->base, rmem->size, name);
+	if (!res) {
+		dev_err(dev, "can't request region for reserved memory\n");
+		return ERR_PTR(-EBUSY);
+	}
+
+	dest_ptr = devm_memremap(dev, rmem->base, rmem->size, flags);
+	if (!dest_ptr) {
+		dev_err(dev, "memremap failed for reserved memory\n");
+		devm_release_mem_region(dev, rmem->base, rmem->size);
+		dest_ptr = ERR_PTR(-ENOMEM);
+	}
+
+	return dest_ptr;
+}
+EXPORT_SYMBOL_GPL(devm_memremap_reserved_mem);
include/linux/of_reserved_mem.h +6 -0
index 60f541912ccf..a36be60ef67c 100644
@@ -39,6 +39,7 @@ void fdt_init_reserved_mem(void);
 void fdt_reserved_mem_save_node(unsigned long node, const char *uname,
 			       phys_addr_t base, phys_addr_t size);
 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np);
+void *devm_memremap_reserved_mem(struct device *dev, unsigned long flags);
 #else
 static inline int of_reserved_mem_device_init_by_idx(struct device *dev,
 					struct device_node *np, int idx)
@@ -54,6 +55,11 @@ static inline struct reserved_mem *of_reserved_mem_lookup(struct device_node *np
 {
 	return NULL;
 }
+static inline void *devm_memremap_reserved_mem(struct device *dev,
+					       unsigned long flags)
+{
+	return NULL;
+}
 #endif
 
 /**
Signature · 1 line
Sent by a computer through tubes
#2 lore raw

PATCH v2 2/5 soc: qcom: cmd-db: Migrate to devm_memremap_reserved_mem()

In reply to Stephen Boyd

This gets rid of some duplicate code, and also makes the reserved memory region show up as 'cmd-db' memory in /proc/iomem.

Cc Evan Green <[email protected]>
Cc Rob Herring <[email protected]>
Cc Bjorn Andersson <[email protected]>
Cc Andy Gross <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dan Williams <[email protected]>
---
 drivers/soc/qcom/cmd-db.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/soc/qcom/cmd-db.c +3 -10
index f6c3d17b05c7..10a34d26b753 100644
@@ -238,18 +238,11 @@ EXPORT_SYMBOL(cmd_db_read_slave_id);
 
 static int cmd_db_dev_probe(struct platform_device *pdev)
 {
-	struct reserved_mem *rmem;
 	int ret = 0;
 
-	rmem = of_reserved_mem_lookup(pdev->dev.of_node);
-	if (!rmem) {
-		dev_err(&pdev->dev, "failed to acquire memory region\n");
-		return -EINVAL;
-	}
-
-	cmd_db_header = memremap(rmem->base, rmem->size, MEMREMAP_WB);
-	if (!cmd_db_header) {
-		ret = -ENOMEM;
+	cmd_db_header = devm_memremap_reserved_mem(&pdev->dev, MEMREMAP_WB);
+	if (IS_ERR(cmd_db_header)) {
+		ret = PTR_ERR(cmd_db_header);
 		cmd_db_header = NULL;
 		return ret;
 	}
Signature · 1 line
Sent by a computer through tubes
#3 lore raw

PATCH v2 3/5 memremap: Add support for read-only memory mappings

In reply to Stephen Boyd

Sometimes we have memories that are supposed to be read-only, but when we map these regions the best we can do is map them as write-back with MEMREMAP_WB. Introduce a read-only memory mapping (MEMREMAP_RO) that allows us to map reserved memory regions as read-only. This way, we're less likely to see these special memory regions become corrupted by stray writes to them.

Cc Evan Green <[email protected]>
Cc Rob Herring <[email protected]>
Cc Bjorn Andersson <[email protected]>
Cc Andy Gross <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dan Williams <[email protected]>
---
 include/linux/io.h |  1 +
 kernel/iomem.c     | 15 +++++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
include/linux/io.h +1 -0
index 32e30e8fb9db..16c7f4498869 100644
@@ -159,6 +159,7 @@ enum {
 	MEMREMAP_WC = 1 << 2,
 	MEMREMAP_ENC = 1 << 3,
 	MEMREMAP_DEC = 1 << 4,
+	MEMREMAP_RO = 1 << 5,
 };
 
 void *memremap(resource_size_t offset, size_t size, unsigned long flags);
kernel/iomem.c +13 -2
index 93c264444510..10d5ef0ff09e 100644
@@ -19,6 +19,13 @@ static void *arch_memremap_wb(resource_size_t offset, unsigned long size)
 }
 #endif
 
+#ifndef arch_memremap_ro
+static void *arch_memremap_ro(resource_size_t offset, unsigned long size)
+{
+	return NULL;
+}
+#endif
+
 #ifndef arch_memremap_can_ram_remap
 static bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
 					unsigned long flags)
@@ -84,7 +91,10 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 	}
 
 	/* Try all mapping types requested until one returns non-NULL */
-	if (flags & MEMREMAP_WB) {
+	if ((flags & MEMREMAP_RO) && is_ram != REGION_INTERSECTS)
+		addr = arch_memremap_ro(offset, size);
+
+	if (!addr && (flags & MEMREMAP_WB)) {
 		/*
 		 * MEMREMAP_WB is special in that it can be satisfied
 		 * from the direct map.  Some archs depend on the
@@ -103,7 +113,8 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 	 * address mapping.  Enforce that this mapping is not aliasing
 	 * System RAM.
 	 */
-	if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) {
+	if (!addr && is_ram == REGION_INTERSECTS &&
+	    (flags != MEMREMAP_WB || flags != MEMREMAP_RO)) {
 		WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
 				&offset, (unsigned long) size);
 		return NULL;
Signature · 1 line
Sent by a computer through tubes
maintainer
#4 lore raw

Re: [PATCH v2 3/5] memremap: Add support for read-only memory mappings

Quoted 64 lines · Stephen Boyd

Sometimes we have memories that are supposed to be read-only, but when we map these regions the best we can do is map them as write-back with MEMREMAP_WB. Introduce a read-only memory mapping (MEMREMAP_RO) that allows us to map reserved memory regions as read-only. This way, we're less likely to see these special memory regions become corrupted by stray writes to them.

Cc Evan Green <[email protected]>
Cc Rob Herring <[email protected]>
Cc Bjorn Andersson <[email protected]>
Cc Andy Gross <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dan Williams <[email protected]>
---
 include/linux/io.h |  1 +
 kernel/iomem.c     | 15 +++++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
include/linux/io.h +1 -0
index 32e30e8fb9db..16c7f4498869 100644
@@ -159,6 +159,7 @@ enum {
 	MEMREMAP_WC = 1 << 2,
 	MEMREMAP_ENC = 1 << 3,
 	MEMREMAP_DEC = 1 << 4,
+	MEMREMAP_RO = 1 << 5,
 };
 
 void *memremap(resource_size_t offset, size_t size, unsigned long flags);
kernel/iomem.c +11 -1
index 93c264444510..10d5ef0ff09e 100644
@@ -19,6 +19,13 @@ static void *arch_memremap_wb(resource_size_t offset, unsigned long size)
 }
 #endif
 
+#ifndef arch_memremap_ro
+static void *arch_memremap_ro(resource_size_t offset, unsigned long size)
+{
+	return NULL;
+}
+#endif
+
 #ifndef arch_memremap_can_ram_remap
 static bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
 					unsigned long flags)
@@ -84,7 +91,10 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 	}
 
 	/* Try all mapping types requested until one returns non-NULL */
-	if (flags & MEMREMAP_WB) {
+	if ((flags & MEMREMAP_RO) && is_ram != REGION_INTERSECTS)
+		addr = arch_memremap_ro(offset, size);
+
+	if (!addr && (flags & MEMREMAP_WB)) {
 		/*
 		 * MEMREMAP_WB is special in that it can be satisfied
 		 * from the direct map.  Some archs depend on the

kernel/iomem.c void *memremap(resource_size_t offset, size_t size, unsigned long flags) · line 120

Quoted 9 lines
kernel/iomem.c +2 -1
@@ -103,7 +113,8 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 	 * address mapping.  Enforce that this mapping is not aliasing
 	 * System RAM.
 	 */
-	if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) {
+	if (!addr && is_ram == REGION_INTERSECTS &&
+	    (flags != MEMREMAP_WB || flags != MEMREMAP_RO)) {
 		WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
 				&offset, (unsigned long) size);
 		return NULL;

This function seems a little confused about whether 'flags' is really a bitmap of flags, or whether it is equal to exactly one entry in the enum. Given that I think it's sensible for somebody to specify 'MEMREMAP_RO | MEMREMAP_WT', then we probably need to start checking these things a bit more thoroughly so we can reject unsupported combinations at the very least.

Will

#5 lore raw

Re: [PATCH v2 3/5] memremap: Add support for read-only memory mappings

Quoting Will Deacon (2019-07-10 07:14:34)

Quoted 21 lines · Stephen Boyd
Quoted 20 lines
kernel/iomem.c +6 -2
@@ -84,7 +91,10 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
      }
 
      /* Try all mapping types requested until one returns non-NULL */
-     if (flags & MEMREMAP_WB) {
+     if ((flags & MEMREMAP_RO) && is_ram != REGION_INTERSECTS)
+             addr = arch_memremap_ro(offset, size);
+
+     if (!addr && (flags & MEMREMAP_WB)) {
              /*
               * MEMREMAP_WB is special in that it can be satisfied
               * from the direct map.  Some archs depend on the
@@ -103,7 +113,8 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
       * address mapping.  Enforce that this mapping is not aliasing
       * System RAM.
       */
-     if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) {
+     if (!addr && is_ram == REGION_INTERSECTS &&
+         (flags != MEMREMAP_WB || flags != MEMREMAP_RO)) {
              WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
Quoted 9 lines

kernel/iomem.c void *memremap(resource_size_t offset, size_t size, unsigned long flags) · line 120

Quoted 2 lines
kernel/iomem.c +0 -0
                              &offset, (unsigned long) size);
              return NULL;

This function seems a little confused about whether 'flags' is really a bitmap of flags, or whether it is equal to exactly one entry in the enum. Given that I think it's sensible for somebody to specify 'MEMREMAP_RO | MEMREMAP_WT', then we probably need to start checking these things a bit more thoroughly so we can reject unsupported combinations at the very least.

I'm also confused about the same thing. I thought it was a "getting worse via best effort" type of thing based on the comment above the function.

* In the case of multiple flags, the different
* mapping types will be attempted in the order listed below until one of
* them succeeds.

(I now realize I should have documented the new flag so that this order would be known. I'll resend this series again with the documentation fix.)

I also thought that the combination of read-only and write through would be OK because the flags are more of a best effort approach to making a mapping. Given that, is there anything to reject? Or do we just keep trying until we can't try anymore?

#6 lore raw

PATCH v2 4/5 arm64: Add support for arch_memremap_ro()

In reply to Stephen Boyd

Pass in PAGE_KERNEL_RO to the underlying IO mapping mechanism to get a read-only mapping for the MEMREMAP_RO type of memory mappings that memremap() supports.

Cc Evan Green <[email protected]>
Cc Rob Herring <[email protected]>
Cc Bjorn Andersson <[email protected]>
Cc Andy Gross <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dan Williams <[email protected]>
---
 arch/arm64/include/asm/io.h | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/include/asm/io.h +1 -0
index b807cb9b517d..cc33f4c8647b 100644
@@ -183,6 +183,7 @@ extern void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size);
 #define ioremap_nocache(addr, size)	__ioremap((addr), (size), __pgprot(PROT_DEVICE_nGnRE))
 #define ioremap_wc(addr, size)		__ioremap((addr), (size), __pgprot(PROT_NORMAL_NC))
 #define ioremap_wt(addr, size)		__ioremap((addr), (size), __pgprot(PROT_DEVICE_nGnRE))
+#define arch_memremap_ro(addr, size)	__ioremap((addr), (size), PAGE_KERNEL_RO)
 #define iounmap				__iounmap
 
 /*
Signature · 1 line
Sent by a computer through tubes
#7 lore raw

PATCH v2 5/5 soc: qcom: cmd-db: Map with read-only mappings

In reply to Stephen Boyd

The command DB is read-only already to the kernel because everything is const marked once we map it. Let's go one step further and try to map the memory as read-only in the page tables. This should make it harder for random code to corrupt the database and change the contents.

Cc Evan Green <[email protected]>
Cc Rob Herring <[email protected]>
Cc Bjorn Andersson <[email protected]>
Cc Andy Gross <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Dan Williams <[email protected]>
---
 drivers/soc/qcom/cmd-db.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/soc/qcom/cmd-db.c +2 -1
index 10a34d26b753..6365e8260282 100644
@@ -240,7 +240,8 @@ static int cmd_db_dev_probe(struct platform_device *pdev)
 {
 	int ret = 0;
 
-	cmd_db_header = devm_memremap_reserved_mem(&pdev->dev, MEMREMAP_WB);
+	cmd_db_header = devm_memremap_reserved_mem(&pdev->dev,
+						   MEMREMAP_RO | MEMREMAP_WB);
 	if (IS_ERR(cmd_db_header)) {
 		ret = PTR_ERR(cmd_db_header);
 		cmd_db_header = NULL;
Signature · 1 line
Sent by a computer through tubes