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 77CF6C433EF for ; Mon, 11 Apr 2022 17:39:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E00A06B0072; Mon, 11 Apr 2022 13:39:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DAE846B0073; Mon, 11 Apr 2022 13:39:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C76376B0074; Mon, 11 Apr 2022 13:39:45 -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 B79926B0072 for ; Mon, 11 Apr 2022 13:39:45 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7E3381D0C for ; Mon, 11 Apr 2022 17:39:45 +0000 (UTC) X-FDA: 79345310730.12.56AF3DB Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id E156C180008 for ; Mon, 11 Apr 2022 17:39:44 +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 dfw.source.kernel.org (Postfix) with ESMTPS id F308D60B6F; Mon, 11 Apr 2022 17:39:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75909C385A4; Mon, 11 Apr 2022 17:39:41 +0000 (UTC) Date: Mon, 11 Apr 2022 18:39:37 +0100 From: Catalin Marinas To: Andy Shevchenko Cc: Will Deacon , Marc Zyngier , Arnd Bergmann , Greg Kroah-Hartman , Andrew Morton , Linus Torvalds , linux-mm , linux-arm Mailing List , Linux Kernel Mailing List , "Rafael J. Wysocki" Subject: Re: [PATCH 02/10] drivers/base: Use ARCH_DMA_MINALIGN instead of ARCH_KMALLOC_MINALIGN Message-ID: References: <20220405135758.774016-1-catalin.marinas@arm.com> <20220405135758.774016-3-catalin.marinas@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: E156C180008 X-Rspam-User: Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf16.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org X-Stat-Signature: bzshg7bj7ro3kpypbf1nwc7hdng3mnj9 X-HE-Tag: 1649698784-494159 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 Mon, Apr 11, 2022 at 05:57:08PM +0300, Andy Shevchenko wrote: > On Wed, Apr 6, 2022 at 2:30 PM Catalin Marinas wrote: > > ARCH_DMA_MINALIGN represents the minimum (static) alignment for safe DMA > > operations while ARCH_KMALLOC_MINALIGN is the minimum kmalloc() objects > > alignment. > > ... > > > - * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same > > + * Thus we use ARCH_DMA_MINALIGN here and get at least the same > > * buffer alignment as if it was allocated by plain kmalloc(). > > But then it becomes not true either, because the kmalloc() has other > alignment constraints. Maybe the comment could be improved a bit but I think it's still valid. After this patch, struct devres becomes: struct devres { struct devres_node node; u8 __aligned(ARCH_DMA_MINALIGN) data[]; }; While we no longer guarantee the ARCH_DMA_MINALIGN alignment (which is too big on most arm64 SoCs), what we need is for devres.data[] to be aligned to the newly introduced arch_kmalloc_minalign(). This would give us the DMA safety guarantees. Since devres.data[] is at an offset multiple of ARCH_DMA_MINALIGN, in order for the array to be aligned to arch_kmalloc_minalign(), all we need is for ARCH_DMA_MINALIGN to be a multiple of arch_kmalloc_minalign(). I actually had to write down some simple equations to convince myself. devres.data[] is at an offset multiple of ARCH_DMA_MINALIGN (after this patch), even when struct devres is included in another structure, so we have: offsetof(struct devres, data) = m * ARCH_DMA_MINALIGN ARCH_DMA_MINALIGN is a power of two while arch_kmalloc_minalign() is also a power of two, equal to or less than ARCH_DMA_MINALIGN: ARCH_DMA_MINALIGN = n * arch_kmalloc_minalign() A kmalloc()'ed object of struct devres (or a container of) is aligned to arch_kmalloc_minalign() by definition so: kmalloc() = p * arch_kmalloc_minalign() >From the above, we can conclude that the data[] pointer is at a multiple of arch_kmalloc_minalign(): devres.data = (p + m * n) * arch_kmalloc_minalign() Where m, n, p are all positive integers (n is also power of two). If we did not change the devres structure, the alignment of ARCH_KMALLOC_MINALIGN would no no be longer sufficient since the dynamic arch_kmalloc_minalign() can be greater than ARCH_KMALLOC_MINALIGN on specific SoCs (the first offsetof equation is no longer true). -- Catalin