~ubuntu-branches/debian/wheezy/linux-2.6/wheezy

« back to all changes in this revision

Viewing changes to net/can/af_can.c

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings, Ben Hutchings, Aurelien Jarno, Martin Michlmayr
  • Date: 2011-04-06 13:53:30 UTC
  • mfrom: (43.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110406135330-wjufxhd0tvn3zx4z
Tags: 2.6.38-3
[ Ben Hutchings ]
* [ppc64] Add to linux-tools package architectures (Closes: #620124)
* [amd64] Save cr4 to mmu_cr4_features at boot time (Closes: #620284)
* appletalk: Fix bugs introduced when removing use of BKL
* ALSA: Fix yet another race in disconnection
* cciss: Fix lost command issue
* ath9k: Fix kernel panic in AR2427
* ses: Avoid kernel panic when lun 0 is not mapped
* PCI/ACPI: Report ASPM support to BIOS if not disabled from command line

[ Aurelien Jarno ]
* rtlwifi: fix build when PCI is not enabled.

[ Martin Michlmayr ]
* rtlwifi: Eliminate udelay calls with too large values (Closes: #620204)

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
module_param(stats_timer, int, S_IRUGO);
78
78
MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)");
79
79
 
80
 
HLIST_HEAD(can_rx_dev_list);
81
 
static struct dev_rcv_lists can_rx_alldev_list;
 
80
/* receive filters subscribed for 'all' CAN devices */
 
81
struct dev_rcv_lists can_rx_alldev_list;
82
82
static DEFINE_SPINLOCK(can_rcvlists_lock);
83
83
 
84
84
static struct kmem_cache *rcv_cache __read_mostly;
114
114
        skb_queue_purge(&sk->sk_receive_queue);
115
115
}
116
116
 
117
 
static int can_create(struct net *net, struct socket *sock, int protocol)
 
117
static int can_create(struct net *net, struct socket *sock, int protocol,
 
118
                      int kern)
118
119
{
119
120
        struct sock *sk;
120
121
        struct can_proto *cp;
125
126
        if (protocol < 0 || protocol >= CAN_NPROTO)
126
127
                return -EINVAL;
127
128
 
128
 
        if (net != &init_net)
 
129
        if (!net_eq(net, &init_net))
129
130
                return -EAFNOSUPPORT;
130
131
 
131
132
#ifdef CONFIG_MODULES
160
161
                goto errout;
161
162
        }
162
163
 
163
 
        if (cp->capability >= 0 && !capable(cp->capability)) {
164
 
                err = -EPERM;
165
 
                goto errout;
166
 
        }
167
 
 
168
164
        sock->ops = cp->ops;
169
165
 
170
166
        sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot);
296
292
 
297
293
static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev)
298
294
{
299
 
        struct dev_rcv_lists *d = NULL;
300
 
        struct hlist_node *n;
301
 
 
302
 
        /*
303
 
         * find receive list for this device
304
 
         *
305
 
         * The hlist_for_each_entry*() macros curse through the list
306
 
         * using the pointer variable n and set d to the containing
307
 
         * struct in each list iteration.  Therefore, after list
308
 
         * iteration, d is unmodified when the list is empty, and it
309
 
         * points to last list element, when the list is non-empty
310
 
         * but no match in the loop body is found.  I.e. d is *not*
311
 
         * NULL when no match is found.  We can, however, use the
312
 
         * cursor variable n to decide if a match was found.
313
 
         */
314
 
 
315
 
        hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
316
 
                if (d->dev == dev)
317
 
                        break;
318
 
        }
319
 
 
320
 
        return n ? d : NULL;
 
295
        if (!dev)
 
296
                return &can_rx_alldev_list;
 
297
        else
 
298
                return (struct dev_rcv_lists *)dev->ml_priv;
321
299
}
322
300
 
323
301
/**
379
357
                return &d->rx[RX_ALL];
380
358
 
381
359
        /* extra filterlists for the subscription of a single non-RTR can_id */
382
 
        if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS)
383
 
            && !(*can_id & CAN_RTR_FLAG)) {
 
360
        if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS) &&
 
