aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mpi/mpicoder.c
diff options
context:
space:
mode:
authorNicolai Stange <nicstange@gmail.com>2016-03-22 08:12:44 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-04-05 08:35:50 -0400
commitab1e912ec1021e7532148398a01eb1283437fe62 (patch)
treed1af09274b06ca93bb30cf8f85cdb178c7fe7526 /lib/mpi/mpicoder.c
parentb698538951a45dd853d9e40f51e143fdd46a60c6 (diff)
lib/mpi: mpi_read_raw_from_sgl(): don't include leading zero SGEs in nbytes
At the very beginning of mpi_read_raw_from_sgl(), the leading zeros of the input scatterlist are counted: lzeros = 0; for_each_sg(sgl, sg, ents, i) { ... if (/* sg contains nonzero bytes */) break; /* sg contains nothing but zeros here */ ents--; lzeros = 0; } Later on, the total number of trailing nonzero bytes is calculated by subtracting the number of leading zero bytes from the total number of input bytes: nbytes -= lzeros; However, since lzeros gets reset to zero for each completely zero leading sg in the loop above, it doesn't include those. Besides wasting resources by allocating a too large output buffer, this mistake propagates into the calculation of x, the number of leading zeros within the most significant output limb: x = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; What's more, the low order bytes of the output, equal in number to the extra bytes in nbytes, are left uninitialized. Fix this by adjusting nbytes for each completely zero leading scatterlist entry. Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") Signed-off-by: Nicolai Stange <nicstange@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'lib/mpi/mpicoder.c')
-rw-r--r--lib/mpi/mpicoder.c8
1 files changed, 2 insertions, 6 deletions
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index 048f0aa505ce..4ba0f2361d3c 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -447,16 +447,12 @@ MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes)
447 break; 447 break;
448 448
449 ents--; 449 ents--;
450 nbytes -= lzeros;
450 lzeros = 0; 451 lzeros = 0;
451 } 452 }
452 453
453 sgl = sg; 454 sgl = sg;
454 455 nbytes -= lzeros;
455 if (!ents)
456 nbytes = 0;
457 else
458 nbytes -= lzeros;
459
460 nbits = nbytes * 8; 456 nbits = nbytes * 8;
461 if (nbits > MAX_EXTERN_MPI_BITS) { 457 if (nbits > MAX_EXTERN_MPI_BITS) {
462 pr_info("MPI: mpi too large (%u bits)\n", nbits); 458 pr_info("MPI: mpi too large (%u bits)\n", nbits);