]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/txg.c
Implement memory and CPU hotplug
[mirror_zfs.git] / module / zfs / txg.c
index b3e537f459c4b1822cda3afc4996f4ed33ad03b3..3efd261550149df55e8bd487a4e7224a7ba3747b 100644 (file)
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Portions Copyright 2011 Martin Matuska
- * Copyright (c) 2013 by Delphix. All rights reserved.
+ * Copyright (c) 2012, 2019 by Delphix. All rights reserved.
  */
 
 #include <sys/zfs_context.h>
 #include <sys/txg_impl.h>
 #include <sys/dmu_impl.h>
+#include <sys/spa_impl.h>
 #include <sys/dmu_tx.h>
 #include <sys/dsl_pool.h>
 #include <sys/dsl_scan.h>
+#include <sys/zil.h>
 #include <sys/callb.h>
-#include <sys/spa_impl.h>
+#include <sys/trace_zfs.h>
 
 /*
  * ZFS Transaction Groups
@@ -46,7 +48,7 @@
  * either be processing, or blocked waiting to enter the next state. There may
  * be up to three active txgs, and there is always a txg in the open state
  * (though it may be blocked waiting to enter the quiescing state). In broad
- * strokes, transactions — operations that change in-memory structures — are
+ * strokes, transactions -- operations that change in-memory structures -- are
  * accepted into the txg in the open state, and are completed while the txg is
  * in the open or quiescing states. The accumulated changes are written to
  * disk in the syncing state.
@@ -54,7 +56,7 @@
  * Open
  *
  * When a new txg becomes active, it first enters the open state. New
- * transactions — updates to in-memory structures — are assigned to the
+ * transactions -- updates to in-memory structures -- are assigned to the
  * currently open txg. There is always a txg in the open state so that ZFS can
  * accept new changes (though the txg may refuse new changes if it has hit
  * some limit). ZFS advances the open txg to the next state for a variety of
  * now transition to the syncing state.
  */
 
-static void txg_sync_thread(dsl_pool_t *dp);
-static void txg_quiesce_thread(dsl_pool_t *dp);
+static void txg_sync_thread(void *arg);
+static void txg_quiesce_thread(void *arg);
 
 int zfs_txg_timeout = 5;       /* max seconds worth of delta per txg */
 
@@ -127,6 +129,8 @@ txg_init(dsl_pool_t *dp, uint64_t txg)
                int i;
 
                mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
+               mutex_init(&tx->tx_cpu[c].tc_open_lock, NULL, MUTEX_NOLOCKDEP,
+                   NULL);
                for (i = 0; i < TXG_SIZE; i++) {
                        cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
                            NULL);
@@ -156,7 +160,7 @@ txg_fini(dsl_pool_t *dp)
        tx_state_t *tx = &dp->dp_tx;
        int c;
 
-       ASSERT(tx->tx_threads == 0);
+       ASSERT0(tx->tx_threads);
 
        mutex_destroy(&tx->tx_sync_lock);
 
@@ -169,6 +173,7 @@ txg_fini(dsl_pool_t *dp)
        for (c = 0; c < max_ncpus; c++) {
                int i;
 
+               mutex_destroy(&tx->tx_cpu[c].tc_open_lock);
                mutex_destroy(&tx->tx_cpu[c].tc_lock);
                for (i = 0; i < TXG_SIZE; i++) {
                        cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
@@ -196,20 +201,20 @@ txg_sync_start(dsl_pool_t *dp)
 
        dprintf("pool %p\n", dp);
 
-       ASSERT(tx->tx_threads == 0);
+       ASSERT0(tx->tx_threads);
 
        tx->tx_threads = 2;
 
        tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
-           dp, 0, &p0, TS_RUN, minclsyspri);
+           dp, 0, &p0, TS_RUN, defclsyspri);
 
        /*
         * The sync thread can need a larger-than-default stack size on
         * 32-bit x86.  This is due in part to nested pools and
         * scrub_visitbp() recursion.
         */
