Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/io: add interface to reserve io memtype for a resource range. (v1.1)

A recent change to the mm code in:
87744ab3832b mm: fix cache mode tracking in vm_insert_mixed()

started enforcing checking the memory type against the registered list for
amixed pfn insertion mappings. It happens that the drm drivers for a number
of gpus relied on this being broken. Currently the driver only inserted
VRAM mappings into the tracking table when they came from the kernel,
and userspace mappings never landed in the table. This led to a regression
where all the mapping end up as UC instead of WC now.

I've considered a number of solutions but since this needs to be fixed
in fixes and not next, and some of the solutions were going to introduce
overhead that hadn't been there before I didn't consider them viable at
this stage. These mainly concerned hooking into the TTM io reserve APIs,
but these API have a bunch of fast paths I didn't want to unwind to add
this to.

The solution I've decided on is to add a new API like the arch_phys_wc
APIs (these would have worked but wc_del didn't take a range), and
use them from the drivers to add a WC compatible mapping to the table
for all VRAM on those GPUs. This means we can then create userspace
mapping that won't get degraded to UC.

v1.1: use CONFIG_X86_PAT + add some comments in io.h

Cc: Toshi Kani <toshi.kani@hp.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: x86@kernel.org
Cc: mcgrof@suse.com
Cc: Dan Williams <dan.j.williams@intel.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Dave Airlie <airlied@redhat.com>

+42
+6
arch/x86/include/asm/io.h
··· 351 351 #define arch_phys_wc_add arch_phys_wc_add 352 352 #endif 353 353 354 + #ifdef CONFIG_X86_PAT 355 + extern int arch_io_reserve_memtype_wc(resource_size_t start, resource_size_t size); 356 + extern void arch_io_free_memtype_wc(resource_size_t start, resource_size_t size); 357 + #define arch_io_reserve_memtype_wc arch_io_reserve_memtype_wc 358 + #endif 359 + 354 360 #endif /* _ASM_X86_IO_H */
+14
arch/x86/mm/pat.c
··· 730 730 free_memtype(start, end); 731 731 } 732 732 733 + int arch_io_reserve_memtype_wc(resource_size_t start, resource_size_t size) 734 + { 735 + enum page_cache_mode type = _PAGE_CACHE_MODE_WC; 736 + 737 + return io_reserve_memtype(start, start + size, &type); 738 + } 739 + EXPORT_SYMBOL(arch_io_reserve_memtype_wc); 740 + 741 + void arch_io_free_memtype_wc(resource_size_t start, resource_size_t size) 742 + { 743 + io_free_memtype(start, start + size); 744 + } 745 + EXPORT_SYMBOL(arch_io_free_memtype_wc); 746 + 733 747 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 734 748 unsigned long size, pgprot_t vma_prot) 735 749 {
+22
include/linux/io.h
··· 141 141 void *memremap(resource_size_t offset, size_t size, unsigned long flags); 142 142 void memunmap(void *addr); 143 143 144 + /* 145 + * On x86 PAT systems we have memory tracking that keeps track of 146 + * the allowed mappings on memory ranges. This tracking works for 147 + * all the in-kernel mapping APIs (ioremap*), but where the user 148 + * wishes to map a range from a physical device into user memory 149 + * the tracking won't be updated. This API is to be used by 150 + * drivers which remap physical device pages into userspace, 151 + * and wants to make sure they are mapped WC and not UC. 152 + */ 153 + #ifndef arch_io_reserve_memtype_wc 154 + static inline int arch_io_reserve_memtype_wc(resource_size_t base, 155 + resource_size_t size) 156 + { 157 + return 0; 158 + } 159 + 160 + static inline void arch_io_free_memtype_wc(resource_size_t base, 161 + resource_size_t size) 162 + { 163 + } 164 + #endif 165 + 144 166 #endif /* _LINUX_IO_H */