From nobody Sat Apr 18 04:19:34 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E81B7C433EF for ; Mon, 18 Jul 2022 06:28:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233163AbiGRG2h (ORCPT ); Mon, 18 Jul 2022 02:28:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233300AbiGRG2g (ORCPT ); Mon, 18 Jul 2022 02:28:36 -0400 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1245615A3C for ; Sun, 17 Jul 2022 23:28:33 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045168;MF=liusong@linux.alibaba.com;NM=1;PH=DS;RN=3;SR=0;TI=SMTPD_---0VJdbkNA_1658125690; Received: from localhost(mailfrom:liusong@linux.alibaba.com fp:SMTPD_---0VJdbkNA_1658125690) by smtp.aliyun-inc.com; Mon, 18 Jul 2022 14:28:30 +0800 From: Liu Song To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] mm/dmapool.c: avoid duplicate memset within dma_pool_alloc Date: Mon, 18 Jul 2022 14:28:10 +0800 Message-Id: <1658125690-76930-1-git-send-email-liusong@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Liu Song In "dma_alloc_from_dev_coherent" and "dma_direct_alloc", the allocated memory is explicitly set to 0. A helper function "use_dev_coherent_memory" is introduced here to determine whether the memory is allocated by "dma_alloc_from_dev_coherent". And use "get_dma_ops" to determine whether the memory is allocated by "dma_direct_alloc". After this modification, memory allocated using "dma_pool_zalloc" can avoid duplicate memset. Signed-off-by: Liu Song --- include/linux/dma-map-ops.h | 5 +++++ mm/dmapool.c | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 0d5b06b..c29948d 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -171,6 +171,10 @@ int dma_alloc_from_dev_coherent(struct device *dev, ss= ize_t size, int dma_release_from_dev_coherent(struct device *dev, int order, void *vad= dr); int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *= vma, void *cpu_addr, size_t size, int *ret); +static inline bool use_dev_coherent_memory(struct device *dev) +{ + return dev->dma_mem ? true : false; +} #else static inline int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, dma_addr_t device_addr, size_t size) @@ -180,6 +184,7 @@ static inline int dma_declare_coherent_memory(struct de= vice *dev, #define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0) #define dma_release_from_dev_coherent(dev, order, vaddr) (0) #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0) +#define use_dev_coherent_memory(dev) (0) #endif /* CONFIG_DMA_DECLARE_COHERENT */ =20 #ifdef CONFIG_DMA_GLOBAL_POOL diff --git a/mm/dmapool.c b/mm/dmapool.c index a7eb5d0..6e03530 100644 --- a/mm/dmapool.c +++ b/mm/dmapool.c @@ -21,6 +21,7 @@ =20 #include #include +#include #include #include #include @@ -372,7 +373,9 @@ void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_f= lags, #endif spin_unlock_irqrestore(&pool->lock, flags); =20 - if (want_init_on_alloc(mem_flags)) + if (want_init_on_alloc(mem_flags) && + !use_dev_coherent_memory(pool->dev) && + get_dma_ops(pool->dev)) memset(retval, 0, pool->size); =20 return retval; --=20 1.8.3.1