From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751995AbdKHKkH (ORCPT ); Wed, 8 Nov 2017 05:40:07 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:46128 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751489AbdKHKkE (ORCPT ); Wed, 8 Nov 2017 05:40:04 -0500 Date: Wed, 8 Nov 2017 11:40:02 +0100 (CET) From: Thomas Gleixner To: David Howells cc: torvalds@linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: Is there a race between __mod_timer() and del_timer()? In-Reply-To: <20750.1510136141@warthog.procyon.org.uk> Message-ID: References: <20750.1510136141@warthog.procyon.org.uk> User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 8 Nov 2017, David Howells wrote: > Is there a race between the optimisation for networking code in __mod_timer() > and del_timer() - or, at least, a race that matters? > > Consider: > > CPU A CPU B > =============================== =============================== > [timer X is active] > ==>__mod_timer(X) > if (timer_pending(timer)) > [Take the true path] > -- IRQ -- ==>del_timer(X) > <== > if (timer->expires == expires) > [Take the true path] > <==return 1 > [timer X is not active] > > There's no locking to prevent this, but __mod_timer() returns without > restarting the timer. I'm not sure this is a problem exactly, however, since > del_timer() *was* issued, and could've deleted the timer after __mod_timer() > returned. Correct, if two CPUs fiddle with the same timer concurrently then there is no guaranteed outcome. > A couple of possible alleviations: > > (1) Recheck timer_pending() before returning from __mod_timer(). That's just adding more instructions into that code path for a dubious value. > (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. > I think it might just be best to put a note in the comments in __mod_timer(). Agreed. Thanks, tglx