Skip to contentSkip to conversation

btrfs: fix memory leak for page count

PATCH In review robbieko 3 participants 4 messages Jul 17, 2020 – Jul 20, 2020 Btrfs file system

lore
Share & tools
lore

robbieko

author
#0 lore raw

PATCH 1/1 btrfs: fix memory leak for page count

From: Robbie Ko <[email protected]>

When lock_delalloc_page, we first lock the page and then check that the page dirty, if the page is not dirty, we will return -EAGAIN but all pages must be freed, otherwise page leak.

---
 fs/btrfs/extent_io.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +3 -2
index 68c96057ad2d..34d55b1e2a88 100644
@@ -1951,7 +1951,7 @@ static int __process_pages_contig(struct address_space *mapping,
 	struct page *pages[16];
 	unsigned ret;
 	int err = 0;
-	int i;
+	int i, j;
 
 	if (page_ops & PAGE_LOCK) {
 		ASSERT(page_ops == PAGE_LOCK);
@@ -1999,7 +1999,8 @@ static int __process_pages_contig(struct address_space *mapping,
 				if (!PageDirty(pages[i]) ||
 				    pages[i]->mapping != mapping) {
 					unlock_page(pages[i]);
-					put_page(pages[i]);
+					for (j = i; j < ret; j++)
+						put_page(pages[j]);
 					err = -EAGAIN;
 					goto out;
 				}
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · robbieko

From: Robbie Ko <[email protected]>

When lock_delalloc_page, we first lock the page and then check that the page dirty, if the page is not dirty, we will return -EAGAIN but all pages must be freed, otherwise page leak.

"When lock_delalloc_page" -> When locking pages for delalloc

We check if it's dirty and if the mapping still matches.

Btw, you can make line length closer to 75 characters, it makes things a bit more readable.

The subject is also a bit confusing:

"btrfs: fix memory leak for page count"

something along the lines "btrfs: fix page leaks after failure to lock page for delalloc" would be more clear to me at least,
it gives a clue about where the problem is.

Quoted 2 lines

The code looks correct, thanks.

Quoted 30 lines
---
 fs/btrfs/extent_io.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +3 -3
index 68c96057ad2d..34d55b1e2a88 100644
@@ -1951,7 +1951,7 @@ static int __process_pages_contig(struct address_space *mapping,
        struct page *pages[16];
        unsigned ret;
        int err = 0;
-       int i;
+       int i, j;
 
        if (page_ops & PAGE_LOCK) {
                ASSERT(page_ops == PAGE_LOCK);
@@ -1999,7 +1999,8 @@ static int __process_pages_contig(struct address_space *mapping,
                                if (!PageDirty(pages[i]) ||
                                    pages[i]->mapping != mapping) {
                                        unlock_page(pages[i]);
-                                       put_page(pages[i]);
+                                       for (j = i; j < ret; j++)
+                                               put_page(pages[j]);
                                        err = -EAGAIN;
                                        goto out;
                                }
--

2.17.1

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
#2 lore raw
Quoted 46 lines · Filipe Manana
Quoted 7 lines · robbieko

From: Robbie Ko <[email protected]>

When lock_delalloc_page, we first lock the page and then check that the page dirty, if the page is not dirty, we will return -EAGAIN but all pages must be freed, otherwise page leak.

"When lock_delalloc_page" -> When locking pages for delalloc

We check if it's dirty and if the mapping still matches.

Btw, you can make line length closer to 75 characters, it makes things a bit more readable.

The subject is also a bit confusing:

"btrfs: fix memory leak for page count"

something along the lines "btrfs: fix page leaks after failure to lock page for delalloc" would be more clear to me at least,
it gives a clue about where the problem is.

Quoted 2 lines

The code looks correct, thanks.

Quoted 15 lines
---
 fs/btrfs/extent_io.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +1 -1
index 68c96057ad2d..34d55b1e2a88 100644
@@ -1951,7 +1951,7 @@ static int __process_pages_contig(struct address_space *mapping,
        struct page *pages[16];
        unsigned ret;
        int err = 0;
-       int i;
+       int i, j;
 
Quoted 9 lines

fs/btrfs/extent_io.c static int __process_pages_contig(struct address_space *mapping, · line 2003

Quoted 9 lines
fs/btrfs/extent_io.c +2 -1
        if (page_ops & PAGE_LOCK) {
                ASSERT(page_ops == PAGE_LOCK);
@@ -1999,7 +1999,8 @@ static int __process_pages_contig(struct address_space *mapping,
                                if (!PageDirty(pages[i]) ||
                                    pages[i]->mapping != mapping) {
                                        unlock_page(pages[i]);
-                                       put_page(pages[i]);
+                                       for (j = i; j < ret; j++)
+                                               put_page(pages[j]);

nit: You don't need to introduce another variable, you can simply reuse i from its current value:
for(; i < ret; i++)
put_pages(pages[j]

If 'j' is to be used I'da rather have it defined in the 'if' branch so it's lifespan is clearly visible.

Quoted 8 lines
Quoted 6 lines
fs/btrfs/extent_io.c +0 -1
                                        err = -EAGAIN;
                                        goto out;
                                }
--

2.17.1

author
#3 lore raw
In reply to Filipe Manana

Filipe Manana 於 2020/7/17 下午8:00 寫道:

Quoted 14 lines · robbieko
Quoted 6 lines

From: Robbie Ko <[email protected]>

When lock_delalloc_page, we first lock the page and then check that the page dirty, if the page is not dirty, we will return -EAGAIN but all pages must be freed, otherwise page leak.

"When lock_delalloc_page" -> When locking pages for delalloc

We check if it's dirty and if the mapping still matches.

Btw, you can make line length closer to 75 characters, it makes things a bit more readable.

Quoted 7 lines

The subject is also a bit confusing:

"btrfs: fix memory leak for page count"

something along the lines "btrfs: fix page leaks after failure to lock page for delalloc" would be more clear to me at least,
it gives a clue about where the problem is.

OK, I will resend patch.

Thanks.

Quoted 36 lines
Quoted 1 line

The code looks correct, thanks.

Quoted 30 lines
---
  fs/btrfs/extent_io.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +3 -3
index 68c96057ad2d..34d55b1e2a88 100644
@@ -1951,7 +1951,7 @@ static int __process_pages_contig(struct address_space *mapping,
         struct page *pages[16];
         unsigned ret;
         int err = 0;
-       int i;
+       int i, j;
 
         if (page_ops & PAGE_LOCK) {
                 ASSERT(page_ops == PAGE_LOCK);
@@ -1999,7 +1999,8 @@ static int __process_pages_contig(struct address_space *mapping,
                                 if (!PageDirty(pages[i]) ||
                                     pages[i]->mapping != mapping) {
                                         unlock_page(pages[i]);
-                                       put_page(pages[i]);
+                                       for (j = i; j < ret; j++)
+                                               put_page(pages[j]);
                                         err = -EAGAIN;
                                         goto out;
                                 }
--

2.17.1