From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666026; cv=none; d=zoho.com; s=zohoarc; b=dUkMPP1Ilb2ag6Ad0pTSFq1IbFMzUjXur4Wqnf42Ow+RXAHR6JTVjQJA70glALFfw9YiuWLVTLg3cNdxofUIZ60Y4IkdsSXQ3IQIjPh2PtRNdjVX2GdyVUAPhVMDf+vKCXjWkFbXyF5dajhPnsfev5F0kkHgw9btAVYwWwK9Vn8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666026; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=gmFkRUjnMPn0H+3vl9QKit/mJp2xO4oax1Qjt+/ao9s=; b=SUe8JnWjsGihpbzz1HmLeUIWN8Rc10+9N9Y0HrRO0hNrXlqMcudQ2CXo/8Q9U4Cj6FY4CFwG1UUQx4Jq+hPh8KHXAd5fPb3knUE0BcBoQFToqxcpGZBWov9XOf+DDPpTsbUHbC5RkilnTH62nUjQFaP4NXPN/wdNqPeHaZAQfIk= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666026334152.62842287714682; Tue, 9 Jul 2019 02:53:46 -0700 (PDT) Received: from localhost ([::1]:48196 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmod-0004g4-Oh for importer@patchew.org; Tue, 09 Jul 2019 05:53:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49821) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmn6-0003Nj-E5 for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmn4-0003zD-JA for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:04 -0400 Received: from hqemgate16.nvidia.com ([216.228.121.65]:10295) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmn4-0003yW-8m for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:02 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate16.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:51:59 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:52:01 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL108.nvidia.com (172.18.146.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:00 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:51:54 +0000 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Tue, 09 Jul 2019 02:52:01 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:08 +0530 Message-ID: <1562665760-26158-2-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665919; bh=gmFkRUjnMPn0H+3vl9QKit/mJp2xO4oax1Qjt+/ao9s=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=aEhNpJRep4chRMESRUSFfeVM/mGuUFPzIx8WGrnonRroh+/T3E2W95SFlgzhaHRhQ vqAsMNq2YAG6MDHP2d5cEoyJGhsNUaLcKlVacWEPwdW0gmchg8mlWGMXtdZgkSmD/W FtcTHtaCpsmSpmJ5yzJG1790l2T8SlE9bIaBC/RIFKfaqKs2NZCCdcjiqyKnFg1n+8 mGFrWV7NapOm3WRhzpkuH6fb9g9j8e+g+42l5wncBaCXpL2dO7O9zMm/THpZwzDlA4 Mfi2O2Ppi3AT7uWvBmjqzn2C6GMO1hM5YiVPeWCXC1U9wT94SZYduy4pmOQRiR954g bSCikbygXk4xw== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.65 Subject: [Qemu-devel] [PATCH v7 01/13] vfio: KABI for migration interface 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" - Defined MIGRATION region type and sub-type. - Used 3 bits to define VFIO device states. Bit 0 =3D> _RUNNING Bit 1 =3D> _SAVING Bit 2 =3D> _RESUMING Combination of these bits defines VFIO device's state during migration _STOPPED =3D> All bits 0 indicates VFIO device stopped. _RUNNING =3D> Normal VFIO device running state. _SAVING | _RUNNING =3D> vCPUs are running, VFIO device is running but s= tart saving state of device i.e. pre-copy state _SAVING =3D> vCPUs are stoppped, VFIO device should be stopped, and save device state,i.e. stop-n-copy state _RESUMING =3D> VFIO device resuming state. _SAVING | _RESUMING =3D> Invalid state if _SAVING and _RESUMING bits ar= e set - Defined vfio_device_migration_info structure which will be placed at 0th offset of migration region to get/set VFIO device related information. Defined members of structure and usage on read/write access: * device_state: (read/write) To convey VFIO device state to be transitioned to. Only 3 bits are = used as of now. * pending bytes: (read only) To get pending bytes yet to be migrated for VFIO device. * data_offset: (read only) To get data offset in migration from where data exist during _SAVING and from where data should be written by user space application dur= ing _RESUMING state * data_size: (read/write) To get and set size of data copied in migration region during _SAVI= NG and _RESUMING state. * start_pfn, page_size, total_pfns: (write only) To get bitmap of dirty pages from vendor driver from given start address for total_pfns. * copied_pfns: (read only) To get number of pfns bitmap copied in migration region. Vendor driver should copy the bitmap with bits set only for pages to be marked dirty in migration region. Vendor driver should return 0 if there are 0 pages dirty in requested range. Vendor driver should return -1 to mark all pages in the sect= ion as dirty Migration region looks like: ------------------------------------------------------------------ |vfio_device_migration_info| data section | | | /////////////////////////////// | ------------------------------------------------------------------ ^ ^ ^ offset 0-trapped part data_offset data_size Data section is always followed by vfio_device_migration_info structure in the region, so data_offset will always be none-0. Offset from where data is copied is decided by kernel driver, data section can be trapped or mapped depending on how kernel driver defines data section. If mmapped, then data_offset should be page aligned, where as initial section which contain vfio_device_migration_info structure might not end at offset which is page aligned. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- linux-headers/linux/vfio.h | 166 +++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 166 insertions(+) diff --git a/linux-headers/linux/vfio.h b/linux-headers/linux/vfio.h index 24f505199f83..6696a4600545 100644 --- a/linux-headers/linux/vfio.h +++ b/linux-headers/linux/vfio.h @@ -372,6 +372,172 @@ struct vfio_region_gfx_edid { */ #define VFIO_REGION_SUBTYPE_IBM_NVLINK2_ATSD (1) =20 +/* Migration region type and sub-type */ +#define VFIO_REGION_TYPE_MIGRATION (2) +#define VFIO_REGION_SUBTYPE_MIGRATION (1) + +/** + * Structure vfio_device_migration_info is placed at 0th offset of + * VFIO_REGION_SUBTYPE_MIGRATION region to get/set VFIO device related mig= ration + * information. Field accesses from this structure are only supported at t= heir + * native width and alignment, otherwise should return error. + * + * device_state: (read/write) + * To indicate vendor driver the state VFIO device should be transiti= oned + * to. If device state transition fails, write on this field return e= rror. + * It consists of 3 bits: + * - If bit 0 set, indicates _RUNNING state. When its reset, that ind= icates + * _STOPPED state. When device is changed to _STOPPED, driver shoul= d stop + * device before write() returns. + * - If bit 1 set, indicates _SAVING state. + * - If bit 2 set, indicates _RESUMING state. + * _SAVING and _RESUMING set at the same time is invalid state. + * + * pending bytes: (read only) + * Number of pending bytes yet to be migrated from vendor driver + * + * data_offset: (read only) + * User application should read data_offset in migration region from = where + * user application should read device data during _SAVING state or w= rite + * device data during _RESUMING state or read dirty pages bitmap. See= below + * for detail of sequence to be followed. + * + * data_size: (read/write) + * User application should read data_size to get size of data copied = in + * migration region during _SAVING state and write size of data copie= d in + * migration region during _RESUMING state. + * + * start_pfn: (write only) + * Start address pfn to get bitmap of dirty pages from vendor driver = duing + * _SAVING state. + * + * page_size: (write only) + * User application should write the page_size of pfn. + * + * total_pfns: (write only) + * Total pfn count from start_pfn for which dirty bitmap is requested. + * + * copied_pfns: (read only) + * pfn count for which dirty bitmap is copied to migration region. + * Vendor driver should copy the bitmap with bits set only for pages = to be + * marked dirty in migration region. + * - Vendor driver should return VFIO_DEVICE_DIRTY_PFNS_NONE if none = of the + * pages are dirty in requested range or rest of the range. + * - Vendor driver should return VFIO_DEVICE_DIRTY_PFNS_ALL to mark a= ll + * pages dirty in the given section. + * - Vendor driver should return pfn count for which bitmap is writte= n in + * the region. + * + * Migration region looks like: + * ------------------------------------------------------------------ + * |vfio_device_migration_info| data section | + * | | /////////////////////////////// | + * ------------------------------------------------------------------ + * ^ ^ ^ + * offset 0-trapped part data_offset data_size + * + * Data section is always followed by vfio_device_migration_info structure + * in the region, so data_offset will always be none-0. Offset from where = data + * is copied is decided by kernel driver, data section can be trapped or + * mapped depending on how kernel driver defines data section. If mmapped, + * then data_offset should be page aligned, where as initial section which + * contain vfio_device_migration_info structure might not end at offset wh= ich + * is page aligned. + * Data_offset can be same or different for device data and dirty page bit= map. + * Vendor driver should decide whether to partition data section and how to + * partition the data section. Vendor driver should return data_offset + * accordingly. + * + * Sequence to be followed: + * In _SAVING|_RUNNING device state or pre-copy phase: + * a. read pending_bytes. If pending_bytes > 0, go through below steps. + * b. read data_offset, indicates kernel driver to write data to staging b= uffer + * which is mmapped. + * c. read data_size, amount of data in bytes written by vendor driver in + * migration region. + * d. if data section is trapped, read from data_offset of data_size. + * e. if data section is mmaped, read data_size bytes from mmaped buffer f= rom + * data_offset in the migration region. + * f. Write data_size and data to file stream. + * g. iterate through steps a to f while (pending_bytes > 0) + * + * In _SAVING device state or stop-and-copy phase: + * a. read config space of device and save to migration file stream. This + * doesn't need to be from vendor driver. Any other special config state + * from driver can be saved as data in following iteration. + * b. read pending_bytes. + * c. read data_offset, indicates kernel driver to write data to staging + * buffer which is mmapped. + * d. read data_size, amount of data in bytes written by vendor driver in + * migration region. + * e. if data section is trapped, read from data_offset of data_size. + * f. if data section is mmaped, read data_size bytes from mmaped buffer f= rom + * data_offset in the migration region. + * g. Write data_size and data to file stream + * h. iterate through steps b to g while (pending_bytes > 0) + * + * When data region is mapped, its user's responsibility to read data from + * data_offset of data_size before moving to next steps. + * + * Dirty page tracking is part of RAM copy state, where vendor driver + * provides the bitmap of pages which are dirtied by vendor driver through + * migration region and as part of RAM copy those pages gets copied to file + * stream. + * + * To get dirty page bitmap: + * a. write start_pfn, page_size and total_pfns. + * b. read copied_pfns. + * - Vendor driver should return VFIO_DEVICE_DIRTY_PFNS_NONE if driver + * doesn't have any page to report dirty in given range or rest of t= he + * range. Exit loop. + * - Vendor driver should return VFIO_DEVICE_DIRTY_PFNS_ALL to mark all + * pages dirty for given range. Mark all pages in the range as dirty= and + * exit the loop. + * - Vendor driver should return copied_pfns and provide bitmap for + * copied_pfn, which means that bitmap copied for given range contai= ns + * information for all pages where some bits are 0s and some are 1s. + * c. read data_offset, where vendor driver has written bitmap. + * d. read bitmap from the region or mmaped part of the region. + * e. Iterate through steps a to d while (total copied_pfns < total_pfns) + * + * In _RESUMING device state: + * - Load device config state. + * - While end of data for this device is not reached, repeat below steps: + * - read data_size from file stream, read data from file stream of + * data_size. + * - read data_offset from where User application should write data. + * if region is mmaped, write data of data_size to mmaped region. + * - write data_size. + * In case of mmapped region, write on data_size indicates kernel + * driver that data is written in staging buffer. + * - if region is trapped, write data of data_size from data_offset. + * + * For user application, data is opaque. User should write data in the same + * order as received. + */ + +struct vfio_device_migration_info { + __u32 device_state; /* VFIO device state */ +#define VFIO_DEVICE_STATE_RUNNING (1 << 0) +#define VFIO_DEVICE_STATE_SAVING (1 << 1) +#define VFIO_DEVICE_STATE_RESUMING (1 << 2) +#define VFIO_DEVICE_STATE_MASK (VFIO_DEVICE_STATE_RUNNING | \ + VFIO_DEVICE_STATE_SAVING | \ + VFIO_DEVICE_STATE_RESUMING) +#define VFIO_DEVICE_STATE_INVALID (VFIO_DEVICE_STATE_SAVING | \ + VFIO_DEVICE_STATE_RESUMING) + __u32 reserved; + __u64 pending_bytes; + __u64 data_offset; + __u64 data_size; + __u64 start_pfn; + __u64 page_size; + __u64 total_pfns; + __u64 copied_pfns; +#define VFIO_DEVICE_DIRTY_PFNS_NONE (0) +#define VFIO_DEVICE_DIRTY_PFNS_ALL (~0ULL) +} __attribute__((packed)); + /* * The MSIX mappable capability informs that MSIX data of a BAR can be mma= pped * which allows direct access to non-MSIX registers which happened to be w= ithin --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666031; cv=none; d=zoho.com; s=zohoarc; b=PP66QyNOpWssp4LoM6ppHJ4zrMgu99lMXRQjkpZURlVNAK8yqxXPtERiS6mj0C4ALa8Bzmvl/tqrDvqNXb0H4juad4kAIz/v5KExG6WpTh8ttFU3caPhPL3uj6ko5T0BsuMb4NqnNfqBPD3I7mrurZhDwLNH8Yxh0UiPtwojrhA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666031; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=nylc0cVMKEEnjbH8m1Te0YQIduVRuoNb4byY+ts2y18=; b=cfeRcIKUE/snfxoesEkdKedxr6ODz9U8MJxNfoIGa2xLgfecxHrlG3PUfhaHS0XQVG9GOaq41FPeGhASt8ZNpPo4NoR83cARDuaxRzVCtSeOw1atR5rKI+iHud+vWsfynm4keB+aNOcZvlREfqHuc7eCeFa+4DQnrB20/H0boYc= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666031233120.77397855981019; Tue, 9 Jul 2019 02:53:51 -0700 (PDT) Received: from localhost ([::1]:48198 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmoo-00050r-AO for importer@patchew.org; Tue, 09 Jul 2019 05:53:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49852) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmnC-0003X8-J6 for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmnB-00041q-Gh for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:10 -0400 Received: from hqemgate15.nvidia.com ([216.228.121.64]:2499) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmnB-00041F-8U for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:09 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate15.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:52:12 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:52:07 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL104.nvidia.com (172.18.146.11) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:07 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:52:01 +0000 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Tue, 09 Jul 2019 02:52:07 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:09 +0530 Message-ID: <1562665760-26158-3-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665932; bh=nylc0cVMKEEnjbH8m1Te0YQIduVRuoNb4byY+ts2y18=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=bQP5q9QY7bpzOZGOx3ITvWdd9F/uPeh1Tj0PE4h2ivmhPcFi4LaIjUSAIbDrHLpxa ezQGwoMaRAqg6IMW8e3eqPyxkE5PGD/fAL25o4pTLuXosSXMAZjNWo+7iIhx32Obdn PHLXpjfkGFQHi3PZkaMXL1zqWiFIz9JgIvJmDfYj0H27ldwIiOmsof8oh2z000TN7g Chg45hzqvjvQqkw4m8VjPopagcmQO+DmisFE4qwh60BN41L8oE1KsY9KXTxniFVspr HCZtkUyk9p8maHDtaJtrXh7nquwbhcxvQ4hXK51PHeMWDaTEsaA3c+ExEVIcAJ2MZQ 6or5kYulFfmpQ== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.64 Subject: [Qemu-devel] [PATCH v7 02/13] vfio: Add function to unmap VFIO region 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This function is used in follwing patch in this series. Migration region is mmaped when migration starts and will be unmapped when migration is complete. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia Reviewed-by: Cornelia Huck --- hw/vfio/common.c | 20 ++++++++++++++++++++ hw/vfio/trace-events | 1 + include/hw/vfio/vfio-common.h | 1 + 3 files changed, 22 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index a859298fdad9..de74dae8d6a6 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -964,6 +964,26 @@ int vfio_region_mmap(VFIORegion *region) return 0; } =20 +void vfio_region_unmap(VFIORegion *region) +{ + int i; + + if (!region->mem) { + return; + } + + for (i =3D 0; i < region->nr_mmaps; i++) { + trace_vfio_region_unmap(memory_region_name(®ion->mmaps[i].mem), + region->mmaps[i].offset, + region->mmaps[i].offset + + region->mmaps[i].size - 1); + memory_region_del_subregion(region->mem, ®ion->mmaps[i].mem); + munmap(region->mmaps[i].mmap, region->mmaps[i].size); + object_unparent(OBJECT(®ion->mmaps[i].mem)); + region->mmaps[i].mmap =3D NULL; + } +} + void vfio_region_exit(VFIORegion *region) { int i; diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index b1ef55a33ffd..8cdc27946cb8 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -111,6 +111,7 @@ vfio_region_mmap(const char *name, unsigned long offset= , unsigned long end) "Reg vfio_region_exit(const char *name, int index) "Device %s, region %d" vfio_region_finalize(const char *name, int index) "Device %s, region %d" vfio_region_mmaps_set_enabled(const char *name, bool enabled) "Region %s m= maps enabled: %d" +vfio_region_unmap(const char *name, unsigned long offset, unsigned long en= d) "Region %s unmap [0x%lx - 0x%lx]" vfio_region_sparse_mmap_header(const char *name, int index, int nr_areas) = "Device %s region %d: %d sparse mmap entries" vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long en= d) "sparse entry %d [0x%lx - 0x%lx]" vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t s= ubtype) "%s index %d, %08x/%0x8" diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 9107bd41c030..93493891ba40 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -171,6 +171,7 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev= , VFIORegion *region, int index, const char *name); int vfio_region_mmap(VFIORegion *region); void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled); +void vfio_region_unmap(VFIORegion *region); void vfio_region_exit(VFIORegion *region); void vfio_region_finalize(VFIORegion *region); void vfio_reset_handler(void *opaque); --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666184; cv=none; d=zoho.com; s=zohoarc; b=Yn3xiIXH+7pjvH1ZDBFLnyfvxqzBzqpGHTogng724CLZwY29qzA8eWpUjH38pQs/ELvJEM5A21JmOEWpe/ve9vZafivz90BGcojbR5S67WsEgwp56zlnb/3TKaqhdVgg9u/d5+q6bjd6yGN6CxTiaQavG0EsuiUcFd8E9iR/VsE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666184; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=PSRLy6S6kyHPuidTbjM/3kb9fZJQ4RoIRSGUoB0Hfcs=; b=C2HAAywhN8AmVXCdv37SRgCAccxxgCZKoDS1sCefNGYyy6p/iG9AFnGDgqP96lJ69ZX/xXQQ6UKSAS7BsU3tGzvcLhN4Wqb/5pz2qBhqf2Vu+IMRmNvHt1ppFiA0F3sk5H7eaWKvJCr6CtwQutXvmlUWIVDX1LKTf2IvnGycFXs= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666184885607.7662609659623; Tue, 9 Jul 2019 02:56:24 -0700 (PDT) Received: from localhost ([::1]:48234 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmrE-000860-OD for importer@patchew.org; Tue, 09 Jul 2019 05:56:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49880) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmnI-0003je-KQ for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmnH-00044M-MN for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:16 -0400 Received: from hqemgate16.nvidia.com ([216.228.121.65]:10308) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmnH-000441-Er for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:15 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate16.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:52:13 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:52:14 -0700 Received: from HQMAIL109.nvidia.com (172.20.187.15) by HQMAIL105.nvidia.com (172.20.187.12) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:13 +0000 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL109.nvidia.com (172.20.187.15) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:13 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:52:07 +0000 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Tue, 09 Jul 2019 02:52:14 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:10 +0530 Message-ID: <1562665760-26158-4-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665933; bh=PSRLy6S6kyHPuidTbjM/3kb9fZJQ4RoIRSGUoB0Hfcs=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=JEoGVd97X3kaRC1M1ROxRsYJ55Zfk8AZhJtRmy/BpdQMDPldQLX2xbZP7tOjBWrJ4 0iTObC7w34Uw8AN4xyomIHkUVNz2+P5EHeb8TJxtJ443zucJL5mO4WF+uNeVNzzNd4 o3N7cRPSISnIlnzfcxD3ucs+VonHolemwXAsgxTJnDlQ8bQCHdkUHDwXbQdwitTtpN PUlj6E0Pho6Zr+V7yvXvQc/8dnk1h67w0Z1/sShG7GJlHiIQWr+ESPXwVcP8KSypH4 bFSjhWy+Q8UBdQoCQUq46F3j7G5zrUyzGuoWahIiRSIejNmPCpK7cBtspRMInUVdU/ l9sRf+5i5KtpQ== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.65 Subject: [Qemu-devel] [PATCH v7 03/13] vfio: Add vfio_get_object callback to VFIODeviceOps 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hook vfio_get_object callback for PCI devices. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia Suggested-by: Cornelia Huck Reviewed-by: Cornelia Huck --- hw/vfio/pci.c | 8 ++++++++ include/hw/vfio/vfio-common.h | 1 + 2 files changed, 9 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index d7a4e1875c05..de0d286fc9dd 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2388,10 +2388,18 @@ static void vfio_pci_compute_needs_reset(VFIODevice= *vbasedev) } } =20 +static Object *vfio_pci_get_object(VFIODevice *vbasedev) +{ + VFIOPCIDevice *vdev =3D container_of(vbasedev, VFIOPCIDevice, vbasedev= ); + + return OBJECT(vdev); +} + static VFIODeviceOps vfio_pci_ops =3D { .vfio_compute_needs_reset =3D vfio_pci_compute_needs_reset, .vfio_hot_reset_multi =3D vfio_pci_hot_reset_multi, .vfio_eoi =3D vfio_intx_eoi, + .vfio_get_object =3D vfio_pci_get_object, }; =20 int vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 93493891ba40..771b6d59a3db 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -119,6 +119,7 @@ struct VFIODeviceOps { void (*vfio_compute_needs_reset)(VFIODevice *vdev); int (*vfio_hot_reset_multi)(VFIODevice *vdev); void (*vfio_eoi)(VFIODevice *vdev); + Object *(*vfio_get_object)(VFIODevice *vdev); }; =20 typedef struct VFIOGroup { --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666406; cv=none; d=zoho.com; s=zohoarc; b=kkIrRFtHmxpa2v50Tzeqoe11JBxtNIju1MuIUZ8lX6K495Hh/GtbwyixRyFcJLy3UYm/+1UJWbi8MBbJrGKcRtacS0P3EHL3qt+LQNVF7dRpTkShIN6KPdIIipC9CrJLX/7kYaGfwjEfasU5fCB90adRoLCYoIkl2gB4NPsWzko= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666406; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=KxCg7RYsF9WCTMR9Vl2FXGp8ITverR3cbhaOp1v7Yf0=; b=LZCpdiPsmfDDbbh78o3sMD6yzAjmyaNPSHVtwDRrzwIT+T3EbV0tSY6EB5+TmpDOU7KYnAmzvUl8IV1vRNaGt/KoLLiifSaERtfSAlSYpv/R5Y2WziMPi7oUC5T7XoOnv3m7vknQ9woBqJZGJpAf8BehFy5AlPCFmc/d9Et5oPo= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15626664060531016.1170641883126; Tue, 9 Jul 2019 03:00:06 -0700 (PDT) Received: from localhost ([::1]:48264 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmuq-0003Hc-K1 for importer@patchew.org; Tue, 09 Jul 2019 06:00:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49911) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmnP-00041T-SU for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmnO-00047V-HP for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:23 -0400 Received: from hqemgate14.nvidia.com ([216.228.121.143]:7169) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmnO-000475-9R for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:22 -0400 Received: from hqpgpgate102.nvidia.com (Not Verified[216.228.121.13]) by hqemgate14.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:52:16 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate102.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:52:20 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL105.nvidia.com (172.20.187.12) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:20 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:52:13 +0000 X-PGP-Universal: processed; by hqpgpgate102.nvidia.com on Tue, 09 Jul 2019 02:52:20 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:11 +0530 Message-ID: <1562665760-26158-5-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665936; bh=KxCg7RYsF9WCTMR9Vl2FXGp8ITverR3cbhaOp1v7Yf0=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=Fdl+AzljAZI3zA2oaWVvR7nVL4xd153A6VAGKWsev+Qi6irD7DotSat4Vn/7e+Tm9 jvk0YwLdWJBElNDxZalc/64tsJtJUyFLbDhDIN599IvhvnkEz24P9jm6Pb2XNz9c/0 CPiZdKmobn1q7iv1eA3u8P1PT0mhqOUT11Bg71mkiFPkQbT5idYqsQa+bZRDEbnM3p JIycW8bPXoIHAUre4i7uNSmMIu/lxXRHguUm7kxZ/fQhgwrShVgtKvuZNe4b3dm88j vwy/4h/xrLbPcsLOxmQud47nPT7aXKdOEzoUXBGYiuwzunbbDj+xQaoH2TtBkMwX2L bKxhRzqnhlIig== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.143 Subject: [Qemu-devel] [PATCH v7 04/13] vfio: Add save and load functions for VFIO PCI devices 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" These functions save and restore PCI device specific data - config space of PCI device. Tested save and restore with MSI and MSIX type. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/pci.c | 114 ++++++++++++++++++++++++++++++++++++++= ++++ include/hw/vfio/vfio-common.h | 2 + 2 files changed, 116 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index de0d286fc9dd..5fe4f8076cac 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2395,11 +2395,125 @@ static Object *vfio_pci_get_object(VFIODevice *vba= sedev) return OBJECT(vdev); } =20 +static void vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f) +{ + VFIOPCIDevice *vdev =3D container_of(vbasedev, VFIOPCIDevice, vbasedev= ); + PCIDevice *pdev =3D &vdev->pdev; + uint16_t pci_cmd; + int i; + + for (i =3D 0; i < PCI_ROM_SLOT; i++) { + uint32_t bar; + + bar =3D pci_default_read_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, = 4); + qemu_put_be32(f, bar); + } + + qemu_put_be32(f, vdev->interrupt); + if (vdev->interrupt =3D=3D VFIO_INT_MSI) { + uint32_t msi_flags, msi_addr_lo, msi_addr_hi =3D 0, msi_data; + bool msi_64bit; + + msi_flags =3D pci_default_read_config(pdev, pdev->msi_cap + PCI_MS= I_FLAGS, + 2); + msi_64bit =3D (msi_flags & PCI_MSI_FLAGS_64BIT); + + msi_addr_lo =3D pci_default_read_config(pdev, + pdev->msi_cap + PCI_MSI_ADDRESS_L= O, 4); + qemu_put_be32(f, msi_addr_lo); + + if (msi_64bit) { + msi_addr_hi =3D pci_default_read_config(pdev, + pdev->msi_cap + PCI_MSI_ADDRE= SS_HI, + 4); + } + qemu_put_be32(f, msi_addr_hi); + + msi_data =3D pci_default_read_config(pdev, + pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 : PCI_MSI_DAT= A_32), + 2); + qemu_put_be32(f, msi_data); + } else if (vdev->interrupt =3D=3D VFIO_INT_MSIX) { + uint16_t offset; + + /* save enable bit and maskall bit */ + offset =3D pci_default_read_config(pdev, + pdev->msix_cap + PCI_MSIX_FLAGS + 1= , 2); + qemu_put_be16(f, offset); + msix_save(pdev, f); + } + pci_cmd =3D pci_default_read_config(pdev, PCI_COMMAND, 2); + qemu_put_be16(f, pci_cmd); +} + +static void vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f) +{ + VFIOPCIDevice *vdev =3D container_of(vbasedev, VFIOPCIDevice, vbasedev= ); + PCIDevice *pdev =3D &vdev->pdev; + uint32_t interrupt_type; + uint32_t msi_flags, msi_addr_lo, msi_addr_hi =3D 0, msi_data; + uint16_t pci_cmd; + bool msi_64bit; + int i; + + /* retore pci bar configuration */ + pci_cmd =3D pci_default_read_config(pdev, PCI_COMMAND, 2); + vfio_pci_write_config(pdev, PCI_COMMAND, + pci_cmd & (!(PCI_COMMAND_IO | PCI_COMMAND_MEMORY))= , 2); + for (i =3D 0; i < PCI_ROM_SLOT; i++) { + uint32_t bar =3D qemu_get_be32(f); + + vfio_pci_write_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, bar, 4); + } + vfio_pci_write_config(pdev, PCI_COMMAND, + pci_cmd | PCI_COMMAND_IO | PCI_COMMAND_MEMORY, 2= ); + + interrupt_type =3D qemu_get_be32(f); + + if (interrupt_type =3D=3D VFIO_INT_MSI) { + /* restore msi configuration */ + msi_flags =3D pci_default_read_config(pdev, + pdev->msi_cap + PCI_MSI_FLAGS,= 2); + msi_64bit =3D (msi_flags & PCI_MSI_FLAGS_64BIT); + + vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS, + msi_flags & (!PCI_MSI_FLAGS_ENABLE), 2); + + msi_addr_lo =3D qemu_get_be32(f); + vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_LO, + msi_addr_lo, 4); + + msi_addr_hi =3D qemu_get_be32(f); + if (msi_64bit) { + vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI, + msi_addr_hi, 4); + } + msi_data =3D qemu_get_be32(f); + vfio_pci_write_config(pdev, + pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 : PCI_MSI_DAT= A_32), + msi_data, 2); + + vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS, + msi_flags | PCI_MSI_FLAGS_ENABLE, 2); + } else if (interrupt_type =3D=3D VFIO_INT_MSIX) { + uint16_t offset =3D qemu_get_be16(f); + + /* load enable bit and maskall bit */ + vfio_pci_write_config(pdev, pdev->msix_cap + PCI_MSIX_FLAGS + 1, + offset, 2); + msix_load(pdev, f); + } + pci_cmd =3D qemu_get_be16(f); + vfio_pci_write_config(pdev, PCI_COMMAND, pci_cmd, 2); +} + static VFIODeviceOps vfio_pci_ops =3D { .vfio_compute_needs_reset =3D vfio_pci_compute_needs_reset, .vfio_hot_reset_multi =3D vfio_pci_hot_reset_multi, .vfio_eoi =3D vfio_intx_eoi, .vfio_get_object =3D vfio_pci_get_object, + .vfio_save_config =3D vfio_pci_save_config, + .vfio_load_config =3D vfio_pci_load_config, }; =20 int vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 771b6d59a3db..ee72bd984a36 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -120,6 +120,8 @@ struct VFIODeviceOps { int (*vfio_hot_reset_multi)(VFIODevice *vdev); void (*vfio_eoi)(VFIODevice *vdev); Object *(*vfio_get_object)(VFIODevice *vdev); + void (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f); + void (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); }; =20 typedef struct VFIOGroup { --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666117; cv=none; d=zoho.com; s=zohoarc; b=AlR3VqCTHVtlXlXD1+an+XsO/e3PPXI10biACYKOsJXIi1Rlmcm5qbxKlFnBMKo7m7OUYCpp0fjX2HyodJ/fyXwBw7S8ZRkG56cR16iQ70LLMYlomT2MsIm+VT+JmmJft7wGHVez/bqEi2AYYFP/JyTRq1zpmwJUDt7MvQzjrbE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666117; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=GAcrL9sBGC5c9BmokU8vkZghd0B7Dg122zj5OUDX0Dg=; b=V3197HHJkCnX7oLFGu8Kew8JmagVtRbxewOAtpu1vNzM5b3VgBXv6kw8JUQ2G6lzRLgFdVcacyukOTvxUBgDXKItY73vQ/tpqVmKKv4OhDWbYjueV3Nd1WlJx4GaP+QI6BK8NqMJdoykDuqh1lzjoDipx5i2+FDwL704Pwl1LKE= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666117277370.78905575845135; Tue, 9 Jul 2019 02:55:17 -0700 (PDT) Received: from localhost ([::1]:48228 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmqC-0007SV-7v for importer@patchew.org; Tue, 09 Jul 2019 05:55:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49965) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmnY-0004FS-5m for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmnU-0004Bb-Od for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:31 -0400 Received: from hqemgate15.nvidia.com ([216.228.121.64]:2523) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmnU-0004BB-GA for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:28 -0400 Received: from hqpgpgate102.nvidia.com (Not Verified[216.228.121.13]) by hqemgate15.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:52:32 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate102.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:52:27 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL106.nvidia.com (172.18.146.12) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:27 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:52:20 +0000 X-PGP-Universal: processed; by hqpgpgate102.nvidia.com on Tue, 09 Jul 2019 02:52:27 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:12 +0530 Message-ID: <1562665760-26158-6-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665952; bh=GAcrL9sBGC5c9BmokU8vkZghd0B7Dg122zj5OUDX0Dg=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=FRTxHfJVbuuLOj/8gN6M7kIthumEFe6JmRu0OK4CkEKtFagkVUP8ZtExShfh4IGB+ VwE6qL6/1QrDyaCir+iLbt4Mr9A4R/vYOEJQbzvp4o42C72T4DyKlU0t8VyWMd5L4j iLocsHgyp7nO8p0vU6UDAQA8y+6EyMe/EXtKTQ0wdoznhEFu4tmGcYr5o6X0xuJBL1 vMqq/4pQx9F+0IaQqj8VDzty//9LDe11oHmjBxVhi4jp1gCq6uYQD2UIFPffCMkVrO 4fTvN2YcsJkWidHK1Evs5rpR1hQSwBzy3pbIibPJIIu2+hf1Egcr9mJjuG5/3k9+kA LrccLFj6cK7WA== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.64 Subject: [Qemu-devel] [PATCH v7 05/13] vfio: Add migration region initialization and finalize function 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" - Migration functions are implemented for VFIO_DEVICE_TYPE_PCI device in th= is patch series. - VFIO device supports migration or not is decided based of migration region query. If migration region query is successful and migration region initialization is successful then migration is supported else migration is blocked. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/Makefile.objs | 2 +- hw/vfio/migration.c | 145 ++++++++++++++++++++++++++++++++++++++= ++++ hw/vfio/trace-events | 3 + include/hw/vfio/vfio-common.h | 14 ++++ 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 hw/vfio/migration.c diff --git a/hw/vfio/Makefile.objs b/hw/vfio/Makefile.objs index abad8b818c9b..36033d1437c5 100644 --- a/hw/vfio/Makefile.objs +++ b/hw/vfio/Makefile.objs @@ -1,4 +1,4 @@ -obj-y +=3D common.o spapr.o +obj-y +=3D common.o spapr.o migration.o obj-$(CONFIG_VFIO_PCI) +=3D pci.o pci-quirks.o display.o obj-$(CONFIG_VFIO_CCW) +=3D ccw.o obj-$(CONFIG_VFIO_PLATFORM) +=3D platform.o diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c new file mode 100644 index 000000000000..a2cfbd5af2e1 --- /dev/null +++ b/hw/vfio/migration.c @@ -0,0 +1,145 @@ +/* + * Migration support for VFIO devices + * + * Copyright NVIDIA, Inc. 2019 + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include + +#include "hw/vfio/vfio-common.h" +#include "cpu.h" +#include "migration/migration.h" +#include "migration/qemu-file.h" +#include "migration/register.h" +#include "migration/blocker.h" +#include "migration/misc.h" +#include "qapi/error.h" +#include "exec/ramlist.h" +#include "exec/ram_addr.h" +#include "pci.h" +#include "trace.h" + +static void vfio_migration_region_exit(VFIODevice *vbasedev) +{ + VFIOMigration *migration =3D vbasedev->migration; + + if (!migration) { + return; + } + + if (migration->region.buffer.size) { + vfio_region_exit(&migration->region.buffer); + vfio_region_finalize(&migration->region.buffer); + } +} + +static int vfio_migration_region_init(VFIODevice *vbasedev) +{ + VFIOMigration *migration =3D vbasedev->migration; + Object *obj =3D NULL; + int ret =3D -EINVAL; + + if (!migration) { + return ret; + } + + if (!vbasedev->ops || !vbasedev->ops->vfio_get_object) { + return ret; + } + + obj =3D vbasedev->ops->vfio_get_object(vbasedev); + if (!obj) { + return ret; + } + + ret =3D vfio_region_setup(obj, vbasedev, &migration->region.buffer, + migration->region.index, "migration"); + if (ret) { + error_report("%s: Failed to setup VFIO migration region %d: %s", + vbasedev->name, migration->region.index, strerror(-re= t)); + goto err; + } + + if (!migration->region.buffer.size) { + ret =3D -EINVAL; + error_report("%s: Invalid region size of VFIO migration region %d:= %s", + vbasedev->name, migration->region.index, strerror(-re= t)); + goto err; + } + + return 0; + +err: + vfio_migration_region_exit(vbasedev); + return ret; +} + +static int vfio_migration_init(VFIODevice *vbasedev, + struct vfio_region_info *info) +{ + int ret; + + vbasedev->migration =3D g_new0(VFIOMigration, 1); + vbasedev->migration->region.index =3D info->index; + + ret =3D vfio_migration_region_init(vbasedev); + if (ret) { + error_report("%s: Failed to initialise migration region", + vbasedev->name); + return ret; + } + + return 0; +} + +/* ---------------------------------------------------------------------- = */ + +int vfio_migration_probe(VFIODevice *vbasedev, Error **errp) +{ + struct vfio_region_info *info; + Error *local_err =3D NULL; + int ret; + + ret =3D vfio_get_dev_region_info(vbasedev, VFIO_REGION_TYPE_MIGRATION, + VFIO_REGION_SUBTYPE_MIGRATION, &info); + if (ret) { + goto add_blocker; + } + + ret =3D vfio_migration_init(vbasedev, info); + if (ret) { + goto add_blocker; + } + + trace_vfio_migration_probe(vbasedev->name, info->index); + return 0; + +add_blocker: + error_setg(&vbasedev->migration_blocker, + "VFIO device doesn't support migration"); + ret =3D migrate_add_blocker(vbasedev->migration_blocker, &local_err); + if (local_err) { + error_propagate(errp, local_err); + error_free(vbasedev->migration_blocker); + } + return ret; +} + +void vfio_migration_finalize(VFIODevice *vbasedev) +{ + if (!vbasedev->migration) { + return; + } + + if (vbasedev->migration_blocker) { + migrate_del_blocker(vbasedev->migration_blocker); + error_free(vbasedev->migration_blocker); + } + + vfio_migration_region_exit(vbasedev); + g_free(vbasedev->migration); +} diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 8cdc27946cb8..191a726a1312 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -143,3 +143,6 @@ vfio_display_edid_link_up(void) "" vfio_display_edid_link_down(void) "" vfio_display_edid_update(uint32_t prefx, uint32_t prefy) "%ux%u" vfio_display_edid_write_error(void) "" + +# migration.c +vfio_migration_probe(char *name, uint32_t index) " (%s) Region %d" diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index ee72bd984a36..152da3f8d6f3 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -57,6 +57,15 @@ typedef struct VFIORegion { uint8_t nr; /* cache the region number for debug */ } VFIORegion; =20 +typedef struct VFIOMigration { + struct { + VFIORegion buffer; + uint32_t index; + } region; + uint64_t pending_bytes; + QemuMutex lock; +} VFIOMigration; + typedef struct VFIOAddressSpace { AddressSpace *as; QLIST_HEAD(, VFIOContainer) containers; @@ -113,6 +122,8 @@ typedef struct VFIODevice { unsigned int num_irqs; unsigned int num_regions; unsigned int flags; + VFIOMigration *migration; + Error *migration_blocker; } VFIODevice; =20 struct VFIODeviceOps { @@ -204,4 +215,7 @@ int vfio_spapr_create_window(VFIOContainer *container, int vfio_spapr_remove_window(VFIOContainer *container, hwaddr offset_within_address_space); =20 +int vfio_migration_probe(VFIODevice *vbasedev, Error **errp); +void vfio_migration_finalize(VFIODevice *vbasedev); + #endif /* HW_VFIO_VFIO_COMMON_H */ --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666798; cv=none; d=zoho.com; s=zohoarc; b=QOMqWZ6wEU43J4+6MweLEnbnp/9Xn0zx/S4GdELPJcK5Yi7Lv5cyrsw2y+sV0FjrskZH/Tl3eqGKavpi1phxbZ42P3BqWDb9FPea5qU6xbAWtgSbZVIxOW/PIhyFipDDc1ODRR/3yZG2IuThVYwRxhkK+PntSeDQ6ayofijrTa8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666798; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=CzIjuh45DV8zioWi2bRTMYHoiTulp8p5NKtpMhF1Pz8=; b=PzJcgvgZgO1mwNBiOnE8LVng5VGpOWz+fUnp+OExW6l5GvmZBkcWdnPuryl2hp2DBNw7DDSd3k62BSTTp8VC4f1QxltqFIUPjPnVx83PHvoGKE+Gx/TgDcwdeikfnwvR8bEE0XoYjOpSesF9fjv9e+8e3IUROhj/Flh3lEnWBPc= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666798698569.6139613338737; Tue, 9 Jul 2019 03:06:38 -0700 (PDT) Received: from localhost ([::1]:48316 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkn19-0008EA-D2 for importer@patchew.org; Tue, 09 Jul 2019 06:06:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50010) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmnf-0004KL-QC for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmne-0004FS-Jl for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:39 -0400 Received: from hqemgate15.nvidia.com ([216.228.121.64]:2535) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmnc-0004Dd-Lo for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:37 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate15.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:52:38 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:52:34 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL106.nvidia.com (172.18.146.12) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:33 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:52:27 +0000 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Tue, 09 Jul 2019 02:52:34 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:13 +0530 Message-ID: <1562665760-26158-7-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665959; bh=CzIjuh45DV8zioWi2bRTMYHoiTulp8p5NKtpMhF1Pz8=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=gIihGRvOKcEraFkCuUdl+z+GEYwrGVrurc6N87dLRQfdtWJp5nMwUmXlLzUHwvEe6 dpj/srGFtI88MoNw1mWAcDWnRJIpHENfHESYdHm2vwoRuTGPTenOhPByHMhpdJOb82 BCPOgvThceCfM7Hnl+6Q7oOdK2iZXencpaKOG86UPmgH0l94XeAsr6i+2QHpqb6m6W 3gU+YGzIomGPcgeaPP0no/jcjnePnWfugkK7z62kd/ao9ydacTvp8Bw1k8ee2mz6yi VOv+2aSqOzoJ5cy9M4VKSqCEhz6PdL1QzMkohiPi6CkHq5TcIE/5A4GSeEVL1hZtBN nPrbCG44iI22A== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.64 Subject: [Qemu-devel] [PATCH v7 06/13] vfio: Add VM state change handler to know state of VM 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" VM state change handler gets called on change in VM's state. This is used t= o set VFIO device state to _RUNNING. VM state change handler, migration state change handler and log_sync listen= er are called asynchronously, which sometimes lead to data corruption in migra= tion region. Initialised mutex that is used to serialize operations on migration= data region during saving state. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/migration.c | 64 +++++++++++++++++++++++++++++++++++++++= ++++ hw/vfio/trace-events | 2 ++ include/hw/vfio/vfio-common.h | 4 +++ 3 files changed, 70 insertions(+) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index a2cfbd5af2e1..c01f08b659d0 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -78,6 +78,60 @@ err: return ret; } =20 +static int vfio_migration_set_state(VFIODevice *vbasedev, uint32_t state) +{ + VFIOMigration *migration =3D vbasedev->migration; + VFIORegion *region =3D &migration->region.buffer; + uint32_t device_state; + int ret =3D 0; + + device_state =3D (state & VFIO_DEVICE_STATE_MASK) | + (vbasedev->device_state & ~VFIO_DEVICE_STATE_MASK); + + if ((device_state & VFIO_DEVICE_STATE_MASK) =3D=3D VFIO_DEVICE_STATE_I= NVALID) { + return -EINVAL; + } + + ret =3D pwrite(vbasedev->fd, &device_state, sizeof(device_state), + region->fd_offset + offsetof(struct vfio_device_migration= _info, + device_state)); + if (ret < 0) { + error_report("%s: Failed to set device state %d %s", + vbasedev->name, ret, strerror(errno)); + return ret; + } + + vbasedev->device_state =3D device_state; + trace_vfio_migration_set_state(vbasedev->name, device_state); + return 0; +} + +static void vfio_vmstate_change(void *opaque, int running, RunState state) +{ + VFIODevice *vbasedev =3D opaque; + + if ((vbasedev->vm_running !=3D running)) { + int ret; + uint32_t dev_state; + + if (running) { + dev_state =3D VFIO_DEVICE_STATE_RUNNING; + } else { + dev_state =3D (vbasedev->device_state & VFIO_DEVICE_STATE_MASK= ) & + ~VFIO_DEVICE_STATE_RUNNING; + } + + ret =3D vfio_migration_set_state(vbasedev, dev_state); + if (ret) { + error_report("%s: Failed to set device state 0x%x", + vbasedev->name, dev_state); + } + vbasedev->vm_running =3D running; + trace_vfio_vmstate_change(vbasedev->name, running, RunState_str(st= ate), + dev_state); + } +} + static int vfio_migration_init(VFIODevice *vbasedev, struct vfio_region_info *info) { @@ -93,6 +147,11 @@ static int vfio_migration_init(VFIODevice *vbasedev, return ret; } =20 + qemu_mutex_init(&vbasedev->migration->lock); + + vbasedev->vm_state =3D qemu_add_vm_change_state_handler(vfio_vmstate_c= hange, + vbasedev); + return 0; } =20 @@ -135,11 +194,16 @@ void vfio_migration_finalize(VFIODevice *vbasedev) return; } =20 + if (vbasedev->vm_state) { + qemu_del_vm_change_state_handler(vbasedev->vm_state); + } + if (vbasedev->migration_blocker) { migrate_del_blocker(vbasedev->migration_blocker); error_free(vbasedev->migration_blocker); } =20 + qemu_mutex_destroy(&vbasedev->migration->lock); vfio_migration_region_exit(vbasedev); g_free(vbasedev->migration); } diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 191a726a1312..3d15bacd031a 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -146,3 +146,5 @@ vfio_display_edid_write_error(void) "" =20 # migration.c vfio_migration_probe(char *name, uint32_t index) " (%s) Region %d" +vfio_migration_set_state(char *name, uint32_t state) " (%s) state %d" +vfio_vmstate_change(char *name, int running, const char *reason, uint32_t = dev_state) " (%s) running %d reason %s device state %d" diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 152da3f8d6f3..f6c70db3a9c1 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -29,6 +29,7 @@ #ifdef CONFIG_LINUX #include #endif +#include "sysemu/sysemu.h" =20 #define VFIO_MSG_PREFIX "vfio %s: " =20 @@ -124,6 +125,9 @@ typedef struct VFIODevice { unsigned int flags; VFIOMigration *migration; Error *migration_blocker; + uint32_t device_state; + VMChangeStateEntry *vm_state; + int vm_running; } VFIODevice; =20 struct VFIODeviceOps { --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666072; cv=none; d=zoho.com; s=zohoarc; b=SpRnTpDm5DoNu9EC0pLGCs4/uD6Ukrehk3PGMMBlf0Kq2EucyQmjnAodx76Zljqv+JepXPa4e2RsYq90mwDgb5tY2hX8DhIkBA5uNT8HwhZS3LXjx2TWxTdGbV40AJKdNgqGSxZNHVi/YnOmTbAH2vH5IcCtg4138OyAu5PylY4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666072; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=SI3JRw38FBSF2Z2uCSJm8vd8dZhbA/5dfd1kkJAiy8A=; b=PTQg9T1EmcPXOZsuvxhaImrfxbJOuI69FQrjIU5BQ2ZLksQKf0x24WIIe53LW2pUQt7vGLUlB/NrfuTMXz3zee0AnSICtu9vajzKQKldw+Yy508XUmxPww6+/X4tSsEwHNO+T5cURnNAevdINtr4dG/c9CyIuhaKZLIhO+psCCc= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666072837229.51913900402985; Tue, 9 Jul 2019 02:54:32 -0700 (PDT) Received: from localhost ([::1]:48206 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmpT-00069N-Qq for importer@patchew.org; Tue, 09 Jul 2019 05:54:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50033) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmnk-0004Ps-7C for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmnj-0004Ha-0B for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:44 -0400 Received: from hqemgate16.nvidia.com ([216.228.121.65]:10328) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmni-0004Gl-OO for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:42 -0400 Received: from hqpgpgate102.nvidia.com (Not Verified[216.228.121.13]) by hqemgate16.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:52:39 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate102.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:52:40 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:40 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:52:34 +0000 X-PGP-Universal: processed; by hqpgpgate102.nvidia.com on Tue, 09 Jul 2019 02:52:40 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:14 +0530 Message-ID: <1562665760-26158-8-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665959; bh=SI3JRw38FBSF2Z2uCSJm8vd8dZhbA/5dfd1kkJAiy8A=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=YoAMa9vyHzXbANdWnqUJSJV2A8dfhQfG1Cwci/HvPS7eD+BVvD8nZIs4Qdt0HxvKh c5T7ntEkTj0dsQk1MsqBXgbXvTku+tduUMGYsPX/CkvbnroAL+tyzleaRcnXNWNTB0 DFXjmXP8gghiXZySNtRExRTFpdoOSRslU7baoJwLiMvee29CpNi03tRc8l2QSoNDKW LqKsMNrsKHx5ip2qausoBYGkA8pcPj55dJ/gG+GXhMukjI6cHlUSwV2RmZLqDF3JYc va+r/OF6jmYDHCw2Vl1sh7EXhpsIQ1O2mXzs0ZScfe/ytxYs2F7YhXYFRZIXMA3zFh n09aGm6gwxhBQ== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.65 Subject: [Qemu-devel] [PATCH v7 07/13] vfio: Add migration state change notifier 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Added migration state change notifier to get notification on migration state change. These states are translated to VFIO device state and conveyed to ve= ndor driver. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/migration.c | 54 +++++++++++++++++++++++++++++++++++++++= ++++ hw/vfio/trace-events | 1 + include/hw/vfio/vfio-common.h | 1 + 3 files changed, 56 insertions(+) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index c01f08b659d0..e4a89a6f9bc7 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -132,6 +132,53 @@ static void vfio_vmstate_change(void *opaque, int runn= ing, RunState state) } } =20 +static void vfio_migration_state_notifier(Notifier *notifier, void *data) +{ + MigrationState *s =3D data; + VFIODevice *vbasedev =3D container_of(notifier, VFIODevice, migration_= state); + int ret; + + trace_vfio_migration_state_notifier(vbasedev->name, s->state); + + switch (s->state) { + case MIGRATION_STATUS_ACTIVE: + if (vbasedev->device_state & VFIO_DEVICE_STATE_RUNNING) { + if (vbasedev->vm_running) { + ret =3D vfio_migration_set_state(vbasedev, + VFIO_DEVICE_STATE_RUNNING | VFIO_DEVICE_STATE_SA= VING); + if (ret) { + error_report("%s: Failed to set state RUNNING and SAVI= NG", + vbasedev->name); + } + } else { + ret =3D vfio_migration_set_state(vbasedev, + VFIO_DEVICE_STATE_SAVING); + if (ret) { + error_report("%s: Failed to set state STOP and SAVING", + vbasedev->name); + } + } + } else { + ret =3D vfio_migration_set_state(vbasedev, + VFIO_DEVICE_STATE_RESUMING); + if (ret) { + error_report("%s: Failed to set state RESUMING", + vbasedev->name); + } + } + return; + + case MIGRATION_STATUS_CANCELLING: + case MIGRATION_STATUS_CANCELLED: + case MIGRATION_STATUS_FAILED: + ret =3D vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RUNNI= NG); + if (ret) { + error_report("%s: Failed to set state RUNNING", vbasedev->name= ); + } + return; + } +} + static int vfio_migration_init(VFIODevice *vbasedev, struct vfio_region_info *info) { @@ -152,6 +199,9 @@ static int vfio_migration_init(VFIODevice *vbasedev, vbasedev->vm_state =3D qemu_add_vm_change_state_handler(vfio_vmstate_c= hange, vbasedev); =20 + vbasedev->migration_state.notify =3D vfio_migration_state_notifier; + add_migration_state_change_notifier(&vbasedev->migration_state); + return 0; } =20 @@ -194,6 +244,10 @@ void vfio_migration_finalize(VFIODevice *vbasedev) return; } =20 + if (vbasedev->migration_state.notify) { + remove_migration_state_change_notifier(&vbasedev->migration_state); + } + if (vbasedev->vm_state) { qemu_del_vm_change_state_handler(vbasedev->vm_state); } diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 3d15bacd031a..69503228f20e 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -148,3 +148,4 @@ vfio_display_edid_write_error(void) "" vfio_migration_probe(char *name, uint32_t index) " (%s) Region %d" vfio_migration_set_state(char *name, uint32_t state) " (%s) state %d" vfio_vmstate_change(char *name, int running, const char *reason, uint32_t = dev_state) " (%s) running %d reason %s device state %d" +vfio_migration_state_notifier(char *name, int state) " (%s) state %d" diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index f6c70db3a9c1..a022484d2636 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -128,6 +128,7 @@ typedef struct VFIODevice { uint32_t device_state; VMChangeStateEntry *vm_state; int vm_running; + Notifier migration_state; } VFIODevice; =20 struct VFIODeviceOps { --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666363; cv=none; d=zoho.com; s=zohoarc; b=Tn4fZVDm9ivsDgfpYjTJADY2H0o3G9TW+fiFlLjpaoxVYZkoNPUb6nIB8mbKHUw6uvuqVFKWaWngIiy1WG8zqUa1kJ7EhXbszdOEqBL9kcFyze9jOjtLWxhO0rnrsSNW7nv6P+1uE2GAreZX/sIyp3M/YxdDgE1MBubJdDuND3w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666363; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=8vUPg3KmhoNDzrGG08rPjtEGsYW1X/3yWpzwzoAlpYU=; b=iGUWwI40rGTkola4S9qOb/cbDLP49fF7FOZDtO9/VGrdn1Or7ZtnbYtd5uJJJ1A4STLS6pYmhhD/quLKJawGk75tv8uHtA02V/7uvNycx8LePWBeUhCI0oafuEaJ2sM8k0/svPXxBAsdtyotScD6qyZJsvw9DBMfiaHJ/uExXGY= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666363944659.8598454031636; Tue, 9 Jul 2019 02:59:23 -0700 (PDT) Received: from localhost ([::1]:48252 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmu8-0002gn-Tt for importer@patchew.org; Tue, 09 Jul 2019 05:59:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50067) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmnt-0004bb-IN for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmnp-0004OJ-Rg for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:51 -0400 Received: from hqemgate14.nvidia.com ([216.228.121.143]:7188) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmno-0004Jd-9V for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:49 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate14.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:52:42 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:52:46 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:46 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:52:40 +0000 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Tue, 09 Jul 2019 02:52:46 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:15 +0530 Message-ID: <1562665760-26158-9-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665962; bh=8vUPg3KmhoNDzrGG08rPjtEGsYW1X/3yWpzwzoAlpYU=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=ipOfz+9+sePnvfBXM23A5xPOHNk40Y6dOPZYc+5fCzbjvkuEfTE7nRCHR6mUNr1jN 2eUoh2cM1pK1/EF8uNN3IbIsEPPgjvdsMc2MHMuPDPzJj7lf0p39meHL6NHk4yvZod 0x29plqcSjE4A/96wh3XMGzhRWG7ydTNRVZj13QbkSLTxdrpIIEZuhpqq0ImdPuhsf tY+yuPRIdtYHzko5W0PzKv/u3sIGLr+/VLtuYmUO/R1o3EQPqYthS/LGZBfnJyZnqw cXZWnaYJaZsAiu8GVjZ0/n7aJW8A2gpZVczLfpmH13yZ/3IfcDVvDjGwsyFKv6kjD3 tatR+2s/riamA== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.143 Subject: [Qemu-devel] [PATCH v7 08/13] vfio: Register SaveVMHandlers for VFIO device 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Define flags to be used as delimeter in migration file stream. Added .save_setup and .save_cleanup functions. Mapped & unmapped migration region from these functions at source during saving or pre-copy phase. Set VFIO device state depending on VM's state. During live migration, VM is running when .save_setup is called, _SAVING | _RUNNING state is set for VFIO device. During save-restore, VM is paused, _SAVING state is set for VFIO de= vice. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/migration.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++= +++- hw/vfio/trace-events | 2 ++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index e4a89a6f9bc7..0597a45fda2d 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -23,6 +23,17 @@ #include "pci.h" #include "trace.h" =20 +/* + * Flags used as delimiter: + * 0xffffffff =3D> MSB 32-bit all 1s + * 0xef10 =3D> emulated (virtual) function IO + * 0x0000 =3D> 16-bits reserved for flags + */ +#define VFIO_MIG_FLAG_END_OF_STATE (0xffffffffef100001ULL) +#define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL) +#define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL) +#define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL) + static void vfio_migration_region_exit(VFIODevice *vbasedev) { VFIOMigration *migration =3D vbasedev->migration; @@ -106,6 +117,74 @@ static int vfio_migration_set_state(VFIODevice *vbased= ev, uint32_t state) return 0; } =20 +/* ---------------------------------------------------------------------- = */ + +static int vfio_save_setup(QEMUFile *f, void *opaque) +{ + VFIODevice *vbasedev =3D opaque; + VFIOMigration *migration =3D vbasedev->migration; + int ret; + + qemu_put_be64(f, VFIO_MIG_FLAG_DEV_SETUP_STATE); + + if (migration->region.buffer.mmaps) { + qemu_mutex_lock_iothread(); + ret =3D vfio_region_mmap(&migration->region.buffer); + qemu_mutex_unlock_iothread(); + if (ret) { + error_report("%s: Failed to mmap VFIO migration region %d: %s", + vbasedev->name, migration->region.index, + strerror(-ret)); + return ret; + } + } + + if (vbasedev->vm_running) { + ret =3D vfio_migration_set_state(vbasedev, + VFIO_DEVICE_STATE_RUNNING | VFIO_DEVICE_STATE_SAV= ING); + if (ret) { + error_report("%s: Failed to set state RUNNING and SAVING", + vbasedev->name); + return ret; + } + } else { + ret =3D vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_SAVIN= G); + if (ret) { + error_report("%s: Failed to set state STOP and SAVING", + vbasedev->name); + return ret; + } + } + + qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); + + ret =3D qemu_file_get_error(f); + if (ret) { + return ret; + } + + trace_vfio_save_setup(vbasedev->name); + return 0; +} + +static void vfio_save_cleanup(void *opaque) +{ + VFIODevice *vbasedev =3D opaque; + VFIOMigration *migration =3D vbasedev->migration; + + if (migration->region.buffer.mmaps) { + vfio_region_unmap(&migration->region.buffer); + } + trace_vfio_save_cleanup(vbasedev->name); +} + +static SaveVMHandlers savevm_vfio_handlers =3D { + .save_setup =3D vfio_save_setup, + .save_cleanup =3D vfio_save_cleanup, +}; + +/* ---------------------------------------------------------------------- = */ + static void vfio_vmstate_change(void *opaque, int running, RunState state) { VFIODevice *vbasedev =3D opaque; @@ -195,7 +274,8 @@ static int vfio_migration_init(VFIODevice *vbasedev, } =20 qemu_mutex_init(&vbasedev->migration->lock); - + register_savevm_live(vbasedev->dev, "vfio", -1, 1, &savevm_vfio_handle= rs, + vbasedev); vbasedev->vm_state =3D qemu_add_vm_change_state_handler(vfio_vmstate_c= hange, vbasedev); =20 diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 69503228f20e..4bb43f18f315 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -149,3 +149,5 @@ vfio_migration_probe(char *name, uint32_t index) " (%s)= Region %d" vfio_migration_set_state(char *name, uint32_t state) " (%s) state %d" vfio_vmstate_change(char *name, int running, const char *reason, uint32_t = dev_state) " (%s) running %d reason %s device state %d" vfio_migration_state_notifier(char *name, int state) " (%s) state %d" +vfio_save_setup(char *name) " (%s)" +vfio_save_cleanup(char *name) " (%s)" --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666276; cv=none; d=zoho.com; s=zohoarc; b=VWBi8NjPiNGhCxW5Uog4DY1R7txbaAkGRzLU6jctrhmk5wo3drOu8t5VMPe89XePT0G0Sb7TO27wTRYhbrV59/GIM3266LS446S9Lgo2L91x1Qkr0hxt4CoLdEw6KDax6mJhYe/uw2cFik62aSTf4phKQfSTevGrEajcE6/0EOY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666276; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=KbMb3xZKD0ac3z4LqHBRa+TwyVc6TDVgzTspInsGsPo=; b=mtuiAx3WmrdKaOCtEirQ5Q2/hoM/DzDqe5im5jjOag7QsPc5PiLcNXglYpwnsiZNrjhXvJ0pZ7mM15P5EsufSKiMBRUN5yprxhkNMAlADzFPC4zpN6qbnDKVeslt1DRST3StO4cvokOc+3j7vmysqVwP0R/Ly8J45X1Q838BIaA= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666276450779.6088110273038; Tue, 9 Jul 2019 02:57:56 -0700 (PDT) Received: from localhost ([::1]:48242 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmsl-0001K9-FR for importer@patchew.org; Tue, 09 Jul 2019 05:57:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50108) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmnz-0004em-69 for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmnx-0004Vp-8u for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:59 -0400 Received: from hqemgate14.nvidia.com ([216.228.121.143]:7206) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmnv-0004Uu-Fe for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:52:56 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate14.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:52:49 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:52:53 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL105.nvidia.com (172.20.187.12) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:53 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:52:46 +0000 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Tue, 09 Jul 2019 02:52:53 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:16 +0530 Message-ID: <1562665760-26158-10-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665969; bh=KbMb3xZKD0ac3z4LqHBRa+TwyVc6TDVgzTspInsGsPo=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=Rfiycn3NMEgQz7HloiRiZOYLpQi99hbLmzBTcRWeQGzyG0ZzqgN5ZWDSBY7HeknY1 WhRD1ASFZzS4p7hA55kQsVqynT1Tnu0kpc3PG+H6IW7SWZlP3oVT7xMG+QW1yXrDnk xFWzc0VVzVD6Utc7B1KWAVLsR+o9DVu0bcnJvdc2r+0UaYU37aNIFWlPti9Gln5vZ2 vM9w+VDfn/zGnqG2zbxAycfVxfEndF/xAV7YyPj0KbX8Z8ub4O4gCRGoGns1PC4u0o rnhHudyt2vUmT5XgEvjAmxTjQA9kpt+In3sNfkfuPjHkTM0GJSCdxMJaFbuld7Hv4F Jyzwk1JCXvqOg== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.143 Subject: [Qemu-devel] [PATCH v7 09/13] vfio: Add save state functions to SaveVMHandlers 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Added .save_live_pending, .save_live_iterate and .save_live_complete_precopy functions. These functions handles pre-copy and stop-and-copy phase. In _SAVING|_RUNNING device state or pre-copy phase: - read pending_bytes - read data_offset - indicates kernel driver to write data to staging buffer which is mmapped. - read data_size - amount of data in bytes written by vendor driver in migr= ation region. - if data section is trapped, pread() from data_offset of data_size. - if data section is mmaped, read mmaped buffer of data_size. - Write data packet to file stream as below: {VFIO_MIG_FLAG_DEV_DATA_STATE, data_size, actual data, VFIO_MIG_FLAG_END_OF_STATE } In _SAVING device state or stop-and-copy phase a. read config space of device and save to migration file stream. This doesn't need to be from vendor driver. Any other special config state from driver can be saved as data in following iteration. b. read pending_bytes c. read data_offset - indicates kernel driver to write data to staging buffer which is mmapped. d. read data_size - amount of data in bytes written by vendor driver in migration region. e. if data section is trapped, pread() from data_offset of data_size. f. if data section is mmaped, read mmaped buffer of data_size. g. Write data packet as below: {VFIO_MIG_FLAG_DEV_DATA_STATE, data_size, actual data} h. iterate through steps b to g while (pending_bytes > 0) i. Write {VFIO_MIG_FLAG_END_OF_STATE} When data region is mapped, its user's responsibility to read data from data_offset of data_size before moving to next steps. .save_live_iterate runs outside the iothread lock in the migration case, wh= ich could race with asynchronous call to get dirty page list causing data corru= ption in mapped migration region. Mutex added here to serial migration buffer read operation. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/migration.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ hw/vfio/trace-events | 6 ++ 2 files changed, 252 insertions(+) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index 0597a45fda2d..4e9b4cce230b 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -117,6 +117,138 @@ static int vfio_migration_set_state(VFIODevice *vbase= dev, uint32_t state) return 0; } =20 +static void *find_data_region(VFIORegion *region, + uint64_t data_offset, + uint64_t data_size) +{ + void *ptr =3D NULL; + int i; + + for (i =3D 0; i < region->nr_mmaps; i++) { + if ((data_offset >=3D region->mmaps[i].offset) && + (data_offset < region->mmaps[i].offset + region->mmaps[i].size= ) && + (data_size <=3D region->mmaps[i].size)) { + ptr =3D region->mmaps[i].mmap + (data_offset - + region->mmaps[i].offset); + break; + } + } + return ptr; +} + +static int vfio_save_buffer(QEMUFile *f, VFIODevice *vbasedev) +{ + VFIOMigration *migration =3D vbasedev->migration; + VFIORegion *region =3D &migration->region.buffer; + uint64_t data_offset =3D 0, data_size =3D 0; + int ret; + + 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 offset %d", + vbasedev->name, ret); + return -EINVAL; + } + + ret =3D pread(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 get migration buffer data size %d", + vbasedev->name, ret); + return -EINVAL; + } + + if (data_size > 0) { + void *buf =3D NULL; + bool buffer_mmaped; + + 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); + if (!buf) { + error_report("%s: Error allocating buffer ", __func__); + return -ENOMEM; + } + + ret =3D pread(vbasedev->fd, buf, data_size, + region->fd_offset + data_offset); + if (ret !=3D data_size) { + error_report("%s: Failed to get migration data %d", + vbasedev->name, ret); + g_free(buf); + return -EINVAL; + } + } + + qemu_put_be64(f, data_size); + qemu_put_buffer(f, buf, data_size); + + if (!buffer_mmaped) { + g_free(buf); + } + migration->pending_bytes -=3D data_size; + } else { + qemu_put_be64(f, data_size); + } + + trace_vfio_save_buffer(vbasedev->name, data_offset, data_size, + migration->pending_bytes); + + ret =3D qemu_file_get_error(f); + if (ret) { + return ret; + } + + return data_size; +} + +static int vfio_update_pending(VFIODevice *vbasedev) +{ + VFIOMigration *migration =3D vbasedev->migration; + VFIORegion *region =3D &migration->region.buffer; + uint64_t pending_bytes =3D 0; + int ret; + + ret =3D pread(vbasedev->fd, &pending_bytes, sizeof(pending_bytes), + region->fd_offset + offsetof(struct vfio_device_migration_= info, + pending_bytes)); + if ((ret < 0) || (ret !=3D sizeof(pending_bytes))) { + error_report("%s: Failed to get pending bytes %d", + vbasedev->name, ret); + migration->pending_bytes =3D 0; + return (ret < 0) ? ret : -EINVAL; + } + + migration->pending_bytes =3D pending_bytes; + trace_vfio_update_pending(vbasedev->name, pending_bytes); + return 0; +} + +static int vfio_save_device_config_state(QEMUFile *f, void *opaque) +{ + VFIODevice *vbasedev =3D opaque; + + qemu_put_be64(f, VFIO_MIG_FLAG_DEV_CONFIG_STATE); + + if (vbasedev->ops && vbasedev->ops->vfio_save_config) { + vbasedev->ops->vfio_save_config(vbasedev, f); + } + + qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); + + trace_vfio_save_device_config_state(vbasedev->name); + + return qemu_file_get_error(f); +} + /* ---------------------------------------------------------------------- = */ =20 static int vfio_save_setup(QEMUFile *f, void *opaque) @@ -178,9 +310,123 @@ static void vfio_save_cleanup(void *opaque) trace_vfio_save_cleanup(vbasedev->name); } =20 +static void vfio_save_pending(QEMUFile *f, void *opaque, + uint64_t threshold_size, + uint64_t *res_precopy_only, + uint64_t *res_compatible, + uint64_t *res_postcopy_only) +{ + VFIODevice *vbasedev =3D opaque; + VFIOMigration *migration =3D vbasedev->migration; + int ret; + + ret =3D vfio_update_pending(vbasedev); + if (ret) { + return; + } + + *res_precopy_only +=3D migration->pending_bytes; + + trace_vfio_save_pending(vbasedev->name, *res_precopy_only, + *res_postcopy_only, *res_compatible); +} + +static int vfio_save_iterate(QEMUFile *f, void *opaque) +{ + VFIODevice *vbasedev =3D opaque; + VFIOMigration *migration =3D vbasedev->migration; + int ret, data_size; + + qemu_put_be64(f, VFIO_MIG_FLAG_DEV_DATA_STATE); + + qemu_mutex_lock(&migration->lock); + data_size =3D vfio_save_buffer(f, vbasedev); + qemu_mutex_unlock(&migration->lock); + + if (data_size < 0) { + error_report("%s: vfio_save_buffer failed %s", vbasedev->name, + strerror(errno)); + return data_size; + } + + qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); + + ret =3D qemu_file_get_error(f); + if (ret) { + return ret; + } + + trace_vfio_save_iterate(vbasedev->name, data_size); + if (data_size =3D=3D 0) { + /* indicates data finished, goto complete phase */ + return 1; + } + + return 0; +} + +static int vfio_save_complete_precopy(QEMUFile *f, void *opaque) +{ + VFIODevice *vbasedev =3D opaque; + VFIOMigration *migration =3D vbasedev->migration; + int ret; + + ret =3D vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_SAVING); + if (ret) { + error_report("%s: Failed to set state STOP and SAVING", + vbasedev->name); + return ret; + } + + ret =3D vfio_save_device_config_state(f, opaque); + if (ret) { + return ret; + } + + ret =3D vfio_update_pending(vbasedev); + if (ret) { + return ret; + } + + while (migration->pending_bytes > 0) { + qemu_put_be64(f, VFIO_MIG_FLAG_DEV_DATA_STATE); + ret =3D vfio_save_buffer(f, vbasedev); + if (ret < 0) { + error_report("%s: Failed to save buffer", vbasedev->name); + return ret; + } else if (ret =3D=3D 0) { + break; + } + + ret =3D vfio_update_pending(vbasedev); + if (ret) { + return ret; + } + } + + qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); + + ret =3D qemu_file_get_error(f); + if (ret) { + return ret; + } + + ret =3D vfio_migration_set_state(vbasedev, ~VFIO_DEVICE_STATE_MASK); + if (ret) { + error_report("%s: Failed to set state STOPPED", vbasedev->name); + return ret; + } + + trace_vfio_save_complete_precopy(vbasedev->name); + 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, }; =20 /* ---------------------------------------------------------------------- = */ diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 4bb43f18f315..bdf40ba368c7 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -151,3 +151,9 @@ vfio_vmstate_change(char *name, int running, const char= *reason, uint32_t dev_st vfio_migration_state_notifier(char *name, int state) " (%s) state %d" vfio_save_setup(char *name) " (%s)" vfio_save_cleanup(char *name) " (%s)" +vfio_save_buffer(char *name, uint64_t data_offset, uint64_t data_size, uin= t64_t pending) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64" pending 0x%"PRIx64 +vfio_update_pending(char *name, uint64_t pending) " (%s) pending 0x%"PRIx64 +vfio_save_device_config_state(char *name) " (%s)" +vfio_save_pending(char *name, uint64_t precopy, uint64_t postcopy, uint64_= 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)" --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666927; cv=none; d=zoho.com; s=zohoarc; b=FGFDTYCswsnfmXidHXDHt6Ye8l4/hg6UQOoW6gRt0jwjgaIIndWIPZSpzGt+kKyT5W0BSLPMmAuocj99RRjqTO+QIg8vcjObDSN6PC3DxQg7Pu5RHBfGpvn7fVd2PjIzMWnjoeFvz1f40Bkq7OQl046j2hwsHLIG8hgjiZGa4hg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666927; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=bcewDAGUZ5tausKSh+MNRWKeewginREXF7zmdFcpZQc=; b=WGtvwxNo1ep9hPHh8Vy1EXgRP7RDb+2mV0Hr6e08x4rvMmMTo3LfZIOUYqK0hHDOHYUkvF8gyKFTII7pyN99mgUcUvIAz4qxQWFKvnC+B3pZAd9j+Ox4PkVMRlNCK9gjC15wE3j+SvW13RtoWNfr0ctxUUB0EolisylpN0SfxAM= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 156266692708078.40482208813228; Tue, 9 Jul 2019 03:08:47 -0700 (PDT) Received: from localhost ([::1]:48342 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkn3D-0002Z6-QA for importer@patchew.org; Tue, 09 Jul 2019 06:08:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50129) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmo6-0004nx-G7 for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmo5-0004Yp-49 for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:06 -0400 Received: from hqemgate14.nvidia.com ([216.228.121.143]:7218) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmo3-0004XJ-92 for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:04 -0400 Received: from hqpgpgate102.nvidia.com (Not Verified[216.228.121.13]) by hqemgate14.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:52:55 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate102.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:53:00 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:52:59 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:52:53 +0000 X-PGP-Universal: processed; by hqpgpgate102.nvidia.com on Tue, 09 Jul 2019 02:53:00 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:17 +0530 Message-ID: <1562665760-26158-11-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665976; bh=bcewDAGUZ5tausKSh+MNRWKeewginREXF7zmdFcpZQc=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=qNIIVQ5Np0aylcmmQrR5fejHLAQbeeaUuJMzA+mnKjzEtmyyyUbPldEIw9GQx2b2P dnQK9ATL2dntB/49V9QI9tYjSy1j9LEJr6t0nrcbUDA6KbHYK9O4clXyXYUS6UpRDf PNJtOZG89B9IOKCWXKx+9asp8pb4UcTpVLmJQbjgvUJ6IITCC5LzWMOCJc2NQ6n4cI fMMXeASXlXUkrrKR4SNohViQlhrM6b7ddV9NWsQ8chjejgm6hB5mdhAiY70g9UoXOA a7iy6S8RVieVTR1GcjLS/Q+kiaKWD3FXwz6wj2SaHIWWt+ixcnsLgKw9k6U8c4q++d KcV85aDQR1atg== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.143 Subject: [Qemu-devel] [PATCH v7 10/13] vfio: Add load state functions to SaveVMHandlers 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Flow during _RESUMING device state: - If Vendor driver defines mappable region, mmap migration region. - Load config state. - For data packet, till VFIO_MIG_FLAG_END_OF_STATE is not reached - read data_size from packet, read buffer of data_size - read data_offset from where QEMU should write data. if region is mmaped, write data of data_size to mmaped region. - write data_size. In case of mmapped region, write to data_size indicates kernel driver that data is written in staging buffer. - if region is trapped, pwrite() data of data_size from data_offset. - Repeat above until VFIO_MIG_FLAG_END_OF_STATE. - Unmap migration region. For user, data is opaque. User should write data in the same order as received. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/migration.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++= ++++ hw/vfio/trace-events | 3 + 2 files changed, 165 insertions(+) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index 4e9b4cce230b..5fb4c5329ede 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -249,6 +249,26 @@ 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) { + vbasedev->ops->vfio_load_config(vbasedev, f); + } + + 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, data); + 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) @@ -421,12 +441,154 @@ static int vfio_save_complete_precopy(QEMUFile *f, v= oid *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.buffer.mmaps) { + ret =3D vfio_region_mmap(&migration->region.buffer); + if (ret) { + error_report("%s: Failed to mmap VFIO migration region %d: %s", + vbasedev->name, migration->region.index, + strerror(-ret)); + return ret; + } + } + + ret =3D vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING); + 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: + { + data =3D qemu_get_be64(f); + if (data =3D=3D VFIO_MIG_FLAG_END_OF_STATE) { + 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.buffer; + 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 offse= t %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); + if (!buf) { + error_report("%s: Error allocating buffer ", __func__); + return -ENOMEM; + } + } + + qemu_get_buffer(f, buf, data_size); + + ret =3D pwrite(vbasedev->fd, &data_size, sizeof(data_size), + region->fd_offset + + offsetof(struct vfio_device_migration_info, data_si= ze)); + if (ret !=3D sizeof(data_size)) { + error_report("%s: Failed to set migration buffer data size= %d", + vbasedev->name, ret); + if (!buffer_mmaped) { + g_free(buf); + } + return -EINVAL; + } + + 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; + } + } + trace_vfio_load_state_device_data(vbasedev->name, data_offset, + data_size); + break; + } + } + + ret =3D qemu_file_get_error(f); + if (ret) { + return ret; + } + data =3D qemu_get_be64(f); + } + + 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, uint64_= 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 +vfio_load_state_device_data(char *name, uint64_t data_offset, uint64_t dat= a_size) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64 --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666707; cv=none; d=zoho.com; s=zohoarc; b=GpJhedsW9g1LqE9ooH5FoFJtBMxpIYkdi708j9aoXadFGShaTfFafAaEKqy9qXc8IP6d6G/7jJ7bOKXPXn7mAQSH10xNEEuOJ5440j2jAgRTLKfYcP/vhvwdMuDlQN7KZ0Bfe2kXwjhiWcym8DOwTs5E5FVP7+m6M+kJezVHRtE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666707; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=25UseSr4xzk281vNhyKMRVl21nV76q6z6xVAUf2Y8Sw=; b=Rmu+mfVK29csUA7uaTbkumNHDMSkklNnJFV/LfxXizLCJccj7LuFDTokVJFDae1aG5NftIG8KyFcjkV2OGgQDh1P7PfTrdKCuh25YFmOIY4aOOwD4yPb5E7q18JCZuLZJhbE6dgQGdk1yzUjkFoNd5BXCr77ygwjPE5bOTke7JA= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 156266670773617.15923330115129; Tue, 9 Jul 2019 03:05:07 -0700 (PDT) Received: from localhost ([::1]:48302 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmze-0006VK-4V for importer@patchew.org; Tue, 09 Jul 2019 06:05:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50155) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmo9-0004wI-Jp for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmo8-0004Zw-7l for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:09 -0400 Received: from hqemgate16.nvidia.com ([216.228.121.65]:10360) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmo7-0004ZS-RX for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:08 -0400 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate16.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:53:05 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:53:06 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL106.nvidia.com (172.18.146.12) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:53:06 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:53:00 +0000 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Tue, 09 Jul 2019 02:53:06 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:18 +0530 Message-ID: <1562665760-26158-12-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665985; bh=25UseSr4xzk281vNhyKMRVl21nV76q6z6xVAUf2Y8Sw=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=hI1ZlSlQyUJbs06UBquSvg0KNGKAmtWTPo/BlsF+K1nYCFFsYMa7LqsMwfLNjT3/C 7haK49fChCdTAMEuhxYq719sMSMbBYkVLsxmCmZLHRAzhEtnpePzEzAmyY8L6MdRju gPl3elygfDMDuh4Y9MbHV1miUwCydWL4a97Ponu8Xm88MZ74gf1Yl7ppK/EFXvCU5c YO3YG0HqGmv8k2LFFgoeY4ePcy9+m+7oJkmh+qdDNdJKb6o24rPSA417IBa+QcydQe CxT7g2To5aSngDLBQJ2Th9Fd/l/xRBvCBBpTZZhfcFsA4wJn3SWmbZ1pmG/HR746BI KBC5T9VIDTWbQ== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.65 Subject: [Qemu-devel] [PATCH v7 11/13] vfio: Add function to get dirty page list 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Dirty page tracking (.log_sync) is part of RAM copying state, where vendor driver provides the bitmap of pages which are dirtied by vendor driver through migration region and as part of RAM copy, those pages gets copied to file stream. To get dirty page bitmap: - write start address, page_size and pfn count. - read count of pfns copied. - Vendor driver should return 0 if driver doesn't have any page to report dirty in given range. - Vendor driver should return -1 to mark all pages dirty for given rang= e. - read data_offset, where vendor driver has written bitmap. - read bitmap from the region or mmaped part of the region. - Iterate above steps till page bitmap for all requested pfns are copied. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/migration.c | 123 ++++++++++++++++++++++++++++++++++++++= ++++ hw/vfio/trace-events | 1 + include/hw/vfio/vfio-common.h | 2 + 3 files changed, 126 insertions(+) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index 5fb4c5329ede..ca1a8c0f5f1f 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -269,6 +269,129 @@ static int vfio_load_device_config_state(QEMUFile *f,= void *opaque) return qemu_file_get_error(f); } =20 +void vfio_get_dirty_page_list(VFIODevice *vbasedev, + uint64_t start_pfn, + uint64_t pfn_count, + uint64_t page_size) +{ + VFIOMigration *migration =3D vbasedev->migration; + VFIORegion *region =3D &migration->region.buffer; + uint64_t count =3D 0; + int64_t copied_pfns =3D 0; + int64_t total_pfns =3D pfn_count; + int ret; + + qemu_mutex_lock(&migration->lock); + + while (total_pfns > 0) { + uint64_t bitmap_size, data_offset =3D 0; + uint64_t start =3D start_pfn + count; + void *buf =3D NULL; + bool buffer_mmaped =3D false; + + ret =3D pwrite(vbasedev->fd, &start, sizeof(start), + region->fd_offset + offsetof(struct vfio_device_migration= _info, + start_pfn)); + if (ret < 0) { + error_report("%s: Failed to set dirty pages start address %d %= s", + vbasedev->name, ret, strerror(errno)); + goto dpl_unlock; + } + + ret =3D pwrite(vbasedev->fd, &page_size, sizeof(page_size), + region->fd_offset + offsetof(struct vfio_device_migration= _info, + page_size)); + if (ret < 0) { + error_report("%s: Failed to set dirty page size %d %s", + vbasedev->name, ret, strerror(errno)); + goto dpl_unlock; + } + + ret =3D pwrite(vbasedev->fd, &total_pfns, sizeof(total_pfns), + region->fd_offset + offsetof(struct vfio_device_migration= _info, + total_pfns)); + if (ret < 0) { + error_report("%s: Failed to set dirty page total pfns %d %s", + vbasedev->name, ret, strerror(errno)); + goto dpl_unlock; + } + + /* Read copied dirty pfns */ + ret =3D pread(vbasedev->fd, &copied_pfns, sizeof(copied_pfns), + region->fd_offset + offsetof(struct vfio_device_migration_= info, + copied_pfns)); + if (ret < 0) { + error_report("%s: Failed to get dirty pages bitmap count %d %s= ", + vbasedev->name, ret, strerror(errno)); + goto dpl_unlock; + } + + if (copied_pfns =3D=3D VFIO_DEVICE_DIRTY_PFNS_NONE) { + /* + * copied_pfns could be 0 if driver doesn't have any page to + * report dirty in given range + */ + break; + } else if (copied_pfns =3D=3D VFIO_DEVICE_DIRTY_PFNS_ALL) { + /* Mark all pages dirty for this range */ + cpu_physical_memory_set_dirty_range(start_pfn * page_size, + pfn_count * page_size, + DIRTY_MEMORY_MIGRATION); + break; + } + + bitmap_size =3D (BITS_TO_LONGS(copied_pfns) + 1) * sizeof(unsigned= long); + + 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 offset %= d", + vbasedev->name, ret); + goto dpl_unlock; + } + + if (region->mmaps) { + buf =3D find_data_region(region, data_offset, bitmap_size); + } + + buffer_mmaped =3D (buf !=3D NULL) ? true : false; + + if (!buffer_mmaped) { + buf =3D g_try_malloc0(bitmap_size); + if (!buf) { + error_report("%s: Error allocating buffer ", __func__); + goto dpl_unlock; + } + + ret =3D pread(vbasedev->fd, buf, bitmap_size, + region->fd_offset + data_offset); + if (ret !=3D bitmap_size) { + error_report("%s: Failed to get dirty pages bitmap %d", + vbasedev->name, ret); + g_free(buf); + goto dpl_unlock; + } + } + + cpu_physical_memory_set_dirty_lebitmap((unsigned long *)buf, + (start_pfn + count) * page_= size, + copied_pfns); + count +=3D copied_pfns; + total_pfns -=3D copied_pfns; + + if (!buffer_mmaped) { + g_free(buf); + } + } + + trace_vfio_get_dirty_page_list(vbasedev->name, start_pfn, pfn_count, + page_size); + +dpl_unlock: + qemu_mutex_unlock(&migration->lock); +} + /* ---------------------------------------------------------------------- = */ =20 static int vfio_save_setup(QEMUFile *f, void *opaque) diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index ac065b559f4e..414a5e69ec5e 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -160,3 +160,4 @@ 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 vfio_load_state_device_data(char *name, uint64_t data_offset, uint64_t dat= a_size) " (%s) Offset 0x%"PRIx64" size 0x%"PRIx64 +vfio_get_dirty_page_list(char *name, uint64_t start, uint64_t pfn_count, u= int64_t page_size) " (%s) start 0x%"PRIx64" pfn_count 0x%"PRIx64 " page siz= e 0x%"PRIx64 diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index a022484d2636..dc1b83a0b4ef 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -222,5 +222,7 @@ int vfio_spapr_remove_window(VFIOContainer *container, =20 int vfio_migration_probe(VFIODevice *vbasedev, Error **errp); void vfio_migration_finalize(VFIODevice *vbasedev); +void vfio_get_dirty_page_list(VFIODevice *vbasedev, uint64_t start_pfn, + uint64_t pfn_count, uint64_t page_size); =20 #endif /* HW_VFIO_VFIO_COMMON_H */ --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666194; cv=none; d=zoho.com; s=zohoarc; b=Gojh6kPt7Kw13gXpjTSaB7kwBD541Ze3uvbe1gpjKUJ7bBAIfu2ga+Omeaj1+SKgAWtLivdee9YmoFg28k8tVztNfBQ0AVdY/ksBp/iLvfjNojDazlnKfpNwpc00+7f9YxcQ0x7HJxynPb0CK8sxwdRmO8NBcmNnsAddiqzk7Z0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666194; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=gFFxatm5vEcsYi2oQ1vg+wARzP2JUq9a8FiNQRij5Bw=; b=m7U2yInndw9vCIMQOFbPJED7+U0qCUV3SBd1ecKyb81pFufbxND7dGttEq704lVZQrHKEQZjbwMSUE8MAr8WtfIgW4N4bdbdSnR5urkIcxGxfTCqxr/CNfy8fbRkVb1ta8KITcfBvQSdc2Rw2W1iajC4mLZah/Ujs2dgtxFY23s= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666194910820.6306991057552; Tue, 9 Jul 2019 02:56:34 -0700 (PDT) Received: from localhost ([::1]:48236 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmrS-00009m-0Z for importer@patchew.org; Tue, 09 Jul 2019 05:56:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50196) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmoG-0005DI-9z for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmoF-0004dO-5r for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:16 -0400 Received: from hqemgate14.nvidia.com ([216.228.121.143]:7233) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmoE-0004d2-UM for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:15 -0400 Received: from hqpgpgate102.nvidia.com (Not Verified[216.228.121.13]) by hqemgate14.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:53:08 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate102.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:53:13 -0700 Received: from HQMAIL111.nvidia.com (172.20.187.18) by HQMAIL104.nvidia.com (172.18.146.11) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:53:13 +0000 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL111.nvidia.com (172.20.187.18) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:53:12 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:53:06 +0000 X-PGP-Universal: processed; by hqpgpgate102.nvidia.com on Tue, 09 Jul 2019 02:53:13 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:19 +0530 Message-ID: <1562665760-26158-13-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665989; bh=gFFxatm5vEcsYi2oQ1vg+wARzP2JUq9a8FiNQRij5Bw=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=LJV2WjWPAK8+Qgc6QLSb3lq3dnGL2Xzk4HHIXk6tVPtOEZoNDf+rVYUjzcwZQSV5C 3WY1kBj+hYudUUi/Yvker7OviOzuxsyVPOZpBDYyWkOb4lsvEiCLqxMnynOMRT9hC0 7ZCVY4cNf0EhitX7iWhJKd35NEX/z0lNFLRt7tcVe2xqYqVoye3TwoXc8mP7cmEZLT IsctoOX+/WMlWaOTIDv4wA/BJgrParWZTyXSW7uC494p/HpoBM9lbnCJG9/6JRv1fD adrclxTigJcg20uZg5QUQIU+IlpkYkSmm2SnifraXjjL2fHWPt9mIMj03drQ/XcPC/ EL0nZ2vMt+HYg== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.143 Subject: [Qemu-devel] [PATCH v7 12/13] vfio: Add vfio_listerner_log_sync to mark dirty pages 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" vfio_listerner_log_sync gets list of dirty pages from vendor driver and mark those pages dirty when in _SAVING state. Return early for the RAM block section of mapped MMIO region. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/common.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index de74dae8d6a6..d5ee35c95e76 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -36,6 +36,7 @@ #include "sysemu/kvm.h" #include "trace.h" #include "qapi/error.h" +#include "migration/migration.h" =20 VFIOGroupList vfio_group_list =3D QLIST_HEAD_INITIALIZER(vfio_group_list); @@ -794,9 +795,43 @@ static void vfio_listener_region_del(MemoryListener *l= istener, } } =20 +static void vfio_listerner_log_sync(MemoryListener *listener, + MemoryRegionSection *section) +{ + uint64_t start_addr, size, pfn_count; + VFIOGroup *group; + VFIODevice *vbasedev; + + if (memory_region_is_ram_device(section->mr)) { + return; + } + + QLIST_FOREACH(group, &vfio_group_list, next) { + QLIST_FOREACH(vbasedev, &group->device_list, next) { + if (vbasedev->device_state & VFIO_DEVICE_STATE_SAVING) { + continue; + } else { + return; + } + } + } + + start_addr =3D TARGET_PAGE_ALIGN(section->offset_within_address_space); + size =3D int128_get64(section->size); + pfn_count =3D size >> TARGET_PAGE_BITS; + + QLIST_FOREACH(group, &vfio_group_list, next) { + QLIST_FOREACH(vbasedev, &group->device_list, next) { + vfio_get_dirty_page_list(vbasedev, start_addr >> TARGET_PAGE_B= ITS, + pfn_count, TARGET_PAGE_SIZE); + } + } +} + static const MemoryListener vfio_memory_listener =3D { .region_add =3D vfio_listener_region_add, .region_del =3D vfio_listener_region_del, + .log_sync =3D vfio_listerner_log_sync, }; =20 static void vfio_listener_release(VFIOContainer *container) --=20 2.7.0 From nobody Sun Apr 28 21:58:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; t=1562666417; cv=none; d=zoho.com; s=zohoarc; b=Wa1l5GLo+u2fH0cdvFmBcxsib6dPjc0GXfgas5Ux8vlU1JTpnJTVwRlxy4ea5aVzJ0aIvjlZVAoY387wTqjboFONjcqJF6LNN2BNEDIHEjw2CiM9ybMU/J9R4IqGUdmL+2TZoJY7j9G6NKfFB2QzRejY7BmgK708wefJwVH6qjU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562666417; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=oDXfXH2QKaDnSbDGeApTn7hYtedGL0xz2YwbKpIZ7YQ=; b=BxcNEqXufuCXpI8A/dtaJC+dJgjJ+v6ZNBlt4eYe2sW1URLrbyh7b9MHjyD0HIjoTpnnwNDhvO0P/3Qg2bUgFfmAtaLzif/h+PGzzG7ALdgUJFCXI/rQ/FXVlklKXCaed2BjVCEcpAWQ+0UmPXyomc+/N+Anp55ECzgl06XUHOY= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562666417006675.8992292014108; Tue, 9 Jul 2019 03:00:17 -0700 (PDT) Received: from localhost ([::1]:48266 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmuz-0003Z0-25 for importer@patchew.org; Tue, 09 Jul 2019 06:00:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50282) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hkmoZ-0005OQ-Ut for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hkmoW-0004n1-Qd for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:34 -0400 Received: from hqemgate16.nvidia.com ([216.228.121.65]:10392) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hkmoW-0004g9-HO for qemu-devel@nongnu.org; Tue, 09 Jul 2019 05:53:32 -0400 Received: from hqpgpgate102.nvidia.com (Not Verified[216.228.121.13]) by hqemgate16.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 09 Jul 2019 02:53:19 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate102.nvidia.com (PGP Universal service); Tue, 09 Jul 2019 02:53:20 -0700 Received: from HQMAIL109.nvidia.com (172.20.187.15) by HQMAIL108.nvidia.com (172.18.146.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:53:20 +0000 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL109.nvidia.com (172.20.187.15) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 9 Jul 2019 09:53:19 +0000 Received: from kwankhede-dev.nvidia.com (10.124.1.5) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 9 Jul 2019 09:53:13 +0000 X-PGP-Universal: processed; by hqpgpgate102.nvidia.com on Tue, 09 Jul 2019 02:53:20 -0700 From: Kirti Wankhede To: , Date: Tue, 9 Jul 2019 15:19:20 +0530 Message-ID: <1562665760-26158-14-git-send-email-kwankhede@nvidia.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> References: <1562665760-26158-1-git-send-email-kwankhede@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1562665999; bh=oDXfXH2QKaDnSbDGeApTn7hYtedGL0xz2YwbKpIZ7YQ=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=HwGDLJS5Kuidr8Ti94elCiXBnJzlCm1wpNt9wVAl2G3sf03WK5mvvb0zjLclG6nRQ 7rWYwer1CW2ILiWczhkz6pFZJl7bxkoWCjBiZwxWCVDS6hBxQi7gCamD8ZZY9yPgJX OCF9BnlTTfP80v91pUcAxCrCfN9zQemok9TAVtGuO7UrvAllmvp2PeHN6+21K4xswM ks0RIV1GjBOa08dD+bSgamoOr+Ih18tCos2fCh4UZ1fODnjKb/eJSyJr2U4Gv0RV36 rCl6dx023hn54Z0iPiA9fOlTBpqAoRSggvHQmYKmRDiNEnTQdMJn+JOgJsY8+OKosY 0Gy0rMvY9QFxw== X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.228.121.65 Subject: [Qemu-devel] [PATCH v7 13/13] vfio: Make vfio-pci device migration capable. 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, yan.y.zhao@intel.com, eskultet@redhat.com, ziye.yang@intel.com, qemu-devel@nongnu.org, cohuck@redhat.com, shuangtai.tst@alibaba-inc.com, dgilbert@redhat.com, zhi.a.wang@intel.com, mlevitsk@redhat.com, pasic@linux.ibm.com, aik@ozlabs.ru, Kirti Wankhede , eauger@redhat.com, felipe@nutanix.com, jonathan.davies@nutanix.com, changpeng.liu@intel.com, Ken.Xue@amd.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Call vfio_migration_probe() and vfio_migration_finalize() functions for vfio-pci device to enable migration for vfio PCI device. Removed vfio_pci_vmstate structure. Signed-off-by: Kirti Wankhede Reviewed-by: Neo Jia --- hw/vfio/pci.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 5fe4f8076cac..2ea17a814d55 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2852,6 +2852,7 @@ static void vfio_realize(PCIDevice *pdev, Error **err= p) vdev->vbasedev.ops =3D &vfio_pci_ops; vdev->vbasedev.type =3D VFIO_DEVICE_TYPE_PCI; vdev->vbasedev.dev =3D DEVICE(vdev); + vdev->vbasedev.device_state =3D 0; =20 tmp =3D g_strdup_printf("%s/iommu_group", vdev->vbasedev.sysfsdev); len =3D readlink(tmp, group_path, sizeof(group_path)); @@ -3112,6 +3113,12 @@ static void vfio_realize(PCIDevice *pdev, Error **er= rp) } } =20 + ret =3D vfio_migration_probe(&vdev->vbasedev, errp); + if (ret) { + error_report("%s: Failed to setup for migration", + vdev->vbasedev.name); + } + vfio_register_err_notifier(vdev); vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); @@ -3131,6 +3138,7 @@ static void vfio_instance_finalize(Object *obj) VFIOPCIDevice *vdev =3D PCI_VFIO(obj); VFIOGroup *group =3D vdev->vbasedev.group; =20 + vdev->vbasedev.device_state =3D 0; vfio_display_finalize(vdev); vfio_bars_finalize(vdev); g_free(vdev->emulated_config_bits); @@ -3159,6 +3167,7 @@ static void vfio_exitfn(PCIDevice *pdev) } vfio_teardown_msi(vdev); vfio_bars_exit(vdev); + vfio_migration_finalize(&vdev->vbasedev); } =20 static void vfio_pci_reset(DeviceState *dev) @@ -3267,11 +3276,6 @@ static Property vfio_pci_dev_properties[] =3D { DEFINE_PROP_END_OF_LIST(), }; =20 -static const VMStateDescription vfio_pci_vmstate =3D { - .name =3D "vfio-pci", - .unmigratable =3D 1, -}; - static void vfio_pci_dev_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); @@ -3279,7 +3283,6 @@ static void vfio_pci_dev_class_init(ObjectClass *klas= s, void *data) =20 dc->reset =3D vfio_pci_reset; dc->props =3D vfio_pci_dev_properties; - dc->vmsd =3D &vfio_pci_vmstate; dc->desc =3D "VFIO-based PCI device assignment"; set_bit(DEVICE_CATEGORY_MISC, dc->categories); pdc->realize =3D vfio_realize; --=20 2.7.0