-       tx->tx_sync_thread = thread_create(NULL, 32<<10, txg_sync_thread,
-           dp, 0, &p0, TS_RUN, minclsyspri);
+       tx->tx_sync_thread = thread_create(NULL, 0, txg_sync_thread,
+           dp, 0, &p0, TS_RUN, defclsyspri);
 
        mutex_exit(&tx->tx_sync_lock);
 }
@@ -233,15 +238,16 @@ txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
 }
 
 static void
-txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, uint64_t time)
+txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, clock_t time)
 {
        CALLB_CPR_SAFE_BEGIN(cpr);
 
-       if (time)
-               (void) cv_timedwait_interruptible(cv, &tx->tx_sync_lock,
+       if (time) {
+               (void) cv_timedwait_idle(cv, &tx->tx_sync_lock,
                    ddi_get_lbolt() + time);
-       else
-               cv_wait_interruptible(cv, &tx->tx_sync_lock);
+       } else {
+               cv_wait_idle(cv, &tx->tx_sync_lock);
+       }
 
        CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
 }
@@ -258,10 +264,10 @@ txg_sync_stop(dsl_pool_t *dp)
        /*
         * Finish off any work in progress.
         */
-       ASSERT(tx->tx_threads == 2);
+       ASSERT3U(tx->tx_threads, ==, 2);
 
        /*
-        * We need to ensure that we've vacated the deferred space_maps.
+        * We need to ensure that we've vacated the deferred metaslab trees.
         */
        txg_wait_synced(dp, tx->tx_open_txg + TXG_DEFER_SIZE);
 
@@ -270,7 +276,7 @@ txg_sync_stop(dsl_pool_t *dp)
         */
        mutex_enter(&tx->tx_sync_lock);
 
-       ASSERT(tx->tx_threads == 2);
+       ASSERT3U(tx->tx_threads, ==, 2);
 
        tx->tx_exiting = 1;
 
@@ -299,14 +305,14 @@ txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
         * significance to the chosen tx_cpu. Because.. Why not use
         * the current cpu to index into the array?
         */
-       kpreempt_disable();
-       tc = &tx->tx_cpu[CPU_SEQID];
-       kpreempt_enable();
-
-       mutex_enter(&tc->tc_lock);
+       tc = &tx->tx_cpu[CPU_SEQID_UNSTABLE];
 
+       mutex_enter(&tc->tc_open_lock);
        txg = tx->tx_open_txg;
+
+       mutex_enter(&tc->tc_lock);
        tc->tc_count[txg & TXG_MASK]++;
+       mutex_exit(&tc->tc_lock);
 
        th->th_cpu = tc;
        th->th_txg = txg;
@@ -319,7 +325,8 @@ txg_rele_to_quiesce(txg_handle_t *th)
 {
        tx_cpu_t *tc = th->th_cpu;
 
-       mutex_exit(&tc->tc_lock);
+       ASSERT(!MUTEX_HELD(&tc->tc_lock));
+       mutex_exit(&tc->tc_open_lock);
 }
 
 void
@@ -348,45 +355,46 @@ txg_rele_to_sync(txg_handle_t *th)
        th->th_cpu = NULL;      /* defensive */
 }
 
