From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 7FD13A034E; Thu, 24 Feb 2022 14:29:48 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 82A7642708; Thu, 24 Feb 2022 14:29:41 +0100 (CET) Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2053.outbound.protection.outlook.com [40.107.220.53]) by mails.dpdk.org (Postfix) with ESMTP id 804A0426F0 for ; Thu, 24 Feb 2022 14:29:37 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CAIoz0x23oj42h9o4q9E6TN4CPg1iDWPpFTE0PvYR5KG+KIB7pMzaAi2ES68VMHsBvCY/fSvwFXkcPSsWFXGZKEFXNPxQxF/kSbwHP0wKbTjA9tnjQ6ESlwFSQBFtULZcOLaTYbCzhF1Rs23kmAKXBeqAV38TyhQ88hNaPk2CL3rYJGQtoNLBv1TYe9VoZpksFL+/FMvAPqpSFUFyC9HvTI5ZzG4TPgp7mqbQa6rOy9MxO+ZgeXrJsSbU0Q88Aq8LIHnyhMRXf3C9o9Bw8Dr/aH8tWPRA22lohzFgTCX0kHGd6KjdH5HJgPRZIJF2DNbpS5pMsBldwkItze6MRxtWQ== 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=I+edqd4XwS71WT5lUkC7h6f+6ePZM3Y4kHrGW+jc4f0=; b=UbK5qhh6VRc6lf6RWROBR/WLI4KmYZpr3f1rUtWEGvp1kaxT8telFNJvPObIwAI2UjLVTQpIRcZV0v34IM/GEJsmMp0nUA56Bx+crZUxnxroTtQNYDRASknAH4cG3igjNqbqIq6t0xXZZJA5vfPREQBMPY+c5Mw5eueNQ4A1hecMdZptgd7+fvRPcqekYpmxwyzZeOjn6R4/2G1+nqB+GdDT7C7pomsAIYBtozvMgWcQ8wZdGmPUeFade0XV8WELJ89imz6VAIwJ8Dt7eWJteuVbygdcfVYJPCy7yn21Rw7bHvH8es81Gfo/+49kT1bmHjo7QMAl9MbMJC7S9ilYbg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.238) smtp.rcpttodomain=dpdk.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=I+edqd4XwS71WT5lUkC7h6f+6ePZM3Y4kHrGW+jc4f0=; b=pEBFmjvlyUoDQh6u+TWfVIwkVzFVGAPP+aZ6RJaIwBtf9n9zx40+7YKgo+ItmpyAsrqzW39KWf+irzAoU60vWc+1MrzVU5sTeKeNq0C2IeVxyzNqS3x59tk2wWoWEy0eYlNaQKxQJoMqmTXZeLB61PlmgvKn+NtihHyKU5mJAWZ1jI0HNkGvcFjYlyk8flTgVoG2q9D+9jNg4z5+Xg4fmM6jHs2sSrLwLZGVBGA6p/h1Z8wSsvrG80X4L+u9r4qn0LBthQ6iOXDtuxLhKgw1aCBhlD/xwFEdsSEZYGAUABrvF5c49SyUNEgq5WQCiS8aKdWq7/fD1jSztxwOv3XAMQ== Received: from DS7PR05CA0107.namprd05.prod.outlook.com (2603:10b6:8:56::25) by MW2PR12MB2459.namprd12.prod.outlook.com (2603:10b6:907:c::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4995.24; Thu, 24 Feb 2022 13:29:34 +0000 Received: from DM6NAM11FT022.eop-nam11.prod.protection.outlook.com (2603:10b6:8:56:cafe::8f) by DS7PR05CA0107.outlook.office365.com (2603:10b6:8:56::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.9 via Frontend Transport; Thu, 24 Feb 2022 13:29:34 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.238) 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 12.22.5.238 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.238; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.238) by DM6NAM11FT022.mail.protection.outlook.com (10.13.172.210) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5017.22 via Frontend Transport; Thu, 24 Feb 2022 13:29:33 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by DRHQMAIL105.nvidia.com (10.27.9.14) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Thu, 24 Feb 2022 13:29:29 +0000 Received: from nvidia.com (10.126.231.35) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.9; Thu, 24 Feb 2022 05:29:27 -0800 From: Xueming Li To: CC: , Matan Azrad , "Viacheslav Ovsiienko" Subject: [PATCH 5/7] vdpa/mlx5: cache and reuse hardware resources Date: Thu, 24 Feb 2022 21:28:18 +0800 Message-ID: <20220224132820.1939650-6-xuemingl@nvidia.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220224132820.1939650-1-xuemingl@nvidia.com> References: <20220224132820.1939650-1-xuemingl@nvidia.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.126.231.35] X-ClientProxiedBy: rnnvmail203.nvidia.com (10.129.68.9) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9d820bf2-8772-4754-153c-08d9f799b1d3 X-MS-TrafficTypeDiagnostic: MW2PR12MB2459:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zaSylq4kXcAW8go2xMId+ol68MgWisFD6GUhIZzdxfPYyHMJrOkhnBVD6bY7KbmQ+XRwZTGIPlxDiBJq6eF4CONpgA/030RH/IUTwNMDJl6Q8Aw9cDxLiuDrEoAN9YqkRrG460xFonY9gZzn7VxCHnKJM9vATKth6Sr6gPYUXCRTaHr/pzKzVPFDlBEwpXQeBbuPOfg66A6xL8csTYxkgEKeqTMKRrEv3Vy4lrbsrAmKXJrSyezwI8Je/XZWC/XsBcUILvtLy+m59nelSeDPbZJllt3YcCJlUPjDn5wd5XHZMhBaV+g7cO4n1EE+GaJIC/hsjx0BUVLIxISO+BXEMvbOELoyUtqX5Bvoef1qUfuriDVrG5qPTT5BHUk86MEzUJGIVUsgiAzZJhpiuWh54Pgkk9iO7MPFk1GNzCSHlCrRaopX63c2WXl0rEBMUrgxdWKdlV9e0Vcg08V+cYYFawFmgYjBjy/bM4oeRHrk/A0oj07WYoV8bUApd9Mt7mJQxXErlsWonsvVsQ8n8sU6NwM4s6uESi8BoxG1oo5Ve+c3o7PWM5HvXFqdIh6zTkizXm9yvSGqkZ/U5Pqisjq1R39SDytM4zjbx0rMB2pp2Ggn4Y6s6OtRvOt+9FzEH6s3P4Rtz21jqSBoGG6kUS6EfAcu22+YfwJV1B9S1xhxvqKom1QfHzGu3TYFC0+fuxA7R5UGOpLks45Wu18O60jRjg== X-Forefront-Antispam-Report: CIP:12.22.5.238; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230001)(4636009)(36840700001)(46966006)(40470700004)(36756003)(4326008)(40460700003)(47076005)(83380400001)(336012)(8936002)(426003)(5660300002)(55016003)(2906002)(70206006)(70586007)(36860700001)(107886003)(508600001)(6286002)(186003)(6666004)(54906003)(16526019)(82310400004)(81166007)(356005)(8676002)(316002)(7696005)(26005)(6916009)(2616005)(86362001)(1076003)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Feb 2022 13:29:33.3843 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9d820bf2-8772-4754-153c-08d9f799b1d3 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.238]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT022.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR12MB2459 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org During device suspend and resume, resources are not changed normally. When huge resources allocated to VM, like huge memory size or lots of queues, time spent on release and recreate became significant. To speed up, this patch reuse resoruces like VM MR and VirtQ memory if not changed. Signed-off-by: Xueming Li --- drivers/vdpa/mlx5/mlx5_vdpa.c | 11 ++++- drivers/vdpa/mlx5/mlx5_vdpa.h | 12 ++++- drivers/vdpa/mlx5/mlx5_vdpa_mem.c | 27 ++++++++++- drivers/vdpa/mlx5/mlx5_vdpa_virtq.c | 73 +++++++++++++++++++++-------- 4 files changed, 99 insertions(+), 24 deletions(-) diff --git a/drivers/vdpa/mlx5/mlx5_vdpa.c b/drivers/vdpa/mlx5/mlx5_vdpa.c index 7e57ae715a8..dbaa590d5d1 100644 --- a/drivers/vdpa/mlx5/mlx5_vdpa.c +++ b/drivers/vdpa/mlx5/mlx5_vdpa.c @@ -241,6 +241,13 @@ mlx5_vdpa_mtu_set(struct mlx5_vdpa_priv *priv) return kern_mtu == vhost_mtu ? 0 : -1; } +static void +mlx5_vdpa_dev_cache_clean(struct mlx5_vdpa_priv *priv) +{ + mlx5_vdpa_virtqs_cleanup(priv); + mlx5_vdpa_mem_dereg(priv); +} + static int mlx5_vdpa_dev_close(int vid) { @@ -260,7 +267,8 @@ mlx5_vdpa_dev_close(int vid) } mlx5_vdpa_steer_unset(priv); mlx5_vdpa_virtqs_release(priv); - mlx5_vdpa_mem_dereg(priv); + if (priv->lm_mr.addr) + mlx5_os_wrapped_mkey_destroy(&priv->lm_mr); priv->state = MLX5_VDPA_STATE_PROBED; priv->vid = 0; /* The mutex may stay locked after event thread cancel - initiate it. */ @@ -661,6 +669,7 @@ mlx5_vdpa_release_dev_resources(struct mlx5_vdpa_priv *priv) { uint32_t i; + mlx5_vdpa_dev_cache_clean(priv); mlx5_vdpa_event_qp_global_release(priv); mlx5_vdpa_err_event_unset(priv); if (priv->steer.tbl) diff --git a/drivers/vdpa/mlx5/mlx5_vdpa.h b/drivers/vdpa/mlx5/mlx5_vdpa.h index e0ba20b953c..540bf87a352 100644 --- a/drivers/vdpa/mlx5/mlx5_vdpa.h +++ b/drivers/vdpa/mlx5/mlx5_vdpa.h @@ -289,13 +289,21 @@ int mlx5_vdpa_err_event_setup(struct mlx5_vdpa_priv *priv); void mlx5_vdpa_err_event_unset(struct mlx5_vdpa_priv *priv); /** - * Release a virtq and all its related resources. + * Release virtqs and resources except that to be reused. * * @param[in] priv * The vdpa driver private structure. */ void mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv); +/** + * Cleanup cached resources of all virtqs. + * + * @param[in] priv + * The vdpa driver private structure. + */ +void mlx5_vdpa_virtqs_cleanup(struct mlx5_vdpa_priv *priv); + /** * Create all the HW virtqs resources and all their related resources. * @@ -323,7 +331,7 @@ int mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv); int mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable); /** - * Unset steering and release all its related resources- stop traffic. + * Unset steering - stop traffic. * * @param[in] priv * The vdpa driver private structure. diff --git a/drivers/vdpa/mlx5/mlx5_vdpa_mem.c b/drivers/vdpa/mlx5/mlx5_vdpa_mem.c index 62f5530e91d..d6e3dd664b5 100644 --- a/drivers/vdpa/mlx5/mlx5_vdpa_mem.c +++ b/drivers/vdpa/mlx5/mlx5_vdpa_mem.c @@ -32,8 +32,6 @@ mlx5_vdpa_mem_dereg(struct mlx5_vdpa_priv *priv) entry = next; } SLIST_INIT(&priv->mr_list); - if (priv->lm_mr.addr) - mlx5_os_wrapped_mkey_destroy(&priv->lm_mr); if (priv->vmem) { free(priv->vmem); priv->vmem = NULL; @@ -149,6 +147,23 @@ mlx5_vdpa_vhost_mem_regions_prepare(int vid, uint8_t *mode, uint64_t *mem_size, return mem; } +static int +mlx5_vdpa_mem_cmp(struct rte_vhost_memory *mem1, struct rte_vhost_memory *mem2) +{ + uint32_t i; + + if (mem1->nregions != mem2->nregions) + return -1; + for (i = 0; i < mem1->nregions; i++) { + if (mem1->regions[i].guest_phys_addr != + mem2->regions[i].guest_phys_addr) + return -1; + if (mem1->regions[i].size != mem2->regions[i].size) + return -1; + } + return 0; +} + #define KLM_SIZE_MAX_ALIGN(sz) ((sz) > MLX5_MAX_KLM_BYTE_COUNT ? \ MLX5_MAX_KLM_BYTE_COUNT : (sz)) @@ -191,6 +206,14 @@ mlx5_vdpa_mem_register(struct mlx5_vdpa_priv *priv) if (!mem) return -rte_errno; + if (priv->vmem != NULL) { + if (mlx5_vdpa_mem_cmp(mem, priv->vmem) == 0) { + /* VM memory not changed, reuse resources. */ + free(mem); + return 0; + } + mlx5_vdpa_mem_dereg(priv); + } priv->vmem = mem; for (i = 0; i < mem->nregions; i++) { reg = &mem->regions[i]; diff --git a/drivers/vdpa/mlx5/mlx5_vdpa_virtq.c b/drivers/vdpa/mlx5/mlx5_vdpa_virtq.c index 6bda9f1814a..c42846ecb3c 100644 --- a/drivers/vdpa/mlx5/mlx5_vdpa_virtq.c +++ b/drivers/vdpa/mlx5/mlx5_vdpa_virtq.c @@ -66,10 +66,33 @@ mlx5_vdpa_virtq_kick_handler(void *cb_arg) DRV_LOG(DEBUG, "Ring virtq %u doorbell.", virtq->index); } +/* Release cached VQ resources. */ +void +mlx5_vdpa_virtqs_cleanup(struct mlx5_vdpa_priv *priv) +{ + unsigned int i, j; + + for (i = 0; i < priv->caps.max_num_virtio_queues * 2; i++) { + struct mlx5_vdpa_virtq *virtq = &priv->virtqs[i]; + + for (j = 0; j < RTE_DIM(virtq->umems); ++j) { + if (virtq->umems[j].obj) { + claim_zero(mlx5_glue->devx_umem_dereg + (virtq->umems[j].obj)); + virtq->umems[j].obj = NULL; + } + if (virtq->umems[j].buf) { + rte_free(virtq->umems[j].buf); + virtq->umems[j].buf = NULL; + } + virtq->umems[j].size = 0; + } + } +} + static int mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq) { - unsigned int i; int ret = -EAGAIN; if (rte_intr_fd_get(virtq->intr_handle) >= 0) { @@ -94,13 +117,6 @@ mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq) claim_zero(mlx5_devx_cmd_destroy(virtq->virtq)); } virtq->virtq = NULL; - for (i = 0; i < RTE_DIM(virtq->umems); ++i) { - if (virtq->umems[i].obj) - claim_zero(mlx5_glue->devx_umem_dereg - (virtq->umems[i].obj)); - rte_free(virtq->umems[i].buf); - } - memset(&virtq->umems, 0, sizeof(virtq->umems)); if (virtq->eqp.fw_qp) mlx5_vdpa_event_qp_destroy(&virtq->eqp); virtq->notifier_state = MLX5_VDPA_NOTIFIER_STATE_DISABLED; @@ -120,7 +136,6 @@ mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv) claim_zero(mlx5_devx_cmd_destroy(virtq->counters)); } priv->features = 0; - memset(priv->virtqs, 0, sizeof(*virtq) * priv->nr_virtqs); priv->nr_virtqs = 0; } @@ -215,6 +230,8 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index) ret = rte_vhost_get_vhost_vring(priv->vid, index, &vq); if (ret) return -1; + if (vq.size == 0) + return 0; virtq->index = index; virtq->vq_size = vq.size; attr.tso_ipv4 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4)); @@ -259,24 +276,42 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index) } /* Setup 3 UMEMs for each virtq. */ for (i = 0; i < RTE_DIM(virtq->umems); ++i) { - virtq->umems[i].size = priv->caps.umems[i].a * vq.size + - priv->caps.umems[i].b; - virtq->umems[i].buf = rte_zmalloc(__func__, - virtq->umems[i].size, 4096); - if (!virtq->umems[i].buf) { + uint32_t size; + void *buf; + struct mlx5dv_devx_umem *obj; + + size = priv->caps.umems[i].a * vq.size + priv->caps.umems[i].b; + if (virtq->umems[i].size == size && + virtq->umems[i].obj != NULL) { + /* Reuse registered memory. */ + memset(virtq->umems[i].buf, 0, size); + goto reuse; + } + if (virtq->umems[i].obj) + claim_zero(mlx5_glue->devx_umem_dereg + (virtq->umems[i].obj)); + if (virtq->umems[i].buf) + rte_free(virtq->umems[i].buf); + virtq->umems[i].size = 0; + virtq->umems[i].obj = NULL; + virtq->umems[i].buf = NULL; + buf = rte_zmalloc(__func__, size, 4096); + if (buf == NULL) { DRV_LOG(ERR, "Cannot allocate umem %d memory for virtq" " %u.", i, index); goto error; } - virtq->umems[i].obj = mlx5_glue->devx_umem_reg(priv->cdev->ctx, - virtq->umems[i].buf, - virtq->umems[i].size, - IBV_ACCESS_LOCAL_WRITE); - if (!virtq->umems[i].obj) { + obj = mlx5_glue->devx_umem_reg(priv->cdev->ctx, buf, size, + IBV_ACCESS_LOCAL_WRITE); + if (obj == NULL) { DRV_LOG(ERR, "Failed to register umem %d for virtq %u.", i, index); goto error; } + virtq->umems[i].size = size; + virtq->umems[i].buf = buf; + virtq->umems[i].obj = obj; +reuse: attr.umems[i].id = virtq->umems[i].obj->umem_id; attr.umems[i].offset = 0; attr.umems[i].size = virtq->umems[i].size; -- 2.35.1