Received: by 2002:a05:7412:8d23:b0:f7:29d7:fb05 with SMTP id bj35csp456842rdb; Sat, 16 Dec 2023 16:28:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IFC1AzML3MJRVr1SOLdXplCmZIcPbWHOOtDNSNQB/5KqSg0KBSnDmY8XcSf4Z9EF31GCmWI X-Received: by 2002:a05:620a:480c:b0:77e:fc1c:97d8 with SMTP id eb12-20020a05620a480c00b0077efc1c97d8mr12017698qkb.36.1702772918443; Sat, 16 Dec 2023 16:28:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702772918; cv=none; d=google.com; s=arc-20160816; b=fd8OYmewF7AblSdmuhZL02MTgZmjOm9Zh30hQgdM9mHBAM8EF+ZWg/dWrgE2iJVu4L GkqbRI35htBBp06god1HyCXm5WmNYmygEQ9enroExZV5y3ywPFTi2HdLaf1+8ekAckJB XD6mYJFvKwOeQYKKc3WUznw/CPurUkahtM/XB2cvBTwq+6ManVJl09fAu9invVcKhGkh OcSBeMmaU9geG5zvkTjT6FhNzG8D2ATQwVwZTCOlLexoE7H8W5kn3UaStAVdAMIN17OI dohuhilBDdP1p6z+yWMz0L8G0QZoKNlyTFCRdTkxjOJWLRGSR4bR3xpIo3v51cGP6TZn BCnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=lrT1RCMXQZTc2Hh40pR14c0voJyQ07lHAhh9XD+b3S4=; fh=wLI906liwyFAqCm+MOQB1j8eDydeZ97JTZyJ29sG0Mg=; b=gtayfqPM1KxQFZR/+hgrub42h1ZDNTuUG81UNjblwC3gg9TFiOleVC1qxIQp0GfrPm 1IHlLAN1W98FP9GryjhdNFDo8UgQu0RYR6Rl2hg+Xm1/2GcKO96YqR/2ZfSgqBCM3cTP cXpYvyDf016Z0Kh4hisukTT3XFgh4dnqMgWrvVMjKYqnQHOxRwqAKMvnsY/jMo/HYmLd jB9tS6Rqfg46zbvEwL84FE61GJPzACncBVaYGxH+PIPfykIGL/hZ2s0ovCK84qf7hsQL GCHaXEPJq1U+0OCdjj+rd3eTIHbViNI/ytUSCvZIbw3DUDaHUctKWWiRAxOlIKe1ZiK4 gVkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QfOyjAO3; spf=pass (google.com: domain of linux-kernel+bounces-2418-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2418-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 l5-20020a05620a28c500b0076f19d66ddfsi21990510qkp.138.2023.12.16.16.28.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 16:28:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2418-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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QfOyjAO3; spf=pass (google.com: domain of linux-kernel+bounces-2418-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2418-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 3450A1C217BA for ; Sun, 17 Dec 2023 00:28:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BE142A47; Sun, 17 Dec 2023 00:28:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QfOyjAO3" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 DCB9436E; Sun, 17 Dec 2023 00:28:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74A96C433C7; Sun, 17 Dec 2023 00:28:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702772905; bh=h2sz8/dtn/sHq1qOGU4fOqWx6jU4GBXGdvx3X6TskjI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=QfOyjAO3Ynvi8JGe+6JpF0MRhhbQIQv/Qo5LgeXRZl0WxMIgHUssSGsFGEI6gu+r9 19JkJv8KNXty2F/o1vusOVekAnH5KAFMMZlklb/C+wGZDzFDsp/pqyPcgt+V2T9flE 0t5yidAH+hRIEDV2bkmshyiYabfuYMG/ID0UIX6pI2FKPytv2WDSZk+xZ9VO5+XuCl U4xv/cjlj+7N8jHI0KZOYJBdoFByvFzQLMDaXEbhIfNYYzXl4rg7bm7kotgj36JB9r HEaHpD6/BTLmVcYAvWFc/n9wSrztmosoZZAaSlS6O6tV4DHuZPFNZNhYEQ9ocARtsn IfpCPGasVbBVw== Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-2ca1e6a94a4so21717051fa.0; Sat, 16 Dec 2023 16:28:25 -0800 (PST) X-Gm-Message-State: AOJu0Yz9P0TWGZLbt2xPE6tW/9UhGTCNoMt3VGoXl4ca8+CT6Ig03A8E Gp3nwg7Z8XvAk+aL/mMs4VgTZwpmZ9qwZjaz9Qg= X-Received: by 2002:a2e:b74b:0:b0:2cc:2f4e:a146 with SMTP id k11-20020a2eb74b000000b002cc2f4ea146mr3510864ljo.53.1702772903626; Sat, 16 Dec 2023 16:28:23 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231215013931.3329455-1-linan666@huaweicloud.com> <20231215013931.3329455-2-linan666@huaweicloud.com> In-Reply-To: From: Song Liu Date: Sat, 16 Dec 2023 16:28:11 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 1/2] md: Fix overflow in is_mddev_idle To: Yu Kuai Cc: linan666@huaweicloud.com, 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)" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Dec 15, 2023 at 6:24=E2=80=AFPM Yu Kuai w= rote: [...] > >> static inline void md_sync_acct_bio(struct bio *bio, unsigned long n= r_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 for looking into this. To keep current behavior, we will need it in gendisk. [...] > >> @@ -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 =3D 1; > >> rcu_read_lock(); > >> rdev_for_each_rcu(rdev, mddev) { > >> struct gendisk *disk =3D rdev->bdev->bd_disk; > >> - curr_events =3D (int)part_stat_read_accum(disk->part0,= sectors) - > >> - atomic_read(&disk->sync_io); > >> + curr_events =3D > >> + (long long)part_stat_read_accum(disk->part0, s= ectors) - > >> + 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) =3D (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 <=3D 0, hence > is_mddev_idle() will always return true, and sync_speed will be limited > to max in this case. We only use this for idle or not check, the behavior is OK (I think). However, this logic is error prone. On 64-bit systems, there is a 4-byte hole behind sync_io. I think we can just use it for atomic64_t so that we don't have to worry about overflow. Thanks, Song