Skip to content

Commit 6a11f75

Browse files
mitatorvalds
authored andcommitted
generic debug pagealloc
CONFIG_DEBUG_PAGEALLOC is now supported by x86, powerpc, sparc64, and s390. This patch implements it for the rest of the architectures by filling the pages with poison byte patterns after free_pages() and verifying the poison patterns before alloc_pages(). This generic one cannot detect invalid page accesses immediately but invalid read access may cause invalid dereference by poisoned memory and invalid write access can be detected after a long delay. Signed-off-by: Akinobu Mita <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 610a77e commit 6a11f75

File tree

16 files changed

+202
-19
lines changed

16 files changed

+202
-19
lines changed

arch/avr32/mm/fault.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -250,21 +250,3 @@ asmlinkage void do_bus_error(unsigned long addr, int write_access,
250250
dump_dtlb();
251251
die("Bus Error", regs, SIGKILL);
252252
}
253-
254-
/*
255-
* This functionality is currently not possible to implement because
256-
* we're using segmentation to ensure a fixed mapping of the kernel
257-
* virtual address space.
258-
*
259-
* It would be possible to implement this, but it would require us to
260-
* disable segmentation at startup and load the kernel mappings into
261-
* the TLB like any other pages. There will be lots of trickery to
262-
* avoid recursive invocation of the TLB miss handler, though...
263-
*/
264-
#ifdef CONFIG_DEBUG_PAGEALLOC
265-
void kernel_map_pages(struct page *page, int numpages, int enable)
266-
{
267-
268-
}
269-
EXPORT_SYMBOL(kernel_map_pages);
270-
#endif

arch/powerpc/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ config PPC_OF_PLATFORM_PCI
228228
depends on PPC64 # not supported on 32 bits yet
229229
default n
230230

231+
config ARCH_SUPPORTS_DEBUG_PAGEALLOC
232+
def_bool y
233+
231234
source "init/Kconfig"
232235

233236
source "kernel/Kconfig.freezer"

arch/powerpc/Kconfig.debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ config DEBUG_STACK_USAGE
3030
config DEBUG_PAGEALLOC
3131
bool "Debug page memory allocations"
3232
depends on DEBUG_KERNEL && !HIBERNATION
33+
depends on ARCH_SUPPORTS_DEBUG_PAGEALLOC
3334
help
3435
Unmap pages from the kernel linear mapping after free_pages().
3536
This results in a large slowdown, but helps to find certain types

arch/s390/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ config PGSTE
7272
config VIRT_CPU_ACCOUNTING
7373
def_bool y
7474

75+
config ARCH_SUPPORTS_DEBUG_PAGEALLOC
76+
def_bool y
77+
7578
mainmenu "Linux Kernel Configuration"
7679

7780
config S390

arch/s390/Kconfig.debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ source "lib/Kconfig.debug"
99
config DEBUG_PAGEALLOC
1010
bool "Debug page memory allocations"
1111
depends on DEBUG_KERNEL
12+
depends on ARCH_SUPPORTS_DEBUG_PAGEALLOC
1213
help
1314
Unmap pages from the kernel linear mapping after free_pages().
1415
This results in a slowdown, but helps to find certain types of

arch/sparc/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ config ARCH_NO_VIRT_TO_BUS
124124
config OF
125125
def_bool y
126126

127+
config ARCH_SUPPORTS_DEBUG_PAGEALLOC
128+
def_bool y if SPARC64
129+
127130
source "init/Kconfig"
128131

129132
source "kernel/Kconfig.freezer"

arch/sparc/Kconfig.debug

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ config STACK_DEBUG
2424

2525
config DEBUG_PAGEALLOC
2626
bool "Debug page memory allocations"
27-
depends on SPARC64 && DEBUG_KERNEL && !HIBERNATION
27+
depends on DEBUG_KERNEL && !HIBERNATION
28+
depends on ARCH_SUPPORTS_DEBUG_PAGEALLOC
2829
help
2930
Unmap pages from the kernel linear mapping after free_pages().
3031
This results in a large slowdown, but helps to find certain types

arch/x86/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ config AUDIT_ARCH
165165
config ARCH_SUPPORTS_OPTIMIZED_INLINING
166166
def_bool y
167167

