From patchwork Fri Nov 30 11:14:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Garry X-Patchwork-Id: 152516 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp3525862ljp; Fri, 30 Nov 2018 03:13:13 -0800 (PST) X-Google-Smtp-Source: AFSGD/WbpmNtSEIiros5E49h5yI6XBcyZZAWv5tJ2/Gnciv0i55A2VdHyiq5rvh5OPTUpfjNZ8gT X-Received: by 2002:a17:902:5066:: with SMTP id f35mr5342227plh.78.1543576393842; Fri, 30 Nov 2018 03:13:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543576393; cv=none; d=google.com; s=arc-20160816; b=dr5R6xw8L6iEEyabKsIYjFosnjYBFRB2PZf5fnk+3b4JHDrbonP5AUgtJUsxQXwjWs sE0Ed/F2g3pKhLIxymwldIrgjZ7w4bNXFZCxXxKoxMPEFWAgu11V5S9BDvYK5UEoh9Z9 5ZfAHfSZp68tIzNlbqwPtJXePgcvZjNkTbV8lxJazUO5kpseyTi9a/YYv9kWsguzamcw P9grq7VVBzph1U3cxEF8MPnxMsa4eWpq9BSW3eo+dLhOGZNXoa3y+0IKnn+FyJFeEcQw ucmsyJbJWoCzDgSwlgHyRI/pIuTGwCnIvxEaZj+f3Yf8tHwJM+2Sh2wTvV8W5PHKTsVk etzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=zrqskRqzTSr7Du1342W9fpsvlxLsJRFZzZ3cxBuGEBA=; b=MnIoowYuqAvVTBAFuGkJzu6RhAvop4Z5Btil4UujFk4aRsz83OuuvwvgRArIsu5XzO di1s7BVYTeSJcp3mCZ3+QKcr7qPwBCNIVkJf6imuEpluzIm2+PhZv4fKKg12H6Bo5fvX bz7YfQQY/eCrApDKnjXxzqED2YWtzZZsW8KqmkSLJFD6dak6n4Ny7CzJPAqRuotqITGy nDY4D5Fkerjwqz6yuLKGbYkrEvDyMvasdNANOvcbXxCYd1rEWEvdO9Pc03LLcTvpptmN o9NbSJ5ULmEl2bF47+wVOWl51llZZteq0QvMk1lFX/VK+9ZKTiMPGd1CQJo46UMzVG+B YjTg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b3si4627872pgc.587.2018.11.30.03.13.13; Fri, 30 Nov 2018 03:13:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726889AbeK3WWH (ORCPT + 32 others); Fri, 30 Nov 2018 17:22:07 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:15617 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726473AbeK3WWH (ORCPT ); Fri, 30 Nov 2018 17:22:07 -0500 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 8C27918376F80; Fri, 30 Nov 2018 19:13:07 +0800 (CST) Received: from localhost.localdomain (10.67.212.75) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.408.0; Fri, 30 Nov 2018 19:12:59 +0800 From: John Garry To: CC: , , , , , , , , John Garry Subject: [PATCH v4] iommu/dma: Use NUMA aware memory allocations in __iommu_dma_alloc_pages() Date: Fri, 30 Nov 2018 19:14:00 +0800 Message-ID: <1543576440-222098-1-git-send-email-john.garry@huawei.com> X-Mailer: git-send-email 2.8.1 MIME-Version: 1.0 X-Originating-IP: [10.67.212.75] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ganapatrao Kulkarni Change function __iommu_dma_alloc_pages() to allocate pages for DMA from respective device NUMA node. The ternary operator which would be for alloc_pages_node() is tidied along with this. The motivation for this change is to have a policy for page allocation consistent with direct DMA mapping, which attempts to allocate pages local to the device, as mentioned in [1]. In addition, for certain workloads it has been observed a marginal performance improvement. The patch caused an observation of 0.9% average throughput improvement for running tcrypt with HiSilicon crypto engine. We also include a modification to use kvzalloc() for kzalloc()/vzalloc() combination. [1] https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1692998.html Signed-off-by: Ganapatrao Kulkarni [JPG: Added kvzalloc(), drop pages ** being device local, remove ternary operator, update message] Signed-off-by: John Garry --- Difference: v1->v2: - Add Ganapatrao's tag and change author v2->v3: - removed ternary operator - stopped making pages ** allocation local to device v3->v4: - Update commit message to include motivation for patch, including headline performance improvement for test. Some notes: This patch was originally posted by Ganapatrao in [2]. However, after initial review, it was never reposted (due to lack of cycles, I think). In addition, the functionality in its sibling patches were merged through patches, as mentioned in [2]; this also refers to a discussion on device local allocations vs CPU local allocations for DMA pool, and which is better [1]. However, as mentioned in [1], dma_alloc_coherent() uses the locality information from the device - as in direct DMA - so this patch is just applying this same policy. And from some testing, mentioned in commit message, shows marginal improvement. [2] https://lore.kernel.org/patchwork/patch/833004/ [3] https://lkml.org/lkml/2018/8/22/391 -- 1.9.1 Reviewed-by: Robin Murphy diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index d1b0475..4afb1a8 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -449,20 +449,17 @@ static void __iommu_dma_free_pages(struct page **pages, int count) kvfree(pages); } -static struct page **__iommu_dma_alloc_pages(unsigned int count, - unsigned long order_mask, gfp_t gfp) +static struct page **__iommu_dma_alloc_pages(struct device *dev, + unsigned int count, unsigned long order_mask, gfp_t gfp) { struct page **pages; - unsigned int i = 0, array_size = count * sizeof(*pages); + unsigned int i = 0, nid = dev_to_node(dev); order_mask &= (2U << MAX_ORDER) - 1; if (!order_mask) return NULL; - if (array_size <= PAGE_SIZE) - pages = kzalloc(array_size, GFP_KERNEL); - else - pages = vzalloc(array_size); + pages = kvzalloc(count * sizeof(*pages), GFP_KERNEL); if (!pages) return NULL; @@ -481,10 +478,12 @@ static struct page **__iommu_dma_alloc_pages(unsigned int count, for (order_mask &= (2U << __fls(count)) - 1; order_mask; order_mask &= ~order_size) { unsigned int order = __fls(order_mask); + gfp_t alloc_flags = gfp; order_size = 1U << order; - page = alloc_pages((order_mask - order_size) ? - gfp | __GFP_NORETRY : gfp, order); + if (order_mask > order_size) + alloc_flags |= __GFP_NORETRY; + page = alloc_pages_node(nid, alloc_flags, order); if (!page) continue; if (!order) @@ -569,7 +568,8 @@ struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp, alloc_sizes = min_size; count = PAGE_ALIGN(size) >> PAGE_SHIFT; - pages = __iommu_dma_alloc_pages(count, alloc_sizes >> PAGE_SHIFT, gfp); + pages = __iommu_dma_alloc_pages(dev, count, alloc_sizes >> PAGE_SHIFT, + gfp); if (!pages) return NULL;