Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp3438895pxa; Tue, 25 Aug 2020 23:38:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwekt69Yrklb+lC2wUDT1wgp1WacjHK5n6Ip/5F/bUrnKVCgA9mhVToM5Mm1H868T26g65 X-Received: by 2002:a17:906:311a:: with SMTP id 26mr14055058ejx.43.1598423901850; Tue, 25 Aug 2020 23:38:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598423901; cv=none; d=google.com; s=arc-20160816; b=gfPmqOIuHKXoKBQ85AAm8R5OPJOh6qgLWQBjnfpX6CbFSaihfXgdlxL9qPiM5m6AvC cPOCU8D4p5EbR5zrQ1YdN184cfD5hZajcaDUlN+xizGvMZ8UXbdhNibER/MgbMp7dFLs wB7eqPKueuheR6/w+F2BkGqPWiYQ7d0o1COelmz9Za6ci75pqqIJWpvibm6SYv4TTXF6 LhCVG1ODNWXajpp1UrKrK4zQGJuaaqmkpgzvQBHbjAAmCar+IV+sANmPKqO0ct6BtGc8 yhWWj5hmMafhC+OP8wrpT7yHbfqAsOO/LdRXECF7m1pGHqAIppPJGALJuYxC+T1OBO4B IdxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:cms-type:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dkim-filter; bh=i9tfBl9XK1ZIcmVNmOj760Z4Av4MmZkmEeWSykU107Y=; b=e//GWM6Q90SunnNVbN/PmHRNxydP92t5LsXp0KgCto3pAo/+4l4P4btO5cD+OECgu6 rfIri48Fgs22J7Y+cQriwbM56dxKRO0L4+qANaqv+Fq9ofLR+z9sF6soz0Rg78xvqYbn 3k34XzmFHYTfhYEIOlTd7Wr5qlgGJ1j6JCAxkHAg8am/R1B1JlBQgu12qh8NFp6owWhJ ymx4IAXRKznGELr3X5jggELSyZVBZCOBgw3kRrcACOkPawxH08CB2KjA8/NKD0dCtRRu uS5iojJ9/IH/CQiOydts4Jeq+Da/OrDKOQCu2KD57h81otYVzf8oDsB+Dko+7htg7caB sTKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b="MDWa/v2U"; 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=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i16si842108edr.196.2020.08.25.23.37.59; Tue, 25 Aug 2020 23:38:21 -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=@samsung.com header.s=mail20170921 header.b="MDWa/v2U"; 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=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727022AbgHZGgh (ORCPT + 99 others); Wed, 26 Aug 2020 02:36:37 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:39452 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726886AbgHZGfu (ORCPT ); Wed, 26 Aug 2020 02:35:50 -0400 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20200826063546euoutp02dcb3f8dad44cee87fa00d3238aaf474e~uve3Mi5cX1512715127euoutp02o for ; Wed, 26 Aug 2020 06:35:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20200826063546euoutp02dcb3f8dad44cee87fa00d3238aaf474e~uve3Mi5cX1512715127euoutp02o DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1598423746; bh=i9tfBl9XK1ZIcmVNmOj760Z4Av4MmZkmEeWSykU107Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MDWa/v2UN64kd/Pdh2+Eqeqoqxov2kLfJNAWiAuYfPUDZdGsjK6rzp0jDh/Ieviln FTtvqfiaz8SXSMsHICUOsl47/oAzIj4oVOeTnycj3hrxaX9nk4J4vuowAU11CNovOv MRLIevFWqldfH/vPZ87AN+jaPiLfdmm0deNKsdu8= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20200826063546eucas1p248e3327a2574966e6260f8ea4243a18c~uve27uU5X0403504035eucas1p2_; Wed, 26 Aug 2020 06:35:46 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id 59.BD.05997.2C2064F5; Wed, 26 Aug 2020 07:35:46 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20200826063546eucas1p268558dcd08ac9b43843f9f5e23da227d~uve2fzbFv0398703987eucas1p2r; Wed, 26 Aug 2020 06:35:46 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20200826063546eusmtrp1324e39600c0b1709cb7b91c674f0a758~uve2fFvRB1167511675eusmtrp1M; Wed, 26 Aug 2020 06:35:46 +0000 (GMT) X-AuditID: cbfec7f4-65dff7000000176d-ed-5f4602c23e0d Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id B0.E0.06314.1C2064F5; Wed, 26 Aug 2020 07:35:45 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20200826063545eusmtip2148831d8e511c634dd22bec68983ea60~uve14XvZb0092300923eusmtip2U; Wed, 26 Aug 2020 06:35:45 +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 Cc: Marek Szyprowski , Christoph Hellwig , Robin Murphy , Bartlomiej Zolnierkiewicz , linux-arm-kernel@lists.infradead.org, David Airlie , Daniel Vetter , Kirti Wankhede , kvm@vger.kernel.org Subject: [PATCH v9 30/32] samples: vfio-mdev/mbochs: fix common struct sg_table related issues Date: Wed, 26 Aug 2020 08:33:14 +0200 Message-Id: <20200826063316.23486-31-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: H4sIAAAAAAAAA+NgFtrMKsWRmVeSWpSXmKPExsWy7djP87qHmNziDT68kLXoPXeSyWLjjPWs Fv+3TWS2uPL1PZvFytVHmSwW7Le2mDO10GLL6bmMFl+uPGSy2PT4GqvF5V1z2CzWHrnLbnHw wxNWB16PNfPWMHrs/baAxWP7twesHve7jzN5bF5S73H732Nmj8k3ljN67L7ZwObR2/yOzaNv yypGj8+b5AK4o7hsUlJzMstSi/TtErgyfm/bxF6wRLDi1o78BsaNfF2MnBwSAiYSbe2XGbsY uTiEBFYwSnx/tZYFwvnCKHHnxE4WkCohgc+MEp++x8N0rO9bDdWxnFGid9d0JriOM1fmMoNU sQkYSnS97WIDsUUEWhklTvTygBQxC1xjknj79x1rFyMHh7BAgsSnV6kgNSwCqhLPph8C6+UV sJOY1/qIDWKbvMTqDQfA4pxA8ePd/VDxfewSO4/EQNguEnfetjFC2MISr45vYYewZSROT+4B e0dCoJlR4uG5tewQTg+jxOWmGVAd1hJ3zv1iAzmIWUBTYv0ufYiwo0T3uknMIGEJAT6JG28F QcLMQOakbdOhwrwSHW1CENVqErOOr4Nbe/DCJWYI20Oie+N5Nkj4TGSU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kGRsu2U4X6kaYfvvde3DRTdfW1pKJj l2V3bww7cEX3+YmKl0rMtrNUezkDopax21noP7DjmaHGtro+5aG64EVHpZfTQtITnsZH2mzy DCqQmsTD6HZt0cTgpAtaNzdGlxSt3+reNuf2Y8MfLVczbF8tnp4c8cMk0EhskYlH7ZJPbQdC XHfadyrdVWIpzkg01GIuKk4EAOSPyuupAgAA X-CMS-MailID: 20200826063546eucas1p268558dcd08ac9b43843f9f5e23da227d X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20200826063546eucas1p268558dcd08ac9b43843f9f5e23da227d X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200826063546eucas1p268558dcd08ac9b43843f9f5e23da227d References: <20200826063316.23486-1-m.szyprowski@samsung.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. While touching this code, also add missing call to dma_unmap_sgtable. Signed-off-by: Marek Szyprowski --- samples/vfio-mdev/mbochs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c index 3cc5e5921682..e03068917273 100644 --- a/samples/vfio-mdev/mbochs.c +++ b/samples/vfio-mdev/mbochs.c @@ -846,7 +846,7 @@ static struct sg_table *mbochs_map_dmabuf(struct dma_buf_attachment *at, if (sg_alloc_table_from_pages(sg, dmabuf->pages, dmabuf->pagecount, 0, dmabuf->mode.size, GFP_KERNEL) < 0) goto err2; - if (!dma_map_sg(at->dev, sg->sgl, sg->nents, direction)) + if (dma_map_sgtable(at->dev, sg, direction, 0)) goto err3; return sg; @@ -868,6 +868,7 @@ static void mbochs_unmap_dmabuf(struct dma_buf_attachment *at, dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id); + dma_unmap_sgtable(at->dev, sg, direction, 0); sg_free_table(sg); kfree(sg); } -- 2.17.1