Skip to contentSkip to conversation

can: bcm: increase tx cycle time precision

PATCH In review Oliver Hartkopp 1 participant 3 messages Aug 24, 2017 – Sep 2, 2017

Subsystems (2)
lore
Share & tools
lore

Oliver Hartkopp

author maintainer
#0 lore raw

PATCH 1/2 can: bcm: increase tx cycle time precision

When creating cyclic TX jobs with the broadcast manager the timer is re-armed based on the current time. Due to the execution time of the timer handling and the CAN frame transmission the fixed cycle time gets expanded over time.

Calculate the new expiration time based on the former expiration time to remove this unpredictable delay and omit unnecessary ktime_get() calls.

---
 net/can/bcm.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
net/can/bcm.c +12 -7
index 47a8748d953a..f1a8d9a5b1cb 100644
@@ -103,7 +103,7 @@ struct bcm_op {
 	struct bcm_timeval ival1, ival2;
 	struct hrtimer timer, thrtimer;
 	struct tasklet_struct tsklet, thrtsklet;
-	ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
+	ktime_t rx_stamp, kt_ival1, kt_ival2, kt_expire, kt_lastmsg;
 	int rx_ifindex;
 	int cfsiz;
 	u32 count;
@@ -364,16 +364,21 @@ static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
 	}
 }
 
+static void bcm_tx_start_the_timer(struct bcm_op *op, ktime_t ival)
+{
+	if (!op->kt_expire)
+		op->kt_expire = ktime_get();
+
+	op->kt_expire = ktime_add(op->kt_expire, ival);
+	hrtimer_start(&op->timer, op->kt_expire, HRTIMER_MODE_ABS);
+}
+
 static void bcm_tx_start_timer(struct bcm_op *op)
 {
 	if (op->kt_ival1 && op->count)
-		hrtimer_start(&op->timer,
-			      ktime_add(ktime_get(), op->kt_ival1),
-			      HRTIMER_MODE_ABS);
+		bcm_tx_start_the_timer(op, op->kt_ival1);
 	else if (op->kt_ival2)
-		hrtimer_start(&op->timer,
-			      ktime_add(ktime_get(), op->kt_ival2),
-			      HRTIMER_MODE_ABS);
+		bcm_tx_start_the_timer(op, op->kt_ival2);
 }
 
 static void bcm_tx_timeout_tsklet(unsigned long data)
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

PATCH 2/2 can: bcm: limit minimum gap of tx cycle time

The cyclic TX message creation is intended for automotive use-cases to implement high precision CAN frame transmissions from kernel space. Usual applications send CAN frames in a cycle time ranging from 1000ms down to 5ms. This patch implements a reasonable minimum gap of 500us.

---
 net/can/bcm.c | 9 +++++++++
 1 file changed, 9 insertions(+)
net/can/bcm.c +9 -0
index f1a8d9a5b1cb..b0dbe7a389ee 100644
@@ -67,6 +67,9 @@
  */
 #define MAX_NFRAMES 256
 
+/* minimum gap between two frame transmissions for TX_SETUP : 500 usecs */
+#define BCM_TX_MIN_NS 500000
+
 /* use of last_frames[index].flags */
 #define RX_RECV    0x40 /* received data for this element */
 #define RX_THR     0x80 /* element not been sent due to throttle feature */
@@ -1020,6 +1023,12 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
 		op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
 		op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
 
+		if (op->kt_ival1 && ktime_to_ns(op->kt_ival1) < BCM_TX_MIN_NS)
+			op->kt_ival1 = BCM_TX_MIN_NS;
+
+		if (op->kt_ival2 && ktime_to_ns(op->kt_ival2) < BCM_TX_MIN_NS)
+			op->kt_ival2 = BCM_TX_MIN_NS;
+
 		/* disable an active timer due to zero values? */
 		if (!op->kt_ival1 && !op->kt_ival2)
 			hrtimer_cancel(&op->timer);
Signature · 1 line
2.11.0
author maintainer
#2 lore raw
In reply to Oliver Hartkopp

Hi Marc,

as this patch set is not urgent, I'll send an update after the hrtimer rework from Anna-Maria & tglx has hit the mainline kernel.

Best regards,
Oliver

Quoted 54 lines · Oliver Hartkopp

When creating cyclic TX jobs with the broadcast manager the timer is re-armed based on the current time. Due to the execution time of the timer handling and the CAN frame transmission the fixed cycle time gets expanded over time.

Calculate the new expiration time based on the former expiration time to remove this unpredictable delay and omit unnecessary ktime_get() calls.

---
  net/can/bcm.c | 19 ++++++++++++-------
  1 file changed, 12 insertions(+), 7 deletions(-)
net/can/bcm.c +12 -7
index 47a8748d953a..f1a8d9a5b1cb 100644
@@ -103,7 +103,7 @@ struct bcm_op {
  	struct bcm_timeval ival1, ival2;
  	struct hrtimer timer, thrtimer;
  	struct tasklet_struct tsklet, thrtsklet;
-	ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
+	ktime_t rx_stamp, kt_ival1, kt_ival2, kt_expire, kt_lastmsg;
  	int rx_ifindex;
  	int cfsiz;
  	u32 count;
@@ -364,16 +364,21 @@ static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
  	}
  }
 
+static void bcm_tx_start_the_timer(struct bcm_op *op, ktime_t ival)
+{
+	if (!op->kt_expire)
+		op->kt_expire = ktime_get();
+
+	op->kt_expire = ktime_add(op->kt_expire, ival);
+	hrtimer_start(&op->timer, op->kt_expire, HRTIMER_MODE_ABS);
+}
+
  static void bcm_tx_start_timer(struct bcm_op *op)
  {
  	if (op->kt_ival1 && op->count)
-		hrtimer_start(&op->timer,
-			      ktime_add(ktime_get(), op->kt_ival1),
-			      HRTIMER_MODE_ABS);
+		bcm_tx_start_the_timer(op, op->kt_ival1);
  	else if (op->kt_ival2)
-		hrtimer_start(&op->timer,
-			      ktime_add(ktime_get(), op->kt_ival2),
-			      HRTIMER_MODE_ABS);
+		bcm_tx_start_the_timer(op, op->kt_ival2);
  }
 
  static void bcm_tx_timeout_tsklet(unsigned long data)