Skip to contentSkip to conversation

Btrfs: cleanup pages properly when ENOMEM in compression

PATCH In review Josef Bacik 2 participants 3 messages Oct 5, 2012 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: cleanup pages properly when ENOMEM in compression

We were freeing non-existent pages which was causing a panic for a user who was suffering from ENOMEM. This patch fixes the problem. Thanks,

---
 fs/btrfs/compression.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
fs/btrfs/compression.c +9 -3
index 43d1c5a..36eb5f2 100644
@@ -577,6 +577,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	u64 em_start;
 	struct extent_map *em;
 	int ret = -ENOMEM;
+	int faili = 0;
 	u32 *sums;
 
 	tree = &BTRFS_I(inode)->io_tree;
@@ -626,8 +627,11 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	for (pg_index = 0; pg_index < nr_pages; pg_index++) {
 		cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
 							      __GFP_HIGHMEM);
-		if (!cb->compressed_pages[pg_index])
+		if (!cb->compressed_pages[pg_index]) {
+			faili = pg_index - 1;
+			ret = -ENOMEM;
 			goto fail2;
+		}
 	}
 	cb->nr_pages = nr_pages;
 
@@ -713,8 +717,10 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	return 0;
 
 fail2:
-	for (pg_index = 0; pg_index < nr_pages; pg_index++)
-		free_page((unsigned long)cb->compressed_pages[pg_index]);
+	while (faili >= 0) {
+		__free_page(cb->compressed_pages[pg_index]);
+		faili--;
+	}
 
 	kfree(cb->compressed_pages);
 fail1:
Signature · 1 line
1.7.7.6
#1 lore raw
Quoted 24 lines · Josef Bacik
fs/btrfs/compression.c +5 -1
@@ -577,6 +577,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	u64 em_start;
 	struct extent_map *em;
 	int ret = -ENOMEM;
+	int faili = 0;
 	u32 *sums;
 
 	tree = &BTRFS_I(inode)->io_tree;
@@ -626,8 +627,11 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	for (pg_index = 0; pg_index < nr_pages; pg_index++) {
 		cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
 							      __GFP_HIGHMEM);
-		if (!cb->compressed_pages[pg_index])
+		if (!cb->compressed_pages[pg_index]) {
+			faili = pg_index - 1;
+			ret = -ENOMEM;
 			goto fail2;
+		}
 	}
 	cb->nr_pages = nr_pages;
 

fs/btrfs/compression.c int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, · line 721

Quoted 7 lines
fs/btrfs/compression.c +2 -2
@@ -713,8 +717,10 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	return 0;
 
 fail2:
-	for (pg_index = 0; pg_index < nr_pages; pg_index++)
-		free_page((unsigned long)cb->compressed_pages[pg_index]);
+	while (faili >= 0) {
+		__free_page(cb->compressed_pages[pg_index]);
                                                 ^^^^^^^^

s/pg_index/faili/

Also, faili (how do you pronounce that?) is initialized to 0, the while loop will always fire (and burn a page), initialize it to -1.

Other than that, I like the fix, thanks.

fs/btrfs/compression.c int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, · line 726

Quoted 5 lines
fs/btrfs/compression.c +2 -0
+		faili--;
+	}
 
 	kfree(cb->compressed_pages);
 fail1:

david

#2 lore raw
Quoted 2 lines · David Sterba

Also, faili (how do you pronounce that?) is initialized to 0, the while loop will always fire (and burn a page), initialize it to -1.

My oversight sorry, it takes a different codepath and this will not happen.

david