1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/mm_types.h>
5 #include <linux/slab.h>
7 #include <linux/kmemcheck.h>
9 void kmemcheck_alloc_shadow(struct page
*page
, int order
, gfp_t flags
, int node
)
18 * With kmemcheck enabled, we need to allocate a memory area for the
19 * shadow bits as well.
21 shadow
= alloc_pages_node(node
, flags
| __GFP_NOTRACK
, order
);
23 if (printk_ratelimit())
24 pr_err("kmemcheck: failed to allocate shadow bitmap\n");
28 for(i
= 0; i
< pages
; ++i
)
29 page
[i
].shadow
= page_address(&shadow
[i
]);
32 * Mark it as non-present for the MMU so that our accesses to
33 * this memory will trigger a page fault and let us analyze
34 * the memory accesses.
36 kmemcheck_hide_pages(page
, pages
);
39 void kmemcheck_free_shadow(struct page
*page
, int order
)
45 if (!kmemcheck_page_is_tracked(page
))
50 kmemcheck_show_pages(page
, pages
);
52 shadow
= virt_to_page(page
[0].shadow
);
54 for(i
= 0; i
< pages
; ++i
)
55 page
[i
].shadow
= NULL
;
57 __free_pages(shadow
, order
);
60 void kmemcheck_slab_alloc(struct kmem_cache
*s
, gfp_t gfpflags
, void *object
,
63 if (unlikely(!object
)) /* Skip object if allocation failed */
67 * Has already been memset(), which initializes the shadow for us
70 if (gfpflags
& __GFP_ZERO
)
73 /* No need to initialize the shadow of a non-tracked slab. */
74 if (s
->flags
& SLAB_NOTRACK
)
77 if (!kmemcheck_enabled
|| gfpflags
& __GFP_NOTRACK
) {
79 * Allow notracked objects to be allocated from
80 * tracked caches. Note however that these objects
81 * will still get page faults on access, they just
82 * won't ever be flagged as uninitialized. If page
83 * faults are not acceptable, the slab cache itself
84 * should be marked NOTRACK.
86 kmemcheck_mark_initialized(object
, size
);
87 } else if (!s
->ctor
) {
89 * New objects should be marked uninitialized before
90 * they're returned to the called.
92 kmemcheck_mark_uninitialized(object
, size
);
96 void kmemcheck_slab_free(struct kmem_cache
*s
, void *object
, size_t size
)
98 /* TODO: RCU freeing is unsupported for now; hide false positives. */
99 if (!s
->ctor
&& !(s
->flags
& SLAB_TYPESAFE_BY_RCU
))
100 kmemcheck_mark_freed(object
, size
);
103 void kmemcheck_pagealloc_alloc(struct page
*page
, unsigned int order
,
108 if (gfpflags
& (__GFP_HIGHMEM
| __GFP_NOTRACK
))
114 * NOTE: We choose to track GFP_ZERO pages too; in fact, they
115 * can become uninitialized by copying uninitialized memory
119 /* XXX: Can use zone->node for node? */
120 kmemcheck_alloc_shadow(page
, order
, gfpflags
, -1);
122 if (gfpflags
& __GFP_ZERO
)
123 kmemcheck_mark_initialized_pages(page
, pages
);
125 kmemcheck_mark_uninitialized_pages(page
, pages
);