Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2009562pxk; Tue, 1 Sep 2020 13:16:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx7GeL3sNeyc/QMI50Z6jQQ9Jl3SDA1/JYZfauvUN1Ui6JTNUmInve/V5smNqIYbffmjii5 X-Received: by 2002:a17:907:2456:: with SMTP id yw22mr95853ejb.337.1598991409994; Tue, 01 Sep 2020 13:16:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598991409; cv=none; d=google.com; s=arc-20160816; b=amftHa7hpD0iNPgZjrikVM/4kM8+wg4lJv6tb+pwOxjUy8OElCi9q7/hvV+CuRL4iT xBimyU6PF+TEKNqIUn9Qze8E4NzOtw3Z4aZMGafbDn3tQYNxeMI4r7f6lIOAAWx15Qof wVPvuJiiMOZZUA9M68bZKoj3hvCsVIVlH2qYsdC+0mKpGS3ray+mPfJJRlhH7+wgSDQa 7Gj4uff9SKzYq3YoYgKEm7sc03nwwX2nvUc8ReSzJHbcG+yKM1vxlcyejDHOecaKLMsg 0y99Eg+Td6RxHZt5LqimchBJXlYL1W6VqkulSARqPL3SMgzn5lzatUsp9801Xbftxq+3 rBTg== 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=L7J5idJRYyeOy/ak4+uxHq1cqRsoyUSKzlGZoITntEg=; b=cZoReCMOH7hPWvSkBOvg74LwxTfE8D8cKsE1fHz5NK2T1tvRdR7o5PRvBZGfjb1+j5 BTIEyWpkjuNahshKBumaLR/02eHARwHsPbl9y/NIFnug2vDefSw1z3BfxocMS0MG3NFY YqjDC1gbDJqBSOH8CukdzIKLoeeo4AdksShcaK8OJUJULrJgWMmYjYVLhDX3gvBZv/3X jpDA9wG/h0ZzQXGFZ5XQXObF9TrCytb2m/if4SWRzLPDo6p4ZUs9oETRFvTJPP1sEe9X 2SFb2oGgESK0qmjI7WKNFkUwOQSUrQk68GBVQn3NACXvjAQp92Sn4gdKlbeyn81Ytdz1 Kyhw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b19si1347004ejk.429.2020.09.01.13.16.27; Tue, 01 Sep 2020 13:16:49 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728204AbgIAUPU (ORCPT + 99 others); Tue, 1 Sep 2020 16:15:20 -0400 Received: from foss.arm.com ([217.140.110.172]:49554 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727103AbgIAUPO (ORCPT ); Tue, 1 Sep 2020 16:15:14 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0E2BE1063; Tue, 1 Sep 2020 13:15:14 -0700 (PDT) Received: from [10.57.40.122] (unknown [10.57.40.122]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5F1043F66F; Tue, 1 Sep 2020 13:15:11 -0700 (PDT) Subject: Re: [PATCH v9 29/32] rapidio: fix common struct sg_table related issues 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: Christoph Hellwig , Bartlomiej Zolnierkiewicz , linux-arm-kernel@lists.infradead.org, David Airlie , Daniel Vetter , Matt Porter , Alexandre Bounine References: <20200826063316.23486-1-m.szyprowski@samsung.com> <20200826063316.23486-30-m.szyprowski@samsung.com> From: Robin Murphy Message-ID: <5beb988a-099b-1247-b3d5-257de44b9ab5@arm.com> Date: Tue, 1 Sep 2020 21:15:10 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20200826063316.23486-30-m.szyprowski@samsung.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-08-26 07:33, 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. Reviewed-by: Robin Murphy > Signed-off-by: Marek Szyprowski > --- > drivers/rapidio/devices/rio_mport_cdev.c | 11 ++++------- > 1 file changed, 4 insertions(+), 7 deletions(-) > > diff --git a/drivers/rapidio/devices/rio_mport_cdev.c b/drivers/rapidio/devices/rio_mport_cdev.c > index a30342942e26..89eb3d212652 100644 > --- a/drivers/rapidio/devices/rio_mport_cdev.c > +++ b/drivers/rapidio/devices/rio_mport_cdev.c > @@ -573,8 +573,7 @@ static void dma_req_free(struct kref *ref) > refcount); > struct mport_cdev_priv *priv = req->priv; > > - dma_unmap_sg(req->dmach->device->dev, > - req->sgt.sgl, req->sgt.nents, req->dir); > + dma_unmap_sgtable(req->dmach->device->dev, &req->sgt, req->dir, 0); > sg_free_table(&req->sgt); > if (req->page_list) { > unpin_user_pages(req->page_list, req->nr_pages); > @@ -814,7 +813,6 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode, > struct mport_dev *md = priv->md; > struct dma_chan *chan; > int ret; > - int nents; > > if (xfer->length == 0) > return -EINVAL; > @@ -930,15 +928,14 @@ rio_dma_transfer(struct file *filp, u32 transfer_mode, > xfer->offset, xfer->length); > } > > - nents = dma_map_sg(chan->device->dev, > - req->sgt.sgl, req->sgt.nents, dir); > - if (nents == 0) { > + ret = dma_map_sgtable(chan->device->dev, &req->sgt, dir, 0); > + if (ret) { > rmcd_error("Failed to map SG list"); > ret = -EFAULT; > goto err_pg; > } > > - ret = do_dma_request(req, xfer, sync, nents); > + ret = do_dma_request(req, xfer, sync, req->sgt.nents); > > if (ret >= 0) { > if (sync == RIO_TRANSFER_ASYNC) >