Merge tag 'gvt-next-fixes-2020-08-05' of https://github.com/intel/gvt-linux into...
[linux-2.6-microblaze.git] / include / linux / kasan.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KASAN_H
3 #define _LINUX_KASAN_H
4
5 #include <linux/types.h>
6
7 struct kmem_cache;
8 struct page;
9 struct vm_struct;
10 struct task_struct;
11
12 #ifdef CONFIG_KASAN
13
14 #include <linux/pgtable.h>
15 #include <asm/kasan.h>
16
17 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
18 extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
19 extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
20 extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
21 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
22
23 int kasan_populate_early_shadow(const void *shadow_start,
24                                 const void *shadow_end);
25
26 static inline void *kasan_mem_to_shadow(const void *addr)
27 {
28         return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
29                 + KASAN_SHADOW_OFFSET;
30 }
31
32 /* Enable reporting bugs after kasan_disable_current() */
33 extern void kasan_enable_current(void);
34
35 /* Disable reporting bugs for current task */
36 extern void kasan_disable_current(void);
37
38 void kasan_unpoison_shadow(const void *address, size_t size);
39
40 void kasan_unpoison_task_stack(struct task_struct *task);
41
42 void kasan_alloc_pages(struct page *page, unsigned int order);
43 void kasan_free_pages(struct page *page, unsigned int order);
44
45 void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
46                         slab_flags_t *flags);
47
48 void kasan_poison_slab(struct page *page);
49 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
50 void kasan_poison_object_data(struct kmem_cache *cache, void *object);
51 void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
52                                         const void *object);
53
54 void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
55                                                 gfp_t flags);
56 void kasan_kfree_large(void *ptr, unsigned long ip);
57 void kasan_poison_kfree(void *ptr, unsigned long ip);
58 void * __must_check kasan_kmalloc(struct kmem_cache *s, const void *object,
59                                         size_t size, gfp_t flags);
60 void * __must_check kasan_krealloc(const void *object, size_t new_size,
61                                         gfp_t flags);
62
63 void * __must_check kasan_slab_alloc(struct kmem_cache *s, void *object,
64                                         gfp_t flags);
65 bool kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
66
67 struct kasan_cache {
68         int alloc_meta_offset;
69         int free_meta_offset;
70 };
71
72 /*
73  * These functions provide a special case to support backing module
74  * allocations with real shadow memory. With KASAN vmalloc, the special
75  * case is unnecessary, as the work is handled in the generic case.
76  */
77 #ifndef CONFIG_KASAN_VMALLOC
78 int kasan_module_alloc(void *addr, size_t size);
79 void kasan_free_shadow(const struct vm_struct *vm);
80 #else
81 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
82 static inline void kasan_free_shadow(const struct vm_struct *vm) {}
83 #endif
84
85 int kasan_add_zero_shadow(void *start, unsigned long size);
86 void kasan_remove_zero_shadow(void *start, unsigned long size);
87
88 size_t __ksize(const void *);
89 static inline void kasan_unpoison_slab(const void *ptr)
90 {
91         kasan_unpoison_shadow(ptr, __ksize(ptr));
92 }
93 size_t kasan_metadata_size(struct kmem_cache *cache);
94
95 bool kasan_save_enable_multi_shot(void);
96 void kasan_restore_multi_shot(bool enabled);
97
98 #else /* CONFIG_KASAN */
99
100 static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
101
102 static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
103
104 static inline void kasan_enable_current(void) {}
105 static inline void kasan_disable_current(void) {}
106
107 static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
108 static inline void kasan_free_pages(struct page *page, unsigned int order) {}
109
110 static inline void kasan_cache_create(struct kmem_cache *cache,
111                                       unsigned int *size,
112                                       slab_flags_t *flags) {}
113
114 static inline void kasan_poison_slab(struct page *page) {}
115 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
116                                         void *object) {}
117 static inline void kasan_poison_object_data(struct kmem_cache *cache,
118                                         void *object) {}
119 static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
120                                 const void *object)
121 {
122         return (void *)object;
123 }
124
125 static inline void *kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags)
126 {
127         return ptr;
128 }
129 static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
130 static inline void kasan_poison_kfree(void *ptr, unsigned long ip) {}
131 static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
132                                 size_t size, gfp_t flags)
133 {
134         return (void *)object;
135 }
136 static inline void *kasan_krealloc(const void *object, size_t new_size,
137                                  gfp_t flags)
138 {
139         return (void *)object;
140 }
141
142 static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
143                                    gfp_t flags)
144 {
145         return object;
146 }
147 static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
148                                    unsigned long ip)
149 {
150         return false;
151 }
152
153 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
154 static inline void kasan_free_shadow(const struct vm_struct *vm) {}
155
156 static inline int kasan_add_zero_shadow(void *start, unsigned long size)
157 {
158         return 0;
159 }
160 static inline void kasan_remove_zero_shadow(void *start,
161                                         unsigned long size)
162 {}
163
164 static inline void kasan_unpoison_slab(const void *ptr) { }
165 static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
166
167 #endif /* CONFIG_KASAN */
168
169 #ifdef CONFIG_KASAN_GENERIC
170
171 #define KASAN_SHADOW_INIT 0
172
173 void kasan_cache_shrink(struct kmem_cache *cache);
174 void kasan_cache_shutdown(struct kmem_cache *cache);
175 void kasan_record_aux_stack(void *ptr);
176
177 #else /* CONFIG_KASAN_GENERIC */
178
179 static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
180 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
181 static inline void kasan_record_aux_stack(void *ptr) {}
182
183 #endif /* CONFIG_KASAN_GENERIC */
184
185 #ifdef CONFIG_KASAN_SW_TAGS
186
187 #define KASAN_SHADOW_INIT 0xFF
188
189 void kasan_init_tags(void);
190
191 void *kasan_reset_tag(const void *addr);
192
193 bool kasan_report(unsigned long addr, size_t size,
194                 bool is_write, unsigned long ip);
195
196 #else /* CONFIG_KASAN_SW_TAGS */
197
198 static inline void kasan_init_tags(void) { }
199
200 static inline void *kasan_reset_tag(const void *addr)
201 {
202         return (void *)addr;
203 }
204
205 #endif /* CONFIG_KASAN_SW_TAGS */
206
207 #ifdef CONFIG_KASAN_VMALLOC
208 int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
209 void kasan_poison_vmalloc(const void *start, unsigned long size);
210 void kasan_unpoison_vmalloc(const void *start, unsigned long size);
211 void kasan_release_vmalloc(unsigned long start, unsigned long end,
212                            unsigned long free_region_start,
213                            unsigned long free_region_end);
214 #else
215 static inline int kasan_populate_vmalloc(unsigned long start,
216                                         unsigned long size)
217 {
218         return 0;
219 }
220
221 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
222 { }
223 static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
224 { }
225 static inline void kasan_release_vmalloc(unsigned long start,
226                                          unsigned long end,
227                                          unsigned long free_region_start,
228                                          unsigned long free_region_end) {}
229 #endif
230
231 #ifdef CONFIG_KASAN_INLINE
232 void kasan_non_canonical_hook(unsigned long addr);
233 #else /* CONFIG_KASAN_INLINE */
234 static inline void kasan_non_canonical_hook(unsigned long addr) { }
235 #endif /* CONFIG_KASAN_INLINE */
236
237 #endif /* LINUX_KASAN_H */