Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4024878rdh; Tue, 28 Nov 2023 09:45:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IGRO/BHxJzpxY3wgMLua2jrDyaJ5BqIdnNikDhFQFUe12fU7Yx1eltnI6nvtpL1gGsPIAMN X-Received: by 2002:a17:902:b282:b0:1cf:b190:ea09 with SMTP id u2-20020a170902b28200b001cfb190ea09mr14604946plr.42.1701193507225; Tue, 28 Nov 2023 09:45:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701193507; cv=none; d=google.com; s=arc-20160816; b=ewWUeARarZlkiHlu6o+86fa4H5o5o/Nn4ToG2pqVxPn5VuX1FL5wgDRL4GM+mkC/lk WnxMrjK+Y6HhJ7YNVMZ5VE+8AxeTfK0e9e3kgR/COjbJEVeebmZ9xY/ZwRgtczsDY7+L FVq8Ly2L5f0dJEI8djP8qBXBB+h0IRiBWFQBprPQ98I0SJ5slxlYwwESZaDN+dPT4iRZ SFLmE5btyJ58igG5+Fds+hmY4WYHs06aAbYZIBJrNzwTDOpAR9DA81ZL67F6Q7HUP+8k K6FrIP2qknTUEgb9rTSLYpVKdU4B0YF0v5e2H/On0OKtL/4SQ4qUSqUuetUzanqSpNCz 8Hrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=zaJxux3dqMDuD448xWUZtAUYxuDGv9ql3IMekGkvTJ4=; fh=lJGsCplz4sVGyyKpjwn/kQESKiVIf1zCSS5DslnBQgo=; b=KF9cJGFOtaWW+ClRyLOW01HjfKOawUZEd3UsI+JvuDxX+liAj0l0ubMRaf/qOxl6Kv LvW7D1/CYzVdltc9gfkLjn+cBdN6wqQyi5mvMDOIAvdYc7OeWe0ei/6iIReJFt5pAn6/ /i4TTY7k/IcL3D2pLqU5rWXm7xoIfPj4r6alIHtq/bPzX3H87uOzw//wO2ENiWhWy5VS ZD6+oXSq3rQHTf22Z53t1i5Dmife/rYfQ53oc2rtnJmjxkPEVPaTnxGGdSqUalimokh6 /gbtpKgHkI6I/Ieo06f6CXY0BBGr3OC/CkBMNvcrUN/K5m2q552z0gVwhferRumvmvir 0hkQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id n3-20020a170903110300b001d006d3b969si939285plh.555.2023.11.28.09.45.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 09:45:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id B6E2880A7F24; Tue, 28 Nov 2023 09:45:03 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345519AbjK1Ros (ORCPT + 99 others); Tue, 28 Nov 2023 12:44:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233170AbjK1Ror (ORCPT ); Tue, 28 Nov 2023 12:44:47 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9A6E10A for ; Tue, 28 Nov 2023 09:44:53 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC3D5C433C7; Tue, 28 Nov 2023 17:44:50 +0000 (UTC) Date: Tue, 28 Nov 2023 17:44:48 +0000 From: Catalin Marinas To: Alan Stern Cc: Christoph Hellwig , Hamza Mahfooz , Dan Williams , Marek Szyprowski , Andrew , Ferry Toth , Andy Shevchenko , Thorsten Leemhuis , iommu@lists.linux.dev, Kernel development list , USB mailing list , Robin Murphy , Will Deacon Subject: Re: Bug in add_dma_entry()'s debugging code Message-ID: References: <736e584f-7d5f-41aa-a382-2f4881ba747f@rowland.harvard.edu> <20231127160759.GA1668@lst.de> <637d6dff-de56-4815-a15a-1afccde073f0@rowland.harvard.edu> <20231128133702.GA9917@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 28 Nov 2023 09:45:03 -0800 (PST) On Tue, Nov 28, 2023 at 10:18:19AM -0500, Alan Stern wrote: > On Tue, Nov 28, 2023 at 02:37:02PM +0100, Christoph Hellwig wrote: > > I'd actually go one step back: > > > > 1) for not cache coherent DMA you can't do overlapping operations inside > > a cache line > > Rephrasing slightly: You mustn't perform multiple non-cache-coherent DMA > operations that touch the same cache line concurrently. (The word > "overlapping" is a a little ambiguous in this context.) The problem is worse. I'd say you should not perform even a single non-cache-coherent DMA (usually from-device or bidirectional) operation if the cache line is shared with anything else modifying it. It doesn't need to be another DMA operation. But that's more difficult to add to the DMA API debug code (maybe something like the bouncing logic in dma_kmalloc_needs_bounce()). > > The logical confcusion from that would be that IFF dma-debug is enabled on > > any platform we need to set ARCH_DMA_MINALIGN to the cache line size. Or just force the kmalloc() min align to cache_line_size(), something like: diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 4a658de44ee9..3ece20367636 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -543,6 +543,8 @@ static inline int dma_get_cache_alignment(void) #ifdef ARCH_HAS_DMA_MINALIGN return ARCH_DMA_MINALIGN; #endif + if (IS_ENABLED(CONFIG_DMA_API_DEBUG)) + return cache_line_size(); return 1; } #endif diff --git a/mm/slab_common.c b/mm/slab_common.c index 8d431193c273..d0b21d6e9328 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -879,7 +879,7 @@ static unsigned int __kmalloc_minalign(void) unsigned int minalign = dma_get_cache_alignment(); if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && - is_swiotlb_allocated()) + is_swiotlb_allocated() && !IS_ENABLED(CONFIG_DMA_API_DEBUG)) minalign = ARCH_KMALLOC_MINALIGN; return max(minalign, arch_slab_minalign()); Also note that to_cacheline_number() in kernel/dma/debug.c only takes into account the L1_CACHE_SHIFT. On arm64 for example, cache_line_size() returns the maximum line of all the cache levels (and we've seen hardware where the L1 is 64-byte, L2 is 128). > > BUT: we're actually reduzing our dependency on ARCH_DMA_MINALIGN by > > moving to bounce buffering unaligned memory for non-coherent > > architectures, > > What's the reason for this? To allow the minimum allocation size to be > smaller than the cache line size? Does the savings in memory make up > for the extra overhead of bounce buffering? > > Or is this just to allow people to be more careless about how they > allocate their DMA buffers (which doesn't seem to make sense)? It's the former and it does make a difference with lots of small structure or string allocations. [...] > I get the impression that you would really like to have two different > versions of kmalloc() and friends: one for buffers that will be used in > DMA (and hence require cache-line alignment) and one for buffers that > won't be. We've been there for the past 2-3 years (and a few other options). It's hard to guess in a generic way because the allocation place may not necessarily know how the device is going to access that memory (PIO, DMA). The conclusion was that for those cases where the buffer is small, we just do a bounce. If it's performance critical, the driver can use a kmem_cache_create(SLAB_HWCACHE_ALIGN) and avoid the bouncing. -- Catalin