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