Skip to contentSkip to conversation

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when rcupreempt is used.

PATCH In review Bharata B Rao 5 participants 20 messages Mar 17, 2009 – Mar 19, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when rcupreempt is used.

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not happen with rcu or tree rcu as cpuacct_charge() is called with preemption disabled. However if rcupreempt is used, the race still exists. Thanks to Li Zefan for explaining this.

Fix this race by explicitly protecting ca and the hierarchy walk with rcu_read_lock().

---
 kernel/sched.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched.c +8 -0
@@ -9891,6 +9891,13 @@ static void cpuacct_charge(struct task_s
 		return;
 
 	cpu = task_cpu(tsk);
+
+	/*
+	 * preemption is already disabled here, but to be safe with
+	 * rcupreempt, take rcu_read_lock(). This protects ca and
+	 * hence the hierarchy walk.
+	 */
+	rcu_read_lock();
 	ca = task_ca(tsk);
 
 	do {
@@ -9898,6 +9905,7 @@ static void cpuacct_charge(struct task_s
 		*cpuusage += cputime;
 		ca = ca->parent;
 	} while (ca);
+	rcu_read_unlock();
 }
 
 struct cgroup_subsys cpuacct_subsys = {
#1 lore raw
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Actually it can also end up access invalid tsk->cgroups. ;)

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

Quoted 38 lines

happen with rcu or tree rcu as cpuacct_charge() is called with preemption disabled. However if rcupreempt is used, the race still exists. Thanks to Li Zefan for explaining this.

Fix this race by explicitly protecting ca and the hierarchy walk with rcu_read_lock().

---
 kernel/sched.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched.c +8 -0
@@ -9891,6 +9891,13 @@ static void cpuacct_charge(struct task_s
 		return;
 
 	cpu = task_cpu(tsk);
+
+	/*
+	 * preemption is already disabled here, but to be safe with
+	 * rcupreempt, take rcu_read_lock(). This protects ca and
+	 * hence the hierarchy walk.
+	 */
+	rcu_read_lock();
 	ca = task_ca(tsk);
 
 	do {
@@ -9898,6 +9905,7 @@ static void cpuacct_charge(struct task_s
 		*cpuusage += cputime;
 		ca = ca->parent;
 	} while (ca);
+	rcu_read_unlock();
 }
 
 struct cgroup_subsys cpuacct_subsys = {
#2 lore raw
Quoted 8 lines · Li Zefan
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Quoted 6 lines

Actually it can also end up access invalid tsk->cgroups. ;)

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

Ok :) Here is the patch again with updated description.

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge() or access to invalid cgroups pointer of the task. This will not happen with rcu or tree rcu as cpuacct_charge() is called with preemption disabled. However if rcupreempt is used, the race is seen. Thanks to Li Zefan for explaining this.

Fix this race by explicitly protecting ca and the hierarchy walk with rcu_read_lock().

---
 kernel/sched.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched.c +8 -0
@@ -9891,6 +9891,13 @@ static void cpuacct_charge(struct task_s
 		return;
 
 	cpu = task_cpu(tsk);
+
+	/*
+	 * preemption is already disabled here, but to be safe with
+	 * rcupreempt, take rcu_read_lock(). This protects ca and
+	 * hence the hierarchy walk.
+	 */
+	rcu_read_lock();
 	ca = task_ca(tsk);
 
 	do {
@@ -9898,6 +9905,7 @@ static void cpuacct_charge(struct task_s
 		*cpuusage += cputime;
 		ca = ca->parent;
 	} while (ca);
+	rcu_read_unlock();
 }
 
 struct cgroup_subsys cpuacct_subsys = {
Tested-by Acked-by
#3 lore raw
Quoted 28 lines · Bharata B Rao
Quoted 8 lines · Li Zefan
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Quoted 6 lines

Actually it can also end up access invalid tsk->cgroups. ;)

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

Ok :) Here is the patch again with updated description.

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge() or access to invalid cgroups pointer of the task. This will not happen with rcu or tree rcu as cpuacct_charge() is called with preemption disabled. However if rcupreempt is used, the race is seen. Thanks to Li Zefan for explaining this.

Quoted 3 lines

Fix this race by explicitly protecting ca and the hierarchy walk with rcu_read_lock().

Looks good and works very well (except for the batch issue that you pointed out, it takes up to batch values before updates are seen).

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

Signature · 1 line
	Balbir
#4 lore raw
Quoted 4 lines · Balbir Singh

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

You trust userspace? I'd rather not.

#5 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Balbir Singh

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

You trust userspace? I'd rather not.

Fair enough.. A badly written application monitor can frequently read this data and cause horrible performance issues. On the other hand large number of CPUs can make the lag even worse. Is it time yet for percpu_counter batch numbers? I've tested this patch and the results were not badly off the mark.

Signature · 1 line
	Balbir
#6 lore raw
Quoted 9 lines · Balbir Singh
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Balbir Singh

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

Quoted 8 lines
Quoted 2 lines

You trust userspace? I'd rather not.

Fair enough.. A badly written application monitor can frequently read this data and cause horrible performance issues. On the other hand large number of CPUs can make the lag even worse. Is it time yet for percpu_counter batch numbers? I've tested this patch and the results were not badly off the mark.

I'd rather err on the side of caution here, you might get some crazy folks depending on it and then expecting us to maintain it.

#7 lore raw
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Balbir Singh
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Balbir Singh

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

You trust userspace? I'd rather not.

Quoted 8 lines
Quoted 5 lines

Fair enough.. A badly written application monitor can frequently read this data and cause horrible performance issues. On the other hand large number of CPUs can make the lag even worse. Is it time yet for percpu_counter batch numbers? I've tested this patch and the results were not badly off the mark.

I'd rather err on the side of caution here, you might get some crazy folks depending on it and then expecting us to maintain it.

So if we want to be cautious, we could use percpu_counter_sum() as Balbir suggested. That would address both the issues with percpu_counter that I pointed out earlier:

- Readers are serialized with writers and we get consistent/correct values during reads.
- Negates the effect of batching and reads would always get updated/current values.

Regards,
Bharata.

#8 lore raw
Quoted 23 lines · On Wed, 18 Mar 2009 08:55:58 +0530 Bharata B Rao <[email protected]>…
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Balbir Singh
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Balbir Singh

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

You trust userspace? I'd rather not.

Quoted 8 lines
Quoted 5 lines

Fair enough.. A badly written application monitor can frequently read this data and cause horrible performance issues. On the other hand large number of CPUs can make the lag even worse. Is it time yet for percpu_counter batch numbers? I've tested this patch and the results were not badly off the mark.

I'd rather err on the side of caution here, you might get some crazy folks depending on it and then expecting us to maintain it.

Quoted 9 lines

So if we want to be cautious, we could use percpu_counter_sum() as Balbir suggested. That would address both the issues with percpu_counter that I pointed out earlier:

- Readers are serialized with writers and we get consistent/correct values during reads.
- Negates the effect of batching and reads would always get updated/current values.

Is this wrong ?
==
-- CONFIG_32BIT
s64 static inline s64 percpu_counter_read_slow(struct percpu_counter *fbc) {
s64 val;
retry:

    val = fbc->counter;
    smp_mb();
    wait_spin_unlock(&fbc->lock);
    if (fbc->counter < val) {
            goto retry;
    return val;
}

==

Thanks,
-Kame

Quoted 8 lines

Regards,
Bharata.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#9 lore raw
Quoted 50 lines · KAMEZAWA Hiroyuki
Quoted 23 lines · On Wed, 18 Mar 2009 08:55:58 +0530 Bharata B Rao <[email protected]>…
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Balbir Singh
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Balbir Singh

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

You trust userspace? I'd rather not.

Quoted 8 lines
Quoted 5 lines

Fair enough.. A badly written application monitor can frequently read this data and cause horrible performance issues. On the other hand large number of CPUs can make the lag even worse. Is it time yet for percpu_counter batch numbers? I've tested this patch and the results were not badly off the mark.

I'd rather err on the side of caution here, you might get some crazy folks depending on it and then expecting us to maintain it.

Quoted 9 lines

So if we want to be cautious, we could use percpu_counter_sum() as Balbir suggested. That would address both the issues with percpu_counter that I pointed out earlier:

- Readers are serialized with writers and we get consistent/correct values during reads.
- Negates the effect of batching and reads would always get updated/current values.

Is this wrong ?
==
-- CONFIG_32BIT
s64 static inline s64 percpu_counter_read_slow(struct percpu_counter *fbc)

{
   s64 val;
retry:
    val = fbc->counter;
    smp_mb();
    wait_spin_unlock(&fbc->lock);
    if (fbc->counter < val) {
            goto retry;
    return val;
}
Quoted 1 line

==

Looks ok to me, but will wait for experts' comments.

However, I did a quick measurement of read times with percpu_counter_read() (no readside lock) and percpu_counter_sum() (readside lock) and I don't see a major slowdown with percpu_counter_sum().

Time taken for 100 reads of cpuacct.stat with 1s delay b/n every read. percpu_counter_read() - 9845 us
percpu_counter_sum() - 9974 us

This is on a 8cpu system.

Regards,
Bharata.

#10 lore raw
Quoted 10 lines · On Wed, 18 Mar 2009 10:18:01 +0530 Bharata B Rao <[email protected]>…

Looks ok to me, but will wait for experts' comments.

However, I did a quick measurement of read times with percpu_counter_read() (no readside lock) and percpu_counter_sum() (readside lock) and I don't see a major slowdown with percpu_counter_sum().

Time taken for 100 reads of cpuacct.stat with 1s delay b/n every read. percpu_counter_read() - 9845 us
percpu_counter_sum() - 9974 us

Then, almost 1 us overhead per read().....Hmm, seems big (as counter).

Thanks,
-Kame

#11 lore raw
Quoted 8 lines · KAMEZAWA Hiroyuki
Quoted 6 lines · On Wed, 18 Mar 2009 10:18:01 +0530 Bharata B Rao <[email protected]>…

Looks ok to me, but will wait for experts' comments.

However, I did a quick measurement of read times with percpu_counter_read() (no readside lock) and percpu_counter_sum() (readside lock) and I don't see a major slowdown with percpu_counter_sum().

Quoted 5 lines
Quoted 4 lines

Time taken for 100 reads of cpuacct.stat with 1s delay b/n every read. percpu_counter_read() - 9845 us
percpu_counter_sum() - 9974 us

Then, almost 1 us overhead per read().....Hmm, seems big (as counter).

Well some cost for correct and accurate counter :)

BTW, I did a few more iterations and I don't see consistent numbers. The results from 7 runs look like this:

percpu_counter_read() - 11325, 11549, 5939, 9999, 7129, 7758, 11385 us percpu_counter_sum() - 8655, 9201, 8705, 11766, 10619, 9186, 8890 us

Regards,
Bharata.

#12 lore raw
In reply to Peter Zijlstra
Quoted 9 lines · On Tue, 17 Mar 2009 14:26:01 +0100 Peter Zijlstra <[email protected]>…
Quoted 4 lines · Balbir Singh

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

You trust userspace? I'd rather not.

hehe, I don't trust, either.
BTW, is there anyone wants "remove rq->lock" patch ? If yes, I'll dig the patch from my graveyard.

Thanks,
-Kame

#13 lore raw
In reply to Balbir Singh
Quoted 37 lines · Balbir Singh
Quoted 28 lines · Bharata B Rao
Quoted 8 lines · Li Zefan
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Quoted 6 lines

Actually it can also end up access invalid tsk->cgroups. ;)

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

Ok :) Here is the patch again with updated description.

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge() or access to invalid cgroups pointer of the task. This will not happen with rcu or tree rcu as cpuacct_charge() is called with preemption disabled. However if rcupreempt is used, the race is seen. Thanks to Li Zefan for explaining this.

