Received: by 2002:a05:7412:b101:b0:e2:908c:2ebd with SMTP id az1csp2661790rdb; Wed, 15 Nov 2023 07:14:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IHGFbOuPFaZWkjXxM0TwXxSxdCljNJXcBkgsX/sW1+/mA8ys8PCwQpZE7WtE0LtxcEfxbb4 X-Received: by 2002:a17:90b:4c04:b0:27d:76e2:ab18 with SMTP id na4-20020a17090b4c0400b0027d76e2ab18mr11640861pjb.9.1700061263148; Wed, 15 Nov 2023 07:14:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700061263; cv=none; d=google.com; s=arc-20160816; b=rou00/kb0+X6HWv7PCIaFvel2pZVpH3VJ8tcbiLVbp7jIVdyaIB9e8CCmQbxm93rHB O4mkI6CEd6Hd1A9VjYeVid92j39sDf1H3br+OrNtfKJ1emOqkZmRHYhm/pZHOWjOKkid 8upwJBQSBNVrfO9jEmdCge24rW7pJWvPDDy35LiOQKh1GItKycPYxCcE65ACfnujzGaf 4tIJcgIVrAUzKB8u7QeQR1OSawOGP9Oc7GbSzcJMTPQEV9t6zTEawF37XKm6g0n3DTaR LD3TdAe9GWtylXtU3SRNRxBpa5kF0reQeh7m0Ps7i0NlKLcCeAIFoZb6EgJxFoIbnYqH cKcg== 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=ciHALJuqd/QArjtSbPzwwJtSpK/poIF7pkyAnlkdyzM=; fh=Ivm/f9oPVddvYLeCK7X7FPY4HbbyM471g0wwB/hncPQ=; b=Xfm4M/FR0RKamMAaXAo/maaBX3v9GrecyPdCQ2P5IFbJZnFXMLDoE4JHPTVSeq4JXg ZTDcu6NINxhXFZjMm+Sif5V0tO1p9bYyM+CEUYwSivKTjHbk0xNRFr/OJWXyE9dXreW3 1r4v0IJd7/agB/n63lu/gS5bCnyzBGngeOj6uC6493E01xCW+6d5Zj+0F2CE9wPjqKtz QYLfmrAvHdIR2VEjFYf+Ji27KHS+rc0YXbVydM+M/IK6e/UA5DJOJANT3cFmZem0A5IP BV+dTgrC6rXzKqtFybYLk+EP9BH9XkKqyqTt82Z0pUmtXeddqIAFyXDUmQduWhg1Modw TObg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UqzQv9sF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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. [23.128.96.37]) by mx.google.com with ESMTPS id gt24-20020a17090af2d800b0027d3f3e6c8csi15021855pjb.54.2023.11.15.07.14.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 07:14:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UqzQv9sF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 4ACC481E6489; Wed, 15 Nov 2023 07:14:20 -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 S1344451AbjKOPOO (ORCPT + 99 others); Wed, 15 Nov 2023 10:14:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344441AbjKOPOH (ORCPT ); Wed, 15 Nov 2023 10:14:07 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44298187 for ; Wed, 15 Nov 2023 07:14:04 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7693BC433C8; Wed, 15 Nov 2023 15:14:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700061243; bh=KLrlZdWeoxjPSxqCeWK5YXQdueYFpYJySJ8TOt2dOEY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UqzQv9sFvb6sCefg7/xL0Sv84ZnEJPUhXfAiXi9JnJkJkJTI57wGnSbLSuYsnIZ6t NDGhVcd3iFdEcq1UJtQqfmPS89fVn8PIB9VlKLacTgjgky9PTMJBNf2YubHupmmo9k ciqp3s+vmxryhNZPVVsvgnJhxBakJ/rD7zKpDEwOqP4BkLN1yvNYAsEsHwPnvdZgOq 82786xqgaMSSTEsHqsonJ2+VUpk5mvKhuWBFR6PVF8tMDyjmayANExKWRS4cWzZbzq rv7dDsFATmh+3KMfBuUtJE8mbKbPpHbNxekhKhxNyxV1odpclLDNECXY3EVE7SlPi3 oIJdXmO2C4E7A== From: Frederic Weisbecker To: LKML Cc: Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "Rafael J . Wysocki" , Frederic Weisbecker Subject: [PATCH 2/4] x86: Fix CPUIDLE_FLAG_IRQ_ENABLE leaking timer reprogram Date: Wed, 15 Nov 2023 10:13:23 -0500 Message-ID: <20231115151325.6262-3-frederic@kernel.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231115151325.6262-1-frederic@kernel.org> References: <20231115151325.6262-1-frederic@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.2 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]); Wed, 15 Nov 2023 07:14:20 -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) Not-yet-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 341ee4f1d91e..920426d691ce 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -124,8 +124,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