From: Jaegeuk Kim Subject: Re: [PATCH] f2fs:Add mutex_lock to protect f2fs_stat_list. Date: Tue, 15 Jan 2013 16:29:38 +0900 Message-ID: <1358234978.8234.53.camel@kjgkr> References: <201301142008133402501@gmail.com> Reply-To: jaegeuk.kim@samsung.com Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-/L1EqtwFrut36SDlhyzZ" Cc: linux-f2fs-devel , linux-fsdevel To: majianpeng Return-path: Received: from mailout3.samsung.com ([203.254.224.33]:50799 "EHLO mailout3.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754175Ab3AOH37 (ORCPT ); Tue, 15 Jan 2013 02:29:59 -0500 Received: from epcpsbgm1.samsung.com (epcpsbgm1 [203.254.230.26]) by mailout3.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0MGN0088EOTHEZ20@mailout3.samsung.com> for linux-fsdevel@vger.kernel.org; Tue, 15 Jan 2013 16:29:41 +0900 (KST) Received: from [12.52.126.105] by mmp1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0MGN00HDNOTHZN50@mmp1.samsung.com> for linux-fsdevel@vger.kernel.org; Tue, 15 Jan 2013 16:29:41 +0900 (KST) In-reply-to: <201301142008133402501@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: --=-/L1EqtwFrut36SDlhyzZ Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Jianpeng, again. :) How about this patch based on your original patch? Could you test this? Thanks, =46rom 74c4159102ff4808cc59ea8513bd276605c30972 Mon Sep 17 00:00:00 2001 From: majianpeng 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 =3D 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); } Signed-off-by: Jianpeng Ma [jaegeuk.kim@samsung.com: fix typos, description, and remove the existing lock] Signed-off-by: Jaegeuk Kim --- fs/f2fs/debug.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/fs/f2fs/debug.c b/fs/f2fs/debug.c index 0c374d1..818f2ed 100644 --- a/fs/f2fs/debug.c +++ b/fs/f2fs/debug.c @@ -26,6 +26,7 @@ =20 static LIST_HEAD(f2fs_stat_list); static struct dentry *debugfs_root; +static DEFINE_MUTEX(f2fs_stat_mutex); =20 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 =3D 0; int j; =20 + mutex_lock(&f2fs_stat_mutex); list_for_each_entry_safe(si, next, &f2fs_stat_list, stat_list) { =20 - mutex_lock(&si->stat_lock); - if (!si->sbi) { - mutex_unlock(&si->stat_lock); - continue; - } update_general_status(si->sbi); =20 seq_printf(s, "\n=3D=3D=3D=3D=3D[ partition info. #%d ]=3D=3D=3D=3D=3D\n= ", i++); @@ -286,8 +283,8 @@ static int stat_show(struct seq_file *s, void *v) seq_printf(s, "\nMemory: %u KB =3D 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; } =20 @@ -313,9 +310,6 @@ static int init_stats(struct f2fs_sb_info *sbi) return -ENOMEM; =20 si =3D sbi->stat_info; - mutex_init(&si->stat_lock); - list_add_tail(&si->stat_list, &f2fs_stat_list); - si->all_area_segs =3D le32_to_cpu(raw_super->segment_count); si->sit_area_segs =3D le32_to_cpu(raw_super->segment_count_sit); si->nat_area_segs =3D 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 =3D si->main_area_sections / le32_to_cpu(raw_super->secs_per_zone); si->sbi =3D sbi; + + mutex_lock(&f2fs_stat_mutex); + list_add_tail(&si->stat_list, &f2fs_stat_list); + mutex_unlock(&f2fs_stat_mutex); + return 0; } =20 @@ -348,10 +347,10 @@ void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { struct f2fs_stat_info *si =3D sbi->stat_info; =20 + mutex_lock(&f2fs_stat_mutex); list_del(&si->stat_list); - mutex_lock(&si->stat_lock); - si->sbi =3D NULL; - mutex_unlock(&si->stat_lock); + mutex_unlock(&f2fs_stat_mutex); + kfree(sbi->stat_info); } =20 --=20 1.8.0.1.250.gb7973fb --=20 Jaegeuk Kim Samsung --=-/L1EqtwFrut36SDlhyzZ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQIcBAABAgAGBQJQ9QViAAoJEEAUqH6CSFDSPBsP/jJtoLUpHgnkE+cB9II/RzNv 8/ekOn9Q4itvRbMLTYr9g7TZl2YBZPO/E4U8IQsL79V6ljOsQSpKzE8SjUCIQHU9 +3xwrqCAaogcza0VQ4GS1EgHNTWaR18AjgzX3ATx/qudtEfEQBP7QT6Txqzdl0CL DzUYiTuuyT75Az5Ut/UeIAUEeVgHxUgdNZp4Ku7sjKTQba4F02HJfjVrE0QguNHq mm91qBShhwbaj5IbASN70GJC5ZSvTyyTnF5CQQUvMAi6bWU7NzZ72M4hftRsypuh 8wup98U0ugYyU65IMVaSHFUX31LONgQZY61kROgmd5yunf3N2PYhtvCBv4o3+5eF tX7LbtQ13soVQP1fONtkW1qMjnyc5rwMg79EFMCOCRwvg0Ga0eosTl0S1bodrbIe wmp//snE5kFQTUl+wLzL+Cu8DgJkGxxpxYsuoEeqLKHtPbGXkWZM84e748FOt+oT hwdSbAKc7UfSTGjV9Zkn/2riWx+dxhGN6AyN6HO6BfdDTOsQYl9yHhdvDf4vPVdF P9WYk0Bps0DouvCM5VpICzJyPl7kiGBU9wCvD/YOnNaQjOed/xHNJexjZ6r+MECV 8RZKL+YjHrebkT/S0opeGZlIa/onC9d0hqjD8kFVnZIsahcqeyIrTFAFLP8IYFGO sSVCT/PhQ3jGMJeFaQdj =zf/j -----END PGP SIGNATURE----- --=-/L1EqtwFrut36SDlhyzZ--