Quoted 3 lines

Fix this race by explicitly protecting ca and the hierarchy walk with rcu_read_lock().

Looks good and works very well (except for the batch issue that you pointed out, it takes up to batch values before updates are seen).

Quoted 8 lines

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

So I guess this ack is not for this patch but for the per-cgroup stime/utime cpuacct controller statistics patch.

Regards,
Bharata.

#14 lore raw
Quoted 44 lines · Bharata B Rao
Quoted 43 lines · Balbir Singh
Quoted 28 lines · Bharata B Rao
Quoted 8 lines · Li Zefan
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Quoted 6 lines

Actually it can also end up access invalid tsk->cgroups. ;)

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

Ok :) Here is the patch again with updated description.

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge() or access to invalid cgroups pointer of the task. This will not happen with rcu or tree rcu as cpuacct_charge() is called with preemption disabled. However if rcupreempt is used, the race is seen. Thanks to Li Zefan for explaining this.

Quoted 3 lines

Fix this race by explicitly protecting ca and the hierarchy walk with rcu_read_lock().

Looks good and works very well (except for the batch issue that you pointed out, it takes up to batch values before updates are seen).

I'd like to get the patches in -tip and see the results, I would recommend using percpu_counter_sum() while reading the data as an enhancement to this patch. If user space does not overwhelm with a lot of reads, sum would work out better.

