Rework eviction space flushing
Subsystems (2)
cover Rework eviction space flushing
This is a set of patches to address how we do space flushing for inode evictions. Historically we've only been allowed to do a few things to reclaim space for inode evictions, mostly because we'd deadlock with iput. But we have delayed iputs in place to make sure we're always doing iput where it's completely safe to do an iput.
However we do run iputs for flushing, so we can't just do FLUSH_ALL, otherwise we could deadlock. Also we still want to prioritize evictions for space reclamation because we likely will free up space for other people to make reservations.
The first 4 patches are preparation patches, just refactoring so we can add this new flushing time for eviction. This allows us to clean up our current ad-hoc loop we have for reclaiming space for evictions and use the common helpers that everybody else uses. Thanks,
Josef
PATCH 1/5 btrfs: add a flush step for delayed iputs
Delayed iputs could very well free up enough space without needing to commit the transaction, so make this step it's own step. This will allow us to skip the step for evictions in a later patch.
--- fs/btrfs/ctree.h | 3 ++- fs/btrfs/space-info.c | 5 +++-- include/trace/events/btrfs.h | 1 + 3 files changed, 6 insertions(+), 3 deletions(-)
fs/btrfs/ctree.h +2 -1
@@ -2733,7 +2733,8 @@ enum btrfs_flush_state {FLUSH_DELALLOC_WAIT =6 ,ALLOC_CHUNK =7 ,ALLOC_CHUNK_FORCE =8 , - COMMIT_TRANS = 9, +RUN_DELAYED_IPUTS =9 , + COMMIT_TRANS = 10, };/*
fs/btrfs/space-info.c +3 -2
@@ -684,7 +684,7 @@ static void flush_space(struct btrfs_fs_info *fs_info,if (ret >0 || ret == -ENOSPC ) ret =0 ;break ; -case COMMIT_TRANS : +case RUN_DELAYED_IPUTS :/* * If we have pending delayed iputs then we could free up a * bunch of pinned space, so make sure we run the iputs before
@@ -692,7 +692,8 @@ static void flush_space(struct btrfs_fs_info *fs_info,*/ btrfs_run_delayed_iputs (fs_info);btrfs_wait_on_delayed_iputs (fs_info); - +break ; +case COMMIT_TRANS : ret =may_commit_transaction (fs_info, space_info);break ;default :
include/trace/events/btrfs.h +1 -0
@@ -1088,6 +1088,7 @@ TRACE_EVENT(btrfs_trigger_flush, { FLUSH_DELAYED_REFS, "FLUSH_ELAYED_REFS"}, \ { ALLOC_CHUNK, "ALLOC_CHUNK"}, \ { ALLOC_CHUNK_FORCE, "ALLOC_CHUNK_FORCE"}, \ + { RUN_DELAYED_IPUTS, "RUN_DELAYED_IPUTS"}, \ { COMMIT_TRANS, "COMMIT_TRANS"}) TRACE_EVENT(btrfs_flush_space,
Signature · 1 line
2.21.0
PATCH 2/5 btrfs: unify error handling for ticket flushing
In reply to Josef BacikCurrently we handle the cleanup of errored out tickets in both the priority flush path and the normal flushing path. This is the same code in both places, so just refactor so we don't duplicate the cleanup work.
--- fs/btrfs/space-info.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-)
fs/btrfs/space-info.c +11 -21
@@ -898,20 +898,19 @@ static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, }while (flush_state <ARRAY_SIZE (priority_flush_states)); } -static int wait_reserve_ticket (struct btrfs_fs_info *fs_info, -struct btrfs_space_info *space_info, -struct reserve_ticket *ticket) +static void wait_reserve_ticket (struct btrfs_fs_info *fs_info, +struct btrfs_space_info *space_info, +struct reserve_ticket *ticket) {DEFINE_WAIT (wait); -u64 reclaim_bytes =0 ;int ret =0 ;spin_lock (&space_info->lock);while (ticket->bytes >0 && ticket->error ==0 ) { ret =prepare_to_wait_event (&ticket->wait, &wait,TASK_KILLABLE );if (ret) { - ret = -EINTR ; + ticket->error = -EINTR ;break ; }spin_unlock (&space_info->lock);
@@ -921,18 +920,7 @@ static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,finish_wait (&ticket->wait, &wait);spin_lock (&space_info->lock); } -if (!ret) - ret = ticket->error; -if (!list_empty (&ticket->list)) -list_del_init (&ticket->list); -if (ticket->bytes && ticket->bytes < ticket->orig_bytes) - reclaim_bytes = ticket->orig_bytes - ticket->bytes;spin_unlock (&space_info->lock); - -if (reclaim_bytes) -btrfs_space_info_add_old_bytes (fs_info, space_info, - reclaim_bytes); -return ret; }/**
@@ -1030,16 +1018,18 @@ static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,return ret;if (flush ==BTRFS_RESERVE_FLUSH_ALL ) -return wait_reserve_ticket (fs_info, space_info, &ticket); +wait_reserve_ticket (fs_info, space_info, &ticket); +else +priority_reclaim_metadata_space (fs_info, space_info, &ticket); - ret =0 ; -priority_reclaim_metadata_space (fs_info, space_info, &ticket);spin_lock (&space_info->lock); -if (ticket.bytes) { + ret = ticket.error; +if (ticket.bytes || ticket.error) {if (ticket.bytes < orig_bytes) reclaim_bytes = orig_bytes - ticket.bytes;list_del_init (&ticket.list); - ret = -ENOSPC ; +if (!ret) + ret = -ENOSPC ; }spin_unlock (&space_info->lock);
Signature · 1 line
2.21.0
PATCH 3/5 btrfs: factor out the ticket flush handling
In reply to Josef BacikWe're going to make this logic a little more complicated for evict, so factor the ticket flushing/waiting code out of __reserve_metadata_bytes. This has no functional change.
--- fs/btrfs/space-info.c | 64 ++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 22 deletions(-)
fs/btrfs/space-info.c +42 -22
@@ -923,6 +923,47 @@ static void wait_reserve_ticket(struct btrfs_fs_info *fs_info,spin_unlock (&space_info->lock); } +/** +* handle_reserve_ticket - do the appropriate flushing and waiting for a ticket +* @fs_info - the fs_info for the fs. +* @space_info - the space_info for the reservation. +* @ticket - the ticket for the reservation. +* @flush - how much we can flush. +* +* This does the work of figuring out how to flush for the ticket, waiting for +* the reservation, and returning the appropriate error if there is one. +*/ +static int handle_reserve_ticket (struct btrfs_fs_info *fs_info, +struct btrfs_space_info *space_info, +struct reserve_ticket *ticket, +enum btrfs_reserve_flush_enum flush) +{ +u64 reclaim_bytes =0 ; +int ret; + +if (flush ==BTRFS_RESERVE_FLUSH_ALL ) +wait_reserve_ticket (fs_info, space_info, ticket); +else +priority_reclaim_metadata_space (fs_info, space_info, ticket); + +spin_lock (&space_info->lock); + ret = ticket->error; +if (ticket->bytes || ticket->error) { +if (ticket->bytes < ticket->orig_bytes) + reclaim_bytes = ticket->orig_bytes - ticket->bytes; +list_del_init (&ticket->list); +if (!ret) + ret = -ENOSPC ; + } +spin_unlock (&space_info->lock); + +if (reclaim_bytes) +btrfs_space_info_add_old_bytes (fs_info, space_info, + reclaim_bytes); +ASSERT (list_empty (&ticket->list)); +return ret; +} +/** * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space * @root - the root we're allocating for
@@ -945,7 +986,6 @@ static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info, {struct reserve_ticket ticket;u64 used; -u64 reclaim_bytes =0 ;int ret =0 ;ASSERT (orig_bytes);
@@ -1017,27 +1057,7 @@ static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,if (!ret || flush ==BTRFS_RESERVE_NO_FLUSH )return ret; -if (flush ==BTRFS_RESERVE_FLUSH_ALL ) -wait_reserve_ticket (fs_info, space_info, &ticket); -else -priority_reclaim_metadata_space (fs_info, space_info, &ticket); - -spin_lock (&space_info->lock); - ret = ticket.error; -if (ticket.bytes || ticket.error) { -if (ticket.bytes < orig_bytes) - reclaim_bytes = orig_bytes - ticket.bytes; -list_del_init (&ticket.list); -if (!ret) - ret = -ENOSPC ; - } -spin_unlock (&space_info->lock); - -if (reclaim_bytes) -btrfs_space_info_add_old_bytes (fs_info, space_info, - reclaim_bytes); -ASSERT (list_empty (&ticket.list)); -return ret; +return handle_reserve_ticket (fs_info, space_info, &ticket, flush); }/**
Signature · 1 line
2.21.0
PATCH 4/5 btrfs: refactor priority_reclaim_metadata_space
In reply to Josef BacikWith the eviction flushing stuff we'll want to allow for different states, but still work basically the same way that priority_reclaim_metadata_space works currently. Refactor this to take the flushing states and size as an argument so we can use the same logic for limit flushing and eviction flushing.
--- fs/btrfs/space-info.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
fs/btrfs/space-info.c +11 -6
@@ -868,9 +868,12 @@ static const enum btrfs_flush_state priority_flush_states[] = { ALLOC_CHUNK, }; -static void priority_reclaim_metadata_space (struct btrfs_fs_info *fs_info, -struct btrfs_space_info *space_info, -struct reserve_ticket *ticket) +static void +priority_reclaim_metadata_space (struct btrfs_fs_info *fs_info, +struct btrfs_space_info *space_info, +struct reserve_ticket *ticket, +const enum btrfs_flush_state *states, +int states_nr) {u64 to_reclaim;int flush_state;
@@ -887,7 +890,7 @@ static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, flush_state =0 ; do { flush_space(fs_info, space_info, to_reclaim, - priority_flush_states[flush_state]); + states[flush_state]); flush_state++; spin_lock(&space_info->lock); if (ticket->bytes == 0) {
@@ -895,7 +898,7 @@ static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,return ; }spin_unlock (&space_info->lock); - }while (flush_state <ARRAY_SIZE (priority_flush_states)); + }while (flush_state < states_nr); } static void wait_reserve_ticket(struct btrfs_fs_info *fs_info,
@@ -944,7 +947,9 @@ static int handle_reserve_ticket(struct btrfs_fs_info *fs_info,if (flush ==BTRFS_RESERVE_FLUSH_ALL )wait_reserve_ticket (fs_info, space_info, ticket);else -priority_reclaim_metadata_space (fs_info, space_info, ticket); +priority_reclaim_metadata_space (fs_info, space_info, ticket, + priority_flush_states, +ARRAY_SIZE (priority_flush_states));spin_lock (&space_info->lock); ret = ticket->error;
Signature · 1 line
2.21.0
Re: [PATCH 4/5] btrfs: refactor priority_reclaim_metadata_space
Quoted 16 lines · Josef Bacik
With the eviction flushing stuff we'll want to allow for different states, but still work basically the same way that
priority_reclaim_metadata_spaceworks currently. Refactor this to take the flushing states and size as an argument so we can use the same logic for limit flushing and eviction flushing.Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/space-info.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)fs/btrfs/space-info.c +0
-0
fs/btrfs/space-info.c static const enum btrfs_flush_state priority_flush_states[] = { · line 872
Quoted 8 lines
fs/btrfs/space-info.c +2
-3@@ -868,9 +868,12 @@ static const enum btrfs_flush_state priority_flush_states[] = { ALLOC_CHUNK, }; -static void priority_reclaim_metadata_space (struct btrfs_fs_info *fs_info, -struct btrfs_space_info *space_info, -struct reserve_ticket *ticket) +static void +priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
Please note that the split type and function name is the reverse of the coding style used in btrfs, I fix such things but please don't introduce it in new patches namely when the original code is using the correct style.
PATCH 5/5 btrfs: introduce an evict flushing state
In reply to Josef BacikWe have this weird space flushing loop inside inode.c for evict where we'll do the normal LIMIT flush, and then commit the transaction and hope we get our space. This is super janky, and in fact there's really nothing stopping us from using FLUSH_ALL except that we run delayed iputs, which means we could deadlock. So introduce a new flush state for eviction that does the normal priority flushing with all of the states that are safe for eviction.
The nice side-effect of this is that we'll try harder for evictions. Previously if (for example generic/269) you had a bunch of other operations happening on the fs you could race with those reservations when committing the transaction, and eventually miss getting a reservation for the evict. With this code we'll have our ticket in place through the transaction commit, so any pinned bytes will go to our pending evictions first.
--- fs/btrfs/ctree.h | 1 + fs/btrfs/inode.c | 81 +++++++++++++++++++------------------------ fs/btrfs/space-info.c | 27 +++++++++++++-- 3 files changed, 62 insertions(+), 47 deletions(-)
fs/btrfs/ctree.h +1 -0
@@ -2721,6 +2721,7 @@ enum btrfs_reserve_flush_enum {* case, use FLUSH LIMIT */ BTRFS_RESERVE_FLUSH_LIMIT , +BTRFS_RESERVE_FLUSH_EVICT , BTRFS_RESERVE_FLUSH_ALL, };
fs/btrfs/inode.c +36 -45
95 lines not on this page · raw message
fs/btrfs/space-info.c +25 -2
@@ -868,6 +868,17 @@ static const enum btrfs_flush_state priority_flush_states[] = { ALLOC_CHUNK, }; +static const enum btrfs_flush_state evict_flush_states[] = { +FLUSH_DELAYED_ITEMS_NR , +FLUSH_DELAYED_ITEMS , +FLUSH_DELAYED_REFS_NR , +FLUSH_DELAYED_REFS , +FLUSH_DELALLOC , +FLUSH_DELALLOC_WAIT , +ALLOC_CHUNK , +COMMIT_TRANS , +}; + static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, struct btrfs_space_info *space_info,
@@ -944,12 +955,24 @@ static int handle_reserve_ticket(struct btrfs_fs_info *fs_info,u64 reclaim_bytes =0 ;int ret; -if (flush ==BTRFS_RESERVE_FLUSH_ALL ) +switch (flush) { +case BTRFS_RESERVE_FLUSH_ALL :wait_reserve_ticket (fs_info, space_info, ticket); -else +break ; +case BTRFS_RESERVE_FLUSH_LIMIT :priority_reclaim_metadata_space (fs_info, space_info, ticket, priority_flush_states,ARRAY_SIZE (priority_flush_states)); +break ; +case BTRFS_RESERVE_FLUSH_EVICT : +priority_reclaim_metadata_space (fs_info, space_info, ticket, + evict_flush_states, +ARRAY_SIZE (evict_flush_states)); +break ; +default : +ASSERT (0 ); +break ; + }spin_lock (&space_info->lock); ret = ticket->error;
Signature · 1 line
2.21.0
Quoted 11 lines · Josef Bacik
This is a set of patches to address how we do space flushing for inode evictions. Historically we've only been allowed to do a few things to reclaim space for inode evictions, mostly because we'd deadlock with iput. But we have delayed iputs in place to make sure we're always doing iput where it's completely safe to do an iput.
However we do run iputs for flushing, so we can't just do
FLUSH_ALL, otherwise we could deadlock. Also we still want to prioritize evictions for space reclamation because we likely will free up space for other people to make reservations.
Quoted 4 lines
The first 4 patches are preparation patches, just refactoring so we can add this new flushing time for eviction. This allows us to clean up our current ad-hoc loop we have for reclaiming space for evictions and use the common helpers that everybody else uses. Thanks,
I'll add this as a topic branch to for-next for testing, I have only skimmed the patches and did not do any kind of review.
Quoted 11 lines · Josef Bacik
This is a set of patches to address how we do space flushing for inode evictions. Historically we've only been allowed to do a few things to reclaim space for inode evictions, mostly because we'd deadlock with iput. But we have delayed iputs in place to make sure we're always doing iput where it's completely safe to do an iput.
However we do run iputs for flushing, so we can't just do
FLUSH_ALL, otherwise we could deadlock. Also we still want to prioritize evictions for space reclamation because we likely will free up space for other people to make reservations.
Quoted 4 lines
The first 4 patches are preparation patches, just refactoring so we can add this new flushing time for eviction. This allows us to clean up our current ad-hoc loop we have for reclaiming space for evictions and use the common helpers that everybody else uses. Thanks,
1-5 added to misc-next, thanks.