Skip to content

Commit 92281de

Browse files
committed
arch: introduce memremap()
Existing users of ioremap_cache() are mapping memory that is known in advance to not have i/o side effects. These users are forced to cast away the __iomem annotation, or otherwise neglect to fix the sparse errors thrown when dereferencing pointers to this memory. Provide memremap() as a non __iomem annotated ioremap_*() in the case when ioremap is otherwise a pointer to cacheable memory. Empirically, ioremap_<cacheable-type>() call sites are seeking memory-like semantics (e.g. speculative reads, and prefetching permitted). memremap() is a break from the ioremap implementation pattern of adding a new memremap_<type>() for each mapping type and having silent compatibility fall backs. Instead, the implementation defines flags that are passed to the central memremap() and if a mapping type is not supported by an arch memremap returns NULL. We introduce a memremap prototype as a trivial wrapper of ioremap_cache() and ioremap_wt(). Later, once all ioremap_cache() and ioremap_wt() usage has been removed from drivers we teach archs to implement arch_memremap() with the ability to strictly enforce the mapping type. Cc: Arnd Bergmann <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Dan Williams <[email protected]>
1 parent 92b19ff commit 92281de

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

arch/ia64/include/asm/io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ static inline void __iomem * ioremap_cache (unsigned long phys_addr, unsigned lo
435435
{
436436
return ioremap(phys_addr, size);
437437
}
438+
#define ioremap_cache ioremap_cache
438439

439440

440441
/*

arch/sh/include/asm/io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ ioremap_cache(phys_addr_t offset, unsigned long size)
342342
{
343343
return __ioremap_mode(offset, size, PAGE_KERNEL);
344344
}
345+
#define ioremap_cache ioremap_cache
345346

346347
#ifdef CONFIG_HAVE_IOREMAP_PROT
347348
static inline void __iomem *

arch/xtensa/include/asm/io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static inline void __iomem *ioremap_cache(unsigned long offset,
5757
else
5858
BUG();
5959
}
60+
#define ioremap_cache ioremap_cache
6061

6162
#define ioremap_wc ioremap_nocache
6263
#define ioremap_wt ioremap_nocache

include/linux/io.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,13 @@ static inline int arch_phys_wc_index(int handle)
121121
#endif
122122
#endif
123123

124+
enum {
125+
/* See memremap() kernel-doc for usage description... */
126+
MEMREMAP_WB = 1 << 0,
127+
MEMREMAP_WT = 1 << 1,
128+
};
129+
130+
void *memremap(resource_size_t offset, size_t size, unsigned long flags);
131+
void memunmap(void *addr);
132+
124133
#endif /* _LINUX_IO_H */

kernel/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ obj-$(CONFIG_JUMP_LABEL) += jump_label.o
9999
obj-$(CONFIG_CONTEXT_TRACKING) += context_tracking.o
100100
obj-$(CONFIG_TORTURE_TEST) += torture.o
101101

102+
obj-$(CONFIG_HAS_IOMEM) += memremap.o
103+
102104
$(obj)/configs.o: $(obj)/config_data.h
103105

104106
# config_data.h contains the same information as ikconfig.h but gzipped.

kernel/memremap.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright(c) 2015 Intel Corporation. All rights reserved.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of version 2 of the GNU General Public License as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful, but
9+
* WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* General Public License for more details.
12+
*/
13+
#include <linux/types.h>
14+
#include <linux/io.h>
15+
#include <linux/mm.h>
16+
17+
#ifndef ioremap_cache
18+
/* temporary while we convert existing ioremap_cache users to memremap */
19+
__weak void __iomem *ioremap_cache(resource_size_t offset, unsigned long size)
20+
{
21+
return ioremap(offset, size);
22+
}
23+
#endif
24+
25+
/**
26+
* memremap() - remap an iomem_resource as cacheable memory
27+
* @offset: iomem resource start address
28+
* @size: size of remap
29+
* @flags: either MEMREMAP_WB or MEMREMAP_WT
30+
*
31+
* memremap() is "ioremap" for cases where it is known that the resource
32+
* being mapped does not have i/o side effects and the __iomem
33+
* annotation is not applicable.
34+
*
35+
* MEMREMAP_WB - matches the default mapping for "System RAM" on
36+
* the architecture. This is usually a read-allocate write-back cache.
37+
* Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
38+
* memremap() will bypass establishing a new mapping and instead return
39+
* a pointer into the direct map.
40+
*
41+
* MEMREMAP_WT - establish a mapping whereby writes either bypass the
42+
* cache or are written through to memory and never exist in a
43+
* cache-dirty state with respect to program visibility. Attempts to
44+
* map "System RAM" with this mapping type will fail.
45+
*/
46+
void *memremap(resource_size_t offset, size_t size, unsigned long flags)
47+
{
48+
int is_ram = region_intersects(offset, size, "System RAM");
49+
void *addr = NULL;
50+
51+
if (is_ram == REGION_MIXED) {
52+
WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
53+
&offset, (unsigned long) size);
54+
return NULL;
55+
}
56+
57+
/* Try all mapping types requested until one returns non-NULL */
58+
if (flags & MEMREMAP_WB) {
59+
flags &= ~MEMREMAP_WB;
60+
/*
61+
* MEMREMAP_WB is special in that it can be satisifed
62+
* from the direct map. Some archs depend on the
63+
* capability of memremap() to autodetect cases where
64+
* the requested range is potentially in "System RAM"
65+
*/
66+
if (is_ram == REGION_INTERSECTS)
67+
addr = __va(offset);
68+
else
69+
addr = ioremap_cache(offset, size);
70+
}
71+
72+
/*
73+
* If we don't have a mapping yet and more request flags are
74+
* pending then we will be attempting to establish a new virtual
75+
* address mapping. Enforce that this mapping is not aliasing
76+
* "System RAM"
77+
*/
78+
if (!addr && is_ram == REGION_INTERSECTS && flags) {
79+
WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
80+
&offset, (unsigned long) size);
81+
return NULL;
82+
}
83+
84+
if (!addr && (flags & MEMREMAP_WT)) {
85+
flags &= ~MEMREMAP_WT;
86+
addr = ioremap_wt(offset, size);
87+
}
88+
89+
return addr;
90+
}
91+
EXPORT_SYMBOL(memremap);
92+
93+
void memunmap(void *addr)
94+
{
95+
if (is_vmalloc_addr(addr))
96+
iounmap((void __iomem *) addr);
97+
}
98+
EXPORT_SYMBOL(memunmap);

0 commit comments

Comments
 (0)