Quoted 6 lines
Quoted 2 lines

So I guess this ack is not for this patch but for the per-cgroup stime/utime cpuacct controller statistics patch.

Yes.. for both these patches actually. Thanks for pointing it out though.

Signature · 1 line
	Balbir
#15 lore raw
In reply to Bharata B Rao
Quoted 28 lines · Bharata B Rao
Quoted 8 lines · Li Zefan
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Quoted 6 lines

Actually it can also end up access invalid tsk->cgroups. ;)

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

Ok :) Here is the patch again with updated description.

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge() or access to invalid cgroups pointer of the task. This will not happen with rcu or tree rcu as cpuacct_charge() is called with preemption disabled. However if rcupreempt is used, the race is seen. Thanks to Li Zefan for explaining this.

Quoted 4 lines

Fix this race by explicitly protecting ca and the hierarchy walk with rcu_read_lock().

I would ditch the comment, it doesn't add anything.

The simple rule is: if you want RCU-safe, use rcu_read_lock(). preempt/irq disable isn't sufficient -- hasn't been for a long long while.

After that,

Quoted 28 lines
---
 kernel/sched.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/sched.c +8 -0
@@ -9891,6 +9891,13 @@ static void cpuacct_charge(struct task_s
 		return;
 
 	cpu = task_cpu(tsk);
+
+	/*
+	 * preemption is already disabled here, but to be safe with
+	 * rcupreempt, take rcu_read_lock(). This protects ca and
+	 * hence the hierarchy walk.
+	 */
+	rcu_read_lock();
 	ca = task_ca(tsk);
 
 	do {
@@ -9898,6 +9905,7 @@ static void cpuacct_charge(struct task_s
 		*cpuusage += cputime;
 		ca = ca->parent;
 	} while (ca);
+	rcu_read_unlock();
 }
 
 struct cgroup_subsys cpuacct_subsys = {
author Acked-by Peter Zijlstra Acked-by Balbir Singh Tested-by Balbir Singh
#16 lore raw
Quoted 36 lines · Peter Zijlstra
Quoted 28 lines · Bharata B Rao
Quoted 8 lines · Li Zefan
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Quoted 6 lines

Actually it can also end up access invalid tsk->cgroups. ;)

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

Ok :) Here is the patch again with updated description.

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge() or access to invalid cgroups pointer of the task. This will not happen with rcu or tree rcu as cpuacct_charge() is called with preemption disabled. However if rcupreempt is used, the race is seen. Thanks to Li Zefan for explaining this.

