Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp9924631rwr; Fri, 12 May 2023 00:56:01 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ56xUsPRowwQH8tW1LRrnp3COvfSs4YPBSSpDSS3lStj7SeHkXybhl4yJ1yvqEzzvBG3hF+ X-Received: by 2002:a05:6a20:4658:b0:f2:13cc:2914 with SMTP id eb24-20020a056a20465800b000f213cc2914mr24864829pzb.16.1683878161637; Fri, 12 May 2023 00:56:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683878161; cv=none; d=google.com; s=arc-20160816; b=d9lL5eOak9yqEBanZHK9V3oU0lYw+6wDUYQRKGTrAZMTfkdtHcxnxTy5xTr3iumDs9 yweI2wSmW5QCoPqLZq5Xh7dq3dguv/841e7Ed1wYA5G/SuwiDnFgMCRs4syuLyApoKoc i1Ofp3Xi12nl5ZNNeBA9emLoq1jXrlrO0AqhSqoHYhSdv4aguEddPXRax/arrp5CZs7V /650kFraqo6qGBHBVvPjMgxyCXrk0BuREpXIUbzDoatpDAu7O7SlmqrntsKQn3T6FWXA xJXhWDPUTpKyXgdYT7jaKDaQiawYAxOCyiBKBsQ+f1vzTH84QwDuh7WjsqSLQhTKuVHE 1ILg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=bYNwvt4ygEmwWz8bQkGTZsHwBw2LQJ0FGs8uf5nofMA=; b=Htnr78DGWH7sIZN7YLlWe9gzqh5NQ4gGR5ndSyG+rt1YeI037ZSnNJD90DGJb0J8wE WuY6L1cFs0T+SSGo7pjQATQef0pCkit0EOnRpfOBaTIvf6kbP8AlHFlp9x3gl30cMBpy gPhtpwGldf0bQVU8NdxSAVVv6qO/9RSH6UgKMcQLqiG4L7n2M1wXpLbS/NZdHYhafJlC hluM6tfK4L6dW6uNkfEPGJRDIHDrozvb5WStq72f0c1Y80fkPgVcoKKQo6JcDEMV4Enx NimlwO+lmkHnz4+PPYWsUnW80wwEPpyNAKAKIaO+ZXlfFZgJP3IDsoP90rgmKzRzZiwG pwuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ictDbm8V; 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=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p6-20020a622906000000b006484c433051si5999220pfp.115.2023.05.12.00.55.49; Fri, 12 May 2023 00:56:01 -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=@linaro.org header.s=google header.b=ictDbm8V; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240048AbjELHzA (ORCPT + 99 others); Fri, 12 May 2023 03:55:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240338AbjELHyh (ORCPT ); Fri, 12 May 2023 03:54:37 -0400 Received: from mail-yw1-x1129.google.com (mail-yw1-x1129.google.com [IPv6:2607:f8b0:4864:20::1129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AF9A11619 for ; Fri, 12 May 2023 00:54:13 -0700 (PDT) Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-55a10577911so139313697b3.0 for ; Fri, 12 May 2023 00:54:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1683878051; x=1686470051; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=bYNwvt4ygEmwWz8bQkGTZsHwBw2LQJ0FGs8uf5nofMA=; b=ictDbm8V1jhuniWNoQbZT3Yyb8YlucqQ1F9qBEzbqNsoBVWt6eNz47yTDwe+3WrbAi iTvKhFbbp9a1nKzyY4ifqeyi1V9ZRU6y/pjmjN0XphCa/Oo6ceVtWdjvrF+rGYqGNCRh jmTkiqNfcpOgYrly6IkZK1YTOM9zje1fDPDnS6Yfo6ZNa4PZ+kvXpenHfoWUytNAAswQ rtYTMtLLYZ0W1xwlGdZ4bUGTxpnbYq33qLlR0HT5vroBaRlzlwX4b1b6wxB5CQRv6qPf isZxJC5u3iBtqInc1/WGSQGryWK5+ayL95sA5NPmyCoAZROCHdIzlmFwO6N9kTlTXOZZ tFhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683878051; x=1686470051; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bYNwvt4ygEmwWz8bQkGTZsHwBw2LQJ0FGs8uf5nofMA=; b=LtD5ME46OQQscaql8EX8q8utJivNLtbWCMfEhpMtlnRJ/911qWkWJk3ioxxfHf1TUC zLyZcDGECwNi7FdJEAQhfYp38DdXdghJhP8dvbCzgZx+nuokquCJXspVIrrh9/d3nfOV /35ECOEO2iB9yx3JSlGiyA8UKZL0TPFafu1+9tmxfFtAcLJAjuuvsf3hjJMETKuDMiEU Gk7y2lf99MMp11P7v67Vs5agwfnOhnXKMS6tYyRCNGCiMtpPlMjQjvVvnB3tW8be/ZyT 3K8v5MPJCNV/qoS/lMZEo8YwLzEcqm6gLJ/M02ljZzGz8kOROeBKiUiF3Gr8C1R7s5k3 CT/Q== X-Gm-Message-State: AC+VfDz8Wgpax3fQn0J9etWyILMTEqAQB3tmtlFKFIyAMq9vDLKAo6TO vS6jUH3w/4GLnXoFJQ/oTfiyDkcIT7GQ/TsjJo4sWw== X-Received: by 2002:a0d:c281:0:b0:536:cb48:9059 with SMTP id e123-20020a0dc281000000b00536cb489059mr24762932ywd.50.1683878051035; Fri, 12 May 2023 00:54:11 -0700 (PDT) MIME-Version: 1.0 References: <8db63020d18fc22e137e4a8f0aa15e6b9949a6f6.1683722688.git.geert+renesas@glider.be> In-Reply-To: From: Ulf Hansson Date: Fri, 12 May 2023 09:53:34 +0200 Message-ID: Subject: Re: [PATCH v2 2/2] iopoll: Do not use timekeeping in read_poll_timeout_atomic() To: Geert Uytterhoeven Cc: 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 , "Rafael J . Wysocki" , Vincent Guittot , linux-arm-kernel@lists.infradead.org, linux-renesas-soc@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 On Thu, 11 May 2023 at 14:44, Geert Uytterhoeven wro= te: > > Hi Ulf, > > On Thu, May 11, 2023 at 12:27=E2=80=AFPM Ulf Hansson wrote: > > On Wed, 10 May 2023 at 15:23, Geert Uytterhoeven > > wrote: > > > read_poll_timeout_atomic() uses ktime_get() to implement the timeout > > > feature, just like its non-atomic counterpart. However, there are > > > several issues with this, due to its use in atomic contexts: > > > > > > 1. When called in the s2ram path (as typically done by clock or PM > > > domain drivers), timekeeping may be suspended, triggering the > > > WARN_ON(timekeeping_suspended) in ktime_get(): > > > > > > WARNING: CPU: 0 PID: 654 at kernel/time/timekeeping.c:843 kti= me_get+0x28/0x78 > > > > > > Calling ktime_get_mono_fast_ns() instead of ktime_get() would ge= t > > > rid of that warning. However, that would break timeout handling= , > > > as (at least on systems with an ARM architectured timer), the ti= me > > > returned by ktime_get_mono_fast_ns() does not advance while > > > timekeeping is suspended. > > > Interestingly, (on the same ARM systems) the time returned by > > > ktime_get() does advance while timekeeping is suspended, despite > > > the warning. > > > > Interesting, looks like we should spend some time to further > > investigate this behaviour. > > Probably, I was a bit surprised by this behavior, too. > > > > 2. Depending on the actual clock source, and especially before a > > > high-resolution clocksource (e.g. the ARM architectured timer) > > > becomes available, time may not advance in atomic contexts, thus > > > breaking timeout handling. > > > > > > Fix this by abandoning the idea that one can rely on timekeeping to > > > implement timeout handling in all atomic contexts, and switch from a > > > global time-based to a locally-estimated timeout handling. In most > > > (all?) cases the timeout condition is exceptional and an error > > > condition, hence any additional delays due to underestimating wall cl= ock > > > time are irrelevant. > > > > I wonder if this isn't an oversimplification of the situation. Don't > > we have timeout-error-conditions that we expected to happen quite > > frequently? > > We may have some. But they definitely do not happen when time > does not advance, or they would have been mitigated long ago > (the loop would never terminate). Right, I was merely thinking of the case when ktime isn't suspended, which of course is the most common case. > > > If so, in these cases, we really don't want to continue looping longer > > than actually needed, as then we will remain in the atomic context > > longer than necessary. > > > > I guess some information about how big these additional delays could > > be, would help to understand better. Of course, it's not entirely easy > > to get that data, but did you run some tests to see how this changes? > > I did some timings (when timekeeping is available), and the differences > are rather minor. The delay and timeout parameters are in =C2=B5s, and > 1 =C2=B5s is already a few orders of magnitude larger than the cycle time > of a contemporary CPU. Ohh, I was certainly expecting a bigger spread. If it's in that ballpark we should certainly be fine. I will run some tests at my side too, as I am curious to see the behaviour. I will let you know, whatever the result is, of course. > > Under-estimates are due to the time spent in op() (depends on the > user, typical use is a hardware device register read), udelay() > (architecture/platform-dependent accuracy), and general loop overhead. Yes, you are right. My main concern is the accuracy of the udelay, but I may be totally wrong here. > > > > Signed-off-by: Geert Uytterhoeven > > > --- > > > Alternatively, one could use a mixed approach (use both > > > ktime_get_mono_fast_ns() and a local (under)estimate, and timeout on = the > > > earliest occasion), but I think that would complicate things without > > > much gain. > > > > Another option could be to provide two different polling APIs for the > > atomic use-case. > > > > One that keeps using ktime, which is more accurate and generally > > favourable - and another, along the lines of what you propose, that > > should be used by those that can't rely on timekeeping. > > At the risk of people picking the wrong one, leading to hard to > find bugs? I agree, If we don't need two APIs, it's certainly better to stick with one= . My main point is that we should not sacrifice "performance" for the most common case, just to keep things simple, right? Kind regards Uffe