From: Elena Reshetova <elena.reshetova@intel.com>
To: axboe@kernel.dk
Cc: james.bottomley@hansenpartnership.com,
linux-kernel@vger.kernel.org, linux-block@vger.kernel.org,
linux-scsi@vger.kernel.org, linux-btrfs@vger.kernel.org,
peterz@infradead.org, gregkh@linuxfoundation.org,
fujita.tomonori@lab.ntt.co.jp, mingo@redhat.com, clm@fb.com,
jbacik@fb.com, dsterba@suse.com, keescook@chromium.org,
Elena Reshetova <elena.reshetova@intel.com>
Subject: [PATCH 5/6] block: convert bsg_device.ref_count from atomic_t to refcount_t
Date: Fri, 20 Oct 2017 11:16:01 +0300 [thread overview]
Message-ID: <1508487362-26663-6-git-send-email-elena.reshetova@intel.com> (raw)
In-Reply-To: <1508487362-26663-1-git-send-email-elena.reshetova@intel.com>
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 bsg_device.ref_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>
---
block/bsg.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/block/bsg.c b/block/bsg.c
index ee1335c..6c98422 100644
--- a/block/bsg.c
+++ b/block/bsg.c
@@ -21,6 +21,7 @@
#include <linux/idr.h>
#include <linux/bsg.h>
#include <linux/slab.h>
+#include <linux/refcount.h>
#include <scsi/scsi.h>
#include <scsi/scsi_ioctl.h>
@@ -38,7 +39,7 @@ struct bsg_device {
struct list_head busy_list;
struct list_head done_list;
struct hlist_node dev_list;
- atomic_t ref_count;
+ refcount_t ref_count;
int queued_cmds;
int done_cmds;
wait_queue_head_t wq_done;
@@ -710,7 +711,7 @@ static int bsg_put_device(struct bsg_device *bd)
mutex_lock(&bsg_mutex);
- do_free = atomic_dec_and_test(&bd->ref_count);
+ do_free = refcount_dec_and_test(&bd->ref_count);
if (!do_free) {
mutex_unlock(&bsg_mutex);
goto out;
@@ -768,7 +769,7 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
bsg_set_block(bd, file);
- atomic_set(&bd->ref_count, 1);
+ refcount_set(&bd->ref_count, 1);
mutex_lock(&bsg_mutex);
hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
@@ -788,7 +789,7 @@ static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
if (bd->queue == q) {
- atomic_inc(&bd->ref_count);
+ refcount_inc(&bd->ref_count);
goto found;
}
}
--
2.7.4
next prev parent reply other threads:[~2017-10-20 8:16 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-20 8:15 [PATCH 0/6] v4 block refcount conversion patches Elena Reshetova
2017-10-20 8:15 ` [PATCH 1/6] block: convert bio.__bi_cnt from atomic_t to refcount_t Elena Reshetova
2017-10-20 8:15 ` [PATCH 2/6] block: convert blk_queue_tag.refcnt " Elena Reshetova
2017-10-20 8:15 ` [PATCH 3/6] block: convert blkcg_gq.refcnt " Elena Reshetova
2017-10-20 8:16 ` [PATCH 4/6] block: convert io_context.active_ref " Elena Reshetova
2017-10-20 8:16 ` Elena Reshetova [this message]
2017-10-20 8:16 ` [PATCH 6/6] drivers, block: convert xen_blkif.refcnt " Elena Reshetova
2017-10-20 8:43 ` [PATCH 0/6] v4 block refcount conversion patches Johannes Thumshirn
2017-10-20 10:25 ` 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=1508487362-26663-6-git-send-email-elena.reshetova@intel.com \
--to=elena.reshetova@intel.com \
--cc=axboe@kernel.dk \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=fujita.tomonori@lab.ntt.co.jp \
--cc=gregkh@linuxfoundation.org \
--cc=james.bottomley@hansenpartnership.com \
--cc=jbacik@fb.com \
--cc=keescook@chromium.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@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;
as well as URLs for NNTP newsgroup(s).