Return-Path: Received: from mail-cys01nam02on0092.outbound.protection.outlook.com ([104.47.37.92]:7188 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932338AbeARVDm (ORCPT ); Thu, 18 Jan 2018 16:03:42 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Robin Murphy , Alex Williamson , Sasha Levin Subject: [added to the 4.1 stable tree] iommu/vt-d: Fix scatterlist offset handling Date: Thu, 18 Jan 2018 21:00:49 +0000 Message-ID: <20180118205908.3220-160-alexander.levin@microsoft.com> References: <20180118205908.3220-1-alexander.levin@microsoft.com> In-Reply-To: <20180118205908.3220-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Robin Murphy This patch has been added to the stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit 29a90b70893817e2f2bb3cea40a29f5308e21b21 ] The intel-iommu DMA ops fail to correctly handle scatterlists where sg->offset is greater than PAGE_SIZE - the IOVA allocation is computed appropriately based on the page-aligned portion of the offset, but the mapping is set up relative to sg->page, which means it fails to actually cover the whole buffer (and in the worst case doesn't cover it at all): (sg->dma_address + sg->dma_len) ----+ sg->dma_address ---------+ | iov_pfn------+ | | | | | v v v iova: a b c d e f |--------|--------|--------|--------|--------| <...calculated....> [_____mapped______] pfn: 0 1 2 3 4 5 |--------|--------|--------|--------|--------| ^ ^ ^ | | | sg->page ----+ | | sg->offset --------------+ | (sg->offset + sg->length) ----------+ As a result, the caller ends up overrunning the mapping into whatever lies beyond, which usually goes badly: [ 429.645492] DMAR: DRHD: handling fault status reg 2 [ 429.650847] DMAR: [DMA Write] Request device [02:00.4] fault addr f26820= 00 ... Whilst this is a fairly rare occurrence, it can happen from the result of intermediate scatterlist processing such as scatterwalk_ffwd() in the crypto layer. Whilst that particular site could be fixed up, it still seems worthwhile to bring intel-iommu in line with other DMA API implementations in handling this robustly. To that end, fix the intel_map_sg() path to line up the mapping correctly (in units of MM pages rather than VT-d pages to match the aligned_nrpages() calculation) regardless of the offset, and use sg_phys() consistently for clarity. Reported-by: Harsh Jain Signed-off-by: Robin Murphy Reviewed by: Ashok Raj Tested by: Jacob Pan Cc: stable@vger.kernel.org Signed-off-by: Alex Williamson Signed-off-by: Sasha Levin --- drivers/iommu/intel-iommu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c index 275f59071f56..9d05a711a2f4 100644 --- a/drivers/iommu/intel-iommu.c +++ b/drivers/iommu/intel-iommu.c @@ -2018,10 +2018,12 @@ static int __domain_mapping(struct dmar_domain *dom= ain, unsigned long iov_pfn, uint64_t tmp; =20 if (!sg_res) { + unsigned int pgoff =3D sg->offset & ~PAGE_MASK; + sg_res =3D aligned_nrpages(sg->offset, sg->length); - sg->dma_address =3D ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offse= t; + sg->dma_address =3D ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + pgoff; sg->dma_length =3D sg->length; - pteval =3D page_to_phys(sg_page(sg)) | prot; + pteval =3D (sg_phys(sg) - pgoff) | prot; phys_pfn =3D pteval >> VTD_PAGE_SHIFT; } =20 @@ -3324,7 +3326,7 @@ static int intel_nontranslate_map_sg(struct device *h= ddev, =20 for_each_sg(sglist, sg, nelems, i) { BUG_ON(!sg_page(sg)); - sg->dma_address =3D page_to_phys(sg_page(sg)) + sg->offset; + sg->dma_address =3D sg_phys(sg); sg->dma_length =3D sg->length; } return nelems; --=20 2.11.0