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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A24CCC433F5 for ; Mon, 11 Apr 2022 17:40:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jIgbH8krQLRhZj9/MjgTmSVlHKkZuTYG9FNyqXksKNA=; b=BlgeMs6+5HiOT/ 3WgblHTyig6ouNQGNDnqaSa41mEAsujNXilXM0a+QIMLtqKsGGW2AiWfFvzEJ3L2HAyxdl5w0/XQW 1670IST4kCMaoT7c/O4qvH3YHvi6TcaAlzDweyMzjX1Ow6xSpp33wVbR2IYNr9peWXdleo8oXlaeT 4pHmwm46QCvAMvOFJddxl4+T8f7/pxrVk1peewi5zlM2aOL5IufcMEi9GcrNNWtt1Sp+MGB6lIYhE oO7sr/Ca6I18X9NPFYUtSPy9Xq+AHD5Fx2bcTjiJpwo7stseFIoAbdWQt9YTh1s2S+dZ/GgUF6RuK rKqZYfKGLZlUpK3jfQ2A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ndy0y-009x9b-Jm; Mon, 11 Apr 2022 17:39:48 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ndy0u-009x8B-TP for linux-arm-kernel@lists.infradead.org; Mon, 11 Apr 2022 17:39:46 +0000 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-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220411_103945_036098_C6F7385E X-CRM114-Status: GOOD ( 20.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel