Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5765732imm; Mon, 23 Jul 2018 05:49:43 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeqK5psmx+v4M+r+Za/u27RsG8QJUobTJ+ZfiSAu6kqoVUZnzE56E0c9ZVF5wgawiMSxG3j X-Received: by 2002:a63:8a41:: with SMTP id y62-v6mr11756823pgd.291.1532350183103; Mon, 23 Jul 2018 05:49:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532350183; cv=none; d=google.com; s=arc-20160816; b=PekKcUDJe0F1fwNTblxTn5bh0xavvNl23WI8i5I2KiFpUBL2vCXQ16kw6i3nfhxpdu +MhN/bgaXnowFqng5zzRZ77I0i4RzhKceBC/L5hKEiD7gJhx60zQUrvLiM/CNlefqA3D YjGi0vGZ9QqrECVCvS7cQv+08bR98n5TSx7d/nVuE+OD4ivsxwVuAd2O0JXObhVPJvEx Oe6fetjatDYZdI3Iqe6IQWBq8RfASwalnP5NIx4Hai0FD0iP/hjPv/wnrkiIYWFDJE8n Wqgq2Sxxq/zCm7tcmFKYjBBJZ1Yb0Ni/3xLHp7JtylR7Ke49+kiqNcW+cKqbvDKCmdkY mYcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=K3OX7TeLvoIHdqc3s7fqvMYe2wrWakow11zloUDXQhU=; b=EYwPVm/O+xwSloNTEPHLQtdoVpMyoVLCQMAZ//UpoDdpc4dSZcjSN/J20RaaeCmVr1 ky1ic0dXLN9FxyIIuVlrMGHb2RpxTQauGDXSk/1QJSnWHmdaeCMgNPLQ5AtevHmyblyD 4TfSti35Yjr4r1FGBXVrTDNautQ/U/HWO+UqfY8jQPeoxcDsRrXNhz9ZnIk7RPlNHqHJ NuAoMCa8625pJoFmdZ8dhisLRd+36iJjVbr52m7sT8qwn1bpZiZ2PpqVwfhZnBMa/Hco zu93XunY/ut1haGrHXFqCEN6eBtzM9z9bqt/nBBx0+zoaB9e4nB63jYP5XA/7d9DiJKK NPHw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w7-v6si8553401pgf.231.2018.07.23.05.49.28; Mon, 23 Jul 2018 05:49:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388818AbeGWNsx (ORCPT + 99 others); Mon, 23 Jul 2018 09:48:53 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:52054 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388389AbeGWNsw (ORCPT ); Mon, 23 Jul 2018 09:48:52 -0400 Received: from localhost (LFbn-1-12238-233.w90-92.abo.wanadoo.fr [90.92.53.233]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 43EABBB3; Mon, 23 Jul 2018 12:47:47 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sascha Hauer , Richard Weinberger Subject: [PATCH 4.4 107/107] ubi: fastmap: Erase outdated anchor PEBs during attach Date: Mon, 23 Jul 2018 14:42:41 +0200 Message-Id: <20180723122418.865465132@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180723122413.003644357@linuxfoundation.org> References: <20180723122413.003644357@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Sascha Hauer commit f78e5623f45bab2b726eec29dc5cefbbab2d0b1c upstream. The fastmap update code might erase the current fastmap anchor PEB in case it doesn't find any new free PEB. When a power cut happens in this situation we must not have any outdated fastmap anchor PEB on the device, because that would be used to attach during next boot. The easiest way to make that sure is to erase all outdated fastmap anchor PEBs synchronously during attach. Signed-off-by: Sascha Hauer Reviewed-by: Richard Weinberger Fixes: dbb7d2a88d2a ("UBI: Add fastmap core") Cc: Signed-off-by: Richard Weinberger Signed-off-by: Greg Kroah-Hartman --- drivers/mtd/ubi/wl.c | 77 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 20 deletions(-) --- a/drivers/mtd/ubi/wl.c +++ b/drivers/mtd/ubi/wl.c @@ -1509,6 +1509,46 @@ static void shutdown_work(struct ubi_dev } /** + * erase_aeb - erase a PEB given in UBI attach info PEB + * @ubi: UBI device description object + * @aeb: UBI attach info PEB + * @sync: If true, erase synchronously. Otherwise schedule for erasure + */ +static int erase_aeb(struct ubi_device *ubi, struct ubi_ainf_peb *aeb, bool sync) +{ + struct ubi_wl_entry *e; + int err; + + e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); + if (!e) + return -ENOMEM; + + e->pnum = aeb->pnum; + e->ec = aeb->ec; + ubi->lookuptbl[e->pnum] = e; + + if (sync) { + err = sync_erase(ubi, e, false); + if (err) + goto out_free; + + wl_tree_add(e, &ubi->free); + ubi->free_count++; + } else { + err = schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false); + if (err) + goto out_free; + } + + return 0; + +out_free: + wl_entry_destroy(ubi, e); + + return err; +} + +/** * ubi_wl_init - initialize the WL sub-system using attaching information. * @ubi: UBI device description object * @ai: attaching information @@ -1545,17 +1585,9 @@ int ubi_wl_init(struct ubi_device *ubi, list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) { cond_resched(); - e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); - if (!e) - goto out_free; - - e->pnum = aeb->pnum; - e->ec = aeb->ec; - ubi->lookuptbl[e->pnum] = e; - if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false)) { - wl_entry_destroy(ubi, e); + err = erase_aeb(ubi, aeb, false); + if (err) goto out_free; - } found_pebs++; } @@ -1615,6 +1647,8 @@ int ubi_wl_init(struct ubi_device *ubi, ubi_assert(!ubi->lookuptbl[e->pnum]); ubi->lookuptbl[e->pnum] = e; } else { + bool sync = false; + /* * Usually old Fastmap PEBs are scheduled for erasure * and we don't have to care about them but if we face @@ -1624,18 +1658,21 @@ int ubi_wl_init(struct ubi_device *ubi, if (ubi->lookuptbl[aeb->pnum]) continue; - e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); - if (!e) - goto out_free; + /* + * The fastmap update code might not find a free PEB for + * writing the fastmap anchor to and then reuses the + * current fastmap anchor PEB. When this PEB gets erased + * and a power cut happens before it is written again we + * must make sure that the fastmap attach code doesn't + * find any outdated fastmap anchors, hence we erase the + * outdated fastmap anchor PEBs synchronously here. + */ + if (aeb->vol_id == UBI_FM_SB_VOLUME_ID) + sync = true; - e->pnum = aeb->pnum; - e->ec = aeb->ec; - ubi_assert(!ubi->lookuptbl[e->pnum]); - ubi->lookuptbl[e->pnum] = e; - if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false)) { - wl_entry_destroy(ubi, e); + err = erase_aeb(ubi, aeb, sync); + if (err) goto out_free; - } } found_pebs++;