Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754669AbXIOLfn (ORCPT ); Sat, 15 Sep 2007 07:35:43 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1751931AbXIOLfg (ORCPT ); Sat, 15 Sep 2007 07:35:36 -0400 Received: from ug-out-1314.google.com ([66.249.92.172]:29635 "EHLO ug-out-1314.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751730AbXIOLff (ORCPT ); Sat, 15 Sep 2007 07:35:35 -0400 DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:subject:from:reply-to:to:content-type:date:message-id:mime-version:x-mailer:content-transfer-encoding; b=k08DjlblblWXLSVC6ikP8BT8nyLBX70Wif14hlmw4lnRcR4KVKTs9didh57o7EamYgNEpBjCl4EHNeUTz6OldvXPhSOCi17IXzELSv3uVbyCsmvA2oUfAAAR5J1SHogLKby3RqcER2VqmANTf9rcZ/eZAMt9bjHO8sn8TyVgrS0= Subject: [Minor patch] Reduce __print_symbol/sprint_symbol stack usage. From: Gilboa Davara Reply-To: gilboad@gmail.com To: linux-kernel@vger.kernel.org Content-Type: text/plain Date: Sat, 15 Sep 2007 14:35:29 +0300 Message-Id: <1189856129.18191.11.camel@gilboa-home-dev.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.10.3 (2.10.3-4.fc7) Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3863 Lines: 121 Hello all, In a small exchange in fedora-kernel-list [1] Eric Sandeen has pointed out a possible stack overflow... when CONFIG_DEBUG_STACKOVERFLOW is enabled. (Though not limited to it) Code path is simple: do_IRQ detects a a near stack overflow condition and calls show_trace_log_lvl which, down the line uses __print_symbol and sprint_symbol to print the call stack. However, both __print_symbol + sprint_symbol are eating no-less then 128+223 bytes on static char arrays, which, given the fact that this code path is actually generated by low stack warning (< 512 bytes), might turn a minor (?) problem (low stack) into a full blown crash. The patch itself is fairly simple and non-intrusive. [2] Both functions allocate memory for their buffers - falling back to minimal address display if memory allocation fails. P.S. Can anyone please point me to the maintainer of kernel/syms? (I rather not spam world + dog for such a minor patch) -- Gilboa Davara [1] http://www.mail-archive.com/fedora-kernel-list@redhat.com/msg00640.html [2]. In theory, there's a second option: pre-allocating memory on a per_cpu basis, however: A. dump_trace/stack are usually called when something bad has happened - reducing the need for performance optimizations. B. per_cpu allocation will also require local_irq_disable/enable as both functions are being called from multiple contexts. Too much hassle. --- linux-2.6/kernel/kallsyms.orig 2007-09-15 11:46:54.000000000 +0300 +++ linux-2.6/kernel/kallsyms.c 2007-09-15 14:25:21.000000000 +0300 @@ -309,30 +309,62 @@ int lookup_symbol_attrs(unsigned long ad /* Look up a kernel symbol and return it in a text buffer. */ int sprint_symbol(char *buffer, unsigned long address) { - char *modname; - const char *name; unsigned long offset, size; - char namebuf[KSYM_NAME_LEN]; + const char *name = NULL; + char *namebuf = NULL; + char *modname; + int ret; + + + /* Static buffer allocation. + Required in-order to reduce stack footprint on + do_IRQ/4KSTACK/i386 */ + namebuf = kmalloc(KSYM_NAME_LEN, GFP_ATOMIC); + if (namebuf) + name = kallsyms_lookup(address, &size, &offset, + &modname, namebuf); - name = kallsyms_lookup(address, &size, &offset, &modname, namebuf); if (!name) - return sprintf(buffer, "0x%lx", address); + ret = sprintf(buffer, "0x%lx", address); + else { + if (modname) + ret = sprintf(buffer, "%s+%#lx/%#lx [%s]", + name, offset, size, modname); + else + ret = sprintf(buffer, "%s+%#lx/%#lx", + name, offset, size); + } - if (modname) - return sprintf(buffer, "%s+%#lx/%#lx [%s]", name, offset, - size, modname); - else - return sprintf(buffer, "%s+%#lx/%#lx", name, offset, size); + if (namebuf) + kfree(namebuf); + + return ret; } /* Look up a kernel symbol and print it to the kernel messages. */ void __print_symbol(const char *fmt, unsigned long address) { - char buffer[KSYM_SYMBOL_LEN]; + char *buffer = NULL; - sprint_symbol(buffer, address); - printk(fmt, buffer); + /* Static buffer allocation. + Required in-order to reduce stack footprint on + do_IRQ/4KSTACK/i386 */ + buffer = kmalloc(KSYM_SYMBOL_LEN, GFP_ATOMIC); + if (buffer) { + sprint_symbol(buffer, address); + printk(fmt, buffer); + kfree(buffer); + } else { + /* Address + '0x' + NULL. */ + char sbuffer[(BITS_PER_LONG / 4) + 3]; + + /* Fall-back mode. + Memory allocation failed. + Convert the address to string and display it. */ + sprintf(sbuffer, "0x%lx", address); + printk(fmt, sbuffer); + } } /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ - 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/