Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp6558092yba; Wed, 1 May 2019 15:01:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqx8rNnRvGAWoWvtvz16LQK+Tilu6ozrXXcRJhSw9SMr2Z6a3fkRDXiCKQFG5nATdSbmGgHh X-Received: by 2002:a65:4c0b:: with SMTP id u11mr320952pgq.405.1556748084861; Wed, 01 May 2019 15:01:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556748084; cv=none; d=google.com; s=arc-20160816; b=bYOAfomiMllLddbwxB3bLbYTRzbhC2Xmogil5dIfAIRj/QR186pjiv8Sr13IFs09yY eguqFEPemeFmfZLUbak5rvH0horzexPNaacALC731gEFzC+8qTsXRnDPBcR4cIIoBG9y pYcHI/WCP1jXD9DjaJSK5OUIBgbMJr0ep8f0D6G7S1trFmZsdfMEQWPH4E8IP91mFwSQ y1sMYoqsC7KQbNTj6mmFxGwcAlhL2CEZBMd3Ho+lTqMd5Lzcx75w0VyzK7vFwG2d/uQC XF5tLYoPnG3ygIdR8FPvR9H+JFPPrnHuy4zI3lZb2J2pGuDENJOCH5aJaquogIjdbYbZ PGVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-signature; bh=8NfadrpETJHHKG3TtI8ia2fqFejEwOO6jW14ZC31NK0=; b=xuF1sWWtEXXcxBLxjkoUgEBZHqHvXgyVo2C0wYTFtgXvb3zFiBIuLZL++VpxwEOO8f OmHE7EzMfLXUuwm1vhEiE1T2aAMahseratq2E4e7q5dSXDPlLB2yYhU1UYPVl3P8hr8+ 2//XwT9An28m7JCNOXrGzIC3xk74XUSv8OZRH7r9+xBKZrfMcHvv++ueeNQNPD2UGdoC 4256hI4AI/P/5gIJ5mkxI9q/NA1MLIKEPfpTWtCMUAte5/G29XvCpNuH9IUQA7ORvzB9 oAjPZp2DIODpK6SaIp++r6vkoOb93+lUxG3ywj3V9ND8O4ru7KYGuIMT3+xXCJ0sdh9Q BXgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tobin.cc header.s=fm3 header.b=gYZXIv6o; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=GoDhtNrr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n1si9323198pgq.247.2019.05.01.15.01.09; Wed, 01 May 2019 15:01:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@tobin.cc header.s=fm3 header.b=gYZXIv6o; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=GoDhtNrr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726245AbfEAV7i (ORCPT + 99 others); Wed, 1 May 2019 17:59:38 -0400 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]:35335 "EHLO wout1-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726144AbfEAV7h (ORCPT ); Wed, 1 May 2019 17:59:37 -0400 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 274C4550; Wed, 1 May 2019 17:59:36 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Wed, 01 May 2019 17:59:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tobin.cc; h=date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=8NfadrpETJHHKG3TtI8ia2fqFej EwOO6jW14ZC31NK0=; b=gYZXIv6oUzHB9lApr+QfbajwhZzBV+ENum6D87kyTpt 4DI5XGbtFm/pyLX/tv0s492kZDUNXvbPcUs+gzsE0qJeUhHAjWRBsdR+zz7fWniW PhJweVJqLaF+8ymGfsN/38Hzio68crmRMY3kdAmo/Kbj2ZEvMSjJld4MACzdvINE omCzTrFYpttGSk2ZvJJvNZX7ApMjsDFOEAmOyKyNNDOFsRPZI5mjWkqIacO9jJob 0RTjGWuCzwJIXxS3BCXAG4n4XtXtOofbGPF5BSTD0dyqw1MT1cqDx3KXRC0/dlDB 5K44EyWYy9Gztq09wAvWw53/TXGR/SuhKhp+i9Jtenw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=8Nfadr pETJHHKG3TtI8ia2fqFejEwOO6jW14ZC31NK0=; b=GoDhtNrraQZvmNxuFZhN/t boP3i8GRSMzazUH7dnetVaSdpCx8fj72xgE96yLLXgF0S/ed8VNPnN9GXZmjMkoO nBSYq871ESynWblov5X2Izv4nxw+ZeadlTEFdc52V5iPV3EzQfyPOs1TTRomPvLf 1DG5pltJrzx966nRM9Kx89JYrUqTBaWgmCHhH1yySrLEgQU/NOjTgEAXi76NeH/j K4qSlI/lyiKpymjmXC9uodIkLMpn/y98NAOCvVb5jz0C9hgdPVVj6J11KmQwg9Ww w2lntuTE9MPgn6BXs8B1mloC+V4/lImPVkx266Zh9IQlSIUK9tCmFHszHEOJDylg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrieejgdduieelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlfedtmdenucfjughrpeffhffvuffkfhggtggujgfofgesthdtredt ofervdenucfhrhhomhepfdfvohgsihhnucevrdcujfgrrhguihhnghdfuceomhgvsehtoh gsihhnrdgttgeqnecukfhppeduvddurdeggedrvddtgedrvdefheenucfrrghrrghmpehm rghilhhfrhhomhepmhgvsehtohgsihhnrdgttgenucevlhhushhtvghrufhiiigvpedu X-ME-Proxy: Received: from localhost (ppp121-44-204-235.bras1.syd2.internode.on.net [121.44.204.235]) by mail.messagingengine.com (Postfix) with ESMTPA id 1C612103C8; Wed, 1 May 2019 17:59:32 -0400 (EDT) Date: Thu, 2 May 2019 07:58:58 +1000 From: "Tobin C. Harding" To: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" , Tyrel Datwyler , Andy Shevchenko , Petr Mladek , Miroslav Benes , Viresh Kumar , Linux Kernel Mailing List Subject: Re: kobject_init_and_add() confusion Message-ID: <20190501215858.GE18827@eros.localdomain> References: <20190430233803.GB10777@eros.localdomain> <20190501111022.GA15959@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190501111022.GA15959@kroah.com> X-Mailer: Mutt 1.11.4 (2019-03-13) User-Agent: Mutt/1.11.4 (2019-03-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 01, 2019 at 01:10:22PM +0200, Greg Kroah-Hartman wrote: > On Wed, May 01, 2019 at 09:38:03AM +1000, Tobin C. Harding wrote: > > Hi, > > > > Looks like I've created a bit of confusion trying to fix memleaks in > > calls to kobject_init_and_add(). Its spread over various patches and > > mailing lists so I'm starting a new thread and CC'ing anyone that > > commented on one of those patches. > > > > If there is a better way to go about this discussion please do tell me. > > > > The problem > > ----------- > > > > Calls to kobject_init_and_add() are leaking memory throughout the kernel > > because of how the error paths are handled. > > s/are leaking/have the potential to leak/ > > Note, no one ever hits these error paths, so it isn't a big issue, and > is why no one has seen this except for the use of syzbot at times. One day I'll find an important issue to fix in the kernel. At the moment sweeping these up is good practice/learning. If you have any _real_ issues that need someone to turn the crank on feel free to dump them on me :) > > The solution > > ------------ > > > > Write the error path code correctly. > > > > Example > > ------- > > > > We have samples/kobject/kobject-example.c but it uses > > kobject_create_and_add(). I thought of adding another example file here > > but could not think of how to do it off the top of my head without being > > super contrived. Can add this to the TODO list if it will help. > > You could take the example I wrote in that old email and use it, or your > version below as well. Responded just now to that email. > > > Here is an attempted canonical usage of kobject_init_and_add() typical > > of the code that currently is getting it wrong. This is the second time > > I've written this and the first time it was wrong even after review (you > > know who you are, you are definitely buying the next round of drinks :) > > > > Assumes we have an object in memory already that has the kobject > > embedded in it. Variable 'kobj' below would typically be &ptr->kobj > > > > > > void fn(void) > > { > > int ret; > > > > ret = kobject_init_and_add(kobj, ktype, NULL, "foo"); > > if (ret) { > > /* > > * This means kobject_init() has succeeded > > kobject_init() can not fail except in fun ways that dumps the stack and > then keeps on going due to the failure being on the caller, not the > kobject code itself. Cheers, writing good documentation is HARD. > > * but kobject_add() failed. > > */ > > goto err_put; > > } > > > > ret = some_init_fn(); > > if (ret) { > > /* > > * We need to wind back kobject_add() AND kobject_put(). > > * kobject_add() incremented the refcount in > > * kobj->parent, that needs to be decremented THEN we need > > * the call to kobject_put() to decrement the refcount of kobj. > > */ > > goto err_del; > > } > > > > ret = some_other_init_fn(); > > if (ret) > > goto other_err; > > > > kobject_uevent(kobj, KOBJ_ADD); > > return 0; > > > > other_err: > > other_clean_up_fn(); > > err_del: > > kobject_del(kobj); > > err_put: > > kobject_put(kobj); > > > > return ret; > > } > > > > > > Have I got this correct? > > From what I can tell, yes. :) > > TODO > > ---- > > > > - Fix all the callsites to kobject_init_and_add() > > - Further clarify the function docstring for kobject_init_and_add() [perhaps] > > More documentation, sure! > > > - Add a section to Documentation/kobject.txt [optional] > > That file should probably be reviewed and converted to .rst, I haven't > looked at it in years. On my TODO list once I get kobject usage clear in my head. > > - Add a sample usage file under samples/kobject [optional] > > Would be a good idea, so we can point people at it. I'll combine your other email example with the extra init/error stuff from this one and BOOM! Thanks Greg. Tobin