Merge tag 'core-rcu-2020-10-12' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / kernel / rcu / tree.c
index f78ee75..06895ef 100644 (file)
 #endif
 #define MODULE_PARAM_PREFIX "rcutree."
 
-#ifndef data_race
-#define data_race(expr)                                                        \
-       ({                                                              \
-               expr;                                                   \
-       })
-#endif
-#ifndef ASSERT_EXCLUSIVE_WRITER
-#define ASSERT_EXCLUSIVE_WRITER(var) do { } while (0)
-#endif
-#ifndef ASSERT_EXCLUSIVE_ACCESS
-#define ASSERT_EXCLUSIVE_ACCESS(var) do { } while (0)
-#endif
-
 /* Data structures. */
 
 /*
@@ -178,6 +165,12 @@ module_param(gp_init_delay, int, 0444);
 static int gp_cleanup_delay;
 module_param(gp_cleanup_delay, int, 0444);
 
+// Add delay to rcu_read_unlock() for strict grace periods.
+static int rcu_unlock_delay;
+#ifdef CONFIG_RCU_STRICT_GRACE_PERIOD
+module_param(rcu_unlock_delay, int, 0444);
+#endif
+
 /*
  * This rcu parameter is runtime-read-only. It reflects
  * a minimum allowed number of objects which can be cached
@@ -468,24 +461,25 @@ static int rcu_is_cpu_rrupt_from_idle(void)
        return __this_cpu_read(rcu_data.dynticks_nesting) == 0;
 }
 
-#define DEFAULT_RCU_BLIMIT 10     /* Maximum callbacks per rcu_do_batch ... */
-#define DEFAULT_MAX_RCU_BLIMIT 10000 /* ... even during callback flood. */
+#define DEFAULT_RCU_BLIMIT (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) ? 1000 : 10)
+                               // Maximum callbacks per rcu_do_batch ...
+#define DEFAULT_MAX_RCU_BLIMIT 10000 // ... even during callback flood.
 static long blimit = DEFAULT_RCU_BLIMIT;
-#define DEFAULT_RCU_QHIMARK 10000 /* If this many pending, ignore blimit. */
+#define DEFAULT_RCU_QHIMARK 10000 // If this many pending, ignore blimit.
 static long qhimark = DEFAULT_RCU_QHIMARK;
-#define DEFAULT_RCU_QLOMARK 100   /* Once only this many pending, use blimit. */
+#define DEFAULT_RCU_QLOMARK 100   // Once only this many pending, use blimit.
 static long qlowmark = DEFAULT_RCU_QLOMARK;
 #define DEFAULT_RCU_QOVLD_MULT 2
 #define DEFAULT_RCU_QOVLD (DEFAULT_RCU_QOVLD_MULT * DEFAULT_RCU_QHIMARK)
-static long qovld = DEFAULT_RCU_QOVLD; /* If this many pending, hammer QS. */
-static long qovld_calc = -1;     /* No pre-initialization lock acquisitions! */
+static long qovld = DEFAULT_RCU_QOVLD; // If this many pending, hammer QS.
+static long qovld_calc = -1;     // No pre-initialization lock acquisitions!
 
 module_param(blimit, long, 0444);
 module_param(qhimark, long, 0444);
 module_param(qlowmark, long, 0444);
 module_param(qovld, long, 0444);
 
-static ulong jiffies_till_first_fqs = ULONG_MAX;
+static ulong jiffies_till_first_fqs = IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) ? 0 : ULONG_MAX;
 static ulong jiffies_till_next_fqs = ULONG_MAX;
 static bool rcu_kick_kthreads;
 static int rcu_divisor = 7;
@@ -1092,11 +1086,6 @@ static void rcu_disable_urgency_upon_qs(struct rcu_data *rdp)
        }
 }
 
