From patchwork Fri Mar 15 16:57:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heikki Krogerus X-Patchwork-Id: 10855305 X-Patchwork-Delegate: andy.shevchenko@gmail.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1CE5317E6 for ; Fri, 15 Mar 2019 16:59:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 09BC92A199 for ; Fri, 15 Mar 2019 16:59:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F22CC2A1F0; Fri, 15 Mar 2019 16:59:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A37902A199 for ; Fri, 15 Mar 2019 16:59:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729687AbfCOQ7K (ORCPT ); Fri, 15 Mar 2019 12:59:10 -0400 Received: from mga17.intel.com ([192.55.52.151]:39358 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729633AbfCOQ6H (ORCPT ); Fri, 15 Mar 2019 12:58:07 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Mar 2019 09:58:07 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,482,1544515200"; d="scan'208";a="155397179" Received: from black.fi.intel.com (HELO black.fi.intel.com.) ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 15 Mar 2019 09:58:05 -0700 From: Heikki Krogerus To: Hans de Goede Cc: Andy Shevchenko , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, platform-driver-x86@vger.kernel.org Subject: [PATCH 02/12] software node: Increment parent node's ref count Date: Fri, 15 Mar 2019 19:57:50 +0300 Message-Id: <20190315165800.5058-3-heikki.krogerus@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190315165800.5058-1-heikki.krogerus@linux.intel.com> References: <20190315165800.5058-1-heikki.krogerus@linux.intel.com> MIME-Version: 1.0 Sender: platform-driver-x86-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If the parent node is removed before its children, release() may be called first for the parent instead of the last child when the child's ref count reaches zero. Since the nodes release their parent's resources in the release callback, that would cause NULL pointer dereference to happen. To prevent that from ever happening, incrementing the parent node's reference count when creating a new child node and decrementing it when the child node is "released". Signed-off-by: Heikki Krogerus --- drivers/base/swnode.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/base/swnode.c b/drivers/base/swnode.c index 3a50a8edd3d3..8d4485d8d0f8 100644 --- a/drivers/base/swnode.c +++ b/drivers/base/swnode.c @@ -560,6 +560,7 @@ static void software_node_release(struct kobject *kobj) if (swnode->parent) { ida_simple_remove(&swnode->parent->child_ids, swnode->id); list_del(&swnode->entry); + kobject_put(&swnode->parent->kobj); } else { ida_simple_remove(&swnode_root_ids, swnode->id); } @@ -610,8 +611,10 @@ fwnode_create_software_node(const struct property_entry *properties, INIT_LIST_HEAD(&swnode->children); swnode->parent = p; - if (p) + if (p) { + kobject_get(&p->kobj); list_add_tail(&swnode->entry, &p->children); + } ret = kobject_init_and_add(&swnode->kobj, &software_node_type, p ? &p->kobj : NULL, "node%d", swnode->id);