From nobody Wed Oct 8 06:01:39 2025 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4EB4D248864; Wed, 2 Jul 2025 09:40:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.149.199.84 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751449224; cv=none; b=ktPTs2MJV25dgkbpm+VZD5gZDWLuKL1vxLwbdzA12wvhIse3S0PyVk2w/bzoH3yQN748LldVgctSKzTEeP6Mm+zF+IXdkBSNmMoIljThrdmQPLEiOgROhz+xTHkC1AiNKyBgzZBVx8G29XqEE2qm+4lTQqY634sTc7EFRLJKDxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751449224; c=relaxed/simple; bh=ZgHnMl9NWn5mCArVqk+Ru79mRa6zWCslA6f+9+nPR4E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gJxaBDSGP+/FaLxne+noxv3dsP3I21yowfXvMN0NmV63HU+/cUNEx8JVS6dUfOWMvkJLuw5iN5Fi5YMR9ZlotWe/5PXvkVq8rnhDrfrqRgxDBjVGz0V1GVxwa35Bs14ySN0WvQP1S1p8gqG6p8rbRNNmxazo/qslH4N+AKlIZaU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ispras.ru; spf=pass smtp.mailfrom=ispras.ru; dkim=pass (1024-bit key) header.d=ispras.ru header.i=@ispras.ru header.b=QHVsQ+gu; arc=none smtp.client-ip=83.149.199.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ispras.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ispras.ru Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ispras.ru header.i=@ispras.ru header.b="QHVsQ+gu" Received: from fedora.intra.ispras.ru (unknown [10.10.165.14]) by mail.ispras.ru (Postfix) with ESMTPSA id C77AC4076182; Wed, 2 Jul 2025 09:40:13 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru C77AC4076182 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1751449213; bh=yuxyEw3gYXfXhUx8APguVkeIxay7jBrsKTT8l2pYXNo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QHVsQ+guVNTwzd/s3GI6k253XvzNMj8ZSbuw4IF/UnkG0mwAZAcAQKjz8l97mXVxb pPx+9RMlAnLKesUaSOvR2Ash+F6lml9MPkmM9dyYNyAPiYjLIOBa+/+bsuYUK6PcGL Pi0Us9cnNEnO99DW4LeXIwG1iQU1n7q8Fk3pwyIs= From: Fedor Pchelkin To: Carlos Maiolino , "Darrick J. Wong" Cc: Fedor Pchelkin , Christoph Hellwig , linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org, lvc-project@linuxtesting.org Subject: [PATCH v2 4/6] xfs: refactor cmp_key_with_cur routines to take advantage of cmp_int() Date: Wed, 2 Jul 2025 12:39:31 +0300 Message-ID: <20250702093935.123798-5-pchelkin@ispras.ru> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250702093935.123798-1-pchelkin@ispras.ru> References: <20250702093935.123798-1-pchelkin@ispras.ru> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The net value of these functions is to determine the result of a three-way-comparison between operands of the same type. Simplify the code using cmp_int() to eliminate potential errors with opencoded casts and subtractions. This also means we can change the return value type of cmp_key_with_cur routines from int64_t to int and make the interface a bit clearer. Found by Linux Verification Center (linuxtesting.org). Suggested-by: "Darrick J. Wong" Signed-off-by: Fedor Pchelkin Reviewed-by: "Darrick J. Wong" --- v2: add R-b fs/xfs/libxfs/xfs_alloc_btree.c | 15 ++++++--------- fs/xfs/libxfs/xfs_bmap_btree.c | 6 +++--- fs/xfs/libxfs/xfs_btree.h | 2 +- fs/xfs/libxfs/xfs_ialloc_btree.c | 6 +++--- fs/xfs/libxfs/xfs_refcount_btree.c | 4 ++-- fs/xfs/libxfs/xfs_rmap_btree.c | 26 +++++--------------------- fs/xfs/libxfs/xfs_rtrefcount_btree.c | 4 ++-- fs/xfs/libxfs/xfs_rtrmap_btree.c | 26 +++++--------------------- fs/xfs/scrub/rcbag_btree.c | 15 +++------------ 9 files changed, 30 insertions(+), 74 deletions(-) diff --git a/fs/xfs/libxfs/xfs_alloc_btree.c b/fs/xfs/libxfs/xfs_alloc_btre= e.c index f371f1b32cfb..fa1f03c1331e 100644 --- a/fs/xfs/libxfs/xfs_alloc_btree.c +++ b/fs/xfs/libxfs/xfs_alloc_btree.c @@ -186,7 +186,7 @@ xfs_allocbt_init_ptr_from_cur( ptr->s =3D agf->agf_cnt_root; } =20 -STATIC int64_t +STATIC int xfs_bnobt_cmp_key_with_cur( struct xfs_btree_cur *cur, const union xfs_btree_key *key) @@ -194,23 +194,20 @@ xfs_bnobt_cmp_key_with_cur( struct xfs_alloc_rec_incore *rec =3D &cur->bc_rec.a; const struct xfs_alloc_rec *kp =3D &key->alloc; =20 - return (int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock; + return cmp_int(be32_to_cpu(kp->ar_startblock), + rec->ar_startblock); } =20 -STATIC int64_t +STATIC int xfs_cntbt_cmp_key_with_cur( struct xfs_btree_cur *cur, const union xfs_btree_key *key) { struct xfs_alloc_rec_incore *rec =3D &cur->bc_rec.a; const struct xfs_alloc_rec *kp =3D &key->alloc; - int64_t diff; - - diff =3D (int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount; - if (diff) - return diff; =20 - return (int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock; + return cmp_int(be32_to_cpu(kp->ar_blockcount), rec->ar_blockcount) ?: + cmp_int(be32_to_cpu(kp->ar_startblock), rec->ar_startblock); } =20 STATIC int diff --git a/fs/xfs/libxfs/xfs_bmap_btree.c b/fs/xfs/libxfs/xfs_bmap_btree.c index bfe67e5d4d11..188feac04b60 100644 --- a/fs/xfs/libxfs/xfs_bmap_btree.c +++ b/fs/xfs/libxfs/xfs_bmap_btree.c @@ -369,13 +369,13 @@ xfs_bmbt_init_rec_from_cur( xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b); } =20 -STATIC int64_t +STATIC int xfs_bmbt_cmp_key_with_cur( struct xfs_btree_cur *cur, const union xfs_btree_key *key) { - return (int64_t)be64_to_cpu(key->bmbt.br_startoff) - - cur->bc_rec.b.br_startoff; + return cmp_int(be64_to_cpu(key->bmbt.br_startoff), + cur->bc_rec.b.br_startoff); } =20 STATIC int diff --git a/fs/xfs/libxfs/xfs_btree.h b/fs/xfs/libxfs/xfs_btree.h index fecd9f0b9398..1bf20d509ac9 100644 --- a/fs/xfs/libxfs/xfs_btree.h +++ b/fs/xfs/libxfs/xfs_btree.h @@ -175,7 +175,7 @@ struct xfs_btree_ops { * Compare key value and cursor value -- positive if key > cur, * negative if key < cur, and zero if equal. */ - int64_t (*cmp_key_with_cur)(struct xfs_btree_cur *cur, + int (*cmp_key_with_cur)(struct xfs_btree_cur *cur, const union xfs_btree_key *key); =20 /* diff --git a/fs/xfs/libxfs/xfs_ialloc_btree.c b/fs/xfs/libxfs/xfs_ialloc_bt= ree.c index ab9fce20b083..100afdd66cdd 100644 --- a/fs/xfs/libxfs/xfs_ialloc_btree.c +++ b/fs/xfs/libxfs/xfs_ialloc_btree.c @@ -265,13 +265,13 @@ xfs_finobt_init_ptr_from_cur( ptr->s =3D agi->agi_free_root; } =20 -STATIC int64_t +STATIC int xfs_inobt_cmp_key_with_cur( struct xfs_btree_cur *cur, const union xfs_btree_key *key) { - return (int64_t)be32_to_cpu(key->inobt.ir_startino) - - cur->bc_rec.i.ir_startino; + return cmp_int(be32_to_cpu(key->inobt.ir_startino), + cur->bc_rec.i.ir_startino); } =20 STATIC int diff --git a/fs/xfs/libxfs/xfs_refcount_btree.c b/fs/xfs/libxfs/xfs_refcoun= t_btree.c index 1c3996b11563..06da3ca14727 100644 --- a/fs/xfs/libxfs/xfs_refcount_btree.c +++ b/fs/xfs/libxfs/xfs_refcount_btree.c @@ -174,7 +174,7 @@ xfs_refcountbt_init_ptr_from_cur( ptr->s =3D agf->agf_refcount_root; } =20 -STATIC int64_t +STATIC int xfs_refcountbt_cmp_key_with_cur( struct xfs_btree_cur *cur, const union xfs_btree_key *key) @@ -185,7 +185,7 @@ xfs_refcountbt_cmp_key_with_cur( =20 start =3D xfs_refcount_encode_startblock(irec->rc_startblock, irec->rc_domain); - return (int64_t)be32_to_cpu(kp->rc_startblock) - start; + return cmp_int(be32_to_cpu(kp->rc_startblock), start); } =20 STATIC int diff --git a/fs/xfs/libxfs/xfs_rmap_btree.c b/fs/xfs/libxfs/xfs_rmap_btree.c index 3cccdb0d0418..bf16aee50d73 100644 --- a/fs/xfs/libxfs/xfs_rmap_btree.c +++ b/fs/xfs/libxfs/xfs_rmap_btree.c @@ -243,34 +243,18 @@ static inline uint64_t offset_keymask(uint64_t offset) return offset & ~XFS_RMAP_OFF_UNWRITTEN; } =20 -STATIC int64_t +STATIC int xfs_rmapbt_cmp_key_with_cur( struct xfs_btree_cur *cur, const union xfs_btree_key *key) { struct xfs_rmap_irec *rec =3D &cur->bc_rec.r; const struct xfs_rmap_key *kp =3D &key->rmap; - __u64 x, y; - int64_t d; - - d =3D (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock; - if (d) - return d; =20 - x =3D be64_to_cpu(kp->rm_owner); - y =3D rec->rm_owner; - if (x > y) - return 1; - else if (y > x) - return -1; - - x =3D offset_keymask(be64_to_cpu(kp->rm_offset)); - y =3D offset_keymask(xfs_rmap_irec_offset_pack(rec)); - if (x > y) - return 1; - else if (y > x) - return -1; - return 0; + return cmp_int(be32_to_cpu(kp->rm_startblock), rec->rm_startblock) ?: + cmp_int(be64_to_cpu(kp->rm_owner), rec->rm_owner) ?: + cmp_int(offset_keymask(be64_to_cpu(kp->rm_offset)), + offset_keymask(xfs_rmap_irec_offset_pack(rec))); } =20 STATIC int diff --git a/fs/xfs/libxfs/xfs_rtrefcount_btree.c b/fs/xfs/libxfs/xfs_rtref= count_btree.c index d9f79ae579c6..ac11e94b42ae 100644 --- a/fs/xfs/libxfs/xfs_rtrefcount_btree.c +++ b/fs/xfs/libxfs/xfs_rtrefcount_btree.c @@ -156,7 +156,7 @@ xfs_rtrefcountbt_init_ptr_from_cur( ptr->l =3D 0; } =20 -STATIC int64_t +STATIC int xfs_rtrefcountbt_cmp_key_with_cur( struct xfs_btree_cur *cur, const union xfs_btree_key *key) @@ -167,7 +167,7 @@ xfs_rtrefcountbt_cmp_key_with_cur( =20 start =3D xfs_refcount_encode_startblock(irec->rc_startblock, irec->rc_domain); - return (int64_t)be32_to_cpu(kp->rc_startblock) - start; + return cmp_int(be32_to_cpu(kp->rc_startblock), start); } =20 STATIC int diff --git a/fs/xfs/libxfs/xfs_rtrmap_btree.c b/fs/xfs/libxfs/xfs_rtrmap_bt= ree.c index 231a189ea2fe..55f903165769 100644 --- a/fs/xfs/libxfs/xfs_rtrmap_btree.c +++ b/fs/xfs/libxfs/xfs_rtrmap_btree.c @@ -185,34 +185,18 @@ static inline uint64_t offset_keymask(uint64_t offset) return offset & ~XFS_RMAP_OFF_UNWRITTEN; } =20 -STATIC int64_t +STATIC int xfs_rtrmapbt_cmp_key_with_cur( struct xfs_btree_cur *cur, const union xfs_btree_key *key) { struct xfs_rmap_irec *rec =3D &cur->bc_rec.r; const struct xfs_rmap_key *kp =3D &key->rmap; - __u64 x, y; - int64_t d; - - d =3D (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock; - if (d) - return d; =20 - x =3D be64_to_cpu(kp->rm_owner); - y =3D rec->rm_owner; - if (x > y) - return 1; - else if (y > x) - return -1; - - x =3D offset_keymask(be64_to_cpu(kp->rm_offset)); - y =3D offset_keymask(xfs_rmap_irec_offset_pack(rec)); - if (x > y) - return 1; - else if (y > x) - return -1; - return 0; + return cmp_int(be32_to_cpu(kp->rm_startblock), rec->rm_startblock) ?: + cmp_int(be64_to_cpu(kp->rm_owner), rec->rm_owner) ?: + cmp_int(offset_keymask(be64_to_cpu(kp->rm_offset)), + offset_keymask(xfs_rmap_irec_offset_pack(rec))); } =20 STATIC int diff --git a/fs/xfs/scrub/rcbag_btree.c b/fs/xfs/scrub/rcbag_btree.c index 46598817b239..9a4ef823c5a7 100644 --- a/fs/xfs/scrub/rcbag_btree.c +++ b/fs/xfs/scrub/rcbag_btree.c @@ -47,7 +47,7 @@ rcbagbt_init_rec_from_cur( bag_rec->rbg_refcount =3D bag_irec->rbg_refcount; } =20 -STATIC int64_t +STATIC int rcbagbt_cmp_key_with_cur( struct xfs_btree_cur *cur, const union xfs_btree_key *key) @@ -55,17 +55,8 @@ rcbagbt_cmp_key_with_cur( struct rcbag_rec *rec =3D (struct rcbag_rec *)&cur->bc_rec; const struct rcbag_key *kp =3D (const struct rcbag_key *)key; =20 - if (kp->rbg_startblock > rec->rbg_startblock) - return 1; - if (kp->rbg_startblock < rec->rbg_startblock) - return -1; - - if (kp->rbg_blockcount > rec->rbg_blockcount) - return 1; - if (kp->rbg_blockcount < rec->rbg_blockcount) - return -1; - - return 0; + return cmp_int(kp->rbg_startblock, rec->rbg_startblock) ?: + cmp_int(kp->rbg_blockcount, rec->rbg_blockcount); } =20 STATIC int --=20 2.50.0