Skip to contentSkip to conversation

f2fs:Add mutex_lock to protect f2fs_stat_list.

PATCH Huajun Li 2 participants 3 messages Jan 15, 2013 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Huajun Li

author
#1 lore raw
In reply to a message not in the archive
Quoted 59 lines · majianpeng

There is an race between umount f2fs and read f2fs/status.
It will case oops.
Fox example:
Thread A Thread B
umount f2fs cat f2fs/status
f2fs_destroy_stats() { stat_show() {
list_for_each_entry_safe(&f2fs_stat_list)
list_del(&si->stat_list);
mutex_lock(&si->stat_lock);
si->sbi = NULL;
mutex_unlock(&si->stat_lock);

 kfree(sbi->stat_info);
                                                                                                    mutex_lock(&si->stat_lock)
---
 fs/f2fs/debug.c |    9 +++++++++
 1 file changed, 9 insertions(+)
fs/f2fs/debug.c +6 -0
index 0e0380a..ec6d004 100644
@@ -26,6 +26,7 @@
 
 static LIST_HEAD(f2fs_stat_list);
 static struct dentry *debugfs_root;
+static DEFINE_MUTEX(f2fs_stat_mutex);
 
 static void update_general_status(struct f2fs_sb_info *sbi)
 {
@@ -180,6 +181,7 @@ static int stat_show(struct seq_file *s, void *v)
        int i = 0;
        int j;
 
+       mutex_lock(&f2fs_stat_mutex);
        list_for_each_entry_safe(si, next, &f2fs_stat_list, stat_list) {
 
                mutex_lock(&si->stat_lock);
@@ -288,6 +290,7 @@ static int stat_show(struct seq_file *s, void *v)
                                si->base_mem >> 10, si->cache_mem >> 10);
                mutex_unlock(&si->stat_lock);
        }
+       mutex_unlock(&f2fs_stat_mutex);
        return 0;
 }
 
@@ -314,7 +317,10 @@ static int init_stats(struct f2fs_sb_info *sbi)
 
        si = sbi->stat_info;
        mutex_init(&si->stat_lock);
+
+       mutex_lock(&f2fs_stat_mutex);
        list_add_tail(&si->stat_list, &f2fs_stat_list);
+       mutex_unlock(&f2fs_stat_mutex);
 
        si->all_area_segs = le32_to_cpu(raw_super->segment_count);
        si->sit_area_segs = le32_to_cpu(raw_super->segment_count_sit);

fs/f2fs/debug.c void f2fs_destroy_stats(struct f2fs_sb_info *sbi) · line 359

Quoted 7 lines
fs/f2fs/debug.c +3 -0
@@ -347,7 +353,10 @@ void f2fs_destroy_stats(struct f2fs_sb_info *sbi)
 {
        struct f2fs_stat_info *si = sbi->stat_info;
 
+       mutex_lock(&f2fs_stat_mutex);
        list_del(&si->stat_list);
+       mutex_unlock(&f2fs_stat_mutex);
+

Hi Jianpeng,
Is it possible to fix the issue by holding si->stat_lock while executing list_del(&si->stat_list) ? this can avoid introducing new variable.

Quoted 15 lines
fs/f2fs/debug.c +0 -1
        mutex_lock(&si->stat_lock);
        si->sbi = NULL;
        mutex_unlock(&si->stat_lock);
--

1.7.9.5
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS, MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122412
_______________________________________________
Linux-f2fs-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#2 lore raw

Hi,

2013-01-15 (화), 10:57 +0800, Huajun Li:

Quoted 7 lines · majianpeng
Quoted 6 lines

There is an race between umount f2fs and read f2fs/status. It will case oops.
Fox example:
Thread A Thread B
umount f2fs cat f2fs/status
f2fs_destroy_stats() { stat_show() {

Quoted 8 lines
Quoted 8 lines

list_for_each_entry_safe(&f2fs_stat_list)
list_del(&si->stat_list);
mutex_lock(&si->stat_lock);
si->sbi = NULL;
mutex_unlock(&si->stat_lock);

 kfree(sbi->stat_info);
                                                                                                    mutex_lock(&si->stat_lock)

Nice catch.
Actually, &si->stat_lock was introduced to cope with this issue. So, we need to remove the existing &si->stat_lock, and add your global f2fs_stat_mutex.
Then, we're able to remove "si->sbi = NULL"-related stuffs too.

Quoted 48 lines
Quoted 48 lines
---
 fs/f2fs/debug.c |    9 +++++++++
 1 file changed, 9 insertions(+)
fs/f2fs/debug.c +6 -0
index 0e0380a..ec6d004 100644
@@ -26,6 +26,7 @@
 
 static LIST_HEAD(f2fs_stat_list);
 static struct dentry *debugfs_root;
+static DEFINE_MUTEX(f2fs_stat_mutex);
 
 static void update_general_status(struct f2fs_sb_info *sbi)
 {
@@ -180,6 +181,7 @@ static int stat_show(struct seq_file *s, void *v)
        int i = 0;
        int j;
 
+       mutex_lock(&f2fs_stat_mutex);
        list_for_each_entry_safe(si, next, &f2fs_stat_list, stat_list) {
 
                mutex_lock(&si->stat_lock);
@@ -288,6 +290,7 @@ static int stat_show(struct seq_file *s, void *v)
                                si->base_mem >> 10, si->cache_mem >> 10);
                mutex_unlock(&si->stat_lock);
        }
+       mutex_unlock(&f2fs_stat_mutex);
        return 0;
 }
 
@@ -314,7 +317,10 @@ static int init_stats(struct f2fs_sb_info *sbi)
 
        si = sbi->stat_info;
        mutex_init(&si->stat_lock);
+
+       mutex_lock(&f2fs_stat_mutex);
        list_add_tail(&si->stat_list, &f2fs_stat_list);
+       mutex_unlock(&f2fs_stat_mutex);
 
        si->all_area_segs = le32_to_cpu(raw_super->segment_count);
        si->sit_area_segs = le32_to_cpu(raw_super->segment_count_sit);
@@ -347,7 +353,10 @@ void f2fs_destroy_stats(struct f2fs_sb_info *sbi)
 {
        struct f2fs_stat_info *si = sbi->stat_info;
 
Quoted 8 lines

fs/f2fs/debug.c void f2fs_destroy_stats(struct f2fs_sb_info *sbi) · line 359

Quoted 4 lines
fs/f2fs/debug.c +3 -0
+       mutex_lock(&f2fs_stat_mutex);
        list_del(&si->stat_list);
+       mutex_unlock(&f2fs_stat_mutex);
+

Hi Jianpeng,
Is it possible to fix the issue by holding si->stat_lock while executing list_del(&si->stat_list) ?

I think it cannot fix the problem.
Still the above errorneous scenario is able to be occurred.

Thank you,

Signature · 2 lines
Jaegeuk Kim
Samsung
#3 lore raw
In reply to a message not in the archive

Hi Jianpeng, again. :)

How about this patch based on your original patch? Could you test this?
Thanks,

From 74c4159102ff4808cc59ea8513bd276605c30972 Mon Sep 17 00:00:00 2001
From: majianpeng <[email protected]>
Date: Mon, 14 Jan 2013 20:08:16 +0800
Subject: [PATCH] f2fs: add global mutex_lock to protect f2fs_stat_list

There is an race condition between umounting f2fs and reading f2fs/status, which results in oops.

Fox example:
Thread A Thread B
umount f2fs cat f2fs/status

f2fs_destroy_stats() { stat_show() {
list_for_each_entry_safe(&f2fs_stat_list)
list_del(&si->stat_list);
mutex_lock(&si->stat_lock);
si->sbi = NULL;
mutex_unlock(&si->stat_lock);
kfree(sbi->stat_info);
} mutex_lock(&si->stat_lock) <- si is gone.

				 ...
				}

Solution with a global lock: f2fs_stat_mutex:
Thread A Thread B
umount f2fs cat f2fs/status

f2fs_destroy_stats() { stat_show() {
mutex_lock(&f2fs_stat_mutex);
list_del(&si->stat_list);
mutex_unlock(&f2fs_stat_mutex);
kfree(sbi->stat_info); mutex_lock(&f2fs_stat_mutex);
} list_for_each_entry_safe(&f2fs_stat_list)

				 ...
				 mutex_unlock(&f2fs_stat_mutex);
				}

[[email protected]: fix typos, description, and remove the existing lock]

---
 fs/f2fs/debug.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
fs/f2fs/debug.c +11 -12
index 0c374d1..818f2ed 100644
@@ -26,6 +26,7 @@
 
 static LIST_HEAD(f2fs_stat_list);
 static struct dentry *debugfs_root;
+static DEFINE_MUTEX(f2fs_stat_mutex);
 
 static void update_general_status(struct f2fs_sb_info *sbi)
 {
@@ -180,13 +181,9 @@ static int stat_show(struct seq_file *s, void *v)
 	int i = 0;
 	int j;
 
+	mutex_lock(&f2fs_stat_mutex);
 	list_for_each_entry_safe(si, next, &f2fs_stat_list, stat_list) {
 
-		mutex_lock(&si->stat_lock);
-		if (!si->sbi) {
-			mutex_unlock(&si->stat_lock);
-			continue;
-		}
 		update_general_status(si->sbi);
 
 		seq_printf(s, "\n=====[ partition info. #%d ]=====\n", i++);
@@ -286,8 +283,8 @@ static int stat_show(struct seq_file *s, void *v)
 		seq_printf(s, "\nMemory: %u KB = static: %u + cached: %u\n",
 				(si->base_mem + si->cache_mem) >> 10,
 				si->base_mem >> 10, si->cache_mem >> 10);
-		mutex_unlock(&si->stat_lock);
 	}
+	mutex_unlock(&f2fs_stat_mutex);
 	return 0;
 }
 
@@ -313,9 +310,6 @@ static int init_stats(struct f2fs_sb_info *sbi)
 		return -ENOMEM;
 
 	si = sbi->stat_info;
-	mutex_init(&si->stat_lock);
-	list_add_tail(&si->stat_list, &f2fs_stat_list);
-
 	si->all_area_segs = le32_to_cpu(raw_super->segment_count);
 	si->sit_area_segs = le32_to_cpu(raw_super->segment_count_sit);
 	si->nat_area_segs = le32_to_cpu(raw_super->segment_count_nat);
@@ -325,6 +319,11 @@ static int init_stats(struct f2fs_sb_info *sbi)
 	si->main_area_zones = si->main_area_sections /
 				le32_to_cpu(raw_super->secs_per_zone);
 	si->sbi = sbi;
+
+	mutex_lock(&f2fs_stat_mutex);
+	list_add_tail(&si->stat_list, &f2fs_stat_list);
+	mutex_unlock(&f2fs_stat_mutex);
+
 	return 0;
 }
 
@@ -348,10 +347,10 @@ void f2fs_destroy_stats(struct f2fs_sb_info *sbi)
 {
 	struct f2fs_stat_info *si = sbi->stat_info;
 
+	mutex_lock(&f2fs_stat_mutex);
 	list_del(&si->stat_list);
-	mutex_lock(&si->stat_lock);
-	si->sbi = NULL;
-	mutex_unlock(&si->stat_lock);
+	mutex_unlock(&f2fs_stat_mutex);
+
 	kfree(sbi->stat_info);
 }
 
Signature · 8 lines
1.8.0.1.250.gb7973fb




-- 
Jaegeuk Kim
Samsung