1 // SPDX-License-Identifier: GPL-2.0
3 * This file contains common generic and tag-based KASAN code.
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
8 * Some code borrowed from https://github.com/xairy/kasan-prototype by
9 * Andrey Konovalov <andreyknvl@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/export.h>
18 #include <linux/interrupt.h>
19 #include <linux/init.h>
20 #include <linux/kasan.h>
21 #include <linux/kernel.h>
22 #include <linux/kmemleak.h>
23 #include <linux/linkage.h>
24 #include <linux/memblock.h>
25 #include <linux/memory.h>
27 #include <linux/module.h>
28 #include <linux/printk.h>
29 #include <linux/sched.h>
30 #include <linux/sched/task_stack.h>
31 #include <linux/slab.h>
32 #include <linux/stacktrace.h>
33 #include <linux/string.h>
34 #include <linux/types.h>
35 #include <linux/vmalloc.h>
36 #include <linux/bug.h>
37 #include <linux/uaccess.h>
39 #include <asm/cacheflush.h>
40 #include <asm/tlbflush.h>
45 static inline int in_irqentry_text(unsigned long ptr)
47 return (ptr >= (unsigned long)&__irqentry_text_start &&
48 ptr < (unsigned long)&__irqentry_text_end) ||
49 (ptr >= (unsigned long)&__softirqentry_text_start &&
50 ptr < (unsigned long)&__softirqentry_text_end);
53 static inline unsigned int filter_irq_stacks(unsigned long *entries,
54 unsigned int nr_entries)
58 for (i = 0; i < nr_entries; i++) {
59 if (in_irqentry_text(entries[i])) {
60 /* Include the irqentry function into the stack. */
67 static inline depot_stack_handle_t save_stack(gfp_t flags)
69 unsigned long entries[KASAN_STACK_DEPTH];
70 unsigned int nr_entries;
72 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
73 nr_entries = filter_irq_stacks(entries, nr_entries);
74 return stack_depot_save(entries, nr_entries, flags);
77 static inline void set_track(struct kasan_track *track, gfp_t flags)
79 track->pid = current->pid;
80 track->stack = save_stack(flags);
83 void kasan_enable_current(void)
85 current->kasan_depth++;
88 void kasan_disable_current(void)
90 current->kasan_depth--;
93 bool __kasan_check_read(const volatile void *p, unsigned int size)
95 return check_memory_region((unsigned long)p, size, false, _RET_IP_);
97 EXPORT_SYMBOL(__kasan_check_read);
99 bool __kasan_check_write(const volatile void *p, unsigned int size)
101 return check_memory_region((unsigned long)p, size, true, _RET_IP_);
103 EXPORT_SYMBOL(__kasan_check_write);
106 void *memset(void *addr, int c, size_t len)
108 check_memory_region((unsigned long)addr, len, true, _RET_IP_);
110 return __memset(addr, c, len);
114 void *memmove(void *dest, const void *src, size_t len)
116 check_memory_region((unsigned long)src, len, false, _RET_IP_);
117 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
119 return __memmove(dest, src, len);
123 void *memcpy(void *dest, const void *src, size_t len)
125 check_memory_region((unsigned long)src, len, false, _RET_IP_);
126 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
128 return __memcpy(dest, src, len);
132 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
133 * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
135 void kasan_poison_shadow(const void *address, size_t size, u8 value)
137 void *shadow_start, *shadow_end;
140 * Perform shadow offset calculation based on untagged address, as
141 * some of the callers (e.g. kasan_poison_object_data) pass tagged
142 * addresses to this function.
144 address = reset_tag(address);
146 shadow_start = kasan_mem_to_shadow(address);
147 shadow_end = kasan_mem_to_shadow(address + size);
149 __memset(shadow_start, value, shadow_end - shadow_start);
152 void kasan_unpoison_shadow(const void *address, size_t size)
154 u8 tag = get_tag(address);
157 * Perform shadow offset calculation based on untagged address, as
158 * some of the callers (e.g. kasan_unpoison_object_data) pass tagged
159 * addresses to this function.
161 address = reset_tag(address);
163 kasan_poison_shadow(address, size, tag);
165 if (size & KASAN_SHADOW_MASK) {
166 u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
168 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
171 *shadow = size & KASAN_SHADOW_MASK;
175 static void __kasan_unpoison_stack(struct task_struct *task, const void *sp)
177 void *base = task_stack_page(task);
178 size_t size = sp - base;
180 kasan_unpoison_shadow(base, size);
183 /* Unpoison the entire stack for a task. */
184 void kasan_unpoison_task_stack(struct task_struct *task)
186 __kasan_unpoison_stack(task, task_stack_page(task) + THREAD_SIZE);
189 /* Unpoison the stack for the current task beyond a watermark sp value. */
190 asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
193 * Calculate the task stack base address. Avoid using 'current'
194 * because this function is called by early resume code which hasn't
195 * yet set up the percpu register (%gs).
197 void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
199 kasan_unpoison_shadow(base, watermark - base);
203 * Clear all poison for the region between the current SP and a provided
204 * watermark value, as is sometimes required prior to hand-crafted asm function
205 * returns in the middle of functions.
207 void kasan_unpoison_stack_above_sp_to(const void *watermark)
209 const void *sp = __builtin_frame_address(0);
210 size_t size = watermark - sp;
212 if (WARN_ON(sp > watermark))
214 kasan_unpoison_shadow(sp, size);
217 void kasan_alloc_pages(struct page *page, unsigned int order)
222 if (unlikely(PageHighMem(page)))
226 for (i = 0; i < (1 << order); i++)
227 page_kasan_tag_set(page + i, tag);
228 kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
231 void kasan_free_pages(struct page *page, unsigned int order)
233 if (likely(!PageHighMem(page)))
234 kasan_poison_shadow(page_address(page),
240 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
241 * For larger allocations larger redzones are used.
243 static inline unsigned int optimal_redzone(unsigned int object_size)
245 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
249 object_size <= 64 - 16 ? 16 :
250 object_size <= 128 - 32 ? 32 :
251 object_size <= 512 - 64 ? 64 :
252 object_size <= 4096 - 128 ? 128 :
253 object_size <= (1 << 14) - 256 ? 256 :
254 object_size <= (1 << 15) - 512 ? 512 :
255 object_size <= (1 << 16) - 1024 ? 1024 : 2048;
258 void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
261 unsigned int orig_size = *size;
262 unsigned int redzone_size;
265 /* Add alloc meta. */
266 cache->kasan_info.alloc_meta_offset = *size;
267 *size += sizeof(struct kasan_alloc_meta);
270 if (IS_ENABLED(CONFIG_KASAN_GENERIC) &&
271 (cache->flags & SLAB_TYPESAFE_BY_RCU || cache->ctor ||
272 cache->object_size < sizeof(struct kasan_free_meta))) {
273 cache->kasan_info.free_meta_offset = *size;
274 *size += sizeof(struct kasan_free_meta);
277 redzone_size = optimal_redzone(cache->object_size);
278 redzone_adjust = redzone_size - (*size - cache->object_size);
279 if (redzone_adjust > 0)
280 *size += redzone_adjust;
282 *size = min_t(unsigned int, KMALLOC_MAX_SIZE,
283 max(*size, cache->object_size + redzone_size));
286 * If the metadata doesn't fit, don't enable KASAN at all.
288 if (*size <= cache->kasan_info.alloc_meta_offset ||
289 *size <= cache->kasan_info.free_meta_offset) {
290 cache->kasan_info.alloc_meta_offset = 0;
291 cache->kasan_info.free_meta_offset = 0;
296 *flags |= SLAB_KASAN;
299 size_t kasan_metadata_size(struct kmem_cache *cache)
301 return (cache->kasan_info.alloc_meta_offset ?
302 sizeof(struct kasan_alloc_meta) : 0) +
303 (cache->kasan_info.free_meta_offset ?
304 sizeof(struct kasan_free_meta) : 0);
307 struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
310 return (void *)object + cache->kasan_info.alloc_meta_offset;
313 struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
316 BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32);
317 return (void *)object + cache->kasan_info.free_meta_offset;
321 static void kasan_set_free_info(struct kmem_cache *cache,
322 void *object, u8 tag)
324 struct kasan_alloc_meta *alloc_meta;
327 alloc_meta = get_alloc_info(cache, object);
329 #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
330 idx = alloc_meta->free_track_idx;
331 alloc_meta->free_pointer_tag[idx] = tag;
332 alloc_meta->free_track_idx = (idx + 1) % KASAN_NR_FREE_STACKS;
335 set_track(&alloc_meta->free_track[idx], GFP_NOWAIT);
338 void kasan_poison_slab(struct page *page)
342 for (i = 0; i < compound_nr(page); i++)
343 page_kasan_tag_reset(page + i);
344 kasan_poison_shadow(page_address(page), page_size(page),
345 KASAN_KMALLOC_REDZONE);
348 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
350 kasan_unpoison_shadow(object, cache->object_size);
353 void kasan_poison_object_data(struct kmem_cache *cache, void *object)
355 kasan_poison_shadow(object,
356 round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
357 KASAN_KMALLOC_REDZONE);
361 * This function assigns a tag to an object considering the following:
362 * 1. A cache might have a constructor, which might save a pointer to a slab
363 * object somewhere (e.g. in the object itself). We preassign a tag for
364 * each object in caches with constructors during slab creation and reuse
365 * the same tag each time a particular object is allocated.
366 * 2. A cache might be SLAB_TYPESAFE_BY_RCU, which means objects can be
367 * accessed after being freed. We preassign tags for objects in these
369 * 3. For SLAB allocator we can't preassign tags randomly since the freelist
370 * is stored as an array of indexes instead of a linked list. Assign tags
371 * based on objects indexes, so that objects that are next to each other
372 * get different tags.
374 static u8 assign_tag(struct kmem_cache *cache, const void *object,
375 bool init, bool keep_tag)
378 * 1. When an object is kmalloc()'ed, two hooks are called:
379 * kasan_slab_alloc() and kasan_kmalloc(). We assign the
380 * tag only in the first one.
381 * 2. We reuse the same tag for krealloc'ed objects.
384 return get_tag(object);
387 * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU
388 * set, assign a tag when the object is being allocated (init == false).
390 if (!cache->ctor && !(cache->flags & SLAB_TYPESAFE_BY_RCU))
391 return init ? KASAN_TAG_KERNEL : random_tag();
393 /* For caches that either have a constructor or SLAB_TYPESAFE_BY_RCU: */
395 /* For SLAB assign tags based on the object index in the freelist. */
396 return (u8)obj_to_index(cache, virt_to_page(object), (void *)object);
399 * For SLUB assign a random tag during slab creation, otherwise reuse
400 * the already assigned tag.
402 return init ? random_tag() : get_tag(object);
406 void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
409 struct kasan_alloc_meta *alloc_info;
411 if (!(cache->flags & SLAB_KASAN))
412 return (void *)object;
414 alloc_info = get_alloc_info(cache, object);
415 __memset(alloc_info, 0, sizeof(*alloc_info));
417 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
418 object = set_tag(object,
419 assign_tag(cache, object, true, false));
421 return (void *)object;
424 static inline bool shadow_invalid(u8 tag, s8 shadow_byte)
426 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
427 return shadow_byte < 0 ||
428 shadow_byte >= KASAN_SHADOW_SCALE_SIZE;
430 /* else CONFIG_KASAN_SW_TAGS: */
431 if ((u8)shadow_byte == KASAN_TAG_INVALID)
433 if ((tag != KASAN_TAG_KERNEL) && (tag != (u8)shadow_byte))
439 static bool __kasan_slab_free(struct kmem_cache *cache, void *object,
440 unsigned long ip, bool quarantine)
445 unsigned long rounded_up_size;
447 tag = get_tag(object);
448 tagged_object = object;
449 object = reset_tag(object);
451 if (unlikely(nearest_obj(cache, virt_to_head_page(object), object) !=
453 kasan_report_invalid_free(tagged_object, ip);
457 /* RCU slabs could be legally used after free within the RCU period */
458 if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
461 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object));
462 if (shadow_invalid(tag, shadow_byte)) {
463 kasan_report_invalid_free(tagged_object, ip);
467 rounded_up_size = round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE);
468 kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
470 if ((IS_ENABLED(CONFIG_KASAN_GENERIC) && !quarantine) ||
471 unlikely(!(cache->flags & SLAB_KASAN)))
474 kasan_set_free_info(cache, object, tag);
476 quarantine_put(get_free_info(cache, object), cache);
478 return IS_ENABLED(CONFIG_KASAN_GENERIC);
481 bool kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip)
483 return __kasan_slab_free(cache, object, ip, true);
486 static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object,
487 size_t size, gfp_t flags, bool keep_tag)
489 unsigned long redzone_start;
490 unsigned long redzone_end;
493 if (gfpflags_allow_blocking(flags))
496 if (unlikely(object == NULL))
499 redzone_start = round_up((unsigned long)(object + size),
500 KASAN_SHADOW_SCALE_SIZE);
501 redzone_end = round_up((unsigned long)object + cache->object_size,
502 KASAN_SHADOW_SCALE_SIZE);
504 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
505 tag = assign_tag(cache, object, false, keep_tag);
507 /* Tag is ignored in set_tag without CONFIG_KASAN_SW_TAGS */
508 kasan_unpoison_shadow(set_tag(object, tag), size);
509 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
510 KASAN_KMALLOC_REDZONE);
512 if (cache->flags & SLAB_KASAN)
513 set_track(&get_alloc_info(cache, object)->alloc_track, flags);
515 return set_tag(object, tag);
518 void * __must_check kasan_slab_alloc(struct kmem_cache *cache, void *object,
521 return __kasan_kmalloc(cache, object, cache->object_size, flags, false);
524 void * __must_check kasan_kmalloc(struct kmem_cache *cache, const void *object,
525 size_t size, gfp_t flags)
527 return __kasan_kmalloc(cache, object, size, flags, true);
529 EXPORT_SYMBOL(kasan_kmalloc);
531 void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
535 unsigned long redzone_start;
536 unsigned long redzone_end;
538 if (gfpflags_allow_blocking(flags))
541 if (unlikely(ptr == NULL))
544 page = virt_to_page(ptr);
545 redzone_start = round_up((unsigned long)(ptr + size),
546 KASAN_SHADOW_SCALE_SIZE);
547 redzone_end = (unsigned long)ptr + page_size(page);
549 kasan_unpoison_shadow(ptr, size);
550 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
556 void * __must_check kasan_krealloc(const void *object, size_t size, gfp_t flags)
560 if (unlikely(object == ZERO_SIZE_PTR))
561 return (void *)object;
563 page = virt_to_head_page(object);
565 if (unlikely(!PageSlab(page)))
566 return kasan_kmalloc_large(object, size, flags);
568 return __kasan_kmalloc(page->slab_cache, object, size,
572 void kasan_poison_kfree(void *ptr, unsigned long ip)
576 page = virt_to_head_page(ptr);
578 if (unlikely(!PageSlab(page))) {
579 if (ptr != page_address(page)) {
580 kasan_report_invalid_free(ptr, ip);
583 kasan_poison_shadow(ptr, page_size(page), KASAN_FREE_PAGE);
585 __kasan_slab_free(page->slab_cache, ptr, ip, false);
589 void kasan_kfree_large(void *ptr, unsigned long ip)
591 if (ptr != page_address(virt_to_head_page(ptr)))
592 kasan_report_invalid_free(ptr, ip);
593 /* The object will be poisoned by page_alloc. */
596 #ifndef CONFIG_KASAN_VMALLOC
597 int kasan_module_alloc(void *addr, size_t size)
602 unsigned long shadow_start;
604 shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
605 scaled_size = (size + KASAN_SHADOW_MASK) >> KASAN_SHADOW_SCALE_SHIFT;
606 shadow_size = round_up(scaled_size, PAGE_SIZE);
608 if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
611 ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
612 shadow_start + shadow_size,
614 PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
615 __builtin_return_address(0));
618 __memset(ret, KASAN_SHADOW_INIT, shadow_size);
619 find_vm_area(addr)->flags |= VM_KASAN;
620 kmemleak_ignore(ret);
627 void kasan_free_shadow(const struct vm_struct *vm)
629 if (vm->flags & VM_KASAN)
630 vfree(kasan_mem_to_shadow(vm->addr));
634 extern void __kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip);
636 void kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip)
638 unsigned long flags = user_access_save();
639 __kasan_report(addr, size, is_write, ip);
640 user_access_restore(flags);
643 #ifdef CONFIG_MEMORY_HOTPLUG
644 static bool shadow_mapped(unsigned long addr)
646 pgd_t *pgd = pgd_offset_k(addr);
654 p4d = p4d_offset(pgd, addr);
657 pud = pud_offset(p4d, addr);
662 * We can't use pud_large() or pud_huge(), the first one is
663 * arch-specific, the last one depends on HUGETLB_PAGE. So let's abuse
664 * pud_bad(), if pud is bad then it's bad because it's huge.
668 pmd = pmd_offset(pud, addr);
674 pte = pte_offset_kernel(pmd, addr);
675 return !pte_none(*pte);
678 static int __meminit kasan_mem_notifier(struct notifier_block *nb,
679 unsigned long action, void *data)
681 struct memory_notify *mem_data = data;
682 unsigned long nr_shadow_pages, start_kaddr, shadow_start;
683 unsigned long shadow_end, shadow_size;
685 nr_shadow_pages = mem_data->nr_pages >> KASAN_SHADOW_SCALE_SHIFT;
686 start_kaddr = (unsigned long)pfn_to_kaddr(mem_data->start_pfn);
687 shadow_start = (unsigned long)kasan_mem_to_shadow((void *)start_kaddr);
688 shadow_size = nr_shadow_pages << PAGE_SHIFT;
689 shadow_end = shadow_start + shadow_size;
691 if (WARN_ON(mem_data->nr_pages % KASAN_SHADOW_SCALE_SIZE) ||
692 WARN_ON(start_kaddr % (KASAN_SHADOW_SCALE_SIZE << PAGE_SHIFT)))
696 case MEM_GOING_ONLINE: {
700 * If shadow is mapped already than it must have been mapped
701 * during the boot. This could happen if we onlining previously
704 if (shadow_mapped(shadow_start))
707 ret = __vmalloc_node_range(shadow_size, PAGE_SIZE, shadow_start,
708 shadow_end, GFP_KERNEL,
709 PAGE_KERNEL, VM_NO_GUARD,
710 pfn_to_nid(mem_data->start_pfn),
711 __builtin_return_address(0));
715 kmemleak_ignore(ret);
718 case MEM_CANCEL_ONLINE:
720 struct vm_struct *vm;
723 * shadow_start was either mapped during boot by kasan_init()
724 * or during memory online by __vmalloc_node_range().
725 * In the latter case we can use vfree() to free shadow.
726 * Non-NULL result of the find_vm_area() will tell us if
727 * that was the second case.
729 * Currently it's not possible to free shadow mapped
730 * during boot by kasan_init(). It's because the code
731 * to do that hasn't been written yet. So we'll just
734 vm = find_vm_area((void *)shadow_start);
736 vfree((void *)shadow_start);
743 static int __init kasan_memhotplug_init(void)
745 hotplug_memory_notifier(kasan_mem_notifier, 0);
750 core_initcall(kasan_memhotplug_init);
753 #ifdef CONFIG_KASAN_VMALLOC
754 static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr,
760 if (likely(!pte_none(*ptep)))
763 page = __get_free_page(GFP_KERNEL);
767 memset((void *)page, KASAN_VMALLOC_INVALID, PAGE_SIZE);
768 pte = pfn_pte(PFN_DOWN(__pa(page)), PAGE_KERNEL);
770 spin_lock(&init_mm.page_table_lock);
771 if (likely(pte_none(*ptep))) {
772 set_pte_at(&init_mm, addr, ptep, pte);
775 spin_unlock(&init_mm.page_table_lock);
781 int kasan_populate_vmalloc(unsigned long addr, unsigned long size)
783 unsigned long shadow_start, shadow_end;
786 if (!is_vmalloc_or_module_addr((void *)addr))
789 shadow_start = (unsigned long)kasan_mem_to_shadow((void *)addr);
790 shadow_start = ALIGN_DOWN(shadow_start, PAGE_SIZE);
791 shadow_end = (unsigned long)kasan_mem_to_shadow((void *)addr + size);
792 shadow_end = ALIGN(shadow_end, PAGE_SIZE);
794 ret = apply_to_page_range(&init_mm, shadow_start,
795 shadow_end - shadow_start,
796 kasan_populate_vmalloc_pte, NULL);
800 flush_cache_vmap(shadow_start, shadow_end);
803 * We need to be careful about inter-cpu effects here. Consider:
806 * WRITE_ONCE(p, vmalloc(100)); while (x = READ_ONCE(p)) ;
809 * With compiler instrumentation, that ends up looking like this:
812 * // vmalloc() allocates memory
813 * // let a = area->addr
814 * // we reach kasan_populate_vmalloc
815 * // and call kasan_unpoison_shadow:
816 * STORE shadow(a), unpoison_val
818 * STORE shadow(a+99), unpoison_val x = LOAD p
819 * // rest of vmalloc process <data dependency>
820 * STORE p, a LOAD shadow(x+99)
822 * If there is no barrier between the end of unpoisioning the shadow
823 * and the store of the result to p, the stores could be committed
824 * in a different order by CPU#0, and CPU#1 could erroneously observe
825 * poison in the shadow.
827 * We need some sort of barrier between the stores.
829 * In the vmalloc() case, this is provided by a smp_wmb() in
830 * clear_vm_uninitialized_flag(). In the per-cpu allocator and in
831 * get_vm_area() and friends, the caller gets shadow allocated but
832 * doesn't have any pages mapped into the virtual address space that
833 * has been reserved. Mapping those pages in will involve taking and
834 * releasing a page-table lock, which will provide the barrier.
841 * Poison the shadow for a vmalloc region. Called as part of the
842 * freeing process at the time the region is freed.
844 void kasan_poison_vmalloc(const void *start, unsigned long size)
846 if (!is_vmalloc_or_module_addr(start))
849 size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
850 kasan_poison_shadow(start, size, KASAN_VMALLOC_INVALID);
853 void kasan_unpoison_vmalloc(const void *start, unsigned long size)
855 if (!is_vmalloc_or_module_addr(start))
858 kasan_unpoison_shadow(start, size);
861 static int kasan_depopulate_vmalloc_pte(pte_t *ptep, unsigned long addr,
866 page = (unsigned long)__va(pte_pfn(*ptep) << PAGE_SHIFT);
868 spin_lock(&init_mm.page_table_lock);
870 if (likely(!pte_none(*ptep))) {
871 pte_clear(&init_mm, addr, ptep);
874 spin_unlock(&init_mm.page_table_lock);
880 * Release the backing for the vmalloc region [start, end), which
881 * lies within the free region [free_region_start, free_region_end).
883 * This can be run lazily, long after the region was freed. It runs
884 * under vmap_area_lock, so it's not safe to interact with the vmalloc/vmap
887 * How does this work?
888 * -------------------
890 * We have a region that is page aligned, labelled as A.
891 * That might not map onto the shadow in a way that is page-aligned:
895 * |????????|????????|AAAAAAAA|AA....AA|AAAAAAAA|????????| < vmalloc
896 * -------- -------- -------- -------- --------
899 * \-------\|/------/ |/---------------/
901 * |??AAAAAA|AAAAAAAA|AA??????| < shadow
904 * First we align the start upwards and the end downwards, so that the
905 * shadow of the region aligns with shadow page boundaries. In the
906 * example, this gives us the shadow page (2). This is the shadow entirely
907 * covered by this allocation.
909 * Then we have the tricky bits. We want to know if we can free the
910 * partially covered shadow pages - (1) and (3) in the example. For this,
911 * we are given the start and end of the free region that contains this
912 * allocation. Extending our previous example, we could have:
914 * free_region_start free_region_end
917 * |FFFFFFFF|FFFFFFFF|AAAAAAAA|AA....AA|AAAAAAAA|FFFFFFFF| < vmalloc
918 * -------- -------- -------- -------- --------
921 * \-------\|/------/ |/---------------/
923 * |FFAAAAAA|AAAAAAAA|AAF?????| < shadow
926 * Once again, we align the start of the free region up, and the end of
927 * the free region down so that the shadow is page aligned. So we can free
928 * page (1) - we know no allocation currently uses anything in that page,
929 * because all of it is in the vmalloc free region. But we cannot free
930 * page (3), because we can't be sure that the rest of it is unused.
932 * We only consider pages that contain part of the original region for
933 * freeing: we don't try to free other pages from the free region or we'd
934 * end up trying to free huge chunks of virtual address space.
939 * How do we know that we're not freeing a page that is simultaneously
940 * being used for a fresh allocation in kasan_populate_vmalloc(_pte)?
942 * We _can_ have kasan_release_vmalloc and kasan_populate_vmalloc running
943 * at the same time. While we run under free_vmap_area_lock, the population
946 * free_vmap_area_lock instead operates to ensure that the larger range
947 * [free_region_start, free_region_end) is safe: because __alloc_vmap_area and
948 * the per-cpu region-finding algorithm both run under free_vmap_area_lock,
949 * no space identified as free will become used while we are running. This
950 * means that so long as we are careful with alignment and only free shadow
951 * pages entirely covered by the free region, we will not run in to any
952 * trouble - any simultaneous allocations will be for disjoint regions.
954 void kasan_release_vmalloc(unsigned long start, unsigned long end,
955 unsigned long free_region_start,
956 unsigned long free_region_end)
958 void *shadow_start, *shadow_end;
959 unsigned long region_start, region_end;
961 region_start = ALIGN(start, PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
962 region_end = ALIGN_DOWN(end, PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
964 free_region_start = ALIGN(free_region_start,
965 PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
967 if (start != region_start &&
968 free_region_start < region_start)
969 region_start -= PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE;
971 free_region_end = ALIGN_DOWN(free_region_end,
972 PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE);
974 if (end != region_end &&
975 free_region_end > region_end)
976 region_end += PAGE_SIZE * KASAN_SHADOW_SCALE_SIZE;
978 shadow_start = kasan_mem_to_shadow((void *)region_start);
979 shadow_end = kasan_mem_to_shadow((void *)region_end);
981 if (shadow_end > shadow_start) {
982 apply_to_page_range(&init_mm, (unsigned long)shadow_start,
983 (unsigned long)(shadow_end - shadow_start),
984 kasan_depopulate_vmalloc_pte, NULL);
985 flush_tlb_kernel_range((unsigned long)shadow_start,
986 (unsigned long)shadow_end);