crypto: atmel - introduce shared i2c core client management and capability-based selection framework
Subsystems (4)
v4 cover crypto: atmel - introduce shared i2c core client management and capability-based selection framework
This patch series introduces a staged refactoring of the Atmel crypto I2C drivers in preparation for a shared core-based architecture. The goal is to consolidate I2C client management and selection logic into a common atmel-i2c core driver while keeping ECC (ECDH) and SHA204A client drivers functionally separate but interoperating through shared infrastructure.
The series moves existing ECC-specific client tracking into a shared management structure, relocates allocation and selection logic, and introduces capability-based filtering for hardware selection. This allows individual crypto drivers to request hardware clients based on supported features while still benefiting from a unified least-loaded selection strategy.
Subsequent patches extend this base by:
- migrating client management fully into the core driver,
- introducing explicit capability advertisement by each hardware client,
- updating ECC and SHA204A drivers to participate in capability-aware allocation,
- and cleaning up probe/remove paths to ensure consistent lifecycle handling.
No functional behavioral changes are intended at this stage beyond internal refactoring and preparation for future feature expansion. The series is designed to preserve existing crypto functionality while gradually centralizing shared logic in the atmel-i2c core layer, reducing duplication and improving maintainability across all Atmel crypto drivers.
Note, this series is a more elaborated part of a bigger refactoring, sketched out here:
https://sashiko.dev/#/patchset/20260512224349.64621-1-l.rubusch%40gmail.com
---
v3 -> v4:
- sashiko feedback[3]
- fixes and patches reordered
- fix: cover a possible memory leak at deregistration and subsequent reregistration
[3] https://sashiko.dev/#/patchset/20260520155703.23018-1-l.rubusch%40gmail.com
v2 -> v3:
- sashiko feedback[2]
- ecc: reorder setting ready flag in probe()
- i2c: unconditionally call flush in unregister client function, to avoid UAF for multiple devices
- i2c: fixed a sleep-in-atomic bug by moving atmel_i2c_flush_queue() outside the spin_lock section
- sha204a: reorder calls in remove() avoid UAF flagged risk
- sha204a: rephrase commit messages
[2] https://sashiko.dev/#/patchset/20260519204803.17034-1-l.rubusch%40gmail.com
v1 -> v2:
- going over Sashiko feedback[1]
- rephrasing commit messages and titles
- fix: introduce a ready/state flag to address the UAF risk
- fix: add kpp lock and refcnt to impede overwriting global driver struct
- fix: explicitely clearing rng cached buffer in return branch
- unregistering ready state by dedicated function
- reorder Atmel ECC related things and atmel I2C at beginning
- reorder Atmel SHA204a related things behind introduction of cap
- patches dropped: NULL checks in remove functions
- changed to EXPORT_SYMBOL_GPL
- additionally to alloc hw client also migrate freeing it to core driver
[1] https://sashiko.dev/#/patchset/20260517180639.9657-1-l.rubusch%40gmail.com
---
Lothar Rubusch (12):
crypto: atmel-ecc - fix use after free situation
crypto: atmel-ecc - fix multi-device kpp registration
crypto: atmel-sha204a - fix heap info leak on I2C transfer failure
crypto: atmel-ecc - rename driver_data before moving it into atmel-i2c
crypto: atmel - rename atmel_ecc_driver_data to atmel_i2c_client_mgmt
crypto: atmel-i2c - move client management instance into core
crypto: atmel-i2c - introduce shared teardown helpers and fix queue
flush
crypto: atmel-ecc - switch to module_i2c_driver
crypto: atmel-i2c - move shared client allocation logic to core
crypto: atmel-i2c - implement capability-based client selection
crypto: atmel-sha204a - integrate into core management tracking
crypto: atmel-sha204a - switch to module_i2c_driver
drivers/crypto/atmel-ecc.c | 177 ++++++++++++++++----------------- drivers/crypto/atmel-i2c.c | 77 ++++++++++++++ drivers/crypto/atmel-i2c.h | 18 +++- drivers/crypto/atmel-sha204a.c | 48 +++++---- 4 files changed, 210 insertions(+), 110 deletions(-)
base-commit: 49e05bb00f2e8168695f7af4d694c39e1423e8a2
Signature · 1 line
2.39.5
PATCH v4 1/12 crypto: atmel-ecc - fix use after free situation
Fixes a possible race condition, when having multiple of such devices attached (identified by sashiko feedback).
The Scenario:
Thread A (Device 1 Probe): Successfully adds i2c_priv to the global
list (Line 324). The lock is released.
Thread B (An active crypto request): Concurrently calls
atmel_ecc_i2c_client_alloc(). It scans the global list, sees Device 1, and assigns a crypto job to it.
Thread A: Moves to line 332. crypto_register_kpp() fails (e.g., out of
memory or name clash).
Thread A: Enters the error path. It removes Device 1 from the list and
frees the i2c_priv memory.
Thread B: Is still actively trying to talk to the I2C hardware using
the i2c_priv pointer it grabbed in Step 2. The memory is now gone. Result: Kernel crash (Use-After-Free).
--- drivers/crypto/atmel-ecc.c | 10 ++++++++++ drivers/crypto/atmel-i2c.h | 2 ++ 2 files changed, 12 insertions(+)
drivers/crypto/atmel-ecc.c +10 -0
@@ -218,6 +218,8 @@ static struct i2c_client *atmel_ecc_i2c_client_alloc(void)list_for_each_entry (i2c_priv, &driver_data.i2c_client_list, i2c_client_list_node) { +if (!i2c_priv->ready) +continue ; tfm_cnt =atomic_read (&i2c_priv->tfm_count);if (tfm_cnt < min_tfm_cnt) { min_tfm_cnt = tfm_cnt;
@@ -322,20 +324,24 @@ static int atmel_ecc_probe(struct i2c_client *client)return ret; i2c_priv =i2c_get_clientdata (client); + i2c_priv->ready = false;spin_lock (&driver_data.i2c_list_lock);list_add_tail (&i2c_priv->i2c_client_list_node, &driver_data.i2c_client_list); + i2c_priv->ready = true;spin_unlock (&driver_data.i2c_list_lock); ret =crypto_register_kpp (&atmel_ecdh_nist_p256);if (ret) {spin_lock (&driver_data.i2c_list_lock); + i2c_priv->ready = false;list_del (&i2c_priv->i2c_client_list_node);spin_unlock (&driver_data.i2c_list_lock);dev_err (&client->dev,"%s alg registration failed\n" , atmel_ecdh_nist_p256.base.cra_driver_name); +return ret; }else {dev_info (&client->dev,"atmel ecc algorithms registered in /proc/crypto\n" ); }
@@ -347,6 +353,10 @@ static void atmel_ecc_remove(struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client); +spin_lock (&driver_data.i2c_list_lock); + i2c_priv->ready = false; +spin_unlock (&driver_data.i2c_list_lock); +/* Return EBUSY if i2c client already allocated. */ if (atomic_read (&i2c_priv->tfm_count)) {/*
drivers/crypto/atmel-i2c.h +2 -0
@@ -129,6 +129,7 @@ struct atmel_ecc_driver_data {* @wake_token_sz : size in bytes of the wake_token * @tfm_count : number of active crypto transformations on i2c client * @hwrng : hold the hardware generated rng +* @ready : hw client is ready to use * * Reads and writes from/to the i2c client are sequential. The first byte * transmitted to the device is treated as the byte size. Any attempt to send
@@ -145,6 +146,7 @@ struct atmel_i2c_client_priv {size_t wake_token_sz;atomic_t tfm_count____cacheline_aligned ;struct hwrng hwrng; +bool ready; };/**
Signature · 1 line
2.39.5
PATCH v4 2/12 crypto: atmel-ecc - fix multi-device kpp registration
In reply to Lothar RubuschWhen multiple atmel-ecc hardware accelerator chips are attached to the same host, registering the same static kpp_alg structure multiple times corrupts internal fields used by the crypto core's algorithm list. This leads to immediate list corruption or kernel panics.
Additionally, removing an individual device via sysfs while active crypto transformations (TFMs) are running triggers a use-after-free (UAF) bug. Because the device driver core lacks unbind error-handling paths, the underlying memory allocated via devm for the i2c_priv structure is freed unconditionally, leaving active transformation context pointers dangling.
Fix these problems by implementing a centralized subsystem tracking matrix:
1. Introduce a global subsystem mutex and reference counter to ensure that the static 'atmel_ecdh_nist_p256' structure is only registered by the first probing device, and unregistered exclusively when the last supporting device unbinds.
2. Maintain per-device allocation tracking with 'tfm_count'. On remove, mark the device unready to halt load-balancing assignments, and block via a completion barrier until all pending transformation contexts bound to that specific physical hardware are freed.
3. Fix a critical re-registration race where a high-velocity unbind and subsequent re-probe cycles occur while crypto core asynchronous users are still purging. Establish a global 'atmel_ecc_unreg_active' state fence to force concurrent probing threads to execute a 2-second timeout bounded wait_for_completion_timeout() rather than unsafely mutating static lists.
--- drivers/crypto/atmel-ecc.c | 107 ++++++++++++++++++++++++++++--------- drivers/crypto/atmel-i2c.h | 1 + 2 files changed, 82 insertions(+), 26 deletions(-)
drivers/crypto/atmel-ecc.c +81 -26
@@ -23,6 +23,11 @@#include <crypto/kpp.h> #include "atmel-i2c.h" +static DEFINE_MUTEX (atmel_ecc_kpp_lock); +static int atmel_ecc_kpp_refcnt; +DECLARE_COMPLETION (atmel_ecc_unreg_done); +static bool atmel_ecc_unreg_active; +static struct atmel_ecc_driver_data driver_data;/**
@@ -243,7 +248,8 @@ static void atmel_ecc_i2c_client_free(struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client); -atomic_dec (&i2c_priv->tfm_count); +if (atomic_dec_and_test (&i2c_priv->tfm_count)) +complete (&i2c_priv->remove_done); }static int atmel_ecdh_init_tfm (struct crypto_kpp *tfm)
@@ -278,7 +284,8 @@ static void atmel_ecdh_exit_tfm(struct crypto_kpp *tfm)struct atmel_ecdh_ctx *ctx =kpp_tfm_ctx (tfm);kfree (ctx->public_key); -crypto_free_kpp (ctx->fallback); +if (ctx->fallback) +crypto_free_kpp (ctx->fallback);atmel_ecc_i2c_client_free (ctx->client); }
@@ -317,6 +324,7 @@ static struct kpp_alg atmel_ecdh_nist_p256 = {static int atmel_ecc_probe (struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv; +unsigned long timeout;int ret; ret =atmel_i2c_probe (client);
@@ -332,50 +340,97 @@ static int atmel_ecc_probe(struct i2c_client *client) i2c_priv->ready = true;spin_unlock (&driver_data.i2c_list_lock); - ret =crypto_register_kpp (&atmel_ecdh_nist_p256); -if (ret) { -spin_lock (&driver_data.i2c_list_lock); - i2c_priv->ready = false; -list_del (&i2c_priv->i2c_client_list_node); -spin_unlock (&driver_data.i2c_list_lock); +mutex_lock (&atmel_ecc_kpp_lock); +/* +* For cases where the same/last such device is still in unregistering, +* and now re-registering (refcnt is 0, but completion still exists). +* Safely capture the pointer, drop the lock and sleep until it +* terminates upon completion or retry limit reached. +*/ +while (atmel_ecc_unreg_active) { +mutex_unlock (&atmel_ecc_kpp_lock); + timeout =wait_for_completion_timeout (&atmel_ecc_unreg_done, +msecs_to_jiffies (2000 )); +mutex_lock (&atmel_ecc_kpp_lock); + +if (timeout ==0 ) { +spin_lock (&driver_data.i2c_list_lock); + i2c_priv->ready = false; +list_del (&i2c_priv->i2c_client_list_node); +spin_unlock (&driver_data.i2c_list_lock); +mutex_unlock (&atmel_ecc_kpp_lock); + +dev_err (&client->dev,"probe timed out, former driver instance not fully deregistered\n" ); +return -ETIMEDOUT ; + } + } -dev_err (&client->dev,"%s alg registration failed\n" , - atmel_ecdh_nist_p256.base.cra_driver_name); -return ret; - }else { -dev_info (&client->dev,"atmel ecc algorithms registered in /proc/crypto\n" ); +if (atmel_ecc_kpp_refcnt ==0 ) { + ret =crypto_register_kpp (&atmel_ecdh_nist_p256); +if (ret) { +spin_lock (&driver_data.i2c_list_lock); + i2c_priv->ready = false; +list_del (&i2c_priv->i2c_client_list_node); +spin_unlock (&driver_data.i2c_list_lock); +mutex_unlock (&atmel_ecc_kpp_lock); + +dev_err (&client->dev,"%s alg registration failed\n" , + atmel_ecdh_nist_p256.base.cra_driver_name); +return ret; + } } + atmel_ecc_kpp_refcnt++; +mutex_unlock (&atmel_ecc_kpp_lock); +dev_info (&client->dev,"atmel ecc algorithms registered in /proc/crypto\n" );return ret; }static void atmel_ecc_remove (struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client); +bool trigger_unreg = false;spin_lock (&driver_data.i2c_list_lock); i2c_priv->ready = false;spin_unlock (&driver_data.i2c_list_lock); -/* Return EBUSY if i2c client already allocated. */ -if (atomic_read (&i2c_priv->tfm_count)) { -/* -* After we return here, the memory backing the device is freed. -* That happens no matter what the return value of this function -* is because in the Linux device model there is no error -* handling for unbinding a driver. -* If there is still some action pending, it probably involves -* accessing the freed memory. -*/ -dev_emerg (&client->dev,"Device is busy, expect memory corruption.\n" ); -return ; +/* +* The Linux crypto core automatically blocks until all active +* transformations utilizing that specific algorithm structure +* are fully freed and closed. +*/ +mutex_lock (&atmel_ecc_kpp_lock); + atmel_ecc_kpp_refcnt--; + +if (atmel_ecc_kpp_refcnt ==0 ) { + trigger_unreg = true; + atmel_ecc_unreg_active = true; +reinit_completion (&atmel_ecc_unreg_done); } +mutex_unlock (&atmel_ecc_kpp_lock); -crypto_unregister_kpp (&atmel_ecdh_nist_p256); +if (atomic_read (&i2c_priv->tfm_count)) +wait_for_completion (&i2c_priv->remove_done);spin_lock (&driver_data.i2c_list_lock);list_del (&i2c_priv->i2c_client_list_node);spin_unlock (&driver_data.i2c_list_lock); + +/* +* The driver registers once an algorithm, but maintains a list of +* supporting i2c devices. Unregister the algorithm only, when the last +* supporting device deregisters. Use completions to assure no inflight +* TFMs and/or re-registering driver probe will then loose memory +* by over initializing the global statics. +*/ +if (trigger_unreg) { +crypto_unregister_kpp (&atmel_ecdh_nist_p256); +mutex_lock (&atmel_ecc_kpp_lock); + atmel_ecc_unreg_active = false; +complete_all (&atmel_ecc_unreg_done); +mutex_unlock (&atmel_ecc_kpp_lock); + } }static const struct of_device_id atmel_ecc_dt_ids[] = {
drivers/crypto/atmel-i2c.h +1 -0
@@ -146,6 +146,7 @@ struct atmel_i2c_client_priv {size_t wake_token_sz;atomic_t tfm_count____cacheline_aligned ;struct hwrng hwrng; +struct completion remove_done;bool ready; };
Signature · 1 line
2.39.5
PATCH v4 3/12 crypto: atmel-sha204a - fix heap info leak on I2C transfer failure
In reply to Lothar RubuschWhen a non-blocking read operation is requested, the driver dynamically allocates memory to track asynchronous transfer status. If the underlying I2C transmission fails, atmel_sha204a_rng_done() logs a rate-limited warning but incorrectly proceeds to cache the pointer to this uninitialized buffer inside the rng->priv data field anyway.
On subsequent execution passes, atmel_sha204a_rng_read_nonblocking() detects the stale rng->priv value, skips executing a hardware data read, and copies up to 32 bytes of uninitialized kernel heap data from this garbage memory pool straight back into the system's hwrng data stream.
Fix this information disclosure vector by immediately releasing the allocated asynchronous work data buffer and explicitly clearing the tracking pointer context whenever an I2C transaction returns a non-zero error status.
Additionally, duplicate the tfm counter decrement within the new error path to ensure the reference counter is properly released before executing the early return, maintaining the driver's availability for subsequent requests.
--- drivers/crypto/atmel-sha204a.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/crypto/atmel-sha204a.c +6 -1
@@ -31,10 +31,15 @@ static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;struct hwrng *rng = areq; -if (status) +if (status) {dev_warn_ratelimited (&i2c_priv->client->dev,"i2c transaction failed (%d)\n" , status); +kfree (work_data); + rng->priv =0 ; +atomic_dec (&i2c_priv->tfm_count); +return ; + } rng->priv = (unsigned long )work_data;atomic_dec (&i2c_priv->tfm_count);
Signature · 1 line
2.39.5
PATCH v4 4/12 crypto: atmel-ecc - rename driver_data before moving it into atmel-i2c
In reply to Lothar RubuschRename the local driver_data instance to atmel_i2c_mgmt in preparation for moving the shared I2C client management infrastructure into the atmel-i2c core driver in a subsequent change.
No functional changes intended.
--- drivers/crypto/atmel-ecc.c | 39 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 20 deletions(-)
drivers/crypto/atmel-ecc.c +19 -20
@@ -28,7 +28,7 @@ static int atmel_ecc_kpp_refcnt;DECLARE_COMPLETION (atmel_ecc_unreg_done);static bool atmel_ecc_unreg_active; -static struct atmel_ecc_driver_data driver_data; +static struct atmel_ecc_driver_data atmel_i2c_mgmt;/** * struct atmel_ecdh_ctx - transformation context
@@ -214,14 +214,14 @@ static struct i2c_client *atmel_ecc_i2c_client_alloc(void)int min_tfm_cnt =INT_MAX ;int tfm_cnt; -spin_lock (&driver_data.i2c_list_lock); +spin_lock (&atmel_i2c_mgmt.i2c_list_lock); -if (list_empty (&driver_data.i2c_client_list)) { -spin_unlock (&driver_data.i2c_list_lock); +if (list_empty (&atmel_i2c_mgmt.i2c_client_list)) { +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock);return ERR_PTR (-ENODEV ); } -list_for_each_entry (i2c_priv, &driver_data.i2c_client_list, +list_for_each_entry (i2c_priv, &atmel_i2c_mgmt.i2c_client_list, i2c_client_list_node) {if (!i2c_priv->ready)continue ;
@@ -239,7 +239,7 @@ static struct i2c_client *atmel_ecc_i2c_client_alloc(void) client = min_i2c_priv->client; } -spin_unlock (&driver_data.i2c_list_lock); +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock);return client; }
@@ -334,11 +334,11 @@ static int atmel_ecc_probe(struct i2c_client *client) i2c_priv =i2c_get_clientdata (client); i2c_priv->ready = false; -spin_lock (&driver_data.i2c_list_lock); +spin_lock (&atmel_i2c_mgmt.i2c_list_lock);list_add_tail (&i2c_priv->i2c_client_list_node, - &driver_data.i2c_client_list); + &atmel_i2c_mgmt.i2c_client_list); i2c_priv->ready = true; -spin_unlock (&driver_data.i2c_list_lock); +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock);mutex_lock (&atmel_ecc_kpp_lock);/*
@@ -352,12 +352,11 @@ static int atmel_ecc_probe(struct i2c_client *client) timeout =wait_for_completion_timeout (&atmel_ecc_unreg_done,msecs_to_jiffies (2000 ));mutex_lock (&atmel_ecc_kpp_lock); -if (timeout ==0 ) { -spin_lock (&driver_data.i2c_list_lock); +spin_lock (&atmel_i2c_mgmt.i2c_list_lock); i2c_priv->ready = false;list_del (&i2c_priv->i2c_client_list_node); -spin_unlock (&driver_data.i2c_list_lock); +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock);mutex_unlock (&atmel_ecc_kpp_lock);dev_err (&client->dev,"probe timed out, former driver instance not fully deregistered\n" );
@@ -368,10 +367,10 @@ static int atmel_ecc_probe(struct i2c_client *client)if (atmel_ecc_kpp_refcnt ==0 ) { ret =crypto_register_kpp (&atmel_ecdh_nist_p256);if (ret) { -spin_lock (&driver_data.i2c_list_lock); +spin_lock (&atmel_i2c_mgmt.i2c_list_lock); i2c_priv->ready = false;list_del (&i2c_priv->i2c_client_list_node); -spin_unlock (&driver_data.i2c_list_lock); +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock);mutex_unlock (&atmel_ecc_kpp_lock); dev_err(&client->dev, "%s alg registration failed\n",
@@ -391,9 +390,9 @@ static void atmel_ecc_remove(struct i2c_client *client)struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client);bool trigger_unreg = false; -spin_lock (&driver_data.i2c_list_lock); +spin_lock (&atmel_i2c_mgmt.i2c_list_lock); i2c_priv->ready = false; -spin_unlock (&driver_data.i2c_list_lock); +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock);/* * The Linux crypto core automatically blocks until all active
@@ -413,9 +412,9 @@ static void atmel_ecc_remove(struct i2c_client *client)if (atomic_read (&i2c_priv->tfm_count))wait_for_completion (&i2c_priv->remove_done); -spin_lock (&driver_data.i2c_list_lock); +spin_lock (&atmel_i2c_mgmt.i2c_list_lock);list_del (&i2c_priv->i2c_client_list_node); -spin_unlock (&driver_data.i2c_list_lock); +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock);/* * The driver registers once an algorithm, but maintains a list of
@@ -459,8 +458,8 @@ static struct i2c_driver atmel_ecc_driver = {static int __init atmel_ecc_init (void ) { -spin_lock_init (&driver_data.i2c_list_lock); -INIT_LIST_HEAD (&driver_data.i2c_client_list); +spin_lock_init (&atmel_i2c_mgmt.i2c_list_lock); +INIT_LIST_HEAD (&atmel_i2c_mgmt.i2c_client_list);return i2c_add_driver (&atmel_ecc_driver); }
Signature · 1 line
2.39.5
PATCH v4 5/12 crypto: atmel - rename atmel_ecc_driver_data to atmel_i2c_client_mgmt
In reply to Lothar RubuschRename struct atmel_ecc_driver_data to atmel_i2c_client_mgmt to reflect its generic role in shared I2C client tracking and locking. A subsequent change will move the client management infrastructure into the atmel-i2c core driver.
No functional changes intended.
--- drivers/crypto/atmel-ecc.c | 2 +- drivers/crypto/atmel-i2c.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/atmel-ecc.c +1 -1
@@ -28,7 +28,7 @@ static int atmel_ecc_kpp_refcnt;DECLARE_COMPLETION (atmel_ecc_unreg_done);static bool atmel_ecc_unreg_active; -static struct atmel_ecc_driver_data atmel_i2c_mgmt; +static struct atmel_i2c_client_mgmt atmel_i2c_mgmt;/** * struct atmel_ecdh_ctx - transformation context
drivers/crypto/atmel-i2c.h +1 -1
@@ -115,7 +115,7 @@ struct atmel_i2c_cmd {#define ECDH_PREFIX_MODE 0x00/* Used for binding tfm objects to i2c clients. */ -struct atmel_ecc_driver_data { +struct atmel_i2c_client_mgmt {struct list_head i2c_client_list;spinlock_t i2c_list_lock; }____cacheline_aligned ;
Signature · 1 line
2.39.5
PATCH v4 6/12 crypto: atmel-i2c - move client management instance into core
In reply to Lothar RubuschMove the global 'atmel_i2c_mgmt' tracking instance out of the ECC driver and into the atmel-i2c core library.
This change consolidates the shared I2C client infrastructure into a central core driver. This centralization allows both the ECC and upcoming SHA204A driver modules to access and reference a unified, common device-management context.
As part of this relocation, replace the explicit runtime initialization calls inside the module init block with static, compile-time macros (__SPIN_LOCK_UNLOCKED and LIST_HEAD_INIT). Export the tracking structure via EXPORT_SYMBOL_GPL() to make it available to dependent sub-modules.
No functional change intended.
--- drivers/crypto/atmel-ecc.c | 4 ---- drivers/crypto/atmel-i2c.c | 6 ++++++ drivers/crypto/atmel-i2c.h | 1 + 3 files changed, 7 insertions(+), 4 deletions(-)
drivers/crypto/atmel-ecc.c +0 -4
@@ -28,8 +28,6 @@ static int atmel_ecc_kpp_refcnt;DECLARE_COMPLETION (atmel_ecc_unreg_done);static bool atmel_ecc_unreg_active; -static struct atmel_i2c_client_mgmt atmel_i2c_mgmt; -/** * struct atmel_ecdh_ctx - transformation context * @client : pointer to i2c client device
@@ -458,8 +456,6 @@ static struct i2c_driver atmel_ecc_driver = {static int __init atmel_ecc_init (void ) { -spin_lock_init (&atmel_i2c_mgmt.i2c_list_lock); -INIT_LIST_HEAD (&atmel_i2c_mgmt.i2c_client_list);return i2c_add_driver (&atmel_ecc_driver); }
drivers/crypto/atmel-i2c.c +6 -0
@@ -21,6 +21,12 @@#include <linux/workqueue.h> #include "atmel-i2c.h" +struct atmel_i2c_client_mgmt atmel_i2c_mgmt = { + .i2c_list_lock =__SPIN_LOCK_UNLOCKED (atmel_i2c_mgmt.i2c_list_lock), + .i2c_client_list =LIST_HEAD_INIT (atmel_i2c_mgmt.i2c_client_list), +}; +EXPORT_SYMBOL_GPL (atmel_i2c_mgmt); + static const struct { u8 value; const char *error_text;
drivers/crypto/atmel-i2c.h +1 -0
@@ -119,6 +119,7 @@ struct atmel_i2c_client_mgmt {struct list_head i2c_client_list;spinlock_t i2c_list_lock; }____cacheline_aligned ; +extern struct atmel_i2c_client_mgmt atmel_i2c_mgmt;/** * atmel_i2c_client_priv - i2c_client private data
Signature · 1 line
2.39.5
PATCH v4 7/12 crypto: atmel-i2c - introduce shared teardown helpers and fix queue flush
In reply to Lothar RubuschIntroduce atmel_i2c_deactivate_client() and atmel_i2c_unregister_client() helpers in the atmel-i2c core library to modularize client teardown. This encapsulates common client state tracking and list manipulation operations.
Convert the ECC driver's error recovery and device removal paths to utilize these new helpers, ensuring consistent execution ordering when modifying device-readiness states and deleting linked-list nodes.
Additionally, migrate the atmel_i2c_flush_queue() call out of the module exit path. It now runs inside the core unregistration helper. Export both new tracking symbols via EXPORT_SYMBOL_GPL() to match the existing core driver licensing standard.
--- drivers/crypto/atmel-ecc.c | 25 ++++++------------------- drivers/crypto/atmel-i2c.c | 20 ++++++++++++++++++++ drivers/crypto/atmel-i2c.h | 3 +++ 3 files changed, 29 insertions(+), 19 deletions(-)
drivers/crypto/atmel-ecc.c +6 -19
@@ -351,12 +351,8 @@ static int atmel_ecc_probe(struct i2c_client *client) msecs_to_jiffies(2000));mutex_lock (&atmel_ecc_kpp_lock);if (timeout ==0 ) { -spin_lock (&atmel_i2c_mgmt.i2c_list_lock); - i2c_priv->ready = false; -list_del (&i2c_priv->i2c_client_list_node); -spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); -mutex_unlock (&atmel_ecc_kpp_lock); - +atmel_i2c_deactivate_client (i2c_priv); +atmel_i2c_unregister_client (i2c_priv);dev_err (&client->dev,"probe timed out, former driver instance not fully deregistered\n" );return -ETIMEDOUT ; }
@@ -365,12 +361,8 @@ static int atmel_ecc_probe(struct i2c_client *client)if (atmel_ecc_kpp_refcnt ==0 ) { ret =crypto_register_kpp (&atmel_ecdh_nist_p256);if (ret) { -spin_lock (&atmel_i2c_mgmt.i2c_list_lock); - i2c_priv->ready = false; -list_del (&i2c_priv->i2c_client_list_node); -spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); -mutex_unlock (&atmel_ecc_kpp_lock); - +atmel_i2c_deactivate_client (i2c_priv); +atmel_i2c_unregister_client (i2c_priv);dev_err (&client->dev,"%s alg registration failed\n" , atmel_ecdh_nist_p256.base.cra_driver_name);return ret;
@@ -388,9 +380,7 @@ static void atmel_ecc_remove(struct i2c_client *client)struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client);bool trigger_unreg = false; -spin_lock (&atmel_i2c_mgmt.i2c_list_lock); - i2c_priv->ready = false; -spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); +atmel_i2c_deactivate_client (i2c_priv);/* * The Linux crypto core automatically blocks until all active
@@ -410,9 +400,7 @@ static void atmel_ecc_remove(struct i2c_client *client)if (atomic_read (&i2c_priv->tfm_count))wait_for_completion (&i2c_priv->remove_done); -spin_lock (&atmel_i2c_mgmt.i2c_list_lock); -list_del (&i2c_priv->i2c_client_list_node); -spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); +atmel_i2c_unregister_client (i2c_priv);/* * The driver registers once an algorithm, but maintains a list of
@@ -461,7 +449,6 @@ static int __init atmel_ecc_init(void) static void __exit atmel_ecc_exit(void) { -atmel_i2c_flush_queue ();i2c_del_driver (&atmel_ecc_driver); }
drivers/crypto/atmel-i2c.c +20 -0
@@ -354,6 +354,26 @@ static int device_sanity_check(struct i2c_client *client)return ret; } +void atmel_i2c_deactivate_client (struct atmel_i2c_client_priv *i2c_priv) +{ +spin_lock (&atmel_i2c_mgmt.i2c_list_lock); + i2c_priv->ready = false; +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); +} +EXPORT_SYMBOL_GPL (atmel_i2c_deactivate_client); + +void atmel_i2c_unregister_client (struct atmel_i2c_client_priv *i2c_priv) +{ +spin_lock (&atmel_i2c_mgmt.i2c_list_lock); +if (!list_empty (&i2c_priv->i2c_client_list_node)) +list_del_init (&i2c_priv->i2c_client_list_node); +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); + +/* don't sleep inside spin locks */ +atmel_i2c_flush_queue (); +} +EXPORT_SYMBOL_GPL (atmel_i2c_unregister_client); +int atmel_i2c_probe (struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv;
drivers/crypto/atmel-i2c.h +3 -0
@@ -193,4 +193,7 @@ void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid);int atmel_i2c_init_ecdh_cmd (struct atmel_i2c_cmd *cmd,struct scatterlist *pubkey); +void atmel_i2c_deactivate_client (struct atmel_i2c_client_priv *i2c_priv); +void atmel_i2c_unregister_client (struct atmel_i2c_client_priv *i2c_priv); +#endif /* __ATMEL_I2C_H__ */
Signature · 1 line
2.39.5
PATCH v4 8/12 crypto: atmel-ecc - switch to module_i2c_driver
In reply to Lothar RubuschRemove custom boilerplate module configuration code and convert the module init/exit paths to use the modern module_i2c_driver() helper macro.
This shortens and simplifies driver initialization. Custom structure setup is no longer required here since management tracking context initialization was already safely moved into the atmel-i2c core library module.
--- drivers/crypto/atmel-ecc.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/crypto/atmel-ecc.c +1 -12
@@ -442,18 +442,7 @@ static struct i2c_driver atmel_ecc_driver = { .id_table = atmel_ecc_id, }; -static int __init atmel_ecc_init (void ) -{ -return i2c_add_driver (&atmel_ecc_driver); -} - -static void __exit atmel_ecc_exit(void) -{ -i2c_del_driver (&atmel_ecc_driver); -} - -module_init (atmel_ecc_init); -module_exit (atmel_ecc_exit); +module_i2c_driver (atmel_ecc_driver);MODULE_AUTHOR ("Tudor Ambarus" );MODULE_DESCRIPTION ("Microchip / Atmel ECC (I2C) driver" );
Signature · 1 line
2.39.5
PATCH v4 9/12 crypto: atmel-i2c - move shared client allocation logic to core
In reply to Lothar RubuschMigrate the I2C client allocation and runtime load-balancing routines out of the ECC driver code and into the central atmel-i2c core library module.
Export the symmetric lifecycle helper interfaces atmel_i2c_client_alloc() and atmel_i2c_client_free() using EXPORT_SYMBOL_GPL() to expose a unified client management API. This consolidation enables the dynamic selection subsystem (which chooses the least-loaded client device based on the active transformation count) to be shared by both the ECC driver and upcoming Atmel crypto modules.
Refactor the ECC driver's transformation context initialization (init_tfm) and teardown (exit_tfm) paths to use this centralized core API.
No functional change is intended.
--- drivers/crypto/atmel-ecc.c | 51 +++----------------------------------- drivers/crypto/atmel-i2c.c | 47 +++++++++++++++++++++++++++++++++++ drivers/crypto/atmel-i2c.h | 3 +++ 3 files changed, 53 insertions(+), 48 deletions(-)
drivers/crypto/atmel-ecc.c +3 -48
@@ -205,51 +205,6 @@ static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)return ret; } -static struct i2c_client *atmel_ecc_i2c_client_alloc (void ) -{ -struct atmel_i2c_client_priv *i2c_priv, *min_i2c_priv =NULL ; -struct i2c_client *client =ERR_PTR (-ENODEV ); -int min_tfm_cnt =INT_MAX ; -int tfm_cnt; - -spin_lock (&atmel_i2c_mgmt.i2c_list_lock); - -if (list_empty (&atmel_i2c_mgmt.i2c_client_list)) { -spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); -return ERR_PTR (-ENODEV ); - } - -list_for_each_entry (i2c_priv, &atmel_i2c_mgmt.i2c_client_list, - i2c_client_list_node) { -if (!i2c_priv->ready) -continue ; - tfm_cnt =atomic_read (&i2c_priv->tfm_count); -if (tfm_cnt < min_tfm_cnt) { - min_tfm_cnt = tfm_cnt; - min_i2c_priv = i2c_priv; - } -if (!min_tfm_cnt) -break ; - } - -if (min_i2c_priv) { -atomic_inc (&min_i2c_priv->tfm_count); - client = min_i2c_priv->client; - } - -spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); - -return client; -} - -static void atmel_ecc_i2c_client_free (struct i2c_client *client) -{ -struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client); - -if (atomic_dec_and_test (&i2c_priv->tfm_count)) -complete (&i2c_priv->remove_done); -} -static int atmel_ecdh_init_tfm (struct crypto_kpp *tfm) {const char *alg =kpp_alg_name (tfm);
@@ -257,7 +212,7 @@ static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)struct atmel_ecdh_ctx *ctx =kpp_tfm_ctx (tfm); ctx->curve_id =ECC_CURVE_NIST_P256 ; - ctx->client =atmel_ecc_i2c_client_alloc (); + ctx->client =atmel_i2c_client_alloc ();if (IS_ERR (ctx->client)) {pr_err ("tfm - i2c_client binding failed\n" );return PTR_ERR (ctx->client);
@@ -267,7 +222,7 @@ static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)if (IS_ERR (fallback)) {dev_err (&ctx->client->dev,"Failed to allocate transformation for '%s': %ld\n" , alg,PTR_ERR (fallback)); -atmel_ecc_i2c_client_free (ctx->client); +atmel_i2c_client_free (ctx->client);return PTR_ERR (fallback); }
@@ -284,7 +239,7 @@ static void atmel_ecdh_exit_tfm(struct crypto_kpp *tfm)kfree (ctx->public_key);if (ctx->fallback)crypto_free_kpp (ctx->fallback); -atmel_ecc_i2c_client_free (ctx->client); +atmel_i2c_client_free (ctx->client); }static unsigned int atmel_ecdh_max_size (struct crypto_kpp *tfm)
drivers/crypto/atmel-i2c.c +47 -0
53 lines not on this page · raw message
drivers/crypto/atmel-i2c.h +3 -0
@@ -193,6 +193,9 @@ void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid);int atmel_i2c_init_ecdh_cmd (struct atmel_i2c_cmd *cmd,struct scatterlist *pubkey); +struct i2c_client *atmel_i2c_client_alloc (void ); +void atmel_i2c_client_free (struct i2c_client *client); +void atmel_i2c_deactivate_client (struct atmel_i2c_client_priv *i2c_priv);void atmel_i2c_unregister_client (struct atmel_i2c_client_priv *i2c_priv);
Signature · 1 line
2.39.5
PATCH v4 10/12 crypto: atmel-i2c - implement capability-based client selection
In reply to Lothar RubuschExtend the shared I2C client allocation interface to support feature-aware hardware selection by introducing capability filtering.
Add a 'caps' mask to 'struct atmel_i2c_client_priv' alongside an 'atmel_i2c_capability' enum. The allocator now explicitly filters hardware nodes by a requested capability bit while retaining the least-loaded device load-balancing scheme.
Update the ECC driver to advertise ATMEL_CAP_ECDH configuration capability during probe, and adapt the tfm context setup execution path to request this specific capability variant. Initialize the bitmask field to zero inside the SHA204A driver context for now.
--- drivers/crypto/atmel-ecc.c | 4 +++- drivers/crypto/atmel-i2c.c | 6 +++++- drivers/crypto/atmel-i2c.h | 8 +++++++- drivers/crypto/atmel-sha204a.c | 2 ++ 4 files changed, 17 insertions(+), 3 deletions(-)
drivers/crypto/atmel-ecc.c +3 -1
@@ -212,7 +212,7 @@ static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)struct atmel_ecdh_ctx *ctx =kpp_tfm_ctx (tfm); ctx->curve_id =ECC_CURVE_NIST_P256 ; - ctx->client =atmel_i2c_client_alloc (); + ctx->client =atmel_i2c_client_alloc (ATMEL_CAP_ECDH );if (IS_ERR (ctx->client)) {pr_err ("tfm - i2c_client binding failed\n" );return PTR_ERR (ctx->client);
@@ -287,6 +287,8 @@ static int atmel_ecc_probe(struct i2c_client *client) i2c_priv =i2c_get_clientdata (client); i2c_priv->ready = false; + i2c_priv->caps =BIT (ATMEL_CAP_ECDH ); +spin_lock (&atmel_i2c_mgmt.i2c_list_lock);list_add_tail (&i2c_priv->i2c_client_list_node, &atmel_i2c_mgmt.i2c_client_list);
drivers/crypto/atmel-i2c.c +5 -1
@@ -57,7 +57,7 @@ static void atmel_i2c_checksum(struct atmel_i2c_cmd *cmd) *__crc16 =cpu_to_le16 (bitrev16 (crc16 (0 , data, len))); } -struct i2c_client *atmel_i2c_client_alloc (void ) +struct i2c_client *atmel_i2c_client_alloc (enum atmel_i2c_capability cap) {struct atmel_i2c_client_priv *i2c_priv, *min_i2c_priv =NULL ;struct i2c_client *client =ERR_PTR (-ENODEV );
@@ -75,6 +75,10 @@ struct i2c_client *atmel_i2c_client_alloc(void) i2c_client_list_node) {if (!i2c_priv->ready)continue ; + +if (!(i2c_priv->caps &BIT (cap))) +continue ; + tfm_cnt =atomic_read (&i2c_priv->tfm_count);if (tfm_cnt < min_tfm_cnt) { min_tfm_cnt = tfm_cnt;
drivers/crypto/atmel-i2c.h +7 -1
@@ -115,6 +115,10 @@ struct atmel_i2c_cmd {#define ECDH_PREFIX_MODE 0x00/* Used for binding tfm objects to i2c clients. */ +enum atmel_i2c_capability { +ATMEL_CAP_ECDH =0 , +}; +struct atmel_i2c_client_mgmt {struct list_head i2c_client_list;spinlock_t i2c_list_lock;
@@ -131,6 +135,7 @@ extern struct atmel_i2c_client_mgmt atmel_i2c_mgmt;* @tfm_count : number of active crypto transformations on i2c client * @hwrng : hold the hardware generated rng * @ready : hw client is ready to use +* @caps : feature capability of the particular driver * * Reads and writes from/to the i2c client are sequential. The first byte * transmitted to the device is treated as the byte size. Any attempt to send
@@ -149,6 +154,7 @@ struct atmel_i2c_client_priv {struct hwrng hwrng;struct completion remove_done;bool ready; +u32 caps; };/**
@@ -193,7 +199,7 @@ void atmel_i2c_init_genkey_cmd(struct atmel_i2c_cmd *cmd, u16 keyid);int atmel_i2c_init_ecdh_cmd (struct atmel_i2c_cmd *cmd,struct scatterlist *pubkey); -struct i2c_client *atmel_i2c_client_alloc (void ); +struct i2c_client *atmel_i2c_client_alloc (enum atmel_i2c_capability cap);void atmel_i2c_client_free (struct i2c_client *client);void atmel_i2c_deactivate_client (struct atmel_i2c_client_priv *i2c_priv);
drivers/crypto/atmel-sha204a.c +2 -0
@@ -178,6 +178,8 @@ static int atmel_sha204a_probe(struct i2c_client *client) i2c_priv =i2c_get_clientdata (client); + i2c_priv->caps =0 ; +memset (&i2c_priv->hwrng,0 ,sizeof (i2c_priv->hwrng)); i2c_priv->hwrng.name =dev_name (&client->dev);
Signature · 1 line
2.39.5
PATCH v4 11/12 crypto: atmel-sha204a - integrate into core management tracking
In reply to Lothar RubuschRegister the SHA204A I2C device instance into the shared atmel_i2c client management tracking list during the probe phase. This allows the driver to participate in the central hardware selection infrastructure.
Rework the error-unwind paths inside atmel_sha204a_probe() to prevent stale entries from remaining in the global tracking structures if a partial initialization failure occurs. If sysfs group creation fails, explicitly trigger devm_hwrng_unregister() to preserve the strict lifecycle ordering introduced in previous stability fixes.
Convert the removal path to use the core teardown helpers. Ensure the device readiness state is deactivated using atmel_i2c_deactivate_client() and the tracking node is removed via atmel_i2c_unregister_client() before local memory resources are freed. This guarantees that any in-flight work queue items are unconditionally flushed, eliminating a potential Use-After-Free (UAF) window during device removal.
No functional change intended beyond improved lifecycle handling.
--- drivers/crypto/atmel-sha204a.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
drivers/crypto/atmel-sha204a.c +22 -4
@@ -177,9 +177,15 @@ static int atmel_sha204a_probe(struct i2c_client *client)return ret; i2c_priv =i2c_get_clientdata (client); + i2c_priv->ready = false; i2c_priv->caps =0 ; +spin_lock (&atmel_i2c_mgmt.i2c_list_lock); +list_add_tail (&i2c_priv->i2c_client_list_node, + &atmel_i2c_mgmt.i2c_client_list); +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); +memset (&i2c_priv->hwrng,0 ,sizeof (i2c_priv->hwrng)); i2c_priv->hwrng.name =dev_name (&client->dev);
@@ -192,15 +198,26 @@ static int atmel_sha204a_probe(struct i2c_client *client) ret =devm_hwrng_register (&client->dev, &i2c_priv->hwrng);if (ret) {dev_err (&client->dev,"failed to register RNG (%d)\n" , ret); -return ret; + goto err_list_del; } ret =sysfs_create_group (&client->dev.kobj, &atmel_sha204a_groups);if (ret) {dev_err (&client->dev,"failed to create sysfs group (%d)\n" , ret); -return ret; + goto err_hwrng_unregister; } +spin_lock (&atmel_i2c_mgmt.i2c_list_lock); + i2c_priv->ready = true; +spin_unlock (&atmel_i2c_mgmt.i2c_list_lock); + +return 0 ; + +err_hwrng_unregister: +devm_hwrng_unregister (&client->dev, &i2c_priv->hwrng); +err_list_del: +atmel_i2c_unregister_client (i2c_priv); +return ret; }
@@ -208,9 +225,11 @@ static void atmel_sha204a_remove(struct i2c_client *client) {struct atmel_i2c_client_priv *i2c_priv =i2c_get_clientdata (client); +atmel_i2c_deactivate_client (i2c_priv); +sysfs_remove_group (&client->dev.kobj, &atmel_sha204a_groups);devm_hwrng_unregister (&client->dev, &i2c_priv->hwrng); -atmel_i2c_flush_queue (); +atmel_i2c_unregister_client (i2c_priv);kfree ((void *)i2c_priv->hwrng.priv); }
@@ -245,7 +264,6 @@ static int __init atmel_sha204a_init(void) static void __exit atmel_sha204a_exit(void) { -atmel_i2c_flush_queue ();i2c_del_driver (&atmel_sha204a_driver); }
Signature · 1 line
2.39.5
PATCH v4 12/12 crypto: atmel-sha204a - switch to module_i2c_driver
In reply to Lothar RubuschReplace explicit module init and exit boilerplate functions with the module_i2c_driver() macro helper to simplify the driver registration path.
--- drivers/crypto/atmel-sha204a.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/crypto/atmel-sha204a.c +1 -12
@@ -257,18 +257,7 @@ static struct i2c_driver atmel_sha204a_driver = { .driver.of_match_table = atmel_sha204a_dt_ids, }; -static int __init atmel_sha204a_init (void ) -{ -return i2c_add_driver (&atmel_sha204a_driver); -} - -static void __exit atmel_sha204a_exit(void) -{ -i2c_del_driver (&atmel_sha204a_driver); -} - -module_init (atmel_sha204a_init); -module_exit (atmel_sha204a_exit); +module_i2c_driver (atmel_sha204a_driver);MODULE_AUTHOR ("Ard Biesheuvel <[email protected]>" );MODULE_DESCRIPTION ("Microchip / Atmel SHA204A (I2C) driver" );
Signature · 1 line
2.39.5