From patchwork Tue Apr 16 07:38:46 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wang.bo116@zte.com.cn X-Patchwork-Id: 236846 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:770:15f::2]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B5C2B2C00F2 for ; Tue, 16 Apr 2013 17:39:37 +1000 (EST) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1US0U9-0006QK-MR; Tue, 16 Apr 2013 07:39:25 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1US0U7-0007K7-R1; Tue, 16 Apr 2013 07:39:23 +0000 Received: from mx7.zte.com.cn ([202.103.147.169] helo=zte.com.cn) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1US0U4-0007Jn-6G for linux-mtd@lists.infradead.org; Tue, 16 Apr 2013 07:39:21 +0000 Received: from mse02.zte.com.cn (unknown [10.30.3.21]) by Websense Email Security Gateway with ESMTPS id 0A80C70A3ED; Tue, 16 Apr 2013 15:38:48 +0800 (CST) Received: from notes_smtp.zte.com.cn ([10.30.1.239]) by mse02.zte.com.cn with ESMTP id r3G7cnoM003290; Tue, 16 Apr 2013 15:38:49 +0800 (GMT-8) (envelope-from wang.bo116@zte.com.cn) To: linux-mtd@lists.infradead.org Subject: MIME-Version: 1.0 X-KeepSent: 4A055B7D:2DBB5049-48257B4F:002965EA; type=4; name=$KeepSent X-Mailer: Lotus Notes Release 6.5.6 March 06, 2007 Message-ID: From: wang.bo116@zte.com.cn Date: Tue, 16 Apr 2013 15:38:46 +0800 X-MIMETrack: Serialize by Router on notes_smtp/zte_ltd(Release 8.5.3FP1 HF212|May 23, 2012) at 2013-04-16 15:38:35, Serialize complete at 2013-04-16 15:38:35 X-MAIL: mse02.zte.com.cn r3G7cnoM003290 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130416_033920_735089_1726BCA5 X-CRM114-Status: GOOD ( 10.41 ) X-Spam-Score: -3.2 (---) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-3.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [202.103.147.169 listed in list.dnswl.org] -0.6 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: richard.weinberger@gmail.com, liu.dong3@zte.com.cn, wang.bo116@zte.com.cn, linux-kernel@vger.kernel.org, cui.yunfeng@zte.com.cn X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org UBI: fix memory leak when use fastmap Sorry, there is something wrong with the previous patch's format, try to submit it again. When use ubi fastmap, there is a memory leak which will make destroy_ai() fail to free the slab. The following patch base on linux-3.9-rc6 fix this problem. Signed-off-by: Wang bo Tested-by: Wang bo Reviewed-by: Cui Yunfeng diff -uprN old_ubi/attach.c new_ubi/attach.c --- old_ubi/attach.c 2013-04-08 03:49:54.000000000 +0000 +++ new_ubi/attach.c 2013-04-16 03:22:47.343750000 +0000 @@ -1212,6 +1212,30 @@ static void destroy_ai(struct ubi_attach kfree(ai); } +static struct ubi_attach_info *alloc_ai(const char *slab_name) +{ + struct ubi_attach_info *ai; + + ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL); + if (!ai) + return ai; + + INIT_LIST_HEAD(&ai->corr); + INIT_LIST_HEAD(&ai->free); + INIT_LIST_HEAD(&ai->erase); + INIT_LIST_HEAD(&ai->alien); + ai->volumes = RB_ROOT; + ai->aeb_slab_cache = kmem_cache_create(slab_name, + sizeof(struct ubi_ainf_peb), + 0, 0, NULL); + if (!ai->aeb_slab_cache) { + kfree(ai); + ai = NULL; + } + + return ai; +} + /** * scan_all - scan entire MTD device. * @ubi: UBI device description object @@ -1315,8 +1339,13 @@ static int scan_fast(struct ubi_device * int err, pnum, fm_anchor = -1; unsigned long long max_sqnum = 0; + struct ubi_attach_info *fm_temp_ai = NULL; err = -ENOMEM; + fm_temp_ai = alloc_ai("ubi_scan_fastmap_slab_cache"); + if (!fm_temp_ai) + goto out; + ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); if (!ech) goto out; @@ -1331,7 +1360,7 @@ static int scan_fast(struct ubi_device * cond_resched(); dbg_gen("process PEB %d", pnum); - err = scan_peb(ubi, ai, pnum, &vol_id, &sqnum); + err = scan_peb(ubi, fm_temp_ai, pnum, &vol_id, &sqnum); if (err < 0) goto out_vidh; @@ -1343,6 +1372,7 @@ static int scan_fast(struct ubi_device * ubi_free_vid_hdr(ubi, vidh); kfree(ech); + destroy_ai(fm_temp_ai); if (fm_anchor < 0) return UBI_NO_FASTMAP; @@ -1351,6 +1381,7 @@ static int scan_fast(struct ubi_device * out_vidh: ubi_free_vid_hdr(ubi, vidh); + destroy_ai(fm_temp_ai); out_ech: kfree(ech); out: @@ -1359,29 +1390,6 @@ out: #endif -static struct ubi_attach_info *alloc_ai(const char *slab_name) -{ - struct ubi_attach_info *ai; - - ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL); - if (!ai) - return ai; - - INIT_LIST_HEAD(&ai->corr); - INIT_LIST_HEAD(&ai->free); - INIT_LIST_HEAD(&ai->erase); - INIT_LIST_HEAD(&ai->alien); - ai->volumes = RB_ROOT; - ai->aeb_slab_cache = kmem_cache_create(slab_name, - sizeof(struct ubi_ainf_peb), - 0, 0, NULL); - if (!ai->aeb_slab_cache) { - kfree(ai); - ai = NULL; - } - - return ai; -} /** * ubi_attach - attach an MTD device. @@ -1419,7 +1427,7 @@ int ubi_attach(struct ubi_device *ubi, i return -ENOMEM; } - err = scan_all(ubi, ai, UBI_FM_MAX_START); + err = scan_all(ubi, ai, 0); } } #else diff -uprN old_ubi/fastmap.c new_ubi/fastmap.c --- old_ubi/fastmap.c 2013-04-08 03:49:54.000000000 +0000 +++ new_ubi/fastmap.c 2013-04-16 03:22:17.468750000 +0000 @@ -552,21 +552,8 @@ static int ubi_attach_fastmap(struct ubi INIT_LIST_HEAD(&used); INIT_LIST_HEAD(&free); INIT_LIST_HEAD(&eba_orphans); - INIT_LIST_HEAD(&ai->corr); - INIT_LIST_HEAD(&ai->free); - INIT_LIST_HEAD(&ai->erase); - INIT_LIST_HEAD(&ai->alien); - ai->volumes = RB_ROOT; ai->min_ec = UBI_MAX_ERASECOUNTER; - ai->aeb_slab_cache = kmem_cache_create("ubi_ainf_peb_slab", - sizeof(struct ubi_ainf_peb), - 0, 0, NULL); - if (!ai->aeb_slab_cache) { - ret = -ENOMEM; - goto fail; - } - fmsb = (struct ubi_fm_sb *)(fm_raw); ai->max_sqnum = fmsb->sqnum; fm_pos += sizeof(struct ubi_fm_sb);