zfs: mmu: take vma_list_mutex before tearing down PT during ARC buffer eviction

Without the lock page mapping and buffer eviction can run in parallel
which may cause following race:

page mapper thread               thread calling arc_evict()
map_addr() {
  page = arc_get_page();
  add_mapping(page, ptep);
                                  evict(page) {
                                    ptep = get_mapping(page);
                                    ptep.write(0);
                                    free(page);
                                  }

  ptep.write(page);
}

ARC code has no well defined order for taking its mutexes. It uses trylock()
and skips a buffer if required locks cannot be acquired. This patch uses
same approach for vma_list_mutex: if evicted buffer is shared try to
lock vma_list_mutex and skip the buffer if this fails.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
aarch64: fix up merge issues

lots of mmu related changes require fixups for AArch64.

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
aarch64: stub new mmu functions in bsd

commit 82f881a2 "zfs: mmu: take vma_list_mutex"
introduces code that needs to be stubbed for now for AArch64.

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
scripts: Add build-standalone-img to build images used without capstan

This script is similar to build-capstan-img. Later we can hook this
script to scripts/build-osv-release to build images used without
capstan. It takes the same args with build-capstan-img.

Example output:

   $ find build/standalone
   build/standalone/cloudius/
   build/standalone/cloudius/osv-iperf
   build/standalone/cloudius/osv-iperf/osv-iperf.esx.ova
   build/standalone/cloudius/osv-iperf/osv-iperf.qemu.qcow2
   build/standalone/cloudius/osv-iperf/osv-iperf.vbox.ova
   build/standalone/cloudius/osv-iperf/osv-iperf.vmw.zip
   build/standalone/cloudius/osv-iperf/osv-iperf.gce.tar.gz

Signed-off-by: Asias He <asias@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
build-osv-release: standalone image support

Make scripts/build-osv-release support both Capstan and standalone
images.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Revert "build: Skip rebuild of usr.img on osv.vmdk and osv.vdi target"

This reverts commit df30ec8d32dac5ad72d71314ab327c48d4e8a054. It breaks
building "osv.vdi" altogether.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
build-standalone-img: Add version number to image filename

Add OSv version number to image filename so that we can actually have
more than one release available for download...

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
build-standalone-img: Fix image versioning

The typo in commit 425c5ce ("build-standalone-img: Add version number to
image filename") caused the shell script to evaluate "version" command
instead of expanding the "version" variable. Fix that.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
dhcp: remove lookup_opcode()

The lookup_opcode() function is incorrect. It was mishandling
DHCP_OPTION_PAD, which does not have a following length byte.

Also, the while condition is reading 'op' value which never
changes. This may result in reads beyond packet size.

Since this function is unused the best fix is to remove it.

Reveiwed-by: Vlad Zolotarov <vladz@cloudius-systems.com>
Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
dhcp: fix parsing of DHCP options

There are several issues with current code. Firstly, the LENGTH_OK
macro was used in the condition in while(). This macro was checking if
options + op_len does not exceed packet limit. This macro works fine
when used from PARSE_OP() inside the switch, but because 'options' is
bumped up by op_len at the end of the loop body, use of this macro in
the while condition may result in premature exit of the loop. This was
causing that some times OSv was not parsing network mask and gateway
leaving them at 0.0.0.0 when started on Goodle Compute Engine. As a
result OSv was not responding over network. See issue #254.

Another issue was that the stop condition which checks for op ==
DHCP_OPTION_END was using 'op' from the outer context, which was never
overwritten. The actual variable which was changed based on the packet
content was redeclared inside the loop.

A third problem, spotted by Vlad, is that the code was not handling
DHCP_OPTION_PAD properly. This option has only opcode byte and no
following length byte. Currrent code would attempt to read the length
byte and skip by that amount, which would yiled incorrect parsing
result.

Reviewed-by: Vlad Zolotarov <vladz@cloudius-systems.com>
Signed-off-by: Tomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
zfs: spa_zio_taskq[ZIO_TYPE_FREE][ZIO_TASKQ_ISSUE]->tq_lock contention

Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: Gordon Ross <gordon.ross@nexenta.com>
Approved by: Richard Lowe <richlowe@richlowe.net>

Reference: https://illumos.org/issues/3581

Patch taken from Illumos and slight changes were needed to port it to OSv.

This patch targets improvement on taskq lock contention by dispatching work
over independent task queues. ZFS on Linux devops mention that it's not clear
whether or not this issue affects their port, but profile results showed that
time spent on taskq_thread() was reduced by about 11%.
Apart from getting performance benefits, the number of threads in OSv was
nicely reduced (from ~344 threads to ~224; so possibly saving a good amount
of memory footprint).
Also good for stepping towards our synchronicity with ZFS upstream.

Addressing the issue #247.

Reviewed-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Raphael S. Carvalho <raphaelsc@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
jvmballoon: fix _soft_max_balloons check

Number of to be released balloons is calculated as a difference between
current number of balloons and sof max. If they are equal no balloons
are released and the loop repeats.

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
build.mk: build arm-clock stub

forgot to build the arm-clock stub, which will cause
a NULL pointer dereference when we turn the mmu on.

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
mmu: remove overloaded align_up and align_down

align.hh functions with the name name can be used instead
without any downside, and with the upside of less confusion.

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
exceptions: add accessors for pc and error

in order to share most of the fault code in core/mmu.cc,
we add accessors to get the program counter and an error
code whose interpretation is arch-dependent
(error_code for x64, ESR for AArch64).

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
aarch64: create initial page tables

create the initial page tables for TTBR0 / TTBR1
and set up the registers for the MMU.

Signed-off-by: Jani Kokkonen <jani.kokkonen@huawei.com>
Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>
aarch64: add page tables, MMU

Signed-off-by: Claudio Fontana <claudio.fontana@huawei.com>