Commit f30c53a873d0d227493197064b8886af2d57bbd6

Authored by Kay Sievers
Committed by Greg Kroah-Hartman
1 parent c744aeae9d

MODULES: add the module name for built in kernel drivers

Signed-off-by: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

Showing 4 changed files with 31 additions and 17 deletions Inline Diff

include/linux/device.h
1 /* 1 /*
2 * device.h - generic, centralized driver model 2 * device.h - generic, centralized driver model
3 * 3 *
4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org> 4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5 * 5 *
6 * This file is released under the GPLv2 6 * This file is released under the GPLv2
7 * 7 *
8 * See Documentation/driver-model/ for more information. 8 * See Documentation/driver-model/ for more information.
9 */ 9 */
10 10
11 #ifndef _DEVICE_H_ 11 #ifndef _DEVICE_H_
12 #define _DEVICE_H_ 12 #define _DEVICE_H_
13 13
14 #include <linux/ioport.h> 14 #include <linux/ioport.h>
15 #include <linux/kobject.h> 15 #include <linux/kobject.h>
16 #include <linux/klist.h> 16 #include <linux/klist.h>
17 #include <linux/list.h> 17 #include <linux/list.h>
18 #include <linux/compiler.h> 18 #include <linux/compiler.h>
19 #include <linux/types.h> 19 #include <linux/types.h>
20 #include <linux/module.h> 20 #include <linux/module.h>
21 #include <linux/pm.h> 21 #include <linux/pm.h>
22 #include <asm/semaphore.h> 22 #include <asm/semaphore.h>
23 #include <asm/atomic.h> 23 #include <asm/atomic.h>
24 #include <asm/device.h> 24 #include <asm/device.h>
25 25
26 #define DEVICE_NAME_SIZE 50 26 #define DEVICE_NAME_SIZE 50
27 #define DEVICE_NAME_HALF __stringify(20) /* Less than half to accommodate slop */ 27 #define DEVICE_NAME_HALF __stringify(20) /* Less than half to accommodate slop */
28 #define DEVICE_ID_SIZE 32 28 #define DEVICE_ID_SIZE 32
29 #define BUS_ID_SIZE KOBJ_NAME_LEN 29 #define BUS_ID_SIZE KOBJ_NAME_LEN
30 30
31 31
32 struct device; 32 struct device;
33 struct device_driver; 33 struct device_driver;
34 struct class; 34 struct class;
35 struct class_device; 35 struct class_device;
36 36
37 struct bus_type { 37 struct bus_type {
38 const char * name; 38 const char * name;
39 39
40 struct subsystem subsys; 40 struct subsystem subsys;
41 struct kset drivers; 41 struct kset drivers;
42 struct kset devices; 42 struct kset devices;
43 struct klist klist_devices; 43 struct klist klist_devices;
44 struct klist klist_drivers; 44 struct klist klist_drivers;
45 45
46 struct blocking_notifier_head bus_notifier; 46 struct blocking_notifier_head bus_notifier;
47 47
48 struct bus_attribute * bus_attrs; 48 struct bus_attribute * bus_attrs;
49 struct device_attribute * dev_attrs; 49 struct device_attribute * dev_attrs;
50 struct driver_attribute * drv_attrs; 50 struct driver_attribute * drv_attrs;
51 51
52 int (*match)(struct device * dev, struct device_driver * drv); 52 int (*match)(struct device * dev, struct device_driver * drv);
53 int (*uevent)(struct device *dev, char **envp, 53 int (*uevent)(struct device *dev, char **envp,
54 int num_envp, char *buffer, int buffer_size); 54 int num_envp, char *buffer, int buffer_size);
55 int (*probe)(struct device * dev); 55 int (*probe)(struct device * dev);
56 int (*remove)(struct device * dev); 56 int (*remove)(struct device * dev);
57 void (*shutdown)(struct device * dev); 57 void (*shutdown)(struct device * dev);
58 58
59 int (*suspend)(struct device * dev, pm_message_t state); 59 int (*suspend)(struct device * dev, pm_message_t state);
60 int (*suspend_late)(struct device * dev, pm_message_t state); 60 int (*suspend_late)(struct device * dev, pm_message_t state);
61 int (*resume_early)(struct device * dev); 61 int (*resume_early)(struct device * dev);
62 int (*resume)(struct device * dev); 62 int (*resume)(struct device * dev);
63 }; 63 };
64 64
65 extern int __must_check bus_register(struct bus_type * bus); 65 extern int __must_check bus_register(struct bus_type * bus);
66 extern void bus_unregister(struct bus_type * bus); 66 extern void bus_unregister(struct bus_type * bus);
67 67
68 extern int __must_check bus_rescan_devices(struct bus_type * bus); 68 extern int __must_check bus_rescan_devices(struct bus_type * bus);
69 69
70 /* iterator helpers for buses */ 70 /* iterator helpers for buses */
71 71
72 int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data, 72 int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
73 int (*fn)(struct device *, void *)); 73 int (*fn)(struct device *, void *));
74 struct device * bus_find_device(struct bus_type *bus, struct device *start, 74 struct device * bus_find_device(struct bus_type *bus, struct device *start,
75 void *data, int (*match)(struct device *, void *)); 75 void *data, int (*match)(struct device *, void *));
76 76
77 int __must_check bus_for_each_drv(struct bus_type *bus, 77 int __must_check bus_for_each_drv(struct bus_type *bus,
78 struct device_driver *start, void *data, 78 struct device_driver *start, void *data,
79 int (*fn)(struct device_driver *, void *)); 79 int (*fn)(struct device_driver *, void *));
80 80
81 /* 81 /*
82 * Bus notifiers: Get notified of addition/removal of devices 82 * Bus notifiers: Get notified of addition/removal of devices
83 * and binding/unbinding of drivers to devices. 83 * and binding/unbinding of drivers to devices.
84 * In the long run, it should be a replacement for the platform 84 * In the long run, it should be a replacement for the platform
85 * notify hooks. 85 * notify hooks.
86 */ 86 */
87 struct notifier_block; 87 struct notifier_block;
88 88
89 extern int bus_register_notifier(struct bus_type *bus, 89 extern int bus_register_notifier(struct bus_type *bus,
90 struct notifier_block *nb); 90 struct notifier_block *nb);
91 extern int bus_unregister_notifier(struct bus_type *bus, 91 extern int bus_unregister_notifier(struct bus_type *bus,
92 struct notifier_block *nb); 92 struct notifier_block *nb);
93 93
94 /* All 4 notifers below get called with the target struct device * 94 /* All 4 notifers below get called with the target struct device *
95 * as an argument. Note that those functions are likely to be called 95 * as an argument. Note that those functions are likely to be called
96 * with the device semaphore held in the core, so be careful. 96 * with the device semaphore held in the core, so be careful.
97 */ 97 */
98 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */ 98 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
99 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */ 99 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
100 #define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */ 100 #define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
101 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be 101 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
102 unbound */ 102 unbound */
103 103
104 /* driverfs interface for exporting bus attributes */ 104 /* driverfs interface for exporting bus attributes */
105 105
106 struct bus_attribute { 106 struct bus_attribute {
107 struct attribute attr; 107 struct attribute attr;
108 ssize_t (*show)(struct bus_type *, char * buf); 108 ssize_t (*show)(struct bus_type *, char * buf);
109 ssize_t (*store)(struct bus_type *, const char * buf, size_t count); 109 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
110 }; 110 };
111 111
112 #define BUS_ATTR(_name,_mode,_show,_store) \ 112 #define BUS_ATTR(_name,_mode,_show,_store) \
113 struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store) 113 struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
114 114
115 extern int __must_check bus_create_file(struct bus_type *, 115 extern int __must_check bus_create_file(struct bus_type *,
116 struct bus_attribute *); 116 struct bus_attribute *);
117 extern void bus_remove_file(struct bus_type *, struct bus_attribute *); 117 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
118 118
119 struct device_driver { 119 struct device_driver {
120 const char * name; 120 const char * name;
121 struct bus_type * bus; 121 struct bus_type * bus;
122 122
123 struct completion unloaded; 123 struct completion unloaded;
124 struct kobject kobj; 124 struct kobject kobj;
125 struct klist klist_devices; 125 struct klist klist_devices;
126 struct klist_node knode_bus; 126 struct klist_node knode_bus;
127 127
128 struct module * owner; 128 struct module * owner;
129 const char * mod_name; /* used for built-in modules */
129 130
130 int (*probe) (struct device * dev); 131 int (*probe) (struct device * dev);
131 int (*remove) (struct device * dev); 132 int (*remove) (struct device * dev);
132 void (*shutdown) (struct device * dev); 133 void (*shutdown) (struct device * dev);
133 int (*suspend) (struct device * dev, pm_message_t state); 134 int (*suspend) (struct device * dev, pm_message_t state);
134 int (*resume) (struct device * dev); 135 int (*resume) (struct device * dev);
135 136
136 unsigned int multithread_probe:1; 137 unsigned int multithread_probe:1;
137 }; 138 };
138 139
139 140
140 extern int __must_check driver_register(struct device_driver * drv); 141 extern int __must_check driver_register(struct device_driver * drv);
141 extern void driver_unregister(struct device_driver * drv); 142 extern void driver_unregister(struct device_driver * drv);
142 143
143 extern struct device_driver * get_driver(struct device_driver * drv); 144 extern struct device_driver * get_driver(struct device_driver * drv);
144 extern void put_driver(struct device_driver * drv); 145 extern void put_driver(struct device_driver * drv);
145 extern struct device_driver *driver_find(const char *name, struct bus_type *bus); 146 extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
146 extern int driver_probe_done(void); 147 extern int driver_probe_done(void);
147 148
148 /* driverfs interface for exporting driver attributes */ 149 /* driverfs interface for exporting driver attributes */
149 150
150 struct driver_attribute { 151 struct driver_attribute {
151 struct attribute attr; 152 struct attribute attr;
152 ssize_t (*show)(struct device_driver *, char * buf); 153 ssize_t (*show)(struct device_driver *, char * buf);
153 ssize_t (*store)(struct device_driver *, const char * buf, size_t count); 154 ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
154 }; 155 };
155 156
156 #define DRIVER_ATTR(_name,_mode,_show,_store) \ 157 #define DRIVER_ATTR(_name,_mode,_show,_store) \
157 struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store) 158 struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
158 159
159 extern int __must_check driver_create_file(struct device_driver *, 160 extern int __must_check driver_create_file(struct device_driver *,
160 struct driver_attribute *); 161 struct driver_attribute *);
161 extern void driver_remove_file(struct device_driver *, struct driver_attribute *); 162 extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
162 163
163 extern int __must_check driver_for_each_device(struct device_driver * drv, 164 extern int __must_check driver_for_each_device(struct device_driver * drv,
164 struct device *start, void *data, 165 struct device *start, void *data,
165 int (*fn)(struct device *, void *)); 166 int (*fn)(struct device *, void *));
166 struct device * driver_find_device(struct device_driver *drv, 167 struct device * driver_find_device(struct device_driver *drv,
167 struct device *start, void *data, 168 struct device *start, void *data,
168 int (*match)(struct device *, void *)); 169 int (*match)(struct device *, void *));
169 170
170 /* 171 /*
171 * device classes 172 * device classes
172 */ 173 */
173 struct class { 174 struct class {
174 const char * name; 175 const char * name;
175 struct module * owner; 176 struct module * owner;
176 177
177 struct subsystem subsys; 178 struct subsystem subsys;
178 struct list_head children; 179 struct list_head children;
179 struct list_head devices; 180 struct list_head devices;
180 struct list_head interfaces; 181 struct list_head interfaces;
181 struct semaphore sem; /* locks both the children and interfaces lists */ 182 struct semaphore sem; /* locks both the children and interfaces lists */
182 183
183 struct kobject *virtual_dir; 184 struct kobject *virtual_dir;
184 185
185 struct class_attribute * class_attrs; 186 struct class_attribute * class_attrs;
186 struct class_device_attribute * class_dev_attrs; 187 struct class_device_attribute * class_dev_attrs;
187 struct device_attribute * dev_attrs; 188 struct device_attribute * dev_attrs;
188 189
189 int (*uevent)(struct class_device *dev, char **envp, 190 int (*uevent)(struct class_device *dev, char **envp,
190 int num_envp, char *buffer, int buffer_size); 191 int num_envp, char *buffer, int buffer_size);
191 int (*dev_uevent)(struct device *dev, char **envp, int num_envp, 192 int (*dev_uevent)(struct device *dev, char **envp, int num_envp,
192 char *buffer, int buffer_size); 193 char *buffer, int buffer_size);
193 194
194 void (*release)(struct class_device *dev); 195 void (*release)(struct class_device *dev);
195 void (*class_release)(struct class *class); 196 void (*class_release)(struct class *class);
196 void (*dev_release)(struct device *dev); 197 void (*dev_release)(struct device *dev);
197 198
198 int (*suspend)(struct device *, pm_message_t state); 199 int (*suspend)(struct device *, pm_message_t state);
199 int (*resume)(struct device *); 200 int (*resume)(struct device *);
200 }; 201 };
201 202
202 extern int __must_check class_register(struct class *); 203 extern int __must_check class_register(struct class *);
203 extern void class_unregister(struct class *); 204 extern void class_unregister(struct class *);
204 205
205 206
206 struct class_attribute { 207 struct class_attribute {
207 struct attribute attr; 208 struct attribute attr;
208 ssize_t (*show)(struct class *, char * buf); 209 ssize_t (*show)(struct class *, char * buf);
209 ssize_t (*store)(struct class *, const char * buf, size_t count); 210 ssize_t (*store)(struct class *, const char * buf, size_t count);
210 }; 211 };
211 212
212 #define CLASS_ATTR(_name,_mode,_show,_store) \ 213 #define CLASS_ATTR(_name,_mode,_show,_store) \
213 struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store) 214 struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
214 215
215 extern int __must_check class_create_file(struct class *, 216 extern int __must_check class_create_file(struct class *,
216 const struct class_attribute *); 217 const struct class_attribute *);
217 extern void class_remove_file(struct class *, const struct class_attribute *); 218 extern void class_remove_file(struct class *, const struct class_attribute *);
218 219
219 struct class_device_attribute { 220 struct class_device_attribute {
220 struct attribute attr; 221 struct attribute attr;
221 ssize_t (*show)(struct class_device *, char * buf); 222 ssize_t (*show)(struct class_device *, char * buf);
222 ssize_t (*store)(struct class_device *, const char * buf, size_t count); 223 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
223 }; 224 };
224 225
225 #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \ 226 #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
226 struct class_device_attribute class_device_attr_##_name = \ 227 struct class_device_attribute class_device_attr_##_name = \
227 __ATTR(_name,_mode,_show,_store) 228 __ATTR(_name,_mode,_show,_store)
228 229
229 extern int __must_check class_device_create_file(struct class_device *, 230 extern int __must_check class_device_create_file(struct class_device *,
230 const struct class_device_attribute *); 231 const struct class_device_attribute *);
231 232
232 /** 233 /**
233 * struct class_device - class devices 234 * struct class_device - class devices
234 * @class: pointer to the parent class for this class device. This is required. 235 * @class: pointer to the parent class for this class device. This is required.
235 * @devt: for internal use by the driver core only. 236 * @devt: for internal use by the driver core only.
236 * @node: for internal use by the driver core only. 237 * @node: for internal use by the driver core only.
237 * @kobj: for internal use by the driver core only. 238 * @kobj: for internal use by the driver core only.
238 * @devt_attr: for internal use by the driver core only. 239 * @devt_attr: for internal use by the driver core only.
239 * @groups: optional additional groups to be created 240 * @groups: optional additional groups to be created
240 * @dev: if set, a symlink to the struct device is created in the sysfs 241 * @dev: if set, a symlink to the struct device is created in the sysfs
241 * directory for this struct class device. 242 * directory for this struct class device.
242 * @class_data: pointer to whatever you want to store here for this struct 243 * @class_data: pointer to whatever you want to store here for this struct
243 * class_device. Use class_get_devdata() and class_set_devdata() to get and 244 * class_device. Use class_get_devdata() and class_set_devdata() to get and
244 * set this pointer. 245 * set this pointer.
245 * @parent: pointer to a struct class_device that is the parent of this struct 246 * @parent: pointer to a struct class_device that is the parent of this struct
246 * class_device. If NULL, this class_device will show up at the root of the 247 * class_device. If NULL, this class_device will show up at the root of the
247 * struct class in sysfs (which is probably what you want to have happen.) 248 * struct class in sysfs (which is probably what you want to have happen.)
248 * @release: pointer to a release function for this struct class_device. If 249 * @release: pointer to a release function for this struct class_device. If
249 * set, this will be called instead of the class specific release function. 250 * set, this will be called instead of the class specific release function.
250 * Only use this if you want to override the default release function, like 251 * Only use this if you want to override the default release function, like
251 * when you are nesting class_device structures. 252 * when you are nesting class_device structures.
252 * @uevent: pointer to a uevent function for this struct class_device. If 253 * @uevent: pointer to a uevent function for this struct class_device. If
253 * set, this will be called instead of the class specific uevent function. 254 * set, this will be called instead of the class specific uevent function.
254 * Only use this if you want to override the default uevent function, like 255 * Only use this if you want to override the default uevent function, like
255 * when you are nesting class_device structures. 256 * when you are nesting class_device structures.
256 */ 257 */
257 struct class_device { 258 struct class_device {
258 struct list_head node; 259 struct list_head node;
259 260
260 struct kobject kobj; 261 struct kobject kobj;
261 struct class * class; /* required */ 262 struct class * class; /* required */
262 dev_t devt; /* dev_t, creates the sysfs "dev" */ 263 dev_t devt; /* dev_t, creates the sysfs "dev" */
263 struct class_device_attribute *devt_attr; 264 struct class_device_attribute *devt_attr;
264 struct class_device_attribute uevent_attr; 265 struct class_device_attribute uevent_attr;
265 struct device * dev; /* not necessary, but nice to have */ 266 struct device * dev; /* not necessary, but nice to have */
266 void * class_data; /* class-specific data */ 267 void * class_data; /* class-specific data */
267 struct class_device *parent; /* parent of this child device, if there is one */ 268 struct class_device *parent; /* parent of this child device, if there is one */
268 struct attribute_group ** groups; /* optional groups */ 269 struct attribute_group ** groups; /* optional groups */
269 270
270 void (*release)(struct class_device *dev); 271 void (*release)(struct class_device *dev);
271 int (*uevent)(struct class_device *dev, char **envp, 272 int (*uevent)(struct class_device *dev, char **envp,
272 int num_envp, char *buffer, int buffer_size); 273 int num_envp, char *buffer, int buffer_size);
273 char class_id[BUS_ID_SIZE]; /* unique to this class */ 274 char class_id[BUS_ID_SIZE]; /* unique to this class */
274 }; 275 };
275 276
276 static inline void * 277 static inline void *
277 class_get_devdata (struct class_device *dev) 278 class_get_devdata (struct class_device *dev)
278 { 279 {
279 return dev->class_data; 280 return dev->class_data;
280 } 281 }
281 282
282 static inline void 283 static inline void
283 class_set_devdata (struct class_device *dev, void *data) 284 class_set_devdata (struct class_device *dev, void *data)
284 { 285 {
285 dev->class_data = data; 286 dev->class_data = data;
286 } 287 }
287 288
288 289
289 extern int __must_check class_device_register(struct class_device *); 290 extern int __must_check class_device_register(struct class_device *);
290 extern void class_device_unregister(struct class_device *); 291 extern void class_device_unregister(struct class_device *);
291 extern void class_device_initialize(struct class_device *); 292 extern void class_device_initialize(struct class_device *);
292 extern int __must_check class_device_add(struct class_device *); 293 extern int __must_check class_device_add(struct class_device *);
293 extern void class_device_del(struct class_device *); 294 extern void class_device_del(struct class_device *);
294 295
295 extern int class_device_rename(struct class_device *, char *); 296 extern int class_device_rename(struct class_device *, char *);
296 297
297 extern struct class_device * class_device_get(struct class_device *); 298 extern struct class_device * class_device_get(struct class_device *);
298 extern void class_device_put(struct class_device *); 299 extern void class_device_put(struct class_device *);
299 300
300 extern void class_device_remove_file(struct class_device *, 301 extern void class_device_remove_file(struct class_device *,
301 const struct class_device_attribute *); 302 const struct class_device_attribute *);
302 extern int __must_check class_device_create_bin_file(struct class_device *, 303 extern int __must_check class_device_create_bin_file(struct class_device *,
303 struct bin_attribute *); 304 struct bin_attribute *);
304 extern void class_device_remove_bin_file(struct class_device *, 305 extern void class_device_remove_bin_file(struct class_device *,
305 struct bin_attribute *); 306 struct bin_attribute *);
306 307
307 struct class_interface { 308 struct class_interface {
308 struct list_head node; 309 struct list_head node;
309 struct class *class; 310 struct class *class;
310 311
311 int (*add) (struct class_device *, struct class_interface *); 312 int (*add) (struct class_device *, struct class_interface *);
312 void (*remove) (struct class_device *, struct class_interface *); 313 void (*remove) (struct class_device *, struct class_interface *);
313 int (*add_dev) (struct device *, struct class_interface *); 314 int (*add_dev) (struct device *, struct class_interface *);
314 void (*remove_dev) (struct device *, struct class_interface *); 315 void (*remove_dev) (struct device *, struct class_interface *);
315 }; 316 };
316 317
317 extern int __must_check class_interface_register(struct class_interface *); 318 extern int __must_check class_interface_register(struct class_interface *);
318 extern void class_interface_unregister(struct class_interface *); 319 extern void class_interface_unregister(struct class_interface *);
319 320
320 extern struct class *class_create(struct module *owner, const char *name); 321 extern struct class *class_create(struct module *owner, const char *name);
321 extern void class_destroy(struct class *cls); 322 extern void class_destroy(struct class *cls);
322 extern struct class_device *class_device_create(struct class *cls, 323 extern struct class_device *class_device_create(struct class *cls,
323 struct class_device *parent, 324 struct class_device *parent,
324 dev_t devt, 325 dev_t devt,
325 struct device *device, 326 struct device *device,
326 const char *fmt, ...) 327 const char *fmt, ...)
327 __attribute__((format(printf,5,6))); 328 __attribute__((format(printf,5,6)));
328 extern void class_device_destroy(struct class *cls, dev_t devt); 329 extern void class_device_destroy(struct class *cls, dev_t devt);
329 330
330 /* interface for exporting device attributes */ 331 /* interface for exporting device attributes */
331 struct device_attribute { 332 struct device_attribute {
332 struct attribute attr; 333 struct attribute attr;
333 ssize_t (*show)(struct device *dev, struct device_attribute *attr, 334 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
334 char *buf); 335 char *buf);
335 ssize_t (*store)(struct device *dev, struct device_attribute *attr, 336 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
336 const char *buf, size_t count); 337 const char *buf, size_t count);
337 }; 338 };
338 339
339 #define DEVICE_ATTR(_name,_mode,_show,_store) \ 340 #define DEVICE_ATTR(_name,_mode,_show,_store) \
340 struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store) 341 struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
341 342
342 extern int __must_check device_create_file(struct device *device, 343 extern int __must_check device_create_file(struct device *device,
343 struct device_attribute * entry); 344 struct device_attribute * entry);
344 extern void device_remove_file(struct device * dev, struct device_attribute * attr); 345 extern void device_remove_file(struct device * dev, struct device_attribute * attr);
345 extern int __must_check device_create_bin_file(struct device *dev, 346 extern int __must_check device_create_bin_file(struct device *dev,
346 struct bin_attribute *attr); 347 struct bin_attribute *attr);
347 extern void device_remove_bin_file(struct device *dev, 348 extern void device_remove_bin_file(struct device *dev,
348 struct bin_attribute *attr); 349 struct bin_attribute *attr);
349 struct device { 350 struct device {
350 struct klist klist_children; 351 struct klist klist_children;
351 struct klist_node knode_parent; /* node in sibling list */ 352 struct klist_node knode_parent; /* node in sibling list */
352 struct klist_node knode_driver; 353 struct klist_node knode_driver;
353 struct klist_node knode_bus; 354 struct klist_node knode_bus;
354 struct device * parent; 355 struct device * parent;
355 356
356 struct kobject kobj; 357 struct kobject kobj;
357 char bus_id[BUS_ID_SIZE]; /* position on parent bus */ 358 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
358 unsigned is_registered:1; 359 unsigned is_registered:1;
359 struct device_attribute uevent_attr; 360 struct device_attribute uevent_attr;
360 struct device_attribute *devt_attr; 361 struct device_attribute *devt_attr;
361 362
362 struct semaphore sem; /* semaphore to synchronize calls to 363 struct semaphore sem; /* semaphore to synchronize calls to
363 * its driver. 364 * its driver.
364 */ 365 */
365 366
366 struct bus_type * bus; /* type of bus device is on */ 367 struct bus_type * bus; /* type of bus device is on */
367 struct device_driver *driver; /* which driver has allocated this 368 struct device_driver *driver; /* which driver has allocated this
368 device */ 369 device */
369 void *driver_data; /* data private to the driver */ 370 void *driver_data; /* data private to the driver */
370 void *platform_data; /* Platform specific data, device 371 void *platform_data; /* Platform specific data, device
371 core doesn't touch it */ 372 core doesn't touch it */
372 struct dev_pm_info power; 373 struct dev_pm_info power;
373 374
374 #ifdef CONFIG_NUMA 375 #ifdef CONFIG_NUMA
375 int numa_node; /* NUMA node this device is close to */ 376 int numa_node; /* NUMA node this device is close to */
376 #endif 377 #endif
377 u64 *dma_mask; /* dma mask (if dma'able device) */ 378 u64 *dma_mask; /* dma mask (if dma'able device) */
378 u64 coherent_dma_mask;/* Like dma_mask, but for 379 u64 coherent_dma_mask;/* Like dma_mask, but for
379 alloc_coherent mappings as 380 alloc_coherent mappings as
380 not all hardware supports 381 not all hardware supports
381 64 bit addresses for consistent 382 64 bit addresses for consistent
382 allocations such descriptors. */ 383 allocations such descriptors. */
383 384
384 struct list_head dma_pools; /* dma pools (if dma'ble) */ 385 struct list_head dma_pools; /* dma pools (if dma'ble) */
385 386
386 struct dma_coherent_mem *dma_mem; /* internal for coherent mem 387 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
387 override */ 388 override */
388 /* arch specific additions */ 389 /* arch specific additions */
389 struct dev_archdata archdata; 390 struct dev_archdata archdata;
390 391
391 /* class_device migration path */ 392 /* class_device migration path */
392 struct list_head node; 393 struct list_head node;
393 struct class *class; /* optional*/ 394 struct class *class; /* optional*/
394 dev_t devt; /* dev_t, creates the sysfs "dev" */ 395 dev_t devt; /* dev_t, creates the sysfs "dev" */
395 struct attribute_group **groups; /* optional groups */ 396 struct attribute_group **groups; /* optional groups */
396 397
397 void (*release)(struct device * dev); 398 void (*release)(struct device * dev);
398 }; 399 };
399 400
400 #ifdef CONFIG_NUMA 401 #ifdef CONFIG_NUMA
401 static inline int dev_to_node(struct device *dev) 402 static inline int dev_to_node(struct device *dev)
402 { 403 {
403 return dev->numa_node; 404 return dev->numa_node;
404 } 405 }
405 static inline void set_dev_node(struct device *dev, int node) 406 static inline void set_dev_node(struct device *dev, int node)
406 { 407 {
407 dev->numa_node = node; 408 dev->numa_node = node;
408 } 409 }
409 #else 410 #else
410 static inline int dev_to_node(struct device *dev) 411 static inline int dev_to_node(struct device *dev)
411 { 412 {
412 return -1; 413 return -1;
413 } 414 }
414 static inline void set_dev_node(struct device *dev, int node) 415 static inline void set_dev_node(struct device *dev, int node)
415 { 416 {
416 } 417 }
417 #endif 418 #endif
418 419
419 static inline void * 420 static inline void *
420 dev_get_drvdata (struct device *dev) 421 dev_get_drvdata (struct device *dev)
421 { 422 {
422 return dev->driver_data; 423 return dev->driver_data;
423 } 424 }
424 425
425 static inline void 426 static inline void
426 dev_set_drvdata (struct device *dev, void *data) 427 dev_set_drvdata (struct device *dev, void *data)
427 { 428 {
428 dev->driver_data = data; 429 dev->driver_data = data;
429 } 430 }
430 431
431 static inline int device_is_registered(struct device *dev) 432 static inline int device_is_registered(struct device *dev)
432 { 433 {
433 return dev->is_registered; 434 return dev->is_registered;
434 } 435 }
435 436
436 void driver_init(void); 437 void driver_init(void);
437 438
438 /* 439 /*
439 * High level routines for use by the bus drivers 440 * High level routines for use by the bus drivers
440 */ 441 */
441 extern int __must_check device_register(struct device * dev); 442 extern int __must_check device_register(struct device * dev);
442 extern void device_unregister(struct device * dev); 443 extern void device_unregister(struct device * dev);
443 extern void device_initialize(struct device * dev); 444 extern void device_initialize(struct device * dev);
444 extern int __must_check device_add(struct device * dev); 445 extern int __must_check device_add(struct device * dev);
445 extern void device_del(struct device * dev); 446 extern void device_del(struct device * dev);
446 extern int device_for_each_child(struct device *, void *, 447 extern int device_for_each_child(struct device *, void *,
447 int (*fn)(struct device *, void *)); 448 int (*fn)(struct device *, void *));
448 extern struct device *device_find_child(struct device *, void *data, 449 extern struct device *device_find_child(struct device *, void *data,
449 int (*match)(struct device *, void *)); 450 int (*match)(struct device *, void *));
450 extern int device_rename(struct device *dev, char *new_name); 451 extern int device_rename(struct device *dev, char *new_name);
451 extern int device_move(struct device *dev, struct device *new_parent); 452 extern int device_move(struct device *dev, struct device *new_parent);
452 453
453 /* 454 /*
454 * Manual binding of a device to driver. See drivers/base/bus.c 455 * Manual binding of a device to driver. See drivers/base/bus.c
455 * for information on use. 456 * for information on use.
456 */ 457 */
457 extern int __must_check device_bind_driver(struct device *dev); 458 extern int __must_check device_bind_driver(struct device *dev);
458 extern void device_release_driver(struct device * dev); 459 extern void device_release_driver(struct device * dev);
459 extern int __must_check device_attach(struct device * dev); 460 extern int __must_check device_attach(struct device * dev);
460 extern int __must_check driver_attach(struct device_driver *drv); 461 extern int __must_check driver_attach(struct device_driver *drv);
461 extern int __must_check device_reprobe(struct device *dev); 462 extern int __must_check device_reprobe(struct device *dev);
462 463
463 /* 464 /*
464 * Easy functions for dynamically creating devices on the fly 465 * Easy functions for dynamically creating devices on the fly
465 */ 466 */
466 extern struct device *device_create(struct class *cls, struct device *parent, 467 extern struct device *device_create(struct class *cls, struct device *parent,
467 dev_t devt, const char *fmt, ...) 468 dev_t devt, const char *fmt, ...)
468 __attribute__((format(printf,4,5))); 469 __attribute__((format(printf,4,5)));
469 extern void device_destroy(struct class *cls, dev_t devt); 470 extern void device_destroy(struct class *cls, dev_t devt);
470 471
471 /* 472 /*
472 * Platform "fixup" functions - allow the platform to have their say 473 * Platform "fixup" functions - allow the platform to have their say
473 * about devices and actions that the general device layer doesn't 474 * about devices and actions that the general device layer doesn't
474 * know about. 475 * know about.
475 */ 476 */
476 /* Notify platform of device discovery */ 477 /* Notify platform of device discovery */
477 extern int (*platform_notify)(struct device * dev); 478 extern int (*platform_notify)(struct device * dev);
478 479
479 extern int (*platform_notify_remove)(struct device * dev); 480 extern int (*platform_notify_remove)(struct device * dev);
480 481
481 482
482 /** 483 /**
483 * get_device - atomically increment the reference count for the device. 484 * get_device - atomically increment the reference count for the device.
484 * 485 *
485 */ 486 */
486 extern struct device * get_device(struct device * dev); 487 extern struct device * get_device(struct device * dev);
487 extern void put_device(struct device * dev); 488 extern void put_device(struct device * dev);
488 489
489 490
490 /* drivers/base/power/shutdown.c */ 491 /* drivers/base/power/shutdown.c */
491 extern void device_shutdown(void); 492 extern void device_shutdown(void);
492 493
493 494
494 /* drivers/base/firmware.c */ 495 /* drivers/base/firmware.c */
495 extern int __must_check firmware_register(struct subsystem *); 496 extern int __must_check firmware_register(struct subsystem *);
496 extern void firmware_unregister(struct subsystem *); 497 extern void firmware_unregister(struct subsystem *);
497 498
498 /* debugging and troubleshooting/diagnostic helpers. */ 499 /* debugging and troubleshooting/diagnostic helpers. */
499 extern const char *dev_driver_string(struct device *dev); 500 extern const char *dev_driver_string(struct device *dev);
500 #define dev_printk(level, dev, format, arg...) \ 501 #define dev_printk(level, dev, format, arg...) \
501 printk(level "%s %s: " format , dev_driver_string(dev) , (dev)->bus_id , ## arg) 502 printk(level "%s %s: " format , dev_driver_string(dev) , (dev)->bus_id , ## arg)
502 503
503 #ifdef DEBUG 504 #ifdef DEBUG
504 #define dev_dbg(dev, format, arg...) \ 505 #define dev_dbg(dev, format, arg...) \
505 dev_printk(KERN_DEBUG , dev , format , ## arg) 506 dev_printk(KERN_DEBUG , dev , format , ## arg)
506 #else 507 #else
507 #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0) 508 #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
508 #endif 509 #endif
509 510
510 #define dev_err(dev, format, arg...) \ 511 #define dev_err(dev, format, arg...) \
511 dev_printk(KERN_ERR , dev , format , ## arg) 512 dev_printk(KERN_ERR , dev , format , ## arg)
512 #define dev_info(dev, format, arg...) \ 513 #define dev_info(dev, format, arg...) \
513 dev_printk(KERN_INFO , dev , format , ## arg) 514 dev_printk(KERN_INFO , dev , format , ## arg)
514 #define dev_warn(dev, format, arg...) \ 515 #define dev_warn(dev, format, arg...) \
515 dev_printk(KERN_WARNING , dev , format , ## arg) 516 dev_printk(KERN_WARNING , dev , format , ## arg)
516 #define dev_notice(dev, format, arg...) \ 517 #define dev_notice(dev, format, arg...) \
517 dev_printk(KERN_NOTICE , dev , format , ## arg) 518 dev_printk(KERN_NOTICE , dev , format , ## arg)
518 519
519 /* Create alias, so I can be autoloaded. */ 520 /* Create alias, so I can be autoloaded. */
520 #define MODULE_ALIAS_CHARDEV(major,minor) \ 521 #define MODULE_ALIAS_CHARDEV(major,minor) \
521 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor)) 522 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
522 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \ 523 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
523 MODULE_ALIAS("char-major-" __stringify(major) "-*") 524 MODULE_ALIAS("char-major-" __stringify(major) "-*")
524 #endif /* _DEVICE_H_ */ 525 #endif /* _DEVICE_H_ */
525 526
include/linux/module.h
1 #ifndef _LINUX_MODULE_H 1 #ifndef _LINUX_MODULE_H
2 #define _LINUX_MODULE_H 2 #define _LINUX_MODULE_H
3 /* 3 /*
4 * Dynamic loading of modules into the kernel. 4 * Dynamic loading of modules into the kernel.
5 * 5 *
6 * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996 6 * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
7 * Rewritten again by Rusty Russell, 2002 7 * Rewritten again by Rusty Russell, 2002
8 */ 8 */
9 #include <linux/spinlock.h> 9 #include <linux/spinlock.h>
10 #include <linux/list.h> 10 #include <linux/list.h>
11 #include <linux/stat.h> 11 #include <linux/stat.h>
12 #include <linux/compiler.h> 12 #include <linux/compiler.h>
13 #include <linux/cache.h> 13 #include <linux/cache.h>
14 #include <linux/kmod.h> 14 #include <linux/kmod.h>
15 #include <linux/elf.h> 15 #include <linux/elf.h>
16 #include <linux/stringify.h> 16 #include <linux/stringify.h>
17 #include <linux/kobject.h> 17 #include <linux/kobject.h>
18 #include <linux/moduleparam.h> 18 #include <linux/moduleparam.h>
19 #include <asm/local.h> 19 #include <asm/local.h>
20 20
21 #include <asm/module.h> 21 #include <asm/module.h>
22 22
23 /* Not Yet Implemented */ 23 /* Not Yet Implemented */
24 #define MODULE_SUPPORTED_DEVICE(name) 24 #define MODULE_SUPPORTED_DEVICE(name)
25 25
26 /* v850 toolchain uses a `_' prefix for all user symbols */ 26 /* v850 toolchain uses a `_' prefix for all user symbols */
27 #ifndef MODULE_SYMBOL_PREFIX 27 #ifndef MODULE_SYMBOL_PREFIX
28 #define MODULE_SYMBOL_PREFIX "" 28 #define MODULE_SYMBOL_PREFIX ""
29 #endif 29 #endif
30 30
31 #define MODULE_NAME_LEN (64 - sizeof(unsigned long)) 31 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
32 32
33 struct kernel_symbol 33 struct kernel_symbol
34 { 34 {
35 unsigned long value; 35 unsigned long value;
36 const char *name; 36 const char *name;
37 }; 37 };
38 38
39 struct modversion_info 39 struct modversion_info
40 { 40 {
41 unsigned long crc; 41 unsigned long crc;
42 char name[MODULE_NAME_LEN]; 42 char name[MODULE_NAME_LEN];
43 }; 43 };
44 44
45 struct module; 45 struct module;
46 46
47 struct module_attribute { 47 struct module_attribute {
48 struct attribute attr; 48 struct attribute attr;
49 ssize_t (*show)(struct module_attribute *, struct module *, char *); 49 ssize_t (*show)(struct module_attribute *, struct module *, char *);
50 ssize_t (*store)(struct module_attribute *, struct module *, 50 ssize_t (*store)(struct module_attribute *, struct module *,
51 const char *, size_t count); 51 const char *, size_t count);
52 void (*setup)(struct module *, const char *); 52 void (*setup)(struct module *, const char *);
53 int (*test)(struct module *); 53 int (*test)(struct module *);
54 void (*free)(struct module *); 54 void (*free)(struct module *);
55 }; 55 };
56 56
57 struct module_kobject 57 struct module_kobject
58 { 58 {
59 struct kobject kobj; 59 struct kobject kobj;
60 struct module *mod; 60 struct module *mod;
61 struct kobject *drivers_dir;
61 }; 62 };
62 63
63 /* These are either module local, or the kernel's dummy ones. */ 64 /* These are either module local, or the kernel's dummy ones. */
64 extern int init_module(void); 65 extern int init_module(void);
65 extern void cleanup_module(void); 66 extern void cleanup_module(void);
66 67
67 /* Archs provide a method of finding the correct exception table. */ 68 /* Archs provide a method of finding the correct exception table. */
68 struct exception_table_entry; 69 struct exception_table_entry;
69 70
70 const struct exception_table_entry * 71 const struct exception_table_entry *
71 search_extable(const struct exception_table_entry *first, 72 search_extable(const struct exception_table_entry *first,
72 const struct exception_table_entry *last, 73 const struct exception_table_entry *last,
73 unsigned long value); 74 unsigned long value);
74 void sort_extable(struct exception_table_entry *start, 75 void sort_extable(struct exception_table_entry *start,
75 struct exception_table_entry *finish); 76 struct exception_table_entry *finish);
76 void sort_main_extable(void); 77 void sort_main_extable(void);
77 78
78 extern struct subsystem module_subsys; 79 extern struct subsystem module_subsys;
79 80
80 #ifdef MODULE 81 #ifdef MODULE
81 #define MODULE_GENERIC_TABLE(gtype,name) \ 82 #define MODULE_GENERIC_TABLE(gtype,name) \
82 extern const struct gtype##_id __mod_##gtype##_table \ 83 extern const struct gtype##_id __mod_##gtype##_table \
83 __attribute__ ((unused, alias(__stringify(name)))) 84 __attribute__ ((unused, alias(__stringify(name))))
84 85
85 extern struct module __this_module; 86 extern struct module __this_module;
86 #define THIS_MODULE (&__this_module) 87 #define THIS_MODULE (&__this_module)
87 #else /* !MODULE */ 88 #else /* !MODULE */
88 #define MODULE_GENERIC_TABLE(gtype,name) 89 #define MODULE_GENERIC_TABLE(gtype,name)
89 #define THIS_MODULE ((struct module *)0) 90 #define THIS_MODULE ((struct module *)0)
90 #endif 91 #endif
91 92
92 /* Generic info of form tag = "info" */ 93 /* Generic info of form tag = "info" */
93 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info) 94 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
94 95
95 /* For userspace: you can also call me... */ 96 /* For userspace: you can also call me... */
96 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias) 97 #define MODULE_ALIAS(_alias) MODULE_INFO(alias, _alias)
97 98
98 /* 99 /*
99 * The following license idents are currently accepted as indicating free 100 * The following license idents are currently accepted as indicating free
100 * software modules 101 * software modules
101 * 102 *
102 * "GPL" [GNU Public License v2 or later] 103 * "GPL" [GNU Public License v2 or later]
103 * "GPL v2" [GNU Public License v2] 104 * "GPL v2" [GNU Public License v2]
104 * "GPL and additional rights" [GNU Public License v2 rights and more] 105 * "GPL and additional rights" [GNU Public License v2 rights and more]
105 * "Dual BSD/GPL" [GNU Public License v2 106 * "Dual BSD/GPL" [GNU Public License v2
106 * or BSD license choice] 107 * or BSD license choice]
107 * "Dual MIT/GPL" [GNU Public License v2 108 * "Dual MIT/GPL" [GNU Public License v2
108 * or MIT license choice] 109 * or MIT license choice]
109 * "Dual MPL/GPL" [GNU Public License v2 110 * "Dual MPL/GPL" [GNU Public License v2
110 * or Mozilla license choice] 111 * or Mozilla license choice]
111 * 112 *
112 * The following other idents are available 113 * The following other idents are available
113 * 114 *
114 * "Proprietary" [Non free products] 115 * "Proprietary" [Non free products]
115 * 116 *
116 * There are dual licensed components, but when running with Linux it is the 117 * There are dual licensed components, but when running with Linux it is the
117 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL 118 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
118 * is a GPL combined work. 119 * is a GPL combined work.
119 * 120 *
120 * This exists for several reasons 121 * This exists for several reasons
121 * 1. So modinfo can show license info for users wanting to vet their setup 122 * 1. So modinfo can show license info for users wanting to vet their setup
122 * is free 123 * is free
123 * 2. So the community can ignore bug reports including proprietary modules 124 * 2. So the community can ignore bug reports including proprietary modules
124 * 3. So vendors can do likewise based on their own policies 125 * 3. So vendors can do likewise based on their own policies
125 */ 126 */
126 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license) 127 #define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
127 128
128 /* Author, ideally of form NAME <EMAIL>[, NAME <EMAIL>]*[ and NAME <EMAIL>] */ 129 /* Author, ideally of form NAME <EMAIL>[, NAME <EMAIL>]*[ and NAME <EMAIL>] */
129 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author) 130 #define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
130 131
131 /* What your module does. */ 132 /* What your module does. */
132 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description) 133 #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
133 134
134 /* One for each parameter, describing how to use it. Some files do 135 /* One for each parameter, describing how to use it. Some files do
135 multiple of these per line, so can't just use MODULE_INFO. */ 136 multiple of these per line, so can't just use MODULE_INFO. */
136 #define MODULE_PARM_DESC(_parm, desc) \ 137 #define MODULE_PARM_DESC(_parm, desc) \
137 __MODULE_INFO(parm, _parm, #_parm ":" desc) 138 __MODULE_INFO(parm, _parm, #_parm ":" desc)
138 139
139 #define MODULE_DEVICE_TABLE(type,name) \ 140 #define MODULE_DEVICE_TABLE(type,name) \
140 MODULE_GENERIC_TABLE(type##_device,name) 141 MODULE_GENERIC_TABLE(type##_device,name)
141 142
142 /* Version of form [<epoch>:]<version>[-<extra-version>]. 143 /* Version of form [<epoch>:]<version>[-<extra-version>].
143 Or for CVS/RCS ID version, everything but the number is stripped. 144 Or for CVS/RCS ID version, everything but the number is stripped.
144 <epoch>: A (small) unsigned integer which allows you to start versions 145 <epoch>: A (small) unsigned integer which allows you to start versions
145 anew. If not mentioned, it's zero. eg. "2:1.0" is after 146 anew. If not mentioned, it's zero. eg. "2:1.0" is after
146 "1:2.0". 147 "1:2.0".
147 <version>: The <version> may contain only alphanumerics and the 148 <version>: The <version> may contain only alphanumerics and the
148 character `.'. Ordered by numeric sort for numeric parts, 149 character `.'. Ordered by numeric sort for numeric parts,
149 ascii sort for ascii parts (as per RPM or DEB algorithm). 150 ascii sort for ascii parts (as per RPM or DEB algorithm).
150 <extraversion>: Like <version>, but inserted for local 151 <extraversion>: Like <version>, but inserted for local
151 customizations, eg "rh3" or "rusty1". 152 customizations, eg "rh3" or "rusty1".
152 153
153 Using this automatically adds a checksum of the .c files and the 154 Using this automatically adds a checksum of the .c files and the
154 local headers in "srcversion". 155 local headers in "srcversion".
155 */ 156 */
156 #define MODULE_VERSION(_version) MODULE_INFO(version, _version) 157 #define MODULE_VERSION(_version) MODULE_INFO(version, _version)
157 158
158 /* Optional firmware file (or files) needed by the module 159 /* Optional firmware file (or files) needed by the module
159 * format is simply firmware file name. Multiple firmware 160 * format is simply firmware file name. Multiple firmware
160 * files require multiple MODULE_FIRMWARE() specifiers */ 161 * files require multiple MODULE_FIRMWARE() specifiers */
161 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware) 162 #define MODULE_FIRMWARE(_firmware) MODULE_INFO(firmware, _firmware)
162 163
163 /* Given an address, look for it in the exception tables */ 164 /* Given an address, look for it in the exception tables */
164 const struct exception_table_entry *search_exception_tables(unsigned long add); 165 const struct exception_table_entry *search_exception_tables(unsigned long add);
165 166
166 struct notifier_block; 167 struct notifier_block;
167 168
168 #ifdef CONFIG_MODULES 169 #ifdef CONFIG_MODULES
169 170
170 /* Get/put a kernel symbol (calls must be symmetric) */ 171 /* Get/put a kernel symbol (calls must be symmetric) */
171 void *__symbol_get(const char *symbol); 172 void *__symbol_get(const char *symbol);
172 void *__symbol_get_gpl(const char *symbol); 173 void *__symbol_get_gpl(const char *symbol);
173 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x))) 174 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
174 175
175 #ifndef __GENKSYMS__ 176 #ifndef __GENKSYMS__
176 #ifdef CONFIG_MODVERSIONS 177 #ifdef CONFIG_MODVERSIONS
177 /* Mark the CRC weak since genksyms apparently decides not to 178 /* Mark the CRC weak since genksyms apparently decides not to
178 * generate a checksums for some symbols */ 179 * generate a checksums for some symbols */
179 #define __CRC_SYMBOL(sym, sec) \ 180 #define __CRC_SYMBOL(sym, sec) \
180 extern void *__crc_##sym __attribute__((weak)); \ 181 extern void *__crc_##sym __attribute__((weak)); \
181 static const unsigned long __kcrctab_##sym \ 182 static const unsigned long __kcrctab_##sym \
182 __attribute_used__ \ 183 __attribute_used__ \
183 __attribute__((section("__kcrctab" sec), unused)) \ 184 __attribute__((section("__kcrctab" sec), unused)) \
184 = (unsigned long) &__crc_##sym; 185 = (unsigned long) &__crc_##sym;
185 #else 186 #else
186 #define __CRC_SYMBOL(sym, sec) 187 #define __CRC_SYMBOL(sym, sec)
187 #endif 188 #endif
188 189
189 /* For every exported symbol, place a struct in the __ksymtab section */ 190 /* For every exported symbol, place a struct in the __ksymtab section */
190 #define __EXPORT_SYMBOL(sym, sec) \ 191 #define __EXPORT_SYMBOL(sym, sec) \
191 extern typeof(sym) sym; \ 192 extern typeof(sym) sym; \
192 __CRC_SYMBOL(sym, sec) \ 193 __CRC_SYMBOL(sym, sec) \
193 static const char __kstrtab_##sym[] \ 194 static const char __kstrtab_##sym[] \
194 __attribute__((section("__ksymtab_strings"))) \ 195 __attribute__((section("__ksymtab_strings"))) \
195 = MODULE_SYMBOL_PREFIX #sym; \ 196 = MODULE_SYMBOL_PREFIX #sym; \
196 static const struct kernel_symbol __ksymtab_##sym \ 197 static const struct kernel_symbol __ksymtab_##sym \
197 __attribute_used__ \ 198 __attribute_used__ \
198 __attribute__((section("__ksymtab" sec), unused)) \ 199 __attribute__((section("__ksymtab" sec), unused)) \
199 = { (unsigned long)&sym, __kstrtab_##sym } 200 = { (unsigned long)&sym, __kstrtab_##sym }
200 201
201 #define EXPORT_SYMBOL(sym) \ 202 #define EXPORT_SYMBOL(sym) \
202 __EXPORT_SYMBOL(sym, "") 203 __EXPORT_SYMBOL(sym, "")
203 204
204 #define EXPORT_SYMBOL_GPL(sym) \ 205 #define EXPORT_SYMBOL_GPL(sym) \
205 __EXPORT_SYMBOL(sym, "_gpl") 206 __EXPORT_SYMBOL(sym, "_gpl")
206 207
207 #define EXPORT_SYMBOL_GPL_FUTURE(sym) \ 208 #define EXPORT_SYMBOL_GPL_FUTURE(sym) \
208 __EXPORT_SYMBOL(sym, "_gpl_future") 209 __EXPORT_SYMBOL(sym, "_gpl_future")
209 210
210 211
211 #ifdef CONFIG_UNUSED_SYMBOLS 212 #ifdef CONFIG_UNUSED_SYMBOLS
212 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused") 213 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
213 #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl") 214 #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
214 #else 215 #else
215 #define EXPORT_UNUSED_SYMBOL(sym) 216 #define EXPORT_UNUSED_SYMBOL(sym)
216 #define EXPORT_UNUSED_SYMBOL_GPL(sym) 217 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
217 #endif 218 #endif
218 219
219 #endif 220 #endif
220 221
221 struct module_ref 222 struct module_ref
222 { 223 {
223 local_t count; 224 local_t count;
224 } ____cacheline_aligned; 225 } ____cacheline_aligned;
225 226
226 enum module_state 227 enum module_state
227 { 228 {
228 MODULE_STATE_LIVE, 229 MODULE_STATE_LIVE,
229 MODULE_STATE_COMING, 230 MODULE_STATE_COMING,
230 MODULE_STATE_GOING, 231 MODULE_STATE_GOING,
231 }; 232 };
232 233
233 /* Similar stuff for section attributes. */ 234 /* Similar stuff for section attributes. */
234 struct module_sect_attr 235 struct module_sect_attr
235 { 236 {
236 struct module_attribute mattr; 237 struct module_attribute mattr;
237 char *name; 238 char *name;
238 unsigned long address; 239 unsigned long address;
239 }; 240 };
240 241
241 struct module_sect_attrs 242 struct module_sect_attrs
242 { 243 {
243 struct attribute_group grp; 244 struct attribute_group grp;
244 int nsections; 245 int nsections;
245 struct module_sect_attr attrs[0]; 246 struct module_sect_attr attrs[0];
246 }; 247 };
247 248
248 struct module_param_attrs; 249 struct module_param_attrs;
249 250
250 struct module 251 struct module
251 { 252 {
252 enum module_state state; 253 enum module_state state;
253 254
254 /* Member of list of modules */ 255 /* Member of list of modules */
255 struct list_head list; 256 struct list_head list;
256 257
257 /* Unique handle for this module */ 258 /* Unique handle for this module */
258 char name[MODULE_NAME_LEN]; 259 char name[MODULE_NAME_LEN];
259 260
260 /* Sysfs stuff. */ 261 /* Sysfs stuff. */
261 struct module_kobject mkobj; 262 struct module_kobject mkobj;
262 struct module_param_attrs *param_attrs; 263 struct module_param_attrs *param_attrs;
263 struct module_attribute *modinfo_attrs; 264 struct module_attribute *modinfo_attrs;
264 const char *version; 265 const char *version;
265 const char *srcversion; 266 const char *srcversion;
266 struct kobject *drivers_dir;
267 267
268 /* Exported symbols */ 268 /* Exported symbols */
269 const struct kernel_symbol *syms; 269 const struct kernel_symbol *syms;
270 unsigned int num_syms; 270 unsigned int num_syms;
271 const unsigned long *crcs; 271 const unsigned long *crcs;
272 272
273 /* GPL-only exported symbols. */ 273 /* GPL-only exported symbols. */
274 const struct kernel_symbol *gpl_syms; 274 const struct kernel_symbol *gpl_syms;
275 unsigned int num_gpl_syms; 275 unsigned int num_gpl_syms;
276 const unsigned long *gpl_crcs; 276 const unsigned long *gpl_crcs;
277 277
278 /* unused exported symbols. */ 278 /* unused exported symbols. */
279 const struct kernel_symbol *unused_syms; 279 const struct kernel_symbol *unused_syms;
280 unsigned int num_unused_syms; 280 unsigned int num_unused_syms;
281 const unsigned long *unused_crcs; 281 const unsigned long *unused_crcs;
282 /* GPL-only, unused exported symbols. */ 282 /* GPL-only, unused exported symbols. */
283 const struct kernel_symbol *unused_gpl_syms; 283 const struct kernel_symbol *unused_gpl_syms;
284 unsigned int num_unused_gpl_syms; 284 unsigned int num_unused_gpl_syms;
285 const unsigned long *unused_gpl_crcs; 285 const unsigned long *unused_gpl_crcs;
286 286
287 /* symbols that will be GPL-only in the near future. */ 287 /* symbols that will be GPL-only in the near future. */
288 const struct kernel_symbol *gpl_future_syms; 288 const struct kernel_symbol *gpl_future_syms;
289 unsigned int num_gpl_future_syms; 289 unsigned int num_gpl_future_syms;
290 const unsigned long *gpl_future_crcs; 290 const unsigned long *gpl_future_crcs;
291 291
292 /* Exception table */ 292 /* Exception table */
293 unsigned int num_exentries; 293 unsigned int num_exentries;
294 const struct exception_table_entry *extable; 294 const struct exception_table_entry *extable;
295 295
296 /* Startup function. */ 296 /* Startup function. */
297 int (*init)(void); 297 int (*init)(void);
298 298
299 /* If this is non-NULL, vfree after init() returns */ 299 /* If this is non-NULL, vfree after init() returns */
300 void *module_init; 300 void *module_init;
301 301
302 /* Here is the actual code + data, vfree'd on unload. */ 302 /* Here is the actual code + data, vfree'd on unload. */
303 void *module_core; 303 void *module_core;
304 304
305 /* Here are the sizes of the init and core sections */ 305 /* Here are the sizes of the init and core sections */
306 unsigned long init_size, core_size; 306 unsigned long init_size, core_size;
307 307
308 /* The size of the executable code in each section. */ 308 /* The size of the executable code in each section. */
309 unsigned long init_text_size, core_text_size; 309 unsigned long init_text_size, core_text_size;
310 310
311 /* The handle returned from unwind_add_table. */ 311 /* The handle returned from unwind_add_table. */
312 void *unwind_info; 312 void *unwind_info;
313 313
314 /* Arch-specific module values */ 314 /* Arch-specific module values */
315 struct mod_arch_specific arch; 315 struct mod_arch_specific arch;
316 316
317 /* Am I unsafe to unload? */ 317 /* Am I unsafe to unload? */
318 int unsafe; 318 int unsafe;
319 319
320 unsigned int taints; /* same bits as kernel:tainted */ 320 unsigned int taints; /* same bits as kernel:tainted */
321 321
322 #ifdef CONFIG_GENERIC_BUG 322 #ifdef CONFIG_GENERIC_BUG
323 /* Support for BUG */ 323 /* Support for BUG */
324 struct list_head bug_list; 324 struct list_head bug_list;
325 struct bug_entry *bug_table; 325 struct bug_entry *bug_table;
326 unsigned num_bugs; 326 unsigned num_bugs;
327 #endif 327 #endif
328 328
329 #ifdef CONFIG_MODULE_UNLOAD 329 #ifdef CONFIG_MODULE_UNLOAD
330 /* Reference counts */ 330 /* Reference counts */
331 struct module_ref ref[NR_CPUS]; 331 struct module_ref ref[NR_CPUS];
332 332
333 /* What modules depend on me? */ 333 /* What modules depend on me? */
334 struct list_head modules_which_use_me; 334 struct list_head modules_which_use_me;
335 335
336 /* Who is waiting for us to be unloaded */ 336 /* Who is waiting for us to be unloaded */
337 struct task_struct *waiter; 337 struct task_struct *waiter;
338 338
339 /* Destruction function. */ 339 /* Destruction function. */
340 void (*exit)(void); 340 void (*exit)(void);
341 #endif 341 #endif
342 342
343 #ifdef CONFIG_KALLSYMS 343 #ifdef CONFIG_KALLSYMS
344 /* We keep the symbol and string tables for kallsyms. */ 344 /* We keep the symbol and string tables for kallsyms. */
345 Elf_Sym *symtab; 345 Elf_Sym *symtab;
346 unsigned long num_symtab; 346 unsigned long num_symtab;
347 char *strtab; 347 char *strtab;
348 348
349 /* Section attributes */ 349 /* Section attributes */
350 struct module_sect_attrs *sect_attrs; 350 struct module_sect_attrs *sect_attrs;
351 #endif 351 #endif
352 352
353 /* Per-cpu data. */ 353 /* Per-cpu data. */
354 void *percpu; 354 void *percpu;
355 355
356 /* The command line arguments (may be mangled). People like 356 /* The command line arguments (may be mangled). People like
357 keeping pointers to this stuff */ 357 keeping pointers to this stuff */
358 char *args; 358 char *args;
359 }; 359 };
360 360
361 /* FIXME: It'd be nice to isolate modules during init, too, so they 361 /* FIXME: It'd be nice to isolate modules during init, too, so they
362 aren't used before they (may) fail. But presently too much code 362 aren't used before they (may) fail. But presently too much code
363 (IDE & SCSI) require entry into the module during init.*/ 363 (IDE & SCSI) require entry into the module during init.*/
364 static inline int module_is_live(struct module *mod) 364 static inline int module_is_live(struct module *mod)
365 { 365 {
366 return mod->state != MODULE_STATE_GOING; 366 return mod->state != MODULE_STATE_GOING;
367 } 367 }
368 368
369 /* Is this address in a module? (second is with no locks, for oops) */ 369 /* Is this address in a module? (second is with no locks, for oops) */
370 struct module *module_text_address(unsigned long addr); 370 struct module *module_text_address(unsigned long addr);
371 struct module *__module_text_address(unsigned long addr); 371 struct module *__module_text_address(unsigned long addr);
372 int is_module_address(unsigned long addr); 372 int is_module_address(unsigned long addr);
373 373
374 /* Returns module and fills in value, defined and namebuf, or NULL if 374 /* Returns module and fills in value, defined and namebuf, or NULL if
375 symnum out of range. */ 375 symnum out of range. */
376 struct module *module_get_kallsym(unsigned int symnum, unsigned long *value, 376 struct module *module_get_kallsym(unsigned int symnum, unsigned long *value,
377 char *type, char *name, size_t namelen); 377 char *type, char *name, size_t namelen);
378 378
379 /* Look for this name: can be of form module:name. */ 379 /* Look for this name: can be of form module:name. */
380 unsigned long module_kallsyms_lookup_name(const char *name); 380 unsigned long module_kallsyms_lookup_name(const char *name);
381 381
382 int is_exported(const char *name, const struct module *mod); 382 int is_exported(const char *name, const struct module *mod);
383 383
384 extern void __module_put_and_exit(struct module *mod, long code) 384 extern void __module_put_and_exit(struct module *mod, long code)
385 __attribute__((noreturn)); 385 __attribute__((noreturn));
386 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code); 386 #define module_put_and_exit(code) __module_put_and_exit(THIS_MODULE, code);
387 387
388 #ifdef CONFIG_MODULE_UNLOAD 388 #ifdef CONFIG_MODULE_UNLOAD
389 unsigned int module_refcount(struct module *mod); 389 unsigned int module_refcount(struct module *mod);
390 void __symbol_put(const char *symbol); 390 void __symbol_put(const char *symbol);
391 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x) 391 #define symbol_put(x) __symbol_put(MODULE_SYMBOL_PREFIX #x)
392 void symbol_put_addr(void *addr); 392 void symbol_put_addr(void *addr);
393 393
394 /* Sometimes we know we already have a refcount, and it's easier not 394 /* Sometimes we know we already have a refcount, and it's easier not
395 to handle the error case (which only happens with rmmod --wait). */ 395 to handle the error case (which only happens with rmmod --wait). */
396 static inline void __module_get(struct module *module) 396 static inline void __module_get(struct module *module)
397 { 397 {
398 if (module) { 398 if (module) {
399 BUG_ON(module_refcount(module) == 0); 399 BUG_ON(module_refcount(module) == 0);
400 local_inc(&module->ref[get_cpu()].count); 400 local_inc(&module->ref[get_cpu()].count);
401 put_cpu(); 401 put_cpu();
402 } 402 }
403 } 403 }
404 404
405 static inline int try_module_get(struct module *module) 405 static inline int try_module_get(struct module *module)
406 { 406 {
407 int ret = 1; 407 int ret = 1;
408 408
409 if (module) { 409 if (module) {
410 unsigned int cpu = get_cpu(); 410 unsigned int cpu = get_cpu();
411 if (likely(module_is_live(module))) 411 if (likely(module_is_live(module)))
412 local_inc(&module->ref[cpu].count); 412 local_inc(&module->ref[cpu].count);
413 else 413 else
414 ret = 0; 414 ret = 0;
415 put_cpu(); 415 put_cpu();
416 } 416 }
417 return ret; 417 return ret;
418 } 418 }
419 419
420 extern void module_put(struct module *module); 420 extern void module_put(struct module *module);
421 421
422 #else /*!CONFIG_MODULE_UNLOAD*/ 422 #else /*!CONFIG_MODULE_UNLOAD*/
423 static inline int try_module_get(struct module *module) 423 static inline int try_module_get(struct module *module)
424 { 424 {
425 return !module || module_is_live(module); 425 return !module || module_is_live(module);
426 } 426 }
427 static inline void module_put(struct module *module) 427 static inline void module_put(struct module *module)
428 { 428 {
429 } 429 }
430 static inline void __module_get(struct module *module) 430 static inline void __module_get(struct module *module)
431 { 431 {
432 } 432 }
433 #define symbol_put(x) do { } while(0) 433 #define symbol_put(x) do { } while(0)
434 #define symbol_put_addr(p) do { } while(0) 434 #define symbol_put_addr(p) do { } while(0)
435 435
436 #endif /* CONFIG_MODULE_UNLOAD */ 436 #endif /* CONFIG_MODULE_UNLOAD */
437 437
438 /* This is a #define so the string doesn't get put in every .o file */ 438 /* This is a #define so the string doesn't get put in every .o file */
439 #define module_name(mod) \ 439 #define module_name(mod) \
440 ({ \ 440 ({ \
441 struct module *__mod = (mod); \ 441 struct module *__mod = (mod); \
442 __mod ? __mod->name : "kernel"; \ 442 __mod ? __mod->name : "kernel"; \
443 }) 443 })
444 444
445 #define __unsafe(mod) \ 445 #define __unsafe(mod) \
446 do { \ 446 do { \
447 if (mod && !(mod)->unsafe) { \ 447 if (mod && !(mod)->unsafe) { \
448 printk(KERN_WARNING \ 448 printk(KERN_WARNING \
449 "Module %s cannot be unloaded due to unsafe usage in" \ 449 "Module %s cannot be unloaded due to unsafe usage in" \
450 " %s:%u\n", (mod)->name, __FILE__, __LINE__); \ 450 " %s:%u\n", (mod)->name, __FILE__, __LINE__); \
451 (mod)->unsafe = 1; \ 451 (mod)->unsafe = 1; \
452 } \ 452 } \
453 } while(0) 453 } while(0)
454 454
455 /* For kallsyms to ask for address resolution. NULL means not found. */ 455 /* For kallsyms to ask for address resolution. NULL means not found. */
456 const char *module_address_lookup(unsigned long addr, 456 const char *module_address_lookup(unsigned long addr,
457 unsigned long *symbolsize, 457 unsigned long *symbolsize,
458 unsigned long *offset, 458 unsigned long *offset,
459 char **modname); 459 char **modname);
460 460
461 /* For extable.c to search modules' exception tables. */ 461 /* For extable.c to search modules' exception tables. */
462 const struct exception_table_entry *search_module_extables(unsigned long addr); 462 const struct exception_table_entry *search_module_extables(unsigned long addr);
463 463
464 int register_module_notifier(struct notifier_block * nb); 464 int register_module_notifier(struct notifier_block * nb);
465 int unregister_module_notifier(struct notifier_block * nb); 465 int unregister_module_notifier(struct notifier_block * nb);
466 466
467 extern void print_modules(void); 467 extern void print_modules(void);
468 468
469 struct device_driver; 469 struct device_driver;
470 void module_add_driver(struct module *, struct device_driver *); 470 void module_add_driver(struct module *, struct device_driver *);
471 void module_remove_driver(struct device_driver *); 471 void module_remove_driver(struct device_driver *);
472 472
473 #else /* !CONFIG_MODULES... */ 473 #else /* !CONFIG_MODULES... */
474 #define EXPORT_SYMBOL(sym) 474 #define EXPORT_SYMBOL(sym)
475 #define EXPORT_SYMBOL_GPL(sym) 475 #define EXPORT_SYMBOL_GPL(sym)
476 #define EXPORT_SYMBOL_GPL_FUTURE(sym) 476 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
477 #define EXPORT_UNUSED_SYMBOL(sym) 477 #define EXPORT_UNUSED_SYMBOL(sym)
478 #define EXPORT_UNUSED_SYMBOL_GPL(sym) 478 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
479 479
480 /* Given an address, look for it in the exception tables. */ 480 /* Given an address, look for it in the exception tables. */
481 static inline const struct exception_table_entry * 481 static inline const struct exception_table_entry *
482 search_module_extables(unsigned long addr) 482 search_module_extables(unsigned long addr)
483 { 483 {
484 return NULL; 484 return NULL;
485 } 485 }
486 486
487 /* Is this address in a module? */ 487 /* Is this address in a module? */
488 static inline struct module *module_text_address(unsigned long addr) 488 static inline struct module *module_text_address(unsigned long addr)
489 { 489 {
490 return NULL; 490 return NULL;
491 } 491 }
492 492
493 /* Is this address in a module? (don't take a lock, we're oopsing) */ 493 /* Is this address in a module? (don't take a lock, we're oopsing) */
494 static inline struct module *__module_text_address(unsigned long addr) 494 static inline struct module *__module_text_address(unsigned long addr)
495 { 495 {
496 return NULL; 496 return NULL;
497 } 497 }
498 498
499 static inline int is_module_address(unsigned long addr) 499 static inline int is_module_address(unsigned long addr)
500 { 500 {
501 return 0; 501 return 0;
502 } 502 }
503 503
504 /* Get/put a kernel symbol (calls should be symmetric) */ 504 /* Get/put a kernel symbol (calls should be symmetric) */
505 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); }) 505 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
506 #define symbol_put(x) do { } while(0) 506 #define symbol_put(x) do { } while(0)
507 #define symbol_put_addr(x) do { } while(0) 507 #define symbol_put_addr(x) do { } while(0)
508 508
509 static inline void __module_get(struct module *module) 509 static inline void __module_get(struct module *module)
510 { 510 {
511 } 511 }
512 512
513 static inline int try_module_get(struct module *module) 513 static inline int try_module_get(struct module *module)
514 { 514 {
515 return 1; 515 return 1;
516 } 516 }
517 517
518 static inline void module_put(struct module *module) 518 static inline void module_put(struct module *module)
519 { 519 {
520 } 520 }
521 521
522 #define module_name(mod) "kernel" 522 #define module_name(mod) "kernel"
523 523
524 #define __unsafe(mod) 524 #define __unsafe(mod)
525 525
526 /* For kallsyms to ask for address resolution. NULL means not found. */ 526 /* For kallsyms to ask for address resolution. NULL means not found. */
527 static inline const char *module_address_lookup(unsigned long addr, 527 static inline const char *module_address_lookup(unsigned long addr,
528 unsigned long *symbolsize, 528 unsigned long *symbolsize,
529 unsigned long *offset, 529 unsigned long *offset,
530 char **modname) 530 char **modname)
531 { 531 {
532 return NULL; 532 return NULL;
533 } 533 }
534 534
535 static inline struct module *module_get_kallsym(unsigned int symnum, 535 static inline struct module *module_get_kallsym(unsigned int symnum,
536 unsigned long *value, 536 unsigned long *value,
537 char *type, char *name, 537 char *type, char *name,
538 size_t namelen) 538 size_t namelen)
539 { 539 {
540 return NULL; 540 return NULL;
541 } 541 }
542 542
543 static inline unsigned long module_kallsyms_lookup_name(const char *name) 543 static inline unsigned long module_kallsyms_lookup_name(const char *name)
544 { 544 {
545 return 0; 545 return 0;
546 } 546 }
547 547
548 static inline int is_exported(const char *name, const struct module *mod) 548 static inline int is_exported(const char *name, const struct module *mod)
549 { 549 {
550 return 0; 550 return 0;
551 } 551 }
552 552
553 static inline int register_module_notifier(struct notifier_block * nb) 553 static inline int register_module_notifier(struct notifier_block * nb)
554 { 554 {
555 /* no events will happen anyway, so this can always succeed */ 555 /* no events will happen anyway, so this can always succeed */
556 return 0; 556 return 0;
557 } 557 }
558 558
559 static inline int unregister_module_notifier(struct notifier_block * nb) 559 static inline int unregister_module_notifier(struct notifier_block * nb)
560 { 560 {
561 return 0; 561 return 0;
562 } 562 }
563 563
564 #define module_put_and_exit(code) do_exit(code) 564 #define module_put_and_exit(code) do_exit(code)
565 565
566 static inline void print_modules(void) 566 static inline void print_modules(void)
567 { 567 {
568 } 568 }
569 569
570 struct device_driver; 570 struct device_driver;
571 struct module; 571 struct module;
572 572
573 static inline void module_add_driver(struct module *module, struct device_driver *driver) 573 static inline void module_add_driver(struct module *module, struct device_driver *driver)
574 { 574 {
575 } 575 }
576 576
577 static inline void module_remove_driver(struct device_driver *driver) 577 static inline void module_remove_driver(struct device_driver *driver)
578 { 578 {
579 } 579 }
580 580
581 #endif /* CONFIG_MODULES */ 581 #endif /* CONFIG_MODULES */
582 582
583 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x) 583 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
584 584
585 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */ 585 /* BELOW HERE ALL THESE ARE OBSOLETE AND WILL VANISH */
586 586
587 #define __MODULE_STRING(x) __stringify(x) 587 #define __MODULE_STRING(x) __stringify(x)
588 588
589 #endif /* _LINUX_MODULE_H */ 589 #endif /* _LINUX_MODULE_H */
1 /* 1 /*
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/module.h> 19 #include <linux/module.h>
20 #include <linux/moduleloader.h> 20 #include <linux/moduleloader.h>
21 #include <linux/init.h> 21 #include <linux/init.h>
22 #include <linux/kernel.h> 22 #include <linux/kernel.h>
23 #include <linux/slab.h> 23 #include <linux/slab.h>
24 #include <linux/vmalloc.h> 24 #include <linux/vmalloc.h>
25 #include <linux/elf.h> 25 #include <linux/elf.h>
26 #include <linux/seq_file.h> 26 #include <linux/seq_file.h>
27 #include <linux/syscalls.h> 27 #include <linux/syscalls.h>
28 #include <linux/fcntl.h> 28 #include <linux/fcntl.h>
29 #include <linux/rcupdate.h> 29 #include <linux/rcupdate.h>
30 #include <linux/capability.h> 30 #include <linux/capability.h>
31 #include <linux/cpu.h> 31 #include <linux/cpu.h>
32 #include <linux/moduleparam.h> 32 #include <linux/moduleparam.h>
33 #include <linux/errno.h> 33 #include <linux/errno.h>
34 #include <linux/err.h> 34 #include <linux/err.h>
35 #include <linux/vermagic.h> 35 #include <linux/vermagic.h>
36 #include <linux/notifier.h> 36 #include <linux/notifier.h>
37 #include <linux/sched.h> 37 #include <linux/sched.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/mutex.h> 41 #include <linux/mutex.h>
42 #include <linux/unwind.h> 42 #include <linux/unwind.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 #include <linux/license.h> 46 #include <linux/license.h>
47 47
48 #if 0 48 #if 0
49 #define DEBUGP printk 49 #define DEBUGP printk
50 #else 50 #else
51 #define DEBUGP(fmt , a...) 51 #define DEBUGP(fmt , a...)
52 #endif 52 #endif
53 53
54 #ifndef ARCH_SHF_SMALL 54 #ifndef ARCH_SHF_SMALL
55 #define ARCH_SHF_SMALL 0 55 #define ARCH_SHF_SMALL 0
56 #endif 56 #endif
57 57
58 /* If this is set, the section belongs in the init part of the module */ 58 /* If this is set, the section belongs in the init part of the module */
59 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) 59 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
60 60
61 /* Protects module list */ 61 /* Protects module list */
62 static DEFINE_SPINLOCK(modlist_lock); 62 static DEFINE_SPINLOCK(modlist_lock);
63 63
64 /* List of modules, protected by module_mutex AND modlist_lock */ 64 /* List of modules, protected by module_mutex AND modlist_lock */
65 static DEFINE_MUTEX(module_mutex); 65 static DEFINE_MUTEX(module_mutex);
66 static LIST_HEAD(modules); 66 static LIST_HEAD(modules);
67 67
68 static BLOCKING_NOTIFIER_HEAD(module_notify_list); 68 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
69 69
70 int register_module_notifier(struct notifier_block * nb) 70 int register_module_notifier(struct notifier_block * nb)
71 { 71 {
72 return blocking_notifier_chain_register(&module_notify_list, nb); 72 return blocking_notifier_chain_register(&module_notify_list, nb);
73 } 73 }
74 EXPORT_SYMBOL(register_module_notifier); 74 EXPORT_SYMBOL(register_module_notifier);
75 75
76 int unregister_module_notifier(struct notifier_block * nb) 76 int unregister_module_notifier(struct notifier_block * nb)
77 { 77 {
78 return blocking_notifier_chain_unregister(&module_notify_list, nb); 78 return blocking_notifier_chain_unregister(&module_notify_list, nb);
79 } 79 }
80 EXPORT_SYMBOL(unregister_module_notifier); 80 EXPORT_SYMBOL(unregister_module_notifier);
81 81
82 /* We require a truly strong try_module_get() */ 82 /* We require a truly strong try_module_get() */
83 static inline int strong_try_module_get(struct module *mod) 83 static inline int strong_try_module_get(struct module *mod)
84 { 84 {
85 if (mod && mod->state == MODULE_STATE_COMING) 85 if (mod && mod->state == MODULE_STATE_COMING)
86 return 0; 86 return 0;
87 return try_module_get(mod); 87 return try_module_get(mod);
88 } 88 }
89 89
90 static inline void add_taint_module(struct module *mod, unsigned flag) 90 static inline void add_taint_module(struct module *mod, unsigned flag)
91 { 91 {
92 add_taint(flag); 92 add_taint(flag);
93 mod->taints |= flag; 93 mod->taints |= flag;
94 } 94 }
95 95
96 /* A thread that wants to hold a reference to a module only while it 96 /* A thread that wants to hold a reference to a module only while it
97 * is running can call ths to safely exit. 97 * is running can call ths to safely exit.
98 * nfsd and lockd use this. 98 * nfsd and lockd use this.
99 */ 99 */
100 void __module_put_and_exit(struct module *mod, long code) 100 void __module_put_and_exit(struct module *mod, long code)
101 { 101 {
102 module_put(mod); 102 module_put(mod);
103 do_exit(code); 103 do_exit(code);
104 } 104 }
105 EXPORT_SYMBOL(__module_put_and_exit); 105 EXPORT_SYMBOL(__module_put_and_exit);
106 106
107 /* Find a module section: 0 means not found. */ 107 /* Find a module section: 0 means not found. */
108 static unsigned int find_sec(Elf_Ehdr *hdr, 108 static unsigned int find_sec(Elf_Ehdr *hdr,
109 Elf_Shdr *sechdrs, 109 Elf_Shdr *sechdrs,
110 const char *secstrings, 110 const char *secstrings,
111 const char *name) 111 const char *name)
112 { 112 {
113 unsigned int i; 113 unsigned int i;
114 114
115 for (i = 1; i < hdr->e_shnum; i++) 115 for (i = 1; i < hdr->e_shnum; i++)
116 /* Alloc bit cleared means "ignore it." */ 116 /* Alloc bit cleared means "ignore it." */
117 if ((sechdrs[i].sh_flags & SHF_ALLOC) 117 if ((sechdrs[i].sh_flags & SHF_ALLOC)
118 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0) 118 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
119 return i; 119 return i;
120 return 0; 120 return 0;
121 } 121 }
122 122
123 /* Provided by the linker */ 123 /* Provided by the linker */
124 extern const struct kernel_symbol __start___ksymtab[]; 124 extern const struct kernel_symbol __start___ksymtab[];
125 extern const struct kernel_symbol __stop___ksymtab[]; 125 extern const struct kernel_symbol __stop___ksymtab[];
126 extern const struct kernel_symbol __start___ksymtab_gpl[]; 126 extern const struct kernel_symbol __start___ksymtab_gpl[];
127 extern const struct kernel_symbol __stop___ksymtab_gpl[]; 127 extern const struct kernel_symbol __stop___ksymtab_gpl[];
128 extern const struct kernel_symbol __start___ksymtab_gpl_future[]; 128 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
129 extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; 129 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
130 extern const struct kernel_symbol __start___ksymtab_unused[]; 130 extern const struct kernel_symbol __start___ksymtab_unused[];
131 extern const struct kernel_symbol __stop___ksymtab_unused[]; 131 extern const struct kernel_symbol __stop___ksymtab_unused[];
132 extern const struct kernel_symbol __start___ksymtab_unused_gpl[]; 132 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
133 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[]; 133 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
134 extern const struct kernel_symbol __start___ksymtab_gpl_future[]; 134 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
135 extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; 135 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
136 extern const unsigned long __start___kcrctab[]; 136 extern const unsigned long __start___kcrctab[];
137 extern const unsigned long __start___kcrctab_gpl[]; 137 extern const unsigned long __start___kcrctab_gpl[];
138 extern const unsigned long __start___kcrctab_gpl_future[]; 138 extern const unsigned long __start___kcrctab_gpl_future[];
139 extern const unsigned long __start___kcrctab_unused[]; 139 extern const unsigned long __start___kcrctab_unused[];
140 extern const unsigned long __start___kcrctab_unused_gpl[]; 140 extern const unsigned long __start___kcrctab_unused_gpl[];
141 141
142 #ifndef CONFIG_MODVERSIONS 142 #ifndef CONFIG_MODVERSIONS
143 #define symversion(base, idx) NULL 143 #define symversion(base, idx) NULL
144 #else 144 #else
145 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) 145 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
146 #endif 146 #endif
147 147
148 /* lookup symbol in given range of kernel_symbols */ 148 /* lookup symbol in given range of kernel_symbols */
149 static const struct kernel_symbol *lookup_symbol(const char *name, 149 static const struct kernel_symbol *lookup_symbol(const char *name,
150 const struct kernel_symbol *start, 150 const struct kernel_symbol *start,
151 const struct kernel_symbol *stop) 151 const struct kernel_symbol *stop)
152 { 152 {
153 const struct kernel_symbol *ks = start; 153 const struct kernel_symbol *ks = start;
154 for (; ks < stop; ks++) 154 for (; ks < stop; ks++)
155 if (strcmp(ks->name, name) == 0) 155 if (strcmp(ks->name, name) == 0)
156 return ks; 156 return ks;
157 return NULL; 157 return NULL;
158 } 158 }
159 159
160 static void printk_unused_warning(const char *name) 160 static void printk_unused_warning(const char *name)
161 { 161 {
162 printk(KERN_WARNING "Symbol %s is marked as UNUSED, " 162 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
163 "however this module is using it.\n", name); 163 "however this module is using it.\n", name);
164 printk(KERN_WARNING "This symbol will go away in the future.\n"); 164 printk(KERN_WARNING "This symbol will go away in the future.\n");
165 printk(KERN_WARNING "Please evalute if this is the right api to use, " 165 printk(KERN_WARNING "Please evalute if this is the right api to use, "
166 "and if it really is, submit a report the linux kernel " 166 "and if it really is, submit a report the linux kernel "
167 "mailinglist together with submitting your code for " 167 "mailinglist together with submitting your code for "
168 "inclusion.\n"); 168 "inclusion.\n");
169 } 169 }
170 170
171 /* Find a symbol, return value, crc and module which owns it */ 171 /* Find a symbol, return value, crc and module which owns it */
172 static unsigned long __find_symbol(const char *name, 172 static unsigned long __find_symbol(const char *name,
173 struct module **owner, 173 struct module **owner,
174 const unsigned long **crc, 174 const unsigned long **crc,
175 int gplok) 175 int gplok)
176 { 176 {
177 struct module *mod; 177 struct module *mod;
178 const struct kernel_symbol *ks; 178 const struct kernel_symbol *ks;
179 179
180 /* Core kernel first. */ 180 /* Core kernel first. */
181 *owner = NULL; 181 *owner = NULL;
182 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab); 182 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
183 if (ks) { 183 if (ks) {
184 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab)); 184 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
185 return ks->value; 185 return ks->value;
186 } 186 }
187 if (gplok) { 187 if (gplok) {
188 ks = lookup_symbol(name, __start___ksymtab_gpl, 188 ks = lookup_symbol(name, __start___ksymtab_gpl,
189 __stop___ksymtab_gpl); 189 __stop___ksymtab_gpl);
190 if (ks) { 190 if (ks) {
191 *crc = symversion(__start___kcrctab_gpl, 191 *crc = symversion(__start___kcrctab_gpl,
192 (ks - __start___ksymtab_gpl)); 192 (ks - __start___ksymtab_gpl));
193 return ks->value; 193 return ks->value;
194 } 194 }
195 } 195 }
196 ks = lookup_symbol(name, __start___ksymtab_gpl_future, 196 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
197 __stop___ksymtab_gpl_future); 197 __stop___ksymtab_gpl_future);
198 if (ks) { 198 if (ks) {
199 if (!gplok) { 199 if (!gplok) {
200 printk(KERN_WARNING "Symbol %s is being used " 200 printk(KERN_WARNING "Symbol %s is being used "
201 "by a non-GPL module, which will not " 201 "by a non-GPL module, which will not "
202 "be allowed in the future\n", name); 202 "be allowed in the future\n", name);
203 printk(KERN_WARNING "Please see the file " 203 printk(KERN_WARNING "Please see the file "
204 "Documentation/feature-removal-schedule.txt " 204 "Documentation/feature-removal-schedule.txt "
205 "in the kernel source tree for more " 205 "in the kernel source tree for more "
206 "details.\n"); 206 "details.\n");
207 } 207 }
208 *crc = symversion(__start___kcrctab_gpl_future, 208 *crc = symversion(__start___kcrctab_gpl_future,
209 (ks - __start___ksymtab_gpl_future)); 209 (ks - __start___ksymtab_gpl_future));
210 return ks->value; 210 return ks->value;
211 } 211 }
212 212
213 ks = lookup_symbol(name, __start___ksymtab_unused, 213 ks = lookup_symbol(name, __start___ksymtab_unused,
214 __stop___ksymtab_unused); 214 __stop___ksymtab_unused);
215 if (ks) { 215 if (ks) {
216 printk_unused_warning(name); 216 printk_unused_warning(name);
217 *crc = symversion(__start___kcrctab_unused, 217 *crc = symversion(__start___kcrctab_unused,
218 (ks - __start___ksymtab_unused)); 218 (ks - __start___ksymtab_unused));
219 return ks->value; 219 return ks->value;
220 } 220 }
221 221
222 if (gplok) 222 if (gplok)
223 ks = lookup_symbol(name, __start___ksymtab_unused_gpl, 223 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
224 __stop___ksymtab_unused_gpl); 224 __stop___ksymtab_unused_gpl);
225 if (ks) { 225 if (ks) {
226 printk_unused_warning(name); 226 printk_unused_warning(name);
227 *crc = symversion(__start___kcrctab_unused_gpl, 227 *crc = symversion(__start___kcrctab_unused_gpl,
228 (ks - __start___ksymtab_unused_gpl)); 228 (ks - __start___ksymtab_unused_gpl));
229 return ks->value; 229 return ks->value;
230 } 230 }
231 231
232 /* Now try modules. */ 232 /* Now try modules. */
233 list_for_each_entry(mod, &modules, list) { 233 list_for_each_entry(mod, &modules, list) {
234 *owner = mod; 234 *owner = mod;
235 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms); 235 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
236 if (ks) { 236 if (ks) {
237 *crc = symversion(mod->crcs, (ks - mod->syms)); 237 *crc = symversion(mod->crcs, (ks - mod->syms));
238 return ks->value; 238 return ks->value;
239 } 239 }
240 240
241 if (gplok) { 241 if (gplok) {
242 ks = lookup_symbol(name, mod->gpl_syms, 242 ks = lookup_symbol(name, mod->gpl_syms,
243 mod->gpl_syms + mod->num_gpl_syms); 243 mod->gpl_syms + mod->num_gpl_syms);
244 if (ks) { 244 if (ks) {
245 *crc = symversion(mod->gpl_crcs, 245 *crc = symversion(mod->gpl_crcs,
246 (ks - mod->gpl_syms)); 246 (ks - mod->gpl_syms));
247 return ks->value; 247 return ks->value;
248 } 248 }
249 } 249 }
250 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms); 250 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
251 if (ks) { 251 if (ks) {
252 printk_unused_warning(name); 252 printk_unused_warning(name);
253 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms)); 253 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
254 return ks->value; 254 return ks->value;
255 } 255 }
256 256
257 if (gplok) { 257 if (gplok) {
258 ks = lookup_symbol(name, mod->unused_gpl_syms, 258 ks = lookup_symbol(name, mod->unused_gpl_syms,
259 mod->unused_gpl_syms + mod->num_unused_gpl_syms); 259 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
260 if (ks) { 260 if (ks) {
261 printk_unused_warning(name); 261 printk_unused_warning(name);
262 *crc = symversion(mod->unused_gpl_crcs, 262 *crc = symversion(mod->unused_gpl_crcs,
263 (ks - mod->unused_gpl_syms)); 263 (ks - mod->unused_gpl_syms));
264 return ks->value; 264 return ks->value;
265 } 265 }
266 } 266 }
267 ks = lookup_symbol(name, mod->gpl_future_syms, 267 ks = lookup_symbol(name, mod->gpl_future_syms,
268 (mod->gpl_future_syms + 268 (mod->gpl_future_syms +
269 mod->num_gpl_future_syms)); 269 mod->num_gpl_future_syms));
270 if (ks) { 270 if (ks) {
271 if (!gplok) { 271 if (!gplok) {
272 printk(KERN_WARNING "Symbol %s is being used " 272 printk(KERN_WARNING "Symbol %s is being used "
273 "by a non-GPL module, which will not " 273 "by a non-GPL module, which will not "
274 "be allowed in the future\n", name); 274 "be allowed in the future\n", name);
275 printk(KERN_WARNING "Please see the file " 275 printk(KERN_WARNING "Please see the file "
276 "Documentation/feature-removal-schedule.txt " 276 "Documentation/feature-removal-schedule.txt "
277 "in the kernel source tree for more " 277 "in the kernel source tree for more "
278 "details.\n"); 278 "details.\n");
279 } 279 }
280 *crc = symversion(mod->gpl_future_crcs, 280 *crc = symversion(mod->gpl_future_crcs,
281 (ks - mod->gpl_future_syms)); 281 (ks - mod->gpl_future_syms));
282 return ks->value; 282 return ks->value;
283 } 283 }
284 } 284 }
285 DEBUGP("Failed to find symbol %s\n", name); 285 DEBUGP("Failed to find symbol %s\n", name);
286 return 0; 286 return 0;
287 } 287 }
288 288
289 /* Search for module by name: must hold module_mutex. */ 289 /* Search for module by name: must hold module_mutex. */
290 static struct module *find_module(const char *name) 290 static struct module *find_module(const char *name)
291 { 291 {
292 struct module *mod; 292 struct module *mod;
293 293
294 list_for_each_entry(mod, &modules, list) { 294 list_for_each_entry(mod, &modules, list) {
295 if (strcmp(mod->name, name) == 0) 295 if (strcmp(mod->name, name) == 0)
296 return mod; 296 return mod;
297 } 297 }
298 return NULL; 298 return NULL;
299 } 299 }
300 300
301 #ifdef CONFIG_SMP 301 #ifdef CONFIG_SMP
302 /* Number of blocks used and allocated. */ 302 /* Number of blocks used and allocated. */
303 static unsigned int pcpu_num_used, pcpu_num_allocated; 303 static unsigned int pcpu_num_used, pcpu_num_allocated;
304 /* Size of each block. -ve means used. */ 304 /* Size of each block. -ve means used. */
305 static int *pcpu_size; 305 static int *pcpu_size;
306 306
307 static int split_block(unsigned int i, unsigned short size) 307 static int split_block(unsigned int i, unsigned short size)
308 { 308 {
309 /* Reallocation required? */ 309 /* Reallocation required? */
310 if (pcpu_num_used + 1 > pcpu_num_allocated) { 310 if (pcpu_num_used + 1 > pcpu_num_allocated) {
311 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2, 311 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
312 GFP_KERNEL); 312 GFP_KERNEL);
313 if (!new) 313 if (!new)
314 return 0; 314 return 0;
315 315
316 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated); 316 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
317 pcpu_num_allocated *= 2; 317 pcpu_num_allocated *= 2;
318 kfree(pcpu_size); 318 kfree(pcpu_size);
319 pcpu_size = new; 319 pcpu_size = new;
320 } 320 }
321 321
322 /* Insert a new subblock */ 322 /* Insert a new subblock */
323 memmove(&pcpu_size[i+1], &pcpu_size[i], 323 memmove(&pcpu_size[i+1], &pcpu_size[i],
324 sizeof(pcpu_size[0]) * (pcpu_num_used - i)); 324 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
325 pcpu_num_used++; 325 pcpu_num_used++;
326 326
327 pcpu_size[i+1] -= size; 327 pcpu_size[i+1] -= size;
328 pcpu_size[i] = size; 328 pcpu_size[i] = size;
329 return 1; 329 return 1;
330 } 330 }
331 331
332 static inline unsigned int block_size(int val) 332 static inline unsigned int block_size(int val)
333 { 333 {
334 if (val < 0) 334 if (val < 0)
335 return -val; 335 return -val;
336 return val; 336 return val;
337 } 337 }
338 338
339 /* Created by linker magic */ 339 /* Created by linker magic */
340 extern char __per_cpu_start[], __per_cpu_end[]; 340 extern char __per_cpu_start[], __per_cpu_end[];
341 341
342 static void *percpu_modalloc(unsigned long size, unsigned long align, 342 static void *percpu_modalloc(unsigned long size, unsigned long align,
343 const char *name) 343 const char *name)
344 { 344 {
345 unsigned long extra; 345 unsigned long extra;
346 unsigned int i; 346 unsigned int i;
347 void *ptr; 347 void *ptr;
348 348
349 if (align > SMP_CACHE_BYTES) { 349 if (align > SMP_CACHE_BYTES) {
350 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n", 350 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
351 name, align, SMP_CACHE_BYTES); 351 name, align, SMP_CACHE_BYTES);
352 align = SMP_CACHE_BYTES; 352 align = SMP_CACHE_BYTES;
353 } 353 }
354 354
355 ptr = __per_cpu_start; 355 ptr = __per_cpu_start;
356 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) { 356 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
357 /* Extra for alignment requirement. */ 357 /* Extra for alignment requirement. */
358 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr; 358 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
359 BUG_ON(i == 0 && extra != 0); 359 BUG_ON(i == 0 && extra != 0);
360 360
361 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size) 361 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
362 continue; 362 continue;
363 363
364 /* Transfer extra to previous block. */ 364 /* Transfer extra to previous block. */
365 if (pcpu_size[i-1] < 0) 365 if (pcpu_size[i-1] < 0)
366 pcpu_size[i-1] -= extra; 366 pcpu_size[i-1] -= extra;
367 else 367 else
368 pcpu_size[i-1] += extra; 368 pcpu_size[i-1] += extra;
369 pcpu_size[i] -= extra; 369 pcpu_size[i] -= extra;
370 ptr += extra; 370 ptr += extra;
371 371
372 /* Split block if warranted */ 372 /* Split block if warranted */
373 if (pcpu_size[i] - size > sizeof(unsigned long)) 373 if (pcpu_size[i] - size > sizeof(unsigned long))
374 if (!split_block(i, size)) 374 if (!split_block(i, size))
375 return NULL; 375 return NULL;
376 376
377 /* Mark allocated */ 377 /* Mark allocated */
378 pcpu_size[i] = -pcpu_size[i]; 378 pcpu_size[i] = -pcpu_size[i];
379 return ptr; 379 return ptr;
380 } 380 }
381 381
382 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n", 382 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
383 size); 383 size);
384 return NULL; 384 return NULL;
385 } 385 }
386 386
387 static void percpu_modfree(void *freeme) 387 static void percpu_modfree(void *freeme)
388 { 388 {
389 unsigned int i; 389 unsigned int i;
390 void *ptr = __per_cpu_start + block_size(pcpu_size[0]); 390 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
391 391
392 /* First entry is core kernel percpu data. */ 392 /* First entry is core kernel percpu data. */
393 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) { 393 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
394 if (ptr == freeme) { 394 if (ptr == freeme) {
395 pcpu_size[i] = -pcpu_size[i]; 395 pcpu_size[i] = -pcpu_size[i];
396 goto free; 396 goto free;
397 } 397 }
398 } 398 }
399 BUG(); 399 BUG();
400 400
401 free: 401 free:
402 /* Merge with previous? */ 402 /* Merge with previous? */
403 if (pcpu_size[i-1] >= 0) { 403 if (pcpu_size[i-1] >= 0) {
404 pcpu_size[i-1] += pcpu_size[i]; 404 pcpu_size[i-1] += pcpu_size[i];
405 pcpu_num_used--; 405 pcpu_num_used--;
406 memmove(&pcpu_size[i], &pcpu_size[i+1], 406 memmove(&pcpu_size[i], &pcpu_size[i+1],
407 (pcpu_num_used - i) * sizeof(pcpu_size[0])); 407 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
408 i--; 408 i--;
409 } 409 }
410 /* Merge with next? */ 410 /* Merge with next? */
411 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) { 411 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
412 pcpu_size[i] += pcpu_size[i+1]; 412 pcpu_size[i] += pcpu_size[i+1];
413 pcpu_num_used--; 413 pcpu_num_used--;
414 memmove(&pcpu_size[i+1], &pcpu_size[i+2], 414 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
415 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0])); 415 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
416 } 416 }
417 } 417 }
418 418
419 static unsigned int find_pcpusec(Elf_Ehdr *hdr, 419 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
420 Elf_Shdr *sechdrs, 420 Elf_Shdr *sechdrs,
421 const char *secstrings) 421 const char *secstrings)
422 { 422 {
423 return find_sec(hdr, sechdrs, secstrings, ".data.percpu"); 423 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
424 } 424 }
425 425
426 static int percpu_modinit(void) 426 static int percpu_modinit(void)
427 { 427 {
428 pcpu_num_used = 2; 428 pcpu_num_used = 2;
429 pcpu_num_allocated = 2; 429 pcpu_num_allocated = 2;
430 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated, 430 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
431 GFP_KERNEL); 431 GFP_KERNEL);
432 /* Static in-kernel percpu data (used). */ 432 /* Static in-kernel percpu data (used). */
433 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES); 433 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
434 /* Free room. */ 434 /* Free room. */
435 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0]; 435 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
436 if (pcpu_size[1] < 0) { 436 if (pcpu_size[1] < 0) {
437 printk(KERN_ERR "No per-cpu room for modules.\n"); 437 printk(KERN_ERR "No per-cpu room for modules.\n");
438 pcpu_num_used = 1; 438 pcpu_num_used = 1;
439 } 439 }
440 440
441 return 0; 441 return 0;
442 } 442 }
443 __initcall(percpu_modinit); 443 __initcall(percpu_modinit);
444 #else /* ... !CONFIG_SMP */ 444 #else /* ... !CONFIG_SMP */
445 static inline void *percpu_modalloc(unsigned long size, unsigned long align, 445 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
446 const char *name) 446 const char *name)
447 { 447 {
448 return NULL; 448 return NULL;
449 } 449 }
450 static inline void percpu_modfree(void *pcpuptr) 450 static inline void percpu_modfree(void *pcpuptr)
451 { 451 {
452 BUG(); 452 BUG();
453 } 453 }
454 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr, 454 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
455 Elf_Shdr *sechdrs, 455 Elf_Shdr *sechdrs,
456 const char *secstrings) 456 const char *secstrings)
457 { 457 {
458 return 0; 458 return 0;
459 } 459 }
460 static inline void percpu_modcopy(void *pcpudst, const void *src, 460 static inline void percpu_modcopy(void *pcpudst, const void *src,
461 unsigned long size) 461 unsigned long size)
462 { 462 {
463 /* pcpusec should be 0, and size of that section should be 0. */ 463 /* pcpusec should be 0, and size of that section should be 0. */
464 BUG_ON(size != 0); 464 BUG_ON(size != 0);
465 } 465 }
466 #endif /* CONFIG_SMP */ 466 #endif /* CONFIG_SMP */
467 467
468 #define MODINFO_ATTR(field) \ 468 #define MODINFO_ATTR(field) \
469 static void setup_modinfo_##field(struct module *mod, const char *s) \ 469 static void setup_modinfo_##field(struct module *mod, const char *s) \
470 { \ 470 { \
471 mod->field = kstrdup(s, GFP_KERNEL); \ 471 mod->field = kstrdup(s, GFP_KERNEL); \
472 } \ 472 } \
473 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ 473 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
474 struct module *mod, char *buffer) \ 474 struct module *mod, char *buffer) \
475 { \ 475 { \
476 return sprintf(buffer, "%s\n", mod->field); \ 476 return sprintf(buffer, "%s\n", mod->field); \
477 } \ 477 } \
478 static int modinfo_##field##_exists(struct module *mod) \ 478 static int modinfo_##field##_exists(struct module *mod) \
479 { \ 479 { \
480 return mod->field != NULL; \ 480 return mod->field != NULL; \
481 } \ 481 } \
482 static void free_modinfo_##field(struct module *mod) \ 482 static void free_modinfo_##field(struct module *mod) \
483 { \ 483 { \
484 kfree(mod->field); \ 484 kfree(mod->field); \
485 mod->field = NULL; \ 485 mod->field = NULL; \
486 } \ 486 } \
487 static struct module_attribute modinfo_##field = { \ 487 static struct module_attribute modinfo_##field = { \
488 .attr = { .name = __stringify(field), .mode = 0444, \ 488 .attr = { .name = __stringify(field), .mode = 0444, \
489 .owner = THIS_MODULE }, \ 489 .owner = THIS_MODULE }, \
490 .show = show_modinfo_##field, \ 490 .show = show_modinfo_##field, \
491 .setup = setup_modinfo_##field, \ 491 .setup = setup_modinfo_##field, \
492 .test = modinfo_##field##_exists, \ 492 .test = modinfo_##field##_exists, \
493 .free = free_modinfo_##field, \ 493 .free = free_modinfo_##field, \
494 }; 494 };
495 495
496 MODINFO_ATTR(version); 496 MODINFO_ATTR(version);
497 MODINFO_ATTR(srcversion); 497 MODINFO_ATTR(srcversion);
498 498
499 #ifdef CONFIG_MODULE_UNLOAD 499 #ifdef CONFIG_MODULE_UNLOAD
500 /* Init the unload section of the module. */ 500 /* Init the unload section of the module. */
501 static void module_unload_init(struct module *mod) 501 static void module_unload_init(struct module *mod)
502 { 502 {
503 unsigned int i; 503 unsigned int i;
504 504
505 INIT_LIST_HEAD(&mod->modules_which_use_me); 505 INIT_LIST_HEAD(&mod->modules_which_use_me);
506 for (i = 0; i < NR_CPUS; i++) 506 for (i = 0; i < NR_CPUS; i++)
507 local_set(&mod->ref[i].count, 0); 507 local_set(&mod->ref[i].count, 0);
508 /* Hold reference count during initialization. */ 508 /* Hold reference count during initialization. */
509 local_set(&mod->ref[raw_smp_processor_id()].count, 1); 509 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
510 /* Backwards compatibility macros put refcount during init. */ 510 /* Backwards compatibility macros put refcount during init. */
511 mod->waiter = current; 511 mod->waiter = current;
512 } 512 }
513 513
514 /* modules using other modules */ 514 /* modules using other modules */
515 struct module_use 515 struct module_use
516 { 516 {
517 struct list_head list; 517 struct list_head list;
518 struct module *module_which_uses; 518 struct module *module_which_uses;
519 }; 519 };
520 520
521 /* Does a already use b? */ 521 /* Does a already use b? */
522 static int already_uses(struct module *a, struct module *b) 522 static int already_uses(struct module *a, struct module *b)
523 { 523 {
524 struct module_use *use; 524 struct module_use *use;
525 525
526 list_for_each_entry(use, &b->modules_which_use_me, list) { 526 list_for_each_entry(use, &b->modules_which_use_me, list) {
527 if (use->module_which_uses == a) { 527 if (use->module_which_uses == a) {
528 DEBUGP("%s uses %s!\n", a->name, b->name); 528 DEBUGP("%s uses %s!\n", a->name, b->name);
529 return 1; 529 return 1;
530 } 530 }
531 } 531 }
532 DEBUGP("%s does not use %s!\n", a->name, b->name); 532 DEBUGP("%s does not use %s!\n", a->name, b->name);
533 return 0; 533 return 0;
534 } 534 }
535 535
536 /* Module a uses b */ 536 /* Module a uses b */
537 static int use_module(struct module *a, struct module *b) 537 static int use_module(struct module *a, struct module *b)
538 { 538 {
539 struct module_use *use; 539 struct module_use *use;
540 if (b == NULL || already_uses(a, b)) return 1; 540 if (b == NULL || already_uses(a, b)) return 1;
541 541
542 if (!strong_try_module_get(b)) 542 if (!strong_try_module_get(b))
543 return 0; 543 return 0;
544 544
545 DEBUGP("Allocating new usage for %s.\n", a->name); 545 DEBUGP("Allocating new usage for %s.\n", a->name);
546 use = kmalloc(sizeof(*use), GFP_ATOMIC); 546 use = kmalloc(sizeof(*use), GFP_ATOMIC);
547 if (!use) { 547 if (!use) {
548 printk("%s: out of memory loading\n", a->name); 548 printk("%s: out of memory loading\n", a->name);
549 module_put(b); 549 module_put(b);
550 return 0; 550 return 0;
551 } 551 }
552 552
553 use->module_which_uses = a; 553 use->module_which_uses = a;
554 list_add(&use->list, &b->modules_which_use_me); 554 list_add(&use->list, &b->modules_which_use_me);
555 return 1; 555 return 1;
556 } 556 }
557 557
558 /* Clear the unload stuff of the module. */ 558 /* Clear the unload stuff of the module. */
559 static void module_unload_free(struct module *mod) 559 static void module_unload_free(struct module *mod)
560 { 560 {
561 struct module *i; 561 struct module *i;
562 562
563 list_for_each_entry(i, &modules, list) { 563 list_for_each_entry(i, &modules, list) {
564 struct module_use *use; 564 struct module_use *use;
565 565
566 list_for_each_entry(use, &i->modules_which_use_me, list) { 566 list_for_each_entry(use, &i->modules_which_use_me, list) {
567 if (use->module_which_uses == mod) { 567 if (use->module_which_uses == mod) {
568 DEBUGP("%s unusing %s\n", mod->name, i->name); 568 DEBUGP("%s unusing %s\n", mod->name, i->name);
569 module_put(i); 569 module_put(i);
570 list_del(&use->list); 570 list_del(&use->list);
571 kfree(use); 571 kfree(use);
572 /* There can be at most one match. */ 572 /* There can be at most one match. */
573 break; 573 break;
574 } 574 }
575 } 575 }
576 } 576 }
577 } 577 }
578 578
579 #ifdef CONFIG_MODULE_FORCE_UNLOAD 579 #ifdef CONFIG_MODULE_FORCE_UNLOAD
580 static inline int try_force_unload(unsigned int flags) 580 static inline int try_force_unload(unsigned int flags)
581 { 581 {
582 int ret = (flags & O_TRUNC); 582 int ret = (flags & O_TRUNC);
583 if (ret) 583 if (ret)
584 add_taint(TAINT_FORCED_RMMOD); 584 add_taint(TAINT_FORCED_RMMOD);
585 return ret; 585 return ret;
586 } 586 }
587 #else 587 #else
588 static inline int try_force_unload(unsigned int flags) 588 static inline int try_force_unload(unsigned int flags)
589 { 589 {
590 return 0; 590 return 0;
591 } 591 }
592 #endif /* CONFIG_MODULE_FORCE_UNLOAD */ 592 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
593 593
594 struct stopref 594 struct stopref
595 { 595 {
596 struct module *mod; 596 struct module *mod;
597 int flags; 597 int flags;
598 int *forced; 598 int *forced;
599 }; 599 };
600 600
601 /* Whole machine is stopped with interrupts off when this runs. */ 601 /* Whole machine is stopped with interrupts off when this runs. */
602 static int __try_stop_module(void *_sref) 602 static int __try_stop_module(void *_sref)
603 { 603 {
604 struct stopref *sref = _sref; 604 struct stopref *sref = _sref;
605 605
606 /* If it's not unused, quit unless we are told to block. */ 606 /* If it's not unused, quit unless we are told to block. */
607 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) { 607 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
608 if (!(*sref->forced = try_force_unload(sref->flags))) 608 if (!(*sref->forced = try_force_unload(sref->flags)))
609 return -EWOULDBLOCK; 609 return -EWOULDBLOCK;
610 } 610 }
611 611
612 /* Mark it as dying. */ 612 /* Mark it as dying. */
613 sref->mod->state = MODULE_STATE_GOING; 613 sref->mod->state = MODULE_STATE_GOING;
614 return 0; 614 return 0;
615 } 615 }
616 616
617 static int try_stop_module(struct module *mod, int flags, int *forced) 617 static int try_stop_module(struct module *mod, int flags, int *forced)
618 { 618 {
619 struct stopref sref = { mod, flags, forced }; 619 struct stopref sref = { mod, flags, forced };
620 620
621 return stop_machine_run(__try_stop_module, &sref, NR_CPUS); 621 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
622 } 622 }
623 623
624 unsigned int module_refcount(struct module *mod) 624 unsigned int module_refcount(struct module *mod)
625 { 625 {
626 unsigned int i, total = 0; 626 unsigned int i, total = 0;
627 627
628 for (i = 0; i < NR_CPUS; i++) 628 for (i = 0; i < NR_CPUS; i++)
629 total += local_read(&mod->ref[i].count); 629 total += local_read(&mod->ref[i].count);
630 return total; 630 return total;
631 } 631 }
632 EXPORT_SYMBOL(module_refcount); 632 EXPORT_SYMBOL(module_refcount);
633 633
634 /* This exists whether we can unload or not */ 634 /* This exists whether we can unload or not */
635 static void free_module(struct module *mod); 635 static void free_module(struct module *mod);
636 636
637 static void wait_for_zero_refcount(struct module *mod) 637 static void wait_for_zero_refcount(struct module *mod)
638 { 638 {
639 /* Since we might sleep for some time, drop the semaphore first */ 639 /* Since we might sleep for some time, drop the semaphore first */
640 mutex_unlock(&module_mutex); 640 mutex_unlock(&module_mutex);
641 for (;;) { 641 for (;;) {
642 DEBUGP("Looking at refcount...\n"); 642 DEBUGP("Looking at refcount...\n");
643 set_current_state(TASK_UNINTERRUPTIBLE); 643 set_current_state(TASK_UNINTERRUPTIBLE);
644 if (module_refcount(mod) == 0) 644 if (module_refcount(mod) == 0)
645 break; 645 break;
646 schedule(); 646 schedule();
647 } 647 }
648 current->state = TASK_RUNNING; 648 current->state = TASK_RUNNING;
649 mutex_lock(&module_mutex); 649 mutex_lock(&module_mutex);
650 } 650 }
651 651
652 asmlinkage long 652 asmlinkage long
653 sys_delete_module(const char __user *name_user, unsigned int flags) 653 sys_delete_module(const char __user *name_user, unsigned int flags)
654 { 654 {
655 struct module *mod; 655 struct module *mod;
656 char name[MODULE_NAME_LEN]; 656 char name[MODULE_NAME_LEN];
657 int ret, forced = 0; 657 int ret, forced = 0;
658 658
659 if (!capable(CAP_SYS_MODULE)) 659 if (!capable(CAP_SYS_MODULE))
660 return -EPERM; 660 return -EPERM;
661 661
662 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0) 662 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
663 return -EFAULT; 663 return -EFAULT;
664 name[MODULE_NAME_LEN-1] = '\0'; 664 name[MODULE_NAME_LEN-1] = '\0';
665 665
666 if (mutex_lock_interruptible(&module_mutex) != 0) 666 if (mutex_lock_interruptible(&module_mutex) != 0)
667 return -EINTR; 667 return -EINTR;
668 668
669 mod = find_module(name); 669 mod = find_module(name);
670 if (!mod) { 670 if (!mod) {
671 ret = -ENOENT; 671 ret = -ENOENT;
672 goto out; 672 goto out;
673 } 673 }
674 674
675 if (!list_empty(&mod->modules_which_use_me)) { 675 if (!list_empty(&mod->modules_which_use_me)) {
676 /* Other modules depend on us: get rid of them first. */ 676 /* Other modules depend on us: get rid of them first. */
677 ret = -EWOULDBLOCK; 677 ret = -EWOULDBLOCK;
678 goto out; 678 goto out;
679 } 679 }
680 680
681 /* Doing init or already dying? */ 681 /* Doing init or already dying? */
682 if (mod->state != MODULE_STATE_LIVE) { 682 if (mod->state != MODULE_STATE_LIVE) {
683 /* FIXME: if (force), slam module count and wake up 683 /* FIXME: if (force), slam module count and wake up
684 waiter --RR */ 684 waiter --RR */
685 DEBUGP("%s already dying\n", mod->name); 685 DEBUGP("%s already dying\n", mod->name);
686 ret = -EBUSY; 686 ret = -EBUSY;
687 goto out; 687 goto out;
688 } 688 }
689 689
690 /* If it has an init func, it must have an exit func to unload */ 690 /* If it has an init func, it must have an exit func to unload */
691 if ((mod->init != NULL && mod->exit == NULL) 691 if ((mod->init != NULL && mod->exit == NULL)
692 || mod->unsafe) { 692 || mod->unsafe) {
693 forced = try_force_unload(flags); 693 forced = try_force_unload(flags);
694 if (!forced) { 694 if (!forced) {
695 /* This module can't be removed */ 695 /* This module can't be removed */
696 ret = -EBUSY; 696 ret = -EBUSY;
697 goto out; 697 goto out;
698 } 698 }
699 } 699 }
700 700
701 /* Set this up before setting mod->state */ 701 /* Set this up before setting mod->state */
702 mod->waiter = current; 702 mod->waiter = current;
703 703
704 /* Stop the machine so refcounts can't move and disable module. */ 704 /* Stop the machine so refcounts can't move and disable module. */
705 ret = try_stop_module(mod, flags, &forced); 705 ret = try_stop_module(mod, flags, &forced);
706 if (ret != 0) 706 if (ret != 0)
707 goto out; 707 goto out;
708 708
709 /* Never wait if forced. */ 709 /* Never wait if forced. */
710 if (!forced && module_refcount(mod) != 0) 710 if (!forced && module_refcount(mod) != 0)
711 wait_for_zero_refcount(mod); 711 wait_for_zero_refcount(mod);
712 712
713 /* Final destruction now noone is using it. */ 713 /* Final destruction now noone is using it. */
714 if (mod->exit != NULL) { 714 if (mod->exit != NULL) {
715 mutex_unlock(&module_mutex); 715 mutex_unlock(&module_mutex);
716 mod->exit(); 716 mod->exit();
717 mutex_lock(&module_mutex); 717 mutex_lock(&module_mutex);
718 } 718 }
719 free_module(mod); 719 free_module(mod);
720 720
721 out: 721 out:
722 mutex_unlock(&module_mutex); 722 mutex_unlock(&module_mutex);
723 return ret; 723 return ret;
724 } 724 }
725 725
726 static void print_unload_info(struct seq_file *m, struct module *mod) 726 static void print_unload_info(struct seq_file *m, struct module *mod)
727 { 727 {
728 struct module_use *use; 728 struct module_use *use;
729 int printed_something = 0; 729 int printed_something = 0;
730 730
731 seq_printf(m, " %u ", module_refcount(mod)); 731 seq_printf(m, " %u ", module_refcount(mod));
732 732
733 /* Always include a trailing , so userspace can differentiate 733 /* Always include a trailing , so userspace can differentiate
734 between this and the old multi-field proc format. */ 734 between this and the old multi-field proc format. */
735 list_for_each_entry(use, &mod->modules_which_use_me, list) { 735 list_for_each_entry(use, &mod->modules_which_use_me, list) {
736 printed_something = 1; 736 printed_something = 1;
737 seq_printf(m, "%s,", use->module_which_uses->name); 737 seq_printf(m, "%s,", use->module_which_uses->name);
738 } 738 }
739 739
740 if (mod->unsafe) { 740 if (mod->unsafe) {
741 printed_something = 1; 741 printed_something = 1;
742 seq_printf(m, "[unsafe],"); 742 seq_printf(m, "[unsafe],");
743 } 743 }
744 744
745 if (mod->init != NULL && mod->exit == NULL) { 745 if (mod->init != NULL && mod->exit == NULL) {
746 printed_something = 1; 746 printed_something = 1;
747 seq_printf(m, "[permanent],"); 747 seq_printf(m, "[permanent],");
748 } 748 }
749 749
750 if (!printed_something) 750 if (!printed_something)
751 seq_printf(m, "-"); 751 seq_printf(m, "-");
752 } 752 }
753 753
754 void __symbol_put(const char *symbol) 754 void __symbol_put(const char *symbol)
755 { 755 {
756 struct module *owner; 756 struct module *owner;
757 unsigned long flags; 757 unsigned long flags;
758 const unsigned long *crc; 758 const unsigned long *crc;
759 759
760 spin_lock_irqsave(&modlist_lock, flags); 760 spin_lock_irqsave(&modlist_lock, flags);
761 if (!__find_symbol(symbol, &owner, &crc, 1)) 761 if (!__find_symbol(symbol, &owner, &crc, 1))
762 BUG(); 762 BUG();
763 module_put(owner); 763 module_put(owner);
764 spin_unlock_irqrestore(&modlist_lock, flags); 764 spin_unlock_irqrestore(&modlist_lock, flags);
765 } 765 }
766 EXPORT_SYMBOL(__symbol_put); 766 EXPORT_SYMBOL(__symbol_put);
767 767
768 void symbol_put_addr(void *addr) 768 void symbol_put_addr(void *addr)
769 { 769 {
770 struct module *modaddr; 770 struct module *modaddr;
771 771
772 if (core_kernel_text((unsigned long)addr)) 772 if (core_kernel_text((unsigned long)addr))
773 return; 773 return;
774 774
775 if (!(modaddr = module_text_address((unsigned long)addr))) 775 if (!(modaddr = module_text_address((unsigned long)addr)))
776 BUG(); 776 BUG();
777 module_put(modaddr); 777 module_put(modaddr);
778 } 778 }
779 EXPORT_SYMBOL_GPL(symbol_put_addr); 779 EXPORT_SYMBOL_GPL(symbol_put_addr);
780 780
781 static ssize_t show_refcnt(struct module_attribute *mattr, 781 static ssize_t show_refcnt(struct module_attribute *mattr,
782 struct module *mod, char *buffer) 782 struct module *mod, char *buffer)
783 { 783 {
784 /* sysfs holds a reference */ 784 /* sysfs holds a reference */
785 return sprintf(buffer, "%u\n", module_refcount(mod)-1); 785 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
786 } 786 }
787 787
788 static struct module_attribute refcnt = { 788 static struct module_attribute refcnt = {
789 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE }, 789 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
790 .show = show_refcnt, 790 .show = show_refcnt,
791 }; 791 };
792 792
793 void module_put(struct module *module) 793 void module_put(struct module *module)
794 { 794 {
795 if (module) { 795 if (module) {
796 unsigned int cpu = get_cpu(); 796 unsigned int cpu = get_cpu();
797 local_dec(&module->ref[cpu].count); 797 local_dec(&module->ref[cpu].count);
798 /* Maybe they're waiting for us to drop reference? */ 798 /* Maybe they're waiting for us to drop reference? */
799 if (unlikely(!module_is_live(module))) 799 if (unlikely(!module_is_live(module)))
800 wake_up_process(module->waiter); 800 wake_up_process(module->waiter);
801 put_cpu(); 801 put_cpu();
802 } 802 }
803 } 803 }
804 EXPORT_SYMBOL(module_put); 804 EXPORT_SYMBOL(module_put);
805 805
806 #else /* !CONFIG_MODULE_UNLOAD */ 806 #else /* !CONFIG_MODULE_UNLOAD */
807 static void print_unload_info(struct seq_file *m, struct module *mod) 807 static void print_unload_info(struct seq_file *m, struct module *mod)
808 { 808 {
809 /* We don't know the usage count, or what modules are using. */ 809 /* We don't know the usage count, or what modules are using. */
810 seq_printf(m, " - -"); 810 seq_printf(m, " - -");
811 } 811 }
812 812
813 static inline void module_unload_free(struct module *mod) 813 static inline void module_unload_free(struct module *mod)
814 { 814 {
815 } 815 }
816 816
817 static inline int use_module(struct module *a, struct module *b) 817 static inline int use_module(struct module *a, struct module *b)
818 { 818 {
819 return strong_try_module_get(b); 819 return strong_try_module_get(b);
820 } 820 }
821 821
822 static inline void module_unload_init(struct module *mod) 822 static inline void module_unload_init(struct module *mod)
823 { 823 {
824 } 824 }
825 #endif /* CONFIG_MODULE_UNLOAD */ 825 #endif /* CONFIG_MODULE_UNLOAD */
826 826
827 static ssize_t show_initstate(struct module_attribute *mattr, 827 static ssize_t show_initstate(struct module_attribute *mattr,
828 struct module *mod, char *buffer) 828 struct module *mod, char *buffer)
829 { 829 {
830 const char *state = "unknown"; 830 const char *state = "unknown";
831 831
832 switch (mod->state) { 832 switch (mod->state) {
833 case MODULE_STATE_LIVE: 833 case MODULE_STATE_LIVE:
834 state = "live"; 834 state = "live";
835 break; 835 break;
836 case MODULE_STATE_COMING: 836 case MODULE_STATE_COMING:
837 state = "coming"; 837 state = "coming";
838 break; 838 break;
839 case MODULE_STATE_GOING: 839 case MODULE_STATE_GOING:
840 state = "going"; 840 state = "going";
841 break; 841 break;
842 } 842 }
843 return sprintf(buffer, "%s\n", state); 843 return sprintf(buffer, "%s\n", state);
844 } 844 }
845 845
846 static struct module_attribute initstate = { 846 static struct module_attribute initstate = {
847 .attr = { .name = "initstate", .mode = 0444, .owner = THIS_MODULE }, 847 .attr = { .name = "initstate", .mode = 0444, .owner = THIS_MODULE },
848 .show = show_initstate, 848 .show = show_initstate,
849 }; 849 };
850 850
851 static struct module_attribute *modinfo_attrs[] = { 851 static struct module_attribute *modinfo_attrs[] = {
852 &modinfo_version, 852 &modinfo_version,
853 &modinfo_srcversion, 853 &modinfo_srcversion,
854 &initstate, 854 &initstate,
855 #ifdef CONFIG_MODULE_UNLOAD 855 #ifdef CONFIG_MODULE_UNLOAD
856 &refcnt, 856 &refcnt,
857 #endif 857 #endif
858 NULL, 858 NULL,
859 }; 859 };
860 860
861 static const char vermagic[] = VERMAGIC_STRING; 861 static const char vermagic[] = VERMAGIC_STRING;
862 862
863 #ifdef CONFIG_MODVERSIONS 863 #ifdef CONFIG_MODVERSIONS
864 static int check_version(Elf_Shdr *sechdrs, 864 static int check_version(Elf_Shdr *sechdrs,
865 unsigned int versindex, 865 unsigned int versindex,
866 const char *symname, 866 const char *symname,
867 struct module *mod, 867 struct module *mod,
868 const unsigned long *crc) 868 const unsigned long *crc)
869 { 869 {
870 unsigned int i, num_versions; 870 unsigned int i, num_versions;
871 struct modversion_info *versions; 871 struct modversion_info *versions;
872 872
873 /* Exporting module didn't supply crcs? OK, we're already tainted. */ 873 /* Exporting module didn't supply crcs? OK, we're already tainted. */
874 if (!crc) 874 if (!crc)
875 return 1; 875 return 1;
876 876
877 versions = (void *) sechdrs[versindex].sh_addr; 877 versions = (void *) sechdrs[versindex].sh_addr;
878 num_versions = sechdrs[versindex].sh_size 878 num_versions = sechdrs[versindex].sh_size
879 / sizeof(struct modversion_info); 879 / sizeof(struct modversion_info);
880 880
881 for (i = 0; i < num_versions; i++) { 881 for (i = 0; i < num_versions; i++) {
882 if (strcmp(versions[i].name, symname) != 0) 882 if (strcmp(versions[i].name, symname) != 0)
883 continue; 883 continue;
884 884
885 if (versions[i].crc == *crc) 885 if (versions[i].crc == *crc)
886 return 1; 886 return 1;
887 printk("%s: disagrees about version of symbol %s\n", 887 printk("%s: disagrees about version of symbol %s\n",
888 mod->name, symname); 888 mod->name, symname);
889 DEBUGP("Found checksum %lX vs module %lX\n", 889 DEBUGP("Found checksum %lX vs module %lX\n",
890 *crc, versions[i].crc); 890 *crc, versions[i].crc);
891 return 0; 891 return 0;
892 } 892 }
893 /* Not in module's version table. OK, but that taints the kernel. */ 893 /* Not in module's version table. OK, but that taints the kernel. */
894 if (!(tainted & TAINT_FORCED_MODULE)) 894 if (!(tainted & TAINT_FORCED_MODULE))
895 printk("%s: no version for \"%s\" found: kernel tainted.\n", 895 printk("%s: no version for \"%s\" found: kernel tainted.\n",
896 mod->name, symname); 896 mod->name, symname);
897 add_taint_module(mod, TAINT_FORCED_MODULE); 897 add_taint_module(mod, TAINT_FORCED_MODULE);
898 return 1; 898 return 1;
899 } 899 }
900 900
901 static inline int check_modstruct_version(Elf_Shdr *sechdrs, 901 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
902 unsigned int versindex, 902 unsigned int versindex,
903 struct module *mod) 903 struct module *mod)
904 { 904 {
905 const unsigned long *crc; 905 const unsigned long *crc;
906 struct module *owner; 906 struct module *owner;
907 907
908 if (!__find_symbol("struct_module", &owner, &crc, 1)) 908 if (!__find_symbol("struct_module", &owner, &crc, 1))
909 BUG(); 909 BUG();
910 return check_version(sechdrs, versindex, "struct_module", mod, 910 return check_version(sechdrs, versindex, "struct_module", mod,
911 crc); 911 crc);
912 } 912 }
913 913
914 /* First part is kernel version, which we ignore. */ 914 /* First part is kernel version, which we ignore. */
915 static inline int same_magic(const char *amagic, const char *bmagic) 915 static inline int same_magic(const char *amagic, const char *bmagic)
916 { 916 {
917 amagic += strcspn(amagic, " "); 917 amagic += strcspn(amagic, " ");
918 bmagic += strcspn(bmagic, " "); 918 bmagic += strcspn(bmagic, " ");
919 return strcmp(amagic, bmagic) == 0; 919 return strcmp(amagic, bmagic) == 0;
920 } 920 }
921 #else 921 #else
922 static inline int check_version(Elf_Shdr *sechdrs, 922 static inline int check_version(Elf_Shdr *sechdrs,
923 unsigned int versindex, 923 unsigned int versindex,
924 const char *symname, 924 const char *symname,
925 struct module *mod, 925 struct module *mod,
926 const unsigned long *crc) 926 const unsigned long *crc)
927 { 927 {
928 return 1; 928 return 1;
929 } 929 }
930 930
931 static inline int check_modstruct_version(Elf_Shdr *sechdrs, 931 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
932 unsigned int versindex, 932 unsigned int versindex,
933 struct module *mod) 933 struct module *mod)
934 { 934 {
935 return 1; 935 return 1;
936 } 936 }
937 937
938 static inline int same_magic(const char *amagic, const char *bmagic) 938 static inline int same_magic(const char *amagic, const char *bmagic)
939 { 939 {
940 return strcmp(amagic, bmagic) == 0; 940 return strcmp(amagic, bmagic) == 0;
941 } 941 }
942 #endif /* CONFIG_MODVERSIONS */ 942 #endif /* CONFIG_MODVERSIONS */
943 943
944 /* Resolve a symbol for this module. I.e. if we find one, record usage. 944 /* Resolve a symbol for this module. I.e. if we find one, record usage.
945 Must be holding module_mutex. */ 945 Must be holding module_mutex. */
946 static unsigned long resolve_symbol(Elf_Shdr *sechdrs, 946 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
947 unsigned int versindex, 947 unsigned int versindex,
948 const char *name, 948 const char *name,
949 struct module *mod) 949 struct module *mod)
950 { 950 {
951 struct module *owner; 951 struct module *owner;
952 unsigned long ret; 952 unsigned long ret;
953 const unsigned long *crc; 953 const unsigned long *crc;
954 954
955 ret = __find_symbol(name, &owner, &crc, 955 ret = __find_symbol(name, &owner, &crc,
956 !(mod->taints & TAINT_PROPRIETARY_MODULE)); 956 !(mod->taints & TAINT_PROPRIETARY_MODULE));
957 if (ret) { 957 if (ret) {
958 /* use_module can fail due to OOM, or module unloading */ 958 /* use_module can fail due to OOM, or module unloading */
959 if (!check_version(sechdrs, versindex, name, mod, crc) || 959 if (!check_version(sechdrs, versindex, name, mod, crc) ||
960 !use_module(mod, owner)) 960 !use_module(mod, owner))
961 ret = 0; 961 ret = 0;
962 } 962 }
963 return ret; 963 return ret;
964 } 964 }
965 965
966 966
967 /* 967 /*
968 * /sys/module/foo/sections stuff 968 * /sys/module/foo/sections stuff
969 * J. Corbet <corbet@lwn.net> 969 * J. Corbet <corbet@lwn.net>
970 */ 970 */
971 #ifdef CONFIG_KALLSYMS 971 #ifdef CONFIG_KALLSYMS
972 static ssize_t module_sect_show(struct module_attribute *mattr, 972 static ssize_t module_sect_show(struct module_attribute *mattr,
973 struct module *mod, char *buf) 973 struct module *mod, char *buf)
974 { 974 {
975 struct module_sect_attr *sattr = 975 struct module_sect_attr *sattr =
976 container_of(mattr, struct module_sect_attr, mattr); 976 container_of(mattr, struct module_sect_attr, mattr);
977 return sprintf(buf, "0x%lx\n", sattr->address); 977 return sprintf(buf, "0x%lx\n", sattr->address);
978 } 978 }
979 979
980 static void free_sect_attrs(struct module_sect_attrs *sect_attrs) 980 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
981 { 981 {
982 int section; 982 int section;
983 983
984 for (section = 0; section < sect_attrs->nsections; section++) 984 for (section = 0; section < sect_attrs->nsections; section++)
985 kfree(sect_attrs->attrs[section].name); 985 kfree(sect_attrs->attrs[section].name);
986 kfree(sect_attrs); 986 kfree(sect_attrs);
987 } 987 }
988 988
989 static void add_sect_attrs(struct module *mod, unsigned int nsect, 989 static void add_sect_attrs(struct module *mod, unsigned int nsect,
990 char *secstrings, Elf_Shdr *sechdrs) 990 char *secstrings, Elf_Shdr *sechdrs)
991 { 991 {
992 unsigned int nloaded = 0, i, size[2]; 992 unsigned int nloaded = 0, i, size[2];
993 struct module_sect_attrs *sect_attrs; 993 struct module_sect_attrs *sect_attrs;
994 struct module_sect_attr *sattr; 994 struct module_sect_attr *sattr;
995 struct attribute **gattr; 995 struct attribute **gattr;
996 996
997 /* Count loaded sections and allocate structures */ 997 /* Count loaded sections and allocate structures */
998 for (i = 0; i < nsect; i++) 998 for (i = 0; i < nsect; i++)
999 if (sechdrs[i].sh_flags & SHF_ALLOC) 999 if (sechdrs[i].sh_flags & SHF_ALLOC)
1000 nloaded++; 1000 nloaded++;
1001 size[0] = ALIGN(sizeof(*sect_attrs) 1001 size[0] = ALIGN(sizeof(*sect_attrs)
1002 + nloaded * sizeof(sect_attrs->attrs[0]), 1002 + nloaded * sizeof(sect_attrs->attrs[0]),
1003 sizeof(sect_attrs->grp.attrs[0])); 1003 sizeof(sect_attrs->grp.attrs[0]));
1004 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]); 1004 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1005 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL); 1005 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1006 if (sect_attrs == NULL) 1006 if (sect_attrs == NULL)
1007 return; 1007 return;
1008 1008
1009 /* Setup section attributes. */ 1009 /* Setup section attributes. */
1010 sect_attrs->grp.name = "sections"; 1010 sect_attrs->grp.name = "sections";
1011 sect_attrs->grp.attrs = (void *)sect_attrs + size[0]; 1011 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1012 1012
1013 sect_attrs->nsections = 0; 1013 sect_attrs->nsections = 0;
1014 sattr = &sect_attrs->attrs[0]; 1014 sattr = &sect_attrs->attrs[0];
1015 gattr = &sect_attrs->grp.attrs[0]; 1015 gattr = &sect_attrs->grp.attrs[0];
1016 for (i = 0; i < nsect; i++) { 1016 for (i = 0; i < nsect; i++) {
1017 if (! (sechdrs[i].sh_flags & SHF_ALLOC)) 1017 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1018 continue; 1018 continue;
1019 sattr->address = sechdrs[i].sh_addr; 1019 sattr->address = sechdrs[i].sh_addr;
1020 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name, 1020 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1021 GFP_KERNEL); 1021 GFP_KERNEL);
1022 if (sattr->name == NULL) 1022 if (sattr->name == NULL)
1023 goto out; 1023 goto out;
1024 sect_attrs->nsections++; 1024 sect_attrs->nsections++;
1025 sattr->mattr.show = module_sect_show; 1025 sattr->mattr.show = module_sect_show;
1026 sattr->mattr.store = NULL; 1026 sattr->mattr.store = NULL;
1027 sattr->mattr.attr.name = sattr->name; 1027 sattr->mattr.attr.name = sattr->name;
1028 sattr->mattr.attr.owner = mod; 1028 sattr->mattr.attr.owner = mod;
1029 sattr->mattr.attr.mode = S_IRUGO; 1029 sattr->mattr.attr.mode = S_IRUGO;
1030 *(gattr++) = &(sattr++)->mattr.attr; 1030 *(gattr++) = &(sattr++)->mattr.attr;
1031 } 1031 }
1032 *gattr = NULL; 1032 *gattr = NULL;
1033 1033
1034 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp)) 1034 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1035 goto out; 1035 goto out;
1036 1036
1037 mod->sect_attrs = sect_attrs; 1037 mod->sect_attrs = sect_attrs;
1038 return; 1038 return;
1039 out: 1039 out:
1040 free_sect_attrs(sect_attrs); 1040 free_sect_attrs(sect_attrs);
1041 } 1041 }
1042 1042
1043 static void remove_sect_attrs(struct module *mod) 1043 static void remove_sect_attrs(struct module *mod)
1044 { 1044 {
1045 if (mod->sect_attrs) { 1045 if (mod->sect_attrs) {
1046 sysfs_remove_group(&mod->mkobj.kobj, 1046 sysfs_remove_group(&mod->mkobj.kobj,
1047 &mod->sect_attrs->grp); 1047 &mod->sect_attrs->grp);
1048 /* We are positive that no one is using any sect attrs 1048 /* We are positive that no one is using any sect attrs
1049 * at this point. Deallocate immediately. */ 1049 * at this point. Deallocate immediately. */
1050 free_sect_attrs(mod->sect_attrs); 1050 free_sect_attrs(mod->sect_attrs);
1051 mod->sect_attrs = NULL; 1051 mod->sect_attrs = NULL;
1052 } 1052 }
1053 } 1053 }
1054 1054
1055 #else 1055 #else
1056 1056
1057 static inline void add_sect_attrs(struct module *mod, unsigned int nsect, 1057 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1058 char *sectstrings, Elf_Shdr *sechdrs) 1058 char *sectstrings, Elf_Shdr *sechdrs)
1059 { 1059 {
1060 } 1060 }
1061 1061
1062 static inline void remove_sect_attrs(struct module *mod) 1062 static inline void remove_sect_attrs(struct module *mod)
1063 { 1063 {
1064 } 1064 }
1065 #endif /* CONFIG_KALLSYMS */ 1065 #endif /* CONFIG_KALLSYMS */
1066 1066
1067 static int module_add_modinfo_attrs(struct module *mod) 1067 static int module_add_modinfo_attrs(struct module *mod)
1068 { 1068 {
1069 struct module_attribute *attr; 1069 struct module_attribute *attr;
1070 struct module_attribute *temp_attr; 1070 struct module_attribute *temp_attr;
1071 int error = 0; 1071 int error = 0;
1072 int i; 1072 int i;
1073 1073
1074 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) * 1074 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1075 (ARRAY_SIZE(modinfo_attrs) + 1)), 1075 (ARRAY_SIZE(modinfo_attrs) + 1)),
1076 GFP_KERNEL); 1076 GFP_KERNEL);
1077 if (!mod->modinfo_attrs) 1077 if (!mod->modinfo_attrs)
1078 return -ENOMEM; 1078 return -ENOMEM;
1079 1079
1080 temp_attr = mod->modinfo_attrs; 1080 temp_attr = mod->modinfo_attrs;
1081 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { 1081 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1082 if (!attr->test || 1082 if (!attr->test ||
1083 (attr->test && attr->test(mod))) { 1083 (attr->test && attr->test(mod))) {
1084 memcpy(temp_attr, attr, sizeof(*temp_attr)); 1084 memcpy(temp_attr, attr, sizeof(*temp_attr));
1085 temp_attr->attr.owner = mod; 1085 temp_attr->attr.owner = mod;
1086 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr); 1086 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1087 ++temp_attr; 1087 ++temp_attr;
1088 } 1088 }
1089 } 1089 }
1090 return error; 1090 return error;
1091 } 1091 }
1092 1092
1093 static void module_remove_modinfo_attrs(struct module *mod) 1093 static void module_remove_modinfo_attrs(struct module *mod)
1094 { 1094 {
1095 struct module_attribute *attr; 1095 struct module_attribute *attr;
1096 int i; 1096 int i;
1097 1097
1098 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { 1098 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1099 /* pick a field to test for end of list */ 1099 /* pick a field to test for end of list */
1100 if (!attr->attr.name) 1100 if (!attr->attr.name)
1101 break; 1101 break;
1102 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr); 1102 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1103 if (attr->free) 1103 if (attr->free)
1104 attr->free(mod); 1104 attr->free(mod);
1105 } 1105 }
1106 kfree(mod->modinfo_attrs); 1106 kfree(mod->modinfo_attrs);
1107 } 1107 }
1108 1108
1109 static int mod_sysfs_setup(struct module *mod, 1109 static int mod_sysfs_setup(struct module *mod,
1110 struct kernel_param *kparam, 1110 struct kernel_param *kparam,
1111 unsigned int num_params) 1111 unsigned int num_params)
1112 { 1112 {
1113 int err; 1113 int err;
1114 1114
1115 if (!module_subsys.kset.subsys) { 1115 if (!module_subsys.kset.subsys) {
1116 printk(KERN_ERR "%s: module_subsys not initialized\n", 1116 printk(KERN_ERR "%s: module_subsys not initialized\n",
1117 mod->name); 1117 mod->name);
1118 err = -EINVAL; 1118 err = -EINVAL;
1119 goto out; 1119 goto out;
1120 } 1120 }
1121 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); 1121 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1122 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name); 1122 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1123 if (err) 1123 if (err)
1124 goto out; 1124 goto out;
1125 kobj_set_kset_s(&mod->mkobj, module_subsys); 1125 kobj_set_kset_s(&mod->mkobj, module_subsys);
1126 mod->mkobj.mod = mod; 1126 mod->mkobj.mod = mod;
1127 1127
1128 /* delay uevent until full sysfs population */ 1128 /* delay uevent until full sysfs population */
1129 kobject_init(&mod->mkobj.kobj); 1129 kobject_init(&mod->mkobj.kobj);
1130 err = kobject_add(&mod->mkobj.kobj); 1130 err = kobject_add(&mod->mkobj.kobj);
1131 if (err) 1131 if (err)
1132 goto out; 1132 goto out;
1133 1133
1134 mod->drivers_dir = kobject_add_dir(&mod->mkobj.kobj, "drivers"); 1134 mod->mkobj.drivers_dir = kobject_add_dir(&mod->mkobj.kobj, "drivers");
1135 if (!mod->drivers_dir) { 1135 if (!mod->mkobj.drivers_dir) {
1136 err = -ENOMEM; 1136 err = -ENOMEM;
1137 goto out_unreg; 1137 goto out_unreg;
1138 } 1138 }
1139 1139
1140 err = module_param_sysfs_setup(mod, kparam, num_params); 1140 err = module_param_sysfs_setup(mod, kparam, num_params);
1141 if (err) 1141 if (err)
1142 goto out_unreg_drivers; 1142 goto out_unreg_drivers;
1143 1143
1144 err = module_add_modinfo_attrs(mod); 1144 err = module_add_modinfo_attrs(mod);
1145 if (err) 1145 if (err)
1146 goto out_unreg_param; 1146 goto out_unreg_param;
1147 1147
1148 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 1148 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1149 return 0; 1149 return 0;
1150 1150
1151 out_unreg_param: 1151 out_unreg_param:
1152 module_param_sysfs_remove(mod); 1152 module_param_sysfs_remove(mod);
1153 out_unreg_drivers: 1153 out_unreg_drivers:
1154 kobject_unregister(mod->drivers_dir); 1154 kobject_unregister(mod->mkobj.drivers_dir);
1155 out_unreg: 1155 out_unreg:
1156 kobject_del(&mod->mkobj.kobj); 1156 kobject_del(&mod->mkobj.kobj);
1157 kobject_put(&mod->mkobj.kobj); 1157 kobject_put(&mod->mkobj.kobj);
1158 out: 1158 out:
1159 return err; 1159 return err;
1160 } 1160 }
1161 1161
1162 static void mod_kobject_remove(struct module *mod) 1162 static void mod_kobject_remove(struct module *mod)
1163 { 1163 {
1164 module_remove_modinfo_attrs(mod); 1164 module_remove_modinfo_attrs(mod);
1165 module_param_sysfs_remove(mod); 1165 module_param_sysfs_remove(mod);
1166 kobject_unregister(mod->drivers_dir); 1166 kobject_unregister(mod->mkobj.drivers_dir);
1167 1167
1168 kobject_unregister(&mod->mkobj.kobj); 1168 kobject_unregister(&mod->mkobj.kobj);
1169 } 1169 }
1170 1170
1171 /* 1171 /*
1172 * unlink the module with the whole machine is stopped with interrupts off 1172 * unlink the module with the whole machine is stopped with interrupts off
1173 * - this defends against kallsyms not taking locks 1173 * - this defends against kallsyms not taking locks
1174 */ 1174 */
1175 static int __unlink_module(void *_mod) 1175 static int __unlink_module(void *_mod)
1176 { 1176 {
1177 struct module *mod = _mod; 1177 struct module *mod = _mod;
1178 list_del(&mod->list); 1178 list_del(&mod->list);
1179 return 0; 1179 return 0;
1180 } 1180 }
1181 1181
1182 /* Free a module, remove from lists, etc (must hold module mutex). */ 1182 /* Free a module, remove from lists, etc (must hold module mutex). */
1183 static void free_module(struct module *mod) 1183 static void free_module(struct module *mod)
1184 { 1184 {
1185 /* Delete from various lists */ 1185 /* Delete from various lists */
1186 stop_machine_run(__unlink_module, mod, NR_CPUS); 1186 stop_machine_run(__unlink_module, mod, NR_CPUS);
1187 remove_sect_attrs(mod); 1187 remove_sect_attrs(mod);
1188 mod_kobject_remove(mod); 1188 mod_kobject_remove(mod);
1189 1189
1190 unwind_remove_table(mod->unwind_info, 0); 1190 unwind_remove_table(mod->unwind_info, 0);
1191 1191
1192 /* Arch-specific cleanup. */ 1192 /* Arch-specific cleanup. */
1193 module_arch_cleanup(mod); 1193 module_arch_cleanup(mod);
1194 1194
1195 /* Module unload stuff */ 1195 /* Module unload stuff */
1196 module_unload_free(mod); 1196 module_unload_free(mod);
1197 1197
1198 /* This may be NULL, but that's OK */ 1198 /* This may be NULL, but that's OK */
1199 module_free(mod, mod->module_init); 1199 module_free(mod, mod->module_init);
1200 kfree(mod->args); 1200 kfree(mod->args);
1201 if (mod->percpu) 1201 if (mod->percpu)
1202 percpu_modfree(mod->percpu); 1202 percpu_modfree(mod->percpu);
1203 1203
1204 /* Free lock-classes: */ 1204 /* Free lock-classes: */
1205 lockdep_free_key_range(mod->module_core, mod->core_size); 1205 lockdep_free_key_range(mod->module_core, mod->core_size);
1206 1206
1207 /* Finally, free the core (containing the module structure) */ 1207 /* Finally, free the core (containing the module structure) */
1208 module_free(mod, mod->module_core); 1208 module_free(mod, mod->module_core);
1209 } 1209 }
1210 1210
1211 void *__symbol_get(const char *symbol) 1211 void *__symbol_get(const char *symbol)
1212 { 1212 {
1213 struct module *owner; 1213 struct module *owner;
1214 unsigned long value, flags; 1214 unsigned long value, flags;
1215 const unsigned long *crc; 1215 const unsigned long *crc;
1216 1216
1217 spin_lock_irqsave(&modlist_lock, flags); 1217 spin_lock_irqsave(&modlist_lock, flags);
1218 value = __find_symbol(symbol, &owner, &crc, 1); 1218 value = __find_symbol(symbol, &owner, &crc, 1);
1219 if (value && !strong_try_module_get(owner)) 1219 if (value && !strong_try_module_get(owner))
1220 value = 0; 1220 value = 0;
1221 spin_unlock_irqrestore(&modlist_lock, flags); 1221 spin_unlock_irqrestore(&modlist_lock, flags);
1222 1222
1223 return (void *)value; 1223 return (void *)value;
1224 } 1224 }
1225 EXPORT_SYMBOL_GPL(__symbol_get); 1225 EXPORT_SYMBOL_GPL(__symbol_get);
1226 1226
1227 /* 1227 /*
1228 * Ensure that an exported symbol [global namespace] does not already exist 1228 * Ensure that an exported symbol [global namespace] does not already exist
1229 * in the Kernel or in some other modules exported symbol table. 1229 * in the Kernel or in some other modules exported symbol table.
1230 */ 1230 */
1231 static int verify_export_symbols(struct module *mod) 1231 static int verify_export_symbols(struct module *mod)
1232 { 1232 {
1233 const char *name = NULL; 1233 const char *name = NULL;
1234 unsigned long i, ret = 0; 1234 unsigned long i, ret = 0;
1235 struct module *owner; 1235 struct module *owner;
1236 const unsigned long *crc; 1236 const unsigned long *crc;
1237 1237
1238 for (i = 0; i < mod->num_syms; i++) 1238 for (i = 0; i < mod->num_syms; i++)
1239 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) { 1239 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
1240 name = mod->syms[i].name; 1240 name = mod->syms[i].name;
1241 ret = -ENOEXEC; 1241 ret = -ENOEXEC;
1242 goto dup; 1242 goto dup;
1243 } 1243 }
1244 1244
1245 for (i = 0; i < mod->num_gpl_syms; i++) 1245 for (i = 0; i < mod->num_gpl_syms; i++)
1246 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) { 1246 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
1247 name = mod->gpl_syms[i].name; 1247 name = mod->gpl_syms[i].name;
1248 ret = -ENOEXEC; 1248 ret = -ENOEXEC;
1249 goto dup; 1249 goto dup;
1250 } 1250 }
1251 1251
1252 dup: 1252 dup:
1253 if (ret) 1253 if (ret)
1254 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n", 1254 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1255 mod->name, name, module_name(owner)); 1255 mod->name, name, module_name(owner));
1256 1256
1257 return ret; 1257 return ret;
1258 } 1258 }
1259 1259
1260 /* Change all symbols so that sh_value encodes the pointer directly. */ 1260 /* Change all symbols so that sh_value encodes the pointer directly. */
1261 static int simplify_symbols(Elf_Shdr *sechdrs, 1261 static int simplify_symbols(Elf_Shdr *sechdrs,
1262 unsigned int symindex, 1262 unsigned int symindex,
1263 const char *strtab, 1263 const char *strtab,
1264 unsigned int versindex, 1264 unsigned int versindex,
1265 unsigned int pcpuindex, 1265 unsigned int pcpuindex,
1266 struct module *mod) 1266 struct module *mod)
1267 { 1267 {
1268 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr; 1268 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1269 unsigned long secbase; 1269 unsigned long secbase;
1270 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1270 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1271 int ret = 0; 1271 int ret = 0;
1272 1272
1273 for (i = 1; i < n; i++) { 1273 for (i = 1; i < n; i++) {
1274 switch (sym[i].st_shndx) { 1274 switch (sym[i].st_shndx) {
1275 case SHN_COMMON: 1275 case SHN_COMMON:
1276 /* We compiled with -fno-common. These are not 1276 /* We compiled with -fno-common. These are not
1277 supposed to happen. */ 1277 supposed to happen. */
1278 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name); 1278 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1279 printk("%s: please compile with -fno-common\n", 1279 printk("%s: please compile with -fno-common\n",
1280 mod->name); 1280 mod->name);
1281 ret = -ENOEXEC; 1281 ret = -ENOEXEC;
1282 break; 1282 break;
1283 1283
1284 case SHN_ABS: 1284 case SHN_ABS:
1285 /* Don't need to do anything */ 1285 /* Don't need to do anything */
1286 DEBUGP("Absolute symbol: 0x%08lx\n", 1286 DEBUGP("Absolute symbol: 0x%08lx\n",
1287 (long)sym[i].st_value); 1287 (long)sym[i].st_value);
1288 break; 1288 break;
1289 1289
1290 case SHN_UNDEF: 1290 case SHN_UNDEF:
1291 sym[i].st_value 1291 sym[i].st_value
1292 = resolve_symbol(sechdrs, versindex, 1292 = resolve_symbol(sechdrs, versindex,
1293 strtab + sym[i].st_name, mod); 1293 strtab + sym[i].st_name, mod);
1294 1294
1295 /* Ok if resolved. */ 1295 /* Ok if resolved. */
1296 if (sym[i].st_value != 0) 1296 if (sym[i].st_value != 0)
1297 break; 1297 break;
1298 /* Ok if weak. */ 1298 /* Ok if weak. */
1299 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1299 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1300 break; 1300 break;
1301 1301
1302 printk(KERN_WARNING "%s: Unknown symbol %s\n", 1302 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1303 mod->name, strtab + sym[i].st_name); 1303 mod->name, strtab + sym[i].st_name);
1304 ret = -ENOENT; 1304 ret = -ENOENT;
1305 break; 1305 break;
1306 1306
1307 default: 1307 default:
1308 /* Divert to percpu allocation if a percpu var. */ 1308 /* Divert to percpu allocation if a percpu var. */
1309 if (sym[i].st_shndx == pcpuindex) 1309 if (sym[i].st_shndx == pcpuindex)
1310 secbase = (unsigned long)mod->percpu; 1310 secbase = (unsigned long)mod->percpu;
1311 else 1311 else
1312 secbase = sechdrs[sym[i].st_shndx].sh_addr; 1312 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1313 sym[i].st_value += secbase; 1313 sym[i].st_value += secbase;
1314 break; 1314 break;
1315 } 1315 }
1316 } 1316 }
1317 1317
1318 return ret; 1318 return ret;
1319 } 1319 }
1320 1320
1321 /* Update size with this section: return offset. */ 1321 /* Update size with this section: return offset. */
1322 static long get_offset(unsigned long *size, Elf_Shdr *sechdr) 1322 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1323 { 1323 {
1324 long ret; 1324 long ret;
1325 1325
1326 ret = ALIGN(*size, sechdr->sh_addralign ?: 1); 1326 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1327 *size = ret + sechdr->sh_size; 1327 *size = ret + sechdr->sh_size;
1328 return ret; 1328 return ret;
1329 } 1329 }
1330 1330
1331 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld 1331 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1332 might -- code, read-only data, read-write data, small data. Tally 1332 might -- code, read-only data, read-write data, small data. Tally
1333 sizes, and place the offsets into sh_entsize fields: high bit means it 1333 sizes, and place the offsets into sh_entsize fields: high bit means it
1334 belongs in init. */ 1334 belongs in init. */
1335 static void layout_sections(struct module *mod, 1335 static void layout_sections(struct module *mod,
1336 const Elf_Ehdr *hdr, 1336 const Elf_Ehdr *hdr,
1337 Elf_Shdr *sechdrs, 1337 Elf_Shdr *sechdrs,
1338 const char *secstrings) 1338 const char *secstrings)
1339 { 1339 {
1340 static unsigned long const masks[][2] = { 1340 static unsigned long const masks[][2] = {
1341 /* NOTE: all executable code must be the first section 1341 /* NOTE: all executable code must be the first section
1342 * in this array; otherwise modify the text_size 1342 * in this array; otherwise modify the text_size
1343 * finder in the two loops below */ 1343 * finder in the two loops below */
1344 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, 1344 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1345 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL }, 1345 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1346 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, 1346 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1347 { ARCH_SHF_SMALL | SHF_ALLOC, 0 } 1347 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1348 }; 1348 };
1349 unsigned int m, i; 1349 unsigned int m, i;
1350 1350
1351 for (i = 0; i < hdr->e_shnum; i++) 1351 for (i = 0; i < hdr->e_shnum; i++)
1352 sechdrs[i].sh_entsize = ~0UL; 1352 sechdrs[i].sh_entsize = ~0UL;
1353 1353
1354 DEBUGP("Core section allocation order:\n"); 1354 DEBUGP("Core section allocation order:\n");
1355 for (m = 0; m < ARRAY_SIZE(masks); ++m) { 1355 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1356 for (i = 0; i < hdr->e_shnum; ++i) { 1356 for (i = 0; i < hdr->e_shnum; ++i) {
1357 Elf_Shdr *s = &sechdrs[i]; 1357 Elf_Shdr *s = &sechdrs[i];
1358 1358
1359 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1359 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1360 || (s->sh_flags & masks[m][1]) 1360 || (s->sh_flags & masks[m][1])
1361 || s->sh_entsize != ~0UL 1361 || s->sh_entsize != ~0UL
1362 || strncmp(secstrings + s->sh_name, 1362 || strncmp(secstrings + s->sh_name,
1363 ".init", 5) == 0) 1363 ".init", 5) == 0)
1364 continue; 1364 continue;
1365 s->sh_entsize = get_offset(&mod->core_size, s); 1365 s->sh_entsize = get_offset(&mod->core_size, s);
1366 DEBUGP("\t%s\n", secstrings + s->sh_name); 1366 DEBUGP("\t%s\n", secstrings + s->sh_name);
1367 } 1367 }
1368 if (m == 0) 1368 if (m == 0)
1369 mod->core_text_size = mod->core_size; 1369 mod->core_text_size = mod->core_size;
1370 } 1370 }
1371 1371
1372 DEBUGP("Init section allocation order:\n"); 1372 DEBUGP("Init section allocation order:\n");
1373 for (m = 0; m < ARRAY_SIZE(masks); ++m) { 1373 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1374 for (i = 0; i < hdr->e_shnum; ++i) { 1374 for (i = 0; i < hdr->e_shnum; ++i) {
1375 Elf_Shdr *s = &sechdrs[i]; 1375 Elf_Shdr *s = &sechdrs[i];
1376 1376
1377 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1377 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1378 || (s->sh_flags & masks[m][1]) 1378 || (s->sh_flags & masks[m][1])
1379 || s->sh_entsize != ~0UL 1379 || s->sh_entsize != ~0UL
1380 || strncmp(secstrings + s->sh_name, 1380 || strncmp(secstrings + s->sh_name,
1381 ".init", 5) != 0) 1381 ".init", 5) != 0)
1382 continue; 1382 continue;
1383 s->sh_entsize = (get_offset(&mod->init_size, s) 1383 s->sh_entsize = (get_offset(&mod->init_size, s)
1384 | INIT_OFFSET_MASK); 1384 | INIT_OFFSET_MASK);
1385 DEBUGP("\t%s\n", secstrings + s->sh_name); 1385 DEBUGP("\t%s\n", secstrings + s->sh_name);
1386 } 1386 }
1387 if (m == 0) 1387 if (m == 0)
1388 mod->init_text_size = mod->init_size; 1388 mod->init_text_size = mod->init_size;
1389 } 1389 }
1390 } 1390 }
1391 1391
1392 static void set_license(struct module *mod, const char *license) 1392 static void set_license(struct module *mod, const char *license)
1393 { 1393 {
1394 if (!license) 1394 if (!license)
1395 license = "unspecified"; 1395 license = "unspecified";
1396 1396
1397 if (!license_is_gpl_compatible(license)) { 1397 if (!license_is_gpl_compatible(license)) {
1398 if (!(tainted & TAINT_PROPRIETARY_MODULE)) 1398 if (!(tainted & TAINT_PROPRIETARY_MODULE))
1399 printk(KERN_WARNING "%s: module license '%s' taints " 1399 printk(KERN_WARNING "%s: module license '%s' taints "
1400 "kernel.\n", mod->name, license); 1400 "kernel.\n", mod->name, license);
1401 add_taint_module(mod, TAINT_PROPRIETARY_MODULE); 1401 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1402 } 1402 }
1403 } 1403 }
1404 1404
1405 /* Parse tag=value strings from .modinfo section */ 1405 /* Parse tag=value strings from .modinfo section */
1406 static char *next_string(char *string, unsigned long *secsize) 1406 static char *next_string(char *string, unsigned long *secsize)
1407 { 1407 {
1408 /* Skip non-zero chars */ 1408 /* Skip non-zero chars */
1409 while (string[0]) { 1409 while (string[0]) {
1410 string++; 1410 string++;
1411 if ((*secsize)-- <= 1) 1411 if ((*secsize)-- <= 1)
1412 return NULL; 1412 return NULL;
1413 } 1413 }
1414 1414
1415 /* Skip any zero padding. */ 1415 /* Skip any zero padding. */
1416 while (!string[0]) { 1416 while (!string[0]) {
1417 string++; 1417 string++;
1418 if ((*secsize)-- <= 1) 1418 if ((*secsize)-- <= 1)
1419 return NULL; 1419 return NULL;
1420 } 1420 }
1421 return string; 1421 return string;
1422 } 1422 }
1423 1423
1424 static char *get_modinfo(Elf_Shdr *sechdrs, 1424 static char *get_modinfo(Elf_Shdr *sechdrs,
1425 unsigned int info, 1425 unsigned int info,
1426 const char *tag) 1426 const char *tag)
1427 { 1427 {
1428 char *p; 1428 char *p;
1429 unsigned int taglen = strlen(tag); 1429 unsigned int taglen = strlen(tag);
1430 unsigned long size = sechdrs[info].sh_size; 1430 unsigned long size = sechdrs[info].sh_size;
1431 1431
1432 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) { 1432 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1433 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') 1433 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1434 return p + taglen + 1; 1434 return p + taglen + 1;
1435 } 1435 }
1436 return NULL; 1436 return NULL;
1437 } 1437 }
1438 1438
1439 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs, 1439 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1440 unsigned int infoindex) 1440 unsigned int infoindex)
1441 { 1441 {
1442 struct module_attribute *attr; 1442 struct module_attribute *attr;
1443 int i; 1443 int i;
1444 1444
1445 for (i = 0; (attr = modinfo_attrs[i]); i++) { 1445 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1446 if (attr->setup) 1446 if (attr->setup)
1447 attr->setup(mod, 1447 attr->setup(mod,
1448 get_modinfo(sechdrs, 1448 get_modinfo(sechdrs,
1449 infoindex, 1449 infoindex,
1450 attr->attr.name)); 1450 attr->attr.name));
1451 } 1451 }
1452 } 1452 }
1453 1453
1454 #ifdef CONFIG_KALLSYMS 1454 #ifdef CONFIG_KALLSYMS
1455 int is_exported(const char *name, const struct module *mod) 1455 int is_exported(const char *name, const struct module *mod)
1456 { 1456 {
1457 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab)) 1457 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1458 return 1; 1458 return 1;
1459 else 1459 else
1460 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms)) 1460 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1461 return 1; 1461 return 1;
1462 else 1462 else
1463 return 0; 1463 return 0;
1464 } 1464 }
1465 1465
1466 /* As per nm */ 1466 /* As per nm */
1467 static char elf_type(const Elf_Sym *sym, 1467 static char elf_type(const Elf_Sym *sym,
1468 Elf_Shdr *sechdrs, 1468 Elf_Shdr *sechdrs,
1469 const char *secstrings, 1469 const char *secstrings,
1470 struct module *mod) 1470 struct module *mod)
1471 { 1471 {
1472 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { 1472 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1473 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) 1473 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1474 return 'v'; 1474 return 'v';
1475 else 1475 else
1476 return 'w'; 1476 return 'w';
1477 } 1477 }
1478 if (sym->st_shndx == SHN_UNDEF) 1478 if (sym->st_shndx == SHN_UNDEF)
1479 return 'U'; 1479 return 'U';
1480 if (sym->st_shndx == SHN_ABS) 1480 if (sym->st_shndx == SHN_ABS)
1481 return 'a'; 1481 return 'a';
1482 if (sym->st_shndx >= SHN_LORESERVE) 1482 if (sym->st_shndx >= SHN_LORESERVE)
1483 return '?'; 1483 return '?';
1484 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) 1484 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1485 return 't'; 1485 return 't';
1486 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC 1486 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1487 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) { 1487 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1488 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) 1488 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1489 return 'r'; 1489 return 'r';
1490 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) 1490 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1491 return 'g'; 1491 return 'g';
1492 else 1492 else
1493 return 'd'; 1493 return 'd';
1494 } 1494 }
1495 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) { 1495 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1496 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) 1496 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1497 return 's'; 1497 return 's';
1498 else 1498 else
1499 return 'b'; 1499 return 'b';
1500 } 1500 }
1501 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name, 1501 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1502 ".debug", strlen(".debug")) == 0) 1502 ".debug", strlen(".debug")) == 0)
1503 return 'n'; 1503 return 'n';
1504 return '?'; 1504 return '?';
1505 } 1505 }
1506 1506
1507 static void add_kallsyms(struct module *mod, 1507 static void add_kallsyms(struct module *mod,
1508 Elf_Shdr *sechdrs, 1508 Elf_Shdr *sechdrs,
1509 unsigned int symindex, 1509 unsigned int symindex,
1510 unsigned int strindex, 1510 unsigned int strindex,
1511 const char *secstrings) 1511 const char *secstrings)
1512 { 1512 {
1513 unsigned int i; 1513 unsigned int i;
1514 1514
1515 mod->symtab = (void *)sechdrs[symindex].sh_addr; 1515 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1516 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1516 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1517 mod->strtab = (void *)sechdrs[strindex].sh_addr; 1517 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1518 1518
1519 /* Set types up while we still have access to sections. */ 1519 /* Set types up while we still have access to sections. */
1520 for (i = 0; i < mod->num_symtab; i++) 1520 for (i = 0; i < mod->num_symtab; i++)
1521 mod->symtab[i].st_info 1521 mod->symtab[i].st_info
1522 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod); 1522 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1523 } 1523 }
1524 #else 1524 #else
1525 static inline void add_kallsyms(struct module *mod, 1525 static inline void add_kallsyms(struct module *mod,
1526 Elf_Shdr *sechdrs, 1526 Elf_Shdr *sechdrs,
1527 unsigned int symindex, 1527 unsigned int symindex,
1528 unsigned int strindex, 1528 unsigned int strindex,
1529 const char *secstrings) 1529 const char *secstrings)
1530 { 1530 {
1531 } 1531 }
1532 #endif /* CONFIG_KALLSYMS */ 1532 #endif /* CONFIG_KALLSYMS */
1533 1533
1534 /* Allocate and load the module: note that size of section 0 is always 1534 /* Allocate and load the module: note that size of section 0 is always
1535 zero, and we rely on this for optional sections. */ 1535 zero, and we rely on this for optional sections. */
1536 static struct module *load_module(void __user *umod, 1536 static struct module *load_module(void __user *umod,
1537 unsigned long len, 1537 unsigned long len,
1538 const char __user *uargs) 1538 const char __user *uargs)
1539 { 1539 {
1540 Elf_Ehdr *hdr; 1540 Elf_Ehdr *hdr;
1541 Elf_Shdr *sechdrs; 1541 Elf_Shdr *sechdrs;
1542 char *secstrings, *args, *modmagic, *strtab = NULL; 1542 char *secstrings, *args, *modmagic, *strtab = NULL;
1543 unsigned int i; 1543 unsigned int i;
1544 unsigned int symindex = 0; 1544 unsigned int symindex = 0;
1545 unsigned int strindex = 0; 1545 unsigned int strindex = 0;
1546 unsigned int setupindex; 1546 unsigned int setupindex;
1547 unsigned int exindex; 1547 unsigned int exindex;
1548 unsigned int exportindex; 1548 unsigned int exportindex;
1549 unsigned int modindex; 1549 unsigned int modindex;
1550 unsigned int obsparmindex; 1550 unsigned int obsparmindex;
1551 unsigned int infoindex; 1551 unsigned int infoindex;
1552 unsigned int gplindex; 1552 unsigned int gplindex;
1553 unsigned int crcindex; 1553 unsigned int crcindex;
1554 unsigned int gplcrcindex; 1554 unsigned int gplcrcindex;
1555 unsigned int versindex; 1555 unsigned int versindex;
1556 unsigned int pcpuindex; 1556 unsigned int pcpuindex;
1557 unsigned int gplfutureindex; 1557 unsigned int gplfutureindex;
1558 unsigned int gplfuturecrcindex; 1558 unsigned int gplfuturecrcindex;
1559 unsigned int unwindex = 0; 1559 unsigned int unwindex = 0;
1560 unsigned int unusedindex; 1560 unsigned int unusedindex;
1561 unsigned int unusedcrcindex; 1561 unsigned int unusedcrcindex;
1562 unsigned int unusedgplindex; 1562 unsigned int unusedgplindex;
1563 unsigned int unusedgplcrcindex; 1563 unsigned int unusedgplcrcindex;
1564 struct module *mod; 1564 struct module *mod;
1565 long err = 0; 1565 long err = 0;
1566 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 1566 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1567 struct exception_table_entry *extable; 1567 struct exception_table_entry *extable;
1568 mm_segment_t old_fs; 1568 mm_segment_t old_fs;
1569 1569
1570 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n", 1570 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1571 umod, len, uargs); 1571 umod, len, uargs);
1572 if (len < sizeof(*hdr)) 1572 if (len < sizeof(*hdr))
1573 return ERR_PTR(-ENOEXEC); 1573 return ERR_PTR(-ENOEXEC);
1574 1574
1575 /* Suck in entire file: we'll want most of it. */ 1575 /* Suck in entire file: we'll want most of it. */
1576 /* vmalloc barfs on "unusual" numbers. Check here */ 1576 /* vmalloc barfs on "unusual" numbers. Check here */
1577 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) 1577 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1578 return ERR_PTR(-ENOMEM); 1578 return ERR_PTR(-ENOMEM);
1579 if (copy_from_user(hdr, umod, len) != 0) { 1579 if (copy_from_user(hdr, umod, len) != 0) {
1580 err = -EFAULT; 1580 err = -EFAULT;
1581 goto free_hdr; 1581 goto free_hdr;
1582 } 1582 }
1583 1583
1584 /* Sanity checks against insmoding binaries or wrong arch, 1584 /* Sanity checks against insmoding binaries or wrong arch,
1585 weird elf version */ 1585 weird elf version */
1586 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0 1586 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1587 || hdr->e_type != ET_REL 1587 || hdr->e_type != ET_REL
1588 || !elf_check_arch(hdr) 1588 || !elf_check_arch(hdr)
1589 || hdr->e_shentsize != sizeof(*sechdrs)) { 1589 || hdr->e_shentsize != sizeof(*sechdrs)) {
1590 err = -ENOEXEC; 1590 err = -ENOEXEC;
1591 goto free_hdr; 1591 goto free_hdr;
1592 } 1592 }
1593 1593
1594 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) 1594 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1595 goto truncated; 1595 goto truncated;
1596 1596
1597 /* Convenience variables */ 1597 /* Convenience variables */
1598 sechdrs = (void *)hdr + hdr->e_shoff; 1598 sechdrs = (void *)hdr + hdr->e_shoff;
1599 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 1599 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1600 sechdrs[0].sh_addr = 0; 1600 sechdrs[0].sh_addr = 0;
1601 1601
1602 for (i = 1; i < hdr->e_shnum; i++) { 1602 for (i = 1; i < hdr->e_shnum; i++) {
1603 if (sechdrs[i].sh_type != SHT_NOBITS 1603 if (sechdrs[i].sh_type != SHT_NOBITS
1604 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) 1604 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1605 goto truncated; 1605 goto truncated;
1606 1606
1607 /* Mark all sections sh_addr with their address in the 1607 /* Mark all sections sh_addr with their address in the
1608 temporary image. */ 1608 temporary image. */
1609 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset; 1609 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1610 1610
1611 /* Internal symbols and strings. */ 1611 /* Internal symbols and strings. */
1612 if (sechdrs[i].sh_type == SHT_SYMTAB) { 1612 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1613 symindex = i; 1613 symindex = i;
1614 strindex = sechdrs[i].sh_link; 1614 strindex = sechdrs[i].sh_link;
1615 strtab = (char *)hdr + sechdrs[strindex].sh_offset; 1615 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1616 } 1616 }
1617 #ifndef CONFIG_MODULE_UNLOAD 1617 #ifndef CONFIG_MODULE_UNLOAD
1618 /* Don't load .exit sections */ 1618 /* Don't load .exit sections */
1619 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0) 1619 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1620 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; 1620 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1621 #endif 1621 #endif
1622 } 1622 }
1623 1623
1624 modindex = find_sec(hdr, sechdrs, secstrings, 1624 modindex = find_sec(hdr, sechdrs, secstrings,
1625 ".gnu.linkonce.this_module"); 1625 ".gnu.linkonce.this_module");
1626 if (!modindex) { 1626 if (!modindex) {
1627 printk(KERN_WARNING "No module found in object\n"); 1627 printk(KERN_WARNING "No module found in object\n");
1628 err = -ENOEXEC; 1628 err = -ENOEXEC;
1629 goto free_hdr; 1629 goto free_hdr;
1630 } 1630 }
1631 mod = (void *)sechdrs[modindex].sh_addr; 1631 mod = (void *)sechdrs[modindex].sh_addr;
1632 1632
1633 if (symindex == 0) { 1633 if (symindex == 0) {
1634 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n", 1634 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1635 mod->name); 1635 mod->name);
1636 err = -ENOEXEC; 1636 err = -ENOEXEC;
1637 goto free_hdr; 1637 goto free_hdr;
1638 } 1638 }
1639 1639
1640 /* Optional sections */ 1640 /* Optional sections */
1641 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab"); 1641 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1642 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl"); 1642 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1643 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future"); 1643 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
1644 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused"); 1644 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1645 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl"); 1645 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
1646 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab"); 1646 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1647 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl"); 1647 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1648 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future"); 1648 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
1649 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused"); 1649 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1650 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl"); 1650 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
1651 setupindex = find_sec(hdr, sechdrs, secstrings, "__param"); 1651 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1652 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table"); 1652 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1653 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm"); 1653 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1654 versindex = find_sec(hdr, sechdrs, secstrings, "__versions"); 1654 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1655 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo"); 1655 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1656 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings); 1656 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1657 #ifdef ARCH_UNWIND_SECTION_NAME 1657 #ifdef ARCH_UNWIND_SECTION_NAME
1658 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME); 1658 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1659 #endif 1659 #endif
1660 1660
1661 /* Don't keep modinfo section */ 1661 /* Don't keep modinfo section */
1662 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 1662 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1663 #ifdef CONFIG_KALLSYMS 1663 #ifdef CONFIG_KALLSYMS
1664 /* Keep symbol and string tables for decoding later. */ 1664 /* Keep symbol and string tables for decoding later. */
1665 sechdrs[symindex].sh_flags |= SHF_ALLOC; 1665 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1666 sechdrs[strindex].sh_flags |= SHF_ALLOC; 1666 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1667 #endif 1667 #endif
1668 if (unwindex) 1668 if (unwindex)
1669 sechdrs[unwindex].sh_flags |= SHF_ALLOC; 1669 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1670 1670
1671 /* Check module struct version now, before we try to use module. */ 1671 /* Check module struct version now, before we try to use module. */
1672 if (!check_modstruct_version(sechdrs, versindex, mod)) { 1672 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1673 err = -ENOEXEC; 1673 err = -ENOEXEC;
1674 goto free_hdr; 1674 goto free_hdr;
1675 } 1675 }
1676 1676
1677 modmagic = get_modinfo(sechdrs, infoindex, "vermagic"); 1677 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1678 /* This is allowed: modprobe --force will invalidate it. */ 1678 /* This is allowed: modprobe --force will invalidate it. */
1679 if (!modmagic) { 1679 if (!modmagic) {
1680 add_taint_module(mod, TAINT_FORCED_MODULE); 1680 add_taint_module(mod, TAINT_FORCED_MODULE);
1681 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n", 1681 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1682 mod->name); 1682 mod->name);
1683 } else if (!same_magic(modmagic, vermagic)) { 1683 } else if (!same_magic(modmagic, vermagic)) {
1684 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n", 1684 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1685 mod->name, modmagic, vermagic); 1685 mod->name, modmagic, vermagic);
1686 err = -ENOEXEC; 1686 err = -ENOEXEC;
1687 goto free_hdr; 1687 goto free_hdr;
1688 } 1688 }
1689 1689
1690 /* Now copy in args */ 1690 /* Now copy in args */
1691 args = strndup_user(uargs, ~0UL >> 1); 1691 args = strndup_user(uargs, ~0UL >> 1);
1692 if (IS_ERR(args)) { 1692 if (IS_ERR(args)) {
1693 err = PTR_ERR(args); 1693 err = PTR_ERR(args);
1694 goto free_hdr; 1694 goto free_hdr;
1695 } 1695 }
1696 1696
1697 if (find_module(mod->name)) { 1697 if (find_module(mod->name)) {
1698 err = -EEXIST; 1698 err = -EEXIST;
1699 goto free_mod; 1699 goto free_mod;
1700 } 1700 }
1701 1701
1702 mod->state = MODULE_STATE_COMING; 1702 mod->state = MODULE_STATE_COMING;
1703 1703
1704 /* Allow arches to frob section contents and sizes. */ 1704 /* Allow arches to frob section contents and sizes. */
1705 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod); 1705 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1706 if (err < 0) 1706 if (err < 0)
1707 goto free_mod; 1707 goto free_mod;
1708 1708
1709 if (pcpuindex) { 1709 if (pcpuindex) {
1710 /* We have a special allocation for this section. */ 1710 /* We have a special allocation for this section. */
1711 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size, 1711 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1712 sechdrs[pcpuindex].sh_addralign, 1712 sechdrs[pcpuindex].sh_addralign,
1713 mod->name); 1713 mod->name);
1714 if (!percpu) { 1714 if (!percpu) {
1715 err = -ENOMEM; 1715 err = -ENOMEM;
1716 goto free_mod; 1716 goto free_mod;
1717 } 1717 }
1718 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 1718 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1719 mod->percpu = percpu; 1719 mod->percpu = percpu;
1720 } 1720 }
1721 1721
1722 /* Determine total sizes, and put offsets in sh_entsize. For now 1722 /* Determine total sizes, and put offsets in sh_entsize. For now
1723 this is done generically; there doesn't appear to be any 1723 this is done generically; there doesn't appear to be any
1724 special cases for the architectures. */ 1724 special cases for the architectures. */
1725 layout_sections(mod, hdr, sechdrs, secstrings); 1725 layout_sections(mod, hdr, sechdrs, secstrings);
1726 1726
1727 /* Do the allocs. */ 1727 /* Do the allocs. */
1728 ptr = module_alloc(mod->core_size); 1728 ptr = module_alloc(mod->core_size);
1729 if (!ptr) { 1729 if (!ptr) {
1730 err = -ENOMEM; 1730 err = -ENOMEM;
1731 goto free_percpu; 1731 goto free_percpu;
1732 } 1732 }
1733 memset(ptr, 0, mod->core_size); 1733 memset(ptr, 0, mod->core_size);
1734 mod->module_core = ptr; 1734 mod->module_core = ptr;
1735 1735
1736 ptr = module_alloc(mod->init_size); 1736 ptr = module_alloc(mod->init_size);
1737 if (!ptr && mod->init_size) { 1737 if (!ptr && mod->init_size) {
1738 err = -ENOMEM; 1738 err = -ENOMEM;
1739 goto free_core; 1739 goto free_core;
1740 } 1740 }
1741 memset(ptr, 0, mod->init_size); 1741 memset(ptr, 0, mod->init_size);
1742 mod->module_init = ptr; 1742 mod->module_init = ptr;
1743 1743
1744 /* Transfer each section which specifies SHF_ALLOC */ 1744 /* Transfer each section which specifies SHF_ALLOC */
1745 DEBUGP("final section addresses:\n"); 1745 DEBUGP("final section addresses:\n");
1746 for (i = 0; i < hdr->e_shnum; i++) { 1746 for (i = 0; i < hdr->e_shnum; i++) {
1747 void *dest; 1747 void *dest;
1748 1748
1749 if (!(sechdrs[i].sh_flags & SHF_ALLOC)) 1749 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1750 continue; 1750 continue;
1751 1751
1752 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK) 1752 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1753 dest = mod->module_init 1753 dest = mod->module_init
1754 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK); 1754 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1755 else 1755 else
1756 dest = mod->module_core + sechdrs[i].sh_entsize; 1756 dest = mod->module_core + sechdrs[i].sh_entsize;
1757 1757
1758 if (sechdrs[i].sh_type != SHT_NOBITS) 1758 if (sechdrs[i].sh_type != SHT_NOBITS)
1759 memcpy(dest, (void *)sechdrs[i].sh_addr, 1759 memcpy(dest, (void *)sechdrs[i].sh_addr,
1760 sechdrs[i].sh_size); 1760 sechdrs[i].sh_size);
1761 /* Update sh_addr to point to copy in image. */ 1761 /* Update sh_addr to point to copy in image. */
1762 sechdrs[i].sh_addr = (unsigned long)dest; 1762 sechdrs[i].sh_addr = (unsigned long)dest;
1763 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name); 1763 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1764 } 1764 }
1765 /* Module has been moved. */ 1765 /* Module has been moved. */
1766 mod = (void *)sechdrs[modindex].sh_addr; 1766 mod = (void *)sechdrs[modindex].sh_addr;
1767 1767
1768 /* Now we've moved module, initialize linked lists, etc. */ 1768 /* Now we've moved module, initialize linked lists, etc. */
1769 module_unload_init(mod); 1769 module_unload_init(mod);
1770 1770
1771 /* Set up license info based on the info section */ 1771 /* Set up license info based on the info section */
1772 set_license(mod, get_modinfo(sechdrs, infoindex, "license")); 1772 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1773 1773
1774 if (strcmp(mod->name, "ndiswrapper") == 0) 1774 if (strcmp(mod->name, "ndiswrapper") == 0)
1775 add_taint(TAINT_PROPRIETARY_MODULE); 1775 add_taint(TAINT_PROPRIETARY_MODULE);
1776 if (strcmp(mod->name, "driverloader") == 0) 1776 if (strcmp(mod->name, "driverloader") == 0)
1777 add_taint_module(mod, TAINT_PROPRIETARY_MODULE); 1777 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1778 1778
1779 /* Set up MODINFO_ATTR fields */ 1779 /* Set up MODINFO_ATTR fields */
1780 setup_modinfo(mod, sechdrs, infoindex); 1780 setup_modinfo(mod, sechdrs, infoindex);
1781 1781
1782 /* Fix up syms, so that st_value is a pointer to location. */ 1782 /* Fix up syms, so that st_value is a pointer to location. */
1783 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex, 1783 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1784 mod); 1784 mod);
1785 if (err < 0) 1785 if (err < 0)
1786 goto cleanup; 1786 goto cleanup;
1787 1787
1788 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */ 1788 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1789 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms); 1789 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1790 mod->syms = (void *)sechdrs[exportindex].sh_addr; 1790 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1791 if (crcindex) 1791 if (crcindex)
1792 mod->crcs = (void *)sechdrs[crcindex].sh_addr; 1792 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1793 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms); 1793 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1794 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr; 1794 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1795 if (gplcrcindex) 1795 if (gplcrcindex)
1796 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr; 1796 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1797 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size / 1797 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1798 sizeof(*mod->gpl_future_syms); 1798 sizeof(*mod->gpl_future_syms);
1799 mod->num_unused_syms = sechdrs[unusedindex].sh_size / 1799 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
1800 sizeof(*mod->unused_syms); 1800 sizeof(*mod->unused_syms);
1801 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size / 1801 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
1802 sizeof(*mod->unused_gpl_syms); 1802 sizeof(*mod->unused_gpl_syms);
1803 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr; 1803 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1804 if (gplfuturecrcindex) 1804 if (gplfuturecrcindex)
1805 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr; 1805 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
1806 1806
1807 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr; 1807 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
1808 if (unusedcrcindex) 1808 if (unusedcrcindex)
1809 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr; 1809 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
1810 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr; 1810 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
1811 if (unusedgplcrcindex) 1811 if (unusedgplcrcindex)
1812 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr; 1812 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
1813 1813
1814 #ifdef CONFIG_MODVERSIONS 1814 #ifdef CONFIG_MODVERSIONS
1815 if ((mod->num_syms && !crcindex) || 1815 if ((mod->num_syms && !crcindex) ||
1816 (mod->num_gpl_syms && !gplcrcindex) || 1816 (mod->num_gpl_syms && !gplcrcindex) ||
1817 (mod->num_gpl_future_syms && !gplfuturecrcindex) || 1817 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
1818 (mod->num_unused_syms && !unusedcrcindex) || 1818 (mod->num_unused_syms && !unusedcrcindex) ||
1819 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) { 1819 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
1820 printk(KERN_WARNING "%s: No versions for exported symbols." 1820 printk(KERN_WARNING "%s: No versions for exported symbols."
1821 " Tainting kernel.\n", mod->name); 1821 " Tainting kernel.\n", mod->name);
1822 add_taint_module(mod, TAINT_FORCED_MODULE); 1822 add_taint_module(mod, TAINT_FORCED_MODULE);
1823 } 1823 }
1824 #endif 1824 #endif
1825 1825
1826 /* Now do relocations. */ 1826 /* Now do relocations. */
1827 for (i = 1; i < hdr->e_shnum; i++) { 1827 for (i = 1; i < hdr->e_shnum; i++) {
1828 const char *strtab = (char *)sechdrs[strindex].sh_addr; 1828 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1829 unsigned int info = sechdrs[i].sh_info; 1829 unsigned int info = sechdrs[i].sh_info;
1830 1830
1831 /* Not a valid relocation section? */ 1831 /* Not a valid relocation section? */
1832 if (info >= hdr->e_shnum) 1832 if (info >= hdr->e_shnum)
1833 continue; 1833 continue;
1834 1834
1835 /* Don't bother with non-allocated sections */ 1835 /* Don't bother with non-allocated sections */
1836 if (!(sechdrs[info].sh_flags & SHF_ALLOC)) 1836 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1837 continue; 1837 continue;
1838 1838
1839 if (sechdrs[i].sh_type == SHT_REL) 1839 if (sechdrs[i].sh_type == SHT_REL)
1840 err = apply_relocate(sechdrs, strtab, symindex, i,mod); 1840 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1841 else if (sechdrs[i].sh_type == SHT_RELA) 1841 else if (sechdrs[i].sh_type == SHT_RELA)
1842 err = apply_relocate_add(sechdrs, strtab, symindex, i, 1842 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1843 mod); 1843 mod);
1844 if (err < 0) 1844 if (err < 0)
1845 goto cleanup; 1845 goto cleanup;
1846 } 1846 }
1847 1847
1848 /* Find duplicate symbols */ 1848 /* Find duplicate symbols */
1849 err = verify_export_symbols(mod); 1849 err = verify_export_symbols(mod);
1850 1850
1851 if (err < 0) 1851 if (err < 0)
1852 goto cleanup; 1852 goto cleanup;
1853 1853
1854 /* Set up and sort exception table */ 1854 /* Set up and sort exception table */
1855 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable); 1855 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1856 mod->extable = extable = (void *)sechdrs[exindex].sh_addr; 1856 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1857 sort_extable(extable, extable + mod->num_exentries); 1857 sort_extable(extable, extable + mod->num_exentries);
1858 1858
1859 /* Finally, copy percpu area over. */ 1859 /* Finally, copy percpu area over. */
1860 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr, 1860 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1861 sechdrs[pcpuindex].sh_size); 1861 sechdrs[pcpuindex].sh_size);
1862 1862
1863 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings); 1863 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1864 1864
1865 err = module_finalize(hdr, sechdrs, mod); 1865 err = module_finalize(hdr, sechdrs, mod);
1866 if (err < 0) 1866 if (err < 0)
1867 goto cleanup; 1867 goto cleanup;
1868 1868
1869 /* flush the icache in correct context */ 1869 /* flush the icache in correct context */
1870 old_fs = get_fs(); 1870 old_fs = get_fs();
1871 set_fs(KERNEL_DS); 1871 set_fs(KERNEL_DS);
1872 1872
1873 /* 1873 /*
1874 * Flush the instruction cache, since we've played with text. 1874 * Flush the instruction cache, since we've played with text.
1875 * Do it before processing of module parameters, so the module 1875 * Do it before processing of module parameters, so the module
1876 * can provide parameter accessor functions of its own. 1876 * can provide parameter accessor functions of its own.
1877 */ 1877 */
1878 if (mod->module_init) 1878 if (mod->module_init)
1879 flush_icache_range((unsigned long)mod->module_init, 1879 flush_icache_range((unsigned long)mod->module_init,
1880 (unsigned long)mod->module_init 1880 (unsigned long)mod->module_init
1881 + mod->init_size); 1881 + mod->init_size);
1882 flush_icache_range((unsigned long)mod->module_core, 1882 flush_icache_range((unsigned long)mod->module_core,
1883 (unsigned long)mod->module_core + mod->core_size); 1883 (unsigned long)mod->module_core + mod->core_size);
1884 1884
1885 set_fs(old_fs); 1885 set_fs(old_fs);
1886 1886
1887 mod->args = args; 1887 mod->args = args;
1888 if (obsparmindex) 1888 if (obsparmindex)
1889 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n", 1889 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
1890 mod->name); 1890 mod->name);
1891 1891
1892 /* Size of section 0 is 0, so this works well if no params */ 1892 /* Size of section 0 is 0, so this works well if no params */
1893 err = parse_args(mod->name, mod->args, 1893 err = parse_args(mod->name, mod->args,
1894 (struct kernel_param *) 1894 (struct kernel_param *)
1895 sechdrs[setupindex].sh_addr, 1895 sechdrs[setupindex].sh_addr,
1896 sechdrs[setupindex].sh_size 1896 sechdrs[setupindex].sh_size
1897 / sizeof(struct kernel_param), 1897 / sizeof(struct kernel_param),
1898 NULL); 1898 NULL);
1899 if (err < 0) 1899 if (err < 0)
1900 goto arch_cleanup; 1900 goto arch_cleanup;
1901 1901
1902 err = mod_sysfs_setup(mod, 1902 err = mod_sysfs_setup(mod,
1903 (struct kernel_param *) 1903 (struct kernel_param *)
1904 sechdrs[setupindex].sh_addr, 1904 sechdrs[setupindex].sh_addr,
1905 sechdrs[setupindex].sh_size 1905 sechdrs[setupindex].sh_size
1906 / sizeof(struct kernel_param)); 1906 / sizeof(struct kernel_param));
1907 if (err < 0) 1907 if (err < 0)
1908 goto arch_cleanup; 1908 goto arch_cleanup;
1909 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 1909 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1910 1910
1911 /* Size of section 0 is 0, so this works well if no unwind info. */ 1911 /* Size of section 0 is 0, so this works well if no unwind info. */
1912 mod->unwind_info = unwind_add_table(mod, 1912 mod->unwind_info = unwind_add_table(mod,
1913 (void *)sechdrs[unwindex].sh_addr, 1913 (void *)sechdrs[unwindex].sh_addr,
1914 sechdrs[unwindex].sh_size); 1914 sechdrs[unwindex].sh_size);
1915 1915
1916 /* Get rid of temporary copy */ 1916 /* Get rid of temporary copy */
1917 vfree(hdr); 1917 vfree(hdr);
1918 1918
1919 /* Done! */ 1919 /* Done! */
1920 return mod; 1920 return mod;
1921 1921
1922 arch_cleanup: 1922 arch_cleanup:
1923 module_arch_cleanup(mod); 1923 module_arch_cleanup(mod);
1924 cleanup: 1924 cleanup:
1925 module_unload_free(mod); 1925 module_unload_free(mod);
1926 module_free(mod, mod->module_init); 1926 module_free(mod, mod->module_init);
1927 free_core: 1927 free_core:
1928 module_free(mod, mod->module_core); 1928 module_free(mod, mod->module_core);
1929 free_percpu: 1929 free_percpu:
1930 if (percpu) 1930 if (percpu)
1931 percpu_modfree(percpu); 1931 percpu_modfree(percpu);
1932 free_mod: 1932 free_mod:
1933 kfree(args); 1933 kfree(args);
1934 free_hdr: 1934 free_hdr:
1935 vfree(hdr); 1935 vfree(hdr);
1936 return ERR_PTR(err); 1936 return ERR_PTR(err);
1937 1937
1938 truncated: 1938 truncated:
1939 printk(KERN_ERR "Module len %lu truncated\n", len); 1939 printk(KERN_ERR "Module len %lu truncated\n", len);
1940 err = -ENOEXEC; 1940 err = -ENOEXEC;
1941 goto free_hdr; 1941 goto free_hdr;
1942 } 1942 }
1943 1943
1944 /* 1944 /*
1945 * link the module with the whole machine is stopped with interrupts off 1945 * link the module with the whole machine is stopped with interrupts off
1946 * - this defends against kallsyms not taking locks 1946 * - this defends against kallsyms not taking locks
1947 */ 1947 */
1948 static int __link_module(void *_mod) 1948 static int __link_module(void *_mod)
1949 { 1949 {
1950 struct module *mod = _mod; 1950 struct module *mod = _mod;
1951 list_add(&mod->list, &modules); 1951 list_add(&mod->list, &modules);
1952 return 0; 1952 return 0;
1953 } 1953 }
1954 1954
1955 /* This is where the real work happens */ 1955 /* This is where the real work happens */
1956 asmlinkage long 1956 asmlinkage long
1957 sys_init_module(void __user *umod, 1957 sys_init_module(void __user *umod,
1958 unsigned long len, 1958 unsigned long len,
1959 const char __user *uargs) 1959 const char __user *uargs)
1960 { 1960 {
1961 struct module *mod; 1961 struct module *mod;
1962 int ret = 0; 1962 int ret = 0;
1963 1963
1964 /* Must have permission */ 1964 /* Must have permission */
1965 if (!capable(CAP_SYS_MODULE)) 1965 if (!capable(CAP_SYS_MODULE))
1966 return -EPERM; 1966 return -EPERM;
1967 1967
1968 /* Only one module load at a time, please */ 1968 /* Only one module load at a time, please */
1969 if (mutex_lock_interruptible(&module_mutex) != 0) 1969 if (mutex_lock_interruptible(&module_mutex) != 0)
1970 return -EINTR; 1970 return -EINTR;
1971 1971
1972 /* Do all the hard work */ 1972 /* Do all the hard work */
1973 mod = load_module(umod, len, uargs); 1973 mod = load_module(umod, len, uargs);
1974 if (IS_ERR(mod)) { 1974 if (IS_ERR(mod)) {
1975 mutex_unlock(&module_mutex); 1975 mutex_unlock(&module_mutex);
1976 return PTR_ERR(mod); 1976 return PTR_ERR(mod);
1977 } 1977 }
1978 1978
1979 /* Now sew it into the lists. They won't access us, since 1979 /* Now sew it into the lists. They won't access us, since
1980 strong_try_module_get() will fail. */ 1980 strong_try_module_get() will fail. */
1981 stop_machine_run(__link_module, mod, NR_CPUS); 1981 stop_machine_run(__link_module, mod, NR_CPUS);
1982 1982
1983 /* Drop lock so they can recurse */ 1983 /* Drop lock so they can recurse */
1984 mutex_unlock(&module_mutex); 1984 mutex_unlock(&module_mutex);
1985 1985
1986 blocking_notifier_call_chain(&module_notify_list, 1986 blocking_notifier_call_chain(&module_notify_list,
1987 MODULE_STATE_COMING, mod); 1987 MODULE_STATE_COMING, mod);
1988 1988
1989 /* Start the module */ 1989 /* Start the module */
1990 if (mod->init != NULL) 1990 if (mod->init != NULL)
1991 ret = mod->init(); 1991 ret = mod->init();
1992 if (ret < 0) { 1992 if (ret < 0) {
1993 /* Init routine failed: abort. Try to protect us from 1993 /* Init routine failed: abort. Try to protect us from
1994 buggy refcounters. */ 1994 buggy refcounters. */
1995 mod->state = MODULE_STATE_GOING; 1995 mod->state = MODULE_STATE_GOING;
1996 synchronize_sched(); 1996 synchronize_sched();
1997 if (mod->unsafe) 1997 if (mod->unsafe)
1998 printk(KERN_ERR "%s: module is now stuck!\n", 1998 printk(KERN_ERR "%s: module is now stuck!\n",
1999 mod->name); 1999 mod->name);
2000 else { 2000 else {
2001 module_put(mod); 2001 module_put(mod);
2002 mutex_lock(&module_mutex); 2002 mutex_lock(&module_mutex);
2003 free_module(mod); 2003 free_module(mod);
2004 mutex_unlock(&module_mutex); 2004 mutex_unlock(&module_mutex);
2005 } 2005 }
2006 return ret; 2006 return ret;
2007 } 2007 }
2008 2008
2009 /* Now it's a first class citizen! */ 2009 /* Now it's a first class citizen! */
2010 mutex_lock(&module_mutex); 2010 mutex_lock(&module_mutex);
2011 mod->state = MODULE_STATE_LIVE; 2011 mod->state = MODULE_STATE_LIVE;
2012 /* Drop initial reference. */ 2012 /* Drop initial reference. */
2013 module_put(mod); 2013 module_put(mod);
2014 unwind_remove_table(mod->unwind_info, 1); 2014 unwind_remove_table(mod->unwind_info, 1);
2015 module_free(mod, mod->module_init); 2015 module_free(mod, mod->module_init);
2016 mod->module_init = NULL; 2016 mod->module_init = NULL;
2017 mod->init_size = 0; 2017 mod->init_size = 0;
2018 mod->init_text_size = 0; 2018 mod->init_text_size = 0;
2019 mutex_unlock(&module_mutex); 2019 mutex_unlock(&module_mutex);
2020 2020
2021 return 0; 2021 return 0;
2022 } 2022 }
2023 2023
2024 static inline int within(unsigned long addr, void *start, unsigned long size) 2024 static inline int within(unsigned long addr, void *start, unsigned long size)
2025 { 2025 {
2026 return ((void *)addr >= start && (void *)addr < start + size); 2026 return ((void *)addr >= start && (void *)addr < start + size);
2027 } 2027 }
2028 2028
2029 #ifdef CONFIG_KALLSYMS 2029 #ifdef CONFIG_KALLSYMS
2030 /* 2030 /*
2031 * This ignores the intensely annoying "mapping symbols" found 2031 * This ignores the intensely annoying "mapping symbols" found
2032 * in ARM ELF files: $a, $t and $d. 2032 * in ARM ELF files: $a, $t and $d.
2033 */ 2033 */
2034 static inline int is_arm_mapping_symbol(const char *str) 2034 static inline int is_arm_mapping_symbol(const char *str)
2035 { 2035 {
2036 return str[0] == '$' && strchr("atd", str[1]) 2036 return str[0] == '$' && strchr("atd", str[1])
2037 && (str[2] == '\0' || str[2] == '.'); 2037 && (str[2] == '\0' || str[2] == '.');
2038 } 2038 }
2039 2039
2040 static const char *get_ksymbol(struct module *mod, 2040 static const char *get_ksymbol(struct module *mod,
2041 unsigned long addr, 2041 unsigned long addr,
2042 unsigned long *size, 2042 unsigned long *size,
2043 unsigned long *offset) 2043 unsigned long *offset)
2044 { 2044 {
2045 unsigned int i, best = 0; 2045 unsigned int i, best = 0;
2046 unsigned long nextval; 2046 unsigned long nextval;
2047 2047
2048 /* At worse, next value is at end of module */ 2048 /* At worse, next value is at end of module */
2049 if (within(addr, mod->module_init, mod->init_size)) 2049 if (within(addr, mod->module_init, mod->init_size))
2050 nextval = (unsigned long)mod->module_init+mod->init_text_size; 2050 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2051 else 2051 else
2052 nextval = (unsigned long)mod->module_core+mod->core_text_size; 2052 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2053 2053
2054 /* Scan for closest preceeding symbol, and next symbol. (ELF 2054 /* Scan for closest preceeding symbol, and next symbol. (ELF
2055 starts real symbols at 1). */ 2055 starts real symbols at 1). */
2056 for (i = 1; i < mod->num_symtab; i++) { 2056 for (i = 1; i < mod->num_symtab; i++) {
2057 if (mod->symtab[i].st_shndx == SHN_UNDEF) 2057 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2058 continue; 2058 continue;
2059 2059
2060 /* We ignore unnamed symbols: they're uninformative 2060 /* We ignore unnamed symbols: they're uninformative
2061 * and inserted at a whim. */ 2061 * and inserted at a whim. */
2062 if (mod->symtab[i].st_value <= addr 2062 if (mod->symtab[i].st_value <= addr
2063 && mod->symtab[i].st_value > mod->symtab[best].st_value 2063 && mod->symtab[i].st_value > mod->symtab[best].st_value
2064 && *(mod->strtab + mod->symtab[i].st_name) != '\0' 2064 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2065 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) 2065 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2066 best = i; 2066 best = i;
2067 if (mod->symtab[i].st_value > addr 2067 if (mod->symtab[i].st_value > addr
2068 && mod->symtab[i].st_value < nextval 2068 && mod->symtab[i].st_value < nextval
2069 && *(mod->strtab + mod->symtab[i].st_name) != '\0' 2069 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2070 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name)) 2070 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2071 nextval = mod->symtab[i].st_value; 2071 nextval = mod->symtab[i].st_value;
2072 } 2072 }
2073 2073
2074 if (!best) 2074 if (!best)
2075 return NULL; 2075 return NULL;
2076 2076
2077 *size = nextval - mod->symtab[best].st_value; 2077 *size = nextval - mod->symtab[best].st_value;
2078 *offset = addr - mod->symtab[best].st_value; 2078 *offset = addr - mod->symtab[best].st_value;
2079 return mod->strtab + mod->symtab[best].st_name; 2079 return mod->strtab + mod->symtab[best].st_name;
2080 } 2080 }
2081 2081
2082 /* For kallsyms to ask for address resolution. NULL means not found. 2082 /* For kallsyms to ask for address resolution. NULL means not found.
2083 We don't lock, as this is used for oops resolution and races are a 2083 We don't lock, as this is used for oops resolution and races are a
2084 lesser concern. */ 2084 lesser concern. */
2085 const char *module_address_lookup(unsigned long addr, 2085 const char *module_address_lookup(unsigned long addr,
2086 unsigned long *size, 2086 unsigned long *size,
2087 unsigned long *offset, 2087 unsigned long *offset,
2088 char **modname) 2088 char **modname)
2089 { 2089 {
2090 struct module *mod; 2090 struct module *mod;
2091 2091
2092 list_for_each_entry(mod, &modules, list) { 2092 list_for_each_entry(mod, &modules, list) {
2093 if (within(addr, mod->module_init, mod->init_size) 2093 if (within(addr, mod->module_init, mod->init_size)
2094 || within(addr, mod->module_core, mod->core_size)) { 2094 || within(addr, mod->module_core, mod->core_size)) {
2095 if (modname) 2095 if (modname)
2096 *modname = mod->name; 2096 *modname = mod->name;
2097 return get_ksymbol(mod, addr, size, offset); 2097 return get_ksymbol(mod, addr, size, offset);
2098 } 2098 }
2099 } 2099 }
2100 return NULL; 2100 return NULL;
2101 } 2101 }
2102 2102
2103 struct module *module_get_kallsym(unsigned int symnum, unsigned long *value, 2103 struct module *module_get_kallsym(unsigned int symnum, unsigned long *value,
2104 char *type, char *name, size_t namelen) 2104 char *type, char *name, size_t namelen)
2105 { 2105 {
2106 struct module *mod; 2106 struct module *mod;
2107 2107
2108 mutex_lock(&module_mutex); 2108 mutex_lock(&module_mutex);
2109 list_for_each_entry(mod, &modules, list) { 2109 list_for_each_entry(mod, &modules, list) {
2110 if (symnum < mod->num_symtab) { 2110 if (symnum < mod->num_symtab) {
2111 *value = mod->symtab[symnum].st_value; 2111 *value = mod->symtab[symnum].st_value;
2112 *type = mod->symtab[symnum].st_info; 2112 *type = mod->symtab[symnum].st_info;
2113 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name, 2113 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2114 namelen); 2114 namelen);
2115 mutex_unlock(&module_mutex); 2115 mutex_unlock(&module_mutex);
2116 return mod; 2116 return mod;
2117 } 2117 }
2118 symnum -= mod->num_symtab; 2118 symnum -= mod->num_symtab;
2119 } 2119 }
2120 mutex_unlock(&module_mutex); 2120 mutex_unlock(&module_mutex);
2121 return NULL; 2121 return NULL;
2122 } 2122 }
2123 2123
2124 static unsigned long mod_find_symname(struct module *mod, const char *name) 2124 static unsigned long mod_find_symname(struct module *mod, const char *name)
2125 { 2125 {
2126 unsigned int i; 2126 unsigned int i;
2127 2127
2128 for (i = 0; i < mod->num_symtab; i++) 2128 for (i = 0; i < mod->num_symtab; i++)
2129 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 && 2129 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2130 mod->symtab[i].st_info != 'U') 2130 mod->symtab[i].st_info != 'U')
2131 return mod->symtab[i].st_value; 2131 return mod->symtab[i].st_value;
2132 return 0; 2132 return 0;
2133 } 2133 }
2134 2134
2135 /* Look for this name: can be of form module:name. */ 2135 /* Look for this name: can be of form module:name. */
2136 unsigned long module_kallsyms_lookup_name(const char *name) 2136 unsigned long module_kallsyms_lookup_name(const char *name)
2137 { 2137 {
2138 struct module *mod; 2138 struct module *mod;
2139 char *colon; 2139 char *colon;
2140 unsigned long ret = 0; 2140 unsigned long ret = 0;
2141 2141
2142 /* Don't lock: we're in enough trouble already. */ 2142 /* Don't lock: we're in enough trouble already. */
2143 if ((colon = strchr(name, ':')) != NULL) { 2143 if ((colon = strchr(name, ':')) != NULL) {
2144 *colon = '\0'; 2144 *colon = '\0';
2145 if ((mod = find_module(name)) != NULL) 2145 if ((mod = find_module(name)) != NULL)
2146 ret = mod_find_symname(mod, colon+1); 2146 ret = mod_find_symname(mod, colon+1);
2147 *colon = ':'; 2147 *colon = ':';
2148 } else { 2148 } else {
2149 list_for_each_entry(mod, &modules, list) 2149 list_for_each_entry(mod, &modules, list)
2150 if ((ret = mod_find_symname(mod, name)) != 0) 2150 if ((ret = mod_find_symname(mod, name)) != 0)
2151 break; 2151 break;
2152 } 2152 }
2153 return ret; 2153 return ret;
2154 } 2154 }
2155 #endif /* CONFIG_KALLSYMS */ 2155 #endif /* CONFIG_KALLSYMS */
2156 2156
2157 /* Called by the /proc file system to return a list of modules. */ 2157 /* Called by the /proc file system to return a list of modules. */
2158 static void *m_start(struct seq_file *m, loff_t *pos) 2158 static void *m_start(struct seq_file *m, loff_t *pos)
2159 { 2159 {
2160 struct list_head *i; 2160 struct list_head *i;
2161 loff_t n = 0; 2161 loff_t n = 0;
2162 2162
2163 mutex_lock(&module_mutex); 2163 mutex_lock(&module_mutex);
2164 list_for_each(i, &modules) { 2164 list_for_each(i, &modules) {
2165 if (n++ == *pos) 2165 if (n++ == *pos)
2166 break; 2166 break;
2167 } 2167 }
2168 if (i == &modules) 2168 if (i == &modules)
2169 return NULL; 2169 return NULL;
2170 return i; 2170 return i;
2171 } 2171 }
2172 2172
2173 static void *m_next(struct seq_file *m, void *p, loff_t *pos) 2173 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2174 { 2174 {
2175 struct list_head *i = p; 2175 struct list_head *i = p;
2176 (*pos)++; 2176 (*pos)++;
2177 if (i->next == &modules) 2177 if (i->next == &modules)
2178 return NULL; 2178 return NULL;
2179 return i->next; 2179 return i->next;
2180 } 2180 }
2181 2181
2182 static void m_stop(struct seq_file *m, void *p) 2182 static void m_stop(struct seq_file *m, void *p)
2183 { 2183 {
2184 mutex_unlock(&module_mutex); 2184 mutex_unlock(&module_mutex);
2185 } 2185 }
2186 2186
2187 static char *taint_flags(unsigned int taints, char *buf) 2187 static char *taint_flags(unsigned int taints, char *buf)
2188 { 2188 {
2189 int bx = 0; 2189 int bx = 0;
2190 2190
2191 if (taints) { 2191 if (taints) {
2192 buf[bx++] = '('; 2192 buf[bx++] = '(';
2193 if (taints & TAINT_PROPRIETARY_MODULE) 2193 if (taints & TAINT_PROPRIETARY_MODULE)
2194 buf[bx++] = 'P'; 2194 buf[bx++] = 'P';
2195 if (taints & TAINT_FORCED_MODULE) 2195 if (taints & TAINT_FORCED_MODULE)
2196 buf[bx++] = 'F'; 2196 buf[bx++] = 'F';
2197 /* 2197 /*
2198 * TAINT_FORCED_RMMOD: could be added. 2198 * TAINT_FORCED_RMMOD: could be added.
2199 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't 2199 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2200 * apply to modules. 2200 * apply to modules.
2201 */ 2201 */
2202 buf[bx++] = ')'; 2202 buf[bx++] = ')';
2203 } 2203 }
2204 buf[bx] = '\0'; 2204 buf[bx] = '\0';
2205 2205
2206 return buf; 2206 return buf;
2207 } 2207 }
2208 2208
2209 static int m_show(struct seq_file *m, void *p) 2209 static int m_show(struct seq_file *m, void *p)
2210 { 2210 {
2211 struct module *mod = list_entry(p, struct module, list); 2211 struct module *mod = list_entry(p, struct module, list);
2212 char buf[8]; 2212 char buf[8];
2213 2213
2214 seq_printf(m, "%s %lu", 2214 seq_printf(m, "%s %lu",
2215 mod->name, mod->init_size + mod->core_size); 2215 mod->name, mod->init_size + mod->core_size);
2216 print_unload_info(m, mod); 2216 print_unload_info(m, mod);
2217 2217
2218 /* Informative for users. */ 2218 /* Informative for users. */
2219 seq_printf(m, " %s", 2219 seq_printf(m, " %s",
2220 mod->state == MODULE_STATE_GOING ? "Unloading": 2220 mod->state == MODULE_STATE_GOING ? "Unloading":
2221 mod->state == MODULE_STATE_COMING ? "Loading": 2221 mod->state == MODULE_STATE_COMING ? "Loading":
2222 "Live"); 2222 "Live");
2223 /* Used by oprofile and other similar tools. */ 2223 /* Used by oprofile and other similar tools. */
2224 seq_printf(m, " 0x%p", mod->module_core); 2224 seq_printf(m, " 0x%p", mod->module_core);
2225 2225
2226 /* Taints info */ 2226 /* Taints info */
2227 if (mod->taints) 2227 if (mod->taints)
2228 seq_printf(m, " %s", taint_flags(mod->taints, buf)); 2228 seq_printf(m, " %s", taint_flags(mod->taints, buf));
2229 2229
2230 seq_printf(m, "\n"); 2230 seq_printf(m, "\n");
2231 return 0; 2231 return 0;
2232 } 2232 }
2233 2233
2234 /* Format: modulename size refcount deps address 2234 /* Format: modulename size refcount deps address
2235 2235
2236 Where refcount is a number or -, and deps is a comma-separated list 2236 Where refcount is a number or -, and deps is a comma-separated list
2237 of depends or -. 2237 of depends or -.
2238 */ 2238 */
2239 const struct seq_operations modules_op = { 2239 const struct seq_operations modules_op = {
2240 .start = m_start, 2240 .start = m_start,
2241 .next = m_next, 2241 .next = m_next,
2242 .stop = m_stop, 2242 .stop = m_stop,
2243 .show = m_show 2243 .show = m_show
2244 }; 2244 };
2245 2245
2246 /* Given an address, look for it in the module exception tables. */ 2246 /* Given an address, look for it in the module exception tables. */
2247 const struct exception_table_entry *search_module_extables(unsigned long addr) 2247 const struct exception_table_entry *search_module_extables(unsigned long addr)
2248 { 2248 {
2249 unsigned long flags; 2249 unsigned long flags;
2250 const struct exception_table_entry *e = NULL; 2250 const struct exception_table_entry *e = NULL;
2251 struct module *mod; 2251 struct module *mod;
2252 2252
2253 spin_lock_irqsave(&modlist_lock, flags); 2253 spin_lock_irqsave(&modlist_lock, flags);
2254 list_for_each_entry(mod, &modules, list) { 2254 list_for_each_entry(mod, &modules, list) {
2255 if (mod->num_exentries == 0) 2255 if (mod->num_exentries == 0)
2256 continue; 2256 continue;
2257 2257
2258 e = search_extable(mod->extable, 2258 e = search_extable(mod->extable,
2259 mod->extable + mod->num_exentries - 1, 2259 mod->extable + mod->num_exentries - 1,
2260 addr); 2260 addr);
2261 if (e) 2261 if (e)
2262 break; 2262 break;
2263 } 2263 }
2264 spin_unlock_irqrestore(&modlist_lock, flags); 2264 spin_unlock_irqrestore(&modlist_lock, flags);
2265 2265
2266 /* Now, if we found one, we are running inside it now, hence 2266 /* Now, if we found one, we are running inside it now, hence
2267 we cannot unload the module, hence no refcnt needed. */ 2267 we cannot unload the module, hence no refcnt needed. */
2268 return e; 2268 return e;
2269 } 2269 }
2270 2270
2271 /* 2271 /*
2272 * Is this a valid module address? 2272 * Is this a valid module address?
2273 */ 2273 */
2274 int is_module_address(unsigned long addr) 2274 int is_module_address(unsigned long addr)
2275 { 2275 {
2276 unsigned long flags; 2276 unsigned long flags;
2277 struct module *mod; 2277 struct module *mod;
2278 2278
2279 spin_lock_irqsave(&modlist_lock, flags); 2279 spin_lock_irqsave(&modlist_lock, flags);
2280 2280
2281 list_for_each_entry(mod, &modules, list) { 2281 list_for_each_entry(mod, &modules, list) {
2282 if (within(addr, mod->module_core, mod->core_size)) { 2282 if (within(addr, mod->module_core, mod->core_size)) {
2283 spin_unlock_irqrestore(&modlist_lock, flags); 2283 spin_unlock_irqrestore(&modlist_lock, flags);
2284 return 1; 2284 return 1;
2285 } 2285 }
2286 } 2286 }
2287 2287
2288 spin_unlock_irqrestore(&modlist_lock, flags); 2288 spin_unlock_irqrestore(&modlist_lock, flags);
2289 2289
2290 return 0; 2290 return 0;
2291 } 2291 }
2292 2292
2293 2293
2294 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */ 2294 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2295 struct module *__module_text_address(unsigned long addr) 2295 struct module *__module_text_address(unsigned long addr)
2296 { 2296 {
2297 struct module *mod; 2297 struct module *mod;
2298 2298
2299 list_for_each_entry(mod, &modules, list) 2299 list_for_each_entry(mod, &modules, list)
2300 if (within(addr, mod->module_init, mod->init_text_size) 2300 if (within(addr, mod->module_init, mod->init_text_size)
2301 || within(addr, mod->module_core, mod->core_text_size)) 2301 || within(addr, mod->module_core, mod->core_text_size))
2302 return mod; 2302 return mod;
2303 return NULL; 2303 return NULL;
2304 } 2304 }
2305 2305
2306 struct module *module_text_address(unsigned long addr) 2306 struct module *module_text_address(unsigned long addr)
2307 { 2307 {
2308 struct module *mod; 2308 struct module *mod;
2309 unsigned long flags; 2309 unsigned long flags;
2310 2310
2311 spin_lock_irqsave(&modlist_lock, flags); 2311 spin_lock_irqsave(&modlist_lock, flags);
2312 mod = __module_text_address(addr); 2312 mod = __module_text_address(addr);
2313 spin_unlock_irqrestore(&modlist_lock, flags); 2313 spin_unlock_irqrestore(&modlist_lock, flags);
2314 2314
2315 return mod; 2315 return mod;
2316 } 2316 }
2317 2317
2318 /* Don't grab lock, we're oopsing. */ 2318 /* Don't grab lock, we're oopsing. */
2319 void print_modules(void) 2319 void print_modules(void)
2320 { 2320 {
2321 struct module *mod; 2321 struct module *mod;
2322 char buf[8]; 2322 char buf[8];
2323 2323
2324 printk("Modules linked in:"); 2324 printk("Modules linked in:");
2325 list_for_each_entry(mod, &modules, list) 2325 list_for_each_entry(mod, &modules, list)
2326 printk(" %s%s", mod->name, taint_flags(mod->taints, buf)); 2326 printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
2327 printk("\n"); 2327 printk("\n");
2328 } 2328 }
2329 2329
2330 static char *make_driver_name(struct device_driver *drv) 2330 static char *make_driver_name(struct device_driver *drv)
2331 { 2331 {
2332 char *driver_name; 2332 char *driver_name;
2333 2333
2334 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2, 2334 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2335 GFP_KERNEL); 2335 GFP_KERNEL);
2336 if (!driver_name) 2336 if (!driver_name)
2337 return NULL; 2337 return NULL;
2338 2338
2339 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name); 2339 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2340 return driver_name; 2340 return driver_name;
2341 } 2341 }
2342 2342
2343 void module_add_driver(struct module *mod, struct device_driver *drv) 2343 void module_add_driver(struct module *mod, struct device_driver *drv)
2344 { 2344 {
2345 char *driver_name; 2345 char *driver_name;
2346 int no_warn; 2346 int no_warn;
2347 struct module_kobject *mk = NULL;
2347 2348
2348 if (!mod || !drv) 2349 if (!drv)
2349 return; 2350 return;
2350 2351
2352 if (mod)
2353 mk = &mod->mkobj;
2354 else if (drv->mod_name) {
2355 struct kobject *mkobj;
2356
2357 /* Lookup built-in module entry in /sys/modules */
2358 mkobj = kset_find_obj(&module_subsys.kset, drv->mod_name);
2359 if (mkobj)
2360 mk = container_of(mkobj, struct module_kobject, kobj);
2361 }
2362
2363 if (!mk)
2364 return;
2365
2351 /* Don't check return codes; these calls are idempotent */ 2366 /* Don't check return codes; these calls are idempotent */
2352 no_warn = sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module"); 2367 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
2353 driver_name = make_driver_name(drv); 2368 driver_name = make_driver_name(drv);
2354 if (driver_name) { 2369 if (driver_name) {
2355 no_warn = sysfs_create_link(mod->drivers_dir, &drv->kobj, 2370 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
2356 driver_name); 2371 driver_name);
2357 kfree(driver_name); 2372 kfree(driver_name);
2358 } 2373 }
2359 } 2374 }
2360 EXPORT_SYMBOL(module_add_driver); 2375 EXPORT_SYMBOL(module_add_driver);
2361 2376
2362 void module_remove_driver(struct device_driver *drv) 2377 void module_remove_driver(struct device_driver *drv)
2363 { 2378 {
2364 char *driver_name; 2379 char *driver_name;
2365 2380
2366 if (!drv) 2381 if (!drv)
2367 return; 2382 return;
2368 2383
2369 sysfs_remove_link(&drv->kobj, "module"); 2384 sysfs_remove_link(&drv->kobj, "module");
2370 if (drv->owner && drv->owner->drivers_dir) { 2385 if (drv->owner && drv->owner->mkobj.drivers_dir) {
2371 driver_name = make_driver_name(drv); 2386 driver_name = make_driver_name(drv);
2372 if (driver_name) { 2387 if (driver_name) {
2373 sysfs_remove_link(drv->owner->drivers_dir, 2388 sysfs_remove_link(drv->owner->mkobj.drivers_dir,
2374 driver_name); 2389 driver_name);
2375 kfree(driver_name); 2390 kfree(driver_name);
2376 } 2391 }
2377 } 2392 }
2378 } 2393 }
2379 EXPORT_SYMBOL(module_remove_driver); 2394 EXPORT_SYMBOL(module_remove_driver);
2380 2395
2381 #ifdef CONFIG_MODVERSIONS 2396 #ifdef CONFIG_MODVERSIONS
2382 /* Generate the signature for struct module here, too, for modversions. */ 2397 /* Generate the signature for struct module here, too, for modversions. */
2383 void struct_module(struct module *mod) { return; } 2398 void struct_module(struct module *mod) { return; }
2384 EXPORT_SYMBOL(struct_module); 2399 EXPORT_SYMBOL(struct_module);
2385 #endif 2400 #endif
2386 2401
1 /* Helpers for initial module or kernel cmdline parsing 1 /* Helpers for initial module or kernel cmdline parsing
2 Copyright (C) 2001 Rusty Russell. 2 Copyright (C) 2001 Rusty Russell.
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/moduleparam.h> 18 #include <linux/moduleparam.h>
19 #include <linux/kernel.h> 19 #include <linux/kernel.h>
20 #include <linux/string.h> 20 #include <linux/string.h>
21 #include <linux/errno.h> 21 #include <linux/errno.h>
22 #include <linux/module.h> 22 #include <linux/module.h>
23 #include <linux/device.h> 23 #include <linux/device.h>
24 #include <linux/err.h> 24 #include <linux/err.h>
25 #include <linux/slab.h> 25 #include <linux/slab.h>
26 26
27 #if 0 27 #if 0
28 #define DEBUGP printk 28 #define DEBUGP printk
29 #else 29 #else
30 #define DEBUGP(fmt, a...) 30 #define DEBUGP(fmt, a...)
31 #endif 31 #endif
32 32
33 static inline char dash2underscore(char c) 33 static inline char dash2underscore(char c)
34 { 34 {
35 if (c == '-') 35 if (c == '-')
36 return '_'; 36 return '_';
37 return c; 37 return c;
38 } 38 }
39 39
40 static inline int parameq(const char *input, const char *paramname) 40 static inline int parameq(const char *input, const char *paramname)
41 { 41 {
42 unsigned int i; 42 unsigned int i;
43 for (i = 0; dash2underscore(input[i]) == paramname[i]; i++) 43 for (i = 0; dash2underscore(input[i]) == paramname[i]; i++)
44 if (input[i] == '\0') 44 if (input[i] == '\0')
45 return 1; 45 return 1;
46 return 0; 46 return 0;
47 } 47 }
48 48
49 static int parse_one(char *param, 49 static int parse_one(char *param,
50 char *val, 50 char *val,
51 struct kernel_param *params, 51 struct kernel_param *params,
52 unsigned num_params, 52 unsigned num_params,
53 int (*handle_unknown)(char *param, char *val)) 53 int (*handle_unknown)(char *param, char *val))
54 { 54 {
55 unsigned int i; 55 unsigned int i;
56 56
57 /* Find parameter */ 57 /* Find parameter */
58 for (i = 0; i < num_params; i++) { 58 for (i = 0; i < num_params; i++) {
59 if (parameq(param, params[i].name)) { 59 if (parameq(param, params[i].name)) {
60 DEBUGP("They are equal! Calling %p\n", 60 DEBUGP("They are equal! Calling %p\n",
61 params[i].set); 61 params[i].set);
62 return params[i].set(val, &params[i]); 62 return params[i].set(val, &params[i]);
63 } 63 }
64 } 64 }
65 65
66 if (handle_unknown) { 66 if (handle_unknown) {
67 DEBUGP("Unknown argument: calling %p\n", handle_unknown); 67 DEBUGP("Unknown argument: calling %p\n", handle_unknown);
68 return handle_unknown(param, val); 68 return handle_unknown(param, val);
69 } 69 }
70 70
71 DEBUGP("Unknown argument `%s'\n", param); 71 DEBUGP("Unknown argument `%s'\n", param);
72 return -ENOENT; 72 return -ENOENT;
73 } 73 }
74 74
75 /* You can use " around spaces, but can't escape ". */ 75 /* You can use " around spaces, but can't escape ". */
76 /* Hyphens and underscores equivalent in parameter names. */ 76 /* Hyphens and underscores equivalent in parameter names. */
77 static char *next_arg(char *args, char **param, char **val) 77 static char *next_arg(char *args, char **param, char **val)
78 { 78 {
79 unsigned int i, equals = 0; 79 unsigned int i, equals = 0;
80 int in_quote = 0, quoted = 0; 80 int in_quote = 0, quoted = 0;
81 char *next; 81 char *next;
82 82
83 if (*args == '"') { 83 if (*args == '"') {
84 args++; 84 args++;
85 in_quote = 1; 85 in_quote = 1;
86 quoted = 1; 86 quoted = 1;
87 } 87 }
88 88
89 for (i = 0; args[i]; i++) { 89 for (i = 0; args[i]; i++) {
90 if (args[i] == ' ' && !in_quote) 90 if (args[i] == ' ' && !in_quote)
91 break; 91 break;
92 if (equals == 0) { 92 if (equals == 0) {
93 if (args[i] == '=') 93 if (args[i] == '=')
94 equals = i; 94 equals = i;
95 } 95 }
96 if (args[i] == '"') 96 if (args[i] == '"')
97 in_quote = !in_quote; 97 in_quote = !in_quote;
98 } 98 }
99 99
100 *param = args; 100 *param = args;
101 if (!equals) 101 if (!equals)
102 *val = NULL; 102 *val = NULL;
103 else { 103 else {
104 args[equals] = '\0'; 104 args[equals] = '\0';
105 *val = args + equals + 1; 105 *val = args + equals + 1;
106 106
107 /* Don't include quotes in value. */ 107 /* Don't include quotes in value. */
108 if (**val == '"') { 108 if (**val == '"') {
109 (*val)++; 109 (*val)++;
110 if (args[i-1] == '"') 110 if (args[i-1] == '"')
111 args[i-1] = '\0'; 111 args[i-1] = '\0';
112 } 112 }
113 if (quoted && args[i-1] == '"') 113 if (quoted && args[i-1] == '"')
114 args[i-1] = '\0'; 114 args[i-1] = '\0';
115 } 115 }
116 116
117 if (args[i]) { 117 if (args[i]) {
118 args[i] = '\0'; 118 args[i] = '\0';
119 next = args + i + 1; 119 next = args + i + 1;
120 } else 120 } else
121 next = args + i; 121 next = args + i;
122 122
123 /* Chew up trailing spaces. */ 123 /* Chew up trailing spaces. */
124 while (*next == ' ') 124 while (*next == ' ')
125 next++; 125 next++;
126 return next; 126 return next;
127 } 127 }
128 128
129 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */ 129 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
130 int parse_args(const char *name, 130 int parse_args(const char *name,
131 char *args, 131 char *args,
132 struct kernel_param *params, 132 struct kernel_param *params,
133 unsigned num, 133 unsigned num,
134 int (*unknown)(char *param, char *val)) 134 int (*unknown)(char *param, char *val))
135 { 135 {
136 char *param, *val; 136 char *param, *val;
137 137
138 DEBUGP("Parsing ARGS: %s\n", args); 138 DEBUGP("Parsing ARGS: %s\n", args);
139 139
140 /* Chew leading spaces */ 140 /* Chew leading spaces */
141 while (*args == ' ') 141 while (*args == ' ')
142 args++; 142 args++;
143 143
144 while (*args) { 144 while (*args) {
145 int ret; 145 int ret;
146 int irq_was_disabled; 146 int irq_was_disabled;
147 147
148 args = next_arg(args, &param, &val); 148 args = next_arg(args, &param, &val);
149 irq_was_disabled = irqs_disabled(); 149 irq_was_disabled = irqs_disabled();
150 ret = parse_one(param, val, params, num, unknown); 150 ret = parse_one(param, val, params, num, unknown);
151 if (irq_was_disabled && !irqs_disabled()) { 151 if (irq_was_disabled && !irqs_disabled()) {
152 printk(KERN_WARNING "parse_args(): option '%s' enabled " 152 printk(KERN_WARNING "parse_args(): option '%s' enabled "
153 "irq's!\n", param); 153 "irq's!\n", param);
154 } 154 }
155 switch (ret) { 155 switch (ret) {
156 case -ENOENT: 156 case -ENOENT:
157 printk(KERN_ERR "%s: Unknown parameter `%s'\n", 157 printk(KERN_ERR "%s: Unknown parameter `%s'\n",
158 name, param); 158 name, param);
159 return ret; 159 return ret;
160 case -ENOSPC: 160 case -ENOSPC:
161 printk(KERN_ERR 161 printk(KERN_ERR
162 "%s: `%s' too large for parameter `%s'\n", 162 "%s: `%s' too large for parameter `%s'\n",
163 name, val ?: "", param); 163 name, val ?: "", param);
164 return ret; 164 return ret;
165 case 0: 165 case 0:
166 break; 166 break;
167 default: 167 default:
168 printk(KERN_ERR 168 printk(KERN_ERR
169 "%s: `%s' invalid for parameter `%s'\n", 169 "%s: `%s' invalid for parameter `%s'\n",
170 name, val ?: "", param); 170 name, val ?: "", param);
171 return ret; 171 return ret;
172 } 172 }
173 } 173 }
174 174
175 /* All parsed OK. */ 175 /* All parsed OK. */
176 return 0; 176 return 0;
177 } 177 }
178 178
179 /* Lazy bastard, eh? */ 179 /* Lazy bastard, eh? */
180 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \ 180 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
181 int param_set_##name(const char *val, struct kernel_param *kp) \ 181 int param_set_##name(const char *val, struct kernel_param *kp) \
182 { \ 182 { \
183 char *endp; \ 183 char *endp; \
184 tmptype l; \ 184 tmptype l; \
185 \ 185 \
186 if (!val) return -EINVAL; \ 186 if (!val) return -EINVAL; \
187 l = strtolfn(val, &endp, 0); \ 187 l = strtolfn(val, &endp, 0); \
188 if (endp == val || ((type)l != l)) \ 188 if (endp == val || ((type)l != l)) \
189 return -EINVAL; \ 189 return -EINVAL; \
190 *((type *)kp->arg) = l; \ 190 *((type *)kp->arg) = l; \
191 return 0; \ 191 return 0; \
192 } \ 192 } \
193 int param_get_##name(char *buffer, struct kernel_param *kp) \ 193 int param_get_##name(char *buffer, struct kernel_param *kp) \
194 { \ 194 { \
195 return sprintf(buffer, format, *((type *)kp->arg)); \ 195 return sprintf(buffer, format, *((type *)kp->arg)); \
196 } 196 }
197 197
198 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, simple_strtoul); 198 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, simple_strtoul);
199 STANDARD_PARAM_DEF(short, short, "%hi", long, simple_strtol); 199 STANDARD_PARAM_DEF(short, short, "%hi", long, simple_strtol);
200 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, simple_strtoul); 200 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, simple_strtoul);
201 STANDARD_PARAM_DEF(int, int, "%i", long, simple_strtol); 201 STANDARD_PARAM_DEF(int, int, "%i", long, simple_strtol);
202 STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, simple_strtoul); 202 STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, simple_strtoul);
203 STANDARD_PARAM_DEF(long, long, "%li", long, simple_strtol); 203 STANDARD_PARAM_DEF(long, long, "%li", long, simple_strtol);
204 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, simple_strtoul); 204 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, simple_strtoul);
205 205
206 int param_set_charp(const char *val, struct kernel_param *kp) 206 int param_set_charp(const char *val, struct kernel_param *kp)
207 { 207 {
208 if (!val) { 208 if (!val) {
209 printk(KERN_ERR "%s: string parameter expected\n", 209 printk(KERN_ERR "%s: string parameter expected\n",
210 kp->name); 210 kp->name);
211 return -EINVAL; 211 return -EINVAL;
212 } 212 }
213 213
214 if (strlen(val) > 1024) { 214 if (strlen(val) > 1024) {
215 printk(KERN_ERR "%s: string parameter too long\n", 215 printk(KERN_ERR "%s: string parameter too long\n",
216 kp->name); 216 kp->name);
217 return -ENOSPC; 217 return -ENOSPC;
218 } 218 }
219 219
220 *(char **)kp->arg = (char *)val; 220 *(char **)kp->arg = (char *)val;
221 return 0; 221 return 0;
222 } 222 }
223 223
224 int param_get_charp(char *buffer, struct kernel_param *kp) 224 int param_get_charp(char *buffer, struct kernel_param *kp)
225 { 225 {
226 return sprintf(buffer, "%s", *((char **)kp->arg)); 226 return sprintf(buffer, "%s", *((char **)kp->arg));
227 } 227 }
228 228
229 int param_set_bool(const char *val, struct kernel_param *kp) 229 int param_set_bool(const char *val, struct kernel_param *kp)
230 { 230 {
231 /* No equals means "set"... */ 231 /* No equals means "set"... */
232 if (!val) val = "1"; 232 if (!val) val = "1";
233 233
234 /* One of =[yYnN01] */ 234 /* One of =[yYnN01] */
235 switch (val[0]) { 235 switch (val[0]) {
236 case 'y': case 'Y': case '1': 236 case 'y': case 'Y': case '1':
237 *(int *)kp->arg = 1; 237 *(int *)kp->arg = 1;
238 return 0; 238 return 0;
239 case 'n': case 'N': case '0': 239 case 'n': case 'N': case '0':
240 *(int *)kp->arg = 0; 240 *(int *)kp->arg = 0;
241 return 0; 241 return 0;
242 } 242 }
243 return -EINVAL; 243 return -EINVAL;
244 } 244 }
245 245
246 int param_get_bool(char *buffer, struct kernel_param *kp) 246 int param_get_bool(char *buffer, struct kernel_param *kp)
247 { 247 {
248 /* Y and N chosen as being relatively non-coder friendly */ 248 /* Y and N chosen as being relatively non-coder friendly */
249 return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'Y' : 'N'); 249 return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'Y' : 'N');
250 } 250 }
251 251
252 int param_set_invbool(const char *val, struct kernel_param *kp) 252 int param_set_invbool(const char *val, struct kernel_param *kp)
253 { 253 {
254 int boolval, ret; 254 int boolval, ret;
255 struct kernel_param dummy = { .arg = &boolval }; 255 struct kernel_param dummy = { .arg = &boolval };
256 256
257 ret = param_set_bool(val, &dummy); 257 ret = param_set_bool(val, &dummy);
258 if (ret == 0) 258 if (ret == 0)
259 *(int *)kp->arg = !boolval; 259 *(int *)kp->arg = !boolval;
260 return ret; 260 return ret;
261 } 261 }
262 262
263 int param_get_invbool(char *buffer, struct kernel_param *kp) 263 int param_get_invbool(char *buffer, struct kernel_param *kp)
264 { 264 {
265 int val; 265 int val;
266 struct kernel_param dummy = { .arg = &val }; 266 struct kernel_param dummy = { .arg = &val };
267 267
268 val = !*(int *)kp->arg; 268 val = !*(int *)kp->arg;
269 return param_get_bool(buffer, &dummy); 269 return param_get_bool(buffer, &dummy);
270 } 270 }
271 271
272 /* We cheat here and temporarily mangle the string. */ 272 /* We cheat here and temporarily mangle the string. */
273 static int param_array(const char *name, 273 static int param_array(const char *name,
274 const char *val, 274 const char *val,
275 unsigned int min, unsigned int max, 275 unsigned int min, unsigned int max,
276 void *elem, int elemsize, 276 void *elem, int elemsize,
277 int (*set)(const char *, struct kernel_param *kp), 277 int (*set)(const char *, struct kernel_param *kp),
278 int *num) 278 int *num)
279 { 279 {
280 int ret; 280 int ret;
281 struct kernel_param kp; 281 struct kernel_param kp;
282 char save; 282 char save;
283 283
284 /* Get the name right for errors. */ 284 /* Get the name right for errors. */
285 kp.name = name; 285 kp.name = name;
286 kp.arg = elem; 286 kp.arg = elem;
287 287
288 /* No equals sign? */ 288 /* No equals sign? */
289 if (!val) { 289 if (!val) {
290 printk(KERN_ERR "%s: expects arguments\n", name); 290 printk(KERN_ERR "%s: expects arguments\n", name);
291 return -EINVAL; 291 return -EINVAL;
292 } 292 }
293 293
294 *num = 0; 294 *num = 0;
295 /* We expect a comma-separated list of values. */ 295 /* We expect a comma-separated list of values. */
296 do { 296 do {
297 int len; 297 int len;
298 298
299 if (*num == max) { 299 if (*num == max) {
300 printk(KERN_ERR "%s: can only take %i arguments\n", 300 printk(KERN_ERR "%s: can only take %i arguments\n",
301 name, max); 301 name, max);
302 return -EINVAL; 302 return -EINVAL;
303 } 303 }
304 len = strcspn(val, ","); 304 len = strcspn(val, ",");
305 305
306 /* nul-terminate and parse */ 306 /* nul-terminate and parse */
307 save = val[len]; 307 save = val[len];
308 ((char *)val)[len] = '\0'; 308 ((char *)val)[len] = '\0';
309 ret = set(val, &kp); 309 ret = set(val, &kp);
310 310
311 if (ret != 0) 311 if (ret != 0)
312 return ret; 312 return ret;
313 kp.arg += elemsize; 313 kp.arg += elemsize;
314 val += len+1; 314 val += len+1;
315 (*num)++; 315 (*num)++;
316 } while (save == ','); 316 } while (save == ',');
317 317
318 if (*num < min) { 318 if (*num < min) {
319 printk(KERN_ERR "%s: needs at least %i arguments\n", 319 printk(KERN_ERR "%s: needs at least %i arguments\n",
320 name, min); 320 name, min);
321 return -EINVAL; 321 return -EINVAL;
322 } 322 }
323 return 0; 323 return 0;
324 } 324 }
325 325
326 int param_array_set(const char *val, struct kernel_param *kp) 326 int param_array_set(const char *val, struct kernel_param *kp)
327 { 327 {
328 struct kparam_array *arr = kp->arg; 328 struct kparam_array *arr = kp->arg;
329 unsigned int temp_num; 329 unsigned int temp_num;
330 330
331 return param_array(kp->name, val, 1, arr->max, arr->elem, 331 return param_array(kp->name, val, 1, arr->max, arr->elem,
332 arr->elemsize, arr->set, arr->num ?: &temp_num); 332 arr->elemsize, arr->set, arr->num ?: &temp_num);
333 } 333 }
334 334
335 int param_array_get(char *buffer, struct kernel_param *kp) 335 int param_array_get(char *buffer, struct kernel_param *kp)
336 { 336 {
337 int i, off, ret; 337 int i, off, ret;
338 struct kparam_array *arr = kp->arg; 338 struct kparam_array *arr = kp->arg;
339 struct kernel_param p; 339 struct kernel_param p;
340 340
341 p = *kp; 341 p = *kp;
342 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) { 342 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
343 if (i) 343 if (i)
344 buffer[off++] = ','; 344 buffer[off++] = ',';
345 p.arg = arr->elem + arr->elemsize * i; 345 p.arg = arr->elem + arr->elemsize * i;
346 ret = arr->get(buffer + off, &p); 346 ret = arr->get(buffer + off, &p);
347 if (ret < 0) 347 if (ret < 0)
348 return ret; 348 return ret;
349 off += ret; 349 off += ret;
350 } 350 }
351 buffer[off] = '\0'; 351 buffer[off] = '\0';
352 return off; 352 return off;
353 } 353 }
354 354
355 int param_set_copystring(const char *val, struct kernel_param *kp) 355 int param_set_copystring(const char *val, struct kernel_param *kp)
356 { 356 {
357 struct kparam_string *kps = kp->arg; 357 struct kparam_string *kps = kp->arg;
358 358
359 if (strlen(val)+1 > kps->maxlen) { 359 if (strlen(val)+1 > kps->maxlen) {
360 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n", 360 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
361 kp->name, kps->maxlen-1); 361 kp->name, kps->maxlen-1);
362 return -ENOSPC; 362 return -ENOSPC;
363 } 363 }
364 strcpy(kps->string, val); 364 strcpy(kps->string, val);
365 return 0; 365 return 0;
366 } 366 }
367 367
368 int param_get_string(char *buffer, struct kernel_param *kp) 368 int param_get_string(char *buffer, struct kernel_param *kp)
369 { 369 {
370 struct kparam_string *kps = kp->arg; 370 struct kparam_string *kps = kp->arg;
371 return strlcpy(buffer, kps->string, kps->maxlen); 371 return strlcpy(buffer, kps->string, kps->maxlen);
372 } 372 }
373 373
374 /* sysfs output in /sys/modules/XYZ/parameters/ */ 374 /* sysfs output in /sys/modules/XYZ/parameters/ */
375 375
376 extern struct kernel_param __start___param[], __stop___param[]; 376 extern struct kernel_param __start___param[], __stop___param[];
377 377
378 #define MAX_KBUILD_MODNAME KOBJ_NAME_LEN 378 #define MAX_KBUILD_MODNAME KOBJ_NAME_LEN
379 379
380 struct param_attribute 380 struct param_attribute
381 { 381 {
382 struct module_attribute mattr; 382 struct module_attribute mattr;
383 struct kernel_param *param; 383 struct kernel_param *param;
384 }; 384 };
385 385
386 struct module_param_attrs 386 struct module_param_attrs
387 { 387 {
388 struct attribute_group grp; 388 struct attribute_group grp;
389 struct param_attribute attrs[0]; 389 struct param_attribute attrs[0];
390 }; 390 };
391 391
392 #define to_param_attr(n) container_of(n, struct param_attribute, mattr); 392 #define to_param_attr(n) container_of(n, struct param_attribute, mattr);
393 393
394 static ssize_t param_attr_show(struct module_attribute *mattr, 394 static ssize_t param_attr_show(struct module_attribute *mattr,
395 struct module *mod, char *buf) 395 struct module *mod, char *buf)
396 { 396 {
397 int count; 397 int count;
398 struct param_attribute *attribute = to_param_attr(mattr); 398 struct param_attribute *attribute = to_param_attr(mattr);
399 399
400 if (!attribute->param->get) 400 if (!attribute->param->get)
401 return -EPERM; 401 return -EPERM;
402 402
403 count = attribute->param->get(buf, attribute->param); 403 count = attribute->param->get(buf, attribute->param);
404 if (count > 0) { 404 if (count > 0) {
405 strcat(buf, "\n"); 405 strcat(buf, "\n");
406 ++count; 406 ++count;
407 } 407 }
408 return count; 408 return count;
409 } 409 }
410 410
411 /* sysfs always hands a nul-terminated string in buf. We rely on that. */ 411 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
412 static ssize_t param_attr_store(struct module_attribute *mattr, 412 static ssize_t param_attr_store(struct module_attribute *mattr,
413 struct module *owner, 413 struct module *owner,
414 const char *buf, size_t len) 414 const char *buf, size_t len)
415 { 415 {
416 int err; 416 int err;
417 struct param_attribute *attribute = to_param_attr(mattr); 417 struct param_attribute *attribute = to_param_attr(mattr);
418 418
419 if (!attribute->param->set) 419 if (!attribute->param->set)
420 return -EPERM; 420 return -EPERM;
421 421
422 err = attribute->param->set(buf, attribute->param); 422 err = attribute->param->set(buf, attribute->param);
423 if (!err) 423 if (!err)
424 return len; 424 return len;
425 return err; 425 return err;
426 } 426 }
427 427
428 #ifdef CONFIG_MODULES 428 #ifdef CONFIG_MODULES
429 #define __modinit 429 #define __modinit
430 #else 430 #else
431 #define __modinit __init 431 #define __modinit __init
432 #endif 432 #endif
433 433
434 /* 434 /*
435 * param_sysfs_setup - setup sysfs support for one module or KBUILD_MODNAME 435 * param_sysfs_setup - setup sysfs support for one module or KBUILD_MODNAME
436 * @mk: struct module_kobject (contains parent kobject) 436 * @mk: struct module_kobject (contains parent kobject)
437 * @kparam: array of struct kernel_param, the actual parameter definitions 437 * @kparam: array of struct kernel_param, the actual parameter definitions
438 * @num_params: number of entries in array 438 * @num_params: number of entries in array
439 * @name_skip: offset where the parameter name start in kparam[].name. Needed for built-in "modules" 439 * @name_skip: offset where the parameter name start in kparam[].name. Needed for built-in "modules"
440 * 440 *
441 * Create a kobject for a (per-module) group of parameters, and create files 441 * Create a kobject for a (per-module) group of parameters, and create files
442 * in sysfs. A pointer to the param_kobject is returned on success, 442 * in sysfs. A pointer to the param_kobject is returned on success,
443 * NULL if there's no parameter to export, or other ERR_PTR(err). 443 * NULL if there's no parameter to export, or other ERR_PTR(err).
444 */ 444 */
445 static __modinit struct module_param_attrs * 445 static __modinit struct module_param_attrs *
446 param_sysfs_setup(struct module_kobject *mk, 446 param_sysfs_setup(struct module_kobject *mk,
447 struct kernel_param *kparam, 447 struct kernel_param *kparam,
448 unsigned int num_params, 448 unsigned int num_params,
449 unsigned int name_skip) 449 unsigned int name_skip)
450 { 450 {
451 struct module_param_attrs *mp; 451 struct module_param_attrs *mp;
452 unsigned int valid_attrs = 0; 452 unsigned int valid_attrs = 0;
453 unsigned int i, size[2]; 453 unsigned int i, size[2];
454 struct param_attribute *pattr; 454 struct param_attribute *pattr;
455 struct attribute **gattr; 455 struct attribute **gattr;
456 int err; 456 int err;
457 457
458 for (i=0; i<num_params; i++) { 458 for (i=0; i<num_params; i++) {
459 if (kparam[i].perm) 459 if (kparam[i].perm)
460 valid_attrs++; 460 valid_attrs++;
461 } 461 }
462 462
463 if (!valid_attrs) 463 if (!valid_attrs)
464 return NULL; 464 return NULL;
465 465
466 size[0] = ALIGN(sizeof(*mp) + 466 size[0] = ALIGN(sizeof(*mp) +
467 valid_attrs * sizeof(mp->attrs[0]), 467 valid_attrs * sizeof(mp->attrs[0]),
468 sizeof(mp->grp.attrs[0])); 468 sizeof(mp->grp.attrs[0]));
469 size[1] = (valid_attrs + 1) * sizeof(mp->grp.attrs[0]); 469 size[1] = (valid_attrs + 1) * sizeof(mp->grp.attrs[0]);
470 470
471 mp = kmalloc(size[0] + size[1], GFP_KERNEL); 471 mp = kmalloc(size[0] + size[1], GFP_KERNEL);
472 if (!mp) 472 if (!mp)
473 return ERR_PTR(-ENOMEM); 473 return ERR_PTR(-ENOMEM);
474 474
475 mp->grp.name = "parameters"; 475 mp->grp.name = "parameters";
476 mp->grp.attrs = (void *)mp + size[0]; 476 mp->grp.attrs = (void *)mp + size[0];
477 477
478 pattr = &mp->attrs[0]; 478 pattr = &mp->attrs[0];
479 gattr = &mp->grp.attrs[0]; 479 gattr = &mp->grp.attrs[0];
480 for (i = 0; i < num_params; i++) { 480 for (i = 0; i < num_params; i++) {
481 struct kernel_param *kp = &kparam[i]; 481 struct kernel_param *kp = &kparam[i];
482 if (kp->perm) { 482 if (kp->perm) {
483 pattr->param = kp; 483 pattr->param = kp;
484 pattr->mattr.show = param_attr_show; 484 pattr->mattr.show = param_attr_show;
485 pattr->mattr.store = param_attr_store; 485 pattr->mattr.store = param_attr_store;
486 pattr->mattr.attr.name = (char *)&kp->name[name_skip]; 486 pattr->mattr.attr.name = (char *)&kp->name[name_skip];
487 pattr->mattr.attr.owner = mk->mod; 487 pattr->mattr.attr.owner = mk->mod;
488 pattr->mattr.attr.mode = kp->perm; 488 pattr->mattr.attr.mode = kp->perm;
489 *(gattr++) = &(pattr++)->mattr.attr; 489 *(gattr++) = &(pattr++)->mattr.attr;
490 } 490 }
491 } 491 }
492 *gattr = NULL; 492 *gattr = NULL;
493 493
494 if ((err = sysfs_create_group(&mk->kobj, &mp->grp))) { 494 if ((err = sysfs_create_group(&mk->kobj, &mp->grp))) {
495 kfree(mp); 495 kfree(mp);
496 return ERR_PTR(err); 496 return ERR_PTR(err);
497 } 497 }
498 return mp; 498 return mp;
499 } 499 }
500 500
501 501
502 #ifdef CONFIG_MODULES 502 #ifdef CONFIG_MODULES
503 503
504 /* 504 /*
505 * module_param_sysfs_setup - setup sysfs support for one module 505 * module_param_sysfs_setup - setup sysfs support for one module
506 * @mod: module 506 * @mod: module
507 * @kparam: module parameters (array) 507 * @kparam: module parameters (array)
508 * @num_params: number of module parameters 508 * @num_params: number of module parameters
509 * 509 *
510 * Adds sysfs entries for module parameters, and creates a link from 510 * Adds sysfs entries for module parameters, and creates a link from
511 * /sys/module/[mod->name]/parameters to /sys/parameters/[mod->name]/ 511 * /sys/module/[mod->name]/parameters to /sys/parameters/[mod->name]/
512 */ 512 */
513 int module_param_sysfs_setup(struct module *mod, 513 int module_param_sysfs_setup(struct module *mod,
514 struct kernel_param *kparam, 514 struct kernel_param *kparam,
515 unsigned int num_params) 515 unsigned int num_params)
516 { 516 {
517 struct module_param_attrs *mp; 517 struct module_param_attrs *mp;
518 518
519 mp = param_sysfs_setup(&mod->mkobj, kparam, num_params, 0); 519 mp = param_sysfs_setup(&mod->mkobj, kparam, num_params, 0);
520 if (IS_ERR(mp)) 520 if (IS_ERR(mp))
521 return PTR_ERR(mp); 521 return PTR_ERR(mp);
522 522
523 mod->param_attrs = mp; 523 mod->param_attrs = mp;
524 return 0; 524 return 0;
525 } 525 }
526 526
527 /* 527 /*
528 * module_param_sysfs_remove - remove sysfs support for one module 528 * module_param_sysfs_remove - remove sysfs support for one module
529 * @mod: module 529 * @mod: module
530 * 530 *
531 * Remove sysfs entries for module parameters and the corresponding 531 * Remove sysfs entries for module parameters and the corresponding
532 * kobject. 532 * kobject.
533 */ 533 */
534 void module_param_sysfs_remove(struct module *mod) 534 void module_param_sysfs_remove(struct module *mod)
535 { 535 {
536 if (mod->param_attrs) { 536 if (mod->param_attrs) {
537 sysfs_remove_group(&mod->mkobj.kobj, 537 sysfs_remove_group(&mod->mkobj.kobj,
538 &mod->param_attrs->grp); 538 &mod->param_attrs->grp);
539 /* We are positive that no one is using any param 539 /* We are positive that no one is using any param
540 * attrs at this point. Deallocate immediately. */ 540 * attrs at this point. Deallocate immediately. */
541 kfree(mod->param_attrs); 541 kfree(mod->param_attrs);
542 mod->param_attrs = NULL; 542 mod->param_attrs = NULL;
543 } 543 }
544 } 544 }
545 #endif 545 #endif
546 546
547 /* 547 /*
548 * kernel_param_sysfs_setup - wrapper for built-in params support 548 * kernel_param_sysfs_setup - wrapper for built-in params support
549 */ 549 */
550 static void __init kernel_param_sysfs_setup(const char *name, 550 static void __init kernel_param_sysfs_setup(const char *name,
551 struct kernel_param *kparam, 551 struct kernel_param *kparam,
552 unsigned int num_params, 552 unsigned int num_params,
553 unsigned int name_skip) 553 unsigned int name_skip)
554 { 554 {
555 struct module_kobject *mk; 555 struct module_kobject *mk;
556 int ret; 556 int ret;
557 557
558 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL); 558 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
559 BUG_ON(!mk); 559 BUG_ON(!mk);
560 560
561 mk->mod = THIS_MODULE; 561 mk->mod = THIS_MODULE;
562 kobj_set_kset_s(mk, module_subsys); 562 kobj_set_kset_s(mk, module_subsys);
563 kobject_set_name(&mk->kobj, name); 563 kobject_set_name(&mk->kobj, name);
564 ret = kobject_register(&mk->kobj); 564 kobject_init(&mk->kobj);
565 ret = kobject_add(&mk->kobj);
565 BUG_ON(ret < 0); 566 BUG_ON(ret < 0);
566 567 param_sysfs_setup(mk, kparam, num_params, name_skip);
567 /* no need to keep the kobject if no parameter is exported */ 568 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
568 if (!param_sysfs_setup(mk, kparam, num_params, name_skip)) { 569 kobject_uevent(&mk->kobj, KOBJ_ADD);
569 kobject_unregister(&mk->kobj);
570 kfree(mk);
571 }
572 } 570 }
573 571
574 /* 572 /*
575 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules 573 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
576 * 574 *
577 * Add module_parameters to sysfs for "modules" built into the kernel. 575 * Add module_parameters to sysfs for "modules" built into the kernel.
578 * 576 *
579 * The "module" name (KBUILD_MODNAME) is stored before a dot, the 577 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
580 * "parameter" name is stored behind a dot in kernel_param->name. So, 578 * "parameter" name is stored behind a dot in kernel_param->name. So,
581 * extract the "module" name for all built-in kernel_param-eters, 579 * extract the "module" name for all built-in kernel_param-eters,
582 * and for all who have the same, call kernel_param_sysfs_setup. 580 * and for all who have the same, call kernel_param_sysfs_setup.
583 */ 581 */
584 static void __init param_sysfs_builtin(void) 582 static void __init param_sysfs_builtin(void)
585 { 583 {
586 struct kernel_param *kp, *kp_begin = NULL; 584 struct kernel_param *kp, *kp_begin = NULL;
587 unsigned int i, name_len, count = 0; 585 unsigned int i, name_len, count = 0;
588 char modname[MAX_KBUILD_MODNAME + 1] = ""; 586 char modname[MAX_KBUILD_MODNAME + 1] = "";
589 587
590 for (i=0; i < __stop___param - __start___param; i++) { 588 for (i=0; i < __stop___param - __start___param; i++) {
591 char *dot; 589 char *dot;
592 590
593 kp = &__start___param[i]; 591 kp = &__start___param[i];
594 592
595 /* We do not handle args without periods. */ 593 /* We do not handle args without periods. */
596 dot = memchr(kp->name, '.', MAX_KBUILD_MODNAME); 594 dot = memchr(kp->name, '.', MAX_KBUILD_MODNAME);
597 if (!dot) { 595 if (!dot) {
598 DEBUGP("couldn't find period in %s\n", kp->name); 596 DEBUGP("couldn't find period in %s\n", kp->name);
599 continue; 597 continue;
600 } 598 }
601 name_len = dot - kp->name; 599 name_len = dot - kp->name;
602 600
603 /* new kbuild_modname? */ 601 /* new kbuild_modname? */
604 if (strlen(modname) != name_len 602 if (strlen(modname) != name_len
605 || strncmp(modname, kp->name, name_len) != 0) { 603 || strncmp(modname, kp->name, name_len) != 0) {
606 /* add a new kobject for previous kernel_params. */ 604 /* add a new kobject for previous kernel_params. */
607 if (count) 605 if (count)
608 kernel_param_sysfs_setup(modname, 606 kernel_param_sysfs_setup(modname,
609 kp_begin, 607 kp_begin,
610 count, 608 count,
611 strlen(modname)+1); 609 strlen(modname)+1);
612 610
613 strncpy(modname, kp->name, name_len); 611 strncpy(modname, kp->name, name_len);
614 modname[name_len] = '\0'; 612 modname[name_len] = '\0';
615 count = 0; 613 count = 0;
616 kp_begin = kp; 614 kp_begin = kp;
617 } 615 }
618 count++; 616 count++;
619 } 617 }
620 618
621 /* last kernel_params need to be registered as well */ 619 /* last kernel_params need to be registered as well */
622 if (count) 620 if (count)
623 kernel_param_sysfs_setup(modname, kp_begin, count, 621 kernel_param_sysfs_setup(modname, kp_begin, count,
624 strlen(modname)+1); 622 strlen(modname)+1);
625 } 623 }
626 624
627 625
628 /* module-related sysfs stuff */ 626 /* module-related sysfs stuff */
629 #ifdef CONFIG_SYSFS 627 #ifdef CONFIG_SYSFS
630 628
631 #define to_module_attr(n) container_of(n, struct module_attribute, attr); 629 #define to_module_attr(n) container_of(n, struct module_attribute, attr);
632 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj); 630 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj);
633 631
634 static ssize_t module_attr_show(struct kobject *kobj, 632 static ssize_t module_attr_show(struct kobject *kobj,
635 struct attribute *attr, 633 struct attribute *attr,
636 char *buf) 634 char *buf)
637 { 635 {
638 struct module_attribute *attribute; 636 struct module_attribute *attribute;
639 struct module_kobject *mk; 637 struct module_kobject *mk;
640 int ret; 638 int ret;
641 639
642 attribute = to_module_attr(attr); 640 attribute = to_module_attr(attr);
643 mk = to_module_kobject(kobj); 641 mk = to_module_kobject(kobj);
644 642
645 if (!attribute->show) 643 if (!attribute->show)
646 return -EIO; 644 return -EIO;
647 645
648 ret = attribute->show(attribute, mk->mod, buf); 646 ret = attribute->show(attribute, mk->mod, buf);
649 647
650 return ret; 648 return ret;
651 } 649 }
652 650
653 static ssize_t module_attr_store(struct kobject *kobj, 651 static ssize_t module_attr_store(struct kobject *kobj,
654 struct attribute *attr, 652 struct attribute *attr,
655 const char *buf, size_t len) 653 const char *buf, size_t len)
656 { 654 {
657 struct module_attribute *attribute; 655 struct module_attribute *attribute;
658 struct module_kobject *mk; 656 struct module_kobject *mk;
659 int ret; 657 int ret;
660 658
661 attribute = to_module_attr(attr); 659 attribute = to_module_attr(attr);
662 mk = to_module_kobject(kobj); 660 mk = to_module_kobject(kobj);
663 661
664 if (!attribute->store) 662 if (!attribute->store)
665 return -EIO; 663 return -EIO;
666 664
667 ret = attribute->store(attribute, mk->mod, buf, len); 665 ret = attribute->store(attribute, mk->mod, buf, len);
668 666
669 return ret; 667 return ret;
670 } 668 }
671 669
672 static struct sysfs_ops module_sysfs_ops = { 670 static struct sysfs_ops module_sysfs_ops = {
673 .show = module_attr_show, 671 .show = module_attr_show,
674 .store = module_attr_store, 672 .store = module_attr_store,
675 }; 673 };
676 674
677 #else 675 #else
678 static struct sysfs_ops module_sysfs_ops = { 676 static struct sysfs_ops module_sysfs_ops = {
679 .show = NULL, 677 .show = NULL,
680 .store = NULL, 678 .store = NULL,
681 }; 679 };
682 #endif 680 #endif
683 681
684 static struct kobj_type module_ktype = { 682 static struct kobj_type module_ktype = {
685 .sysfs_ops = &module_sysfs_ops, 683 .sysfs_ops = &module_sysfs_ops,
686 }; 684 };
687 685
688 decl_subsys(module, &module_ktype, NULL); 686 decl_subsys(module, &module_ktype, NULL);
689 687
690 /* 688 /*
691 * param_sysfs_init - wrapper for built-in params support 689 * param_sysfs_init - wrapper for built-in params support
692 */ 690 */
693 static int __init param_sysfs_init(void) 691 static int __init param_sysfs_init(void)
694 { 692 {
695 int ret; 693 int ret;
696 694
697 ret = subsystem_register(&module_subsys); 695 ret = subsystem_register(&module_subsys);
698 if (ret < 0) { 696 if (ret < 0) {
699 printk(KERN_WARNING "%s (%d): subsystem_register error: %d\n", 697 printk(KERN_WARNING "%s (%d): subsystem_register error: %d\n",
700 __FILE__, __LINE__, ret); 698 __FILE__, __LINE__, ret);
701 return ret; 699 return ret;
702 } 700 }
703 701
704 param_sysfs_builtin(); 702 param_sysfs_builtin();
705 703
706 return 0; 704 return 0;
707 } 705 }
708 subsys_initcall(param_sysfs_init); 706 subsys_initcall(param_sysfs_init);
709 707
710 EXPORT_SYMBOL(param_set_byte); 708 EXPORT_SYMBOL(param_set_byte);
711 EXPORT_SYMBOL(param_get_byte); 709 EXPORT_SYMBOL(param_get_byte);
712 EXPORT_SYMBOL(param_set_short); 710 EXPORT_SYMBOL(param_set_short);
713 EXPORT_SYMBOL(param_get_short); 711 EXPORT_SYMBOL(param_get_short);
714 EXPORT_SYMBOL(param_set_ushort); 712 EXPORT_SYMBOL(param_set_ushort);
715 EXPORT_SYMBOL(param_get_ushort); 713 EXPORT_SYMBOL(param_get_ushort);
716 EXPORT_SYMBOL(param_set_int); 714 EXPORT_SYMBOL(param_set_int);
717 EXPORT_SYMBOL(param_get_int); 715 EXPORT_SYMBOL(param_get_int);
718 EXPORT_SYMBOL(param_set_uint); 716 EXPORT_SYMBOL(param_set_uint);
719 EXPORT_SYMBOL(param_get_uint); 717 EXPORT_SYMBOL(param_get_uint);
720 EXPORT_SYMBOL(param_set_long); 718 EXPORT_SYMBOL(param_set_long);
721 EXPORT_SYMBOL(param_get_long); 719 EXPORT_SYMBOL(param_get_long);
722 EXPORT_SYMBOL(param_set_ulong); 720 EXPORT_SYMBOL(param_set_ulong);
723 EXPORT_SYMBOL(param_get_ulong); 721 EXPORT_SYMBOL(param_get_ulong);
724 EXPORT_SYMBOL(param_set_charp); 722 EXPORT_SYMBOL(param_set_charp);
725 EXPORT_SYMBOL(param_get_charp); 723 EXPORT_SYMBOL(param_get_charp);
726 EXPORT_SYMBOL(param_set_bool); 724 EXPORT_SYMBOL(param_set_bool);
727 EXPORT_SYMBOL(param_get_bool); 725 EXPORT_SYMBOL(param_get_bool);
728 EXPORT_SYMBOL(param_set_invbool); 726 EXPORT_SYMBOL(param_set_invbool);
729 EXPORT_SYMBOL(param_get_invbool); 727 EXPORT_SYMBOL(param_get_invbool);
730 EXPORT_SYMBOL(param_array_set); 728 EXPORT_SYMBOL(param_array_set);
731 EXPORT_SYMBOL(param_array_get); 729 EXPORT_SYMBOL(param_array_get);
732 EXPORT_SYMBOL(param_set_copystring); 730 EXPORT_SYMBOL(param_set_copystring);
733 EXPORT_SYMBOL(param_get_string); 731 EXPORT_SYMBOL(param_get_string);