Skip to contentSkip to conversation

cgroup/cpuset: Fix a race condition when reading cpuset.*

PATCH In review Qais Yousef 2 participants 5 messages Feb 11, 2020 – Feb 13, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cgroup/cpuset: Fix a race condition when reading cpuset.*

LTP cpuset_hotplug_test.sh was failing with the following error message

cpuset_hotplug 1 TFAIL: root group's cpus isn't expected(Result: 0-5, Expect: 0,2-5).

Which is due to a race condition between cpu hotplug operation and reading cpuset.cpus file.

When a cpu is onlined/offlined, cpuset schedules a workqueue to sync its internal data structures with the new values. If a read happens during this window, the user will read a stale value, hence triggering the failure above.

To fix the issue make sure cpuset_wait_for_hotplug() is called before allowing any value to be read, hence forcing the synchronization to happen before the read.

I ran 500 iterations with this fix applied with no failure triggered.

---

I think it's okay to flush the workqueue from the read context? We do it on the write, so I assumed it's okay on the read too. But it'd be good to confirm it doesn't break any rule I'm not aware of.

 kernel/cgroup/cpuset.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/cgroup/cpuset.c +3 -0
index 58f5073acff7..593055522626 100644
@@ -2405,6 +2405,9 @@ static int cpuset_common_seq_show(struct seq_file *sf, void *v)
 	cpuset_filetype_t type = seq_cft(sf)->private;
 	int ret = 0;
 
+	/* Ensure all hotplug ops were done before reading any value */
+	cpuset_wait_for_hotplug();
+
 	spin_lock_irq(&callback_lock);
 
 	switch (type) {
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 12 lines · Qais Yousef

LTP cpuset_hotplug_test.sh was failing with the following error message

cpuset_hotplug 1 TFAIL: root group's cpus isn't expected(Result: 0-5, Expect: 0,2-5).

Which is due to a race condition between cpu hotplug operation and reading cpuset.cpus file.

When a cpu is onlined/offlined, cpuset schedules a workqueue to sync its internal data structures with the new values. If a read happens during this window, the user will read a stale value, hence triggering the failure above.

Quoted 7 lines

To fix the issue make sure cpuset_wait_for_hotplug() is called before allowing any value to be read, hence forcing the synchronization to happen before the read.

I ran 500 iterations with this fix applied with no failure triggered.

Hello, Qais. I just applied a patch which makes the operation synchronous. Can you see whether the problem is gone on the cgroup/for-next branch?

git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git for-next

Thanks.

Signature · 1 line
tejun
#2 lore raw

Hi Tejun

Quoted 19 lines · Tejun Heo
Quoted 18 lines · Qais Yousef

LTP cpuset_hotplug_test.sh was failing with the following error message

cpuset_hotplug 1 TFAIL: root group's cpus isn't expected(Result: 0-5, Expect: 0,2-5).

Which is due to a race condition between cpu hotplug operation and reading cpuset.cpus file.

When a cpu is onlined/offlined, cpuset schedules a workqueue to sync its internal data structures with the new values. If a read happens during this window, the user will read a stale value, hence triggering the failure above.

To fix the issue make sure cpuset_wait_for_hotplug() is called before allowing any value to be read, hence forcing the synchronization to happen before the read.

I ran 500 iterations with this fix applied with no failure triggered.

Quoted 7 lines
Quoted 1 line

Hello, Qais. I just applied a patch which makes the operation synchronous. Can you see whether the problem is gone on the cgroup/for-next branch?

git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git for-next

I ran 500 iterations of cpuset_hotplug_test.sh on the branch, it passed.

I also cherry-picked commit 6426bfb1d5f0 ("cpuset: Make cpuset hotplug synchronous") into v5.6-rc1 and ran 100 iterations and it passed too.

While investigating the problem, I could reproduce it all the way back to v5.0. Stopped there so earlier versions could still have the problem.

Do you think it's worth porting the change to stable trees? Admittedly the problem should be benign, but it did trigger an LTP failure.

I can check 4.19 and 4.14 stable trees (which at least in Android world are still relevant) if you agree it makes sense to put a fix in stable.

Thanks

maintainer
#3 lore raw

Hello,

Quoted 4 lines · Qais Yousef

I ran 500 iterations of cpuset_hotplug_test.sh on the branch, it passed.

I also cherry-picked commit 6426bfb1d5f0 ("cpuset: Make cpuset hotplug synchronous") into v5.6-rc1 and ran 100 iterations and it passed too.

Awesome, thanks for verifying.

Quoted 5 lines

While investigating the problem, I could reproduce it all the way back to v5.0. Stopped there so earlier versions could still have the problem.

Do you think it's worth porting the change to stable trees? Admittedly the problem should be benign, but it did trigger an LTP failure.

I'm afraid not. It's not an issue which would affect actual use cases and there's (as always) some risks involved with backporting it, so the benefit just doesn't seem justifiable here.

Thanks.

Signature · 1 line
tejun
#4 lore raw
Quoted 13 lines · Tejun Heo

Hello,

Quoted 4 lines · Qais Yousef

I ran 500 iterations of cpuset_hotplug_test.sh on the branch, it passed.

I also cherry-picked commit 6426bfb1d5f0 ("cpuset: Make cpuset hotplug synchronous") into v5.6-rc1 and ran 100 iterations and it passed too.

Awesome, thanks for verifying.

Quoted 3 lines

While investigating the problem, I could reproduce it all the way back to v5.0. Stopped there so earlier versions could still have the problem.

Quoted 6 lines
Quoted 2 lines

Do you think it's worth porting the change to stable trees? Admittedly the problem should be benign, but it did trigger an LTP failure.

I'm afraid not. It's not an issue which would affect actual use cases and there's (as always) some risks involved with backporting it, so the benefit just doesn't seem justifiable here.

Yeah I can't see how a real application would depend on this functionality other than for informational reasons. Or testing like in this case.

Thanks

--
Qais Yousef