Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2467616ybb; Mon, 30 Mar 2020 06:46:37 -0700 (PDT) X-Google-Smtp-Source: ADFU+vslfKJoRPgjosejEUfzu3IlTE+ESid8+WVFSFcNIBh7A0IEHssAjTgPaSsaEXBkHtxmItQC X-Received: by 2002:a9d:6b02:: with SMTP id g2mr9355310otp.340.1585575997106; Mon, 30 Mar 2020 06:46:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585575997; cv=none; d=google.com; s=arc-20160816; b=v0zGdUazafMCrAe3r979io2paBgfpEu4A+rW9J2bf28H56nHsVGbWDFSb1CBcsyS0d zaPHxYfqTjwc0Hvq/KE8CNmT408ZZU+F/hO5CBfT25ZeTs/BZchSNe5LHRI+f2OVliJM AzsXpBzebP3Cgx1U1OzY3oEa9m5BUkr1CaLPwgvzIIEvtecjVIkASFuZ9IUpMJLYyYhU clS2t4SIgQly4nWrj4xavJ2OvX2oJ2DRlR9k1nNo0LUgNolBFvagGXtdW/YIVpSlmwgm oiEVLHFjl1q5yuOO14UpesL/DiFull1rNCXS7J13Y22AalhQKaS4HgyMiAH4sTbDMFLh wb0Q== 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=BpNoxjxsyUQISKRpzzXMR6/zoksills8/jIH7S5gcQs=; b=oBb1PHi/cyBHhVf6/3Qk45GcoigelYkatelWr+tusYitITKHplC/i3JeH+y+WB8Wrf yBjAY1X/XvIpMX0C+CfbI5oTaYGbOx6rxMpng7q6s+kLLPoB7/EH3FQRQJj2Hs6g9AZZ +i6N5CYQEE0Adwo7GvJTLWcvmPbDNvVRLI9fj77iM3Yseu9y/rCweHR/bk4+ojfIbCf7 GW0NYGm6TMr4+OOzsIx8/7pbh/bRlCYrO7oa+lzYq3r7BQnSEdPHW5rIycVTxRscMnfI zb8RPglBasRK5K8dL9+XGVtzEEuZyhVDIxdUHt8CbYCkEs/GHCnD/tfltFQ5AGj34god bu5Q== 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 u207si5730623oia.176.2020.03.30.06.46.14; Mon, 30 Mar 2020 06:46:37 -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 S1728397AbgC3Npm (ORCPT + 99 others); Mon, 30 Mar 2020 09:45:42 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:12149 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728065AbgC3Npm (ORCPT ); Mon, 30 Mar 2020 09:45:42 -0400 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id E5D7B9A16427D494C05D; Mon, 30 Mar 2020 21:45:36 +0800 (CST) Received: from use12-sp2.huawei.com (10.67.189.174) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.487.0; Mon, 30 Mar 2020 21:45:28 +0800 From: Xiaoming Ni To: , , , CC: , , , , , , , Subject: [PATCH v3] mtd:fix cache_state to avoid writing to bad blocks repeatedly Date: Mon, 30 Mar 2020 21:45:25 +0800 Message-ID: <1585575925-84017-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 Cc: stable@vger.kernel.org --- 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