xref: /openbmc/linux/mm/kasan/common.c (revision c8ec3743)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file contains common generic and tag-based KASAN code.
4  *
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7  *
8  * Some code borrowed from https://github.com/xairy/kasan-prototype by
9  *        Andrey Konovalov <andreyknvl@gmail.com>
10  *
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.
14  *
15  */
16 
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>
26 #include <linux/mm.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 
38 #include "kasan.h"
39 #include "../slab.h"
40 
41 static inline int in_irqentry_text(unsigned long ptr)
42 {
43 	return (ptr >= (unsigned long)&__irqentry_text_start &&
44 		ptr < (unsigned long)&__irqentry_text_end) ||
45 		(ptr >= (unsigned long)&__softirqentry_text_start &&
46 		 ptr < (unsigned long)&__softirqentry_text_end);
47 }
48 
49 static inline void filter_irq_stacks(struct stack_trace *trace)
50 {
51 	int i;
52 
53 	if (!trace->nr_entries)
54 		return;
55 	for (i = 0; i < trace->nr_entries; i++)
56 		if (in_irqentry_text(trace->entries[i])) {
57 			/* Include the irqentry function into the stack. */
58 			trace->nr_entries = i + 1;
59 			break;
60 		}
61 }
62 
63 static inline depot_stack_handle_t save_stack(gfp_t flags)
64 {
65 	unsigned long entries[KASAN_STACK_DEPTH];
66 	struct stack_trace trace = {
67 		.nr_entries = 0,
68 		.entries = entries,
69 		.max_entries = KASAN_STACK_DEPTH,
70 		.skip = 0
71 	};
72 
73 	save_stack_trace(&trace);
74 	filter_irq_stacks(&trace);
75 	if (trace.nr_entries != 0 &&
76 	    trace.entries[trace.nr_entries-1] == ULONG_MAX)
77 		trace.nr_entries--;
78 
79 	return depot_save_stack(&trace, flags);
80 }
81 
82 static inline void set_track(struct kasan_track *track, gfp_t flags)
83 {
84 	track->pid = current->pid;
85 	track->stack = save_stack(flags);
86 }
87 
88 void kasan_enable_current(void)
89 {
90 	current->kasan_depth++;
91 }
92 
93 void kasan_disable_current(void)
94 {
95 	current->kasan_depth--;
96 }
97 
98 void kasan_check_read(const volatile void *p, unsigned int size)
99 {
100 	check_memory_region((unsigned long)p, size, false, _RET_IP_);
101 }
102 EXPORT_SYMBOL(kasan_check_read);
103 
104 void kasan_check_write(const volatile void *p, unsigned int size)
105 {
106 	check_memory_region((unsigned long)p, size, true, _RET_IP_);
107 }
108 EXPORT_SYMBOL(kasan_check_write);
109 
110 #undef memset
111 void *memset(void *addr, int c, size_t len)
112 {
113 	check_memory_region((unsigned long)addr, len, true, _RET_IP_);
114 
115 	return __memset(addr, c, len);
116 }
117 
118 #undef memmove
119 void *memmove(void *dest, const void *src, size_t len)
120 {
121 	check_memory_region((unsigned long)src, len, false, _RET_IP_);
122 	check_memory_region((unsigned long)dest, len, true, _RET_IP_);
123 
124 	return __memmove(dest, src, len);
125 }
126 
127 #undef memcpy
128 void *memcpy(void *dest, const void *src, size_t len)
129 {
130 	check_memory_region((unsigned long)src, len, false, _RET_IP_);
131 	check_memory_region((unsigned long)dest, len, true, _RET_IP_);
132 
133 	return __memcpy(dest, src, len);
134 }
135 
136 /*
137  * Poisons the shadow memory for 'size' bytes starting from 'addr'.
138  * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
139  */
140 void kasan_poison_shadow(const void *address, size_t size, u8 value)
141 {
142 	void *shadow_start, *shadow_end;
143 
144 	/*
145 	 * Perform shadow offset calculation based on untagged address, as
146 	 * some of the callers (e.g. kasan_poison_object_data) pass tagged
147 	 * addresses to this function.
148 	 */
149 	address = reset_tag(address);
150 
151 	shadow_start = kasan_mem_to_shadow(address);
152 	shadow_end = kasan_mem_to_shadow(address + size);
153 
154 	__memset(shadow_start, value, shadow_end - shadow_start);
155 }
156 
157 void kasan_unpoison_shadow(const void *address, size_t size)
158 {
159 	u8 tag = get_tag(address);
160 
161 	/*
162 	 * Perform shadow offset calculation based on untagged address, as
163 	 * some of the callers (e.g. kasan_unpoison_object_data) pass tagged
164 	 * addresses to this function.
165 	 */
166 	address = reset_tag(address);
167 
168 	kasan_poison_shadow(address, size, tag);
169 
170 	if (size & KASAN_SHADOW_MASK) {
171 		u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
172 
173 		if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
174 			*shadow = tag;
175 		else
176 			*shadow = size & KASAN_SHADOW_MASK;
177 	}
178 }
179 
180 static void __kasan_unpoison_stack(struct task_struct *task, const void *sp)
181 {
182 	void *base = task_stack_page(task);
183 	size_t size = sp - base;
184 
185 	kasan_unpoison_shadow(base, size);
186 }
187 
188 /* Unpoison the entire stack for a task. */
189 void kasan_unpoison_task_stack(struct task_struct *task)
190 {
191 	__kasan_unpoison_stack(task, task_stack_page(task) + THREAD_SIZE);
192 }
193 
194 /* Unpoison the stack for the current task beyond a watermark sp value. */
195 asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
196 {
197 	/*
198 	 * Calculate the task stack base address.  Avoid using 'current'
199 	 * because this function is called by early resume code which hasn't
200 	 * yet set up the percpu register (%gs).
201 	 */
202 	void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
203 
204 	kasan_unpoison_shadow(base, watermark - base);
205 }
206 
207 /*
208  * Clear all poison for the region between the current SP and a provided
209  * watermark value, as is sometimes required prior to hand-crafted asm function
210  * returns in the middle of functions.
211  */
212 void kasan_unpoison_stack_above_sp_to(const void *watermark)
213 {
214 	const void *sp = __builtin_frame_address(0);
215 	size_t size = watermark - sp;
216 
217 	if (WARN_ON(sp > watermark))
218 		return;
219 	kasan_unpoison_shadow(sp, size);
220 }
221 
222 void kasan_alloc_pages(struct page *page, unsigned int order)
223 {
224 	u8 tag;
225 	unsigned long i;
226 
227 	if (unlikely(PageHighMem(page)))
228 		return;
229 
230 	tag = random_tag();
231 	for (i = 0; i < (1 << order); i++)
232 		page_kasan_tag_set(page + i, tag);
233 	kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
234 }
235 
236 void kasan_free_pages(struct page *page, unsigned int order)
237 {
238 	if (likely(!PageHighMem(page)))
239 		kasan_poison_shadow(page_address(page),
240 				PAGE_SIZE << order,
241 				KASAN_FREE_PAGE);
242 }
243 
244 /*
245  * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
246  * For larger allocations larger redzones are used.
247  */
248 static inline unsigned int optimal_redzone(unsigned int object_size)
249 {
250 	if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
251 		return 0;
252 
253 	return
254 		object_size <= 64        - 16   ? 16 :
255 		object_size <= 128       - 32   ? 32 :
256 		object_size <= 512       - 64   ? 64 :
257 		object_size <= 4096      - 128  ? 128 :
258 		object_size <= (1 << 14) - 256  ? 256 :
259 		object_size <= (1 << 15) - 512  ? 512 :
260 		object_size <= (1 << 16) - 1024 ? 1024 : 2048;
261 }
262 
263 void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
264 			slab_flags_t *flags)
265 {
266 	unsigned int orig_size = *size;
267 	unsigned int redzone_size;
268 	int redzone_adjust;
269 
270 	/* Add alloc meta. */
271 	cache->kasan_info.alloc_meta_offset = *size;
272 	*size += sizeof(struct kasan_alloc_meta);
273 
274 	/* Add free meta. */
275 	if (IS_ENABLED(CONFIG_KASAN_GENERIC) &&
276 	    (cache->flags & SLAB_TYPESAFE_BY_RCU || cache->ctor ||
277 	     cache->object_size < sizeof(struct kasan_free_meta))) {
278 		cache->kasan_info.free_meta_offset = *size;
279 		*size += sizeof(struct kasan_free_meta);
280 	}
281 
282 	redzone_size = optimal_redzone(cache->object_size);
283 	redzone_adjust = redzone_size -	(*size - cache->object_size);
284 	if (redzone_adjust > 0)
285 		*size += redzone_adjust;
286 
287 	*size = min_t(unsigned int, KMALLOC_MAX_SIZE,
288 			max(*size, cache->object_size + redzone_size));
289 
290 	/*
291 	 * If the metadata doesn't fit, don't enable KASAN at all.
292 	 */
293 	if (*size <= cache->kasan_info.alloc_meta_offset ||
294 			*size <= cache->kasan_info.free_meta_offset) {
295 		cache->kasan_info.alloc_meta_offset = 0;
296 		cache->kasan_info.free_meta_offset = 0;
297 		*size = orig_size;
298 		return;
299 	}
300 
301 	*flags |= SLAB_KASAN;
302 }
303 
304 size_t kasan_metadata_size(struct kmem_cache *cache)
305 {
306 	return (cache->kasan_info.alloc_meta_offset ?
307 		sizeof(struct kasan_alloc_meta) : 0) +
308 		(cache->kasan_info.free_meta_offset ?
309 		sizeof(struct kasan_free_meta) : 0);
310 }
311 
312 struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
313 					const void *object)
314 {
315 	BUILD_BUG_ON(sizeof(struct kasan_alloc_meta) > 32);
316 	return (void *)object + cache->kasan_info.alloc_meta_offset;
317 }
318 
319 struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
320 				      const void *object)
321 {
322 	BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32);
323 	return (void *)object + cache->kasan_info.free_meta_offset;
324 }
325 
326 void kasan_poison_slab(struct page *page)
327 {
328 	unsigned long i;
329 
330 	for (i = 0; i < (1 << compound_order(page)); i++)
331 		page_kasan_tag_reset(page + i);
332 	kasan_poison_shadow(page_address(page),
333 			PAGE_SIZE << compound_order(page),
334 			KASAN_KMALLOC_REDZONE);
335 }
336 
337 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
338 {
339 	kasan_unpoison_shadow(object, cache->object_size);
340 }
341 
342 void kasan_poison_object_data(struct kmem_cache *cache, void *object)
343 {
344 	kasan_poison_shadow(object,
345 			round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
346 			KASAN_KMALLOC_REDZONE);
347 }
348 
349 /*
350  * This function assigns a tag to an object considering the following:
351  * 1. A cache might have a constructor, which might save a pointer to a slab
352  *    object somewhere (e.g. in the object itself). We preassign a tag for
353  *    each object in caches with constructors during slab creation and reuse
354  *    the same tag each time a particular object is allocated.
355  * 2. A cache might be SLAB_TYPESAFE_BY_RCU, which means objects can be
356  *    accessed after being freed. We preassign tags for objects in these
357  *    caches as well.
358  * 3. For SLAB allocator we can't preassign tags randomly since the freelist
359  *    is stored as an array of indexes instead of a linked list. Assign tags
360  *    based on objects indexes, so that objects that are next to each other
361  *    get different tags.
362  */
363 static u8 assign_tag(struct kmem_cache *cache, const void *object,
364 			bool init, bool krealloc)
365 {
366 	/* Reuse the same tag for krealloc'ed objects. */
367 	if (krealloc)
368 		return get_tag(object);
369 
370 	/*
371 	 * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU
372 	 * set, assign a tag when the object is being allocated (init == false).
373 	 */
374 	if (!cache->ctor && !(cache->flags & SLAB_TYPESAFE_BY_RCU))
375 		return init ? KASAN_TAG_KERNEL : random_tag();
376 
377 	/* For caches that either have a constructor or SLAB_TYPESAFE_BY_RCU: */
378 #ifdef CONFIG_SLAB
379 	/* For SLAB assign tags based on the object index in the freelist. */
380 	return (u8)obj_to_index(cache, virt_to_page(object), (void *)object);
381 #else
382 	/*
383 	 * For SLUB assign a random tag during slab creation, otherwise reuse
384 	 * the already assigned tag.
385 	 */
386 	return init ? random_tag() : get_tag(object);
387 #endif
388 }
389 
390 void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
391 						const void *object)
392 {
393 	struct kasan_alloc_meta *alloc_info;
394 
395 	if (!(cache->flags & SLAB_KASAN))
396 		return (void *)object;
397 
398 	alloc_info = get_alloc_info(cache, object);
399 	__memset(alloc_info, 0, sizeof(*alloc_info));
400 
401 	if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
402 		object = set_tag(object,
403 				assign_tag(cache, object, true, false));
404 
405 	return (void *)object;
406 }
407 
408 void * __must_check kasan_slab_alloc(struct kmem_cache *cache, void *object,
409 					gfp_t flags)
410 {
411 	return kasan_kmalloc(cache, object, cache->object_size, flags);
412 }
413 
414 static inline bool shadow_invalid(u8 tag, s8 shadow_byte)
415 {
416 	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
417 		return shadow_byte < 0 ||
418 			shadow_byte >= KASAN_SHADOW_SCALE_SIZE;
419 	else
420 		return tag != (u8)shadow_byte;
421 }
422 
423 static bool __kasan_slab_free(struct kmem_cache *cache, void *object,
424 			      unsigned long ip, bool quarantine)
425 {
426 	s8 shadow_byte;
427 	u8 tag;
428 	void *tagged_object;
429 	unsigned long rounded_up_size;
430 
431 	tag = get_tag(object);
432 	tagged_object = object;
433 	object = reset_tag(object);
434 
435 	if (unlikely(nearest_obj(cache, virt_to_head_page(object), object) !=
436 	    object)) {
437 		kasan_report_invalid_free(tagged_object, ip);
438 		return true;
439 	}
440 
441 	/* RCU slabs could be legally used after free within the RCU period */
442 	if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
443 		return false;
444 
445 	shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object));
446 	if (shadow_invalid(tag, shadow_byte)) {
447 		kasan_report_invalid_free(tagged_object, ip);
448 		return true;
449 	}
450 
451 	rounded_up_size = round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE);
452 	kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
453 
454 	if ((IS_ENABLED(CONFIG_KASAN_GENERIC) && !quarantine) ||
455 			unlikely(!(cache->flags & SLAB_KASAN)))
456 		return false;
457 
458 	set_track(&get_alloc_info(cache, object)->free_track, GFP_NOWAIT);
459 	quarantine_put(get_free_info(cache, object), cache);
460 
461 	return IS_ENABLED(CONFIG_KASAN_GENERIC);
462 }
463 
464 bool kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip)
465 {
466 	return __kasan_slab_free(cache, object, ip, true);
467 }
468 
469 static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object,
470 				size_t size, gfp_t flags, bool krealloc)
471 {
472 	unsigned long redzone_start;
473 	unsigned long redzone_end;
474 	u8 tag;
475 
476 	if (gfpflags_allow_blocking(flags))
477 		quarantine_reduce();
478 
479 	if (unlikely(object == NULL))
480 		return NULL;
481 
482 	redzone_start = round_up((unsigned long)(object + size),
483 				KASAN_SHADOW_SCALE_SIZE);
484 	redzone_end = round_up((unsigned long)object + cache->object_size,
485 				KASAN_SHADOW_SCALE_SIZE);
486 
487 	if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
488 		tag = assign_tag(cache, object, false, krealloc);
489 
490 	/* Tag is ignored in set_tag without CONFIG_KASAN_SW_TAGS */
491 	kasan_unpoison_shadow(set_tag(object, tag), size);
492 	kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
493 		KASAN_KMALLOC_REDZONE);
494 
495 	if (cache->flags & SLAB_KASAN)
496 		set_track(&get_alloc_info(cache, object)->alloc_track, flags);
497 
498 	return set_tag(object, tag);
499 }
500 
501 void * __must_check kasan_kmalloc(struct kmem_cache *cache, const void *object,
502 				size_t size, gfp_t flags)
503 {
504 	return __kasan_kmalloc(cache, object, size, flags, false);
505 }
506 EXPORT_SYMBOL(kasan_kmalloc);
507 
508 void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
509 						gfp_t flags)
510 {
511 	struct page *page;
512 	unsigned long redzone_start;
513 	unsigned long redzone_end;
514 
515 	if (gfpflags_allow_blocking(flags))
516 		quarantine_reduce();
517 
518 	if (unlikely(ptr == NULL))
519 		return NULL;
520 
521 	page = virt_to_page(ptr);
522 	redzone_start = round_up((unsigned long)(ptr + size),
523 				KASAN_SHADOW_SCALE_SIZE);
524 	redzone_end = (unsigned long)ptr + (PAGE_SIZE << compound_order(page));
525 
526 	kasan_unpoison_shadow(ptr, size);
527 	kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
528 		KASAN_PAGE_REDZONE);
529 
530 	return (void *)ptr;
531 }
532 
533 void * __must_check kasan_krealloc(const void *object, size_t size, gfp_t flags)
534 {
535 	struct page *page;
536 
537 	if (unlikely(object == ZERO_SIZE_PTR))
538 		return (void *)object;
539 
540 	page = virt_to_head_page(object);
541 
542 	if (unlikely(!PageSlab(page)))
543 		return kasan_kmalloc_large(object, size, flags);
544 	else
545 		return __kasan_kmalloc(page->slab_cache, object, size,
546 						flags, true);
547 }
548 
549 void kasan_poison_kfree(void *ptr, unsigned long ip)
550 {
551 	struct page *page;
552 
553 	page = virt_to_head_page(ptr);
554 
555 	if (unlikely(!PageSlab(page))) {
556 		if (ptr != page_address(page)) {
557 			kasan_report_invalid_free(ptr, ip);
558 			return;
559 		}
560 		kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
561 				KASAN_FREE_PAGE);
562 	} else {
563 		__kasan_slab_free(page->slab_cache, ptr, ip, false);
564 	}
565 }
566 
567 void kasan_kfree_large(void *ptr, unsigned long ip)
568 {
569 	if (ptr != page_address(virt_to_head_page(ptr)))
570 		kasan_report_invalid_free(ptr, ip);
571 	/* The object will be poisoned by page_alloc. */
572 }
573 
574 int kasan_module_alloc(void *addr, size_t size)
575 {
576 	void *ret;
577 	size_t scaled_size;
578 	size_t shadow_size;
579 	unsigned long shadow_start;
580 
581 	shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
582 	scaled_size = (size + KASAN_SHADOW_MASK) >> KASAN_SHADOW_SCALE_SHIFT;
583 	shadow_size = round_up(scaled_size, PAGE_SIZE);
584 
585 	if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
586 		return -EINVAL;
587 
588 	ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
589 			shadow_start + shadow_size,
590 			GFP_KERNEL,
591 			PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
592 			__builtin_return_address(0));
593 
594 	if (ret) {
595 		__memset(ret, KASAN_SHADOW_INIT, shadow_size);
596 		find_vm_area(addr)->flags |= VM_KASAN;
597 		kmemleak_ignore(ret);
598 		return 0;
599 	}
600 
601 	return -ENOMEM;
602 }
603 
604 void kasan_free_shadow(const struct vm_struct *vm)
605 {
606 	if (vm->flags & VM_KASAN)
607 		vfree(kasan_mem_to_shadow(vm->addr));
608 }
609 
610 #ifdef CONFIG_MEMORY_HOTPLUG
611 static bool shadow_mapped(unsigned long addr)
612 {
613 	pgd_t *pgd = pgd_offset_k(addr);
614 	p4d_t *p4d;
615 	pud_t *pud;
616 	pmd_t *pmd;
617 	pte_t *pte;
618 
619 	if (pgd_none(*pgd))
620 		return false;
621 	p4d = p4d_offset(pgd, addr);
622 	if (p4d_none(*p4d))
623 		return false;
624 	pud = pud_offset(p4d, addr);
625 	if (pud_none(*pud))
626 		return false;
627 
628 	/*
629 	 * We can't use pud_large() or pud_huge(), the first one is
630 	 * arch-specific, the last one depends on HUGETLB_PAGE.  So let's abuse
631 	 * pud_bad(), if pud is bad then it's bad because it's huge.
632 	 */
633 	if (pud_bad(*pud))
634 		return true;
635 	pmd = pmd_offset(pud, addr);
636 	if (pmd_none(*pmd))
637 		return false;
638 
639 	if (pmd_bad(*pmd))
640 		return true;
641 	pte = pte_offset_kernel(pmd, addr);
642 	return !pte_none(*pte);
643 }
644 
645 static int __meminit kasan_mem_notifier(struct notifier_block *nb,
646 			unsigned long action, void *data)
647 {
648 	struct memory_notify *mem_data = data;
649 	unsigned long nr_shadow_pages, start_kaddr, shadow_start;
650 	unsigned long shadow_end, shadow_size;
651 
652 	nr_shadow_pages = mem_data->nr_pages >> KASAN_SHADOW_SCALE_SHIFT;
653 	start_kaddr = (unsigned long)pfn_to_kaddr(mem_data->start_pfn);
654 	shadow_start = (unsigned long)kasan_mem_to_shadow((void *)start_kaddr);
655 	shadow_size = nr_shadow_pages << PAGE_SHIFT;
656 	shadow_end = shadow_start + shadow_size;
657 
658 	if (WARN_ON(mem_data->nr_pages % KASAN_SHADOW_SCALE_SIZE) ||
659 		WARN_ON(start_kaddr % (KASAN_SHADOW_SCALE_SIZE << PAGE_SHIFT)))
660 		return NOTIFY_BAD;
661 
662 	switch (action) {
663 	case MEM_GOING_ONLINE: {
664 		void *ret;
665 
666 		/*
667 		 * If shadow is mapped already than it must have been mapped
668 		 * during the boot. This could happen if we onlining previously
669 		 * offlined memory.
670 		 */
671 		if (shadow_mapped(shadow_start))
672 			return NOTIFY_OK;
673 
674 		ret = __vmalloc_node_range(shadow_size, PAGE_SIZE, shadow_start,
675 					shadow_end, GFP_KERNEL,
676 					PAGE_KERNEL, VM_NO_GUARD,
677 					pfn_to_nid(mem_data->start_pfn),
678 					__builtin_return_address(0));
679 		if (!ret)
680 			return NOTIFY_BAD;
681 
682 		kmemleak_ignore(ret);
683 		return NOTIFY_OK;
684 	}
685 	case MEM_CANCEL_ONLINE:
686 	case MEM_OFFLINE: {
687 		struct vm_struct *vm;
688 
689 		/*
690 		 * shadow_start was either mapped during boot by kasan_init()
691 		 * or during memory online by __vmalloc_node_range().
692 		 * In the latter case we can use vfree() to free shadow.
693 		 * Non-NULL result of the find_vm_area() will tell us if
694 		 * that was the second case.
695 		 *
696 		 * Currently it's not possible to free shadow mapped
697 		 * during boot by kasan_init(). It's because the code
698 		 * to do that hasn't been written yet. So we'll just
699 		 * leak the memory.
700 		 */
701 		vm = find_vm_area((void *)shadow_start);
702 		if (vm)
703 			vfree((void *)shadow_start);
704 	}
705 	}
706 
707 	return NOTIFY_OK;
708 }
709 
710 static int __init kasan_memhotplug_init(void)
711 {
712 	hotplug_memory_notifier(kasan_mem_notifier, 0);
713 
714 	return 0;
715 }
716 
717 core_initcall(kasan_memhotplug_init);
718 #endif
719