-noinstr bool __rcu_is_watching(void)
-{
-       return !rcu_dynticks_curr_cpu_in_eqs();
-}
-
 /**
  * rcu_is_watching - see if RCU thinks that the current CPU is not idle
  *
@@ -1229,13 +1218,28 @@ static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
                return 1;
        }
 
-       /* If waiting too long on an offline CPU, complain. */
-       if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp)) &&
-           time_after(jiffies, rcu_state.gp_start + HZ)) {
+       /*
+        * Complain if a CPU that is considered to be offline from RCU's
+        * perspective has not yet reported a quiescent state.  After all,
+        * the offline CPU should have reported a quiescent state during
+        * the CPU-offline process, or, failing that, by rcu_gp_init()
+        * if it ran concurrently with either the CPU going offline or the
+        * last task on a leaf rcu_node structure exiting its RCU read-side
+        * critical section while all CPUs corresponding to that structure
+        * are offline.  This added warning detects bugs in any of these
+        * code paths.
+        *
+        * The rcu_node structure's ->lock is held here, which excludes
+        * the relevant portions the CPU-hotplug code, the grace-period
+        * initialization code, and the rcu_read_unlock() code paths.
+        *
+        * For more detail, please refer to the "Hotplug CPU" section
+        * of RCU's Requirements documentation.
+        */
+       if (WARN_ON_ONCE(!(rdp->grpmask & rcu_rnp_online_cpus(rnp)))) {
                bool onl;
                struct rcu_node *rnp1;
 
-               WARN_ON(1);  /* Offline CPUs are supposed to report QS! */
                pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
                        __func__, rnp->grplo, rnp->grphi, rnp->level,
                        (long)rnp->gp_seq, (long)rnp->completedqs);
@@ -1498,9 +1502,10 @@ static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
 
        /* Trace depending on how much we were able to accelerate. */
        if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
-               trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccWaitCB"));
+               trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccWaitCB"));
        else
-               trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccReadyCB"));
+               trace_rcu_grace_period(rcu_state.name, gp_seq_req, TPS("AccReadyCB"));
+
        return ret;
 }
 
@@ -1575,6 +1580,19 @@ static void __maybe_unused rcu_advance_cbs_nowake(struct rcu_node *rnp,
        raw_spin_unlock_rcu_node(rnp);
 }
 
+/*
+ * In CONFIG_RCU_STRICT_GRACE_PERIOD=y kernels, attempt to generate a
+ * quiescent state.  This is intended to be invoked when the CPU notices
+ * a new grace period.
+ */
+static void rcu_strict_gp_check_qs(void)
+{
+       if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) {
+               rcu_read_lock();
+               rcu_read_unlock();
+       }
+}
+
 /*
  * Update CPU-local rcu_data state to record the beginnings and ends of
  * grace periods.  The caller must hold the ->lock of the leaf rcu_node
@@ -1645,6 +1663,7 @@ static void note_gp_changes(struct rcu_data *rdp)
        }
        needwake = __note_gp_changes(rnp, rdp);
        raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
+       rcu_strict_gp_check_qs();
        if (needwake)
                rcu_gp_kthread_wake();
 }
@@ -1682,6 +1701,15 @@ static void rcu_gp_torture_wait(void)
        }
 }
 
+/*
+ * Handler for on_each_cpu() to invoke the target CPU's RCU core
+ * processing.
+ */
+static void rcu_strict_gp_boundary(void *unused)
+{
+       invoke_rcu_core();
+}
+
 /*
  * Initialize a new grace period.  Return false if no grace period required.
  */
@@ -1720,10 +1748,13 @@ static bool rcu_gp_init(void)
        raw_spin_unlock_irq_rcu_node(rnp);
 
        /*
-        * Apply per-leaf buffered online and offline operations to the
-        * rcu_node tree.  Note that this new grace period need not wait
-        * for subsequent online CPUs, and that quiescent-state forcing
-        * will handle subsequent offline CPUs.
+        * Apply per-leaf buffered online and offline operations to
+        * the rcu_node tree. Note that this new grace period need not
+        * wait for subsequent online CPUs, and that RCU hooks in the CPU
+        * offlining path, when combined with checks in this function,
+        * will handle CPUs that are currently going offline or that will
+        * go offline later.  Please also refer to "Hotplug CPU" section
+        * of RCU's Requirements documentation.
         */
        rcu_state.gp_state = RCU_GP_ONOFF;
        rcu_for_each_leaf_node(rnp) {
@@ -1810,6 +1841,10 @@ static bool rcu_gp_init(void)
                WRITE_ONCE(rcu_state.gp_activity, jiffies);
        }
 
+       // If strict, make all CPUs aware of new grace period.
+       if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
+               on_each_cpu(rcu_strict_gp_boundary, NULL, 0);
+
        return true;
 }
 
