incoming
Subsystems (8)
Andrew Morton
maintainer6 fixes based on 2187f215ebaac73ddbd814696d7c7fa34f0c3de0:
Andrey Ryabinin <[email protected]>:
kasan: fix crashes on access to memory mapped by vm_map_ram()
Daniel Axtens <[email protected]>:
mm/memory.c: add apply_to_existing_page_range() helper
kasan: use apply_to_existing_page_range() for releasing vmalloc shadow
kasan: don't assume percpu shadow allocations will succeed
Yang Shi <[email protected]>:
mm: vmscan: protect shrinker idr replace with CONFIG_MEMCG
Changbin Du <[email protected]>:
lib/Kconfig.debug: fix some messed up configurations
include/linux/kasan.h | 15 +++-- include/linux/mm.h | 3 + lib/Kconfig.debug | 100 ++++++++++++++++++------------------ mm/kasan/common.c | 36 ++++++++----- mm/memory.c | 136 ++++++++++++++++++++++++++++++++++---------------- mm/vmalloc.c | 133 ++++++++++++++++++++++++++++-------------------- mm/vmscan.c | 2 7 files changed, 260 insertions(+), 165 deletions(-)
Andrew Morton
maintainerPATCH 1/6 kasan: fix crashes on access to memory mapped by vm_map_ram()
From: Andrey Ryabinin <[email protected]>
Subject: kasan: fix crashes on access to memory mapped by vm_map_ram()
With CONFIG_KASAN_VMALLOC=y any use of memory obtained via vm_map_ram() will crash because there is no shadow backing that memory.
Instead of sprinkling additional kasan_populate_vmalloc() calls all over the vmalloc code, move it into alloc_vmap_area(). This will fix vm_map_ram() and simplify the code a bit.
---
include/linux/kasan.h | 15 ++++--- mm/kasan/common.c | 27 ++++++++---- mm/vmalloc.c | 85 ++++++++++++++++++---------------------- 3 files changed, 67 insertions(+), 60 deletions(-)
include/linux/kasan.h renamed from include/linux/kasan.h~kasan-fix-crashes-on-access-to-memory-mapped-by-vm_map_ram +9 -6
@@ -205,20 +205,23 @@ static inline void *kasan_reset_tag(cons#endif /* CONFIG_KASAN_SW_TAGS */ #ifdef CONFIG_KASAN_VMALLOC -int kasan_populate_vmalloc (unsigned long requested_size, -struct vm_struct *area); -void kasan_poison_vmalloc (void *start,unsigned long size); +int kasan_populate_vmalloc (unsigned long addr,unsigned long size); +void kasan_poison_vmalloc (const void *start,unsigned long size); +void kasan_unpoison_vmalloc (const void *start,unsigned long size);void kasan_release_vmalloc (unsigned long start,unsigned long end,unsigned long free_region_start,unsigned long free_region_end);#else -static inline int kasan_populate_vmalloc (unsigned long requested_size, -struct vm_struct *area) +static inline int kasan_populate_vmalloc (unsigned long start, +unsigned long size) {return 0 ; } -static inline void kasan_poison_vmalloc (void *start,unsigned long size) {} +static inline void kasan_poison_vmalloc (const void *start,unsigned long size) +{ } +static inline void kasan_unpoison_vmalloc (const void *start,unsigned long size) +{ } static inline void kasan_release_vmalloc(unsigned long start, unsigned long end, unsigned long free_region_start,
mm/kasan/common.c renamed from mm/kasan/common.c~kasan-fix-crashes-on-access-to-memory-mapped-by-vm_map_ram +18 -9
@@ -778,15 +778,17 @@ static int kasan_populate_vmalloc_pte(ptreturn 0 ; } -int kasan_populate_vmalloc (unsigned long requested_size,struct vm_struct *area) +int kasan_populate_vmalloc (unsigned long addr,unsigned long size) {unsigned long shadow_start, shadow_end;int ret; - shadow_start = (unsigned long )kasan_mem_to_shadow (area->addr); +if (!is_vmalloc_or_module_addr ((void *)addr)) +return 0 ; + + shadow_start = (unsigned long )kasan_mem_to_shadow ((void *)addr); shadow_start =ALIGN_DOWN (shadow_start,PAGE_SIZE ); - shadow_end = (unsigned long )kasan_mem_to_shadow (area->addr + - area->size); + shadow_end = (unsigned long )kasan_mem_to_shadow ((void *)addr + size); shadow_end =ALIGN (shadow_end,PAGE_SIZE ); ret = apply_to_page_range(&init_mm, shadow_start,
@@ -797,10 +799,6 @@ int kasan_populate_vmalloc(unsigned longflush_cache_vmap (shadow_start, shadow_end); -kasan_unpoison_shadow (area->addr, requested_size); - - area->flags |=VM_KASAN ; -/* * We need to be careful about inter-cpu effects here. Consider: *
@@ -843,12 +841,23 @@ int kasan_populate_vmalloc(unsigned long* Poison the shadow for a vmalloc region. Called as part of the * freeing process at the time the region is freed. */ -void kasan_poison_vmalloc (void *start,unsigned long size) +void kasan_poison_vmalloc (const void *start,unsigned long size) { +if (!is_vmalloc_or_module_addr (start)) +return ; + size =round_up (size,KASAN_SHADOW_SCALE_SIZE );kasan_poison_shadow (start, size,KASAN_VMALLOC_INVALID ); } +void kasan_unpoison_vmalloc (const void *start,unsigned long size) +{ +if (!is_vmalloc_or_module_addr (start)) +return ; + +kasan_unpoison_shadow (start, size); +} +static int kasan_depopulate_vmalloc_pte (pte_t *ptep,unsigned long addr,void *unused) {
mm/vmalloc.c renamed from mm/vmalloc.c~kasan-fix-crashes-on-access-to-memory-mapped-by-vm_map_ram +40 -45
164 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 2/6 mm/memory.c: add apply_to_existing_page_range() helper
In reply to Andrew MortonFrom: Daniel Axtens <[email protected]>
Subject: mm/memory.c: add apply_to_existing_page_range() helper
apply_to_page_range() takes an address range, and if any parts of it are not covered by the existing page table hierarchy, it allocates memory to fill them in.
In some use cases, this is not what we want - we want to be able to operate exclusively on PTEs that are already in the tables.
Add apply_to_existing_page_range() for this. Adjust the walker functions for apply_to_page_range to take 'create', which switches them between the old and new modes.
This will be used in KASAN vmalloc.
---
include/linux/mm.h | 3 mm/memory.c | 136 +++++++++++++++++++++++++++++-------------- 2 files changed, 97 insertions(+), 42 deletions(-)
include/linux/mm.h renamed from include/linux/mm.h~mm-add-apply_to_existing_pages-helper +3 -0
@@ -2621,6 +2621,9 @@ static inline int vm_fault_to_errno(vm_ftypedef int (*pte_fn_t )(pte_t *pte,unsigned long addr,void *data);extern int apply_to_page_range (struct mm_struct *mm,unsigned long address,unsigned long size,pte_fn_t fn,void *data); +extern int apply_to_existing_page_range (struct mm_struct *mm, +unsigned long address,unsigned long size, +pte_fn_t fn,void *data);#ifdef CONFIG_PAGE_POISONING extern bool page_poisoning_enabled (void );
mm/memory.c renamed from mm/memory.c~mm-add-apply_to_existing_pages-helper +94 -42
213 lines not on this page · raw message
_
Andrew Morton
maintainerPATCH 3/6 kasan: use apply_to_existing_page_range() for releasing vmalloc shadow
In reply to Andrew MortonFrom: Daniel Axtens <[email protected]>
Subject: kasan: use apply_to_existing_page_range() for releasing vmalloc shadow
kasan_release_vmalloc uses apply_to_page_range to release vmalloc shadow. Unfortunately, apply_to_page_range can allocate memory to fill in page table entries, which is not what we want.
Also, kasan_release_vmalloc is called under free_vmap_area_lock, so if apply_to_page_range does allocate memory, we get a sleep in atomic bug:
BUG: sleeping function called from invalid context at mm/page_alloc.c:46= 81 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 15087, name: Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0x199/0x216 lib/dump_stack.c:118 ___might_sleep.cold.97+0x1f5/0x238 kernel/sched/core.c:6800 __might_sleep+0x95/0x190 kernel/sched/core.c:6753 prepare_alloc_pages mm/page_alloc.c:4681 [inline] __alloc_pages_nodemask+0x3cd/0x890 mm/page_alloc.c:4730 alloc_pages_current+0x10c/0x210 mm/mempolicy.c:2211 alloc_pages include/linux/gfp.h:532 [inline] __get_free_pages+0xc/0x40 mm/page_alloc.c:4786 __pte_alloc_one_kernel include/asm-generic/pgalloc.h:21 [inline] pte_alloc_one_kernel include/asm-generic/pgalloc.h:33 [inline] __pte_alloc_kernel+0x1d/0x200 mm/memory.c:459 apply_to_pte_range mm/memory.c:2031 [inline] apply_to_pmd_range mm/memory.c:2068 [inline] apply_to_pud_range mm/memory.c:2088 [inline] apply_to_p4d_range mm/memory.c:2108 [inline] apply_to_page_range+0x77d/0xa00 mm/memory.c:2133 kasan_release_vmalloc+0xa7/0xc0 mm/kasan/common.c:970 __purge_vmap_area_lazy+0xcbb/0x1f30 mm/vmalloc.c:1313 try_purge_vmap_area_lazy mm/vmalloc.c:1332 [inline] free_vmap_area_noflush+0x2ca/0x390 mm/vmalloc.c:1368 free_unmap_vmap_area mm/vmalloc.c:1381 [inline] remove_vm_area+0x1cc/0x230 mm/vmalloc.c:2209 vm_remove_mappings mm/vmalloc.c:2236 [inline] __vunmap+0x223/0xa20 mm/vmalloc.c:2299 __vfree+0x3f/0xd0 mm/vmalloc.c:2356 __vmalloc_area_node mm/vmalloc.c:2507 [inline] __vmalloc_node_range+0x5d5/0x810 mm/vmalloc.c:2547 __vmalloc_node mm/vmalloc.c:2607 [inline] __vmalloc_node_flags mm/vmalloc.c:2621 [inline] vzalloc+0x6f/0x80 mm/vmalloc.c:2666 alloc_one_pg_vec_page net/packet/af_packet.c:4233 [inline] alloc_pg_vec net/packet/af_packet.c:4258 [inline] packet_set_ring+0xbc0/0x1b50 net/packet/af_packet.c:4342 packet_setsockopt+0xed7/0x2d90 net/packet/af_packet.c:3695 __sys_setsockopt+0x29b/0x4d0 net/socket.c:2117 __do_sys_setsockopt net/socket.c:2133 [inline] __se_sys_setsockopt net/socket.c:2130 [inline] __x64_sys_setsockopt+0xbe/0x150 net/socket.c:2130 do_syscall_64+0xfa/0x780 arch/x86/entry/common.c:294 entry_SYSCALL_64_after_hwframe+0x49/0xbe
Switch to using the apply_to_existing_page_range() helper instead, which won't allocate memory.
ow memory")
---
mm/kasan/common.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
mm/kasan/common.c renamed from mm/kasan/common.c~kasan-use-apply_to_existing_pages-for-releasing-vmalloc-shadow +6 -3
@@ -957,6 +957,7 @@ void kasan_release_vmalloc(unsigned long {void *shadow_start, *shadow_end;unsigned long region_start, region_end; +unsigned long size; region_start =ALIGN (start,PAGE_SIZE *KASAN_SHADOW_SCALE_SIZE ); region_end =ALIGN_DOWN (end,PAGE_SIZE *KASAN_SHADOW_SCALE_SIZE );
@@ -979,9 +980,11 @@ void kasan_release_vmalloc(unsigned long shadow_end =kasan_mem_to_shadow ((void *)region_end);if (shadow_end > shadow_start) { -apply_to_page_range (&init_mm, (unsigned long )shadow_start, - (unsigned long )(shadow_end - shadow_start), - kasan_depopulate_vmalloc_pte,NULL ); + size = shadow_end - shadow_start; +apply_to_existing_page_range (&init_mm, + (unsigned long )shadow_start, + size, kasan_depopulate_vmalloc_pte, +NULL );flush_tlb_kernel_range ((unsigned long )shadow_start, (unsigned long )shadow_end); }
_
Andrew Morton
maintainerPATCH 4/6 kasan: don't assume percpu shadow allocations will succeed
In reply to Andrew MortonFrom: Daniel Axtens <[email protected]>
Subject: kasan: don't assume percpu shadow allocations will succeed
syzkaller and the fault injector showed that I was wrong to assume that we could ignore percpu shadow allocation failures.
Handle failures properly. Merge all the allocated areas back into the fre= e
list and release the shadow, then clean up and return NULL. The shadow is released unconditionally, which relies upon the fact that the release function is able to tolerate pages not being present.
Also clean up shadows in the recovery path - currently they are not released, which leaks a bit of memory.
ow memory")
---
mm/vmalloc.c | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-)
mm/vmalloc.c renamed from mm/vmalloc.c~kasan-dont-assume-percpu-shadow-allocations-will-succeed +38 -10
@@ -3288,7 +3288,7 @@ struct vm_struct **pcpu_get_vm_areas(construct vmap_area **vas, *va;struct vm_struct **vms;int area, area2, last_area, term_area; -unsigned long base, start, size, end, last_end; +unsigned long base, start, size, end, last_end, orig_start, orig_end;bool purged = false;enum fit_type type;
@@ -3418,6 +3418,15 @@ retry:spin_unlock (&free_vmap_area_lock); +/* populate the kasan shadow space */ +for (area =0 ; area < nr_vms; area++) { +if (kasan_populate_vmalloc (vas[area]->va_start, sizes[area])) + goto err_free_shadow; + +kasan_unpoison_vmalloc ((void *)vas[area]->va_start, + sizes[area]); + } +/* insert all vm's */ spin_lock (&vmap_area_lock);for (area =0 ; area < nr_vms; area++) {
@@ -3428,13 +3437,6 @@ retry: }spin_unlock (&vmap_area_lock); -/* populate the shadow space outside of the lock */ -for (area =0 ; area < nr_vms; area++) { -/* assume success here */ -kasan_populate_vmalloc (vas[area]->va_start, sizes[area]); -kasan_unpoison_vmalloc ((void *)vms[area]->addr, sizes[area]); - } -kfree (vas);return vms;
@@ -3446,8 +3448,12 @@ recovery:* and when pcpu_get_vm_areas() is success. */ while (area--) { -merge_or_add_vmap_area (vas[area], &free_vmap_area_root, - &free_vmap_area_list); + orig_start = vas[area]->va_start; + orig_end = vas[area]->va_end; + va =merge_or_add_vmap_area (vas[area], &free_vmap_area_root, + &free_vmap_area_list); +kasan_release_vmalloc (orig_start, orig_end, + va->va_start, va->va_end); vas[area] =NULL ; }
@@ -3482,6 +3488,28 @@ err_free2:kfree (vas);kfree (vms);return NULL ; + +err_free_shadow: +spin_lock (&free_vmap_area_lock); +/* +* We release all the vmalloc shadows, even the ones for regions that +* hadn't been successfully added. This relies on kasan_release_vmalloc +* being able to tolerate this case. +*/ +for (area =0 ; area < nr_vms; area++) { + orig_start = vas[area]->va_start; + orig_end = vas[area]->va_end; + va =merge_or_add_vmap_area (vas[area], &free_vmap_area_root, + &free_vmap_area_list); +kasan_release_vmalloc (orig_start, orig_end, + va->va_start, va->va_end); + vas[area] =NULL ; +kfree (vms[area]); + } +spin_unlock (&free_vmap_area_lock); +kfree (vas); +kfree (vms); +return NULL ; }/**
_
Andrew Morton
maintainerPATCH 5/6 mm: vmscan: protect shrinker idr replace with CONFIG_MEMCG
In reply to Andrew MortonFrom: Yang Shi <[email protected]>
Subject: mm: vmscan: protect shrinker idr replace with CONFIG_MEMCG
Since commit 0a432dcbeb32edc ("mm: shrinker: make shrinker not depend on memcg kmem"), shrinkers' idr is protected by CONFIG_MEMCG instead of CONFIG_MEMCG_KMEM, so it makes no sense to protect shrinker idr replace with CONFIG_MEMCG_KMEM.
And in the CONFIG_MEMCG && CONFIG_SLOB case, shrinker_idr contains only shrinker, and it is deferred_split_shrinker. But it is never actually called, since idr_replace() is never compiled due to the wrong #ifdef. The deferred_split_shrinker all the time is staying in half-registered state, and it's never called for subordinate mem cgroups.
---
mm/vmscan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-protect-shrinker-idr-replace-with-config_memcg +1 -1
@@ -387,7 +387,7 @@ void register_shrinker_prepared(struct s {down_write (&shrinker_rwsem);list_add_tail (&shrinker->list, &shrinker_list); -#ifdef CONFIG_MEMCG_KMEM +#ifdef CONFIG_MEMCG if (shrinker->flags &SHRINKER_MEMCG_AWARE )idr_replace (&shrinker_idr, shrinker, shrinker->id);#endif
_
Andrew Morton
maintainerPATCH 6/6 lib/Kconfig.debug: fix some messed up configurations
In reply to Andrew MortonFrom: Changbin Du <[email protected]>
Subject: lib/Kconfig.debug: fix some messed up configurations
Some configuration items are messed up during conflict resolving. For example, STRICT_DEVMEM should not in testing menu, but kunit should. This patch fixes all of them.
---
lib/Kconfig.debug | 100 ++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 49 deletions(-)
lib/Kconfig.debug renamed from lib/Kconfig.debug~lib-kconfigdebug-fix-some-messed-up-configurations +51 -49
@@ -1483,6 +1483,55 @@ config PROVIDE_OHCI1394_DMA_INIT See Documentation/debugging-via-ohci1394.txt for more information. +source "samples/Kconfig" + +config ARCH_HAS_DEVMEM_IS_ALLOWED + bool + +config STRICT_DEVMEM + bool "Filter access to /dev/mem" + depends on MMU && DEVMEM + depends on ARCH_HAS_DEVMEM_IS_ALLOWED + default y if PPC || X86 || ARM64 + help + If this option is disabled, you allow userspace (root) access to all + of memory, including kernel and userspace memory. Accidental + access to this is obviously disastrous, but specific access can + be used by people debugging the kernel. Note that with PAT support + enabled, even in this case there are restrictions on /dev/mem + use due to the cache aliasing requirements. + + If this option is switched on, and IO_STRICT_DEVMEM=n, the /dev/mem + file only allows userspace access to PCI space and the BIOS code and + data regions. This is sufficient for dosemu and X and all common + users of /dev/mem. + + If in doubt, say Y. + +config IO_STRICT_DEVMEM + bool "Filter I/O access to /dev/mem" + depends on STRICT_DEVMEM + help + If this option is disabled, you allow userspace (root) access to all + io-memory regardless of whether a driver is actively using that + range. Accidental access to this is obviously disastrous, but + specific access can be used by people debugging kernel drivers. + + If this option is switched on, the /dev/mem file only allows + userspace access to *idle* io-memory ranges (see /proc/iomem) This + may break traditional users of /dev/mem (dosemu, legacy X, etc...) + if the driver using a given range cannot be disabled. + + If in doubt, say Y. + +menu "$(SRCARCH) Debugging" + +source "arch/$(SRCARCH)/Kconfig.debug" + +endmenu + +menu "Kernel Testing and Coverage" + source "lib/kunit/Kconfig" config NOTIFIER_ERROR_INJECTION
@@ -1643,10 +1692,6 @@ config FAULT_INJECTION_STACKTRACE_FILTER help Provide stacktrace filter for fault-injection capabilities -endmenu # "Kernel Testing and Coverage" - -menu "Kernel Testing and Coverage" - config ARCH_HAS_KCOV bool help
@@ -2130,52 +2175,7 @@ config MEMTEST memtest=17, mean do 17 test patterns. If you are unsure how to answer this question, answer N. -source "samples/Kconfig" - -config ARCH_HAS_DEVMEM_IS_ALLOWED - bool - -config STRICT_DEVMEM - bool "Filter access to /dev/mem" - depends on MMU && DEVMEM - depends on ARCH_HAS_DEVMEM_IS_ALLOWED - default y if PPC || X86 || ARM64 - ---help--- - If this option is disabled, you allow userspace (root) access to all - of memory, including kernel and userspace memory. Accidental - access to this is obviously disastrous, but specific access can - be used by people debugging the kernel. Note that with PAT support - enabled, even in this case there are restrictions on /dev/mem - use due to the cache aliasing requirements. - - If this option is switched on, and IO_STRICT_DEVMEM=n, the /dev/mem - file only allows userspace access to PCI space and the BIOS code and - data regions. This is sufficient for dosemu and X and all common - users of /dev/mem. - - If in doubt, say Y. -config IO_STRICT_DEVMEM - bool "Filter I/O access to /dev/mem" - depends on STRICT_DEVMEM - ---help--- - If this option is disabled, you allow userspace (root) access to all - io-memory regardless of whether a driver is actively using that - range. Accidental access to this is obviously disastrous, but - specific access can be used by people debugging kernel drivers. - - If this option is switched on, the /dev/mem file only allows - userspace access to *idle* io-memory ranges (see /proc/iomem) This - may break traditional users of /dev/mem (dosemu, legacy X, etc...) - if the driver using a given range cannot be disabled. - - If in doubt, say Y. - -menu "$(SRCARCH) Debugging" - -source "arch/$(SRCARCH)/Kconfig.debug" - -endmenu config HYPERV_TESTING bool "Microsoft Hyper-V driver testing"
@@ -2184,4 +2184,6 @@ config HYPERV_TESTING help Select this option to enable Hyper-V vmbus testing. +endmenu # "Kernel Testing and Coverage" + endmenu # Kernel hacking
_