Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp9939350rwr; Fri, 12 May 2023 01:11:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7XsjIhEUfleOLbVeV2j2ch6x8ZnWqoT4jhy6Htc4kklZFweb3ZRfJlcZxvRer/BBGhfIPV X-Received: by 2002:a05:6a00:244d:b0:626:2ce1:263c with SMTP id d13-20020a056a00244d00b006262ce1263cmr29205006pfj.5.1683879062800; Fri, 12 May 2023 01:11:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683879062; cv=none; d=google.com; s=arc-20160816; b=tJqPJHgBPs4kKGnrv5qwZBrmkZJBXwbzn3COytmdkma7NMzkmCEHjFN/uDe2w1i2QP CJvwyOXxDHK2S5P+NTjSOUDb0DNQLdANv4CQDw57eYsW2hXjChjqT54qrWbZiMt2c+Ir XwQIX7GHr/qUk4cjW0e7DCahzj1GeJNZzQwr1+3H9C5a8B13Fo3EJGAL3clPjNm7Hs7Z ZgBOBsG/rMT5LeNbKNAnF7tzju4uqyIoUbqbsAQ0G9Y+jRRItb8qRQXvXWKm9VMegI5G ZCkueKVZVu49VnaQOiIc9p/2BbtzrEsG26odcMB1/JO6BoTlZMh9qNBNex3Sv8+gxrFH h+vA== 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=bsYHMM4KQR/dk/tUS7ledM0bnCWuN9TnhrrUBjdXT54=; b=zsn40IzUXfgXBUUZw4qoXrYMEJlKdZhWAypAxlSEtpdbAU991cDasE9FDTHVhnq2eN FjSgQldeJuKU56x+enC98zAIJhKoy8w7FqRMG78nqoabqyTNX849cZ348O6FAxygi2Fl aU0/2kZug6Fi0EVHcaWWz+Jg4+R0SUAVP5j6vmK4xcZVZtJxGlD8rumqjh15P6EpjaKI EJb0VzSghpqmjyDQZbzT6RkFHY6K+7IgT/ISioVKUtc/wjEAWVjULqXqYteI7djqOl4C ZPFX+M67b/OROeIYo90J6X5HOXHBpgTeW9qud1wPf26/4ILS1m2lFgS7kfYkBNg306fA Kmaw== 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 c3-20020a637243000000b00517d81adf00si9273374pgn.624.2023.05.12.01.10.50; Fri, 12 May 2023 01:11:02 -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 S240074AbjELIDm convert rfc822-to-8bit (ORCPT + 99 others); Fri, 12 May 2023 04:03:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239907AbjELIDh (ORCPT ); Fri, 12 May 2023 04:03:37 -0400 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 870051B1; Fri, 12 May 2023 01:03:36 -0700 (PDT) Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-55cc8aadc97so145945517b3.3; Fri, 12 May 2023 01:03:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683878615; x=1686470615; 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=7s4e0MjmU1kwM5vp61pgU5E9clOgFoIIasjKLiukKxM=; b=YfrN0ZZOhLX+JyIuNretCxodipOST0lQKJtqWeHIcC5VOh4eeSo22uMm0JI1lEl8o4 5VfN98E6tN64F/NGbxVt5+ZvYOXbDDr+KwUFrwCZqYPYu5OdHiywdQktnh+bIgbHcrXb HvsTjxUSbhGKZXCeKap0q1bae5jqj0vpX25FMLy5oZxOm4jry0FDVVgBd89KHi6xQdWU k91lN0LgVd4e4CwlOewOFyYGk1WPZZhgKD9jsFbhcA3gRPXVx0Y/gbNr3Nd3ATzpbWK2 U7LFoBBJ9bwxv/D5NdihZWTWoHDTeNfXDylCG9EbCj8OqBSHqLMLY1iEHgRZgYw0L0Cf 03Ww== X-Gm-Message-State: AC+VfDyl1GN1rooicTl+ZCLOyJeGdfgSvbxF1tdoCT3L/7OTktYkBVVZ 4wuFTcA6GxEG68VpsczRvT+Mo2KLPcoSIRk0 X-Received: by 2002:a81:9c10:0:b0:559:e974:82 with SMTP id m16-20020a819c10000000b00559e9740082mr26094993ywa.20.1683878615301; Fri, 12 May 2023 01:03:35 -0700 (PDT) Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com. [209.85.128.180]) by smtp.gmail.com with ESMTPSA id g142-20020a815294000000b0055d6b67f468sm5484204ywb.14.2023.05.12.01.03.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 12 May 2023 01:03:33 -0700 (PDT) Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-55cc8aadc97so145944667b3.3; Fri, 12 May 2023 01:03:33 -0700 (PDT) X-Received: by 2002:a0d:cc09:0:b0:55a:5870:3d47 with SMTP id o9-20020a0dcc09000000b0055a58703d47mr23747937ywd.26.1683878613001; Fri, 12 May 2023 01:03:33 -0700 (PDT) MIME-Version: 1.0 References: <8db63020d18fc22e137e4a8f0aa15e6b9949a6f6.1683722688.git.geert+renesas@glider.be> In-Reply-To: From: Geert Uytterhoeven Date: Fri, 12 May 2023 10:03:22 +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 Fri, May 12, 2023 at 9:54 AM Ulf Hansson wrote: > On Thu, 11 May 2023 at 14:44, Geert Uytterhoeven wrote: > > 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). > > 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 µs, and > > 1 µs 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? Most of these loops run just 1 or 2 cycles. Performance mostly kicks in when timing out, but note that not calling ktime_get() also reduces loop overhead... 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