Lazy itable initialization for Ext4
Subsystems (2)
v2 cover Lazy itable initialization for Ext4
Hi, all
I am presenting you second version of my lazy inode table initialization code for Ext4. The patch set consist of five patches. The first one adds helper function for blkdev_issue_zeroout called sb_issue_zeroout as I am using it to zero out inode table. Second patch adds new pair of mount options (inititable/noinititable), so you can enable or disable this feature. In default it is off (noinititable), so in order to try the new code you should moutn the fs like this:
mount -o noinititable /dev/sda /mnt/
The third patch adds the inode table initialization code itself. Thread initialization was heavily inspired by nilfs2 segctord. And last two patched are making use of sb_issue_discard() in other places in Ext4, where is need to zero out a part of disk space.
To Andreas:
You suggested the approach with reading the table first to determine if the device is sparse, or thinly provisioned, or trimmed SSD. In this case the reading would be much more efficient than writing, so it would be a win. But I just wonder, if we de believe the device, that when returning zeroes it is safe to no zero the inode table, why not do it at mkfs time instead of kernel ?
To Ted:
You were suggesting that it would be nice if the thread will not run, or just quits when the system runs on the battery power. I agree that in that case we probably should not do this to save some battery life. But is it necessary, or wise to do this in kernel ? What we should do when the system runs on battery and user still want to run the lazy initialization ? I would rather let the userspace handle it. For example just remount the filesystem with -o noinititable.
___________
DESCRIPTION
___________
When lazy_itable_init extended option is passed to mke2fs, it considerably speed up filesystem creation because inode tables are not zeroed out, thus contains some old data. When this fs is mounted filesystem code should initialize (zero out) inode tables. So far this code was missing for ext4 and this patch adds this feature.
For purpose of zeroing inode tables it introduces new kernel thread called ext4lazyinit, which is created on demand and destroyed, when it is no longer needed. There is only one thread for all ext4 filesystems in the system. When the first filesystem with inititable mount option is mounted, ext4lazyinit thread is created, then the filesystem can register its request in the request list.
This thread then walks through the list of requests picking up scheduled requests and invoking ext4_init_inode_table(). Next schedule time for the request is determined from the time it took to zero out inode table, so we do not take the whole I/O bandwidth. When the thread is no longer necessary (request list is empty) it frees the appropriate structures and exits (it can be invoked later on by another filesystem).
We do not disturb regular inode allocations in any way, it just do not care whether the inode table is, or is not zeroed. But we when zeroing we have to skip used inodes, obviously. Also we should prevent new inode allocations from the group, while zeroing is on the way. For that we take write alloc_sem lock in ext4_init_inode_table() and read alloc_sem in the ext4_claim_inode, so when we are unlucky and allocator hits the group which is currently being zeroed, it just has to wait.
_________________
BENCHMARK RESULTS
_________________
We are trying to avoid performance loss when the ext4lazyinit thread is working. This is done really simply: just measure the time it takes to zero out inode table in one group and determine next schedule time from that number. For example to reach approx. 10% of the I/O bandwidth we should wait for 9 times the zeroout-time (1 time slice it is woking and 9 time slices it is sleeping). So this multiplier (9 in our example) is defining how much I/O bandwidth would be used by the thread. It is very simple method, but I think that it serves our needs.
In my benchmark I have set different values of multipliers (EXT4_LI_WAIT_MULT) to see how it affects performance. As a tool for performance measuring I have used postmark (see parameters bellow). I have created average from five postmark runs to gen more stable results. In each run I have created ext4 filesystem on the device (with lazy_itable_init set properly), mounted with inititable/noinititable mount option and run the postmark measuring the running time and number of groups the ext4lazyinit thread initializes in one run. There are the results.
All test was done on 2.6.35 kernel with and without my patches. In tables below you can see comparison between the performance of the kernel without my patches and several different settings (see 3rd column).
Graph is attached.
Type |NOPATCH NOITABLEINIT DIFF |
==================================+==================================+
Total_duration |130.00 130.00 -0.00% | Duration_of_transactions |77.80 77.40 -0.51% | Transactions/s |642.73 646.15 0.53% | Files_created/s |575.15 575.15 -0.00% | Creation_alone/s |1024.83 1020.58 -0.41% | Creation_mixed_with_transaction/s |318.29 319.99 0.53% | Read/s |321.03 322.74 0.53% | Append/s |321.69 323.40 0.53% | Deleted/s |575.15 575.15 -0.00% | Deletion_alone/s |1015.03 1010.82 -0.41% | Deletion_mixed_with_transaction/s |324.44 326.16 0.53% | Read_B/s |21179620.40 21179620.40 -0.00% | Write_B/s |66279880.00 66279880.00 -0.00% |
==================================+==================================+
RUNTIME: 2m10 GROUPS ZEROED: 0
Type |NOPATCH MULT=10 DIFF |
==================================+==================================+
Total_duration |130.00 132.40 1.85% | Duration_of_transactions |77.80 80.80 3.86% | Transactions/s |642.73 618.82 -3.72% | Files_created/s |575.15 564.67 -1.82% | Creation_alone/s |1024.83 1033.17 0.81% | Creation_mixed_with_transaction/s |318.29 306.45 -3.72% | Read/s |321.03 309.09 -3.72% | Append/s |321.69 309.72 -3.72% | Deleted/s |575.15 564.67 -1.82% | Deletion_alone/s |1015.03 1023.29 0.81% | Deletion_mixed_with_transaction/s |324.44 312.37 -3.72% | Read_B/s |21179620.40 20793522.40 -1.82% | Write_B/s |66279880.00 65071617.60 -1.82% |
==================================+==================================+
RUNTIME: 2m13 GROUPS ZEROED: 156
Type |NOPATCH MULT=5 DIFF |
==================================+==================================+
Total_duration |130.00 137.20 5.54% | Duration_of_transactions |77.80 84.60 8.74% | Transactions/s |642.73 591.04 -8.04% | Files_created/s |575.15 544.96 -5.25% | Creation_alone/s |1024.83 1021.09 -0.36% | Creation_mixed_with_transaction/s |318.29 292.69 -8.04% | Read/s |321.03 295.21 -8.04% | Append/s |321.69 295.81 -8.05% | Deleted/s |575.15 544.96 -5.25% | Deletion_alone/s |1015.03 1011.33 -0.36% | Deletion_mixed_with_transaction/s |324.44 298.34 -8.04% | Read_B/s |21179620.40 20067661.60 -5.25% | Write_B/s |66279880.00 62800096.00 -5.25% |
==================================+==================================+
RUNTIME: 2m16 GROUPS ZEROED: 324
Type |NOPATCH MULT=2 DIFF |
==================================+==================================+
Total_duration |130.00 148.40 14.15% | Duration_of_transactions |77.80 95.00 22.11% | Transactions/s |642.73 526.38 -18.10% | Files_created/s |575.15 503.78 -12.41% | Creation_alone/s |1024.83 1004.24 -2.01% | Creation_mixed_with_transaction/s |318.29 260.67 -18.10% | Read/s |321.03 262.92 -18.10% | Append/s |321.69 263.45 -18.10% | Deleted/s |575.15 503.78 -12.41% | Deletion_alone/s |1015.03 994.64 -2.01% | Deletion_mixed_with_transaction/s |324.44 265.71 -18.10% | Read_B/s |21179620.40 18551581.20 -12.41% | Write_B/s |66279880.00 58055650.40 -12.41% |
==================================+==================================+
RUNTIME: 2m28 GROUPS ZEROED: 748
The benchmark showed, that patch itself does not introduce any performance loss (at least for postmark), when ext4lazyinit thread is not activated. However, when it is activated, there is explicit performance loss due to inode table zeroing, but with EXT4_LI_WAIT_MULT=10 it is just about 1.8%, which may, or may not be much, so when I think about it now we should probably make this settable via sysfs. What do you think ?
___________________
POSTMARK PARAMETERS
___________________
set number 50000
set transactions 50000
set read 4096
set write 4096
set bias read 5
set bias create 5
set report terse
set size 1000 200000
set buffering false
Any comments are welcomed.
Thanks!
-Lukas
---
[PATCH 1/5] Add helper function for blkdev_issue_zeroout [PATCH 2/5] Add inititable/noinititable mount options for ext4 [PATCH 3/5] Add inode table initialization code for Ext4 [PATCH 4/5] Use sb_issue_zeroout in setup_new_group_blocks [PATCH 5/5] Use sb_issue_discard in ext4_ext_zeroout
fs/ext4/ext4.h | 37 +++++ fs/ext4/extents.c | 68 +-------- fs/ext4/ialloc.c | 108 +++++++++++++ fs/ext4/resize.c | 44 ++---- fs/ext4/super.c | 405 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/blkdev.h | 8 + 6 files changed, 575 insertions(+), 95 deletions(-)
PATCH v2 1/5 Add helper function for blkdev_issue_zeroout
This is done the same way as function sb_issue_discard for blkdev_issue_discard.
--- include/linux/blkdev.h | 8 ++++++++ 1 files changed, 8 insertions(+), 0 deletions(-)
include/linux/blkdev.h +8 -0
@@ -1023,6 +1023,14 @@ static inline int sb_issue_discard(struct super_block *sb,return blkdev_issue_discard (sb->s_bdev, block, nr_blocks,GFP_KERNEL ,BLKDEV_IFL_WAIT |BLKDEV_IFL_BARRIER ); } +static inline int sb_issue_zeroout (struct super_block *sb, +sector_t block,sector_t nr_blocks) +{ + block <<= (sb->s_blocksize_bits -9 ); + nr_blocks <<= (sb->s_blocksize_bits -9 ); +return blkdev_issue_zeroout (sb->s_bdev, block, nr_blocks,GFP_KERNEL , +BLKDEV_IFL_WAIT |BLKDEV_IFL_BARRIER ); +}extern int blk_verify_command (unsigned char *cmd,fmode_t has_write_perm);
Signature · 1 line
1.7.2.2
Re: [PATCH 1/5] Add helper function for blkdev_issue_zeroout
include/linux/blkdev.h static inline int sb_issue_discard(struct super_block *sb, · line 1033
Quoted 8 lines · Lukas Czerner
include/linux/blkdev.h +8
-0+static inline int sb_issue_zeroout (struct super_block *sb, +sector_t block,sector_t nr_blocks) +{ + block <<= (sb->s_blocksize_bits -9 ); + nr_blocks <<= (sb->s_blocksize_bits -9 ); +return blkdev_issue_zeroout (sb->s_bdev, block, nr_blocks,GFP_KERNEL , +BLKDEV_IFL_WAIT |BLKDEV_IFL_BARRIER ); +}
While I can understand that we might need a barrier for this (to avoid it being reordered with later writes that are using these blocks), I'm not sure it needs to wait for previous IO to complete.
Cheers, Andreas
Re: [PATCH 1/5] Add helper function for blkdev_issue_zeroout
Quoted 13 lines · Andreas Dilger
include/linux/blkdev.h static inline int sb_issue_discard(struct super_block *sb, · line 1033
Quoted 8 lines · Lukas Czerner
include/linux/blkdev.h +8
-0+static inline int sb_issue_zeroout (struct super_block *sb, +sector_t block,sector_t nr_blocks) +{ + block <<= (sb->s_blocksize_bits -9 ); + nr_blocks <<= (sb->s_blocksize_bits -9 ); +return blkdev_issue_zeroout (sb->s_bdev, block, nr_blocks,GFP_KERNEL , +BLKDEV_IFL_WAIT |BLKDEV_IFL_BARRIER ); +}While I can understand that we might need a barrier for this (to avoid it being reordered with later writes that are using these blocks), I'm not sure it needs to wait for previous IO to complete.
We are waiting for submitted bios to finish. Especially in my patch it is needed, because I need to know how long it takes to get this IO on disk do determine next schedule time, depending on the IO load. I am not sure about this, but when it would not wait it will complete fairly quickly and my "IO throttling" code would be useless.
And if I am wrong, and you are really convinced that it should not wait, what about sb_issue_discard(), it is also waiting for completion, is it ok?
Regards.
-Lukas
Quoted 3 lines
Cheers, Andreas
PATCH v2 2/5 Add inititable/noinititable mount options for ext4
In reply to Lukas CzernerAdd new mount flag EXT4_MOUNT_INIT_INODE_TABLE and add new pair of mount options (inititable/noinititable). When mounted with inititable file system should try to initialize uninitialized inode tables, otherwise it should prevent initializing inode tables.
For now, default is noinittable.
--- fs/ext4/ext4.h | 1 + fs/ext4/super.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 0 deletions(-)
fs/ext4/ext4.h +1 -0
@@ -885,6 +885,7 @@ struct ext4_inode_info {#define EXT4_MOUNT_DATA_ERR_ABORT 0x10000000/* Abort on file data write */ #define EXT4_MOUNT_BLOCK_VALIDITY 0x20000000/* Block validity checking */ #define EXT4_MOUNT_DISCARD 0x40000000/* Issue DISCARD requests */ +#define EXT4_MOUNT_INIT_INODE_TABLE 0x80000000/* Initialize uninitialized itables */ #define clear_opt (o, opt) o &= ~EXT4_MOUNT_##opt#define set_opt (o, opt) o |= EXT4_MOUNT_##opt
fs/ext4/super.c +12 -0
@@ -986,6 +986,9 @@ static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)if (test_opt (sb,DIOREAD_NOLOCK ))seq_puts (seq,",dioread_nolock" ); +if (test_opt (sb,INIT_INODE_TABLE )) +seq_puts (seq,",init_inode_table" ); +ext4_show_quota_options (seq, sb);return 0 ;
@@ -1161,6 +1164,7 @@ enum { Opt_inode_readahead_blks, Opt_journal_ioprio, Opt_dioread_nolock, Opt_dioread_lock, Opt_discard, Opt_nodiscard, + Opt_init_inode_table, Opt_noinit_inode_table, }; static const match_table_t tokens = {
@@ -1231,6 +1235,8 @@ static const match_table_t tokens = { {Opt_dioread_lock, "dioread_lock"}, {Opt_discard, "discard"}, {Opt_nodiscard, "nodiscard"}, + {Opt_init_inode_table, "inititable"}, + {Opt_noinit_inode_table, "noinititable"}, {Opt_err, NULL}, };
@@ -1699,6 +1705,12 @@ set_qf_format:case Opt_dioread_lock:clear_opt (sbi->s_mount_opt,DIOREAD_NOLOCK );break ; +case Opt_init_inode_table: +set_opt (sbi->s_mount_opt,INIT_INODE_TABLE ); +break ; +case Opt_noinit_inode_table: +clear_opt (sbi->s_mount_opt,INIT_INODE_TABLE ); +break ;default :ext4_msg (sb,KERN_ERR , "Unrecognized mount option \"%s\" "
Signature · 1 line
1.7.2.2
PATCH v2 3/5 Add inode table initialization code for Ext4
In reply to Lukas CzernerWhen lazy_itable_init extended option is passed to mke2fs, it considerably speed up filesystem creation because inode tables are not zeroed out, thus contains some old data. When this fs is mounted filesystem code should initialize (zero out) inode tables. So far this code was missing for ext4 and this patch adds this feature.
For purpose of zeroing inode tables it introduces new kernel thread called ext4lazyinit, which is created on demand and destroyed, when it is no longer needed. There is only one thread for all ext4 filesystems in the system. When the first filesystem with inititable mount option is mounted, ext4lazyinit thread is created, then the filesystem can register its request in the request list.
This thread then walks through the list of requests picking up scheduled requests and invoking ext4_init_inode_table(). Next schedule time for the request is determined from the time it took to zero out inode table, so we do not take the whole I/O bandwidth. When the thread is no longer necessary (request list is empty) it frees the appropriate structures and exits (it can be invoked later on by another filesystem).
We do not disturb regular inode allocations in any way, it just do not care whether the inode table is, or is not zeroed. But we when zeroing we have to skip used inodes, obviously. Also we should prevent new inode allocations from the group, while zeroing is on the way. For that we take write alloc_sem lock in ext4_init_inode_table() and read alloc_sem in the ext4_claim_inode, so when we are unlucky and allocator hits the group which is currently being zeroed, it just has to wait.
--- fs/ext4/ext4.h | 36 +++++ fs/ext4/ialloc.c | 108 +++++++++++++++ fs/ext4/super.c | 393 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 537 insertions(+), 0 deletions(-)
fs/ext4/ext4.h +36 -0
@@ -1144,6 +1144,9 @@ struct ext4_sb_info {/* workqueue for dio unwritten */ struct workqueue_struct *dio_unwritten_wq; + +/* Lazy inode table initialization info */ +struct ext4_li_request *s_li_request; };static inline struct ext4_sb_info *EXT4_SB (struct super_block *sb)
@@ -1467,6 +1470,37 @@ void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,extern struct proc_dir_entry *ext4_proc_root;/* +* Timeout and state flag for lazy initialization inode thread. +*/ +#define EXT4_LI_WAIT_MULT 10 +#define EXT4_LI_MAX_START_DELAY 5 +#define EXT4_LAZYINIT_QUIT 0x0001 +#define EXT4_LAZYINIT_RUNNING 0x0002 + +/* +* Lazy inode table initialization info +*/ +struct ext4_lazy_init { +unsigned long li_state; + +wait_queue_head_t li_wait_daemon; +wait_queue_head_t li_wait_task; +struct timer_list li_timer; +struct task_struct *li_task; + +struct list_head li_request_list; +struct mutex li_list_mtx; +}; + +struct ext4_li_request { +struct super_block *lr_super; +struct ext4_sb_info *lr_sbi; +ext4_group_t lr_next_group; +struct list_head lr_request; +unsigned long lr_next_sched; +}; + +/* * Function prototypes */
@@ -1539,6 +1573,8 @@ extern unsigned ext4_init_inode_bitmap(struct super_block *sb,ext4_group_t group, struct ext4_group_desc *desc);extern void mark_bitmap_end (int start_bit,int end_bit,char *bitmap); +extern int ext4_init_inode_table (struct super_block *sb, +ext4_group_t group);/* mballoc.c */ extern long ext4_mb_stats;
fs/ext4/ialloc.c +108 -0
149 lines not on this page · raw message
fs/ext4/super.c +393 -0
447 lines not on this page · raw message
Signature · 1 line
1.7.2.2
Re: [PATCH 3/5] Add inode table initialization code for Ext4
fs/ext4/ialloc.c unsigned long ext4_count_dirs(struct super_block * sb) · line 1246
Quoted 8 lines · Lukas Czerner
fs/ext4/ialloc.c +8
-0+extern int ext4_init_inode_table(struct super_block *sb, ext4_group_t +{ +if (sb->s_flags &MS_RDONLY ) { +ext4_warning (sb,"Filesystem mounted read only. " +"Won't zeroout anything!" ); + ret =1 ; + goto out; + }
Is it actually needed to print this error message? This thread shouldn't be run if the filesystem was originally mounted read-only (that is checked in ext4_register_li_request()), and if it is later remounted read-only (due to user action or filesystem error) I don't think it is critical to print a message here.
The ext4lazyinit thread will be restarted when the filesystem is remounted read-write in the future.
fs/ext4/ialloc.c unsigned long ext4_count_dirs(struct super_block * sb) · line 1291
Quoted 7 lines
fs/ext4/ialloc.c +7
-0+/* +* Skip zeroout if the inode table is full. But we set the ZEROED +* flag anyway, because obviously, when it is full it does not need +* further zeroing. +*/ +if (unlikely (num ==0 )) + goto skip_zeroout;
In fact, this is still safe as long as num < EXT4_INODES_PER_GROUP(sb).
fs/ext4/ialloc.c unsigned long ext4_count_dirs(struct super_block * sb) · line 1294
Quoted 2 lines
fs/ext4/ialloc.c +2
-0+ext4_debug ("going to zero out inode table in group %d\n" , + group);
the "group" should align with the '(' on the previous line.
fs/ext4/super.c static int ext4_feature_set_ok(struct super_block *sb, int readonly) · line 2480
Quoted 13 lines
fs/ext4/super.c +13
-0+/* Find next suitable group adn run ext4_init_inode_table */ +static int ext4_run_li_request (struct ext4_li_request *elr) +{ +for (group = elr->lr_next_group; group < ngroups; group++) { + gdp =ext4_get_group_desc (sb, group,NULL ); +if (!gdp) { + ret =1 ; +break ; + } + +if (!(gdp->bg_flags &cpu_to_le16 (EXT4_BG_INODE_ZEROED ))) +break ; + }
There is nowhere in the code that lr_next_group is increased. That should probably be done in this function. Otherwise, for a large filesystem with 256k groups in it there will be an O(n^2) search for the next group to zero.
fs/ext4/super.c static int ext4_feature_set_ok(struct super_block *sb, int readonly) · line 2572
Quoted 9 lines
fs/ext4/super.c +9
-0+static int ext4_lazyinit_thread (void *arg) +{ +while (true) { +list_for_each_safe (pos, n, &eli->li_request_list) { +/* Handle jiffies overflow - is it even possible?*/ +if (timeout > jiffies) + timeout = (ULONG_MAX - timeout) + jiffies; +else + timeout = jiffies - timeout;
I'm not sure this is needed. When you compute "jiffies - timeout", it doesn't matter if "jiffies" has suddenly wrapped, the result will be the same.
fs/ext4/super.c static int ext4_feature_set_ok(struct super_block *sb, int readonly) · line 2685
Quoted 6 lines
fs/ext4/super.c +6
-0+static ext4_group_t ext4_has_uninit_itable (struct super_block *sb) +{ +for (group =0 ; group < ngroups; group++) { + gdp =ext4_get_group_desc (sb, group,NULL ); +if (!(gdp->bg_flags &cpu_to_le16 (EXT4_BG_INODE_ZEROED ))) +break ;
It would be much more efficient to do this in ext4_check_descriptors(), which is already walking through all of the groups checking the validity of the group descriptors at mount time.
An alternative that would speed up mounting of large filesystems is to do the ext4_check_descriptors() checks from the ext4lazyinit thread. If any groups are found to be in error (which should be very rare) they can be marked in the group descriptor with a new per-group EXT4_BG_ERROR flag and skipped for any future allocations. This would need an additional in-memory flag that tracked if the group descriptor's checksum and block pointers had been validated yet or not, so that they are not used by anything before they are checked. That is a change that could be made at some later stage, after the initial patch is landed.
Cheers, Andreas
Re: [PATCH 3/5] Add inode table initialization code for Ext4
Quoted 16 lines · Andreas Dilger
fs/ext4/ialloc.c unsigned long ext4_count_dirs(struct super_block * sb) · line 1246
Quoted 8 lines · Lukas Czerner
fs/ext4/ialloc.c +8
-0+extern int ext4_init_inode_table(struct super_block *sb, ext4_group_t +{ +if (sb->s_flags &MS_RDONLY ) { +ext4_warning (sb,"Filesystem mounted read only. " +"Won't zeroout anything!" ); + ret =1 ; + goto out; + }Is it actually needed to print this error message? This thread shouldn't be run if the filesystem was originally mounted read-only (that is checked in
ext4_register_li_request()), and if it is later remounted read-only (due to user action or filesystem error) I don't think it is critical to print a message here.
Quoted 2 lines
The ext4lazyinit thread will be restarted when the filesystem is remounted read-write in the future.
Yeah, it is not needed, because when filesystem is mounted (or remounted) as read only the thread would not start, or would be destroyed. I'll get rid of that error message, but I would rather leave the check anyway, just to be sure.
Quoted 10 lines
fs/ext4/ialloc.c unsigned long ext4_count_dirs(struct super_block * sb) · line 1291
Quoted 8 lines
fs/ext4/ialloc.c +7
-0+/* +* Skip zeroout if the inode table is full. But we set the ZEROED +* flag anyway, because obviously, when it is full it does not need +* further zeroing. +*/ +if (unlikely (num ==0 )) + goto skip_zeroout;In fact, this is still safe as long as num <
EXT4_INODES_PER_GROUP(sb).
Yes, it is safe when num is zero, but in that case the barrier would be sent anyway, so I rather skip it. Or I can fix the blkdev_issue_zeroout() to check if nr_sects is zero and exit before the barrier, this might be a better solution.
Quoted 15 lines
fs/ext4/ialloc.c unsigned long ext4_count_dirs(struct super_block * sb) · line 1294
Quoted 3 lines
fs/ext4/ialloc.c +2
-0+ext4_debug ("going to zero out inode table in group %d\n" , + group);the "group" should align with the '(' on the previous line.
Quoted 9 lines
fs/ext4/super.c +9
-0+/* Find next suitable group adn run ext4_init_inode_table */ +static int ext4_run_li_request (struct ext4_li_request *elr) +{ +for (group = elr->lr_next_group; group < ngroups; group++) { + gdp =ext4_get_group_desc (sb, group,NULL ); +if (!gdp) { + ret =1 ; +break ; + }
Quoted 8 lines
fs/ext4/super.c static int ext4_feature_set_ok(struct super_block *sb, int readonly) · line 2480
Quoted 4 lines
fs/ext4/super.c +4
-0+ +if (!(gdp->bg_flags &cpu_to_le16 (EXT4_BG_INODE_ZEROED ))) +break ; + }There is nowhere in the code that
lr_next_groupis increased. That should probably be done in this function. Otherwise, for a large filesystem with 256k groups in it there will be an O(n^2) search for the next group to zero.
I do not believe I forgot to set the lr_next_group, but obviously I did :). I'll fix that.
Quoted 14 lines
fs/ext4/super.c static int ext4_feature_set_ok(struct super_block *sb, int readonly) · line 2572
Quoted 10 lines
fs/ext4/super.c +9
-0+static int ext4_lazyinit_thread (void *arg) +{ +while (true) { +list_for_each_safe (pos, n, &eli->li_request_list) { +/* Handle jiffies overflow - is it even possible?*/ +if (timeout > jiffies) + timeout = (ULONG_MAX - timeout) + jiffies; +else + timeout = jiffies - timeout;I'm not sure this is needed. When you compute "jiffies - timeout", it doesn't matter if "jiffies" has suddenly wrapped, the result will be the same.
You're right, what I was thinking...
Quoted 11 lines
fs/ext4/super.c static int ext4_feature_set_ok(struct super_block *sb, int readonly) · line 2685
Quoted 7 lines
fs/ext4/super.c +6
-0+static ext4_group_t ext4_has_uninit_itable (struct super_block *sb) +{ +for (group =0 ; group < ngroups; group++) { + gdp =ext4_get_group_desc (sb, group,NULL ); +if (!(gdp->bg_flags &cpu_to_le16 (EXT4_BG_INODE_ZEROED ))) +break ;It would be much more efficient to do this in
ext4_check_descriptors(), which is already walking through all of the groups checking the validity of the group descriptors at mount time.
Sounds good.
Quoted 10 lines
An alternative that would speed up mounting of large filesystems is to do the
ext4_check_descriptors()checks from the ext4lazyinit thread. If any groups are found to be in error (which should be very rare) they can be marked in the group descriptor with a new per-groupEXT4_BG_ERRORflag and skipped for any future allocations. This would need an additional in-memory flag that tracked if the group descriptor's checksum and block pointers had been validated yet or not, so that they are not used by anything before they are checked. That is a change that could be made at some later stage, after the initial patch is landed.
Right now, when descriptors are bad filesystem is not mounted, which is a good thing because we do not want to work with corrupted filesystem and we are forcing user to run fsck to fix it (if it is possible). But in your scenario we will be actually using the filesystem with errors. Of course we will not be using corrupted groups, but user will probably ignore the error and try to use the filesystem as he was used to. Is it a good thing ? We won't be able to use data from corrupted groups and what we will tell user when he would like to work with them ?
Quoted 3 lines
Cheers, Andreas
Thanks a lot for you comments and suggestions Andreas, I really appreciate it.
Regards.
-Lukas
PATCH v2 4/5 Use sb_issue_zeroout in setup_new_group_blocks
In reply to Lukas CzernerUse sn_issue_discard to zero out inode table and descriptor table blocks instead of old approach which involves journaling.
--- fs/ext4/resize.c | 44 +++++++++++--------------------------------- 1 files changed, 11 insertions(+), 33 deletions(-)
fs/ext4/resize.c +5 -24
@@ -226,23 +226,12 @@ static int setup_new_group_blocks(struct super_block *sb, }/* Zero out all of the reserved backup group descriptor table blocks */ -for (i =0 , bit = gdblocks +1 , block = start + bit; - i < reserved_gdb; i++, block++, bit++) { -struct buffer_head *gdb; - -ext4_debug ("clear reserved block %#04llx (+%d)\n" , block, bit); - -if ((err =extend_or_restart_transaction (handle,1 , bh))) - goto exit_bh; +ext4_debug ("clear inode table blocks %#04llx -> %#04llx\n" , + block, sbi->s_itb_per_group); + err =sb_issue_zeroout (sb, gdblocks + start +1 , reserved_gdb); +if (err) + goto exit_journal; -if (IS_ERR (gdb =bclean (handle, sb, block))) { - err =PTR_ERR (gdb); - goto exit_bh; - } -ext4_handle_dirty_metadata (handle,NULL , gdb); -ext4_set_bit (bit, bh->b_data); -brelse (gdb); - }ext4_debug ("mark block bitmap %#04llx (+%llu)\n" , input->block_bitmap, input->block_bitmap - start);ext4_set_bit (input->block_bitmap - start, bh->b_data);
@@ -251,23 +240,12 @@ static int setup_new_group_blocks(struct super_block *sb,ext4_set_bit (input->inode_bitmap - start, bh->b_data);/* Zero out all of the inode table blocks */ -for (i =0 , block = input->inode_table, bit = block - start; - i < sbi->s_itb_per_group; i++, bit++, block++) { -struct buffer_head *it; - -ext4_debug ("clear inode block %#04llx (+%d)\n" , block, bit); - -if ((err =extend_or_restart_transaction (handle,1 , bh))) - goto exit_bh;
PATCH v2 5/5 Use sb_issue_discard in ext4_ext_zeroout
In reply to Lukas CzernerChange ext4_ext4_zeroout to use sb_issue_discard instead of its own approach to zero out extents.
--- fs/ext4/extents.c | 68 ++++------------------------------------------------ 1 files changed, 6 insertions(+), 62 deletions(-)
fs/ext4/extents.c +6 -62
@@ -2536,77 +2536,21 @@ void ext4_ext_release(struct super_block *sb)#endif } -static void bi_complete (struct bio *bio,int error) -{ -complete ((struct completion *)bio->bi_private); -} -/* FIXME!! we need to try to merge to left or right after zero-out */ static int ext4_ext_zeroout (struct inode *inode,struct ext4_extent *ex) { +ext4_fsblk_t ee_pblock; +unsigned int ee_len;int ret; -struct bio *bio; -int blkbits, blocksize; -sector_t ee_pblock; -struct completion event; -unsigned int ee_len, len, done, offset; - - blkbits = inode->i_blkbits; - blocksize = inode->i_sb->s_blocksize; ee_len =ext4_ext_get_actual_len (ex); ee_pblock =ext_pblock (ex); -/* convert ee_pblock to 512 byte sectors */ - ee_pblock = ee_pblock << (blkbits -9 ); - -while (ee_len >0 ) { - -if (ee_len >BIO_MAX_PAGES ) - len =BIO_MAX_PAGES ; -else - len = ee_len; - - bio =bio_alloc (GFP_NOIO , len); -if (!bio) -return -ENOMEM ; - - bio->bi_sector = ee_pblock; - bio->bi_bdev = inode->i_sb->s_bdev; - - done =0 ; - offset =0 ; -while (done < len) { - ret =bio_add_page (bio,ZERO_PAGE (0 ), - blocksize, offset); -if (ret != blocksize) { -/* -* We can't add any more pages because of -* hardware limitations. Start a new bio. -*/ -break ; - } - done++; - offset += blocksize; -if (offset >=PAGE_CACHE_SIZE ) - offset =0 ; - } - -init_completion (&event); - bio->bi_private = &event; - bio->bi_end_io = bi_complete; -submit_bio (WRITE , bio); -wait_for_completion (&event); + ret =sb_issue_zeroout (inode->i_sb, ee_pblock, ee_len); +if (ret >0 ) + ret =0 ; -if (!test_bit (BIO_UPTODATE , &bio->bi_flags)) { -bio_put (bio); -return -EIO ; - } -bio_put (bio); - ee_len -= done; - ee_pblock += done << (blkbits -9 ); - } -return 0 ; +return ret; }#define EXT4_EXT_ZERO_LEN 7
Signature · 1 line
1.7.2.2
Re: [PATCH 5/5] Use sb_issue_discard in ext4_ext_zeroout
Quoted 2 lines · Lukas Czerner
Change
ext4_ext4_zerooutto usesb_issue_discardinstead of its own approach to zero out extents.
typo - should be ext4_ext_zeroout().
Cheers, Andreas
Graph attached.
-Lukas
Quoted 1 line · Lukas Czerner
Graph attached.
I assume the groups are "unpatched", "noinititable", "MULT=10, "MULT=5", and "MULT=2"?
Cheers, Andreas
Quoted 7 lines · Andreas Dilger
Quoted 1 line · Lukas Czerner
Graph attached.
I assume the groups are "unpatched", "noinititable", "MULT=10, "MULT=5", and "MULT=2"?
Cheers, Andreas
Of course, sorry about that. Fixed graph attached.
-Lukas
Quoted 4 lines · Lukas Czerner
Second patch adds new pair of mount
options (inititable/noinititable), so you can enable or disable this feature. In default it is off (noinititable), so in order to try the new code you should moutn the fs like this:
typo ^^^^^^
Quoted 1 line
mount -o noinititable
/dev/sda/mnt/
typo ^^^
It should use "inititable" if you want to try the new code.
Quoted 7 lines
To Andreas:
You suggested the approach with reading the table first to determine if the device is sparse, or thinly provisioned, or trimmed SSD. In this case the reading would be much more efficient than writing, so it would be a win. But I just wonder, if we de believe the device, that when returning zeroes it is safe to no zero the inode table, why not do it at mkfs time instead of kernel ?
Good question, but I think the answer is that reading the full itable at mke2fs time, just like writing it at mke2fs time, is _serialized_ time spent waiting for the filesystem to become useful. Doing it in the background in the kernel can happen in parallel with other operations (e.g. formatting other disks, waiting for user input from the installer, downloading updates, etc).
Quoted 8 lines
To Ted:
You were suggesting that it would be nice if the thread will not run, or just quits when the system runs on the battery power. I agree that in that case we probably should not do this to save some battery life. But is it necessary, or wise to do this in kernel ? What we should do when the system runs on battery and user still want to run the lazy initialization ? I would rather let the userspace handle it. For example just remount the filesystem with -o noinititable.
I would tend to agree with Ted. There will be _some_ time that the system is plugged in to charge the battery, and this is very normal when installing the system initially, so delaying the zeroing will not affect most users. For the case where the user IS on battery power for some reason, I think it is better to avoid consuming the battery in that case.
Maybe a good way to compromise is to just put the thread to sleep for 5- or 10-minute intervals while on battery power, and only start zeroing once plugged in. That solves the situation where (like me) the laptop stays on for months at a time with only suspend/resume, and is only rarely rebooted, but it is plugged in to recharge often.
Since we don't expect to need the itable zeroing unless there is corruption of the on-disk group descriptor data, I don't think that it is urgent to do this immediately after install. If there is corruption within hours of installing a system, there are more serious problems with the system that we cannot fix.
Quoted 11 lines
In my benchmark I have set different values of multipliers (
EXT4_LI_WAIT_MULT) to see how it affects performance. As a tool for performance measuring I have used postmark (see parameters bellow). I have created average from five postmark runs to gen more stable results. In each run I have created ext4 filesystem on the device (withlazy_itable_initset properly), mounted with inititable/noinititable mount option and run the postmark measuring the running time and number of groups the ext4lazyinit thread initializes in one run.Type |NOPATCH MULT=10 DIFF |
==================================+==================================+
Quoted 8 lines
Total_duration |130.00 132.40 1.85% | Duration_of_transactions |77.80 80.80 3.86% | Transactions/s |642.73 618.82 -3.72% | [snip] Read_B/s |21179620.40 20793522.40 -1.82% | Write_B/s |66279880.00 65071617.60 -1.82% |==================================+==================================+
RUNTIME: 2m13 GROUPS ZEROED: 156
This is a relatively minor overhead, and when one considers that this is a very metadata-heavy benchmark being run immediately after reformatting the filesystem, it is not a very realistic real-world situation.
The good (expected) news is that there is no performance impact when the thread is not active, so this is a one-time hit. In fairness, the "NOPATCH" test times should include the full mke2fs time as well, if one wants to consider the real-world impact of a much faster mke2fs run and a slightly-slower runtime for a few minutes.
Do you have any idea of how long the zeroing takes to complete in the case of MULT=10 without any load, as a function of the filesystem size? That would tell us what the minimum time after startup that the system might be slowed down by the zeroing thread.
Quoted 6 lines
The benchmark showed, that patch itself does not introduce any performance loss (at least for postmark), when ext4lazyinit thread is not activated. However, when it is activated, there is explicit performance loss due to inode table zeroing, but with EXT4_LI_WAIT_MULT=10 it is just about 1.8%, which may, or may not be much, so when I think about it now we should probably make this settable via sysfs. What do you think ?
I don't think it is necessary to have a sysfs parameter for this. Instead
I would suggest making the "inititable" mount option take an optional
numeric parameter that specifies the MULT factor. The ideal solution is
to make the zeroing happen with a MULT=100 under IO load, but run full-out (i.e. MULT=0?) while there is no IO load. That said, I don't think it is critical enough to delay this patch from landing to implement that.
Cheers, Andreas
Quoted 11 lines · Andreas Dilger
Quoted 4 lines · Lukas Czerner
Second patch adds new pair of mount
options (inititable/noinititable), so you can enable or disable this feature. In default it is off (noinititable), so in order to try the new code you should moutn the fs like this:typo ^^^^^^
Quoted 1 line
mount -o noinititable
/dev/sda/mnt/typo ^^^
It should use "inititable" if you want to try the new code.
Of course, thanks.
Quoted 9 lines
Quoted 8 lines
To Andreas:
You suggested the approach with reading the table first to determine if the device is sparse, or thinly provisioned, or trimmed SSD. In this case the reading would be much more efficient than writing, so it would be a win. But I just wonder, if we de believe the device, that when returning zeroes it is safe to no zero the inode table, why not do it at mkfs time instead of kernel ?
Quoted 6 lines
Good question, but I think the answer is that reading the full itable at mke2fs time, just like writing it at mke2fs time, is _serialized_ time spent waiting for the filesystem to become useful. Doing it in the background in the kernel can happen in parallel with other operations (e.g. formatting other disks, waiting for user input from the installer, downloading updates, etc).
I think that important thing is how long it will take to verify that the device is, or is not sparse. Obviously (in almost all cases) we won't be reading all inode tables from usual physical disk, because there will be some garbage, so we will just mark everything not zeroed.
In case of SSD we can just do the trim and verify that it really returns zeroes. If there are some devices which will return zeroes after trim, but after one power cycle it will return garbage, we do not care that much about it, because obviously when we do not believe the device in mkfs, we can't believe it in kernel.
In the case of sparse and thinly provisioned devices reads will be fairly quick, so it should not take long to verify that it really return zeroes for all inode tables. The question is, how long exactly will it take.
Quoted 22 lines
Quoted 9 lines
To Ted:
You were suggesting that it would be nice if the thread will not run, or just quits when the system runs on the battery power. I agree that in that case we probably should not do this to save some battery life. But is it necessary, or wise to do this in kernel ? What we should do when the system runs on battery and user still want to run the lazy initialization ? I would rather let the userspace handle it. For example just remount the filesystem with -o noinititable.I would tend to agree with Ted. There will be _some_ time that the system is plugged in to charge the battery, and this is very normal when installing the system initially, so delaying the zeroing will not affect most users. For the case where the user IS on battery power for some reason, I think it is better to avoid consuming the battery in that case.
Maybe a good way to compromise is to just put the thread to sleep for 5- or 10-minute intervals while on battery power, and only start zeroing once plugged in. That solves the situation where (like me) the laptop stays on for months at a time with only suspend/resume, and is only rarely rebooted, but it is plugged in to recharge often.
Quoted 5 lines
Since we don't expect to need the itable zeroing unless there is corruption of the on-disk group descriptor data, I don't think that it is urgent to do this immediately after install. If there is corruption within hours of installing a system, there are more serious problems with the system that we cannot fix.
I still do not see the reason why not to do simply
mount -o remount,noinititable <dir>
I believe that there are daemons which are adjusting system settings when the system is running on battery. I really do not like the kernel solution for this because once it is hardcoded in kernel I can not do anything about it, even if I want to run ext4lazyinit no matter what. I really think that there is no way we should hard code it in the kernel without any possibility for user to decide on his own.
Quoted 31 lines
Quoted 12 lines
In my benchmark I have set different values of multipliers (
EXT4_LI_WAIT_MULT) to see how it affects performance. As a tool for performance measuring I have used postmark (see parameters bellow). I have created average from five postmark runs to gen more stable results. In each run I have created ext4 filesystem on the device (withlazy_itable_initset properly), mounted with inititable/noinititable mount option and run the postmark measuring the running time and number of groups the ext4lazyinit thread initializes in one run.Type |NOPATCH MULT=10 DIFF |
==================================+==================================+Quoted 8 lines
Total_duration |130.00 132.40 1.85% | Duration_of_transactions |77.80 80.80 3.86% | Transactions/s |642.73 618.82 -3.72% | [snip] Read_B/s |21179620.40 20793522.40 -1.82% | Write_B/s |66279880.00 65071617.60 -1.82% |==================================+==================================+
RUNTIME: 2m13 GROUPS ZEROED: 156This is a relatively minor overhead, and when one considers that this is a very metadata-heavy benchmark being run immediately after reformatting the filesystem, it is not a very realistic real-world situation.
The good (expected) news is that there is no performance impact when the thread is not active, so this is a one-time hit. In fairness, the "NOPATCH" test times should include the full mke2fs time as well, if one wants to consider the real-world impact of a much faster mke2fs run and a slightly-slower runtime for a few minutes.
Quoted 4 lines
Do you have any idea of how long the zeroing takes to complete in the case of MULT=10 without any load, as a function of the filesystem size? That would tell us what the minimum time after startup that the system might be slowed down by the zeroing thread.
Well, that depends on how fast the device is. In this case zeroing one single group takes approx. 28ms without any load. So we can do a little math to figure out average time to complete the task (assume 4k block size).
149GB filesystem - the one I was using in the test. 1192 groups -> 1192 inode tables
1 inode table takes (28ms zeroing + 28*10ms waiting) = 308ms 1192 inode tables takes 367136ms = 367.136s = 6m7.136s
In the real test it took 6m22s which is pretty close to my calculation.
Quoted 8 lines
Quoted 7 lines
The benchmark showed, that patch itself does not introduce any performance loss (at least for postmark), when ext4lazyinit thread is not activated. However, when it is activated, there is explicit performance loss due to inode table zeroing, but with EXT4_LI_WAIT_MULT=10 it is just about 1.8%, which may, or may not be much, so when I think about it now we should probably make this settable via sysfs. What do you think ?
Quoted 5 lines
I don't think it is necessary to have a sysfs parameter for this. Instead
I would suggest making the "inititable" mount option take an optional
numeric parameter that specifies the MULT factor. The ideal solution is
to make the zeroing happen with a MULT=100 under IO load, but run full-out (i.e. MULT=0?) while there is no IO load. That said, I don't think it is critical enough to delay this patch from landing to implement that.
Right, mount option parameter would be even better.
Quoted 3 lines
Cheers, Andreas
Thanks!
-Lukas