kasan, mm: rename kasan_poison_kfree
[platform/kernel/linux-starfive.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/static_key.h>
6 #include <linux/types.h>
7
8 struct kmem_cache;
9 struct page;
10 struct vm_struct;
11 struct task_struct;
12
13 #ifdef CONFIG_KASAN
14
15 #include <linux/linkage.h>
16 #include <asm/kasan.h>
17
18 /* kasan_data struct is used in KUnit tests for KASAN expected failures */
19 struct kunit_kasan_expectation {
20         bool report_expected;
21         bool report_found;
22 };
23
24 #endif
25
26 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
27
28 #include <linux/pgtable.h>
29
30 /* Software KASAN implementations use shadow memory. */
31
32 #ifdef CONFIG_KASAN_SW_TAGS
33 #define KASAN_SHADOW_INIT 0xFF
34 #else
35 #define KASAN_SHADOW_INIT 0
36 #endif
37
38 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
39 extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
40 extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
41 extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
42 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
43
44 int kasan_populate_early_shadow(const void *shadow_start,
45                                 const void *shadow_end);
46
47 static inline void *kasan_mem_to_shadow(const void *addr)
48 {
49         return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
50                 + KASAN_SHADOW_OFFSET;
51 }
52
53 int kasan_add_zero_shadow(void *start, unsigned long size);
54 void kasan_remove_zero_shadow(void *start, unsigned long size);
55
56 /* Enable reporting bugs after kasan_disable_current() */
57 extern void kasan_enable_current(void);
58
59 /* Disable reporting bugs for current task */
60 extern void kasan_disable_current(void);
61
62 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
63
64 static inline int kasan_add_zero_shadow(void *start, unsigned long size)
65 {
66         return 0;
67 }
68 static inline void kasan_remove_zero_shadow(void *start,
69                                         unsigned long size)
70 {}
71
72 static inline void kasan_enable_current(void) {}
73 static inline void kasan_disable_current(void) {}
74
75 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
76
77 #ifdef CONFIG_KASAN
78
79 struct kasan_cache {
80         int alloc_meta_offset;
81         int free_meta_offset;
82 };
83
84 #ifdef CONFIG_KASAN_HW_TAGS
85 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
86 static __always_inline bool kasan_enabled(void)
87 {
88         return static_branch_likely(&kasan_flag_enabled);
89 }
90 #else
91 static inline bool kasan_enabled(void)
92 {
93         return true;
94 }
95 #endif
96
97 void __kasan_unpoison_range(const void *addr, size_t size);
98 static __always_inline void kasan_unpoison_range(const void *addr, size_t size)
99 {
100         if (kasan_enabled())
101                 __kasan_unpoison_range(addr, size);
102 }
103
104 void __kasan_alloc_pages(struct page *page, unsigned int order);
105 static __always_inline void kasan_alloc_pages(struct page *page,
106                                                 unsigned int order)
107 {
108         if (kasan_enabled())
109                 __kasan_alloc_pages(page, order);
110 }
111
112 void __kasan_free_pages(struct page *page, unsigned int order);
113 static __always_inline void kasan_free_pages(struct page *page,
114                                                 unsigned int order)
115 {
116         if (kasan_enabled())
117                 __kasan_free_pages(page, order);
118 }
119
120 void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
121                                 slab_flags_t *flags);
122 static __always_inline void kasan_cache_create(struct kmem_cache *cache,
123                                 unsigned int *size, slab_flags_t *flags)
124 {
125         if (kasan_enabled())
126                 __kasan_cache_create(cache, size, flags);
127 }
128
129 size_t __kasan_metadata_size(struct kmem_cache *cache);
130 static __always_inline size_t kasan_metadata_size(struct kmem_cache *cache)
131 {
132         if (kasan_enabled())
133                 return __kasan_metadata_size(cache);
134         return 0;
135 }
136
137 void __kasan_poison_slab(struct page *page);
138 static __always_inline void kasan_poison_slab(struct page *page)
139 {
140         if (kasan_enabled())
141                 __kasan_poison_slab(page);
142 }
143
144 void __kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
145 static __always_inline void kasan_unpoison_object_data(struct kmem_cache *cache,
146                                                         void *object)
147 {
148         if (kasan_enabled())
149                 __kasan_unpoison_object_data(cache, object);
150 }
151
152 void __kasan_poison_object_data(struct kmem_cache *cache, void *object);
153 static __always_inline void kasan_poison_object_data(struct kmem_cache *cache,
154                                                         void *object)
155 {
156         if (kasan_enabled())
157                 __kasan_poison_object_data(cache, object);
158 }
159
160 void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache,
161                                           const void *object);
162 static __always_inline void * __must_check kasan_init_slab_obj(
163                                 struct kmem_cache *cache, const void *object)
164 {
165         if (kasan_enabled())
166                 return __kasan_init_slab_obj(cache, object);
167         return (void *)object;
168 }
169
170 bool __kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
171 static __always_inline bool kasan_slab_free(struct kmem_cache *s, void *object,
172                                                 unsigned long ip)
173 {
174         if (kasan_enabled())
175                 return __kasan_slab_free(s, object, ip);
176         return false;
177 }
178
179 void __kasan_slab_free_mempool(void *ptr, unsigned long ip);
180 static __always_inline void kasan_slab_free_mempool(void *ptr, unsigned long ip)
181 {
182         if (kasan_enabled())
183                 __kasan_slab_free_mempool(ptr, ip);
184 }
185
186 void * __must_check __kasan_slab_alloc(struct kmem_cache *s,
187                                        void *object, gfp_t flags);
188 static __always_inline void * __must_check kasan_slab_alloc(
189                                 struct kmem_cache *s, void *object, gfp_t flags)
190 {
191         if (kasan_enabled())
192                 return __kasan_slab_alloc(s, object, flags);
193         return object;
194 }
195
196 void * __must_check __kasan_kmalloc(struct kmem_cache *s, const void *object,
197                                     size_t size, gfp_t flags);
198 static __always_inline void * __must_check kasan_kmalloc(struct kmem_cache *s,
199                                 const void *object, size_t size, gfp_t flags)
200 {
201         if (kasan_enabled())
202                 return __kasan_kmalloc(s, object, size, flags);
203         return (void *)object;
204 }
205
206 void * __must_check __kasan_kmalloc_large(const void *ptr,
207                                           size_t size, gfp_t flags);
208 static __always_inline void * __must_check kasan_kmalloc_large(const void *ptr,
209                                                       size_t size, gfp_t flags)
210 {
211         if (kasan_enabled())
212                 return __kasan_kmalloc_large(ptr, size, flags);
213         return (void *)ptr;
214 }
215
216 void * __must_check __kasan_krealloc(const void *object,
217                                      size_t new_size, gfp_t flags);
218 static __always_inline void * __must_check kasan_krealloc(const void *object,
219                                                  size_t new_size, gfp_t flags)
220 {
221         if (kasan_enabled())
222                 return __kasan_krealloc(object, new_size, flags);
223         return (void *)object;
224 }
225
226 void __kasan_kfree_large(void *ptr, unsigned long ip);
227 static __always_inline void kasan_kfree_large(void *ptr, unsigned long ip)
228 {
229         if (kasan_enabled())
230                 __kasan_kfree_large(ptr, ip);
231 }
232
233 bool kasan_save_enable_multi_shot(void);
234 void kasan_restore_multi_shot(bool enabled);
235
236 #else /* CONFIG_KASAN */
237
238 static inline bool kasan_enabled(void)
239 {
240         return false;
241 }
242 static inline void kasan_unpoison_range(const void *address, size_t size) {}
243 static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
244 static inline void kasan_free_pages(struct page *page, unsigned int order) {}
245 static inline void kasan_cache_create(struct kmem_cache *cache,
246                                       unsigned int *size,
247                                       slab_flags_t *flags) {}
248 static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
249 static inline void kasan_poison_slab(struct page *page) {}
250 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
251                                         void *object) {}
252 static inline void kasan_poison_object_data(struct kmem_cache *cache,
253                                         void *object) {}
254 static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
255                                 const void *object)
256 {
257         return (void *)object;
258 }
259 static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
260                                    unsigned long ip)
261 {
262         return false;
263 }
264 static inline void kasan_slab_free_mempool(void *ptr, unsigned long ip) {}
265 static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
266                                    gfp_t flags)
267 {
268         return object;
269 }
270 static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
271                                 size_t size, gfp_t flags)
272 {
273         return (void *)object;
274 }
275 static inline void *kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
276 {
277         return (void *)ptr;
278 }
279 static inline void *kasan_krealloc(const void *object, size_t new_size,
280                                  gfp_t flags)
281 {
282         return (void *)object;
283 }
284 static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
285
286 #endif /* CONFIG_KASAN */
287
288 #if defined(CONFIG_KASAN) && CONFIG_KASAN_STACK
289 void kasan_unpoison_task_stack(struct task_struct *task);
290 #else
291 static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
292 #endif
293
294 #ifdef CONFIG_KASAN_GENERIC
295
296 void kasan_cache_shrink(struct kmem_cache *cache);
297 void kasan_cache_shutdown(struct kmem_cache *cache);
298 void kasan_record_aux_stack(void *ptr);
299
300 #else /* CONFIG_KASAN_GENERIC */
301
302 static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
303 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
304 static inline void kasan_record_aux_stack(void *ptr) {}
305
306 #endif /* CONFIG_KASAN_GENERIC */
307
308 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
309
310 static inline void *kasan_reset_tag(const void *addr)
311 {
312         return (void *)arch_kasan_reset_tag(addr);
313 }
314
315 bool kasan_report(unsigned long addr, size_t size,
316                 bool is_write, unsigned long ip);
317
318 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
319
320 static inline void *kasan_reset_tag(const void *addr)
321 {
322         return (void *)addr;
323 }
324
325 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/
326
327 #ifdef CONFIG_KASAN_SW_TAGS
328 void __init kasan_init_sw_tags(void);
329 #else
330 static inline void kasan_init_sw_tags(void) { }
331 #endif
332
333 #ifdef CONFIG_KASAN_HW_TAGS
334 void kasan_init_hw_tags_cpu(void);
335 void __init kasan_init_hw_tags(void);
336 #else
337 static inline void kasan_init_hw_tags_cpu(void) { }
338 static inline void kasan_init_hw_tags(void) { }
339 #endif
340
341 #ifdef CONFIG_KASAN_VMALLOC
342
343 int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
344 void kasan_poison_vmalloc(const void *start, unsigned long size);
345 void kasan_unpoison_vmalloc(const void *start, unsigned long size);
346 void kasan_release_vmalloc(unsigned long start, unsigned long end,
347                            unsigned long free_region_start,
348                            unsigned long free_region_end);
349
350 #else /* CONFIG_KASAN_VMALLOC */
351
352 static inline int kasan_populate_vmalloc(unsigned long start,
353                                         unsigned long size)
354 {
355         return 0;
356 }
357
358 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
359 { }
360 static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
361 { }
362 static inline void kasan_release_vmalloc(unsigned long start,
363                                          unsigned long end,
364                                          unsigned long free_region_start,
365                                          unsigned long free_region_end) {}
366
367 #endif /* CONFIG_KASAN_VMALLOC */
368
369 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
370                 !defined(CONFIG_KASAN_VMALLOC)
371
372 /*
373  * These functions provide a special case to support backing module
374  * allocations with real shadow memory. With KASAN vmalloc, the special
375  * case is unnecessary, as the work is handled in the generic case.
376  */
377 int kasan_module_alloc(void *addr, size_t size);
378 void kasan_free_shadow(const struct vm_struct *vm);
379
380 #else /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
381
382 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
383 static inline void kasan_free_shadow(const struct vm_struct *vm) {}
384
385 #endif /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
386
387 #ifdef CONFIG_KASAN_INLINE
388 void kasan_non_canonical_hook(unsigned long addr);
389 #else /* CONFIG_KASAN_INLINE */
390 static inline void kasan_non_canonical_hook(unsigned long addr) { }
391 #endif /* CONFIG_KASAN_INLINE */
392
393 #endif /* LINUX_KASAN_H */