Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp994871ybt; Tue, 7 Jul 2020 05:28:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzieIh9L9JJ/qySDzk7AXmSP35tydb1KbAv7MIyGlN33GWdZ7AY3GvGiv7Zsk7xIhIQfMDu X-Received: by 2002:aa7:dd14:: with SMTP id i20mr18402432edv.41.1594124925131; Tue, 07 Jul 2020 05:28:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594124925; cv=none; d=google.com; s=arc-20160816; b=qQoGhnCLYnnC1Fc6U/TMyaNjR1i+T5QZR4IsSR+mAzwj/rNpklS7AZjF3an2DEzbyT NH7GHMVQwyvPAcnUyH+ZM9RzplOIe5mB7mihcFtWhFgp9JlA9F3SkNdS+oktxjU0bbvb +OR7pIO0KsnKhVIRbw1iJ1Q4feqo4SJA3QrTNnuqg6hd/rlWRWmsKhEBj/WW5fdsnTl6 JNObjUhG18FAYgOoi26y3qmX9554CpDN9W33iKsQZ7uU6HUKtwey+5VabbOhULxkGaqG 0QgGOerpZjcK+XrVJyOWcsH1+WsBEn6sBaenMQbcDyoBUWj/+no0fKNVuXbYiLS751i5 Hhgg== 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 :message-id:date:subject:cc:to:from; bh=gRyRIfKCo/roC+CGTFlrH2bpwrIxQq61vrca4NBJZGA=; b=xDnV225aSECFC9AyOqT3n34MX42eZl6QWB03RioMH/2ZKSRlIlm/60UIaOmG+rp/kP 4hiN+hCczY4YgS0UrQno4bN6ZPP8oo7l/thq9yGD/lL8v4PmaALguM2/XVeeJRkIODMr ojt8T8RudSNw5wLOJ9KKnzeEJghN0my2HwEtGmG2dfuZCYDdUe7sRz6WxAfFCA9Eo4+P D26B+m1w5SxctYTfeQqcZOW53pjABCgQR/BJzMBeu5B/fCrjlUbHS1PR8bav+j2WCTgg lJX9hytyt2okgGuwsmZxCM6QKw8ZGKmMv5VJb2xaIwqfx73JJ0G8a/jQafSKAbRuRKoX 4lkg== 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 u25si14825944edq.436.2020.07.07.05.28.21; Tue, 07 Jul 2020 05:28:45 -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 S1727090AbgGGM2P (ORCPT + 99 others); Tue, 7 Jul 2020 08:28:15 -0400 Received: from mx2.suse.de ([195.135.220.15]:48080 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726540AbgGGM2P (ORCPT ); Tue, 7 Jul 2020 08:28:15 -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 34EDBABF4; Tue, 7 Jul 2020 12:28:14 +0000 (UTC) From: Nicolas Saenz Julienne To: Christoph Hellwig , Marek Szyprowski , Robin Murphy , David Rientjes Cc: linux-rpi-kernel@lists.infradead.org, jeremy.linton@arm.com, Nicolas Saenz Julienne , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [PATCH] dma-pool: use single atomic pool for both DMA zones Date: Tue, 7 Jul 2020 14:28:04 +0200 Message-Id: <20200707122804.21262-1-nsaenzjulienne@suse.de> X-Mailer: git-send-email 2.27.0 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 When allocating atomic DMA memory for a device, the dma-pool core queries __dma_direct_optimal_gfp_mask() to check which atomic pool to use. It turns out the GFP flag returned is only an optimistic guess. The pool selected might sometimes live in a zone higher than the device's view of memory. As there isn't a way to grantee a mapping between a device's DMA constraints and correct GFP flags this unifies both DMA atomic pools. The resulting pool is allocated in the lower DMA zone available, if any, so as for devices to always get accessible memory while having the flexibility of using dma_pool_kernel for the non constrained ones. Fixes: c84dc6e68a1d ("dma-pool: add additional coherent pools to map to gfp mask") Reported-by: Jeremy Linton Suggested-by: Robin Murphy Signed-off-by: Nicolas Saenz Julienne --- kernel/dma/pool.c | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c index 8cfa01243ed2..883f7a583969 100644 --- a/kernel/dma/pool.c +++ b/kernel/dma/pool.c @@ -13,10 +13,11 @@ #include #include +#define GFP_ATOMIC_POOL_DMA (IS_ENABLED(CONFIG_ZONE_DMA) ? GFP_DMA : \ + IS_ENABLED(CONFIG_ZONE_DMA32) ? GFP_DMA32 : 0) + static struct gen_pool *atomic_pool_dma __ro_after_init; static unsigned long pool_size_dma; -static struct gen_pool *atomic_pool_dma32 __ro_after_init; -static unsigned long pool_size_dma32; static struct gen_pool *atomic_pool_kernel __ro_after_init; static unsigned long pool_size_kernel; @@ -42,16 +43,13 @@ static void __init dma_atomic_pool_debugfs_init(void) return; debugfs_create_ulong("pool_size_dma", 0400, root, &pool_size_dma); - debugfs_create_ulong("pool_size_dma32", 0400, root, &pool_size_dma32); debugfs_create_ulong("pool_size_kernel", 0400, root, &pool_size_kernel); } static void dma_atomic_pool_size_add(gfp_t gfp, size_t size) { - if (gfp & __GFP_DMA) + if (gfp & GFP_ATOMIC_POOL_DMA) pool_size_dma += size; - else if (gfp & __GFP_DMA32) - pool_size_dma32 += size; else pool_size_kernel += size; } @@ -132,12 +130,11 @@ static void atomic_pool_resize(struct gen_pool *pool, gfp_t gfp) static void atomic_pool_work_fn(struct work_struct *work) { - if (IS_ENABLED(CONFIG_ZONE_DMA)) - atomic_pool_resize(atomic_pool_dma, - GFP_KERNEL | GFP_DMA); - if (IS_ENABLED(CONFIG_ZONE_DMA32)) - atomic_pool_resize(atomic_pool_dma32, - GFP_KERNEL | GFP_DMA32); + gfp_t dma_gfp = GFP_ATOMIC_POOL_DMA; + + if (dma_gfp) + atomic_pool_resize(atomic_pool_dma, GFP_KERNEL | dma_gfp); + atomic_pool_resize(atomic_pool_kernel, GFP_KERNEL); } @@ -168,6 +165,7 @@ static __init struct gen_pool *__dma_atomic_pool_init(size_t pool_size, static int __init dma_atomic_pool_init(void) { + gfp_t dma_gfp = GFP_ATOMIC_POOL_DMA; int ret = 0; /* @@ -185,18 +183,13 @@ static int __init dma_atomic_pool_init(void) GFP_KERNEL); if (!atomic_pool_kernel) ret = -ENOMEM; - if (IS_ENABLED(CONFIG_ZONE_DMA)) { + + if (dma_gfp) { atomic_pool_dma = __dma_atomic_pool_init(atomic_pool_size, - GFP_KERNEL | GFP_DMA); + GFP_KERNEL | dma_gfp); if (!atomic_pool_dma) ret = -ENOMEM; } - if (IS_ENABLED(CONFIG_ZONE_DMA32)) { - atomic_pool_dma32 = __dma_atomic_pool_init(atomic_pool_size, - GFP_KERNEL | GFP_DMA32); - if (!atomic_pool_dma32) - ret = -ENOMEM; - } dma_atomic_pool_debugfs_init(); return ret; @@ -206,14 +199,12 @@ postcore_initcall(dma_atomic_pool_init); static inline struct gen_pool *dev_to_pool(struct device *dev) { u64 phys_mask; - gfp_t gfp; - - gfp = dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask, - &phys_mask); - if (IS_ENABLED(CONFIG_ZONE_DMA) && gfp == GFP_DMA) - return atomic_pool_dma; - if (IS_ENABLED(CONFIG_ZONE_DMA32) && gfp == GFP_DMA32) - return atomic_pool_dma32; + + if (atomic_pool_dma && + dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask, + &phys_mask)) + return atomic_pool_dma; + return atomic_pool_kernel; } -- 2.27.0