From nobody Thu Feb 12 04:51:48 2026 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 15C88C77B61 for ; Fri, 28 Apr 2023 08:52:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345625AbjD1IwZ (ORCPT ); Fri, 28 Apr 2023 04:52:25 -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 Content-Transfer-Encoding: quoted-printable 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-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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, i= nt sectors, p =3D bb->page; lo =3D 0; hi =3D bb->count; - /* Find the last range that starts at-or-before 's' */ - while (hi - lo > 1) { - int mid =3D (lo + hi) / 2; - sector_t a =3D BB_OFFSET(p[mid]); - - if (a <=3D s) - lo =3D mid; - else - hi =3D mid; - } - if (hi > lo && BB_OFFSET(p[lo]) > s) - hi =3D 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 =3D (lo + hi) / 2; + + a =3D BB_OFFSET(p[mid]); + if (a <=3D s) + lo =3D mid; + else + hi =3D mid; + } =20 - if (hi > lo) { /* we found a range that might merge with the start * of our new range */ - sector_t a =3D BB_OFFSET(p[lo]); - sector_t e =3D a + BB_LEN(p[lo]); - int ack =3D BB_ACK(p[lo]); + a =3D BB_OFFSET(p[lo]); + e =3D a + BB_LEN(p[lo]); + ack =3D BB_ACK(p[lo]); =20 - if (e >=3D s) { + if (a > s) { + hi =3D lo; + } else if (e >=3D s) { /* Yes, we can merge with a previous range */ if (s =3D=3D a && s + sectors >=3D e) /* new range covers old */ @@ -232,72 +236,71 @@ int badblocks_set(struct badblocks *bb, sector_t s, i= nt sectors, } sectors =3D 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 =3D BB_OFFSET(p[hi]); - sector_t e =3D a + BB_LEN(p[hi]); - int ack =3D BB_ACK(p[hi]); - - if (a <=3D s + sectors) { - /* merging is possible */ - if (e <=3D s + sectors) { - /* full overlap */ - - e =3D s + sectors; - ack =3D acknowledged; - } else - ack =3D ack && acknowledged; - - a =3D s; - if (e - a <=3D BB_MAX_LEN) { - p[hi] =3D BB_MAKE(a, e-a, ack); - s =3D e; - } else { - p[hi] =3D BB_MAKE(a, BB_MAX_LEN, ack); - s =3D 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 =3D BB_OFFSET(p[hi]); + e =3D a + BB_LEN(p[hi]); + ack =3D BB_ACK(p[hi]); + + if (a <=3D s + sectors) { + /* merging is possible */ + if (e <=3D s + sectors) { + /* full overlap */ + e =3D s + sectors; + ack =3D acknowledged; + } else + ack =3D ack && acknowledged; + + a =3D s; + if (e - a <=3D BB_MAX_LEN) { + p[hi] =3D BB_MAKE(a, e-a, ack); + s =3D e; + } else { + p[hi] =3D BB_MAKE(a, BB_MAX_LEN, ack); + s =3D a + BB_MAX_LEN; + } + sectors =3D e - s; + lo =3D hi; + hi++; + changed =3D true; } - sectors =3D e - s; - lo =3D hi; - hi++; - changed =3D true; } - } - if (sectors =3D=3D 0 && hi < bb->count) { - /* we might be able to combine lo and hi */ - /* Note: 's' is at the end of 'lo' */ - sector_t loa =3D BB_OFFSET(p[lo]), hia =3D BB_OFFSET(p[hi]); - sector_t hie =3D hia + BB_LEN(p[hi]); - int newlen =3D max(s, hie) - loa; - int ack =3D BB_ACK(p[lo]) && BB_ACK(p[hi]); - - if (s >=3D hia) { - while (s >=3D hie) { - /* lo contains hi, just remove hi */ - memmove(p + hi, p + hi + 1, - (bb->count - hi - 1) * 8); - bb->count--; - if (hi >=3D bb->count) - break; - hia =3D BB_OFFSET(p[hi]); - hie =3D hia + BB_LEN(p[hi]); - } - if (s >=3D hia && hi < bb->count) { - if (newlen > BB_MAX_LEN) { - p[lo] =3D BB_MAKE(loa, BB_MAX_LEN, ack); - p[hi] =3D BB_MAKE(loa + BB_MAX_LEN, - newlen - BB_MAX_LEN, - BB_ACK(p[hi])); - } else { - p[lo] =3D BB_MAKE(loa, newlen, ack); + if (sectors =3D=3D 0 && hi < bb->count) { + /* we might be able to combine lo and hi */ + /* Note: 's' is at the end of 'lo' */ + sector_t loa =3D BB_OFFSET(p[lo]), hia =3D BB_OFFSET(p[hi]); + sector_t hie =3D hia + BB_LEN(p[hi]); + int newlen =3D max(s, hie) - loa; + + ack =3D BB_ACK(p[lo]) && BB_ACK(p[hi]); + if (s >=3D hia) { + while (s >=3D hie) { + /* lo contains hi, just remove hi */ memmove(p + hi, p + hi + 1, (bb->count - hi - 1) * 8); bb->count--; + if (hi >=3D bb->count) + break; + hia =3D BB_OFFSET(p[hi]); + hie =3D hia + BB_LEN(p[hi]); + } + if (s >=3D hia && hi < bb->count) { + if (newlen > BB_MAX_LEN) { + p[lo] =3D BB_MAKE(loa, BB_MAX_LEN, ack); + p[hi] =3D BB_MAKE(loa + BB_MAX_LEN, + newlen - BB_MAX_LEN, + BB_ACK(p[hi])); + } else { + p[lo] =3D BB_MAKE(loa, newlen, ack); + memmove(p + hi, p + hi + 1, + (bb->count - hi - 1) * 8); + bb->count--; + } } + changed =3D true; } - changed =3D true; } } while (sectors) { --=20 2.31.1