From patchwork Tue Oct 25 13:58:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10795 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1024140wru; Tue, 25 Oct 2022 07:02:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5MYIIrrrGL5SbcqS9QiKmkWYIEWPhHFOLFZMdnYaCFAjD43AY6OE44XO5nFMQ7Hz+LPVay X-Received: by 2002:a17:906:fe46:b0:73d:939a:ec99 with SMTP id wz6-20020a170906fe4600b0073d939aec99mr33081287ejb.169.1666706530160; Tue, 25 Oct 2022 07:02:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706530; cv=none; d=google.com; s=arc-20160816; b=DN1LqE0I7pfm0lXMeuRUCxV1y0MPeeceF50I5B2vdD7e7S4IVWH8PNQ7kS4L/k0Mmf Ab5WUOVPSkd3igHmC8qsXSKhCki3GG5SNt1jzGiJx0N1lW0jYx/NEp87jBhLMokpxBbq G7KHlF3pNsqhXovpr6bCKL9CyO8Qn7MqqLEwAE/hmUGVLCRYfiWrgQns4xZipyVUq9qi 3QBg8QF2nnMYE34KCP3ayeYw/nDo8n5vx6LUtWpqGpiyIs4gk4cMtpmsx3oSNqUex5aG RfJ1gZiH5UJ/TFL7aijbEvMHNKOUKQP/7uM+i5KTlSaifff3Z3djLw2OaRQO7vFkt69S YCQw== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=JsI07DTqXjWlRwYDoljjsPLtGx2xh4qIy28E4h8N7no=; b=A8ydwKGvedCRfxfLK8rfNUSx0MpqwYN6hiM+m5/jo9J8mkZPH4j1Gjn6c6iq5I0326 YCEJpPyREpBgmDBu/5sLB8JEaqKAcPj8F8vsIbkYFAdSx8+MejT1G9LHPUgT4zb9aVDk KC5Q/zTGINLTuZIrDLHypzO/mx+USPOrtE954HbGcmPSAIfr+M+fwMUkOIkVJLJM7t+s YjhH/GIVYqfzuv8XvsfCbbJw/0o+2hMoxT7au7cITTmKJC9iAG5cCr3qkrRsCv6wbRrE cybYkq1/nwA5CXztg1oHsYhXjwwyhIH8rniQ3cn7ChPGt2h3vwnboHgCcSIpa5g5fzc7 l7gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=T3EsepFn; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s1-20020a056402520100b00461bbc0f917si3254300edd.605.2022.10.25.07.01.34; Tue, 25 Oct 2022 07:02:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=T3EsepFn; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231945AbiJYN7d (ORCPT + 99 others); Tue, 25 Oct 2022 09:59:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231191AbiJYN7Y (ORCPT ); Tue, 25 Oct 2022 09:59:24 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D25561B9CD; Tue, 25 Oct 2022 06:59:22 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JsI07DTqXjWlRwYDoljjsPLtGx2xh4qIy28E4h8N7no=; b=T3EsepFnG1VEPakgQAPv6utfeISI5b9SCb3qu9nw9yeGr8jw8t1zR6Dx6ch73PUNFHVOGL oqi15SnAg6HRSDAkl4rHMmQDmCPs6a9Lv63YmgSPuSPwPNSps9UbplBD71AHpxg2pJhoxM FyKJ6I+g+ewtLZORJV916KEkkleW6We3dblqJb1P61KSpLSm5qD968Gg2KoZJ148/HVXoU TIw427aYJT6plp81tvDQn1+JvmlMVHx6z/OXTHK8T9S81zxHoAeP4HroqZjYTVa+HVrrka zTpa7evXpC5fGDXUF91xC6m66JO487MZ65rfWuv1Y0eNCYhcD7momR1AF1QKSQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JsI07DTqXjWlRwYDoljjsPLtGx2xh4qIy28E4h8N7no=; b=i7qAAP55lL27scwdgwJ3teCam7iQjkFLDDmuZcmI7LTkktrsPSWpugrETm6inYdHUHJQBi M/D/ekZWd+6fl2BQ== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen , "Rafael J . Wysocki" , Viresh Kumar , Michael Ellerman Subject: [PATCH v3 01/17] cpufreq: Prepare timer flags for hierarchical timer pull model Date: Tue, 25 Oct 2022 15:58:34 +0200 Message-Id: <20221025135850.51044-2-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668466599433141?= X-GMAIL-MSGID: =?utf-8?q?1747668466599433141?= Note: This is a proposal only. I was waiting on input how to change this driver properly to use the already existing infrastructure. See therfore the thread on linux-pm mailinglist: https://lore.kernel.org/linux-pm/4c99f34b-40f1-e6cc-2669-7854b615b5fd@linutronix.de/ gpstates timer is the only timer using TIMER_PINNED and TIMER_DEFERRABLE flag. When moving to hierarchical timer pull model, pinned and deferrable timers are stored in separate bases. To ensure gpstates timer always expires on the CPU where it is pinned to, keep only TIMER_PINNED flag and drop TIMER_DEFERRABLE flag. While at it, rewrite comment explaining the rule for timer expiry for the next interval and fix whitespace damages. Signed-off-by: Anna-Maria Behnsen Cc: linux-pm@vger.kernel.org Cc: Rafael J. Wysocki Cc: Viresh Kumar Cc: Michael Ellerman --- drivers/cpufreq/powernv-cpufreq.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/drivers/cpufreq/powernv-cpufreq.c b/drivers/cpufreq/powernv-cpufreq.c index fddbd1ea1635..08d6bd54539d 100644 --- a/drivers/cpufreq/powernv-cpufreq.c +++ b/drivers/cpufreq/powernv-cpufreq.c @@ -640,18 +640,18 @@ static inline int calc_global_pstate(unsigned int elapsed_time, return highest_lpstate_idx + index_diff; } -static inline void queue_gpstate_timer(struct global_pstate_info *gpstates) +static inline void queue_gpstate_timer(struct global_pstate_info *gpstates) { unsigned int timer_interval; /* - * Setting up timer to fire after GPSTATE_TIMER_INTERVAL ms, But - * if it exceeds MAX_RAMP_DOWN_TIME ms for ramp down time. - * Set timer such that it fires exactly at MAX_RAMP_DOWN_TIME - * seconds of ramp down time. + * Timer should expire next time after GPSTATE_TIMER_INTERVAL. If + * the resulting interval (elapsed time + interval) between last + * and next timer expiry is greater than MAX_RAMP_DOWN_TIME, ensure + * it is maximum MAX_RAMP_DOWN_TIME when queueing the next timer. */ if ((gpstates->elapsed_time + GPSTATE_TIMER_INTERVAL) - > MAX_RAMP_DOWN_TIME) + > MAX_RAMP_DOWN_TIME) timer_interval = MAX_RAMP_DOWN_TIME - gpstates->elapsed_time; else timer_interval = GPSTATE_TIMER_INTERVAL; @@ -865,8 +865,7 @@ static int powernv_cpufreq_cpu_init(struct cpufreq_policy *policy) /* initialize timer */ gpstates->policy = policy; - timer_setup(&gpstates->timer, gpstate_timer_handler, - TIMER_PINNED | TIMER_DEFERRABLE); + timer_setup(&gpstates->timer, gpstate_timer_handler, TIMER_PINNED); gpstates->timer.expires = jiffies + msecs_to_jiffies(GPSTATE_TIMER_INTERVAL); spin_lock_init(&gpstates->gpstate_lock); From patchwork Tue Oct 25 13:58:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10792 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1023398wru; Tue, 25 Oct 2022 07:01:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7/1OjE2kcSgCQmAojIsirJokp24h9kA+W/Uv8XmdIjqoBXY3PBPocWawQlUrJRp7AGgn3C X-Received: by 2002:a17:902:70c4:b0:178:54cf:d397 with SMTP id l4-20020a17090270c400b0017854cfd397mr37718578plt.24.1666706481665; Tue, 25 Oct 2022 07:01:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706481; cv=none; d=google.com; s=arc-20160816; b=t5EZRGcYzl0mvpxZmup2ITn9+xECgwDE5CFUKny9wK3qMkp1yJJWQff6j4Igdgl7PJ iq+HLoPKkfQb9ftqX1oltglPqPK4ES0rWF6xdm5tk8daWaBWru6BLgYn94ZurAc1BuLt mEfCsL3KG6wR2jdIwr3FALvGWvtVuV9GtdwqmeTtuezRL3AmWxAJbqLw4Aajz63rsNJ5 4je8oyQzxFlGKmFyFWQ5RUnzdkLW9m6+lkXvij1VF83Qf4qdFcgVV+5W1uSKBA4aWVz1 dwA1juQIMIPvHtL5bed7ZJfXiGt3Aek1+SoNz0afh35PKuX1Oh2zeY9NeDh/7jlil0ev ep0g== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=Qg194zTnM1JYpSXcqxwvHVSZ+y39iQ+5387UWMyWJM0=; b=xVNbhYjW/7Oa4Yf9TpZ4tBB51silLhX0tXfYx9xOt4LyK0pacIbfqQFI3YvBrjGfr9 uwcpRWqsrcfnuRdBqxybhEJzDnTW7MZAwm0ChmTjzaMP0A16+TCjgUpDRkWgrcSB/Ty4 gAfv6wH/HmC9H8SVu2CvpkNUijdWeF6oJwm+goZYfjRWJsPtXorNZqiallOA9Hz8nqzS K01s/kwTVjyd3dk210PQboVclcLf6mGEjBaHGAqfmjzVA7xke4j+6kym7qUsp3FSYOIA DsLbYtzAZI1G1SzNN02CP+8+1SPLJvRrp32sFefwZLsafIg6B4+MCxhjLaIwpM96lwKL 14Mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=i74phoyz; dkim=neutral (no key) header.i=@linutronix.de header.b=dms6+CSW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n2-20020a170902e54200b0017f580f646fsi3398280plf.304.2022.10.25.07.00.39; Tue, 25 Oct 2022 07:01:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=i74phoyz; dkim=neutral (no key) header.i=@linutronix.de header.b=dms6+CSW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231769AbiJYN7a (ORCPT + 99 others); Tue, 25 Oct 2022 09:59:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230158AbiJYN7Y (ORCPT ); Tue, 25 Oct 2022 09:59:24 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CC1917EF36; Tue, 25 Oct 2022 06:59:23 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qg194zTnM1JYpSXcqxwvHVSZ+y39iQ+5387UWMyWJM0=; b=i74phoyzJ529cxBcKmjz7fM1pp+F2ytguqNCUXKngOtOhAl5ikabmbj0G2xsBlh+oMOx7d yZT1v59aOeghY/7nlmWWXhWmxf/CxeZwhAeAzIf7uMtePetZ0wvpUl2UQXIGluFq4PiMoa h/3WVY41ZuhxuacCiTr5dpASf/0BCLRW7xldXsJwqApek8jYwtqCoNhHUSYTbL1XK7fXyj V63XrkfcsCp/s5dwzrDUMwrAGi2uD1def+NDKWvDM/d/2OCme+2uW0JLBlwxHi0V7d+SL2 GssFArUyQNGg3cWKl9NZu4xROF7UlTYGF9Bo43zwvgr0yXUsYCfEpyu4qlgXSA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qg194zTnM1JYpSXcqxwvHVSZ+y39iQ+5387UWMyWJM0=; b=dms6+CSWDUlibiggCdS5Fp5rB/0PriZtqDiV/BUtimWZoMauPplPbqXMjtu3bHU8hadOy7 vP4cY8PJNSmuXnDg== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 02/17] tick-sched: Warn when next tick seems to be in the past Date: Tue, 25 Oct 2022 15:58:35 +0200 Message-Id: <20221025135850.51044-3-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668415675233408?= X-GMAIL-MSGID: =?utf-8?q?1747668415675233408?= When the next tick is in the past, the delta between basemono and the next tick gets negativ. But the next tick should never be in the past. The negative effect of a wrong next tick might be a stop of the tick and timers might expire late. To prevent expensive debugging when changing underlying code, add a WARN_ON_ONCE into this code path. Signed-off-by: Anna-Maria Behnsen Reviewed-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- kernel/time/tick-sched.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index b0e3c9205946..7ffdc7ba19b4 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -826,6 +826,8 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) * If the tick is due in the next period, keep it ticking or * force prod the timer. */ + WARN_ON_ONCE(basemono > next_tick); + delta = next_tick - basemono; if (delta <= (u64)TICK_NSEC) { /* From patchwork Tue Oct 25 13:58:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10793 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1023463wru; Tue, 25 Oct 2022 07:01:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4rWnbV2gic/bk6yp4/VljROWttr+HNrIKP9UNia0zln+NkvN8lBK+/OOAs3emMpycddpdS X-Received: by 2002:a17:90b:1e0c:b0:20f:bcbf:ab5 with SMTP id pg12-20020a17090b1e0c00b0020fbcbf0ab5mr43344352pjb.187.1666706485025; Tue, 25 Oct 2022 07:01:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706485; cv=none; d=google.com; s=arc-20160816; b=iZ1DuaImR/ACYDFWc0eue8J8fg9PGIiwkpkX4D+5bPg7GmfSvAm5whytNmPjcJrhgk S+sowRscTDwuaNRDqOgZxaJY/NCjP9M95sW4lmVGqGVdyI06mg1c3FOAbyoaze1icQgt LhH3qRRJL9KTWU513v1ZJ9q53w689UYNDwuoHx3nsz3w85ActDux5C5aBxXcphzBbE/j Hux6W1Rpx3mH7ZB4ClP0OPoZHXZVWW7sTdwwnczuzHjAV1kJ6YSdscy5/DbGFRH0Ip8V 9FCoQtbm5wF3aTLSmFHCDlDqg8Id4fh7ezlVhDE5ns/mMxLx3meWxx9HndIX2l2mYp4X keKw== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=l3DckWoO10WRg2UnQ17gmGvcP+Cj39tE06wtS471Li8=; b=dPh5uweNIpJZzE5Y2t/+9cP5CH4LimLBTmvMgIS16OgnJkoZlUnY326GkQB4kQoe5H xF803rWeocw3hcoJjnp6Ug3SEzHW+xm6S3T4ZlZY43yT8FwdNXoHGiROlETi0Ol4C/eG KTbRoC31p4a1WXMWTlUMDikek/VPXSj+ZaVNYBB1ySKphOfu78SFHiRljqeuvQIjKbTl PnJhwDlOw6w5ot0cP/1UiSglEKd2hOX3DOpjm1mBHMv0piAb7Zg9SF0DurPBJ05bbZiY AHwZmCbP+IGJ72w/K695An7FwYDh6fnqTe7QWhIoGDqYzAAzypFpxisMw+njoBRjMp6w +Fng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=em6zftEa; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x5-20020a63db45000000b0043cb5d08908si3104225pgi.138.2022.10.25.07.01.09; Tue, 25 Oct 2022 07:01:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=em6zftEa; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232327AbiJYN7t (ORCPT + 99 others); Tue, 25 Oct 2022 09:59:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231492AbiJYN7Z (ORCPT ); Tue, 25 Oct 2022 09:59:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A17AD18A03B; Tue, 25 Oct 2022 06:59:23 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706362; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l3DckWoO10WRg2UnQ17gmGvcP+Cj39tE06wtS471Li8=; b=em6zftEaoVI3HcNeSkFX3GvNS7Mq87YKqxPi6jzPuNuMnsd9hOpgXqU7YxHKnOnE0H2Ufu sMMiJ3fP/8URs7IkpEbXMveDREe9Xgh6xeGoiBBECWHhoneW8CwhTfZ7ZfxLHfNyhDZlj2 byidkY6FCvMadm7QpHIlKqV+AjY7oxRV3K72azJSsMGdosRYL5uomyDcy9PeuyAJ93ZXZs SetjXxk2RaAkHdrysvgQd+ms+lQVxFnRIzqoCkh6xwgcvMsyEVqML/rFdUokeLBy2A9x1J uahbz5O8/T4v4H3Ipi+mvXlTfVD0H6r+Gp17i4lkUIKiC2blUUr0Q5p3xhGKtg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706362; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l3DckWoO10WRg2UnQ17gmGvcP+Cj39tE06wtS471Li8=; b=vp4yKAYpyd4o5LxaF5jjMGHvfVD89NYnRkiFrZ1e/WQoU6DXiDUEbm31y9dPKO9NE6TBLM vFUfK3nSHkMMU3Dw== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 03/17] timer: Move store of next event into __next_timer_interrupt() Date: Tue, 25 Oct 2022 15:58:36 +0200 Message-Id: <20221025135850.51044-4-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668418999273730?= X-GMAIL-MSGID: =?utf-8?q?1747668418999273730?= Both call sides of __next_timer_interrupt() store the return value directly in base->next_expiry. Move the store into __next_timer_interrupt(). No functional change. Signed-off-by: Anna-Maria Behnsen Reviewed-by: Thomas Gleixner --- kernel/time/timer.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 717fcb9fb14a..7695c733dfa5 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1571,8 +1571,10 @@ static int next_pending_bucket(struct timer_base *base, unsigned offset, /* * Search the first expiring timer in the various clock levels. Caller must * hold base->lock. + * + * Store next expiry time in base->next_expiry. */ -static unsigned long __next_timer_interrupt(struct timer_base *base) +static void __next_timer_interrupt(struct timer_base *base) { unsigned long clk, next, adj; unsigned lvl, offset = 0; @@ -1638,10 +1640,11 @@ static unsigned long __next_timer_interrupt(struct timer_base *base) clk += adj; } + base->next_expiry = next; base->next_expiry_recalc = false; base->timers_pending = !(next == base->clk + NEXT_TIMER_MAX_DELTA); - return next; + return; } #ifdef CONFIG_NO_HZ_COMMON @@ -1701,7 +1704,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) raw_spin_lock(&base->lock); if (base->next_expiry_recalc) - base->next_expiry = __next_timer_interrupt(base); + __next_timer_interrupt(base); nextevt = base->next_expiry; /* @@ -1784,7 +1787,7 @@ static inline void __run_timers(struct timer_base *base) WARN_ON_ONCE(!levels && !base->next_expiry_recalc && base->timers_pending); base->clk++; - base->next_expiry = __next_timer_interrupt(base); + __next_timer_interrupt(base); while (levels--) expire_timers(base, heads + levels); From patchwork Tue Oct 25 13:58:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10803 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1025202wru; Tue, 25 Oct 2022 07:03:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7lpk0+beNfzjkEr3Suo/RkGnqsU2ORceBj8x7Q8jWeAa2mNg2BA9Gpcl9eIU3dT5YwjQ3u X-Received: by 2002:a17:907:7f92:b0:78d:ed9c:d86f with SMTP id qk18-20020a1709077f9200b0078ded9cd86fmr32711088ejc.251.1666706629541; Tue, 25 Oct 2022 07:03:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706629; cv=none; d=google.com; s=arc-20160816; b=gLazfCbpYLKHkFblxsnYfkamPasAolXNdE0KQvwPrSX4evSRgHw0fgHzcRULHXXZFw 2Uc3U800APVpWeNajp70i21QhIfLRmReqjT/x8pvGMdYYaiNaeikHDcSl4UK2cgY34Lt 8K4Lw/u7oBXko0sqdWAGIZNljOnoCCYD7zvPpINvLorAuSUtqNajneuLOkUYFIfywN45 DRuOMe1vKiu/K0WAUVlxC+34JLULb7OThEvZcD9ZApzSu6ZdhhBifcU5EEO528YGU6P2 oOFk8Pv0LMmg4YYYxzy07hQt4PlM6zaNFOBP515K2fsCNWLoKj/ENI0iQ+tVMcIl/gAu 7HyA== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=I9KrkHTSRk9elfiy6IejGCtNRlCE++/FB5CiuBByAlk=; b=sDjQ1fz49lwSq7Vv11CxScyLQojYOB8bu1NVkSI4DYKcHUmgJHbtJcGFbmu4AZOjNy 3VTuK07DdTdvvLfwgstPvnngqMwWM57Fk8T29VUP85iy4gAyN7p5QDmDnkHsCAmWj0aO 33c1edrMFQ4BV2mnT5ICmKLjXQtV8uYDlVt+YVAakWNC4xLcR3V7WgO5H/g2HXXpbz9H tCD4SinAzjit8Fg9i5T70yOqHWuoHgBboZCB/GBvAU56z8fYmre9Jb3Byh65AmLx2ZIV 7wK3pVUMYKTxe7yDlmIalRS4hXw5Ena09xgPcR0sPDI+JTywcdcG6hvTcnNQm2+FYecD zFzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="Tf/9SYQH"; dkim=neutral (no key) header.i=@linutronix.de header.b=OOxPYlfB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d23-20020aa7c1d7000000b0046194b2dd53si2675490edp.119.2022.10.25.07.03.24; Tue, 25 Oct 2022 07:03:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="Tf/9SYQH"; dkim=neutral (no key) header.i=@linutronix.de header.b=OOxPYlfB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232288AbiJYN7o (ORCPT + 99 others); Tue, 25 Oct 2022 09:59:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231526AbiJYN7Z (ORCPT ); Tue, 25 Oct 2022 09:59:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 371C13BB; Tue, 25 Oct 2022 06:59:24 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706362; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=I9KrkHTSRk9elfiy6IejGCtNRlCE++/FB5CiuBByAlk=; b=Tf/9SYQHSN3IMkZcm9kCZ3W33EouF/PiE/b3UwATQokC0EG5MUmZFfaAaIw39OPEAGAF5q iQr76ZtsG8TfY63OCJJ3O9/tzgpnPH07vtmiA6dFcZC+Zyz2G9+V6BNpVSUU13v2T/+fxv 9nM/6qUrpyyQz2Ig9L0SRTlljVnV2GkKK8t+BTNSU/awtJpdbuAC+MIpV7a/G25MbhV/QP IU0hjTK/jGQbmg3rOHBUHtaK6HRgecu0ArBIpH/i01KKv4LXgf1/cyqxu2a4UyXAdPVykw 1ZhmDzeWXqPKo1EArrzyXdnWCBaaBnFQ/8TC5YsmdQYrIIqVEq/L4DeGGba4pg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706362; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=I9KrkHTSRk9elfiy6IejGCtNRlCE++/FB5CiuBByAlk=; b=OOxPYlfB3OREmJmPIE3ZC4Fnc8ctFR4lOH9DY2WH8sgffZSKb7Qg8OSTwLzHciSDREJlBN tiS+6dl2kh8Hd1Bg== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 04/17] timer: Split next timer interrupt logic Date: Tue, 25 Oct 2022 15:58:37 +0200 Message-Id: <20221025135850.51044-5-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668570563585883?= X-GMAIL-MSGID: =?utf-8?q?1747668570563585883?= Logic for getting next timer interrupt (no matter of recalculated or already stored in base->next_expiry) is split into a separate function "next_timer_interrupt()" to make it available for new call sites. No functional change. Signed-off-by: Anna-Maria Behnsen Reviewed-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- kernel/time/timer.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 7695c733dfa5..f34bc75ff848 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1681,6 +1681,15 @@ static u64 cmp_next_hrtimer_event(u64 basem, u64 expires) return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC; } + +static unsigned long next_timer_interrupt(struct timer_base *base) +{ + if (base->next_expiry_recalc) + __next_timer_interrupt(base); + + return base->next_expiry; +} + /** * get_next_timer_interrupt - return the time (clock mono) of the next timer * @basej: base time jiffies @@ -1703,9 +1712,8 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) return expires; raw_spin_lock(&base->lock); - if (base->next_expiry_recalc) - __next_timer_interrupt(base); - nextevt = base->next_expiry; + + nextevt = next_timer_interrupt(base); /* * We have a fresh next event. Check whether we can forward the From patchwork Tue Oct 25 13:58:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10799 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1024565wru; Tue, 25 Oct 2022 07:02:56 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5CdPlEz9a2Cc7BJY5DpsvDju1/wpWHRQKeq6VU2k1ft0IqYoAuT4igSoR4bMWguQYAsx+g X-Received: by 2002:a17:90b:2306:b0:212:533:3188 with SMTP id mt6-20020a17090b230600b0021205333188mr32755171pjb.210.1666706576368; Tue, 25 Oct 2022 07:02:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706576; cv=none; d=google.com; s=arc-20160816; b=0T4GThZwktD7hyRQrv4eZQDUNDjvBzSyjcVOW/gNohlSiiXJrmargsHNi5QZE8vhJ1 hPXtZtUPoFZmMlsC2M1CPrEIURboWOKMcmKP9pQ+iBfrX4wQD0fnO8cYo4787SvHbvXS zlChdI8YhGuoqWF9jsMTdAQGkOWR6GbqY/1G5e2P8IJYmmZGvXU37Fga+oiT9AzwZxgI TcsQF+IQ/sxCkWJluJO4WD/Jn5QwTef4A000nt8aDPMFTOiC1tuKmhw5UySlmcH0DKgI xfqZEKFEqo7MJO/21x5U8soZtlzA6G2iBhZW1ciDzERGlQxAH0WLcg9DothXMQXoGKtV cbbw== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=vvmb4wfRRA26uf1ExR6XWWcWf4ly6fbBq4i3VJ/TCEY=; b=LrfSubqICbKjVgvbsUxX07mIdaezCJI2Ik0NiLe4+1a7JcV98I7x0U8TKHLpwTBNCK gP2JP3hJUXT87ylJ1v5eCDJT4TTOm9JBW0mw6Y/4UZoNKS/unZqgrZr7uBJhczMkpJ5b UxAbKbo6Z56I4rAkkP0YgvOWyxBNf8ftnvgvq/U5O5LYVvjhGqVVQxzlHBMdKV+DYnbX LtJvSCbDhBaPlLxPrNQz1wavoDbMXlaCX9a5zpDwAxIr5Z3genDIbHJ6KnO9zDatycje Clz4dY3NWu7dY5lZMgBAmSHSyQ78ffRQ8FotVN65uQT8Qqau+YbPjMYvCIkQSEPLU1HW muFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=LwzQPpX+; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h17-20020a170902f55100b00186b6a32284si3395790plf.548.2022.10.25.07.02.39; Tue, 25 Oct 2022 07:02:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=LwzQPpX+; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232296AbiJYN7x (ORCPT + 99 others); Tue, 25 Oct 2022 09:59:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231546AbiJYN70 (ORCPT ); Tue, 25 Oct 2022 09:59:26 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3917A29C; Tue, 25 Oct 2022 06:59:25 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706363; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vvmb4wfRRA26uf1ExR6XWWcWf4ly6fbBq4i3VJ/TCEY=; b=LwzQPpX+Ny/PxpUuI5+8qcVrqQfT+WjE7/NENxhUShNmOEw/iTf37CJ4FwRBqsuzl/u5z+ wnhOmVBSHuvvQyowXC9m+wb5s2TtJpaDZsDxGrXBFbncS/SR0qiDtqA+1c8pv+rwlX9rfm aKX3hwqW3fvLBrefg8HwPjql35faDPU5ouq3uzKL5MPAVoxJLVIEeSj2Pgi26Jjk/kIKG7 eEX9DHBUgsGP6QEHMDXA0SIbTKgCLNyh0PJSXLS9Q2AxQ0fmesw+KOHPOCPl+ANDsd6Tw8 jjKvOFiZGe8GwKADdu1P0m0wi7qVYiwGlix0MqBMBM89OEmMaLhlzh/DYDe1LQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706363; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vvmb4wfRRA26uf1ExR6XWWcWf4ly6fbBq4i3VJ/TCEY=; b=8N2pUT0OaCAesECVu/HpMphD9DDy2spXcuoPHDFPo3FjS0DZYJm78WHzj9wdoRKnl4Q9t9 /tIBD5wqb5wMx+Cg== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 05/17] timer: Rework idle logic Date: Tue, 25 Oct 2022 15:58:38 +0200 Message-Id: <20221025135850.51044-6-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668514898907104?= X-GMAIL-MSGID: =?utf-8?q?1747668514898907104?= From: Thomas Gleixner To improve readability of the code, split base->idle calculation and expires calculation into separate parts. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen Reviewed-by: Frederic Weisbecker --- kernel/time/timer.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index f34bc75ff848..cb4194ecca60 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1727,21 +1727,20 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) base->clk = nextevt; } - if (time_before_eq(nextevt, basej)) { - expires = basem; - base->is_idle = false; - } else { - if (base->timers_pending) - expires = basem + (u64)(nextevt - basej) * TICK_NSEC; - /* - * If we expect to sleep more than a tick, mark the base idle. - * Also the tick is stopped so any added timer must forward - * the base clk itself to keep granularity small. This idle - * logic is only maintained for the BASE_STD base, deferrable - * timers may still see large granularity skew (by design). - */ - if ((expires - basem) > TICK_NSEC) - base->is_idle = true; + /* + * Base is idle if the next event is more than a tick away. Also + * the tick is stopped so any added timer must forward the base clk + * itself to keep granularity small. This idle logic is only + * maintained for the BASE_STD base, deferrable timers may still + * see large granularity skew (by design). + */ + base->is_idle = time_after(nextevt, basej + 1); + + if (base->timers_pending) { + /* If we missed a tick already, force 0 delta */ + if (time_before_eq(nextevt, basej)) + nextevt = basej; + expires = basem + (u64)(nextevt - basej) * TICK_NSEC; } raw_spin_unlock(&base->lock); From patchwork Tue Oct 25 13:58:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10805 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1025409wru; Tue, 25 Oct 2022 07:04:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM70flcKm29hEe9zSlE3gQAETmzVkqpan1FQb01dUbauX7EFTGfeMfbMXU1R2tpOiX/zkefK X-Received: by 2002:a05:6a00:1312:b0:536:fefd:e64a with SMTP id j18-20020a056a00131200b00536fefde64amr38762598pfu.26.1666706645338; Tue, 25 Oct 2022 07:04:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706645; cv=none; d=google.com; s=arc-20160816; b=gxZLgy8ZpZH/xjpLSuyXcEuykFoZHD2Zf3HRIociHHwm8RqWIdApFAF0Ddw3UxGueV TIs8onI4PwDpHkRnZ7awaHha7sZGR1DoU2nsSeSb1KvM0S+43f/dnj3yN0/t2D4tjVuD /XpReopl3K2d+iMl+UiHmv4Oxas5rwUu+MyFak84GzBsTkKwsUEq/m16UzQZD6lp5R5A nTvud07H4bX1S8e6rP3WzKU/y0qdztYQnJ80lIqmrMVcAqs2Rfpqkn6MNu1dWQBwBYcJ 0mXb01XwSYCZvkJominN/EbT5UxjxxcnKsiGIOAqlBykvEfk+R8aHZdIYhhJTL4E1yFq 0olQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=x46JYBFTO1X/bJKO4tug7/7x9UDRQaxrzWXN6ho7ju4=; b=ri9mXiPzochYUcSURhOrxgRfMRBv02H+kKlImB8T1pU1PIwRi1x6qeMT/VmYFA14sD VlUMG5e2YY5cLnhd7ebV+YxoVxvuB+N9xsk3oaJLrECzt/4lnzk7qufg6iqAd1DUhI0T Zzst30ruyII0/Zf3kffu95s1B60++okgt6+m8gOwNVIb3bBeDyg4VrwwxhOFWu2gNAFp CYh9IsopH4TWiPt/uCtG2k8Wa1WbbkQRRn+OmRvqszoi7Tphl56NzOgtKk3GSY/QX6X9 nyJDlMg26vckncaxnBZz2bRy/lFHk7qcy7nwTP0QC6hSjHUWtJPseId8oSwvpGfziLIj kE4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BZ9FSMHi; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s9-20020a63d049000000b0046408c6d349si2977084pgi.478.2022.10.25.07.03.49; Tue, 25 Oct 2022 07:04:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BZ9FSMHi; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232483AbiJYN76 (ORCPT + 99 others); Tue, 25 Oct 2022 09:59:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231801AbiJYN7a (ORCPT ); Tue, 25 Oct 2022 09:59:30 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 982972C7; Tue, 25 Oct 2022 06:59:25 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x46JYBFTO1X/bJKO4tug7/7x9UDRQaxrzWXN6ho7ju4=; b=BZ9FSMHiqTVkwnPyfP7hBRki0zdIzjsksJ/AdWXQA8VJXkVr4YyI82kSznMZcNPEQBEtdu N6Q21sTkpJhpZJxgKK5Hh1nzOZnW2SfuL5qhq1TbN+kAJI1ITuCAS0hIdf8OC0EFy0shHf jd6qJ4ySunva5bs64xSNxLMF6bEtY0vx/3hhm3kv6beYkoLQ55avLqdeqozi4EWPdrjQbk uElV227jngkbOs6GAWHRe6ToSwFV9QosOCg9foEbRF3FUcGvuGCDUGL055sL6S+m4wC3wR yrkBWcrMpjkbTLsZoBKvCl8IiEB1MbK1NPZ8COBioErA9utQyywMV2+A/5sXqA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x46JYBFTO1X/bJKO4tug7/7x9UDRQaxrzWXN6ho7ju4=; b=hBj8RzG3hZ+zE1Oay8C9CZq39/On6KUzfUwuERExfFtyF1/Nfm5wm0cmKrfn/xj/4B/mCv ZMG1pDtndboAgNCw== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen , Richard Cochran Subject: [PATCH v3 06/17] timer: Keep the pinned timers separate from the others Date: Tue, 25 Oct 2022 15:58:39 +0200 Message-Id: <20221025135850.51044-7-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668587461529097?= X-GMAIL-MSGID: =?utf-8?q?1747668587461529097?= Seperate the storage space for pinned timers. This is preparatory work for changing the NOHZ timer placement from a push at enqueue time to a pull at expiry time model. No functional change. Originally-by: Richard Cochran (linutronix GmbH) Signed-off-by: Anna-Maria Behnsen Reviewed-by: Frederic Weisbecker --- kernel/time/timer.c | 109 ++++++++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 28 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index cb4194ecca60..b3eea90cb212 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -187,12 +187,18 @@ EXPORT_SYMBOL(jiffies_64); #define WHEEL_SIZE (LVL_SIZE * LVL_DEPTH) #ifdef CONFIG_NO_HZ_COMMON -# define NR_BASES 2 -# define BASE_STD 0 -# define BASE_DEF 1 +/* + * If multiple bases need to be locked, use the base ordering for lock + * nesting, i.e. lowest number first. + */ +# define NR_BASES 3 +# define BASE_LOCAL 0 +# define BASE_GLOBAL 1 +# define BASE_DEF 2 #else # define NR_BASES 1 -# define BASE_STD 0 +# define BASE_LOCAL 0 +# define BASE_GLOBAL 0 # define BASE_DEF 0 #endif @@ -902,7 +908,10 @@ static int detach_if_pending(struct timer_list *timer, struct timer_base *base, static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu) { - struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu); + int index = tflags & TIMER_PINNED ? BASE_LOCAL : BASE_GLOBAL; + struct timer_base *base; + + base = per_cpu_ptr(&timer_bases[index], cpu); /* * If the timer is deferrable and NO_HZ_COMMON is set then we need @@ -915,7 +924,10 @@ static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu) static inline struct timer_base *get_timer_this_cpu_base(u32 tflags) { - struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); + int index = tflags & TIMER_PINNED ? BASE_LOCAL : BASE_GLOBAL; + struct timer_base *base; + + base = this_cpu_ptr(&timer_bases[index]); /* * If the timer is deferrable and NO_HZ_COMMON is set then we need @@ -1700,9 +1712,10 @@ static unsigned long next_timer_interrupt(struct timer_base *base) */ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) { - struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); + unsigned long nextevt, nextevt_local, nextevt_global; + struct timer_base *base_local, *base_global; + bool local_first, is_idle; u64 expires = KTIME_MAX; - unsigned long nextevt; /* * Pretend that there is no timer pending if the cpu is offline. @@ -1711,38 +1724,69 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) if (cpu_is_offline(smp_processor_id())) return expires; - raw_spin_lock(&base->lock); + base_local = this_cpu_ptr(&timer_bases[BASE_LOCAL]); + base_global = this_cpu_ptr(&timer_bases[BASE_GLOBAL]); - nextevt = next_timer_interrupt(base); + raw_spin_lock(&base_local->lock); + raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING); + + nextevt_local = next_timer_interrupt(base_local); + nextevt_global = next_timer_interrupt(base_global); /* * We have a fresh next event. Check whether we can forward the * base. We can only do that when @basej is past base->clk * otherwise we might rewind base->clk. */ - if (time_after(basej, base->clk)) { - if (time_after(nextevt, basej)) - base->clk = basej; - else if (time_after(nextevt, base->clk)) - base->clk = nextevt; + if (time_after(basej, base_local->clk)) { + if (time_after(nextevt_local, basej)) + base_local->clk = basej; + else if (time_after(nextevt_local, base_local->clk)) + base_local->clk = nextevt_local; + } + + if (time_after(basej, base_global->clk)) { + if (time_after(nextevt_global, basej)) + base_global->clk = basej; + else if (time_after(nextevt_global, base_global->clk)) + base_global->clk = nextevt_global; } /* - * Base is idle if the next event is more than a tick away. Also + * Check whether the local event is expiring before or at the same + * time as the global event. + * + * Note, that nextevt_global and nextevt_local might be based on + * different base->clk values. So it's not guaranteed that + * comparing with empty bases results in a correct local_first. + */ + if (base_local->timers_pending && base_global->timers_pending) + local_first = time_before_eq(nextevt_local, nextevt_global); + else + local_first = base_local->timers_pending; + + nextevt = local_first ? nextevt_local : nextevt_global; + + /* + * Bases are idle if the next event is more than a tick away. Also * the tick is stopped so any added timer must forward the base clk * itself to keep granularity small. This idle logic is only - * maintained for the BASE_STD base, deferrable timers may still - * see large granularity skew (by design). + * maintained for the BASE_LOCAL and BASE_GLOBAL base, deferrable + * timers may still see large granularity skew (by design). */ - base->is_idle = time_after(nextevt, basej + 1); + is_idle = time_after(nextevt, basej + 1); + + /* We need to mark both bases in sync */ + base_local->is_idle = base_global->is_idle = is_idle; - if (base->timers_pending) { + if (base_local->timers_pending || base_global->timers_pending) { /* If we missed a tick already, force 0 delta */ if (time_before_eq(nextevt, basej)) nextevt = basej; expires = basem + (u64)(nextevt - basej) * TICK_NSEC; } - raw_spin_unlock(&base->lock); + raw_spin_unlock(&base_global->lock); + raw_spin_unlock(&base_local->lock); return cmp_next_hrtimer_event(basem, expires); } @@ -1754,7 +1798,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) */ void timer_clear_idle(void) { - struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); + struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_LOCAL]); /* * We do this unlocked. The worst outcome is a remote enqueue sending @@ -1763,6 +1807,9 @@ void timer_clear_idle(void) * the lock in the exit from idle path. */ base->is_idle = false; + + base = this_cpu_ptr(&timer_bases[BASE_GLOBAL]); + base->is_idle = false; } #endif @@ -1808,11 +1855,13 @@ static inline void __run_timers(struct timer_base *base) */ static __latent_entropy void run_timer_softirq(struct softirq_action *h) { - struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); + struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_LOCAL]); __run_timers(base); - if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) + if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) { + __run_timers(this_cpu_ptr(&timer_bases[BASE_GLOBAL])); __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF])); + } } /* @@ -1820,17 +1869,21 @@ static __latent_entropy void run_timer_softirq(struct softirq_action *h) */ static void run_local_timers(void) { - struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); + struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_LOCAL]); hrtimer_run_queues(); /* Raise the softirq only if required. */ if (time_before(jiffies, base->next_expiry)) { if (!IS_ENABLED(CONFIG_NO_HZ_COMMON)) return; - /* CPU is awake, so check the deferrable base. */ + /* CPU is awake, so check for the global base. */ base++; - if (time_before(jiffies, base->next_expiry)) - return; + if (time_before(jiffies, base->next_expiry)) { + /* CPU is awake, so check the deferrable base. */ + base++; + if (time_before(jiffies, base->next_expiry)) + return; + } } raise_softirq(TIMER_SOFTIRQ); } From patchwork Tue Oct 25 13:58:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10800 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1024604wru; Tue, 25 Oct 2022 07:03:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6amYee81KjyA/kFm+d+ZuQOyfPRqYf7zvYQeOkGbi5ehHuJx7hI0baexy4wiUd0DsMpo0O X-Received: by 2002:a17:902:e74f:b0:186:a981:2334 with SMTP id p15-20020a170902e74f00b00186a9812334mr10354812plf.88.1666706580427; Tue, 25 Oct 2022 07:03:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706580; cv=none; d=google.com; s=arc-20160816; b=ksesBLW966P+kJtg4LKPM9kWh6ezgI+q1O0lKZJJ+Zu1l8ekHk5JRN41am9u+dbEXA bu/3f4o627lbej1CJLJORyZaIJjI1nMJSPQNSezcZWZfqIvI45CZozuIty2tnpMsF+u4 nydMV/ejPjVXVFmw9JaKW5lmdcl/FKquKyiy2B2YLaCPCR1OsMXD+yy+oxTlIlaxu72u 3uIR0l1Fx9fY9kKAteajs1r8hnVCh3ibDBWogxHeF+U5Kaz9iqZaqhjE6qFC4n4srCyj SYq2KF3Wsl8rRYu5SBzJPPhiKp3SDlh1sN8XeD7DjaIhc6HEz0jiM6+5wCECOSunlUWk NReg== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=bEYRtSQZY6jeCn9Fg5wiaPDLLW9bMvPs60Ugkh6xZUM=; b=ldSjXg/vS0zCUvrQ1J4G7NDbBAf1CZZwzC2G1s+XA8caWmWn6Nvb4sxc4plhBDg7Qk 22GfPpl7xZNCesVVMHlxYj8Wj+WVLVLQ2xf8/Hfn8LYHsrP5qVY4lo5sILQgcdrCDqa1 pdxGYHPZNAankKK6wiXrMC6m14EzdQWn2JbM+sX+jTFcGSDcf1mW1qGHv2C5ITuZwJYx crICRB+9r9VCMgeBiwM+JeUjiB+FB1+gWDwoPKbiw7hSV6n3+juhcBn4MQ8hEwU2V+tC WXuGI7jEcmNT8agpYZheOZ8oYRe0ieP+jgGyrWhNtO2lwX3ZvcRsbs3D8Miv6CAKctL5 gLSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BJZKlLp2; dkim=neutral (no key) header.i=@linutronix.de header.b=fgQwUeSi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n11-20020a654ccb000000b00461bf5f86fdsi2975598pgt.403.2022.10.25.07.02.45; Tue, 25 Oct 2022 07:03:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BJZKlLp2; dkim=neutral (no key) header.i=@linutronix.de header.b=fgQwUeSi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231315AbiJYOAI (ORCPT + 99 others); Tue, 25 Oct 2022 10:00:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231796AbiJYN7a (ORCPT ); Tue, 25 Oct 2022 09:59:30 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0ECDB23; Tue, 25 Oct 2022 06:59:25 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bEYRtSQZY6jeCn9Fg5wiaPDLLW9bMvPs60Ugkh6xZUM=; b=BJZKlLp2fBwdYyOnyjBirLcT1R3s1L0dw5UkQHCoKz6k0UuaYvdI7xogqsF5iJmM2t1Zzu GIHpuTpZn8DjdrFKVmsAP92ivrwqwzjXgI6Re737SBFgRdYncJatpEQxjVze1W6Tx8Acwc d6RzrjlXI4IruX6O+wVURAY/CGKmDRl5NDkr1WbxkrLRd/d3rslj8vHIJcYdRTqQOHvuag bRYhV2PRE+dTAjRnx2+JsoHP3KVyBNZ59unMeHTlpNA/UJEVfl38vnkSfqiVXvCKkm6vMf p1eCxjOBfK3iC1W3l0yOFAMjoxCfJhuDZaPRquSbVS6kiR1Gjy5FiXsP9ShLeA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bEYRtSQZY6jeCn9Fg5wiaPDLLW9bMvPs60Ugkh6xZUM=; b=fgQwUeSi6EU55H15YYflIBCOdeSxfE/YqvZJuu8IWanCQJRN8Le1041TeRnMB6JHRexQPh zWKHrGPCAIcc8HAw== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen , Richard Cochran Subject: [PATCH v3 07/17] timer: Retrieve next expiry of pinned/non-pinned timers seperately Date: Tue, 25 Oct 2022 15:58:40 +0200 Message-Id: <20221025135850.51044-8-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668519326534692?= X-GMAIL-MSGID: =?utf-8?q?1747668519326534692?= For the conversion of the NOHZ timer placement to a pull at expiry time model it's required to have seperate expiry times for the pinned and the non-pinned (movable) timers. Therefore struct timer_events is introduced. No functional change Originally-by: Richard Cochran (linutronix GmbH) Signed-off-by: Anna-Maria Behnsen --- kernel/time/tick-internal.h | 8 ++++- kernel/time/tick-sched.c | 20 ++++++++---- kernel/time/timer.c | 65 ++++++++++++++++++++++++++++--------- 3 files changed, 70 insertions(+), 23 deletions(-) diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h index 649f2b48e8f0..fcb2d45c2934 100644 --- a/kernel/time/tick-internal.h +++ b/kernel/time/tick-internal.h @@ -8,6 +8,11 @@ #include "timekeeping.h" #include "tick-sched.h" +struct timer_events { + u64 local; + u64 global; +}; + #ifdef CONFIG_GENERIC_CLOCKEVENTS # define TICK_DO_TIMER_NONE -1 @@ -163,7 +168,8 @@ static inline void timers_update_nohz(void) { } DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases); -extern u64 get_next_timer_interrupt(unsigned long basej, u64 basem); +extern void get_next_timer_interrupt(unsigned long basej, u64 basem, + struct timer_events *tevt); void timer_clear_idle(void); #define CLOCK_SET_WALL \ diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 7ffdc7ba19b4..78f172d1f3d2 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -784,7 +784,8 @@ static inline bool local_timer_softirq_pending(void) static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) { - u64 basemono, next_tick, delta, expires; + struct timer_events tevt = { .local = KTIME_MAX, .global = KTIME_MAX }; + u64 basemono, delta, expires; unsigned long basejiff; unsigned int seq; @@ -809,7 +810,11 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) */ if (rcu_needs_cpu() || arch_needs_cpu() || irq_work_needs_cpu() || local_timer_softirq_pending()) { - next_tick = basemono + TICK_NSEC; + /* + * If anyone needs the CPU, treat this as a local + * timer expiring in a jiffy. + */ + tevt.local = basemono + TICK_NSEC; } else { /* * Get the next pending timer. If high resolution @@ -818,17 +823,18 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) * disabled this also looks at the next expiring * hrtimer. */ - next_tick = get_next_timer_interrupt(basejiff, basemono); - ts->next_timer = next_tick; + get_next_timer_interrupt(basejiff, basemono, &tevt); + tevt.local = min_t(u64, tevt.local, tevt.global); + ts->next_timer = tevt.local; } /* * If the tick is due in the next period, keep it ticking or * force prod the timer. */ - WARN_ON_ONCE(basemono > next_tick); + WARN_ON_ONCE(basemono > tevt.local); - delta = next_tick - basemono; + delta = tevt.local - basemono; if (delta <= (u64)TICK_NSEC) { /* * Tell the timer code that the base is not idle, i.e. undo @@ -861,7 +867,7 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) else expires = KTIME_MAX; - ts->timer_expires = min_t(u64, expires, next_tick); + ts->timer_expires = min_t(u64, expires, tevt.local); out: return ts->timer_expires; diff --git a/kernel/time/timer.c b/kernel/time/timer.c index b3eea90cb212..069478e65230 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1664,7 +1664,7 @@ static void __next_timer_interrupt(struct timer_base *base) * Check, if the next hrtimer event is before the next timer wheel * event: */ -static u64 cmp_next_hrtimer_event(u64 basem, u64 expires) +static void cmp_next_hrtimer_event(u64 basem, struct timer_events *tevt) { u64 nextevt = hrtimer_get_next_event(); @@ -1672,15 +1672,17 @@ static u64 cmp_next_hrtimer_event(u64 basem, u64 expires) * If high resolution timers are enabled * hrtimer_get_next_event() returns KTIME_MAX. */ - if (expires <= nextevt) - return expires; + if (tevt->local <= nextevt) + return; /* * If the next timer is already expired, return the tick base * time so the tick is fired immediately. */ - if (nextevt <= basem) - return basem; + if (nextevt <= basem) { + tevt->local = basem; + return; + } /* * Round up to the next jiffie. High resolution timers are @@ -1690,7 +1692,7 @@ static u64 cmp_next_hrtimer_event(u64 basem, u64 expires) * * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3 */ - return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC; + tevt->local = DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC; } @@ -1703,26 +1705,31 @@ static unsigned long next_timer_interrupt(struct timer_base *base) } /** - * get_next_timer_interrupt - return the time (clock mono) of the next timer + * get_next_timer_interrupt * @basej: base time jiffies * @basem: base time clock monotonic + * @tevt: Pointer to the storage for the expiry values * - * Returns the tick aligned clock monotonic time of the next pending - * timer or KTIME_MAX if no timer is pending. + * Stores the next pending local and global timer expiry values in the + * struct pointed to by @tevt. If a queue is empty the corresponding field + * is set to KTIME_MAX. */ -u64 get_next_timer_interrupt(unsigned long basej, u64 basem) +void get_next_timer_interrupt(unsigned long basej, u64 basem, + struct timer_events *tevt) { unsigned long nextevt, nextevt_local, nextevt_global; struct timer_base *base_local, *base_global; bool local_first, is_idle; - u64 expires = KTIME_MAX; + + /* Preset local / global events */ + tevt->local = tevt->global = KTIME_MAX; /* * Pretend that there is no timer pending if the cpu is offline. * Possible pending timers will be migrated later to an active cpu. */ if (cpu_is_offline(smp_processor_id())) - return expires; + return; base_local = this_cpu_ptr(&timer_bases[BASE_LOCAL]); base_global = this_cpu_ptr(&timer_bases[BASE_GLOBAL]); @@ -1779,16 +1786,44 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) /* We need to mark both bases in sync */ base_local->is_idle = base_global->is_idle = is_idle; - if (base_local->timers_pending || base_global->timers_pending) { + /* + * If the bases are not marked idle, i.e one of the events is at + * max. one tick away, then the CPU can't go into a NOHZ idle + * sleep. Use the earlier event of both and store it in the local + * expiry value. The next global event is irrelevant in this case + * and can be left as KTIME_MAX. CPU will wakeup on time. + */ + if (!is_idle) { /* If we missed a tick already, force 0 delta */ if (time_before_eq(nextevt, basej)) nextevt = basej; - expires = basem + (u64)(nextevt - basej) * TICK_NSEC; + tevt->local = basem + (u64)(nextevt - basej) * TICK_NSEC; + goto unlock; } + + /* + * If the bases are marked idle, i.e. the next event on both the + * local and the global queue are farther away than a tick, + * evaluate both bases. No need to check whether one of the bases + * has an already expired timer as this is caught by the !is_idle + * condition above. + */ + if (base_local->timers_pending) + tevt->local = basem + (u64)(nextevt_local - basej) * TICK_NSEC; + + /* + * If the local queue expires first, then the global event can be + * ignored. The CPU wakes up before that. If the global queue is + * empty, nothing to do either. + */ + if (!local_first && base_global->timers_pending) + tevt->global = basem + (u64)(nextevt_global - basej) * TICK_NSEC; + +unlock: raw_spin_unlock(&base_global->lock); raw_spin_unlock(&base_local->lock); - return cmp_next_hrtimer_event(basem, expires); + cmp_next_hrtimer_event(basem, tevt); } /** From patchwork Tue Oct 25 13:58:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10798 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1024517wru; Tue, 25 Oct 2022 07:02:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7bb+YhD4K6n1mZqbfevJzkOiTF2womd3rvL15B6z1DXjH2bCD7k2n76/R9tkk/SkvWBwF+ X-Received: by 2002:a17:903:2285:b0:185:44df:d916 with SMTP id b5-20020a170903228500b0018544dfd916mr38775240plh.120.1666706571090; Tue, 25 Oct 2022 07:02:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706571; cv=none; d=google.com; s=arc-20160816; b=CRDlRC75CwjG50uX9OdXUTETdIQb1ceuBQhoovyYB8m6Sl32/Sb5XLOyCVz/Im260l rO5vTZjzayUSHJ9+MifdGOJJHGgWDAc42UdmpImeGDXwn/RJdTtysw6vhXB+TMSTMuxh RDrF5gY/reBbWEixY8qBZVX8xXMwfWoO4LG1GxHrchKC2MCvQYlh8cXFf8QHjqdCI8D8 zWnyHryH8g4+O8Sss/hM8EOU20+eSKvMXCdibix14GbIEZ+HUIe6Krb/RoSgrnQzhYV7 77DaeqzJEHWPl1Luy+7AnX9RtNApIpM6pNwbssFM6jfp/dZYbCs+CCvdN1ZoJEQzovIl 03Sg== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=o6uqNXLi9fS/j/t+vRTXJBiE22APnO3iPTi53EIarr4=; b=maYo+nFo4oMQV10sx+R7zHRHjmEKRwfyI1AwRXcTnlANCcjUl3WWH+DvDJKdhaPICM wxQXHYmho5U8zwN9Y9rR5L1uUNVKK1RVvPKcGK+Gy8FqB2SOpNqiIcJ98VDUez5YZqFX 9cjlkdDAzT+Jqj5slEiTqucmFmOoVNShic5X6N7zqaGJ1O7hufdl9qfYmsmY2iYBTU1L voq5Pq0A/3neQdWaH1iFh5lIcg/QWvp2Mwb1CtSXsHfWG+tmnECoJoYkJOZtkSUs16PE PVpcwxOToqR6vW8yTo8eZmuVWP/y60sVm4tDBl78Qio/XyHK42RuZ+7SNLwSMuMSnR6j 2bcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=IADpQrO9; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=8eofvPNN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lx2-20020a17090b4b0200b001ed40b70436si4188955pjb.155.2022.10.25.07.02.35; Tue, 25 Oct 2022 07:02:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=IADpQrO9; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=8eofvPNN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232547AbiJYOAN (ORCPT + 99 others); Tue, 25 Oct 2022 10:00:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231811AbiJYN7a (ORCPT ); Tue, 25 Oct 2022 09:59:30 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 884D1BDF; Tue, 25 Oct 2022 06:59:26 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706365; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=o6uqNXLi9fS/j/t+vRTXJBiE22APnO3iPTi53EIarr4=; b=IADpQrO9FsdT2Wos+BkvFJ9hjjdCh4J+cOqRGOoYjt3bnRm3xDgwNx89+L80e+paMSqk2S SvQHqauzMLFnVh/2rsm27Lv9IQSDZLlIRZILg0M20RvYZcBw4HFGP1VwAmo4y4+wN+OSVg LI05beGqz/QfdGGNxfsn7oXrF7hqw2VhHXdEVm9xFjx8P8ZXWiz3bBIbi8z5OoYiXgwA96 QSCHO0iQJSYE2PiZfinnps6DexqXxPexByDGMjTmaIUwTRQ/+/2PVgm57jMKO8OoIdX9Co eP7PeXKJ5eaI8eCmj+CMpqI9HQLsWgbvpbgg4L7Xw2zG6PJH1oVv1HP7+pBTuA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706365; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=o6uqNXLi9fS/j/t+vRTXJBiE22APnO3iPTi53EIarr4=; b=8eofvPNNbQD92dcU/AsphDAJ7l/BqOxnIQ7vR6FpjpkbEt7FENM3a0/xo/Yqp6+oniYnu8 k7KMGh2fs+K9qjCQ== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 08/17] timer: Rename get_next_timer_interrupt() Date: Tue, 25 Oct 2022 15:58:41 +0200 Message-Id: <20221025135850.51044-9-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668509542171524?= X-GMAIL-MSGID: =?utf-8?q?1747668509542171524?= get_next_timer_interrupt() does more than simply getting the next timer interrupt. The timer bases are forwarded and also marked as idle when possible and the next timer interrupt information is required for this. To get not confused, rename function to a more descriptive name. No functional change. Signed-off-by: Anna-Maria Behnsen --- kernel/time/tick-internal.h | 4 ++-- kernel/time/tick-sched.c | 16 ++++++++-------- kernel/time/timer.c | 10 +++++++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h index fcb2d45c2934..6f5f164506d5 100644 --- a/kernel/time/tick-internal.h +++ b/kernel/time/tick-internal.h @@ -168,8 +168,8 @@ static inline void timers_update_nohz(void) { } DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases); -extern void get_next_timer_interrupt(unsigned long basej, u64 basem, - struct timer_events *tevt); +extern void forward_and_idle_timer_bases(unsigned long basej, u64 basem, + struct timer_events *tevt); void timer_clear_idle(void); #define CLOCK_SET_WALL \ diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 78f172d1f3d2..7f7bfe8b498d 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -802,11 +802,11 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) * Keep the periodic tick, when RCU, architecture or irq_work * requests it. * Aside of that check whether the local timer softirq is - * pending. If so its a bad idea to call get_next_timer_interrupt() - * because there is an already expired timer, so it will request - * immediate expiry, which rearms the hardware timer with a - * minimal delta which brings us back to this place - * immediately. Lather, rinse and repeat... + * pending. If so its a bad idea to call + * forward_and_idle_timer_bases() because there is an already + * expired timer, so it will request immeditate expiry, which + * rearms the hardware timer with a minimal delta which brings us + * back to this place immediately. Lather, rinse and repeat... */ if (rcu_needs_cpu() || arch_needs_cpu() || irq_work_needs_cpu() || local_timer_softirq_pending()) { @@ -823,7 +823,7 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) * disabled this also looks at the next expiring * hrtimer. */ - get_next_timer_interrupt(basejiff, basemono, &tevt); + forward_and_idle_timer_bases(basejiff, basemono, &tevt); tevt.local = min_t(u64, tevt.local, tevt.global); ts->next_timer = tevt.local; } @@ -838,7 +838,7 @@ static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) if (delta <= (u64)TICK_NSEC) { /* * Tell the timer code that the base is not idle, i.e. undo - * the effect of get_next_timer_interrupt(): + * the effect of forward_and_idle_timer_bases(): */ timer_clear_idle(); /* @@ -1141,7 +1141,7 @@ void tick_nohz_idle_retain_tick(void) { tick_nohz_retain_tick(this_cpu_ptr(&tick_cpu_sched)); /* - * Undo the effect of get_next_timer_interrupt() called from + * Undo the effect of forward_and_idle_timer_bases() called from * tick_nohz_next_event(). */ timer_clear_idle(); diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 069478e65230..1990eb1b2cc9 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1705,7 +1705,7 @@ static unsigned long next_timer_interrupt(struct timer_base *base) } /** - * get_next_timer_interrupt + * forward_and_idle_timer_bases * @basej: base time jiffies * @basem: base time clock monotonic * @tevt: Pointer to the storage for the expiry values @@ -1713,9 +1713,13 @@ static unsigned long next_timer_interrupt(struct timer_base *base) * Stores the next pending local and global timer expiry values in the * struct pointed to by @tevt. If a queue is empty the corresponding field * is set to KTIME_MAX. + * + * If required, base->clk is forwarded and base is also marked as + * idle. Idle handling of timer bases is allowed only to be done by + * CPU itself. */ -void get_next_timer_interrupt(unsigned long basej, u64 basem, - struct timer_events *tevt) +void forward_and_idle_timer_bases(unsigned long basej, u64 basem, + struct timer_events *tevt) { unsigned long nextevt, nextevt_local, nextevt_global; struct timer_base *base_local, *base_global; From patchwork Tue Oct 25 13:58:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10794 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1024133wru; Tue, 25 Oct 2022 07:02:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7BQLs6SYxxga+TWNrQ1wPkH225XMt4fePSqDGRyXraLKhEtiNHBa975XAKW887C7kJpHFb X-Received: by 2002:a17:907:9618:b0:78d:fa75:e2b8 with SMTP id gb24-20020a170907961800b0078dfa75e2b8mr33581198ejc.65.1666706540201; Tue, 25 Oct 2022 07:02:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706540; cv=none; d=google.com; s=arc-20160816; b=eNTYs3bR5hQVSLiMRXXGIdVIe8oJP5AJ/x/h4Bok+Qj+HocnZG8w2rSUpNFPZheeLQ ZGDyVqQVPy0ez6K51VfOudyAllYDWXS6Sxz3n/BVECcbP8V7i6AUGTqYP4I84qVMITi6 v+BugiZgbJQt88vQnAXBDDSPDzYBRd9orPM1Z71o7P9jIr1zw0ZfjXJChyeD7pRneWVW KMbrGGnUEThdckbwdgfS8ULRH33KqgdnQ5M2tV0U/uDmzWbngF7db8GjBwpgKAwCcKcf qeWi9A5vtjQOoAOIZyZVtM1ofxCwQDahJIhQ9ecp/I1N0b0qPdntTyuehFADHRqzB1RL 4AuQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=Yz5QT49atKnBQG/qBqEE3tDeAp0qP/kdCDP7jVvSCFM=; b=chKaXRrsFmmdNIWFClpp5+S1NZoa0DVaeWeTc2mH3zvaN4Ybd3TqgUnKExBMye5PpQ 2FKNXY3l27Fdr3jPJIr5aamu4hsiqTRG3EmuVxx91+pQksQ4zsj3WRdWwIdI799f0jj9 U4d0qNOyZ2kSkorawucxCVuGn+SB6LNwnjl7a3EgMYKLqv97CrUgV1wpBME15XAWb9bc IOBaQxgG02wgNpo3gm/sapjqtn7ISw91rxsLRfAuB3rxrVxlDz0WwTOQhfAu+nWcwLxf C9NNGg5w8EdND+KwkyMXBtiF3PAA8DN8ioqDddMjn/W+XNBCzvg+E6mJeesnn/PPsJiN GYhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="fSks/ca8"; dkim=neutral (no key) header.i=@linutronix.de header.b=jVY37Lfu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hd42-20020a17090796aa00b00780636a4a58si2866262ejc.626.2022.10.25.07.01.52; Tue, 25 Oct 2022 07:02:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="fSks/ca8"; dkim=neutral (no key) header.i=@linutronix.de header.b=jVY37Lfu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231995AbiJYOA1 (ORCPT + 99 others); Tue, 25 Oct 2022 10:00:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231938AbiJYN7d (ORCPT ); Tue, 25 Oct 2022 09:59:33 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B27C2DC4; Tue, 25 Oct 2022 06:59:27 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706365; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Yz5QT49atKnBQG/qBqEE3tDeAp0qP/kdCDP7jVvSCFM=; b=fSks/ca8LpAozeH+YWraVKFk64y/NlxHDqFuNY2NEwhyCgxX6Sp7sJ4Jof1/h7adP8UqqF R83ncl9gPTc5HGmWpLB+MoXBxVahnqKwh9FedrlefYzEW+jQ5ohH3S1T1vHVMaQV8OZMG4 T0a66LDsOPtjXVwlfSiRrW4s1DFFUN0M/MqrJd+ePLz4zapGGNjfXOngA2/5JJqdztu5QX xAhHDd4vpxn30DYdk8TH2e1vAMGPzn9TV9JDroz5CnDMqXP7sLTJLyUHXcKMZS8UfnX/7C 7ikYKpeq+l2EMzys6Sxct7606MvE7pAAWwR8HSghV4liBQhrtLr5zN8DB+swFQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706365; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Yz5QT49atKnBQG/qBqEE3tDeAp0qP/kdCDP7jVvSCFM=; b=jVY37Lfuu2bepa56hR/qvENwl19HeRKglymqDFH9aH7Xp5wCCz0O2obI5mQe2YTnl9ECyZ KzNtHLjCPEX1SWDg== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 09/17] timer: Split out "get next timer interrupt" functionality Date: Tue, 25 Oct 2022 15:58:42 +0200 Message-Id: <20221025135850.51044-10-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668477168087893?= X-GMAIL-MSGID: =?utf-8?q?1747668477168087893?= forward_and_idle_timer_bases() includes the functionality for getting the next timer interrupt. To reuse it, it is splitted into an separate function "get_next_timer_interrupt()". This is preparatory work for the conversion of the NOHZ timer placement to a pull at expiry time model. No functional change. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timer.c | 93 +++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 1990eb1b2cc9..167ae89f0f46 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1704,6 +1704,46 @@ static unsigned long next_timer_interrupt(struct timer_base *base) return base->next_expiry; } +static unsigned long get_next_timer_interrupt(struct timer_base *base_local, + struct timer_base *base_global, + unsigned long basej, u64 basem, + struct timer_events *tevt) +{ + unsigned long nextevt_local, nextevt_global; + bool local_first; + + nextevt_local = next_timer_interrupt(base_local); + nextevt_global = next_timer_interrupt(base_global); + + /* + * Check whether the local event is expiring before or at the same + * time as the global event. + * + * Note, that nextevt_global and next_evtlocal might be based on + * different base->clk values. So it's not guaranteed that + * comparing with empty bases results in a correct local_first. + */ + if (base_local->timers_pending && base_global->timers_pending) + local_first = time_before_eq(nextevt_local, nextevt_global); + else + local_first = base_local->timers_pending; + + /* + * Update tevt->* values: + * + * If the local queue expires first, then the global event can + * be ignored. If the global queue is empty, nothing to do + * either. + */ + if (!local_first && base_global->timers_pending) + tevt->global = basem + (u64)(nextevt_global - basej) * TICK_NSEC; + + if (base_local->timers_pending) + tevt->local = basem + (u64)(nextevt_local - basej) * TICK_NSEC; + + return local_first ? nextevt_local : nextevt_global; +} + /** * forward_and_idle_timer_bases * @basej: base time jiffies @@ -1723,7 +1763,7 @@ void forward_and_idle_timer_bases(unsigned long basej, u64 basem, { unsigned long nextevt, nextevt_local, nextevt_global; struct timer_base *base_local, *base_global; - bool local_first, is_idle; + bool is_idle; /* Preset local / global events */ tevt->local = tevt->global = KTIME_MAX; @@ -1741,8 +1781,11 @@ void forward_and_idle_timer_bases(unsigned long basej, u64 basem, raw_spin_lock(&base_local->lock); raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING); - nextevt_local = next_timer_interrupt(base_local); - nextevt_global = next_timer_interrupt(base_global); + nextevt = get_next_timer_interrupt(base_local, base_global, + basej, basem, tevt); + + nextevt_local = base_local->next_expiry; + nextevt_global = base_global->next_expiry; /* * We have a fresh next event. Check whether we can forward the @@ -1763,21 +1806,6 @@ void forward_and_idle_timer_bases(unsigned long basej, u64 basem, base_global->clk = nextevt_global; } - /* - * Check whether the local event is expiring before or at the same - * time as the global event. - * - * Note, that nextevt_global and nextevt_local might be based on - * different base->clk values. So it's not guaranteed that - * comparing with empty bases results in a correct local_first. - */ - if (base_local->timers_pending && base_global->timers_pending) - local_first = time_before_eq(nextevt_local, nextevt_global); - else - local_first = base_local->timers_pending; - - nextevt = local_first ? nextevt_local : nextevt_global; - /* * Bases are idle if the next event is more than a tick away. Also * the tick is stopped so any added timer must forward the base clk @@ -1790,43 +1818,24 @@ void forward_and_idle_timer_bases(unsigned long basej, u64 basem, /* We need to mark both bases in sync */ base_local->is_idle = base_global->is_idle = is_idle; + raw_spin_unlock(&base_global->lock); + raw_spin_unlock(&base_local->lock); + /* * If the bases are not marked idle, i.e one of the events is at * max. one tick away, then the CPU can't go into a NOHZ idle * sleep. Use the earlier event of both and store it in the local * expiry value. The next global event is irrelevant in this case - * and can be left as KTIME_MAX. CPU will wakeup on time. + * and can be reset as KTIME_MAX. CPU will wakeup on time. */ if (!is_idle) { /* If we missed a tick already, force 0 delta */ if (time_before_eq(nextevt, basej)) nextevt = basej; tevt->local = basem + (u64)(nextevt - basej) * TICK_NSEC; - goto unlock; + tevt->global = KTIME_MAX; } - /* - * If the bases are marked idle, i.e. the next event on both the - * local and the global queue are farther away than a tick, - * evaluate both bases. No need to check whether one of the bases - * has an already expired timer as this is caught by the !is_idle - * condition above. - */ - if (base_local->timers_pending) - tevt->local = basem + (u64)(nextevt_local - basej) * TICK_NSEC; - - /* - * If the local queue expires first, then the global event can be - * ignored. The CPU wakes up before that. If the global queue is - * empty, nothing to do either. - */ - if (!local_first && base_global->timers_pending) - tevt->global = basem + (u64)(nextevt_global - basej) * TICK_NSEC; - -unlock: - raw_spin_unlock(&base_global->lock); - raw_spin_unlock(&base_local->lock); - cmp_next_hrtimer_event(basem, tevt); } From patchwork Tue Oct 25 13:58:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10801 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1024967wru; Tue, 25 Oct 2022 07:03:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6JPDp89UCeRKkBZBno3g01lt3t0U30LJj4HUZRW8gIG+bq/2RmBWL7MzYw+T8td7Riu/cr X-Received: by 2002:a17:90b:4f8c:b0:213:245b:b49 with SMTP id qe12-20020a17090b4f8c00b00213245b0b49mr9005471pjb.110.1666706610280; Tue, 25 Oct 2022 07:03:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706610; cv=none; d=google.com; s=arc-20160816; b=HC3moMVPy+AgD4mea8+WdFsz29Ax40CO/5JBG1/LH3nqzdu0A4ZB6dhEOEjlnGCGcG P2mIxvMGEbowpuIZzcP3Pz+4n9P0Ik7QAibO9JUpGcV+kJMYvDjMAWy/T7YC6rA2+mpQ GhXMJwtku1/hAu21QNncXOcMmHzIAuoT0kzTPbG9IH7aCZJcNFZ1f0DAGyEek1QFffk0 YYwfdkEE58F6aJXzS/ZJQmOQVe7cABVm1SSnkkj7oOPJikt7eeDN9pAgmGEuoczoxUZJ 3ERYUAeGiaQwc0lPwMSdZStXXMMl1j7T7V4w9zjfzKBnXjwAiXvYbvfOondB/RzMpnM7 PE9g== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=IjZFfNcyum+PDjbc3AHd6/uCS3i8MsMOcfFwmxfp/G8=; b=ZhTuRYrHK1c/5qIBE8JrEnMsR2V9TOX+zcfuWNsBU1Clzkmr0uQZg0YxCwtSoetl8X OPtPUDbT3LHyEfluTx4HfzS1yHKIPJXDcSYHHIT5zGbsnl6i9uKLmvp9HWvJmtM4b7yh RV+ZOaZr5Wj6A7HEk59LLTQNmll6fggiuup6ykLzWocnILSc0Xc4gSLN+NlEEk24CQZ+ AHJcVmc1+DLVWfY61NP/fsDxIDYbhH6CPw8o/4FKn5nuZCsUjRgLF8mznymE6RpLIDcF zzuYL+e7pBga5nlTlzfxwsa8j/X7ih/p68/rnIIqDva2zhrS7WaPB6NTy1jd0ouwAUXR dHVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=UGETlyUb; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q4-20020a056a00088400b0056b82fbafebsi3783104pfj.379.2022.10.25.07.03.15; Tue, 25 Oct 2022 07:03:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=UGETlyUb; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232411AbiJYOAj (ORCPT + 99 others); Tue, 25 Oct 2022 10:00:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231872AbiJYN7d (ORCPT ); Tue, 25 Oct 2022 09:59:33 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B53A38BD; Tue, 25 Oct 2022 06:59:27 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706366; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IjZFfNcyum+PDjbc3AHd6/uCS3i8MsMOcfFwmxfp/G8=; b=UGETlyUbeKc76z87+EiwJTtOgXzZt9nBjRIlvPB+43LPxVq66Dbt+jPjjOj7/ZFpYuqsIW I0e4W3BYeNd8LbwVjg6tY/8gQLKK+FLTtjQchvMJdeyx+M4RUT39dIAXup7j8xP776BazL Uw5eK5TV1VTIZbqp0sCaOEQZAW7jXxWEcssNm3q9ioeDkiHPzwNC7Ews0VZAkB8SK2Yf2z vLUMsX7pSxW0NUPXDSaHjsGWkm2M6uwS/NGh9wTwDljUDzg45uBXoYZyiGbUIq1NW+gPuE UJ/SQOZZ4kZxHd90FKE1vQhlYAkP5JNqg7pE2XzS6Ny3qmCv3SYahFDPeJn8Uw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706366; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IjZFfNcyum+PDjbc3AHd6/uCS3i8MsMOcfFwmxfp/G8=; b=oMJlLtkx5PBtCsSDRXrQdvne6oh7AExalkKbxyvC+Y9LxFQv/AhZgza0IWSZwZbkC+1+De Lox22y4x9bbXKdCw== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 10/17] timer: Add get next timer interrupt functionality for remote CPUs Date: Tue, 25 Oct 2022 15:58:43 +0200 Message-Id: <20221025135850.51044-11-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668550563200660?= X-GMAIL-MSGID: =?utf-8?q?1747668550563200660?= To prepare for the conversion of the NOHZ timer placement to a pull at expiry time model it's required to have functionality available getting the next timer interrupt on a remote CPU. Signed-off-by: Anna-Maria Behnsen --- kernel/time/tick-internal.h | 3 +++ kernel/time/timer.c | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h index 6f5f164506d5..7b65abc9d803 100644 --- a/kernel/time/tick-internal.h +++ b/kernel/time/tick-internal.h @@ -170,6 +170,9 @@ DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases); extern void forward_and_idle_timer_bases(unsigned long basej, u64 basem, struct timer_events *tevt); +extern void get_next_timer_interrupt_remote(unsigned long basej, u64 basem, + struct timer_events *tevt, + unsigned int cpu); void timer_clear_idle(void); #define CLOCK_SET_WALL \ diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 167ae89f0f46..54631e86763f 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1744,6 +1744,47 @@ static unsigned long get_next_timer_interrupt(struct timer_base *base_local, return local_first ? nextevt_local : nextevt_global; } +/** + * get_next_timer_interrupt_remote + * @basej: base time jiffies + * @basem: base time clock monotonic + * @tevt: Pointer to the storage for the expiry values + * @cpu: Remote CPU + * + * Stores the next pending local and global timer expiry values in the + * struct pointed to by @tevt. If a queue is empty the corresponding + * field is set to KTIME_MAX. If local event expires before global + * event, global event is set to KTIME_MAX as well. + */ +void get_next_timer_interrupt_remote(unsigned long basej, u64 basem, + struct timer_events *tevt, + unsigned int cpu) +{ + struct timer_base *base_local, *base_global; + unsigned long flags; + + /* Preset local / global events */ + tevt->local = tevt->global = KTIME_MAX; + + /* + * Pretend that there is no timer pending if the cpu is offline. + * Possible pending timers will be migrated later to an active cpu. + */ + if (cpu_is_offline(cpu)) + return; + + base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu); + base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu); + + raw_spin_lock_irqsave(&base_local->lock, flags); + raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING); + + get_next_timer_interrupt(base_local, base_global, basej, basem, tevt); + + raw_spin_unlock(&base_global->lock); + raw_spin_unlock_irqrestore(&base_local->lock, flags); +} + /** * forward_and_idle_timer_bases * @basej: base time jiffies From patchwork Tue Oct 25 13:58:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10797 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1024273wru; Tue, 25 Oct 2022 07:02:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5qKWVEr/B8MUW06JafVtH2W/Sd5OY7P1nozhNtNKk7z1EgapeBzGo5q15VsKSr/kx2yOkp X-Received: by 2002:a05:6402:3588:b0:45d:7d14:baf2 with SMTP id y8-20020a056402358800b0045d7d14baf2mr35472497edc.1.1666706550413; Tue, 25 Oct 2022 07:02:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706550; cv=none; d=google.com; s=arc-20160816; b=J4ms6l/3f+B4zy9M1TaFPENZyyWjlP2P4DczulkOmZD6vYuv5ybIgrCn1g3NL12B7C BMAbf+xqaZwxz+tOTL64kwQFyrv0QqdV+dggHT1y9cXfXWxEi3r0XCVY8Zxp0vkbmFo4 IeM+wr7lsZ39R091sOeG8z428k2MCEYkWwllF4zgna2+bT6ZpNYfvtZ7G2B6xkp44ot3 IVNP+6xfdg4HOlJ5RkOEPUs1Yk8xgzBZcS20mbAiEIU+ry80QmNzFUp2MuHVb+vDxn+J uWQ/yimQYZU4qRkSY28/zpiHsobmgTnndQnHuI5D5x9x8jxPx3tAf9fKcP5qBga/EO8/ +CEA== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=CGHVsSTlrh42mtIuI9uwDJB0LFHl02eMk5Ez8/za1gE=; b=WgwahrSqDXCKq9wZcBZxD9ItuAusVWLspRF/PGKE4UgjtQPUuO2ySa2zgxIwQydypY pQuWsZqQYApG7v7Vye7rLD7+MH8mjCnEOOII7p2uR9vPb4jwPVzYkVNkWc+raOtPwR0U tGc5I2Dp3A4wKe7LAfD0Qgank2wlC449cOzxBWRCY2mFIYo6/aMyBFniu01g9d64Gx9W OgiuIHzqYElwPyPb+VVyd/XzoSiFfYF006ddrnAjwYiXu7JkjK3W6USRsZqVKIbXgy91 RaXpzE/NEuw+wdfFZB491h0heb5ke5bGYGglSpCizyhDCHHq04bmxZGxwIzwaMHnYFjs d+wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=psy71Xoy; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gt33-20020a1709072da100b00788361f96a2si887899ejc.776.2022.10.25.07.02.04; Tue, 25 Oct 2022 07:02:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=psy71Xoy; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232280AbiJYOAc (ORCPT + 99 others); Tue, 25 Oct 2022 10:00:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231917AbiJYN7d (ORCPT ); Tue, 25 Oct 2022 09:59:33 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B60C559B; Tue, 25 Oct 2022 06:59:28 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706367; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CGHVsSTlrh42mtIuI9uwDJB0LFHl02eMk5Ez8/za1gE=; b=psy71XoywLxpFRQtpsAzmGAtKp/n3Nkt57Eou0efsjHndu1b2cex56ZmmiWO/z5Ir+Jvln wStFFpbewui20Pxoz2OrdFzZdUjknLDe+y+h93ds4RSxKv0o3cKEHoYBoeq/18yELHDcv0 E3/FXpntETQtt2XKbhjzkEjzkWiQ2QxR//TCVgSMq6KH2V7PMJmBBCnt5IWpc3RW54gXC8 +z3YNawaC+b8+yJ4hK+g+fgBZvxTaJu5RgF5p/G8AlZ6A3yvb4QJQwgv26MUUDj/zbcbt3 xo+chAZ27SWHljI3LC0FS9lR0r6V2Fv96vK3AdJPsiO+KhSj+bIslofR4ilvaA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706367; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CGHVsSTlrh42mtIuI9uwDJB0LFHl02eMk5Ez8/za1gE=; b=cDavS7ruVI86fuuZ0pIbMSDz0yM97jug9hFd7TT1eAn1P/NDH13s+F8kfvIDSwfEkg6gaD zqwM8KHh929+elBQ== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , "Richard Cochran (linutronix GmbH)" , Anna-Maria Behnsen Subject: [PATCH v3 11/17] timer: Restructure internal locking Date: Tue, 25 Oct 2022 15:58:44 +0200 Message-Id: <20221025135850.51044-12-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668487815825274?= X-GMAIL-MSGID: =?utf-8?q?1747668487815825274?= From: "Richard Cochran (linutronix GmbH)" Move the locking out from __run_timers() to the call sites, so the protected section can be extended at the call site. Preparatory patch for changing the NOHZ timer placement to a pull at expiry time model. No functional change. Signed-off-by: Richard Cochran (linutronix GmbH) Signed-off-by: Anna-Maria Behnsen --- kernel/time/timer.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 54631e86763f..bce2f87d5e70 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1911,11 +1911,7 @@ static inline void __run_timers(struct timer_base *base) struct hlist_head heads[LVL_DEPTH]; int levels; - if (time_before(jiffies, base->next_expiry)) - return; - - timer_base_lock_expiry(base); - raw_spin_lock_irq(&base->lock); + lockdep_assert_held(&base->lock); while (time_after_eq(jiffies, base->clk) && time_after_eq(jiffies, base->next_expiry)) { @@ -1935,21 +1931,36 @@ static inline void __run_timers(struct timer_base *base) while (levels--) expire_timers(base, heads + levels); } +} + +static void __run_timer_base(struct timer_base *base) +{ + if (time_before(jiffies, base->next_expiry)) + return; + + timer_base_lock_expiry(base); + raw_spin_lock_irq(&base->lock); + __run_timers(base); raw_spin_unlock_irq(&base->lock); timer_base_unlock_expiry(base); } +static void run_timer_base(int index) +{ + struct timer_base *base = this_cpu_ptr(&timer_bases[index]); + + __run_timer_base(base); +} + /* * This function runs timers and the timer-tq in bottom half context. */ static __latent_entropy void run_timer_softirq(struct softirq_action *h) { - struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_LOCAL]); - - __run_timers(base); + run_timer_base(BASE_LOCAL); if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) { - __run_timers(this_cpu_ptr(&timer_bases[BASE_GLOBAL])); - __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF])); + run_timer_base(BASE_GLOBAL); + run_timer_base(BASE_DEF); } } From patchwork Tue Oct 25 13:58:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10802 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1025132wru; Tue, 25 Oct 2022 07:03:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6OszwIYhyTEgC8sXdWtv6dE58vDtaalD4/Xeyy/qoMzuq+tbBB5tKmn6d4Ptppvq28ec54 X-Received: by 2002:a17:902:f602:b0:178:9818:48a4 with SMTP id n2-20020a170902f60200b00178981848a4mr38527432plg.148.1666706624160; Tue, 25 Oct 2022 07:03:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706624; cv=none; d=google.com; s=arc-20160816; b=w2hTnaRT3dhBINitwv6YJrZRhGXjj/X1wF3ZJwTjEO1xDZEbXUm1J4DF2cbrxQBSfh pme9xHjUgGGW/eQzQVbICP/jxYt/rKgK3dufhXmCEU6b9HpaHL3ipUx/6rVRjjRvAICL N0bdViWEfFESTcqRGcQqJxoqcoEU4d8VvTrZOqBnHmucmkayL869ph/lvcTmiAoIJnl0 HhM0KOsOAVfzb2scfwFKhDF3VtRPiQHTeU0qCBXZV4zL3j9D7vTGWKueC0sDQVJUnAzY Dh8SDHcViAyQhrqe57WG9SEGIsWKbU/iQyThd5kSsG8tQXTzcHHjJJHeHVfFllzsN6By e/kA== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=kjVAw8MhbYxCC7wINXEcQMmNl7FSEGbHT8j9jxIsmGY=; b=hGwvK1Ewj+e/Ck4TMtUS4DOUFMug+rIKlSgBcPRsQp3fNKei1qMdfNY+0Cy2tWqPB5 8bpY9agRm9scmg6OmGem64l+s8P45cWorQGa3FFhDb0bBJg2zQLsRTvPIYuWwELl30xc 63oLLY4gDg9IZyDdPpQFYCYqmOe8HwrlL5cqAbwP8qgx3DUJv82/aRQ8/Pe4qVBGJCqv 31j3cRlzu0gmZNSbXgZzHjd6hPX+d0Ng9PukZy0BH8NQMfJYlEUNtJfl6gUqgJBIsCY9 mq8mukNzd9VhDHB6UugtbbrHskWFWln8rKM3NBrgmYAVkt8y0vmmcyw9YhwjTgo+RUxp C0ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=mcYLXutm; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f20-20020a056a00229400b005655fc278f3si3530512pfe.201.2022.10.25.07.03.24; Tue, 25 Oct 2022 07:03:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=mcYLXutm; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232580AbiJYOAT (ORCPT + 99 others); Tue, 25 Oct 2022 10:00:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231926AbiJYN7d (ORCPT ); Tue, 25 Oct 2022 09:59:33 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B7B93BB; Tue, 25 Oct 2022 06:59:28 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706367; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kjVAw8MhbYxCC7wINXEcQMmNl7FSEGbHT8j9jxIsmGY=; b=mcYLXutmXzSR9ZuMfSpPMy8mQSASFUo61PQ7SHQrKWDUi8VnIlN2rlivC+NIYxUL+jWhXo XukDXg7bgvlqbKnay6xYfaddDvjB4JzoR/3at5ZyWY6B4aY9vzMijr17r1FPG2M/OxmEw7 P5jo7g0efgIjCM9NQhOJOak/NtBguBLHDt/WfOXVVHpOG8cRa+GPW+HIfQ8BDobKRsj4U+ jLlgMSxzHk+oh2E29A/B26h8puv9BNJP3ky9uFDl9CpHkcqUJZaZPAmbD1Qux2dh2Bq5ya l/f0ifnZi1PXLK5lEq/OBOtQmqjfd7z3NH6HtT/s0Xjl7RJejjmCnHkNrOpFkg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706367; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kjVAw8MhbYxCC7wINXEcQMmNl7FSEGbHT8j9jxIsmGY=; b=HlRzm2pHphXnjc98iqPYLFBQ1DfuNTeMTrE2jFII10yIqd6mRgG+TLp3EnVl/OalcwMbou CI+wEHaGio7wslCA== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 12/17] timer: Check if timers base is handled already Date: Tue, 25 Oct 2022 15:58:45 +0200 Message-Id: <20221025135850.51044-13-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668565319021846?= X-GMAIL-MSGID: =?utf-8?q?1747668565319021846?= Due to the conversion of the NOHZ timer placement to a pull at expiry time model, the per CPU timer bases with non pinned timers are no longer handled only by the local CPU. In case a remote CPU already expires the non pinned timers base of the local cpu, nothing more needs to be done by the local CPU. A check at the begin of the expire timers routine is required, because timer base lock is dropped before executing the timer callback function. This is a preparatory work, but has no functional impact right now. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timer.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index bce2f87d5e70..0790ec8efe82 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1913,6 +1913,9 @@ static inline void __run_timers(struct timer_base *base) lockdep_assert_held(&base->lock); + if (!!base->running_timer) + return; + while (time_after_eq(jiffies, base->clk) && time_after_eq(jiffies, base->next_expiry)) { levels = collect_expired_timers(base, heads); From patchwork Tue Oct 25 13:58:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10806 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1025477wru; Tue, 25 Oct 2022 07:04:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7m4U0sbC/069FnimTqb/E1ybLJR27/ykpQ09aRhr3kS+ddX1qIdaRJ9/hKrMSLLhYg3+ey X-Received: by 2002:a17:90b:1a85:b0:20d:c705:92b9 with SMTP id ng5-20020a17090b1a8500b0020dc70592b9mr62458932pjb.213.1666706650703; Tue, 25 Oct 2022 07:04:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706650; cv=none; d=google.com; s=arc-20160816; b=TcSEyljB4dF2fJ9H+l1DtrUqnCt0Vxb6DlEhGG+kGz7PpQI+epP08y+JXLVBJtr8K7 rsSY3GckHTVDqp3uOXytRqFBnoU0iZEOAd4rFVEyfq1I4F0xnTWAoFsAHXb8BYix+ch7 eyA3GNnsreaFileQJMsfaBIpylauT11g9+zhmjUW3O/jVah0Y4J+I0yii6xnFpbLjofy bewyfAI+eEoJHGzS/DfXfU41ULTEKND96ohoPRofCr4bGwPr3R3NJspebrGEU6NR4ZDD F2gVbSXGv+tHJNAwI6ckruCUtZcebmVu3H7hk+jlVbrPbw52Z0Xvmi2QUhHBpUX2+P38 VM7g== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=7kvCElohZyUyNJGM2JneGjqwOan7vBCE2nbaLgbbE24=; b=hu7QxUjwRbxG/okn6CG/yMbntoc2EZ9KmIghcUJ/Me1U5xUi3YWxuJVqXEI1ZwezgM esGQaAMZFx5H/sn9Q1tTpMxmU8fXkYzTSVFuzXQp90mhMQC88dnzmtFpZyVdeRvEEKbj jGv14EQb9fEvf3/jTU3VSsvSwxsblya0G8Q438NHZ1bD4hpFIW9H/BuWZ+Gg9qvvUBLW HJ4qswxn52Ep383aqc8zQGMUXEs+T439BKPwmxF9g6x9bk+UOtU8XsgPRyOPHOXZPY4T DCYoHr2oV7bNiXmoQGqGIo1fyDI7juQr255u6zfAlWgVjlP+ryGkz2caqfohw1gUK/vm yMMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=1rvNmBFH; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=C6EV5i1S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c7-20020a17090a558700b002009ed52bf6si3163796pji.19.2022.10.25.07.03.55; Tue, 25 Oct 2022 07:04:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=1rvNmBFH; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=C6EV5i1S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232321AbiJYOAt (ORCPT + 99 others); Tue, 25 Oct 2022 10:00:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232066AbiJYN7h (ORCPT ); Tue, 25 Oct 2022 09:59:37 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B98E65B4; Tue, 25 Oct 2022 06:59:29 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7kvCElohZyUyNJGM2JneGjqwOan7vBCE2nbaLgbbE24=; b=1rvNmBFHfqTzHK97MH5AwbRl/2ohHfzGQXVUxbumwMMYUql4LUSdarJUdEjUB/2J4INa2K lb7eCMqax2eY+QdE5NkIEWoATeFTZKEEEm1oRFwRiOpJRsPKqKp+tSqGRzLtRV4aLdiH2P KEVQZy2lVd7P0D+aSDHIplsRslihKWsjDhTmSMFNiBF41svMCJCRAdEA0gWt3snZFlkwYH 7NbrCLJNIRM4agpP3644czZxZ7bz7yAkdHlldQvFKL5FIqrzkb2y04faAPB8sDj1+Y7RAL RFrLzNG4SgvvjbXp+w/419e241dsGffAmyJedlzDcqAthmonyySAoVL6agJkMA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7kvCElohZyUyNJGM2JneGjqwOan7vBCE2nbaLgbbE24=; b=C6EV5i1SHcU/aOMrMx8cq4g8OZp2nrmCWSD0GaxibGuR5WE/UzyOd0FqnDEfWOp7NpytzM ObJWWF8A79a2cCCw== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , "Richard Cochran (linutronix GmbH)" , Anna-Maria Behnsen Subject: [PATCH v3 13/17] tick/sched: Split out jiffies update helper function Date: Tue, 25 Oct 2022 15:58:46 +0200 Message-Id: <20221025135850.51044-14-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668593204387023?= X-GMAIL-MSGID: =?utf-8?q?1747668593204387023?= From: "Richard Cochran (linutronix GmbH)" The logic to get the time of the last jiffies update will be needed by the timer pull model as well. Move the code into a global funtion in anticipation of the new caller. No functional change. Signed-off-by: Richard Cochran (linutronix GmbH) Signed-off-by: Anna-Maria Behnsen --- kernel/time/tick-internal.h | 1 + kernel/time/tick-sched.c | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h index 7b65abc9d803..8f99e35e75b4 100644 --- a/kernel/time/tick-internal.h +++ b/kernel/time/tick-internal.h @@ -158,6 +158,7 @@ static inline void tick_nohz_init(void) { } #ifdef CONFIG_NO_HZ_COMMON extern unsigned long tick_nohz_active; extern void timers_update_nohz(void); +extern u64 get_jiffies_update(unsigned long *basej); # ifdef CONFIG_SMP extern struct static_key_false timers_migration_enabled; # endif diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 7f7bfe8b498d..868679ff3421 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -782,19 +782,31 @@ static inline bool local_timer_softirq_pending(void) return local_softirq_pending() & BIT(TIMER_SOFTIRQ); } -static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) +/* + * Read jiffies and the time when jiffies were updated last + */ +u64 get_jiffies_update(unsigned long *basej) { - struct timer_events tevt = { .local = KTIME_MAX, .global = KTIME_MAX }; - u64 basemono, delta, expires; unsigned long basejiff; unsigned int seq; + u64 basemono; - /* Read jiffies and the time when jiffies were updated last */ do { seq = read_seqcount_begin(&jiffies_seq); basemono = last_jiffies_update; basejiff = jiffies; } while (read_seqcount_retry(&jiffies_seq, seq)); + *basej = basejiff; + return basemono; +} + +static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu) +{ + struct timer_events tevt = { .local = KTIME_MAX, .global = KTIME_MAX }; + u64 basemono, delta, expires; + unsigned long basejiff; + + basemono = get_jiffies_update(&basejiff); ts->last_jiffies = basejiff; ts->timer_expires_base = basemono; From patchwork Tue Oct 25 13:58:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10804 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1025201wru; Tue, 25 Oct 2022 07:03:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM40HEPlASYDdxIDpUisWP8BivsLetY+7ULiVZ1vuLz5wUMWXw8fJKFBDOSYp7JZxyUUJpFd X-Received: by 2002:a17:90b:1e51:b0:212:d06b:b5fa with SMTP id pi17-20020a17090b1e5100b00212d06bb5famr25529492pjb.171.1666706629614; Tue, 25 Oct 2022 07:03:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706629; cv=none; d=google.com; s=arc-20160816; b=FEJXCcY5L8cXB7E1ZrxsSefLQ9SrlP4R5CGNIMGhTKrtCRBwyAMSl+59TF1djONiRI I6zydNuLStBIQcbmTc3UOcFrsnwTF9HmtARxI0U+cQkT/QZ+Vw9dwbsfu6GF5S2PavT0 wfWMxbKaS2g7neqnzD3iDO1ijjIhalB5/HEudt7aGcVCFo+0YuA8SYULMtGkWYXLG41C SKMDFgoQ+eJap3uED1TKi7XIUioywgil7U0GpvM27Gy40nnsJKcGBDiS7XwZzXutD4Lx ChuPM8WvMzz4s4NXcNe0Ydxd/MIoVKt522kPYgWhRIFNljFwyBJ5KIswPiqCbxbv1Hrd taow== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=667QCwiq+iADKLvztNrdPYY2aL/BrN4JFCT1OAFk/pI=; b=F3NVMuvOJosYgHye1sy8Pz61z38eZ8qY3NeDpGF8OMytke7xmFgXEikuCBLwlAOKgy ku653c1scDpHEPy2sYBEbGQhjr6yjuHZdtfkQOE7D1Di5i737dL/QTnlmrsxIYMKFtq3 vBRQhOaMFrXkUlZrsP7TW6l1D7kkQqwukB0wk5bjO8xoYNYfV+h2eSOy6y1o+KjGhsxO cPx0CBYN1tWyRXyTqEDbi9y2+GGXTk09U5ORLyC+GHFc2Lm50E+oBySP6h18XANo0vpk +2S3+QRv3n6C/IDiX2flEk/HKPoagyaS0krvUzUzcMIsM9G1XXifGHyz7GdjAXMOA1fs 6quA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=uezquWYy; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id na18-20020a17090b4c1200b00212d699c686si3627299pjb.125.2022.10.25.07.03.33; Tue, 25 Oct 2022 07:03:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=uezquWYy; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232674AbiJYOAn (ORCPT + 99 others); Tue, 25 Oct 2022 10:00:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232085AbiJYN7h (ORCPT ); Tue, 25 Oct 2022 09:59:37 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BE20BC0A; Tue, 25 Oct 2022 06:59:30 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=667QCwiq+iADKLvztNrdPYY2aL/BrN4JFCT1OAFk/pI=; b=uezquWYyEK7cqQX9y+DHarZwNb/r2CTbJTQTFluZwsOTH/b1uvW/+qlZYZtyCj/RIzuX6e FDwysHn1UMUR2Y7S4gerOhU3xxbL6uUkfTOw0SzRpdgWkqenEeK9BULDTn8SNIDTOVV1tm 82j61/Bgatw3OF1ZPbA0AfIpUG+m0KBA1FWOzBv7/9kTzR61/MazNolfOA82iuaoYgKgZ0 VntlKRl38FYmDhMococxZrEdY3i745vCAXenjC+lV+jgGXu7yE7U5zVcC7xry35vZmf5QY yusVDEE6LvQBURFRjnuCGEB0P8smXOUwwLG4tAAokhOicPearIbykfwYTRhcGg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=667QCwiq+iADKLvztNrdPYY2aL/BrN4JFCT1OAFk/pI=; b=w+YsvPgWcqimpr2wgJhAnUGpyfyik40iab0V80asp28PI1L9oL/3qrONrzrtJu+Hzy7xN0 FnXxvv1cME3m0rAQ== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 14/17] timer: Implement the hierarchical pull model Date: Tue, 25 Oct 2022 15:58:47 +0200 Message-Id: <20221025135850.51044-15-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668570661314661?= X-GMAIL-MSGID: =?utf-8?q?1747668570661314661?= Placing timers at enqueue time on a target CPU based on dubious heuristics does not make any sense: 1) Most timer wheel timers are canceled or rearmed before they expire. 2) The heuristics to predict which CPU will be busy when the timer expires are wrong by definition. So placing the timers at enqueue wastes precious cycles. The proper solution to this problem is to always queue the timers on the local CPU and allow the non pinned timers to be pulled onto a busy CPU at expiry time. Therefore split the timer storage into local pinned and global timers: Local pinned timers are always expired on the CPU on which they have been queued. Global timers can be expired on any CPU. As long as a CPU is busy it expires both local and global timers. When a CPU goes idle it arms for the first expiring local timer. If the first expiring pinned (local) timer is before the first expiring movable timer, then no action is required because the CPU will wake up before the first movable timer expires. If the first expiring movable timer is before the first expiring pinned (local) timer, then this timer is queued into a idle timerqueue and eventually expired by some other active CPU. To avoid global locking the timerqueues are implemented as a hierarchy. The lowest level of the hierarchy holds the CPUs. The CPUs are associated to groups of 8, which are seperated per node. If more than one CPU group exist, then a second level in the hierarchy collects the groups. Depending on the size of the system more than 2 levels are required. Each group has a "migrator" which checks the timerqueue during the tick for remote expirable timers. If the last CPU in a group goes idle it reports the first expiring event in the group up to the next group(s) in the hierarchy. If the last CPU goes idle it arms its timer for the first system wide expiring timer to ensure that no timer event is missed. Signed-off-by: Anna-Maria Behnsen --- include/linux/cpuhotplug.h | 1 + kernel/time/Makefile | 3 + kernel/time/timer.c | 61 +- kernel/time/timer_migration.c | 1239 +++++++++++++++++++++++++++++++++ kernel/time/timer_migration.h | 121 ++++ 5 files changed, 1420 insertions(+), 5 deletions(-) create mode 100644 kernel/time/timer_migration.c create mode 100644 kernel/time/timer_migration.h diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h index f61447913db9..95e89495251a 100644 --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -244,6 +244,7 @@ enum cpuhp_state { CPUHP_AP_PERF_POWERPC_HV_24x7_ONLINE, CPUHP_AP_PERF_POWERPC_HV_GPCI_ONLINE, CPUHP_AP_PERF_CSKY_ONLINE, + CPUHP_AP_TMIGR_ONLINE, CPUHP_AP_WATCHDOG_ONLINE, CPUHP_AP_WORKQUEUE_ONLINE, CPUHP_AP_RANDOM_ONLINE, diff --git a/kernel/time/Makefile b/kernel/time/Makefile index 7e875e63ff3b..4af2a264a160 100644 --- a/kernel/time/Makefile +++ b/kernel/time/Makefile @@ -17,6 +17,9 @@ endif obj-$(CONFIG_GENERIC_SCHED_CLOCK) += sched_clock.o obj-$(CONFIG_TICK_ONESHOT) += tick-oneshot.o tick-sched.o obj-$(CONFIG_LEGACY_TIMER_TICK) += tick-legacy.o +ifeq ($(CONFIG_SMP),y) + obj-$(CONFIG_NO_HZ_COMMON) += timer_migration.o +endif obj-$(CONFIG_HAVE_GENERIC_VDSO) += vsyscall.o obj-$(CONFIG_DEBUG_FS) += timekeeping_debug.o obj-$(CONFIG_TEST_UDELAY) += test_udelay.o diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 0790ec8efe82..67f9cc077ff7 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -53,6 +53,7 @@ #include #include "tick-internal.h" +#include "timer_migration.h" #define CREATE_TRACE_POINTS #include @@ -592,10 +593,13 @@ trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer) /* * We might have to IPI the remote CPU if the base is idle and the - * timer is not deferrable. If the other CPU is on the way to idle - * then it can't set base->is_idle as we hold the base lock: + * timer is pinned. If it is a non pinned timer, it is only queued + * on the remote CPU, when timer was running during queueing. Then + * everything is handled by remote CPU anyway. If the other CPU is + * on the way to idle then it can't set base->is_idle as we hold + * the base lock: */ - if (base->is_idle) + if (base->is_idle && timer->flags & TIMER_PINNED) wake_up_nohz_cpu(base->cpu); } @@ -1719,7 +1723,7 @@ static unsigned long get_next_timer_interrupt(struct timer_base *base_local, * Check whether the local event is expiring before or at the same * time as the global event. * - * Note, that nextevt_global and next_evtlocal might be based on + * Note, that nextevt_global and nextevt_local might be based on * different base->clk values. So it's not guaranteed that * comparing with empty bases results in a correct local_first. */ @@ -1856,6 +1860,36 @@ void forward_and_idle_timer_bases(unsigned long basej, u64 basem, */ is_idle = time_after(nextevt, basej + 1); + if (is_idle) { + u64 next_tmigr; + + next_tmigr = tmigr_cpu_deactivate(tevt->global); + + tevt->global = KTIME_MAX; + + /* + * If CPU is the last going idle in timer migration + * hierarchy, make sure CPU will wake up in time to handle + * remote timers. next_tmigr == KTIME_MAX if other CPUs are + * still active. + */ + if (next_tmigr < tevt->local) { + u64 tmp; + + /* If we missed a tick already, force 0 delta */ + if (next_tmigr < basem) + next_tmigr = basem; + + tmp = div_u64(next_tmigr - basem, TICK_NSEC); + + nextevt = basej + (unsigned long)tmp; + tevt->local = next_tmigr; + is_idle = time_after(nextevt, basej + 1); + } else { + nextevt = nextevt_local; + } + } + /* We need to mark both bases in sync */ base_local->is_idle = base_global->is_idle = is_idle; @@ -1899,6 +1933,9 @@ void timer_clear_idle(void) base = this_cpu_ptr(&timer_bases[BASE_GLOBAL]); base->is_idle = false; + + /* Activate without holding the timer_base->lock */ + tmigr_cpu_activate(); } #endif @@ -1955,6 +1992,15 @@ static void run_timer_base(int index) __run_timer_base(base); } +#ifdef CONFIG_SMP +void timer_expire_remote(unsigned int cpu) +{ + struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu); + + __run_timer_base(base); +} +#endif + /* * This function runs timers and the timer-tq in bottom half context. */ @@ -1964,6 +2010,9 @@ static __latent_entropy void run_timer_softirq(struct softirq_action *h) if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) { run_timer_base(BASE_GLOBAL); run_timer_base(BASE_DEF); + + if (is_timers_nohz_active()) + tmigr_handle_remote(); } } @@ -1985,7 +2034,9 @@ static void run_local_timers(void) /* CPU is awake, so check the deferrable base. */ base++; if (time_before(jiffies, base->next_expiry)) - return; + /* CPU is awake, so check tmigr hierarchy. */ + if (!tmigr_requires_handle_remote()) + return; } } raise_softirq(TIMER_SOFTIRQ); diff --git a/kernel/time/timer_migration.c b/kernel/time/timer_migration.c new file mode 100644 index 000000000000..d68b8fd06a97 --- /dev/null +++ b/kernel/time/timer_migration.c @@ -0,0 +1,1239 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Infrastructure for migrateable timers + * + * Copyright(C) 2022 linutronix GmbH + */ +#include +#include +#include +#include +#include + +#include "timer_migration.h" +#include "tick-internal.h" + +/* + * The timer migration mechanism is built on a hierarchy of groups. The + * lowest level group contains CPUs, the next level groups of CPU groups + * and so forth. The CPU groups are kept per node so for the normal case + * lock contention won't happen accross nodes. Depending on the number of + * CPUs per node even the next level might be kept as groups of CPU groups + * per node and only the levels above cross the node topology. + * + * Example topology for a two node system with 24 CPUs each. + * + * LVL 2 [GRP2:0] + * GRP1:0 = GRP1:M + * + * LVL 1 [GRP1:0] [GRP1:1] + * GRP0:0 - GRP0:2 GRP0:3 - GRP0:5 + * + * LVL 0 [GRP0:0] [GRP0:1] [GRP0:2] [GRP0:3] [GRP0:4] [GRP0:5] + * CPUS 0-7 8-15 16-23 24-31 32-39 40-47 + * + * The groups hold a timer queue of events sorted by expiry time. These + * queues are updated when CPUs go in idle. When they come out of idle + * ignore bit of events is set. + * + * Each group has a designated migrator CPU/group as long as a CPU/group is + * active in the group. This designated role is necessary to avoid that all + * active CPUs in a group try to migrate expired timers from other cpus, + * which would result in massive lock bouncing. + * + * When a CPU is awake, it checks in it's own timer tick the group + * hierarchy up to the point where it is assigned the migrator role or if + * no CPU is active, it also checks the groups where no migrator is set + * (TMIGR_NONE). + * + * If it finds expired timers in one of the group queues it pulls them over + * from the idle CPU and runs the timer function. After that it updates the + * group and the parent groups if required. + * + * CPUs which go idle arm their CPU local timer hardware for the next local + * (pinned) timer event. If the next migrateable timer expires after the + * next local timer or the CPU has no migrateable timer pending then the + * CPU does not queue an event in the LVL0 group. If the next migrateable + * timer expires before the next local timer then the CPU queues that timer + * in the LVL0 group. In both cases the CPU marks itself idle in the LVL0 + * group. + * + * If the CPU is the migrator of the group then it delegates that role to + * the next active CPU in the group or sets migrator to TMIGR_NONE when + * there is no active CPU in the group. This delegation needs to be + * propagated up the hierarchy so hand over from other leaves can happen at + * all hierarchy levels w/o doing a search. + * + * When the last CPU in the system goes idle, then it drops all migrator + * duties up to the top level of the hierarchy (LVL2 in the example). It + * then has to make sure, that it arms it's own local hardware timer for + * the earliest event in the system. + * + * Lifetime rules: + * + * The groups are built up at init time or when CPUs come online. They are + * not destroyed when a group becomes empty due to offlining. The group + * just won't participate in the hierachary management anymore. Destroying + * groups would result in interesting race conditions which would just make + * the whole mechanism slow and complex. + * + * Locking rules: + * + * For setting up new groups and handling events it's required to lock both + * child and parent group. The lock odering is always bottom up. This also + * includes the per CPU locks in struct tmigr_cpu. For updating migrator + * and active CPU/group information atomic_cmpxchg() is used instead and + * only per CPU tmigr_cpu->lock is held. + * + * During setup of groups tmigr_level_list is required. It is protected by + * tmigr_mutex. + */ + +#ifdef DEBUG +# define DBG_BUG_ON(x) BUG_ON(x) +#else +# define DBG_BUG_ON(x) +#endif + +static DEFINE_MUTEX(tmigr_mutex); +static struct list_head *tmigr_level_list __read_mostly; + +static unsigned int tmigr_cores_per_group __read_mostly; +static unsigned int tmigr_hierarchy_levels __read_mostly; +static unsigned int tmigr_crossnode_level __read_mostly; + +static DEFINE_PER_CPU(struct tmigr_cpu, tmigr_cpu); + +#define TMIGR_NONE 0xFF +#define BIT_CNT 8 + +static DEFINE_STATIC_KEY_FALSE(tmigr_enabled); + +static inline bool is_tmigr_enabled(void) +{ + return static_branch_unlikely(&tmigr_enabled); +} + +/* + * Returns true, when @childmask corresponds to group migrator or when group + * is not active - so no migrator is set. + */ +static bool tmigr_check_migrator(struct tmigr_group *group, u32 childmask) +{ + union tmigr_state s; + + s.state = atomic_read(group->migr_state); + + if ((s.migrator != (u8)childmask) && (s.migrator != TMIGR_NONE)) + return false; + + return true; +} + +typedef bool (*up_f)(struct tmigr_group *, struct tmigr_group *, void *); + +static void __walk_groups(up_f up, void *data, + struct tmigr_cpu *tmc) +{ + struct tmigr_group *child = NULL, *group = tmc->tmgroup; + + do { + DBG_BUG_ON(group->level >= tmigr_hierarchy_levels); + + if (up(group, child, data)) + break; + + child = group; + group = group->parent; + } while (group); +} + +static void walk_groups(up_f up, void *data, struct tmigr_cpu *tmc) +{ + lockdep_assert_held(&tmc->lock); + + __walk_groups(up, data, tmc); +} + +/** + * struct tmigr_walk - data required for walking the hierarchy + * @evt: Pointer to tmigr_event which needs to be queued (of idle + * child group) + * @childmask: childmask of child group + * @nextexp: Next CPU event expiry information which is handed + * into tmigr code by timer code + * (forward_and_idle_timer_bases()); it is furthermore + * used for next event which is queued if timer + * migration hierarchy is completely idle + * @childstate: tmigr_group->migr_state of child - will be only reread + * when cmpxchg in group fails (is required for deactive + * path and new timer path) + * @groupstate: tmigr_group->migr_state of group - will be only reread + * when cmpxchg in group fails (is required for active, + * deactive and new timer path) + */ +struct tmigr_walk { + struct tmigr_event *evt; + u32 childmask; + u64 nextexp; + union tmigr_state childstate; + union tmigr_state groupstate; +}; + +/** + * struct tmigr_remote_data - data required for (check) remote expiry + * hierarchy walk + * @basej: timer base in jiffies + * @now: timer base monotonic + * @childmask: childmask of child group + * @check: is set to 1 if there is the need to handle remote timers; + * required in tmigr_check_handle_remote() only + * @wakeup: returns expiry of first timer in idle timer migration hierarchy + * to make sure timer is handled in time; it is stored in per CPU + * tmigr_cpu struct of CPU which expires remote timers + */ +struct tmigr_remote_data { + unsigned long basej; + u64 now; + u32 childmask; + int check; + u64 wakeup; +}; + +/* + * Returns next event of timerqueue @group->events + * + * Removes timers with ignore bits and update next_expiry and event cpu + * value in group. Expiry value of group event is updated in + * tmigr_update_events() only. + */ +static struct tmigr_event *tmigr_next_groupevt(struct tmigr_group *group) +{ + struct timerqueue_node *node = NULL; + struct tmigr_event *evt = NULL; + + lockdep_assert_held(&group->lock); + + group->next_expiry = KTIME_MAX; + + while ((node = timerqueue_getnext(&group->events))) { + evt = container_of(node, struct tmigr_event, nextevt); + + if (!test_bit(0, &evt->ignore)) { + group->next_expiry = evt->nextevt.expires; + return evt; + } + + /* + * Remove next timers with ignore bits, because group lock + * is held anyway + */ + if (!timerqueue_del(&group->events, node)) + break; + } + + return NULL; +} + +/* + * Return next event which is already expired of group timerqueue + * + * Event is also removed from queue. + */ +static struct tmigr_event *tmigr_next_expired_groupevt(struct tmigr_group *group, + u64 now) +{ + struct tmigr_event *evt = tmigr_next_groupevt(group); + + if (!evt || now < evt->nextevt.expires) + return NULL; + + /* + * Event is already expired. Remove it. If it's not the last event, + * then update all group event related information. + */ + if (timerqueue_del(&group->events, &evt->nextevt)) + tmigr_next_groupevt(group); + else + group->next_expiry = KTIME_MAX; + + return evt; +} + +static u64 tmigr_next_groupevt_expires(struct tmigr_group *group) +{ + struct tmigr_event *evt; + + evt = tmigr_next_groupevt(group); + + if (!evt) + return KTIME_MAX; + else + return evt->nextevt.expires; +} + +static bool tmigr_active_up(struct tmigr_group *group, + struct tmigr_group *child, + void *ptr) +{ + union tmigr_state curstate, newstate; + struct tmigr_walk *data = ptr; + bool walk_done; + u32 childmask; + + childmask = data->childmask; + newstate = curstate = data->groupstate; + +retry: + walk_done = true; + + if (newstate.migrator == TMIGR_NONE) { + newstate.migrator = (u8)childmask; + + /* Changes need to be propagated */ + walk_done = false; + } + + newstate.active |= (u8)childmask; + + newstate.seq++; + + if (atomic_cmpxchg(group->migr_state, curstate.state, newstate.state) != curstate.state) { + newstate.state = curstate.state = atomic_read(group->migr_state); + goto retry; + } + + if (group->parent && (walk_done == false)) { + data->groupstate.state = atomic_read(group->parent->migr_state); + data->childmask = group->childmask; + } + + /* + * Group is active, event will be ignored - bit is updated without + * holding the lock. In case bit is set while another CPU already + * handles remote events, nothing happens, because it is clear that + * CPU became active just in this moment, or in worst case event is + * handled remote. Nothing to worry about. + */ + set_bit(0, &group->groupevt.ignore); + + return walk_done; +} + +static void __tmigr_cpu_activate(struct tmigr_cpu *tmc) +{ + struct tmigr_walk data; + data.childmask = tmc->childmask; + data.groupstate.state = atomic_read(tmc->tmgroup->migr_state); + + set_bit(0, &tmc->cpuevt.ignore); + + walk_groups(&tmigr_active_up, &data, tmc); +} + +void tmigr_cpu_activate(void) +{ + struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu); + + if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online || !tmc->idle) + return; + + raw_spin_lock(&tmc->lock); + tmc->idle = 0; + tmc->wakeup = KTIME_MAX; + __tmigr_cpu_activate(tmc); + raw_spin_unlock(&tmc->lock); +} + +/* + * Returns true, if there is nothing to be propagated to the next level + * + * @data->nextexp is reset to KTIME_MAX; it is reused for first global + * event which needs to be handled by migrator (in toplevel group) + * + * This is the only place where group event expiry value is set. + */ +static bool tmigr_update_events(struct tmigr_group *group, + struct tmigr_group *child, + struct tmigr_walk *data) +{ + struct tmigr_event *evt, *first_childevt; + bool walk_done; + u64 nextexp; + + if (child) { + if (data->childstate.active) + return true; + + raw_spin_lock(&child->lock); + raw_spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING); + + first_childevt = tmigr_next_groupevt(child); + nextexp = child->next_expiry; + evt = &child->groupevt; + } else { + nextexp = data->nextexp; + + /* + * Set @data->nextexp to KTIME_MAX; it is reused for first + * global event which needs to be handled by migrator (in + * toplevel group) + */ + data->nextexp = KTIME_MAX; + + first_childevt = evt = data->evt; + if (test_bit(0, &evt->ignore)) + return true; + + raw_spin_lock(&group->lock); + } + + if (nextexp == KTIME_MAX) { + set_bit(0, &evt->ignore); + walk_done = true; + goto unlock; + } + + walk_done = !group->parent; + + /* + * Update of event cpu and ignore bit is required only when @child + * is set (child is equal or higher than lvl0), but it doesn't + * matter if it is written once more to per cpu event; make the + * update unconditional. + */ + evt->cpu = first_childevt->cpu; + clear_bit(0, &evt->ignore); + + /* + * If child event is already queued in group, remove it from queue + * when expiry time changed only + */ + if (timerqueue_node_queued(&evt->nextevt)) { + if (evt->nextevt.expires == nextexp) + goto check_toplvl; + + if (evt->nextevt.expires != nextexp && + !timerqueue_del(&group->events, &evt->nextevt)) + group->next_expiry = KTIME_MAX; + } + + evt->nextevt.expires = nextexp; + + if (timerqueue_add(&group->events, &evt->nextevt)) + group->next_expiry = nextexp; + +check_toplvl: + if (walk_done && (data->groupstate.migrator == TMIGR_NONE)) { + /* + * Toplevel group is idle and it has to be ensured global + * timers are handled in time. (This could be optimized by + * keeping track of the last global scheduled event and + * only arming it on CPU if the new event is earlier. Not + * sure if its worth the complexity.) + */ + data->nextexp = tmigr_next_groupevt_expires(group); + } + +unlock: + raw_spin_unlock(&group->lock); + + if (child) + raw_spin_unlock(&child->lock); + + return walk_done; +} + +static bool tmigr_new_timer_up(struct tmigr_group *group, + struct tmigr_group *child, + void *ptr) +{ + struct tmigr_walk *data = ptr; + bool walk_done; + + walk_done = tmigr_update_events(group, child, data); + + if (!walk_done) { + /* Update state information for next iteration */ + data->childstate.state = atomic_read(group->migr_state); + if (group->parent) + data->groupstate.state = atomic_read(group->parent->migr_state); + } + + return walk_done; +} + +/* + * Returns expiry of next timer that needs to be handled. KTIME_MAX is + * returned, when an active CPU will handle all timer migration hierarchy + * timers. + */ +static u64 tmigr_new_timer(struct tmigr_cpu *tmc, u64 nextexp) +{ + struct tmigr_walk data = { .evt = &tmc->cpuevt, + .nextexp = nextexp }; + + lockdep_assert_held(&tmc->lock); + + if (tmc->remote) + return KTIME_MAX; + + clear_bit(0, &tmc->cpuevt.ignore); + + data.groupstate.state = atomic_read(tmc->tmgroup->migr_state); + + walk_groups(&tmigr_new_timer_up, &data, tmc); + + /* If there is a new first global event, make sure it is handled */ + return data.nextexp; +} + +static bool tmigr_inactive_up(struct tmigr_group *group, + struct tmigr_group *child, + void *ptr) +{ + union tmigr_state curstate, newstate; + struct tmigr_walk *data = ptr; + bool walk_done; + u32 childmask; + + childmask = data->childmask; + newstate = curstate = data->groupstate; + +retry: + walk_done = true; + + /* Reset active bit when child is no longer active */ + if (!data->childstate.active) + newstate.active &= ~(u8)childmask; + + if (newstate.migrator == (u8)childmask) { + /* + * Find a new migrator for the group, because child group + * is idle! + */ + if (!data->childstate.active) { + unsigned long new_migr_bit; + + new_migr_bit = find_first_bit((const long *)&newstate.active, BIT_CNT); + + /* Changes need to be propagated */ + walk_done = false; + data->childmask = group->childmask; + + if (new_migr_bit != BIT_CNT) + newstate.migrator = BIT(new_migr_bit); + else + newstate.migrator = TMIGR_NONE; + } + } + + newstate.seq++; + + DBG_BUG_ON((newstate.migrator != TMIGR_NONE) && !(newstate.active)); + + if (atomic_cmpxchg(group->migr_state, curstate.state, newstate.state) != curstate.state) { + /* + * Something changed in child/parent group in the meantime, + * reread the state of child and parent; Update of + * data->childstate is required for event handling; + */ + if (child) + data->childstate.state = atomic_read(child->migr_state); + newstate.state = curstate.state = atomic_read(group->migr_state); + + goto retry; + } + + data->groupstate = newstate; + + /* Event Handling */ + tmigr_update_events(group, child, data); + + if (group->parent && (walk_done == false)) { + data->childstate = newstate; + data->groupstate.state = atomic_read(group->parent->migr_state); + } + + /* + * data->nextexp was set by tmigr_update_events() and contains the + * expiry of first global event which needs to be handled + */ + if (data->nextexp != KTIME_MAX) { + DBG_BUG_ON(group->parent); + /* + * Toplevel path - If this cpu is about going offline wake + * up some random other cpu so it will take over the + * migrator duty and program its timer properly. Ideally + * wake the cpu with the closest expiry time, but that's + * overkill to figure out. + */ + if (!(this_cpu_ptr(&tmigr_cpu)->online)) { + unsigned int cpu = smp_processor_id(); + + cpu = cpumask_any_but(cpu_online_mask, cpu); + smp_send_reschedule(cpu); + } + } + + return walk_done; +} + +static u64 __tmigr_cpu_deactivate(struct tmigr_cpu *tmc, u64 nextexp) +{ + struct tmigr_walk data = { .childmask = tmc->childmask, + .evt = &tmc->cpuevt, + .nextexp = nextexp, + .childstate.state = 0 }; + + data.groupstate.state = atomic_read(tmc->tmgroup->migr_state); + + /* + * If nextexp is KTIME_MAX, CPU event will be ignored because, + * local timer expires before global timer, no global timer is set + * or CPU goes offline. + */ + if (nextexp != KTIME_MAX) + clear_bit(0, &tmc->cpuevt.ignore); + + walk_groups(&tmigr_inactive_up, &data, tmc); + return data.nextexp; +} + +/** + * tmigr_cpu_deactivate - Put current CPU into inactive state + * @nextexp: The next timer event expiry set in the current CPU + * + * Must be called with interrupts disabled. + * + * Return: next event of the current CPU or next event from the hierarchy + * if this CPU is the top level migrator or hierarchy is completely idle. + */ +u64 tmigr_cpu_deactivate(u64 nextexp) +{ + struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu); + u64 ret = KTIME_MAX; + + if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online) + return ret; + + raw_spin_lock(&tmc->lock); + + /* + * CPU is already deactivated in timer migration + * hierarchy. tick_nohz_get_sleep_length() calls + * tick_nohz_next_event() and thereby timer idle path is + * executed once more. tmc->wakeup holds the first timer, when + * timer migration hierarchy is completely idle and remote + * expiry was done. If there is no new next expiry value + * handed in which should be inserted into the timer migration + * hierarchy, wakeup value is returned. + */ + if (tmc->idle) { + u64 ret = tmc->wakeup; + + tmc->wakeup = KTIME_MAX; + + if (nextexp != KTIME_MAX) { + if (nextexp != tmc->cpuevt.nextevt.expires || + test_bit(0, &tmc->cpuevt.ignore)) + ret = tmigr_new_timer(tmc, nextexp); + } + + goto unlock; + } + + /* + * When tmigr_remote is active, set cpu inactive path and queuing of + * nextexp is done by handle remote path. + */ + ret = __tmigr_cpu_deactivate(tmc, nextexp); + + tmc->idle = 1; + +unlock: + raw_spin_unlock(&tmc->lock); + return ret; +} + +static u64 tmigr_handle_remote_cpu(unsigned int cpu, u64 now, + unsigned long jif) +{ + struct timer_events tevt; + struct tmigr_walk data; + struct tmigr_cpu *tmc; + u64 next = KTIME_MAX; + unsigned long flags; + + tmc = per_cpu_ptr(&tmigr_cpu, cpu); + + raw_spin_lock_irqsave(&tmc->lock, flags); + /* + * Remote CPU is offline or no longer idle or other cpu handles cpu + * timers already or next event was already expired - return! + */ + if (!tmc->online || tmc->remote || test_bit(0, &tmc->cpuevt.ignore) || + now < tmc->cpuevt.nextevt.expires) { + raw_spin_unlock_irqrestore(&tmc->lock, flags); + return next; + } + + tmc->remote = 1; + + /* Drop the lock to allow the remote CPU to exit idle */ + raw_spin_unlock_irqrestore(&tmc->lock, flags); + + if (cpu != smp_processor_id()) + timer_expire_remote(cpu); + + /* next event of cpu */ + get_next_timer_interrupt_remote(jif, now, &tevt, cpu); + + raw_spin_lock_irqsave(&tmc->lock, flags); + /* + * Nothing more to do when CPU came out of idle in the meantime - needs + * to be checked when holding the base lock to prevent race. + */ + if (!tmc->idle) + goto unlock; + + data.evt = &tmc->cpuevt; + data.nextexp = tevt.global; + data.groupstate.state = atomic_read(tmc->tmgroup->migr_state); + clear_bit(0, &tmc->cpuevt.ignore); + + walk_groups(&tmigr_new_timer_up, &data, tmc); + + next = data.nextexp; + +unlock: + tmc->remote = 0; + raw_spin_unlock_irqrestore(&tmc->lock, flags); + + return next; +} + +static bool tmigr_handle_remote_up(struct tmigr_group *group, + struct tmigr_group *child, + void *ptr) +{ + struct tmigr_remote_data *data = ptr; + u64 now, next = KTIME_MAX; + unsigned long flags, jif; + struct tmigr_event *evt; + u32 childmask; + + jif = data->basej; + now = data->now; + + childmask = data->childmask; + +again: + /* + * Handle the group only if @childmask is the migrator or if the + * group has no migrator. Otherwise the group is active and is + * handled by its own migrator. + */ + if (!tmigr_check_migrator(group, childmask)) + return true; + + raw_spin_lock_irqsave(&group->lock, flags); + + evt = tmigr_next_expired_groupevt(group, now); + + if (evt) { + unsigned int remote_cpu; + + remote_cpu = READ_ONCE(evt->cpu); + + raw_spin_unlock_irqrestore(&group->lock, flags); + + next = tmigr_handle_remote_cpu(remote_cpu, now, jif); + + /* check if there is another event, that needs to be handled */ + goto again; + } else { + raw_spin_unlock_irqrestore(&group->lock, flags); + } + + /* Update of childmask for next level */ + data->childmask = group->childmask; + data->wakeup = next; + + return false; +} + +/** + * tmigr_handle_remote - Handle migratable timers on remote idle CPUs + * + * Called from the timer soft interrupt with interrupts enabled. + */ +void tmigr_handle_remote(void) +{ + struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu); + struct tmigr_remote_data data; + unsigned long flags; + + if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online) + return; + + /* + * NOTE: This is a doubled check because migrator test will be done + * in tmigr_handle_remote_up() anyway. Keep this check to fasten + * the return when nothing has to be done. + */ + if (!tmigr_check_migrator(tmc->tmgroup, tmc->childmask)) + return; + + data.now = get_jiffies_update(&data.basej); + data.childmask = tmc->childmask; + data.wakeup = KTIME_MAX; + + __walk_groups(&tmigr_handle_remote_up, &data, tmc); + + raw_spin_lock_irqsave(&tmc->lock, flags); + if (tmc->idle) + tmc->wakeup = data.wakeup; + + raw_spin_unlock_irqrestore(&tmc->lock, flags); + + return; +} + +static bool tmigr_requires_handle_remote_up(struct tmigr_group *group, + struct tmigr_group *child, + void *ptr) +{ + struct tmigr_remote_data *data = ptr; + u32 childmask; + + childmask = data->childmask; + + /* + * Handle the group only if child is the migrator or if the group + * has no migrator. Otherwise the group is active and is handled by + * its own migrator. + */ + if (!tmigr_check_migrator(group, childmask)) + return true; + + /* + * Racy lockless check for next_expiry + */ + if (data->now >= group->next_expiry) { + data->check = 1; + return true; + } + + /* Update of childmask for next level */ + data->childmask = group->childmask; + return false; +} + +int tmigr_requires_handle_remote(void) +{ + struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu); + struct tmigr_remote_data data; + + if (!is_tmigr_enabled() || !tmc->tmgroup || !tmc->online) + return 0; + + if (!tmigr_check_migrator(tmc->tmgroup, tmc->childmask)) + return 0; + + data.now = get_jiffies_update(&data.basej); + data.childmask = tmc->childmask; + + __walk_groups(&tmigr_requires_handle_remote_up, &data, tmc); + + return data.check; +} + +static void tmigr_init_group(struct tmigr_group *group, unsigned int lvl, + unsigned int node, atomic_t *migr_state) +{ + union tmigr_state s; + + raw_spin_lock_init(&group->lock); + + group->level = lvl; + group->numa_node = lvl < tmigr_crossnode_level ? node : NUMA_NO_NODE; + + group->num_childs = 0; + + /* + * num_cores is required for level=0 groups only during setup and + * when siblings exists but it doesn't matter if this value is set + * in other groups as well + */ + group->num_cores = 1; + + s.migrator = TMIGR_NONE; + s.active = 0; + s.seq = 0; + atomic_set(migr_state, s.state); + + group->migr_state = migr_state; + + timerqueue_init_head(&group->events); + timerqueue_init(&group->groupevt.nextevt); + group->groupevt.nextevt.expires = KTIME_MAX; + group->next_expiry = KTIME_MAX; + set_bit(0, &group->groupevt.ignore); +} + +static bool sibling_in_group(int newcpu, struct tmigr_group *group) +{ + int i, cpu; + + /* Find a sibling of newcpu in group members */ + for (i = 0; i < group->num_childs; i++) { + cpu = group->cpus[i]; + + if (cpumask_test_cpu(newcpu, topology_sibling_cpumask(cpu))) + return true; + } + return false; +} + +static struct tmigr_group *tmigr_get_group(unsigned int cpu, unsigned int node, + unsigned int lvl) +{ + struct tmigr_group *tmp, *group = NULL; + bool first_loop = true; + atomic_t *migr_state; + +reloop: + /* Try to attach to an exisiting group first */ + list_for_each_entry(tmp, &tmigr_level_list[lvl], list) { + /* + * If @lvl is below the cross numa node level, check whether + * this group belongs to the same numa node. + */ + if (lvl < tmigr_crossnode_level && tmp->numa_node != node) + continue; + + /* Capacity left? */ + if (tmp->num_childs >= TMIGR_CHILDS_PER_GROUP) + continue; + + /* + * If this is the lowest level of the hierarchy, make sure + * that thread siblings share a group. It is only executed + * when siblings exist. ALL groups of lowest level needs to + * be checked for thread sibling, before thread cpu is + * added to a random group with capacity. When all groups + * are checked and no thread sibling was found, reloop of + * level zero groups is required to get a group with + * capacity. + */ + if (!lvl && (tmigr_cores_per_group != TMIGR_CHILDS_PER_GROUP)) { + if (first_loop == true && !sibling_in_group(cpu, tmp)) { + continue; + } else if (first_loop == false) { + if (tmp->num_cores >= tmigr_cores_per_group) + continue; + else + tmp->num_cores++; + } + } + + group = tmp; + break; + } + + if (group) { + return group; + } else if (first_loop == true) { + first_loop = false; + goto reloop; + } + + /* Allocate and set up a new group with corresponding migr_state */ + group = kzalloc_node(sizeof(*group), GFP_KERNEL, node); + if (!group) + return ERR_PTR(-ENOMEM); + + migr_state = kzalloc_node(sizeof(atomic_t), GFP_KERNEL, node); + if (!migr_state) { + kfree(group); + return ERR_PTR(-ENOMEM); + } + + tmigr_init_group(group, lvl, node, migr_state); + /* Setup successful. Add it to the hierarchy */ + list_add(&group->list, &tmigr_level_list[lvl]); + return group; +} + +static void tmigr_connect_child_parent(struct tmigr_group *child, + struct tmigr_group *parent) +{ + union tmigr_state childstate; + unsigned long flags; + + raw_spin_lock_irqsave(&child->lock, flags); + raw_spin_lock_nested(&parent->lock, SINGLE_DEPTH_NESTING); + + child->parent = parent; + child->childmask = BIT(parent->num_childs++); + + raw_spin_unlock(&parent->lock); + raw_spin_unlock_irqrestore(&child->lock, flags); + + /* + * To prevent inconsistent states, active childs needs to be active + * in new parent as well. Inactive childs are already marked + * inactive in parent group. + */ + childstate.state = atomic_read(child->migr_state); + if (childstate.migrator != TMIGR_NONE) { + struct tmigr_walk data; + + data.childmask = child->childmask; + data.groupstate.state = atomic_read(parent->migr_state); + + /* + * There is only one new level per time. When connecting + * child and parent and set child active when parent is + * inactive, parent needs to be the upperst + * level. Otherwise there went something wrong! + */ + WARN_ON(!tmigr_active_up(parent, child, &data) && parent->parent); + } +} + +static int tmigr_setup_groups(unsigned int cpu, unsigned int node) +{ + struct tmigr_group *group, *child, **stack; + int top = 0, err = 0, i = 0; + struct list_head *lvllist; + size_t sz; + + sz = sizeof(struct tmigr_group *) * tmigr_hierarchy_levels; + stack = kzalloc(sz, GFP_KERNEL); + if (!stack) + return -ENOMEM; + + do { + group = tmigr_get_group(cpu, node, i); + if (IS_ERR(group)) { + err = IS_ERR(group); + break; + } + + top = i; + stack[i++] = group; + + /* + * When booting only less CPUs of a system than CPUs are + * available, not all calculated hierarchy levels are required. + * + * The loop is aborted as soon as the highest level, which might + * be different from tmigr_hierarchy_levels, contains only a + * single group. + */ + if (group->parent || i == tmigr_hierarchy_levels || + (list_empty(&tmigr_level_list[i]) && + list_is_singular(&tmigr_level_list[i - 1]))) + break; + + } while (i < tmigr_hierarchy_levels); + + do { + group = stack[--i]; + + if (err < 0) { + list_del(&group->list); + kfree(group); + continue; + } + + DBG_BUG_ON(i != group->level); + + /* + * Update tmc -> group / child -> group connection + */ + if (i == 0) { + struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu); + unsigned long flags; + + raw_spin_lock_irqsave(&group->lock, flags); + + tmc->tmgroup = group; + tmc->childmask = BIT(group->num_childs); + + group->cpus[group->num_childs++] = cpu; + + raw_spin_unlock_irqrestore(&group->lock, flags); + + /* There are no childs that needs to be connected */ + continue; + } else { + child = stack[i - 1]; + tmigr_connect_child_parent(child, group); + } + + /* check if upperst level was newly created */ + if (top != i) + continue; + + DBG_BUG_ON(top == 0); + + lvllist = &tmigr_level_list[top]; + if (group->num_childs == 1 && list_is_singular(lvllist)) { + lvllist = &tmigr_level_list[top - 1]; + list_for_each_entry(child, lvllist, list) { + if (child->parent) + continue; + + tmigr_connect_child_parent(child, group); + } + } + } while (i > 0); + + kfree(stack); + + return err; +} + +static int tmigr_add_cpu(unsigned int cpu) +{ + unsigned int node = cpu_to_node(cpu); + int ret; + mutex_lock(&tmigr_mutex); + ret = tmigr_setup_groups(cpu, node); + mutex_unlock(&tmigr_mutex); + + return ret; +} + +static int tmigr_cpu_online(unsigned int cpu) +{ + struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu); + unsigned long flags; + unsigned int ret; + + /* First online attempt? Initialize CPU data */ + if (!tmc->tmgroup) { + raw_spin_lock_init(&tmc->lock); + + ret = tmigr_add_cpu(cpu); + if (ret < 0) + return ret; + + if (tmc->childmask == 0) + return -EINVAL; + + timerqueue_init(&tmc->cpuevt.nextevt); + tmc->cpuevt.nextevt.expires = KTIME_MAX; + set_bit(0, &tmc->cpuevt.ignore); + tmc->cpuevt.cpu = cpu; + + tmc->remote = 0; + tmc->idle = 0; + tmc->wakeup = KTIME_MAX; + } + raw_spin_lock_irqsave(&tmc->lock, flags); + __tmigr_cpu_activate(tmc); + tmc->online = 1; + raw_spin_unlock_irqrestore(&tmc->lock, flags); + return 0; +} + +static int tmigr_cpu_offline(unsigned int cpu) +{ + struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu); + + raw_spin_lock_irq(&tmc->lock); + tmc->online = 0; + __tmigr_cpu_deactivate(tmc, KTIME_MAX); + raw_spin_unlock_irq(&tmc->lock); + + return 0; +} + +static int __init tmigr_init(void) +{ + unsigned int cpulvl, nodelvl, cpus_per_node, i, ns; + unsigned int nnodes = num_possible_nodes(); + unsigned int ncpus = num_possible_cpus(); + int ret = -ENOMEM; + size_t sz; + + /* Nothing to do if running on UP */ + if (ncpus == 1) + return 0; + + /* + * Unfortunately there is no reliable way to determine the number of SMT + * siblings in a generic way. tmigr_init() is called after SMP bringup, + * so for the normal boot case it can be assumed that all siblings have + * been brought up and the number of siblings of the current cpu can be + * used. If someone booted with 'maxcpus=N/2' on the kernel command line + * and (at least x86) bring up the siblings later then the siblings will + * end up in different groups. Bad luck. + */ + ns = cpumask_weight(topology_sibling_cpumask(raw_smp_processor_id())); + tmigr_cores_per_group = TMIGR_CHILDS_PER_GROUP; + if (ns >= 2 && ns < TMIGR_CHILDS_PER_GROUP) + tmigr_cores_per_group /= ns; + + /* + * Calculate the required hierarchy levels. Unfortunately there is no + * reliable information available, unless all possible CPUs have been + * brought up and all numa nodes are populated. + * + * Estimate the number of levels with the number of possible nodes and + * the number of possible cpus. Assume CPUs are spread evenly accross + * nodes. We cannot rely on cpumask_of_node() because there only already + * online CPUs are considered. + */ + cpus_per_node = DIV_ROUND_UP(ncpus, nnodes); + + /* Calc the hierarchy levels required to hold the CPUs of a node */ + cpulvl = DIV_ROUND_UP(order_base_2(cpus_per_node), + ilog2(TMIGR_CHILDS_PER_GROUP)); + + /* Calculate the extra levels to connect all nodes */ + nodelvl = DIV_ROUND_UP(order_base_2(nnodes), + ilog2(TMIGR_CHILDS_PER_GROUP)); + + tmigr_hierarchy_levels = cpulvl + nodelvl; + + /* + * If a numa node spawns more than one CPU level group then the next + * level(s) of the hierarchy contains groups which handle all CPU groups + * of the same numa node. The level above goes accross numa nodes. Store + * this information for the setup code to decide when node matching is + * not longer required. + */ + tmigr_crossnode_level = cpulvl; + + sz = sizeof(struct list_head) * tmigr_hierarchy_levels; + tmigr_level_list = kzalloc(sz, GFP_KERNEL); + if (!tmigr_level_list) + goto err; + + for (i = 0; i < tmigr_hierarchy_levels; i++) + INIT_LIST_HEAD(&tmigr_level_list[i]); + + pr_info("Timer migration: %d hierarchy levels; %d childs per group;" + " %d cores_per_group; %d crossnode level\n", + tmigr_hierarchy_levels, TMIGR_CHILDS_PER_GROUP, + tmigr_cores_per_group, tmigr_crossnode_level); + + ret = cpuhp_setup_state(CPUHP_AP_TMIGR_ONLINE, "tmigr:online", + tmigr_cpu_online, tmigr_cpu_offline); + if (ret) + goto err; + + static_branch_enable(&tmigr_enabled); + + return 0; + +err: + pr_err("Timer migration setup failed\n"); + return ret; +} +late_initcall(tmigr_init); diff --git a/kernel/time/timer_migration.h b/kernel/time/timer_migration.h new file mode 100644 index 000000000000..42d7550ea231 --- /dev/null +++ b/kernel/time/timer_migration.h @@ -0,0 +1,121 @@ +#ifndef _KERNEL_TIME_MIGRATION_H +#define _KERNEL_TIME_MIGRATION_H + +/* Per group capacity. Must be a power of 2! */ +#define TMIGR_CHILDS_PER_GROUP 8 + +/** + * struct tmigr_event - a timer event associated to a CPU + * @nextevt: The node to enqueue an event in the parent group queue + * @cpu: The CPU to which this event belongs + * @ignore: Hint whether the event could be ignored; it is set when + * CPU or group is active; + */ +struct tmigr_event { + struct timerqueue_node nextevt; + unsigned int cpu; + unsigned long ignore; +}; + +/** + * struct tmigr_group - timer migration hierarchy group + * @lock: Lock protecting the event information + * @cpus: Array with CPUs which are member of group; required for + * sibling CPUs; used only when level == 0 + * @parent: Pointer to parent group + * @list: List head that is added to per level tmigr_level_list + * @level: Hierarchy level of group + * @numa_node: Is set to numa node when level < tmigr_crossnode_level; + * otherwise it is set to NUMA_NO_NODE; Required for setup + * only + * @num_childs: Counter of group childs; Required for setup only + * @num_cores: Counter of cores per group; Required for setup only when + * level == 0 and siblings exist + * @migr_state: State of group (see struct tmigr_state) + * @childmask: childmask of group in parent group; is set during setup + * never changed; could be read lockless + * @events: Timer queue for child events queued in the group + * @groupevt: Next event of group; it is only reliable when group is + * !active (ignore bit is set when group is active) + * @next_expiry: Base monotonic expiry time of next event of group; + * Used for racy lockless check whether remote expiry is + * required; it is always reliable + */ +struct tmigr_group { + raw_spinlock_t lock; + unsigned int cpus[TMIGR_CHILDS_PER_GROUP]; + struct tmigr_group *parent; + struct list_head list; + unsigned int level; + unsigned int numa_node; + unsigned int num_childs; + unsigned int num_cores; + atomic_t *migr_state; + u32 childmask; + struct timerqueue_head events; + struct tmigr_event groupevt; + u64 next_expiry; +}; + +/** + * struct tmigr_cpu - timer migration per CPU group + * @lock: Lock protecting tmigr_cpu group information + * @online: Indicates wheter CPU is online + * @idle: Indicates wheter CPU is idle in timer migration hierarchy + * @remote: Is set when timers of CPU are expired remote + * @tmgroup: Pointer to parent group + * @childmask: childmask of tmigr_cpu in parent group + * @cpuevt: CPU event which could be queued into parent group + * @wakeup: Stores the first timer when the timer migration hierarchy is + * completely idle and remote expiry was done; is returned to + * timer code when tmigr_cpu_deactive() is called and group is + * idle; afterwards a reset to KTIME_MAX is required; + */ +struct tmigr_cpu { + raw_spinlock_t lock; + int online; + int idle; + int remote; + struct tmigr_group *tmgroup; + u32 childmask; + struct tmigr_event cpuevt; + u64 wakeup; +}; + +/** + * union tmigr_state - state of tmigr_group + * @complete: Combined version of the state + * @split: Splitted version of the state + */ +union tmigr_state { + u32 state; + /** + * struct - splitted state of tmigr_group + * @active: Contains each childmask bit of active childs + * @migrator: Contains childmask of child which is migrator + * @seq: Seqence number to prevent race when update in child + * group are propagated in wrong order (especially when + * migrator changes are involved) + */ + struct { + u8 active; + u8 migrator; + u16 seq; + } __packed; +}; + +#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON) +extern void tmigr_handle_remote(void); +extern int tmigr_requires_handle_remote(void); +extern void tmigr_cpu_activate(void); +extern u64 tmigr_cpu_deactivate(u64 nextevt); +extern void timer_expire_remote(unsigned int cpu); +#else +static inline void tmigr_handle_remote(void) { } +extern inline int tmigr_requires_handle_remote(void) { return 0; } +static inline void tmigr_cpu_activate(void) { } +static inline u64 tmigr_cpu_deactivate(u64 nextevt) { return KTIME_MAX; } +extern inline void timer_expire_remote(unsigned int cpu) { } +#endif + +#endif From patchwork Tue Oct 25 13:58:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10809 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1026289wru; Tue, 25 Oct 2022 07:05:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM54GI8e4Xz2+t/bDwAtswJg7OoXj8J1q0aflTvmB5KDeQguVI1HmLKhVkPC7S7TH3bTbQg0 X-Received: by 2002:a17:902:bf08:b0:178:90fb:8cda with SMTP id bi8-20020a170902bf0800b0017890fb8cdamr38340522plb.9.1666706721995; Tue, 25 Oct 2022 07:05:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706721; cv=none; d=google.com; s=arc-20160816; b=DKAKVl+NFrOVOncz/yIwovCJvFuMMxTdFVBYtgguohxmFIOBxB/QUzXksWRICcSoy9 XvwB7Wqpax/SjDBKT7kFVEGgAb5eGB9AJxqWeipn4UJGHTayC32u0Gwb3q8MCopLEGla 3z2B94gDyR8QrGzftgeIsRuhdDw7wEwV9oqXiKJ+F04rIj9YTybkEgv8F1qmAkv0ZTLe tJ49ZH4v4M971L7XWzyZrPQ6w3OEIAbrSYFXWN9fkqMO90RDV8ElwaGFFCu/qOchjwEN AK8F0TIs3nL3p6NvP4vRfssrKWp70e5rzNoJnR78jikLZ40v5gZRq9faIo7kpnOoY1gT 6PMA== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=3r8eFvXX7ewTWx91C3hEXLRF8LG7h66hSM9FnLvuvbY=; b=hABIrLxV0GkEEuhhL4WSKNHiX/obQLoj3O5ocQqSUF4Cvd6tHqbp598riP5L3O0ztS 2aR9ykPzKZ21xQPyOEqA4booLXyiCX5cNk/4gUiiSmI+4+iEJx5kPYaymAZ8RFWP64q6 yUqqcWG1PmY1GanZpPVHwh2I8PjvhZNgokiiA7VAWSsXGK6UKWQqgJxm+OzrwpWzp95Y ClWh7TyBtxUPdyixWKURG/1THKUtTibhQbjQI96+zh4oo+kyavVCMHQZWWmuX8BR2JOE gC33hGjrOc1thlK/suXdYco4MpZqed55mTlTpt6oL/7A1Ar9BIdh4W0wh2fLTYzAAGHf kDJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wClEbjJs; dkim=neutral (no key) header.i=@linutronix.de header.b=JleMxpC5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bx17-20020a056a00429100b0055706edd79csi3070233pfb.240.2022.10.25.07.05.08; Tue, 25 Oct 2022 07:05:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wClEbjJs; dkim=neutral (no key) header.i=@linutronix.de header.b=JleMxpC5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230388AbiJYOBJ (ORCPT + 99 others); Tue, 25 Oct 2022 10:01:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232174AbiJYN7h (ORCPT ); Tue, 25 Oct 2022 09:59:37 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AB56E0BC; Tue, 25 Oct 2022 06:59:30 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706369; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3r8eFvXX7ewTWx91C3hEXLRF8LG7h66hSM9FnLvuvbY=; b=wClEbjJseJEAEj1agp1ZPP+vXzt++cAMTswcwsjpza8uM4kPm3Ndw5nGzdByxBJyy6XnYj pKpCZFQpmh2c26XnKRSz2WgpFCQotX3eW5Fl6LqxEJvEqyWf2vTcRrbps7Xob1oLfyJapq BYP/EkZHcZDe/4v3t2ExSLJf3vjmRMo1lIDEUgc+IR/LPlMVrezjVaXZSa9Gi+CXTtA1m6 dvvT+VSBkd3B3dw8amtXgd6Ga1aTUIuzJakP/Gn61aQatEke0E7eh16pqQDfCzazq4HTgk PC5z4UiGNzZHgekKKOa0fRBfJo6IESMAXwb9L5XqijA7qUoY8WOa5h8ycpTftQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706369; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3r8eFvXX7ewTWx91C3hEXLRF8LG7h66hSM9FnLvuvbY=; b=JleMxpC5zVweo7sN4lVtElzmCTyxCBVd3omw897K6J4kqYM9TFA2vNfwz1/gzczHD76eER EnsNeZmfc+s3mnAw== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 15/17] timer_migration: Add tracepoints Date: Tue, 25 Oct 2022 15:58:48 +0200 Message-Id: <20221025135850.51044-16-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668668005697260?= X-GMAIL-MSGID: =?utf-8?q?1747668668005697260?= The timer pull logic needs proper debugging aids. Add tracepoints so the hierarchical idle machinery can be diagnosed. Signed-off-by: Anna-Maria Behnsen --- include/trace/events/timer_migration.h | 277 +++++++++++++++++++++++++ kernel/time/timer_migration.c | 24 +++ 2 files changed, 301 insertions(+) create mode 100644 include/trace/events/timer_migration.h diff --git a/include/trace/events/timer_migration.h b/include/trace/events/timer_migration.h new file mode 100644 index 000000000000..0c4824056930 --- /dev/null +++ b/include/trace/events/timer_migration.h @@ -0,0 +1,277 @@ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM timer_migration + +#if !defined(_TRACE_TIMER_MIGRATION_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_TIMER_MIGRATION_H + +#include + +/* Group events */ +TRACE_EVENT(tmigr_group_set, + + TP_PROTO(struct tmigr_group *group), + + TP_ARGS(group), + + TP_STRUCT__entry( + __field( void *, group ) + __field( unsigned int, lvl ) + __field( unsigned int, numa_node ) + ), + + TP_fast_assign( + __entry->group = group; + __entry->lvl = group->level; + __entry->numa_node = group->numa_node; + ), + + TP_printk("group=%p lvl=%d numa=%d", + __entry->group, __entry->lvl, __entry->numa_node) +); + +TRACE_EVENT(tmigr_connect_child_parent, + + TP_PROTO(struct tmigr_group *child), + + TP_ARGS(child), + + TP_STRUCT__entry( + __field( void *, child ) + __field( void *, parent ) + __field( unsigned int, lvl ) + __field( unsigned int, numa_node ) + __field( unsigned int, num_childs ) + __field( u32, childmask ) + ), + + TP_fast_assign( + __entry->child = child; + __entry->parent = child->parent; + __entry->lvl = child->parent->level; + __entry->numa_node = child->parent->numa_node; + __entry->numa_node = child->parent->num_childs; + __entry->childmask = child->childmask; + ), + + TP_printk("group=%p childmask=%0x parent=%p lvl=%d numa=%d num_childs=%d", + __entry->child, __entry->childmask, __entry->parent, + __entry->lvl, __entry->numa_node, __entry->num_childs) +); + +TRACE_EVENT(tmigr_connect_cpu_parent, + + TP_PROTO(struct tmigr_cpu *tmc), + + TP_ARGS(tmc), + + TP_STRUCT__entry( + __field( void *, parent ) + __field( unsigned int, cpu ) + __field( unsigned int, lvl ) + __field( unsigned int, numa_node ) + __field( unsigned int, num_childs ) + __field( u32, childmask ) + ), + + TP_fast_assign( + __entry->parent = tmc->tmgroup; + __entry->cpu = tmc->cpuevt.cpu; + __entry->lvl = tmc->tmgroup->level; + __entry->numa_node = tmc->tmgroup->numa_node; + __entry->numa_node = tmc->tmgroup->num_childs; + __entry->childmask = tmc->childmask; + ), + + TP_printk("cpu=%d childmask=%0x parent=%p lvl=%d numa=%d num_childs=%d", + __entry->cpu, __entry->childmask, __entry->parent, + __entry->lvl, __entry->numa_node, __entry->num_childs) +); + +DECLARE_EVENT_CLASS(tmigr_group_and_cpu, + + TP_PROTO(struct tmigr_group *group, union tmigr_state state, u32 childmask), + + TP_ARGS(group, state, childmask), + + TP_STRUCT__entry( + __field( void *, group ) + __field( void *, parent ) + __field( unsigned int, lvl ) + __field( unsigned int, numa_node ) + __field( u8, active ) + __field( u8, migrator ) + __field( u32, childmask ) + ), + + TP_fast_assign( + __entry->group = group; + __entry->parent = group->parent; + __entry->lvl = group->level; + __entry->numa_node = group->numa_node; + __entry->active = state.active; + __entry->migrator = state.migrator; + __entry->childmask = childmask; + ), + + TP_printk("group=%p lvl=%d numa=%d active=%0x migrator=%0x " + "parent=%p childmask=%0x", + __entry->group, __entry->lvl, __entry->numa_node, + __entry->active, __entry->migrator, + __entry->parent, __entry->childmask) +); + +DEFINE_EVENT(tmigr_group_and_cpu, tmigr_group_set_cpu_inactive, + + TP_PROTO(struct tmigr_group *group, union tmigr_state state, u32 childmask), + + TP_ARGS(group, state, childmask) +); + +DEFINE_EVENT(tmigr_group_and_cpu, tmigr_group_set_cpu_active, + + TP_PROTO(struct tmigr_group *group, union tmigr_state state, u32 childmask), + + TP_ARGS(group, state, childmask) +); + +/* CPU events*/ +DECLARE_EVENT_CLASS(tmigr_cpugroup, + + TP_PROTO(struct tmigr_cpu *tmc), + + TP_ARGS(tmc), + + TP_STRUCT__entry( + __field( void *, parent) + __field( unsigned int, cpu) + ), + + TP_fast_assign( + __entry->cpu = tmc->cpuevt.cpu; + __entry->parent = tmc->tmgroup; + ), + + TP_printk("cpu=%d parent=%p", __entry->cpu, __entry->parent) +); + +DEFINE_EVENT(tmigr_cpugroup, tmigr_cpu_new_timer, + + TP_PROTO(struct tmigr_cpu *tmc), + + TP_ARGS(tmc) +); + +DEFINE_EVENT(tmigr_cpugroup, tmigr_cpu_active, + + TP_PROTO(struct tmigr_cpu *tmc), + + TP_ARGS(tmc) +); + +DEFINE_EVENT(tmigr_cpugroup, tmigr_cpu_online, + + TP_PROTO(struct tmigr_cpu *tmc), + + TP_ARGS(tmc) +); + +DEFINE_EVENT(tmigr_cpugroup, tmigr_cpu_offline, + + TP_PROTO(struct tmigr_cpu *tmc), + + TP_ARGS(tmc) +); + +DEFINE_EVENT(tmigr_cpugroup, tmigr_handle_remote_cpu, + + TP_PROTO(struct tmigr_cpu *tmc), + + TP_ARGS(tmc) +); + +TRACE_EVENT(tmigr_cpu_idle, + + TP_PROTO(struct tmigr_cpu *tmc, u64 nextevt), + + TP_ARGS(tmc, nextevt), + + TP_STRUCT__entry( + __field( void *, parent) + __field( unsigned int, cpu) + __field( u64, nextevt) + ), + + TP_fast_assign( + __entry->cpu = tmc->cpuevt.cpu; + __entry->parent = tmc->tmgroup; + __entry->nextevt = nextevt; + ), + + TP_printk("cpu=%d parent=%p nextevt=%llu", + __entry->cpu, __entry->parent, __entry->nextevt) +); + +TRACE_EVENT(tmigr_update_events, + + TP_PROTO(struct tmigr_group *child, struct tmigr_group *group, + union tmigr_state childstate, union tmigr_state groupstate, + u64 nextevt), + + TP_ARGS(child, group, childstate, groupstate, nextevt), + + TP_STRUCT__entry( + __field( void *, child ) + __field( void *, group ) + __field( u64, nextevt ) + __field( u64, group_next_expiry ) + __field( unsigned int, group_lvl ) + __field( u8, child_active ) + __field( u8, group_active ) + __field( unsigned int, child_evtcpu ) + __field( u64, child_evt_expiry ) + ), + + TP_fast_assign( + __entry->child = child; + __entry->group = group; + __entry->nextevt = nextevt; + __entry->group_next_expiry = group->next_expiry; + __entry->group_lvl = group->level; + __entry->child_active = childstate.active; + __entry->group_active = groupstate.active; + __entry->child_evtcpu = child ? child->groupevt.cpu : 0; + __entry->child_evt_expiry = child ? child->groupevt.nextevt.expires : 0; + ), + + TP_printk("child=%p group=%p group_lvl=%d child_active=%0x group_active=%0x " + "nextevt=%llu next_expiry=%llu child_evt_expiry=%llu child_evtcpu=%d", + __entry->child, __entry->group, __entry->group_lvl, __entry->child_active, + __entry->group_active, + __entry->nextevt, __entry->group_next_expiry, __entry->child_evt_expiry, + __entry->child_evtcpu) +); + +TRACE_EVENT(tmigr_handle_remote, + + TP_PROTO(struct tmigr_group *group), + + TP_ARGS(group), + + TP_STRUCT__entry( + __field( void * , group ) + __field( unsigned int , lvl ) + ), + + TP_fast_assign( + __entry->group = group; + __entry->lvl = group->level; + ), + + TP_printk("group=%p lvl=%d", + __entry->group, __entry->lvl) +); + +#endif /* _TRACE_TIMER_MIGRATION_H */ + +/* This part must be outside protection */ +#include diff --git a/kernel/time/timer_migration.c b/kernel/time/timer_migration.c index d68b8fd06a97..6b954e034959 100644 --- a/kernel/time/timer_migration.c +++ b/kernel/time/timer_migration.c @@ -13,6 +13,9 @@ #include "timer_migration.h" #include "tick-internal.h" +#define CREATE_TRACE_POINTS +#include + /* * The timer migration mechanism is built on a hierarchy of groups. The * lowest level group contains CPUs, the next level groups of CPU groups @@ -317,6 +320,8 @@ static bool tmigr_active_up(struct tmigr_group *group, */ set_bit(0, &group->groupevt.ignore); + trace_tmigr_group_set_cpu_active(group, newstate, childmask); + return walk_done; } @@ -341,6 +346,7 @@ void tmigr_cpu_activate(void) raw_spin_lock(&tmc->lock); tmc->idle = 0; tmc->wakeup = KTIME_MAX; + trace_tmigr_cpu_active(tmc); __tmigr_cpu_activate(tmc); raw_spin_unlock(&tmc->lock); } @@ -435,6 +441,9 @@ static bool tmigr_update_events(struct tmigr_group *group, data->nextexp = tmigr_next_groupevt_expires(group); } + trace_tmigr_update_events(child, group, data->childstate, + data->groupstate, nextexp); + unlock: raw_spin_unlock(&group->lock); @@ -478,6 +487,8 @@ static u64 tmigr_new_timer(struct tmigr_cpu *tmc, u64 nextexp) if (tmc->remote) return KTIME_MAX; + trace_tmigr_cpu_new_timer(tmc); + clear_bit(0, &tmc->cpuevt.ignore); data.groupstate.state = atomic_read(tmc->tmgroup->migr_state); @@ -576,6 +587,8 @@ static bool tmigr_inactive_up(struct tmigr_group *group, } } + trace_tmigr_group_set_cpu_inactive(group, data->childstate, childmask); + return walk_done; } @@ -652,6 +665,7 @@ u64 tmigr_cpu_deactivate(u64 nextexp) tmc->idle = 1; unlock: + trace_tmigr_cpu_idle(tmc, ret); raw_spin_unlock(&tmc->lock); return ret; } @@ -678,6 +692,8 @@ static u64 tmigr_handle_remote_cpu(unsigned int cpu, u64 now, return next; } + trace_tmigr_handle_remote_cpu(tmc); + tmc->remote = 1; /* Drop the lock to allow the remote CPU to exit idle */ @@ -728,6 +744,7 @@ static bool tmigr_handle_remote_up(struct tmigr_group *group, childmask = data->childmask; + trace_tmigr_handle_remote(group); again: /* * Handle the group only if @childmask is the migrator or if the @@ -963,6 +980,7 @@ static struct tmigr_group *tmigr_get_group(unsigned int cpu, unsigned int node, tmigr_init_group(group, lvl, node, migr_state); /* Setup successful. Add it to the hierarchy */ list_add(&group->list, &tmigr_level_list[lvl]); + trace_tmigr_group_set(group); return group; } @@ -981,6 +999,8 @@ static void tmigr_connect_child_parent(struct tmigr_group *child, raw_spin_unlock(&parent->lock); raw_spin_unlock_irqrestore(&child->lock, flags); + trace_tmigr_connect_child_parent(child); + /* * To prevent inconsistent states, active childs needs to be active * in new parent as well. Inactive childs are already marked @@ -1067,6 +1087,8 @@ static int tmigr_setup_groups(unsigned int cpu, unsigned int node) raw_spin_unlock_irqrestore(&group->lock, flags); + trace_tmigr_connect_cpu_parent(tmc); + /* There are no childs that needs to be connected */ continue; } else { @@ -1135,6 +1157,7 @@ static int tmigr_cpu_online(unsigned int cpu) tmc->wakeup = KTIME_MAX; } raw_spin_lock_irqsave(&tmc->lock, flags); + trace_tmigr_cpu_online(tmc); __tmigr_cpu_activate(tmc); tmc->online = 1; raw_spin_unlock_irqrestore(&tmc->lock, flags); @@ -1148,6 +1171,7 @@ static int tmigr_cpu_offline(unsigned int cpu) raw_spin_lock_irq(&tmc->lock); tmc->online = 0; __tmigr_cpu_deactivate(tmc, KTIME_MAX); + trace_tmigr_cpu_offline(tmc); raw_spin_unlock_irq(&tmc->lock); return 0; From patchwork Tue Oct 25 13:58:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10808 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1026077wru; Tue, 25 Oct 2022 07:05:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM56+2Fr6uHn2B+Vd6aLC6cZ91k3ZAzTeCGnYPvAbYlwNxPrvAwoqX02ZQbSTdI38JJb+0jg X-Received: by 2002:a17:903:1d1:b0:17f:6494:f8c3 with SMTP id e17-20020a17090301d100b0017f6494f8c3mr38518604plh.157.1666706700561; Tue, 25 Oct 2022 07:05:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706700; cv=none; d=google.com; s=arc-20160816; b=m1fcDGMG7rffj3AnKkTaxyVtsnGmLqe2SPtwLLu5PaZxAnTpsvXaPIrpGnto/rsBvf gE747ztd5ohQ6HKz4XKgtTjiwFP7ap/2WpU49N77xb7v6T8IhqXXR6qDCTtQ71Z1XwWP rBjn5qSRoQ+xnhgcr9kAPouGk/LuK/Mn+rAIlB0rX90SCIOhk+75D0XIkr/TA82Ojd9C wb1PpX6wP2Y1pzHB9OeG9snIOEKaxhiAspszXZqip7PE7G08u8L0GCXi+IWJT7evHawJ FlnukLlP/cbwnx8ghSCw+sEeBfpBHsWVMLibEnaWr6hUkl2j5zJoKwvbyPofm67KwkKJ cuYg== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=t38ZOSk7RADUS9yQPx+jhxDPVyx+Ww7vTN6ZsN90lKg=; b=lKkytLj5m0MWQVqN0vSv9fXr4StV4cST2IH5Oc6HDfkoLihTS9Wm3WQlnKNJwPbMiJ znSgeDy/odnTEf7RA70wgT2BdqHIgbPRXFFQKppQUaT4yqVwyMWvYwmWHNBjMi4Hurds j8DmtMSS2wAmvgKmLvq6MxHGrahPNIt4xS2pKg2sZQi3CIhWT916ybHiWjMkwZO3pmt9 EZAc1519x4VyVP2+9/7EPi9gH/W/xA8vf/0vFevH+2lkzo0eseuaZyp2eDXf9+n42+xI qr5CmXvYRMW+FrdP8du7e/RaGRNN81aMw82mOUtmjQUJyiscNuJWhLBtNZnEDEFQTHkH E5jA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=bEwlhGzp; dkim=neutral (no key) header.i=@linutronix.de header.b=WydopO46; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m7-20020a656a07000000b00438e839828bsi3461125pgu.72.2022.10.25.07.04.39; Tue, 25 Oct 2022 07:05:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=bEwlhGzp; dkim=neutral (no key) header.i=@linutronix.de header.b=WydopO46; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232786AbiJYOBD (ORCPT + 99 others); Tue, 25 Oct 2022 10:01:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232169AbiJYN7h (ORCPT ); Tue, 25 Oct 2022 09:59:37 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C446FAC9; Tue, 25 Oct 2022 06:59:31 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706370; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=t38ZOSk7RADUS9yQPx+jhxDPVyx+Ww7vTN6ZsN90lKg=; b=bEwlhGzpXpX8hMG8IqtPZmhlQRTQikKxqNs+gbQgfv3WFD96OO3tUpQT4EGa3g/BzQnubQ YJSxR22rP/7t7RYGTs+tiAigsONu+jLzODJiGTM/3aFyR1hciqB+lBSGwaCnd2Th7JbGQq uwSOnET8vkJWh2fJdQx7Pue1BuPsppC5/Vnq2Kf4G1QWNwkR7MP7bc6yZ3yGh4vYfD9AGW NcMkYkdYIzu/1fLOmu4BzSl8JZJExpqx06XXG7SZsxGs6MycZ4DWUgN6eLrtxp1JzRVe61 FhrUh86UP3a4VJCA0facxuPBjkZB5p26oaplpC/jxnH/cB37pSiJfUs6KLYmgQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706370; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=t38ZOSk7RADUS9yQPx+jhxDPVyx+Ww7vTN6ZsN90lKg=; b=WydopO46wwu45sP+yRfKw5ZwrGNEVisF1hJAUAO3vVDu9Yj/PohrKCIGU8fAEgVTpFn5D9 G2QbL4hSwWjxuBDg== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen Subject: [PATCH v3 16/17] add_timer_on(): Make sure callers have TIMER_PINNED flag Date: Tue, 25 Oct 2022 15:58:49 +0200 Message-Id: <20221025135850.51044-17-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668645328186052?= X-GMAIL-MSGID: =?utf-8?q?1747668645328186052?= The hierachical timer pull model at expiry time is now in place. Timers which should expire on a dedicated CPU needs the TIMER_PINNED flag. Otherwise they will be queued on the dedicated CPU but in global timer base and those timers could also expire on other CPUs. Only timers with TIMER_PINNED flag will end up in local timer base Therefore add the missing TIMER_PINNED flag for those who use add_timer_on() without the flag. Signed-off-by: Anna-Maria Behnsen --- arch/x86/kernel/tsc_sync.c | 3 ++- kernel/time/clocksource.c | 2 +- kernel/workqueue.c | 7 +++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/tsc_sync.c b/arch/x86/kernel/tsc_sync.c index 9452dc9664b5..eab827288e0f 100644 --- a/arch/x86/kernel/tsc_sync.c +++ b/arch/x86/kernel/tsc_sync.c @@ -110,7 +110,8 @@ static int __init start_sync_check_timer(void) if (!cpu_feature_enabled(X86_FEATURE_TSC_ADJUST) || tsc_clocksource_reliable) return 0; - timer_setup(&tsc_sync_check_timer, tsc_sync_check_timer_fn, 0); + timer_setup(&tsc_sync_check_timer, tsc_sync_check_timer_fn, + TIMER_PINNED); tsc_sync_check_timer.expires = jiffies + SYNC_CHECK_INTERVAL; add_timer(&tsc_sync_check_timer); diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c index 8058bec87ace..f8c310e62758 100644 --- a/kernel/time/clocksource.c +++ b/kernel/time/clocksource.c @@ -523,7 +523,7 @@ static inline void clocksource_start_watchdog(void) { if (watchdog_running || !watchdog || list_empty(&watchdog_list)) return; - timer_setup(&watchdog_timer, clocksource_watchdog, 0); + timer_setup(&watchdog_timer, clocksource_watchdog, TIMER_PINNED); watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask)); watchdog_running = 1; diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 7cd5f5e7e0a1..a0f7bf7be6f2 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -1670,10 +1670,13 @@ static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, dwork->cpu = cpu; timer->expires = jiffies + delay; - if (unlikely(cpu != WORK_CPU_UNBOUND)) + if (unlikely(cpu != WORK_CPU_UNBOUND)) { + timer->flags |= TIMER_PINNED; add_timer_on(timer, cpu); - else + } else { + timer->flags &= ~TIMER_PINNED; add_timer(timer); + } } /** From patchwork Tue Oct 25 13:58:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 10807 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1025888wru; Tue, 25 Oct 2022 07:04:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7UGAtlH2hPnbM7LUFL2/JToEWbjTe0bvtk9MRhs9ysG2dUJcGaFq2Y4QX0zig6TFW3jfLE X-Received: by 2002:a17:906:4fca:b0:78d:b042:eeca with SMTP id i10-20020a1709064fca00b0078db042eecamr31520484ejw.685.1666706684000; Tue, 25 Oct 2022 07:04:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666706683; cv=none; d=google.com; s=arc-20160816; b=lwdkNayjQytKWldCrz8yBSqPQ1h3UBsKFm3XVshV3pVZcnq8ra37pBqb8p1EPlt90E INKoMvkIPfyvJJGVnnUHO5AFQZg9sCtkEXFtP1bIwucmzS78ddjJgyzJi12CFcsoTmDo SXteEKyq8pCi6TTdsOQVCPW5j08VQeO1D37XJUVLNJG/mQ4JdmhqrB6qOg96OKctG12e HZqV572fm8ChvUFg3Fh2XFBMiYVNTT75V1+1uPGYHQmQB9NNZtTqpDdyhNY9RSM+O/UH ISRqX7W9qYOVTYVsawbQbIuYFgELtM3/eG/+C+p/2k0IZAjR/XKqD5My5V0fRx0Q6FW9 D7lw== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=oP0fWluRaqq+tZG2dJILM23sro1m5U/XHwLQFeRCl3o=; b=bFzOUu9ELV0k0S3gvqrLwFoBHghoaLNNqLqvVzE6qTzRMzAsEuJjay3faqvGGinpeO /k/C04GqM6KI+zdFdGdB30WiZazlHaBiklTXo/k8FI1SzNerrZONqDF/NrsBbFT4hDNu pLFdjOQVFcokQPJ5WAfFY0dR4fkw7pvnn3+sz5ec8WAyynWR0NjOp/60TM3Dl6X31AwI Xy1hGOeBjYKRsQmP429B0wgWLuF86sDRcNY7bNw1hzlnuIhtSabG8A3AbWsn2V+85Ugk wbzaw5nLwZJW8sy/2GjobRb9dRBVd8LU7c9q9JzSwSlvd0win6syn+65Npctq3YLiEa4 uSNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=0Eg6Zbxm; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id js5-20020a17090797c500b0078d27c1c8f4si3411671ejc.696.2022.10.25.07.04.15; Tue, 25 Oct 2022 07:04:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=0Eg6Zbxm; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232721AbiJYOA4 (ORCPT + 99 others); Tue, 25 Oct 2022 10:00:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232161AbiJYN7h (ORCPT ); Tue, 25 Oct 2022 09:59:37 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6B5417AAB; Tue, 25 Oct 2022 06:59:31 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666706370; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oP0fWluRaqq+tZG2dJILM23sro1m5U/XHwLQFeRCl3o=; b=0Eg6ZbxmuMXbfIgn0sAXp3ULCCXFyqQQtHzl2xH5jlgUOd8RjrvMvZPngiSqQ/uBF1PlvC 9C0tbJW+sVdS+DAjMs9HDPuEHChU1u8mwIhVatnjiFurN8cB6b1ONTjDgf6PYEJ11KhDWm 0cPWMzqqFpUPBjMGMZW7dh5kVzEHrBNTua/xN61Djdnunb72Q5WJlBah36fwTd6QL5OrTZ uJlJWIj5makdOkBQ9ydWs+dfO5xprJa7l2oNflXamBNY66R8DL+fC5gAcKXpnjwgTiSLA+ jsfaS9bMQsglAe/04br6zM+/zkNYHqSoF7gKXy9x0hB1rgx+baiz5OH82C1jMg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666706370; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oP0fWluRaqq+tZG2dJILM23sro1m5U/XHwLQFeRCl3o=; b=8lpLCKUTS76ShpC/dXVU5vgSp553OSmhz+hNadGKfYlzIIqj55OD6ImpPLdzbexTWiCEpO 7i4ia9DdR8hIqNDQ== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Eric Dumazet , Thomas Gleixner , "Rafael J. Wysocki" , linux-pm@vger.kernel.org, Arjan van de Ven , "Paul E. McKenney" , Frederic Weisbecker , Rik van Riel , Anna-Maria Behnsen , Richard Cochran Subject: [PATCH v3 17/17] timer: Always queue timers on the local CPU Date: Tue, 25 Oct 2022 15:58:50 +0200 Message-Id: <20221025135850.51044-18-anna-maria@linutronix.de> In-Reply-To: <20221025135850.51044-1-anna-maria@linutronix.de> References: <20221025135850.51044-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747668628155509194?= X-GMAIL-MSGID: =?utf-8?q?1747668628155509194?= The timer pull model is in place so we can remove the heuristics which try to guess the best target CPU at enqueue/modification time. All non pinned timers are queued on the local CPU in the seperate storage and eventually pulled at expiry time to a remote CPU. When a timer is added via add_timer_on(), TIMER_PINNED flag is required to ensure it expires on the specified CPU. Otherwise it will be enqueued in the global timer base which could be expired by a remote CPU. WARN_ONCE() is added to prevent misuse. Originally-by: Richard Cochran (linutronix GmbH) Signed-off-by: Anna-Maria Behnsen --- kernel/time/timer.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 67f9cc077ff7..f05e547e0c6c 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -947,17 +947,6 @@ static inline struct timer_base *get_timer_base(u32 tflags) return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK); } -static inline struct timer_base * -get_target_base(struct timer_base *base, unsigned tflags) -{ -#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON) - if (static_branch_likely(&timers_migration_enabled) && - !(tflags & TIMER_PINNED)) - return get_timer_cpu_base(tflags, get_nohz_timer_target()); -#endif - return get_timer_this_cpu_base(tflags); -} - static inline void forward_timer_base(struct timer_base *base) { unsigned long jnow = READ_ONCE(jiffies); @@ -1093,7 +1082,7 @@ __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int option if (!ret && (options & MOD_TIMER_PENDING_ONLY)) goto out_unlock; - new_base = get_target_base(base, timer->flags); + new_base = get_timer_this_cpu_base(timer->flags); if (base != new_base) { /* @@ -1228,6 +1217,10 @@ void add_timer_on(struct timer_list *timer, int cpu) BUG_ON(timer_pending(timer) || !timer->function); + WARN_ONCE(!(timer->flags & TIMER_PINNED), "TIMER_PINNED flag for " + "add_timer_on() is missing: timer=%p function=%ps", + timer, timer->function); + new_base = get_timer_cpu_base(timer->flags, cpu); /*