Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2179894rwr; Fri, 21 Apr 2023 05:39:23 -0700 (PDT) X-Google-Smtp-Source: AKy350Y093yRR8dytUTgZTQ7/lnvhw8zhuGDYQlv6LFWdY9rQ7jaFkxznf3fmxhB0uejFRMkuvKT X-Received: by 2002:a17:903:22ce:b0:19a:a815:2877 with SMTP id y14-20020a17090322ce00b0019aa8152877mr6162922plg.6.1682080763575; Fri, 21 Apr 2023 05:39:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682080763; cv=none; d=google.com; s=arc-20160816; b=1EFvqBt5VOn2HpKuM2iIxXPvfj5rw5E7LuWjH65sltphdiBe/RDa/d8q+wzCdjC+pf XiAmgGyzlzPGbdzCsg2SLnqVdZ5u9eY5h42X3Gswh6FiqoxhltC1JuBoQl8VbehoYDFG eGjTWhyChtjsCvIMP6d8n8Oyb/LWutuCSPT+CNv6R4DyLQwmkqAQ0calkqbeMSMUEeqP cslDg51eLRzaonQZvPAfwc1uy/ZKBEMMtVIiiboHubi0s84HwrjGrQKntl0xa3ux4oV0 plE+j5URlX1/r58nXsJR+hY+GBv2djPfXKok6TVPM54917IfMRyLMRfBj7bL0iLy4Weq PtdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=Njem2EnreFvAtgn6B+edYgr8vAHE6goW0X8102xZIts=; b=i7KZP9ifx4S33s1wF5DkrFwtNrF7yHRzodlJ4ToIc9KdtI3NQyMLR7XV2es6nH+S4d Vn5mG7gL5ZYsvsQJ28qLzZDQxxFqdibZrQC7gaF0EuxbT4+KuBkhYhO9o7rGiKHSZs5T /0w7ZS78AhHDhICl7EpjQWNCq3aRhsgBVdyxev+BsXKeLloL20LiOiHCyb/W1iGGpo9Z Fyrtu5DcAR//fZPTjCoEX0j8cx+EtAyX2k0/zV7fEAgMjQ6V8c6jpThUTPA7T+RLHjf6 bm4M0o9yiu2bsOgKXaoF/Tpr3olRZ4RWBYLZIycQabsilOeygv4og1X/deq2Fyy6WLLs 6mig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=I+WKMXy1; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q2-20020a170902eb8200b001a6db0a7ff0si4523173plg.131.2023.04.21.05.39.09; Fri, 21 Apr 2023 05:39:23 -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=@kernel.org header.s=k20201202 header.b=I+WKMXy1; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231451AbjDUMg7 (ORCPT + 99 others); Fri, 21 Apr 2023 08:36:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229464AbjDUMg6 (ORCPT ); Fri, 21 Apr 2023 08:36:58 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D53F2E66 for ; Fri, 21 Apr 2023 05:36:56 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 679EF64FD2 for ; Fri, 21 Apr 2023 12:36:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74E99C433D2; Fri, 21 Apr 2023 12:36:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682080615; bh=u4jrpcuB0tGC0jTpHkQU1WpjtyJWlbwEF7D0jtYMEX0=; h=Date:From:To:Cc:Subject:From; b=I+WKMXy1X8Yiv+dsnqps2XrgcLGK9AxC368CWYKMSwljatGpNDLDXg0+LXtlP5p7Y BC/WLDcNuUofT11oMlYwkF+e+TmwtBdqtViPiwN0iIWpc2yT9LmbhLtSVtqY+ycBAM cvaifLRMSCZSZXaVO8x/lvpBP1xwE3eu1xUInH7cEIFUrhyRYDfZ0m6xLO9mbjs9xk bkOgRrgy9g3nPxyMZA6bxAic//7i/5t7QOuIvFuVwXJ0DXB4VEVF2+tPePxg3bgGH1 SLAyT+iZx9YEBv26VrA0n0UgP7+FM+T1Qlfs2HPlf8IETS7cZC3zG+XPDgnjoLMdk+ NTMBJ5dywtHhQ== Date: Fri, 21 Apr 2023 14:36:52 +0200 From: Frederic Weisbecker To: Huacai Chen , WANG Xuerui Cc: Peter Zijlstra , Thomas Gleixner , "Rafael J. Wysocki" , Anna-Maria Behnsen , LKML Subject: Loongson (and other $ARCHs?) idle VS timer enqueue Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 Hi, I'm looking at the __arch_cpu_idle() implementation in Loongarch and I'm wondering about the rollback code. I don't understand well that code but with the help from PeterZ I might have seen something, so tell me if I'm wrong: when an interrupt happens within __arch_cpu_idle(), handle_vint() rolls back the return value to the beginning of __arch_cpu_idle(), so that TIF_NEED_RESCHED is checked again. Is that correct? Because if an interrupt fires while in __arch_cpu_idle(), that IRQ might enqueue a new timer and that new timer needs to be reprogrammed from the main idle loop and just checking TIF_NEED_RESCHED doesn't tell about that information. More generally IRQs must _not_ be re-enabled between cpuidle_select() (or just tick_nohz_idle_stop_tick() if no cpuidle support) and the last halting ASM instruction. If that happens there must be a mechanism to cope with that and make sure we return to the main idle loop. If that mechanism has to go through rollback (I wish your arch allows you to find a simpler and less error prone mechanism through), then the rollback must actually fast forward to after the halting instruction so that the main idle loop re-checks the timers. But then __arch_cpu_idle() alone is not enough to be part of the fastforward section, it has to start before the raw_local_irq_enable() in arch_cpu_idle(). Another way to cope with this would be to have: #define TIF_IDLE_TIMER ... #define TIF_IDLE_EXIT (TIF_NEED_RESCHED | TIF_IDLE_TIMER) And set that from the timer enqueue in idle time and check TIF_IDLE_EXIT on idle callback. It depends how many architectures are concerned by this. All I know so far is: * mwait based mechanism should be fine if called with IRQs disabled (or sti is called right before) but then we must be sure that IRQs have never been even temporarily re-enabled between cpuidle_select() and mwait. How to detect that kind of mistake? * wfi based mechanism look fine, but again we must make sure IRQs have never been re-enabled. * sti;hlt should be fine but again... * Need to check all other archs I'm trying to find an automated way to debug this kind of issue but it's not easy... Thanks.