Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp545657ybj; Thu, 7 May 2020 01:50:09 -0700 (PDT) X-Google-Smtp-Source: APiQypKqGV27c82GJBZS6vjuyghZEaEyx7phhz0TynnimCloBCmR5pO1DrlV9K8gCl/T3EpqcSZA X-Received: by 2002:a17:906:17c1:: with SMTP id u1mr11172773eje.47.1588841409026; Thu, 07 May 2020 01:50:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588841409; cv=none; d=google.com; s=arc-20160816; b=ya0/VYyL3jq4jWT/Ko6qrEIalbK32uMxLFqE/AdzEQUoqb3wltNWLxS3X2K1kgLAw4 tn5hYYBvTjj/z1TwbBOvJp3k3uwE5afLFnNad0HJA56Ot76mI4h76O1u2Bb/7kqHiCmV tJyMI80ZzKVyGHD3NILd37XJeg4eDIeWLkDvN9HgRsfodZx3AwD7pernwTuVn9E0ZrfF EUfO4Gs6oCgJBBhLxNWeH1t11Mges8Bl8o0O2rFj0TxxSwjjaS+5RwzEeHe1QPNIcywQ 5cClX1/w2NAYFU7DAy9nnnpNueOQArUfHHFzj6RQW5c+maX84sitJalEsHCKxzoJ6Uqk kXnA== 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:dkim-signature; bh=GOj2i2e0G1hf/TdwWNiZFyEjE3oPm0LZIISDTcjRCZ0=; b=LEQ5AC5ltIseVHH37QYpIIWa3dhKK5im7VhWCDVA9ITgm24N3jJP7z4FdXKbSWLQ04 5iZcbR1dEATgdayGoxg+WlsSAFasGd7YHmWqB2YhPFTa4eT5Sjjk83iyMdmo31cGaikO eyDtEep9VjBTDJwa+LDuJdlE1FPzttBUa0hyPL36r6GHKKRm/6aCeqqgX8yREnKH+y+A bhaguEG4wmjoIiILmz4tt5zoeCEsnydfehWi2+ohxmkPW9SjyyESGp6j5WRrXejOnr2Z /bMg0E4W55haa8EC1cSHHIk2Yun+5fnYKuY0UfGe7T8kgrKowVOgIKZJn6QzKNlKBMk/ 2+6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xs4all.nl header.s=s1 header.b=OsQAOw4B; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i15si2820588ejc.172.2020.05.07.01.49.44; Thu, 07 May 2020 01:50:09 -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=@xs4all.nl header.s=s1 header.b=OsQAOw4B; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725858AbgEGIr6 (ORCPT + 99 others); Thu, 7 May 2020 04:47:58 -0400 Received: from lb1-smtp-cloud7.xs4all.net ([194.109.24.24]:42713 "EHLO lb1-smtp-cloud7.xs4all.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725802AbgEGIr6 (ORCPT ); Thu, 7 May 2020 04:47:58 -0400 Received: from cust-b5b5937f ([IPv6:fc0c:c16d:66b8:757f:c639:739b:9d66:799d]) by smtp-cloud7.xs4all.net with ESMTPA id WcByjUh3NtKAsWcC2jYyHm; Thu, 07 May 2020 10:47:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xs4all.nl; s=s1; t=1588841275; bh=GOj2i2e0G1hf/TdwWNiZFyEjE3oPm0LZIISDTcjRCZ0=; h=Subject:To:From:Message-ID:Date:MIME-Version:Content-Type:From: Subject; b=OsQAOw4BCiMS8nSpCtEbPal3MaDqPDcvb/AHQPtbrBn3ybi/ChB9KwzXym2ZJIKEO 9n3yxwQxFyEQdTZjBDavlhIJ3bwHq99nJquR43ukYrIEo8zFaxzzorFeTA9UX6Mxr7 ZfY09bw58dMpE1iugEl5bl4lD2KkLSoIqeZRlTxUsbmPi2ileE8h9LVdgcYdY9J02p k1SYzSlQ04u5cqWHv/K/V44E8Dcc8g5gk1hF/kcK5zfD+Q794ZuD51BKklzQ44VlF+ cM8xZ14JNalRlgsLP5+CJKpqYIx2q5sysXW+fsaZppDUXAwt1PStxKdRo87bHZXmb7 3ArwWoakmqsyA== Subject: Re: [PATCH v3 01/25] dma-mapping: add generic helpers for mapping sgtable objects To: Marek Szyprowski , dri-devel@lists.freedesktop.org, iommu@lists.linux-foundation.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Cc: Bartlomiej Zolnierkiewicz , David Airlie , Joerg Roedel , Robin Murphy , Christoph Hellwig , linux-arm-kernel@lists.infradead.org References: <20200505083926.28503-1-m.szyprowski@samsung.com> <20200505084614.30424-1-m.szyprowski@samsung.com> From: Hans Verkuil Message-ID: <13d9029f-7d03-3624-1e22-f88b769e1b30@xs4all.nl> Date: Thu, 7 May 2020 10:47:42 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <20200505084614.30424-1-m.szyprowski@samsung.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CMAE-Envelope: MS4wfAY0hozkR2I/OEMAY07+RlgR2H0QspkxLI2sVoCrd7jtub/9esOqC31rXfxbYuIfHeJywJq8p3okwW7LEnUgwRaMNBLBmY8XaNmNC1q8aD5iqeqrS0+3 loy39fSj6vKmxMdJ/t/aMa5txJkwIIgY1BiELtj9686KjXylfnKrRwbDajHHCli2Nt7ZhivGlG/zCAB4/X+SSpgwR3rKKJZiUe5NvgAh6hGEoywFvYE1HSZk q2x9Is67YxPhYF4ju5OX4UztbgGbpq5EbFAyWG1RHCFu9yQ5Xdwa10osaZR9lg5ePqvj8UY1Fu1L8YZnB5pron1MFYUvqMX5G0z5T/LdyqVpebpH/rQ9ob5x cZNA271ZHYJvk7MTMWGAknMenTl9VrTI2Ivs0vG9seLlf4sMGjm+toxhFV+oKDbGOQKvfDpBZExiNqoOmA0VuD+gBc1WsPWoLRlo0kmF1APZyBumzDdWHIU4 tkK7mecVvV6KzLBwTtyGvFU8TRAxyohKPBqdfyxWZKckt5QzTXjamshO2gVMKfD22MppB9HSSp4M5Fy+ Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Marek, On 05/05/2020 10:45, Marek Szyprowski wrote: > struct sg_table is a common structure used for describing a memory > buffer. 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 pages (nents entry). > > It turned out that it was a common mistake to misuse nents and orig_nents > entries, call 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 introduce a common wrappers operating directly > on the struct sg_table objects, which take care of the proper use of > the nents and orig_nents entries. > > Signed-off-by: Marek Szyprowski > --- > include/linux/dma-mapping.h | 32 ++++++++++++++++++++++++++++++++ > include/linux/iommu.h | 6 ++++++ > 2 files changed, 38 insertions(+) > > diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h > index b43116a..8364c20d 100644 > --- a/include/linux/dma-mapping.h > +++ b/include/linux/dma-mapping.h > @@ -609,6 +609,36 @@ static inline void dma_sync_single_range_for_device(struct device *dev, > return dma_sync_single_for_device(dev, addr + offset, size, dir); > } > > +static inline int dma_map_sgtable_attrs(struct device *dev, > + struct sg_table *sgt, enum dma_data_direction dir, unsigned long attrs) Please document these new inline functions! The core reason IMHO why so many drivers do this wrong is that the struct sg_table documentation is very poor. In particular w.r.t. how the nents and orig_nents fields should be used. It confused me, and clearly I am not alone in that. So while this is a very nice improvement, it doesn't address the core issue, i.e. the documentation. Especially some examples on how to use these new functions would be welcome. Regards, Hans > +{ > + int n = dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs); > + > + if (n > 0) { > + sgt->nents = n; > + return 0; > + } > + return -EINVAL; > +} > + > +static inline void dma_unmap_sgtable_attrs(struct device *dev, > + struct sg_table *sgt, enum dma_data_direction dir, unsigned long attrs) > +{ > + dma_unmap_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs); > +} > + > +static inline void dma_sync_sgtable_for_cpu(struct device *dev, > + struct sg_table *sgt, enum dma_data_direction dir) > +{ > + dma_sync_sg_for_cpu(dev, sgt->sgl, sgt->orig_nents, dir); > +} > + > +static inline void dma_sync_sgtable_for_device(struct device *dev, > + struct sg_table *sgt, enum dma_data_direction dir) > +{ > + dma_sync_sg_for_device(dev, sgt->sgl, sgt->orig_nents, dir); > +} > + > #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0) > #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0) > #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0) > @@ -617,6 +647,8 @@ static inline void dma_sync_single_range_for_device(struct device *dev, > #define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0) > #define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0) > #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0) > +#define dma_map_sgtable(d, s, r) dma_map_sgtable_attrs(d, s, r, 0) > +#define dma_unmap_sgtable(d, s, r) dma_unmap_sgtable_attrs(d, s, r, 0) > > extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, > void *cpu_addr, dma_addr_t dma_addr, size_t size, > diff --git a/include/linux/iommu.h b/include/linux/iommu.h > index 7ef8b0b..5106b65 100644 > --- a/include/linux/iommu.h > +++ b/include/linux/iommu.h > @@ -466,6 +466,12 @@ extern size_t iommu_map_sg_atomic(struct iommu_domain *domain, > extern void iommu_set_fault_handler(struct iommu_domain *domain, > iommu_fault_handler_t handler, void *token); > > +static inline size_t iommu_map_sgtable(struct iommu_domain *domain, > + unsigned long iova, struct sg_table *sgt, int prot) > +{ > + return iommu_map_sg(domain, iova, sgt->sgl, sgt->orig_nents, prot); > +} > + > extern void iommu_get_resv_regions(struct device *dev, struct list_head *list); > extern void iommu_put_resv_regions(struct device *dev, struct list_head *list); > extern void generic_iommu_put_resv_regions(struct device *dev, >