From patchwork Wed Oct 22 15:33:08 2008 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Korolev X-Patchwork-Id: 5347 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [18.85.46.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2E3EFDDE07 for ; Thu, 23 Oct 2008 02:34:55 +1100 (EST) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.68 #1 (Red Hat Linux)) id 1Ksfhw-00028c-M6; Wed, 22 Oct 2008 15:33:12 +0000 Received: from casper.infradead.org ([2001:770:15f::2]) by bombadil.infradead.org with esmtps (Exim 4.68 #1 (Red Hat Linux)) id 1Ksfhu-00028K-EG for linux-mtd@bombadil.infradead.org; Wed, 22 Oct 2008 15:33:10 +0000 Received: from localhost ([127.0.0.1]) by casper.infradead.org with esmtps (Exim 4.69 #1 (Red Hat Linux)) id 1Ksfht-0008C9-44; Wed, 22 Oct 2008 15:33:09 +0000 Date: Wed, 22 Oct 2008 16:33:08 +0100 (BST) From: Alexey Korolev To: dwmw2@infradead.org, dedekind@infradead.org, linux-mtd@lists.infradead.org Subject: [PATCH][nandsim] FIX RAM wasting via kmalloc Message-ID: User-Agent: Alpine 1.10 (LFD 962 2008-03-14) MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org See http://www.infradead.org/rpr.html X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.9 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-mtd-bounces@lists.infradead.org Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Hi All, Nandsim consumes ~2x more RAM than the density of simulated device. It becomes critical if we need to simulate 256MB NAND and run stress tests on it. We investigated the reasons. nandsim allocates space for pages using kmalloc function. The size of LP nand page is 2112 bytes. kmalloc gets space from slab pools by chunks 2^n. So if we need to kmalloc 2112 bytes, 4096 bytes will be consumed by system. The best way to avoid this issue would be using kmem_cache allocations. AFAIK this mechanism specially designed to handle cases when arrays of allocations are used. The following patch solves allocation issues. This patch is also available at my mtd-fixes git tree. Commit number is ef6bcbc4f806dc554abd6507b31c64d60167d175 Thanks, Alexey Signed-off-by: Alexey Korolev Acked-by: Artem Bityutskiy diff --git a/drivers/mtd/nand/nandsim.c b/drivers/mtd/nand/nandsim.c index ae7c577..0551cbf 100644 --- a/drivers/mtd/nand/nandsim.c +++ b/drivers/mtd/nand/nandsim.c @@ -295,6 +295,9 @@ struct nandsim { /* The simulated NAND flash pages array */ union ns_mem *pages; + /* Slab allocator for nand pages */ + struct kmem_cache *nand_pages_slab; + /* Internal buffer of page + OOB size bytes */ union ns_mem buf; @@ -420,8 +423,8 @@ static struct mtd_info *nsmtd; static u_char ns_verify_buf[NS_LARGEST_PAGE_SIZE]; /* - * Allocate array of page pointers and initialize the array to NULL - * pointers. + * Allocate array of page pointers, create slab allocation for an array + * and initialize the array by NULL pointers. * * RETURNS: 0 if success, -ENOMEM if memory alloc fails. */ @@ -437,6 +440,8 @@ static int alloc_device(struct nandsim *ns) for (i = 0; i < ns->geom.pgnum; i++) { ns->pages[i].byte = NULL; } + ns->nand_pages_slab = kmem_cache_create("nandsim", + ns->geom.pgszoob, 0, 0, NULL); return 0; } @@ -451,8 +456,10 @@ static void free_device(struct nandsim *ns) if (ns->pages) { for (i = 0; i < ns->geom.pgnum; i++) { if (ns->pages[i].byte) - kfree(ns->pages[i].byte); + kmem_cache_free(ns->nand_pages_slab, + ns->pages[i].byte); } + kmem_cache_destroy(ns->nand_pages_slab); vfree(ns->pages); } } @@ -1279,7 +1286,7 @@ static void erase_sector(struct nandsim *ns) for (i = 0; i < ns->geom.pgsec; i++) { if (mypage->byte != NULL) { NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i); - kfree(mypage->byte); + kmem_cache_free(ns->nand_pages_slab, mypage->byte); mypage->byte = NULL; } mypage++; @@ -1301,10 +1308,10 @@ static int prog_page(struct nandsim *ns, int num) /* * We allocate memory with GFP_NOFS because a flash FS may * utilize this. If it is holding an FS lock, then gets here, - * then kmalloc runs writeback which goes to the FS again - * and deadlocks. This was seen in practice. + * then kernel memory alloc runs writeback which goes to the FS + * again and deadlocks. This was seen in practice. */ - mypage->byte = kmalloc(ns->geom.pgszoob, GFP_NOFS); + mypage->byte = kmem_cache_alloc(ns->nand_pages_slab, GFP_NOFS); if (mypage->byte == NULL) { NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row); return -1;