From patchwork Tue Dec 4 08:23:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Boichat X-Patchwork-Id: 10711255 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 35A6616B1 for ; Tue, 4 Dec 2018 08:23:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 291912ABF6 for ; Tue, 4 Dec 2018 08:23:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1D65B2AC17; Tue, 4 Dec 2018 08:23:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D6C512ABE4 for ; Tue, 4 Dec 2018 08:23:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D0B6C6B6DB5; Tue, 4 Dec 2018 03:23:29 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id CB9156B6DBC; Tue, 4 Dec 2018 03:23:29 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA79D6B6DBD; Tue, 4 Dec 2018 03:23:29 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) by kanga.kvack.org (Postfix) with ESMTP id 789146B6DB5 for ; Tue, 4 Dec 2018 03:23:29 -0500 (EST) Received: by mail-pl1-f199.google.com with SMTP id a10so12023243plp.14 for ; Tue, 04 Dec 2018 00:23:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:mime-version:content-transfer-encoding; bh=BC2yXIgwrgbb+vp2SoDp3a5EYKg+hZ4W2JytOoK4K4A=; b=D9/avOUIBrFCuhPSnr3Me5Oo+Xvld0xVZEKZt0S2Qp0fcmD51w2eMj4I63kjeuChSH AOOzQjmNuynqAiI3OyJkDiya5cCHkcdhyeMltU+6+2NK7+rWYHZmFJyj3n/ZAk1Y6DhF wYYj7fD+DzHDcUJp+f/i0i22MmT4lcD1D8ZaJRuCMT02OZGhtVSvk0FQ6CqfhgkyY5Uv qPZ9sOsTc5APm0P2BAeR/bMXVRY9sFroKDe+M4HhOwpwPomGxB4rEyUoj/qWK5eXcvJy zj68fudcXoOfST92iZR/e3IIpmDyfvct8kDwCI7q+SSx9SyDgf4mLbZvU26+9Q9qKozM qftg== X-Gm-Message-State: AA+aEWbR+m0WaYbjQfVEoITV7WKK35MJ1xYAIo8G1hUGSN0pdoHjgp5m VcMU6GVdQ8eeLMXc4eHvxqAhB8MdLIMfF0akwDzBuvcCrrfOvavZ3jOo47NmLzC8yLvJqhVMr6+ upnC3SxvrQotdsrgHPRRSlc5N+X5cscxbIUqGd+wwGO6twBeLYpw3adhCFtQNSMT33XoaQuwEe6 6dkd4fykLllF/tB8asrY4TfCdDfNOv5/ZtZFMvqwvzb6Bx4fsgwAuaNmQD9XX3k/tV70BFlQBmL yhmlgHHpUXg62aqRL5OrGggYbrvAd50lkWmkD9hQuDzGH5N9BeXjwE52iy3looaDrxWsb4REl3y Vzj0VU+bq2TtUEezrG50sj55vmUg3ax1oz1KZwRhCtA3aQnsps2+/XDkSoJ+y9G4jg6xdWUP5Pq H X-Received: by 2002:a63:c42:: with SMTP id 2mr14880136pgm.372.1543911809011; Tue, 04 Dec 2018 00:23:29 -0800 (PST) X-Received: by 2002:a63:c42:: with SMTP id 2mr14880107pgm.372.1543911807830; Tue, 04 Dec 2018 00:23:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543911807; cv=none; d=google.com; s=arc-20160816; b=ZS0yzairzdfCNuq2hIjKnWkmWvstbfTs4RO3obcDiUgXePzxLhHWNvadW7grkYmEsY tDdQ8CPl7ruCms/sj+lVDWNyYHvtoL5fjxFCwrTIIanv/IKrlsaEXi3M2LvZnrdASrD/ 0DZ6QfbE7OT0yw/KP7gO4yNncW1MFkrZF0jRJVOMQa+9mhx5ngrOFqZ7FzLuhbzThaOH c+BRcZ+IOvDhixuF1lSMlgfi21Pjqf+x6iRuB/KvqLqOvQwUkrpRqYhXXqO61b0J0Pyb nusckLOXgXjbQXlQ3BF1xG6NO6lSChSrqgM2mPKzVpbzHveLsQDcaYxoKhVD33OHWVAs uKTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=BC2yXIgwrgbb+vp2SoDp3a5EYKg+hZ4W2JytOoK4K4A=; b=NfhBUS6QM2GfOjD6AM83ZnAbilVyyVob5MWpvgT0NzT0tnIusfVUXKTtE9u52PmyMg AHKSgvlsqiMFXKPUOZRI20qYadOkT+yDfXh8uHQhdKczj+M2d1vpL6L98Vnw0xQQQMlo cGlvyQs2yUFCKP/SKW55hApJf9dUlUAkBxkF5UP5gdTeGtGPv+doeKdG+TFa544scASX OESM+TrUs9RZ5zNQdPgPt5Eml8uddaFKEdRs00MasNX9a0xL45ahViRXtxOyA1AJP4Se bf9WC5aCmrluEQp5uADaj8sRFM6yvfCF8Q8xp2f3pKZbJyk1LBFv4+V6oAKt57gVLdNb CN+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TIjArO+k; spf=pass (google.com: domain of drinkcat@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=drinkcat@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id q77sor22845524pfi.33.2018.12.04.00.23.27 for (Google Transport Security); Tue, 04 Dec 2018 00:23:27 -0800 (PST) Received-SPF: pass (google.com: domain of drinkcat@chromium.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TIjArO+k; spf=pass (google.com: domain of drinkcat@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=drinkcat@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=BC2yXIgwrgbb+vp2SoDp3a5EYKg+hZ4W2JytOoK4K4A=; b=TIjArO+kHpXg+DvsilBuxqhzyDTY0F589DL9ubB2vO/d22duhswOs4v1PKAYnn3RID QDXhFfuefgwzAaYIO51TU+12aJAh7kSt/oge38mwE2FktiwNt6CouZE9kec7ZQpDkong P/9qkf/AYCBd5sZFs/zAZWHRF2jL4PgO8ZF2A= X-Google-Smtp-Source: AFSGD/VpTQsU4DiTZ31xNf7o/qoPM2bSEhhvzRB/WoSeJt9tmOMRs+2XyBTMAtmWqbQpg1/GbsZAbw== X-Received: by 2002:a62:c613:: with SMTP id m19mr19435575pfg.207.1543911807270; Tue, 04 Dec 2018 00:23:27 -0800 (PST) Received: from drinkcat2.tpe.corp.google.com ([2401:fa00:1:b:f659:7f17:ea11:4e8e]) by smtp.gmail.com with ESMTPSA id y6sm44911418pfd.104.2018.12.04.00.23.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Dec 2018 00:23:26 -0800 (PST) From: Nicolas Boichat To: Robin Murphy Cc: Will Deacon , Joerg Roedel , linux-arm-kernel@lists.infradead.org, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Christoph Lameter , Vlastimil Babka , Michal Hocko , linux-mm@kvack.org, Yong Wu , Matthias Brugger , Tomasz Figa , yingjoe.chen@mediatek.com, hch@infradead.org, Matthew Wilcox Subject: [PATCH v3, RFC] iommu/io-pgtable-arm-v7s: Use page_frag to request DMA32 memory Date: Tue, 4 Dec 2018 16:23:00 +0800 Message-Id: <20181204082300.95106-1-drinkcat@chromium.org> X-Mailer: git-send-email 2.20.0.rc1.387.gf8505762e3-goog MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP IOMMUs using ARMv7 short-descriptor format require page tables (level 1 and 2) to be allocated within the first 4GB of RAM, even on 64-bit systems. For level 1/2 tables, ensure GFP_DMA32 is used if CONFIG_ZONE_DMA32 is defined (e.g. on arm64 platforms). For level 2 tables (1 KB), we use page_frag to allocate these pages, as we cannot directly use kmalloc (no slab cache for GFP_DMA32) or kmem_cache (mm/ code treats GFP_DMA32 as an invalid flag). One downside is that we only free the allocated page if all the 4 fragments (4 IOMMU L2 tables) are freed, but given that we usually only allocate limited number of IOMMU L2 tables, this should not have too much impact on memory usage: In the absolute worst case (4096 L2 page tables, each on their own 4K page), we would use 16 MB of memory for 4 MB of L2 tables. Also, print an error when the physical address does not fit in 32-bit, to make debugging easier in the future. Fixes: ad67f5a6545f ("arm64: replace ZONE_DMA with ZONE_DMA32") Signed-off-by: Nicolas Boichat Acked-by: Will Deacon --- As an alternative to the series [1], which adds support for GFP_DMA32 to kmem_cache in mm/. IMHO the solution in [1] is cleaner and more efficient, as it allows freed fragments (L2 tables) to be reused, but this approach does not require any core change. [1] https://patchwork.kernel.org/cover/10677529/, 3 patches drivers/iommu/io-pgtable-arm-v7s.c | 32 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/io-pgtable-arm-v7s.c b/drivers/iommu/io-pgtable-arm-v7s.c index 445c3bde04800c..0de6a51eb6755f 100644 --- a/drivers/iommu/io-pgtable-arm-v7s.c +++ b/drivers/iommu/io-pgtable-arm-v7s.c @@ -161,6 +161,12 @@ #define ARM_V7S_TCR_PD1 BIT(5) +#ifdef CONFIG_ZONE_DMA32 +#define ARM_V7S_TABLE_GFP_DMA GFP_DMA32 +#else +#define ARM_V7S_TABLE_GFP_DMA GFP_DMA +#endif + typedef u32 arm_v7s_iopte; static bool selftest_running; @@ -169,7 +175,7 @@ struct arm_v7s_io_pgtable { struct io_pgtable iop; arm_v7s_iopte *pgd; - struct kmem_cache *l2_tables; + struct page_frag_cache l2_tables; spinlock_t split_lock; }; @@ -198,13 +204,17 @@ static void *__arm_v7s_alloc_table(int lvl, gfp_t gfp, void *table = NULL; if (lvl == 1) - table = (void *)__get_dma_pages(__GFP_ZERO, get_order(size)); + table = (void *)__get_free_pages( + __GFP_ZERO | ARM_V7S_TABLE_GFP_DMA, get_order(size)); else if (lvl == 2) - table = kmem_cache_zalloc(data->l2_tables, gfp | GFP_DMA); + table = page_frag_alloc(&data->l2_tables, size, + gfp | __GFP_ZERO | ARM_V7S_TABLE_GFP_DMA); phys = virt_to_phys(table); - if (phys != (arm_v7s_iopte)phys) + if (phys != (arm_v7s_iopte)phys) { /* Doesn't fit in PTE */ + dev_err(dev, "Page table does not fit in PTE: %pa", &phys); goto out_free; + } if (table && !(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) { dma = dma_map_single(dev, table, size, DMA_TO_DEVICE); if (dma_mapping_error(dev, dma)) @@ -227,7 +237,7 @@ static void *__arm_v7s_alloc_table(int lvl, gfp_t gfp, if (lvl == 1) free_pages((unsigned long)table, get_order(size)); else - kmem_cache_free(data->l2_tables, table); + page_frag_free(table); return NULL; } @@ -244,7 +254,7 @@ static void __arm_v7s_free_table(void *table, int lvl, if (lvl == 1) free_pages((unsigned long)table, get_order(size)); else - kmem_cache_free(data->l2_tables, table); + page_frag_free(table); } static void __arm_v7s_pte_sync(arm_v7s_iopte *ptep, int num_entries, @@ -515,7 +525,6 @@ static void arm_v7s_free_pgtable(struct io_pgtable *iop) __arm_v7s_free_table(iopte_deref(pte, 1), 2, data); } __arm_v7s_free_table(data->pgd, 1, data); - kmem_cache_destroy(data->l2_tables); kfree(data); } @@ -729,17 +738,11 @@ static struct io_pgtable *arm_v7s_alloc_pgtable(struct io_pgtable_cfg *cfg, !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS)) return NULL; - data = kmalloc(sizeof(*data), GFP_KERNEL); + data = kzalloc(sizeof(*data), GFP_KERNEL); if (!data) return NULL; spin_lock_init(&data->split_lock); - data->l2_tables = kmem_cache_create("io-pgtable_armv7s_l2", - ARM_V7S_TABLE_SIZE(2), - ARM_V7S_TABLE_SIZE(2), - SLAB_CACHE_DMA, NULL); - if (!data->l2_tables) - goto out_free_data; data->iop.ops = (struct io_pgtable_ops) { .map = arm_v7s_map, @@ -789,7 +792,6 @@ static struct io_pgtable *arm_v7s_alloc_pgtable(struct io_pgtable_cfg *cfg, return &data->iop; out_free_data: - kmem_cache_destroy(data->l2_tables); kfree(data); return NULL; }