Skip to contentSkip to conversation

fallocate missing fd_offset

PATCH Superseded “William Roche 3 participants 6 messages Jan 21, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover fallocate missing fd_offset

From: William Roche <[email protected]>

Working on the poisoned memory recovery mechanisms with David Hildenbrand, it appeared that the file hole punching done with the memory discard functions are missing the file offset value fd_offset to correctly modify the right file location.

I'm not sure that guest_memfd would use a non zero fd_offset, but I'm also modifying the ram_block_discard_guest_memfd_range() function to include this fallocate use case too.

The fix is checkpatch.pl clean
make check runs fine on both ARM and x86

William Roche (1):
system/physmem: take into account fd_offset for file fallocate

 system/physmem.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.5
#1 lore raw

PATCH 1/1 system/physmem: take into account fd_offset for file fallocate

From: William Roche <[email protected]>

Punching a hole in a file with fallocate needs to take into account the fd_offset value for a correct file location.

Fixes 4b870dc4d0c0 ("hostmem-file: add offset option")
---
 system/physmem.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
system/physmem.c +8 -6
index c76503aea8..687ca94875 100644
@@ -3689,18 +3689,20 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length)
             }
 
             ret = fallocate(rb->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-                            start, length);
+                            start + rb->fd_offset, length);
             if (ret) {
                 ret = -errno;
                 error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)",
-                             __func__, rb->idstr, start, length, ret);
+                             __func__, rb->idstr, start + rb->fd_offset, length,
+                             ret);
                 goto err;
             }
 #else
             ret = -ENOSYS;
             error_report("%s: fallocate not available/file"
                          "%s:%" PRIx64 " +%zx (%d)",
-                         __func__, rb->idstr, start, length, ret);
+                         __func__, rb->idstr, start + rb->fd_offset, length,
+                         ret);
             goto err;
 #endif
         }
@@ -3748,17 +3750,17 @@ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,
 
 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
     ret = fallocate(rb->guest_memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-                    start, length);
+                    start + rb->offset, length);
 
     if (ret) {
         ret = -errno;
         error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)",
-                     __func__, rb->idstr, start, length, ret);
+                     __func__, rb->idstr, start + rb->fd_offset, length, ret);
     }
 #else
     ret = -ENOSYS;
     error_report("%s: fallocate not available %s:%" PRIx64 " +%zx (%d)",
-                 __func__, rb->idstr, start, length, ret);
+                 __func__, rb->idstr, start + rb->fd_offset, length, ret);
 #endif
 
     return ret;
Signature · 1 line
2.43.5
#2 lore raw

Re: [PATCH 1/1] system/physmem: take into account fd_offset for file fallocate

Quoted 30 lines · “William Roche

From: William Roche <[email protected]>

Punching a hole in a file with fallocate needs to take into account the fd_offset value for a correct file location.

Fixes 4b870dc4d0c0 ("hostmem-file: add offset option")
---
 system/physmem.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
system/physmem.c +3 -2
index c76503aea8..687ca94875 100644
@@ -3689,18 +3689,20 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length)
             }
 
             ret = fallocate(rb->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-                            start, length);
+                            start + rb->fd_offset, length);
             if (ret) {
                 ret = -errno;
                 error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)",
-                             __func__, rb->idstr, start, length, ret);
+                             __func__, rb->idstr, start + rb->fd_offset, length,
+                             ret);
                 goto err;
             }

system/physmem.c int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length) · line 3708

Quoted 10 lines
system/physmem.c +2 -1
 #else
             ret = -ENOSYS;
             error_report("%s: fallocate not available/file"
                          "%s:%" PRIx64 " +%zx (%d)",
-                         __func__, rb->idstr, start, length, ret);
+                         __func__, rb->idstr, start + rb->fd_offset, length,
+                         ret);
             goto err;
 #endif
         }

We do have plenty of fd_offset bugs then.. this makes sense to me. Nitpick is we could use a var to cache the total offset.

Quoted 9 lines
system/physmem.c +1 -1
@@ -3748,17 +3750,17 @@ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,
 
 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
     ret = fallocate(rb->guest_memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-                    start, length);
+                    start + rb->offset, length);
 
     if (ret) {
         ret = -errno;

system/physmem.c int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start, · line 3764

Quoted 10 lines
system/physmem.c +2 -2
         error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)",
-                     __func__, rb->idstr, start, length, ret);
+                     __func__, rb->idstr, start + rb->fd_offset, length, ret);
     }
 #else
     ret = -ENOSYS;
     error_report("%s: fallocate not available %s:%" PRIx64 " +%zx (%d)",
-                 __func__, rb->idstr, start, length, ret);
+                 __func__, rb->idstr, start + rb->fd_offset, length, ret);
 #endif

IIUC the offset doesn't apply to gmemfd, see:

        new_block->guest_memfd = kvm_create_guest_memfd(new_block->max_length,
                                                        0, errp);

So my understanding is no matter how the host offset was specified, it ignores it at least in the qemu gmemfd code to always offset from 0, which makes sense to me, as gmemfd is anonymous anyway, and can be created more than one for each VM, so I don't yet see why a gmemfd needs an offset indeed.

Thanks,

Signature · 1 line
Peter Xu
Reviewed-by
#3 lore raw

Re: [PATCH 1/1] system/physmem: take into account fd_offset for file fallocate

Quoted 36 lines · Peter Xu
Quoted 35 lines · “William Roche

From: William Roche <[email protected]>

Punching a hole in a file with fallocate needs to take into account the fd_offset value for a correct file location.

Fixes 4b870dc4d0c0 ("hostmem-file: add offset option")
---
  system/physmem.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
system/physmem.c +3 -3
index c76503aea8..687ca94875 100644
@@ -3689,18 +3689,20 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length)
              }
 
              ret = fallocate(rb->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-                            start, length);
