From nobody Tue Feb 10 19:49:30 2026 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A1EC73530; Wed, 28 Feb 2024 11:49:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709120982; cv=none; b=FmaSe6s/bO+2SSGc1lXOBxlz14VDkHukBY2W3OLzMN+BEgC+aTzKWGmDM7ohazywZBk/5N7+cI8F/f8YwQ5kND62O60js+nLDHQrGeGoJJSaSwHef7v00wD20Vr/gre+02wqMnANhZdqtOa/PqfsyTAc+rlZfzhMGZFxDtrurrs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709120982; c=relaxed/simple; bh=WcnAPi+LGdvS0TdAGH0xsFUzCm5KImEKZ7D6nZzkh0g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IyrWcLgCABi/0p7jh8/J07xEMYNcOe1FOsG+4n5uSINXj1Md9g4VM/Ykkm2Cw6fbufWjp0ev3V79dxoAobY7n1GKpkBlF9NF1YrYWZwXcs55lDRhn754aTCkAWFYpQvq/BcFGDvb+kY3rv0cK//M7MrzmrVYiL4KnO/YOU9OzeQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4TlCLx3JtRz4f3lgR; Wed, 28 Feb 2024 19:49:29 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id C59021A0232; Wed, 28 Feb 2024 19:49:36 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgAn9g7IHd9l+eamFQ--.6969S15; Wed, 28 Feb 2024 19:49:36 +0800 (CST) From: Yu Kuai To: xni@redhat.com, paul.e.luse@linux.intel.com, song@kernel.org, shli@fb.com, neilb@suse.com Cc: linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai3@huawei.com, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com Subject: [PATCH md-6.9 v3 11/11] md/raid1: factor out helpers to choose the best rdev from read_balance() Date: Wed, 28 Feb 2024 19:43:33 +0800 Message-Id: <20240228114333.527222-12-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240228114333.527222-1-yukuai1@huaweicloud.com> References: <20240228114333.527222-1-yukuai1@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: cCh0CgAn9g7IHd9l+eamFQ--.6969S15 X-Coremail-Antispam: 1UD129KBjvJXoW3JrWrWw15Wr1UAFyfAryxXwb_yoW3Zw1Upw 45GFn2yrWUZryruwn5tr4UWrWS934fJa18GrWkG34S93sagrZ0qFnrKryY9FyDGFs3Cw12 qw15Gr47C3Z7GFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPI14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E 14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIx kGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVW8JVW5JwCI42IY6xIIjxv20xvEc7CjxVAF wI0_Gr1j6F4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr 0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUQ SdkUUUUU= X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Yu Kuai The way that best rdev is chosen: 1) If the read is sequential from one rdev: - if rdev is rotational, use this rdev; - if rdev is non-rotational, use this rdev until total read length exceed disk opt io size; 2) If the read is not sequential: - if there is idle disk, use it, otherwise: - if the array has non-rotational disk, choose the rdev with minimal inflight IO; - if all the underlaying disks are rotational disk, choose the rdev with closest IO; There are no functional changes, just to make code cleaner and prepare for following refactor. Co-developed-by: Paul Luse Signed-off-by: Paul Luse Signed-off-by: Yu Kuai Reviewed-by: Xiao Ni --- drivers/md/raid1.c | 175 +++++++++++++++++++++++++-------------------- 1 file changed, 98 insertions(+), 77 deletions(-) diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c index 526f0d977040..c00f2aefbc56 100644 --- a/drivers/md/raid1.c +++ b/drivers/md/raid1.c @@ -730,74 +730,71 @@ static bool should_choose_next(struct r1conf *conf, i= nt disk) mirror->next_seq_sect - opt_iosize >=3D mirror->seq_start; } =20 -/* - * This routine returns the disk from which the requested read should - * be done. There is a per-array 'next expected sequential IO' sector - * number - if this matches on the next IO then we use the last disk. - * There is also a per-disk 'last know head position' sector that is - * maintained from IRQ contexts, both the normal and the resync IO - * completion handlers update this position correctly. If there is no - * perfect sequential match then we pick the disk whose head is closest. - * - * If there are 2 mirrors in the same 2 devices, performance degrades - * because position is mirror, not device based. - * - * The rdev for the device selected will have nr_pending incremented. - */ -static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *ma= x_sectors) +static bool rdev_readable(struct md_rdev *rdev, struct r1bio *r1_bio) { - const sector_t this_sector =3D r1_bio->sector; - int sectors; - int best_good_sectors; - int best_disk, best_dist_disk, best_pending_disk, sequential_disk; - int disk; - sector_t best_dist; - unsigned int min_pending; - struct md_rdev *rdev; + if (!rdev || test_bit(Faulty, &rdev->flags)) + return false; =20 - retry: - sectors =3D r1_bio->sectors; - best_disk =3D -1; - best_dist_disk =3D -1; - sequential_disk =3D -1; - best_dist =3D MaxSector; - best_pending_disk =3D -1; - min_pending =3D UINT_MAX; - best_good_sectors =3D 0; - clear_bit(R1BIO_FailFast, &r1_bio->state); + /* still in recovery */ + if (!test_bit(In_sync, &rdev->flags) && + rdev->recovery_offset < r1_bio->sector + r1_bio->sectors) + return false; =20 - if (raid1_should_read_first(conf->mddev, this_sector, sectors)) - return choose_first_rdev(conf, r1_bio, max_sectors); + /* don't read from slow disk unless have to */ + if (test_bit(WriteMostly, &rdev->flags)) + return false; + + /* don't split IO for bad blocks unless have to */ + if (rdev_has_badblock(rdev, r1_bio->sector, r1_bio->sectors)) + return false; + + return true; +} + +struct read_balance_ctl { + sector_t closest_dist; + int closest_dist_disk; + int min_pending; + int min_pending_disk; + int sequential_disk; + int readable_disks; +}; + +static int choose_best_rdev(struct r1conf *conf, struct r1bio *r1_bio) +{ + int disk; + struct read_balance_ctl ctl =3D { + .closest_dist_disk =3D -1, + .closest_dist =3D MaxSector, + .min_pending_disk =3D -1, + .min_pending =3D UINT_MAX, + .sequential_disk =3D -1, + }; =20 for (disk =3D 0 ; disk < conf->raid_disks * 2 ; disk++) { + struct md_rdev *rdev; sector_t dist; unsigned int pending; =20 - rdev =3D conf->mirrors[disk].rdev; - if (r1_bio->bios[disk] =3D=3D IO_BLOCKED - || rdev =3D=3D NULL - || test_bit(Faulty, &rdev->flags)) - continue; - if (!test_bit(In_sync, &rdev->flags) && - rdev->recovery_offset < this_sector + sectors) - continue; - if (test_bit(WriteMostly, &rdev->flags)) + if (r1_bio->bios[disk] =3D=3D IO_BLOCKED) continue; - if (rdev_has_badblock(rdev, this_sector, sectors)) + + rdev =3D conf->mirrors[disk].rdev; + if (!rdev_readable(rdev, r1_bio)) continue; =20 - if (best_disk >=3D 0) - /* At least two disks to choose from so failfast is OK */ + /* At least two disks to choose from so failfast is OK */ + if (ctl.readable_disks++ =3D=3D 1) set_bit(R1BIO_FailFast, &r1_bio->state); =20 pending =3D atomic_read(&rdev->nr_pending); - dist =3D abs(this_sector - conf->mirrors[disk].head_position); + dist =3D abs(r1_bio->sector - conf->mirrors[disk].head_position); + /* Don't change to another disk for sequential reads */ if (is_sequential(conf, disk, r1_bio)) { - if (!should_choose_next(conf, disk)) { - best_disk =3D disk; - break; - } + if (!should_choose_next(conf, disk)) + return disk; + /* * Add 'pending' to avoid choosing this disk if * there is other idle disk. @@ -807,17 +804,17 @@ static int read_balance(struct r1conf *conf, struct r= 1bio *r1_bio, int *max_sect * If there is no other idle disk, this disk * will be chosen. */ - sequential_disk =3D disk; + ctl.sequential_disk =3D disk; } =20 - if (min_pending > pending) { - min_pending =3D pending; - best_pending_disk =3D disk; + if (ctl.min_pending > pending) { + ctl.min_pending =3D pending; + ctl.min_pending_disk =3D disk; } =20 - if (dist < best_dist) { - best_dist =3D dist; - best_dist_disk =3D disk; + if (ctl.closest_dist > dist) { + ctl.closest_dist =3D dist; + ctl.closest_dist_disk =3D disk; } } =20 @@ -825,8 +822,8 @@ static int read_balance(struct r1conf *conf, struct r1b= io *r1_bio, int *max_sect * sequential IO size exceeds optimal iosize, however, there is no other * idle disk, so choose the sequential disk. */ - if (best_disk =3D=3D -1 && min_pending !=3D 0) - best_disk =3D sequential_disk; + if (ctl.sequential_disk !=3D -1 && ctl.min_pending !=3D 0) + return ctl.sequential_disk; =20 /* * If all disks are rotational, choose the closest disk. If any disk is @@ -834,25 +831,49 @@ static int read_balance(struct r1conf *conf, struct r= 1bio *r1_bio, int *max_sect * disk is rotational, which might/might not be optimal for raids with * mixed ratation/non-rotational disks depending on workload. */ - if (best_disk =3D=3D -1) { - if (READ_ONCE(conf->nonrot_disks) || min_pending =3D=3D 0) - best_disk =3D best_pending_disk; - else - best_disk =3D best_dist_disk; - } + if (ctl.min_pending_disk !=3D -1 && + (READ_ONCE(conf->nonrot_disks) || ctl.min_pending =3D=3D 0)) + return ctl.min_pending_disk; + else + return ctl.closest_dist_disk; +} =20 - if (best_disk >=3D 0) { - rdev =3D conf->mirrors[best_disk].rdev; - if (!rdev) - goto retry; +/* + * This routine returns the disk from which the requested read should be d= one. + * + * 1) If resync is in progress, find the first usable disk and use it even= if it + * has some bad blocks. + * + * 2) Now that there is no resync, loop through all disks and skipping slow + * disks and disks with bad blocks for now. Only pay attention to key disk + * choice. + * + * 3) If we've made it this far, now look for disks with bad blocks and ch= oose + * the one with most number of sectors. + * + * 4) If we are all the way at the end, we have no choice but to use a dis= k even + * if it is write mostly. + * + * The rdev for the device selected will have nr_pending incremented. + */ +static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, + int *max_sectors) +{ + int disk; =20 - sectors =3D best_good_sectors; - update_read_sectors(conf, disk, this_sector, sectors); - } - *max_sectors =3D sectors; + clear_bit(R1BIO_FailFast, &r1_bio->state); + + if (raid1_should_read_first(conf->mddev, r1_bio->sector, + r1_bio->sectors)) + return choose_first_rdev(conf, r1_bio, max_sectors); =20 - if (best_disk >=3D 0) - return best_disk; + disk =3D choose_best_rdev(conf, r1_bio); + if (disk >=3D 0) { + *max_sectors =3D r1_bio->sectors; + update_read_sectors(conf, disk, r1_bio->sector, + r1_bio->sectors); + return disk; + } =20 /* * If we are here it means we didn't find a perfectly good disk so --=20 2.39.2