Skip to contentSkip to conversation

f2fs: fix to avoid potential negative .f_bfree

PATCH In review Chao Yu 3 participants 6 messages Apr 26, 2019 – Apr 30, 2019 F2FS file system

lore
Share & tools
lore

Chao Yu

author
#0 lore raw

PATCH 1/2 f2fs: fix to avoid potential negative .f_bfree

When calculating .f_bfree value in f2fs_statfs(), sbi->unusable_block_count can be increased after the judgment condition, result in overflow of .f_bfree in later calculation. This patch fixes to use a temporary signed variable to save the calculation result of .f_bfree.

	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
 		buf->f_bfree = 0;
 	else
		buf->f_bfree -= sbi->unusable_block_count;
---
 fs/f2fs/super.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/f2fs/super.c +5 -2
index 2376bb01b5c4..fcc9793dbc2c 100644
@@ -1216,6 +1216,7 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
 	block_t total_count, user_block_count, start_count;
 	u64 avail_node_count;
+	long long bfree;
 
 	total_count = le64_to_cpu(sbi->raw_super->block_count);
 	user_block_count = sbi->user_block_count;
@@ -1226,10 +1227,12 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	buf->f_blocks = total_count - start_count;
 	buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
 						sbi->current_reserved_blocks;
-	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
+
+	bfree = buf->f_bfree - sbi->unusable_block_count;
+	if (unlikely(bfree < 0))
 		buf->f_bfree = 0;
 	else
-		buf->f_bfree -= sbi->unusable_block_count;
+		buf->f_bfree = bfree;
 
 	if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
 		buf->f_bavail = buf->f_bfree -
Signature · 1 line
2.18.0.rc1
author
#1 lore raw

PATCH 2/2 f2fs: fix to handle error in f2fs_disable_checkpoint()

In f2fs_disable_checkpoint(), it needs to detect and propagate error number returned from f2fs_write_checkpoint().

---
 fs/f2fs/super.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/f2fs/super.c +4 -1
index fcc9793dbc2c..ad0faa19cb69 100644
@@ -1507,9 +1507,12 @@ static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
 	mutex_lock(&sbi->gc_mutex);
 	cpc.reason = CP_PAUSE;
 	set_sbi_flag(sbi, SBI_CP_DISABLED);
-	f2fs_write_checkpoint(sbi, &cpc);
+	err = f2fs_write_checkpoint(sbi, &cpc);
+	if (err)
+		goto out_unlock;
 
 	sbi->unusable_block_count = 0;
+out_unlock:
 	mutex_unlock(&sbi->gc_mutex);
 restore_flag:
 	sbi->sb->s_flags = s_flags;	/* Restore MS_RDONLY status */
Signature · 1 line
2.18.0.rc1
maintainer
#2 lore raw
In reply to Chao Yu
Quoted 9 lines · Chao Yu

When calculating .f_bfree value in f2fs_statfs(), sbi->unusable_block_count can be increased after the judgment condition, result in overflow of .f_bfree in later calculation. This patch fixes to use a temporary signed variable to save the calculation result of .f_bfree.

	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
fs/f2fs/super.c +0 -0
 		buf->f_bfree = 0;
 	else
		buf->f_bfree -= sbi->unusable_block_count;

Do we just need stat_lock for this?

Quoted 35 lines
---
 fs/f2fs/super.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/f2fs/super.c +5 -2
index 2376bb01b5c4..fcc9793dbc2c 100644
@@ -1216,6 +1216,7 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
 	block_t total_count, user_block_count, start_count;
 	u64 avail_node_count;
+	long long bfree;
 
 	total_count = le64_to_cpu(sbi->raw_super->block_count);
 	user_block_count = sbi->user_block_count;
@@ -1226,10 +1227,12 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	buf->f_blocks = total_count - start_count;
 	buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
 						sbi->current_reserved_blocks;
-	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
+
+	bfree = buf->f_bfree - sbi->unusable_block_count;
+	if (unlikely(bfree < 0))
 		buf->f_bfree = 0;
 	else
-		buf->f_bfree -= sbi->unusable_block_count;
+		buf->f_bfree = bfree;
 
 	if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
 		buf->f_bavail = buf->f_bfree -
