From patchwork Thu Jul 26 13:02:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 142965 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp431254ljj; Thu, 26 Jul 2018 06:02:25 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe5LQF+4JFMS70Hak3jlmc9mhIdmc/YjjclUUBH/t7NGBpAKie7gg09BU/OzOF/2ccwQV4w X-Received: by 2002:a63:1811:: with SMTP id y17-v6mr1881974pgl.356.1532610145627; Thu, 26 Jul 2018 06:02:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532610145; cv=none; d=google.com; s=arc-20160816; b=0jX3ySE2DVIT50PF6x9ewktPb8MgS0pgyr5SiQB2FM5DklVxtYCSBszIm1d5P7IClq I+ZUJBwMeTClTJhI3Mc1CdrIToIsViwhhmf+QSEvMfuZX6XQwEiXEOUx7ZrkK5nF5jc7 Rqb6qxpYHEjrEjs3Ks2YNDIfJz3zVG7bn6iajEqunaVIQvwg67/2pfpmQHTqlqQy7xj1 R/+o7EsBg7katP1sJT+8hkUYNXL6SELEt9bNELO4jr7ZNS+GaxmIdBhUMgd09C5AXlg+ fTU/9KQGDpA4G9V49JSDKK7LuxwSBO3l6xXGrkMD5TRMgHW+PA/41k0/2AG8yYX2yusA 5hGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=nTGdXJiJblo1L4+5NOivAYaGS+ghePACZCy4c7W71hc=; b=fjSbgyzxtADqf9+1iYFGbAoI305i17PV6lMOyQ4a/2/zjvw+rzMk0HfONexFEtO9/6 ghtej1HpueS8fP4qYBQzVf0zWWhS9nUrCDbRW+0mOoiRDN/kSiYNRQzEZOCU7A6VY7hR XQXiISqwQD/3p1mcapz04hYV43lCxrJHnP5vYGFTMeNkW1Eom9mJ4KJwOMw83fj3GPEL G4pqe7nUWLOlakin6Sv/9pS0dCrYVlVrQ0idFGGTuVka7v7hUToqXhl4PD2HUu0EIPrC rWzNJSqbe67kyj0icFk79zsFKYZKnnyt2ANnXSyoiZ2h53InQusQE1aQAW5Zg0a3TVG1 OLFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=mail header.b=Ez1FyiJH; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o5-v6si1207118pll.449.2018.07.26.06.02.24; Thu, 26 Jul 2018 06:02:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=mail header.b=Ez1FyiJH; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730379AbeGZOTK (ORCPT + 13 others); Thu, 26 Jul 2018 10:19:10 -0400 Received: from frisell.zx2c4.com ([192.95.5.64]:38803 "EHLO frisell.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729510AbeGZOTJ (ORCPT ); Thu, 26 Jul 2018 10:19:09 -0400 Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTP id a46bf7a0; Thu, 26 Jul 2018 12:51:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=from:to:cc :subject:date:message-id; s=mail; bh=LDJbAE4nAscBsBEbjGT7O4IlpGc =; b=Ez1FyiJH7UiavPpKL7lXkJ07d9JwM8WlWVn0HmFTK5Lp9DlLYDaMtVeFYoh WAF358R7eSngq1wSu4Mu0NMdz9Z9oItmEZPvVRzL7DJ0NJL3Kk+Hm3yYX3O8MHXG xQP86BcacDORI7Dd+ypcHYupQY8cgla12yxnZN2GrAZ33gvN8FhPVoD5Njo/Rl/Z WEYO5cpZZIjPygD+DEXLMNo9ouqiyPY3CuMB8FT7z/+v/adl1ymzsjMMq6PN3i/y mZ3sGKTDBi6tUbbbOTibAoufcuXsEhkaC/RYy45fn9rvIG1ZeYbZubnEqYfTeR9R COlx8Hcntr9giqwP/dAs/FLis+Q== Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 75f00311 (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Thu, 26 Jul 2018 12:51:40 +0000 (UTC) From: "Jason A. Donenfeld" To: stable@vger.kernel.org Cc: Christoph Hellwig Subject: [PATCH 4.17.y] Revert "iommu/intel-iommu: Enable CONFIG_DMA_DIRECT_OPS=y and clean up intel_{alloc, free}_coherent()" Date: Thu, 26 Jul 2018 15:02:05 +0200 Message-Id: <20180726130205.1973-1-Jason@zx2c4.com> Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Christoph Hellwig commit 7ec916f82c48dcfc115eee2e3e0e6d400e310fc5 upstream. This commit may cause a less than required dma mask to be used for some allocations, which apparently leads to module load failures for iwlwifi sometimes. This reverts commit d657c5c73ca987214a6f9436e435b34fc60f332a. Signed-off-by: Christoph Hellwig Reported-by: Fabio Coatti Tested-by: Fabio Coatti --- Backporting this and submitting this to stable@, because without it, ordinary WiFi is broken on a fairly vanilla Thinkpad P50, on all 4.17 kernels. drivers/iommu/Kconfig | 1 - drivers/iommu/intel-iommu.c | 62 +++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 17 deletions(-) -- 2.18.0 diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index b38798cc5288..f3a21343e636 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -142,7 +142,6 @@ config DMAR_TABLE config INTEL_IOMMU bool "Support for Intel IOMMU using DMA Remapping Devices" depends on PCI_MSI && ACPI && (X86 || IA64_GENERIC) - select DMA_DIRECT_OPS select IOMMU_API select IOMMU_IOVA select DMAR_TABLE diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c index 749d8f235346..6392a4964fc5 100644 --- a/drivers/iommu/intel-iommu.c +++ b/drivers/iommu/intel-iommu.c @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -3709,30 +3708,61 @@ static void *intel_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags, unsigned long attrs) { - void *vaddr; + struct page *page = NULL; + int order; - vaddr = dma_direct_alloc(dev, size, dma_handle, flags, attrs); - if (iommu_no_mapping(dev) || !vaddr) - return vaddr; + size = PAGE_ALIGN(size); + order = get_order(size); - *dma_handle = __intel_map_single(dev, virt_to_phys(vaddr), - PAGE_ALIGN(size), DMA_BIDIRECTIONAL, - dev->coherent_dma_mask); - if (!*dma_handle) - goto out_free_pages; - return vaddr; + if (!iommu_no_mapping(dev)) + flags &= ~(GFP_DMA | GFP_DMA32); + else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) { + if (dev->coherent_dma_mask < DMA_BIT_MASK(32)) + flags |= GFP_DMA; + else + flags |= GFP_DMA32; + } + + if (gfpflags_allow_blocking(flags)) { + unsigned int count = size >> PAGE_SHIFT; + + page = dma_alloc_from_contiguous(dev, count, order, flags); + if (page && iommu_no_mapping(dev) && + page_to_phys(page) + size > dev->coherent_dma_mask) { + dma_release_from_contiguous(dev, page, count); + page = NULL; + } + } + + if (!page) + page = alloc_pages(flags, order); + if (!page) + return NULL; + memset(page_address(page), 0, size); + + *dma_handle = __intel_map_single(dev, page_to_phys(page), size, + DMA_BIDIRECTIONAL, + dev->coherent_dma_mask); + if (*dma_handle) + return page_address(page); + if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) + __free_pages(page, order); -out_free_pages: - dma_direct_free(dev, size, vaddr, *dma_handle, attrs); return NULL; } static void intel_free_coherent(struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle, unsigned long attrs) { - if (!iommu_no_mapping(dev)) - intel_unmap(dev, dma_handle, PAGE_ALIGN(size)); - dma_direct_free(dev, size, vaddr, dma_handle, attrs); + int order; + struct page *page = virt_to_page(vaddr); + + size = PAGE_ALIGN(size); + order = get_order(size); + + intel_unmap(dev, dma_handle, size); + if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) + __free_pages(page, order); } static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,