Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3790772rwb; Tue, 16 Aug 2022 08:49:23 -0700 (PDT) X-Google-Smtp-Source: AA6agR6K+Wuj8IFHfLE0YiXGtsHB1TjtSZs92DfI4OdgYsnwYCFOTHeeGzeVZEMlk8pwKVSgLuBk X-Received: by 2002:a05:6402:d05:b0:425:b5c8:faeb with SMTP id eb5-20020a0564020d0500b00425b5c8faebmr18830081edb.273.1660664963608; Tue, 16 Aug 2022 08:49:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660664963; cv=none; d=google.com; s=arc-20160816; b=sl9ipYr6S+sll0L0c/0t7bt0JM8DcxZFvOSo6jhedtvPMWViNxG226IMZ+euVgAhyq wmTrGrHBQEDUbjKL6J0l8YRe3NiXosh+FOMFhMZT2JR68z+Fm17HhdIbmuYPFxuw5Nyn VCWfDrcQr3QNhmBviJkJk98pvAG2UvdKnSsi7CiavcZ+TtBmvXQ54cMaaGeotQWVYGTS yreCDnCEdCwcxu0KMxe1e/oq35b7E7wBZwIbtfKZ8VEEwr6mcOqpnS9mkFhY0XGr4Rpw PGc5+/K1eo1/6TcuA23PhAc7igWkXgJBDCy+lBiyFp5nq73H+9JRISH+eQMZLSMgI+e4 ho0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=zfUT+wu8wBetcvNW8YwFN0mtBgOtLJJKtVc8VJQce2A=; b=kp9L93ezCIbmy7BauDTiJNF5t9KxwEX1YID1qrtlrGlWYjXxiempPU8gSiDILlfsXo knNfsuo0FnsKVNqEBG1p9q3EvAgar2rFiiCrMdZUAY+u7iMLA/NBCGXjyXErob4WoTW8 DShrxdoZaLYGlTMom93EYNMP1dEKugppRVkDbIHX4wx0IMujUgLq6WG+fWKBFVhoci5i UFXBGQspvxx0DPNMQ6TFEoTYLTnqbbXHAndfj100I6pXLgEUOmRsOgmZ1t8p7OXkw6W0 9Xv/JNvSFD2raUoWC4F2FhqUa8v9qWSmOYdDErDasnT/zWzLBrQIiYn+38LzF15d6EaA q87w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gn16-20020a1709070d1000b00730aacadc13si11144718ejc.993.2022.08.16.08.48.57; Tue, 16 Aug 2022 08:49:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234363AbiHPPCh (ORCPT + 99 others); Tue, 16 Aug 2022 11:02:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236046AbiHPPCO (ORCPT ); Tue, 16 Aug 2022 11:02:14 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5094C8670E for ; Tue, 16 Aug 2022 08:00:33 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DF3E51424; Tue, 16 Aug 2022 08:00:33 -0700 (PDT) Received: from [10.57.13.180] (unknown [10.57.13.180]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id AB3EC3F66F; Tue, 16 Aug 2022 08:00:31 -0700 (PDT) Message-ID: <413d8666-7a82-efd7-6716-13658016ca10@arm.com> Date: Tue, 16 Aug 2022 16:00:27 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH v2] mm/dmapool.c: avoid duplicate memset within dma_pool_alloc Content-Language: en-GB To: Marek Szyprowski , Liu Song , 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> From: Robin Murphy In-Reply-To: <1dbe63ff-5575-745b-653a-a992ae53e1aa@samsung.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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