Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2448732ybb; Mon, 30 Mar 2020 06:24:40 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuyEGt8jzmr+210niT20Qxjum3WeyaNm2mHT8L+LnXoVxZaooWDZjCtTw8Zn6dQoedw2hO9 X-Received: by 2002:aca:ea05:: with SMTP id i5mr7743517oih.162.1585574680040; Mon, 30 Mar 2020 06:24:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585574680; cv=none; d=google.com; s=arc-20160816; b=xqRQZRt7xQeNx+7QK7rvDzoNtVWzBz6v6z7EXtce2DXKhKJSugLHdgmxcl4DKma33Z EdCF2n11HRBWaUggpcAlPwCdg/w9Na+oU+/WYgp6CmHgSK1GjH6pQhDhvMgHLpN7X5Ra /D+wyp533VDRX4lKuqSoTe5pNjrOaMid7cH33QKs7TDOiX4xXxYwLlKAxNXemj9y2FCD /G0wxlbAgFD4ro+gMXLneqAbzuefauMUiX4RQqLYErSPA0+fMORcYS03hUQdhp+bILj+ czWkybZyWDMBL4rjb3bZmq1hUcdGNkn8gQfLHDYhyDParTsGuYAPRjY2mDDM8kpi7wD9 Tfzg== 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=V2P6A07kKG3i1En4WWYYdgUFCFWj/biAmjsGd2zppImkLCb99lNQsrIrcboyw2lBqx CyYDvneUIUlZKalpTdbRIqwdPqTNmc+9MBSO6TwR5U/pnwMULrDMhtd6Q9V2vCUgS2Xz Mn7p4KNDUWBEZ+8I2PqNAt8PiUHYYEECnMcpJPOCb+54lgWCwuouO8iPCUDpR6DT5jJx FAV0fnkDWv2aJ+YQxbzQjuuRamCcXSfdpTFi7/z2br/bzyxOI7n/59nXKOGeESfBZSeQ 1Erng5KcdjqnTDXuyOdACrbItz5uFNZ/0+BKT6r9lH9+L+6N1y9t2C71P5BanoYiWrR8 pw5g== 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 o20si6107659ota.17.2020.03.30.06.24.27; Mon, 30 Mar 2020 06:24:40 -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 S1730159AbgC3NLb (ORCPT + 99 others); Mon, 30 Mar 2020 09:11:31 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:60558 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730070AbgC3NLb (ORCPT ); Mon, 30 Mar 2020 09:11:31 -0400 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 26A80442C2BD11412B18; Mon, 30 Mar 2020 21:11:23 +0800 (CST) Received: from use12-sp2.huawei.com (10.67.189.174) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.487.0; Mon, 30 Mar 2020 21:11:12 +0800 From: Xiaoming Ni To: , , CC: , , , , , , Subject: [PATCH v2] mtd:clear cache_state to avoid writing to bad blocks repeatedly Date: Mon, 30 Mar 2020 21:11:09 +0800 Message-ID: <1585573869-81863-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