Skip to contentSkip to conversation

f2fs: Remove lock from check_valid_map

PATCH New Huang Ying 2 participants 7 messages Sep 7, 2014 – Sep 9, 2014 1 message not in the archive F2FS file system

lore
Share & tools
lore

Huang Ying

author
#0 lore raw

PATCH 1/1 f2fs: Remove lock from check_valid_map

Only one bit is read in check_valid_map, holding a lock to do that doesn't help anything except decreasing performance.

---
 fs/f2fs/gc.c |    2 --
 1 file changed, 2 deletions(-)
fs/f2fs/gc.c +0 -2
@@ -382,10 +382,8 @@ static int check_valid_map(struct f2fs_s
 	struct seg_entry *sentry;
 	int ret;
 
-	mutex_lock(&sit_i->sentry_lock);
 	sentry = get_seg_entry(sbi, segno);
 	ret = f2fs_test_bit(offset, sentry->cur_valid_map);
-	mutex_unlock(&sit_i->sentry_lock);
 	return ret;
 }
 
author
#1 lore raw

PATCH 1/1 f2fs: Remove lock from check_valid_map

Only one bit is read in check_valid_map, holding a lock to do that doesn't help anything except decreasing performance.

---

v2: Fixed a build warning.

---
 fs/f2fs/gc.c |    3 ---
 1 file changed, 3 deletions(-)
