Skip to contentSkip to conversation

ext4: Try to better reuse recently freed space

RFC PATCH Superseded Lukas Czerner 4 participants 12 messages Jul 4, 2013 – Jul 10, 2013 ext4 file system

lore
Share & tools
lore
#0 lore raw

cover ext4: Try to better reuse recently freed space

Hi all,

as I just recently discovered here http://www.ogris.de/blkalloc/ ext4 have some unexpected allocation strategies which can cause some problems in certain scenarios (see the 1/1 patch).

This is my attempt to fix this, however I think that this will need some discussion because it changes some of the block allocator heuristic quite significantly and I would like to make sure that it will not hurt our performance in some widely used workloads. Eric, could you run your performance testing with your test suite to see how it performs ?

Also, I think that the old behaviour might be quite helpful for SSD because we're not overwriting existing blocks but rather trying to use new one, so the firmware should have easier job. However I do not know how significant impact if at all it might have. If we find out that this is helpful for SSD we might make this conditional depending on the type of the device.

Which brings me to the other problem. Since the original behaviour is really bad for thinly provisioned devices we would like to avoid using it even if the underlying storage is SSD, however IIRC there is no way to distinguish this from within the kernel.

See the comparison between the old and new allocator behaviour
http://people.redhat.com/lczerner/allocator/

Comments and testing is welcomed.

Thanks!
-Lukas

#1 lore raw

RFC PATCH 1/1 ext4: Try to better reuse recently freed space

Currently if the block allocator can not find the goal to allocate we would use global goal for stream allocation. However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:

- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop. This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try to use the proper more specific goal which is based on inode position.

---
 fs/ext4/ext4.h    |  7 ++++---
 fs/ext4/inode.c   |  8 ++++++++
 fs/ext4/mballoc.c | 20 ++++++++------------
 3 files changed, 20 insertions(+), 15 deletions(-)
fs/ext4/ext4.h +4 -3
index 6ed348d..4dffa92 100644
@@ -917,6 +917,10 @@ struct ext4_inode_info {
 
 	/* Precomputed uuid+inum+igen checksum for seeding inode checksums */
 	__u32 i_csum_seed;
+
+	/* where last allocation was done - for stream allocation */
+	unsigned long i_last_group;
+	unsigned long i_last_start;
 };
 
 /*
@@ -1242,9 +1246,6 @@ struct ext4_sb_info {
 	unsigned int s_mb_order2_reqs;
 	unsigned int s_mb_group_prealloc;
 	unsigned int s_max_dir_size_kb;
-	/* where last allocation was done - for stream allocation */
-	unsigned long s_mb_last_group;
-	unsigned long s_mb_last_start;
 
 	/* stats for buddy allocator */
 	atomic_t s_bal_reqs;	/* number of reqs with len > 1 */
fs/ext4/inode.c +8 -0
index 0188e65..07d0434 100644
@@ -3702,6 +3702,10 @@ void ext4_truncate(struct inode *inode)
 	else
 		ext4_ind_truncate(handle, inode);
 
+	/* Invalidate last allocation counters */
+	ei->i_last_group = UINT_MAX;
+	ei->i_last_start = UINT_MAX;
+
 	up_write(&ei->i_data_sem);
 
 	if (IS_SYNC(inode))
@@ -4060,6 +4064,10 @@ struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
 	ei->i_block_group = iloc.block_group;
 	ei->i_last_alloc_group = ~0;
+
+	/* Invalidate last allocation counters */
+	ei->i_last_group = UINT_MAX;
+	ei->i_last_start = UINT_MAX;
 	/*
 	 * NOTE! The in-memory inode i_data array is in little-endian order
 	 * even on big-endian machines: we do NOT byteswap the block numbers!
fs/ext4/mballoc.c +8 -12
index a9ff5e5..6c23666 100644
@@ -1591,7 +1591,6 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 					struct ext4_buddy *e4b)
 {
-	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
 	int ret;
 
 	BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
@@ -1622,10 +1621,8 @@ static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 	get_page(ac->ac_buddy_page);
 	/* store last allocated for subsequent stream allocation */
 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		spin_lock(&sbi->s_md_lock);
-		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
-		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
-		spin_unlock(&sbi->s_md_lock);
+		EXT4_I(ac->ac_inode)->i_last_group = ac->ac_f_ex.fe_group;
+		EXT4_I(ac->ac_inode)->i_last_start = ac->ac_f_ex.fe_start;
 	}
 }
 
@@ -2080,13 +2077,12 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 			ac->ac_2order = i - 1;
 	}
 
-	/* if stream allocation is enabled, use global goal */
-	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		/* TBD: may be hot point */
-		spin_lock(&sbi->s_md_lock);
-		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
-		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
-		spin_unlock(&sbi->s_md_lock);
+	/* if stream allocation is enabled and per inode goal is
+	 * set, use it */
+	if ((ac->ac_flags & EXT4_MB_STREAM_ALLOC) &&
+	   (EXT4_I(ac->ac_inode)->i_last_start != UINT_MAX)) {
+		ac->ac_g_ex.fe_group = EXT4_I(ac->ac_inode)->i_last_group;
+		ac->ac_g_ex.fe_start = EXT4_I(ac->ac_inode)->i_last_start;
 	}
 
 	/* Let's just scan groups to find more-less suitable blocks */
Signature · 1 line
1.8.3.1
reviewer
#2 lore raw
Quoted 23 lines · Lukas Czerner

Currently if the block allocator can not find the goal to allocate we would use global goal for stream allocation. However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:

- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop. This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Quoted 7 lines

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try to use the proper more specific goal which is based on inode position.

In principle the patch looks fine to me. Just minor style nit below:

Quoted 13 lines
---
 fs/ext4/ext4.h    |  7 ++++---
 fs/ext4/inode.c   |  8 ++++++++
 fs/ext4/mballoc.c | 20 ++++++++------------
 3 files changed, 20 insertions(+), 15 deletions(-)
fs/ext4/ext4.h +0 -0
index 6ed348d..4dffa92 100644

