Skip to content

Commit edd7ab7

Browse files
committed
Merge tag 'core-mm-2020-12-14' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull kmap updates from Thomas Gleixner: "The new preemtible kmap_local() implementation: - Consolidate all kmap_atomic() internals into a generic implementation which builds the base for the kmap_local() API and make the kmap_atomic() interface wrappers which handle the disabling/enabling of preemption and pagefaults. - Switch the storage from per-CPU to per task and provide scheduler support for clearing mapping when scheduling out and restoring them when scheduling back in. - Merge the migrate_disable/enable() code, which is also part of the scheduler pull request. This was required to make the kmap_local() interface available which does not disable preemption when a mapping is established. It has to disable migration instead to guarantee that the virtual address of the mapped slot is the same across preemption. - Provide better debug facilities: guard pages and enforced utilization of the mapping mechanics on 64bit systems when the architecture allows it. - Provide the new kmap_local() API which can now be used to cleanup the kmap_atomic() usage sites all over the place. Most of the usage sites do not require the implicit disabling of preemption and pagefaults so the penalty on 64bit and 32bit non-highmem systems is removed and quite some of the code can be simplified. A wholesale conversion is not possible because some usage depends on the implicit side effects and some need to be cleaned up because they work around these side effects. The migrate disable side effect is only effective on highmem systems and when enforced debugging is enabled. On 64bit and 32bit non-highmem systems the overhead is completely avoided" * tag 'core-mm-2020-12-14' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (33 commits) ARM: highmem: Fix cache_is_vivt() reference x86/crashdump/32: Simplify copy_oldmem_page() io-mapping: Provide iomap_local variant mm/highmem: Provide kmap_local* sched: highmem: Store local kmaps in task struct x86: Support kmap_local() forced debugging mm/highmem: Provide CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP mm/highmem: Provide and use CONFIG_DEBUG_KMAP_LOCAL microblaze/mm/highmem: Add dropped #ifdef back xtensa/mm/highmem: Make generic kmap_atomic() work correctly mm/highmem: Take kmap_high_get() properly into account highmem: High implementation details and document API Documentation/io-mapping: Remove outdated blurb io-mapping: Cleanup atomic iomap mm/highmem: Remove the old kmap_atomic cruft highmem: Get rid of kmap_types.h xtensa/mm/highmem: Switch to generic kmap atomic sparc/mm/highmem: Switch to generic kmap atomic powerpc/mm/highmem: Switch to generic kmap atomic nds32/mm/highmem: Switch to generic kmap atomic ...
2 parents adb35e8 + 68061c0 commit edd7ab7

88 files changed

Lines changed: 946 additions & 1423 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/driver-api/io-mapping.rst‎

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,78 +20,72 @@ A mapping object is created during driver initialization using::
2020
mappable, while 'size' indicates how large a mapping region to
2121
enable. Both are in bytes.
2222

23-
This _wc variant provides a mapping which may only be used
24-
with the io_mapping_map_atomic_wc or io_mapping_map_wc.
23+
This _wc variant provides a mapping which may only be used with
24+
io_mapping_map_atomic_wc(), io_mapping_map_local_wc() or
25+
io_mapping_map_wc().
2526

26-
With this mapping object, individual pages can be mapped either atomically
27-
or not, depending on the necessary scheduling environment. Of course, atomic
28-
maps are more efficient::
27+
With this mapping object, individual pages can be mapped either temporarily
28+
or long term, depending on the requirements. Of course, temporary maps are
29+
more efficient. They come in two flavours::
30+
31+
void *io_mapping_map_local_wc(struct io_mapping *mapping,
32+
unsigned long offset)
2933

3034
void *io_mapping_map_atomic_wc(struct io_mapping *mapping,
3135
unsigned long offset)
3236

33-
'offset' is the offset within the defined mapping region.
34-
Accessing addresses beyond the region specified in the
35-
creation function yields undefined results. Using an offset
36-
which is not page aligned yields an undefined result. The
37-
return value points to a single page in CPU address space.
37+
'offset' is the offset within the defined mapping region. Accessing
38+
addresses beyond the region specified in the creation function yields
39+
undefined results. Using an offset which is not page aligned yields an
40+
undefined result. The return value points to a single page in CPU address
41+
space.
3842

39-
This _wc variant returns a write-combining map to the
40-
page and may only be used with mappings created by
41-
io_mapping_create_wc
43+
This _wc variant returns a write-combining map to the page and may only be
44+
used with mappings created by io_mapping_create_wc()
4245

43-
Note that the task may not sleep while holding this page
44-
mapped.
46+
Temporary mappings are only valid in the context of the caller. The mapping
47+
is not guaranteed to be globaly visible.
4548