@@ -1898,7 +1933,7 @@ static void rcu_gp_fqs_loop(void)
                        break;
                /* If time for quiescent-state forcing, do it. */
                if (!time_after(rcu_state.jiffies_force_qs, jiffies) ||
-                   (gf & RCU_GP_FLAG_FQS)) {
+                   (gf & (RCU_GP_FLAG_FQS | RCU_GP_FLAG_OVLD))) {
                        trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq,
                                               TPS("fqsstart"));
                        rcu_gp_fqs(first_gp_fqs);
@@ -2026,6 +2061,10 @@ static void rcu_gp_cleanup(void)
                           rcu_state.gp_flags & RCU_GP_FLAG_INIT);
        }
        raw_spin_unlock_irq_rcu_node(rnp);
+
+       // If strict, make all CPUs aware of the end of the old grace period.
+       if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
+               on_each_cpu(rcu_strict_gp_boundary, NULL, 0);
 }
 
 /*
@@ -2204,7 +2243,7 @@ rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  * structure.  This must be called from the specified CPU.
  */
 static void
-rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
+rcu_report_qs_rdp(struct rcu_data *rdp)
 {
        unsigned long flags;
        unsigned long mask;
@@ -2213,6 +2252,7 @@ rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
                               rcu_segcblist_is_offloaded(&rdp->cblist);
        struct rcu_node *rnp;
 
+       WARN_ON_ONCE(rdp->cpu != smp_processor_id());
        rnp = rdp->mynode;
        raw_spin_lock_irqsave_rcu_node(rnp, flags);
        if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
@@ -2229,8 +2269,7 @@ rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
                return;
        }
        mask = rdp->grpmask;
