aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c214
1 files changed, 179 insertions, 35 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 0a049837008e..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);
@@ -1068,7 +1078,8 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1068{ 1078{
1069 const unsigned long *crc; 1079 const unsigned long *crc;
1070 1080
1071 if (!find_symbol("module_layout", NULL, &crc, true, false)) 1081 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1082 &crc, true, false))
1072 BUG(); 1083 BUG();
1073 return check_version(sechdrs, versindex, "module_layout", mod, crc); 1084 return check_version(sechdrs, versindex, "module_layout", mod, crc);
1074} 1085}
@@ -1271,6 +1282,10 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1271 struct module_notes_attrs *notes_attrs; 1282 struct module_notes_attrs *notes_attrs;
1272 struct bin_attribute *nattr; 1283 struct bin_attribute *nattr;
1273 1284
1285 /* failed to create section attributes, so can't create notes */
1286 if (!mod->sect_attrs)
1287 return;
1288
1274 /* Count notes sections and allocate structures. */ 1289 /* Count notes sections and allocate structures. */
1275 notes = 0; 1290 notes = 0;
1276 for (i = 0; i < nsect; i++) 1291 for (i = 0; i < nsect; i++)
@@ -1490,6 +1505,8 @@ static int __unlink_module(void *_mod)
1490/* Free a module, remove from lists, etc (must hold module_mutex). */ 1505/* Free a module, remove from lists, etc (must hold module_mutex). */
1491static void free_module(struct module *mod) 1506static void free_module(struct module *mod)
1492{ 1507{
1508 trace_module_free(mod);
1509
1493 /* Delete from various lists */ 1510 /* Delete from various lists */
1494 stop_machine(__unlink_module, mod, NULL); 1511 stop_machine(__unlink_module, mod, NULL);
1495 remove_notes_attrs(mod); 1512 remove_notes_attrs(mod);
@@ -1519,6 +1536,10 @@ static void free_module(struct module *mod)
1519 1536
1520 /* Finally, free the core (containing the module structure) */ 1537 /* Finally, free the core (containing the module structure) */
1521 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
1522} 1543}
1523 1544
1524void *__symbol_get(const char *symbol) 1545void *__symbol_get(const char *symbol)
@@ -1776,6 +1797,17 @@ static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1776 } 1797 }
1777} 1798}
1778 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
1779#ifdef CONFIG_KALLSYMS 1811#ifdef CONFIG_KALLSYMS
1780 1812
1781/* lookup symbol in given range of kernel_symbols */ 1813/* lookup symbol in given range of kernel_symbols */
@@ -1841,13 +1873,93 @@ static char elf_type(const Elf_Sym *sym,
1841 return '?'; 1873 return '?';
1842} 1874}
1843 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
1844static void add_kallsyms(struct module *mod, 1949static void add_kallsyms(struct module *mod,
1845 Elf_Shdr *sechdrs, 1950 Elf_Shdr *sechdrs,
1951 unsigned int shnum,
1846 unsigned int symindex, 1952 unsigned int symindex,
1847 unsigned int strindex, 1953 unsigned int strindex,
1848 const char *secstrings) 1954 unsigned long symoffs,
1955 unsigned long stroffs,
1956 const char *secstrings,
1957 unsigned long *strmap)
1849{ 1958{
1850 unsigned int i; 1959 unsigned int i, ndst;
1960 const Elf_Sym *src;
1961 Elf_Sym *dst;
1962 char *s;
1851 1963
1852 mod->symtab = (void *)sechdrs[symindex].sh_addr; 1964 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1853 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1965 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
@@ -1857,13 +1969,46 @@ static void add_kallsyms(struct module *mod,
1857 for (i = 0; i < mod->num_symtab; i++) 1969 for (i = 0; i < mod->num_symtab; i++)
1858 mod->symtab[i].st_info 1970 mod->symtab[i].st_info
1859 = 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];
1860} 1989}
1861#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
1862static inline void add_kallsyms(struct module *mod, 2003static inline void add_kallsyms(struct module *mod,
1863 Elf_Shdr *sechdrs, 2004 Elf_Shdr *sechdrs,
2005 unsigned int shnum,
1864 unsigned int symindex, 2006 unsigned int symindex,
1865 unsigned int strindex, 2007 unsigned int strindex,
1866 const char *secstrings) 2008 unsigned long symoffs,
2009 unsigned long stroffs,
2010 const char *secstrings,
2011 const unsigned long *strmap)
1867{ 2012{
1868} 2013}
1869#endif /* CONFIG_KALLSYMS */ 2014#endif /* CONFIG_KALLSYMS */
@@ -1938,6 +2083,8 @@ static noinline struct module *load_module(void __user *umod,
1938 struct module *mod; 2083 struct module *mod;
1939 long err = 0; 2084 long err = 0;
1940 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
1941 mm_segment_t old_fs; 2088 mm_segment_t old_fs;
1942 2089
1943 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", 2090 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
@@ -2019,11 +2166,6 @@ static noinline struct module *load_module(void __user *umod,
2019 /* Don't keep modinfo and version sections. */ 2166 /* Don't keep modinfo and version sections. */
2020 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2167 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2021 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2168 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2022#ifdef CONFIG_KALLSYMS
2023 /* Keep symbol and string tables for decoding later. */
2024 sechdrs[symindex].sh_flags |= SHF_ALLOC;
2025 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2026#endif
2027 2169
2028 /* Check module struct version now, before we try to use module. */ 2170 /* Check module struct version now, before we try to use module. */
2029 if (!check_modstruct_version(sechdrs, versindex, mod)) { 2171 if (!check_modstruct_version(sechdrs, versindex, mod)) {
@@ -2059,6 +2201,13 @@ static noinline struct module *load_module(void __user *umod,
2059 goto free_hdr; 2201 goto free_hdr;
2060 } 2202 }
2061 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
2062 if (find_module(mod->name)) { 2211 if (find_module(mod->name)) {
2063 err = -EEXIST; 2212 err = -EEXIST;
2064 goto free_mod; 2213 goto free_mod;
@@ -2088,6 +2237,8 @@ static noinline struct module *load_module(void __user *umod,
2088 this is done generically; there doesn't appear to be any 2237 this is done generically; there doesn't appear to be any
2089 special cases for the architectures. */ 2238 special cases for the architectures. */
2090 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);
2091 2242
2092 /* Do the allocs. */ 2243 /* Do the allocs. */
2093 ptr = module_alloc_update_bounds(mod->core_size); 2244 ptr = module_alloc_update_bounds(mod->core_size);
@@ -2221,10 +2372,6 @@ static noinline struct module *load_module(void __user *umod,
2221 sizeof(*mod->ctors), &mod->num_ctors); 2372 sizeof(*mod->ctors), &mod->num_ctors);
2222#endif 2373#endif
2223 2374
2224#ifdef CONFIG_MARKERS
2225 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2226 sizeof(*mod->markers), &mod->num_markers);
2227#endif
2228#ifdef CONFIG_TRACEPOINTS 2375#ifdef CONFIG_TRACEPOINTS
2229 mod->tracepoints = section_objs(hdr, sechdrs, secstrings, 2376 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2230 "__tracepoints", 2377 "__tracepoints",
@@ -2296,7 +2443,10 @@ static noinline struct module *load_module(void __user *umod,
2296 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, 2443 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2297 sechdrs[pcpuindex].sh_size); 2444 sechdrs[pcpuindex].sh_size);
2298 2445
2299 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;
2300 2450
2301 if (!mod->taints) { 2451 if (!mod->taints) {
2302 struct _ddebug *debug; 2452 struct _ddebug *debug;
@@ -2357,6 +2507,8 @@ static noinline struct module *load_module(void __user *umod,
2357 /* Get rid of temporary copy */ 2507 /* Get rid of temporary copy */
2358 vfree(hdr); 2508 vfree(hdr);
2359 2509
2510 trace_module_load(mod);
2511
2360 /* Done! */ 2512 /* Done! */
2361 return mod; 2513 return mod;
2362 2514
@@ -2366,13 +2518,14 @@ static noinline struct module *load_module(void __user *umod,
2366 synchronize_sched(); 2518 synchronize_sched();
2367 module_arch_cleanup(mod); 2519 module_arch_cleanup(mod);
2368 cleanup: 2520 cleanup:
2521 free_modinfo(mod);
2369 kobject_del(&mod->mkobj.kobj); 2522 kobject_del(&mod->mkobj.kobj);
2370 kobject_put(&mod->mkobj.kobj); 2523 kobject_put(&mod->mkobj.kobj);
2371 free_unload: 2524 free_unload:
2372 module_unload_free(mod); 2525 module_unload_free(mod);
2373#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2526#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2374 free_init:
2375 percpu_modfree(mod->refptr); 2527 percpu_modfree(mod->refptr);
2528 free_init:
2376#endif 2529#endif
2377 module_free(mod, mod->module_init); 2530 module_free(mod, mod->module_init);
2378 free_core: 2531 free_core:
@@ -2383,6 +2536,7 @@ static noinline struct module *load_module(void __user *umod,
2383 percpu_modfree(percpu); 2536 percpu_modfree(percpu);
2384 free_mod: 2537 free_mod:
2385 kfree(args); 2538 kfree(args);
2539 kfree(strmap);
2386 free_hdr: 2540 free_hdr:
2387 vfree(hdr); 2541 vfree(hdr);
2388 return ERR_PTR(err); 2542 return ERR_PTR(err);
@@ -2472,6 +2626,11 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2472 /* Drop initial reference. */ 2626 /* Drop initial reference. */
2473 module_put(mod); 2627 module_put(mod);
2474 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
2475 module_free(mod, mod->module_init); 2634 module_free(mod, mod->module_init);
2476 mod->module_init = NULL; 2635 mod->module_init = NULL;
2477 mod->init_size = 0; 2636 mod->init_size = 0;
@@ -2933,27 +3092,12 @@ void module_layout(struct module *mod,
2933 struct modversion_info *ver, 3092 struct modversion_info *ver,
2934 struct kernel_param *kp, 3093 struct kernel_param *kp,
2935 struct kernel_symbol *ks, 3094 struct kernel_symbol *ks,
2936 struct marker *marker,
2937 struct tracepoint *tp) 3095 struct tracepoint *tp)
2938{ 3096{
2939} 3097}
2940EXPORT_SYMBOL(module_layout); 3098EXPORT_SYMBOL(module_layout);
2941#endif 3099#endif
2942 3100
2943#ifdef CONFIG_MARKERS
2944void module_update_markers(void)
2945{
2946 struct module *mod;
2947
2948 mutex_lock(&module_mutex);
2949 list_for_each_entry(mod, &modules, list)
2950 if (!mod->taints)
2951 marker_update_probe_range(mod->markers,
2952 mod->markers + mod->num_markers);
2953 mutex_unlock(&module_mutex);
2954}
2955#endif
2956
2957#ifdef CONFIG_TRACEPOINTS 3101#ifdef CONFIG_TRACEPOINTS
2958void module_update_tracepoints(void) 3102void module_update_tracepoints(void)
2959{ 3103{