From patchwork Fri Apr 28 08:50:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226086 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64248C7EE21 for ; Fri, 28 Apr 2023 08:51:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345445AbjD1Ivy (ORCPT ); Fri, 28 Apr 2023 04:51:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345598AbjD1Iva (ORCPT ); Fri, 28 Apr 2023 04:51:30 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D36046AC; Fri, 28 Apr 2023 01:51:25 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Q75tb0MWXz4f3kkL; Fri, 28 Apr 2023 16:51:19 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S5; Fri, 28 Apr 2023 16:51:20 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 01/10] block/badblocks: only set bb->changed when badblocks changes Date: Fri, 28 Apr 2023 16:50:11 +0800 Message-Id: <20230428085020.2283981-2-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S5 X-Coremail-Antispam: 1UD129KBjvJXoW7AFykGw4rJr4xJw4Dtry8Grg_yoW8CrW8pr 9xC3Wftryjg3WIgF1UZ3W7Kw1FgayfXF48Gw4ay345GryxG3s3tF1vq34Yqa4j9r1a9rnI qa4Fgryjva4DC37anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPEb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUGw A2048vs2IY020Ec7CjxVAFwI0_JFI_Gr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV WxJr0_GcWl84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ GcCE3s1lnxkEFVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64 xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j 6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x02628vn2 kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_ Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17 CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0 I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I 8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73 UjIFyTuYvjxU7D73DUUUU X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan bb->changed and unacked_exist is set and badblocks_update_acked() is involked even if no badblocks changes in badblocks_set(). Only update them when badblocks changes. Fixes: 9e0e252a048b ("badblocks: Add core badblock management code") Signed-off-by: Li Nan --- block/badblocks.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index 1b4caa42c5f1..7e6ebe2ac12c 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -166,6 +166,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, int lo, hi; int rv = 0; unsigned long flags; + bool changed = false; if (bb->shift < 0) /* badblocks are disabled */ @@ -229,6 +230,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, s = a + BB_MAX_LEN; } sectors = e - s; + changed = true; } } if (sectors && hi < bb->count) { @@ -259,6 +261,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, sectors = e - s; lo = hi; hi++; + changed = true; } } if (sectors == 0 && hi < bb->count) { @@ -277,6 +280,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, memmove(p + hi, p + hi + 1, (bb->count - hi - 1) * 8); bb->count--; + changed = true; } } while (sectors) { @@ -299,14 +303,17 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, p[hi] = BB_MAKE(s, this_sectors, acknowledged); sectors -= this_sectors; s += this_sectors; + changed = true; } } - bb->changed = true; - if (!acknowledged) - bb->unacked_exist = true; - else - badblocks_update_acked(bb); + if (changed) { + bb->changed = changed; + if (!acknowledged) + bb->unacked_exist = true; + else + badblocks_update_acked(bb); + } write_sequnlock_irqrestore(&bb->lock, flags); return rv; From patchwork Fri Apr 28 08:50:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226084 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A83DC77B61 for ; Fri, 28 Apr 2023 08:51:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345296AbjD1Ivx (ORCPT ); Fri, 28 Apr 2023 04:51:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345599AbjD1Iva (ORCPT ); Fri, 28 Apr 2023 04:51:30 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03B4FE59; Fri, 28 Apr 2023 01:51:23 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Q75tb3Ty6z4f3pCS; Fri, 28 Apr 2023 16:51:19 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S6; Fri, 28 Apr 2023 16:51:20 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 02/10] block/badblocks: fix badblocks loss when badblocks combine Date: Fri, 28 Apr 2023 16:50:12 +0800 Message-Id: <20230428085020.2283981-3-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S6 X-Coremail-Antispam: 1UD129KBjvJXoWxJrW8urW3AryDWw17Kry3urg_yoW8XrW5pw 1fZwnIgr1IgFyv9F4UX3WDGF1Sg3Z7JF45Ga1Yqa48uFyDAw1xKF4vq3WYvrW0gF4xXF90 va1rWryDZFyfG37anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPEb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV WxJr0_GcWl84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ GcCE3s1lnxkEFVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64 xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j 6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x02628vn2 kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_ Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17 CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0 I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I 8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73 UjIFyTuYvjxUc-txDUUUU X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan badblocks will loss if we set it as below: # echo 1 1 > bad_blocks # echo 3 1 > bad_blocks # echo 1 5 > bad_blocks # cat bad_blocks 1 3 In badblocks_set(), if there is an intersection between p[lo] and p[hi], we will combine them. The end of new badblocks is p[hi]'s end now. but p[lo] may cross p[hi] and new end should be the larger of p[lo] and p[hi]. Fixes: 9e0e252a048b ("badblocks: Add core badblock management code") Signed-off-by: Li Nan --- block/badblocks.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index 7e6ebe2ac12c..2c2ef8284a3f 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -267,16 +267,14 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, if (sectors == 0 && hi < bb->count) { /* we might be able to combine lo and hi */ /* Note: 's' is at the end of 'lo' */ - sector_t a = BB_OFFSET(p[hi]); - int lolen = BB_LEN(p[lo]); - int hilen = BB_LEN(p[hi]); - int newlen = lolen + hilen - (s - a); + sector_t a = BB_OFFSET(p[lo]); + int newlen = max(s, BB_OFFSET(p[hi]) + BB_LEN(p[hi])) - a; - if (s >= a && newlen < BB_MAX_LEN) { + if (s >= BB_OFFSET(p[hi]) && newlen < BB_MAX_LEN) { /* yes, we can combine them */ int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); - p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); + p[lo] = BB_MAKE(a, newlen, ack); memmove(p + hi, p + hi + 1, (bb->count - hi - 1) * 8); bb->count--; From patchwork Fri Apr 28 08:50:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226088 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA727C7EE24 for ; Fri, 28 Apr 2023 08:51:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345366AbjD1Ivz (ORCPT ); Fri, 28 Apr 2023 04:51:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345604AbjD1Iva (ORCPT ); Fri, 28 Apr 2023 04:51:30 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3760A49CE; Fri, 28 Apr 2023 01:51:25 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Q75tb6dcvz4f3pCb; Fri, 28 Apr 2023 16:51:19 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S7; Fri, 28 Apr 2023 16:51:21 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 03/10] block/badblocks: fix badblocks overlap Date: Fri, 28 Apr 2023 16:50:13 +0800 Message-Id: <20230428085020.2283981-4-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S7 X-Coremail-Antispam: 1UD129KBjvJXoW7ZF17Xw4fuFy8tFWrJrW7XFb_yoW8KF4rpw 1Sy3Wagry8WFyI9F45X3WDWr1fKas7Jr4UGa13A34UWFykCrnakF1ktw1j9rW8KFZ7XFs0 v3yrWFyj9F98GwUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPEb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUWw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV WxJr0_GcWl84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ GcCE3s1lnxkEFVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64 xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j 6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x02628vn2 kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_ Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17 CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0 I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I 8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73 UjIFyTuYvjxUwfHjUUUUU X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan badblocks will overlap if we set it as below: # echo 0 1 > bad_blocks # echo 3 1 > bad_blocks # echo 5 1 > bad_blocks # echo 0 512 > bad_blocks # cat bad_blocks 0 512 5 1 The reason for this is we only combine two badblocks once even if it should be combined many times. Fix it by classifying combine: 1)lo contain hi just remove hi, and check next hi. 2)lo intersects with hi newlen > BB_MAX_LEN, set lo to MAX, and the remaining is hi. newlen <= BB_MAX_LEN, remain unchanged, 2-in-1. Fixes: 9e0e252a048b ("badblocks: Add core badblock management code") Signed-off-by: Li Nan --- block/badblocks.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index 2c2ef8284a3f..f34351b59414 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -245,6 +245,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, /* merging is possible */ if (e <= s + sectors) { /* full overlap */ + e = s + sectors; ack = acknowledged; } else @@ -267,17 +268,35 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, if (sectors == 0 && hi < bb->count) { /* we might be able to combine lo and hi */ /* Note: 's' is at the end of 'lo' */ - sector_t a = BB_OFFSET(p[lo]); - int newlen = max(s, BB_OFFSET(p[hi]) + BB_LEN(p[hi])) - a; - - if (s >= BB_OFFSET(p[hi]) && newlen < BB_MAX_LEN) { - /* yes, we can combine them */ - int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); - - p[lo] = BB_MAKE(a, newlen, ack); - memmove(p + hi, p + hi + 1, - (bb->count - hi - 1) * 8); - bb->count--; + sector_t loa = BB_OFFSET(p[lo]), hia = BB_OFFSET(p[hi]); + sector_t hie = hia + BB_LEN(p[hi]); + int newlen = max(s, hie) - loa; + int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); + + if (s >= hia) { + while (s >= hie) { + /* lo contains hi, just remove hi */ + memmove(p + hi, p + hi + 1, + (bb->count - hi - 1) * 8); + bb->count--; + if (hi >= bb->count) + break; + hia = BB_OFFSET(p[hi]); + hie = hia + BB_LEN(p[hi]); + } + if (s >= hia && hi < bb->count) { + if (newlen > BB_MAX_LEN) { + p[lo] = BB_MAKE(loa, BB_MAX_LEN, ack); + p[hi] = BB_MAKE(loa + BB_MAX_LEN, + newlen - BB_MAX_LEN, + BB_ACK(p[hi])); + } else { + p[lo] = BB_MAKE(loa, newlen, ack); + memmove(p + hi, p + hi + 1, + (bb->count - hi - 1) * 8); + bb->count--; + } + } changed = true; } } From patchwork Fri Apr 28 08:50:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226087 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD699C7EE25 for ; Fri, 28 Apr 2023 08:51:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345393AbjD1Iv4 (ORCPT ); Fri, 28 Apr 2023 04:51:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345611AbjD1Ivb (ORCPT ); Fri, 28 Apr 2023 04:51:31 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A4144699; Fri, 28 Apr 2023 01:51:25 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Q75tc3ZDJz4f3vfS; Fri, 28 Apr 2023 16:51:20 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S8; Fri, 28 Apr 2023 16:51:21 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 04/10] block/badblocks: fix the bug of reverse order Date: Fri, 28 Apr 2023 16:50:14 +0800 Message-Id: <20230428085020.2283981-5-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S8 X-Coremail-Antispam: 1UD129KBjvdXoWrKFyruF1kuw1xKr4DWr1UGFg_yoWfAFX_Ja 40yay8Xrn5Jr1ayw1SyF1vyF4FvFW5Cr18Kry7Jr1kZa1Uta18Aws8Kr98Xrn8CFyDG39I yryfXr9Ivr4IqjkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUbkkYFVCjjxCrM7AC8VAFwI0_Wr0E3s1l1xkIjI8I6I8E6xAIw20E Y4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l82xGYIkIc2x26280x7IE14v26r126s 0DM28IrcIa0xkI8VCY1x0267AKxVW5JVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4AK67xG Y2AK021l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14 v26F4UJVW0owA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AK xVW0oVCq3wAac4AC62xK8xCEY4vEwIxC4wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F4 0EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_ Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JM4IIrI8v6xkF7I0E8c xan2IY04v7M4kE6xkIj40Ew7xC0wCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0x vEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVj vjDU0xZFpf9x07UMa0PUUUUU= X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan Order of badblocks will be reversed if we set a large area at once. 'hi' remains unchanged while adding continuous badblocks is wrong, the next setting is greater than 'hi', it should be added to the next position. Let 'hi' +1 each cycle. # echo 0 2048 > bad_blocks # cat bad_blocks 1536 512 1024 512 512 512 0 512 Fixes: 9e0e252a048b ("badblocks: Add core badblock management code") Signed-off-by: Li Nan --- block/badblocks.c | 1 + 1 file changed, 1 insertion(+) diff --git a/block/badblocks.c b/block/badblocks.c index f34351b59414..11e3a3ae2c72 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -320,6 +320,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, p[hi] = BB_MAKE(s, this_sectors, acknowledged); sectors -= this_sectors; s += this_sectors; + hi++; changed = true; } } From patchwork Fri Apr 28 08:50:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226090 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DEC52C77B61 for ; Fri, 28 Apr 2023 08:52:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345439AbjD1Iv6 (ORCPT ); Fri, 28 Apr 2023 04:51:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345613AbjD1Ivb (ORCPT ); Fri, 28 Apr 2023 04:51:31 -0400 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2268D3A9D; Fri, 28 Apr 2023 01:51:26 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4Q75tc2nj3z4f3mJx; Fri, 28 Apr 2023 16:51:20 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S9; Fri, 28 Apr 2023 16:51:22 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 05/10] block/badblocks: fix ack set fail in badblocks_set Date: Fri, 28 Apr 2023 16:50:15 +0800 Message-Id: <20230428085020.2283981-6-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S9 X-Coremail-Antispam: 1UD129KBjvdXoWrZF4xtrWrWrW8tF17JFWUArb_yoWkXrg_J3 WFyFZ5Xrn5CFs8Cr1Yy3W0qrsY9F45Cr4kCw12qrn7XrsrtF1DJws8tr93Wrn5CFZrC390 v34rXr9Iva4IqjkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUbkAYFVCjjxCrM7AC8VAFwI0_Wr0E3s1l1xkIjI8I6I8E6xAIw20E Y4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l82xGYIkIc2x26280x7IE14v26r126s 0DM28IrcIa0xkI8VCY1x0267AKxVW5JVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4AK67xG Y2AK021l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14 v26F4UJVW0owA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AK xVW0oVCq3wAac4AC62xK8xCEY4vEwIxC4wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F4 0EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_ Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JM4IIrI8v6xkF7I0E8c xan2IY04v7M4kE6xkIj40Ew7xC0wCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIev Ja73UjIFyTuYvjxUwZ2-UUUUU X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan If we try to set ack for a BB_MAX_LEN badblocks, it will return 0(success) but did not set ack at all in badblocks_set(). Check ack before setting to fix it, and do not set badblocks already exist. Fixes: 9e0e252a048b ("badblocks: Add core badblock management code") Signed-off-by: Li Nan --- block/badblocks.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index 11e3a3ae2c72..c11eb869f2f3 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -219,18 +219,18 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, if (e < s + sectors) e = s + sectors; if (e - a <= BB_MAX_LEN) { - p[lo] = BB_MAKE(a, e-a, ack); s = e; } else { /* does not all fit in one range, * make p[lo] maximal */ - if (BB_LEN(p[lo]) != BB_MAX_LEN) - p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); s = a + BB_MAX_LEN; } + if (s - a != BB_LEN(p[lo]) || ack != BB_ACK(p[lo])) { + p[lo] = BB_MAKE(a, s - a, ack); + changed = true; + } sectors = e - s; - changed = true; } } if (sectors && hi < bb->count) { From patchwork Fri Apr 28 08:50:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226091 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17BE0C7EE22 for ; Fri, 28 Apr 2023 08:52:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345566AbjD1Iv7 (ORCPT ); Fri, 28 Apr 2023 04:51:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345398AbjD1Ivc (ORCPT ); Fri, 28 Apr 2023 04:51:32 -0400 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 986D1468F; Fri, 28 Apr 2023 01:51:26 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4Q75tc5vsQz4f3mJk; Fri, 28 Apr 2023 16:51:20 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S10; Fri, 28 Apr 2023 16:51:22 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 06/10] block/badblocks: check bb->count instead of 'hi > lo' Date: Fri, 28 Apr 2023 16:50:16 +0800 Message-Id: <20230428085020.2283981-7-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S10 X-Coremail-Antispam: 1UD129KBjvJXoWxJFW7XFykXw1DKw17KFyxGrg_yoWrZryxpw 4SyaySgFy8WF4S9rn8JF4DGF15KayxZr48JF15Jw1xCF1vkr9I9FnFyw10gFWvkFZ3XFn0 qw1rWFyIyFZxAwUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmFb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVWxJr0_GcWl84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAF wI0_GcCE3s1lnxkEFVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4 xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v2 6r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x0262 8vn2kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_ Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAI cVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU1lksDUUUUU== X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan 'hi > lo' only holds when bb->count is 0. Check it is complicated after dichotomy. Check bb->count instead. And this will make future fix more convenient. No functional change intended. Signed-off-by: Li Nan --- block/badblocks.c | 155 +++++++++++++++++++++++----------------------- 1 file changed, 79 insertions(+), 76 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index c11eb869f2f3..3cb8513cbd7f 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -187,28 +187,32 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, p = bb->page; lo = 0; hi = bb->count; - /* Find the last range that starts at-or-before 's' */ - while (hi - lo > 1) { - int mid = (lo + hi) / 2; - sector_t a = BB_OFFSET(p[mid]); - - if (a <= s) - lo = mid; - else - hi = mid; - } - if (hi > lo && BB_OFFSET(p[lo]) > s) - hi = lo; + if (bb->count) { + sector_t a; + sector_t e; + int ack; + + /* Find the last range that starts at-or-before 's' */ + while (hi - lo > 1) { + int mid = (lo + hi) / 2; + + a = BB_OFFSET(p[mid]); + if (a <= s) + lo = mid; + else + hi = mid; + } - if (hi > lo) { /* we found a range that might merge with the start * of our new range */ - sector_t a = BB_OFFSET(p[lo]); - sector_t e = a + BB_LEN(p[lo]); - int ack = BB_ACK(p[lo]); + a = BB_OFFSET(p[lo]); + e = a + BB_LEN(p[lo]); + ack = BB_ACK(p[lo]); - if (e >= s) { + if (a > s) { + hi = lo; + } else if (e >= s) { /* Yes, we can merge with a previous range */ if (s == a && s + sectors >= e) /* new range covers old */ @@ -232,72 +236,71 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, } sectors = e - s; } - } - if (sectors && hi < bb->count) { - /* 'hi' points to the first range that starts after 's'. - * Maybe we can merge with the start of that range - */ - sector_t a = BB_OFFSET(p[hi]); - sector_t e = a + BB_LEN(p[hi]); - int ack = BB_ACK(p[hi]); - - if (a <= s + sectors) { - /* merging is possible */ - if (e <= s + sectors) { - /* full overlap */ - - e = s + sectors; - ack = acknowledged; - } else - ack = ack && acknowledged; - - a = s; - if (e - a <= BB_MAX_LEN) { - p[hi] = BB_MAKE(a, e-a, ack); - s = e; - } else { - p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); - s = a + BB_MAX_LEN; + if (sectors && hi < bb->count) { + /* 'hi' points to the first range that starts after 's'. + * Maybe we can merge with the start of that range + */ + a = BB_OFFSET(p[hi]); + e = a + BB_LEN(p[hi]); + ack = BB_ACK(p[hi]); + + if (a <= s + sectors) { + /* merging is possible */ + if (e <= s + sectors) { + /* full overlap */ + e = s + sectors; + ack = acknowledged; + } else + ack = ack && acknowledged; + + a = s; + if (e - a <= BB_MAX_LEN) { + p[hi] = BB_MAKE(a, e-a, ack); + s = e; + } else { + p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); + s = a + BB_MAX_LEN; + } + sectors = e - s; + lo = hi; + hi++; + changed = true; } - sectors = e - s; - lo = hi; - hi++; - changed = true; } - } - if (sectors == 0 && hi < bb->count) { - /* we might be able to combine lo and hi */ - /* Note: 's' is at the end of 'lo' */ - sector_t loa = BB_OFFSET(p[lo]), hia = BB_OFFSET(p[hi]); - sector_t hie = hia + BB_LEN(p[hi]); - int newlen = max(s, hie) - loa; - int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); - - if (s >= hia) { - while (s >= hie) { - /* lo contains hi, just remove hi */ - memmove(p + hi, p + hi + 1, - (bb->count - hi - 1) * 8); - bb->count--; - if (hi >= bb->count) - break; - hia = BB_OFFSET(p[hi]); - hie = hia + BB_LEN(p[hi]); - } - if (s >= hia && hi < bb->count) { - if (newlen > BB_MAX_LEN) { - p[lo] = BB_MAKE(loa, BB_MAX_LEN, ack); - p[hi] = BB_MAKE(loa + BB_MAX_LEN, - newlen - BB_MAX_LEN, - BB_ACK(p[hi])); - } else { - p[lo] = BB_MAKE(loa, newlen, ack); + if (sectors == 0 && hi < bb->count) { + /* we might be able to combine lo and hi */ + /* Note: 's' is at the end of 'lo' */ + sector_t loa = BB_OFFSET(p[lo]), hia = BB_OFFSET(p[hi]); + sector_t hie = hia + BB_LEN(p[hi]); + int newlen = max(s, hie) - loa; + + ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); + if (s >= hia) { + while (s >= hie) { + /* lo contains hi, just remove hi */ memmove(p + hi, p + hi + 1, (bb->count - hi - 1) * 8); bb->count--; + if (hi >= bb->count) + break; + hia = BB_OFFSET(p[hi]); + hie = hia + BB_LEN(p[hi]); + } + if (s >= hia && hi < bb->count) { + if (newlen > BB_MAX_LEN) { + p[lo] = BB_MAKE(loa, BB_MAX_LEN, ack); + p[hi] = BB_MAKE(loa + BB_MAX_LEN, + newlen - BB_MAX_LEN, + BB_ACK(p[hi])); + } else { + p[lo] = BB_MAKE(loa, newlen, ack); + memmove(p + hi, p + hi + 1, + (bb->count - hi - 1) * 8); + bb->count--; + } } + changed = true; } - changed = true; } } while (sectors) { From patchwork Fri Apr 28 08:50:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226085 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4EA33C7EE22 for ; Fri, 28 Apr 2023 08:51:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345531AbjD1Ivy (ORCPT ); Fri, 28 Apr 2023 04:51:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345608AbjD1Ivb (ORCPT ); Fri, 28 Apr 2023 04:51:31 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D432E4C18; Fri, 28 Apr 2023 01:51:25 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Q75td4kcyz4f3jYw; Fri, 28 Apr 2023 16:51:21 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S11; Fri, 28 Apr 2023 16:51:23 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 07/10] block/badblocks: factor out a helper to merge badblocks Date: Fri, 28 Apr 2023 16:50:17 +0800 Message-Id: <20230428085020.2283981-8-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S11 X-Coremail-Antispam: 1UD129KBjvJXoWxCF45WFyUZFW8uw1UuFykXwb_yoWrXFyxpr 1ay3WS9FyUXF47W3Z8JF4DKr1rKayfXr4UAFWfA3ykuF18CrZIqF1v9ryruFy09rZxWFn0 q3WYgry29as8C3JanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmFb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVWxJr0_GcWl84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAF wI0_GcCE3s1lnxkEFVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4 xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v2 6r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x0262 8vn2kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_ Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAI cVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU1lksDUUUUU== X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan Add a helper badblocks_merge() to merge badblocks, it makes code more readable. No functional change. Signed-off-by: Li Nan --- block/badblocks.c | 143 +++++++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 64 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index 3cb8513cbd7f..f498fae201a1 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -144,6 +144,80 @@ static void badblocks_update_acked(struct badblocks *bb) bb->unacked_exist = false; } +/* + * try to merge new range with lo and hi. + * if s is within lo, merge with lo. + * if s + sectors > start of hi, merge with hi. + * + * Return: + * Number of merged sectors + */ +static int badblocks_merge(struct badblocks *bb, sector_t s, int sectors, + int acknowledged, int *lo, int *hi, bool *changed) +{ + u64 *p = bb->page; + sector_t a = BB_OFFSET(p[*lo]); + sector_t e = a + BB_LEN(p[*lo]); + int merged_sectors = 0, ack = acknowledged; + + if (a > s) { + *hi = *lo; + } else if (e >= s) { + /* we can merge with a previous range */ + if (s > a || s + sectors < e) + ack = ack && BB_ACK(p[*lo]); + + if (e < s + sectors) + e = s + sectors; + if (e - a <= BB_MAX_LEN) { + s = e; + } else { + /* + * does not all fit in one range, + * make p[lo] maximal + */ + s = a + BB_MAX_LEN; + } + if (s - a != BB_LEN(p[*lo]) || ack != BB_ACK(p[*lo])) { + p[*lo] = BB_MAKE(a, s - a, ack); + *changed = true; + } + merged_sectors += sectors - e + s; + sectors = e - s; + } + if (sectors && *hi < bb->count) { + /* + * 'hi' points to the first range that starts after 's'. + * Maybe we can merge with the start of that range + */ + a = BB_OFFSET(p[*hi]); + e = a + BB_LEN(p[*hi]); + ack = acknowledged; + + if (a <= s + sectors) { + /* merging is possible */ + if (e <= s + sectors) { + /* full overlap */ + e = s + sectors; + } else + ack = ack && BB_ACK(p[*hi]); + + a = s; + if (e - a <= BB_MAX_LEN) + s = e; + else + s = a + BB_MAX_LEN; + p[*hi] = BB_MAKE(a, s-a, ack); + + merged_sectors += sectors - e + s; + *changed = true; + *lo = *hi; + *hi += 1; + } + } + return merged_sectors; +} + /** * badblocks_set() - Add a range of bad blocks to the table. * @bb: the badblocks structure that holds all badblock information @@ -191,6 +265,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, sector_t a; sector_t e; int ack; + int merged_sectors; /* Find the last range that starts at-or-before 's' */ while (hi - lo > 1) { @@ -203,70 +278,10 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, hi = mid; } - /* we found a range that might merge with the start - * of our new range - */ - a = BB_OFFSET(p[lo]); - e = a + BB_LEN(p[lo]); - ack = BB_ACK(p[lo]); - - if (a > s) { - hi = lo; - } else if (e >= s) { - /* Yes, we can merge with a previous range */ - if (s == a && s + sectors >= e) - /* new range covers old */ - ack = acknowledged; - else - ack = ack && acknowledged; - - if (e < s + sectors) - e = s + sectors; - if (e - a <= BB_MAX_LEN) { - s = e; - } else { - /* does not all fit in one range, - * make p[lo] maximal - */ - s = a + BB_MAX_LEN; - } - if (s - a != BB_LEN(p[lo]) || ack != BB_ACK(p[lo])) { - p[lo] = BB_MAKE(a, s - a, ack); - changed = true; - } - sectors = e - s; - } - if (sectors && hi < bb->count) { - /* 'hi' points to the first range that starts after 's'. - * Maybe we can merge with the start of that range - */ - a = BB_OFFSET(p[hi]); - e = a + BB_LEN(p[hi]); - ack = BB_ACK(p[hi]); - - if (a <= s + sectors) { - /* merging is possible */ - if (e <= s + sectors) { - /* full overlap */ - e = s + sectors; - ack = acknowledged; - } else - ack = ack && acknowledged; - - a = s; - if (e - a <= BB_MAX_LEN) { - p[hi] = BB_MAKE(a, e-a, ack); - s = e; - } else { - p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); - s = a + BB_MAX_LEN; - } - sectors = e - s; - lo = hi; - hi++; - changed = true; - } - } + merged_sectors = badblocks_merge(bb, s, sectors, acknowledged, + &lo, &hi, &changed); + s += merged_sectors; + sectors -= merged_sectors; if (sectors == 0 && hi < bb->count) { /* we might be able to combine lo and hi */ /* Note: 's' is at the end of 'lo' */ From patchwork Fri Apr 28 08:50:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226092 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B651BC77B60 for ; Fri, 28 Apr 2023 08:52:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345358AbjD1IwA (ORCPT ); Fri, 28 Apr 2023 04:52:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345621AbjD1Ivd (ORCPT ); Fri, 28 Apr 2023 04:51:33 -0400 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D3A64C39; Fri, 28 Apr 2023 01:51:27 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4Q75td4zMbz4f3mK0; Fri, 28 Apr 2023 16:51:21 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S12; Fri, 28 Apr 2023 16:51:23 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 08/10] block/badblocks: factor out a helper to combine badblocks Date: Fri, 28 Apr 2023 16:50:18 +0800 Message-Id: <20230428085020.2283981-9-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S12 X-Coremail-Antispam: 1UD129KBjvJXoWxAFWrAFyxGF15Kw4xuw4rKrg_yoW5ZFyUpw 1Sy3Waqr1xWFWI9a1UJa1DKr13Ka97Zr4rJF4fAw1xCF18CwnI9rnrJw1Ygay8KFWfXF90 q34rWFyjyF97GwUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmFb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVWxJr0_GcWl84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAF wI0_GcCE3s1lnxkEFVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4 xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v2 6r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x0262 8vn2kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_ Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAI cVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU1lksDUUUUU== X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan Add a helper badblocks_combine() to combine badblocks, it makes code more readable. No functional change. Signed-off-by: Li Nan --- block/badblocks.c | 87 ++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index f498fae201a1..c87c68d4bcac 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -218,6 +218,51 @@ static int badblocks_merge(struct badblocks *bb, sector_t s, int sectors, return merged_sectors; } +/* + * try to combine lo and hi(lo + 1) if lo intersects with hi + */ +static void badblocks_combine(struct badblocks *bb, int lo) +{ + u64 *p = bb->page; + sector_t loe = BB_OFFSET(p[lo]) + BB_LEN(p[lo]); + int hi = lo + 1; + + if (hi >= bb->count) + return; + /* we might be able to combine lo and hi */ + + if (loe >= BB_OFFSET(p[hi])) { + sector_t loa = BB_OFFSET(p[lo]), hia = BB_OFFSET(p[hi]); + sector_t hie = hia + BB_LEN(p[hi]); + int newlen = max(loe, hie) - loa; + int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); + + while (loe >= hie) { + /* lo contains hi, just remove hi */ + memmove(p + hi, p + hi + 1, + (bb->count - hi - 1) * 8); + bb->count--; + if (hi >= bb->count) + break; + hia = BB_OFFSET(p[hi]); + hie = hia + BB_LEN(p[hi]); + } + if (loe >= hia && hi < bb->count) { + if (newlen > BB_MAX_LEN) { + p[lo] = BB_MAKE(loa, BB_MAX_LEN, ack); + p[hi] = BB_MAKE(loa + BB_MAX_LEN, + newlen - BB_MAX_LEN, + BB_ACK(p[hi])); + } else { + p[lo] = BB_MAKE(loa, newlen, ack); + memmove(p + hi, p + hi + 1, + (bb->count - hi - 1) * 8); + bb->count--; + } + } + } +} + /** * badblocks_set() - Add a range of bad blocks to the table. * @bb: the badblocks structure that holds all badblock information @@ -262,16 +307,13 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, lo = 0; hi = bb->count; if (bb->count) { - sector_t a; - sector_t e; - int ack; int merged_sectors; /* Find the last range that starts at-or-before 's' */ while (hi - lo > 1) { int mid = (lo + hi) / 2; + int a = BB_OFFSET(p[mid]); - a = BB_OFFSET(p[mid]); if (a <= s) lo = mid; else @@ -282,41 +324,8 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, &lo, &hi, &changed); s += merged_sectors; sectors -= merged_sectors; - if (sectors == 0 && hi < bb->count) { - /* we might be able to combine lo and hi */ - /* Note: 's' is at the end of 'lo' */ - sector_t loa = BB_OFFSET(p[lo]), hia = BB_OFFSET(p[hi]); - sector_t hie = hia + BB_LEN(p[hi]); - int newlen = max(s, hie) - loa; - - ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); - if (s >= hia) { - while (s >= hie) { - /* lo contains hi, just remove hi */ - memmove(p + hi, p + hi + 1, - (bb->count - hi - 1) * 8); - bb->count--; - if (hi >= bb->count) - break; - hia = BB_OFFSET(p[hi]); - hie = hia + BB_LEN(p[hi]); - } - if (s >= hia && hi < bb->count) { - if (newlen > BB_MAX_LEN) { - p[lo] = BB_MAKE(loa, BB_MAX_LEN, ack); - p[hi] = BB_MAKE(loa + BB_MAX_LEN, - newlen - BB_MAX_LEN, - BB_ACK(p[hi])); - } else { - p[lo] = BB_MAKE(loa, newlen, ack); - memmove(p + hi, p + hi + 1, - (bb->count - hi - 1) * 8); - bb->count--; - } - } - changed = true; - } - } + if (sectors == 0) + badblocks_combine(bb, lo); } while (sectors) { /* didn't merge (it all). From patchwork Fri Apr 28 08:50:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226094 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2EB0AC77B7E for ; Fri, 28 Apr 2023 08:52:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345563AbjD1IwC (ORCPT ); Fri, 28 Apr 2023 04:52:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345627AbjD1Ive (ORCPT ); Fri, 28 Apr 2023 04:51:34 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B581D1FCF; Fri, 28 Apr 2023 01:51:26 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Q75tf4v39z4f3vfD; Fri, 28 Apr 2023 16:51:22 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S13; Fri, 28 Apr 2023 16:51:23 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 09/10] block/badblocks: factor out a helper to create badblocks Date: Fri, 28 Apr 2023 16:50:19 +0800 Message-Id: <20230428085020.2283981-10-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S13 X-Coremail-Antispam: 1UD129KBjvJXoW7tw4UCrW7tFW8WrWxZF48tFb_yoW8tr43pr sIy3Zagry7Ww1xXanxX3ZrKr1rK3yfZF1UGr47Aw1UGFyxCwnrtFn2vryfuFyj9Fy3Jr4q q3WYgryY9asrC37anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmFb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVWxJr0_GcWl84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAF wI0_GcCE3s1lnxkEFVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4 xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v2 6r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x0262 8vn2kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_ Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAI cVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU1lksDUUUUU== X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan Add a helper badblocks_create() to create badblocks, it makes code more readable. No functional change. Signed-off-by: Li Nan --- block/badblocks.c | 65 ++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index c87c68d4bcac..bb0324b66f57 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -263,6 +263,46 @@ static void badblocks_combine(struct badblocks *bb, int lo) } } +/* + * creat new badblocks if it can't merge with existing region + * + * Return: + * 0: success + * 1: failed to set badblocks (out of space) + */ +static int badblocks_create(struct badblocks *bb, sector_t s, sector_t sectors, + int hi, int acknowledged, bool *changed) +{ + u64 *p = bb->page; + int rv = 0; + + while (sectors) { + int this_sectors = sectors; + + /* didn't merge (it all). + * Need to add a range just before 'hi' + */ + if (bb->count >= MAX_BADBLOCKS) { + /* No room for more */ + rv = 1; + break; + } + + memmove(p + hi + 1, p + hi, + (bb->count - hi) * 8); + bb->count++; + + if (this_sectors > BB_MAX_LEN) + this_sectors = BB_MAX_LEN; + p[hi] = BB_MAKE(s, this_sectors, acknowledged); + sectors -= this_sectors; + s += this_sectors; + hi++; + *changed = true; + } + return rv; +} + /** * badblocks_set() - Add a range of bad blocks to the table. * @bb: the badblocks structure that holds all badblock information @@ -327,30 +367,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, if (sectors == 0) badblocks_combine(bb, lo); } - while (sectors) { - /* didn't merge (it all). - * Need to add a range just before 'hi' - */ - if (bb->count >= MAX_BADBLOCKS) { - /* No room for more */ - rv = 1; - break; - } else { - int this_sectors = sectors; - - memmove(p + hi + 1, p + hi, - (bb->count - hi) * 8); - bb->count++; - - if (this_sectors > BB_MAX_LEN) - this_sectors = BB_MAX_LEN; - p[hi] = BB_MAKE(s, this_sectors, acknowledged); - sectors -= this_sectors; - s += this_sectors; - hi++; - changed = true; - } - } + rv = badblocks_create(bb, s, sectors, hi, acknowledged, &changed); if (changed) { bb->changed = changed; From patchwork Fri Apr 28 08:50:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Nan X-Patchwork-Id: 13226093 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD44AC77B61 for ; Fri, 28 Apr 2023 08:52:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345457AbjD1IwB (ORCPT ); Fri, 28 Apr 2023 04:52:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345632AbjD1Ive (ORCPT ); Fri, 28 Apr 2023 04:51:34 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 114944487; Fri, 28 Apr 2023 01:51:28 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Q75tf6xTjz4f3lXn; Fri, 28 Apr 2023 16:51:22 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3rLAGiUtkh9RFIQ--.49176S14; Fri, 28 Apr 2023 16:51:24 +0800 (CST) From: linan666@huaweicloud.com To: axboe@kernel.dk, vishal.l.verma@intel.com, dan.j.williams@intel.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: [PATCH 10/10] block/badblocks: try to merge badblocks as much as possible Date: Fri, 28 Apr 2023 16:50:20 +0800 Message-Id: <20230428085020.2283981-11-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230428085020.2283981-1-linan666@huaweicloud.com> References: <20230428085020.2283981-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3rLAGiUtkh9RFIQ--.49176S14 X-Coremail-Antispam: 1UD129KBjvJXoW7Ww48tFW5WF4UKF43Cr1xZrb_yoW8Cry7pF n0y3WfKry2gr17W3W5X3W8Kr10g34fJF4UCF43Xw1jkFyxGwn3tF1kZw4FqFyjgF43Wrs0 v3Wruryjva4kCa7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmIb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVWxJr0_GcWl84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAF wI0_GcCE3s1lnxkEFVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4 xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v2 6r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x0262 8vn2kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_ Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Gr0_Xr1lIxAI cVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMI IF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr1j6F4UJbIYCTnI WIevJa73UjIFyTuYvjxUwZ2-UUUUU X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Li Nan If we set a new badblocks, we first merge it with existing region, then try to combine lo and hi. If there are still badblocks need to be set, create it. It is a bad way when setting a laget number of badblocks. for example, it will become chaotic if we set as below: # echo 1 1 > bad_blocks # echo 512 1 > bad_blocks # echo 0 513 > bad_blocks # cat bad_blocks 0 512 512 1 512 1 Fix it by trying to merge as much as possible. If we have merged any badblocks, retry to merge next sectors. Do not check sectors while combining, we should combine lo and hi each sycle. Fixes: 9e0e252a048b ("badblocks: Add core badblock management code") Signed-off-by: Li Nan --- block/badblocks.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index bb0324b66f57..7e6fce10c82d 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -347,8 +347,6 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, lo = 0; hi = bb->count; if (bb->count) { - int merged_sectors; - /* Find the last range that starts at-or-before 's' */ while (hi - lo > 1) { int mid = (lo + hi) / 2; @@ -360,12 +358,19 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors, hi = mid; } - merged_sectors = badblocks_merge(bb, s, sectors, acknowledged, - &lo, &hi, &changed); - s += merged_sectors; - sectors -= merged_sectors; - if (sectors == 0) + while (sectors) { + int merged_sectors; + + merged_sectors = badblocks_merge(bb, s, sectors, acknowledged, + &lo, &hi, &changed); + /* can't merge, break to create */ + if (!merged_sectors) + break; + + s += merged_sectors; + sectors -= merged_sectors; badblocks_combine(bb, lo); + } } rv = badblocks_create(bb, s, sectors, hi, acknowledged, &changed);