pinctrl: baytrail: Convert unsigned to unsigned int
[linux/fpc-iii.git] / mm / kasan / kasan.c
blobc3bd5209da380d9a51a0fa4515f4fcdeefcad409
1 /*
2 * This file contains shadow memory manipulation code.
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7 * Some code borrowed from https://github.com/xairy/kasan-prototype by
8 * Andrey Konovalov <andreyknvl@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #define DISABLE_BRANCH_PROFILING
19 #include <linux/export.h>
20 #include <linux/interrupt.h>
21 #include <linux/init.h>
22 #include <linux/kasan.h>
23 #include <linux/kernel.h>
24 #include <linux/kmemleak.h>
25 #include <linux/linkage.h>
26 #include <linux/memblock.h>
27 #include <linux/memory.h>
28 #include <linux/mm.h>
29 #include <linux/module.h>
30 #include <linux/printk.h>
31 #include <linux/sched.h>
32 #include <linux/sched/task_stack.h>
33 #include <linux/slab.h>
34 #include <linux/stacktrace.h>
35 #include <linux/string.h>
36 #include <linux/types.h>
37 #include <linux/vmalloc.h>
38 #include <linux/bug.h>
40 #include "kasan.h"
41 #include "../slab.h"
43 void kasan_enable_current(void)
45 current->kasan_depth++;
48 void kasan_disable_current(void)
50 current->kasan_depth--;
54 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
55 * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
57 static void kasan_poison_shadow(const void *address, size_t size, u8 value)
59 void *shadow_start, *shadow_end;
61 shadow_start = kasan_mem_to_shadow(address);
62 shadow_end = kasan_mem_to_shadow(address + size);
64 memset(shadow_start, value, shadow_end - shadow_start);
67 void kasan_unpoison_shadow(const void *address, size_t size)
69 kasan_poison_shadow(address, size, 0);
71 if (size & KASAN_SHADOW_MASK) {
72 u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
73 *shadow = size & KASAN_SHADOW_MASK;
77 static void __kasan_unpoison_stack(struct task_struct *task, const void *sp)
79 void *base = task_stack_page(task);
80 size_t size = sp - base;
82 kasan_unpoison_shadow(base, size);
85 /* Unpoison the entire stack for a task. */
86 void kasan_unpoison_task_stack(struct task_struct *task)
88 __kasan_unpoison_stack(task, task_stack_page(task) + THREAD_SIZE);
91 /* Unpoison the stack for the current task beyond a watermark sp value. */
92 asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
95 * Calculate the task stack base address. Avoid using 'current'
96 * because this function is called by early resume code which hasn't
97 * yet set up the percpu register (%gs).
99 void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
101 kasan_unpoison_shadow(base, watermark - base);
105 * Clear all poison for the region between the current SP and a provided
106 * watermark value, as is sometimes required prior to hand-crafted asm function
107 * returns in the middle of functions.
109 void kasan_unpoison_stack_above_sp_to(const void *watermark)
111 const void *sp = __builtin_frame_address(0);
112 size_t size = watermark - sp;
114 if (WARN_ON(sp > watermark))
115 return;
116 kasan_unpoison_shadow(sp, size);
120 * All functions below always inlined so compiler could
121 * perform better optimizations in each of __asan_loadX/__assn_storeX
122 * depending on memory access size X.
125 static __always_inline bool memory_is_poisoned_1(unsigned long addr)
127 s8 shadow_value = *(s8 *)kasan_mem_to_shadow((void *)addr);
129 if (unlikely(shadow_value)) {
130 s8 last_accessible_byte = addr & KASAN_SHADOW_MASK;
131 return unlikely(last_accessible_byte >= shadow_value);
134 return false;
137 static __always_inline bool memory_is_poisoned_2_4_8(unsigned long addr,
138 unsigned long size)
140 u8 *shadow_addr = (u8 *)kasan_mem_to_shadow((void *)addr);
143 * Access crosses 8(shadow size)-byte boundary. Such access maps
144 * into 2 shadow bytes, so we need to check them both.
146 if (unlikely(((addr + size - 1) & KASAN_SHADOW_MASK) < size - 1))
147 return *shadow_addr || memory_is_poisoned_1(addr + size - 1);
149 return memory_is_poisoned_1(addr + size - 1);
152 static __always_inline bool memory_is_poisoned_16(unsigned long addr)
154 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
156 /* Unaligned 16-bytes access maps into 3 shadow bytes. */
157 if (unlikely(!IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE)))
158 return *shadow_addr || memory_is_poisoned_1(addr + 15);
160 return *shadow_addr;
163 static __always_inline unsigned long bytes_is_nonzero(const u8 *start,
164 size_t size)
166 while (size) {
167 if (unlikely(*start))
168 return (unsigned long)start;
169 start++;
170 size--;
173 return 0;
176 static __always_inline unsigned long memory_is_nonzero(const void *start,
177 const void *end)
179 unsigned int words;
180 unsigned long ret;
181 unsigned int prefix = (unsigned long)start % 8;
183 if (end - start <= 16)
184 return bytes_is_nonzero(start, end - start);
186 if (prefix) {
187 prefix = 8 - prefix;
188 ret = bytes_is_nonzero(start, prefix);
189 if (unlikely(ret))
190 return ret;
191 start += prefix;
194 words = (end - start) / 8;
195 while (words) {
196 if (unlikely(*(u64 *)start))
197 return bytes_is_nonzero(start, 8);
198 start += 8;
199 words--;
202 return bytes_is_nonzero(start, (end - start) % 8);
205 static __always_inline bool memory_is_poisoned_n(unsigned long addr,
206 size_t size)
208 unsigned long ret;
210 ret = memory_is_nonzero(kasan_mem_to_shadow((void *)addr),
211 kasan_mem_to_shadow((void *)addr + size - 1) + 1);
213 if (unlikely(ret)) {
214 unsigned long last_byte = addr + size - 1;
215 s8 *last_shadow = (s8 *)kasan_mem_to_shadow((void *)last_byte);
217 if (unlikely(ret != (unsigned long)last_shadow ||
218 ((long)(last_byte & KASAN_SHADOW_MASK) >= *last_shadow)))
219 return true;
221 return false;
224 static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size)
226 if (__builtin_constant_p(size)) {
227 switch (size) {
228 case 1:
229 return memory_is_poisoned_1(addr);
230 case 2:
231 case 4:
232 case 8:
233 return memory_is_poisoned_2_4_8(addr, size);
234 case 16:
235 return memory_is_poisoned_16(addr);
236 default:
237 BUILD_BUG();
241 return memory_is_poisoned_n(addr, size);
244 static __always_inline void check_memory_region_inline(unsigned long addr,
245 size_t size, bool write,
246 unsigned long ret_ip)
248 if (unlikely(size == 0))
249 return;
251 if (unlikely((void *)addr <
252 kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
253 kasan_report(addr, size, write, ret_ip);
254 return;
257 if (likely(!memory_is_poisoned(addr, size)))
258 return;
260 kasan_report(addr, size, write, ret_ip);
263 static void check_memory_region(unsigned long addr,
264 size_t size, bool write,
265 unsigned long ret_ip)
267 check_memory_region_inline(addr, size, write, ret_ip);
270 void kasan_check_read(const volatile void *p, unsigned int size)
272 check_memory_region((unsigned long)p, size, false, _RET_IP_);
274 EXPORT_SYMBOL(kasan_check_read);
276 void kasan_check_write(const volatile void *p, unsigned int size)
278 check_memory_region((unsigned long)p, size, true, _RET_IP_);
280 EXPORT_SYMBOL(kasan_check_write);
282 #undef memset
283 void *memset(void *addr, int c, size_t len)
285 check_memory_region((unsigned long)addr, len, true, _RET_IP_);
287 return __memset(addr, c, len);
290 #undef memmove
291 void *memmove(void *dest, const void *src, size_t len)
293 check_memory_region((unsigned long)src, len, false, _RET_IP_);
294 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
296 return __memmove(dest, src, len);
299 #undef memcpy
300 void *memcpy(void *dest, const void *src, size_t len)
302 check_memory_region((unsigned long)src, len, false, _RET_IP_);
303 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
305 return __memcpy(dest, src, len);
308 void kasan_alloc_pages(struct page *page, unsigned int order)
310 if (likely(!PageHighMem(page)))
311 kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
314 void kasan_free_pages(struct page *page, unsigned int order)
316 if (likely(!PageHighMem(page)))
317 kasan_poison_shadow(page_address(page),
318 PAGE_SIZE << order,
319 KASAN_FREE_PAGE);
323 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
324 * For larger allocations larger redzones are used.
326 static unsigned int optimal_redzone(unsigned int object_size)
328 return
329 object_size <= 64 - 16 ? 16 :
330 object_size <= 128 - 32 ? 32 :
331 object_size <= 512 - 64 ? 64 :
332 object_size <= 4096 - 128 ? 128 :
333 object_size <= (1 << 14) - 256 ? 256 :
334 object_size <= (1 << 15) - 512 ? 512 :
335 object_size <= (1 << 16) - 1024 ? 1024 : 2048;
338 void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
339 slab_flags_t *flags)
341 unsigned int orig_size = *size;
342 int redzone_adjust;
344 /* Add alloc meta. */
345 cache->kasan_info.alloc_meta_offset = *size;
346 *size += sizeof(struct kasan_alloc_meta);
348 /* Add free meta. */
349 if (cache->flags & SLAB_TYPESAFE_BY_RCU || cache->ctor ||
350 cache->object_size < sizeof(struct kasan_free_meta)) {
351 cache->kasan_info.free_meta_offset = *size;
352 *size += sizeof(struct kasan_free_meta);
354 redzone_adjust = optimal_redzone(cache->object_size) -
355 (*size - cache->object_size);
357 if (redzone_adjust > 0)
358 *size += redzone_adjust;
360 *size = min_t(unsigned int, KMALLOC_MAX_SIZE,
361 max(*size, cache->object_size +
362 optimal_redzone(cache->object_size)));
365 * If the metadata doesn't fit, don't enable KASAN at all.
367 if (*size <= cache->kasan_info.alloc_meta_offset ||
368 *size <= cache->kasan_info.free_meta_offset) {
369 cache->kasan_info.alloc_meta_offset = 0;
370 cache->kasan_info.free_meta_offset = 0;
371 *size = orig_size;
372 return;
375 *flags |= SLAB_KASAN;
378 void kasan_cache_shrink(struct kmem_cache *cache)
380 quarantine_remove_cache(cache);
383 void kasan_cache_shutdown(struct kmem_cache *cache)
385 if (!__kmem_cache_empty(cache))
386 quarantine_remove_cache(cache);
389 size_t kasan_metadata_size(struct kmem_cache *cache)
391 return (cache->kasan_info.alloc_meta_offset ?
392 sizeof(struct kasan_alloc_meta) : 0) +
393 (cache->kasan_info.free_meta_offset ?
394 sizeof(struct kasan_free_meta) : 0);
397 void kasan_poison_slab(struct page *page)
399 kasan_poison_shadow(page_address(page),
400 PAGE_SIZE << compound_order(page),
401 KASAN_KMALLOC_REDZONE);
404 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
406 kasan_unpoison_shadow(object, cache->object_size);
409 void kasan_poison_object_data(struct kmem_cache *cache, void *object)
411 kasan_poison_shadow(object,
412 round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
413 KASAN_KMALLOC_REDZONE);
416 static inline int in_irqentry_text(unsigned long ptr)
418 return (ptr >= (unsigned long)&__irqentry_text_start &&
419 ptr < (unsigned long)&__irqentry_text_end) ||
420 (ptr >= (unsigned long)&__softirqentry_text_start &&
421 ptr < (unsigned long)&__softirqentry_text_end);
424 static inline void filter_irq_stacks(struct stack_trace *trace)
426 int i;
428 if (!trace->nr_entries)
429 return;
430 for (i = 0; i < trace->nr_entries; i++)
431 if (in_irqentry_text(trace->entries[i])) {
432 /* Include the irqentry function into the stack. */
433 trace->nr_entries = i + 1;
434 break;
438 static inline depot_stack_handle_t save_stack(gfp_t flags)
440 unsigned long entries[KASAN_STACK_DEPTH];
441 struct stack_trace trace = {
442 .nr_entries = 0,
443 .entries = entries,
444 .max_entries = KASAN_STACK_DEPTH,
445 .skip = 0
448 save_stack_trace(&trace);
449 filter_irq_stacks(&trace);
450 if (trace.nr_entries != 0 &&
451 trace.entries[trace.nr_entries-1] == ULONG_MAX)
452 trace.nr_entries--;
454 return depot_save_stack(&trace, flags);
457 static inline void set_track(struct kasan_track *track, gfp_t flags)
459 track->pid = current->pid;
460 track->stack = save_stack(flags);
463 struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
464 const void *object)
466 BUILD_BUG_ON(sizeof(struct kasan_alloc_meta) > 32);
467 return (void *)object + cache->kasan_info.alloc_meta_offset;
470 struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
471 const void *object)
473 BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32);
474 return (void *)object + cache->kasan_info.free_meta_offset;
477 void kasan_init_slab_obj(struct kmem_cache *cache, const void *object)
479 struct kasan_alloc_meta *alloc_info;
481 if (!(cache->flags & SLAB_KASAN))
482 return;
484 alloc_info = get_alloc_info(cache, object);
485 __memset(alloc_info, 0, sizeof(*alloc_info));
488 void kasan_slab_alloc(struct kmem_cache *cache, void *object, gfp_t flags)
490 kasan_kmalloc(cache, object, cache->object_size, flags);
493 static bool __kasan_slab_free(struct kmem_cache *cache, void *object,
494 unsigned long ip, bool quarantine)
496 s8 shadow_byte;
497 unsigned long rounded_up_size;
499 if (unlikely(nearest_obj(cache, virt_to_head_page(object), object) !=
500 object)) {
501 kasan_report_invalid_free(object, ip);
502 return true;
505 /* RCU slabs could be legally used after free within the RCU period */
506 if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
507 return false;
509 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object));
510 if (shadow_byte < 0 || shadow_byte >= KASAN_SHADOW_SCALE_SIZE) {
511 kasan_report_invalid_free(object, ip);
512 return true;
515 rounded_up_size = round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE);
516 kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
518 if (!quarantine || unlikely(!(cache->flags & SLAB_KASAN)))
519 return false;
521 set_track(&get_alloc_info(cache, object)->free_track, GFP_NOWAIT);
522 quarantine_put(get_free_info(cache, object), cache);
523 return true;
526 bool kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip)
528 return __kasan_slab_free(cache, object, ip, true);
531 void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size,
532 gfp_t flags)
534 unsigned long redzone_start;
535 unsigned long redzone_end;
537 if (gfpflags_allow_blocking(flags))
538 quarantine_reduce();
540 if (unlikely(object == NULL))
541 return;
543 redzone_start = round_up((unsigned long)(object + size),
544 KASAN_SHADOW_SCALE_SIZE);
545 redzone_end = round_up((unsigned long)object + cache->object_size,
546 KASAN_SHADOW_SCALE_SIZE);
548 kasan_unpoison_shadow(object, size);
549 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
550 KASAN_KMALLOC_REDZONE);
552 if (cache->flags & SLAB_KASAN)
553 set_track(&get_alloc_info(cache, object)->alloc_track, flags);
555 EXPORT_SYMBOL(kasan_kmalloc);
557 void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
559 struct page *page;
560 unsigned long redzone_start;
561 unsigned long redzone_end;
563 if (gfpflags_allow_blocking(flags))
564 quarantine_reduce();
566 if (unlikely(ptr == NULL))
567 return;
569 page = virt_to_page(ptr);
570 redzone_start = round_up((unsigned long)(ptr + size),
571 KASAN_SHADOW_SCALE_SIZE);
572 redzone_end = (unsigned long)ptr + (PAGE_SIZE << compound_order(page));
574 kasan_unpoison_shadow(ptr, size);
575 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
576 KASAN_PAGE_REDZONE);
579 void kasan_krealloc(const void *object, size_t size, gfp_t flags)
581 struct page *page;
583 if (unlikely(object == ZERO_SIZE_PTR))
584 return;
586 page = virt_to_head_page(object);
588 if (unlikely(!PageSlab(page)))
589 kasan_kmalloc_large(object, size, flags);
590 else
591 kasan_kmalloc(page->slab_cache, object, size, flags);
594 void kasan_poison_kfree(void *ptr, unsigned long ip)
596 struct page *page;
598 page = virt_to_head_page(ptr);
600 if (unlikely(!PageSlab(page))) {
601 if (ptr != page_address(page)) {
602 kasan_report_invalid_free(ptr, ip);
603 return;
605 kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
606 KASAN_FREE_PAGE);
607 } else {
608 __kasan_slab_free(page->slab_cache, ptr, ip, false);
612 void kasan_kfree_large(void *ptr, unsigned long ip)
614 if (ptr != page_address(virt_to_head_page(ptr)))
615 kasan_report_invalid_free(ptr, ip);
616 /* The object will be poisoned by page_alloc. */
619 int kasan_module_alloc(void *addr, size_t size)
621 void *ret;
622 size_t scaled_size;
623 size_t shadow_size;
624 unsigned long shadow_start;
626 shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
627 scaled_size = (size + KASAN_SHADOW_MASK) >> KASAN_SHADOW_SCALE_SHIFT;
628 shadow_size = round_up(scaled_size, PAGE_SIZE);
630 if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
631 return -EINVAL;
633 ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
634 shadow_start + shadow_size,
635 GFP_KERNEL | __GFP_ZERO,
636 PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
637 __builtin_return_address(0));
639 if (ret) {
640 find_vm_area(addr)->flags |= VM_KASAN;
641 kmemleak_ignore(ret);
642 return 0;
645 return -ENOMEM;
648 void kasan_free_shadow(const struct vm_struct *vm)
650 if (vm->flags & VM_KASAN)
651 vfree(kasan_mem_to_shadow(vm->addr));
654 static void register_global(struct kasan_global *global)
656 size_t aligned_size = round_up(global->size, KASAN_SHADOW_SCALE_SIZE);
658 kasan_unpoison_shadow(global->beg, global->size);
660 kasan_poison_shadow(global->beg + aligned_size,
661 global->size_with_redzone - aligned_size,
662 KASAN_GLOBAL_REDZONE);
665 void __asan_register_globals(struct kasan_global *globals, size_t size)
667 int i;
669 for (i = 0; i < size; i++)
670 register_global(&globals[i]);
672 EXPORT_SYMBOL(__asan_register_globals);
674 void __asan_unregister_globals(struct kasan_global *globals, size_t size)
677 EXPORT_SYMBOL(__asan_unregister_globals);
679 #define DEFINE_ASAN_LOAD_STORE(size) \
680 void __asan_load##size(unsigned long addr) \
682 check_memory_region_inline(addr, size, false, _RET_IP_);\
684 EXPORT_SYMBOL(__asan_load##size); \
685 __alias(__asan_load##size) \
686 void __asan_load##size##_noabort(unsigned long); \
687 EXPORT_SYMBOL(__asan_load##size##_noabort); \
688 void __asan_store##size(unsigned long addr) \
690 check_memory_region_inline(addr, size, true, _RET_IP_); \
692 EXPORT_SYMBOL(__asan_store##size); \
693 __alias(__asan_store##size) \
694 void __asan_store##size##_noabort(unsigned long); \
695 EXPORT_SYMBOL(__asan_store##size##_noabort)
697 DEFINE_ASAN_LOAD_STORE(1);
698 DEFINE_ASAN_LOAD_STORE(2);
699 DEFINE_ASAN_LOAD_STORE(4);
700 DEFINE_ASAN_LOAD_STORE(8);
701 DEFINE_ASAN_LOAD_STORE(16);
703 void __asan_loadN(unsigned long addr, size_t size)
705 check_memory_region(addr, size, false, _RET_IP_);
707 EXPORT_SYMBOL(__asan_loadN);
709 __alias(__asan_loadN)
710 void __asan_loadN_noabort(unsigned long, size_t);
711 EXPORT_SYMBOL(__asan_loadN_noabort);
713 void __asan_storeN(unsigned long addr, size_t size)
715 check_memory_region(addr, size, true, _RET_IP_);
717 EXPORT_SYMBOL(__asan_storeN);
719 __alias(__asan_storeN)
720 void __asan_storeN_noabort(unsigned long, size_t);
721 EXPORT_SYMBOL(__asan_storeN_noabort);
723 /* to shut up compiler complaints */
724 void __asan_handle_no_return(void) {}
725 EXPORT_SYMBOL(__asan_handle_no_return);
727 /* Emitted by compiler to poison large objects when they go out of scope. */
728 void __asan_poison_stack_memory(const void *addr, size_t size)
731 * Addr is KASAN_SHADOW_SCALE_SIZE-aligned and the object is surrounded
732 * by redzones, so we simply round up size to simplify logic.
734 kasan_poison_shadow(addr, round_up(size, KASAN_SHADOW_SCALE_SIZE),
735 KASAN_USE_AFTER_SCOPE);
737 EXPORT_SYMBOL(__asan_poison_stack_memory);
739 /* Emitted by compiler to unpoison large objects when they go into scope. */
740 void __asan_unpoison_stack_memory(const void *addr, size_t size)
742 kasan_unpoison_shadow(addr, size);
744 EXPORT_SYMBOL(__asan_unpoison_stack_memory);
746 /* Emitted by compiler to poison alloca()ed objects. */
747 void __asan_alloca_poison(unsigned long addr, size_t size)
749 size_t rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
750 size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) -
751 rounded_up_size;
752 size_t rounded_down_size = round_down(size, KASAN_SHADOW_SCALE_SIZE);
754 const void *left_redzone = (const void *)(addr -
755 KASAN_ALLOCA_REDZONE_SIZE);
756 const void *right_redzone = (const void *)(addr + rounded_up_size);
758 WARN_ON(!IS_ALIGNED(addr, KASAN_ALLOCA_REDZONE_SIZE));
760 kasan_unpoison_shadow((const void *)(addr + rounded_down_size),
761 size - rounded_down_size);
762 kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE,
763 KASAN_ALLOCA_LEFT);
764 kasan_poison_shadow(right_redzone,
765 padding_size + KASAN_ALLOCA_REDZONE_SIZE,
766 KASAN_ALLOCA_RIGHT);
768 EXPORT_SYMBOL(__asan_alloca_poison);
770 /* Emitted by compiler to unpoison alloca()ed areas when the stack unwinds. */
771 void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom)
773 if (unlikely(!stack_top || stack_top > stack_bottom))
774 return;
776 kasan_unpoison_shadow(stack_top, stack_bottom - stack_top);
778 EXPORT_SYMBOL(__asan_allocas_unpoison);
780 /* Emitted by the compiler to [un]poison local variables. */
781 #define DEFINE_ASAN_SET_SHADOW(byte) \
782 void __asan_set_shadow_##byte(const void *addr, size_t size) \
784 __memset((void *)addr, 0x##byte, size); \
786 EXPORT_SYMBOL(__asan_set_shadow_##byte)
788 DEFINE_ASAN_SET_SHADOW(00);
789 DEFINE_ASAN_SET_SHADOW(f1);
790 DEFINE_ASAN_SET_SHADOW(f2);
791 DEFINE_ASAN_SET_SHADOW(f3);
792 DEFINE_ASAN_SET_SHADOW(f5);
793 DEFINE_ASAN_SET_SHADOW(f8);
795 #ifdef CONFIG_MEMORY_HOTPLUG
796 static bool shadow_mapped(unsigned long addr)
798 pgd_t *pgd = pgd_offset_k(addr);
799 p4d_t *p4d;
800 pud_t *pud;
801 pmd_t *pmd;
802 pte_t *pte;
804 if (pgd_none(*pgd))
805 return false;
806 p4d = p4d_offset(pgd, addr);
807 if (p4d_none(*p4d))
808 return false;
809 pud = pud_offset(p4d, addr);
810 if (pud_none(*pud))
811 return false;
814 * We can't use pud_large() or pud_huge(), the first one is
815 * arch-specific, the last one depends on HUGETLB_PAGE. So let's abuse
816 * pud_bad(), if pud is bad then it's bad because it's huge.
818 if (pud_bad(*pud))
819 return true;
820 pmd = pmd_offset(pud, addr);
821 if (pmd_none(*pmd))
822 return false;
824 if (pmd_bad(*pmd))
825 return true;
826 pte = pte_offset_kernel(pmd, addr);
827 return !pte_none(*pte);
830 static int __meminit kasan_mem_notifier(struct notifier_block *nb,
831 unsigned long action, void *data)
833 struct memory_notify *mem_data = data;
834 unsigned long nr_shadow_pages, start_kaddr, shadow_start;
835 unsigned long shadow_end, shadow_size;
837 nr_shadow_pages = mem_data->nr_pages >> KASAN_SHADOW_SCALE_SHIFT;
838 start_kaddr = (unsigned long)pfn_to_kaddr(mem_data->start_pfn);
839 shadow_start = (unsigned long)kasan_mem_to_shadow((void *)start_kaddr);
840 shadow_size = nr_shadow_pages << PAGE_SHIFT;
841 shadow_end = shadow_start + shadow_size;
843 if (WARN_ON(mem_data->nr_pages % KASAN_SHADOW_SCALE_SIZE) ||
844 WARN_ON(start_kaddr % (KASAN_SHADOW_SCALE_SIZE << PAGE_SHIFT)))
845 return NOTIFY_BAD;
847 switch (action) {
848 case MEM_GOING_ONLINE: {
849 void *ret;
852 * If shadow is mapped already than it must have been mapped
853 * during the boot. This could happen if we onlining previously
854 * offlined memory.
856 if (shadow_mapped(shadow_start))
857 return NOTIFY_OK;
859 ret = __vmalloc_node_range(shadow_size, PAGE_SIZE, shadow_start,
860 shadow_end, GFP_KERNEL,
861 PAGE_KERNEL, VM_NO_GUARD,
862 pfn_to_nid(mem_data->start_pfn),
863 __builtin_return_address(0));
864 if (!ret)
865 return NOTIFY_BAD;
867 kmemleak_ignore(ret);
868 return NOTIFY_OK;
870 case MEM_CANCEL_ONLINE:
871 case MEM_OFFLINE: {
872 struct vm_struct *vm;
875 * shadow_start was either mapped during boot by kasan_init()
876 * or during memory online by __vmalloc_node_range().
877 * In the latter case we can use vfree() to free shadow.
878 * Non-NULL result of the find_vm_area() will tell us if
879 * that was the second case.
881 * Currently it's not possible to free shadow mapped
882 * during boot by kasan_init(). It's because the code
883 * to do that hasn't been written yet. So we'll just
884 * leak the memory.
886 vm = find_vm_area((void *)shadow_start);
887 if (vm)
888 vfree((void *)shadow_start);
892 return NOTIFY_OK;
895 static int __init kasan_memhotplug_init(void)
897 hotplug_memory_notifier(kasan_mem_notifier, 0);
899 return 0;
902 core_initcall(kasan_memhotplug_init);
903 #endif