OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / mm / kmemcheck.c
blobfd814fd61319adcc4763efa749005fc67ede8593
1 #include <linux/gfp.h>
2 #include <linux/mm_types.h>
3 #include <linux/mm.h>
4 #include <linux/slab.h>
5 #include <linux/kmemcheck.h>
7 void kmemcheck_alloc_shadow(struct page *page, int order, gfp_t flags, int node)
9 struct page *shadow;
10 int pages;
11 int i;
13 pages = 1 << order;
16 * With kmemcheck enabled, we need to allocate a memory area for the
17 * shadow bits as well.
19 shadow = alloc_pages_node(node, flags | __GFP_NOTRACK, order);
20 if (!shadow) {
21 if (printk_ratelimit())
22 printk(KERN_ERR "kmemcheck: failed to allocate "
23 "shadow bitmap\n");
24 return;
27 for(i = 0; i < pages; ++i)
28 page[i].shadow = page_address(&shadow[i]);
31 * Mark it as non-present for the MMU so that our accesses to
32 * this memory will trigger a page fault and let us analyze
33 * the memory accesses.
35 kmemcheck_hide_pages(page, pages);
38 void kmemcheck_free_shadow(struct page *page, int order)
40 struct page *shadow;
41 int pages;
42 int i;
44 if (!kmemcheck_page_is_tracked(page))
45 return;
47 pages = 1 << order;
49 kmemcheck_show_pages(page, pages);
51 shadow = virt_to_page(page[0].shadow);
53 for(i = 0; i < pages; ++i)
54 page[i].shadow = NULL;
56 __free_pages(shadow, order);
59 void kmemcheck_slab_alloc(struct kmem_cache *s, gfp_t gfpflags, void *object,
60 size_t size)
63 * Has already been memset(), which initializes the shadow for us
64 * as well.
66 if (gfpflags & __GFP_ZERO)
67 return;
69 /* No need to initialize the shadow of a non-tracked slab. */
70 if (s->flags & SLAB_NOTRACK)
71 return;
73 if (!kmemcheck_enabled || gfpflags & __GFP_NOTRACK) {
75 * Allow notracked objects to be allocated from
76 * tracked caches. Note however that these objects
77 * will still get page faults on access, they just
78 * won't ever be flagged as uninitialized. If page
79 * faults are not acceptable, the slab cache itself
80 * should be marked NOTRACK.
82 kmemcheck_mark_initialized(object, size);
83 } else if (!s->ctor) {
85 * New objects should be marked uninitialized before
86 * they're returned to the called.
88 kmemcheck_mark_uninitialized(object, size);
92 void kmemcheck_slab_free(struct kmem_cache *s, void *object, size_t size)
94 /* TODO: RCU freeing is unsupported for now; hide false positives. */
95 if (!s->ctor && !(s->flags & SLAB_DESTROY_BY_RCU))
96 kmemcheck_mark_freed(object, size);
99 void kmemcheck_pagealloc_alloc(struct page *page, unsigned int order,
100 gfp_t gfpflags)
102 int pages;
104 if (gfpflags & (__GFP_HIGHMEM | __GFP_NOTRACK))
105 return;
107 pages = 1 << order;
110 * NOTE: We choose to track GFP_ZERO pages too; in fact, they
111 * can become uninitialized by copying uninitialized memory
112 * into them.
115 /* XXX: Can use zone->node for node? */
116 kmemcheck_alloc_shadow(page, order, gfpflags, -1);
118 if (gfpflags & __GFP_ZERO)
119 kmemcheck_mark_initialized_pages(page, pages);
120 else
121 kmemcheck_mark_uninitialized_pages(page, pages);