1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. 6 * Copyright (c) 2014-2015 François Tigeot 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice unmodified, this list of conditions, and the following 14 * disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #ifndef _LINUXKPI_LINUX_KERNEL_H_ 31 #define _LINUXKPI_LINUX_KERNEL_H_ 32 33 #include <sys/cdefs.h> 34 #include <sys/types.h> 35 #include <sys/systm.h> 36 #include <sys/param.h> 37 #include <sys/libkern.h> 38 #include <sys/stat.h> 39 #include <sys/smp.h> 40 #include <sys/stddef.h> 41 #include <sys/syslog.h> 42 #include <sys/time.h> 43 44 #include <linux/bitops.h> 45 #include <linux/build_bug.h> 46 #include <linux/compiler.h> 47 #include <linux/container_of.h> 48 #include <linux/limits.h> 49 #include <linux/minmax.h> 50 #include <linux/stringify.h> 51 #include <linux/errno.h> 52 #include <linux/sched.h> 53 #include <linux/types.h> 54 #include <linux/typecheck.h> 55 #include <linux/jiffies.h> 56 #include <linux/log2.h> 57 #include <linux/kconfig.h> 58 59 #include <asm/byteorder.h> 60 #include <asm/cpufeature.h> 61 #include <asm/processor.h> 62 #include <asm/uaccess.h> 63 64 #include <linux/stdarg.h> 65 66 #define KERN_CONT "" 67 #define KERN_EMERG "<0>" 68 #define KERN_ALERT "<1>" 69 #define KERN_CRIT "<2>" 70 #define KERN_ERR "<3>" 71 #define KERN_WARNING "<4>" 72 #define KERN_NOTICE "<5>" 73 #define KERN_INFO "<6>" 74 #define KERN_DEBUG "<7>" 75 76 #define S8_C(x) x 77 #define U8_C(x) x ## U 78 #define S16_C(x) x 79 #define U16_C(x) x ## U 80 #define S32_C(x) x 81 #define U32_C(x) x ## U 82 #define S64_C(x) x ## LL 83 #define U64_C(x) x ## ULL 84 85 #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) 86 #define BUG_ON(cond) do { \ 87 if (cond) { \ 88 panic("BUG ON %s failed at %s:%d", \ 89 __stringify(cond), __FILE__, __LINE__); \ 90 } \ 91 } while (0) 92 93 extern int linuxkpi_warn_dump_stack; 94 #define WARN_ON(cond) ({ \ 95 bool __ret = (cond); \ 96 if (__ret) { \ 97 printf("WARNING %s failed at %s:%d\n", \ 98 __stringify(cond), __FILE__, __LINE__); \ 99 if (linuxkpi_warn_dump_stack) \ 100 linux_dump_stack(); \ 101 } \ 102 unlikely(__ret); \ 103 }) 104 105 #define WARN_ON_SMP(cond) WARN_ON(cond) 106 107 #define WARN_ON_ONCE(cond) ({ \ 108 static bool __warn_on_once; \ 109 bool __ret = (cond); \ 110 if (__ret && !__warn_on_once) { \ 111 __warn_on_once = 1; \ 112 printf("WARNING %s failed at %s:%d\n", \ 113 __stringify(cond), __FILE__, __LINE__); \ 114 if (linuxkpi_warn_dump_stack) \ 115 linux_dump_stack(); \ 116 } \ 117 unlikely(__ret); \ 118 }) 119 120 #define oops_in_progress SCHEDULER_STOPPED() 121 122 #undef ALIGN 123 #define ALIGN(x, y) roundup2((x), (y)) 124 #define ALIGN_DOWN(x, y) rounddown2(x, y) 125 #undef PTR_ALIGN 126 #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) 127 #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0) 128 #define DIV_ROUND_UP(x, n) howmany(x, n) 129 #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n) 130 #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) 131 #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n)) 132 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 133 134 #define printk(...) printf(__VA_ARGS__) 135 #define vprintk(f, a) vprintf(f, a) 136 137 #define PTR_IF(x, p) ((x) ? (p) : NULL) 138 139 #define asm __asm 140 141 extern void linux_dump_stack(void); 142 #define dump_stack() linux_dump_stack() 143 144 struct va_format { 145 const char *fmt; 146 va_list *va; 147 }; 148 149 static inline int 150 vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 151 { 152 ssize_t ssize = size; 153 int i; 154 155 i = vsnprintf(buf, size, fmt, args); 156 157 return ((i >= ssize) ? (ssize - 1) : i); 158 } 159 160 static inline int 161 scnprintf(char *buf, size_t size, const char *fmt, ...) 162 { 163 va_list args; 164 int i; 165 166 va_start(args, fmt); 167 i = vscnprintf(buf, size, fmt, args); 168 va_end(args); 169 170 return (i); 171 } 172 173 /* 174 * The "pr_debug()" and "pr_devel()" macros should produce zero code 175 * unless DEBUG is defined: 176 */ 177 #ifdef DEBUG 178 extern int linuxkpi_debug; 179 #define pr_debug(fmt, ...) \ 180 do { \ 181 if (linuxkpi_debug) \ 182 log(LOG_DEBUG, fmt, ##__VA_ARGS__); \ 183 } while (0) 184 #define pr_devel(fmt, ...) \ 185 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 186 #else 187 #define pr_debug(fmt, ...) \ 188 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 189 #define pr_devel(fmt, ...) \ 190 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 191 #endif 192 193 #ifndef pr_fmt 194 #define pr_fmt(fmt) fmt 195 #endif 196 197 /* 198 * Print a one-time message (analogous to WARN_ONCE() et al): 199 */ 200 #define printk_once(...) do { \ 201 static bool __print_once; \ 202 \ 203 if (!__print_once) { \ 204 __print_once = true; \ 205 printk(__VA_ARGS__); \ 206 } \ 207 } while (0) 208 209 /* 210 * Log a one-time message (analogous to WARN_ONCE() et al): 211 */ 212 #define log_once(level,...) do { \ 213 static bool __log_once; \ 214 \ 215 if (unlikely(!__log_once)) { \ 216 __log_once = true; \ 217 log(level, __VA_ARGS__); \ 218 } \ 219 } while (0) 220 221 #define pr_emerg(fmt, ...) \ 222 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 223 #define pr_alert(fmt, ...) \ 224 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 225 #define pr_crit(fmt, ...) \ 226 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 227 #define pr_err(fmt, ...) \ 228 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 229 #define pr_err_once(fmt, ...) \ 230 log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 231 #define pr_warning(fmt, ...) \ 232 log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 233 #define pr_warn(...) \ 234 pr_warning(__VA_ARGS__) 235 #define pr_warn_once(fmt, ...) \ 236 log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 237 #define pr_notice(fmt, ...) \ 238 log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 239 #define pr_info(fmt, ...) \ 240 log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 241 #define pr_info_once(fmt, ...) \ 242 log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 243 #define pr_cont(fmt, ...) \ 244 printk(KERN_CONT fmt, ##__VA_ARGS__) 245 #define pr_warn_ratelimited(...) do { \ 246 static linux_ratelimit_t __ratelimited; \ 247 if (linux_ratelimited(&__ratelimited)) \ 248 pr_warning(__VA_ARGS__); \ 249 } while (0) 250 251 #ifndef WARN 252 #define WARN(condition, ...) ({ \ 253 bool __ret_warn_on = (condition); \ 254 if (unlikely(__ret_warn_on)) \ 255 pr_warning(__VA_ARGS__); \ 256 unlikely(__ret_warn_on); \ 257 }) 258 #endif 259 260 #ifndef WARN_ONCE 261 #define WARN_ONCE(condition, ...) ({ \ 262 bool __ret_warn_on = (condition); \ 263 if (unlikely(__ret_warn_on)) \ 264 pr_warn_once(__VA_ARGS__); \ 265 unlikely(__ret_warn_on); \ 266 }) 267 #endif 268 269 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 270 271 #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) 272 273 #define _RET_IP_ __builtin_return_address(0) 274 275 static inline unsigned long long 276 simple_strtoull(const char *cp, char **endp, unsigned int base) 277 { 278 return (strtouq(cp, endp, base)); 279 } 280 281 static inline long long 282 simple_strtoll(const char *cp, char **endp, unsigned int base) 283 { 284 return (strtoq(cp, endp, base)); 285 } 286 287 static inline unsigned long 288 simple_strtoul(const char *cp, char **endp, unsigned int base) 289 { 290 return (strtoul(cp, endp, base)); 291 } 292 293 static inline long 294 simple_strtol(const char *cp, char **endp, unsigned int base) 295 { 296 return (strtol(cp, endp, base)); 297 } 298 299 static inline int 300 kstrtoul(const char *cp, unsigned int base, unsigned long *res) 301 { 302 char *end; 303 304 *res = strtoul(cp, &end, base); 305 306 /* skip newline character, if any */ 307 if (*end == '\n') 308 end++; 309 if (*cp == 0 || *end != 0) 310 return (-EINVAL); 311 return (0); 312 } 313 314 static inline int 315 kstrtol(const char *cp, unsigned int base, long *res) 316 { 317 char *end; 318 319 *res = strtol(cp, &end, base); 320 321 /* skip newline character, if any */ 322 if (*end == '\n') 323 end++; 324 if (*cp == 0 || *end != 0) 325 return (-EINVAL); 326 return (0); 327 } 328 329 static inline int 330 kstrtoint(const char *cp, unsigned int base, int *res) 331 { 332 char *end; 333 long temp; 334 335 *res = temp = strtol(cp, &end, base); 336 337 /* skip newline character, if any */ 338 if (*end == '\n') 339 end++; 340 if (*cp == 0 || *end != 0) 341 return (-EINVAL); 342 if (temp != (int)temp) 343 return (-ERANGE); 344 return (0); 345 } 346 347 static inline int 348 kstrtouint(const char *cp, unsigned int base, unsigned int *res) 349 { 350 char *end; 351 unsigned long temp; 352 353 *res = temp = strtoul(cp, &end, base); 354 355 /* skip newline character, if any */ 356 if (*end == '\n') 357 end++; 358 if (*cp == 0 || *end != 0) 359 return (-EINVAL); 360 if (temp != (unsigned int)temp) 361 return (-ERANGE); 362 return (0); 363 } 364 365 static inline int 366 kstrtou8(const char *cp, unsigned int base, u8 *res) 367 { 368 char *end; 369 unsigned long temp; 370 371 *res = temp = strtoul(cp, &end, base); 372 373 /* skip newline character, if any */ 374 if (*end == '\n') 375 end++; 376 if (*cp == 0 || *end != 0) 377 return (-EINVAL); 378 if (temp != (u8)temp) 379 return (-ERANGE); 380 return (0); 381 } 382 383 static inline int 384 kstrtou16(const char *cp, unsigned int base, u16 *res) 385 { 386 char *end; 387 unsigned long temp; 388 389 *res = temp = strtoul(cp, &end, base); 390 391 /* skip newline character, if any */ 392 if (*end == '\n') 393 end++; 394 if (*cp == 0 || *end != 0) 395 return (-EINVAL); 396 if (temp != (u16)temp) 397 return (-ERANGE); 398 return (0); 399 } 400 401 static inline int 402 kstrtou32(const char *cp, unsigned int base, u32 *res) 403 { 404 405 return (kstrtouint(cp, base, res)); 406 } 407 408 static inline int 409 kstrtou64(const char *cp, unsigned int base, u64 *res) 410 { 411 char *end; 412 413 *res = strtouq(cp, &end, base); 414 415 /* skip newline character, if any */ 416 if (*end == '\n') 417 end++; 418 if (*cp == 0 || *end != 0) 419 return (-EINVAL); 420 return (0); 421 } 422 423 static inline int 424 kstrtoull(const char *cp, unsigned int base, unsigned long long *res) 425 { 426 return (kstrtou64(cp, base, (u64 *)res)); 427 } 428 429 static inline int 430 kstrtobool(const char *s, bool *res) 431 { 432 int len; 433 434 if (s == NULL || (len = strlen(s)) == 0 || res == NULL) 435 return (-EINVAL); 436 437 /* skip newline character, if any */ 438 if (s[len - 1] == '\n') 439 len--; 440 441 if (len == 1 && strchr("yY1", s[0]) != NULL) 442 *res = true; 443 else if (len == 1 && strchr("nN0", s[0]) != NULL) 444 *res = false; 445 else if (strncasecmp("on", s, len) == 0) 446 *res = true; 447 else if (strncasecmp("off", s, len) == 0) 448 *res = false; 449 else 450 return (-EINVAL); 451 452 return (0); 453 } 454 455 static inline int 456 kstrtobool_from_user(const char __user *s, size_t count, bool *res) 457 { 458 char buf[8] = {}; 459 460 if (count > (sizeof(buf) - 1)) 461 count = (sizeof(buf) - 1); 462 463 if (copy_from_user(buf, s, count)) 464 return (-EFAULT); 465 466 return (kstrtobool(buf, res)); 467 } 468 469 static inline int 470 kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, 471 int *p) 472 { 473 char buf[36] = {}; 474 475 if (count > (sizeof(buf) - 1)) 476 count = (sizeof(buf) - 1); 477 478 if (copy_from_user(buf, s, count)) 479 return (-EFAULT); 480 481 return (kstrtoint(buf, base, p)); 482 } 483 484 static inline int 485 kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, 486 unsigned int *p) 487 { 488 char buf[36] = {}; 489 490 if (count > (sizeof(buf) - 1)) 491 count = (sizeof(buf) - 1); 492 493 if (copy_from_user(buf, s, count)) 494 return (-EFAULT); 495 496 return (kstrtouint(buf, base, p)); 497 } 498 499 static inline int 500 kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, 501 unsigned int *p) 502 { 503 504 return (kstrtouint_from_user(s, count, base, p)); 505 } 506 507 static inline int 508 kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, 509 u8 *p) 510 { 511 char buf[8] = {}; 512 513 if (count > (sizeof(buf) - 1)) 514 count = (sizeof(buf) - 1); 515 516 if (copy_from_user(buf, s, count)) 517 return (-EFAULT); 518 519 return (kstrtou8(buf, base, p)); 520 } 521 522 #define offsetofend(t, m) \ 523 (offsetof(t, m) + sizeof((((t *)0)->m))) 524 525 /* 526 * This looks more complex than it should be. But we need to 527 * get the type for the ~ right in round_down (it needs to be 528 * as wide as the result!), and we want to evaluate the macro 529 * arguments just once each. 530 */ 531 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 532 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 533 #define round_down(x, y) ((x) & ~__round_mask(x, y)) 534 535 #define smp_processor_id() PCPU_GET(cpuid) 536 #define num_possible_cpus() mp_ncpus 537 #define num_online_cpus() mp_ncpus 538 539 #if defined(__i386__) || defined(__amd64__) 540 extern bool linux_cpu_has_clflush; 541 #define cpu_has_clflush linux_cpu_has_clflush 542 #endif 543 544 #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) 545 546 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \ 547 __typeof(divisor) __d = (divisor); \ 548 unsigned long long __ret = (x) + (__d) / 2; \ 549 __ret /= __d; \ 550 __ret; \ 551 }) 552 553 static inline uintmax_t 554 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 555 { 556 uintmax_t q = (x / divisor); 557 uintmax_t r = (x % divisor); 558 559 return ((q * multiplier) + ((r * multiplier) / divisor)); 560 } 561 562 typedef struct linux_ratelimit { 563 struct timeval lasttime; 564 int counter; 565 } linux_ratelimit_t; 566 567 static inline bool 568 linux_ratelimited(linux_ratelimit_t *rl) 569 { 570 return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); 571 } 572 573 #define __is_constexpr(x) \ 574 __builtin_constant_p(x) 575 576 /* 577 * The is_signed() macro below returns true if the passed data type is 578 * signed. Else false is returned. 579 */ 580 #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) 581 582 #define TAINT_WARN 0 583 #define test_taint(x) (0) 584 #define add_taint(x,y) do { \ 585 } while (0) 586 587 static inline int 588 _h2b(const char c) 589 { 590 591 if (c >= '0' && c <= '9') 592 return (c - '0'); 593 if (c >= 'a' && c <= 'f') 594 return (10 + c - 'a'); 595 if (c >= 'A' && c <= 'F') 596 return (10 + c - 'A'); 597 return (-EINVAL); 598 } 599 600 static inline int 601 hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen) 602 { 603 int hi4, lo4; 604 605 while (binlen > 0) { 606 hi4 = _h2b(*hexsrc++); 607 lo4 = _h2b(*hexsrc++); 608 if (hi4 < 0 || lo4 < 0) 609 return (-EINVAL); 610 611 *bindst++ = (hi4 << 4) | lo4; 612 binlen--; 613 } 614 615 return (0); 616 } 617 618 static inline bool 619 mac_pton(const char *macin, uint8_t *macout) 620 { 621 const char *s, *d; 622 uint8_t mac[6], hx, lx;; 623 int i; 624 625 if (strlen(macin) < (3 * 6 - 1)) 626 return (false); 627 628 i = 0; 629 s = macin; 630 do { 631 /* Should we also support '-'-delimiters? */ 632 d = strchrnul(s, ':'); 633 hx = lx = 0; 634 while (s < d) { 635 /* Fail on abc:123:xxx:... */ 636 if ((d - s) > 2) 637 return (false); 638 /* We do support non-well-formed strings: 3:45:6:... */ 639 if ((d - s) > 1) { 640 hx = _h2b(*s); 641 if (hx < 0) 642 return (false); 643 s++; 644 } 645 lx = _h2b(*s); 646 if (lx < 0) 647 return (false); 648 s++; 649 } 650 mac[i] = (hx << 4) | lx; 651 i++; 652 if (i >= 6) 653 return (false); 654 } while (d != NULL && *d != '\0'); 655 656 memcpy(macout, mac, 6); 657 return (true); 658 } 659 660 #define DECLARE_FLEX_ARRAY(_t, _n) \ 661 struct { struct { } __dummy_ ## _n; _t _n[0]; } 662 663 #endif /* _LINUXKPI_LINUX_KERNEL_H_ */ 664