[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20250829190431.133717-1-thaliathenerder@gmail.com>
Date: Fri, 29 Aug 2025 15:04:31 -0400
From: Thalia <thaliathenerder@...il.com>
To: rafael@...nel.org
Cc: pavel@...nel.org,
lenb@...nel.org,
linux-pm@...r.kernel.org,
linux-kernel@...r.kernel.org,
Thalia <thaliathenerder@...il.com>
Subject: [PATCH] power: Handle kmalloc failure in pm_vt_switch_required()
This commit improves the robustness of the pm_vt_switch_required() function
by adding error handling for kmalloc() failure. The function now returns an
error code (-ENOMEM) and logs a message to the kernel ring buffer, making
it easier to debug memory allocation issues.
(that makes me sound so smart yay)
(this is my first commit so sorry if i did something wrong)
Signed-off-by: Thalia <thaliathenerder@...il.com>
---
kernel/power/console.c | 33 +++++++++++++++++++++++++++------
1 file changed, 27 insertions(+), 6 deletions(-)
diff --git a/kernel/power/console.c b/kernel/power/console.c
index 19c48aa5355d..0e4b8e0c480a 100644
--- a/kernel/power/console.c
+++ b/kernel/power/console.c
@@ -60,13 +60,34 @@ void pm_vt_switch_required(struct device *dev, bool required)
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
if (!entry)
goto out;
-
- entry->required = required;
- entry->dev = dev;
-
- list_add(&entry->head, &pm_vt_switch_list);
+int pm_vt_switch_required(struct device *dev, bool required)
+{
+ struct pm_vt_switch *entry, *tmp;
+ int ret = 0;
+
+ mutex_lock(&vt_switch_mutex);
+ list_for_each_entry(tmp, &pm_vt_switch_list, head) {
+ if (tmp->dev == dev) {
+ /* already registered, update requirement */
+ tmp->required = required;
+ goto out;
+ }
+ }
+
+ entry = kmalloc(sizeof(*entry), GFP_KERNEL);
+ if (!entry) {
+ pr_err("Failed to allocate memory for vt_switch entry\n");
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ entry->required = required;
+ entry->dev = dev;
+
+ list_add(&entry->head, &pm_vt_switch_list);
out:
- mutex_unlock(&vt_switch_mutex);
+ mutex_unlock(&vt_switch_mutex);
+ return ret;
}
EXPORT_SYMBOL(pm_vt_switch_required);
--
2.51.0
Powered by blists - more mailing lists