Skip to contentSkip to conversation

Fix TASKLET_STATE_SCHED WARN_ON()

PATCH Oleg Nesterov 2 participants 3 messages Jun 15, 2007

lore
Share & tools
lore
#0 lore raw
Quoted 4 lines · john stultz

The following additional patch should correct this issue. Although since we weren't actually hitting it, the issue is a bit theoretical, so I've not been able to prove it really fixes anything.

Could you please look at the message below? I sent it privately near a month ago, but I think these problems are not fixed yet.

Oleg.

---------------------------------------------------------------------------

Quoted 21 lines
(quoted diff) +16 -0
+__tasklet_action(struct softirq_action *a, struct tasklet_struct *list)
+{
+	int loops = 1000000;
 
 	while (list) {
 		struct tasklet_struct *t = list;
 
 		list = list->next;
+		/*
+		 * Should always succeed - after a tasklist got on the
+		 * list (after getting the SCHED bit set from 0 to 1),
+		 * nothing but the tasklet softirq it got queued to can
+		 * lock it:
+		 */
+		if (!tasklet_trylock(t)) {
+			WARN_ON(1);
+			continue;
+		}
+
+		t->next = NULL;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+		/*
+		 * If we cannot handle the tasklet because it's disabled,
+		 * mark it as pending. tasklet_enable() will later
+		 * re-schedule the tasklet.
+		 */
+		if (unlikely(atomic_read(&t->count))) {
+out_disabled:
+			/* implicit unlock: */
+			wmb();
+			t->state = TASKLET_STATEF_PENDING;

What if tasklet_enable() happens just before this line ?

After the next schedule_tasklet() we have all bits set: SCHED, RUN, PENDING. The next invocation of __tasklet_action() clears SCHED, but tasklet_tryunlock() below can never succeed because of PENDING.

Quoted 10 lines
(quoted diff) +10 -0
+again:
+		t->func(t->data);
+
+		/*
+		 * Try to unlock the tasklet. We must use cmpxchg, because
+		 * another CPU might have scheduled or disabled the tasklet.
+		 * We only allow the STATE_RUN -> 0 transition here.
+		 */
+		while (!tasklet_tryunlock(t)) {
+			/*

Review

Quoted 10 lines
(quoted diff) +10 -0
+			 * If it got disabled meanwhile, bail out:
+			 */
+			if (atomic_read(&t->count))
+				goto out_disabled;
+			/*
+			 * If it got scheduled meanwhile, re-execute
+			 * the tasklet function:
+			 */
+			if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
+				goto again;

TASKLET_STATE_SCHED could be set by tasklet_kill(), in this case it is not nice to call t->func() again (but probably harmless).

#1 lore raw
Quoted 8 lines · Oleg Nesterov
Quoted 4 lines · john stultz

The following additional patch should correct this issue. Although since we weren't actually hitting it, the issue is a bit theoretical, so I've not been able to prove it really fixes anything.

Could you please look at the message below? I sent it privately near a month ago, but I think these problems are not fixed yet.

Hmm. Maybe you sent it to others on the cc list, as I can't find it in my box. But apologies anyway.

Quoted 36 lines

---------------------------------------------------------------------------

Quoted 21 lines
(quoted diff) +16 -0
+__tasklet_action(struct softirq_action *a, struct tasklet_struct *list)
+{
+	int loops = 1000000;
 
 	while (list) {
 		struct tasklet_struct *t = list;
 
 		list = list->next;
+		/*
+		 * Should always succeed - after a tasklist got on the
+		 * list (after getting the SCHED bit set from 0 to 1),
+		 * nothing but the tasklet softirq it got queued to can
+		 * lock it:
+		 */
+		if (!tasklet_trylock(t)) {
+			WARN_ON(1);
+			continue;
+		}
+
+		t->next = NULL;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+		/*
+		 * If we cannot handle the tasklet because it's disabled,
+		 * mark it as pending. tasklet_enable() will later
+		 * re-schedule the tasklet.
+		 */
+		if (unlikely(atomic_read(&t->count))) {
+out_disabled:
+			/* implicit unlock: */
+			wmb();
+			t->state = TASKLET_STATEF_PENDING;

What if tasklet_enable() happens just before this line ?

Quoted 3 lines

After the next schedule_tasklet() we have all bits set: SCHED, RUN, PENDING. The next invocation of __tasklet_action() clears SCHED, but tasklet_tryunlock() below can never succeed because of PENDING.

Yep. I've only been focusing on races in schedule/action, as I've been hunting issues w/ rcu. But I'll agree that the other state changes look problematic. I know Paul McKenney was looking at some of the other state changes and was seeing some potential problems as well.

thanks
-john

#2 lore raw
Quoted 8 lines · john stultz
Quoted 3 lines · Oleg Nesterov

Could you please look at the message below? I sent it privately near a month ago, but I think these problems are not fixed yet.

Hmm. Maybe you sent it to others on the cc list, as I can't find it in my box. But apologies anyway.

checking my mbox... Oops, you are right, sorry!

Quoted 8 lines
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+		if (unlikely(atomic_read(&t->count))) {
+out_disabled:
+			/* implicit unlock: */
+			wmb();
+			t->state = TASKLET_STATEF_PENDING;

What if tasklet_enable() happens just before this line ?

Quoted 8 lines
Quoted 3 lines

After the next schedule_tasklet() we have all bits set: SCHED, RUN, PENDING. The next invocation of __tasklet_action() clears SCHED, but tasklet_tryunlock() below can never succeed because of PENDING.

Yep. I've only been focusing on races in schedule/action, as I've been hunting issues w/ rcu. But I'll agree that the other state changes look problematic. I know Paul McKenney was looking at some of the other state changes and was seeing some potential problems as well.

OK, thanks. But doesn't this mean your 2-nd patch is questionable?

Review

Quoted 13 lines
(quoted diff) +13 -0
+               } else {
+                       /* This is subtle. If we hit the corner case above
+                        * It is possible that we get preempted right here,
+                        * and another task has successfully called
+                        * tasklet_schedule(), then this function, and
+                        * failed on the trylock. Thus we must be sure
+                        * before releasing the tasklet lock, that the
+                        * SCHED_BIT is clear. Otherwise the tasklet
+                        * may get its SCHED_BIT set, but not added to the
+                        * list
+                        */
+                       if (!tasklet_tryunlock(t))
+                               goto again;

Again, tasklet_tryunlock() can fail because _PENDING was set by __tasklet_action(). In that case __tasklet_common_schedule() goes to the endless loop, no?

Oleg.