public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Alexey Dobriyan <adobriyan@gmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Add refcount type and refcount misuse debugging
Date: Tue, 6 Dec 2011 16:30:54 -0800	[thread overview]
Message-ID: <20111206163054.f5e916e3.akpm@linux-foundation.org> (raw)
In-Reply-To: <20111206230107.GA22471@p183.telecom.by>

On Wed, 7 Dec 2011 02:01:07 +0300
Alexey Dobriyan <adobriyan@gmail.com> wrote:

> There is quite a lot of idiomatic code which does
> 
> 	if (atomic_dec_and_test(&obj->refcnt))
> 		[destroy obj]
> 
> Bugs like double-frees in this case are dereferred and it may not be
> immediately obvious that double-free has happened.
> 
> The answer is to wrap reference count debugging to every such operation.
> 
> Enter _refcnt_t (non-atomic version), refcnt_t (atomic version) datatypes
> and CONFIG_DEBUG_REFCNT config option.
> 
> The latter directly checks for
> a) GET on dead object
> b) PUT on dead object (aka double PUT)
> (and indirectly for memory corruptions turning positive integers into negative)
> 
> All of this has basic idea coming from grsecurity/PaX's CONFIG_PAX_REFCOUNT code.
> The main difference is that developer has to opt in into new code.
> 
> Convert struct proc_dir_entry for a start.
> 

Fair enough.

Does _refcnt_t need to exist?

The use of a leading _ to denote the nonatomic variant is a bit odd but
I guess it's better than putting "_nonatomic" at the end of everything.

How much code bloat will all this cause, and is there anything that can be
done to reduce it?

>  Very ligthly tested.

erk.

>
> ...
>
> --- /dev/null
> +++ b/include/linux/refcnt.h
> @@ -0,0 +1,89 @@
> +/*
> + * Use these types iff
> + * a) object is created with refcount 1, and
> + * b) every GET does +1, and
> + * c) every PUT does -1, and
> + * d) once refcount reaches 0, object is destroyed.
> + *
> + * Do not use otherwise.
> + *
> + * Use underscored version if refcount manipulations are under
> + * some sort of locking already making additional atomicity unnecessary.
> + */
> +#ifndef _LINUX_REFCNT_H
> +#define _LINUX_REFCNT_H
> +#include <linux/types.h>
> +#include <asm/atomic.h>

linux/atomic.h is more modern.

> +#ifdef CONFIG_DEBUG_REFCNT
> +#include <asm/bug.h>
> +#endif

This is a bit risky.  It means that a .c file such as

#include <linux/refcnt.h>
foo()
{
	BUG();
}

will compile OK with CONFIG_DEBUG_REFCNT=y and will fail with
CONFIG_DEBUG_REFCNT=n.  This makes Randy send more emails.  Suggest
removal of the ifdef.

> +typedef struct {
> +	int _n;

The code would look nicer if the unneeded _ was removed.

> +} _refcnt_t;
> +#define _REFCNT_INIT	((_refcnt_t){ ._n = 1 })
>
> ...
>
> +typedef struct {
> +	atomic_t _n;

s/_//

> +} refcnt_t;
> +#define REFCNT_INIT	((refcnt_t){ ._n = ATOMIC_INIT(1) })
> +
>
> ...
>

  reply	other threads:[~2011-12-07  0:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-06 23:01 [PATCH] Add refcount type and refcount misuse debugging Alexey Dobriyan
2011-12-07  0:30 ` Andrew Morton [this message]
2011-12-08 20:15   ` [PATCH v2] " Alexey Dobriyan
2011-12-07 19:22 ` [PATCH] " Will Drewry
2011-12-07 23:14   ` Alexey Dobriyan

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=20111206163054.f5e916e3.akpm@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=adobriyan@gmail.com \
    --cc=linux-kernel@vger.kernel.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