psi-util: fix error handling

Signed-off-by: zhaoxiaohu <zhaoxiaohu@kuaishou.com>
This commit is contained in:
zhaoxiaohu 2024-09-04 11:11:54 +08:00 committed by zhangyao
parent 3fcb7f815b
commit 9b963cce2e
2 changed files with 65 additions and 1 deletions

View File

@ -0,0 +1,60 @@
From 90ec8ebe33ec72ed6d9f451de9443d67dd351d72 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Mon, 27 Feb 2023 19:02:41 +0100
Subject: [PATCH] psi-util: fix error handling
We checked ERRNO_IS_NOT_SUPPORTED on a possible positive non-error code,
which isn't right.
Fix that. Also add caching, since we are about to call this more often.
Reference: https://github.com/systemd/systemd/pull/26610/commits/1ea90a9d409999159345ad8e193f7ef4cc7f634d
Signed-off-by: zhaoxiaohu <zhaoxiaohu@kuaishou.com>
Signed-off-by: yuwang <yuwang@kuaishou.com>
---
src/shared/psi-util.c | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/src/shared/psi-util.c b/src/shared/psi-util.c
index 009095e..9045ae2 100644
--- a/src/shared/psi-util.c
+++ b/src/shared/psi-util.c
@@ -106,20 +106,25 @@ int read_resource_pressure(const char *path, PressureType type, ResourcePressure
}
int is_pressure_supported(void) {
+ static thread_local int cached = -1;
const char *p;
+ int r;
- /* The pressure files, both under /proc and in cgroups, will exist
- * even if the kernel has PSI support disabled; we have to read
- * the file to make sure it doesn't return -EOPNOTSUPP */
- FOREACH_STRING(p, "/proc/pressure/cpu", "/proc/pressure/io", "/proc/pressure/memory") {
- int r;
+ /* The pressure files, both under /proc/ and in cgroups, will exist even if the kernel has PSI
+ * support disabled; we have to read the file to make sure it doesn't return -EOPNOTSUPP */
+ if (cached >= 0)
+ return cached;
+
+ FOREACH_STRING(p, "/proc/pressure/cpu", "/proc/pressure/io", "/proc/pressure/memory") {
r = read_virtual_file(p, 0, NULL, NULL);
- if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
- return 0;
- if (r < 0)
+ if (r < 0) {
+ if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
+ return (cached = false);
+
return r;
+ }
}
- return 1;
+ return (cached = true);
}
--
2.33.0

View File

@ -25,7 +25,7 @@
Name: systemd Name: systemd
Url: https://systemd.io/ Url: https://systemd.io/
Version: 249 Version: 249
Release: 92 Release: 93
License: MIT and LGPLv2+ and GPLv2+ License: MIT and LGPLv2+ and GPLv2+
Summary: System and Service Manager Summary: System and Service Manager
@ -695,6 +695,7 @@ Patch6642: backport-sysusers-insist-that-root-group-is-0.patch
Patch6643: backport-importd-Always-specify-file-unpacked-by-tar.patch Patch6643: backport-importd-Always-specify-file-unpacked-by-tar.patch
Patch6644: backport-docs-remove-dev-tty-confusion.patch Patch6644: backport-docs-remove-dev-tty-confusion.patch
Patch6645: backport-wait-online-make-manager_link_is_online-return-0-whe.patch Patch6645: backport-wait-online-make-manager_link_is_online-return-0-whe.patch
Patch6646: backport-psi-util-fix-error-handling.patch
Patch9001: update-rtc-with-system-clock-when-shutdown.patch Patch9001: update-rtc-with-system-clock-when-shutdown.patch
Patch9002: udev-add-actions-while-rename-netif-failed.patch Patch9002: udev-add-actions-while-rename-netif-failed.patch
@ -2209,6 +2210,9 @@ grep -q -E '^KEYMAP="?fi-latin[19]"?' /etc/vconsole.conf 2>/dev/null &&
/usr/bin/systemd-cryptenroll /usr/bin/systemd-cryptenroll
%changelog %changelog
* Wed Dec 04 2024 zhangyao <zhangyao108@huawei.com> - 249-93
- add backport-psi-util-fix-error-handling.patch
* Wed Dec 04 2024 zhangyao <zhangyao108@huawei.com> - 249-92 * Wed Dec 04 2024 zhangyao <zhangyao108@huawei.com> - 249-92
- wait-online: fix handling of unmanaged state - wait-online: fix handling of unmanaged state
- add backport-wait-online-make-manager_link_is_online-return-0-whe.patch - add backport-wait-online-make-manager_link_is_online-return-0-whe.patch