Skip to content

Commit 307fd54

Browse files
Cristian Stoicaherbertx
Cristian Stoica
authored andcommitted
crypto: caam - remove duplicated sg copy functions
Replace equivalent (and partially incorrect) scatter-gather functions with ones from crypto-API. The replacement is motivated by page-faults in sg_copy_part triggered by successive calls to crypto_hash_update. The following fault appears after calling crypto_ahash_update twice, first with 13 and then with 285 bytes: Unable to handle kernel paging request for data at address 0x00000008 Faulting instruction address: 0xf9bf9a8c Oops: Kernel access of bad area, sig: 11 [#1] SMP NR_CPUS=8 CoreNet Generic Modules linked in: tcrypt(+) caamhash caam_jr caam tls CPU: 6 PID: 1497 Comm: cryptomgr_test Not tainted 3.12.19-rt30-QorIQ-SDK-V1.6+g9fda9f2 #75 task: e9308530 ti: e700e000 task.ti: e700e000 NIP: f9bf9a8c LR: f9bfcf28 CTR: c0019ea0 REGS: e700fb80 TRAP: 0300 Not tainted (3.12.19-rt30-QorIQ-SDK-V1.6+g9fda9f2) MSR: 00029002 <CE,EE,ME> CR: 44f92024 XER: 20000000 DEAR: 00000008, ESR: 00000000 GPR00: f9bfcf28 e700fc30 e9308530 e70b1e55 00000000 ffffffdd e70b1e54 0bebf888 GPR08: 902c7ef5 c0e771e2 00000002 00000888 c0019ea0 00000000 00000000 c07a4154 GPR16: c08d0000 e91a8f9c 00000001 e98fb400 00000100 e9c83028 e70b1e08 e70b1d48 GPR24: e992ce10 e70b1dc8 f9bfe4f4 e70b1e55 ffffffdd e70b1ce0 00000000 00000000 NIP [f9bf9a8c] sg_copy+0x1c/0x100 [caamhash] LR [f9bfcf28] ahash_update_no_ctx+0x628/0x660 [caamhash] Call Trace: [e700fc30] [f9bf9c50] sg_copy_part+0xe0/0x160 [caamhash] (unreliable) [e700fc50] [f9bfcf28] ahash_update_no_ctx+0x628/0x660 [caamhash] [e700fcb0] [f954e19c] crypto_tls_genicv+0x13c/0x300 [tls] [e700fd10] [f954e65c] crypto_tls_encrypt+0x5c/0x260 [tls] [e700fd40] [c02250ec] __test_aead.constprop.9+0x2bc/0xb70 [e700fe40] [c02259f0] alg_test_aead+0x50/0xc0 [e700fe60] [c02241e4] alg_test+0x114/0x2e0 [e700fee0] [c022276c] cryptomgr_test+0x4c/0x60 [e700fef0] [c004f658] kthread+0x98/0xa0 [e700ff40] [c000fd04] ret_from_kernel_thread+0x5c/0x64 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 65fafbe commit 307fd54

File tree

2 files changed

+14
-62
lines changed

2 files changed

+14
-62
lines changed

drivers/crypto/caam/caamhash.c

+14-8
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,9 @@ static int ahash_update_ctx(struct ahash_request *req)
836836
edesc->sec4_sg + sec4_sg_src_index,
837837
chained);
838838
if (*next_buflen) {
839-
sg_copy_part(next_buf, req->src, to_hash -
840-
*buflen, req->nbytes);
839+
scatterwalk_map_and_copy(next_buf, req->src,
840+
to_hash - *buflen,
841+
*next_buflen, 0);
841842
state->current_buf = !state->current_buf;
842843
}
843844
} else {
@@ -878,7 +879,8 @@ static int ahash_update_ctx(struct ahash_request *req)
878879
kfree(edesc);
879880
}
880881
} else if (*next_buflen) {
881-
sg_copy(buf + *buflen, req->src, req->nbytes);
882+
scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
883+
req->nbytes, 0);
882884
*buflen = *next_buflen;
883885
*next_buflen = last_buflen;
884886
}
@@ -1262,8 +1264,9 @@ static int ahash_update_no_ctx(struct ahash_request *req)
12621264
src_map_to_sec4_sg(jrdev, req->src, src_nents,
12631265
edesc->sec4_sg + 1, chained);
12641266
if (*next_buflen) {
1265-
sg_copy_part(next_buf, req->src, to_hash - *buflen,
1266-
req->nbytes);
1267+
scatterwalk_map_and_copy(next_buf, req->src,
1268+
to_hash - *buflen,
1269+
*next_buflen, 0);
12671270
state->current_buf = !state->current_buf;
12681271
}
12691272

