linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] power: Handle kmalloc failure in pm_vt_switch_required()
@ 2025-08-29 19:04 Thalia
  0 siblings, 0 replies; only message in thread
From: Thalia @ 2025-08-29 19:04 UTC (permalink / raw)
  To: rafael; +Cc: pavel, lenb, linux-pm, linux-kernel, Thalia

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@gmail.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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2025-08-29 19:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-29 19:04 [PATCH] power: Handle kmalloc failure in pm_vt_switch_required() Thalia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).