Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3082795pxb; Mon, 16 Nov 2020 05:29:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJxeHdSkGT2IAEE1Q87wd9okQ9x5Ts5mLBLUrBrL+fHGvCbgh0PSjRkSsuGUX4BByjQY9p5P X-Received: by 2002:aa7:de01:: with SMTP id h1mr15403041edv.269.1605533352000; Mon, 16 Nov 2020 05:29:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605533351; cv=none; d=google.com; s=arc-20160816; b=FgUYs/VDy6gH1ic3ujZGO9MCW2ap4EfWtSEmbzb7uHN9wGgsq4OB3bQqiEtDqMT1M6 mPZFFYXIuAjsXvb2ytioPM6LiSH8sUALuEr2HObBkULXxgwMNh6osNAQQPcYiqyD0fJv umFHUjBu1JXMTi7CJfu6R82EUGqpsdUYgzf9/xQMPOjKNV8Jfm2vXRqtDvOV8gRNKmjl 69AZvnCOeNfTAYOQ1hSJHb129J7poc9KnGf1rvqCrGEUizN+97Fon86Du8Z5u8V/mRGs iWlL1B7CRbjVer+2UClZ9/rw3MsSCbk4zEL04nY3ay0l5U/skXyFuqQt8GjUbn2yeF97 rgPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :to:subject; bh=bpAWzEO/ZzMpyr/vbiqVLxy5FYm/gCG7Abqd/4+dpos=; b=IL2bN+Egwq5RzhvHQloXKPcAIbEMW5tsOIePWjGCFS+CGMqpObpT1p2Z73ckUoxBkO gjm06AN3lSh1DV1uupt+IHf4ZhBbYUK5CNRMMIXqrrBUqcbf2FuwYW7NZsXeHWLNUFfu W2YfDNoJCBnZlS9CQaTuhrcIhzYZvVHPYOnuRI7AzbH18QAUPSc5Ny2Zlzuc1uZD9QPq Jizy7948O9bzQ35C1D7fTWYpLDeStNeAZaoIv4a+e2kqfqSqbDBiAFFRTMU72DDwuv3C eemEZpCoAKGZSjmf3stEKNK30TYPzrtxZOUTMD0ykD9cQDUX9Ad8lFDED7N4nvdvwjTf RKEg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rl2si11341850ejb.720.2020.11.16.05.28.49; Mon, 16 Nov 2020 05:29:11 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728682AbgKPNYg (ORCPT + 99 others); Mon, 16 Nov 2020 08:24:36 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:7545 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726615AbgKPNYg (ORCPT ); Mon, 16 Nov 2020 08:24:36 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4CZVCn2y74zhb1C; Mon, 16 Nov 2020 21:24:21 +0800 (CST) Received: from [10.174.176.199] (10.174.176.199) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.487.0; Mon, 16 Nov 2020 21:24:23 +0800 Subject: Re: [PATCH] tick/nohz: Reduce the critical region for jiffies_seq To: Thomas Gleixner , , , , Shiyuan Hu , Hewenliang References: <87h7pq8kyc.fsf@nanos.tec.linutronix.de> <66c172ec-72a1-022a-d387-6c836a698912@huawei.com> <87o8jxzgj7.fsf@nanos.tec.linutronix.de> From: Yunfeng Ye Message-ID: Date: Mon, 16 Nov 2020 21:24:23 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <87o8jxzgj7.fsf@nanos.tec.linutronix.de> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.176.199] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020/11/16 19:29, Thomas Gleixner wrote: > On Mon, Nov 16 2020 at 14:07, Yunfeng Ye wrote: >> On 2020/11/16 3:43, Thomas Gleixner wrote: >>>> and the conflict between jiffies_lock and jiffies_seq increases, >>>> especially in multi-core scenarios. >>> >>> This does not make sense. The sequence counter is updated when holding >>> the lock, so there is no conflict between the lock and the sequence >>> count. >>> >> Yes, there is no conflict between the lock and the sequence count, but >> when tick_do_update_jiffies64() is called one by one, the sequence count >> will be updated, it will affect the latency of tick_nohz_next_event(), >> because the priority of read seqcount is less than writer. > > It's clear to me because I know how that code works, but for someone who > is not familiar with it your description of the problem is confusing at > best. > >> During a tick period, > > 'During a tick period' is misleading. The tick period is the reciprocal > value ot the tick frequency. > > What you want to explain is that if jiffies require an update, i.e. > > now > last_update + period > > then multiple CPUs might contend on it until last_update is forwarded > and the quick check is preventing it again. > Yes, your are right, thanks. >> the tick_do_update_jiffies64() is called concurrency, and the >> time is up to 30+us. so the lockless quick check in tick_do_update_jiffies64() >> cannot intercept all concurrency. > > It cannot catch all of it, right. > > There are quite some other inefficiencies in that code and the seqcount > held time can be reduced way further. Let me stare at it. > I think the write seqcount only protecting the last_jiffies_update/jiffies_64/ tick_next_period is enough. The modification which has not been tested, look like this: diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index f0199a4ba1ad..d5f9930e6bc7 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -66,15 +66,13 @@ static void tick_do_update_jiffies64(ktime_t now) /* Reevaluate with jiffies_lock held */ raw_spin_lock(&jiffies_lock); - write_seqcount_begin(&jiffies_seq); delta = ktime_sub(now, last_jiffies_update); if (delta >= tick_period) { + ktime_t tmp_jiffies_update = + ktime_add(last_jiffies_update, tick_period); delta = ktime_sub(delta, tick_period); - /* Pairs with the lockless read in this function. */ - WRITE_ONCE(last_jiffies_update, - ktime_add(last_jiffies_update, tick_period)); /* Slow path for long timeouts */ if (unlikely(delta >= tick_period)) { @@ -82,21 +80,25 @@ static void tick_do_update_jiffies64(ktime_t now) ticks = ktime_divns(delta, incr); - /* Pairs with the lockless read in this function. */ - WRITE_ONCE(last_jiffies_update, - ktime_add_ns(last_jiffies_update, - incr * ticks)); + tmp_jiffies_update = + ktime_add_ns(tmp_jiffies_update, + incr * ticks); } - do_timer(++ticks); + ticks++; + + write_seqcount_begin(&jiffies_seq); + /* Pairs with the lockless read in this function. */ + WRITE_ONCE(last_jiffies_update, tmp_jiffies_update); + jiffies_64 += ticks; /* Keep the tick_next_period variable up to date */ tick_next_period = ktime_add(last_jiffies_update, tick_period); - } else { write_seqcount_end(&jiffies_seq); + calc_global_load(); + } else { raw_spin_unlock(&jiffies_lock); return; } - write_seqcount_end(&jiffies_seq); raw_spin_unlock(&jiffies_lock); update_wall_time(); } > Thanks, > > tglx > . >