+/*
+ * Blocks until all transactions in the group are committed.
+ *
+ * On return, the transaction group has reached a stable state in which it can
+ * then be passed off to the syncing context.
+ */
 static void
 txg_quiesce(dsl_pool_t *dp, uint64_t txg)
 {
-       hrtime_t start;
-       txg_history_t *th;
        tx_state_t *tx = &dp->dp_tx;
+       uint64_t tx_open_time;
        int g = txg & TXG_MASK;
        int c;
 
        /*
-        * Grab all tx_cpu locks so nobody else can get into this txg.
+        * Grab all tc_open_locks so nobody else can get into this txg.
         */
        for (c = 0; c < max_ncpus; c++)
-               mutex_enter(&tx->tx_cpu[c].tc_lock);
+               mutex_enter(&tx->tx_cpu[c].tc_open_lock);
 
        ASSERT(txg == tx->tx_open_txg);
        tx->tx_open_txg++;
+       tx->tx_open_time = tx_open_time = gethrtime();
+
+       DTRACE_PROBE2(txg__quiescing, dsl_pool_t *, dp, uint64_t, txg);
+       DTRACE_PROBE2(txg__opened, dsl_pool_t *, dp, uint64_t, tx->tx_open_txg);
 
        /*
         * Now that we've incremented tx_open_txg, we can let threads
         * enter the next transaction group.
         */
        for (c = 0; c < max_ncpus; c++)
-               mutex_exit(&tx->tx_cpu[c].tc_lock);
+               mutex_exit(&tx->tx_cpu[c].tc_open_lock);
 
-       /*
-        * Measure how long the txg was open and replace the kstat.
-        */
-       th = dsl_pool_txg_history_get(dp, txg);
-       th->th_kstat.open_time = gethrtime() - th->th_kstat.birth;
-       th->th_kstat.state = TXG_STATE_QUIESCING;
-       dsl_pool_txg_history_put(th);
-       dsl_pool_txg_history_add(dp, tx->tx_open_txg);
+       spa_txg_history_set(dp->dp_spa, txg, TXG_STATE_OPEN, tx_open_time);
+       spa_txg_history_add(dp->dp_spa, txg + 1, tx_open_time);
 
        /*
         * Quiesce the transaction group by waiting for everyone to txg_exit().
         */
-       start = gethrtime();
-
        for (c = 0; c < max_ncpus; c++) {
                tx_cpu_t *tc = &tx->tx_cpu[c];
                mutex_enter(&tc->tc_lock);
@@ -395,12 +403,7 @@ txg_quiesce(dsl_pool_t *dp, uint64_t txg)
                mutex_exit(&tc->tc_lock);
        }
 
-       /*
-        * Measure how long the txg took to quiesce.
-        */
-       th = dsl_pool_txg_history_get(dp, txg);
-       th->th_kstat.quiesce_time = gethrtime() - start;
-       dsl_pool_txg_history_put(th);
+       spa_txg_history_set(dp->dp_spa, txg, TXG_STATE_QUIESCED, gethrtime());
 }
 
 static void
@@ -415,6 +418,9 @@ txg_do_callbacks(list_t *cb_list)
 
 /*
  * Dispatch the commit callbacks registered on this txg to worker threads.
+ *
+ * If no callbacks are registered for a given TXG, nothing happens.
+ * This function creates a taskq for the associated pool, if needed.
  */
 static void
 txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
@@ -425,7 +431,10 @@ txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
 
        for (c = 0; c < max_ncpus; c++) {
                tx_cpu_t *tc = &tx->tx_cpu[c];
-               /* No need to lock tx_cpu_t at this point */
+               /*
+                * No need to lock tx_cpu_t at this point, since this can
+                * only be called once a txg has been synced.
+                */
 
                int g = txg & TXG_MASK;
 
@@ -437,11 +446,12 @@ txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
                         * Commit callback taskq hasn't been created yet.
                         */
                        tx->tx_commit_cb_taskq = taskq_create("tx_commit_cb",
-                           100, minclsyspri, max_ncpus, INT_MAX,
-                           TASKQ_THREADS_CPU_PCT | TASKQ_PREPOPULATE);
+                           100, defclsyspri, boot_ncpus, boot_ncpus * 2,
+                           TASKQ_PREPOPULATE | TASKQ_DYNAMIC |
+                           TASKQ_THREADS_CPU_PCT);
                }
 
-               cb_list = kmem_alloc(sizeof (list_t), KM_PUSHPAGE);
+               cb_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
                list_create(cb_list, sizeof (dmu_tx_callback_t),
                    offsetof(dmu_tx_callback_t, dcb_node));
 
@@ -463,36 +473,52 @@ txg_wait_callbacks(dsl_pool_t *dp)
        tx_state_t *tx = &dp->dp_tx;
 
        if (tx->tx_commit_cb_taskq != NULL)
