2023-01-18 21:42:41

by Dmitry Safonov

[permalink] [raw]
Subject: [PATCH v4 0/4] net/crypto: Introduce crypto_pool

Changes since v3 [6]:
- Cleanup seg6_hmac_init() and seg6_hmac_exit() declaration/usage
left-overs (reported by Jakub)
- Remove max(size, __scratch_size) from crypto_pool_reserve_scratch()

Changes since v2 [5]:
- Fix incorrect rebase of v2: tcp_md5_add_crypto_pool() was
called on twsk creation even for sockets without TCP-MD5 key
- Documentation title underline length
(Reported-by: kernel test robot <[email protected]>)
- Migrate crypto_pool_scratch to __rcu, using rcu_dereference*()
and rcu_replace_pointer(). As well, I changed local_bh_{en,dis}able()
to rcu_read_{,un}lock_bh().
(Addressing Jakub's review)
- Correct Documentation/ to use proper kerneldoc style, include it in
toc/tree and editor notes (from Jakub's comments)
- Avoid cast in crypto_pool_get() (Jakub's review)
- Select CRYPTO in Kconfig, not only CRYPTO_POOL (Jakub's reivew)
- Remove free_batch[] with synchronize_rcu() in favor of a struct
with a flexible array inside + call_rcu() (suggested by Jakub)
- Change scratch `size` argument type from (unsigned long) to (size_t)
for consistency
- Combined crypto_pool_alloc_ahash() and crypto_pool_reserve_scratch(),
now the scratch area size is supplied on crypto_pool allocation
(suggested by Jakub)
- Removed CONFIG_CRYPTO_POOL_DEFAULT_SCRATCH_SIZE
- CRYPTO_POOL now is a hidden symbol (Jakub's review)
- Simplified __cpool_alloc_ahash() error-paths, adding local variables
(suggested by Jakub)
- Resurrect a pool waiting to be destroyed if possible (Jakub's review)
- Rename _get() => _start(), _put() => _end(), _add() => _get()
(suggested by Jakub)

Changes since v1 [1]:
- Patches went through 3 iterations inside bigger TCP-AO patch set [2],
now I'm splitting it apart and sending it once again as a stand-alone
patch set to help reviewing it and make it easier to merge.
It is second part of that big series, once it merges the next part
will be TCP changes to add Authentication Option support (RFC5925),
that use API provided by these patches.
- Corrected kerneldoc-style comment near crypto_pool_reserve_scratch()
(Reported-By: kernel test robot <[email protected]>)
- Added short Documentation/ page for crypto_pool API

Add crypto_pool - an API for allocating per-CPU array of crypto requests
on slow-path (in sleep'able contexts) and for using them on a fast-path,
which is RX/TX for net/* users.

The design is based on the current implementations of md5sig_pool, which
this patch set makes generic by separating it from TCP core, moving it
to crypto/ and adding support for other hashing algorithms than MD5.
It makes a generic implementation for a common net/ pattern.

The initial motivation to have this API is TCP-AO, that's going to use
the very same pattern as TCP-MD5, but for multiple hashing algorithms.
Previously, I've suggested to add such API on TCP-AO patch submission [3],
where Herbert kindly suggested to help with introducing new crypto API.
See also discussion and motivation in crypto_pool-v1 [4].

The API will allow:
- to reuse per-CPU ahash_request(s) for different users
- to allocate only one per-CPU scratch buffer rather than a new one for
each user
- to have a common API for net/ users that need ahash on RX/TX fast path

In this version I've wired up TCP-MD5 and IPv6-SR-HMAC as users.
Potentially, xfrm_ipcomp and xfrm_ah can be converted as well.
The initial reason for patches would be to have TCP-AO as a user, which
would let it share per-CPU crypto_request for any supported hashing
algorithm.

[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/T/#u
[3]: http://lkml.kernel.org/r/[email protected]
[4]: https://lore.kernel.org/all/[email protected]/T/#u
[5]: https://lore.kernel.org/all/[email protected]/
[6]: https://lore.kernel.org/all/[email protected]/T/#u

Cc: Andy Lutomirski <[email protected]>
Cc: Bob Gilligan <[email protected]>
Cc: David Ahern <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Dmitry Safonov <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Hideaki YOSHIFUJI <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Leonard Crestez <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: Salam Noureddine <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

Dmitry Safonov (4):
crypto: Introduce crypto_pool
crypto/net/tcp: Use crypto_pool for TCP-MD5
crypto/net/ipv6: sr: Switch to using crypto_pool
crypto/Documentation: Add crypto_pool kernel API

Documentation/crypto/crypto_pool.rst | 36 +++
Documentation/crypto/index.rst | 1 +
crypto/Kconfig | 3 +
crypto/Makefile | 1 +
crypto/crypto_pool.c | 333 +++++++++++++++++++++++++++
include/crypto/pool.h | 46 ++++
include/net/seg6_hmac.h | 9 -
include/net/tcp.h | 24 +-
net/ipv4/Kconfig | 1 +
net/ipv4/tcp.c | 104 ++-------
net/ipv4/tcp_ipv4.c | 100 ++++----
net/ipv4/tcp_minisocks.c | 21 +-
net/ipv6/Kconfig | 1 +
net/ipv6/seg6.c | 14 +-
net/ipv6/seg6_hmac.c | 207 +++++++----------
net/ipv6/tcp_ipv6.c | 61 +++--
16 files changed, 636 insertions(+), 326 deletions(-)
create mode 100644 Documentation/crypto/crypto_pool.rst
create mode 100644 crypto/crypto_pool.c
create mode 100644 include/crypto/pool.h


base-commit: c1649ec55708ae42091a2f1bca1ab49ecd722d55
--
2.39.0


2023-01-18 21:42:56

by Dmitry Safonov

[permalink] [raw]
Subject: [PATCH v4 4/4] crypto/Documentation: Add crypto_pool kernel API

Signed-off-by: Dmitry Safonov <[email protected]>
---
Documentation/crypto/crypto_pool.rst | 36 ++++++++++++++++++++++++++++
Documentation/crypto/index.rst | 1 +
2 files changed, 37 insertions(+)
create mode 100644 Documentation/crypto/crypto_pool.rst

diff --git a/Documentation/crypto/crypto_pool.rst b/Documentation/crypto/crypto_pool.rst
new file mode 100644
index 000000000000..84abd1f2ee80
--- /dev/null
+++ b/Documentation/crypto/crypto_pool.rst
@@ -0,0 +1,36 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Per-CPU pool of crypto requests
+===============================
+
+Overview
+--------
+The crypto pool API manages pre-allocated per-CPU pool of crypto requests,
+providing ability to use crypto requests on fast paths, potentially in atomic
+contexts. The allocation and initialization of the requests should be done
+before their usage as it's slow-path and may sleep.
+
+Order of operations
+-------------------
+You are required to allocate a new pool prior using it and manage its lifetime.
+You can allocate a per-CPU pool of ahash requests by crypto_pool_alloc_ahash().
+It will give you a pool id that you can use further on fast-path for hashing.
+You can increase the reference counter for an allocated pool via
+crypto_pool_get(). Decrease the reference counter by crypto_pool_release().
+When the refcounter hits zero, the pool is scheduled for destruction and you
+can't use the corresponding crypto pool id anymore.
+Note that crypto_pool_get() and crypto_pool_release() must be called
+only for an already existing pool and can be called in atomic contexts.
+
+crypto_pool_start() disables bh and returns you back ``struct crypto_pool *``,
+which is a generic type for different crypto requests and has ``scratch`` area
+that can be used as a temporary buffer for your operation.
+
+crypto_pool_end() enables bh back once you've done with your crypto
+operation.
+
+.. kernel-doc:: include/crypto/pool.h
+ :identifiers:
+
+.. kernel-doc:: crypto/crypto_pool.c
+ :identifiers:
diff --git a/Documentation/crypto/index.rst b/Documentation/crypto/index.rst
index 21338fa92642..3eaf4e964e5b 100644
--- a/Documentation/crypto/index.rst
+++ b/Documentation/crypto/index.rst
@@ -25,6 +25,7 @@ for cryptographic use cases, as well as programming examples.
devel-algos
userspace-if
crypto_engine
+ crypto_pool
api
api-samples
descore-readme
--
2.39.0