From patchwork Mon Nov 22 11:08:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shunsuke Mie X-Patchwork-Id: 12631673 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B332C433F5 for ; Mon, 22 Nov 2021 11:08:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236049AbhKVLLn (ORCPT ); Mon, 22 Nov 2021 06:11:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235587AbhKVLLk (ORCPT ); Mon, 22 Nov 2021 06:11:40 -0500 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FB5DC061757 for ; Mon, 22 Nov 2021 03:08:33 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id p18-20020a17090ad31200b001a78bb52876so16554772pju.3 for ; Mon, 22 Nov 2021 03:08:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=igel-co-jp.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=r3BsV1GTEqKXRyiYvYJGLvk2Pn1r3f4nsDFd9biBydc=; b=APqXzT+zadmpYq9XewB4Hxtxw2cFZymST7E4a8Ff9UdwuRgpWx9lTe53jJVda6Adu0 KQO96VWw9lOKubLh15GUKl2EWg3hZ4kYQwOeqXIRENbzfM8e/bE4HOEM1vUApRpUmHZX HtuTHt/R+0RwD8/bQ2zy5/TXGRtZzjNxZZKe3mVv5liLwRC3jL1cwRDaJ7e9um/te3wh hlkH1Zm+Zs4lV0jPODxLw0ubCpIV9TDCGAOEjST4TsMv6GOAlX/5fcHMy5O5rBmjNyt8 3UK+axs8zvTmpRSietmqLcjCO1SRGUHaxOpsajyPEGdygQlCX0OkPqgtm43URr7qGsIu qqFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=r3BsV1GTEqKXRyiYvYJGLvk2Pn1r3f4nsDFd9biBydc=; b=xOGVWDvr0qYroKSXHYv0MfmkFgSdvGTcOxh9nUlJVRESVyC5fuKliPvaiMv6q53f9A RFgfiRXvMP2h9Jphwx3g5vNE/bx1IdDDHENLb7nBzUzg6MqOX2ICSSeWcnv9KydLCxrA 46ac85zHWPtQU1+GLxaZytiLt1v/9I0MdGBQEk0fIxyfsD3jz+IoRX1caM1Kf0thDrYn pU/VBg0feIjjvJbSyqw+AywZgp3zLwVTlKhuMVVmGnhKMxSEiQeXYF7rAGKQL+zEz5uz lZIgRNihUe+cp+9XUeinuqdfoujExnflDMmWKqT0998BWJrs1jdYBdEP1X9+1KisUH2D 3Nrg== X-Gm-Message-State: AOAM530ca86Hg4JNV6A7hFS38/08XpAEfGpd3PshVCod40Zhc0nFdJ67 fk+XjLKOvBT8fuHSca1C0PMKlw== X-Google-Smtp-Source: ABdhPJy9uqTeKGDE2WvyoBL+W3QZz3Y0/gkcwR4vQfRwPr0FI5APvxamrkxamDcP1aJbG1p46s9cEw== X-Received: by 2002:a17:90a:5d8b:: with SMTP id t11mr29665906pji.8.1637579312795; Mon, 22 Nov 2021 03:08:32 -0800 (PST) Received: from tyrell.hq.igel.co.jp (napt.igel.co.jp. [219.106.231.132]) by smtp.gmail.com with ESMTPSA id h6sm9572816pfh.82.2021.11.22.03.08.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Nov 2021 03:08:32 -0800 (PST) From: Shunsuke Mie To: Zhu Yanjun Cc: Shunsuke Mie , =?utf-8?q?Christian_K=C3=B6nig?= , Alex Deucher , Daniel Vetter , Doug Ledford , Jason Gunthorpe , Jianxin Xiong , Leon Romanovsky , Maor Gottlieb , Sean Hefty , Sumit Semwal , dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, dhobsong@igel.co.jp, taki@igel.co.jp, etom@igel.co.jp Subject: [RFC PATCH v4 1/2] RDMA/umem: Change for rdma devices has not dma device Date: Mon, 22 Nov 2021 20:08:16 +0900 Message-Id: <20211122110817.33319-2-mie@igel.co.jp> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211122110817.33319-1-mie@igel.co.jp> References: <20211122110817.33319-1-mie@igel.co.jp> Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Current implementation requires a dma device for RDMA driver to use dma-buf memory space as RDMA buffer. However, software RDMA drivers has not dma device and copy RDMA data using CPU instead of hardware. This patch changes to be hold a dma-buf on struct ib_umem_dmabuf. This allows the software RDMA driver to map dma-buf memory for CPU memory accessing. Signed-off-by: Shunsuke Mie --- drivers/infiniband/core/umem_dmabuf.c | 20 ++++++++++++++++---- include/rdma/ib_umem.h | 1 + 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/drivers/infiniband/core/umem_dmabuf.c b/drivers/infiniband/core/umem_dmabuf.c index e824baf4640d..ebbb0a259fd4 100644 --- a/drivers/infiniband/core/umem_dmabuf.c +++ b/drivers/infiniband/core/umem_dmabuf.c @@ -117,9 +117,6 @@ struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device, if (check_add_overflow(offset, (unsigned long)size, &end)) return ret; - if (unlikely(!ops || !ops->move_notify)) - return ret; - dmabuf = dma_buf_get(fd); if (IS_ERR(dmabuf)) return ERR_CAST(dmabuf); @@ -133,6 +130,8 @@ struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device, goto out_release_dmabuf; } + umem_dmabuf->dmabuf = dmabuf; + umem = &umem_dmabuf->umem; umem->ibdev = device; umem->length = size; @@ -143,6 +142,13 @@ struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device, if (!ib_umem_num_pages(umem)) goto out_free_umem; + /* Software RDMA drivers has not dma device. Just get dmabuf from fd */ + if (!device->dma_device) + goto done; + + if (unlikely(!ops || !ops->move_notify)) + goto out_free_umem; + umem_dmabuf->attach = dma_buf_dynamic_attach( dmabuf, device->dma_device, @@ -152,6 +158,7 @@ struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device, ret = ERR_CAST(umem_dmabuf->attach); goto out_free_umem; } +done: return umem_dmabuf; out_free_umem: @@ -165,13 +172,18 @@ EXPORT_SYMBOL(ib_umem_dmabuf_get); void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf) { - struct dma_buf *dmabuf = umem_dmabuf->attach->dmabuf; + struct dma_buf *dmabuf = umem_dmabuf->dmabuf; + + if (!umem_dmabuf->attach) + goto free_dmabuf; dma_resv_lock(dmabuf->resv, NULL); ib_umem_dmabuf_unmap_pages(umem_dmabuf); dma_resv_unlock(dmabuf->resv); dma_buf_detach(dmabuf, umem_dmabuf->attach); + +free_dmabuf: dma_buf_put(dmabuf); kfree(umem_dmabuf); } diff --git a/include/rdma/ib_umem.h b/include/rdma/ib_umem.h index 5ae9dff74dac..11c0cf7e0dd8 100644 --- a/include/rdma/ib_umem.h +++ b/include/rdma/ib_umem.h @@ -32,6 +32,7 @@ struct ib_umem { struct ib_umem_dmabuf { struct ib_umem umem; struct dma_buf_attachment *attach; + struct dma_buf *dmabuf; struct sg_table *sgt; struct scatterlist *first_sg; struct scatterlist *last_sg; From patchwork Mon Nov 22 11:08:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Shunsuke Mie X-Patchwork-Id: 12631675 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75080C433EF for ; Mon, 22 Nov 2021 11:09:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232726AbhKVLMP (ORCPT ); Mon, 22 Nov 2021 06:12:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236194AbhKVLLo (ORCPT ); Mon, 22 Nov 2021 06:11:44 -0500 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 595E4C061574 for ; Mon, 22 Nov 2021 03:08:38 -0800 (PST) Received: by mail-pl1-x62c.google.com with SMTP id p18so13724572plf.13 for ; Mon, 22 Nov 2021 03:08:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=igel-co-jp.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zWlby2sFftHMLmve376Nr7mmMkSU248+Ur4FH61bHpc=; b=0rKS43idbB+hATRIgfuw+HwVROS1wVMp1IWzOKdaA0HUjwDtvVMb970KswaOpBWOE0 yx3jSQTvMmyMUkW3djgyG+1GcHjZ2fvqA9C08xBdBfEEcaMgEgNcOwlN9IkhWipMktBz sGFFxBZX/15TKe+RzqptXBEZeqXLDnXNvDLm8jWMpqrsCU8zYGWmvaZVfDi/zdmrTiYK 4UOChwQt+pA5N67fe+VdAiA8pzI3tQRaNorzE8EBOqiCRoCPFHertZpLsK5XfQCPDBUm sJgQOxu0Mdc2J9fwoo01/uXGy0TQrJhmVUvloxajLk9LN1baJhhZT7QDtD1LCOYUiZbc 95rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zWlby2sFftHMLmve376Nr7mmMkSU248+Ur4FH61bHpc=; b=cTu2DDpkeGV46iVEKNPSdpZxSkmsUnX0nwso5sHSdq3N7Lb9uNZbhX9su7hw+WYSkS QohneMIZ5/KcIH1OuPjL4ViYXMmChRaoMdsn0FcwAt4NIsHNvM5L6c2p9Nfk41AVkZxi 24NC/aQynCriI77MXp5TKcq+Kerkbj0qOxGGp3hKlXKcdnLP7Rmdht+4kLng8rpUYlo1 Q3Vl1wnrTwW+fm1lEpu7huN9ulZKnDj+zYam4PbDDxQpyY816ffjFCXJ9VA/dTZe6qim y1zyu9rkE20ZPllHY67XK703lrWGZqOxsTVlds6FKYsXs0jZk6ii+sbxf8rq/X09TbGN FRlg== X-Gm-Message-State: AOAM531J4Uq/l2pB5Cg/0CWl76cASccYDbxPbmjFwNklzUAgwfRzUou2 ocNXBk+rKuaMvRymWkd5sRIhlg== X-Google-Smtp-Source: ABdhPJwGV7PdqdYCWBksNRXXGS0hy/fqd8dJUJXINnVkFj0H9VrDJgpVsnczgdxodacHp9K/lZzWAA== X-Received: by 2002:a17:903:2045:b0:142:3d07:2866 with SMTP id q5-20020a170903204500b001423d072866mr106231404pla.17.1637579317824; Mon, 22 Nov 2021 03:08:37 -0800 (PST) Received: from tyrell.hq.igel.co.jp (napt.igel.co.jp. [219.106.231.132]) by smtp.gmail.com with ESMTPSA id h6sm9572816pfh.82.2021.11.22.03.08.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Nov 2021 03:08:37 -0800 (PST) From: Shunsuke Mie To: Zhu Yanjun Cc: Shunsuke Mie , =?utf-8?q?Christian_K=C3=B6nig?= , Alex Deucher , Daniel Vetter , Doug Ledford , Jason Gunthorpe , Jianxin Xiong , Leon Romanovsky , Maor Gottlieb , Sean Hefty , Sumit Semwal , dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, dhobsong@igel.co.jp, taki@igel.co.jp, etom@igel.co.jp Subject: [RFC PATCH v4 2/2] RDMA/rxe: Add dma-buf support Date: Mon, 22 Nov 2021 20:08:17 +0900 Message-Id: <20211122110817.33319-3-mie@igel.co.jp> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211122110817.33319-1-mie@igel.co.jp> References: <20211122110817.33319-1-mie@igel.co.jp> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Implement a ib device operation ‘reg_user_mr_dmabuf’. Generate a rxe_map from the memory space linked the passed dma-buf. Signed-off-by: Shunsuke Mie --- drivers/infiniband/sw/rxe/rxe_loc.h | 2 + drivers/infiniband/sw/rxe/rxe_mr.c | 113 ++++++++++++++++++++++++++ drivers/infiniband/sw/rxe/rxe_verbs.c | 34 ++++++++ 3 files changed, 149 insertions(+) diff --git a/drivers/infiniband/sw/rxe/rxe_loc.h b/drivers/infiniband/sw/rxe/rxe_loc.h index 1ca43b859d80..8bc19ea1a376 100644 --- a/drivers/infiniband/sw/rxe/rxe_loc.h +++ b/drivers/infiniband/sw/rxe/rxe_loc.h @@ -75,6 +75,8 @@ u8 rxe_get_next_key(u32 last_key); void rxe_mr_init_dma(struct rxe_pd *pd, int access, struct rxe_mr *mr); int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova, int access, struct rxe_mr *mr); +int rxe_mr_dmabuf_init_user(struct rxe_pd *pd, int fd, u64 start, u64 length, + u64 iova, int access, struct rxe_mr *mr); int rxe_mr_init_fast(struct rxe_pd *pd, int max_pages, struct rxe_mr *mr); int rxe_mr_copy(struct rxe_mr *mr, u64 iova, void *addr, int length, enum rxe_mr_copy_dir dir); diff --git a/drivers/infiniband/sw/rxe/rxe_mr.c b/drivers/infiniband/sw/rxe/rxe_mr.c index 53271df10e47..b954e5647f82 100644 --- a/drivers/infiniband/sw/rxe/rxe_mr.c +++ b/drivers/infiniband/sw/rxe/rxe_mr.c @@ -4,6 +4,8 @@ * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved. */ +#include +#include #include "rxe.h" #include "rxe_loc.h" @@ -245,6 +247,114 @@ int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova, return err; } +static int rxe_map_dmabuf_mr(struct rxe_mr *mr, + struct ib_umem_dmabuf *umem_dmabuf) +{ + struct rxe_map_set *set; + struct rxe_phys_buf *buf = NULL; + struct rxe_map **map; + void *vaddr; + int num_buf = 0; + int err; + size_t remain; + struct dma_buf_map dmabuf_map; + + err = dma_buf_vmap(umem_dmabuf->dmabuf, &dmabuf_map); + if (err || dmabuf_map.is_iomem) + goto err_out; + + set = mr->cur_map_set; + set->page_shift = PAGE_SHIFT; + set->page_mask = PAGE_SIZE - 1; + + map = set->map; + buf = map[0]->buf; + + vaddr = dmabuf_map.vaddr; + remain = umem_dmabuf->dmabuf->size; + + for (; remain; vaddr += PAGE_SIZE) { + if (num_buf >= RXE_BUF_PER_MAP) { + map++; + buf = map[0]->buf; + num_buf = 0; + } + + buf->addr = (uintptr_t)vaddr; + if (remain >= PAGE_SIZE) + buf->size = PAGE_SIZE; + else + buf->size = remain; + remain -= buf->size; + + num_buf++; + buf++; + } + + return 0; + +err_out: + return err; +} + +static void rxe_unmap_dmabuf_mr(struct rxe_mr *mr) +{ + struct ib_umem_dmabuf *umem_dmabuf = to_ib_umem_dmabuf(mr->umem); + struct rxe_map *map = mr->cur_map_set->map[0]; + struct dma_buf_map dma_buf_map = + DMA_BUF_MAP_INIT_VADDR((void *)(uintptr_t)map->buf->addr); + + dma_buf_vunmap(umem_dmabuf->dmabuf, &dma_buf_map); +} + +int rxe_mr_dmabuf_init_user(struct rxe_pd *pd, int fd, u64 start, u64 length, + u64 iova, int access, struct rxe_mr *mr) +{ + struct ib_umem_dmabuf *umem_dmabuf; + struct rxe_map_set *set; + int err; + + umem_dmabuf = ib_umem_dmabuf_get(pd->ibpd.device, start, length, fd, + access, NULL); + if (IS_ERR(umem_dmabuf)) { + err = PTR_ERR(umem_dmabuf); + goto err_out; + } + + rxe_mr_init(access, mr); + + err = rxe_mr_alloc(mr, ib_umem_num_pages(&umem_dmabuf->umem), 0); + if (err) { + pr_warn("%s: Unable to allocate memory for map\n", __func__); + goto err_release_umem; + } + + mr->ibmr.pd = &pd->ibpd; + mr->umem = &umem_dmabuf->umem; + mr->access = access; + mr->state = RXE_MR_STATE_VALID; + mr->type = IB_MR_TYPE_USER; + + set = mr->cur_map_set; + set->length = length; + set->iova = iova; + set->va = start; + set->offset = ib_umem_offset(mr->umem); + + err = rxe_map_dmabuf_mr(mr, umem_dmabuf); + if (err) + goto err_free_map_set; + + return 0; + +err_free_map_set: + rxe_mr_free_map_set(mr->num_map, mr->cur_map_set); +err_release_umem: + ib_umem_release(&umem_dmabuf->umem); +err_out: + return err; +} + int rxe_mr_init_fast(struct rxe_pd *pd, int max_pages, struct rxe_mr *mr) { int err; @@ -703,6 +813,9 @@ void rxe_mr_cleanup(struct rxe_pool_entry *arg) { struct rxe_mr *mr = container_of(arg, typeof(*mr), pelem); + if (mr->umem && mr->umem->is_dmabuf) + rxe_unmap_dmabuf_mr(mr); + ib_umem_release(mr->umem); if (mr->cur_map_set) diff --git a/drivers/infiniband/sw/rxe/rxe_verbs.c b/drivers/infiniband/sw/rxe/rxe_verbs.c index 0aa0d7e52773..dc7d27b3cb90 100644 --- a/drivers/infiniband/sw/rxe/rxe_verbs.c +++ b/drivers/infiniband/sw/rxe/rxe_verbs.c @@ -940,6 +940,39 @@ static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, return ERR_PTR(err); } +static struct ib_mr *rxe_reg_user_mr_dmabuf(struct ib_pd *ibpd, u64 start, + u64 length, u64 iova, int fd, + int access, struct ib_udata *udata) +{ + int err; + struct rxe_dev *rxe = to_rdev(ibpd->device); + struct rxe_pd *pd = to_rpd(ibpd); + struct rxe_mr *mr; + + mr = rxe_alloc(&rxe->mr_pool); + if (!mr) { + err = -ENOMEM; + goto err2; + } + + rxe_add_index(mr); + + rxe_add_ref(pd); + + err = rxe_mr_dmabuf_init_user(pd, fd, start, length, iova, access, mr); + if (err) + goto err3; + + return &mr->ibmr; + +err3: + rxe_drop_ref(pd); + rxe_drop_index(mr); + rxe_drop_ref(mr); +err2: + return ERR_PTR(err); +} + static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type, u32 max_num_sg) { @@ -1105,6 +1138,7 @@ static const struct ib_device_ops rxe_dev_ops = { .query_qp = rxe_query_qp, .query_srq = rxe_query_srq, .reg_user_mr = rxe_reg_user_mr, + .reg_user_mr_dmabuf = rxe_reg_user_mr_dmabuf, .req_notify_cq = rxe_req_notify_cq, .resize_cq = rxe_resize_cq,