Skip to content

Commit

Permalink
char_dev: pin parent kobject
Browse files Browse the repository at this point in the history
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 <dmitry.torokhov@gmail.com>
Acked-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
dtor authored and torvalds committed Oct 22, 2012
1 parent 6f0c058 commit 2f0157f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion fs/char_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,19 @@ static int exact_lock(dev_t dev, void *data)
*/
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
{
int error;

p->dev = dev;
p->count = count;
return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);

error = kobj_map(cdev_map, dev, count, NULL,
exact_match, exact_lock, p);
if (error)
return error;

kobject_get(p->kobj.parent);

return 0;
}

static void cdev_unmap(dev_t dev, unsigned count)
Expand All @@ -498,14 +508,20 @@ void cdev_del(struct cdev *p)
static void cdev_default_release(struct kobject *kobj)
{
struct cdev *p = container_of(kobj, struct cdev, kobj);
struct kobject *parent = kobj->parent;

cdev_purge(p);
kobject_put(parent);
}

static void cdev_dynamic_release(struct kobject *kobj)
{
struct cdev *p = container_of(kobj, struct cdev, kobj);
struct kobject *parent = kobj->parent;

cdev_purge(p);
kfree(p);
kobject_put(parent);
}

static struct kobj_type ktype_cdev_default = {
Expand Down

0 comments on commit 2f0157f

Please sign in to comment.