kmemcheck.h 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
#ifndef LINUX_KMEMCHECK_H
#define LINUX_KMEMCHECK_H

#include <linux/mm_types.h>
#include <linux/types.h>

#ifdef CONFIG_KMEMCHECK
extern int kmemcheck_enabled;

V
Vegard Nossum 已提交
10
/* The slab-related functions. */
11 12
void kmemcheck_alloc_shadow(struct page *page, int order, gfp_t flags, int node);
void kmemcheck_free_shadow(struct page *page, int order);
V
Vegard Nossum 已提交
13 14 15 16
void kmemcheck_slab_alloc(struct kmem_cache *s, gfp_t gfpflags, void *object,
			  size_t size);
void kmemcheck_slab_free(struct kmem_cache *s, void *object, size_t size);

17 18 19
void kmemcheck_pagealloc_alloc(struct page *p, unsigned int order,
			       gfp_t gfpflags);

V
Vegard Nossum 已提交
20 21 22 23 24 25 26 27 28 29 30 31
void kmemcheck_show_pages(struct page *p, unsigned int n);
void kmemcheck_hide_pages(struct page *p, unsigned int n);

bool kmemcheck_page_is_tracked(struct page *p);

void kmemcheck_mark_unallocated(void *address, unsigned int n);
void kmemcheck_mark_uninitialized(void *address, unsigned int n);
void kmemcheck_mark_initialized(void *address, unsigned int n);
void kmemcheck_mark_freed(void *address, unsigned int n);

void kmemcheck_mark_unallocated_pages(struct page *p, unsigned int n);
void kmemcheck_mark_uninitialized_pages(struct page *p, unsigned int n);
32
void kmemcheck_mark_initialized_pages(struct page *p, unsigned int n);
V
Vegard Nossum 已提交
33

34 35
int kmemcheck_show_addr(unsigned long address);
int kmemcheck_hide_addr(unsigned long address);
36

37 38
bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size);

39 40 41
#else
#define kmemcheck_enabled 0

V
Vegard Nossum 已提交
42
static inline void
43
kmemcheck_alloc_shadow(struct page *page, int order, gfp_t flags, int node)
V
Vegard Nossum 已提交
44 45 46 47
{
}

static inline void
48
kmemcheck_free_shadow(struct page *page, int order)
V
Vegard Nossum 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
{
}

static inline void
kmemcheck_slab_alloc(struct kmem_cache *s, gfp_t gfpflags, void *object,
		     size_t size)
{
}

static inline void kmemcheck_slab_free(struct kmem_cache *s, void *object,
				       size_t size)
{
}

63 64 65 66 67
static inline void kmemcheck_pagealloc_alloc(struct page *p,
	unsigned int order, gfp_t gfpflags)
{
}

V
Vegard Nossum 已提交
68 69 70 71
static inline bool kmemcheck_page_is_tracked(struct page *p)
{
	return false;
}
V
Vegard Nossum 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

static inline void kmemcheck_mark_unallocated(void *address, unsigned int n)
{
}

static inline void kmemcheck_mark_uninitialized(void *address, unsigned int n)
{
}

static inline void kmemcheck_mark_initialized(void *address, unsigned int n)
{
}

static inline void kmemcheck_mark_freed(void *address, unsigned int n)
{
}
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

static inline void kmemcheck_mark_unallocated_pages(struct page *p,
						    unsigned int n)
{
}

static inline void kmemcheck_mark_uninitialized_pages(struct page *p,
						      unsigned int n)
{
}

static inline void kmemcheck_mark_initialized_pages(struct page *p,
						    unsigned int n)
{
}

104 105 106 107 108
static inline bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size)
{
	return true;
}

109 110
#endif /* CONFIG_KMEMCHECK */

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/*
 * Bitfield annotations
 *
 * How to use: If you have a struct using bitfields, for example
 *
 *     struct a {
 *             int x:8, y:8;
 *     };
 *
 * then this should be rewritten as
 *
 *     struct a {
 *             kmemcheck_bitfield_begin(flags);
 *             int x:8, y:8;
 *             kmemcheck_bitfield_end(flags);
 *     };
 *
 * Now the "flags_begin" and "flags_end" members may be used to refer to the
 * beginning and end, respectively, of the bitfield (and things like
 * &x.flags_begin is allowed). As soon as the struct is allocated, the bit-
 * fields should be annotated:
 *
 *     struct a *a = kmalloc(sizeof(struct a), GFP_KERNEL);
 *     kmemcheck_annotate_bitfield(a, flags);
 *
 * Note: We provide the same definitions for both kmemcheck and non-
 * kmemcheck kernels. This makes it harder to introduce accidental errors. It
 * is also allowed to pass NULL pointers to kmemcheck_annotate_bitfield().
 */
#define kmemcheck_bitfield_begin(name)	\
	int name##_begin[0];

#define kmemcheck_bitfield_end(name)	\
	int name##_end[0];

#define kmemcheck_annotate_bitfield(ptr, name)				\
	do if (ptr) {							\
		int _n = (long) &((ptr)->name##_end)			\
			- (long) &((ptr)->name##_begin);		\
		BUILD_BUG_ON(_n < 0);					\
									\
		kmemcheck_mark_initialized(&((ptr)->name##_begin), _n);	\
	} while (0)

#define kmemcheck_annotate_variable(var)				\
	do {								\
		kmemcheck_mark_initialized(&(var), sizeof(var));	\
	} while (0)							\

160
#endif /* LINUX_KMEMCHECK_H */