fs/ext4/ext4.h struct ext4_inode_info { · line 923

Quoted 7 lines
fs/ext4/ext4.h +4 -0
@@ -917,6 +917,10 @@ struct ext4_inode_info {
 
 	/* Precomputed uuid+inum+igen checksum for seeding inode checksums */
 	__u32 i_csum_seed;
+
+	/* where last allocation was done - for stream allocation */
+	unsigned long i_last_group;
+	unsigned long i_last_start;

Can we use proper types here please? I think they should be
ext4_grpblk_t and ext4_group_t. And maybe we should define something like EXT4_INVAL_GRPNO and use that instead of UINT_MAX.

Honza

Quoted 18 lines
fs/ext4/ext4.h +0 -3
 };
 
 /*
@@ -1242,9 +1246,6 @@ struct ext4_sb_info {
 	unsigned int s_mb_order2_reqs;
 	unsigned int s_mb_group_prealloc;
 	unsigned int s_max_dir_size_kb;
-	/* where last allocation was done - for stream allocation */
-	unsigned long s_mb_last_group;
-	unsigned long s_mb_last_start;
 
 	/* stats for buddy allocator */
 	atomic_t s_bal_reqs;	/* number of reqs with len > 1 */
fs/ext4/inode.c +0 -0
index 0188e65..07d0434 100644

fs/ext4/inode.c void ext4_truncate(struct inode *inode) · line 3708

Quoted 7 lines
fs/ext4/inode.c +4 -0
@@ -3702,6 +3702,10 @@ void ext4_truncate(struct inode *inode)
 	else
 		ext4_ind_truncate(handle, inode);
 
+	/* Invalidate last allocation counters */
+	ei->i_last_group = UINT_MAX;
+	ei->i_last_start = UINT_MAX;
+

And

Quoted 66 lines
fs/ext4/inode.c +4 -0
 	up_write(&ei->i_data_sem);
 
 	if (IS_SYNC(inode))
@@ -4060,6 +4064,10 @@ struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
 	ei->i_block_group = iloc.block_group;
 	ei->i_last_alloc_group = ~0;
+
+	/* Invalidate last allocation counters */
+	ei->i_last_group = UINT_MAX;
+	ei->i_last_start = UINT_MAX;
 	/*
 	 * NOTE! The in-memory inode i_data array is in little-endian order
 	 * even on big-endian machines: we do NOT byteswap the block numbers!
fs/ext4/mballoc.c +8 -12
index a9ff5e5..6c23666 100644
@@ -1591,7 +1591,6 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 					struct ext4_buddy *e4b)
 {
-	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
 	int ret;
 
 	BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
@@ -1622,10 +1621,8 @@ static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 	get_page(ac->ac_buddy_page);
 	/* store last allocated for subsequent stream allocation */
 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		spin_lock(&sbi->s_md_lock);
-		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
-		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
-		spin_unlock(&sbi->s_md_lock);
+		EXT4_I(ac->ac_inode)->i_last_group = ac->ac_f_ex.fe_group;
+		EXT4_I(ac->ac_inode)->i_last_start = ac->ac_f_ex.fe_start;
 	}
 }
 
@@ -2080,13 +2077,12 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 			ac->ac_2order = i - 1;
 	}
 
-	/* if stream allocation is enabled, use global goal */
-	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		/* TBD: may be hot point */
-		spin_lock(&sbi->s_md_lock);
-		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
-		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
-		spin_unlock(&sbi->s_md_lock);
+	/* if stream allocation is enabled and per inode goal is
+	 * set, use it */
+	if ((ac->ac_flags & EXT4_MB_STREAM_ALLOC) &&
+	   (EXT4_I(ac->ac_inode)->i_last_start != UINT_MAX)) {
+		ac->ac_g_ex.fe_group = EXT4_I(ac->ac_inode)->i_last_group;
+		ac->ac_g_ex.fe_start = EXT4_I(ac->ac_inode)->i_last_start;
 	}
 
 	/* Let's just scan groups to find more-less suitable blocks */
Signature · 6 lines
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw
Quoted 55 lines · Jan Kara

Date: Thu, 4 Jul 2013 17:09:05 +0200
From: Jan Kara <[email protected]>
To: Lukas Czerner <[email protected]>
Cc: [email protected], [email protected],
[email protected], [email protected], [email protected]
Subject: Re: [RFC PATCH 1/1] ext4: Try to better reuse recently freed space

Quoted 23 lines · Lukas Czerner

Currently if the block allocator can not find the goal to allocate we would use global goal for stream allocation. However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:

- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop. This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Quoted 7 lines

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try to use the proper more specific goal which is based on inode position.

In principle the patch looks fine to me. Just minor style nit below:

Quoted 14 lines
---
 fs/ext4/ext4.h    |  7 ++++---
 fs/ext4/inode.c   |  8 ++++++++
 fs/ext4/mballoc.c | 20 ++++++++------------
 3 files changed, 20 insertions(+), 15 deletions(-)
fs/ext4/ext4.h +0 -0
index 6ed348d..4dffa92 100644
@@ -917,6 +917,10 @@ struct ext4_inode_info {
 
Quoted 9 lines

fs/ext4/ext4.h struct ext4_inode_info { · line 923

Quoted 6 lines
fs/ext4/ext4.h +4 -0
 	/* Precomputed uuid+inum+igen checksum for seeding inode checksums */
 	__u32 i_csum_seed;
+
+	/* where last allocation was done - for stream allocation */
+	unsigned long i_last_group;
+	unsigned long i_last_start;

Can we use proper types here please? I think they should be
ext4_grpblk_t and ext4_group_t. And maybe we should define something like EXT4_INVAL_GRPNO and use that instead of UINT_MAX.

Sure, it'll look better with named types and defined invalidate value. I'll resend the patch soon.

Thanks!
-Lukas

Quoted 95 lines

Honza

Quoted 18 lines
fs/ext4/ext4.h +0 -3
 };
 
 /*
@@ -1242,9 +1246,6 @@ struct ext4_sb_info {
 	unsigned int s_mb_order2_reqs;
 	unsigned int s_mb_group_prealloc;
 	unsigned int s_max_dir_size_kb;
-	/* where last allocation was done - for stream allocation */
-	unsigned long s_mb_last_group;
-	unsigned long s_mb_last_start;
 
 	/* stats for buddy allocator */
 	atomic_t s_bal_reqs;	/* number of reqs with len > 1 */
fs/ext4/inode.c +0 -0
index 0188e65..07d0434 100644

fs/ext4/inode.c void ext4_truncate(struct inode *inode) · line 3708

Quoted 7 lines
fs/ext4/inode.c +4 -0
@@ -3702,6 +3702,10 @@ void ext4_truncate(struct inode *inode)
 	else
 		ext4_ind_truncate(handle, inode);
 
+	/* Invalidate last allocation counters */
+	ei->i_last_group = UINT_MAX;
+	ei->i_last_start = UINT_MAX;
+

And

Quoted 66 lines
fs/ext4/inode.c +4 -0
 	up_write(&ei->i_data_sem);
 
 	if (IS_SYNC(inode))
@@ -4060,6 +4064,10 @@ struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
 	ei->i_block_group = iloc.block_group;
 	ei->i_last_alloc_group = ~0;
+
+	/* Invalidate last allocation counters */
+	ei->i_last_group = UINT_MAX;
+	ei->i_last_start = UINT_MAX;
 	/*
 	 * NOTE! The in-memory inode i_data array is in little-endian order
 	 * even on big-endian machines: we do NOT byteswap the block numbers!
fs/ext4/mballoc.c +8 -12
index a9ff5e5..6c23666 100644
@@ -1591,7 +1591,6 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 					struct ext4_buddy *e4b)
 {
-	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
 	int ret;
 
 	BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
@@ -1622,10 +1621,8 @@ static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 	get_page(ac->ac_buddy_page);
 	/* store last allocated for subsequent stream allocation */
 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		spin_lock(&sbi->s_md_lock);
-		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
-		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
-		spin_unlock(&sbi->s_md_lock);
+		EXT4_I(ac->ac_inode)->i_last_group = ac->ac_f_ex.fe_group;
+		EXT4_I(ac->ac_inode)->i_last_start = ac->ac_f_ex.fe_start;
 	}
 }
 
