Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kernel.h: split out kstrtox() and simple_strtox() to a separate header

kernel.h is being used as a dump for all kinds of stuff for a long time.
Here is the attempt to start cleaning it up by splitting out kstrtox() and
simple_strtox() helpers.

At the same time convert users in header and lib folders to use new
header. Though for time being include new header back to kernel.h to
avoid twisted indirected includes for existing users.

[andy.shevchenko@gmail.com: fix documentation references]
Link: https://lkml.kernel.org/r/20210615220003.377901-1-andy.shevchenko@gmail.com

Link: https://lkml.kernel.org/r/20210611185815.44103-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Francis Laniel <laniel_francis@privacyrequired.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Kars Mulder <kerneldev@karsmulder.nl>
Cc: Trond Myklebust <trond.myklebust@hammerspace.com>
Cc: Anna Schumaker <anna.schumaker@netapp.com>
Cc: "J. Bruce Fields" <bfields@fieldses.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Andy Shevchenko and committed by
Linus Torvalds
4c527293 ce71efd0

+163 -156
+2 -5
Documentation/core-api/kernel-api.rst
··· 24 24 .. kernel-doc:: lib/vsprintf.c 25 25 :export: 26 26 27 - .. kernel-doc:: include/linux/kernel.h 28 - :functions: kstrtol 29 - 30 - .. kernel-doc:: include/linux/kernel.h 31 - :functions: kstrtoul 27 + .. kernel-doc:: include/linux/kstrtox.h 28 + :functions: kstrtol kstrtoul 32 29 33 30 .. kernel-doc:: lib/kstrtox.c 34 31 :export:
+1 -142
include/linux/kernel.h
··· 10 10 #include <linux/types.h> 11 11 #include <linux/compiler.h> 12 12 #include <linux/bitops.h> 13 + #include <linux/kstrtox.h> 13 14 #include <linux/log2.h> 14 15 #include <linux/math.h> 15 16 #include <linux/minmax.h> ··· 180 179 181 180 void do_exit(long error_code) __noreturn; 182 181 void complete_and_exit(struct completion *, long) __noreturn; 183 - 184 - /* Internal, do not use. */ 185 - int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); 186 - int __must_check _kstrtol(const char *s, unsigned int base, long *res); 187 - 188 - int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); 189 - int __must_check kstrtoll(const char *s, unsigned int base, long long *res); 190 - 191 - /** 192 - * kstrtoul - convert a string to an unsigned long 193 - * @s: The start of the string. The string must be null-terminated, and may also 194 - * include a single newline before its terminating null. The first character 195 - * may also be a plus sign, but not a minus sign. 196 - * @base: The number base to use. The maximum supported base is 16. If base is 197 - * given as 0, then the base of the string is automatically detected with the 198 - * conventional semantics - If it begins with 0x the number will be parsed as a 199 - * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 200 - * parsed as an octal number. Otherwise it will be parsed as a decimal. 201 - * @res: Where to write the result of the conversion on success. 202 - * 203 - * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 204 - * Preferred over simple_strtoul(). Return code must be checked. 205 - */ 206 - static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) 207 - { 208 - /* 209 - * We want to shortcut function call, but 210 - * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. 211 - */ 212 - if (sizeof(unsigned long) == sizeof(unsigned long long) && 213 - __alignof__(unsigned long) == __alignof__(unsigned long long)) 214 - return kstrtoull(s, base, (unsigned long long *)res); 215 - else 216 - return _kstrtoul(s, base, res); 217 - } 218 - 219 - /** 220 - * kstrtol - convert a string to a long 221 - * @s: The start of the string. The string must be null-terminated, and may also 222 - * include a single newline before its terminating null. The first character 223 - * may also be a plus sign or a minus sign. 224 - * @base: The number base to use. The maximum supported base is 16. If base is 225 - * given as 0, then the base of the string is automatically detected with the 226 - * conventional semantics - If it begins with 0x the number will be parsed as a 227 - * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 228 - * parsed as an octal number. Otherwise it will be parsed as a decimal. 229 - * @res: Where to write the result of the conversion on success. 230 - * 231 - * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 232 - * Preferred over simple_strtol(). Return code must be checked. 233 - */ 234 - static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) 235 - { 236 - /* 237 - * We want to shortcut function call, but 238 - * __builtin_types_compatible_p(long, long long) = 0. 239 - */ 240 - if (sizeof(long) == sizeof(long long) && 241 - __alignof__(long) == __alignof__(long long)) 242 - return kstrtoll(s, base, (long long *)res); 243 - else 244 - return _kstrtol(s, base, res); 245 - } 246 - 247 - int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); 248 - int __must_check kstrtoint(const char *s, unsigned int base, int *res); 249 - 250 - static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) 251 - { 252 - return kstrtoull(s, base, res); 253 - } 254 - 255 - static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) 256 - { 257 - return kstrtoll(s, base, res); 258 - } 259 - 260 - static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) 261 - { 262 - return kstrtouint(s, base, res); 263 - } 264 - 265 - static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) 266 - { 267 - return kstrtoint(s, base, res); 268 - } 269 - 270 - int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); 271 - int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); 272 - int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); 273 - int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); 274 - int __must_check kstrtobool(const char *s, bool *res); 275 - 276 - int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); 277 - int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); 278 - int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res); 279 - int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res); 280 - int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res); 281 - int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res); 282 - int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res); 283 - int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); 284 - int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); 285 - int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); 286 - int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res); 287 - 288 - static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) 289 - { 290 - return kstrtoull_from_user(s, count, base, res); 291 - } 292 - 293 - static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res) 294 - { 295 - return kstrtoll_from_user(s, count, base, res); 296 - } 297 - 298 - static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res) 299 - { 300 - return kstrtouint_from_user(s, count, base, res); 301 - } 302 - 303 - static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res) 304 - { 305 - return kstrtoint_from_user(s, count, base, res); 306 - } 307 - 308 - /* 309 - * Use kstrto<foo> instead. 310 - * 311 - * NOTE: simple_strto<foo> does not check for the range overflow and, 312 - * depending on the input, may give interesting results. 313 - * 314 - * Use these functions if and only if you cannot use kstrto<foo>, because 315 - * the conversion ends on the first non-digit character, which may be far 316 - * beyond the supported range. It might be useful to parse the strings like 317 - * 10x50 or 12:21 without altering original string or temporary buffer in use. 318 - * Keep in mind above caveat. 319 - */ 320 - 321 - extern unsigned long simple_strtoul(const char *,char **,unsigned int); 322 - extern long simple_strtol(const char *,char **,unsigned int); 323 - extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 324 - extern long long simple_strtoll(const char *,char **,unsigned int); 325 182 326 183 extern int num_to_str(char *buf, int size, 327 184 unsigned long long num, unsigned int width);
+155
include/linux/kstrtox.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef _LINUX_KSTRTOX_H 3 + #define _LINUX_KSTRTOX_H 4 + 5 + #include <linux/compiler.h> 6 + #include <linux/types.h> 7 + 8 + /* Internal, do not use. */ 9 + int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); 10 + int __must_check _kstrtol(const char *s, unsigned int base, long *res); 11 + 12 + int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); 13 + int __must_check kstrtoll(const char *s, unsigned int base, long long *res); 14 + 15 + /** 16 + * kstrtoul - convert a string to an unsigned long 17 + * @s: The start of the string. The string must be null-terminated, and may also 18 + * include a single newline before its terminating null. The first character 19 + * may also be a plus sign, but not a minus sign. 20 + * @base: The number base to use. The maximum supported base is 16. If base is 21 + * given as 0, then the base of the string is automatically detected with the 22 + * conventional semantics - If it begins with 0x the number will be parsed as a 23 + * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 24 + * parsed as an octal number. Otherwise it will be parsed as a decimal. 25 + * @res: Where to write the result of the conversion on success. 26 + * 27 + * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 28 + * Preferred over simple_strtoul(). Return code must be checked. 29 + */ 30 + static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) 31 + { 32 + /* 33 + * We want to shortcut function call, but 34 + * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. 35 + */ 36 + if (sizeof(unsigned long) == sizeof(unsigned long long) && 37 + __alignof__(unsigned long) == __alignof__(unsigned long long)) 38 + return kstrtoull(s, base, (unsigned long long *)res); 39 + else 40 + return _kstrtoul(s, base, res); 41 + } 42 + 43 + /** 44 + * kstrtol - convert a string to a long 45 + * @s: The start of the string. The string must be null-terminated, and may also 46 + * include a single newline before its terminating null. The first character 47 + * may also be a plus sign or a minus sign. 48 + * @base: The number base to use. The maximum supported base is 16. If base is 49 + * given as 0, then the base of the string is automatically detected with the 50 + * conventional semantics - If it begins with 0x the number will be parsed as a 51 + * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 52 + * parsed as an octal number. Otherwise it will be parsed as a decimal. 53 + * @res: Where to write the result of the conversion on success. 54 + * 55 + * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 56 + * Preferred over simple_strtol(). Return code must be checked. 57 + */ 58 + static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) 59 + { 60 + /* 61 + * We want to shortcut function call, but 62 + * __builtin_types_compatible_p(long, long long) = 0. 63 + */ 64 + if (sizeof(long) == sizeof(long long) && 65 + __alignof__(long) == __alignof__(long long)) 66 + return kstrtoll(s, base, (long long *)res); 67 + else 68 + return _kstrtol(s, base, res); 69 + } 70 + 71 + int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); 72 + int __must_check kstrtoint(const char *s, unsigned int base, int *res); 73 + 74 + static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) 75 + { 76 + return kstrtoull(s, base, res); 77 + } 78 + 79 + static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) 80 + { 81 + return kstrtoll(s, base, res); 82 + } 83 + 84 + static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) 85 + { 86 + return kstrtouint(s, base, res); 87 + } 88 + 89 + static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) 90 + { 91 + return kstrtoint(s, base, res); 92 + } 93 + 94 + int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); 95 + int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); 96 + int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); 97 + int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); 98 + int __must_check kstrtobool(const char *s, bool *res); 99 + 100 + int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); 101 + int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); 102 + int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res); 103 + int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res); 104 + int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res); 105 + int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res); 106 + int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res); 107 + int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); 108 + int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); 109 + int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); 110 + int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res); 111 + 112 + static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) 113 + { 114 + return kstrtoull_from_user(s, count, base, res); 115 + } 116 + 117 + static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res) 118 + { 119 + return kstrtoll_from_user(s, count, base, res); 120 + } 121 + 122 + static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res) 123 + { 124 + return kstrtouint_from_user(s, count, base, res); 125 + } 126 + 127 + static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res) 128 + { 129 + return kstrtoint_from_user(s, count, base, res); 130 + } 131 + 132 + /* 133 + * Use kstrto<foo> instead. 134 + * 135 + * NOTE: simple_strto<foo> does not check for the range overflow and, 136 + * depending on the input, may give interesting results. 137 + * 138 + * Use these functions if and only if you cannot use kstrto<foo>, because 139 + * the conversion ends on the first non-digit character, which may be far 140 + * beyond the supported range. It might be useful to parse the strings like 141 + * 10x50 or 12:21 without altering original string or temporary buffer in use. 142 + * Keep in mind above caveat. 143 + */ 144 + 145 + extern unsigned long simple_strtoul(const char *,char **,unsigned int); 146 + extern long simple_strtol(const char *,char **,unsigned int); 147 + extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 148 + extern long long simple_strtoll(const char *,char **,unsigned int); 149 + 150 + static inline int strtobool(const char *s, bool *res) 151 + { 152 + return kstrtobool(s, res); 153 + } 154 + 155 + #endif /* _LINUX_KSTRTOX_H */
-7
include/linux/string.h
··· 2 2 #ifndef _LINUX_STRING_H_ 3 3 #define _LINUX_STRING_H_ 4 4 5 - 6 5 #include <linux/compiler.h> /* for inline */ 7 6 #include <linux/types.h> /* for size_t */ 8 7 #include <linux/stddef.h> /* for NULL */ ··· 183 184 extern void argv_free(char **argv); 184 185 185 186 extern bool sysfs_streq(const char *s1, const char *s2); 186 - extern int kstrtobool(const char *s, bool *res); 187 - static inline int strtobool(const char *s, bool *res) 188 - { 189 - return kstrtobool(s, res); 190 - } 191 - 192 187 int match_string(const char * const *array, size_t n, const char *string); 193 188 int __sysfs_match_string(const char * const *array, size_t n, const char *s); 194 189
+1
include/linux/sunrpc/cache.h
··· 14 14 #include <linux/kref.h> 15 15 #include <linux/slab.h> 16 16 #include <linux/atomic.h> 17 + #include <linux/kstrtox.h> 17 18 #include <linux/proc_fs.h> 18 19 19 20 /*
+3 -2
lib/kstrtox.c
··· 14 14 */ 15 15 #include <linux/ctype.h> 16 16 #include <linux/errno.h> 17 - #include <linux/kernel.h> 18 - #include <linux/math64.h> 19 17 #include <linux/export.h> 18 + #include <linux/kstrtox.h> 19 + #include <linux/math64.h> 20 20 #include <linux/types.h> 21 21 #include <linux/uaccess.h> 22 + 22 23 #include "kstrtox.h" 23 24 24 25 const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
+1
lib/parser.c
··· 6 6 #include <linux/ctype.h> 7 7 #include <linux/types.h> 8 8 #include <linux/export.h> 9 + #include <linux/kstrtox.h> 9 10 #include <linux/parser.h> 10 11 #include <linux/slab.h> 11 12 #include <linux/string.h>