Skip to content

Commit ce2c9cb

Browse files
committed
kobject: remove the static array for the name
Due to historical reasons, struct kobject contained a static array for the name, and a dynamic pointer in case the name got bigger than the array. That's just dumb, as people didn't always know which variable to reference, even with the accessor for the kobject name. This patch removes the static array, potentially saving a lot of memory as the majority of kobjects do not have a very long name. Thanks to Kay for the idea to do this. Cc: Kay Sievers <kay.sievers@vrfy.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 34980ca commit ce2c9cb

2 files changed

Lines changed: 42 additions & 44 deletions

File tree

‎include/linux/kobject.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ extern const char *kobject_actions[];
6363

6464
struct kobject {
6565
const char * k_name;
66-
char name[KOBJ_NAME_LEN];
6766
struct kref kref;
6867
struct list_head entry;
6968
struct kobject * parent;
@@ -188,18 +187,18 @@ extern struct kobject * kset_find_obj(struct kset *, const char *);
188187
* Use this when initializing an embedded kset with no other
189188
* fields to initialize.
190189
*/
191-
#define set_kset_name(str) .kset = { .kobj = { .name = str } }
190+
#define set_kset_name(str) .kset = { .kobj = { .k_name = str } }
192191

193192

194193
#define decl_subsys(_name,_type,_uevent_ops) \
195194
struct kset _name##_subsys = { \
196-
.kobj = { .name = __stringify(_name) }, \
195+
.kobj = { .k_name = __stringify(_name) }, \
197196
.ktype = _type, \
198197
.uevent_ops =_uevent_ops, \
199198
}
200199
#define decl_subsys_name(_varname,_name,_type,_uevent_ops) \
201200
struct kset _varname##_subsys = { \
202-
.kobj = { .name = __stringify(_name) }, \
201+
.kobj = { .k_name = __stringify(_name) }, \
203202
.ktype = _type, \
204203
.uevent_ops =_uevent_ops, \
205204
}

‎lib/kobject.c‎

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ int kobject_shadow_add(struct kobject *kobj, struct sysfs_dirent *shadow_parent)
170170
if (!(kobj = kobject_get(kobj)))
171171
return -ENOENT;
172172
if (!kobj->k_name)
173-
kobj->k_name = kobj->name;
173+
kobject_set_name(kobj, "NO_NAME");
174174
if (!*kobj->k_name) {
175175
pr_debug("kobject attempted to be registered with no name!\n");
176176
WARN_ON(1);
@@ -181,7 +181,7 @@ int kobject_shadow_add(struct kobject *kobj, struct sysfs_dirent *shadow_parent)
181181

182182
pr_debug("kobject %s: registering. parent: %s, set: %s\n",
183183
kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
184-
kobj->kset ? kobj->kset->kobj.name : "<NULL>" );
184+
kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" );
185185

186186
if (kobj->kset) {
187187
spin_lock(&kobj->kset->list_lock);
@@ -255,54 +255,50 @@ int kobject_register(struct kobject * kobj)
255255
int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
256256
{
257257
int error = 0;
258-
int limit = KOBJ_NAME_LEN;
258+
int limit;
259259
int need;
260260
va_list args;
261-
char * name;
261+
char *name;
262262

263-
/*
264-
* First, try the static array
265-
*/
266-
va_start(args,fmt);
267-
need = vsnprintf(kobj->name,limit,fmt,args);
263+
/* find out how big a buffer we need */
264+
name = kmalloc(1024, GFP_KERNEL);
265+
if (!name) {
266+
error = -ENOMEM;
267+
goto done;
268+
}
269+
va_start(args, fmt);
270+
need = vsnprintf(name, 1024, fmt, args);
268271
va_end(args);
269-
if (need < limit)
270-
name = kobj->name;
271-
else {
272-
/*
273-
* Need more space? Allocate it and try again
274-
*/
275-
limit = need + 1;
276-
name = kmalloc(limit,GFP_KERNEL);
277-
if (!name) {
278-
error = -ENOMEM;
279-
goto Done;
280-
}
281-
va_start(args,fmt);
282-
need = vsnprintf(name,limit,fmt,args);
283-
va_end(args);
284-
285-
/* Still? Give up. */
286-
if (need >= limit) {
287-
kfree(name);
288-
error = -EFAULT;
289-
goto Done;
290-
}
272+
kfree(name);
273+
274+
/* Allocate the new space and copy the string in */
275+
limit = need + 1;
276+
name = kmalloc(limit, GFP_KERNEL);
277+
if (!name) {
278+
error = -ENOMEM;
279+
goto done;
280+
}
281+
va_start(args, fmt);
282+
need = vsnprintf(name, limit, fmt, args);
283+
va_end(args);
284+
285+
/* something wrong with the string we copied? */
286+
if (need >= limit) {
287+
kfree(name);
288+
error = -EFAULT;
289+
goto done;
291290
}
292291

293292
/* Free the old name, if necessary. */
294-
if (kobj->k_name && kobj->k_name != kobj->name)
295-
kfree(kobj->k_name);
293+
kfree(kobj->k_name);
296294

297295
/* Now, set the new name */
298296
kobj->k_name = name;
299-
Done:
297+
done:
300298
return error;
301299
}
302-
303300
EXPORT_SYMBOL(kobject_set_name);
304301

305-
306302
/**
307303
* kobject_rename - change the name of an object
308304
* @kobj: object in question.
@@ -477,13 +473,16 @@ void kobject_cleanup(struct kobject * kobj)
477473
struct kobj_type * t = get_ktype(kobj);
478474
struct kset * s = kobj->kset;
479475
struct kobject * parent = kobj->parent;
476+
const char *name = kobj->k_name;
480477

481478
pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
482-
if (kobj->k_name != kobj->name)
483-
kfree(kobj->k_name);
484-
kobj->k_name = NULL;
485-
if (t && t->release)
479+
if (t && t->release) {
486480
t->release(kobj);
481+
/* If we have a release function, we can guess that this was
482+
* not a statically allocated kobject, so we should be safe to
483+
* free the name */
484+
kfree(name);
485+
}
487486
if (s)
488487
kset_put(s);
489488
kobject_put(parent);

0 commit comments

Comments
 (0)