@@ -2080,13 +2077,12 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 			ac->ac_2order = i - 1;
 	}
 
-	/* if stream allocation is enabled, use global goal */
-	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		/* TBD: may be hot point */
-		spin_lock(&sbi->s_md_lock);
-		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
-		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
-		spin_unlock(&sbi->s_md_lock);
+	/* if stream allocation is enabled and per inode goal is
+	 * set, use it */
+	if ((ac->ac_flags & EXT4_MB_STREAM_ALLOC) &&
+	   (EXT4_I(ac->ac_inode)->i_last_start != UINT_MAX)) {
+		ac->ac_g_ex.fe_group = EXT4_I(ac->ac_inode)->i_last_group;
+		ac->ac_g_ex.fe_start = EXT4_I(ac->ac_inode)->i_last_start;
 	}
 
 	/* Let's just scan groups to find more-less suitable blocks */
Signature · 6 lines
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw

Thank you very much for looking into this issue Lukas!

Ric,
What are the odds of getting a patch as a hot fix or beta for a proof of concept? The deadline is 7/15/2013 and this may eliminate the biggest remaining issue.

Thanks again and best regards,

Mario

Quoted 152 lines

-----Original Message-----
From: Lukáš Czerner [mailto:[email protected]]
Sent: Thursday, July 04, 2013 10:33 AM
To: Jan Kara
Cc: [email protected]; [email protected]; Gallegos, Mario; Hargrave, Jordan; [email protected]
Subject: Re: [RFC PATCH 1/1] ext4: Try to better reuse recently freed space

Quoted 50 lines · Jan Kara

Date: Thu, 4 Jul 2013 17:09:05 +0200
From: Jan Kara <[email protected]>
To: Lukas Czerner <[email protected]>
Cc: [email protected], [email protected],
[email protected], [email protected], [email protected]
Subject: Re: [RFC PATCH 1/1] ext4: Try to better reuse recently freed space

Quoted 23 lines · Lukas Czerner

Currently if the block allocator can not find the goal to allocate we would use global goal for stream allocation. However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:

- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop. This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Quoted 7 lines

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try to use the proper more specific goal which is based on inode position.

In principle the patch looks fine to me. Just minor style nit below:

Quoted 8 lines
---
 fs/ext4/ext4.h    |  7 ++++---
 fs/ext4/inode.c   |  8 ++++++++
 fs/ext4/mballoc.c | 20 ++++++++------------
 3 files changed, 20 insertions(+), 15 deletions(-)
Quoted 7 lines

