Skip to contentSkip to conversation

small ioremap cleanups

PATCH In review Christoph Hellwig 3 participants 7 messages Aug 24, 2021 – Aug 26, 2021

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

Hi Andrew,

the first patch moves a little code around the vmalloc/ioremap boundary followed a bigger move by Nick earlier. The second enforces non-executable mapping on ioremap just like we do for vmap. No driver currently uses executable mappings anyway, as they should.

#1 lore raw

PATCH 1/2 mm: move ioremap_page_range to vmalloc.c

This keeps it together with the implementation, and to remove the vmap_range wrapper.

---
 include/linux/vmalloc.h |  3 ---
 mm/Makefile             |  3 ++-
 mm/ioremap.c            | 25 -------------------------
 mm/vmalloc.c            | 22 +++++++++++++++++-----
 4 files changed, 19 insertions(+), 34 deletions(-)
include/linux/vmalloc.h +0 -3
index 2644425b6dce..671d402c3778 100644
@@ -225,9 +225,6 @@ static inline bool is_vm_area_hugepages(const void *addr)
 }
 
 #ifdef CONFIG_MMU
-int vmap_range(unsigned long addr, unsigned long end,
-			phys_addr_t phys_addr, pgprot_t prot,
-			unsigned int max_page_shift);
 void vunmap_range(unsigned long addr, unsigned long end);
 static inline void set_vm_flush_reset_perms(void *addr)
 {
mm/Makefile +2 -1
index e3436741d539..0e0a5a6fe127 100644
@@ -38,7 +38,7 @@ mmu-y			:= nommu.o
 mmu-$(CONFIG_MMU)	:= highmem.o memory.o mincore.o \
 			   mlock.o mmap.o mmu_gather.o mprotect.o mremap.o \
 			   msync.o page_vma_mapped.o pagewalk.o \
-			   pgtable-generic.o rmap.o vmalloc.o ioremap.o
+			   pgtable-generic.o rmap.o vmalloc.o
 
 
 ifdef CONFIG_CROSS_MEMORY_ATTACH
@@ -128,3 +128,4 @@ obj-$(CONFIG_PTDUMP_CORE) += ptdump.o
 obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o
 obj-$(CONFIG_IO_MAPPING) += io-mapping.o
 obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
+obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
mm/ioremap.c +0 -25
index 8ee0136f8cb0..5fe598ecd9b7 100644
@@ -8,33 +8,9 @@
  */
 #include <linux/vmalloc.h>
 #include <linux/mm.h>
-#include <linux/sched.h>
 #include <linux/io.h>
 #include <linux/export.h>
-#include <asm/cacheflush.h>
 
-#include "pgalloc-track.h"
-
-#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
-static unsigned int __ro_after_init iomap_max_page_shift = BITS_PER_LONG - 1;
-
-static int __init set_nohugeiomap(char *str)
-{
-	iomap_max_page_shift = PAGE_SHIFT;
-	return 0;
-}
-early_param("nohugeiomap", set_nohugeiomap);
-#else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
-static const unsigned int iomap_max_page_shift = PAGE_SHIFT;
-#endif	/* CONFIG_HAVE_ARCH_HUGE_VMAP */
-
-int ioremap_page_range(unsigned long addr,
-		       unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
-{
-	return vmap_range(addr, end, phys_addr, prot, iomap_max_page_shift);
-}
-
-#ifdef CONFIG_GENERIC_IOREMAP
 void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot)
 {
 	unsigned long offset, vaddr;
@@ -71,4 +47,3 @@ void iounmap(volatile void __iomem *addr)
 	vunmap((void *)((unsigned long)addr & PAGE_MASK));
 }
 EXPORT_SYMBOL(iounmap);
-#endif /* CONFIG_GENERIC_IOREMAP */
mm/vmalloc.c +17 -5
index d5cd52805149..e44983fb2d15 100644
@@ -44,6 +44,19 @@
 #include "internal.h"
 #include "pgalloc-track.h"
 
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1;
+
+static int __init set_nohugeiomap(char *str)
+{
+	ioremap_max_page_shift = PAGE_SHIFT;
+	return 0;
+}
+early_param("nohugeiomap", set_nohugeiomap);
+#else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
+static const unsigned int ioremap_max_page_shift = PAGE_SHIFT;
+#endif	/* CONFIG_HAVE_ARCH_HUGE_VMAP */
+
 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
 static bool __ro_after_init vmap_allow_huge = true;
 
@@ -298,15 +311,14 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end,
 	return err;
 }
 
