LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
* PATCH improve worst case module load times
@ 2007-06-12  1:00 Brian Behlendorf
  0 siblings, 0 replies; 2+ messages in thread
From: Brian Behlendorf @ 2007-06-12  1:00 UTC (permalink / raw)
  To: linuxppc-dev

[-- Attachment #1: Type: text/plain, Size: 795 bytes --]


Recently I ran in to an apparently well known ppc/ppc64 issue regarding slow 
module load times which I see was posted to lkml back on '05.

   http://lkml.org/lkml/2005/9/28/180

  In my environment I'm forced to live with a few very large kernel modules 
and frequent reboots so this was become a bit of an issue.  I took a look at 
the root cause and put together a patch to address the issue.  The proposed 
patch resolves the problem by removing the order(n^2) algorithm and replaces 
it with an order(n) chained hash.

  It works well for me but I'm not a powerpc guru so I'd appreciate a review 
of the patch.  I thought about posting it to lkml but after I learned there 
was a ppc dev list this seemed like the right place to post.
  
-- 
Thanks,
Brian Behlendorf
behlendorf1@llnl.gov

[-- Attachment #2: powerpc-reloc.diff --]
[-- Type: text/x-diff, Size: 11546 bytes --]

Author: Brian Behlendorf <behlendorf1@llnl.gov>, LLNL
Date:   Mon Jun 11 2007

For ppc and ppc64 replace the O(N^2) algorithm used to calculate unique 
relocations with a O(N) chained hash.  The only significant downside to 
the new algorithm is that is requires at least one memory allocation.
Allocation size is limited to a single page and the kernel is given every
opportunity to satisfy the request.  In the unlikely event of a memory 
allocation failure the module will fail to load and -ENOMEM will be 
returned up the stack.  

The performance improvement on my embedded ppc dev system was dramatic.  
Without the patch it took 35 seconds to load the desired module stack
which contained a few worst case modules with a large number of 
relocatable symbols.  With the patch the time dropped to less than 1 
second.

While I didn't do any extensive testing the small module case may be
slightly impacted due to the addition of the page alloc.  If this is
a concern the patch could be reworked to choose either the O(N^2) or
O(N) algorithm based on the number of symbols.  This would give us
the best of both worlds.
    
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>

--- arch/powerpc/kernel/module_64.c.orig	2007-06-11 11:13:23.877345417 -0700
+++ arch/powerpc/kernel/module_64.c	2007-06-11 14:22:23.287707424 -0700
@@ -77,30 +77,15 @@
 	0x4e, 0x80, 0x04, 0x20  /* bctr */
 } };
 
-/* Count how many different 24-bit relocations (different symbol,
-   different addend) */
-static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
-{
-	unsigned int i, j, ret = 0;
-
-	/* FIXME: Only count external ones --RR */
-	/* Sure, this is order(n^2), but it's usually short, and not
-           time critical */
-	for (i = 0; i < num; i++) {
-		/* Only count 24-bit relocs, others don't need stubs */
-		if (ELF64_R_TYPE(rela[i].r_info) != R_PPC_REL24)
-			continue;
-		for (j = 0; j < i; j++) {
-			/* If this addend appeared before, it's
-                           already been counted */
-			if (rela[i].r_info == rela[j].r_info
-			    && rela[i].r_addend == rela[j].r_addend)
-				break;
-		}
-		if (j == i) ret++;
-	}
-	return ret;
-}
+#define HASH_RELOC_NEXT          (PAGE_SIZE / sizeof(Elf64_Rela *) - 1)
+
+struct hash_reloc {
+	/* Page full of Elf64_Rela pointers; increasing the hash depth is
+	 * accomplished by chaining pages of the ptr element in the page. */
+	Elf64_Rela **r_page;
+	int r_depth;
+	int r_duplicate;
+};
 
 void *module_alloc(unsigned long size)
 {
@@ -118,12 +103,112 @@
            table entries. */
 }
 
