Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <djwong@kernel.org>
To: Fedor Pchelkin <pchelkin@ispras.ru>
Cc: Carlos Maiolino <cem@kernel.org>,
	Chandan Babu R <chandanbabu@kernel.org>,
	Brian Foster <bfoster@redhat.com>,
	linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	lvc-project@linuxtesting.org, Alexey Nepomnyashih <sdl@nppct.ru>,
	stable@vger.kernel.org
Subject: Re: [PATCH] xfs: fix diff_two_keys calculation for cnt btree
Date: Sat, 26 Apr 2025 08:03:59 -0700	[thread overview]
Message-ID: <20250426150359.GQ25675@frogsfrogsfrogs> (raw)
In-Reply-To: <20250426134232.128864-1-pchelkin@ispras.ru>

On Sat, Apr 26, 2025 at 04:42:31PM +0300, Fedor Pchelkin wrote:
> Currently the difference is computed on 32-bit unsigned values although
> eventually it is stored in a variable of int64_t type. This gives awkward
> results, e.g. when the diff _should_ be negative, it is represented as
> some large positive int64_t value.
> 
> Perform the calculations directly in int64_t as all other diff_two_keys
> routines actually do.
> 
> Found by Linux Verification Center (linuxtesting.org) with Svace static
> analysis tool.
> 
> Fixes: 08438b1e386b ("xfs: plumb in needed functions for range querying of the freespace btrees")
> Cc: stable@vger.kernel.org
> Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
> ---
>  fs/xfs/libxfs/xfs_alloc_btree.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/xfs/libxfs/xfs_alloc_btree.c b/fs/xfs/libxfs/xfs_alloc_btree.c
> index a4ac37ba5d51..b3c54ae90e25 100644
> --- a/fs/xfs/libxfs/xfs_alloc_btree.c
> +++ b/fs/xfs/libxfs/xfs_alloc_btree.c
> @@ -238,13 +238,13 @@ xfs_cntbt_diff_two_keys(
>  	ASSERT(!mask || (mask->alloc.ar_blockcount &&
>  			 mask->alloc.ar_startblock));
>  
> -	diff =  be32_to_cpu(k1->alloc.ar_blockcount) -
> -		be32_to_cpu(k2->alloc.ar_blockcount);
> +	diff = (int64_t)be32_to_cpu(k1->alloc.ar_blockcount) -
> +			be32_to_cpu(k2->alloc.ar_blockcount);

Perhaps it's time to hoist cmp_int to include/ and refactor all these
things to use it?

#define cmp_int(l, r)          ((l > r) - (l < r))

--D

>  	if (diff)
>  		return diff;
>  
> -	return  be32_to_cpu(k1->alloc.ar_startblock) -
> -		be32_to_cpu(k2->alloc.ar_startblock);
> +	return (int64_t)be32_to_cpu(k1->alloc.ar_startblock) -
> +			be32_to_cpu(k2->alloc.ar_startblock);
>  }
>  
>  static xfs_failaddr_t
> -- 
> 2.49.0
> 
> 

  reply	other threads:[~2025-04-26 15:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-26 13:42 [PATCH] xfs: fix diff_two_keys calculation for cnt btree Fedor Pchelkin
2025-04-26 15:03 ` Darrick J. Wong [this message]
2025-04-27 11:56   ` Fedor Pchelkin
2025-04-27 14:25     ` Kent Overstreet
2025-05-05 10:47   ` Fedor Pchelkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250426150359.GQ25675@frogsfrogsfrogs \
    --to=djwong@kernel.org \
    --cc=bfoster@redhat.com \
    --cc=cem@kernel.org \
    --cc=chandanbabu@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=pchelkin@ispras.ru \
    --cc=sdl@nppct.ru \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox