From nobody Wed Apr 8 17:27:21 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5D3BFA3740 for ; Thu, 27 Oct 2022 20:44:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236802AbiJ0UoK (ORCPT ); Thu, 27 Oct 2022 16:44:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236638AbiJ0UoB (ORCPT ); Thu, 27 Oct 2022 16:44:01 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF3678B2C9 for ; Thu, 27 Oct 2022 13:43:59 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id o4so4161438wrq.6 for ; Thu, 27 Oct 2022 13:43:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RxfT3udHjY/1MFp4/+Y9TLXrCrikupuEdzT2r1qucRk=; b=WObTEuSpCZ6kMlzBm/Y1HjE9FvJSeykn2DUTcsVTC2eDycf2ZDGPUcrb6r7L/423El ZuPxAEamLFG8SeagtCXwBy5pSPP+SPHLPCAZ69QkfrsuhUAbe8J3usllgZj0Wb0pak7D NvVxE45Ve5j4+4at2dVO+/HKBYzr1h1+M4dDQtm3VDf0fEnOOhBaQRI4rBw80CwPGew8 5GnuzdG1g15c5nYNOEiTVTrMlgrL4s9YKXC0J6XyJKcYiXhBSzPr/nY7HlQVFjw+fvvx aMFfDbEp+su/jwS7NkHAVa08sXoo//f0GGNa0Zbsa6mccpRHxksq0jQcP4biMrmUBdgj uUuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RxfT3udHjY/1MFp4/+Y9TLXrCrikupuEdzT2r1qucRk=; b=bsu8+oBOrf0ByfjigaEMHxVGAR+p4zoHRi2+edzEGSlkQzoiUeX9v5Fs9K4jHGR+Rp mtETPSI5vwNnERegQfnq3FUAugcnT6OZc7MQVuQcdKnJPaKi1O+YWbIQCNQ70h80bmYY CIoDrIvl1cKzjiLBUj25Nv7yiQfz78SQ3i//0rruu2o+wdTw+acMl+1NwgHu2WRiykeb ETIll1M/Ab7hR5hJwgHT3TrwAERyKkl8LdFLze9QFOejeFkLoom2tm246NfKYJxO/c8P zVvZK13d0AyiV2LryfXdZWgrWWIUcXRZkeVIa1OX+JrzMO089Y/Mj5acoDrTNMrXRlg7 lrAw== X-Gm-Message-State: ACrzQf2pPrpvWbZPYLyGMWtuX6YjoUQL13OrnbFP5XGrDNXCCK3HUhRg rJQHyCKryX4w74R3XavFUfRD0OjCm4Z7iOKt X-Google-Smtp-Source: AMsMyM5uB1RtXoPm8/baWtcX8vcSmcCs4lg1PeNXA3NJ9eLfk5R01WukaeXAdIrN4jxeBP3JtYByLQ== X-Received: by 2002:a05:6000:811:b0:236:622d:a7d6 with SMTP id bt17-20020a056000081100b00236622da7d6mr19178175wrb.258.1666903438049; Thu, 27 Oct 2022 13:43:58 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id n3-20020a5d6b83000000b00236644228besm1968739wrx.40.2022.10.27.13.43.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Oct 2022 13:43:57 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org, David Ahern , Eric Dumazet Cc: Dmitry Safonov , Andy Lutomirski , Ard Biesheuvel , Bob Gilligan , Dan Carpenter , "David S. Miller" , Dmitry Safonov <0x7f454c46@gmail.com>, Eric Biggers , "Eric W. Biederman" , Francesco Ruggeri , Herbert Xu , Hideaki YOSHIFUJI , Ivan Delalande , Jakub Kicinski , Leonard Crestez , Paolo Abeni , Salam Noureddine , Shuah Khan , netdev@vger.kernel.org, linux-crypto@vger.kernel.org Subject: [PATCH v3 02/36] crypto_pool: Add crypto_pool_reserve_scratch() Date: Thu, 27 Oct 2022 21:43:13 +0100 Message-Id: <20221027204347.529913-3-dima@arista.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221027204347.529913-1-dima@arista.com> References: <20221027204347.529913-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Instead of having build-time hardcoded constant, reallocate scratch area, if needed by user. Different algos, different users may need different size of temp per-CPU buffer. Only up-sizing supported for simplicity. Signed-off-by: Dmitry Safonov --- crypto/Kconfig | 6 ++++ crypto/crypto_pool.c | 77 ++++++++++++++++++++++++++++++++++--------- include/crypto/pool.h | 3 +- 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index 768d331e626b..e002cd321e79 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -1397,6 +1397,12 @@ config CRYPTO_POOL help Per-CPU pool of crypto requests ready for usage in atomic contexts. =20 +config CRYPTO_POOL_DEFAULT_SCRATCH_SIZE + hex "Per-CPU default scratch area size" + depends on CRYPTO_POOL + default 0x100 + range 0x100 0x10000 + if !KMSAN # avoid false positives from assembly if ARM source "arch/arm/crypto/Kconfig" diff --git a/crypto/crypto_pool.c b/crypto/crypto_pool.c index 37131952c5a7..2f1deb3f5218 100644 --- a/crypto/crypto_pool.c +++ b/crypto/crypto_pool.c @@ -1,13 +1,14 @@ // SPDX-License-Identifier: GPL-2.0-or-later =20 #include +#include #include #include #include #include #include =20 -static unsigned long scratch_size =3D DEFAULT_CRYPTO_POOL_SCRATCH_SZ; +static unsigned long scratch_size =3D CONFIG_CRYPTO_POOL_DEFAULT_SCRATCH_S= IZE; static DEFINE_PER_CPU(void *, crypto_pool_scratch); =20 struct crypto_pool_entry { @@ -22,26 +23,69 @@ static struct crypto_pool_entry cpool[CPOOL_SIZE]; static unsigned int cpool_populated; static DEFINE_MUTEX(cpool_mutex); =20 -static int crypto_pool_scratch_alloc(void) +/* Slow-path */ +/** + * crypto_pool_reserve_scratch - re-allocates scratch buffer, slow-path + * @size: request size for the scratch/temp buffer + */ +#define FREE_BATCH_SIZE 64 +int crypto_pool_reserve_scratch(unsigned long size) { - int cpu; - - lockdep_assert_held(&cpool_mutex); + void *free_batch[FREE_BATCH_SIZE]; + int cpu, err =3D 0; + unsigned int i =3D 0; =20 + mutex_lock(&cpool_mutex); + if (size =3D=3D scratch_size) { + for_each_possible_cpu(cpu) { + if (per_cpu(crypto_pool_scratch, cpu)) + continue; + goto allocate_scratch; + } + mutex_unlock(&cpool_mutex); + return 0; + } +allocate_scratch: + size =3D max(size, scratch_size); + cpus_read_lock(); for_each_possible_cpu(cpu) { - void *scratch =3D per_cpu(crypto_pool_scratch, cpu); + void *scratch, *old_scratch; =20 - if (scratch) + scratch =3D kmalloc_node(size, GFP_KERNEL, cpu_to_node(cpu)); + if (!scratch) { + err =3D -ENOMEM; + break; + } + + old_scratch =3D per_cpu(crypto_pool_scratch, cpu); + /* Pairs with crypto_pool_get() */ + WRITE_ONCE(*per_cpu_ptr(&crypto_pool_scratch, cpu), scratch); + if (!cpu_online(cpu)) { + kfree(old_scratch); continue; + } + free_batch[i++] =3D old_scratch; + if (i =3D=3D FREE_BATCH_SIZE) { + cpus_read_unlock(); + synchronize_rcu(); + while (i > 0) + kfree(free_batch[--i]); + cpus_read_lock(); + } + } + cpus_read_unlock(); + if (!err) + scratch_size =3D size; + mutex_unlock(&cpool_mutex); =20 - scratch =3D kmalloc_node(scratch_size, GFP_KERNEL, - cpu_to_node(cpu)); - if (!scratch) - return -ENOMEM; - per_cpu(crypto_pool_scratch, cpu) =3D scratch; + if (i > 0) { + synchronize_rcu(); + while (i > 0) + kfree(free_batch[--i]); } - return 0; + return err; } +EXPORT_SYMBOL_GPL(crypto_pool_reserve_scratch); =20 static void crypto_pool_scratch_free(void) { @@ -138,7 +182,6 @@ int crypto_pool_alloc_ahash(const char *alg) =20 /* slow-path */ mutex_lock(&cpool_mutex); - for (i =3D 0; i < cpool_populated; i++) { if (cpool[i].alg && !strcmp(cpool[i].alg, alg)) { if (kref_read(&cpool[i].kref) > 0) { @@ -263,7 +306,11 @@ int crypto_pool_get(unsigned int id, struct crypto_poo= l *c) return -EINVAL; } ret->req =3D *this_cpu_ptr(cpool[id].req); - ret->base.scratch =3D this_cpu_read(crypto_pool_scratch); + /* + * Pairs with crypto_pool_reserve_scratch(), scartch area is + * valid (allocated) until crypto_pool_put(). + */ + ret->base.scratch =3D READ_ONCE(*this_cpu_ptr(&crypto_pool_scratch)); return 0; } EXPORT_SYMBOL_GPL(crypto_pool_get); diff --git a/include/crypto/pool.h b/include/crypto/pool.h index 2c61aa45faff..c7d817860cc3 100644 --- a/include/crypto/pool.h +++ b/include/crypto/pool.h @@ -4,8 +4,6 @@ =20 #include =20 -#define DEFAULT_CRYPTO_POOL_SCRATCH_SZ 128 - struct crypto_pool { void *scratch; }; @@ -20,6 +18,7 @@ struct crypto_pool_ahash { struct ahash_request *req; }; =20 +int crypto_pool_reserve_scratch(unsigned long size); int crypto_pool_alloc_ahash(const char *alg); void crypto_pool_add(unsigned int id); void crypto_pool_release(unsigned int id); --=20 2.38.1