Skip to content

Commit 1517f63

Browse files
ndreysherbertx
authored andcommitted
crypto: caam - drop global context pointer and init_done
Leverage devres to get rid of code storing global context as well as init_done flag. Original code also has a circular deallocation dependency where unregister_algs() -> caam_rng_exit() -> caam_jr_free() chain would only happen if all of JRs were freed. Fix this by moving caam_rng_exit() outside of unregister_algs() and doing it specifically for JR that instantiated HWRNG. Signed-off-by: Andrey Smirnov <[email protected]> Cc: Chris Healy <[email protected]> Cc: Lucas Stach <[email protected]> Cc: Horia Geantă <[email protected]> Cc: Herbert Xu <[email protected]> Cc: Iuliana Prodan <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Reviewed-by: Horia Geantă <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 8483c83 commit 1517f63

File tree

3 files changed

+44
-43
lines changed

3 files changed

+44
-43
lines changed

drivers/crypto/caam/caamrng.c

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct buf_data {
7070

7171
/* rng per-device context */
7272
struct caam_rng_ctx {
73+
struct hwrng rng;
7374
struct device *jrdev;
7475
dma_addr_t sh_desc_dma;
7576
u32 sh_desc[DESC_RNG_LEN];
@@ -78,13 +79,10 @@ struct caam_rng_ctx {
7879
struct buf_data bufs[2];
7980
};
8081

81-
static struct caam_rng_ctx *rng_ctx;
82-
83-
/*
84-
* Variable used to avoid double free of resources in case
85-
* algorithm registration was unsuccessful
86-
*/
87-
static bool init_done;
82+
static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r)
83+
{
84+
return (struct caam_rng_ctx *)r->priv;
85+
}
8886

8987
static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd)
9088
{
@@ -143,7 +141,7 @@ static inline int submit_job(struct caam_rng_ctx *ctx, int to_current)
143141

144142
static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait)
145143
{
146-
struct caam_rng_ctx *ctx = rng_ctx;
144+
struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
147145
struct buf_data *bd = &ctx->bufs[ctx->current_buf];
148146
int next_buf_idx, copied_idx;
149147
int err;
@@ -246,17 +244,18 @@ static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id)
246244

247245
static void caam_cleanup(struct hwrng *rng)
248246
{
247+
struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
249248
int i;
250249
struct buf_data *bd;
251250

252251
for (i = 0; i < 2; i++) {
253-
bd = &rng_ctx->bufs[i];
252+
bd = &ctx->bufs[i];
254253
if (atomic_read(&bd->empty) == BUF_PENDING)
255254
wait_for_completion(&bd->filled);
256255
}
257256

258-
rng_unmap_ctx(rng_ctx);
259-
caam_jr_free(rng_ctx->jrdev);
257+
rng_unmap_ctx(ctx);
258+
caam_jr_free(ctx->jrdev);
260259
}
261260

262261
static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)
@@ -277,7 +276,7 @@ static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id)
277276

278277
static int caam_init(struct hwrng *rng)
279278
{
280-
struct caam_rng_ctx *ctx = rng_ctx;
279+
struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
281280
int err;
282281

283282
ctx->jrdev = caam_jr_alloc();
@@ -309,28 +308,19 @@ static int caam_init(struct hwrng *rng)
309308
return err;
310309
}
311310

312-
static struct hwrng caam_rng = {
313-
.name = "rng-caam",
314-
.init = caam_init,
315-
.cleanup = caam_cleanup,
316-
.read = caam_read,
317-
};
311+
int caam_rng_init(struct device *ctrldev);
318312

319-
void caam_rng_exit(void)
313+
void caam_rng_exit(struct device *ctrldev)
320314
{
321-
if (!init_done)
322-
return;
323-
324-
hwrng_unregister(&caam_rng);
325-
kfree(rng_ctx);
315+
devres_release_group(ctrldev, caam_rng_init);
326316
}
327317