46-
::
49+
io_mapping_map_local_wc() has a side effect on X86 32bit as it disables
50+
migration to make the mapping code work. No caller can rely on this side
51+
effect.
4752

48-
void io_mapping_unmap_atomic(void *vaddr)
53+
io_mapping_map_atomic_wc() has the side effect of disabling preemption and
54+
pagefaults. Don't use in new code. Use io_mapping_map_local_wc() instead.
55+
56+
Nested mappings need to be undone in reverse order because the mapping
57+
code uses a stack for keeping track of them::
4958

50-
'vaddr' must be the value returned by the last
51-
io_mapping_map_atomic_wc call. This unmaps the specified
52-
page and allows the task to sleep once again.
59+
addr1 = io_mapping_map_local_wc(map1, offset1);
60+
addr2 = io_mapping_map_local_wc(map2, offset2);
61+
...
62+
io_mapping_unmap_local(addr2);
63+
io_mapping_unmap_local(addr1);
64+
65+
The mappings are released with::
66+
67+
void io_mapping_unmap_local(void *vaddr)
68+
void io_mapping_unmap_atomic(void *vaddr)
5369

54-
If you need to sleep while holding the lock, you can use the non-atomic
55-
variant, although they may be significantly slower.
70+
'vaddr' must be the value returned by the last io_mapping_map_local_wc() or
71+
io_mapping_map_atomic_wc() call. This unmaps the specified mapping and
72+
undoes the side effects of the mapping functions.
5673

57-
::
74+
If you need to sleep while holding a mapping, you can use the regular
75+
variant, although this may be significantly slower::
5876

5977
void *io_mapping_map_wc(struct io_mapping *mapping,
6078
unsigned long offset)
6179

62-
This works like io_mapping_map_atomic_wc except it allows
63-
the task to sleep while holding the page mapped.
80+
This works like io_mapping_map_atomic/local_wc() except it has no side
81+
effects and the pointer is globaly visible.
6482

65-
66-
::
83+
The mappings are released with::
6784

6885
void io_mapping_unmap(void *vaddr)
6986

70-
This works like io_mapping_unmap_atomic, except it is used
71-
for pages mapped with io_mapping_map_wc.
87+
Use for pages mapped with io_mapping_map_wc().
7288

7389
At driver close time, the io_mapping object must be freed::
7490

7591
void io_mapping_free(struct io_mapping *mapping)
76-
77-
Current Implementation
78-
======================
79-
80-
The initial implementation of these functions uses existing mapping
81-
mechanisms and so provides only an abstraction layer and no new
82-
functionality.
83-
84-
On 64-bit processors, io_mapping_create_wc calls ioremap_wc for the whole
85-
range, creating a permanent kernel-visible mapping to the resource. The
86-
map_atomic and map functions add the requested offset to the base of the
87-
virtual address returned by ioremap_wc.
88-
89-
On 32-bit processors with HIGHMEM defined, io_mapping_map_atomic_wc uses
90-
kmap_atomic_pfn to map the specified page in an atomic fashion;
91-
kmap_atomic_pfn isn't really supposed to be used with device pages, but it
92-
provides an efficient mapping for this usage.
93-
94-
On 32-bit processors without HIGHMEM defined, io_mapping_map_atomic_wc and
95-
io_mapping_map_wc both use ioremap_wc, a terribly inefficient function which
96-
performs an IPI to inform all processors about the new mapping. This results
97-
in a significant performance penalty.

‎arch/alpha/include/asm/kmap_types.h‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎arch/arc/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ config LINUX_RAM_BASE
507507
config HIGHMEM
508508
bool "High Memory Support"
509509
select ARCH_DISCONTIGMEM_ENABLE
510+
select KMAP_LOCAL
510511
help
511512
With ARC 2G:2G address split, only upper 2G is directly addressable by
512513
kernel. Enable this to potentially allow access to rest of 2G and PAE

‎arch/arc/include/asm/highmem.h‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@
99
#ifdef CONFIG_HIGHMEM
1010

1111
#include <uapi/asm/page.h>
12-
#include <asm/kmap_types.h>
12+
#include <asm/kmap_size.h>
13+
14+
#define FIXMAP_SIZE PGDIR_SIZE
15+
#define PKMAP_SIZE PGDIR_SIZE
1316

