From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932880AbXGQIyK (ORCPT ); Tue, 17 Jul 2007 04:54:10 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1759547AbXGQIxx (ORCPT ); Tue, 17 Jul 2007 04:53:53 -0400 Received: from www.osadl.org ([213.239.205.134]:55588 "EHLO mail.tglx.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1758823AbXGQIxw (ORCPT ); Tue, 17 Jul 2007 04:53:52 -0400 Subject: [PATCH] posix-timer: fix deletion race From: Thomas Gleixner To: Jeremy Katz Cc: linux-kernel@vger.kernel.org, Andrew Morton , Ingo Molnar , Oleg Nesterov , Stable Team In-Reply-To: References: Content-Type: text/plain Date: Tue, 17 Jul 2007 10:53:49 +0200 Message-Id: <1184662429.12353.426.camel@chaos> Mime-Version: 1.0 X-Mailer: Evolution 2.10.1 (2.10.1-4.fc7) Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org X-Mailing-List: linux-kernel@vger.kernel.org Jeremy Katz experienced a posix-timer related bug on 2.6.14. This is caused by a subtle race, which is there since the original posix timer commit and persists until today. timer_delete does: lock_timer(); timer->it_process = NULL; unlock_timer(); release_posix_timer(); timer->it_process is checked in lock_timer() to prevent access to a timer, which is on the way to be deleted, but the check happens after idr_lock is dropped. This allows release_posix_timer() to delete the timer before the lock code can check the timer: CPU 0 CPU 1 lock_timer(); timer->it_process = NULL; unlock_timer(); lock_timer() spin_lock(idr_lock); timer = idr_find(); spin_lock(timer->lock); spin_unlock(idr_lock); release_posix_timer(); spin_lock(idr_lock); idr_remove(timer); spin_unlock(idr_lock); free_timer(timer); if (timer->......) Change the locking to prevent this. Signed-off-by: Thomas Gleixner diff --git a/kernel/posix-timers.c b/kernel/posix-timers.c index 329ce01..c2ac6fd 100644 --- a/kernel/posix-timers.c +++ b/kernel/posix-timers.c @@ -605,13 +605,14 @@ static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags) timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id); if (timr) { spin_lock(&timr->it_lock); - spin_unlock(&idr_lock); if ((timr->it_id != timer_id) || !(timr->it_process) || timr->it_process->tgid != current->tgid) { - unlock_timer(timr, *flags); + spin_unlock(&timr->it_lock); + spin_unlock_irqrestore(&idr_lock, *flags); timr = NULL; - } + } else + spin_unlock(&idr_lock); } else spin_unlock_irqrestore(&idr_lock, *flags);