Commit 9bc3dd24 authored by Corentin Labbe's avatar Corentin Labbe Committed by Herbert Xu

crypto: sun4i-ss - fix kmap usage

With the recent kmap change, some tests which were conditional on
CONFIG_DEBUG_HIGHMEM now are enabled by default.
This permit to detect a problem in sun4i-ss usage of kmap.

sun4i-ss uses two kmap via sg_miter (one for input, one for output), but
using two kmap at the same time is hard:
"the ordering has to be correct and with sg_miter that's probably hard to get
right." (quoting Tlgx)

So the easiest solution is to never have two sg_miter/kmap open at the same time.
After each use of sg_miter, I store the current index, for being able to
resume sg_miter to the right place.

Fixes: 6298e948 ("crypto: sunxi-ss - Add Allwinner Security System crypto accelerator")
Signed-off-by: default avatarCorentin Labbe <clabbe@baylibre.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 4ec8977b
...@@ -31,6 +31,8 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) ...@@ -31,6 +31,8 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
unsigned int ileft = areq->cryptlen; unsigned int ileft = areq->cryptlen;
unsigned int oleft = areq->cryptlen; unsigned int oleft = areq->cryptlen;
unsigned int todo; unsigned int todo;
unsigned long pi = 0, po = 0; /* progress for in and out */
bool miter_err;
struct sg_mapping_iter mi, mo; struct sg_mapping_iter mi, mo;
unsigned int oi, oo; /* offset for in and out */ unsigned int oi, oo; /* offset for in and out */
unsigned long flags; unsigned long flags;
...@@ -63,23 +65,23 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) ...@@ -63,23 +65,23 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
} }
writel(mode, ss->base + SS_CTL); writel(mode, ss->base + SS_CTL);
sg_miter_start(&mi, areq->src, sg_nents(areq->src),
SG_MITER_FROM_SG | SG_MITER_ATOMIC);
sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
SG_MITER_TO_SG | SG_MITER_ATOMIC);
sg_miter_next(&mi);
sg_miter_next(&mo);
if (!mi.addr || !mo.addr) {
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
err = -EINVAL;
goto release_ss;
}
ileft = areq->cryptlen / 4; ileft = areq->cryptlen / 4;
oleft = areq->cryptlen / 4; oleft = areq->cryptlen / 4;
oi = 0; oi = 0;
oo = 0; oo = 0;
do { do {
if (ileft) {
sg_miter_start(&mi, areq->src, sg_nents(areq->src),
SG_MITER_FROM_SG | SG_MITER_ATOMIC);
if (pi)
sg_miter_skip(&mi, pi);
miter_err = sg_miter_next(&mi);
if (!miter_err || !mi.addr) {
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
err = -EINVAL;
goto release_ss;
}
todo = min(rx_cnt, ileft); todo = min(rx_cnt, ileft);
todo = min_t(size_t, todo, (mi.length - oi) / 4); todo = min_t(size_t, todo, (mi.length - oi) / 4);
if (todo) { if (todo) {
...@@ -88,14 +90,26 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) ...@@ -88,14 +90,26 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
oi += todo * 4; oi += todo * 4;
} }
if (oi == mi.length) { if (oi == mi.length) {
sg_miter_next(&mi); pi += mi.length;
oi = 0; oi = 0;
} }
sg_miter_stop(&mi);
}
spaces = readl(ss->base + SS_FCSR); spaces = readl(ss->base + SS_FCSR);
rx_cnt = SS_RXFIFO_SPACES(spaces); rx_cnt = SS_RXFIFO_SPACES(spaces);
tx_cnt = SS_TXFIFO_SPACES(spaces); tx_cnt = SS_TXFIFO_SPACES(spaces);
sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
SG_MITER_TO_SG | SG_MITER_ATOMIC);
if (po)
sg_miter_skip(&mo, po);
miter_err = sg_miter_next(&mo);
if (!miter_err || !mo.addr) {
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
err = -EINVAL;
goto release_ss;
}
todo = min(tx_cnt, oleft); todo = min(tx_cnt, oleft);
todo = min_t(size_t, todo, (mo.length - oo) / 4); todo = min_t(size_t, todo, (mo.length - oo) / 4);
if (todo) { if (todo) {
...@@ -104,9 +118,10 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) ...@@ -104,9 +118,10 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
oo += todo * 4; oo += todo * 4;
} }
if (oo == mo.length) { if (oo == mo.length) {
sg_miter_next(&mo);
oo = 0; oo = 0;
po += mo.length;
} }
sg_miter_stop(&mo);
} while (oleft); } while (oleft);
if (areq->iv) { if (areq->iv) {
...@@ -120,8 +135,6 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq) ...@@ -120,8 +135,6 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
} }
release_ss: release_ss:
sg_miter_stop(&mi);
sg_miter_stop(&mo);
writel(0, ss->base + SS_CTL); writel(0, ss->base + SS_CTL);
spin_unlock_irqrestore(&ss->slock, flags); spin_unlock_irqrestore(&ss->slock, flags);
return err; return err;
...@@ -174,6 +187,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) ...@@ -174,6 +187,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
unsigned int todo; unsigned int todo;
void *backup_iv = NULL; void *backup_iv = NULL;
struct sg_mapping_iter mi, mo; struct sg_mapping_iter mi, mo;
unsigned long pi = 0, po = 0; /* progress for in and out */
bool miter_err;
unsigned int oi, oo; /* offset for in and out */ unsigned int oi, oo; /* offset for in and out */
unsigned int ob = 0; /* offset in buf */ unsigned int ob = 0; /* offset in buf */
unsigned int obo = 0; /* offset in bufo*/ unsigned int obo = 0; /* offset in bufo*/
...@@ -234,17 +249,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) ...@@ -234,17 +249,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
} }
writel(mode, ss->base + SS_CTL); writel(mode, ss->base + SS_CTL);
sg_miter_start(&mi, areq->src, sg_nents(areq->src),
SG_MITER_FROM_SG | SG_MITER_ATOMIC);
sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
SG_MITER_TO_SG | SG_MITER_ATOMIC);
sg_miter_next(&mi);
sg_miter_next(&mo);
if (!mi.addr || !mo.addr) {
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
err = -EINVAL;
goto release_ss;
}
ileft = areq->cryptlen; ileft = areq->cryptlen;
oleft = areq->cryptlen; oleft = areq->cryptlen;
oi = 0; oi = 0;
...@@ -252,6 +256,16 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) ...@@ -252,6 +256,16 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
while (oleft) { while (oleft) {
if (ileft) { if (ileft) {
sg_miter_start(&mi, areq->src, sg_nents(areq->src),
SG_MITER_FROM_SG | SG_MITER_ATOMIC);
if (pi)
sg_miter_skip(&mi, pi);
miter_err = sg_miter_next(&mi);
if (!miter_err || !mi.addr) {
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
err = -EINVAL;
goto release_ss;
}
/* /*
* todo is the number of consecutive 4byte word that we * todo is the number of consecutive 4byte word that we
* can read from current SG * can read from current SG
...@@ -284,31 +298,38 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) ...@@ -284,31 +298,38 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
} }
} }
if (oi == mi.length) { if (oi == mi.length) {
sg_miter_next(&mi); pi += mi.length;
oi = 0; oi = 0;
} }
sg_miter_stop(&mi);
} }
spaces = readl(ss->base + SS_FCSR); spaces = readl(ss->base + SS_FCSR);
rx_cnt = SS_RXFIFO_SPACES(spaces); rx_cnt = SS_RXFIFO_SPACES(spaces);
tx_cnt = SS_TXFIFO_SPACES(spaces); tx_cnt = SS_TXFIFO_SPACES(spaces);
dev_dbg(ss->dev,
"%x %u/%zu %u/%u cnt=%u %u/%zu %u/%u cnt=%u %u\n",
mode,
oi, mi.length, ileft, areq->cryptlen, rx_cnt,
oo, mo.length, oleft, areq->cryptlen, tx_cnt, ob);
if (!tx_cnt) if (!tx_cnt)
continue; continue;
sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
SG_MITER_TO_SG | SG_MITER_ATOMIC);
if (po)
sg_miter_skip(&mo, po);
miter_err = sg_miter_next(&mo);
if (!miter_err || !mo.addr) {
dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
err = -EINVAL;
goto release_ss;
}
/* todo in 4bytes word */ /* todo in 4bytes word */
todo = min(tx_cnt, oleft / 4); todo = min(tx_cnt, oleft / 4);
todo = min_t(size_t, todo, (mo.length - oo) / 4); todo = min_t(size_t, todo, (mo.length - oo) / 4);
if (todo) { if (todo) {
readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo); readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
oleft -= todo * 4; oleft -= todo * 4;
oo += todo * 4; oo += todo * 4;
if (oo == mo.length) { if (oo == mo.length) {
sg_miter_next(&mo); po += mo.length;
oo = 0; oo = 0;
} }
} else { } else {
...@@ -333,12 +354,14 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) ...@@ -333,12 +354,14 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
obo += todo; obo += todo;
oo += todo; oo += todo;
if (oo == mo.length) { if (oo == mo.length) {
po += mo.length;
sg_miter_next(&mo); sg_miter_next(&mo);
oo = 0; oo = 0;
} }
} while (obo < obl); } while (obo < obl);
/* bufo must be fully used here */ /* bufo must be fully used here */
} }
sg_miter_stop(&mo);
} }
if (areq->iv) { if (areq->iv) {
if (mode & SS_DECRYPTION) { if (mode & SS_DECRYPTION) {
...@@ -351,8 +374,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq) ...@@ -351,8 +374,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
} }
release_ss: release_ss:
sg_miter_stop(&mi);
sg_miter_stop(&mo);
writel(0, ss->base + SS_CTL); writel(0, ss->base + SS_CTL);
spin_unlock_irqrestore(&ss->slock, flags); spin_unlock_irqrestore(&ss->slock, flags);
......
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