Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3767351pxt; Tue, 10 Aug 2021 10:51:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwq/L2Z/gg5ttn0lgHru8m1WTFDzOwf6rco0ptzS2UPT6RDO1xEHVgxM8G5AhN1rKE90rYQ X-Received: by 2002:a02:1cc5:: with SMTP id c188mr28187613jac.46.1628617861838; Tue, 10 Aug 2021 10:51:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628617861; cv=none; d=google.com; s=arc-20160816; b=VcVO0QqBLcWD9185OVar6FgWl+x8AOhN9htBnsYiv8D6fiKZYYpdRgzmW/6kxWgV1w FiJrVmldKPucfZZtWvF3g6tsYxaMwbD603cvSM6+BG1BBhLocQIwTmizDUtE7YZEX9XD Gw/6MFsAUiI+x26c2Sex2J3+OnaW/tQEdKwFonb5hf936V9HlXpvzBASOrxpKSJAD0gV DoGth8x26K2/kYh+EddqKahtBeox7wvBZExeElzqi+9elbuHTCXo/G/2xjhpTX/Mx6DO LANPlhlhGwVS2cDb3VdVgLOucY8TYnEVHKcF6xJ4Z9I8wrmWrqsRqtg1RLYwk3YH1Uv1 nemw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nsIR4ZAxs6Qxy6g8VAdK3Clg8kLfGSPw0BlU5L7ijFA=; b=cn7tzQgKgjaCZW/jq+7Ojz45MKGJ2QooWTe/1cUqjHGLNpVcnAlnvaVZ4XA5XM70a1 WyN5DCobOUxlyt8bZH39ywlHg9Xmo/E+PjpLKjzKz6W6cMMIJTvRAq3ZzBjh2nNxBHT1 u4mL/oAM45pTKUaZRer/doR67mchI/rANwLmTcf4NaFHH7bk0aIi/BGEhosdGW8lJ7cM 4SCgEzy0iS0sXAS3vvA+NCf/1rnEwYk4mW08wOzbUVp6H6ztfJIJaFkPrwCVgAWVxOzd 1J0YobhPXa4RL5Rr7G4ToDDMKirBTG11+g8wpiVQllbMvT8L/TND3w7cfUD8+96t+RKU S9bQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zX+WSNcB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y196si11807159iof.86.2021.08.10.10.50.49; Tue, 10 Aug 2021 10:51:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zX+WSNcB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236634AbhHJRtd (ORCPT + 99 others); Tue, 10 Aug 2021 13:49:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:58770 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235703AbhHJRq2 (ORCPT ); Tue, 10 Aug 2021 13:46:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 197226124D; Tue, 10 Aug 2021 17:40:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628617229; bh=jef2gEx8Vc+ec0qkrg54qDt2b5Om48zuJyNo+RatI9k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zX+WSNcBhOHTblsIZutd6fx6csvf3bvgUC6zanJFrW+LbB0CobO6JAC9/VlYc5Lb1 0f7SQntaS3OMXLJJDCeqgF/fng/b8vlxAuYyp3cgZVWNwxzUff3sqU2vAnSR785SMP mOISx/6inf5rbq7JzviFFTJ6m6ZOHLXTwLELp6xc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+aa7c2385d46c5eba0b89@syzkaller.appspotmail.com, syzbot+abea4558531bae1ba9fe@syzkaller.appspotmail.com, Thomas Gleixner , Sebastian Andrzej Siewior Subject: [PATCH 5.10 101/135] timers: Move clearing of base::timer_running under base:: Lock Date: Tue, 10 Aug 2021 19:30:35 +0200 Message-Id: <20210810172959.193183931@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810172955.660225700@linuxfoundation.org> References: <20210810172955.660225700@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Gleixner commit bb7262b295472eb6858b5c49893954794027cd84 upstream. syzbot reported KCSAN data races vs. timer_base::timer_running being set to NULL without holding base::lock in expire_timers(). This looks innocent and most reads are clearly not problematic, but Frederic identified an issue which is: int data = 0; void timer_func(struct timer_list *t) { data = 1; } CPU 0 CPU 1 ------------------------------ -------------------------- base = lock_timer_base(timer, &flags); raw_spin_unlock(&base->lock); if (base->running_timer != timer) call_timer_fn(timer, fn, baseclk); ret = detach_if_pending(timer, base, true); base->running_timer = NULL; raw_spin_unlock_irqrestore(&base->lock, flags); raw_spin_lock(&base->lock); x = data; If the timer has previously executed on CPU 1 and then CPU 0 can observe base->running_timer == NULL and returns, assuming the timer has completed, but it's not guaranteed on all architectures. The comment for del_timer_sync() makes that guarantee. Moving the assignment under base->lock prevents this. For non-RT kernel it's performance wise completely irrelevant whether the store happens before or after taking the lock. For an RT kernel moving the store under the lock requires an extra unlock/lock pair in the case that there is a waiter for the timer, but that's not the end of the world. Reported-by: syzbot+aa7c2385d46c5eba0b89@syzkaller.appspotmail.com Reported-by: syzbot+abea4558531bae1ba9fe@syzkaller.appspotmail.com Fixes: 030dcdd197d7 ("timers: Prepare support for PREEMPT_RT") Signed-off-by: Thomas Gleixner Tested-by: Sebastian Andrzej Siewior Link: https://lore.kernel.org/r/87lfea7gw8.fsf@nanos.tec.linutronix.de Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman --- kernel/time/timer.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1265,8 +1265,10 @@ static inline void timer_base_unlock_exp static void timer_sync_wait_running(struct timer_base *base) { if (atomic_read(&base->timer_waiters)) { + raw_spin_unlock_irq(&base->lock); spin_unlock(&base->expiry_lock); spin_lock(&base->expiry_lock); + raw_spin_lock_irq(&base->lock); } } @@ -1450,14 +1452,14 @@ static void expire_timers(struct timer_b if (timer->flags & TIMER_IRQSAFE) { raw_spin_unlock(&base->lock); call_timer_fn(timer, fn, baseclk); - base->running_timer = NULL; raw_spin_lock(&base->lock); + base->running_timer = NULL; } else { raw_spin_unlock_irq(&base->lock); call_timer_fn(timer, fn, baseclk); + raw_spin_lock_irq(&base->lock); base->running_timer = NULL; timer_sync_wait_running(base); - raw_spin_lock_irq(&base->lock); } } }