Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp18982lfv; Tue, 12 Apr 2022 15:29:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDFZ40fJFOJ67giaGI5EcDedvkLDnUjoyqeOOgtLTyTxgQdcCoMEVsH1pANyrSKcLD/eK/ X-Received: by 2002:a17:90b:1bc3:b0:1cd:3ce7:aae8 with SMTP id oa3-20020a17090b1bc300b001cd3ce7aae8mr3386085pjb.1.1649802558813; Tue, 12 Apr 2022 15:29:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649802558; cv=none; d=google.com; s=arc-20160816; b=ps8lDetlXH8e8IJWODZcMzt62XSd7Wxk9gXgny/JR2HHo1a+bMAHwv+OMQqLALCWiF r4LOU1UY8nGDRRmXxvQop8ccoXmbDWXckP9oblD86DHJj7a6SC+Gkv9cc24Lxxf26cqZ TcFHiuuUtQNZsoqa27BLrTvsgEfOCBlAUS6H5S1fQIfaTv/mqadB69PpE0Lea/QHFTpH cKdiDizMxfxinJOdKpTf0t6gZnCQw/fkT0Z6PM4uMxAjUR3KF2I4EvK5v5fZ1UoO5KjF emsg16md0AG+sDifLZc/6ouzThg8voaI/SpMIsh+GkJi2LByK6NiS12CNdlAZ3wrX0pL 2gXQ== 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=tZgsJH0aF+59RMkyFgOUNK6gucA9q79eIIYcR5VDjJs=; b=P23BHTk4xtXLYDm46HTHjuKMr+qqALv06DpJ2Ey2j8c71i1NqCrjkseAvN0CbLdIw7 o6br7zuOzHG79c4L30jlZccqPRMlgeDj9w2/I09nsQxRMT7n59mNDekl3tQOCuNtnXom O3OV8jjEHwiseoHjw1MHtlNXEchrSAAXj6a34IFSncr4DkOxmql4nrpwmrkS5cAgJPe4 z/IcjDYdyHVqOry60UfzOeOIc50ygeUfHRmF+fiktIgpwectxOM/cmzHGYorAyD1hny1 m05jrYiBsXDb41UyB+ZvrR6k992lLWDSn7eHyoywGaaIwJ6rcKzmXTS5VCWGA8THJ3Jc F0Gw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id 20-20020a630114000000b0039cef73106esi3813324pgb.511.2022.04.12.15.29.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 15:29:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DDF8E18B782; Tue, 12 Apr 2022 14:07:31 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236278AbiDLKjO (ORCPT + 99 others); Tue, 12 Apr 2022 06:39:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352529AbiDLKdg (ORCPT ); Tue, 12 Apr 2022 06:33:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EBE224BC4 for ; Tue, 12 Apr 2022 02:33:05 -0700 (PDT) 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 EE239617DA for ; Tue, 12 Apr 2022 09:33:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E77CC385A5; Tue, 12 Apr 2022 09:33:02 +0000 (UTC) Date: Tue, 12 Apr 2022 10:32:58 +0100 From: Catalin Marinas To: Herbert Xu Cc: Ard Biesheuvel , Will Deacon , Marc Zyngier , Arnd Bergmann , Greg Kroah-Hartman , Andrew Morton , Linus Torvalds , linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, "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-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 08, 2022 at 05:11:28PM +0800, Herbert Xu wrote: > On Fri, Apr 08, 2022 at 10:04:54AM +0100, Catalin Marinas wrote: > > My point is that if the crypto code kmallocs a size aligned to > > crypto_tfm_ctx_alignment() (and CRYPTO_MINALIGN), the slab allocator > > will return memory aligned to CRYPTO_MINALIGN even if > > ARCH_KMALLOC_MINALIGN is smaller. > > No we don't align the size to CRYPTO_MINALIGN at all. We simply > assume that this is the alignment returned by kmalloc. > > > Would the crypto code, say, do a kmalloc(64) and expect a 128 byte > > alignment (when CRYPTO_MINALIGN == 128)? Or does it align the size to > > CRYPTO_MINALIGN and do a kmalloc(128) directly? If it's the latter, I > > don't think there's a problem. > > It's the former. Does this only matter for DMA? If there other unrelated alignment requirements, I think those drivers should be fixed for their own cra_alignmask independent of the CPU cache line and DMA needs (e.g. some 1024-bit vectors that would benefit from an aligned load). With this series, the dynamic arch_kmalloc_minalign() still provides the DMA-safe alignment even if it would be smaller than the default CRYPTO_MINALIGN of 128. Let's say we have a structure: struct crypto_something { u8 some_field; u8 data[] CRYPTO_MINALIGN_ATTR; }; The sizeof(struct crypto_something) is automatically a multiple of CRYPTO_MINALIGN (128 bytes for arm64). While kmalloc() could return a smaller alignment, arch_kmalloc_minalign(), the data pointer above is still aligned to arch_kmalloc_minalign() and DMA-safe since CRYPTO_MINALIGN is a multiple of this (similar to the struct devres case, https://lore.kernel.org/all/YlRn2Wal4ezjvomZ@arm.com/). Even if such struct is included in another struct, the alignment and sizeof is inherited by the containing object. So let's assume the driver needs 64 bytes of data in addition to the struct, it would allocate: kmalloc(sizeof(struct crypto_something) + 64); Prior to this series, kmalloc() would return a 256-byte aligned pointer. With this series, if the cache line size on a SoC is 64-byte, the allocation falls under the kmalloc-192 cache, so 'data' would be 64-byte aligned which is safe for DMA. > I think you can still make the change you want, but first you need > to modify the affected drivers to specify their actual alignment > requirement explicitly through cra_alignmask and then use the > correct methods to access the context pointer. At a quick grep, most cra_alignmask values are currently 15 or smaller. I'm not convinced the driver needs to know about the CPU cache alignment. We could set cra_alignmask to CRYPTO_MINALIGN but that would incur unnecessary overhead via function like setkey_unaligned() when the arch_kmalloc_minalign() was already sufficient for DMA safety. Maybe I miss some use-cases or I focus too much on DMA safety. Thanks. -- Catalin