virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Jeremy Fitzhardinge <jeremy@goop.org>
To: Andi Kleen <ak@suse.de>
Cc: virtualization@lists.osdl.org,
	Ivan Kokshaysky <ink@jurassic.park.msu.ru>,
	Russell King <rmk@arm.linux.org.uk>,
	Matt Mackall <mpm@selenic.com>, Ian Molton <spyro@f2s.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	lkml <linux-kernel@vger.kernel.org>,
	Tim Yamin <plasmaroo@gentoo.org>,
	Richard Henderson <rth@twiddle.net>
Subject: [PATCH 04/28] deflate stack usage in lib/inflate.c
Date: Sat, 14 Apr 2007 13:41:58 -0700	[thread overview]
Message-ID: <20070414204923.672434775@goop.org> (raw)
In-Reply-To: 20070414204154.871250608@goop.org

[-- Attachment #1: deflate-stack-usage-in-lib_inflate_c.patch --]
[-- Type: text/plain, Size: 8438 bytes --]

inflate_fixed and huft_build together use around 2.7k of stack.  When
using 4k stacks, I saw stack overflows from interrupts arriving while
unpacking the root initrd:

do_IRQ: stack overflow: 384
 [<c0106b64>] show_trace_log_lvl+0x1a/0x30
 [<c01075e6>] show_trace+0x12/0x14
 [<c010763f>] dump_stack+0x16/0x18
 [<c0107ca4>] do_IRQ+0x6d/0xd9
 [<c010202b>] xen_evtchn_do_upcall+0x6e/0xa2
 [<c0106781>] xen_hypervisor_callback+0x25/0x2c
 [<c010116c>] xen_restore_fl+0x27/0x29
 [<c0330f63>] _spin_unlock_irqrestore+0x4a/0x50
 [<c0117aab>] change_page_attr+0x577/0x584
 [<c0117b45>] kernel_map_pages+0x8d/0xb4
 [<c016a314>] cache_alloc_refill+0x53f/0x632
 [<c016a6c2>] __kmalloc+0xc1/0x10d
 [<c0463d34>] malloc+0x10/0x12
 [<c04641c1>] huft_build+0x2a7/0x5fa
 [<c04645a5>] inflate_fixed+0x91/0x136
 [<c04657e2>] unpack_to_rootfs+0x5f2/0x8c1
 [<c0465acf>] populate_rootfs+0x1e/0xe4

(This was under Xen, but there's no reason it couldn't happen on bare
  hardware.)

This patch mallocs the local variables, thereby reducing the stack
usage to sane levels.

Also, up the heap size for the kernel decompressor to deal with the
extra allocation.

Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Cc: Tim Yamin <plasmaroo@gentoo.org>
Cc: Andi Kleen <ak@suse.de>
Cc: Matt Mackall <mpm@selenic.com>
Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Russell King <rmk@arm.linux.org.uk>
Cc: Ian Molton <spyro@f2s.com>

---
 arch/alpha/boot/misc.c             |    2 -
 arch/arm/boot/compressed/misc.c    |    2 -
 arch/arm26/boot/compressed/misc.c  |    2 -
 arch/i386/boot/compressed/misc.c   |    2 -
 arch/x86_64/boot/compressed/misc.c |    2 -
 lib/inflate.c                      |   66 ++++++++++++++++++++++++++----------
 6 files changed, 54 insertions(+), 22 deletions(-)

===================================================================
--- a/arch/alpha/boot/misc.c
+++ b/arch/alpha/boot/misc.c
@@ -98,7 +98,7 @@ static ulg free_mem_ptr;
 static ulg free_mem_ptr;
 static ulg free_mem_ptr_end;
 
-#define HEAP_SIZE 0x2000
+#define HEAP_SIZE 0x3000
 
 #include "../../../lib/inflate.c"
 
===================================================================
--- a/arch/arm/boot/compressed/misc.c
+++ b/arch/arm/boot/compressed/misc.c
@@ -239,7 +239,7 @@ static ulg free_mem_ptr;
 static ulg free_mem_ptr;
 static ulg free_mem_ptr_end;
 
-#define HEAP_SIZE 0x2000
+#define HEAP_SIZE 0x3000
 
 #include "../../../../lib/inflate.c"
 
===================================================================
--- a/arch/arm26/boot/compressed/misc.c
+++ b/arch/arm26/boot/compressed/misc.c
@@ -182,7 +182,7 @@ static ulg free_mem_ptr;
 static ulg free_mem_ptr;
 static ulg free_mem_ptr_end;
 
-#define HEAP_SIZE 0x2000
+#define HEAP_SIZE 0x3000
 
 #include "../../../../lib/inflate.c"
 
===================================================================
--- a/arch/i386/boot/compressed/misc.c
+++ b/arch/i386/boot/compressed/misc.c
@@ -189,7 +189,7 @@ static unsigned long free_mem_ptr;
 static unsigned long free_mem_ptr;
 static unsigned long free_mem_end_ptr;
 
-#define HEAP_SIZE             0x3000
+#define HEAP_SIZE             0x4000
 
 static char *vidmem = (char *)0xb8000;
 static int vidport;
===================================================================
--- a/arch/x86_64/boot/compressed/misc.c
+++ b/arch/x86_64/boot/compressed/misc.c
@@ -189,7 +189,7 @@ static long free_mem_ptr;
 static long free_mem_ptr;
 static long free_mem_end_ptr;
 
-#define HEAP_SIZE             0x6000
+#define HEAP_SIZE             0x7000
 
 static char *vidmem = (char *)0xb8000;
 static int vidport;
===================================================================
--- a/lib/inflate.c
+++ b/lib/inflate.c
@@ -292,7 +292,6 @@ STATIC int INIT huft_build(
    oversubscribed set of lengths), and three if not enough memory. */
 {
   unsigned a;                   /* counter for codes of length k */
-  unsigned c[BMAX+1];           /* bit length count table */
   unsigned f;                   /* i repeats in table every f entries */
   int g;                        /* maximum code length */
   int h;                        /* table level */
@@ -303,18 +302,33 @@ STATIC int INIT huft_build(
   register unsigned *p;         /* pointer into c[], b[], or v[] */
   register struct huft *q;      /* points to current table */
   struct huft r;                /* table entry for structure assignment */
-  struct huft *u[BMAX];         /* table stack */
-  unsigned v[N_MAX];            /* values in order of bit length */
   register int w;               /* bits before this table == (l * h) */
-  unsigned x[BMAX+1];           /* bit offsets, then code stack */
   unsigned *xp;                 /* pointer into x */
   int y;                        /* number of dummy codes added */
   unsigned z;                   /* number of entries in current table */
+  struct {
+    unsigned c[BMAX+1];           /* bit length count table */
+    struct huft *u[BMAX];         /* table stack */
+    unsigned v[N_MAX];            /* values in order of bit length */
+    unsigned x[BMAX+1];           /* bit offsets, then code stack */
+  } *stk;
+  unsigned *c, *v, *x;
+  struct huft **u;
+  int ret;
 
 DEBG("huft1 ");
 
+  stk = malloc(sizeof(*stk));
+  if (stk == NULL)
+    return 3;			/* out of memory */
+
+  c = stk->c;
+  v = stk->v;
+  x = stk->x;
+  u = stk->u;
+
   /* Generate counts for each bit length */
-  memzero(c, sizeof(c));
+  memzero(stk->c, sizeof(stk->c));
   p = b;  i = n;
   do {
     Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"), 
@@ -326,7 +340,8 @@ DEBG("huft1 ");
   {
     *t = (struct huft *)NULL;
     *m = 0;
-    return 2;
+    ret = 2;
+    goto out;
   }
 
 DEBG("huft2 ");
@@ -351,10 +366,14 @@ DEBG("huft3 ");
 
   /* Adjust last length count to fill out codes, if needed */
   for (y = 1 << j; j < i; j++, y <<= 1)
-    if ((y -= c[j]) < 0)
-      return 2;                 /* bad input: more codes than bits */
-  if ((y -= c[i]) < 0)
-    return 2;
+    if ((y -= c[j]) < 0) {
+      ret = 2;                 /* bad input: more codes than bits */
+      goto out;
+    }
+  if ((y -= c[i]) < 0) {
+    ret = 2;
+    goto out;
+  }
   c[i] += y;
 
 DEBG("huft4 ");
@@ -428,7 +447,8 @@ DEBG1("3 ");
         {
           if (h)
             huft_free(u[0]);
-          return 3;             /* not enough memory */
+          ret = 3;             /* not enough memory */
+	  goto out;
         }
 DEBG1("4 ");
         hufts += z + 1;         /* track memory usage */
@@ -492,7 +512,11 @@ DEBG("huft7 ");
 DEBG("huft7 ");
 
   /* Return true (1) if we were given an incomplete table */
-  return y != 0 && g != 1;
+  ret = y != 0 && g != 1;
+
+  out:
+  free(stk);
+  return ret;
 }
 
 
@@ -705,9 +729,13 @@ STATIC int noinline INIT inflate_fixed(v
   struct huft *td;      /* distance code table */
   int bl;               /* lookup bits for tl */
   int bd;               /* lookup bits for td */
-  unsigned l[288];      /* length list for huft_build */
+  unsigned *l;          /* length list for huft_build */
 
 DEBG("<fix");
+
+  l = malloc(sizeof(*l) * 288);
+  if (l == NULL)
+    return 3;			/* out of memory */
 
   /* set up literal table */
   for (i = 0; i < 144; i++)
@@ -719,9 +747,10 @@ DEBG("<fix");
   for (; i < 288; i++)          /* make a complete, but wrong code set */
     l[i] = 8;
   bl = 7;
-  if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
+  if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
+    free(l);
     return i;
-
+  }
 
   /* set up distance table */
   for (i = 0; i < 30; i++)      /* make an incomplete code set */
@@ -730,6 +759,7 @@ DEBG("<fix");
   if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
   {
     huft_free(tl);
+    free(l);
 
     DEBG(">");
     return i;
@@ -737,11 +767,13 @@ DEBG("<fix");
 
 
   /* decompress until an end-of-block code */
-  if (inflate_codes(tl, td, bl, bd))
+  if (inflate_codes(tl, td, bl, bd)) {
+    free(l);
     return 1;
-
+  }
 
   /* free the decoding tables, return */
+  free(l);
   huft_free(tl);
   huft_free(td);
   return 0;

-- 

  parent reply	other threads:[~2007-04-14 20:41 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-14 20:41 [PATCH 00/28] Updates for firstfloor paravirt-ops patches Jeremy Fitzhardinge
2007-04-14 20:41 ` [PATCH 01/28] revert account-for-module-percpu-space-separately-from-kernel-percpu Jeremy Fitzhardinge
2007-04-14 20:41 ` [PATCH 02/28] Account for module percpu space separately from kernel percpu Jeremy Fitzhardinge
2007-04-14 20:41 ` [PATCH 03/28] fix allow-percpu-variables-to-be-page-aligned.patch Jeremy Fitzhardinge
2007-04-14 20:41 ` Jeremy Fitzhardinge [this message]
2007-04-14 20:41 ` [PATCH 05/28] Page-align the GDT Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 06/28] Convert PDA into the percpu section Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 07/28] cleanups to help using per-cpu variables from asm Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 08/28] Define per_cpu_offset Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 09/28] Fix UP gdt bugs Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 10/28] i386: map enough initial memory to create lowmem mappings Jeremy Fitzhardinge
2007-04-14 22:04   ` H. Peter Anvin
2007-04-15  9:46     ` Jan Engelhardt
2007-04-15 10:17       ` Andreas Schwab
2007-04-19 20:47   ` Chuck Ebbert
2007-04-19 20:50     ` Andi Kleen
2007-04-19 20:55       ` H. Peter Anvin
2007-04-19 21:04         ` Andi Kleen
2007-04-19 21:11           ` H. Peter Anvin
2007-04-19 21:22             ` Chuck Ebbert
2007-04-19 21:35               ` Jeremy Fitzhardinge
2007-04-23  9:12                 ` Eric W. Biederman
2007-04-23 16:01                   ` H. Peter Anvin
2007-04-23 16:34                   ` Jeremy Fitzhardinge
2007-04-23 16:42                     ` H. Peter Anvin
2007-04-23 17:02                       ` Jeremy Fitzhardinge
2007-04-23 17:22                         ` H. Peter Anvin
2007-04-23 18:00                           ` Eric W. Biederman
2007-04-23 17:31                         ` Eric W. Biederman
2007-04-23 17:45                           ` H. Peter Anvin
2007-04-23 17:52                             ` Eric W. Biederman
2007-04-23 17:54                             ` Andi Kleen
2007-04-23 17:21                       ` Eric W. Biederman
2007-04-23 18:06                         ` Jeremy Fitzhardinge
2007-04-23 18:54                           ` Eric W. Biederman
2007-04-23 19:10                             ` Jeremy Fitzhardinge
2007-04-23 19:14                               ` H. Peter Anvin
2007-04-23 19:21                                 ` Jeremy Fitzhardinge
2007-04-23 19:39                                   ` Eric W. Biederman
2007-04-23 20:41                                     ` H. Peter Anvin
2007-04-25 20:54                 ` Eric W. Biederman
2007-04-25 21:31                   ` Jeremy Fitzhardinge
2007-04-25 22:00                     ` Eric W. Biederman
2007-04-25 22:06                       ` Jeremy Fitzhardinge
2007-04-25 22:18                         ` Eric W. Biederman
2007-04-25 22:52                           ` Jeremy Fitzhardinge
2007-04-25 23:33                             ` Eric W. Biederman
2007-04-25 23:41                               ` Jeremy Fitzhardinge
2007-04-26  0:33                                 ` Chris Wright
2007-04-26  0:55                                   ` Jeremy Fitzhardinge
2007-04-29 16:44                         ` Eric W. Biederman
2007-04-29 16:55                           ` Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 11/28] x86: incremental update for i386 and x86-64 check_bugs Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 12/28] i386: now its ok to use identify_boot_cpu Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 13/28] paravirt: flush lazy mmu updates on kunmap_atomic Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 14/28] fix paravirt-documentation Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 15/28] In compat mode, the return value here was uninitialized Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 16/28] kRemove a warning about unused variable in !CONFIG_ACPI compilation Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 17/28] x86: cleanup arch/i386/kernel/cpu/mcheck/p4.c Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 18/28] Copying of the pgd range must happen under the pgd_lock Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 19/28] Dont implement native_kmap_atomic_pte for !HIGHPTE Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 20/28] Now that the VDSO can be relocated, we can support it in VMI configurations Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 21/28] Implement vmi_kmap_atomic_pte Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 22/28] Convert VMI timer to use clock events Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 23/28] Fix BusLogic to stop using check_region Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 24/28] paravirt: drop unused ptep_get_and_clear Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 25/28] From: Jeremy Fitzhardinge <jeremy@goop.org> Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 26/28] From: Andrew Morton <akpm@linux-foundation.org> Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 27/28] paravirt: little compile fixes for vmi.c Jeremy Fitzhardinge
2007-04-14 20:42 ` [PATCH 28/28] Add a sched_clock paravirt_op Jeremy Fitzhardinge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070414204923.672434775@goop.org \
    --to=jeremy@goop.org \
    --cc=ak@suse.de \
    --cc=akpm@linux-foundation.org \
    --cc=ink@jurassic.park.msu.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpm@selenic.com \
    --cc=plasmaroo@gentoo.org \
    --cc=rmk@arm.linux.org.uk \
    --cc=rth@twiddle.net \
    --cc=spyro@f2s.com \
    --cc=virtualization@lists.osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).