printf: Remove unused 'bprintf'
[drm/drm-misc.git] / include / linux / kmsan.h
blob2b1432cc16d5970dc541fb41734cbaf3672eaba6
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * KMSAN API for subsystems.
5 * Copyright (C) 2017-2022 Google LLC
6 * Author: Alexander Potapenko <glider@google.com>
8 */
9 #ifndef _LINUX_KMSAN_H
10 #define _LINUX_KMSAN_H
12 #include <linux/dma-direction.h>
13 #include <linux/gfp.h>
14 #include <linux/kmsan-checks.h>
15 #include <linux/types.h>
17 struct page;
18 struct kmem_cache;
19 struct task_struct;
20 struct scatterlist;
21 struct urb;
23 #ifdef CONFIG_KMSAN
25 /**
26 * kmsan_task_create() - Initialize KMSAN state for the task.
27 * @task: task to initialize.
29 void kmsan_task_create(struct task_struct *task);
31 /**
32 * kmsan_task_exit() - Notify KMSAN that a task has exited.
33 * @task: task about to finish.
35 void kmsan_task_exit(struct task_struct *task);
37 /**
38 * kmsan_init_shadow() - Initialize KMSAN shadow at boot time.
40 * Allocate and initialize KMSAN metadata for early allocations.
42 void __init kmsan_init_shadow(void);
44 /**
45 * kmsan_init_runtime() - Initialize KMSAN state and enable KMSAN.
47 void __init kmsan_init_runtime(void);
49 /**
50 * kmsan_memblock_free_pages() - handle freeing of memblock pages.
51 * @page: struct page to free.
52 * @order: order of @page.
54 * Freed pages are either returned to buddy allocator or held back to be used
55 * as metadata pages.
57 bool __init __must_check kmsan_memblock_free_pages(struct page *page,
58 unsigned int order);
60 /**
61 * kmsan_alloc_page() - Notify KMSAN about an alloc_pages() call.
62 * @page: struct page pointer returned by alloc_pages().
63 * @order: order of allocated struct page.
64 * @flags: GFP flags used by alloc_pages()
66 * KMSAN marks 1<<@order pages starting at @page as uninitialized, unless
67 * @flags contain __GFP_ZERO.
69 void kmsan_alloc_page(struct page *page, unsigned int order, gfp_t flags);
71 /**
72 * kmsan_free_page() - Notify KMSAN about a free_pages() call.
73 * @page: struct page pointer passed to free_pages().
74 * @order: order of deallocated struct page.
76 * KMSAN marks freed memory as uninitialized.
78 void kmsan_free_page(struct page *page, unsigned int order);
80 /**
81 * kmsan_copy_page_meta() - Copy KMSAN metadata between two pages.
82 * @dst: destination page.
83 * @src: source page.
85 * KMSAN copies the contents of metadata pages for @src into the metadata pages
86 * for @dst. If @dst has no associated metadata pages, nothing happens.
87 * If @src has no associated metadata pages, @dst metadata pages are unpoisoned.
89 void kmsan_copy_page_meta(struct page *dst, struct page *src);
91 /**
92 * kmsan_slab_alloc() - Notify KMSAN about a slab allocation.
93 * @s: slab cache the object belongs to.
94 * @object: object pointer.
95 * @flags: GFP flags passed to the allocator.
97 * Depending on cache flags and GFP flags, KMSAN sets up the metadata of the
98 * newly created object, marking it as initialized or uninitialized.
100 void kmsan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags);
103 * kmsan_slab_free() - Notify KMSAN about a slab deallocation.
104 * @s: slab cache the object belongs to.
105 * @object: object pointer.
107 * KMSAN marks the freed object as uninitialized.
109 void kmsan_slab_free(struct kmem_cache *s, void *object);
112 * kmsan_kmalloc_large() - Notify KMSAN about a large slab allocation.
113 * @ptr: object pointer.
114 * @size: object size.
115 * @flags: GFP flags passed to the allocator.
117 * Similar to kmsan_slab_alloc(), but for large allocations.
119 void kmsan_kmalloc_large(const void *ptr, size_t size, gfp_t flags);
122 * kmsan_kfree_large() - Notify KMSAN about a large slab deallocation.
123 * @ptr: object pointer.
125 * Similar to kmsan_slab_free(), but for large allocations.
127 void kmsan_kfree_large(const void *ptr);
130 * kmsan_map_kernel_range_noflush() - Notify KMSAN about a vmap.
131 * @start: start of vmapped range.
132 * @end: end of vmapped range.
133 * @prot: page protection flags used for vmap.
134 * @pages: array of pages.
135 * @page_shift: page_shift passed to vmap_range_noflush().
137 * KMSAN maps shadow and origin pages of @pages into contiguous ranges in
138 * vmalloc metadata address range. Returns 0 on success, callers must check
139 * for non-zero return value.
141 int __must_check kmsan_vmap_pages_range_noflush(unsigned long start,
142 unsigned long end,
143 pgprot_t prot,
144 struct page **pages,
145 unsigned int page_shift);
148 * kmsan_vunmap_kernel_range_noflush() - Notify KMSAN about a vunmap.
149 * @start: start of vunmapped range.
150 * @end: end of vunmapped range.
152 * KMSAN unmaps the contiguous metadata ranges created by
153 * kmsan_map_kernel_range_noflush().
155 void kmsan_vunmap_range_noflush(unsigned long start, unsigned long end);
158 * kmsan_ioremap_page_range() - Notify KMSAN about a ioremap_page_range() call.
159 * @addr: range start.
160 * @end: range end.
161 * @phys_addr: physical range start.
162 * @prot: page protection flags used for ioremap_page_range().
163 * @page_shift: page_shift argument passed to vmap_range_noflush().
165 * KMSAN creates new metadata pages for the physical pages mapped into the
166 * virtual memory. Returns 0 on success, callers must check for non-zero return
167 * value.
169 int __must_check kmsan_ioremap_page_range(unsigned long addr, unsigned long end,
170 phys_addr_t phys_addr, pgprot_t prot,
171 unsigned int page_shift);
174 * kmsan_iounmap_page_range() - Notify KMSAN about a iounmap_page_range() call.
175 * @start: range start.
176 * @end: range end.
178 * KMSAN unmaps the metadata pages for the given range and, unlike for
179 * vunmap_page_range(), also deallocates them.
181 void kmsan_iounmap_page_range(unsigned long start, unsigned long end);
184 * kmsan_handle_dma() - Handle a DMA data transfer.
185 * @page: first page of the buffer.
186 * @offset: offset of the buffer within the first page.
187 * @size: buffer size.
188 * @dir: one of possible dma_data_direction values.
190 * Depending on @direction, KMSAN:
191 * * checks the buffer, if it is copied to device;
192 * * initializes the buffer, if it is copied from device;
193 * * does both, if this is a DMA_BIDIRECTIONAL transfer.
195 void kmsan_handle_dma(struct page *page, size_t offset, size_t size,
196 enum dma_data_direction dir);
199 * kmsan_handle_dma_sg() - Handle a DMA transfer using scatterlist.
200 * @sg: scatterlist holding DMA buffers.
201 * @nents: number of scatterlist entries.
202 * @dir: one of possible dma_data_direction values.
204 * Depending on @direction, KMSAN:
205 * * checks the buffers in the scatterlist, if they are copied to device;
206 * * initializes the buffers, if they are copied from device;
207 * * does both, if this is a DMA_BIDIRECTIONAL transfer.
209 void kmsan_handle_dma_sg(struct scatterlist *sg, int nents,
210 enum dma_data_direction dir);
213 * kmsan_handle_urb() - Handle a USB data transfer.
214 * @urb: struct urb pointer.
215 * @is_out: data transfer direction (true means output to hardware).
217 * If @is_out is true, KMSAN checks the transfer buffer of @urb. Otherwise,
218 * KMSAN initializes the transfer buffer.
220 void kmsan_handle_urb(const struct urb *urb, bool is_out);
223 * kmsan_unpoison_entry_regs() - Handle pt_regs in low-level entry code.
224 * @regs: struct pt_regs pointer received from assembly code.
226 * KMSAN unpoisons the contents of the passed pt_regs, preventing potential
227 * false positive reports. Unlike kmsan_unpoison_memory(),
228 * kmsan_unpoison_entry_regs() can be called from the regions where
229 * kmsan_in_runtime() returns true, which is the case in early entry code.
231 void kmsan_unpoison_entry_regs(const struct pt_regs *regs);
234 * kmsan_get_metadata() - Return a pointer to KMSAN shadow or origins.
235 * @addr: kernel address.
236 * @is_origin: whether to return origins or shadow.
238 * Return NULL if metadata cannot be found.
240 void *kmsan_get_metadata(void *addr, bool is_origin);
243 * kmsan_enable_current(): Enable KMSAN for the current task.
245 * Each kmsan_enable_current() current call must be preceded by a
246 * kmsan_disable_current() call. These call pairs may be nested.
248 void kmsan_enable_current(void);
251 * kmsan_disable_current(): Disable KMSAN for the current task.
253 * Each kmsan_disable_current() current call must be followed by a
254 * kmsan_enable_current() call. These call pairs may be nested.
256 void kmsan_disable_current(void);
259 * memset_no_sanitize_memory(): Fill memory without KMSAN instrumentation.
260 * @s: address of kernel memory to fill.
261 * @c: constant byte to fill the memory with.
262 * @n: number of bytes to fill.
264 * This is like memset(), but without KMSAN instrumentation.
266 static inline void *memset_no_sanitize_memory(void *s, int c, size_t n)
268 return __memset(s, c, n);
271 extern bool kmsan_enabled;
272 extern int panic_on_kmsan;
275 * KMSAN performs a lot of consistency checks that are currently enabled by
276 * default. BUG_ON is normally discouraged in the kernel, unless used for
277 * debugging, but KMSAN itself is a debugging tool, so it makes little sense to
278 * recover if something goes wrong.
280 #define KMSAN_WARN_ON(cond) \
281 ({ \
282 const bool __cond = WARN_ON(cond); \
283 if (unlikely(__cond)) { \
284 WRITE_ONCE(kmsan_enabled, false); \
285 if (panic_on_kmsan) { \
286 /* Can't call panic() here because */ \
287 /* of uaccess checks. */ \
288 BUG(); \
291 __cond; \
294 #else
296 static inline void kmsan_init_shadow(void)
300 static inline void kmsan_init_runtime(void)
304 static inline bool __must_check kmsan_memblock_free_pages(struct page *page,
305 unsigned int order)
307 return true;
310 static inline void kmsan_task_create(struct task_struct *task)
314 static inline void kmsan_task_exit(struct task_struct *task)
318 static inline void kmsan_alloc_page(struct page *page, unsigned int order,
319 gfp_t flags)
323 static inline void kmsan_free_page(struct page *page, unsigned int order)
327 static inline void kmsan_copy_page_meta(struct page *dst, struct page *src)
331 static inline void kmsan_slab_alloc(struct kmem_cache *s, void *object,
332 gfp_t flags)
336 static inline void kmsan_slab_free(struct kmem_cache *s, void *object)
340 static inline void kmsan_kmalloc_large(const void *ptr, size_t size,
341 gfp_t flags)
345 static inline void kmsan_kfree_large(const void *ptr)
349 static inline int __must_check kmsan_vmap_pages_range_noflush(
350 unsigned long start, unsigned long end, pgprot_t prot,
351 struct page **pages, unsigned int page_shift)
353 return 0;
356 static inline void kmsan_vunmap_range_noflush(unsigned long start,
357 unsigned long end)
361 static inline int __must_check kmsan_ioremap_page_range(unsigned long start,
362 unsigned long end,
363 phys_addr_t phys_addr,
364 pgprot_t prot,
365 unsigned int page_shift)
367 return 0;
370 static inline void kmsan_iounmap_page_range(unsigned long start,
371 unsigned long end)
375 static inline void kmsan_handle_dma(struct page *page, size_t offset,
376 size_t size, enum dma_data_direction dir)
380 static inline void kmsan_handle_dma_sg(struct scatterlist *sg, int nents,
381 enum dma_data_direction dir)
385 static inline void kmsan_handle_urb(const struct urb *urb, bool is_out)
389 static inline void kmsan_unpoison_entry_regs(const struct pt_regs *regs)
393 static inline void kmsan_enable_current(void)
397 static inline void kmsan_disable_current(void)
401 static inline void *memset_no_sanitize_memory(void *s, int c, size_t n)
403 return memset(s, c, n);
406 #define KMSAN_WARN_ON WARN_ON
408 #endif
410 #endif /* _LINUX_KMSAN_H */