fs/f2fs/gc.c +0 -3
@@ -378,14 +378,11 @@ static void put_gc_inode(struct list_hea
 static int check_valid_map(struct f2fs_sb_info *sbi,
 				unsigned int segno, int offset)
 {
-	struct sit_info *sit_i = SIT_I(sbi);
 	struct seg_entry *sentry;
 	int ret;
 
-	mutex_lock(&sit_i->sentry_lock);
 	sentry = get_seg_entry(sbi, segno);
 	ret = f2fs_test_bit(offset, sentry->cur_valid_map);
-	mutex_unlock(&sit_i->sentry_lock);
 	return ret;
 }
 
maintainer
#2 lore raw

Hi,

Quoted 17 lines · Huang Ying

Only one bit is read in check_valid_map, holding a lock to do that doesn't help anything except decreasing performance.

---

v2: Fixed a build warning.

---
 fs/f2fs/gc.c |    3 ---
 1 file changed, 3 deletions(-)
fs/f2fs/gc.c +0 -0
@@ -378,14 +378,11 @@ static void put_gc_inode(struct list_hea
 static int check_valid_map(struct f2fs_sb_info *sbi,
 				unsigned int segno, int offset)

fs/f2fs/gc.c static void put_gc_inode(struct list_hea · line 386

Quoted 10 lines
fs/f2fs/gc.c +0 -3
 {
-	struct sit_info *sit_i = SIT_I(sbi);
 	struct seg_entry *sentry;
 	int ret;
 
-	mutex_lock(&sit_i->sentry_lock);
 	sentry = get_seg_entry(sbi, segno);
 	ret = f2fs_test_bit(offset, sentry->cur_valid_map);
-	mutex_unlock(&sit_i->sentry_lock);
 	return ret;

The f2fs_test_bit is not atomic, so I'm not sure this is a good approach. How about introducing rw_semaphore?

Quoted 2 lines
fs/f2fs/gc.c +0 -0
 }
 

A message that is not in the archive (sent off-list or not yet received): CAC=cRTOdvOp=zBT986SqGXC2+iRxGzSgKdFYzTQjbAamYsGVsg@mail.gmail.com

maintainer
#4 lore raw

Hi Huang,

Quoted 39 lines · huang ying

Hi, Jaegeuk,

Quoted 25 lines · Jaegeuk Kim

Hi,

Quoted 22 lines · Huang Ying

Only one bit is read in check_valid_map, holding a lock to do that doesn't help anything except decreasing performance.

---

v2: Fixed a build warning.

---
 fs/f2fs/gc.c |    3 ---
 1 file changed, 3 deletions(-)
fs/f2fs/gc.c +0 -1
@@ -378,14 +378,11 @@ static void put_gc_inode(struct list_hea
 static int check_valid_map(struct f2fs_sb_info *sbi,
                              unsigned int segno, int offset)
 {
-     struct sit_info *sit_i = SIT_I(sbi);
      struct seg_entry *sentry;
      int ret;
 
Quoted 9 lines

fs/f2fs/gc.c static void put_gc_inode(struct list_hea · line 386

Quoted 5 lines
fs/f2fs/gc.c +0 -2
-     mutex_lock(&sit_i->sentry_lock);
      sentry = get_seg_entry(sbi, segno);
      ret = f2fs_test_bit(offset, sentry->cur_valid_map);
-     mutex_unlock(&sit_i->sentry_lock);
      return ret;

The f2fs_test_bit is not atomic, so I'm not sure this is a good approach. How about introducing rw_semaphore?

Quoted 6 lines

IMO, f2fs_test_bit just read a global variable (a byte in cur_valid_map), then check its value. The byte may be changed in another CPU concurrently. But even protected with a mutex, it can be changed in another CPU immediately after mutex_unlock. So mutex does not help here. Here we just read a global variable, not read/modify/write, so, we don't need atomic too.

Hmm. This is a pretty hard corner case to allow the mutex removal under the following assumption.

1. All the sit entries are cached in a global array, which means that it never happens that any sit entry pointers are changed.

2. I agree that f2fs_gc tries to conduct the cleaning with best effort, and it triggers again when it detects there is something to do more. So, check_valid_bitmap doesn't need to make a precise decision.

But, what I concern is the consistent policy to use such the mutex. If we break the rule, it becomes harder to debug potential bugs.

Anyway, have you been facing with such the lock contention?

Thanks,

Quoted 3 lines

Best Regards,
Huang, Ying

author
#5 lore raw
Quoted 55 lines · Jaegeuk Kim

Hi Huang,

Quoted 39 lines · huang ying

Hi, Jaegeuk,

Quoted 25 lines · Jaegeuk Kim

Hi,

Quoted 22 lines · Huang Ying

Only one bit is read in check_valid_map, holding a lock to do that doesn't help anything except decreasing performance.

---

v2: Fixed a build warning.

---
 fs/f2fs/gc.c |    3 ---
 1 file changed, 3 deletions(-)
fs/f2fs/gc.c +0 -1
@@ -378,14 +378,11 @@ static void put_gc_inode(struct list_hea
 static int check_valid_map(struct f2fs_sb_info *sbi,
                              unsigned int segno, int offset)
 {
-     struct sit_info *sit_i = SIT_I(sbi);
      struct seg_entry *sentry;
      int ret;
 
Quoted 9 lines

fs/f2fs/gc.c static void put_gc_inode(struct list_hea · line 386

Quoted 5 lines
fs/f2fs/gc.c +0 -2
-     mutex_lock(&sit_i->sentry_lock);
      sentry = get_seg_entry(sbi, segno);
      ret = f2fs_test_bit(offset, sentry->cur_valid_map);
-     mutex_unlock(&sit_i->sentry_lock);
      return ret;

The f2fs_test_bit is not atomic, so I'm not sure this is a good approach. How about introducing rw_semaphore?

Quoted 6 lines

IMO, f2fs_test_bit just read a global variable (a byte in cur_valid_map), then check its value. The byte may be changed in another CPU concurrently. But even protected with a mutex, it can be changed in another CPU immediately after mutex_unlock. So mutex does not help here. Here we just read a global variable, not read/modify/write, so, we don't need atomic too.

Hmm. This is a pretty hard corner case to allow the mutex removal under the following assumption.

1. All the sit entries are cached in a global array, which means that it never happens that any sit entry pointers are changed.

Quoted 6 lines

2. I agree that f2fs_gc tries to conduct the cleaning with best effort, and it triggers again when it detects there is something to do more. So, check_valid_bitmap doesn't need to make a precise decision.

But, what I concern is the consistent policy to use such the mutex. If we break the rule, it becomes harder to debug potential bugs.

Yes. We definitely need a rule. But I suggest to make a small tweak to the rule. If we just read one variable with fixed address, we need not to use a mutex to protect that.

Quoted 1 line

Anyway, have you been facing with such the lock contention?

No, I just review the code and thinks the mutex is not necessary.

Best Regards,
Huang, Ying

maintainer
#6 lore raw
Quoted 60 lines · Huang Ying
Quoted 59 lines · Jaegeuk Kim

Hi Huang,

Quoted 39 lines · huang ying

Hi, Jaegeuk,

Quoted 25 lines · Jaegeuk Kim

Hi,

Quoted 22 lines · Huang Ying

Only one bit is read in check_valid_map, holding a lock to do that doesn't help anything except decreasing performance.

---

v2: Fixed a build warning.

---
 fs/f2fs/gc.c |    3 ---
 1 file changed, 3 deletions(-)
fs/f2fs/gc.c +0 -1
@@ -378,14 +378,11 @@ static void put_gc_inode(struct list_hea
 static int check_valid_map(struct f2fs_sb_info *sbi,
                              unsigned int segno, int offset)
 {
-     struct sit_info *sit_i = SIT_I(sbi);
      struct seg_entry *sentry;
      int ret;
 
Quoted 9 lines

fs/f2fs/gc.c static void put_gc_inode(struct list_hea · line 386

Quoted 5 lines
fs/f2fs/gc.c +0 -2
-     mutex_lock(&sit_i->sentry_lock);
      sentry = get_seg_entry(sbi, segno);
      ret = f2fs_test_bit(offset, sentry->cur_valid_map);
-     mutex_unlock(&sit_i->sentry_lock);
      return ret;

The f2fs_test_bit is not atomic, so I'm not sure this is a good approach. How about introducing rw_semaphore?

Quoted 6 lines

IMO, f2fs_test_bit just read a global variable (a byte in cur_valid_map), then check its value. The byte may be changed in another CPU concurrently. But even protected with a mutex, it can be changed in another CPU immediately after mutex_unlock. So mutex does not help here. Here we just read a global variable, not read/modify/write, so, we don't need atomic too.

Hmm. This is a pretty hard corner case to allow the mutex removal under the following assumption.

1. All the sit entries are cached in a global array, which means that it never happens that any sit entry pointers are changed.

2. I agree that f2fs_gc tries to conduct the cleaning with best effort, and it triggers again when it detects there is something to do more. So, check_valid_bitmap doesn't need to make a precise decision.

Quoted 5 lines
Quoted 2 lines

But, what I concern is the consistent policy to use such the mutex. If we break the rule, it becomes harder to debug potential bugs.

Yes. We definitely need a rule. But I suggest to make a small tweak to the rule.

I don't think there is enough reason that we should take a small tweak while breaking the locking policy. It's related to neither performance issue nor a bug case.

Even if f2fs suffers from lock contention here, I think we need to bet on rw_semaphore to satisfy the rule and performance at the same time.

Thanks,

Quoted 9 lines

If we just read one variable with fixed address, we need not to use a mutex to protect that.

Quoted 1 line

Anyway, have you been facing with such the lock contention?

No, I just review the code and thinks the mutex is not necessary.

Best Regards,
Huang, Ying

author
#7 lore raw
Quoted 64 lines · Jaegeuk Kim
Quoted 63 lines · Huang Ying
Quoted 61 lines · Jaegeuk Kim

Hi Huang,

Quoted 39 lines · huang ying

Hi, Jaegeuk,

Quoted 25 lines · Jaegeuk Kim

Hi,

Quoted 22 lines · Huang Ying

Only one bit is read in check_valid_map, holding a lock to do that doesn't help anything except decreasing performance.

---

v2: Fixed a build warning.

---
 fs/f2fs/gc.c |    3 ---
 1 file changed, 3 deletions(-)
fs/f2fs/gc.c +0 -1
@@ -378,14 +378,11 @@ static void put_gc_inode(struct list_hea
 static int check_valid_map(struct f2fs_sb_info *sbi,
                              unsigned int segno, int offset)
 {
-     struct sit_info *sit_i = SIT_I(sbi);
      struct seg_entry *sentry;
      int ret;
 
Quoted 9 lines

fs/f2fs/gc.c static void put_gc_inode(struct list_hea · line 386

Quoted 5 lines
fs/f2fs/gc.c +0 -2
-     mutex_lock(&sit_i->sentry_lock);
      sentry = get_seg_entry(sbi, segno);
      ret = f2fs_test_bit(offset, sentry->cur_valid_map);
-     mutex_unlock(&sit_i->sentry_lock);
      return ret;

The f2fs_test_bit is not atomic, so I'm not sure this is a good approach. How about introducing rw_semaphore?

Quoted 6 lines

IMO, f2fs_test_bit just read a global variable (a byte in cur_valid_map), then check its value. The byte may be changed in another CPU concurrently. But even protected with a mutex, it can be changed in another CPU immediately after mutex_unlock. So mutex does not help here. Here we just read a global variable, not read/modify/write, so, we don't need atomic too.

Hmm. This is a pretty hard corner case to allow the mutex removal under the following assumption.

1. All the sit entries are cached in a global array, which means that it never happens that any sit entry pointers are changed.

2. I agree that f2fs_gc tries to conduct the cleaning with best effort, and it triggers again when it detects there is something to do more. So, check_valid_bitmap doesn't need to make a precise decision.

But, what I concern is the consistent policy to use such the mutex. If we break the rule, it becomes harder to debug potential bugs.

Quoted 6 lines
Quoted 2 lines

Yes. We definitely need a rule. But I suggest to make a small tweak to the rule.

I don't think there is enough reason that we should take a small tweak while breaking the locking policy. It's related to neither performance issue nor a bug case.

I don't want to break the locking rule. I just propose a suggestion to tweak the rule itself a little. To make something like "If we just read one variable with fixed address, we need not to use a lock to protect that." to be part of the rule.

But if you think it is better to use a lock here. That is not a problem for me.

Best Regards,
Huang, Ying

Quoted 14 lines

Even if f2fs suffers from lock contention here, I think we need to bet on rw_semaphore to satisfy the rule and performance at the same time.

Thanks,

Quoted 9 lines

If we just read one variable with fixed address, we need not to use a mutex to protect that.

Quoted 1 line

Anyway, have you been facing with such the lock contention?

No, I just review the code and thinks the mutex is not necessary.

Best Regards,
Huang, Ying