Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp590867ybb; Sat, 28 Mar 2020 06:02:54 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtXfkd8Eu05adW2bd4OA4snTo4q9cCFVdcVMsLTSgZBn0RZmU/sbCq2UVAN+xHLb7RasCUN X-Received: by 2002:a05:6830:148d:: with SMTP id s13mr2546371otq.342.1585400573993; Sat, 28 Mar 2020 06:02:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585400573; cv=none; d=google.com; s=arc-20160816; b=VEhh/u67fvoiKwVAPYRsFYDAPVsB6353N7eBShlX3Pqf55iuDYB/ifg0HO9AGVHMvy qmLeuLnPc8faT6EF2gMh2I9uhbtLMRZdfXHWVIgVqcpNA6c5v8CQUAXKv36lcbPTT9cP TNKNb9uu37QnQiJAcXHcoTLTCqKkD2Nbxt2ne5vi9hOdcdfQ3vWj8kkkamCIa3Q+h/Es it8MJ9ZIUEmEqtrDaqT1owYYP4dpJ41FliZd4eldlhHVRHwK3Ef2JX06nVJPgg1GvIcH ZVni8x8Z3pIVfdxRzfbx1bJN7Efew+yKOcjliNTi1mXDsi/0TdtZXbQFh3mZzejC0PEb qjBQ== 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=YsGWQN4d0trxJC16g3nkfrjC6kM/BhBbVf/OZWOhAog=; b=y19A3TqryzeENhew5O8Co1KN5sWNqe1LLbBolOBTX3Pkt23lfbaskHOvCmBBI9R6ss JvcQTip6MWwnXxyNos+SmrNnUJ23kYEzs1iRZZ64ji9RiqCvU6yt0D7JXeAqFd0bQwZu X8nbDBmQ6TiI/2yWyqu1Tt0F7fMgbEMoz0rOy7zkytz+uTLxRJQOcn8yvS2R4dEFUspp +/vXiBX1w+meXupXJ9ObBHzXlWVdcmdAuC+pSbv50bbDIfqtFBvu/pi4GlZUhqKzdl3a e2Xrr5fFEXx1DYKXq885+2Wh8TfCIMZmanxFSrmrqCiqqW2kFRz5zQ5dYgIfJ1h/KFPu hc/A== 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 l63si3935136oif.161.2020.03.28.06.02.37; Sat, 28 Mar 2020 06:02:53 -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 S1726369AbgC1NCP (ORCPT + 99 others); Sat, 28 Mar 2020 09:02:15 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:56746 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726225AbgC1NCP (ORCPT ); Sat, 28 Mar 2020 09:02:15 -0400 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id CC0D1DB02DE44016DCAE; Sat, 28 Mar 2020 21:01:26 +0800 (CST) Received: from use12-sp2.huawei.com (10.67.189.174) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.487.0; Sat, 28 Mar 2020 21:01:20 +0800 From: Xiaoming Ni To: , , CC: , , , , , , Subject: [PATCH] mtd:clear cache_state to avoid writing to bad clocks repeatedly Date: Sat, 28 Mar 2020 21:01:17 +0800 Message-ID: <1585400477-65705-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 clocks repeatedly. Signed-off-by: Xiaoming Ni --- 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..98c25d6 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 clocks repeatedly */ - mtdblk->cache_state = STATE_EMPTY; - return 0; + if (ret == 0 || ret == -EIO) + mtdblk->cache_state = STATE_EMPTY; + return ret; } -- 1.8.5.6