Skip to contentSkip to conversation

net/sched: cbs: fix calculation error of idleslope credits

PATCH In review Xiaoyong Yan 3 participants 3 messages Sep 18, 2020 – Sep 19, 2020

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

PATCH 1/1 net/sched: cbs: fix calculation error of idleslope credits

in the function cbs_dequeue_soft, when q->credits< 0, (now- q->last) should be accounted for sendslope, not idleslope.

so the solution is as follows: when q->credits is less than 0, directly calculate delay time, activate hrtimer and when hrtimer fires, calculate idleslope credits and update it to q->credits.

---
 net/sched/sch_cbs.c | 71 ++++++++++++++++++++++++++++++---------------
 1 file changed, 48 insertions(+), 23 deletions(-)
net/sched/sch_cbs.c +48 -23
index 2eaac2ff380f..b870576839d1 100644
@@ -76,7 +76,9 @@ struct cbs_sched_data {
 	s32 hicredit; /* in bytes */
 	s64 sendslope; /* in bytes/s */
 	s64 idleslope; /* in bytes/s */
-	struct qdisc_watchdog watchdog;
+	struct hrtimer timer;
+	struct Qdisc *sch;
+	u64 last_expires;
 	int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
 		       struct sk_buff **to_free);
 	struct sk_buff *(*dequeue)(struct Qdisc *sch);
@@ -84,6 +86,41 @@ struct cbs_sched_data {
 	struct list_head cbs_list;
 };
 
+/* timediff is in ns, slope is in bytes/s */
+static s64 timediff_to_credits(s64 timediff, s64 slope)
+{
+	return div64_s64(timediff * slope, NSEC_PER_SEC);
+}
+
+static void cbs_timer_schedule(struct cbs_sched_data *q, u64 expires)
+{
+	if (test_bit(__QDISC_STATE_DEACTIVATED,
+				&qdisc_root_sleeping(q->sch)->state))
+		return;
+	if (q->last_expires == expires)
+		return;
+	q->last_expires = expires;
+	hrtimer_start(&q->timer,
+			ns_to_ktime(expires),
+			HRTIMER_MODE_ABS_PINNED);
+
+}
+static enum hrtimer_restart cbs_timer(struct hrtimer *timer)
+{
+	struct cbs_sched_data *q = container_of(timer, struct cbs_sched_data, timer);
+	s64 now = ktime_get_ns();
+	s64 credits;
+
+	credits = timediff_to_credits(now- q->last, q->idleslope);
+	credits = q->credits+ credits;
+	q->credits = clamp_t(s64, credits, q->locredit, q->hicredit);
+	q->last = now;
+	rcu_read_lock();
+	__netif_schedule(qdisc_root(q->sch));
+	rcu_read_unlock();
+
+	return HRTIMER_NORESTART;
+}
 static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 			     struct Qdisc *child,
 			     struct sk_buff **to_free)
@@ -135,12 +172,6 @@ static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 	return q->enqueue(skb, sch, to_free);
 }
 
