Skip to contentSkip to conversation

f2fs: shrink spinlock coverage

PATCH Superseded Chao Yu 2 participants 4 messages May 6, 2020 – May 7, 2020 F2FS file system

lore
Share & tools
lore

Chao Yu

author
#0 lore raw

PATCH v2 1/1 f2fs: shrink spinlock coverage

In f2fs_try_to_free_nids(), .nid_list_lock spinlock critical region will increase as expected shrink number increase, to avoid spining other CPUs for long time, it's better to implement like extent cache and nats shrinker.

---
v2:
- fix unlock wrong spinlock.

 fs/f2fs/node.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
fs/f2fs/node.c +11 -4
index 4da0d8713df5..ad0b14f4dab8 100644
@@ -2488,7 +2488,6 @@ void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
 {
 	struct f2fs_nm_info *nm_i = NM_I(sbi);
-	struct free_nid *i, *next;
 	int nr = nr_shrink;
 
 	if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
@@ -2498,14 +2497,22 @@ int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
 		return 0;
 
 	spin_lock(&nm_i->nid_list_lock);
-	list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
-		if (nr_shrink <= 0 ||
-				nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
+	while (nr_shrink) {
+		struct free_nid *i;
+
+		if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
 			break;
 
+		i = list_first_entry(&nm_i->free_nid_list,
+					struct free_nid, list);
+		list_del(&i->list);
+		spin_unlock(&nm_i->nid_list_lock);
+
 		__remove_free_nid(sbi, i, FREE_NID);
 		kmem_cache_free(free_nid_slab, i);
 		nr_shrink--;
+
+		spin_lock(&nm_i->nid_list_lock);
 	}
 	spin_unlock(&nm_i->nid_list_lock);
 	mutex_unlock(&nm_i->build_lock);
Signature · 1 line
2.18.0.rc1
maintainer
#1 lore raw
Quoted 33 lines · Chao Yu

In f2fs_try_to_free_nids(), .nid_list_lock spinlock critical region will increase as expected shrink number increase, to avoid spining other CPUs for long time, it's better to implement like extent cache and nats shrinker.

---
v2:
- fix unlock wrong spinlock.

 fs/f2fs/node.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
fs/f2fs/node.c +2 -4
index 4da0d8713df5..ad0b14f4dab8 100644
@@ -2488,7 +2488,6 @@ void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
 {
 	struct f2fs_nm_info *nm_i = NM_I(sbi);
-	struct free_nid *i, *next;
 	int nr = nr_shrink;
 
 	if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
@@ -2498,14 +2497,22 @@ int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
 		return 0;
 
 	spin_lock(&nm_i->nid_list_lock);
-	list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
-		if (nr_shrink <= 0 ||
-				nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
+	while (nr_shrink) {
+		struct free_nid *i;

fs/f2fs/node.c int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink) · line 2511

Quoted 10 lines
fs/f2fs/node.c +7 -0
+
+		if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
 			break;
 
+		i = list_first_entry(&nm_i->free_nid_list,
+					struct free_nid, list);
+		list_del(&i->list);
+		spin_unlock(&nm_i->nid_list_lock);
+
 		__remove_free_nid(sbi, i, FREE_NID);

__remove_free_nid() will do list_del again. btw, how about just splitting out given nr_shrink into multiple trials?

Quoted 9 lines
fs/f2fs/node.c +2 -0
 		kmem_cache_free(free_nid_slab, i);
 		nr_shrink--;
+
+		spin_lock(&nm_i->nid_list_lock);
 	}
 	spin_unlock(&nm_i->nid_list_lock);
 	mutex_unlock(&nm_i->build_lock);
Signature · 1 line
2.18.0.rc1
author
#2 lore raw
Quoted 38 lines · Jaegeuk Kim
Quoted 37 lines · Chao Yu

In f2fs_try_to_free_nids(), .nid_list_lock spinlock critical region will increase as expected shrink number increase, to avoid spining other CPUs for long time, it's better to implement like extent cache and nats shrinker.

---
v2:
- fix unlock wrong spinlock.

 fs/f2fs/node.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
fs/f2fs/node.c +4 -4
index 4da0d8713df5..ad0b14f4dab8 100644
@@ -2488,7 +2488,6 @@ void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
 {
 	struct f2fs_nm_info *nm_i = NM_I(sbi);
-	struct free_nid *i, *next;
 	int nr = nr_shrink;
 
 	if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
@@ -2498,14 +2497,22 @@ int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
 		return 0;
 
 	spin_lock(&nm_i->nid_list_lock);
-	list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
-		if (nr_shrink <= 0 ||
-				nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
+	while (nr_shrink) {
+		struct free_nid *i;
+
+		if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
 			break;
 
Quoted 8 lines

fs/f2fs/node.c int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink) · line 2511

Quoted 6 lines
fs/f2fs/node.c +5 -0
+		i = list_first_entry(&nm_i->free_nid_list,
+					struct free_nid, list);
+		list_del(&i->list);
+		spin_unlock(&nm_i->nid_list_lock);
+
 		__remove_free_nid(sbi, i, FREE_NID);

__remove_free_nid() will do list_del again. btw, how about just splitting out

Oh, my bad.

How about moving __remove_free_nid into .nid_list_lock coverage?

Quoted 1 line

given nr_shrink into multiple trials?

Like this?

	while (shrink) {
		batch = DEFAULT_BATCH_NUMBER; // 16
		spinlock();
		list_for_each_entry_safe() {
			if (!shrink || !batch)
				break;
			remove_item_from_list;
			shrink--;
			batch--;
		}
		spin_unlock();
	}

Thanks,

Quoted 12 lines
Quoted 10 lines
fs/f2fs/node.c +2 -0
 		kmem_cache_free(free_nid_slab, i);
 		nr_shrink--;
+
+		spin_lock(&nm_i->nid_list_lock);
 	}
 	spin_unlock(&nm_i->nid_list_lock);
 	mutex_unlock(&nm_i->build_lock);
Signature · 1 line
2.18.0.rc1

.

maintainer
#3 lore raw
Quoted 48 lines · Chao Yu
Quoted 38 lines · Jaegeuk Kim
Quoted 37 lines · Chao Yu

In f2fs_try_to_free_nids(), .nid_list_lock spinlock critical region will increase as expected shrink number increase, to avoid spining other CPUs for long time, it's better to implement like extent cache and nats shrinker.

---
v2:
- fix unlock wrong spinlock.

 fs/f2fs/node.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
fs/f2fs/node.c +4 -4
index 4da0d8713df5..ad0b14f4dab8 100644
@@ -2488,7 +2488,6 @@ void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
 {
 	struct f2fs_nm_info *nm_i = NM_I(sbi);
-	struct free_nid *i, *next;
 	int nr = nr_shrink;
 
 	if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
@@ -2498,14 +2497,22 @@ int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
 		return 0;
 
 	spin_lock(&nm_i->nid_list_lock);
-	list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
-		if (nr_shrink <= 0 ||
-				nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
+	while (nr_shrink) {
+		struct free_nid *i;
+
+		if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
 			break;
 
Quoted 8 lines

fs/f2fs/node.c int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink) · line 2511

Quoted 6 lines
fs/f2fs/node.c +5 -0
+		i = list_first_entry(&nm_i->free_nid_list,
+					struct free_nid, list);
+		list_del(&i->list);
+		spin_unlock(&nm_i->nid_list_lock);
+
 		__remove_free_nid(sbi, i, FREE_NID);

__remove_free_nid() will do list_del again. btw, how about just splitting out

Quoted 7 lines

Oh, my bad.

How about moving __remove_free_nid into .nid_list_lock coverage?

Quoted 1 line

given nr_shrink into multiple trials?

Like this?

Yes.

Quoted 28 lines
	while (shrink) {
		batch = DEFAULT_BATCH_NUMBER; // 16
		spinlock();
		list_for_each_entry_safe() {
			if (!shrink || !batch)
				break;
			remove_item_from_list;
			shrink--;
			batch--;
		}
		spin_unlock();
	}

Thanks,

Quoted 12 lines
Quoted 10 lines
fs/f2fs/node.c +2 -0
 		kmem_cache_free(free_nid_slab, i);
 		nr_shrink--;
+
+		spin_lock(&nm_i->nid_list_lock);
 	}
 	spin_unlock(&nm_i->nid_list_lock);
 	mutex_unlock(&nm_i->build_lock);
Signature · 1 line
2.18.0.rc1

.