Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2347354ybk; Sun, 17 May 2020 18:31:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw9OoXTA7JWZIhgqHgu+MZIQBv5fOqBdqfuViEpwXzL28WFH2uGEGiei4n8+KuIPfN7MUxE X-Received: by 2002:a17:907:447f:: with SMTP id oo23mr12566390ejb.274.1589765478925; Sun, 17 May 2020 18:31:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589765478; cv=none; d=google.com; s=arc-20160816; b=PKDyRFRcgiww96o3AAEsqRFvrVaPfyTQ/ok+78WzsOjVaDlGFSZ3Iyn5fouV1o5LJK 9WwjNXZNc9oPKPCEJckTuWd72hgGRiuIj1FZAofwt4CdOg9grji9QSEl3RwwrrqzmcZ6 jVCEaC2qXEcsDZZ3z3/pgwh92zbliyJ4kMulAM0XEYN7/AiFUo1ob8PbuiTY0M7IjXP6 38+y6oprLclZAuGeh8KJwWOuj3wz2GSI/UMru5plyAScUoaoHMyTbVsRETbT/827fa8f QIfi1BIVAusMr+8JCnY5yBMOmdFmrDiIAqxWYOnr6+R+X0tOkvodGQz6QZh1y5Tv5XPU U3Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=uZfH/yW2EkwOl9gEcet4jN7ZXi9BLKHqqQrfr1sC6ac=; b=c+6aJwtVaIVbKdn/5QZdPjn3DpLNf86dlTlDUQZHmR7VmgwIBSVo1lw9BUGYXVEG0y avZryHXe8SHPjGC7Ya0FDXhrjmw7PtNdKRuRcemkfgOtEIgYtW/SWVLFfBGOeDNbrCIM VDFYXHUbC8JpFNw3gboukCvVoeTIl9UU24YgPdRLDVswrmeLlo1LSKqwrNlql9cNVth3 v+2/Yw4muPlMj5S1Rtigb8Tbk7eZB749j1Qg8dIIawNgjgbEKdh+8fGblU/2Ac2lidfs 06WIe1/Z4n80LZca3hDyJw5LTlXXyKXOG9hsINIVGxPMR0FRMKUIbSfFjaGkHwFrT8hp d6Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=vDcWgigW; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l35si5135136ede.136.2020.05.17.18.30.55; Sun, 17 May 2020 18:31:18 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=vDcWgigW; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726703AbgERB10 (ORCPT + 99 others); Sun, 17 May 2020 21:27:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726665AbgERB1Z (ORCPT ); Sun, 17 May 2020 21:27:25 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E280C061A0C for ; Sun, 17 May 2020 18:27:24 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id g14so21212660wme.1 for ; Sun, 17 May 2020 18:27:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=uZfH/yW2EkwOl9gEcet4jN7ZXi9BLKHqqQrfr1sC6ac=; b=vDcWgigWf+8ZKsquN2u4JqfQ8umHJX5fe46rUTGhJECREweESiSfWnUn0R+kFnVYzL Po+JjR2lydKpGPNuAuKUfwyrCt05L9FE2SiboNT/Di3jcIyohybAZ7DEy0Rfme5FXmov hLdWtACwrYRBlLG0QN8oUbUrydmDczxFu2izF8ScEASNprtdB5wc/pnUdJmba6hRwV9n aHCMGNU+KjzWjQTSpCRhc4bWnVvgd0rJg1YDnGNoSnyOoDtovxLu56C1biuKkr+dRjvW tJZ3MvsoObtcyH+Pvwi+VeXvwxYehRqFZ5a/TM/TAH2YVIfFH9U6+mtgTNk5l3SnaaaD oGZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=uZfH/yW2EkwOl9gEcet4jN7ZXi9BLKHqqQrfr1sC6ac=; b=IfHtw0nAmxkPO1GiDOLzCvJo2aaV//Vk7MSqQdRyoCxAwz+bwcBMTRmsPMbvIJJfGg BioOF2obPmprcvbQ+WV+K2d0WOtp29ilt7I+5gId6imI8OxcKvFd/a22Usiis5py27DJ BHiux9X9wcvXetyJGlSuX1pMYaRazi/ir2tcB9IMbH/4RS/IQgMgLf92Vd3RFCrR03l6 mBDcwyVaFAZB6GvwRqB8JylSpuPBXopipJfnlQqT531ZYsKPJrRilYAst8jLwdnfCf6f +gFiIsMcEx7r1B+xeDUnvqsDmI4SVcD2UxHB4aZO163GwZ/zGwAEZeOjocV2XkwsTMcp 8PLA== X-Gm-Message-State: AOAM5306Kq3sfdMnoGR42CCg3OqjakJbA3QAg3UjrWMne9UhOlhZqU7O 2uLdzfHsfMkDTHkQ3wcTDI+SKh89/R9P8yMvkh8o7EBBvWM= X-Received: by 2002:a05:600c:2255:: with SMTP id a21mr17035637wmm.67.1589765242888; Sun, 17 May 2020 18:27:22 -0700 (PDT) MIME-Version: 1.0 References: <20200513132114.6046-1-m.szyprowski@samsung.com> <20200513133245.6408-1-m.szyprowski@samsung.com> <20200513133245.6408-13-m.szyprowski@samsung.com> In-Reply-To: <20200513133245.6408-13-m.szyprowski@samsung.com> From: Qiang Yu Date: Mon, 18 May 2020 09:27:11 +0800 Message-ID: Subject: Re: [PATCH v5 13/38] drm: lima: fix common struct sg_table related issues To: Marek Szyprowski Cc: dri-devel , iommu@lists.linux-foundation.org, linaro-mm-sig@lists.linaro.org, Linux Kernel Mailing List , Christoph Hellwig , Robin Murphy , Bartlomiej Zolnierkiewicz , linux-arm-kernel@lists.infradead.org, David Airlie , Daniel Vetter , lima@lists.freedesktop.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Looks good for me, patch is: Reviewed-by: Qiang Yu Regards, Qiang On Wed, May 13, 2020 at 9:33 PM Marek Szyprowski wrote: > > The Documentation/DMA-API-HOWTO.txt states that the dma_map_sg() function > returns the number of the created entries in the DMA address space. > However the subsequent calls to the dma_sync_sg_for_{device,cpu}() and > dma_unmap_sg must be called with the original number of the entries > passed to the dma_map_sg(). > > struct sg_table is a common structure used for describing a non-contiguous > memory buffer, used commonly in the DRM and graphics subsystems. It > consists of a scatterlist with memory pages and DMA addresses (sgl entry), > as well as the number of scatterlist entries: CPU pages (orig_nents entry) > and DMA mapped pages (nents entry). > > It turned out that it was a common mistake to misuse nents and orig_nents > entries, calling DMA-mapping functions with a wrong number of entries or > ignoring the number of mapped entries returned by the dma_map_sg() > function. > > To avoid such issues, lets use a common dma-mapping wrappers operating > directly on the struct sg_table objects and use scatterlist page > iterators where possible. This, almost always, hides references to the > nents and orig_nents entries, making the code robust, easier to follow > and copy/paste safe. > > Signed-off-by: Marek Szyprowski > --- > For more information, see '[PATCH v5 00/38] DRM: fix struct sg_table nents > vs. orig_nents misuse' thread: > https://lore.kernel.org/linux-iommu/20200513132114.6046-1-m.szyprowski@samsung.com/T/ > --- > drivers/gpu/drm/lima/lima_gem.c | 11 ++++++++--- > drivers/gpu/drm/lima/lima_vm.c | 5 ++--- > 2 files changed, 10 insertions(+), 6 deletions(-) > > diff --git a/drivers/gpu/drm/lima/lima_gem.c b/drivers/gpu/drm/lima/lima_gem.c > index 5404e0d..cda43f6 100644 > --- a/drivers/gpu/drm/lima/lima_gem.c > +++ b/drivers/gpu/drm/lima/lima_gem.c > @@ -69,8 +69,7 @@ int lima_heap_alloc(struct lima_bo *bo, struct lima_vm *vm) > return ret; > > if (bo->base.sgt) { > - dma_unmap_sg(dev, bo->base.sgt->sgl, > - bo->base.sgt->nents, DMA_BIDIRECTIONAL); > + dma_unmap_sgtable(dev, bo->base.sgt, DMA_BIDIRECTIONAL, 0); > sg_free_table(bo->base.sgt); > } else { > bo->base.sgt = kmalloc(sizeof(*bo->base.sgt), GFP_KERNEL); > @@ -80,7 +79,13 @@ int lima_heap_alloc(struct lima_bo *bo, struct lima_vm *vm) > } > } > > - dma_map_sg(dev, sgt.sgl, sgt.nents, DMA_BIDIRECTIONAL); > + ret = dma_map_sgtable(dev, &sgt, DMA_BIDIRECTIONAL, 0); > + if (ret) { > + sg_free_table(&sgt); > + kfree(bo->base.sgt); > + bo->base.sgt = NULL; > + return ret; > + } > > *bo->base.sgt = sgt; > > diff --git a/drivers/gpu/drm/lima/lima_vm.c b/drivers/gpu/drm/lima/lima_vm.c > index 5b92fb8..2b2739a 100644 > --- a/drivers/gpu/drm/lima/lima_vm.c > +++ b/drivers/gpu/drm/lima/lima_vm.c > @@ -124,7 +124,7 @@ int lima_vm_bo_add(struct lima_vm *vm, struct lima_bo *bo, bool create) > if (err) > goto err_out1; > > - for_each_sg_dma_page(bo->base.sgt->sgl, &sg_iter, bo->base.sgt->nents, 0) { > + for_each_sgtable_dma_page(bo->base.sgt, &sg_iter, 0) { > err = lima_vm_map_page(vm, sg_page_iter_dma_address(&sg_iter), > bo_va->node.start + offset); > if (err) > @@ -298,8 +298,7 @@ int lima_vm_map_bo(struct lima_vm *vm, struct lima_bo *bo, int pageoff) > mutex_lock(&vm->lock); > > base = bo_va->node.start + (pageoff << PAGE_SHIFT); > - for_each_sg_dma_page(bo->base.sgt->sgl, &sg_iter, > - bo->base.sgt->nents, pageoff) { > + for_each_sgtable_dma_page(bo->base.sgt, &sg_iter, pageoff) { > err = lima_vm_map_page(vm, sg_page_iter_dma_address(&sg_iter), > base + offset); > if (err) > -- > 1.9.1 >