Skip to contentSkip to conversation

can: fix hrtimer/tasklet termination in bcm op removal

PATCH In review Oliver Hartkopp 3 participants 5 messages Jan 18, 2017 – Jan 30, 2017

Subsystems (2)
lore
Share & tools
lore

Oliver Hartkopp

author maintainer
#0 lore raw

PATCH 1/1 can: fix hrtimer/tasklet termination in bcm op removal

When removing a bcm tx operation either a hrtimer or a tasklet might run. As the hrtimer triggers its associated tasklet and vice versa we need to take care to mutually terminate both handlers.

---
 net/can/bcm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
net/can/bcm.c +16 -7
index 21ac75390e3d..a7dc80c8e724 100644
@@ -734,14 +734,23 @@ static struct bcm_op *bcm_find_op(struct list_head *ops,
 
 static void bcm_remove_op(struct bcm_op *op)
 {
-	hrtimer_cancel(&op->timer);
-	hrtimer_cancel(&op->thrtimer);
-
-	if (op->tsklet.func)
-		tasklet_kill(&op->tsklet);
+	if (op->tsklet.func) {
+		while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
+		       test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
+		       hrtimer_active(&op->timer)) {
+			hrtimer_cancel(&op->timer);
+			tasklet_kill(&op->tsklet);
+		}
+	}
 
-	if (op->thrtsklet.func)
-		tasklet_kill(&op->thrtsklet);
+	if (op->thrtsklet.func) {
+		while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
+		       test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
+		       hrtimer_active(&op->thrtimer)) {
+			hrtimer_cancel(&op->thrtimer);
+			tasklet_kill(&op->thrtsklet);
+		}
+	}
 
 	if ((op->frames) && (op->frames != &op->sframe))
 		kfree(op->frames);
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

Hi Michael,

thanks for your bug report!

I created and tested this patch with some weird settings using a 1 usec cycle together with TX_SETUP and TX_DELETE.

Can you please check this patch with your setup?

Best regards,
Oliver

Quoted 46 lines · Oliver Hartkopp

When removing a bcm tx operation either a hrtimer or a tasklet might run. As the hrtimer triggers its associated tasklet and vice versa we need to take care to mutually terminate both handlers.

---
 net/can/bcm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
net/can/bcm.c +16 -7
index 21ac75390e3d..a7dc80c8e724 100644
@@ -734,14 +734,23 @@ static struct bcm_op *bcm_find_op(struct list_head *ops,
 
 static void bcm_remove_op(struct bcm_op *op)
 {
-	hrtimer_cancel(&op->timer);
-	hrtimer_cancel(&op->thrtimer);
-
-	if (op->tsklet.func)
-		tasklet_kill(&op->tsklet);
+	if (op->tsklet.func) {
+		while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
+		       test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
+		       hrtimer_active(&op->timer)) {
+			hrtimer_cancel(&op->timer);
+			tasklet_kill(&op->tsklet);
+		}
+	}
 
-	if (op->thrtsklet.func)
-		tasklet_kill(&op->thrtsklet);
+	if (op->thrtsklet.func) {
+		while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
+		       test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
+		       hrtimer_active(&op->thrtimer)) {
+			hrtimer_cancel(&op->thrtimer);
+			tasklet_kill(&op->thrtsklet);
+		}
+	}
 
 	if ((op->frames) && (op->frames != &op->sframe))
 		kfree(op->frames);
Tested-by on v1 1/1
#2 lore raw
In reply to Oliver Hartkopp

When removing a bcm tx operation either a hrtimer or a tasklet might run. As the hrtimer triggers its associated tasklet and vice versa we need to take care to mutually terminate both handlers.

Quoted 40 lines
---
 net/can/bcm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
net/can/bcm.c +16 -8
index 21ac75390e3d..a7dc80c8e724 100644
@@ -734,14 +734,23 @@ static struct bcm_op *bcm_find_op(struct list_head *ops,
 
 static void bcm_remove_op(struct bcm_op *op)
 {
-	hrtimer_cancel(&op->timer);
-	hrtimer_cancel(&op->thrtimer);
-
-	if (op->tsklet.func)
-		tasklet_kill(&op->tsklet);
+	if (op->tsklet.func) {
+		while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
+		       test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
+		       hrtimer_active(&op->timer)) {
+			hrtimer_cancel(&op->timer);
+			tasklet_kill(&op->tsklet);
+		}
+	}
 
-	if (op->thrtsklet.func)
-		tasklet_kill(&op->thrtsklet);
+	if (op->thrtsklet.func) {
+		while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
+		       test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
+		       hrtimer_active(&op->thrtimer)) {
+			hrtimer_cancel(&op->thrtimer);
+			tasklet_kill(&op->thrtsklet);
+		}
+	}
 
 	if ((op->frames) && (op->frames != &op->sframe))
 		kfree(op->frames);
--
author maintainer
#3 lore raw
Quoted 7 lines · Michael Josenhans

When removing a bcm tx operation either a hrtimer or a tasklet might run. As the hrtimer triggers its associated tasklet and vice versa we need to take care to mutually terminate both handlers.

Thanks for testing!

@Marc: This is a candidate for stable too.

Best regards,
Oliver

Quoted 41 lines
Quoted 41 lines
---
 net/can/bcm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
net/can/bcm.c +16 -7
index 21ac75390e3d..a7dc80c8e724 100644
@@ -734,14 +734,23 @@ static struct bcm_op *bcm_find_op(struct
 list_head *ops,
 
 static void bcm_remove_op(struct bcm_op *op)
 {
-    hrtimer_cancel(&op->timer);
-    hrtimer_cancel(&op->thrtimer);
-
-    if (op->tsklet.func)
-        tasklet_kill(&op->tsklet);
+    if (op->tsklet.func) {
+        while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
+               test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
+               hrtimer_active(&op->timer)) {
+            hrtimer_cancel(&op->timer);
+            tasklet_kill(&op->tsklet);
+        }
+    }
 
-    if (op->thrtsklet.func)
-        tasklet_kill(&op->thrtsklet);
+    if (op->thrtsklet.func) {
+        while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
+               test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
+               hrtimer_active(&op->thrtimer)) {
+            hrtimer_cancel(&op->thrtimer);
+            tasklet_kill(&op->thrtsklet);
+        }
+    }
 
     if ((op->frames) && (op->frames != &op->sframe))
         kfree(op->frames);

--

#4 lore raw
Quoted 6 lines · Oliver Hartkopp
Quoted 4 lines · Michael Josenhans

When removing a bcm tx operation either a hrtimer or a tasklet might run. As the hrtimer triggers its associated tasklet and vice versa we need to take care to mutually terminate both handlers.

Quoted 7 lines
Quoted 3 lines

Thanks for testing!

@Marc: This is a candidate for stable too.

added to can.

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |