x86/mm: Abstract switching CR3
[linux/fpc-iii.git] / mm / kmemcheck.c
blob800d64b854ea6695eccf83dee42502bfca74cf45
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/gfp.h>
3 #include <linux/mm_types.h>
4 #include <linux/mm.h>
5 #include <linux/slab.h>
6 #include "slab.h"
7 #include <linux/kmemcheck.h>
9 void kmemcheck_alloc_shadow(struct page *page, int order, gfp_t flags, int node)
11 struct page *shadow;
12 int pages;
13 int i;
15 pages = 1 << order;
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);
22 if (!shadow) {
23 if (printk_ratelimit())
24 pr_err("kmemcheck: failed to allocate shadow bitmap\n");
25 return;
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)
41 struct page *shadow;
42 int pages;
43 int i;
45 if (!kmemcheck_page_is_tracked(page))
46 return;
48 pages = 1 << order;
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,
61 size_t size)
63 if (unlikely(!object)) /* Skip object if allocation failed */
64 return;
67 * Has already been memset(), which initializes the shadow for us
68 * as well.
70 if (gfpflags & __GFP_ZERO)
71 return;
73 /* No need to initialize the shadow of a non-tracked slab. */
74 if (s->flags & SLAB_NOTRACK)
75 return;
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,
104 gfp_t gfpflags)
106 int pages;
108 if (gfpflags & (__GFP_HIGHMEM | __GFP_NOTRACK))
109 return;
111 pages = 1 << order;
114 * NOTE: We choose to track GFP_ZERO pages too; in fact, they
115 * can become uninitialized by copying uninitialized memory
116 * into them.
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);
124 else
125 kmemcheck_mark_uninitialized_pages(page, pages);