Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp514167ybj; Thu, 7 May 2020 00:56:25 -0700 (PDT) X-Google-Smtp-Source: APiQypJnCezcHtjlO0bTSRC25WUX+tbZH2wvAeLFbkgKGcvvLcdkdywFHU78GSmoeoaNqcvi1op+ X-Received: by 2002:a50:a624:: with SMTP id d33mr11016167edc.58.1588838185756; Thu, 07 May 2020 00:56:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588838185; cv=none; d=google.com; s=arc-20160816; b=f6wVrwGK2FARrrBckRVSEci23vPX9/vEk+5erC+jfQzFDmh4sPECmxLl86Xqm1EbNo ET84KP9Plbq7b23TgcOpmyM0clRX0bt5rgdTB8QfstDxtM6ab72542W5d3BGu9mWzRCv ex/iRF2LqrZl6124kqRTapQ7DfbWX/lNk7oqNDYzAxuBywAQZE8uRX8d/VnPx+PVtz7R vMr65gYtOU1Xuu0+mQcmj74MUHLFyFJGKuzTS5ownlD6TShaCRH9NGxKEt8dpfEGXMQC MNsem491dIKBhn8QxisUkdsdUaESUuv5LeWRiebx0lIWypWGhhtAL0zmN6LFj+0+Yzt0 7+Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=dN2GmBMzXUVdLQt7YSu38GnTCLeLT37OnpzDzZp687w=; b=y+UB7vjSmxYiVxDmPGcf8jazdFEWy7wFqhFVNkR67DblTAlLOQE5UV+Usa32gA8lHL awtPAyDADO2TbrQaghfEx9rcz1zzHpZ1KkHV4cj5cZmQXUDZGGlHnezs8xDBN38qY/WH 3qBnAFF+9eG5sX+Kf3Bgh5aCX4CmDnbHYRdty88t07eY+PGgfteHi+zPIMAdxon92m2R fzlWlGGgUzbD+hCjgcaHWsY2o1s9lTESaMhYWuDzLvVVg7ZUB7YGz9e1JZMlgVu5JrvR /Yc3ViY0CyV19TM1EVP2qEhhN/qcP9AuxRYeG6Yqyo+gzhw0DXkNnduQZ+TjR4KFCBix Snog== 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 w21si2776449ejz.276.2020.05.07.00.56.02; Thu, 07 May 2020 00:56:25 -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 S1726074AbgEGHxq (ORCPT + 99 others); Thu, 7 May 2020 03:53:46 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:3834 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725862AbgEGHxq (ORCPT ); Thu, 7 May 2020 03:53:46 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 1481E6579506A334BFF2; Thu, 7 May 2020 15:53:44 +0800 (CST) Received: from [127.0.0.1] (10.67.102.197) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.487.0; Thu, 7 May 2020 15:53:38 +0800 Subject: ping; [PATCH v4] mtd: clear cache_state to avoid writing to bad blocks repeatedly To: , , CC: , , , , , References: <1585618319-119741-1-git-send-email-nixiaoming@huawei.com> From: Xiaoming Ni Message-ID: <528b61f3-bddb-d508-8f18-37cebec9e5eb@huawei.com> Date: Thu, 7 May 2020 15:53:32 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.1.2 MIME-Version: 1.0 In-Reply-To: <1585618319-119741-1-git-send-email-nixiaoming@huawei.com> Content-Type: text/plain; charset="gbk"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.102.197] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ping On 2020/3/31 9:31, Xiaoming Ni wrote: > 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; > } > > >