Quoted 4 lines

Fix this race by explicitly protecting ca and the hierarchy walk with rcu_read_lock().

I would ditch the comment, it doesn't add anything.

Quoted 8 lines

The simple rule is: if you want RCU-safe, use rcu_read_lock(). preempt/irq disable isn't sufficient -- hasn't been for a long long while.

After that,

Ok. Removed the comment. Here is the updated patch.

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge() or access to invalid cgroups pointer of the task. This will not happen with rcu or tree rcu as cpuacct_charge() is called with preemption disabled. However if rcupreempt is used, the race is seen. Thanks to Li Zefan for explaining this.

Fix this race by explicitly protecting ca and the hierarchy walk with rcu_read_lock().

---
 kernel/sched.c |    3 +++
 1 file changed, 3 insertions(+)
kernel/sched.c +3 -0
@@ -9894,6 +9894,8 @@ static void cpuacct_charge(struct task_s
 		return;
 
 	cpu = task_cpu(tsk);
+
+	rcu_read_lock();
 	ca = task_ca(tsk);
 
 	do {
@@ -9901,6 +9903,7 @@ static void cpuacct_charge(struct task_s
 		*cpuusage += cputime;
 		ca = ca->parent;
 	} while (ca);
+	rcu_read_unlock();
 }
 
 struct cgroup_subsys cpuacct_subsys = {
#17 lore raw
In reply to Li Zefan
Quoted 8 lines · Li Zefan
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Quoted 7 lines

Actually it can also end up access invalid tsk->cgroups. ;)

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

That problem should only occur if we dereference tsk->cgroups separately and then use that to dereference cg->subsys. Since we use task_subsys_state() and that is RCU safe, we should be OK.

Signature · 1 line
	Balbir
#18 lore raw
Quoted 11 lines · Balbir Singh
Quoted 9 lines · Li Zefan
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Actually it can also end up access invalid tsk->cgroups. ;)

