Skip to content

Commit

Permalink
Merge branch 'possible_net_t'
Browse files Browse the repository at this point in the history
Eric W. Biederman says:

====================
Introduce possible_net_t

The current usage of write_pnet and read_pnet is a little laborious and
error prone as you only notice if you failed to include them if are
compiling with network namespaces enabled.

possible_net_t remedies that by using a type that is 0 bytes when
network namespaces are disabled and can only be read and written to with
read_pnet and write_pnet.

Aka less work and safer for the same effect.

I kill hold_net and release_net first as are they are haven't been used
since 2008 and are noise at the points where write_pnet and read_pnet
are used.

I have folded in Eric Dumazets suggestions to improve the killing of
hold_net and release net.  And respon.  I had to respin anyway as
there was enough changes elsewhere in the tree the previous version
of these patches did not quite apply cleanly.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Mar 12, 2015
2 parents 6c7005f + 0c5c9fb commit 20453d8
Show file tree
Hide file tree
Showing 27 changed files with 49 additions and 155 deletions.
9 changes: 2 additions & 7 deletions include/linux/netdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -1721,9 +1721,7 @@ struct net_device {
struct netpoll_info __rcu *npinfo;
#endif

#ifdef CONFIG_NET_NS
struct net *nd_net;
#endif
possible_net_t nd_net;

/* mid-layer private */
union {
Expand Down Expand Up @@ -1863,10 +1861,7 @@ struct net *dev_net(const struct net_device *dev)
static inline
void dev_net_set(struct net_device *dev, struct net *net)
{
#ifdef CONFIG_NET_NS
release_net(dev->nd_net);
dev->nd_net = hold_net(net);
#endif
write_pnet(&dev->nd_net, net);
}

static inline bool netdev_uses_dsa(struct net_device *dev)
Expand Down
4 changes: 1 addition & 3 deletions include/net/cfg80211.h
Original file line number Diff line number Diff line change
Expand Up @@ -3183,10 +3183,8 @@ struct wiphy {
const struct ieee80211_ht_cap *ht_capa_mod_mask;
const struct ieee80211_vht_cap *vht_capa_mod_mask;

#ifdef CONFIG_NET_NS
/* the network namespace this phy lives in currently */
struct net *_net;
#endif
possible_net_t _net;

#ifdef CONFIG_CFG80211_WEXT
const struct iw_handler_def *wext;
Expand Down
9 changes: 1 addition & 8 deletions include/net/fib_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,10 @@ static inline void fib_rule_get(struct fib_rule *rule)
atomic_inc(&rule->refcnt);
}

static inline void fib_rule_put_rcu(struct rcu_head *head)
{
struct fib_rule *rule = container_of(head, struct fib_rule, rcu);
release_net(rule->fr_net);
kfree(rule);
}

static inline void fib_rule_put(struct fib_rule *rule)
{
if (atomic_dec_and_test(&rule->refcnt))
call_rcu(&rule->rcu, fib_rule_put_rcu);
kfree_rcu(rule, rcu);
}

static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
Expand Down
4 changes: 1 addition & 3 deletions include/net/genetlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ struct genl_info {
struct genlmsghdr * genlhdr;
void * userhdr;
struct nlattr ** attrs;
#ifdef CONFIG_NET_NS
struct net * _net;
#endif
possible_net_t _net;
void * user_ptr[2];
struct sock * dst_sk;
};
Expand Down
4 changes: 1 addition & 3 deletions include/net/inet_hashtables.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ struct inet_ehash_bucket {
* ports are created in O(1) time? I thought so. ;-) -DaveM
*/
struct inet_bind_bucket {
#ifdef CONFIG_NET_NS
struct net *ib_net;
#endif
possible_net_t ib_net;
unsigned short port;
signed char fastreuse;
signed char fastreuseport;
Expand Down
8 changes: 4 additions & 4 deletions include/net/ip_vs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ static inline struct net *skb_net(const struct sk_buff *skb)
* Start with the most likely hit
* End with BUG
*/
if (likely(skb->dev && skb->dev->nd_net))
if (likely(skb->dev && dev_net(skb->dev)))
return dev_net(skb->dev);
if (skb_dst(skb) && skb_dst(skb)->dev)
return dev_net(skb_dst(skb)->dev);
WARN(skb->sk, "Maybe skb_sknet should be used in %s() at line:%d\n",
__func__, __LINE__);
if (likely(skb->sk && skb->sk->sk_net))
if (likely(skb->sk && sock_net(skb->sk)))
return sock_net(skb->sk);
pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
__func__, __LINE__);
Expand All @@ -71,11 +71,11 @@ static inline struct net *skb_sknet(const struct sk_buff *skb)
#ifdef CONFIG_NET_NS
#ifdef CONFIG_IP_VS_DEBUG
/* Start with the most likely hit */
if (likely(skb->sk && skb->sk->sk_net))
if (likely(skb->sk && sock_net(skb->sk)))
return sock_net(skb->sk);
WARN(skb->dev, "Maybe skb_net should be used instead in %s() line:%d\n",
__func__, __LINE__);
if (likely(skb->dev && skb->dev->nd_net))
if (likely(skb->dev && dev_net(skb->dev)))
return dev_net(skb->dev);
pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
__func__, __LINE__);
Expand Down
8 changes: 2 additions & 6 deletions include/net/neighbour.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ enum {
};

struct neigh_parms {
#ifdef CONFIG_NET_NS
struct net *net;
#endif
possible_net_t net;
struct net_device *dev;
struct list_head list;
int (*neigh_setup)(struct neighbour *);
Expand Down Expand Up @@ -167,9 +165,7 @@ struct neigh_ops {

struct pneigh_entry {
struct pneigh_entry *next;
#ifdef CONFIG_NET_NS
struct net *net;
#endif
possible_net_t net;
struct net_device *dev;
u8 flags;
u8 key[0];
Expand Down
52 changes: 13 additions & 39 deletions include/net/net_namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ struct net {
atomic_t count; /* To decided when the network
* namespace should be shut down.
*/
#ifdef NETNS_REFCNT_DEBUG
atomic_t use_count; /* To track references we
* destroy on demand
*/
#endif
spinlock_t rules_mod_lock;

atomic64_t cookie_gen;
Expand Down Expand Up @@ -236,48 +231,27 @@ int net_eq(const struct net *net1, const struct net *net2)
#endif


#ifdef NETNS_REFCNT_DEBUG
static inline struct net *hold_net(struct net *net)
{
if (net)
atomic_inc(&net->use_count);
return net;
}

static inline void release_net(struct net *net)
{
if (net)
atomic_dec(&net->use_count);
}
#else
static inline struct net *hold_net(struct net *net)
{
return net;
}

static inline void release_net(struct net *net)
{
}
#endif

typedef struct {
#ifdef CONFIG_NET_NS
struct net *net;
#endif
} possible_net_t;

static inline void write_pnet(struct net **pnet, struct net *net)
static inline void write_pnet(possible_net_t *pnet, struct net *net)
{
*pnet = net;
#ifdef CONFIG_NET_NS
pnet->net = net;
#endif
}

static inline struct net *read_pnet(struct net * const *pnet)
static inline struct net *read_pnet(const possible_net_t *pnet)
{
return *pnet;
}

#ifdef CONFIG_NET_NS
return pnet->net;
#else

#define write_pnet(pnet, net) do { (void)(net);} while (0)
#define read_pnet(pnet) (&init_net)

return &init_net;
#endif
}

#define for_each_net(VAR) \
list_for_each_entry(VAR, &net_namespace_list, list)
Expand Down
5 changes: 2 additions & 3 deletions include/net/netfilter/nf_conntrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ struct nf_conn {
/* Timer function; drops refcnt when it goes off. */
struct timer_list timeout;

#ifdef CONFIG_NET_NS
struct net *ct_net;
#endif
possible_net_t ct_net;

/* all members below initialized via memset */
u8 __nfct_init_offset[0];

Expand Down
6 changes: 2 additions & 4 deletions include/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ struct sock_common {
struct hlist_nulls_node skc_portaddr_node;
};
struct proto *skc_prot;
#ifdef CONFIG_NET_NS
struct net *skc_net;
#endif
possible_net_t skc_net;

#if IS_ENABLED(CONFIG_IPV6)
struct in6_addr skc_v6_daddr;
Expand Down Expand Up @@ -2204,7 +2202,7 @@ static inline void sk_change_net(struct sock *sk, struct net *net)

if (!net_eq(current_net, net)) {
put_net(current_net);
sock_net_set(sk, hold_net(net));
sock_net_set(sk, net);
}
}

Expand Down
8 changes: 2 additions & 6 deletions include/net/xfrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ struct xfrm_state_walk {

/* Full description of state of transformer. */
struct xfrm_state {
#ifdef CONFIG_NET_NS
struct net *xs_net;
#endif
possible_net_t xs_net;
union {
struct hlist_node gclist;
struct hlist_node bydst;
Expand Down Expand Up @@ -522,9 +520,7 @@ struct xfrm_policy_queue {
};

struct xfrm_policy {
#ifdef CONFIG_NET_NS
struct net *xp_net;
#endif
possible_net_t xp_net;
struct hlist_node bydst;
struct hlist_node byidx;

Expand Down
4 changes: 2 additions & 2 deletions net/9p/trans_fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
sin_server.sin_family = AF_INET;
sin_server.sin_addr.s_addr = in_aton(addr);
sin_server.sin_port = htons(opts.port);
err = __sock_create(read_pnet(&current->nsproxy->net_ns), PF_INET,
err = __sock_create(current->nsproxy->net_ns, PF_INET,
SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
if (err) {
pr_err("%s (%d): problem creating socket\n",
Expand Down Expand Up @@ -988,7 +988,7 @@ p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)

sun_server.sun_family = PF_UNIX;
strcpy(sun_server.sun_path, addr);
err = __sock_create(read_pnet(&current->nsproxy->net_ns), PF_UNIX,
err = __sock_create(current->nsproxy->net_ns, PF_UNIX,
SOCK_STREAM, 0, &csocket, 1);
if (err < 0) {
pr_err("%s (%d): problem creating socket\n",
Expand Down
2 changes: 0 additions & 2 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -6841,8 +6841,6 @@ void free_netdev(struct net_device *dev)
{
struct napi_struct *p, *n;

release_net(dev_net(dev));

netif_free_tx_queues(dev);
#ifdef CONFIG_SYSFS
kvfree(dev->_rx);
Expand Down
17 changes: 3 additions & 14 deletions net/core/fib_rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
r->pref = pref;
r->table = table;
r->flags = flags;
r->fr_net = hold_net(ops->fro_net);
r->fr_net = ops->fro_net;

r->suppress_prefixlen = -1;
r->suppress_ifgroup = -1;
Expand Down Expand Up @@ -116,7 +116,6 @@ static int __fib_rules_register(struct fib_rules_ops *ops)
if (ops->family == o->family)
goto errout;

hold_net(net);
list_add_tail_rcu(&ops->list, &net->rules_ops);
err = 0;
errout:
Expand Down Expand Up @@ -160,15 +159,6 @@ static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
}
}

static void fib_rules_put_rcu(struct rcu_head *head)
{
struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
struct net *net = ops->fro_net;

release_net(net);
kfree(ops);
}

void fib_rules_unregister(struct fib_rules_ops *ops)
{
struct net *net = ops->fro_net;
Expand All @@ -178,7 +168,7 @@ void fib_rules_unregister(struct fib_rules_ops *ops)
fib_rules_cleanup_ops(ops);
spin_unlock(&net->rules_mod_lock);

call_rcu(&ops->rcu, fib_rules_put_rcu);
kfree_rcu(ops, rcu);
}
EXPORT_SYMBOL_GPL(fib_rules_unregister);

Expand Down Expand Up @@ -303,7 +293,7 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
err = -ENOMEM;
goto errout;
}
rule->fr_net = hold_net(net);
rule->fr_net = net;

if (tb[FRA_PRIORITY])
rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
Expand Down Expand Up @@ -423,7 +413,6 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
return 0;

errout_free:
release_net(rule->fr_net);
kfree(rule);
errout:
rules_ops_put(ops);
Expand Down
9 changes: 2 additions & 7 deletions net/core/neighbour.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
if (!n)
goto out;

write_pnet(&n->net, hold_net(net));
write_pnet(&n->net, net);
memcpy(n->key, pkey, key_len);
n->dev = dev;
if (dev)
Expand All @@ -600,7 +600,6 @@ struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
if (tbl->pconstructor && tbl->pconstructor(n)) {
if (dev)
dev_put(dev);
release_net(net);
kfree(n);
n = NULL;
goto out;
Expand Down Expand Up @@ -634,7 +633,6 @@ int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
tbl->pdestructor(n);
if (n->dev)
dev_put(n->dev);
release_net(pneigh_net(n));
kfree(n);
return 0;
}
Expand All @@ -657,7 +655,6 @@ static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
tbl->pdestructor(n);
if (n->dev)
dev_put(n->dev);
release_net(pneigh_net(n));
kfree(n);
continue;
}
Expand Down Expand Up @@ -1428,11 +1425,10 @@ struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
dev_hold(dev);
p->dev = dev;
write_pnet(&p->net, hold_net(net));
write_pnet(&p->net, net);
p->sysctl_table = NULL;

if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
release_net(net);
dev_put(dev);
kfree(p);
return NULL;
Expand Down Expand Up @@ -1472,7 +1468,6 @@ EXPORT_SYMBOL(neigh_parms_release);

static void neigh_parms_destroy(struct neigh_parms *parms)
{
release_net(neigh_parms_net(parms));
kfree(parms);
}

Expand Down
Loading

0 comments on commit 20453d8

Please sign in to comment.