From patchwork Fri Jun 2 08:50:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geert Uytterhoeven X-Patchwork-Id: 102391 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp888858vqr; Fri, 2 Jun 2023 02:05:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6lV2FdnaJRBKvCVjzaxpi8R7bIZxtNN1A3nWW9nFQjEMXqx3oOVeTMkLhiGU8Bf1nFSlUN X-Received: by 2002:a17:90a:e50a:b0:250:7347:39d9 with SMTP id t10-20020a17090ae50a00b00250734739d9mr922939pjy.37.1685696720356; Fri, 02 Jun 2023 02:05:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685696720; cv=none; d=google.com; s=arc-20160816; b=ZpZfoJAV90hdcvusbNeunhbWzliDjesf/3EGx+ntSyKIeK5WAmJQyNH2MIxeL+hxno WL/t7YdZcBZR9MN4NhIU05jKCu6hJUbQo2OFm/zsEZyxDJl9Kb7T71Moy+aEzJKiCajc y2zUUsYQHpZhPjav4Z0T2jTj6bQa+tUZu9TPY+yxnurqZ+BgYU5NZABZbzqnKVPE16vo 3FONFw46CQeLd8gQK2hfe81HRYLHfcUsfMsGrWSaF/OG3FRHtGTwyoJ9kEwgLeJS88Qs A8vpIl0OedMyr1EwvloRIx39gghPeWmG+bOagN67Yca/G77lnSP3e09SPC/RTb7vvscH HCUg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bs8-20020a632808000000b0053f21272753si695406pgb.9.2023.06.02.02.05.07; Fri, 02 Jun 2023 02:05:20 -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 S235023AbjFBIwq (ORCPT + 99 others); Fri, 2 Jun 2023 04:52:46 -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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767581123738673990?= X-GMAIL-MSGID: =?utf-8?q?1767581123738673990?= 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; \ })