fs/ext4/ext4.h struct ext4_inode_info { · line 918

Quoted 7 lines
fs/ext4/ext4.h index 6ed348d..4dffa92 renamed from fs/ext4/ext4.h +0 -0

100644

fs/ext4/ext4.h +0 -0
@@ -917,6 +917,10 @@ struct ext4_inode_info {
 
 	/* Precomputed uuid+inum+igen checksum for seeding inode

checksums */

Quoted 8 lines

fs/ext4/ext4.h struct ext4_inode_info { · line 923

Quoted 5 lines
fs/ext4/ext4.h +4 -0
 	__u32 i_csum_seed;
+
+	/* where last allocation was done - for stream allocation */
+	unsigned long i_last_group;
+	unsigned long i_last_start;

Can we use proper types here please? I think they should be
ext4_grpblk_t and ext4_group_t. And maybe we should define something like EXT4_INVAL_GRPNO and use that instead of UINT_MAX.

Sure, it'll look better with named types and defined invalidate value. I'll resend the patch soon.

Thanks!
-Lukas

Quoted 28 lines

Honza

Quoted 18 lines
fs/ext4/ext4.h +0 -3
 };
 
 /*
@@ -1242,9 +1246,6 @@ struct ext4_sb_info {
 	unsigned int s_mb_order2_reqs;
 	unsigned int s_mb_group_prealloc;
 	unsigned int s_max_dir_size_kb;
-	/* where last allocation was done - for stream allocation */
-	unsigned long s_mb_last_group;
-	unsigned long s_mb_last_start;
 
 	/* stats for buddy allocator */
 	atomic_t s_bal_reqs;	/* number of reqs with len > 1 */
fs/ext4/inode.c index renamed from fs/ext4/inode.c +0 -0

0188e65..07d0434 100644

fs/ext4/inode.c void ext4_truncate(struct inode *inode) · line 3708

Quoted 7 lines
fs/ext4/inode.c +4 -0
@@ -3702,6 +3702,10 @@ void ext4_truncate(struct inode *inode)
 	else
 		ext4_ind_truncate(handle, inode);
 
+	/* Invalidate last allocation counters */
+	ei->i_last_group = UINT_MAX;
+	ei->i_last_start = UINT_MAX;
+

And

Quoted 4 lines

fs/ext4/inode.c struct inode *ext4_iget(struct super_block

Quoted 4 lines
fs/ext4/inode.c +0 -0
 	up_write(&ei->i_data_sem);
 
 	if (IS_SYNC(inode))
@@ -4060,6 +4064,10 @@ struct inode *ext4_iget(struct super_block

*sb, unsigned long ino)

Quoted 10 lines

fs/ext4/inode.c struct inode *ext4_iget(struct super_block *sb, unsigned long ino)

Quoted 10 lines
fs/ext4/inode.c +4 -0
 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
 	ei->i_block_group = iloc.block_group;
 	ei->i_last_alloc_group = ~0;
+
+	/* Invalidate last allocation counters */
+	ei->i_last_group = UINT_MAX;
+	ei->i_last_start = UINT_MAX;
 	/*
 	 * NOTE! The in-memory inode i_data array is in little-endian order
 	 * even on big-endian machines: we do NOT byteswap the block

numbers!

Quoted 6 lines

fs/ext4/mballoc.c static int mb_mark_used(struct ext4_buddy · line 1591

Quoted 6 lines
fs/ext4/mballoc.c index renamed from fs/ext4/mballoc.c +0 -0

a9ff5e5..6c23666 100644

fs/ext4/mballoc.c +0 -0
@@ -1591,7 +1591,6 @@ static int mb_mark_used(struct ext4_buddy
 *e4b, struct ext4_free_extent *ex)  static void

ext4_mb_use_best_found(struct ext4_allocation_context *ac,

Quoted 6 lines

fs/ext4/mballoc.c static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)

Quoted 6 lines
fs/ext4/mballoc.c +0 -1
 					struct ext4_buddy *e4b)
 {
-	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
 	int ret;
 
 	BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group); @@ -1622,10

+1621,8

Quoted 1 line
Quoted 1 line

@@ static void ext4_mb_use_best_found(struct

ext4_allocation_context *ac,

Quoted 9 lines

fs/ext4/mballoc.c static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,

Quoted 8 lines
fs/ext4/mballoc.c +1 -4
 	get_page(ac->ac_buddy_page);
 	/* store last allocated for subsequent stream allocation */
 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		spin_lock(&sbi->s_md_lock);
-		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
-		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
-		spin_unlock(&sbi->s_md_lock);
+		EXT4_I(ac->ac_inode)->i_last_group = ac-

ac_f_ex.fe_group;

Quoted 34 lines
Quoted 5 lines

fs/ext4/mballoc.c ext4_mb_regular_allocator(struct

Quoted 5 lines
fs/ext4/mballoc.c +1 -0
+		EXT4_I(ac->ac_inode)->i_last_start = ac->ac_f_ex.fe_start;
 	}
 }
@@ -2080,13 +2077,12 @@ ext4_mb_regular_allocator(struct

ext4_allocation_context *ac)

Quoted 28 lines
Quoted 5 lines
fs/ext4/mballoc.c +0 -2
 			ac->ac_2order = i - 1;
 	}
 
-	/* if stream allocation is enabled, use global goal */
-	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {

fs/ext4/mballoc.c ext4_mb_regular_allocator(struct ext4_allocation_context *ac)

Quoted 10 lines
fs/ext4/mballoc.c +5 -5
-		/* TBD: may be hot point */
-		spin_lock(&sbi->s_md_lock);
-		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
-		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
-		spin_unlock(&sbi->s_md_lock);
+	/* if stream allocation is enabled and per inode goal is
+	 * set, use it */
+	if ((ac->ac_flags & EXT4_MB_STREAM_ALLOC) &&
+	   (EXT4_I(ac->ac_inode)->i_last_start != UINT_MAX)) {
+		ac->ac_g_ex.fe_group = EXT4_I(ac->ac_inode)-

i_last_group;

Quoted 11 lines
fs/ext4/mballoc.c +1 -1
+		ac->ac_g_ex.fe_start = EXT4_I(ac->ac_inode)->i_last_start;
 	}
 
 	/* Let's just scan groups to find more-less suitable blocks */
--

1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

PATCH v2 1/1 ext4: Try to better reuse recently freed space

In reply to Lukáš Czerner

Currently if the block allocator can not find the goal to allocate we would use global goal for stream allocation. However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:

- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop. This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try to use the proper more specific goal which is based on inode position.

---
v2: Use proper types. Define invalid value

 fs/ext4/ext4.h    | 12 +++++++++---
 fs/ext4/inode.c   |  8 ++++++++
 fs/ext4/mballoc.c | 20 ++++++++------------
 3 files changed, 25 insertions(+), 15 deletions(-)
fs/ext4/ext4.h +9 -3
index 6ed348d..d3e9d10 100644
@@ -917,9 +917,18 @@ struct ext4_inode_info {
 
 	/* Precomputed uuid+inum+igen checksum for seeding inode checksums */
 	__u32 i_csum_seed;
+
+	/* Where last allocation was done - for stream allocation */
+	ext4_group_t i_last_group;
+	ext4_grpblk_t i_last_start;
 };
 
 /*
+ * Invalid value for last allocation pointer
+ */
+#define EXT4_INVAL_GRPNO		UINT_MAX
+
+/*
  * File system states
  */
 #define	EXT4_VALID_FS			0x0001	/* Unmounted cleanly */
@@ -1242,9 +1251,6 @@ struct ext4_sb_info {
 	unsigned int s_mb_order2_reqs;
 	unsigned int s_mb_group_prealloc;
 	unsigned int s_max_dir_size_kb;
-	/* where last allocation was done - for stream allocation */
-	unsigned long s_mb_last_group;
-	unsigned long s_mb_last_start;
 
 	/* stats for buddy allocator */
 	atomic_t s_bal_reqs;	/* number of reqs with len > 1 */
fs/ext4/inode.c +8 -0
index 0188e65..8d3e67c 100644
@@ -3702,6 +3702,10 @@ void ext4_truncate(struct inode *inode)
 	else
 		ext4_ind_truncate(handle, inode);
 
+	/* Invalidate last allocation pointers */
+	ei->i_last_group = EXT4_INVAL_GRPNO;
+	ei->i_last_start = 0;
+
 	up_write(&ei->i_data_sem);
 
 	if (IS_SYNC(inode))
@@ -4060,6 +4064,10 @@ struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
 	ei->i_block_group = iloc.block_group;
 	ei->i_last_alloc_group = ~0;
+
+	/* Invalidate last allocation counters */
+	ei->i_last_group = EXT4_INVAL_GRPNO;
+	ei->i_last_start = 0;
 	/*
 	 * NOTE! The in-memory inode i_data array is in little-endian order
 	 * even on big-endian machines: we do NOT byteswap the block numbers!
fs/ext4/mballoc.c +8 -12
index a9ff5e5..7609f77 100644
@@ -1591,7 +1591,6 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 					struct ext4_buddy *e4b)
 {
-	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
 	int ret;
 
 	BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
@@ -1622,10 +1621,8 @@ static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 	get_page(ac->ac_buddy_page);
 	/* store last allocated for subsequent stream allocation */
 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		spin_lock(&sbi->s_md_lock);
-		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
-		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
-		spin_unlock(&sbi->s_md_lock);
+		EXT4_I(ac->ac_inode)->i_last_group = ac->ac_f_ex.fe_group;
+		EXT4_I(ac->ac_inode)->i_last_start = ac->ac_f_ex.fe_start;
 	}
 }
 
@@ -2080,13 +2077,12 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 			ac->ac_2order = i - 1;
 	}
 
