Skip to content

Commit 95848dc

Browse files
Christoph Hellwigaxboe
authored andcommitted
zram: take device and not only bvec offset into account
Commit af8b04c ("zram: simplify bvec iteration in __zram_make_request") changed the bio iteration in zram to rely on the implicit capping to page boundaries in bio_for_each_segment. But it failed to care for the fact zram not only care about the page alignment of the bio payload, but also the page alignment into the device. For buffered I/O and swap those are the same, but for direct I/O or kernel internal I/O like XFS log buffer writes they can differ. Fix this by open coding bio_for_each_segment and limiting the bvec len so that it never crosses over a page alignment boundary in the device in addition to the payload boundary already taken care of by bio_iter_iovec. Cc: stable@vger.kernel.org Fixes: af8b04c ("zram: simplify bvec iteration in __zram_make_request") Reported-by: Dusty Mabe <dusty@dustymabe.com> Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Sergey Senozhatsky <senozhatsky@chromium.org> Link: https://lore.kernel.org/r/20230805055537.147835-1-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent a592ab6 commit 95848dc

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

drivers/block/zram/zram_drv.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,15 +1870,16 @@ static void zram_bio_discard(struct zram *zram, struct bio *bio)
18701870

18711871
static void zram_bio_read(struct zram *zram, struct bio *bio)
18721872
{
1873-
struct bvec_iter iter;
1874-
struct bio_vec bv;
1875-
unsigned long start_time;
1873+
unsigned long start_time = bio_start_io_acct(bio);
1874+
struct bvec_iter iter = bio->bi_iter;
18761875

1877-
start_time = bio_start_io_acct(bio);
1878-
bio_for_each_segment(bv, bio, iter) {
1876+
do {
18791877
u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
18801878
u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
18811879
SECTOR_SHIFT;
1880+
struct bio_vec bv = bio_iter_iovec(bio, iter);
1881+
1882+
bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset);
18821883

18831884
if (zram_bvec_read(zram, &bv, index, offset, bio) < 0) {
18841885
atomic64_inc(&zram->stats.failed_reads);
@@ -1890,22 +1891,26 @@ static void zram_bio_read(struct zram *zram, struct bio *bio)
18901891
zram_slot_lock(zram, index);
18911892
zram_accessed(zram, index);
18921893
zram_slot_unlock(zram, index);
1893-
}
1894+
1895+
bio_advance_iter_single(bio, &iter, bv.bv_len);
1896+
} while (iter.bi_size);
1897+
18941898
bio_end_io_acct(bio, start_time);
18951899
bio_endio(bio);
18961900
}
18971901

18981902
static void zram_bio_write(struct zram *zram, struct bio *bio)
18991903
{
1900-
struct bvec_iter iter;
1901-
struct bio_vec bv;
1902-
unsigned long start_time;
1904+
unsigned long start_time = bio_start_io_acct(bio);
1905+
struct bvec_iter iter = bio->bi_iter;
19031906

1904-
start_time = bio_start_io_acct(bio);
1905-
bio_for_each_segment(bv, bio, iter) {
1907+
do {
19061908
u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
19071909
u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
19081910
SECTOR_SHIFT;
1911+
struct bio_vec bv = bio_iter_iovec(bio, iter);
1912+
1913+
bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset);
19091914

19101915
if (zram_bvec_write(zram, &bv, index, offset, bio) < 0) {
19111916
atomic64_inc(&zram->stats.failed_writes);
@@ -1916,7 +1921,10 @@ static void zram_bio_write(struct zram *zram, struct bio *bio)
19161921
zram_slot_lock(zram, index);
19171922
zram_accessed(zram, index);
19181923
zram_slot_unlock(zram, index);
1919-
}
1924+
1925+
bio_advance_iter_single(bio, &iter, bv.bv_len);
1926+
} while (iter.bi_size);
1927+
19201928
bio_end_io_acct(bio, start_time);
19211929
bio_endio(bio);
19221930
}

0 commit comments

Comments
 (0)