Hello Sophiel,

Thank you for the patch. Using memory below 4 GiB for both read and write
buffers addresses the DMA limitation described in the commit message. I
reviewed the supplied diff statically and have the following observations;
I have not compiled or tested the changes.

   1.

   *Zero-length transfers can prevent loop termination.*

   In the write fallback, a zero return from rump_sys_pwrite() leaves
   written unchanged. Similarly, a zero return from rump_sys_pread() leaves
   done unchanged. Both loops can therefore continue indefinitely while
   holding rumpdisk_rwlock.

   The read-side issue already exists in the previous implementation.
   Nevertheless, both loops should handle a zero return explicitly: terminate
   the read on EOF and report an appropriate error when a write makes no
   progress.
   2.

   *Short reads can invalidate the page-boundary assumption.*

   In the direct read path, a positive short read can leave buf + done
   unaligned. A subsequent request of up to pagesize bytes may then cross a
   page boundary, contrary to the stated reason for splitting transfers.

   For this path, the transfer length should also be limited to the bytes
   remaining in the current page. This issue is inherited from the previous
   read loop; the fixed-address bounce-buffer path avoids it.
   3.

   *Short-write handling differs between allocation paths.*

   The contiguous-buffer path performs one pwrite() and returns a short
   result, whereas the fallback retries until completion or error. If a later
   fallback operation fails after earlier writes succeeded, setting written
   = -1 also discards the completed byte count.

   Please check the device interface’s partial-write contract and make the
   two paths consistent with it.
   4.

   *The zero-count return bypasses the open-device check.*

   The new early return precedes the bd->taken check, allowing a
   zero-length write to return success without verifying that the device is
   still open. Moving it after that check would preserve the existing
   validation order.

For validation, I suggest testing the single-page fallback by forcing the
larger contiguous allocation to fail, together with injected zero, short,
and error returns from the underlying I/O calls. Successful boot with at
least 4 GiB of RAM would exercise the reported scenario, but would not
establish coverage of these fallback conditions.

Best regards,

Alperen Erkan

28 Kas 2026 Cmt 04:31 tarihinde Sophiel Zhou via Bug reports for the GNU
Hurd <[email protected]> şunu yazdı:

