Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1624517pxa; Thu, 6 Aug 2020 11:51:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJycsHuwyJv6B3K4DzE+6gUT46gf5JiZLxGKgQUN5mOatoDJ3DWpn7cM9m0p2AyWMwA04kHU X-Received: by 2002:a17:906:16c8:: with SMTP id t8mr5686670ejd.484.1596739889089; Thu, 06 Aug 2020 11:51:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596739889; cv=none; d=google.com; s=arc-20160816; b=V/swuQnDBRD+RiID0vVBUJdJLQzVUNsiL3ZOm5HMxZfFPv2wygsBZBA7LuPs3NcRr7 Lf9d2cNheZ39q2u54CNwrDU+HQDN+snzsQOSxcjkNmb3V3KzTakGy6WzUVEcwT+VFds1 CwJXPMhTyKhqJuaf4wfuHstElx/3h2hr53rV9ebxNpRIcgtReyfgJss11W/ZRBaO3N28 /by8YdLmTHRPY8+AFjU4CM0SdZnqUEvS4nJa84IN9w/kV0Z4wKEAhU5Fimi8Rh1a4ErP twniiM5cbpPMKsMovefYYvDqh6YRwJwmrLgUn4VcsxDIIhJxMA54Aoh/SnmOwLzxPzzn t7OQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=vQ8Ze/LiBPsSRyoNojdzzyfkdJdVp8HWKVdkq2snkAw=; b=hcF1cyJ42wUs4OVUYlgbK9L6wamVYYbPPOrFHNL+Oa64pGgB7IsYiqOch8flVtFSey wsPmazRUNS78mYe8HtxQ+RoLaF8YQK+9Y7bQ3ZIn0a3l1V8wSJg+6xE7BHYFSDk6B3+/ qPfeX0KWqd6NkTUnAyxHtvashLVqfA5Ub6gtfN+hUZue7wEJ+KfxD3vVeJKlSTfBghLx MFoGOK7w0R3dHCsKDFmJ41P5+5qujXl3D/qZ9o0wXxnOoiOVdtoD7dM/5sNXX23vdQl8 qctg77Whi5GrGuEEJ+0VHzkc3oIa6Cm2RZU6mQN4I2atXYWsGFk6WO1KEsCb/oaxHQgT xjIQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k23si3612843edx.21.2020.08.06.11.51.07; Thu, 06 Aug 2020 11:51:29 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728253AbgHFStM (ORCPT + 99 others); Thu, 6 Aug 2020 14:49:12 -0400 Received: from mx2.suse.de ([195.135.220.15]:43418 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727884AbgHFStK (ORCPT ); Thu, 6 Aug 2020 14:49:10 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id BC128ACA7; Thu, 6 Aug 2020 18:49:25 +0000 (UTC) From: Nicolas Saenz Julienne To: amit.pundir@linaro.org, hch@lst.de, linux-kernel@vger.kernel.org, Marek Szyprowski , Robin Murphy Cc: rientjes@google.com, jeremy.linton@arm.com, linux-rpi-kernel@lists.infradead.org, Nicolas Saenz Julienne , iommu@lists.linux-foundation.org Subject: [PATCH v3 2/2] dma-pool: Only allocate from CMA when in same memory zone Date: Thu, 6 Aug 2020 20:47:55 +0200 Message-Id: <20200806184756.32075-3-nsaenzjulienne@suse.de> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200806184756.32075-1-nsaenzjulienne@suse.de> References: <20200806184756.32075-1-nsaenzjulienne@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There is no guarantee to CMA's placement, so allocating a zone specific atomic pool from CMA might return memory from a completely different memory zone. To get around this double check CMA's placement before allocating from it. Signed-off-by: Nicolas Saenz Julienne [hch: rebased, added a fallback to the page allocator, allow dipping into lower CMA pools] Signed-off-by: Christoph Hellwig --- Changes since v2: - Go back to v1 behavior kernel/dma/pool.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c index 5d071d4a3cba..30d28d761490 100644 --- a/kernel/dma/pool.c +++ b/kernel/dma/pool.c @@ -3,7 +3,9 @@ * Copyright (C) 2012 ARM Ltd. * Copyright (C) 2020 Google LLC */ +#include #include +#include #include #include #include @@ -55,6 +57,29 @@ static void dma_atomic_pool_size_add(gfp_t gfp, size_t size) pool_size_kernel += size; } +static bool cma_in_zone(gfp_t gfp) +{ + unsigned long size; + phys_addr_t end; + struct cma *cma; + + cma = dev_get_cma_area(NULL); + if (!cma) + return false; + + size = cma_get_size(cma); + if (!size) + return false; + + /* CMA can't cross zone boundaries, see cma_activate_area() */ + end = cma_get_base(cma) - memblock_start_of_DRAM() + size - 1; + if (IS_ENABLED(CONFIG_ZONE_DMA) && (gfp & GFP_DMA)) + return end <= DMA_BIT_MASK(zone_dma_bits); + if (IS_ENABLED(CONFIG_ZONE_DMA32) && (gfp & GFP_DMA32)) + return end <= DMA_BIT_MASK(32); + return true; +} + static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, gfp_t gfp) { @@ -68,7 +93,11 @@ static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, do { pool_size = 1 << (PAGE_SHIFT + order); - page = alloc_pages(gfp, order); + if (cma_in_zone(gfp)) + page = dma_alloc_from_contiguous(NULL, 1 << order, + order, false); + if (!page) + page = alloc_pages(gfp, order); } while (!page && order-- > 0); if (!page) goto out; -- 2.28.0