@@ -1304,7 +1307,8 @@ static int ahash_update_no_ctx(struct ahash_request *req)
13041307
kfree(edesc);
13051308
}
13061309
} else if (*next_buflen) {
1307-
sg_copy(buf + *buflen, req->src, req->nbytes);
1310+
scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
1311+
req->nbytes, 0);
13081312
*buflen = *next_buflen;
13091313
*next_buflen = 0;
13101314
}
@@ -1476,7 +1480,8 @@ static int ahash_update_first(struct ahash_request *req)
14761480
}
14771481

14781482
if (*next_buflen)
1479-
sg_copy_part(next_buf, req->src, to_hash, req->nbytes);
1483+
scatterwalk_map_and_copy(next_buf, req->src, to_hash,
1484+
*next_buflen, 0);
14801485

14811486
sh_len = desc_len(sh_desc);
14821487
desc = edesc->hw_desc;
@@ -1511,7 +1516,8 @@ static int ahash_update_first(struct ahash_request *req)
15111516
state->update = ahash_update_no_ctx;
15121517
state->finup = ahash_finup_no_ctx;
15131518
state->final = ahash_final_no_ctx;
1514-
sg_copy(next_buf, req->src, req->nbytes);
1519+
scatterwalk_map_and_copy(next_buf, req->src, 0,
1520+
req->nbytes, 0);
15151521
}
15161522
#ifdef DEBUG
15171523
print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",

drivers/crypto/caam/sg_sw_sec4.h

-54
Original file line numberDiff line numberDiff line change
@@ -116,57 +116,3 @@ static int dma_unmap_sg_chained(struct device *dev, struct scatterlist *sg,
116116
}
117117
return nents;
118118
}
119-
120-
/* Map SG page in kernel virtual address space and copy */
121-
static inline void sg_map_copy(u8 *dest, struct scatterlist *sg,
122-
int len, int offset)
123-
{
124-
u8 *mapped_addr;
125-
126-
/*
127-
* Page here can be user-space pinned using get_user_pages
128-
* Same must be kmapped before use and kunmapped subsequently
129-
*/
130-
mapped_addr = kmap_atomic(sg_page(sg));
131-
memcpy(dest, mapped_addr + offset, len);
132-
kunmap_atomic(mapped_addr);
133-
}
134-
135-
/* Copy from len bytes of sg to dest, starting from beginning */
136-
static inline void sg_copy(u8 *dest, struct scatterlist *sg, unsigned int len)
137-
{
138-
struct scatterlist *current_sg = sg;
139-
int cpy_index = 0, next_cpy_index = current_sg->length;
140-
141-
while (next_cpy_index < len) {
142-
sg_map_copy(dest + cpy_index, current_sg, current_sg->length,
143-
current_sg->offset);
144-
current_sg = scatterwalk_sg_next(current_sg);
145-
cpy_index = next_cpy_index;
146-
next_cpy_index += current_sg->length;
147-
}
148-
if (cpy_index < len)
149-
sg_map_copy(dest + cpy_index, current_sg, len-cpy_index,
150-
current_sg->offset);
151-
}
152-
153-
/* Copy sg data, from to_skip to end, to dest */
154-
static inline void sg_copy_part(u8 *dest, struct scatterlist *sg,
155-
int to_skip, unsigned int end)
156-
{
157-
struct scatterlist *current_sg = sg;
158-
int sg_index, cpy_index, offset;
159-
160-
sg_index = current_sg->length;
161-
while (sg_index <= to_skip) {
162-
current_sg = scatterwalk_sg_next(current_sg);
163-
sg_index += current_sg->length;
164-
}
165-
cpy_index = sg_index - to_skip;
166-
offset = current_sg->offset + current_sg->length - cpy_index;
167-
sg_map_copy(dest, current_sg, cpy_index, offset);
168-
if (end - sg_index) {
169-
current_sg = scatterwalk_sg_next(current_sg);
170-
sg_copy(dest + cpy_index, current_sg, end - sg_index);
171-
}
172-
}

0 commit comments

Comments
 (0)