Skip to contentSkip to conversation

f2fs: fix to avoid race during access gc_thread pointer

PATCH In review Chao Yu 2 participants 2 messages May 8, 2018 F2FS file system

lore
Share & tools
lore

Chao Yu

author
#0 lore raw

PATCH 1/1 f2fs: fix to avoid race during access gc_thread pointer

Thread A Thread B
- f2fs_remount
- stop_gc_thread
- f2fs_sbi_store

   sbi->gc_thread = NULL;
				  access sbi->gc_thread->gc_*

Previously, we allocate memory for sbi->gc_thread based on background gc thread mount option, the memory can be released if we turn off that mount option, but still there are several places access gc_thread pointer without considering race condition, result in NULL point dereference.

In order to fix this issue, use sb->s_umount to exclude those operations.

---
 fs/f2fs/sysfs.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/f2fs/sysfs.c +7 -0
index f3c3fb4cbb0d..0aec4db7fa02 100644
@@ -179,6 +179,7 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
 	unsigned long t;
 	unsigned int *ui;
 	ssize_t ret;
+	bool gc_entry = (a->struct_type == GC_THREAD);
 
 	ptr = __struct_ptr(sbi, a->struct_type);
 	if (!ptr)
@@ -277,8 +278,14 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
 		return count;
 	}
 
+	if (gc_entry)
+		down_read(&sbi->sb->s_umount);
+
 	*ui = t;
 
+	if (gc_entry)
+		up_read(&sbi->sb->s_umount);
+
 	if (!strcmp(a->attr.name, "iostat_enable") && *ui == 0)
 		f2fs_reset_iostat(sbi);
 	return count;
Signature · 1 line
2.17.0.391.g1f1cddd558b5
maintainer
#1 lore raw
Quoted 25 lines · Chao Yu

Thread A Thread B
- f2fs_remount
- stop_gc_thread
- f2fs_sbi_store

   sbi->gc_thread = NULL;
				  access sbi->gc_thread->gc_*

Previously, we allocate memory for sbi->gc_thread based on background gc thread mount option, the memory can be released if we turn off that mount option, but still there are several places access gc_thread pointer without considering race condition, result in NULL point dereference.

In order to fix this issue, use sb->s_umount to exclude those operations.

---
 fs/f2fs/sysfs.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/f2fs/sysfs.c +0 -0
index f3c3fb4cbb0d..0aec4db7fa02 100644

fs/f2fs/sysfs.c static ssize_t f2fs_sbi_store(struct f2fs_attr *a, · line 184

Quoted 6 lines
fs/f2fs/sysfs.c +1 -0
@@ -179,6 +179,7 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
 	unsigned long t;
 	unsigned int *ui;
 	ssize_t ret;
+	bool gc_entry = (a->struct_type == GC_THREAD);
 
 	ptr = __struct_ptr(sbi, a->struct_type);

We need to cover __struct_ptr() here.

Quoted 18 lines
fs/f2fs/sysfs.c +6 -0
 	if (!ptr)
@@ -277,8 +278,14 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
 		return count;
 	}
 
+	if (gc_entry)
+		down_read(&sbi->sb->s_umount);
+
 	*ui = t;
 
+	if (gc_entry)
+		up_read(&sbi->sb->s_umount);
+
 	if (!strcmp(a->attr.name, "iostat_enable") && *ui == 0)
 		f2fs_reset_iostat(sbi);
 	return count;
Signature · 1 line
2.17.0.391.g1f1cddd558b5