Signature · 1 line
2.18.0.rc1
maintainer
#3 lore raw
Quoted 6 lines · Jaegeuk Kim
Quoted 5 lines · Chao Yu

When calculating .f_bfree value in f2fs_statfs(), sbi->unusable_block_count can be increased after the judgment condition, result in overflow of .f_bfree in later calculation. This patch fixes to use a temporary signed variable to save the calculation result of .f_bfree.

Quoted 6 lines
Quoted 4 lines
	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
fs/f2fs/super.c +0 -0
 		buf->f_bfree = 0;
 	else
		buf->f_bfree -= sbi->unusable_block_count;

Do we just need stat_lock for this?

Like we access other stat value in statfs(), we just need the instantaneous value of .unusable_block_count, so we don't need additional stat_lock, right?

Thanks,

Quoted 36 lines
Quoted 36 lines
---
 fs/f2fs/super.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/f2fs/super.c +5 -2
index 2376bb01b5c4..fcc9793dbc2c 100644
@@ -1216,6 +1216,7 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
 	block_t total_count, user_block_count, start_count;
 	u64 avail_node_count;
+	long long bfree;
 
 	total_count = le64_to_cpu(sbi->raw_super->block_count);
 	user_block_count = sbi->user_block_count;
@@ -1226,10 +1227,12 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	buf->f_blocks = total_count - start_count;
 	buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
 						sbi->current_reserved_blocks;
-	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
+
+	bfree = buf->f_bfree - sbi->unusable_block_count;
+	if (unlikely(bfree < 0))
 		buf->f_bfree = 0;
 	else
-		buf->f_bfree -= sbi->unusable_block_count;
+		buf->f_bfree = bfree;
 
 	if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
 		buf->f_bavail = buf->f_bfree -
Signature · 1 line
2.18.0.rc1
maintainer
#4 lore raw
Quoted 12 lines · Chao Yu
Quoted 11 lines · Jaegeuk Kim
Quoted 9 lines · Chao Yu

When calculating .f_bfree value in f2fs_statfs(), sbi->unusable_block_count can be increased after the judgment condition, result in overflow of .f_bfree in later calculation. This patch fixes to use a temporary signed variable to save the calculation result of .f_bfree.

	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
fs/f2fs/super.c +0 -0
 		buf->f_bfree = 0;
 	else
		buf->f_bfree -= sbi->unusable_block_count;
Quoted 4 lines
Quoted 1 line

Do we just need stat_lock for this?

Like we access other stat value in statfs(), we just need the instantaneous value of .unusable_block_count, so we don't need additional stat_lock, right?

What I've concerend is whether or not this fixes all the inconsistent values. The original intention was providing stats in best effort, so we wouldn't use any lock.

Quoted 39 lines

Thanks,

Quoted 36 lines
Quoted 36 lines
---
 fs/f2fs/super.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/f2fs/super.c +5 -2
index 2376bb01b5c4..fcc9793dbc2c 100644
@@ -1216,6 +1216,7 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
 	block_t total_count, user_block_count, start_count;
 	u64 avail_node_count;
+	long long bfree;
 
 	total_count = le64_to_cpu(sbi->raw_super->block_count);
 	user_block_count = sbi->user_block_count;
@@ -1226,10 +1227,12 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	buf->f_blocks = total_count - start_count;
 	buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
 						sbi->current_reserved_blocks;
-	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
+
+	bfree = buf->f_bfree - sbi->unusable_block_count;
+	if (unlikely(bfree < 0))
 		buf->f_bfree = 0;
 	else
-		buf->f_bfree -= sbi->unusable_block_count;
+		buf->f_bfree = bfree;
 
 	if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
 		buf->f_bavail = buf->f_bfree -
Signature · 1 line
2.18.0.rc1
author
#5 lore raw
Quoted 13 lines · Jaegeuk Kim
Quoted 12 lines · Chao Yu
Quoted 11 lines · Jaegeuk Kim
Quoted 9 lines · Chao Yu

When calculating .f_bfree value in f2fs_statfs(), sbi->unusable_block_count can be increased after the judgment condition, result in overflow of .f_bfree in later calculation. This patch fixes to use a temporary signed variable to save the calculation result of .f_bfree.

	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
