Back to home page

LXR

 
 

    


0001 #include <linux/gfp.h>
0002 #include <linux/mm_types.h>
0003 #include <linux/mm.h>
0004 #include <linux/slab.h>
0005 #include "slab.h"
0006 #include <linux/kmemcheck.h>
0007 
0008 void kmemcheck_alloc_shadow(struct page *page, int order, gfp_t flags, int node)
0009 {
0010     struct page *shadow;
0011     int pages;
0012     int i;
0013 
0014     pages = 1 << order;
0015 
0016     /*
0017      * With kmemcheck enabled, we need to allocate a memory area for the
0018      * shadow bits as well.
0019      */
0020     shadow = alloc_pages_node(node, flags | __GFP_NOTRACK, order);
0021     if (!shadow) {
0022         if (printk_ratelimit())
0023             pr_err("kmemcheck: failed to allocate shadow bitmap\n");
0024         return;
0025     }
0026 
0027     for(i = 0; i < pages; ++i)
0028         page[i].shadow = page_address(&shadow[i]);
0029 
0030     /*
0031      * Mark it as non-present for the MMU so that our accesses to
0032      * this memory will trigger a page fault and let us analyze
0033      * the memory accesses.
0034      */
0035     kmemcheck_hide_pages(page, pages);
0036 }
0037 
0038 void kmemcheck_free_shadow(struct page *page, int order)
0039 {
0040     struct page *shadow;
0041     int pages;
0042     int i;
0043 
0044     if (!kmemcheck_page_is_tracked(page))
0045         return;
0046 
0047     pages = 1 << order;
0048 
0049     kmemcheck_show_pages(page, pages);
0050 
0051     shadow = virt_to_page(page[0].shadow);
0052 
0053     for(i = 0; i < pages; ++i)
0054         page[i].shadow = NULL;
0055 
0056     __free_pages(shadow, order);
0057 }
0058 
0059 void kmemcheck_slab_alloc(struct kmem_cache *s, gfp_t gfpflags, void *object,
0060               size_t size)
0061 {
0062     if (unlikely(!object)) /* Skip object if allocation failed */
0063         return;
0064 
0065     /*
0066      * Has already been memset(), which initializes the shadow for us
0067      * as well.
0068      */
0069     if (gfpflags & __GFP_ZERO)
0070         return;
0071 
0072     /* No need to initialize the shadow of a non-tracked slab. */
0073     if (s->flags & SLAB_NOTRACK)
0074         return;
0075 
0076     if (!kmemcheck_enabled || gfpflags & __GFP_NOTRACK) {
0077         /*
0078          * Allow notracked objects to be allocated from
0079          * tracked caches. Note however that these objects
0080          * will still get page faults on access, they just
0081          * won't ever be flagged as uninitialized. If page
0082          * faults are not acceptable, the slab cache itself
0083          * should be marked NOTRACK.
0084          */
0085         kmemcheck_mark_initialized(object, size);
0086     } else if (!s->ctor) {
0087         /*
0088          * New objects should be marked uninitialized before
0089          * they're returned to the called.
0090          */
0091         kmemcheck_mark_uninitialized(object, size);
0092     }
0093 }
0094 
0095 void kmemcheck_slab_free(struct kmem_cache *s, void *object, size_t size)
0096 {
0097     /* TODO: RCU freeing is unsupported for now; hide false positives. */
0098     if (!s->ctor && !(s->flags & SLAB_DESTROY_BY_RCU))
0099         kmemcheck_mark_freed(object, size);
0100 }
0101 
0102 void kmemcheck_pagealloc_alloc(struct page *page, unsigned int order,
0103                    gfp_t gfpflags)
0104 {
0105     int pages;
0106 
0107     if (gfpflags & (__GFP_HIGHMEM | __GFP_NOTRACK))
0108         return;
0109 
0110     pages = 1 << order;
0111 
0112     /*
0113      * NOTE: We choose to track GFP_ZERO pages too; in fact, they
0114      * can become uninitialized by copying uninitialized memory
0115      * into them.
0116      */
0117 
0118     /* XXX: Can use zone->node for node? */
0119     kmemcheck_alloc_shadow(page, order, gfpflags, -1);
0120 
0121     if (gfpflags & __GFP_ZERO)
0122         kmemcheck_mark_initialized_pages(page, pages);
0123     else
0124         kmemcheck_mark_uninitialized_pages(page, pages);
0125 }