Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp8752006rwr; Thu, 11 May 2023 05:56:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ480lnVemNujB7zCY5gnL/eqUmAWwExuJYS9VW6XLnOaEu3jH1OPvH6DdcYtC1oFoACW5Fj X-Received: by 2002:a17:902:bb8c:b0:1a1:ee8c:eef5 with SMTP id m12-20020a170902bb8c00b001a1ee8ceef5mr18304402pls.7.1683809798763; Thu, 11 May 2023 05:56:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683809798; cv=none; d=google.com; s=arc-20160816; b=itVaiuUsIoDwEbmd69r3+YAhgg4WGUN39ARDAntdOsS6wQP2+YjELxAxDgebVHOAux 0VDQZlcnoSdTDiFP9Jasvhz94MbsiTD/UV0on8ZrwrfFgqVstr68JJLIK+P40h1YYU/E hj9pu0JfuvTwZRZA8EXtSoaLhWXsojgewIVJJttQkRA9kzu2w3t/dj2smmfze9L3mcbC 3n1geJC0Iokdow2FMRciS8eNJ85dKE68tYHaMRKIE8ayQIt6YmWcX/oyME2Zrx3pxI5a /R/jKPkc8DGlbAxzy/PAg6zwVApPqhRnSrDE1SmULDxVCFVZRsW8+AB8ni1pTO3+CIEz umtw== 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; bh=da+brcNcnOavhrdxQtDV+iNXhk1z3/KNxGv36akl5PU=; b=YsjcXPnBj/SQLbW9rsNYjHN2HbCLRFGR27i28u2xSh58DFvPaBkUYIlDFEmPRibZfn 2gllKD4N8BgWlSoRpPVTAKntEK/ojrB9bcSKIIR1Uzig9HCpkVaoBePnIsjVAiZgqaQQ 2DgO/GYGzJ29BtjhAhQMjbmbWrh6Txfmf1+8vL4MDn61ddLTGIt+AabbMi74bGen4/a7 4RfLcel0U6KKy9arCV8Uw4BzDSZKLWl2cCol70yYvkQZREVkDILPdjqIOCSjh1CEI4FQ GOfF2NMCqQ8Fn2NTdFWvjjAmm4nVaw+GGtLClllJ6J5V+dvOntOWcPCxgQd8zZXQoX8E Z0sw== 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 kk9-20020a17090b4a0900b00250365255b9si8198351pjb.116.2023.05.11.05.56.26; Thu, 11 May 2023 05:56:38 -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 S237719AbjEKMoj convert rfc822-to-8bit (ORCPT + 99 others); Thu, 11 May 2023 08:44:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237587AbjEKMod (ORCPT ); Thu, 11 May 2023 08:44:33 -0400 Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B60B4C3F; Thu, 11 May 2023 05:44:28 -0700 (PDT) Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-ba68b965b24so1455223276.2; Thu, 11 May 2023 05:44:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683809067; x=1686401067; 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=g0nWnWXv1mK+kIB0lnxvJVZQ7RoySZ21qc81l9PKn/8=; b=ApUx6NVuD/z8euWrEo2eCnudjEVIQBZCcdGJHhecBpSGO17n9k891mpTwSS/iw71eM NXRfIOiDr88L8giMrbo17Ysmq1rtKX7B4O82uy2eYK7ym45HhwBTa/SzZj48Q1obGD4u bTrvKSexAYqx7EL6kKMJt9pEHsuUdQj36OJAmWMkuXnVh14kXkeqlMSdgLht1QH7Im6j 8UVDsBIUnHsiFW+L+GTGOyGfqAwztpSilf9z5L6fDVTZzJGPoVC5cMVOaDlnxtOhQaCx N+GshXDBpvNa64WT35Zh6jLJ6BpoqH1iYS02DTu2ypJpnhbTAox5Z5ujhJG30zQtYG0c uN9w== X-Gm-Message-State: AC+VfDzNErexG8kjQO13EJIKx0e2viuo2DM0wc5vsK0zmRmrXgsfLC61 aEx65CMI+R+H245NKAirrhggsmNYALzesw== X-Received: by 2002:a25:d6d5:0:b0:b95:99be:b8c with SMTP id n204-20020a25d6d5000000b00b9599be0b8cmr17072601ybg.64.1683809066983; Thu, 11 May 2023 05:44:26 -0700 (PDT) Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com. [209.85.128.182]) by smtp.gmail.com with ESMTPSA id 191-20020a2513c8000000b00b8f13ff2a8esm4376507ybt.61.2023.05.11.05.44.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 11 May 2023 05:44:26 -0700 (PDT) Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-55a26b46003so129787357b3.1; Thu, 11 May 2023 05:44:26 -0700 (PDT) X-Received: by 2002:a81:4f16:0:b0:54f:ba86:9df with SMTP id d22-20020a814f16000000b0054fba8609dfmr21614696ywb.28.1683809066117; Thu, 11 May 2023 05:44:26 -0700 (PDT) MIME-Version: 1.0 References: <8db63020d18fc22e137e4a8f0aa15e6b9949a6f6.1683722688.git.geert+renesas@glider.be> In-Reply-To: From: Geert Uytterhoeven Date: Thu, 11 May 2023 14:44:14 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 2/2] iopoll: Do not use timekeeping in read_poll_timeout_atomic() To: Ulf Hansson 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: 8BIT X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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 Hi Ulf, On Thu, May 11, 2023 at 12:27 PM 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 ktime_get+0x28/0x78 > > > > Calling ktime_get_mono_fast_ns() instead of ktime_get() would get > > rid of that warning. However, that would break timeout handling, > > as (at least on systems with an ARM architectured timer), the time > > 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 clock > > 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). > 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 µs, and 1 µs is already a few orders of magnitude larger than the cycle time of a contemporary CPU. 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. > > 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? Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds