Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1540617ybg; Thu, 11 Jun 2020 12:29:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwc0e4rUufLefxFbPoxC8IRJeLnEAp4kH6BXWxrmz1j6k3Zds0mYL4qeIdu5gQ0dRT+3ql9 X-Received: by 2002:a17:906:c672:: with SMTP id ew18mr10207511ejb.404.1591903776425; Thu, 11 Jun 2020 12:29:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591903776; cv=none; d=google.com; s=arc-20160816; b=LItk5y7r4/IZrJLx60NqbiSHCFS1i8qAhIgDIl6WUW8HPtwZbnWr1whfuWOtTA52Id AmLDGc0T3HAolv7lHuo/bWlD6OKPttXi2Tsol5zqGOF/vKlgN6UkVuHmyJ75vrah+Kat +qkirEof21Yxy7YvSDlOngvuZJNOqU1WCeG8LcMi3tf+6NpqMnAzZVDy1OixvM/ogqGl uZFqW6nL9bDdnMUNK69b8z+oO3nN6eUOYAm6Uq2VdhNtDzycpxCUB4Czj9MZyEoDFhTy 3dOZUhGZwW24BGPd95FqCiUiQLj7DtPI+hQgKFDKGmEwTpJLp0ELaiZcXB8tEkZTrTXJ x5KA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date:dkim-signature; bh=0RWHH5fLF59Um6Y5l8fqB4Uo2eJszov0rE1UzYFPSzI=; b=TZW/Qnz2Yyf3cJNv1hppLzkJDyCtedUlV3NYj+2X2KlaV/cr7yh2Yn0VcCY73pM4UB dcXctN1SGid7JEygIU5k73uyRnMPvTs4yaQp9leKOWpUDoYVK5juHLpwAm3JQ+eZIQsh Zm6lS2APIjFLGi1ZqL3UhpScOFRkSPgl5Ce7Bpxp+mSMDqEcIhF6jRFHvIsPpL7UG86r 85meRxo00wAMb5+zJtrMU0wMJuaUNWj7COrdvWXMDCBuQnS8UBclUBMUFgjlxw3dU1Mu tf++TmuWR34+i3Z+8t+1guuoP6hltNqXArGXQbvjSiQsA5xTwOPohq+aR1UrNqql48Rb xmfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=JdRQWIST; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s5si2266141ejd.121.2020.06.11.12.29.13; Thu, 11 Jun 2020 12:29:36 -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=@google.com header.s=20161025 header.b=JdRQWIST; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726990AbgFKTUc (ORCPT + 99 others); Thu, 11 Jun 2020 15:20:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725873AbgFKTUa (ORCPT ); Thu, 11 Jun 2020 15:20:30 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA1A6C08C5C1 for ; Thu, 11 Jun 2020 12:20:30 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id m2so2775835pjv.2 for ; Thu, 11 Jun 2020 12:20:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=0RWHH5fLF59Um6Y5l8fqB4Uo2eJszov0rE1UzYFPSzI=; b=JdRQWISTkKmhO8vUDE4vizFpUg804f1yUuzpg9VXBOYxGIyGbiL0XFdu0GZ+GvlSXT PrAd9RYV1ky0p+RrYgxGIxbun8jtjtOeLjr+umGxdeUqbsBPMNd5KjsGMbN+DB7VPvBy VqCLY10ISyV7J0igBjPB7QYIqKjqNHx+xK60voGz1sLBHrNIe+YkEo8Xdvq7WMAI7OnI yArx81zMkQGM8m7uR0qqEFh7j3Xe9OMF223woYvkRZlJQjzHWRWfbL7A3z7X0yYdWR57 /D/Uwaqwn/YZLRdlwSH1/t110UayyKpuU/YmyxMlsuuMUeCE6Pw1Jblkh+Xil+j5Y4o2 uMcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=0RWHH5fLF59Um6Y5l8fqB4Uo2eJszov0rE1UzYFPSzI=; b=cwoOMWiUBwbZc+E3WdTyGwd5Q9ha7udlq/v/RF8tT7bBwCa1ydTKzj0fzoGjWiilbN 2nV4YFTw8NEaLkJjH9R/NbADXUKj2jGepcnM3/k28H1+v4/n7/pSNvV5CVxDawkV9K5F orh9Q9LNH8a7R0qui5mjPhfVOZbGR9AkEhW4VKf3Hod5N9mMha4pXIMOLvowsQPorP73 cBOW+/Mk/QLeVaVhqaLAieGnoYddTszOyczxiWjZesW0j3cnsrsUh4iAPvvxgTZIHP1g e2b7MdpDUQj4uhACZPhHdBD7lU415RGoMdXYsvX95OgUsF9AlH9vvtBysqn3moyh7lWq Triw== X-Gm-Message-State: AOAM531sM4s+yxZj3KZVemFZbQ6bj6Vh1eNw3RamN/EGsQjQ8/wz8ZOp S+T9ntLgZHQxYeJgnpMVnasZXw== X-Received: by 2002:a17:90b:3690:: with SMTP id mj16mr9472674pjb.104.1591903229097; Thu, 11 Jun 2020 12:20:29 -0700 (PDT) Received: from [2620:15c:17:3:3a5:23a7:5e32:4598] ([2620:15c:17:3:3a5:23a7:5e32:4598]) by smtp.gmail.com with ESMTPSA id bv16sm3380989pjb.46.2020.06.11.12.20.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jun 2020 12:20:28 -0700 (PDT) Date: Thu, 11 Jun 2020 12:20:28 -0700 (PDT) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Christoph Hellwig cc: Thomas Lendacky , Brijesh Singh , Marek Szyprowski , Robin Murphy , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [patch for-5.8 1/4] dma-direct: always align allocation size in dma_direct_alloc_pages() In-Reply-To: Message-ID: References: User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org dma_alloc_contiguous() does size >> PAGE_SHIFT and set_memory_decrypted() works at page granularity. It's necessary to page align the allocation size in dma_direct_alloc_pages() for consistent behavior. This also fixes an issue when arch_dma_prep_coherent() is called on an unaligned allocation size for dma_alloc_need_uncached() when CONFIG_DMA_DIRECT_REMAP is disabled but CONFIG_ARCH_HAS_DMA_SET_UNCACHED is enabled. Cc: stable@vger.kernel.org Signed-off-by: David Rientjes --- kernel/dma/direct.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -112,11 +112,12 @@ static inline bool dma_should_free_from_pool(struct device *dev, struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, gfp_t gfp, unsigned long attrs) { - size_t alloc_size = PAGE_ALIGN(size); int node = dev_to_node(dev); struct page *page = NULL; u64 phys_limit; + VM_BUG_ON(!PAGE_ALIGNED(size)); + if (attrs & DMA_ATTR_NO_WARN) gfp |= __GFP_NOWARN; @@ -124,14 +125,14 @@ struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, gfp &= ~__GFP_ZERO; gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask, &phys_limit); - page = dma_alloc_contiguous(dev, alloc_size, gfp); + page = dma_alloc_contiguous(dev, size, gfp); if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { - dma_free_contiguous(dev, page, alloc_size); + dma_free_contiguous(dev, page, size); page = NULL; } again: if (!page) - page = alloc_pages_node(node, gfp, get_order(alloc_size)); + page = alloc_pages_node(node, gfp, get_order(size)); if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { dma_free_contiguous(dev, page, size); page = NULL; @@ -158,8 +159,10 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, struct page *page; void *ret; + size = PAGE_ALIGN(size); + if (dma_should_alloc_from_pool(dev, gfp, attrs)) { - ret = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &page, gfp); + ret = dma_alloc_from_pool(dev, size, &page, gfp); if (!ret) return NULL; goto done; @@ -183,10 +186,10 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, dma_alloc_need_uncached(dev, attrs)) || (IS_ENABLED(CONFIG_DMA_REMAP) && PageHighMem(page))) { /* remove any dirty cache lines on the kernel alias */ - arch_dma_prep_coherent(page, PAGE_ALIGN(size)); + arch_dma_prep_coherent(page, size); /* create a coherent mapping */ - ret = dma_common_contiguous_remap(page, PAGE_ALIGN(size), + ret = dma_common_contiguous_remap(page, size, dma_pgprot(dev, PAGE_KERNEL, attrs), __builtin_return_address(0)); if (!ret)