-/* timediff is in ns, slope is in bytes/s */
-static s64 timediff_to_credits(s64 timediff, s64 slope)
-{
-	return div64_s64(timediff * slope, NSEC_PER_SEC);
-}
-
 static s64 delay_from_credits(s64 credits, s64 slope)
 {
 	if (unlikely(slope == 0))
@@ -183,25 +214,17 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
 
 	/* The previous packet is still being sent */
 	if (now < q->last) {
-		qdisc_watchdog_schedule_ns(&q->watchdog, q->last);
+		cbs_timer_schedule(q, q->last);
 		return NULL;
 	}
 	if (q->credits < 0) {
-		credits = timediff_to_credits(now - q->last, q->idleslope);
-
-		credits = q->credits + credits;
-		q->credits = min_t(s64, credits, q->hicredit);
-
-		if (q->credits < 0) {
-			s64 delay;
-
-			delay = delay_from_credits(q->credits, q->idleslope);
-			qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
+		s64 delay;
 
-			q->last = now;
+		delay = delay_from_credits(q->credits, q->idleslope);
+	    cbs_timer_schedule(q, now+ delay);
+		q->last = now;
 
-			return NULL;
-		}
+		return NULL;
 	}
 	skb = cbs_child_dequeue(sch, qdisc);
 	if (!skb)
@@ -424,7 +447,9 @@ static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
 	q->enqueue = cbs_enqueue_soft;
 	q->dequeue = cbs_dequeue_soft;
 
-	qdisc_watchdog_init(&q->watchdog, sch);
+	hrtimer_init(&q->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
+	q->timer.function = cbs_timer;
+	q->sch = sch;
 
 	return cbs_change(sch, opt, extack);
 }
@@ -438,7 +463,7 @@ static void cbs_destroy(struct Qdisc *sch)
 	if (!q->qdisc)
 		return;
 
-	qdisc_watchdog_cancel(&q->watchdog);
+	hrtimer_cancel(&q->timer);
 	cbs_disable_offload(dev, q);
 
 	spin_lock(&cbs_list_lock);
Signature · 1 line
2.25.1
maintainer
#1 lore raw

From: Xiaoyong Yan <[email protected]>
Date: Fri, 18 Sep 2020 01:42:52 -0700

net/sched/sch_cbs.c static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch) · line 225

Quoted 3 lines
net/sched/sch_cbs.c +3 -0
+		delay = delay_from_credits(q->credits, q->idleslope);
+	    cbs_timer_schedule(q, now+ delay);
+		q->last = now;

This indentation cannot be correct.

Please fix this.

#2 lore raw
In reply to Xiaoyong Yan
Thank you for the patch! Perhaps something to improve:

Hi Xiaoyong,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.9-rc5 next-20200918]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Xiaoyong-Yan/net-sched-cbs-fix-calculation-error-of-idleslope-credits/20200918-164430
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 10b82d5176488acee2820e5a2cf0f2ec5c3488b6
config: h8300-randconfig-m031-20200917 (attached as .config)
compiler: h8300-linux-gcc (GCC) 9.3.0

If you fix the issue, kindly add following tag as appropriate

smatch warnings:
net/sched/sch_cbs.c:224 cbs_dequeue_soft() warn: inconsistent indenting

# https://github.com/0day-ci/linux/commit/6e3656a6f5879070aaee0343f90e633de7d2d85a
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Xiaoyong-Yan/net-sched-cbs-fix-calculation-error-of-idleslope-credits/20200918-164430 git checkout 6e3656a6f5879070aaee0343f90e633de7d2d85a
vim +224 net/sched/sch_cbs.c

205
206 static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
207 {
208 struct cbs_sched_data *q = qdisc_priv(sch);
209 struct Qdisc *qdisc = q->qdisc;
210 s64 now = ktime_get_ns();
211 struct sk_buff *skb;
212 s64 credits;
213 int len;
214
215 /* The previous packet is still being sent */
216 if (now < q->last) {
217 cbs_timer_schedule(q, q->last);
218 return NULL;
219 }
220 if (q->credits < 0) {
221 s64 delay;
222
223 delay = delay_from_credits(q->credits, q->idleslope);

 > 224		    cbs_timer_schedule(q, now+ delay);

225 q->last = now;
226
227 return NULL;
228 }
229 skb = cbs_child_dequeue(sch, qdisc);
230 if (!skb)
231 return NULL;
232
233 len = qdisc_pkt_len(skb);
234
235 /* As sendslope is a negative number, this will decrease the
236 * amount of q->credits.
237 */
238 credits = credits_from_len(len, q->sendslope,
239 atomic64_read(&q->port_rate));
240 credits += q->credits;
241
242 q->credits = max_t(s64, credits, q->locredit);
243 /* Estimate of the transmission of the last byte of the packet in ns */
244 if (unlikely(atomic64_read(&q->port_rate) == 0))
245 q->last = now;
246 else
247 q->last = now + div64_s64(len * NSEC_PER_SEC,
248 atomic64_read(&q->port_rate));
249
250 return skb;
251 }
252

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org