Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1718198ybh; Tue, 14 Jul 2020 05:40:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwVkcd2BHbUA4U3BjEU43lBrAM2yOSuFXXO3HkK8B1kKvH+GT/GrB0reHS9Ulw1zJnMeuy X-Received: by 2002:a05:6402:1597:: with SMTP id c23mr4101606edv.243.1594730456874; Tue, 14 Jul 2020 05:40:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594730456; cv=none; d=google.com; s=arc-20160816; b=LD2JlljJIA6jsVDNMdlu/Uynu3dhe4BTQYlnqStVhvjn4JVIYaujnPbnQVRciRwjs8 5u1pEEiMMg9jsc9LmR/TsCrS2gKzcL01T9APTTY8GC9k5l8RJmltBG3QUTGTrnTDl8uL i33OtWdseaPEQcAomEuAh5AtQZ5a9qCUaLkLt+qR3wXCmVRyr1pMyK/vSEUTrQlDF4s5 ty897HutoylOQOWO88UkpkldUOv9U7FyhVLWLSxwgxHdNQwkdo7nTiIqX0IVK5/ewdCe r+m6Uo+lgNIT2Foi0jh7RXN6qzJVAhmigwhhudDgqZFtPtQQZ2wbyUw6wh/JR/sjJiM4 oSqQ== 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=hnN6dKl7I1CA/X6rhmOznrnrjAvkPK0YMMRUFV8E6y0=; b=zKanhXyk0cd1f9s1rYl6cn8MZrY36RelrEN8a0HXfkYXOW/jORgFJJZkelK2BKJdU8 KIuC8jXLulEs5JV4ipfUsiI5yIWKctpjeIXuYquhU3O0uzYJSykv53F2M7GD2+TkkvXI lA6GH/ifKlm8iLf6pvfPz6IqHwAwnoGabrYxJuFJ1YJWvU17dFFcQ3BynFJwSwQv103g uHhn4SbXUeu85iRuZtuac/QdVa/0Qt0DrGA9Bo+hhFD3v9Cs4UCiVEP2nnFV8Ka6PXUV QmFh+N7c5TIQif0+Xt0kpNlIe6ch8+ZVHNXBX9jfRgYHt8D9+QFm44pCz5Y+BQafxLLi 7QoQ== 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 h25si8428541ejx.479.2020.07.14.05.40.33; Tue, 14 Jul 2020 05:40:56 -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 S1728519AbgGNMkH (ORCPT + 99 others); Tue, 14 Jul 2020 08:40:07 -0400 Received: from mx2.suse.de ([195.135.220.15]:54810 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728461AbgGNMkB (ORCPT ); Tue, 14 Jul 2020 08:40:01 -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 B6D5AB02E; Tue, 14 Jul 2020 12:40:02 +0000 (UTC) From: Nicolas Saenz Julienne To: hch@lst.de, linux-kernel@vger.kernel.org, Marek Szyprowski , Robin Murphy Cc: linux-rpi-kernel@lists.infradead.org, jeremy.linton@arm.com, Nicolas Saenz Julienne , iommu@lists.linux-foundation.org Subject: [PATCH v2 3/4] dma-pool: Introduce dma_guess_pool() Date: Tue, 14 Jul 2020 14:39:27 +0200 Message-Id: <20200714123928.8581-4-nsaenzjulienne@suse.de> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714123928.8581-1-nsaenzjulienne@suse.de> References: <20200714123928.8581-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 dma-pool's dev_to_pool() creates the false impression that there is a way to grantee a mapping between a device's DMA constraints and an atomic pool. It tuns out it's just a guess, and the device might need to use an atomic pool containing memory from a 'safer' (or lower) memory zone. To help mitigate this, introduce dma_guess_pool() which can be fed a device's DMA constraints and atomic pools already known to be faulty, in order for it to provide an better guess on which pool to use. Signed-off-by: Nicolas Saenz Julienne --- kernel/dma/pool.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c index 318035e093fb..5b9eaa2b498d 100644 --- a/kernel/dma/pool.c +++ b/kernel/dma/pool.c @@ -203,7 +203,7 @@ static int __init dma_atomic_pool_init(void) } postcore_initcall(dma_atomic_pool_init); -static inline struct gen_pool *dev_to_pool(struct device *dev) +static inline struct gen_pool *dma_guess_pool_from_device(struct device *dev) { u64 phys_mask; gfp_t gfp; @@ -217,10 +217,30 @@ static inline struct gen_pool *dev_to_pool(struct device *dev) return atomic_pool_kernel; } +static inline struct gen_pool *dma_get_safer_pool(struct gen_pool *bad_pool) +{ + if (bad_pool == atomic_pool_kernel) + return atomic_pool_dma32 ? : atomic_pool_dma; + + if (bad_pool == atomic_pool_dma32) + return atomic_pool_dma; + + return NULL; +} + +static inline struct gen_pool *dma_guess_pool(struct device *dev, + struct gen_pool *bad_pool) +{ + if (bad_pool) + return dma_get_safer_pool(bad_pool); + + return dma_guess_pool_from_device(dev); +} + void *dma_alloc_from_pool(struct device *dev, size_t size, struct page **ret_page, gfp_t flags) { - struct gen_pool *pool = dev_to_pool(dev); + struct gen_pool *pool = dma_guess_pool(dev, NULL); unsigned long val; void *ptr = NULL; @@ -249,7 +269,7 @@ void *dma_alloc_from_pool(struct device *dev, size_t size, bool dma_free_from_pool(struct device *dev, void *start, size_t size) { - struct gen_pool *pool = dev_to_pool(dev); + struct gen_pool *pool = dma_guess_pool(dev, NULL); if (!pool || !gen_pool_has_addr(pool, (unsigned long)start, size)) return false; -- 2.27.0