x86, olpc: Speed up device tree creation during boot
authorAndres Salomon <dilinger@queued.net>
Mon, 29 Nov 2010 23:39:51 +0000 (23:39 +0000)
committerH. Peter Anvin <hpa@linux.intel.com>
Thu, 16 Dec 2010 01:11:40 +0000 (17:11 -0800)
Calling alloc_bootmem() for tiny chunks of memory over and over is really
slow; on an XO-1, it caused the time between when the kernel started
booting and when the display came alive (post-lxfb probe) to increase
to 44s.  This patch optimizes the prom_early_alloc function by
calling alloc_bootmem for 4k-sized blocks of memory, and handing out
chunks of that to callers.  With this patch, the time between kernel load
and display initialization decreased to 23s.  If there's a better way to
do this early in the boot process, please let me know.

(Note: increasing the chunk size to 16k didn't noticably affect boot time,
and wasted 9k.)

v4: clarify comment, requested by hpa
v3: fix wasted memory buglet found by Milton Miller, and style fix.
v2: reorder prom_early_alloc as suggested by Grant.

Signed-off-by: Andres Salomon <dilinger@queued.net>
LKML-Reference: <20101129153951.74202a84@queued.net>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
arch/x86/platform/olpc/olpc_dt.c

index 70546975a920386ec5a31752e7f3db90c0d249a4..dab87464753058b10b7b3094c734126dcc862066 100644 (file)
@@ -126,14 +126,32 @@ static unsigned int prom_early_allocated __initdata;
 
 void * __init prom_early_alloc(unsigned long size)
 {
+       static u8 *mem;
+       static size_t free_mem;
        void *res;
 
-       res = alloc_bootmem(size);
-       if (res)
-               memset(res, 0, size);
-
-       prom_early_allocated += size;
+       if (free_mem < size) {
+               const size_t chunk_size = max(PAGE_SIZE, size);
+
+               /*
+                * To mimimize the number of allocations, grab at least
+                * PAGE_SIZE of memory (that's an arbitrary choice that's
+                * fast enough on the platforms we care about while minimizing
+                * wasted bootmem) and hand off chunks of it to callers.
+                */
+               res = alloc_bootmem(chunk_size);
+               if (!res)
+                       return NULL;
+               prom_early_allocated += chunk_size;
+               memset(res, 0, chunk_size);
+               free_mem = chunk_size;
+               mem = res;
+       }
 
+       /* allocate from the local cache */
+       free_mem -= size;
+       res = mem;
+       mem += size;
        return res;
 }