aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
authorHaavard Skinnemoen <haavard.skinnemoen@atmel.com>2009-10-06 11:36:55 -0400
committerHaavard Skinnemoen <haavard.skinnemoen@atmel.com>2009-10-06 11:36:55 -0400
commitd94e5fcbf1420366dcb4102bafe04dbcfc0d0d4b (patch)
treea9b7de7df6da5c3132cc68169b9c47ba288ccd42 /kernel/module.c
parentd55651168a20078a94597a297d5cdfd807bf07b6 (diff)
parent374576a8b6f865022c0fd1ca62396889b23d66dd (diff)
Merge commit 'v2.6.32-rc3'
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c211
1 files changed, 177 insertions, 34 deletions
diff --git a/kernel/module.c b/kernel/module.c
index fd1411403558..8b7d8805819d 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -47,6 +47,7 @@
47#include <linux/rculist.h> 47#include <linux/rculist.h>
48#include <asm/uaccess.h> 48#include <asm/uaccess.h>
49#include <asm/cacheflush.h> 49#include <asm/cacheflush.h>
50#include <asm/mmu_context.h>
50#include <linux/license.h> 51#include <linux/license.h>
51#include <asm/sections.h> 52#include <asm/sections.h>
52#include <linux/tracepoint.h> 53#include <linux/tracepoint.h>
@@ -55,6 +56,11 @@
55#include <linux/percpu.h> 56#include <linux/percpu.h>
56#include <linux/kmemleak.h> 57#include <linux/kmemleak.h>
57 58
59#define CREATE_TRACE_POINTS
60#include <trace/events/module.h>
61
62EXPORT_TRACEPOINT_SYMBOL(module_get);
63
58#if 0 64#if 0
59#define DEBUGP printk 65#define DEBUGP printk
60#else 66#else
@@ -364,7 +370,7 @@ EXPORT_SYMBOL_GPL(find_module);
364 370
365#ifdef CONFIG_SMP 371#ifdef CONFIG_SMP
366 372
367#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA 373#ifndef CONFIG_HAVE_LEGACY_PER_CPU_AREA
368 374
369static void *percpu_modalloc(unsigned long size, unsigned long align, 375static void *percpu_modalloc(unsigned long size, unsigned long align,
370 const char *name) 376 const char *name)
@@ -389,7 +395,7 @@ static void percpu_modfree(void *freeme)
389 free_percpu(freeme); 395 free_percpu(freeme);
390} 396}
391 397
392#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */ 398#else /* ... CONFIG_HAVE_LEGACY_PER_CPU_AREA */
393 399
394/* Number of blocks used and allocated. */ 400/* Number of blocks used and allocated. */
395static unsigned int pcpu_num_used, pcpu_num_allocated; 401static unsigned int pcpu_num_used, pcpu_num_allocated;
@@ -535,7 +541,7 @@ static int percpu_modinit(void)
535} 541}
536__initcall(percpu_modinit); 542__initcall(percpu_modinit);
537 543
538#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */ 544#endif /* CONFIG_HAVE_LEGACY_PER_CPU_AREA */
539 545
540static unsigned int find_pcpusec(Elf_Ehdr *hdr, 546static unsigned int find_pcpusec(Elf_Ehdr *hdr,
541 Elf_Shdr *sechdrs, 547 Elf_Shdr *sechdrs,
@@ -909,16 +915,18 @@ void __symbol_put(const char *symbol)
909} 915}
910EXPORT_SYMBOL(__symbol_put); 916EXPORT_SYMBOL(__symbol_put);
911 917
918/* Note this assumes addr is a function, which it currently always is. */
912void symbol_put_addr(void *addr) 919void symbol_put_addr(void *addr)
913{ 920{
914 struct module *modaddr; 921 struct module *modaddr;
922 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
915 923
916 if (core_kernel_text((unsigned long)addr)) 924 if (core_kernel_text(a))
917 return; 925 return;
918 926
919 /* module_text_address is safe here: we're supposed to have reference 927 /* module_text_address is safe here: we're supposed to have reference
920 * to module from symbol_get, so it can't go away. */ 928 * to module from symbol_get, so it can't go away. */
921 modaddr = __module_text_address((unsigned long)addr); 929 modaddr = __module_text_address(a);
922 BUG_ON(!modaddr); 930 BUG_ON(!modaddr);
923 module_put(modaddr); 931 module_put(modaddr);
924} 932}
@@ -940,6 +948,8 @@ void module_put(struct module *module)
940 if (module) { 948 if (module) {
941 unsigned int cpu = get_cpu(); 949 unsigned int cpu = get_cpu();
942 local_dec(__module_ref_addr(module, cpu)); 950 local_dec(__module_ref_addr(module, cpu));
951 trace_module_put(module, _RET_IP_,
952 local_read(__module_ref_addr(module, cpu)));
943 /* Maybe they're waiting for us to drop reference? */ 953 /* Maybe they're waiting for us to drop reference? */
944 if (unlikely(!module_is_live(module))) 954 if (unlikely(!module_is_live(module)))
945 wake_up_process(module->waiter); 955 wake_up_process(module->waiter);
@@ -1272,6 +1282,10 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1272 struct module_notes_attrs *notes_attrs; 1282 struct module_notes_attrs *notes_attrs;
1273 struct bin_attribute *nattr; 1283 struct bin_attribute *nattr;
1274 1284
1285 /* failed to create section attributes, so can't create notes */
1286 if (!mod->sect_attrs)
1287 return;
1288
1275 /* Count notes sections and allocate structures. */ 1289 /* Count notes sections and allocate structures. */
1276 notes = 0; 1290 notes = 0;
1277 for (i = 0; i < nsect; i++) 1291 for (i = 0; i < nsect; i++)
@@ -1491,6 +1505,8 @@ static int __unlink_module(void *_mod)
1491/* Free a module, remove from lists, etc (must hold module_mutex). */ 1505/* Free a module, remove from lists, etc (must hold module_mutex). */
1492static void free_module(struct module *mod) 1506static void free_module(struct module *mod)
1493{ 1507{
1508 trace_module_free(mod);
1509
1494 /* Delete from various lists */ 1510 /* Delete from various lists */
1495 stop_machine(__unlink_module, mod, NULL); 1511 stop_machine(__unlink_module, mod, NULL);
1496 remove_notes_attrs(mod); 1512 remove_notes_attrs(mod);
@@ -1520,6 +1536,10 @@ static void free_module(struct module *mod)
1520 1536
1521 /* Finally, free the core (containing the module structure) */ 1537 /* Finally, free the core (containing the module structure) */
1522 module_free(mod, mod->module_core); 1538 module_free(mod, mod->module_core);
1539
1540#ifdef CONFIG_MPU
1541 update_protections(current->mm);
1542#endif
1523} 1543}
1524 1544
1525void *__symbol_get(const char *symbol) 1545void *__symbol_get(const char *symbol)
@@ -1777,6 +1797,17 @@ static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1777 } 1797 }
1778} 1798}
1779 1799
1800static void free_modinfo(struct module *mod)
1801{
1802 struct module_attribute *attr;
1803 int i;
1804
1805 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1806 if (attr->free)
1807 attr->free(mod);
1808 }
1809}
1810
1780#ifdef CONFIG_KALLSYMS 1811#ifdef CONFIG_KALLSYMS
1781 1812
1782/* lookup symbol in given range of kernel_symbols */ 1813/* lookup symbol in given range of kernel_symbols */
@@ -1842,13 +1873,93 @@ static char elf_type(const Elf_Sym *sym,
1842 return '?'; 1873 return '?';
1843} 1874}
1844 1875
1876static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1877 unsigned int shnum)
1878{
1879 const Elf_Shdr *sec;
1880
1881 if (src->st_shndx == SHN_UNDEF
1882 || src->st_shndx >= shnum
1883 || !src->st_name)
1884 return false;
1885
1886 sec = sechdrs + src->st_shndx;
1887 if (!(sec->sh_flags & SHF_ALLOC)
1888#ifndef CONFIG_KALLSYMS_ALL
1889 || !(sec->sh_flags & SHF_EXECINSTR)
1890#endif
1891 || (sec->sh_entsize & INIT_OFFSET_MASK))
1892 return false;
1893
1894 return true;
1895}
1896
1897static unsigned long layout_symtab(struct module *mod,
1898 Elf_Shdr *sechdrs,
1899 unsigned int symindex,
1900 unsigned int strindex,
1901 const Elf_Ehdr *hdr,
1902 const char *secstrings,
1903 unsigned long *pstroffs,
1904 unsigned long *strmap)
1905{
1906 unsigned long symoffs;
1907 Elf_Shdr *symsect = sechdrs + symindex;
1908 Elf_Shdr *strsect = sechdrs + strindex;
1909 const Elf_Sym *src;
1910 const char *strtab;
1911 unsigned int i, nsrc, ndst;
1912
1913 /* Put symbol section at end of init part of module. */
1914 symsect->sh_flags |= SHF_ALLOC;
1915 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
1916 symindex) | INIT_OFFSET_MASK;
1917 DEBUGP("\t%s\n", secstrings + symsect->sh_name);
1918
1919 src = (void *)hdr + symsect->sh_offset;
1920 nsrc = symsect->sh_size / sizeof(*src);
1921 strtab = (void *)hdr + strsect->sh_offset;
1922 for (ndst = i = 1; i < nsrc; ++i, ++src)
1923 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
1924 unsigned int j = src->st_name;
1925
1926 while(!__test_and_set_bit(j, strmap) && strtab[j])
1927 ++j;
1928 ++ndst;
1929 }
1930
1931 /* Append room for core symbols at end of core part. */
1932 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1933 mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
1934
1935 /* Put string table section at end of init part of module. */
1936 strsect->sh_flags |= SHF_ALLOC;
1937 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1938 strindex) | INIT_OFFSET_MASK;
1939 DEBUGP("\t%s\n", secstrings + strsect->sh_name);
1940
1941 /* Append room for core symbols' strings at end of core part. */
1942 *pstroffs = mod->core_size;
1943 __set_bit(0, strmap);
1944 mod->core_size += bitmap_weight(strmap, strsect->sh_size);
1945
1946 return symoffs;
1947}
1948
1845static void add_kallsyms(struct module *mod, 1949static void add_kallsyms(struct module *mod,
1846 Elf_Shdr *sechdrs, 1950 Elf_Shdr *sechdrs,
1951 unsigned int shnum,
1847 unsigned int symindex, 1952 unsigned int symindex,
1848 unsigned int strindex, 1953 unsigned int strindex,
1849 const char *secstrings) 1954 unsigned long symoffs,
1955 unsigned long stroffs,
1956 const char *secstrings,
1957 unsigned long *strmap)
1850{ 1958{
1851 unsigned int i; 1959 unsigned int i, ndst;
1960 const Elf_Sym *src;
1961 Elf_Sym *dst;
1962 char *s;
1852 1963
1853 mod->symtab = (void *)sechdrs[symindex].sh_addr; 1964 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1854 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1965 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
@@ -1858,13 +1969,46 @@ static void add_kallsyms(struct module *mod,
1858 for (i = 0; i < mod->num_symtab; i++) 1969 for (i = 0; i < mod->num_symtab; i++)
1859 mod->symtab[i].st_info 1970 mod->symtab[i].st_info
1860 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod); 1971 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1972
1973 mod->core_symtab = dst = mod->module_core + symoffs;
1974 src = mod->symtab;
1975 *dst = *src;
1976 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
1977 if (!is_core_symbol(src, sechdrs, shnum))
1978 continue;
1979 dst[ndst] = *src;
1980 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
1981 ++ndst;
1982 }
1983 mod->core_num_syms = ndst;
1984
1985 mod->core_strtab = s = mod->module_core + stroffs;
1986 for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
1987 if (test_bit(i, strmap))
1988 *++s = mod->strtab[i];
1861} 1989}
1862#else 1990#else
1991static inline unsigned long layout_symtab(struct module *mod,
1992 Elf_Shdr *sechdrs,
1993 unsigned int symindex,
1994 unsigned int strindex,
1995 const Elf_Ehdr *hdr,
1996 const char *secstrings,
1997 unsigned long *pstroffs,
1998 unsigned long *strmap)
1999{
2000 return 0;
2001}
2002
1863static inline void add_kallsyms(struct module *mod, 2003static inline void add_kallsyms(struct module *mod,
1864 Elf_Shdr *sechdrs, 2004 Elf_Shdr *sechdrs,
2005 unsigned int shnum,
1865 unsigned int symindex, 2006 unsigned int symindex,
1866 unsigned int strindex, 2007 unsigned int strindex,
1867 const char *secstrings) 2008 unsigned long symoffs,
2009 unsigned long stroffs,
2010 const char *secstrings,
2011 const unsigned long *strmap)
1868{ 2012{
1869} 2013}
1870#endif /* CONFIG_KALLSYMS */ 2014#endif /* CONFIG_KALLSYMS */
@@ -1939,6 +2083,8 @@ static noinline struct module *load_module(void __user *umod,
1939 struct module *mod; 2083 struct module *mod;
1940 long err = 0; 2084 long err = 0;
1941 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 2085 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
2086 unsigned long symoffs, stroffs, *strmap;
2087
1942 mm_segment_t old_fs; 2088 mm_segment_t old_fs;
1943 2089
1944 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", 2090 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
@@ -2020,11 +2166,6 @@ static noinline struct module *load_module(void __user *umod,
2020 /* Don't keep modinfo and version sections. */ 2166 /* Don't keep modinfo and version sections. */
2021 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2167 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2022 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2168 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2023#ifdef CONFIG_KALLSYMS
2024 /* Keep symbol and string tables for decoding later. */
2025 sechdrs[symindex].sh_flags |= SHF_ALLOC;
2026 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2027#endif
2028 2169
2029 /* Check module struct version now, before we try to use module. */ 2170 /* Check module struct version now, before we try to use module. */
2030 if (!check_modstruct_version(sechdrs, versindex, mod)) { 2171 if (!check_modstruct_version(sechdrs, versindex, mod)) {
@@ -2060,6 +2201,13 @@ static noinline struct module *load_module(void __user *umod,
2060 goto free_hdr; 2201 goto free_hdr;
2061 } 2202 }
2062 2203
2204 strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
2205 * sizeof(long), GFP_KERNEL);
2206 if (!strmap) {
2207 err = -ENOMEM;
2208 goto free_mod;
2209 }
2210
2063 if (find_module(mod->name)) { 2211 if (find_module(mod->name)) {
2064 err = -EEXIST; 2212 err = -EEXIST;
2065 goto free_mod; 2213 goto free_mod;
@@ -2089,6 +2237,8 @@ static noinline struct module *load_module(void __user *umod,
2089 this is done generically; there doesn't appear to be any 2237 this is done generically; there doesn't appear to be any
2090 special cases for the architectures. */ 2238 special cases for the architectures. */
2091 layout_sections(mod, hdr, sechdrs, secstrings); 2239 layout_sections(mod, hdr, sechdrs, secstrings);
2240 symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
2241 secstrings, &stroffs, strmap);
2092 2242
2093 /* Do the allocs. */ 2243 /* Do the allocs. */
2094 ptr = module_alloc_update_bounds(mod->core_size); 2244 ptr = module_alloc_update_bounds(mod->core_size);
@@ -2222,10 +2372,6 @@ static noinline struct module *load_module(void __user *umod,
2222 sizeof(*mod->ctors), &mod->num_ctors); 2372 sizeof(*mod->ctors), &mod->num_ctors);
2223#endif 2373#endif
2224 2374
2225#ifdef CONFIG_MARKERS
2226 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2227 sizeof(*mod->markers), &mod->num_markers);
2228#endif
2229#ifdef CONFIG_TRACEPOINTS 2375#ifdef CONFIG_TRACEPOINTS
2230 mod->tracepoints = section_objs(hdr, sechdrs, secstrings, 2376 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2231 "__tracepoints", 2377 "__tracepoints",
@@ -2297,7 +2443,10 @@ static noinline struct module *load_module(void __user *umod,
2297 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, 2443 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2298 sechdrs[pcpuindex].sh_size); 2444 sechdrs[pcpuindex].sh_size);
2299 2445
2300 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings); 2446 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
2447 symoffs, stroffs, secstrings, strmap);
2448 kfree(strmap);
2449 strmap = NULL;
2301 2450
2302 if (!mod->taints) { 2451 if (!mod->taints) {
2303 struct _ddebug *debug; 2452 struct _ddebug *debug;
@@ -2358,6 +2507,8 @@ static noinline struct module *load_module(void __user *umod,
2358 /* Get rid of temporary copy */ 2507 /* Get rid of temporary copy */
2359 vfree(hdr); 2508 vfree(hdr);
2360 2509
2510 trace_module_load(mod);
2511
2361 /* Done! */ 2512 /* Done! */
2362 return mod; 2513 return mod;
2363 2514
@@ -2367,13 +2518,14 @@ static noinline struct module *load_module(void __user *umod,
2367 synchronize_sched(); 2518 synchronize_sched();
2368 module_arch_cleanup(mod); 2519 module_arch_cleanup(mod);
2369 cleanup: 2520 cleanup:
2521 free_modinfo(mod);
2370 kobject_del(&mod->mkobj.kobj); 2522 kobject_del(&mod->mkobj.kobj);
2371 kobject_put(&mod->mkobj.kobj); 2523 kobject_put(&mod->mkobj.kobj);
2372 free_unload: 2524 free_unload:
2373 module_unload_free(mod); 2525 module_unload_free(mod);
2374#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2526#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2375 free_init:
2376 percpu_modfree(mod->refptr); 2527 percpu_modfree(mod->refptr);
2528 free_init:
2377#endif 2529#endif
2378 module_free(mod, mod->module_init); 2530 module_free(mod, mod->module_init);
2379 free_core: 2531 free_core:
@@ -2384,6 +2536,7 @@ static noinline struct module *load_module(void __user *umod,
2384 percpu_modfree(percpu); 2536 percpu_modfree(percpu);
2385 free_mod: 2537 free_mod:
2386 kfree(args); 2538 kfree(args);
2539 kfree(strmap);
2387 free_hdr: 2540 free_hdr:
2388 vfree(hdr); 2541 vfree(hdr);
2389 return ERR_PTR(err); 2542 return ERR_PTR(err);
@@ -2473,6 +2626,11 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2473 /* Drop initial reference. */ 2626 /* Drop initial reference. */
2474 module_put(mod); 2627 module_put(mod);
2475 trim_init_extable(mod); 2628 trim_init_extable(mod);
2629#ifdef CONFIG_KALLSYMS
2630 mod->num_symtab = mod->core_num_syms;
2631 mod->symtab = mod->core_symtab;
2632 mod->strtab = mod->core_strtab;
2633#endif
2476 module_free(mod, mod->module_init); 2634 module_free(mod, mod->module_init);
2477 mod->module_init = NULL; 2635 mod->module_init = NULL;
2478 mod->init_size = 0; 2636 mod->init_size = 0;
@@ -2934,27 +3092,12 @@ void module_layout(struct module *mod,
2934 struct modversion_info *ver, 3092 struct modversion_info *ver,
2935 struct kernel_param *kp, 3093 struct kernel_param *kp,
2936 struct kernel_symbol *ks, 3094 struct kernel_symbol *ks,
2937 struct marker *marker,
2938 struct tracepoint *tp) 3095 struct tracepoint *tp)
2939{ 3096{
2940} 3097}
2941EXPORT_SYMBOL(module_layout); 3098EXPORT_SYMBOL(module_layout);
2942#endif 3099#endif
2943 3100
2944#ifdef CONFIG_MARKERS
2945void module_update_markers(void)
2946{
2947 struct module *mod;
2948
2949 mutex_lock(&module_mutex);
2950 list_for_each_entry(mod, &modules, list)
2951 if (!mod->taints)
2952 marker_update_probe_range(mod->markers,
2953 mod->markers + mod->num_markers);
2954 mutex_unlock(&module_mutex);
2955}
2956#endif
2957
2958#ifdef CONFIG_TRACEPOINTS 3101#ifdef CONFIG_TRACEPOINTS
2959void module_update_tracepoints(void) 3102void module_update_tracepoints(void)
2960{ 3103{