Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp608432ybk; Wed, 13 May 2020 08:24:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQlwBikWwf3ZrwEmoBWbMbjo1xFX/rsLiDbUp6uOn/w5KcKrvk57km9FuZs+S7SZWPztXI X-Received: by 2002:a05:6402:507:: with SMTP id m7mr159150edv.7.1589383451678; Wed, 13 May 2020 08:24:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589383451; cv=none; d=google.com; s=arc-20160816; b=FmN+ClGy0u6b4CzTnLTDMZD5Bhxjx3AzBlbiXHdPf6PJ8Sr7MYbZvPc0IRSyGi5edR TGeWn8YVXwqzcjleSBgd9GdSc4XvyekyqFjCwnlzLQazEvxaKXHixUh4jgU5E6okN68W 3UYU7hEOGxagQZouGnyoZn3jzHtkpTT285PaxxGFropImydQ62uIrzfDvLJwmJyE9NXv 8Kt9J6CUBqXR+lPsbIqZ9Ny06cHDPHZI8D6f20ZCNYFObfWL8AaXQDo+F5vHQG90Jr19 XJltwO8XkB+h/noHCL31txQjJPBeIZVKFNfdauhPq5wE0UzEVYtse8i4EWS4XQ5voOb+ IeMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:ironport-sdr:ironport-sdr; bh=XtpyKRWSbUn1TC/7BUNpQglYMkCi1gG6WzUt3jUwGY8=; b=w8yM7FraEqdlNdLVxa3+6rSdNSSuAdIE29jvKJEtb/CwVaZP8urtIHefMdXGYnMNgj COO+KcZw/Yi0ay9Qv5RVkaiGJiaCOvTOcNBmxUFZXsKyoGeVTwJRxUFeBCLjR+c4+cak SUxYuagdtJDBePO+P6a9Z3vALfnn6+KrjHZAKokw5Vb4Y/Afcc9rJUg+G6pP6EbSVs1w vV44xlvutKbGA3xQI0Nk/NDtOmvmrIWlW4h4qQT7tZA7Sh2WfdYDJbybP/Pp99qh0KTn l/lz+3XKFMkjP3SCtAsSWeOqwltfgq7a7dmPdT6nguIRh1SwTqwNNlv7giqoaYMIHueS UY1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k19si6131679edj.455.2020.05.13.08.23.48; Wed, 13 May 2020 08:24:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731645AbgEMPSo (ORCPT + 99 others); Wed, 13 May 2020 11:18:44 -0400 Received: from mga06.intel.com ([134.134.136.31]:37459 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728692AbgEMPSn (ORCPT ); Wed, 13 May 2020 11:18:43 -0400 IronPort-SDR: XBAO56FGfdHKvt7Ps5lAsWUCUlMImvJHDIczkVOGX1InYZYtZchCI0ezeQJEP51GfRcjZpEkMB bULchRFVDcug== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 May 2020 08:18:43 -0700 IronPort-SDR: ZYUSbumjQv0c93Qa2sJu6X8ea2I6840uLUiFkzZ+VI+MZNfSQZdlNMce96urcLoifwV9JDXFw9 iSQ364fXHd5A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,388,1583222400"; d="scan'208";a="371935382" Received: from black.fi.intel.com (HELO black.fi.intel.com.) ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 13 May 2020 08:18:41 -0700 From: Heikki Krogerus To: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, Naresh Kamboju , kernel test robot , Brendan Higgins , Randy Dunlap , "Rafael J. Wysocki" Subject: [PATCH] kobject: Make sure the parent does not get released before its children Date: Wed, 13 May 2020 18:18:40 +0300 Message-Id: <20200513151840.36400-1-heikki.krogerus@linux.intel.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the function kobject_cleanup(), kobject_del(kobj) is called before the kobj->release(). That makes it possible to release the parent of the kobject before the kobject itself. To fix that, adding function __kboject_del() that does everything that kobject_del() does except release the parent reference. kobject_cleanup() then calls __kobject_del() instead of kobject_del(), and separately decrements the reference count of the parent kobject after kobj->release() has been called. Reported-by: Naresh Kamboju Reported-by: kernel test robot Fixes: 7589238a8cf3 ("Revert "software node: Simplify software_node_release() function"") Cc: Brendan Higgins Cc: Randy Dunlap Suggested-by: "Rafael J. Wysocki" Signed-off-by: Heikki Krogerus --- lib/kobject.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/kobject.c b/lib/kobject.c index 65fa7bf70c57..32432036bef8 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -599,14 +599,7 @@ int kobject_move(struct kobject *kobj, struct kobject *new_parent) } EXPORT_SYMBOL_GPL(kobject_move); -/** - * kobject_del() - Unlink kobject from hierarchy. - * @kobj: object. - * - * This is the function that should be called to delete an object - * successfully added via kobject_add(). - */ -void kobject_del(struct kobject *kobj) +static void __kobject_del(struct kobject *kobj) { struct kernfs_node *sd; const struct kobj_type *ktype; @@ -625,9 +618,23 @@ void kobject_del(struct kobject *kobj) kobj->state_in_sysfs = 0; kobj_kset_leave(kobj); - kobject_put(kobj->parent); kobj->parent = NULL; } + +/** + * kobject_del() - Unlink kobject from hierarchy. + * @kobj: object. + * + * This is the function that should be called to delete an object + * successfully added via kobject_add(). + */ +void kobject_del(struct kobject *kobj) +{ + struct kobject *parent = kobj->parent; + + __kobject_del(kobj); + kobject_put(parent); +} EXPORT_SYMBOL(kobject_del); /** @@ -663,6 +670,7 @@ EXPORT_SYMBOL(kobject_get_unless_zero); */ static void kobject_cleanup(struct kobject *kobj) { + struct kobject *parent = kobj->parent; struct kobj_type *t = get_ktype(kobj); const char *name = kobj->name; @@ -684,7 +692,7 @@ static void kobject_cleanup(struct kobject *kobj) if (kobj->state_in_sysfs) { pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n", kobject_name(kobj), kobj); - kobject_del(kobj); + __kobject_del(kobj); } if (t && t->release) { @@ -698,6 +706,8 @@ static void kobject_cleanup(struct kobject *kobj) pr_debug("kobject: '%s': free name\n", name); kfree_const(name); } + + kobject_put(parent); } #ifdef CONFIG_DEBUG_KOBJECT_RELEASE -- 2.26.2