From patchwork Fri Dec 6 13:04:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13897192 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2118C20767C; Fri, 6 Dec 2024 13:04:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490261; cv=none; b=uALo27/GlZb5CLcM/si4MikclglRRAdgCZ9ZLBDYHVdFGaj9Y2EfUNbYuLvtU0ZiElcZw2boaREU8aXANiX2ELPEq8Pg1Eb4r9rl4Dyi4kinZhJCwZxl69wspOQs5rQC0H7LyRcqHuw09WKhWwhwKMERoJBbdoXCt1P+q+VVM24= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490261; c=relaxed/simple; bh=6M3MwJVPQVJfwZCyUXnKFfzUiBOjUgHdAX5hBQtJ7Q0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KqEUD1XaenY9sjyQQlH3x2OR9SFDK4whqtxVoaR/o8gO5H22QsmEgTwhWzYqNbIOGrbPFeh/0O0LR1A0pcDsw4HuIPS+lZ2oTPPbjN0FMqKhWyplNvYGn3/6DhjfN5l/XcuEeMkVO2fq2ndXNZQZh78u9kwQwbYWf8DyWIVviSE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=q/6cjzG5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="q/6cjzG5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E5ADC4CEDE; Fri, 6 Dec 2024 13:04:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733490259; bh=6M3MwJVPQVJfwZCyUXnKFfzUiBOjUgHdAX5hBQtJ7Q0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q/6cjzG5zZIDgkBwgqZvGzdSl48pBze2jgEk6hRe4Ym9GdrA0ri1eVBRb4x5S/9zV 3LdX69qDJVqw59SRrKNeGfePaleEcdssZ+l65kNq5n+bwK/rc7+nFc3QSrLIgztFoJ ozZAf0/eX6Z6JMm6f2Xf37Lb3AlEvC2J6Ja18TRZVtMObd8ER6mLL9nHpUODhEll4V 3f+dHtR2xkFOa3yzmNE2i6wyXXc/8XSKgJJCgo14GWV3nsXHlegr73844rmdf5ec+E ZO3jSA/SxJ8VpBL3ljgpqGqDrtrPiSRc79cIn74Il/+gltSygcQn+UUjnnzCwTweQb aiWkGR+nB9DYg== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Rafael J . Wysocki" , Daniel Lezcano , linux-pm@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Peter Zijlstra , Marcelo Tosatti Subject: [PATCH 1/5] cpuidle: Remove unnecessary current_clr_polling_and_test() from haltpoll Date: Fri, 6 Dec 2024 14:04:04 +0100 Message-ID: <20241206130408.18690-2-frederic@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241206130408.18690-1-frederic@kernel.org> References: <20241206130408.18690-1-frederic@kernel.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When cpuidle drivers ->enter() callback are called, the TIF_NR_POLLING flag is cleared already and TIF_NEED_RESCHED checked by call_cpuidle(). Therefore calling current_clr_polling_and_test() is redundant here and further setting of TIF_NEED_RESCHED will result in an IPI and thus an idle loop exit. This call can be safely removed. Cc: Marcelo Tosatti Acked-by: Rafael J. Wysocki Signed-off-by: Frederic Weisbecker --- drivers/cpuidle/cpuidle-haltpoll.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/drivers/cpuidle/cpuidle-haltpoll.c b/drivers/cpuidle/cpuidle-haltpoll.c index bcd03e893a0a..07dd9f000273 100644 --- a/drivers/cpuidle/cpuidle-haltpoll.c +++ b/drivers/cpuidle/cpuidle-haltpoll.c @@ -28,9 +28,6 @@ static enum cpuhp_state haltpoll_hp_state; static __cpuidle int default_enter_idle(struct cpuidle_device *dev, struct cpuidle_driver *drv, int index) { - if (current_clr_polling_and_test()) - return index; - arch_cpu_idle(); return index; } From patchwork Fri Dec 6 13:04:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13897193 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18C82206F1F; Fri, 6 Dec 2024 13:04:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490263; cv=none; b=tldVqpcC5yudQaFXAjZLC+I9FBb24+FTxbkdrXN3acvDBnS+HO63dBERIcSnRDskoHON8o07XX36pJxCz2rSPeIS7vqt44aHMbqOZp03AUiZjJCEgk3j895l72zqliwsC3OvBC/X5eaqx7IPs/KS0b0105aHLzos8patTFXzRAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490263; c=relaxed/simple; bh=vnlbtpQU0ZM/p6oSUAtyreorxH4BApB8/i74FdQ9nU0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iuvYde0+qKHNJcYwl8eunnk8IKmCht87VnyDBZT9lRIeN8I6M2LGGgWxH5R+fHt5efrI2GwkfykpKOoqLgeULuDHAhUFeWTRSkIOUHKzVNpugLCFE8KA0c5pUeMWrvlA3TWAH62//Ndxm2rBQsG2qNGmGCiovIGYiBkJlHaUdDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=O6R3V9ZK; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="O6R3V9ZK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F33BC4CEE2; Fri, 6 Dec 2024 13:04:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733490262; bh=vnlbtpQU0ZM/p6oSUAtyreorxH4BApB8/i74FdQ9nU0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O6R3V9ZKuAK4GNNEeN5z54qY1XiCDLI7dWdmQd9l4a8L+JgvAxQxRxHxaDeLbB9j/ WZojL8el2aMp2iZbVRZC/iMlNEehQThDHTg2CpWr608lUVLlDfVd945t8PfynrGwZP ltPOOYO2/MyALttdoL1jjuVTuvHq+oprhyC0cvjrZhji8p7ZQLZZFpxqf0uGrLtFhk Wf79c6J0Sx4krmaPBvGAICVKCxFFlk+ErJHdwoyJo7Mgn4mbQQmDvLaUEdkExis6wg bYO78iUqspc4EJlmJ7kuZBoHXXV3DiXFJwzObazdYT63kOvYgCCGcSNbsiFESMrJq7 hkKaC+QA9rMfA== From: Frederic Weisbecker To: LKML Cc: Peter Zijlstra , "Rafael J . Wysocki" , Daniel Lezcano , linux-pm@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Frederic Weisbecker Subject: [PATCH 2/5] cpuidle: Introduce CPUIDLE_FLAG_MWAIT Date: Fri, 6 Dec 2024 14:04:05 +0100 Message-ID: <20241206130408.18690-3-frederic@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241206130408.18690-1-frederic@kernel.org> References: <20241206130408.18690-1-frederic@kernel.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Peter Zijlstra Provide a way to tell the cpuidle core about states monitoring TIF_NEED_RESCHED on the hardware level, monitor/mwait users being the only examples in use. This will allow cpuidle core to manage TIF_NR_POLLING on behalf of all kinds of TIF_NEED_RESCHED watching states while keeping a necessary distinction for the governors between software loops polling on TIF_NEED_RESCHED and hardware monitored writes to thread flags. [fweisbec: _ Initialize flag from acpi_processor_setup_cstates() instead of acpi_processor_setup_lpi_states(), as the latter seem to be about arm64... _ Rename CPUIDLE_FLAG_NO_IPI to CPUIDLE_FLAG_MWAIT] Not-yet-signed-off-by: Peter Zijlstra Signed-off-by: Frederic Weisbecker Signed-off-by: Peter Zijlstra (Intel) --- drivers/acpi/processor_idle.c | 3 +++ drivers/idle/intel_idle.c | 5 ++++- include/linux/cpuidle.h | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/acpi/processor_idle.c b/drivers/acpi/processor_idle.c index 698897b29de2..66cb5536d91e 100644 --- a/drivers/acpi/processor_idle.c +++ b/drivers/acpi/processor_idle.c @@ -806,6 +806,9 @@ static int acpi_processor_setup_cstates(struct acpi_processor *pr) if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) drv->safe_state_index = count; + if (cx->entry_method == ACPI_CSTATE_FFH) + state->flags |= CPUIDLE_FLAG_MWAIT; + /* * Halt-induced C1 is not good for ->enter_s2idle, because it * re-enables interrupts on exit. Moreover, C1 is generally not diff --git a/drivers/idle/intel_idle.c b/drivers/idle/intel_idle.c index ac4d8faa3886..d52723fbeb04 100644 --- a/drivers/idle/intel_idle.c +++ b/drivers/idle/intel_idle.c @@ -1787,7 +1787,8 @@ static void __init intel_idle_init_cstates_acpi(struct cpuidle_driver *drv) if (cx->type > ACPI_STATE_C1) state->target_residency *= 3; - state->flags = MWAIT2flg(cx->address); + state->flags = MWAIT2flg(cx->address) | CPUIDLE_FLAG_MWAIT; + if (cx->type > ACPI_STATE_C2) state->flags |= CPUIDLE_FLAG_TLB_FLUSHED; @@ -2072,6 +2073,8 @@ static bool __init intel_idle_verify_cstate(unsigned int mwait_hint) static void state_update_enter_method(struct cpuidle_state *state, int cstate) { + state->flags |= CPUIDLE_FLAG_MWAIT; + if (state->flags & CPUIDLE_FLAG_INIT_XSTATE) { /* * Combining with XSTATE with IBRS or IRQ_ENABLE flags diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h index a9ee4fe55dcf..b8084617aa27 100644 --- a/include/linux/cpuidle.h +++ b/include/linux/cpuidle.h @@ -85,6 +85,7 @@ struct cpuidle_state { #define CPUIDLE_FLAG_OFF BIT(4) /* disable this state by default */ #define CPUIDLE_FLAG_TLB_FLUSHED BIT(5) /* idle-state flushes TLBs */ #define CPUIDLE_FLAG_RCU_IDLE BIT(6) /* idle-state takes care of RCU */ +#define CPUIDLE_FLAG_MWAIT BIT(7) /* hardware need_resched() monitoring */ struct cpuidle_device_kobj; struct cpuidle_state_kobj; From patchwork Fri Dec 6 13:04:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13897194 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BAA2D206F1F; Fri, 6 Dec 2024 13:04:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490266; cv=none; b=HwMyLkip55GOJ2WRIKVyW3sKuGiQZ5WlK5AWHsyU9r5BVN5ygUb0bk77VG0LnyUP0cfKCWwMt+rpbHlFqOw12IZWUFh7mvw3uWil4IU0QyXHdWScINYSzFM2FBe1R58Ay+sy+VXtG9khJVkzwkAsv4TDtpJi4fu4MWbfSuxP3wc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490266; c=relaxed/simple; bh=ENzSnQiAZfR27XuxVLR8K7cUxh52qf/cfM2RpyKvVos=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KIiIC9hyxR2Pcgtmk9DyleJ54HPrs/aCslgGq4DnoPJstVRLGyFGQF147qttX1kakL7So5QGnHLUePql9vYRwqc5FLiRgWmMDDR8D5RaCOVU59coPwp+vXCLOyh4wGdDJ+9amrr99NzI9wTBAMrleO3gvJRrKAk+I11s2ON3FBI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SKQqHjFq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SKQqHjFq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 16414C4CEDE; Fri, 6 Dec 2024 13:04:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733490265; bh=ENzSnQiAZfR27XuxVLR8K7cUxh52qf/cfM2RpyKvVos=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SKQqHjFqv/DXin/IZtt3F5AzpjOOOM/Ys1uZ4Jw4x5/LmwGUjWMcDxEi6+U9cBdI8 HhfKb4sNmY56xdqPrdiNcAmW/ErrERPhvtdnRfXzjK4WbWH96biUXGIggd8Xj8YsOd QuqLpZmEa0U5PRFpQ1+FKDwrtGVNbuKenLG5w9o45fsLjQbM88dCfz5/E3wVdUVzsB fSOU7gHAI8qnqLd/yd1BPQGddVnkNBdJnKnBx/HsozbAbrY2gdbOb3/Z1fh81kDgLa i28fU1fY2jrmob+HGlCiOpUsnjy8OblIJNMzRAhRRdANbcnrnhmuYouh69jorir2PE ka8DPmqgX5OeA== From: Frederic Weisbecker To: LKML Cc: Peter Zijlstra , "Rafael J . Wysocki" , Daniel Lezcano , linux-pm@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Frederic Weisbecker Subject: [PATCH 3/5] cpuidle: Handle TIF_NR_POLLING on behalf of CPUIDLE_FLAG_MWAIT states Date: Fri, 6 Dec 2024 14:04:06 +0100 Message-ID: <20241206130408.18690-4-frederic@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241206130408.18690-1-frederic@kernel.org> References: <20241206130408.18690-1-frederic@kernel.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Peter Zijlstra The current handling of TIF_NR_POLLING is a bit of a maze: 1) TIF_NR_POLLING is set on idle entry (one atomic set) 2) Once cpuidle has selected an appropriate state and the tick is evaluated and then possibly stopped, TIF_NR_POLLING is cleared (one RmW operation) 2) The cpuidle state is then called with TIF_NR_POLLING cleared but if the state polls on (or monitors) need_resched() it sets again TIF_NR_POLLING before sleeping and clears it on wake-up. Summary: another pair of set/clear 3) Set back TIF_NR_POLLING (one atomic set) 4) goto 2) if need_resched() is not set All those costly atomic operations, fully ordered RmW for some of them, could be avoided if the cpuidle core knew in advance if the target state polls on (or monitors) need_resched(). If so, TIF_NR_POLLING could simply be set once upon entering the idle loop and cleared once after idle loop exit. Start dealing with that with handling TIF_NR_POLLING on behalf of mwait based states. [fweisbec: _ Handle broadcast properly _ Ignore mwait_idle() as it can be used by default_idle_call()] Not-yet-signed-off-by: Peter Zijlstra Signed-off-by: Frederic Weisbecker Signed-off-by: Peter Zijlstra (Intel) --- arch/x86/include/asm/mwait.h | 3 +-- drivers/cpuidle/cpuidle.c | 22 +++++++++++++++++++- include/linux/sched/idle.h | 7 ++++++- kernel/sched/idle.c | 40 +++++++++++++----------------------- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index 920426d691ce..3634d00e5c37 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -116,7 +116,7 @@ static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx) */ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) { - if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) { + if (static_cpu_has_bug(X86_BUG_MONITOR) || !need_resched()) { if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) { mb(); clflush((void *)¤t_thread_info()->flags); @@ -134,7 +134,6 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo } } } - current_clr_polling(); } /* diff --git a/drivers/cpuidle/cpuidle.c b/drivers/cpuidle/cpuidle.c index 0835da449db8..46c0a2726f67 100644 --- a/drivers/cpuidle/cpuidle.c +++ b/drivers/cpuidle/cpuidle.c @@ -217,10 +217,10 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev, int index) { int entered_state; - struct cpuidle_state *target_state = &drv->states[index]; bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP); ktime_t time_start, time_end; + bool polling; instrumentation_begin(); @@ -237,6 +237,23 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev, broadcast = false; } + polling = target_state->flags & CPUIDLE_FLAG_MWAIT; + + /* + * If the target state doesn't poll on need_resched(), this is + * the last check after which further TIF_NEED_RESCHED remote setting + * will involve an IPI. + */ + if (!polling && current_clr_polling_and_test()) { + if (broadcast) + tick_broadcast_exit(); + dev->last_residency_ns = 0; + local_irq_enable(); + instrumentation_end(); + return -EBUSY; + } + + if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED) leave_mm(); @@ -336,6 +353,9 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev, dev->states_usage[index].rejected++; } + if (!polling) + __current_set_polling(); + instrumentation_end(); return entered_state; diff --git a/include/linux/sched/idle.h b/include/linux/sched/idle.h index e670ac282333..3e3482bfb028 100644 --- a/include/linux/sched/idle.h +++ b/include/linux/sched/idle.h @@ -68,6 +68,8 @@ static __always_inline bool __must_check current_set_polling_and_test(void) static __always_inline bool __must_check current_clr_polling_and_test(void) { + bool ret; + __current_clr_polling(); /* @@ -76,7 +78,10 @@ static __always_inline bool __must_check current_clr_polling_and_test(void) */ smp_mb__after_atomic(); - return unlikely(tif_need_resched()); + ret = unlikely(tif_need_resched()); + if (ret) + __current_set_polling(); + return ret; } #else diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index 621696269584..9eece3df1080 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -114,12 +114,13 @@ void __cpuidle default_idle_call(void) stop_critical_timings(); ct_cpuidle_enter(); - arch_cpu_idle(); + arch_cpu_idle(); // XXX assumes !polling ct_cpuidle_exit(); start_critical_timings(); trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id()); cond_tick_broadcast_exit(); + __current_set_polling(); } local_irq_enable(); instrumentation_end(); @@ -128,31 +129,14 @@ void __cpuidle default_idle_call(void) static int call_cpuidle_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev) { + int ret; + if (current_clr_polling_and_test()) return -EBUSY; - return cpuidle_enter_s2idle(drv, dev); -} - -static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev, - int next_state) -{ - /* - * The idle task must be scheduled, it is pointless to go to idle, just - * update no idle residency and return. - */ - if (current_clr_polling_and_test()) { - dev->last_residency_ns = 0; - local_irq_enable(); - return -EBUSY; - } - - /* - * Enter the idle state previously returned by the governor decision. - * This function will block until an interrupt occurs and will take - * care of re-enabling the local interrupts - */ - return cpuidle_enter(drv, dev, next_state); + ret = cpuidle_enter_s2idle(drv, dev); + __current_set_polling(); + return ret; } /** @@ -213,7 +197,7 @@ static void cpuidle_idle_call(void) tick_nohz_idle_stop_tick(); next_state = cpuidle_find_deepest_state(drv, dev, max_latency_ns); - call_cpuidle(drv, dev, next_state); + cpuidle_enter(drv, dev, next_state); } else { bool stop_tick = true; @@ -227,7 +211,12 @@ static void cpuidle_idle_call(void) else tick_nohz_idle_retain_tick(); - entered_state = call_cpuidle(drv, dev, next_state); + /* + * Enter the idle state previously returned by the governor decision. + * This function will block until an interrupt occurs and will take + * care of re-enabling the local interrupts. + */ + entered_state = cpuidle_enter(drv, dev, next_state); /* * Give the governor an opportunity to reflect on the outcome */ @@ -235,7 +224,6 @@ static void cpuidle_idle_call(void) } exit_idle: - __current_set_polling(); /* * It is up to the idle functions to re-enable local interrupts From patchwork Fri Dec 6 13:04:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13897195 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 236FE20C47C; Fri, 6 Dec 2024 13:04:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490268; cv=none; b=OqLdFuUZbD9O2JUmEkVto3DbWm8mCx5N9rki83KaqiXvDCddsTLASE/AArciRbhBoLm5upqX0Dz0dY3tlyqWLqp9oMh1+3NCnHVKXHGxBb3dr7u5y7CDaUiBYLJD/Lu3tf/jT0GNP0V90iRA92DppeheMfyL8irYZXCF5tT9YqU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490268; c=relaxed/simple; bh=3JoSvgVSyWPdcDhFxJbqeFmv3Z/rfrPtZzh7qZxBCX8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZIVTXU5OfoULJWNWAT/e9Iz64iDHlwIzx4qGsD6akpVhwp8Uc3pg3INMQxU8sxw4MAVmuGazr+8OgLZCCFtKnHeG3X1omVwmsGQoftS101FTlOH2Gr+AU8MWdM4S764/YOENpNwYEGuY0FazckxRICjrltSk0VL6HVbDIZgDQbU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HaYTzm4W; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HaYTzm4W" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B5524C4CED2; Fri, 6 Dec 2024 13:04:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733490268; bh=3JoSvgVSyWPdcDhFxJbqeFmv3Z/rfrPtZzh7qZxBCX8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HaYTzm4WPezL5jQKIv/vqxlxcLlqC6eIqwMGZkUG0FOSlkx3/WTK2SMhFX1qdMzIf FRfCsoQfeKHWczjfmu2HXKzqy/KzYuf0EQ2buVIOiMQrEeShKQeucA/gkicmSyU3jE lnqyj3ybzFgvkfmT+gQxSPkHGR2UdWXxqtsV3HSR1oMSiY/Ew9KcY0U2zWX1Nh3wSn jK4PIFtwWTqA8xYaT4TYnCStsKk0d1MJ2FxhML4aUiKIGWhUTW32EJeUy9r163OJy5 GUEDg4U+m4et5EeyTYz01pIVfBiyCA3/kJPi60SPVPgkp74MdV6alW8meR4L+p3zdz G2AUXR9VFd/Ow== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , Peter Zijlstra , "Rafael J . Wysocki" , Daniel Lezcano , linux-pm@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "Rafael J. Wysocki" Subject: [PATCH 4/5] cpuidle: Remove call_cpuidle_s2idle() Date: Fri, 6 Dec 2024 14:04:07 +0100 Message-ID: <20241206130408.18690-5-frederic@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241206130408.18690-1-frederic@kernel.org> References: <20241206130408.18690-1-frederic@kernel.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This middle call is unecessary, especially now that its counterpart call_cpuidle() has been removed. Suggested-by: Rafael J. Wysocki Signed-off-by: Frederic Weisbecker --- kernel/sched/idle.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index 9eece3df1080..86b902eb24fe 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -126,19 +126,6 @@ void __cpuidle default_idle_call(void) instrumentation_end(); } -static int call_cpuidle_s2idle(struct cpuidle_driver *drv, - struct cpuidle_device *dev) -{ - int ret; - - if (current_clr_polling_and_test()) - return -EBUSY; - - ret = cpuidle_enter_s2idle(drv, dev); - __current_set_polling(); - return ret; -} - /** * cpuidle_idle_call - the main idle function * @@ -184,10 +171,12 @@ static void cpuidle_idle_call(void) u64 max_latency_ns; if (idle_should_enter_s2idle()) { - - entered_state = call_cpuidle_s2idle(drv, dev); - if (entered_state > 0) - goto exit_idle; + if (!current_clr_polling_and_test()) { + entered_state = cpuidle_enter_s2idle(drv, dev); + __current_set_polling(); + if (entered_state > 0) + goto exit_idle; + } max_latency_ns = U64_MAX; } else { From patchwork Fri Dec 6 13:04:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13897196 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F20C5207655; Fri, 6 Dec 2024 13:04:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490272; cv=none; b=UZzzq5LP2O0dpEwdk3Sh7e9ArdZ/93DMAVPkgNO3XvOnpiIfmdT5DZm3GcXMgDiBJrbsMZ0U8eGe/W7Cgy27WBtjfS1uUt0yOhqgH240XSFtYq4iRKMrRzt+PFByy2Ao2CJmMnamJa6HWUGFs8CRIJI3v+IrfwvimtVs5bmrwvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733490272; c=relaxed/simple; bh=pAiIzXceRjp6FxVnLb+1NpJxcsd0OOyzRdXDgocx5VE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q22fB8oeDS40I9e7QufRmlPePQXFNtbppjVpqezKQUtkrLqc3sWFYRgkbAoAzcdraf6XkWL5aVEam7QmBzm+1lTPGuUbwrpdFPWxPq5ncJDkRLnlNiMOjKc64KB4HdoDwIsnFAwZzc5bkao5uONlV4H2SahFMlTEVV3iOFeUMUY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=N79inSYl; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="N79inSYl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69C48C4CEDF; Fri, 6 Dec 2024 13:04:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733490270; bh=pAiIzXceRjp6FxVnLb+1NpJxcsd0OOyzRdXDgocx5VE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N79inSYl0xqIXhRAaoAt++wveZTGUUhPsc4QufIjsd9Y0vJJDA18ildfeplaCgt53 xz/jK56qcPjZJDjcnXsVG4hbhG044d9zg9UtKUDTyj9EZwYD1skC7czsdoKCHhxE7k 82BFwdKeBqyPf0THDhNPqqmdIpp1C6VkabdATuPlsIipAJ+Sr4WN8HGLd0oB8mCjGn GSnJjs1wXxhhBc6+7uHGCUSs9yIUn3fmLd7zDXsRhLQZ8VJr+te3MXeq5/geV9BxW1 4enRbW5bAah1DdxihRA1HLWg6fCCIBoSqmC0M9whGkJni/3DVMdksJd0uPysZI1B7T TTBzffSAO5Gzw== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , Peter Zijlstra , "Rafael J . Wysocki" , Daniel Lezcano , linux-pm@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen Subject: [PATCH 5/5] cpuidle: Handle TIF_NR_POLLING on behalf of software polling idle states Date: Fri, 6 Dec 2024 14:04:08 +0100 Message-ID: <20241206130408.18690-6-frederic@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241206130408.18690-1-frederic@kernel.org> References: <20241206130408.18690-1-frederic@kernel.org> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Software polling idle states set again TIF_NR_POLLING and clear it upon exit. This involves error prone duplicated code and wasted cycles performing atomic operations, sometimes RmW fully ordered. To avoid this, benefit instead from the same generic TIF_NR_POLLING handling that is currently in use for hardware monitoring states. Signed-off-by: Frederic Weisbecker --- drivers/cpuidle/cpuidle-powernv.c | 10 ---------- drivers/cpuidle/cpuidle-pseries.c | 11 ----------- drivers/cpuidle/cpuidle.c | 2 +- drivers/cpuidle/poll_state.c | 30 ++++++++++++------------------ 4 files changed, 13 insertions(+), 40 deletions(-) diff --git a/drivers/cpuidle/cpuidle-powernv.c b/drivers/cpuidle/cpuidle-powernv.c index 9ebedd972df0..1bf0d2234016 100644 --- a/drivers/cpuidle/cpuidle-powernv.c +++ b/drivers/cpuidle/cpuidle-powernv.c @@ -71,8 +71,6 @@ static int snooze_loop(struct cpuidle_device *dev, { u64 snooze_exit_time; - set_thread_flag(TIF_POLLING_NRFLAG); - local_irq_enable(); snooze_exit_time = get_tb() + get_snooze_timeout(dev, drv, index); @@ -81,21 +79,13 @@ static int snooze_loop(struct cpuidle_device *dev, HMT_very_low(); while (!need_resched()) { if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) { - /* - * Task has not woken up but we are exiting the polling - * loop anyway. Require a barrier after polling is - * cleared to order subsequent test of need_resched(). - */ - clear_thread_flag(TIF_POLLING_NRFLAG); dev->poll_time_limit = true; - smp_mb(); break; } } HMT_medium(); ppc64_runlatch_on(); - clear_thread_flag(TIF_POLLING_NRFLAG); local_irq_disable(); diff --git a/drivers/cpuidle/cpuidle-pseries.c b/drivers/cpuidle/cpuidle-pseries.c index f68c65f1d023..704bb01d9e9e 100644 --- a/drivers/cpuidle/cpuidle-pseries.c +++ b/drivers/cpuidle/cpuidle-pseries.c @@ -40,8 +40,6 @@ int snooze_loop(struct cpuidle_device *dev, struct cpuidle_driver *drv, { u64 snooze_exit_time; - set_thread_flag(TIF_POLLING_NRFLAG); - pseries_idle_prolog(); raw_local_irq_enable(); snooze_exit_time = get_tb() + snooze_timeout; @@ -51,21 +49,12 @@ int snooze_loop(struct cpuidle_device *dev, struct cpuidle_driver *drv, HMT_low(); HMT_very_low(); if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) { - /* - * Task has not woken up but we are exiting the polling - * loop anyway. Require a barrier after polling is - * cleared to order subsequent test of need_resched(). - */ dev->poll_time_limit = true; - clear_thread_flag(TIF_POLLING_NRFLAG); - smp_mb(); break; } } HMT_medium(); - clear_thread_flag(TIF_POLLING_NRFLAG); - raw_local_irq_disable(); pseries_idle_epilog(); diff --git a/drivers/cpuidle/cpuidle.c b/drivers/cpuidle/cpuidle.c index 46c0a2726f67..fecc50c2860e 100644 --- a/drivers/cpuidle/cpuidle.c +++ b/drivers/cpuidle/cpuidle.c @@ -237,7 +237,7 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev, broadcast = false; } - polling = target_state->flags & CPUIDLE_FLAG_MWAIT; + polling = target_state->flags & (CPUIDLE_FLAG_MWAIT | CPUIDLE_FLAG_POLLING); /* * If the target state doesn't poll on need_resched(), this is diff --git a/drivers/cpuidle/poll_state.c b/drivers/cpuidle/poll_state.c index 9b6d90a72601..d69936e2517e 100644 --- a/drivers/cpuidle/poll_state.c +++ b/drivers/cpuidle/poll_state.c @@ -13,35 +13,29 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev, struct cpuidle_driver *drv, int index) { - u64 time_start; - - time_start = local_clock_noinstr(); + u64 time_start = local_clock_noinstr(); + unsigned int loop_count = 0; + u64 limit; dev->poll_time_limit = false; raw_local_irq_enable(); - if (!current_set_polling_and_test()) { - unsigned int loop_count = 0; - u64 limit; - limit = cpuidle_poll_time(drv, dev); + limit = cpuidle_poll_time(drv, dev); - while (!need_resched()) { - cpu_relax(); - if (loop_count++ < POLL_IDLE_RELAX_COUNT) - continue; + while (!need_resched()) { + cpu_relax(); + if (loop_count++ < POLL_IDLE_RELAX_COUNT) + continue; - loop_count = 0; - if (local_clock_noinstr() - time_start > limit) { - dev->poll_time_limit = true; - break; - } + loop_count = 0; + if (local_clock_noinstr() - time_start > limit) { + dev->poll_time_limit = true; + break; } } raw_local_irq_disable(); - current_clr_polling(); - return index; }