From: scott_gzh@163.com
To: herbert@gondor.apana.org.au, davem@davemloft.net
Cc: linux-crypto@vger.kernel.org, Scott GUO <scottzhguo@tencent.com>
Subject: [PATCH 1/2] scatterlist: Introduce sglist_shift_{left,right} helpers
Date: Fri, 15 May 2026 16:36:44 +0800 [thread overview]
Message-ID: <20260515083645.4024574-2-scott_gzh@163.com> (raw)
In-Reply-To: <20260515083645.4024574-1-scott_gzh@163.com>
From: Scott GUO <scottzhguo@tencent.com>
These helpers shifts data within sglist in a chunck by
chunck manner.
Signed-off-by: GUO Zihua <scottzhguo@tencent.com>
---
crypto/scatterwalk.c | 79 ++++++++++++++++++++++++++++++++++++
include/crypto/scatterwalk.h | 6 +++
2 files changed, 85 insertions(+)
diff --git a/crypto/scatterwalk.c b/crypto/scatterwalk.c
index be0e24843806..958ab7e20d9d 100644
--- a/crypto/scatterwalk.c
+++ b/crypto/scatterwalk.c
@@ -180,6 +180,85 @@ void memcpy_sglist(struct scatterlist *dst, struct scatterlist *src,
}
EXPORT_SYMBOL_GPL(memcpy_sglist);
+static unsigned int sglist_total_len(struct scatterlist *sg)
+{
+ unsigned int total = 0;
+
+ for (; sg; sg = sg_next(sg))
+ total += sg->length;
+ return total;
+}
+
+/**
+ * sglist_shift_left - shift a region of data left within a scatterlist
+ * @sg: scatterlist to operate on
+ * @dst_off: destination offset in bytes (must be <= @src_off)
+ * @src_off: source offset in bytes (start of the region to move)
+ * @nbytes: number of bytes to move
+ *
+ * Moves [src_off, src_off+nbytes) to [dst_off, dst_off+nbytes).
+ * Handles overlapping regions safely by copying forward (low to high).
+ */
+void sglist_shift_left(struct scatterlist *sg, unsigned int dst_off,
+ unsigned int src_off, unsigned int nbytes)
+{
+ u8 buf[16];
+
+ if (!nbytes || dst_off == src_off)
+ return;
+ if (WARN_ON_ONCE(dst_off > src_off))
+ return;
+ if (WARN_ON_ONCE(src_off + nbytes > sglist_total_len(sg)))
+ return;
+
+ while (nbytes) {
+ unsigned int chunk = min_t(unsigned int, nbytes, sizeof(buf));
+
+ memcpy_from_sglist(buf, sg, src_off, chunk);
+ memcpy_to_sglist(sg, dst_off, buf, chunk);
+ src_off += chunk;
+ dst_off += chunk;
+ nbytes -= chunk;
+ }
+}
+EXPORT_SYMBOL_GPL(sglist_shift_left);
+
+/**
+ * sglist_shift_right - shift a region of data right within a scatterlist
+ * @sg: scatterlist to operate on
+ * @dst_off: destination offset in bytes (must be >= @src_off)
+ * @src_off: source offset in bytes (start of the region to move)
+ * @nbytes: number of bytes to move
+ *
+ * Moves [src_off, src_off+nbytes) to [dst_off, dst_off+nbytes).
+ * Handles overlapping regions safely by copying backward (high to low).
+ */
+void sglist_shift_right(struct scatterlist *sg, unsigned int dst_off,
+ unsigned int src_off, unsigned int nbytes)
+{
+ unsigned int src_end = src_off + nbytes;
+ unsigned int dst_end = dst_off + nbytes;
+ u8 buf[16];
+
+ if (!nbytes || dst_off == src_off)
+ return;
+ if (WARN_ON_ONCE(dst_off < src_off))
+ return;
+ if (WARN_ON_ONCE(dst_end > sglist_total_len(sg)))
+ return;
+
+ while (nbytes) {
+ unsigned int chunk = min_t(unsigned int, nbytes, sizeof(buf));
+
+ src_end -= chunk;
+ dst_end -= chunk;
+ memcpy_from_sglist(buf, sg, src_end, chunk);
+ memcpy_to_sglist(sg, dst_end, buf, chunk);
+ nbytes -= chunk;
+ }
+}
+EXPORT_SYMBOL_GPL(sglist_shift_right);
+
struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
struct scatterlist *src,
unsigned int len)
diff --git a/include/crypto/scatterwalk.h b/include/crypto/scatterwalk.h
index 624fab589c2c..eed6aee5bf9c 100644
--- a/include/crypto/scatterwalk.h
+++ b/include/crypto/scatterwalk.h
@@ -249,6 +249,12 @@ static inline void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
memcpy_from_sglist(buf, sg, start, nbytes);
}
+void sglist_shift_left(struct scatterlist *sg, unsigned int dst_off,
+ unsigned int src_off, unsigned int nbytes);
+
+void sglist_shift_right(struct scatterlist *sg, unsigned int dst_off,
+ unsigned int src_off, unsigned int nbytes);
+
struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
struct scatterlist *src,
unsigned int len);
--
2.41.3
next prev parent reply other threads:[~2026-05-15 8:38 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-15 8:36 [PATCH 0/2] authencesn: Refactor in-place decryption scott_gzh
2026-05-15 8:36 ` scott_gzh [this message]
2026-05-15 8:36 ` [PATCH 2/2] authencesn: Refactor inplace-decryption with sglist shift helper scott_gzh
2026-05-15 10:41 ` [PATCH 0/2] authencesn: Refactor in-place decryption Scott Guo
2026-05-15 11:01 ` Scott Guo
2026-05-18 2:33 ` Herbert Xu
2026-05-18 2:55 ` Scott Guo
2026-05-18 3:17 ` Herbert Xu
2026-05-18 6:48 ` Scott Guo
2026-05-18 8:21 ` Herbert Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260515083645.4024574-2-scott_gzh@163.com \
--to=scott_gzh@163.com \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=scottzhguo@tencent.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox