Add and use a generic version of devmem_is_allowed()
authorPalmer Dabbelt <palmerdabbelt@google.com>
Fri, 11 Dec 2020 20:30:26 +0000 (12:30 -0800)
committerPalmer Dabbelt <palmerdabbelt@google.com>
Fri, 11 Dec 2020 20:30:26 +0000 (12:30 -0800)
As part of adding STRICT_DEVMEM support to the RISC-V port, Zong provided an
implementation of devmem_is_allowed() that's exactly the same as the version in
a handful of other ports.  Rather than duplicate code, I've put a generic
version of this in lib/ and used it for the RISC-V port.

* palmer/generic-devmem:
  arm64: Use the generic devmem_is_allowed()
  arm: Use the generic devmem_is_allowed()
  RISC-V: Use the new generic devmem_is_allowed()
  lib: Add a generic version of devmem_is_allowed()

12 files changed:
arch/arm/Kconfig
arch/arm/include/asm/io.h
arch/arm/mm/mmap.c
arch/arm64/Kconfig
arch/arm64/include/asm/io.h
arch/arm64/mm/mmap.c
arch/riscv/Kconfig
include/asm-generic/io.h
lib/Kconfig
lib/Kconfig.debug
lib/Makefile
lib/devmem_is_allowed.c [new file with mode: 0644]

index fe2f17eb2b505ace1720741f7cd550d6eeb6f356..ae7c7364871bb19ea31f2a0de202983b6a89c564 100644 (file)
@@ -5,7 +5,6 @@ config ARM
        select ARCH_32BIT_OFF_T
        select ARCH_HAS_BINFMT_FLAT
        select ARCH_HAS_DEBUG_VIRTUAL if MMU
-       select ARCH_HAS_DEVMEM_IS_ALLOWED
        select ARCH_HAS_DMA_WRITE_COMBINE if !ARM_DMA_MEM_BUFFERABLE
        select ARCH_HAS_ELF_RANDOMIZE
        select ARCH_HAS_FORTIFY_SOURCE
@@ -56,6 +55,7 @@ config ARM
        select GENERIC_IRQ_PROBE
        select GENERIC_IRQ_SHOW
        select GENERIC_IRQ_SHOW_LEVEL
+       select GENERIC_LIB_DEVMEM_IS_ALLOWED
        select GENERIC_PCI_IOMAP
        select GENERIC_SCHED_CLOCK
        select GENERIC_SMP_IDLE_THREAD
index ab2b654084fa33151c4df80b3f65ed0076e1852a..fc748122f1e0d61107b82728c5c9e8f69d08c990 100644 (file)
@@ -441,7 +441,6 @@ extern void pci_iounmap(struct pci_dev *dev, void __iomem *addr);
 #define ARCH_HAS_VALID_PHYS_ADDR_RANGE
 extern int valid_phys_addr_range(phys_addr_t addr, size_t size);
 extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
-extern int devmem_is_allowed(unsigned long pfn);
 #endif
 
 /*
index b8d912ac9e61495aaeab9db3fd5d294c70f5aa41..a0f8a0ca0788adccb4459ff0eec6e8dc8d095f29 100644 (file)
@@ -165,25 +165,3 @@ int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
 {
        return (pfn + (size >> PAGE_SHIFT)) <= (1 + (PHYS_MASK >> PAGE_SHIFT));
 }
-
-#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.
- * We mimic x86 here by disallowing 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
index f858c352f72a47cdef887a9a6d291b4f0a65dadd..dd3c81d84e28c009124f47f4b811e9632f6f956f 100644 (file)
@@ -13,7 +13,6 @@ config ARM64
        select ARCH_BINFMT_ELF_STATE
        select ARCH_HAS_DEBUG_VIRTUAL
        select ARCH_HAS_DEBUG_VM_PGTABLE
-       select ARCH_HAS_DEVMEM_IS_ALLOWED
        select ARCH_HAS_DMA_PREP_COHERENT
        select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI
        select ARCH_HAS_FAST_MULTIPLIER
@@ -112,6 +111,7 @@ config ARM64
        select GENERIC_IRQ_PROBE
        select GENERIC_IRQ_SHOW
        select GENERIC_IRQ_SHOW_LEVEL
+       select GENERIC_LIB_DEVMEM_IS_ALLOWED
        select GENERIC_PCI_IOMAP
        select GENERIC_PTDUMP
        select GENERIC_SCHED_CLOCK
index fd172c41df905120f157b90eecb93b3911e32e6a..5ea8656a2030bff30e447041029f13106b4c44c3 100644 (file)
@@ -201,6 +201,4 @@ extern void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size);
 extern int valid_phys_addr_range(phys_addr_t addr, size_t size);
 extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
 
-extern int devmem_is_allowed(unsigned long pfn);
-
 #endif /* __ASM_IO_H */