+/* Hash function for unique relocations */
+static unsigned hash_reloc_key(const Elf64_Rela *r)
+{
+	return ((ELF64_R_SYM(r->r_info) + r->r_addend) % HASH_RELOC_NEXT);
+}
+
+/* Hash insert can return -errno or number of inserted elements,
+ * duplicate elements will not be inserted in the hash and return 0. */
+static long hash_reloc_insert(struct hash_reloc *hash,
+			      Elf64_Rela *r1, unsigned key)
+{
+	Elf64_Rela **p1 = hash->r_page, **p2 = NULL;
+	unsigned i = 0;
+
+	while (p1 && p1[key]) {
+
+		/* Check for duplicates */
+		if ((r1->r_info == p1[key]->r_info) &&
+		    (r1->r_addend == p1[key]->r_addend)) {
+			hash->r_duplicate++;
+			return 0;
+		}
+
+		p2 = p1;
+		p1 = (Elf64_Rela **)p2[HASH_RELOC_NEXT];
+		i++;
+	}
+
+	if (i >= hash->r_depth) {
+		p1 = (Elf64_Rela **)get_zeroed_page(GFP_KERNEL);
+		if (!p1) {
+			printk("Unable to allocate memory for module\n");
+			return -ENOMEM;
+		}
+
+		hash->r_depth++;
+		p2[HASH_RELOC_NEXT] = (Elf64_Rela *)p1;
+	}
+
+	p1[key] = r1;
+	return 1;
+}
+
+static long hash_reloc_init(struct hash_reloc *hash)
+{
+	if (!(hash->r_page = (Elf64_Rela **)get_zeroed_page(GFP_KERNEL))) {
+		printk("Unable to allocate memory for module\n");
+		return -ENOMEM;
+	}
+
+	hash->r_depth = 1;
+	hash->r_duplicate = 0;
+	return 0;
+}
+
+static void hash_reloc_cleanup(struct hash_reloc *hash)
+{
+	Elf64_Rela **p1, **p2;
+
+	p1 = hash->r_page;
+	while (p1) {
+		p2 = (Elf64_Rela **)p1[HASH_RELOC_NEXT];
+		free_page((unsigned long)p1);
+		p1 = p2;
+	}
+}
+
+/* Count how many unique 24-bit relocations (different symbol and addend) */
+static long count_relocs(const Elf64_Rela *rela, unsigned int num)
+{
+	struct hash_reloc hash;
+	Elf64_Rela *r;
+	long rc, ret = 0;
+	unsigned i, key;
+
+	hash_reloc_init(&hash);
+
+	/* Use a chained hash with a uniform key for an order(N) algorithm */
+	for (i = 0; i < num; i++) {
+		r = (Elf64_Rela *)&rela[i];
+		key = hash_reloc_key(r);
+
+		/* Only count 24-bit relocs, others don't need stubs */
+		if (ELF64_R_TYPE(r->r_info) != R_PPC_REL24)
+			continue;
+
+		if ((rc = hash_reloc_insert(&hash, r, key)) < 0) {
+			ret = rc;
+			break;
+		}
+
+		ret += rc;
+	}
+
+	DEBUGP("Symbols %d, duplicates %d, depth %d\n",
+	       num, hash.r_duplicate, hash.r_depth);
+	hash_reloc_cleanup(&hash);
+	return ret;
+}
+
 /* Get size of potential trampolines required. */
-static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
-				    const Elf64_Shdr *sechdrs)
+static long get_stubs_size(const Elf64_Ehdr *hdr,
+			   const Elf64_Shdr *sechdrs)
 {
 	/* One extra reloc so it's always 0-funcaddr terminated */
-	unsigned long relocs = 1;
+	long rc, ret = sizeof(struct ppc64_stub_entry);
 	unsigned i;
 
 	/* Every relocated section... */
@@ -133,14 +218,17 @@
 			DEBUGP("Ptr: %p.  Number: %lu\n",
 			       (void *)sechdrs[i].sh_addr,
 			       sechdrs[i].sh_size / sizeof(Elf64_Rela));
-			relocs += count_relocs((void *)sechdrs[i].sh_addr,
-					       sechdrs[i].sh_size
-					       / sizeof(Elf64_Rela));
+			rc = count_relocs((void *)sechdrs[i].sh_addr,
+					  sechdrs[i].sh_size /
+					  sizeof(Elf64_Rela));
+			if (rc < 0)
+				return rc;
+
+			ret += (rc * sizeof(struct ppc64_stub_entry));
 		}
 	}
 
