Commit 5e376613899076396d0c97de67ad072587267370
Committed by
Linus Torvalds
1 parent
0159677857
Exists in
master
and in
4 other branches
[PATCH] symbol_put_addr() locks kernel
Even since a previous patch: Fix race between CONFIG_DEBUG_SLABALLOC and modules Sun, 27 Jun 2004 17:55:19 +0000 (17:55 +0000) http://www.kernel.org/git/?p=linux/kernel/git/torvalds/old-2.6-bkcvs.git;a=commit;h=92b3db26d31cf21b70e3c1eadc56c179506d8fbe The function symbol_put_addr() will deadlock the kernel. symbol_put_addr() would acquire modlist_lock, then while holding the lock call two functions kernel_text_address() and module_text_address() which also try to acquire the same lock. This deadlocks the kernel of course. This patch changes symbol_put_addr() to not acquire the modlist_lock, it doesn't need it since it never looks at the module list directly. Also, it now uses core_kernel_text() instead of kernel_text_address(). The latter has an additional check for addr inside a module, but we don't need to do that since we call module_text_address() (the same function kernel_text_address uses) ourselves. Signed-off-by: Trent Piepho <xyzzy@speakeasy.org> Cc: Zwane Mwaikambo <zwane@fsmlabs.com> Acked-by: Rusty Russell <rusty@rustcorp.com.au> Cc: Johannes Stezenbach <js@linuxtv.org> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Showing 3 changed files with 8 additions and 7 deletions Inline Diff
include/linux/kernel.h
1 | #ifndef _LINUX_KERNEL_H | 1 | #ifndef _LINUX_KERNEL_H |
2 | #define _LINUX_KERNEL_H | 2 | #define _LINUX_KERNEL_H |
3 | 3 | ||
4 | /* | 4 | /* |
5 | * 'kernel.h' contains some often-used function prototypes etc | 5 | * 'kernel.h' contains some often-used function prototypes etc |
6 | */ | 6 | */ |
7 | 7 | ||
8 | #ifdef __KERNEL__ | 8 | #ifdef __KERNEL__ |
9 | 9 | ||
10 | #include <stdarg.h> | 10 | #include <stdarg.h> |
11 | #include <linux/linkage.h> | 11 | #include <linux/linkage.h> |
12 | #include <linux/stddef.h> | 12 | #include <linux/stddef.h> |
13 | #include <linux/types.h> | 13 | #include <linux/types.h> |
14 | #include <linux/compiler.h> | 14 | #include <linux/compiler.h> |
15 | #include <linux/bitops.h> | 15 | #include <linux/bitops.h> |
16 | #include <asm/byteorder.h> | 16 | #include <asm/byteorder.h> |
17 | #include <asm/bug.h> | 17 | #include <asm/bug.h> |
18 | 18 | ||
19 | extern const char linux_banner[]; | 19 | extern const char linux_banner[]; |
20 | 20 | ||
21 | #define INT_MAX ((int)(~0U>>1)) | 21 | #define INT_MAX ((int)(~0U>>1)) |
22 | #define INT_MIN (-INT_MAX - 1) | 22 | #define INT_MIN (-INT_MAX - 1) |
23 | #define UINT_MAX (~0U) | 23 | #define UINT_MAX (~0U) |
24 | #define LONG_MAX ((long)(~0UL>>1)) | 24 | #define LONG_MAX ((long)(~0UL>>1)) |
25 | #define LONG_MIN (-LONG_MAX - 1) | 25 | #define LONG_MIN (-LONG_MAX - 1) |
26 | #define ULONG_MAX (~0UL) | 26 | #define ULONG_MAX (~0UL) |
27 | 27 | ||
28 | #define STACK_MAGIC 0xdeadbeef | 28 | #define STACK_MAGIC 0xdeadbeef |
29 | 29 | ||
30 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | 30 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
31 | #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1)) | 31 | #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1)) |
32 | 32 | ||
33 | #define KERN_EMERG "<0>" /* system is unusable */ | 33 | #define KERN_EMERG "<0>" /* system is unusable */ |
34 | #define KERN_ALERT "<1>" /* action must be taken immediately */ | 34 | #define KERN_ALERT "<1>" /* action must be taken immediately */ |
35 | #define KERN_CRIT "<2>" /* critical conditions */ | 35 | #define KERN_CRIT "<2>" /* critical conditions */ |
36 | #define KERN_ERR "<3>" /* error conditions */ | 36 | #define KERN_ERR "<3>" /* error conditions */ |
37 | #define KERN_WARNING "<4>" /* warning conditions */ | 37 | #define KERN_WARNING "<4>" /* warning conditions */ |
38 | #define KERN_NOTICE "<5>" /* normal but significant condition */ | 38 | #define KERN_NOTICE "<5>" /* normal but significant condition */ |
39 | #define KERN_INFO "<6>" /* informational */ | 39 | #define KERN_INFO "<6>" /* informational */ |
40 | #define KERN_DEBUG "<7>" /* debug-level messages */ | 40 | #define KERN_DEBUG "<7>" /* debug-level messages */ |
41 | 41 | ||
42 | extern int console_printk[]; | 42 | extern int console_printk[]; |
43 | 43 | ||
44 | #define console_loglevel (console_printk[0]) | 44 | #define console_loglevel (console_printk[0]) |
45 | #define default_message_loglevel (console_printk[1]) | 45 | #define default_message_loglevel (console_printk[1]) |
46 | #define minimum_console_loglevel (console_printk[2]) | 46 | #define minimum_console_loglevel (console_printk[2]) |
47 | #define default_console_loglevel (console_printk[3]) | 47 | #define default_console_loglevel (console_printk[3]) |
48 | 48 | ||
49 | struct completion; | 49 | struct completion; |
50 | struct pt_regs; | 50 | struct pt_regs; |
51 | struct user; | 51 | struct user; |
52 | 52 | ||
53 | /** | 53 | /** |
54 | * might_sleep - annotation for functions that can sleep | 54 | * might_sleep - annotation for functions that can sleep |
55 | * | 55 | * |
56 | * this macro will print a stack trace if it is executed in an atomic | 56 | * this macro will print a stack trace if it is executed in an atomic |
57 | * context (spinlock, irq-handler, ...). | 57 | * context (spinlock, irq-handler, ...). |
58 | * | 58 | * |
59 | * This is a useful debugging help to be able to catch problems early and not | 59 | * This is a useful debugging help to be able to catch problems early and not |
60 | * be biten later when the calling function happens to sleep when it is not | 60 | * be biten later when the calling function happens to sleep when it is not |
61 | * supposed to. | 61 | * supposed to. |
62 | */ | 62 | */ |
63 | #ifdef CONFIG_PREEMPT_VOLUNTARY | 63 | #ifdef CONFIG_PREEMPT_VOLUNTARY |
64 | extern int cond_resched(void); | 64 | extern int cond_resched(void); |
65 | # define might_resched() cond_resched() | 65 | # define might_resched() cond_resched() |
66 | #else | 66 | #else |
67 | # define might_resched() do { } while (0) | 67 | # define might_resched() do { } while (0) |
68 | #endif | 68 | #endif |
69 | 69 | ||
70 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP | 70 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
71 | void __might_sleep(char *file, int line); | 71 | void __might_sleep(char *file, int line); |
72 | # define might_sleep() \ | 72 | # define might_sleep() \ |
73 | do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0) | 73 | do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0) |
74 | #else | 74 | #else |
75 | # define might_sleep() do { might_resched(); } while (0) | 75 | # define might_sleep() do { might_resched(); } while (0) |
76 | #endif | 76 | #endif |
77 | 77 | ||
78 | #define might_sleep_if(cond) do { if (unlikely(cond)) might_sleep(); } while (0) | 78 | #define might_sleep_if(cond) do { if (unlikely(cond)) might_sleep(); } while (0) |
79 | 79 | ||
80 | #define abs(x) ({ \ | 80 | #define abs(x) ({ \ |
81 | int __x = (x); \ | 81 | int __x = (x); \ |
82 | (__x < 0) ? -__x : __x; \ | 82 | (__x < 0) ? -__x : __x; \ |
83 | }) | 83 | }) |
84 | 84 | ||
85 | #define labs(x) ({ \ | 85 | #define labs(x) ({ \ |
86 | long __x = (x); \ | 86 | long __x = (x); \ |
87 | (__x < 0) ? -__x : __x; \ | 87 | (__x < 0) ? -__x : __x; \ |
88 | }) | 88 | }) |
89 | 89 | ||
90 | extern struct atomic_notifier_head panic_notifier_list; | 90 | extern struct atomic_notifier_head panic_notifier_list; |
91 | extern long (*panic_blink)(long time); | 91 | extern long (*panic_blink)(long time); |
92 | NORET_TYPE void panic(const char * fmt, ...) | 92 | NORET_TYPE void panic(const char * fmt, ...) |
93 | __attribute__ ((NORET_AND format (printf, 1, 2))); | 93 | __attribute__ ((NORET_AND format (printf, 1, 2))); |
94 | extern void oops_enter(void); | 94 | extern void oops_enter(void); |
95 | extern void oops_exit(void); | 95 | extern void oops_exit(void); |
96 | extern int oops_may_print(void); | 96 | extern int oops_may_print(void); |
97 | fastcall NORET_TYPE void do_exit(long error_code) | 97 | fastcall NORET_TYPE void do_exit(long error_code) |
98 | ATTRIB_NORET; | 98 | ATTRIB_NORET; |
99 | NORET_TYPE void complete_and_exit(struct completion *, long) | 99 | NORET_TYPE void complete_and_exit(struct completion *, long) |
100 | ATTRIB_NORET; | 100 | ATTRIB_NORET; |
101 | extern unsigned long simple_strtoul(const char *,char **,unsigned int); | 101 | extern unsigned long simple_strtoul(const char *,char **,unsigned int); |
102 | extern long simple_strtol(const char *,char **,unsigned int); | 102 | extern long simple_strtol(const char *,char **,unsigned int); |
103 | extern unsigned long long simple_strtoull(const char *,char **,unsigned int); | 103 | extern unsigned long long simple_strtoull(const char *,char **,unsigned int); |
104 | extern long long simple_strtoll(const char *,char **,unsigned int); | 104 | extern long long simple_strtoll(const char *,char **,unsigned int); |
105 | extern int sprintf(char * buf, const char * fmt, ...) | 105 | extern int sprintf(char * buf, const char * fmt, ...) |
106 | __attribute__ ((format (printf, 2, 3))); | 106 | __attribute__ ((format (printf, 2, 3))); |
107 | extern int vsprintf(char *buf, const char *, va_list) | 107 | extern int vsprintf(char *buf, const char *, va_list) |
108 | __attribute__ ((format (printf, 2, 0))); | 108 | __attribute__ ((format (printf, 2, 0))); |
109 | extern int snprintf(char * buf, size_t size, const char * fmt, ...) | 109 | extern int snprintf(char * buf, size_t size, const char * fmt, ...) |
110 | __attribute__ ((format (printf, 3, 4))); | 110 | __attribute__ ((format (printf, 3, 4))); |
111 | extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | 111 | extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
112 | __attribute__ ((format (printf, 3, 0))); | 112 | __attribute__ ((format (printf, 3, 0))); |
113 | extern int scnprintf(char * buf, size_t size, const char * fmt, ...) | 113 | extern int scnprintf(char * buf, size_t size, const char * fmt, ...) |
114 | __attribute__ ((format (printf, 3, 4))); | 114 | __attribute__ ((format (printf, 3, 4))); |
115 | extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) | 115 | extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
116 | __attribute__ ((format (printf, 3, 0))); | 116 | __attribute__ ((format (printf, 3, 0))); |
117 | 117 | ||
118 | extern int sscanf(const char *, const char *, ...) | 118 | extern int sscanf(const char *, const char *, ...) |
119 | __attribute__ ((format (scanf, 2, 3))); | 119 | __attribute__ ((format (scanf, 2, 3))); |
120 | extern int vsscanf(const char *, const char *, va_list) | 120 | extern int vsscanf(const char *, const char *, va_list) |
121 | __attribute__ ((format (scanf, 2, 0))); | 121 | __attribute__ ((format (scanf, 2, 0))); |
122 | 122 | ||
123 | extern int get_option(char **str, int *pint); | 123 | extern int get_option(char **str, int *pint); |
124 | extern char *get_options(const char *str, int nints, int *ints); | 124 | extern char *get_options(const char *str, int nints, int *ints); |
125 | extern unsigned long long memparse(char *ptr, char **retptr); | 125 | extern unsigned long long memparse(char *ptr, char **retptr); |
126 | 126 | ||
127 | extern int core_kernel_text(unsigned long addr); | ||
127 | extern int __kernel_text_address(unsigned long addr); | 128 | extern int __kernel_text_address(unsigned long addr); |
128 | extern int kernel_text_address(unsigned long addr); | 129 | extern int kernel_text_address(unsigned long addr); |
129 | extern int session_of_pgrp(int pgrp); | 130 | extern int session_of_pgrp(int pgrp); |
130 | 131 | ||
131 | extern void dump_thread(struct pt_regs *regs, struct user *dump); | 132 | extern void dump_thread(struct pt_regs *regs, struct user *dump); |
132 | 133 | ||
133 | #ifdef CONFIG_PRINTK | 134 | #ifdef CONFIG_PRINTK |
134 | asmlinkage int vprintk(const char *fmt, va_list args) | 135 | asmlinkage int vprintk(const char *fmt, va_list args) |
135 | __attribute__ ((format (printf, 1, 0))); | 136 | __attribute__ ((format (printf, 1, 0))); |
136 | asmlinkage int printk(const char * fmt, ...) | 137 | asmlinkage int printk(const char * fmt, ...) |
137 | __attribute__ ((format (printf, 1, 2))); | 138 | __attribute__ ((format (printf, 1, 2))); |
138 | #else | 139 | #else |
139 | static inline int vprintk(const char *s, va_list args) | 140 | static inline int vprintk(const char *s, va_list args) |
140 | __attribute__ ((format (printf, 1, 0))); | 141 | __attribute__ ((format (printf, 1, 0))); |
141 | static inline int vprintk(const char *s, va_list args) { return 0; } | 142 | static inline int vprintk(const char *s, va_list args) { return 0; } |
142 | static inline int printk(const char *s, ...) | 143 | static inline int printk(const char *s, ...) |
143 | __attribute__ ((format (printf, 1, 2))); | 144 | __attribute__ ((format (printf, 1, 2))); |
144 | static inline int printk(const char *s, ...) { return 0; } | 145 | static inline int printk(const char *s, ...) { return 0; } |
145 | #endif | 146 | #endif |
146 | 147 | ||
147 | unsigned long int_sqrt(unsigned long); | 148 | unsigned long int_sqrt(unsigned long); |
148 | 149 | ||
149 | static inline int __attribute_pure__ long_log2(unsigned long x) | 150 | static inline int __attribute_pure__ long_log2(unsigned long x) |
150 | { | 151 | { |
151 | int r = 0; | 152 | int r = 0; |
152 | for (x >>= 1; x > 0; x >>= 1) | 153 | for (x >>= 1; x > 0; x >>= 1) |
153 | r++; | 154 | r++; |
154 | return r; | 155 | return r; |
155 | } | 156 | } |
156 | 157 | ||
157 | static inline unsigned long | 158 | static inline unsigned long |
158 | __attribute_const__ roundup_pow_of_two(unsigned long x) | 159 | __attribute_const__ roundup_pow_of_two(unsigned long x) |
159 | { | 160 | { |
160 | return 1UL << fls_long(x - 1); | 161 | return 1UL << fls_long(x - 1); |
161 | } | 162 | } |
162 | 163 | ||
163 | extern int printk_ratelimit(void); | 164 | extern int printk_ratelimit(void); |
164 | extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst); | 165 | extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst); |
165 | 166 | ||
166 | static inline void console_silent(void) | 167 | static inline void console_silent(void) |
167 | { | 168 | { |
168 | console_loglevel = 0; | 169 | console_loglevel = 0; |
169 | } | 170 | } |
170 | 171 | ||
171 | static inline void console_verbose(void) | 172 | static inline void console_verbose(void) |
172 | { | 173 | { |
173 | if (console_loglevel) | 174 | if (console_loglevel) |
174 | console_loglevel = 15; | 175 | console_loglevel = 15; |
175 | } | 176 | } |
176 | 177 | ||
177 | extern void bust_spinlocks(int yes); | 178 | extern void bust_spinlocks(int yes); |
178 | extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ | 179 | extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ |
179 | extern int panic_timeout; | 180 | extern int panic_timeout; |
180 | extern int panic_on_oops; | 181 | extern int panic_on_oops; |
181 | extern int tainted; | 182 | extern int tainted; |
182 | extern const char *print_tainted(void); | 183 | extern const char *print_tainted(void); |
183 | extern void add_taint(unsigned); | 184 | extern void add_taint(unsigned); |
184 | 185 | ||
185 | /* Values used for system_state */ | 186 | /* Values used for system_state */ |
186 | extern enum system_states { | 187 | extern enum system_states { |
187 | SYSTEM_BOOTING, | 188 | SYSTEM_BOOTING, |
188 | SYSTEM_RUNNING, | 189 | SYSTEM_RUNNING, |
189 | SYSTEM_HALT, | 190 | SYSTEM_HALT, |
190 | SYSTEM_POWER_OFF, | 191 | SYSTEM_POWER_OFF, |
191 | SYSTEM_RESTART, | 192 | SYSTEM_RESTART, |
192 | SYSTEM_SUSPEND_DISK, | 193 | SYSTEM_SUSPEND_DISK, |
193 | } system_state; | 194 | } system_state; |
194 | 195 | ||
195 | #define TAINT_PROPRIETARY_MODULE (1<<0) | 196 | #define TAINT_PROPRIETARY_MODULE (1<<0) |
196 | #define TAINT_FORCED_MODULE (1<<1) | 197 | #define TAINT_FORCED_MODULE (1<<1) |
197 | #define TAINT_UNSAFE_SMP (1<<2) | 198 | #define TAINT_UNSAFE_SMP (1<<2) |
198 | #define TAINT_FORCED_RMMOD (1<<3) | 199 | #define TAINT_FORCED_RMMOD (1<<3) |
199 | #define TAINT_MACHINE_CHECK (1<<4) | 200 | #define TAINT_MACHINE_CHECK (1<<4) |
200 | #define TAINT_BAD_PAGE (1<<5) | 201 | #define TAINT_BAD_PAGE (1<<5) |
201 | 202 | ||
202 | extern void dump_stack(void); | 203 | extern void dump_stack(void); |
203 | 204 | ||
204 | #ifdef DEBUG | 205 | #ifdef DEBUG |
205 | #define pr_debug(fmt,arg...) \ | 206 | #define pr_debug(fmt,arg...) \ |
206 | printk(KERN_DEBUG fmt,##arg) | 207 | printk(KERN_DEBUG fmt,##arg) |
207 | #else | 208 | #else |
208 | #define pr_debug(fmt,arg...) \ | 209 | #define pr_debug(fmt,arg...) \ |
209 | do { } while (0) | 210 | do { } while (0) |
210 | #endif | 211 | #endif |
211 | 212 | ||
212 | #define pr_info(fmt,arg...) \ | 213 | #define pr_info(fmt,arg...) \ |
213 | printk(KERN_INFO fmt,##arg) | 214 | printk(KERN_INFO fmt,##arg) |
214 | 215 | ||
215 | /* | 216 | /* |
216 | * Display an IP address in readable format. | 217 | * Display an IP address in readable format. |
217 | */ | 218 | */ |
218 | 219 | ||
219 | #define NIPQUAD(addr) \ | 220 | #define NIPQUAD(addr) \ |
220 | ((unsigned char *)&addr)[0], \ | 221 | ((unsigned char *)&addr)[0], \ |
221 | ((unsigned char *)&addr)[1], \ | 222 | ((unsigned char *)&addr)[1], \ |
222 | ((unsigned char *)&addr)[2], \ | 223 | ((unsigned char *)&addr)[2], \ |
223 | ((unsigned char *)&addr)[3] | 224 | ((unsigned char *)&addr)[3] |
224 | #define NIPQUAD_FMT "%u.%u.%u.%u" | 225 | #define NIPQUAD_FMT "%u.%u.%u.%u" |
225 | 226 | ||
226 | #define NIP6(addr) \ | 227 | #define NIP6(addr) \ |
227 | ntohs((addr).s6_addr16[0]), \ | 228 | ntohs((addr).s6_addr16[0]), \ |
228 | ntohs((addr).s6_addr16[1]), \ | 229 | ntohs((addr).s6_addr16[1]), \ |
229 | ntohs((addr).s6_addr16[2]), \ | 230 | ntohs((addr).s6_addr16[2]), \ |
230 | ntohs((addr).s6_addr16[3]), \ | 231 | ntohs((addr).s6_addr16[3]), \ |
231 | ntohs((addr).s6_addr16[4]), \ | 232 | ntohs((addr).s6_addr16[4]), \ |
232 | ntohs((addr).s6_addr16[5]), \ | 233 | ntohs((addr).s6_addr16[5]), \ |
233 | ntohs((addr).s6_addr16[6]), \ | 234 | ntohs((addr).s6_addr16[6]), \ |
234 | ntohs((addr).s6_addr16[7]) | 235 | ntohs((addr).s6_addr16[7]) |
235 | #define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" | 236 | #define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" |
236 | #define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x" | 237 | #define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x" |
237 | 238 | ||
238 | #if defined(__LITTLE_ENDIAN) | 239 | #if defined(__LITTLE_ENDIAN) |
239 | #define HIPQUAD(addr) \ | 240 | #define HIPQUAD(addr) \ |
240 | ((unsigned char *)&addr)[3], \ | 241 | ((unsigned char *)&addr)[3], \ |
241 | ((unsigned char *)&addr)[2], \ | 242 | ((unsigned char *)&addr)[2], \ |
242 | ((unsigned char *)&addr)[1], \ | 243 | ((unsigned char *)&addr)[1], \ |
243 | ((unsigned char *)&addr)[0] | 244 | ((unsigned char *)&addr)[0] |
244 | #elif defined(__BIG_ENDIAN) | 245 | #elif defined(__BIG_ENDIAN) |
245 | #define HIPQUAD NIPQUAD | 246 | #define HIPQUAD NIPQUAD |
246 | #else | 247 | #else |
247 | #error "Please fix asm/byteorder.h" | 248 | #error "Please fix asm/byteorder.h" |
248 | #endif /* __LITTLE_ENDIAN */ | 249 | #endif /* __LITTLE_ENDIAN */ |
249 | 250 | ||
250 | /* | 251 | /* |
251 | * min()/max() macros that also do | 252 | * min()/max() macros that also do |
252 | * strict type-checking.. See the | 253 | * strict type-checking.. See the |
253 | * "unnecessary" pointer comparison. | 254 | * "unnecessary" pointer comparison. |
254 | */ | 255 | */ |
255 | #define min(x,y) ({ \ | 256 | #define min(x,y) ({ \ |
256 | typeof(x) _x = (x); \ | 257 | typeof(x) _x = (x); \ |
257 | typeof(y) _y = (y); \ | 258 | typeof(y) _y = (y); \ |
258 | (void) (&_x == &_y); \ | 259 | (void) (&_x == &_y); \ |
259 | _x < _y ? _x : _y; }) | 260 | _x < _y ? _x : _y; }) |
260 | 261 | ||
261 | #define max(x,y) ({ \ | 262 | #define max(x,y) ({ \ |
262 | typeof(x) _x = (x); \ | 263 | typeof(x) _x = (x); \ |
263 | typeof(y) _y = (y); \ | 264 | typeof(y) _y = (y); \ |
264 | (void) (&_x == &_y); \ | 265 | (void) (&_x == &_y); \ |
265 | _x > _y ? _x : _y; }) | 266 | _x > _y ? _x : _y; }) |
266 | 267 | ||
267 | /* | 268 | /* |
268 | * ..and if you can't take the strict | 269 | * ..and if you can't take the strict |
269 | * types, you can specify one yourself. | 270 | * types, you can specify one yourself. |
270 | * | 271 | * |
271 | * Or not use min/max at all, of course. | 272 | * Or not use min/max at all, of course. |
272 | */ | 273 | */ |
273 | #define min_t(type,x,y) \ | 274 | #define min_t(type,x,y) \ |
274 | ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; }) | 275 | ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; }) |
275 | #define max_t(type,x,y) \ | 276 | #define max_t(type,x,y) \ |
276 | ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; }) | 277 | ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; }) |
277 | 278 | ||
278 | 279 | ||
279 | /** | 280 | /** |
280 | * container_of - cast a member of a structure out to the containing structure | 281 | * container_of - cast a member of a structure out to the containing structure |
281 | * @ptr: the pointer to the member. | 282 | * @ptr: the pointer to the member. |
282 | * @type: the type of the container struct this is embedded in. | 283 | * @type: the type of the container struct this is embedded in. |
283 | * @member: the name of the member within the struct. | 284 | * @member: the name of the member within the struct. |
284 | * | 285 | * |
285 | */ | 286 | */ |
286 | #define container_of(ptr, type, member) ({ \ | 287 | #define container_of(ptr, type, member) ({ \ |
287 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | 288 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
288 | (type *)( (char *)__mptr - offsetof(type,member) );}) | 289 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
289 | 290 | ||
290 | /* | 291 | /* |
291 | * Check at compile time that something is of a particular type. | 292 | * Check at compile time that something is of a particular type. |
292 | * Always evaluates to 1 so you may use it easily in comparisons. | 293 | * Always evaluates to 1 so you may use it easily in comparisons. |
293 | */ | 294 | */ |
294 | #define typecheck(type,x) \ | 295 | #define typecheck(type,x) \ |
295 | ({ type __dummy; \ | 296 | ({ type __dummy; \ |
296 | typeof(x) __dummy2; \ | 297 | typeof(x) __dummy2; \ |
297 | (void)(&__dummy == &__dummy2); \ | 298 | (void)(&__dummy == &__dummy2); \ |
298 | 1; \ | 299 | 1; \ |
299 | }) | 300 | }) |
300 | 301 | ||
301 | /* | 302 | /* |
302 | * Check at compile time that 'function' is a certain type, or is a pointer | 303 | * Check at compile time that 'function' is a certain type, or is a pointer |
303 | * to that type (needs to use typedef for the function type.) | 304 | * to that type (needs to use typedef for the function type.) |
304 | */ | 305 | */ |
305 | #define typecheck_fn(type,function) \ | 306 | #define typecheck_fn(type,function) \ |
306 | ({ typeof(type) __tmp = function; \ | 307 | ({ typeof(type) __tmp = function; \ |
307 | (void)__tmp; \ | 308 | (void)__tmp; \ |
308 | }) | 309 | }) |
309 | 310 | ||
310 | #endif /* __KERNEL__ */ | 311 | #endif /* __KERNEL__ */ |
311 | 312 | ||
312 | #define SI_LOAD_SHIFT 16 | 313 | #define SI_LOAD_SHIFT 16 |
313 | struct sysinfo { | 314 | struct sysinfo { |
314 | long uptime; /* Seconds since boot */ | 315 | long uptime; /* Seconds since boot */ |
315 | unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ | 316 | unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ |
316 | unsigned long totalram; /* Total usable main memory size */ | 317 | unsigned long totalram; /* Total usable main memory size */ |
317 | unsigned long freeram; /* Available memory size */ | 318 | unsigned long freeram; /* Available memory size */ |
318 | unsigned long sharedram; /* Amount of shared memory */ | 319 | unsigned long sharedram; /* Amount of shared memory */ |
319 | unsigned long bufferram; /* Memory used by buffers */ | 320 | unsigned long bufferram; /* Memory used by buffers */ |
320 | unsigned long totalswap; /* Total swap space size */ | 321 | unsigned long totalswap; /* Total swap space size */ |
321 | unsigned long freeswap; /* swap space still available */ | 322 | unsigned long freeswap; /* swap space still available */ |
322 | unsigned short procs; /* Number of current processes */ | 323 | unsigned short procs; /* Number of current processes */ |
323 | unsigned short pad; /* explicit padding for m68k */ | 324 | unsigned short pad; /* explicit padding for m68k */ |
324 | unsigned long totalhigh; /* Total high memory size */ | 325 | unsigned long totalhigh; /* Total high memory size */ |
325 | unsigned long freehigh; /* Available high memory size */ | 326 | unsigned long freehigh; /* Available high memory size */ |
326 | unsigned int mem_unit; /* Memory unit size in bytes */ | 327 | unsigned int mem_unit; /* Memory unit size in bytes */ |
327 | char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ | 328 | char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ |
328 | }; | 329 | }; |
329 | 330 | ||
330 | /* Force a compilation error if condition is true */ | 331 | /* Force a compilation error if condition is true */ |
331 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) | 332 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
332 | 333 | ||
333 | /* Trap pasters of __FUNCTION__ at compile-time */ | 334 | /* Trap pasters of __FUNCTION__ at compile-time */ |
334 | #define __FUNCTION__ (__func__) | 335 | #define __FUNCTION__ (__func__) |
335 | 336 | ||
336 | #endif | 337 | #endif |
337 | 338 |
kernel/extable.c
1 | /* Rewritten by Rusty Russell, on the backs of many others... | 1 | /* Rewritten by Rusty Russell, on the backs of many others... |
2 | Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM. | 2 | Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM. |
3 | 3 | ||
4 | This program is free software; you can redistribute it and/or modify | 4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by | 5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or | 6 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. | 7 | (at your option) any later version. |
8 | 8 | ||
9 | This program is distributed in the hope that it will be useful, | 9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. | 12 | GNU General Public License for more details. |
13 | 13 | ||
14 | You should have received a copy of the GNU General Public License | 14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software | 15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | */ | 17 | */ |
18 | #include <linux/module.h> | 18 | #include <linux/module.h> |
19 | #include <linux/init.h> | 19 | #include <linux/init.h> |
20 | #include <asm/uaccess.h> | 20 | #include <asm/uaccess.h> |
21 | #include <asm/sections.h> | 21 | #include <asm/sections.h> |
22 | 22 | ||
23 | extern struct exception_table_entry __start___ex_table[]; | 23 | extern struct exception_table_entry __start___ex_table[]; |
24 | extern struct exception_table_entry __stop___ex_table[]; | 24 | extern struct exception_table_entry __stop___ex_table[]; |
25 | 25 | ||
26 | /* Sort the kernel's built-in exception table */ | 26 | /* Sort the kernel's built-in exception table */ |
27 | void __init sort_main_extable(void) | 27 | void __init sort_main_extable(void) |
28 | { | 28 | { |
29 | sort_extable(__start___ex_table, __stop___ex_table); | 29 | sort_extable(__start___ex_table, __stop___ex_table); |
30 | } | 30 | } |
31 | 31 | ||
32 | /* Given an address, look for it in the exception tables. */ | 32 | /* Given an address, look for it in the exception tables. */ |
33 | const struct exception_table_entry *search_exception_tables(unsigned long addr) | 33 | const struct exception_table_entry *search_exception_tables(unsigned long addr) |
34 | { | 34 | { |
35 | const struct exception_table_entry *e; | 35 | const struct exception_table_entry *e; |
36 | 36 | ||
37 | e = search_extable(__start___ex_table, __stop___ex_table-1, addr); | 37 | e = search_extable(__start___ex_table, __stop___ex_table-1, addr); |
38 | if (!e) | 38 | if (!e) |
39 | e = search_module_extables(addr); | 39 | e = search_module_extables(addr); |
40 | return e; | 40 | return e; |
41 | } | 41 | } |
42 | 42 | ||
43 | static int core_kernel_text(unsigned long addr) | 43 | int core_kernel_text(unsigned long addr) |
44 | { | 44 | { |
45 | if (addr >= (unsigned long)_stext && | 45 | if (addr >= (unsigned long)_stext && |
46 | addr <= (unsigned long)_etext) | 46 | addr <= (unsigned long)_etext) |
47 | return 1; | 47 | return 1; |
48 | 48 | ||
49 | if (addr >= (unsigned long)_sinittext && | 49 | if (addr >= (unsigned long)_sinittext && |
50 | addr <= (unsigned long)_einittext) | 50 | addr <= (unsigned long)_einittext) |
51 | return 1; | 51 | return 1; |
52 | return 0; | 52 | return 0; |
53 | } | 53 | } |
54 | 54 | ||
55 | int __kernel_text_address(unsigned long addr) | 55 | int __kernel_text_address(unsigned long addr) |
56 | { | 56 | { |
57 | if (core_kernel_text(addr)) | 57 | if (core_kernel_text(addr)) |
58 | return 1; | 58 | return 1; |
59 | return __module_text_address(addr) != NULL; | 59 | return __module_text_address(addr) != NULL; |
60 | } | 60 | } |
61 | 61 | ||
62 | int kernel_text_address(unsigned long addr) | 62 | int kernel_text_address(unsigned long addr) |
63 | { | 63 | { |
64 | if (core_kernel_text(addr)) | 64 | if (core_kernel_text(addr)) |
65 | return 1; | 65 | return 1; |
66 | return module_text_address(addr) != NULL; | 66 | return module_text_address(addr) != NULL; |
67 | } | 67 | } |
68 | 68 |
kernel/module.c
1 | /* Rewritten by Rusty Russell, on the backs of many others... | 1 | /* Rewritten by Rusty Russell, on the backs of many others... |
2 | Copyright (C) 2002 Richard Henderson | 2 | Copyright (C) 2002 Richard Henderson |
3 | Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM. | 3 | Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM. |
4 | 4 | ||
5 | This program is free software; you can redistribute it and/or modify | 5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by | 6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or | 7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. | 8 | (at your option) any later version. |
9 | 9 | ||
10 | This program is distributed in the hope that it will be useful, | 10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. | 13 | GNU General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software | 16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ | 18 | */ |
19 | #include <linux/config.h> | 19 | #include <linux/config.h> |
20 | #include <linux/module.h> | 20 | #include <linux/module.h> |
21 | #include <linux/moduleloader.h> | 21 | #include <linux/moduleloader.h> |
22 | #include <linux/init.h> | 22 | #include <linux/init.h> |
23 | #include <linux/kernel.h> | 23 | #include <linux/kernel.h> |
24 | #include <linux/slab.h> | 24 | #include <linux/slab.h> |
25 | #include <linux/vmalloc.h> | 25 | #include <linux/vmalloc.h> |
26 | #include <linux/elf.h> | 26 | #include <linux/elf.h> |
27 | #include <linux/seq_file.h> | 27 | #include <linux/seq_file.h> |
28 | #include <linux/syscalls.h> | 28 | #include <linux/syscalls.h> |
29 | #include <linux/fcntl.h> | 29 | #include <linux/fcntl.h> |
30 | #include <linux/rcupdate.h> | 30 | #include <linux/rcupdate.h> |
31 | #include <linux/capability.h> | 31 | #include <linux/capability.h> |
32 | #include <linux/cpu.h> | 32 | #include <linux/cpu.h> |
33 | #include <linux/moduleparam.h> | 33 | #include <linux/moduleparam.h> |
34 | #include <linux/errno.h> | 34 | #include <linux/errno.h> |
35 | #include <linux/err.h> | 35 | #include <linux/err.h> |
36 | #include <linux/vermagic.h> | 36 | #include <linux/vermagic.h> |
37 | #include <linux/notifier.h> | 37 | #include <linux/notifier.h> |
38 | #include <linux/stop_machine.h> | 38 | #include <linux/stop_machine.h> |
39 | #include <linux/device.h> | 39 | #include <linux/device.h> |
40 | #include <linux/string.h> | 40 | #include <linux/string.h> |
41 | #include <linux/sched.h> | 41 | #include <linux/sched.h> |
42 | #include <linux/mutex.h> | 42 | #include <linux/mutex.h> |
43 | #include <asm/uaccess.h> | 43 | #include <asm/uaccess.h> |
44 | #include <asm/semaphore.h> | 44 | #include <asm/semaphore.h> |
45 | #include <asm/cacheflush.h> | 45 | #include <asm/cacheflush.h> |
46 | 46 | ||
47 | #if 0 | 47 | #if 0 |
48 | #define DEBUGP printk | 48 | #define DEBUGP printk |
49 | #else | 49 | #else |
50 | #define DEBUGP(fmt , a...) | 50 | #define DEBUGP(fmt , a...) |
51 | #endif | 51 | #endif |
52 | 52 | ||
53 | #ifndef ARCH_SHF_SMALL | 53 | #ifndef ARCH_SHF_SMALL |
54 | #define ARCH_SHF_SMALL 0 | 54 | #define ARCH_SHF_SMALL 0 |
55 | #endif | 55 | #endif |
56 | 56 | ||
57 | /* If this is set, the section belongs in the init part of the module */ | 57 | /* If this is set, the section belongs in the init part of the module */ |
58 | #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) | 58 | #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) |
59 | 59 | ||
60 | /* Protects module list */ | 60 | /* Protects module list */ |
61 | static DEFINE_SPINLOCK(modlist_lock); | 61 | static DEFINE_SPINLOCK(modlist_lock); |
62 | 62 | ||
63 | /* List of modules, protected by module_mutex AND modlist_lock */ | 63 | /* List of modules, protected by module_mutex AND modlist_lock */ |
64 | static DEFINE_MUTEX(module_mutex); | 64 | static DEFINE_MUTEX(module_mutex); |
65 | static LIST_HEAD(modules); | 65 | static LIST_HEAD(modules); |
66 | 66 | ||
67 | static BLOCKING_NOTIFIER_HEAD(module_notify_list); | 67 | static BLOCKING_NOTIFIER_HEAD(module_notify_list); |
68 | 68 | ||
69 | int register_module_notifier(struct notifier_block * nb) | 69 | int register_module_notifier(struct notifier_block * nb) |
70 | { | 70 | { |
71 | return blocking_notifier_chain_register(&module_notify_list, nb); | 71 | return blocking_notifier_chain_register(&module_notify_list, nb); |
72 | } | 72 | } |
73 | EXPORT_SYMBOL(register_module_notifier); | 73 | EXPORT_SYMBOL(register_module_notifier); |
74 | 74 | ||
75 | int unregister_module_notifier(struct notifier_block * nb) | 75 | int unregister_module_notifier(struct notifier_block * nb) |
76 | { | 76 | { |
77 | return blocking_notifier_chain_unregister(&module_notify_list, nb); | 77 | return blocking_notifier_chain_unregister(&module_notify_list, nb); |
78 | } | 78 | } |
79 | EXPORT_SYMBOL(unregister_module_notifier); | 79 | EXPORT_SYMBOL(unregister_module_notifier); |
80 | 80 | ||
81 | /* We require a truly strong try_module_get() */ | 81 | /* We require a truly strong try_module_get() */ |
82 | static inline int strong_try_module_get(struct module *mod) | 82 | static inline int strong_try_module_get(struct module *mod) |
83 | { | 83 | { |
84 | if (mod && mod->state == MODULE_STATE_COMING) | 84 | if (mod && mod->state == MODULE_STATE_COMING) |
85 | return 0; | 85 | return 0; |
86 | return try_module_get(mod); | 86 | return try_module_get(mod); |
87 | } | 87 | } |
88 | 88 | ||
89 | /* A thread that wants to hold a reference to a module only while it | 89 | /* A thread that wants to hold a reference to a module only while it |
90 | * is running can call ths to safely exit. | 90 | * is running can call ths to safely exit. |
91 | * nfsd and lockd use this. | 91 | * nfsd and lockd use this. |
92 | */ | 92 | */ |
93 | void __module_put_and_exit(struct module *mod, long code) | 93 | void __module_put_and_exit(struct module *mod, long code) |
94 | { | 94 | { |
95 | module_put(mod); | 95 | module_put(mod); |
96 | do_exit(code); | 96 | do_exit(code); |
97 | } | 97 | } |
98 | EXPORT_SYMBOL(__module_put_and_exit); | 98 | EXPORT_SYMBOL(__module_put_and_exit); |
99 | 99 | ||
100 | /* Find a module section: 0 means not found. */ | 100 | /* Find a module section: 0 means not found. */ |
101 | static unsigned int find_sec(Elf_Ehdr *hdr, | 101 | static unsigned int find_sec(Elf_Ehdr *hdr, |
102 | Elf_Shdr *sechdrs, | 102 | Elf_Shdr *sechdrs, |
103 | const char *secstrings, | 103 | const char *secstrings, |
104 | const char *name) | 104 | const char *name) |
105 | { | 105 | { |
106 | unsigned int i; | 106 | unsigned int i; |
107 | 107 | ||
108 | for (i = 1; i < hdr->e_shnum; i++) | 108 | for (i = 1; i < hdr->e_shnum; i++) |
109 | /* Alloc bit cleared means "ignore it." */ | 109 | /* Alloc bit cleared means "ignore it." */ |
110 | if ((sechdrs[i].sh_flags & SHF_ALLOC) | 110 | if ((sechdrs[i].sh_flags & SHF_ALLOC) |
111 | && strcmp(secstrings+sechdrs[i].sh_name, name) == 0) | 111 | && strcmp(secstrings+sechdrs[i].sh_name, name) == 0) |
112 | return i; | 112 | return i; |
113 | return 0; | 113 | return 0; |
114 | } | 114 | } |
115 | 115 | ||
116 | /* Provided by the linker */ | 116 | /* Provided by the linker */ |
117 | extern const struct kernel_symbol __start___ksymtab[]; | 117 | extern const struct kernel_symbol __start___ksymtab[]; |
118 | extern const struct kernel_symbol __stop___ksymtab[]; | 118 | extern const struct kernel_symbol __stop___ksymtab[]; |
119 | extern const struct kernel_symbol __start___ksymtab_gpl[]; | 119 | extern const struct kernel_symbol __start___ksymtab_gpl[]; |
120 | extern const struct kernel_symbol __stop___ksymtab_gpl[]; | 120 | extern const struct kernel_symbol __stop___ksymtab_gpl[]; |
121 | extern const struct kernel_symbol __start___ksymtab_gpl_future[]; | 121 | extern const struct kernel_symbol __start___ksymtab_gpl_future[]; |
122 | extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; | 122 | extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; |
123 | extern const unsigned long __start___kcrctab[]; | 123 | extern const unsigned long __start___kcrctab[]; |
124 | extern const unsigned long __start___kcrctab_gpl[]; | 124 | extern const unsigned long __start___kcrctab_gpl[]; |
125 | extern const unsigned long __start___kcrctab_gpl_future[]; | 125 | extern const unsigned long __start___kcrctab_gpl_future[]; |
126 | 126 | ||
127 | #ifndef CONFIG_MODVERSIONS | 127 | #ifndef CONFIG_MODVERSIONS |
128 | #define symversion(base, idx) NULL | 128 | #define symversion(base, idx) NULL |
129 | #else | 129 | #else |
130 | #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) | 130 | #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) |
131 | #endif | 131 | #endif |
132 | 132 | ||
133 | /* lookup symbol in given range of kernel_symbols */ | 133 | /* lookup symbol in given range of kernel_symbols */ |
134 | static const struct kernel_symbol *lookup_symbol(const char *name, | 134 | static const struct kernel_symbol *lookup_symbol(const char *name, |
135 | const struct kernel_symbol *start, | 135 | const struct kernel_symbol *start, |
136 | const struct kernel_symbol *stop) | 136 | const struct kernel_symbol *stop) |
137 | { | 137 | { |
138 | const struct kernel_symbol *ks = start; | 138 | const struct kernel_symbol *ks = start; |
139 | for (; ks < stop; ks++) | 139 | for (; ks < stop; ks++) |
140 | if (strcmp(ks->name, name) == 0) | 140 | if (strcmp(ks->name, name) == 0) |
141 | return ks; | 141 | return ks; |
142 | return NULL; | 142 | return NULL; |
143 | } | 143 | } |
144 | 144 | ||
145 | /* Find a symbol, return value, crc and module which owns it */ | 145 | /* Find a symbol, return value, crc and module which owns it */ |
146 | static unsigned long __find_symbol(const char *name, | 146 | static unsigned long __find_symbol(const char *name, |
147 | struct module **owner, | 147 | struct module **owner, |
148 | const unsigned long **crc, | 148 | const unsigned long **crc, |
149 | int gplok) | 149 | int gplok) |
150 | { | 150 | { |
151 | struct module *mod; | 151 | struct module *mod; |
152 | const struct kernel_symbol *ks; | 152 | const struct kernel_symbol *ks; |
153 | 153 | ||
154 | /* Core kernel first. */ | 154 | /* Core kernel first. */ |
155 | *owner = NULL; | 155 | *owner = NULL; |
156 | ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab); | 156 | ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab); |
157 | if (ks) { | 157 | if (ks) { |
158 | *crc = symversion(__start___kcrctab, (ks - __start___ksymtab)); | 158 | *crc = symversion(__start___kcrctab, (ks - __start___ksymtab)); |
159 | return ks->value; | 159 | return ks->value; |
160 | } | 160 | } |
161 | if (gplok) { | 161 | if (gplok) { |
162 | ks = lookup_symbol(name, __start___ksymtab_gpl, | 162 | ks = lookup_symbol(name, __start___ksymtab_gpl, |
163 | __stop___ksymtab_gpl); | 163 | __stop___ksymtab_gpl); |
164 | if (ks) { | 164 | if (ks) { |
165 | *crc = symversion(__start___kcrctab_gpl, | 165 | *crc = symversion(__start___kcrctab_gpl, |
166 | (ks - __start___ksymtab_gpl)); | 166 | (ks - __start___ksymtab_gpl)); |
167 | return ks->value; | 167 | return ks->value; |
168 | } | 168 | } |
169 | } | 169 | } |
170 | ks = lookup_symbol(name, __start___ksymtab_gpl_future, | 170 | ks = lookup_symbol(name, __start___ksymtab_gpl_future, |
171 | __stop___ksymtab_gpl_future); | 171 | __stop___ksymtab_gpl_future); |
172 | if (ks) { | 172 | if (ks) { |
173 | if (!gplok) { | 173 | if (!gplok) { |
174 | printk(KERN_WARNING "Symbol %s is being used " | 174 | printk(KERN_WARNING "Symbol %s is being used " |
175 | "by a non-GPL module, which will not " | 175 | "by a non-GPL module, which will not " |
176 | "be allowed in the future\n", name); | 176 | "be allowed in the future\n", name); |
177 | printk(KERN_WARNING "Please see the file " | 177 | printk(KERN_WARNING "Please see the file " |
178 | "Documentation/feature-removal-schedule.txt " | 178 | "Documentation/feature-removal-schedule.txt " |
179 | "in the kernel source tree for more " | 179 | "in the kernel source tree for more " |
180 | "details.\n"); | 180 | "details.\n"); |
181 | } | 181 | } |
182 | *crc = symversion(__start___kcrctab_gpl_future, | 182 | *crc = symversion(__start___kcrctab_gpl_future, |
183 | (ks - __start___ksymtab_gpl_future)); | 183 | (ks - __start___ksymtab_gpl_future)); |
184 | return ks->value; | 184 | return ks->value; |
185 | } | 185 | } |
186 | 186 | ||
187 | /* Now try modules. */ | 187 | /* Now try modules. */ |
188 | list_for_each_entry(mod, &modules, list) { | 188 | list_for_each_entry(mod, &modules, list) { |
189 | *owner = mod; | 189 | *owner = mod; |
190 | ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms); | 190 | ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms); |
191 | if (ks) { | 191 | if (ks) { |
192 | *crc = symversion(mod->crcs, (ks - mod->syms)); | 192 | *crc = symversion(mod->crcs, (ks - mod->syms)); |
193 | return ks->value; | 193 | return ks->value; |
194 | } | 194 | } |
195 | 195 | ||
196 | if (gplok) { | 196 | if (gplok) { |
197 | ks = lookup_symbol(name, mod->gpl_syms, | 197 | ks = lookup_symbol(name, mod->gpl_syms, |
198 | mod->gpl_syms + mod->num_gpl_syms); | 198 | mod->gpl_syms + mod->num_gpl_syms); |
199 | if (ks) { | 199 | if (ks) { |
200 | *crc = symversion(mod->gpl_crcs, | 200 | *crc = symversion(mod->gpl_crcs, |
201 | (ks - mod->gpl_syms)); | 201 | (ks - mod->gpl_syms)); |
202 | return ks->value; | 202 | return ks->value; |
203 | } | 203 | } |
204 | } | 204 | } |
205 | ks = lookup_symbol(name, mod->gpl_future_syms, | 205 | ks = lookup_symbol(name, mod->gpl_future_syms, |
206 | (mod->gpl_future_syms + | 206 | (mod->gpl_future_syms + |
207 | mod->num_gpl_future_syms)); | 207 | mod->num_gpl_future_syms)); |
208 | if (ks) { | 208 | if (ks) { |
209 | if (!gplok) { | 209 | if (!gplok) { |
210 | printk(KERN_WARNING "Symbol %s is being used " | 210 | printk(KERN_WARNING "Symbol %s is being used " |
211 | "by a non-GPL module, which will not " | 211 | "by a non-GPL module, which will not " |
212 | "be allowed in the future\n", name); | 212 | "be allowed in the future\n", name); |
213 | printk(KERN_WARNING "Please see the file " | 213 | printk(KERN_WARNING "Please see the file " |
214 | "Documentation/feature-removal-schedule.txt " | 214 | "Documentation/feature-removal-schedule.txt " |
215 | "in the kernel source tree for more " | 215 | "in the kernel source tree for more " |
216 | "details.\n"); | 216 | "details.\n"); |
217 | } | 217 | } |
218 | *crc = symversion(mod->gpl_future_crcs, | 218 | *crc = symversion(mod->gpl_future_crcs, |
219 | (ks - mod->gpl_future_syms)); | 219 | (ks - mod->gpl_future_syms)); |
220 | return ks->value; | 220 | return ks->value; |
221 | } | 221 | } |
222 | } | 222 | } |
223 | DEBUGP("Failed to find symbol %s\n", name); | 223 | DEBUGP("Failed to find symbol %s\n", name); |
224 | return 0; | 224 | return 0; |
225 | } | 225 | } |
226 | 226 | ||
227 | /* Search for module by name: must hold module_mutex. */ | 227 | /* Search for module by name: must hold module_mutex. */ |
228 | static struct module *find_module(const char *name) | 228 | static struct module *find_module(const char *name) |
229 | { | 229 | { |
230 | struct module *mod; | 230 | struct module *mod; |
231 | 231 | ||
232 | list_for_each_entry(mod, &modules, list) { | 232 | list_for_each_entry(mod, &modules, list) { |
233 | if (strcmp(mod->name, name) == 0) | 233 | if (strcmp(mod->name, name) == 0) |
234 | return mod; | 234 | return mod; |
235 | } | 235 | } |
236 | return NULL; | 236 | return NULL; |
237 | } | 237 | } |
238 | 238 | ||
239 | #ifdef CONFIG_SMP | 239 | #ifdef CONFIG_SMP |
240 | /* Number of blocks used and allocated. */ | 240 | /* Number of blocks used and allocated. */ |
241 | static unsigned int pcpu_num_used, pcpu_num_allocated; | 241 | static unsigned int pcpu_num_used, pcpu_num_allocated; |
242 | /* Size of each block. -ve means used. */ | 242 | /* Size of each block. -ve means used. */ |
243 | static int *pcpu_size; | 243 | static int *pcpu_size; |
244 | 244 | ||
245 | static int split_block(unsigned int i, unsigned short size) | 245 | static int split_block(unsigned int i, unsigned short size) |
246 | { | 246 | { |
247 | /* Reallocation required? */ | 247 | /* Reallocation required? */ |
248 | if (pcpu_num_used + 1 > pcpu_num_allocated) { | 248 | if (pcpu_num_used + 1 > pcpu_num_allocated) { |
249 | int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2, | 249 | int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2, |
250 | GFP_KERNEL); | 250 | GFP_KERNEL); |
251 | if (!new) | 251 | if (!new) |
252 | return 0; | 252 | return 0; |
253 | 253 | ||
254 | memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated); | 254 | memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated); |
255 | pcpu_num_allocated *= 2; | 255 | pcpu_num_allocated *= 2; |
256 | kfree(pcpu_size); | 256 | kfree(pcpu_size); |
257 | pcpu_size = new; | 257 | pcpu_size = new; |
258 | } | 258 | } |
259 | 259 | ||
260 | /* Insert a new subblock */ | 260 | /* Insert a new subblock */ |
261 | memmove(&pcpu_size[i+1], &pcpu_size[i], | 261 | memmove(&pcpu_size[i+1], &pcpu_size[i], |
262 | sizeof(pcpu_size[0]) * (pcpu_num_used - i)); | 262 | sizeof(pcpu_size[0]) * (pcpu_num_used - i)); |
263 | pcpu_num_used++; | 263 | pcpu_num_used++; |
264 | 264 | ||
265 | pcpu_size[i+1] -= size; | 265 | pcpu_size[i+1] -= size; |
266 | pcpu_size[i] = size; | 266 | pcpu_size[i] = size; |
267 | return 1; | 267 | return 1; |
268 | } | 268 | } |
269 | 269 | ||
270 | static inline unsigned int block_size(int val) | 270 | static inline unsigned int block_size(int val) |
271 | { | 271 | { |
272 | if (val < 0) | 272 | if (val < 0) |
273 | return -val; | 273 | return -val; |
274 | return val; | 274 | return val; |
275 | } | 275 | } |
276 | 276 | ||
277 | /* Created by linker magic */ | 277 | /* Created by linker magic */ |
278 | extern char __per_cpu_start[], __per_cpu_end[]; | 278 | extern char __per_cpu_start[], __per_cpu_end[]; |
279 | 279 | ||
280 | static void *percpu_modalloc(unsigned long size, unsigned long align, | 280 | static void *percpu_modalloc(unsigned long size, unsigned long align, |
281 | const char *name) | 281 | const char *name) |
282 | { | 282 | { |
283 | unsigned long extra; | 283 | unsigned long extra; |
284 | unsigned int i; | 284 | unsigned int i; |
285 | void *ptr; | 285 | void *ptr; |
286 | 286 | ||
287 | if (align > SMP_CACHE_BYTES) { | 287 | if (align > SMP_CACHE_BYTES) { |
288 | printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n", | 288 | printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n", |
289 | name, align, SMP_CACHE_BYTES); | 289 | name, align, SMP_CACHE_BYTES); |
290 | align = SMP_CACHE_BYTES; | 290 | align = SMP_CACHE_BYTES; |
291 | } | 291 | } |
292 | 292 | ||
293 | ptr = __per_cpu_start; | 293 | ptr = __per_cpu_start; |
294 | for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) { | 294 | for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) { |
295 | /* Extra for alignment requirement. */ | 295 | /* Extra for alignment requirement. */ |
296 | extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr; | 296 | extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr; |
297 | BUG_ON(i == 0 && extra != 0); | 297 | BUG_ON(i == 0 && extra != 0); |
298 | 298 | ||
299 | if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size) | 299 | if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size) |
300 | continue; | 300 | continue; |
301 | 301 | ||
302 | /* Transfer extra to previous block. */ | 302 | /* Transfer extra to previous block. */ |
303 | if (pcpu_size[i-1] < 0) | 303 | if (pcpu_size[i-1] < 0) |
304 | pcpu_size[i-1] -= extra; | 304 | pcpu_size[i-1] -= extra; |
305 | else | 305 | else |
306 | pcpu_size[i-1] += extra; | 306 | pcpu_size[i-1] += extra; |
307 | pcpu_size[i] -= extra; | 307 | pcpu_size[i] -= extra; |
308 | ptr += extra; | 308 | ptr += extra; |
309 | 309 | ||
310 | /* Split block if warranted */ | 310 | /* Split block if warranted */ |
311 | if (pcpu_size[i] - size > sizeof(unsigned long)) | 311 | if (pcpu_size[i] - size > sizeof(unsigned long)) |
312 | if (!split_block(i, size)) | 312 | if (!split_block(i, size)) |
313 | return NULL; | 313 | return NULL; |
314 | 314 | ||
315 | /* Mark allocated */ | 315 | /* Mark allocated */ |
316 | pcpu_size[i] = -pcpu_size[i]; | 316 | pcpu_size[i] = -pcpu_size[i]; |
317 | return ptr; | 317 | return ptr; |
318 | } | 318 | } |
319 | 319 | ||
320 | printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n", | 320 | printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n", |
321 | size); | 321 | size); |
322 | return NULL; | 322 | return NULL; |
323 | } | 323 | } |
324 | 324 | ||
325 | static void percpu_modfree(void *freeme) | 325 | static void percpu_modfree(void *freeme) |
326 | { | 326 | { |
327 | unsigned int i; | 327 | unsigned int i; |
328 | void *ptr = __per_cpu_start + block_size(pcpu_size[0]); | 328 | void *ptr = __per_cpu_start + block_size(pcpu_size[0]); |
329 | 329 | ||
330 | /* First entry is core kernel percpu data. */ | 330 | /* First entry is core kernel percpu data. */ |
331 | for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) { | 331 | for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) { |
332 | if (ptr == freeme) { | 332 | if (ptr == freeme) { |
333 | pcpu_size[i] = -pcpu_size[i]; | 333 | pcpu_size[i] = -pcpu_size[i]; |
334 | goto free; | 334 | goto free; |
335 | } | 335 | } |
336 | } | 336 | } |
337 | BUG(); | 337 | BUG(); |
338 | 338 | ||
339 | free: | 339 | free: |
340 | /* Merge with previous? */ | 340 | /* Merge with previous? */ |
341 | if (pcpu_size[i-1] >= 0) { | 341 | if (pcpu_size[i-1] >= 0) { |
342 | pcpu_size[i-1] += pcpu_size[i]; | 342 | pcpu_size[i-1] += pcpu_size[i]; |
343 | pcpu_num_used--; | 343 | pcpu_num_used--; |
344 | memmove(&pcpu_size[i], &pcpu_size[i+1], | 344 | memmove(&pcpu_size[i], &pcpu_size[i+1], |
345 | (pcpu_num_used - i) * sizeof(pcpu_size[0])); | 345 | (pcpu_num_used - i) * sizeof(pcpu_size[0])); |
346 | i--; | 346 | i--; |
347 | } | 347 | } |
348 | /* Merge with next? */ | 348 | /* Merge with next? */ |
349 | if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) { | 349 | if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) { |
350 | pcpu_size[i] += pcpu_size[i+1]; | 350 | pcpu_size[i] += pcpu_size[i+1]; |
351 | pcpu_num_used--; | 351 | pcpu_num_used--; |
352 | memmove(&pcpu_size[i+1], &pcpu_size[i+2], | 352 | memmove(&pcpu_size[i+1], &pcpu_size[i+2], |
353 | (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0])); | 353 | (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0])); |
354 | } | 354 | } |
355 | } | 355 | } |
356 | 356 | ||
357 | static unsigned int find_pcpusec(Elf_Ehdr *hdr, | 357 | static unsigned int find_pcpusec(Elf_Ehdr *hdr, |
358 | Elf_Shdr *sechdrs, | 358 | Elf_Shdr *sechdrs, |
359 | const char *secstrings) | 359 | const char *secstrings) |
360 | { | 360 | { |
361 | return find_sec(hdr, sechdrs, secstrings, ".data.percpu"); | 361 | return find_sec(hdr, sechdrs, secstrings, ".data.percpu"); |
362 | } | 362 | } |
363 | 363 | ||
364 | static int percpu_modinit(void) | 364 | static int percpu_modinit(void) |
365 | { | 365 | { |
366 | pcpu_num_used = 2; | 366 | pcpu_num_used = 2; |
367 | pcpu_num_allocated = 2; | 367 | pcpu_num_allocated = 2; |
368 | pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated, | 368 | pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated, |
369 | GFP_KERNEL); | 369 | GFP_KERNEL); |
370 | /* Static in-kernel percpu data (used). */ | 370 | /* Static in-kernel percpu data (used). */ |
371 | pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES); | 371 | pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES); |
372 | /* Free room. */ | 372 | /* Free room. */ |
373 | pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0]; | 373 | pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0]; |
374 | if (pcpu_size[1] < 0) { | 374 | if (pcpu_size[1] < 0) { |
375 | printk(KERN_ERR "No per-cpu room for modules.\n"); | 375 | printk(KERN_ERR "No per-cpu room for modules.\n"); |
376 | pcpu_num_used = 1; | 376 | pcpu_num_used = 1; |
377 | } | 377 | } |
378 | 378 | ||
379 | return 0; | 379 | return 0; |
380 | } | 380 | } |
381 | __initcall(percpu_modinit); | 381 | __initcall(percpu_modinit); |
382 | #else /* ... !CONFIG_SMP */ | 382 | #else /* ... !CONFIG_SMP */ |
383 | static inline void *percpu_modalloc(unsigned long size, unsigned long align, | 383 | static inline void *percpu_modalloc(unsigned long size, unsigned long align, |
384 | const char *name) | 384 | const char *name) |
385 | { | 385 | { |
386 | return NULL; | 386 | return NULL; |
387 | } | 387 | } |
388 | static inline void percpu_modfree(void *pcpuptr) | 388 | static inline void percpu_modfree(void *pcpuptr) |
389 | { | 389 | { |
390 | BUG(); | 390 | BUG(); |
391 | } | 391 | } |
392 | static inline unsigned int find_pcpusec(Elf_Ehdr *hdr, | 392 | static inline unsigned int find_pcpusec(Elf_Ehdr *hdr, |
393 | Elf_Shdr *sechdrs, | 393 | Elf_Shdr *sechdrs, |
394 | const char *secstrings) | 394 | const char *secstrings) |
395 | { | 395 | { |
396 | return 0; | 396 | return 0; |
397 | } | 397 | } |
398 | static inline void percpu_modcopy(void *pcpudst, const void *src, | 398 | static inline void percpu_modcopy(void *pcpudst, const void *src, |
399 | unsigned long size) | 399 | unsigned long size) |
400 | { | 400 | { |
401 | /* pcpusec should be 0, and size of that section should be 0. */ | 401 | /* pcpusec should be 0, and size of that section should be 0. */ |
402 | BUG_ON(size != 0); | 402 | BUG_ON(size != 0); |
403 | } | 403 | } |
404 | #endif /* CONFIG_SMP */ | 404 | #endif /* CONFIG_SMP */ |
405 | 405 | ||
406 | #define MODINFO_ATTR(field) \ | 406 | #define MODINFO_ATTR(field) \ |
407 | static void setup_modinfo_##field(struct module *mod, const char *s) \ | 407 | static void setup_modinfo_##field(struct module *mod, const char *s) \ |
408 | { \ | 408 | { \ |
409 | mod->field = kstrdup(s, GFP_KERNEL); \ | 409 | mod->field = kstrdup(s, GFP_KERNEL); \ |
410 | } \ | 410 | } \ |
411 | static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ | 411 | static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ |
412 | struct module *mod, char *buffer) \ | 412 | struct module *mod, char *buffer) \ |
413 | { \ | 413 | { \ |
414 | return sprintf(buffer, "%s\n", mod->field); \ | 414 | return sprintf(buffer, "%s\n", mod->field); \ |
415 | } \ | 415 | } \ |
416 | static int modinfo_##field##_exists(struct module *mod) \ | 416 | static int modinfo_##field##_exists(struct module *mod) \ |
417 | { \ | 417 | { \ |
418 | return mod->field != NULL; \ | 418 | return mod->field != NULL; \ |
419 | } \ | 419 | } \ |
420 | static void free_modinfo_##field(struct module *mod) \ | 420 | static void free_modinfo_##field(struct module *mod) \ |
421 | { \ | 421 | { \ |
422 | kfree(mod->field); \ | 422 | kfree(mod->field); \ |
423 | mod->field = NULL; \ | 423 | mod->field = NULL; \ |
424 | } \ | 424 | } \ |
425 | static struct module_attribute modinfo_##field = { \ | 425 | static struct module_attribute modinfo_##field = { \ |
426 | .attr = { .name = __stringify(field), .mode = 0444, \ | 426 | .attr = { .name = __stringify(field), .mode = 0444, \ |
427 | .owner = THIS_MODULE }, \ | 427 | .owner = THIS_MODULE }, \ |
428 | .show = show_modinfo_##field, \ | 428 | .show = show_modinfo_##field, \ |
429 | .setup = setup_modinfo_##field, \ | 429 | .setup = setup_modinfo_##field, \ |
430 | .test = modinfo_##field##_exists, \ | 430 | .test = modinfo_##field##_exists, \ |
431 | .free = free_modinfo_##field, \ | 431 | .free = free_modinfo_##field, \ |
432 | }; | 432 | }; |
433 | 433 | ||
434 | MODINFO_ATTR(version); | 434 | MODINFO_ATTR(version); |
435 | MODINFO_ATTR(srcversion); | 435 | MODINFO_ATTR(srcversion); |
436 | 436 | ||
437 | #ifdef CONFIG_MODULE_UNLOAD | 437 | #ifdef CONFIG_MODULE_UNLOAD |
438 | /* Init the unload section of the module. */ | 438 | /* Init the unload section of the module. */ |
439 | static void module_unload_init(struct module *mod) | 439 | static void module_unload_init(struct module *mod) |
440 | { | 440 | { |
441 | unsigned int i; | 441 | unsigned int i; |
442 | 442 | ||
443 | INIT_LIST_HEAD(&mod->modules_which_use_me); | 443 | INIT_LIST_HEAD(&mod->modules_which_use_me); |
444 | for (i = 0; i < NR_CPUS; i++) | 444 | for (i = 0; i < NR_CPUS; i++) |
445 | local_set(&mod->ref[i].count, 0); | 445 | local_set(&mod->ref[i].count, 0); |
446 | /* Hold reference count during initialization. */ | 446 | /* Hold reference count during initialization. */ |
447 | local_set(&mod->ref[raw_smp_processor_id()].count, 1); | 447 | local_set(&mod->ref[raw_smp_processor_id()].count, 1); |
448 | /* Backwards compatibility macros put refcount during init. */ | 448 | /* Backwards compatibility macros put refcount during init. */ |
449 | mod->waiter = current; | 449 | mod->waiter = current; |
450 | } | 450 | } |
451 | 451 | ||
452 | /* modules using other modules */ | 452 | /* modules using other modules */ |
453 | struct module_use | 453 | struct module_use |
454 | { | 454 | { |
455 | struct list_head list; | 455 | struct list_head list; |
456 | struct module *module_which_uses; | 456 | struct module *module_which_uses; |
457 | }; | 457 | }; |
458 | 458 | ||
459 | /* Does a already use b? */ | 459 | /* Does a already use b? */ |
460 | static int already_uses(struct module *a, struct module *b) | 460 | static int already_uses(struct module *a, struct module *b) |
461 | { | 461 | { |
462 | struct module_use *use; | 462 | struct module_use *use; |
463 | 463 | ||
464 | list_for_each_entry(use, &b->modules_which_use_me, list) { | 464 | list_for_each_entry(use, &b->modules_which_use_me, list) { |
465 | if (use->module_which_uses == a) { | 465 | if (use->module_which_uses == a) { |
466 | DEBUGP("%s uses %s!\n", a->name, b->name); | 466 | DEBUGP("%s uses %s!\n", a->name, b->name); |
467 | return 1; | 467 | return 1; |
468 | } | 468 | } |
469 | } | 469 | } |
470 | DEBUGP("%s does not use %s!\n", a->name, b->name); | 470 | DEBUGP("%s does not use %s!\n", a->name, b->name); |
471 | return 0; | 471 | return 0; |
472 | } | 472 | } |
473 | 473 | ||
474 | /* Module a uses b */ | 474 | /* Module a uses b */ |
475 | static int use_module(struct module *a, struct module *b) | 475 | static int use_module(struct module *a, struct module *b) |
476 | { | 476 | { |
477 | struct module_use *use; | 477 | struct module_use *use; |
478 | if (b == NULL || already_uses(a, b)) return 1; | 478 | if (b == NULL || already_uses(a, b)) return 1; |
479 | 479 | ||
480 | if (!strong_try_module_get(b)) | 480 | if (!strong_try_module_get(b)) |
481 | return 0; | 481 | return 0; |
482 | 482 | ||
483 | DEBUGP("Allocating new usage for %s.\n", a->name); | 483 | DEBUGP("Allocating new usage for %s.\n", a->name); |
484 | use = kmalloc(sizeof(*use), GFP_ATOMIC); | 484 | use = kmalloc(sizeof(*use), GFP_ATOMIC); |
485 | if (!use) { | 485 | if (!use) { |
486 | printk("%s: out of memory loading\n", a->name); | 486 | printk("%s: out of memory loading\n", a->name); |
487 | module_put(b); | 487 | module_put(b); |
488 | return 0; | 488 | return 0; |
489 | } | 489 | } |
490 | 490 | ||
491 | use->module_which_uses = a; | 491 | use->module_which_uses = a; |
492 | list_add(&use->list, &b->modules_which_use_me); | 492 | list_add(&use->list, &b->modules_which_use_me); |
493 | return 1; | 493 | return 1; |
494 | } | 494 | } |
495 | 495 | ||
496 | /* Clear the unload stuff of the module. */ | 496 | /* Clear the unload stuff of the module. */ |
497 | static void module_unload_free(struct module *mod) | 497 | static void module_unload_free(struct module *mod) |
498 | { | 498 | { |
499 | struct module *i; | 499 | struct module *i; |
500 | 500 | ||
501 | list_for_each_entry(i, &modules, list) { | 501 | list_for_each_entry(i, &modules, list) { |
502 | struct module_use *use; | 502 | struct module_use *use; |
503 | 503 | ||
504 | list_for_each_entry(use, &i->modules_which_use_me, list) { | 504 | list_for_each_entry(use, &i->modules_which_use_me, list) { |
505 | if (use->module_which_uses == mod) { | 505 | if (use->module_which_uses == mod) { |
506 | DEBUGP("%s unusing %s\n", mod->name, i->name); | 506 | DEBUGP("%s unusing %s\n", mod->name, i->name); |
507 | module_put(i); | 507 | module_put(i); |
508 | list_del(&use->list); | 508 | list_del(&use->list); |
509 | kfree(use); | 509 | kfree(use); |
510 | /* There can be at most one match. */ | 510 | /* There can be at most one match. */ |
511 | break; | 511 | break; |
512 | } | 512 | } |
513 | } | 513 | } |
514 | } | 514 | } |
515 | } | 515 | } |
516 | 516 | ||
517 | #ifdef CONFIG_MODULE_FORCE_UNLOAD | 517 | #ifdef CONFIG_MODULE_FORCE_UNLOAD |
518 | static inline int try_force_unload(unsigned int flags) | 518 | static inline int try_force_unload(unsigned int flags) |
519 | { | 519 | { |
520 | int ret = (flags & O_TRUNC); | 520 | int ret = (flags & O_TRUNC); |
521 | if (ret) | 521 | if (ret) |
522 | add_taint(TAINT_FORCED_RMMOD); | 522 | add_taint(TAINT_FORCED_RMMOD); |
523 | return ret; | 523 | return ret; |
524 | } | 524 | } |
525 | #else | 525 | #else |
526 | static inline int try_force_unload(unsigned int flags) | 526 | static inline int try_force_unload(unsigned int flags) |
527 | { | 527 | { |
528 | return 0; | 528 | return 0; |
529 | } | 529 | } |
530 | #endif /* CONFIG_MODULE_FORCE_UNLOAD */ | 530 | #endif /* CONFIG_MODULE_FORCE_UNLOAD */ |
531 | 531 | ||
532 | struct stopref | 532 | struct stopref |
533 | { | 533 | { |
534 | struct module *mod; | 534 | struct module *mod; |
535 | int flags; | 535 | int flags; |
536 | int *forced; | 536 | int *forced; |
537 | }; | 537 | }; |
538 | 538 | ||
539 | /* Whole machine is stopped with interrupts off when this runs. */ | 539 | /* Whole machine is stopped with interrupts off when this runs. */ |
540 | static int __try_stop_module(void *_sref) | 540 | static int __try_stop_module(void *_sref) |
541 | { | 541 | { |
542 | struct stopref *sref = _sref; | 542 | struct stopref *sref = _sref; |
543 | 543 | ||
544 | /* If it's not unused, quit unless we are told to block. */ | 544 | /* If it's not unused, quit unless we are told to block. */ |
545 | if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) { | 545 | if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) { |
546 | if (!(*sref->forced = try_force_unload(sref->flags))) | 546 | if (!(*sref->forced = try_force_unload(sref->flags))) |
547 | return -EWOULDBLOCK; | 547 | return -EWOULDBLOCK; |
548 | } | 548 | } |
549 | 549 | ||
550 | /* Mark it as dying. */ | 550 | /* Mark it as dying. */ |
551 | sref->mod->state = MODULE_STATE_GOING; | 551 | sref->mod->state = MODULE_STATE_GOING; |
552 | return 0; | 552 | return 0; |
553 | } | 553 | } |
554 | 554 | ||
555 | static int try_stop_module(struct module *mod, int flags, int *forced) | 555 | static int try_stop_module(struct module *mod, int flags, int *forced) |
556 | { | 556 | { |
557 | struct stopref sref = { mod, flags, forced }; | 557 | struct stopref sref = { mod, flags, forced }; |
558 | 558 | ||
559 | return stop_machine_run(__try_stop_module, &sref, NR_CPUS); | 559 | return stop_machine_run(__try_stop_module, &sref, NR_CPUS); |
560 | } | 560 | } |
561 | 561 | ||
562 | unsigned int module_refcount(struct module *mod) | 562 | unsigned int module_refcount(struct module *mod) |
563 | { | 563 | { |
564 | unsigned int i, total = 0; | 564 | unsigned int i, total = 0; |
565 | 565 | ||
566 | for (i = 0; i < NR_CPUS; i++) | 566 | for (i = 0; i < NR_CPUS; i++) |
567 | total += local_read(&mod->ref[i].count); | 567 | total += local_read(&mod->ref[i].count); |
568 | return total; | 568 | return total; |
569 | } | 569 | } |
570 | EXPORT_SYMBOL(module_refcount); | 570 | EXPORT_SYMBOL(module_refcount); |
571 | 571 | ||
572 | /* This exists whether we can unload or not */ | 572 | /* This exists whether we can unload or not */ |
573 | static void free_module(struct module *mod); | 573 | static void free_module(struct module *mod); |
574 | 574 | ||
575 | static void wait_for_zero_refcount(struct module *mod) | 575 | static void wait_for_zero_refcount(struct module *mod) |
576 | { | 576 | { |
577 | /* Since we might sleep for some time, drop the semaphore first */ | 577 | /* Since we might sleep for some time, drop the semaphore first */ |
578 | mutex_unlock(&module_mutex); | 578 | mutex_unlock(&module_mutex); |
579 | for (;;) { | 579 | for (;;) { |
580 | DEBUGP("Looking at refcount...\n"); | 580 | DEBUGP("Looking at refcount...\n"); |
581 | set_current_state(TASK_UNINTERRUPTIBLE); | 581 | set_current_state(TASK_UNINTERRUPTIBLE); |
582 | if (module_refcount(mod) == 0) | 582 | if (module_refcount(mod) == 0) |
583 | break; | 583 | break; |
584 | schedule(); | 584 | schedule(); |
585 | } | 585 | } |
586 | current->state = TASK_RUNNING; | 586 | current->state = TASK_RUNNING; |
587 | mutex_lock(&module_mutex); | 587 | mutex_lock(&module_mutex); |
588 | } | 588 | } |
589 | 589 | ||
590 | asmlinkage long | 590 | asmlinkage long |
591 | sys_delete_module(const char __user *name_user, unsigned int flags) | 591 | sys_delete_module(const char __user *name_user, unsigned int flags) |
592 | { | 592 | { |
593 | struct module *mod; | 593 | struct module *mod; |
594 | char name[MODULE_NAME_LEN]; | 594 | char name[MODULE_NAME_LEN]; |
595 | int ret, forced = 0; | 595 | int ret, forced = 0; |
596 | 596 | ||
597 | if (!capable(CAP_SYS_MODULE)) | 597 | if (!capable(CAP_SYS_MODULE)) |
598 | return -EPERM; | 598 | return -EPERM; |
599 | 599 | ||
600 | if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) | 600 | if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) |
601 | return -EFAULT; | 601 | return -EFAULT; |
602 | name[MODULE_NAME_LEN-1] = '\0'; | 602 | name[MODULE_NAME_LEN-1] = '\0'; |
603 | 603 | ||
604 | if (mutex_lock_interruptible(&module_mutex) != 0) | 604 | if (mutex_lock_interruptible(&module_mutex) != 0) |
605 | return -EINTR; | 605 | return -EINTR; |
606 | 606 | ||
607 | mod = find_module(name); | 607 | mod = find_module(name); |
608 | if (!mod) { | 608 | if (!mod) { |
609 | ret = -ENOENT; | 609 | ret = -ENOENT; |
610 | goto out; | 610 | goto out; |
611 | } | 611 | } |
612 | 612 | ||
613 | if (!list_empty(&mod->modules_which_use_me)) { | 613 | if (!list_empty(&mod->modules_which_use_me)) { |
614 | /* Other modules depend on us: get rid of them first. */ | 614 | /* Other modules depend on us: get rid of them first. */ |
615 | ret = -EWOULDBLOCK; | 615 | ret = -EWOULDBLOCK; |
616 | goto out; | 616 | goto out; |
617 | } | 617 | } |
618 | 618 | ||
619 | /* Doing init or already dying? */ | 619 | /* Doing init or already dying? */ |
620 | if (mod->state != MODULE_STATE_LIVE) { | 620 | if (mod->state != MODULE_STATE_LIVE) { |
621 | /* FIXME: if (force), slam module count and wake up | 621 | /* FIXME: if (force), slam module count and wake up |
622 | waiter --RR */ | 622 | waiter --RR */ |
623 | DEBUGP("%s already dying\n", mod->name); | 623 | DEBUGP("%s already dying\n", mod->name); |
624 | ret = -EBUSY; | 624 | ret = -EBUSY; |
625 | goto out; | 625 | goto out; |
626 | } | 626 | } |
627 | 627 | ||
628 | /* If it has an init func, it must have an exit func to unload */ | 628 | /* If it has an init func, it must have an exit func to unload */ |
629 | if ((mod->init != NULL && mod->exit == NULL) | 629 | if ((mod->init != NULL && mod->exit == NULL) |
630 | || mod->unsafe) { | 630 | || mod->unsafe) { |
631 | forced = try_force_unload(flags); | 631 | forced = try_force_unload(flags); |
632 | if (!forced) { | 632 | if (!forced) { |
633 | /* This module can't be removed */ | 633 | /* This module can't be removed */ |
634 | ret = -EBUSY; | 634 | ret = -EBUSY; |
635 | goto out; | 635 | goto out; |
636 | } | 636 | } |
637 | } | 637 | } |
638 | 638 | ||
639 | /* Set this up before setting mod->state */ | 639 | /* Set this up before setting mod->state */ |
640 | mod->waiter = current; | 640 | mod->waiter = current; |
641 | 641 | ||
642 | /* Stop the machine so refcounts can't move and disable module. */ | 642 | /* Stop the machine so refcounts can't move and disable module. */ |
643 | ret = try_stop_module(mod, flags, &forced); | 643 | ret = try_stop_module(mod, flags, &forced); |
644 | if (ret != 0) | 644 | if (ret != 0) |
645 | goto out; | 645 | goto out; |
646 | 646 | ||
647 | /* Never wait if forced. */ | 647 | /* Never wait if forced. */ |
648 | if (!forced && module_refcount(mod) != 0) | 648 | if (!forced && module_refcount(mod) != 0) |
649 | wait_for_zero_refcount(mod); | 649 | wait_for_zero_refcount(mod); |
650 | 650 | ||
651 | /* Final destruction now noone is using it. */ | 651 | /* Final destruction now noone is using it. */ |
652 | if (mod->exit != NULL) { | 652 | if (mod->exit != NULL) { |
653 | mutex_unlock(&module_mutex); | 653 | mutex_unlock(&module_mutex); |
654 | mod->exit(); | 654 | mod->exit(); |
655 | mutex_lock(&module_mutex); | 655 | mutex_lock(&module_mutex); |
656 | } | 656 | } |
657 | free_module(mod); | 657 | free_module(mod); |
658 | 658 | ||
659 | out: | 659 | out: |
660 | mutex_unlock(&module_mutex); | 660 | mutex_unlock(&module_mutex); |
661 | return ret; | 661 | return ret; |
662 | } | 662 | } |
663 | 663 | ||
664 | static void print_unload_info(struct seq_file *m, struct module *mod) | 664 | static void print_unload_info(struct seq_file *m, struct module *mod) |
665 | { | 665 | { |
666 | struct module_use *use; | 666 | struct module_use *use; |
667 | int printed_something = 0; | 667 | int printed_something = 0; |
668 | 668 | ||
669 | seq_printf(m, " %u ", module_refcount(mod)); | 669 | seq_printf(m, " %u ", module_refcount(mod)); |
670 | 670 | ||
671 | /* Always include a trailing , so userspace can differentiate | 671 | /* Always include a trailing , so userspace can differentiate |
672 | between this and the old multi-field proc format. */ | 672 | between this and the old multi-field proc format. */ |
673 | list_for_each_entry(use, &mod->modules_which_use_me, list) { | 673 | list_for_each_entry(use, &mod->modules_which_use_me, list) { |
674 | printed_something = 1; | 674 | printed_something = 1; |
675 | seq_printf(m, "%s,", use->module_which_uses->name); | 675 | seq_printf(m, "%s,", use->module_which_uses->name); |
676 | } | 676 | } |
677 | 677 | ||
678 | if (mod->unsafe) { | 678 | if (mod->unsafe) { |
679 | printed_something = 1; | 679 | printed_something = 1; |
680 | seq_printf(m, "[unsafe],"); | 680 | seq_printf(m, "[unsafe],"); |
681 | } | 681 | } |
682 | 682 | ||
683 | if (mod->init != NULL && mod->exit == NULL) { | 683 | if (mod->init != NULL && mod->exit == NULL) { |
684 | printed_something = 1; | 684 | printed_something = 1; |
685 | seq_printf(m, "[permanent],"); | 685 | seq_printf(m, "[permanent],"); |
686 | } | 686 | } |
687 | 687 | ||
688 | if (!printed_something) | 688 | if (!printed_something) |
689 | seq_printf(m, "-"); | 689 | seq_printf(m, "-"); |
690 | } | 690 | } |
691 | 691 | ||
692 | void __symbol_put(const char *symbol) | 692 | void __symbol_put(const char *symbol) |
693 | { | 693 | { |
694 | struct module *owner; | 694 | struct module *owner; |
695 | unsigned long flags; | 695 | unsigned long flags; |
696 | const unsigned long *crc; | 696 | const unsigned long *crc; |
697 | 697 | ||
698 | spin_lock_irqsave(&modlist_lock, flags); | 698 | spin_lock_irqsave(&modlist_lock, flags); |
699 | if (!__find_symbol(symbol, &owner, &crc, 1)) | 699 | if (!__find_symbol(symbol, &owner, &crc, 1)) |
700 | BUG(); | 700 | BUG(); |
701 | module_put(owner); | 701 | module_put(owner); |
702 | spin_unlock_irqrestore(&modlist_lock, flags); | 702 | spin_unlock_irqrestore(&modlist_lock, flags); |
703 | } | 703 | } |
704 | EXPORT_SYMBOL(__symbol_put); | 704 | EXPORT_SYMBOL(__symbol_put); |
705 | 705 | ||
706 | void symbol_put_addr(void *addr) | 706 | void symbol_put_addr(void *addr) |
707 | { | 707 | { |
708 | unsigned long flags; | 708 | struct module *modaddr; |
709 | 709 | ||
710 | spin_lock_irqsave(&modlist_lock, flags); | 710 | if (core_kernel_text((unsigned long)addr)) |
711 | if (!kernel_text_address((unsigned long)addr)) | 711 | return; |
712 | BUG(); | ||
713 | 712 | ||
714 | module_put(module_text_address((unsigned long)addr)); | 713 | if (!(modaddr = module_text_address((unsigned long)addr))) |
715 | spin_unlock_irqrestore(&modlist_lock, flags); | 714 | BUG(); |
715 | module_put(modaddr); | ||
716 | } | 716 | } |
717 | EXPORT_SYMBOL_GPL(symbol_put_addr); | 717 | EXPORT_SYMBOL_GPL(symbol_put_addr); |
718 | 718 | ||
719 | static ssize_t show_refcnt(struct module_attribute *mattr, | 719 | static ssize_t show_refcnt(struct module_attribute *mattr, |
720 | struct module *mod, char *buffer) | 720 | struct module *mod, char *buffer) |
721 | { | 721 | { |
722 | /* sysfs holds a reference */ | 722 | /* sysfs holds a reference */ |
723 | return sprintf(buffer, "%u\n", module_refcount(mod)-1); | 723 | return sprintf(buffer, "%u\n", module_refcount(mod)-1); |
724 | } | 724 | } |
725 | 725 | ||
726 | static struct module_attribute refcnt = { | 726 | static struct module_attribute refcnt = { |
727 | .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE }, | 727 | .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE }, |
728 | .show = show_refcnt, | 728 | .show = show_refcnt, |
729 | }; | 729 | }; |
730 | 730 | ||
731 | #else /* !CONFIG_MODULE_UNLOAD */ | 731 | #else /* !CONFIG_MODULE_UNLOAD */ |
732 | static void print_unload_info(struct seq_file *m, struct module *mod) | 732 | static void print_unload_info(struct seq_file *m, struct module *mod) |
733 | { | 733 | { |
734 | /* We don't know the usage count, or what modules are using. */ | 734 | /* We don't know the usage count, or what modules are using. */ |
735 | seq_printf(m, " - -"); | 735 | seq_printf(m, " - -"); |
736 | } | 736 | } |
737 | 737 | ||
738 | static inline void module_unload_free(struct module *mod) | 738 | static inline void module_unload_free(struct module *mod) |
739 | { | 739 | { |
740 | } | 740 | } |
741 | 741 | ||
742 | static inline int use_module(struct module *a, struct module *b) | 742 | static inline int use_module(struct module *a, struct module *b) |
743 | { | 743 | { |
744 | return strong_try_module_get(b); | 744 | return strong_try_module_get(b); |
745 | } | 745 | } |
746 | 746 | ||
747 | static inline void module_unload_init(struct module *mod) | 747 | static inline void module_unload_init(struct module *mod) |
748 | { | 748 | { |
749 | } | 749 | } |
750 | #endif /* CONFIG_MODULE_UNLOAD */ | 750 | #endif /* CONFIG_MODULE_UNLOAD */ |
751 | 751 | ||
752 | static struct module_attribute *modinfo_attrs[] = { | 752 | static struct module_attribute *modinfo_attrs[] = { |
753 | &modinfo_version, | 753 | &modinfo_version, |
754 | &modinfo_srcversion, | 754 | &modinfo_srcversion, |
755 | #ifdef CONFIG_MODULE_UNLOAD | 755 | #ifdef CONFIG_MODULE_UNLOAD |
756 | &refcnt, | 756 | &refcnt, |
757 | #endif | 757 | #endif |
758 | NULL, | 758 | NULL, |
759 | }; | 759 | }; |
760 | 760 | ||
761 | static const char vermagic[] = VERMAGIC_STRING; | 761 | static const char vermagic[] = VERMAGIC_STRING; |
762 | 762 | ||
763 | #ifdef CONFIG_MODVERSIONS | 763 | #ifdef CONFIG_MODVERSIONS |
764 | static int check_version(Elf_Shdr *sechdrs, | 764 | static int check_version(Elf_Shdr *sechdrs, |
765 | unsigned int versindex, | 765 | unsigned int versindex, |
766 | const char *symname, | 766 | const char *symname, |
767 | struct module *mod, | 767 | struct module *mod, |
768 | const unsigned long *crc) | 768 | const unsigned long *crc) |
769 | { | 769 | { |
770 | unsigned int i, num_versions; | 770 | unsigned int i, num_versions; |
771 | struct modversion_info *versions; | 771 | struct modversion_info *versions; |
772 | 772 | ||
773 | /* Exporting module didn't supply crcs? OK, we're already tainted. */ | 773 | /* Exporting module didn't supply crcs? OK, we're already tainted. */ |
774 | if (!crc) | 774 | if (!crc) |
775 | return 1; | 775 | return 1; |
776 | 776 | ||
777 | versions = (void *) sechdrs[versindex].sh_addr; | 777 | versions = (void *) sechdrs[versindex].sh_addr; |
778 | num_versions = sechdrs[versindex].sh_size | 778 | num_versions = sechdrs[versindex].sh_size |
779 | / sizeof(struct modversion_info); | 779 | / sizeof(struct modversion_info); |
780 | 780 | ||
781 | for (i = 0; i < num_versions; i++) { | 781 | for (i = 0; i < num_versions; i++) { |
782 | if (strcmp(versions[i].name, symname) != 0) | 782 | if (strcmp(versions[i].name, symname) != 0) |
783 | continue; | 783 | continue; |
784 | 784 | ||
785 | if (versions[i].crc == *crc) | 785 | if (versions[i].crc == *crc) |
786 | return 1; | 786 | return 1; |
787 | printk("%s: disagrees about version of symbol %s\n", | 787 | printk("%s: disagrees about version of symbol %s\n", |
788 | mod->name, symname); | 788 | mod->name, symname); |
789 | DEBUGP("Found checksum %lX vs module %lX\n", | 789 | DEBUGP("Found checksum %lX vs module %lX\n", |
790 | *crc, versions[i].crc); | 790 | *crc, versions[i].crc); |
791 | return 0; | 791 | return 0; |
792 | } | 792 | } |
793 | /* Not in module's version table. OK, but that taints the kernel. */ | 793 | /* Not in module's version table. OK, but that taints the kernel. */ |
794 | if (!(tainted & TAINT_FORCED_MODULE)) { | 794 | if (!(tainted & TAINT_FORCED_MODULE)) { |
795 | printk("%s: no version for \"%s\" found: kernel tainted.\n", | 795 | printk("%s: no version for \"%s\" found: kernel tainted.\n", |
796 | mod->name, symname); | 796 | mod->name, symname); |
797 | add_taint(TAINT_FORCED_MODULE); | 797 | add_taint(TAINT_FORCED_MODULE); |
798 | } | 798 | } |
799 | return 1; | 799 | return 1; |
800 | } | 800 | } |
801 | 801 | ||
802 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, | 802 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, |
803 | unsigned int versindex, | 803 | unsigned int versindex, |
804 | struct module *mod) | 804 | struct module *mod) |
805 | { | 805 | { |
806 | const unsigned long *crc; | 806 | const unsigned long *crc; |
807 | struct module *owner; | 807 | struct module *owner; |
808 | 808 | ||
809 | if (!__find_symbol("struct_module", &owner, &crc, 1)) | 809 | if (!__find_symbol("struct_module", &owner, &crc, 1)) |
810 | BUG(); | 810 | BUG(); |
811 | return check_version(sechdrs, versindex, "struct_module", mod, | 811 | return check_version(sechdrs, versindex, "struct_module", mod, |
812 | crc); | 812 | crc); |
813 | } | 813 | } |
814 | 814 | ||
815 | /* First part is kernel version, which we ignore. */ | 815 | /* First part is kernel version, which we ignore. */ |
816 | static inline int same_magic(const char *amagic, const char *bmagic) | 816 | static inline int same_magic(const char *amagic, const char *bmagic) |
817 | { | 817 | { |
818 | amagic += strcspn(amagic, " "); | 818 | amagic += strcspn(amagic, " "); |
819 | bmagic += strcspn(bmagic, " "); | 819 | bmagic += strcspn(bmagic, " "); |
820 | return strcmp(amagic, bmagic) == 0; | 820 | return strcmp(amagic, bmagic) == 0; |
821 | } | 821 | } |
822 | #else | 822 | #else |
823 | static inline int check_version(Elf_Shdr *sechdrs, | 823 | static inline int check_version(Elf_Shdr *sechdrs, |
824 | unsigned int versindex, | 824 | unsigned int versindex, |
825 | const char *symname, | 825 | const char *symname, |
826 | struct module *mod, | 826 | struct module *mod, |
827 | const unsigned long *crc) | 827 | const unsigned long *crc) |
828 | { | 828 | { |
829 | return 1; | 829 | return 1; |
830 | } | 830 | } |
831 | 831 | ||
832 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, | 832 | static inline int check_modstruct_version(Elf_Shdr *sechdrs, |
833 | unsigned int versindex, | 833 | unsigned int versindex, |
834 | struct module *mod) | 834 | struct module *mod) |
835 | { | 835 | { |
836 | return 1; | 836 | return 1; |
837 | } | 837 | } |
838 | 838 | ||
839 | static inline int same_magic(const char *amagic, const char *bmagic) | 839 | static inline int same_magic(const char *amagic, const char *bmagic) |
840 | { | 840 | { |
841 | return strcmp(amagic, bmagic) == 0; | 841 | return strcmp(amagic, bmagic) == 0; |
842 | } | 842 | } |
843 | #endif /* CONFIG_MODVERSIONS */ | 843 | #endif /* CONFIG_MODVERSIONS */ |
844 | 844 | ||
845 | /* Resolve a symbol for this module. I.e. if we find one, record usage. | 845 | /* Resolve a symbol for this module. I.e. if we find one, record usage. |
846 | Must be holding module_mutex. */ | 846 | Must be holding module_mutex. */ |
847 | static unsigned long resolve_symbol(Elf_Shdr *sechdrs, | 847 | static unsigned long resolve_symbol(Elf_Shdr *sechdrs, |
848 | unsigned int versindex, | 848 | unsigned int versindex, |
849 | const char *name, | 849 | const char *name, |
850 | struct module *mod) | 850 | struct module *mod) |
851 | { | 851 | { |
852 | struct module *owner; | 852 | struct module *owner; |
853 | unsigned long ret; | 853 | unsigned long ret; |
854 | const unsigned long *crc; | 854 | const unsigned long *crc; |
855 | 855 | ||
856 | ret = __find_symbol(name, &owner, &crc, mod->license_gplok); | 856 | ret = __find_symbol(name, &owner, &crc, mod->license_gplok); |
857 | if (ret) { | 857 | if (ret) { |
858 | /* use_module can fail due to OOM, or module unloading */ | 858 | /* use_module can fail due to OOM, or module unloading */ |
859 | if (!check_version(sechdrs, versindex, name, mod, crc) || | 859 | if (!check_version(sechdrs, versindex, name, mod, crc) || |
860 | !use_module(mod, owner)) | 860 | !use_module(mod, owner)) |
861 | ret = 0; | 861 | ret = 0; |
862 | } | 862 | } |
863 | return ret; | 863 | return ret; |
864 | } | 864 | } |
865 | 865 | ||
866 | 866 | ||
867 | /* | 867 | /* |
868 | * /sys/module/foo/sections stuff | 868 | * /sys/module/foo/sections stuff |
869 | * J. Corbet <corbet@lwn.net> | 869 | * J. Corbet <corbet@lwn.net> |
870 | */ | 870 | */ |
871 | #ifdef CONFIG_KALLSYMS | 871 | #ifdef CONFIG_KALLSYMS |
872 | static ssize_t module_sect_show(struct module_attribute *mattr, | 872 | static ssize_t module_sect_show(struct module_attribute *mattr, |
873 | struct module *mod, char *buf) | 873 | struct module *mod, char *buf) |
874 | { | 874 | { |
875 | struct module_sect_attr *sattr = | 875 | struct module_sect_attr *sattr = |
876 | container_of(mattr, struct module_sect_attr, mattr); | 876 | container_of(mattr, struct module_sect_attr, mattr); |
877 | return sprintf(buf, "0x%lx\n", sattr->address); | 877 | return sprintf(buf, "0x%lx\n", sattr->address); |
878 | } | 878 | } |
879 | 879 | ||
880 | static void add_sect_attrs(struct module *mod, unsigned int nsect, | 880 | static void add_sect_attrs(struct module *mod, unsigned int nsect, |
881 | char *secstrings, Elf_Shdr *sechdrs) | 881 | char *secstrings, Elf_Shdr *sechdrs) |
882 | { | 882 | { |
883 | unsigned int nloaded = 0, i, size[2]; | 883 | unsigned int nloaded = 0, i, size[2]; |
884 | struct module_sect_attrs *sect_attrs; | 884 | struct module_sect_attrs *sect_attrs; |
885 | struct module_sect_attr *sattr; | 885 | struct module_sect_attr *sattr; |
886 | struct attribute **gattr; | 886 | struct attribute **gattr; |
887 | 887 | ||
888 | /* Count loaded sections and allocate structures */ | 888 | /* Count loaded sections and allocate structures */ |
889 | for (i = 0; i < nsect; i++) | 889 | for (i = 0; i < nsect; i++) |
890 | if (sechdrs[i].sh_flags & SHF_ALLOC) | 890 | if (sechdrs[i].sh_flags & SHF_ALLOC) |
891 | nloaded++; | 891 | nloaded++; |
892 | size[0] = ALIGN(sizeof(*sect_attrs) | 892 | size[0] = ALIGN(sizeof(*sect_attrs) |
893 | + nloaded * sizeof(sect_attrs->attrs[0]), | 893 | + nloaded * sizeof(sect_attrs->attrs[0]), |
894 | sizeof(sect_attrs->grp.attrs[0])); | 894 | sizeof(sect_attrs->grp.attrs[0])); |
895 | size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]); | 895 | size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]); |
896 | if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL))) | 896 | if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL))) |
897 | return; | 897 | return; |
898 | 898 | ||
899 | /* Setup section attributes. */ | 899 | /* Setup section attributes. */ |
900 | sect_attrs->grp.name = "sections"; | 900 | sect_attrs->grp.name = "sections"; |
901 | sect_attrs->grp.attrs = (void *)sect_attrs + size[0]; | 901 | sect_attrs->grp.attrs = (void *)sect_attrs + size[0]; |
902 | 902 | ||
903 | sattr = §_attrs->attrs[0]; | 903 | sattr = §_attrs->attrs[0]; |
904 | gattr = §_attrs->grp.attrs[0]; | 904 | gattr = §_attrs->grp.attrs[0]; |
905 | for (i = 0; i < nsect; i++) { | 905 | for (i = 0; i < nsect; i++) { |
906 | if (! (sechdrs[i].sh_flags & SHF_ALLOC)) | 906 | if (! (sechdrs[i].sh_flags & SHF_ALLOC)) |
907 | continue; | 907 | continue; |
908 | sattr->address = sechdrs[i].sh_addr; | 908 | sattr->address = sechdrs[i].sh_addr; |
909 | strlcpy(sattr->name, secstrings + sechdrs[i].sh_name, | 909 | strlcpy(sattr->name, secstrings + sechdrs[i].sh_name, |
910 | MODULE_SECT_NAME_LEN); | 910 | MODULE_SECT_NAME_LEN); |
911 | sattr->mattr.show = module_sect_show; | 911 | sattr->mattr.show = module_sect_show; |
912 | sattr->mattr.store = NULL; | 912 | sattr->mattr.store = NULL; |
913 | sattr->mattr.attr.name = sattr->name; | 913 | sattr->mattr.attr.name = sattr->name; |
914 | sattr->mattr.attr.owner = mod; | 914 | sattr->mattr.attr.owner = mod; |
915 | sattr->mattr.attr.mode = S_IRUGO; | 915 | sattr->mattr.attr.mode = S_IRUGO; |
916 | *(gattr++) = &(sattr++)->mattr.attr; | 916 | *(gattr++) = &(sattr++)->mattr.attr; |
917 | } | 917 | } |
918 | *gattr = NULL; | 918 | *gattr = NULL; |
919 | 919 | ||
920 | if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp)) | 920 | if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp)) |
921 | goto out; | 921 | goto out; |
922 | 922 | ||
923 | mod->sect_attrs = sect_attrs; | 923 | mod->sect_attrs = sect_attrs; |
924 | return; | 924 | return; |
925 | out: | 925 | out: |
926 | kfree(sect_attrs); | 926 | kfree(sect_attrs); |
927 | } | 927 | } |
928 | 928 | ||
929 | static void remove_sect_attrs(struct module *mod) | 929 | static void remove_sect_attrs(struct module *mod) |
930 | { | 930 | { |
931 | if (mod->sect_attrs) { | 931 | if (mod->sect_attrs) { |
932 | sysfs_remove_group(&mod->mkobj.kobj, | 932 | sysfs_remove_group(&mod->mkobj.kobj, |
933 | &mod->sect_attrs->grp); | 933 | &mod->sect_attrs->grp); |
934 | /* We are positive that no one is using any sect attrs | 934 | /* We are positive that no one is using any sect attrs |
935 | * at this point. Deallocate immediately. */ | 935 | * at this point. Deallocate immediately. */ |
936 | kfree(mod->sect_attrs); | 936 | kfree(mod->sect_attrs); |
937 | mod->sect_attrs = NULL; | 937 | mod->sect_attrs = NULL; |
938 | } | 938 | } |
939 | } | 939 | } |
940 | 940 | ||
941 | 941 | ||
942 | #else | 942 | #else |
943 | static inline void add_sect_attrs(struct module *mod, unsigned int nsect, | 943 | static inline void add_sect_attrs(struct module *mod, unsigned int nsect, |
944 | char *sectstrings, Elf_Shdr *sechdrs) | 944 | char *sectstrings, Elf_Shdr *sechdrs) |
945 | { | 945 | { |
946 | } | 946 | } |
947 | 947 | ||
948 | static inline void remove_sect_attrs(struct module *mod) | 948 | static inline void remove_sect_attrs(struct module *mod) |
949 | { | 949 | { |
950 | } | 950 | } |
951 | #endif /* CONFIG_KALLSYMS */ | 951 | #endif /* CONFIG_KALLSYMS */ |
952 | 952 | ||
953 | static int module_add_modinfo_attrs(struct module *mod) | 953 | static int module_add_modinfo_attrs(struct module *mod) |
954 | { | 954 | { |
955 | struct module_attribute *attr; | 955 | struct module_attribute *attr; |
956 | struct module_attribute *temp_attr; | 956 | struct module_attribute *temp_attr; |
957 | int error = 0; | 957 | int error = 0; |
958 | int i; | 958 | int i; |
959 | 959 | ||
960 | mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) * | 960 | mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) * |
961 | (ARRAY_SIZE(modinfo_attrs) + 1)), | 961 | (ARRAY_SIZE(modinfo_attrs) + 1)), |
962 | GFP_KERNEL); | 962 | GFP_KERNEL); |
963 | if (!mod->modinfo_attrs) | 963 | if (!mod->modinfo_attrs) |
964 | return -ENOMEM; | 964 | return -ENOMEM; |
965 | 965 | ||
966 | temp_attr = mod->modinfo_attrs; | 966 | temp_attr = mod->modinfo_attrs; |
967 | for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { | 967 | for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { |
968 | if (!attr->test || | 968 | if (!attr->test || |
969 | (attr->test && attr->test(mod))) { | 969 | (attr->test && attr->test(mod))) { |
970 | memcpy(temp_attr, attr, sizeof(*temp_attr)); | 970 | memcpy(temp_attr, attr, sizeof(*temp_attr)); |
971 | temp_attr->attr.owner = mod; | 971 | temp_attr->attr.owner = mod; |
972 | error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr); | 972 | error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr); |
973 | ++temp_attr; | 973 | ++temp_attr; |
974 | } | 974 | } |
975 | } | 975 | } |
976 | return error; | 976 | return error; |
977 | } | 977 | } |
978 | 978 | ||
979 | static void module_remove_modinfo_attrs(struct module *mod) | 979 | static void module_remove_modinfo_attrs(struct module *mod) |
980 | { | 980 | { |
981 | struct module_attribute *attr; | 981 | struct module_attribute *attr; |
982 | int i; | 982 | int i; |
983 | 983 | ||
984 | for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { | 984 | for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { |
985 | /* pick a field to test for end of list */ | 985 | /* pick a field to test for end of list */ |
986 | if (!attr->attr.name) | 986 | if (!attr->attr.name) |
987 | break; | 987 | break; |
988 | sysfs_remove_file(&mod->mkobj.kobj,&attr->attr); | 988 | sysfs_remove_file(&mod->mkobj.kobj,&attr->attr); |
989 | if (attr->free) | 989 | if (attr->free) |
990 | attr->free(mod); | 990 | attr->free(mod); |
991 | } | 991 | } |
992 | kfree(mod->modinfo_attrs); | 992 | kfree(mod->modinfo_attrs); |
993 | } | 993 | } |
994 | 994 | ||
995 | static int mod_sysfs_setup(struct module *mod, | 995 | static int mod_sysfs_setup(struct module *mod, |
996 | struct kernel_param *kparam, | 996 | struct kernel_param *kparam, |
997 | unsigned int num_params) | 997 | unsigned int num_params) |
998 | { | 998 | { |
999 | int err; | 999 | int err; |
1000 | 1000 | ||
1001 | memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); | 1001 | memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); |
1002 | err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name); | 1002 | err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name); |
1003 | if (err) | 1003 | if (err) |
1004 | goto out; | 1004 | goto out; |
1005 | kobj_set_kset_s(&mod->mkobj, module_subsys); | 1005 | kobj_set_kset_s(&mod->mkobj, module_subsys); |
1006 | mod->mkobj.mod = mod; | 1006 | mod->mkobj.mod = mod; |
1007 | err = kobject_register(&mod->mkobj.kobj); | 1007 | err = kobject_register(&mod->mkobj.kobj); |
1008 | if (err) | 1008 | if (err) |
1009 | goto out; | 1009 | goto out; |
1010 | 1010 | ||
1011 | err = module_param_sysfs_setup(mod, kparam, num_params); | 1011 | err = module_param_sysfs_setup(mod, kparam, num_params); |
1012 | if (err) | 1012 | if (err) |
1013 | goto out_unreg; | 1013 | goto out_unreg; |
1014 | 1014 | ||
1015 | err = module_add_modinfo_attrs(mod); | 1015 | err = module_add_modinfo_attrs(mod); |
1016 | if (err) | 1016 | if (err) |
1017 | goto out_unreg; | 1017 | goto out_unreg; |
1018 | 1018 | ||
1019 | return 0; | 1019 | return 0; |
1020 | 1020 | ||
1021 | out_unreg: | 1021 | out_unreg: |
1022 | kobject_unregister(&mod->mkobj.kobj); | 1022 | kobject_unregister(&mod->mkobj.kobj); |
1023 | out: | 1023 | out: |
1024 | return err; | 1024 | return err; |
1025 | } | 1025 | } |
1026 | 1026 | ||
1027 | static void mod_kobject_remove(struct module *mod) | 1027 | static void mod_kobject_remove(struct module *mod) |
1028 | { | 1028 | { |
1029 | module_remove_modinfo_attrs(mod); | 1029 | module_remove_modinfo_attrs(mod); |
1030 | module_param_sysfs_remove(mod); | 1030 | module_param_sysfs_remove(mod); |
1031 | 1031 | ||
1032 | kobject_unregister(&mod->mkobj.kobj); | 1032 | kobject_unregister(&mod->mkobj.kobj); |
1033 | } | 1033 | } |
1034 | 1034 | ||
1035 | /* | 1035 | /* |
1036 | * unlink the module with the whole machine is stopped with interrupts off | 1036 | * unlink the module with the whole machine is stopped with interrupts off |
1037 | * - this defends against kallsyms not taking locks | 1037 | * - this defends against kallsyms not taking locks |
1038 | */ | 1038 | */ |
1039 | static int __unlink_module(void *_mod) | 1039 | static int __unlink_module(void *_mod) |
1040 | { | 1040 | { |
1041 | struct module *mod = _mod; | 1041 | struct module *mod = _mod; |
1042 | list_del(&mod->list); | 1042 | list_del(&mod->list); |
1043 | return 0; | 1043 | return 0; |
1044 | } | 1044 | } |
1045 | 1045 | ||
1046 | /* Free a module, remove from lists, etc (must hold module mutex). */ | 1046 | /* Free a module, remove from lists, etc (must hold module mutex). */ |
1047 | static void free_module(struct module *mod) | 1047 | static void free_module(struct module *mod) |
1048 | { | 1048 | { |
1049 | /* Delete from various lists */ | 1049 | /* Delete from various lists */ |
1050 | stop_machine_run(__unlink_module, mod, NR_CPUS); | 1050 | stop_machine_run(__unlink_module, mod, NR_CPUS); |
1051 | remove_sect_attrs(mod); | 1051 | remove_sect_attrs(mod); |
1052 | mod_kobject_remove(mod); | 1052 | mod_kobject_remove(mod); |
1053 | 1053 | ||
1054 | /* Arch-specific cleanup. */ | 1054 | /* Arch-specific cleanup. */ |
1055 | module_arch_cleanup(mod); | 1055 | module_arch_cleanup(mod); |
1056 | 1056 | ||
1057 | /* Module unload stuff */ | 1057 | /* Module unload stuff */ |
1058 | module_unload_free(mod); | 1058 | module_unload_free(mod); |
1059 | 1059 | ||
1060 | /* This may be NULL, but that's OK */ | 1060 | /* This may be NULL, but that's OK */ |
1061 | module_free(mod, mod->module_init); | 1061 | module_free(mod, mod->module_init); |
1062 | kfree(mod->args); | 1062 | kfree(mod->args); |
1063 | if (mod->percpu) | 1063 | if (mod->percpu) |
1064 | percpu_modfree(mod->percpu); | 1064 | percpu_modfree(mod->percpu); |
1065 | 1065 | ||
1066 | /* Finally, free the core (containing the module structure) */ | 1066 | /* Finally, free the core (containing the module structure) */ |
1067 | module_free(mod, mod->module_core); | 1067 | module_free(mod, mod->module_core); |
1068 | } | 1068 | } |
1069 | 1069 | ||
1070 | void *__symbol_get(const char *symbol) | 1070 | void *__symbol_get(const char *symbol) |
1071 | { | 1071 | { |
1072 | struct module *owner; | 1072 | struct module *owner; |
1073 | unsigned long value, flags; | 1073 | unsigned long value, flags; |
1074 | const unsigned long *crc; | 1074 | const unsigned long *crc; |
1075 | 1075 | ||
1076 | spin_lock_irqsave(&modlist_lock, flags); | 1076 | spin_lock_irqsave(&modlist_lock, flags); |
1077 | value = __find_symbol(symbol, &owner, &crc, 1); | 1077 | value = __find_symbol(symbol, &owner, &crc, 1); |
1078 | if (value && !strong_try_module_get(owner)) | 1078 | if (value && !strong_try_module_get(owner)) |
1079 | value = 0; | 1079 | value = 0; |
1080 | spin_unlock_irqrestore(&modlist_lock, flags); | 1080 | spin_unlock_irqrestore(&modlist_lock, flags); |
1081 | 1081 | ||
1082 | return (void *)value; | 1082 | return (void *)value; |
1083 | } | 1083 | } |
1084 | EXPORT_SYMBOL_GPL(__symbol_get); | 1084 | EXPORT_SYMBOL_GPL(__symbol_get); |
1085 | 1085 | ||
1086 | /* | 1086 | /* |
1087 | * Ensure that an exported symbol [global namespace] does not already exist | 1087 | * Ensure that an exported symbol [global namespace] does not already exist |
1088 | * in the Kernel or in some other modules exported symbol table. | 1088 | * in the Kernel or in some other modules exported symbol table. |
1089 | */ | 1089 | */ |
1090 | static int verify_export_symbols(struct module *mod) | 1090 | static int verify_export_symbols(struct module *mod) |
1091 | { | 1091 | { |
1092 | const char *name = NULL; | 1092 | const char *name = NULL; |
1093 | unsigned long i, ret = 0; | 1093 | unsigned long i, ret = 0; |
1094 | struct module *owner; | 1094 | struct module *owner; |
1095 | const unsigned long *crc; | 1095 | const unsigned long *crc; |
1096 | 1096 | ||
1097 | for (i = 0; i < mod->num_syms; i++) | 1097 | for (i = 0; i < mod->num_syms; i++) |
1098 | if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) { | 1098 | if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) { |
1099 | name = mod->syms[i].name; | 1099 | name = mod->syms[i].name; |
1100 | ret = -ENOEXEC; | 1100 | ret = -ENOEXEC; |
1101 | goto dup; | 1101 | goto dup; |
1102 | } | 1102 | } |
1103 | 1103 | ||
1104 | for (i = 0; i < mod->num_gpl_syms; i++) | 1104 | for (i = 0; i < mod->num_gpl_syms; i++) |
1105 | if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) { | 1105 | if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) { |
1106 | name = mod->gpl_syms[i].name; | 1106 | name = mod->gpl_syms[i].name; |
1107 | ret = -ENOEXEC; | 1107 | ret = -ENOEXEC; |
1108 | goto dup; | 1108 | goto dup; |
1109 | } | 1109 | } |
1110 | 1110 | ||
1111 | dup: | 1111 | dup: |
1112 | if (ret) | 1112 | if (ret) |
1113 | printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n", | 1113 | printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n", |
1114 | mod->name, name, module_name(owner)); | 1114 | mod->name, name, module_name(owner)); |
1115 | 1115 | ||
1116 | return ret; | 1116 | return ret; |
1117 | } | 1117 | } |
1118 | 1118 | ||
1119 | /* Change all symbols so that sh_value encodes the pointer directly. */ | 1119 | /* Change all symbols so that sh_value encodes the pointer directly. */ |
1120 | static int simplify_symbols(Elf_Shdr *sechdrs, | 1120 | static int simplify_symbols(Elf_Shdr *sechdrs, |
1121 | unsigned int symindex, | 1121 | unsigned int symindex, |
1122 | const char *strtab, | 1122 | const char *strtab, |
1123 | unsigned int versindex, | 1123 | unsigned int versindex, |
1124 | unsigned int pcpuindex, | 1124 | unsigned int pcpuindex, |
1125 | struct module *mod) | 1125 | struct module *mod) |
1126 | { | 1126 | { |
1127 | Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr; | 1127 | Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr; |
1128 | unsigned long secbase; | 1128 | unsigned long secbase; |
1129 | unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); | 1129 | unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); |
1130 | int ret = 0; | 1130 | int ret = 0; |
1131 | 1131 | ||
1132 | for (i = 1; i < n; i++) { | 1132 | for (i = 1; i < n; i++) { |
1133 | switch (sym[i].st_shndx) { | 1133 | switch (sym[i].st_shndx) { |
1134 | case SHN_COMMON: | 1134 | case SHN_COMMON: |
1135 | /* We compiled with -fno-common. These are not | 1135 | /* We compiled with -fno-common. These are not |
1136 | supposed to happen. */ | 1136 | supposed to happen. */ |
1137 | DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name); | 1137 | DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name); |
1138 | printk("%s: please compile with -fno-common\n", | 1138 | printk("%s: please compile with -fno-common\n", |
1139 | mod->name); | 1139 | mod->name); |
1140 | ret = -ENOEXEC; | 1140 | ret = -ENOEXEC; |
1141 | break; | 1141 | break; |
1142 | 1142 | ||
1143 | case SHN_ABS: | 1143 | case SHN_ABS: |
1144 | /* Don't need to do anything */ | 1144 | /* Don't need to do anything */ |
1145 | DEBUGP("Absolute symbol: 0x%08lx\n", | 1145 | DEBUGP("Absolute symbol: 0x%08lx\n", |
1146 | (long)sym[i].st_value); | 1146 | (long)sym[i].st_value); |
1147 | break; | 1147 | break; |
1148 | 1148 | ||
1149 | case SHN_UNDEF: | 1149 | case SHN_UNDEF: |
1150 | sym[i].st_value | 1150 | sym[i].st_value |
1151 | = resolve_symbol(sechdrs, versindex, | 1151 | = resolve_symbol(sechdrs, versindex, |
1152 | strtab + sym[i].st_name, mod); | 1152 | strtab + sym[i].st_name, mod); |
1153 | 1153 | ||
1154 | /* Ok if resolved. */ | 1154 | /* Ok if resolved. */ |
1155 | if (sym[i].st_value != 0) | 1155 | if (sym[i].st_value != 0) |
1156 | break; | 1156 | break; |
1157 | /* Ok if weak. */ | 1157 | /* Ok if weak. */ |
1158 | if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) | 1158 | if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) |
1159 | break; | 1159 | break; |
1160 | 1160 | ||
1161 | printk(KERN_WARNING "%s: Unknown symbol %s\n", | 1161 | printk(KERN_WARNING "%s: Unknown symbol %s\n", |
1162 | mod->name, strtab + sym[i].st_name); | 1162 | mod->name, strtab + sym[i].st_name); |
1163 | ret = -ENOENT; | 1163 | ret = -ENOENT; |
1164 | break; | 1164 | break; |
1165 | 1165 | ||
1166 | default: | 1166 | default: |
1167 | /* Divert to percpu allocation if a percpu var. */ | 1167 | /* Divert to percpu allocation if a percpu var. */ |
1168 | if (sym[i].st_shndx == pcpuindex) | 1168 | if (sym[i].st_shndx == pcpuindex) |
1169 | secbase = (unsigned long)mod->percpu; | 1169 | secbase = (unsigned long)mod->percpu; |
1170 | else | 1170 | else |
1171 | secbase = sechdrs[sym[i].st_shndx].sh_addr; | 1171 | secbase = sechdrs[sym[i].st_shndx].sh_addr; |
1172 | sym[i].st_value += secbase; | 1172 | sym[i].st_value += secbase; |
1173 | break; | 1173 | break; |
1174 | } | 1174 | } |
1175 | } | 1175 | } |
1176 | 1176 | ||
1177 | return ret; | 1177 | return ret; |
1178 | } | 1178 | } |
1179 | 1179 | ||
1180 | /* Update size with this section: return offset. */ | 1180 | /* Update size with this section: return offset. */ |
1181 | static long get_offset(unsigned long *size, Elf_Shdr *sechdr) | 1181 | static long get_offset(unsigned long *size, Elf_Shdr *sechdr) |
1182 | { | 1182 | { |
1183 | long ret; | 1183 | long ret; |
1184 | 1184 | ||
1185 | ret = ALIGN(*size, sechdr->sh_addralign ?: 1); | 1185 | ret = ALIGN(*size, sechdr->sh_addralign ?: 1); |
1186 | *size = ret + sechdr->sh_size; | 1186 | *size = ret + sechdr->sh_size; |
1187 | return ret; | 1187 | return ret; |
1188 | } | 1188 | } |
1189 | 1189 | ||
1190 | /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld | 1190 | /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld |
1191 | might -- code, read-only data, read-write data, small data. Tally | 1191 | might -- code, read-only data, read-write data, small data. Tally |
1192 | sizes, and place the offsets into sh_entsize fields: high bit means it | 1192 | sizes, and place the offsets into sh_entsize fields: high bit means it |
1193 | belongs in init. */ | 1193 | belongs in init. */ |
1194 | static void layout_sections(struct module *mod, | 1194 | static void layout_sections(struct module *mod, |
1195 | const Elf_Ehdr *hdr, | 1195 | const Elf_Ehdr *hdr, |
1196 | Elf_Shdr *sechdrs, | 1196 | Elf_Shdr *sechdrs, |
1197 | const char *secstrings) | 1197 | const char *secstrings) |
1198 | { | 1198 | { |
1199 | static unsigned long const masks[][2] = { | 1199 | static unsigned long const masks[][2] = { |
1200 | /* NOTE: all executable code must be the first section | 1200 | /* NOTE: all executable code must be the first section |
1201 | * in this array; otherwise modify the text_size | 1201 | * in this array; otherwise modify the text_size |
1202 | * finder in the two loops below */ | 1202 | * finder in the two loops below */ |
1203 | { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, | 1203 | { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, |
1204 | { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL }, | 1204 | { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL }, |
1205 | { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, | 1205 | { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, |
1206 | { ARCH_SHF_SMALL | SHF_ALLOC, 0 } | 1206 | { ARCH_SHF_SMALL | SHF_ALLOC, 0 } |
1207 | }; | 1207 | }; |
1208 | unsigned int m, i; | 1208 | unsigned int m, i; |
1209 | 1209 | ||
1210 | for (i = 0; i < hdr->e_shnum; i++) | 1210 | for (i = 0; i < hdr->e_shnum; i++) |
1211 | sechdrs[i].sh_entsize = ~0UL; | 1211 | sechdrs[i].sh_entsize = ~0UL; |
1212 | 1212 | ||
1213 | DEBUGP("Core section allocation order:\n"); | 1213 | DEBUGP("Core section allocation order:\n"); |
1214 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { | 1214 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { |
1215 | for (i = 0; i < hdr->e_shnum; ++i) { | 1215 | for (i = 0; i < hdr->e_shnum; ++i) { |
1216 | Elf_Shdr *s = &sechdrs[i]; | 1216 | Elf_Shdr *s = &sechdrs[i]; |
1217 | 1217 | ||
1218 | if ((s->sh_flags & masks[m][0]) != masks[m][0] | 1218 | if ((s->sh_flags & masks[m][0]) != masks[m][0] |
1219 | || (s->sh_flags & masks[m][1]) | 1219 | || (s->sh_flags & masks[m][1]) |
1220 | || s->sh_entsize != ~0UL | 1220 | || s->sh_entsize != ~0UL |
1221 | || strncmp(secstrings + s->sh_name, | 1221 | || strncmp(secstrings + s->sh_name, |
1222 | ".init", 5) == 0) | 1222 | ".init", 5) == 0) |
1223 | continue; | 1223 | continue; |
1224 | s->sh_entsize = get_offset(&mod->core_size, s); | 1224 | s->sh_entsize = get_offset(&mod->core_size, s); |
1225 | DEBUGP("\t%s\n", secstrings + s->sh_name); | 1225 | DEBUGP("\t%s\n", secstrings + s->sh_name); |
1226 | } | 1226 | } |
1227 | if (m == 0) | 1227 | if (m == 0) |
1228 | mod->core_text_size = mod->core_size; | 1228 | mod->core_text_size = mod->core_size; |
1229 | } | 1229 | } |
1230 | 1230 | ||
1231 | DEBUGP("Init section allocation order:\n"); | 1231 | DEBUGP("Init section allocation order:\n"); |
1232 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { | 1232 | for (m = 0; m < ARRAY_SIZE(masks); ++m) { |
1233 | for (i = 0; i < hdr->e_shnum; ++i) { | 1233 | for (i = 0; i < hdr->e_shnum; ++i) { |
1234 | Elf_Shdr *s = &sechdrs[i]; | 1234 | Elf_Shdr *s = &sechdrs[i]; |
1235 | 1235 | ||
1236 | if ((s->sh_flags & masks[m][0]) != masks[m][0] | 1236 | if ((s->sh_flags & masks[m][0]) != masks[m][0] |
1237 | || (s->sh_flags & masks[m][1]) | 1237 | || (s->sh_flags & masks[m][1]) |
1238 | || s->sh_entsize != ~0UL | 1238 | || s->sh_entsize != ~0UL |
1239 | || strncmp(secstrings + s->sh_name, | 1239 | || strncmp(secstrings + s->sh_name, |
1240 | ".init", 5) != 0) | 1240 | ".init", 5) != 0) |
1241 | continue; | 1241 | continue; |
1242 | s->sh_entsize = (get_offset(&mod->init_size, s) | 1242 | s->sh_entsize = (get_offset(&mod->init_size, s) |
1243 | | INIT_OFFSET_MASK); | 1243 | | INIT_OFFSET_MASK); |
1244 | DEBUGP("\t%s\n", secstrings + s->sh_name); | 1244 | DEBUGP("\t%s\n", secstrings + s->sh_name); |
1245 | } | 1245 | } |
1246 | if (m == 0) | 1246 | if (m == 0) |
1247 | mod->init_text_size = mod->init_size; | 1247 | mod->init_text_size = mod->init_size; |
1248 | } | 1248 | } |
1249 | } | 1249 | } |
1250 | 1250 | ||
1251 | static inline int license_is_gpl_compatible(const char *license) | 1251 | static inline int license_is_gpl_compatible(const char *license) |
1252 | { | 1252 | { |
1253 | return (strcmp(license, "GPL") == 0 | 1253 | return (strcmp(license, "GPL") == 0 |
1254 | || strcmp(license, "GPL v2") == 0 | 1254 | || strcmp(license, "GPL v2") == 0 |
1255 | || strcmp(license, "GPL and additional rights") == 0 | 1255 | || strcmp(license, "GPL and additional rights") == 0 |
1256 | || strcmp(license, "Dual BSD/GPL") == 0 | 1256 | || strcmp(license, "Dual BSD/GPL") == 0 |
1257 | || strcmp(license, "Dual MIT/GPL") == 0 | 1257 | || strcmp(license, "Dual MIT/GPL") == 0 |
1258 | || strcmp(license, "Dual MPL/GPL") == 0); | 1258 | || strcmp(license, "Dual MPL/GPL") == 0); |
1259 | } | 1259 | } |
1260 | 1260 | ||
1261 | static void set_license(struct module *mod, const char *license) | 1261 | static void set_license(struct module *mod, const char *license) |
1262 | { | 1262 | { |
1263 | if (!license) | 1263 | if (!license) |
1264 | license = "unspecified"; | 1264 | license = "unspecified"; |
1265 | 1265 | ||
1266 | mod->license_gplok = license_is_gpl_compatible(license); | 1266 | mod->license_gplok = license_is_gpl_compatible(license); |
1267 | if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) { | 1267 | if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) { |
1268 | printk(KERN_WARNING "%s: module license '%s' taints kernel.\n", | 1268 | printk(KERN_WARNING "%s: module license '%s' taints kernel.\n", |
1269 | mod->name, license); | 1269 | mod->name, license); |
1270 | add_taint(TAINT_PROPRIETARY_MODULE); | 1270 | add_taint(TAINT_PROPRIETARY_MODULE); |
1271 | } | 1271 | } |
1272 | } | 1272 | } |
1273 | 1273 | ||
1274 | /* Parse tag=value strings from .modinfo section */ | 1274 | /* Parse tag=value strings from .modinfo section */ |
1275 | static char *next_string(char *string, unsigned long *secsize) | 1275 | static char *next_string(char *string, unsigned long *secsize) |
1276 | { | 1276 | { |
1277 | /* Skip non-zero chars */ | 1277 | /* Skip non-zero chars */ |
1278 | while (string[0]) { | 1278 | while (string[0]) { |
1279 | string++; | 1279 | string++; |
1280 | if ((*secsize)-- <= 1) | 1280 | if ((*secsize)-- <= 1) |
1281 | return NULL; | 1281 | return NULL; |
1282 | } | 1282 | } |
1283 | 1283 | ||
1284 | /* Skip any zero padding. */ | 1284 | /* Skip any zero padding. */ |
1285 | while (!string[0]) { | 1285 | while (!string[0]) { |
1286 | string++; | 1286 | string++; |
1287 | if ((*secsize)-- <= 1) | 1287 | if ((*secsize)-- <= 1) |
1288 | return NULL; | 1288 | return NULL; |
1289 | } | 1289 | } |
1290 | return string; | 1290 | return string; |
1291 | } | 1291 | } |
1292 | 1292 | ||
1293 | static char *get_modinfo(Elf_Shdr *sechdrs, | 1293 | static char *get_modinfo(Elf_Shdr *sechdrs, |
1294 | unsigned int info, | 1294 | unsigned int info, |
1295 | const char *tag) | 1295 | const char *tag) |
1296 | { | 1296 | { |
1297 | char *p; | 1297 | char *p; |
1298 | unsigned int taglen = strlen(tag); | 1298 | unsigned int taglen = strlen(tag); |
1299 | unsigned long size = sechdrs[info].sh_size; | 1299 | unsigned long size = sechdrs[info].sh_size; |
1300 | 1300 | ||
1301 | for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) { | 1301 | for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) { |
1302 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') | 1302 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
1303 | return p + taglen + 1; | 1303 | return p + taglen + 1; |
1304 | } | 1304 | } |
1305 | return NULL; | 1305 | return NULL; |
1306 | } | 1306 | } |
1307 | 1307 | ||
1308 | static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs, | 1308 | static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs, |
1309 | unsigned int infoindex) | 1309 | unsigned int infoindex) |
1310 | { | 1310 | { |
1311 | struct module_attribute *attr; | 1311 | struct module_attribute *attr; |
1312 | int i; | 1312 | int i; |
1313 | 1313 | ||
1314 | for (i = 0; (attr = modinfo_attrs[i]); i++) { | 1314 | for (i = 0; (attr = modinfo_attrs[i]); i++) { |
1315 | if (attr->setup) | 1315 | if (attr->setup) |
1316 | attr->setup(mod, | 1316 | attr->setup(mod, |
1317 | get_modinfo(sechdrs, | 1317 | get_modinfo(sechdrs, |
1318 | infoindex, | 1318 | infoindex, |
1319 | attr->attr.name)); | 1319 | attr->attr.name)); |
1320 | } | 1320 | } |
1321 | } | 1321 | } |
1322 | 1322 | ||
1323 | #ifdef CONFIG_KALLSYMS | 1323 | #ifdef CONFIG_KALLSYMS |
1324 | int is_exported(const char *name, const struct module *mod) | 1324 | int is_exported(const char *name, const struct module *mod) |
1325 | { | 1325 | { |
1326 | if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab)) | 1326 | if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab)) |
1327 | return 1; | 1327 | return 1; |
1328 | else | 1328 | else |
1329 | if (lookup_symbol(name, mod->syms, mod->syms + mod->num_syms)) | 1329 | if (lookup_symbol(name, mod->syms, mod->syms + mod->num_syms)) |
1330 | return 1; | 1330 | return 1; |
1331 | else | 1331 | else |
1332 | return 0; | 1332 | return 0; |
1333 | } | 1333 | } |
1334 | 1334 | ||
1335 | /* As per nm */ | 1335 | /* As per nm */ |
1336 | static char elf_type(const Elf_Sym *sym, | 1336 | static char elf_type(const Elf_Sym *sym, |
1337 | Elf_Shdr *sechdrs, | 1337 | Elf_Shdr *sechdrs, |
1338 | const char *secstrings, | 1338 | const char *secstrings, |
1339 | struct module *mod) | 1339 | struct module *mod) |
1340 | { | 1340 | { |
1341 | if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { | 1341 | if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { |
1342 | if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) | 1342 | if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) |
1343 | return 'v'; | 1343 | return 'v'; |
1344 | else | 1344 | else |
1345 | return 'w'; | 1345 | return 'w'; |
1346 | } | 1346 | } |
1347 | if (sym->st_shndx == SHN_UNDEF) | 1347 | if (sym->st_shndx == SHN_UNDEF) |
1348 | return 'U'; | 1348 | return 'U'; |
1349 | if (sym->st_shndx == SHN_ABS) | 1349 | if (sym->st_shndx == SHN_ABS) |
1350 | return 'a'; | 1350 | return 'a'; |
1351 | if (sym->st_shndx >= SHN_LORESERVE) | 1351 | if (sym->st_shndx >= SHN_LORESERVE) |
1352 | return '?'; | 1352 | return '?'; |
1353 | if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) | 1353 | if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) |
1354 | return 't'; | 1354 | return 't'; |
1355 | if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC | 1355 | if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC |
1356 | && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) { | 1356 | && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) { |
1357 | if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) | 1357 | if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) |
1358 | return 'r'; | 1358 | return 'r'; |
1359 | else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) | 1359 | else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) |
1360 | return 'g'; | 1360 | return 'g'; |
1361 | else | 1361 | else |
1362 | return 'd'; | 1362 | return 'd'; |
1363 | } | 1363 | } |
1364 | if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { | 1364 | if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { |
1365 | if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) | 1365 | if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) |
1366 | return 's'; | 1366 | return 's'; |
1367 | else | 1367 | else |
1368 | return 'b'; | 1368 | return 'b'; |
1369 | } | 1369 | } |
1370 | if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name, | 1370 | if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name, |
1371 | ".debug", strlen(".debug")) == 0) | 1371 | ".debug", strlen(".debug")) == 0) |
1372 | return 'n'; | 1372 | return 'n'; |
1373 | return '?'; | 1373 | return '?'; |
1374 | } | 1374 | } |
1375 | 1375 | ||
1376 | static void add_kallsyms(struct module *mod, | 1376 | static void add_kallsyms(struct module *mod, |
1377 | Elf_Shdr *sechdrs, | 1377 | Elf_Shdr *sechdrs, |
1378 | unsigned int symindex, | 1378 | unsigned int symindex, |
1379 | unsigned int strindex, | 1379 | unsigned int strindex, |
1380 | const char *secstrings) | 1380 | const char *secstrings) |
1381 | { | 1381 | { |
1382 | unsigned int i; | 1382 | unsigned int i; |
1383 | 1383 | ||
1384 | mod->symtab = (void *)sechdrs[symindex].sh_addr; | 1384 | mod->symtab = (void *)sechdrs[symindex].sh_addr; |
1385 | mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym); | 1385 | mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym); |
1386 | mod->strtab = (void *)sechdrs[strindex].sh_addr; | 1386 | mod->strtab = (void *)sechdrs[strindex].sh_addr; |
1387 | 1387 | ||
1388 | /* Set types up while we still have access to sections. */ | 1388 | /* Set types up while we still have access to sections. */ |
1389 | for (i = 0; i < mod->num_symtab; i++) | 1389 | for (i = 0; i < mod->num_symtab; i++) |
1390 | mod->symtab[i].st_info | 1390 | mod->symtab[i].st_info |
1391 | = elf_type(&mod->symtab[i], sechdrs, secstrings, mod); | 1391 | = elf_type(&mod->symtab[i], sechdrs, secstrings, mod); |
1392 | } | 1392 | } |
1393 | #else | 1393 | #else |
1394 | static inline void add_kallsyms(struct module *mod, | 1394 | static inline void add_kallsyms(struct module *mod, |
1395 | Elf_Shdr *sechdrs, | 1395 | Elf_Shdr *sechdrs, |
1396 | unsigned int symindex, | 1396 | unsigned int symindex, |
1397 | unsigned int strindex, | 1397 | unsigned int strindex, |
1398 | const char *secstrings) | 1398 | const char *secstrings) |
1399 | { | 1399 | { |
1400 | } | 1400 | } |
1401 | #endif /* CONFIG_KALLSYMS */ | 1401 | #endif /* CONFIG_KALLSYMS */ |
1402 | 1402 | ||
1403 | /* Allocate and load the module: note that size of section 0 is always | 1403 | /* Allocate and load the module: note that size of section 0 is always |
1404 | zero, and we rely on this for optional sections. */ | 1404 | zero, and we rely on this for optional sections. */ |
1405 | static struct module *load_module(void __user *umod, | 1405 | static struct module *load_module(void __user *umod, |
1406 | unsigned long len, | 1406 | unsigned long len, |
1407 | const char __user *uargs) | 1407 | const char __user *uargs) |
1408 | { | 1408 | { |
1409 | Elf_Ehdr *hdr; | 1409 | Elf_Ehdr *hdr; |
1410 | Elf_Shdr *sechdrs; | 1410 | Elf_Shdr *sechdrs; |
1411 | char *secstrings, *args, *modmagic, *strtab = NULL; | 1411 | char *secstrings, *args, *modmagic, *strtab = NULL; |
1412 | unsigned int i, symindex = 0, strindex = 0, setupindex, exindex, | 1412 | unsigned int i, symindex = 0, strindex = 0, setupindex, exindex, |
1413 | exportindex, modindex, obsparmindex, infoindex, gplindex, | 1413 | exportindex, modindex, obsparmindex, infoindex, gplindex, |
1414 | crcindex, gplcrcindex, versindex, pcpuindex, gplfutureindex, | 1414 | crcindex, gplcrcindex, versindex, pcpuindex, gplfutureindex, |
1415 | gplfuturecrcindex; | 1415 | gplfuturecrcindex; |
1416 | struct module *mod; | 1416 | struct module *mod; |
1417 | long err = 0; | 1417 | long err = 0; |
1418 | void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ | 1418 | void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ |
1419 | struct exception_table_entry *extable; | 1419 | struct exception_table_entry *extable; |
1420 | mm_segment_t old_fs; | 1420 | mm_segment_t old_fs; |
1421 | 1421 | ||
1422 | DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", | 1422 | DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", |
1423 | umod, len, uargs); | 1423 | umod, len, uargs); |
1424 | if (len < sizeof(*hdr)) | 1424 | if (len < sizeof(*hdr)) |
1425 | return ERR_PTR(-ENOEXEC); | 1425 | return ERR_PTR(-ENOEXEC); |
1426 | 1426 | ||
1427 | /* Suck in entire file: we'll want most of it. */ | 1427 | /* Suck in entire file: we'll want most of it. */ |
1428 | /* vmalloc barfs on "unusual" numbers. Check here */ | 1428 | /* vmalloc barfs on "unusual" numbers. Check here */ |
1429 | if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) | 1429 | if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) |
1430 | return ERR_PTR(-ENOMEM); | 1430 | return ERR_PTR(-ENOMEM); |
1431 | if (copy_from_user(hdr, umod, len) != 0) { | 1431 | if (copy_from_user(hdr, umod, len) != 0) { |
1432 | err = -EFAULT; | 1432 | err = -EFAULT; |
1433 | goto free_hdr; | 1433 | goto free_hdr; |
1434 | } | 1434 | } |
1435 | 1435 | ||
1436 | /* Sanity checks against insmoding binaries or wrong arch, | 1436 | /* Sanity checks against insmoding binaries or wrong arch, |
1437 | weird elf version */ | 1437 | weird elf version */ |
1438 | if (memcmp(hdr->e_ident, ELFMAG, 4) != 0 | 1438 | if (memcmp(hdr->e_ident, ELFMAG, 4) != 0 |
1439 | || hdr->e_type != ET_REL | 1439 | || hdr->e_type != ET_REL |
1440 | || !elf_check_arch(hdr) | 1440 | || !elf_check_arch(hdr) |
1441 | || hdr->e_shentsize != sizeof(*sechdrs)) { | 1441 | || hdr->e_shentsize != sizeof(*sechdrs)) { |
1442 | err = -ENOEXEC; | 1442 | err = -ENOEXEC; |
1443 | goto free_hdr; | 1443 | goto free_hdr; |
1444 | } | 1444 | } |
1445 | 1445 | ||
1446 | if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) | 1446 | if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) |
1447 | goto truncated; | 1447 | goto truncated; |
1448 | 1448 | ||
1449 | /* Convenience variables */ | 1449 | /* Convenience variables */ |
1450 | sechdrs = (void *)hdr + hdr->e_shoff; | 1450 | sechdrs = (void *)hdr + hdr->e_shoff; |
1451 | secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; | 1451 | secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; |
1452 | sechdrs[0].sh_addr = 0; | 1452 | sechdrs[0].sh_addr = 0; |
1453 | 1453 | ||
1454 | for (i = 1; i < hdr->e_shnum; i++) { | 1454 | for (i = 1; i < hdr->e_shnum; i++) { |
1455 | if (sechdrs[i].sh_type != SHT_NOBITS | 1455 | if (sechdrs[i].sh_type != SHT_NOBITS |
1456 | && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) | 1456 | && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) |
1457 | goto truncated; | 1457 | goto truncated; |
1458 | 1458 | ||
1459 | /* Mark all sections sh_addr with their address in the | 1459 | /* Mark all sections sh_addr with their address in the |
1460 | temporary image. */ | 1460 | temporary image. */ |
1461 | sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset; | 1461 | sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset; |
1462 | 1462 | ||
1463 | /* Internal symbols and strings. */ | 1463 | /* Internal symbols and strings. */ |
1464 | if (sechdrs[i].sh_type == SHT_SYMTAB) { | 1464 | if (sechdrs[i].sh_type == SHT_SYMTAB) { |
1465 | symindex = i; | 1465 | symindex = i; |
1466 | strindex = sechdrs[i].sh_link; | 1466 | strindex = sechdrs[i].sh_link; |
1467 | strtab = (char *)hdr + sechdrs[strindex].sh_offset; | 1467 | strtab = (char *)hdr + sechdrs[strindex].sh_offset; |
1468 | } | 1468 | } |
1469 | #ifndef CONFIG_MODULE_UNLOAD | 1469 | #ifndef CONFIG_MODULE_UNLOAD |
1470 | /* Don't load .exit sections */ | 1470 | /* Don't load .exit sections */ |
1471 | if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0) | 1471 | if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0) |
1472 | sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; | 1472 | sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; |
1473 | #endif | 1473 | #endif |
1474 | } | 1474 | } |
1475 | 1475 | ||
1476 | modindex = find_sec(hdr, sechdrs, secstrings, | 1476 | modindex = find_sec(hdr, sechdrs, secstrings, |
1477 | ".gnu.linkonce.this_module"); | 1477 | ".gnu.linkonce.this_module"); |
1478 | if (!modindex) { | 1478 | if (!modindex) { |
1479 | printk(KERN_WARNING "No module found in object\n"); | 1479 | printk(KERN_WARNING "No module found in object\n"); |
1480 | err = -ENOEXEC; | 1480 | err = -ENOEXEC; |
1481 | goto free_hdr; | 1481 | goto free_hdr; |
1482 | } | 1482 | } |
1483 | mod = (void *)sechdrs[modindex].sh_addr; | 1483 | mod = (void *)sechdrs[modindex].sh_addr; |
1484 | 1484 | ||
1485 | if (symindex == 0) { | 1485 | if (symindex == 0) { |
1486 | printk(KERN_WARNING "%s: module has no symbols (stripped?)\n", | 1486 | printk(KERN_WARNING "%s: module has no symbols (stripped?)\n", |
1487 | mod->name); | 1487 | mod->name); |
1488 | err = -ENOEXEC; | 1488 | err = -ENOEXEC; |
1489 | goto free_hdr; | 1489 | goto free_hdr; |
1490 | } | 1490 | } |
1491 | 1491 | ||
1492 | /* Optional sections */ | 1492 | /* Optional sections */ |
1493 | exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab"); | 1493 | exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab"); |
1494 | gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl"); | 1494 | gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl"); |
1495 | gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future"); | 1495 | gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future"); |
1496 | crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab"); | 1496 | crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab"); |
1497 | gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl"); | 1497 | gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl"); |
1498 | gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future"); | 1498 | gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future"); |
1499 | setupindex = find_sec(hdr, sechdrs, secstrings, "__param"); | 1499 | setupindex = find_sec(hdr, sechdrs, secstrings, "__param"); |
1500 | exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table"); | 1500 | exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table"); |
1501 | obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm"); | 1501 | obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm"); |
1502 | versindex = find_sec(hdr, sechdrs, secstrings, "__versions"); | 1502 | versindex = find_sec(hdr, sechdrs, secstrings, "__versions"); |
1503 | infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo"); | 1503 | infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo"); |
1504 | pcpuindex = find_pcpusec(hdr, sechdrs, secstrings); | 1504 | pcpuindex = find_pcpusec(hdr, sechdrs, secstrings); |
1505 | 1505 | ||
1506 | /* Don't keep modinfo section */ | 1506 | /* Don't keep modinfo section */ |
1507 | sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC; | 1507 | sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC; |
1508 | #ifdef CONFIG_KALLSYMS | 1508 | #ifdef CONFIG_KALLSYMS |
1509 | /* Keep symbol and string tables for decoding later. */ | 1509 | /* Keep symbol and string tables for decoding later. */ |
1510 | sechdrs[symindex].sh_flags |= SHF_ALLOC; | 1510 | sechdrs[symindex].sh_flags |= SHF_ALLOC; |
1511 | sechdrs[strindex].sh_flags |= SHF_ALLOC; | 1511 | sechdrs[strindex].sh_flags |= SHF_ALLOC; |
1512 | #endif | 1512 | #endif |
1513 | 1513 | ||
1514 | /* Check module struct version now, before we try to use module. */ | 1514 | /* Check module struct version now, before we try to use module. */ |
1515 | if (!check_modstruct_version(sechdrs, versindex, mod)) { | 1515 | if (!check_modstruct_version(sechdrs, versindex, mod)) { |
1516 | err = -ENOEXEC; | 1516 | err = -ENOEXEC; |
1517 | goto free_hdr; | 1517 | goto free_hdr; |
1518 | } | 1518 | } |
1519 | 1519 | ||
1520 | modmagic = get_modinfo(sechdrs, infoindex, "vermagic"); | 1520 | modmagic = get_modinfo(sechdrs, infoindex, "vermagic"); |
1521 | /* This is allowed: modprobe --force will invalidate it. */ | 1521 | /* This is allowed: modprobe --force will invalidate it. */ |
1522 | if (!modmagic) { | 1522 | if (!modmagic) { |
1523 | add_taint(TAINT_FORCED_MODULE); | 1523 | add_taint(TAINT_FORCED_MODULE); |
1524 | printk(KERN_WARNING "%s: no version magic, tainting kernel.\n", | 1524 | printk(KERN_WARNING "%s: no version magic, tainting kernel.\n", |
1525 | mod->name); | 1525 | mod->name); |
1526 | } else if (!same_magic(modmagic, vermagic)) { | 1526 | } else if (!same_magic(modmagic, vermagic)) { |
1527 | printk(KERN_ERR "%s: version magic '%s' should be '%s'\n", | 1527 | printk(KERN_ERR "%s: version magic '%s' should be '%s'\n", |
1528 | mod->name, modmagic, vermagic); | 1528 | mod->name, modmagic, vermagic); |
1529 | err = -ENOEXEC; | 1529 | err = -ENOEXEC; |
1530 | goto free_hdr; | 1530 | goto free_hdr; |
1531 | } | 1531 | } |
1532 | 1532 | ||
1533 | /* Now copy in args */ | 1533 | /* Now copy in args */ |
1534 | args = strndup_user(uargs, ~0UL >> 1); | 1534 | args = strndup_user(uargs, ~0UL >> 1); |
1535 | if (IS_ERR(args)) { | 1535 | if (IS_ERR(args)) { |
1536 | err = PTR_ERR(args); | 1536 | err = PTR_ERR(args); |
1537 | goto free_hdr; | 1537 | goto free_hdr; |
1538 | } | 1538 | } |
1539 | 1539 | ||
1540 | if (find_module(mod->name)) { | 1540 | if (find_module(mod->name)) { |
1541 | err = -EEXIST; | 1541 | err = -EEXIST; |
1542 | goto free_mod; | 1542 | goto free_mod; |
1543 | } | 1543 | } |
1544 | 1544 | ||
1545 | mod->state = MODULE_STATE_COMING; | 1545 | mod->state = MODULE_STATE_COMING; |
1546 | 1546 | ||
1547 | /* Allow arches to frob section contents and sizes. */ | 1547 | /* Allow arches to frob section contents and sizes. */ |
1548 | err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod); | 1548 | err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod); |
1549 | if (err < 0) | 1549 | if (err < 0) |
1550 | goto free_mod; | 1550 | goto free_mod; |
1551 | 1551 | ||
1552 | if (pcpuindex) { | 1552 | if (pcpuindex) { |
1553 | /* We have a special allocation for this section. */ | 1553 | /* We have a special allocation for this section. */ |
1554 | percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size, | 1554 | percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size, |
1555 | sechdrs[pcpuindex].sh_addralign, | 1555 | sechdrs[pcpuindex].sh_addralign, |
1556 | mod->name); | 1556 | mod->name); |
1557 | if (!percpu) { | 1557 | if (!percpu) { |
1558 | err = -ENOMEM; | 1558 | err = -ENOMEM; |
1559 | goto free_mod; | 1559 | goto free_mod; |
1560 | } | 1560 | } |
1561 | sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; | 1561 | sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; |
1562 | mod->percpu = percpu; | 1562 | mod->percpu = percpu; |
1563 | } | 1563 | } |
1564 | 1564 | ||
1565 | /* Determine total sizes, and put offsets in sh_entsize. For now | 1565 | /* Determine total sizes, and put offsets in sh_entsize. For now |
1566 | this is done generically; there doesn't appear to be any | 1566 | this is done generically; there doesn't appear to be any |
1567 | special cases for the architectures. */ | 1567 | special cases for the architectures. */ |
1568 | layout_sections(mod, hdr, sechdrs, secstrings); | 1568 | layout_sections(mod, hdr, sechdrs, secstrings); |
1569 | 1569 | ||
1570 | /* Do the allocs. */ | 1570 | /* Do the allocs. */ |
1571 | ptr = module_alloc(mod->core_size); | 1571 | ptr = module_alloc(mod->core_size); |
1572 | if (!ptr) { | 1572 | if (!ptr) { |
1573 | err = -ENOMEM; | 1573 | err = -ENOMEM; |
1574 | goto free_percpu; | 1574 | goto free_percpu; |
1575 | } | 1575 | } |
1576 | memset(ptr, 0, mod->core_size); | 1576 | memset(ptr, 0, mod->core_size); |
1577 | mod->module_core = ptr; | 1577 | mod->module_core = ptr; |
1578 | 1578 | ||
1579 | ptr = module_alloc(mod->init_size); | 1579 | ptr = module_alloc(mod->init_size); |
1580 | if (!ptr && mod->init_size) { | 1580 | if (!ptr && mod->init_size) { |
1581 | err = -ENOMEM; | 1581 | err = -ENOMEM; |
1582 | goto free_core; | 1582 | goto free_core; |
1583 | } | 1583 | } |
1584 | memset(ptr, 0, mod->init_size); | 1584 | memset(ptr, 0, mod->init_size); |
1585 | mod->module_init = ptr; | 1585 | mod->module_init = ptr; |
1586 | 1586 | ||
1587 | /* Transfer each section which specifies SHF_ALLOC */ | 1587 | /* Transfer each section which specifies SHF_ALLOC */ |
1588 | DEBUGP("final section addresses:\n"); | 1588 | DEBUGP("final section addresses:\n"); |
1589 | for (i = 0; i < hdr->e_shnum; i++) { | 1589 | for (i = 0; i < hdr->e_shnum; i++) { |
1590 | void *dest; | 1590 | void *dest; |
1591 | 1591 | ||
1592 | if (!(sechdrs[i].sh_flags & SHF_ALLOC)) | 1592 | if (!(sechdrs[i].sh_flags & SHF_ALLOC)) |
1593 | continue; | 1593 | continue; |
1594 | 1594 | ||
1595 | if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK) | 1595 | if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK) |
1596 | dest = mod->module_init | 1596 | dest = mod->module_init |
1597 | + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK); | 1597 | + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK); |
1598 | else | 1598 | else |
1599 | dest = mod->module_core + sechdrs[i].sh_entsize; | 1599 | dest = mod->module_core + sechdrs[i].sh_entsize; |
1600 | 1600 | ||
1601 | if (sechdrs[i].sh_type != SHT_NOBITS) | 1601 | if (sechdrs[i].sh_type != SHT_NOBITS) |
1602 | memcpy(dest, (void *)sechdrs[i].sh_addr, | 1602 | memcpy(dest, (void *)sechdrs[i].sh_addr, |
1603 | sechdrs[i].sh_size); | 1603 | sechdrs[i].sh_size); |
1604 | /* Update sh_addr to point to copy in image. */ | 1604 | /* Update sh_addr to point to copy in image. */ |
1605 | sechdrs[i].sh_addr = (unsigned long)dest; | 1605 | sechdrs[i].sh_addr = (unsigned long)dest; |
1606 | DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name); | 1606 | DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name); |
1607 | } | 1607 | } |
1608 | /* Module has been moved. */ | 1608 | /* Module has been moved. */ |
1609 | mod = (void *)sechdrs[modindex].sh_addr; | 1609 | mod = (void *)sechdrs[modindex].sh_addr; |
1610 | 1610 | ||
1611 | /* Now we've moved module, initialize linked lists, etc. */ | 1611 | /* Now we've moved module, initialize linked lists, etc. */ |
1612 | module_unload_init(mod); | 1612 | module_unload_init(mod); |
1613 | 1613 | ||
1614 | /* Set up license info based on the info section */ | 1614 | /* Set up license info based on the info section */ |
1615 | set_license(mod, get_modinfo(sechdrs, infoindex, "license")); | 1615 | set_license(mod, get_modinfo(sechdrs, infoindex, "license")); |
1616 | 1616 | ||
1617 | if (strcmp(mod->name, "ndiswrapper") == 0) | 1617 | if (strcmp(mod->name, "ndiswrapper") == 0) |
1618 | add_taint(TAINT_PROPRIETARY_MODULE); | 1618 | add_taint(TAINT_PROPRIETARY_MODULE); |
1619 | if (strcmp(mod->name, "driverloader") == 0) | 1619 | if (strcmp(mod->name, "driverloader") == 0) |
1620 | add_taint(TAINT_PROPRIETARY_MODULE); | 1620 | add_taint(TAINT_PROPRIETARY_MODULE); |
1621 | 1621 | ||
1622 | /* Set up MODINFO_ATTR fields */ | 1622 | /* Set up MODINFO_ATTR fields */ |
1623 | setup_modinfo(mod, sechdrs, infoindex); | 1623 | setup_modinfo(mod, sechdrs, infoindex); |
1624 | 1624 | ||
1625 | /* Fix up syms, so that st_value is a pointer to location. */ | 1625 | /* Fix up syms, so that st_value is a pointer to location. */ |
1626 | err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex, | 1626 | err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex, |
1627 | mod); | 1627 | mod); |
1628 | if (err < 0) | 1628 | if (err < 0) |
1629 | goto cleanup; | 1629 | goto cleanup; |
1630 | 1630 | ||
1631 | /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */ | 1631 | /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */ |
1632 | mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms); | 1632 | mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms); |
1633 | mod->syms = (void *)sechdrs[exportindex].sh_addr; | 1633 | mod->syms = (void *)sechdrs[exportindex].sh_addr; |
1634 | if (crcindex) | 1634 | if (crcindex) |
1635 | mod->crcs = (void *)sechdrs[crcindex].sh_addr; | 1635 | mod->crcs = (void *)sechdrs[crcindex].sh_addr; |
1636 | mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms); | 1636 | mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms); |
1637 | mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr; | 1637 | mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr; |
1638 | if (gplcrcindex) | 1638 | if (gplcrcindex) |
1639 | mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr; | 1639 | mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr; |
1640 | mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size / | 1640 | mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size / |
1641 | sizeof(*mod->gpl_future_syms); | 1641 | sizeof(*mod->gpl_future_syms); |
1642 | mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr; | 1642 | mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr; |
1643 | if (gplfuturecrcindex) | 1643 | if (gplfuturecrcindex) |
1644 | mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr; | 1644 | mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr; |
1645 | 1645 | ||
1646 | #ifdef CONFIG_MODVERSIONS | 1646 | #ifdef CONFIG_MODVERSIONS |
1647 | if ((mod->num_syms && !crcindex) || | 1647 | if ((mod->num_syms && !crcindex) || |
1648 | (mod->num_gpl_syms && !gplcrcindex) || | 1648 | (mod->num_gpl_syms && !gplcrcindex) || |
1649 | (mod->num_gpl_future_syms && !gplfuturecrcindex)) { | 1649 | (mod->num_gpl_future_syms && !gplfuturecrcindex)) { |
1650 | printk(KERN_WARNING "%s: No versions for exported symbols." | 1650 | printk(KERN_WARNING "%s: No versions for exported symbols." |
1651 | " Tainting kernel.\n", mod->name); | 1651 | " Tainting kernel.\n", mod->name); |
1652 | add_taint(TAINT_FORCED_MODULE); | 1652 | add_taint(TAINT_FORCED_MODULE); |
1653 | } | 1653 | } |
1654 | #endif | 1654 | #endif |
1655 | 1655 | ||
1656 | /* Now do relocations. */ | 1656 | /* Now do relocations. */ |
1657 | for (i = 1; i < hdr->e_shnum; i++) { | 1657 | for (i = 1; i < hdr->e_shnum; i++) { |
1658 | const char *strtab = (char *)sechdrs[strindex].sh_addr; | 1658 | const char *strtab = (char *)sechdrs[strindex].sh_addr; |
1659 | unsigned int info = sechdrs[i].sh_info; | 1659 | unsigned int info = sechdrs[i].sh_info; |
1660 | 1660 | ||
1661 | /* Not a valid relocation section? */ | 1661 | /* Not a valid relocation section? */ |
1662 | if (info >= hdr->e_shnum) | 1662 | if (info >= hdr->e_shnum) |
1663 | continue; | 1663 | continue; |
1664 | 1664 | ||
1665 | /* Don't bother with non-allocated sections */ | 1665 | /* Don't bother with non-allocated sections */ |
1666 | if (!(sechdrs[info].sh_flags & SHF_ALLOC)) | 1666 | if (!(sechdrs[info].sh_flags & SHF_ALLOC)) |
1667 | continue; | 1667 | continue; |
1668 | 1668 | ||
1669 | if (sechdrs[i].sh_type == SHT_REL) | 1669 | if (sechdrs[i].sh_type == SHT_REL) |
1670 | err = apply_relocate(sechdrs, strtab, symindex, i,mod); | 1670 | err = apply_relocate(sechdrs, strtab, symindex, i,mod); |
1671 | else if (sechdrs[i].sh_type == SHT_RELA) | 1671 | else if (sechdrs[i].sh_type == SHT_RELA) |
1672 | err = apply_relocate_add(sechdrs, strtab, symindex, i, | 1672 | err = apply_relocate_add(sechdrs, strtab, symindex, i, |
1673 | mod); | 1673 | mod); |
1674 | if (err < 0) | 1674 | if (err < 0) |
1675 | goto cleanup; | 1675 | goto cleanup; |
1676 | } | 1676 | } |
1677 | 1677 | ||
1678 | /* Find duplicate symbols */ | 1678 | /* Find duplicate symbols */ |
1679 | err = verify_export_symbols(mod); | 1679 | err = verify_export_symbols(mod); |
1680 | 1680 | ||
1681 | if (err < 0) | 1681 | if (err < 0) |
1682 | goto cleanup; | 1682 | goto cleanup; |
1683 | 1683 | ||
1684 | /* Set up and sort exception table */ | 1684 | /* Set up and sort exception table */ |
1685 | mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable); | 1685 | mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable); |
1686 | mod->extable = extable = (void *)sechdrs[exindex].sh_addr; | 1686 | mod->extable = extable = (void *)sechdrs[exindex].sh_addr; |
1687 | sort_extable(extable, extable + mod->num_exentries); | 1687 | sort_extable(extable, extable + mod->num_exentries); |
1688 | 1688 | ||
1689 | /* Finally, copy percpu area over. */ | 1689 | /* Finally, copy percpu area over. */ |
1690 | percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, | 1690 | percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, |
1691 | sechdrs[pcpuindex].sh_size); | 1691 | sechdrs[pcpuindex].sh_size); |
1692 | 1692 | ||
1693 | add_kallsyms(mod, sechdrs, symindex, strindex, secstrings); | 1693 | add_kallsyms(mod, sechdrs, symindex, strindex, secstrings); |
1694 | 1694 | ||
1695 | err = module_finalize(hdr, sechdrs, mod); | 1695 | err = module_finalize(hdr, sechdrs, mod); |
1696 | if (err < 0) | 1696 | if (err < 0) |
1697 | goto cleanup; | 1697 | goto cleanup; |
1698 | 1698 | ||
1699 | /* flush the icache in correct context */ | 1699 | /* flush the icache in correct context */ |
1700 | old_fs = get_fs(); | 1700 | old_fs = get_fs(); |
1701 | set_fs(KERNEL_DS); | 1701 | set_fs(KERNEL_DS); |
1702 | 1702 | ||
1703 | /* | 1703 | /* |
1704 | * Flush the instruction cache, since we've played with text. | 1704 | * Flush the instruction cache, since we've played with text. |
1705 | * Do it before processing of module parameters, so the module | 1705 | * Do it before processing of module parameters, so the module |
1706 | * can provide parameter accessor functions of its own. | 1706 | * can provide parameter accessor functions of its own. |
1707 | */ | 1707 | */ |
1708 | if (mod->module_init) | 1708 | if (mod->module_init) |
1709 | flush_icache_range((unsigned long)mod->module_init, | 1709 | flush_icache_range((unsigned long)mod->module_init, |
1710 | (unsigned long)mod->module_init | 1710 | (unsigned long)mod->module_init |
1711 | + mod->init_size); | 1711 | + mod->init_size); |
1712 | flush_icache_range((unsigned long)mod->module_core, | 1712 | flush_icache_range((unsigned long)mod->module_core, |
1713 | (unsigned long)mod->module_core + mod->core_size); | 1713 | (unsigned long)mod->module_core + mod->core_size); |
1714 | 1714 | ||
1715 | set_fs(old_fs); | 1715 | set_fs(old_fs); |
1716 | 1716 | ||
1717 | mod->args = args; | 1717 | mod->args = args; |
1718 | if (obsparmindex) | 1718 | if (obsparmindex) |
1719 | printk(KERN_WARNING "%s: Ignoring obsolete parameters\n", | 1719 | printk(KERN_WARNING "%s: Ignoring obsolete parameters\n", |
1720 | mod->name); | 1720 | mod->name); |
1721 | 1721 | ||
1722 | /* Size of section 0 is 0, so this works well if no params */ | 1722 | /* Size of section 0 is 0, so this works well if no params */ |
1723 | err = parse_args(mod->name, mod->args, | 1723 | err = parse_args(mod->name, mod->args, |
1724 | (struct kernel_param *) | 1724 | (struct kernel_param *) |
1725 | sechdrs[setupindex].sh_addr, | 1725 | sechdrs[setupindex].sh_addr, |
1726 | sechdrs[setupindex].sh_size | 1726 | sechdrs[setupindex].sh_size |
1727 | / sizeof(struct kernel_param), | 1727 | / sizeof(struct kernel_param), |
1728 | NULL); | 1728 | NULL); |
1729 | if (err < 0) | 1729 | if (err < 0) |
1730 | goto arch_cleanup; | 1730 | goto arch_cleanup; |
1731 | 1731 | ||
1732 | err = mod_sysfs_setup(mod, | 1732 | err = mod_sysfs_setup(mod, |
1733 | (struct kernel_param *) | 1733 | (struct kernel_param *) |
1734 | sechdrs[setupindex].sh_addr, | 1734 | sechdrs[setupindex].sh_addr, |
1735 | sechdrs[setupindex].sh_size | 1735 | sechdrs[setupindex].sh_size |
1736 | / sizeof(struct kernel_param)); | 1736 | / sizeof(struct kernel_param)); |
1737 | if (err < 0) | 1737 | if (err < 0) |
1738 | goto arch_cleanup; | 1738 | goto arch_cleanup; |
1739 | add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); | 1739 | add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); |
1740 | 1740 | ||
1741 | /* Get rid of temporary copy */ | 1741 | /* Get rid of temporary copy */ |
1742 | vfree(hdr); | 1742 | vfree(hdr); |
1743 | 1743 | ||
1744 | /* Done! */ | 1744 | /* Done! */ |
1745 | return mod; | 1745 | return mod; |
1746 | 1746 | ||
1747 | arch_cleanup: | 1747 | arch_cleanup: |
1748 | module_arch_cleanup(mod); | 1748 | module_arch_cleanup(mod); |
1749 | cleanup: | 1749 | cleanup: |
1750 | module_unload_free(mod); | 1750 | module_unload_free(mod); |
1751 | module_free(mod, mod->module_init); | 1751 | module_free(mod, mod->module_init); |
1752 | free_core: | 1752 | free_core: |
1753 | module_free(mod, mod->module_core); | 1753 | module_free(mod, mod->module_core); |
1754 | free_percpu: | 1754 | free_percpu: |
1755 | if (percpu) | 1755 | if (percpu) |
1756 | percpu_modfree(percpu); | 1756 | percpu_modfree(percpu); |
1757 | free_mod: | 1757 | free_mod: |
1758 | kfree(args); | 1758 | kfree(args); |
1759 | free_hdr: | 1759 | free_hdr: |
1760 | vfree(hdr); | 1760 | vfree(hdr); |
1761 | return ERR_PTR(err); | 1761 | return ERR_PTR(err); |
1762 | 1762 | ||
1763 | truncated: | 1763 | truncated: |
1764 | printk(KERN_ERR "Module len %lu truncated\n", len); | 1764 | printk(KERN_ERR "Module len %lu truncated\n", len); |
1765 | err = -ENOEXEC; | 1765 | err = -ENOEXEC; |
1766 | goto free_hdr; | 1766 | goto free_hdr; |
1767 | } | 1767 | } |
1768 | 1768 | ||
1769 | /* | 1769 | /* |
1770 | * link the module with the whole machine is stopped with interrupts off | 1770 | * link the module with the whole machine is stopped with interrupts off |
1771 | * - this defends against kallsyms not taking locks | 1771 | * - this defends against kallsyms not taking locks |
1772 | */ | 1772 | */ |
1773 | static int __link_module(void *_mod) | 1773 | static int __link_module(void *_mod) |
1774 | { | 1774 | { |
1775 | struct module *mod = _mod; | 1775 | struct module *mod = _mod; |
1776 | list_add(&mod->list, &modules); | 1776 | list_add(&mod->list, &modules); |
1777 | return 0; | 1777 | return 0; |
1778 | } | 1778 | } |
1779 | 1779 | ||
1780 | /* This is where the real work happens */ | 1780 | /* This is where the real work happens */ |
1781 | asmlinkage long | 1781 | asmlinkage long |
1782 | sys_init_module(void __user *umod, | 1782 | sys_init_module(void __user *umod, |
1783 | unsigned long len, | 1783 | unsigned long len, |
1784 | const char __user *uargs) | 1784 | const char __user *uargs) |
1785 | { | 1785 | { |
1786 | struct module *mod; | 1786 | struct module *mod; |
1787 | int ret = 0; | 1787 | int ret = 0; |
1788 | 1788 | ||
1789 | /* Must have permission */ | 1789 | /* Must have permission */ |
1790 | if (!capable(CAP_SYS_MODULE)) | 1790 | if (!capable(CAP_SYS_MODULE)) |
1791 | return -EPERM; | 1791 | return -EPERM; |
1792 | 1792 | ||
1793 | /* Only one module load at a time, please */ | 1793 | /* Only one module load at a time, please */ |
1794 | if (mutex_lock_interruptible(&module_mutex) != 0) | 1794 | if (mutex_lock_interruptible(&module_mutex) != 0) |
1795 | return -EINTR; | 1795 | return -EINTR; |
1796 | 1796 | ||
1797 | /* Do all the hard work */ | 1797 | /* Do all the hard work */ |
1798 | mod = load_module(umod, len, uargs); | 1798 | mod = load_module(umod, len, uargs); |
1799 | if (IS_ERR(mod)) { | 1799 | if (IS_ERR(mod)) { |
1800 | mutex_unlock(&module_mutex); | 1800 | mutex_unlock(&module_mutex); |
1801 | return PTR_ERR(mod); | 1801 | return PTR_ERR(mod); |
1802 | } | 1802 | } |
1803 | 1803 | ||
1804 | /* Now sew it into the lists. They won't access us, since | 1804 | /* Now sew it into the lists. They won't access us, since |
1805 | strong_try_module_get() will fail. */ | 1805 | strong_try_module_get() will fail. */ |
1806 | stop_machine_run(__link_module, mod, NR_CPUS); | 1806 | stop_machine_run(__link_module, mod, NR_CPUS); |
1807 | 1807 | ||
1808 | /* Drop lock so they can recurse */ | 1808 | /* Drop lock so they can recurse */ |
1809 | mutex_unlock(&module_mutex); | 1809 | mutex_unlock(&module_mutex); |
1810 | 1810 | ||
1811 | blocking_notifier_call_chain(&module_notify_list, | 1811 | blocking_notifier_call_chain(&module_notify_list, |
1812 | MODULE_STATE_COMING, mod); | 1812 | MODULE_STATE_COMING, mod); |
1813 | 1813 | ||
1814 | /* Start the module */ | 1814 | /* Start the module */ |
1815 | if (mod->init != NULL) | 1815 | if (mod->init != NULL) |
1816 | ret = mod->init(); | 1816 | ret = mod->init(); |
1817 | if (ret < 0) { | 1817 | if (ret < 0) { |
1818 | /* Init routine failed: abort. Try to protect us from | 1818 | /* Init routine failed: abort. Try to protect us from |
1819 | buggy refcounters. */ | 1819 | buggy refcounters. */ |
1820 | mod->state = MODULE_STATE_GOING; | 1820 | mod->state = MODULE_STATE_GOING; |
1821 | synchronize_sched(); | 1821 | synchronize_sched(); |
1822 | if (mod->unsafe) | 1822 | if (mod->unsafe) |
1823 | printk(KERN_ERR "%s: module is now stuck!\n", | 1823 | printk(KERN_ERR "%s: module is now stuck!\n", |
1824 | mod->name); | 1824 | mod->name); |
1825 | else { | 1825 | else { |
1826 | module_put(mod); | 1826 | module_put(mod); |
1827 | mutex_lock(&module_mutex); | 1827 | mutex_lock(&module_mutex); |
1828 | free_module(mod); | 1828 | free_module(mod); |
1829 | mutex_unlock(&module_mutex); | 1829 | mutex_unlock(&module_mutex); |
1830 | } | 1830 | } |
1831 | return ret; | 1831 | return ret; |
1832 | } | 1832 | } |
1833 | 1833 | ||
1834 | /* Now it's a first class citizen! */ | 1834 | /* Now it's a first class citizen! */ |
1835 | mutex_lock(&module_mutex); | 1835 | mutex_lock(&module_mutex); |
1836 | mod->state = MODULE_STATE_LIVE; | 1836 | mod->state = MODULE_STATE_LIVE; |
1837 | /* Drop initial reference. */ | 1837 | /* Drop initial reference. */ |
1838 | module_put(mod); | 1838 | module_put(mod); |
1839 | module_free(mod, mod->module_init); | 1839 | module_free(mod, mod->module_init); |
1840 | mod->module_init = NULL; | 1840 | mod->module_init = NULL; |
1841 | mod->init_size = 0; | 1841 | mod->init_size = 0; |
1842 | mod->init_text_size = 0; | 1842 | mod->init_text_size = 0; |
1843 | mutex_unlock(&module_mutex); | 1843 | mutex_unlock(&module_mutex); |
1844 | 1844 | ||
1845 | return 0; | 1845 | return 0; |
1846 | } | 1846 | } |
1847 | 1847 | ||
1848 | static inline int within(unsigned long addr, void *start, unsigned long size) | 1848 | static inline int within(unsigned long addr, void *start, unsigned long size) |
1849 | { | 1849 | { |
1850 | return ((void *)addr >= start && (void *)addr < start + size); | 1850 | return ((void *)addr >= start && (void *)addr < start + size); |
1851 | } | 1851 | } |
1852 | 1852 | ||
1853 | #ifdef CONFIG_KALLSYMS | 1853 | #ifdef CONFIG_KALLSYMS |
1854 | /* | 1854 | /* |
1855 | * This ignores the intensely annoying "mapping symbols" found | 1855 | * This ignores the intensely annoying "mapping symbols" found |
1856 | * in ARM ELF files: $a, $t and $d. | 1856 | * in ARM ELF files: $a, $t and $d. |
1857 | */ | 1857 | */ |
1858 | static inline int is_arm_mapping_symbol(const char *str) | 1858 | static inline int is_arm_mapping_symbol(const char *str) |
1859 | { | 1859 | { |
1860 | return str[0] == '$' && strchr("atd", str[1]) | 1860 | return str[0] == '$' && strchr("atd", str[1]) |
1861 | && (str[2] == '\0' || str[2] == '.'); | 1861 | && (str[2] == '\0' || str[2] == '.'); |
1862 | } | 1862 | } |
1863 | 1863 | ||
1864 | static const char *get_ksymbol(struct module *mod, | 1864 | static const char *get_ksymbol(struct module *mod, |
1865 | unsigned long addr, | 1865 | unsigned long addr, |
1866 | unsigned long *size, | 1866 | unsigned long *size, |
1867 | unsigned long *offset) | 1867 | unsigned long *offset) |
1868 | { | 1868 | { |
1869 | unsigned int i, best = 0; | 1869 | unsigned int i, best = 0; |
1870 | unsigned long nextval; | 1870 | unsigned long nextval; |
1871 | 1871 | ||
1872 | /* At worse, next value is at end of module */ | 1872 | /* At worse, next value is at end of module */ |
1873 | if (within(addr, mod->module_init, mod->init_size)) | 1873 | if (within(addr, mod->module_init, mod->init_size)) |
1874 | nextval = (unsigned long)mod->module_init+mod->init_text_size; | 1874 | nextval = (unsigned long)mod->module_init+mod->init_text_size; |
1875 | else | 1875 | else |
1876 | nextval = (unsigned long)mod->module_core+mod->core_text_size; | 1876 | nextval = (unsigned long)mod->module_core+mod->core_text_size; |
1877 | 1877 | ||
1878 | /* Scan for closest preceeding symbol, and next symbol. (ELF | 1878 | /* Scan for closest preceeding symbol, and next symbol. (ELF |
1879 | starts real symbols at 1). */ | 1879 | starts real symbols at 1). */ |
1880 | for (i = 1; i < mod->num_symtab; i++) { | 1880 | for (i = 1; i < mod->num_symtab; i++) { |
1881 | if (mod->symtab[i].st_shndx == SHN_UNDEF) | 1881 | if (mod->symtab[i].st_shndx == SHN_UNDEF) |
1882 | continue; | 1882 | continue; |
1883 | 1883 | ||
1884 | /* We ignore unnamed symbols: they're uninformative | 1884 | /* We ignore unnamed symbols: they're uninformative |
1885 | * and inserted at a whim. */ | 1885 | * and inserted at a whim. */ |
1886 | if (mod->symtab[i].st_value <= addr | 1886 | if (mod->symtab[i].st_value <= addr |
1887 | && mod->symtab[i].st_value > mod->symtab[best].st_value | 1887 | && mod->symtab[i].st_value > mod->symtab[best].st_value |
1888 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' | 1888 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' |
1889 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) | 1889 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) |
1890 | best = i; | 1890 | best = i; |
1891 | if (mod->symtab[i].st_value > addr | 1891 | if (mod->symtab[i].st_value > addr |
1892 | && mod->symtab[i].st_value < nextval | 1892 | && mod->symtab[i].st_value < nextval |
1893 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' | 1893 | && *(mod->strtab + mod->symtab[i].st_name) != '\0' |
1894 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) | 1894 | && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) |
1895 | nextval = mod->symtab[i].st_value; | 1895 | nextval = mod->symtab[i].st_value; |
1896 | } | 1896 | } |
1897 | 1897 | ||
1898 | if (!best) | 1898 | if (!best) |
1899 | return NULL; | 1899 | return NULL; |
1900 | 1900 | ||
1901 | *size = nextval - mod->symtab[best].st_value; | 1901 | *size = nextval - mod->symtab[best].st_value; |
1902 | *offset = addr - mod->symtab[best].st_value; | 1902 | *offset = addr - mod->symtab[best].st_value; |
1903 | return mod->strtab + mod->symtab[best].st_name; | 1903 | return mod->strtab + mod->symtab[best].st_name; |
1904 | } | 1904 | } |
1905 | 1905 | ||
1906 | /* For kallsyms to ask for address resolution. NULL means not found. | 1906 | /* For kallsyms to ask for address resolution. NULL means not found. |
1907 | We don't lock, as this is used for oops resolution and races are a | 1907 | We don't lock, as this is used for oops resolution and races are a |
1908 | lesser concern. */ | 1908 | lesser concern. */ |
1909 | const char *module_address_lookup(unsigned long addr, | 1909 | const char *module_address_lookup(unsigned long addr, |
1910 | unsigned long *size, | 1910 | unsigned long *size, |
1911 | unsigned long *offset, | 1911 | unsigned long *offset, |
1912 | char **modname) | 1912 | char **modname) |
1913 | { | 1913 | { |
1914 | struct module *mod; | 1914 | struct module *mod; |
1915 | 1915 | ||
1916 | list_for_each_entry(mod, &modules, list) { | 1916 | list_for_each_entry(mod, &modules, list) { |
1917 | if (within(addr, mod->module_init, mod->init_size) | 1917 | if (within(addr, mod->module_init, mod->init_size) |
1918 | || within(addr, mod->module_core, mod->core_size)) { | 1918 | || within(addr, mod->module_core, mod->core_size)) { |
1919 | *modname = mod->name; | 1919 | *modname = mod->name; |
1920 | return get_ksymbol(mod, addr, size, offset); | 1920 | return get_ksymbol(mod, addr, size, offset); |
1921 | } | 1921 | } |
1922 | } | 1922 | } |
1923 | return NULL; | 1923 | return NULL; |
1924 | } | 1924 | } |
1925 | 1925 | ||
1926 | struct module *module_get_kallsym(unsigned int symnum, | 1926 | struct module *module_get_kallsym(unsigned int symnum, |
1927 | unsigned long *value, | 1927 | unsigned long *value, |
1928 | char *type, | 1928 | char *type, |
1929 | char namebuf[128]) | 1929 | char namebuf[128]) |
1930 | { | 1930 | { |
1931 | struct module *mod; | 1931 | struct module *mod; |
1932 | 1932 | ||
1933 | mutex_lock(&module_mutex); | 1933 | mutex_lock(&module_mutex); |
1934 | list_for_each_entry(mod, &modules, list) { | 1934 | list_for_each_entry(mod, &modules, list) { |
1935 | if (symnum < mod->num_symtab) { | 1935 | if (symnum < mod->num_symtab) { |
1936 | *value = mod->symtab[symnum].st_value; | 1936 | *value = mod->symtab[symnum].st_value; |
1937 | *type = mod->symtab[symnum].st_info; | 1937 | *type = mod->symtab[symnum].st_info; |
1938 | strncpy(namebuf, | 1938 | strncpy(namebuf, |
1939 | mod->strtab + mod->symtab[symnum].st_name, | 1939 | mod->strtab + mod->symtab[symnum].st_name, |
1940 | 127); | 1940 | 127); |
1941 | mutex_unlock(&module_mutex); | 1941 | mutex_unlock(&module_mutex); |
1942 | return mod; | 1942 | return mod; |
1943 | } | 1943 | } |
1944 | symnum -= mod->num_symtab; | 1944 | symnum -= mod->num_symtab; |
1945 | } | 1945 | } |
1946 | mutex_unlock(&module_mutex); | 1946 | mutex_unlock(&module_mutex); |
1947 | return NULL; | 1947 | return NULL; |
1948 | } | 1948 | } |
1949 | 1949 | ||
1950 | static unsigned long mod_find_symname(struct module *mod, const char *name) | 1950 | static unsigned long mod_find_symname(struct module *mod, const char *name) |
1951 | { | 1951 | { |
1952 | unsigned int i; | 1952 | unsigned int i; |
1953 | 1953 | ||
1954 | for (i = 0; i < mod->num_symtab; i++) | 1954 | for (i = 0; i < mod->num_symtab; i++) |
1955 | if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 && | 1955 | if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 && |
1956 | mod->symtab[i].st_info != 'U') | 1956 | mod->symtab[i].st_info != 'U') |
1957 | return mod->symtab[i].st_value; | 1957 | return mod->symtab[i].st_value; |
1958 | return 0; | 1958 | return 0; |
1959 | } | 1959 | } |
1960 | 1960 | ||
1961 | /* Look for this name: can be of form module:name. */ | 1961 | /* Look for this name: can be of form module:name. */ |
1962 | unsigned long module_kallsyms_lookup_name(const char *name) | 1962 | unsigned long module_kallsyms_lookup_name(const char *name) |
1963 | { | 1963 | { |
1964 | struct module *mod; | 1964 | struct module *mod; |
1965 | char *colon; | 1965 | char *colon; |
1966 | unsigned long ret = 0; | 1966 | unsigned long ret = 0; |
1967 | 1967 | ||
1968 | /* Don't lock: we're in enough trouble already. */ | 1968 | /* Don't lock: we're in enough trouble already. */ |
1969 | if ((colon = strchr(name, ':')) != NULL) { | 1969 | if ((colon = strchr(name, ':')) != NULL) { |
1970 | *colon = '\0'; | 1970 | *colon = '\0'; |
1971 | if ((mod = find_module(name)) != NULL) | 1971 | if ((mod = find_module(name)) != NULL) |
1972 | ret = mod_find_symname(mod, colon+1); | 1972 | ret = mod_find_symname(mod, colon+1); |
1973 | *colon = ':'; | 1973 | *colon = ':'; |
1974 | } else { | 1974 | } else { |
1975 | list_for_each_entry(mod, &modules, list) | 1975 | list_for_each_entry(mod, &modules, list) |
1976 | if ((ret = mod_find_symname(mod, name)) != 0) | 1976 | if ((ret = mod_find_symname(mod, name)) != 0) |
1977 | break; | 1977 | break; |
1978 | } | 1978 | } |
1979 | return ret; | 1979 | return ret; |
1980 | } | 1980 | } |
1981 | #endif /* CONFIG_KALLSYMS */ | 1981 | #endif /* CONFIG_KALLSYMS */ |
1982 | 1982 | ||
1983 | /* Called by the /proc file system to return a list of modules. */ | 1983 | /* Called by the /proc file system to return a list of modules. */ |
1984 | static void *m_start(struct seq_file *m, loff_t *pos) | 1984 | static void *m_start(struct seq_file *m, loff_t *pos) |
1985 | { | 1985 | { |
1986 | struct list_head *i; | 1986 | struct list_head *i; |
1987 | loff_t n = 0; | 1987 | loff_t n = 0; |
1988 | 1988 | ||
1989 | mutex_lock(&module_mutex); | 1989 | mutex_lock(&module_mutex); |
1990 | list_for_each(i, &modules) { | 1990 | list_for_each(i, &modules) { |
1991 | if (n++ == *pos) | 1991 | if (n++ == *pos) |
1992 | break; | 1992 | break; |
1993 | } | 1993 | } |
1994 | if (i == &modules) | 1994 | if (i == &modules) |
1995 | return NULL; | 1995 | return NULL; |
1996 | return i; | 1996 | return i; |
1997 | } | 1997 | } |
1998 | 1998 | ||
1999 | static void *m_next(struct seq_file *m, void *p, loff_t *pos) | 1999 | static void *m_next(struct seq_file *m, void *p, loff_t *pos) |
2000 | { | 2000 | { |
2001 | struct list_head *i = p; | 2001 | struct list_head *i = p; |
2002 | (*pos)++; | 2002 | (*pos)++; |
2003 | if (i->next == &modules) | 2003 | if (i->next == &modules) |
2004 | return NULL; | 2004 | return NULL; |
2005 | return i->next; | 2005 | return i->next; |
2006 | } | 2006 | } |
2007 | 2007 | ||
2008 | static void m_stop(struct seq_file *m, void *p) | 2008 | static void m_stop(struct seq_file *m, void *p) |
2009 | { | 2009 | { |
2010 | mutex_unlock(&module_mutex); | 2010 | mutex_unlock(&module_mutex); |
2011 | } | 2011 | } |
2012 | 2012 | ||
2013 | static int m_show(struct seq_file *m, void *p) | 2013 | static int m_show(struct seq_file *m, void *p) |
2014 | { | 2014 | { |
2015 | struct module *mod = list_entry(p, struct module, list); | 2015 | struct module *mod = list_entry(p, struct module, list); |
2016 | seq_printf(m, "%s %lu", | 2016 | seq_printf(m, "%s %lu", |
2017 | mod->name, mod->init_size + mod->core_size); | 2017 | mod->name, mod->init_size + mod->core_size); |
2018 | print_unload_info(m, mod); | 2018 | print_unload_info(m, mod); |
2019 | 2019 | ||
2020 | /* Informative for users. */ | 2020 | /* Informative for users. */ |
2021 | seq_printf(m, " %s", | 2021 | seq_printf(m, " %s", |
2022 | mod->state == MODULE_STATE_GOING ? "Unloading": | 2022 | mod->state == MODULE_STATE_GOING ? "Unloading": |
2023 | mod->state == MODULE_STATE_COMING ? "Loading": | 2023 | mod->state == MODULE_STATE_COMING ? "Loading": |
2024 | "Live"); | 2024 | "Live"); |
2025 | /* Used by oprofile and other similar tools. */ | 2025 | /* Used by oprofile and other similar tools. */ |
2026 | seq_printf(m, " 0x%p", mod->module_core); | 2026 | seq_printf(m, " 0x%p", mod->module_core); |
2027 | 2027 | ||
2028 | seq_printf(m, "\n"); | 2028 | seq_printf(m, "\n"); |
2029 | return 0; | 2029 | return 0; |
2030 | } | 2030 | } |
2031 | 2031 | ||
2032 | /* Format: modulename size refcount deps address | 2032 | /* Format: modulename size refcount deps address |
2033 | 2033 | ||
2034 | Where refcount is a number or -, and deps is a comma-separated list | 2034 | Where refcount is a number or -, and deps is a comma-separated list |
2035 | of depends or -. | 2035 | of depends or -. |
2036 | */ | 2036 | */ |
2037 | struct seq_operations modules_op = { | 2037 | struct seq_operations modules_op = { |
2038 | .start = m_start, | 2038 | .start = m_start, |
2039 | .next = m_next, | 2039 | .next = m_next, |
2040 | .stop = m_stop, | 2040 | .stop = m_stop, |
2041 | .show = m_show | 2041 | .show = m_show |
2042 | }; | 2042 | }; |
2043 | 2043 | ||
2044 | /* Given an address, look for it in the module exception tables. */ | 2044 | /* Given an address, look for it in the module exception tables. */ |
2045 | const struct exception_table_entry *search_module_extables(unsigned long addr) | 2045 | const struct exception_table_entry *search_module_extables(unsigned long addr) |
2046 | { | 2046 | { |
2047 | unsigned long flags; | 2047 | unsigned long flags; |
2048 | const struct exception_table_entry *e = NULL; | 2048 | const struct exception_table_entry *e = NULL; |
2049 | struct module *mod; | 2049 | struct module *mod; |
2050 | 2050 | ||
2051 | spin_lock_irqsave(&modlist_lock, flags); | 2051 | spin_lock_irqsave(&modlist_lock, flags); |
2052 | list_for_each_entry(mod, &modules, list) { | 2052 | list_for_each_entry(mod, &modules, list) { |
2053 | if (mod->num_exentries == 0) | 2053 | if (mod->num_exentries == 0) |
2054 | continue; | 2054 | continue; |
2055 | 2055 | ||
2056 | e = search_extable(mod->extable, | 2056 | e = search_extable(mod->extable, |
2057 | mod->extable + mod->num_exentries - 1, | 2057 | mod->extable + mod->num_exentries - 1, |
2058 | addr); | 2058 | addr); |
2059 | if (e) | 2059 | if (e) |
2060 | break; | 2060 | break; |
2061 | } | 2061 | } |
2062 | spin_unlock_irqrestore(&modlist_lock, flags); | 2062 | spin_unlock_irqrestore(&modlist_lock, flags); |
2063 | 2063 | ||
2064 | /* Now, if we found one, we are running inside it now, hence | 2064 | /* Now, if we found one, we are running inside it now, hence |
2065 | we cannot unload the module, hence no refcnt needed. */ | 2065 | we cannot unload the module, hence no refcnt needed. */ |
2066 | return e; | 2066 | return e; |
2067 | } | 2067 | } |
2068 | 2068 | ||
2069 | /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */ | 2069 | /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */ |
2070 | struct module *__module_text_address(unsigned long addr) | 2070 | struct module *__module_text_address(unsigned long addr) |
2071 | { | 2071 | { |
2072 | struct module *mod; | 2072 | struct module *mod; |
2073 | 2073 | ||
2074 | list_for_each_entry(mod, &modules, list) | 2074 | list_for_each_entry(mod, &modules, list) |
2075 | if (within(addr, mod->module_init, mod->init_text_size) | 2075 | if (within(addr, mod->module_init, mod->init_text_size) |
2076 | || within(addr, mod->module_core, mod->core_text_size)) | 2076 | || within(addr, mod->module_core, mod->core_text_size)) |
2077 | return mod; | 2077 | return mod; |
2078 | return NULL; | 2078 | return NULL; |
2079 | } | 2079 | } |
2080 | 2080 | ||
2081 | struct module *module_text_address(unsigned long addr) | 2081 | struct module *module_text_address(unsigned long addr) |
2082 | { | 2082 | { |
2083 | struct module *mod; | 2083 | struct module *mod; |
2084 | unsigned long flags; | 2084 | unsigned long flags; |
2085 | 2085 | ||
2086 | spin_lock_irqsave(&modlist_lock, flags); | 2086 | spin_lock_irqsave(&modlist_lock, flags); |
2087 | mod = __module_text_address(addr); | 2087 | mod = __module_text_address(addr); |
2088 | spin_unlock_irqrestore(&modlist_lock, flags); | 2088 | spin_unlock_irqrestore(&modlist_lock, flags); |
2089 | 2089 | ||
2090 | return mod; | 2090 | return mod; |
2091 | } | 2091 | } |
2092 | 2092 | ||
2093 | /* Don't grab lock, we're oopsing. */ | 2093 | /* Don't grab lock, we're oopsing. */ |
2094 | void print_modules(void) | 2094 | void print_modules(void) |
2095 | { | 2095 | { |
2096 | struct module *mod; | 2096 | struct module *mod; |
2097 | 2097 | ||
2098 | printk("Modules linked in:"); | 2098 | printk("Modules linked in:"); |
2099 | list_for_each_entry(mod, &modules, list) | 2099 | list_for_each_entry(mod, &modules, list) |
2100 | printk(" %s", mod->name); | 2100 | printk(" %s", mod->name); |
2101 | printk("\n"); | 2101 | printk("\n"); |
2102 | } | 2102 | } |
2103 | 2103 | ||
2104 | void module_add_driver(struct module *mod, struct device_driver *drv) | 2104 | void module_add_driver(struct module *mod, struct device_driver *drv) |
2105 | { | 2105 | { |
2106 | if (!mod || !drv) | 2106 | if (!mod || !drv) |
2107 | return; | 2107 | return; |
2108 | 2108 | ||
2109 | /* Don't check return code; this call is idempotent */ | 2109 | /* Don't check return code; this call is idempotent */ |
2110 | sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module"); | 2110 | sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module"); |
2111 | } | 2111 | } |
2112 | EXPORT_SYMBOL(module_add_driver); | 2112 | EXPORT_SYMBOL(module_add_driver); |
2113 | 2113 | ||
2114 | void module_remove_driver(struct device_driver *drv) | 2114 | void module_remove_driver(struct device_driver *drv) |
2115 | { | 2115 | { |
2116 | if (!drv) | 2116 | if (!drv) |
2117 | return; | 2117 | return; |
2118 | sysfs_remove_link(&drv->kobj, "module"); | 2118 | sysfs_remove_link(&drv->kobj, "module"); |
2119 | } | 2119 | } |
2120 | EXPORT_SYMBOL(module_remove_driver); | 2120 | EXPORT_SYMBOL(module_remove_driver); |
2121 | 2121 | ||
2122 | #ifdef CONFIG_MODVERSIONS | 2122 | #ifdef CONFIG_MODVERSIONS |
2123 | /* Generate the signature for struct module here, too, for modversions. */ | 2123 | /* Generate the signature for struct module here, too, for modversions. */ |
2124 | void struct_module(struct module *mod) { return; } | 2124 | void struct_module(struct module *mod) { return; } |
2125 | EXPORT_SYMBOL(struct_module); | 2125 | EXPORT_SYMBOL(struct_module); |
2126 | #endif | 2126 | #endif |