Make KM_SLEEP an alias of KM_PUSHPAGE
Use GFP_NOIO in KM_SLEEP

This should prevent direct reclaim issues without requiring
Linux-specific changes to code from Solaris. This is what is done in
FreeBSD.

Note that a change to __taskq_dispatch() module/spl/spl-taskq.c is
needed to make this work. Changing KM_PUSHPAGE to use GFP_NOIO is fine,
but adding __GFP_HIGH to that triggers a hard-coded panic in
__taskq_dispatch() during zvol initialization. Removing the hard coded
panic has no ill effects.

Signed-off-by: Richard Yao <ryao@cs.stonybrook.edu>
Use Linux SLAB allocator for spl_kmem_cache objects

This replaces kmem_zalloc() allocations of spl_kmem_cache objects that
are approximately 32KB in size with a Linux SLAB allocations. This
eliminates the need to supress debug warnings and makes the changes
necessary to replace the __vmalloc() in kv_alloc() with SLAB
allocations.

We also take advantage of the SLAB allocator's SLAB_HWCACHE_ALIGN flag
to reduce false sharing, which could increase performance.

Signed-off-by: Richard Yao <ryao@cs.stonybrook.edu>
Revert "Make CONFIG_PREEMPT Fatal"

This reverts commit 7731d46b69bd893d515c55e87ffa8a9bd2ddfb38.

Signed-off-by: Prakash Surya <surya1@llnl.gov>
Remove SPL_LINUX_CONFIG autoconf macro

Since removing the check for CONFIG_PREEMPT, there are no consumers of
the SPL_LINUX_CONFIG macro. As such, there is no reason to keep it
around.

Signed-off-by: Prakash Surya <surya1@llnl.gov>
Emergency slab objects

This patch is designed to resolve a deadlock which can occur with
__vmalloc() based slabs.  The issue is that the Linux kernel does
not honor the flags passed to __vmalloc().  This makes it unsafe
to use in a writeback context.  Unfortunately, this is a use case
ZFS depends on for correct operation.

Fixing this issue in the upstream kernel was pursued and patches
are available which resolve the issue.

  https://bugs.gentoo.org/show_bug.cgi?id=416685

However, these changes were rejected because upstream felt that
using __vmalloc() in the context of writeback should never be done.
Their solution was for us to rewrite parts of ZFS to accomidate
the Linux VM.

While that is probably the right long term solution, and it is
something we want to pursue, it is not a trivial task and will
likely destabilize the existing code.  This work has been planned
for the 0.7.0 release but in the meanwhile we want to improve the
SPL slab implementation to accomidate this expected ZFS usage.

This is accomplished by performing the __vmalloc() asynchronously
in the context of a work queue.  This doesn't prevent the posibility
of the worker thread from deadlocking.  However, the caller can now
safely block on a wait queue for the slab allocation to complete.

Normally this will occur in a reasonable amount of time and the
caller will be woken up when the new slab is available,.  The objects
will then get cached in the per-cpu magazines and everything will
proceed as usual.

However, if the __vmalloc() deadlocks for the reasons described
above, or is just very slow, then the callers on the wait queues
will timeout out.  When this rare situation occurs they will attempt
to kmalloc() a single minimally sized object using the GFP_NOIO flags.
This allocation will not deadlock because kmalloc() will honor the
passed flags and the caller will be able to make forward progress.

As long as forward progress can be maintained then even if the
worker thread is deadlocked the critical thread will make progress.
This will eventually allow the deadlocked worker thread to complete
and normal operation will resume.

These emergency allocations will likely be slow since they require
contiguous pages.  However, their use should be rare so the impact
is expected to be minimal.  If that turns out not to be the case in
practice further optimizations are possible.

One additional concern is if these emergency objects are long lived.
Right now they are simply tracked on a list which must be walked when
an object is freed.  Is they accumulate on a system and the list
grows freeing objects will become more expensive.  This could be
handled relatively easily by using a hash instead of a list, but that
optimization (if needed) is left for a follow up patch.

Additionally, these emeregency objects could be repacked in to existing
slabs as objects are freed if the kmem_cache_set_move() functionality
was implemented.  See issue https://github.com/zfsonlinux/spl/issues/26
for full details.  This work would also help reduce ZFS's memory
fragmentation problems.

The /proc/spl/kmem/slab file has had two new columns added at the
end.  The 'emerg' column reports the current number of these emergency
objects in use for the cache, and the following 'max' column shows
the historical worst case.  These value should give us a good idea
of how often these objects are needed.  Based on these values under
real use cases we can tune the default behavior.

Lastly, as a side benefit using a single work queue for the slab
allocations should reduce cpu contention on the global virtual address
space lock.   This should manifest itself as reduced cpu usage for
the system.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Revert "Add TASKQ_NORECLAIM flag"

This reverts commit 372c2572336468cbf60272aa7e735b7ca0c3807c.  The
use of the PF_MEMALLOC flag was always a hack to work around memory
reclaim deadlocks.  Those issues are believed to be resolved so this
workaround can be safely reverted.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Revert "Detect kernels that honor gfp flags passed to vmalloc()"

This reverts commit 36811b4430aaea8c8b91bbe7d812a26799865499.
Which is no longer required because there is now SPL code in
place to safely handle the deadlocks the kernel patch was designed
to address.  Therefore we can unconditionally use vmalloc() and
drop all the PF_MEMALLOC code.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Revert "Fix NULL deref in balance_pgdat()"

This reverts commit b8b6e4c453929596b630fa1cca1ee26a532a2ab4.  The
use of the PF_MEMALLOC flag was always a hack to work around memory
reclaim deadlocks.  Those issues are believed to be resolved so this
workaround can be safely reverted.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Revert "Disable vmalloc() direct reclaim"

This reverts commit 2092cf68d89a51eb0d6193aeadabb579dfc4b4a0.  The
use of the PF_MEMALLOC flag was always a hack to work around memory
reclaim deadlocks.  Those issues are believed to be resolved so this
workaround can be safely reverted.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Add PF_NOFS debugging flag

PF_NOFS is a per-process debug flag which is set in current->flags to
detect when a process is performing an unsafe allocation.  All tasks
with PF_NOFS set must strictly use KM_PUSHPAGE for allocations because
if they enter direct reclaim and initiate I/O they may deadlock.

When debugging is disabled, any incorrect usage will be detected and
a call stack with a warning will be printed to the console.  The flags
will then be automatically corrected to allow for safe execution.  If
debugging is enabled this will be treated as a fatal condition.

To avoid any risk of conflicting with the existing PF_ flags.  The
PF_NOFS bit shadows the rarely used PF_MUTEX_TESTER bit.  Only when
CONFIG_RT_MUTEX_TESTER is not set, and we know this bit is unused,
will the PF_NOFS bit be valid.  Happily, most existing distributions
ship a kernel with CONFIG_RT_MUTEX_TESTER disabled.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Mutex ASSERT on self deadlock

Generate an assertion if we're going to deadlock the system by
attempting to acquire a mutex the process is already holding.

There are currently no known instances of this under normal
operation, but it _might_ be possible when using a ZVOL as a
swap device.  I want to ensure we catch this immediately if it
were to occur.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Switch KM_SLEEP to KM_PUSHPAGE

To be squashed in to a single commit when the branch is rebased
and all of these call sites have been fixed.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>