mirror of
https://github.com/adulau/aha.git
synced 2024-12-28 03:36:19 +00:00
Phonet: convert devices list to RCU
Signed-off-by: Rémi Denis-Courmont <remi.denis-courmont@nokia.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
parent
d90310243f
commit
eeb74a9d45
3 changed files with 45 additions and 26 deletions
|
@ -25,7 +25,7 @@
|
||||||
|
|
||||||
struct phonet_device_list {
|
struct phonet_device_list {
|
||||||
struct list_head list;
|
struct list_head list;
|
||||||
spinlock_t lock;
|
struct mutex lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct phonet_device_list *phonet_device_list(struct net *net);
|
struct phonet_device_list *phonet_device_list(struct net *net);
|
||||||
|
|
|
@ -61,7 +61,8 @@ static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
|
||||||
pnd->netdev = dev;
|
pnd->netdev = dev;
|
||||||
bitmap_zero(pnd->addrs, 64);
|
bitmap_zero(pnd->addrs, 64);
|
||||||
|
|
||||||
list_add(&pnd->list, &pndevs->list);
|
BUG_ON(!mutex_is_locked(&pndevs->lock));
|
||||||
|
list_add_rcu(&pnd->list, &pndevs->list);
|
||||||
return pnd;
|
return pnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,6 +71,7 @@ static struct phonet_device *__phonet_get(struct net_device *dev)
|
||||||
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
|
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
|
||||||
struct phonet_device *pnd;
|
struct phonet_device *pnd;
|
||||||
|
|
||||||
|
BUG_ON(!mutex_is_locked(&pndevs->lock));
|
||||||
list_for_each_entry(pnd, &pndevs->list, list) {
|
list_for_each_entry(pnd, &pndevs->list, list) {
|
||||||
if (pnd->netdev == dev)
|
if (pnd->netdev == dev)
|
||||||
return pnd;
|
return pnd;
|
||||||
|
@ -77,6 +79,18 @@ static struct phonet_device *__phonet_get(struct net_device *dev)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct phonet_device *__phonet_get_rcu(struct net_device *dev)
|
||||||
|
{
|
||||||
|
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
|
||||||
|
struct phonet_device *pnd;
|
||||||
|
|
||||||
|
list_for_each_entry_rcu(pnd, &pndevs->list, list) {
|
||||||
|
if (pnd->netdev == dev)
|
||||||
|
return pnd;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static void phonet_device_destroy(struct net_device *dev)
|
static void phonet_device_destroy(struct net_device *dev)
|
||||||
{
|
{
|
||||||
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
|
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
|
||||||
|
@ -84,11 +98,11 @@ static void phonet_device_destroy(struct net_device *dev)
|
||||||
|
|
||||||
ASSERT_RTNL();
|
ASSERT_RTNL();
|
||||||
|
|
||||||
spin_lock_bh(&pndevs->lock);
|
mutex_lock(&pndevs->lock);
|
||||||
pnd = __phonet_get(dev);
|
pnd = __phonet_get(dev);
|
||||||
if (pnd)
|
if (pnd)
|
||||||
list_del(&pnd->list);
|
list_del_rcu(&pnd->list);
|
||||||
spin_unlock_bh(&pndevs->lock);
|
mutex_unlock(&pndevs->lock);
|
||||||
|
|
||||||
if (pnd) {
|
if (pnd) {
|
||||||
u8 addr;
|
u8 addr;
|
||||||
|
@ -106,8 +120,8 @@ struct net_device *phonet_device_get(struct net *net)
|
||||||
struct phonet_device *pnd;
|
struct phonet_device *pnd;
|
||||||
struct net_device *dev = NULL;
|
struct net_device *dev = NULL;
|
||||||
|
|
||||||
spin_lock_bh(&pndevs->lock);
|
rcu_read_lock();
|
||||||
list_for_each_entry(pnd, &pndevs->list, list) {
|
list_for_each_entry_rcu(pnd, &pndevs->list, list) {
|
||||||
dev = pnd->netdev;
|
dev = pnd->netdev;
|
||||||
BUG_ON(!dev);
|
BUG_ON(!dev);
|
||||||
|
|
||||||
|
@ -118,7 +132,7 @@ struct net_device *phonet_device_get(struct net *net)
|
||||||
}
|
}
|
||||||
if (dev)
|
if (dev)
|
||||||
dev_hold(dev);
|
dev_hold(dev);
|
||||||
spin_unlock_bh(&pndevs->lock);
|
rcu_read_unlock();
|
||||||
return dev;
|
return dev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,7 +142,7 @@ int phonet_address_add(struct net_device *dev, u8 addr)
|
||||||
struct phonet_device *pnd;
|
struct phonet_device *pnd;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
spin_lock_bh(&pndevs->lock);
|
mutex_lock(&pndevs->lock);
|
||||||
/* Find or create Phonet-specific device data */
|
/* Find or create Phonet-specific device data */
|
||||||
pnd = __phonet_get(dev);
|
pnd = __phonet_get(dev);
|
||||||
if (pnd == NULL)
|
if (pnd == NULL)
|
||||||
|
@ -137,7 +151,7 @@ int phonet_address_add(struct net_device *dev, u8 addr)
|
||||||
err = -ENOMEM;
|
err = -ENOMEM;
|
||||||
else if (test_and_set_bit(addr >> 2, pnd->addrs))
|
else if (test_and_set_bit(addr >> 2, pnd->addrs))
|
||||||
err = -EEXIST;
|
err = -EEXIST;
|
||||||
spin_unlock_bh(&pndevs->lock);
|
mutex_unlock(&pndevs->lock);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,27 +161,32 @@ int phonet_address_del(struct net_device *dev, u8 addr)
|
||||||
struct phonet_device *pnd;
|
struct phonet_device *pnd;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
spin_lock_bh(&pndevs->lock);
|
mutex_lock(&pndevs->lock);
|
||||||
pnd = __phonet_get(dev);
|
pnd = __phonet_get(dev);
|
||||||
if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
|
if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) {
|
||||||
err = -EADDRNOTAVAIL;
|
err = -EADDRNOTAVAIL;
|
||||||
else if (bitmap_empty(pnd->addrs, 64)) {
|
pnd = NULL;
|
||||||
list_del(&pnd->list);
|
} else if (bitmap_empty(pnd->addrs, 64))
|
||||||
|
list_del_rcu(&pnd->list);
|
||||||
|
else
|
||||||
|
pnd = NULL;
|
||||||
|
mutex_unlock(&pndevs->lock);
|
||||||
|
|
||||||
|
if (pnd) {
|
||||||
|
synchronize_rcu();
|
||||||
kfree(pnd);
|
kfree(pnd);
|
||||||
}
|
}
|
||||||
spin_unlock_bh(&pndevs->lock);
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Gets a source address toward a destination, through a interface. */
|
/* Gets a source address toward a destination, through a interface. */
|
||||||
u8 phonet_address_get(struct net_device *dev, u8 daddr)
|
u8 phonet_address_get(struct net_device *dev, u8 daddr)
|
||||||
{
|
{
|
||||||
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
|
|
||||||
struct phonet_device *pnd;
|
struct phonet_device *pnd;
|
||||||
u8 saddr;
|
u8 saddr;
|
||||||
|
|
||||||
spin_lock_bh(&pndevs->lock);
|
rcu_read_lock();
|
||||||
pnd = __phonet_get(dev);
|
pnd = __phonet_get_rcu(dev);
|
||||||
if (pnd) {
|
if (pnd) {
|
||||||
BUG_ON(bitmap_empty(pnd->addrs, 64));
|
BUG_ON(bitmap_empty(pnd->addrs, 64));
|
||||||
|
|
||||||
|
@ -178,7 +197,7 @@ u8 phonet_address_get(struct net_device *dev, u8 daddr)
|
||||||
saddr = find_first_bit(pnd->addrs, 64) << 2;
|
saddr = find_first_bit(pnd->addrs, 64) << 2;
|
||||||
} else
|
} else
|
||||||
saddr = PN_NO_ADDR;
|
saddr = PN_NO_ADDR;
|
||||||
spin_unlock_bh(&pndevs->lock);
|
rcu_read_unlock();
|
||||||
|
|
||||||
if (saddr == PN_NO_ADDR) {
|
if (saddr == PN_NO_ADDR) {
|
||||||
/* Fallback to another device */
|
/* Fallback to another device */
|
||||||
|
@ -200,8 +219,8 @@ int phonet_address_lookup(struct net *net, u8 addr)
|
||||||
struct phonet_device *pnd;
|
struct phonet_device *pnd;
|
||||||
int err = -EADDRNOTAVAIL;
|
int err = -EADDRNOTAVAIL;
|
||||||
|
|
||||||
spin_lock_bh(&pndevs->lock);
|
rcu_read_lock();
|
||||||
list_for_each_entry(pnd, &pndevs->list, list) {
|
list_for_each_entry_rcu(pnd, &pndevs->list, list) {
|
||||||
/* Don't allow unregistering devices! */
|
/* Don't allow unregistering devices! */
|
||||||
if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
|
if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
|
||||||
((pnd->netdev->flags & IFF_UP)) != IFF_UP)
|
((pnd->netdev->flags & IFF_UP)) != IFF_UP)
|
||||||
|
@ -213,7 +232,7 @@ int phonet_address_lookup(struct net *net, u8 addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
found:
|
found:
|
||||||
spin_unlock_bh(&pndevs->lock);
|
rcu_read_unlock();
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,7 +323,7 @@ static int phonet_init_net(struct net *net)
|
||||||
}
|
}
|
||||||
|
|
||||||
INIT_LIST_HEAD(&pnn->pndevs.list);
|
INIT_LIST_HEAD(&pnn->pndevs.list);
|
||||||
spin_lock_init(&pnn->pndevs.lock);
|
mutex_init(&pnn->pndevs.lock);
|
||||||
mutex_init(&pnn->routes.lock);
|
mutex_init(&pnn->routes.lock);
|
||||||
net_assign_generic(net, phonet_net_id, pnn);
|
net_assign_generic(net, phonet_net_id, pnn);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -131,8 +131,8 @@ static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
|
||||||
int addr_idx = 0, addr_start_idx = cb->args[1];
|
int addr_idx = 0, addr_start_idx = cb->args[1];
|
||||||
|
|
||||||
pndevs = phonet_device_list(sock_net(skb->sk));
|
pndevs = phonet_device_list(sock_net(skb->sk));
|
||||||
spin_lock_bh(&pndevs->lock);
|
rcu_read_lock();
|
||||||
list_for_each_entry(pnd, &pndevs->list, list) {
|
list_for_each_entry_rcu(pnd, &pndevs->list, list) {
|
||||||
u8 addr;
|
u8 addr;
|
||||||
|
|
||||||
if (dev_idx > dev_start_idx)
|
if (dev_idx > dev_start_idx)
|
||||||
|
@ -154,7 +154,7 @@ static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
spin_unlock_bh(&pndevs->lock);
|
rcu_read_unlock();
|
||||||
cb->args[0] = dev_idx;
|
cb->args[0] = dev_idx;
|
||||||
cb->args[1] = addr_idx;
|
cb->args[1] = addr_idx;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue