Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp2548073rwo; Sun, 23 Jul 2023 18:39:21 -0700 (PDT) X-Google-Smtp-Source: APBJJlEA3JIJO24SSuNp1bjbZ6SP4dofdc+N96jp7oEkeSTjQlLRVwx8a3D69iSXV3cu7P4PGeDs X-Received: by 2002:adf:f385:0:b0:314:3358:d57f with SMTP id m5-20020adff385000000b003143358d57fmr7124180wro.56.1690162760776; Sun, 23 Jul 2023 18:39:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690162760; cv=none; d=google.com; s=arc-20160816; b=PHlsJZzXYIza3bYpLMgNxxrDuMyvHmc0uHfWb6bLaVoaofoSmGk9bM/HftKU4R37wU L1Cq2zwhuBbdGFyxoPJH+gdgcldOdIk6qVHA5eq3RTbnWdlyaeq2xEhEkLXBTJIU/I+I dyUSXqzxwjVV3W18urZ5goNyzHYk68P/RT0MHkIBUcp/fPGpofbI96W/6CcKispw/Ugj 0z5OiqgFgOyvWv4SIY/ZLgTyueanfkflqBU8EbJfRJDv3yIXDfZX4dfco2GyKad59CB/ kxrbnpepqLSOWPOJAL8Lg20mGfA9zV4IvsnU1MZcou8UBO47BXDxW7r7pMNT21Py8PCh 9wAw== 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=IOKQ9oU6jI1TNlsmey4ImPdFkwhLFMDcx76Ycai/q/w=; fh=Zs/DyC5EKcMK5z7LBSQe3ewh3rx+LhhmTXrpBh3CdFA=; b=sByqpDO/ztyubl+61yNGZYtFiq1i3lXgJvtvHz2mncQMQBw6+TDnzb9RmOUrjCo+u2 kRM/lqZXF0G/kDzrv0B9JoB/sKkQ2D5Jqde2R6nnpSQs2avMFu18wgfhEm/oB2qGUgL5 MUH2jWOxLINJ81OaS9XqBXfqRwhmazH6cIXVHjH7778INRCNdDsgUvdTFnoXwoIWhUjP YuNf3Oxk0BG0A127ABRvhPlbx9F7ZMBk4Q26TS56oaIgZrWg9smMDABfRdnJvJDw4cdp WYUbcG8V3sfqru3llFLy9xORd7cNY5SVKCCRfkVURa4WFGUhZMhsz6se70KO0XhGA1Ys V1rQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gJ9aF8uC; 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 h6-20020aa7de06000000b0051e19bf1f79si5728231edv.481.2023.07.23.18.38.57; Sun, 23 Jul 2023 18:39: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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gJ9aF8uC; 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 S230039AbjGXBRV (ORCPT + 99 others); Sun, 23 Jul 2023 21:17:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229926AbjGXBRN (ORCPT ); Sun, 23 Jul 2023 21:17:13 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4C9B1728; Sun, 23 Jul 2023 18:16:42 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 520AE60F05; Mon, 24 Jul 2023 01:15:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 534C4C433C8; Mon, 24 Jul 2023 01:15:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690161351; bh=qhh8Lf1XCfAF8rtfnerjBiHBNF3y9GF83zIN60JVDwI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gJ9aF8uCk8b5z+BbkS22Ft5ZPBXvqz1JRkAHXtvj6g/jxXl2sKEbO1eSrtGpzBydk Hzh8+AQ4FZtxOxxOX+Mp53HDvM/80e4rOHIr4oUUG1YFFQXKlWl3aj9i2ejx1+IIlu qEAaGDQ/aCp39ffHgquH+BGUnPyCqQSTyVRjb3WpuU7sAovVaybJykvlB7q5GVYHBK ptaerpgJh+t7J77kebusx/B3SWrfGI7AbIUNKXm+HLYG+QWrR9DBXubfwOsk45oDVA lY3vfzAIrnHyWSNT8/W1/ds7OFLQriEk3Q81A0N889Q7qFmI0h+mNJP3l8JuEGnztP suq2UoBnFeA2g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Geert Uytterhoeven , Peter Zijlstra , Arnd Bergmann , Tony Lindgren , Ulf Hansson , Sasha Levin Subject: [PATCH AUTOSEL 6.4 25/58] iopoll: Call cpu_relax() in busy loops Date: Sun, 23 Jul 2023 21:12:53 -0400 Message-Id: <20230724011338.2298062-25-sashal@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230724011338.2298062-1-sashal@kernel.org> References: <20230724011338.2298062-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.4.5 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 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 From: Geert Uytterhoeven [ Upstream commit b407460ee99033503993ac7437d593451fcdfe44 ] 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 Link: https://lore.kernel.org/r/45c87bec3397fdd704376807f0eec5cc71be440f.1685692810.git.geert+renesas@glider.be Signed-off-by: Sasha Levin --- include/linux/iopoll.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/iopoll.h b/include/linux/iopoll.h index 2c8860e406bd8..0417360a6db9b 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.39.2