-int vmap_range(unsigned long addr, unsigned long end,
-			phys_addr_t phys_addr, pgprot_t prot,
-			unsigned int max_page_shift)
+int ioremap_page_range(unsigned long addr, unsigned long end,
+		phys_addr_t phys_addr, pgprot_t prot)
 {
 	int err;
 
-	err = vmap_range_noflush(addr, end, phys_addr, prot, max_page_shift);
+	err = vmap_range_noflush(addr, end, phys_addr, prot,
+				 ioremap_max_page_shift);
 	flush_cache_vmap(addr, end);
-
 	return err;
 }
 
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/2] mm: move ioremap_page_range to vmalloc.c

I love your patch! Yet something to improve:

Hi Christoph,

I love your patch! Yet something to improve:

[auto build test ERROR on v5.14-rc7]
[cannot apply to hnaz-linux-mm/master next-20210824]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Christoph-Hellwig/mm-move-ioremap_page_range-to-vmalloc-c/20210824-181347
base:    e22ce8eb631bdc47a4a4ea7ecf4e4ba499db4f93
config: riscv-randconfig-s032-20210824 (attached as .config)
compiler: riscv64-linux-gcc (GCC) 11.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-348-gf0e6938b-dirty
# https://github.com/0day-ci/linux/commit/bb163539c969c4a902dbbe9e3bfd81ae00ee8c6e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Christoph-Hellwig/mm-move-ioremap_page_range-to-vmalloc-c/20210824-181347 git checkout bb163539c969c4a902dbbe9e3bfd81ae00ee8c6e
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=riscv

If you fix the issue, kindly add following tag as appropriate

All error/warnings (new ones prefixed by >>):

>> mm/ioremap.c:14:15: warning: no previous prototype for 'ioremap_prot' [-Wmissing-prototypes]
      14 | void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot)
         |               ^~~~~~~~~~~~
   In file included from arch/riscv/include/asm/io.h:136,
                    from include/linux/io.h:13,
                    from mm/ioremap.c:11:
>> include/asm-generic/io.h:959:17: error: conflicting types for 'iounmap'; have 'void(volatile void *)'

959 | #define iounmap iounmap
| ^~~~~~~
mm/ioremap.c:45:6: note: in expansion of macro 'iounmap'
45 | void iounmap(volatile void __iomem *addr)
| ^~~~~~~
include/asm-generic/io.h:959:17: note: previous definition of 'iounmap' with type 'void(void *)'
959 | #define iounmap iounmap
| ^~~~~~~
include/asm-generic/io.h:960:20: note: in expansion of macro 'iounmap'
960 | static inline void iounmap(void __iomem *addr)
| ^~~~~~~

vim +959 include/asm-generic/io.h

3f7e212df82ca04 Arnd Bergmann     2009-05-13  957  
b3ada9d0ce8d286 Greentime Hu      2017-11-22  958  #ifndef iounmap
b3ada9d0ce8d286 Greentime Hu      2017-11-22 @959  #define iounmap iounmap
b3ada9d0ce8d286 Greentime Hu      2017-11-22  960  static inline void iounmap(void __iomem *addr)
b3ada9d0ce8d286 Greentime Hu      2017-11-22  961  {
b3ada9d0ce8d286 Greentime Hu      2017-11-22  962  }
b3ada9d0ce8d286 Greentime Hu      2017-11-22  963  #endif
80b0ca98f91ddbc Christoph Hellwig 2019-08-13  964  #elif defined(CONFIG_GENERIC_IOREMAP)
ca5999fde0a1761 Mike Rapoport     2020-06-08  965  #include <linux/pgtable.h>
80b0ca98f91ddbc Christoph Hellwig 2019-08-13  966  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] mm: move ioremap_page_range to vmalloc.c