-               taskq_wait(tx->tx_commit_cb_taskq);
+               taskq_wait_outstanding(tx->tx_commit_cb_taskq, 0);
+}
+
+static boolean_t
+txg_is_syncing(dsl_pool_t *dp)
+{
+       tx_state_t *tx = &dp->dp_tx;
+       ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
+       return (tx->tx_syncing_txg != 0);
+}
+
+static boolean_t
+txg_is_quiescing(dsl_pool_t *dp)
+{
+       tx_state_t *tx = &dp->dp_tx;
+       ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
+       return (tx->tx_quiescing_txg != 0);
+}
+
+static boolean_t
+txg_has_quiesced_to_sync(dsl_pool_t *dp)
+{
+       tx_state_t *tx = &dp->dp_tx;
+       ASSERT(MUTEX_HELD(&tx->tx_sync_lock));
+       return (tx->tx_quiesced_txg != 0);
 }
 
 static void
-txg_sync_thread(dsl_pool_t *dp)
+txg_sync_thread(void *arg)
 {
+       dsl_pool_t *dp = arg;
        spa_t *spa = dp->dp_spa;
        tx_state_t *tx = &dp->dp_tx;
        callb_cpr_t cpr;
-       uint64_t start, delta;
-
-#ifdef _KERNEL
-       /*
-        * Annotate this process with a flag that indicates that it is
-        * unsafe to use KM_SLEEP during memory allocations due to the
-        * potential for a deadlock.  KM_PUSHPAGE should be used instead.
-        */
-       current->flags |= PF_NOFS;
-#endif /* _KERNEL */
+       clock_t start, delta;
 
+       (void) spl_fstrans_mark();
        txg_thread_enter(tx, &cpr);
 
        start = delta = 0;
        for (;;) {
-               hrtime_t hrstart;
-               txg_history_t *th;
-               uint64_t timer, timeout;
+               clock_t timeout = zfs_txg_timeout * hz;
+               clock_t timer;
                uint64_t txg;
-
-               timeout = zfs_txg_timeout * hz;
+               uint64_t dirty_min_bytes =
+                   zfs_dirty_data_max * zfs_dirty_data_sync_percent / 100;
 
                /*
                 * We sync when we're scanning, there's someone waiting
@@ -503,7 +529,8 @@ txg_sync_thread(dsl_pool_t *dp)
                while (!dsl_scan_active(dp->dp_scan) &&
                    !tx->tx_exiting && timer > 0 &&
                    tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
-                   tx->tx_quiesced_txg == 0) {
+                   !txg_has_quiesced_to_sync(dp) &&
+                   dp->dp_dirty_total < dirty_min_bytes) {
                        dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
                            tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
                        txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
@@ -515,7 +542,7 @@ txg_sync_thread(dsl_pool_t *dp)
                 * Wait until the quiesce thread hands off a txg to us,
                 * prompting it to do so if necessary.
                 */
-               while (!tx->tx_exiting && tx->tx_quiesced_txg == 0) {
+               while (!tx->tx_exiting && !txg_has_quiesced_to_sync(dp)) {
                        if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
                                tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
                        cv_broadcast(&tx->tx_quiesce_more_cv);
@@ -530,57 +557,40 @@ txg_sync_thread(dsl_pool_t *dp)
                 * us.  This may cause the quiescing thread to now be
                 * able to quiesce another txg, so we must signal it.
                 */
+               ASSERT(tx->tx_quiesced_txg != 0);
                txg = tx->tx_quiesced_txg;
                tx->tx_quiesced_txg = 0;
                tx->tx_syncing_txg = txg;
+               DTRACE_PROBE2(txg__syncing, dsl_pool_t *, dp, uint64_t, txg);
                cv_broadcast(&tx->tx_quiesce_more_cv);
 
-               th = dsl_pool_txg_history_get(dp, txg);
-               th->th_kstat.state = TXG_STATE_SYNCING;
-               vdev_get_stats(spa->spa_root_vdev, &th->th_vs1);
-               dsl_pool_txg_history_put(th);
-
                dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
                    txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
                mutex_exit(&tx->tx_sync_lock);
 
+               txg_stat_t *ts = spa_txg_history_init_io(spa, txg, dp);
                start = ddi_get_lbolt();
-               hrstart = gethrtime();
                spa_sync(spa, txg);
                delta = ddi_get_lbolt() - start;
+               spa_txg_history_fini_io(spa, ts);
 
                mutex_enter(&tx->tx_sync_lock);
                tx->tx_synced_txg = txg;
                tx->tx_syncing_txg = 0;
+               DTRACE_PROBE2(txg__synced, dsl_pool_t *, dp, uint64_t, txg);
                cv_broadcast(&tx->tx_sync_done_cv);
 
                /*
                 * Dispatch commit callbacks to worker threads.
                 */
                txg_dispatch_callbacks(dp, txg);
-
-               /*
-                * Measure the txg sync time determine the amount of I/O done.
-                */
-               th = dsl_pool_txg_history_get(dp, txg);
-               vdev_get_stats(spa->spa_root_vdev, &th->th_vs2);
-               th->th_kstat.sync_time = gethrtime() - hrstart;
-               th->th_kstat.nread = th->th_vs2.vs_bytes[ZIO_TYPE_READ] -
-                   th->th_vs1.vs_bytes[ZIO_TYPE_READ];
-               th->th_kstat.nwritten = th->th_vs2.vs_bytes[ZIO_TYPE_WRITE] -
-                   th->th_vs1.vs_bytes[ZIO_TYPE_WRITE];
-               th->th_kstat.reads = th->th_vs2.vs_ops[ZIO_TYPE_READ] -
-                   th->th_vs1.vs_ops[ZIO_TYPE_READ];
-               th->th_kstat.writes = th->th_vs2.vs_ops[ZIO_TYPE_WRITE] -
-                   th->th_vs1.vs_ops[ZIO_TYPE_WRITE];
-               th->th_kstat.state = TXG_STATE_COMMITTED;
-               dsl_pool_txg_history_put(th);
        }
 }
 
 static void
-txg_quiesce_thread(dsl_pool_t *dp)
+txg_quiesce_thread(void *arg)
 {
+       dsl_pool_t *dp = arg;
        tx_state_t *tx = &dp->dp_tx;
        callb_cpr_t cpr;
 
@@ -598,7 +608,7 @@ txg_quiesce_thread(dsl_pool_t *dp)
                 */
                while (!tx->tx_exiting &&
                    (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
-                   tx->tx_quiesced_txg != 0))
+                   txg_has_quiesced_to_sync(dp)))
                        txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
 
                if (tx->tx_exiting)
@@ -608,6 +618,8 @@ txg_quiesce_thread(dsl_pool_t *dp)
                dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
                    txg, tx->tx_quiesce_txg_waiting,
                    tx->tx_sync_txg_waiting);
