Эх сурвалжийг харах

Btrfs: fix uncheck memory allocations

To make Btrfs code more robust, several return value checks where memory
allocation can fail are introduced. I use BUG_ON where I don't know how
to handle the error properly, which increases the number of using the
notorious BUG_ON, though.

Signed-off-by: Yoshinori Sano <yoshinori.sano@gmail.com>
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Yoshinori Sano 14 жил өмнө
parent
commit
dac97e516c

+ 6 - 0
fs/btrfs/compression.c

@@ -340,6 +340,8 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
 
 
 	WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
 	WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
 	cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
 	cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
+	if (!cb)
+		return -ENOMEM;
 	atomic_set(&cb->pending_bios, 0);
 	atomic_set(&cb->pending_bios, 0);
 	cb->errors = 0;
 	cb->errors = 0;
 	cb->inode = inode;
 	cb->inode = inode;
@@ -354,6 +356,10 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
 	bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
 	bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
 
 
 	bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
 	bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
+	if(!bio) {
+		kfree(cb);
+		return -ENOMEM;
+	}
 	bio->bi_private = cb;
 	bio->bi_private = cb;
 	bio->bi_end_io = end_compressed_bio_write;
 	bio->bi_end_io = end_compressed_bio_write;
 	atomic_inc(&cb->pending_bios);
 	atomic_inc(&cb->pending_bios);

+ 4 - 0
fs/btrfs/extent-tree.c

@@ -6978,6 +6978,10 @@ static noinline int get_new_locations(struct inode *reloc_inode,
 			struct disk_extent *old = exts;
 			struct disk_extent *old = exts;
 			max *= 2;
 			max *= 2;
 			exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
 			exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
+			if (!exts) {
+				ret = -ENOMEM;
+				goto out;
+			}
 			memcpy(exts, old, sizeof(*exts) * nr);
 			memcpy(exts, old, sizeof(*exts) * nr);
 			if (old != *extents)
 			if (old != *extents)
 				kfree(old);
 				kfree(old);

+ 2 - 0
fs/btrfs/inode.c

@@ -290,6 +290,7 @@ static noinline int add_async_extent(struct async_cow *cow,
 	struct async_extent *async_extent;
 	struct async_extent *async_extent;
 
 
 	async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
 	async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
+	BUG_ON(!async_extent);
 	async_extent->start = start;
 	async_extent->start = start;
 	async_extent->ram_size = ram_size;
 	async_extent->ram_size = ram_size;
 	async_extent->compressed_size = compressed_size;
 	async_extent->compressed_size = compressed_size;
@@ -388,6 +389,7 @@ again:
 	     (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
 	     (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
 		WARN_ON(pages);
 		WARN_ON(pages);
 		pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
 		pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
+		BUG_ON(!pages);
 
 
 		if (BTRFS_I(inode)->force_compress)
 		if (BTRFS_I(inode)->force_compress)
 			compress_type = BTRFS_I(inode)->force_compress;
 			compress_type = BTRFS_I(inode)->force_compress;