* [PATCH v1 0/2] scatterlist: introduce sg_nents_for_len
@ 2015-06-01 16:15 Tom Lendacky
2015-06-01 16:15 ` [PATCH v1 1/2] " Tom Lendacky
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Tom Lendacky @ 2015-06-01 16:15 UTC (permalink / raw)
To: linux-crypto; +Cc: David Miller, linux-kernel, Herbert Xu
This series introduces and makes use of a new scatterlist function named
sg_nents_for_len. The purpose of this function is determine the number
of scatterlist entries required to satisify a specified length. The
resulting count can be used as input to the dma_map_sg function to map
only the entries needed.
This function is needed for cases where a supplied scatterlist has not
marked the last filled sg entry as the end of the scatterlist. In these
cases, the sg_nents function count includes sg entries that may not have
been filled in. When using this value in a dma_map_sg call it can result
in errors/bugs being generated.
The following patches are included in this series:
- Introduce the sg_nents_for_len function
- Update the ccp driver to use the sg_nents_for_len function
This patch series is based on cryptodev-2.6.
---
Tom Lendacky (2):
scatterlist: introduce sg_nents_for_len
crypto: ccp - Protect against poorly marked end of sg list
drivers/crypto/ccp/ccp-ops.c | 7 +++++--
include/linux/scatterlist.h | 1 +
lib/scatterlist.c | 32 ++++++++++++++++++++++++++++++++
3 files changed, 38 insertions(+), 2 deletions(-)
--
Tom Lendacky
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v1 1/2] scatterlist: introduce sg_nents_for_len
2015-06-01 16:15 [PATCH v1 0/2] scatterlist: introduce sg_nents_for_len Tom Lendacky
@ 2015-06-01 16:15 ` Tom Lendacky
2015-06-01 16:15 ` [PATCH v1 2/2] crypto: ccp - Protect against poorly marked end of sg list Tom Lendacky
2015-06-03 5:09 ` [PATCH v1 0/2] scatterlist: introduce sg_nents_for_len Herbert Xu
2 siblings, 0 replies; 4+ messages in thread
From: Tom Lendacky @ 2015-06-01 16:15 UTC (permalink / raw)
To: linux-crypto; +Cc: David Miller, linux-kernel, Herbert Xu
When performing a dma_map_sg() call, the number of sg entries to map is
required. Using sg_nents to retrieve the number of sg entries will
return the total number of entries in the sg list up to the entry marked
as the end. If there happen to be unused entries in the list, these will
still be counted. Some dma_map_sg() implementations will not handle the
unused entries correctly (lib/swiotlb.c) and execute a BUG_ON.
The sg_nents_for_len() function will traverse the sg list and return the
number of entries required to satisfy the supplied length argument. This
can then be supplied to the dma_map_sg() call to successfully map the
sg.
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
---
include/linux/scatterlist.h | 1 +
lib/scatterlist.c | 32 ++++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+)
diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h
index ed8f9e7..a0edb99 100644
--- a/include/linux/scatterlist.h
+++ b/include/linux/scatterlist.h
@@ -221,6 +221,7 @@ static inline void *sg_virt(struct scatterlist *sg)
}
int sg_nents(struct scatterlist *sg);
+int sg_nents_for_len(struct scatterlist *sg, u64 len);
struct scatterlist *sg_next(struct scatterlist *);
struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
void sg_init_table(struct scatterlist *, unsigned int);
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index c9f2e8c..99fbc2f 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -56,6 +56,38 @@ int sg_nents(struct scatterlist *sg)
}
EXPORT_SYMBOL(sg_nents);
+/**
+ * sg_nents_for_len - return total count of entries in scatterlist
+ * needed to satisfy the supplied length
+ * @sg: The scatterlist
+ * @len: The total required length
+ *
+ * Description:
+ * Determines the number of entries in sg that are required to meet
+ * the supplied length, taking into acount chaining as well
+ *
+ * Returns:
+ * the number of sg entries needed, negative error on failure
+ *
+ **/
+int sg_nents_for_len(struct scatterlist *sg, u64 len)
+{
+ int nents;
+ u64 total;
+
+ if (!len)
+ return 0;
+
+ for (nents = 0, total = 0; sg; sg = sg_next(sg)) {
+ nents++;
+ total += sg->length;
+ if (total >= len)
+ return nents;
+ }
+
+ return -EINVAL;
+}
+EXPORT_SYMBOL(sg_nents_for_len);
/**
* sg_last - return the last scatterlist entry in a list
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v1 2/2] crypto: ccp - Protect against poorly marked end of sg list
2015-06-01 16:15 [PATCH v1 0/2] scatterlist: introduce sg_nents_for_len Tom Lendacky
2015-06-01 16:15 ` [PATCH v1 1/2] " Tom Lendacky
@ 2015-06-01 16:15 ` Tom Lendacky
2015-06-03 5:09 ` [PATCH v1 0/2] scatterlist: introduce sg_nents_for_len Herbert Xu
2 siblings, 0 replies; 4+ messages in thread
From: Tom Lendacky @ 2015-06-01 16:15 UTC (permalink / raw)
To: linux-crypto; +Cc: David Miller, linux-kernel, Herbert Xu
Scatter gather lists can be created with more available entries than are
actually used (e.g. using sg_init_table() to reserve a specific number
of sg entries, but in actuality using something less than that based on
the data length). The caller sometimes fails to mark the last entry
with sg_mark_end(). In these cases, sg_nents() will return the original
size of the sg list as opposed to the actual number of sg entries that
contain valid data.
On arm64, if the sg_nents() value is used in a call to dma_map_sg() in
this situation, then it causes a BUG_ON in lib/swiotlb.c because an
"empty" sg list entry results in dma_capable() returning false and
swiotlb trying to create a bounce buffer of size 0. This occurred in
the userspace crypto interface before being fixed by
0f477b655a52 ("crypto: algif - Mark sgl end at the end of data")
Protect against this by using the new sg_nents_for_len() function which
returns only the number of sg entries required to meet the desired
length and supplying that value to dma_map_sg().
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
---
drivers/crypto/ccp/ccp-ops.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
index 542453c..d09c6c4 100644
--- a/drivers/crypto/ccp/ccp-ops.c
+++ b/drivers/crypto/ccp/ccp-ops.c
@@ -52,7 +52,7 @@ struct ccp_dm_workarea {
struct ccp_sg_workarea {
struct scatterlist *sg;
- unsigned int nents;
+ int nents;
struct scatterlist *dma_sg;
struct device *dma_dev;
@@ -495,7 +495,10 @@ static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
if (!sg)
return 0;
- wa->nents = sg_nents(sg);
+ wa->nents = sg_nents_for_len(sg, len);
+ if (wa->nents < 0)
+ return wa->nents;
+
wa->bytes_left = len;
wa->sg_used = 0;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1 0/2] scatterlist: introduce sg_nents_for_len
2015-06-01 16:15 [PATCH v1 0/2] scatterlist: introduce sg_nents_for_len Tom Lendacky
2015-06-01 16:15 ` [PATCH v1 1/2] " Tom Lendacky
2015-06-01 16:15 ` [PATCH v1 2/2] crypto: ccp - Protect against poorly marked end of sg list Tom Lendacky
@ 2015-06-03 5:09 ` Herbert Xu
2 siblings, 0 replies; 4+ messages in thread
From: Herbert Xu @ 2015-06-03 5:09 UTC (permalink / raw)
To: Tom Lendacky; +Cc: linux-crypto, David Miller, linux-kernel
On Mon, Jun 01, 2015 at 11:15:19AM -0500, Tom Lendacky wrote:
> This series introduces and makes use of a new scatterlist function named
> sg_nents_for_len. The purpose of this function is determine the number
> of scatterlist entries required to satisify a specified length. The
> resulting count can be used as input to the dma_map_sg function to map
> only the entries needed.
>
> This function is needed for cases where a supplied scatterlist has not
> marked the last filled sg entry as the end of the scatterlist. In these
> cases, the sg_nents function count includes sg entries that may not have
> been filled in. When using this value in a dma_map_sg call it can result
> in errors/bugs being generated.
>
> The following patches are included in this series:
> - Introduce the sg_nents_for_len function
> - Update the ccp driver to use the sg_nents_for_len function
>
> This patch series is based on cryptodev-2.6.
Both applied. Thanks.
--
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-06-03 5:09 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-01 16:15 [PATCH v1 0/2] scatterlist: introduce sg_nents_for_len Tom Lendacky
2015-06-01 16:15 ` [PATCH v1 1/2] " Tom Lendacky
2015-06-01 16:15 ` [PATCH v1 2/2] crypto: ccp - Protect against poorly marked end of sg list Tom Lendacky
2015-06-03 5:09 ` [PATCH v1 0/2] scatterlist: introduce sg_nents_for_len Herbert Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®