Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
APFS: one request's compressed pieces on four processors at once
vcvkk91daf3d50072
diff --git a/tools/apfs/apfs-tool.c b/tools/apfs/apfs-tool.cindex a225967..7cdaaf1 100644--- a/tools/apfs/apfs-tool.c+++ b/tools/apfs/apfs-tool.c@@ -19,6 +19,8 @@/* pread(2) and clock_gettime(2) come with -D_DEFAULT_SOURCE, set by build-tool.sh */#include <fcntl.h>#include <inttypes.h>+#include <pthread.h>+#include <stdatomic.h>#include <stdio.h>#include <stdlib.h>#include <string.h>@@ -62,6 +64,81 @@ fnv1a(const uint8_t *p, size_t n, uint64_t h)#define FNV_INIT 0xcbf29ce484222325ULL+/*+ * APFS_TOOL_THREADS=N: nx->parallel on threads of this program, so that what the kernel+ * does on several processors -- whole pieces of one request read and decompressed at once+ * -- runs here the same way (and can be run under ThreadSanitizer). A thread per worker+ * per call: slow, and only for checking.+ */+struct host_par {+ int (*fn)(void *arg, uint32_t item, uint32_t worker);+ void *arg;+ uint32_t items;+ atomic_uint next;+ atomic_int error;+};++struct host_par_arg {+ struct host_par *hp;+ uint32_t worker;+};++static void+host_par_work(struct host_par *hp, uint32_t worker)+{+ for (;;) {+ uint32_t item = atomic_fetch_add(&hp->next, 1);+ int error, none = 0;++ if (item >= hp->items) {+ break;+ }+ error = hp->fn(hp->arg, item, worker);+ if (error != 0) {+ atomic_compare_exchange_strong(&hp->error, &none, error);+ }+ }+}++static void *+host_par_thread(void *p)+{+ struct host_par_arg *a = p;++ host_par_work(a->hp, a->worker);+ return NULL;+}++static int+host_parallel(void *ctx, uint32_t items, uint32_t workers,+ int (*fn)(void *arg, uint32_t item, uint32_t worker), void *arg)+{+ struct host_par hp;+ struct host_par_arg a[16];+ pthread_t th[16];+ uint32_t w, started = 1;++ (void)ctx;+ hp.fn = fn;+ hp.arg = arg;+ hp.items = items;+ atomic_init(&hp.next, 0);+ atomic_init(&hp.error, 0);+ for (w = 1; w < workers && w < 16; w++) {+ a[w].hp = &hp;+ a[w].worker = w;+ if (pthread_create(&th[w], NULL, host_par_thread, &a[w]) != 0) {+ break;+ }+ started++;+ }+ host_par_work(&hp, 0);+ for (w = 1; w < started; w++) {+ pthread_join(th[w], NULL);+ }+ return atomic_load(&hp.error);+}+static intopen_image(const char *path, struct apfs_nx *nx, struct apfs_vol *vol){@@ -84,6 +161,14 @@ open_image(const char *path, struct apfs_nx *nx, struct apfs_vol *vol)fprintf(stderr, "apfs-tool: cannot read the container: %s\n", strerror(error));return 1;}+ if (getenv("APFS_TOOL_THREADS") != NULL) {+ long t = strtol(getenv("APFS_TOOL_THREADS"), NULL, 0);++ if (t > 1) {+ nx->parallel = host_parallel;+ nx->parallel_max = (uint32_t)(t < 16 ? t : 16);+ }+ }/* the first volume that has a root directory, like apfs.py picks it */for (i = 0; i < APFS_NX_MAX_FILE_SYSTEMS; i++) {struct apfs_inode ino;@@ -500,6 +585,9 @@ cmd_census(const char *path, const char *where, bool meta_only)fprintf(stderr, "apfs-tool: inflate %" PRIu64 " calls, %" PRIu64 " bytes in, %" PRIu64" bytes out, %" PRIu64 " by zlib\n", nx.inflate_calls, nx.inflate_in, nx.inflate_out,nx.inflate_fallbacks);+ fprintf(stderr, "apfs-tool: %" PRIu64 " requests of whole pieces, %" PRIu64 " pieces in "+ "them, on %u threads at most\n", nx.batch_calls, nx.batch_pieces,+ nx.parallel != NULL ? nx.parallel_max : 1);fprintf(stderr, "apfs-tool: highest block touched %" PRIu64 " (%.2f GB in), of which ""file contents reach %" PRIu64 " (%.2f GB in)\n", nx.max_addr,nx.max_addr * (double)nx.block_size / 1e9, nx.max_data_addr,diff --git a/tools/apfs/build-tool.sh b/tools/apfs/build-tool.shindex e969378..076468e 100755--- a/tools/apfs/build-tool.sh+++ b/tools/apfs/build-tool.sh@@ -18,11 +18,13 @@ done[ -n "$SRC" ] || { echo "build-tool.sh: apfs_lib.c is in neither the kernel tree nor xnu/files" >&2; exit 1; }mkdir -p "$(dirname "$OUT")"+# TOOL_CFLAGS: more for the compiler, e.g. "-fsanitize=thread -g" with APFS_TOOL_THREADS+TOOL_CFLAGS=${TOOL_CFLAGS:-}OBJ=$(mktemp -d)trap 'rm -rf "$OBJ"' EXIT# (libdeflate as released compares signed with unsigned in a few places; -Wextra says so)-cc -std=c11 -O2 -Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -I"$HERE/host-shim" -c -o "$OBJ/apfs_deflate.o" \+cc -std=c11 -O2 -Wall -Wextra -Wno-sign-compare -Wno-unused-parameter $TOOL_CFLAGS -I"$HERE/host-shim" -c -o "$OBJ/apfs_deflate.o" \"$SRC/apfs_deflate.c"cc -std=c11 -D_DEFAULT_SOURCE -O2 -Wall -Wextra -Wno-unused-parameter -I"$SRC" -I"$HERE" \- -o "$OUT" "$HERE/apfs-tool.c" "$SRC/apfs_lib.c" "$OBJ/apfs_deflate.o" -lz+ $TOOL_CFLAGS -o "$OUT" "$HERE/apfs-tool.c" "$SRC/apfs_lib.c" "$OBJ/apfs_deflate.o" -lz -pthreadecho "build-tool.sh: $OUT (from $SRC)"diff --git a/xnu/files/bsd/dev/redwood_apfs_check.c b/xnu/files/bsd/dev/redwood_apfs_check.cindex 573a4a8..7a18629 100644--- a/xnu/files/bsd/dev/redwood_apfs_check.c+++ b/xnu/files/bsd/dev/redwood_apfs_check.c@@ -102,7 +102,28 @@ static const char *volatile watch_phase = "";#define FNV_INIT 0xcbf29ce484222325ULL#define FNV_PRIME 0x100000001b3ULL-#define READ_CHUNK (64 * 1024)+/*+ * How much each read asks for: 64 KB, one compressed piece, unless boot-arg apfs_chunk says+ * otherwise (up to 4 MB) -- a bigger read is one request whose pieces the driver can+ * decompress on several processors at once.+ */+static uint32_t+read_chunk(void)+{+ static uint32_t chunk;++ if (chunk == 0) {+ uint32_t want = 64 * 1024;++ PE_parse_boot_argn("apfs_chunk", &want, sizeof(want));+ if (want < 4096 || want > 4 * 1024 * 1024) {+ want = 64 * 1024;+ }+ chunk = want;+ }+ return chunk;+}+#define READ_CHUNK read_chunk()#define DIR_CHUNK 4096#define MAX_DEPTH 48#define PATH_CAP 2048diff --git a/xnu/files/bsd/miscfs/apfs/apfs.h b/xnu/files/bsd/miscfs/apfs/apfs.hindex f6035ff..740d5d4 100644--- a/xnu/files/bsd/miscfs/apfs/apfs.h+++ b/xnu/files/bsd/miscfs/apfs/apfs.h@@ -28,8 +28,20 @@struct apfs_node;-#define APFS_IOBUF 65536 /* what one round of vnop_read reads */-#define APFS_IOBUF_KEEP 8+/*+ * What one round of vnop_read reads. Alone on the volume, am_iobuf_size (boot-arg+ * apfs_round): a read(2) of that much is one request to apfs_lib, whose whole compressed+ * pieces are decompressed on several processors -- the bigger the round, the more there is+ * to share out. With other reads in flight, APFS_ROUND_SHARED: the processors are busy+ * anyway, and a round that stays in the cache until uiomove copies it out is worth more.+ * One reader with 1 MB reads over the restore ramdisk, rounds of 128 KB / 256 KB / 512 KB /+ * 1 MB: 867 / 663 / 612 / 561 ms; four readers: 1429 / 1534 / 1747 / 2009 ms, and 1327+ * with rounds of 64 or 128 KB and no helpers.+ */+#define APFS_IOBUF_DEFAULT (1024 * 1024)+#define APFS_IOBUF_MAX (2 * 1024 * 1024)+#define APFS_ROUND_SHARED (128 * 1024)+#define APFS_IOBUF_KEEP 8struct apfs_mount {mount_t am_mp;@@ -57,11 +69,12 @@ struct apfs_mount {uint64_t am_lock_held_ticks;uint64_t am_lock_t0; /* when the holder took it; only the holder writes *//*- * vnop_read's 64 KB buffers, kept for the next read rather than allocated and freed- * for every one (at that size the kernel goes to the VM). Under am_lock.+ * vnop_read's buffers, kept for the next read rather than allocated and freed for+ * every one (at that size the kernel goes to the VM). Under am_lock.*/void *am_iobuf[APFS_IOBUF_KEEP];uint32_t am_iobuf_kept;+ uint32_t am_iobuf_size;};/* The private part of a vnode: which object it is. */diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.c b/xnu/files/bsd/miscfs/apfs/apfs_lib.cindex 1c88457..c6f3521 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.c@@ -2246,6 +2246,24 @@ out:#define APFS_PIECE_CAP (APFS_DECMPFS_BLOCK_SIZE + 1024) /* stored: 1 + 64 KB; zlib: a few bytes more */#define APFS_SCRATCH_KEEP 16+/*+ * A whole piece of a request, planned with the lock and lying in one run on the device:+ * what one thread needs to read it and decompress it into the caller's buffer, with no+ * lock and nothing shared. Up to APFS_BATCH of them are done together, on as many+ * threads as nx->parallel brings -- one reader's request of many pieces no longer waits+ * on one processor while the others idle.+ */+struct apfs_piece_job {+ uint64_t addr; /* the first block the piece lies in */+ uint32_t skew; /* where in that block it starts */+ uint32_t plen; /* compressed */+ uint8_t *dst; /* the caller's, `want` bytes */+ size_t want;+};++#define APFS_BATCH 32+#define APFS_PAR_MAX 8+struct apfs_scratch {struct apfs_scratch *next;struct libdeflate_decompressor *dec;@@ -2257,6 +2275,7 @@ struct apfs_scratch {*/uint8_t *raw;size_t raw_cap;+ struct apfs_piece_job *jobs; /* APFS_BATCH, made when first wanted */};static void@@ -2274,6 +2293,9 @@ scratch_free(struct apfs_scratch *sc)if (sc->raw != NULL) {apfs_free(sc->raw, sc->raw_cap);}+ if (sc->jobs != NULL) {+ apfs_free(sc->jobs, APFS_BATCH * sizeof(*sc->jobs));+ }apfs_free(sc, sizeof(*sc));}@@ -2327,6 +2349,118 @@ scratch_drain(struct apfs_nx *nx)nx->scratch_kept = 0;}+/* One batch on its way: the jobs, a scratch for each worker, what each worker counted. */+struct apfs_batch {+ struct apfs_nx *nx;+ struct apfs_piece_job *jobs;+ struct apfs_scratch *sc[APFS_PAR_MAX];+ struct {+ uint64_t calls, bytes, data_ticks, inflate_ticks, out, fallbacks;+ } w[APFS_PAR_MAX];+};++static int+batch_one(void *arg, uint32_t item, uint32_t worker)+{+ struct apfs_batch *b = arg;+ struct apfs_piece_job *j = &b->jobs[item];+ struct apfs_scratch *sc = b->sc[worker];+ uint32_t bs = b->nx->block_size;+ size_t span = ((size_t)j->skew + j->plen + bs - 1) / bs * bs;+ size_t got = 0;+ bool fell_back = false;+ uint64_t t0, t1;+ int error;++ t0 = apfs_ticks();+ error = dev_read_data(b->nx, j->addr, sc->raw, span, &b->w[worker].calls,+ &b->w[worker].bytes);+ t1 = apfs_ticks();+ b->w[worker].data_ticks += t1 - t0;+ if (error) {+ return error;+ }+ error = apfs_inflate_fast(sc->dec, sc->raw + j->skew, j->plen, j->dst, j->want, &got,+ &fell_back);+ b->w[worker].inflate_ticks += apfs_ticks() - t1;+ b->w[worker].out += got;+ if (fell_back) {+ b->w[worker].fallbacks++;+ }+ if (error == 0 && got != j->want) {+ error = EIO;+ }+ return error;+}++/*+ * Do the `n` jobs gathered in sc->jobs: let go of the lock, spread them over the threads+ * nx->parallel brings (one scratch each, sc being the caller's), take the lock back and+ * add up what the workers counted. Inflate time is then the sum over the workers.+ */+static int+batch_run(struct apfs_vol *vol, struct apfs_scratch *sc, uint32_t n)+{+ struct apfs_nx *nx = vol->nx;+ struct apfs_batch b;+ uint32_t workers = 1, w, i;+ uint64_t in = 0;+ int error = 0;++ memset(&b, 0, sizeof(b));+ b.nx = nx;+ b.jobs = sc->jobs;+ b.sc[0] = sc;+ if (nx->parallel != NULL && n > 1) {+ /* the threads shared out among the requests in flight: all of them to one alone */+ workers = nx->requests > 1 ? nx->parallel_max / nx->requests : nx->parallel_max;+ if (workers < 1) {+ workers = 1;+ }+ if (workers > n) {+ workers = n;+ }+ if (workers > APFS_PAR_MAX) {+ workers = APFS_PAR_MAX;+ }+ for (w = 1; w < workers; w++) {+ b.sc[w] = scratch_get(nx);+ if (b.sc[w] == NULL) {+ break;+ }+ }+ workers = w;+ }+ nx_unlock(nx);+ if (workers > 1) {+ error = nx->parallel(nx->parallel_ctx, n, workers, batch_one, &b);+ } else {+ for (i = 0; i < n && error == 0; i++) {+ error = batch_one(&b, i, 0);+ }+ }+ nx_relock(nx);+ for (i = 0; i < n; i++) {+ in += sc->jobs[i].plen;+ }+ for (w = 0; w < workers; w++) {+ nx->data_calls += b.w[w].calls;+ nx->data_bytes += b.w[w].bytes;+ nx->data_ticks += b.w[w].data_ticks;+ nx->inflate_ticks += b.w[w].inflate_ticks;+ nx->inflate_out += b.w[w].out;+ nx->inflate_fallbacks += b.w[w].fallbacks;+ if (w > 0) {+ scratch_put(nx, b.sc[w]);+ }+ }+ nx->inflate_calls += n;+ nx->inflate_in += in;+ nx->batch_calls++;+ nx->batch_pieces += n;+ return error;+}+static intread_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,struct apfs_cfile *cf, uint64_t off, uint8_t *buf, size_t len, size_t *out)@@ -2335,6 +2469,7 @@ read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,uint8_t *big = NULL; /* a piece bigger than APFS_PIECE_CAP, should there be one */size_t big_cap = 0;uint32_t bs = vol->nx->block_size;+ uint32_t nbatch = 0; /* whole pieces gathered in sc->jobs, not yet done */uint64_t first, last, i;int error = 0;@@ -2430,6 +2565,34 @@ read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,goto out;}}+ if (direct && planned && nruns == 1 && runs[0].at == 0 &&+ runs[0].len == plen) {+ /* its place in the caller's buffer is kept; batch_run fills it */+ struct apfs_piece_job *j;++ if (sc->jobs == NULL) {+ sc->jobs = apfs_alloc(APFS_BATCH * sizeof(*sc->jobs));+ if (sc->jobs == NULL) {+ error = ENOMEM;+ goto out;+ }+ }+ j = &sc->jobs[nbatch++];+ j->addr = runs[0].addr;+ j->skew = runs[0].skew;+ j->plen = plen;+ j->dst = buf + *out;+ j->want = want;+ *out += want;+ if (nbatch == APFS_BATCH) {+ error = batch_run(vol, sc, nbatch);+ nbatch = 0;+ if (error) {+ goto out;+ }+ }+ continue;+ }if (!raw_type && !direct && sc->mine == NULL) {sc->mine = apfs_alloc(APFS_DECMPFS_BLOCK_SIZE);if (sc->mine == NULL) {@@ -2520,6 +2683,9 @@ read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,memcpy(buf + *out, cf->held_data + from_in_piece, n);*out += n;}+ if (nbatch > 0) {+ error = batch_run(vol, sc, nbatch);+ }out:if (big != NULL) {apfs_free(big, big_cap);diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.h b/xnu/files/bsd/miscfs/apfs/apfs_lib.hindex 6605a3a..34e918b 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.h+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.h@@ -169,6 +169,26 @@ struct apfs_nx {*/struct apfs_scratch *scratch;uint32_t scratch_kept;+ /*+ * Several threads for one request: run fn(arg, item, worker) for every item below+ * `items`, on up to `workers` threads at once, the caller's among them, each with a+ * worker number below `workers`; return the first error any item gave. Called+ * without the lock, and fn takes none. NULL (the program on the build machine): the+ * items are done one after another on the caller's thread.+ */+ int (*parallel)(void *ctx, uint32_t items, uint32_t workers,+ int (*fn)(void *arg, uint32_t item, uint32_t worker), void *arg);+ void *parallel_ctx;+ uint32_t parallel_max; /* threads it can bring, the caller's included */+ /*+ * Reads in flight on the container, kept by the caller with the lock held. The+ * threads nx->parallel brings are shared out among them: all of them to a request+ * that is alone, none to one among parallel_max or more -- the processors are busy+ * already, and on the phone four readers came out 7% slower with helpers than+ * without. 0 (the program on the build machine) counts as alone.+ */+ uint32_t requests;+ uint64_t batch_calls, batch_pieces; /* how many requests used it, for how much */};/* A volume inside the container. */diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.cindex fbb717f..4439bba 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c@@ -19,6 +19,11 @@#include <sys/sysctl.h>#include <sys/vnode_internal.h>#include <sys/fcntl.h>+#include <sys/proc.h>+#include <kern/thread.h>+#include <libkern/OSAtomic.h>+#include <machine/machine_routines.h>+#include <pexpert/pexpert.h>static LCK_GRP_DECLARE(apfs_lck_grp, "apfs");@@ -243,6 +248,10 @@ apfs_stats_print(void)(unsigned long long)apfs_ticks_ms(nx->inflate_ticks),(unsigned long long)nx->inflate_in, (unsigned long long)nx->inflate_out,(unsigned long long)nx->inflate_fallbacks);+ /* requests whose whole pieces went out together (nx->parallel), and how many pieces */+ printf("apfs: batch %llu requests %llu pieces %u threads at most\n",+ (unsigned long long)nx->batch_calls, (unsigned long long)nx->batch_pieces,+ nx->parallel != NULL ? nx->parallel_max : 1);printf("apfs: blocks %llu read %llu cache hits highest block %llu\n",(unsigned long long)nx->reads, (unsigned long long)nx->hits,(unsigned long long)nx->max_addr);@@ -395,13 +404,168 @@ apfs_lib_relock(void *ctx)apfs_lock((struct apfs_mount *)ctx);}+/* ------------------------------------------------------- threads for one request */++/*+ * apfs_lib's nx->parallel: a few kernel threads that help a request of many compressed+ * pieces along, each reading and decompressing whole pieces into the caller's buffer while+ * the caller does the same. A request posts a job and wakes as many helpers as it can+ * use; a helper that comes takes a worker number and then items until none are left. The+ * caller works through items too, then takes its job off the list -- no helper can join+ * after that -- and waits for the helpers still inside it. The job lives on the caller's+ * stack, which is why nothing may touch it once the caller is back.+ *+ * Boot-arg apfs_par=N: at most N threads on one request, the caller's included (default:+ * four, or the processors if fewer -- see apfs_pool_start(); 1 turns the helpers off).+ */+struct apfs_pjob {+ struct apfs_pjob *pj_next;+ int (*pj_fn)(void *arg, uint32_t item, uint32_t worker);+ void *pj_arg;+ uint32_t pj_items;+ uint32_t pj_workers;+ volatile SInt32 pj_taken; /* items handed out so far (may run past pj_items) */+ volatile SInt32 pj_error;+ uint32_t pj_joined; /* worker numbers given out; under apfs_pool_lock */+ uint32_t pj_active; /* helpers inside the job now; under apfs_pool_lock */+};++static LCK_MTX_DECLARE(apfs_pool_lock, &apfs_lck_grp);+static struct apfs_pjob *apfs_pool_jobs; /* oldest first */+static uint32_t apfs_pool_threads;+static uint32_t apfs_par_max; /* threads on one request, the caller's included */++static void+apfs_pjob_work(struct apfs_pjob *pj, uint32_t worker)+{+ for (;;) {+ SInt32 item = OSIncrementAtomic(&pj->pj_taken);+ int error;++ if (item < 0 || (uint32_t)item >= pj->pj_items) {+ break;+ }+ error = pj->pj_fn(pj->pj_arg, (uint32_t)item, worker);+ if (error != 0) {+ OSCompareAndSwap(0, (UInt32)error, (volatile UInt32 *)&pj->pj_error);+ }+ }+}++static void+apfs_pool_thread(__unused void *arg, __unused wait_result_t wr)+{+ lck_mtx_lock(&apfs_pool_lock);+ for (;;) {+ struct apfs_pjob *pj;+ uint32_t worker;++ for (pj = apfs_pool_jobs; pj != NULL; pj = pj->pj_next) {+ if (pj->pj_joined < pj->pj_workers &&+ (uint32_t)pj->pj_taken < pj->pj_items) {+ break;+ }+ }+ if (pj == NULL) {+ (void)msleep(&apfs_pool_jobs, &apfs_pool_lock, PRIBIO, "apfs_pool", NULL);+ continue;+ }+ worker = pj->pj_joined++;+ pj->pj_active++;+ lck_mtx_unlock(&apfs_pool_lock);+ apfs_pjob_work(pj, worker);+ lck_mtx_lock(&apfs_pool_lock);+ if (--pj->pj_active == 0) {+ wakeup(&pj->pj_active);+ }+ }+}++static int+apfs_parallel(__unused void *ctx, uint32_t items, uint32_t workers,+ int (*fn)(void *arg, uint32_t item, uint32_t worker), void *arg)+{+ struct apfs_pjob pj, **pp;+ uint32_t i;++ bzero(&pj, sizeof(pj));+ pj.pj_fn = fn;+ pj.pj_arg = arg;+ pj.pj_items = items;+ pj.pj_workers = workers;+ pj.pj_joined = 1; /* the caller is worker 0 */++ lck_mtx_lock(&apfs_pool_lock);+ for (pp = &apfs_pool_jobs; *pp != NULL; pp = &(*pp)->pj_next) {+ }+ *pp = &pj;+ for (i = 1; i < workers; i++) {+ wakeup_one((caddr_t)&apfs_pool_jobs);+ }+ lck_mtx_unlock(&apfs_pool_lock);++ apfs_pjob_work(&pj, 0);++ lck_mtx_lock(&apfs_pool_lock);+ for (pp = &apfs_pool_jobs; *pp != &pj; pp = &(*pp)->pj_next) {+ }+ *pp = pj.pj_next;+ while (pj.pj_active > 0) {+ (void)msleep(&pj.pj_active, &apfs_pool_lock, PRIBIO, "apfs_pjob", NULL);+ }+ lck_mtx_unlock(&apfs_pool_lock);+ return pj.pj_error;+}++/*+ * The helpers, once, at the first mount. Four threads on one request at most unless+ * apfs_par says otherwise: on this phone one reader with 1 MB reads took 765 ms over the+ * restore ramdisk with two, 612 with four or five, 714 with all eight -- the four small+ * cores take two and a half times as long over a piece, and a request waits for its+ * slowest one.+ */+static void+apfs_pool_start(void)+{+ uint32_t want = ml_get_topology_info()->num_cpus, i;++ if (want > 4) {+ want = 4;+ }+ if (want < 1) {+ want = 1;+ }+ PE_parse_boot_argn("apfs_par", &want, sizeof(want));+ if (want < 1) {+ want = 1;+ }+ lck_mtx_lock(&apfs_pool_lock);+ if (apfs_par_max != 0) {+ lck_mtx_unlock(&apfs_pool_lock);+ return;+ }+ apfs_par_max = want;+ lck_mtx_unlock(&apfs_pool_lock);+ for (i = 0; i + 1 < want; i++) {+ thread_t th;++ if (kernel_thread_start(apfs_pool_thread, NULL, &th) != KERN_SUCCESS) {+ break;+ }+ thread_deallocate(th);+ OSIncrementAtomic((volatile SInt32 *)&apfs_pool_threads);+ }+ printf("apfs: %u threads on a request of many pieces (%u helpers)\n", want,+ apfs_pool_threads);+}+/* ------------------------------------------------------------------ mount */static voidapfs_free_mount(struct apfs_mount *am){while (am->am_iobuf_kept > 0) {- kfree_data(am->am_iobuf[--am->am_iobuf_kept], APFS_IOBUF);+ kfree_data(am->am_iobuf[--am->am_iobuf_kept], am->am_iobuf_size);}apfs_nx_close(&am->am_nx);lck_mtx_destroy(&am->am_lock, &apfs_lck_grp);@@ -467,6 +631,20 @@ apfs_mount_common(mount_t mp, vnode_t devvp, vfs_context_t ctx)am->am_nx.unlock = apfs_lib_unlock;am->am_nx.relock = apfs_lib_relock;am->am_nx.lock_ctx = am;+ am->am_iobuf_size = APFS_IOBUF_DEFAULT;+ {+ uint32_t round = 0;++ if (PE_parse_boot_argn("apfs_round", &round, sizeof(round)) &&+ round >= 64 * 1024 && round <= APFS_IOBUF_MAX && (round % (64 * 1024)) == 0) {+ am->am_iobuf_size = round;+ }+ }+ apfs_pool_start();+ if (apfs_pool_threads > 0) {+ am->am_nx.parallel = apfs_parallel;+ am->am_nx.parallel_max = apfs_pool_threads + 1;+ }/* the first volume that has a root directory */{diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vnops.c b/xnu/files/bsd/miscfs/apfs/apfs_vnops.cindex a0c44b7..654aa2a 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vnops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vnops.c@@ -185,15 +185,18 @@ apfs_vnop_read(struct vnop_read_args *ap)buf = am->am_iobuf[--am->am_iobuf_kept];} else {apfs_unlock(am);- buf = kalloc_data(APFS_IOBUF, Z_WAITOK);+ buf = kalloc_data(am->am_iobuf_size, Z_WAITOK);if (buf == NULL) {return ENOMEM;}apfs_lock(am);}/* the lock is held at the top of every round and let go of only for uiomove */+ am->am_nx.requests++;while (uio_resid(uio) > 0 && (uint64_t)uio_offset(uio) < size) {- size_t want = (size_t)MIN((user_ssize_t)APFS_IOBUF, uio_resid(uio));+ size_t round = (am->am_nx.requests <= 1)+ ? am->am_iobuf_size : MIN(am->am_iobuf_size, APFS_ROUND_SHARED);+ size_t want = (size_t)MIN((user_ssize_t)round, uio_resid(uio));size_t got = 0;error = apfs_read_file(&am->am_vol, &ino, &APFS_VP(vp)->an_cf,@@ -209,6 +212,7 @@ apfs_vnop_read(struct vnop_read_args *ap)}read_bytes += got;}+ am->am_nx.requests--;if (am->am_iobuf_kept < APFS_IOBUF_KEEP) {am->am_iobuf[am->am_iobuf_kept++] = buf;buf = NULL;@@ -218,7 +222,7 @@ apfs_vnop_read(struct vnop_read_args *ap)am->am_read_bytes += read_bytes;apfs_unlock(am);if (buf != NULL) {- kfree_data(buf, APFS_IOBUF);+ kfree_data(buf, am->am_iobuf_size);}return error;}@@ -289,8 +293,10 @@ apfs_vnop_pagein(struct vnop_pagein_args *ap)}want = (size_t)MIN(end - at, (off_t)(size - (uint64_t)at));apfs_lock(am);+ am->am_nx.requests++;error = apfs_read_file(&am->am_vol, &ino, &APFS_VP(vp)->an_cf, (uint64_t)at,dst, want, &got);+ am->am_nx.requests--;apfs_unlock(am);if (error) {break;