From patchwork Mon Jul 18 06:28:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Song X-Patchwork-Id: 12920764 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C108C43334 for ; Mon, 18 Jul 2022 06:28:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1A2856B008C; Mon, 18 Jul 2022 02:28:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 12AFF6B0092; Mon, 18 Jul 2022 02:28:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F34BB6B0093; Mon, 18 Jul 2022 02:28:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E093C6B008C for ; Mon, 18 Jul 2022 02:28:35 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B425418B for ; Mon, 18 Jul 2022 06:28:35 +0000 (UTC) X-FDA: 79699241790.27.4956302 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) by imf18.hostedemail.com (Postfix) with ESMTP id 359751C007E for ; Mon, 18 Jul 2022 06:28:33 +0000 (UTC) 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 ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of liusong@linux.alibaba.com designates 115.124.30.131 as permitted sender) smtp.mailfrom=liusong@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658125715; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=7zL6R27+Zp+CG5r7K0y6Yd0i5gAmHPHV8Xe/rKxayQU=; b=67OTgLEdRNtHVyGlHVC74UGok1PVUbMUgwQBMp7Rf4p/alHib/HEaj4xnWALlP8JCbQbO6 z8f/ka7oQSeib7XJdNn9efEFJCoFYWY1IJFSk3u8n58vl+fQcq0zUAIUOxMRfZdMXVPOyy z2u8bJ2/XNXtDQC5SW41j5+suZ2kkBs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658125715; a=rsa-sha256; cv=none; b=ESXWNA8FH7gf0NvvdY0cEwkXOBjvlEJcXggnY6F35iJZ5O+dRSdhU4zL0vxCY9KKdbynpp c2KfP6SgIS/DQsYBML1Zp94ttIZIQhi9qhd9H10lgGcCcTOgSjdT5AQmSKnMW4t0h1m26W JsrzO6kwv7i2uYuN/kkQG46/oXLo7U8= X-Rspamd-Queue-Id: 359751C007E X-Rspamd-Server: rspam04 Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of liusong@linux.alibaba.com designates 115.124.30.131 as permitted sender) smtp.mailfrom=liusong@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com X-Rspam-User: X-Stat-Signature: esxfd61gsq17cr37ne7zx73eiycewigk X-HE-Tag: 1658125713-916022 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: 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, ssize_t size, int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr); 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 device *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 */ #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 @@ #include #include +#include #include #include #include @@ -372,7 +373,9 @@ void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags, #endif spin_unlock_irqrestore(&pool->lock, flags); - 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); return retval;