Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp608756ybk; Wed, 13 May 2020 08:24:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyzvwpc6YBWfj/pnDIAosL4dlLYH+Ev11kpysnJAv5hSfLNNwaeUeUaYC5EgmW1EcrWbgr6 X-Received: by 2002:a17:906:4957:: with SMTP id f23mr423806ejt.340.1589383481493; Wed, 13 May 2020 08:24:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589383481; cv=none; d=google.com; s=arc-20160816; b=OxtQaK29Ftms8dNxft3MGqiX3B6YydALVC8vAhfMWgTooUEAtJJ3zj4DkA2EuNWTaM EfJkQnph8wluGEcGVi5IYusPTX1sUnfkXkKbkDHYopdzjA8WXBmmMX+MUDYQ2/7Ut62N hDDcBz8gRrnmDdp39s/BKB9PmBb5T/kW/KBXu31W8O6YXRwub7dc6Hv3w87s8kof8BiG dG1AO3ByYZpgYeYvLeGYV+aeKSGk2lGSGG+rofshUrA7N3CSHS6ZMXgTc0Cqw1dZQo8F En26rFEuynEbrP99LtRVGBYEnoNF4eH9JLLb+6AvD5CTkon0aAbPl60mAB0gmfvDmgNJ MCUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version; bh=klA7kjCRgTh7w4ABQm1XG/GGzZPWDtCKKvZ6hhZvn0M=; b=nNU5HA8ifoNBiKAtxf6MaN5/3JwNPRqd8howrkpBiG0lByfaUYe8P4C7xkyMoNMb57 lWmmNc8SiLyxivqJ5uR1HIiz5P8QiYX1BMF3HCdA/wUSf6F6VmyrbBgunuU7h2iIGso6 WzyXBAO9LRV0/UbStjbCYXVa0btXdA95+HngsrBlczD2lHfYOoo9zCzVBPpfkU1ahshI BEJEE99awGXGGs5tZf0icnSIyymRYt8ViYvFBnEACS02cutxyy6AjB9mxIiaoh8v7PB4 pV1AyiOm+j3Az+kFG+nE6E0iKbQuLbObWkFuxh+9xxeD9+zcKk4qocjSLlgDaSECokO2 /lag== 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d14si5620eja.116.2020.05.13.08.24.18; Wed, 13 May 2020 08:24:41 -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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388806AbgEMPUj (ORCPT + 99 others); Wed, 13 May 2020 11:20:39 -0400 Received: from mail-ot1-f65.google.com ([209.85.210.65]:44092 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728692AbgEMPUi (ORCPT ); Wed, 13 May 2020 11:20:38 -0400 Received: by mail-ot1-f65.google.com with SMTP id j4so13679288otr.11 for ; Wed, 13 May 2020 08:20:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=klA7kjCRgTh7w4ABQm1XG/GGzZPWDtCKKvZ6hhZvn0M=; b=YK0ioO+YfCbNiaGbOoSJRicx6qImHVCju1HKhrbmZ+nAvS238PNTwdF2e8T7HZbTWW 7ThBzh6Ypt0tU6ddGckPvCTonNrN4FoCIqedhj+9V+5vCMXnmB/0xEG+6on6grUvLVA1 VekEw4jRwmH9LhGweKlmEf2kYFzp95crlrIx1XbtVWFlBHMsk8HcWDE21dgo4LZLahYD kvPW0O4nqLEbuZFOOVSO1tuvvUwdwKhfemGivyz+YRyMLqJL0y1y8S1LFYsNgF6Bdk06 6RRGLEOvoLZkcOiVM5t6/9PhBlHaSWyyr5BlmaT7vqUb7dgX6ZItMD0U6+qH6b2J3dUc 0gEg== X-Gm-Message-State: AGi0PuboOSvRofnPZ4xXwz6ZOHnFXuYuqxcfjfU9bpEGD2IXkchCO0vu mE7iQJA+jvj9Bnlwd+eIYaaXVWSLPJxqDk3aTBY= X-Received: by 2002:a05:6830:10ce:: with SMTP id z14mr20242588oto.118.1589383237714; Wed, 13 May 2020 08:20:37 -0700 (PDT) MIME-Version: 1.0 References: <20200513151840.36400-1-heikki.krogerus@linux.intel.com> In-Reply-To: <20200513151840.36400-1-heikki.krogerus@linux.intel.com> From: "Rafael J. Wysocki" Date: Wed, 13 May 2020 17:20:26 +0200 Message-ID: Subject: Re: [PATCH] kobject: Make sure the parent does not get released before its children To: Heikki Krogerus Cc: Greg Kroah-Hartman , Linux Kernel Mailing List , Naresh Kamboju , kernel test robot , Brendan Higgins , Randy Dunlap , "Rafael J. Wysocki" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 13, 2020 at 5:18 PM Heikki Krogerus wrote: > > 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 Reviewed-by: Rafael J. Wysocki > --- > 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 >