From: Willy Tarreau <willy@w.ods.org>
To: Vadim Lobanov <vlobanov@speakeasy.net>
Cc: Nish Aravamudan <nish.aravamudan@gmail.com>,
Davide Libenzi <davidel@xmailserver.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Andrew Morton <akpm@osdl.org>
Subject: Re: [patch] sys_epoll_wait() timeout saga ...
Date: Sat, 24 Sep 2005 09:51:23 +0200 [thread overview]
Message-ID: <20050924075123.GA24999@alpha.home.local> (raw)
In-Reply-To: <Pine.LNX.4.58.0509240030370.24744@shell2.speakeasy.net>
On Sat, Sep 24, 2005 at 12:33:05AM -0700, Vadim Lobanov wrote:
> On Sat, 24 Sep 2005, Willy Tarreau wrote:
>
> > On Fri, Sep 23, 2005 at 09:44:10PM -0700, Nish Aravamudan wrote:
> > > > > * that why (t * HZ) / 1000.
> > > > > */
> > > > > - jtimeout = timeout == -1 || timeout > (MAX_SCHEDULE_TIMEOUT - 1000) / HZ ?
> > > > > + jtimeout = timeout < 0 || (timeout / 1000) >= (MAX_SCHEDULE_TIMEOUT / HZ) ?
> > > > > MAX_SCHEDULE_TIMEOUT: (timeout * HZ + 999) / 1000;
> > > >
> > > > Here, I'm not certain that gcc will optimize the divide. It would be better
> > > > anyway to write this which is equivalent, and a pure integer comparison :
> > > >
> > > > + jtimeout = timeout < 0 || timeout >= 1000 * MAX_SCHEDULE_TIMEOUT / HZ ?
> > > > > MAX_SCHEDULE_TIMEOUT: (timeout * HZ + 999) / 1000;
> > >
> > > Just a question here, maybe it's dumb.
> >
> > Your question is not dumb, this code is not trivial at all !
> >
> > > * and / have the same priority in the order of operations, yes? If so,
> > > won't the the 1000 * MAX_SCHEDULE_TIMEOUT overflow
> > > (MAX_SCHEDULE_TIMEOUT is LONG_MAX)?
> >
> > Yes it can, and that's why I said that gcc should send a warning when
> > comparing an int with something too large for an int. But I should have
> > forced the constant to be evaluated as long long. At the moment, the
> > constant cannot overflow, but it can reach a value so high that
> > timeout/1000 will never reach it. Example :
> > MAX_SCHEDULE_TIMEOUT=LONG_MAX
> > HZ=250
> > timeout=LONG_MAX-1
> > => timeout/1000 < MAX_SCHEDULE_TIMEOUT/HZ
> > but (timeout * HZ + 999) / 1000 will still overflow !
> >
> > So I finally think that the safest test would be to avoid the timeout
> > range which can overflow in the computation, using something like this
> > (but which will limit the timeout to 49 days on HZ=1000 machines) :
> >
> > + jtimeout = timeout < 0 || \
> > + timeout >= (1000ULL * MAX_SCHEDULE_TIMEOUT / HZ) || \
> > + timeout >= (LONG_MAX / HZ - 1000) ?
> > MAX_SCHEDULE_TIMEOUT: (timeout * HZ + 999) / 1000;
>
> It seems that we can make the second overflow test be less strict by
> doing the following instead:
> timeout >= (LONG_MAX - 1000) / HZ
> Unless I'm confused. :-)
oops, you're right. Then it produces the following patch :
diff -purN linux-2.6.13/fs/eventpoll.c linux-2.6.13-epoll/fs/eventpoll.c
--- linux-2.6.13/fs/eventpoll.c Sun Sep 11 08:25:26 2005
+++ linux-2.6.13-epoll/fs/eventpoll.c Sat Sep 24 09:49:43 2005
@@ -1504,9 +1504,12 @@ static int ep_poll(struct eventpoll *ep,
/*
* Calculate the timeout by checking for the "infinite" value ( -1 )
* and the overflow condition. The passed timeout is in milliseconds,
- * that why (t * HZ) / 1000.
+ * that why (t * HZ) / 1000. Note that we also want to avoid an
+ * overflow in the multiply.
*/
- jtimeout = timeout == -1 || timeout > (MAX_SCHEDULE_TIMEOUT - 1000) / HZ ?
+ jtimeout = timeout < 0 ||
+ timeout > (MAX_SCHEDULE_TIMEOUT * 1000ULL / HZ) ||
+ timeout > (LONG_MAX - 1000) / HZ ?
MAX_SCHEDULE_TIMEOUT: (timeout * HZ + 999) / 1000;
retry:
Interestingly, as long as MAC_SCHEDULE_TIMEOUT == LONG_MAX, the check is
identical to the initial one (and does not add any divide) !
Regards,
Willy
next prev parent reply other threads:[~2005-09-24 7:54 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-09-23 18:13 [patch] sys_epoll_wait() timeout saga Davide Libenzi
2005-09-23 18:24 ` Nish Aravamudan
2005-09-24 4:05 ` Willy Tarreau
2005-09-24 4:44 ` Nish Aravamudan
2005-09-24 6:15 ` Willy Tarreau
2005-09-24 7:33 ` Vadim Lobanov
2005-09-24 7:51 ` Willy Tarreau [this message]
2005-09-24 15:10 ` Davide Libenzi
2005-09-24 17:20 ` Willy Tarreau
2005-09-24 18:19 ` Davide Libenzi
2005-09-25 6:05 ` Andrew Morton
2005-09-25 6:20 ` Willy Tarreau
2005-09-25 6:32 ` Andrew Morton
2005-09-25 7:08 ` Vadim Lobanov
2005-09-25 8:03 ` Willy Tarreau
2005-09-24 17:19 ` Nishanth Aravamudan
2005-09-24 18:25 ` Davide Libenzi
2005-09-24 19:38 ` [PATCH 0/3] fixes for overflow in poll(), epoll(), and msec_to_jiffies() Willy Tarreau
2005-09-24 19:44 ` [PATCH 1/3] 2.6.14-rc2-mm1: fixes for overflow msec_to_jiffies() Willy Tarreau
2005-09-29 9:43 ` Andrew Morton
2005-09-29 19:41 ` Willy Tarreau
2005-09-29 19:52 ` Andrew Morton
2005-09-29 20:55 ` Willy Tarreau
2005-10-01 17:39 ` Willy Tarreau
2005-09-24 19:47 ` [PATCH 2/3] 2.6.14-rc2-mm1: fixes for overflow in epoll() Willy Tarreau
2005-09-24 19:52 ` [PATCH 3/3] 2.6.14-rc2-mm1 : fixes for overflow in sys_poll() Willy Tarreau
2005-10-01 20:39 ` Willy Tarreau
2005-09-24 20:08 ` [PATCH 0/3] fixes for overflow in poll(), epoll(), and msec_to_jiffies() Davide Libenzi
2005-09-24 20:21 ` Willy TARREAU
2005-09-25 20:55 ` Nishanth Aravamudan
2005-09-25 22:06 ` Willy Tarreau
2005-09-24 21:25 ` [patch] sys_epoll_wait() timeout saga Vadim Lobanov
2005-09-24 18:30 ` Willy Tarreau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050924075123.GA24999@alpha.home.local \
--to=willy@w.ods.org \
--cc=akpm@osdl.org \
--cc=davidel@xmailserver.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nish.aravamudan@gmail.com \
--cc=vlobanov@speakeasy.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox