Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp1497846rdh; Fri, 24 Nov 2023 14:32:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IHfzr/wQrloZyjLhSHKcaGVst+OZzRddJGSE0hY3wgWpb2rIL1/qV5q0hlsTsEgXmpyk4fy X-Received: by 2002:a17:90a:c718:b0:27d:1972:136c with SMTP id o24-20020a17090ac71800b0027d1972136cmr4820291pjt.38.1700865177031; Fri, 24 Nov 2023 14:32:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700865177; cv=none; d=google.com; s=arc-20160816; b=p86e4DdERh2xycbo11H1C1aFFTYmCnNDArE0ira/TqASfieLERLLm4yQITdVZ5Di9M ZuJSL4Oa0Qt4p8gz6nbDYh57sfxZgHk/Mjz3/LRWzAVa3K8ZpySQ/UC9SY15DHdLj8tZ MVVGE9uc0Ax914rFf7zomio3549Py5zRc03AP9QxbQKmJIMqWMHvBW8LMzorLoOsyZL9 snB6oGyRun4ZHp2opuV+OBQ4T9Nqri7y6vSFFo86LLVhgm4vi9o7l5gies3khVrQdBfQ BTNoc59pPbodSluIbPADPqmZMDZOwHZeuJJg3lHlCsL0IFPpxQDZWOBlII+Ke3XC6fQ6 IdYA== 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:from :dkim-signature; bh=rYbpj2Bk4kub7HDN8+41XPy4sMNBEzeqtZNmcv3C1Ic=; fh=IDVMcnS6D26GUyxqaB9flHqld6ARLJ0tlhgNrVL7FDw=; b=XoFpXjxtZ+c9HHbxlbOv/fpwH8Inm/CmUMwKLADOiF5scsRJJ5tLPDlI7kCg/xyMX7 LgUYkX8lV3fo9KXt3NHMktH3ed20jdFhaFwHC8y/Y/M2toOYv+J9Pm46rWzbSQXRIVRg tx4gSQJvdYeG3/qu4gcAhhKgjD2cw+PAgVUBpfLBaqxRqxoxi0HkDVx5z8GFe+e9+e9r Umgso8Xmm9INIj/iF0wdKtI83wL0ALRWQ9IWgstKJBqR4wxow4OgobkC7gjBKdX2dEeI Ch5pBbiLRoLxuOjrex/lfivWYlfqTovCdg+WkUBpg81HJXzpOxsJvlWjf3i2ClbcBhl5 CEsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HPa6VZmh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id t14-20020a17090ad50e00b002850fe7e057si4357847pju.108.2023.11.24.14.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 14:32:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HPa6VZmh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id E2173802D524; Fri, 24 Nov 2023 14:32:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231512AbjKXWck (ORCPT + 99 others); Fri, 24 Nov 2023 17:32:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230074AbjKXWch (ORCPT ); Fri, 24 Nov 2023 17:32:37 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5756810FB for ; Fri, 24 Nov 2023 14:32:44 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90621C433C7; Fri, 24 Nov 2023 22:32:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700865164; bh=CXS0kf7QfS6H/bXiY0zJwq5tEcqIiy9tCvQovQrbkN4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HPa6VZmhmx1d/lcTIANzys+lP9tpG585RQPkJGTDl5qaYFfx4ng4+D3Ue51VjCGVl +YxcXCxqvYdyVGph2cL4+kMGm5zFSgpCHg5EBFOVd/8zwyRWjqOFBk6snNRHBTH1wR OFJBStdXapHsWXqoti/A4nXAHrp8NESBWDq5BJBGYQ1eOE1jlVlHkg3NaSap/6tNoS yb6GgWzrn1W2nuFIxs8TJFNr334MzPXjLxHCmzBN3R1FFILiFAcXnSztVzBPWlNXWN N/zdiw303k4a4e8NdAGfP9X1Loxo+a/jrmE/Qe/uz4Z/Bq5ZE758VybQJPlKasDCZI V7UcKca+vdGMQ== 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 1/7] x86: Fix CPUIDLE_FLAG_IRQ_ENABLE leaking timer reprogram Date: Fri, 24 Nov 2023 23:32:20 +0100 Message-ID: <20231124223226.24249-2-frederic@kernel.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231124223226.24249-1-frederic@kernel.org> References: <20231124223226.24249-1-frederic@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 24 Nov 2023 14:32:56 -0800 (PST) From: Peter Zijlstra intel_idle_irq() re-enables IRQs very early. As a result, an interrupt may fire before mwait() is eventually called. If such an interrupt queues a timer, it may go unnoticed until mwait returns and the idle loop handles the tick re-evaluation. And monitoring TIF_NEED_RESCHED doesn't help because a local timer enqueue doesn't set that flag. The issue is mitigated by the fact that this idle handler is only invoked for shallow C-states when, presumably, the next tick is supposed to be close enough. There may still be rare cases though when the next tick is far away and the selected C-state is shallow, resulting in a timer getting ignored for a while. Fix this with using sti_mwait() whose IRQ-reenablement only triggers upon calling mwait(), dealing with the race while keeping the interrupt latency within acceptable bounds. Fixes: c227233ad64c (intel_idle: enable interrupts before C1 on Xeons) Signed-off-by: Peter Zijlstra Acked-by: Rafael J. Wysocki Signed-off-by: Frederic Weisbecker --- arch/x86/include/asm/mwait.h | 11 +++++++++-- drivers/idle/intel_idle.c | 19 +++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index 778df05f8539..bae83810505b 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -115,8 +115,15 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo } __monitor((void *)¤t_thread_info()->flags, 0, 0); - if (!need_resched()) - __mwait(eax, ecx); + + if (!need_resched()) { + if (ecx & 1) { + __mwait(eax, ecx); + } else { + __sti_mwait(eax, ecx); + raw_local_irq_disable(); + } + } } current_clr_polling(); } diff --git a/drivers/idle/intel_idle.c b/drivers/idle/intel_idle.c index dcda0afecfc5..3e01a6b23e75 100644 --- a/drivers/idle/intel_idle.c +++ b/drivers/idle/intel_idle.c @@ -131,11 +131,12 @@ static unsigned int mwait_substates __initdata; #define MWAIT2flg(eax) ((eax & 0xFF) << 24) static __always_inline int __intel_idle(struct cpuidle_device *dev, - struct cpuidle_driver *drv, int index) + struct cpuidle_driver *drv, + int index, bool irqoff) { struct cpuidle_state *state = &drv->states[index]; unsigned long eax = flg2MWAIT(state->flags); - unsigned long ecx = 1; /* break on interrupt flag */ + unsigned long ecx = 1*irqoff; /* break on interrupt flag */ mwait_idle_with_hints(eax, ecx); @@ -159,19 +160,13 @@ static __always_inline int __intel_idle(struct cpuidle_device *dev, static __cpuidle int intel_idle(struct cpuidle_device *dev, struct cpuidle_driver *drv, int index) { - return __intel_idle(dev, drv, index); + return __intel_idle(dev, drv, index, true); } static __cpuidle int intel_idle_irq(struct cpuidle_device *dev, struct cpuidle_driver *drv, int index) { - int ret; - - raw_local_irq_enable(); - ret = __intel_idle(dev, drv, index); - raw_local_irq_disable(); - - return ret; + return __intel_idle(dev, drv, index, false); } static __cpuidle int intel_idle_ibrs(struct cpuidle_device *dev, @@ -184,7 +179,7 @@ static __cpuidle int intel_idle_ibrs(struct cpuidle_device *dev, if (smt_active) __update_spec_ctrl(0); - ret = __intel_idle(dev, drv, index); + ret = __intel_idle(dev, drv, index, true); if (smt_active) __update_spec_ctrl(spec_ctrl); @@ -196,7 +191,7 @@ static __cpuidle int intel_idle_xstate(struct cpuidle_device *dev, struct cpuidle_driver *drv, int index) { fpu_idle_fpregs(); - return __intel_idle(dev, drv, index); + return __intel_idle(dev, drv, index, true); } /** -- 2.42.1