* [PATCH] bufio: fix the next_buf calculation
@ 2018-04-16 10:05 Michael Chang
2018-04-17 17:11 ` Daniel Kiper
0 siblings, 1 reply; 4+ messages in thread
From: Michael Chang @ 2018-04-16 10:05 UTC (permalink / raw)
To: grub-devel
The next_buf is the offset to the next cached block rounded to the size of
bufio->block_size. However the calculation needs the block_size to be in power
of 2 is not always valid. As an example, files with smaller size than
block_size will have the block_size leveled to the size of file which can be
set arbitrary value.
This patch fixes the next_buf calculation to accept any integers.
Signed-off-by: Michael Chang <mchang@suse.com>
---
grub-core/io/bufio.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/grub-core/io/bufio.c b/grub-core/io/bufio.c
index 22438277d..d0b0f71b6 100644
--- a/grub-core/io/bufio.c
+++ b/grub-core/io/bufio.c
@@ -132,7 +132,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
return res;
/* Need to read some more. */
- next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
+ next_buf = (grub_divmod64 (file->offset + res + len - 1, bufio->block_size, NULL)) * bufio->block_size;
/* Now read between file->offset + res and bufio->buffer_at. */
if (file->offset + res < next_buf)
{
--
2.13.6
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] bufio: fix the next_buf calculation
2018-04-16 10:05 [PATCH] bufio: fix the next_buf calculation Michael Chang
@ 2018-04-17 17:11 ` Daniel Kiper
2018-04-18 7:28 ` Michael Chang
0 siblings, 1 reply; 4+ messages in thread
From: Daniel Kiper @ 2018-04-17 17:11 UTC (permalink / raw)
To: mchang; +Cc: grub-devel, phcoder
CC-ing Vladimir.
On Mon, Apr 16, 2018 at 06:05:04PM +0800, Michael Chang wrote:
> The next_buf is the offset to the next cached block rounded to the size of
> bufio->block_size. However the calculation needs the block_size to be in power
> of 2 is not always valid. As an example, files with smaller size than
> block_size will have the block_size leveled to the size of file which can be
> set arbitrary value.
>
> This patch fixes the next_buf calculation to accept any integers.
>
> Signed-off-by: Michael Chang <mchang@suse.com>
> ---
> grub-core/io/bufio.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/grub-core/io/bufio.c b/grub-core/io/bufio.c
> index 22438277d..d0b0f71b6 100644
> --- a/grub-core/io/bufio.c
> +++ b/grub-core/io/bufio.c
> @@ -132,7 +132,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
> return res;
>
> /* Need to read some more. */
> - next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
> + next_buf = (grub_divmod64 (file->offset + res + len - 1, bufio->block_size, NULL)) * bufio->block_size;
Should not you fix this in grub_bufio_open()? I think that
you should look for closest power of 2 in it.
Daniel
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] bufio: fix the next_buf calculation
2018-04-17 17:11 ` Daniel Kiper
@ 2018-04-18 7:28 ` Michael Chang
2018-04-18 9:15 ` Daniel Kiper
0 siblings, 1 reply; 4+ messages in thread
From: Michael Chang @ 2018-04-18 7:28 UTC (permalink / raw)
To: Daniel Kiper; +Cc: grub-devel, phcoder
On Tue, Apr 17, 2018 at 07:11:34PM +0200, Daniel Kiper wrote:
> CC-ing Vladimir.
>
> On Mon, Apr 16, 2018 at 06:05:04PM +0800, Michael Chang wrote:
> > The next_buf is the offset to the next cached block rounded to the size of
> > bufio->block_size. However the calculation needs the block_size to be in power
> > of 2 is not always valid. As an example, files with smaller size than
> > block_size will have the block_size leveled to the size of file which can be
> > set arbitrary value.
> >
> > This patch fixes the next_buf calculation to accept any integers.
> >
> > Signed-off-by: Michael Chang <mchang@suse.com>
> > ---
> > grub-core/io/bufio.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/grub-core/io/bufio.c b/grub-core/io/bufio.c
> > index 22438277d..d0b0f71b6 100644
> > --- a/grub-core/io/bufio.c
> > +++ b/grub-core/io/bufio.c
> > @@ -132,7 +132,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
> > return res;
> >
> > /* Need to read some more. */
> > - next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
> > + next_buf = (grub_divmod64 (file->offset + res + len - 1, bufio->block_size, NULL)) * bufio->block_size;
>
> Should not you fix this in grub_bufio_open()? I think that
> you should look for closest power of 2 in it.
Of course here we can round up or down the bufio->block_size to meet power of
2. The down side of round-down is inefficient for small files as it can't cache
entire file in one go. The round-up will have to allocate (slightly) larger
than needed buffer to hold small files which is not a big deal ...
If you insist, I can work a new patch to round up the block_size in
grub_bufio_open, just let me know.
Thanks,
Michael
>
> Daniel
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] bufio: fix the next_buf calculation
2018-04-18 7:28 ` Michael Chang
@ 2018-04-18 9:15 ` Daniel Kiper
0 siblings, 0 replies; 4+ messages in thread
From: Daniel Kiper @ 2018-04-18 9:15 UTC (permalink / raw)
To: mchang; +Cc: dkiper, grub-devel, phcoder
On Wed, Apr 18, 2018 at 03:28:20PM +0800, Michael Chang wrote:
> On Tue, Apr 17, 2018 at 07:11:34PM +0200, Daniel Kiper wrote:
> > CC-ing Vladimir.
> >
> > On Mon, Apr 16, 2018 at 06:05:04PM +0800, Michael Chang wrote:
> > > The next_buf is the offset to the next cached block rounded to the size of
> > > bufio->block_size. However the calculation needs the block_size to be in power
> > > of 2 is not always valid. As an example, files with smaller size than
> > > block_size will have the block_size leveled to the size of file which can be
> > > set arbitrary value.
> > >
> > > This patch fixes the next_buf calculation to accept any integers.
> > >
> > > Signed-off-by: Michael Chang <mchang@suse.com>
> > > ---
> > > grub-core/io/bufio.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/grub-core/io/bufio.c b/grub-core/io/bufio.c
> > > index 22438277d..d0b0f71b6 100644
> > > --- a/grub-core/io/bufio.c
> > > +++ b/grub-core/io/bufio.c
> > > @@ -132,7 +132,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
> > > return res;
> > >
> > > /* Need to read some more. */
> > > - next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
> > > + next_buf = (grub_divmod64 (file->offset + res + len - 1, bufio->block_size, NULL)) * bufio->block_size;
> >
> > Should not you fix this in grub_bufio_open()? I think that
> > you should look for closest power of 2 in it.
>
> Of course here we can round up or down the bufio->block_size to meet power of
> 2. The down side of round-down is inefficient for small files as it can't cache
> entire file in one go. The round-up will have to allocate (slightly) larger
> than needed buffer to hold small files which is not a big deal ...
>
> If you insist, I can work a new patch to round up the block_size in
> grub_bufio_open, just let me know.
Please do.
Daniel
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-04-18 9:15 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-16 10:05 [PATCH] bufio: fix the next_buf calculation Michael Chang
2018-04-17 17:11 ` Daniel Kiper
2018-04-18 7:28 ` Michael Chang
2018-04-18 9:15 ` Daniel Kiper
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.