From patchwork Wed Feb 22 17:48:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149395 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E57A4C636D6 for ; Wed, 22 Feb 2023 17:50:38 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtFa-0000H1-RQ; Wed, 22 Feb 2023 12:49:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFY-0000GQ-VJ for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:49:52 -0500 Received: from mail-mw2nam10on20614.outbound.protection.outlook.com ([2a01:111:f400:7e89::614] helo=NAM10-MW2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFW-0004EL-Iu for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:49:52 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AvaecS1ChM6rcC5HOZ1FKNZIPA3Ig4ptkf94230A1Ui4AzlZfH+lVQ73hriosO8b6+Dc1mQzHMC/1Mmb/xyWVQbw/zP+Yg5IBEWnyOqxPcxUPQbg89UptnvdM7COPb8HXnJnOaxh9WZa0VkM5WLTeLkS3PcX35SJWbxfG80kuJdqya5pj1TwlcB0LVVMgDyG4EBUfp6f66cfwtTIoWo/kZe+I0KuiONjbPE7SFAZLHEXpCMbkbKUYx4FwwGbheoCsyxt5+8C+uli6liCQ0VnfLECy6XUnXKE43eP/ZEvhVBTq8oGJF1wiO4BiI+oepshTQ/lccmNRowUT/smiZpzSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=7mDSTonaJ8I/HJslSWBy/OcPpoADj+3xSOXo5ICaF9c=; b=KFg893WwL+qeOR4hOB4CWvnVBtDYwiTSYUSxyqslfrpUis/Kv3zKBb+g50zpB+sTh4Rg5dimYPHvuKk1au3/h36XaX7VE/wgUYz10/jIxJBaAhGCS7FniPOi9qFNqcHR/Z/x66Nwdnu19nzIiT0uQx6C4DwhJ++L0xjLGU5JtxT1O+FFwC1gZf4jb1NROBkZqdukRlFoozel+u8V/nN8f9XIPweCI1z5RKSO9QP69vb0GeVXBO7DNWAWXtfwr3UjKd+84i0rGIS8+cg8WeUzeYDZnpViTBf4FlXwL0J8docRWwpeorN2L2z5t2BQM3zFDR7vUnhGWgPgXPmrW3eFuQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7mDSTonaJ8I/HJslSWBy/OcPpoADj+3xSOXo5ICaF9c=; b=uhGz5es8yyHxTaqlVgHqP58Usj4viH/EnaTxSEzhlHe6kopYV+yiHvT0t4mhMAsjX8jaE+LDWdi/SGvT1E09Hkr8ylw6ManQNufFK/0DQHfzmOV2ir/UhU23q59OdXCtG+O88iT/BH0hEjXITt8KY+RrdAI7JcL6F5MYcVV2qeayk7oDttq+kajvZGKPEckiwz3MJ1bpJARbIqu2dIUCv29F74ao4VJdVV1/j4CtR8tzGtp44Uf7+VtPT8DfFcmzBUjVldU/JHYhofVOA8P71dmWUaKx1ZZt8QNEnQm7Rsq8JrdoCPdDB4mGs/TUfbisD1Pr1AAeMt+w+Uj7fEjXVA== Received: from BL0PR02CA0023.namprd02.prod.outlook.com (2603:10b6:207:3c::36) by PH0PR12MB7906.namprd12.prod.outlook.com (2603:10b6:510:26c::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 17:49:46 +0000 Received: from BL02EPF0000C406.namprd05.prod.outlook.com (2603:10b6:207:3c:cafe::81) by BL0PR02CA0023.outlook.office365.com (2603:10b6:207:3c::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:49:46 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C406.mail.protection.outlook.com (10.167.241.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:49:45 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:27 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:27 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:49:22 -0800 To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 01/20] migration: Pass threshold_size to .state_pending_{estimate, exact}() Date: Wed, 22 Feb 2023 19:48:56 +0200 Message-ID: <20230222174915.5647-2-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C406:EE_|PH0PR12MB7906:EE_ X-MS-Office365-Filtering-Correlation-Id: b8ce5149-9ab6-4550-827e-08db14fd2f89 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Eru/U6qmgGCLcJRVkWvWom+2yoyKj3FbTKOBYk+kJujXWMU3sMBDxtZ+aQVbtppASOUqdl5fAjC/o8dMVR4KUtqpiLNg5vo+1CjAF5HW6CDUtfzLvOtq5RBVqvozIaxNwa4xeETBTRxO14Y4BdcK3KnbkgKMs1IfSrW8HxoLJyZrMTI+y50UEIVkettSaMrLmQfFStoDeRb4qrtkN0UaRgHM7hFtFuYS04TyHa0QxUIURJqRNxXcjHWe6YUSSFuZXerhPGagC+6OyRmq1gxVeL4RNlZ8nSWsbaUrvaW39il9cvTkOTpoR3lNx8y8fJ8J5etEgH/Jg9o1QHZK9Qe9HUjtl6oNetoNJK3OBZtLZ0Ql6ZfRTsz71DvyUn7IJ7pxnaiiExphr3yD4JvEo9MS0oDkE/GIRalduWZvenQeDffFDkSJXkBpaq2kXDQ89yi7rVtc6K/P4zVbGBXh1ukzpKZvg5xPD03OCG2IK9cSnPuwgQ39Zme1y4gKIJRYaThbMrDZYKL0VIc5E5gZD1azgUnHHBoBj+7uX69JI7Yi+uPTmG8pcXblhgWwc4CSwjadsS5xJaomayzPXDcv6xO2ctLj2eD3GUO/CaOjBvgKEOe2jyEX7x29BFA2GZW8dS5qIYNKh4kC1dsiQaUKOA50rVzaiO+WeRjibKldmKcrjwvQLk/mH5/SbtZjXlmeT9H6qz3Q/KA3NNlQzg1WEySaM/KTRaWWDE8FvdR9htT6rqY= X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(396003)(39860400002)(346002)(136003)(376002)(451199018)(36840700001)(46966006)(40470700004)(83380400001)(82740400003)(36860700001)(7636003)(82310400005)(2906002)(86362001)(356005)(36756003)(8936002)(5660300002)(41300700001)(7416002)(40460700003)(26005)(186003)(6666004)(47076005)(426003)(1076003)(4326008)(40480700001)(336012)(316002)(2616005)(70586007)(7696005)(6916009)(8676002)(54906003)(478600001)(70206006)(142923001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:49:45.7848 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b8ce5149-9ab6-4550-827e-08db14fd2f89 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C406.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7906 Received-SPF: softfail client-ip=2a01:111:f400:7e89::614; envelope-from=avihaih@nvidia.com; helo=NAM10-MW2-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Avihai Horon X-Patchwork-Original-From: Avihai Horon via From: Avihai Horon Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Pass threshold_size to .state_pending_{estimate,exact}(). This parameter will be used in the following patch by VFIO migration to force the complete transmission of all VFIO pre-copy initial bytes prior moving to stop-copy phase, which can reduce migration downtime. Signed-off-by: Avihai Horon --- include/migration/register.h | 7 ++++--- migration/savevm.h | 6 ++++-- hw/s390x/s390-stattrib.c | 4 ++-- hw/vfio/migration.c | 3 ++- migration/block-dirty-bitmap.c | 2 +- migration/block.c | 4 ++-- migration/migration.c | 12 ++++++++---- migration/ram.c | 6 ++++-- migration/savevm.c | 12 ++++++++---- migration/trace-events | 4 ++-- 10 files changed, 37 insertions(+), 23 deletions(-) diff --git a/include/migration/register.h b/include/migration/register.h index a8dfd8fefd..85d22931a7 100644 --- a/include/migration/register.h +++ b/include/migration/register.h @@ -61,11 +61,12 @@ typedef struct SaveVMHandlers { * pending data. */ /* This estimates the remaining data to transfer */ - void (*state_pending_estimate)(void *opaque, uint64_t *must_precopy, + void (*state_pending_estimate)(void *opaque, uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy); /* This calculate the exact remaining data to transfer */ - void (*state_pending_exact)(void *opaque, uint64_t *must_precopy, - uint64_t *can_postcopy); + void (*state_pending_exact)(void *opaque, uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy); LoadStateHandler *load_state; int (*load_setup)(QEMUFile *f, void *opaque); int (*load_cleanup)(void *opaque); diff --git a/migration/savevm.h b/migration/savevm.h index fb636735f0..c94d31f051 100644 --- a/migration/savevm.h +++ b/migration/savevm.h @@ -40,9 +40,11 @@ void qemu_savevm_state_cleanup(void); void qemu_savevm_state_complete_postcopy(QEMUFile *f); int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only, bool inactivate_disks); -void qemu_savevm_state_pending_exact(uint64_t *must_precopy, +void qemu_savevm_state_pending_exact(uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy); -void qemu_savevm_state_pending_estimate(uint64_t *must_precopy, +void qemu_savevm_state_pending_estimate(uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy); void qemu_savevm_send_ping(QEMUFile *f, uint32_t value); void qemu_savevm_send_open_return_path(QEMUFile *f); diff --git a/hw/s390x/s390-stattrib.c b/hw/s390x/s390-stattrib.c index aed919ad7d..f1d4064c09 100644 --- a/hw/s390x/s390-stattrib.c +++ b/hw/s390x/s390-stattrib.c @@ -182,8 +182,8 @@ static int cmma_save_setup(QEMUFile *f, void *opaque) return 0; } -static void cmma_state_pending(void *opaque, uint64_t *must_precopy, - uint64_t *can_postcopy) +static void cmma_state_pending(void *opaque, uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy) { S390StAttribState *sas = S390_STATTRIB(opaque); S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index a2c3d9bade..4fb7d01532 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -314,7 +314,8 @@ static void vfio_save_cleanup(void *opaque) * repeatedly while pending RAM size is over the threshold, thus migration * can't converge and querying the VFIO device pending data size is useless. */ -static void vfio_state_pending_exact(void *opaque, uint64_t *must_precopy, +static void vfio_state_pending_exact(void *opaque, uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy) { VFIODevice *vbasedev = opaque; diff --git a/migration/block-dirty-bitmap.c b/migration/block-dirty-bitmap.c index fe73aa94b1..4fe0b83bc8 100644 --- a/migration/block-dirty-bitmap.c +++ b/migration/block-dirty-bitmap.c @@ -762,7 +762,7 @@ static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque) return 0; } -static void dirty_bitmap_state_pending(void *opaque, +static void dirty_bitmap_state_pending(void *opaque, uint64_t threshold_size, uint64_t *must_precopy, uint64_t *can_postcopy) { diff --git a/migration/block.c b/migration/block.c index 426a25bb19..70438a299c 100644 --- a/migration/block.c +++ b/migration/block.c @@ -853,8 +853,8 @@ static int block_save_complete(QEMUFile *f, void *opaque) return 0; } -static void block_state_pending(void *opaque, uint64_t *must_precopy, - uint64_t *can_postcopy) +static void block_state_pending(void *opaque, uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy) { /* Estimate pending number of bytes to send */ uint64_t pending; diff --git a/migration/migration.c b/migration/migration.c index ae2025d9d8..a0777d9848 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -3866,15 +3866,19 @@ static MigIterateState migration_iteration_run(MigrationState *s) uint64_t must_precopy, can_postcopy; bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE; - qemu_savevm_state_pending_estimate(&must_precopy, &can_postcopy); + qemu_savevm_state_pending_estimate(s->threshold_size, &must_precopy, + &can_postcopy); uint64_t pending_size = must_precopy + can_postcopy; - trace_migrate_pending_estimate(pending_size, must_precopy, can_postcopy); + trace_migrate_pending_estimate(pending_size, s->threshold_size, + must_precopy, can_postcopy); if (must_precopy <= s->threshold_size) { - qemu_savevm_state_pending_exact(&must_precopy, &can_postcopy); + qemu_savevm_state_pending_exact(s->threshold_size, &must_precopy, + &can_postcopy); pending_size = must_precopy + can_postcopy; - trace_migrate_pending_exact(pending_size, must_precopy, can_postcopy); + trace_migrate_pending_exact(pending_size, s->threshold_size, + must_precopy, can_postcopy); } if (!pending_size || pending_size < s->threshold_size) { diff --git a/migration/ram.c b/migration/ram.c index 96e8a19a58..514a18b5d7 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -3489,7 +3489,8 @@ static int ram_save_complete(QEMUFile *f, void *opaque) return 0; } -static void ram_state_pending_estimate(void *opaque, uint64_t *must_precopy, +static void ram_state_pending_estimate(void *opaque, uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy) { RAMState **temp = opaque; @@ -3505,7 +3506,8 @@ static void ram_state_pending_estimate(void *opaque, uint64_t *must_precopy, } } -static void ram_state_pending_exact(void *opaque, uint64_t *must_precopy, +static void ram_state_pending_exact(void *opaque, uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy) { RAMState **temp = opaque; diff --git a/migration/savevm.c b/migration/savevm.c index aa54a67fda..a642c0dd5a 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -1541,7 +1541,8 @@ flush: * the result is split into the amount for units that can and * for units that can't do postcopy. */ -void qemu_savevm_state_pending_estimate(uint64_t *must_precopy, +void qemu_savevm_state_pending_estimate(uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy) { SaveStateEntry *se; @@ -1558,11 +1559,13 @@ void qemu_savevm_state_pending_estimate(uint64_t *must_precopy, continue; } } - se->ops->state_pending_estimate(se->opaque, must_precopy, can_postcopy); + se->ops->state_pending_estimate(se->opaque, threshold_size, + must_precopy, can_postcopy); } } -void qemu_savevm_state_pending_exact(uint64_t *must_precopy, +void qemu_savevm_state_pending_exact(uint64_t threshold_size, + uint64_t *must_precopy, uint64_t *can_postcopy) { SaveStateEntry *se; @@ -1579,7 +1582,8 @@ void qemu_savevm_state_pending_exact(uint64_t *must_precopy, continue; } } - se->ops->state_pending_exact(se->opaque, must_precopy, can_postcopy); + se->ops->state_pending_exact(se->opaque, threshold_size, must_precopy, + can_postcopy); } } diff --git a/migration/trace-events b/migration/trace-events index 92161eeac5..b23c044f5e 100644 --- a/migration/trace-events +++ b/migration/trace-events @@ -150,8 +150,8 @@ migrate_fd_cleanup(void) "" migrate_fd_error(const char *error_desc) "error=%s" migrate_fd_cancel(void) "" migrate_handle_rp_req_pages(const char *rbname, size_t start, size_t len) "in %s at 0x%zx len 0x%zx" -migrate_pending_exact(uint64_t size, uint64_t pre, uint64_t post) "exact pending size %" PRIu64 " (pre = %" PRIu64 " post=%" PRIu64 ")" -migrate_pending_estimate(uint64_t size, uint64_t pre, uint64_t post) "estimate pending size %" PRIu64 " (pre = %" PRIu64 " post=%" PRIu64 ")" +migrate_pending_exact(uint64_t size, uint64_t threshold_size, uint64_t pre, uint64_t post) "exact pending size %" PRIu64 " threshold size %" PRIu64 " (pre = %" PRIu64 " post=%" PRIu64 ")" +migrate_pending_estimate(uint64_t size, uint64_t threshold_size, uint64_t pre, uint64_t post) "estimate pending size %" PRIu64 " threshold size %" PRIu64 " (pre = %" PRIu64 " post=%" PRIu64 ")" migrate_send_rp_message(int msg_type, uint16_t len) "%d: len %d" migrate_send_rp_recv_bitmap(char *name, int64_t size) "block '%s' size 0x%"PRIi64 migration_completion_file_err(void) "" From patchwork Wed Feb 22 17:48:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149396 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9FB47C61DA4 for ; Wed, 22 Feb 2023 17:50:39 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtFh-0000Hb-Ed; Wed, 22 Feb 2023 12:50:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFg-0000HH-0Z for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:00 -0500 Received: from mail-bn8nam11on20600.outbound.protection.outlook.com ([2a01:111:f400:7eae::600] helo=NAM11-BN8-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFe-0004GD-28 for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:49:59 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=W42HO2cOtMpeWuFrM7EG8DEMRRpXOQBqWIz2qFRO6BV5jvKt4m00skk8aNcQLejHFPVPdy+qBNMPvvqUMlddUcEWQIPrF7fm57csw6mzTLJp9EAS3pfYx0P5lRUUyrPq3zKrkaTIj+exCSGCVBDKZVsj1pAxL1ac7ogD3+X00wBpvB5vLJ7T32d7KRv9e3DaNrC9GPYbVanOfxWoKJ9BVWa/5F5JqDafAEeaPuovItPy90DJH4BAkqbWHhe1UZvz4kVJOwFXFoec2+I4pg9L/XUmcOVBZ86Y+hooC9hNgKtQNojIXz0DUnrNGqcmX883+PmdF9UUrJBZQaqZeIR3ZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=kdyRRTetDwxFgF/Olcd3F36AuidHyC6CwLsxd6C+c2Q=; b=RzI/EEiq09xw5g281q7LVSa50U+HwXPDy9s0a6MFm4EWaPg+m0ph+1ywyCttaoyNPpDry6CSzh0GaK/cSEBFMm6Zoci8rpiin+1pAj1rz2M6g1YmlyqKOqSWxVXHaxbdAqmQaeldn8HanZbwv5r+wW3DKbNYAhA/pqcwa7lmBXkge0Yb96rjnProbJ0Oz5yHWaglwSNIhlgap5JnBhlXUKkt3nLBXxcdiFHQNFZy0EbaVaIhsrIdTa2WlZxRNsgoQYw4H3kmfZMsr5d/WKI9hgIMHAWFpGSMDITfw2oThK+NE1aEbxN4SCOz6a/1PEMXLY7VxClDwhItaWazcu1DUg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=kdyRRTetDwxFgF/Olcd3F36AuidHyC6CwLsxd6C+c2Q=; b=hxNjG27Eui/ZL01VttUVFCrFsioiw9UCDZKJz5VzbbZssjT0GJDDfLFANn1AsCyvIkxSI7SdDfNxaZjPPYE72cfbPii4jff7VXfSpb/lUI4Vwij5RSFMsnGgLLRJDrBhDp4gLTVlJIwrOvaV2NtOPkRjxv5MyjARd9D4M1mQYV/Wj8UHo9gcWVX0uZ63BuDxjVGOSH0RCQBWl4VMiyTmznM9K+xJ6P0FAiucckTubyWc53yVk/V+Nta9P+YkxArXdJHCpIbHHuvIo6KmRoDt7I/U8m1Q0LJ0pfhA7x2WmLiYgwoFrMo+0XkQsZfBrCx2eEZhPXvpfeBZ8emxp49I8A== Received: from BN0PR04CA0062.namprd04.prod.outlook.com (2603:10b6:408:ea::7) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19; Wed, 22 Feb 2023 17:49:51 +0000 Received: from BL02EPF0000C405.namprd05.prod.outlook.com (2603:10b6:408:ea:cafe::c) by BN0PR04CA0062.outlook.office365.com (2603:10b6:408:ea::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21 via Frontend Transport; Wed, 22 Feb 2023 17:49:50 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C405.mail.protection.outlook.com (10.167.241.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:49:50 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:33 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:32 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:49:27 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 02/20] vfio/migration: Refactor vfio_save_block() to return saved data size Date: Wed, 22 Feb 2023 19:48:57 +0200 Message-ID: <20230222174915.5647-3-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C405:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 327923af-0b8c-48e4-7dee-08db14fd3224 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JDodYKwK9z/WSRLevFDTR5WYCBONJlGsu3uppR8mJV8QdAiS3i+aiwDtFqZGU8pGu3cP9agy9neANfMCQ1Di0uka0bWggZxL5F9xvmekLPYva8asCQgHL6Poti4rkW8XYnH3QvqCSJHBmAqXGRQxC+ufbbabLEwynklReaLYgsF3aEfNNDr2Sgv+lUry6WJVTvCpy431BaHFFJveDIel8VanP5p2c4QSm7A71mt0OxpkBTUNIKRSvfIBFSYOdNLCgowPTB3T5AS0oePOXBTR+bMsv/D7y4ZsQpo3VyTo+nveA1Hc7qoTrqE8SPGfHRIRWnpm8vZjNsAGAZ1+U6G79cBEk8o9COtA6Xfn8b9Q7e8NjFoS/mUbMfaWzRU6nIMDY5GKXg8+5p4wrE9XjgONcUl7y8/je66SovjRIokBfCM/p3T/WiRlkegW8Q9qCM9ddBAY/VCP8ftE2HMkElHeUIu2aY9X0ip2AQGpg6yk6AnvMJ/4Dkeny8bBWMpX/bx6dtubobH1MqvfQg0hFydEvJTBQdYZTu785aTw5vM5PM0ED6AkWElVOPjbFw0s9qXIT9wwxk67GqDWckCcQIm9eBr1m/WutMGF2WXAsJCyR/JXxTP4nn741dVUrSWbP39MTqyXdqlK+2vnH/hvkUD9QkAm5SFsqv305Fssk7Tpi0hBDUu2z27eSBIFJiRMw8qqIVoCcsNLmlNIKCdszbmqYQ== X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(346002)(39860400002)(396003)(376002)(136003)(451199018)(40470700004)(46966006)(36840700001)(86362001)(82310400005)(2616005)(316002)(54906003)(40460700003)(36860700001)(2906002)(70206006)(70586007)(7416002)(8676002)(4326008)(6916009)(5660300002)(41300700001)(1076003)(47076005)(6666004)(186003)(26005)(478600001)(426003)(40480700001)(36756003)(7696005)(336012)(82740400003)(7636003)(356005)(8936002)(83380400001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:49:50.1559 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 327923af-0b8c-48e4-7dee-08db14fd3224 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C405.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Received-SPF: softfail client-ip=2a01:111:f400:7eae::600; envelope-from=avihaih@nvidia.com; helo=NAM11-BN8-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Refactor vfio_save_block() to return the size of saved data on success and -errno on error. This will be used in next patch to implement VFIO migration pre-copy support. Signed-off-by: Avihai Horon Reviewed-by: Cédric Le Goater --- hw/vfio/migration.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index 4fb7d01532..94a4df73d0 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -240,8 +240,8 @@ static int vfio_query_stop_copy_size(VFIODevice *vbasedev, return 0; } -/* Returns 1 if end-of-stream is reached, 0 if more data and -errno if error */ -static int vfio_save_block(QEMUFile *f, VFIOMigration *migration) +/* Returns the size of saved data on success and -errno on error */ +static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration) { ssize_t data_size; @@ -251,7 +251,7 @@ static int vfio_save_block(QEMUFile *f, VFIOMigration *migration) return -errno; } if (data_size == 0) { - return 1; + return 0; } qemu_put_be64(f, VFIO_MIG_FLAG_DEV_DATA_STATE); @@ -261,7 +261,7 @@ static int vfio_save_block(QEMUFile *f, VFIOMigration *migration) trace_vfio_save_block(migration->vbasedev->name, data_size); - return qemu_file_get_error(f); + return qemu_file_get_error(f) ?: data_size; } /* ---------------------------------------------------------------------- */ @@ -335,6 +335,7 @@ static void vfio_state_pending_exact(void *opaque, uint64_t threshold_size, static int vfio_save_complete_precopy(QEMUFile *f, void *opaque) { VFIODevice *vbasedev = opaque; + ssize_t data_size; int ret; /* We reach here with device state STOP only */ @@ -345,11 +346,11 @@ static int vfio_save_complete_precopy(QEMUFile *f, void *opaque) } do { - ret = vfio_save_block(f, vbasedev->migration); - if (ret < 0) { - return ret; + data_size = vfio_save_block(f, vbasedev->migration); + if (data_size < 0) { + return data_size; } - } while (!ret); + } while (data_size); qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); ret = qemu_file_get_error(f); From patchwork Wed Feb 22 17:48:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149403 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B333EC636D6 for ; Wed, 22 Feb 2023 17:51:29 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGE-0000S2-6M; Wed, 22 Feb 2023 12:50:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFy-0000KY-MQ for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:19 -0500 Received: from mail-mw2nam10hn20307.outbound.protection.outlook.com ([2a01:111:f400:7e89::307] helo=NAM10-MW2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFv-0004US-Kq for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:18 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HF4cKI/QL0DHJD3hyKCUkB8rD1DRlIHGkt9mFResGXKKbrukbMIAQSzVOedJm03Xw3CUQ560jWyh/FwJihCUFSFpUg4tcr2y0oO5EzTlE/20Dap1uLrGN8X+MD3GJxstosOgHeSXcxOKsD4q4iQyMEOumqQ0xdPDAqIJ+LeEzFUtv5Sp9QoA8enwgW9PT+5DWZPAfANQssM4xaLriw90E55Sc9/flHjfGhsojST8y+7koV0jtR+sxirKW68A8JLWn3ASto4jglLR44hEO9T9s9dWHzCDrkKDLTUYSRvMIFjT81l2F4Io4sDDAZPWrtumekTnZWpSrRSx025qa9kqrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=s55doGS/te85wrCGPGD28NbFiD3C8eZfHZEvAqjcPUU=; b=eoMF3aR/8+GoCPdCPHpWio/UEbWhnReUT+yDYGceSKbBhon09IxSlqLatOncPgmfeM5SQdJFxqOnr6wvMDoNkYtGb3YNL3gi5DK5cWB3E/YaFJRt7gKqR7rIv3qIJL5ak3G7PJ28S3HE6vCMVlP8BF5ReKbOwpv7IhNKze3glt7xXdbuibb2P/mXub/+XVBGdE58IsusAvVVphGGdFMX2anuZy26gspPYV7A1070Rf3iqHuFbUZDYU0Z400JQILIjeznDkEQC5vRMgG31ZcsmY0fnC34AQfhTbEj4lOdUreyYldUPMntqtqMWvPSX9TpnMWaY9iAbNZ7FRNaVyCv+g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=s55doGS/te85wrCGPGD28NbFiD3C8eZfHZEvAqjcPUU=; b=iZgdPixEYhoRDdt0XkegIwsveayOg4Ab90bFRDRunBlfJPnThAvMDSIeo8ZIQcFPQm558MmrPLwS6GbmA7CIMOKXNhl34AmkHvmGAOm+dusH6QRkINQD9CSY4bvqveNFqqDdbakxBVKwnGOSccnZv9c0WcyQjoAVi3qn8fRrArCb72yIUMUBL0ClR0AW/Ds5Hr10yVWipv7yPofz6MH9tH9dSHM5NVVfVLqOhp6kv2ePGVjl27979z8VpGPXBfRM8z8f2ijHtzdxViTHUupUdg10Yi6FPgC438sjI/YGwJDMl4LTbwmclTBK7jSKfa+34GT9PDQojNVV4Y6p5cvOsw== Received: from MW3PR05CA0021.namprd05.prod.outlook.com (2603:10b6:303:2b::26) by DS7PR12MB6093.namprd12.prod.outlook.com (2603:10b6:8:9e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 17:50:09 +0000 Received: from CO1NAM11FT111.eop-nam11.prod.protection.outlook.com (2603:10b6:303:2b:cafe::55) by MW3PR05CA0021.outlook.office365.com (2603:10b6:303:2b::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.18 via Frontend Transport; Wed, 22 Feb 2023 17:50:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT111.mail.protection.outlook.com (10.13.174.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.20 via Frontend Transport; Wed, 22 Feb 2023 17:50:06 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:39 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:38 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:49:33 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 03/20] vfio/migration: Add VFIO migration pre-copy support Date: Wed, 22 Feb 2023 19:48:58 +0200 Message-ID: <20230222174915.5647-4-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT111:EE_|DS7PR12MB6093:EE_ X-MS-Office365-Filtering-Correlation-Id: c407ab97-4a88-4cc3-f5ed-08db14fd3bc7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LhG9Q8IN6RgfxleKCjMs3rTWtg2llGyyPgzabjL/OKZ9qBeygUVqGTQnL95VAyuOgo3tlI6clLNzuI7HDAN3fmpOA4pf+bB1lJaL8IpQa/sttDs5z0lcM9jGb605qWvU2iKdU2Zsg1ub9s6y3B0a72zLsBqxmQdn125WQyvW9PgrZrTsu6I7dExOnr5XAbY9jdLfy6U/GbLNW4lisrmebIdvhOHyMOimIMBl6RyJRFgQrpJps+BD6utIr7/YkrPBUDVwH97MUnwnLpVceLLWzYGxfS8/4VZM9wYoRU0gMe0fQoPcuy/TNUmCpiJb2KPhvKAa4Rt2XCSBitrH4JFcqne3yqUg+6/QXMypTdWCBfJhy3AS+ZvXGsmvACDgqL0D3cpgGoHNpwCZijQ36rEhYbAtxqIRglMSnV+k7btgFER6x+QrgK2pvOx0GhJ3m4GSQaMzUnUm5A9DMqedUs5EdyahAoMxR1ep+qAvaF9qFl8MLkDIg51N6Jesiozeq60WZPR0JuOzJL6n44gzSg4j0RsS3yqGls5fd35tIdSSmIwrY754qHuX60qK3jGD0lAyecsV95lub1hfDmWk1R7gaPkU3153a4RUTFliP24bA81bNxVwqG6EtfkV5Ly3G2n3yo1lFVkZuNS8SfU/kE7Qtn6iDhy0wQQTXhtiqpaG/6WyVo1197tWReXh5+31HYpaaDmY+CnAvh0m5cX9N6sgHOu23FJeZpFLfDj/TunImf1FCNe1jLtwEQJBbxH7P390EbtKniDClFtSzmIUBSGg/aTGFoQXQobcO77rx8IRnRCHA3ZDEdFkGabIqLKk6IS5 X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(396003)(136003)(39860400002)(376002)(346002)(451199018)(5400799012)(46966006)(40470700004)(36840700001)(26005)(186003)(82310400005)(336012)(36756003)(7696005)(2616005)(6916009)(478600001)(86362001)(426003)(47076005)(966005)(6666004)(83380400001)(8676002)(70206006)(70586007)(1076003)(316002)(54906003)(4326008)(41300700001)(7416002)(36860700001)(8936002)(5660300002)(34020700004)(30864003)(82740400003)(40480700001)(2906002)(7636003)(40460700003)(356005)(12100799015); DIR:OUT; SFP:1501; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:06.4359 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c407ab97-4a88-4cc3-f5ed-08db14fd3bc7 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT111.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6093 Received-SPF: softfail client-ip=2a01:111:f400:7e89::307; envelope-from=avihaih@nvidia.com; helo=NAM10-MW2-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Pre-copy support allows the VFIO device data to be transferred while the VM is running. This helps to accommodate VFIO devices that have a large amount of data that needs to be transferred, and it can reduce migration downtime. Pre-copy support is optional in VFIO migration protocol v2. Implement pre-copy of VFIO migration protocol v2 and use it for devices that support it. Full description of it can be found here [1]. [1] https://lore.kernel.org/kvm/20221206083438.37807-3-yishaih@nvidia.com/ Signed-off-by: Avihai Horon --- docs/devel/vfio-migration.rst | 35 +++++-- include/hw/vfio/vfio-common.h | 3 + hw/vfio/common.c | 6 +- hw/vfio/migration.c | 175 ++++++++++++++++++++++++++++++++-- hw/vfio/trace-events | 4 +- 5 files changed, 201 insertions(+), 22 deletions(-) diff --git a/docs/devel/vfio-migration.rst b/docs/devel/vfio-migration.rst index c214c73e28..ba80b9150d 100644 --- a/docs/devel/vfio-migration.rst +++ b/docs/devel/vfio-migration.rst @@ -7,12 +7,14 @@ the guest is running on source host and restoring this saved state on the destination host. This document details how saving and restoring of VFIO devices is done in QEMU. -Migration of VFIO devices currently consists of a single stop-and-copy phase. -During the stop-and-copy phase the guest is stopped and the entire VFIO device -data is transferred to the destination. - -The pre-copy phase of migration is currently not supported for VFIO devices. -Support for VFIO pre-copy will be added later on. +Migration of VFIO devices consists of two phases: the optional pre-copy phase, +and the stop-and-copy phase. The pre-copy phase is iterative and allows to +accommodate VFIO devices that have a large amount of data that needs to be +transferred. The iterative pre-copy phase of migration allows for the guest to +continue whilst the VFIO device state is transferred to the destination, this +helps to reduce the total downtime of the VM. VFIO devices can choose to skip +the pre-copy phase of migration by not reporting the VFIO_MIGRATION_PRE_COPY +flag in VFIO_DEVICE_FEATURE_MIGRATION ioctl. Note that currently VFIO migration is supported only for a single device. This is due to VFIO migration's lack of P2P support. However, P2P support is planned @@ -29,10 +31,20 @@ VFIO implements the device hooks for the iterative approach as follows: * A ``load_setup`` function that sets the VFIO device on the destination in _RESUMING state. +* A ``state_pending_estimate`` function that reports an estimate of the + remaining pre-copy data that the vendor driver has yet to save for the VFIO + device. + * A ``state_pending_exact`` function that reads pending_bytes from the vendor driver, which indicates the amount of data that the vendor driver has yet to save for the VFIO device. +* An ``is_active_iterate`` function that indicates ``save_live_iterate`` is + active only when the VFIO device is in pre-copy states. + +* A ``save_live_iterate`` function that reads the VFIO device's data from the + vendor driver during iterative pre-copy phase. + * A ``save_state`` function to save the device config space if it is present. * A ``save_live_complete_precopy`` function that sets the VFIO device in @@ -95,8 +107,10 @@ Flow of state changes during Live migration =========================================== Below is the flow of state change during live migration. -The values in the brackets represent the VM state, the migration state, and +The values in the parentheses represent the VM state, the migration state, and the VFIO device state, respectively. +The text in the square brackets represents the flow if the VFIO device supports +pre-copy. Live migration save path ------------------------ @@ -108,11 +122,12 @@ Live migration save path | migrate_init spawns migration_thread Migration thread then calls each device's .save_setup() - (RUNNING, _SETUP, _RUNNING) + (RUNNING, _SETUP, _RUNNING [_PRE_COPY]) | - (RUNNING, _ACTIVE, _RUNNING) - If device is active, get pending_bytes by .state_pending_exact() + (RUNNING, _ACTIVE, _RUNNING [_PRE_COPY]) + If device is active, get pending_bytes by .state_pending_{estimate,exact}() If total pending_bytes >= threshold_size, call .save_live_iterate() + [Data of VFIO device for pre-copy phase is copied] Iterate till total pending bytes converge and are less than threshold | On migration completion, vCPU stops and calls .save_live_complete_precopy for diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 87524c64a4..ee55d442b4 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -66,6 +66,9 @@ typedef struct VFIOMigration { int data_fd; void *data_buffer; size_t data_buffer_size; + uint64_t precopy_init_size; + uint64_t precopy_dirty_size; + uint64_t mig_flags; } VFIOMigration; typedef struct VFIOAddressSpace { diff --git a/hw/vfio/common.c b/hw/vfio/common.c index bab83c0e55..6f5afe9f5a 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -409,7 +409,8 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container) } if (vbasedev->pre_copy_dirty_page_tracking == ON_OFF_AUTO_OFF && - migration->device_state == VFIO_DEVICE_STATE_RUNNING) { + (migration->device_state == VFIO_DEVICE_STATE_RUNNING || + migration->device_state == VFIO_DEVICE_STATE_PRE_COPY)) { return false; } } @@ -438,7 +439,8 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container) return false; } - if (migration->device_state == VFIO_DEVICE_STATE_RUNNING) { + if (migration->device_state == VFIO_DEVICE_STATE_RUNNING || + migration->device_state == VFIO_DEVICE_STATE_PRE_COPY) { continue; } else { return false; diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index 94a4df73d0..307983d57d 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -67,6 +67,8 @@ static const char *mig_state_to_str(enum vfio_device_mig_state state) return "STOP_COPY"; case VFIO_DEVICE_STATE_RESUMING: return "RESUMING"; + case VFIO_DEVICE_STATE_PRE_COPY: + return "PRE_COPY"; default: return "UNKNOWN STATE"; } @@ -240,6 +242,23 @@ static int vfio_query_stop_copy_size(VFIODevice *vbasedev, return 0; } +static int vfio_query_precopy_size(VFIOMigration *migration, + uint64_t *init_size, uint64_t *dirty_size) +{ + struct vfio_precopy_info precopy = { + .argsz = sizeof(precopy), + }; + + if (ioctl(migration->data_fd, VFIO_MIG_GET_PRECOPY_INFO, &precopy)) { + return -errno; + } + + *init_size = precopy.initial_bytes; + *dirty_size = precopy.dirty_bytes; + + return 0; +} + /* Returns the size of saved data on success and -errno on error */ static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration) { @@ -248,6 +267,11 @@ static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration) data_size = read(migration->data_fd, migration->data_buffer, migration->data_buffer_size); if (data_size < 0) { + /* Pre-copy emptied all the device state for now */ + if (errno == ENOMSG) { + return 0; + } + return -errno; } if (data_size == 0) { @@ -264,6 +288,31 @@ static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration) return qemu_file_get_error(f) ?: data_size; } +static void vfio_update_estimated_pending_data(VFIOMigration *migration, + uint64_t data_size) +{ + if (!data_size) { + /* + * Pre-copy emptied all the device state for now, update estimated sizes + * accordingly. + */ + migration->precopy_init_size = 0; + migration->precopy_dirty_size = 0; + + return; + } + + if (migration->precopy_init_size) { + uint64_t init_size = MIN(migration->precopy_init_size, data_size); + + migration->precopy_init_size -= init_size; + data_size -= init_size; + } + + migration->precopy_dirty_size -= MIN(migration->precopy_dirty_size, + data_size); +} + /* ---------------------------------------------------------------------- */ static int vfio_save_setup(QEMUFile *f, void *opaque) @@ -284,6 +333,35 @@ static int vfio_save_setup(QEMUFile *f, void *opaque) return -ENOMEM; } + if (migration->mig_flags & VFIO_MIGRATION_PRE_COPY) { + uint64_t init_size = 0, dirty_size = 0; + int ret; + + switch (migration->device_state) { + case VFIO_DEVICE_STATE_RUNNING: + ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_PRE_COPY, + VFIO_DEVICE_STATE_RUNNING); + if (ret) { + return ret; + } + + vfio_query_precopy_size(migration, &init_size, &dirty_size); + migration->precopy_init_size = init_size; + migration->precopy_dirty_size = dirty_size; + + break; + case VFIO_DEVICE_STATE_STOP: + /* vfio_save_complete_precopy() will go to STOP_COPY */ + + migration->precopy_init_size = 0; + migration->precopy_dirty_size = 0; + + break; + default: + return -EINVAL; + } + } + trace_vfio_save_setup(vbasedev->name, migration->data_buffer_size); qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); @@ -302,23 +380,44 @@ static void vfio_save_cleanup(void *opaque) trace_vfio_save_cleanup(vbasedev->name); } +static void vfio_state_pending_estimate(void *opaque, uint64_t threshold_size, + uint64_t *must_precopy, + uint64_t *can_postcopy) +{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + + if (migration->device_state != VFIO_DEVICE_STATE_PRE_COPY) { + return; + } + + /* + * Initial size should be transferred during pre-copy phase so stop-copy + * phase will not be slowed down. Report threshold_size to force another + * pre-copy iteration. + */ + *must_precopy += migration->precopy_init_size ? + threshold_size : + migration->precopy_dirty_size; + + trace_vfio_state_pending_estimate(vbasedev->name, *must_precopy, + *can_postcopy, + migration->precopy_init_size, + migration->precopy_dirty_size); +} + /* * Migration size of VFIO devices can be as little as a few KBs or as big as * many GBs. This value should be big enough to cover the worst case. */ #define VFIO_MIG_STOP_COPY_SIZE (100 * GiB) -/* - * Only exact function is implemented and not estimate function. The reason is - * that during pre-copy phase of migration the estimate function is called - * repeatedly while pending RAM size is over the threshold, thus migration - * can't converge and querying the VFIO device pending data size is useless. - */ static void vfio_state_pending_exact(void *opaque, uint64_t threshold_size, uint64_t *must_precopy, uint64_t *can_postcopy) { VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; uint64_t stop_copy_size = VFIO_MIG_STOP_COPY_SIZE; /* @@ -328,8 +427,57 @@ static void vfio_state_pending_exact(void *opaque, uint64_t threshold_size, vfio_query_stop_copy_size(vbasedev, &stop_copy_size); *must_precopy += stop_copy_size; + if (migration->device_state == VFIO_DEVICE_STATE_PRE_COPY) { + uint64_t init_size = 0, dirty_size = 0; + + vfio_query_precopy_size(migration, &init_size, &dirty_size); + migration->precopy_init_size = init_size; + migration->precopy_dirty_size = dirty_size; + + /* + * Initial size should be transferred during pre-copy phase so + * stop-copy phase will not be slowed down. Report threshold_size + * to force another pre-copy iteration. + */ + *must_precopy += migration->precopy_init_size ? + threshold_size : + migration->precopy_dirty_size; + } + trace_vfio_state_pending_exact(vbasedev->name, *must_precopy, *can_postcopy, - stop_copy_size); + stop_copy_size, migration->precopy_init_size, + migration->precopy_dirty_size); +} + +static bool vfio_is_active_iterate(void *opaque) +{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + + return migration->device_state == VFIO_DEVICE_STATE_PRE_COPY; +} + +static int vfio_save_iterate(QEMUFile *f, void *opaque) +{ + VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; + ssize_t data_size; + + data_size = vfio_save_block(f, migration); + if (data_size < 0) { + return data_size; + } + qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE); + + vfio_update_estimated_pending_data(migration, data_size); + + trace_vfio_save_iterate(vbasedev->name); + + /* + * A VFIO device's pre-copy dirty_bytes is not guaranteed to reach zero. + * Return 1 so following handlers will not be potentially blocked. + */ + return 1; } static int vfio_save_complete_precopy(QEMUFile *f, void *opaque) @@ -338,7 +486,7 @@ static int vfio_save_complete_precopy(QEMUFile *f, void *opaque) ssize_t data_size; int ret; - /* We reach here with device state STOP only */ + /* We reach here with device state STOP or STOP_COPY only */ ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP_COPY, VFIO_DEVICE_STATE_STOP); if (ret) { @@ -457,7 +605,10 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id) static const SaveVMHandlers savevm_vfio_handlers = { .save_setup = vfio_save_setup, .save_cleanup = vfio_save_cleanup, + .state_pending_estimate = vfio_state_pending_estimate, .state_pending_exact = vfio_state_pending_exact, + .is_active_iterate = vfio_is_active_iterate, + .save_live_iterate = vfio_save_iterate, .save_live_complete_precopy = vfio_save_complete_precopy, .save_state = vfio_save_state, .load_setup = vfio_load_setup, @@ -470,13 +621,18 @@ static const SaveVMHandlers savevm_vfio_handlers = { static void vfio_vmstate_change(void *opaque, bool running, RunState state) { VFIODevice *vbasedev = opaque; + VFIOMigration *migration = vbasedev->migration; enum vfio_device_mig_state new_state; int ret; if (running) { new_state = VFIO_DEVICE_STATE_RUNNING; } else { - new_state = VFIO_DEVICE_STATE_STOP; + new_state = + (migration->device_state == VFIO_DEVICE_STATE_PRE_COPY && + (state == RUN_STATE_FINISH_MIGRATE || state == RUN_STATE_PAUSED)) ? + VFIO_DEVICE_STATE_STOP_COPY : + VFIO_DEVICE_STATE_STOP; } /* @@ -590,6 +746,7 @@ static int vfio_migration_init(VFIODevice *vbasedev) migration->vbasedev = vbasedev; migration->device_state = VFIO_DEVICE_STATE_RUNNING; migration->data_fd = -1; + migration->mig_flags = mig_flags; oid = vmstate_if_get_id(VMSTATE_IF(DEVICE(obj))); if (oid) { diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 669d9fe07c..51613e02e6 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -161,6 +161,8 @@ vfio_save_block(const char *name, int data_size) " (%s) data_size %d" vfio_save_cleanup(const char *name) " (%s)" vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d" vfio_save_device_config_state(const char *name) " (%s)" +vfio_save_iterate(const char *name) " (%s)" vfio_save_setup(const char *name, uint64_t data_buffer_size) " (%s) data buffer size 0x%"PRIx64 -vfio_state_pending_exact(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t stopcopy_size) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" stopcopy size 0x%"PRIx64 +vfio_state_pending_estimate(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" precopy initial size 0x%"PRIx64" precopy dirty size 0x%"PRIx64 +vfio_state_pending_exact(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t stopcopy_size, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy 0x%"PRIx64" postcopy 0x%"PRIx64" stopcopy size 0x%"PRIx64" precopy initial size 0x%"PRIx64" precopy dirty size 0x%"PRIx64 vfio_vmstate_change(const char *name, int running, const char *reason, const char *dev_state) " (%s) running %d reason %s device state %s" From patchwork Wed Feb 22 17:48:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149397 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B5117C61DA4 for ; Wed, 22 Feb 2023 17:50:57 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGC-0000Mz-Lt; Wed, 22 Feb 2023 12:50:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFy-0000KW-Lw for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:19 -0500 Received: from mail-mw2nam12hn20312.outbound.protection.outlook.com ([2a01:111:f400:fe5a::312] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFw-0004UZ-DU for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:18 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WSfCk0z+Unv+RwDdnM56eHfeZRkIZ8CDDpRcSUo+uj0BG9/irit+QSIiPsug7TTuudQ4THLUy5Z3Ny5Z8G4paJnEusdCaYFZQZHjf+gDY+BXfnkkQep5S0+8cMlOa4NDYaMir38890vveK5pe3KaEecrK8vVv08beWS8xh+aL2mZv2A2drcK/MB7M064IcJWHdgm96Xlt+dxlwzAaP7uN7+3qUFi7eWxGGU2g2ZNmpTrCXwsQGLl44qsiFZmzTNazM74Kx8KhJEBnqm82EkvzMHfFj5Re277WN1Pxm6YuLlFFtKmhQVfBBqfhNNkcB6iba3gIYNE3U4AMaUypE1D9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=L7YvrQ3fpbZHRvgyCYu+RNFSY9ltI+7XcevHIrZMA/A=; b=guHa1j/3/zd/FVynVdOqZ3ZcDsagmKP7KHSNEMG/EZ69tYcvUwgD7AIo0+HWtjYAZW+f9QXlhiCOVHwoIgt01r84FTX01QT9X1J4OuP3Y1i4QE5djI3WQvc/XqKUu1hQKfyKk4ycmTj/E1F0cc2vs7gUtTrS5X/21pe/HGfS6ioRHEWyCiuMSrIjfIF18O2lDCjwgm/+YIy0gB/FuoSxmH07NIevrpgz6oTRHyyKA4q9MKSCPUYebzpCAYGp2c266wk1dLX7rqYb6LgMAD8zs0p7yU5ruvSAWll1TCI+XOvjpVBifIGUo9oNe28zbvUAjR1SAj6iZBdxz+54y0+dGg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=L7YvrQ3fpbZHRvgyCYu+RNFSY9ltI+7XcevHIrZMA/A=; b=mlnsYYiwU5a2E+FkEeeMVS/ofYFs3LmM524/ToGKHKI5t7VBkagc94WEnLxvNKZF+yczor/cI++MckABQLRIfzVjkeckKC7cxN6/orPrcdK0OhVy7iC7kOZirqeic9oBkwKV57i8gl9R371pAa2E3gErAzLk8vIBHx5gpebqtdNDGLB7CsbYa2QlEg8csy6GvIczMlZIF1TX+GNmJ2Gvsuu7XgC1Cebrob1qKmvo2aNlwW7V4B6qdxoQMucXZrxD6W2fgLPKR6cuqbw+NKuONHvRRuw7Ftn5pqA639hW1kMepMMPXs2aE5Dj+WrwU0Y8QODua+fHg7RMiA4ezkbXuA== Received: from MW4PR04CA0210.namprd04.prod.outlook.com (2603:10b6:303:86::35) by PH7PR12MB7164.namprd12.prod.outlook.com (2603:10b6:510:203::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19; Wed, 22 Feb 2023 17:50:13 +0000 Received: from CO1NAM11FT017.eop-nam11.prod.protection.outlook.com (2603:10b6:303:86:cafe::95) by MW4PR04CA0210.outlook.office365.com (2603:10b6:303:86::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:50:13 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT017.mail.protection.outlook.com (10.13.175.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.20 via Frontend Transport; Wed, 22 Feb 2023 17:50:12 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:45 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:44 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:49:39 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 04/20] vfio/common: Fix error reporting in vfio_get_dirty_bitmap() Date: Wed, 22 Feb 2023 19:48:59 +0200 Message-ID: <20230222174915.5647-5-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT017:EE_|PH7PR12MB7164:EE_ X-MS-Office365-Filtering-Correlation-Id: c50b3bde-92d6-4cc8-4191-08db14fd3f20 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dsyVJu97Ww7k+c4hPDP2pGPIFxrlbyyhVjX5UkSL+jynqwrG5V3mabe3NBZgLOdsYyeiEma69ITbeMIiONu2vFc3bVOAEV78NrK5gqVq4WuX14DaURiBUng+e4UgIhcPoAM7X6M0gVhPVL6AvqM3cucx/Ff6IAP8sgzLYbGOkW0VK8UcSsTgPhhSHaKjE/9pvPoVRC2TpI63F4NqaczJ8u+y0lDC38A6rDzCQxd7tiIMA+Edi9OgSD5x3tLjmfywvj+Nb+nBhD0yk1LpBdjwKVWMT0zqswnRpcA1WNfMGyGwgK8I1XPCECcuS0kzV27pZJrKTqMG2p+ftfliv8i3VxoizgEMGO6/p89lR8nR/lIeVJXBIc6/IYkZ+1P8GEl3bJRXhg2gZlLkUhe+8w/CdBJlaKK98JUveJaOyIO8FwoqRDzX5npKI6uzeFbE4zXtw9jvMWCeOw1ZtlY/1pF4N0M5FRIw0mDDqAUX3YP3MfBwlcioQAHqV8FqhiWgaLmvZaxXqu2vI/jVw8lHkMBLCSJQX6B1nvzcuSGNYK41DgFyyC259IatclbWyayRN/Na2nTHhvGK7tDrkcNvDU059wfPVMOJcfhAzyNro8CxD/blNJFT6wf00Vs9ehMTMz5pSQtZcrOW8zU2TYP+BQxGtWBJwwTLGa/BO+mQk/T8VSku6gJaQ157JwCwsRjag2gZ4kpqh8uuLX9Gmy/oAkpAjZzkZMLP6lJ1RHSql19H5d7mTK1EK3HMUuMZ815vOR2QbWuBb0tWARsPIKsEnflYiBiUvFLJjfWGgYjyodsFpGM= X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(39860400002)(346002)(5400799012)(451199018)(40470700004)(36840700001)(46966006)(54906003)(316002)(5660300002)(41300700001)(26005)(7696005)(8936002)(7416002)(478600001)(4744005)(356005)(70586007)(70206006)(86362001)(6916009)(4326008)(8676002)(82310400005)(2906002)(336012)(82740400003)(40460700003)(36860700001)(34020700004)(7636003)(40480700001)(6666004)(2616005)(426003)(36756003)(1076003)(186003)(47076005)(14143004)(12100799015); DIR:OUT; SFP:1501; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:12.0479 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c50b3bde-92d6-4cc8-4191-08db14fd3f20 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT017.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7164 Received-SPF: softfail client-ip=2a01:111:f400:fe5a::312; envelope-from=avihaih@nvidia.com; helo=NAM12-MW2-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Return -errno instead of -1 if VFIO_IOMMU_DIRTY_PAGES ioctl fails in vfio_get_dirty_bitmap(). Signed-off-by: Avihai Horon Reviewed-by: Cédric Le Goater --- hw/vfio/common.c | 1 + 1 file changed, 1 insertion(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 6f5afe9f5a..27db71427e 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1337,6 +1337,7 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, dbitmap); if (ret) { + ret = -errno; error_report("Failed to get dirty bitmap for iova: 0x%"PRIx64 " size: 0x%"PRIx64" err: %d", (uint64_t)range->iova, (uint64_t)range->size, errno); From patchwork Wed Feb 22 17:49:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149413 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 720D3C636D6 for ; Wed, 22 Feb 2023 17:53:04 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGD-0000OL-7d; Wed, 22 Feb 2023 12:50:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFw-0000KR-SW for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:19 -0500 Received: from mail-bn8nam11on20600.outbound.protection.outlook.com ([2a01:111:f400:7eae::600] helo=NAM11-BN8-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFv-0004UM-3e for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:16 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N5zYrkoLKG2NSIDI6SZFKSdZGNeQmZ7mXT4KM99XNcoT6ZCEHerbcPax7N7dhAhE4azbTmMP/WLMQutdtB8vUhovxssio6xNBeUghqbBSi2+LBm1y5p8wdq0/EYIBAa4H0JEln9/LLxiU+9mwGaG31/Ei/4RaAwUp0waITEitIXGrf6w5loNVrA7OLL1vVvkVmA8Ddfc86iFBSHiLuBS/KQkAqzIT+mYMvoXvBHEvMwCiD/oIkyZcaHwkyrsxGrJlwup4u3SDN+uNjfj0k+lMws66hasfHc+cTPukYQazaOZgQrUXc2HZxM3acCaCCrN1EzpdzGi73jtoAgXl0kpjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=/+xCzB/Nt9FY/rO0YUCNnQRxutaJkP2rKNzqTJl2iL8=; b=fAO5Z+JsifsBel9X2UyZQGcCtfCbAy3ehpH7GX3Lzn9WDos+9GqS5YRtVPn+5lvQ7SGd5850cc9dbXhQDmL2zTpUCyd75MBICJx8w3fa3u1WoyQFkOE3b38zzFc06RGoT9refm/XwCTveDEaXiHPjBSvvCGLny/Zev5490pfvgiGei7GNuiTWwcuKpbCwgyTZiP0BbcaNVBzXf4CvOG1XmotDCgqWmF4H7/Gz+aB9faEJJ1xUPwbgcJHX8Q19/OM6sM6ZUkDs96ioFhNfQLFD+6CXq9LLN5M7U406bQWqU0hzB1IoOveT1fxM6Qrb9GSt3O9P4i3t1lfvesOnqYSZQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/+xCzB/Nt9FY/rO0YUCNnQRxutaJkP2rKNzqTJl2iL8=; b=Ril5G40Z+i7r6LjEjgKQVKhRHWAmvON4NwelCM2bW/TeCPg3STXpXNWN4MxDvsRvH/AI2ypTnGXnhLUFF6rx5Cmy+rySLVoO9lKVJVGJbdFj5xZhtj03m/mksFyr8l/0QGQO5v4mw667/I8WJYybpTtiIe+L//Z5P2laaY/mSPzQlrhRv8jfI8ohWSE24HuzmPNyD/v5RlpWgidmExzORLsaoMq32Ca7vWLVafbz4K62zfogFfWZi5dCBSHah2g/pTSbkpBCNUhp0PdglRI5o8w+SBSy/36GJKFd0k/vrGdE6npYj0e81OKrbQPcomi6vCvNO9dm4r1VoTifkQG/Zg== Received: from BN9PR03CA0707.namprd03.prod.outlook.com (2603:10b6:408:ef::22) by SN7PR12MB8060.namprd12.prod.outlook.com (2603:10b6:806:343::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Wed, 22 Feb 2023 17:50:08 +0000 Received: from BL02EPF0000C407.namprd05.prod.outlook.com (2603:10b6:408:ef:cafe::b4) by BN9PR03CA0707.outlook.office365.com (2603:10b6:408:ef::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.23 via Frontend Transport; Wed, 22 Feb 2023 17:50:08 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C407.mail.protection.outlook.com (10.167.241.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:50:08 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:51 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:50 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:49:45 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 05/20] vfio/common: Fix wrong %m usages Date: Wed, 22 Feb 2023 19:49:00 +0200 Message-ID: <20230222174915.5647-6-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C407:EE_|SN7PR12MB8060:EE_ X-MS-Office365-Filtering-Correlation-Id: aa137e8d-59db-4ae8-809e-08db14fd3cdb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QlU+lJL6QZMTsXLUsz/RWcdnSYlCaaMMgSiPKgXJYMnUWPMPAYaLnSdGerntB7TsEVI8J9dZDFn9eoANO5EGDX88hdd/Rrm5eN201fJ4rwV/VRhErOPcQ2EKvR/qu+5l5tQqR6mAT5qG/iWQH2kdR5mwS73shgDEFQHwuhy4gYs4KHD4oBk9eWtFUdAB5F4xHC1CQUowbPd6d6hpb1SCjacW0NKL9frw9F8T7+4q7ywMcVYt7b/b0Kp7s34aC6FnD7c8NESKXzwdl67DxWd7zFOYhDus84sDF9jFz7aJ/2+7xam36aH1mM6FGZOZLjbl67slrGQyxAvti0zKjpCpnZK+5JhF4PSbpbTT0bUtUhKZZtOm2Q3+yUBINIynTwJPSd4NnGZvwUOgVHHF5u+mAHN2msrGBveWejaR/5WZK28JguN/qfAL2yM1c24yHpU249dozALB+wvjA/uw05FD3cJzNJy9dEaFgPqNVFbecpQgXgiypXcVmxpvDi3h8GF3n3lXa3JyrV+9xOpZ5OJ/wKbYb81BpdMxdWffJeB2+vS1XJ0hJOkBiMI/pb8+V7S94GRFvlKvROrn77TIvHl79A8VWZ0/hMIu5oUeiID3a9RymtIJ0Bv/sRrTVErCjp56Y2vmkbtOMeDUwkkmiGp7L4g6MweYWaS04lG6xsyJTpCrlxb4D9X2WKnXSLT3JXCyAdGHABw8tZLGVPV4UUozZQ== X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(39860400002)(376002)(136003)(346002)(396003)(451199018)(36840700001)(46966006)(40470700004)(26005)(6666004)(186003)(7696005)(83380400001)(2616005)(336012)(54906003)(1076003)(478600001)(316002)(7636003)(70206006)(4326008)(2906002)(8936002)(8676002)(6916009)(41300700001)(70586007)(7416002)(5660300002)(82740400003)(36860700001)(40460700003)(40480700001)(36756003)(82310400005)(47076005)(426003)(356005)(86362001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:08.1325 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: aa137e8d-59db-4ae8-809e-08db14fd3cdb X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C407.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8060 Received-SPF: softfail client-ip=2a01:111:f400:7eae::600; envelope-from=avihaih@nvidia.com; helo=NAM11-BN8-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org There are several places where the %m conversion is used if one of vfio_dma_map(), vfio_dma_unmap() or vfio_get_dirty_bitmap() fail. The %m usage in these places is wrong since %m relies on errno value while the above functions don't report errors via errno. Fix it by using strerror() with the returned value instead. Signed-off-by: Avihai Horon Reviewed-by: Cédric Le Goater --- hw/vfio/common.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 27db71427e..930eda40a1 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -705,17 +705,17 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) read_only); if (ret) { error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", " - "0x%"HWADDR_PRIx", %p) = %d (%m)", + "0x%"HWADDR_PRIx", %p) = %d (%s)", container, iova, - iotlb->addr_mask + 1, vaddr, ret); + iotlb->addr_mask + 1, vaddr, ret, strerror(-ret)); } } else { ret = vfio_dma_unmap(container, iova, iotlb->addr_mask + 1, iotlb); if (ret) { error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", " - "0x%"HWADDR_PRIx") = %d (%m)", + "0x%"HWADDR_PRIx") = %d (%s)", container, iova, - iotlb->addr_mask + 1, ret); + iotlb->addr_mask + 1, ret, strerror(-ret)); } } out: @@ -1097,8 +1097,9 @@ static void vfio_listener_region_add(MemoryListener *listener, vaddr, section->readonly); if (ret) { error_setg(&err, "vfio_dma_map(%p, 0x%"HWADDR_PRIx", " - "0x%"HWADDR_PRIx", %p) = %d (%m)", - container, iova, int128_get64(llsize), vaddr, ret); + "0x%"HWADDR_PRIx", %p) = %d (%s)", + container, iova, int128_get64(llsize), vaddr, ret, + strerror(-ret)); if (memory_region_is_ram_device(section->mr)) { /* Allow unexpected mappings not to be fatal for RAM devices */ error_report_err(err); @@ -1230,16 +1231,18 @@ static void vfio_listener_region_del(MemoryListener *listener, ret = vfio_dma_unmap(container, iova, int128_get64(llsize), NULL); if (ret) { error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", " - "0x%"HWADDR_PRIx") = %d (%m)", - container, iova, int128_get64(llsize), ret); + "0x%"HWADDR_PRIx") = %d (%s)", + container, iova, int128_get64(llsize), ret, + strerror(-ret)); } iova += int128_get64(llsize); } ret = vfio_dma_unmap(container, iova, int128_get64(llsize), NULL); if (ret) { error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", " - "0x%"HWADDR_PRIx") = %d (%m)", - container, iova, int128_get64(llsize), ret); + "0x%"HWADDR_PRIx") = %d (%s)", + container, iova, int128_get64(llsize), ret, + strerror(-ret)); } } @@ -1386,9 +1389,9 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) translated_addr); if (ret) { error_report("vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", " - "0x%"HWADDR_PRIx") = %d (%m)", - container, iova, - iotlb->addr_mask + 1, ret); + "0x%"HWADDR_PRIx") = %d (%s)", + container, iova, iotlb->addr_mask + 1, ret, + strerror(-ret)); } } rcu_read_unlock(); From patchwork Wed Feb 22 17:49:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149400 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0374DC636D6 for ; Wed, 22 Feb 2023 17:51:13 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGE-0000S9-CA; Wed, 22 Feb 2023 12:50:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFy-0000KX-Lw for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:19 -0500 Received: from mail-dm6nam12on2061d.outbound.protection.outlook.com ([2a01:111:f400:fe59::61d] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFv-0004UQ-O2 for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:18 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nEfx5tTL6TA4yhJsxT1jZjLO2ow9YmGjdjcIp15xDVEHalXvq/G0noD1lWu14fiLfwteP80N5oVxzcXANihdH/Wd5qBD3843v8G5cys2NvLVqanaJ7P2jLKbZywt1aosDUSYw9LRB1dWUWoJzKDUjnlIQF8B/tgjINjkOWJFpUXWbhwGwrlO3rqiPKc4oRxV+3S5r+nDYYNBf4B9pLF69z8uO6ZQzPHbaLMAseQdCWsrdOFt6fk4kRGFU7Sycd85W5ma1L+6ovlcp/4x/FFq7FGeLE3n/AYV1NFl5Evqi1WahEYbKDuA9FKDDoUKwR4nuknQ+NIi490ZMGXIu6AwQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ouPy+EY7bDasn4WSDeDPlkIuxUqS5DN9vVo7w77Qn/I=; b=TibvKYuFyKxs1lwnlGNzcb0RzA3G/72p9c52QNwgY0mqrcaZrHcTu5lDmQA8MY+1NgQANa2j+xwlCWSI6DfKGmF3IfwkPPjW2ka4S+ZTBT/W2hwBqSfAC4UBkINdHWWj7oazE7fLXXJcxUHxUapG1L4xcvUIQb+mQcrLuOtrVftHetLSzNzhzhNdmWwyRgtvm40Fv+OQek6eSM6SxcAddc+h0BykwkMVMY+oA9ot3GrkA90I9YPiConChRIFhUPi9HwZMm28poeIOAQKsxYX7sMjSkt8vsb4q/4jP9ri6EJcEimRSBa9mFySbfV++uvT8pV0+a9IRbUqdJz68HGHGg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ouPy+EY7bDasn4WSDeDPlkIuxUqS5DN9vVo7w77Qn/I=; b=pWNiqI55PGG3yO5EQIMJUidsLViRd/LNkOML/Be+ndMOFCMDVxtNwO5GqvzR4moBigRpvGvPY3SKYk+V+W83LlITomjUB3RLpvZaLK9u1y3iDGPffXld4WeKnCq8gNg4Q75GxYQnUGYIVPCrcYKPmBGxmxfOwX+nCkjNMH1PjgmyT1NEFcX6880cfKLKxMqSQ2PeXHwe+N/vNtyWMmFIlN6NXb/weuZ5U1G4aWTY9e0IVWD5PNposZqg/1OUqv54waYwgXZHZp3XAQM54kychrrF3yoLSEjPjx+lmdSikTO8f6sFBsGAUzYDQ/vIc40n5muNLX1I7EV4GPiBjF114g== Received: from BL0PR02CA0012.namprd02.prod.outlook.com (2603:10b6:207:3c::25) by DS0PR12MB8575.namprd12.prod.outlook.com (2603:10b6:8:164::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6086.21; Wed, 22 Feb 2023 17:50:11 +0000 Received: from BL02EPF0000C406.namprd05.prod.outlook.com (2603:10b6:207:3c:cafe::aa) by BL0PR02CA0012.outlook.office365.com (2603:10b6:207:3c::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:50:11 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C406.mail.protection.outlook.com (10.167.241.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:50:11 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:56 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:49:56 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:49:51 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 06/20] vfio/common: Abort migration if dirty log start/stop/sync fails Date: Wed, 22 Feb 2023 19:49:01 +0200 Message-ID: <20230222174915.5647-7-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C406:EE_|DS0PR12MB8575:EE_ X-MS-Office365-Filtering-Correlation-Id: 7db32cb3-f4d2-4409-f5f0-08db14fd3e9f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: x6nXQ6YiJ+71F8JpKjvhoQpuv5HfP0oOqVTtwJ+DzmHwlLgQSkfpQ/FKM/rjbFJ7vZO/K5XsCYCD3pJOy5FEw9e/F+Zdke4tQakEn8HxI6ve9aKuJ3q8VqGUb1r6oLvKs6wketr9sSiY792QY5X523ntEEmhC23YiWOhAVV5FJ0k6ef5XVVfns/EUs0Gw1ArJyx/3MXoajswMX9Db/T0N540dGdnKAys8pKPC6Y/8vC1c4quSuLLHO8DXmh1rl5bJdKv9SCDFZAY4Yg5NRZxuvQrJUxbG+7GrTqzxdq38fmkUbDxEqRpIId8TrpA/UiqYbBwBmdqhvyzwT2pEm8lbHj8b/ZU6ne0HROvcylrpPqolaiioYZq5pK95Dvb68FWN/sDJ7cfLhnLP63ETxPCjyS2+ZF6lXX2i7R11tZvWoJvAI4xKF7H9L9yQS7eBUikusTGZbabG1C+Cq9coKwVy7hw+qXSQLwu9olpT3yz296z9Hq4tMP5dP2/O9ZagKpz3syUsI/2RX7v7rzURSGeC+Y1+CZMkijwHv6M00rPJzg1L124e5mBkNVD67l+/wOP5tuHDC2CUNqhTNCB9+/kuYRDmglgTltSiDVmdPOt4cK+aQ3LyZ5QYFA9Ssua3I1EAWSB5n1jHUQTdQOkwr9x4Yr+2C915zVIFgoMaZDOvolxgsxewQVzx2/y3EuV21760c3BBYv6fq+nbde415JHPcf80vPvAL+VcRGqdHwfI/Q= X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(136003)(376002)(39860400002)(396003)(346002)(451199018)(40470700004)(46966006)(36840700001)(41300700001)(5660300002)(2906002)(336012)(7636003)(36860700001)(83380400001)(40480700001)(36756003)(54906003)(4326008)(6916009)(66574015)(2616005)(426003)(47076005)(82740400003)(356005)(1076003)(26005)(6666004)(186003)(316002)(86362001)(40460700003)(7696005)(478600001)(82310400005)(8676002)(70586007)(70206006)(8936002)(7416002)(14143004); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:11.1134 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7db32cb3-f4d2-4409-f5f0-08db14fd3e9f X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C406.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8575 Received-SPF: softfail client-ip=2a01:111:f400:fe59::61d; envelope-from=avihaih@nvidia.com; helo=NAM12-DM6-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org If VFIO dirty pages log start/stop/sync fails during migration, migration should be aborted as pages dirtied by VFIO devices might not be reported properly. This is not the case today, where in such scenario only an error is printed. Fix it by aborting migration in the above scenario. Fixes: 758b96b61d5c ("vfio/migrate: Move switch of dirty tracking into vfio_memory_listener") Fixes: b6dd6504e303 ("vfio: Add vfio_listener_log_sync to mark dirty pages") Fixes: 9e7b0442f23a ("vfio: Add ioctl to get dirty pages bitmap during dma unmap") Signed-off-by: Avihai Horon Reviewed-by: Cédric Le Goater --- hw/vfio/common.c | 53 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 930eda40a1..ac93b85632 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -42,6 +42,7 @@ #include "migration/migration.h" #include "migration/misc.h" #include "migration/blocker.h" +#include "migration/qemu-file.h" #include "sysemu/tpm.h" VFIOGroupList vfio_group_list = @@ -390,6 +391,19 @@ void vfio_unblock_multiple_devices_migration(void) multiple_devices_migration_blocker = NULL; } +static void vfio_set_migration_error(int err) +{ + MigrationState *ms = migrate_get_current(); + + if (migration_is_setup_or_active(ms->state)) { + WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) { + if (ms->to_dst_file) { + qemu_file_set_error(ms->to_dst_file, err); + } + } + } +} + static bool vfio_devices_all_dirty_tracking(VFIOContainer *container) { VFIOGroup *group; @@ -682,6 +696,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) if (iotlb->target_as != &address_space_memory) { error_report("Wrong target AS \"%s\", only system memory is allowed", iotlb->target_as->name ? iotlb->target_as->name : "none"); + vfio_set_migration_error(-EINVAL); return; } @@ -716,6 +731,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) "0x%"HWADDR_PRIx") = %d (%s)", container, iova, iotlb->addr_mask + 1, ret, strerror(-ret)); + vfio_set_migration_error(ret); } } out: @@ -1261,7 +1277,7 @@ static void vfio_listener_region_del(MemoryListener *listener, } } -static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start) +static int vfio_set_dirty_page_tracking(VFIOContainer *container, bool start) { int ret; struct vfio_iommu_type1_dirty_bitmap dirty = { @@ -1269,7 +1285,7 @@ static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start) }; if (!container->dirty_pages_supported) { - return; + return 0; } if (start) { @@ -1280,23 +1296,34 @@ static void vfio_set_dirty_page_tracking(VFIOContainer *container, bool start) ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, &dirty); if (ret) { + ret = -errno; error_report("Failed to set dirty tracking flag 0x%x errno: %d", dirty.flags, errno); } + + return ret; } static void vfio_listener_log_global_start(MemoryListener *listener) { VFIOContainer *container = container_of(listener, VFIOContainer, listener); + int ret; - vfio_set_dirty_page_tracking(container, true); + ret = vfio_set_dirty_page_tracking(container, true); + if (ret) { + vfio_set_migration_error(ret); + } } static void vfio_listener_log_global_stop(MemoryListener *listener) { VFIOContainer *container = container_of(listener, VFIOContainer, listener); + int ret; - vfio_set_dirty_page_tracking(container, false); + ret = vfio_set_dirty_page_tracking(container, false); + if (ret) { + vfio_set_migration_error(ret); + } } static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, @@ -1372,19 +1399,18 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) VFIOContainer *container = giommu->container; hwaddr iova = iotlb->iova + giommu->iommu_offset; ram_addr_t translated_addr; + int ret = -EINVAL; trace_vfio_iommu_map_dirty_notify(iova, iova + iotlb->addr_mask); if (iotlb->target_as != &address_space_memory) { error_report("Wrong target AS \"%s\", only system memory is allowed", iotlb->target_as->name ? iotlb->target_as->name : "none"); - return; + goto out; } rcu_read_lock(); if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) { - int ret; - ret = vfio_get_dirty_bitmap(container, iova, iotlb->addr_mask + 1, translated_addr); if (ret) { @@ -1395,6 +1421,11 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) } } rcu_read_unlock(); + +out: + if (ret) { + vfio_set_migration_error(ret); + } } static int vfio_ram_discard_get_dirty_bitmap(MemoryRegionSection *section, @@ -1487,13 +1518,19 @@ static void vfio_listener_log_sync(MemoryListener *listener, MemoryRegionSection *section) { VFIOContainer *container = container_of(listener, VFIOContainer, listener); + int ret; if (vfio_listener_skipped_section(section)) { return; } if (vfio_devices_all_dirty_tracking(container)) { - vfio_sync_dirty_bitmap(container, section); + ret = vfio_sync_dirty_bitmap(container, section); + if (ret) { + error_report("vfio: Failed to sync dirty bitmap, err: %d (%s)", ret, + strerror(-ret)); + vfio_set_migration_error(ret); + } } } From patchwork Wed Feb 22 17:49:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149399 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 855B8C636D6 for ; Wed, 22 Feb 2023 17:51:07 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGF-0000SW-Vt; Wed, 22 Feb 2023 12:50:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtG0-0000Kg-EQ for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:21 -0500 Received: from mail-bn1nam02on20619.outbound.protection.outlook.com ([2a01:111:f400:7eb2::619] helo=NAM02-BN1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtFx-0004Uj-K8 for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:20 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lix7baJpas14X+8W/M6ZSTSsXEBtqiKsmPMQblMFrJUtMtpU7IsrqeSg5BYk6cMgnHi83Ebg/BIU7+yKV+3aQbh2FH9Q2v5yv7fZ0MFAf98TQ1rOI76rjK9MgCQAKcG8OGyGJAOzvvaaP/cJGGpBeC5BK/XtSvWXIoiN3A800vyfY+QxsY7w6emjAB7e9NNFNOO1dn7SjrxNf0zSBRss2nvHW60hsOOSbVFQQ302yEBvnI3b9HstJuEUdGZjUcoUdczNMQ9d4X6tdJiXJ3dCNtz6LWxcAdri+FvadtAJsZq+dyjdZ7T9KItBQXl4yr59i4eNNcqi+8foE/9EGgR+XA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=RWBesjB5UlqC9AzGy47xkg8lmKzGZV5hbhYfN6Uze8A=; b=iPoaNNAuBZKIxYA2Tl+5Xf25VHrhSqCbfEA9yOeCZJjO2pTh/yLFQTRsc8SJz+A7Xnpo6HQ/JBcKz4eXjWQ5l7SnMNuAJ1OuEMVB8zb0cLuK/14kZ/bRS9XboXi5gaIH1cEjS98dw22dZsrWXDHQg+nE8GZO3TTWM3pyK+mIFWn6bvKai3w3JpD0vex5awZoa1b8cYs22NaYN0j/1L28KQClhkiwkmxJjAGgK7E1vsY7ixNCPimtSCTsVpeJ5X9dz1q7DBq9NpunRWpKwq+jXu+lj8zVcp9G8ozFqAQC+xKVgVeQQf+bVP2hYrro8pmWjqPCAk1sTqx0GuPL9ggJ5Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=RWBesjB5UlqC9AzGy47xkg8lmKzGZV5hbhYfN6Uze8A=; b=EtuBHlBaDNADET/GKOCIxjcqUptkjULGuaQAGIIMCFWStLPE0F8M20IMg+HzpK1itM2adRhWaeB8qiZL6EyPxh/xBjjRSPQlIaCngF8fwFuyfQH4Q44rBgvHxM3bvduvv43C4JLk/6H69f48WlLmRNIH+XUjwqK86pZMgs5pxrLbx0ip1DT/qU4smEczL4LrazrtrWjmqOyuSqcfgGmaVtP58B+JrwLzW9nJROy+nn9ob1n6jHaUlf3dIQb6Gnm4Wlv8a1cncyUH21QlyBd42F7pbXLB3fp2sZiHZqYr5ZMjjrje2PYYckrpB4TVZACpsbroqLHTnmQip2zjFpwshw== Received: from BN9PR03CA0711.namprd03.prod.outlook.com (2603:10b6:408:ef::26) by PH7PR12MB6564.namprd12.prod.outlook.com (2603:10b6:510:210::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 17:50:14 +0000 Received: from BL02EPF0000C407.namprd05.prod.outlook.com (2603:10b6:408:ef:cafe::eb) by BN9PR03CA0711.outlook.office365.com (2603:10b6:408:ef::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:50:14 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C407.mail.protection.outlook.com (10.167.241.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:50:14 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:02 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:02 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:49:56 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 07/20] vfio/common: Add VFIOBitmap and (de)alloc functions Date: Wed, 22 Feb 2023 19:49:02 +0200 Message-ID: <20230222174915.5647-8-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C407:EE_|PH7PR12MB6564:EE_ X-MS-Office365-Filtering-Correlation-Id: fb7e7cca-fe86-4687-94e6-08db14fd4097 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: v8DURMl0vK/GE3y0dYFBIuhaGkzRS2KZbGHVo2TPshnWMs0mYlp0UhFCO/HNDYy/ZxNiywYQrSXttF0v8ZYklhOB3MmSIkGlENlfhGLTH6Wqa0hLsuelc5Mf2wQjKauDafxfMQZ4EWV+UAAfemFbtZWByxhp4yPXacZ5zBbiGzHy0zH8xx5NGz+ZH+kIIymIcTRth6brM+diT47/FAiUojd2mM4J6d6TGMbazweFZ84/VdH5BSPvp6BNQtWWF9arw1QoVe/AKdNKM87OzIrGE02rq1qUygucTo0guvFZyBL3GEoL8G2WYN/TRx5j9KKmUxfnX/D/P5+IUlBjUKRtwYZnVS/ZJjrfYyS0hN9EB9DfhYYANGEVJPkqlOdmNP3xTOUJSid3IsU5gSF31OiPLmc2mU1rklua7vV48pmh4fxCp5Kxtkk/MXtzn+8x8WN6a54W+j5xwcD9hMQvtMmTeRWb1vgyMxjcGL65045LgTEF42Nwaai9deQDYX5iB6qnOF0meSf+g/VoruRr29Z0aJgSqzQuGIT0SrruaQkcXzMakuUU94cC2ogQj56LSsDJ3AuSrNWPfqVBocSJ9cHs7HiNs5Fvk08ZtzS4HDLxLuS9ahShSerOcPbPAqhL0uJlKdfjt9Lycfaes1DE12Q4Z7Zpd9iXMpK6jbUB+947Jj9ckV2o8SEHpiY7f+YgkTTT2NeIvXUCfq6MNjtoM05A1Q== X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(396003)(39860400002)(376002)(136003)(346002)(451199018)(36840700001)(40470700004)(46966006)(26005)(47076005)(8936002)(336012)(83380400001)(36860700001)(41300700001)(356005)(40480700001)(7636003)(2906002)(426003)(86362001)(82740400003)(82310400005)(36756003)(40460700003)(6666004)(5660300002)(7416002)(2616005)(1076003)(6916009)(186003)(70206006)(4326008)(70586007)(8676002)(54906003)(316002)(478600001)(7696005)(66899018); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:14.4139 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fb7e7cca-fe86-4687-94e6-08db14fd4097 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C407.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6564 Received-SPF: softfail client-ip=2a01:111:f400:7eb2::619; envelope-from=avihaih@nvidia.com; helo=NAM02-BN1-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org There are already two places where dirty page bitmap allocation and calculations are done in open code. With device dirty page tracking being added in next patches, there are going to be even more places. To avoid code duplication, introduce VFIOBitmap struct and corresponding alloc and dealloc functions and use them where applicable. Signed-off-by: Avihai Horon --- hw/vfio/common.c | 89 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 29 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index ac93b85632..84f08bdbbb 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -320,6 +320,41 @@ const MemoryRegionOps vfio_region_ops = { * Device state interfaces */ +typedef struct { + unsigned long *bitmap; + hwaddr size; + hwaddr pages; +} VFIOBitmap; + +static VFIOBitmap *vfio_bitmap_alloc(hwaddr size) +{ + VFIOBitmap *vbmap = g_try_new0(VFIOBitmap, 1); + if (!vbmap) { + errno = ENOMEM; + + return NULL; + } + + vbmap->pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size(); + vbmap->size = ROUND_UP(vbmap->pages, sizeof(__u64) * BITS_PER_BYTE) / + BITS_PER_BYTE; + vbmap->bitmap = g_try_malloc0(vbmap->size); + if (!vbmap->bitmap) { + g_free(vbmap); + errno = ENOMEM; + + return NULL; + } + + return vbmap; +} + +static void vfio_bitmap_dealloc(VFIOBitmap *vbmap) +{ + g_free(vbmap->bitmap); + g_free(vbmap); +} + bool vfio_mig_active(void) { VFIOGroup *group; @@ -470,9 +505,14 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container, { struct vfio_iommu_type1_dma_unmap *unmap; struct vfio_bitmap *bitmap; - uint64_t pages = REAL_HOST_PAGE_ALIGN(size) / qemu_real_host_page_size(); + VFIOBitmap *vbmap; int ret; + vbmap = vfio_bitmap_alloc(size); + if (!vbmap) { + return -errno; + } + unmap = g_malloc0(sizeof(*unmap) + sizeof(*bitmap)); unmap->argsz = sizeof(*unmap) + sizeof(*bitmap); @@ -486,35 +526,28 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container, * qemu_real_host_page_size to mark those dirty. Hence set bitmap_pgsize * to qemu_real_host_page_size. */ - bitmap->pgsize = qemu_real_host_page_size(); - bitmap->size = ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) / - BITS_PER_BYTE; + bitmap->size = vbmap->size; + bitmap->data = (__u64 *)vbmap->bitmap; - if (bitmap->size > container->max_dirty_bitmap_size) { - error_report("UNMAP: Size of bitmap too big 0x%"PRIx64, - (uint64_t)bitmap->size); + if (vbmap->size > container->max_dirty_bitmap_size) { + error_report("UNMAP: Size of bitmap too big 0x%"PRIx64, vbmap->size); ret = -E2BIG; goto unmap_exit; } - bitmap->data = g_try_malloc0(bitmap->size); - if (!bitmap->data) { - ret = -ENOMEM; - goto unmap_exit; - } - ret = ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, unmap); if (!ret) { - cpu_physical_memory_set_dirty_lebitmap((unsigned long *)bitmap->data, - iotlb->translated_addr, pages); + cpu_physical_memory_set_dirty_lebitmap(vbmap->bitmap, + iotlb->translated_addr, vbmap->pages); } else { error_report("VFIO_UNMAP_DMA with DIRTY_BITMAP : %m"); } - g_free(bitmap->data); unmap_exit: g_free(unmap); + vfio_bitmap_dealloc(vbmap); + return ret; } @@ -1331,7 +1364,7 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, { struct vfio_iommu_type1_dirty_bitmap *dbitmap; struct vfio_iommu_type1_dirty_bitmap_get *range; - uint64_t pages; + VFIOBitmap *vbmap; int ret; if (!container->dirty_pages_supported) { @@ -1341,6 +1374,11 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, return 0; } + vbmap = vfio_bitmap_alloc(size); + if (!vbmap) { + return -errno; + } + dbitmap = g_malloc0(sizeof(*dbitmap) + sizeof(*range)); dbitmap->argsz = sizeof(*dbitmap) + sizeof(*range); @@ -1355,15 +1393,8 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, * to qemu_real_host_page_size. */ range->bitmap.pgsize = qemu_real_host_page_size(); - - pages = REAL_HOST_PAGE_ALIGN(range->size) / qemu_real_host_page_size(); - range->bitmap.size = ROUND_UP(pages, sizeof(__u64) * BITS_PER_BYTE) / - BITS_PER_BYTE; - range->bitmap.data = g_try_malloc0(range->bitmap.size); - if (!range->bitmap.data) { - ret = -ENOMEM; - goto err_out; - } + range->bitmap.size = vbmap->size; + range->bitmap.data = (__u64 *)vbmap->bitmap; ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, dbitmap); if (ret) { @@ -1374,14 +1405,14 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, goto err_out; } - cpu_physical_memory_set_dirty_lebitmap((unsigned long *)range->bitmap.data, - ram_addr, pages); + cpu_physical_memory_set_dirty_lebitmap(vbmap->bitmap, ram_addr, + vbmap->pages); trace_vfio_get_dirty_bitmap(container->fd, range->iova, range->size, range->bitmap.size, ram_addr); err_out: - g_free(range->bitmap.data); g_free(dbitmap); + vfio_bitmap_dealloc(vbmap); return ret; } From patchwork Wed Feb 22 17:49:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149404 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F0601C64EC7 for ; Wed, 22 Feb 2023 17:51:31 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGN-0000bt-RU; Wed, 22 Feb 2023 12:50:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGM-0000bP-4Y for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:42 -0500 Received: from mail-dm6nam10hn20300.outbound.protection.outlook.com ([2a01:111:f400:7e88::300] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGK-0004cn-9r for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:41 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BmTlLtzR9ZWxI8SEuJcRQlPCMSPWF35RIxRiBIQ1eyLIH16Rc/oGmuDGlUzYABYWoHoM/vQisLXlzIHPipfNBeNOnqq3bgq5acw9Nhexg7jsf35/HLQFer0hSi/U/1S1fIbjer1u+sP6e+8OUVT2vElVZV1gaEUC2OAOV/blPv2ChYV4yOP4MjYUKwowHdXax8jZG9n2Fz15q3oMf58494h5PDiGRZGQR7DGc41V70nqq/JaR7AKafUM9U9P7aThDnzx5Ojzw0acgALTNix5B/NeI447MFjgquC6ro6iSD5fRl2m1HiV5zJ/hXLPWHv9Gc+A/ow3bDz0mhySCbygiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=N6u07zgrVbZiMR+VnEnnIjBG6o/OUltURcQu6o2j/Oo=; b=QjGszwfm2mXEiiPmXGctCToVCy7RInc9VCYYMFau0fF858Ztlc08KqG/tU38RcshcbR+kelCZ8EQ0wsc0j+S9Lo6GU9TUI/uDPuKYySENY/XT6jtK49bqkbwnJtvI+gmRC3SEUnPSquN0gq5sPBDxYc5MyslrkR4HXDFPG4vKztzR2XIp5EF5tmFilNJnPqNvlpXwT5rWUpxE5kiNlxqn74aYXlBBM+3XjYUGw2ksfPjsR/Ip6iG6R6k5E1FbU4rnnprgmkvPmV5d5ioPOWq7FCSjZAa6osnvntJkJACIDp3VRcvbwLOggETp2VVn1Iy3Tv3g+YAvVo68IdZbCOkYg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=N6u07zgrVbZiMR+VnEnnIjBG6o/OUltURcQu6o2j/Oo=; b=r4J/GfXFN9fgMzU6LeDdnBfi342AiPlxg6MAkCvp9BHs/ttZQfm8i9UsIf+fa005RuWmyUYQy+20uE7te16zcDN99p+inY50PKFkdLWqZ9p80eWdwKB69iEjpDaKVMFWSKfCP9z0eVRqr1s83lyOa2G+t4WuBRBSQjFHetstgVVcqtcABFHqFTOyGQubNknqeGqjHD4oSG0/xirc+0byI7VFndKW5vsDM6hekT1R76HDUHZgts1N/Sqrj8UkMB8dXxwpm+jv+LsA25hkhQib/ZUkXbRwodKzaL/4zIupYx04AtkoBr3krM70J9mhlnyWcQYuqpuX8+Bh+nFXEphzeg== Received: from MW4PR04CA0128.namprd04.prod.outlook.com (2603:10b6:303:84::13) by MN2PR12MB4144.namprd12.prod.outlook.com (2603:10b6:208:15f::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19; Wed, 22 Feb 2023 17:50:36 +0000 Received: from CO1NAM11FT068.eop-nam11.prod.protection.outlook.com (2603:10b6:303:84:cafe::2b) by MW4PR04CA0128.outlook.office365.com (2603:10b6:303:84::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:50:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT068.mail.protection.outlook.com (10.13.175.142) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.20 via Frontend Transport; Wed, 22 Feb 2023 17:50:35 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:08 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:07 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:02 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 08/20] util: Add iova_tree_nnodes() Date: Wed, 22 Feb 2023 19:49:03 +0200 Message-ID: <20230222174915.5647-9-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT068:EE_|MN2PR12MB4144:EE_ X-MS-Office365-Filtering-Correlation-Id: 3d31fe87-d791-4a72-f962-08db14fd4d3c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jVGfEGnp1YFCtYZ0eHXxkzm/sBdG2j+Rt1xJIh02RqeIiFCpEqKLrRu3eJwAj1ISr1b+1l3xej3gzcBF1FFNqP+msaYiV7p6jxVz90EAdD/5J16WYIxsFzYXwOpChyhbIVBP3/e3nKmfK5HxLhj9klWMIYtB/RZB2pfBUYcJnb1TWumWR3ydniIDUNlZDdUstLomkPu3RN1tAIBP9eajZuMZR/eztjWiLaQ7MeZ411z9xNEBKavbiIWO7+UoCHczNEctDIV1tnhzJ72aKbiHMk8YnyYaZLVwEbXdzVFE27Z1joEEkJth9x7k8TRKiUBdLIIXO7xWzela7iEZbo1h8HrHfJFMjce9GrqU4TJQ8ng/hRq+XQ74xsUjjnsPKdxGmL6JOS3fS1pqusYihafazr6wB7AqErpUyQQpKM9Y/pCPFE+mxI0c6dJA/gAaD3m/Zo1x9BjfWBSUcmaR+O3YAyyF3LoYhDKRCs82WvT+DQIhzWYy07dtlGVXB/1RAbZsCSN93Y8l25M+HKO2LxgBFiVeGo4/Dvx4lxUYnIwMmUTI2HQ1jxG0ckjKGHWBadwBjnpCDsNOfqNQ9bcHpAc27DboWbmwc3np1Hgm1r9qIS24iBl3rS7fMf87yyLwN2tVuPIuOw8JZIcF7jMC9pkDHpuN04AJegh5xq+CcHk1/2AvZr2xuLfaSkV+3p3lbP5UCNcdt0FR0YMqHM9SOKqABZPexBSTNaReUjtyQKsnSyj35pziQPy2X1K8+fqebmMJTGMwRdWbbltTnj2sUBqSiQ== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(376002)(396003)(136003)(39860400002)(346002)(5400799012)(451199018)(36840700001)(46966006)(40470700004)(7636003)(82740400003)(316002)(41300700001)(186003)(40480700001)(26005)(34020700004)(36860700001)(426003)(7416002)(336012)(47076005)(8936002)(2616005)(356005)(5660300002)(54906003)(478600001)(1076003)(6916009)(40460700003)(70586007)(7696005)(4326008)(8676002)(70206006)(82310400005)(36756003)(86362001)(2906002)(12100799015); DIR:OUT; SFP:1501; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:35.7242 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3d31fe87-d791-4a72-f962-08db14fd4d3c X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT068.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4144 Received-SPF: softfail client-ip=2a01:111:f400:7e88::300; envelope-from=avihaih@nvidia.com; helo=NAM10-DM6-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Joao Martins Add iova_tree_nnodes() which returns the number of nodes in the IOVA tree. Signed-off-by: Joao Martins Acked-by: Peter Xu --- include/qemu/iova-tree.h | 11 +++++++++++ util/iova-tree.c | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/include/qemu/iova-tree.h b/include/qemu/iova-tree.h index 8528e5c98f..7bb80783ce 100644 --- a/include/qemu/iova-tree.h +++ b/include/qemu/iova-tree.h @@ -164,4 +164,15 @@ int iova_tree_alloc_map(IOVATree *tree, DMAMap *map, hwaddr iova_begin, */ void iova_tree_destroy(IOVATree *tree); +/** + * iova_tree_nnodes: + * + * @tree: the iova tree to consult + * + * Returns the number of nodes in the iova tree + * + * Return: >=0 for the number of nodes. + */ +gint iova_tree_nnodes(IOVATree *tree); + #endif diff --git a/util/iova-tree.c b/util/iova-tree.c index 536789797e..6141a6229b 100644 --- a/util/iova-tree.c +++ b/util/iova-tree.c @@ -280,3 +280,8 @@ void iova_tree_destroy(IOVATree *tree) g_tree_destroy(tree->tree); g_free(tree); } + +gint iova_tree_nnodes(IOVATree *tree) +{ + return g_tree_nnodes(tree->tree); +} From patchwork Wed Feb 22 17:49:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149408 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4ED9FC636D6 for ; Wed, 22 Feb 2023 17:52:06 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGE-0000SA-Dj; Wed, 22 Feb 2023 12:50:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGB-0000Pg-Th for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:33 -0500 Received: from mail-dm6nam10on20620.outbound.protection.outlook.com ([2a01:111:f400:7e88::620] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGA-0004bD-2V for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:31 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UhEGE/5iHtS3s/nzzZMCg8fVHGaAyskVqr/7t6ZGzTPRY3Neuy/WxlMWyTH0VVU1gRqnAdJaPH3ZhgX/bepTF2Dfk5ygOpmrzntheDZkniQTyJaPP27kcd8K1TqZzNHH/8SWY3TArMX+DEwV6L/E+/UJD7T+1HyI+/e39ATSHSq4hIZVAn4FKVRzHijJAip91vTAyGg1P+Eck7UHS632CDi4n7hyPU61+/RJuoZcp4Wc4kYSqUNo9ibSNWTHPth8myshO+ZyNmOsoDKJkX0tfupROUzvDsSa5QkuZeTUHOQMpF0+J/E1i9c8jgjUpDdZ5tCQ59clIyDqSfZQM5umhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=+cfvXnQ252Sk3CvCMBgvnfON/9JW4CEJ6dnfYcNSIZ0=; b=drpXsL6WrwlWEtu5U6K9V7+GSv1bl5WV3zItbXTJJMRi3d0xvmCIJL2lkpiLDnR5W89Nn8YWXr+gtPJPOmTw3mLmHrt0x/hoe2d2yBpcXupMj+7/gkXifayIK+HpwPeM4mMEvBNPuZNliYJ0dLqzm8BrWDxYT5HrE7ave8hIvxykz+CtWDuGX8VfdGtJXo8TSI0Js+L3w6uvkLxWKO6aOMcoXgZMN0wdlLHwOq1oknBgYRG6DxJpbRPswCVlh4n0JvDK1olRqHH9dRzoOTykbDD0CeCYZBohAXDJGD8q8/w1+vmkBEt1XAZqhDzPoJzuacHQMP3yBPQQWMtmnLg2Gw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=+cfvXnQ252Sk3CvCMBgvnfON/9JW4CEJ6dnfYcNSIZ0=; b=VSmoN25VbvXNAwqcopjQ06wCFJP4fzl8wm1hD1/lhzqe03oq7rhjb60vfVdLBr5ihMpK49vMI3UWu2R/oGjBtwhJYJ6QJ4PwEC+GX6iGLWnizRJchJ8EvnuxxFfg1UgjFjnC0sh6avR6pj75U8d5tDs1XRq9AvJ5ufIB+b2AIIJsX5oLFCmOUjw/osMYb4ZTa3rVSmZOljZglWo4k+29krIDzUAhLX/w6IfR4hpMys1Ns8WjU/9OIO0qw8Ao4AW93xuuOFib9YYBqv4qzgcwUxXEI/WJdmAD3Y39IvRuZgT7/1ZcOtycVWliCjWPax+cKtWknXvnmx734Rp0yUJ/rA== Received: from BN0PR04CA0071.namprd04.prod.outlook.com (2603:10b6:408:ea::16) by DS0PR12MB6463.namprd12.prod.outlook.com (2603:10b6:8:c5::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.15; Wed, 22 Feb 2023 17:50:27 +0000 Received: from BL02EPF0000C405.namprd05.prod.outlook.com (2603:10b6:408:ea:cafe::e1) by BN0PR04CA0071.outlook.office365.com (2603:10b6:408:ea::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21 via Frontend Transport; Wed, 22 Feb 2023 17:50:27 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C405.mail.protection.outlook.com (10.167.241.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:50:26 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:14 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:13 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:08 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 09/20] util: Extend iova_tree_foreach() to take data argument Date: Wed, 22 Feb 2023 19:49:04 +0200 Message-ID: <20230222174915.5647-10-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C405:EE_|DS0PR12MB6463:EE_ X-MS-Office365-Filtering-Correlation-Id: a4f67b00-9884-4c8e-f535-08db14fd4810 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: juqMoWJEE53jfOyF0WTUM4PUM7/Ms5yeWdUDoqK5eNyuP2hdaPnLJg5a+VUJRhnNyoEq0RPCuubYBK96UcURdrUC+lRSiWloBcQJGJKHKYastJxSHVCsXb79Qu8BoNr9qcbopNWe38kUfIMndUrAwhc7jADsJP4F8Zg1sVkoUNM5NtMZBKfzFLUXpdSs2oFVsq8iL7oAOQSVXPeoJtUFdlnuEvsw+TKlE5/7kCy4fbXN+lHHp/dpsPoIvyail8D4IrWA2+qGBBspwlK2YiBDl9hW3L6WRXDUWBPWvYga23bqa2I1FmHalYbmApfiaz4oDqEPJ3GagfhbY77MWRPv1Eug6cvXPoUrxb1DLNIkHjTcU5i4uZQKJ5TIo9qJ5WtK6spr6ZACsnqsb2dhlmafpUdfBBBE+UtSWvPuiFYv5/ZwkpuZTR9p5rYvDv1dyfrQ5kKCRH0rfoo9ASWPvKJOKnS7rIUyXZIT8Wm+/QNX0KhwpLco2zk8wYVPAP4tLCvyKixOxNvk1l5a+NEtRbBI3DhcSTK13S2RTJOfWtiVZdtUCjIJwE7o0DwaHnBaZonoz+FYIVQsRQZXSgV/otBtb539pRPPBixkVaQw7J1Feg0rtzgaGJ5YTYOu63q/jnp4rQ7OofR/K4t7awWnrfvbVYEni36CjOTlJKv6hxlvfMuhA8lpdvA8DsqTlQWRJtk/T+jCmX4Fcj1prt+HLfGkxA== X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(376002)(396003)(39860400002)(136003)(346002)(451199018)(36840700001)(46966006)(40470700004)(478600001)(40480700001)(70206006)(36756003)(316002)(40460700003)(54906003)(6916009)(70586007)(8676002)(82310400005)(6666004)(8936002)(7416002)(5660300002)(36860700001)(2906002)(41300700001)(1076003)(82740400003)(83380400001)(2616005)(7636003)(26005)(336012)(47076005)(426003)(356005)(86362001)(7696005)(4326008)(186003); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:26.9378 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a4f67b00-9884-4c8e-f535-08db14fd4810 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C405.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6463 Received-SPF: softfail client-ip=2a01:111:f400:7e88::620; envelope-from=avihaih@nvidia.com; helo=NAM10-DM6-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Joao Martins Extend iova_tree_foreach() to take data argument to be passed and used by the iterator. While at it, fix a documentation error: The documentation says iova_tree_foreach() returns a value even though it is a void function. Signed-off-by: Joao Martins Acked-by: Peter Xu --- include/qemu/iova-tree.h | 8 +++++--- util/iova-tree.c | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/include/qemu/iova-tree.h b/include/qemu/iova-tree.h index 7bb80783ce..1332dce014 100644 --- a/include/qemu/iova-tree.h +++ b/include/qemu/iova-tree.h @@ -38,7 +38,7 @@ typedef struct DMAMap { hwaddr size; /* Inclusive */ IOMMUAccessFlags perm; } QEMU_PACKED DMAMap; -typedef gboolean (*iova_tree_iterator)(DMAMap *map); +typedef gboolean (*iova_tree_iterator)(DMAMap *map, gpointer data); /** * iova_tree_new: @@ -129,12 +129,14 @@ const DMAMap *iova_tree_find_address(const IOVATree *tree, hwaddr iova); * * @tree: the iova tree to iterate on * @iterator: the interator for the mappings, return true to stop + * @data: data to be passed to the iterator * * Iterate over the iova tree. * - * Return: 1 if found any overlap, 0 if not, <0 if error. + * Return: None. */ -void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator); +void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator, + gpointer data); /** * iova_tree_alloc_map: diff --git a/util/iova-tree.c b/util/iova-tree.c index 6141a6229b..9845427b86 100644 --- a/util/iova-tree.c +++ b/util/iova-tree.c @@ -42,6 +42,11 @@ typedef struct IOVATreeFindIOVAArgs { const DMAMap *result; } IOVATreeFindIOVAArgs; +typedef struct IOVATreeIterator { + iova_tree_iterator fn; + gpointer data; +} IOVATreeIterator; + /** * Iterate args to the next hole * @@ -151,17 +156,22 @@ int iova_tree_insert(IOVATree *tree, const DMAMap *map) static gboolean iova_tree_traverse(gpointer key, gpointer value, gpointer data) { - iova_tree_iterator iterator = data; + IOVATreeIterator *iterator = data; DMAMap *map = key; g_assert(key == value); - return iterator(map); + return iterator->fn(map, iterator->data); } -void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator) +void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator, + gpointer data) { - g_tree_foreach(tree->tree, iova_tree_traverse, iterator); + IOVATreeIterator arg = { + .fn = iterator, + .data = data, + }; + g_tree_foreach(tree->tree, iova_tree_traverse, &arg); } void iova_tree_remove(IOVATree *tree, DMAMap map) From patchwork Wed Feb 22 17:49:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149405 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 229A9C64ED6 for ; Wed, 22 Feb 2023 17:51:32 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGf-00012g-5x; Wed, 22 Feb 2023 12:51:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGW-0000vm-U7 for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:52 -0500 Received: from mail-dm6nam12hn2030a.outbound.protection.outlook.com ([2a01:111:f400:fe59::30a] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGU-0004dp-2Y for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:51 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SF7jjATkpn+akOIF1fw4Vrr3xvfgReJ0AQ33fm3vm4voj1Q6JY/NQWqjCysk9kps1M88BrmRrTmPW089vS2sEm/MkjUgXcLQW5U+6TMfjDqWBV41DmBlrKu2dp63D7TwtWv8lQjjmXvbHkm7va2dz55JZX9cw+uTNT8+PkluvOl7OXZmLXKI/6YvKD5VjiRCuROdqfSHwWijZ4Co4bfrouEFqXcF6mgUsLRW95JJjzjdrF4HHJZmeYi3onhRzaKXGryZOwJlWJFtY13Gu5NjHMaqxaGJ0JL3Q6u0kUZ1ygvN8eDDwkeWyxJvsVHzPlVFIQrKYsm0pAehxvxu/qn7FQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=8w7zw2ymiu/KVwZjfJB71BuHrNJxdSTywyvrDCDXxlM=; b=LrvK0xWMS/8pl45hyHHB3OEvwWZtyHdAy6/xGby8OoD5FZXY34uoCNfPUJCyiKoWYyeFyUUkUIo68Nqs6i4jCD5+awCYCbuJjpCVjoILa4n0ojqXE0AYOA3qZcKYXOaLcvxv/PQXvJm6+sX9U+sF3vQmI07DaEBVsowe5z10z+qpf0H0sz3nvT1S/s0FxGuOI20m0LrVh+BAJsmB/6rWn0kyA8D0EwNEbnseOGku+c5b21Lfg5v2ub6bAsrrftgZgFxS7ZlYqYwsmbeDYzNLG+vxVHeaRNLCx+zuoE6+9xB13g+5JXe9nbtmjsWPWpGQGQhghONITzfFDUInz+EAWw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=8w7zw2ymiu/KVwZjfJB71BuHrNJxdSTywyvrDCDXxlM=; b=LQCboAaqEUqsao287Ewsag8Lt8srn7mELF2pRATXxyflRRbjKawiMYCThAnPTlSLnJI1HRIm3d/PhLpiRxfNQNWl7pGUEiAvvX+IFPJdF2Ce/S9WR3/Dj/9HNB3BRvmZZEeak44dpYrbXushyM0pk6YZPX/gSvCnNPEDVnqTuLX5p4q++wCP6kqo3B/Z4X+4KulzHmspF6QsW4Duj8Nz9vUZ6hMZ+C9ZmiMzvIfeXqGLjqwrDXmFU0bHq2wjTz9ZdLHpKCc6S5YVttKrYYFl11xxx+3rzHv/1a3hCLZ6d7EB368QHeMUXpw4AMRR3wAXPxal/FEEQVrtuqU1RSkXCQ== Received: from MW4PR03CA0234.namprd03.prod.outlook.com (2603:10b6:303:b9::29) by DS7PR12MB8321.namprd12.prod.outlook.com (2603:10b6:8:ec::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19; Wed, 22 Feb 2023 17:50:46 +0000 Received: from CO1NAM11FT087.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b9:cafe::48) by MW4PR03CA0234.outlook.office365.com (2603:10b6:303:b9::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21 via Frontend Transport; Wed, 22 Feb 2023 17:50:46 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT087.mail.protection.outlook.com (10.13.174.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:50:46 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:20 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:19 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:14 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 10/20] vfio/common: Record DMA mapped IOVA ranges Date: Wed, 22 Feb 2023 19:49:05 +0200 Message-ID: <20230222174915.5647-11-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT087:EE_|DS7PR12MB8321:EE_ X-MS-Office365-Filtering-Correlation-Id: 4ce598b4-a8d2-47cd-8095-08db14fd53ad X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kF/hyc0NEpp/Lotia4M+cTQkKHw3Oeg3lc2RoG20Ajz0GqVAyPydIytrdnTt+pVtIXMoxqcrB0WSzcswCqsWi7CSUXJkdShakevtxWdbm+nvFbicnDQK/U9dMTqgACE2+O5cJD/yhCOosho6uNhDQxPnl1hBN/Exl+JIb6xcIGunXOgMIwGMra/23EjE+nf0TLOx/E/HZ8ZxwevJ3IOk9ES5Rkpd2mEjjJUnI13kgZaQCH+kjRHiX5tQ+bkn8Swt6P04hLq1AFhTky9FnTxOddAaIGYvTy2TrE/+y2h8XNWgVtng8jfusYHMgEuox7KT273kKRU0ygcMsUj63gVkQu8ElXBOymGRbsFtv6p7GP3gGE73m65DLuGqghjVn6Uzha5KEpKHvxBFuDzoj8X5WWcwQp1K0/VyyB6HRFNyWngRXF6+E43bVHcNbiRNwprA6p+lRvREtTM1k+uGhzloqbtfnbwK41L7Uiq9IBq4uH6Ztu05Ha6WQyA3zkGBFrexegtISLa0xTqAtPMbunWVeDgq2ljokaVNIN6MhSLI8k+dHYRFI1pgRpwcN8mWVjQMvJFb/FAIdQysDZS0driBzcUId8ccqlJlTFXWnlDwV6SrPGzGLoTj9mTYKjjCjBi0A9ep6nY5GFG94srNmPilVhZQnBgbZJRus25AZwFoj26hlK1E//weoQ8tvYE4DDdQNxg8oRKuDoVzUbU5FyD4PuEw5hkzV+TmJHIUlLyQ2PpTTGXBX1n7Szafo38bGv+NHhr+WM5jitkNH80SYGApxA== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(346002)(396003)(376002)(136003)(39860400002)(5400799012)(451199018)(46966006)(40470700004)(36840700001)(7696005)(2616005)(5660300002)(34020700004)(36860700001)(7416002)(6666004)(336012)(1076003)(26005)(186003)(54906003)(426003)(478600001)(47076005)(316002)(83380400001)(356005)(8676002)(40460700003)(41300700001)(6916009)(4326008)(40480700001)(8936002)(70586007)(70206006)(7636003)(36756003)(82310400005)(86362001)(82740400003)(2906002)(12100799015); DIR:OUT; SFP:1501; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:46.4968 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4ce598b4-a8d2-47cd-8095-08db14fd53ad X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT087.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB8321 Received-SPF: softfail client-ip=2a01:111:f400:fe59::30a; envelope-from=avihaih@nvidia.com; helo=NAM12-DM6-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Joao Martins According to the device DMA logging uAPI, IOVA ranges to be logged by the device must be provided all at once upon DMA logging start. As preparation for the following patches which will add device dirty page tracking, keep a record of all DMA mapped IOVA ranges so later they can be used for DMA logging start. Note that when vIOMMU is enabled DMA mapped IOVA ranges are not tracked. This is due to the dynamic nature of vIOMMU DMA mapping/unmapping. Following patches will address the vIOMMU case specifically. Signed-off-by: Joao Martins Signed-off-by: Avihai Horon Signed-off-by: Joao Martins Signed-off-by: Avihai Horon Signed-off-by: Joao Martins Signed-off-by: Avihai Horon --- include/hw/vfio/vfio-common.h | 3 ++ hw/vfio/common.c | 86 +++++++++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index ee55d442b4..6f36876ce0 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -23,6 +23,7 @@ #include "exec/memory.h" #include "qemu/queue.h" +#include "qemu/iova-tree.h" #include "qemu/notify.h" #include "ui/console.h" #include "hw/display/ramfb.h" @@ -92,6 +93,8 @@ typedef struct VFIOContainer { uint64_t max_dirty_bitmap_size; unsigned long pgsizes; unsigned int dma_max_mappings; + IOVATree *mappings; + QemuMutex mappings_mutex; QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list; diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 84f08bdbbb..6041da6c7e 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -44,6 +44,7 @@ #include "migration/blocker.h" #include "migration/qemu-file.h" #include "sysemu/tpm.h" +#include "qemu/iova-tree.h" VFIOGroupList vfio_group_list = QLIST_HEAD_INITIALIZER(vfio_group_list); @@ -426,6 +427,11 @@ void vfio_unblock_multiple_devices_migration(void) multiple_devices_migration_blocker = NULL; } +static bool vfio_have_giommu(VFIOContainer *container) +{ + return !QLIST_EMPTY(&container->giommu_list); +} + static void vfio_set_migration_error(int err) { MigrationState *ms = migrate_get_current(); @@ -499,6 +505,51 @@ static bool vfio_devices_all_running_and_mig_active(VFIOContainer *container) return true; } +static int vfio_record_mapping(VFIOContainer *container, hwaddr iova, + hwaddr size, bool readonly) +{ + DMAMap map = { + .iova = iova, + .size = size - 1, /* IOVATree is inclusive, so subtract 1 from size */ + .perm = readonly ? IOMMU_RO : IOMMU_RW, + }; + int ret; + + if (vfio_have_giommu(container)) { + return 0; + } + + WITH_QEMU_LOCK_GUARD(&container->mappings_mutex) { + ret = iova_tree_insert(container->mappings, &map); + if (ret) { + if (ret == IOVA_ERR_INVALID) { + ret = -EINVAL; + } else if (ret == IOVA_ERR_OVERLAP) { + ret = -EEXIST; + } + } + } + + return ret; +} + +static void vfio_erase_mapping(VFIOContainer *container, hwaddr iova, + hwaddr size) +{ + DMAMap map = { + .iova = iova, + .size = size - 1, /* IOVATree is inclusive, so subtract 1 from size */ + }; + + if (vfio_have_giommu(container)) { + return; + } + + WITH_QEMU_LOCK_GUARD(&container->mappings_mutex) { + iova_tree_remove(container->mappings, map); + } +} + static int vfio_dma_unmap_bitmap(VFIOContainer *container, hwaddr iova, ram_addr_t size, IOMMUTLBEntry *iotlb) @@ -599,6 +650,8 @@ static int vfio_dma_unmap(VFIOContainer *container, DIRTY_CLIENTS_NOCODE); } + vfio_erase_mapping(container, iova, size); + return 0; } @@ -612,6 +665,16 @@ static int vfio_dma_map(VFIOContainer *container, hwaddr iova, .iova = iova, .size = size, }; + int ret; + + ret = vfio_record_mapping(container, iova, size, readonly); + if (ret) { + error_report("vfio: Failed to record mapping, iova: 0x%" HWADDR_PRIx + ", size: 0x" RAM_ADDR_FMT ", ret: %d (%s)", + iova, size, ret, strerror(-ret)); + + return ret; + } if (!readonly) { map.flags |= VFIO_DMA_MAP_FLAG_WRITE; @@ -628,8 +691,12 @@ static int vfio_dma_map(VFIOContainer *container, hwaddr iova, return 0; } + ret = -errno; error_report("VFIO_MAP_DMA failed: %s", strerror(errno)); - return -errno; + + vfio_erase_mapping(container, iova, size); + + return ret; } static void vfio_host_win_add(VFIOContainer *container, @@ -2183,16 +2250,23 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, QLIST_INIT(&container->giommu_list); QLIST_INIT(&container->hostwin_list); QLIST_INIT(&container->vrdl_list); + container->mappings = iova_tree_new(); + if (!container->mappings) { + error_setg(errp, "Cannot allocate DMA mappings tree"); + ret = -ENOMEM; + goto free_container_exit; + } + qemu_mutex_init(&container->mappings_mutex); ret = vfio_init_container(container, group->fd, errp); if (ret) { - goto free_container_exit; + goto destroy_mappings_exit; } ret = vfio_ram_block_discard_disable(container, true); if (ret) { error_setg_errno(errp, -ret, "Cannot set discarding of RAM broken"); - goto free_container_exit; + goto destroy_mappings_exit; } switch (container->iommu_type) { @@ -2328,6 +2402,10 @@ listener_release_exit: enable_discards_exit: vfio_ram_block_discard_disable(container, false); +destroy_mappings_exit: + qemu_mutex_destroy(&container->mappings_mutex); + iova_tree_destroy(container->mappings); + free_container_exit: g_free(container); @@ -2382,6 +2460,8 @@ static void vfio_disconnect_container(VFIOGroup *group) } trace_vfio_disconnect_container(container->fd); + qemu_mutex_destroy(&container->mappings_mutex); + iova_tree_destroy(container->mappings); close(container->fd); g_free(container); From patchwork Wed Feb 22 17:49:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149398 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B9017C636D6 for ; Wed, 22 Feb 2023 17:51:02 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGQ-0000j0-6b; Wed, 22 Feb 2023 12:50:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGO-0000cA-4J for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:44 -0500 Received: from mail-dm3nam02on20612.outbound.protection.outlook.com ([2a01:111:f400:7e83::612] helo=NAM02-DM3-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGL-0004d2-Sa for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:50:43 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NeYAjPKoJ0CE3RySaNwCG/FUDujpVa7Cs6Pb9ZjQWyoR7VPOqCeVTnqDVVqZvy9QZrPNSQD15fUtCP/Cyy8C4ilQh50VkL3ZF2sx1Lae0Lziagz2nw+AdMPZAJC5RQbeE2c92jXa6rqfstJDQ1BOgZDpRgV204ZnlsQvy2Fa3+VYkPODG8J26o7WlCQVTQbfYhWlX+f61+I7K9aSNlPg6fAEB2SUJ+OBsZkAxqRsPHwGwra2CCF1i4/UeYRCZbXDnTZZXIXJZRjTVlEqusBf0kubk0Mc27ZLlnU1kfsIzVH9RPmabURuvzlTWyxeWVZ74TywmXZPqHI2Y20r8ZL9Ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ogTmVdDZmLID0Fa/54YdVcKIXizIRBlUUe14ENDtHrM=; b=U4iBVf7CTTaXw6jp5XnF7820vHMQwwXKPLnlb5fzuh25yIYUlN732063g1nfzaaxjkGWAHtTTiqCs9uxZ6neO3FzM+Zyf2JCjwwz1cR3cybEXBw7QdLTWRpnFgfCqgpau7Q0twwp7PXdQXjAs7F2qcbpYbJywXlp+K4ZzNxFQOkNit/4PxBzMjF/UxsfApEXSqtME/E+AgjsBQ5QDLNRb0v4Vq17YVwbiPC8bpAoIxApDQS9KPBGuFBcAMVb7Zqy++UiP5FjKAfq5nLbXW09R+Zxd6UgUgImk+FAOqAPbroLUQQX3yhSqcSUml4g+TX6J249zD1L2KlF+Ke0GwLecQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ogTmVdDZmLID0Fa/54YdVcKIXizIRBlUUe14ENDtHrM=; b=hme4iFL+eB4ZuU4BqmbSlBQO9oB5I9pjd+MtdLemENX/ZKZUBFp9+1M0R9CHQquQp9QXNrFZbwIbbNp1JvKhpTnEnk8+Olb7RtWafn1xkxcJpFw+eCvZSdiE7kEURr9tjDSn78ilD/K1Aov8jYP+ynnetVCm4om/j2nvWwix0QnXIrUo40y5/E3m9JK8gFrmfWVhJTo18hzjhEj0ddmg+le9c6ASJq0Fdx3zCpERkCi3DqU+uG406cXxNKfcxfpoMV9RGNQuKxNk1jpD/sUnczhjof8ZhgJmCj2CvrT8Kvup63wAv+vDuyBj5IYjTuQ0rDUBMLP2pPhfAaSZmn3HEw== Received: from BN0PR04CA0124.namprd04.prod.outlook.com (2603:10b6:408:ed::9) by DM4PR12MB8521.namprd12.prod.outlook.com (2603:10b6:8:17e::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 17:50:38 +0000 Received: from BL02EPF0000C403.namprd05.prod.outlook.com (2603:10b6:408:ed:cafe::67) by BN0PR04CA0124.outlook.office365.com (2603:10b6:408:ed::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21 via Frontend Transport; Wed, 22 Feb 2023 17:50:38 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C403.mail.protection.outlook.com (10.167.241.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:50:38 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:25 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:25 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:20 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 11/20] vfio/common: Add device dirty page tracking start/stop Date: Wed, 22 Feb 2023 19:49:06 +0200 Message-ID: <20230222174915.5647-12-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C403:EE_|DM4PR12MB8521:EE_ X-MS-Office365-Filtering-Correlation-Id: 841fada8-49cd-4c93-609d-08db14fd4efa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TrlL4BFw2SIq+dOgrmfKNVasnjGHvYn7OBzSppIFNJ1aB6JyUiUfuXJi5Y13GHNPD09FOGjAiOckr1/jOgk9F0WtoMkhD9hLYkMgNtG7ARraq8MvjWYbE6ZGoLH8KpTaIeNpj9ux0y0TRVl8MDzBg3jW+J/QRHPSMZ/A709OxkyQkgPKjwHkcPCARwF8MNPtna/hz2G2JGqiAvkveXKSudWyDbKV2oveDUBd0yO24lx1rcigqdrD346nRKrG+MpyKUGjdYz4KLCt7WmCNmE8jg46XsfNrLqLK7TfRWzw6PwaIVqQHTyk5b3KpprpcJ/h1nyMskGHOU0ihwt991zNao5HYSP+dKhDCpnaGhBpDJdJFAjlteifb/CLuL18K7+s55UikRegq4RPa17Az5GCzRyfSMDRfcEGjtoSRtfnM2Xpi/TpZ19VUQOTSIuGVbxEcBEV4hj6sJc2CcrHGzL2iFYJOkvgOE50K0lBEZuRI90T0gQRHJg6wM2GgULoYPmUOKjaQWKyBJ1feOS5QYBqFqeP/Iwbd8Qh8DkKMxELzlXFqCn8TMSV7qbJzSa2rjoxLjIliVFueurLFTaC7/Z5seycSH1jSwucZxin0eEVhXSJfy/BfLY98sBgJtqAoQ3iD3sxgTvjfWWnjXzHTjQ8HOaeb0SINO4fwRavTzm06PTPi7y/RHVg9dQPHeap1hz5KJyCUyiDT+3ub4voFJM6sHEGUZzm9qIlgegbk4vZ510= X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(346002)(396003)(136003)(376002)(39860400002)(451199018)(46966006)(40470700004)(36840700001)(7696005)(82310400005)(26005)(336012)(186003)(36756003)(6916009)(478600001)(2616005)(426003)(86362001)(70206006)(83380400001)(70586007)(8676002)(1076003)(6666004)(47076005)(54906003)(316002)(4326008)(41300700001)(7416002)(8936002)(36860700001)(5660300002)(82740400003)(40480700001)(2906002)(7636003)(356005)(40460700003)(14143004); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:38.5182 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 841fada8-49cd-4c93-609d-08db14fd4efa X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C403.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB8521 Received-SPF: softfail client-ip=2a01:111:f400:7e83::612; envelope-from=avihaih@nvidia.com; helo=NAM02-DM3-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Joao Martins Add device dirty page tracking start/stop functionality. This uses the device DMA logging uAPI to start and stop dirty page tracking by device. Device dirty page tracking is used only if all devices within a container support device dirty page tracking. Signed-off-by: Joao Martins Signed-off-by: Avihai Horon --- include/hw/vfio/vfio-common.h | 2 + hw/vfio/common.c | 211 +++++++++++++++++++++++++++++++++- 2 files changed, 211 insertions(+), 2 deletions(-) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 6f36876ce0..1f21e1fa43 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -149,6 +149,8 @@ typedef struct VFIODevice { VFIOMigration *migration; Error *migration_blocker; OnOffAuto pre_copy_dirty_page_tracking; + bool dirty_pages_supported; + bool dirty_tracking; } VFIODevice; struct VFIODeviceOps { diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 6041da6c7e..740153e7d7 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -473,6 +473,22 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainer *container) return true; } +static bool vfio_devices_all_device_dirty_tracking(VFIOContainer *container) +{ + VFIOGroup *group; + VFIODevice *vbasedev; + + QLIST_FOREACH(group, &container->group_list, container_next) { + QLIST_FOREACH(vbasedev, &group->device_list, next) { + if (!vbasedev->dirty_pages_supported) { + return false; + } + } + } + + return true; +} + /* * Check if all VFIO devices are running and migration is active, which is * essentially equivalent to the migration being in pre-copy phase. @@ -1404,13 +1420,192 @@ static int vfio_set_dirty_page_tracking(VFIOContainer *container, bool start) return ret; } +static int vfio_devices_dma_logging_set(VFIOContainer *container, + struct vfio_device_feature *feature) +{ + bool status = (feature->flags & VFIO_DEVICE_FEATURE_MASK) == + VFIO_DEVICE_FEATURE_DMA_LOGGING_START; + VFIODevice *vbasedev; + VFIOGroup *group; + int ret = 0; + + QLIST_FOREACH(group, &container->group_list, container_next) { + QLIST_FOREACH(vbasedev, &group->device_list, next) { + if (vbasedev->dirty_tracking == status) { + continue; + } + + ret = ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature); + if (ret) { + ret = -errno; + error_report("%s: Failed to set DMA logging %s, err %d (%s)", + vbasedev->name, status ? "start" : "stop", ret, + strerror(errno)); + goto out; + } + vbasedev->dirty_tracking = status; + } + } + +out: + return ret; +} + +static int vfio_devices_dma_logging_stop(VFIOContainer *container) +{ + uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature), + sizeof(uint64_t))] = {}; + struct vfio_device_feature *feature = (struct vfio_device_feature *)buf; + + feature->argsz = sizeof(buf); + feature->flags = VFIO_DEVICE_FEATURE_SET; + feature->flags |= VFIO_DEVICE_FEATURE_DMA_LOGGING_STOP; + + return vfio_devices_dma_logging_set(container, feature); +} + +static gboolean vfio_device_dma_logging_range_add(DMAMap *map, gpointer data) +{ + struct vfio_device_feature_dma_logging_range **out = data; + struct vfio_device_feature_dma_logging_range *range = *out; + + range->iova = map->iova; + /* IOVATree is inclusive, DMA logging uAPI isn't, so add 1 to length */ + range->length = map->size + 1; + + *out = ++range; + + return false; +} + +static gboolean vfio_iova_tree_get_first(DMAMap *map, gpointer data) +{ + DMAMap *first = data; + + first->iova = map->iova; + first->size = map->size; + + return true; +} + +static gboolean vfio_iova_tree_get_last(DMAMap *map, gpointer data) +{ + DMAMap *last = data; + + last->iova = map->iova; + last->size = map->size; + + return false; +} + +static struct vfio_device_feature * +vfio_device_feature_dma_logging_start_create(VFIOContainer *container) +{ + struct vfio_device_feature *feature; + size_t feature_size; + struct vfio_device_feature_dma_logging_control *control; + struct vfio_device_feature_dma_logging_range *ranges; + unsigned int max_ranges; + unsigned int cur_ranges; + + feature_size = sizeof(struct vfio_device_feature) + + sizeof(struct vfio_device_feature_dma_logging_control); + feature = g_malloc0(feature_size); + feature->argsz = feature_size; + feature->flags = VFIO_DEVICE_FEATURE_SET; + feature->flags |= VFIO_DEVICE_FEATURE_DMA_LOGGING_START; + + control = (struct vfio_device_feature_dma_logging_control *)feature->data; + control->page_size = qemu_real_host_page_size(); + + QEMU_LOCK_GUARD(&container->mappings_mutex); + + /* + * DMA logging uAPI guarantees to support at least num_ranges that fits into + * a single host kernel page. To be on the safe side, use this as a limit + * from which to merge to a single range. + */ + max_ranges = qemu_real_host_page_size() / sizeof(*ranges); + cur_ranges = iova_tree_nnodes(container->mappings); + control->num_ranges = (cur_ranges <= max_ranges) ? cur_ranges : 1; + ranges = g_try_new0(struct vfio_device_feature_dma_logging_range, + control->num_ranges); + if (!ranges) { + g_free(feature); + errno = ENOMEM; + + return NULL; + } + + control->ranges = (uint64_t)ranges; + if (cur_ranges <= max_ranges) { + iova_tree_foreach(container->mappings, + vfio_device_dma_logging_range_add, &ranges); + } else { + DMAMap first, last; + + iova_tree_foreach(container->mappings, vfio_iova_tree_get_first, + &first); + iova_tree_foreach(container->mappings, vfio_iova_tree_get_last, &last); + ranges->iova = first.iova; + /* IOVATree is inclusive, DMA logging uAPI isn't, so add 1 to length */ + ranges->length = (last.iova - first.iova) + last.size + 1; + } + + return feature; +} + +static void vfio_device_feature_dma_logging_start_destroy( + struct vfio_device_feature *feature) +{ + struct vfio_device_feature_dma_logging_control *control = + (struct vfio_device_feature_dma_logging_control *)feature->data; + struct vfio_device_feature_dma_logging_range *ranges = + (struct vfio_device_feature_dma_logging_range *)control->ranges; + + g_free(ranges); + g_free(feature); +} + +static int vfio_devices_dma_logging_start(VFIOContainer *container) +{ + struct vfio_device_feature *feature; + int ret; + + feature = vfio_device_feature_dma_logging_start_create(container); + if (!feature) { + return -errno; + } + + ret = vfio_devices_dma_logging_set(container, feature); + if (ret) { + vfio_devices_dma_logging_stop(container); + } + + vfio_device_feature_dma_logging_start_destroy(feature); + + return ret; +} + static void vfio_listener_log_global_start(MemoryListener *listener) { VFIOContainer *container = container_of(listener, VFIOContainer, listener); int ret; - ret = vfio_set_dirty_page_tracking(container, true); + if (vfio_devices_all_device_dirty_tracking(container)) { + if (vfio_have_giommu(container)) { + /* Device dirty page tracking currently doesn't support vIOMMU */ + return; + } + + ret = vfio_devices_dma_logging_start(container); + } else { + ret = vfio_set_dirty_page_tracking(container, true); + } + if (ret) { + error_report("vfio: Could not start dirty page tracking, err: %d (%s)", + ret, strerror(-ret)); vfio_set_migration_error(ret); } } @@ -1420,8 +1615,20 @@ static void vfio_listener_log_global_stop(MemoryListener *listener) VFIOContainer *container = container_of(listener, VFIOContainer, listener); int ret; - ret = vfio_set_dirty_page_tracking(container, false); + if (vfio_devices_all_device_dirty_tracking(container)) { + if (vfio_have_giommu(container)) { + /* Device dirty page tracking currently doesn't support vIOMMU */ + return; + } + + ret = vfio_devices_dma_logging_stop(container); + } else { + ret = vfio_set_dirty_page_tracking(container, false); + } + if (ret) { + error_report("vfio: Could not stop dirty page tracking, err: %d (%s)", + ret, strerror(-ret)); vfio_set_migration_error(ret); } } From patchwork Wed Feb 22 17:49:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149401 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4F99DC61DA4 for ; Wed, 22 Feb 2023 17:51:15 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGj-0001Si-Pd; Wed, 22 Feb 2023 12:51:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGg-00018I-4u for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:03 -0500 Received: from mail-bn8nam12hn2032c.outbound.protection.outlook.com ([2a01:111:f400:fe5b::32c] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGe-0004gO-3i for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:01 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Oy5G/QaNEC/+4QGxm7rXV1mMi0iOBgKMjH9mTNSz97D5yX2xliJYgB8oYYyWTtWmMlXxNUXo2pTbsquRBvcUeEpvklKqWWJwB4udjNuOgSqxlZtUk1Q4LpOtBHSF3qfLOK6r+GBm2sjohNf3md3RTcmKOanQYNfC3YcuEIRx2D+clMT+A+oytPZ2Q4hBCW6iEByhOWPfdy/p0kzby/Ls9ctzJraNZy+3DIqgac96h8hZIuGB1q4iC2pueZfoMd6c8r4CFHqT6h+/lkqfAmO+3yPr7AYAqM9dzOtFARTDcfDYTmEHf/AgOkHEITfBIJNwHY29YYpXIjhlhdiTFI+22g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wRhfPjoGAzjNOGDndAf9hTbw3NCGkhDTCYNw83QQs8k=; b=JAVhHuJr87lL6Rvb6P5VW7h/m2kXNNXQySHV0iKEvQx8fB6cTSUWl82kzAjRGRyrUWwVlwoSu90P6cuMwarvatJrWTgeXeY2zQwJCQZVeYtMKBB8UnbulSjHNJVqQftl5cj6iFQQ0CqLIEYNOpyU0PNkZBf9wRArUuK1QlTsrnlDAQDO6FpTxoO1ytsNNVKO6YTZDjOBbHecj3lmBcrXNkpyklQk43I+J5RF/dPSBfC7AQruP+dUSBlDmh6U545v4i7YlEYd2kuKEpXV7NiWxYyBR8XO+gNlv/NanAPCFFKTazzKfVCcrhCl6k2UEJpmEvTd9ch4z0BE4ht4LZtAOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wRhfPjoGAzjNOGDndAf9hTbw3NCGkhDTCYNw83QQs8k=; b=hp0kFU8UVj8Yd4RZTYdTXi0rt0gEfbj0c7lmYgLdmhDpltp/d/Y9ALkAsGzMVU/4wqe8NAI3RDO3UVxrKmgtobODv9WmxcvvT2WInHMcWWsbJuZfgfsLu0EcrJ/BSo1MgGAqtxG2lP26mT+STUiFxvx74HGbvlfAIw8pACSTt1hVqJkg39MZSwOe0KTo5RpU5KnXoVraFM+EB4IaQ25EsVQU8HLgDaPmaa7RZ3z0H5pTI7NOBOvOZ7jXsmO2WyPPUky0lcl+RUNqqKoKW+Pw1ifowgOKovWE8h+WjszSiIGrpIp87cjgC6rze7rTkw53DLQ/v7VoHNqsfSP7tZVLVg== Received: from MW4P222CA0008.NAMP222.PROD.OUTLOOK.COM (2603:10b6:303:114::13) by SA0PR12MB4510.namprd12.prod.outlook.com (2603:10b6:806:94::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19; Wed, 22 Feb 2023 17:50:57 +0000 Received: from CO1NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:303:114:cafe::90) by MW4P222CA0008.outlook.office365.com (2603:10b6:303:114::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:50:56 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT043.mail.protection.outlook.com (10.13.174.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.20 via Frontend Transport; Wed, 22 Feb 2023 17:50:56 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:31 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:31 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:25 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 12/20] vfio/common: Extract code from vfio_get_dirty_bitmap() to new function Date: Wed, 22 Feb 2023 19:49:07 +0200 Message-ID: <20230222174915.5647-13-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT043:EE_|SA0PR12MB4510:EE_ X-MS-Office365-Filtering-Correlation-Id: 953b4672-1012-4dbe-7369-08db14fd59aa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QMSF/Zr5M1M+01jBQhI9QuZ5jLL/Jaj+rL2vMzTCuJvzlZpeCZSeItT8DiobSgkAK4pnC4qV8/DSjN9jvCZPsnJz0TdBvmgRB2ZYAV9fnOfTlaaalVfjcYIbO8AJjOj+FwL98iExUvkJhLTLDcFQL0inRvjquv8d6RjRFz8kgTmIh6HbRMX4XzOCJ3nWtuc3DQUGdWHFuOClRJdnXpDbBFF2J9bB3/gBBdp3brweKMfP3QQJROoKPOTWChY93AiootxeLExTrRYJm7Ca3qpWLl373BqqcSPpcjumnWeMbX9xParSIOkEk+kyc8IWKBIWm3JUCJFulMOtD0KumacZkgAl3GfnehCAkJ/EbV7sEKf0DSnhIdhr4j1qioz1Xd6DFCwa57P23tBy5diT/mHjjYRBV2CrZflRVsFJZeImXjFz8wZVxwajQ3sTfGDf+VLb0COEjxFD1dfBxw84dxF2CGOBpJQDz4o7zQiRF5xAMyqkarzOtNuavRQR/xS9kQ2jfibWHDZ6tG+UWE9/4ZDfn8aNyiMRMZQxxnChlvgDUyJbnnwh+RE0FWFUzlbj9Wt9vGIha3HlKKEpyNTbovdoiE+tsnGa3tEasIW6kJ6T+GGa4HlAfCFQnVV8e/oD7GhnUTArLCHXilIMXpU26uN/7u56RUMzvJBN+xWUv7hLFuJCIot6qrL96as4B8ULZaM9lH0zfZaye39t8Ot/tR+sqBrLC7EBsCdNQFHboBMGidyKr9x4v9C/nLaEhmejgFjcj/7t3pxiHADIRJFBjGN6WZ+FnBx1jvEvbrpBVFk+Of8= X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(136003)(346002)(376002)(39860400002)(396003)(451199018)(5400799012)(40470700004)(46966006)(36840700001)(66899018)(82310400005)(40460700003)(36860700001)(1076003)(34020700004)(426003)(47076005)(83380400001)(7696005)(36756003)(7636003)(6666004)(316002)(82740400003)(54906003)(356005)(2616005)(40480700001)(478600001)(186003)(336012)(26005)(2906002)(7416002)(8936002)(41300700001)(5660300002)(6916009)(8676002)(4326008)(70206006)(70586007)(86362001)(14143004)(12100799015); DIR:OUT; SFP:1501; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:56.5744 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 953b4672-1012-4dbe-7369-08db14fd59aa X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT043.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4510 Received-SPF: softfail client-ip=2a01:111:f400:fe5b::32c; envelope-from=avihaih@nvidia.com; helo=NAM12-BN8-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Extract the VFIO_IOMMU_DIRTY_PAGES ioctl code in vfio_get_dirty_bitmap() to its own function. This will help the code to be more readable after next patch will add device dirty page bitmap sync functionality. Signed-off-by: Avihai Horon --- hw/vfio/common.c | 53 ++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 740153e7d7..3ab5d8d442 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1633,26 +1633,13 @@ static void vfio_listener_log_global_stop(MemoryListener *listener) } } -static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, - uint64_t size, ram_addr_t ram_addr) +static int vfio_query_dirty_bitmap(VFIOContainer *container, VFIOBitmap *vbmap, + hwaddr iova, hwaddr size) { struct vfio_iommu_type1_dirty_bitmap *dbitmap; struct vfio_iommu_type1_dirty_bitmap_get *range; - VFIOBitmap *vbmap; int ret; - if (!container->dirty_pages_supported) { - cpu_physical_memory_set_dirty_range(ram_addr, size, - tcg_enabled() ? DIRTY_CLIENTS_ALL : - DIRTY_CLIENTS_NOCODE); - return 0; - } - - vbmap = vfio_bitmap_alloc(size); - if (!vbmap) { - return -errno; - } - dbitmap = g_malloc0(sizeof(*dbitmap) + sizeof(*range)); dbitmap->argsz = sizeof(*dbitmap) + sizeof(*range); @@ -1676,16 +1663,42 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, error_report("Failed to get dirty bitmap for iova: 0x%"PRIx64 " size: 0x%"PRIx64" err: %d", (uint64_t)range->iova, (uint64_t)range->size, errno); - goto err_out; + } + + g_free(dbitmap); + + return ret; +} + +static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, + uint64_t size, ram_addr_t ram_addr) +{ + VFIOBitmap *vbmap; + int ret; + + if (!container->dirty_pages_supported) { + cpu_physical_memory_set_dirty_range(ram_addr, size, + tcg_enabled() ? DIRTY_CLIENTS_ALL : + DIRTY_CLIENTS_NOCODE); + return 0; + } + + vbmap = vfio_bitmap_alloc(size); + if (!vbmap) { + return -errno; + } + + ret = vfio_query_dirty_bitmap(container, vbmap, iova, size); + if (ret) { + goto out; } cpu_physical_memory_set_dirty_lebitmap(vbmap->bitmap, ram_addr, vbmap->pages); - trace_vfio_get_dirty_bitmap(container->fd, range->iova, range->size, - range->bitmap.size, ram_addr); -err_out: - g_free(dbitmap); + trace_vfio_get_dirty_bitmap(container->fd, iova, size, vbmap->size, + ram_addr); +out: vfio_bitmap_dealloc(vbmap); return ret; From patchwork Wed Feb 22 17:49:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149402 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 669DDC61DA4 for ; Wed, 22 Feb 2023 17:51:21 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGv-0002RP-PP; Wed, 22 Feb 2023 12:51:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGt-00024J-Rj for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:15 -0500 Received: from mail-dm6nam10hn2030d.outbound.protection.outlook.com ([2a01:111:f400:7e88::30d] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGk-0004hI-EV for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:08 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CaD3tmD4HuZoRi9XNDC3AJZjFuCL1fUKR/moR4iZ0aUI8Kt48MoV4S8lDm/TBCjBofVonLX+SqSFtNrvjaRMQjM0tpJBLErgyhh6SAKmTu+JgYrX+cnpE7wzkBBMuDFGwYvf8/1tDr45f4ENHHnt7vz4OBlYAvTZwGkSsWaTR8XOt8lYji4jV/t2b63NkHcHEz8E9NPVeAUlyEgIGC3fdf7RmSz5FJl3a348AH86/R/96PoV1bDmJnwev+a0CUgB0IKuIqjtyJTWBsrmM+RQwokZmmnw5giO89h7Ntwt28DCVcbxQZfovOik9JolhArxR/5Fr+LTdQdNo9rUctnBng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=LjYuqeL3uW+7Wy53zNDLXMUQdB4XWmOklx0hNC08KVU=; b=TlGf9PnOufQGa/ixrDToG/o3ZWDe/vK2rbw7InfYZDN67fNCDieEYgXEbOt2XDKqRUFXTJzIz3I749ZShma35mBPy07ZyI+Itl/qEnRYN4No0Y6biZVQYgO0dKKGaCQ/8guiwlCOLTnAAwHjTrF1Cya39qT+5YqI7IyKXVxnp0k3y4JmAX5VrXnmmYygGXUAzly3HwHhqgHunHRKAkaCPbUTVDWEjS+f9jwW59oqB33P80wUxf9BvWGNefZrkk5jSXuk4V8S6S8PKfXpi54/CmbEioZQfwtqtqGg6WdgC5XupItLJVWN/80rj0OueNLVdPpwpFGZAIetxRkJF2nbLw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LjYuqeL3uW+7Wy53zNDLXMUQdB4XWmOklx0hNC08KVU=; b=kZ/Aroa0YHBw03FaDxPBYA4/kmjRXZNOsPGLQzLQA9dADuUfbAX+LAE2nY6NsownnCA10op5DwpNdM2efnhzjcpxtHCiFHkCZ+0aZ3Zm5jG3PRWunZWrflrWdSeKDD4jEZ2PhhpVluIduPT2zj2TwcN+goGcQW/pGOoXlw6LJAntcW8e++EnZAlUIEGenZHWNbkNERwqkKN3b6iArT+WHK2DJqMpjSZiWaEuajs2zyODYDxsPc66PV8YrUfFOl4gtEJz89Tw+T1MPSbh4jrG3bxLm4VOrn9gVhaaL5HPTVGxWw6oMQ8E9RyyPtbV/PH0JZ+O5FvsmoUqd3gOfS8rbw== Received: from MW4PR04CA0357.namprd04.prod.outlook.com (2603:10b6:303:8a::32) by IA0PR12MB7673.namprd12.prod.outlook.com (2603:10b6:208:435::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19; Wed, 22 Feb 2023 17:51:01 +0000 Received: from CO1NAM11FT077.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8a:cafe::ad) by MW4PR04CA0357.outlook.office365.com (2603:10b6:303:8a::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.20 via Frontend Transport; Wed, 22 Feb 2023 17:51:01 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT077.mail.protection.outlook.com (10.13.175.55) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.18 via Frontend Transport; Wed, 22 Feb 2023 17:51:00 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:37 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:36 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:31 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 13/20] vfio/common: Add device dirty page bitmap sync Date: Wed, 22 Feb 2023 19:49:08 +0200 Message-ID: <20230222174915.5647-14-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT077:EE_|IA0PR12MB7673:EE_ X-MS-Office365-Filtering-Correlation-Id: ae066b1b-4d82-4387-42a9-08db14fd5c41 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MZ/7tZiLNkYFxzg4eHBWbJmfrDmYBlQ1V+B4d8A0ZIij/sF/3Dyxca/9BG804zTD1bp3J/tENjqGfdweADkz31aTMXlMxL1gBy23IZBrjYh03gd6OJ5/sCNB7UJaK8c9kJvP/ZdcyQ8GQ3E7493KOfmfGTzIIbq/uT8Ay9mHOzMh+DGDOofT/Hy832uCPBbiHSKH/wQGmr5/O7VTdkA48MYUptisxHjLRjqfK24TrQWmGAwZRnXnrOvZpTvsmKAXuyXiTBgki64/aQd88EmMv2vbSalEwrlx49l8rndFSEgNyzzBreowsmYuzcbItyM9GrJgRhIdbaBcpzaxBIEiqFkuX1Z1lGdlceVQzr45GBWtiTWcPAeHyPHdhClxlxGzIAMRNw8KSGgHzpaFhGb1AibPtU66HclMN3t9/WbhaSbWvQAHmifv3hvwB9PG/jHMuJ9fnDSsCIkYH3YduyS59LoEDgAA85f65dg5J9NlRprtjA6Umi/Zz4IWkx/zST6BLLZGlgY66FkR27t0wrv26cv62OFUnckFQiWWBGQmGX69YTI81N5IBm4kZ+nmPSWcU/zAN/wX9BpYQaJ+gKyafxKXKkEOKSD7r5cbbf9Q9C4gWQ9Z5Z+CGHx+MOIHMNNCmPUxeckM7+sCoax+sH1powijTsWdZ5eQdVwOHY6bh6q/Npa+ayIibfyAV6NJNqssxDZ007MOsUx0Jg4TwqC1W0h/do9X/MfmgS69TeVlaJOGS5oQBxcKAjp0UzPd3LwmxR+9HgW909LvppcfNfL2tmS8H9C6ix0rVmDq0uVuqo0= X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(346002)(39860400002)(5400799012)(451199018)(46966006)(40470700004)(36840700001)(426003)(47076005)(83380400001)(36756003)(2906002)(5660300002)(7416002)(1076003)(54906003)(86362001)(41300700001)(478600001)(6666004)(7696005)(66899018)(82310400005)(36860700001)(34020700004)(7636003)(26005)(2616005)(336012)(40480700001)(186003)(356005)(8676002)(40460700003)(6916009)(316002)(4326008)(8936002)(82740400003)(70586007)(70206006)(14143004)(12100799015); DIR:OUT; SFP:1501; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:51:00.9181 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ae066b1b-4d82-4387-42a9-08db14fd5c41 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT077.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7673 Received-SPF: softfail client-ip=2a01:111:f400:7e88::30d; envelope-from=avihaih@nvidia.com; helo=NAM10-DM6-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Joao Martins Add device dirty page bitmap sync functionality. This uses the device DMA logging uAPI to sync dirty page bitmap from the device. Device dirty page bitmap sync is used only if all devices within a container support device dirty page tracking. Signed-off-by: Joao Martins Signed-off-by: Avihai Horon --- hw/vfio/common.c | 95 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 9 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 3ab5d8d442..797eb2c26e 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -356,6 +356,9 @@ static void vfio_bitmap_dealloc(VFIOBitmap *vbmap) g_free(vbmap); } +static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, + uint64_t size, ram_addr_t ram_addr); + bool vfio_mig_active(void) { VFIOGroup *group; @@ -631,10 +634,16 @@ static int vfio_dma_unmap(VFIOContainer *container, .iova = iova, .size = size, }; + bool need_dirty_sync = false; + int ret; - if (iotlb && container->dirty_pages_supported && - vfio_devices_all_running_and_mig_active(container)) { - return vfio_dma_unmap_bitmap(container, iova, size, iotlb); + if (iotlb && vfio_devices_all_running_and_mig_active(container)) { + if (!vfio_devices_all_device_dirty_tracking(container) && + container->dirty_pages_supported) { + return vfio_dma_unmap_bitmap(container, iova, size, iotlb); + } + + need_dirty_sync = true; } while (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) { @@ -660,10 +669,12 @@ static int vfio_dma_unmap(VFIOContainer *container, return -errno; } - if (iotlb && vfio_devices_all_running_and_mig_active(container)) { - cpu_physical_memory_set_dirty_range(iotlb->translated_addr, size, - tcg_enabled() ? DIRTY_CLIENTS_ALL : - DIRTY_CLIENTS_NOCODE); + if (need_dirty_sync) { + ret = vfio_get_dirty_bitmap(container, iova, size, + iotlb->translated_addr); + if (ret) { + return ret; + } } vfio_erase_mapping(container, iova, size); @@ -1633,6 +1644,65 @@ static void vfio_listener_log_global_stop(MemoryListener *listener) } } +static int vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova, + hwaddr size, void *bitmap) +{ + uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) + + sizeof(struct vfio_device_feature_dma_logging_report), + sizeof(uint64_t))] = {}; + struct vfio_device_feature *feature = (struct vfio_device_feature *)buf; + struct vfio_device_feature_dma_logging_report *report = + (struct vfio_device_feature_dma_logging_report *)feature->data; + + report->iova = iova; + report->length = size; + report->page_size = qemu_real_host_page_size(); + report->bitmap = (uint64_t)bitmap; + + feature->argsz = sizeof(buf); + feature->flags = + VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_LOGGING_REPORT; + + if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) { + return -errno; + } + + return 0; +} + +static int vfio_devices_query_dirty_bitmap(VFIOContainer *container, + VFIOBitmap *vbmap, hwaddr iova, + hwaddr size) +{ + VFIODevice *vbasedev; + VFIOGroup *group; + int ret; + + if (vfio_have_giommu(container)) { + /* Device dirty page tracking currently doesn't support vIOMMU */ + bitmap_set(vbmap->bitmap, 0, vbmap->pages); + + return 0; + } + + QLIST_FOREACH(group, &container->group_list, container_next) { + QLIST_FOREACH(vbasedev, &group->device_list, next) { + ret = vfio_device_dma_logging_report(vbasedev, iova, size, + vbmap->bitmap); + if (ret) { + error_report("%s: Failed to get DMA logging report, iova: " + "0x%" HWADDR_PRIx ", size: 0x%" HWADDR_PRIx + ", err: %d (%s)", + vbasedev->name, iova, size, ret, strerror(-ret)); + + return ret; + } + } + } + + return 0; +} + static int vfio_query_dirty_bitmap(VFIOContainer *container, VFIOBitmap *vbmap, hwaddr iova, hwaddr size) { @@ -1673,10 +1743,12 @@ static int vfio_query_dirty_bitmap(VFIOContainer *container, VFIOBitmap *vbmap, static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, uint64_t size, ram_addr_t ram_addr) { + bool all_device_dirty_tracking = + vfio_devices_all_device_dirty_tracking(container); VFIOBitmap *vbmap; int ret; - if (!container->dirty_pages_supported) { + if (!container->dirty_pages_supported && !all_device_dirty_tracking) { cpu_physical_memory_set_dirty_range(ram_addr, size, tcg_enabled() ? DIRTY_CLIENTS_ALL : DIRTY_CLIENTS_NOCODE); @@ -1688,7 +1760,12 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, return -errno; } - ret = vfio_query_dirty_bitmap(container, vbmap, iova, size); + if (all_device_dirty_tracking) { + ret = vfio_devices_query_dirty_bitmap(container, vbmap, iova, size); + } else { + ret = vfio_query_dirty_bitmap(container, vbmap, iova, size); + } + if (ret) { goto out; } From patchwork Wed Feb 22 17:49:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149407 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3B4A1C636D6 for ; Wed, 22 Feb 2023 17:51:55 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGo-0001hI-2q; Wed, 22 Feb 2023 12:51:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGk-0001XJ-6o for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:06 -0500 Received: from mail-dm6nam10on20603.outbound.protection.outlook.com ([2a01:111:f400:7e88::603] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGi-0004gm-2e for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:05 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JPlZIctUrziSo/MrpRyFQ1GUKr+88PeE3FJ7CXeOoM7LbFZm3olc/7CzoFnIV8oOeJ0h6k9N2SYP0NTrgiCVLGNWUJ+5bdMrnwtHHOAihacK0EFWWVdql93vT/41uIjqGf9hjPvVYA/jdFitCQ/u15k8Xu8t3mN0QjwEoe6Hzv5x1/E4+FVL2Ryu+21GtzHV6ZFCV4nveeReNz8uyttImxBwfr/kFUr/nKsa/D/cKd2Sib+JGCc1rco8i3LdkV7K3XT2AocizY9NJq/2Yc6f9q7Xq4v73MUttJGycaNj7JZzyLgq6mbqWQtdnI//RuLanUQsAyUeJGA4ioMBr39+/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=gM/3mxEWLiI68YZL+YmuiSddmce59bvGTMzbqaTB6WI=; b=YhFzI9kVJAvhHpSSemR/tNHsAQE+eNEaarvhcN5t3bnTgiaQfPS+LstwzqINcIp/kR1Zp7Q1IBeP/hTqAFVYBc1KXvZtc08WDmMzaXwJ54U3f3mUgx7i8FsauZOWxMTyIuqva35FGV/M7QijJNMEKTTJvqkw/zk4pWJoW0x7K84gh6S7sHpDTLf9HsKPabABWBPN8HBtmQMpPuMkzJNddTRn1/7OrXZXYk0QF2Mx9jNArtav7C/BjP+8d3QelKTZB0/zeBDph0O2beo3dPHK8MZkK2fR5TrN1RXGPB8f04++cUBuKJlTnznOdXG2dF+Oe4mhLLIz6NAwEvySbOZXwA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gM/3mxEWLiI68YZL+YmuiSddmce59bvGTMzbqaTB6WI=; b=ebaGBmzjyRyFkY6eH+dYAd2eEbRMiPvqkbKIl/qB605vBBLS2JM4obdQRyA52t4CXycLAGA3ajWmCwrPnfyM/sUPjpHsq/FdXWrO0c13retYS3ifWXE/7b8NWZtz13d0jURWsjqv0r457+2FAYdUs6FJM35sk1vV64KDstgDs45w0kctrKsBalO8XXVJfGIeb94h1pXDkzRJ3zJXPeSNHt9u2o9AhiHwy//IIg3pEB7lESsWQzeXhfFPkmh87DLqYd8SwFDL3gM4kGz//UkzMnoO6p/ChcGOiPvKlmFpk0cZo3tIzMQ4OUdeq0hV55RVAFnsk/IQ0HmQOE8OlcIEww== Received: from BN0PR04CA0147.namprd04.prod.outlook.com (2603:10b6:408:ed::32) by DM4PR12MB7501.namprd12.prod.outlook.com (2603:10b6:8:113::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20; Wed, 22 Feb 2023 17:50:59 +0000 Received: from BL02EPF0000C403.namprd05.prod.outlook.com (2603:10b6:408:ed:cafe::54) by BN0PR04CA0147.outlook.office365.com (2603:10b6:408:ed::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21 via Frontend Transport; Wed, 22 Feb 2023 17:50:59 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C403.mail.protection.outlook.com (10.167.241.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:50:59 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:43 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:42 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:37 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 14/20] vfio/common: Extract vIOMMU code from vfio_sync_dirty_bitmap() Date: Wed, 22 Feb 2023 19:49:09 +0200 Message-ID: <20230222174915.5647-15-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C403:EE_|DM4PR12MB7501:EE_ X-MS-Office365-Filtering-Correlation-Id: 49773ebd-b981-4982-6596-08db14fd5b5d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +X2IMFlfU/QecwkgcvyqxRjrn+feY2LZvOmd/44HGyRMUEL5LMSMQwl0aCsZkV9ylbo+lf5MQ+YFlNs/PvgW//gkyM3Giq5/shgyzQDqYU+J8pZ1HmMXWw2/41ZDe2eqfwsmqCkJPoVmh2qfm/BDG7WJndv7wgoM9wj9V71+LNvQOvX6noppamCpMgWTkCMpyXjZAo/McibZamtlmGAjRU+D3JmLT2KayeRbTQy3GPtWa9v99xVFlD1Ttur6/0MP6HG2kZMCvbirYldDLiiL5z/e2c+7O9euxrfFq7B6KEiJFmaY18Atrldql4RMe3pEyRExcbpPZFbOY9jRlfxqeWvAdh/m0hC+IAWhS0VxFLzPztLel+fpt3uOe4kmvTD0H9TeyUnZEOOcxq7ETiL8Mn2tkH3tJ/0yexVC458KFsaT8xXiR1jHMu7qNCQc+uBD85qO2RSKdA53iVfEK+QFOj4Jtc+e+nLXFPTvEtzmGRAH0zP4Q++Q4qlKbpxlDHZ/A3MpMsl1ZYtXcKOFECyWiwCovljdrrcArtYV2H+OCKdFWRF0zxya5xL9SYpSndKDbYP/O9o5NBs7MYisuykxcdEcsRGiXFhCPGnJImKOECiXr0Xc1PQU0jZn+WPr5AzWBKMAUGr7t7UDcwzVFFQQYMax951OU+S7qmfm2ykXvd8UYHXeJwZvCD/q8g7S5g3FgjGeup/x+UFLe/FGLdE5C/Nr845DW0KeF/wGNUcZbxo= X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(136003)(39860400002)(346002)(376002)(396003)(451199018)(36840700001)(40470700004)(46966006)(2906002)(186003)(26005)(41300700001)(356005)(82740400003)(6666004)(1076003)(7636003)(2616005)(7696005)(5660300002)(7416002)(36860700001)(8936002)(316002)(478600001)(40460700003)(8676002)(70586007)(336012)(40480700001)(6916009)(82310400005)(70206006)(4326008)(54906003)(83380400001)(86362001)(47076005)(36756003)(426003)(14143004); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:50:59.3154 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 49773ebd-b981-4982-6596-08db14fd5b5d X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C403.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB7501 Received-SPF: softfail client-ip=2a01:111:f400:7e88::603; envelope-from=avihaih@nvidia.com; helo=NAM10-DM6-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Extract vIOMMU code from vfio_sync_dirty_bitmap() to a new function and restructure the code. This is done as preparation for the following patches which will add vIOMMU support to device dirty page tracking. No functional changes intended. Signed-off-by: Avihai Horon --- hw/vfio/common.c | 63 +++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 797eb2c26e..4a7fff6eeb 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1866,37 +1866,50 @@ static int vfio_sync_ram_discard_listener_dirty_bitmap(VFIOContainer *container, &vrdl); } +static int vfio_sync_iommu_dirty_bitmap(VFIOContainer *container, + MemoryRegionSection *section) +{ + VFIOGuestIOMMU *giommu; + bool found = false; + Int128 llend; + vfio_giommu_dirty_notifier gdn; + int idx; + + QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) { + if (MEMORY_REGION(giommu->iommu_mr) == section->mr && + giommu->n.start == section->offset_within_region) { + found = true; + break; + } + } + + if (!found) { + return 0; + } + + gdn.giommu = giommu; + idx = memory_region_iommu_attrs_to_index(giommu->iommu_mr, + MEMTXATTRS_UNSPECIFIED); + + llend = int128_add(int128_make64(section->offset_within_region), + section->size); + llend = int128_sub(llend, int128_one()); + + iommu_notifier_init(&gdn.n, vfio_iommu_map_dirty_notify, IOMMU_NOTIFIER_MAP, + section->offset_within_region, int128_get64(llend), + idx); + memory_region_iommu_replay(giommu->iommu_mr, &gdn.n); + + return 0; +} + static int vfio_sync_dirty_bitmap(VFIOContainer *container, MemoryRegionSection *section) { ram_addr_t ram_addr; if (memory_region_is_iommu(section->mr)) { - VFIOGuestIOMMU *giommu; - - QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) { - if (MEMORY_REGION(giommu->iommu_mr) == section->mr && - giommu->n.start == section->offset_within_region) { - Int128 llend; - vfio_giommu_dirty_notifier gdn = { .giommu = giommu }; - int idx = memory_region_iommu_attrs_to_index(giommu->iommu_mr, - MEMTXATTRS_UNSPECIFIED); - - llend = int128_add(int128_make64(section->offset_within_region), - section->size); - llend = int128_sub(llend, int128_one()); - - iommu_notifier_init(&gdn.n, - vfio_iommu_map_dirty_notify, - IOMMU_NOTIFIER_MAP, - section->offset_within_region, - int128_get64(llend), - idx); - memory_region_iommu_replay(giommu->iommu_mr, &gdn.n); - break; - } - } - return 0; + return vfio_sync_iommu_dirty_bitmap(container, section); } else if (memory_region_has_ram_discard_manager(section->mr)) { return vfio_sync_ram_discard_listener_dirty_bitmap(container, section); } From patchwork Wed Feb 22 17:49:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149406 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2C5D7C61DA4 for ; Wed, 22 Feb 2023 17:51:42 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtGx-0002Zu-17; Wed, 22 Feb 2023 12:51:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGv-0002Nj-8n for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:17 -0500 Received: from mail-mw2nam10on2062e.outbound.protection.outlook.com ([2a01:111:f400:7e89::62e] helo=NAM10-MW2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGt-0004hj-Be for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:17 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AERNimqqEwHGzR6brX5AxXmIlJgmZPIetInzpXCjz7bTO7XC/2oJWf+so7uQa5Ml+7y++TCOAcrWO5JxTbs64gskgMBr3aJPK1XxkmdXaGMduBaVF2T/4C7D2Uqm7D1hZW1K6X+sU0PDI/JA1KiPEcvWHKgFxAYg2b0a9sLsya1VSX5jS2XbGqSxojy5dfCl/Dvil2kSXRfs+nv8Kmr8OldyLYGcNq+nEbtzZHvJYKPzsz0rl+1M8oAc4xV/odvl2KnQdmwjMAMkvUvynAhx6BiH+RQs1fpFYxGrz6TDdTeOOaGNPcQVJoQkfXTbPaBs5imJHY9VYnGE39QH7aQetQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=B1BvjsoS1EXvcvulu+xjNLg8P8QBhrafJDQ8shKEzKM=; b=A039gxKuwZnBgp88FTD+/n1MD7xh40FHteU0bmaY7zOvx4CrW6tbTzg4puXhlrvf72GFP49j5m/WxzuGE9PjFksWZ45Q57kA1unsz6IYoGC2rksX++QIzCwGKdomtUgehcqcmjPQStQhB+3DDqza6qN1yXG8Cx8QdPQZdLrckXXu6kr1FkocMtL1GcGQZUDG7eHa5COCg/6NmjFP6O256uqxtS9H4vfNO8tDAykeC/g6MJC5ANLov0199QXKShdJqOftEtbk9qUCPQuPgssb0vi9BGPdYiZls70gJhrW/K1p4MLwVIv3f2J2GLTBTwrDNXId5d5vJSa7PB7zrH210Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=B1BvjsoS1EXvcvulu+xjNLg8P8QBhrafJDQ8shKEzKM=; b=V8gyhKXLfc2sBJSwDUAqj33r+GG6C14Gq2q2Pg0lprYrhAvavrxTOf+uDXikoJUeI4CHolsKdEGjRqF2K7HBIdsPt9oeOAtwWNO5svi6fIqH2UJ5zyfOR/Q8IXUlOFR0GRuEytEOQA7oCdEuufFmEFqpadfV4DFQUhG7aaE828YAdLGhfAMnKvZitm8cND6Ah+IclqcwsBtJWXhy/NjK5m7ly4aV+8TYuDOZAZudl9cmJ8RuSu6eZuQmFvXV5oiwkAkMzf1FZxY+3tgq49QgpKK4xMDcvtHYYMKz6V9XHsQDex2faVx8bxK2yQlbU5dRW2//VGQtNhk2liMmmxY/1w== Received: from BN0PR04CA0147.namprd04.prod.outlook.com (2603:10b6:408:ed::32) by SN7PR12MB6909.namprd12.prod.outlook.com (2603:10b6:806:263::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19; Wed, 22 Feb 2023 17:51:06 +0000 Received: from BL02EPF0000C403.namprd05.prod.outlook.com (2603:10b6:408:ed:cafe::ab) by BN0PR04CA0147.outlook.office365.com (2603:10b6:408:ed::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21 via Frontend Transport; Wed, 22 Feb 2023 17:51:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C403.mail.protection.outlook.com (10.167.241.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:51:06 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:49 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:48 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:43 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 15/20] memory/iommu: Add IOMMU_ATTR_MAX_IOVA attribute Date: Wed, 22 Feb 2023 19:49:10 +0200 Message-ID: <20230222174915.5647-16-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C403:EE_|SN7PR12MB6909:EE_ X-MS-Office365-Filtering-Correlation-Id: b3eb0015-773d-449e-88df-08db14fd5f6a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4UceuDxWyl06eTY8+PXwVEGjgdUo++zWv2QYPhVnOucCf0fo1FQb8Sfg8s3lRidqRLewP2j06svEuXLU+Fbp+p1aZUVku0CmZc/1J+aYY+hJRFsShyddK2PqebGpuFi8ozQ9b3TBfJu1LUw2wOaUrX/jk9SzEkGPyVv3y9AcCdMRv5311FlQK6h7cKATOEhNYngNd3SnJgqI5kgshPmORkwDoCSn5UsUrKEgRgVmAJt3iqfmxq1Y1auqU7SAB6OQ6V1F025yKZiGeLBJJXByv2VdLHORcCET6sM7ULN6iGBgzecZAdwcMKnSdgSOmX+/qDXeSWuHTQiNs7VTfojWX0HlNupq9R+6sS1VtmWv9w3xOwMCOQb8PpKgV7Eb2Zk1DUN3oXCM9cZuvqJ0Mvl4hq2zf1XoDd6BQcgPctOCFdDu5xnu4j/w9jXasxTmm1+9sfuuFKzXSvE8ImJtHRYl45Ofzf1rKTfC8XrWtoWAmPVKSAkL1wPsLH0E38SjZaLlz62a0ID20yvaD1qWQV6cCAeNBOgQVhKevXGR5qI0hzWkNoy0OLxrkwGn5tx0rtWK0zauzNQZI4aKud0A62knh0yqN4h3oLPaN9GIt6jr6jYa+OEHaCXoDVfmZXeMcCdFem0I6TQZK4UaPHJ6nUYWFmcs+AQm6TIpIUsf7sy7D58qIYY0KhSiGWf10jM6SKXKfIj0ehFCSIIXyZ57ayovzw== X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(346002)(39860400002)(451199018)(46966006)(40470700004)(36840700001)(426003)(47076005)(83380400001)(4744005)(36756003)(2906002)(5660300002)(7416002)(1076003)(54906003)(86362001)(41300700001)(478600001)(6666004)(7696005)(82310400005)(36860700001)(7636003)(26005)(2616005)(336012)(40480700001)(186003)(356005)(8676002)(40460700003)(6916009)(316002)(4326008)(8936002)(82740400003)(70586007)(70206006); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:51:06.1280 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b3eb0015-773d-449e-88df-08db14fd5f6a X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C403.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6909 Received-SPF: softfail client-ip=2a01:111:f400:7e89::62e; envelope-from=avihaih@nvidia.com; helo=NAM10-MW2-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Add a new IOMMU attribute IOMMU_ATTR_MAX_IOVA which indicates the maximal IOVA that an IOMMU can use. This attribute will be used by VFIO device dirty page tracking so it can track the entire IOVA space when needed (i.e. when vIOMMU is enabled). Signed-off-by: Avihai Horon Acked-by: Peter Xu --- include/exec/memory.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 2e602a2fad..cdd47fb79b 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -316,7 +316,8 @@ typedef struct MemoryRegionClass { enum IOMMUMemoryRegionAttr { - IOMMU_ATTR_SPAPR_TCE_FD + IOMMU_ATTR_SPAPR_TCE_FD, + IOMMU_ATTR_MAX_IOVA, }; /* From patchwork Wed Feb 22 17:49:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149409 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7B7D0C636D6 for ; Wed, 22 Feb 2023 17:52:10 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtH1-0002pp-UO; Wed, 22 Feb 2023 12:51:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtH0-0002ni-9T for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:22 -0500 Received: from mail-bn8nam11hn20300.outbound.protection.outlook.com ([2a01:111:f400:7eae::300] helo=NAM11-BN8-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGy-0004ix-F4 for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:22 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D09gBquFIUE1K3YP0iQkyVmY+ZuKK1WadNn9FkjZKPQUtOw6nujhFv/2wP7XHVxr750rI3AH9Ee7knZtvubEOqOXaiV3jjmgZ8zt3Zcr6DbjEgQKy04YCSrvtThkeqDWhQOSPw0xjYcUnCx5r9CwU3qMqV6pmhcTHgfUN9LtcTnJfiqm5S6IIKfuVd8q0Q8aslLB7GYIlA82lYYpG+hBMNHpAC0DVv3Bte88oJtks2LdsPJX/O63p6/uKTQ67NpM5tTjLkc4YuoctbUdN5CGfywFgUnIePzxqVLcMYf39nvoHptzXDEABywNrTMi1wZpYmiWISwN/GHu/Y29cs0t3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=w8TJ9rvcw6aZs8xr6BZ3FAgwphVc21Ogf6zXPFRrIHs=; b=jmof8zmvlZIRQAFpeWOalzmBUmKFrVuS4NrHeQk4Q/Ol38IU+j0YtEvroxTtXEK2jCIU5z6GwSBeZb+KHxC+/v4gosnBIH0VL4q+13pYLdnEuF5gJmVBUGGPcwqLynP2ZE25PtyXMyqwk+wBmMgvgEpmXzOP1U/koh3nJIJAt0qHftsDNq7gb2ojtdX3KOthZi/MzybIHpSB6dAal2q4wqW/WuypFu58uAAhjJAgJwXU8AhendJgxYLFk6SiH5tRdGJSVA7M/0np3kSC1bjmiMsVQd1CuXQ0S36aR+j2uVeWyiPttVozYwkHwYN1cepJK1hdTIakXc/aZJsQAOi3IQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=w8TJ9rvcw6aZs8xr6BZ3FAgwphVc21Ogf6zXPFRrIHs=; b=rwpAG4seooK5bG/Ii822gYsr8TvbI4T6I/kOiU3NQqbQUOQ2zld7dssEewjOgUhTBxg39VOA2pzqOsO19gOKfHICtT2SF7Tn302Dy9PoaTW+i7dnRtvxQSFg3kqQznAB029imRR5+4h9KZevUdVDzoOZz89iNrD0hn/nlqw8hf2eddPrBmmnY6By80RcrqRpqQD9wtrKXsfpdFSRGCfOSb0hXV1rQjawde6xMWjnaSiEfOmMQGO2PWAx8UAgDpVCYFVXHAGy9kwWzXjd3eVIcGtKdInG4LltRAQ7hUJ4wEYsKEMDrr8OEtIR7P0uLD7hQDkEcHr4HiubosFiqXbchQ== Received: from MW4PR03CA0007.namprd03.prod.outlook.com (2603:10b6:303:8f::12) by MN0PR12MB6055.namprd12.prod.outlook.com (2603:10b6:208:3cd::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.19; Wed, 22 Feb 2023 17:51:17 +0000 Received: from CO1NAM11FT086.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8f:cafe::48) by MW4PR03CA0007.outlook.office365.com (2603:10b6:303:8f::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.20 via Frontend Transport; Wed, 22 Feb 2023 17:51:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT086.mail.protection.outlook.com (10.13.175.73) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:51:16 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:54 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:50:54 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:49 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 16/20] intel-iommu: Implement get_attr() method Date: Wed, 22 Feb 2023 19:49:11 +0200 Message-ID: <20230222174915.5647-17-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT086:EE_|MN0PR12MB6055:EE_ X-MS-Office365-Filtering-Correlation-Id: f442ee45-dfc9-4cec-0af5-08db14fd65d0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dol4XnaQoNaUSg4/tLkryzCd6AMI0cHJKJWKhYddsL91mGz/QNRp/hCojGR0WBoETgNRoP6O/r8TTAeIp5KOwpmWrYjHuX4ZqQ4+NS/xIG2FHt6WmYjVZNrlUCEvZjqSKm67tQdl49hTSr00jt6jziFVqdB86opsVjB4Bpgubz2ZyRxmhi5Hsy/oh8M/+1Z1SXu61nwXOFyU0XFE0vLJXHXTQWYakPT3Wx9l7O/KHTBSKf7v4Zf7z/3sF7eVjsWHkelPh5JpVbibR2mu0bh1yK7x9HeA+MmGt8pEp+8CveAjvXwyO2Bp3JSjSJ+Dr4Tv0117cigv5bFAyPSQRuOU9+lv649sbalxULEngyg+k8fsRfa8uzy6Ry4OkzTMJda7yaXD/3NtHSXhOFYPrRGli4I25kA0IohP8k9V2lmqzurBVWvCOooi5yzmnwlQXVaQECvB5y3pdS5JIRsp+IFIsjQ1T6vNGxfj7PQDHne/c6h+zYLgbQ5I8/ppkn01diYs+wc0GdxE+1Xq0EKCqae21ndlxircDDt7DW3BDfzUElITyqTDw6PPtW2OpDb1qXyoNZYtQqbV6JA5gLhgEfAgKMl4TrUMsmvrYVUvwYVzIK1+U4z7qiZTZUYffnFYSQJhNXHxZVNRx0bWPbMEKx870kXgc2pHn0Ngp773+uSpxK/jiwZKWWRfwHHGKuH1HZYg6FG1ebLMGwisz1ddcaHxO4movYAeKnUylxqRGhX/tfe01QaZnplqNP0JLw0Ft3qJSldtSmvYPRSOMcgjqR4DUg== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(396003)(376002)(39860400002)(136003)(346002)(5400799012)(451199018)(46966006)(36840700001)(40470700004)(316002)(82740400003)(7636003)(41300700001)(2906002)(36756003)(7416002)(40460700003)(5660300002)(40480700001)(36860700001)(4326008)(86362001)(8676002)(70206006)(8936002)(356005)(70586007)(6916009)(34020700004)(54906003)(1076003)(47076005)(336012)(2616005)(82310400005)(426003)(26005)(186003)(478600001)(7696005)(6666004)(12100799015); DIR:OUT; SFP:1501; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:51:16.9534 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f442ee45-dfc9-4cec-0af5-08db14fd65d0 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT086.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6055 Received-SPF: softfail client-ip=2a01:111:f400:7eae::300; envelope-from=avihaih@nvidia.com; helo=NAM11-BN8-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Implement get_attr() method and use the address width property to report the IOMMU_ATTR_MAX_IOVA attribute. Signed-off-by: Avihai Horon Acked-by: Peter Xu --- hw/i386/intel_iommu.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 98a5c304a7..b0068b0df4 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3841,6 +3841,23 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n) return; } +static int vtd_iommu_get_attr(IOMMUMemoryRegion *iommu_mr, + enum IOMMUMemoryRegionAttr attr, void *data) +{ + VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu); + IntelIOMMUState *s = vtd_as->iommu_state; + + if (attr == IOMMU_ATTR_MAX_IOVA) { + hwaddr *max_iova = data; + + *max_iova = (1ULL << s->aw_bits) - 1; + + return 0; + } + + return -EINVAL; +} + /* Do the initialization. It will also be called when reset, so pay * attention when adding new initialization stuff. */ @@ -4173,6 +4190,7 @@ static void vtd_iommu_memory_region_class_init(ObjectClass *klass, imrc->translate = vtd_iommu_translate; imrc->notify_flag_changed = vtd_iommu_notify_flag_changed; imrc->replay = vtd_iommu_replay; + imrc->get_attr = vtd_iommu_get_attr; } static const TypeInfo vtd_iommu_memory_region_info = { From patchwork Wed Feb 22 17:49:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149411 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A1768C61DA4 for ; Wed, 22 Feb 2023 17:52:41 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtH2-0002qk-Oa; Wed, 22 Feb 2023 12:51:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtH0-0002ob-Un for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:22 -0500 Received: from mail-bn7nam10on20607.outbound.protection.outlook.com ([2a01:111:f400:7e8a::607] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtGy-0004iw-Ef for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:22 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V/oIzcsN7CgycQcef8ClaeEl6ro5OMA6M4J3UL8qtDv+6YAtv7TB4eVIRaBlXrR29arQamzEq8NSWEakYtgYwlE+if04CHzbn8M8VdQdiDhCsKsYDH7Nl3FCKUVWH/ES6cJrNrhxg4JnepaxAurZrdjUYv/vgfcTPXu5TCV6n6Bk8Tfq/EgjffXotQk6fTzc5TsZAmPf/njLeB7x3S/EyGsH/scQ6vyJ2rkG4cUko7NTDwVWkHwjlDFAOdEeF16BeJ7NhvPruE0t0lwSVDtTZKuwcssVgdY/5CoOKcyzeY0yAfla0c74eR/LwPOALKKiyDO+alfhGbqarvG9ENi/oQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4f598anREjn3pD2d/AdGIJhZp719+wMiRMww7pzdJAI=; b=Jctil+F8GKOB13HEdsAWHCfZFIIv/4vrPp7geW7udylR3RATECnaigKG51iYb+SHiX0mUxtWPxI7ecpP/BRfXXhFttV2FQSMiU6M2XN0pM62s62gVXqfbcXbjA9LnG2/BeeqKbJeQSKZ6JEizn+ZWEjkB8eQNkNxDLpGws+jDpL6FLSwSzhpXME0ef73a9qx5pD/cyLVQqIMEYWMybCnmt/M1dU5gDaFsgc2GNfp0s5ISQj4Ebqf3IZPcmDwr00352nxozZDJF9oFXqFUol6tx5AAiP37trELvfRBwR2PmcfqzrjUqo/7G3PXzUQ/87+1zFJDXZKviO7t1pDGAn9/Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4f598anREjn3pD2d/AdGIJhZp719+wMiRMww7pzdJAI=; b=eGTHHp0uiZBr4U6hpGPO8nHh+1r0oPsrmarrwkYB+0Bn1TXIGjS4BMfxglx1jysaqh07zkzLUMednuwzsOyp7o9szf99ZYLreC6VJIfCx0qOnt5pSmawAMEAHb22rEraBoZpQkH0irszjctZcPXM0+ZhcuJGOL+z1F57+ZlnFs8qfcmg89PS+Y4azmacw47ksO5JWkSTTQSR03OAZrBgQxotT453D2LAicri+0DCBSHeXbD7D09iVYiKy8V3qZe9PYBP0XAX/Imbme6xzqN74ezOO4yFItECbo0qx9+ZfFATNikN2SasEzoPIb1J9SIlWAC8U7k4n16cdL9Nvz6BIg== Received: from BN0PR04CA0086.namprd04.prod.outlook.com (2603:10b6:408:ea::31) by BY5PR12MB4194.namprd12.prod.outlook.com (2603:10b6:a03:210::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19; Wed, 22 Feb 2023 17:51:16 +0000 Received: from BL02EPF0000C405.namprd05.prod.outlook.com (2603:10b6:408:ea:cafe::71) by BN0PR04CA0086.outlook.office365.com (2603:10b6:408:ea::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:51:15 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C405.mail.protection.outlook.com (10.167.241.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:51:15 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:51:00 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:51:00 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:50:54 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 17/20] vfio/common: Support device dirty page tracking with vIOMMU Date: Wed, 22 Feb 2023 19:49:12 +0200 Message-ID: <20230222174915.5647-18-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C405:EE_|BY5PR12MB4194:EE_ X-MS-Office365-Filtering-Correlation-Id: fdcab1d9-660c-4cf6-9914-08db14fd64da X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nlJFAM/cTRsbh97mHRpnCldnqrQPLMw1J3fDa+kEzy50p5RPeBCxuziJj7oBXabPxMfAuLSWXdBEziK7aTMUT0x5JlvkmcKTmeorLPfmiNWiEjppI1UXXYtms6wAI8CHESWZrW9pc3varEL5jvD/KCFZSbdUj5RqTXc0dozVSSm0crfhajz/0uHdsgOUIrDWSCPWNpP1hhfWcdBidNlqq4xHq9fXglz7kUwMiOHe7pMGKImo4KEP+wfHxnPBwPgsSO5FkgDLBOqTGGf73u7bkMpQyfiBfIqelke9xjS1xmeX5DQqiQiU4YMuRmayDM9B/g6S+Rh8aS9VVHR5Sloa6frn5tUIBgjODyTJKcAwgjbMNfy4H90xJ4rEwwyPnFEcmJyC54ASoaL5FROgKTXHnJmov66bqCISb66HddwnO64ovItjNBJWvqUY8fVDBZLqzEOkzYFkhxJcZKOejfLLn7neJcl8zQUXOKXPKZPiqTf4XYuppAx/9Rk7V26EOxs20gDPTbqJYvJx8JxBlCrB3zWJORf4S3hV3KVahbdiDhY6hHH3JOuf0WOeCN92rGKjT123ZFC9eslnm8oTEJPRKsXIgeiXyl7c8kbOdTSUHqzehhaEHGKpAhxo/mecfYe3DkR0sNSyaTcAGOq8AidVCdSS7h/h/zhLBh7SuOyFzwJC2x7LD7p0vo2xftmgbgDmkEZe7PTGJWpFxHgffO2ouYZB5PVvn00o9QxiosQwsGk= X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(39860400002)(376002)(136003)(346002)(396003)(451199018)(40470700004)(36840700001)(46966006)(186003)(26005)(83380400001)(4326008)(70586007)(6916009)(70206006)(40480700001)(8676002)(8936002)(36756003)(5660300002)(7416002)(2616005)(1076003)(6666004)(7696005)(336012)(478600001)(426003)(47076005)(40460700003)(356005)(41300700001)(54906003)(316002)(86362001)(7636003)(82740400003)(82310400005)(2906002)(36860700001)(14143004); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:51:15.2354 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fdcab1d9-660c-4cf6-9914-08db14fd64da X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C405.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4194 Received-SPF: softfail client-ip=2a01:111:f400:7e8a::607; envelope-from=avihaih@nvidia.com; helo=NAM10-BN7-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Currently, device dirty page tracking with vIOMMU is not supported - RAM pages are perpetually marked dirty in this case. When vIOMMU is used, IOVA ranges are DMA mapped/unmapped on the fly as the vIOMMU maps/unmaps them. These IOVA ranges can potentially be mapped anywhere in the vIOMMU IOVA space. Due to this dynamic nature of vIOMMU mapping/unmapping, tracking only the currently mapped IOVA ranges, as done in the non-vIOMMU case, doesn't work very well. Instead, to support device dirty tracking when vIOMMU is enabled, track the entire vIOMMU IOVA space. If that fails (IOVA space can be rather big and we might hit HW limitation), try tracking smaller range while marking untracked ranges dirty. Signed-off-by: Avihai Horon --- include/hw/vfio/vfio-common.h | 2 + hw/vfio/common.c | 196 +++++++++++++++++++++++++++++++--- 2 files changed, 181 insertions(+), 17 deletions(-) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 1f21e1fa43..1dc00cabcd 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -95,6 +95,8 @@ typedef struct VFIOContainer { unsigned int dma_max_mappings; IOVATree *mappings; QemuMutex mappings_mutex; + /* Represents the range [0, giommu_tracked_range) not inclusive */ + hwaddr giommu_tracked_range; QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list; diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 4a7fff6eeb..1024788bcc 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -45,6 +45,8 @@ #include "migration/qemu-file.h" #include "sysemu/tpm.h" #include "qemu/iova-tree.h" +#include "hw/boards.h" +#include "hw/mem/memory-device.h" VFIOGroupList vfio_group_list = QLIST_HEAD_INITIALIZER(vfio_group_list); @@ -430,6 +432,38 @@ void vfio_unblock_multiple_devices_migration(void) multiple_devices_migration_blocker = NULL; } +static uint64_t vfio_get_ram_size(void) +{ + MachineState *ms = MACHINE(qdev_get_machine()); + uint64_t plugged_size; + + plugged_size = get_plugged_memory_size(); + if (plugged_size == (uint64_t)-1) { + plugged_size = 0; + } + + return ms->ram_size + plugged_size; +} + +static int vfio_iommu_get_max_iova(VFIOContainer *container, hwaddr *max_iova) +{ + VFIOGuestIOMMU *giommu; + int ret; + + giommu = QLIST_FIRST(&container->giommu_list); + if (!giommu) { + return -ENOENT; + } + + ret = memory_region_iommu_get_attr(giommu->iommu_mr, IOMMU_ATTR_MAX_IOVA, + max_iova); + if (ret) { + return ret; + } + + return 0; +} + static bool vfio_have_giommu(VFIOContainer *container) { return !QLIST_EMPTY(&container->giommu_list); @@ -1510,7 +1544,8 @@ static gboolean vfio_iova_tree_get_last(DMAMap *map, gpointer data) } static struct vfio_device_feature * -vfio_device_feature_dma_logging_start_create(VFIOContainer *container) +vfio_device_feature_dma_logging_start_create(VFIOContainer *container, + bool giommu) { struct vfio_device_feature *feature; size_t feature_size; @@ -1529,6 +1564,16 @@ vfio_device_feature_dma_logging_start_create(VFIOContainer *container) control = (struct vfio_device_feature_dma_logging_control *)feature->data; control->page_size = qemu_real_host_page_size(); + if (giommu) { + ranges = g_malloc0(sizeof(*ranges)); + ranges->iova = 0; + ranges->length = container->giommu_tracked_range; + control->num_ranges = 1; + control->ranges = (uint64_t)ranges; + + return feature; + } + QEMU_LOCK_GUARD(&container->mappings_mutex); /* @@ -1578,12 +1623,12 @@ static void vfio_device_feature_dma_logging_start_destroy( g_free(feature); } -static int vfio_devices_dma_logging_start(VFIOContainer *container) +static int vfio_devices_dma_logging_start(VFIOContainer *container, bool giommu) { struct vfio_device_feature *feature; int ret; - feature = vfio_device_feature_dma_logging_start_create(container); + feature = vfio_device_feature_dma_logging_start_create(container, giommu); if (!feature) { return -errno; } @@ -1598,18 +1643,128 @@ static int vfio_devices_dma_logging_start(VFIOContainer *container) return ret; } +typedef struct { + hwaddr *ranges; + unsigned int ranges_num; +} VFIOGIOMMUDeviceDTRanges; + +/* + * This value is used in the second attempt to start device dirty tracking with + * vIOMMU, or if the giommu fails to report its max iova. + * It should be in the middle, not too big and not too small, allowing devices + * with HW limitations to do device dirty tracking while covering a fair amount + * of the IOVA space. + * + * This arbitrary value was chosen becasue it is the minimum value of Intel + * IOMMU max IOVA and mlx5 devices support tracking a range of this size. + */ +#define VFIO_IOMMU_DEFAULT_MAX_IOVA ((1ULL << 39) - 1) + +#define VFIO_IOMMU_RANGES_NUM 3 +static VFIOGIOMMUDeviceDTRanges * +vfio_iommu_device_dirty_tracking_ranges_create(VFIOContainer *container) +{ + hwaddr iommu_max_iova = VFIO_IOMMU_DEFAULT_MAX_IOVA; + hwaddr retry_iova; + hwaddr ram_size = vfio_get_ram_size(); + VFIOGIOMMUDeviceDTRanges *dt_ranges; + int ret; + + dt_ranges = g_try_new0(VFIOGIOMMUDeviceDTRanges, 1); + if (!dt_ranges) { + errno = ENOMEM; + + return NULL; + } + + dt_ranges->ranges_num = VFIO_IOMMU_RANGES_NUM; + + dt_ranges->ranges = g_try_new0(hwaddr, dt_ranges->ranges_num); + if (!dt_ranges->ranges) { + g_free(dt_ranges); + errno = ENOMEM; + + return NULL; + } + + /* + * With vIOMMU we try to track the entire IOVA space. As the IOVA space can + * be rather big, devices might not be able to track it due to HW + * limitations. In that case: + * (1) Retry tracking a smaller part of the IOVA space. + * (2) Retry tracking a range in the size of the physical memory. + */ + ret = vfio_iommu_get_max_iova(container, &iommu_max_iova); + if (!ret) { + /* Check 2^64 wrap around */ + if (!REAL_HOST_PAGE_ALIGN(iommu_max_iova)) { + iommu_max_iova -= qemu_real_host_page_size(); + } + } + + retry_iova = MIN(iommu_max_iova / 2, VFIO_IOMMU_DEFAULT_MAX_IOVA); + + dt_ranges->ranges[0] = REAL_HOST_PAGE_ALIGN(iommu_max_iova); + dt_ranges->ranges[1] = REAL_HOST_PAGE_ALIGN(retry_iova); + dt_ranges->ranges[2] = REAL_HOST_PAGE_ALIGN(MIN(ram_size, retry_iova / 2)); + + return dt_ranges; +} + +static void vfio_iommu_device_dirty_tracking_ranges_destroy( + VFIOGIOMMUDeviceDTRanges *dt_ranges) +{ + g_free(dt_ranges->ranges); + g_free(dt_ranges); +} + +static int vfio_devices_start_dirty_page_tracking(VFIOContainer *container) +{ + VFIOGIOMMUDeviceDTRanges *dt_ranges; + int ret; + int i; + + if (!vfio_have_giommu(container)) { + return vfio_devices_dma_logging_start(container, false); + } + + dt_ranges = vfio_iommu_device_dirty_tracking_ranges_create(container); + if (!dt_ranges) { + return -errno; + } + + for (i = 0; i < dt_ranges->ranges_num; i++) { + container->giommu_tracked_range = dt_ranges->ranges[i]; + ret = vfio_devices_dma_logging_start(container, true); + if (!ret) { + break; + } + + if (i < dt_ranges->ranges_num - 1) { + warn_report("Failed to start device dirty tracking with vIOMMU " + "with range of size 0x%" HWADDR_PRIx + ", err: %d. Retrying with range " + "of size 0x%" HWADDR_PRIx, + dt_ranges->ranges[i], ret, dt_ranges->ranges[i + 1]); + } else { + error_report("Failed to start device dirty tracking with vIOMMU " + "with range of size 0x%" HWADDR_PRIx ", err: %d", + dt_ranges->ranges[i], ret); + } + } + + vfio_iommu_device_dirty_tracking_ranges_destroy(dt_ranges); + + return ret; +} + static void vfio_listener_log_global_start(MemoryListener *listener) { VFIOContainer *container = container_of(listener, VFIOContainer, listener); int ret; if (vfio_devices_all_device_dirty_tracking(container)) { - if (vfio_have_giommu(container)) { - /* Device dirty page tracking currently doesn't support vIOMMU */ - return; - } - - ret = vfio_devices_dma_logging_start(container); + ret = vfio_devices_start_dirty_page_tracking(container); } else { ret = vfio_set_dirty_page_tracking(container, true); } @@ -1627,11 +1782,6 @@ static void vfio_listener_log_global_stop(MemoryListener *listener) int ret; if (vfio_devices_all_device_dirty_tracking(container)) { - if (vfio_have_giommu(container)) { - /* Device dirty page tracking currently doesn't support vIOMMU */ - return; - } - ret = vfio_devices_dma_logging_stop(container); } else { ret = vfio_set_dirty_page_tracking(container, false); @@ -1670,6 +1820,17 @@ static int vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova, return 0; } +static bool vfio_iommu_range_is_device_tracked(VFIOContainer *container, + hwaddr iova, hwaddr size) +{ + /* Check for 2^64 wrap around */ + if (!(iova + size)) { + return false; + } + + return iova + size <= container->giommu_tracked_range; +} + static int vfio_devices_query_dirty_bitmap(VFIOContainer *container, VFIOBitmap *vbmap, hwaddr iova, hwaddr size) @@ -1679,10 +1840,11 @@ static int vfio_devices_query_dirty_bitmap(VFIOContainer *container, int ret; if (vfio_have_giommu(container)) { - /* Device dirty page tracking currently doesn't support vIOMMU */ - bitmap_set(vbmap->bitmap, 0, vbmap->pages); + if (!vfio_iommu_range_is_device_tracked(container, iova, size)) { + bitmap_set(vbmap->bitmap, 0, vbmap->pages); - return 0; + return 0; + } } QLIST_FOREACH(group, &container->group_list, container_next) { From patchwork Wed Feb 22 17:49:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149414 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0A0FBC61DA4 for ; Wed, 22 Feb 2023 17:53:19 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtHD-0003Hg-PO; Wed, 22 Feb 2023 12:51:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtHB-0003Ad-KW for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:33 -0500 Received: from mail-co1nam11hn20308.outbound.protection.outlook.com ([2a01:111:f400:7eab::308] helo=NAM11-CO1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtH8-0004lV-9K for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:33 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IHL276+Mtf+OrJEo4Fq0ZSJ4RNQstAWRRV2uVh5L8D0vqsVZiKmWs0p+9fnf3FTH5mUNnqlpiBCL3nAP4AbE4FySYS3rD3ePM6SD62Q0Xxcu+RePR05b/trmiaaloQgLhkFxEDC3lLl6gqsuQI0RaRSaffk/8Tr35O/cyAzjLgzt5vWPtqtt9MYYBtMkYI+ggnfN7KemI/bEvRrRfaf1cbqW2VRPHX3GTpBzJy5hCBq6EagDibmmRzhVQGU7KAL5X6RSbIhLjU8ZtDnNyJjixeecO4J/Tpp8nvRsbQH11bbWMG6rY4ve3oOVQLx4677+KxV1K8ZML46ozVZpwysxoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=GJqTmy46TePv2SKu05LaGX4Z/E0vByKnojWPlTFt+zk=; b=M33ZJgfvIA+thpzWlyu6BoAljYutzdoXQdUN5WdykqcFXQFGeba9/CZ5PcLedQlfM75RwJY+LmMN+HnmBRANg+sYeN81tX7nI+vaRvkc6DVGgeTNAzDUqZst8AYl0t71RSlmEI80cGeNHwXsF1IYwPpvzkiNVXEiK3SjnpdGRDVoHwsZAksGlhaiXFhddjuWGDYx8+jDWn1un1lXDkSfTmO07FOsgmBLVsTexGZBENlipGcHJHu1seuGwVWPY0EyztpqsecudQSGaQYZlVkMjDfGCkdcap4xVEjc7Hkdaay0B16jgh7D78ORYQKUeqryJBxZvB9PxikvRWVsEWraIw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GJqTmy46TePv2SKu05LaGX4Z/E0vByKnojWPlTFt+zk=; b=n9fAHBcBLOEdpUUH7oGqd15QPcfSXHZPhmjR39q6HV5ppukha/ZEAs8b0s/sywuu9/khR9fBkfN33a3u2tHRnzjPDGBPa03YxeDv6qDmGs8+TRYFve4VCtnFoq7mpWP52I05pZ06Ajc7YLnl75BrBRdFvpjEXFeV/3W3ZrBnDPgA2zHCkEcHCsCnLg/Xi3DtBCU0jdkP8k/semxZGaa0oQrdHwlAo7FM9Tac8+9H8/Eh3HKae7WGZf36SYgtx/bQaEEg2ozFEDkTXd9dbOvlGPZjFdL/ImP2beeU5W+Tn56Uo9AdhlRbO0O1qWOk6zecPbOd7Gf8aThxWavsXSZwpg== Received: from MW4PR03CA0226.namprd03.prod.outlook.com (2603:10b6:303:b9::21) by BL1PR12MB5093.namprd12.prod.outlook.com (2603:10b6:208:309::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 17:51:25 +0000 Received: from CO1NAM11FT087.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b9:cafe::dc) by MW4PR03CA0226.outlook.office365.com (2603:10b6:303:b9::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:51:25 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT087.mail.protection.outlook.com (10.13.174.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:51:25 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:51:06 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:51:05 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:51:00 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 18/20] vfio/common: Optimize device dirty page tracking with vIOMMU Date: Wed, 22 Feb 2023 19:49:13 +0200 Message-ID: <20230222174915.5647-19-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT087:EE_|BL1PR12MB5093:EE_ X-MS-Office365-Filtering-Correlation-Id: b7c841c1-be44-44ea-d2f1-08db14fd6ac5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kxh5lEmF6nZ+P+i4Tfb8WXf5PZRpxsBxW0MIrP0UbkZwdQp+kxwq/xrgFwH30AMKMhbAQEmwcvygj6HRqG3wyEcYCX396YDJt1sDj9I7zFh7sncekufKJKD66oT2lO7Kiw12M2nIqcrBXoP8JmEeCwBNVKY5AiZSF0jIMyJ4sixR9ic4N/jIoCao6fwHexiGKxGOeB2ihkvMfntCGP7XFuMn9DhBiflyo4aN3v93bvNmoYU6qN852VTgXr5Auve1dzVed0o2WLYbwDCpPztxwZJRkA4X1VAuFre9PY49wVIYm7cSyDyRZ712+P+acQBsVbQArTJegzHC/AW8OX8bJLKBmfqrWlCoF3E6LW+vI6lW4xFLblhGdUgzgo57qXmDTdBhDC04BhCNvCif6Qz77xb9d6ppQwBnid7OjgippNnq+cCxmEhxzXjdqY+8irySZbhy/m6o4Y/sXYQ8oDObBBTsqr/DMqVAFZ9KdCi23BYBu83K/Df+CULY9WCkgUvgZWajeWRTDgevBlnAWBH0qg+yd82A1UqHi7JuTlOMOlUUqamjCBSGjV9xzh13yVxxMfaZ0d1sBOKnk7UKoctV/iePg5WS6iRXsqXZRlohSd+lJJv0N+/zzLcdDvMKFuBTbmVHsWhySxGf0Y++NBMyK4escsNNJvjtuixkT9ZHnpTgzTtNHI4lsAem8v01ITkWT5yb1LZSU6xGTz13/xJJfTXvLVpsADNOtb9uggSRbS+ofrR89+4DKS0tEYi4YsksMUGpbIcCNkYCSWfRcQLSwi6RvECmjXZw96C7WOf+Ozo= X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(136003)(396003)(39860400002)(346002)(376002)(5400799012)(451199018)(46966006)(40470700004)(36840700001)(66899018)(82310400005)(83380400001)(316002)(47076005)(336012)(54906003)(426003)(36756003)(6916009)(7636003)(82740400003)(34020700004)(70586007)(36860700001)(8676002)(6666004)(70206006)(2616005)(26005)(186003)(1076003)(40460700003)(7696005)(41300700001)(5660300002)(4326008)(7416002)(478600001)(8936002)(2906002)(40480700001)(86362001)(356005)(14143004)(12100799015); DIR:OUT; SFP:1501; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:51:25.2750 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b7c841c1-be44-44ea-d2f1-08db14fd6ac5 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT087.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5093 Received-SPF: softfail client-ip=2a01:111:f400:7eab::308; envelope-from=avihaih@nvidia.com; helo=NAM11-CO1-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org When vIOMMU is enabled, syncing dirty page bitmaps is done by replaying the vIOMMU mappings and querying the dirty bitmap for each mapping. With device dirty tracking this causes a lot of overhead, since the HW is queried many times (even with small idle guest this can end up with thousands of calls to HW). Optimize this by de-coupling dirty bitmap query from vIOMMU replay. Now a single dirty bitmap is queried per vIOMMU MR section, which is then used for all corresponding vIOMMU mappings within that MR section. Signed-off-by: Avihai Horon --- hw/vfio/common.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 2 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 1024788bcc..f16a57d42b 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1946,8 +1946,42 @@ out: typedef struct { IOMMUNotifier n; VFIOGuestIOMMU *giommu; + VFIOBitmap *vbmap; } vfio_giommu_dirty_notifier; +static int vfio_iommu_set_dirty_bitmap(VFIOContainer *container, + vfio_giommu_dirty_notifier *gdn, + hwaddr iova, hwaddr size, + ram_addr_t ram_addr) +{ + VFIOBitmap *vbmap = gdn->vbmap; + VFIOBitmap *dst_vbmap; + hwaddr start_iova = REAL_HOST_PAGE_ALIGN(gdn->n.start); + hwaddr copy_offset; + + dst_vbmap = vfio_bitmap_alloc(size); + if (!dst_vbmap) { + return -errno; + } + + if (!vfio_iommu_range_is_device_tracked(container, iova, size)) { + bitmap_set(dst_vbmap->bitmap, 0, dst_vbmap->pages); + + goto out; + } + + copy_offset = (iova - start_iova) / qemu_real_host_page_size(); + bitmap_copy_with_src_offset(dst_vbmap->bitmap, vbmap->bitmap, copy_offset, + dst_vbmap->pages); + +out: + cpu_physical_memory_set_dirty_lebitmap(dst_vbmap->bitmap, ram_addr, + dst_vbmap->pages); + vfio_bitmap_dealloc(dst_vbmap); + + return 0; +} + static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) { vfio_giommu_dirty_notifier *gdn = container_of(n, @@ -1968,8 +2002,15 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) rcu_read_lock(); if (vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) { - ret = vfio_get_dirty_bitmap(container, iova, iotlb->addr_mask + 1, - translated_addr); + if (gdn->vbmap) { + ret = vfio_iommu_set_dirty_bitmap(container, gdn, iova, + iotlb->addr_mask + 1, + translated_addr); + } else { + ret = vfio_get_dirty_bitmap(container, iova, iotlb->addr_mask + 1, + translated_addr); + } + if (ret) { error_report("vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx") = %d (%s)", @@ -2033,6 +2074,7 @@ static int vfio_sync_iommu_dirty_bitmap(VFIOContainer *container, { VFIOGuestIOMMU *giommu; bool found = false; + VFIOBitmap *vbmap = NULL; Int128 llend; vfio_giommu_dirty_notifier gdn; int idx; @@ -2050,6 +2092,7 @@ static int vfio_sync_iommu_dirty_bitmap(VFIOContainer *container, } gdn.giommu = giommu; + gdn.vbmap = NULL; idx = memory_region_iommu_attrs_to_index(giommu->iommu_mr, MEMTXATTRS_UNSPECIFIED); @@ -2057,11 +2100,49 @@ static int vfio_sync_iommu_dirty_bitmap(VFIOContainer *container, section->size); llend = int128_sub(llend, int128_one()); + /* + * Optimize device dirty tracking if the MR section is at least partially + * tracked. Optimization is done by querying a single dirty bitmap for the + * entire range instead of querying dirty bitmap for each vIOMMU mapping. + */ + if (vfio_devices_all_device_dirty_tracking(container)) { + hwaddr start = REAL_HOST_PAGE_ALIGN(section->offset_within_region); + hwaddr end = int128_get64(llend); + hwaddr size; + int ret; + + if (start >= container->giommu_tracked_range) { + goto notifier_init; + } + + size = REAL_HOST_PAGE_ALIGN( + MIN(container->giommu_tracked_range - 1, end) - start); + + vbmap = vfio_bitmap_alloc(size); + if (!vbmap) { + return -errno; + } + + ret = vfio_devices_query_dirty_bitmap(container, vbmap, start, size); + if (ret) { + vfio_bitmap_dealloc(vbmap); + + return ret; + } + + gdn.vbmap = vbmap; + } + +notifier_init: iommu_notifier_init(&gdn.n, vfio_iommu_map_dirty_notify, IOMMU_NOTIFIER_MAP, section->offset_within_region, int128_get64(llend), idx); memory_region_iommu_replay(giommu->iommu_mr, &gdn.n); + if (vbmap) { + vfio_bitmap_dealloc(vbmap); + } + return 0; } From patchwork Wed Feb 22 17:49:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149410 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1538BC61DA4 for ; Wed, 22 Feb 2023 17:52:18 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtHG-0003JD-17; Wed, 22 Feb 2023 12:51:38 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtHC-0003Dw-Cx for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:34 -0500 Received: from mail-bn8nam12hn2032f.outbound.protection.outlook.com ([2a01:111:f400:fe5b::32f] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtHA-0004mT-JZ for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:34 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NZY0K3NEeDWfK9LWJmijhr1L/9gmIQInumhatMKDtQAAWwJ3aYIGV05/MabIc+d+FJN0lTT1hpVNVC61WkcmlJJvacRTOk7NvocYIcAMV/l48IHPRkyPL2j5OrLVhvk8f/FwxnETHMsCqUfd5q67jtwUJScnXkPPv9NsH5kLieMZOYyupbSYoyxF7NNdbwiZEGrpoKcg8BPLD/EWENNeem74Bqm0VbF9ecwH0+nqkoWV197Vfy1LaLlbp64OF/gbwG7VOTL9s24PA7bQpWkfbic0vVMpZIu2ZOAr+U+gvxyWuX6o9cK9VlRya/lDxmHkcJCxjyqO+BItFc4Curq+qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vKPX17EF4CWtPAgcRWN71q21MhmJKg9uuuViT8+0PUQ=; b=hlS0AeTHTFFkpYULTy+os/PDNuifsm/Xx781aRi4OoVqniAZc//REDxACEFprJVa3Ok88l7ag1MeaYjt3PpxipUUtbbC+l4RXqxxDjW/Lx//bi6kCY7GdT/4ddLSRCWh80CaoI+3nNOUCYqcPyi+J6quvcV5gfeMoHxB7jdeJZ0ZqUMuqxJMCZrL5z0Iu7AROXIrAabdE5q6RxaRfP8pHiaU8lXa7qCoL+G6+AfXbl8WMr1Oh7qQrIspN9FENV1Yx6mFGZnaYJfSoInNlhrkrglRUdbgS2LxtShuD2J4lfc0OCJE3uH7qt9BXJuDaHCSNZyww073izTTsoMbhsBMbQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vKPX17EF4CWtPAgcRWN71q21MhmJKg9uuuViT8+0PUQ=; b=UKjeqCZjy4FiIZSGSWCX06mUSEd6CfeRtLnvPXxxS9ivRZdpyc3Cq5S2ZLzhfraE5oI7GlUouf9ZK2twxNYp5uwJM3z/djOce6WWOdLNqT6tRHKlveLVR4z/1thQclQ/D7jjxvySkctEqzlawzfVzYgoqmP3OGV6t24dcQLP7DJ92wXhRV60WF+HY+p4Wg6hVix+PMODzKQA6A3FiSxT3PvXxHaijwkpajTWmDbbHc42xcL+rDsgzF1Qi9OapXSL1J899/xajH44lWCOIm4TJch+cu+u3qsISvXIwStgCBwxnIHMYW3+sfkuP06bAeFqVLui7FOi4Rd8WOaCdhGH9A== Received: from MW4PR03CA0222.namprd03.prod.outlook.com (2603:10b6:303:b9::17) by DS0PR12MB7726.namprd12.prod.outlook.com (2603:10b6:8:130::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 17:51:29 +0000 Received: from CO1NAM11FT087.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b9:cafe::55) by MW4PR03CA0222.outlook.office365.com (2603:10b6:303:b9::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.20 via Frontend Transport; Wed, 22 Feb 2023 17:51:28 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT087.mail.protection.outlook.com (10.13.174.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:51:28 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:51:11 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:51:11 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:51:06 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 19/20] vfio/migration: Query device dirty page tracking support Date: Wed, 22 Feb 2023 19:49:14 +0200 Message-ID: <20230222174915.5647-20-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT087:EE_|DS0PR12MB7726:EE_ X-MS-Office365-Filtering-Correlation-Id: 01e12957-6020-42ee-6674-08db14fd6c88 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /PQMHbeZ4kHS93gifaZaDgW05KmKfKRy6bm7OZedXATY7XF7HsA+l5yN0sK90A0mOvKgp5SPx50GEZJZ9N5uXKCmxC0EK97D7bz4su9irQJ6J0ezfs8Vj6IOZM9E1/2qib2bNLEzPn5sIECbYFhQp0/quiqh0x15sDCSWButDyl1LF1fwBkaDIhmIA9wXWvmbzPeHFIKyv6M+CmSWw/AmGUHvqX2pHZan/X1J/um9DRDmJrD28Y/mo5+SNRhXQvV1rAKW3djkWSOC/IDIDzXd9cCydTpN94apqNTUpnbXqD8vIqyAN/aTyHneKjm573LX4kZWa8CD6TBk9Rg8kHXDtVf9MBuZKdOzNhLq79QVh58rgSbMdH1cFaS5sAatbuJGF3z9ZkXvKX8YE5XWjUuX/Ad75lr51EJAG03t0jM1BCFb/cefllcLB/Y9fJ6AWpYWYEVR/TAK9xwA3SebsR99MjuAEFNYJU15O0kmZwdjd2u73lqBvXBYViXeoGwnM8a9oa1fvGk09OsliljvbxAWX31dW3Lxr2YcE8OA9thdER23l16xIRDxeZvIHbyU5av2N73I9aEQXl02JosbDp8VphNjyfNAM4U1OvHfXpwVYThq0d3mtNKkK18aqlnUrfFh4IWmxC2Q9F2hnu3UbC/jMqs1pa0Gerl9BEA7gg9j/mLRqndjEG7AP0L3J/U7fSQo74R9J29qhl3KzLPUHteu4/5B/7jUGFa5/mG+B0/5EOSBDE/PZXuq8q8QdWu7ogNnrk4kvno7hxkcXKmCxTs+WaEOVdX7lxm6VDjEKXBG1w= X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(376002)(396003)(39860400002)(136003)(346002)(451199018)(5400799012)(46966006)(40470700004)(36840700001)(186003)(426003)(356005)(26005)(336012)(2616005)(1076003)(6666004)(5660300002)(41300700001)(36756003)(47076005)(86362001)(8936002)(2906002)(40480700001)(82740400003)(7696005)(40460700003)(82310400005)(6916009)(8676002)(54906003)(4326008)(36860700001)(34020700004)(70586007)(316002)(70206006)(7416002)(7636003)(478600001)(14143004)(12100799015); DIR:OUT; SFP:1501; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:51:28.2279 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 01e12957-6020-42ee-6674-08db14fd6c88 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT087.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7726 Received-SPF: softfail client-ip=2a01:111:f400:fe5b::32f; envelope-from=avihaih@nvidia.com; helo=NAM12-BN8-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Joao Martins Now that everything has been set up for device dirty page tracking, query the device for device dirty page tracking support. Signed-off-by: Joao Martins Signed-off-by: Avihai Horon --- hw/vfio/migration.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index 307983d57d..ae2be3dd3a 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -713,6 +713,19 @@ static int vfio_migration_query_flags(VFIODevice *vbasedev, uint64_t *mig_flags) return 0; } +static bool vfio_dma_logging_supported(VFIODevice *vbasedev) +{ + uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature), + sizeof(uint64_t))] = {}; + struct vfio_device_feature *feature = (struct vfio_device_feature *)buf; + + feature->argsz = sizeof(buf); + feature->flags = + VFIO_DEVICE_FEATURE_PROBE | VFIO_DEVICE_FEATURE_DMA_LOGGING_START; + + return !ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature); +} + static int vfio_migration_init(VFIODevice *vbasedev) { int ret; @@ -748,6 +761,8 @@ static int vfio_migration_init(VFIODevice *vbasedev) migration->data_fd = -1; migration->mig_flags = mig_flags; + vbasedev->dirty_pages_supported = vfio_dma_logging_supported(vbasedev); + oid = vmstate_if_get_id(VMSTATE_IF(DEVICE(obj))); if (oid) { path = g_strdup_printf("%s/vfio", oid); From patchwork Wed Feb 22 17:49:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Avihai Horon X-Patchwork-Id: 13149412 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2CF78C636D6 for ; Wed, 22 Feb 2023 17:52:44 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pUtHU-0003ok-2d; Wed, 22 Feb 2023 12:51:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtHJ-0003QX-9v for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:42 -0500 Received: from mail-co1nam11on2062e.outbound.protection.outlook.com ([2a01:111:f400:7eab::62e] helo=NAM11-CO1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pUtHH-0004qD-9h for qemu-devel@nongnu.org; Wed, 22 Feb 2023 12:51:40 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=O8FZFBCyJ6oyHWSrfswi1PII5QGTzzgWFLWdK4g1881/8hYiUhYLvErDAVvuOEEnotcjT+n8X8K4EiFCacPReNjKd1wwJ26QAiK3GzKC3KKllOs2eiK7GckCe2cYWfniYB+0v/9SnW8ILg+5adK8BqUnjJok2m1xnJIr7HByDUDaLDnjFIDRjMs9vcLpvqQis3VDfMfmQSQqxlJPFA2lTHRvBGK47+VonbZ4155bfIYT/JZcIpZmNiZJtHoSwQNWUehpTGXm/oQvHoI8VBqN1CNIikVVsmhZhHpY1kvQrtLP9f4sPjElk+JmPlsyjsjWrUpbS3GIrhONb9vAmDmIFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=0jLoCLeXqy63DEZII3YZpoJOiPqLkWT9hMNOKwJkDoU=; b=mT10ohdGN/ADm4lVePcQLbZD/jaXy8FwNeKEzP7sO/Tn6Drg1gvGeRSbz6E/U53igSr6OjODAyA4W1IJ1QMcLQt0tEGVAhwxvAMiuONqwZlOz0THqchj2ZlYKfjEUQe9IIeOqAD4+qVX5/U337XYKKHNqDO2R3xQSLFQc/71nipjUK/ddlMY4tFVN7v2nQawB0r1P2kW9UL7m7YRehB+Ck7/W29Wz96XWC0wlAaSeCvDP03KhczrMVLqekGpeimIaoNumjE2C9oZoFfUbhCuEl1JavJAyjCM0wmYHs1D5famuu3K/KjiTIDioBmxFQG1LirEDPc4CaLXb9VnDINteQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=nongnu.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0jLoCLeXqy63DEZII3YZpoJOiPqLkWT9hMNOKwJkDoU=; b=SgZ7kySzLnctmi6RZ4y9TnkfT26Fp2m9bWTMu4iR+JlYC1DdVXMRwi9QCGddJwlkE2OJMF1Pk9FAjKmelXQCF+NQqmevi6euTVEM1ajP9dphTDCa/zzjhmxME73IxWUlAH0gMEfrd1C7+AfKbc3lcCAnqpYEFor5EBcWCmu7d4fsnIGAyOkEp7ecMY8CcFHNzcm8Ye0aVH0tD/dmnUqnggsO2G1hyYhsV0y7vrNmpRC/4pGHeUyr2ulYjJP9ZKweXS+F1v1dDSTopxB2FiCr5+vuSeVGv7obmkBbSpUih+Ec2em/HPbGicjhrqUkCr5JlUWh6ok7xeFIlsbRlTqi8w== Received: from BN0PR04CA0127.namprd04.prod.outlook.com (2603:10b6:408:ed::12) by MW3PR12MB4569.namprd12.prod.outlook.com (2603:10b6:303:57::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 17:51:34 +0000 Received: from BL02EPF0000C403.namprd05.prod.outlook.com (2603:10b6:408:ed:cafe::d6) by BN0PR04CA0127.outlook.office365.com (2603:10b6:408:ed::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.19 via Frontend Transport; Wed, 22 Feb 2023 17:51:33 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL02EPF0000C403.mail.protection.outlook.com (10.167.241.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.14 via Frontend Transport; Wed, 22 Feb 2023 17:51:33 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:51:17 -0800 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Wed, 22 Feb 2023 09:51:17 -0800 Received: from vdi.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Wed, 22 Feb 2023 09:51:12 -0800 From: Avihai Horon To: CC: Alex Williamson , =?utf-8?q?C=C3=A9dric_Le_G?= =?utf-8?q?oater?= , Juan Quintela , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Peter Xu , "Jason Wang" , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "David Hildenbrand" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Yishai Hadas , Jason Gunthorpe , Maor Gottlieb , Avihai Horon , Kirti Wankhede , Tarun Gupta , Joao Martins Subject: [PATCH v2 20/20] docs/devel: Document VFIO device dirty page tracking Date: Wed, 22 Feb 2023 19:49:15 +0200 Message-ID: <20230222174915.5647-21-avihaih@nvidia.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20230222174915.5647-1-avihaih@nvidia.com> References: <20230222174915.5647-1-avihaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000C403:EE_|MW3PR12MB4569:EE_ X-MS-Office365-Filtering-Correlation-Id: a7ea2ff1-fe5a-402a-c6f3-08db14fd6faf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ek7ZIrqIuQ8lZ0ymtDEHZ4h4gdaPd15z2C2/xyfBGXcBBDliEkrc5BKpA0mfTZWeJid3cPKcC/G028LR9Jm8XuCYmZ3x2x9WLHNYfAzfAdtTEx30TUeYQCsJYLa4Z0/8l0PmnpRIOou3mIUjNGJFjuxfTmtrpmo9LEhp800EtusKw4QN8TlrATp3MxMkOjBl9TW+IqcVQVG2RX4vitmg+TYC2fJTIkX4B2E3/voVmlOj8J4TV3Z+QrE7aXGrv6uhnkIr+sTQUnjCjaSgEhz3N3oArIUntyxIsYXrn3DeYtdVCNbsLBqQU0qXTnXM6du2v7dNl+fO8d3CYO799kfYNPyL5fnnhU3SCizsb+9BzNsvVFcexcJCzX462slwZHdFbK/ptokOaNZAVEqNIF79pKZx4O/qmQQhS7C3yLwp4UG4cQd0PlWZcfQQHzza6WaeunyCjy5W77XmYkGL8DD1WxwW4V7ogJq8p5R1NGaTevoGepZ2joD1YzMdRlL5FhfTCQSfUomAXakp/8L3cM2sM3cdswp+hyF/tTTTBEaRlNmxDGx8ldh/uhNMyWliKuplneEfpkmKweghRHcv+PxmyTTh4fh/DTLAVsFxXKQOFMufY0b8QCk9/5HsAyPvYYnKv9J0ETK28gyafaUbEqaxpl8ZBjZUHb9rBjhZcazXTBFzpXXtvwNOcj3QmIs+QdS5awm8VmpSP+ezhKuD/J6zdRF2IXFY+Vj0B9Fn/+rpa+Q= X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230025)(4636009)(346002)(376002)(136003)(39860400002)(396003)(451199018)(36840700001)(46966006)(40470700004)(186003)(26005)(8936002)(6916009)(40460700003)(7416002)(1076003)(336012)(5660300002)(2616005)(4326008)(47076005)(426003)(83380400001)(82310400005)(40480700001)(36756003)(478600001)(36860700001)(41300700001)(7636003)(82740400003)(7696005)(8676002)(70206006)(70586007)(316002)(86362001)(54906003)(2906002)(356005)(14143004); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 17:51:33.3941 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a7ea2ff1-fe5a-402a-c6f3-08db14fd6faf X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000C403.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4569 Received-SPF: softfail client-ip=2a01:111:f400:7eab::62e; envelope-from=avihaih@nvidia.com; helo=NAM11-CO1-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Adjust the VFIO dirty page tracking documentation and add a section to describe device dirty page tracking. Signed-off-by: Avihai Horon Reviewed-by: Cédric Le Goater --- docs/devel/vfio-migration.rst | 50 ++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/docs/devel/vfio-migration.rst b/docs/devel/vfio-migration.rst index ba80b9150d..a432cda081 100644 --- a/docs/devel/vfio-migration.rst +++ b/docs/devel/vfio-migration.rst @@ -71,22 +71,37 @@ System memory dirty pages tracking ---------------------------------- A ``log_global_start`` and ``log_global_stop`` memory listener callback informs -the VFIO IOMMU module to start and stop dirty page tracking. A ``log_sync`` -memory listener callback marks those system memory pages as dirty which are -used for DMA by the VFIO device. The dirty pages bitmap is queried per -container. All pages pinned by the vendor driver through external APIs have to -be marked as dirty during migration. When there are CPU writes, CPU dirty page -tracking can identify dirtied pages, but any page pinned by the vendor driver -can also be written by the device. There is currently no device or IOMMU -support for dirty page tracking in hardware. +the VFIO dirty tracking module to start and stop dirty page tracking. A +``log_sync`` memory listener callback queries the dirty page bitmap from the +dirty tracking module and marks system memory pages which were DMA-ed by the +VFIO device as dirty. The dirty page bitmap is queried per container. + +Currently there are two ways dirty page tracking can be done: +(1) Device dirty tracking: +In this method the device is responsible to log and report its DMAs. This +method can be used only if the device is capable of tracking its DMAs. +Discovering device capability, starting and stopping dirty tracking, and +syncing the dirty bitmaps from the device are done using the DMA logging uAPI. +More info about the uAPI can be found in the comments of the +``vfio_device_feature_dma_logging_control`` and +``vfio_device_feature_dma_logging_report`` structures in the header file +linux-headers/linux/vfio.h. + +(2) VFIO IOMMU module: +In this method dirty tracking is done by IOMMU. However, there is currently no +IOMMU support for dirty page tracking. For this reason, all pages are +perpetually marked dirty, unless the device driver pins pages through external +APIs in which case only those pinned pages are perpetually marked dirty. + +If the above two methods are not supported, all pages are perpetually marked +dirty by QEMU. By default, dirty pages are tracked during pre-copy as well as stop-and-copy -phase. So, a page pinned by the vendor driver will be copied to the destination -in both phases. Copying dirty pages in pre-copy phase helps QEMU to predict if -it can achieve its downtime tolerances. If QEMU during pre-copy phase keeps -finding dirty pages continuously, then it understands that even in stop-and-copy -phase, it is likely to find dirty pages and can predict the downtime -accordingly. +phase. So, a page marked as dirty will be copied to the destination in both +phases. Copying dirty pages in pre-copy phase helps QEMU to predict if it can +achieve its downtime tolerances. If QEMU during pre-copy phase keeps finding +dirty pages continuously, then it understands that even in stop-and-copy phase, +it is likely to find dirty pages and can predict the downtime accordingly. QEMU also provides a per device opt-out option ``pre-copy-dirty-page-tracking`` which disables querying the dirty bitmap during pre-copy phase. If it is set to @@ -97,10 +112,9 @@ System memory dirty pages tracking when vIOMMU is enabled --------------------------------------------------------- With vIOMMU, an IO virtual address range can get unmapped while in pre-copy -phase of migration. In that case, the unmap ioctl returns any dirty pages in -that range and QEMU reports corresponding guest physical pages dirty. During -stop-and-copy phase, an IOMMU notifier is used to get a callback for mapped -pages and then dirty pages bitmap is fetched from VFIO IOMMU modules for those +phase of migration. In that case, dirty page bitmap for this range is queried +and synced with QEMU. During stop-and-copy phase, an IOMMU notifier is used to +get a callback for mapped pages and then dirty page bitmap is fetched for those mapped ranges. Flow of state changes during Live migration