Skip to contentSkip to conversation

XFS: Free buffer pages array unconditionally

PATCH In review Dave Chinner 2 participants 4 messages Dec 14, 2009 – Dec 15, 2009 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 XFS: Free buffer pages array unconditionally

The code in xfs_free_buf() only attempts to free the b_pages array if the buffer is a page cache backed or page allocated buffer. The extra log buffer that is used when the log wraps uses pages that are allocated to a different log buffer, but it still has a b_pages array allocated when those pages are associated to with the extra buffer in xfs_buf_associate_memory.

Hence we need to always attempt to free the b_pages array when tearing down a buffer, not just on buffers that are explicitly marked as page bearing buffers. This fixes a leak detected by the kernel memory leak code.

---
 fs/xfs/linux-2.6/xfs_buf.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
fs/xfs/linux-2.6/xfs_buf.c +2 -2
index 4ddc973..529d6a6 100644
@@ -318,6 +318,7 @@ _xfs_buf_free_pages(
 {
 	if (bp->b_pages != bp->b_page_array) {
 		kmem_free(bp->b_pages);
+		bp->b_pages = NULL;
 	}
 }
 
@@ -349,9 +350,8 @@ xfs_buf_free(
 				ASSERT(!PagePrivate(page));
 			page_cache_release(page);
 		}
-		_xfs_buf_free_pages(bp);
 	}
-
+	_xfs_buf_free_pages(bp);
 	xfs_buf_deallocate(bp);
 }
 
Signature · 6 lines
1.6.5

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 9 lines · Dave Chinner

The code in xfs_free_buf() only attempts to free the b_pages array if the buffer is a page cache backed or page allocated buffer. The extra log buffer that is used when the log wraps uses pages that are allocated to a different log buffer, but it still has a b_pages array allocated when those pages are associated to with the extra buffer in xfs_buf_associate_memory.

Hence we need to always attempt to free the b_pages array when tearing down a buffer, not just on buffers that are explicitly marked as page bearing buffers. This fixes a leak detected by the kernel memory leak code.

Three places call xfs_buf_get_pages();
- _xfs_buf_lookup_pages(), which sets the _XBF_PAGE_CACHE flag in the buffer after the call
- xfs_buf_associate_memory(), which sets no flag bit
- xfs_buf_get_noaddr(), which sets the _XBF_PAGES flag.

The only place that checks for _XBF_PAGES is xfs_buf_free().

Given that, I have two comments:
- You could just as easily have set the _XBF_PAGES flag in xfs_buf_associate_memory, thereby making that flag indicate consistently that the buffer has allocated pages
- Or, since you are proposing unconditionally freeing the pages, we can perhaps drop the _XBF_PAGES flag altogether since it no longer serves much purpose. (I prefer this.)

-Alex

Quoted 18 lines
---
 fs/xfs/linux-2.6/xfs_buf.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
fs/xfs/linux-2.6/xfs_buf.c +1 -0
index 4ddc973..529d6a6 100644
@@ -318,6 +318,7 @@ _xfs_buf_free_pages(
 {
 	if (bp->b_pages != bp->b_page_array) {
 		kmem_free(bp->b_pages);
+		bp->b_pages = NULL;
 	}
 }
 

fs/xfs/linux-2.6/xfs_buf.c xfs_buf_free( · line 356

Quoted 9 lines
fs/xfs/linux-2.6/xfs_buf.c +1 -2
@@ -349,9 +350,8 @@ xfs_buf_free(
 				ASSERT(!PagePrivate(page));
 			page_cache_release(page);
 		}
-		_xfs_buf_free_pages(bp);
 	}
-
+	_xfs_buf_free_pages(bp);
 	xfs_buf_deallocate(bp);
 }

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#2 lore raw
Quoted 17 lines · Alex Elder
Quoted 9 lines · Dave Chinner

The code in xfs_free_buf() only attempts to free the b_pages array if the buffer is a page cache backed or page allocated buffer. The extra log buffer that is used when the log wraps uses pages that are allocated to a different log buffer, but it still has a b_pages array allocated when those pages are associated to with the extra buffer in xfs_buf_associate_memory.

Hence we need to always attempt to free the b_pages array when tearing down a buffer, not just on buffers that are explicitly marked as page bearing buffers. This fixes a leak detected by the kernel memory leak code.