(quoted diff) +0 -0
 		buf->f_bfree = 0;
 	else
		buf->f_bfree -= sbi->unusable_block_count;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Do we just need stat_lock for this?

Like we access other stat value in statfs(), we just need the instantaneous value of .unusable_block_count, so we don't need additional stat_lock, right?

What I've concerend is whether or not this fixes all the inconsistent values. The original intention was providing stats in best effort, so we wouldn't use any lock.

Hmm.. I've made a patch to protect .unusable_block_count update/access as below, how about merging this two patch, in addition, in this patch, let's add stat_lock around accessing .f_bfree/.unusable_block_count.

Quoted 1 line

From b927209d10fc222243037d05ccc899f48569e773 Mon Sep 17 00:00:00 2001

From: Chao Yu <[email protected]>
Date: Fri, 26 Apr 2019 15:51:22 +0800
Subject: [PATCH] f2fs: fix to cover sbi->unusable_block_count with stat_lock

---
 fs/f2fs/checkpoint.c | 4 ++++
 fs/f2fs/segment.c    | 5 ++++-
 fs/f2fs/super.c      | 2 ++
 3 files changed, 10 insertions(+), 1 deletion(-)
fs/f2fs/checkpoint.c +0 -0
index f6ba9a743a2d..526a70ea7433 100644
@@ -1536,7 +1536,11 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct

cp_control *cpc)
clear_sbi_flag(sbi, SBI_IS_DIRTY); clear_sbi_flag(sbi, SBI_NEED_CP); clear_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH);
+
+ spin_lock(&sbi->stat_lock);
sbi->unusable_block_count = 0;
+ spin_unlock(&sbi->stat_lock);
+
__set_cp_next_pack(sbi);

/*

fs/f2fs/segment.c +0 -0
index 5c3f07540db1..f10c394cf467 100644
@@ -2203,8 +2203,11 @@ static void update_sit_entry(struct f2fs_sb_info *sbi,

block_t blkaddr, int del)
* before, we must track that to know how much space we
* really have.
*/
- if (f2fs_test_bit(offset, se->ckpt_valid_map))
+ if (f2fs_test_bit(offset, se->ckpt_valid_map)) {
+ spin_lock(&sbi->stat_lock);
sbi->unusable_block_count++;
+ spin_unlock(&sbi->stat_lock);
+ }
}

if (f2fs_test_and_clear_bit(offset, se->discard_map))

fs/f2fs/super.c +2 -0
index 1a05a636bd2a..dd6e7b351b58 100644
@@ -1546,7 +1546,9 @@ static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
 	set_sbi_flag(sbi, SBI_CP_DISABLED);
 	f2fs_write_checkpoint(sbi, &cpc);
 
+	spin_lock(&sbi->stat_lock);
 	sbi->unusable_block_count = 0;
+	spin_unlock(&sbi->stat_lock);
 	mutex_unlock(&sbi->gc_mutex);
 restore_flag:
 	sbi->sb->s_flags = s_flags;	/* Restore MS_RDONLY status */
Signature · 1 line
2.18.0.rc1
Quoted 42 lines
Quoted 36 lines

Thanks,

Quoted 32 lines
Quoted 32 lines
---
 fs/f2fs/super.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/f2fs/super.c +5 -2
index 2376bb01b5c4..fcc9793dbc2c 100644
@@ -1216,6 +1216,7 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
 	block_t total_count, user_block_count, start_count;
 	u64 avail_node_count;
+	long long bfree;
 
 	total_count = le64_to_cpu(sbi->raw_super->block_count);
 	user_block_count = sbi->user_block_count;
@@ -1226,10 +1227,12 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
 	buf->f_blocks = total_count - start_count;
 	buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
 						sbi->current_reserved_blocks;
-	if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
+
+	bfree = buf->f_bfree - sbi->unusable_block_count;
+	if (unlikely(bfree < 0))
 		buf->f_bfree = 0;
 	else
-		buf->f_bfree -= sbi->unusable_block_count;
+		buf->f_bfree = bfree;
 
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
fs/f2fs/super.c +0 -0
 	if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
 		buf->f_bavail = buf->f_bfree -
Signature · 1 line
2.18.0.rc1

.