aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c213
1 files changed, 178 insertions, 35 deletions
diff --git a/kernel/module.c b/kernel/module.c
index eccb561dd8a3..5842a71cf052 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,
@@ -942,6 +948,8 @@ void module_put(struct module *module)
942 if (module) { 948 if (module) {
943 unsigned int cpu = get_cpu(); 949 unsigned int cpu = get_cpu();
944 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)));
945 /* Maybe they're waiting for us to drop reference? */ 953 /* Maybe they're waiting for us to drop reference? */
946 if (unlikely(!module_is_live(module))) 954 if (unlikely(!module_is_live(module)))
947 wake_up_process(module->waiter); 955 wake_up_process(module->waiter);
@@ -1179,7 +1187,8 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
1179 1187
1180 /* Count loaded sections and allocate structures */ 1188 /* Count loaded sections and allocate structures */
1181 for (i = 0; i < nsect; i++) 1189 for (i = 0; i < nsect; i++)
1182 if (sechdrs[i].sh_flags & SHF_ALLOC) 1190 if (sechdrs[i].sh_flags & SHF_ALLOC
1191 && sechdrs[i].sh_size)
1183 nloaded++; 1192 nloaded++;
1184 size[0] = ALIGN(sizeof(*sect_attrs) 1193 size[0] = ALIGN(sizeof(*sect_attrs)
1185 + nloaded * sizeof(sect_attrs->attrs[0]), 1194 + nloaded * sizeof(sect_attrs->attrs[0]),
@@ -1199,6 +1208,8 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
1199 for (i = 0; i < nsect; i++) { 1208 for (i = 0; i < nsect; i++) {
1200 if (! (sechdrs[i].sh_flags & SHF_ALLOC)) 1209 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1201 continue; 1210 continue;
1211 if (!sechdrs[i].sh_size)
1212 continue;
1202 sattr->address = sechdrs[i].sh_addr; 1213 sattr->address = sechdrs[i].sh_addr;
1203 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name, 1214 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1204 GFP_KERNEL); 1215 GFP_KERNEL);
@@ -1274,6 +1285,10 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1274 struct module_notes_attrs *notes_attrs; 1285 struct module_notes_attrs *notes_attrs;
1275 struct bin_attribute *nattr; 1286 struct bin_attribute *nattr;
1276 1287
1288 /* failed to create section attributes, so can't create notes */
1289 if (!mod->sect_attrs)
1290 return;
1291
1277 /* Count notes sections and allocate structures. */ 1292 /* Count notes sections and allocate structures. */
1278 notes = 0; 1293 notes = 0;
1279 for (i = 0; i < nsect; i++) 1294 for (i = 0; i < nsect; i++)
@@ -1493,6 +1508,8 @@ static int __unlink_module(void *_mod)
1493/* Free a module, remove from lists, etc (must hold module_mutex). */ 1508/* Free a module, remove from lists, etc (must hold module_mutex). */
1494static void free_module(struct module *mod) 1509static void free_module(struct module *mod)
1495{ 1510{
1511 trace_module_free(mod);
1512
1496 /* Delete from various lists */ 1513 /* Delete from various lists */
1497 stop_machine(__unlink_module, mod, NULL); 1514 stop_machine(__unlink_module, mod, NULL);
1498 remove_notes_attrs(mod); 1515 remove_notes_attrs(mod);
@@ -1522,6 +1539,10 @@ static void free_module(struct module *mod)
1522 1539
1523 /* Finally, free the core (containing the module structure) */ 1540 /* Finally, free the core (containing the module structure) */
1524 module_free(mod, mod->module_core); 1541 module_free(mod, mod->module_core);
1542
1543#ifdef CONFIG_MPU
1544 update_protections(current->mm);
1545#endif
1525} 1546}
1526 1547
1527void *__symbol_get(const char *symbol) 1548void *__symbol_get(const char *symbol)
@@ -1779,6 +1800,17 @@ static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1779 } 1800 }
1780} 1801}
1781 1802
1803static void free_modinfo(struct module *mod)
1804{
1805 struct module_attribute *attr;
1806 int i;
1807
1808 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1809 if (attr->free)
1810 attr->free(mod);
1811 }
1812}
1813
1782#ifdef CONFIG_KALLSYMS 1814#ifdef CONFIG_KALLSYMS
1783 1815
1784/* lookup symbol in given range of kernel_symbols */ 1816/* lookup symbol in given range of kernel_symbols */
@@ -1844,13 +1876,93 @@ static char elf_type(const Elf_Sym *sym,
1844 return '?'; 1876 return '?';
1845} 1877}
1846 1878
1879static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1880 unsigned int shnum)
1881{
1882 const Elf_Shdr *sec;
1883
1884 if (src->st_shndx == SHN_UNDEF
1885 || src->st_shndx >= shnum
1886 || !src->st_name)
1887 return false;
1888
1889 sec = sechdrs + src->st_shndx;
1890 if (!(sec->sh_flags & SHF_ALLOC)
1891#ifndef CONFIG_KALLSYMS_ALL
1892 || !(sec->sh_flags & SHF_EXECINSTR)
1893#endif
1894 || (sec->sh_entsize & INIT_OFFSET_MASK))
1895 return false;
1896
1897 return true;
1898}
1899
1900static unsigned long layout_symtab(struct module *mod,
1901 Elf_Shdr *sechdrs,
1902 unsigned int symindex,
1903 unsigned int strindex,
1904 const Elf_Ehdr *hdr,
1905 const char *secstrings,
1906 unsigned long *pstroffs,
1907 unsigned long *strmap)
1908{
1909 unsigned long symoffs;
1910 Elf_Shdr *symsect = sechdrs + symindex;
1911 Elf_Shdr *strsect = sechdrs + strindex;
1912 const Elf_Sym *src;
1913 const char *strtab;
1914 unsigned int i, nsrc, ndst;
1915
1916 /* Put symbol section at end of init part of module. */
1917 symsect->sh_flags |= SHF_ALLOC;
1918 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
1919 symindex) | INIT_OFFSET_MASK;
1920 DEBUGP("\t%s\n", secstrings + symsect->sh_name);
1921
1922 src = (void *)hdr + symsect->sh_offset;
1923 nsrc = symsect->sh_size / sizeof(*src);
1924 strtab = (void *)hdr + strsect->sh_offset;
1925 for (ndst = i = 1; i < nsrc; ++i, ++src)
1926 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
1927 unsigned int j = src->st_name;
1928
1929 while(!__test_and_set_bit(j, strmap) && strtab[j])
1930 ++j;
1931 ++ndst;
1932 }
1933
1934 /* Append room for core symbols at end of core part. */
1935 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1936 mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
1937
1938 /* Put string table section at end of init part of module. */
1939 strsect->sh_flags |= SHF_ALLOC;
1940 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1941 strindex) | INIT_OFFSET_MASK;
1942 DEBUGP("\t%s\n", secstrings + strsect->sh_name);
1943
1944 /* Append room for core symbols' strings at end of core part. */
1945 *pstroffs = mod->core_size;
1946 __set_bit(0, strmap);
1947 mod->core_size += bitmap_weight(strmap, strsect->sh_size);
1948
1949 return symoffs;
1950}
1951
1847static void add_kallsyms(struct module *mod, 1952static void add_kallsyms(struct module *mod,
1848 Elf_Shdr *sechdrs, 1953 Elf_Shdr *sechdrs,
1954 unsigned int shnum,
1849 unsigned int symindex, 1955 unsigned int symindex,
1850 unsigned int strindex, 1956 unsigned int strindex,
1851 const char *secstrings) 1957 unsigned long symoffs,
1958 unsigned long stroffs,
1959 const char *secstrings,
1960 unsigned long *strmap)
1852{ 1961{
1853 unsigned int i; 1962 unsigned int i, ndst;
1963 const Elf_Sym *src;
1964 Elf_Sym *dst;
1965 char *s;
1854 1966
1855 mod->symtab = (void *)sechdrs[symindex].sh_addr; 1967 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1856 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1968 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
@@ -1860,13 +1972,46 @@ static void add_kallsyms(struct module *mod,
1860 for (i = 0; i < mod->num_symtab; i++) 1972 for (i = 0; i < mod->num_symtab; i++)
1861 mod->symtab[i].st_info 1973 mod->symtab[i].st_info
1862 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod); 1974 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1975
1976 mod->core_symtab = dst = mod->module_core + symoffs;
1977 src = mod->symtab;
1978 *dst = *src;
1979 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
1980 if (!is_core_symbol(src, sechdrs, shnum))
1981 continue;
1982 dst[ndst] = *src;
1983 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
1984 ++ndst;
1985 }
1986 mod->core_num_syms = ndst;
1987
1988 mod->core_strtab = s = mod->module_core + stroffs;
1989 for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
1990 if (test_bit(i, strmap))
1991 *++s = mod->strtab[i];
1863} 1992}
1864#else 1993#else
1994static inline unsigned long layout_symtab(struct module *mod,
1995 Elf_Shdr *sechdrs,
1996 unsigned int symindex,
1997 unsigned int strindex,
1998 const Elf_Ehdr *hdr,
1999 const char *secstrings,
2000 unsigned long *pstroffs,
2001 unsigned long *strmap)
2002{
2003 return 0;
2004}
2005
1865static inline void add_kallsyms(struct module *mod, 2006static inline void add_kallsyms(struct module *mod,
1866 Elf_Shdr *sechdrs, 2007 Elf_Shdr *sechdrs,
2008 unsigned int shnum,
1867 unsigned int symindex, 2009 unsigned int symindex,
1868 unsigned int strindex, 2010 unsigned int strindex,
1869 const char *secstrings) 2011 unsigned long symoffs,
2012 unsigned long stroffs,
2013 const char *secstrings,
2014 const unsigned long *strmap)
1870{ 2015{
1871} 2016}
1872#endif /* CONFIG_KALLSYMS */ 2017#endif /* CONFIG_KALLSYMS */
@@ -1941,6 +2086,8 @@ static noinline struct module *load_module(void __user *umod,
1941 struct module *mod; 2086 struct module *mod;
1942 long err = 0; 2087 long err = 0;
1943 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 2088 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
2089 unsigned long symoffs, stroffs, *strmap;
2090
1944 mm_segment_t old_fs; 2091 mm_segment_t old_fs;
1945 2092
1946 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", 2093 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
@@ -2022,11 +2169,6 @@ static noinline struct module *load_module(void __user *umod,
2022 /* Don't keep modinfo and version sections. */ 2169 /* Don't keep modinfo and version sections. */
2023 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2170 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2024 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2171 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2025#ifdef CONFIG_KALLSYMS
2026 /* Keep symbol and string tables for decoding later. */
2027 sechdrs[symindex].sh_flags |= SHF_ALLOC;
2028 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2029#endif
2030 2172
2031 /* Check module struct version now, before we try to use module. */ 2173 /* Check module struct version now, before we try to use module. */
2032 if (!check_modstruct_version(sechdrs, versindex, mod)) { 2174 if (!check_modstruct_version(sechdrs, versindex, mod)) {
@@ -2062,6 +2204,13 @@ static noinline struct module *load_module(void __user *umod,
2062 goto free_hdr; 2204 goto free_hdr;
2063 } 2205 }
2064 2206
2207 strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
2208 * sizeof(long), GFP_KERNEL);
2209 if (!strmap) {
2210 err = -ENOMEM;
2211 goto free_mod;
2212 }
2213
2065 if (find_module(mod->name)) { 2214 if (find_module(mod->name)) {
2066 err = -EEXIST; 2215 err = -EEXIST;
2067 goto free_mod; 2216 goto free_mod;
@@ -2091,6 +2240,8 @@ static noinline struct module *load_module(void __user *umod,
2091 this is done generically; there doesn't appear to be any 2240 this is done generically; there doesn't appear to be any
2092 special cases for the architectures. */ 2241 special cases for the architectures. */
2093 layout_sections(mod, hdr, sechdrs, secstrings); 2242 layout_sections(mod, hdr, sechdrs, secstrings);
2243 symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
2244 secstrings, &stroffs, strmap);
2094 2245
2095 /* Do the allocs. */ 2246 /* Do the allocs. */
2096 ptr = module_alloc_update_bounds(mod->core_size); 2247 ptr = module_alloc_update_bounds(mod->core_size);
@@ -2224,10 +2375,6 @@ static noinline struct module *load_module(void __user *umod,
2224 sizeof(*mod->ctors), &mod->num_ctors); 2375 sizeof(*mod->ctors), &mod->num_ctors);
2225#endif 2376#endif
2226 2377
2227#ifdef CONFIG_MARKERS
2228 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2229 sizeof(*mod->markers), &mod->num_markers);
2230#endif
2231#ifdef CONFIG_TRACEPOINTS 2378#ifdef CONFIG_TRACEPOINTS
2232 mod->tracepoints = section_objs(hdr, sechdrs, secstrings, 2379 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2233 "__tracepoints", 2380 "__tracepoints",
@@ -2299,7 +2446,10 @@ static noinline struct module *load_module(void __user *umod,
2299 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, 2446 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2300 sechdrs[pcpuindex].sh_size); 2447 sechdrs[pcpuindex].sh_size);
2301 2448
2302 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings); 2449 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
2450 symoffs, stroffs, secstrings, strmap);
2451 kfree(strmap);
2452 strmap = NULL;
2303 2453
2304 if (!mod->taints) { 2454 if (!mod->taints) {
2305 struct _ddebug *debug; 2455 struct _ddebug *debug;
@@ -2355,12 +2505,13 @@ static noinline struct module *load_module(void __user *umod,
2355 if (err < 0) 2505 if (err < 0)
2356 goto unlink; 2506 goto unlink;
2357 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2507 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2358 if (mod->sect_attrs) 2508 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2359 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2360 2509
2361 /* Get rid of temporary copy */ 2510 /* Get rid of temporary copy */
2362 vfree(hdr); 2511 vfree(hdr);
2363 2512
2513 trace_module_load(mod);
2514
2364 /* Done! */ 2515 /* Done! */
2365 return mod; 2516 return mod;
2366 2517
@@ -2370,13 +2521,14 @@ static noinline struct module *load_module(void __user *umod,
2370 synchronize_sched(); 2521 synchronize_sched();
2371 module_arch_cleanup(mod); 2522 module_arch_cleanup(mod);
2372 cleanup: 2523 cleanup:
2524 free_modinfo(mod);
2373 kobject_del(&mod->mkobj.kobj); 2525 kobject_del(&mod->mkobj.kobj);
2374 kobject_put(&mod->mkobj.kobj); 2526 kobject_put(&mod->mkobj.kobj);
2375 free_unload: 2527 free_unload:
2376 module_unload_free(mod); 2528 module_unload_free(mod);
2377#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2529#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2378 free_init:
2379 percpu_modfree(mod->refptr); 2530 percpu_modfree(mod->refptr);
2531 free_init:
2380#endif 2532#endif
2381 module_free(mod, mod->module_init); 2533 module_free(mod, mod->module_init);
2382 free_core: 2534 free_core:
@@ -2387,6 +2539,7 @@ static noinline struct module *load_module(void __user *umod,
2387 percpu_modfree(percpu); 2539 percpu_modfree(percpu);
2388 free_mod: 2540 free_mod:
2389 kfree(args); 2541 kfree(args);
2542 kfree(strmap);
2390 free_hdr: 2543 free_hdr:
2391 vfree(hdr); 2544 vfree(hdr);
2392 return ERR_PTR(err); 2545 return ERR_PTR(err);
@@ -2476,6 +2629,11 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2476 /* Drop initial reference. */ 2629 /* Drop initial reference. */
2477 module_put(mod); 2630 module_put(mod);
2478 trim_init_extable(mod); 2631 trim_init_extable(mod);
2632#ifdef CONFIG_KALLSYMS
2633 mod->num_symtab = mod->core_num_syms;
2634 mod->symtab = mod->core_symtab;
2635 mod->strtab = mod->core_strtab;
2636#endif
2479 module_free(mod, mod->module_init); 2637 module_free(mod, mod->module_init);
2480 mod->module_init = NULL; 2638 mod->module_init = NULL;
2481 mod->init_size = 0; 2639 mod->init_size = 0;
@@ -2937,27 +3095,12 @@ void module_layout(struct module *mod,
2937 struct modversion_info *ver, 3095 struct modversion_info *ver,
2938 struct kernel_param *kp, 3096 struct kernel_param *kp,
2939 struct kernel_symbol *ks, 3097 struct kernel_symbol *ks,
2940 struct marker *marker,
2941 struct tracepoint *tp) 3098 struct tracepoint *tp)
2942{ 3099{
2943} 3100}
2944EXPORT_SYMBOL(module_layout); 3101EXPORT_SYMBOL(module_layout);
2945#endif 3102#endif
2946 3103
2947#ifdef CONFIG_MARKERS
2948void module_update_markers(void)
2949{
2950 struct module *mod;
2951
2952 mutex_lock(&module_mutex);
2953 list_for_each_entry(mod, &modules, list)
2954 if (!mod->taints)
2955 marker_update_probe_range(mod->markers,
2956 mod->markers + mod->num_markers);
2957 mutex_unlock(&module_mutex);
2958}
2959#endif
2960
2961#ifdef CONFIG_TRACEPOINTS 3104#ifdef CONFIG_TRACEPOINTS
2962void module_update_tracepoints(void) 3105void module_update_tracepoints(void)
2963{ 3106{