Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2288097ybk; Mon, 11 May 2020 17:22:55 -0700 (PDT) X-Google-Smtp-Source: APiQypI9KKU/jZ1nRLVzshMugYy30FrdOKQ+G5hT+YqH0gt69WxsC/Lzrs/QzCm5h/5JMtH29YV2 X-Received: by 2002:a05:6402:1651:: with SMTP id s17mr16344495edx.173.1589242975627; Mon, 11 May 2020 17:22:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589242975; cv=none; d=google.com; s=arc-20160816; b=NOrUJjU/YLxvWJFVu1Gx1kpw1RFnRzQd9Mt1xMrMqTLvIIMhtmWWM4maG8KbTnu6E5 mH2XkoB3W2kGdubqyP4imTKOvETODdRAYq7tOVvOSkjdNvgeobFcwb3WiDB5/fF3sj9n cJtiI3R7Ks4SZTSv4L41UOPsuHNLJKhYEIzL3mtTYVmYH2u6lZ+pKY1nF4iUxgUez4T8 +jZ+skBJBFnI0+unIPnn87dHFcxcTx3J+lL3+S3H6XJIVXHL3SS/jvcXB6HpJz46Sul/ 3XKRhpnabPaQOO3H9nFhTFdlsUrPGtcPGgpdNYIwMy7bN8njxq9Z6ov3u3lCF7HbbQpW pofg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=yYvlC25wr+CzuB3Crda1mNyLVU8oUCfO/x46K5yCuro=; b=rGvhjsUgjDuNplmFmZjc/Esw06YklMDjX4ghcTI9IBwALLRHcRc8FYQmFVeYMIrx6T AzIAwj0EVzY1Yw1Y+ZHGxlqBXjvN9jPjwir584kse68sE/7Ajc6LRTPdPwj6/HyKR4jy l7DbKlIUqEMCrjJk9CkKfIqRLHX8Wce37s2w4YPn8dX9+mvUxeXOz0xAnQuwubfkyg/p 7UPL9UVzDZeBwQY6KpvAH5+8IgX1qa/zEFi7PJdi9GSdq+uDnHNZAHGxL7BKWFfAWMn4 nv4NvEF4PHQrxZGZyk19rnQ6OvCCDx2yUpV2x0sSEmO2Y2if9ViwcoN/F1c9z7/R5Sjb zkww== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g17si6696874ejm.271.2020.05.11.17.22.32; Mon, 11 May 2020 17:22:55 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728128AbgELAUb (ORCPT + 99 others); Mon, 11 May 2020 20:20:31 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:24252 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725855AbgELAUb (ORCPT ); Mon, 11 May 2020 20:20:31 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Mon, 11 May 2020 17:19:27 -0700 Received: from [0.0.0.0] (oddjob.vmware.com [10.253.4.32]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 53A74404D5; Mon, 11 May 2020 17:19:26 -0700 (PDT) Subject: Re: [Linux-graphics-maintainer] [PATCH v3 15/25] drm: vmwgfx: fix common struct sg_table related issues To: Marek Szyprowski , , , , CC: Bartlomiej Zolnierkiewicz , Airlie , VMware Graphics , Daniel Vetter , Robin Murphy , Christoph Hellwig , References: <20200505083926.28503-1-m.szyprowski@samsung.com> <20200505084614.30424-1-m.szyprowski@samsung.com> <20200505084614.30424-15-m.szyprowski@samsung.com> From: Roland Scheidegger Message-ID: <8adef36a-1f35-e8df-3b7b-2f994a204be1@vmware.com> Date: Tue, 12 May 2020 02:19:25 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0 Thunderbird/68.2.1 MIME-Version: 1.0 In-Reply-To: <20200505084614.30424-15-m.szyprowski@samsung.com> Content-Type: text/plain; charset="utf-8" Content-Language: de-DE Content-Transfer-Encoding: 7bit Received-SPF: None (EX13-EDG-OU-001.vmware.com: sroland@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I'm not exactly an expert on this, but looks alright to me. Acked-by: Roland Scheidegger Am 05.05.20 um 10:46 schrieb Marek Szyprowski: > The Documentation/DMA-API-HOWTO.txt states that dma_map_sg returns the > numer of the created entries in the DMA address space. However the > subsequent calls to dma_sync_sg_for_{device,cpu} and dma_unmap_sg must be > called with the original number of the entries passed to dma_map_sg. The > sg_table->nents in turn holds the result of the dma_map_sg call as stated > in include/linux/scatterlist.h. A common mistake was to ignore a result > of the dma_map_sg function and don't use the sg_table->orig_nents at all. > > To avoid such issues, lets use common dma-mapping wrappers operating > directly on the struct sg_table objects and adjust references to the > nents and orig_nents respectively. > > Signed-off-by: Marek Szyprowski > --- > For more information, see '[PATCH v3 00/25] DRM: fix struct sg_table nents > vs. orig_nents misuse' thread: https://lkml.org/lkml/2020/5/5/187 > --- > drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c | 17 ++++------------- > 1 file changed, 4 insertions(+), 13 deletions(-) > > diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c > index bf0bc46..e50ae8b 100644 > --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c > +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c > @@ -362,8 +362,7 @@ static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt) > { > struct device *dev = vmw_tt->dev_priv->dev->dev; > > - dma_unmap_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.nents, > - DMA_BIDIRECTIONAL); > + dma_unmap_sgtable(dev, vmw_tt->sgt, DMA_BIDIRECTIONAL); > vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents; > } > > @@ -383,16 +382,8 @@ static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt) > static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt) > { > struct device *dev = vmw_tt->dev_priv->dev->dev; > - int ret; > - > - ret = dma_map_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.orig_nents, > - DMA_BIDIRECTIONAL); > - if (unlikely(ret == 0)) > - return -ENOMEM; > > - vmw_tt->sgt.nents = ret; > - > - return 0; > + return dma_map_sgtable(dev, vmw_tt->sgt, DMA_BIDIRECTIONAL); > } > > /** > @@ -449,10 +440,10 @@ static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt) > if (unlikely(ret != 0)) > goto out_sg_alloc_fail; > > - if (vsgt->num_pages > vmw_tt->sgt.nents) { > + if (vsgt->num_pages > vmw_tt->sgt.orig_nents) { > uint64_t over_alloc = > sgl_size * (vsgt->num_pages - > - vmw_tt->sgt.nents); > + vmw_tt->sgt.orig_nents); > > ttm_mem_global_free(glob, over_alloc); > vmw_tt->sg_alloc_size -= over_alloc; >