linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xiaoyong Yan <yanxiaoyong5@gmail.com>
To: jhs@mojatatu.com, xiyou.wangcong@gmail.com, jiri@resnulli.us
Cc: davem@davemloft.net, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Xiaoyong Yan <yanxiaoyong5@gmail.com>
Subject: [PATCH] net/sched: cbs: fix calculation error of idleslope credits
Date: Fri, 18 Sep 2020 01:42:52 -0700	[thread overview]
Message-ID: <20200918084252.4200-1-yanxiaoyong5@gmail.com> (raw)

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.

Signed-off-by: Xiaoyong Yan <yanxiaoyong5@gmail.com>
---
 net/sched/sch_cbs.c | 71 ++++++++++++++++++++++++++++++---------------
 1 file changed, 48 insertions(+), 23 deletions(-)

diff --git a/net/sched/sch_cbs.c b/net/sched/sch_cbs.c
index 2eaac2ff380f..b870576839d1 100644
--- a/net/sched/sch_cbs.c
+++ b/net/sched/sch_cbs.c
@@ -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);
-- 
2.25.1


             reply	other threads:[~2020-09-18  8:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-18  8:42 Xiaoyong Yan [this message]
2020-09-19  0:50 ` [PATCH] net/sched: cbs: fix calculation error of idleslope credits David Miller
2020-09-19 11:35 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200918084252.4200-1-yanxiaoyong5@gmail.com \
    --to=yanxiaoyong5@gmail.com \
    --cc=davem@davemloft.net \
    --cc=jhs@mojatatu.com \
    --cc=jiri@resnulli.us \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).