* improve similar pkg suggestions and share code in core-functions for all scripts...
[t2sde.git] / package / kernel / linux / up-next-acpi-usleep_range.patch
blob30cd2ad92896f2ca6f6e11343bbcbe7aa932485d
1 From: "Rafael J. Wysocki" <rjw@rjwysocki.net>
2 To: Linux ACPI <linux-acpi@vger.kernel.org>
3 Cc: LKML <linux-kernel@vger.kernel.org>, Linux PM <linux-pm@vger.kernel.org>,
4 Len Brown <len.brown@intel.com>, Arjan van de Ven <arjan@linux.intel.com>,
5 Pierre Gondois <pierre.gondois@arm.com>,
6 Dietmar Eggemann <dietmar.eggemann@arm.com>,
7 Hans de Goede <hdegoede@redhat.com>,
8 Mario Limonciello <mario.limonciello@amd.com>,
9 "Gautham R. Shenoy" <gautham.shenoy@amd.com>
10 Subject: [PATCH v1] ACPI: OSL: Use usleep_range() in acpi_os_sleep()
11 Date: Thu, 05 Dec 2024 12:24:35 +0100
12 Message-ID: <5857066.DvuYhMxLoT@rjwysocki.net>
13 Precedence: bulk
14 X-Mailing-List: linux-kernel@vger.kernel.org
15 List-Id: <linux-kernel.vger.kernel.org>
16 List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
17 List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
18 MIME-Version: 1.0
19 Content-Transfer-Encoding: 7Bit
20 Content-Type: text/plain; charset="UTF-8"
22 From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
23 Subject: [PATCH v0.1] ACPI: OSL: Use usleep_range() in acpi_os_sleep()
25 As stated by Len in [1], the extra delay added by msleep() to the
26 sleep time value passed to it can be significant, roughly between
27 1.5 ns on systems with HZ = 1000 and as much as 15 ms on systems with
28 HZ = 100, which is hardly acceptable, at least for small sleep time
29 values.
31 msleep(5) on the default HZ = 250 in Ubuntu on a modern PC takes about
32 12 ms. This results in over 800 ms of spurious system resume delay on
33 systems such as the Dell XPS-13-9300, which use ASL Sleep(5ms) in a
34 tight loop.
36 Address this by using usleep_range() in acpi_os_sleep() instead of
37 msleep(). For short sleep times this is a no brainer, but even for
38 long sleeps usleep_range() should be preferred because timer wheel
39 timers are optimized for cancelation before they expire and this
40 particular timer is not going to be canceled.
42 Add at least 50 us on top of the requested sleep time in case the
43 timer can be subject to coalescing, which is consistent with what's
44 done in user space in this context [2], but for sleeps longer than 5 ms
45 use 1% of the requested sleep time for this purpose.
47 The rationale here is that longer sleeps don't need that much of a timer
48 precision as a rule and making the timer a more likely candidate for
49 coalescing in these cases is generally desirable. It starts at 5 ms so
50 that the delta between the requested sleep time and the effective
51 deadline is a contiuous function of the former.
53 Link: https://lore.kernel.org/linux-pm/c7db7e804c453629c116d508558eaf46477a2d73.1731708405.git.len.brown@intel.com/ [1]
54 Link: https://lore.kernel.org/linux-pm/
55 CAJvTdK=Q1kwWA6Wxn8Zcf0OicDEk6cHYFAvQVizgA47mXu63+g@mail.gmail.com/ [2]
56 Closes: https://bugzilla.kernel.org/show_bug.cgi?id=216263
57 Reported-by: Len Brown <lenb@kernel.org>
58 Reviewed-by: Hans de Goede <hdegoede@redhat.com>
59 Reviewed-by: Mario Limonciello <mario.limonciello@amd.com>
60 Tested-by: Mario Limonciello <mario.limonciello@amd.com>
61 Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
62 ---
64 The previous RFC version of this patch is here:
66 https://lore.kernel.org/linux-pm/5839859.DvuYhMxLoT@rjwysocki.net/
68 The difference between this version and the RFC is a changelog update
69 suggested by Len, the Closes: tag and the tags from Hans and Mario.
71 ---
72 drivers/acpi/osl.c | 22 +++++++++++++++++++++-
73 1 file changed, 21 insertions(+), 1 deletion(-)
75 Index: linux-pm/drivers/acpi/osl.c
76 ===================================================================
77 --- linux-pm.orig/drivers/acpi/osl.c
78 +++ linux-pm/drivers/acpi/osl.c
79 @@ -607,7 +607,27 @@ acpi_status acpi_os_remove_interrupt_han
81 void acpi_os_sleep(u64 ms)
83 - msleep(ms);
84 + u64 usec = ms * USEC_PER_MSEC, delta_us = 50;
86 + /*
87 + * Use a hrtimer because the timer wheel timers are optimized for
88 + * cancelation before they expire and this timer is not going to be
89 + * canceled.
90 + *
91 + * Set the delta between the requested sleep time and the effective
92 + * deadline to at least 50 us in case there is an opportunity for timer
93 + * coalescing.
94 + *
95 + * Moreover, longer sleeps can be assumed to need somewhat less timer
96 + * precision, so sacrifice some of it for making the timer a more likely
97 + * candidate for coalescing by setting the delta to 1% of the sleep time
98 + * if it is above 5 ms (this value is chosen so that the delta is a
99 + * continuous function of the sleep time).
100 + */
101 + if (ms > 5)
102 + delta_us = (USEC_PER_MSEC / 100) * ms;
104 + usleep_range(usec, usec + delta_us);
107 void acpi_os_stall(u32 us)