Phonet: use per-namespace devices list

Signed-off-by: RĂ©mi Denis-Courmont <remi.denis-courmont@nokia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
diff --git a/include/net/phonet/pn_dev.h b/include/net/phonet/pn_dev.h
index 4ba2aed..5054dc5 100644
--- a/include/net/phonet/pn_dev.h
+++ b/include/net/phonet/pn_dev.h
@@ -28,7 +28,7 @@
 	spinlock_t lock;
 };
 
-extern struct phonet_device_list pndevs;
+struct phonet_device_list *phonet_device_list(struct net *net);
 
 struct phonet_device {
 	struct list_head list;
diff --git a/net/phonet/pn_dev.c b/net/phonet/pn_dev.c
index 3e24c05..80a322d 100644
--- a/net/phonet/pn_dev.c
+++ b/net/phonet/pn_dev.c
@@ -28,32 +28,41 @@
 #include <linux/netdevice.h>
 #include <linux/phonet.h>
 #include <net/sock.h>
+#include <net/netns/generic.h>
 #include <net/phonet/pn_dev.h>
 
-/* when accessing, remember to lock with spin_lock(&pndevs.lock); */
-struct phonet_device_list pndevs = {
-	.list = LIST_HEAD_INIT(pndevs.list),
-	.lock = __SPIN_LOCK_UNLOCKED(pndevs.lock),
+struct phonet_net {
+	struct phonet_device_list pndevs;
 };
 
+int phonet_net_id;
+
+struct phonet_device_list *phonet_device_list(struct net *net)
+{
+	struct phonet_net *pnn = net_generic(net, phonet_net_id);
+	return &pnn->pndevs;
+}
+
 /* Allocate new Phonet device. */
 static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
 {
+	struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
 	struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
 	if (pnd == NULL)
 		return NULL;
 	pnd->netdev = dev;
 	bitmap_zero(pnd->addrs, 64);
 
-	list_add(&pnd->list, &pndevs.list);
+	list_add(&pnd->list, &pndevs->list);
 	return pnd;
 }
 
 static struct phonet_device *__phonet_get(struct net_device *dev)
 {
+	struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
 	struct phonet_device *pnd;
 
-	list_for_each_entry(pnd, &pndevs.list, list) {
+	list_for_each_entry(pnd, &pndevs->list, list) {
 		if (pnd->netdev == dev)
 			return pnd;
 	}
@@ -68,32 +77,33 @@
 
 struct net_device *phonet_device_get(struct net *net)
 {
+	struct phonet_device_list *pndevs = phonet_device_list(net);
 	struct phonet_device *pnd;
 	struct net_device *dev;
 
-	spin_lock_bh(&pndevs.lock);
-	list_for_each_entry(pnd, &pndevs.list, list) {
+	spin_lock_bh(&pndevs->lock);
+	list_for_each_entry(pnd, &pndevs->list, list) {
 		dev = pnd->netdev;
 		BUG_ON(!dev);
 
-		if (net_eq(dev_net(dev), net) &&
-			(dev->reg_state == NETREG_REGISTERED) &&
+		if ((dev->reg_state == NETREG_REGISTERED) &&
 			((pnd->netdev->flags & IFF_UP)) == IFF_UP)
 			break;
 		dev = NULL;
 	}
 	if (dev)
 		dev_hold(dev);
-	spin_unlock_bh(&pndevs.lock);
+	spin_unlock_bh(&pndevs->lock);
 	return dev;
 }
 
 int phonet_address_add(struct net_device *dev, u8 addr)
 {
+	struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
 	struct phonet_device *pnd;
 	int err = 0;
 
-	spin_lock_bh(&pndevs.lock);
+	spin_lock_bh(&pndevs->lock);
 	/* Find or create Phonet-specific device data */
 	pnd = __phonet_get(dev);
 	if (pnd == NULL)
@@ -102,31 +112,33 @@
 		err = -ENOMEM;
 	else if (test_and_set_bit(addr >> 2, pnd->addrs))
 		err = -EEXIST;
-	spin_unlock_bh(&pndevs.lock);
+	spin_unlock_bh(&pndevs->lock);
 	return err;
 }
 
 int phonet_address_del(struct net_device *dev, u8 addr)
 {
+	struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
 	struct phonet_device *pnd;
 	int err = 0;
 
-	spin_lock_bh(&pndevs.lock);
+	spin_lock_bh(&pndevs->lock);
 	pnd = __phonet_get(dev);
 	if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
 		err = -EADDRNOTAVAIL;
 	else if (bitmap_empty(pnd->addrs, 64))
 		__phonet_device_free(pnd);
-	spin_unlock_bh(&pndevs.lock);
+	spin_unlock_bh(&pndevs->lock);
 	return err;
 }
 
 /* Gets a source address toward a destination, through a interface. */
 u8 phonet_address_get(struct net_device *dev, u8 addr)
 {
+	struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
 	struct phonet_device *pnd;
 
-	spin_lock_bh(&pndevs.lock);
+	spin_lock_bh(&pndevs->lock);
 	pnd = __phonet_get(dev);
 	if (pnd) {
 		BUG_ON(bitmap_empty(pnd->addrs, 64));
@@ -136,30 +148,31 @@
 			addr = find_first_bit(pnd->addrs, 64) << 2;
 	} else
 		addr = PN_NO_ADDR;
-	spin_unlock_bh(&pndevs.lock);
+	spin_unlock_bh(&pndevs->lock);
 	return addr;
 }
 
 int phonet_address_lookup(struct net *net, u8 addr)
 {
+	struct phonet_device_list *pndevs = phonet_device_list(net);
 	struct phonet_device *pnd;
+	int err = -EADDRNOTAVAIL;
 
-	spin_lock_bh(&pndevs.lock);
-	list_for_each_entry(pnd, &pndevs.list, list) {
-		if (!net_eq(dev_net(pnd->netdev), net))
-			continue;
+	spin_lock_bh(&pndevs->lock);
+	list_for_each_entry(pnd, &pndevs->list, list) {
 		/* Don't allow unregistering devices! */
 		if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
 				((pnd->netdev->flags & IFF_UP)) != IFF_UP)
 			continue;
 
 		if (test_bit(addr >> 2, pnd->addrs)) {
-			spin_unlock_bh(&pndevs.lock);
-			return 0;
+			err = 0;
+			goto found;
 		}
 	}
-	spin_unlock_bh(&pndevs.lock);
-	return -EADDRNOTAVAIL;
+found:
+	spin_unlock_bh(&pndevs->lock);
+	return err;
 }
 
 /* notify Phonet of device events */
@@ -169,14 +182,16 @@
 	struct net_device *dev = arg;
 
 	if (what == NETDEV_UNREGISTER) {
+		struct phonet_device_list *pndevs;
 		struct phonet_device *pnd;
 
 		/* Destroy phonet-specific device data */
-		spin_lock_bh(&pndevs.lock);
+		pndevs = phonet_device_list(dev_net(dev));
+		spin_lock_bh(&pndevs->lock);
 		pnd = __phonet_get(dev);
 		if (pnd)
 			__phonet_device_free(pnd);
-		spin_unlock_bh(&pndevs.lock);
+		spin_unlock_bh(&pndevs->lock);
 	}
 	return 0;
 
@@ -187,10 +202,41 @@
 	.priority = 0,
 };
 
+/* Per-namespace Phonet devices handling */
+static int phonet_init_net(struct net *net)
+{
+	struct phonet_net *pnn = kmalloc(sizeof(*pnn), GFP_KERNEL);
+	if (!pnn)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&pnn->pndevs.list);
+	spin_lock_init(&pnn->pndevs.lock);
+	net_assign_generic(net, phonet_net_id, pnn);
+	return 0;
+}
+
+static void phonet_exit_net(struct net *net)
+{
+	struct phonet_net *pnn = net_generic(net, phonet_net_id);
+	struct phonet_device *pnd, *n;
+
+	list_for_each_entry_safe(pnd, n, &pnn->pndevs.list, list)
+		__phonet_device_free(pnd);
+
+	kfree(pnn);
+}
+
+static struct pernet_operations phonet_net_ops = {
+	.init = phonet_init_net,
+	.exit = phonet_exit_net,
+};
+
 /* Initialize Phonet devices list */
 int __init phonet_device_init(void)
 {
-	int err;
+	int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
+	if (err)
+		return err;
 
 	register_netdevice_notifier(&phonet_device_notifier);
 	err = phonet_netlink_register();
@@ -201,11 +247,7 @@
 
 void phonet_device_exit(void)
 {
-	struct phonet_device *pnd, *n;
-
 	rtnl_unregister_all(PF_PHONET);
 	unregister_netdevice_notifier(&phonet_device_notifier);
-
-	list_for_each_entry_safe(pnd, n, &pndevs.list, list)
-		__phonet_device_free(pnd);
+	unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
 }
diff --git a/net/phonet/pn_netlink.c b/net/phonet/pn_netlink.c
index 918a4f0..1ceea1f 100644
--- a/net/phonet/pn_netlink.c
+++ b/net/phonet/pn_netlink.c
@@ -123,17 +123,16 @@
 
 static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
 {
-	struct net *net = sock_net(skb->sk);
+	struct phonet_device_list *pndevs;
 	struct phonet_device *pnd;
 	int dev_idx = 0, dev_start_idx = cb->args[0];
 	int addr_idx = 0, addr_start_idx = cb->args[1];
 
-	spin_lock_bh(&pndevs.lock);
-	list_for_each_entry(pnd, &pndevs.list, list) {
+	pndevs = phonet_device_list(sock_net(skb->sk));
+	spin_lock_bh(&pndevs->lock);
+	list_for_each_entry(pnd, &pndevs->list, list) {
 		u8 addr;
 
-		if (!net_eq(dev_net(pnd->netdev), net))
-			continue;
 		if (dev_idx > dev_start_idx)
 			addr_start_idx = 0;
 		if (dev_idx++ < dev_start_idx)
@@ -153,7 +152,7 @@
 	}
 
 out:
-	spin_unlock_bh(&pndevs.lock);
+	spin_unlock_bh(&pndevs->lock);
 	cb->args[0] = dev_idx;
 	cb->args[1] = addr_idx;