Quoted 8 lines
Quoted 5 lines

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

That problem should only occur if we dereference tsk->cgroups separately and then use that to dereference cg->subsys. Since we use

Do you mean tsk->cgroups->subsys is safe and
cg = tsk->cgroups;...; cg->subsys is unsafe ?
This is wrong.

Quoted 2 lines

task_subsys_state() and that is RCU safe, we should be OK.

Yes, it's RCU safe, which means it's unsafe without rcu_read_lock/unlock.

#19 lore raw
Quoted 18 lines · Li Zefan
Quoted 17 lines · Balbir Singh
Quoted 14 lines · Li Zefan
Quoted 6 lines · Bharata B Rao

cpuacct: Make cpuacct hierarchy walk in cpuacct_charge() safe when
rcupreempt is used.

cpuacct_charge() obtains task's ca and does a hierarchy walk upwards. This can race with the task's movement between cgroups. This race can cause an access to freed ca pointer in cpuacct_charge(). This will not

Actually it can also end up access invalid tsk->cgroups. ;)

get tsk->cgroups (cg)
(move tsk to another cgroup) or (tsk exiting) -> kfree(tsk->cgroups)
get cg->subsys[..]

Quoted 6 lines
Quoted 2 lines

That problem should only occur if we dereference tsk->cgroups separately and then use that to dereference cg->subsys. Since we use

Do you mean tsk->cgroups->subsys is safe and
cg = tsk->cgroups;...; cg->subsys is unsafe ?
This is wrong.

Without rcu_read_lock/unlock they are unsafe, even with the lock, we need to use rcu_dereference() to make sure we get consistent values.

Quoted 6 lines
Quoted 3 lines

task_subsys_state() and that is RCU safe, we should be OK.

Yes, it's RCU safe, which means it's unsafe without rcu_read_lock/unlock.

Yes, I meant under rcu_read_lock/unlock.

Signature · 1 line
	Balbir