From patchwork Mon Oct 22 00:57:19 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 1623181 Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 4CF72DFFF1 for ; Mon, 22 Oct 2012 00:57:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754821Ab2JVA5Z (ORCPT ); Sun, 21 Oct 2012 20:57:25 -0400 Received: from mail-pb0-f46.google.com ([209.85.160.46]:51318 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754692Ab2JVA5Z (ORCPT ); Sun, 21 Oct 2012 20:57:25 -0400 Received: by mail-pb0-f46.google.com with SMTP id rr4so1600982pbb.19 for ; Sun, 21 Oct 2012 17:57:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=IQ4e/QMqIkCoW56Ol+Ab7fklBUnsQtBUUbOj8CucOwg=; b=UyY2xCyi6OfCnBXiJBAUl+/lU+zhJRXamoUtvgHc9IvA2iZw2Z0B6icxievFWsRUO6 FN85Eo93KX0JzxlZJAwsgQyHG5WpF2TIT8tE7xrNGMxQjXtN2vPKZeCvQQfpMk+h//fa DXN8jBH1XmHKUlffohFb2x4PUmZi2Z4FJejV/aLu+vIajaBwfMmqcojNx/7DsYaq2jkc hNEefDUzbyWiCmT4k/sTcZLHkBBA2ZiCtga1rLru9OdDWpBUKdpcC1RD0UEyNtKaxQqI oUP7/1+XW7E5NDe893uYawqnZpXSnqCg+0hnHL6WYpsXSJ9ZqHjA5LgME71SKNHkeock f/pw== Received: by 10.68.216.131 with SMTP id oq3mr24982758pbc.147.1350867444604; Sun, 21 Oct 2012 17:57:24 -0700 (PDT) Received: from mailhub.coreip.homeip.net (c-67-188-112-76.hsd1.ca.comcast.net. [67.188.112.76]) by mx.google.com with ESMTPS id lb3sm5005210pbc.73.2012.10.21.17.57.22 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 21 Oct 2012 17:57:23 -0700 (PDT) From: Dmitry Torokhov To: Al Viro , Dave Jones Cc: Linus Torvalds , linux-kernel@vger.kernel.org, linux-input@vger.kernel.org Subject: [PATCH 1/2] char_dev: pin parent kobject Date: Sun, 21 Oct 2012 17:57:19 -0700 Message-Id: <1350867440-6109-1-git-send-email-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <20121021073928.GV2616@ZenIV.linux.org.uk> References: <20121021073928.GV2616@ZenIV.linux.org.uk> Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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 --- OK, so it looks like cdev kobject's parent is unused so we can just have users set themselves as parents of character device and have cdev_add() pin it. fs/char_dev.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/fs/char_dev.c b/fs/char_dev.c index 3f152b9..afc2bb6 100644 --- a/fs/char_dev.c +++ b/fs/char_dev.c @@ -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) @@ -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 = {