From patchwork Sat May 6 01:23: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: 90650 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp796461vqo; Fri, 5 May 2023 18:33:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4Ery3EttPAJBwD5SbvfmTQfNaDiZQgLfqVaTe/s2OkSodYZ73UATRcGMLPTJaR4BUdguwP X-Received: by 2002:a17:90b:3609:b0:247:2437:d5c4 with SMTP id ml9-20020a17090b360900b002472437d5c4mr3523936pjb.13.1683336793596; Fri, 05 May 2023 18:33:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683336793; cv=none; d=google.com; s=arc-20160816; b=EprFYZ1nvdPhAqUDZgV5mhaFwMSOcnK6WO/PJq4FP/O4AmeNqIw0OAmpQj54y5Cife SBgxw9i9gygbco3zvpASMoTYXcH/4+vc6BUvpxp1wbZobWOALzMHgws8oSehQv3YSVlx F00uzgWjEd8zmgv6jxgwy+MQ7rf9dFXOOsCO1E+fqt5vJur7tn48wXtK9biUtmFMSY2Z 4/j//kAk7PVzGE5A1o0o1Jm8A+0JqSErJ35fXCy8BFFrLpNvNXt5GqeeHake6Mt0QSZH pXIjIQ8BYp4QPvfkqB2LZqj8Xk0Z97yRK9dC4V9mF7jpI3tTeH4fJb6ywIPyz80m8eNR MCbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=MDAApK1+Bape1Mbk3b7UpgmvTNkB6dN/EwU0JL93pQs=; b=0g0ljJySoGiVwhu6bfcruTQKw43c/Bi5Wl5L9/NAPrQWk1Ty3GQ5YlICLECauWtPqm kOhNSEntOe/VfivTIw1f4DChy/PTS3b0KiffxapA4T20AUSdv5pv5q31i3vBabTTiEcp w2qq0NGk+yPbWBinOi1FrERJIuQ7mNSgv4FhZFnjYGFNA69kWs42Ub7+JF4y0On0CfHg XUUBas7MiRvD5ryK045fwSQe/x6kTGIMNjWnlIza3Ccp05h/GFDUd+Z/ddO6GzS3peGV CPJ3Jr9I/xKuppA2k88lw4xjjkzCtku02bwkL8PY+uTw+uBLHlUo+o1ChloLgtATmcUL UAUg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b133-20020a63348b000000b0051802927ee1si3427656pga.330.2023.05.05.18.33.00; Fri, 05 May 2023 18:33:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231940AbjEFBYl (ORCPT + 99 others); Fri, 5 May 2023 21:24:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231428AbjEFBY3 (ORCPT ); Fri, 5 May 2023 21:24:29 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E98547293; Fri, 5 May 2023 18:24:27 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4QCqbC4dFjz4f3pxk; Sat, 6 May 2023 09:24:23 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3X7NHrFVkfh1lIw--.56086S5; Sat, 06 May 2023 09:24:24 +0800 (CST) From: linan666@huaweicloud.com To: song@kernel.org, neilb@suse.de, Rob.Becker@riverbed.com Cc: linux-raid@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 v2 1/4] md/raid10: fix slab-out-of-bounds in md_bitmap_get_counter Date: Sat, 6 May 2023 09:23:12 +0800 Message-Id: <20230506012315.3370489-2-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230506012315.3370489-1-linan666@huaweicloud.com> References: <20230506012315.3370489-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3X7NHrFVkfh1lIw--.56086S5 X-Coremail-Antispam: 1UD129KBjvdXoWrur48urW5XF4UKw4rZFy7Jrb_yoWDXrc_Ca 4FqFyxJrW7CFy3AwnxXw4xZrWjyw4Du3ZruFsaqrySvF18u343WrWvyrnxt3WxGrWj9345 Jryjgayrur4YkjkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUbk8YFVCjjxCrM7AC8VAFwI0_Xr0_Wr1l1xkIjI8I6I8E6xAIw20E Y4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l82xGYIkIc2x26280x7IE14v26r18M2 8IrcIa0xkI8VCY1x0267AKxVWUCVW8JwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK 021l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r 4UJVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ GcCE3s1lnxkEFVAIw20F6cxK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64 xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_JrI_JrylYx0Ex4A2jsIE14v26r1j 6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x02628vn2 kIc2xKxwAKzVCY07xG64k0F24l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_ Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17 CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0 I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I 8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73 UjIFyTuYvjxUwkucDUUUU X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765106561314571619?= X-GMAIL-MSGID: =?utf-8?q?1765106561314571619?= From: Li Nan If we write a large number to md/bitmap_set_bits, md_bitmap_checkpage() will return -EINVAL because "page >= bitmap->pages", but the return value was not checked immediately in md_bitmap_get_counter() in order to set *blocks value and slab-out-of-bounds occurs. Return directly if err is -EINVAL. Fixes: ef4256733506 ("md/bitmap: optimise scanning of empty bitmaps.") Signed-off-by: Li Nan Reviewed-by: Yu Kuai --- drivers/md/md-bitmap.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/md/md-bitmap.c b/drivers/md/md-bitmap.c index 920bb68156d2..0b41ef422da7 100644 --- a/drivers/md/md-bitmap.c +++ b/drivers/md/md-bitmap.c @@ -1388,6 +1388,8 @@ __acquires(bitmap->lock) int err; err = md_bitmap_checkpage(bitmap, page, create, 0); + if (err == -EINVAL) + return NULL; if (bitmap->bp[page].hijacked || bitmap->bp[page].map == NULL) From patchwork Sat May 6 01:23: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: 90652 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp798185vqo; Fri, 5 May 2023 18:39:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6LP1H9vGfa1KClrbDg4GTLNMxR+c9XcpEx7hrAIYrBnF8ir9DfqT8bnM7kCcqIUmYA46Pf X-Received: by 2002:a17:90a:a613:b0:24e:2966:f62 with SMTP id c19-20020a17090aa61300b0024e29660f62mr3552002pjq.42.1683337171924; Fri, 05 May 2023 18:39:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683337171; cv=none; d=google.com; s=arc-20160816; b=Kv9JcOTX/VcI+YV7hmkMO3APpBu/TrqFszKsHqdcdXrIQYQwYCP1HcCJyEOdzC+PgS x7zXXjVBG0NfDtNM/4LH9xmuXX3aX798uRnomzDUKIcF9FdbbkFXudWzJOg0xt9nVZ8G rOzvh9ITzWe2wcaxSiZ47tb29wcNq0Y4d1UM0x6E830D/h25UBVHmCTUGZc3Qfk5z3KP uIQ/SCDxgeFX1VzondLNEVdPRK9gtoTf74eL2WtLsGfUXptxq6AsQOwZhTAdzmhfWLen RVC3UkzJBI5Su1J0uhgS5+Tr+0bK6/el3joTsxwVuJyI7DGjCy2Nbv7sDR/Ne8iJK7a9 QISw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=7JEr1jzyZ822hxLfDqE03e0SDMfm10WJ/q5HofDRhdM=; b=uUEEzaJKXyBbcNykMyNPIWm+Vq/ZPGCMBKlHc1dt97YcVOOoB0fJMoxJ+hyzG/YCyt g3+AuJ+IT8IvuNvJKeG6mhlTphhxbsSM1rAk/oJFDFZIu5mGstyomHWPRyX0l83GN+MW 3VjOij2FLqY1/knB/koOle4vcnIN12/LdVdfsbIox/A2TOMTKlstRuwFuGe4nU8kW7mP XLfoZcsoW3RxdAX0Pwn0i7DDsAWDn10QcqqSJO8booxo9tanMuP+ALiql0k8gEyAGiHF /8TofiEVy67Ls+Xp6W0mWygBFPxzvGD/w23L6IQRUOTz7n7EfUkSWWolM6WQgGmnAAem j/jQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mu18-20020a17090b389200b0023f0955f301si8484656pjb.166.2023.05.05.18.39.19; Fri, 05 May 2023 18:39:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231694AbjEFBYf (ORCPT + 99 others); Fri, 5 May 2023 21:24:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231255AbjEFBY3 (ORCPT ); Fri, 5 May 2023 21:24:29 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E58837289; Fri, 5 May 2023 18:24:27 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4QCqbD0sjHz4f3tNS; Sat, 6 May 2023 09:24:24 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3X7NHrFVkfh1lIw--.56086S6; Sat, 06 May 2023 09:24:25 +0800 (CST) From: linan666@huaweicloud.com To: song@kernel.org, neilb@suse.de, Rob.Becker@riverbed.com Cc: linux-raid@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 v2 2/4] md/raid10: fix overflow in safe_delay_store Date: Sat, 6 May 2023 09:23:13 +0800 Message-Id: <20230506012315.3370489-3-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230506012315.3370489-1-linan666@huaweicloud.com> References: <20230506012315.3370489-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3X7NHrFVkfh1lIw--.56086S6 X-Coremail-Antispam: 1UD129KBjvJXoWxCF18ur15ur18KFy8Kr45Wrg_yoW5Wr4Upa n3J34Yyw4UJryfKF4IvF4DGFy3Was2qrWDt3y2k393JanrXFs0gw1fJa1FvFy5CryYvF13 JrW5AFyUur1UAaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmjb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2vYz4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrV ACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWU JVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2 ka0xkIwI1lw4CEc2x0rVAKj4xxMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kfnx nUUI43ZEXa7IU847K7UUUUU== X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765106958166172055?= X-GMAIL-MSGID: =?utf-8?q?1765106958166172055?= From: Li Nan There is no input check when echo md/safe_mode_delay, and overflow will occur. There is risk of overflow in strict_strtoul_scaled(), too. Fix it by using kstrtoul instead of parsing word one by one. Fixes: 72e02075a33f ("md: factor out parsing of fixed-point numbers") Signed-off-by: Li Nan Reviewed-by: Yu Kuai --- drivers/md/md.c | 70 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/drivers/md/md.c b/drivers/md/md.c index 8e344b4b3444..fd5c3babcd6d 100644 --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -3767,52 +3767,74 @@ static int analyze_sbs(struct mddev *mddev) */ int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) { - unsigned long result = 0; - long decimals = -1; - while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { - if (*cp == '.') - decimals = 0; - else if (decimals < scale) { - unsigned int value; - value = *cp - '0'; - result = result * 10 + value; - if (decimals >= 0) - decimals++; - } - cp++; - } - if (*cp == '\n') - cp++; - if (*cp) + unsigned long result = 0, decimals = 0; + char *pos, *str; + int rv; + + str = kmemdup_nul(cp, strlen(cp), GFP_KERNEL); + if (!str) + return -ENOMEM; + pos = strchr(str, '.'); + if (pos) { + int cnt = scale; + + *pos = '\0'; + while (isdigit(*(++pos))) { + if (cnt) { + decimals = decimals * 10 + *pos - '0'; + cnt--; + } + } + if (*pos == '\n') + pos++; + if (*pos) { + kfree(str); + return -EINVAL; + } + decimals *= int_pow(10, cnt); + } + + rv = kstrtoul(str, 10, &result); + kfree(str); + if (rv) + return rv; + + if (result > (ULONG_MAX - decimals) / (unsigned int)int_pow(10, scale)) return -EINVAL; - if (decimals < 0) - decimals = 0; - *res = result * int_pow(10, scale - decimals); - return 0; + *res = result * int_pow(10, scale) + decimals; + + return rv; } static ssize_t safe_delay_show(struct mddev *mddev, char *page) { - int msec = (mddev->safemode_delay*1000)/HZ; - return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); + unsigned int msec = ((unsigned long)mddev->safemode_delay*1000)/HZ; + + return sprintf(page, "%u.%03u\n", msec/1000, msec%1000); } static ssize_t safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) { unsigned long msec; + int ret; if (mddev_is_clustered(mddev)) { pr_warn("md: Safemode is disabled for clustered mode\n"); return -EINVAL; } - if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) + ret = strict_strtoul_scaled(cbuf, &msec, 3); + if (ret < 0) + return ret; + if (msec > UINT_MAX) return -EINVAL; + if (msec == 0) mddev->safemode_delay = 0; else { unsigned long old_delay = mddev->safemode_delay; + /* HZ <= 1000, so new_delay < UINT_MAX, too */ unsigned long new_delay = (msec*HZ)/1000; if (new_delay == 0) From patchwork Sat May 6 01:23: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: 90654 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp799080vqo; Fri, 5 May 2023 18:42:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ523Hu34knaaLizJW6L9Kvu2ld2a//x0jIm2pp+WD2K9Lnw1TRecgyvXxx5lxqq5SHX0Rt4 X-Received: by 2002:a05:6a20:442a:b0:ff:68f1:679 with SMTP id ce42-20020a056a20442a00b000ff68f10679mr1241838pzb.52.1683337355621; Fri, 05 May 2023 18:42:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683337355; cv=none; d=google.com; s=arc-20160816; b=osJfsbRZCxuRBG8S1RrIG1POkB6i9zM1Tl6rOpUfINKzrNzqZZFEanJE/HKqjMwTZe nbWzipgreqpJna8vvXnOh36x1si3K6jeAkz/JtYsv0XSH8o7QVAv4Mv7ag9sI38X9SSv +qaBpE6C0VLfdBHlq/0w7Z/ewdql8ZMaLC5Ac4lKxj84PJiE1vxmY90UHqM0JQHIfwpa VWhd7f81E4CqrZafOG6Sez5HjxVrjr3j5+Ezd2K4mE5MoTnX7kHWmw+HRPX62Nf7y0qi 13bpVM6d/qipCqh+qZU5b7LGenbdFm3Z2QBy/5NqUyRKaLqE+7yoviw1XBJKlb1fx4Ec uaPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=1YLXeR/5kHPFzxuXJNIveNrhmma9wPB0fMjvVklTLUE=; b=bP0qgwDBCUAjshswRmxV6Zrv3dj7MR3VDFm2aA0x39DrFSTt1u6JeI5U13h0VjMmhk QTgvBKLY29Rqn0mHYcYf3SvuQfHR3JVp+sBFAIguiJ/vYi0BYupZQNJJTed2EYRcNevm XCzBxjnw7rNFT/UXKYQO94QfURrdcP9k1pmsM2e0Vnuroo24uMPrB74hkPTZcrj/rREo LaCJ6KGBLhgMCjvG0tVM+mCHyeZXrnS42fbgF2GABJgdwRcTCiX1eXX9fmmUFRbVSil9 FrQiODjdeom79XKzZDarPdo2kNWGHT2hEMplkp7yUKdSXj5/tgL1L7CdMsF5CJMyQ37N Mkig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c19-20020a637253000000b0050f77f03312si3036138pgn.853.2023.05.05.18.42.22; Fri, 05 May 2023 18:42:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231854AbjEFBYi (ORCPT + 99 others); Fri, 5 May 2023 21:24:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231437AbjEFBY3 (ORCPT ); Fri, 5 May 2023 21:24:29 -0400 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D1F07297; Fri, 5 May 2023 18:24:28 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4QCqbC714mz4f3mLL; Sat, 6 May 2023 09:24:23 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3X7NHrFVkfh1lIw--.56086S7; Sat, 06 May 2023 09:24:25 +0800 (CST) From: linan666@huaweicloud.com To: song@kernel.org, neilb@suse.de, Rob.Becker@riverbed.com Cc: linux-raid@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 v2 3/4] md/raid10: fix wrong setting of max_corr_read_errors Date: Sat, 6 May 2023 09:23:14 +0800 Message-Id: <20230506012315.3370489-4-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230506012315.3370489-1-linan666@huaweicloud.com> References: <20230506012315.3370489-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3X7NHrFVkfh1lIw--.56086S7 X-Coremail-Antispam: 1UD129KBjvJXoW7CrWDCFWruF13uF43uF4kJFb_yoW8urW3pw 4DAasxZr15J34UAr1DJFykua4Fyw1FkayjyryrAw4fWa43Jr98ta4UGayjgr1DWF1ft3W3 XFZ8KrWDCa18tFUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmjb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUWw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2vYz4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrV ACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWU JVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2 ka0xkIwI1lw4CEc2x0rVAKj4xxMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7 AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE 2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcV C2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2Kfnx nUUI43ZEXa7IU1U73PUUUUU== X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765107150772881675?= X-GMAIL-MSGID: =?utf-8?q?1765107150772881675?= From: Li Nan max_corr_read_errors should not be negative number. Change it to unsigned int where use it. Fixes: 1e50915fe0bb ("raid: improve MD/raid10 handling of correctable read errors.") Signed-off-by: Li Nan Reviewed-by: Yu Kuai --- drivers/md/md.c | 2 +- drivers/md/raid10.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/md/md.c b/drivers/md/md.c index fd5c3babcd6d..4a1e566d6bdc 100644 --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -4486,7 +4486,7 @@ __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_stor static ssize_t max_corrected_read_errors_show(struct mddev *mddev, char *page) { - return sprintf(page, "%d\n", + return sprintf(page, "%u\n", atomic_read(&mddev->max_corr_read_errors)); } diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c index 4fcfcb350d2b..4d615fcc6a50 100644 --- a/drivers/md/raid10.c +++ b/drivers/md/raid10.c @@ -2727,7 +2727,8 @@ static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10 int sect = 0; /* Offset from r10_bio->sector */ int sectors = r10_bio->sectors; struct md_rdev *rdev; - int max_read_errors = atomic_read(&mddev->max_corr_read_errors); + unsigned int max_read_errors = + atomic_read(&mddev->max_corr_read_errors); int d = r10_bio->devs[r10_bio->read_slot].devnum; /* still own a reference to this rdev, so it cannot @@ -2743,7 +2744,7 @@ static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10 check_decay_read_errors(mddev, rdev); atomic_inc(&rdev->read_errors); if (atomic_read(&rdev->read_errors) > max_read_errors) { - pr_notice("md/raid10:%s: %pg: Raid device exceeded read_error threshold [cur %d:max %d]\n", + pr_notice("md/raid10:%s: %pg: Raid device exceeded read_error threshold [cur %u:max %u]\n", mdname(mddev), rdev->bdev, atomic_read(&rdev->read_errors), max_read_errors); pr_notice("md/raid10:%s: %pg: Failing raid device\n", From patchwork Sat May 6 01:23: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: 90649 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp796210vqo; Fri, 5 May 2023 18:32:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4S1ZuNM8MHb2q3bQnM7DS6guI16MBvZ6Rt9McML/ubVbkEAC0JTbrAlo+fXMYSA7hJdpTe X-Received: by 2002:a17:90b:30c6:b0:24e:cdc:e1ba with SMTP id hi6-20020a17090b30c600b0024e0cdce1bamr3139721pjb.25.1683336744005; Fri, 05 May 2023 18:32:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683336743; cv=none; d=google.com; s=arc-20160816; b=xVKUOoDAORg+a0NJLhfr4nq36vjBfwsruIlWHfDzCiemdAphNq0HWwjiNbaC0uoxwH WRgUr5twW54iv/vq7qKlgy0TtqPjDBLhxaImyApNEDfHMU4HboCkxoi0Qi4/Oi33d+Ns VHvU60Xe8VyAeGgdpAA0nCM7Ja462z/2g94nvCSn9STnihh7t0WASSdkqvPzWmHBbxFH MRhJTBHcphFP8jgcP1GGoQj1TjdziRnh+d6enJBGS2+E1crrKkF+Sh2ciyKMCXVwPbKZ 3atwDryeIyB0L2HzTr8+aMvt9qlzU3Qtbkw4cFcJLw//5xF1admjUk79+Kcx7jgPPhOQ +Dzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=uijFt9ss5rYk7wJ2GjakG0A3vUXe9Sum6Qh9mmB27Q8=; b=Ororv9WE+GKzQ+VkVxy1ABFQtKopV6xMuT3nar20znT9fwSWZKr3lFEK6f3nWllmi4 Lws7JrXAUDp8Gh0IeFQ/pxEQQvT45Flg32gwB2qkCivo4EOk7ISYC+KoHkD+RHMRlxGa fGgubu1soPEJFThiNlo/A5WUmZ1WZobnoSLFdnLHXryJY96eB4AH6RKrRnQyKxUdnhft 1UQRwzb+FNNy4dzxfadI3QgeTcs4ifBHFJWFTYNVcfeQjdmHqQRbJtO+z5ajw+SzuUk/ MDgbH010KNlMJNBSjZ+LlhWDIFdfEos8TtlH5tmg+vCoPLyGq3P7HSYKEPLagYbStqlH AHKA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w14-20020a63934e000000b0051357734521si3130428pgm.604.2023.05.05.18.32.10; Fri, 05 May 2023 18:32:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232040AbjEFBYp (ORCPT + 99 others); Fri, 5 May 2023 21:24:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231530AbjEFBY3 (ORCPT ); Fri, 5 May 2023 21:24:29 -0400 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 907D96EAF; Fri, 5 May 2023 18:24:28 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4QCqbD2gl1z4f3npy; Sat, 6 May 2023 09:24:24 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3X7NHrFVkfh1lIw--.56086S8; Sat, 06 May 2023 09:24:26 +0800 (CST) From: linan666@huaweicloud.com To: song@kernel.org, neilb@suse.de, Rob.Becker@riverbed.com Cc: linux-raid@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 v2 4/4] md/raid10: optimize check_decay_read_errors() Date: Sat, 6 May 2023 09:23:15 +0800 Message-Id: <20230506012315.3370489-5-linan666@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230506012315.3370489-1-linan666@huaweicloud.com> References: <20230506012315.3370489-1-linan666@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgD3X7NHrFVkfh1lIw--.56086S8 X-Coremail-Antispam: 1UD129KBjvJXoWxCFWxAF4UZFWfKrWkJrW8JFb_yoWrZF4fpa n8Aas8Jr1UJryUAw1DJFyDt3WFyryfAayjyrWxJa1xXwn5Jrn8ta4UGry29348Gas8Aw15 XFZ8Grs8CF4DKFUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmFb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E 14v26rxl6s0DM2vYz4IE04k24VAvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I 8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AK xVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxV A2Y2ka0xkIwI1lw4CEc2x0rVAKj4xxMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY 6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17 CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF 0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU1nmitUUUUU== X-CM-SenderInfo: polqt0awwwqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765106509777130713?= X-GMAIL-MSGID: =?utf-8?q?1765106509777130713?= From: Li Nan check_decay_read_errors() is used to handle rdev->read_errors. But read_errors is inc and read after check_decay_read_errors() is invoked in fix_read_error(). Put all operations of read_errors into check_decay_read_errors() and clean up unnecessary atomic_read of read_errors. Suggested-by: Yu Kuai Signed-off-by: Li Nan --- drivers/md/raid10.c | 66 ++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c index 4d615fcc6a50..79f94882227d 100644 --- a/drivers/md/raid10.c +++ b/drivers/md/raid10.c @@ -2655,39 +2655,53 @@ static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio) } /* - * Used by fix_read_error() to decay the per rdev read_errors. + * Used by fix_read_error() to decay the per rdev read_errors and check if + * read_error > max_read_errors. * We halve the read error count for every hour that has elapsed * since the last recorded read error. * */ -static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev) +static bool check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev) { - long cur_time_mon; + time64_t cur_time_mon = ktime_get_seconds(); unsigned long hours_since_last; - unsigned int read_errors = atomic_read(&rdev->read_errors); - - cur_time_mon = ktime_get_seconds(); + unsigned int read_errors; + unsigned int max_read_errors = + atomic_read(&mddev->max_corr_read_errors); if (rdev->last_read_error == 0) { /* first time we've seen a read error */ rdev->last_read_error = cur_time_mon; - return; - } + } else { + hours_since_last = (long)(cur_time_mon - + rdev->last_read_error) / 3600; - hours_since_last = (long)(cur_time_mon - - rdev->last_read_error) / 3600; + rdev->last_read_error = cur_time_mon; - rdev->last_read_error = cur_time_mon; + /* + * if hours_since_last is > the number of bits in read_errors + * just set read errors to 0. We do this to avoid + * overflowing the shift of read_errors by hours_since_last. + */ + read_errors = atomic_read(&rdev->read_errors); + if (hours_since_last >= 8 * sizeof(read_errors)) + atomic_set(&rdev->read_errors, 0); + else + atomic_set(&rdev->read_errors, + read_errors >> hours_since_last); + } - /* - * if hours_since_last is > the number of bits in read_errors - * just set read errors to 0. We do this to avoid - * overflowing the shift of read_errors by hours_since_last. - */ - if (hours_since_last >= 8 * sizeof(read_errors)) - atomic_set(&rdev->read_errors, 0); - else - atomic_set(&rdev->read_errors, read_errors >> hours_since_last); + read_errors = atomic_inc_return(&rdev->read_errors); + if (read_errors > max_read_errors) { + pr_notice("md/raid10:%s: %pg: Raid device exceeded read_error threshold [cur %u:max %u]\n", + mdname(mddev), rdev->bdev, read_errors, max_read_errors); + pr_notice("md/raid10:%s: %pg: Failing raid device\n", + mdname(mddev), rdev->bdev); + md_error(mddev, rdev); + return false; + } + + return true; } static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector, @@ -2727,8 +2741,6 @@ static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10 int sect = 0; /* Offset from r10_bio->sector */ int sectors = r10_bio->sectors; struct md_rdev *rdev; - unsigned int max_read_errors = - atomic_read(&mddev->max_corr_read_errors); int d = r10_bio->devs[r10_bio->read_slot].devnum; /* still own a reference to this rdev, so it cannot @@ -2741,15 +2753,7 @@ static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10 more fix_read_error() attempts */ return; - check_decay_read_errors(mddev, rdev); - atomic_inc(&rdev->read_errors); - if (atomic_read(&rdev->read_errors) > max_read_errors) { - pr_notice("md/raid10:%s: %pg: Raid device exceeded read_error threshold [cur %u:max %u]\n", - mdname(mddev), rdev->bdev, - atomic_read(&rdev->read_errors), max_read_errors); - pr_notice("md/raid10:%s: %pg: Failing raid device\n", - mdname(mddev), rdev->bdev); - md_error(mddev, rdev); + if (check_decay_read_errors(mddev, rdev)) { r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED; return; }