Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp143451pxb; Fri, 8 Jan 2021 00:41:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJxdPH4/GpzMtfv3ShYtUYV5g1uy2B3+Van3qR77mCPud8HTePbDY2Bskwy5iZc+KRR5Phog X-Received: by 2002:a17:907:2052:: with SMTP id pg18mr2017943ejb.153.1610095300192; Fri, 08 Jan 2021 00:41:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610095300; cv=none; d=google.com; s=arc-20160816; b=0JHhrIqg2SEgYugN8ET92n1NiVt+Yb8eKovJIEfUwIVCKQGF+TppJNtpsfomAB9Rsd DG+m0L3QgHUholOke3cpXf6LPxv5FoQrlA49U5uPnX/uk97BZE0xcZMKtqykf/n2/GbZ 2LFw0k97dD8T1ij6AF0ZsMU+qKIRv62Bq+yyLX4UZWiYB2CckFxClLLO5p/bjWAkCLGX aCnWa+T/+BskB32H+haoY7v3nwFRRNrQSe4CLgmy86riLbo0qhJh+977dqwKQlA7BBJR fV7WaS5QUrOzS/qjeyn3QnNbbdkXK2YcsJDXJ9stAXZxGNt7rC4AZ3GbdfepqQE/Uyms 6oWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=naJFZbBBW8Z/PipZop5jp7RZQislmTLGZO8Bd0Lmm8M=; b=upzJIrHuFQGP7W3Is0IIaLB8UTNw8f1eNHeCjIvyYM64z69O5NpCS/KY1E1//qhb63 v7wDxOOjplJqkhFia+Xph3mo9jagB1pZ1iR7KGks0yfECvs4UVRQcOfUJBNoC5e2Qn97 K/u1TAGl5sGQSuvUwgcLv9dpdVKWIY7E9VK/cC44OQKwoDMpc8SgTl67CmQ1ooKCYRML 6cVKI+OY369m8uVfAKqeDROeK6VSvce2qSdZ68qBEFd8zKFThj2YZAfpm3yD5Q6p3oxm 6lK1x2+NEkCxM43ceB4u4b8gfbvxd5QzlRApoDFicbn3296m3DSvIDhAE/uULnG+JcnV Wihw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UrBg4gpa; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w1si3530899edl.297.2021.01.08.00.41.16; Fri, 08 Jan 2021 00:41:40 -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=@redhat.com header.s=mimecast20190719 header.b=UrBg4gpa; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727146AbhAHIkf (ORCPT + 99 others); Fri, 8 Jan 2021 03:40:35 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:59155 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725816AbhAHIkf (ORCPT ); Fri, 8 Jan 2021 03:40:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610095148; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=naJFZbBBW8Z/PipZop5jp7RZQislmTLGZO8Bd0Lmm8M=; b=UrBg4gpa2kYnrrjcBDzwnn9dPE9MiMuDPuFTUe9TlgranjIePrnCVuv3ZhL5uAsgPqON3I /20JNmIMiCUZlVVmz+3R4/Cj5d3+lAiPSn4LwyHc6ICBbc7q724+ZI1TNkzQt8UAWXx6KA uMc264EMrt5j6N5H+A0e7vj3XeSO2XU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-556-sWW27GvwNcesngDnpIJc_A-1; Fri, 08 Jan 2021 03:39:06 -0500 X-MC-Unique: sWW27GvwNcesngDnpIJc_A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 55F8D1005504; Fri, 8 Jan 2021 08:39:05 +0000 (UTC) Received: from [10.72.12.229] (ovpn-12-229.pek2.redhat.com [10.72.12.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 975E75B4BD; Fri, 8 Jan 2021 08:38:59 +0000 (UTC) Subject: Re: [PATCH v1] vdpa/mlx5: Fix memory key MTT population To: Eli Cohen , mst@redhat.com, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: lulu@redhat.com References: <20210107071845.GA224876@mtl-vdi-166.wap.labs.mlnx> From: Jason Wang Message-ID: <07d336a3-7fc2-5e4a-667a-495b5bb755da@redhat.com> Date: Fri, 8 Jan 2021 16:38:55 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20210107071845.GA224876@mtl-vdi-166.wap.labs.mlnx> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/1/7 下午3:18, Eli Cohen wrote: > 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. > > The hardware expects each MTT entry to contain the DMA address of a > contiguous block of memory of size (1 << mr->log_size) bytes. > dma_map_sg_attrs() can coalesce several sg entries into a single > scatter/gather entry of contiguous DMA range so we need to scan the list > and refer to the size of each s/g entry. > > In addition, get rid of fill_sg() which effect is overwritten by > populate_mtts(). > > Fixes: 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code") > Signed-off-by: Eli Cohen > --- > V0->V1: > 1. Fix typos > 2. Improve changelog Acked-by: Jason Wang > > 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);