blob: 4ea958d0ee07bb4c11c5945b644d9043bf788184 [file] [edit]
// SPDX-License-Identifier: GPL-2.0-only
/* bvecq iterator tests. Exercises:
* - advance
* - revert
* - npages
* - alignment
*
* See also kunit_iov_iter.c for bvecq copy and extract tests.
*
* Copyright (C) 2026 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/uio.h>
#include <linux/bvecq.h>
#include <kunit/test.h>
MODULE_DESCRIPTION("bvecq testing");
MODULE_AUTHOR("David Howells <dhowells@redhat.com>");
MODULE_LICENSE("GPL");
/*
* Info for the fill pattern generator.
*/
static const u8 bvecq_kunit_orders[16] = {
0, 1, 2, 3,
0, 1, 3, 2,
0, 2, 1, 3,
2, 0, 3, 1
};
static const s8 bvecq_kunit_fills[16] = {
1, 2, 3, 5, 7, 11, 13, 17,
-1, -2, -3, -5, -7, -11, -13, -17
};
static struct folio *bvecq_kunit_folios[16];
static size_t bvecq_kunit_folios_nr;
/*
* Info for the fill pattern predictor.
*/
struct bvecq_test_pagemap {
u32 pos;
u32 offset;
u8 folio;
u8 subpage;
};
static struct bvecq_test_pagemap bvecq_kunit_pagemap[64];
static int bvecq_kunit_pagemap_nr;
/*
* bvecq assembly recipes.
*/
enum bvecq_test_cmd {
BVECQ_KUNIT_STOP, /* Stop loading */
BVECQ_KUNIT_ALLOC, /* Append new bvecq of size count and move to it */
BVECQ_KUNIT_LOAD, /* Load count segments into current bvecq */
BVECQ_KUNIT_EMPTY, /* Add count empty segments into current bvecq */
} __mode(byte);
struct bvecq_test_recipe {
enum bvecq_test_cmd cmd;
u8 count;
};
#define ALLOC(C) { .cmd = BVECQ_KUNIT_ALLOC, .count = C }
#define LOAD(C) { .cmd = BVECQ_KUNIT_LOAD, .count = C }
#define EMPTY(C) { .cmd = BVECQ_KUNIT_EMPTY, .count = C }
#define END { .cmd = BVECQ_KUNIT_STOP, }
static const struct bvecq_test_recipe bvecq_test_recipe_0[] = {
ALLOC(16), LOAD(16),
END
};
static const struct bvecq_test_recipe bvecq_test_recipe_1[] = {
ALLOC(8), LOAD(8),
ALLOC(8), LOAD(8),
END
};
static const struct bvecq_test_recipe bvecq_test_recipe_2[] = {
ALLOC(9), LOAD(8), EMPTY(1),
ALLOC(8), LOAD(8),
END
};
static const struct bvecq_test_recipe bvecq_test_recipe_3[] = {
ALLOC(9), EMPTY(1), LOAD(2), EMPTY(2), LOAD(2), EMPTY(1),
ALLOC(8), LOAD(4), EMPTY(2),
ALLOC(8), EMPTY(1), LOAD(4),
ALLOC(8), EMPTY(2), LOAD(4), EMPTY(1),
END
};
static const struct bvecq_test_recipe bvecq_test_recipe_4[] = {
ALLOC(9), LOAD(2), EMPTY(1),
ALLOC(2),
ALLOC(2), LOAD(2),
ALLOC(2), EMPTY(2),
ALLOC(8), LOAD(8),
END
};
static const struct bvecq_test_recipe bvecq_test_recipe_5[] = {
ALLOC(2),
ALLOC(9), LOAD(2), EMPTY(1),
ALLOC(2),
ALLOC(2), LOAD(2),
ALLOC(2), EMPTY(2),
ALLOC(8), LOAD(8),
ALLOC(2),
END
};
static const struct bvecq_test_recipe bvecq_test_recipe_6[] = {
ALLOC(2), EMPTY(1),
ALLOC(2),
ALLOC(9), LOAD(2), EMPTY(1),
ALLOC(2),
ALLOC(2), LOAD(2),
ALLOC(2), EMPTY(2),
ALLOC(8), LOAD(8),
ALLOC(2),
ALLOC(2), EMPTY(1),
END
};
/*
* Sets of variations of how much of each folio is added to the bvecq chain,
* allowing a part of the folio to be excluded from each end.
*/
struct bvecq_test_partials {
u16 add_start; /* Amount to exclude from the low end */
u16 sub_end; /* Amount to exclude from the high end */
};
/* All whole folios. */
static const struct bvecq_test_partials bvecq_test_partials_full[16] = {
[0 ... 15] = { 0, 0 }
};
/* Scattered partial folios, with some folios contiguous. */
static const struct bvecq_test_partials bvecq_test_partials_scattered[16] = {
//0, 1, 2, 3,
{ 0x277, 0x136 },
{ 0x999, 0x1011 },
{ 0x2020, 0x0 },
{ 0x0, 0x1123 },
//0, 1, 3, 2,
{ 0x0, 0x136 },
{ 0x27, 0x777 },
{ 0x771, 0x0 },
{ 0x555, 0x0 },
//0, 2, 1, 3,
{ 0x277, 0x136 },
{ 0x1123, 0x0 },
{ 0x0, 0x0 },
{ 0, 0x5011 },
//2, 0, 3, 1
{ 0x2020, 0x0 },
{ 0x277, 0x136 },
{ 0x1999, 0x1011 },
{ 0x0, 0x1010 },
};
/* Partial folios, all with a byte excluded from each end. */
static const struct bvecq_test_partials bvecq_test_partials_edges[16] = {
[0 ... 15] = { 1, 1 }
};
/*
* Clean up routines.
*/
static void bvecq_kunit_release_folios(void *data)
{
release_pages(bvecq_kunit_folios, bvecq_kunit_folios_nr);
memset(bvecq_kunit_folios, 0, sizeof(bvecq_kunit_folios));
bvecq_kunit_folios_nr = 0;
}
static void bvecq_kunit_bvecq_put(void *data)
{
bvecq_put(data);
}
/*
* Calculate the fill pattern, for a particular byte of a particular subpage of
* a particular folio.
*/
static inline u8 bvecq_kunit_pattern(unsigned int folio, int subpage, int offset)
{
int fill_step = bvecq_kunit_fills[folio];
return (subpage + offset * fill_step) & 0xff;
}
/*
* Calculate the fill pattern, given the 'byte position' in the bvecq chain.
* This looks up the folio, subpage and offset from a table generated whilst
* the bvecq is being assembled.
*/
static inline u8 bvecq_kunit_predict(struct kunit *test, size_t pos)
{
size_t last_pos = 0;
for (int i = 0; i < bvecq_kunit_pagemap_nr; i++) {
if (pos < bvecq_kunit_pagemap[i].pos)
return bvecq_kunit_pattern(
bvecq_kunit_pagemap[i].folio,
bvecq_kunit_pagemap[i].subpage,
bvecq_kunit_pagemap[i].offset + (pos - last_pos));
last_pos = bvecq_kunit_pagemap[i].pos;
}
KUNIT_FAIL_AND_ABORT(test, "Overran pagemap pos=%zx\n", pos);
return 0;
}
/*
* Determine which page we're looking at, taking all the folios to be split
* into pages. This can be used to determine what iov_iter_npages() will say.
*/
static inline int bvecq_kunit_pos2page(struct kunit *test, size_t pos)
{
for (int i = 0; i < bvecq_kunit_pagemap_nr; i++)
if (pos < bvecq_kunit_pagemap[i].pos)
return i;
if (pos > bvecq_kunit_pagemap[bvecq_kunit_pagemap_nr - 1].pos)
KUNIT_FAIL_AND_ABORT(test, "Overran pagemap pos=%zx\n", pos);
return bvecq_kunit_pagemap_nr;
}
/*
* Allocate a variety of folios of different orders and fill them with the
* pattern set for each folio.
*/
static void iov_bvecq_alloc_folios(struct kunit *test)
{
struct folio *folio, **folios = bvecq_kunit_folios;
const int nr = ARRAY_SIZE(bvecq_kunit_folios);
bvecq_kunit_pagemap_nr = 0;
memset(bvecq_kunit_pagemap, 0, sizeof(bvecq_kunit_pagemap));
memset(bvecq_kunit_folios, 0, sizeof(bvecq_kunit_folios));
if (kunit_add_action_or_reset(test, bvecq_kunit_release_folios, NULL) < 0)
KUNIT_FAIL_AND_ABORT(test, "Can't set release action (OOM)\n");
for (int i = 0; i < nr; i++) {
folio = folio_alloc(GFP_KERNEL, bvecq_kunit_orders[i]);
KUNIT_ASSERT_NOT_NULL(test, folio);
folios[i] = folio;
bvecq_kunit_folios_nr = i + 1;
/* Fill each subpage with a different but predictable pattern. */
for (int j = 0; j < folio_nr_pages(folio); j++) {
u8 *p = kmap_local_folio(folio, PAGE_SIZE * j);
for (int k = 0; k < PAGE_SIZE; k++)
p[k] = bvecq_kunit_pattern(i, j, k);
kunmap_local(p);
}
}
}
/*
* Follow the specified recipe to allocate a bvecq chain and stick the folios
* and empty gaps into it. Further, the offset into and length of each folio
* is adjusted from the partials list. Each ALLOC command will append a new
* bvecq to the chain and move to filling that.
*/
static struct bvecq *bvecq_kunit_load_chain(struct kunit *test,
const struct bvecq_test_recipe *rec,
const struct bvecq_test_partials *parts,
size_t *size)
{
struct bvecq *head = NULL, *bq = NULL, *p;
unsigned int alloc_flags = 0;
size_t pos = 0;
int folio_index = 0;
*size = 0;
for (; rec->cmd != BVECQ_KUNIT_STOP; rec++) {
if (!head)
KUNIT_ASSERT_EQ(test, rec->cmd, BVECQ_KUNIT_ALLOC);
switch (rec->cmd) {
case BVECQ_KUNIT_STOP:
break;
case BVECQ_KUNIT_ALLOC:
p = bvecq_alloc_one(rec->count, GFP_KERNEL, &alloc_flags);
KUNIT_ASSERT_NOT_NULL(test, p);
if (!head) {
head = p;
if (kunit_add_action_or_reset(
test, bvecq_kunit_bvecq_put, head) < 0)
KUNIT_FAIL_AND_ABORT(
test, "Can't set release action (OOM)\n");
} else {
bvecq_append(bq, p);
}
bq = p;
break;
case BVECQ_KUNIT_LOAD:
KUNIT_ASSERT_LE(test, folio_index + rec->count,
ARRAY_SIZE(bvecq_kunit_orders));
KUNIT_ASSERT_LE(test, bq->nr_slots + rec->count, bq->max_slots);
for (int i = 0; i < rec->count; i++) {
struct folio *folio = bvecq_kunit_folios[folio_index];
size_t flen = PAGE_SIZE << bvecq_kunit_orders[folio_index];
size_t foff = 0;
KUNIT_ASSERT_LE(test,
parts[folio_index].add_start +
parts[folio_index].sub_end,
flen);
foff += parts[folio_index].add_start;
flen -= parts[folio_index].add_start;
flen -= parts[folio_index].sub_end;
bvec_set_page(&bq->bv[bq->nr_slots + i],
folio_page(folio, 0), flen, foff);
*size += flen;
for (int j = 0; j < folio_nr_pages(folio); j++) {
struct bvecq_test_pagemap *pm;
size_t part;
if (foff >= PAGE_SIZE) {
foff -= PAGE_SIZE;
continue;
}
part = min(flen, PAGE_SIZE - foff);
pos += part;
pm = &bvecq_kunit_pagemap[bvecq_kunit_pagemap_nr];
pm->pos = pos;
pm->folio = folio_index;
pm->subpage = j;
pm->offset = foff;
bvecq_kunit_pagemap_nr++;
flen -= part;
if (!flen)
break;
foff = 0;
}
folio_index++;
}
bvecq_filled_to(bq, bq->nr_slots + rec->count);
break;
case BVECQ_KUNIT_EMPTY:
KUNIT_ASSERT_LE(test, bq->nr_slots + rec->count, bq->max_slots);
for (int i = 0; i < rec->count; i++) {
bq->bv[bq->nr_slots + i].bv_page = ERR_PTR(-EIO);
bq->bv[bq->nr_slots + i].bv_offset = 0;
bq->bv[bq->nr_slots + i].bv_len = 0;
}
bvecq_filled_to(bq, bq->nr_slots + rec->count);
break;
}
}
return head;
}
/*
* Check the next few bytes of pattern from an iterator. Note that it is
* permitted for the iterator to have less data remaining than the number to
* check - in which case the copy is required to come up short.
*/
static void bvecq_kunit_check_next(struct kunit *test, struct iov_iter *iter,
size_t *pos, size_t *left, size_t nr)
{
size_t part, n;
u8 buf[3], pred[3];
KUNIT_ASSERT_EQ(test, iov_iter_count(iter), *left);
part = min(*left, nr);
n = copy_from_iter(buf, nr, iter);
KUNIT_ASSERT_EQ(test, n, part);
*left -= n;
for (size_t i = 0; i < n; i++)
pred[i] = bvecq_kunit_predict(test, *pos + i);
for (size_t i = 0; i < n; i++) {
if (buf[i] != pred[i]) {
pr_notice(">>> pos=%05zx+%zx c=%zx buf=%*phN pred=%*phN\n",
*pos, i, iov_iter_count(iter) + n,
(int)n, buf, (int)n, pred);
}
KUNIT_ASSERT_EQ(test, buf[i], pred[i]);
}
*pos += n;
}
/*
* Test advancing and reverting over a bvecq chain.
*
* The loop starts at each 'byte position' in the chain, advancing a small
* amount, copying a few bytes of data, checking the fill pattern, reverting a
* bit, checking, reverting, checking and then reverting to a byte position one
* further on than the previous loop. Use small prime numbers for the advance
* and first revert so that we should test crossing the edges of the folios.
*/
static void bvecq_kunit_advance(struct kunit *test,
const struct bvecq_test_recipe *recipe,
const struct bvecq_test_partials *parts)
{
struct iov_iter iter;
struct bvecq *bq, *p;
unsigned long bvec_align = 0;
size_t size, pos = 0, from;
int npages;
iov_bvecq_alloc_folios(test);
bq = bvecq_kunit_load_chain(test, recipe, parts, &size);
/* Compound up the alignments for ITER_BVEC on each individual bvecq
* span and compare that to the alignment for ITER_BVECQ. There is an
* issue in ITER_BVEC, however, in that it includes bv_offset from
* bio_vecs for which bv_len is 0, which ITER_BVECQ does not.
*/
for (p = bq; p; p = p->next) {
size_t psize = 0;
for (int i = 0; i < p->nr_slots; i++)
psize += p->bv[i].bv_len;
if (!psize)
continue;
iov_iter_bvec(&iter, ITER_SOURCE, p->bv, p->nr_slots, psize);
bvec_align |= iov_iter_alignment(&iter);
}
iov_iter_bvec_queue(&iter, ITER_SOURCE, bq, 0, 0, size);
KUNIT_ASSERT_EQ(test, iov_iter_alignment(&iter), bvec_align);
npages = iov_iter_npages(&iter, INT_MAX);
from = iov_iter_count(&iter);
do {
size_t left = from, part;
KUNIT_ASSERT_EQ(test, iov_iter_npages(&iter, INT_MAX),
npages - bvecq_kunit_pos2page(test, pos));
/* 3 steps forward. */
part = min(left, 3);
iov_iter_advance(&iter, 3); /* May be short. */
pos += part;
left -= part;
bvecq_kunit_check_next(test, &iter, &pos, &left, 3);
/* 2 steps back */
part = umin(2, pos);
iov_iter_revert(&iter, part);
pos -= part;
left += part;
bvecq_kunit_check_next(test, &iter, &pos, &left, 2);
/* 1 more step back */
part = umin(1, pos);
iov_iter_revert(&iter, part);
pos -= part;
left += part;
bvecq_kunit_check_next(test, &iter, &pos, &left, 1);
KUNIT_ASSERT_EQ(test, left, iov_iter_count(&iter));
/* Move to one place on from the current loop. */
from--;
if (iov_iter_count(&iter) < from) {
part = from - iov_iter_count(&iter);
iov_iter_revert(&iter, part);
pos -= part;
}
KUNIT_ASSERT_EQ(test, from, iov_iter_count(&iter));
} while (from > 0);
}
/*
* Test jumping around in a bvecq chain using advance and revert.
*
* The loop starts at the start of the chain, then jumps forwards through the
* chain and then back again by large amounts, sampling the data each time.
*/
static void bvecq_kunit_jump(struct kunit *test,
const struct bvecq_test_recipe *recipe,
const struct bvecq_test_partials *parts)
{
struct iov_iter iter;
struct bvecq *bq;
size_t size, pos = 0, step = 0x100;
bool dir = false;
int npages;
iov_bvecq_alloc_folios(test);
bq = bvecq_kunit_load_chain(test, recipe, parts, &size);
iov_iter_bvec_queue(&iter, ITER_SOURCE, bq, 0, 0, size);
npages = iov_iter_npages(&iter, INT_MAX);
while (step < size) {
size_t left = iov_iter_count(&iter), part;
KUNIT_ASSERT_EQ(test, iov_iter_npages(&iter, INT_MAX),
npages - bvecq_kunit_pos2page(test, pos));
if (!dir) {
/* Advance by step and then 3 more in check. */
part = min(left, step);
iov_iter_advance(&iter, step); /* May be short. */
pos += part;
left -= part;
bvecq_kunit_check_next(test, &iter, &pos, &left, 3);
KUNIT_ASSERT_EQ(test, left, iov_iter_count(&iter));
if (left == 0) {
/* Switch to reverting. */
dir = true;
step += 3;
}
} else {
/* Revert by step and then advance by 3 in check. */
part = min(pos, step);
iov_iter_revert(&iter, part);
pos -= part;
left += part;
bvecq_kunit_check_next(test, &iter, &pos, &left, 3);
KUNIT_ASSERT_EQ(test, left, iov_iter_count(&iter));
if (pos <= 3) {
/* Switch to advancing. */
dir = false;
step *= 2;
}
}
}
}
/*
* Run the individual test combinations
*/
#define ADVANCE_TESTS_SET(CHECK, PARTIALS) \
TEST(CHECK, PARTIALS, 0) \
TEST(CHECK, PARTIALS, 1) \
TEST(CHECK, PARTIALS, 2) \
TEST(CHECK, PARTIALS, 3) \
TEST(CHECK, PARTIALS, 4) \
TEST(CHECK, PARTIALS, 5) \
TEST(CHECK, PARTIALS, 6)
#define ADVANCE_TESTS(CHECK) \
ADVANCE_TESTS_SET(CHECK, full) \
ADVANCE_TESTS_SET(CHECK, scattered) \
ADVANCE_TESTS_SET(CHECK, edges)
#define TEST(CHECK, PARTIALS, RECIPE) \
static void bvecq_kunit_##CHECK##_##PARTIALS##_##RECIPE(struct kunit *test) \
{ \
bvecq_kunit_##CHECK(test, \
bvecq_test_recipe_##RECIPE, \
bvecq_test_partials_##PARTIALS); \
}
/* Emit test stub functions. */
ADVANCE_TESTS(advance);
ADVANCE_TESTS(jump);
#undef TEST
#define TEST(CHECK, PARTIALS, RECIPE) \
KUNIT_CASE(bvecq_kunit_##CHECK##_##PARTIALS##_##RECIPE),
static struct kunit_case bvecq_kunit_cases[] = {
ADVANCE_TESTS(advance)
ADVANCE_TESTS(jump)
{}
};
static struct kunit_suite bvecq_kunit_suite = {
.name = "bvecq",
.test_cases = bvecq_kunit_cases,
};
kunit_test_suites(&bvecq_kunit_suite);