361
            !(*can_id & CAN_RTR_FLAG)) {
384
362
 
385
363
                if (*can_id & CAN_EFF_FLAG) {
386
364
                        if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) {
437
415
 
438
416
        /* insert new receiver  (dev,canid,mask) -> (func,data) */
439
417
 
 
418
        if (dev && dev->type != ARPHRD_CAN)
 
419
                return -ENODEV;
 
420
 
440
421
        r = kmem_cache_alloc(rcv_cache, GFP_KERNEL);
441
422
        if (!r)
442
423
                return -ENOMEM;
472
453
EXPORT_SYMBOL(can_rx_register);
473
454
 
474
455
/*
475
 
 * can_rx_delete_device - rcu callback for dev_rcv_lists structure removal
476
 
 */
477
 
static void can_rx_delete_device(struct rcu_head *rp)
478
 
{
479
 
        struct dev_rcv_lists *d = container_of(rp, struct dev_rcv_lists, rcu);
480
 
 
481
 
        kfree(d);
482
 
}
483
 
 
484
 
/*
485
456
 * can_rx_delete_receiver - rcu callback for single receiver entry removal
486
457
 */
487
458
static void can_rx_delete_receiver(struct rcu_head *rp)
510
481
        struct hlist_node *next;
511
482
        struct dev_rcv_lists *d;
512
483
 
 
484
        if (dev && dev->type != ARPHRD_CAN)
 
485
                return;
 
486
 
513
487
        spin_lock(&can_rcvlists_lock);
514
488
 
515
489
        d = find_dev_rcv_lists(dev);
529
503
         */
530
504
 
531
505
        hlist_for_each_entry_rcu(r, next, rl, list) {
532
 
                if (r->can_id == can_id && r->mask == mask
533
 
                    && r->func == func && r->data == data)
 
506
                if (r->can_id == can_id && r->mask == mask &&
 
507
                    r->func == func && r->data == data)
534
508
                        break;
535
509
        }
536
510
 
545
519
                       "dev %s, id %03X, mask %03X\n",
546
520
                       DNAME(dev), can_id, mask);
547
521
                r = NULL;
548
 
                d = NULL;
549
522
                goto out;
550
523
        }
551
524
 
556
529
                can_pstats.rcv_entries--;
557
530
 
558
531
        /* remove device structure requested by NETDEV_UNREGISTER */
559
 
        if (d->remove_on_zero_entries && !d->entries)
560
 
                hlist_del_rcu(&d->list);
561
 
        else
562
 
                d = NULL;
 
532
        if (d->remove_on_zero_entries && !d->entries) {
 
533
                kfree(d);
 
534
                dev->ml_priv = NULL;
 
535
        }
563
536
 
564
537
 out:
565
538
        spin_unlock(&can_rcvlists_lock);
567
540
        /* schedule the receiver item for deletion */
568
541
        if (r)
569
542
                call_rcu(&r->rcu, can_rx_delete_receiver);
570
 
 
571
 
        /* schedule the device structure for deletion */
572
 
        if (d)
573
 
                call_rcu(&d->rcu, can_rx_delete_device);
574
543
}
575
544
EXPORT_SYMBOL(can_rx_unregister);
576
545
 
784
753
 
785
754
        case NETDEV_REGISTER:
786
755
 
787
 
                /*
788
 
                 * create new dev_rcv_lists for this device
789
 
                 *
790
 
                 * N.B. zeroing the struct is the correct initialization
791
 
                 * for the embedded hlist_head structs.
792
 
                 * Another list type, e.g. list_head, would require
793
 
                 * explicit initialization.
794
 
                 */
795
 
 
 
756
                /* create new dev_rcv_lists for this device */
796
757
                d = kzalloc(sizeof(*d), GFP_KERNEL);
797
758
                if (!d) {
798
759
                        printk(KERN_ERR
799
760
                               "can: allocation of receive list failed\n");
800
761
                        return NOTIFY_DONE;
801
762
                }
802
 
                d->dev = dev;
803
 
 
804
 
                spin_lock(&can_rcvlists_lock);
805
 
                hlist_add_head_rcu(&d->list, &can_rx_dev_list);
