From nobody Thu Sep 19 23:12:06 2024 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) (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 783933B784; Thu, 18 Jul 2024 08:42:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.61.82.184 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721292125; cv=none; b=UlKkxI+0JIzzllN8gwIErQto9Gr4FcN2NZZ58Y9DPOS89DvobUlQ8VH+GFXyud5HJ4rZ2SUYqX9u+wT5Wnje5YJX7NghyVpDjNHpNNu+zKWJzzPcKFP1R5JrHwPgHmi8V5AV85Mh7g07OuB7cIrr030fFMe6NyIjLvhEobx804w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721292125; c=relaxed/simple; bh=bZxPqhxRGdkOe6ZLtw3jKcUWDWSwAmYsYQ61dXx/HOM=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=n8PIYb4gV/t2p7F6PPCiu5NEaXDnGbSnNnjw/TFc5E3SVGQL4f++IjkfMMO/JycI3aVxEZLzzxeIOHb5VdBIeqr6S/1lYVf6Yuwi6Js0Y6jkpw+K7em2ZBqUWCTbo6Eb031M+zwNHL9W1QyusGTZmLWu36tfPlLVhyKwkKI3OEw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com; spf=pass smtp.mailfrom=mediatek.com; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b=f7uD2/F7; arc=none smtp.client-ip=210.61.82.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mediatek.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="f7uD2/F7" X-UUID: 954dd25644e111ef87684b57767b52b1-20240718 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=7cJPIYBOZKxw9By0YCCiUkOLCsHVgJUmv6aYpcZa7dk=; b=f7uD2/F7SkTFOfUYHRiR66c1Sk2Hi+FvaLxUyzevtOaezO80RN9BEmg9rJz0ekolP6FcjJKCmy60GT9YNMmNRLexmkAAKLL1IU6+2uEkN9hQuRq471+y4EiQKy4L4L7F6aKrVpqXMzhVQbP3pHW84cehEMVTCGJ5M5Da7rTWl4o=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.40,REQID:53f467ad-055b-4c6d-be37-d96deb48ce75,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:ba885a6,CLOUDID:6b877245-a117-4f46-a956-71ffeac67bfa,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:11|1,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES :1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_ULN X-UUID: 954dd25644e111ef87684b57767b52b1-20240718 Received: from mtkmbs13n2.mediatek.inc [(172.21.101.108)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 717439943; Thu, 18 Jul 2024 16:41:53 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Thu, 18 Jul 2024 16:41:53 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Thu, 18 Jul 2024 16:41:53 +0800 From: boy.wu To: Tejun Heo , Josef Bacik , Jens Axboe CC: Matthias Brugger , AngeloGioacchino Del Regno , Boris Burkov , , , , , , , Boy Wu Subject: [PATCH v4] blk-cgroup: Replace u64 sync with spinlock for iostat Date: Thu, 18 Jul 2024 16:41:12 +0800 Message-ID: <20240718084112.12202-1-boy.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-Product-Ver: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-AS-Result: No-10--7.618200-8.000000 X-TMASE-MatchedRID: YZKi2GGnn9+ZtBi01n3C9xafLXbshfogcx5k3wffojOHwGEm+CpYGTEG FjeZwyRUzbrWhbT2b8cTMOv2lZG8w2zFfXEzaNt8wCZxkTHxccn64i5lgawyBFSOymiJfTYXZmL HoWzm2fS1hH+43GdK0QG2ORx9EyapCCk23kSAVszovZduO2AF9zQAp53S718HVo+424uIBNJYTM 99NlfBm1EXNKIgpuQj+9p3HcFJM3mVhIWL9FEuN54CIKY/Hg3AGdQnQSTrKGPEQdG7H66TyH4gK q42LRYkpO8ojRYm4lcl4rDZgwtgKw3kTcv9FMbtpUElRFcYt7l+3BndfXUhXQ== X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--7.618200-8.000000 X-TMASE-Version: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-SNTS-SMTP: F123CDD9A65F6182C8CF3180A6DA8AB398AC9F1B101EEA855735D2109E5C8CC32000:8 X-MTK: N Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Boy Wu In 32bit SMP systems, if multiple CPUs call blkcg_print_stat, it may cause blkcg_fill_root_iostats to have a concurrent problem on the seqlock in u64_stats_update, which will cause a deadlock on u64_stats_fetch_begin in blkcg_print_one_stat. Thus, replace u64 sync with spinlock to protect iostat. Fixes: ef45fe470e1e ("blk-cgroup: show global disk stats in root cgroup io.= stat") Signed-off-by: Boy Wu --- Change in v2: - update commit message - Remove u64_sync - Replace spin_lock_irq with guard statement - Replace blkg->q->queue_lock with blkg_stat_lock Change in v3: - update commit message - Add spinlock in blkg_iostat_set structure - Replace all u64_sync with spinlock for iostat - Replace blkg_stat_lock with iostat.spinlock Change in v4: - update commit message - Remove spinlock in blkg_iostat_set structure - Replace iostat.spinlock with blkg_stat_lock - Add 32-bit systems only define --- block/blk-cgroup.c | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 37e6cc91d576..faa604c6fab9 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -329,7 +329,6 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg,= struct gendisk *disk, INIT_WORK(&blkg->async_bio_work, blkg_async_bio_workfn); #endif =20 - u64_stats_init(&blkg->iostat.sync); for_each_possible_cpu(cpu) { u64_stats_init(&per_cpu_ptr(blkg->iostat_cpu, cpu)->sync); per_cpu_ptr(blkg->iostat_cpu, cpu)->blkg =3D blkg; @@ -632,18 +631,18 @@ static void blkg_iostat_set(struct blkg_iostat *dst, = struct blkg_iostat *src) static void __blkg_clear_stat(struct blkg_iostat_set *bis) { struct blkg_iostat cur =3D {0}; - unsigned long flags; =20 - flags =3D u64_stats_update_begin_irqsave(&bis->sync); blkg_iostat_set(&bis->cur, &cur); blkg_iostat_set(&bis->last, &cur); - u64_stats_update_end_irqrestore(&bis->sync, flags); } =20 static void blkg_clear_stat(struct blkcg_gq *blkg) { int cpu; =20 +#if BITS_PER_LONG =3D=3D 32 + guard(raw_spinlock_irqsave)(&blkg_stat_lock); +#endif for_each_possible_cpu(cpu) { struct blkg_iostat_set *s =3D per_cpu_ptr(blkg->iostat_cpu, cpu); =20 @@ -995,15 +994,12 @@ static void blkcg_iostat_update(struct blkcg_gq *blkg= , struct blkg_iostat *cur, struct blkg_iostat *last) { struct blkg_iostat delta; - unsigned long flags; =20 /* propagate percpu delta to global */ - flags =3D u64_stats_update_begin_irqsave(&blkg->iostat.sync); blkg_iostat_set(&delta, cur); blkg_iostat_sub(&delta, last); blkg_iostat_add(&blkg->iostat.cur, &delta); blkg_iostat_add(last, &delta); - u64_stats_update_end_irqrestore(&blkg->iostat.sync, flags); } =20 static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu) @@ -1034,7 +1030,6 @@ static void __blkcg_rstat_flush(struct blkcg *blkcg, = int cpu) struct blkcg_gq *blkg =3D bisc->blkg; struct blkcg_gq *parent =3D blkg->parent; struct blkg_iostat cur; - unsigned int seq; =20 /* * Order assignment of `next_bisc` from `bisc->lnode.next` in @@ -1051,10 +1046,7 @@ static void __blkcg_rstat_flush(struct blkcg *blkcg,= int cpu) goto propagate_up; /* propagate up to parent only */ =20 /* fetch the current per-cpu values */ - do { - seq =3D u64_stats_fetch_begin(&bisc->sync); - blkg_iostat_set(&cur, &bisc->cur); - } while (u64_stats_fetch_retry(&bisc->sync, seq)); + blkg_iostat_set(&cur, &bisc->cur); =20 blkcg_iostat_update(blkg, &cur, &bisc->last); =20 @@ -1112,7 +1104,6 @@ static void blkcg_fill_root_iostats(void) struct blkcg_gq *blkg =3D bdev->bd_disk->queue->root_blkg; struct blkg_iostat tmp; int cpu; - unsigned long flags; =20 memset(&tmp, 0, sizeof(tmp)); for_each_possible_cpu(cpu) { @@ -1133,10 +1124,10 @@ static void blkcg_fill_root_iostats(void) tmp.bytes[BLKG_IOSTAT_DISCARD] +=3D cpu_dkstats->sectors[STAT_DISCARD] << 9; } - - flags =3D u64_stats_update_begin_irqsave(&blkg->iostat.sync); +#if BITS_PER_LONG =3D=3D 32 + guard(raw_spinlock_irqsave)(&blkg_stat_lock); +#endif blkg_iostat_set(&blkg->iostat.cur, &tmp); - u64_stats_update_end_irqrestore(&blkg->iostat.sync, flags); } } =20 @@ -1145,7 +1136,6 @@ static void blkcg_print_one_stat(struct blkcg_gq *blk= g, struct seq_file *s) struct blkg_iostat_set *bis =3D &blkg->iostat; u64 rbytes, wbytes, rios, wios, dbytes, dios; const char *dname; - unsigned seq; int i; =20 if (!blkg->online) @@ -1157,16 +1147,18 @@ static void blkcg_print_one_stat(struct blkcg_gq *b= lkg, struct seq_file *s) =20 seq_printf(s, "%s ", dname); =20 - do { - seq =3D u64_stats_fetch_begin(&bis->sync); - +#if BITS_PER_LONG =3D=3D 32 + scoped_guard(raw_spinlock_irqsave, &blkg_stat_lock) { +#endif rbytes =3D bis->cur.bytes[BLKG_IOSTAT_READ]; wbytes =3D bis->cur.bytes[BLKG_IOSTAT_WRITE]; dbytes =3D bis->cur.bytes[BLKG_IOSTAT_DISCARD]; rios =3D bis->cur.ios[BLKG_IOSTAT_READ]; wios =3D bis->cur.ios[BLKG_IOSTAT_WRITE]; dios =3D bis->cur.ios[BLKG_IOSTAT_DISCARD]; - } while (u64_stats_fetch_retry(&bis->sync, seq)); +#if BITS_PER_LONG =3D=3D 32 + } +#endif =20 if (rbytes || wbytes || rios || wios) { seq_printf(s, "rbytes=3D%llu wbytes=3D%llu rios=3D%llu wios=3D%llu dbyte= s=3D%llu dios=3D%llu", --=20 2.18.0