2014-12-08 21:35:51 +03:00
|
|
|
/*
|
2014-12-08 21:04:42 +03:00
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
|
|
|
*
|
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
2014-12-08 21:35:51 +03:00
|
|
|
*/
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2020-07-16 01:58:15 +03:00
|
|
|
#include <linux/percpu_compat.h>
|
2014-12-08 21:04:42 +03:00
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/kmem_cache.h>
|
|
|
|
#include <sys/taskq.h>
|
|
|
|
#include <sys/timer.h>
|
|
|
|
#include <sys/vmem.h>
|
2018-02-15 04:01:15 +03:00
|
|
|
#include <sys/wait.h>
|
2014-12-08 21:04:42 +03:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/swap.h>
|
2015-11-25 16:35:22 +03:00
|
|
|
#include <linux/prefetch.h>
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Within the scope of spl-kmem.c file the kmem_cache_* definitions
|
|
|
|
* are removed to allow access to the real Linux slab allocator.
|
|
|
|
*/
|
|
|
|
#undef kmem_cache_destroy
|
|
|
|
#undef kmem_cache_create
|
|
|
|
#undef kmem_cache_alloc
|
|
|
|
#undef kmem_cache_free
|
|
|
|
|
|
|
|
|
Enforce architecture-specific barriers around clear_bit()
The comment above the Linux 3.16 kernel's clear_bit() states:
/**
* clear_bit - Clears a bit in memory
* @nr: Bit to clear
* @addr: Address to start counting from
*
* clear_bit() is atomic and may not be reordered. However, it does
* not contain a memory barrier, so if it is used for locking purposes,
* you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
* in order to ensure changes are visible on other processors.
*/
This comment does not make sense in the context of x86 because x86 maps the
operations to barrier(), which is a compiler barrier. However, it does make
sense to me when I consider architectures that reorder around atomic
instructions. In such situations, a processor is allowed to execute the
wake_up_bit() before clear_bit() and we have a race. There are a few
architectures that suffer from this issue.
In such situations, the other processor would wake-up, see the bit is still
taken and go to sleep, while the one responsible for waking it up will
assume that it did its job and continue.
This patch implements a wrapper that maps smp_mb__{before,after}_atomic() to
smp_mb__{before,after}_clear_bit() on older kernels and changes our code to
leverage it in a manner consistent with the mainline kernel.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-05 02:47:51 +03:00
|
|
|
/*
|
|
|
|
* Linux 3.16 replaced smp_mb__{before,after}_{atomic,clear}_{dec,inc,bit}()
|
|
|
|
* with smp_mb__{before,after}_atomic() because they were redundant. This is
|
|
|
|
* only used inside our SLAB allocator, so we implement an internal wrapper
|
|
|
|
* here to give us smp_mb__{before,after}_atomic() on older kernels.
|
|
|
|
*/
|
|
|
|
#ifndef smp_mb__before_atomic
|
|
|
|
#define smp_mb__before_atomic(x) smp_mb__before_clear_bit(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef smp_mb__after_atomic
|
|
|
|
#define smp_mb__after_atomic(x) smp_mb__after_clear_bit(x)
|
|
|
|
#endif
|
|
|
|
|
2018-02-24 21:05:37 +03:00
|
|
|
/* BEGIN CSTYLED */
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2014-12-06 01:11:18 +03:00
|
|
|
/*
|
|
|
|
* Cache magazines are an optimization designed to minimize the cost of
|
|
|
|
* allocating memory. They do this by keeping a per-cpu cache of recently
|
|
|
|
* freed objects, which can then be reallocated without taking a lock. This
|
|
|
|
* can improve performance on highly contended caches. However, because
|
|
|
|
* objects in magazines will prevent otherwise empty slabs from being
|
|
|
|
* immediately released this may not be ideal for low memory machines.
|
|
|
|
*
|
|
|
|
* For this reason spl_kmem_cache_magazine_size can be used to set a maximum
|
|
|
|
* magazine size. When this value is set to 0 the magazine size will be
|
|
|
|
* automatically determined based on the object size. Otherwise magazines
|
|
|
|
* will be limited to 2-256 objects per magazine (i.e per cpu). Magazines
|
|
|
|
* may never be entirely disabled in this implementation.
|
|
|
|
*/
|
|
|
|
unsigned int spl_kmem_cache_magazine_size = 0;
|
|
|
|
module_param(spl_kmem_cache_magazine_size, uint, 0444);
|
|
|
|
MODULE_PARM_DESC(spl_kmem_cache_magazine_size,
|
2015-11-13 22:00:05 +03:00
|
|
|
"Default magazine size (2-256), set automatically (0)");
|
2014-12-06 01:11:18 +03:00
|
|
|
|
2014-12-08 21:04:42 +03:00
|
|
|
/*
|
|
|
|
* The default behavior is to report the number of objects remaining in the
|
|
|
|
* cache. This allows the Linux VM to repeatedly reclaim objects from the
|
|
|
|
* cache when memory is low satisfy other memory allocations. Alternately,
|
|
|
|
* setting this value to KMC_RECLAIM_ONCE limits how aggressively the cache
|
|
|
|
* is reclaimed. This may increase the likelihood of out of memory events.
|
|
|
|
*/
|
|
|
|
unsigned int spl_kmem_cache_reclaim = 0 /* KMC_RECLAIM_ONCE */;
|
|
|
|
module_param(spl_kmem_cache_reclaim, uint, 0644);
|
|
|
|
MODULE_PARM_DESC(spl_kmem_cache_reclaim, "Single reclaim pass (0x1)");
|
|
|
|
|
|
|
|
unsigned int spl_kmem_cache_obj_per_slab = SPL_KMEM_CACHE_OBJ_PER_SLAB;
|
|
|
|
module_param(spl_kmem_cache_obj_per_slab, uint, 0644);
|
|
|
|
MODULE_PARM_DESC(spl_kmem_cache_obj_per_slab, "Number of objects per slab");
|
|
|
|
|
Refine slab cache sizing
This change is designed to improve the memory utilization of
slabs by more carefully setting their size. The way the code
currently works is problematic for slabs which contain large
objects (>1MB). This is due to slabs being unconditionally
rounded up to a power of two which may result in unused space
at the end of the slab.
The reason the existing code rounds up every slab is because it
assumes it will backed by the buddy allocator. Since the buddy
allocator can only performs power of two allocations this is
desirable because it avoids wasting any space. However, this
logic breaks down if slab is backed by vmalloc() which operates
at a page level granularity. In this case, the optimal thing to
do is calculate the minimum required slab size given certain
constraints (object size, alignment, objects/slab, etc).
Therefore, this patch reworks the spl_slab_size() function so
that it sizes KMC_KMEM slabs differently than KMC_VMEM slabs.
KMC_KMEM slabs are rounded up to the nearest power of two, and
KMC_VMEM slabs are allowed to be the minimum required size.
This change also reduces the default number of objects per slab.
This reduces how much memory a single cache object can pin, which
can result in significant memory saving for highly fragmented
caches. But depending on the workload it may result in slabs
being allocated and freed more frequently. In practice, this
has been shown to be a better default for most workloads.
Also the maximum slab size has been reduced to 4MB on 32-bit
systems. Due to the limited virtual address space it's critical
the we be as frugal as possible. A limit of 4M still lets us
reasonably comfortably allocate a limited number of 1MB objects.
Finally, the kmem:slab_small and kmem:slab_large SPLAT tests
were extended to provide better test coverage of various object
sizes and alignments. Caches are created with random parameters
and their basic functionality is verified by allocating several
slabs worth of objects.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-16 01:06:18 +03:00
|
|
|
unsigned int spl_kmem_cache_max_size = SPL_KMEM_CACHE_MAX_SIZE;
|
2014-12-08 21:04:42 +03:00
|
|
|
module_param(spl_kmem_cache_max_size, uint, 0644);
|
|
|
|
MODULE_PARM_DESC(spl_kmem_cache_max_size, "Maximum size of slab in MB");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For small objects the Linux slab allocator should be used to make the most
|
|
|
|
* efficient use of the memory. However, large objects are not supported by
|
|
|
|
* the Linux slab and therefore the SPL implementation is preferred. A cutoff
|
|
|
|
* of 16K was determined to be optimal for architectures using 4K pages.
|
|
|
|
*/
|
|
|
|
#if PAGE_SIZE == 4096
|
|
|
|
unsigned int spl_kmem_cache_slab_limit = 16384;
|
|
|
|
#else
|
|
|
|
unsigned int spl_kmem_cache_slab_limit = 0;
|
|
|
|
#endif
|
|
|
|
module_param(spl_kmem_cache_slab_limit, uint, 0644);
|
|
|
|
MODULE_PARM_DESC(spl_kmem_cache_slab_limit,
|
2014-12-08 21:35:51 +03:00
|
|
|
"Objects less than N bytes use the Linux slab");
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2015-01-10 01:00:34 +03:00
|
|
|
/*
|
|
|
|
* The number of threads available to allocate new slabs for caches. This
|
|
|
|
* should not need to be tuned but it is available for performance analysis.
|
|
|
|
*/
|
|
|
|
unsigned int spl_kmem_cache_kmem_threads = 4;
|
|
|
|
module_param(spl_kmem_cache_kmem_threads, uint, 0444);
|
|
|
|
MODULE_PARM_DESC(spl_kmem_cache_kmem_threads,
|
|
|
|
"Number of spl_kmem_cache threads");
|
2018-02-24 21:05:37 +03:00
|
|
|
/* END CSTYLED */
|
2015-01-10 01:00:34 +03:00
|
|
|
|
2014-12-08 21:04:42 +03:00
|
|
|
/*
|
|
|
|
* Slab allocation interfaces
|
|
|
|
*
|
|
|
|
* While the Linux slab implementation was inspired by the Solaris
|
|
|
|
* implementation I cannot use it to emulate the Solaris APIs. I
|
|
|
|
* require two features which are not provided by the Linux slab.
|
|
|
|
*
|
|
|
|
* 1) Constructors AND destructors. Recent versions of the Linux
|
|
|
|
* kernel have removed support for destructors. This is a deal
|
|
|
|
* breaker for the SPL which contains particularly expensive
|
|
|
|
* initializers for mutex's, condition variables, etc. We also
|
|
|
|
* require a minimal level of cleanup for these data types unlike
|
2014-12-08 21:35:51 +03:00
|
|
|
* many Linux data types which do need to be explicitly destroyed.
|
2014-12-08 21:04:42 +03:00
|
|
|
*
|
|
|
|
* 2) Virtual address space backed slab. Callers of the Solaris slab
|
|
|
|
* expect it to work well for both small are very large allocations.
|
|
|
|
* Because of memory fragmentation the Linux slab which is backed
|
|
|
|
* by kmalloc'ed memory performs very badly when confronted with
|
|
|
|
* large numbers of large allocations. Basing the slab on the
|
|
|
|
* virtual address space removes the need for contiguous pages
|
|
|
|
* and greatly improve performance for large allocations.
|
|
|
|
*
|
|
|
|
* For these reasons, the SPL has its own slab implementation with
|
|
|
|
* the needed features. It is not as highly optimized as either the
|
|
|
|
* Solaris or Linux slabs, but it should get me most of what is
|
|
|
|
* needed until it can be optimized or obsoleted by another approach.
|
|
|
|
*
|
|
|
|
* One serious concern I do have about this method is the relatively
|
|
|
|
* small virtual address space on 32bit arches. This will seriously
|
|
|
|
* constrain the size of the slab caches and their performance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct list_head spl_kmem_cache_list; /* List of caches */
|
|
|
|
struct rw_semaphore spl_kmem_cache_sem; /* Cache list lock */
|
2019-08-31 00:32:18 +03:00
|
|
|
taskq_t *spl_kmem_cache_taskq; /* Task queue for aging / reclaim */
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
static void spl_cache_shrink(spl_kmem_cache_t *skc, void *obj);
|
|
|
|
|
|
|
|
static void *
|
|
|
|
kv_alloc(spl_kmem_cache_t *skc, int size, int flags)
|
|
|
|
{
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
gfp_t lflags = kmem_flags_convert(flags);
|
2014-12-08 21:04:42 +03:00
|
|
|
void *ptr;
|
|
|
|
|
2020-08-18 02:04:28 +03:00
|
|
|
ptr = spl_vmalloc(size, lflags | __GFP_HIGHMEM);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
/* Resulting allocated memory will be page aligned */
|
|
|
|
ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
return (ptr);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kv_free(spl_kmem_cache_t *skc, void *ptr, int size)
|
|
|
|
{
|
|
|
|
ASSERT(IS_P2ALIGNED(ptr, PAGE_SIZE));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Linux direct reclaim path uses this out of band value to
|
|
|
|
* determine if forward progress is being made. Normally this is
|
|
|
|
* incremented by kmem_freepages() which is part of the various
|
|
|
|
* Linux slab implementations. However, since we are using none
|
|
|
|
* of that infrastructure we are responsible for incrementing it.
|
|
|
|
*/
|
|
|
|
if (current->reclaim_state)
|
|
|
|
current->reclaim_state->reclaimed_slab += size >> PAGE_SHIFT;
|
|
|
|
|
2020-08-18 02:04:28 +03:00
|
|
|
vfree(ptr);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Required space for each aligned sks.
|
|
|
|
*/
|
|
|
|
static inline uint32_t
|
|
|
|
spl_sks_size(spl_kmem_cache_t *skc)
|
|
|
|
{
|
2014-12-08 21:35:51 +03:00
|
|
|
return (P2ROUNDUP_TYPED(sizeof (spl_kmem_slab_t),
|
|
|
|
skc->skc_obj_align, uint32_t));
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Required space for each aligned object.
|
|
|
|
*/
|
|
|
|
static inline uint32_t
|
|
|
|
spl_obj_size(spl_kmem_cache_t *skc)
|
|
|
|
{
|
|
|
|
uint32_t align = skc->skc_obj_align;
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
return (P2ROUNDUP_TYPED(skc->skc_obj_size, align, uint32_t) +
|
|
|
|
P2ROUNDUP_TYPED(sizeof (spl_kmem_obj_t), align, uint32_t));
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
2019-10-11 01:45:52 +03:00
|
|
|
uint64_t
|
|
|
|
spl_kmem_cache_inuse(kmem_cache_t *cache)
|
|
|
|
{
|
|
|
|
return (cache->skc_obj_total);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_cache_inuse);
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
spl_kmem_cache_entry_size(kmem_cache_t *cache)
|
|
|
|
{
|
|
|
|
return (cache->skc_obj_size);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_cache_entry_size);
|
|
|
|
|
2014-12-08 21:04:42 +03:00
|
|
|
/*
|
|
|
|
* Lookup the spl_kmem_object_t for an object given that object.
|
|
|
|
*/
|
|
|
|
static inline spl_kmem_obj_t *
|
|
|
|
spl_sko_from_obj(spl_kmem_cache_t *skc, void *obj)
|
|
|
|
{
|
2014-12-08 21:35:51 +03:00
|
|
|
return (obj + P2ROUNDUP_TYPED(skc->skc_obj_size,
|
|
|
|
skc->skc_obj_align, uint32_t));
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's important that we pack the spl_kmem_obj_t structure and the
|
|
|
|
* actual objects in to one large address space to minimize the number
|
|
|
|
* of calls to the allocator. It is far better to do a few large
|
|
|
|
* allocations and then subdivide it ourselves. Now which allocator
|
|
|
|
* we use requires balancing a few trade offs.
|
|
|
|
*
|
|
|
|
* For small objects we use kmem_alloc() because as long as you are
|
|
|
|
* only requesting a small number of pages (ideally just one) its cheap.
|
|
|
|
* However, when you start requesting multiple pages with kmem_alloc()
|
|
|
|
* it gets increasingly expensive since it requires contiguous pages.
|
|
|
|
* For this reason we shift to vmem_alloc() for slabs of large objects
|
|
|
|
* which removes the need for contiguous pages. We do not use
|
|
|
|
* vmem_alloc() in all cases because there is significant locking
|
|
|
|
* overhead in __get_vm_area_node(). This function takes a single
|
|
|
|
* global lock when acquiring an available virtual address range which
|
|
|
|
* serializes all vmem_alloc()'s for all slab caches. Using slightly
|
|
|
|
* different allocation functions for small and large objects should
|
|
|
|
* give us the best of both worlds.
|
|
|
|
*
|
2020-07-30 08:03:23 +03:00
|
|
|
* +------------------------+
|
|
|
|
* | spl_kmem_slab_t --+-+ |
|
|
|
|
* | skc_obj_size <-+ | |
|
|
|
|
* | spl_kmem_obj_t | |
|
|
|
|
* | skc_obj_size <---+ |
|
|
|
|
* | spl_kmem_obj_t | |
|
|
|
|
* | ... v |
|
|
|
|
* +------------------------+
|
2014-12-08 21:04:42 +03:00
|
|
|
*/
|
|
|
|
static spl_kmem_slab_t *
|
|
|
|
spl_slab_alloc(spl_kmem_cache_t *skc, int flags)
|
|
|
|
{
|
|
|
|
spl_kmem_slab_t *sks;
|
2020-07-30 08:03:23 +03:00
|
|
|
void *base;
|
|
|
|
uint32_t obj_size;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
base = kv_alloc(skc, skc->skc_slab_size, flags);
|
|
|
|
if (base == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
sks = (spl_kmem_slab_t *)base;
|
|
|
|
sks->sks_magic = SKS_MAGIC;
|
|
|
|
sks->sks_objs = skc->skc_slab_objs;
|
|
|
|
sks->sks_age = jiffies;
|
|
|
|
sks->sks_cache = skc;
|
|
|
|
INIT_LIST_HEAD(&sks->sks_list);
|
|
|
|
INIT_LIST_HEAD(&sks->sks_free_list);
|
|
|
|
sks->sks_ref = 0;
|
|
|
|
obj_size = spl_obj_size(skc);
|
|
|
|
|
2020-07-30 08:03:23 +03:00
|
|
|
for (int i = 0; i < sks->sks_objs; i++) {
|
|
|
|
void *obj = base + spl_sks_size(skc) + (i * obj_size);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
|
2020-07-30 08:03:23 +03:00
|
|
|
spl_kmem_obj_t *sko = spl_sko_from_obj(skc, obj);
|
2014-12-08 21:04:42 +03:00
|
|
|
sko->sko_addr = obj;
|
|
|
|
sko->sko_magic = SKO_MAGIC;
|
|
|
|
sko->sko_slab = sks;
|
|
|
|
INIT_LIST_HEAD(&sko->sko_list);
|
|
|
|
list_add_tail(&sko->sko_list, &sks->sks_free_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (sks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a slab from complete or partial list, it must be called with
|
|
|
|
* the 'skc->skc_lock' held but the actual free must be performed
|
|
|
|
* outside the lock to prevent deadlocking on vmem addresses.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
spl_slab_free(spl_kmem_slab_t *sks,
|
2014-12-08 21:35:51 +03:00
|
|
|
struct list_head *sks_list, struct list_head *sko_list)
|
2014-12-08 21:04:42 +03:00
|
|
|
{
|
|
|
|
spl_kmem_cache_t *skc;
|
|
|
|
|
|
|
|
ASSERT(sks->sks_magic == SKS_MAGIC);
|
|
|
|
ASSERT(sks->sks_ref == 0);
|
|
|
|
|
|
|
|
skc = sks->sks_cache;
|
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update slab/objects counters in the cache, then remove the
|
|
|
|
* slab from the skc->skc_partial_list. Finally add the slab
|
|
|
|
* and all its objects in to the private work lists where the
|
|
|
|
* destructors will be called and the memory freed to the system.
|
|
|
|
*/
|
|
|
|
skc->skc_obj_total -= sks->sks_objs;
|
|
|
|
skc->skc_slab_total--;
|
|
|
|
list_del(&sks->sks_list);
|
|
|
|
list_add(&sks->sks_list, sks_list);
|
|
|
|
list_splice_init(&sks->sks_free_list, sko_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-12-06 01:11:18 +03:00
|
|
|
* Reclaim empty slabs at the end of the partial list.
|
2014-12-08 21:04:42 +03:00
|
|
|
*/
|
|
|
|
static void
|
2014-12-06 01:11:18 +03:00
|
|
|
spl_slab_reclaim(spl_kmem_cache_t *skc)
|
2014-12-08 21:04:42 +03:00
|
|
|
{
|
2019-12-14 03:07:48 +03:00
|
|
|
spl_kmem_slab_t *sks = NULL, *m = NULL;
|
|
|
|
spl_kmem_obj_t *sko = NULL, *n = NULL;
|
2014-12-08 21:04:42 +03:00
|
|
|
LIST_HEAD(sks_list);
|
|
|
|
LIST_HEAD(sko_list);
|
|
|
|
|
|
|
|
/*
|
2014-12-06 01:11:18 +03:00
|
|
|
* Empty slabs and objects must be moved to a private list so they
|
|
|
|
* can be safely freed outside the spin lock. All empty slabs are
|
|
|
|
* at the end of skc->skc_partial_list, therefore once a non-empty
|
|
|
|
* slab is found we can stop scanning.
|
2014-12-08 21:04:42 +03:00
|
|
|
*/
|
|
|
|
spin_lock(&skc->skc_lock);
|
2014-12-08 21:35:51 +03:00
|
|
|
list_for_each_entry_safe_reverse(sks, m,
|
|
|
|
&skc->skc_partial_list, sks_list) {
|
2014-12-06 01:11:18 +03:00
|
|
|
|
|
|
|
if (sks->sks_ref > 0)
|
2014-12-08 21:04:42 +03:00
|
|
|
break;
|
|
|
|
|
2014-12-06 01:11:18 +03:00
|
|
|
spl_slab_free(sks, &sks_list, &sko_list);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
|
|
|
|
/*
|
2020-07-30 08:03:23 +03:00
|
|
|
* The following two loops ensure all the object destructors are run,
|
|
|
|
* and the slabs themselves are freed. This is all done outside the
|
|
|
|
* skc->skc_lock since this allows the destructor to sleep, and
|
|
|
|
* allows us to perform a conditional reschedule when a freeing a
|
|
|
|
* large number of objects and slabs back to the system.
|
2014-12-08 21:04:42 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
list_for_each_entry_safe(sko, n, &sko_list, sko_list) {
|
|
|
|
ASSERT(sko->sko_magic == SKO_MAGIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry_safe(sks, m, &sks_list, sks_list) {
|
|
|
|
ASSERT(sks->sks_magic == SKS_MAGIC);
|
|
|
|
kv_free(skc, sks, skc->skc_slab_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static spl_kmem_emergency_t *
|
|
|
|
spl_emergency_search(struct rb_root *root, void *obj)
|
|
|
|
{
|
|
|
|
struct rb_node *node = root->rb_node;
|
|
|
|
spl_kmem_emergency_t *ske;
|
|
|
|
unsigned long address = (unsigned long)obj;
|
|
|
|
|
|
|
|
while (node) {
|
|
|
|
ske = container_of(node, spl_kmem_emergency_t, ske_node);
|
|
|
|
|
2015-01-16 02:11:45 +03:00
|
|
|
if (address < ske->ske_obj)
|
2014-12-08 21:04:42 +03:00
|
|
|
node = node->rb_left;
|
2015-01-16 02:11:45 +03:00
|
|
|
else if (address > ske->ske_obj)
|
2014-12-08 21:04:42 +03:00
|
|
|
node = node->rb_right;
|
|
|
|
else
|
2014-12-08 21:35:51 +03:00
|
|
|
return (ske);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
return (NULL);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
spl_emergency_insert(struct rb_root *root, spl_kmem_emergency_t *ske)
|
|
|
|
{
|
|
|
|
struct rb_node **new = &(root->rb_node), *parent = NULL;
|
|
|
|
spl_kmem_emergency_t *ske_tmp;
|
2015-01-16 02:11:45 +03:00
|
|
|
unsigned long address = ske->ske_obj;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
while (*new) {
|
|
|
|
ske_tmp = container_of(*new, spl_kmem_emergency_t, ske_node);
|
|
|
|
|
|
|
|
parent = *new;
|
2015-01-16 02:11:45 +03:00
|
|
|
if (address < ske_tmp->ske_obj)
|
2014-12-08 21:04:42 +03:00
|
|
|
new = &((*new)->rb_left);
|
2015-01-16 02:11:45 +03:00
|
|
|
else if (address > ske_tmp->ske_obj)
|
2014-12-08 21:04:42 +03:00
|
|
|
new = &((*new)->rb_right);
|
|
|
|
else
|
2014-12-08 21:35:51 +03:00
|
|
|
return (0);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_link_node(&ske->ske_node, parent, new);
|
|
|
|
rb_insert_color(&ske->ske_node, root);
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
return (1);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a single emergency object and track it in a red black tree.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spl_emergency_alloc(spl_kmem_cache_t *skc, int flags, void **obj)
|
|
|
|
{
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
gfp_t lflags = kmem_flags_convert(flags);
|
2014-12-08 21:04:42 +03:00
|
|
|
spl_kmem_emergency_t *ske;
|
2015-01-16 02:11:45 +03:00
|
|
|
int order = get_order(skc->skc_obj_size);
|
2014-12-08 21:04:42 +03:00
|
|
|
int empty;
|
|
|
|
|
|
|
|
/* Last chance use a partial slab if one now exists */
|
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
empty = list_empty(&skc->skc_partial_list);
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
if (!empty)
|
|
|
|
return (-EEXIST);
|
|
|
|
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
ske = kmalloc(sizeof (*ske), lflags);
|
2014-12-08 21:04:42 +03:00
|
|
|
if (ske == NULL)
|
|
|
|
return (-ENOMEM);
|
|
|
|
|
2015-01-16 02:11:45 +03:00
|
|
|
ske->ske_obj = __get_free_pages(lflags, order);
|
|
|
|
if (ske->ske_obj == 0) {
|
2014-12-08 21:04:42 +03:00
|
|
|
kfree(ske);
|
|
|
|
return (-ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
empty = spl_emergency_insert(&skc->skc_emergency_tree, ske);
|
|
|
|
if (likely(empty)) {
|
|
|
|
skc->skc_obj_total++;
|
|
|
|
skc->skc_obj_emergency++;
|
|
|
|
if (skc->skc_obj_emergency > skc->skc_obj_emergency_max)
|
|
|
|
skc->skc_obj_emergency_max = skc->skc_obj_emergency;
|
|
|
|
}
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
|
|
|
|
if (unlikely(!empty)) {
|
2015-01-16 02:11:45 +03:00
|
|
|
free_pages(ske->ske_obj, order);
|
2014-12-08 21:04:42 +03:00
|
|
|
kfree(ske);
|
|
|
|
return (-EINVAL);
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:11:45 +03:00
|
|
|
*obj = (void *)ske->ske_obj;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Locate the passed object in the red black tree and free it.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spl_emergency_free(spl_kmem_cache_t *skc, void *obj)
|
|
|
|
{
|
|
|
|
spl_kmem_emergency_t *ske;
|
2015-01-16 02:11:45 +03:00
|
|
|
int order = get_order(skc->skc_obj_size);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
ske = spl_emergency_search(&skc->skc_emergency_tree, obj);
|
2015-01-10 01:00:34 +03:00
|
|
|
if (ske) {
|
2014-12-08 21:04:42 +03:00
|
|
|
rb_erase(&ske->ske_node, &skc->skc_emergency_tree);
|
|
|
|
skc->skc_obj_emergency--;
|
|
|
|
skc->skc_obj_total--;
|
|
|
|
}
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
|
2015-01-10 01:00:34 +03:00
|
|
|
if (ske == NULL)
|
2014-12-08 21:04:42 +03:00
|
|
|
return (-ENOENT);
|
|
|
|
|
2015-01-16 02:11:45 +03:00
|
|
|
free_pages(ske->ske_obj, order);
|
2014-12-08 21:04:42 +03:00
|
|
|
kfree(ske);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release objects from the per-cpu magazine back to their slab. The flush
|
|
|
|
* argument contains the max number of entries to remove from the magazine.
|
|
|
|
*/
|
|
|
|
static void
|
2020-07-24 19:39:26 +03:00
|
|
|
spl_cache_flush(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flush)
|
2014-12-08 21:04:42 +03:00
|
|
|
{
|
2020-07-24 19:39:26 +03:00
|
|
|
spin_lock(&skc->skc_lock);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
|
|
|
ASSERT(skm->skm_magic == SKM_MAGIC);
|
|
|
|
|
2020-07-24 19:39:26 +03:00
|
|
|
int count = MIN(flush, skm->skm_avail);
|
|
|
|
for (int i = 0; i < count; i++)
|
2014-12-08 21:04:42 +03:00
|
|
|
spl_cache_shrink(skc, skm->skm_objs[i]);
|
|
|
|
|
|
|
|
skm->skm_avail -= count;
|
|
|
|
memmove(skm->skm_objs, &(skm->skm_objs[count]),
|
2014-12-08 21:35:51 +03:00
|
|
|
sizeof (void *) * skm->skm_avail);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Size a slab based on the size of each aligned object plus spl_kmem_obj_t.
|
|
|
|
* When on-slab we want to target spl_kmem_cache_obj_per_slab. However,
|
|
|
|
* for very small objects we may end up with more than this so as not
|
|
|
|
* to waste space in the minimal allocation of a single page. Also for
|
|
|
|
* very large objects we may use as few as spl_kmem_cache_obj_per_slab_min,
|
|
|
|
* lower than this and we will fail.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spl_slab_size(spl_kmem_cache_t *skc, uint32_t *objs, uint32_t *size)
|
|
|
|
{
|
Refine slab cache sizing
This change is designed to improve the memory utilization of
slabs by more carefully setting their size. The way the code
currently works is problematic for slabs which contain large
objects (>1MB). This is due to slabs being unconditionally
rounded up to a power of two which may result in unused space
at the end of the slab.
The reason the existing code rounds up every slab is because it
assumes it will backed by the buddy allocator. Since the buddy
allocator can only performs power of two allocations this is
desirable because it avoids wasting any space. However, this
logic breaks down if slab is backed by vmalloc() which operates
at a page level granularity. In this case, the optimal thing to
do is calculate the minimum required slab size given certain
constraints (object size, alignment, objects/slab, etc).
Therefore, this patch reworks the spl_slab_size() function so
that it sizes KMC_KMEM slabs differently than KMC_VMEM slabs.
KMC_KMEM slabs are rounded up to the nearest power of two, and
KMC_VMEM slabs are allowed to be the minimum required size.
This change also reduces the default number of objects per slab.
This reduces how much memory a single cache object can pin, which
can result in significant memory saving for highly fragmented
caches. But depending on the workload it may result in slabs
being allocated and freed more frequently. In practice, this
has been shown to be a better default for most workloads.
Also the maximum slab size has been reduced to 4MB on 32-bit
systems. Due to the limited virtual address space it's critical
the we be as frugal as possible. A limit of 4M still lets us
reasonably comfortably allocate a limited number of 1MB objects.
Finally, the kmem:slab_small and kmem:slab_large SPLAT tests
were extended to provide better test coverage of various object
sizes and alignments. Caches are created with random parameters
and their basic functionality is verified by allocating several
slabs worth of objects.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-16 01:06:18 +03:00
|
|
|
uint32_t sks_size, obj_size, max_size, tgt_size, tgt_objs;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2020-07-30 08:03:23 +03:00
|
|
|
sks_size = spl_sks_size(skc);
|
|
|
|
obj_size = spl_obj_size(skc);
|
|
|
|
max_size = (spl_kmem_cache_max_size * 1024 * 1024);
|
|
|
|
tgt_size = (spl_kmem_cache_obj_per_slab * obj_size + sks_size);
|
Refine slab cache sizing
This change is designed to improve the memory utilization of
slabs by more carefully setting their size. The way the code
currently works is problematic for slabs which contain large
objects (>1MB). This is due to slabs being unconditionally
rounded up to a power of two which may result in unused space
at the end of the slab.
The reason the existing code rounds up every slab is because it
assumes it will backed by the buddy allocator. Since the buddy
allocator can only performs power of two allocations this is
desirable because it avoids wasting any space. However, this
logic breaks down if slab is backed by vmalloc() which operates
at a page level granularity. In this case, the optimal thing to
do is calculate the minimum required slab size given certain
constraints (object size, alignment, objects/slab, etc).
Therefore, this patch reworks the spl_slab_size() function so
that it sizes KMC_KMEM slabs differently than KMC_VMEM slabs.
KMC_KMEM slabs are rounded up to the nearest power of two, and
KMC_VMEM slabs are allowed to be the minimum required size.
This change also reduces the default number of objects per slab.
This reduces how much memory a single cache object can pin, which
can result in significant memory saving for highly fragmented
caches. But depending on the workload it may result in slabs
being allocated and freed more frequently. In practice, this
has been shown to be a better default for most workloads.
Also the maximum slab size has been reduced to 4MB on 32-bit
systems. Due to the limited virtual address space it's critical
the we be as frugal as possible. A limit of 4M still lets us
reasonably comfortably allocate a limited number of 1MB objects.
Finally, the kmem:slab_small and kmem:slab_large SPLAT tests
were extended to provide better test coverage of various object
sizes and alignments. Caches are created with random parameters
and their basic functionality is verified by allocating several
slabs worth of objects.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-16 01:06:18 +03:00
|
|
|
|
2020-07-30 08:03:23 +03:00
|
|
|
if (tgt_size <= max_size) {
|
|
|
|
tgt_objs = (tgt_size - sks_size) / obj_size;
|
|
|
|
} else {
|
|
|
|
tgt_objs = (max_size - sks_size) / obj_size;
|
|
|
|
tgt_size = (tgt_objs * obj_size) + sks_size;
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
Refine slab cache sizing
This change is designed to improve the memory utilization of
slabs by more carefully setting their size. The way the code
currently works is problematic for slabs which contain large
objects (>1MB). This is due to slabs being unconditionally
rounded up to a power of two which may result in unused space
at the end of the slab.
The reason the existing code rounds up every slab is because it
assumes it will backed by the buddy allocator. Since the buddy
allocator can only performs power of two allocations this is
desirable because it avoids wasting any space. However, this
logic breaks down if slab is backed by vmalloc() which operates
at a page level granularity. In this case, the optimal thing to
do is calculate the minimum required slab size given certain
constraints (object size, alignment, objects/slab, etc).
Therefore, this patch reworks the spl_slab_size() function so
that it sizes KMC_KMEM slabs differently than KMC_VMEM slabs.
KMC_KMEM slabs are rounded up to the nearest power of two, and
KMC_VMEM slabs are allowed to be the minimum required size.
This change also reduces the default number of objects per slab.
This reduces how much memory a single cache object can pin, which
can result in significant memory saving for highly fragmented
caches. But depending on the workload it may result in slabs
being allocated and freed more frequently. In practice, this
has been shown to be a better default for most workloads.
Also the maximum slab size has been reduced to 4MB on 32-bit
systems. Due to the limited virtual address space it's critical
the we be as frugal as possible. A limit of 4M still lets us
reasonably comfortably allocate a limited number of 1MB objects.
Finally, the kmem:slab_small and kmem:slab_large SPLAT tests
were extended to provide better test coverage of various object
sizes and alignments. Caches are created with random parameters
and their basic functionality is verified by allocating several
slabs worth of objects.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-16 01:06:18 +03:00
|
|
|
if (tgt_objs == 0)
|
|
|
|
return (-ENOSPC);
|
|
|
|
|
|
|
|
*objs = tgt_objs;
|
|
|
|
*size = tgt_size;
|
|
|
|
|
|
|
|
return (0);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a guess at reasonable per-cpu magazine size based on the size of
|
|
|
|
* each object and the cost of caching N of them in each magazine. Long
|
|
|
|
* term this should really adapt based on an observed usage heuristic.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spl_magazine_size(spl_kmem_cache_t *skc)
|
|
|
|
{
|
|
|
|
uint32_t obj_size = spl_obj_size(skc);
|
|
|
|
int size;
|
|
|
|
|
2014-12-06 01:11:18 +03:00
|
|
|
if (spl_kmem_cache_magazine_size > 0)
|
|
|
|
return (MAX(MIN(spl_kmem_cache_magazine_size, 256), 2));
|
|
|
|
|
2014-12-08 21:04:42 +03:00
|
|
|
/* Per-magazine sizes below assume a 4Kib page size */
|
|
|
|
if (obj_size > (PAGE_SIZE * 256))
|
|
|
|
size = 4; /* Minimum 4Mib per-magazine */
|
|
|
|
else if (obj_size > (PAGE_SIZE * 32))
|
|
|
|
size = 16; /* Minimum 2Mib per-magazine */
|
|
|
|
else if (obj_size > (PAGE_SIZE))
|
|
|
|
size = 64; /* Minimum 256Kib per-magazine */
|
|
|
|
else if (obj_size > (PAGE_SIZE / 4))
|
|
|
|
size = 128; /* Minimum 128Kib per-magazine */
|
|
|
|
else
|
|
|
|
size = 256;
|
|
|
|
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a per-cpu magazine to associate with a specific core.
|
|
|
|
*/
|
|
|
|
static spl_kmem_magazine_t *
|
|
|
|
spl_magazine_alloc(spl_kmem_cache_t *skc, int cpu)
|
|
|
|
{
|
|
|
|
spl_kmem_magazine_t *skm;
|
2014-12-08 21:35:51 +03:00
|
|
|
int size = sizeof (spl_kmem_magazine_t) +
|
|
|
|
sizeof (void *) * skc->skc_mag_size;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
skm = kmalloc_node(size, GFP_KERNEL, cpu_to_node(cpu));
|
2014-12-08 21:04:42 +03:00
|
|
|
if (skm) {
|
|
|
|
skm->skm_magic = SKM_MAGIC;
|
|
|
|
skm->skm_avail = 0;
|
|
|
|
skm->skm_size = skc->skc_mag_size;
|
|
|
|
skm->skm_refill = skc->skc_mag_refill;
|
|
|
|
skm->skm_cache = skc;
|
|
|
|
skm->skm_cpu = cpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (skm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a per-cpu magazine associated with a specific core.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
spl_magazine_free(spl_kmem_magazine_t *skm)
|
|
|
|
{
|
|
|
|
ASSERT(skm->skm_magic == SKM_MAGIC);
|
|
|
|
ASSERT(skm->skm_avail == 0);
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
kfree(skm);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create all pre-cpu magazines of reasonable sizes.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spl_magazine_create(spl_kmem_cache_t *skc)
|
|
|
|
{
|
2019-12-14 03:07:48 +03:00
|
|
|
int i = 0;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2020-07-30 23:56:00 +03:00
|
|
|
ASSERT((skc->skc_flags & KMC_SLAB) == 0);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2015-10-12 22:31:05 +03:00
|
|
|
skc->skc_mag = kzalloc(sizeof (spl_kmem_magazine_t *) *
|
|
|
|
num_possible_cpus(), kmem_flags_convert(KM_SLEEP));
|
2014-12-08 21:04:42 +03:00
|
|
|
skc->skc_mag_size = spl_magazine_size(skc);
|
|
|
|
skc->skc_mag_refill = (skc->skc_mag_size + 1) / 2;
|
|
|
|
|
2015-10-12 22:31:05 +03:00
|
|
|
for_each_possible_cpu(i) {
|
2014-12-08 21:04:42 +03:00
|
|
|
skc->skc_mag[i] = spl_magazine_alloc(skc, i);
|
|
|
|
if (!skc->skc_mag[i]) {
|
|
|
|
for (i--; i >= 0; i--)
|
|
|
|
spl_magazine_free(skc->skc_mag[i]);
|
|
|
|
|
2015-10-12 22:31:05 +03:00
|
|
|
kfree(skc->skc_mag);
|
2014-12-08 21:04:42 +03:00
|
|
|
return (-ENOMEM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy all pre-cpu magazines.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
spl_magazine_destroy(spl_kmem_cache_t *skc)
|
|
|
|
{
|
|
|
|
spl_kmem_magazine_t *skm;
|
2019-12-14 03:07:48 +03:00
|
|
|
int i = 0;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2020-07-30 23:56:00 +03:00
|
|
|
ASSERT((skc->skc_flags & KMC_SLAB) == 0);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2015-10-12 22:31:05 +03:00
|
|
|
for_each_possible_cpu(i) {
|
2014-12-08 21:04:42 +03:00
|
|
|
skm = skc->skc_mag[i];
|
|
|
|
spl_cache_flush(skc, skm, skm->skm_avail);
|
|
|
|
spl_magazine_free(skm);
|
2014-12-08 21:35:51 +03:00
|
|
|
}
|
2015-10-12 22:31:05 +03:00
|
|
|
|
|
|
|
kfree(skc->skc_mag);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a object cache based on the following arguments:
|
|
|
|
* name cache name
|
|
|
|
* size cache object size
|
|
|
|
* align cache object alignment
|
|
|
|
* ctor cache object constructor
|
|
|
|
* dtor cache object destructor
|
|
|
|
* reclaim cache object reclaim
|
|
|
|
* priv cache private data for ctor/dtor/reclaim
|
|
|
|
* vmp unused must be NULL
|
|
|
|
* flags
|
2020-07-30 08:03:23 +03:00
|
|
|
* KMC_KVMEM Force kvmem backed SPL cache
|
2014-12-08 21:04:42 +03:00
|
|
|
* KMC_SLAB Force Linux slab backed cache
|
2019-07-21 20:34:02 +03:00
|
|
|
* KMC_NODEBUG Disable debugging (unsupported)
|
2014-12-08 21:04:42 +03:00
|
|
|
*/
|
|
|
|
spl_kmem_cache_t *
|
|
|
|
spl_kmem_cache_create(char *name, size_t size, size_t align,
|
Remove skc_reclaim, hdr_recl, kmem_cache shrinker
The SPL kmem_cache implementation provides a mechanism, `skc_reclaim`,
whereby individual caches can register a callback to be invoked when
there is memory pressure. This mechanism is used in only one place: the
ARC registers the `hdr_recl()` reclaim function. This function wakes up
the `arc_reap_zthr`, whose job is to call `kmem_cache_reap()` and
`arc_reduce_target_size()`.
The `skc_reclaim` callbacks are invoked only by shrinker callbacks and
`arc_reap_zthr`, and only callback only wakes up `arc_reap_zthr`. When
called from `arc_reap_zthr`, waking `arc_reap_zthr` is a no-op. When
called from shrinker callbacks, we are already aware of memory pressure
and responding to it. Therefore there is little benefit to ever calling
the `hdr_recl()` `skc_reclaim` callback.
The `arc_reap_zthr` also wakes once a second, and if memory is low when
allocating an ARC buffer. Therefore, additionally waking it from the
shrinker calbacks has little benefit.
The shrinker callbacks can be invoked very frequently, e.g. 10,000 times
per second. Additionally, for invocation of the shrinker callback,
skc_reclaim is invoked many times. Therefore, this mechanism consumes
significant amounts of CPU time.
The kmem_cache shrinker calls `spl_kmem_cache_reap_now()`, which,
in addition to invoking `skc_reclaim()`, does two things to attempt to
free pages for use by the system:
1. Return free objects from the magazine layer to the slab layer
2. Return entirely-free slabs to the page layer (i.e. free pages)
These actions apply only to caches implemented by the SPL, not those
that use the underlying kernel SLAB/SLUB caches. The SPL caches are
used for objects >=32KB, which are primarily linear ABD's cached in the
DBUF cache.
These actions (freeing objects from the magazine layer and returning
entirely-free slabs) are also taken whenever a `kmem_cache_free()` call
finds a full magazine. So there would typically be zero entirely-free
slabs, and the number of objects in magazines is limited (typically no
more than 64 objects per magazine, and there's one magazine per CPU).
Therefore the benefit of `spl_kmem_cache_reap_now()`, while nonzero, is
modest.
We also call `spl_kmem_cache_reap_now()` from the `arc_reap_zthr`, when
memory pressure is detected. Therefore, calling
`spl_kmem_cache_reap_now()` from the kmem_cache shrinker is not needed.
This commit removes the `skc_reclaim` mechanism, its only callback
`hdr_recl()`, and the kmem_cache shrinker callback.
Reviewed-By: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Reviewed-by: Pavel Zakharov <pavel.zakharov@delphix.com>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #10576
2020-07-19 19:58:30 +03:00
|
|
|
spl_kmem_ctor_t ctor, spl_kmem_dtor_t dtor, void *reclaim,
|
2014-12-08 21:35:51 +03:00
|
|
|
void *priv, void *vmp, int flags)
|
2014-12-08 21:04:42 +03:00
|
|
|
{
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
gfp_t lflags = kmem_flags_convert(KM_SLEEP);
|
2014-12-08 21:35:51 +03:00
|
|
|
spl_kmem_cache_t *skc;
|
2014-12-08 21:04:42 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unsupported flags
|
|
|
|
*/
|
|
|
|
ASSERT(vmp == NULL);
|
Remove skc_reclaim, hdr_recl, kmem_cache shrinker
The SPL kmem_cache implementation provides a mechanism, `skc_reclaim`,
whereby individual caches can register a callback to be invoked when
there is memory pressure. This mechanism is used in only one place: the
ARC registers the `hdr_recl()` reclaim function. This function wakes up
the `arc_reap_zthr`, whose job is to call `kmem_cache_reap()` and
`arc_reduce_target_size()`.
The `skc_reclaim` callbacks are invoked only by shrinker callbacks and
`arc_reap_zthr`, and only callback only wakes up `arc_reap_zthr`. When
called from `arc_reap_zthr`, waking `arc_reap_zthr` is a no-op. When
called from shrinker callbacks, we are already aware of memory pressure
and responding to it. Therefore there is little benefit to ever calling
the `hdr_recl()` `skc_reclaim` callback.
The `arc_reap_zthr` also wakes once a second, and if memory is low when
allocating an ARC buffer. Therefore, additionally waking it from the
shrinker calbacks has little benefit.
The shrinker callbacks can be invoked very frequently, e.g. 10,000 times
per second. Additionally, for invocation of the shrinker callback,
skc_reclaim is invoked many times. Therefore, this mechanism consumes
significant amounts of CPU time.
The kmem_cache shrinker calls `spl_kmem_cache_reap_now()`, which,
in addition to invoking `skc_reclaim()`, does two things to attempt to
free pages for use by the system:
1. Return free objects from the magazine layer to the slab layer
2. Return entirely-free slabs to the page layer (i.e. free pages)
These actions apply only to caches implemented by the SPL, not those
that use the underlying kernel SLAB/SLUB caches. The SPL caches are
used for objects >=32KB, which are primarily linear ABD's cached in the
DBUF cache.
These actions (freeing objects from the magazine layer and returning
entirely-free slabs) are also taken whenever a `kmem_cache_free()` call
finds a full magazine. So there would typically be zero entirely-free
slabs, and the number of objects in magazines is limited (typically no
more than 64 objects per magazine, and there's one magazine per CPU).
Therefore the benefit of `spl_kmem_cache_reap_now()`, while nonzero, is
modest.
We also call `spl_kmem_cache_reap_now()` from the `arc_reap_zthr`, when
memory pressure is detected. Therefore, calling
`spl_kmem_cache_reap_now()` from the kmem_cache shrinker is not needed.
This commit removes the `skc_reclaim` mechanism, its only callback
`hdr_recl()`, and the kmem_cache shrinker callback.
Reviewed-By: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Reviewed-by: Pavel Zakharov <pavel.zakharov@delphix.com>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #10576
2020-07-19 19:58:30 +03:00
|
|
|
ASSERT(reclaim == NULL);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
might_sleep();
|
|
|
|
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
skc = kzalloc(sizeof (*skc), lflags);
|
2014-12-08 21:04:42 +03:00
|
|
|
if (skc == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
skc->skc_magic = SKC_MAGIC;
|
|
|
|
skc->skc_name_size = strlen(name) + 1;
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
skc->skc_name = (char *)kmalloc(skc->skc_name_size, lflags);
|
2014-12-08 21:04:42 +03:00
|
|
|
if (skc->skc_name == NULL) {
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
kfree(skc);
|
2014-12-08 21:04:42 +03:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
strncpy(skc->skc_name, name, skc->skc_name_size);
|
|
|
|
|
|
|
|
skc->skc_ctor = ctor;
|
|
|
|
skc->skc_dtor = dtor;
|
|
|
|
skc->skc_private = priv;
|
|
|
|
skc->skc_vmp = vmp;
|
|
|
|
skc->skc_linux_cache = NULL;
|
|
|
|
skc->skc_flags = flags;
|
|
|
|
skc->skc_obj_size = size;
|
|
|
|
skc->skc_obj_align = SPL_KMEM_CACHE_ALIGN;
|
|
|
|
atomic_set(&skc->skc_ref, 0);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&skc->skc_list);
|
|
|
|
INIT_LIST_HEAD(&skc->skc_complete_list);
|
|
|
|
INIT_LIST_HEAD(&skc->skc_partial_list);
|
|
|
|
skc->skc_emergency_tree = RB_ROOT;
|
|
|
|
spin_lock_init(&skc->skc_lock);
|
|
|
|
init_waitqueue_head(&skc->skc_waitq);
|
|
|
|
skc->skc_slab_fail = 0;
|
|
|
|
skc->skc_slab_create = 0;
|
|
|
|
skc->skc_slab_destroy = 0;
|
|
|
|
skc->skc_slab_total = 0;
|
|
|
|
skc->skc_slab_alloc = 0;
|
|
|
|
skc->skc_slab_max = 0;
|
|
|
|
skc->skc_obj_total = 0;
|
|
|
|
skc->skc_obj_alloc = 0;
|
|
|
|
skc->skc_obj_max = 0;
|
|
|
|
skc->skc_obj_deadlock = 0;
|
|
|
|
skc->skc_obj_emergency = 0;
|
|
|
|
skc->skc_obj_emergency_max = 0;
|
|
|
|
|
2020-06-27 04:06:50 +03:00
|
|
|
rc = percpu_counter_init_common(&skc->skc_linux_alloc, 0,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (rc != 0) {
|
|
|
|
kfree(skc);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-08 21:04:42 +03:00
|
|
|
/*
|
|
|
|
* Verify the requested alignment restriction is sane.
|
|
|
|
*/
|
|
|
|
if (align) {
|
|
|
|
VERIFY(ISP2(align));
|
|
|
|
VERIFY3U(align, >=, SPL_KMEM_CACHE_ALIGN);
|
|
|
|
VERIFY3U(align, <=, PAGE_SIZE);
|
|
|
|
skc->skc_obj_align = align;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When no specific type of slab is requested (kmem, vmem, or
|
|
|
|
* linuxslab) then select a cache type based on the object size
|
|
|
|
* and default tunables.
|
|
|
|
*/
|
2020-08-18 02:04:28 +03:00
|
|
|
if (!(skc->skc_flags & (KMC_SLAB | KMC_KVMEM))) {
|
2014-12-08 21:04:42 +03:00
|
|
|
if (spl_kmem_cache_slab_limit &&
|
2019-03-01 04:57:47 +03:00
|
|
|
size <= (size_t)spl_kmem_cache_slab_limit) {
|
|
|
|
/*
|
|
|
|
* Objects smaller than spl_kmem_cache_slab_limit can
|
|
|
|
* use the Linux slab for better space-efficiency.
|
|
|
|
*/
|
2014-12-08 21:04:42 +03:00
|
|
|
skc->skc_flags |= KMC_SLAB;
|
2019-03-01 04:57:47 +03:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* All other objects are considered large and are
|
2019-07-21 20:34:10 +03:00
|
|
|
* placed on kvmem backed slabs.
|
2019-03-01 04:57:47 +03:00
|
|
|
*/
|
2019-07-21 20:34:10 +03:00
|
|
|
skc->skc_flags |= KMC_KVMEM;
|
2019-03-01 04:57:47 +03:00
|
|
|
}
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given the type of slab allocate the required resources.
|
|
|
|
*/
|
2020-08-18 02:04:28 +03:00
|
|
|
if (skc->skc_flags & KMC_KVMEM) {
|
2014-12-08 21:04:42 +03:00
|
|
|
rc = spl_slab_size(skc,
|
|
|
|
&skc->skc_slab_objs, &skc->skc_slab_size);
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
rc = spl_magazine_create(skc);
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
} else {
|
2015-09-28 19:08:11 +03:00
|
|
|
unsigned long slabflags = 0;
|
|
|
|
|
Refine slab cache sizing
This change is designed to improve the memory utilization of
slabs by more carefully setting their size. The way the code
currently works is problematic for slabs which contain large
objects (>1MB). This is due to slabs being unconditionally
rounded up to a power of two which may result in unused space
at the end of the slab.
The reason the existing code rounds up every slab is because it
assumes it will backed by the buddy allocator. Since the buddy
allocator can only performs power of two allocations this is
desirable because it avoids wasting any space. However, this
logic breaks down if slab is backed by vmalloc() which operates
at a page level granularity. In this case, the optimal thing to
do is calculate the minimum required slab size given certain
constraints (object size, alignment, objects/slab, etc).
Therefore, this patch reworks the spl_slab_size() function so
that it sizes KMC_KMEM slabs differently than KMC_VMEM slabs.
KMC_KMEM slabs are rounded up to the nearest power of two, and
KMC_VMEM slabs are allowed to be the minimum required size.
This change also reduces the default number of objects per slab.
This reduces how much memory a single cache object can pin, which
can result in significant memory saving for highly fragmented
caches. But depending on the workload it may result in slabs
being allocated and freed more frequently. In practice, this
has been shown to be a better default for most workloads.
Also the maximum slab size has been reduced to 4MB on 32-bit
systems. Due to the limited virtual address space it's critical
the we be as frugal as possible. A limit of 4M still lets us
reasonably comfortably allocate a limited number of 1MB objects.
Finally, the kmem:slab_small and kmem:slab_large SPLAT tests
were extended to provide better test coverage of various object
sizes and alignments. Caches are created with random parameters
and their basic functionality is verified by allocating several
slabs worth of objects.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-16 01:06:18 +03:00
|
|
|
if (size > (SPL_MAX_KMEM_ORDER_NR_PAGES * PAGE_SIZE)) {
|
|
|
|
rc = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:08:11 +03:00
|
|
|
#if defined(SLAB_USERCOPY)
|
|
|
|
/*
|
|
|
|
* Required for PAX-enabled kernels if the slab is to be
|
2019-08-31 00:32:18 +03:00
|
|
|
* used for copying between user and kernel space.
|
2015-09-28 19:08:11 +03:00
|
|
|
*/
|
|
|
|
slabflags |= SLAB_USERCOPY;
|
|
|
|
#endif
|
|
|
|
|
2017-01-17 23:05:14 +03:00
|
|
|
#if defined(HAVE_KMEM_CACHE_CREATE_USERCOPY)
|
2019-03-01 04:57:47 +03:00
|
|
|
/*
|
|
|
|
* Newer grsec patchset uses kmem_cache_create_usercopy()
|
|
|
|
* instead of SLAB_USERCOPY flag
|
|
|
|
*/
|
|
|
|
skc->skc_linux_cache = kmem_cache_create_usercopy(
|
|
|
|
skc->skc_name, size, align, slabflags, 0, size, NULL);
|
2017-01-17 23:05:14 +03:00
|
|
|
#else
|
2019-03-01 04:57:47 +03:00
|
|
|
skc->skc_linux_cache = kmem_cache_create(
|
|
|
|
skc->skc_name, size, align, slabflags, NULL);
|
2017-01-17 23:05:14 +03:00
|
|
|
#endif
|
2014-12-08 21:04:42 +03:00
|
|
|
if (skc->skc_linux_cache == NULL) {
|
|
|
|
rc = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
down_write(&spl_kmem_cache_sem);
|
|
|
|
list_add_tail(&skc->skc_list, &spl_kmem_cache_list);
|
|
|
|
up_write(&spl_kmem_cache_sem);
|
|
|
|
|
|
|
|
return (skc);
|
|
|
|
out:
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
kfree(skc->skc_name);
|
2020-06-27 04:06:50 +03:00
|
|
|
percpu_counter_destroy(&skc->skc_linux_alloc);
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
kfree(skc);
|
2014-12-08 21:04:42 +03:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_cache_create);
|
|
|
|
|
|
|
|
/*
|
2014-12-08 21:35:51 +03:00
|
|
|
* Register a move callback for cache defragmentation.
|
2014-12-08 21:04:42 +03:00
|
|
|
* XXX: Unimplemented but harmless to stub out for now.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
spl_kmem_cache_set_move(spl_kmem_cache_t *skc,
|
|
|
|
kmem_cbrc_t (move)(void *, void *, size_t, void *))
|
|
|
|
{
|
2014-12-08 21:35:51 +03:00
|
|
|
ASSERT(move != NULL);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_cache_set_move);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a cache and all objects associated with the cache.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
spl_kmem_cache_destroy(spl_kmem_cache_t *skc)
|
|
|
|
{
|
|
|
|
DECLARE_WAIT_QUEUE_HEAD(wq);
|
|
|
|
taskqid_t id;
|
|
|
|
|
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
2020-08-18 02:04:28 +03:00
|
|
|
ASSERT(skc->skc_flags & (KMC_KVMEM | KMC_SLAB));
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
down_write(&spl_kmem_cache_sem);
|
|
|
|
list_del_init(&skc->skc_list);
|
|
|
|
up_write(&spl_kmem_cache_sem);
|
|
|
|
|
|
|
|
/* Cancel any and wait for any pending delayed tasks */
|
|
|
|
VERIFY(!test_and_set_bit(KMC_BIT_DESTROY, &skc->skc_flags));
|
|
|
|
|
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
id = skc->skc_taskqid;
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
|
|
|
|
taskq_cancel_id(spl_kmem_cache_taskq, id);
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
/*
|
|
|
|
* Wait until all current callers complete, this is mainly
|
2014-12-08 21:04:42 +03:00
|
|
|
* to catch the case where a low memory situation triggers a
|
2014-12-08 21:35:51 +03:00
|
|
|
* cache reaping action which races with this destroy.
|
|
|
|
*/
|
2014-12-08 21:04:42 +03:00
|
|
|
wait_event(wq, atomic_read(&skc->skc_ref) == 0);
|
|
|
|
|
2020-08-18 02:04:28 +03:00
|
|
|
if (skc->skc_flags & KMC_KVMEM) {
|
2014-12-08 21:04:42 +03:00
|
|
|
spl_magazine_destroy(skc);
|
2014-12-06 01:11:18 +03:00
|
|
|
spl_slab_reclaim(skc);
|
2014-12-08 21:04:42 +03:00
|
|
|
} else {
|
|
|
|
ASSERT(skc->skc_flags & KMC_SLAB);
|
|
|
|
kmem_cache_destroy(skc->skc_linux_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
/*
|
|
|
|
* Validate there are no objects in use and free all the
|
|
|
|
* spl_kmem_slab_t, spl_kmem_obj_t, and object buffers.
|
|
|
|
*/
|
2014-12-08 21:04:42 +03:00
|
|
|
ASSERT3U(skc->skc_slab_alloc, ==, 0);
|
|
|
|
ASSERT3U(skc->skc_obj_alloc, ==, 0);
|
|
|
|
ASSERT3U(skc->skc_slab_total, ==, 0);
|
|
|
|
ASSERT3U(skc->skc_obj_total, ==, 0);
|
|
|
|
ASSERT3U(skc->skc_obj_emergency, ==, 0);
|
|
|
|
ASSERT(list_empty(&skc->skc_complete_list));
|
|
|
|
|
2020-06-27 04:06:50 +03:00
|
|
|
ASSERT3U(percpu_counter_sum(&skc->skc_linux_alloc), ==, 0);
|
|
|
|
percpu_counter_destroy(&skc->skc_linux_alloc);
|
|
|
|
|
2014-12-08 21:04:42 +03:00
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
kfree(skc->skc_name);
|
|
|
|
kfree(skc);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_cache_destroy);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate an object from a slab attached to the cache. This is used to
|
|
|
|
* repopulate the per-cpu magazine caches in batches when they run low.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
spl_cache_obj(spl_kmem_cache_t *skc, spl_kmem_slab_t *sks)
|
|
|
|
{
|
|
|
|
spl_kmem_obj_t *sko;
|
|
|
|
|
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
|
|
|
ASSERT(sks->sks_magic == SKS_MAGIC);
|
|
|
|
|
|
|
|
sko = list_entry(sks->sks_free_list.next, spl_kmem_obj_t, sko_list);
|
|
|
|
ASSERT(sko->sko_magic == SKO_MAGIC);
|
|
|
|
ASSERT(sko->sko_addr != NULL);
|
|
|
|
|
|
|
|
/* Remove from sks_free_list */
|
|
|
|
list_del_init(&sko->sko_list);
|
|
|
|
|
|
|
|
sks->sks_age = jiffies;
|
|
|
|
sks->sks_ref++;
|
|
|
|
skc->skc_obj_alloc++;
|
|
|
|
|
|
|
|
/* Track max obj usage statistics */
|
|
|
|
if (skc->skc_obj_alloc > skc->skc_obj_max)
|
|
|
|
skc->skc_obj_max = skc->skc_obj_alloc;
|
|
|
|
|
|
|
|
/* Track max slab usage statistics */
|
|
|
|
if (sks->sks_ref == 1) {
|
|
|
|
skc->skc_slab_alloc++;
|
|
|
|
|
|
|
|
if (skc->skc_slab_alloc > skc->skc_slab_max)
|
|
|
|
skc->skc_slab_max = skc->skc_slab_alloc;
|
|
|
|
}
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
return (sko->sko_addr);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic slab allocation function to run by the global work queues.
|
|
|
|
* It is responsible for allocating a new slab, linking it in to the list
|
|
|
|
* of partial slabs, and then waking any waiters.
|
|
|
|
*/
|
2016-05-19 20:59:40 +03:00
|
|
|
static int
|
|
|
|
__spl_cache_grow(spl_kmem_cache_t *skc, int flags)
|
2014-12-08 21:04:42 +03:00
|
|
|
{
|
|
|
|
spl_kmem_slab_t *sks;
|
|
|
|
|
2014-07-13 22:45:20 +04:00
|
|
|
fstrans_cookie_t cookie = spl_fstrans_mark();
|
2016-05-19 20:59:40 +03:00
|
|
|
sks = spl_slab_alloc(skc, flags);
|
2014-07-13 22:45:20 +04:00
|
|
|
spl_fstrans_unmark(cookie);
|
2015-12-18 05:31:58 +03:00
|
|
|
|
2014-12-08 21:04:42 +03:00
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
if (sks) {
|
|
|
|
skc->skc_slab_total++;
|
|
|
|
skc->skc_obj_total += sks->sks_objs;
|
|
|
|
list_add_tail(&sks->sks_list, &skc->skc_partial_list);
|
2016-05-19 20:59:40 +03:00
|
|
|
|
|
|
|
smp_mb__before_atomic();
|
|
|
|
clear_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags);
|
|
|
|
smp_mb__after_atomic();
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
2016-05-19 20:59:40 +03:00
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
|
|
|
|
return (sks == NULL ? -ENOMEM : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spl_cache_grow_work(void *data)
|
|
|
|
{
|
|
|
|
spl_kmem_alloc_t *ska = (spl_kmem_alloc_t *)data;
|
|
|
|
spl_kmem_cache_t *skc = ska->ska_cache;
|
|
|
|
|
2020-02-13 22:23:02 +03:00
|
|
|
int error = __spl_cache_grow(skc, ska->ska_flags);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
atomic_dec(&skc->skc_ref);
|
Enforce architecture-specific barriers around clear_bit()
The comment above the Linux 3.16 kernel's clear_bit() states:
/**
* clear_bit - Clears a bit in memory
* @nr: Bit to clear
* @addr: Address to start counting from
*
* clear_bit() is atomic and may not be reordered. However, it does
* not contain a memory barrier, so if it is used for locking purposes,
* you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
* in order to ensure changes are visible on other processors.
*/
This comment does not make sense in the context of x86 because x86 maps the
operations to barrier(), which is a compiler barrier. However, it does make
sense to me when I consider architectures that reorder around atomic
instructions. In such situations, a processor is allowed to execute the
wake_up_bit() before clear_bit() and we have a race. There are a few
architectures that suffer from this issue.
In such situations, the other processor would wake-up, see the bit is still
taken and go to sleep, while the one responsible for waking it up will
assume that it did its job and continue.
This patch implements a wrapper that maps smp_mb__{before,after}_atomic() to
smp_mb__{before,after}_clear_bit() on older kernels and changes our code to
leverage it in a manner consistent with the mainline kernel.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-05 02:47:51 +03:00
|
|
|
smp_mb__before_atomic();
|
2014-12-08 21:04:42 +03:00
|
|
|
clear_bit(KMC_BIT_GROWING, &skc->skc_flags);
|
Enforce architecture-specific barriers around clear_bit()
The comment above the Linux 3.16 kernel's clear_bit() states:
/**
* clear_bit - Clears a bit in memory
* @nr: Bit to clear
* @addr: Address to start counting from
*
* clear_bit() is atomic and may not be reordered. However, it does
* not contain a memory barrier, so if it is used for locking purposes,
* you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
* in order to ensure changes are visible on other processors.
*/
This comment does not make sense in the context of x86 because x86 maps the
operations to barrier(), which is a compiler barrier. However, it does make
sense to me when I consider architectures that reorder around atomic
instructions. In such situations, a processor is allowed to execute the
wake_up_bit() before clear_bit() and we have a race. There are a few
architectures that suffer from this issue.
In such situations, the other processor would wake-up, see the bit is still
taken and go to sleep, while the one responsible for waking it up will
assume that it did its job and continue.
This patch implements a wrapper that maps smp_mb__{before,after}_atomic() to
smp_mb__{before,after}_clear_bit() on older kernels and changes our code to
leverage it in a manner consistent with the mainline kernel.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-05 02:47:51 +03:00
|
|
|
smp_mb__after_atomic();
|
2020-02-13 22:23:02 +03:00
|
|
|
if (error == 0)
|
|
|
|
wake_up_all(&skc->skc_waitq);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
kfree(ska);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns non-zero when a new slab should be available.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spl_cache_grow_wait(spl_kmem_cache_t *skc)
|
|
|
|
{
|
2014-12-08 21:35:51 +03:00
|
|
|
return (!test_bit(KMC_BIT_GROWING, &skc->skc_flags));
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No available objects on any slabs, create a new slab. Note that this
|
|
|
|
* functionality is disabled for KMC_SLAB caches which are backed by the
|
|
|
|
* Linux slab.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spl_cache_grow(spl_kmem_cache_t *skc, int flags, void **obj)
|
|
|
|
{
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
int remaining, rc = 0;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
ASSERT0(flags & ~KM_PUBLIC_MASK);
|
2014-12-08 21:04:42 +03:00
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
|
|
|
ASSERT((skc->skc_flags & KMC_SLAB) == 0);
|
|
|
|
might_sleep();
|
|
|
|
*obj = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before allocating a new slab wait for any reaping to complete and
|
|
|
|
* then return so the local magazine can be rechecked for new objects.
|
|
|
|
*/
|
|
|
|
if (test_bit(KMC_BIT_REAPING, &skc->skc_flags)) {
|
|
|
|
rc = spl_wait_on_bit(&skc->skc_flags, KMC_BIT_REAPING,
|
|
|
|
TASK_UNINTERRUPTIBLE);
|
|
|
|
return (rc ? rc : -EAGAIN);
|
|
|
|
}
|
|
|
|
|
2016-05-19 20:59:40 +03:00
|
|
|
/*
|
2020-08-18 02:04:28 +03:00
|
|
|
* Note: It would be nice to reduce the overhead of context switch
|
|
|
|
* and improve NUMA locality, by trying to allocate a new slab in the
|
|
|
|
* current process context with KM_NOSLEEP flag.
|
2016-05-19 20:59:40 +03:00
|
|
|
*
|
2020-08-18 02:04:28 +03:00
|
|
|
* However, this can't be applied to vmem/kvmem due to a bug that
|
2020-06-09 02:32:02 +03:00
|
|
|
* spl_vmalloc() doesn't honor gfp flags in page table allocation.
|
2016-05-19 20:59:40 +03:00
|
|
|
*/
|
|
|
|
|
2014-12-08 21:04:42 +03:00
|
|
|
/*
|
|
|
|
* This is handled by dispatching a work request to the global work
|
|
|
|
* queue. This allows us to asynchronously allocate a new slab while
|
|
|
|
* retaining the ability to safely fall back to a smaller synchronous
|
|
|
|
* allocations to ensure forward progress is always maintained.
|
|
|
|
*/
|
|
|
|
if (test_and_set_bit(KMC_BIT_GROWING, &skc->skc_flags) == 0) {
|
|
|
|
spl_kmem_alloc_t *ska;
|
|
|
|
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
ska = kmalloc(sizeof (*ska), kmem_flags_convert(flags));
|
2014-12-08 21:04:42 +03:00
|
|
|
if (ska == NULL) {
|
Enforce architecture-specific barriers around clear_bit()
The comment above the Linux 3.16 kernel's clear_bit() states:
/**
* clear_bit - Clears a bit in memory
* @nr: Bit to clear
* @addr: Address to start counting from
*
* clear_bit() is atomic and may not be reordered. However, it does
* not contain a memory barrier, so if it is used for locking purposes,
* you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
* in order to ensure changes are visible on other processors.
*/
This comment does not make sense in the context of x86 because x86 maps the
operations to barrier(), which is a compiler barrier. However, it does make
sense to me when I consider architectures that reorder around atomic
instructions. In such situations, a processor is allowed to execute the
wake_up_bit() before clear_bit() and we have a race. There are a few
architectures that suffer from this issue.
In such situations, the other processor would wake-up, see the bit is still
taken and go to sleep, while the one responsible for waking it up will
assume that it did its job and continue.
This patch implements a wrapper that maps smp_mb__{before,after}_atomic() to
smp_mb__{before,after}_clear_bit() on older kernels and changes our code to
leverage it in a manner consistent with the mainline kernel.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-05 02:47:51 +03:00
|
|
|
clear_bit_unlock(KMC_BIT_GROWING, &skc->skc_flags);
|
|
|
|
smp_mb__after_atomic();
|
2014-12-08 21:04:42 +03:00
|
|
|
wake_up_all(&skc->skc_waitq);
|
|
|
|
return (-ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic_inc(&skc->skc_ref);
|
|
|
|
ska->ska_cache = skc;
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
ska->ska_flags = flags;
|
2014-12-08 21:04:42 +03:00
|
|
|
taskq_init_ent(&ska->ska_tqe);
|
|
|
|
taskq_dispatch_ent(spl_kmem_cache_taskq,
|
|
|
|
spl_cache_grow_work, ska, 0, &ska->ska_tqe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The goal here is to only detect the rare case where a virtual slab
|
|
|
|
* allocation has deadlocked. We must be careful to minimize the use
|
|
|
|
* of emergency objects which are more expensive to track. Therefore,
|
|
|
|
* we set a very long timeout for the asynchronous allocation and if
|
|
|
|
* the timeout is reached the cache is flagged as deadlocked. From
|
|
|
|
* this point only new emergency objects will be allocated until the
|
|
|
|
* asynchronous allocation completes and clears the deadlocked flag.
|
|
|
|
*/
|
|
|
|
if (test_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags)) {
|
|
|
|
rc = spl_emergency_alloc(skc, flags, obj);
|
|
|
|
} else {
|
|
|
|
remaining = wait_event_timeout(skc->skc_waitq,
|
2014-12-16 03:02:48 +03:00
|
|
|
spl_cache_grow_wait(skc), HZ / 10);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2015-01-10 01:00:34 +03:00
|
|
|
if (!remaining) {
|
2014-12-08 21:04:42 +03:00
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
if (test_bit(KMC_BIT_GROWING, &skc->skc_flags)) {
|
|
|
|
set_bit(KMC_BIT_DEADLOCKED, &skc->skc_flags);
|
|
|
|
skc->skc_obj_deadlock++;
|
|
|
|
}
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Refill a per-cpu magazine with objects from the slabs for this cache.
|
|
|
|
* Ideally the magazine can be repopulated using existing objects which have
|
|
|
|
* been released, however if we are unable to locate enough free objects new
|
|
|
|
* slabs of objects will be created. On success NULL is returned, otherwise
|
|
|
|
* the address of a single emergency object is returned for use by the caller.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
spl_cache_refill(spl_kmem_cache_t *skc, spl_kmem_magazine_t *skm, int flags)
|
|
|
|
{
|
|
|
|
spl_kmem_slab_t *sks;
|
|
|
|
int count = 0, rc, refill;
|
|
|
|
void *obj = NULL;
|
|
|
|
|
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
|
|
|
ASSERT(skm->skm_magic == SKM_MAGIC);
|
|
|
|
|
|
|
|
refill = MIN(skm->skm_refill, skm->skm_size - skm->skm_avail);
|
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
|
|
|
|
while (refill > 0) {
|
|
|
|
/* No slabs available we may need to grow the cache */
|
|
|
|
if (list_empty(&skc->skc_partial_list)) {
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
|
|
|
|
local_irq_enable();
|
|
|
|
rc = spl_cache_grow(skc, flags, &obj);
|
|
|
|
local_irq_disable();
|
|
|
|
|
|
|
|
/* Emergency object for immediate use by caller */
|
|
|
|
if (rc == 0 && obj != NULL)
|
|
|
|
return (obj);
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Rescheduled to different CPU skm is not local */
|
|
|
|
if (skm != skc->skc_mag[smp_processor_id()])
|
|
|
|
goto out;
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
/*
|
|
|
|
* Potentially rescheduled to the same CPU but
|
2014-12-08 21:04:42 +03:00
|
|
|
* allocations may have occurred from this CPU while
|
2014-12-08 21:35:51 +03:00
|
|
|
* we were sleeping so recalculate max refill.
|
|
|
|
*/
|
2014-12-08 21:04:42 +03:00
|
|
|
refill = MIN(refill, skm->skm_size - skm->skm_avail);
|
|
|
|
|
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Grab the next available slab */
|
|
|
|
sks = list_entry((&skc->skc_partial_list)->next,
|
2014-12-08 21:35:51 +03:00
|
|
|
spl_kmem_slab_t, sks_list);
|
2014-12-08 21:04:42 +03:00
|
|
|
ASSERT(sks->sks_magic == SKS_MAGIC);
|
|
|
|
ASSERT(sks->sks_ref < sks->sks_objs);
|
|
|
|
ASSERT(!list_empty(&sks->sks_free_list));
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
/*
|
|
|
|
* Consume as many objects as needed to refill the requested
|
|
|
|
* cache. We must also be careful not to overfill it.
|
|
|
|
*/
|
|
|
|
while (sks->sks_ref < sks->sks_objs && refill-- > 0 &&
|
|
|
|
++count) {
|
2014-12-08 21:04:42 +03:00
|
|
|
ASSERT(skm->skm_avail < skm->skm_size);
|
|
|
|
ASSERT(count < skm->skm_size);
|
2014-12-08 21:35:51 +03:00
|
|
|
skm->skm_objs[skm->skm_avail++] =
|
|
|
|
spl_cache_obj(skc, sks);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Move slab to skc_complete_list when full */
|
|
|
|
if (sks->sks_ref == sks->sks_objs) {
|
|
|
|
list_del(&sks->sks_list);
|
|
|
|
list_add(&sks->sks_list, &skc->skc_complete_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
out:
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release an object back to the slab from which it came.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
spl_cache_shrink(spl_kmem_cache_t *skc, void *obj)
|
|
|
|
{
|
|
|
|
spl_kmem_slab_t *sks = NULL;
|
|
|
|
spl_kmem_obj_t *sko = NULL;
|
|
|
|
|
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
|
|
|
|
|
|
|
sko = spl_sko_from_obj(skc, obj);
|
|
|
|
ASSERT(sko->sko_magic == SKO_MAGIC);
|
|
|
|
sks = sko->sko_slab;
|
|
|
|
ASSERT(sks->sks_magic == SKS_MAGIC);
|
|
|
|
ASSERT(sks->sks_cache == skc);
|
|
|
|
list_add(&sko->sko_list, &sks->sks_free_list);
|
|
|
|
|
|
|
|
sks->sks_age = jiffies;
|
|
|
|
sks->sks_ref--;
|
|
|
|
skc->skc_obj_alloc--;
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
/*
|
|
|
|
* Move slab to skc_partial_list when no longer full. Slabs
|
2014-12-08 21:04:42 +03:00
|
|
|
* are added to the head to keep the partial list is quasi-full
|
2014-12-08 21:35:51 +03:00
|
|
|
* sorted order. Fuller at the head, emptier at the tail.
|
|
|
|
*/
|
2014-12-08 21:04:42 +03:00
|
|
|
if (sks->sks_ref == (sks->sks_objs - 1)) {
|
|
|
|
list_del(&sks->sks_list);
|
|
|
|
list_add(&sks->sks_list, &skc->skc_partial_list);
|
|
|
|
}
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
/*
|
|
|
|
* Move empty slabs to the end of the partial list so
|
|
|
|
* they can be easily found and freed during reclamation.
|
|
|
|
*/
|
2014-12-08 21:04:42 +03:00
|
|
|
if (sks->sks_ref == 0) {
|
|
|
|
list_del(&sks->sks_list);
|
|
|
|
list_add_tail(&sks->sks_list, &skc->skc_partial_list);
|
|
|
|
skc->skc_slab_alloc--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate an object from the per-cpu magazine, or if the magazine
|
|
|
|
* is empty directly allocate from a slab and repopulate the magazine.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
spl_kmem_cache_alloc(spl_kmem_cache_t *skc, int flags)
|
|
|
|
{
|
|
|
|
spl_kmem_magazine_t *skm;
|
|
|
|
void *obj = NULL;
|
|
|
|
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
ASSERT0(flags & ~KM_PUBLIC_MASK);
|
2014-12-08 21:04:42 +03:00
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
|
|
|
ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate directly from a Linux slab. All optimizations are left
|
|
|
|
* to the underlying cache we only need to guarantee that KM_SLEEP
|
|
|
|
* callers will never fail.
|
|
|
|
*/
|
|
|
|
if (skc->skc_flags & KMC_SLAB) {
|
|
|
|
struct kmem_cache *slc = skc->skc_linux_cache;
|
|
|
|
do {
|
Refactor generic memory allocation interfaces
This patch achieves the following goals:
1. It replaces the preprocessor kmem flag to gfp flag mapping with
proper translation logic. This eliminates the potential for
surprises that were previously possible where kmem flags were
mapped to gfp flags.
2. It maps vmem_alloc() allocations to kmem_alloc() for allocations
sized less than or equal to the newly-added spl_kmem_alloc_max
parameter. This ensures that small allocations will not contend
on a single global lock, large allocations can still be handled,
and potentially limited virtual address space will not be squandered.
This behavior is entirely different than under Illumos due to
different memory management strategies employed by the respective
kernels. However, this functionally provides the semantics required.
3. The --disable-debug-kmem, --enable-debug-kmem (default), and
--enable-debug-kmem-tracking allocators have been unified in to
a single spl_kmem_alloc_impl() allocation function. This was
done to simplify the code and make it more maintainable.
4. Improve portability by exposing an implementation of the memory
allocations functions that can be safely used in the same way
they are used on Illumos. Specifically, callers may safely
use KM_SLEEP in contexts which perform filesystem IO. This
allows us to eliminate an entire class of Linux specific changes
which were previously required to avoid deadlocking the system.
This change will be largely transparent to existing callers but there
are a few caveats:
1. Because the headers were refactored and extraneous includes removed
callers may find they need to explicitly add additional #includes.
In particular, kmem_cache.h must now be explicitly includes to
access the SPL's kmem cache implementation. This behavior is
different from Illumos but it was done to avoid always masking
the Linux slab functions when kmem.h is included.
2. Callers, like Lustre, which made assumptions about the definitions
of KM_SLEEP, KM_NOSLEEP, and KM_PUSHPAGE will need to be updated.
Other callers such as ZFS which did not will not require changes.
3. KM_PUSHPAGE is no longer overloaded to imply GFP_NOIO. It retains
its original meaning of allowing allocations to access reserved
memory. KM_PUSHPAGE callers can be converted back to KM_SLEEP.
4. The KM_NODEBUG flags has been retired and the default warning
threshold increased to 32k.
5. The kmem_virt() functions has been removed. For callers which
need to distinguish between a physical and virtual address use
is_vmalloc_addr().
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-08 23:37:14 +03:00
|
|
|
obj = kmem_cache_alloc(slc, kmem_flags_convert(flags));
|
2014-12-08 21:04:42 +03:00
|
|
|
} while ((obj == NULL) && !(flags & KM_NOSLEEP));
|
|
|
|
|
2019-10-18 20:24:28 +03:00
|
|
|
if (obj != NULL) {
|
|
|
|
/*
|
|
|
|
* Even though we leave everything up to the
|
|
|
|
* underlying cache we still keep track of
|
|
|
|
* how many objects we've allocated in it for
|
|
|
|
* better debuggability.
|
|
|
|
*/
|
2020-06-27 04:06:50 +03:00
|
|
|
percpu_counter_inc(&skc->skc_linux_alloc);
|
2019-10-18 20:24:28 +03:00
|
|
|
}
|
2014-12-08 21:04:42 +03:00
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
local_irq_disable();
|
|
|
|
|
|
|
|
restart:
|
2014-12-08 21:35:51 +03:00
|
|
|
/*
|
|
|
|
* Safe to update per-cpu structure without lock, but
|
2014-12-08 21:04:42 +03:00
|
|
|
* in the restart case we must be careful to reacquire
|
|
|
|
* the local magazine since this may have changed
|
2014-12-08 21:35:51 +03:00
|
|
|
* when we need to grow the cache.
|
|
|
|
*/
|
2014-12-08 21:04:42 +03:00
|
|
|
skm = skc->skc_mag[smp_processor_id()];
|
|
|
|
ASSERT(skm->skm_magic == SKM_MAGIC);
|
|
|
|
|
|
|
|
if (likely(skm->skm_avail)) {
|
|
|
|
/* Object available in CPU cache, use it */
|
|
|
|
obj = skm->skm_objs[--skm->skm_avail];
|
|
|
|
} else {
|
|
|
|
obj = spl_cache_refill(skc, skm, flags);
|
Refine slab cache sizing
This change is designed to improve the memory utilization of
slabs by more carefully setting their size. The way the code
currently works is problematic for slabs which contain large
objects (>1MB). This is due to slabs being unconditionally
rounded up to a power of two which may result in unused space
at the end of the slab.
The reason the existing code rounds up every slab is because it
assumes it will backed by the buddy allocator. Since the buddy
allocator can only performs power of two allocations this is
desirable because it avoids wasting any space. However, this
logic breaks down if slab is backed by vmalloc() which operates
at a page level granularity. In this case, the optimal thing to
do is calculate the minimum required slab size given certain
constraints (object size, alignment, objects/slab, etc).
Therefore, this patch reworks the spl_slab_size() function so
that it sizes KMC_KMEM slabs differently than KMC_VMEM slabs.
KMC_KMEM slabs are rounded up to the nearest power of two, and
KMC_VMEM slabs are allowed to be the minimum required size.
This change also reduces the default number of objects per slab.
This reduces how much memory a single cache object can pin, which
can result in significant memory saving for highly fragmented
caches. But depending on the workload it may result in slabs
being allocated and freed more frequently. In practice, this
has been shown to be a better default for most workloads.
Also the maximum slab size has been reduced to 4MB on 32-bit
systems. Due to the limited virtual address space it's critical
the we be as frugal as possible. A limit of 4M still lets us
reasonably comfortably allocate a limited number of 1MB objects.
Finally, the kmem:slab_small and kmem:slab_large SPLAT tests
were extended to provide better test coverage of various object
sizes and alignments. Caches are created with random parameters
and their basic functionality is verified by allocating several
slabs worth of objects.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-16 01:06:18 +03:00
|
|
|
if ((obj == NULL) && !(flags & KM_NOSLEEP))
|
2014-12-08 21:04:42 +03:00
|
|
|
goto restart;
|
Refine slab cache sizing
This change is designed to improve the memory utilization of
slabs by more carefully setting their size. The way the code
currently works is problematic for slabs which contain large
objects (>1MB). This is due to slabs being unconditionally
rounded up to a power of two which may result in unused space
at the end of the slab.
The reason the existing code rounds up every slab is because it
assumes it will backed by the buddy allocator. Since the buddy
allocator can only performs power of two allocations this is
desirable because it avoids wasting any space. However, this
logic breaks down if slab is backed by vmalloc() which operates
at a page level granularity. In this case, the optimal thing to
do is calculate the minimum required slab size given certain
constraints (object size, alignment, objects/slab, etc).
Therefore, this patch reworks the spl_slab_size() function so
that it sizes KMC_KMEM slabs differently than KMC_VMEM slabs.
KMC_KMEM slabs are rounded up to the nearest power of two, and
KMC_VMEM slabs are allowed to be the minimum required size.
This change also reduces the default number of objects per slab.
This reduces how much memory a single cache object can pin, which
can result in significant memory saving for highly fragmented
caches. But depending on the workload it may result in slabs
being allocated and freed more frequently. In practice, this
has been shown to be a better default for most workloads.
Also the maximum slab size has been reduced to 4MB on 32-bit
systems. Due to the limited virtual address space it's critical
the we be as frugal as possible. A limit of 4M still lets us
reasonably comfortably allocate a limited number of 1MB objects.
Finally, the kmem:slab_small and kmem:slab_large SPLAT tests
were extended to provide better test coverage of various object
sizes and alignments. Caches are created with random parameters
and their basic functionality is verified by allocating several
slabs worth of objects.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-16 01:06:18 +03:00
|
|
|
|
|
|
|
local_irq_enable();
|
|
|
|
goto ret;
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
local_irq_enable();
|
|
|
|
ASSERT(obj);
|
|
|
|
ASSERT(IS_P2ALIGNED(obj, skc->skc_obj_align));
|
|
|
|
|
|
|
|
ret:
|
|
|
|
/* Pre-emptively migrate object to CPU L1 cache */
|
|
|
|
if (obj) {
|
|
|
|
if (obj && skc->skc_ctor)
|
|
|
|
skc->skc_ctor(obj, skc->skc_private, flags);
|
|
|
|
else
|
|
|
|
prefetchw(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (obj);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_cache_alloc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free an object back to the local per-cpu magazine, there is no
|
|
|
|
* guarantee that this is the same magazine the object was originally
|
|
|
|
* allocated from. We may need to flush entire from the magazine
|
|
|
|
* back to the slabs to make space.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
spl_kmem_cache_free(spl_kmem_cache_t *skc, void *obj)
|
|
|
|
{
|
|
|
|
spl_kmem_magazine_t *skm;
|
|
|
|
unsigned long flags;
|
2014-12-06 01:11:18 +03:00
|
|
|
int do_reclaim = 0;
|
2015-01-10 01:00:34 +03:00
|
|
|
int do_emergency = 0;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
|
|
|
ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the destructor
|
|
|
|
*/
|
|
|
|
if (skc->skc_dtor)
|
|
|
|
skc->skc_dtor(obj, skc->skc_private);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the object from the Linux underlying Linux slab.
|
|
|
|
*/
|
|
|
|
if (skc->skc_flags & KMC_SLAB) {
|
|
|
|
kmem_cache_free(skc->skc_linux_cache, obj);
|
2020-06-27 04:06:50 +03:00
|
|
|
percpu_counter_dec(&skc->skc_linux_alloc);
|
2015-07-23 23:45:31 +03:00
|
|
|
return;
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-10 01:00:34 +03:00
|
|
|
* While a cache has outstanding emergency objects all freed objects
|
|
|
|
* must be checked. However, since emergency objects will never use
|
|
|
|
* a virtual address these objects can be safely excluded as an
|
|
|
|
* optimization.
|
2014-12-08 21:04:42 +03:00
|
|
|
*/
|
2015-01-10 01:00:34 +03:00
|
|
|
if (!is_vmalloc_addr(obj)) {
|
|
|
|
spin_lock(&skc->skc_lock);
|
|
|
|
do_emergency = (skc->skc_obj_emergency > 0);
|
|
|
|
spin_unlock(&skc->skc_lock);
|
|
|
|
|
|
|
|
if (do_emergency && (spl_emergency_free(skc, obj) == 0))
|
2015-07-23 23:45:31 +03:00
|
|
|
return;
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
2014-12-08 21:35:51 +03:00
|
|
|
/*
|
|
|
|
* Safe to update per-cpu structure without lock, but
|
2014-12-08 21:04:42 +03:00
|
|
|
* no remote memory allocation tracking is being performed
|
|
|
|
* it is entirely possible to allocate an object from one
|
2014-12-08 21:35:51 +03:00
|
|
|
* CPU cache and return it to another.
|
|
|
|
*/
|
2014-12-08 21:04:42 +03:00
|
|
|
skm = skc->skc_mag[smp_processor_id()];
|
|
|
|
ASSERT(skm->skm_magic == SKM_MAGIC);
|
|
|
|
|
2014-12-06 01:11:18 +03:00
|
|
|
/*
|
|
|
|
* Per-CPU cache full, flush it to make space for this object,
|
|
|
|
* this may result in an empty slab which can be reclaimed once
|
|
|
|
* interrupts are re-enabled.
|
|
|
|
*/
|
|
|
|
if (unlikely(skm->skm_avail >= skm->skm_size)) {
|
2014-12-08 21:04:42 +03:00
|
|
|
spl_cache_flush(skc, skm, skm->skm_refill);
|
2014-12-06 01:11:18 +03:00
|
|
|
do_reclaim = 1;
|
|
|
|
}
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
/* Available space in cache, use it */
|
|
|
|
skm->skm_objs[skm->skm_avail++] = obj;
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
2014-12-06 01:11:18 +03:00
|
|
|
|
|
|
|
if (do_reclaim)
|
|
|
|
spl_slab_reclaim(skc);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_cache_free);
|
|
|
|
|
|
|
|
/*
|
Remove skc_reclaim, hdr_recl, kmem_cache shrinker
The SPL kmem_cache implementation provides a mechanism, `skc_reclaim`,
whereby individual caches can register a callback to be invoked when
there is memory pressure. This mechanism is used in only one place: the
ARC registers the `hdr_recl()` reclaim function. This function wakes up
the `arc_reap_zthr`, whose job is to call `kmem_cache_reap()` and
`arc_reduce_target_size()`.
The `skc_reclaim` callbacks are invoked only by shrinker callbacks and
`arc_reap_zthr`, and only callback only wakes up `arc_reap_zthr`. When
called from `arc_reap_zthr`, waking `arc_reap_zthr` is a no-op. When
called from shrinker callbacks, we are already aware of memory pressure
and responding to it. Therefore there is little benefit to ever calling
the `hdr_recl()` `skc_reclaim` callback.
The `arc_reap_zthr` also wakes once a second, and if memory is low when
allocating an ARC buffer. Therefore, additionally waking it from the
shrinker calbacks has little benefit.
The shrinker callbacks can be invoked very frequently, e.g. 10,000 times
per second. Additionally, for invocation of the shrinker callback,
skc_reclaim is invoked many times. Therefore, this mechanism consumes
significant amounts of CPU time.
The kmem_cache shrinker calls `spl_kmem_cache_reap_now()`, which,
in addition to invoking `skc_reclaim()`, does two things to attempt to
free pages for use by the system:
1. Return free objects from the magazine layer to the slab layer
2. Return entirely-free slabs to the page layer (i.e. free pages)
These actions apply only to caches implemented by the SPL, not those
that use the underlying kernel SLAB/SLUB caches. The SPL caches are
used for objects >=32KB, which are primarily linear ABD's cached in the
DBUF cache.
These actions (freeing objects from the magazine layer and returning
entirely-free slabs) are also taken whenever a `kmem_cache_free()` call
finds a full magazine. So there would typically be zero entirely-free
slabs, and the number of objects in magazines is limited (typically no
more than 64 objects per magazine, and there's one magazine per CPU).
Therefore the benefit of `spl_kmem_cache_reap_now()`, while nonzero, is
modest.
We also call `spl_kmem_cache_reap_now()` from the `arc_reap_zthr`, when
memory pressure is detected. Therefore, calling
`spl_kmem_cache_reap_now()` from the kmem_cache shrinker is not needed.
This commit removes the `skc_reclaim` mechanism, its only callback
`hdr_recl()`, and the kmem_cache shrinker callback.
Reviewed-By: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Reviewed-by: Pavel Zakharov <pavel.zakharov@delphix.com>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #10576
2020-07-19 19:58:30 +03:00
|
|
|
* Depending on how many and which objects are released it may simply
|
|
|
|
* repopulate the local magazine which will then need to age-out. Objects
|
|
|
|
* which cannot fit in the magazine will be released back to their slabs
|
|
|
|
* which will also need to age out before being released. This is all just
|
|
|
|
* best effort and we do not want to thrash creating and destroying slabs.
|
2014-12-08 21:04:42 +03:00
|
|
|
*/
|
|
|
|
void
|
Clean up OS-specific ARC and kmem code
OS-specific code (e.g. under `module/os/linux`) does not need to share
its code structure with any other operating systems. In particular, the
ARC and kmem code need not be similar to the code in illumos, because we
won't be syncing this OS-specific code between operating systems. For
example, if/when illumos support is added to the common repo, we would
add a file `module/os/illumos/zfs/arc_os.c` for the illumos versions of
this code.
Therefore, we can simplify the code in the OS-specific ARC and kmem
routines.
These changes do not impact system behavior, they are purely code
cleanup. The changes are:
Arenas are not used on Linux or FreeBSD (they are always `NULL`), so
`heap_arena`, `zio_arena`, and `zio_alloc_arena` can be removed, along
with code that uses them.
In `arc_available_memory()`:
* `desfree` is unused, remove it
* rename `freemem` to avoid conflict with pre-existing `#define`
* remove checks related to arenas
* use units of bytes, rather than converting from bytes to pages and
then back to bytes
`SPL_KMEM_CACHE_REAP` is unused, remove it.
`skc_reap` is unused, remove it.
The `count` argument to `spl_kmem_cache_reap_now()` is unused, remove
it.
`vmem_size()` and associated type and macros are unused, remove them.
In `arc_memory_throttle()`, use a less confusing variable name to store
the result of `arc_free_memory()`.
Reviewed-by: George Wilson <gwilson@delphix.com>
Reviewed-by: Pavel Zakharov <pavel.zakharov@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@ixsystems.com>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #10499
2020-06-29 19:01:07 +03:00
|
|
|
spl_kmem_cache_reap_now(spl_kmem_cache_t *skc)
|
2014-12-08 21:04:42 +03:00
|
|
|
{
|
|
|
|
ASSERT(skc->skc_magic == SKC_MAGIC);
|
|
|
|
ASSERT(!test_bit(KMC_BIT_DESTROY, &skc->skc_flags));
|
|
|
|
|
Remove skc_reclaim, hdr_recl, kmem_cache shrinker
The SPL kmem_cache implementation provides a mechanism, `skc_reclaim`,
whereby individual caches can register a callback to be invoked when
there is memory pressure. This mechanism is used in only one place: the
ARC registers the `hdr_recl()` reclaim function. This function wakes up
the `arc_reap_zthr`, whose job is to call `kmem_cache_reap()` and
`arc_reduce_target_size()`.
The `skc_reclaim` callbacks are invoked only by shrinker callbacks and
`arc_reap_zthr`, and only callback only wakes up `arc_reap_zthr`. When
called from `arc_reap_zthr`, waking `arc_reap_zthr` is a no-op. When
called from shrinker callbacks, we are already aware of memory pressure
and responding to it. Therefore there is little benefit to ever calling
the `hdr_recl()` `skc_reclaim` callback.
The `arc_reap_zthr` also wakes once a second, and if memory is low when
allocating an ARC buffer. Therefore, additionally waking it from the
shrinker calbacks has little benefit.
The shrinker callbacks can be invoked very frequently, e.g. 10,000 times
per second. Additionally, for invocation of the shrinker callback,
skc_reclaim is invoked many times. Therefore, this mechanism consumes
significant amounts of CPU time.
The kmem_cache shrinker calls `spl_kmem_cache_reap_now()`, which,
in addition to invoking `skc_reclaim()`, does two things to attempt to
free pages for use by the system:
1. Return free objects from the magazine layer to the slab layer
2. Return entirely-free slabs to the page layer (i.e. free pages)
These actions apply only to caches implemented by the SPL, not those
that use the underlying kernel SLAB/SLUB caches. The SPL caches are
used for objects >=32KB, which are primarily linear ABD's cached in the
DBUF cache.
These actions (freeing objects from the magazine layer and returning
entirely-free slabs) are also taken whenever a `kmem_cache_free()` call
finds a full magazine. So there would typically be zero entirely-free
slabs, and the number of objects in magazines is limited (typically no
more than 64 objects per magazine, and there's one magazine per CPU).
Therefore the benefit of `spl_kmem_cache_reap_now()`, while nonzero, is
modest.
We also call `spl_kmem_cache_reap_now()` from the `arc_reap_zthr`, when
memory pressure is detected. Therefore, calling
`spl_kmem_cache_reap_now()` from the kmem_cache shrinker is not needed.
This commit removes the `skc_reclaim` mechanism, its only callback
`hdr_recl()`, and the kmem_cache shrinker callback.
Reviewed-By: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Reviewed-by: Pavel Zakharov <pavel.zakharov@delphix.com>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #10576
2020-07-19 19:58:30 +03:00
|
|
|
if (skc->skc_flags & KMC_SLAB)
|
|
|
|
return;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
Remove skc_reclaim, hdr_recl, kmem_cache shrinker
The SPL kmem_cache implementation provides a mechanism, `skc_reclaim`,
whereby individual caches can register a callback to be invoked when
there is memory pressure. This mechanism is used in only one place: the
ARC registers the `hdr_recl()` reclaim function. This function wakes up
the `arc_reap_zthr`, whose job is to call `kmem_cache_reap()` and
`arc_reduce_target_size()`.
The `skc_reclaim` callbacks are invoked only by shrinker callbacks and
`arc_reap_zthr`, and only callback only wakes up `arc_reap_zthr`. When
called from `arc_reap_zthr`, waking `arc_reap_zthr` is a no-op. When
called from shrinker callbacks, we are already aware of memory pressure
and responding to it. Therefore there is little benefit to ever calling
the `hdr_recl()` `skc_reclaim` callback.
The `arc_reap_zthr` also wakes once a second, and if memory is low when
allocating an ARC buffer. Therefore, additionally waking it from the
shrinker calbacks has little benefit.
The shrinker callbacks can be invoked very frequently, e.g. 10,000 times
per second. Additionally, for invocation of the shrinker callback,
skc_reclaim is invoked many times. Therefore, this mechanism consumes
significant amounts of CPU time.
The kmem_cache shrinker calls `spl_kmem_cache_reap_now()`, which,
in addition to invoking `skc_reclaim()`, does two things to attempt to
free pages for use by the system:
1. Return free objects from the magazine layer to the slab layer
2. Return entirely-free slabs to the page layer (i.e. free pages)
These actions apply only to caches implemented by the SPL, not those
that use the underlying kernel SLAB/SLUB caches. The SPL caches are
used for objects >=32KB, which are primarily linear ABD's cached in the
DBUF cache.
These actions (freeing objects from the magazine layer and returning
entirely-free slabs) are also taken whenever a `kmem_cache_free()` call
finds a full magazine. So there would typically be zero entirely-free
slabs, and the number of objects in magazines is limited (typically no
more than 64 objects per magazine, and there's one magazine per CPU).
Therefore the benefit of `spl_kmem_cache_reap_now()`, while nonzero, is
modest.
We also call `spl_kmem_cache_reap_now()` from the `arc_reap_zthr`, when
memory pressure is detected. Therefore, calling
`spl_kmem_cache_reap_now()` from the kmem_cache shrinker is not needed.
This commit removes the `skc_reclaim` mechanism, its only callback
`hdr_recl()`, and the kmem_cache shrinker callback.
Reviewed-By: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Reviewed-by: Pavel Zakharov <pavel.zakharov@delphix.com>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #10576
2020-07-19 19:58:30 +03:00
|
|
|
atomic_inc(&skc->skc_ref);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prevent concurrent cache reaping when contended.
|
|
|
|
*/
|
|
|
|
if (test_and_set_bit(KMC_BIT_REAPING, &skc->skc_flags))
|
|
|
|
goto out;
|
|
|
|
|
2014-12-06 01:11:18 +03:00
|
|
|
/* Reclaim from the magazine and free all now empty slabs. */
|
2020-07-24 19:39:26 +03:00
|
|
|
unsigned long irq_flags;
|
|
|
|
local_irq_save(irq_flags);
|
|
|
|
spl_kmem_magazine_t *skm = skc->skc_mag[smp_processor_id()];
|
|
|
|
spl_cache_flush(skc, skm, skm->skm_avail);
|
|
|
|
local_irq_restore(irq_flags);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
2014-12-06 01:11:18 +03:00
|
|
|
spl_slab_reclaim(skc);
|
Enforce architecture-specific barriers around clear_bit()
The comment above the Linux 3.16 kernel's clear_bit() states:
/**
* clear_bit - Clears a bit in memory
* @nr: Bit to clear
* @addr: Address to start counting from
*
* clear_bit() is atomic and may not be reordered. However, it does
* not contain a memory barrier, so if it is used for locking purposes,
* you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
* in order to ensure changes are visible on other processors.
*/
This comment does not make sense in the context of x86 because x86 maps the
operations to barrier(), which is a compiler barrier. However, it does make
sense to me when I consider architectures that reorder around atomic
instructions. In such situations, a processor is allowed to execute the
wake_up_bit() before clear_bit() and we have a race. There are a few
architectures that suffer from this issue.
In such situations, the other processor would wake-up, see the bit is still
taken and go to sleep, while the one responsible for waking it up will
assume that it did its job and continue.
This patch implements a wrapper that maps smp_mb__{before,after}_atomic() to
smp_mb__{before,after}_clear_bit() on older kernels and changes our code to
leverage it in a manner consistent with the mainline kernel.
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
2014-12-05 02:47:51 +03:00
|
|
|
clear_bit_unlock(KMC_BIT_REAPING, &skc->skc_flags);
|
|
|
|
smp_mb__after_atomic();
|
2014-12-08 21:04:42 +03:00
|
|
|
wake_up_bit(&skc->skc_flags, KMC_BIT_REAPING);
|
|
|
|
out:
|
|
|
|
atomic_dec(&skc->skc_ref);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_cache_reap_now);
|
|
|
|
|
2017-03-16 02:41:52 +03:00
|
|
|
/*
|
|
|
|
* This is stubbed out for code consistency with other platforms. There
|
|
|
|
* is existing logic to prevent concurrent reaping so while this is ugly
|
|
|
|
* it should do no harm.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
spl_kmem_cache_reap_active()
|
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_cache_reap_active);
|
|
|
|
|
2014-12-08 21:04:42 +03:00
|
|
|
/*
|
|
|
|
* Reap all free slabs from all registered caches.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
spl_kmem_reap(void)
|
|
|
|
{
|
Remove skc_reclaim, hdr_recl, kmem_cache shrinker
The SPL kmem_cache implementation provides a mechanism, `skc_reclaim`,
whereby individual caches can register a callback to be invoked when
there is memory pressure. This mechanism is used in only one place: the
ARC registers the `hdr_recl()` reclaim function. This function wakes up
the `arc_reap_zthr`, whose job is to call `kmem_cache_reap()` and
`arc_reduce_target_size()`.
The `skc_reclaim` callbacks are invoked only by shrinker callbacks and
`arc_reap_zthr`, and only callback only wakes up `arc_reap_zthr`. When
called from `arc_reap_zthr`, waking `arc_reap_zthr` is a no-op. When
called from shrinker callbacks, we are already aware of memory pressure
and responding to it. Therefore there is little benefit to ever calling
the `hdr_recl()` `skc_reclaim` callback.
The `arc_reap_zthr` also wakes once a second, and if memory is low when
allocating an ARC buffer. Therefore, additionally waking it from the
shrinker calbacks has little benefit.
The shrinker callbacks can be invoked very frequently, e.g. 10,000 times
per second. Additionally, for invocation of the shrinker callback,
skc_reclaim is invoked many times. Therefore, this mechanism consumes
significant amounts of CPU time.
The kmem_cache shrinker calls `spl_kmem_cache_reap_now()`, which,
in addition to invoking `skc_reclaim()`, does two things to attempt to
free pages for use by the system:
1. Return free objects from the magazine layer to the slab layer
2. Return entirely-free slabs to the page layer (i.e. free pages)
These actions apply only to caches implemented by the SPL, not those
that use the underlying kernel SLAB/SLUB caches. The SPL caches are
used for objects >=32KB, which are primarily linear ABD's cached in the
DBUF cache.
These actions (freeing objects from the magazine layer and returning
entirely-free slabs) are also taken whenever a `kmem_cache_free()` call
finds a full magazine. So there would typically be zero entirely-free
slabs, and the number of objects in magazines is limited (typically no
more than 64 objects per magazine, and there's one magazine per CPU).
Therefore the benefit of `spl_kmem_cache_reap_now()`, while nonzero, is
modest.
We also call `spl_kmem_cache_reap_now()` from the `arc_reap_zthr`, when
memory pressure is detected. Therefore, calling
`spl_kmem_cache_reap_now()` from the kmem_cache shrinker is not needed.
This commit removes the `skc_reclaim` mechanism, its only callback
`hdr_recl()`, and the kmem_cache shrinker callback.
Reviewed-By: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Reviewed-by: Pavel Zakharov <pavel.zakharov@delphix.com>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #10576
2020-07-19 19:58:30 +03:00
|
|
|
spl_kmem_cache_t *skc = NULL;
|
2014-12-08 21:04:42 +03:00
|
|
|
|
Remove skc_reclaim, hdr_recl, kmem_cache shrinker
The SPL kmem_cache implementation provides a mechanism, `skc_reclaim`,
whereby individual caches can register a callback to be invoked when
there is memory pressure. This mechanism is used in only one place: the
ARC registers the `hdr_recl()` reclaim function. This function wakes up
the `arc_reap_zthr`, whose job is to call `kmem_cache_reap()` and
`arc_reduce_target_size()`.
The `skc_reclaim` callbacks are invoked only by shrinker callbacks and
`arc_reap_zthr`, and only callback only wakes up `arc_reap_zthr`. When
called from `arc_reap_zthr`, waking `arc_reap_zthr` is a no-op. When
called from shrinker callbacks, we are already aware of memory pressure
and responding to it. Therefore there is little benefit to ever calling
the `hdr_recl()` `skc_reclaim` callback.
The `arc_reap_zthr` also wakes once a second, and if memory is low when
allocating an ARC buffer. Therefore, additionally waking it from the
shrinker calbacks has little benefit.
The shrinker callbacks can be invoked very frequently, e.g. 10,000 times
per second. Additionally, for invocation of the shrinker callback,
skc_reclaim is invoked many times. Therefore, this mechanism consumes
significant amounts of CPU time.
The kmem_cache shrinker calls `spl_kmem_cache_reap_now()`, which,
in addition to invoking `skc_reclaim()`, does two things to attempt to
free pages for use by the system:
1. Return free objects from the magazine layer to the slab layer
2. Return entirely-free slabs to the page layer (i.e. free pages)
These actions apply only to caches implemented by the SPL, not those
that use the underlying kernel SLAB/SLUB caches. The SPL caches are
used for objects >=32KB, which are primarily linear ABD's cached in the
DBUF cache.
These actions (freeing objects from the magazine layer and returning
entirely-free slabs) are also taken whenever a `kmem_cache_free()` call
finds a full magazine. So there would typically be zero entirely-free
slabs, and the number of objects in magazines is limited (typically no
more than 64 objects per magazine, and there's one magazine per CPU).
Therefore the benefit of `spl_kmem_cache_reap_now()`, while nonzero, is
modest.
We also call `spl_kmem_cache_reap_now()` from the `arc_reap_zthr`, when
memory pressure is detected. Therefore, calling
`spl_kmem_cache_reap_now()` from the kmem_cache shrinker is not needed.
This commit removes the `skc_reclaim` mechanism, its only callback
`hdr_recl()`, and the kmem_cache shrinker callback.
Reviewed-By: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Reviewed-by: Pavel Zakharov <pavel.zakharov@delphix.com>
Signed-off-by: Matthew Ahrens <mahrens@delphix.com>
Closes #10576
2020-07-19 19:58:30 +03:00
|
|
|
down_read(&spl_kmem_cache_sem);
|
|
|
|
list_for_each_entry(skc, &spl_kmem_cache_list, skc_list) {
|
|
|
|
spl_kmem_cache_reap_now(skc);
|
|
|
|
}
|
|
|
|
up_read(&spl_kmem_cache_sem);
|
2014-12-08 21:04:42 +03:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(spl_kmem_reap);
|
|
|
|
|
|
|
|
int
|
|
|
|
spl_kmem_cache_init(void)
|
|
|
|
{
|
|
|
|
init_rwsem(&spl_kmem_cache_sem);
|
|
|
|
INIT_LIST_HEAD(&spl_kmem_cache_list);
|
|
|
|
spl_kmem_cache_taskq = taskq_create("spl_kmem_cache",
|
2015-07-24 20:32:55 +03:00
|
|
|
spl_kmem_cache_kmem_threads, maxclsyspri,
|
2015-06-24 19:53:47 +03:00
|
|
|
spl_kmem_cache_kmem_threads * 8, INT_MAX,
|
|
|
|
TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
|
2014-12-08 21:04:42 +03:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spl_kmem_cache_fini(void)
|
|
|
|
{
|
|
|
|
taskq_destroy(spl_kmem_cache_taskq);
|
|
|
|
}
|