Skip to contentSkip to conversation

Add STRICT_DEVMEM support on RISC-V

PATCH New Zong Li 1 participant 3 messages Jun 22, 2020 RISC-V

lore
Share & tools
lore

Zong Li

author
#0 lore raw

v2 cover Add STRICT_DEVMEM support on RISC-V

This patch set adds devmem_is_allowed to support STRICT_DEVMEM. In devmem_is_allowed, it invokes page_is_ram to check the specific address whether registered as System RAM in iomem_resource. The resource initialization is in kexec/kdump patch series which is reviewing, page_is_ram checking is no use until it is merged.

Zong Li (2):
riscv: Support CONFIG_STRICT_DEVMEM
riscv: mm: refine the Makefile

 arch/riscv/Kconfig          |  1 +
 arch/riscv/include/asm/io.h |  2 ++
 arch/riscv/mm/Makefile      |  5 +----
 arch/riscv/mm/mmap.c        | 24 ++++++++++++++++++++++++
 4 files changed, 28 insertions(+), 4 deletions(-)
 create mode 100644 arch/riscv/mm/mmap.c
Signature · 1 line
2.27.0
author
#1 lore raw

PATCH v2 1/2 riscv: Support CONFIG_STRICT_DEVMEM

Implement the 'devmem_is_allowed()' interface for RISC-V, like some of other architectures have done. It will be called from range_is_allowed() when userpsace attempts to access /dev/mem.

Access to exclusive IOMEM and kernel RAM is denied unless CONFIG_STRICT_DEVMEM is set to 'n'.

Test it by devmem, the result as follows:

- CONFIG_STRICT_DEVMEM=y

        $ devmem 0x10010000
        0x00000000
        $ devmem 0x80200000
        0x0000106F

- CONFIG_STRICT_DEVMEM is not set

        $ devmem 0x10010000
        devmem: mmap: Operation not permitted
        $ devmem 0x80200000
        devmem: mmap: Operation not permitted

Changes in v2:
- Move implementations out from mm/init.c. Suggested by Nick Kossifidis.

---
 arch/riscv/Kconfig          |  1 +
 arch/riscv/include/asm/io.h |  2 ++
 arch/riscv/mm/Makefile      |  1 +
 arch/riscv/mm/mmap.c        | 24 ++++++++++++++++++++++++
 4 files changed, 28 insertions(+)
 create mode 100644 arch/riscv/mm/mmap.c
arch/riscv/Kconfig +1 -0
index 089293e4ad46..c6569939f7ba 100644
@@ -17,6 +17,7 @@ config RISCV
 	select ARCH_HAS_BINFMT_FLAT
 	select ARCH_HAS_DEBUG_VIRTUAL if MMU
 	select ARCH_HAS_DEBUG_WX
+	select ARCH_HAS_DEVMEM_IS_ALLOWED
 	select ARCH_HAS_GCOV_PROFILE_ALL
 	select ARCH_HAS_GIGANTIC_PAGE
 	select ARCH_HAS_MMIOWB
arch/riscv/include/asm/io.h +2 -0
index 3835c3295dc5..04ac65ab93ce 100644
@@ -147,4 +147,6 @@ __io_writes_outs(outs, u64, q, __io_pbr(), __io_paw())
 
 #include <asm-generic/io.h>
 
+extern int devmem_is_allowed(unsigned long pfn);
+
 #endif /* _ASM_RISCV_IO_H */
arch/riscv/mm/Makefile +1 -0
index 363ef01c30b1..4530ad52f1d6 100644
@@ -10,6 +10,7 @@ obj-y += extable.o
 obj-$(CONFIG_MMU) += fault.o pageattr.o
 obj-y += cacheflush.o
 obj-y += context.o
+obj-y += mmap.o
 
 ifeq ($(CONFIG_MMU),y)
 obj-$(CONFIG_SMP) += tlbflush.o
arch/riscv/mm/mmap.c new file +24 -0
new file mode 100644
index 000000000000..095fc82897b1
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 SiFive
+ */
+
+#ifdef CONFIG_STRICT_DEVMEM
+#include <linux/ioport.h>
+
+/*
+ * devmem_is_allowed() checks to see if /dev/mem access to a certain address
+ * is valid. The argument is a physical page number.
+ *
+ * Disallow access to system RAM as well as device-exclusive MMIO regions.
+ * This effectively disable read()/write() on /dev/mem.
+ */
+int devmem_is_allowed(unsigned long pfn)
+{
+	if (iomem_is_exclusive(pfn << PAGE_SHIFT))
+		return 0;
+	if (!page_is_ram(pfn))
+		return 1;
+	return 0;
+}
+#endif
Signature · 1 line
2.27.0
author
#2 lore raw

PATCH v2 2/2 riscv: mm: refine the Makefile

In reply to Zong Li

Put all objects which selected by obj-y together.

---
 arch/riscv/mm/Makefile | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/riscv/mm/Makefile +1 -5
index 4530ad52f1d6..7f95f66e135a 100644
@@ -5,12 +5,8 @@ ifdef CONFIG_FTRACE
 CFLAGS_REMOVE_init.o = -pg
 endif
 
-obj-y += init.o
-obj-y += extable.o
+obj-y += init.o extable.o cacheflush.o context.o mmap.o
 obj-$(CONFIG_MMU) += fault.o pageattr.o
-obj-y += cacheflush.o
-obj-y += context.o
-obj-y += mmap.o
 
 ifeq ($(CONFIG_MMU),y)
 obj-$(CONFIG_SMP) += tlbflush.o
Signature · 1 line
2.27.0