328318
int caam_rng_init(struct device *ctrldev)
329319
{
320+
struct caam_rng_ctx *ctx;
330321
u32 rng_inst;
331322
struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
332-
int err;
333-
init_done = false;
323+
int ret;
334324

335325
/* Check for an instantiated RNG before registration */
336326
if (priv->era < 10)
@@ -342,18 +332,27 @@ int caam_rng_init(struct device *ctrldev)
342332
if (!rng_inst)
343333
return 0;
344334

345-
rng_ctx = kmalloc(sizeof(*rng_ctx), GFP_DMA | GFP_KERNEL);
346-
if (!rng_ctx)
335+
if (!devres_open_group(ctrldev, caam_rng_init, GFP_KERNEL))
336+
return -ENOMEM;
337+
338+
ctx = devm_kzalloc(ctrldev, sizeof(*ctx), GFP_DMA | GFP_KERNEL);
339+
if (!ctx)
347340
return -ENOMEM;
348341

342+
ctx->rng.name = "rng-caam";
343+
ctx->rng.init = caam_init;
344+
ctx->rng.cleanup = caam_cleanup;
345+
ctx->rng.read = caam_read;
346+
ctx->rng.priv = (unsigned long)ctx;
347+
349348
dev_info(ctrldev, "registering rng-caam\n");
350349

351-
err = hwrng_register(&caam_rng);
352-
if (!err) {
353-
init_done = true;
354-
return err;
350+
ret = devm_hwrng_register(ctrldev, &ctx->rng);
351+
if (ret) {
352+
caam_rng_exit(ctrldev);
353+
return ret;
355354
}
356355

357-
kfree(rng_ctx);
358-
return err;
356+
devres_close_group(ctrldev, caam_rng_init);
357+
return 0;
359358
}

drivers/crypto/caam/intern.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct caam_drv_private_jr {
4747
struct caam_job_ring __iomem *rregs; /* JobR's register space */
4848
struct tasklet_struct irqtask;
4949
int irq; /* One per queue */
50+
bool hwrng;
5051

5152
/* Number of scatterlist crypt transforms active on the JobR */
5253
atomic_t tfm_count ____cacheline_aligned;
@@ -163,7 +164,7 @@ static inline void caam_pkc_exit(void)
163164
#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API
164165

165166
int caam_rng_init(struct device *dev);
166-
void caam_rng_exit(void);
167+
void caam_rng_exit(struct device *dev);
167168

168169
#else
169170

@@ -172,9 +173,7 @@ static inline int caam_rng_init(struct device *dev)
172173
return 0;
173174
}
174175

175-
static inline void caam_rng_exit(void)
176-
{
177-
}
176+
static inline void caam_rng_exit(struct device *dev) {}
178177

179178
#endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API */
180179

drivers/crypto/caam/jr.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ static struct jr_driver_data driver_data;
2727
static DEFINE_MUTEX(algs_lock);
2828
static unsigned int active_devs;
2929

30-
static void register_algs(struct device *dev)
30+
static void register_algs(struct caam_drv_private_jr *jrpriv,
31+
struct device *dev)
3132
{
3233
mutex_lock(&algs_lock);
3334

@@ -37,7 +38,7 @@ static void register_algs(struct device *dev)
3738
caam_algapi_init(dev);
3839
caam_algapi_hash_init(dev);
3940
caam_pkc_init(dev);
40-
caam_rng_init(dev);
41+
jrpriv->hwrng = !caam_rng_init(dev);
4142
caam_qi_algapi_init(dev);
4243

4344
algs_unlock:
@@ -53,7 +54,6 @@ static void unregister_algs(void)
5354

5455
caam_qi_algapi_exit();
5556

56-
caam_rng_exit();
5757
caam_pkc_exit();
5858
caam_algapi_hash_exit();
5959
caam_algapi_exit();
@@ -135,6 +135,9 @@ static int caam_jr_remove(struct platform_device *pdev)
135135
jrdev = &pdev->dev;
136136
jrpriv = dev_get_drvdata(jrdev);
137137

138+
if (jrpriv->hwrng)
139+
caam_rng_exit(jrdev->parent);
140+
138141
/*
139142
* Return EBUSY if job ring already allocated.
140143
*/
@@ -514,7 +517,7 @@ static int caam_jr_probe(struct platform_device *pdev)
514517
int error;
515518

516519
jrdev = &pdev->dev;
517-
jrpriv = devm_kmalloc(jrdev, sizeof(*jrpriv), GFP_KERNEL);
520+
jrpriv = devm_kzalloc(jrdev, sizeof(*jrpriv), GFP_KERNEL);
518521
if (!jrpriv)
519522
return -ENOMEM;
520523

@@ -590,7 +593,7 @@ static int caam_jr_probe(struct platform_device *pdev)
590593

591594
atomic_set(&jrpriv->tfm_count, 0);
592595

593-
register_algs(jrdev->parent);
596+
register_algs(jrpriv, jrdev->parent);
594597

595598
return 0;
596599
}

0 commit comments

Comments
 (0)