1417
/* start after vmalloc area */
1518
#define FIXMAP_BASE (PAGE_OFFSET - FIXMAP_SIZE - PKMAP_SIZE)
16-
#define FIXMAP_SIZE PGDIR_SIZE /* only 1 PGD worth */
17-
#define KM_TYPE_NR ((FIXMAP_SIZE >> PAGE_SHIFT)/NR_CPUS)
18-
#define FIXMAP_ADDR(nr) (FIXMAP_BASE + ((nr) << PAGE_SHIFT))
19+
20+
#define FIX_KMAP_SLOTS (KM_MAX_IDX * NR_CPUS)
21+
#define FIX_KMAP_BEGIN (0UL)
22+
#define FIX_KMAP_END ((FIX_KMAP_BEGIN + FIX_KMAP_SLOTS) - 1)
23+
24+
#define FIXADDR_TOP (FIXMAP_BASE + (FIX_KMAP_END << PAGE_SHIFT))
25+
26+
/*
27+
* This should be converted to the asm-generic version, but of course this
28+
* is needlessly different from all other architectures. Sigh - tglx
29+
*/
30+
#define __fix_to_virt(x) (FIXADDR_TOP - ((x) << PAGE_SHIFT))
31+
#define __virt_to_fix(x) (((FIXADDR_TOP - ((x) & PAGE_MASK))) >> PAGE_SHIFT)
1932

2033
/* start after fixmap area */
2134
#define PKMAP_BASE (FIXMAP_BASE + FIXMAP_SIZE)
22-
#define PKMAP_SIZE PGDIR_SIZE
2335
#define LAST_PKMAP (PKMAP_SIZE >> PAGE_SHIFT)
2436
#define LAST_PKMAP_MASK (LAST_PKMAP - 1)
2537
#define PKMAP_ADDR(nr) (PKMAP_BASE + ((nr) << PAGE_SHIFT))
@@ -29,11 +41,13 @@
2941

3042
extern void kmap_init(void);
3143

44+
#define arch_kmap_local_post_unmap(vaddr) \
45+
local_flush_tlb_kernel_range(vaddr, vaddr + PAGE_SIZE)
46+
3247
static inline void flush_cache_kmaps(void)
3348
{
3449
flush_cache_all();
3550
}
36-
3751
#endif
3852

3953
#endif

‎arch/arc/include/asm/kmap_types.h‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎arch/arc/mm/highmem.c‎

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@
3636
* This means each only has 1 PGDIR_SIZE worth of kvaddr mappings, which means
3737
* 2M of kvaddr space for typical config (8K page and 11:8:13 traversal split)
3838
*
39-
* - fixmap anyhow needs a limited number of mappings. So 2M kvaddr == 256 PTE
40-
* slots across NR_CPUS would be more than sufficient (generic code defines
41-
* KM_TYPE_NR as 20).
39+
* - The fixed KMAP slots for kmap_local/atomic() require KM_MAX_IDX slots per
40+
* CPU. So the number of CPUs sharing a single PTE page is limited.
4241
*
4342
* - pkmap being preemptible, in theory could do with more than 256 concurrent
4443
* mappings. However, generic pkmap code: map_new_virtual(), doesn't traverse
@@ -47,48 +46,6 @@
4746
*/
4847

4948
extern pte_t * pkmap_page_table;
50-
static pte_t * fixmap_page_table;
51-
52-
void *kmap_atomic_high_prot(struct page *page, pgprot_t prot)
53-
{
54-
int idx, cpu_idx;
55-
unsigned long vaddr;
56-
57-
cpu_idx = kmap_atomic_idx_push();
58-
idx = cpu_idx + KM_TYPE_NR * smp_processor_id();
59-
vaddr = FIXMAP_ADDR(idx);
60-
61-
set_pte_at(&init_mm, vaddr, fixmap_page_table + idx,
62-
mk_pte(page, prot));
63-
64-
return (void *)vaddr;
65-
}
66-
EXPORT_SYMBOL(kmap_atomic_high_prot);
67-
68-
void kunmap_atomic_high(void *kv)
69-
{
70-
unsigned long kvaddr = (unsigned long)kv;
71-
72-
if (kvaddr >= FIXMAP_BASE && kvaddr < (FIXMAP_BASE + FIXMAP_SIZE)) {
73-
74-
/*
75-
* Because preemption is disabled, this vaddr can be associated
76-
* with the current allocated index.
77-
* But in case of multiple live kmap_atomic(), it still relies on
78-
* callers to unmap in right order.
79-
*/
80-
int cpu_idx = kmap_atomic_idx();
81-
int idx = cpu_idx + KM_TYPE_NR * smp_processor_id();
82-
83-
WARN_ON(kvaddr != FIXMAP_ADDR(idx));
84-
85-
pte_clear(&init_mm, kvaddr, fixmap_page_table + idx);
86-
local_flush_tlb_kernel_range(kvaddr, kvaddr + PAGE_SIZE);
87-
88-
kmap_atomic_idx_pop();
89-
}
90-
}
91-
EXPORT_SYMBOL(kunmap_atomic_high);
9249

