Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1927101rwd; Fri, 2 Jun 2023 02:06:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6K1cXPiRckwNBKuecKYGJUypn7H5YXVVbwG2Nr7CL72X6RGOVOySKK0MVMCRjl/2SnbwDq X-Received: by 2002:a05:6a00:2e86:b0:652:efd7:bbc3 with SMTP id fd6-20020a056a002e8600b00652efd7bbc3mr709972pfb.21.1685696812554; Fri, 02 Jun 2023 02:06:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685696812; cv=none; d=google.com; s=arc-20160816; b=ZgZE80MGi6z7di0ndzAmMzJJOOz6zqRuZDhm+r40WXmeNdhxFPCyXn8c+w1bRQlBFf 5l47WF23QFZvLrihEeXom4rUk2mHrntdZJtHZDDxkEbyKOv8WzaDXhRhNq77LQjXIm4n xTPLULoDA89onr0KjBzsEnBIlZXZINQxuM40h0UiNxJDWi1ongr/+H91MT/BL1QaomBO xJU0b5MnbOdt7ciP5lkmaqD+eMFm0odOYqEn4X2a0xKhDMRHR88cpTQOXguzFu9mu2XD lRtSUNJSKI/YgwJnmzm3BLBZ+QaTNdt6wSKSjkWdJsA4S2yZ03ZjCpvf47EsXTJSDE8q 8Mow== 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=UtDc6xTPLw+W0uCTOJiMcfXasxWfpaRoJbMiLb/Ldsw=; b=CqwBxUwIg1mknBi5l8cQonUU9OsHJtwIPAyr/qjG0xvH0Ngbh4996TnyXz0wyu9Xf8 mTPdwGHEOHLdqqqfN3r7ncrOhgqo9OsAM5zrflkol3dC++zfrG1pMIbDz/8al0Z1GTkQ WkXkOyAfeQfY4WRDBGNswBXZItUkD/367sbI+wHPcMbJSXYmE/fCct4CgxnEC1zLzlkw BqHA71+RKVsAx25B+uywGAk8Haiin3a3cdN7e3gMaVCc3RQ9W03I7JzRBPNeq65CEi5/ NY30yuwe1znOJJKwvCoDPMWuXNaFORA8seCAtopzmMbDhdLk6kfU30RS7VQ6JwKU6MzP aUiQ== 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 e29-20020aa7981d000000b0063b731e3938si463370pfl.2.2023.06.02.02.06.39; Fri, 02 Jun 2023 02:06:52 -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 S234923AbjFBIwp (ORCPT + 99 others); Fri, 2 Jun 2023 04:52:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234919AbjFBIwA (ORCPT ); Fri, 2 Jun 2023 04:52:00 -0400 Received: from laurent.telenet-ops.be (laurent.telenet-ops.be [IPv6:2a02:1800:110:4::f00:19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A5DD1735 for ; Fri, 2 Jun 2023 01:51:16 -0700 (PDT) Received: from ramsan.of.borg ([IPv6:2a02:1810:ac12:ed30:158c:2ccf:1f70:e136]) by laurent.telenet-ops.be with bizsmtp id 48qo2A00B1tRZS8018qoyS; Fri, 02 Jun 2023 10:51:14 +0200 Received: from rox.of.borg ([192.168.97.57]) by ramsan.of.borg with esmtp (Exim 4.95) (envelope-from ) id 1q50UO-00BhY9-RW; Fri, 02 Jun 2023 10:50:48 +0200 Received: from geert by rox.of.borg with local (Exim 4.95) (envelope-from ) id 1q50Uh-00APxS-V7; Fri, 02 Jun 2023 10:50:47 +0200 From: Geert Uytterhoeven To: Michael Turquette , Stephen Boyd , Yoshihiro Shimoda , Magnus Damm , Joerg Roedel , Robin Murphy Cc: 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 , linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-renesas-soc@vger.kernel.org, linux-pm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH v3 1/7] iopoll: Call cpu_relax() in busy loops Date: Fri, 2 Jun 2023 10:50:36 +0200 Message-Id: <45c87bec3397fdd704376807f0eec5cc71be440f.1685692810.git.geert+renesas@glider.be> 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=unavailable 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 Reviewed-by: Tony Lindgren Reviewed-by: Ulf Hansson --- v3: - Add Reviewed-by, 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