Zqiang caught a snafu in the rude grace period, if a too small grace
period was reached, a UAF could occur. However, his patch used
a WARN_ONCE() on schedule_on_each_cpu() OOM failures, which would not
have handled the failure properly.

Instead of throwing a warning or relying on dynamic alloc_percpu(), replace
the dynamic allocation with static per cpu work_struct instances. Using
schedule_work_on() and flush_work() keeps everything in task context,
guarantees the required context switch on every online CPU, and stays
zero allocation so it cannot fail under OOM.

Suggested-by: Zqiang <[email protected]>
Signed-off-by: Bradley Morgan <[email protected]>
---
 kernel/rcu/tasks.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index 627295396cd9..f81c0ea0b92a 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -1348,11 +1348,25 @@ static void rcu_tasks_be_rude(struct work_struct *work)
 {
 }
 
+static DEFINE_PER_CPU(struct work_struct, tasks_rude_work);
+
 // Wait for one rude RCU-tasks grace period.
 static void rcu_tasks_rude_wait_gp(struct rcu_tasks *rtp)
 {
+       int cpu;
+
+       guard(cpus_read_lock)();
        rtp->n_ipis += cpumask_weight(cpu_online_mask);
-       schedule_on_each_cpu(rcu_tasks_be_rude);
+
+       for_each_online_cpu(cpu) {
+               struct work_struct *work = &per_cpu(tasks_rude_work, cpu);
+
+               INIT_WORK(work, rcu_tasks_be_rude);
+               schedule_work_on(cpu, work);
+       }
+
+       for_each_online_cpu(cpu)
+               flush_work(&per_cpu(tasks_rude_work, cpu));
 }
 
 static void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func);
-- 
2.53.0


Reply via email to