Skip to contentSkip to conversation

kobject: name reference should not be lost when lack of memory error happens

PATCH In review Roman Tereshonkov 3 participants 4 messages Oct 7, 2010 – Oct 9, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kobject: name reference should not be lost when lack of memory error happens

If lack of memory error happens we must restore the old kobject name reference

---
 lib/kobject.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
lib/kobject.c +3 -1
index f07c572..c5cb117 100644
@@ -222,8 +222,10 @@ int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
 		return 0;
 
 	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
-	if (!kobj->name)
+	if (!kobj->name) {
+		kobj->name = old_name;
 		return -ENOMEM;
+	}
 
 	/* ewww... some of these buggers have '/' in the name ... */
 	while ((s = strchr(kobj->name, '/')))
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 1 line · Roman Tereshonkov

If lack of memory error happens we must restore the old kobject name reference

No, we can't do that, as the caller will not be expecting that to have happened, right Kay?

I think the code as-is is correct, your kobject is in an unknown state and can't be used anymore.

thanks,

greg k-h

Quoted 23 lines
---
 lib/kobject.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
lib/kobject.c +3 -1
index f07c572..c5cb117 100644
@@ -222,8 +222,10 @@ int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
 		return 0;
 
 	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
-	if (!kobj->name)
+	if (!kobj->name) {
+		kobj->name = old_name;
 		return -ENOMEM;
+	}
 
 	/* ewww... some of these buggers have '/' in the name ... */
 	while ((s = strchr(kobj->name, '/')))
Signature · 1 line
1.7.0.4
#2 lore raw
Quoted 8 lines · Greg KH
Quoted 1 line · Roman Tereshonkov

If lack of memory error happens we must restore the old kobject name reference

No, we can't do that, as the caller will not be expecting that to have happened, right Kay?

I think the code as-is is correct, your kobject is in an unknown state and can't be used anymore.

Yeah, we've been there several times already with no clear decision so far.

Trying to set a kobject name twice seems like a serious bug.

We should kill the old_name logic, and add a WARN() when kobj->name is already set when the function is called, I think.

People must use kobject_rename() if they want a new name, like the comment above kobject_set_name() says.

Kay

#3 lore raw
Quoted 10 lines · Kay Sievers
Quoted 8 lines · Greg KH
Quoted 1 line · Roman Tereshonkov

If lack of memory error happens we must restore the old kobject name reference

No, we can't do that, as the caller will not be expecting that to have happened, right Kay?

I think the code as-is is correct, your kobject is in an unknown state and can't be used anymore.

Quoted 6 lines

Yeah, we've been there several times already with no clear decision so far.

Trying to set a kobject name twice seems like a serious bug.

We should kill the old_name logic, and add a WARN() when kobj->name is already set when the function is called, I think.

Agreed, it will be interesting to see what is trying to do this :)

thanks,

greg k-h