806
 
                spin_unlock(&can_rcvlists_lock);
 
763
                BUG_ON(dev->ml_priv);
 
764
                dev->ml_priv = d;
807
765
 
808
766
                break;
809
767
 
810
768
        case NETDEV_UNREGISTER:
811
769
                spin_lock(&can_rcvlists_lock);
812
770
 
813
 
                d = find_dev_rcv_lists(dev);
 
771
                d = dev->ml_priv;
814
772
                if (d) {
815
 
                        if (d->entries) {
 
773
                        if (d->entries)
816
774
                                d->remove_on_zero_entries = 1;
817
 
                                d = NULL;
818
 
                        } else
819
 
                                hlist_del_rcu(&d->list);
 
775
                        else {
 
776
                                kfree(d);
 
777
                                dev->ml_priv = NULL;
 
778
                        }
820
779
                } else
821
780
                        printk(KERN_ERR "can: notifier: receive list not "
822
781
                               "found for dev %s\n", dev->name);
823
782
 
824
783
                spin_unlock(&can_rcvlists_lock);
825
784
 
826
 
                if (d)
827
 
                        call_rcu(&d->rcu, can_rx_delete_device);
828
 
 
829
785
                break;
830
786
        }
831
787
 
842
798
        .func = can_rcv,
843
799
};
844
800
 
845
 
static struct net_proto_family can_family_ops __read_mostly = {
 
801
static const struct net_proto_family can_family_ops = {
846
802
        .family = PF_CAN,
847
803
        .create = can_create,
848
804
        .owner  = THIS_MODULE,
857
813
{
858
814
        printk(banner);
859
815
 
 
816
        memset(&can_rx_alldev_list, 0, sizeof(can_rx_alldev_list));
 
817
 
860
818
        rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver),
861
819
                                      0, 0, NULL);
862
820
        if (!rcv_cache)
863
821
                return -ENOMEM;
864
822
 
865
 
        /*
866
 
         * Insert can_rx_alldev_list for reception on all devices.
867
 
         * This struct is zero initialized which is correct for the
868
 
         * embedded hlist heads, the dev pointer, and the entries counter.
869
 
         */
870
 
 
871
 
        spin_lock(&can_rcvlists_lock);
872
 
        hlist_add_head_rcu(&can_rx_alldev_list.list, &can_rx_dev_list);
873
 
        spin_unlock(&can_rcvlists_lock);
874
 
 
875
823
        if (stats_timer) {
876
824
                /* the statistics are updated every second (timer triggered) */
877
825
                setup_timer(&can_stattimer, can_stat_update, 0);
891
839
 
892
840
static __exit void can_exit(void)
893
841
{
894
 
        struct dev_rcv_lists *d;
895
 
        struct hlist_node *n, *next;
 
842
        struct net_device *dev;
896
843
 
897
844
        if (stats_timer)
898
845
                del_timer(&can_stattimer);
904
851
        unregister_netdevice_notifier(&can_netdev_notifier);
905
852
        sock_unregister(PF_CAN);
906
853
 
907
 
        /* remove can_rx_dev_list */
908
 
        spin_lock(&can_rcvlists_lock);
909
 
        hlist_del(&can_rx_alldev_list.list);
910
 
        hlist_for_each_entry_safe(d, n, next, &can_rx_dev_list, list) {
911
 
                hlist_del(&d->list);
912
 
                kfree(d);
 
854
        /* remove created dev_rcv_lists from still registered CAN devices */
 
855
        rcu_read_lock();
 
856
        for_each_netdev_rcu(&init_net, dev) {
 
857
                if (dev->type == ARPHRD_CAN && dev->ml_priv){
 
858
 
 
859
                        struct dev_rcv_lists *d = dev->ml_priv;
 
860
 
 
861
                        BUG_ON(d->entries);
 
862
                        kfree(d);
 
863
                        dev->ml_priv = NULL;
 
864
                }
913
865
        }
914
 
        spin_unlock(&can_rcvlists_lock);
 
866
        rcu_read_unlock();
915
867
 
916
868
        rcu_barrier(); /* Wait for completion of call_rcu()'s */
917
869