Skip to content

Commit f991bd2

Browse files
Eric DumazetAl Viro
authored andcommitted
fs: introduce a per-cpu last_ino allocator
new_inode() dirties a contended cache line to get increasing inode numbers. This limits performance on workloads that cause significant parallel inode allocation. Solve this problem by using a per_cpu variable fed by the shared last_ino in batches of 1024 allocations. This reduces contention on the shared last_ino, and give same spreading ino numbers than before (i.e. same wraparound after 2^32 allocations). Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: Nick Piggin <npiggin@suse.de> Signed-off-by: Dave Chinner <dchinner@redhat.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 7de9c6e commit f991bd2

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

‎fs/inode.c‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,43 @@ static struct inode *find_inode_fast(struct super_block *sb,
717717
return NULL;
718718
}
719719

720+
/*
721+
* Each cpu owns a range of LAST_INO_BATCH numbers.
722+
* 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
723+
* to renew the exhausted range.
724+
*
725+
* This does not significantly increase overflow rate because every CPU can
726+
* consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
727+
* NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
728+
* 2^32 range, and is a worst-case. Even a 50% wastage would only increase
729+
* overflow rate by 2x, which does not seem too significant.
730+
*
731+
* On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
732+
* error if st_ino won't fit in target struct field. Use 32bit counter
733+
* here to attempt to avoid that.
734+
*/
735+
#define LAST_INO_BATCH 1024
736+
static DEFINE_PER_CPU(unsigned int, last_ino);
737+
738+
static unsigned int get_next_ino(void)
739+
{
740+
unsigned int *p = &get_cpu_var(last_ino);
741+
unsigned int res = *p;
742+
743+
#ifdef CONFIG_SMP
744+
if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
745+
static atomic_t shared_last_ino;
746+
int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
747+
748+
res = next - LAST_INO_BATCH;
749+
}
750+
#endif
751+
752+
*p = ++res;
753+
put_cpu_var(last_ino);
754+
return res;
755+
}
756+
720757
/**
721758
* new_inode - obtain an inode
722759
* @sb: superblock
@@ -731,12 +768,6 @@ static struct inode *find_inode_fast(struct super_block *sb,
731768
*/
732769
struct inode *new_inode(struct super_block *sb)
733770
{
734-
/*
735-
* On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
736-
* error if st_ino won't fit in target struct field. Use 32bit counter
737-
* here to attempt to avoid that.
738-
*/
739-
static unsigned int last_ino;
740771
struct inode *inode;
741772

742773
spin_lock_prefetch(&inode_lock);
@@ -745,7 +776,7 @@ struct inode *new_inode(struct super_block *sb)
745776
if (inode) {
746777
spin_lock(&inode_lock);
747778
__inode_sb_list_add(inode);
748-
inode->i_ino = ++last_ino;
779+
inode->i_ino = get_next_ino();
749780
inode->i_state = 0;
750781
spin_unlock(&inode_lock);
751782
}

0 commit comments

Comments
 (0)