Skip to content

Commit db19395

Browse files
jtpittman195axboe
authored andcommitted
block: bsg: move atomic_t ref_count variable to refcount API
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> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 62d2a19 commit db19395

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

block/bsg.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct bsg_device {
3737
struct request_queue *queue;
3838
spinlock_t lock;
3939
struct hlist_node dev_list;
40-
atomic_t ref_count;
40+
refcount_t ref_count;
4141
char name[20];
4242
int max_queue;
4343
};
@@ -252,7 +252,7 @@ static int bsg_put_device(struct bsg_device *bd)
252252

253253
mutex_lock(&bsg_mutex);
254254

255-
if (!atomic_dec_and_test(&bd->ref_count)) {
255+
if (!refcount_dec_and_test(&bd->ref_count)) {
256256
mutex_unlock(&bsg_mutex);
257257
return 0;
258258
}
@@ -290,7 +290,7 @@ static struct bsg_device *bsg_add_device(struct inode *inode,
290290

291291
bd->queue = rq;
292292

293-
atomic_set(&bd->ref_count, 1);
293+
refcount_set(&bd->ref_count, 1);
294294
hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
295295

296296
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)
308308

309309
hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
310310
if (bd->queue == q) {
311-
atomic_inc(&bd->ref_count);
311+
refcount_inc(&bd->ref_count);
312312
goto found;
313313
}
314314
}

0 commit comments

Comments
 (0)