Loading Documentation/page_owner.c 0 → 100644 +134 −0 Original line number Diff line number Diff line /* * User-space helper to sort the output of /sys/kernel/debug/page_owner * * Example use: * cat /sys/kernel/debug/page_owner > page_owner_full.txt * grep -v ^PFN page_owner_full.txt > page_owner.txt * ./sort page_owner.txt sorted_page_owner.txt */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> struct block_list { char *txt; int len; int num; }; static struct block_list *list; static int list_size; static int max_size; struct block_list *block_head; int read_block(char *buf, int buf_size, FILE *fin) { char *curr = buf, *const buf_end = buf + buf_size; while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) { if (*curr == '\n') /* empty line */ return curr - buf; curr += strlen(curr); } return -1; /* EOF or no space left in buf. */ } static int compare_txt(struct block_list *l1, struct block_list *l2) { return strcmp(l1->txt, l2->txt); } static int compare_num(struct block_list *l1, struct block_list *l2) { return l2->num - l1->num; } static void add_list(char *buf, int len) { if (list_size != 0 && len == list[list_size-1].len && memcmp(buf, list[list_size-1].txt, len) == 0) { list[list_size-1].num++; return; } if (list_size == max_size) { printf("max_size too small??\n"); exit(1); } list[list_size].txt = malloc(len+1); list[list_size].len = len; list[list_size].num = 1; memcpy(list[list_size].txt, buf, len); list[list_size].txt[len] = 0; list_size++; if (list_size % 1000 == 0) { printf("loaded %d\r", list_size); fflush(stdout); } } #define BUF_SIZE 1024 int main(int argc, char **argv) { FILE *fin, *fout; char buf[BUF_SIZE]; int ret, i, count; struct block_list *list2; struct stat st; fin = fopen(argv[1], "r"); fout = fopen(argv[2], "w"); if (!fin || !fout) { printf("Usage: ./program <input> <output>\n"); perror("open: "); exit(2); } fstat(fileno(fin), &st); max_size = st.st_size / 100; /* hack ... */ list = malloc(max_size * sizeof(*list)); for(;;) { ret = read_block(buf, BUF_SIZE, fin); if (ret < 0) break; add_list(buf, ret); } printf("loaded %d\n", list_size); printf("sorting ....\n"); qsort(list, list_size, sizeof(list[0]), compare_txt); list2 = malloc(sizeof(*list) * list_size); printf("culling\n"); for (i=count=0;i<list_size;i++) { if (count == 0 || strcmp(list2[count-1].txt, list[i].txt) != 0) { list2[count++] = list[i]; } else { list2[count-1].num += list[i].num; } } qsort(list2, count, sizeof(list[0]), compare_num); for (i=0;i<count;i++) { fprintf(fout, "%d times:\n%s\n", list2[i].num, list2[i].txt); } return 0; } include/linux/mm_types.h +7 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ #include <linux/spinlock.h> #include <linux/rbtree.h> #include <linux/rwsem.h> #include <linux/stacktrace.h> #include <linux/completion.h> #include <linux/cpumask.h> #include <linux/page-debug-flags.h> Loading Loading @@ -177,6 +178,12 @@ struct page { #ifdef LAST_NID_NOT_IN_PAGE_FLAGS int _last_nid; #endif #ifdef CONFIG_PAGE_OWNER int order; gfp_t gfp_mask; struct stack_trace trace; unsigned long trace_entries[8]; #endif } /* * The struct page can be forced to be double word aligned so that atomic ops Loading include/linux/stacktrace.h +3 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,8 @@ extern void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace); extern void print_stack_trace(struct stack_trace *trace, int spaces); extern int snprint_stack_trace(char *buf, int buf_len, struct stack_trace *trace, int spaces); #ifdef CONFIG_USER_STACKTRACE_SUPPORT extern void save_stack_trace_user(struct stack_trace *trace); Loading @@ -32,6 +34,7 @@ extern void save_stack_trace_user(struct stack_trace *trace); # define save_stack_trace_tsk(tsk, trace) do { } while (0) # define save_stack_trace_user(trace) do { } while (0) # define print_stack_trace(trace, spaces) do { } while (0) # define snprint_stack_trace(buf, len, trace, spaces) do { } while (0) #endif #endif kernel/stacktrace.c +23 −0 Original line number Diff line number Diff line Loading @@ -11,6 +11,29 @@ #include <linux/kallsyms.h> #include <linux/stacktrace.h> int snprint_stack_trace(char *buf, int buf_len, struct stack_trace *trace, int spaces) { int ret = 0; int i; if (WARN_ON(!trace->entries)) return 0; for (i = 0; i < trace->nr_entries; i++) { unsigned long ip = trace->entries[i]; int printed = snprintf(buf, buf_len, "%*c[<%p>] %pS\n", 1 + spaces, ' ', (void *) ip, (void *) ip); buf_len -= printed; ret += printed; buf += printed; } return ret; } EXPORT_SYMBOL_GPL(snprint_stack_trace); void print_stack_trace(struct stack_trace *trace, int spaces) { int i; Loading lib/Kconfig.debug +12 −0 Original line number Diff line number Diff line Loading @@ -99,6 +99,18 @@ config UNUSED_SYMBOLS you really need it, and what the merge plan to the mainline kernel for your module is. config PAGE_OWNER bool "Track page owner" depends on DEBUG_KERNEL && STACKTRACE_SUPPORT select DEBUG_FS select STACKTRACE help This keeps track of what call chain is the owner of a page, may help to find bare alloc_page(s) leaks. Eats a fair amount of memory. See Documentation/page_owner.c for user-space helper. If unsure, say N. config DEBUG_FS bool "Debug Filesystem" help Loading Loading
Documentation/page_owner.c 0 → 100644 +134 −0 Original line number Diff line number Diff line /* * User-space helper to sort the output of /sys/kernel/debug/page_owner * * Example use: * cat /sys/kernel/debug/page_owner > page_owner_full.txt * grep -v ^PFN page_owner_full.txt > page_owner.txt * ./sort page_owner.txt sorted_page_owner.txt */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> struct block_list { char *txt; int len; int num; }; static struct block_list *list; static int list_size; static int max_size; struct block_list *block_head; int read_block(char *buf, int buf_size, FILE *fin) { char *curr = buf, *const buf_end = buf + buf_size; while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) { if (*curr == '\n') /* empty line */ return curr - buf; curr += strlen(curr); } return -1; /* EOF or no space left in buf. */ } static int compare_txt(struct block_list *l1, struct block_list *l2) { return strcmp(l1->txt, l2->txt); } static int compare_num(struct block_list *l1, struct block_list *l2) { return l2->num - l1->num; } static void add_list(char *buf, int len) { if (list_size != 0 && len == list[list_size-1].len && memcmp(buf, list[list_size-1].txt, len) == 0) { list[list_size-1].num++; return; } if (list_size == max_size) { printf("max_size too small??\n"); exit(1); } list[list_size].txt = malloc(len+1); list[list_size].len = len; list[list_size].num = 1; memcpy(list[list_size].txt, buf, len); list[list_size].txt[len] = 0; list_size++; if (list_size % 1000 == 0) { printf("loaded %d\r", list_size); fflush(stdout); } } #define BUF_SIZE 1024 int main(int argc, char **argv) { FILE *fin, *fout; char buf[BUF_SIZE]; int ret, i, count; struct block_list *list2; struct stat st; fin = fopen(argv[1], "r"); fout = fopen(argv[2], "w"); if (!fin || !fout) { printf("Usage: ./program <input> <output>\n"); perror("open: "); exit(2); } fstat(fileno(fin), &st); max_size = st.st_size / 100; /* hack ... */ list = malloc(max_size * sizeof(*list)); for(;;) { ret = read_block(buf, BUF_SIZE, fin); if (ret < 0) break; add_list(buf, ret); } printf("loaded %d\n", list_size); printf("sorting ....\n"); qsort(list, list_size, sizeof(list[0]), compare_txt); list2 = malloc(sizeof(*list) * list_size); printf("culling\n"); for (i=count=0;i<list_size;i++) { if (count == 0 || strcmp(list2[count-1].txt, list[i].txt) != 0) { list2[count++] = list[i]; } else { list2[count-1].num += list[i].num; } } qsort(list2, count, sizeof(list[0]), compare_num); for (i=0;i<count;i++) { fprintf(fout, "%d times:\n%s\n", list2[i].num, list2[i].txt); } return 0; }
include/linux/mm_types.h +7 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ #include <linux/spinlock.h> #include <linux/rbtree.h> #include <linux/rwsem.h> #include <linux/stacktrace.h> #include <linux/completion.h> #include <linux/cpumask.h> #include <linux/page-debug-flags.h> Loading Loading @@ -177,6 +178,12 @@ struct page { #ifdef LAST_NID_NOT_IN_PAGE_FLAGS int _last_nid; #endif #ifdef CONFIG_PAGE_OWNER int order; gfp_t gfp_mask; struct stack_trace trace; unsigned long trace_entries[8]; #endif } /* * The struct page can be forced to be double word aligned so that atomic ops Loading
include/linux/stacktrace.h +3 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,8 @@ extern void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace); extern void print_stack_trace(struct stack_trace *trace, int spaces); extern int snprint_stack_trace(char *buf, int buf_len, struct stack_trace *trace, int spaces); #ifdef CONFIG_USER_STACKTRACE_SUPPORT extern void save_stack_trace_user(struct stack_trace *trace); Loading @@ -32,6 +34,7 @@ extern void save_stack_trace_user(struct stack_trace *trace); # define save_stack_trace_tsk(tsk, trace) do { } while (0) # define save_stack_trace_user(trace) do { } while (0) # define print_stack_trace(trace, spaces) do { } while (0) # define snprint_stack_trace(buf, len, trace, spaces) do { } while (0) #endif #endif
kernel/stacktrace.c +23 −0 Original line number Diff line number Diff line Loading @@ -11,6 +11,29 @@ #include <linux/kallsyms.h> #include <linux/stacktrace.h> int snprint_stack_trace(char *buf, int buf_len, struct stack_trace *trace, int spaces) { int ret = 0; int i; if (WARN_ON(!trace->entries)) return 0; for (i = 0; i < trace->nr_entries; i++) { unsigned long ip = trace->entries[i]; int printed = snprintf(buf, buf_len, "%*c[<%p>] %pS\n", 1 + spaces, ' ', (void *) ip, (void *) ip); buf_len -= printed; ret += printed; buf += printed; } return ret; } EXPORT_SYMBOL_GPL(snprint_stack_trace); void print_stack_trace(struct stack_trace *trace, int spaces) { int i; Loading
lib/Kconfig.debug +12 −0 Original line number Diff line number Diff line Loading @@ -99,6 +99,18 @@ config UNUSED_SYMBOLS you really need it, and what the merge plan to the mainline kernel for your module is. config PAGE_OWNER bool "Track page owner" depends on DEBUG_KERNEL && STACKTRACE_SUPPORT select DEBUG_FS select STACKTRACE help This keeps track of what call chain is the owner of a page, may help to find bare alloc_page(s) leaks. Eats a fair amount of memory. See Documentation/page_owner.c for user-space helper. If unsure, say N. config DEBUG_FS bool "Debug Filesystem" help Loading