diff options
-rw-r--r-- | kernel/time.c | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/kernel/time.c b/kernel/time.c index 4a8657171584..c6c80ea5d0ea 100644 --- a/kernel/time.c +++ b/kernel/time.c | |||
@@ -500,15 +500,56 @@ unsigned int jiffies_to_usecs(const unsigned long j) | |||
500 | } | 500 | } |
501 | EXPORT_SYMBOL(jiffies_to_usecs); | 501 | EXPORT_SYMBOL(jiffies_to_usecs); |
502 | 502 | ||
503 | /* | ||
504 | * When we convert to jiffies then we interpret incoming values | ||
505 | * the following way: | ||
506 | * | ||
507 | * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET) | ||
508 | * | ||
509 | * - 'too large' values [that would result in larger than | ||
510 | * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. | ||
511 | * | ||
512 | * - all other values are converted to jiffies by either multiplying | ||
513 | * the input value by a factor or dividing it with a factor | ||
514 | * | ||
515 | * We must also be careful about 32-bit overflows. | ||
516 | */ | ||
503 | unsigned long msecs_to_jiffies(const unsigned int m) | 517 | unsigned long msecs_to_jiffies(const unsigned int m) |
504 | { | 518 | { |
505 | if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) | 519 | /* |
520 | * Negative value, means infinite timeout: | ||
521 | */ | ||
522 | if ((int)m < 0) | ||
506 | return MAX_JIFFY_OFFSET; | 523 | return MAX_JIFFY_OFFSET; |
524 | |||
507 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) | 525 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) |
526 | /* | ||
527 | * HZ is equal to or smaller than 1000, and 1000 is a nice | ||
528 | * round multiple of HZ, divide with the factor between them, | ||
529 | * but round upwards: | ||
530 | */ | ||
508 | return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ); | 531 | return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ); |
509 | #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) | 532 | #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) |
533 | /* | ||
534 | * HZ is larger than 1000, and HZ is a nice round multiple of | ||
535 | * 1000 - simply multiply with the factor between them. | ||
536 | * | ||
537 | * But first make sure the multiplication result cannot | ||
538 | * overflow: | ||
539 | */ | ||
540 | if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) | ||
541 | return MAX_JIFFY_OFFSET; | ||
542 | |||
510 | return m * (HZ / MSEC_PER_SEC); | 543 | return m * (HZ / MSEC_PER_SEC); |
511 | #else | 544 | #else |
545 | /* | ||
546 | * Generic case - multiply, round and divide. But first | ||
547 | * check that if we are doing a net multiplication, that | ||
548 | * we wouldnt overflow: | ||
549 | */ | ||
550 | if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) | ||
551 | return MAX_JIFFY_OFFSET; | ||
552 | |||
512 | return (m * HZ + MSEC_PER_SEC - 1) / MSEC_PER_SEC; | 553 | return (m * HZ + MSEC_PER_SEC - 1) / MSEC_PER_SEC; |
513 | #endif | 554 | #endif |
514 | } | 555 | } |