Re: Is there a race between __mod_timer() and del_timer()?
From: Linus Torvalds
Date: Wed Nov 08 2017 - 11:26:57 EST
On Wed, Nov 8, 2017 at 2:15 AM, David Howells <dhowells@xxxxxxxxxx> wrote:
>
> (2) Set timer->expires to jiffies in del_timer() - but since there's nothing
> preventing the optimisation in __mod_timer() from occurring concurrently
> with del_timer(), this probably won't help.
Right. The "race" is fundamental, and not in the timer code, but in the user.
If somebody does "del_timer()" at the same time somebody else modifies
the timer, it's not clear which one will win. The timer going away is
basically just "somebody modified it, but then immediately afterwards
another user deleted it".
So the modification was successful, but the end result is that the
timer is deleted, so it obviously isn't started.
I'm not even sure it merits a comment in the timer code, because the
timer code seems to do the right thing. The problem is in whoever
modifies and deletes a timer at the same time. It's fundamentally not
well-defined: either operation might happen "last", so you may end up
with a timer active or not, based purely on timing and luck.
Linus