Skip to content

Commit 901ed07

Browse files
Wang Shilongtytso
authored andcommitted
ext4: reduce lock contention in __ext4_new_inode
While running number of creating file threads concurrently, we found heavy lock contention on group spinlock: FUNC TOTAL_TIME(us) COUNT AVG(us) ext4_create 1707443399 1440000 1185.72 _raw_spin_lock 1317641501 180899929 7.28 jbd2__journal_start 287821030 1453950 197.96 jbd2_journal_get_write_access 33441470 73077185 0.46 ext4_add_nondir 29435963 1440000 20.44 ext4_add_entry 26015166 1440049 18.07 ext4_dx_add_entry 25729337 1432814 17.96 ext4_mark_inode_dirty 12302433 5774407 2.13 most of cpu time blames to _raw_spin_lock, here is some testing numbers with/without patch. Test environment: Server : SuperMicro Sever (2 x E5-2690 v3@2.60GHz, 128GB 2133MHz DDR4 Memory, 8GbFC) Storage : 2 x RAID1 (DDN SFA7700X, 4 x Toshiba PX02SMU020 200GB Read Intensive SSD) format command: mkfs.ext4 -J size=4096 test command: mpirun -np 48 mdtest -n 30000 -d /ext4/mdtest.out -F -C \ -r -i 1 -v -p 10 -u #first run to load inode mpirun -np 48 mdtest -n 30000 -d /ext4/mdtest.out -F -C \ -r -i 3 -v -p 10 -u Kernel version: 4.13.0-rc3 Test 1,440,000 files with 48 directories by 48 processes: Without patch: File Creation File removal 79,033 289,569 ops/per second 81,463 285,359 79,875 288,475 With patch: File Creation File removal 810669 301694 812805 302711 813965 297670 Creation performance is improved more than 10X with large journal size. The main problem here is we test bitmap and do some check and journal operations which could be slept, then we test and set with lock hold, this could be racy, and make 'inode' steal by other process. However, after first try, we could confirm handle has been started and inode bitmap journaled too, then we could find and set bit with lock hold directly, this will mostly gurateee success with second try. Tested-by: Shuichi Ihara <sihara@ddn.com> Signed-off-by: Wang Shilong <wshilong@ddn.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Jan Kara <jack@suse.cz>
1 parent 2fe435d commit 901ed07

1 file changed

Lines changed: 39 additions & 11 deletions

File tree

‎fs/ext4/ialloc.c‎

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,27 @@ static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
730730
return ret;
731731
}
732732

733+
static int find_inode_bit(struct super_block *sb, ext4_group_t group,
734+
struct buffer_head *bitmap, unsigned long *ino)
735+
{
736+
next:
737+
*ino = ext4_find_next_zero_bit((unsigned long *)
738+
bitmap->b_data,
739+
EXT4_INODES_PER_GROUP(sb), *ino);
740+
if (*ino >= EXT4_INODES_PER_GROUP(sb))
741+
return 0;
742+
743+
if ((EXT4_SB(sb)->s_journal == NULL) &&
744+
recently_deleted(sb, group, *ino)) {
745+
*ino = *ino + 1;
746+
if (*ino < EXT4_INODES_PER_GROUP(sb))
747+
goto next;
748+
return 0;
749+
}
750+
751+
return 1;
752+
}
753+
733754
/*
734755
* There are two policies for allocating an inode. If the new inode is
735756
* a directory, then a forward search is made for a block group with both
@@ -910,21 +931,16 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
910931
}
911932

912933
repeat_in_this_group:
913-
ino = ext4_find_next_zero_bit((unsigned long *)
914-
inode_bitmap_bh->b_data,
915-
EXT4_INODES_PER_GROUP(sb), ino);
916-
if (ino >= EXT4_INODES_PER_GROUP(sb))
934+
ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
935+
if (!ret2)
917936
goto next_group;
918-
if (group == 0 && (ino+1) < EXT4_FIRST_INO(sb)) {
937+
938+
if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
919939
ext4_error(sb, "reserved inode found cleared - "
920940
"inode=%lu", ino + 1);
921941
goto next_group;
922942
}
923-
if ((EXT4_SB(sb)->s_journal == NULL) &&
924-
recently_deleted(sb, group, ino)) {
925-
ino++;
926-
goto next_inode;
927-
}
943+
928944
if (!handle) {
929945
BUG_ON(nblocks <= 0);
930946
handle = __ext4_journal_start_sb(dir->i_sb, line_no,
@@ -944,11 +960,23 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
944960
}
945961
ext4_lock_group(sb, group);
946962
ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
963+
if (ret2) {
964+
/* Someone already took the bit. Repeat the search
965+
* with lock held.
966+
*/
967+
ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
968+
if (ret2) {
969+
ext4_set_bit(ino, inode_bitmap_bh->b_data);
970+
ret2 = 0;
971+
} else {
972+
ret2 = 1; /* we didn't grab the inode */
973+
}
974+
}
947975
ext4_unlock_group(sb, group);
948976
ino++; /* the inode bitmap is zero-based */
949977
if (!ret2)
950978
goto got; /* we grabbed the inode! */
951-
next_inode:
979+
952980
if (ino < EXT4_INODES_PER_GROUP(sb))
953981
goto repeat_in_this_group;
954982
next_group:

0 commit comments

Comments
 (0)