Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1711183pxb; Mon, 8 Mar 2021 04:39:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJwtJXDz5zmzD2inuMq9H1KhVNwMHwCCpKo9tpksjeqC55yIVpVK/UXLZL6wcKyKM8Unpxqx X-Received: by 2002:a17:906:128e:: with SMTP id k14mr14539460ejb.427.1615207164745; Mon, 08 Mar 2021 04:39:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615207164; cv=none; d=google.com; s=arc-20160816; b=evMd++1wKXdduZzL1ua/z0kyJXxEplTiqdYEHriXPnEvyK3M+vDuD+sLdup/qnfbRi Tze6btdOjp9dk7IV5+FdubuSG4mtVIC7uqt1T98vkt7nSb98rhZNrJVntuHNjiFpGFgM HCwHw8hPrE0iaEsLGb+uiJAIcyyGBaioXXpXxFitVwGYBgcPbYpk8iGeyaLRVtInb+bC UF8VPOu7CBPKl6DN/62OLNwGB0WidEZns75P3iYxUAX5mGDL35PfSfXm9lXKDa+9o7Ik vpmRwEkCOMvy8CCS9UO898sr6urK+fNvXvhyl5QczLHWlb1cPF8EaVX2vIPyD0PTp30D k8tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ar9+kSd8yiZpnU1N/6HkTgILCg6gfWtJm3nk6CAU97s=; b=fYGqilDjF45hGtHx46lLxtB6m98A7xGBMTqbEhcS8O0Ae9pN5h7K4ScYpqFHbLSdMp 2Qkefb35MmBPlhkFCdxZBKu3TpG2p0aO/g2y/P+Fbidn0owuU0vbZWyx4gy9WON/pPy9 k1GU3ExaN9t7PMP28Wko9lKb8/CjNw0eSbBEVX5A3qL39YlL5e7btSG7YmLOVxOzXiB4 FCBijRUEc/KEV5cukLxAX5CrupNLpO9qTQptSFC42J57S0bB+qZ7Ten43asJH9NSuRT/ VpBrv2F+uBjyVKtxxgTdq4SX4yQfAmAI8n1eyI7PFAleBzqSHZyYuYhOufdoEzUXFep9 rs6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LhcbSqSX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u2si6652615ejg.288.2021.03.08.04.39.01; Mon, 08 Mar 2021 04:39:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LhcbSqSX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232191AbhCHMfl (ORCPT + 99 others); Mon, 8 Mar 2021 07:35:41 -0500 Received: from mail.kernel.org ([198.145.29.99]:44232 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231864AbhCHMfN (ORCPT ); Mon, 8 Mar 2021 07:35:13 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id A8086651C9; Mon, 8 Mar 2021 12:35:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615206913; bh=YDVCP2lX8XkTA53jIjKsJ0XzxGvlpkbhjDHf1OVepeM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LhcbSqSXfd2HcXhfiqfOJ8s0/JJcpZetfYcnufefvuw2CKilEONZfWHpR6zNRU1mG 1xy+gUT4K6Cw8On5Jc/UuUUasVJ/p3oAmVXsZiV4IdeUu1SvEa2rvM7tMQ8ii4AgX3 ruZSDvF4s5brOeUerejRwh0c4ggj7o4YPEwxBkUE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ard Biesheuvel , Herbert Xu Subject: [PATCH 5.10 23/42] crypto - shash: reduce minimum alignment of shash_desc structure Date: Mon, 8 Mar 2021 13:30:49 +0100 Message-Id: <20210308122719.264148779@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210308122718.120213856@linuxfoundation.org> References: <20210308122718.120213856@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ard Biesheuvel commit 660d2062190db131d2feaf19914e90f868fe285c upstream. Unlike many other structure types defined in the crypto API, the 'shash_desc' structure is permitted to live on the stack, which implies its contents may not be accessed by DMA masters. (This is due to the fact that the stack may be located in the vmalloc area, which requires a different virtual-to-physical translation than the one implemented by the DMA subsystem) Our definition of CRYPTO_MINALIGN_ATTR is based on ARCH_KMALLOC_MINALIGN, which may take DMA constraints into account on architectures that support non-cache coherent DMA such as ARM and arm64. In this case, the value is chosen to reflect the largest cacheline size in the system, in order to ensure that explicit cache maintenance as required by non-coherent DMA masters does not affect adjacent, unrelated slab allocations. On arm64, this value is currently set at 128 bytes. This means that applying CRYPTO_MINALIGN_ATTR to struct shash_desc is both unnecessary (as it is never used for DMA), and undesirable, given that it wastes stack space (on arm64, performing the alignment costs 112 bytes in the worst case, and the hole between the 'tfm' and '__ctx' members takes up another 120 bytes, resulting in an increased stack footprint of up to 232 bytes.) So instead, let's switch to the minimum SLAB alignment, which does not take DMA constraints into account. Note that this is a no-op for x86. Signed-off-by: Ard Biesheuvel Signed-off-by: Herbert Xu Signed-off-by: Greg Kroah-Hartman --- include/crypto/hash.h | 8 ++++---- include/linux/crypto.h | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) --- a/include/crypto/hash.h +++ b/include/crypto/hash.h @@ -149,7 +149,7 @@ struct ahash_alg { struct shash_desc { struct crypto_shash *tfm; - void *__ctx[] CRYPTO_MINALIGN_ATTR; + void *__ctx[] __aligned(ARCH_SLAB_MINALIGN); }; #define HASH_MAX_DIGESTSIZE 64 @@ -162,9 +162,9 @@ struct shash_desc { #define HASH_MAX_STATESIZE 512 -#define SHASH_DESC_ON_STACK(shash, ctx) \ - char __##shash##_desc[sizeof(struct shash_desc) + \ - HASH_MAX_DESCSIZE] CRYPTO_MINALIGN_ATTR; \ +#define SHASH_DESC_ON_STACK(shash, ctx) \ + char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \ + __aligned(__alignof__(struct shash_desc)); \ struct shash_desc *shash = (struct shash_desc *)__##shash##_desc /** --- a/include/linux/crypto.h +++ b/include/linux/crypto.h @@ -151,9 +151,12 @@ * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual * declaration) is used to ensure that the crypto_tfm context structure is * aligned correctly for the given architecture so that there are no alignment - * faults for C data types. In particular, this is required on platforms such - * as arm where pointers are 32-bit aligned but there are data types such as - * u64 which require 64-bit alignment. + * faults for C data types. On architectures that support non-cache coherent + * DMA, such as ARM or arm64, it also takes into account the minimal alignment + * that is required to ensure that the context struct member does not share any + * cachelines with the rest of the struct. This is needed to ensure that cache + * maintenance for non-coherent DMA (cache invalidation in particular) does not + * affect data that may be accessed by the CPU concurrently. */ #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN