linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zdenek Kabelac <zkabelac@redhat.com>
To: Elena Reshetova <elena.reshetova@intel.com>, dm-devel@redhat.com
Cc: linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-raid@vger.kernel.org, kent.overstreet@gmail.com,
	koverstreet@google.com, ejt@redhat.com, snitzer@redhat.com,
	shli@kernel.org, agk@redhat.com, peterz@infradead.org,
	keescook@chromium.org
Subject: Re: [PATCH 4/4] dm: convert table_device.count from atomic_t to refcount_t
Date: Thu, 23 Nov 2017 16:19:21 +0100	[thread overview]
Message-ID: <92f8c04f-ef1a-c41b-e82f-8d68a3ec84b5@redhat.com> (raw)
In-Reply-To: <1508485059-21881-5-git-send-email-elena.reshetova@intel.com>

Dne 20.10.2017 v 09:37 Elena Reshetova napsal(a):
> atomic_t variables are currently used to implement reference
> counters with the following properties:
>   - counter is initialized to 1 using atomic_set()
>   - a resource is freed upon counter reaching zero
>   - once counter reaches zero, its further
>     increments aren't allowed
>   - counter schema uses basic atomic operations
>     (set, inc, inc_not_zero, dec_and_test, etc.)
> 
> Such atomic variables should be converted to a newly provided
> refcount_t type and API that prevents accidental counter overflows
> and underflows. This is important since overflows and underflows
> can lead to use-after-free situation and be exploitable.
> 
> The variable table_device.count is used as pure reference counter.
> Convert it to refcount_t and fix up the operations.
> 
> Suggested-by: Kees Cook <keescook@chromium.org>
> Reviewed-by: David Windsor <dwindsor@gmail.com>
> Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com>
> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
> ---
>   drivers/md/dm.c | 12 +++++++-----
>   1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/md/dm.c b/drivers/md/dm.c
> index 4be8532..be12f3f 100644
> --- a/drivers/md/dm.c
> +++ b/drivers/md/dm.c
> @@ -24,6 +24,7 @@
>   #include <linux/delay.h>
>   #include <linux/wait.h>
>   #include <linux/pr.h>
> +#include <linux/refcount.h>
>   
>   #define DM_MSG_PREFIX "core"
>   
> @@ -98,7 +99,7 @@ struct dm_md_mempools {
>   
>   struct table_device {
>   	struct list_head list;
> -	atomic_t count;
> +	refcount_t count;
>   	struct dm_dev dm_dev;
>   };
>   
> @@ -685,10 +686,11 @@ int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
>   
>   		format_dev_t(td->dm_dev.name, dev);
>   
> -		atomic_set(&td->count, 0);
> +		refcount_set(&td->count, 1);
>   		list_add(&td->list, &md->table_devices);
> +	} else {
> +		refcount_inc(&td->count);
>   	}
> -	atomic_inc(&td->count);
>   	mutex_unlock(&md->table_devices_lock);
>   


NACK

This patch (2a0b4682e09d76466f7b8f5e347ae2ff02f033af) currently breaks 
accounting of opened devices.

I.e.   multisegment device  (target with 3 segments is not properly accounted)


Patch needs reworking and users of 'dm' and 4.15-rc0 kernel should rather 
switch back to 4.14 ATM as it's unclear which other parts can be affected.

Zdenek

>   	*result = &td->dm_dev;
> @@ -701,7 +703,7 @@ void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
>   	struct table_device *td = container_of(d, struct table_device, dm_dev);
>   
>   	mutex_lock(&md->table_devices_lock);
> -	if (atomic_dec_and_test(&td->count)) {
> +	if (refcount_dec_and_test(&td->count)) {
>   		close_table_device(td, md);
>   		list_del(&td->list);
>   		kfree(td);
> @@ -718,7 +720,7 @@ static void free_table_devices(struct list_head *devices)
>   		struct table_device *td = list_entry(tmp, struct table_device, list);
>   
>   		DMWARN("dm_destroy: %s still exists with %d references",
> -		       td->dm_dev.name, atomic_read(&td->count));
> +		       td->dm_dev.name, refcount_read(&td->count));
>   		kfree(td);
>   	}
>   }
> 

  reply	other threads:[~2017-11-23 15:19 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-20  7:37 [PATCH 0/4] dm and bcache refcount conversions Elena Reshetova
2017-10-20  7:37 ` [PATCH 1/4] bcache: convert cached_dev.count from atomic_t to refcount_t Elena Reshetova
2017-10-20 18:39   ` Michael Lyle
2017-10-23  6:45     ` Reshetova, Elena
2017-10-20  7:37 ` [PATCH 2/4] dm cache: convert dm_cache_metadata.ref_count " Elena Reshetova
2017-10-20  7:37 ` [PATCH 3/4] dm: convert dm_dev_internal.count " Elena Reshetova
2017-11-23 15:49   ` [dm-devel] " Alasdair G Kergon
2017-11-24  7:36     ` Reshetova, Elena
2017-11-25  5:56       ` Mike Snitzer
2017-11-28 10:07         ` Reshetova, Elena
2017-11-28 19:02           ` Mike Snitzer
2017-11-29  8:05             ` Reshetova, Elena
2017-10-20  7:37 ` [PATCH 4/4] dm: convert table_device.count " Elena Reshetova
2017-11-23 15:19   ` Zdenek Kabelac [this message]
2017-11-24  8:29     ` Reshetova, Elena
2017-11-24 14:04       ` Alasdair G Kergon
2017-11-24 14:44         ` Reshetova, Elena

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=92f8c04f-ef1a-c41b-e82f-8d68a3ec84b5@redhat.com \
    --to=zkabelac@redhat.com \
    --cc=agk@redhat.com \
    --cc=dm-devel@redhat.com \
    --cc=ejt@redhat.com \
    --cc=elena.reshetova@intel.com \
    --cc=keescook@chromium.org \
    --cc=kent.overstreet@gmail.com \
    --cc=koverstreet@google.com \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=shli@kernel.org \
    --cc=snitzer@redhat.com \
    /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;
as well as URLs for NNTP newsgroup(s).