btrfs: cleanups and improvements around extent map release
cover btrfs: cleanups and improvements around extent map release
From: Filipe Manana <[email protected]>
These make the folio release path release extent maps more often when they are not needed, as well as some cleanups. More details in the change logs.
Filipe Manana (5):
btrfs: rename some variables at try_release_extent_mapping()
btrfs: use btrfs_get_fs_generation() at try_release_extent_mapping()
btrfs: remove i_size restriction at try_release_extent_mapping()
btrfs: be better releasing extent maps at try_release_extent_mapping()
btrfs: make try_release_extent_mapping() return a bool
fs/btrfs/extent_io.c | 149 +++++++++++++++++++++---------------------- fs/btrfs/extent_io.h | 2 +- fs/btrfs/inode.c | 7 +- 3 files changed, 77 insertions(+), 81 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/5 btrfs: rename some variables at try_release_extent_mapping()
From: Filipe Manana <[email protected]>
Rename the following variables:
1) "btrfs_inode" to "inode", because it's shorter to type and clear, and we don't have a vfs inode here as well, so there's no confusion;
2) "tree" to "io_tree", to be clear which tree we are dealing with, since we use 2 different trees in the function;
3) "map" to "extent_tree" since "map" gives the idea we are dealing with an extent map for example, but we are dealing with the inode's extent tree (the tree which stores extent maps).
These also makes the next patches simpler.
--- fs/btrfs/extent_io.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
fs/btrfs/extent_io.c +12 -12
@@ -2398,9 +2398,9 @@ int try_release_extent_mapping(struct page *page, gfp_t mask)struct extent_map *em;u64 start =page_offset (page);u64 end = start +PAGE_SIZE -1 ; -struct btrfs_inode *btrfs_inode =page_to_inode (page); -struct extent_io_tree *tree = &btrfs_inode->io_tree; -struct extent_map_tree *map = &btrfs_inode->extent_tree; +struct btrfs_inode *inode =page_to_inode (page); +struct extent_io_tree *io_tree = &inode->io_tree; +struct extent_map_tree *extent_tree = &inode->extent_tree;if (gfpflags_allow_blocking (mask) && page->mapping->host->i_size >SZ_16M ) {
@@ -2410,19 +2410,19 @@ int try_release_extent_mapping(struct page *page, gfp_t mask)u64 cur_gen; len = end - start +1 ; -write_lock (&map->lock); - em =lookup_extent_mapping (map, start, len); +write_lock (&extent_tree->lock); + em =lookup_extent_mapping (extent_tree, start, len);if (!em) { -write_unlock (&map->lock); +write_unlock (&extent_tree->lock);break ; }if ((em->flags &EXTENT_FLAG_PINNED ) || em->start != start) { -write_unlock (&map->lock); +write_unlock (&extent_tree->lock);free_extent_map (em);break ; } -if (test_range_bit_exists (tree, em->start, +if (test_range_bit_exists (io_tree, em->start,extent_map_end (em) -1 ,EXTENT_LOCKED )) goto next;
@@ -2442,7 +2442,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask)* Otherwise don't remove it, we could be racing with an * ongoing fast fsync that could miss the new extent. */ - fs_info = btrfs_inode->root->fs_info; + fs_info = inode->root->fs_info;spin_lock (&fs_info->trans_lock); cur_gen = fs_info->generation;spin_unlock (&fs_info->trans_lock);
@@ -2457,12 +2457,12 @@ int try_release_extent_mapping(struct page *page, gfp_t mask)* hurts the fsync performance for workloads with a data * size that exceeds or is close to the system's memory). */ -remove_extent_mapping (btrfs_inode, em); +remove_extent_mapping (inode, em);/* once for the rb tree */ free_extent_map (em); next: start =extent_map_end (em); -write_unlock (&map->lock); +write_unlock (&extent_tree->lock);/* once for us */ free_extent_map (em);
@@ -2470,7 +2470,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask)cond_resched ();/* Allow large-extent preemption. */ } } -return try_release_extent_state (tree, page, mask); +return try_release_extent_state (io_tree, page, mask); }struct btrfs_fiemap_entry {
Signature · 1 line
2.43.0
Re: [PATCH 1/5] btrfs: rename some variables at try_release_extent_mapping()
PATCH 2/5 btrfs: use btrfs_get_fs_generation() at try_release_extent_mapping()
In reply to [email protected]From: Filipe Manana <[email protected]>
Nowadays we have the btrfs_get_fs_generation() to get the current generation of the filesystem, so there's no need anymore to lock the transaction spinlock to read it.
--- fs/btrfs/extent_io.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
fs/btrfs/extent_io.c +1 -6
@@ -2406,8 +2406,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask) page->mapping->host->i_size > SZ_16M) {u64 len;while (start <= end) { -struct btrfs_fs_info *fs_info; -u64 cur_gen; +const u64 cur_gen =btrfs_get_fs_generation (inode->root->fs_info); len = end - start +1 ;write_lock (&extent_tree->lock);
@@ -2442,10 +2441,6 @@ int try_release_extent_mapping(struct page *page, gfp_t mask)* Otherwise don't remove it, we could be racing with an * ongoing fast fsync that could miss the new extent. */ - fs_info = inode->root->fs_info; -spin_lock (&fs_info->trans_lock); - cur_gen = fs_info->generation; -spin_unlock (&fs_info->trans_lock);if (em->generation >= cur_gen) goto next; remove_em:
Signature · 1 line
2.43.0
Re: [PATCH 2/5] btrfs: use btrfs_get_fs_generation() at try_release_extent_mapping()
PATCH 3/5 btrfs: remove i_size restriction at try_release_extent_mapping()
In reply to [email protected]From: Filipe Manana <[email protected]>
Currently we don't attempt to release extent maps if the inode has an i_size that is not greater than 16M. This condition was added way back in 2008 by commit 70dec8079d78 ("Btrfs: extent_io and extent_state optimizations"), without any explanation about it. A quick chat with Chris on slack revealed that the goal was probably to release the extent maps for small files only when closing the inode. This however can be harmful in case we have tons of such files being kept open for very long periods of time, since we will consume more and more pages for extent maps.
So remove the condition.
--- fs/btrfs/extent_io.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/extent_io.c +1 -2
@@ -2402,8 +2402,7 @@ int try_release_extent_mapping(struct page *page, gfp_t mask)struct extent_io_tree *io_tree = &inode->io_tree;struct extent_map_tree *extent_tree = &inode->extent_tree; -if (gfpflags_allow_blocking (mask) && - page->mapping->host->i_size >SZ_16M ) { +if (gfpflags_allow_blocking (mask)) {u64 len;while (start <= end) {const u64 cur_gen =btrfs_get_fs_generation (inode->root->fs_info);
Signature · 1 line
2.43.0
Re: [PATCH 3/5] btrfs: remove i_size restriction at try_release_extent_mapping()
I wonder if we should eventually backport this to stable, as it could mitigate OOM situations w/o the em shrinker?
PATCH 4/5 btrfs: be better releasing extent maps at try_release_extent_mapping()
In reply to [email protected]From: Filipe Manana <[email protected]>
At try_release_extent_mapping(), called during the release folio callback (btrfs_release_folio() callchain), we don't release any extent maps in the range if the gfp flags don't allow blocking. This behaviour is exaggerated because:
1) Both searching for extent maps and removing them are not blocking operations. The only thing that it is the cond_resched() call at the end of the loop that searches for and removes extent maps;
2) We currently only operate on a single page, so for the case where block size matches the page size, we can only have one extent map, and for the case where the block size is smaller than the page size, we can have at most 16 extent maps.
So it's very unlikely the cond_resched() call will ever block even in the block size smaller than page size scenario.
So instead of not removing any extent maps at all in case the gfp glags don't allow blocking, keep removing extent maps while we don't need to reschedule. This makes it safe for the subpage case and for a future where we can process folios with a size larger than a page.
--- fs/btrfs/extent_io.c | 120 ++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 59 deletions(-)
fs/btrfs/extent_io.c +61 -59
@@ -2395,73 +2395,75 @@ static int try_release_extent_state(struct extent_io_tree *tree,*/ int try_release_extent_mapping (struct page *page,gfp_t mask) { -struct extent_map *em;u64 start =page_offset (page);u64 end = start +PAGE_SIZE -1 ;struct btrfs_inode *inode =page_to_inode (page);struct extent_io_tree *io_tree = &inode->io_tree; -struct extent_map_tree *extent_tree = &inode->extent_tree; - -if (gfpflags_allow_blocking (mask)) { -u64 len; -while (start <= end) { -const u64 cur_gen =btrfs_get_fs_generation (inode->root->fs_info); - - len = end - start +1 ; -write_lock (&extent_tree->lock); - em =lookup_extent_mapping (extent_tree, start, len); -if (!em) { -write_unlock (&extent_tree->lock); -break ; - } -if ((em->flags &EXTENT_FLAG_PINNED ) || - em->start != start) { -write_unlock (&extent_tree->lock); -free_extent_map (em); -break ; - } -if (test_range_bit_exists (io_tree, em->start, -extent_map_end (em) -1 , -EXTENT_LOCKED )) - goto next; -/* -* If it's not in the list of modified extents, used -* by a fast fsync, we can remove it. If it's being -* logged we can safely remove it since fsync took an -* extra reference on the em. -*/ -if (list_empty (&em->list) || - (em->flags &EXTENT_FLAG_LOGGING )) - goto remove_em; -/* -* If it's in the list of modified extents, remove it -* only if its generation is older then the current one, -* in which case we don't need it for a fast fsync. -* Otherwise don't remove it, we could be racing with an -* ongoing fast fsync that could miss the new extent. -*/ -if (em->generation >= cur_gen) - goto next; -remove_em: -/* -* We only remove extent maps that are not in the list of -* modified extents or that are in the list but with a -* generation lower then the current generation, so there -* is no need to set the full fsync flag on the inode (it -* hurts the fsync performance for workloads with a data -* size that exceeds or is close to the system's memory). -*/ -remove_extent_mapping (inode, em); -/* once for the rb tree */ + +while (start <= end) { +const u64 cur_gen =btrfs_get_fs_generation (inode->root->fs_info); +const u64 len = end - start +1 ; +struct extent_map_tree *extent_tree = &inode->extent_tree; +struct extent_map *em; + +write_lock (&extent_tree->lock); + em =lookup_extent_mapping (extent_tree, start, len); +if (!em) { +write_unlock (&extent_tree->lock); +break ; + } +if ((em->flags &EXTENT_FLAG_PINNED ) || em->start != start) { +write_unlock (&extent_tree->lock);free_extent_map (em); +break ; + } +if (test_range_bit_exists (io_tree, em->start, +extent_map_end (em) -1 ,EXTENT_LOCKED )) + goto next; +/* +* If it's not in the list of modified extents, used by a fast +* fsync, we can remove it. If it's being logged we can safely +* remove it since fsync took an extra reference on the em. +*/ +if (list_empty (&em->list) || (em->flags &EXTENT_FLAG_LOGGING )) + goto remove_em; +/* +* If it's in the list of modified extents, remove it only if +* its generation is older then the current one, in which case +* we don't need it for a fast fsync. Otherwise don't remove it, +* we could be racing with an ongoing fast fsync that could miss +* the new extent. +*/ +if (em->generation >= cur_gen) + goto next; +remove_em: +/* +* We only remove extent maps that are not in the list of +* modified extents or that are in the list but with a +* generation lower then the current generation, so there is no +* need to set the full fsync flag on the inode (it hurts the +* fsync performance for workloads with a data size that exceeds +* or is close to the system's memory). +*/ +remove_extent_mapping (inode, em); +/* Once for the inode's extent map tree. */ +free_extent_map (em); next: - start =extent_map_end (em); -write_unlock (&extent_tree->lock); + start =extent_map_end (em); +write_unlock (&extent_tree->lock); -/* once for us */ -free_extent_map (em); +/* Once for us, for the lookup_extent_mapping() reference. */ +free_extent_map (em); + +if (need_resched ()) { +/* +* If we need to resched but we can't block just exit +* and leave any remaining extent maps. +*/ +if (!gfpflags_allow_blocking (mask)) +break ; -cond_resched ();/* Allow large-extent preemption. */ +cond_resched (); } }return try_release_extent_state (io_tree, page, mask);
Signature · 1 line
2.43.0
Re: [PATCH 4/5] btrfs: be better releasing extent maps at try_release_extent_mapping()
PATCH 5/5 btrfs: make try_release_extent_mapping() return a bool
In reply to [email protected]From: Filipe Manana <[email protected]>
Currently try_release_extent_mapping() as an int return type, but we use it as a boolean. Its only caller, the release folio callback, also returns a boolean which corresponds to try_release_extent_mapping()'s return value. So change its return value type to bool as well as its helper try_release_extent_state().
--- fs/btrfs/extent_io.c | 17 +++++++++-------- fs/btrfs/extent_io.h | 2 +- fs/btrfs/inode.c | 7 +++---- 3 files changed, 13 insertions(+), 13 deletions(-)
fs/btrfs/extent_io.c +9 -8
@@ -2355,19 +2355,20 @@ int extent_invalidate_folio(struct extent_io_tree *tree,* are locked or under IO and drops the related state bits if it is safe * to drop the page. */ -static int try_release_extent_state (struct extent_io_tree *tree, +static bool try_release_extent_state (struct extent_io_tree *tree,struct page *page,gfp_t mask) {u64 start =page_offset (page);u64 end = start +PAGE_SIZE -1 ; -int ret =1 ; +bool ret;if (test_range_bit_exists (tree, start, end,EXTENT_LOCKED )) { - ret =0 ; + ret = false; }else {u32 clear_bits = ~(EXTENT_LOCKED |EXTENT_NODATASUM |EXTENT_DELALLOC_NEW |EXTENT_CTLBITS |EXTENT_QGROUP_RESERVED ); +int ret2;/* * At this point we can safely clear everything except the
@@ -2375,15 +2376,15 @@ static int try_release_extent_state(struct extent_io_tree *tree,* The delalloc new bit will be cleared by ordered extent * completion. */ - ret =__clear_extent_bit (tree, start, end, clear_bits,NULL ,NULL ); + ret2 =__clear_extent_bit (tree, start, end, clear_bits,NULL ,NULL );/* if clear_extent_bit failed for enomem reasons, * we can't allow the release to continue. */ -if (ret <0 ) - ret =0 ; +if (ret2 <0 ) + ret = false;else - ret =1 ; + ret = true; }return ret; }
@@ -2393,7 +2394,7 @@ static int try_release_extent_state(struct extent_io_tree *tree,* in the range corresponding to the page, both state records and extent * map records are removed */ -int try_release_extent_mapping (struct page *page,gfp_t mask) +bool try_release_extent_mapping (struct page *page,gfp_t mask) {u64 start =page_offset (page);u64 end = start +PAGE_SIZE -1 ;
fs/btrfs/extent_io.h +1 -1
@@ -230,7 +230,7 @@ static inline void extent_changeset_free(struct extent_changeset *changeset)kfree (changeset); } -int try_release_extent_mapping (struct page *page,gfp_t mask); +bool try_release_extent_mapping (struct page *page,gfp_t mask);int try_release_extent_buffer (struct page *page);int btrfs_read_folio (struct file *file,struct folio *folio);
fs/btrfs/inode.c +3 -4
@@ -7902,13 +7902,12 @@ static void wait_subpage_spinlock(struct page *page)static bool __btrfs_release_folio (struct folio *folio,gfp_t gfp_flags) { -int ret =try_release_extent_mapping (&folio->page, gfp_flags); - -if (ret ==1 ) { +if (try_release_extent_mapping (&folio->page, gfp_flags)) {wait_subpage_spinlock (&folio->page);clear_page_extent_mapped (&folio->page); +return true; } -return ret; +return false; }static bool btrfs_release_folio (struct folio *folio,gfp_t gfp_flags)
Signature · 1 line
2.43.0
Re: [PATCH 5/5] btrfs: make try_release_extent_mapping() return a bool
Quoted 11 lines · On Wed, Apr 17, 2024 at 12:03:30PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
These make the folio release path release extent maps more often when they are not needed, as well as some cleanups. More details in the change logs.
Filipe Manana (5):
btrfs: rename some variables attry_release_extent_mapping()
btrfs: usebtrfs_get_fs_generation()attry_release_extent_mapping()
btrfs: removei_sizerestriction attry_release_extent_mapping()
btrfs: be better releasing extent maps attry_release_extent_mapping()
btrfs: maketry_release_extent_mapping()return a bool