Skip to content

Commit 1db7959

Browse files
adam900710kdave
authored andcommitted
btrfs: do not wait for short bulk allocation
[BUG] There is a recent report that when memory pressure is high (including cached pages), btrfs can spend most of its time on memory allocation in btrfs_alloc_page_array() for compressed read/write. [CAUSE] For btrfs_alloc_page_array() we always go alloc_pages_bulk_array(), and even if the bulk allocation failed (fell back to single page allocation) we still retry but with extra memalloc_retry_wait(). If the bulk alloc only returned one page a time, we would spend a lot of time on the retry wait. The behavior was introduced in commit 395cb57 ("btrfs: wait between incomplete batch memory allocations"). [FIX] Although the commit mentioned that other filesystems do the wait, it's not the case at least nowadays. All the mainlined filesystems only call memalloc_retry_wait() if they failed to allocate any page (not only for bulk allocation). If there is any progress, they won't call memalloc_retry_wait() at all. For example, xfs_buf_alloc_pages() would only call memalloc_retry_wait() if there is no allocation progress at all, and the call is not for metadata readahead. So I don't believe we should call memalloc_retry_wait() unconditionally for short allocation. Call memalloc_retry_wait() if it fails to allocate any page for tree block allocation (which goes with __GFP_NOFAIL and may not need the special handling anyway), and reduce the latency for btrfs_alloc_page_array(). Reported-by: Julian Taylor <julian.taylor@1und1.de> Tested-by: Julian Taylor <julian.taylor@1und1.de> Link: https://lore.kernel.org/all/8966c095-cbe7-4d22-9784-a647d1bf27c3@1und1.de/ Fixes: 395cb57 ("btrfs: wait between incomplete batch memory allocations") CC: stable@vger.kernel.org # 6.1+ Reviewed-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me> Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Qu Wenruo <wqu@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 073bda7 commit 1db7959

1 file changed

Lines changed: 4 additions & 14 deletions

File tree

fs/btrfs/extent_io.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -681,31 +681,21 @@ static void end_bbio_data_read(struct btrfs_bio *bbio)
681681
int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array,
682682
gfp_t extra_gfp)
683683
{
684+
const gfp_t gfp = GFP_NOFS | extra_gfp;
684685
unsigned int allocated;
685686

686687
for (allocated = 0; allocated < nr_pages;) {
687688
unsigned int last = allocated;
688689

689-
allocated = alloc_pages_bulk_array(GFP_NOFS | extra_gfp,
690-
nr_pages, page_array);
691-
692-
if (allocated == nr_pages)
693-
return 0;
694-
695-
/*
696-
* During this iteration, no page could be allocated, even
697-
* though alloc_pages_bulk_array() falls back to alloc_page()
698-
* if it could not bulk-allocate. So we must be out of memory.
699-
*/
700-
if (allocated == last) {
690+
allocated = alloc_pages_bulk_array(gfp, nr_pages, page_array);
691+
if (unlikely(allocated == last)) {
692+
/* No progress, fail and do cleanup. */
701693
for (int i = 0; i < allocated; i++) {
702694
__free_page(page_array[i]);
703695
page_array[i] = NULL;
704696
}
705697
return -ENOMEM;
706698
}
707-
708-
memalloc_retry_wait(GFP_NOFS);
709699
}
710700
return 0;
711701
}

0 commit comments

Comments
 (0)