netdevice: Move qdisc_list back into net_device proper.
[safe/jmp/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         const struct Qdisc_ops  *ops = qdisc->ops;
549
550         gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
551         if (ops->reset)
552                 ops->reset(qdisc);
553         if (ops->destroy)
554                 ops->destroy(qdisc);
555
556         module_put(ops->owner);
557         dev_put(qdisc_dev(qdisc));
558
559         kfree((char *) qdisc - qdisc->padded);
560 }
561
562 /* Under queue->lock and BH! */
563
564 void qdisc_destroy(struct Qdisc *qdisc)
565 {
566         struct net_device *dev = qdisc_dev(qdisc);
567
568         if (qdisc->flags & TCQ_F_BUILTIN ||
569             !atomic_dec_and_test(&qdisc->refcnt))
570                 return;
571
572         spin_lock_bh(&dev->qdisc_list_lock);
573         list_del(&qdisc->list);
574         spin_unlock_bh(&dev->qdisc_list_lock);
575
576         call_rcu(&qdisc->q_rcu, __qdisc_destroy);
577 }
578 EXPORT_SYMBOL(qdisc_destroy);
579
580 static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
581 {
582         unsigned int i;
583
584         for (i = 0; i < dev->num_tx_queues; i++) {
585                 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
586
587                 if (txq->qdisc_sleeping != &noop_qdisc)
588                         return false;
589         }
590         return true;
591 }
592
593 static void attach_one_default_qdisc(struct net_device *dev,
594                                      struct netdev_queue *dev_queue,
595                                      void *_unused)
596 {
597         struct Qdisc *qdisc;
598
599         if (dev->tx_queue_len) {
600                 qdisc = qdisc_create_dflt(dev, dev_queue,
601                                           &pfifo_fast_ops, TC_H_ROOT);
602                 if (!qdisc) {
603                         printk(KERN_INFO "%s: activation failed\n", dev->name);
604                         return;
605                 }
606                 spin_lock_bh(&dev->qdisc_list_lock);
607                 list_add_tail(&qdisc->list, &dev->qdisc_list);
608                 spin_unlock_bh(&dev->qdisc_list_lock);
609         } else {
610                 qdisc =  &noqueue_qdisc;
611         }
612         dev_queue->qdisc_sleeping = qdisc;
613 }
614
615 static void transition_one_qdisc(struct net_device *dev,
616                                  struct netdev_queue *dev_queue,
617                                  void *_need_watchdog)
618 {
619         int *need_watchdog_p = _need_watchdog;
620
621         spin_lock_bh(&dev_queue->lock);
622         rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
623         if (dev_queue->qdisc != &noqueue_qdisc)
624                 *need_watchdog_p = 1;
625         spin_unlock_bh(&dev_queue->lock);
626 }
627
628 void dev_activate(struct net_device *dev)
629 {
630         int need_watchdog;
631
632         /* No queueing discipline is attached to device;
633            create default one i.e. pfifo_fast for devices,
634            which need queueing and noqueue_qdisc for
635            virtual interfaces
636          */
637
638         if (dev_all_qdisc_sleeping_noop(dev))
639                 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
640
641         if (!netif_carrier_ok(dev))
642                 /* Delay activation until next carrier-on event */
643                 return;
644
645         need_watchdog = 0;
646         netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
647
648         if (need_watchdog) {
649                 dev->trans_start = jiffies;
650                 dev_watchdog_up(dev);
651         }
652 }
653
654 static void dev_deactivate_queue(struct net_device *dev,
655                                  struct netdev_queue *dev_queue,
656                                  void *_qdisc_default)
657 {
658         struct Qdisc *qdisc_default = _qdisc_default;
659         struct sk_buff *skb = NULL;
660         struct Qdisc *qdisc;
661
662         spin_lock_bh(&dev_queue->lock);
663
664         qdisc = dev_queue->qdisc;
665         if (qdisc) {
666                 dev_queue->qdisc = qdisc_default;
667                 qdisc_reset(qdisc);
668
669                 skb = qdisc->gso_skb;
670                 qdisc->gso_skb = NULL;
671         }
672
673         spin_unlock_bh(&dev_queue->lock);
674
675         kfree_skb(skb);
676 }
677
678 static bool some_qdisc_is_running(struct net_device *dev, int lock)
679 {
680         unsigned int i;
681
682         for (i = 0; i < dev->num_tx_queues; i++) {
683                 struct netdev_queue *dev_queue;
684                 spinlock_t *root_lock;
685                 struct Qdisc *q;
686                 int val;
687
688                 dev_queue = netdev_get_tx_queue(dev, i);
689                 q = dev_queue->qdisc;
690                 root_lock = qdisc_root_lock(q);
691
692                 if (lock)
693                         spin_lock_bh(root_lock);
694
695                 val = test_bit(__QDISC_STATE_RUNNING, &q->state);
696
697                 if (lock)
698                         spin_unlock_bh(root_lock);
699
700                 if (val)
701                         return true;
702         }
703         return false;
704 }
705
706 void dev_deactivate(struct net_device *dev)
707 {
708         bool running;
709
710         netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
711
712         dev_watchdog_down(dev);
713
714         /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
715         synchronize_rcu();
716
717         /* Wait for outstanding qdisc_run calls. */
718         do {
719                 while (some_qdisc_is_running(dev, 0))
720                         yield();
721
722                 /*
723                  * Double-check inside queue lock to ensure that all effects
724                  * of the queue run are visible when we return.
725                  */
726                 running = some_qdisc_is_running(dev, 1);
727
728                 /*
729                  * The running flag should never be set at this point because
730                  * we've already set dev->qdisc to noop_qdisc *inside* the same
731                  * pair of spin locks.  That is, if any qdisc_run starts after
732                  * our initial test it should see the noop_qdisc and then
733                  * clear the RUNNING bit before dropping the queue lock.  So
734                  * if it is set here then we've found a bug.
735                  */
736         } while (WARN_ON_ONCE(running));
737 }
738
739 static void dev_init_scheduler_queue(struct net_device *dev,
740                                      struct netdev_queue *dev_queue,
741                                      void *_qdisc)
742 {
743         struct Qdisc *qdisc = _qdisc;
744
745         dev_queue->qdisc = qdisc;
746         dev_queue->qdisc_sleeping = qdisc;
747 }
748
749 void dev_init_scheduler(struct net_device *dev)
750 {
751         netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
752         dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
753
754         setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
755 }
756
757 static void shutdown_scheduler_queue(struct net_device *dev,
758                                      struct netdev_queue *dev_queue,
759                                      void *_qdisc_default)
760 {
761         struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
762         struct Qdisc *qdisc_default = _qdisc_default;
763
764         if (qdisc) {
765                 spinlock_t *root_lock = qdisc_root_lock(qdisc);
766
767                 dev_queue->qdisc = qdisc_default;
768                 dev_queue->qdisc_sleeping = qdisc_default;
769
770                 spin_lock(root_lock);
771                 qdisc_destroy(qdisc);
772                 spin_unlock(root_lock);
773         }
774 }
775
776 void dev_shutdown(struct net_device *dev)
777 {
778         netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
779         shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
780         BUG_TRAP(!timer_pending(&dev->watchdog_timer));
781 }