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