Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp842713pxb; Tue, 3 Nov 2020 14:09:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJz1mi06hStoQzgtnB5c9KqZyC8CtXCvjYE0QWGYHR0dMKZ68K5J1aUGgwiCSyKFZ4e8XoLm X-Received: by 2002:a50:e705:: with SMTP id a5mr24098522edn.29.1604441341378; Tue, 03 Nov 2020 14:09:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604441341; cv=none; d=google.com; s=arc-20160816; b=ZhblVdgHv1nmmWm3fFVGZPjKjl2UUDxC07FljV5JKp2o8G1wF+SPMqhHjpliRl+uC6 hqMLJAL86Fz7v3r9bUedQqughxPr58xE+/4Ezhjge0rIxnsxIhNgfOGMczCcrdJPNA4O UNEMma3hQpCQOYLokWb128dvWJ4rY07lW70OIZI0777ZqGegZ55xZ0iwMjLzxt+jZttv Kk2Yj9edRlZ44bvCYvkhkAes6qOeRxZ5BwSHDu6u9QbnKNDn8fbC9hpotKPP8CkHF2Ui 1w03JzEX2q/oHtMgMTKHA46Up63Co/aVIN+yd5lNXJWKmqoNS8OjI+HXVtvOcBHHsqGA 0zvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bKbqumNi/SCOHE1V1h5GudDa12B7AbwbDRUm/99D1UQ=; b=pVda4UccM1+IDcp6XjMxieyxaSNNUYU03RcA6upOBXeDyKSdOjmCHOxDZZQ2AupWpu S5rCXG+ua5DSOnUOgh/m+U00bssQrnjW+LPX9MEGO2eIBIXfNUDs2qmZm8+FFrhVt/DC TvHdeGQBuPMjs11zrkEvbzDf8rQ0EiwZCcO9GtcifzIp2RRCSD0rtfrAMJBAhHkkeQr/ AYbisLDYCRShCGosBAd7VNk1nom1hp9eela2QPRZ0sqBYlSlbhbHmgic6LeQBjylAo62 gXRcfXHDlz1XIdqH9nqgnOMh4a8KHPtalLH2IsBd1JDzrkGvm2ryRKNZyLw6eHyDk4Xe aUyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="oI/hHmwu"; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rs18si119802ejb.162.2020.11.03.14.08.38; Tue, 03 Nov 2020 14:09:01 -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=@kernel.org header.s=default header.b="oI/hHmwu"; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730692AbgKCUnR (ORCPT + 99 others); Tue, 3 Nov 2020 15:43:17 -0500 Received: from mail.kernel.org ([198.145.29.99]:56542 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730615AbgKCUnM (ORCPT ); Tue, 3 Nov 2020 15:43:12 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 11A79223BD; Tue, 3 Nov 2020 20:43:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604436191; bh=ZHdm73npPxc3HkiRS9j9pQeYPuOlOCW9dZ/VgnwzAm4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oI/hHmwuL7B193K6rBShgZ2D0hnNw7zpCWgrgkBvLFwsWE5nlZm3ggrgaV6AbbZ+v RFht4cqv0B5353/ATOPBh7ZfVdSMc2AF+e8VZWf2+qTIGVdFaZfVeBOQRmkvYrDDrl 0s3FF7Qec7rqLxYAnPJfi5iR7vuT9ZbVdJFDq7g0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Marek Szyprowski , Steven Price , Rob Herring , Sasha Levin Subject: [PATCH 5.9 104/391] drm: panfrost: fix common struct sg_table related issues Date: Tue, 3 Nov 2020 21:32:35 +0100 Message-Id: <20201103203353.843935062@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203348.153465465@linuxfoundation.org> References: <20201103203348.153465465@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marek Szyprowski [ Upstream commit 34a4e66faf8b22c8409cbd46839ba5e488b1e6a9 ] 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 Reviewed-by: Steven Price Reviewed-by: Rob Herring Signed-off-by: Sasha Levin --- drivers/gpu/drm/panfrost/panfrost_gem.c | 4 ++-- drivers/gpu/drm/panfrost/panfrost_mmu.c | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c index 33355dd302f11..1a6cea0e0bd74 100644 --- a/drivers/gpu/drm/panfrost/panfrost_gem.c +++ b/drivers/gpu/drm/panfrost/panfrost_gem.c @@ -41,8 +41,8 @@ static void panfrost_gem_free_object(struct drm_gem_object *obj) for (i = 0; i < n_sgt; i++) { if (bo->sgts[i].sgl) { - dma_unmap_sg(pfdev->dev, bo->sgts[i].sgl, - bo->sgts[i].nents, DMA_BIDIRECTIONAL); + dma_unmap_sgtable(pfdev->dev, &bo->sgts[i], + DMA_BIDIRECTIONAL, 0); sg_free_table(&bo->sgts[i]); } } diff --git a/drivers/gpu/drm/panfrost/panfrost_mmu.c b/drivers/gpu/drm/panfrost/panfrost_mmu.c index e8f7b11352d27..776448c527ea9 100644 --- a/drivers/gpu/drm/panfrost/panfrost_mmu.c +++ b/drivers/gpu/drm/panfrost/panfrost_mmu.c @@ -253,7 +253,7 @@ static int mmu_map_sg(struct panfrost_device *pfdev, struct panfrost_mmu *mmu, struct io_pgtable_ops *ops = mmu->pgtbl_ops; u64 start_iova = iova; - for_each_sg(sgt->sgl, sgl, sgt->nents, count) { + for_each_sgtable_dma_sg(sgt, sgl, count) { unsigned long paddr = sg_dma_address(sgl); size_t len = sg_dma_len(sgl); @@ -517,10 +517,9 @@ static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as, if (ret) goto err_pages; - if (!dma_map_sg(pfdev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL)) { - ret = -EINVAL; + ret = dma_map_sgtable(pfdev->dev, sgt, DMA_BIDIRECTIONAL, 0); + if (ret) goto err_map; - } mmu_map_sg(pfdev, bomapping->mmu, addr, IOMMU_WRITE | IOMMU_READ | IOMMU_NOEXEC, sgt); -- 2.27.0