9350
static noinline pte_t * __init alloc_kmap_pgtable(unsigned long kvaddr)
9451
{
@@ -108,10 +65,9 @@ void __init kmap_init(void)
10865
{
10966
/* Due to recursive include hell, we can't do this in processor.h */
11067
BUILD_BUG_ON(PAGE_OFFSET < (VMALLOC_END + FIXMAP_SIZE + PKMAP_SIZE));
68+
BUILD_BUG_ON(LAST_PKMAP > PTRS_PER_PTE);
69+
BUILD_BUG_ON(FIX_KMAP_SLOTS > PTRS_PER_PTE);
11170

112-
BUILD_BUG_ON(KM_TYPE_NR > PTRS_PER_PTE);
11371
pkmap_page_table = alloc_kmap_pgtable(PKMAP_BASE);
114-
115-
BUILD_BUG_ON(LAST_PKMAP > PTRS_PER_PTE);
116-
fixmap_page_table = alloc_kmap_pgtable(FIXMAP_BASE);
72+
alloc_kmap_pgtable(FIXMAP_BASE);
11773
}

‎arch/arm/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,7 @@ config HAVE_ARCH_PFN_VALID
14991499
config HIGHMEM
15001500
bool "High Memory Support"
15011501
depends on MMU
1502+
select KMAP_LOCAL
15021503
help
15031504
The address space of ARM processors is only 4 Gigabytes large
15041505
and it has to accommodate user address space, kernel address

‎arch/arm/include/asm/fixmap.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
#define FIXADDR_TOP (FIXADDR_END - PAGE_SIZE)
88

99
#include <linux/pgtable.h>
10-
#include <asm/kmap_types.h>
10+
#include <asm/kmap_size.h>
1111

1212
enum fixed_addresses {
1313
FIX_EARLYCON_MEM_BASE,
1414
__end_of_permanent_fixed_addresses,
1515

1616
FIX_KMAP_BEGIN = __end_of_permanent_fixed_addresses,
17-
FIX_KMAP_END = FIX_KMAP_BEGIN + (KM_TYPE_NR * NR_CPUS) - 1,
17+
FIX_KMAP_END = FIX_KMAP_BEGIN + (KM_MAX_IDX * NR_CPUS) - 1,
1818

1919
/* Support writing RO kernel text via kprobes, jump labels, etc. */
2020
FIX_TEXT_POKE0,

‎arch/arm/include/asm/highmem.h‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#ifndef _ASM_HIGHMEM_H
33
#define _ASM_HIGHMEM_H
44

5-
#include <asm/kmap_types.h>
5+
#include <asm/cachetype.h>
6+
#include <asm/fixmap.h>
67

78
#define PKMAP_BASE (PAGE_OFFSET - PMD_SIZE)
89
#define LAST_PKMAP PTRS_PER_PTE
@@ -46,19 +47,32 @@ extern pte_t *pkmap_page_table;
4647

4748
#ifdef ARCH_NEEDS_KMAP_HIGH_GET
4849
extern void *kmap_high_get(struct page *page);
49-
#else
50+
51+
static inline void *arch_kmap_local_high_get(struct page *page)
52+
{
53+
if (IS_ENABLED(CONFIG_DEBUG_HIGHMEM) && !cache_is_vivt())
54+
return NULL;
55+
return kmap_high_get(page);
56+
}
57+
#define arch_kmap_local_high_get arch_kmap_local_high_get
58+
59+
#else /* ARCH_NEEDS_KMAP_HIGH_GET */
5060
static inline void *kmap_high_get(struct page *page)
5161
{
5262
return NULL;
5363
}
54-
#endif
64+
#endif /* !ARCH_NEEDS_KMAP_HIGH_GET */
5565

56-
/*
57-
* The following functions are already defined by <linux/highmem.h>
58-
* when CONFIG_HIGHMEM is not set.
59-
*/
60-
#ifdef CONFIG_HIGHMEM
61-
extern void *kmap_atomic_pfn(unsigned long pfn);
62-
#endif
66+
#define arch_kmap_local_post_map(vaddr, pteval) \
67+
local_flush_tlb_kernel_page(vaddr)
68+
69+
#define arch_kmap_local_pre_unmap(vaddr) \
70+
do { \
71+
if (cache_is_vivt()) \
72+
__cpuc_flush_dcache_area((void *)vaddr, PAGE_SIZE); \
73+
} while (0)
74+
75+
#define arch_kmap_local_post_unmap(vaddr) \
76+
local_flush_tlb_kernel_page(vaddr)
6377

6478
#endif

‎arch/arm/include/asm/kmap_types.h‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)