Skip to content

Commit 2f0157f

Browse files
dtortorvalds
authored andcommitted
char_dev: pin parent kobject
In certain cases (for example when a cdev structure is embedded into another object whose lifetime is controlled by a separate kobject) it is beneficial to tie lifetime of another object to the lifetime of character device so that related object is not freed until after char_dev object is freed. To achieve this let's pin kobject's parent when doing cdev_add() and unpin when last reference to cdev structure is being released. Signed-off-by: Dmitry Torokhov <[email protected]> Acked-by: Al Viro <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 6f0c058 commit 2f0157f

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

fs/char_dev.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,19 @@ static int exact_lock(dev_t dev, void *data)
471471
*/
472472
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
473473
{
474+
int error;
475+
474476
p->dev = dev;
475477
p->count = count;
476-
return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
478+
479+
error = kobj_map(cdev_map, dev, count, NULL,
480+
exact_match, exact_lock, p);
481+
if (error)
482+
return error;
483+
484+
kobject_get(p->kobj.parent);
485+
486+
return 0;
477487
}
478488

479489
static void cdev_unmap(dev_t dev, unsigned count)
@@ -498,14 +508,20 @@ void cdev_del(struct cdev *p)
498508
static void cdev_default_release(struct kobject *kobj)
499509
{
500510
struct cdev *p = container_of(kobj, struct cdev, kobj);
511+
struct kobject *parent = kobj->parent;
512+
501513
cdev_purge(p);
514+
kobject_put(parent);
502515
}
503516

504517
static void cdev_dynamic_release(struct kobject *kobj)
505518
{
506519
struct cdev *p = container_of(kobj, struct cdev, kobj);
520+
struct kobject *parent = kobj->parent;
521+
507522
cdev_purge(p);
508523
kfree(p);
524+
kobject_put(parent);
509525
}
510526

511527
static struct kobj_type ktype_cdev_default = {

0 commit comments

Comments
 (0)