Path: blob/main/sys/contrib/openzfs/lib/libzpool/abd_os.c
48378 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (c) 2014 by Chunwei Chen. All rights reserved.23* Copyright (c) 2019 by Delphix. All rights reserved.24* Copyright (c) 2023, 2024, Klara Inc.25*/2627#include <sys/abd_impl.h>28#include <sys/param.h>29#include <sys/zio.h>30#include <sys/arc.h>31#include <sys/zfs_context.h>32#include <sys/zfs_znode.h>3334/*35* We're simulating scatter/gather with 4K allocations, since that's more like36* what a typical kernel does.37*/38#define ABD_PAGESIZE (4096)39#define ABD_PAGESHIFT (12)40#define ABD_PAGEMASK (ABD_PAGESIZE-1)4142/*43* See rationale in module/os/linux/zfs/abd_os.c, but in userspace this is44* mostly useful to get a mix of linear and scatter ABDs for testing.45*/46#define ABD_SCATTER_MIN_SIZE (512 * 3)4748abd_t *abd_zero_scatter = NULL;4950static uint_t51abd_iovcnt_for_bytes(size_t size)52{53/*54* Each iovec points to a 4K page. There's no real reason to do this55* in userspace, but our whole point here is to make it feel a bit56* more like a real paged memory model.57*/58return (P2ROUNDUP(size, ABD_PAGESIZE) / ABD_PAGESIZE);59}6061abd_t *62abd_alloc_struct_impl(size_t size)63{64/*65* Zero-sized means it will be used for a linear or gang abd, so just66* allocate the abd itself and return.67*/68if (size == 0)69return (umem_alloc(sizeof (abd_t), UMEM_NOFAIL));7071/*72* Allocating for a scatter abd, so compute how many ABD_PAGESIZE73* iovecs we will need to hold this size. Append that allocation to the74* end. Note that struct abd_scatter has includes abd_iov[1], so we75* allocate one less iovec than we need.76*77* Note we're not allocating the pages proper, just the iovec pointers.78* That's down in abd_alloc_chunks. We _could_ do it here in a single79* allocation, but it's fiddly and harder to read for no real gain.80*/81uint_t n = abd_iovcnt_for_bytes(size);82abd_t *abd = umem_alloc(sizeof (abd_t) + (n-1) * sizeof (struct iovec),83UMEM_NOFAIL);84ABD_SCATTER(abd).abd_offset = 0;85ABD_SCATTER(abd).abd_iovcnt = n;86return (abd);87}8889void90abd_free_struct_impl(abd_t *abd)91{92/* For scatter, compute the extra amount we need to free */93uint_t iovcnt =94abd_is_linear(abd) || abd_is_gang(abd) ?950 : (ABD_SCATTER(abd).abd_iovcnt - 1);96umem_free(abd, sizeof (abd_t) + iovcnt * sizeof (struct iovec));97}9899void100abd_alloc_chunks(abd_t *abd, size_t size)101{102/*103* We've already allocated the iovec array; ensure that the wanted size104* actually matches, otherwise the caller has made a mistake somewhere.105*/106uint_t n = ABD_SCATTER(abd).abd_iovcnt;107ASSERT3U(n, ==, abd_iovcnt_for_bytes(size));108109/*110* Allocate a ABD_PAGESIZE region for each iovec.111*/112struct iovec *iov = ABD_SCATTER(abd).abd_iov;113for (int i = 0; i < n; i++) {114iov[i].iov_base =115umem_alloc_aligned(ABD_PAGESIZE, ABD_PAGESIZE, UMEM_NOFAIL);116iov[i].iov_len = ABD_PAGESIZE;117}118}119120void121abd_free_chunks(abd_t *abd)122{123uint_t n = ABD_SCATTER(abd).abd_iovcnt;124struct iovec *iov = ABD_SCATTER(abd).abd_iov;125for (int i = 0; i < n; i++)126umem_free_aligned(iov[i].iov_base, ABD_PAGESIZE);127}128129boolean_t130abd_size_alloc_linear(size_t size)131{132return (size < ABD_SCATTER_MIN_SIZE);133}134135void136abd_update_scatter_stats(abd_t *abd, abd_stats_op_t op)137{138ASSERT(op == ABDSTAT_INCR || op == ABDSTAT_DECR);139int waste = P2ROUNDUP(abd->abd_size, ABD_PAGESIZE) - abd->abd_size;140if (op == ABDSTAT_INCR) {141arc_space_consume(waste, ARC_SPACE_ABD_CHUNK_WASTE);142} else {143arc_space_return(waste, ARC_SPACE_ABD_CHUNK_WASTE);144}145}146147void148abd_update_linear_stats(abd_t *abd, abd_stats_op_t op)149{150(void) abd;151(void) op;152ASSERT(op == ABDSTAT_INCR || op == ABDSTAT_DECR);153}154155void156abd_verify_scatter(abd_t *abd)157{158#ifdef ZFS_DEBUG159/*160* scatter abds shall have:161* - at least one iovec162* - all iov_base point somewhere163* - all iov_len are ABD_PAGESIZE164* - offset set within the abd pages somewhere165*/166uint_t n = ABD_SCATTER(abd).abd_iovcnt;167ASSERT3U(n, >, 0);168169uint_t len = 0;170for (int i = 0; i < n; i++) {171ASSERT3P(ABD_SCATTER(abd).abd_iov[i].iov_base, !=, NULL);172ASSERT3U(ABD_SCATTER(abd).abd_iov[i].iov_len, ==, ABD_PAGESIZE);173len += ABD_PAGESIZE;174}175176ASSERT3U(ABD_SCATTER(abd).abd_offset, <, len);177#endif178}179180void181abd_init(void)182{183/*184* Create the "zero" scatter abd. This is always the size of the185* largest possible block, but only actually has a single allocated186* page, which all iovecs in the abd point to.187*/188abd_zero_scatter = abd_alloc_struct(SPA_MAXBLOCKSIZE);189abd_zero_scatter->abd_flags |= ABD_FLAG_OWNER;190abd_zero_scatter->abd_size = SPA_MAXBLOCKSIZE;191192void *zero =193umem_alloc_aligned(ABD_PAGESIZE, ABD_PAGESIZE, UMEM_NOFAIL);194memset(zero, 0, ABD_PAGESIZE);195196uint_t n = abd_iovcnt_for_bytes(SPA_MAXBLOCKSIZE);197struct iovec *iov = ABD_SCATTER(abd_zero_scatter).abd_iov;198for (int i = 0; i < n; i++) {199iov[i].iov_base = zero;200iov[i].iov_len = ABD_PAGESIZE;201}202}203204void205abd_fini(void)206{207umem_free_aligned(208ABD_SCATTER(abd_zero_scatter).abd_iov[0].iov_base, ABD_PAGESIZE);209abd_free_struct(abd_zero_scatter);210abd_zero_scatter = NULL;211}212213void214abd_free_linear_page(abd_t *abd)215{216/*217* LINEAR_PAGE is specific to the Linux kernel; we never set this218* flag, so this will never be called.219*/220(void) abd;221PANIC("unreachable");222}223224abd_t *225abd_alloc_for_io(size_t size, boolean_t is_metadata)226{227return (abd_alloc(size, is_metadata));228}229230abd_t *231abd_get_offset_scatter(abd_t *dabd, abd_t *sabd, size_t off, size_t size)232{233234/*235* Create a new scatter dabd by borrowing data pages from sabd to cover236* off+size.237*238* sabd is an existing scatter abd with a set of iovecs, each covering239* an ABD_PAGESIZE (4K) allocation. It's "zero" is at abd_offset.240*241* [........][........][........][........]242* ^- sabd_offset243*244* We want to produce a new abd, referencing those allocations at the245* given offset.246*247* [........][........][........][........]248* ^- dabd_offset = sabd_offset + off249* ^- dabd_offset + size250*251* In this example, dabd needs three iovecs. The first iovec is offset252* 0, so the final dabd_offset is masked back into the first iovec.253*254* [........][........][........]255* ^- dabd_offset256*/257size_t soff = ABD_SCATTER(sabd).abd_offset + off;258size_t doff = soff & ABD_PAGEMASK;259size_t iovcnt = abd_iovcnt_for_bytes(doff + size);260261/*262* If the passed-in abd has enough allocated iovecs already, reuse it.263* Otherwise, make a new one. The caller will free the original if the264* one it gets back is not the same.265*266* Note that it's ok if we reuse an abd with more iovecs than we need.267* abd_size has the usable amount of data, and the abd does not own the268* pages referenced by the iovecs. At worst, they're holding dangling269* pointers that we'll never use anyway.270*/271if (dabd == NULL || ABD_SCATTER(dabd).abd_iovcnt < iovcnt)272dabd = abd_alloc_struct(iovcnt << ABD_PAGESHIFT);273274/* Set offset into first page in view */275ABD_SCATTER(dabd).abd_offset = doff;276277/* Copy the wanted iovecs from the source to the dest */278memcpy(&ABD_SCATTER(dabd).abd_iov[0],279&ABD_SCATTER(sabd).abd_iov[soff >> ABD_PAGESHIFT],280iovcnt * sizeof (struct iovec));281282return (dabd);283}284285void286abd_iter_init(struct abd_iter *aiter, abd_t *abd)287{288ASSERT(!abd_is_gang(abd));289abd_verify(abd);290memset(aiter, 0, sizeof (struct abd_iter));291aiter->iter_abd = abd;292}293294boolean_t295abd_iter_at_end(struct abd_iter *aiter)296{297ASSERT3U(aiter->iter_pos, <=, aiter->iter_abd->abd_size);298return (aiter->iter_pos == aiter->iter_abd->abd_size);299}300301void302abd_iter_advance(struct abd_iter *aiter, size_t amount)303{304ASSERT0P(aiter->iter_mapaddr);305ASSERT0(aiter->iter_mapsize);306307if (abd_iter_at_end(aiter))308return;309310aiter->iter_pos += amount;311ASSERT3U(aiter->iter_pos, <=, aiter->iter_abd->abd_size);312}313314void315abd_iter_map(struct abd_iter *aiter)316{317ASSERT0P(aiter->iter_mapaddr);318ASSERT0(aiter->iter_mapsize);319320if (abd_iter_at_end(aiter))321return;322323if (abd_is_linear(aiter->iter_abd)) {324aiter->iter_mapaddr =325ABD_LINEAR_BUF(aiter->iter_abd) + aiter->iter_pos;326aiter->iter_mapsize =327aiter->iter_abd->abd_size - aiter->iter_pos;328return;329}330331/*332* For scatter, we index into the appropriate iovec, and return the333* smaller of the amount requested, or up to the end of the page.334*/335size_t poff = aiter->iter_pos + ABD_SCATTER(aiter->iter_abd).abd_offset;336337ASSERT3U(poff >> ABD_PAGESHIFT, <=,338ABD_SCATTER(aiter->iter_abd).abd_iovcnt);339struct iovec *iov = &ABD_SCATTER(aiter->iter_abd).340abd_iov[poff >> ABD_PAGESHIFT];341342aiter->iter_mapsize = MIN(ABD_PAGESIZE - (poff & ABD_PAGEMASK),343aiter->iter_abd->abd_size - aiter->iter_pos);344ASSERT3U(aiter->iter_mapsize, <=, ABD_PAGESIZE);345346aiter->iter_mapaddr = iov->iov_base + (poff & ABD_PAGEMASK);347}348349void350abd_iter_unmap(struct abd_iter *aiter)351{352if (abd_iter_at_end(aiter))353return;354355ASSERT3P(aiter->iter_mapaddr, !=, NULL);356ASSERT3U(aiter->iter_mapsize, >, 0);357358aiter->iter_mapaddr = NULL;359aiter->iter_mapsize = 0;360}361362void363abd_cache_reap_now(void)364{365}366367/*368* Borrow a raw buffer from an ABD without copying the contents of the ABD369* into the buffer. If the ABD is scattered, this will alloate a raw buffer370* whose contents are undefined. To copy over the existing data in the ABD, use371* abd_borrow_buf_copy() instead.372*/373void *374abd_borrow_buf(abd_t *abd, size_t n)375{376void *buf;377abd_verify(abd);378ASSERT3U(abd->abd_size, >=, 0);379if (abd_is_linear(abd)) {380buf = abd_to_buf(abd);381} else {382buf = zio_buf_alloc(n);383}384#ifdef ZFS_DEBUG385(void) zfs_refcount_add_many(&abd->abd_children, n, buf);386#endif387return (buf);388}389390void *391abd_borrow_buf_copy(abd_t *abd, size_t n)392{393void *buf = abd_borrow_buf(abd, n);394if (!abd_is_linear(abd)) {395abd_copy_to_buf(buf, abd, n);396}397return (buf);398}399400/*401* Return a borrowed raw buffer to an ABD. If the ABD is scattered, this will402* no change the contents of the ABD and will ASSERT that you didn't modify403* the buffer since it was borrowed. If you want any changes you made to buf to404* be copied back to abd, use abd_return_buf_copy() instead.405*/406void407abd_return_buf(abd_t *abd, void *buf, size_t n)408{409abd_verify(abd);410ASSERT3U(abd->abd_size, >=, n);411#ifdef ZFS_DEBUG412(void) zfs_refcount_remove_many(&abd->abd_children, n, buf);413#endif414if (abd_is_linear(abd)) {415ASSERT3P(buf, ==, abd_to_buf(abd));416} else {417ASSERT0(abd_cmp_buf(abd, buf, n));418zio_buf_free(buf, n);419}420}421422void423abd_return_buf_copy(abd_t *abd, void *buf, size_t n)424{425if (!abd_is_linear(abd)) {426abd_copy_from_buf(abd, buf, n);427}428abd_return_buf(abd, buf, n);429}430431432