Skip to content

Commit

Permalink
lz4_zfs: FreeBSD-local changes to use a custom allocator.
Browse files Browse the repository at this point in the history
This is not associated with a specific upstream commit but apparently
a local diff applied as part of:

commit e92ffd9b626833ebdbf2742c8ffddc6cd94b963e
Merge: 3c3df3660072 17b2ae0
Author: Martin Matuska <mm@FreeBSD.org>
Date:   Sat Jan 22 23:05:15 2022 +0100

    zfs: merge openzfs/zfs@17b2ae0b2 (master) into main

    Notable upstream pull request merges:
      openzfs#12766 Fix error propagation from lzc_send_redacted
      openzfs#12805 Updated the lz4 decompressor
      openzfs#12851 FreeBSD: Provide correct file generation number
      openzfs#12857 Verify dRAID empty sectors
      openzfs#12874 FreeBSD: Update argument types for VOP_READDIR
      openzfs#12896 Reduce number of arc_prune threads
      openzfs#12934 FreeBSD: Fix zvol_*_open() locking
      openzfs#12947 lz4: Cherrypick fix for CVE-2021-3520
      openzfs#12961 FreeBSD: Fix leaked strings in libspl mnttab
      openzfs#12964 Fix handling of errors from dmu_write_uio_dbuf() on FreeBSD
      openzfs#12981 Introduce a flag to skip comparing the local mac when raw sending
      openzfs#12985 Avoid memory allocations in the ARC eviction thread

    Obtained from:  OpenZFS
    OpenZFS commit: 17b2ae0
  • Loading branch information
mmatuska authored and bsdjhb committed Jul 12, 2023
1 parent c59bcbc commit f514117
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions module/zfs/lz4_zfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ static int LZ4_compress64kCtx(void *ctx, const char *source, char *dest,
int LZ4_uncompress_unknownOutputSize(const char *source, char *dest,
int isize, int maxOutputSize);

static kmem_cache_t *lz4_cache;
static void *lz4_alloc(int flags);
static void lz4_free(void *ctx);

size_t
lz4_compress_zfs(void *s_start, void *d_start, size_t s_len,
Expand Down Expand Up @@ -842,8 +843,7 @@ real_LZ4_compress(const char *source, char *dest, int isize, int osize)
void *ctx;
int result;

ASSERT(lz4_cache != NULL);
ctx = kmem_cache_alloc(lz4_cache, KM_SLEEP);
ctx = lz4_alloc(KM_SLEEP);

/*
* out of kernel memory, gently fall through - this will disable
Expand All @@ -859,10 +859,29 @@ real_LZ4_compress(const char *source, char *dest, int isize, int osize)
else
result = LZ4_compressCtx(ctx, source, dest, isize, osize);

kmem_cache_free(lz4_cache, ctx);
lz4_free(ctx);
return (result);
}

#ifdef __FreeBSD__
/*
* FreeBSD has 4, 8 and 16 KB malloc zones which can be used here.
* Should struct refTables get resized this may need to be revisited, hence
* compiler-time asserts.
*/
_Static_assert(sizeof(struct refTables) <= 16384,
"refTables too big for malloc");
_Static_assert((sizeof(struct refTables) % 4096) == 0,
"refTables not a multiple of page size");
#else
#define ZFS_LZ4_USE_CACHE
#endif

#ifdef ZFS_LZ4_USE_CACHE
static kmem_cache_t *lz4_cache;
#endif

#ifdef ZFS_LZ4_USE_CACHE
void
lz4_init(void)
{
Expand All @@ -878,3 +897,39 @@ lz4_fini(void)
lz4_cache = NULL;
}
}

static void *
lz4_alloc(int flags)
{
ASSERT(lz4_cache != NULL);
return (kmem_cache_alloc(lz4_cache, flags));
}

static void
lz4_free(void *ctx)
{
kmem_cache_free(lz4_cache, ctx);
}
#else
void
lz4_init(void)
{
}

void
lz4_fini(void)
{
}

static void *
lz4_alloc(int flags)
{
return (kmem_alloc(sizeof (struct refTables), flags));
}

static void
lz4_free(void *ctx)
{
kmem_free(ctx, sizeof (struct refTables));
}
#endif

0 comments on commit f514117

Please sign in to comment.