Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp192949rdb; Thu, 22 Feb 2024 00:06:35 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVkflkFPQaPaeiqccNQRczP6PW3j8urqNjgBKGXqQggdDAXml6KMfv3T1zJtrCYrdpB2fs4MdWbWTO4xaWVEKFZ7rp9KQcKnHBH/IiqAg== X-Google-Smtp-Source: AGHT+IEp4L8k4xcvwsFT9rZYDWaa2uxJIgrNs25RJA9NNHJiuFVfpDppn5Wc2dFnaDcnJoZG5cbd X-Received: by 2002:a05:6214:4605:b0:68f:b7cd:303e with SMTP id oq5-20020a056214460500b0068fb7cd303emr2341785qvb.24.1708589195059; Thu, 22 Feb 2024 00:06:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708589195; cv=pass; d=google.com; s=arc-20160816; b=Yi6GRxtlj9uW5A4HkjkDEGUp68UGOPQyGgNEHxngsI86l6RUxDlbGObq7G/vuwYJis 2rI1M6GQoEc9a06QGGEeUuKc1uwMmPkDFPwOcXk191M1niwm0vqcm48fb6/TCk84Bu4h PUU+6v7tSc57U2rexBQpAdjFcI3ojIGVsx4lHIa93kd/tIbWnzNM6wuhEGRZhOaStdwK zw4GP1J2c2my0NAMSjW2jxtZQNzgnfR0DyULgBZg7fFP0KRxdtaCSGJG4LECldPrqhjz jAh5IUac9tplGUsKxEh17nyRDAWCQ7z8tiIScVnJ8sYO+uYQSi2MSIaWUOHOQMCummLp 5vtQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=bVI/VO/okphHthZ3TpZIU5hEeyXVZsqtBRGjoIN0/QY=; fh=5pFDn0/OhEinqiskZflJb/MnGts2EKkuL30fOGsymHI=; b=PuKxFitjsssmxfPKsHEFUkNHooxSX3chf/tYFlOusj1iiSvNJP1OFL7X9fGcQz/MQR H2n7cctxwlQgqL0Fjd0ar7WM4T63w1cclivbojbpt290MWoXMMa0wPZLdD5bjzyz4VJH FgMujFxqTFiw72dWgUErHjyS48iviLMLyG8p9d9MNXX4riDQIEjx+KiQwcUfNCzK1SKi GsRnpCY4e8xaX0o4Fv3gbp3g9j0RUkM7hg+FqS+oSIt+FZhrOQQ7UlGLCq2JKpq8cBrg K5p6LTx3yACCTMKNoimGvhlG6yU0A+OD13stCnMXepB5PdmqtCHoUveyMdPdwo7a6W4K 5h8Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-76056-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76056-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id a2-20020a0562140c2200b0068f572e776asi10899790qvd.38.2024.02.22.00.06.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 00:06:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-76056-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-76056-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76056-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 903791C22F98 for ; Thu, 22 Feb 2024 08:06:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A95F128684; Thu, 22 Feb 2024 08:04:01 +0000 (UTC) Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (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 7A15720313; Thu, 22 Feb 2024 08:03:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708589040; cv=none; b=r1lzlBKeoH7KlDIYB0Jy9YxhCHmt6nH8jYW0/qeJkc+LFEqInWaSJSp4Py8oE2+iM/2q3O/rkn+4/H78HcA5ZqiJtWSPywLtXtlS0HGLs9yCz+zm7LPQFk7IDQL9Lpka6sqylN1BF8rCD5FCDti+jvhKEQT/RSTMFHzaXpG55xs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708589040; c=relaxed/simple; bh=Iju3C+0qplBOBdqyqtyOXnYa0Y+uKJhdDQlEn2mhijM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=taaRz5wkJOO8mMr+Ffi9Rcvz6nyxb15c/TXFR5vgb0C/HVjuhqWHfjNZdKVV2X9c8IISUK9XPuFno2qO4gW48ZE3l9xRM8kv8HOhf/Yhqwc6yYBVrYaN35M6FY3rhDHkIk53HRFWIPADnbGSMh5eAKdyWt9mIgx3XBS6sD8Abl4= 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.56 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 dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4TgQdF078rz4f3l7W; Thu, 22 Feb 2024 16:03:45 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 0B6331A0283; Thu, 22 Feb 2024 16:03:50 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgBXKBHc_9ZlnaUhEw--.37287S14; Thu, 22 Feb 2024 16:03:49 +0800 (CST) From: Yu Kuai To: paul.e.luse@linux.intel.com, song@kernel.org, neilb@suse.com, shli@fb.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 10/10] md/raid1: factor out helpers to choose the best rdev from read_balance() Date: Thu, 22 Feb 2024 15:58:06 +0800 Message-Id: <20240222075806.1816400-11-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240222075806.1816400-1-yukuai1@huaweicloud.com> References: <20240222075806.1816400-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: 8bit X-CM-TRANSID:cCh0CgBXKBHc_9ZlnaUhEw--.37287S14 X-Coremail-Antispam: 1UD129KBjvJXoW3JrWrWw15WryrArWrXr1DWrg_yoW3Gr1fpw 45GFnay3yUZryruwn5tF4UWrWS934rJa18GrZ7C34I93sagrZ8tF97KryY9Fy5Grs3uw12 v345Gr47C3Z7uFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPI14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_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/ 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 --- drivers/md/raid1.c | 171 ++++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 79 deletions(-) diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c index 223ef8d06f67..938b0e0170df 100644 --- a/drivers/md/raid1.c +++ b/drivers/md/raid1.c @@ -730,73 +730,68 @@ static bool should_choose_next(struct r1conf *conf, int disk) mirror->next_seq_sect - opt_iosize >= mirror->seq_start; } -/* - * 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 *max_sectors) +static bool rdev_readable(struct md_rdev *rdev, struct r1bio *r1_bio) { - const sector_t this_sector = r1_bio->sector; - int sectors; - int best_good_sectors; - int best_disk, best_dist_disk, best_pending_disk; - int disk; - sector_t best_dist; - unsigned int min_pending; - struct md_rdev *rdev; + if (!rdev || test_bit(Faulty, &rdev->flags)) + return false; - retry: - sectors = r1_bio->sectors; - best_disk = -1; - best_dist_disk = -1; - best_dist = MaxSector; - best_pending_disk = -1; - min_pending = UINT_MAX; - best_good_sectors = 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; - 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 readable_disks; +}; + +static int choose_best_rdev(struct r1conf *conf, struct r1bio *r1_bio) +{ + int disk; + struct read_balance_ctl ctl = { + .closest_dist_disk = -1, + .closest_dist = MaxSector, + .min_pending_disk = -1, + .min_pending = UINT_MAX, + }; for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) { + struct md_rdev *rdev; sector_t dist; unsigned int pending; - rdev = conf->mirrors[disk].rdev; - if (r1_bio->bios[disk] == IO_BLOCKED - || rdev == 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] == IO_BLOCKED) continue; - if (rdev_has_badblock(rdev, this_sector, sectors)) + + rdev = conf->mirrors[disk].rdev; + if (!rdev_readable(rdev, r1_bio)) continue; - if (best_disk >= 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++ == 1) set_bit(R1BIO_FailFast, &r1_bio->state); pending = atomic_read(&rdev->nr_pending); - dist = abs(this_sector - conf->mirrors[disk].head_position); + dist = 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 = disk; - break; - } + if (!should_choose_next(conf, disk)) + return disk; /* * Add 'pending' to avoid choosing this disk if there is @@ -810,42 +805,60 @@ static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sect dist = 0; } - if (min_pending > pending) { - min_pending = pending; - best_pending_disk = disk; + if (ctl.min_pending > pending) { + ctl.min_pending = pending; + ctl.min_pending_disk = disk; } - if (dist < best_dist) { - best_dist = dist; - best_dist_disk = disk; + if (dist < ctl.closest_dist) { + ctl.closest_dist = dist; + ctl.closest_dist_disk = disk; } } - /* - * If all disks are rotational, choose the closest disk. If any disk is - * non-rotational, choose the disk with less pending request even the - * disk is rotational, which might/might not be optimal for raids with - * mixed ratation/non-rotational disks depending on workload. - */ - if (best_disk == -1) { - if (conf->mddev->nonrot_disks || min_pending == 0) - best_disk = best_pending_disk; - else - best_disk = best_dist_disk; - } - if (best_disk >= 0) { - rdev = conf->mirrors[best_disk].rdev; - if (!rdev) - goto retry; + if (ctl.min_pending_disk != -1 && + (conf->mddev->nonrot_disks || ctl.min_pending == 0)) + return ctl.min_pending_disk; + else + return ctl.closest_dist_disk; +} - sectors = best_good_sectors; - update_read_sectors(conf, disk, this_sector, sectors); - } - *max_sectors = sectors; +/* + * This routine returns the disk from which the requested read should be done. + * + * 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 choose + * 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 disk 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; + + clear_bit(R1BIO_FailFast, &r1_bio->state); - if (best_disk >= 0) - return best_disk; + if (raid1_should_read_first(conf->mddev, r1_bio->sector, + r1_bio->sectors)) + return choose_first_rdev(conf, r1_bio, max_sectors); + + disk = choose_best_rdev(conf, r1_bio); + if (disk >= 0) { + *max_sectors = r1_bio->sectors; + update_read_sectors(conf, disk, r1_bio->sector, + r1_bio->sectors); + return disk; + } /* * If we are here it means we didn't find a perfectly good disk so -- 2.39.2