739a8711ab30c42df56b7f4efd02f902d8e5003a
[pandora-kernel.git] / net / sched / sch_generic.c
1 /*
2  * net/sched/sch_generic.c      Generic packet scheduler routines.
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *              Jamal Hadi Salim, <hadi@cyberus.ca> 990601
11  *              - Ingress support
12  */
13
14 #include <linux/bitops.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/init.h>
25 #include <linux/rcupdate.h>
26 #include <linux/list.h>
27 #include <net/pkt_sched.h>
28
29 /* Main transmission queue. */
30
31 /* Modifications to data participating in scheduling must be protected with
32  * queue->lock spinlock.
33  *
34  * The idea is the following:
35  * - enqueue, dequeue are serialized via top level device
36  *   spinlock queue->lock.
37  * - ingress filtering is serialized via top level device
38  *   spinlock dev->rx_queue.lock.
39  * - updates to tree and tree walking are only done under the rtnl mutex.
40  */
41
42 void qdisc_lock_tree(struct net_device *dev)
43         __acquires(dev->rx_queue.lock)
44 {
45         unsigned int i;
46
47         local_bh_disable();
48         for (i = 0; i < dev->num_tx_queues; i++) {
49                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
50                 spin_lock(&txq->lock);
51         }
52         spin_lock(&dev->rx_queue.lock);
53 }
54 EXPORT_SYMBOL(qdisc_lock_tree);
55
56 void qdisc_unlock_tree(struct net_device *dev)
57         __releases(dev->rx_queue.lock)
58 {
59         unsigned int i;
60
61         spin_unlock(&dev->rx_queue.lock);
62         for (i = 0; i < dev->num_tx_queues; i++) {
63                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
64                 spin_unlock(&txq->lock);
65         }
66         local_bh_enable();
67 }
68 EXPORT_SYMBOL(qdisc_unlock_tree);
69
70 static inline int qdisc_qlen(struct Qdisc *q)
71 {
72         return q->q.qlen;
73 }
74
75 static inline int dev_requeue_skb(struct sk_buff *skb,
76                                   struct netdev_queue *dev_queue,
77                                   struct Qdisc *q)
78 {
79         if (unlikely(skb->next))
80                 q->gso_skb = skb;
81         else
82                 q->ops->requeue(skb, q);
83
84         netif_schedule_queue(dev_queue);
85         return 0;
86 }
87
88 static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
89 {
90         struct sk_buff *skb;
91
92         if ((skb = q->gso_skb))
93                 q->gso_skb = NULL;
94         else
95                 skb = q->dequeue(q);
96
97         return skb;
98 }
99
100 static inline int handle_dev_cpu_collision(struct sk_buff *skb,
101                                            struct netdev_queue *dev_queue,
102                                            struct Qdisc *q)
103 {
104         int ret;
105
106         if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
107                 /*
108                  * Same CPU holding the lock. It may be a transient
109                  * configuration error, when hard_start_xmit() recurses. We
110                  * detect it by checking xmit owner and drop the packet when
111                  * deadloop is detected. Return OK to try the next skb.
112                  */
113                 kfree_skb(skb);
114                 if (net_ratelimit())
115                         printk(KERN_WARNING "Dead loop on netdevice %s, "
116                                "fix it urgently!\n", dev_queue->dev->name);
117                 ret = qdisc_qlen(q);
118         } else {
119                 /*
120                  * Another cpu is holding lock, requeue & delay xmits for
121                  * some time.
122                  */
123                 __get_cpu_var(netdev_rx_stat).cpu_collision++;
124                 ret = dev_requeue_skb(skb, dev_queue, q);
125         }
126
127         return ret;
128 }
129
130 /*
131  * NOTE: Called under queue->lock with locally disabled BH.
132  *
133  * __QDISC_STATE_RUNNING guarantees only one CPU can process
134  * this qdisc at a time. queue->lock serializes queue accesses for
135  * this queue AND txq->qdisc pointer itself.
136  *
137  *  netif_tx_lock serializes accesses to device driver.
138  *
139  *  queue->lock and netif_tx_lock are mutually exclusive,
140  *  if one is grabbed, another must be free.
141  *
142  * Note, that this procedure can be called by a watchdog timer
143  *
144  * Returns to the caller:
145  *                              0  - queue is empty or throttled.
146  *                              >0 - queue is not empty.
147  *
148  */
149 static inline int qdisc_restart(struct netdev_queue *txq,
150                                 struct Qdisc *q)
151 {
152         int ret = NETDEV_TX_BUSY;
153         struct net_device *dev;
154         spinlock_t *root_lock;
155         struct sk_buff *skb;
156
157         /* Dequeue packet */
158         if (unlikely((skb = dequeue_skb(q)) == NULL))
159                 return 0;
160
161         root_lock = qdisc_root_lock(q);
162
163         /* And release qdisc */
164         spin_unlock(root_lock);
165
166         dev = txq->dev;
167
168         HARD_TX_LOCK(dev, txq, smp_processor_id());
169         if (!netif_subqueue_stopped(dev, skb))
170                 ret = dev_hard_start_xmit(skb, dev, txq);
171         HARD_TX_UNLOCK(dev, txq);
172
173         spin_lock(root_lock);
174
175         switch (ret) {
176         case NETDEV_TX_OK:
177                 /* Driver sent out skb successfully */
178                 ret = qdisc_qlen(q);
179                 break;
180
181         case NETDEV_TX_LOCKED:
182                 /* Driver try lock failed */
183                 ret = handle_dev_cpu_collision(skb, txq, q);
184                 break;
185
186         default:
187                 /* Driver returned NETDEV_TX_BUSY - requeue skb */
188                 if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
189                         printk(KERN_WARNING "BUG %s code %d qlen %d\n",
190                                dev->name, ret, q->q.qlen);
191
192                 ret = dev_requeue_skb(skb, txq, q);
193                 break;
194         }
195
196         return ret;
197 }
198
199 void __qdisc_run(struct netdev_queue *txq)
200 {
201         unsigned long start_time = jiffies;
202         struct Qdisc *q = txq->qdisc;
203
204         while (qdisc_restart(txq, q)) {
205                 if (netif_tx_queue_stopped(txq))
206                         break;
207
208                 /*
209                  * Postpone processing if
210                  * 1. another process needs the CPU;
211                  * 2. we've been doing it for too long.
212                  */
213                 if (need_resched() || jiffies != start_time) {
214                         netif_schedule_queue(txq);
215                         break;
216                 }
217         }
218
219         clear_bit(__QDISC_STATE_RUNNING, &q->state);
220 }
221
222 static void dev_watchdog(unsigned long arg)
223 {
224         struct net_device *dev = (struct net_device *)arg;
225
226         netif_tx_lock(dev);
227         if (!qdisc_tx_is_noop(dev)) {
228                 if (netif_device_present(dev) &&
229                     netif_running(dev) &&
230                     netif_carrier_ok(dev)) {
231                         int some_queue_stopped = 0;
232                         unsigned int i;
233
234                         for (i = 0; i < dev->num_tx_queues; i++) {
235                                 struct netdev_queue *txq;
236
237                                 txq = netdev_get_tx_queue(dev, i);
238                                 if (netif_tx_queue_stopped(txq)) {
239                                         some_queue_stopped = 1;
240                                         break;
241                                 }
242                         }
243
244                         if (some_queue_stopped &&
245                             time_after(jiffies, (dev->trans_start +
246                                                  dev->watchdog_timeo))) {
247                                 printk(KERN_INFO "NETDEV WATCHDOG: %s: "
248                                        "transmit timed out\n",
249                                        dev->name);
250                                 dev->tx_timeout(dev);
251                                 WARN_ON_ONCE(1);
252                         }
253                         if (!mod_timer(&dev->watchdog_timer,
254                                        round_jiffies(jiffies +
255                                                      dev->watchdog_timeo)))
256                                 dev_hold(dev);
257                 }
258         }
259         netif_tx_unlock(dev);
260
261         dev_put(dev);
262 }
263
264 void __netdev_watchdog_up(struct net_device *dev)
265 {
266         if (dev->tx_timeout) {
267                 if (dev->watchdog_timeo <= 0)
268                         dev->watchdog_timeo = 5*HZ;
269                 if (!mod_timer(&dev->watchdog_timer,
270                                round_jiffies(jiffies + dev->watchdog_timeo)))
271                         dev_hold(dev);
272         }
273 }
274
275 static void dev_watchdog_up(struct net_device *dev)
276 {
277         __netdev_watchdog_up(dev);
278 }
279
280 static void dev_watchdog_down(struct net_device *dev)
281 {
282         netif_tx_lock_bh(dev);
283         if (del_timer(&dev->watchdog_timer))
284                 dev_put(dev);
285         netif_tx_unlock_bh(dev);
286 }
287
288 /**
289  *      netif_carrier_on - set carrier
290  *      @dev: network device
291  *
292  * Device has detected that carrier.
293  */
294 void netif_carrier_on(struct net_device *dev)
295 {
296         if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
297                 linkwatch_fire_event(dev);
298                 if (netif_running(dev))
299                         __netdev_watchdog_up(dev);
300         }
301 }
302 EXPORT_SYMBOL(netif_carrier_on);
303
304 /**
305  *      netif_carrier_off - clear carrier
306  *      @dev: network device
307  *
308  * Device has detected loss of carrier.
309  */
310 void netif_carrier_off(struct net_device *dev)
311 {
312         if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
313                 linkwatch_fire_event(dev);
314 }
315 EXPORT_SYMBOL(netif_carrier_off);
316
317 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
318    under all circumstances. It is difficult to invent anything faster or
319    cheaper.
320  */
321
322 static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
323 {
324         kfree_skb(skb);
325         return NET_XMIT_CN;
326 }
327
328 static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
329 {
330         return NULL;
331 }
332
333 static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
334 {
335         if (net_ratelimit())
336                 printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
337                        skb->dev->name);
338         kfree_skb(skb);
339         return NET_XMIT_CN;
340 }
341
342 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
343         .id             =       "noop",
344         .priv_size      =       0,
345         .enqueue        =       noop_enqueue,
346         .dequeue        =       noop_dequeue,
347         .requeue        =       noop_requeue,
348         .owner          =       THIS_MODULE,
349 };
350
351 static struct netdev_queue noop_netdev_queue = {
352         .lock           =       __SPIN_LOCK_UNLOCKED(noop_netdev_queue.lock),
353         .qdisc          =       &noop_qdisc,
354 };
355
356 struct Qdisc noop_qdisc = {
357         .enqueue        =       noop_enqueue,
358         .dequeue        =       noop_dequeue,
359         .flags          =       TCQ_F_BUILTIN,
360         .ops            =       &noop_qdisc_ops,
361         .list           =       LIST_HEAD_INIT(noop_qdisc.list),
362         .dev_queue      =       &noop_netdev_queue,
363 };
364 EXPORT_SYMBOL(noop_qdisc);
365
366 static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
367         .id             =       "noqueue",
368         .priv_size      =       0,
369         .enqueue        =       noop_enqueue,
370         .dequeue        =       noop_dequeue,
371         .requeue        =       noop_requeue,
372         .owner          =       THIS_MODULE,
373 };
374
375 static struct Qdisc noqueue_qdisc = {
376         .enqueue        =       NULL,
377         .dequeue        =       noop_dequeue,
378         .flags          =       TCQ_F_BUILTIN,
379         .ops            =       &noqueue_qdisc_ops,
380         .list           =       LIST_HEAD_INIT(noqueue_qdisc.list),
381 };
382
383
384 static const u8 prio2band[TC_PRIO_MAX+1] =
385         { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
386
387 /* 3-band FIFO queue: old style, but should be a bit faster than
388    generic prio+fifo combination.
389  */
390
391 #define PFIFO_FAST_BANDS 3
392
393 static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
394                                              struct Qdisc *qdisc)
395 {
396         struct sk_buff_head *list = qdisc_priv(qdisc);
397         return list + prio2band[skb->priority & TC_PRIO_MAX];
398 }
399
400 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
401 {
402         struct sk_buff_head *list = prio2list(skb, qdisc);
403
404         if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
405                 qdisc->q.qlen++;
406                 return __qdisc_enqueue_tail(skb, qdisc, list);
407         }
408
409         return qdisc_drop(skb, qdisc);
410 }
411
412 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
413 {
414         int prio;
415         struct sk_buff_head *list = qdisc_priv(qdisc);
416
417         for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
418                 if (!skb_queue_empty(list + prio)) {
419                         qdisc->q.qlen--;
420                         return __qdisc_dequeue_head(qdisc, list + prio);
421                 }
422         }
423
424         return NULL;
425 }
426
427 static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
428 {
429         qdisc->q.qlen++;
430         return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
431 }
432
433 static void pfifo_fast_reset(struct Qdisc* qdisc)
434 {
435         int prio;
436         struct sk_buff_head *list = qdisc_priv(qdisc);
437
438         for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
439                 __qdisc_reset_queue(qdisc, list + prio);
440
441         qdisc->qstats.backlog = 0;
442         qdisc->q.qlen = 0;
443 }
444
445 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
446 {
447         struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
448
449         memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
450         NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
451         return skb->len;
452
453 nla_put_failure:
454         return -1;
455 }
456
457 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
458 {
459         int prio;
460         struct sk_buff_head *list = qdisc_priv(qdisc);
461
462         for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
463                 skb_queue_head_init(list + prio);
464
465         return 0;
466 }
467
468 static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
469         .id             =       "pfifo_fast",
470         .priv_size      =       PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
471         .enqueue        =       pfifo_fast_enqueue,
472         .dequeue        =       pfifo_fast_dequeue,
473         .requeue        =       pfifo_fast_requeue,
474         .init           =       pfifo_fast_init,
475         .reset          =       pfifo_fast_reset,
476         .dump           =       pfifo_fast_dump,
477         .owner          =       THIS_MODULE,
478 };
479
480 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
481                           struct Qdisc_ops *ops)
482 {
483         void *p;
484         struct Qdisc *sch;
485         unsigned int size;
486         int err = -ENOBUFS;
487
488         /* ensure that the Qdisc and the private data are 32-byte aligned */
489         size = QDISC_ALIGN(sizeof(*sch));
490         size += ops->priv_size + (QDISC_ALIGNTO - 1);
491
492         p = kzalloc(size, GFP_KERNEL);
493         if (!p)
494                 goto errout;
495         sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
496         sch->padded = (char *) sch - (char *) p;
497
498         INIT_LIST_HEAD(&sch->list);
499         skb_queue_head_init(&sch->q);
500         sch->ops = ops;
501         sch->enqueue = ops->enqueue;
502         sch->dequeue = ops->dequeue;
503         sch->dev_queue = dev_queue;
504         dev_hold(qdisc_dev(sch));
505         atomic_set(&sch->refcnt, 1);
506
507         return sch;
508 errout:
509         return ERR_PTR(err);
510 }
511
512 struct Qdisc * qdisc_create_dflt(struct net_device *dev,
513                                  struct netdev_queue *dev_queue,
514                                  struct Qdisc_ops *ops,
515                                  unsigned int parentid)
516 {
517         struct Qdisc *sch;
518
519         sch = qdisc_alloc(dev_queue, ops);
520         if (IS_ERR(sch))
521                 goto errout;
522         sch->parent = parentid;
523
524         if (!ops->init || ops->init(sch, NULL) == 0)
525                 return sch;
526
527         qdisc_destroy(sch);
528 errout:
529         return NULL;
530 }
531 EXPORT_SYMBOL(qdisc_create_dflt);
532
533 /* Under queue->lock and BH! */
534
535 void qdisc_reset(struct Qdisc *qdisc)
536 {
537         const struct Qdisc_ops *ops = qdisc->ops;
538
539         if (ops->reset)
540                 ops->reset(qdisc);
541 }
542 EXPORT_SYMBOL(qdisc_reset);
543
544 /* this is the rcu callback function to clean up a qdisc when there
545  * are no further references to it */
546
547 static void __qdisc_destroy(struct rcu_head *head)
548 {
549         struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
550         kfree((char *) qdisc - qdisc->padded);
551 }
552
553 /* Under queue->lock and BH! */
554
555 void qdisc_destroy(struct Qdisc *qdisc)
556 {
557         const struct Qdisc_ops  *ops = qdisc->ops;
558
559         if (qdisc->flags & TCQ_F_BUILTIN ||
560             !atomic_dec_and_test(&qdisc->refcnt))
561                 return;
562
563         list_del(&qdisc->list);
564         gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
565         if (ops->reset)
566                 ops->reset(qdisc);
567         if (ops->destroy)
568                 ops->destroy(qdisc);
569
570         module_put(ops->owner);
571         dev_put(qdisc_dev(qdisc));
572         call_rcu(&qdisc->q_rcu, __qdisc_destroy);
573 }
574 EXPORT_SYMBOL(qdisc_destroy);
575
576 static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
577 {
578         unsigned int i;
579
580         for (i = 0; i < dev->num_tx_queues; i++) {
581                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
582
583                 if (txq->qdisc_sleeping != &noop_qdisc)
584                         return false;
585         }
586         return true;
587 }
588
589 static void attach_one_default_qdisc(struct net_device *dev,
590                                      struct netdev_queue *dev_queue,
591                                      void *_unused)
592 {
593         struct Qdisc *qdisc;
594
595         if (dev->tx_queue_len) {
596                 qdisc = qdisc_create_dflt(dev, dev_queue,
597                                           &pfifo_fast_ops, TC_H_ROOT);
598                 if (!qdisc) {
599                         printk(KERN_INFO "%s: activation failed\n", dev->name);
600                         return;
601                 }
602                 list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
603         } else {
604                 qdisc =  &noqueue_qdisc;
605         }
606         dev_queue->qdisc_sleeping = qdisc;
607 }
608
609 static void transition_one_qdisc(struct net_device *dev,
610                                  struct netdev_queue *dev_queue,
611                                  void *_need_watchdog)
612 {
613         int *need_watchdog_p = _need_watchdog;
614
615         spin_lock_bh(&dev_queue->lock);
616         rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
617         if (dev_queue->qdisc != &noqueue_qdisc)
618                 *need_watchdog_p = 1;
619         spin_unlock_bh(&dev_queue->lock);
620 }
621
622 void dev_activate(struct net_device *dev)
623 {
624         int need_watchdog;
625
626         /* No queueing discipline is attached to device;
627            create default one i.e. pfifo_fast for devices,
628            which need queueing and noqueue_qdisc for
629            virtual interfaces
630          */
631
632         if (dev_all_qdisc_sleeping_noop(dev))
633                 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
634
635         if (!netif_carrier_ok(dev))
636                 /* Delay activation until next carrier-on event */
637                 return;
638
639         need_watchdog = 0;
640         netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
641
642         if (need_watchdog) {
643                 dev->trans_start = jiffies;
644                 dev_watchdog_up(dev);
645         }
646 }
647
648 static void dev_deactivate_queue(struct net_device *dev,
649                                  struct netdev_queue *dev_queue,
650                                  void *_qdisc_default)
651 {
652         struct Qdisc *qdisc_default = _qdisc_default;
653         struct sk_buff *skb = NULL;
654         struct Qdisc *qdisc;
655
656         spin_lock_bh(&dev_queue->lock);
657
658         qdisc = dev_queue->qdisc;
659         if (qdisc) {
660                 dev_queue->qdisc = qdisc_default;
661                 qdisc_reset(qdisc);
662
663                 skb = qdisc->gso_skb;
664                 qdisc->gso_skb = NULL;
665         }
666
667         spin_unlock_bh(&dev_queue->lock);
668
669         kfree_skb(skb);
670 }
671
672 static bool some_qdisc_is_running(struct net_device *dev, int lock)
673 {
674         unsigned int i;
675
676         for (i = 0; i < dev->num_tx_queues; i++) {
677                 struct netdev_queue *dev_queue;
678                 spinlock_t *root_lock;
679                 struct Qdisc *q;
680                 int val;
681
682                 dev_queue = netdev_get_tx_queue(dev, i);
683                 q = dev_queue->qdisc;
684                 root_lock = qdisc_root_lock(q);
685
686                 if (lock)
687                         spin_lock_bh(root_lock);
688
689                 val = test_bit(__QDISC_STATE_RUNNING, &q->state);
690
691                 if (lock)
692                         spin_unlock_bh(root_lock);
693
694                 if (val)
695                         return true;
696         }
697         return false;
698 }
699
700 void dev_deactivate(struct net_device *dev)
701 {
702         bool running;
703
704         netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
705
706         dev_watchdog_down(dev);
707
708         /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
709         synchronize_rcu();
710
711         /* Wait for outstanding qdisc_run calls. */
712         do {
713                 while (some_qdisc_is_running(dev, 0))
714                         yield();
715
716                 /*
717                  * Double-check inside queue lock to ensure that all effects
718                  * of the queue run are visible when we return.
719                  */
720                 running = some_qdisc_is_running(dev, 1);
721
722                 /*
723                  * The running flag should never be set at this point because
724                  * we've already set dev->qdisc to noop_qdisc *inside* the same
725                  * pair of spin locks.  That is, if any qdisc_run starts after
726                  * our initial test it should see the noop_qdisc and then
727                  * clear the RUNNING bit before dropping the queue lock.  So
728                  * if it is set here then we've found a bug.
729                  */
730         } while (WARN_ON_ONCE(running));
731 }
732
733 static void dev_init_scheduler_queue(struct net_device *dev,
734                                      struct netdev_queue *dev_queue,
735                                      void *_qdisc)
736 {
737         struct Qdisc *qdisc = _qdisc;
738
739         dev_queue->qdisc = qdisc;
740         dev_queue->qdisc_sleeping = qdisc;
741         INIT_LIST_HEAD(&dev_queue->qdisc_list);
742 }
743
744 void dev_init_scheduler(struct net_device *dev)
745 {
746         qdisc_lock_tree(dev);
747         netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
748         dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
749         qdisc_unlock_tree(dev);
750
751         setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
752 }
753
754 static void shutdown_scheduler_queue(struct net_device *dev,
755                                      struct netdev_queue *dev_queue,
756                                      void *_qdisc_default)
757 {
758         struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
759         struct Qdisc *qdisc_default = _qdisc_default;
760
761         if (qdisc) {
762                 dev_queue->qdisc = qdisc_default;
763                 dev_queue->qdisc_sleeping = qdisc_default;
764
765                 qdisc_destroy(qdisc);
766         }
767 }
768
769 void dev_shutdown(struct net_device *dev)
770 {
771         qdisc_lock_tree(dev);
772         netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
773         shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
774         BUG_TRAP(!timer_pending(&dev->watchdog_timer));
775         qdisc_unlock_tree(dev);
776 }