> On x86_64 systems with 4 GiB or more of RAM (e.g. Debian
> GNU/Hurd amd64 running under QEMU/KVM with >= 4 GiB of memory),
> boot fails when ext2fs accesses the root disk through piixide0:
>
>   ext2fs: part:2:device:wd0: Input/output error
>
> On x86_64, GNU Mach may allocate physical pages above 4 GiB,
> which cannot be accessed by legacy 32-bit DMA controllers such as
> piixide. The existing write path only used a low-memory bounce
> buffer for unaligned source buffers, leaving page-aligned writes
> and reads vulnerable to DMA failures.
>
> Fix both paths in block-rump.c:
>
> * rumpdisk_device_write(): Always copy the data into a buffer
>   allocated below 4 GiB using
>   vm_allocate_contiguous(..., 0, 0x100000000ULL, 0).
>   If contiguous allocation fails due to fragmentation, fall back
>   to a single-page bounce buffer below 4 GiB. Also handle
>   count == 0 early.
>
> * rumpdisk_device_read(): First allocate the destination buffer
>   directly below 4 GiB using vm_allocate_contiguous(). If that
>   fails, fall back to vm_allocate() with a single-page bounce
>   buffer below 4 GiB.
>
> Signed-off-by: Sophiel Zhou <[email protected]>
> ---
>  rumpdisk/block-rump.c | 221 ++++++++++++++++++++++++------------------
>  1 file changed, 129 insertions(+), 92 deletions(-)
>
> diff --git a/rumpdisk/block-rump.c b/rumpdisk/block-rump.c
> index 35fb00e9..abb52ad8 100644
> --- a/rumpdisk/block-rump.c
> +++ b/rumpdisk/block-rump.c
> @@ -412,6 +412,12 @@ rumpdisk_device_write (void *d, mach_port_t
> reply_port,
>    if ((bd->mode & D_WRITE) == 0)
>      return D_INVALID_OPERATION;
>
> +  if (count == 0)
> +    {
> +      *bytes_written = 0;
> +      return D_SUCCESS;
> +    }
> +
>    pthread_rwlock_rdlock (&rumpdisk_rwlock);
>    /* Ensure device is still open */
>    if (! bd->taken)
> @@ -420,69 +426,68 @@ rumpdisk_device_write (void *d, mach_port_t
> reply_port,
>        return D_INVALID_OPERATION;
>      }
>
> -  if ((vm_offset_t) data % pagesize)
> -    {
> -      /* Not aligned, have to copy to aligned buffer.  */
> -      vm_address_t buf;
> -      rpc_phys_addr_t pap;
> -      kern_return_t ret;
> -      int err;
> -
> -      /* While at it, make it contiguous */
> -      ret = vm_allocate_contiguous (master_host, mach_task_self (), &buf,
> &pap, count, 0, 0x100000000ULL, 0);
> -      if (ret != KERN_SUCCESS)
> -       {
> -         pthread_rwlock_unlock (&rumpdisk_rwlock);
> -         return ENOMEM;
> -       }
> -
> -      memcpy ((void*) buf, data, count);
> -
> -      written = rump_sys_pwrite (bd->rump_fd, (const void *)buf,
> (size_t)count, (off_t)bn * bd->block_size);
> -      err = errno;
> -
> -      vm_deallocate (mach_task_self (), (vm_address_t) buf, count);
> -
> -      if (written < 0)
> -       {
> -         vm_deallocate (mach_task_self (), (vm_address_t) data, count);
> -         pthread_rwlock_unlock (&rumpdisk_rwlock);
> -         return rump_errno2host (err);
> -       }
> -    }
> -  else
> -    {
> -      volatile uint8_t dummy_read __attribute__ ((unused));
> -      int npages = (count + pagesize - 1) / pagesize;
> -      int i;
> -
> -      /* Fault-in the memory pages by reading a single byte of each */
> -      for (i = 0; i < npages; i++)
> -       dummy_read = ((volatile uint8_t *)data)[i * pagesize];
> -
> -      /* XXX: _bus_dmamap_load_buffer seems not to call
> rumpcomp_pci_virt_to_mach
> -       * for each page, so separate out pages ourselves :/ */
> -      written = 0;
> -      while (written < count)
> -       {
> -         size_t todo = count - written;
> -         ssize_t done;
> -
> -         if (todo > pagesize)
> -           todo = pagesize;
> -
> -         done = rump_sys_pwrite (bd->rump_fd, (const void *)data +
> written, todo, (off_t)bn * bd->block_size + written);
> -
> -         if (done < 0)
> -           {
> -             vm_deallocate (mach_task_self (), (vm_address_t) data,
> count);
> -             pthread_rwlock_unlock (&rumpdisk_rwlock);
> -             return rump_errno2host (errno);
> -           }
> +  /* Ensure physical addresses are below 4 GiB for 32-bit DMA controllers
> +     such as piixide.  */
> +  {
> +    vm_address_t buf;
> +    rpc_phys_addr_t pap;
> +    kern_return_t ret;
> +    int err = 0;
> +
> +    ret = vm_allocate_contiguous (master_host, mach_task_self (), &buf,
> &pap,
> +                                 count, 0, 0x100000000ULL, 0);
> +    if (ret == KERN_SUCCESS)
> +      {
> +       memcpy ((void *) buf, data, count);
> +       written = rump_sys_pwrite (bd->rump_fd, (const void *) buf,
> +                                  (size_t) count, (off_t) bn *
> bd->block_size);
> +       if (written < 0)
> +         err = errno;
> +       vm_deallocate (mach_task_self (), buf, count);
> +      }
> +    else
> +      {
> +       /* Fall back to a single-page bounce buffer if <4G memory is
> fragmented.  */
> +       ret = vm_allocate_contiguous (master_host, mach_task_self (),
> &buf, &pap,
> +                                     pagesize, 0, 0x100000000ULL, 0);
> +       if (ret != KERN_SUCCESS)
> +         {
> +           pthread_rwlock_unlock (&rumpdisk_rwlock);
> +           return ENOMEM;
> +         }
> +
> +       written = 0;
> +       while (written < count)
> +         {
> +           size_t todo = count - written;
> +           ssize_t done;
> +
> +           if (todo > pagesize)
> +             todo = pagesize;
> +
> +           memcpy ((void *) buf, (const void *) data + written, todo);
> +           done = rump_sys_pwrite (bd->rump_fd, (const void *) buf, todo,
> +                                   (off_t) bn * bd->block_size + written);
> +           if (done < 0)
> +             {
> +               err = errno;
> +               written = -1;
> +               break;
> +             }
> +
> +           written += done;
> +         }
> +
> +       vm_deallocate (mach_task_self (), buf, pagesize);
> +      }
>
> -         written += done;
> -       }
> -    }
> +    if (written < 0)
> +      {
> +       vm_deallocate (mach_task_self (), (vm_address_t) data, count);
> +       pthread_rwlock_unlock (&rumpdisk_rwlock);
> +       return rump_errno2host (err);
> +      }
> +  }
>
>    vm_deallocate (mach_task_self (), (vm_address_t) data, count);
>
> @@ -519,40 +524,72 @@ rumpdisk_device_read (void *d, mach_port_t
> reply_port,
>        return D_INVALID_OPERATION;
>      }
>
> -  /* TODO: directly write at *data when it is aligned */
> +  /* Ensure physical addresses are below 4 GiB for 32-bit DMA controllers
> +     such as piixide.  */
>    *data = 0;
> -  ret = vm_allocate (mach_task_self (), &buf, npages * pagesize, TRUE);
> -  if (ret != KERN_SUCCESS)
> -    {
> -      pthread_rwlock_unlock (&rumpdisk_rwlock);
> -      return ENOMEM;
> -    }
> -
> -  /* Ensure physical allocation by writing a single byte of each */
> -  for (i = 0; i < npages; i++)
> -    ((uint8_t *)buf)[i * pagesize] = 0;
> +  {
> +    rpc_phys_addr_t pap;
> +    vm_address_t bounce = 0;
>
> -  /* XXX: _bus_dmamap_load_buffer seems not to call
> rumpcomp_pci_virt_to_mach
> -   * for each page, so separate out pages ourselves :/ */
> -  done = 0;
> -  while (done < count)
> -    {
> -      size_t todo = count - done;
> -
> -      if (todo > pagesize)
> -       todo = pagesize;
> +    ret = vm_allocate_contiguous (master_host, mach_task_self (), &buf,
> &pap,
> +                                 npages * pagesize, 0, 0x100000000ULL, 0);
> +    if (ret != KERN_SUCCESS)
> +      {
> +       /* Fall back to normal allocation + single-page <4G bounce
> buffer.  */
> +       ret = vm_allocate (mach_task_self (), &buf, npages * pagesize,
> TRUE);
> +       if (ret != KERN_SUCCESS)
> +         {
> +           pthread_rwlock_unlock (&rumpdisk_rwlock);
> +           return ENOMEM;
> +         }
> +
> +       ret = vm_allocate_contiguous (master_host, mach_task_self (),
> +                                     &bounce, &pap, pagesize, 0,
> +                                     0x100000000ULL, 0);
> +       if (ret != KERN_SUCCESS)
> +         {
> +           vm_deallocate (mach_task_self (), buf, npages * pagesize);
> +           pthread_rwlock_unlock (&rumpdisk_rwlock);
> +           return ENOMEM;
> +         }
> +
> +       /* Ensure physical allocation by writing a single byte of each */
> +       for (i = 0; i < npages; i++)
> +         ((uint8_t *) buf)[i * pagesize] = 0;
> +      }
>
> -      err = rump_sys_pread (bd->rump_fd, (void *)buf + done, todo,
> (off_t)bn * bd->block_size + done);
> -      if (err < 0)
> -       {
> -         err = errno;
> -         vm_deallocate (mach_task_self (), buf, npages * pagesize);
> -         pthread_rwlock_unlock (&rumpdisk_rwlock);
> -         return rump_errno2host (err);
> -       }
> +    /* XXX: _bus_dmamap_load_buffer seems not to call
> rumpcomp_pci_virt_to_mach
> +     * for each page, so separate out pages ourselves :/ */
> +    done = 0;
> +    while (done < count)
> +      {
> +       size_t todo = count - done;
> +       void *dst = bounce ? (void *) bounce : (void *) buf + done;
> +
> +       if (todo > pagesize)
> +         todo = pagesize;
> +
> +       err = rump_sys_pread (bd->rump_fd, dst, todo,
> +                             (off_t) bn * bd->block_size + done);
> +       if (err < 0)
> +         {
> +           err = errno;
> +           if (bounce)
> +             vm_deallocate (mach_task_self (), bounce, pagesize);
> +           vm_deallocate (mach_task_self (), buf, npages * pagesize);
> +           pthread_rwlock_unlock (&rumpdisk_rwlock);
> +           return rump_errno2host (err);
> +         }
> +
> +       if (bounce)
> +         memcpy ((void *) buf + done, dst, err);
> +
> +       done += err;
> +      }
>
> -      done += err;
> -    }
> +    if (bounce)
> +      vm_deallocate (mach_task_self (), bounce, pagesize);
> +  }
>
>    *bytes_read = done;
>    *data = (void*) buf;
> --
> 2.56.0
>
>
>

Reply via email to