Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp462318ybj; Tue, 5 May 2020 01:49:33 -0700 (PDT) X-Google-Smtp-Source: APiQypJdbXa3QlM+41VGIYhGoJ3PFs9YS3TD3/cbbwG9YIv9n4c33P8EhB/SKwqR2jlpa2HLWul4 X-Received: by 2002:a17:906:1186:: with SMTP id n6mr1581571eja.45.1588668573342; Tue, 05 May 2020 01:49:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588668573; cv=none; d=google.com; s=arc-20160816; b=FfajVvX9aVD+443/cQ3xjlM2HAiTJskgqKoxQY9eYleAX7I1OjLSvZ2n3TVb0vsVfA NzKl5dV1xzDx5KGQ7awI5I713fQo4CdBsmP/uK/u7DOmjZle6h8wQdS47YLdL50rg/AR mmClCRi9KvItF7XEMMG0O2DKaZF9wVCW7Tz1WWScxwsSXLkX9Sm1s0ZzMMruUF7flNg0 XAV6CUAHJ5CR3uIslhupIpAcAwtI/ylgpuaiTtdm7tZBchiddWg4XIgg01+2fgPdmp+M sUDvayBgTeKwPf6zoJw8wJo6N1RH2AYUIp/Ej3fei+ptm9Djiz4rW4haT8+K/tgT5pxl xyww== 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=TxE/6G1g28mmKh7hooUAC2aT+D+6azanETRBOwRYElg=; b=NnD2D7zvBD/9dLao1he+YrFr1Q6EzBX6au21+RsYweDqOd6zgSW2x6YIuOpnuwbLSJ QKqfr1RddZL3HWe/3gbEvGvO5kUglVt6upZN2xBAnwAV+MtyREDjd7K2u4DhfgtWffQW A/5j80hODiA6Ydxk8V7dh1HFUPnCxXaszZeup1fwsrcG5PEIHyk64oTSffj2jAY/O2xh UNREntFI0QJ0W0UQ8ZE+x6zyuYEK2qw7zFD4yqOlMVZCY00bt/y5Lnlxy0b2wvMj/24R KVHYxe7+Vb7USv3R2LrNVh6szEbA6p+a1eRPZ9jB/ld+mYhV2VmORt5C9iP+tbE545DW xQMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=omkg7R3W; 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 n24si744091ejg.142.2020.05.05.01.49.10; Tue, 05 May 2020 01:49:33 -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=omkg7R3W; 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 S1728795AbgEEIrF (ORCPT + 99 others); Tue, 5 May 2020 04:47:05 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:52038 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728727AbgEEIqk (ORCPT ); Tue, 5 May 2020 04:46:40 -0400 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20200505084639euoutp02afbfc845b5851da16f2c6347c15a8d63~MFX3rg9bu2639926399euoutp02T for ; Tue, 5 May 2020 08:46:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20200505084639euoutp02afbfc845b5851da16f2c6347c15a8d63~MFX3rg9bu2639926399euoutp02T DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1588668399; bh=TxE/6G1g28mmKh7hooUAC2aT+D+6azanETRBOwRYElg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=omkg7R3W/3Q2Bl+cb32xRIYW7VX/up76W1blrssWmzK4/YV9vkZmGlUAHrftdH/ky sBm3jWLZP1zBzJJT1oktuawgrGb+xZ++K3Cust+asUCXnN8sc9seby6u1Iz1mdAPcQ 6J8uGx4WXNmvPiCCXkEoHiF7i93T48TUIMuyi/8U= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20200505084638eucas1p14d6b13af09eff6253e2019c1163d2ea3~MFX3YaiQa2425224252eucas1p1F; Tue, 5 May 2020 08:46:38 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id 54.42.60679.EE721BE5; Tue, 5 May 2020 09:46:38 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20200505084638eucas1p24f356b441a3589e9528d239c0b9ac666~MFX28WU0-1948219482eucas1p2b; Tue, 5 May 2020 08:46:38 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20200505084638eusmtrp186bd6d7ec444ca0de6918ea0b42f52cb~MFX27rb8P0969409694eusmtrp1G; Tue, 5 May 2020 08:46:38 +0000 (GMT) X-AuditID: cbfec7f4-0e5ff7000001ed07-58-5eb127ee86c4 Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id A1.24.08375.EE721BE5; Tue, 5 May 2020 09:46:38 +0100 (BST) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20200505084637eusmtip174e6b9fe26a7b71a38ddfeb81a667898~MFX2aZfgP0580805808eusmtip1C; Tue, 5 May 2020 08:46:37 +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 v3 24/25] samples: vfio-mdev/mbochs: fix common struct sg_table related issues Date: Tue, 5 May 2020 10:46:13 +0200 Message-Id: <20200505084614.30424-24-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200505084614.30424-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA0VSa0hTYRjmO7edTSenafhhkTDIMEjTJA4YZuCPE4hFEYaVecqDl9y0zVku SmkosZx4KROvQzKvc9PUeSHU5TVBRMW0LJP5J2tecomZaZsH699ze9/n5eMjUUkb7kUmyFM5 hZxNkhIirH3w19iJ5WPN0ScLKvxp3dgIQjcXG3F6tz0fpad+rhB0XcMAQut7gumy53fp1tFy QNunFhC6xTqN05NdZQRt6P8koPtWF/FQMdNY0QiYNxt6jDFvfMGZ+adDCPP6ZQbzcceKMoUz NYDpns0kGJ1mmWByW+sBs95y5KJLlOhMLJeUkMYp/ENiRPGzVVlYitnt/nDZWzwT5LhqgZCE VBDMqm4mtEBESqhaADs2DbjTkFB2ALsqhbyxDuBW+ziyP/H5R72AN2oAzLUaMJ44JpqWnhHO FEEFQK1Nu4c9qCwAh3WuzhBKTSPQ9md5r8OdioH2ueo9jFFHoeZ3EebEYioEduvacL7OGzaY elEnFjp068Tc3rGQGhTAfsuM4w7SQcJg7bKKz7vDpaFWAY8Pw9HCHIzPawBcGDMIeJID4OTj YsCnguHc2BbhXIRSvtDY5c/L56B2Y5Lg97vBGdsBp4w6YEH7C5SXxfBJtoRP+8CSoaZ/tX3j E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NFwUvnq 7Y+P9t25sOyPjaDdzg/1fZuuaMVvUF+4KkRLeLIi5+sLf9/O2pJYzcG2yXqzaPG561YH8sIS NuxL+OLNGrR760KPOes3HzKO4fEpKTeN2Za65cTOG4vi7GYL2K1Yv7u5N4dpuUzs+Y6jqd9f fjj96Kcd58uVivOUWIozEg21mIuKEwHQUyHvqgIAAA== X-CMS-MailID: 20200505084638eucas1p24f356b441a3589e9528d239c0b9ac666 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20200505084638eucas1p24f356b441a3589e9528d239c0b9ac666 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200505084638eucas1p24f356b441a3589e9528d239c0b9ac666 References: <20200505083926.28503-1-m.szyprowski@samsung.com> <20200505084614.30424-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 dma_map_sg returns the numer of the created entries in the DMA address space. However the subsequent calls to dma_sync_sg_for_{device,cpu} and dma_unmap_sg must be called with the original number of the entries passed to dma_map_sg. The sg_table->nents in turn holds the result of the dma_map_sg call as stated in include/linux/scatterlist.h. A common mistake was to ignore a result of the dma_map_sg function and don't use the sg_table->orig_nents at all. To avoid such issues, lets use common dma-mapping wrappers operating directly on the struct sg_table objects and adjust references to the nents and orig_nents respectively. While touching this code, also add missing call to dma_unmap_sgtable. Signed-off-by: Marek Szyprowski --- For more information, see '[PATCH v3 00/25] DRM: fix struct sg_table nents vs. orig_nents misuse' thread: https://lkml.org/lkml/2020/5/5/187 --- 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 3cc5e59..f2c62e0 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)) 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); sg_free_table(sg); kfree(sg); } -- 1.9.1