In reply to Christoph Hellwig

Excerpts from Christoph Hellwig's message of August 24, 2021 7:12 pm:

Quoted 39 lines

This keeps it together with the implementation, and to remove the vmap_range wrapper.

---
 include/linux/vmalloc.h |  3 ---
 mm/Makefile             |  3 ++-
 mm/ioremap.c            | 25 -------------------------
 mm/vmalloc.c            | 22 +++++++++++++++++-----
 4 files changed, 19 insertions(+), 34 deletions(-)
include/linux/vmalloc.h +0 -3
index 2644425b6dce..671d402c3778 100644
@@ -225,9 +225,6 @@ static inline bool is_vm_area_hugepages(const void *addr)
 }
 
 #ifdef CONFIG_MMU
-int vmap_range(unsigned long addr, unsigned long end,
-			phys_addr_t phys_addr, pgprot_t prot,
-			unsigned int max_page_shift);
 void vunmap_range(unsigned long addr, unsigned long end);
 static inline void set_vm_flush_reset_perms(void *addr)
 {
mm/Makefile +1 -1
index e3436741d539..0e0a5a6fe127 100644
@@ -38,7 +38,7 @@ mmu-y			:= nommu.o
 mmu-$(CONFIG_MMU)	:= highmem.o memory.o mincore.o \
 			   mlock.o mmap.o mmu_gather.o mprotect.o mremap.o \
 			   msync.o page_vma_mapped.o pagewalk.o \
-			   pgtable-generic.o rmap.o vmalloc.o ioremap.o
+			   pgtable-generic.o rmap.o vmalloc.o
 
 
 ifdef CONFIG_CROSS_MEMORY_ATTACH

mm/Makefile obj-$(CONFIG_PTDUMP_CORE) += ptdump.o · line 131

Quoted 4 lines
mm/Makefile +1 -0
@@ -128,3 +128,4 @@ obj-$(CONFIG_PTDUMP_CORE) += ptdump.o
 obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o
 obj-$(CONFIG_IO_MAPPING) += io-mapping.o
 obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
+obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o

Nice cleanup.

Quoted 76 lines
mm/ioremap.c +0 -25
index 8ee0136f8cb0..5fe598ecd9b7 100644
@@ -8,33 +8,9 @@
  */
 #include <linux/vmalloc.h>
 #include <linux/mm.h>
-#include <linux/sched.h>
 #include <linux/io.h>
 #include <linux/export.h>
-#include <asm/cacheflush.h>
 
-#include "pgalloc-track.h"
-
-#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
-static unsigned int __ro_after_init iomap_max_page_shift = BITS_PER_LONG - 1;
-
-static int __init set_nohugeiomap(char *str)
-{
-	iomap_max_page_shift = PAGE_SHIFT;
-	return 0;
-}
-early_param("nohugeiomap", set_nohugeiomap);
-#else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
-static const unsigned int iomap_max_page_shift = PAGE_SHIFT;
-#endif	/* CONFIG_HAVE_ARCH_HUGE_VMAP */
-
-int ioremap_page_range(unsigned long addr,
-		       unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
-{
-	return vmap_range(addr, end, phys_addr, prot, iomap_max_page_shift);
-}
-
-#ifdef CONFIG_GENERIC_IOREMAP
 void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot)
 {
 	unsigned long offset, vaddr;
@@ -71,4 +47,3 @@ void iounmap(volatile void __iomem *addr)
 	vunmap((void *)((unsigned long)addr & PAGE_MASK));
 }
 EXPORT_SYMBOL(iounmap);
-#endif /* CONFIG_GENERIC_IOREMAP */
mm/vmalloc.c +15 -3
index d5cd52805149..e44983fb2d15 100644
@@ -44,6 +44,19 @@
 #include "internal.h"
 #include "pgalloc-track.h"
 
+#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
+static unsigned int __ro_after_init ioremap_max_page_shift = BITS_PER_LONG - 1;
+
+static int __init set_nohugeiomap(char *str)
+{
+	ioremap_max_page_shift = PAGE_SHIFT;
+	return 0;
+}
+early_param("nohugeiomap", set_nohugeiomap);
+#else /* CONFIG_HAVE_ARCH_HUGE_VMAP */
+static const unsigned int ioremap_max_page_shift = PAGE_SHIFT;
+#endif	/* CONFIG_HAVE_ARCH_HUGE_VMAP */
+
 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
 static bool __ro_after_init vmap_allow_huge = true;
 
@@ -298,15 +311,14 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end,
 	return err;
 }
 
