linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: James Bottomley <James.Bottomley@hansenpartnership.com>
Cc: Matthew Wilcox <willy@infradead.org>,
	Wang Hai <wanghai38@huawei.com>,
	cl@linux.com, penberg@kernel.org, rientjes@google.com,
	iamjoonsoo.kim@lge.com, akpm@linux-foundation.org,
	khlebnikov@yandex-team.ru, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org
Subject: Re: kobject_init_and_add is easy to misuse
Date: Tue, 2 Jun 2020 19:36:03 +0200	[thread overview]
Message-ID: <20200602173603.GB3579519@kroah.com> (raw)
In-Reply-To: <1591111514.4253.32.camel@HansenPartnership.com>

On Tue, Jun 02, 2020 at 08:25:14AM -0700, James Bottomley wrote:
> On Tue, 2020-06-02 at 05:10 -0700, Matthew Wilcox wrote:
> > On Tue, Jun 02, 2020 at 07:50:33PM +0800, Wang Hai wrote:
> > > syzkaller reports for memory leak when kobject_init_and_add()
> > > returns an error in the function sysfs_slab_add() [1]
> > > 
> > > When this happened, the function kobject_put() is not called for
> > > the
> > > corresponding kobject, which potentially leads to memory leak.
> > > 
> > > This patch fixes the issue by calling kobject_put() even if
> > > kobject_init_and_add() fails.
> > 
> > I think this speaks to a deeper problem with kobject_init_and_add()
> > -- the need to call kobject_put() if it fails is not readily apparent
> > to most users.  This same bug appears in the first three users of
> > kobject_init_and_add() that I checked --
> > arch/ia64/kernel/topology.c
> > drivers/firmware/dmi-sysfs.c
> > drivers/firmware/efi/esrt.c
> > drivers/scsi/iscsi_boot_sysfs.c
> > 
> > Some do get it right --
> > arch/powerpc/kernel/cacheinfo.c
> > drivers/gpu/drm/ttm/ttm_bo.c
> > drivers/gpu/drm/ttm/ttm_memory.c
> > drivers/infiniband/hw/mlx4/sysfs.c
> > 
> > I'd argue that the current behaviour is wrong,
> 
> Absolutely agree with this.  We have a big meta pattern here where we
> introduce functions with tortuous semantics then someone creates a
> checker for the semantics and misuses come crawling out of the woodwork
> leading to floods of patches, usually for little or never used error
> paths, which really don't buy anything apart from theoretical
> correctness.  Just insisting on simple semantics would have avoided
> this.

I "introduced" this way back at the end of 2007.  It's not exactly a new
function.

> >  that kobject_init_and_add() should call kobject_put() if the add
> > fails.  This would need a tree-wide audit.  But somebody needs to do
> > that anyway because based on my random sampling, half of the users
> > currently get it wrong.
> 
> Well, the semantics of kobject_init() are free on fail, so these are
> the ones everyone seems to be using.  The semantics of kobject_add are
> put on fail.  The problem is that put on fail isn't necessarily correct
> in the kobject_init() case: the release function may make assumptions
> about the object hierarchy which aren't satisfied in the kobject_init()
> failure case.  This argues that kobject_init_and_add() can't ever have
> correct semantics and we should eliminate it.

At the time, it did reduce common functionality and error handling all
into a simpler function.  And, given it's history, it must have somehow
worked for the past 12 years or so :)

Odds are, lots of the callers shouldn't be messing around with kobjects
in the first place.  Originally it was only assumed that there would be
very few users.  But it has spread to filesystems and firmware
subsystems.  Drivers should never use it though, so it's a good hint
something is wrong there...

Anyway, patches to fix this up to make a "sane" api for kobjects is
always appreciated.  Personally I don't have the time at the moment.

thanks,

greg k-h


  reply	other threads:[~2020-06-02 17:36 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-02 11:50 [PATCH] mm/slub: fix a memory leak in sysfs_slab_add() Wang Hai
2020-06-02 12:10 ` kobject_init_and_add is easy to misuse Matthew Wilcox
2020-06-02 13:48   ` Konstantin Khlebnikov
2020-06-02 14:04   ` Greg Kroah-Hartman
2020-06-02 14:57     ` Matthew Wilcox
2020-06-02 15:25   ` James Bottomley
2020-06-02 17:36     ` Greg Kroah-Hartman [this message]
2020-06-02 19:54       ` James Bottomley
2020-06-02 20:07         ` Greg Kroah-Hartman
2020-06-02 21:51           ` James Bottomley
2020-06-03  0:04             ` James Bottomley
2020-06-03  0:22             ` Jason Gunthorpe
2020-06-03 18:04               ` James Bottomley
2020-06-03 18:36                 ` Jason Gunthorpe
2020-06-03 19:02                   ` James Bottomley
2020-06-03 19:30                     ` Jason Gunthorpe
2020-06-03 20:56                       ` James Bottomley
2020-06-04  0:23                         ` Jason Gunthorpe
2020-06-02 19:46   ` Jason Gunthorpe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200602173603.GB3579519@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@linux.com \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=khlebnikov@yandex-team.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=penberg@kernel.org \
    --cc=rientjes@google.com \
    --cc=wanghai38@huawei.com \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox