moduleparam.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #ifndef _LINUX_MODULE_PARAMS_H
  2. #define _LINUX_MODULE_PARAMS_H
  3. /* (C) Copyright 2001, 2002 Rusty Russell IBM Corporation */
  4. #include <linux/init.h>
  5. #include <linux/stringify.h>
  6. #include <linux/kernel.h>
  7. /* You can override this manually, but generally this should match the
  8. module name. */
  9. #ifdef MODULE
  10. #define MODULE_PARAM_PREFIX /* empty */
  11. #else
  12. #define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
  13. #endif
  14. /* Chosen so that structs with an unsigned long line up. */
  15. #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
  16. #ifdef MODULE
  17. #define __MODULE_INFO(tag, name, info) \
  18. static const char __UNIQUE_ID(name)[] \
  19. __used __attribute__((section(".modinfo"), unused, aligned(1))) \
  20. = __stringify(tag) "=" info
  21. #else /* !MODULE */
  22. /* This struct is here for syntactic coherency, it is not used */
  23. #define __MODULE_INFO(tag, name, info) \
  24. struct __UNIQUE_ID(name) {}
  25. #endif
  26. #define __MODULE_PARM_TYPE(name, _type) \
  27. __MODULE_INFO(parmtype, name##type, #name ":" _type)
  28. /* One for each parameter, describing how to use it. Some files do
  29. multiple of these per line, so can't just use MODULE_INFO. */
  30. #define MODULE_PARM_DESC(_parm, desc) \
  31. __MODULE_INFO(parm, _parm, #_parm ":" desc)
  32. struct kernel_param;
  33. /*
  34. * Flags available for kernel_param_ops
  35. *
  36. * NOARG - the parameter allows for no argument (foo instead of foo=1)
  37. */
  38. enum {
  39. KERNEL_PARAM_OPS_FL_NOARG = (1 << 0)
  40. };
  41. struct kernel_param_ops {
  42. /* How the ops should behave */
  43. unsigned int flags;
  44. /* Returns 0, or -errno. arg is in kp->arg. */
  45. int (*set)(const char *val, const struct kernel_param *kp);
  46. /* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  47. int (*get)(char *buffer, const struct kernel_param *kp);
  48. /* Optional function to free kp->arg when module unloaded. */
  49. void (*free)(void *arg);
  50. };
  51. /*
  52. * Flags available for kernel_param
  53. *
  54. * UNSAFE - the parameter is dangerous and setting it will taint the kernel
  55. */
  56. enum {
  57. KERNEL_PARAM_FL_UNSAFE = (1 << 0)
  58. };
  59. struct kernel_param {
  60. const char *name;
  61. const struct kernel_param_ops *ops;
  62. u16 perm;
  63. s8 level;
  64. u8 flags;
  65. union {
  66. void *arg;
  67. const struct kparam_string *str;
  68. const struct kparam_array *arr;
  69. };
  70. };
  71. /* Special one for strings we want to copy into */
  72. struct kparam_string {
  73. unsigned int maxlen;
  74. char *string;
  75. };
  76. /* Special one for arrays */
  77. struct kparam_array
  78. {
  79. unsigned int max;
  80. unsigned int elemsize;
  81. unsigned int *num;
  82. const struct kernel_param_ops *ops;
  83. void *elem;
  84. };
  85. /**
  86. * module_param - typesafe helper for a module/cmdline parameter
  87. * @value: the variable to alter, and exposed parameter name.
  88. * @type: the type of the parameter
  89. * @perm: visibility in sysfs.
  90. *
  91. * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
  92. * ".") the kernel commandline parameter. Note that - is changed to _, so
  93. * the user can use "foo-bar=1" even for variable "foo_bar".
  94. *
  95. * @perm is 0 if the the variable is not to appear in sysfs, or 0444
  96. * for world-readable, 0644 for root-writable, etc. Note that if it
  97. * is writable, you may need to use kparam_block_sysfs_write() around
  98. * accesses (esp. charp, which can be kfreed when it changes).
  99. *
  100. * The @type is simply pasted to refer to a param_ops_##type and a
  101. * param_check_##type: for convenience many standard types are provided but
  102. * you can create your own by defining those variables.
  103. *
  104. * Standard types are:
  105. * byte, short, ushort, int, uint, long, ulong
  106. * charp: a character pointer
  107. * bool: a bool, values 0/1, y/n, Y/N.
  108. * invbool: the above, only sense-reversed (N = true).
  109. */
  110. #define module_param(name, type, perm) \
  111. module_param_named(name, name, type, perm)
  112. /**
  113. * module_param_named - typesafe helper for a renamed module/cmdline parameter
  114. * @name: a valid C identifier which is the parameter name.
  115. * @value: the actual lvalue to alter.
  116. * @type: the type of the parameter
  117. * @perm: visibility in sysfs.
  118. *
  119. * Usually it's a good idea to have variable names and user-exposed names the
  120. * same, but that's harder if the variable must be non-static or is inside a
  121. * structure. This allows exposure under a different name.
  122. */
  123. #define module_param_named(name, value, type, perm) \
  124. param_check_##type(name, &(value)); \
  125. module_param_cb(name, &param_ops_##type, &value, perm); \
  126. __MODULE_PARM_TYPE(name, #type)
  127. /**
  128. * module_param_cb - general callback for a module/cmdline parameter
  129. * @name: a valid C identifier which is the parameter name.
  130. * @ops: the set & get operations for this parameter.
  131. * @perm: visibility in sysfs.
  132. *
  133. * The ops can have NULL set or get functions.
  134. */
  135. #define module_param_cb(name, ops, arg, perm) \
  136. __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1, 0)
  137. /**
  138. * <level>_param_cb - general callback for a module/cmdline parameter
  139. * to be evaluated before certain initcall level
  140. * @name: a valid C identifier which is the parameter name.
  141. * @ops: the set & get operations for this parameter.
  142. * @perm: visibility in sysfs.
  143. *
  144. * The ops can have NULL set or get functions.
  145. */
  146. #define __level_param_cb(name, ops, arg, perm, level) \
  147. __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, level, 0)
  148. #define core_param_cb(name, ops, arg, perm) \
  149. __level_param_cb(name, ops, arg, perm, 1)
  150. #define postcore_param_cb(name, ops, arg, perm) \
  151. __level_param_cb(name, ops, arg, perm, 2)
  152. #define arch_param_cb(name, ops, arg, perm) \
  153. __level_param_cb(name, ops, arg, perm, 3)
  154. #define subsys_param_cb(name, ops, arg, perm) \
  155. __level_param_cb(name, ops, arg, perm, 4)
  156. #define fs_param_cb(name, ops, arg, perm) \
  157. __level_param_cb(name, ops, arg, perm, 5)
  158. #define device_param_cb(name, ops, arg, perm) \
  159. __level_param_cb(name, ops, arg, perm, 6)
  160. #define late_param_cb(name, ops, arg, perm) \
  161. __level_param_cb(name, ops, arg, perm, 7)
  162. /* On alpha, ia64 and ppc64 relocations to global data cannot go into
  163. read-only sections (which is part of respective UNIX ABI on these
  164. platforms). So 'const' makes no sense and even causes compile failures
  165. with some compilers. */
  166. #if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64)
  167. #define __moduleparam_const
  168. #else
  169. #define __moduleparam_const const
  170. #endif
  171. /* This is the fundamental function for registering boot/module
  172. parameters. */
  173. #define __module_param_call(prefix, name, ops, arg, perm, level, flags) \
  174. /* Default value instead of permissions? */ \
  175. static const char __param_str_##name[] = prefix #name; \
  176. static struct kernel_param __moduleparam_const __param_##name \
  177. __used \
  178. __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
  179. = { __param_str_##name, ops, VERIFY_OCTAL_PERMISSIONS(perm), \
  180. level, flags, { arg } }
  181. /* Obsolete - use module_param_cb() */
  182. #define module_param_call(name, set, get, arg, perm) \
  183. static struct kernel_param_ops __param_ops_##name = \
  184. { 0, (void *)set, (void *)get }; \
  185. __module_param_call(MODULE_PARAM_PREFIX, \
  186. name, &__param_ops_##name, arg, \
  187. (perm) + sizeof(__check_old_set_param(set))*0, -1, 0)
  188. /* We don't get oldget: it's often a new-style param_get_uint, etc. */
  189. static inline int
  190. __check_old_set_param(int (*oldset)(const char *, struct kernel_param *))
  191. {
  192. return 0;
  193. }
  194. /**
  195. * kparam_block_sysfs_write - make sure a parameter isn't written via sysfs.
  196. * @name: the name of the parameter
  197. *
  198. * There's no point blocking write on a paramter that isn't writable via sysfs!
  199. */
  200. #define kparam_block_sysfs_write(name) \
  201. do { \
  202. BUG_ON(!(__param_##name.perm & 0222)); \
  203. __kernel_param_lock(); \
  204. } while (0)
  205. /**
  206. * kparam_unblock_sysfs_write - allows sysfs to write to a parameter again.
  207. * @name: the name of the parameter
  208. */
  209. #define kparam_unblock_sysfs_write(name) \
  210. do { \
  211. BUG_ON(!(__param_##name.perm & 0222)); \
  212. __kernel_param_unlock(); \
  213. } while (0)
  214. /**
  215. * kparam_block_sysfs_read - make sure a parameter isn't read via sysfs.
  216. * @name: the name of the parameter
  217. *
  218. * This also blocks sysfs writes.
  219. */
  220. #define kparam_block_sysfs_read(name) \
  221. do { \
  222. BUG_ON(!(__param_##name.perm & 0444)); \
  223. __kernel_param_lock(); \
  224. } while (0)
  225. /**
  226. * kparam_unblock_sysfs_read - allows sysfs to read a parameter again.
  227. * @name: the name of the parameter
  228. */
  229. #define kparam_unblock_sysfs_read(name) \
  230. do { \
  231. BUG_ON(!(__param_##name.perm & 0444)); \
  232. __kernel_param_unlock(); \
  233. } while (0)
  234. #ifdef CONFIG_SYSFS
  235. extern void __kernel_param_lock(void);
  236. extern void __kernel_param_unlock(void);
  237. #else
  238. static inline void __kernel_param_lock(void)
  239. {
  240. }
  241. static inline void __kernel_param_unlock(void)
  242. {
  243. }
  244. #endif
  245. #ifndef MODULE
  246. /**
  247. * core_param - define a historical core kernel parameter.
  248. * @name: the name of the cmdline and sysfs parameter (often the same as var)
  249. * @var: the variable
  250. * @type: the type of the parameter
  251. * @perm: visibility in sysfs
  252. *
  253. * core_param is just like module_param(), but cannot be modular and
  254. * doesn't add a prefix (such as "printk."). This is for compatibility
  255. * with __setup(), and it makes sense as truly core parameters aren't
  256. * tied to the particular file they're in.
  257. */
  258. #define core_param(name, var, type, perm) \
  259. param_check_##type(name, &(var)); \
  260. __module_param_call("", name, &param_ops_##type, &var, perm, -1, 0)
  261. #endif /* !MODULE */
  262. /**
  263. * module_param_string - a char array parameter
  264. * @name: the name of the parameter
  265. * @string: the string variable
  266. * @len: the maximum length of the string, incl. terminator
  267. * @perm: visibility in sysfs.
  268. *
  269. * This actually copies the string when it's set (unlike type charp).
  270. * @len is usually just sizeof(string).
  271. */
  272. #define module_param_string(name, string, len, perm) \
  273. static const struct kparam_string __param_string_##name \
  274. = { len, string }; \
  275. __module_param_call(MODULE_PARAM_PREFIX, name, \
  276. &param_ops_string, \
  277. .str = &__param_string_##name, perm, -1, 0);\
  278. __MODULE_PARM_TYPE(name, "string")
  279. /**
  280. * parameq - checks if two parameter names match
  281. * @name1: parameter name 1
  282. * @name2: parameter name 2
  283. *
  284. * Returns true if the two parameter names are equal.
  285. * Dashes (-) are considered equal to underscores (_).
  286. */
  287. extern bool parameq(const char *name1, const char *name2);
  288. /**
  289. * parameqn - checks if two parameter names match
  290. * @name1: parameter name 1
  291. * @name2: parameter name 2
  292. * @n: the length to compare
  293. *
  294. * Similar to parameq(), except it compares @n characters.
  295. */
  296. extern bool parameqn(const char *name1, const char *name2, size_t n);
  297. /* Called on module insert or kernel boot */
  298. extern char *parse_args(const char *name,
  299. char *args,
  300. const struct kernel_param *params,
  301. unsigned num,
  302. s16 level_min,
  303. s16 level_max,
  304. int (*unknown)(char *param, char *val,
  305. const char *doing));
  306. /* Called by module remove. */
  307. #ifdef CONFIG_SYSFS
  308. extern void destroy_params(const struct kernel_param *params, unsigned num);
  309. #else
  310. static inline void destroy_params(const struct kernel_param *params,
  311. unsigned num)
  312. {
  313. }
  314. #endif /* !CONFIG_SYSFS */
  315. /* All the helper functions */
  316. /* The macros to do compile-time type checking stolen from Jakub
  317. Jelinek, who IIRC came up with this idea for the 2.4 module init code. */
  318. #define __param_check(name, p, type) \
  319. static inline type __always_unused *__check_##name(void) { return(p); }
  320. /**
  321. * param_check_unsafe - Warn and taint the kernel if setting dangerous options.
  322. *
  323. * This gets called from all the standard param setters, but can be used from
  324. * custom setters as well.
  325. */
  326. static inline void
  327. param_check_unsafe(const struct kernel_param *kp)
  328. {
  329. if (kp->flags & KERNEL_PARAM_FL_UNSAFE) {
  330. pr_warn("Setting dangerous option %s - tainting kernel\n",
  331. kp->name);
  332. add_taint(TAINT_USER, LOCKDEP_STILL_OK);
  333. }
  334. }
  335. extern struct kernel_param_ops param_ops_byte;
  336. extern int param_set_byte(const char *val, const struct kernel_param *kp);
  337. extern int param_get_byte(char *buffer, const struct kernel_param *kp);
  338. #define param_check_byte(name, p) __param_check(name, p, unsigned char)
  339. extern struct kernel_param_ops param_ops_short;
  340. extern int param_set_short(const char *val, const struct kernel_param *kp);
  341. extern int param_get_short(char *buffer, const struct kernel_param *kp);
  342. #define param_check_short(name, p) __param_check(name, p, short)
  343. extern struct kernel_param_ops param_ops_ushort;
  344. extern int param_set_ushort(const char *val, const struct kernel_param *kp);
  345. extern int param_get_ushort(char *buffer, const struct kernel_param *kp);
  346. #define param_check_ushort(name, p) __param_check(name, p, unsigned short)
  347. extern struct kernel_param_ops param_ops_int;
  348. extern int param_set_int(const char *val, const struct kernel_param *kp);
  349. extern int param_get_int(char *buffer, const struct kernel_param *kp);
  350. #define param_check_int(name, p) __param_check(name, p, int)
  351. extern struct kernel_param_ops param_ops_uint;
  352. extern int param_set_uint(const char *val, const struct kernel_param *kp);
  353. extern int param_get_uint(char *buffer, const struct kernel_param *kp);
  354. #define param_check_uint(name, p) __param_check(name, p, unsigned int)
  355. extern struct kernel_param_ops param_ops_long;
  356. extern int param_set_long(const char *val, const struct kernel_param *kp);
  357. extern int param_get_long(char *buffer, const struct kernel_param *kp);
  358. #define param_check_long(name, p) __param_check(name, p, long)
  359. extern struct kernel_param_ops param_ops_ulong;
  360. extern int param_set_ulong(const char *val, const struct kernel_param *kp);
  361. extern int param_get_ulong(char *buffer, const struct kernel_param *kp);
  362. #define param_check_ulong(name, p) __param_check(name, p, unsigned long)
  363. extern struct kernel_param_ops param_ops_ullong;
  364. extern int param_set_ullong(const char *val, const struct kernel_param *kp);
  365. extern int param_get_ullong(char *buffer, const struct kernel_param *kp);
  366. #define param_check_ullong(name, p) __param_check(name, p, unsigned long long)
  367. extern struct kernel_param_ops param_ops_charp;
  368. extern int param_set_charp(const char *val, const struct kernel_param *kp);
  369. extern int param_get_charp(char *buffer, const struct kernel_param *kp);
  370. #define param_check_charp(name, p) __param_check(name, p, char *)
  371. /* We used to allow int as well as bool. We're taking that away! */
  372. extern struct kernel_param_ops param_ops_bool;
  373. extern int param_set_bool(const char *val, const struct kernel_param *kp);
  374. extern int param_get_bool(char *buffer, const struct kernel_param *kp);
  375. #define param_check_bool(name, p) __param_check(name, p, bool)
  376. extern struct kernel_param_ops param_ops_invbool;
  377. extern int param_set_invbool(const char *val, const struct kernel_param *kp);
  378. extern int param_get_invbool(char *buffer, const struct kernel_param *kp);
  379. #define param_check_invbool(name, p) __param_check(name, p, bool)
  380. /* An int, which can only be set like a bool (though it shows as an int). */
  381. extern struct kernel_param_ops param_ops_bint;
  382. extern int param_set_bint(const char *val, const struct kernel_param *kp);
  383. #define param_get_bint param_get_int
  384. #define param_check_bint param_check_int
  385. /**
  386. * module_param_array - a parameter which is an array of some type
  387. * @name: the name of the array variable
  388. * @type: the type, as per module_param()
  389. * @nump: optional pointer filled in with the number written
  390. * @perm: visibility in sysfs
  391. *
  392. * Input and output are as comma-separated values. Commas inside values
  393. * don't work properly (eg. an array of charp).
  394. *
  395. * ARRAY_SIZE(@name) is used to determine the number of elements in the
  396. * array, so the definition must be visible.
  397. */
  398. #define module_param_array(name, type, nump, perm) \
  399. module_param_array_named(name, name, type, nump, perm)
  400. /**
  401. * module_param_array_named - renamed parameter which is an array of some type
  402. * @name: a valid C identifier which is the parameter name
  403. * @array: the name of the array variable
  404. * @type: the type, as per module_param()
  405. * @nump: optional pointer filled in with the number written
  406. * @perm: visibility in sysfs
  407. *
  408. * This exposes a different name than the actual variable name. See
  409. * module_param_named() for why this might be necessary.
  410. */
  411. #define module_param_array_named(name, array, type, nump, perm) \
  412. param_check_##type(name, &(array)[0]); \
  413. static const struct kparam_array __param_arr_##name \
  414. = { .max = ARRAY_SIZE(array), .num = nump, \
  415. .ops = &param_ops_##type, \
  416. .elemsize = sizeof(array[0]), .elem = array }; \
  417. __module_param_call(MODULE_PARAM_PREFIX, name, \
  418. &param_array_ops, \
  419. .arr = &__param_arr_##name, \
  420. perm, -1, 0); \
  421. __MODULE_PARM_TYPE(name, "array of " #type)
  422. extern struct kernel_param_ops param_array_ops;
  423. extern struct kernel_param_ops param_ops_string;
  424. extern int param_set_copystring(const char *val, const struct kernel_param *);
  425. extern int param_get_string(char *buffer, const struct kernel_param *kp);
  426. /* for exporting parameters in /sys/module/.../parameters */
  427. struct module;
  428. #if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
  429. extern int module_param_sysfs_setup(struct module *mod,
  430. const struct kernel_param *kparam,
  431. unsigned int num_params);
  432. extern void module_param_sysfs_remove(struct module *mod);
  433. #else
  434. static inline int module_param_sysfs_setup(struct module *mod,
  435. const struct kernel_param *kparam,
  436. unsigned int num_params)
  437. {
  438. return 0;
  439. }
  440. static inline void module_param_sysfs_remove(struct module *mod)
  441. { }
  442. #endif
  443. #endif /* _LINUX_MODULE_PARAMS_H */