168+
config ARCH_SUPPORTS_DEBUG_PAGEALLOC
169+
def_bool y
170+
168171
# Use the generic interrupt handling code in kernel/irq/:
169172
config GENERIC_HARDIRQS
170173
bool

arch/x86/Kconfig.debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ config DEBUG_STACK_USAGE
7575
config DEBUG_PAGEALLOC
7676
bool "Debug page memory allocations"
7777
depends on DEBUG_KERNEL
78+
depends on ARCH_SUPPORTS_DEBUG_PAGEALLOC
7879
---help---
7980
Unmap pages from the kernel linear mapping after free_pages().
8081
This results in a large slowdown, but helps to find certain types

include/linux/mm_types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/rwsem.h>
1212
#include <linux/completion.h>
1313
#include <linux/cpumask.h>
14+
#include <linux/page-debug-flags.h>
1415
#include <asm/page.h>
1516
#include <asm/mmu.h>
1617

@@ -174,6 +175,9 @@ struct vm_area_struct {
174175
#ifdef CONFIG_NUMA
175176
struct mempolicy *vm_policy; /* NUMA policy for the VMA */
176177
#endif
178+
#ifdef CONFIG_WANT_PAGE_DEBUG_FLAGS
179+
unsigned long debug_flags; /* Use atomic bitops on this */
180+
#endif
177181
};
178182

