Skip to contentSkip to conversation

bufio: fix the next_buf calculation

PATCH In review Michael Chang 2 participants 4 messages Apr 16, 2018 – Apr 18, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bufio: fix the next_buf calculation

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.

---
 grub-core/io/bufio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/io/bufio.c +1 -1
index 22438277d..d0b0f71b6 100644
@@ -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)
     {
Signature · 1 line
2.13.6
#1 lore raw

CC-ing Vladimir.

Quoted 18 lines · Michael Chang

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.

---
 grub-core/io/bufio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/io/bufio.c +0 -0
index 22438277d..d0b0f71b6 100644

grub-core/io/bufio.c grub_bufio_read (grub_file_t file, char *buf, grub_size_t len) · line 135

Quoted 5 lines
grub-core/io/bufio.c +1 -1
@@ -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

#2 lore raw
Quoted 23 lines · Daniel Kiper

CC-ing Vladimir.

Quoted 20 lines · Michael Chang

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.

---
 grub-core/io/bufio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/io/bufio.c +0 -0
index 22438277d..d0b0f71b6 100644
@@ -132,7 +132,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
     return res;
 
Quoted 6 lines

grub-core/io/bufio.c grub_bufio_read (grub_file_t file, char *buf, grub_size_t len) · line 135

Quoted 3 lines
grub-core/io/bufio.c +1 -1
   /* 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

Quoted 2 lines

Daniel

#3 lore raw
Quoted 31 lines · Michael Chang
Quoted 23 lines · Daniel Kiper

CC-ing Vladimir.

Quoted 20 lines · Michael Chang

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.

---
 grub-core/io/bufio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/io/bufio.c +0 -0
index 22438277d..d0b0f71b6 100644
@@ -132,7 +132,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
     return res;
 
Quoted 6 lines

grub-core/io/bufio.c grub_bufio_read (grub_file_t file, char *buf, grub_size_t len) · line 135

Quoted 3 lines
grub-core/io/bufio.c +1 -1
   /* 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.

Quoted 7 lines

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