[PATCH 1/2] z2ram: copy every segment before completing a request

From: shao.mingyin

Date: Sat Nov 14 2026 - 11:34:18 EST


From: Shao Mingyin <shao.mingyin@xxxxxxxxxx>

z2_queue_rq() copies only blk_rq_cur_bytes() from the first bio and then
completes the entire request. The blk-mq conversion removed the loop
that used __blk_end_request_cur() to advance through each segment.
Multi-segment reads consequently leave data untouched, while writes
silently discard all but the first segment.

Walk all request segments and map each one for the copy. Advance the
buffer within a segment when crossing a Zorro RAM chunk boundary, and
check the size of the whole request against the backing storage before
copying anything.

Fixes: 8535fd6f702a ("z2ram: convert to blk-mq")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Shao Mingyin <shao.mingyin@xxxxxxxxxx>
---
Testing:
Built with W=1 using m68k amiga_defconfig. Exercised the actual request
function in a host test program with substitutes for requests, segment
iteration and page mapping. Read and write tests cover one segment,
three segments, and copies crossing a backing chunk boundary. The base
function loses or corrupts data in six of eight scenarios; the fixed
function matches the full-buffer oracle in all eight. The ordinary
single-segment controls pass in both versions. No Amiga hardware test
was performed.

drivers/block/z2ram.c | 39 +++++++++++++++++++++++----------------
1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/drivers/block/z2ram.c b/drivers/block/z2ram.c
index b216cba1c3bf2b750aaae59a6793bade44c857bd..15ef92a9744414b9387f4520dc3c7b2f42bebc87 100644
--- a/drivers/block/z2ram.c
+++ b/drivers/block/z2ram.c
@@ -36,6 +36,7 @@
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/pgtable.h>
+#include <linux/highmem.h>

#include <asm/setup.h>
#include <asm/amigahw.h>
@@ -70,11 +71,12 @@ static blk_status_t z2_queue_rq(struct blk_mq_hw_ctx *hctx,
{
struct request *req = bd->rq;
unsigned long start = blk_rq_pos(req) << 9;
- unsigned long len = blk_rq_cur_bytes(req);
+ struct req_iterator iter;
+ struct bio_vec bv;

blk_mq_start_request(req);

- if (start + len > z2ram_size) {
+ if (start > z2ram_size || blk_rq_bytes(req) > z2ram_size - start) {
pr_err(DEVICE_NAME ": bad access: block=%llu, "
"count=%u\n",
(unsigned long long)blk_rq_pos(req),
@@ -84,20 +86,25 @@ static blk_status_t z2_queue_rq(struct blk_mq_hw_ctx *hctx,

spin_lock_irq(&z2ram_lock);

- while (len) {
- unsigned long addr = start & Z2RAM_CHUNKMASK;
- unsigned long size = Z2RAM_CHUNKSIZE - addr;
- void *buffer = bio_data(req->bio);
-
- if (len < size)
- size = len;
- addr += z2ram_map[start >> Z2RAM_CHUNKSHIFT];
- if (rq_data_dir(req) == READ)
- memcpy(buffer, (char *)addr, size);
- else
- memcpy((char *)addr, buffer, size);
- start += size;
- len -= size;
+ rq_for_each_segment(bv, req, iter) {
+ unsigned long len = bv.bv_len;
+ char *buffer = bvec_kmap_local(&bv);
+ char *ptr = buffer;
+
+ while (len) {
+ unsigned long offset = start & Z2RAM_CHUNKMASK;
+ unsigned long size = min(len, Z2RAM_CHUNKSIZE - offset);
+ unsigned long addr = z2ram_map[start >> Z2RAM_CHUNKSHIFT];
+
+ if (rq_data_dir(req) == READ)
+ memcpy(ptr, (char *)addr + offset, size);
+ else
+ memcpy((char *)addr + offset, ptr, size);
+ start += size;
+ ptr += size;
+ len -= size;
+ }
+ kunmap_local(buffer);
}

spin_unlock_irq(&z2ram_lock);
--
2.43.0