Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3847660rdh; Tue, 28 Nov 2023 05:37:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IEv1ZrPbfLnMlpAHis6KSa35EcJl789PdY8ktRXLI1vhb/eq48zg0HfIorVaDEzNZ3wkcAT X-Received: by 2002:a05:6808:1188:b0:3ae:5c89:dcc2 with SMTP id j8-20020a056808118800b003ae5c89dcc2mr20114470oil.34.1701178657646; Tue, 28 Nov 2023 05:37:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701178657; cv=none; d=google.com; s=arc-20160816; b=G1XQUkPrLZdBos0dnrApoRVqxmu0iOi0ipAk5mHRVCnTCxIvZt5We+EbQ/5yQcO4o4 zYuQJtUhcssAgTn6LD8gqyFnbr4d0hsEpCPK3baouTH4O+v7f496wwolwN/nhpkWYS5k RTC90Xg7C+yxaUeDUVYpILVUWUDzQqPROiPOUdInXPP3ifxfnhnV80zDih0Js30BWBAb z+m1PWvIKCRYi/ttIML/qZQv7WZK8UGe/gGW+Z9Ox3hVIHUMEg+jqXQ/NE0AgtCR/XUD gCCs9TVJgc02wWPGB1WX5I86u3t9/jSHnjc0ZyGi7vvVISeQg/n5imCloIrjsjJPntIm Vg5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=j++xyNic6+jM7rGUPG6y3Hi1v17oQ8XYPtZt25ppSno=; fh=gqtOwxOaDOovoVu8HZeUJqHlkepvhzF1nCXAvlcdIZI=; b=tWa5yO5TSefesgRTJEjlWnpSuDj28pBlXZ4nQiovBtlQzkMBIWPfWId7HMQquPbU7M 0EJZ/RHmYFL6tTqs6LCJXWNIJPzVVxL35ZqIko60hQRukFRQUbylCE8BuYGc2jxzUMis M3I+w+5n5mtxz+TK73qHdFs6Hh/zpembPI2rhcjGYj6f70wC5x1o/vtTjS5lWVlrQx28 C2Az7roKg+0aEok1v7OsVP0bg8WcKsCnbga/8kJDLDnW8ijchkZ11d8DoHQDacm4Zqr3 cihfC21LCJJSKu2UH4tn9QJj30xQ/ttoXiBrGg4OyC6ehR/WNWWWhlAk6I4KiBSmfjb6 mFuQ== 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:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id w19-20020a634913000000b005c1b289757dsi11395786pga.88.2023.11.28.05.37.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 05:37:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 3EF4F80BE7C4; Tue, 28 Nov 2023 05:37:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345895AbjK1NhF (ORCPT + 99 others); Tue, 28 Nov 2023 08:37:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345983AbjK1NhC (ORCPT ); Tue, 28 Nov 2023 08:37:02 -0500 Received: from verein.lst.de (verein.lst.de [213.95.11.211]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99F79183; Tue, 28 Nov 2023 05:37:06 -0800 (PST) Received: by verein.lst.de (Postfix, from userid 2407) id 9FA90227A87; Tue, 28 Nov 2023 14:37:02 +0100 (CET) Date: Tue, 28 Nov 2023 14:37:02 +0100 From: Christoph Hellwig 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 , Catalin Marinas , Robin Murphy , Will Deacon Subject: Re: Bug in add_dma_entry()'s debugging code Message-ID: <20231128133702.GA9917@lst.de> References: <736e584f-7d5f-41aa-a382-2f4881ba747f@rowland.harvard.edu> <20231127160759.GA1668@lst.de> <637d6dff-de56-4815-a15a-1afccde073f0@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <637d6dff-de56-4815-a15a-1afccde073f0@rowland.harvard.edu> User-Agent: Mutt/1.5.17 (2007-11-01) 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 morse.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 (morse.vger.email [0.0.0.0]); Tue, 28 Nov 2023 05:37:35 -0800 (PST) On Mon, Nov 27, 2023 at 11:51:21AM -0500, Alan Stern wrote: > The buffers in the bug report were allocated by kmalloc(). Doesn't > kmalloc() guarantee that on architectures with non-cache-coherent DMA, > allocations will be aligned on cache-line boundaries (or larger)? Isn't > this what ARCH_DMA_MINALIGN and ARCH_KMALLOC_MINALIGN are supposed to > take care of in include/linux/slab.h? Oh. Yes, the variable alignment from kmalloc make things complicated. > Architectures must ensure that kmalloc'ed buffer is > DMA-safe. Drivers and subsystems depend on it. If an architecture > isn't fully DMA-coherent (i.e. hardware doesn't ensure that data in > the CPU cache is identical to data in main memory), > ARCH_DMA_MINALIGN must be set so that the memory allocator > makes sure that kmalloc'ed buffer doesn't share a cache line with > the others. See arch/arm/include/asm/cache.h as an example. > > It says nothing about avoiding more than one DMA operation at a time to > prevent overlap. Is the documentation wrong? The documentation is a bit lacking unfortunately. Again, assuming you actually have non-coherent mappings you'd easily break the fragile cache line ownership if you did. That doesn't apply to x86 as-is, but we really try to keep drivers portable. > > This might not have an > > effect on the particular plaform you are currently running on, but it > > is still wrong. > > Who decides what is right and what is wrong in this area? Clearly you > have a different opinion from David S. Miller, Richard Henderson, and > Jakub Jelinek (the authors of that documentation file). I don't think this about anyone's opinion. It's a fundamental propery of how to manage caches in the face of non-coherent DMA. > > > Note that there have been various mumblings about > > using nosnoop dma on x86, in which case you'll have the issue there > > as well. > > Unless the people who implement nosnoop DMA also modify kmalloc() or > ARCH_DMA_MINALIGN. Or don't do it on kmalloc buffers. > I guess the real question boils down to where the responsiblity should > lie. Should kmalloc() guarantee that the memory regions it provides > will always be suitable for DMA, with no overlap issues? Or should all > the innumerable users of kmalloc() be responsible for jumping through > hoops to request arch-specific minimum alignment for their DMA buffers? I'd actually go one step back: 1) for not cache coherent DMA you can't do overlapping operations inside a cache line 2) dma-debug is intended to find DMA API misuses, even if they don't have bad effects on your particular system 3) the fact that the kmalloc implementation returns differently aligned memory depending on the platform breaks how dma-debug works currently 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. BUT: we're actually reduzing our dependency on ARCH_DMA_MINALIGN by moving to bounce buffering unaligned memory for non-coherent architectures, which makes this even more complicated. Right now I don't have a good idea how to actually deal with having the cachline sanity checks with that, but I'm Ccing some of the usual suspects if they have a better idea.