From patchwork Wed Aug 26 06:32:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 248359 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:522:0:0:0:0 with SMTP id h2csp4063602ils; Tue, 25 Aug 2020 23:36:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMdBg3KHlJB224y75iTklUOlmkyLY3USzMP1wTDvL8Sxrfx8pTqYqteKUtC0zQebWa7t+Q X-Received: by 2002:a63:734d:: with SMTP id d13mr1691147pgn.169.1598423778717; Tue, 25 Aug 2020 23:36:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598423778; cv=none; d=google.com; s=arc-20160816; b=vqhxKWK18sviDeK5520HX9cyvg1rr7iEWYi7h114wEu2OFPiPfx22khZDCPyVr0sZu FxEWfvyi45xWRQ0BFVMt9MF1cNcR0sR8IEfG0x0WZKjslf04ILSdc50fXxXA4rcCbLwb za2q6d5S2Y7zbLYaad5Z1sy3A7jbNHgmRzqj4uQYeH+sBuPTbsdYikHJgtW8FGwQwbzS UJ6rgUBoTnC5fIiJHiq1hA90VyVTUT4tVUy8Msy4qJbvWuxz25x2yFVW4nNvWKZQU/Y+ YsCBfX3CLxjqRJxZOqCCSeTUdP2DYFiBX5SF5TgbxHjJZtCfjUSgkVJRlaA5q/2Sm979 qMbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:references:cms-type:in-reply-to:message-id:date :subject:to:from:dkim-signature:dkim-filter:delivered-to; bh=BmAGZWYoz9IF5LI6cUFYSRXQStyU+FoKnCpyzpRMdE8=; b=xDX74HkUHsLWbc2kAV4o//LM2jxGo1/A0Woq6c2lSkNv3M473iMYfkEtWyFRbicgLc rxyCg1hdYmSPj0UXKD/hve3vDA4X5EnnCOYSMD9VRYbLKrd6BH1WVUXjw+uEi8+kTr0r 2xrLxppFBNZMh5uUNYMM4LGjkqS4xIjrJGiCUAnLBkm/6jZUVSXNdV9x7Zs+Y8YVtsBR vwN7HV+PEowiTY2L8X/qqcOak9niUgosf2Cbkn1V8rC1V+zjEKP2PELCC7Huflpu9ACg cyb4xttEWwLjvglP3teI5Qx7t2k74KJSBf5BGN3MjJCSh8X0tEzgzpNe2MA4Xg/Xl2nL Q4Rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@samsung.com header.s=mail20170921 header.b=S1WiNNxO; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 131.252.210.177 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from gabe.freedesktop.org (gabe.freedesktop.org. [131.252.210.177]) by mx.google.com with ESMTPS id a193si1126895pfd.249.2020.08.25.23.36.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Aug 2020 23:36:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 131.252.210.177 as permitted sender) client-ip=131.252.210.177; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@samsung.com header.s=mail20170921 header.b=S1WiNNxO; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 131.252.210.177 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 324C46EA19; Wed, 26 Aug 2020 06:35:57 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) by gabe.freedesktop.org (Postfix) with ESMTPS id 276156EA0C for ; Wed, 26 Aug 2020 06:35:37 +0000 (UTC) Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20200826063535euoutp028a1dee92dde9ac9e51791ab36cb5a3be~uvetBvf0n1511215112euoutp02Q for ; Wed, 26 Aug 2020 06:35:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20200826063535euoutp028a1dee92dde9ac9e51791ab36cb5a3be~uvetBvf0n1511215112euoutp02Q DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1598423735; bh=p60/viRe3KtwGWtJeORUVY3qfIOgIF9iybwgzZ0CmPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S1WiNNxONRipzWaDBpFHJGu6zhxa4eC18RFZOJDRYCzS0uwsUpNeqR9UrMuw+PCrc J0Yv60HS976lo0mmTHiOcS+NLBG07UemU8U3bFWgjoX0HqVsFVyuXOyJ+chlEl3tGd H7pzWISa+n5bOiG4kew9bSQKvmi/65w6Tzy/0WWc= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20200826063535eucas1p1ec3c07ae73a90da4655bd0b6f633e016~uves0gNAh1479914799eucas1p1c; Wed, 26 Aug 2020 06:35:35 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id 0E.AD.05997.7B2064F5; Wed, 26 Aug 2020 07:35:35 +0100 (BST) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20200826063535eucas1p10cef37d8364216cf57c97e96d3959dd2~uvesPRM8S1480314803eucas1p1i; Wed, 26 Aug 2020 06:35:35 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20200826063535eusmtrp21794edd135bc8b04c20367fa6c631e7d~uvesOilpN0466204662eusmtrp2K; Wed, 26 Aug 2020 06:35:35 +0000 (GMT) X-AuditID: cbfec7f4-65dff7000000176d-cb-5f4602b7a639 Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id D2.53.06017.6B2064F5; Wed, 26 Aug 2020 07:35:34 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20200826063534eusmtip284cd426d5290cc263f07966ec9018e8a~uverllRjy0302303023eusmtip2x; Wed, 26 Aug 2020 06:35:34 +0000 (GMT) From: Marek Szyprowski To: dri-devel@lists.freedesktop.org, iommu@lists.linux-foundation.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Subject: [PATCH v9 12/32] drm: msm: fix common struct sg_table related issues Date: Wed, 26 Aug 2020 08:32:56 +0200 Message-Id: <20200826063316.23486-13-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200826063316.23486-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA0WSfUhTURjGOffu7l6Hq9smeVDLHGlZ5Dd4YSUVVpcgjKCgyHTlRS2/2pxm /yiJo2ZqU8IlYkssy89cw68IdWir5jR16NRMxcQSrMSPNGW27ar993ue93nPczgcAhUYMDci PimVkSZJEkRcHqfx/WrvkSbkdFTA5AMvKq/nI0I1qOsxaqNRhVLmpV9cqnTGBKhX1V0IpWkT U4vmSYTSTg1ilKrNhFMDraVcqrZzDKdmnq2gVMfvbxg1VtQJju+ka8pqAP1uWcOhW0rGcLpp eQKjx3MNCP2mIpMetU6hdJGlEtBvh7O4tKF1BKfzdVWAXtDuPe98hXc0hkmIT2Ok/mHRvDiV cRZL6RLdKdSYkSyw4KEETgQkQ+Dk4hKuBDxCQL4EcGI1B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W/ZEdxGEWWMIs0Tf1EliHsICvxL9r3SwgNouAqsStnRvBNvAK2Ems2jSPBWKDvMTqDQeY QWxOoPjx7n6obbYSp9fOYJ7AyLeAkWEVo0hqaXFuem6xkV5xYm5xaV66XnJ+7iZGYCRtO/Zz yw7GrnfBhxgFOBiVeHgXsLnGC7EmlhVX5h5ilOBgVhLhdTp7Ok6INyWxsiq1KD++qDQntfgQ oynQUROZpUST84FRnlcSb2hqaG5haWhubG5sZqEkztshcDBGSCA9sSQ1OzW1ILUIpo+Jg1Oq gfH4v3vLZm3yFQq9s3TP/tm9Uv8vrDZ7Uem7J9dgk6zFH5e42ad+fZ+3OLVT1Uox/p7q9aT0 ZxfYWYQ4bAwDiu7vbZ3hbsd49dRqxf8VE2/P7Eg807Kn9bTnNlaWxokpNdL589Zce1k+aUP3 y+xpxYZ3TUsvKDz6kP/MdPvMNYum2Fb7v3kjun23EktxRqKhFnNRcSIAlxwXaboCAAA= X-CMS-MailID: 20200826063535eucas1p10cef37d8364216cf57c97e96d3959dd2 X-Msg-Generator: CA X-RootMTR: 20200826063535eucas1p10cef37d8364216cf57c97e96d3959dd2 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200826063535eucas1p10cef37d8364216cf57c97e96d3959dd2 References: <20200826063316.23486-1-m.szyprowski@samsung.com> X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: freedreno@lists.freedesktop.org, Bartlomiej Zolnierkiewicz , David Airlie , Sean Paul , linux-arm-msm@vger.kernel.org, Robin Murphy , Christoph Hellwig , linux-arm-kernel@lists.infradead.org, Marek Szyprowski MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 Acked-by: Rob Clark --- drivers/gpu/drm/msm/msm_gem.c | 13 +++++-------- drivers/gpu/drm/msm/msm_gpummu.c | 14 ++++++-------- drivers/gpu/drm/msm/msm_iommu.c | 2 +- 3 files changed, 12 insertions(+), 17 deletions(-) -- 2.17.1 _______________________________________________ dri-devel mailing list dri-devel@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/dri-devel Reported-by: kernel test robot diff --git a/drivers/gpu/drm/msm/msm_gem.c b/drivers/gpu/drm/msm/msm_gem.c index b2f49152b4d4..8c7ae812b813 100644 --- a/drivers/gpu/drm/msm/msm_gem.c +++ b/drivers/gpu/drm/msm/msm_gem.c @@ -53,11 +53,10 @@ static void sync_for_device(struct msm_gem_object *msm_obj) struct device *dev = msm_obj->base.dev->dev; if (get_dma_ops(dev) && IS_ENABLED(CONFIG_ARM64)) { - dma_sync_sg_for_device(dev, msm_obj->sgt->sgl, - msm_obj->sgt->nents, DMA_BIDIRECTIONAL); + dma_sync_sgtable_for_device(dev, msm_obj->sgt, + DMA_BIDIRECTIONAL); } else { - dma_map_sg(dev, msm_obj->sgt->sgl, - msm_obj->sgt->nents, DMA_BIDIRECTIONAL); + dma_map_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0); } } @@ -66,11 +65,9 @@ static void sync_for_cpu(struct msm_gem_object *msm_obj) struct device *dev = msm_obj->base.dev->dev; if (get_dma_ops(dev) && IS_ENABLED(CONFIG_ARM64)) { - dma_sync_sg_for_cpu(dev, msm_obj->sgt->sgl, - msm_obj->sgt->nents, DMA_BIDIRECTIONAL); + dma_sync_sgtable_for_cpu(dev, msm_obj->sgt, DMA_BIDIRECTIONAL); } else { - dma_unmap_sg(dev, msm_obj->sgt->sgl, - msm_obj->sgt->nents, DMA_BIDIRECTIONAL); + dma_unmap_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0); } } diff --git a/drivers/gpu/drm/msm/msm_gpummu.c b/drivers/gpu/drm/msm/msm_gpummu.c index 310a31b05faa..319f06c28235 100644 --- a/drivers/gpu/drm/msm/msm_gpummu.c +++ b/drivers/gpu/drm/msm/msm_gpummu.c @@ -30,21 +30,19 @@ static int msm_gpummu_map(struct msm_mmu *mmu, uint64_t iova, { struct msm_gpummu *gpummu = to_msm_gpummu(mmu); unsigned idx = (iova - GPUMMU_VA_START) / GPUMMU_PAGE_SIZE; - struct scatterlist *sg; + struct sg_dma_page_iter dma_iter; unsigned prot_bits = 0; - unsigned i, j; if (prot & IOMMU_WRITE) prot_bits |= 1; if (prot & IOMMU_READ) prot_bits |= 2; - for_each_sg(sgt->sgl, sg, sgt->nents, i) { - dma_addr_t addr = sg->dma_address; - for (j = 0; j < sg->length / GPUMMU_PAGE_SIZE; j++, idx++) { - gpummu->table[idx] = addr | prot_bits; - addr += GPUMMU_PAGE_SIZE; - } + for_each_sgtable_dma_page(sgt, &dma_iter, 0) { + dma_addr_t addr = sg_page_iter_dma_address(&dma_iter); + + BUILD_BUG_ON(GPUMMU_PAGE_SIZE != PAGE_SIZE); + gpummu->table[idx++] = addr | prot_bits; } /* we can improve by deferring flush for multiple map() */ diff --git a/drivers/gpu/drm/msm/msm_iommu.c b/drivers/gpu/drm/msm/msm_iommu.c index 3a381a9674c9..6c31e65834c6 100644 --- a/drivers/gpu/drm/msm/msm_iommu.c +++ b/drivers/gpu/drm/msm/msm_iommu.c @@ -36,7 +36,7 @@ static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova, struct msm_iommu *iommu = to_msm_iommu(mmu); size_t ret; - ret = iommu_map_sg(iommu->domain, iova, sgt->sgl, sgt->nents, prot); + ret = iommu_map_sgtable(iommu->domain, iova, sgt, prot); WARN_ON(!ret); return (ret == len) ? 0 : -EINVAL;