Three places call xfs_buf_get_pages();
- _xfs_buf_lookup_pages(), which sets the _XBF_PAGE_CACHE flag in the buffer after the call
- xfs_buf_associate_memory(), which sets no flag bit
- xfs_buf_get_noaddr(), which sets the _XBF_PAGES flag.

Quoted 6 lines

The only place that checks for _XBF_PAGES is xfs_buf_free().

Given that, I have two comments:
- You could just as easily have set the _XBF_PAGES flag in xfs_buf_associate_memory, thereby making that flag indicate consistently that the buffer has allocated pages

_XBF_PAGES is used to indicate that the buffer owns the pages and must free them when the buffer is freed.

_XBF_PAGE_CACHE indicates pages are allocated out of the buftarg page cache and need to be released when the buffer is freed.

In both cases, xfs_buf_free() has to free the pages associated with these buffers.

In the case of xfs_buf_associate_memory(), the memory attached to the buffer is owned by an external entity and hence we are not allowed to free it when we free the buffer. Therefore we can't set _XBF_PAGES on the buffer.

Quoted 2 lines

- Or, since you are proposing unconditionally freeing the pages,

This change doesn't unconditionally free the pages attached to the buffer - it unconditionally frees the array used to index the pages on the buffer when the buffer is torn down. i.e:

   +------+         +------+
   |  bp  |         |      |-------> page
   |      | b_pages |      |-------> page
   |      |-------->|      |.............
   |      |         |      |-------> page
   |      |         |      |-------> page
   +------+         +------+

^^^^^^
This array is what we are
freeing unconditionally.

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Reviewed-by
#3 lore raw
Quoted 7 lines · Dave Chinner
Quoted 6 lines · Alex Elder
Quoted 5 lines · Dave Chinner

The code in xfs_free_buf() only attempts to free the b_pages array if the buffer is a page cache backed or page allocated buffer. The extra log buffer that is used when the log wraps uses pages that are allocated to a different log buffer, but it still has a b_pages array allocated when those pages are associated to with the extra buffer in xfs_buf_associate_memory.

I get it now. You're right, I was confusing the array with the pages themselves. And in hindsight the title of your patch and the explanation is abundantly clear on what's being freed... Thank you for carefully explaining it to me (again).

The patch looks good.

-Alex

Quoted 45 lines
Quoted 9 lines
Quoted 3 lines

Hence we need to always attempt to free the b_pages array when tearing down a buffer, not just on buffers that are explicitly marked as page bearing buffers. This fixes a leak detected by the kernel memory leak code.

Three places call xfs_buf_get_pages();
- _xfs_buf_lookup_pages(), which sets the _XBF_PAGE_CACHE flag in the buffer after the call
- xfs_buf_associate_memory(), which sets no flag bit
- xfs_buf_get_noaddr(), which sets the _XBF_PAGES flag.

Quoted 6 lines

The only place that checks for _XBF_PAGES is xfs_buf_free().

Given that, I have two comments:
- You could just as easily have set the _XBF_PAGES flag in xfs_buf_associate_memory, thereby making that flag indicate consistently that the buffer has allocated pages

_XBF_PAGES is used to indicate that the buffer owns the pages and must free them when the buffer is freed.

_XBF_PAGE_CACHE indicates pages are allocated out of the buftarg page cache and need to be released when the buffer is freed.

In both cases, xfs_buf_free() has to free the pages associated with these buffers.

In the case of xfs_buf_associate_memory(), the memory attached to the buffer is owned by an external entity and hence we are not allowed to free it when we free the buffer. Therefore we can't set _XBF_PAGES on the buffer.

Quoted 2 lines

- Or, since you are proposing unconditionally freeing the pages,

This change doesn't unconditionally free the pages attached to the buffer - it unconditionally frees the array used to index the pages on the buffer when the buffer is torn down. i.e:

   +------+         +------+
   |  bp  |         |      |-------> page
   |      | b_pages |      |-------> page
   |      |-------->|      |.............
   |      |         |      |-------> page
   |      |         |      |-------> page
   +------+         +------+
Quoted 7 lines

^^^^^^
This array is what we are
freeing unconditionally.

Cheers,

Dave.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs