From nobody Sun Nov 24 11:00:28 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8279C39ACC; Sun, 10 Nov 2024 13:48:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731246528; cv=none; b=R7ywesqltdJdMFOBLdTBqPBUUoxl0Uzxa136x421ifmrT88A/B1CD77PjH8ibvmz9u96+wiDGElzX0mNTyPqlQ/aI4KPVY1HOROO2OJgGCm/fwsxLvT6F8qvfuhUFh1M9UVuQR6nuIp01MCxda5CpzY5K3CpB2at/gPJPkb+CRE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731246528; c=relaxed/simple; bh=VQa5hPUNLRxzvwEAt3oNliqSysWpsiNm2rHDkhx27Qo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CbdcKu/N5MnCXGx5a7Fzjq5fgjKHBz9b1Bq44MFISPTG8bryIs5XfILgz3fsH7uc7Sl6DRE+5ZrtaRDEaAoIrrlFvSy6zO4xXdiHq0/OWQRPNaNSzSMa/IH0JS22w3lFRLG+2AFnUKzALvH0+B5N+ytSvEbi8KFEsrGvr4PZqJY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Tbwn+cXg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Tbwn+cXg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0CBDC4CED7; Sun, 10 Nov 2024 13:48:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731246528; bh=VQa5hPUNLRxzvwEAt3oNliqSysWpsiNm2rHDkhx27Qo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Tbwn+cXgi4quixPazZCRA6HhxSXDht8uuq+E2lgG1YeNDEgkUWomgWp99vh6T/skB zkiffBJS3FrHzXBAw31eL6cmacuooXGdYnFyYa3113h2V2l6Dhx1SdP9TR4s/X+Wrd g4JdYhy44M+FB7Z226q17VbOuQH4lM86NlzQUjyjyBBU4brH5FLDPBE7Dhwtq6Tt9A NT+ESahEqANE7VtzBUZJoh2iCr2wUjx+YwfWbXKtqV9aRKVz0nrxvtFY1kJgYLzkVc BQC+tIoyC13ZVB4MRe71XAwWPtrRHLIT48GvvoP4+bRyRgOSxGZwAias/hvLlX0YHu q0TZD00o562fQ== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Leon Romanovsky , Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, Randy Dunlap Subject: [PATCH v3 17/17] vfio/mlx5: Enable the DMA link API Date: Sun, 10 Nov 2024 15:47:04 +0200 Message-ID: <8d676c44cc3b90b2dc001c9d97dccdd85847866a.1731244445.git.leon@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Leon Romanovsky Remove intermediate scatter-gather table completely and enable new DMA link API. Signed-off-by: Leon Romanovsky --- drivers/vfio/pci/mlx5/cmd.c | 295 ++++++++++++++++------------------- drivers/vfio/pci/mlx5/cmd.h | 21 ++- drivers/vfio/pci/mlx5/main.c | 31 ---- 3 files changed, 148 insertions(+), 199 deletions(-) diff --git a/drivers/vfio/pci/mlx5/cmd.c b/drivers/vfio/pci/mlx5/cmd.c index 1832a6c1f35d..cde1481ed23c 100644 --- a/drivers/vfio/pci/mlx5/cmd.c +++ b/drivers/vfio/pci/mlx5/cmd.c @@ -345,25 +345,81 @@ static u32 *alloc_mkey_in(u32 npages, u32 pdn) return in; } =20 -static int create_mkey(struct mlx5_core_dev *mdev, u32 npages, - struct mlx5_vhca_data_buffer *buf, u32 *mkey_in, +static int create_mkey(struct mlx5_core_dev *mdev, u32 npages, u32 *mkey_i= n, u32 *mkey) { + int inlen =3D MLX5_ST_SZ_BYTES(create_mkey_in) + + sizeof(__be64) * round_up(npages, 2); + + return mlx5_core_create_mkey(mdev, mkey, mkey_in, inlen); +} + +static void unregister_dma_pages(struct mlx5_core_dev *mdev, u32 npages, + u32 *mkey_in, struct dma_iova_state *state, + enum dma_data_direction dir) +{ + dma_addr_t addr; __be64 *mtt; - int inlen; + int i; =20 - mtt =3D (__be64 *)MLX5_ADDR_OF(create_mkey_in, mkey_in, klm_pas_mtt); - if (buf) { - struct sg_dma_page_iter dma_iter; + WARN_ON_ONCE(dir =3D=3D DMA_NONE); =20 - for_each_sgtable_dma_page(&buf->table.sgt, &dma_iter, 0) - *mtt++ =3D cpu_to_be64(sg_page_iter_dma_address(&dma_iter)); + if (dma_use_iova(state)) { + dma_iova_destroy(mdev->device, state, npages * PAGE_SIZE, dir, 0); + } else { + mtt =3D (__be64 *)MLX5_ADDR_OF(create_mkey_in, mkey_in, + klm_pas_mtt); + for (i =3D npages - 1; i >=3D 0; i--) { + addr =3D be64_to_cpu(mtt[i]); + dma_unmap_page(mdev->device, addr, PAGE_SIZE, dir); + } } +} =20 - inlen =3D MLX5_ST_SZ_BYTES(create_mkey_in) + - sizeof(__be64) * round_up(npages, 2); +static int register_dma_pages(struct mlx5_core_dev *mdev, u32 npages, + struct page **page_list, u32 *mkey_in, + struct dma_iova_state *state, + enum dma_data_direction dir) +{ + dma_addr_t addr; + size_t mapped =3D 0; + __be64 *mtt; + int i, err; =20 - return mlx5_core_create_mkey(mdev, mkey, mkey_in, inlen); + WARN_ON_ONCE(dir =3D=3D DMA_NONE); + + mtt =3D (__be64 *)MLX5_ADDR_OF(create_mkey_in, mkey_in, klm_pas_mtt); + + if (dma_iova_try_alloc(mdev->device, state, 0, npages * PAGE_SIZE)) { + addr =3D state->addr; + for (i =3D 0; i < npages; i++) { + err =3D dma_iova_link(mdev->device, state, + page_to_phys(page_list[i]), mapped, + PAGE_SIZE, dir, 0); + if (err) + goto error; + *mtt++ =3D cpu_to_be64(addr); + addr +=3D PAGE_SIZE; + mapped +=3D PAGE_SIZE; + } + err =3D dma_iova_sync(mdev->device, state, 0, mapped); + if (err) + goto error; + } else { + for (i =3D 0; i < npages; i++) { + addr =3D dma_map_page(mdev->device, page_list[i], 0, + PAGE_SIZE, dir); + err =3D dma_mapping_error(mdev->device, addr); + if (err) + goto error; + *mtt++ =3D cpu_to_be64(addr); + } + } + return 0; + +error: + unregister_dma_pages(mdev, i, mkey_in, state, dir); + return err; } =20 static int mlx5vf_dma_data_buffer(struct mlx5_vhca_data_buffer *buf) @@ -379,96 +435,93 @@ static int mlx5vf_dma_data_buffer(struct mlx5_vhca_da= ta_buffer *buf) if (buf->mkey_in || !buf->npages) return -EINVAL; =20 - ret =3D dma_map_sgtable(mdev->device, &buf->table.sgt, buf->dma_dir, 0); - if (ret) - return ret; - buf->mkey_in =3D alloc_mkey_in(buf->npages, buf->migf->pdn); - if (!buf->mkey_in) { - ret =3D -ENOMEM; - goto err; - } + if (!buf->mkey_in) + return -ENOMEM; =20 - ret =3D create_mkey(mdev, buf->npages, buf, buf->mkey_in, &buf->mkey); + ret =3D register_dma_pages(mdev, buf->npages, buf->page_list, + buf->mkey_in, &buf->state, buf->dma_dir); + if (ret) + goto err_register_dma; + + ret =3D create_mkey(mdev, buf->npages, buf->mkey_in, &buf->mkey); if (ret) goto err_create_mkey; =20 return 0; =20 err_create_mkey: + unregister_dma_pages(mdev, buf->npages, buf->mkey_in, &buf->state, + buf->dma_dir); +err_register_dma: kvfree(buf->mkey_in); buf->mkey_in =3D NULL; -err: - dma_unmap_sgtable(mdev->device, &buf->table.sgt, buf->dma_dir, 0); return ret; } =20 +static void free_page_list(u32 npages, struct page **page_list) +{ + int i; + + /* Undo alloc_pages_bulk_array() */ + for (i =3D npages - 1; i >=3D 0; i--) + __free_page(page_list[i]); + + kvfree(page_list); +} + void mlx5vf_free_data_buffer(struct mlx5_vhca_data_buffer *buf) { - struct mlx5_vf_migration_file *migf =3D buf->migf; - struct sg_page_iter sg_iter; + struct mlx5vf_pci_core_device *mvdev =3D buf->migf->mvdev; + struct mlx5_core_dev *mdev =3D mvdev->mdev; =20 - lockdep_assert_held(&migf->mvdev->state_mutex); - WARN_ON(migf->mvdev->mdev_detach); + lockdep_assert_held(&mvdev->state_mutex); + WARN_ON(mvdev->mdev_detach); =20 if (buf->mkey_in) { - mlx5_core_destroy_mkey(migf->mvdev->mdev, buf->mkey); + mlx5_core_destroy_mkey(mdev, buf->mkey); + unregister_dma_pages(mdev, buf->npages, buf->mkey_in, + &buf->state, buf->dma_dir); kvfree(buf->mkey_in); - dma_unmap_sgtable(migf->mvdev->mdev->device, &buf->table.sgt, - buf->dma_dir, 0); } =20 - /* Undo alloc_pages_bulk_array() */ - for_each_sgtable_page(&buf->table.sgt, &sg_iter, 0) - __free_page(sg_page_iter_page(&sg_iter)); - sg_free_append_table(&buf->table); + free_page_list(buf->npages, buf->page_list); kfree(buf); } =20 -static int mlx5vf_add_migration_pages(struct mlx5_vhca_data_buffer *buf, - unsigned int npages) +static int mlx5vf_add_pages(struct page ***page_list, unsigned int npages) { - unsigned int to_alloc =3D npages; - struct page **page_list; - unsigned long filled; - unsigned int to_fill; - int ret; + unsigned int filled =3D 0, done =3D 0; + int i; =20 - to_fill =3D min_t(unsigned int, npages, PAGE_SIZE / sizeof(*page_list)); - page_list =3D kvzalloc(to_fill * sizeof(*page_list), GFP_KERNEL_ACCOUNT); - if (!page_list) + *page_list =3D kvcalloc(npages, sizeof(struct page *), GFP_KERNEL_ACCOUNT= ); + if (!*page_list) return -ENOMEM; =20 - do { - filled =3D alloc_pages_bulk_array(GFP_KERNEL_ACCOUNT, to_fill, - page_list); - if (!filled) { - ret =3D -ENOMEM; + for (;;) { + filled =3D alloc_pages_bulk_array(GFP_KERNEL_ACCOUNT, + npages - done, + *page_list + done); + if (!filled) goto err; - } - to_alloc -=3D filled; - ret =3D sg_alloc_append_table_from_pages( - &buf->table, page_list, filled, 0, - filled << PAGE_SHIFT, UINT_MAX, SG_MAX_SINGLE_ALLOC, - GFP_KERNEL_ACCOUNT); =20 - if (ret) - goto err; - buf->npages +=3D filled; - /* clean input for another bulk allocation */ - memset(page_list, 0, filled * sizeof(*page_list)); - to_fill =3D min_t(unsigned int, to_alloc, - PAGE_SIZE / sizeof(*page_list)); - } while (to_alloc > 0); + done +=3D filled; + if (done =3D=3D npages) + break; + } =20 - kvfree(page_list); return 0; =20 err: - kvfree(page_list); - return ret; + for (i =3D 0; i < done; i++) + __free_page(*page_list[i]); + + kvfree(*page_list); + *page_list =3D NULL; + return -ENOMEM; } =20 + struct mlx5_vhca_data_buffer * mlx5vf_alloc_data_buffer(struct mlx5_vf_migration_file *migf, u32 npages, enum dma_data_direction dma_dir) @@ -483,10 +536,12 @@ mlx5vf_alloc_data_buffer(struct mlx5_vf_migration_fil= e *migf, u32 npages, buf->dma_dir =3D dma_dir; buf->migf =3D migf; if (npages) { - ret =3D mlx5vf_add_migration_pages(buf, npages); + ret =3D mlx5vf_add_pages(&buf->page_list, npages); if (ret) goto end; =20 + buf->npages =3D npages; + if (dma_dir !=3D DMA_NONE) { ret =3D mlx5vf_dma_data_buffer(buf); if (ret) @@ -1345,101 +1400,16 @@ static void mlx5vf_destroy_qp(struct mlx5_core_dev= *mdev, kfree(qp); } =20 -static void free_recv_pages(struct mlx5_vhca_recv_buf *recv_buf) -{ - int i; - - /* Undo alloc_pages_bulk_array() */ - for (i =3D 0; i < recv_buf->npages; i++) - __free_page(recv_buf->page_list[i]); - - kvfree(recv_buf->page_list); -} - -static int alloc_recv_pages(struct mlx5_vhca_recv_buf *recv_buf, - unsigned int npages) -{ - unsigned int filled =3D 0, done =3D 0; - int i; - - recv_buf->page_list =3D kvcalloc(npages, sizeof(*recv_buf->page_list), - GFP_KERNEL_ACCOUNT); - if (!recv_buf->page_list) - return -ENOMEM; - - for (;;) { - filled =3D alloc_pages_bulk_array(GFP_KERNEL_ACCOUNT, - npages - done, - recv_buf->page_list + done); - if (!filled) - goto err; - - done +=3D filled; - if (done =3D=3D npages) - break; - } - - recv_buf->npages =3D npages; - return 0; - -err: - for (i =3D 0; i < npages; i++) { - if (recv_buf->page_list[i]) - __free_page(recv_buf->page_list[i]); - } - - kvfree(recv_buf->page_list); - return -ENOMEM; -} -static void unregister_dma_pages(struct mlx5_core_dev *mdev, u32 npages, - u32 *mkey_in) -{ - dma_addr_t addr; - __be64 *mtt; - int i; - - mtt =3D (__be64 *)MLX5_ADDR_OF(create_mkey_in, mkey_in, klm_pas_mtt); - for (i =3D npages - 1; i >=3D 0; i--) { - addr =3D be64_to_cpu(mtt[i]); - dma_unmap_single(mdev->device, addr, PAGE_SIZE, - DMA_FROM_DEVICE); - } -} - -static int register_dma_pages(struct mlx5_core_dev *mdev, u32 npages, - struct page **page_list, u32 *mkey_in) -{ - dma_addr_t addr; - __be64 *mtt; - int i; - - mtt =3D (__be64 *)MLX5_ADDR_OF(create_mkey_in, mkey_in, klm_pas_mtt); - - for (i =3D 0; i < npages; i++) { - addr =3D dma_map_page(mdev->device, page_list[i], 0, PAGE_SIZE, - DMA_FROM_DEVICE); - if (dma_mapping_error(mdev->device, addr)) - goto error; - - *mtt++ =3D cpu_to_be64(addr); - } - - return 0; - -error: - unregister_dma_pages(mdev, i, mkey_in); - return -ENOMEM; -} - static void mlx5vf_free_qp_recv_resources(struct mlx5_core_dev *mdev, struct mlx5_vhca_qp *qp) { struct mlx5_vhca_recv_buf *recv_buf =3D &qp->recv_buf; =20 mlx5_core_destroy_mkey(mdev, recv_buf->mkey); - unregister_dma_pages(mdev, recv_buf->npages, recv_buf->mkey_in); + unregister_dma_pages(mdev, recv_buf->npages, recv_buf->mkey_in, + &recv_buf->state, DMA_FROM_DEVICE); kvfree(recv_buf->mkey_in); - free_recv_pages(&qp->recv_buf); + free_page_list(recv_buf->npages, recv_buf->page_list); } =20 static int mlx5vf_alloc_qp_recv_resources(struct mlx5_core_dev *mdev, @@ -1450,10 +1420,12 @@ static int mlx5vf_alloc_qp_recv_resources(struct ml= x5_core_dev *mdev, struct mlx5_vhca_recv_buf *recv_buf =3D &qp->recv_buf; int err; =20 - err =3D alloc_recv_pages(recv_buf, npages); - if (err < 0) + err =3D mlx5vf_add_pages(&recv_buf->page_list, npages); + if (err) return err; =20 + recv_buf->npages =3D npages; + recv_buf->mkey_in =3D alloc_mkey_in(npages, pdn); if (!recv_buf->mkey_in) { err =3D -ENOMEM; @@ -1461,24 +1433,25 @@ static int mlx5vf_alloc_qp_recv_resources(struct ml= x5_core_dev *mdev, } =20 err =3D register_dma_pages(mdev, npages, recv_buf->page_list, - recv_buf->mkey_in); + recv_buf->mkey_in, &recv_buf->state, + DMA_FROM_DEVICE); if (err) goto err_register_dma; =20 - err =3D create_mkey(mdev, npages, NULL, recv_buf->mkey_in, - &recv_buf->mkey); + err =3D create_mkey(mdev, npages, recv_buf->mkey_in, &recv_buf->mkey); if (err) goto err_create_mkey; =20 return 0; =20 err_create_mkey: - unregister_dma_pages(mdev, npages, recv_buf->mkey_in); + unregister_dma_pages(mdev, npages, recv_buf->mkey_in, &recv_buf->state, + DMA_FROM_DEVICE); err_register_dma: kvfree(recv_buf->mkey_in); recv_buf->mkey_in =3D NULL; end: - free_recv_pages(recv_buf); + free_page_list(npages, recv_buf->page_list); return err; } =20 diff --git a/drivers/vfio/pci/mlx5/cmd.h b/drivers/vfio/pci/mlx5/cmd.h index 25dd6ff54591..d7821b5ca772 100644 --- a/drivers/vfio/pci/mlx5/cmd.h +++ b/drivers/vfio/pci/mlx5/cmd.h @@ -53,7 +53,8 @@ struct mlx5_vf_migration_header { }; =20 struct mlx5_vhca_data_buffer { - struct sg_append_table table; + struct page **page_list; + struct dma_iova_state state; loff_t start_pos; u64 length; u32 npages; @@ -63,10 +64,6 @@ struct mlx5_vhca_data_buffer { u8 stop_copy_chunk_num; struct list_head buf_elm; struct mlx5_vf_migration_file *migf; - /* Optimize mlx5vf_get_migration_page() for sequential access */ - struct scatterlist *last_offset_sg; - unsigned int sg_last_entry; - unsigned long last_offset; }; =20 struct mlx5vf_async_data { @@ -133,6 +130,7 @@ struct mlx5_vhca_cq { struct mlx5_vhca_recv_buf { u32 npages; struct page **page_list; + struct dma_iova_state state; u32 next_rq_offset; u32 *mkey_in; u32 mkey; @@ -224,8 +222,17 @@ struct mlx5_vhca_data_buffer * mlx5vf_get_data_buffer(struct mlx5_vf_migration_file *migf, u32 npages, enum dma_data_direction dma_dir); void mlx5vf_put_data_buffer(struct mlx5_vhca_data_buffer *buf); -struct page *mlx5vf_get_migration_page(struct mlx5_vhca_data_buffer *buf, - unsigned long offset); +static inline struct page * +mlx5vf_get_migration_page(struct mlx5_vhca_data_buffer *buf, + unsigned long offset) +{ + int page_entry =3D offset / PAGE_SIZE; + + if (page_entry >=3D buf->npages) + return NULL; + + return buf->page_list[page_entry]; +} void mlx5vf_state_mutex_unlock(struct mlx5vf_pci_core_device *mvdev); void mlx5vf_disable_fds(struct mlx5vf_pci_core_device *mvdev, enum mlx5_vf_migf_state *last_save_state); diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c index a1dbee3be1e0..d6cf97101c41 100644 --- a/drivers/vfio/pci/mlx5/main.c +++ b/drivers/vfio/pci/mlx5/main.c @@ -34,37 +34,6 @@ static struct mlx5vf_pci_core_device *mlx5vf_drvdata(str= uct pci_dev *pdev) core_device); } =20 -struct page * -mlx5vf_get_migration_page(struct mlx5_vhca_data_buffer *buf, - unsigned long offset) -{ - unsigned long cur_offset =3D 0; - struct scatterlist *sg; - unsigned int i; - - /* All accesses are sequential */ - if (offset < buf->last_offset || !buf->last_offset_sg) { - buf->last_offset =3D 0; - buf->last_offset_sg =3D buf->table.sgt.sgl; - buf->sg_last_entry =3D 0; - } - - cur_offset =3D buf->last_offset; - - for_each_sg(buf->last_offset_sg, sg, - buf->table.sgt.orig_nents - buf->sg_last_entry, i) { - if (offset < sg->length + cur_offset) { - buf->last_offset_sg =3D sg; - buf->sg_last_entry +=3D i; - buf->last_offset =3D cur_offset; - return nth_page(sg_page(sg), - (offset - cur_offset) / PAGE_SIZE); - } - cur_offset +=3D sg->length; - } - return NULL; -} - static void mlx5vf_disable_fd(struct mlx5_vf_migration_file *migf) { mutex_lock(&migf->lock); --=20 2.47.0