]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
crypto: scatterwalk - add new functions for skipping data
authorEric Biggers <ebiggers@google.com>
Wed, 19 Feb 2025 18:23:24 +0000 (10:23 -0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Sun, 2 Mar 2025 07:19:43 +0000 (15:19 +0800)
Add scatterwalk_skip() to skip the given number of bytes in a
scatter_walk.  Previously support for skipping was provided through
scatterwalk_copychunks(..., 2) followed by scatterwalk_done(), which was
confusing and less efficient.

Also add scatterwalk_start_at_pos() which starts a scatter_walk at the
given position, equivalent to scatterwalk_start() + scatterwalk_skip().
This addresses another common need in a more streamlined way.

Later patches will convert various users to use these functions.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/scatterwalk.c
include/crypto/scatterwalk.h

index 16f6ba896fb63bdc3cc9ad1ab56881270fd2c18d..af436ad02e3fff46e704192db5771e9aa29525e4 100644 (file)
 #include <linux/module.h>
 #include <linux/scatterlist.h>
 
+void scatterwalk_skip(struct scatter_walk *walk, unsigned int nbytes)
+{
+       struct scatterlist *sg = walk->sg;
+
+       nbytes += walk->offset - sg->offset;
+
+       while (nbytes > sg->length) {
+               nbytes -= sg->length;
+               sg = sg_next(sg);
+       }
+       walk->sg = sg;
+       walk->offset = sg->offset + nbytes;
+}
+EXPORT_SYMBOL_GPL(scatterwalk_skip);
+
 static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
 {
        void *src = out ? buf : sgdata;
index 924efbaefe67a9f4ad799c65a22c00aa1e3ae8c5..5c7765f601e0c4caccfcd3611acb5b5751d0a30f 100644 (file)
@@ -33,6 +33,22 @@ static inline void scatterwalk_start(struct scatter_walk *walk,
        walk->offset = sg->offset;
 }
 
+/*
+ * This is equivalent to scatterwalk_start(walk, sg) followed by
+ * scatterwalk_skip(walk, pos).
+ */
+static inline void scatterwalk_start_at_pos(struct scatter_walk *walk,
+                                           struct scatterlist *sg,
+                                           unsigned int pos)
+{
+       while (pos > sg->length) {
+               pos -= sg->length;
+               sg = sg_next(sg);
+       }
+       walk->sg = sg;
+       walk->offset = sg->offset + pos;
+}
+
 static inline unsigned int scatterwalk_pagelen(struct scatter_walk *walk)
 {
        unsigned int len = walk->sg->offset + walk->sg->length - walk->offset;
@@ -92,6 +108,8 @@ static inline void scatterwalk_done(struct scatter_walk *walk, int out,
                scatterwalk_pagedone(walk, out, more);
 }
 
+void scatterwalk_skip(struct scatter_walk *walk, unsigned int nbytes);
+
 void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
                            size_t nbytes, int out);