index 3028bacbc4e9c40135b1f84a28a67500218c3078..07937b49cb881c2d2795a16e025da4081ae3921d 100644 (file)
@@ -47,24 +47,3 @@ int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
 {
        return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
 }
-
-#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.  We mimic x86 here by
- * disallowing 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
index d9da5d92e40da0f20325c54219c8e91bb9cb72f4..2b41f6d8e4583bd2106e8bd306bb702c52aa774d 100644 (file)
@@ -44,6 +44,7 @@ config RISCV
        select GENERIC_IOREMAP
        select GENERIC_IRQ_MULTI_HANDLER
        select GENERIC_IRQ_SHOW
+       select GENERIC_LIB_DEVMEM_IS_ALLOWED
        select GENERIC_PCI_IOMAP
        select GENERIC_PTDUMP if MMU
        select GENERIC_SCHED_CLOCK
index 9ea83d80eb6f93f62fa98ba9875d03ed58871a69..c6af40ce03befec1740fd9ab705648e1eba2bf81 100644 (file)
@@ -1137,6 +1137,10 @@ static inline void memcpy_toio(volatile void __iomem *addr, const void *buffer,
 }
 #endif
 
+#ifndef CONFIG_GENERIC_DEVMEM_IS_ALLOWED
+extern int devmem_is_allowed(unsigned long pfn);
+#endif
+
 #endif /* __KERNEL__ */
 
 #endif /* __ASM_GENERIC_IO_H */
index b46a9fd122c81acabd888e8b250e78be4f575ef6..46806332a8cc2c3235a379b4300d113da959bbe0 100644 (file)
@@ -686,6 +686,9 @@ config GENERIC_LIB_CMPDI2
 config GENERIC_LIB_UCMPDI2
        bool
 
+config GENERIC_LIB_DEVMEM_IS_ALLOWED
+       bool
+
 config PLDMFW
        bool
        default n
index d7a7bc3b6098287dc7af87b2c94d5304675401f3..c63fc68f9bf2e85bfc38a9192c70996b7f3f57e7 100644 (file)
@@ -1645,7 +1645,7 @@ config ARCH_HAS_DEVMEM_IS_ALLOWED
 config STRICT_DEVMEM
        bool "Filter access to /dev/mem"
        depends on MMU && DEVMEM
-       depends on ARCH_HAS_DEVMEM_IS_ALLOWED
+       depends on ARCH_HAS_DEVMEM_IS_ALLOWED || GENERIC_LIB_DEVMEM_IS_ALLOWED
        default y if PPC || X86 || ARM64
        help
          If this option is disabled, you allow userspace (root) access to all
index ce45af50983a2a5e35823dbd1e632aa425ab3560..431d7d894bf797f5f05eff4ef87a964f8be7fdb5 100644 (file)
@@ -352,3 +352,5 @@ obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
 obj-$(CONFIG_BITS_TEST) += test_bits.o
+
+obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
diff --git a/lib/devmem_is_allowed.c b/lib/devmem_is_allowed.c
new file mode 100644 (file)
index 0000000..c0d67c5
--- /dev/null
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * A generic version of devmem_is_allowed.
+ *
+ * Based on arch/arm64/mm/mmap.c
+ *
+ * Copyright (C) 2020 Google, Inc.
+ * Copyright (C) 2012 ARM Ltd.
+ */
+
+#include <linux/mm.h>
+#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.  We mimic x86 here by
+ * disallowing 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;
+}