From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.1 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A089C43331 for ; Wed, 1 Apr 2020 19:00:28 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 16599206EB for ; Wed, 1 Apr 2020 19:00:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UJ92XaL+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 16599206EB Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:36080 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJibD-0008Sh-6w for qemu-devel@archiver.kernel.org; Wed, 01 Apr 2020 15:00:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58229) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJiZm-0006iy-JP for qemu-devel@nongnu.org; Wed, 01 Apr 2020 14:59:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jJiZj-0000e0-K8 for qemu-devel@nongnu.org; Wed, 01 Apr 2020 14:58:57 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:60520 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jJiZj-0000dM-C5 for qemu-devel@nongnu.org; Wed, 01 Apr 2020 14:58:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585767534; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jx2AyuAs3C0kLf7h9pnqGR+Kk9+Ny2Oc+tVSAXIgXLg=; b=UJ92XaL+CWz6nzzP/fSK9dBuUv3Ex0LxpdJVnXAX+zydKBIee8TGX8GOx08OF4vK75erpH Bbu4kyT9pBUYHX7ZyChUBrXuIHduDfrXzfAQ2B2NxyQHVGTblZaLc7CGZxe4Mpgac7w/Rz r0o8tEUsxUAI2pFwzVFi8w5J5KNWGog= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-28-yrZZmz-cPKCaLTztSv6UKw-1; Wed, 01 Apr 2020 14:58:48 -0400 X-MC-Unique: yrZZmz-cPKCaLTztSv6UKw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DF0DC18B5F7D; Wed, 1 Apr 2020 18:58:45 +0000 (UTC) Received: from work-vm (ovpn-115-201.ams2.redhat.com [10.36.115.201]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A56574DA24; Wed, 1 Apr 2020 18:58:31 +0000 (UTC) Date: Wed, 1 Apr 2020 19:58:29 +0100 From: "Dr. David Alan Gilbert" To: Kirti Wankhede Subject: Re: [PATCH v16 QEMU 10/16] vfio: Add load state functions to SaveVMHandlers Message-ID: <20200401185829.GH52559@work-vm> References: <1585084154-29461-1-git-send-email-kwankhede@nvidia.com> <1585084154-29461-11-git-send-email-kwankhede@nvidia.com> MIME-Version: 1.0 In-Reply-To: <1585084154-29461-11-git-send-email-kwankhede@nvidia.com> User-Agent: Mutt/1.13.4 (2020-02-15) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhengxiao.zx@alibaba-inc.com, kevin.tian@intel.com, yi.l.liu@intel.com, cjia@nvidia.com, eskultet@redhat.com, ziye.yang@intel.com, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, qemu-devel@nongnu.org, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, alex.williamson@redhat.com, eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, yan.y.zhao@intel.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" * Kirti Wankhede (kwankhede@nvidia.com) wrote: > Sequence during _RESUMING device state: > While data for this device is available, repeat below steps: > a. read data_offset from where user application should write data. > b. write data of data_size to migration region from data_offset. > c. write data_size which indicates vendor driver that data is written in > staging buffer. >=20 > For user, data is opaque. User should write data in the same order as > received. >=20 > Signed-off-by: Kirti Wankhede > Reviewed-by: Neo Jia > --- > hw/vfio/migration.c | 179 +++++++++++++++++++++++++++++++++++++++++++++= ++++++ > hw/vfio/trace-events | 3 + > 2 files changed, 182 insertions(+) >=20 > diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c > index ecbeed5182c2..ab295d25620e 100644 > --- a/hw/vfio/migration.c > +++ b/hw/vfio/migration.c > @@ -269,6 +269,33 @@ static int vfio_save_device_config_state(QEMUFile *f= , void *opaque) > return qemu_file_get_error(f); > } > =20 > +static int vfio_load_device_config_state(QEMUFile *f, void *opaque) > +{ > + VFIODevice *vbasedev =3D opaque; > + uint64_t data; > + > + if (vbasedev->ops && vbasedev->ops->vfio_load_config) { > + int ret; > + > + ret =3D vbasedev->ops->vfio_load_config(vbasedev, f); > + if (ret) { > + error_report("%s: Failed to load device config space", > + vbasedev->name); > + return ret; > + } > + } > + > + data =3D qemu_get_be64(f); > + if (data !=3D VFIO_MIG_FLAG_END_OF_STATE) { > + error_report("%s: Failed loading device config space, " > + "end flag incorrect 0x%"PRIx64, vbasedev->name, dat= a); > + return -EINVAL; > + } > + > + trace_vfio_load_device_config_state(vbasedev->name); > + return qemu_file_get_error(f); > +} > + > /* ---------------------------------------------------------------------= - */ > =20 > static int vfio_save_setup(QEMUFile *f, void *opaque) > @@ -434,12 +461,164 @@ static int vfio_save_complete_precopy(QEMUFile *f,= void *opaque) > return ret; > } > =20 > +static int vfio_load_setup(QEMUFile *f, void *opaque) > +{ > + VFIODevice *vbasedev =3D opaque; > + VFIOMigration *migration =3D vbasedev->migration; > + int ret =3D 0; > + > + if (migration->region.mmaps) { > + ret =3D vfio_region_mmap(&migration->region); > + if (ret) { > + error_report("%s: Failed to mmap VFIO migration region %d: %= s", > + vbasedev->name, migration->region.nr, > + strerror(-ret)); > + return ret; > + } > + } > + > + ret =3D vfio_migration_set_state(vbasedev, ~0, VFIO_DEVICE_STATE_RES= UMING); > + if (ret) { > + error_report("%s: Failed to set state RESUMING", vbasedev->name)= ; > + } > + return ret; > +} > + > +static int vfio_load_cleanup(void *opaque) > +{ > + vfio_save_cleanup(opaque); > + return 0; > +} > + > +static int vfio_load_state(QEMUFile *f, void *opaque, int version_id) > +{ > + VFIODevice *vbasedev =3D opaque; > + VFIOMigration *migration =3D vbasedev->migration; > + int ret =3D 0; > + uint64_t data, data_size; > + > + data =3D qemu_get_be64(f); > + while (data !=3D VFIO_MIG_FLAG_END_OF_STATE) { > + > + trace_vfio_load_state(vbasedev->name, data); > + > + switch (data) { > + case VFIO_MIG_FLAG_DEV_CONFIG_STATE: > + { > + ret =3D vfio_load_device_config_state(f, opaque); > + if (ret) { > + return ret; > + } > + break; > + } > + case VFIO_MIG_FLAG_DEV_SETUP_STATE: > + { > + uint64_t region_size =3D qemu_get_be64(f); > + > + if (migration->region.size < region_size) { > + error_report("%s: SETUP STATE: migration region too smal= l, " > + "0x%"PRIx64 " < 0x%"PRIx64, vbasedev->name, > + migration->region.size, region_size); > + return -EINVAL; > + } > + > + data =3D qemu_get_be64(f); > + if (data =3D=3D VFIO_MIG_FLAG_END_OF_STATE) { Can you explain why you're reading this here rather than letting it drop through to the read at the end of the loop? > + return ret; > + } else { > + error_report("%s: SETUP STATE: EOS not found 0x%"PRIx64, > + vbasedev->name, data); > + return -EINVAL; > + } > + break; > + } > + case VFIO_MIG_FLAG_DEV_DATA_STATE: > + { > + VFIORegion *region =3D &migration->region; > + void *buf =3D NULL; > + bool buffer_mmaped =3D false; > + uint64_t data_offset =3D 0; > + > + data_size =3D qemu_get_be64(f); > + if (data_size =3D=3D 0) { > + break; > + } > + > + ret =3D pread(vbasedev->fd, &data_offset, sizeof(data_offset= ), > + region->fd_offset + > + offsetof(struct vfio_device_migration_info, > + data_offset)); > + if (ret !=3D sizeof(data_offset)) { > + error_report("%s:Failed to get migration buffer data off= set %d", > + vbasedev->name, ret); > + return -EINVAL; > + } > + > + if (region->mmaps) { > + buf =3D find_data_region(region, data_offset, data_size)= ; > + } > + > + buffer_mmaped =3D (buf !=3D NULL) ? true : false; > + > + if (!buffer_mmaped) { > + buf =3D g_try_malloc0(data_size); data_size has been read off the wire at this point; can we sanity check it? > + if (!buf) { > + error_report("%s: Error allocating buffer ", __func_= _); > + return -ENOMEM; > + } > + } > + > + qemu_get_buffer(f, buf, data_size); > + > + if (!buffer_mmaped) { > + ret =3D pwrite(vbasedev->fd, buf, data_size, > + region->fd_offset + data_offset); > + g_free(buf); > + > + if (ret !=3D data_size) { > + error_report("%s: Failed to set migration buffer %d"= , > + vbasedev->name, ret); > + return -EINVAL; > + } > + } > + > + ret =3D pwrite(vbasedev->fd, &data_size, sizeof(data_size), > + region->fd_offset + > + offsetof(struct vfio_device_migration_info, data_= size)); > + if (ret !=3D sizeof(data_size)) { > + error_report("%s: Failed to set migration buffer data si= ze %d", > + vbasedev->name, ret); > + if (!buffer_mmaped) { > + g_free(buf); > + } > + return -EINVAL; > + } > + > + trace_vfio_load_state_device_data(vbasedev->name, data_offse= t, > + data_size); > + break; > + } I'd add here a default: that complains about an unknown tag. > + } > + > + ret =3D qemu_file_get_error(f); > + if (ret) { > + return ret; > + } > + data =3D qemu_get_be64(f); I'd also check file_get_error again at this point; if you're unlucky you get junk in 'data' and things get more confusing. > + } > + > + return ret; > +} > + > static SaveVMHandlers savevm_vfio_handlers =3D { > .save_setup =3D vfio_save_setup, > .save_cleanup =3D vfio_save_cleanup, > .save_live_pending =3D vfio_save_pending, > .save_live_iterate =3D vfio_save_iterate, > .save_live_complete_precopy =3D vfio_save_complete_precopy, > + .load_setup =3D vfio_load_setup, > + .load_cleanup =3D vfio_load_cleanup, > + .load_state =3D vfio_load_state, > }; > =20 > /* ---------------------------------------------------------------------= - */ > diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events > index bdf40ba368c7..ac065b559f4e 100644 > --- a/hw/vfio/trace-events > +++ b/hw/vfio/trace-events > @@ -157,3 +157,6 @@ vfio_save_device_config_state(char *name) " (%s)" > vfio_save_pending(char *name, uint64_t precopy, uint64_t postcopy, uint6= 4_t compatible) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" compatible = 0x%"PRIx64 > vfio_save_iterate(char *name, int data_size) " (%s) data_size %d" > vfio_save_complete_precopy(char *name) " (%s)" > +vfio_load_device_config_state(char *name) " (%s)" > +vfio_load_state(char *name, uint64_t data) " (%s) data 0x%"PRIx64 Please use const char*'s in traces. > +vfio_load_state_device_data(char *name, uint64_t data_offset, uint64_t d= ata_size) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64 > --=20 > 2.7.0 >=20 -- Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK