Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp331300ybk; Sat, 9 May 2020 04:19:13 -0700 (PDT) X-Google-Smtp-Source: APiQypJQP91/679DIWJqV/vRHpLBfdIewDmGJ0IkA1qhJNSHBOYzf3gGQAPk03g+lIs1ZJyAZM7U X-Received: by 2002:a05:6402:1684:: with SMTP id a4mr5607617edv.99.1589023152990; Sat, 09 May 2020 04:19:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589023152; cv=none; d=google.com; s=arc-20160816; b=dAtI9QsAah95U9IjWv6756vtH7nMnzjgXie1xrGmvXPAuBWu7g8zHudRkHHLYc/a55 S6NmYOxnXXa4ZGXuhCeLb97ncgmY2gpcQqnVAJmKaP/rNCc49OY+aAwARUzsg7OOXi9i n92XnSNb7UItfAumWLf4E/AeOh4NUzz7LMudILrBSqZNumwh+R9GzAzshLkXywgl+DIo bEz4kAlD9mXvO4D3o8FGy395OEftp40VrJSZZFAn3LuChhs7UXFpyRHvaBUAUvGLIxXO 1cQ0XzM9xeLHFX0vj+ovhLbeaGKcEUm5kV4CsQENF48fptKc0eSGwPvcsAmc8kKx096I Zg/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=ysg8CTXYTf/IN6lBF3EyoSlB7iY5m5w6uZlP2Qaxe0Y=; b=peMbBkpeckVHA7Xr9w4RTUGwTLWaaGfXLnwAkDd+Hc+TT6rcYuWGiWqrKIMC65uLEL jhGXoPrhgJUlOKBfPMQAhmVcTAH45yTdHgQn/1i8rMMyvkhLsI+J3NQTqU3fRh6E0heG fuXm3+aMSBZdHYbRrs4eSzz+dKqxV1m8fizz7cyb8akK9Kw7yE+9LbyyMT/BwGm5Vp+x zdobbIjj6v3deoqJN8cQRWxTeQRcUKFxBp60s+NRzu9t4RtjRlXHHkiviaOB3Qgrq2f1 QVltfaVouHvpD1VRnT7M6OUghYOqHvwNZxZ/RHlp1k73dJWaLy9gpEAuAp8xBdUBMFyT 5eAw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cq16si1589832edb.479.2020.05.09.04.18.49; Sat, 09 May 2020 04:19:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728365AbgEILPQ (ORCPT + 99 others); Sat, 9 May 2020 07:15:16 -0400 Received: from smtp2207-205.mail.aliyun.com ([121.197.207.205]:49555 "EHLO smtp2207-205.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728285AbgEILO6 (ORCPT ); Sat, 9 May 2020 07:14:58 -0400 X-Alimail-AntiSpam: AC=CONTINUE;BC=0.07436282|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_alarm|0.050626-0.000244671-0.949129;FP=0|0|0|0|0|-1|-1|-1;HT=e02c03303;MF=liaoweixiong@allwinnertech.com;NM=1;PH=DS;RN=20;RT=20;SR=0;TI=SMTPD_---.HVopGr-_1589022874; Received: from PC-liaoweixiong.allwinnertech.com(mailfrom:liaoweixiong@allwinnertech.com fp:SMTPD_---.HVopGr-_1589022874) by smtp.aliyun-inc.com(10.147.44.129); Sat, 09 May 2020 19:14:49 +0800 From: WeiXiong Liao To: Kees Cook , Anton Vorontsov , Colin Cross , Tony Luck , Jonathan Corbet , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , Mauro Carvalho Chehab , "David S. Miller" , Rob Herring , Greg Kroah-Hartman , Pavel Tatashin Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org, WeiXiong Liao Subject: [PATCH v5 09/12] pstore/zone: Provide way to skip "broken" zone for MTD devices Date: Sat, 9 May 2020 19:14:11 +0800 Message-Id: <1589022854-19821-10-git-send-email-liaoweixiong@allwinnertech.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1589022854-19821-1-git-send-email-liaoweixiong@allwinnertech.com> References: <1589022854-19821-1-git-send-email-liaoweixiong@allwinnertech.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org One requirement to support MTD devices in pstore/zone is having a way to declare certain regions as broken. Add this support to pstore/zone. The MTD driver should return -ENOMSG when encountering a bad region, which tells pstore/zone to skip and try the next one. Signed-off-by: WeiXiong Liao Link: https://lore.kernel.org/r/1585126506-18635-9-git-send-email-liaoweixiong@allwinnertech.com Signed-off-by: Kees Cook --- fs/pstore/blk.c | 10 +++++-- fs/pstore/zone.c | 65 ++++++++++++++++++++++++++++++++++++--------- include/linux/pstore_blk.h | 3 ++- include/linux/pstore_zone.h | 12 ++++++--- 4 files changed, 71 insertions(+), 19 deletions(-) diff --git a/fs/pstore/blk.c b/fs/pstore/blk.c index 84ca98923d8a..bf27dbd18db2 100644 --- a/fs/pstore/blk.c +++ b/fs/pstore/blk.c @@ -101,9 +101,12 @@ * means error. * @write: The same as @read, but the following error number: * -EBUSY means try to write again later. + * -ENOMSG means to try next zone. * @panic_write:The write operation only used for panic case. It's optional - * if you do not care panic log. The parameters and return value - * are the same as @read. + * if you do not care panic log. The parameters are relative + * value to storage. + * On success, the number of bytes should be returned, others + * excluding -ENOMSG mean error. -ENOMSG means to try next zone. */ struct psblk_device { unsigned long total_size; @@ -315,6 +318,9 @@ static ssize_t psblk_blk_panic_write(const char *buf, size_t size, /* size and off must align to SECTOR_SIZE for block device */ ret = blkdev_panic_write(buf, off >> SECTOR_SHIFT, size >> SECTOR_SHIFT); + /* try next zone */ + if (ret == -ENOMSG) + return ret; return ret ? -EIO : size; } diff --git a/fs/pstore/zone.c b/fs/pstore/zone.c index a51594f95347..9a9a4a7134d0 100644 --- a/fs/pstore/zone.c +++ b/fs/pstore/zone.c @@ -247,6 +247,9 @@ static int psz_zone_write(struct pstore_zone *zone, return 0; dirty: + /* no need to mark dirty if going to try next zone */ + if (wcnt == -ENOMSG) + return -ENOMSG; atomic_set(&zone->dirty, true); /* flush dirty zones nicely */ if (wcnt == -EBUSY && !is_on_panic()) @@ -389,7 +392,11 @@ static int psz_recover_oops_meta(struct psz_context *cxt) return -EINVAL; rcnt = info->read((char *)buf, len, zone->off); - if (rcnt != len) { + if (rcnt == -ENOMSG) { + pr_debug("%s with id %lu may be broken, skip\n", + zone->name, i); + continue; + } else if (rcnt != len) { pr_err("read %s with id %lu failed\n", zone->name, i); return (int)rcnt < 0 ? (int)rcnt : -EIO; } @@ -724,24 +731,58 @@ static void psz_write_kmsg_hdr(struct pstore_zone *zone, hdr->counter = 0; } +/* + * In case zone is broken, which may occur to MTD device, we try each zones, + * start at cxt->oops_write_cnt. + */ static inline int notrace psz_oops_write_record(struct psz_context *cxt, struct pstore_record *record) { + int ret = -EBUSY; size_t size, hlen; struct pstore_zone *zone; - unsigned int zonenum; + unsigned int i; - zonenum = cxt->oops_write_cnt; - zone = cxt->opszs[zonenum]; - if (unlikely(!zone)) - return -ENOSPC; - cxt->oops_write_cnt = (zonenum + 1) % cxt->oops_max_cnt; + for (i = 0; i < cxt->oops_max_cnt; i++) { + unsigned int zonenum, len; - pr_debug("write %s to zone id %d\n", zone->name, zonenum); - psz_write_kmsg_hdr(zone, record); - hlen = sizeof(struct psz_oops_header); - size = min_t(size_t, record->size, zone->buffer_size - hlen); - return psz_zone_write(zone, FLUSH_ALL, record->buf, size, hlen); + zonenum = (cxt->oops_write_cnt + i) % cxt->oops_max_cnt; + zone = cxt->opszs[zonenum]; + if (unlikely(!zone)) + return -ENOSPC; + + /* avoid destorying old data, allocate a new one */ + len = zone->buffer_size + sizeof(*zone->buffer); + zone->oldbuf = zone->buffer; + zone->buffer = kzalloc(len, GFP_KERNEL); + if (!zone->buffer) { + zone->buffer = zone->oldbuf; + return -ENOMEM; + } + zone->buffer->sig = zone->oldbuf->sig; + + pr_debug("write %s to zone id %d\n", zone->name, zonenum); + psz_write_kmsg_hdr(zone, record); + hlen = sizeof(struct psz_oops_header); + size = min_t(size_t, record->size, zone->buffer_size - hlen); + ret = psz_zone_write(zone, FLUSH_ALL, record->buf, size, hlen); + if (likely(!ret || ret != -ENOMSG)) { + cxt->oops_write_cnt = zonenum + 1; + cxt->oops_write_cnt %= cxt->oops_max_cnt; + /* no need to try next zone, free last zone buffer */ + kfree(zone->oldbuf); + zone->oldbuf = NULL; + return ret; + } + + pr_debug("zone %u may be broken, try next dmesg zone\n", + zonenum); + kfree(zone->buffer); + zone->buffer = zone->oldbuf; + zone->oldbuf = NULL; + } + + return -EBUSY; } static int notrace psz_oops_write(struct psz_context *cxt, diff --git a/include/linux/pstore_blk.h b/include/linux/pstore_blk.h index d8f609e60288..828b0763d477 100644 --- a/include/linux/pstore_blk.h +++ b/include/linux/pstore_blk.h @@ -14,7 +14,8 @@ * @start_sect: start sector to block device * @sects: sectors count on buf * - * Return: On success, zero should be returned. Others mean error. + * Return: On success, zero should be returned. Others excluding -ENOMSG + * mean error. -ENOMSG means to try next zone. * * Panic write to block device must be aligned to SECTOR_SIZE. */ diff --git a/include/linux/pstore_zone.h b/include/linux/pstore_zone.h index 94f441b8b616..ddb3dfea4ea6 100644 --- a/include/linux/pstore_zone.h +++ b/include/linux/pstore_zone.h @@ -23,11 +23,15 @@ * @read: The general read operation. Both of the function parameters * @size and @offset are relative value to storage. * On success, the number of bytes should be returned, others - * means error. - * @write: The same as @read, but -EBUSY means try to write again later. + * mean error. + * @write: The same as @read, but the following error number: + * -EBUSY means try to write again later. + * -ENOMSG means to try next zone. * @panic_write:The write operation only used for panic case. It's optional - * if you do not care panic log. The parameters and return value - * are the same as @read. + * if you do not care panic log. The parameters are relative + * value to storage. + * On success, the number of bytes should be returned, others + * excluding -ENOMSG mean error. -ENOMSG means to try next zone. */ struct pstore_zone_info { struct module *owner; -- 1.9.1