Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5101297imm; Tue, 31 Jul 2018 05:38:47 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdcuoOZSuYxNDcgoZdZ+AK0SmtmVQhy123DJYFZHpoHm3An74OCy2ZtyoYIsSUWPQE77oRD X-Received: by 2002:a17:902:e005:: with SMTP id ca5-v6mr20358331plb.224.1533040727602; Tue, 31 Jul 2018 05:38:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533040727; cv=none; d=google.com; s=arc-20160816; b=XtrphD5Bmw7VMOPW2s9+KymgX+Rgjof2uHSIpeaIBH4Lo5MQs/2xTgYUAyiRFkSygi TTwMx7rSkMSdE+dpDWQCFxgylPDqp4MHmHHDSErfn381+58MEh4yTUktcHgJXcXNjKbZ HmAVwInBnzh6Cmr674L3CVGIMU7guUej8IOhni9oBwLGIx5mqXFMB/PtpdSxjFItaoMe OqEQtbus0VV+5qC3Ctc917zbzpkCUNtNLIUi5N3Ed0TTnT5osDnQY+D51CP8rtji78/x rOBjpi/FEKF+TRrdbQ373cUTw3gZTLyOEH8dQk1tck5qHHLNjVfEamme+uGnh/gAxAMt BMSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:arc-authentication-results; bh=TY6Avfh8hExmpxZ7XcliWOvRqShpTb++LWfuzZ/0wYw=; b=zOcunmDOy+ccNJ7QSLft2SzO306TamiAa5DTUrGkfcrvz8dTDcnuiDwmgJzrH+Z+nN ffdtTm/1yTqgGVh9lXPkfvkwfJ8dXESh9xfX78rOXg+e/F6PL2CJyV8dUmHVsvodP1Lp ABYUxFDipikdknCbkR3GVOgf2TOlEtlyHs5EA/XH2l6IbWNkxLq+nAJl18QXHB1GPYNo mLhfiF89zHjlO//oXDzO1cfLFGEEIKTgRjpIr20lTnIaCVoaUBkl8V7PR3RJ+oPBQ3cu R28DJuGeeoNMebB0ZHOzrU6sR616lIL/YMGzbEWPh826TmAnEDJljVrzVTq0lb1f5btU HANA== 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 n11-v6si11909645plk.225.2018.07.31.05.38.33; Tue, 31 Jul 2018 05:38:47 -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 S1732244AbeGaORn (ORCPT + 99 others); Tue, 31 Jul 2018 10:17:43 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:10190 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1732150AbeGaORn (ORCPT ); Tue, 31 Jul 2018 10:17:43 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id E495A17720F38; Tue, 31 Jul 2018 20:37:27 +0800 (CST) Received: from [127.0.0.1] (10.121.91.26) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.399.0; Tue, 31 Jul 2018 20:37:27 +0800 Subject: Re: [PATCH] timers: fix offset calculation when the expires align with LVL_GRAN To: Thomas Gleixner CC: , , References: <1532669610-103892-1-git-send-email-xuyiping@hisilicon.com> From: Xu YiPing Message-ID: <4738717a-0ca7-8fc4-567d-8bd669f9eaf6@hisilicon.com> Date: Tue, 31 Jul 2018 20:37:26 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.121.91.26] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018/7/30 19:03, Thomas Gleixner wrote: > On Fri, 27 Jul 2018, Xu YiPing wrote: > >> when the expires of timer is align with LVL_GRAN(n), it will be trigged >> in 'expires + LVL_GRAN(n)'. >> >> Some drivers like power runtime use the timer to start a power down >> of device, it could saves power if the timer is triggerd in time, >> especially when LEVEL=0 with low expires. > >>From the above I have no idea what you are trying to 'fix', but see below. > >> Signed-off-by: Xu YiPing >> --- >> kernel/time/timer.c | 3 ++- >> 1 file changed, 2 insertions(+), 1 deletion(-) >> >> diff --git a/kernel/time/timer.c b/kernel/time/timer.c >> index cc2d23e..76655e2 100644 >> --- a/kernel/time/timer.c >> +++ b/kernel/time/timer.c >> @@ -487,7 +487,8 @@ static inline void timer_set_idx(struct timer_list *timer, unsigned int idx) >> */ >> static inline unsigned calc_index(unsigned expires, unsigned lvl) >> { >> - expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl); >> + expires = (expires + LVL_GRAN(lvl) - 1) >> LVL_SHIFT(lvl); >> + >> return LVL_OFFS(lvl) + (expires & LVL_MASK); > > This is fundamentally wrong. > > Assume the following scenario: > > base->clk = 1; > timer->expires = 1; > > __internal_add_timer(base, timer) > { > idx = calc_wheel_index(timer->expires, base->clk) > { > delta = expires - clk; > > if (delta < LVL_START(1)) > idx = calc_index(expires, 0) > { > expires = (expires + LVL_GRAN(0) - 1) >> LVL_SHIFT(0); > return LVL_OFFS(0) + (expires & LVL_MASK); > > Now lets use real numbers: > > __internal_add_timer(base, timer) > { > idx = calc_wheel_index(1, 1) > { > delta = 1 - 1; <- 0 > > if (0 < LVL_START(1)) > idx = calc_index(1, 0) > { > expires = (1 + LVL_GRAN(0) - 1) >> LVL_SHIFT(0); > ----> expires = 0 > return LVL_OFFS(0) + (0 & LVL_MASK); > ----> 0 > > So the returned index is 0, which means that the timer will expire in > LVL_SIZE - 1 == 63 ticks. > yes, i missed this case. > The above example is the worst case, but you broke other assumptions as > well. The timer wheel guarantees that a timer armed with: > > mod_timer(timer, jiffies + 1) > > will not fire before aty least one jiffy has elapsed. Let's look at the > time line: > > |-------------------|-------------------|----------------| > tick tick tick > jiffies jiffies + 1 jiffies + 2 > > | | > | Any timer armed | ^ > | here must be | | > | queued here -------------------------| > > in order to guarantee that. Timer wheel timers are not accurate and never > can be. > understand, thanks. > Thanks, > > tglx > > . >