Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp7084979rdb; Fri, 15 Dec 2023 18:24:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IFUx2nYphEsIyuvKZWdzq00yYtU1wstPzqkQehQy46E/LaeD5Gziu9hOJzcAlPiEc8Aq/Y3 X-Received: by 2002:a05:600c:458d:b0:40b:4b9d:6953 with SMTP id r13-20020a05600c458d00b0040b4b9d6953mr4398949wmo.38.1702693484052; Fri, 15 Dec 2023 18:24:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702693484; cv=none; d=google.com; s=arc-20160816; b=oCB9Jq25dS3vpwK6dzhAguabBBJNGAIezfF/xvjt7PuHDaspjUGVhD+mV/8pW7xDxf wwyKtesKbDCIBndJXtO87yRYqWBI7k2ke3s2BnYkpTzNQxROl3kiS6Oq/97rw45NUHhY H4SR0si+xnSc+zhCGUmtk/1zgWYppEMxfMlZlWC3e2BMTh/t9fAYGKL1s2iO34crGsfd oZ1bHkgW2pEbLbqlQqbP35DM+ZcupJzRTHZMDn3Gkx9clmC+qYuWV6PeUU1OCVPmyylb oYAjRhLtPrnd5iDhWisG2Tq8fihC66aSHLKEm0ftbFNi2Tj91jAGNg/Alx2+vtgSbFUH MI6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:date:message-id:from :references:cc:to:subject; bh=3i5m1w63UCtUETWGKcwApFqSPsWDUeMSxKlTjfjHpBQ=; fh=rnmEY9gSAdNuF1rBXsR+C2gQOIecCrGEakEXOdrpSfI=; b=PvSEw6fIs0Ua3zJKECABO1yqqisKZUpXtfvgEFKXWtP5+teHZkGolK3jFHbLS+zWgG LDaZ9QPAVoTRRu8j4Pqb+LxYHjgeumI8EncOCHruQucBjxv2ZOrQjPhNg7QJ0cdZJmj6 t0K92LHF3WBiMChni0U62zUOzcgVGyZXgMAGEQGc1YuT9Ag5Sk+UaAzfaha2h9L/Kwel lsxuOFEF7wd4RUdyvp98htmY2IrWWJO8f345r0kVWGbrPE0TdKWtZfcepEg9XJXz79rl 9MC4WB+ihCtrsBcmD8cVWKpkcbAypZj5J64R0KKArH8bSVLk0qdqZADJSNS4xOxbpgBE c52w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-1959-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1959-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id s20-20020a170906061400b00a1d24ae0eb2si6702562ejb.867.2023.12.15.18.24.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 18:24:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1959-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-1959-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1959-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 am.mirrors.kernel.org (Postfix) with ESMTPS id C997E1F24DD3 for ; Sat, 16 Dec 2023 02:24:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 309811C38; Sat, 16 Dec 2023 02:24:29 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org 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 78F5010A02; Sat, 16 Dec 2023 02:24:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4SsVJz2r3Lz4f3jLt; Sat, 16 Dec 2023 10:24:19 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 04D501A08D2; Sat, 16 Dec 2023 10:24:22 +0800 (CST) Received: from [10.174.176.73] (unknown [10.174.176.73]) by APP1 (Coremail) with SMTP id cCh0CgC3BQxUCn1lhvkTDw--.2147S3; Sat, 16 Dec 2023 10:24:21 +0800 (CST) Subject: Re: [PATCH v2 1/2] md: Fix overflow in is_mddev_idle To: Song Liu , linan666@huaweicloud.com Cc: axboe@kernel.dk, linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com, "yukuai (C)" References: <20231215013931.3329455-1-linan666@huaweicloud.com> <20231215013931.3329455-2-linan666@huaweicloud.com> From: Yu Kuai Message-ID: Date: Sat, 16 Dec 2023 10:24:20 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:cCh0CgC3BQxUCn1lhvkTDw--.2147S3 X-Coremail-Antispam: 1UD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Hi, 在 2023/12/16 7:12, Song Liu 写道: > On Thu, Dec 14, 2023 at 5:41 PM wrote: >> >> From: Li Nan >> >> UBSAN reports this problem: >> >> UBSAN: Undefined behaviour in drivers/md/md.c:8175:15 >> signed integer overflow: >> -2147483291 - 2072033152 cannot be represented in type 'int' >> Call trace: >> dump_backtrace+0x0/0x310 >> show_stack+0x28/0x38 >> dump_stack+0xec/0x15c >> ubsan_epilogue+0x18/0x84 >> handle_overflow+0x14c/0x19c >> __ubsan_handle_sub_overflow+0x34/0x44 >> is_mddev_idle+0x338/0x3d8 >> md_do_sync+0x1bb8/0x1cf8 >> md_thread+0x220/0x288 >> kthread+0x1d8/0x1e0 >> ret_from_fork+0x10/0x18 >> >> 'curr_events' will overflow when stat accum or 'sync_io' is greater than >> INT_MAX. >> >> Fix it by changing sync_io, last_events and curr_events to 64bit. >> >> Signed-off-by: Li Nan >> --- >> drivers/md/md.h | 4 ++-- >> include/linux/blkdev.h | 2 +- >> drivers/md/md.c | 7 ++++--- >> 3 files changed, 7 insertions(+), 6 deletions(-) >> >> diff --git a/drivers/md/md.h b/drivers/md/md.h >> index ade83af123a2..1a4f976951c1 100644 >> --- a/drivers/md/md.h >> +++ b/drivers/md/md.h >> @@ -50,7 +50,7 @@ struct md_rdev { >> >> sector_t sectors; /* Device size (in 512bytes sectors) */ >> struct mddev *mddev; /* RAID array if running */ >> - int last_events; /* IO event timestamp */ >> + long long last_events; /* IO event timestamp */ >> >> /* >> * If meta_bdev is non-NULL, it means that a separate device is >> @@ -584,7 +584,7 @@ extern void mddev_unlock(struct mddev *mddev); >> >> static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors) >> { >> - atomic_add(nr_sectors, &bdev->bd_disk->sync_io); >> + atomic64_add(nr_sectors, &bdev->bd_disk->sync_io); >> } >> >> static inline void md_sync_acct_bio(struct bio *bio, unsigned long nr_sectors) >> diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h >> index 3f8a21cd9233..d28b98adf457 100644 >> --- a/include/linux/blkdev.h >> +++ b/include/linux/blkdev.h >> @@ -170,7 +170,7 @@ struct gendisk { >> struct list_head slave_bdevs; >> #endif >> struct timer_rand_state *random; >> - atomic_t sync_io; /* RAID */ >> + atomic64_t sync_io; /* RAID */ >> struct disk_events *ev; > > As we are on this, I wonder whether we really need this. > AFAICT, is_mddev_idle() is the only consumer of sync_io. > We can probably do the same check in is_mddev_idle() > without sync_io. After reviewing some code, what I'm understand is following: I think 'sync_io' is used to distinguish 'sync io' from raid(this can from different raid, for example, different partition is used for different array) and other io(any io, even it's not from raid). And if there are any other IO, sync speed is limited to min, otherwise it's limited to max. If we want to keep this behaviour, I can't think of any other way for now... > > Thanks, > Song > > >> >> #ifdef CONFIG_BLK_DEV_ZONED >> diff --git a/drivers/md/md.c b/drivers/md/md.c >> index c94373d64f2c..1d71b2a9af03 100644 >> --- a/drivers/md/md.c >> +++ b/drivers/md/md.c >> @@ -8496,14 +8496,15 @@ static int is_mddev_idle(struct mddev *mddev, int init) >> { >> struct md_rdev *rdev; >> int idle; >> - int curr_events; >> + long long curr_events; >> >> idle = 1; >> rcu_read_lock(); >> rdev_for_each_rcu(rdev, mddev) { >> struct gendisk *disk = rdev->bdev->bd_disk; >> - curr_events = (int)part_stat_read_accum(disk->part0, sectors) - >> - atomic_read(&disk->sync_io); >> + curr_events = >> + (long long)part_stat_read_accum(disk->part0, sectors) - >> + atomic64_read(&disk->sync_io); By the way, I don't think this overflow is problematic, assume that sectors accumulate by 'A' and sync_io accumulate by 'B': (setros + A) - (sync_io + B) -(sectors - sync_io) = (A - B) Nomatter overflow or truncation happened of not in the abouve addition and subtraction, the result is correct. And even if io accounting is disabled, which means sectors and A is always 0, the result will always be -B that is <= 0, hence is_mddev_idle() will always return true, and sync_speed will be limited to max in this case. Thanks, Kuai >> /* sync IO will cause sync_io to increase before the disk_stats >> * as sync_io is counted when a request starts, and >> * disk_stats is counted when it completes. >> -- >> 2.39.2 >> >> > . >