Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4306818pxy; Tue, 27 Apr 2021 01:39:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCKAQqfyIQDUACD97pvqehphvi+dCyphHdNrmKHla6dQiLG8Mh3TAGEMHWuXrDLVzmvluz X-Received: by 2002:a17:907:7283:: with SMTP id dt3mr22171496ejc.47.1619512781787; Tue, 27 Apr 2021 01:39:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619512781; cv=none; d=google.com; s=arc-20160816; b=H+I3pTP97th8uVGcN7SmU1Y72nQMQWfpconY0dr7kWgia3Gwkxl9ixhWlRVFxNirz8 FfeLfhFC13+XSIpdmJkfI2xeetfSoSTRGDqEVMifiNB1TsfyshRLSLd6hc1eut5aOBlt 0Dh4v/icQW9TViDH9W6psotFpPVinvolVnXBmy3+DgeqTNgI+L/r2MGxGZzndrKc61pi fEiLzYo0ZJE5YhlR/dyUwEipMQ24DgNeJehaTFZFCsCmz3l1wAoS6s36GgrYeSX06o05 +xL86d6xi3BRqL3F4A90hfGG5BT6FafZ5SlDDrwaMZdkGNBiN/kwB5eP+QGoJdnSW7TK 8tVg== 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:subject:cc:to:from:date:dkim-signature:dkim-signature :message-id; bh=XVZYJbAotayFIl1hGiY8jQKHKUn22N09L0dqKXvU0Xo=; b=mCjbQhTlB06ghxXDW+uINA74JTdSK7fVBqPs9M7yOsWp4BksjWG8tRme6qDPRsKnk3 ZcODjOxJjEXOyBydCGDBivdrBm19m3QtzwYUoRenwUf8nJ9fjxJ0Q6NGLUcrpiIO/PQD vGgLzthr2Eit5MbNmF+dZbZm38kZ7y7iiE7wHtp9ygihP7ZV/5YiZ6zLMXF5Ndb1gZaw TX+3Y3CHHLrgQnlPEDdgFqSgUyQ5PxIdxRdNM6tP18ETWa1Tx3CjN2d/GlH+S++KFwvF Eb1LrWCjFlhy0sXhOU9/5/wFLF/TDjPQg57zpB05Kd7G1TQGhd4wY+mePOcGdf1T38dz ahSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=IgQB1hL7; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id fi16si3908005ejb.693.2021.04.27.01.39.18; Tue, 27 Apr 2021 01:39:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=IgQB1hL7; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235457AbhD0IjC (ORCPT + 99 others); Tue, 27 Apr 2021 04:39:02 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:39616 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235198AbhD0Iiq (ORCPT ); Tue, 27 Apr 2021 04:38:46 -0400 Message-Id: <20210427083724.840364566@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1619512683; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: references:references; bh=XVZYJbAotayFIl1hGiY8jQKHKUn22N09L0dqKXvU0Xo=; b=IgQB1hL7taws5N3M1NauyjKQEkvcj/z5T1nE2qdO8htNzXSmORkDP8AxiHEEVYdoKskaI4 jAmPZ4ubS7Mbuswjgqj5AK+jnFMm+K4TFUDH795FD8itLnXBrebmhKCiQEu2fyEzwtFbjM xI1FBg3JPNEPn+slLXYvbOgxys/KzH10OMOYvkmOWR6c+03fTT0Spk6LrsfIYHmakm2YI8 4Iw87Ua9v7BVkSQLcpcDc4BBjaae297Wwt88j5Mpu/Isi2hmpPST69Mt6rn0B6DX5mYo+e 3WHx13Ef79iLYs/mJjk0FfFKUltv3Fd25b6h4aM898dcrxcPsTbwmKjucHzBUg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1619512683; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: references:references; bh=XVZYJbAotayFIl1hGiY8jQKHKUn22N09L0dqKXvU0Xo=; b=lbfnB3vP22t1thNBiHjKLBR9OEtvSDV9/yvBpf/2DZfgGhdyMfdP/TbSzo6cPj8g5Hfgxg d3EXg0EFjMlAsnCw== Date: Tue, 27 Apr 2021 10:25:45 +0200 From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Peter Zijlstra , Marcelo Tosatti , Frederic Weisbecker , Peter Xu , Nitesh Narayan Lal , Alex Belits , "Rafael J. Wysocki" , John Stultz Subject: [patch 8/8] hrtimer: Avoid more SMP function calls in clock_was_set() References: <20210427082537.611978720@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-transfer-encoding: 8-bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are more indicators whether the SMP function calls on clock_was_set() can be avoided: - When the remote CPU is currently handling hrtimer_interrupt(). In that case the remote CPU will update offsets and reevaluate the timer bases before reprogramming anyway, so nothing to do. By unconditionally updating the offsets the following checks are possible: - When the offset update already happened on the remote CPU then the remote update attempt will yield the same seqeuence number and no IPI is required. - After updating it can be checked whether the first expiring timer in the affected clock bases moves before the first expiring (softirq) timer of the CPU. If that's not the case then sending the IPI is not required. Signed-off-by: Thomas Gleixner --- kernel/time/hrtimer.c | 66 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 9 deletions(-) --- a/kernel/time/hrtimer.c +++ b/kernel/time/hrtimer.c @@ -880,6 +880,60 @@ static void hrtimer_reprogram(struct hrt tick_program_event(expires, 1); } +static bool update_needs_ipi(struct hrtimer_cpu_base *cpu_base, + unsigned int active) +{ + struct hrtimer_clock_base *base; + unsigned int seq; + ktime_t expires; + + /* + * If the remote CPU is currently handling an hrtimer interrupt, it + * will update and reevaluate the first expiring timer of all clock + * bases before reprogramming. Nothing to do here. + */ + if (cpu_base->in_hrtirq) + return false; + + /* + * Update the base offsets unconditionally so the following quick + * check whether the SMP function call is required works. + */ + seq = cpu_base->clock_was_set_seq; + hrtimer_update_base(cpu_base); + + /* + * If the sequence did not change over the update then the + * remote CPU already handled it. + */ + if (seq == cpu_base->clock_was_set_seq) + return false; + + /* + * Walk the affected clock bases and check whether the first expiring + * timer in a clock base is moving ahead of the first expiring timer of + * @cpu_base. If so, the IPI must be invoked because per CPU clock + * event devices cannot be remotely reprogrammed. + */ + for_each_active_base(base, cpu_base, active) { + struct timerqueue_node *next; + + next = timerqueue_getnext(&base->active); + expires = ktime_sub(next->expires, base->offset); + if (expires < cpu_base->expires_next) + return true; + + /* Extra check for softirq clock bases */ + if (base->clockid < HRTIMER_BASE_MONOTONIC_SOFT) + continue; + if (cpu_base->softirq_activated) + continue; + if (expires < cpu_base->softirq_expires_next) + return true; + } + return false; +} + /* * Clock was set. This might affect CLOCK_REALTIME, CLOCK_TAI and * CLOCK_BOOTTIME (for late sleep time injection). @@ -914,16 +968,10 @@ void clock_was_set(unsigned int bases) unsigned long flags; raw_spin_lock_irqsave(&cpu_base->lock, flags); - /* - * Only send the IPI when there are timers queued in one of - * the affected clock bases. Otherwise update the base - * remote to ensure that the next enqueue of a timer on - * such a clock base will see the correct offsets. - */ - if (cpu_base->active_bases & bases) + + if (update_needs_ipi(cpu_base, bases)) cpumask_set_cpu(cpu, mask); - else - hrtimer_update_base(cpu_base); + raw_spin_unlock_irqrestore(&cpu_base->lock, flags); }