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 A283CC433F5 for ; Thu, 14 Apr 2022 14:28:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 109726B0071; Thu, 14 Apr 2022 10:28:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0B9D26B0073; Thu, 14 Apr 2022 10:28:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC3006B0074; Thu, 14 Apr 2022 10:27:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id DAFA56B0071 for ; Thu, 14 Apr 2022 10:27:59 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 8F163122D96 for ; Thu, 14 Apr 2022 14:27:59 +0000 (UTC) X-FDA: 79355713878.25.DB31C58 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf05.hostedemail.com (Postfix) with ESMTP id E37C7100005 for ; Thu, 14 Apr 2022 14:27:58 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 152FCB8244E; Thu, 14 Apr 2022 14:27:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55F59C385A1; Thu, 14 Apr 2022 14:27:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649946475; bh=tv0ZyPejhNVJIC2/gOEs9RAJTEBZHd2tc7EDYWiVeWU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=cUFsG+ssXiitZzQH2dFqquickLykGtIy+KPcIU0tOfpS45sWP9CWaIzJVfWIvFtM4 ks0fYXDEE3Q+El1sfA/qU4ViMelLPpb7O7/eKxuRU/Jf5uOYO1QUkJ6Qy54s3Wgsna vdR9JS1bR0dSAZT+ik0m6guvHz6+1R/e8N78ZEnw= Date: Thu, 14 Apr 2022 16:27:53 +0200 From: Greg Kroah-Hartman To: Ard Biesheuvel Cc: Linus Torvalds , Catalin Marinas , Herbert Xu , Will Deacon , Marc Zyngier , Arnd Bergmann , Andrew Morton , Linux Memory Management List , Linux ARM , Linux Kernel Mailing List , "David S. Miller" Subject: Re: [PATCH 07/10] crypto: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Stat-Signature: jwqggzumyiwoxay9o7xsxtb8wzysdapo Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=cUFsG+ss; spf=pass (imf05.hostedemail.com: domain of gregkh@linuxfoundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E37C7100005 X-HE-Tag: 1649946478-543753 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 Thu, Apr 14, 2022 at 03:52:53PM +0200, Ard Biesheuvel wrote: > On Thu, 14 Apr 2022 at 07:38, Greg Kroah-Hartman > wrote: > > > > On Wed, Apr 13, 2022 at 09:53:24AM -1000, Linus Torvalds wrote: > > > On Tue, Apr 12, 2022 at 10:47 PM Catalin Marinas > > > wrote: > > > > > > > > I agree. There is also an implicit expectation that the DMA API works on > > > > kmalloc'ed buffers and that's what ARCH_DMA_MINALIGN is for (and the > > > > dynamic arch_kmalloc_minalign() in this series). But the key point is > > > > that the driver doesn't need to know the CPU cache topology, coherency, > > > > the DMA API and kmalloc() take care of these. > > > > > > Honestly, I think it would probably be worth discussing the "kmalloc > > > DMA alignment" issues. > > > > > > 99.9% of kmalloc users don't want to do DMA. > > > > > > And there's actually a fair amount of small kmalloc for random stuff. > > > Right now on my laptop, I have > > > > > > kmalloc-8 16907 18432 8 512 1 : ... > > > > > > according to slabinfo, so almost 17 _thousand_ allocations of 8 bytes. > > > > > > It's all kinds of sad if those allocations need to be 64 bytes in size > > > just because of some silly DMA alignment issue, when none of them want > > > it. > > > > > Actually, the alignment for non-cache coherent DMA is 128 bytes on > arm64, not 64 bytes. > > > > Yeah, yeah, wasting a megabyte of memory is "just a megabyte" these > > > days. Which is crazy. It's literally memory that could have been used > > > for something much more useful than just pure and utter waste. > > > > > > I think we could and should just say "people who actually require DMA > > > accesses should say so at kmalloc time". We literally have that > > > GFP_DMA and ZOME_DMA for various historical reasons, so we've been > > > able to do that before. > > > > > > No, that historical GFP_DMA isn't what arm64 wants - it's the old > > > crazy "legacy 16MB DMA" thing that ISA DMA used to have. > > > > > > But the basic issue was true then, and is true now - DMA allocations > > > are fairly special, and should not be that hard to just mark as such. > > > > "fairly special" == "all USB transactions", so it will take a lot of > > auditing here. I think also many SPI controllers require this and maybe > > I2C? Perhaps other bus types do as well. > > > > So please don't make this change without some way of figuring out just > > what drivers need to be fixed up, as it's going to be a lot... > > > > Yeah, the current de facto contract of being able to DMA map anything > that was allocated via the linear map makes it quite hard to enforce > the use of dma_kmalloc() for this. > > What we might do, given the fact that only inbound non-cache coherent > DMA is problematic, is dropping the kmalloc alignment to 8 like on > x86, and falling back to bounce buffering when a misaligned, non-cache > coherent inbound DMA mapping is created, using the SWIOTLB bounce > buffering code that we already have, and is already in use on most > affected systems for other reasons (i.e., DMA addressing limits) Ick, that's a mess. > This will cause some performance regressions, but in a way that seems > fixable to me: taking network drivers as an example, the RX buffers > that are filled using inbound DMA are typically owned by the driver > itself, which could be updated to round up its allocations and DMA > mappings. Block devices typically operate on quantities that are > aligned sufficiently already. In other cases, we will likely notice > if/when this fallback is taken on a hot path, but if we don't, at > least we know a bounce buffer is being used whenever we cannot perform > the DMA safely in-place. We can move to having an "allocator-per-bus" for memory like this to allow the bus to know if this is a DMA requirement or not. So for all USB drivers, we would have: usb_kmalloc(size, flags); and then it might even be easier to verify with static tools that the USB drivers are sending only properly allocated data. Same for SPI and other busses. https://lore.kernel.org/r/230a9486fc68ea0182df46255e42a51099403642.1648032613.git.christophe.leroy@csgroup.eu is an example of a SPI driver that has been there "for forever" yet always got it wrong. If we could have had some way to know "only memory allocated with this function is allowed on the bus" that would have fixed the issue a long time ago. Anyway, just an idea, it's up to you all if this is worth it or not. Adding performance regressions at the expense of memory size feels like a rough trade-off to go through until things are fixed up. greg k-h