Skip to content

Commit 31d21d2

Browse files
sherllytytso
authored andcommitted
ext4: convert from atomic_t to refcount_t on ext4_io_end->count
refcount_t type and corresponding API can protect refcounters from accidental underflow and overflow and further use-after-free situations. Signed-off-by: Xiyu Yang <[email protected]> Signed-off-by: Xin Tan <[email protected]> Reviewed-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 1811bc4 commit 31d21d2

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

fs/ext4/ext4.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef _EXT4_H
1818
#define _EXT4_H
1919

20+
#include <linux/refcount.h>
2021
#include <linux/types.h>
2122
#include <linux/blkdev.h>
2223
#include <linux/magic.h>
@@ -241,7 +242,7 @@ typedef struct ext4_io_end {
241242
struct bio *bio; /* Linked list of completed
242243
* bios covering the extent */
243244
unsigned int flag; /* unwritten or not */
244-
atomic_t count; /* reference counter */
245+
refcount_t count; /* reference counter */
245246
struct list_head list_vec; /* list of ext4_io_end_vec */
246247
} ext4_io_end_t;
247248

fs/ext4/page-io.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,14 @@ ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
279279
io_end->inode = inode;
280280
INIT_LIST_HEAD(&io_end->list);
281281
INIT_LIST_HEAD(&io_end->list_vec);
282-
atomic_set(&io_end->count, 1);
282+
refcount_set(&io_end->count, 1);
283283
}
284284
return io_end;
285285
}
286286

287287
void ext4_put_io_end_defer(ext4_io_end_t *io_end)
288288
{
289-
if (atomic_dec_and_test(&io_end->count)) {
289+
if (refcount_dec_and_test(&io_end->count)) {
290290
if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) ||
291291
list_empty(&io_end->list_vec)) {
292292
ext4_release_io_end(io_end);
@@ -300,7 +300,7 @@ int ext4_put_io_end(ext4_io_end_t *io_end)
300300
{
301301
int err = 0;
302302

303-
if (atomic_dec_and_test(&io_end->count)) {
303+
if (refcount_dec_and_test(&io_end->count)) {
304304
if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
305305
err = ext4_convert_unwritten_io_end_vec(io_end->handle,
306306
io_end);
@@ -314,7 +314,7 @@ int ext4_put_io_end(ext4_io_end_t *io_end)
314314

315315
ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
316316
{
317-
atomic_inc(&io_end->count);
317+
refcount_inc(&io_end->count);
318318
return io_end;
319319
}
320320

0 commit comments

Comments
 (0)