Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2172102pxa; Mon, 3 Aug 2020 09:11:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy1wJRkUyPp7X7+2vixpC5cQ2QGGUKIjilYbj4ebVO+yb2zmPniCOMj5rL/DFGdqeMRxkf8 X-Received: by 2002:a17:906:fc10:: with SMTP id ov16mr17983999ejb.171.1596471101294; Mon, 03 Aug 2020 09:11:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596471101; cv=none; d=google.com; s=arc-20160816; b=nHj5RcNREz+0WMGIYLdYxqUGlwGCM58e9TE6pWTxIg0EXo1ZJcLrwxTIWQ/yNxIxsh qId3oXN5/0OSHYD0JrJJDRK6LZ+JqqIgWRWTrzY/cyhnR0jqamGM815FZJrezx1nPHx1 BIKyUlDvi9qvwt21+6y5Leke3lSp7tD1OSTg0Qr+8P/F+QQaNOj/7kiF89GblzDdcU99 y/C2AyOSt6pSrezng2IvcJFbiGr/NO+g6oL/saYr7S0eYT3liyI5JycchTyacu9yOw/0 Qk2UOOvlr8isGQIQay8/kDvBJzUEIHPgbD6kbCtaFp+pHFvvJ02u1kcJ6m8WpxF0plab 7i9w== 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=s94Am3ehMjxz6Bos04gl6s0sSok8xrQ68Gh9iOnS+uA=; b=ppltpC2sO4IKP2/2VYcRADvk7RhbGnAPeOBe89PFrHON3dy6F+GhBFOSWK6GS2sRbZ FRkgfOdXzrdHgE04ei7UY3srAFIOVKiSkwVjeEd1Ue9xobySrljfepPyIhwyRnZaxrsE i+QNOnsAIE99mauMQB2UAJJeM3qoQVjRycvRoCaDT9+MYIIhcASihQ0kui7tnYjX6UbP wMb6W4uzQnbU+na2wDcIyoQCiMJTWaLMaJlfUX1c8JlcYwwUxUZ+DByzwBFzKu15CrDk q6t/QZSQhQoJUIp8ZgDG9l/M5PH11btl4+lCFw/bCjnaNKCT80qwKTON9qL3uZy3zaIs q+ag== 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 sb27si10611272ejb.188.2020.08.03.09.11.18; Mon, 03 Aug 2020 09:11:41 -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 S1727964AbgHCQKH (ORCPT + 99 others); Mon, 3 Aug 2020 12:10:07 -0400 Received: from mx2.suse.de ([195.135.220.15]:50726 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727930AbgHCQKG (ORCPT ); Mon, 3 Aug 2020 12:10:06 -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 245B1B752; Mon, 3 Aug 2020 16:10:20 +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 v2 2/2] dma-pool: Only allocate from CMA when in same memory zone Date: Mon, 3 Aug 2020 18:09:56 +0200 Message-Id: <20200803160956.19235-3-nsaenzjulienne@suse.de> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200803160956.19235-1-nsaenzjulienne@suse.de> References: <20200803160956.19235-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 --- Changes since v1: - Make cma_in_zone() more strict, GFP_KERNEL doesn't default to true now kernel/dma/pool.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c index 5d071d4a3cba..582523ccf4fe 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,32 @@ 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); + if (gfp & GFP_KERNEL) + return end > DMA_BIT_MASK(32); + + return false; +} + static int atomic_pool_expand(struct gen_pool *pool, size_t pool_size, gfp_t gfp) { @@ -68,7 +96,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