Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7296552rwr; Wed, 10 May 2023 06:36:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Wkg6GKtzDN+jJC5PV+he11dMzxjtBR345DBZCBLD5oOC3HpkbZWAq4KoMgqaSiTyQjCR2 X-Received: by 2002:a17:902:d4c9:b0:1ac:731f:8f52 with SMTP id o9-20020a170902d4c900b001ac731f8f52mr13233077plg.47.1683725772579; Wed, 10 May 2023 06:36:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683725772; cv=none; d=google.com; s=arc-20160816; b=DwBWm/dRMewuXoyaJBVITM8Q49O7XIdJ6/nGbdGqv8NI9ZGqirU380yW+xXR0bxm6W yUIQ0EAN8Q7kH6E24Zr706jDTZ71V29zRsKdZm1svXImCdNPRVe2TYRb59pywVKa8/c/ d0Ybkxy+gsVXQt14bgip1oS/113j7jGlM3pDF9yxYZ1I1MikKCAjLBpZ6wRbiwYwfII7 0NdVgWOT5pcjlmcFxMTVu6uW9otkR+91Bb0oYqbzZzLyYgLF6VFf+U+izHe4i0r4OJKo hpGsBeidHVPS6J2+6lmB15M7TUikNYqE5btDqz1FL2/j3/PhXdepPHxneXvXVETwgGld B1Fw== 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; bh=Tp0MDIluaMWXnQRKuWvD5mz6XjgVberXqy75EQcHSFU=; b=F8D4USmvqf87S4jElxBYXXVrcULZzVpW7fxyQRvNMub6zaJIaKEMpf95itDbPtuS3C 61ZLHVkojXRwwV9hRPLQkZ6tWSsVzLQWIyqQdTTnnR/mTniVeNld8JAsGefC0gBxF8Oq 85VrmETn8/BOpHAF09LXrrV4PpWjOLlkB78SOFJSpbhQ9m0h65Ir2vWTF0TJvqEqb0Dm 8f0PqbB13KA19acKZkp3OYqwA4c70V1sG9mj+hdNysKzrlokLPyuMtsvYDBwF90fsC+E AbZeEdeChLTb23i6EVT2L4SDG0rKgDpXFhMxV2bBkyuw4kGDk6px66tIRltiS3rsylo6 Ifow== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ay8-20020a1709028b8800b001ac482e25a0si3909956plb.374.2023.05.10.06.35.57; Wed, 10 May 2023 06:36:12 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236778AbjEJNXm (ORCPT + 99 others); Wed, 10 May 2023 09:23:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236644AbjEJNXk (ORCPT ); Wed, 10 May 2023 09:23:40 -0400 Received: from albert.telenet-ops.be (albert.telenet-ops.be [IPv6:2a02:1800:110:4::f00:1a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FFED5BAC for ; Wed, 10 May 2023 06:23:35 -0700 (PDT) Received: from ramsan.of.borg ([IPv6:2a02:1810:ac12:ed30:cc75:d8ef:4074:8af9]) by albert.telenet-ops.be with bizsmtp id v1PG2900C3l7qvk061PGDS; Wed, 10 May 2023 15:23:32 +0200 Received: from rox.of.borg ([192.168.97.57]) by ramsan.of.borg with esmtp (Exim 4.95) (envelope-from ) id 1pwjme-001nDk-DC; Wed, 10 May 2023 15:23:16 +0200 Received: from geert by rox.of.borg with local (Exim 4.95) (envelope-from ) id 1pwjmm-00F6qK-40; Wed, 10 May 2023 15:23:16 +0200 From: Geert Uytterhoeven To: Stephen Boyd , Tomasz Figa , Sylwester Nawrocki , Will Deacon , Arnd Bergmann , Wolfram Sang , Dejin Zheng , Kai-Heng Feng , Nicholas Piggin , Heiko Carstens , Peter Zijlstra , Russell King , John Stultz , Thomas Gleixner , Tony Lindgren , Krzysztof Kozlowski , Tero Kristo , Ulf Hansson , "Rafael J . Wysocki" , Vincent Guittot Cc: linux-arm-kernel@lists.infradead.org, linux-renesas-soc@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH v2 1/2] iopoll: Call cpu_relax() in busy loops Date: Wed, 10 May 2023 15:23:13 +0200 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE, 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 It is considered good practice to call cpu_relax() in busy loops, see Documentation/process/volatile-considered-harmful.rst. This can not only lower CPU power consumption or yield to a hyperthreaded twin processor, but also allows an architecture to mitigate hardware issues (e.g. ARM Erratum 754327 for Cortex-A9 prior to r2p0) in the architecture-specific cpu_relax() implementation. In addition, cpu_relax() is also a compiler barrier. It is not immediately obvious that the @op argument "function" will result in an actual function call (e.g. in case of inlining). Where a function call is a C sequence point, this is lost on inlining. Therefore, with agressive enough optimization it might be possible for the compiler to hoist the: (val) = op(args); "load" out of the loop because it doesn't see the value changing. The addition of cpu_relax() would inhibit this. As the iopoll helpers lack calls to cpu_relax(), people are sometimes reluctant to use them, and may fall back to open-coded polling loops (including cpu_relax() calls) instead. Fix this by adding calls to cpu_relax() to the iopoll helpers: - For the non-atomic case, it is sufficient to call cpu_relax() in case of a zero sleep-between-reads value, as a call to usleep_range() is a safe barrier otherwise. However, it doesn't hurt to add the call regardless, for simplicity, and for similarity with the atomic case below. - For the atomic case, cpu_relax() must be called regardless of the sleep-between-reads value, as there is no guarantee all architecture-specific implementations of udelay() handle this. Signed-off-by: Geert Uytterhoeven Acked-by: Peter Zijlstra (Intel) Acked-by: Arnd Bergmann --- v2: - Add Acked-by, - Add compiler barrier and inlining explanation (thanks, Peter!). --- include/linux/iopoll.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/iopoll.h b/include/linux/iopoll.h index 2c8860e406bd8cae..0417360a6db9b0d6 100644 --- a/include/linux/iopoll.h +++ b/include/linux/iopoll.h @@ -53,6 +53,7 @@ } \ if (__sleep_us) \ usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ + cpu_relax(); \ } \ (cond) ? 0 : -ETIMEDOUT; \ }) @@ -95,6 +96,7 @@ } \ if (__delay_us) \ udelay(__delay_us); \ + cpu_relax(); \ } \ (cond) ? 0 : -ETIMEDOUT; \ }) -- 2.34.1