Skip to content

Commit 01c9749

Browse files
committed
Merge: Powerpc Enable KFENCE support
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5195 Description: Powerpc Enable KFENCE support JIRA: https://issues.redhat.com/browse/RHEL-52738 Build Info: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=64265726 Tested: Verified Brew build test kernel RPMs Signed-off-by: Mamatha Inamdar <[email protected]> commit a5edf98 Author: Nicholas Miehlbradt <[email protected]> Date: Mon Sep 26 07:57:26 2022 +0000 powerpc/64s: Enable KFENCE on book3s64 KFENCE support was added for ppc32 in commit 90cbac0 ("powerpc: Enable KFENCE for PPC32"). Enable KFENCE on ppc64 architecture with hash and radix MMUs. It uses the same mechanism as debug pagealloc to protect/unprotect pages. All KFENCE kunit tests pass on both MMUs. KFENCE memory is initially allocated using memblock but is later marked as SLAB allocated. This necessitates the change to __pud_free to ensure that the KFENCE pages are freed appropriately. Based on previous work by Christophe Leroy and Jordan Niethe. Signed-off-by: Nicholas Miehlbradt <[email protected]> Reviewed-by: Russell Currey <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mamatha Inamdar <[email protected]> Approved-by: Steve Best <[email protected]> Approved-by: Rafael Aquini <[email protected]> Approved-by: CKI KWF Bot <[email protected]> Merged-by: Rado Vrbovsky <[email protected]>
2 parents b1f323e + 5808e6e commit 01c9749

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

arch/powerpc/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ config PPC
185185
select HAVE_ARCH_JUMP_LABEL_RELATIVE
186186
select HAVE_ARCH_KASAN if PPC32 && PPC_PAGE_SHIFT <= 14
187187
select HAVE_ARCH_KASAN_VMALLOC if PPC32 && PPC_PAGE_SHIFT <= 14
188-
select HAVE_ARCH_KFENCE if PPC_BOOK3S_32 || PPC_8xx || 40x
188+
select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOC
189189
select HAVE_ARCH_KGDB
190190
select HAVE_ARCH_MMAP_RND_BITS
191191
select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT

arch/powerpc/include/asm/book3s/64/pgalloc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ static inline void __pud_free(pud_t *pud)
113113

114114
/*
115115
* Early pud pages allocated via memblock allocator
116-
* can't be directly freed to slab
116+
* can't be directly freed to slab. KFENCE pages have
117+
* both reserved and slab flags set so need to be freed
118+
* kmem_cache_free.
117119
*/
118-
if (PageReserved(page))
120+
if (PageReserved(page) && !PageSlab(page))
119121
free_reserved_page(page);
120122
else
121123
kmem_cache_free(PGT_CACHE(PUD_CACHE_INDEX), pud);

arch/powerpc/include/asm/book3s/64/pgtable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ static inline void vmemmap_remove_mapping(unsigned long start,
10551055
}
10561056
#endif
10571057

1058-
#ifdef CONFIG_DEBUG_PAGEALLOC
1058+
#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KFENCE)
10591059
static inline void __kernel_map_pages(struct page *page, int numpages, int enable)
10601060
{
10611061
if (radix_enabled())

arch/powerpc/include/asm/kfence.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,25 @@
1111
#include <linux/mm.h>
1212
#include <asm/pgtable.h>
1313

14+
#ifdef CONFIG_PPC64_ELF_ABI_V1
15+
#define ARCH_FUNC_PREFIX "."
16+
#endif
17+
1418
static inline bool arch_kfence_init_pool(void)
1519
{
1620
return true;
1721
}
1822

23+
#ifdef CONFIG_PPC64
24+
static inline bool kfence_protect_page(unsigned long addr, bool protect)
25+
{
26+
struct page *page = virt_to_page(addr);
27+
28+
__kernel_map_pages(page, 1, !protect);
29+
30+
return true;
31+
}
32+
#else
1933
static inline bool kfence_protect_page(unsigned long addr, bool protect)
2034
{
2135
pte_t *kpte = virt_to_kpte(addr);
@@ -29,5 +43,6 @@ static inline bool kfence_protect_page(unsigned long addr, bool protect)
2943

3044
return true;
3145
}
46+
#endif
3247

3348
#endif /* __ASM_POWERPC_KFENCE_H */

arch/powerpc/mm/book3s64/hash_utils.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
424424
break;
425425

426426
cond_resched();
427-
if (debug_pagealloc_enabled() &&
427+
if (debug_pagealloc_enabled_or_kfence() &&
428428
(paddr >> PAGE_SHIFT) < linear_map_hash_count)
429429
linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
430430
}
@@ -773,7 +773,7 @@ static void __init htab_init_page_sizes(void)
773773
bool aligned = true;
774774
init_hpte_page_sizes();
775775

776-
if (!debug_pagealloc_enabled()) {
776+
if (!debug_pagealloc_enabled_or_kfence()) {
777777
/*
778778
* Pick a size for the linear mapping. Currently, we only
779779
* support 16M, 1M and 4K which is the default
@@ -1061,7 +1061,7 @@ static void __init htab_initialize(void)
10611061

10621062
prot = pgprot_val(PAGE_KERNEL);
10631063

1064-
if (debug_pagealloc_enabled()) {
1064+
if (debug_pagealloc_enabled_or_kfence()) {
10651065
linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
10661066
linear_map_hash_slots = memblock_alloc_try_nid(
10671067
linear_map_hash_count, 1, MEMBLOCK_LOW_LIMIT,
@@ -1983,7 +1983,7 @@ long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
19831983
return slot;
19841984
}
19851985

1986-
#ifdef CONFIG_DEBUG_PAGEALLOC
1986+
#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KFENCE)
19871987
static DEFINE_SPINLOCK(linear_map_hash_lock);
19881988

19891989
static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
@@ -2056,7 +2056,7 @@ void hash__kernel_map_pages(struct page *page, int numpages, int enable)
20562056
}
20572057
local_irq_restore(flags);
20582058
}
2059-
#endif /* CONFIG_DEBUG_PAGEALLOC */
2059+
#endif /* CONFIG_DEBUG_PAGEALLOC || CONFIG_KFENCE */
20602060

20612061
void hash__setup_initial_memory_limit(phys_addr_t first_memblock_base,
20622062
phys_addr_t first_memblock_size)

arch/powerpc/mm/book3s64/radix_pgtable.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#include <trace/events/thp.h>
3636

37+
#include <mm/mmu_decl.h>
38+
3739
unsigned int mmu_base_pid;
3840
unsigned long radix_mem_block_size __ro_after_init;
3941

@@ -300,7 +302,7 @@ static int __meminit create_physical_mapping(unsigned long start,
300302
int psize;
301303
unsigned long max_mapping_size = radix_mem_block_size;
302304

303-
if (debug_pagealloc_enabled())
305+
if (debug_pagealloc_enabled_or_kfence())
304306
max_mapping_size = PAGE_SIZE;
305307

306308
start = ALIGN(start, PAGE_SIZE);
@@ -938,7 +940,7 @@ void __meminit radix__vmemmap_remove_mapping(unsigned long start, unsigned long
938940
#endif
939941
#endif
940942

941-
#ifdef CONFIG_DEBUG_PAGEALLOC
943+
#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KFENCE)
942944
void radix__kernel_map_pages(struct page *page, int numpages, int enable)
943945
{
944946
unsigned long addr;

0 commit comments

Comments
 (0)