-	/* if stream allocation is enabled, use global goal */
-	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		/* TBD: may be hot point */
-		spin_lock(&sbi->s_md_lock);
-		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
-		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
-		spin_unlock(&sbi->s_md_lock);
+	/* if stream allocation is enabled and per inode goal is
+	 * set, use it */
+	if ((ac->ac_flags & EXT4_MB_STREAM_ALLOC) &&
+	   (EXT4_I(ac->ac_inode)->i_last_group != EXT4_INVAL_GRPNO)) {
+		ac->ac_g_ex.fe_group = EXT4_I(ac->ac_inode)->i_last_group;
+		ac->ac_g_ex.fe_start = EXT4_I(ac->ac_inode)->i_last_start;
 	}
 
 	/* Let's just scan groups to find more-less suitable blocks */
Signature · 1 line
1.8.3.1
reviewer
#6 lore raw
Quoted 23 lines · Lukas Czerner

Currently if the block allocator can not find the goal to allocate we would use global goal for stream allocation. However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:

- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop. This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Quoted 7 lines

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try to use the proper more specific goal which is based on inode position.

When looking at your patch for second time, I started wondering, whether
we need per-inode stream goal at all. We already do set goal in the allocation request for mballoc (ar->goal) e.g. in ext4_ext_find_goal(). It seems strange to then reset it inside mballoc and I don't even think mballoc will change it to something else now when the goal is per-inode and not global.

Which makes me think again about why someone introduced global goal for stream allocations in the first place? Maybe the motivation was that at each moment we will have only one area from where we allocate streaming data (large chunks of blocks) which may presumably reduce fragmentation. I've added Andreas to CC, maybe he'll remember the purpose.

Honza

Quoted 116 lines

---
v2: Use proper types. Define invalid value

 fs/ext4/ext4.h    | 12 +++++++++---
 fs/ext4/inode.c   |  8 ++++++++
 fs/ext4/mballoc.c | 20 ++++++++------------
 3 files changed, 25 insertions(+), 15 deletions(-)
fs/ext4/ext4.h +9 -3
index 6ed348d..d3e9d10 100644
@@ -917,9 +917,18 @@ struct ext4_inode_info {
 
 	/* Precomputed uuid+inum+igen checksum for seeding inode checksums */
 	__u32 i_csum_seed;
+
+	/* Where last allocation was done - for stream allocation */
+	ext4_group_t i_last_group;
+	ext4_grpblk_t i_last_start;
 };
 
 /*
+ * Invalid value for last allocation pointer
+ */
+#define EXT4_INVAL_GRPNO		UINT_MAX
+
+/*
  * File system states
  */
 #define	EXT4_VALID_FS			0x0001	/* Unmounted cleanly */
@@ -1242,9 +1251,6 @@ struct ext4_sb_info {
 	unsigned int s_mb_order2_reqs;
 	unsigned int s_mb_group_prealloc;
 	unsigned int s_max_dir_size_kb;
-	/* where last allocation was done - for stream allocation */
-	unsigned long s_mb_last_group;
-	unsigned long s_mb_last_start;
 
 	/* stats for buddy allocator */
 	atomic_t s_bal_reqs;	/* number of reqs with len > 1 */
fs/ext4/inode.c +8 -0
index 0188e65..8d3e67c 100644
@@ -3702,6 +3702,10 @@ void ext4_truncate(struct inode *inode)
 	else
 		ext4_ind_truncate(handle, inode);
 
+	/* Invalidate last allocation pointers */
+	ei->i_last_group = EXT4_INVAL_GRPNO;
+	ei->i_last_start = 0;
+
 	up_write(&ei->i_data_sem);
 
 	if (IS_SYNC(inode))
@@ -4060,6 +4064,10 @@ struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
 	ei->i_block_group = iloc.block_group;
 	ei->i_last_alloc_group = ~0;
+
+	/* Invalidate last allocation counters */
+	ei->i_last_group = EXT4_INVAL_GRPNO;
+	ei->i_last_start = 0;
 	/*
 	 * NOTE! The in-memory inode i_data array is in little-endian order
 	 * even on big-endian machines: we do NOT byteswap the block numbers!
fs/ext4/mballoc.c +8 -12
index a9ff5e5..7609f77 100644
@@ -1591,7 +1591,6 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 					struct ext4_buddy *e4b)
 {
-	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
 	int ret;
 
 	BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
@@ -1622,10 +1621,8 @@ static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 	get_page(ac->ac_buddy_page);
 	/* store last allocated for subsequent stream allocation */
 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		spin_lock(&sbi->s_md_lock);
-		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
-		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
-		spin_unlock(&sbi->s_md_lock);
+		EXT4_I(ac->ac_inode)->i_last_group = ac->ac_f_ex.fe_group;
+		EXT4_I(ac->ac_inode)->i_last_start = ac->ac_f_ex.fe_start;
 	}
 }
 
@@ -2080,13 +2077,12 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 			ac->ac_2order = i - 1;
 	}
 