-       if (rdp->cpu == smp_processor_id())
-               rdp->core_needs_qs = false;
+       rdp->core_needs_qs = false;
        if ((rnp->qsmask & mask) == 0) {
                raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
        } else {
@@ -2279,7 +2318,7 @@ rcu_check_quiescent_state(struct rcu_data *rdp)
         * Tell RCU we are done (but rcu_report_qs_rdp() will be the
         * judge of that).
         */
-       rcu_report_qs_rdp(rdp->cpu, rdp);
+       rcu_report_qs_rdp(rdp);
 }
 
 /*
@@ -2376,6 +2415,7 @@ int rcutree_dead_cpu(unsigned int cpu)
  */
 static void rcu_do_batch(struct rcu_data *rdp)
 {
+       int div;
        unsigned long flags;
        const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
                               rcu_segcblist_is_offloaded(&rdp->cblist);
@@ -2404,9 +2444,15 @@ static void rcu_do_batch(struct rcu_data *rdp)
        rcu_nocb_lock(rdp);
        WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
        pending = rcu_segcblist_n_cbs(&rdp->cblist);
-       bl = max(rdp->blimit, pending >> rcu_divisor);
-       if (unlikely(bl > 100))
-               tlimit = local_clock() + rcu_resched_ns;
+       div = READ_ONCE(rcu_divisor);
+       div = div < 0 ? 7 : div > sizeof(long) * 8 - 2 ? sizeof(long) * 8 - 2 : div;
+       bl = max(rdp->blimit, pending >> div);
+       if (unlikely(bl > 100)) {
+               long rrn = READ_ONCE(rcu_resched_ns);
+
+               rrn = rrn < NSEC_PER_MSEC ? NSEC_PER_MSEC : rrn > NSEC_PER_SEC ? NSEC_PER_SEC : rrn;
+               tlimit = local_clock() + rrn;
+       }
        trace_rcu_batch_start(rcu_state.name,
                              rcu_segcblist_n_cbs(&rdp->cblist), bl);
        rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
@@ -2547,8 +2593,7 @@ static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
                raw_spin_lock_irqsave_rcu_node(rnp, flags);
                rcu_state.cbovldnext |= !!rnp->cbovldmask;
                if (rnp->qsmask == 0) {
-                       if (!IS_ENABLED(CONFIG_PREEMPT_RCU) ||
-                           rcu_preempt_blocked_readers_cgp(rnp)) {
+                       if (rcu_preempt_blocked_readers_cgp(rnp)) {
                                /*
                                 * No point in scanning bits because they
                                 * are all zero.  But we might need to
@@ -2616,6 +2661,14 @@ void rcu_force_quiescent_state(void)
 }
 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
 
+// Workqueue handler for an RCU reader for kernels enforcing struct RCU
+// grace periods.
+static void strict_work_handler(struct work_struct *work)
+{
+       rcu_read_lock();
+       rcu_read_unlock();
+}
+
 /* Perform RCU core processing work for the current CPU.  */
 static __latent_entropy void rcu_core(void)
 {
@@ -2660,6 +2713,10 @@ static __latent_entropy void rcu_core(void)
        /* Do any needed deferred wakeups of rcuo kthreads. */
        do_nocb_deferred_wakeup(rdp);
        trace_rcu_utilization(TPS("End RCU core"));
+
+       // If strict GPs, schedule an RCU reader in a clean environment.
+       if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
+               queue_work_on(rdp->cpu, rcu_gp_wq, &rdp->strict_work);
 }
 
 static void rcu_core_si(struct softirq_action *h)
@@ -3022,6 +3079,12 @@ struct kfree_rcu_cpu_work {
  * @monitor_todo: Tracks whether a @monitor_work delayed work is pending
  * @initialized: The @rcu_work fields have been initialized
  * @count: Number of objects for which GP not started
+ * @bkvcache:
+ *     A simple cache list that contains objects for reuse purpose.
+ *     In order to save some per-cpu space the list is singular.
+ *     Even though it is lockless an access has to be protected by the
+ *     per-cpu lock.
+ * @nr_bkv_objs: number of allocated objects at @bkvcache.
  *
  * This is a per-CPU structure.  The reason that it is not included in
  * the rcu_data structure is to permit this code to be extracted from
@@ -3037,14 +3100,6 @@ struct kfree_rcu_cpu {
        bool monitor_todo;
        bool initialized;
        int count;
-
-       /*
-        * A simple cache list that contains objects for
-        * reuse purpose. In order to save some per-cpu
-        * space the list is singular. Even though it is
-        * lockless an access has to be protected by the
-        * per-cpu lock.
-        */
        struct llist_head bkvcache;
        int nr_bkv_objs;
 };
@@ -3445,7 +3500,7 @@ kfree_rcu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
        unsigned long count = 0;
 
        /* Snapshot count of all CPUs */
-       for_each_online_cpu(cpu) {
+       for_each_possible_cpu(cpu) {
                struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
 
                count += READ_ONCE(krcp->count);
@@ -3460,7 +3515,7 @@ kfree_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
        int cpu, freed = 0;
        unsigned long flags;
 
-       for_each_online_cpu(cpu) {
+       for_each_possible_cpu(cpu) {
                int count;
                struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
 
@@ -3493,7 +3548,7 @@ void __init kfree_rcu_scheduler_running(void)
        int cpu;
        unsigned long flags;
 
-       for_each_online_cpu(cpu) {
+       for_each_possible_cpu(cpu) {
                struct kfree_rcu_cpu *krcp = per_cpu_ptr(&krc, cpu);
 
                raw_spin_lock_irqsave(&krcp->lock, flags);
@@ -3857,6 +3912,7 @@ rcu_boot_init_percpu_data(int cpu)
 
        /* Set up local state, ensuring consistent view of global state. */
        rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
+       INIT_WORK(&rdp->strict_work, strict_work_handler);
        WARN_ON_ONCE(rdp->dynticks_nesting != 1);
        WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp)));
        rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
@@ -3975,8 +4031,6 @@ int rcutree_offline_cpu(unsigned int cpu)
        return 0;
 }
 
-static DEFINE_PER_CPU(int, rcu_cpu_started);
-
 /*
  * Mark the specified CPU as being online so that subsequent grace periods
  * (both expedited and normal) will wait on it.  Note that this means that
@@ -3996,12 +4050,11 @@ void rcu_cpu_starting(unsigned int cpu)
        struct rcu_node *rnp;
        bool newcpu;
 
-       if (per_cpu(rcu_cpu_started, cpu))
+       rdp = per_cpu_ptr(&rcu_data, cpu);
+       if (rdp->cpu_started)
                return;
+       rdp->cpu_started = true;
 
-       per_cpu(rcu_cpu_started, cpu) = 1;
-
-       rdp = per_cpu_ptr(&rcu_data, cpu);
        rnp = rdp->mynode;
        mask = rdp->grpmask;
        raw_spin_lock_irqsave_rcu_node(rnp, flags);
@@ -4061,7 +4114,7 @@ void rcu_report_dead(unsigned int cpu)
        raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
        raw_spin_unlock(&rcu_state.ofl_lock);
 
-       per_cpu(rcu_cpu_started, cpu) = 0;
+       rdp->cpu_started = false;
 }
 
 /*