linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ext4: simplify 32bit calculation of lblk
@ 2023-04-03 13:53 wuchi
  2023-04-05  5:40 ` Christoph Hellwig
  0 siblings, 1 reply; 6+ messages in thread
From: wuchi @ 2023-04-03 13:53 UTC (permalink / raw)
  To: tytso, adilger.kernel, ojaswin, ritesh.list; +Cc: linux-ext4, linux-kernel

commit <ad4fb9cafe100a> (ext4: fix 32bit overflow in ext4_ext_find_goal())
uses value compare to fix 32bit overflow. Try to simplify that.

Signed-off-by: wuchi <wuchi.zero@gmail.com>
---
 fs/ext4/extents.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 3559ea6b0781..324b7d1386e0 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -237,10 +237,7 @@ static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
 			ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
 			ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
 
-			if (block > ext_block)
-				return ext_pblk + (block - ext_block);
-			else
-				return ext_pblk - (ext_block - block);
+			return ext_pblk + ((signed long long)block - (signed long long)ext_block);
 		}
 
 		/* it looks like index is empty;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] ext4: simplify 32bit calculation of lblk
  2023-04-03 13:53 [PATCH] ext4: simplify 32bit calculation of lblk wuchi
@ 2023-04-05  5:40 ` Christoph Hellwig
  2023-04-05  8:47   ` chi wu
  0 siblings, 1 reply; 6+ messages in thread
From: Christoph Hellwig @ 2023-04-05  5:40 UTC (permalink / raw)
  To: wuchi; +Cc: tytso, adilger.kernel, ojaswin, ritesh.list, linux-ext4,
	linux-kernel

On Mon, Apr 03, 2023 at 09:53:04PM +0800, wuchi wrote:
> -			if (block > ext_block)
> -				return ext_pblk + (block - ext_block);
> -			else
> -				return ext_pblk - (ext_block - block);
> +			return ext_pblk + ((signed long long)block - (signed long long)ext_block);

And what exactly is the value add here, except for turning an easy
to parse statement into a complex expression using casts?


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ext4: simplify 32bit calculation of lblk
  2023-04-05  5:40 ` Christoph Hellwig
@ 2023-04-05  8:47   ` chi wu
  2023-04-05 11:43     ` David Laight
  0 siblings, 1 reply; 6+ messages in thread
From: chi wu @ 2023-04-05  8:47 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: tytso, adilger.kernel, ojaswin, ritesh.list, linux-ext4,
	linux-kernel

Christoph Hellwig <hch@infradead.org> 于2023年4月5日周三 13:40写道:
>
> On Mon, Apr 03, 2023 at 09:53:04PM +0800, wuchi wrote:
> > -                     if (block > ext_block)
> > -                             return ext_pblk + (block - ext_block);
> > -                     else
> > -                             return ext_pblk - (ext_block - block);
> > +                     return ext_pblk + ((signed long long)block - (signed long long)ext_block);
>
> And what exactly is the value add here, except for turning an easy
> to parse statement into a complex expression using casts?
>
Yes,it will be more complex. the original intention is to reduce the
conditional branch.

=======

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH] ext4: simplify 32bit calculation of lblk
  2023-04-05  8:47   ` chi wu
@ 2023-04-05 11:43     ` David Laight
  2023-04-05 12:47       ` chi wu
  0 siblings, 1 reply; 6+ messages in thread
From: David Laight @ 2023-04-05 11:43 UTC (permalink / raw)
  To: 'chi wu', Christoph Hellwig
  Cc: tytso@mit.edu, adilger.kernel@dilger.ca, ojaswin@linux.ibm.com,
	ritesh.list@gmail.com, linux-ext4@vger.kernel.org,
	linux-kernel@vger.kernel.org

From: chi wu
> Sent: 05 April 2023 09:48
> 
> Christoph Hellwig <hch@infradead.org> 于2023年4月5日周三 13:40写道:
> >
> > On Mon, Apr 03, 2023 at 09:53:04PM +0800, wuchi wrote:
> > > -                     if (block > ext_block)
> > > -                             return ext_pblk + (block - ext_block);
> > > -                     else
> > > -                             return ext_pblk - (ext_block - block);
> > > +                     return ext_pblk + ((signed long long)block - (signed long long)ext_block);
> >
> > And what exactly is the value add here, except for turning an easy
> > to parse statement into a complex expression using casts?
> >
> Yes,it will be more complex. the original intention is to reduce the
> conditional branch.

What is wrong with just:
	return ext_pblk + block - ext_block;
(64bit + 32bit - 32bit)

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ext4: simplify 32bit calculation of lblk
  2023-04-05 11:43     ` David Laight