+                            start + rb->fd_offset, length);
              if (ret) {
                  ret = -errno;
                  error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)",
-                             __func__, rb->idstr, start, length, ret);
+                             __func__, rb->idstr, start + rb->fd_offset, length,
+                             ret);
                  goto err;
              }
  #else
              ret = -ENOSYS;
              error_report("%s: fallocate not available/file"
                           "%s:%" PRIx64 " +%zx (%d)",
-                         __func__, rb->idstr, start, length, ret);
Quoted 8 lines

system/physmem.c int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length) · line 3708

Quoted 5 lines
system/physmem.c +2 -0
+                         __func__, rb->idstr, start + rb->fd_offset, length,
+                         ret);
              goto err;
  #endif
          }

We do have plenty of fd_offset bugs then.. this makes sense to me. Nitpick is we could use a var to cache the total offset.

Agreed that makes sense.

Quoted 23 lines
Quoted 10 lines
system/physmem.c +1 -1
@@ -3748,17 +3750,17 @@ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,
 
  #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
      ret = fallocate(rb->guest_memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-                    start, length);
+                    start + rb->offset, length);
 
      if (ret) {
          ret = -errno;

system/physmem.c int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start, · line 3764

Quoted 10 lines
system/physmem.c +2 -2
          error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)",
-                     __func__, rb->idstr, start, length, ret);
+                     __func__, rb->idstr, start + rb->fd_offset, length, ret);
      }
  #else
      ret = -ENOSYS;
      error_report("%s: fallocate not available %s:%" PRIx64 " +%zx (%d)",
-                 __func__, rb->idstr, start, length, ret);
+                 __func__, rb->idstr, start + rb->fd_offset, length, ret);
  #endif

IIUC the offset doesn't apply to gmemfd, see:

Quoted 7 lines
         new_block->guest_memfd = kvm_create_guest_memfd(new_block->max_length,
                                                         0, errp);

So my understanding is no matter how the host offset was specified, it ignores it at least in the qemu gmemfd code to always offset from 0, which makes sense to me, as gmemfd is anonymous anyway, and can be created more than one for each VM, so I don't yet see why a gmemfd needs an offset indeed.

Right.

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw

Re: [PATCH 1/1] system/physmem: take into account fd_offset for file fallocate

Thank you Peter and David for your feedback.

Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Peter Xu
Quoted 8 lines · “William Roche

From: William Roche <[email protected]>

Punching a hole in a file with fallocate needs to take into account the fd_offset value for a correct file location.

Fixes 4b870dc4d0c0 ("hostmem-file: add offset option")

[...]

Quoted 3 lines
Quoted 3 lines

We do have plenty of fd_offset bugs then.. this makes sense to me. Nitpick is we could use a var to cache the total offset.

Ok.

Quoted 10 lines

Agreed that makes sense.

Quoted 7 lines

system/physmem.c int · line 3753

Quoted 7 lines
system/physmem.c +1 -1
@@ -3748,17 +3750,17 @@ int 
 ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,
   #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
       ret = fallocate(rb->guest_memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-                    start, length);
+                    start + rb->offset, length);

I also had this nit - as I should have used rb->fd_offset.

Quoted 16 lines
Quoted 16 lines

system/physmem.c int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start, · line 3764

Quoted 13 lines
system/physmem.c +2 -2
       if (ret) {
           ret = -errno;
           error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)",
-                     __func__, rb->idstr, start, length, ret);
+                     __func__, rb->idstr, start + rb->fd_offset, length, ret);
       }
   #else
       ret = -ENOSYS;
       error_report("%s: fallocate not available %s:%" PRIx64 " +%zx (%d)",
-                 __func__, rb->idstr, start, length, ret);
+                 __func__, rb->idstr, start + rb->fd_offset, length, 
 ret);
   #endif

IIUC the offset doesn't apply to gmemfd, see:

Quoted 8 lines
Quoted 8 lines

         new_block->guest_memfd = kvm_create_guest_memfd(new_block-

Review int · line 3765

Quoted 1 line
(quoted diff) +0 -0
 max_length,

                                                         0, errp);

So my understanding is no matter how the host offset was specified, it ignores it at least in the qemu gmemfd code to always offset from 0, which makes sense to me, as gmemfd is anonymous anyway, and can be created more than one for each VM, so I don't yet see why a gmemfd needs an offset indeed.

Ok I'll remove the ram_block_discard_guest_memfd_range() modifications but include a small comment indicating that we ignore fd_offset in this case.

Quoted 4 lines

Right.

I'm preparing a v2 that I'll send in a few hours.

William.

#5 lore raw

Re: [PATCH 1/1] system/physmem: take into account fd_offset for file fallocate

Quoted 26 lines · William Roche

Thank you Peter and David for your feedback.

Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Peter Xu
Quoted 8 lines · “William Roche

From: William Roche <[email protected]>

Punching a hole in a file with fallocate needs to take into account the fd_offset value for a correct file location.

Fixes 4b870dc4d0c0 ("hostmem-file: add offset option")

[...]

Quoted 4 lines
Quoted 4 lines

We do have plenty of fd_offset bugs then.. this makes sense to me. Nitpick
is we could use a var to cache the total offset.

Ok.

Quoted 4 lines

Agreed that makes sense.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

system/physmem.c int · line 3753

Quoted 6 lines
system/physmem.c +1 -1
@@ -3748,17 +3750,17 @@ int
 ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,
   #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
       ret = fallocate(rb->guest_memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-                    start, length);
+                    start + rb->offset, length);

I also had this nit - as I should have used rb->fd_offset.

Ah, sneaky :)

Signature · 3 lines
Cheers,

David / dhildenb