-	/* if stream allocation is enabled, use global goal */
-	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		/* TBD: may be hot point */
-		spin_lock(&sbi->s_md_lock);
-		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
-		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
-		spin_unlock(&sbi->s_md_lock);
+	/* if stream allocation is enabled and per inode goal is
+	 * set, use it */
+	if ((ac->ac_flags & EXT4_MB_STREAM_ALLOC) &&
+	   (EXT4_I(ac->ac_inode)->i_last_group != EXT4_INVAL_GRPNO)) {
+		ac->ac_g_ex.fe_group = EXT4_I(ac->ac_inode)->i_last_group;
+		ac->ac_g_ex.fe_start = EXT4_I(ac->ac_inode)->i_last_start;
 	}
 
 	/* Let's just scan groups to find more-less suitable blocks */
Signature · 1 line
1.8.3.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#7 lore raw
Quoted 38 lines · Jan Kara

Date: Mon, 8 Jul 2013 10:56:03 +0200
From: Jan Kara <[email protected]>
To: Lukas Czerner <[email protected]>
Cc: [email protected], [email protected],
Andreas Dilger <[email protected]>
Subject: Re: [PATCH v2] ext4: Try to better reuse recently freed space

Quoted 23 lines · Lukas Czerner

Currently if the block allocator can not find the goal to allocate we would use global goal for stream allocation. However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:

- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop. This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Quoted 7 lines

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try to use the proper more specific goal which is based on inode position.

Quoted 6 lines

When looking at your patch for second time, I started wondering, whether
we need per-inode stream goal at all. We already do set goal in the allocation request for mballoc (ar->goal) e.g. in ext4_ext_find_goal(). It seems strange to then reset it inside mballoc and I don't even think mballoc will change it to something else now when the goal is per-inode and not global.

Yes, we do set the goal in the allocation request and it is supposed to be the "best" goal. However sometimes it can not be fulfilled because we do not have any free block at "goal".

That's when the global (or per-inode) goal comes into play. I suppose that there was several reasons for that. First of all it makes it easier for allocator, because it can directly jump at the point where we allocated last time and it is likely that there is some free space to allocate from - so the benefit is that we do not have to walk all the space in between which is likely to be allocated.

The second might be to keep new allocation together.

This is also what is creating the problem which this patch is trying to fix. We assume that when the allocation request goal can not be fulfilled then all the space between that goal and the global goal is allocated so we jump directly to the global goal. However it might not be the case at all.

With per-inode goal we can jump directly to the point we allocated last time for given inode and start searching free space from there in the case that allocation request goal can not be fulfilled for some reason. Also we're able to invalidate that goal when we truncate the file so we can start searching from the allocation request goal.

Obviously the per-inode goal does not have all the benefits of the global goal, because we're less likely to have free space right after per-inode goal then after global goal, but it does not have that nasty side-effect of touching all the block in the file system unnecessarily, getting more and more distant from the allocation request goal even though it's not always needed, and unnecessarily pushing the allocation to the slower parts of the disk in some cases.

-Lukas

Quoted 126 lines

Which makes me think again about why someone introduced global goal for stream allocations in the first place? Maybe the motivation was that at each moment we will have only one area from where we allocate streaming data (large chunks of blocks) which may presumably reduce fragmentation. I've added Andreas to CC, maybe he'll remember the purpose.

Honza

Quoted 116 lines

---
v2: Use proper types. Define invalid value

 fs/ext4/ext4.h    | 12 +++++++++---
 fs/ext4/inode.c   |  8 ++++++++
 fs/ext4/mballoc.c | 20 ++++++++------------
 3 files changed, 25 insertions(+), 15 deletions(-)
fs/ext4/ext4.h +9 -3
index 6ed348d..d3e9d10 100644
@@ -917,9 +917,18 @@ struct ext4_inode_info {
 
 	/* Precomputed uuid+inum+igen checksum for seeding inode checksums */
 	__u32 i_csum_seed;
+
+	/* Where last allocation was done - for stream allocation */
+	ext4_group_t i_last_group;
+	ext4_grpblk_t i_last_start;
 };
 
 /*
+ * Invalid value for last allocation pointer
+ */
+#define EXT4_INVAL_GRPNO		UINT_MAX
+
+/*
  * File system states
  */
 #define	EXT4_VALID_FS			0x0001	/* Unmounted cleanly */
@@ -1242,9 +1251,6 @@ struct ext4_sb_info {
 	unsigned int s_mb_order2_reqs;
 	unsigned int s_mb_group_prealloc;
 	unsigned int s_max_dir_size_kb;
-	/* where last allocation was done - for stream allocation */
-	unsigned long s_mb_last_group;
-	unsigned long s_mb_last_start;
 
 	/* stats for buddy allocator */
 	atomic_t s_bal_reqs;	/* number of reqs with len > 1 */
fs/ext4/inode.c +8 -0
index 0188e65..8d3e67c 100644
@@ -3702,6 +3702,10 @@ void ext4_truncate(struct inode *inode)
 	else
 		ext4_ind_truncate(handle, inode);
 
+	/* Invalidate last allocation pointers */
+	ei->i_last_group = EXT4_INVAL_GRPNO;
+	ei->i_last_start = 0;
+
 	up_write(&ei->i_data_sem);
 
 	if (IS_SYNC(inode))
@@ -4060,6 +4064,10 @@ struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
 	ei->i_block_group = iloc.block_group;
 	ei->i_last_alloc_group = ~0;
+
+	/* Invalidate last allocation counters */
+	ei->i_last_group = EXT4_INVAL_GRPNO;
+	ei->i_last_start = 0;
 	/*
 	 * NOTE! The in-memory inode i_data array is in little-endian order
 	 * even on big-endian machines: we do NOT byteswap the block numbers!
fs/ext4/mballoc.c +8 -12
index a9ff5e5..7609f77 100644
@@ -1591,7 +1591,6 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 					struct ext4_buddy *e4b)
 {
-	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
 	int ret;
 
 	BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
@@ -1622,10 +1621,8 @@ static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
 	get_page(ac->ac_buddy_page);
 	/* store last allocated for subsequent stream allocation */
 	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		spin_lock(&sbi->s_md_lock);
-		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
-		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
-		spin_unlock(&sbi->s_md_lock);
+		EXT4_I(ac->ac_inode)->i_last_group = ac->ac_f_ex.fe_group;
+		EXT4_I(ac->ac_inode)->i_last_start = ac->ac_f_ex.fe_start;
 	}
 }
 
@@ -2080,13 +2077,12 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 			ac->ac_2order = i - 1;
 	}
 
