Skip to content

Commit fddd520

Browse files
committed
module_param: allow 'bool' module_params to be bool, not just int.
Impact: API cleanup For historical reasons, 'bool' parameters must be an int, not a bool. But there are around 600 users, so a conversion seems like useless churn. So we use __same_type() to distinguish, and handle both cases. Signed-off-by: Rusty Russell <[email protected]>
1 parent d2c123c commit fddd520

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

include/linux/moduleparam.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef int (*param_get_fn)(char *buffer, struct kernel_param *kp);
3838

3939
/* Flag bits for kernel_param.flags */
4040
#define KPARAM_KMALLOCED 1
41+
#define KPARAM_ISBOOL 2
4142

4243
struct kernel_param {
4344
const char *name;
@@ -83,7 +84,7 @@ struct kparam_array
8384
parameters. perm sets the visibility in sysfs: 000 means it's
8485
not there, read bits mean it's readable, write bits mean it's
8586
writable. */
86-
#define __module_param_call(prefix, name, set, get, arg, perm) \
87+
#define __module_param_call(prefix, name, set, get, arg, isbool, perm) \
8788
/* Default value instead of permissions? */ \
8889
static int __param_perm_check_##name __attribute__((unused)) = \
8990
BUILD_BUG_ON_ZERO((perm) < 0 || (perm) > 0777 || ((perm) & 2)) \
@@ -92,10 +93,13 @@ struct kparam_array
9293
static struct kernel_param __moduleparam_const __param_##name \
9394
__used \
9495
__attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
95-
= { __param_str_##name, perm, 0, set, get, { arg } }
96+
= { __param_str_##name, perm, isbool ? KPARAM_ISBOOL : 0, \
97+
set, get, { arg } }
9698

9799
#define module_param_call(name, set, get, arg, perm) \
98-
__module_param_call(MODULE_PARAM_PREFIX, name, set, get, arg, perm)
100+
__module_param_call(MODULE_PARAM_PREFIX, \
101+
name, set, get, arg, \
102+
__same_type(*(arg), bool), perm)
99103

100104
/* Helper functions: type is byte, short, ushort, int, uint, long,
101105
ulong, charp, bool or invbool, or XXX if you define param_get_XXX,
@@ -124,15 +128,16 @@ struct kparam_array
124128
#define core_param(name, var, type, perm) \
125129
param_check_##type(name, &(var)); \
126130
__module_param_call("", name, param_set_##type, param_get_##type, \
127-
&var, perm)
131+
&var, __same_type(var, bool), perm)
128132
#endif /* !MODULE */
129133

130134
/* Actually copy string: maxlen param is usually sizeof(string). */
131135
#define module_param_string(name, string, len, perm) \
132136
static const struct kparam_string __param_string_##name \
133137
= { len, string }; \
134-
module_param_call(name, param_set_copystring, param_get_string, \
135-
.str = &__param_string_##name, perm); \
138+
__module_param_call(MODULE_PARAM_PREFIX, name, \
139+
param_set_copystring, param_get_string, \
140+
.str = &__param_string_##name, 0, perm); \
136141
__MODULE_PARM_TYPE(name, "string")
137142

138143
/* Called on module insert or kernel boot */
@@ -190,9 +195,16 @@ extern int param_set_charp(const char *val, struct kernel_param *kp);
190195
extern int param_get_charp(char *buffer, struct kernel_param *kp);
191196
#define param_check_charp(name, p) __param_check(name, p, char *)
192197

198+
/* For historical reasons "bool" parameters can be (unsigned) "int". */
193199
extern int param_set_bool(const char *val, struct kernel_param *kp);
194200
extern int param_get_bool(char *buffer, struct kernel_param *kp);
195-
#define param_check_bool(name, p) __param_check(name, p, int)
201+
#define param_check_bool(name, p) \
202+
static inline void __check_##name(void) \
203+
{ \
204+
BUILD_BUG_ON(!__same_type(*(p), bool) && \
205+
!__same_type(*(p), unsigned int) && \
206+
!__same_type(*(p), int)); \
207+
}
196208

197209
extern int param_set_invbool(const char *val, struct kernel_param *kp);
198210
extern int param_get_invbool(char *buffer, struct kernel_param *kp);
@@ -203,8 +215,10 @@ extern int param_get_invbool(char *buffer, struct kernel_param *kp);
203215
static const struct kparam_array __param_arr_##name \
204216
= { ARRAY_SIZE(array), nump, param_set_##type, param_get_##type,\
205217
sizeof(array[0]), array }; \
206-
module_param_call(name, param_array_set, param_array_get, \
207-
.arr = &__param_arr_##name, perm); \
218+
__module_param_call(MODULE_PARAM_PREFIX, name, \
219+
param_array_set, param_array_get, \
220+
.arr = &__param_arr_##name, \
221+
__same_type(array[0], bool), perm); \
208222
__MODULE_PARM_TYPE(name, "array of " #type)
209223

210224
#define module_param_array(name, type, nump, perm) \

kernel/params.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,35 +238,54 @@ int param_get_charp(char *buffer, struct kernel_param *kp)
238238
return sprintf(buffer, "%s", *((char **)kp->arg));
239239
}
240240

241+
/* Actually could be a bool or an int, for historical reasons. */
241242
int param_set_bool(const char *val, struct kernel_param *kp)
242243
{
244+
bool v;
245+
243246
/* No equals means "set"... */
244247
if (!val) val = "1";
245248

246249
/* One of =[yYnN01] */
247250
switch (val[0]) {
248251
case 'y': case 'Y': case '1':
249-
*(int *)kp->arg = 1;
250-
return 0;
252+
v = true;
253+
break;
251254
case 'n': case 'N': case '0':
252-
*(int *)kp->arg = 0;
253-
return 0;
255+
v = false;
256+
break;
257+
default:
258+
return -EINVAL;
254259
}
255-
return -EINVAL;
260+
261+
if (kp->flags & KPARAM_ISBOOL)
262+
*(bool *)kp->arg = v;
263+
else
264+
*(int *)kp->arg = v;
265+
return 0;
256266
}
257267

258268
int param_get_bool(char *buffer, struct kernel_param *kp)
259269
{
270+
bool val;
271+
if (kp->flags & KPARAM_ISBOOL)
272+
val = *(bool *)kp->arg;
273+
else
274+
val = *(int *)kp->arg;
275+
260276
/* Y and N chosen as being relatively non-coder friendly */
261-
return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'Y' : 'N');
277+
return sprintf(buffer, "%c", val ? 'Y' : 'N');
262278
}
263279

280+
/* This one must be bool. */
264281
int param_set_invbool(const char *val, struct kernel_param *kp)
265282
{
266-
int boolval, ret;
283+
int ret;
284+
bool boolval;
267285
struct kernel_param dummy;
268286

269287
dummy.arg = &boolval;
288+
dummy.flags = KPARAM_ISBOOL;
270289
ret = param_set_bool(val, &dummy);
271290
if (ret == 0)
272291
*(bool *)kp->arg = !boolval;

0 commit comments

Comments
 (0)