All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: Albert Esteve <aesteve@redhat.com>
Cc: qemu-devel@nongnu.org, jasowang@redhat.com, david@redhat.com,
	slp@redhat.com, "Alex Bennée" <alex.bennee@linaro.org>,
	"Michael S. Tsirkin" <mst@redhat.com>
Subject: Re: [RFC PATCH v2 5/5] vhost_user: Implement mem_read/mem_write handlers
Date: Thu, 5 Sep 2024 15:18:39 -0400	[thread overview]
Message-ID: <20240905191839.GG1922502@fedora> (raw)
In-Reply-To: <CADSE00L=U4jmEvosaSt=a2EQ654kfh4cKzNLQFpFXX==pHZ0Hg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3122 bytes --]

On Wed, Sep 04, 2024 at 03:01:06PM +0200, Albert Esteve wrote:
> On Thu, Jul 11, 2024 at 10:55 AM Stefan Hajnoczi <stefanha@redhat.com>
> wrote:
> 
> > On Fri, Jun 28, 2024 at 04:57:10PM +0200, Albert Esteve wrote:
> > > Implement function handlers for memory read and write
> > > operations.
> > >
> > > Signed-off-by: Albert Esteve <aesteve@redhat.com>
> > > ---
> > >  hw/virtio/vhost-user.c | 34 ++++++++++++++++++++++++++++++----
> > >  1 file changed, 30 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
> > > index 18cacb2d68..79becbc87b 100644
> > > --- a/hw/virtio/vhost-user.c
> > > +++ b/hw/virtio/vhost-user.c
> > > @@ -1884,16 +1884,42 @@ static int
> > >  vhost_user_backend_handle_mem_read(struct vhost_dev *dev,
> > >                                     VhostUserMemRWMsg *mem_rw)
> > >  {
> > > -    /* TODO */
> > > -    return -EPERM;
> > > +    ram_addr_t offset;
> > > +    int fd;
> > > +    MemoryRegion *mr;
> > > +
> > > +    mr = vhost_user_get_mr_data(mem_rw->guest_address, &offset, &fd);
> > > +
> > > +    if (!mr) {
> > > +        error_report("Failed to get memory region with address %"
> > PRIx64,
> > > +                     mem_rw->guest_address);
> > > +        return -EFAULT;
> > > +    }
> > > +
> > > +    memcpy(mem_rw->data, memory_region_get_ram_ptr(mr) + offset,
> > mem_rw->size);
> >
> > Don't try to write this from scratch. Use address_space_read/write(). It
> > supports corner cases like crossing MemoryRegions.
> >
> 
> I am having issues getting the address space from the vhost_dev struct to
> feed
> address_spave_read/write() function with the first parameter. But I found
> mr->ops.
> Would something like this perhaps be enough?
> 
> ```
>     mr->ops->read_with_attrs(mr->opaque, mem_rw->guest_address,
>                              &mem_rw->data, mem_rw->size,
>                              MEMTXATTRS_UNSPECIFIED);
> ```

You can use dev->vdev->dma_as to get the AddressSpace for
address_space_read/write():

  struct vhost_dev {
      VirtIODevice *vdev;
  
  struct VirtIODevice
  {
      ...
      AddressSpace *dma_as;

> 
> 
> >
> > > +
> > > +    return 0;
> > >  }
> > >
> > >  static int
> > >  vhost_user_backend_handle_mem_write(struct vhost_dev *dev,
> > >                                     VhostUserMemRWMsg *mem_rw)
> > >  {
> > > -    /* TODO */
> > > -    return -EPERM;
> > > +    ram_addr_t offset;
> > > +    int fd;
> > > +    MemoryRegion *mr;
> > > +
> > > +    mr = vhost_user_get_mr_data(mem_rw->guest_address, &offset, &fd);
> > > +
> > > +    if (!mr) {
> > > +        error_report("Failed to get memory region with address %"
> > PRIx64,
> > > +                     mem_rw->guest_address);
> > > +        return -EFAULT;
> > > +    }
> > > +
> > > +    memcpy(memory_region_get_ram_ptr(mr) + offset, mem_rw->data,
> > mem_rw->size);
> > > +
> > > +    return 0;
> > >  }
> > >
> > >  static void close_backend_channel(struct vhost_user *u)
> > > --
> > > 2.45.2
> > >
> >

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2024-09-05 19:19 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-28 14:57 [RFC PATCH v2 0/5] vhost-user: Add SHMEM_MAP/UNMAP requests Albert Esteve
2024-06-28 14:57 ` [RFC PATCH v2 1/5] vhost-user: Add VIRTIO Shared Memory map request Albert Esteve
2024-07-11  7:45   ` Stefan Hajnoczi
2024-09-03  9:54     ` Albert Esteve
2024-09-03 11:54       ` Albert Esteve
2024-09-05 16:45         ` Stefan Hajnoczi
2024-09-11 11:57           ` Albert Esteve
2024-09-11 14:54             ` Stefan Hajnoczi
2024-09-04  7:28     ` Albert Esteve
2024-06-28 14:57 ` [RFC PATCH v2 2/5] vhost_user: Add frontend command for shmem config Albert Esteve
2024-07-11  8:10   ` Stefan Hajnoczi
2024-09-04  9:05     ` Albert Esteve
2024-07-11  8:15   ` Stefan Hajnoczi
2024-06-28 14:57 ` [RFC PATCH v2 3/5] vhost-user-dev: Add cache BAR Albert Esteve
2024-07-11  8:25   ` Stefan Hajnoczi
2024-09-04 11:20     ` Albert Esteve
2024-06-28 14:57 ` [RFC PATCH v2 4/5] vhost_user: Add MEM_READ/WRITE backend requests Albert Esteve
2024-07-11  8:53   ` Stefan Hajnoczi
2024-06-28 14:57 ` [RFC PATCH v2 5/5] vhost_user: Implement mem_read/mem_write handlers Albert Esteve
2024-07-11  8:55   ` Stefan Hajnoczi
2024-09-04 13:01     ` Albert Esteve
2024-09-05 19:18       ` Stefan Hajnoczi [this message]
2024-09-10  7:14         ` Albert Esteve
2024-07-11  9:01 ` [RFC PATCH v2 0/5] vhost-user: Add SHMEM_MAP/UNMAP requests Stefan Hajnoczi
2024-07-11 10:56 ` Alyssa Ross
2024-07-12  2:06   ` David Stevens
2024-07-12  5:47     ` Michael S. Tsirkin
2024-07-15  2:30       ` Jason Wang
2024-07-16  1:21       ` David Stevens
2024-09-03  8:42         ` Albert Esteve
2024-09-05 16:39           ` Stefan Hajnoczi
2024-09-06  7:03             ` Albert Esteve
2024-09-06 13:15               ` Stefan Hajnoczi
2024-09-05 15:56         ` Stefan Hajnoczi
2024-09-06  4:18           ` David Stevens
2024-09-06 13:00             ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240905191839.GG1922502@fedora \
    --to=stefanha@redhat.com \
    --cc=aesteve@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=david@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=slp@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.