linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block: bsg: move atomic_t ref_count variable to refcount API
@ 2018-08-27 18:33 John Pittman
  2018-08-28  1:17 ` Jens Axboe
  0 siblings, 1 reply; 2+ messages in thread
From: John Pittman @ 2018-08-27 18:33 UTC (permalink / raw)
  To: axboe; +Cc: linux-scsi, linux-block, John Pittman

Currently, variable ref_count within the bsg_device struct is of
type atomic_t.  For variables being used as reference counters,
the refcount API should be used instead of atomic.  The newer
refcount API works to prevent counter overflows and use-after-free
bugs.  So, move this varable from the atomic API to refcount,
potentially avoiding the issues mentioned.

Signed-off-by: John Pittman <jpittman@redhat.com>
---
 block/bsg.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/block/bsg.c b/block/bsg.c
index db588add6ba6..9a442c23a715 100644
--- a/block/bsg.c
+++ b/block/bsg.c
@@ -37,7 +37,7 @@ struct bsg_device {
 	struct request_queue *queue;
 	spinlock_t lock;
 	struct hlist_node dev_list;
-	atomic_t ref_count;
+	refcount_t ref_count;
 	char name[20];
 	int max_queue;
 };
@@ -252,7 +252,7 @@ static int bsg_put_device(struct bsg_device *bd)
 
 	mutex_lock(&bsg_mutex);
 
-	if (!atomic_dec_and_test(&bd->ref_count)) {
+	if (!refcount_dec_and_test(&bd->ref_count)) {
 		mutex_unlock(&bsg_mutex);
 		return 0;
 	}
@@ -290,7 +290,7 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
 
 	bd->queue = rq;
 
-	atomic_set(&bd->ref_count, 1);
+	refcount_set(&bd->ref_count, 1);
 	hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
 
 	strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
@@ -308,7 +308,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.17.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] block: bsg: move atomic_t ref_count variable to refcount API
  2018-08-27 18:33 [PATCH] block: bsg: move atomic_t ref_count variable to refcount API John Pittman
@ 2018-08-28  1:17 ` Jens Axboe
  0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2018-08-28  1:17 UTC (permalink / raw)
  To: John Pittman; +Cc: linux-scsi, linux-block

On 8/27/18 12:33 PM, John Pittman wrote:
> Currently, variable ref_count within the bsg_device struct is of
> type atomic_t.  For variables being used as reference counters,
> the refcount API should be used instead of atomic.  The newer
> refcount API works to prevent counter overflows and use-after-free
> bugs.  So, move this varable from the atomic API to refcount,
> potentially avoiding the issues mentioned.

Applied, thanks.

-- 
Jens Axboe

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-08-28  1:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-08-27 18:33 [PATCH] block: bsg: move atomic_t ref_count variable to refcount API John Pittman
2018-08-28  1:17 ` Jens Axboe

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).