kmemcheck for v2.6.27
Subsystems (11)
Linus,
Please pull the kmemcheck feature git tree from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.git kmemcheck-for-linus
kmemcheck has been written by Vegard Nossum and Pekka Enberg, with some help from the x86 guys as well. Bears the ACKs of all subsystems affected. (x86 and SLUB)
[ NOTE, this tree has been merged on top of x86/for-linus +
tracing/for-linus, so please pull it only after having pulled those trees. ]
Thanks,
Ingo
------------------>
Pekka Enberg (5):
x86: __show_registers() and __show_regs() API unification
slab: move struct kmem_cache to headers
kmemcheck: add Vegard and Pekka to MAINTAINERS
x86: add hooks for kmemcheck on x86_64
slab: add hooks for kmemcheck
Vegard Nossum (16):
x86: add save_stack_trace_bp() for tracing from a specific stack frame
stacktrace: add forward-declaration struct task_struct
tasklets: new tasklet scheduling function
kmemcheck: add the kmemcheck core
x86: add hooks for kmemcheck
kmemcheck: add mm functions
slub: add hooks for kmemcheck
kmemcheck: enable in the x86 Kconfig
kmemcheck: fix sparse warnings
softirq: raise the right softirq
kmemcheck: use the proper comment style
kmemcheck: fix use of uninitialized spinlock
kmemcheck: constrain tracking to non-debugged caches
kmemcheck: mark SMP support BROKEN
kmemcheck: use capital Y/N in kconfig help-texts
kmemcheck: remove unnecessary tests in the slab allocator
Documentation/kmemcheck.txt | 135 ++++++++++ MAINTAINERS | 8 + arch/x86/Kconfig.debug | 109 +++++++++ arch/x86/kernel/process.c | 2 +- arch/x86/kernel/process_32.c | 4 +- arch/x86/kernel/process_64.c | 12 +- arch/x86/kernel/stacktrace.c | 7 + arch/x86/kernel/traps_32.c | 11 +- arch/x86/kernel/traps_64.c | 11 +- arch/x86/mm/Makefile | 2 + arch/x86/mm/fault.c | 18 ++- arch/x86/mm/kmemcheck/Makefile | 3 + arch/x86/mm/kmemcheck/error.c | 216 ++++++++++++++++ arch/x86/mm/kmemcheck/error.h | 15 ++ arch/x86/mm/kmemcheck/kmemcheck.c | 485 +++++++++++++++++++++++++++++++++++++ arch/x86/mm/kmemcheck/opcode.c | 72 ++++++ arch/x86/mm/kmemcheck/opcode.h | 9 + arch/x86/mm/kmemcheck/pte.c | 22 ++ arch/x86/mm/kmemcheck/pte.h | 10 + arch/x86/mm/kmemcheck/shadow.c | 175 +++++++++++++ arch/x86/mm/kmemcheck/shadow.h | 16 ++ arch/x86/mm/kmemcheck/smp.c | 80 ++++++ arch/x86/mm/kmemcheck/smp.h | 23 ++ arch/x86/mm/kmemcheck/string.c | 95 +++++++ include/asm-x86/kdebug.h | 3 +- include/asm-x86/kmemcheck.h | 36 +++ include/asm-x86/pgtable.h | 4 +- include/asm-x86/pgtable_32.h | 6 + include/asm-x86/pgtable_64.h | 6 + include/asm-x86/string_32.h | 8 + include/asm-x86/string_64.h | 1 + include/linux/gfp.h | 3 +- include/linux/interrupt.h | 14 + include/linux/kmemcheck.h | 70 ++++++ include/linux/mm_types.h | 4 + include/linux/slab.h | 7 + include/linux/slab_def.h | 81 ++++++ include/linux/stacktrace.h | 3 + init/main.c | 2 + kernel/fork.c | 16 +- kernel/softirq.c | 11 + kernel/sysctl.c | 12 + mm/Makefile | 2 +- mm/kmemcheck.c | 97 ++++++++ mm/slab.c | 101 ++------- mm/slub.c | 20 ++- 46 files changed, 1936 insertions(+), 111 deletions(-) create mode 100644 Documentation/kmemcheck.txt create mode 100644 arch/x86/mm/kmemcheck/Makefile create mode 100644 arch/x86/mm/kmemcheck/error.c create mode 100644 arch/x86/mm/kmemcheck/error.h create mode 100644 arch/x86/mm/kmemcheck/kmemcheck.c create mode 100644 arch/x86/mm/kmemcheck/opcode.c create mode 100644 arch/x86/mm/kmemcheck/opcode.h create mode 100644 arch/x86/mm/kmemcheck/pte.c create mode 100644 arch/x86/mm/kmemcheck/pte.h create mode 100644 arch/x86/mm/kmemcheck/shadow.c create mode 100644 arch/x86/mm/kmemcheck/shadow.h create mode 100644 arch/x86/mm/kmemcheck/smp.c create mode 100644 arch/x86/mm/kmemcheck/smp.h create mode 100644 arch/x86/mm/kmemcheck/string.c create mode 100644 include/asm-x86/kmemcheck.h create mode 100644 include/linux/kmemcheck.h create mode 100644 mm/kmemcheck.c
Documentation/kmemcheck.txt new file +135 -0
@@ -0,0 +1,135 @@ +Contents +======== + + 1. How to use + 2. Technical description + 3. Changes to the slab allocators + 4. Problems + 5. Parameters + 6. Future enhancements + + +How to use (IMPORTANT) +====================== + +Always remember this: kmemcheck _will_ give false positives. So don't enable +it and spam the mailing list with its reports; you are not going to be heard, +and it will make people's skins thicker for when the real errors are found. + +Instead, I encourage maintainers and developers to find errors in _their_ +_own_ code. And if you find false positives, you can try to work around them, +try to figure out if it's a real bug or not, or simply ignore them. Most +developers know their own code and will quickly and efficiently determine the +root cause of a kmemcheck report. This is therefore also the most efficient +way to work with kmemcheck. + +If you still want to run kmemcheck to inspect others' code, the rule of thumb +should be: If it's not obvious (to you), don't tell us about it either. Most +likely the code is correct and you'll only waste our time. If you can work +out the error, please do send the maintainer a heads up and/or a patch, but +don't expect him/her to fix something that wasn't wrong in the first place. + + +Technical description +===================== + +kmemcheck works by marking memory pages non-present. This means that whenever +somebody attempts to access the page, a page fault is generated. The page +fault handler notices that the page was in fact only hidden, and so it calls +on the kmemcheck code to make further investigations. + +When the investigations are completed, kmemcheck "shows" the page by marking +it present (as it would be under normal circumstances). This way, the +interrupted code can continue as usual. + +But after the instruction has been executed, we should hide the page again, so +that we can catch the next access too! Now kmemcheck makes use of a debugging +feature of the processor, namely single-stepping. When the processor has +finished the one instruction that generated the memory access, a debug +exception is raised. From here, we simply hide the page again and continue +execution, this time with the single-stepping feature turned off. + + +Changes to the slab allocators +============================== + +kmemcheck requires some assistance from the memory allocator in order to work. +The memory allocator needs to + +1. Tell kmemcheck about newly allocated pages and pages that are about to + be freed. This allows kmemcheck to set up and tear down the shadow memory + for the pages in question. The shadow memory stores the status of each byte + in the allocation proper, e.g. whether it is initialized or uninitialized. +2. Tell kmemcheck which parts of memory should be marked uninitialized. There + are actually a few more states, such as "not yet allocated" and "recently + freed". + +If a slab cache is set up using the SLAB_NOTRACK flag, it will never return +memory that can take page faults because of kmemcheck. + +If a slab cache is NOT set up using the SLAB_NOTRACK flag, callers can still +request memory with the __GFP_NOTRACK flag. This does not prevent the page +faults from occurring, however, but marks the object in question as being +initialized so that no warnings will ever be produced for this object. + +Currently, the SLAB and SLUB allocators are supported by kmemcheck. + + +Problems +======== + +The most prominent problem seems to be that of bit-fields. kmemcheck can only +track memory with byte granularity. Therefore, when gcc generates code to +access only one bit in a bit-field, there is really no way for kmemcheck to +know which of the other bits will be used or thrown away. Consequently, there +may be bogus warnings for bit-field accesses. There is some experimental +support to detect this automatically, though it is probably better to work +around this by explicitly initializing whole bit-fields at once. + +Some allocations are used for DMA. As DMA doesn't go through the paging +mechanism, we have absolutely no way to detect DMA writes. This means that +spurious warnings may be seen on access to DMA memory. DMA allocations should +be annotated with the __GFP_NOTRACK flag or allocated from caches marked +SLAB_NOTRACK to work around this problem. + + +Parameters +========== + +In addition to enabling CONFIG_KMEMCHECK before the kernel is compiled, the +parameter kmemcheck=1 must be passed to the kernel when it is started in order +to actually do the tracking. So by default, there is only a very small +(probably negligible) overhead for enabling the config option. + +Similarly, kmemcheck may be turned on or off at run-time using, respectively: + +echo 1 > /proc/sys/kernel/kmemcheck + and +echo 0 > /proc/sys/kernel/kmemcheck + +Note that this is a lazy setting; once turned off, the old allocations will +still have to take a single page fault exception before tracking is turned off +for that particular page. Enabling kmemcheck on will only enable tracking for +allocations made from that point onwards. + +The default mode is the one-shot mode, where only the first error is reported +before kmemcheck is disabled. This mode can be enabled by passing kmemcheck=2 +to the kernel at boot, or running + +echo 2 > /proc/sys/kernel/kmemcheck + +when the kernel is already running. + + +Future enhancements +=================== + +There is already some preliminary support for catching use-after-free errors. +What still needs to be done is delaying kfree() so that memory is not +reallocated immediately after freeing it. [Suggested by Pekka Enberg.] + +It should be possible to allow SMP systems by duplicating the page tables for +each processor in the system. This is probably extremely difficult, however. +[Suggested by Ingo Molnar.] + +Support for instruction set extensions like XMM, SSE2, etc.
MAINTAINERS +8 -0
@@ -2431,6 +2431,14 @@ M: [email protected] L: [email protected] S: Maintained +KMEMCHECK +P: Vegard Nossum +M: [email protected] +P Pekka Enberg +M: [email protected] +L: [email protected] +S: Maintained + KPROBES P: Ananth N Mavinakayanahalli M: [email protected]
arch/x86/Kconfig.debug +109 -0
115 lines not on this page · raw message
arch/x86/kernel/process.c +1 -1
@@ -43,7 +43,7 @@ void arch_task_cache_init(void) task_xstate_cachep =kmem_cache_create ("task_xstate" , xstate_size, __alignof__(union thread_xstate ), -SLAB_PANIC ,NULL ); +SLAB_PANIC |SLAB_NOTRACK ,NULL ); }/*
arch/x86/kernel/process_32.c +2 -2
@@ -151,7 +151,7 @@ void cpu_idle(void) } } -void __show_registers (struct pt_regs *regs,int all) +void __show_regs (struct pt_regs *regs,int all) {unsigned long cr0 =0L , cr2 =0L , cr3 =0L , cr4 =0L ;unsigned long d0, d1, d2, d3, d6, d7;
@@ -212,7 +212,7 @@ void __show_registers(struct pt_regs *regs, int all)void show_regs (struct pt_regs *regs) { -__show_registers (regs,1 ); +__show_regs (regs,1 );show_trace (NULL , regs, ®s->sp, regs->bp); }
arch/x86/kernel/process_64.c +8 -4
@@ -149,7 +149,7 @@ void cpu_idle(void) }/* Prints also some state that isn't saved in the pt_regs */ -void __show_regs (struct pt_regs * regs) +void __show_regs (struct pt_regs * regs,int all) {unsigned long cr0 =0L , cr2 =0L , cr3 =0L , cr4 =0L , fs, gs, shadowgs;unsigned long d0, d1, d2, d3, d6, d7;
@@ -188,13 +188,17 @@ void __show_regs(struct pt_regs * regs)rdmsrl (MSR_GS_BASE , gs);rdmsrl (MSR_KERNEL_GS_BASE , shadowgs); +printk ("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n" , + fs,fsindex,gs,gsindex,shadowgs); + +if (!all) +return ; + cr0 =read_cr0 (); cr2 =read_cr2 (); cr3 =read_cr3 (); cr4 =read_cr4 (); -printk ("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n" , - fs,fsindex,gs,gsindex,shadowgs);printk ("CS: %04x DS: %04x ES: %04x CR0: %016lx\n" , cs, ds, es, cr0);printk ("CR2: %016lx CR3: %016lx CR4: %016lx\n" , cr2, cr3, cr4);
@@ -211,7 +215,7 @@ void __show_regs(struct pt_regs * regs)void show_regs (struct pt_regs *regs) {printk ("CPU %d:" ,smp_processor_id ()); -__show_regs (regs); +__show_regs (regs,1 );show_trace (NULL , regs, (void *)(regs +1 ), regs->bp); }
arch/x86/kernel/stacktrace.c +7 -0
@@ -75,6 +75,13 @@ void save_stack_trace(struct stack_trace *trace) trace->entries[trace->nr_entries++] =ULONG_MAX ; } +void save_stack_trace_bp (struct stack_trace *trace,unsigned long bp) +{ +dump_trace (current,NULL ,NULL , bp, &save_stack_ops, trace); +if (trace->nr_entries < trace->max_entries) + trace->entries[trace->nr_entries++] =ULONG_MAX ; +} +void save_stack_trace_tsk (struct task_struct *tsk,struct stack_trace *trace) {dump_trace (tsk,NULL ,NULL ,0 , &save_stack_ops_nosched, trace);
arch/x86/kernel/traps_32.c +10 -1
@@ -58,6 +58,7 @@#include <asm/nmi.h> #include <asm/smp.h> #include <asm/io.h> +#include <asm/kmemcheck.h> #include "mach_traps.h"
@@ -330,7 +331,7 @@ void show_registers(struct pt_regs *regs)int i;print_modules (); -__show_registers (regs,0 ); +__show_regs (regs,0 ); printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)", TASK_COMM_LEN, current->comm, task_pid_nr(current),
@@ -908,6 +909,14 @@ void __kprobes do_debug(struct pt_regs *regs, long error_code)get_debugreg (condition,6 ); +/* Catch kmemcheck conditions first of all! */ +if (condition &DR_STEP ) { +if (kmemcheck_active (regs)) { +kmemcheck_hide (regs); +return ; + } + } +/* * The processor cleared BTF, so don't mark that we need it set. */
arch/x86/kernel/traps_64.c +10 -1
@@ -48,6 +48,7 @@#include <asm/nmi.h> #include <asm/smp.h> #include <asm/io.h> +#include <asm/kmemcheck.h> #include <asm/pgalloc.h> #include <asm/proto.h> #include <asm/pda.h>
@@ -462,7 +463,7 @@ void show_registers(struct pt_regs *regs) sp = regs->sp;printk ("CPU %d " , cpu); -__show_regs (regs); +__show_regs (regs,1 );printk ("Process %s (pid: %d, threadinfo %p, task %p)\n" , cur->comm, cur->pid,task_thread_info (cur), cur);
@@ -937,6 +938,14 @@ asmlinkage void __kprobes do_debug(struct pt_regs * regs,get_debugreg (condition,6 ); +/* Catch kmemcheck conditions first of all! */ +if (condition &DR_STEP ) { +if (kmemcheck_active (regs)) { +kmemcheck_hide (regs); +return ; + } + } +/* * The processor cleared BTF, so don't mark that we need it set. */
arch/x86/mm/Makefile +2 -0
@@ -8,6 +8,8 @@ obj-$(CONFIG_X86_PTDUMP) += dump_pagetables.o obj-$(CONFIG_HIGHMEM) += highmem_32.o +obj-$(CONFIG_KMEMCHECK) += kmemcheck/ + ifeq ($(CONFIG_X86_32),y) obj-$(CONFIG_NUMA) += discontig_32.o else
arch/x86/mm/fault.c +15 -3
@@ -33,6 +33,7 @@#include <asm/smp.h> #include <asm/tlbflush.h> #include <asm/proto.h> +#include <asm/kmemcheck.h> #include <asm-generic/sections.h> /*
@@ -596,6 +597,13 @@ void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code) si_code =SEGV_MAPERR ; +/* +* Detect and handle instructions that would cause a page fault for +* both a tracked kernel page and a userspace page. +*/ +if (kmemcheck_active (regs)) +kmemcheck_hide (regs); +if (notify_page_fault (regs))return ;
@@ -617,9 +625,13 @@ void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)#else if (unlikely (address >=TASK_SIZE64 )) {#endif -if (!(error_code & (PF_RSVD |PF_USER |PF_PROT )) && -vmalloc_fault (address) >=0 ) -return ; +if (!(error_code & (PF_RSVD |PF_USER |PF_PROT ))) { +if (vmalloc_fault (address) >=0 ) +return ; + +if (kmemcheck_fault (regs, address, error_code)) +return ; + }/* Can handle a stale RO->RW TLB */ if (spurious_fault (address, error_code))
arch/x86/mm/kmemcheck/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-y := error.o kmemcheck.o opcode.o pte.o shadow.o string.o + +obj-$(CONFIG_KMEMCHECK_USE_SMP) += smp.o
arch/x86/mm/kmemcheck/error.c new file +216 -0
216 lines not on this page · raw message
arch/x86/mm/kmemcheck/error.h new file +15 -0
@@ -0,0 +1,15 @@ +#ifndef ARCH__X86__MM__KMEMCHECK__ERROR_H +#define ARCH__X86__MM__KMEMCHECK__ERROR_H + +#include <linux/ptrace.h> + +#include "shadow.h" + +void kmemcheck_error_save (enum kmemcheck_shadow state, +unsigned long address,unsigned int size,struct pt_regs *regs); + +void kmemcheck_error_save_bug (struct pt_regs *regs); + +void kmemcheck_error_recall (void ); + +#endif
arch/x86/mm/kmemcheck/kmemcheck.c new file +485 -0
485 lines not on this page · raw message
arch/x86/mm/kmemcheck/opcode.c new file +72 -0
72 lines not on this page · raw message
arch/x86/mm/kmemcheck/opcode.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef ARCH__X86__MM__KMEMCHECK__OPCODE_H +#define ARCH__X86__MM__KMEMCHECK__OPCODE_H + +#include <linux/types.h> + +unsigned int kmemcheck_opcode_get_size (const uint8_t *op); +const uint8_t *kmemcheck_opcode_get_primary (const uint8_t *op); + +#endif
arch/x86/mm/kmemcheck/pte.c new file +22 -0
@@ -0,0 +1,22 @@ +#include <linux/mm.h> + +#include <asm/pgtable.h> + +#include "pte.h" + +pte_t *kmemcheck_pte_lookup (unsigned long address) +{ +pte_t *pte; +unsigned int level; + + pte =lookup_address (address, &level); +if (!pte) +return NULL ; +if (level !=PG_LEVEL_4K ) +return NULL ; +if (!pte_hidden (*pte)) +return NULL ; + +return pte; +} +
arch/x86/mm/kmemcheck/pte.h new file +10 -0
@@ -0,0 +1,10 @@ +#ifndef ARCH__X86__MM__KMEMCHECK__PTE_H +#define ARCH__X86__MM__KMEMCHECK__PTE_H + +#include <linux/mm.h> + +#include <asm/pgtable.h> + +pte_t *kmemcheck_pte_lookup (unsigned long address); + +#endif
arch/x86/mm/kmemcheck/shadow.c new file +175 -0
175 lines not on this page · raw message
arch/x86/mm/kmemcheck/shadow.h new file +16 -0
@@ -0,0 +1,16 @@ +#ifndef ARCH__X86__MM__KMEMCHECK__SHADOW_H +#define ARCH__X86__MM__KMEMCHECK__SHADOW_H + +enum kmemcheck_shadow { +KMEMCHECK_SHADOW_UNALLOCATED , +KMEMCHECK_SHADOW_UNINITIALIZED , +KMEMCHECK_SHADOW_INITIALIZED , +KMEMCHECK_SHADOW_FREED , +}; + +void *kmemcheck_shadow_lookup (unsigned long address); + +enum kmemcheck_shadow kmemcheck_shadow_test (void *shadow,unsigned int size); +void kmemcheck_shadow_set (void *shadow,unsigned int size); + +#endif
arch/x86/mm/kmemcheck/smp.c new file +80 -0
80 lines not on this page · raw message
arch/x86/mm/kmemcheck/smp.h new file +23 -0
@@ -0,0 +1,23 @@ +#ifndef ARCH__X86__MM__KMEMCHECK__SMP_H +#define ARCH__X86__MM__KMEMCHECK__SMP_H + +#ifdef CONFIG_KMEMCHECK_USE_SMP +void kmemcheck_smp_init (void ); + +void kmemcheck_pause_allbutself (void ); +void kmemcheck_resume (void ); +#else +static inline void kmemcheck_smp_init (void ) +{ +} + +static inline void kmemcheck_pause_allbutself (void ) +{ +} + +static inline void kmemcheck_resume (void ) +{ +} +#endif + +#endif
arch/x86/mm/kmemcheck/string.c new file +95 -0
95 lines not on this page · raw message
include/asm-x86/kdebug.h +1 -2
@@ -27,10 +27,9 @@ extern void printk_address(unsigned long address, int reliable);extern void die (const char *,struct pt_regs *,long );extern int __must_check __die (const char *,struct pt_regs *,long );extern void show_registers (struct pt_regs *regs); -extern void __show_registers (struct pt_regs *,int all);extern void show_trace (struct task_struct *t,struct pt_regs *regs,unsigned long *sp,unsigned long bp); -extern void __show_regs (struct pt_regs *regs); +extern void __show_regs (struct pt_regs *regs,int all);extern void show_regs (struct pt_regs *regs);extern unsigned long oops_begin (void );extern void oops_end (unsigned long ,struct pt_regs *,int signr);
include/asm-x86/kmemcheck.h new file +36 -0
@@ -0,0 +1,36 @@ +#ifndef ASM_X86_KMEMCHECK_H +#define ASM_X86_KMEMCHECK_H + +#include <linux/types.h> +#include <asm/ptrace.h> + +#ifdef CONFIG_KMEMCHECK +bool kmemcheck_active (struct pt_regs *regs); + +void kmemcheck_show (struct pt_regs *regs); +void kmemcheck_hide (struct pt_regs *regs); + +bool kmemcheck_fault (struct pt_regs *regs, +unsigned long address,unsigned long error_code); +#else +static inline bool kmemcheck_active (struct pt_regs *regs) +{ +return false; +} + +static inline void kmemcheck_show (struct pt_regs *regs) +{ +} + +static inline void kmemcheck_hide (struct pt_regs *regs) +{ +} + +static inline bool kmemcheck_fault (struct pt_regs *regs, +unsigned long address,unsigned long error_code) +{ +return false; +} +#endif /* CONFIG_KMEMCHECK */ + +#endif
include/asm-x86/pgtable.h +2 -2
@@ -16,7 +16,7 @@#define _PAGE_BIT_GLOBAL 8/* Global TLB entry PPro+ */ #define _PAGE_BIT_UNUSED1 9/* available for programmer */ #define _PAGE_BIT_UNUSED2 10 -#define _PAGE_BIT_UNUSED3 11 +#define _PAGE_BIT_HIDDEN 11/* hidden by kmemcheck */ #define _PAGE_BIT_PAT_LARGE 12/* On 2MB or 1GB pages */ #define _PAGE_BIT_NX 63/* No execute: only valid after cpuid check */
@@ -31,7 +31,7 @@#define _PAGE_GLOBAL (_AT(pteval_t, 1) << _PAGE_BIT_GLOBAL)#define _PAGE_UNUSED1 (_AT(pteval_t, 1) << _PAGE_BIT_UNUSED1)#define _PAGE_UNUSED2 (_AT(pteval_t, 1) << _PAGE_BIT_UNUSED2) -#define _PAGE_UNUSED3 (_AT(pteval_t, 1) << _PAGE_BIT_UNUSED3) +#define _PAGE_HIDDEN (_AT(pteval_t, 1) << _PAGE_BIT_HIDDEN)#define _PAGE_PAT (_AT(pteval_t, 1) << _PAGE_BIT_PAT)#define _PAGE_PAT_LARGE (_AT(pteval_t, 1) << _PAGE_BIT_PAT_LARGE)
include/asm-x86/pgtable_32.h +6 -0
@@ -85,6 +85,12 @@ extern unsigned long pg0[];#define pte_present (x) ((x).pte_low & (_PAGE_PRESENT | _PAGE_PROTNONE)) +#ifdef CONFIG_KMEMCHECK +#define pte_hidden (x) ((x).pte_low & (_PAGE_HIDDEN)) +#else +#define pte_hidden (x) 0 +#endif +/* To avoid harmful races, pmd_none(x) should check only the lower when PAE */ #define pmd_none (x) (!(unsigned long)pmd_val((x)))#define pmd_present (x) (pmd_val((x)) & _PAGE_PRESENT)
include/asm-x86/pgtable_64.h +6 -0
@@ -172,6 +172,12 @@ static inline int pmd_bad(pmd_t pmd)#define pte_none (x) (!pte_val((x)))#define pte_present (x) (pte_val((x)) & (_PAGE_PRESENT | _PAGE_PROTNONE)) +#ifdef CONFIG_KMEMCHECK +#define pte_hidden (x) (pte_val((x)) & (_PAGE_HIDDEN)) +#else +#define pte_hidden (x) 0 +#endif +#define pages_to_mb (x) ((x) >> (20 - PAGE_SHIFT))/* FIXME: is this right? */ #define pte_page (x) pfn_to_page(pte_pfn((x)))#define pte_pfn (x) ((pte_val((x)) & __PHYSICAL_MASK) >> PAGE_SHIFT)
include/asm-x86/string_32.h +8 -0
@@ -315,6 +315,14 @@ void *__constant_c_and_count_memset(void *s, unsigned long pattern, (count)) \ : __memset((s), (c), (count))) +/* If kmemcheck is enabled, our best bet is a custom memset() that disables +* checking in order to save a whole lot of (unnecessary) page faults. */ +#ifdef CONFIG_KMEMCHECK +void *kmemcheck_memset (void *s,int c,size_t n); +#undef memset +#define memset (s, c, n) kmemcheck_memset((s), (c), (n)) +#endif +/* * find the first occurrence of byte 'c', or 1 past the area if none */
include/asm-x86/string_64.h +1 -0
@@ -45,6 +45,7 @@ extern void *__memcpy(void *to, const void *from, size_t len);#define __HAVE_ARCH_MEMSETvoid *memset (void *s,int c,size_t n); +void *__memset (void *s,int c,size_t n);#define __HAVE_ARCH_MEMMOVEvoid *memmove (void *dest,const void *src,size_t count);
include/linux/gfp.h +2 -1
@@ -50,8 +50,9 @@ struct vm_area_struct;#define __GFP_THISNODE ((__force gfp_t)0x40000u)/* No fallback, no policies */ #define __GFP_RECLAIMABLE ((__force gfp_t)0x80000u)/* Page is reclaimable */ #define __GFP_MOVABLE ((__force gfp_t)0x100000u)/* Page is movable */ +#define __GFP_NOTRACK ((__force gfp_t)0x200000u)/* Don't track with kmemcheck */ -#define __GFP_BITS_SHIFT 21/* Room for 21 __GFP_FOO bits */ +#define __GFP_BITS_SHIFT 22/* Room for 22 __GFP_FOO bits */ #define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/* This equals 0, but use constants in case they ever change */
include/linux/interrupt.h +14 -0
@@ -377,6 +377,20 @@ static inline void tasklet_hi_schedule(struct tasklet_struct *t)__tasklet_hi_schedule (t); } +extern void __tasklet_hi_schedule_first (struct tasklet_struct *t); + +/* +* This version avoids touching any other tasklets. Needed for kmemcheck +* in order not to take any page faults while enqueueing this tasklet; +* consider VERY carefully whether you really need this or +* tasklet_hi_schedule()... +*/ +static inline void tasklet_hi_schedule_first (struct tasklet_struct *t) +{ +if (!test_and_set_bit (TASKLET_STATE_SCHED , &t->state)) +__tasklet_hi_schedule_first (t); +} +static inline void tasklet_disable_nosync (struct tasklet_struct *t) {
include/linux/kmemcheck.h new file +70 -0
70 lines not on this page · raw message
include/linux/mm_types.h +4 -0
@@ -94,6 +94,10 @@ struct page {#ifdef CONFIG_CGROUP_MEM_RES_CTLR unsigned long page_cgroup;#endif + +#ifdef CONFIG_KMEMCHECK +void *shadow; +#endif };/*
include/linux/slab.h +7 -0
@@ -34,6 +34,13 @@# define SLAB_DEBUG_OBJECTS 0x00000000UL#endif +/* Don't track use of uninitialized memory */ +#ifdef CONFIG_KMEMCHECK +# define SLAB_NOTRACK 0x00800000UL +#else +# define SLAB_NOTRACK 0x00000000UL +#endif +/* The following flags affect the page allocator grouping pages by mobility */ #define SLAB_RECLAIM_ACCOUNT 0x00020000UL/* Objects are reclaimable */ #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT/* Objects are short-lived */
include/linux/slab_def.h +81 -0
87 lines not on this page · raw message
include/linux/stacktrace.h +3 -0
@@ -2,6 +2,8 @@#define __LINUX_STACKTRACE_H #ifdef CONFIG_STACKTRACE +struct task_struct; + struct stack_trace { unsigned int nr_entries, max_entries; unsigned long *entries;
@@ -9,6 +11,7 @@ struct stack_trace { };extern void save_stack_trace (struct stack_trace *trace); +extern void save_stack_trace_bp (struct stack_trace *trace,unsigned long bp);extern void save_stack_trace_tsk (struct task_struct *tsk,struct stack_trace *trace);
init/main.c +2 -0
@@ -60,6 +60,7 @@#include <linux/sched.h> #include <linux/signal.h> #include <linux/idr.h> +#include <linux/kmemcheck.h> #include <asm/io.h> #include <asm/bugs.h>
@@ -779,6 +780,7 @@ static void __init do_pre_smp_initcalls(void) {extern int spawn_ksoftirqd (void ); +kmemcheck_init ();migration_init ();spawn_ksoftirqd ();if (!nosoftlockup)
kernel/fork.c +8 -8
@@ -150,7 +150,7 @@ void __init fork_init(unsigned long mempages)/* create a slab on which task_structs can be allocated */ task_struct_cachep =kmem_cache_create ("task_struct" ,sizeof (struct task_struct ), -ARCH_MIN_TASKALIGN ,SLAB_PANIC ,NULL ); +ARCH_MIN_TASKALIGN ,SLAB_PANIC |SLAB_NOTRACK ,NULL );#endif /* do the arch specific task caches init */
@@ -1415,23 +1415,23 @@ void __init proc_caches_init(void) { sighand_cachep =kmem_cache_create ("sighand_cache" ,sizeof (struct sighand_struct ),0 , -SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_DESTROY_BY_RCU , - sighand_ctor); +SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_DESTROY_BY_RCU | +SLAB_NOTRACK , sighand_ctor); signal_cachep =kmem_cache_create ("signal_cache" ,sizeof (struct signal_struct ),0 , -SLAB_HWCACHE_ALIGN |SLAB_PANIC ,NULL ); +SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_NOTRACK ,NULL ); files_cachep =kmem_cache_create ("files_cache" ,sizeof (struct files_struct ),0 , -SLAB_HWCACHE_ALIGN |SLAB_PANIC ,NULL ); +SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_NOTRACK ,NULL ); fs_cachep =kmem_cache_create ("fs_cache" ,sizeof (struct fs_struct ),0 , -SLAB_HWCACHE_ALIGN |SLAB_PANIC ,NULL ); +SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_NOTRACK ,NULL ); vm_area_cachep =kmem_cache_create ("vm_area_struct" ,sizeof (struct vm_area_struct ),0 , -SLAB_PANIC ,NULL ); +SLAB_PANIC |SLAB_NOTRACK ,NULL ); mm_cachep =kmem_cache_create ("mm_struct" ,sizeof (struct mm_struct ),ARCH_MIN_MMSTRUCT_ALIGN , -SLAB_HWCACHE_ALIGN |SLAB_PANIC ,NULL ); +SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_NOTRACK ,NULL ); }/*
kernel/softirq.c +11 -0
@@ -393,6 +393,17 @@ void __tasklet_hi_schedule(struct tasklet_struct *t)EXPORT_SYMBOL (__tasklet_hi_schedule); +void __tasklet_hi_schedule_first (struct tasklet_struct *t) +{ +BUG_ON (!irqs_disabled ()); + + t->next =__get_cpu_var (tasklet_hi_vec).head; +__get_cpu_var (tasklet_hi_vec).head = t; +__raise_softirq_irqoff (HI_SOFTIRQ ); +} + +EXPORT_SYMBOL (__tasklet_hi_schedule_first); +static void tasklet_action (struct softirq_action *a) {struct tasklet_struct *list;
kernel/sysctl.c +12 -0
@@ -27,6 +27,7 @@#include <linux/security.h> #include <linux/ctype.h> #include <linux/utsname.h> +#include <linux/kmemcheck.h> #include <linux/smp_lock.h> #include <linux/fs.h> #include <linux/init.h>
@@ -813,6 +814,17 @@ static struct ctl_table kern_table[] = { .child = key_sysctls, },#endif +#ifdef CONFIG_KMEMCHECK + { + .ctl_name = CTL_UNNUMBERED, + .procname = "kmemcheck", + .data = &kmemcheck_enabled, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, +#endif + /* * NOTE: do not add new entries to this table unless you have read * Documentation/sysctl/ctl_unnumbered.txt
mm/Makefile +1 -1
@@ -27,10 +27,10 @@ obj-$(CONFIG_TINY_SHMEM) += tiny-shmem.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_SLAB) += slab.o obj-$(CONFIG_SLUB) += slub.o +obj-$(CONFIG_KMEMCHECK) += kmemcheck.o obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-$(CONFIG_FS_XIP) += filemap_xip.o obj-$(CONFIG_MIGRATION) += migrate.o obj-$(CONFIG_SMP) += allocpercpu.o obj-$(CONFIG_QUICKLIST) += quicklist.o obj-$(CONFIG_CGROUP_MEM_RES_CTLR) += memcontrol.o -
mm/kmemcheck.c new file +97 -0
97 lines not on this page · raw message
mm/slab.c +18 -83
154 lines not on this page · raw message
mm/slub.c +18 -2
@@ -23,6 +23,7 @@#include <linux/kallsyms.h> #include <linux/memory.h> #include <linux/math64.h> +#include <linux/kmemcheck.h> /* * Lock order:
@@ -174,7 +175,7 @@ static inline void ClearSlabDebug(struct page *page) SLAB_TRACE | SLAB_DESTROY_BY_RCU)#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \ - SLAB_CACHE_DMA) + SLAB_CACHE_DMA | SLAB_NOTRACK)#ifndef ARCH_KMALLOC_MINALIGN #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
@@ -1122,6 +1123,13 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)stat (get_cpu_slab (s,raw_smp_processor_id ()),ORDER_FALLBACK ); } + +if (kmemcheck_enabled + && !(s->flags & (SLAB_NOTRACK |DEBUG_DEFAULT_FLAGS ))) + { +kmemcheck_alloc_shadow (s, flags, node, page,compound_order (page)); + } + page->objects =oo_objects (oo); mod_zone_page_state(page_zone(page), (s->flags & SLAB_RECLAIM_ACCOUNT) ?
@@ -1195,6 +1203,9 @@ static void __free_slab(struct kmem_cache *s, struct page *page)ClearSlabDebug (page); } +if (kmemcheck_page_is_tracked (page)) +kmemcheck_free_shadow (s, page,compound_order (page)); + mod_zone_page_state(page_zone(page), (s->flags & SLAB_RECLAIM_ACCOUNT) ? NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
@@ -1647,6 +1658,7 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,if (unlikely ((gfpflags & __GFP_ZERO) && object))memset (object,0 , objsize); +kmemcheck_slab_alloc (s, gfpflags, object, c->objsize);return object; }
@@ -1751,6 +1763,7 @@ static __always_inline void slab_free(struct kmem_cache *s,local_irq_save (flags); c =get_cpu_slab (s,smp_processor_id ()); +kmemcheck_slab_free (s, object, c->objsize);debug_check_no_locks_freed (object, c->objsize);if (!(s->flags &SLAB_DEBUG_OBJECTS ))debug_check_no_obj_freed (object, s->objsize);
@@ -2602,7 +2615,8 @@ static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)if (!s || !text || !kmem_cache_open (s, flags, text, realsize,ARCH_KMALLOC_MINALIGN , -SLAB_CACHE_DMA |__SYSFS_ADD_DEFERRED,NULL )) { +SLAB_CACHE_DMA |SLAB_NOTRACK |__SYSFS_ADD_DEFERRED, +NULL )) {kfree (s);kfree (text); goto unlock_out;
@@ -4308,6 +4322,8 @@ static char *create_unique_id(struct kmem_cache *s) *p++ ='a' ;if (s->flags &SLAB_DEBUG_FREE ) *p++ ='F' ; +if (!(s->flags &SLAB_NOTRACK )) + *p++ ='t' ;if (p != name +1 ) *p++ ='-' ; p +=sprintf (p,"%07d" , s->size);