Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5174028imm; Tue, 31 Jul 2018 06:46:25 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcSwcmcOCa+1Y4mh1AHm0FKNvfCWEhiNHiK88lq/x8oS0oY/AO3P5SDs+bmfzJh5qaVH7f7 X-Received: by 2002:a17:902:b08d:: with SMTP id p13-v6mr21090660plr.0.1533044785103; Tue, 31 Jul 2018 06:46:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533044785; cv=none; d=google.com; s=arc-20160816; b=TsWZa4MfdBXzigAZmHFMRlbvwyuWJ/bkDkfDLa14zKgKv8q8NVLr6oSfFX1WxbBjGt rbqt//iURYuSg08n11KiLYavXPDiBsd6n0vO613YCqzvFCe6fdtMN+ey4g3/P2ZrZWCX 3jpypr6xgQB2Vax45bwBqh5nMuoOj6Rf4bpLW+hFiMh+4MjdTURMQsNL9APschn+yvwe e3fGQWlJZqF8C3TtTR7PQlpCAkzBE9AOx/vwSGdFklSZ62l+yZhJ6MI4n1lfXIOFiD+u Nf0pDPY7ARhLPpj5s1mNm4B6WbEPvrC9OJoZIvfPpLeSEydX7OceD0lUX+6iT3tnamWD tsdA== 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=2bj0YmRuXqPzPuVEtr+ej/vJViJsZhsKT/tKZsF0UwE=; b=N2XCLDVwPxOd+cmN0wTnisDC9o2lDGGHhNuvApTCf4U2QcVzw33jWSrbbUKqXhS4HF oecy0eX4iBGQUnFWROcR0hGlb1GrK1jLDKO5UfCfcNoQakVtonwxmvvT2Fz13y3t5K9f itvBvGOSCMQewfHTUW5JNq5VpHRbgizMMKhSYUfgS5VXgZlopgc6RLHyg3xWXql8NRT/ Q5BrCTXy1LjgqxUJ7gpj6kfvIWiAe603RATjy411LJyJOBO5oEtHkG5bbBaEJYr7fxM1 /8ydgnCfPNQA7RnKDjjMhSirdXdX2s6+y7K8gVw90CBD9j7UFD+YPRyOa4P/opvDuxml B+lQ== 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 v123-v6si14153391pfb.324.2018.07.31.06.46.10; Tue, 31 Jul 2018 06:46:25 -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 S1732227AbeGaPZp (ORCPT + 99 others); Tue, 31 Jul 2018 11:25:45 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:10191 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1732127AbeGaPZp (ORCPT ); Tue, 31 Jul 2018 11:25:45 -0400 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 1319CC1C373C2; Tue, 31 Jul 2018 21:45:15 +0800 (CST) Received: from [127.0.0.1] (10.121.91.26) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.399.0; Tue, 31 Jul 2018 21:45:12 +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: Date: Tue, 31 Jul 2018 21:45:11 +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 LVL_GRAN(0) = 1 and LVL_SHIFT(0) = 0 after the calculation, expires = 1 ? > 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. > > 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. > > Thanks, > > tglx > > . >