Received: by 10.223.185.116 with SMTP id b49csp6323497wrg; Wed, 28 Feb 2018 07:34:13 -0800 (PST) X-Google-Smtp-Source: AG47ELv7uoWnr1bSEX1WCrn07645mXFppNrNvLMLQ2yF8OCmjEmVOGAjN9bRfy/ruQkyLXhEGnbx X-Received: by 2002:a17:902:24d:: with SMTP id 71-v6mr386387plc.282.1519832053409; Wed, 28 Feb 2018 07:34:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519832053; cv=none; d=google.com; s=arc-20160816; b=N3TVfnho9qkGRoOFmX0O+Xkvs6KQ4ewK28GE0wsTaxOg2o4l1LPFU54kKZ1pK+ZF1f mEEGolTJ+RT3FNobEOdd0dOFo9w4fwD3ew94Mix6GYxit8PD+UQa+z+Cbas+z+uwt7/a Lm4a2to6yYYTkn7MVbafnuc4pwOGf2pwe0nQNOcpMKfFY5a2fFFHXe4UgP1rTjM1YPK7 JrU8n/jOEadiiUClkv8ZxhNMZwOvDDMP5RCDgYWvSyB0sl5sD8PhbDWOfH+Tmrt5iA2I /k9DFr26seLqcDpyhdCGlzz4RUcUn0YNhIp5nkRN9infeCZOUf4IysMU19eDkQ4/2BAA a4Cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=wnlzroWSDXmPbNBvUK3TfBV7gdA6+ZNgvZJ/DZIMm/s=; b=OxR1oH2C5HtWMeMRpSSRrqfX+qMwfzuKB55vvrEh3WE7p+zgxOwZhgXJ5LLzmwcxhL agp4DlEBpX+DJZDWyayy8LEXzdU1KPiASl2vzjbaQR24alBNSJR0Ej8LdwDhuDorJdkX ARFJE50kYXkOLgK1C+BRr28g7x1ljwwsElLYgw14jQUHKVamefsZLh34GXqi/oNCL7O4 3i3fxJFH8yKF+ptmlgtTDck562YaLtXCjIN3eJCBx3VudFVGZm5cpWE9M+Q+XSONGdAc ayNppPxo8vI8KmGFCbLUWbyCvovC36DqRq6oEXEs2LVQaoFDEulJGw1RLeQzGAgOnIsO qAbw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v22si1143284pgc.724.2018.02.28.07.33.58; Wed, 28 Feb 2018 07:34:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933120AbeB1Pc1 (ORCPT + 99 others); Wed, 28 Feb 2018 10:32:27 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:33313 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752859AbeB1PWi (ORCPT ); Wed, 28 Feb 2018 10:22:38 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1er3Yd-0006Xg-Vl; Wed, 28 Feb 2018 15:22:16 +0000 Received: from ben by deadeye with local (Exim 4.90_1) (envelope-from ) id 1er3Yc-0008MG-Rt; Wed, 28 Feb 2018 15:22:14 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Alex Williamson" , "Harsh Jain" , "Robin Murphy" Date: Wed, 28 Feb 2018 15:20:18 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 006/254] iommu/vt-d: Fix scatterlist offset handling In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.55-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Robin Murphy commit 29a90b70893817e2f2bb3cea40a29f5308e21b21 upstream. 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 f2682000 ... 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 Signed-off-by: Alex Williamson Signed-off-by: Ben Hutchings --- drivers/iommu/intel-iommu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) --- a/drivers/iommu/intel-iommu.c +++ b/drivers/iommu/intel-iommu.c @@ -2008,10 +2008,12 @@ static int __domain_mapping(struct dmar_ uint64_t tmp; if (!sg_res) { + unsigned int pgoff = sg->offset & ~PAGE_MASK; + sg_res = aligned_nrpages(sg->offset, sg->length); - sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset; + sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + pgoff; sg->dma_length = sg->length; - pteval = page_to_phys(sg_page(sg)) | prot; + pteval = (sg_phys(sg) - pgoff) | prot; phys_pfn = pteval >> VTD_PAGE_SHIFT; } @@ -3345,7 +3347,7 @@ static int intel_nontranslate_map_sg(str for_each_sg(sglist, sg, nelems, i) { BUG_ON(!sg_page(sg)); - sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset; + sg->dma_address = sg_phys(sg); sg->dma_length = sg->length; } return nelems;