public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>, Greg KH <greg@kroah.com>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: [PATCH RESEND] char_dev: add cdev->release() and convert cdev_alloc() to use it
Date: Thu, 28 Aug 2008 18:36:56 +0200	[thread overview]
Message-ID: <48B6D428.2020308@kernel.org> (raw)

Add cdev->release() so that cdev can be considered in more involved
object lifetime management.  cdev_alloc() used a separate ktype for
auto-free release().  This patch converts it to use cdev->release() so
that there's no need for separate ktype and cdev_init() can be used
for auto-free variant too.

Signed-off-by: Tejun Heo <tj@kernel.org>
---
This one is also for CUSE.  Oops, forgot lkml.  Resending.  Thanks.

 fs/char_dev.c        |   30 +++++++++++++-----------------
 include/linux/cdev.h |    1 +
 2 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/fs/char_dev.c b/fs/char_dev.c
index 3cb7cda..3d50199 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -478,26 +478,22 @@ void cdev_del(struct cdev *p)
 }
 
 
-static void cdev_default_release(struct kobject *kobj)
+static void cdev_release(struct kobject *kobj)
 {
 	struct cdev *p = container_of(kobj, struct cdev, kobj);
 	cdev_purge(p);
+	if (p->release)
+		p->release(p);
 }
 
-static void cdev_dynamic_release(struct kobject *kobj)
-{
-	struct cdev *p = container_of(kobj, struct cdev, kobj);
-	cdev_purge(p);
-	kfree(p);
-}
-
-static struct kobj_type ktype_cdev_default = {
-	.release	= cdev_default_release,
+static struct kobj_type cdev_ktype = {
+	.release	= cdev_release,
 };
 
-static struct kobj_type ktype_cdev_dynamic = {
-	.release	= cdev_dynamic_release,
-};
+static void cdev_alloc_release(struct cdev *cdev)
+{
+	kfree(cdev);
+}
 
 /**
  * cdev_alloc() - allocate a cdev structure
@@ -506,10 +502,10 @@ static struct kobj_type ktype_cdev_dynamic = {
  */
 struct cdev *cdev_alloc(void)
 {
-	struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
+	struct cdev *p = kmalloc(sizeof(struct cdev), GFP_KERNEL);
 	if (p) {
-		INIT_LIST_HEAD(&p->list);
-		kobject_init(&p->kobj, &ktype_cdev_dynamic);
+		cdev_init(p, NULL);
+		p->release = cdev_alloc_release;
 	}
 	return p;
 }
@@ -526,7 +522,7 @@ void cdev_init(struct cdev *cdev, const struct file_operations *fops)
 {
 	memset(cdev, 0, sizeof *cdev);
 	INIT_LIST_HEAD(&cdev->list);
-	kobject_init(&cdev->kobj, &ktype_cdev_default);
+	kobject_init(&cdev->kobj, &cdev_ktype);
 	cdev->ops = fops;
 }
 
diff --git a/include/linux/cdev.h b/include/linux/cdev.h
index fb45919..9c3b17e 100644
--- a/include/linux/cdev.h
+++ b/include/linux/cdev.h
@@ -16,6 +16,7 @@ struct cdev {
 	struct list_head list;
 	dev_t dev;
 	unsigned int count;
+	void (*release)(struct cdev *);
 };
 
 void cdev_init(struct cdev *, const struct file_operations *);
-- 
1.5.4.5


             reply	other threads:[~2008-08-28 16:39 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-28 16:36 Tejun Heo [this message]
2008-08-28 16:47 ` [PATCH RESEND] char_dev: add cdev->release() and convert cdev_alloc() to use it Greg KH
2008-08-28 16:56   ` Tejun Heo
2008-08-28 17:38     ` Greg KH
2008-08-28 17:44       ` Tejun Heo
2008-08-28 17:48         ` Greg KH
2008-08-28 17:55           ` Tejun Heo
2008-08-28 18:17             ` Greg KH
2008-11-13  8:58               ` Tejun Heo
     [not found]                 ` <492136F5.8010903@kernel.org>
     [not found]                   ` <20081117171717.GB31306@kroah.com>
     [not found]                     ` <49221CE8.1050402@kernel.org>
2008-11-18  1:40                       ` Tejun Heo
2008-11-18 13:44                         ` Boaz Harrosh
2008-11-18 15:58                           ` Tejun Heo
2008-11-19 17:59                             ` Miklos Szeredi
2008-11-20  5:54                             ` Greg KH
2008-11-20  6:28                               ` Tejun Heo
2008-11-20 11:45                                 ` Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=48B6D428.2020308@kernel.org \
    --to=tj@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=greg@kroah.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox