mirror_zfs/include/os/linux/kernel/linux/mod_compat.h
Rob Norris 06a196020e vdev_disk: rewrite BIO filling machinery to avoid split pages
This commit tackles a number of issues in the way BIOs (`struct bio`)
are constructed for submission to the Linux block layer.

The kernel has a hard upper limit on the number of pages/segments that
can be added to a BIO, as well as a separate limit for each device
(related to its queue depth and other scheduling characteristics).

ZFS counts the number of memory pages in the request ABD
(`abd_nr_pages_off()`, and then uses that as the number of segments to
put into the BIO, up to the hard upper limit. If it requires more than
the limit, it will create multiple BIOs.

Leaving aside the fact that page count method is wrong (see below), not
limiting to the device segment max means that the device driver will
need to split the BIO in half. This is alone is not necessarily a
problem, but it interacts with another issue to cause a much larger
problem.

The kernel function to add a segment to a BIO (`bio_add_page()`) takes a
`struct page` pointer, and offset+len within it. `struct page` can
represent a run of contiguous memory pages (known as a "compound page").
In can be of arbitrary length.

The ZFS functions that count ABD pages and load them into the BIO
(`abd_nr_pages_off()`, `bio_map()` and `abd_bio_map_off()`) will never
consider a page to be more than `PAGE_SIZE` (4K), even if the `struct
page` is for multiple pages. In this case, it will load the same `struct
page` into the BIO multiple times, with the offset adjusted each time.

With a sufficiently large ABD, this can easily lead to the BIO being
entirely filled much earlier than it could have been. This is also
further contributes to the problem caused by the incorrect segment limit
calculation, as its much easier to go past the device limit, and so
require a split.

Again, this is not a problem on its own.

The logic for "never submit more than `PAGE_SIZE`" is actually a little
more subtle. It will actually never submit a buffer that crosses a 4K
page boundary.

In practice, this is fine, as most ABDs are scattered, that is a list of
complete 4K pages, and so are loaded in as such.

Linear ABDs are typically allocated from slabs, and for small sizes they
are frequently not aligned to page boundaries. For example, a 12K
allocation can span four pages, eg:

     -- 4K -- -- 4K -- -- 4K -- -- 4K --
    |        |        |        |        |
          :## ######## ######## ######:    [1K, 4K, 4K, 3K]

Such an allocation would be loaded into a BIO as you see:

    [1K, 4K, 4K, 3K]

This tends not to be a problem in practice, because even if the BIO were
filled and needed to be split, each half would still have either a start
or end aligned to the logical block size of the device (assuming 4K at
least).

---

In ideal circumstances, these shortcomings don't cause any particular
problems. Its when they start to interact with other ZFS features that
things get interesting.

Aggregation will create a "gang" ABD, which is simply a list of other
ABDs. Iterating over a gang ABD is just iterating over each ABD within
it in turn.

Because the segments are simply loaded in order, we can end up with
uneven segments either side of the "gap" between the two ABDs. For
example, two 12K ABDs might be aggregated and then loaded as:

    [1K, 4K, 4K, 3K, 2K, 4K, 4K, 2K]

Should a split occur, each individual BIO can end up either having an
start or end offset that is not aligned to the logical block size, which
some drivers (eg SCSI) will reject. However, this tends not to happen
because the default aggregation limit usually keeps the BIO small enough
to not require more than one split, and most pages are actually full 4K
pages, so hitting an uneven gap is very rare anyway.

If the pool is under particular memory pressure, then an IO can be
broken down into a "gang block", a 512-byte block composed of a header
and up to three block pointers. Each points to a fragment of the
original write, or in turn, another gang block, breaking the original
data up over and over until space can be found in the pool for each of
them.

Each gang header is a separate 512-byte memory allocation from a slab,
that needs to be written down to disk. When the gang header is added to
the BIO, its a single 512-byte segment.

Pulling all this together, consider a large aggregated write of gang
blocks. This results a BIO containing lots of 512-byte segments. Given
our tendency to overfill the BIO, a split is likely, and most possible
split points will yield a pair of BIOs that are misaligned. Drivers that
care, like the SCSI driver, will reject them.

---

This commit is a substantial refactor and rewrite of much of `vdev_disk`
to sort all this out.

`vdev_bio_max_segs()` now returns the ideal maximum size for the device,
if available. There's also a tuneable `zfs_vdev_disk_max_segs` to
override this, to assist with testing.

We scan the ABD up front to count the number of pages within it, and to
confirm that if we submitted all those pages to one or more BIOs, it
could be split at any point with creating a misaligned BIO.  If the
pages in the BIO are not usable (as in any of the above situations), the
ABD is linearised, and then checked again. This is the same technique
used in `vdev_geom` on FreeBSD, adjusted for Linux's variable page size
and allocator quirks.

`vbio_t` is a cleanup and enhancement of the old `dio_request_t`. The
idea is simply that it can hold all the state needed to create, submit
and return multiple BIOs, including all the refcounts, the ABD copy if
it was needed, and so on. Apart from what I hope is a clearer interface,
the major difference is that because we know how many BIOs we'll need up
front, we don't need the old overflow logic that would grow the BIO
array, throw away all the old work and restart. We can get it right from
the start.

Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <rob.norris@klarasystems.com>
Sponsored-by: Klara, Inc.
Sponsored-by: Wasabi Technology, Inc.
Closes #15533
Closes #15588
2024-03-25 16:51:14 -07:00

206 lines
6.8 KiB
C

/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or https://opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (C) 2016 Gvozden Neskovic <neskovic@gmail.com>.
* Copyright (c) 2020 by Delphix. All rights reserved.
*/
#ifndef _MOD_COMPAT_H
#define _MOD_COMPAT_H
#include <linux/module.h>
#include <linux/moduleparam.h>
/*
* Despite constifying struct kernel_param_ops, some older kernels define a
* `__check_old_set_param()` function in their headers that checks for a
* non-constified `->set()`. This has long been fixed in Linux mainline, but
* since we support older kernels, we workaround it by using a preprocessor
* definition to disable it.
*/
#define __check_old_set_param(_) (0)
typedef const struct kernel_param zfs_kernel_param_t;
#define ZMOD_RW 0644
#define ZMOD_RD 0444
enum scope_prefix_types {
zfs,
zfs_arc,
zfs_brt,
zfs_condense,
zfs_dbuf,
zfs_dbuf_cache,
zfs_deadman,
zfs_dedup,
zfs_l2arc,
zfs_livelist,
zfs_livelist_condense,
zfs_lua,
zfs_metaslab,
zfs_mg,
zfs_multihost,
zfs_prefetch,
zfs_reconstruct,
zfs_recv,
zfs_send,
zfs_spa,
zfs_trim,
zfs_txg,
zfs_vdev,
zfs_vdev_disk,
zfs_vdev_file,
zfs_vdev_mirror,
zfs_vnops,
zfs_zevent,
zfs_zio,
zfs_zil
};
/*
* While we define our own s64/u64 types, there is no reason to reimplement the
* existing Linux kernel types, so we use the preprocessor to remap our
* "custom" implementations to the kernel ones. This is done because the CPP
* does not allow us to write conditional definitions. The fourth definition
* exists because the CPP will not allow us to replace things like INT with int
* before string concatenation.
*/
#define spl_param_set_int param_set_int
#define spl_param_get_int param_get_int
#define spl_param_ops_int param_ops_int
#define spl_param_ops_INT param_ops_int
#define spl_param_set_long param_set_long
#define spl_param_get_long param_get_long
#define spl_param_ops_long param_ops_long
#define spl_param_ops_LONG param_ops_long
#define spl_param_set_uint param_set_uint
#define spl_param_get_uint param_get_uint
#define spl_param_ops_uint param_ops_uint
#define spl_param_ops_UINT param_ops_uint
#define spl_param_set_ulong param_set_ulong
#define spl_param_get_ulong param_get_ulong
#define spl_param_ops_ulong param_ops_ulong
#define spl_param_ops_ULONG param_ops_ulong
#define spl_param_set_charp param_set_charp
#define spl_param_get_charp param_get_charp
#define spl_param_ops_charp param_ops_charp
#define spl_param_ops_STRING param_ops_charp
int spl_param_set_s64(const char *val, zfs_kernel_param_t *kp);
extern int spl_param_get_s64(char *buffer, zfs_kernel_param_t *kp);
extern const struct kernel_param_ops spl_param_ops_s64;
#define spl_param_ops_S64 spl_param_ops_s64
extern int spl_param_set_u64(const char *val, zfs_kernel_param_t *kp);
extern int spl_param_get_u64(char *buffer, zfs_kernel_param_t *kp);
extern const struct kernel_param_ops spl_param_ops_u64;
#define spl_param_ops_U64 spl_param_ops_u64
/*
* Declare a module parameter / sysctl node
*
* "scope_prefix" the part of the sysctl / sysfs tree the node resides under
* (currently a no-op on Linux)
* "name_prefix" the part of the variable name that will be excluded from the
* exported names on platforms with a hierarchical namespace
* "name" the part of the variable that will be exposed on platforms with a
* hierarchical namespace, or as name_prefix ## name on Linux
* "type" the variable type
* "perm" the permissions (read/write or read only)
* "desc" a brief description of the option
*
* Examples:
* ZFS_MODULE_PARAM(zfs_vdev_mirror, zfs_vdev_mirror_, rotating_inc, UINT,
* ZMOD_RW, "Rotating media load increment for non-seeking I/O's");
* on FreeBSD:
* vfs.zfs.vdev.mirror.rotating_inc
* on Linux:
* zfs_vdev_mirror_rotating_inc
*
* ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, UINT, ZMOD_RW,
* "Limit one prefetch call to this size");
* on FreeBSD:
* vfs.zfs.dmu_prefetch_max
* on Linux:
* dmu_prefetch_max
*/
#define ZFS_MODULE_PARAM(scope_prefix, name_prefix, name, type, perm, desc) \
_Static_assert( \
sizeof (scope_prefix) == sizeof (enum scope_prefix_types), \
"" #scope_prefix " size mismatch with enum scope_prefix_types"); \
module_param_cb(name_prefix ## name, &spl_param_ops_ ## type, \
&name_prefix ## name, perm); \
MODULE_PARM_DESC(name_prefix ## name, desc)
/*
* Declare a module parameter / sysctl node
*
* "scope_prefix" the part of the the sysctl / sysfs tree the node resides under
* (currently a no-op on Linux)
* "name_prefix" the part of the variable name that will be excluded from the
* exported names on platforms with a hierarchical namespace
* "name" the part of the variable that will be exposed on platforms with a
* hierarchical namespace, or as name_prefix ## name on Linux
* "setfunc" setter function
* "getfunc" getter function
* "perm" the permissions (read/write or read only)
* "desc" a brief description of the option
*
* Examples:
* ZFS_MODULE_PARAM_CALL(zfs_spa, spa_, slop_shift, param_set_slop_shift,
* param_get_int, ZMOD_RW, "Reserved free space in pool");
* on FreeBSD:
* vfs.zfs.spa_slop_shift
* on Linux:
* spa_slop_shift
*/
#define ZFS_MODULE_PARAM_CALL( \
scope_prefix, name_prefix, name, setfunc, getfunc, perm, desc) \
_Static_assert( \
sizeof (scope_prefix) == sizeof (enum scope_prefix_types), \
"" #scope_prefix " size mismatch with enum scope_prefix_types"); \
module_param_call(name_prefix ## name, setfunc, getfunc, \
&name_prefix ## name, perm); \
MODULE_PARM_DESC(name_prefix ## name, desc)
/*
* As above, but there is no variable with the name name_prefix ## name,
* so NULL is passed to module_param_call instead.
*/
#define ZFS_MODULE_VIRTUAL_PARAM_CALL( \
scope_prefix, name_prefix, name, setfunc, getfunc, perm, desc) \
_Static_assert( \
sizeof (scope_prefix) == sizeof (enum scope_prefix_types), \
"" #scope_prefix " size mismatch with enum scope_prefix_types"); \
module_param_call(name_prefix ## name, setfunc, getfunc, NULL, perm); \
MODULE_PARM_DESC(name_prefix ## name, desc)
#define ZFS_MODULE_PARAM_ARGS const char *buf, zfs_kernel_param_t *kp
#endif /* _MOD_COMPAT_H */