From patchwork Tue Feb 14 20:06:43 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Weinberger X-Patchwork-Id: 141188 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from merlin.infradead.org (unknown [IPv6:2001:4978:20e::2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 47BE11007D1 for ; Wed, 15 Feb 2012 07:56:29 +1100 (EST) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1RxPPC-0002G8-AR; Tue, 14 Feb 2012 20:55:18 +0000 Received: from galois.linutronix.de ([2001:470:1f0b:1c35:abcd:42:0:1]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1RxPOu-0002CL-Bw for linux-mtd@lists.infradead.org; Tue, 14 Feb 2012 20:55:03 +0000 Received: from custos.ou.linutronix.de ([212.62.202.73] helo=localhost.localdomain) by Galois.linutronix.de with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1RxPOt-0007rV-1c; Tue, 14 Feb 2012 21:54:59 +0100 From: Richard Weinberger To: linux-mtd@lists.infradead.org Subject: [RFC][PATCH 4/7] MTD: UBI: Make wl subsystem checkpoint aware Date: Tue, 14 Feb 2012 21:06:43 +0100 Message-Id: <1329250006-22944-5-git-send-email-rw@linutronix.de> X-Mailer: git-send-email 1.7.7 In-Reply-To: <1329250006-22944-1-git-send-email-rw@linutronix.de> References: <1329250006-22944-1-git-send-email-rw@linutronix.de> X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1, SHORTCIRCUIT=-0.0001 X-Spam-Note: CRM114 invocation failed X-Spam-Score: -1.9 (-) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-1.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 T_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: tglx@linutronix.de, dedekind1@gmail.com, linux-kernel@vger.kernel.org, Richard Weinberger , tim.bird@am.sony.com X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-mtd-bounces@lists.infradead.org Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Integrates checkpointing into the wl subsystem. Checkpointing deals with PEBs, it has to tell the wl subsystem which PEBs are currently used and must not touched by the wl thread. Signed-off-by: Richard Weinberger --- drivers/mtd/ubi/ubi.h | 5 + drivers/mtd/ubi/wl.c | 208 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 211 insertions(+), 2 deletions(-) diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index b4dca54..4e8e8d2 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -567,6 +567,11 @@ int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum); int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si); void ubi_wl_close(struct ubi_device *ubi); int ubi_thread(void *u); +int ubi_wl_get_cp_peb(struct ubi_device *ubi, int max_pnum); +int ubi_wl_put_cp_peb(struct ubi_device *ubi, int pnum, int torture); +#define is_cp_block(__ubi__, __e__) __is_cp_block(__ubi__, __e__->pnum) +int __is_cp_block(struct ubi_device *ubi, int pnum); +void ubi_flush_prot_queue(struct ubi_device *ubi); /* io.c */ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c index 0696e36..b3884fa 100644 --- a/drivers/mtd/ubi/wl.c +++ b/drivers/mtd/ubi/wl.c @@ -175,6 +175,32 @@ static int paranoid_check_in_pq(const struct ubi_device *ubi, #define paranoid_check_in_pq(ubi, e) 0 #endif +#ifdef CONFIG_MTD_UBI_CHECKPOINT +/** + * __is_cp_block - returns 1 if a PEB is currently used for checkpointing + * @ubi: UBI device description object + * @pnum: The to be checked PEB + */ +int __is_cp_block(struct ubi_device *ubi, int pnum) +{ + int i; + + if (!ubi->cp) + return 0; + + for (i = 0; i < ubi->cp->used_blocks; i++) + if (ubi->cp->peb[i] == pnum) + return 1; + + return 0; +} +#else +int __is_cp_block(struct ubi_device *ubi, int pnum) +{ + return 0; +} +#endif + /** * wl_tree_add - add a wear-leveling entry to a WL RB-tree. * @e: the wear-leveling entry to add @@ -379,15 +405,74 @@ static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max) return e; } +#ifdef CONFIG_MTD_UBI_CHECKPOINT /** - * ubi_wl_get_peb - get a physical eraseblock. + * find_early_wl_entry - find wear-leveling entry with a low pnum. + * @root: the RB-tree where to look for + * @max_pnum: highest possible pnum + * + * This function looks for a wear leveling entry containing a eb which + * is in front of the memory. + */ +static struct ubi_wl_entry *find_early_wl_entry(struct rb_root *root, int max_pnum) +{ + struct rb_node *p; + struct ubi_wl_entry *e, *victim = NULL; + + ubi_rb_for_each_entry(p, e, root, u.rb) { + if (e->pnum < max_pnum) { + victim = e; + max_pnum = e->pnum; + } + } + + return victim; +} + +/** + * ubi_wl_get_cp_peb - find a physical erase block with a given maximal number + * @ubi: UBI device description object + * @max_pnum: the highest acceptable erase block number + * + * The function returns a physical erase block with a given maximal number + * and removes it from the wl subsystem. + * Must be called with wl_lock held! + */ +int ubi_wl_get_cp_peb(struct ubi_device *ubi, int max_pnum) +{ + int ret = -ENOSPC; + struct ubi_wl_entry *e; + + if (!ubi->free.rb_node) { + ubi_err("no free eraseblocks"); + + goto out; + } + + e = find_early_wl_entry(&ubi->free, max_pnum); + if (!e) + goto out; + + ret = e->pnum; + + /* remove it from the free list, + * the wl subsystem does no longer know this erase block */ + rb_erase(&e->u.rb, &ubi->free); +out: + + return ret; +} +#endif + +/** + * __ubi_wl_get_peb - get a physical eraseblock. * @ubi: UBI device description object * @dtype: type of data which will be stored in this physical eraseblock * * This function returns a physical eraseblock in case of success and a * negative error code in case of failure. Might sleep. */ -int ubi_wl_get_peb(struct ubi_device *ubi, int dtype) +static int __ubi_wl_get_peb(struct ubi_device *ubi, int dtype) { int err, medium_ec; struct ubi_wl_entry *e, *first, *last; @@ -473,6 +558,48 @@ retry: return e->pnum; } +#ifdef CONFIG_MTD_UBI_CHECKPOINT +/* ubi_wl_get_peb - works exaclty like __ubi_wl_get_peb but keeps track of + * all checkpointing pools. + */ +int ubi_wl_get_peb(struct ubi_device *ubi, int dtype) +{ + struct ubi_cp_pool *pool; + + if (dtype == UBI_LONGTERM) + pool = &ubi->long_pool; + else if (dtype == UBI_SHORTTERM) + pool = &ubi->short_pool; + else if (dtype == UBI_UNKNOWN) + pool = &ubi->unk_pool; + else + BUG(); + + /* pool contains no free blocks, create a new one and write a checkoint */ + if (pool->used == pool->size) { + for (pool->size = 0; pool->size < pool->max_size; pool->size++) { + pool->pebs[pool->size] = __ubi_wl_get_peb(ubi, dtype); + if (pool->pebs[pool->size] < 0) + break; + } + + pool->used = 0; + ubi_update_checkpoint(ubi); + } + + /* we got not a single free PEB */ + if (!pool->size) + return -1; + + return pool->pebs[pool->used++]; +} +#else +int ubi_wl_get_peb(struct ubi_device *ubi, int dtype) +{ + return __ubi_wl_get_peb(ubi, dtype); +} +#endif + /** * prot_queue_del - remove a physical eraseblock from the protection queue. * @ubi: UBI device description object @@ -603,6 +730,21 @@ repeat: } /** + * ubi_flush_prot_queue - flushes the protection queue + * @ubi: UBI device description object + * + * This function flushes the protection queue such that checkpointing + * gets aware of them. + */ +void ubi_flush_prot_queue(struct ubi_device *ubi) +{ + int i; + + for (i = 0; i < UBI_PROT_QUEUE_LEN; i++) + serve_prot_queue(ubi); +} + +/** * schedule_ubi_work - schedule a work. * @ubi: UBI device description object * @wrk: the work to schedule @@ -638,6 +780,9 @@ static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, { struct ubi_work *wl_wrk; + ubi_assert(e); + ubi_assert(!is_cp_block(ubi, e)); + dbg_wl("schedule erasure of PEB %d, EC %d, torture %d", e->pnum, e->ec, torture); @@ -653,6 +798,53 @@ static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, return 0; } +#ifdef CONFIG_MTD_UBI_CHECKPOINT +/** + * ubi_wl_put_cp_peb - return a CP PEB to the wear-leveling sub-system + * + * see: ubi_wl_put_peb() + */ +int ubi_wl_put_cp_peb(struct ubi_device *ubi, int pnum, int torture) +{ + int i, err = 0; + struct ubi_wl_entry *e; + + dbg_wl("PEB %d", pnum); + ubi_assert(pnum >= 0); + ubi_assert(pnum < ubi->peb_count); + + spin_lock(&ubi->wl_lock); + e = ubi->lookuptbl[pnum]; + + /* This can happen if we recovered from a checkpoint the very + * frist time and writing now a new one. In this case the wl system + * has never seen any PEB used by the original checkpoint. + */ + if (!e) { + e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_ATOMIC); + if (!e) { + spin_unlock(&ubi->wl_lock); + return -ENOMEM; + } + + e->pnum = pnum; + /* use the ec value from the checkpoint */ + for (i = 0; i < UBI_CP_MAX_BLOCKS; i++) { + if (pnum == ubi->cp->peb[i]) { + e->ec = ubi->cp->ec[i]; + break; + } + } + } + + spin_unlock(&ubi->wl_lock); + + err = schedule_erase(ubi, e, torture); + + return err; +} +#endif + /** * wear_leveling_worker - wear-leveling worker function. * @ubi: UBI device description object @@ -1030,6 +1222,8 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, dbg_wl("erase PEB %d EC %d", pnum, e->ec); + ubi_assert(!is_cp_block(ubi, e)); + err = sync_erase(ubi, e, wl_wrk->torture); if (!err) { /* Fine, we've erased it successfully */ @@ -1464,6 +1658,9 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si) e->pnum = seb->pnum; e->ec = seb->ec; + + ubi_assert(!is_cp_block(ubi, e)); + ubi->lookuptbl[e->pnum] = e; if (schedule_erase(ubi, e, 0)) { kmem_cache_free(ubi_wl_entry_slab, e); @@ -1481,7 +1678,10 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si) e->pnum = seb->pnum; e->ec = seb->ec; ubi_assert(e->ec >= 0); + ubi_assert(!is_cp_block(ubi, e)); + wl_tree_add(e, &ubi->free); + ubi->lookuptbl[e->pnum] = e; } @@ -1496,6 +1696,10 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si) e->pnum = seb->pnum; e->ec = seb->ec; ubi->lookuptbl[e->pnum] = e; + + if (__is_cp_block(ubi, seb->pnum)) + continue; + if (!seb->scrub) { dbg_wl("add PEB %d EC %d to the used tree", e->pnum, e->ec);