--- /dev/null
+--- linux-2.4.21/net/sched/sch_htb.c Thu Jun 19 09:33:50 2003
++++ linux-2.4/net/sched/sch_htb.c Wed Jun 18 21:57:07 2003
+@@ -9,6 +9,8 @@
+ * Authors: Martin Devera, <devik@cdi.cz>
+ *
+ * Credits (in time order) for older HTB versions:
++ * Stef Coene <stef.coene@docum.org>
++ * HTB support at LARTC mailing list
+ * Ondrej Kraus, <krauso@barr.cz>
+ * found missing INIT_QDISC(htb)
+ * Vladimir Smelhaus, Aamer Akhter, Bert Hubert
+@@ -19,7 +21,7 @@
+ * created test case so that I was able to fix nasty bug
+ * and many others. thanks.
+ *
+- * $Id$
++ * $Id$
+ */
+ #include <linux/config.h>
+ #include <linux/module.h>
+@@ -71,7 +73,7 @@
+ #define HTB_HYSTERESIS 1/* whether to use mode hysteresis for speedup */
+ #define HTB_QLOCK(S) spin_lock_bh(&(S)->dev->queue_lock)
+ #define HTB_QUNLOCK(S) spin_unlock_bh(&(S)->dev->queue_lock)
+-#define HTB_VER 0x3000a /* major must be matched with number suplied by TC as version */
++#define HTB_VER 0x3000c /* major must be matched with number suplied by TC as version */
+
+ #if HTB_VER >> 16 != TC_HTB_PROTOVER
+ #error "Mismatched sch_htb.c and pkt_sch.h"
+@@ -217,6 +219,9 @@
+ /* time of nearest event per level (row) */
+ unsigned long near_ev_cache[TC_HTB_MAXDEPTH];
+
++ /* cached value of jiffies in dequeue */
++ unsigned long jiffies;
++
+ /* whether we hit non-work conserving class during this dequeue; we use */
+ int nwc_hit; /* this to disable mindelay complaint in dequeue */
+
+@@ -336,7 +341,7 @@
+ static void htb_debug_dump (struct htb_sched *q)
+ {
+ int i,p;
+- printk(KERN_DEBUG "htb*g j=%lu\n",jiffies);
++ printk(KERN_DEBUG "htb*g j=%lu lj=%lu\n",jiffies,q->jiffies);
+ /* rows */
+ for (i=TC_HTB_MAXDEPTH-1;i>=0;i--) {
+ printk(KERN_DEBUG "htb*r%d m=%x",i,q->row_mask[i]);
+@@ -419,8 +424,8 @@
+ if ((delay <= 0 || delay > cl->mbuffer) && net_ratelimit())
+ printk(KERN_ERR "HTB: suspicious delay in wait_tree d=%ld cl=%X h=%d\n",delay,cl->classid,debug_hint);
+ #endif
+- cl->pq_key = jiffies + PSCHED_US2JIFFIE(delay);
+- if (cl->pq_key == jiffies)
++ cl->pq_key = q->jiffies + PSCHED_US2JIFFIE(delay);
++ if (cl->pq_key == q->jiffies)
+ cl->pq_key++;
+
+ /* update the nearest event cache */
+@@ -598,7 +603,7 @@
+ long toks;
+
+ if ((toks = (cl->ctokens + *diff)) < (
+-#ifdef HTB_HYSTERESIS
++#if HTB_HYSTERESIS
+ cl->cmode != HTB_CANT_SEND ? -cl->cbuffer :
+ #endif
+ 0)) {
+@@ -606,7 +611,7 @@
+ return HTB_CANT_SEND;
+ }
+ if ((toks = (cl->tokens + *diff)) >= (
+-#ifdef HTB_HYSTERESIS
++#if HTB_HYSTERESIS
+ cl->cmode == HTB_CAN_SEND ? -cl->buffer :
+ #endif
+ 0))
+@@ -809,7 +814,7 @@
+ cl->classid, diff,
+ (unsigned long long) q->now,
+ (unsigned long long) cl->t_c,
+- jiffies);
++ q->jiffies);
+ diff = 1000;
+ }
+ #endif
+@@ -852,6 +857,7 @@
+ *
+ * Scans event queue for pending events and applies them. Returns jiffies to
+ * next pending event (0 for no event in pq).
++ * Note: Aplied are events whose have cl->pq_key <= jiffies.
+ */
+ static long htb_do_events(struct htb_sched *q,int level)
+ {
+@@ -866,9 +872,9 @@
+ while (p->rb_left) p = p->rb_left;
+
+ cl = rb_entry(p, struct htb_class, pq_node);
+- if (cl->pq_key - (jiffies+1) < 0x80000000) {
+- HTB_DBG(8,3,"htb_do_ev_ret delay=%ld\n",cl->pq_key - jiffies);
+- return cl->pq_key - jiffies;
++ if (cl->pq_key - (q->jiffies+1) < 0x80000000) {
++ HTB_DBG(8,3,"htb_do_ev_ret delay=%ld\n",cl->pq_key - q->jiffies);
++ return cl->pq_key - q->jiffies;
+ }
+ htb_safe_rb_erase(p,q->wait_pq+level);
+ diff = PSCHED_TDIFF_SAFE(q->now, cl->t_c, (u32)cl->mbuffer, 0);
+@@ -879,7 +885,7 @@
+ cl->classid, diff,
+ (unsigned long long) q->now,
+ (unsigned long long) cl->t_c,
+- jiffies);
++ q->jiffies);
+ diff = 1000;
+ }
+ #endif
+@@ -987,7 +993,8 @@
+ delay = 5*HZ;
+ }
+ del_timer(&q->timer);
+- q->timer.expires = jiffies + delay;
++ /* why don't use jiffies here ? because expires can be in past */
++ q->timer.expires = q->jiffies + delay;
+ add_timer(&q->timer);
+ sch->flags |= TCQ_F_THROTTLED;
+ sch->stats.overlimits++;
+@@ -1004,6 +1011,7 @@
+ int evs_used = 0;
+ #endif
+
++ q->jiffies = jiffies;
+ HTB_DBG(3,1,"htb_deq dircnt=%d qlen=%d\n",skb_queue_len(&q->direct_queue),
+ sch->q.qlen);
+
+@@ -1023,14 +1031,14 @@
+ /* common case optimization - skip event handler quickly */
+ int m;
+ long delay;
+- if (jiffies - q->near_ev_cache[level] < 0x80000000 || 0) {
++ if (q->jiffies - q->near_ev_cache[level] < 0x80000000 || 0) {
+ delay = htb_do_events(q,level);
+- q->near_ev_cache[level] += delay ? delay : HZ;
++ q->near_ev_cache[level] = q->jiffies + (delay ? delay : HZ);
+ #ifdef HTB_DEBUG
+ evs_used++;
+ #endif
+ } else
+- delay = q->near_ev_cache[level] - jiffies;
++ delay = q->near_ev_cache[level] - q->jiffies;
+
+ if (delay && min_delay > delay)
+ min_delay = delay;
+@@ -1049,8 +1057,8 @@
+ #ifdef HTB_DEBUG
+ if (!q->nwc_hit && min_delay >= 10*HZ && net_ratelimit()) {
+ if (min_delay == LONG_MAX) {
+- printk(KERN_ERR "HTB: dequeue bug (%d), report it please !\n",
+- evs_used);
++ printk(KERN_ERR "HTB: dequeue bug (%d,%lu,%lu), report it please !\n",
++ evs_used,q->jiffies,jiffies);
+ htb_debug_dump(q);
+ } else
+ printk(KERN_WARNING "HTB: mindelay=%ld, some class has "
+@@ -1059,7 +1067,7 @@
+ #endif
+ htb_delay_by (sch,min_delay > 5*HZ ? 5*HZ : min_delay);
+ fin:
+- HTB_DBG(3,1,"htb_deq_end %s j=%lu skb=%p\n",sch->dev->name,jiffies,skb);
++ HTB_DBG(3,1,"htb_deq_end %s j=%lu skb=%p\n",sch->dev->name,q->jiffies,skb);
+ return skb;
+ }
+
+@@ -1424,7 +1432,7 @@
+ parent = parentid == TC_H_ROOT ? NULL : htb_find (parentid,sch);
+
+ hopt = RTA_DATA(tb[TCA_HTB_PARMS-1]);
+- HTB_DBG(0,1,"htb_chg cl=%p, clid=%X, opt/prio=%d, rate=%u, buff=%d, quant=%d\n", cl,cl?cl->classid:0,(int)hopt->prio,hopt->rate.rate,hopt->buffer,hopt->quantum);
++ HTB_DBG(0,1,"htb_chg cl=%p(%X), clid=%X, parid=%X, opt/prio=%d, rate=%u, buff=%d, quant=%d\n", cl,cl?cl->classid:0,classid,parentid,(int)hopt->prio,hopt->rate.rate,hopt->buffer,hopt->quantum);
+ rtab = qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB-1]);
+ ctab = qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB-1]);
+ if (!rtab || !ctab) goto failure;
--- /dev/null
+--- linux-2.4/net/sched/sch_htb.c 2003/06/18 19:55:49 1.20
++++ linux-2.4/net/sched/sch_htb.c 2003/07/05 10:37:51
+@@ -98,7 +98,8 @@
+ from LSB
+ */
+ #ifdef HTB_DEBUG
+-#define HTB_DBG(S,L,FMT,ARG...) if (((q->debug>>(2*S))&3) >= L) \
++#define HTB_DBG_COND(S,L) (((q->debug>>(2*S))&3) >= L)
++#define HTB_DBG(S,L,FMT,ARG...) if (HTB_DBG_COND(S,L)) \
+ printk(KERN_DEBUG FMT,##ARG)
+ #define HTB_CHCL(cl) BUG_TRAP((cl)->magic == HTB_CMAGIC)
+ #define HTB_PASSQ q,
+@@ -114,6 +115,7 @@
+ rb_erase(N,R); \
+ (N)->rb_color = -1; } while (0)
+ #else
++#define HTB_DBG_COND(S,L) (0)
+ #define HTB_DBG(S,L,FMT,ARG...)
+ #define HTB_PASSQ
+ #define HTB_ARGQ
+@@ -1202,7 +1204,8 @@ static int htb_dump(struct Qdisc *sch, s
+ gopt.direct_pkts = q->direct_pkts;
+
+ #ifdef HTB_DEBUG
+- htb_debug_dump(q);
++ if (HTB_DBG_COND(0,2))
++ htb_debug_dump(q);
+ #endif
+ gopt.version = HTB_VER;
+ gopt.rate2quantum = q->rate2quantum;
--- /dev/null
+diff -urN linux-2.4.20/net/sched/sch_htb.c linux-2.4.21/net/sched/sch_htb.c
+--- linux-2.4.20/net/sched/sch_htb.c Fri Nov 29 00:53:16 2002
++++ linux-2.4.21/net/sched/sch_htb.c Fri Jun 13 16:51:39 2003
+@@ -19,7 +19,7 @@
+ * created test case so that I was able to fix nasty bug
+ * and many others. thanks.
+ *
+- * $Id$
++ * $Id$
+ */
+ #include <linux/config.h>
+ #include <linux/module.h>
+@@ -71,16 +71,12 @@
+ #define HTB_HYSTERESIS 1/* whether to use mode hysteresis for speedup */
+ #define HTB_QLOCK(S) spin_lock_bh(&(S)->dev->queue_lock)
+ #define HTB_QUNLOCK(S) spin_unlock_bh(&(S)->dev->queue_lock)
+-#define HTB_VER 0x30007 /* major must be matched with number suplied by TC as version */
++#define HTB_VER 0x3000a /* major must be matched with number suplied by TC as version */
+
+ #if HTB_VER >> 16 != TC_HTB_PROTOVER
+ #error "Mismatched sch_htb.c and pkt_sch.h"
+ #endif
+
+-/* temporary debug defines to be removed after beta stage */
+-#define DEVIK_MEND(N)
+-#define DEVIK_MSTART(N)
+-
+ /* debugging support; S is subsystem, these are defined:
+ 0 - netlink messages
+ 1 - enqueue
+@@ -106,7 +102,9 @@
+ #define HTB_PASSQ q,
+ #define HTB_ARGQ struct htb_sched *q,
+ #define static
++#undef __inline__
+ #define __inline__
++#undef inline
+ #define inline
+ #define HTB_CMAGIC 0xFEFAFEF1
+ #define htb_safe_rb_erase(N,R) do { BUG_TRAP((N)->rb_color != -1); \
+@@ -421,7 +419,6 @@
+ if ((delay <= 0 || delay > cl->mbuffer) && net_ratelimit())
+ printk(KERN_ERR "HTB: suspicious delay in wait_tree d=%ld cl=%X h=%d\n",delay,cl->classid,debug_hint);
+ #endif
+- DEVIK_MSTART(9);
+ cl->pq_key = jiffies + PSCHED_US2JIFFIE(delay);
+ if (cl->pq_key == jiffies)
+ cl->pq_key++;
+@@ -440,7 +437,6 @@
+ }
+ rb_link_node(&cl->pq_node, parent, p);
+ rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);
+- DEVIK_MEND(9);
+ }
+
+ /**
+@@ -689,7 +685,6 @@
+ struct htb_sched *q = (struct htb_sched *)sch->data;
+ struct htb_class *cl = htb_classify(skb,sch);
+
+- DEVIK_MSTART(0);
+ if (cl == HTB_DIRECT || !cl) {
+ /* enqueue to helper queue */
+ if (q->direct_queue.qlen < q->direct_qlen && cl) {
+@@ -698,25 +693,20 @@
+ } else {
+ kfree_skb (skb);
+ sch->stats.drops++;
+- DEVIK_MEND(0);
+ return NET_XMIT_DROP;
+ }
+ } else if (cl->un.leaf.q->enqueue(skb, cl->un.leaf.q) != NET_XMIT_SUCCESS) {
+ sch->stats.drops++;
+ cl->stats.drops++;
+- DEVIK_MEND(0);
+ return NET_XMIT_DROP;
+ } else {
+ cl->stats.packets++; cl->stats.bytes += skb->len;
+- DEVIK_MSTART(1);
+ htb_activate (q,cl);
+- DEVIK_MEND(1);
+ }
+
+ sch->q.qlen++;
+ sch->stats.packets++; sch->stats.bytes += skb->len;
+ HTB_DBG(1,1,"htb_enq_ok cl=%X skb=%p\n",cl?cl->classid:0,skb);
+- DEVIK_MEND(0);
+ return NET_XMIT_SUCCESS;
+ }
+
+@@ -952,7 +942,6 @@
+ //struct htb_sched *q = (struct htb_sched *)sch->data;
+ struct htb_class *cl,*start;
+ /* look initial class up in the row */
+- DEVIK_MSTART(6);
+ start = cl = htb_lookup_leaf (q->row[level]+prio,prio,q->ptr[level]+prio);
+
+ do {
+@@ -971,8 +960,6 @@
+ cl = htb_lookup_leaf (q->row[level]+prio,prio,q->ptr[level]+prio);
+ } while (cl != start);
+
+- DEVIK_MEND(6);
+- DEVIK_MSTART(7);
+ if (likely(skb != NULL)) {
+ if ((cl->un.leaf.deficit[level] -= skb->len) < 0) {
+ HTB_DBG(4,2,"htb_next_cl oldptr=%p quant_add=%d\n",
+@@ -984,11 +971,8 @@
+ gives us slightly better performance */
+ if (!cl->un.leaf.q->q.qlen)
+ htb_deactivate (q,cl);
+- DEVIK_MSTART(8);
+ htb_charge_class (q,cl,level,skb->len);
+- DEVIK_MEND(8);
+ }
+- DEVIK_MEND(7);
+ return skb;
+ }
+
+@@ -1016,6 +1000,9 @@
+ struct htb_sched *q = (struct htb_sched *)sch->data;
+ int level;
+ long min_delay;
++#ifdef HTB_DEBUG
++ int evs_used = 0;
++#endif
+
+ HTB_DBG(3,1,"htb_deq dircnt=%d qlen=%d\n",skb_queue_len(&q->direct_queue),
+ sch->q.qlen);
+@@ -1027,27 +1014,26 @@
+ return skb;
+ }
+
+- DEVIK_MSTART(2);
+ if (!sch->q.qlen) goto fin;
+ PSCHED_GET_TIME(q->now);
+
+- min_delay = HZ*5;
++ min_delay = LONG_MAX;
+ q->nwc_hit = 0;
+ for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
+ /* common case optimization - skip event handler quickly */
+ int m;
+ long delay;
+- DEVIK_MSTART(3);
+ if (jiffies - q->near_ev_cache[level] < 0x80000000 || 0) {
+ delay = htb_do_events(q,level);
+ q->near_ev_cache[level] += delay ? delay : HZ;
++#ifdef HTB_DEBUG
++ evs_used++;
++#endif
+ } else
+ delay = q->near_ev_cache[level] - jiffies;
+
+ if (delay && min_delay > delay)
+ min_delay = delay;
+- DEVIK_MEND(3);
+- DEVIK_MSTART(5);
+ m = ~q->row_mask[level];
+ while (m != (int)(-1)) {
+ int prio = ffz (m);
+@@ -1056,24 +1042,24 @@
+ if (likely(skb != NULL)) {
+ sch->q.qlen--;
+ sch->flags &= ~TCQ_F_THROTTLED;
+- DEVIK_MEND(5);
+ goto fin;
+ }
+ }
+- DEVIK_MEND(5);
+ }
+- DEVIK_MSTART(4);
+ #ifdef HTB_DEBUG
+- if (!q->nwc_hit && min_delay >= 5*HZ && net_ratelimit()) {
+- printk(KERN_ERR "HTB: mindelay=%ld, report it please !\n",min_delay);
+- htb_debug_dump(q);
++ if (!q->nwc_hit && min_delay >= 10*HZ && net_ratelimit()) {
++ if (min_delay == LONG_MAX) {
++ printk(KERN_ERR "HTB: dequeue bug (%d), report it please !\n",
++ evs_used);
++ htb_debug_dump(q);
++ } else
++ printk(KERN_WARNING "HTB: mindelay=%ld, some class has "
++ "too small rate\n",min_delay);
+ }
+ #endif
+- htb_delay_by (sch,min_delay);
+- DEVIK_MEND(4);
++ htb_delay_by (sch,min_delay > 5*HZ ? 5*HZ : min_delay);
+ fin:
+ HTB_DBG(3,1,"htb_deq_end %s j=%lu skb=%p\n",sch->dev->name,jiffies,skb);
+- DEVIK_MEND(2);
+ return skb;
+ }
+
+@@ -1444,6 +1430,7 @@
+ if (!rtab || !ctab) goto failure;
+
+ if (!cl) { /* new class */
++ struct Qdisc *new_q;
+ /* check for valid classid */
+ if (!classid || TC_H_MAJ(classid^sch->handle) || htb_find(classid,sch))
+ goto failure;
+@@ -1467,6 +1454,10 @@
+ cl->magic = HTB_CMAGIC;
+ #endif
+
++ /* create leaf qdisc early because it uses kmalloc(GPF_KERNEL)
++ so that can't be used inside of sch_tree_lock
++ -- thanks to Karlis Peisenieks */
++ new_q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
+ sch_tree_lock(sch);
+ if (parent && !parent->level) {
+ /* turn parent into inner node */
+@@ -1485,8 +1476,7 @@
+ memset (&parent->un.inner,0,sizeof(parent->un.inner));
+ }
+ /* leaf (we) needs elementary qdisc */
+- if (!(cl->un.leaf.q = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops)))
+- cl->un.leaf.q = &noop_qdisc;
++ cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
+
+ cl->classid = classid; cl->parent = parent;
+
+@@ -1514,11 +1504,11 @@
+ if (!cl->level) {
+ cl->un.leaf.quantum = rtab->rate.rate / q->rate2quantum;
+ if (!hopt->quantum && cl->un.leaf.quantum < 1000) {
+- printk(KERN_WARNING "HTB: quantum of class %X is small. Consider r2q change.", cl->classid);
++ printk(KERN_WARNING "HTB: quantum of class %X is small. Consider r2q change.\n", cl->classid);
+ cl->un.leaf.quantum = 1000;
+ }
+ if (!hopt->quantum && cl->un.leaf.quantum > 200000) {
+- printk(KERN_WARNING "HTB: quantum of class %X is big. Consider r2q change.", cl->classid);
++ printk(KERN_WARNING "HTB: quantum of class %X is big. Consider r2q change.\n", cl->classid);
+ cl->un.leaf.quantum = 200000;
+ }
+ if (hopt->quantum)