Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp680151pxu; Wed, 6 Jan 2021 01:08:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJzOYHV6EQ0Z7Ns4zNh3dvWqVFXZ4WO55WZL9dS+PIjKexYB0VbZTHsrGDhNSGJ9+wRgbvnF X-Received: by 2002:a50:fc8b:: with SMTP id f11mr3365805edq.11.1609924135250; Wed, 06 Jan 2021 01:08:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609924135; cv=none; d=google.com; s=arc-20160816; b=aux5oh38YheKA9ohBfkATYRPvFduTRFhRV1eQJ08ZlOGAwprLirjebR3eQSaa59Qrv hfG9QZmmKkiEtri4Qdep59AT6uXVFzWCtcYbZpMbTSXWCtbi4iQsFyhCNfSkv4c3SpbQ OOYtcvVIWxliFVPcxgbO1yqGW9GO83ty3E6y6ZdZc1TZiDCoZzgf8hn/tiS2Cd3NwIz+ jWZt6r+Zej5jPIeS1/M//M0smIdXHLCjhy4e150Y3Q3tKdOT/R0BkVsJ92Nrqcp3+7NJ Hw9gKMwKqvx+GPSr2xhrmySmtaub7NRD1gbooFl0pvUiBhD7qp+4J/1fML0XLkd8aIwE q72A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:dkim-signature:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date; bh=/6zQ5JVLCnlALckA+soCkshFtscBTwWdM/kojj3UBbc=; b=BNJNz0nvTtNOVo0N/Ja9QGpke53zzRQvdnJLG3eetj04sJyOmApuDxzenwS+dBE8DW YpyovqkUTL9/2Hii3XPFTknRQ8+Ds8rt8y/QRARIKb9tBKbe5Xa2cWt6Sdl1Akl6BcYm m9hTcE8ffdb2fnnXzRvvIcMOhRKVsYVph2JPq4d2xYq2HBBYZhMWK7a5s92gGjHdYJWV BLlYQGbKK2CHAiKFtOn/gw11LA4jKvI5GClwwwnosvOjmIDEO9v1h5pjGvgXXXfq/M9L kYsWeXNYC1xOhUabU+6d/v+GLphfSiDCZMhFhW8hFzaRvack5MFYBK6qslvK6lhYLmrU Q2xQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=ZOHgEzkN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m10si732166eda.139.2021.01.06.01.08.31; Wed, 06 Jan 2021 01:08:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=ZOHgEzkN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726830AbhAFJGq (ORCPT + 99 others); Wed, 6 Jan 2021 04:06:46 -0500 Received: from hqnvemgate26.nvidia.com ([216.228.121.65]:12249 "EHLO hqnvemgate26.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725877AbhAFJGo (ORCPT ); Wed, 6 Jan 2021 04:06:44 -0500 Received: from hqmail.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate26.nvidia.com (using TLS: TLSv1.2, AES256-SHA) id ; Wed, 06 Jan 2021 01:06:03 -0800 Received: from mtl-vdi-166.wap.labs.mlnx (172.20.145.6) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Wed, 6 Jan 2021 09:06:01 +0000 Date: Wed, 6 Jan 2021 11:05:57 +0200 From: Eli Cohen To: , , , , CC: , Subject: [PATCH] vdpa/mlx5: Fix memory key MTT population Message-ID: <20210106090557.GA170338@mtl-vdi-166.wap.labs.mlnx> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline User-Agent: Mutt/1.9.5 (bf161cf53efb) (2018-04-13) X-Originating-IP: [172.20.145.6] X-ClientProxiedBy: HQMAIL105.nvidia.com (172.20.187.12) To HQMAIL107.nvidia.com (172.20.187.13) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1609923964; bh=/6zQ5JVLCnlALckA+soCkshFtscBTwWdM/kojj3UBbc=; h=Date:From:To:CC:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:User-Agent:X-Originating-IP:X-ClientProxiedBy; b=ZOHgEzkNwAxbNyOW0BuLSEj2lUITltuumCcArPpUc+abiYkuK5c31dUP9QRu3TT+d FkjdRAqv4d9+DHC9xOUTjulTj2HL+m3c2y45txzQk1EzJ3d4YK0GlL+csKwCUsTqIb Blv8Amtgfc1NWozC8lorZxcjxrds5a3mR5kb3AKQjuKLGu9597DL/4yIu+bEgaEKZ3 r2BfUfP1BhCwxjOZ5YDdpux+9S9m2Cq5qI+8gHon2Enb+7IV4BfslGAq9YmWI1NXwc oHzvqcF31nu/LbIpVPcavIWgQ5fMmwOyAc++fE2yeQKT9+0WPIyq3L45yLtLpikj/X yu9cpyK9ySWtA== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org map_direct_mr() assumed that the number of scatter/gather entries returned by dma_map_sg_attrs() was equal to the number of segments in the sgl list. This led to wrong population of the mkey object. Fix this by properly referring to the returned value. In addition, get rid of fill_sg() whjich effect is overwritten bu populate_mtts(). Fixes: 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code") Signed-off-by: Eli Cohen --- drivers/vdpa/mlx5/core/mlx5_vdpa.h | 1 + drivers/vdpa/mlx5/core/mr.c | 28 ++++++++++++---------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/drivers/vdpa/mlx5/core/mlx5_vdpa.h b/drivers/vdpa/mlx5/core/mlx5_vdpa.h index 5c92a576edae..08f742fd2409 100644 --- a/drivers/vdpa/mlx5/core/mlx5_vdpa.h +++ b/drivers/vdpa/mlx5/core/mlx5_vdpa.h @@ -15,6 +15,7 @@ struct mlx5_vdpa_direct_mr { struct sg_table sg_head; int log_size; int nsg; + int nent; struct list_head list; u64 offset; }; diff --git a/drivers/vdpa/mlx5/core/mr.c b/drivers/vdpa/mlx5/core/mr.c index 4b6195666c58..d300f799efcd 100644 --- a/drivers/vdpa/mlx5/core/mr.c +++ b/drivers/vdpa/mlx5/core/mr.c @@ -25,17 +25,6 @@ static int get_octo_len(u64 len, int page_shift) return (npages + 1) / 2; } -static void fill_sg(struct mlx5_vdpa_direct_mr *mr, void *in) -{ - struct scatterlist *sg; - __be64 *pas; - int i; - - pas = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt); - for_each_sg(mr->sg_head.sgl, sg, mr->nsg, i) - (*pas) = cpu_to_be64(sg_dma_address(sg)); -} - static void mlx5_set_access_mode(void *mkc, int mode) { MLX5_SET(mkc, mkc, access_mode_1_0, mode & 0x3); @@ -45,10 +34,18 @@ static void mlx5_set_access_mode(void *mkc, int mode) static void populate_mtts(struct mlx5_vdpa_direct_mr *mr, __be64 *mtt) { struct scatterlist *sg; + int nsg = mr->nsg; + u64 dma_addr; + u64 dma_len; + int j = 0; int i; - for_each_sg(mr->sg_head.sgl, sg, mr->nsg, i) - mtt[i] = cpu_to_be64(sg_dma_address(sg)); + for_each_sg(mr->sg_head.sgl, sg, mr->nent, i) { + for (dma_addr = sg_dma_address(sg), dma_len = sg_dma_len(sg); + nsg && dma_len; + nsg--, dma_addr += BIT(mr->log_size), dma_len -= BIT(mr->log_size)) + mtt[j++] = cpu_to_be64(dma_addr); + } } static int create_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct_mr *mr) @@ -64,7 +61,6 @@ static int create_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct return -ENOMEM; MLX5_SET(create_mkey_in, in, uid, mvdev->res.uid); - fill_sg(mr, in); mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry); MLX5_SET(mkc, mkc, lw, !!(mr->perm & VHOST_MAP_WO)); MLX5_SET(mkc, mkc, lr, !!(mr->perm & VHOST_MAP_RO)); @@ -276,8 +272,8 @@ static int map_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct_mr done: mr->log_size = log_entity_size; mr->nsg = nsg; - err = dma_map_sg_attrs(dma, mr->sg_head.sgl, mr->nsg, DMA_BIDIRECTIONAL, 0); - if (!err) + mr->nent = dma_map_sg_attrs(dma, mr->sg_head.sgl, mr->nsg, DMA_BIDIRECTIONAL, 0); + if (!mr->nent) goto err_map; err = create_direct_mr(mvdev, mr); -- 2.28.0