Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759576AbZF2UIl (ORCPT ); Mon, 29 Jun 2009 16:08:41 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1752926AbZF2UIe (ORCPT ); Mon, 29 Jun 2009 16:08:34 -0400 Received: from mail.open.by ([193.232.92.17]:52456 "EHLO post.open.by" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752778AbZF2UId (ORCPT ); Mon, 29 Jun 2009 16:08:33 -0400 X-SpamTest-Envelope-From: sergey.senozhatsky@mail.by X-SpamTest-Group-ID: 00000000 X-SpamTest-Info: Profiles 8864 [Jun 29 2009] X-SpamTest-Info: helo_type=3 X-SpamTest-Info: {relay has no DNS name} X-SpamTest-Method: none X-SpamTest-Rate: 55 X-SpamTest-SPF: softfail X-SpamTest-Status: Not detected X-SpamTest-Status-Extended: not_detected X-SpamTest-Version: SMTP-Filter Version 3.0.0 [0284], KAS30/Release Date: Mon, 29 Jun 2009 23:10:14 +0300 From: Sergey Senozhatsky To: Catalin Marinas Cc: Pekka Enberg , "Paul E. McKenney" , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: kmemleak hexdump proposal Message-ID: <20090629201014.GA5414@localdomain.by> References: <20090628173632.GA3890@localdomain.by> <84144f020906290243u7a362465p6b1f566257fa3239@mail.gmail.com> <20090629101917.GA3093@localdomain.by> <1246270774.6364.9.camel@penberg-laptop> <1246271880.21450.13.camel@pc1117.cambridge.arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1246271880.21450.13.camel@pc1117.cambridge.arm.com> User-Agent: Mutt/1.5.20 (2009-06-14) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 7151 Lines: 189 Hello. This is actually draft. We'll discuss details during next merge window (or earlier). Thanks. --- hex dump prints not more than HEX_MAX_LINES lines by HEX_ROW_SIZE (16 or 32) bytes. ( min(object->size, HEX_MAX_LINES * HEX_ROW_SIZE) ). Example (HEX_ROW_SIZE 16): unreferenced object 0xf68b59b8 (size 32): comm "swapper", pid 1, jiffies 4294877610 hex dump (first 32 bytes): 70 6e 70 20 30 30 3a 30 31 00 5a 5a 5a 5a 5a 5a pnp 00:01.ZZZZZZ 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a a5 ZZZZZZZZZZZZZZZ. backtrace: [] kmemleak_alloc+0x11b/0x2b0 [] kmem_cache_alloc+0x111/0x1c0 [] reserve_range+0x3e/0x1b0 [] system_pnp_probe+0x94/0x140 [] pnp_device_probe+0x84/0x100 [] driver_probe_device+0x89/0x170 [] __driver_attach+0x99/0xa0 [] bus_for_each_dev+0x58/0x90 [] driver_attach+0x24/0x40 [] bus_add_driver+0xc4/0x290 [] driver_register+0x70/0x130 [] pnp_register_driver+0x26/0x40 [] pnp_system_init+0x1b/0x2e [] do_one_initcall+0x3f/0x1a0 [] kernel_init+0x13e/0x1a6 [] kernel_thread_helper+0x7/0x10 Example (HEX_ROW_SIZE 32): unreferenced object 0xf5e2e130 (size 2048): comm "modprobe", pid 2084, jiffies 4294880769 hex dump (first 64 bytes): 24 97 ff ff fc ff ff ff fc ff ff ff fc ff ff ff fc ff ff ff fc ff ff ff fc ff ff ff fc ff ff ff $............................... fc ff ff ff fc ff ff ff fc ff ff ff fc ff ff ff fc ff ff ff fc ff ff ff fc ff ff ff fc ff ff ff ................................ backtrace: [] kmemleak_alloc+0x11b/0x2b0 [] __kmalloc+0x16d/0x210 [] pcpu_mem_alloc+0x2d/0x80 [] pcpu_extend_area_map+0x62/0x100 [] pcpu_alloc+0x317/0x480 [] __alloc_percpu+0x17/0x30 [] alloc_disk_node+0x54/0x150 [] alloc_disk+0x1c/0x40 [] loop_alloc+0x6b/0x170 [loop] [] 0xfd61e092 [] do_one_initcall+0x3f/0x1a0 [] sys_init_module+0xdd/0x220 [] sysenter_do_call+0x12/0x22 [] 0xffffffff unreferenced object 0xf5d3cd00 (size 32): comm "consolechars", pid 2433, jiffies 4294894598 hex dump (first 32 bytes): 60 15 58 c1 00 00 00 00 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a a5 `.X.....ZZZZZZZZZZZZZZZZZZZZZZZ. backtrace: [] kmemleak_alloc+0x11b/0x2b0 [] kmem_cache_alloc+0x111/0x1c0 [] tty_ldisc_try_get+0x2b/0x130 [] tty_ldisc_get+0x37/0x70 [] tty_ldisc_reinit+0x34/0x70 [] tty_ldisc_release+0x35/0x60 [] tty_release_dev+0x33e/0x500 [] tty_release+0x20/0x40 [] __fput+0xed/0x200 [] fput+0x22/0x40 [] filp_close+0x49/0x90 [] put_files_struct+0xb5/0xe0 [] exit_files+0x45/0x60 [] do_exit+0x133/0x6c0 [] do_group_exit+0x45/0xa0 [] sys_exit_group+0x22/0x40 hexdump.c can print prefix: case DUMP_PREFIX_ADDRESS: f6998df0: 3c 06 00 00 00 00 00 00 78 69 00 00 .... <.......xi...................... case DUMP_PREFIX_OFFSET: 00000000: 3c 06 00 00 00 00 00 00 78 69 00 00 .... <.......xi...................... default: 3c 06 00 00 00 00 00 00 78 69 00 00 00 00 00 .... <.......xi...................... hex_dump_object(struct seq_file *seq, struct kmemleak_object *object) can be extended to accept prefix flag either. Though I don't think it's so important. So, it prints without any prefix. diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 5063873..65c5d74 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -160,6 +160,15 @@ struct kmemleak_object { /* flag set to not scan the object */ #define OBJECT_NO_SCAN (1 << 2) +/* number of bytes to print per line; must be 16 or 32 */ +#define HEX_ROW_SIZE 32 +/* number of bytes to print at a time (1, 2, 4, 8) */ +#define HEX_GROUP_SIZE 1 +/* include ASCII after the hex output */ +#define HEX_ASCII 1 +/* max number of lines to be printed */ +#define HEX_MAX_LINES 2 + /* the list of all allocated objects */ static LIST_HEAD(object_list); /* the list of gray-colored objects (see color_gray comment below) */ @@ -181,6 +190,8 @@ static atomic_t kmemleak_initialized = ATOMIC_INIT(0); static atomic_t kmemleak_early_log = ATOMIC_INIT(1); /* set if a fata kmemleak error has occurred */ static atomic_t kmemleak_error = ATOMIC_INIT(0); +/* set if hex dump should be printed */ +static atomic_t kmemleak_hex_dump = ATOMIC_INIT(1); /* minimum and maximum address that may be valid pointers */ static unsigned long min_addr = ULONG_MAX; @@ -258,6 +269,35 @@ static void kmemleak_disable(void); kmemleak_disable(); \ } while (0) + +/* + * Printing of the objects hex dump to the seq file. The number on lines + * to be printed is limited to HEX_MAX_LINES to prevent seq file spamming. + * The actual number of printed bytes depends on HEX_ROW_SIZE. + * It must be called with the object->lock held. + */ +static void hex_dump_object(struct seq_file *seq, + struct kmemleak_object *object) +{ + const u8 *ptr = (const u8 *)object->pointer; + /* Limit the number of lines to HEX_MAX_LINES. */ + int len = min(object->size, (size_t)(HEX_MAX_LINES * HEX_ROW_SIZE)); + int i, remaining = len; + unsigned char linebuf[200]; + + seq_printf(seq, " hex dump (first %d bytes):\n", len); + + for (i = 0; i < len; i += HEX_ROW_SIZE) { + int linelen = min(remaining, HEX_ROW_SIZE); + remaining -= HEX_ROW_SIZE; + hex_dump_to_buffer(ptr + i, linelen, HEX_ROW_SIZE, + HEX_GROUP_SIZE, linebuf, + sizeof(linebuf), HEX_ASCII); + + seq_printf(seq, " %s\n", linebuf); + } +} + /* * Object colors, encoded with count and min_count: * - white - orphan object, not enough references to it (count < min_count) @@ -303,6 +343,11 @@ static void print_unreferenced(struct seq_file *seq, object->pointer, object->size); seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu\n", object->comm, object->pid, object->jiffies); + + /* check whether hex dump should be printed */ + if (atomic_read(&kmemleak_hex_dump)) + hex_dump_object(seq, object); + seq_printf(seq, " backtrace:\n"); for (i = 0; i < object->trace_len; i++) { @@ -1269,6 +1314,10 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, start_scan_thread(); else if (strncmp(buf, "scan=off", 8) == 0) stop_scan_thread(); + else if (strncmp(buf, "hexdump=on", 10) == 0) + atomic_set(&kmemleak_hex_dump, 1); + else if (strncmp(buf, "hexdump=off", 11) == 0) + atomic_set(&kmemleak_hex_dump, 0); else if (strncmp(buf, "scan=", 5) == 0) { unsigned long secs; int err; Sergey -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/