-int vmap_range(unsigned long addr, unsigned long end,
-			phys_addr_t phys_addr, pgprot_t prot,
-			unsigned int max_page_shift)
+int ioremap_page_range(unsigned long addr, unsigned long end,
+		phys_addr_t phys_addr, pgprot_t prot)

mm/vmalloc.c static int vmap_range_noflush(unsigned long addr, unsigned long end, · line 323

Quoted 10 lines
mm/vmalloc.c +2 -2
 {
 	int err;
 
-	err = vmap_range_noflush(addr, end, phys_addr, prot, max_page_shift);
+	err = vmap_range_noflush(addr, end, phys_addr, prot,
+				 ioremap_max_page_shift);
 	flush_cache_vmap(addr, end);
-
 	return err;
 }

I guess this is fine. I wanted to have an API that can be used to remap memory without struct page and that is not MMIO but since you removed the caller it could come back if other things make use of it.

Thanks,
Nick

#4 lore raw

PATCH 2/2 mm: don't allow executable ioremap mappings

In reply to Christoph Hellwig

There is no need to execute from iomem (and most platforms it is impossible anyway), so add the pgprot_nx() call similar to vmap.

---
 mm/vmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmalloc.c +1 -1
index e44983fb2d15..3055f04b486b 100644
@@ -316,7 +316,7 @@ int ioremap_page_range(unsigned long addr, unsigned long end,
 {
 	int err;
 
-	err = vmap_range_noflush(addr, end, phys_addr, prot,
+	err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
 				 ioremap_max_page_shift);
 	flush_cache_vmap(addr, end);
 	return err;
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH 2/2] mm: don't allow executable ioremap mappings

Excerpts from Christoph Hellwig's message of August 24, 2021 7:12 pm:

Quoted 13 lines

There is no need to execute from iomem (and most platforms it is impossible anyway), so add the pgprot_nx() call similar to vmap.

---
 mm/vmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmalloc.c +0 -0
index e44983fb2d15..3055f04b486b 100644

mm/vmalloc.c int ioremap_page_range(unsigned long addr, unsigned long end, · line 320

Quoted 6 lines
mm/vmalloc.c +1 -1
@@ -316,7 +316,7 @@ int ioremap_page_range(unsigned long addr, unsigned long end,
 {
 	int err;
 
-	err = vmap_range_noflush(addr, end, phys_addr, prot,
+	err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
 				 ioremap_max_page_shift);

I can't see why this is a problem. powerpcs can but it seems like a bad idea anyway.

Any point to a WARN_ON or return -EINVAL? Hmm, maybe that doesn't work for archs that don't support NX. We could add a check for ones that do support it though... But that's for another patch.

Thanks,
Nick

#6 lore raw

Re: [PATCH 2/2] mm: don't allow executable ioremap mappings

Quoted 6 lines · Nicholas Piggin

I can't see why this is a problem. powerpcs can but it seems like a bad idea anyway.

Any point to a WARN_ON or return -EINVAL? Hmm, maybe that doesn't work for archs that don't support NX. We could add a check for ones that do support it though... But that's for another patch.

This is the same as we do for regular vmap. I can't remember why we decided on this particular approach, as it's been a while.