Received: from eggs.gnu.org ([2001:4830:134:3::10]:35119) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ab95J-00040S-Db for qemu-devel@nongnu.org; Wed, 02 Mar 2016 10:53:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ab95E-0003y8-QB for qemu-devel@nongnu.org; Wed, 02 Mar 2016 10:53:09 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34243) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ab95E-0003xv-GQ for qemu-devel@nongnu.org; Wed, 02 Mar 2016 10:53:04 -0500 From: Markus Armbruster References: <1456771254-17511-1-git-send-email-armbru@redhat.com> <1456771254-17511-22-git-send-email-armbru@redhat.com> Date: Wed, 02 Mar 2016 16:53:01 +0100 In-Reply-To: (=?utf-8?Q?=22Marc-Andr=C3=A9?= Lureau"'s message of "Wed, 2 Mar 2016 16:28:53 +0100") Message-ID: <878u20j2qq.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 21/38] ivshmem: Disentangle ivshmem_read() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?utf-8?Q?Marc-Andr=C3=A9?= Lureau Cc: Paolo Bonzini , cam , Claudio Fontana , QEMU , David Marchand Marc-Andr=C3=A9 Lureau writes: > Hi > > On Mon, Feb 29, 2016 at 7:40 PM, Markus Armbruster wr= ote: >> Signed-off-by: Markus Armbruster >> --- >> hw/misc/ivshmem.c | 189 +++++++++++++++++++++++++++--------------------= ------- >> 1 file changed, 96 insertions(+), 93 deletions(-) >> >> diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c >> index 5d33be4..fc46666 100644 >> --- a/hw/misc/ivshmem.c >> +++ b/hw/misc/ivshmem.c >> @@ -564,14 +564,106 @@ static void setup_interrupt(IVShmemState *s, int = vector) >> } >> } >> >> +static void process_msg_shmem(IVShmemState *s, int fd) >> +{ >> + Error *err =3D NULL; >> + void *ptr; >> + >> + if (memory_region_is_mapped(&s->ivshmem)) { >> + error_report("shm already initialized"); >> + close(fd); >> + return; >> + } >> + >> + if (check_shm_size(s, fd, &err) =3D=3D -1) { >> + error_report_err(err); >> + close(fd); >> + return; >> + } >> + >> + /* mmap the region and map into the BAR2 */ >> + ptr =3D mmap(0, s->ivshmem_size, PROT_READ | PROT_WRITE, MAP_SHARED= , fd, 0); >> + if (ptr =3D=3D MAP_FAILED) { >> + error_report("Failed to mmap shared memory %s", strerror(errno)= ); >> + close(fd); >> + return; >> + } >> + memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s), >> + "ivshmem.bar2", s->ivshmem_size, ptr); >> + qemu_set_ram_fd(s->ivshmem.ram_addr, fd); >> + vmstate_register_ram(&s->ivshmem, DEVICE(s)); >> + memory_region_add_subregion(&s->bar, 0, &s->ivshmem); >> +} >> + >> +static void process_msg_disconnect(IVShmemState *s, uint16_t posn) >> +{ >> + IVSHMEM_DPRINTF("posn %d has gone away\n", posn); >> + close_peer_eventfds(s, posn); >> +} >> + >> +static void process_msg_connect(IVShmemState *s, uint16_t posn, int fd) >> +{ >> + Peer *peer =3D &s->peers[posn]; >> + int vector; >> + >> + /* >> + * The N-th connect message for this peer comes with the file >> + * descriptor for vector N-1. Count messages to find the vector. >> + */ >> + if (peer->nb_eventfds >=3D s->vectors) { >> + error_report("Too many eventfd received, device has %d vectors", >> + s->vectors); >> + close(fd); >> + return; >> + } >> + vector =3D peer->nb_eventfds++; >> + >> + IVSHMEM_DPRINTF("eventfds[%d][%d] =3D %d\n", posn, vector, fd); >> + event_notifier_init_fd(&peer->eventfds[vector], fd); >> + fcntl_setfl(fd, O_NONBLOCK); /* msix/irqfd poll non block */ >> + >> + if (posn =3D=3D s->vm_id) { >> + setup_interrupt(s, vector); >> + } >> + >> + if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) { >> + ivshmem_add_eventfd(s, posn, vector); >> + } >> +} >> + >> +static void process_msg(IVShmemState *s, int64_t msg, int fd) >> +{ >> + IVSHMEM_DPRINTF("posn is %" PRId64 ", fd is %d\n", msg, fd); >> + >> + if (msg < -1 || msg > IVSHMEM_MAX_PEERS) { >> + error_report("server sent invalid message %" PRId64, msg); >> + close(fd); >> + return; >> + } >> + >> + if (msg =3D=3D -1) { >> + process_msg_shmem(s, fd); > > the previous code used to close fd if any, it's worth to keep that imho I'm blind. Where? >> + return; >> + } >> + >> + if (msg >=3D s->nb_peers) { >> + resize_peers(s, msg + 1); >> + } >> + >> + if (fd >=3D 0) { >> + process_msg_connect(s, msg, fd); >> + } else if (s->vm_id =3D=3D -1) { >> + s->vm_id =3D msg; >> + } else { >> + process_msg_disconnect(s, msg); >> + } >> +} >> + >> static void ivshmem_read(void *opaque, const uint8_t *buf, int size) >> { >> IVShmemState *s =3D opaque; >> int incoming_fd; >> - int new_eventfd; >> int64_t incoming_posn; >> - Error *err =3D NULL; >> - Peer *peer; >> >> if (!fifo_update_and_get_i64(s, buf, size, &incoming_posn)) { >> return; >> @@ -581,96 +673,7 @@ static void ivshmem_read(void *opaque, const uint8_= t *buf, int size) >> IVSHMEM_DPRINTF("posn is %" PRId64 ", fd is %d\n", >> incoming_posn, incoming_fd); >> >> - if (incoming_posn < -1 || incoming_posn > IVSHMEM_MAX_PEERS) { >> - error_report("server sent invalid message %" PRId64, >> - incoming_posn); >> - if (incoming_fd !=3D -1) { >> - close(incoming_fd); >> - } >> - return; >> - } >> - >> - if (incoming_posn >=3D s->nb_peers) { >> - resize_peers(s, incoming_posn + 1); >> - } >> - >> - peer =3D &s->peers[incoming_posn]; >> - >> - if (incoming_fd =3D=3D -1) { >> - /* if posn is positive and unseen before then this is our posn*/ >> - if (incoming_posn >=3D 0 && s->vm_id =3D=3D -1) { >> - /* receive our posn */ >> - s->vm_id =3D incoming_posn; >> - } else { >> - /* otherwise an fd =3D=3D -1 means an existing peer has gon= e away */ >> - IVSHMEM_DPRINTF("posn %" PRId64 " has gone away\n", incomin= g_posn); >> - close_peer_eventfds(s, incoming_posn); >> - } >> - return; >> - } >> - >> - /* if the position is -1, then it's shared memory region fd */ >> - if (incoming_posn =3D=3D -1) { >> - void * map_ptr; >> - >> - if (memory_region_is_mapped(&s->ivshmem)) { >> - error_report("shm already initialized"); >> - close(incoming_fd); >> - return; >> - } >> - >> - if (check_shm_size(s, incoming_fd, &err) =3D=3D -1) { >> - error_report_err(err); >> - close(incoming_fd); >> - return; >> - } >> - >> - /* mmap the region and map into the BAR2 */ >> - map_ptr =3D mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_= SHARED, >> - incoming_fd= , 0); >> - if (map_ptr =3D=3D MAP_FAILED) { >> - error_report("Failed to mmap shared memory %s", strerror(er= rno)); >> - close(incoming_fd); >> - return; >> - } >> - memory_region_init_ram_ptr(&s->ivshmem, OBJECT(s), >> - "ivshmem.bar2", s->ivshmem_size, map= _ptr); >> - qemu_set_ram_fd(s->ivshmem.ram_addr, incoming_fd); >> - vmstate_register_ram(&s->ivshmem, DEVICE(s)); >> - >> - IVSHMEM_DPRINTF("guest h/w addr =3D %p, size =3D %" PRIu64 "\n", >> - map_ptr, s->ivshmem_size); >> - >> - memory_region_add_subregion(&s->bar, 0, &s->ivshmem); >> - >> - return; >> - } >> - >> - /* each peer has an associated array of eventfds, and we keep >> - * track of how many eventfds received so far */ >> - /* get a new eventfd: */ >> - if (peer->nb_eventfds >=3D s->vectors) { >> - error_report("Too many eventfd received, device has %d vectors", >> - s->vectors); >> - close(incoming_fd); >> - return; >> - } >> - >> - new_eventfd =3D peer->nb_eventfds++; >> - >> - /* this is an eventfd for a particular peer VM */ >> - IVSHMEM_DPRINTF("eventfds[%" PRId64 "][%d] =3D %d\n", incoming_posn, >> - new_eventfd, incoming_fd); >> - event_notifier_init_fd(&peer->eventfds[new_eventfd], incoming_fd); >> - fcntl_setfl(incoming_fd, O_NONBLOCK); /* msix/irqfd poll non block = */ >> - >> - if (incoming_posn =3D=3D s->vm_id) { >> - setup_interrupt(s, new_eventfd); >> - } >> - >> - if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) { >> - ivshmem_add_eventfd(s, incoming_posn, new_eventfd); >> - } >> + process_msg(s, incoming_posn, incoming_fd); > > while at it, you may want to rename incoming_posn to msg for consistency. I didn't to minimize churn, but you're probably right. I'll try and see how the diff comes out. >> } >> >> static void ivshmem_check_version(void *opaque, const uint8_t * buf, in= t size) >> -- >> 2.4.3 >> >> > > looks good otherwise Thanks!