From mboxrd@z Thu Jan 1 00:00:00 1970 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 B03C2C25B0E for ; Wed, 17 Aug 2022 02:03:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A2928D0001; Tue, 16 Aug 2022 22:03:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 352646B0074; Tue, 16 Aug 2022 22:03:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 219CA8D0001; Tue, 16 Aug 2022 22:03:11 -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 11B4A6B0073 for ; Tue, 16 Aug 2022 22:03:11 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E749812075C for ; Wed, 17 Aug 2022 02:03:10 +0000 (UTC) X-FDA: 79807436940.16.BBCB7E5 Received: from out30-57.freemail.mail.aliyun.com (out30-57.freemail.mail.aliyun.com [115.124.30.57]) by imf20.hostedemail.com (Postfix) with ESMTP id 80F3B1C0079 for ; Wed, 17 Aug 2022 02:03:09 +0000 (UTC) X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R151e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046051;MF=liusong@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0VMT7Q2R_1660701783; Received: from 30.178.80.164(mailfrom:liusong@linux.alibaba.com fp:SMTPD_---0VMT7Q2R_1660701783) by smtp.aliyun-inc.com; Wed, 17 Aug 2022 10:03:04 +0800 Message-ID: <40c60196-b30c-359f-e1a2-171a6be45296@linux.alibaba.com> Date: Wed, 17 Aug 2022 10:03:02 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.0.3 Subject: Re: [PATCH v2] mm/dmapool.c: avoid duplicate memset within dma_pool_alloc To: Robin Murphy , Marek Szyprowski , akpm@linux-foundation.org, "linux-arm-kernel@lists.infradead.org" , Christoph Hellwig Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" References: <1658125690-76930-1-git-send-email-liusong@linux.alibaba.com> <1dbe63ff-5575-745b-653a-a992ae53e1aa@samsung.com> <413d8666-7a82-efd7-6716-13658016ca10@arm.com> From: Liu Song In-Reply-To: <413d8666-7a82-efd7-6716-13658016ca10@arm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660701790; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3g5hdS8LcuP+dJ2vaeyPdfkepk76qsORcTUnck7qlRo=; b=b/R207nkZnfECDz1W7I2tWAlLdxRMfMus0dA6WSUmW+1Kw+Z3QpZs349fjCA2He4Fb8nK/ J0XJv12NIr2qk67DzeKwvrE3gdxsUB5u5RxZVTzV4iTLcYns2bGPvUOKQeVVEnmJreoQuS L2VBofj2TyA47Fz9y5NJ4muEJ5FcE3o= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of liusong@linux.alibaba.com designates 115.124.30.57 as permitted sender) smtp.mailfrom=liusong@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660701790; a=rsa-sha256; cv=none; b=EPeR/qHm0I4l5ji7ZUfUBg9ulpUENPDvB+GMHMgSBCouQDw4A7z8MDcgf8A6yGQMtKFXlD Obqr9LOekYbDEKfG+sMbTAY3sNBLx19Ahv3gPeRBhAHgTOdRzTrPyhawI2l3uBDiIRPvOm EMEo07ek+Q89yYOHPwRfZk2nGGH7PAs= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 80F3B1C0079 X-Rspam-User: Authentication-Results: imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of liusong@linux.alibaba.com designates 115.124.30.57 as permitted sender) smtp.mailfrom=liusong@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com X-Stat-Signature: uxhx5tnfpw6fqom868f3nzzid7ts9wrd X-HE-Tag: 1660701789-476428 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: > On 2022-08-16 13:39, Marek Szyprowski wrote: >> Hi, >> >> On 18.07.2022 08:28, Liu Song wrote: >>> 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 >> >> This patch landed linux next-20220816. Unfortunately it causes serious >> issues on ARM 32bit systems. I've observed it on ARM 32bit Samsung >> Exynos 5422 based Odroid XU4 board with USB r8152 driver. After applying >> this patch and loading r8152 driver I only the following endless >> messages in the log: >> >> xhci-hcd xhci-hcd.9.auto: WARN Event TRB for slot 1 ep 0 with no TDs >> queued? >> xhci-hcd xhci-hcd.9.auto: WARN Event TRB for slot 1 ep 0 with no TDs >> queued? >> xhci-hcd xhci-hcd.9.auto: WARN Event TRB for slot 1 ep 0 with no TDs >> queued? >> >> It looks that there are drivers which rely on the fact that the dma >> coherent buffers are always zeroed. > > It's not even that, the change here is just obviously broken, since it > ends up entirely ignoring want_init_on_alloc() for devices using > dma-direct. Sure, the memory backing a dma_page is zeroed *once* by > its initial dma-coherent allocation, but who says we're not not > reallocating pool entries from an existing dma_page? > > I'm not convinced it's worth trying to special-case this at all, since > we can only do it reliably for the first pool entry allocated from a > new dma_page, and that will only happen as the pool initially grows to > a suitable size for its working set, after which no further new pages > are likely to be allocated for the lifetime of the pool. Even if there > is a case to be made for doing so, it would need to be based on the > flow through dma_pool_alloc() itself, not some nonsense heuristic on > the device. Hi, First of all, I am very sorry that there are missing branches that have not been considered fully, but there is a possibility that the memset to 0 will be repeated whether re-allocation from the dma pool, and this patch needs to be fixed. Thanks > > Andrew, please drop this patch. > > Thanks, > Robin. > >>> --- >>>    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; >> >> Best regards