powerpc changes kernel page protections with apply_to_existing_page_range() and a callback that updates one PTE and flushes it. A request flushes the TLB once for every page, and large mappings cannot be changed at all, so huge vmalloc ranges are refused up front. On hash, only the start of a request is checked to be in the vmalloc or the I/O region. The availability of the generic CPA core makes powerpc's implementation of set_memory_*() APIs unnecessary and it can be replaced with several hooks required by the CPA core. This way the core can manage the policy decisions and the architecture only has to take care of the page table manipulation. Switch to the generic CPA core and its set_memory() entry points. For powerpc this means that: * the TLB is flushed once per request, after a ptesync on radix * a large mapping is changed in place when a request covers it entirely; a request for a part of it fails with a warning and -EINVAL, because powerpc cannot split * huge vmalloc ranges are no longer refused up front; on 8xx a 16K, 512K or 8M page changes as one * on hash, the whole request has to be in the vmalloc or the I/O region * an empty PTE in a vmalloc range is reported with a warning and -EFAULT where the old walk skipped it * requests are serialized by the core; the old walk ran without a lock * lazily unmapped vmap areas and unused kmaps are purged before a request, so that they do not keep stale mappings or TLB entries with the old protection; set_memory_*() may now sleep there The hooks decode an entry with pte_pfn() and pte_pgprot(), which work for the leaves of every level on book3s64, and write it with pte_update(), which keeps the bits the hardware updates and removes the hash page table entry where there is one. On 8xx the decode hook extends a leaf to its block with __pte_leaf_size(), and pte_update() writes the whole block. An 8M page spans two page tables, and pte_update() takes it by its first PMD entry. __kernel_map_pages() runs in the page allocator with interrupts disabled. It keeps changing the PTEs with apply_to_existing_page_range(), now with one flush per call. It was the only user of set_memory_np() and set_memory_p(), so they are removed. Signed-off-by: Mike Rapoport (Microsoft) --- arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/set_memory.h | 46 +--------- arch/powerpc/mm/pageattr.c | 160 +++++++++++++++++++--------------- 3 files changed, 94 insertions(+), 113 deletions(-) diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index c3b6cd655606..fbe3a3abeea8 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -211,6 +211,7 @@ config PPC select GENERIC_IOREMAP select GENERIC_IRQ_SHOW select GENERIC_IRQ_SHOW_LEVEL + select GENERIC_PAGEATTR select GENERIC_PCI_IOMAP if PCI select GENERIC_SMP_IDLE_THREAD select HAS_IOPORT if PCI diff --git a/arch/powerpc/include/asm/set_memory.h b/arch/powerpc/include/asm/set_memory.h index 9c8d5747755d..d049d48157a0 100644 --- a/arch/powerpc/include/asm/set_memory.h +++ b/arch/powerpc/include/asm/set_memory.h @@ -2,50 +2,6 @@ #ifndef _ASM_POWERPC_SET_MEMORY_H #define _ASM_POWERPC_SET_MEMORY_H -#define SET_MEMORY_RO 0 -#define SET_MEMORY_RW 1 -#define SET_MEMORY_NX 2 -#define SET_MEMORY_X 3 -#define SET_MEMORY_NP 4 /* Set memory non present */ -#define SET_MEMORY_P 5 /* Set memory present */ -#define SET_MEMORY_ROX 6 - -int change_memory_attr(unsigned long addr, int numpages, long action); - -static inline int __must_check set_memory_ro(unsigned long addr, int numpages) -{ - return change_memory_attr(addr, numpages, SET_MEMORY_RO); -} - -static inline int __must_check set_memory_rw(unsigned long addr, int numpages) -{ - return change_memory_attr(addr, numpages, SET_MEMORY_RW); -} - -static inline int __must_check set_memory_nx(unsigned long addr, int numpages) -{ - return change_memory_attr(addr, numpages, SET_MEMORY_NX); -} - -static inline int __must_check set_memory_x(unsigned long addr, int numpages) -{ - return change_memory_attr(addr, numpages, SET_MEMORY_X); -} - -static inline int __must_check set_memory_np(unsigned long addr, int numpages) -{ - return change_memory_attr(addr, numpages, SET_MEMORY_NP); -} - -static inline int __must_check set_memory_p(unsigned long addr, int numpages) -{ - return change_memory_attr(addr, numpages, SET_MEMORY_P); -} - -static inline int __must_check set_memory_rox(unsigned long addr, int numpages) -{ - return change_memory_attr(addr, numpages, SET_MEMORY_ROX); -} -#define set_memory_rox set_memory_rox +#include #endif diff --git a/arch/powerpc/mm/pageattr.c b/arch/powerpc/mm/pageattr.c index ac22bf28086f..a2da183aa575 100644 --- a/arch/powerpc/mm/pageattr.c +++ b/arch/powerpc/mm/pageattr.c @@ -7,6 +7,7 @@ */ #include +#include #include #include @@ -16,109 +17,132 @@ #include -static pte_basic_t pte_update_delta(pte_t *ptep, unsigned long addr, - unsigned long old, unsigned long new) +void arch_cpa_decode_leaf(struct cpa_leaf *leaf) { - return pte_update(&init_mm, addr, ptep, old & ~new, new & ~old, 0); + pte_t pte = ptep_get(leaf->kpte); + unsigned long size, nr; + pmd_t *pmdp; + + /* + * A 16K or 512K page of 8xx is a contiguous block of entries in one + * page table, each mapping its part of the page. An 8M page spans two + * tables, and pte_update() takes it by its first PMD entry. + */ + if (IS_ENABLED(CONFIG_PPC_8xx) && leaf->level == PGTABLE_LEVEL_PTE) { + size = __pte_leaf_size(pmdp_get(pmd_off_k(leaf->addr)), pte); + if (size > PAGE_SIZE && size < PMD_SIZE) { + nr = size / PAGE_SIZE; + leaf->kpte = PTR_ALIGN_DOWN(leaf->kpte, + nr * sizeof(pte_t)); + leaf->size = size; + leaf->addr &= ~(size - 1); + pte = ptep_get(leaf->kpte); + } else if (size > PMD_SIZE) { + leaf->size = size; + leaf->addr &= ~(size - 1); + pmdp = pmd_off_k(leaf->addr); + pte = ptep_get(pte_offset_kernel(pmdp, leaf->addr)); + leaf->kpte = (pte_t *)pmdp; + } + } + + leaf->pfn = pte_pfn(pte); + leaf->prot = pte_pgprot(pte); } -/* - * Updates the attributes of a page atomically. - * - * This sequence is safe against concurrent updates, and also allows updating the - * attributes of a page currently being executed or accessed. - */ -static int change_page_attr(pte_t *ptep, unsigned long addr, void *data) +void arch_cpa_modify_leaf(struct cpa_leaf *leaf, pgprot_t prot) { - long action = (long)data; - - addr &= PAGE_MASK; - /* modify the PTE bits as desired */ - switch (action) { - case SET_MEMORY_RO: - /* Don't clear DIRTY bit */ - pte_update_delta(ptep, addr, _PAGE_KERNEL_RW & ~_PAGE_DIRTY, _PAGE_KERNEL_RO); - break; - case SET_MEMORY_ROX: - /* Don't clear DIRTY bit */ - pte_update_delta(ptep, addr, _PAGE_KERNEL_RW & ~_PAGE_DIRTY, _PAGE_KERNEL_ROX); - break; - case SET_MEMORY_RW: - pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_RW); - break; - case SET_MEMORY_NX: - pte_update_delta(ptep, addr, _PAGE_KERNEL_ROX, _PAGE_KERNEL_RO); - break; - case SET_MEMORY_X: - pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_ROX); - break; - case SET_MEMORY_NP: - pte_update(&init_mm, addr, ptep, _PAGE_PRESENT, 0, 0); - break; - case SET_MEMORY_P: - pte_update(&init_mm, addr, ptep, 0, _PAGE_PRESENT, 0); - break; - default: - WARN_ON_ONCE(1); - break; - } + unsigned long old = pgprot_val(leaf->prot), new = pgprot_val(prot); + pte_update(&init_mm, leaf->addr, leaf->kpte, old & ~new, new & ~old, + leaf->size > PAGE_SIZE); +} + +void arch_cpa_flush(struct cpa_data *cpa) +{ /* See ptesync comment in radix__set_pte_at() */ if (radix_enabled()) asm volatile("ptesync": : :"memory"); - flush_tlb_kernel_range(addr, addr + PAGE_SIZE); - - return 0; + cpa_flush_tlb_kernel_range(cpa); } -int change_memory_attr(unsigned long addr, int numpages, long action) +#define CPA_OP_TRANS(old, new) { .set = __pgprot((new) & ~(old)), \ + .clr = __pgprot((old) & ~(new)) } + +static const struct cpa_op_prot cpa_ops[CPA_OP_NR] = { + /* Don't clear DIRTY bit */ + [CPA_OP_RO] = CPA_OP_TRANS(_PAGE_KERNEL_RW & ~_PAGE_DIRTY, + _PAGE_KERNEL_RO), + [CPA_OP_RW] = CPA_OP_TRANS(_PAGE_KERNEL_RO, _PAGE_KERNEL_RW), + [CPA_OP_X] = CPA_OP_TRANS(_PAGE_KERNEL_RO, _PAGE_KERNEL_ROX), + [CPA_OP_NX] = CPA_OP_TRANS(_PAGE_KERNEL_ROX, _PAGE_KERNEL_RO), + /* Don't clear DIRTY bit */ + [CPA_OP_ROX] = CPA_OP_TRANS(_PAGE_KERNEL_RW & ~_PAGE_DIRTY, + _PAGE_KERNEL_ROX), +}; + +int arch_cpa_prepare(struct cpa_data *cpa, enum cpa_op op) { - unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE); - unsigned long size = numpages * PAGE_SIZE; - - if (!numpages) - return 0; - - if (WARN_ON_ONCE(is_vmalloc_or_module_addr((void *)addr) && - is_vm_area_hugepages((void *)addr))) - return -EINVAL; - #ifdef CONFIG_PPC_BOOK3S_64 + unsigned long end = cpa->addr + cpa->numpages * PAGE_SIZE; + /* * On hash, the linear mapping is not in the Linux page table so - * apply_to_existing_page_range() will have no effect. If in the future - * the set_memory_* functions are used on the linear map this will need + * changing its protections has no effect. If in the future the + * set_memory_* functions are used on the linear map this will need * to be updated. */ - if (!radix_enabled()) { - int region = get_region_id(addr); + if (!radix_enabled() && cpa->numpages) { + int region = get_region_id(cpa->addr); + int end_region = get_region_id(end - 1); - if (WARN_ON_ONCE(region != VMALLOC_REGION_ID && region != IO_REGION_ID)) + if (WARN_ON_ONCE((region != VMALLOC_REGION_ID && + region != IO_REGION_ID) || + end_region != region)) return -EINVAL; } #endif - return apply_to_existing_page_range(&init_mm, start, size, - change_page_attr, (void *)action); + cpa_init_op(cpa, &cpa_ops[op]); + + return 0; } #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KFENCE) #ifdef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC +static int change_present(pte_t *ptep, unsigned long addr, void *data) +{ + if (data) + pte_update(&init_mm, addr, ptep, 0, _PAGE_PRESENT, 0); + else + pte_update(&init_mm, addr, ptep, _PAGE_PRESENT, 0, 0); + + return 0; +} + void __kernel_map_pages(struct page *page, int numpages, int enable) { int err; unsigned long addr = (unsigned long)page_address(page); + unsigned long size = numpages * PAGE_SIZE; if (PageHighMem(page)) return; - if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled()) + if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled()) { err = hash__kernel_map_pages(page, numpages, enable); - else if (enable) - err = set_memory_p(addr, numpages); - else - err = set_memory_np(addr, numpages); + } else { + err = apply_to_existing_page_range(&init_mm, addr, size, + change_present, + (void *)(long)enable); + + /* See ptesync comment in radix__set_pte_at() */ + if (radix_enabled()) + asm volatile("ptesync": : :"memory"); + + flush_tlb_kernel_range(addr, addr + size); + } if (err) panic("%s: changing memory protections failed\n", __func__); -- 2.53.0