From: Andreas Dilger <adilger@clusterfs.com>
To: Linus Torvalds <torvalds@osdl.org>
Cc: Anton Altaparmakov <aia21@cam.ac.uk>,
"Stephen C. Tweedie" <sct@redhat.com>,
"ext2-devel@lists.sourceforge.net"
<ext2-devel@lists.sourceforge.net>, Andrew Morton <akpm@osdl.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] sector_t overflow in block layer
Date: Thu, 18 May 2006 17:23:24 -0600 [thread overview]
Message-ID: <20060518232324.GW5964@schatzie.adilger.int> (raw)
In-Reply-To: <Pine.LNX.4.64.0605181526240.10823@g5.osdl.org>
On May 18, 2006 15:41 -0700, Linus Torvalds wrote:
> On Thu, 18 May 2006, Anton Altaparmakov wrote:
> > I think you missed that Andreas said he is worried about 64-bit overflows
> > as well.
>
> Ahh. Ok. However, then the test _really_ should be something like
>
> sector_t maxsector, sector;
> int sector_shift = get_sector_shift(bh->b_size);
>
> maxsector = (~(sector_t)0) >> sector_shift;
> if (unshifted_value > maxsector)
> return -EIO;
> sector = (sector_t) unshifted_value << sector_shift;
>
> which is a lot clearer, and likely faster too, with a proper
> get_sector_shift.
I looked at that also, but it isn't clear from the use of "b_size" here
that there is any constraint that b_size is a power of two, only that it
is a multiple of 512. Granted, I don't know whether there are any users
of such a crazy thing, but the fact that this is in bytes instead of a
shift made me think twice.
> Something like this:
>
> /*
> * What it the shift required to turn a bh of size
> * "size" into a 512-byte sector count?
> */
> static inline int get_sector_shift(unsigned int size)
> {
> int shift = -1;
> unsigned int n = 256;
>
> do {
> shift++;
> } while ((n += n) < size);
> return shift;
> }
>
> which should generate good code on just about any architecture (it avoids
> actually using shifts on purpose), and I think the end result will end up
> being more readable (I'm not claiming that the "get_sector_shift()"
> implementation is readable, I'm claiming that the _users_ will be more
> readable).
This in fact exists virtually identically in blkdev.h::blksize_bits()
which I had a look at, but worried a bit about b_size != 2^n and also
the fact that this has branches and/or loop unwinding vs. the fixed
shift operations.
> Of course, even better would be to not have "b_size" at all, but use
> "b_shift", but we don't have that.
I was thinking exactly the same thing myself.
> And the sector shift calculation might be fast enough that it's even
> a win (turning a 64-bit multiply into a shift _tends_ to be better)
My thought was that the gratuitous 64-bit multiply in the 32-bit case
was offset by the fact that the comparison is easy. In the 64-bit case
we are already doing a 64-bit multiply so the goal is to make the
comparison as cheap as possible.
In the end, I don't really care about the exact mechanics of the check...
Cheers, Andreas
--
Andreas Dilger
Principal Software Engineer
Cluster File Systems, Inc.
next prev parent reply other threads:[~2006-05-18 23:23 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1147849273.16827.27.camel@localhost.localdomain>
[not found] ` <m3odxxukcp.fsf@bzzz.home.net>
[not found] ` <1147884610.16827.44.camel@localhost.localdomain>
[not found] ` <m34pzo36d4.fsf@bzzz.home.net>
[not found] ` <1147888715.12067.38.camel@dyn9047017100.beaverton.ibm.com>
[not found] ` <m364k4zfor.fsf@bzzz.home.net>
[not found] ` <20060517235804.GA5731@schatzie.adilger.int>
[not found] ` <1147947803.5464.19.camel@sisko.sctweedie.blueyonder.co.uk>
2006-05-18 18:59 ` Andreas Dilger
2006-05-18 20:58 ` Alexey Dobriyan
2006-05-18 21:06 ` Linus Torvalds
2006-05-18 22:12 ` Anton Altaparmakov
2006-05-18 22:41 ` Linus Torvalds
2006-05-18 23:23 ` Andreas Dilger [this message]
2006-05-19 19:36 ` Stephen C. Tweedie
2006-05-19 20:11 ` Andrew Morton
2006-05-19 20:55 ` Andreas Dilger
2006-05-19 21:15 ` Andrew Morton
2006-05-19 21:05 ` Stephen C. Tweedie
2006-05-18 22:43 ` Andreas Dilger
2006-05-22 0:14 ` Pavel Machek
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=20060518232324.GW5964@schatzie.adilger.int \
--to=adilger@clusterfs.com \
--cc=aia21@cam.ac.uk \
--cc=akpm@osdl.org \
--cc=ext2-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=sct@redhat.com \
--cc=torvalds@osdl.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
all inboxes | Powered by JetHome®