-	DEBUGP("Looks like a total of %lu stubs, max\n", relocs);
-	return relocs * sizeof(struct ppc64_stub_entry);
+	return ret;
 }
 
 static void dedotify_versions(struct modversion_info *vers,
@@ -172,7 +260,8 @@
 			      char *secstrings,
 			      struct module *me)
 {
-	unsigned int i;
+	unsigned i;
+	long rc;
 
 	/* Find .toc and .stubs sections, symtab and strtab */
 	for (i = 1; i < hdr->e_shnum; i++) {
@@ -209,7 +298,10 @@
 		me->arch.toc_section = me->arch.stubs_section;
 
 	/* Override the stubs size */
-	sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
+	if ((rc = get_stubs_size(hdr, sechdrs)) < 0)
+		return rc;
+
+	sechdrs[me->arch.stubs_section].sh_size = rc;
 	return 0;
 }
 
--- arch/powerpc/kernel/module_32.c.orig	2007-06-11 13:24:52.440788813 -0700
+++ arch/powerpc/kernel/module_32.c	2007-06-11 14:13:07.998973232 -0700
@@ -35,6 +35,16 @@
 
 LIST_HEAD(module_bug_list);
 
+#define HASH_RELOC_NEXT          (PAGE_SIZE / sizeof(Elf32_Rela *) - 1)
+
+struct hash_reloc {
+	/* Page full of Elf32_Rela pointers; increasing the hash depth is
+	 * accomplished by chaining pages of the ptr element in the page. */
+	Elf32_Rela **r_page;
+	int r_depth;
+	int r_duplicate;
+};
+
 void *module_alloc(unsigned long size)
 {
 	if (size == 0)
@@ -50,36 +60,109 @@
            table entries. */
 }
 
-/* Count how many different relocations (different symbol, different
-   addend) */
-static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
+/* Hash function for unique relocations */
+static unsigned hash_reloc_key(const Elf32_Rela *r)
+{
+	return ((ELF32_R_SYM(r->r_info) + r->r_addend) % HASH_RELOC_NEXT);
+}
+
+/* Hash insert can return -errno or number of inserted elements,
+ * duplicate elements will not be inserted in the hash and return 0. */
+static long hash_reloc_insert(struct hash_reloc *hash,
+			      Elf32_Rela *r1, unsigned key)
+{
+	Elf32_Rela **p1 = hash->r_page, **p2 = NULL;
+	unsigned i = 0;
+
+	while (p1 && p1[key]) {
+
+		/* Check for duplicates */
+		if (ELF32_R_SYM(r1->r_info) == ELF32_R_SYM(p1[key]->r_info) &&
+		    r1->r_addend == p1[key]->r_addend) {
+			hash->r_duplicate++;
+			return 0;
+		}
+
+		p2 = p1;
+		p1 = (Elf32_Rela **)p2[HASH_RELOC_NEXT];
+		i++;
+	}
+
+	if (i >= hash->r_depth) {
+		p1 = (Elf32_Rela **)get_zeroed_page(GFP_KERNEL);
+		if (!p1) {
+			printk("Unable to allocate memory for module\n");
+			return -ENOMEM;
+		}
+
+		hash->r_depth++;
+		p2[HASH_RELOC_NEXT] = (Elf32_Rela *)p1;
+	}
+
+	p1[key] = r1;
+	return 1;
+}
+
+static long hash_reloc_init(struct hash_reloc *hash)
 {
-	unsigned int i, j, ret = 0;
+	if (!(hash->r_page = (Elf32_Rela **)get_zeroed_page(GFP_KERNEL))) {
+		printk("Unable to allocate memory for module\n");
+		return -ENOMEM;
+	}
 
-	/* Sure, this is order(n^2), but it's usually short, and not
-           time critical */
+	hash->r_depth = 1;
+	hash->r_duplicate = 0;
+	return 0;
+}
+
+static void hash_reloc_cleanup(struct hash_reloc *hash)
+{
+	Elf32_Rela **p1, **p2;
+
+	p1 = hash->r_page;
+	while (p1) {
+		p2 = (Elf32_Rela **)p1[HASH_RELOC_NEXT];
+		free_page((unsigned long)p1);
+		p1 = p2;
+	}
+}
+
+/* Count how many unique relocations (different symbol and addend) */
+static long count_relocs(const Elf32_Rela *rela, unsigned int num)
+{
+	struct hash_reloc hash;
+	Elf32_Rela *r;
+	long rc, ret = 0;
+	unsigned i, key;
+
+	hash_reloc_init(&hash);
+
+	/* Use a chained hash with a uniform key for an order(N) algorithm */
 	for (i = 0; i < num; i++) {
-		for (j = 0; j < i; j++) {
-			/* If this addend appeared before, it's
-                           already been counted */
-			if (ELF32_R_SYM(rela[i].r_info)
-			    == ELF32_R_SYM(rela[j].r_info)
-			    && rela[i].r_addend == rela[j].r_addend)
-				break;
+		r = (Elf32_Rela *)&rela[i];
+		key = hash_reloc_key(r);
+		if ((rc = hash_reloc_insert(&hash, r, key)) < 0) {
+			ret = rc;
+			break;
 		}
-		if (j == i) ret++;
+
+		ret += rc;
 	}
+
+	DEBUGP("Symbols %d, duplicates %d, depth %d\n",
+	       num, hash.r_duplicate, hash.r_depth);
+	hash_reloc_cleanup(&hash);
 	return ret;
 }
 
 /* Get the potential trampolines size required of the init and
    non-init sections */
-static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
+static long get_plt_size(const Elf32_Ehdr *hdr,
 				  const Elf32_Shdr *sechdrs,
 				  const char *secstrings,
 				  int is_init)
 {
-	unsigned long ret = 0;
+	long rc, ret = 0;
 	unsigned i;
 
 	/* Everything marked ALLOC (this includes the exported
@@ -100,11 +183,13 @@
 			DEBUGP("Ptr: %p.  Number: %u\n",
 			       (void *)hdr + sechdrs[i].sh_offset,
 			       sechdrs[i].sh_size / sizeof(Elf32_Rela));
-			ret += count_relocs((void *)hdr
-					     + sechdrs[i].sh_offset,
-					     sechdrs[i].sh_size
-					     / sizeof(Elf32_Rela))
-				* sizeof(struct ppc_plt_entry);
+			rc = count_relocs((void *)hdr + sechdrs[i].sh_offset,
+			                  sechdrs[i].sh_size /
+					  sizeof(Elf32_Rela));
+			if (rc < 0)
+				return rc;
+
+			ret += (rc * sizeof(struct ppc_plt_entry));
 		}
 	}
 
@@ -116,7 +201,8 @@
 			      char *secstrings,
 			      struct module *me)
 {
-	unsigned int i;
+	unsigned i;
+	long rc;
 
 	/* Find .plt and .init.plt sections */
 	for (i = 0; i < hdr->e_shnum; i++) {
@@ -131,10 +217,15 @@
 	}
 
 	/* Override their sizes */
-	sechdrs[me->arch.core_plt_section].sh_size
-		= get_plt_size(hdr, sechdrs, secstrings, 0);
-	sechdrs[me->arch.init_plt_section].sh_size
-		= get_plt_size(hdr, sechdrs, secstrings, 1);
+	if ((rc = get_plt_size(hdr, sechdrs, secstrings, 0)) < 0)
+		return rc;
+
+	sechdrs[me->arch.core_plt_section].sh_size = rc;
+
+	if ((rc = get_plt_size(hdr, sechdrs, secstrings, 1)) < 0)
+		return rc;
+
+	sechdrs[me->arch.init_plt_section].sh_size = rc;
 	return 0;
 }
 

^ permalink raw reply	[flat|nested] 2+ messages in thread
* Re: PATCH improve worst case module load times
@ 2009-03-25 19:14 Jianhua Ding
  0 siblings, 0 replies; 2+ messages in thread
From: Jianhua Ding @ 2009-03-25 19:14 UTC (permalink / raw)
  To: linuxppc-dev


[-- Attachment #1.1: Type: text/plain, Size: 687 bytes --]

Dear Mr. Brian Behlendorf,

I have read your post about improving module load time in the following
site and
Found it very helpful.

http://ozlabs.org/pipermail/linuxppc-dev/2007-June/037641.html

I have the similar issue with Linux 2.6.14 for PPC platform. Since I
cannot find 
the original patch for 2.6.14 so I just manually merge some deltas from
2.6.27.
After patch the loading time is reduced dramatically however it traps
(DSI). 
I wonder if you have heard any other related issues which I missed
patch.

Thanks and best regards,

Jianhua Ding

SW engineer at Nortel

PS: Attached please find the modified module.c file for your reference. 
<<module.c>> 

[-- Attachment #1.2: Type: text/html, Size: 1687 bytes --]

[-- Attachment #2: module.c --]
[-- Type: application/octet-stream, Size: 10403 bytes --]

/*  Kernel module help for PPC.
    Copyright (C) 2001 Rusty Russell.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include <linux/module.h>
#include <linux/moduleloader.h>
#include <linux/elf.h>
#include <linux/vmalloc.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/cache.h>
#include <linux/sort.h>

#if 1
#define DEBUGP printk
#else
#define DEBUGP(fmt , ...)
#endif

LIST_HEAD(module_bug_list);

void *module_alloc(unsigned long size)
{
	if (size == 0)
		return NULL;
	return vmalloc(size);
}

/* Free memory returned from module_alloc */
void module_free(struct module *mod, void *module_region)
{
	vfree(module_region);
	/* FIXME: If module_region == mod->init_region, trim exception
           table entries. */
}

/* Count how many different relocations (different symbol, different
   addend) */
static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
{
	unsigned int i, r_info, r_addend, _count_relocs;

	_count_relocs = 0;
	r_info = 0;
	r_addend = 0;
	for (i = 0; i < num; i++)
		/* Only count 24-bit relocs, others don't need stubs */
		if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
		    (r_info != ELF32_R_SYM(rela[i].r_info) ||
		     r_addend != rela[i].r_addend)) {
			_count_relocs++;
			r_info = ELF32_R_SYM(rela[i].r_info);
			r_addend = rela[i].r_addend;
		}

	return _count_relocs;
}

static int relacmp(const void *_x, const void *_y)
{
	const Elf32_Rela *x, *y;

	y = (Elf32_Rela *)_x;
	x = (Elf32_Rela *)_y;

	/* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
	 * make the comparison cheaper/faster. It won't affect the sorting or
	 * the counting algorithms' performance
	 */
	if (x->r_info < y->r_info)
		return -1;
	else if (x->r_info > y->r_info)
		return 1;
	else if (x->r_addend < y->r_addend)
		return -1;
	else if (x->r_addend > y->r_addend)
		return 1;
	else
		return 0;
}

static void relaswap(void *_x, void *_y, int size)
{
	uint32_t *x, *y, tmp;
	int i;

	y = (uint32_t *)_x;
	x = (uint32_t *)_y;

	for (i = 0; i < sizeof(Elf32_Rela) / sizeof(uint32_t); i++) {
		tmp = x[i];
		x[i] = y[i];
		y[i] = tmp;
	}
}

/* Get the potential trampolines size required of the init and
   non-init sections */
static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
				  const Elf32_Shdr *sechdrs,
				  const char *secstrings,
				  int is_init)
{
	unsigned long ret = 0;
	unsigned i;

	/* Everything marked ALLOC (this includes the exported
           symbols) */
	for (i = 1; i < hdr->e_shnum; i++) {
		/* If it's called *.init*, and we're not init, we're
                   not interested */
		if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
		    != is_init)
			continue;

		/* We don't want to look at debug sections. */
		if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0)
			continue;

		if (sechdrs[i].sh_type == SHT_RELA) {
			DEBUGP("Found relocations in section %u\n", i);
			DEBUGP("Ptr: %p.  Number: %u\n",
			       (void *)hdr + sechdrs[i].sh_offset,
			       sechdrs[i].sh_size / sizeof(Elf32_Rela));

			/* Sort the relocation information based on a symbol and
			 * addend key. This is a stable O(n*log n) complexity
			 * alogrithm but it will reduce the complexity of
			 * count_relocs() to linear complexity O(n)
			 */
			sort((void *)hdr + sechdrs[i].sh_offset,
			     sechdrs[i].sh_size / sizeof(Elf32_Rela),
			     sizeof(Elf32_Rela), relacmp, relaswap);

			ret += count_relocs((void *)hdr
					     + sechdrs[i].sh_offset,
					     sechdrs[i].sh_size
					     / sizeof(Elf32_Rela))
				* sizeof(struct ppc_plt_entry);
		}
	}

	return ret;
}

int module_frob_arch_sections(Elf32_Ehdr *hdr,
			      Elf32_Shdr *sechdrs,
			      char *secstrings,
			      struct module *me)
{
	unsigned int i;

	/* Find .plt and .init.plt sections */
	for (i = 0; i < hdr->e_shnum; i++) {
		if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
			me->arch.init_plt_section = i;
		else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
			me->arch.core_plt_section = i;
	}
	if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
		printk("Module doesn't contain .plt or .init.plt sections.\n");
		return -ENOEXEC;
	}

	/* Override their sizes */
	sechdrs[me->arch.core_plt_section].sh_size
		= get_plt_size(hdr, sechdrs, secstrings, 0);
	sechdrs[me->arch.init_plt_section].sh_size
		= get_plt_size(hdr, sechdrs, secstrings, 1);
	return 0;
}

