diff options
author | Guillaume Knispel <gknispel@proformatique.com> | 2009-09-22 19:43:30 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-09-23 10:39:27 -0400 |
commit | 5ae87e79ecb5baa65e9cf48be874098fafad0668 (patch) | |
tree | 47dceb61ead03159e93ca998a6f88777474db4f3 /fs/select.c | |
parent | f58f2fa9286db0ce9124ca9986d56aa5420b7f59 (diff) |
poll/select: avoid arithmetic overflow in __estimate_accuracy()
__estimate_accuracy() was prone to integer overflow, for example if *tv ==
{2147, 483648000} on a 32 bit computer (or even for delays as small as
{429, 500000000} if the task is niced).
Because the result was already forced between 0 and 100ms, the effect of
the overflow was not too problematic, but the use of the hrtimer range
feature was not optimal in overflow cases.
This patch ensures that there can not be an integer overflow in this
function.
Signed-off-by: Guillaume Knispel <gknispel@proformatique.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Arjan van de Ven <arjan@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Tejun Heo <tj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs/select.c')
-rw-r--r-- | fs/select.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/fs/select.c b/fs/select.c index 8084834e123e..a201fc370223 100644 --- a/fs/select.c +++ b/fs/select.c | |||
@@ -41,22 +41,28 @@ | |||
41 | * better solutions.. | 41 | * better solutions.. |
42 | */ | 42 | */ |
43 | 43 | ||
44 | #define MAX_SLACK (100 * NSEC_PER_MSEC) | ||
45 | |||
44 | static long __estimate_accuracy(struct timespec *tv) | 46 | static long __estimate_accuracy(struct timespec *tv) |
45 | { | 47 | { |
46 | long slack; | 48 | long slack; |
47 | int divfactor = 1000; | 49 | int divfactor = 1000; |
48 | 50 | ||
51 | if (tv->tv_sec < 0) | ||
52 | return 0; | ||
53 | |||
49 | if (task_nice(current) > 0) | 54 | if (task_nice(current) > 0) |
50 | divfactor = divfactor / 5; | 55 | divfactor = divfactor / 5; |
51 | 56 | ||
57 | if (tv->tv_sec > MAX_SLACK / (NSEC_PER_SEC/divfactor)) | ||
58 | return MAX_SLACK; | ||
59 | |||
52 | slack = tv->tv_nsec / divfactor; | 60 | slack = tv->tv_nsec / divfactor; |
53 | slack += tv->tv_sec * (NSEC_PER_SEC/divfactor); | 61 | slack += tv->tv_sec * (NSEC_PER_SEC/divfactor); |
54 | 62 | ||
55 | if (slack > 100 * NSEC_PER_MSEC) | 63 | if (slack > MAX_SLACK) |
56 | slack = 100 * NSEC_PER_MSEC; | 64 | return MAX_SLACK; |
57 | 65 | ||
58 | if (slack < 0) | ||
59 | slack = 0; | ||
60 | return slack; | 66 | return slack; |
61 | } | 67 | } |
62 | 68 | ||