Skip to contentSkip to conversation

9pfs: fix wrong I/O block size in Rgetattr

PATCH In review Christian Schoenebeck 3 participants 5 messages Sep 22, 2021 – Sep 23, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 9pfs: fix wrong I/O block size in Rgetattr

When client sent a 9p Tgetattr request then the wrong I/O block size value was returned by 9p server; instead of host file system's I/O block size it should rather return an I/O block size according to 9p session's 'msize' value, because the value returned to client should be an "optimum" block size for I/O (i.e. to maximize performance), it should not reflect the actual physical block size of the underlying storage media.

The I/O block size of a host filesystem is typically 4k, so the value returned was far too low for good 9p I/O performance.

This patch adds stat_to_iounit() with a similar approach as the existing get_iounit() function.

---
 hw/9pfs/9p.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
hw/9pfs/9p.c +20 -1
index c857b31321..708b030474 100644
@@ -1262,6 +1262,25 @@ static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
 #define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
 
 
+static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf)
+{
+    int32_t iounit = 0;
+    V9fsState *s = pdu->s;
+
+    /*
+     * iounit should be multiples of st_blksize (host filesystem block size)
+     * as well as less than (client msize - P9_IOHDRSZ)
+     */
+    if (stbuf->st_blksize) {
+        iounit = stbuf->st_blksize;
+        iounit *= (s->msize - P9_IOHDRSZ) / stbuf->st_blksize;
+    }
+    if (!iounit) {
+        iounit = s->msize - P9_IOHDRSZ;
+    }
+    return iounit;
+}
+
 static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
                                 V9fsStatDotl *v9lstat)
 {
@@ -1273,7 +1292,7 @@ static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
     v9lstat->st_gid = stbuf->st_gid;
     v9lstat->st_rdev = stbuf->st_rdev;
     v9lstat->st_size = stbuf->st_size;
-    v9lstat->st_blksize = stbuf->st_blksize;
+    v9lstat->st_blksize = stat_to_iounit(pdu, stbuf);
     v9lstat->st_blocks = stbuf->st_blocks;
     v9lstat->st_atime_sec = stbuf->st_atime;
     v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
Signature · 1 line
2.20.1
#1 lore raw
Quoted 29 lines · Christian Schoenebeck

When client sent a 9p Tgetattr request then the wrong I/O block size value was returned by 9p server; instead of host file system's I/O block size it should rather return an I/O block size according to 9p session's 'msize' value, because the value returned to client should be an "optimum" block size for I/O (i.e. to maximize performance), it should not reflect the actual physical block size of the underlying storage media.

The I/O block size of a host filesystem is typically 4k, so the value returned was far too low for good 9p I/O performance.

This patch adds stat_to_iounit() with a similar approach as the existing get_iounit() function.

---
  hw/9pfs/9p.c | 21 ++++++++++++++++++++-
  1 file changed, 20 insertions(+), 1 deletion(-)
hw/9pfs/9p.c +2 -0
index c857b31321..708b030474 100644
@@ -1262,6 +1262,25 @@ static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
  #define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
 
 
+static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf)
+{

hw/9pfs/9p.c static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, · line 1276

Quoted 10 lines
hw/9pfs/9p.c +10 -0
+    int32_t iounit = 0;
+    V9fsState *s = pdu->s;
+
+    /*
+     * iounit should be multiples of st_blksize (host filesystem block size)
+     * as well as less than (client msize - P9_IOHDRSZ)
+     */
+    if (stbuf->st_blksize) {
+        iounit = stbuf->st_blksize;
+        iounit *= (s->msize - P9_IOHDRSZ) / stbuf->st_blksize;

Is that:

  iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, stbuf->st_blksize);

?

Quoted 20 lines
hw/9pfs/9p.c +8 -1
+    }
+    if (!iounit) {
+        iounit = s->msize - P9_IOHDRSZ;
+    }
+    return iounit;
+}
+
  static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
                                  V9fsStatDotl *v9lstat)
  {
@@ -1273,7 +1292,7 @@ static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
      v9lstat->st_gid = stbuf->st_gid;
      v9lstat->st_rdev = stbuf->st_rdev;
      v9lstat->st_size = stbuf->st_size;
-    v9lstat->st_blksize = stbuf->st_blksize;
+    v9lstat->st_blksize = stat_to_iounit(pdu, stbuf);
      v9lstat->st_blocks = stbuf->st_blocks;
      v9lstat->st_atime_sec = stbuf->st_atime;
      v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
#2 lore raw
Quoted 44 lines · On Mittwoch, 22. September 2021 17:42:08 CEST Philippe Mathieu-Daudé wrote:
Quoted 30 lines · Christian Schoenebeck

When client sent a 9p Tgetattr request then the wrong I/O block size value was returned by 9p server; instead of host file system's I/O block size it should rather return an I/O block size according to 9p session's 'msize' value, because the value returned to client should be an "optimum" block size for I/O (i.e. to maximize performance), it should not reflect the actual physical block size of the underlying storage media.

The I/O block size of a host filesystem is typically 4k, so the value returned was far too low for good 9p I/O performance.

This patch adds stat_to_iounit() with a similar approach as the existing get_iounit() function.

---

  hw/9pfs/9p.c | 21 ++++++++++++++++++++-
  1 file changed, 20 insertions(+), 1 deletion(-)
hw/9pfs/9p.c +1 -0
index c857b31321..708b030474 100644
@@ -1262,6 +1262,25 @@ static int coroutine_fn stat_to_v9stat(V9fsPDU
 *pdu, V9fsPath *path,> 
  #define P9_STATS_ALL           0x00003fffULL /* Mask for All fields
  above */> 
+static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat
 *stbuf) +{

hw/9pfs/9p.c static int coroutine_fn stat_to_v9stat(V9fsPDU · line 1276

Quoted 10 lines
hw/9pfs/9p.c +9 -0
+    int32_t iounit = 0;
+    V9fsState *s = pdu->s;
+
+    /*
+     * iounit should be multiples of st_blksize (host filesystem block
 size) +     * as well as less than (client msize - P9_IOHDRSZ)
+     */
+    if (stbuf->st_blksize) {
+        iounit = stbuf->st_blksize;
+        iounit *= (s->msize - P9_IOHDRSZ) / stbuf->st_blksize;

Is that:

Quoted 4 lines
   iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, stbuf->st_blksize);

?

Yes it is, thanks for the hint! :)

I actually just took the equivalent, already existing code from get_iounit():
https://github.com/qemu/qemu/blob/2c3e83f92d93fbab071b8a96b8ab769b01902475/hw/9pfs/9p.c#L1880

Would it be OK to do that subsequently with cleanup patches? My plan was to first address this with one patch, and addressing the cleanup issues separately later on, because this patch is required for testing the following kernel patches:
https://lore.kernel.org/netdev/[email protected]/

And I wanted to keep things simple by only requiring one patch on QEMU side for now.

Quoted 26 lines
Quoted 26 lines
hw/9pfs/9p.c +7 -0
+    }
+    if (!iounit) {
+        iounit = s->msize - P9_IOHDRSZ;
+    }
+    return iounit;
+}
+
 
  static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
 
                                  V9fsStatDotl *v9lstat)
 
  {
@@ -1273,7 +1292,7 @@ static int stat_to_v9stat_dotl(V9fsPDU *pdu, const

struct stat *stbuf,>

      v9lstat->st_gid = stbuf->st_gid;
      v9lstat->st_rdev = stbuf->st_rdev;
      v9lstat->st_size = stbuf->st_size;
hw/9pfs/9p.c +1 -1
-    v9lstat->st_blksize = stbuf->st_blksize;
+    v9lstat->st_blksize = stat_to_iounit(pdu, stbuf);
 
      v9lstat->st_blocks = stbuf->st_blocks;
      v9lstat->st_atime_sec = stbuf->st_atime;
      v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
Reviewed-by
#3 lore raw
Quoted 55 lines · On Wed, 22 Sep 2021 17:55:02 +0200 Christian Schoenebeck…
Quoted 44 lines · On Mittwoch, 22. September 2021 17:42:08 CEST Philippe Mathieu-Daudé wrote:
Quoted 30 lines · Christian Schoenebeck

When client sent a 9p Tgetattr request then the wrong I/O block size value was returned by 9p server; instead of host file system's I/O block size it should rather return an I/O block size according to 9p session's 'msize' value, because the value returned to client should be an "optimum" block size for I/O (i.e. to maximize performance), it should not reflect the actual physical block size of the underlying storage media.

The I/O block size of a host filesystem is typically 4k, so the value returned was far too low for good 9p I/O performance.

This patch adds stat_to_iounit() with a similar approach as the existing get_iounit() function.

---

  hw/9pfs/9p.c | 21 ++++++++++++++++++++-
  1 file changed, 20 insertions(+), 1 deletion(-)
hw/9pfs/9p.c +1 -0
index c857b31321..708b030474 100644
@@ -1262,6 +1262,25 @@ static int coroutine_fn stat_to_v9stat(V9fsPDU
 *pdu, V9fsPath *path,> 
  #define P9_STATS_ALL           0x00003fffULL /* Mask for All fields
  above */> 
+static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat
 *stbuf) +{

hw/9pfs/9p.c static int coroutine_fn stat_to_v9stat(V9fsPDU · line 1276

Quoted 10 lines
hw/9pfs/9p.c +9 -0
+    int32_t iounit = 0;
+    V9fsState *s = pdu->s;
+
+    /*
+     * iounit should be multiples of st_blksize (host filesystem block
 size) +     * as well as less than (client msize - P9_IOHDRSZ)
+     */
+    if (stbuf->st_blksize) {
+        iounit = stbuf->st_blksize;
+        iounit *= (s->msize - P9_IOHDRSZ) / stbuf->st_blksize;

Is that:

Quoted 4 lines
   iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, stbuf->st_blksize);

?

Yes it is, thanks for the hint! :)

I actually just took the equivalent, already existing code from get_iounit():
https://github.com/qemu/qemu/blob/2c3e83f92d93fbab071b8a96b8ab769b01902475/hw/9pfs/9p.c#L1880

Quoted 9 lines

Would it be OK to do that subsequently with cleanup patches? My plan was to first address this with one patch, and addressing the cleanup issues separately later on, because this patch is required for testing the following kernel patches:
https://lore.kernel.org/netdev/[email protected]/

And I wanted to keep things simple by only requiring one patch on QEMU side for now.

Fair enough and you're the maintainer anyway so this is your call. :-)

Subsequent cleanup would be to switch to QEMU_ALIGN_DOWN() like Philippe suggested but also to consolidate the logic in a common helper in order to avoid the code duplication.

The patch is correct and simple enough to be merged as is :

Quoted 29 lines
Quoted 27 lines
Quoted 27 lines
hw/9pfs/9p.c +7 -0
+    }
+    if (!iounit) {
+        iounit = s->msize - P9_IOHDRSZ;
+    }
+    return iounit;
+}
+
 
  static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
 
                                  V9fsStatDotl *v9lstat)
 
  {
@@ -1273,7 +1292,7 @@ static int stat_to_v9stat_dotl(V9fsPDU *pdu, const

struct stat *stbuf,>

      v9lstat->st_gid = stbuf->st_gid;
      v9lstat->st_rdev = stbuf->st_rdev;
      v9lstat->st_size = stbuf->st_size;
hw/9pfs/9p.c +1 -1
-    v9lstat->st_blksize = stbuf->st_blksize;
+    v9lstat->st_blksize = stat_to_iounit(pdu, stbuf);
 
      v9lstat->st_blocks = stbuf->st_blocks;
      v9lstat->st_atime_sec = stbuf->st_atime;
      v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
#4 lore raw
Quoted 4 lines · On Donnerstag, 23. September 2021 10:40:58 CEST Greg Kurz wrote:

On Wed, 22 Sep 2021 17:55:02 +0200

Quoted 1 line · Christian Schoenebeck

On Mittwoch, 22. September 2021 17:42:08 CEST Philippe Mathieu-Daudé

Quoted 69 lines · wrote:
Quoted 65 lines
Quoted 46 lines · Christian Schoenebeck
Quoted 39 lines

When client sent a 9p Tgetattr request then the wrong I/O block size value was returned by 9p server; instead of host file system's I/O block size it should rather return an I/O block size according to 9p session's 'msize' value, because the value returned to client should be an "optimum" block size for I/O (i.e. to maximize performance), it should not reflect the actual physical block size of the underlying storage media.

The I/O block size of a host filesystem is typically 4k, so the value returned was far too low for good 9p I/O performance.

This patch adds stat_to_iounit() with a similar approach as the existing get_iounit() function.

---

  hw/9pfs/9p.c | 21 ++++++++++++++++++++-
  1 file changed, 20 insertions(+), 1 deletion(-)
hw/9pfs/9p.c +6 -0
index c857b31321..708b030474 100644
@@ -1262,6 +1262,25 @@ static int coroutine_fn stat_to_v9stat(V9fsPDU
 *pdu, V9fsPath *path,>
 
  #define P9_STATS_ALL           0x00003fffULL /* Mask for All fields
  above */>
 
+static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat
 *stbuf) +{
+    int32_t iounit = 0;
+    V9fsState *s = pdu->s;
+
+    /*
+     * iounit should be multiples of st_blksize (host filesystem

block
size) + * as well as less than (client msize - P9_IOHDRSZ)

hw/9pfs/9p.c static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, · line 1276

Quoted 4 lines
hw/9pfs/9p.c +4 -0
+     */
+    if (stbuf->st_blksize) {
+        iounit = stbuf->st_blksize;
+        iounit *= (s->msize - P9_IOHDRSZ) / stbuf->st_blksize;

Is that:

Quoted 3 lines
   iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, stbuf->st_blksize);

?

Yes it is, thanks for the hint! :)

I actually just took the equivalent, already existing code from get_iounit():
https://github.com/qemu/qemu/blob/2c3e83f92d93fbab071b8a96b8ab769b0190247
5/hw/9pfs/9p.c#L1880

Would it be OK to do that subsequently with cleanup patches? My plan was to
first address this with one patch, and addressing the cleanup issues separately later on, because this patch is required for testing the following kernel patches:
https://lore.kernel.org/netdev/[email protected]
m/

Quoted 3 lines

And I wanted to keep things simple by only requiring one patch on QEMU side
for now.

Quoted 6 lines

Fair enough and you're the maintainer anyway so this is your call. :-)

Subsequent cleanup would be to switch to QEMU_ALIGN_DOWN() like Philippe suggested but also to consolidate the logic in a common helper in order to avoid the code duplication.

It will include code deduplication as well, yes.

Quoted 3 lines

The patch is correct and simple enough to be merged as is :

Queued on 9p.next:
https://github.com/cschoenebeck/qemu/commits/9p.next

Thanks!

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
hw/9pfs/9p.c +7 -0
+    }
+    if (!iounit) {
+        iounit = s->msize - P9_IOHDRSZ;
+    }
+    return iounit;
+}
+
 
  static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat
  *stbuf,
 
                                  V9fsStatDotl *v9lstat)
 
  {
@@ -1273,7 +1292,7 @@ static int stat_to_v9stat_dotl(V9fsPDU *pdu,

const
struct stat *stbuf,>

      v9lstat->st_gid = stbuf->st_gid;
      v9lstat->st_rdev = stbuf->st_rdev;
      v9lstat->st_size = stbuf->st_size;
hw/9pfs/9p.c +1 -1
-    v9lstat->st_blksize = stbuf->st_blksize;
+    v9lstat->st_blksize = stat_to_iounit(pdu, stbuf);
 
      v9lstat->st_blocks = stbuf->st_blocks;
      v9lstat->st_atime_sec = stbuf->st_atime;
      v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;