-	/* if stream allocation is enabled, use global goal */
-	if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
-		/* TBD: may be hot point */
-		spin_lock(&sbi->s_md_lock);
-		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
-		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
-		spin_unlock(&sbi->s_md_lock);
+	/* if stream allocation is enabled and per inode goal is
+	 * set, use it */
+	if ((ac->ac_flags & EXT4_MB_STREAM_ALLOC) &&
+	   (EXT4_I(ac->ac_inode)->i_last_group != EXT4_INVAL_GRPNO)) {
+		ac->ac_g_ex.fe_group = EXT4_I(ac->ac_inode)->i_last_group;
+		ac->ac_g_ex.fe_start = EXT4_I(ac->ac_inode)->i_last_start;
 	}
 
 	/* Let's just scan groups to find more-less suitable blocks */
Signature · 1 line
1.8.3.1
reviewer
#8 lore raw
Quoted 51 lines · Lukáš Czerner
Quoted 38 lines · Jan Kara

Date: Mon, 8 Jul 2013 10:56:03 +0200
From: Jan Kara <[email protected]>
To: Lukas Czerner <[email protected]>
Cc: [email protected], [email protected],
Andreas Dilger <[email protected]>
Subject: Re: [PATCH v2] ext4: Try to better reuse recently freed space

Quoted 23 lines · Lukas Czerner

Currently if the block allocator can not find the goal to allocate we would use global goal for stream allocation. However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:

- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop. This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Quoted 7 lines

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try to use the proper more specific goal which is based on inode position.

Quoted 6 lines

When looking at your patch for second time, I started wondering, whether
we need per-inode stream goal at all. We already do set goal in the allocation request for mballoc (ar->goal) e.g. in ext4_ext_find_goal(). It seems strange to then reset it inside mballoc and I don't even think mballoc will change it to something else now when the goal is per-inode and not global.

Yes, we do set the goal in the allocation request and it is supposed to be the "best" goal. However sometimes it can not be fulfilled because we do not have any free block at "goal".

Quoted 6 lines

That's when the global (or per-inode) goal comes into play. I suppose that there was several reasons for that. First of all it makes it easier for allocator, because it can directly jump at the point where we allocated last time and it is likely that there is some free space to allocate from - so the benefit is that we do not have to walk all the space in between which is likely to be allocated.

Yep, but my question is: If we have per-inode streaming goal, can you
show an example when the "best" goal will be different from the "streaming" goal? Because from a (I admit rather quick) look at how each of these is computed, it seems that both will point after the next allocated block in case of streaming IO.

Honza

Signature · 6 lines
Jan Kara <[email protected]>
SUSE Labs, CR
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#9 lore raw
Quoted 52 lines · Jan Kara
Quoted 44 lines · Lukáš Czerner
Quoted 31 lines · Jan Kara

On Mon, 8 Jul 2013, Jan Kara <[email protected]> wrote:

Quoted 23 lines · Lukas Czerner

Currently if the block allocator can not find the goal to
allocate we would use global goal for stream allocation.
However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:
- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop.
This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Quoted 6 lines

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try
to use the proper more specific goal which is based on inode
position.

Quoted 7 lines

When looking at your patch for second time, I started wondering, whether we need per-inode stream goal at all. We already do set goal in the allocation request for mballoc (ar->goal) e.g. in ext4_ext_find_goal().
It seems strange to then reset it inside mballoc and I don't even think mballoc will change it to something else now when the goal is per-inode and not global.

Yes, we do set the goal in the allocation request and it is supposed to be the "best" goal. However sometimes it can not be fulfilled because we do not have any free block at "goal".

Quoted 6 lines

That's when the global (or per-inode) goal comes into play. I suppose that there was several reasons for that. First of all it makes it easier for allocator, because it can directly jump at the point where we allocated last time and it is likely that there is some free space to allocate from - so the benefit is that we do not have to walk all the space in between which is likely to be allocated.

Quoted 5 lines

Yep, but my question is: If we have per-inode streaming goal, can you show an example when the "best" goal will be different from the "streaming" goal? Because from a (I admit rather quick) look at how each of these is computed, it seems that both will point after the next allocated block in case of streaming IO.

There were a few goals with the design of the mballoc allocator:
- keep large allocations sized and aligned on RAID chunks
- pack small allocations together, so they can fit into a single RAID chunk and avoid many read-modify-write cycles
- keep allocations relatively close together, so that seeking is minimized and it doesn't search through fragmented free space

It was designed for high-bandwidth streaming IO, and not small random IO as is seen with VM images and thin-provisioned storage. That said, with TRIM and sparse storage, there is no guarantee that offset within the virtual device has any association with the real disk, so I'm not sure it makes sense to optimize for this case.

The ext4 code will already bias new allocations towards the beginning of the disk because this is how inodes are allocated and this influences each inode's block allocation. The global target allows the large allocations to get out of the mess of fragmented free space and into other parts of the filesystem that are not as heavily used.

Rather than tweaking the global target (which is currently just a simple round-robin), it probably makes sense to look at a global extent map for free space, so that it is easier to find large chunks of free space. It is still desirable to keep these allocations closer together, otherwise the "optimum" location for several large allocations may be on different ends of the disk and the situation would be far worse than making periodic writes over the whole disk.

Cheers, Andreas

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
Quoted 67 lines · Andreas Dilger

Date: Mon, 8 Jul 2013 15:27:21 -0600
From: Andreas Dilger <[email protected]>
To: Jan Kara <[email protected]>
Cc: Lukáš Czerner <[email protected]>,
Alex Zhuravlev <[email protected]>, "[email protected] List" <[email protected]>
Subject: Re: [PATCH v2] ext4: Try to better reuse recently freed space

Quoted 52 lines · Jan Kara
Quoted 44 lines · Lukáš Czerner
Quoted 31 lines · Jan Kara

On Mon, 8 Jul 2013, Jan Kara <[email protected]> wrote:

Quoted 23 lines · Lukas Czerner

Currently if the block allocator can not find the goal to
allocate we would use global goal for stream allocation.
However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:
- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop.
This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Quoted 6 lines

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try
to use the proper more specific goal which is based on inode
position.

Quoted 7 lines

When looking at your patch for second time, I started wondering, whether we need per-inode stream goal at all. We already do set goal in the allocation request for mballoc (ar->goal) e.g. in ext4_ext_find_goal().
It seems strange to then reset it inside mballoc and I don't even think mballoc will change it to something else now when the goal is per-inode and not global.