+               tx->tx_quiescing_txg = txg;
+
                mutex_exit(&tx->tx_sync_lock);
                txg_quiesce(dp, txg);
                mutex_enter(&tx->tx_sync_lock);
@@ -616,24 +628,26 @@ txg_quiesce_thread(dsl_pool_t *dp)
                 * Hand this txg off to the sync thread.
                 */
                dprintf("quiesce done, handing off txg %llu\n", txg);
+               tx->tx_quiescing_txg = 0;
                tx->tx_quiesced_txg = txg;
+               DTRACE_PROBE2(txg__quiesced, dsl_pool_t *, dp, uint64_t, txg);
                cv_broadcast(&tx->tx_sync_more_cv);
                cv_broadcast(&tx->tx_quiesce_done_cv);
        }
 }
 
 /*
- * Delay this thread by 'ticks' if we are still in the open transaction
- * group and there is already a waiting txg quiesing or quiesced.  Abort
- * the delay if this txg stalls or enters the quiesing state.
+ * Delay this thread by delay nanoseconds if we are still in the open
+ * transaction group and there is already a waiting txg quiescing or quiesced.
+ * Abort the delay if this txg stalls or enters the quiescing state.
  */
 void
-txg_delay(dsl_pool_t *dp, uint64_t txg, int ticks)
+txg_delay(dsl_pool_t *dp, uint64_t txg, hrtime_t delay, hrtime_t resolution)
 {
        tx_state_t *tx = &dp->dp_tx;
-       clock_t timeout = ddi_get_lbolt() + ticks;
+       hrtime_t start = gethrtime();
 
-       /* don't delay if this txg could transition to quiesing immediately */
+       /* don't delay if this txg could transition to quiescing immediately */
        if (tx->tx_open_txg > txg ||
            tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
                return;
@@ -644,25 +658,26 @@ txg_delay(dsl_pool_t *dp, uint64_t txg, int ticks)
                return;
        }
 
-       while (ddi_get_lbolt() < timeout &&
-           tx->tx_syncing_txg < txg-1 && !txg_stalled(dp))
-               (void) cv_timedwait(&tx->tx_quiesce_more_cv, &tx->tx_sync_lock,
-                   timeout);
+       while (gethrtime() - start < delay &&
+           tx->tx_syncing_txg < txg-1 && !txg_stalled(dp)) {
+               (void) cv_timedwait_hires(&tx->tx_quiesce_more_cv,
+                   &tx->tx_sync_lock, delay, resolution, 0);
+       }
 
        DMU_TX_STAT_BUMP(dmu_tx_delay);
 
        mutex_exit(&tx->tx_sync_lock);
 }
 
