Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5302670imm; Tue, 31 Jul 2018 08:43:03 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcW8pWoXDuUukoZ8du5RZDNvN7JqRfzEOCVXYGX7WdPxGXlGk4TgEyLZungUrAxrgp6Kiyg X-Received: by 2002:a63:db4f:: with SMTP id x15-v6mr20973237pgi.214.1533051783212; Tue, 31 Jul 2018 08:43:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533051783; cv=none; d=google.com; s=arc-20160816; b=oAloG4nVa8mBNJKEx3DxSJNWhLCVmipwadyeTz5dVhYm+OUFAivXloh+c5U0eOjlD+ CDT1wLddhuaHKEXemslKbfylC0o6EYjzquLuBhIlq/Oxe43VYbkkiLWtv+YeZQBlHfuP X1KomMBmMiCXkMHdrUpkrN2Oy5Qt+Eg9mQNxW009FWF9rjMHAOcZj307UNst/TYtFEc0 rtJtc2TA16g7scBCl1rMgpwoaJ1BORXcrWAWaektysakYSepo9lyx7KSjv+XzLdhW2o/ i8X5h4XQQxVVqr7ory/wU2N1uO1gHIo29RsoPcwbYoXNyYpmANhkt5edqQezdAhS8zEP Q64w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date :arc-authentication-results; bh=MsIRNr6EqHXf8GgLjjh2MQ7UgSPSyRzpx4goS45WD5g=; b=KXgPTzOBWXma2NtQv7OUFfxJwk2h083l/U5dcgPjoFOlmA/Cy8WIaI4VmqZ9L7hQgl PYiBTy562OWOK159HwxHz/YV5sXYDX4VeBR5zG/wCVpj8y+jE7VnPkLP4p7N/euDJr2H bLwddaMM5VF/mnuAUDfbduK16VrhvyVT6SqDkcde4znmDpt7u7apLZ7QSd6cZLnTmIxA xn7ql/jx8gNTuefvZcUf0ivkVRg6cvU1+qLVqhpjLh2i4UoJJSgreiJ3mvAv6MzW+UlT AmOs1nNtwIlYqTUfJ4h4bj+HerEv/ZuiZ3oUKBp+AO51nqOfXsnAkwBgm083/GDTXj4E lgbQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f132-v6si13589666pgc.20.2018.07.31.08.42.48; Tue, 31 Jul 2018 08:43:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732492AbeGaRWM (ORCPT + 99 others); Tue, 31 Jul 2018 13:22:12 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:60020 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732268AbeGaRWM (ORCPT ); Tue, 31 Jul 2018 13:22:12 -0400 Received: from hsi-kbw-5-158-153-52.hsi19.kabel-badenwuerttemberg.de ([5.158.153.52] helo=nanos.tec.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1fkWlg-00071U-DQ; Tue, 31 Jul 2018 17:41:01 +0200 Date: Tue, 31 Jul 2018 17:41:00 +0200 (CEST) From: Thomas Gleixner To: Gaurav Kohli cc: John Stultz , sboyd@kernel.org, Anna-Maria Gleixner , LKML , linux-arm-msm@vger.kernel.org Subject: Re: [PATCH] timers: Clear must_forward_clk inside base lock In-Reply-To: <1532594522-28045-1-git-send-email-gkohli@codeaurora.org> Message-ID: References: <1532594522-28045-1-git-send-email-gkohli@codeaurora.org> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Gaurav, On Thu, 26 Jul 2018, Gaurav Kohli wrote: > While migrating timer to new base, there is a need > to update base clk by calling forward_timer_base to > avoid stale clock , but at the same time if run_timer > is exectuing in new core it may set must_forward_clk > to false and due to this forward base logic may fail as > per below check: > > if (likely(!base->must_forward_clk)) > return; After twisting my brain for a while I can understand what you are trying to say, but please look at your own sentence once again. One sentence spawning 6 lines with a really convoluted structure and then you spend 3 lines to copy a code snippet which is really not helpful. Please try to structure the description and use a simple table to show the race, e.g.: base->must_forward_clock is indicating that the base clock might be stale due to a long idle sleep. The forwarding takes either place in the timer softirq or when a timer is enqueued while the base is idle. If the enqueue to an idle base happens from a remote CPU then the following race can happen: CPU0 CPU1 run_timer_softirq() mod_timer(timer) base->must_forward_clk = false; base = lock_base(timer); __run_timers(base) if (base->must_forward_clk) forward(base); lock(base->lock); queue_timer(base, timer); ^^^ Based on stale base->clk unlock(base); forward(base); The root cause is that base->must_forward_clk is cleared outside the base->lock held region, so the remote queueing CPU observes it as cleared, but the base clock is still stale. This can cause large granularity values for timers, i.e. the accuracy of the expiry time suffers. Prevent this by clearing the flag with base->lock held, so that the forwarding takes place before the cleared flag is observable by a remote CPU. Can you see the difference? > raw_spin_lock_irq(&base->lock); > > + /* > + * must_forward_clk must be cleared before running timers so that any > + * timer functions that call mod_timer will not try to forward the > + * base. idle trcking / clock forwarding logic is only used with I know that the 'trcking' typo was in the original comment, but it does not make anything better if you just blindly move it. > + * BASE_STD timers. > + * > + * The deferrable base does not do idle tracking at all, so we do > + * not forward it. This can result in very large variations in > + * granularity for deferrable timers, but they can be deferred for > + * long periods due to idle. This part of the comment is still correct, but now it's also confusing because the flag is cleared for _ALL_ bases and not only for BASE_STD. So at least you want to change that to something like this: * The must_forward_clk flag is cleared unconditionally also for * the deferrable base. The deferrable base is not affected by idle * tracking and never forwarded, so clearing the flag is a NOOP. * * The fact that the deferrable base is never forwarded can cause * large varations in granularity for deferrable timers, but they * can be deferred for long periods due to idle anyway. See? If you move a comment you really have to think about whether it is still correct. If not, then you have to adjust it so it makes sense and not just move it blindly around and be done with it. Think about yourself looking at that code in a year from now when you forgot all the gory details already. Thanks, tglx