···310310 return ktime_sub_ns(kt, usec * 1000);311311}312312313313+extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs);314314+313315/*314316 * The resolution of the clocks. The resolution value is returned in315317 * the clock_getres() system call to give application programmers an
···176176177177 t = timespec_to_ktime(ts);178178 if (cmd == FUTEX_WAIT)179179- t = ktime_add(ktime_get(), t);179179+ t = ktime_add_safe(ktime_get(), t);180180 tp = &t;181181 }182182 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE)
+31-17
kernel/hrtimer.c
···326326#endif /* BITS_PER_LONG >= 64 */327327328328/*329329+ * Add two ktime values and do a safety check for overflow:330330+ */331331+ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)332332+{333333+ ktime_t res = ktime_add(lhs, rhs);334334+335335+ /*336336+ * We use KTIME_SEC_MAX here, the maximum timeout which we can337337+ * return to user space in a timespec:338338+ */339339+ if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)340340+ res = ktime_set(KTIME_SEC_MAX, 0);341341+342342+ return res;343343+}344344+345345+/*329346 * Check, whether the timer is on the callback pending list330347 */331348static inline int hrtimer_cb_pending(const struct hrtimer *timer)···442425 ktime_t expires = ktime_sub(timer->expires, base->offset);443426 int res;444427428428+ WARN_ON_ONCE(timer->expires.tv64 < 0);429429+445430 /*446431 * When the callback is running, we do not reprogram the clock event447432 * device. The timer callback is either running on a different CPU or···453434 */454435 if (hrtimer_callback_running(timer))455436 return 0;437437+438438+ /*439439+ * CLOCK_REALTIME timer might be requested with an absolute440440+ * expiry time which is less than base->offset. Nothing wrong441441+ * about that, just avoid to call into the tick code, which442442+ * has now objections against negative expiry values.443443+ */444444+ if (expires.tv64 < 0)445445+ return -ETIME;456446457447 if (expires.tv64 >= expires_next->tv64)458448 return 0;···710682 */711683 orun++;712684 }713713- timer->expires = ktime_add(timer->expires, interval);714714- /*715715- * Make sure, that the result did not wrap with a very large716716- * interval.717717- */718718- if (timer->expires.tv64 < 0)719719- timer->expires = ktime_set(KTIME_SEC_MAX, 0);685685+ timer->expires = ktime_add_safe(timer->expires, interval);720686721687 return orun;722688}···861839 new_base = switch_hrtimer_base(timer, base);862840863841 if (mode == HRTIMER_MODE_REL) {864864- tim = ktime_add(tim, new_base->get_time());842842+ tim = ktime_add_safe(tim, new_base->get_time());865843 /*866844 * CONFIG_TIME_LOW_RES is a temporary way for architectures867845 * to signal that they simply return xtime in···870848 * timeouts. This will go away with the GTOD framework.871849 */872850#ifdef CONFIG_TIME_LOW_RES873873- tim = ktime_add(tim, base->resolution);851851+ tim = ktime_add_safe(tim, base->resolution);874852#endif875875- /*876876- * Careful here: User space might have asked for a877877- * very long sleep, so the add above might result in a878878- * negative number, which enqueues the timer in front879879- * of the queue.880880- */881881- if (tim.tv64 < 0)882882- tim.tv64 = KTIME_MAX;883853 }884854 timer->expires = tim;885855
+5-3
kernel/posix-timers.c
···767767 /* SIGEV_NONE timers are not queued ! See common_timer_get */768768 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {769769 /* Setup correct expiry time for relative timers */770770- if (mode == HRTIMER_MODE_REL)771771- timer->expires = ktime_add(timer->expires,772772- timer->base->get_time());770770+ if (mode == HRTIMER_MODE_REL) {771771+ timer->expires =772772+ ktime_add_safe(timer->expires,773773+ timer->base->get_time());774774+ }773775 return 0;774776 }775777