Honor __GFP_NORETRY in spl_kmem_cache_alloc()

The Open Solaris man page states that KM_NOSLEEP will "Return NULL
immediately if memory is not available". However, we currently do not
honor this behavior. We map KM_NOSLEEP to a Linux equivalent that
contains __GFP_NORETRY, which is meant to prevent this behavior. We
current this by checking for __GFP_NORETRY in spl_kmem_cache_alloc() and
spl_cache_refill() If it is set, exit the busy loop and return with
whatever we have done.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Call SLAB object constructors and destructors on demand

The SPL's SLAB code was written with an optimization to preconstruct
objects before they are used. This is semantically differs from the
Solaris behavior where objects are constructed and deconstructed on
demand. A consequence of preallocation is that buffers that should be
zeroed by constructors will contain stale data whenever the buffer is
reused. I discovered this when experimenting with the conversion of
important ARC memory allocations to SLAB allocation.

We switch to on-demand construction and deconstruction to correct this.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Linux 3.8 compat: Use kuid_t/kgid_t when required

When CONFIG_UIDGID_STRICT_TYPE_CHECKS is enabled uid_t/git_t are
replaced by kuid_t/kgid_t, which are structures instead of integral
types. This causes any code that uses an integral type to fail to build.
The User Namespace functionality introduced in Linux 3.8 requires
CONFIG_UIDGID_STRICT_TYPE_CHECKS, so we could not build against any
kernel that supported it.

We resolve this by converting between the new kuid_t/kgid_t structures
and the original uid_t/gid_t types.

Original-patch-by: DHE
Rewrite-by: Richard Yao <ryao@gentoo.org>
Return -1 for generic kmem cache shrinker

It has been observed that it's possible to get in a state where
shrink_slabs() will spin repeated invoking the generic kmem cache
shrinker.  It fails to detect it's not making forward progress
reclaiming from the cache and doesn't give up.  To ensure this
never occurs we unconditionally return -1 after reclaiming what
we can.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue zfsonlinux/zfs#1276
Issue zfsonlinux/zfs#1598
Allocate spl_kmem_alloc_t objects from mempool

Allocating new slabs requires first allocating a spl_kmem_alloc_t
object before the actual allocation routine is done. This allocation can
fail when the system is memory constrained. This is particularly bad
because swap operations on ZFS can depend on the success of slab
allocation. We attempt to ensure that spl_kmem_alloc_t always succeed by
switching to the Linux slab allocator and pre-reserving memory via a
Linux mempool. This should prevent spl_kmem_alloc_t allocations from
failing.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Do not check for failures from internal KM_SLEEP allocations

KM_SLEEP allocations do not fail, so it makes little sense to check for
a failure. Lets convert these checks into assertions.

Signed-off-by: Richard Yao <ryao@gentoo.org>
kmem_alloc/vmem_alloc: Yield CPU before retrying memory allocations

Solaris and Linux both have kswapd for paging out memory. However, the
Linux kernel also has a concept of direct reclaim, where an allocation
that is low on memory will attempt to free memory. Unfortunately, we can
have hundreds of threads doing this when the system is under heavy load.
This can cause discernible lags and will leave the system in a state
where it appears to be deadlocked.

We set __GFP_NORETRY to prevent the kernel's own routines from retrying
allocations repeatedly. We then try the allocation, yield the CPU if it
fails and then try again. This should allow us to perform more
gracefully when hundreds of threads block on memory allocations during
heavy system load.

Signed-off-by: Richard Yao <ryao@gentoo.org>
Force emergency allocations to trigger under low memory conditions

A concept of an emergency allocation was introduced last year to permit
allocations that took excessive periods of time to switch to physical
memory to make forward progress. Emergency allocations currently occur
by setting a flag, telling kmem_cache_alloc() to try again and then
doing the allocation. This is useful, but it might not always trigger
because we do not permit allocations in the spl_cache_grow() to
retry. This permits allocation failures to fail before the timer for
emergency allocations can trigger. This will result in a return to
kmem_cache_alloc(), which will then repeat the process. The result is
that we can loop infinitely without any forward progress under low
memory conditions because the emergency allocations will not trigger.

We change the code to force the worker to retry indefinitely. This
guarentees that the emergency allocations will trigger under low memory
conditions. In addition, we remove the KMC_BIT_DEADLOCKED bit in favor
of immediate use of the emergency allocation.

Signed-off-by: Richard Yao <ryao@gentoo.org>