int apply_relocate(Elf32_Shdr *sechdrs,
		   const char *strtab,
		   unsigned int symindex,
		   unsigned int relsec,
		   struct module *module)
{
	printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n",
	       module->name);
	return -ENOEXEC;
}

static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
{
	if (entry->jump[0] == 0x3d600000 + ((val + 0x8000) >> 16)
	    && entry->jump[1] == 0x396b0000 + (val & 0xffff))
		return 1;
	return 0;
}

/* Set up a trampoline in the PLT to bounce us to the distant function */
static uint32_t do_plt_call(void *location,
			    Elf32_Addr val,
			    Elf32_Shdr *sechdrs,
			    struct module *mod)
{
	struct ppc_plt_entry *entry;

	DEBUGP("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
	/* Init, or core PLT? */
	if (location >= mod->module_core
	    && location < mod->module_core + mod->core_size)
		entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
	else
		entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;

	/* Find this entry, or if that fails, the next avail. entry */
	while (entry->jump[0]) {
		if (entry_matches(entry, val)) return (uint32_t)entry;
		entry++;
	}

	/* Stolen from Paul Mackerras as well... */
	entry->jump[0] = 0x3d600000+((val+0x8000)>>16);	/* lis r11,sym@ha */
	entry->jump[1] = 0x396b0000 + (val&0xffff);	/* addi r11,r11,sym@l*/
	entry->jump[2] = 0x7d6903a6;			/* mtctr r11 */
	entry->jump[3] = 0x4e800420;			/* bctr */

	DEBUGP("Initialized plt for 0x%x at %p\n", val, entry);
	return (uint32_t)entry;
}

int apply_relocate_add(Elf32_Shdr *sechdrs,
		       const char *strtab,
		       unsigned int symindex,
		       unsigned int relsec,
		       struct module *module)
{
	unsigned int i;
	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
	Elf32_Sym *sym;
	uint32_t *location;
	uint32_t value;

	DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
	       sechdrs[relsec].sh_info);
	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
		/* This is where to make the change */
		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
			+ rela[i].r_offset;
		/* This is the symbol it is referring to.  Note that all
		   undefined symbols have been resolved.  */
		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
			+ ELF32_R_SYM(rela[i].r_info);
		/* `Everything is relative'. */
		value = sym->st_value + rela[i].r_addend;

		switch (ELF32_R_TYPE(rela[i].r_info)) {
		case R_PPC_ADDR32:
			/* Simply set it */
			*(uint32_t *)location = value;
			break;

		case R_PPC_ADDR16_LO:
			/* Low half of the symbol */
			*(uint16_t *)location = value;
			break;

		case R_PPC_ADDR16_HI:
			/* Higher half of the symbol */
			*(uint16_t *)location = (value >> 16);
			break;
		
		case R_PPC_ADDR16_HA:
			/* Sign-adjusted lower 16 bits: PPC ELF ABI says:
			   (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
			   This is the same, only sane.
			 */
			*(uint16_t *)location = (value + 0x8000) >> 16;
			break;

		case R_PPC_REL24:
			if ((int)(value - (uint32_t)location) < -0x02000000
			    || (int)(value - (uint32_t)location) >= 0x02000000)
				value = do_plt_call(location, value,
						    sechdrs, module);

			/* Only replace bits 2 through 26 */
			DEBUGP("REL24 value = %08X. location = %08X\n",
			       value, (uint32_t)location);
			DEBUGP("Location before: %08X.\n",
			       *(uint32_t *)location);
			*(uint32_t *)location
				= (*(uint32_t *)location & ~0x03fffffc)
				| ((value - (uint32_t)location)
				   & 0x03fffffc);
			DEBUGP("Location after: %08X.\n",
			       *(uint32_t *)location);
			DEBUGP("ie. jump to %08X+%08X = %08X\n",
			       *(uint32_t *)location & 0x03fffffc,
			       (uint32_t)location,
			       (*(uint32_t *)location & 0x03fffffc)
			       + (uint32_t)location);
			break;

		case R_PPC_REL32:
			/* 32-bit relative jump. */
			*(uint32_t *)location = value - (uint32_t)location;
			break;

		default:
			printk("%s: unknown ADD relocation: %u\n",
			       module->name,
			       ELF32_R_TYPE(rela[i].r_info));
			return -ENOEXEC;
		}
	}
	return 0;
}

