Skip to contentSkip to conversation

rcu/tasks: Use static per cpu work_struct for Tasks Rude grace periods

PATCH In review Bradley Morgan 3 participants 4 messages Oct 17 – Oct 18 RCU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rcu/tasks: Use static per cpu work_struct for Tasks Rude grace periods

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.

---
 kernel/rcu/tasks.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
kernel/rcu/tasks.h +15 -1
index 627295396cd9..f81c0ea0b92a 100644
@@ -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);
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 5 lines · Bradley Morgan

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.

Quoted 8 lines

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.

Sorry, Bradley, but no, absolutely not.

Because this not taking a suggestion, but is instead *exactly* Zqiang's patch. We do not commit plagiarism in the Linux-kernel community.

Zqiang, please submit a proper patch with appropriate credit to Bradley. The obvious approach would be to following his example, that is, to credit him using Suggested-by, but Reported-by or a sentence calling out his feedback in the commit log would suffice. However, I will defer to your best judgment.

Thanx, Paul

Quoted 37 lines
---
 kernel/rcu/tasks.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
kernel/rcu/tasks.h +15 -1
index 627295396cd9..f81c0ea0b92a 100644
@@ -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);
Signature · 1 line
2.53.0
#2 lore raw
Quoted 18 lines · Paul E. McKenney
Quoted 6 lines · Bradley Morgan

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

Quoted 7 lines

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.

Sorry, Bradley, but no, absolutely not.

Quoted 8 lines

Because this not taking a suggestion, but is instead *exactly* Zqiang's patch. We do not commit plagiarism in the Linux-kernel community.

Zqiang, please submit a proper patch with appropriate credit to Bradley. The obvious approach would be to following his example, that is, to credit him using Suggested-by, but Reported-by or a sentence calling out his feedback in the commit log would suffice. However, I will defer to your best judgment.

Apologies, I must've misunderstood..
:(

Quoted 38 lines

Thanx, Paul

kernel/rcu/tasks.h static void rcu_tasks_be_rude(struct work_struct

Quoted 9 lines
---
 kernel/rcu/tasks.h | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
kernel/rcu/tasks.h +0 -0
index 627295396cd9..f81c0ea0b92a 100644
@@ -1348,11 +1348,25 @@ static void rcu_tasks_be_rude(struct work_struct

*work)

Quoted 25 lines
kernel/rcu/tasks.h +15 -1
 {
 }
 
+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));
 }
 
Quoted 4 lines

kernel/rcu/tasks.h static void rcu_tasks_be_rude(struct work_struct · line 1372

Quoted 1 line
kernel/rcu/tasks.h +0 -0
 static void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t

func);

Quoted 2 lines
Signature · 1 line
2.53.0

--- Thanks!
"I'm not a very positive person" - Linus torvalds

reviewer
#3 lore raw
In reply to Paul E. McKenney
Quoted 20 lines · Bradley Morgan
Quoted 7 lines

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.

Quoted 9 lines

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.

Sorry, Bradley, but no, absolutely not.

Quoted 8 lines

Because this not taking a suggestion, but is instead *exactly* Zqiang's patch. We do not commit plagiarism in the Linux-kernel community.

Zqiang, please submit a proper patch with appropriate credit to Bradley. The obvious approach would be to following his example, that is, to credit him using Suggested-by, but Reported-by or a sentence calling out his feedback in the commit log would suffice. However, I will defer to your best judgment.

Thanks, Paul

I will resend it as v2 and add Bradley as Suggested-by.

Thanks
Zqiang

Quoted 43 lines

Thanx, Paul

Quoted 39 lines
---
 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