ext4: get discard out of jbd2 commit kthread
PATCH 1/1 ext4: get discard out of jbd2 commit kthread
Right now, discard is issued and waited to be completed in jbd2 commit kthread context after the logs are committed. When large amount of files are deleted and discard is flooding, jbd2 commit kthread can be blocked for long time. Then all of the metadata operations can be blocked to wait the log space.
One case is the page fault path with read mm->mmap_sem held, which wants to update the file time but has to wait for the log space. When other threads in the task wants to do mmap, then write mmap_sem is blocked. Finally all of the following read mmap_sem requirements are blocked, even the ps command which need to read the /proc/pid/ -cmdline. Our monitor service which needs to read /proc/pid/cmdline used to be blocked for 5 mins.
This patch moves the discard out of jbd2 kthread context and do it in kworker. And drain the kwork when cannot get space in mb buddy. This is done out of jbd2 handle and won't block the commit process. After that, we could use blk-wbt or other method to throttle the discard and needn't to worry it block the jbd2 commit kthread any more.
--- fs/ext4/balloc.c | 1 + fs/ext4/ext4.h | 4 ++ fs/ext4/mballoc.c | 130 ++++++++++++++++++++++++++++++++++++++++++------------ fs/ext4/mballoc.h | 5 +++ fs/ext4/super.c | 3 ++ 5 files changed, 116 insertions(+), 27 deletions(-)
fs/ext4/balloc.c +1 -0
@@ -661,6 +661,7 @@ int ext4_should_retry_alloc(struct super_block *sb, int *retries)*/ jbd_debug (1 ,"%s: retrying operation after ENOSPC\n" , sb->s_id); (void )jbd2_journal_force_commit_nested (sbi->s_journal); +flush_work (&sbi->s_afd_work);return 1 ; }
fs/ext4/ext4.h +4 -0
@@ -1531,6 +1531,10 @@ struct ext4_sb_info {struct list_head *s_mb_largest_free_orders;rwlock_t *s_mb_largest_free_orders_locks; +struct list_head s_afd_list; +spinlock_t s_afd_lock; +struct work_struct s_afd_work; +/* tunables */ unsigned long s_stripe;unsigned int s_mb_max_linear_groups;
fs/ext4/mballoc.c +103 -27
176 lines not on this page · raw message
fs/ext4/mballoc.h +5 -0
@@ -212,6 +212,11 @@ struct ext4_buddy {ext4_group_t bd_group; }; +struct ext4_async_free_data { +struct list_head list_node; +struct list_head free_list; +}; +static inline ext4_fsblk_t ext4_grp_offs_to_block (struct super_block *sb,struct ext4_free_extent *fex) {
fs/ext4/super.c +3 -0
@@ -5752,6 +5752,9 @@ static int ext4_sync_fs(struct super_block *sb, int wait) ret = err; } +if (test_opt (sb,DISCARD )) +flush_work (&sbi->s_afd_work); +return ret; }
Signature · 1 line
1.8.3.1
Theodore Y. Ts'o
maintainerQuoted 14 lines · Wang Jianchao
Right now, discard is issued and waited to be completed in jbd2 commit kthread context after the logs are committed. When large amount of files are deleted and discard is flooding, jbd2 commit kthread can be blocked for long time. Then all of the metadata operations can be blocked to wait the log space.
One case is the page fault path with read
mm->mmap_semheld, which wants to update the file time but has to wait for the log space. When other threads in the task wants to do mmap, then writemmap_semis blocked. Finally all of the following readmmap_semrequirements are blocked, even the ps command which need to read the/proc/pid/-cmdline. Our monitor service which needs to read/proc/pid/cmdlineused to be blocked for 5 mins.
Quoted 6 lines
This patch moves the discard out of jbd2 kthread context and do it in kworker. And drain the kwork when cannot get space in mb buddy. This is done out of jbd2 handle and won't block the commit process. After that, we could use blk-wbt or other method to throttle the discard and needn't to worry it block the jbd2 commit kthread any more.
Wouldn't be much simpler to do something like this?
if (discard_bio) {
- submit_bio_wait(discard_bio);
- bio_put(discard_bio);
+ submit_bio(discard_bio);
}We're throwing away the return value from submit_bio_wait(), so there's no real need to wait for I/O to complete so we can fetch the I/O status.
That way we don't need to move all of this to a kworker context.
- Ted
Quoted 31 lines · Theodore Y. Ts'o
Quoted 14 lines · Wang Jianchao
Right now, discard is issued and waited to be completed in jbd2 commit kthread context after the logs are committed. When large amount of files are deleted and discard is flooding, jbd2 commit kthread can be blocked for long time. Then all of the metadata operations can be blocked to wait the log space.
One case is the page fault path with read
mm->mmap_semheld, which wants to update the file time but has to wait for the log space. When other threads in the task wants to do mmap, then writemmap_semis blocked. Finally all of the following readmmap_semrequirements are blocked, even the ps command which need to read the/proc/pid/-cmdline. Our monitor service which needs to read/proc/pid/cmdlineused to be blocked for 5 mins.Quoted 6 lines
This patch moves the discard out of jbd2 kthread context and do it in kworker. And drain the kwork when cannot get space in mb buddy. This is done out of jbd2 handle and won't block the commit process. After that, we could use blk-wbt or other method to throttle the discard and needn't to worry it block the jbd2 commit kthread any more.
Wouldn't be much simpler to do something like this?
if (discard_bio) {fs/ext4/mballoc.c +1
-2-submit_bio_wait (discard_bio); -bio_put (discard_bio); +submit_bio (discard_bio);}
Quoted 5 lines
We're throwing away the return value from
submit_bio_wait(), so there's no real need to wait for I/O to complete so we can fetch the I/O status.That way we don't need to move all of this to a kworker context.
The submit_bio also needs to be out of jbd2 commit kthread as it may be blocked due to blk-wbt or no enough request tag. ;)
Best Regards
Jianchao
Quoted 3 lines
- Ted
Theodore Y. Ts'o
maintainerQuoted 4 lines · Wang Jianchao
Quoted 1 line
That way we don't need to move all of this to a kworker context.
The
submit_bioalso needs to be out of jbd2 commit kthread as it may be blocked due to blk-wbt or no enough request tag. ;)
Actually, there's a bigger deal that I hadn't realized, about why we is why are currently using submit_bio_wait(). We *must* wait until discard has completed before we call ext4_free_data_in_buddy(), which is what allows those blocks to be reused by the block allocator.
If the discard happens after we reallocate the block, there is a good chance that we will end up corrupting a data or metadata block, leading to user data loss.
There's another corollary to this; if you use blk-wbt, and you are doing lots of deletes, and we move this all to a writeback thread, this *significantly* increases the chance that the user will see ENOSPC errors in the case where they are with a very full (close to 100% used) file system.
I'd argue that this is a *really* good reason why using mount -o discard is Just A Bad Idea if you are running with blk-wbt. If discards are slow, using fstrim is a much better choice. It's also the case that for most SSD's and workloads, doing frequent discards doesn't actually help that much. The write endurance of the device is not compromised that much if you only run fs-trim and discard unused blocks once a day, or even once a week --- I only recommend use of mount -o discard in cases where the discard operation is effectively free. (e.g., in cases where the FTL is implemented on the Host OS, or you are running with super-fast flash which is PCIe or NVMe attached.)
Cheers,
- Ted
Quoted 6 lines · Theodore Y. Ts'o
Quoted 4 lines · Wang Jianchao
Quoted 1 line
That way we don't need to move all of this to a kworker context.
The
submit_bioalso needs to be out of jbd2 commit kthread as it may be blocked due to blk-wbt or no enough request tag. ;)
Quoted 8 lines
Actually, there's a bigger deal that I hadn't realized, about why we is why are currently using
submit_bio_wait(). We *must* wait until discard has completed before we callext4_free_data_in_buddy(), which is what allows those blocks to be reused by the block allocator.If the discard happens after we reallocate the block, there is a good chance that we will end up corrupting a data or metadata block, leading to user data loss.
Yes
Quoted 6 lines
There's another corollary to this; if you use blk-wbt, and you are doing lots of deletes, and we move this all to a writeback thread, this *significantly* increases the chance that the user will see ENOSPC errors in the case where they are with a very full (close to 100% used) file system.
We would flush the kwork that's doing discard in this patch. That's done in ext4_should_retry_alloc()
Quoted 11 lines
I'd argue that this is a *really* good reason why using mount -o discard is Just A Bad Idea if you are running with blk-wbt. If discards are slow, using fstrim is a much better choice. It's also the case that for most SSD's and workloads, doing frequent discards doesn't actually help that much. The write endurance of the device is not compromised that much if you only run fs-trim and discard unused blocks once a day, or even once a week --- I only recommend use of mount -o discard in cases where the discard operation is effectively free. (e.g., in cases where the FTL is implemented on the Host OS, or you are running with super-fast flash which is PCIe or NVMe attached.)
We're running ext4 with discard on a nbd device whose backend is storage cluster. The discard can help to free the unused space to storage pool.
And sometimes application delete a lot of data and discard is flooding. Then we see the jbd2 commit kthread is blocked for a long time. Even move the discard out of jbd2, we still see the write IO of jbd2 log could be blocked. blk-wbt could help to relieve this. Finally the delay is shift to allocation path. But this is better than blocking the page fault path which holds the read mm->mmap_sem.
Best regards
Jianchao
Quoted 5 lines
Cheers,
- Ted
Theodore Y. Ts'o
maintainerQuoted 10 lines · Wang Jianchao
We're running ext4 with discard on a nbd device whose backend is storage cluster. The discard can help to free the unused space to storage pool.
And sometimes application delete a lot of data and discard is flooding. Then we see the jbd2 commit kthread is blocked for a long time. Even move the discard out of jbd2, we still see the write IO of jbd2 log could be blocked. blk-wbt could help to relieve this. Finally the delay is shift to allocation path. But this is better than blocking the page fault path which holds the read
mm->mmap_sem.
I'm assuming that the problem is when the application deletes a lot of data, the discard flood is causing performance problems on your nbd server. Is that the high level problem that you are trying to solve?
So if that's the case, I'd suggest a different approach. First, move kmem_cache_free(ext4_free_data_cachep, entry) out of ext4_free_data_in_buddy() to its caller, ext4_process_data. Then if discard is enabled, after calling ext4_free_data_in_buddy(), the ext4_free_data struct will be detached from rbtree rooted in ext4_group_info.bb_free_root, and then we can attach it to a new rbtree rooted in ext4_group_info.bb_discard_root.
This allows the block to be reused as soon the commit is finished (allowing for potentially more efficient block allocations), but we can now keep track of which blocks would be useful for discarding and decouple that from when we release the blocks to be reused. We can now use the pre-existing fstrim kernel thread infrastructure to lock a block group, and we can now iterate over the rbtree, and take into account which blocks have since become allocated --- since if a block has been allocated, there's no need to send a discard for it.
I think this will be more efficient, and will allow us to share more of the code for fstrim and the discard-at-runtime model used by "mount -o discard". We can also fine-tune how quickly we issue discards; it might be that if user has executed "rm -rf" it might actually better to wait until the deletes have completed, even if it takes several commit intervals, since it might allow us to combine discards if the blocks 100-199 and 400-500 are released in one commit, and blocks 200-399 are released two or three commits later.
Something else I'd urge you to consider is whether it's possible to enhance the nbd protocol to add some kind of back-channel notification when the shared storage is getting low on space. In that case, when the nbd client code a request from the nbd server indicating, "please issue discards if possible", it could either trigger an upcall to userspace, which could then issue the fstrim ioctl, which in the case where "mount -o discard" is enabled, would accelerate when discards took place.
We could then make the fstrim thread normally work on a much slower pace, but when there is a signal from the shared storage that space is needed, clients could accelerate when they issue discards to free up shared space.
Cheers,
- Ted
P.S. One other potential thought; if we have established a new bb_discard_root rbtree, it *might* actually be beneficial to consider using that information in the block allocator. One of the best way to tell an SSD that block is no longer needed is to simply overwrite that block. If we do that, we don't need to send a discard to that block any more.
Of course, we still want to keep blocks contiguous since even though seeks are free for SSD's, we want to keep large reads contiguous as much as possible, and we want to keep the extent tree as compact as possible. But if we have just released a 12k file, and we are writing a new 12k file, and don't really care *where* in the block group we are writing that file, reusing blocks that had just been freed might actually be a good strategy.
That's not something you need to implement in this patch series, but it might be an interesting optimization.
Quoted 12 lines · Theodore Y. Ts'o
Quoted 10 lines · Wang Jianchao
We're running ext4 with discard on a nbd device whose backend is storage cluster. The discard can help to free the unused space to storage pool.
And sometimes application delete a lot of data and discard is flooding. Then we see the jbd2 commit kthread is blocked for a long time. Even move the discard out of jbd2, we still see the write IO of jbd2 log could be blocked. blk-wbt could help to relieve this. Finally the delay is shift to allocation path. But this is better than blocking the page fault path which holds the read
mm->mmap_sem.
Quoted 4 lines
I'm assuming that the problem is when the application deletes a lot of data, the discard flood is causing performance problems on your nbd server. Is that the high level problem that you are trying to solve?
Yes, not only the discard sometimes could be very slow, but also it would degrade the performance of normal write IO
Quoted 17 lines
So if that's the case, I'd suggest a different approach. First, move
kmem_cache_free(ext4_free_data_cachep, entry)out ofext4_free_data_in_buddy()to its caller,ext4_process_data. Then if discard is enabled, after callingext4_free_data_in_buddy(), theext4_free_datastruct will be detached from rbtree rooted in ext4_group_info.bb_free_root, and then we can attach it to a new rbtree rooted in ext4_group_info.bb_discard_root.This allows the block to be reused as soon the commit is finished (allowing for potentially more efficient block allocations), but we can now keep track of which blocks would be useful for discarding and decouple that from when we release the blocks to be reused. We can now use the pre-existing fstrim kernel thread infrastructure to lock a block group, and we can now iterate over the rbtree, and take into account which blocks have since become allocated --- since if a block has been allocated, there's no need to send a discard for it.
Quoted 8 lines
I think this will be more efficient, and will allow us to share more of the code for fstrim and the discard-at-runtime model used by "mount -o discard". We can also fine-tune how quickly we issue discards; it might be that if user has executed "rm -rf" it might actually better to wait until the deletes have completed, even if it takes several commit intervals, since it might allow us to combine discards if the blocks 100-199 and 400-500 are released in one commit, and blocks 200-399 are released two or three commits later.
Yes, this is more efficient and fair. I will cook the next version path based on the suggestion above.
Quoted 10 lines
Something else I'd urge you to consider is whether it's possible to enhance the nbd protocol to add some kind of back-channel notification when the shared storage is getting low on space. In that case, when the nbd client code a request from the nbd server indicating, "please issue discards if possible", it could either trigger an upcall to userspace, which could then issue the fstrim ioctl, which in the case where "mount -o discard" is enabled, would accelerate when discards took place.
Quoted 4 lines
We could then make the fstrim thread normally work on a much slower pace, but when there is a signal from the shared storage that space is needed, clients could accelerate when they issue discards to free up shared space.
This sounds great !!!
I will share this with my colleagues to see how to implement it.
Quoted 11 lines
Cheers,
- Ted
P.S. One other potential thought; if we have established a newbb_discard_rootrbtree, it *might* actually be beneficial to consider using that information in the block allocator. One of the best way to tell an SSD that block is no longer needed is to simply overwrite that block. If we do that, we don't need to send a discard to that block any more.
This seems also true for the storage server. If the nbd client reuse the blocks that's just freed, the server needn't to do new allocation.
Quoted 9 lines
Of course, we still want to keep blocks contiguous since even though seeks are free for SSD's, we want to keep large reads contiguous as much as possible, and we want to keep the extent tree as compact as possible. But if we have just released a 12k file, and we are writing a new 12k file, and don't really care *where* in the block group we are writing that file, reusing blocks that had just been freed might actually be a good strategy.
Quoted 3 lines
That's not something you need to implement in this patch series, but it might be an interesting optimization.
And thanks a million for your suggestions
Best Regards
Jianchao