@ 2023-04-05 12:47       ` chi wu
  2023-04-05 13:55         ` David Laight
  0 siblings, 1 reply; 6+ messages in thread
From: chi wu @ 2023-04-05 12:47 UTC (permalink / raw)
  To: David Laight
  Cc: Christoph Hellwig, tytso@mit.edu, adilger.kernel@dilger.ca,
	ojaswin@linux.ibm.com, ritesh.list@gmail.com,
	linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org

David Laight <David.Laight@aculab.com> 于2023年4月5日周三 19:43写道:
>
> From: chi wu
> > Sent: 05 April 2023 09:48
> >
> > Christoph Hellwig <hch@infradead.org> 于2023年4月5日周三 13:40写道:
> > >
> > > On Mon, Apr 03, 2023 at 09:53:04PM +0800, wuchi wrote:
> > > > -                     if (block > ext_block)
> > > > -                             return ext_pblk + (block - ext_block);
> > > > -                     else
> > > > -                             return ext_pblk - (ext_block - block);
> > > > +                     return ext_pblk + ((signed long long)block - (signed long long)ext_block);
> > >
> > > And what exactly is the value add here, except for turning an easy
> > > to parse statement into a complex expression using casts?
> > >
> > Yes,it will be more complex. the original intention is to reduce the
> > conditional branch.
>
> What is wrong with just:
>         return ext_pblk + block - ext_block;
> (64bit + 32bit - 32bit)
>
oh, It's my fault. I am trapped in that ext_pblk + block may overflow,
but it is ok here. thanks.
>         David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH] ext4: simplify 32bit calculation of lblk
  2023-04-05 12:47       ` chi wu
@ 2023-04-05 13:55         ` David Laight
  0 siblings, 0 replies; 6+ messages in thread
From: David Laight @ 2023-04-05 13:55 UTC (permalink / raw)
  To: 'chi wu'
  Cc: Christoph Hellwig, tytso@mit.edu, adilger.kernel@dilger.ca,
	ojaswin@linux.ibm.com, ritesh.list@gmail.com,
	linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org

From: chi wu
> Sent: 05 April 2023 13:48
> 
> David Laight <David.Laight@aculab.com> 于2023年4月5日周三 19:43写道:
> >
> > From: chi wu
> > > Sent: 05 April 2023 09:48
> > >
> > > Christoph Hellwig <hch@infradead.org> 于2023年4月5日周三 13:40写道:
> > > >
> > > > On Mon, Apr 03, 2023 at 09:53:04PM +0800, wuchi wrote:
> > > > > -                     if (block > ext_block)
> > > > > -                             return ext_pblk + (block - ext_block);
> > > > > -                     else
> > > > > -                             return ext_pblk - (ext_block - block);
> > > > > +                     return ext_pblk + ((signed long long)block - (signed long
> long)ext_block);
> > > >
> > > > And what exactly is the value add here, except for turning an easy
> > > > to parse statement into a complex expression using casts?
> > > >
> > > Yes,it will be more complex. the original intention is to reduce the
> > > conditional branch.
> >
> > What is wrong with just:
> >         return ext_pblk + block - ext_block;
> > (64bit + 32bit - 32bit)
> >

> oh, It's my fault. I am trapped in that ext_pblk + block may overflow,
> but it is ok here. thanks.

That doesn't matter, it will 'un-overflow' when ext_block is subtracted.
You do need the '+' to happen before the '-', ok because +/- group
left to right in C.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-04-05 13:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-03 13:53 [PATCH] ext4: simplify 32bit calculation of lblk wuchi
2023-04-05  5:40 ` Christoph Hellwig
2023-04-05  8:47   ` chi wu
2023-04-05 11:43     ` David Laight
2023-04-05 12:47       ` chi wu
2023-04-05 13:55         ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).