Skip to content

Commit 825c43f

Browse files
ardbiesheuveltorvalds
authored andcommitted
kmap_local: don't assume kmap PTEs are linear arrays in memory
The kmap_local conversion broke the ARM architecture, because the new code assumes that all PTEs used for creating kmaps form a linear array in memory, and uses array indexing to look up the kmap PTE belonging to a certain kmap index. On ARM, this cannot work, not only because the PTE pages may be non-adjacent in memory, but also because ARM/!LPAE interleaves hardware entries and extended entries (carrying software-only bits) in a way that is not compatible with array indexing. Fortunately, this only seems to affect configurations with more than 8 CPUs, due to the way the per-CPU kmap slots are organized in memory. Work around this by permitting an architecture to set a Kconfig symbol that signifies that the kmap PTEs do not form a lineary array in memory, and so the only way to locate the appropriate one is to walk the page tables. Link: https://lore.kernel.org/linux-arm-kernel/20211026131249.3731275-1-ardb@kernel.org/ Link: https://lkml.kernel.org/r/20211116094737.7391-1-ardb@kernel.org Fixes: 2a15ba8 ("ARM: highmem: Switch to generic kmap atomic") Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Reported-by: Quanyang Wang <quanyang.wang@windriver.com> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Acked-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d78f385 commit 825c43f

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

arch/arm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,7 @@ config HIGHMEM
14631463
bool "High Memory Support"
14641464
depends on MMU
14651465
select KMAP_LOCAL
1466+
select KMAP_LOCAL_NON_LINEAR_PTE_ARRAY
14661467
help
14671468
The address space of ARM processors is only 4 Gigabytes large
14681469
and it has to accommodate user address space, kernel address

mm/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,9 @@ config MAPPING_DIRTY_HELPERS
890890
config KMAP_LOCAL
891891
bool
892892

893+
config KMAP_LOCAL_NON_LINEAR_PTE_ARRAY
894+
bool
895+
893896
# struct io_mapping based helper. Selected by drivers that need them
894897
config IO_MAPPING
895898
bool

mm/highmem.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -503,16 +503,22 @@ static inline int kmap_local_calc_idx(int idx)
503503

504504
static pte_t *__kmap_pte;
505505

506-
static pte_t *kmap_get_pte(void)
506+
static pte_t *kmap_get_pte(unsigned long vaddr, int idx)
507507
{
508+
if (IS_ENABLED(CONFIG_KMAP_LOCAL_NON_LINEAR_PTE_ARRAY))
509+
/*
510+
* Set by the arch if __kmap_pte[-idx] does not produce
511+
* the correct entry.
512+
*/
513+
return virt_to_kpte(vaddr);
508514
if (!__kmap_pte)
509515
__kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
510-
return __kmap_pte;
516+
return &__kmap_pte[-idx];
511517
}
512518

513519
void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t prot)
514520
{
515-
pte_t pteval, *kmap_pte = kmap_get_pte();
521+
pte_t pteval, *kmap_pte;
516522
unsigned long vaddr;
517523
int idx;
518524

@@ -524,9 +530,10 @@ void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t prot)
524530
preempt_disable();
525531
idx = arch_kmap_local_map_idx(kmap_local_idx_push(), pfn);
526532
vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
527-
BUG_ON(!pte_none(*(kmap_pte - idx)));
533+
kmap_pte = kmap_get_pte(vaddr, idx);
534+
BUG_ON(!pte_none(*kmap_pte));
528535
pteval = pfn_pte(pfn, prot);
529-
arch_kmap_local_set_pte(&init_mm, vaddr, kmap_pte - idx, pteval);
536+
arch_kmap_local_set_pte(&init_mm, vaddr, kmap_pte, pteval);
530537
arch_kmap_local_post_map(vaddr, pteval);
531538
current->kmap_ctrl.pteval[kmap_local_idx()] = pteval;
532539
preempt_enable();
@@ -559,7 +566,7 @@ EXPORT_SYMBOL(__kmap_local_page_prot);
559566
void kunmap_local_indexed(void *vaddr)
560567
{
561568
unsigned long addr = (unsigned long) vaddr & PAGE_MASK;
562-
pte_t *kmap_pte = kmap_get_pte();
569+
pte_t *kmap_pte;
563570
int idx;
564571

565572
if (addr < __fix_to_virt(FIX_KMAP_END) ||
@@ -584,8 +591,9 @@ void kunmap_local_indexed(void *vaddr)
584591
idx = arch_kmap_local_unmap_idx(kmap_local_idx(), addr);
585592
WARN_ON_ONCE(addr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
586593

594+
kmap_pte = kmap_get_pte(addr, idx);
587595
arch_kmap_local_pre_unmap(addr);
588-
pte_clear(&init_mm, addr, kmap_pte - idx);
596+
pte_clear(&init_mm, addr, kmap_pte);
589597
arch_kmap_local_post_unmap(addr);
590598
current->kmap_ctrl.pteval[kmap_local_idx()] = __pte(0);
591599
kmap_local_idx_pop();
@@ -607,7 +615,7 @@ EXPORT_SYMBOL(kunmap_local_indexed);
607615
void __kmap_local_sched_out(void)
608616
{
609617
struct task_struct *tsk = current;
610-
pte_t *kmap_pte = kmap_get_pte();
618+
pte_t *kmap_pte;
611619
int i;
612620

613621
/* Clear kmaps */
@@ -634,16 +642,17 @@ void __kmap_local_sched_out(void)
634642
idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
635643

636644
addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
645+
kmap_pte = kmap_get_pte(addr, idx);
637646
arch_kmap_local_pre_unmap(addr);
638-
pte_clear(&init_mm, addr, kmap_pte - idx);
647+
pte_clear(&init_mm, addr, kmap_pte);
639648
arch_kmap_local_post_unmap(addr);
640649
}
641650
}
642651

643652
void __kmap_local_sched_in(void)
644653
{
645654
struct task_struct *tsk = current;
646-
pte_t *kmap_pte = kmap_get_pte();
655+
pte_t *kmap_pte;
647656
int i;
648657

649658
/* Restore kmaps */
@@ -663,7 +672,8 @@ void __kmap_local_sched_in(void)
663672
/* See comment in __kmap_local_sched_out() */
664673
idx = arch_kmap_local_map_idx(i, pte_pfn(pteval));
665674
addr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
666-
set_pte_at(&init_mm, addr, kmap_pte - idx, pteval);
675+
kmap_pte = kmap_get_pte(addr, idx);
676+
set_pte_at(&init_mm, addr, kmap_pte, pteval);
667677
arch_kmap_local_post_map(addr, pteval);
668678
}
669679
}

0 commit comments

Comments
 (0)