-void
-txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
+static boolean_t
+txg_wait_synced_impl(dsl_pool_t *dp, uint64_t txg, boolean_t wait_sig)
 {
        tx_state_t *tx = &dp->dp_tx;
 
        ASSERT(!dsl_pool_config_held(dp));
 
        mutex_enter(&tx->tx_sync_lock);
-       ASSERT(tx->tx_threads == 2);
+       ASSERT3U(tx->tx_threads, ==, 2);
        if (txg == 0)
                txg = tx->tx_open_txg + TXG_DEFER_SIZE;
        if (tx->tx_sync_txg_waiting < txg)
@@ -671,32 +686,100 @@ txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
            txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
        while (tx->tx_synced_txg < txg) {
                dprintf("broadcasting sync more "
-                   "tx_synced=%llu waiting=%llu dp=%p\n",
+                   "tx_synced=%llu waiting=%llu dp=%px\n",
                    tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
                cv_broadcast(&tx->tx_sync_more_cv);
-               cv_wait(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
+               if (wait_sig) {
+                       /*
+                        * Condition wait here but stop if the thread receives a
+                        * signal. The caller may call txg_wait_synced*() again
+                        * to resume waiting for this txg.
+                        */
+                       if (cv_wait_io_sig(&tx->tx_sync_done_cv,
+                           &tx->tx_sync_lock) == 0) {
+                               mutex_exit(&tx->tx_sync_lock);
+                               return (B_TRUE);
+                       }
+               } else {
+                       cv_wait_io(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
+               }
        }
        mutex_exit(&tx->tx_sync_lock);
+       return (B_FALSE);
+}
+
+void
+txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
+{
+       VERIFY0(txg_wait_synced_impl(dp, txg, B_FALSE));
+}
+
+/*
+ * Similar to a txg_wait_synced but it can be interrupted from a signal.
+ * Returns B_TRUE if the thread was signaled while waiting.
+ */
+boolean_t
+txg_wait_synced_sig(dsl_pool_t *dp, uint64_t txg)
+{
+       return (txg_wait_synced_impl(dp, txg, B_TRUE));
 }
 
+/*
+ * Wait for the specified open transaction group.  Set should_quiesce
+ * when the current open txg should be quiesced immediately.
+ */
 void
-txg_wait_open(dsl_pool_t *dp, uint64_t txg)
+txg_wait_open(dsl_pool_t *dp, uint64_t txg, boolean_t should_quiesce)
 {
        tx_state_t *tx = &dp->dp_tx;
 
        ASSERT(!dsl_pool_config_held(dp));
 
        mutex_enter(&tx->tx_sync_lock);
-       ASSERT(tx->tx_threads == 2);
+       ASSERT3U(tx->tx_threads, ==, 2);
        if (txg == 0)
                txg = tx->tx_open_txg + 1;
-       if (tx->tx_quiesce_txg_waiting < txg)
+       if (tx->tx_quiesce_txg_waiting < txg && should_quiesce)
                tx->tx_quiesce_txg_waiting = txg;
        dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
            txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
        while (tx->tx_open_txg < txg) {
                cv_broadcast(&tx->tx_quiesce_more_cv);
-               cv_wait(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
+               /*
+                * Callers setting should_quiesce will use cv_wait_io() and
+                * be accounted for as iowait time.  Otherwise, the caller is
+                * understood to be idle and cv_wait_sig() is used to prevent
+                * incorrectly inflating the system load average.
+                */
+               if (should_quiesce == B_TRUE) {
+                       cv_wait_io(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
+               } else {
+                       cv_wait_idle(&tx->tx_quiesce_done_cv,
+                           &tx->tx_sync_lock);
+               }
+       }
+       mutex_exit(&tx->tx_sync_lock);
+}
+
+/*
+ * If there isn't a txg syncing or in the pipeline, push another txg through
+ * the pipeline by quiescing the open txg.
+ */
+void
+txg_kick(dsl_pool_t *dp)
+{
+       tx_state_t *tx = &dp->dp_tx;
+
+       ASSERT(!dsl_pool_config_held(dp));
+
+       mutex_enter(&tx->tx_sync_lock);
+       if (!txg_is_syncing(dp) &&
+           !txg_is_quiescing(dp) &&
+           tx->tx_quiesce_txg_waiting <= tx->tx_open_txg &&
+           tx->tx_sync_txg_waiting <= tx->tx_synced_txg &&
+           tx->tx_quiesced_txg <= tx->tx_synced_txg) {
+               tx->tx_quiesce_txg_waiting = tx->tx_open_txg + 1;
+               cv_broadcast(&tx->tx_quiesce_more_cv);
        }
        mutex_exit(&tx->tx_sync_lock);
 }
@@ -717,37 +800,89 @@ txg_sync_waiting(dsl_pool_t *dp)
            tx->tx_quiesced_txg != 0);
 }
 
+/*
+ * Verify that this txg is active (open, quiescing, syncing).  Non-active
+ * txg's should not be manipulated.
+ */
+#ifdef ZFS_DEBUG
+void
+txg_verify(spa_t *spa, uint64_t txg)
+{
+       dsl_pool_t *dp __maybe_unused = spa_get_dsl(spa);
+       if (txg <= TXG_INITIAL || txg == ZILTEST_TXG)
+               return;
+       ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
+       ASSERT3U(txg, >=, dp->dp_tx.tx_synced_txg);
+       ASSERT3U(txg, >=, dp->dp_tx.tx_open_txg - TXG_CONCURRENT_STATES);
+}
+#endif
+
 /*
  * Per-txg object lists.
  */
 void
-txg_list_create(txg_list_t *tl, size_t offset)
+txg_list_create(txg_list_t *tl, spa_t *spa, size_t offset)
 {
        int t;
 
        mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
 
        tl->tl_offset = offset;
+       tl->tl_spa = spa;
 
        for (t = 0; t < TXG_SIZE; t++)
                tl->tl_head[t] = NULL;
 }
 
+static boolean_t
+txg_list_empty_impl(txg_list_t *tl, uint64_t txg)
+{
+       ASSERT(MUTEX_HELD(&tl->tl_lock));
+       TXG_VERIFY(tl->tl_spa, txg);
+       return (tl->tl_head[txg & TXG_MASK] == NULL);
+}
+
+boolean_t
+txg_list_empty(txg_list_t *tl, uint64_t txg)
+{
+       mutex_enter(&tl->tl_lock);
+       boolean_t ret = txg_list_empty_impl(tl, txg);
+       mutex_exit(&tl->tl_lock);
+
+       return (ret);
+}
+
 void
 txg_list_destroy(txg_list_t *tl)
 {
        int t;
 
+       mutex_enter(&tl->tl_lock);
        for (t = 0; t < TXG_SIZE; t++)
-               ASSERT(txg_list_empty(tl, t));
+               ASSERT(txg_list_empty_impl(tl, t));
+       mutex_exit(&tl->tl_lock);
 
        mutex_destroy(&tl->tl_lock);
 }
 
+/*
+ * Returns true if all txg lists are empty.
+ *
+ * Warning: this is inherently racy (an item could be added immediately
+ * after this function returns).
+ */
 boolean_t
-txg_list_empty(txg_list_t *tl, uint64_t txg)
+txg_all_lists_empty(txg_list_t *tl)
 {
-       return (tl->tl_head[txg & TXG_MASK] == NULL);
+       mutex_enter(&tl->tl_lock);
+       for (int i = 0; i < TXG_SIZE; i++) {
+               if (!txg_list_empty_impl(tl, i)) {
+                       mutex_exit(&tl->tl_lock);
+                       return (B_FALSE);
+               }
+       }
+       mutex_exit(&tl->tl_lock);
+       return (B_TRUE);
 }
 
 /*
@@ -761,6 +896,7 @@ txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
        txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
        boolean_t add;
 
+       TXG_VERIFY(tl->tl_spa, txg);
        mutex_enter(&tl->tl_lock);
        add = (tn->tn_member[t] == 0);
        if (add) {
@@ -785,6 +921,7 @@ txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg)
        txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
        boolean_t add;
 
+       TXG_VERIFY(tl->tl_spa, txg);
        mutex_enter(&tl->tl_lock);
        add = (tn->tn_member[t] == 0);
        if (add) {
@@ -812,8 +949,11 @@ txg_list_remove(txg_list_t *tl, uint64_t txg)
        txg_node_t *tn;
        void *p = NULL;
 
+       TXG_VERIFY(tl->tl_spa, txg);
        mutex_enter(&tl->tl_lock);
        if ((tn = tl->tl_head[t]) != NULL) {
+               ASSERT(tn->tn_member[t]);
+               ASSERT(tn->tn_next[t] == NULL || tn->tn_next[t]->tn_member[t]);
                p = (char *)tn - tl->tl_offset;
                tl->tl_head[t] = tn->tn_next[t];
                tn->tn_next[t] = NULL;
@@ -833,6 +973,7 @@ txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
        int t = txg & TXG_MASK;
        txg_node_t *tn, **tp;
 
+       TXG_VERIFY(tl->tl_spa, txg);
        mutex_enter(&tl->tl_lock);
 
        for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
@@ -856,18 +997,24 @@ txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
        int t = txg & TXG_MASK;
        txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
 
+       TXG_VERIFY(tl->tl_spa, txg);
        return (tn->tn_member[t] != 0);
 }
 
 /*
- * Walk a txg list -- only safe if you know it's not changing.
+ * Walk a txg list
  */
 void *
 txg_list_head(txg_list_t *tl, uint64_t txg)
 {
        int t = txg & TXG_MASK;
-       txg_node_t *tn = tl->tl_head[t];
+       txg_node_t *tn;
 
+       mutex_enter(&tl->tl_lock);
+       tn = tl->tl_head[t];
+       mutex_exit(&tl->tl_lock);
+
+       TXG_VERIFY(tl->tl_spa, txg);
        return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
 }
 
@@ -877,12 +1024,15 @@ txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
        int t = txg & TXG_MASK;
        txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
 
+       TXG_VERIFY(tl->tl_spa, txg);
+
+       mutex_enter(&tl->tl_lock);
        tn = tn->tn_next[t];
+       mutex_exit(&tl->tl_lock);
 
        return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
 }
 
-#if defined(_KERNEL) && defined(HAVE_SPL)
 EXPORT_SYMBOL(txg_init);
 EXPORT_SYMBOL(txg_fini);
 EXPORT_SYMBOL(txg_sync_start);
@@ -898,6 +1048,7 @@ EXPORT_SYMBOL(txg_wait_callbacks);
 EXPORT_SYMBOL(txg_stalled);
 EXPORT_SYMBOL(txg_sync_waiting);
 
-module_param(zfs_txg_timeout, int, 0644);
-MODULE_PARM_DESC(zfs_txg_timeout, "Max seconds worth of delta per txg");
-#endif
+/* BEGIN CSTYLED */
+ZFS_MODULE_PARAM(zfs_txg, zfs_txg_, timeout, INT, ZMOD_RW,
+       "Max seconds worth of delta per txg");
+/* END CSTYLED */