Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3018607ybb; Mon, 30 Mar 2020 18:34:22 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvvDkDCKptonETACcNpLBq82Q7OGX4gAxGWd0RzSkFoS2wIv+xdfyCX0InHC1Z+3K8jfrzM X-Received: by 2002:a05:6830:1d95:: with SMTP id y21mr11735255oti.180.1585618461906; Mon, 30 Mar 2020 18:34:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585618461; cv=none; d=google.com; s=arc-20160816; b=ePIpnPYJyZEMlfyn/p/N7AyQ56OI2m7qlr8HCQBpUccq4N8aItNhVvsJp9ZaEzulYn jT/Nl/r42HDFYPdsMAFDT3weBtIMh39Kxq/kgMziz+qwQD5GnjWMQcdzRBAI/mthacGV 9l1kfGVSXzEX3Y3SGX3KjfH15eyyF9TxAJfs9hMn3oijl4+2u2JvvE4+2hkpliN7fMsw PHNkm8XZ+45dT4QEAxtAzvYF9AYaU5me07m28FQZ7T5SewaCH06dJeEkbUsAURpHxp9O 723RKUuBssdzL7STtzVrcFbsuB8nq4/7ULTACILWnK0YiHaPsI7SEUwDUkw9jzrRLwGs LHzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=dpub8JMDKFkxqeVx1Sa+KYuaidMFe6lNyyHNy/9Rqto=; b=eX1F51GItD5JEXRQRb3SF6FwyyJdHebd4tzjHnWH4+rz3ATNtC2MpHKTYrp4nVVWQS 8+qLK04jIqv4/3UDDMbeU8viexDkmNIzUo3rK9pGO96qPwhQvHTU/b9bHUpgP1k1+lcC 1ndV0WjXLuLd4eI+7qbxvcj7/LY5rmIgIrGGqezzPzgapy4DWWP8YuhBtrYPsCZDv7MI z1sk0c75NQtQV/NXAsM4vuCfijQHPLPPXdFZo9dQoU/hqAllfCRqf0HXA9KsQmaVLxTA J/zfKYHy7gOi9nJgpEFaCmtUt662Dk/io8bPmz1L18c9CFZOuUSDXmzGH99xcDxmNnUK tT8A== 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 b6si6831286otj.275.2020.03.30.18.34.08; Mon, 30 Mar 2020 18:34:21 -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 S1729358AbgCaBcL (ORCPT + 99 others); Mon, 30 Mar 2020 21:32:11 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:12654 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729021AbgCaBcL (ORCPT ); Mon, 30 Mar 2020 21:32:11 -0400 Received: from DGGEMS409-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 38501D422F8AFB709CCE; Tue, 31 Mar 2020 09:32:08 +0800 (CST) Received: from use12-sp2.huawei.com (10.67.189.174) by DGGEMS409-HUB.china.huawei.com (10.3.19.209) with Microsoft SMTP Server id 14.3.487.0; Tue, 31 Mar 2020 09:32:01 +0800 From: Xiaoming Ni To: , , , CC: , , , , , , Subject: [PATCH v4] mtd: clear cache_state to avoid writing to bad blocks repeatedly Date: Tue, 31 Mar 2020 09:31:59 +0800 Message-ID: <1585618319-119741-1-git-send-email-nixiaoming@huawei.com> X-Mailer: git-send-email 1.8.5.6 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.189.174] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The function call process is as follows: mtd_blktrans_work() while (1) do_blktrans_request() mtdblock_writesect() do_cached_write() write_cached_data() /*if cache_state is STATE_DIRTY*/ erase_write() write_cached_data() returns failure without modifying cache_state and cache_offset. So when do_cached_write() is called again, write_cached_data() will be called again to perform erase_write() on the same cache_offset. But if this cache_offset points to a bad block, erase_write() will always return -EIO. Writing to this mtdblk is equivalent to losing the current data, and repeatedly writing to the bad block. Repeatedly writing a bad block has no real benefits, but brings some negative effects: 1 Lost subsequent data 2 Loss of flash device life 3 erase_write() bad blocks are very time-consuming. For example: the function do_erase_oneblock() in chips/cfi_cmdset_0020.c or chips/cfi_cmdset_0002.c may take more than 20 seconds to return Therefore, when erase_write() returns -EIO in write_cached_data(), clear cache_state to avoid writing to bad blocks repeatedly. Signed-off-by: Xiaoming Ni Reviewed-by: Miquel Raynal --- drivers/mtd/mtdblock.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/mtd/mtdblock.c b/drivers/mtd/mtdblock.c index 078e0f6..32e52d8 100644 --- a/drivers/mtd/mtdblock.c +++ b/drivers/mtd/mtdblock.c @@ -89,8 +89,6 @@ static int write_cached_data (struct mtdblk_dev *mtdblk) ret = erase_write (mtd, mtdblk->cache_offset, mtdblk->cache_size, mtdblk->cache_data); - if (ret) - return ret; /* * Here we could arguably set the cache state to STATE_CLEAN. @@ -98,9 +96,14 @@ static int write_cached_data (struct mtdblk_dev *mtdblk) * be notified if this content is altered on the flash by other * means. Let's declare it empty and leave buffering tasks to * the buffer cache instead. + * + * If this cache_offset points to a bad block, data cannot be + * written to the device. Clear cache_state to avoid writing to + * bad blocks repeatedly. */ - mtdblk->cache_state = STATE_EMPTY; - return 0; + if (ret == 0 || ret == -EIO) + mtdblk->cache_state = STATE_EMPTY; + return ret; } -- 1.8.5.6