Skip to contentSkip to conversation

Freeing a dynamic struct cdev

DISCUSSION James C. Georgas 2 participants 5 messages Aug 6, 2005 – Aug 7, 2005 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

If I allocate a struct cdev using cdev_alloc(), what function do I call to free it when I'm done with it?

Signature · 1 line
James C. Georgas <[email protected]>
#1 lore raw
Quoted 2 lines · On Sat, 06 Aug 2005 09:26:15 -0400 James C. Georgas wrote:

If I allocate a struct cdev using cdev_alloc(), what function do I call to free it when I'm done with it?

Should be cdev_put(), which calls kobject_put(), which implements ref counting (using krefs), so that when the last reference is going away, the object will be removed.

---
~Randy

#2 lore raw
Quoted 11 lines · Randy.Dunlap
Quoted 2 lines · On Sat, 06 Aug 2005 09:26:15 -0400 James C. Georgas wrote:

If I allocate a struct cdev using cdev_alloc(), what function do I call to free it when I'm done with it?

Should be cdev_put(), which calls kobject_put(), which implements ref counting (using krefs), so that when the last reference is going away, the object will be removed.

---
~Randy

cdev_put() is not an exported symbol in the fs/char_dev.c file. Should it be?

Signature · 1 line
James C. Georgas <[email protected]>
#3 lore raw
Quoted 10 lines · On Sat, 06 Aug 2005 15:26:18 -0400 James C. Georgas wrote:
Quoted 9 lines · Randy.Dunlap
Quoted 2 lines · On Sat, 06 Aug 2005 09:26:15 -0400 James C. Georgas wrote:

If I allocate a struct cdev using cdev_alloc(), what function do I call to free it when I'm done with it?

Should be cdev_put(), which calls kobject_put(), which implements ref counting (using krefs), so that when the last reference is going away, the object will be removed.

Quoted 5 lines
Quoted 2 lines

---
~Randy

cdev_put() is not an exported symbol in the fs/char_dev.c file. Should it be?

Sorry about that, typo on my part. It's cdev_del().

---
~Randy

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

Ok, I really blew it with that last post:

Quoted 5 lines

cdev_add() calls:

	kobj_map()
	kobject_get() [indirectly]

This is wrong. When I first looked at it, I saw:

cdev_add() -> kobj_map() -> exact_lock() -> cdev_get() -> kobject_get()

but exact_lock() isn't called here; it is only passed as a function pointer to kobj_map(), and so calling cdev_add() does /not/ increment the reference count of the struct cdev.

I guess what threw me for a loop here was that I was expecting separate
functions: one to undo cdev_add() and one to undo cdev_alloc(), but cdev_del() accomplishes both tasks in one function, with the kobj_unmap() part having no effect, if the cdev is not mapped at the time of the call.

Signature · 1 line
James C. Georgas <[email protected]>