Commit 2c5e88dc authored by Andrey Smirnov's avatar Andrey Smirnov Committed by Herbert Xu

crypto: caam - simplify RNG implementation

Rework CAAM RNG implementation as follows:

- Make use of the fact that HWRNG supports partial reads and will
handle such cases gracefully by removing recursion in caam_read()

- Convert blocking caam_read() codepath to do a single blocking job
read directly into requested buffer, bypassing any intermediary
buffers

- Convert async caam_read() codepath into a simple single
reader/single writer FIFO use-case, thus simplifying concurrency
handling and delegating buffer read/write position management to KFIFO
subsystem.

- Leverage the same low level RNG data extraction code for both async
and blocking caam_read() scenarios, get rid of the shared job
descriptor and make non-shared one as a simple as possible (just
HEADER + ALGORITHM OPERATION + FIFO STORE)

- Split private context from DMA related memory, so that the former
could be allocated without GFP_DMA.

NOTE: On its face value this commit decreased throughput numbers
reported by

  dd if=/dev/hwrng of=/dev/null bs=1 count=100K [iflag=nonblock]

by about 15%, however commits that enable prediction resistance and
limit JR total size impact the performance so much and move the
bottleneck such as to make this regression irrelevant.

NOTE: On the bright side, this commit reduces RNG in kernel DMA buffer
memory usage from 2 x RN_BUF_SIZE (~256K) to 32K.
Signed-off-by: default avatarAndrey Smirnov <andrew.smirnov@gmail.com>
Reviewed-by: default avatarHoria Geantă <horia.geanta@nxp.com>
Cc: Chris Healy <cphealy@gmail.com>
Cc: Lucas Stach <l.stach@pengutronix.de>
Cc: Horia Geantă <horia.geanta@nxp.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Iuliana Prodan <iuliana.prodan@nxp.com>
Cc: linux-crypto@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-imx@nxp.com
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 1517f63c
...@@ -7,35 +7,12 @@ ...@@ -7,35 +7,12 @@
* *
* Based on caamalg.c crypto API driver. * Based on caamalg.c crypto API driver.
* *
* relationship between job descriptors to shared descriptors:
*
* --------------- --------------
* | JobDesc #0 |-------------------->| ShareDesc |
* | *(buffer 0) | |------------->| (generate) |
* --------------- | | (move) |
* | | (store) |
* --------------- | --------------
* | JobDesc #1 |------|
* | *(buffer 1) |
* ---------------
*
* A job desc looks like this:
*
* ---------------------
* | Header |
* | ShareDesc Pointer |
* | SEQ_OUT_PTR |
* | (output buffer) |
* ---------------------
*
* The SharedDesc never changes, and each job descriptor points to one of two
* buffers for each device, from which the data will be copied into the
* requested destination
*/ */
#include <linux/hw_random.h> #include <linux/hw_random.h>
#include <linux/completion.h> #include <linux/completion.h>
#include <linux/atomic.h> #include <linux/atomic.h>
#include <linux/kfifo.h>
#include "compat.h" #include "compat.h"
...@@ -45,38 +22,26 @@ ...@@ -45,38 +22,26 @@
#include "jr.h" #include "jr.h"
#include "error.h" #include "error.h"
#define CAAM_RNG_MAX_FIFO_STORE_SIZE U16_MAX
#define CAAM_RNG_FIFO_LEN SZ_32K /* Must be a multiple of 2 */
/* /*
* Maximum buffer size: maximum number of random, cache-aligned bytes that * Length of used descriptors, see caam_init_desc()
* will be generated and moved to seq out ptr (extlen not allowed)
*/ */
#define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \ #define CAAM_RNG_DESC_LEN (CAAM_CMD_SZ + \
L1_CACHE_BYTES) CAAM_CMD_SZ + \
CAAM_CMD_SZ + CAAM_PTR_SZ_MAX)
/* length of descriptors */
#define DESC_JOB_O_LEN (CAAM_CMD_SZ * 2 + CAAM_PTR_SZ_MAX * 2)
#define DESC_RNG_LEN (3 * CAAM_CMD_SZ)
/* Buffer, its dma address and lock */
struct buf_data {
u8 buf[RN_BUF_SIZE] ____cacheline_aligned;
dma_addr_t addr;
struct completion filled;
u32 hw_desc[DESC_JOB_O_LEN];
#define BUF_NOT_EMPTY 0
#define BUF_EMPTY 1
#define BUF_PENDING 2 /* Empty, but with job pending --don't submit another */
atomic_t empty;
};
/* rng per-device context */ /* rng per-device context */
struct caam_rng_ctx { struct caam_rng_ctx {
struct hwrng rng; struct hwrng rng;
struct device *jrdev; struct device *jrdev;
dma_addr_t sh_desc_dma; struct device *ctrldev;
u32 sh_desc[DESC_RNG_LEN]; void *desc_async;
unsigned int cur_buf_idx; void *desc_sync;
int current_buf; struct work_struct worker;
struct buf_data bufs[2]; struct kfifo fifo;
}; };
static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r) static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r)
...@@ -84,228 +49,153 @@ static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r) ...@@ -84,228 +49,153 @@ static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r)
return (struct caam_rng_ctx *)r->priv; return (struct caam_rng_ctx *)r->priv;
} }
static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd) static void caam_rng_done(struct device *jrdev, u32 *desc, u32 err,
void *context)
{ {
if (bd->addr) struct completion *done = context;
dma_unmap_single(jrdev, bd->addr, RN_BUF_SIZE,
DMA_FROM_DEVICE);
}
static inline void rng_unmap_ctx(struct caam_rng_ctx *ctx)
{
struct device *jrdev = ctx->jrdev;
if (ctx->sh_desc_dma)
dma_unmap_single(jrdev, ctx->sh_desc_dma,
desc_bytes(ctx->sh_desc), DMA_TO_DEVICE);
rng_unmap_buf(jrdev, &ctx->bufs[0]);
rng_unmap_buf(jrdev, &ctx->bufs[1]);
}
static void rng_done(struct device *jrdev, u32 *desc, u32 err, void *context)
{
struct buf_data *bd;
bd = container_of(desc, struct buf_data, hw_desc[0]);
if (err) if (err)
caam_jr_strstatus(jrdev, err); caam_jr_strstatus(jrdev, err);
atomic_set(&bd->empty, BUF_NOT_EMPTY); complete(done);
complete(&bd->filled);
/* Buffer refilled, invalidate cache */
dma_sync_single_for_cpu(jrdev, bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE);
print_hex_dump_debug("rng refreshed buf@: ", DUMP_PREFIX_ADDRESS, 16, 4,
bd->buf, RN_BUF_SIZE, 1);
} }
static inline int submit_job(struct caam_rng_ctx *ctx, int to_current) static u32 *caam_init_desc(u32 *desc, dma_addr_t dst_dma, int len)
{ {
struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)]; init_job_desc(desc, 0); /* + 1 cmd_sz */
struct device *jrdev = ctx->jrdev; /* Generate random bytes: + 1 cmd_sz */
u32 *desc = bd->hw_desc; append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG);
int err; /* Store bytes: + 1 cmd_sz + caam_ptr_sz */
append_fifo_store(desc, dst_dma, len, FIFOST_TYPE_RNGSTORE);
dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf)); print_hex_dump_debug("rng job desc@: ", DUMP_PREFIX_ADDRESS,
init_completion(&bd->filled); 16, 4, desc, desc_bytes(desc), 1);
err = caam_jr_enqueue(jrdev, desc, rng_done, ctx);
if (err != -EINPROGRESS)
complete(&bd->filled); /* don't wait on failed job*/
else
atomic_inc(&bd->empty); /* note if pending */
return err; return desc;
} }
static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait) static int caam_rng_read_one(struct device *jrdev,
void *dst, int len,
void *desc,
struct completion *done)
{ {
struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); dma_addr_t dst_dma;
struct buf_data *bd = &ctx->bufs[ctx->current_buf];
int next_buf_idx, copied_idx;
int err; int err;
if (atomic_read(&bd->empty)) { len = min_t(int, len, CAAM_RNG_MAX_FIFO_STORE_SIZE);
/* try to submit job if there wasn't one */
if (atomic_read(&bd->empty) == BUF_EMPTY) {
err = submit_job(ctx, 1);
/* if can't submit job, can't even wait */
if (err != -EINPROGRESS)
return 0;
}
/* no immediate data, so exit if not waiting */
if (!wait)
return 0;
/* waiting for pending job */
if (atomic_read(&bd->empty))
wait_for_completion(&bd->filled);
}
next_buf_idx = ctx->cur_buf_idx + max;
dev_dbg(ctx->jrdev, "%s: start reading at buffer %d, idx %d\n",
__func__, ctx->current_buf, ctx->cur_buf_idx);
/* if enough data in current buffer */ dst_dma = dma_map_single(jrdev, dst, len, DMA_FROM_DEVICE);
if (next_buf_idx < RN_BUF_SIZE) { if (dma_mapping_error(jrdev, dst_dma)) {
memcpy(data, bd->buf + ctx->cur_buf_idx, max); dev_err(jrdev, "unable to map destination memory\n");
ctx->cur_buf_idx = next_buf_idx; return -ENOMEM;
return max;
} }
/* else, copy what's left... */ init_completion(done);
copied_idx = RN_BUF_SIZE - ctx->cur_buf_idx; err = caam_jr_enqueue(jrdev,
memcpy(data, bd->buf + ctx->cur_buf_idx, copied_idx); caam_init_desc(desc, dst_dma, len),
ctx->cur_buf_idx = 0; caam_rng_done, done);
atomic_set(&bd->empty, BUF_EMPTY); if (err == -EINPROGRESS) {
wait_for_completion(done);
/* ...refill... */ err = 0;
submit_job(ctx, 1); }
/* and use next buffer */ dma_unmap_single(jrdev, dst_dma, len, DMA_FROM_DEVICE);
ctx->current_buf = !ctx->current_buf;
dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf);
/* since there already is some data read, don't wait */ return err ?: len;
return copied_idx + caam_read(rng, data + copied_idx,
max - copied_idx, false);
} }
static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx) static void caam_rng_fill_async(struct caam_rng_ctx *ctx)
{ {
struct device *jrdev = ctx->jrdev; struct scatterlist sg[1];
u32 *desc = ctx->sh_desc; struct completion done;
int len, nents;
init_sh_desc(desc, HDR_SHARE_SERIAL);
sg_init_table(sg, ARRAY_SIZE(sg));
/* Generate random bytes */ nents = kfifo_dma_in_prepare(&ctx->fifo, sg, ARRAY_SIZE(sg),
append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG); CAAM_RNG_FIFO_LEN);
if (!nents)
/* Store bytes */ return;
append_seq_fifo_store(desc, RN_BUF_SIZE, FIFOST_TYPE_RNGSTORE);
len = caam_rng_read_one(ctx->jrdev, sg_virt(&sg[0]),
ctx->sh_desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc), sg[0].length,
DMA_TO_DEVICE); ctx->desc_async,
if (dma_mapping_error(jrdev, ctx->sh_desc_dma)) { &done);
dev_err(jrdev, "unable to map shared descriptor\n"); if (len < 0)
return -ENOMEM; return;
}
kfifo_dma_in_finish(&ctx->fifo, len);
print_hex_dump_debug("rng shdesc@: ", DUMP_PREFIX_ADDRESS, 16, 4, }
desc, desc_bytes(desc), 1);
return 0; static void caam_rng_worker(struct work_struct *work)
{
struct caam_rng_ctx *ctx = container_of(work, struct caam_rng_ctx,
worker);
caam_rng_fill_async(ctx);
} }
static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id) static int caam_read(struct hwrng *rng, void *dst, size_t max, bool wait)
{ {
struct device *jrdev = ctx->jrdev; struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
struct buf_data *bd = &ctx->bufs[buf_id]; int out;
u32 *desc = bd->hw_desc;
int sh_len = desc_len(ctx->sh_desc);
init_job_desc_shared(desc, ctx->sh_desc_dma, sh_len, HDR_SHARE_DEFER | if (wait) {
HDR_REVERSE); struct completion done;
bd->addr = dma_map_single(jrdev, bd->buf, RN_BUF_SIZE, DMA_FROM_DEVICE); return caam_rng_read_one(ctx->jrdev, dst, max,
if (dma_mapping_error(jrdev, bd->addr)) { ctx->desc_sync, &done);
dev_err(jrdev, "unable to map dst\n");
return -ENOMEM;
} }
append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0); out = kfifo_out(&ctx->fifo, dst, max);
if (kfifo_len(&ctx->fifo) <= CAAM_RNG_FIFO_LEN / 2)
schedule_work(&ctx->worker);
print_hex_dump_debug("rng job desc@: ", DUMP_PREFIX_ADDRESS, 16, 4, return out;
desc, desc_bytes(desc), 1);
return 0;
} }
static void caam_cleanup(struct hwrng *rng) static void caam_cleanup(struct hwrng *rng)
{ {
struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
int i;
struct buf_data *bd;
for (i = 0; i < 2; i++) { flush_work(&ctx->worker);
bd = &ctx->bufs[i];
if (atomic_read(&bd->empty) == BUF_PENDING)
wait_for_completion(&bd->filled);
}
rng_unmap_ctx(ctx);
caam_jr_free(ctx->jrdev); caam_jr_free(ctx->jrdev);
kfifo_free(&ctx->fifo);
} }
static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id) static int caam_init(struct hwrng *rng)
{ {
struct buf_data *bd = &ctx->bufs[buf_id]; struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
int err; int err;
err = rng_create_job_desc(ctx, buf_id); ctx->desc_sync = devm_kzalloc(ctx->ctrldev, CAAM_RNG_DESC_LEN,
if (err) GFP_DMA | GFP_KERNEL);
return err; if (!ctx->desc_sync)
return -ENOMEM;
atomic_set(&bd->empty, BUF_EMPTY); ctx->desc_async = devm_kzalloc(ctx->ctrldev, CAAM_RNG_DESC_LEN,
submit_job(ctx, buf_id == ctx->current_buf); GFP_DMA | GFP_KERNEL);
wait_for_completion(&bd->filled); if (!ctx->desc_async)
return -ENOMEM;
return 0; if (kfifo_alloc(&ctx->fifo, CAAM_RNG_FIFO_LEN, GFP_DMA | GFP_KERNEL))
} return -ENOMEM;
static int caam_init(struct hwrng *rng) INIT_WORK(&ctx->worker, caam_rng_worker);
{
struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng);
int err;
ctx->jrdev = caam_jr_alloc(); ctx->jrdev = caam_jr_alloc();
err = PTR_ERR_OR_ZERO(ctx->jrdev); err = PTR_ERR_OR_ZERO(ctx->jrdev);
if (err) { if (err) {
kfifo_free(&ctx->fifo);
pr_err("Job Ring Device allocation for transform failed\n"); pr_err("Job Ring Device allocation for transform failed\n");
return err; return err;
} }
err = rng_create_sh_desc(ctx); /*
if (err) * Fill async buffer to have early randomness data for
goto free_jrdev; * hw_random
*/
ctx->current_buf = 0; caam_rng_fill_async(ctx);
ctx->cur_buf_idx = 0;
err = caam_init_buf(ctx, 0);
if (err)
goto free_jrdev;
err = caam_init_buf(ctx, 1);
if (err)
goto free_jrdev;
return 0; return 0;
free_jrdev:
caam_jr_free(ctx->jrdev);
return err;
} }
int caam_rng_init(struct device *ctrldev); int caam_rng_init(struct device *ctrldev);
...@@ -335,10 +225,12 @@ int caam_rng_init(struct device *ctrldev) ...@@ -335,10 +225,12 @@ int caam_rng_init(struct device *ctrldev)
if (!devres_open_group(ctrldev, caam_rng_init, GFP_KERNEL)) if (!devres_open_group(ctrldev, caam_rng_init, GFP_KERNEL))
return -ENOMEM; return -ENOMEM;
ctx = devm_kzalloc(ctrldev, sizeof(*ctx), GFP_DMA | GFP_KERNEL); ctx = devm_kzalloc(ctrldev, sizeof(*ctx), GFP_KERNEL);
if (!ctx) if (!ctx)
return -ENOMEM; return -ENOMEM;
ctx->ctrldev = ctrldev;
ctx->rng.name = "rng-caam"; ctx->rng.name = "rng-caam";
ctx->rng.init = caam_init; ctx->rng.init = caam_init;
ctx->rng.cleanup = caam_cleanup; ctx->rng.cleanup = caam_cleanup;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment