kmemcheck v4
Subsystems (14)
PATCH 1/4 kmemcheck v4
Greetings,
I post the fourth revision of kmemcheck. It seems that we are slowly converging towards something usable :-)
General description: kmemcheck is a patch to the linux kernel that detects use of uninitialized memory. It does this by trapping every read and write to memory that was allocated dynamically (e.g. using kmalloc()). If a memory address is read that has not previously been written to, a message is printed to the kernel log.
Changes since v3:
- More clean-ups. Hopefully the SLUB bits are clearer now.
- Don't print directly from the page fault handler. Instead, we save all errors on a ring queue and print them from a helper kernel thread.
- Some experimental support for graceful bit-field handling.
- Preliminary support for use-after-free detection.
- I've also separated the patch into logical chunks.
On my machine, the kmemcheck-enabled kernel now boots into full graphical desktop. As expected, it is much, much slower than the vanilla kernel, but still surprisingly usable. Unfortunately, the kernel usually freezes hard after a couple of hours for unknown reasons -- ideas and/or patches are welcome ;-)
The patches apply to v2.6.25-rc1.
Kind regards,
Vegard Nossum
From 0fcca4341b6b1b277d936558aa3cab0f212bad9b Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Thu, 14 Feb 2008 19:10:40 +0100
Subject: [PATCH] kmemcheck: add the core kmemcheck changes
General description: kmemcheck is a patch to the linux kernel that detects use of uninitialized memory. It does this by trapping every read and write to memory that was allocated dynamically (e.g. using kmalloc()). If a memory address is read that has not previously been written to, a message is printed to the kernel log.
--- Documentation/kmemcheck.txt | 73 ++++ arch/x86/Kconfig.debug | 35 ++ arch/x86/kernel/Makefile | 2 + arch/x86/kernel/kmemcheck_32.c | 781 ++++++++++++++++++++++++++++++++++++++++ include/asm-x86/kmemcheck.h | 3 + include/asm-x86/kmemcheck_32.h | 22 ++ include/asm-x86/pgtable.h | 4 +- include/asm-x86/pgtable_32.h | 1 + include/linux/gfp.h | 3 +- include/linux/kmemcheck.h | 17 + include/linux/page-flags.h | 7 + 11 files changed, 945 insertions(+), 3 deletions(-) create mode 100644 Documentation/kmemcheck.txt create mode 100644 arch/x86/kernel/kmemcheck_32.c create mode 100644 include/asm-x86/kmemcheck.h create mode 100644 include/asm-x86/kmemcheck_32.h create mode 100644 include/linux/kmemcheck.h
Documentation/kmemcheck.txt new file +73 -0
@@ -0,0 +1,73 @@ +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 memory allocator (SLUB) +====================================== + +kmemcheck requires some assistance from the memory allocator in order to work. +The memory allocator needs to + +1. Request twice as much memory as would normally be needed. The bottom half + of the memory is what the user actually sees and uses; the upper half + contains the so-called shadow memory, which stores the status of each byte + in the bottom half, e.g. initialized or uninitialized. +2. Tell kmemcheck which parts of memory that 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 occuring, however, but marks the object in question as being +initialized so that no warnings will ever be produced for this object. + + +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 that 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. + + +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.
arch/x86/Kconfig.debug +35 -0
@@ -134,6 +134,41 @@ config IOMMU_LEAK Add a simple leak tracer to the IOMMU code. This is useful when you are debugging a buggy device driver that leaks IOMMU mappings. +config KMEMCHECK + bool "kmemcheck: trap use of uninitialized memory" + depends on M386 && !X86_GENERIC && !SMP + depends on !CC_OPTIMIZE_FOR_SIZE + depends on !DEBUG_PAGEALLOC && SLUB + select DEBUG_INFO + select FRAME_POINTER + select STACKTRACE + default n + help + This option enables tracing of dynamically allocated kernel memory + to see if memory is used before it has been given an initial value. + Be aware that this requires half of your memory for bookkeeping and + will insert extra code at *every* read and write to tracked memory + thus slow down the kernel code (but user code is unaffected). + +config KMEMCHECK_PARTIAL_OK + bool "kmemcheck: allow partially uninitialized memory" + depends on KMEMCHECK + default y + help + This option works around certain GCC optimizations that produce + 32-bit reads from 16-bit variables where the upper 16 bits are + thrown away afterwards. This may of course also hide some real + bugs. + +config KMEMCHECK_BITOPS_OK + bool "kmemcheck: allow bit-field manipulation" + depends on KMEMCHECK + default n + help + This option silences warnings that would be generated for bit-field + accesses where not all the bits are initialized at the same time. + This may also hide some real bugs. + # # IO delay types: #
arch/x86/kernel/Makefile +2 -0
@@ -78,6 +78,8 @@ endif obj-$(CONFIG_SCx200) += scx200.o scx200-y += scx200_32.o +obj-$(CONFIG_KMEMCHECK) += kmemcheck_32.o + ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y)
arch/x86/kernel/kmemcheck_32.c new file +781 -0
781 lines not on this page · raw message
include/asm-x86/kmemcheck.h new file +3 -0
@@ -0,0 +1,3 @@ +#ifdef CONFIG_X86_32 +# include "kmemcheck_32.h" +#endif
include/asm-x86/kmemcheck_32.h new file +22 -0
@@ -0,0 +1,22 @@ +#ifndef ASM_X86_KMEMCHECK_32_H +#define ASM_X86_KMEMCHECK_32_H + +#include <linux/percpu.h> +#include <asm/pgtable.h> + +enum kmemcheck_method { +KMEMCHECK_READ , +KMEMCHECK_WRITE , +}; + +#ifdef CONFIG_KMEMCHECK +void kmemcheck_prepare (struct pt_regs *regs); + +void kmemcheck_show (struct pt_regs *regs); +void kmemcheck_hide (struct pt_regs *regs); + +void kmemcheck_access (struct pt_regs *regs, +unsigned long address,enum kmemcheck_method method); +#endif + +#endif
include/asm-x86/pgtable.h +2 -2
@@ -17,8 +17,8 @@#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_PAT_LARGE 12/* On 2MB or 1GB pages */ +#define _PAGE_BIT_HIDDEN 11#define _PAGE_BIT_NX 63/* No execute: only valid after cpuid check */ /*
@@ -37,9 +37,9 @@#define _PAGE_GLOBAL (_AC(1, L)<<_PAGE_BIT_GLOBAL)/* Global TLB entry */ #define _PAGE_UNUSED1 (_AC(1, L)<<_PAGE_BIT_UNUSED1)#define _PAGE_UNUSED2 (_AC(1, L)<<_PAGE_BIT_UNUSED2) -#define _PAGE_UNUSED3 (_AC(1, L)<<_PAGE_BIT_UNUSED3)#define _PAGE_PAT (_AC(1, L)<<_PAGE_BIT_PAT)#define _PAGE_PAT_LARGE (_AC(1, L)<<_PAGE_BIT_PAT_LARGE) +#define _PAGE_HIDDEN (_AC(1, L)<<_PAGE_BIT_HIDDEN)#if defined(CONFIG_X86_64 ) || defined(CONFIG_X86_PAE )#define _PAGE_NX (_AC(1, ULL) << _PAGE_BIT_NX)
include/asm-x86/pgtable_32.h +1 -0
@@ -87,6 +87,7 @@ void paging_init(void);extern unsigned long pg0[];#define pte_present (x) ((x).pte_low & (_PAGE_PRESENT | _PAGE_PROTNONE)) +#define pte_hidden (x) ((x).pte_low & (_PAGE_HIDDEN))/* To avoid harmful races, pmd_none(x) should check only the lower when PAE */ #define pmd_none (x) (!(unsigned long)pmd_val(x))
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/kmemcheck.h new file +17 -0
@@ -0,0 +1,17 @@ +#ifndef LINUX_KMEMCHECK_H +#define LINUX_KMEMCHECK_H + +#ifdef CONFIG_KMEMCHECK +void kmemcheck_show_pages (struct page *p,unsigned int n); +void kmemcheck_hide_pages (struct page *p,unsigned int n); + +void kmemcheck_mark_unallocated (void *address,unsigned int n); +void kmemcheck_mark_uninitialized (void *address,unsigned int n); +void kmemcheck_mark_initialized (void *address,unsigned int n); +void kmemcheck_mark_freed (void *address,unsigned int n); + +void kmemcheck_mark_unallocated_pages (struct page *p,unsigned int n); +void kmemcheck_mark_uninitialized_pages (struct page *p,unsigned int n); +#endif + +#endif
include/linux/page-flags.h +7 -0
@@ -90,6 +90,8 @@#define PG_reclaim 17/* To be reclaimed asap */ #define PG_buddy 19/* Page is free, on buddy lists */ +#define PG_tracked 20/* Page is tracked by kmemcheck */ +/* PG_readahead is only used for file reads; PG_reclaim is only for writes */ #define PG_readahead PG_reclaim/* Reminder to do async read-ahead */
@@ -296,6 +298,11 @@ static inline void __ClearPageTail(struct page *page)#define SetPageUncached (page) set_bit(PG_uncached, &(page)->flags)#define ClearPageUncached (page) clear_bit(PG_uncached, &(page)->flags) +#define PageTracked (page) test_bit(PG_tracked, &(page)->flags) +#define SetPageTracked (page) set_bit(PG_tracked, &(page)->flags) +#define ClearPageTracked (page) clear_bit(PG_tracked, &(page)->flags) + +struct page ;/* forward declaration */ extern void cancel_dirty_page (struct page *page,unsigned int account_size);
Signature · 1 line
1.5.3.8
PATCH 2/4 kmemcheck v4
From 5cbf7d1cb81b4f8bb4d80c027b74cdf0f08aaaff Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Thu, 14 Feb 2008 19:20:51 +0100
Subject: [PATCH] kmemcheck: add the x86 hooks
The hooks that we modify are:
- Page fault handler (to handle kmemcheck faults)
- Debug exception handler (to hide pages after single-stepping the instruction that caused the page fault)
Also redefine memset() to use the optimized version if kmemcheck is enabled.
--- arch/x86/kernel/cpu/common.c | 7 +++++++ arch/x86/kernel/entry_32.S | 8 ++++---- arch/x86/kernel/traps_32.c | 22 +++++++++++++++++++++- arch/x86/mm/fault.c | 21 +++++++++++++++++---- include/asm-x86/string_32.h | 8 ++++++++ 5 files changed, 57 insertions(+), 9 deletions(-)
arch/x86/kernel/cpu/common.c +7 -0
@@ -634,6 +634,13 @@ void __init early_cpu_init(void)nexgen_init_cpu ();umc_init_cpu ();early_cpu_detect (); + +#ifdef CONFIG_KMEMCHECK +/* +* We need 4K granular PTEs for kmemcheck: +*/ +setup_clear_cpu_cap (X86_FEATURE_PSE ); +#endif }/* Make sure %fs is initialized properly in idle threads */
arch/x86/kernel/entry_32.S +4 -4
@@ -289,7 +289,7 @@ ENTRY(ia32_sysenter_target) CFI_DEF_CFA esp, 0 CFI_REGISTER esp, ebp movl TSS_sysenter_sp0(%esp),%esp -sysenter_past_esp: +ENTRY(sysenter_past_esp) /* * No need to follow this irqs on/off section: the syscall * disabled irqs and here we enable it straight after entry:
@@ -767,7 +767,7 @@ label: \ CFI_ADJUST_CFA_OFFSET 4; \ CFI_REL_OFFSET eip, 0 -KPROBE_ENTRY(debug) +KPROBE_ENTRY(x86_debug) RING0_INT_FRAME cmpl $ia32_sysenter_target,(%esp) jne debug_stack_correct
@@ -781,7 +781,7 @@ debug_stack_correct: call do_debug jmp ret_from_exception CFI_ENDPROC -KPROBE_END(debug) +KPROBE_END(x86_debug) /* * NMI is doubly nasty. It can happen _while_ we're handling
@@ -835,7 +835,7 @@ nmi_debug_stack_check: /* We have a RING0_INT_FRAME here */ cmpw $__KERNEL_CS,16(%esp) jne nmi_stack_correct - cmpl $debug,(%esp) + cmpl $x86_debug,(%esp) jb nmi_stack_correct cmpl $debug_esp_fix_insn,(%esp) ja nmi_stack_correct
arch/x86/kernel/traps_32.c +21 -1
@@ -56,6 +56,7 @@#include <asm/arch_hooks.h> #include <linux/kdebug.h> #include <asm/stacktrace.h> +#include <asm/kmemcheck.h> #include <linux/module.h>
@@ -841,6 +842,10 @@ void __kprobes do_int3(struct pt_regs *regs, long error_code) }#endif +extern void ia32_sysenter_target (void ); +extern void sysenter_past_esp (void ); +extern void x86_debug (void ); +/* * Our handling of the processor debug registers is non-trivial. * We do not clear them on entry and exit from the kernel. Therefore
@@ -872,6 +877,20 @@ void __kprobes do_debug(struct pt_regs * regs, long error_code)get_debugreg (condition,6 ); +#ifdef CONFIG_KMEMCHECK +/* Catch kmemcheck conditions first of all! */ +if (condition &DR_STEP ) { +if (!(regs->flags &VM_MASK ) && !user_mode (regs) && + ((void *)regs->ip != system_call) && + ((void *)regs->ip != x86_debug) && + ((void *)regs->ip != ia32_sysenter_target) && + ((void *)regs->ip != sysenter_past_esp)) { +kmemcheck_hide (regs); +return ; + } + } +#endif +/* * The processor cleared BTF, so don't mark that we need it set. */
@@ -881,6 +900,7 @@ void __kprobes do_debug(struct pt_regs * regs, long error_code)if (notify_die (DIE_DEBUG ,"debug" , regs, condition, error_code,SIGTRAP ) ==NOTIFY_STOP )return ; +/* It's safe to allow irq's after DR6 has been saved */ if (regs->flags &X86_EFLAGS_IF )local_irq_enable ();
@@ -1154,7 +1174,7 @@ void __init trap_init(void)#endif set_trap_gate (0 ,÷_error); -set_intr_gate (1 ,&debug); +set_intr_gate (1 ,&x86_debug);set_intr_gate (2 ,&nmi);set_system_intr_gate (3 , &int3);/* int3/4 can be called from all */ set_system_gate (4 ,&overflow);
arch/x86/mm/fault.c +17 -4
@@ -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> /*
@@ -493,7 +494,8 @@ static int spurious_fault(unsigned long address,* * This assumes no large pages in there. */ -static int vmalloc_fault (unsigned long address) +static int vmalloc_fault (struct pt_regs *regs,unsigned long address, +unsigned long error_code) {#ifdef CONFIG_X86_32 unsigned long pgd_paddr;
@@ -511,8 +513,19 @@ static int vmalloc_fault(unsigned long address)if (!pmd_k)return -1 ; pte_k =pte_offset_kernel (pmd_k, address); -if (!pte_present (*pte_k)) -return -1 ; +if (!pte_present (*pte_k)) { +if (!pte_hidden (*pte_k)) +return -1 ; + +#ifdef CONFIG_KMEMCHECK +kmemcheck_prepare (regs); +if (error_code &2 ) +kmemcheck_access (regs, address,KMEMCHECK_WRITE ); +else +kmemcheck_access (regs, address,KMEMCHECK_READ ); +kmemcheck_show (regs); +#endif + }return 0 ;#else pgd_t *pgd, *pgd_ref;
@@ -623,7 +636,7 @@ void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)if (unlikely (address >=TASK_SIZE64 )) {#endif if (!(error_code & (PF_RSVD |PF_USER |PF_PROT )) && -vmalloc_fault (address) >=0 ) +vmalloc_fault (regs, address, error_code) >=0 )return ;/* Can handle a stale RO->RW TLB */
include/asm-x86/string_32.h +8 -0
@@ -262,6 +262,14 @@ __asm__ __volatile__( \ __constant_c_x_memset((s),(0x01010101UL*(unsigned char)(c)),(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 (unsigned long s,int c,size_t n); +#undef memset +#define memset (s, c, n) kmemcheck_memset((unsigned long) (s), (c), (n)) +#endif +/* * find the first occurrence of byte 'c', or 1 past the area if none */
Signature · 1 line
1.5.3.8
PATCH 3/4 kmemcheck v4
In reply to Vegard NossumFrom 4ce1c09e38b2402dc04f0246916f3c23abe8f3e1 Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Thu, 14 Feb 2008 19:25:39 +0100
Subject: [PATCH] kmemcheck: make SLUB use kmemcheck
With kmemcheck enabled, SLUB needs to do this:
1. Request twice as much memory as would normally be needed. The bottom half of the memory is what the user actually sees and uses; the upper half contains the so-called shadow memory, which stores the status of each byte in the bottom half, e.g. initialized or uninitialized.
2. Tell kmemcheck which parts of memory that 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 occuring, however, but marks the object in question as being initialized so that no warnings will ever be produced for this object.
--- include/linux/slab.h | 1 + kernel/fork.c | 15 ++++--- mm/slub.c | 113 +++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 102 insertions(+), 27 deletions(-)
include/linux/slab.h +1 -0
@@ -28,6 +28,7 @@#define SLAB_DESTROY_BY_RCU 0x00080000UL/* Defer freeing slabs to RCU */ #define SLAB_MEM_SPREAD 0x00100000UL/* Spread some memory over cpuset */ #define SLAB_TRACE 0x00200000UL/* Trace allocations and frees */ +#define SLAB_NOTRACK 0x00400000UL/* Don't track use of uninitialized memory */ /* The following flags affect the page allocator grouping pages by mobility */ #define SLAB_RECLAIM_ACCOUNT 0x00020000UL/* Objects are reclaimable */
kernel/fork.c +8 -7
@@ -141,7 +141,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 /*
@@ -1547,23 +1547,24 @@ 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 , +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 ); }/*
mm/slub.c +93 -20
229 lines not on this page · raw message
Signature · 1 line
1.5.3.8
PATCH 4/4 kmemcheck v4
In reply to Vegard NossumFrom f65bd157b88d3ae9a75737cdff5d6f27920af43d Mon Sep 17 00:00:00 2001
From: Ingo Molnar <[email protected]>
Date: Thu, 14 Feb 2008 20:53:42 +0100
Subject: [PATCH] kmemcheck: Fix-up (some bogus) reports
--- include/asm-generic/siginfo.h | 8 ++++++++ include/linux/fs.h | 4 ++-- include/linux/netdevice.h | 4 ++-- include/linux/skbuff.h | 6 +++++- include/net/inet_sock.h | 3 ++- include/net/tcp.h | 11 +++++++++++ init/do_mounts.c | 8 ++++++-- kernel/signal.c | 12 ++++++++++++ net/core/skbuff.c | 6 ++++++ net/ipv4/tcp_output.c | 4 ++++ 10 files changed, 58 insertions(+), 8 deletions(-)
include/asm-generic/siginfo.h +8 -0
@@ -278,11 +278,19 @@ void do_schedule_next_timer(struct siginfo *info);static inline void copy_siginfo (struct siginfo *to,struct siginfo *from) { +#ifdef CONFIG_KMEMCHECK +memcpy (to, from,sizeof (*to)); +#else +/* +* Optimization, only copy up to the size of the largest known +* union member: +*/ if (from->si_code <0 )memcpy (to, from,sizeof (*to));else /* _sigchld is currently the largest know union member */ memcpy (to, from, __ARCH_SI_PREAMBLE_SIZE +sizeof (from->_sifields._sigchld)); +#endif }#endif
include/linux/fs.h +2 -2
@@ -875,8 +875,8 @@ struct file_lock {struct pid *fl_nspid;wait_queue_head_t fl_wait;struct file *fl_file; -unsigned char fl_flags; -unsigned char fl_type; +unsigned int fl_flags; +unsigned int fl_type;loff_t fl_start;loff_t fl_end;
include/linux/netdevice.h +2 -2
@@ -193,8 +193,8 @@ struct dev_addr_list {struct dev_addr_list *next;u8 da_addr[MAX_ADDR_LEN ]; -u8 da_addrlen; -u8 da_synced; +unsigned int da_addrlen; +unsigned int da_synced;int da_users;int da_gusers; };
include/linux/skbuff.h +5 -1
@@ -1294,7 +1294,11 @@ static inline void __skb_queue_purge(struct sk_buff_head *list)static inline struct sk_buff *__dev_alloc_skb (unsigned int length,gfp_t gfp_mask) { -struct sk_buff *skb =alloc_skb (length +NET_SKB_PAD , gfp_mask); +struct sk_buff *skb; +#ifdef CONFIG_KMEMCHECK + gfp_mask |= __GFP_ZERO; +#endif + skb =alloc_skb (length +NET_SKB_PAD , gfp_mask);if (likely (skb))skb_reserve (skb,NET_SKB_PAD );return skb;
include/net/inet_sock.h +2 -1
@@ -73,7 +73,8 @@ struct inet_request_sock { sack_ok : 1, wscale_ok : 1, ecn_ok : 1, - acked : 1; + acked : 1, + __filler : 3;struct ip_options *opt; };
include/net/tcp.h +11 -0
@@ -951,6 +951,17 @@ static inline void tcp_openreq_init(struct request_sock *req,tcp_rsk (req)->rcv_isn =TCP_SKB_CB (skb)->seq; req->mss = rx_opt->mss_clamp; req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval :0 ; +#ifdef CONFIG_KMEMCHECK +/* bitfield init */ + ireq->snd_wscale = + ireq->rcv_wscale = + ireq->tstamp_ok = + ireq->sack_ok = + ireq->wscale_ok = + ireq->ecn_ok = + ireq->acked = + ireq->__filler =0 ; +#endif ireq->tstamp_ok = rx_opt->tstamp_ok; ireq->sack_ok = rx_opt->sack_ok; ireq->snd_wscale = rx_opt->snd_wscale;
init/do_mounts.c +6 -2
@@ -201,9 +201,13 @@ static int __init do_mount_root(char *name, char *fs, int flags, void *data)return 0 ; } +#if PAGE_SIZE <PATH_MAX +# error increase the fs_names allocation size here +#endif +void __init mount_block_root (char *name,int flags) { -char *fs_names =__getname (); +char *fs_names = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO,1 );char *p;#ifdef CONFIG_BLOCK char b[BDEVNAME_SIZE ];
@@ -251,7 +255,7 @@ retry:#endif panic ("VFS: Unable to mount root fs on %s" , b); out: -putname (fs_names); +free_pages ((unsigned long )fs_names,1 ); }#ifdef CONFIG_ROOT_NFS
kernel/signal.c +12 -0
@@ -691,6 +691,12 @@ static int send_signal(int sig, struct siginfo *info, struct task_struct *t,list_add_tail (&q->list, &signals->list);switch ((unsigned long ) info) {case (unsigned long )SEND_SIG_NOINFO : +/* +* Make sure we always have a fully initialized +* siginfo struct: +*/ +memset (&q->info,0 ,sizeof (q->info)); + q->info.si_signo = sig; q->info.si_errno =0 ; q->info.si_code =SI_USER ;
@@ -698,6 +704,12 @@ static int send_signal(int sig, struct siginfo *info, struct task_struct *t, q->info.si_uid = current->uid;break ;case (unsigned long )SEND_SIG_PRIV : +/* +* Make sure we always have a fully initialized +* siginfo struct: +*/ +memset (&q->info,0 ,sizeof (q->info)); + q->info.si_signo = sig; q->info.si_errno =0 ; q->info.si_code =SI_KERNEL ;
net/core/skbuff.c +6 -0
@@ -223,6 +223,9 @@ struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,struct sk_buff *child = skb +1 ;atomic_t *fclone_ref = (atomic_t *) (child +1 ); +#ifdef CONFIG_KMEMCHECK +memset (child,0 , offsetof(struct sk_buff , tail)); +#endif skb->fclone =SKB_FCLONE_ORIG ;atomic_set (fclone_ref,1 );
@@ -255,6 +258,9 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev,int node = dev->dev.parent ?dev_to_node (dev->dev.parent) :-1 ;struct sk_buff *skb; +#ifdef CONFIG_KMEMCHECK + gfp_mask |= __GFP_ZERO; +#endif skb =__alloc_skb (length +NET_SKB_PAD , gfp_mask,0 , node);if (likely (skb)) {skb_reserve (skb,NET_SKB_PAD );
net/ipv4/tcp_output.c +4 -0
@@ -333,6 +333,10 @@ static inline void TCP_ECN_send(struct sock *sk, struct sk_buff *skb,static void tcp_init_nondata_skb (struct sk_buff *skb,u32 seq,u8 flags) { skb->csum =0 ; + skb->local_df = skb->cloned = skb->ip_summed = skb->nohdr = + skb->nfctinfo =0 ; + skb->pkt_type = skb->fclone = skb->ipvs_property = skb->peeked = + skb->nf_trace =0 ;TCP_SKB_CB (skb)->flags = flags;TCP_SKB_CB (skb)->sacked =0 ;
Signature · 1 line
1.5.3.8
Hi,
Quoted 5 lines · Vegard Nossum
include/linux/skbuff.h +0
-0
include/linux/skbuff.h static inline void __skb_queue_purge(struct · line 1301
Quoted 9 lines
include/linux/skbuff.h +4
-1@@ -1294,7 +1294,11 @@ static inline void __skb_queue_purge(struct sk_buff_head *list)static inline struct sk_buff *__dev_alloc_skb (unsigned int length,gfp_t gfp_mask) { -struct sk_buff *skb =alloc_skb (length +NET_SKB_PAD , gfp_mask); +struct sk_buff *skb; +#ifdef CONFIG_KMEMCHECK + gfp_mask |= __GFP_ZERO; +#endif
Use __GFP_NOTRACK here (no need to wrap it in CONFIG_KMEMCHECK either).
Quoted 26 lines
include/linux/skbuff.h +1
-0+ skb =alloc_skb (length +NET_SKB_PAD , gfp_mask);if (likely (skb))skb_reserve (skb,NET_SKB_PAD );return skb;init/do_mounts.c +5
-1@@ -201,9 +201,13 @@ static int __init do_mount_root(char *name, char *fs, int flags, void *data)return 0 ; } +#if PAGE_SIZE <PATH_MAX +# error increase the fs_names allocation size here +#endif +void __init mount_block_root (char *name,int flags) { -char *fs_names =__getname (); +char *fs_names = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO,1 );char *p;#ifdef CONFIG_BLOCK char b[BDEVNAME_SIZE];
init/do_mounts.c retry: · line 258
Quoted 5 lines
init/do_mounts.c +1
-1@@ -251,7 +255,7 @@ retry:#endif panic ("VFS: Unable to mount root fs on %s" , b); out: -putname (fs_names); +free_pages ((unsigned long )fs_names,1 );
As discussed before, I don't think kmemcheck should be complaining about this (even though this is a potential bug). Have you tried with the current patches to see if it still triggers? Could have been one of the kmemcheck bugs, no?
init/do_mounts.c struct sk_buff *__netdev_alloc_skb(struct net_device · line 264
Quoted 8 lines
init/do_mounts.c +3
-0@@ -255,6 +258,9 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;struct sk_buff *skb; +#ifdef CONFIG_KMEMCHECK + gfp_mask |= __GFP_ZERO; +#endif
__GFP_NOTRACK here
Pekka
Quoted 19 lines · Vegard Nossum
From
4ce1c09e38b2402dc04f0246916f3c23abe8f3e1Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Thu, 14 Feb 2008 19:25:39 +0100
Subject: [PATCH] kmemcheck: make SLUB use kmemcheckWith kmemcheck enabled, SLUB needs to do this:
1. Request twice as much memory as would normally be needed. The bottom half of the memory is what the user actually sees and uses; the upper half contains the so-called shadow memory, which stores the status of each
byte
in the bottom half, e.g. initialized or uninitialized.
2. Tell kmemcheck which parts of memory that 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_NOTRACKflag, it will never return memory that can take page faults because of kmemcheck.
Quoted 6 lines
If a slab cache is NOT set up using the
SLAB_NOTRACKflag, callers can still request memory with the__GFP_NOTRACKflag. This does not prevent the page faults from occuring, however, but marks the object in question as being initialized so that no warnings will ever be produced for this object.Signed-off-by Vegard Nossum <[email protected]>
Looks good to me.
Quoted 76 lines · Pekka Enberg
Hi,
Quoted 5 lines · Vegard Nossum
include/linux/skbuff.h +0
-0include/linux/skbuff.h static inline void __skb_queue_purge(struct · line 1301
Quoted 9 lines
include/linux/skbuff.h +4
-1@@ -1294,7 +1294,11 @@ static inline void __skb_queue_purge(struct sk_buff_head *list)static inline struct sk_buff *__dev_alloc_skb (unsigned int length,gfp_t gfp_mask) { -struct sk_buff *skb =alloc_skb (length +NET_SKB_PAD , gfp_mask); +struct sk_buff *skb; +#ifdef CONFIG_KMEMCHECK + gfp_mask |= __GFP_ZERO; +#endif Use
__GFP_NOTRACKhere (no need to wrap it inCONFIG_KMEMCHECKeither).Quoted 28 lines
include/linux/skbuff.h +1
-0+ skb =alloc_skb (length +NET_SKB_PAD , gfp_mask);if (likely (skb))skb_reserve(skb, NET_SKB_PAD); return skb;init/do_mounts.c +5
-1@@ -201,9 +201,13 @@ static int __init do_mount_root(char *name, char *fs, int flags, void *data)return 0 ; } +#if PAGE_SIZE <PATH_MAX +# error increase the fs_names allocation size here +#endif +void __init mount_block_root (char *name,int flags) { -char *fs_names =__getname (); +char *fs_names = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO,1 );char *p;#ifdef CONFIG_BLOCK char b[BDEVNAME_SIZE];init/do_mounts.c retry: · line 258
Quoted 5 lines
init/do_mounts.c +1
-1@@ -251,7 +255,7 @@ retry:#endif panic ("VFS: Unable to mount root fs on %s" , b); out: -putname (fs_names); +free_pages ((unsigned long )fs_names,1 );As discussed before, I don't think kmemcheck should be complaining about
this (even though this is a potential bug). Have you tried with the current patches to see if it still triggers? Could have been one of the kmemcheck bugs, no?init/do_mounts.c struct sk_buff *__netdev_alloc_skb(struct net_device · line 264
Quoted 8 lines
init/do_mounts.c +3
-0@@ -255,6 +258,9 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;struct sk_buff *skb; +#ifdef CONFIG_KMEMCHECK + gfp_mask |= __GFP_ZERO; +#endif
__GFP_NOTRACKhere
Quoted 2 lines
Pekka
Will fix those, thanks!
Vegard
arch/x86/kernel/kmemcheck_32.c line 253
Quoted 7 lines · Vegard Nossum
arch/x86/kernel/kmemcheck_32.c new file +7
-0+DEFINE_PER_CPU (bool, kmemcheck_busy) = false; +DEFINE_PER_CPU (uint32_t, kmemcheck_addr1) =0 ; +DEFINE_PER_CPU (uint32_t, kmemcheck_addr2) =0 ; +DEFINE_PER_CPU (uint32_t, kmemcheck_reg_flags) =0 ; + +DEFINE_PER_CPU (int, kmemcheck_num) =0 ; +DEFINE_PER_CPU (int, kmemcheck_balance) =0 ;
No need to explicitly initialize these, they're automatically zeroed.
arch/x86/kernel/kmemcheck_32.c line 266
Quoted 13 lines
arch/x86/kernel/kmemcheck_32.c new file +13
-0+ +/* +* Called from the #PF handler. +*/ +void +kmemcheck_show (struct pt_regs *regs) +{ +int n; + +BUG_ON (!irqs_disabled ()); + +if (__get_cpu_var (kmemcheck_balance) !=0 ) { + oops_in_progress =1 ;
Why?
Quoted 5 lines
arch/x86/kernel/kmemcheck_32.c new file +4
-0+panic ("kmemcheck: extra #PF" ); + } + +/*
arch/x86/kernel/kmemcheck_32.c line 313
Quoted 10 lines
arch/x86/kernel/kmemcheck_32.c new file +10
-0+* Called from the #DB handler. +*/ +void +kmemcheck_hide (struct pt_regs *regs) +{ +BUG_ON (!irqs_disabled ()); + + --__get_cpu_var (kmemcheck_balance); +if (unlikely (__get_cpu_var (kmemcheck_balance) !=0 )) { + oops_in_progress =1 ;
ditto
Quoted 11 lines
arch/x86/kernel/kmemcheck_32.c new file +11
-0+panic ("kmemcheck: extra #DB" ); + } + +static void +kmemcheck_read (struct pt_regs *regs,uint32_t address,unsigned int size) +{ +void *shadow; +enum shadow status; + + shadow =address_get_shadow (address); +if (!shadow)
arch/x86/kernel/kmemcheck_32.c line 593
Quoted 10 lines
arch/x86/kernel/kmemcheck_32.c new file +10
-0+return ; + + status =test (shadow, size); +if (status ==SHADOW_INITIALIZED ) +return ; + +/* Don't warn about it again. */ +set (shadow, size); + + oops_in_progress =1 ;
I don't see you setting that to zero anywhere. What's this doing here anyway?
Quoted 10 lines
arch/x86/kernel/kmemcheck_32.c new file +6
-0+error_save (status, address, size, regs); +} + +/* +* A faster implementation of memset() when tracking is enabled. We cannot +* assume that all pages within the range are tracked, so copying has to be
+ * split into page-sized (or smaller, for the ends) chunks.
+ */
arch/x86/kernel/kmemcheck_32.c line 748
Quoted 5 lines
arch/x86/kernel/kmemcheck_32.c new file +5
-0+void +kmemcheck_memset (unsigned long s,int c,size_t n) +{ +unsigned long a_page, a_offset; +unsigned long b_page, b_offset;
Uhm, s/a_page/start_page/g, s/b_page/end_page/g and same for the offsets too.
Quoted 9 lines
include/linux/gfp.h +0
-0@@ -50,8 +50,9 @@ struct vm_area_struct;#define __GFP_THISNODE ((__force gfp_t)0x40000u)/* No fallback, no policies */
#define__GFP_RECLAIMABLE((__forcegfp_t)0x80000u) /* Page is
reclaimable */
Quoted 3 lines
#define __GFP_MOVABLE ((__force gfp_t)0x100000u) /* Page is movable */ +#define __GFP_NOTRACK ((__force gfp_t)0x200000u) /* Don't trackwith kmemcheck */
I think we can set __GFP_NOTRACK to zero to eliminate all dead code when
CONFIG_KMEMCHECK is disabled?
include/linux/page-flags.h line 94
Quoted 10 lines
include/linux/page-flags.h +2
-0@@ -90,6 +90,8 @@#define PG_reclaim 17/* To be reclaimed asap */ #define PG_buddy 19/* Page is free, on buddy lists */ +#define PG_tracked 20/* Page is tracked by kmemcheck */ +
I think we should re-use PG_owner_priv_1 for this (see PG_pinned, for example).
Quoted 2 lines · On Thu, 14 Feb 2008 20:59:46 +0100 Vegard Nossum wrote:
Greetings,
Just a few doc comments (below).
Quoted 41 lines
From
0fcca4341b6b1b277d936558aa3cab0f212bad9bMon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Thu, 14 Feb 2008 19:10:40 +0100
Subject: [PATCH] kmemcheck: add the core kmemcheck changesGeneral description: kmemcheck is a patch to the linux kernel that detects use of uninitialized memory. It does this by trapping every read and write to memory that was allocated dynamically (e.g. using
kmalloc()). If a memory address is read that has not previously been written to, a message is printed to the kernel log.Signed-off-by Vegard Nossum <[email protected]>--- Documentation/kmemcheck.txt | 73 ++++ arch/x86/Kconfig.debug | 35 ++ arch/x86/kernel/Makefile | 2 + arch/x86/kernel/kmemcheck_32.c | 781 ++++++++++++++++++++++++++++++++++++++++ include/asm-x86/kmemcheck.h | 3 + include/asm-x86/kmemcheck_32.h | 22 ++ include/asm-x86/pgtable.h | 4 +- include/asm-x86/pgtable_32.h | 1 + include/linux/gfp.h | 3 +- include/linux/kmemcheck.h | 17 + include/linux/page-flags.h | 7 + 11 files changed, 945 insertions(+), 3 deletions(-)create mode 100644
Documentation/kmemcheck.txtcreate mode 100644arch/x86/kernel/kmemcheck_32.ccreate mode 100644include/asm-x86/kmemcheck.hcreate mode 100644include/asm-x86/kmemcheck_32.hcreate mode 100644include/linux/kmemcheck.hDocumentation/kmemcheck.txt new file +2
-0@@ -0,0 +1,73 @@ + +Changes to the memory allocator (SLUB)
Documentation/kmemcheck.txt line 31
Quoted 10 lines
Documentation/kmemcheck.txt new file +10
-0+====================================== + +kmemcheck requires some assistance from the memory allocator in order to work. +The memory allocator needs to + +1. Request twice as much memory as would normally be needed. The bottom half + of the memory is what the user actually sees and uses; the upper half + contains the so-called shadow memory, which stores the status of each byte + in the bottom half, e.g. initialized or uninitialized. +2. Tell kmemcheck which parts of memory that should be marked uninitialized.
Drop "that".
Documentation/kmemcheck.txt line 40
Quoted 9 lines
Documentation/kmemcheck.txt new file +9
-0+ 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 occuring, however, but marks the object in question as being
occurring,
Documentation/kmemcheck.txt line 50
Quoted 10 lines
Documentation/kmemcheck.txt new file +10
-0+initialized so that no warnings will ever be produced for this object. + + +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 that will be used or thrown away. Consequently,
Drop "that".
Documentation/kmemcheck.txt line 60
Quoted 10 lines
Documentation/kmemcheck.txt new file +10
-0+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. +
---
~Randy
The ifdefs are quite ugly. I would recommend to define standard functions (kmemcheck_init_zero or similar and an own __GFP flag) that can be used without ifdef and easily nop'ed out on !KMEMCHECK kernels.
-Andi
Quoted 3 lines · Andi Kleen
The ifdefs are quite ugly. I would recommend to define standard functions (
kmemcheck_init_zeroor similar and an own__GFPflag) that can be used without ifdef and easily nop'ed out on !KMEMCHECK kernels.
Yes, they are. We do in fact have a flag for this purpose, __GFP_NOTRACK (or SLAB_NOTRACK for entire slab caches). I have now changed some of the hunks to use this method instead of ifdefs. Thanks for pointing this out.
Also note that this patch is not meant to be a part of kmemcheck itself; it simply silences some of the (some bogus) warnings. Preferably, each one of these call-sites should be carefully audited and "fixed" independently of kmemcheck itself. In fact, the patch should probably be split, one for the bogus warnings (where the bogus-warning fixes are no-ops if kmemcheck is disabled), and the real errors in their own patches.
There is currently a huge problem with bit-fields. When bit-fields are initialized, gcc may use the AND/OR instructions to initialize one field at a time (depending on what the C code looks like, of course). This is seen by kmemcheck as 1. full 8/16/32-bit read 2. full 8/16/32-bit write. Therefore the first initialization of (access to) a bit-field variable will generally cause a warning from kmemcheck. And this is perfectly legal from the compiler point of view.
Ingo's approach so far has been to initialize all the bit-field variables at once. This is probably what the author of the original code wants to do anyway, though not in all cases. I admit that I am not confident enough with kernel code myself to decide what the appropriate fix would be in most cases of kmemcheck warnings. Therefore, take this last patch with a grain of salt (for now).
Thank you.
Vegard