Yes, we do set the goal in the allocation request and it is supposed to be the "best" goal. However sometimes it can not be fulfilled because we do not have any free block at "goal".

Quoted 6 lines

That's when the global (or per-inode) goal comes into play. I suppose that there was several reasons for that. First of all it makes it easier for allocator, because it can directly jump at the point where we allocated last time and it is likely that there is some free space to allocate from - so the benefit is that we do not have to walk all the space in between which is likely to be allocated.

Quoted 5 lines

Yep, but my question is: If we have per-inode streaming goal, can you show an example when the "best" goal will be different from the "streaming" goal? Because from a (I admit rather quick) look at how each of these is computed, it seems that both will point after the next allocated block in case of streaming IO.

Quoted 8 lines

There were a few goals with the design of the mballoc allocator:
- keep large allocations sized and aligned on RAID chunks
- pack small allocations together, so they can fit into a single RAID chunk and avoid many read-modify-write cycles
- keep allocations relatively close together, so that seeking is minimized and it doesn't search through fragmented free space

It was designed for high-bandwidth streaming IO, and not small random IO as is seen with VM images and thin-provisioned storage. That said, with TRIM and sparse storage, there is no guarantee that offset within the virtual device has any association with the real disk, so I'm not sure it makes sense to optimize for this case.

However it is used for small random IO as well. We're going to use EXT4_MB_STREAM_ALLOC every time when the file, or allocation is bigger than sbi->s_mb_stream_request. Are you saying that it was not designed that way ?

Moreover we do not really care about the association of the offset within the virtual device and the real disk. That's not the problem. The offset might only be a problem on real disk, where it might be getting slower as we move further into the file system, but that's also questionable in some cases.

However with thin-provisioning this might really be a problem because we would "provision" much more blocks that the file system really needs, because we're not reusing blocks which has been recently freed, but rather touching new block. This will unnecessarily drain thin-provisioning free space pool.

Quoted 4 lines

The ext4 code will already bias new allocations towards the beginning of the disk because this is how inodes are allocated and this influences each inode's block allocation. The global target allows the large allocations to get out of the mess of fragmented free space and into other parts of the filesystem that are not as heavily used.

Rather than tweaking the global target (which is currently just a simple round-robin), it probably makes sense to look at a global extent map for free space, so that it is easier to find large chunks of free space. It is still desirable to keep these allocations closer together, otherwise the "optimum" location for several large allocations may be on different ends of the disk and the situation would be far worse than making periodic writes over the whole disk.

I agre that having a global extent map for free space will be useful. But this is a different problem even though somewhat related.

Thanks!
-Lukas

Quoted 8 lines

Cheers, Andreas

#11 lore raw
In reply to Jan Kara
Quoted 65 lines · Jan Kara

Date: Mon, 8 Jul 2013 13:59:51 +0200
From: Jan Kara <[email protected]>
To: Lukáš Czerner <[email protected]>
Cc: Jan Kara <[email protected]>, [email protected],
Andreas Dilger <[email protected]>
Subject: Re: [PATCH v2] ext4: Try to better reuse recently freed space

Quoted 51 lines · Lukáš Czerner
Quoted 38 lines · Jan Kara

Date: Mon, 8 Jul 2013 10:56:03 +0200
From: Jan Kara <[email protected]>
To: Lukas Czerner <[email protected]>
Cc: [email protected], [email protected],
Andreas Dilger <[email protected]>
Subject: Re: [PATCH v2] ext4: Try to better reuse recently freed space

Quoted 23 lines · Lukas Czerner

Currently if the block allocator can not find the goal to allocate we would use global goal for stream allocation. However the global goal (s_mb_last_group and s_mb_last_start) will move further every time such allocation appears and never move backwards.

This causes several problems in certain scenarios:

- the goal will move further and further preventing us from reusing space which might have been freed since then. This is ok from the file system point of view because we will reuse that space eventually, however we're allocating block from slower parts of the spinning disk even though it might not be necessary.
- The above also causes more serious problem for example for thinly provisioned storage (sparse images backed storage as well), because instead of reusing blocks which are already provisioned we would try to use new blocks. This would unnecessarily drain storage free blocks pool.
- This will also cause blocks to be allocated further from the given goal than it's necessary. Consider for example truncating, or removing and rewriting the file in the loop. This workload will never reuse freed blocks until we continually claim and free all the block in the file system.

Quoted 7 lines

Note that file systems like xfs, ext3, or btrfs does not have this problem. This is simply caused by the notion of global pool.

Fix this by changing the global goal to be goal per inode. This will allow us to invalidate the goal every time the inode has been truncated, or newly created, so in those cases we would try to use the proper more specific goal which is based on inode position.

Quoted 6 lines

When looking at your patch for second time, I started wondering, whether
we need per-inode stream goal at all. We already do set goal in the allocation request for mballoc (ar->goal) e.g. in ext4_ext_find_goal(). It seems strange to then reset it inside mballoc and I don't even think mballoc will change it to something else now when the goal is per-inode and not global.

Yes, we do set the goal in the allocation request and it is supposed to be the "best" goal. However sometimes it can not be fulfilled because we do not have any free block at "goal".

Quoted 6 lines

That's when the global (or per-inode) goal comes into play. I suppose that there was several reasons for that. First of all it makes it easier for allocator, because it can directly jump at the point where we allocated last time and it is likely that there is some free space to allocate from - so the benefit is that we do not have to walk all the space in between which is likely to be allocated.

Quoted 5 lines

Yep, but my question is: If we have per-inode streaming goal, can you
show an example when the "best" goal will be different from the "streaming" goal? Because from a (I admit rather quick) look at how each of these is computed, it seems that both will point after the next allocated block in case of streaming IO.

EXT4_MB_STREAM_ALLOC or "streaming IO" is quite misleading name for what we have in ext4. It simply means that the file (or allocation) is bigger than certain threshold.

So I think that one example would be when writing in the middle of sparse file when other processes might have already allocated requested blocks. This might be the case for file system images for example. Also for some reason I am seeing this when writing into file system image even though there are no other processes allocating from that file system.

Simply hacking ext4 to print out the numbers when the goals differs and running xfstests shows that there are cases where it differs and where it helps to allocate from the per-inode goal.

-Lukas

Quoted 3 lines

Honza