179183
struct core_thread {

include/linux/page-debug-flags.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef LINUX_PAGE_DEBUG_FLAGS_H
2+
#define LINUX_PAGE_DEBUG_FLAGS_H
3+
4+
/*
5+
* page->debug_flags bits:
6+
*
7+
* PAGE_DEBUG_FLAG_POISON is set for poisoned pages. This is used to
8+
* implement generic debug pagealloc feature. The pages are filled with
9+
* poison patterns and set this flag after free_pages(). The poisoned
10+
* pages are verified whether the patterns are not corrupted and clear
11+
* the flag before alloc_pages().
12+
*/
13+
14+
enum page_debug_flags {
15+
PAGE_DEBUG_FLAG_POISON, /* Page is poisoned */
16+
};
17+
18+
/*
19+
* Ensure that CONFIG_WANT_PAGE_DEBUG_FLAGS reliably
20+
* gets turned off when no debug features are enabling it!
21+
*/
22+
23+
#ifdef CONFIG_WANT_PAGE_DEBUG_FLAGS
24+
#if !defined(CONFIG_PAGE_POISONING) \
25+
/* && !defined(CONFIG_PAGE_DEBUG_SOMETHING_ELSE) && ... */
26+
#error WANT_PAGE_DEBUG_FLAGS is turned on with no debug features!
27+
#endif
28+
#endif /* CONFIG_WANT_PAGE_DEBUG_FLAGS */
29+
30+
#endif /* LINUX_PAGE_DEBUG_FLAGS_H */

include/linux/poison.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
#define TIMER_ENTRY_STATIC ((void *) 0x74737461)
1919

20+
/********** mm/debug-pagealloc.c **********/
21+
#define PAGE_POISON 0xaa
22+
2023
/********** mm/slab.c **********/
2124
/*
2225
* Magic nums for obj red zoning.

lib/Kconfig.debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ config SYSCTL_SYSCALL_CHECK
796796
to properly maintain and use. This enables checks that help
797797
you to keep things correct.
798798

799+
source mm/Kconfig.debug
799800
source kernel/trace/Kconfig
800801

801802
config PROVIDE_OHCI1394_DMA_INIT

mm/Kconfig.debug

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
config WANT_PAGE_DEBUG_FLAGS
2+
bool
3+
4+
config PAGE_POISONING
5+
bool "Debug page memory allocations"
6+
depends on DEBUG_KERNEL && !ARCH_SUPPORTS_DEBUG_PAGEALLOC
7+
depends on !HIBERNATION
8+
select DEBUG_PAGEALLOC
9+
select WANT_PAGE_DEBUG_FLAGS
10+
help
11+
Fill the pages with poison patterns after free_pages() and verify
12+
the patterns before alloc_pages(). This results in a large slowdown,
13+
but helps to find certain types of memory corruptions.
14+
15+
This option cannot enalbe with hibernation. Otherwise, it will get
16+
wrong messages for memory corruption because the free pages are not
17+
saved to the suspend image.

mm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o
2424
obj-$(CONFIG_TMPFS_POSIX_ACL) += shmem_acl.o
2525
obj-$(CONFIG_SLOB) += slob.o
2626
obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o
27+
obj-$(CONFIG_PAGE_POISONING) += debug-pagealloc.o
2728
obj-$(CONFIG_SLAB) += slab.o
2829
obj-$(CONFIG_SLUB) += slub.o
2930
obj-$(CONFIG_FAILSLAB) += failslab.o

mm/debug-pagealloc.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <linux/kernel.h>
2+
#include <linux/mm.h>
3+
#include <linux/page-debug-flags.h>
4+
#include <linux/poison.h>
5+
6+
static inline void set_page_poison(struct page *page)
7+
{
8+
__set_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags);
9+
}
10+
11+
static inline void clear_page_poison(struct page *page)
12+
{
13+
__clear_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags);
14+
}
15+
16+
static inline bool page_poison(struct page *page)
17+
{
18+
return test_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags);
19+
}
20+
21+
static void poison_highpage(struct page *page)
22+
{
23+
/*
24+
* Page poisoning for highmem pages is not implemented.
25+
*
26+
* This can be called from interrupt contexts.
27+
* So we need to create a new kmap_atomic slot for this
28+
* application and it will need interrupt protection.
29+
*/
30+
}
31+
32+
static void poison_page(struct page *page)
33+
{
34+
void *addr;
35+
36+
if (PageHighMem(page)) {
37+
poison_highpage(page);
38+
return;
39+
}
40+
set_page_poison(page);
41+
addr = page_address(page);
42+
memset(addr, PAGE_POISON, PAGE_SIZE);
43+
}
44+
45+
static void poison_pages(struct page *page, int n)
46+
{
47+
int i;
48+
49+
for (i = 0; i < n; i++)
50+
poison_page(page + i);
51+
}
52+
53+
static bool single_bit_flip(unsigned char a, unsigned char b)
54+
{
55+
unsigned char error = a ^ b;
56+
57+
return error && !(error & (error - 1));
58+
}
59+
60+
static void check_poison_mem(unsigned char *mem, size_t bytes)
61+
{
62+
unsigned char *start;
63+
unsigned char *end;
64+
65+
for (start = mem; start < mem + bytes; start++) {
66+
if (*start != PAGE_POISON)
67+
break;
68+
}
69+
if (start == mem + bytes)
70+
return;
71+
72+
for (end = mem + bytes - 1; end > start; end--) {
73+
if (*end != PAGE_POISON)
74+
break;
75+
}
76+
77+
if (!printk_ratelimit())
78+
return;
79+
else if (start == end && single_bit_flip(*start, PAGE_POISON))
80+
printk(KERN_ERR "pagealloc: single bit error\n");
81+
else
82+
printk(KERN_ERR "pagealloc: memory corruption\n");
83+
84+
print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
85+
end - start + 1, 1);
86+
dump_stack();
87+
}
88+
89+
static void unpoison_highpage(struct page *page)
90+
{
91+
/*
92+
* See comment in poison_highpage().
93+
* Highmem pages should not be poisoned for now
94+
*/
95+
BUG_ON(page_poison(page));
96+
}
97+
98+
static void unpoison_page(struct page *page)
99+
{
100+
if (PageHighMem(page)) {
101+
unpoison_highpage(page);
102+
return;
103+
}
104+
if (page_poison(page)) {
105+
void *addr = page_address(page);
106+
107+
check_poison_mem(addr, PAGE_SIZE);
108+
clear_page_poison(page);
109+
}
110+
}
111+
112+
static void unpoison_pages(struct page *page, int n)
113+
{
114+
int i;
115+
116+
for (i = 0; i < n; i++)
117+
unpoison_page(page + i);
118+
}
119+
120+
void kernel_map_pages(struct page *page, int numpages, int enable)
121+
{
122+
if (!debug_pagealloc_enabled)
123+
return;
124+
125+
if (enable)
126+
unpoison_pages(page, numpages);
127+
else
128+
poison_pages(page, numpages);
129+
}

0 commit comments

Comments
 (0)