License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / mm / kasan / kasan.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
0b24becc
AR
2#ifndef __MM_KASAN_KASAN_H
3#define __MM_KASAN_KASAN_H
4
5#include <linux/kasan.h>
cd11016e 6#include <linux/stackdepot.h>
0b24becc
AR
7
8#define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
9#define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1)
10
0316bec2
AR
11#define KASAN_FREE_PAGE 0xFF /* page was freed */
12#define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */
13#define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */
14#define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */
bebf56a1 15#define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */
0316bec2 16
c420f167
AR
17/*
18 * Stack redzone shadow values
19 * (Those are compiler's ABI, don't change them)
20 */
21#define KASAN_STACK_LEFT 0xF1
22#define KASAN_STACK_MID 0xF2
23#define KASAN_STACK_RIGHT 0xF3
24#define KASAN_STACK_PARTIAL 0xF4
828347f8 25#define KASAN_USE_AFTER_SCOPE 0xF8
c420f167 26
bebf56a1
AR
27/* Don't break randconfig/all*config builds */
28#ifndef KASAN_ABI_VERSION
29#define KASAN_ABI_VERSION 1
30#endif
b8c73fc2 31
0b24becc
AR
32struct kasan_access_info {
33 const void *access_addr;
34 const void *first_bad_addr;
35 size_t access_size;
36 bool is_write;
37 unsigned long ip;
38};
39
bebf56a1
AR
40/* The layout of struct dictated by compiler */
41struct kasan_source_location {
42 const char *filename;
43 int line_no;
44 int column_no;
45};
46
47/* The layout of struct dictated by compiler */
48struct kasan_global {
49 const void *beg; /* Address of the beginning of the global variable. */
50 size_t size; /* Size of the global variable. */
51 size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */
52 const void *name;
53 const void *module_name; /* Name of the module where the global variable is declared. */
54 unsigned long has_dynamic_init; /* This needed for C++ */
55#if KASAN_ABI_VERSION >= 4
56 struct kasan_source_location *location;
57#endif
045d599a
DV
58#if KASAN_ABI_VERSION >= 5
59 char *odr_indicator;
60#endif
bebf56a1
AR
61};
62
7ed2f9e6
AP
63/**
64 * Structures to keep alloc and free tracks *
65 */
66
cd11016e
AP
67#define KASAN_STACK_DEPTH 64
68
7ed2f9e6 69struct kasan_track {
cd11016e
AP
70 u32 pid;
71 depot_stack_handle_t stack;
7ed2f9e6
AP
72};
73
74struct kasan_alloc_meta {
b3cbd9bf
AR
75 struct kasan_track alloc_track;
76 struct kasan_track free_track;
7ed2f9e6
AP
77};
78
55834c59
AP
79struct qlist_node {
80 struct qlist_node *next;
81};
7ed2f9e6 82struct kasan_free_meta {
55834c59
AP
83 /* This field is used while the object is in the quarantine.
84 * Otherwise it might be used for the allocator freelist.
85 */
86 struct qlist_node quarantine_link;
7ed2f9e6
AP
87};
88
89struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
90 const void *object);
91struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
92 const void *object);
93
0b24becc
AR
94static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
95{
96 return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
97 << KASAN_SHADOW_SCALE_SHIFT);
98}
99
0b24becc
AR
100void kasan_report(unsigned long addr, size_t size,
101 bool is_write, unsigned long ip);
7e088978 102void kasan_report_double_free(struct kmem_cache *cache, void *object,
5ab6d91a 103 void *ip);
0b24becc 104
80a9201a 105#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB)
55834c59
AP
106void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
107void quarantine_reduce(void);
108void quarantine_remove_cache(struct kmem_cache *cache);
109#else
110static inline void quarantine_put(struct kasan_free_meta *info,
111 struct kmem_cache *cache) { }
112static inline void quarantine_reduce(void) { }
113static inline void quarantine_remove_cache(struct kmem_cache *cache) { }
114#endif
115
0b24becc 116#endif