int module_finalize(const Elf_Ehdr *hdr,
		    const Elf_Shdr *sechdrs,
		    struct module *me)
{
	char *secstrings;
	unsigned int i;

	me->arch.bug_table = NULL;
	me->arch.num_bugs = 0;

	/* Find the __bug_table section, if present */
	secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
	for (i = 1; i < hdr->e_shnum; i++) {
		if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
			continue;
		me->arch.bug_table = (void *) sechdrs[i].sh_addr;
		me->arch.num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
		break;
	}

	/*
	 * Strictly speaking this should have a spinlock to protect against
	 * traversals, but since we only traverse on BUG()s, a spinlock
	 * could potentially lead to deadlock and thus be counter-productive.
	 */
	list_add(&me->arch.bug_list, &module_bug_list);

	return 0;
}

void module_arch_cleanup(struct module *mod)
{
	list_del(&mod->arch.bug_list);
}

struct bug_entry *module_find_bug(unsigned long bugaddr)
{
	struct mod_arch_specific *mod;
	unsigned int i;
	struct bug_entry *bug;

	list_for_each_entry(mod, &module_bug_list, bug_list) {
		bug = mod->bug_table;
		for (i = 0; i < mod->num_bugs; ++i, ++bug)
			if (bugaddr == bug->bug_addr)
				return bug;
	}
	return NULL;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-03-25 19:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-12  1:00 PATCH improve worst case module load times Brian Behlendorf
  -- strict thread matches above, loose matches on Subject: below --
2009-03-25 19:14 Jianhua Ding

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox