Skip to content

Commit

Permalink
percpu: add __percpu sparse annotations to net
Browse files Browse the repository at this point in the history
Add __percpu sparse annotations to net.

These annotations are to make sparse consider percpu variables to be
in a different address space and warn if accessed without going
through percpu accessors.  This patch doesn't affect normal builds.

The macro and type tricks around snmp stats make things a bit
interesting.  DEFINE/DECLARE_SNMP_STAT() macros mark the target field
as __percpu and SNMP_UPD_PO_STATS() macro is updated accordingly.  All
snmp_mib_*() users which used to cast the argument to (void **) are
updated to cast it to (void __percpu **).

Signed-off-by: Tejun Heo <[email protected]>
Acked-by: David S. Miller <[email protected]>
Cc: Patrick McHardy <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Vlad Yasevich <[email protected]>
Cc: [email protected]
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
htejun authored and davem330 committed Feb 17, 2010
1 parent 2bb4646 commit 7d720c3
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 114 deletions.
6 changes: 3 additions & 3 deletions include/net/ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ extern struct ipv4_config ipv4_config;
#define NET_ADD_STATS_BH(net, field, adnd) SNMP_ADD_STATS_BH((net)->mib.net_statistics, field, adnd)
#define NET_ADD_STATS_USER(net, field, adnd) SNMP_ADD_STATS_USER((net)->mib.net_statistics, field, adnd)

extern unsigned long snmp_fold_field(void *mib[], int offt);
extern int snmp_mib_init(void *ptr[2], size_t mibsize);
extern void snmp_mib_free(void *ptr[2]);
extern unsigned long snmp_fold_field(void __percpu *mib[], int offt);
extern int snmp_mib_init(void __percpu *ptr[2], size_t mibsize);
extern void snmp_mib_free(void __percpu *ptr[2]);

extern struct local_ports {
seqlock_t lock;
Expand Down
2 changes: 1 addition & 1 deletion include/net/ipcomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct crypto_comp;

struct ipcomp_data {
u16 threshold;
struct crypto_comp **tfms;
struct crypto_comp * __percpu *tfms;
};

struct ip_comp_hdr;
Expand Down
2 changes: 1 addition & 1 deletion include/net/neighbour.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct neigh_table {
rwlock_t lock;
unsigned long last_rand;
struct kmem_cache *kmem_cachep;
struct neigh_statistics *stats;
struct neigh_statistics __percpu *stats;
struct neighbour **hash_buckets;
unsigned int hash_mask;
__u32 hash_rnd;
Expand Down
2 changes: 1 addition & 1 deletion include/net/netns/conntrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct netns_ct {
struct hlist_head *expect_hash;
struct hlist_nulls_head unconfirmed;
struct hlist_nulls_head dying;
struct ip_conntrack_stat *stat;
struct ip_conntrack_stat __percpu *stat;
int sysctl_events;
unsigned int sysctl_events_retry_timeout;
int sysctl_acct;
Expand Down
2 changes: 1 addition & 1 deletion include/net/netns/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct netns_core {

int sysctl_somaxconn;

struct prot_inuse *inuse;
struct prot_inuse __percpu *inuse;
};

#endif
2 changes: 1 addition & 1 deletion include/net/route.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct rt_cache_stat {
unsigned int out_hlist_search;
};

extern struct ip_rt_acct *ip_rt_acct;
extern struct ip_rt_acct __percpu *ip_rt_acct;

struct in_device;
extern int ip_rt_init(void);
Expand Down
12 changes: 8 additions & 4 deletions include/net/snmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ struct linux_xfrm_mib {
* nonlocked_atomic_inc() primitives -AK
*/
#define DEFINE_SNMP_STAT(type, name) \
__typeof__(type) *name[2]
__typeof__(type) __percpu *name[2]
#define DECLARE_SNMP_STAT(type, name) \
extern __typeof__(type) *name[2]
extern __typeof__(type) __percpu *name[2]

#define SNMP_STAT_BHPTR(name) (name[0])
#define SNMP_STAT_USRPTR(name) (name[1])
Expand All @@ -148,9 +148,13 @@ struct linux_xfrm_mib {
__this_cpu_add(mib[0]->mibs[field], addend)
#define SNMP_ADD_STATS_USER(mib, field, addend) \
this_cpu_add(mib[1]->mibs[field], addend)
/*
* Use "__typeof__(*mib[0]) *ptr" instead of "__typeof__(mib[0]) ptr"
* to make @ptr a non-percpu pointer.
*/
#define SNMP_UPD_PO_STATS(mib, basefield, addend) \
do { \
__typeof__(mib[0]) ptr; \
__typeof__(*mib[0]) *ptr; \
preempt_disable(); \
ptr = this_cpu_ptr((mib)[!in_softirq()]); \
ptr->mibs[basefield##PKTS]++; \
Expand All @@ -159,7 +163,7 @@ struct linux_xfrm_mib {
} while (0)
#define SNMP_UPD_PO_STATS_BH(mib, basefield, addend) \
do { \
__typeof__(mib[0]) ptr = \
__typeof__(*mib[0]) *ptr = \
__this_cpu_ptr((mib)[!in_softirq()]); \
ptr->mibs[basefield##PKTS]++; \
ptr->mibs[basefield##OCTETS] += addend;\
Expand Down
2 changes: 1 addition & 1 deletion include/net/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ extern int tcp_v4_md5_do_del(struct sock *sk,
#define tcp_twsk_md5_key(twsk) NULL
#endif

extern struct tcp_md5sig_pool **tcp_alloc_md5sig_pool(struct sock *);
extern struct tcp_md5sig_pool * __percpu *tcp_alloc_md5sig_pool(struct sock *);
extern void tcp_free_md5sig_pool(void);

extern struct tcp_md5sig_pool *__tcp_get_md5sig_pool(int cpu);
Expand Down
2 changes: 1 addition & 1 deletion net/8021q/vlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct vlan_dev_info {
struct proc_dir_entry *dent;
unsigned long cnt_inc_headroom_on_tx;
unsigned long cnt_encap_on_xmit;
struct vlan_rx_stats *vlan_rx_stats;
struct vlan_rx_stats __percpu *vlan_rx_stats;
};

static inline struct vlan_dev_info *vlan_dev_info(const struct net_device *dev)
Expand Down
5 changes: 3 additions & 2 deletions net/dccp/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1005,12 +1005,13 @@ EXPORT_SYMBOL_GPL(dccp_shutdown);

static inline int dccp_mib_init(void)
{
return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
return snmp_mib_init((void __percpu **)dccp_statistics,
sizeof(struct dccp_mib));
}

static inline void dccp_mib_exit(void)
{
snmp_mib_free((void**)dccp_statistics);
snmp_mib_free((void __percpu **)dccp_statistics);
}

static int thash_entries;
Expand Down
46 changes: 23 additions & 23 deletions net/ipv4/af_inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ int inet_ctl_sock_create(struct sock **sk, unsigned short family,
}
EXPORT_SYMBOL_GPL(inet_ctl_sock_create);

unsigned long snmp_fold_field(void *mib[], int offt)
unsigned long snmp_fold_field(void __percpu *mib[], int offt)
{
unsigned long res = 0;
int i;
Expand All @@ -1398,7 +1398,7 @@ unsigned long snmp_fold_field(void *mib[], int offt)
}
EXPORT_SYMBOL_GPL(snmp_fold_field);

int snmp_mib_init(void *ptr[2], size_t mibsize)
int snmp_mib_init(void __percpu *ptr[2], size_t mibsize)
{
BUG_ON(ptr == NULL);
ptr[0] = __alloc_percpu(mibsize, __alignof__(unsigned long long));
Expand All @@ -1416,7 +1416,7 @@ int snmp_mib_init(void *ptr[2], size_t mibsize)
}
EXPORT_SYMBOL_GPL(snmp_mib_init);

void snmp_mib_free(void *ptr[2])
void snmp_mib_free(void __percpu *ptr[2])
{
BUG_ON(ptr == NULL);
free_percpu(ptr[0]);
Expand Down Expand Up @@ -1460,56 +1460,56 @@ static const struct net_protocol icmp_protocol = {

static __net_init int ipv4_mib_init_net(struct net *net)
{
if (snmp_mib_init((void **)net->mib.tcp_statistics,
if (snmp_mib_init((void __percpu **)net->mib.tcp_statistics,
sizeof(struct tcp_mib)) < 0)
goto err_tcp_mib;
if (snmp_mib_init((void **)net->mib.ip_statistics,
if (snmp_mib_init((void __percpu **)net->mib.ip_statistics,
sizeof(struct ipstats_mib)) < 0)
goto err_ip_mib;
if (snmp_mib_init((void **)net->mib.net_statistics,
if (snmp_mib_init((void __percpu **)net->mib.net_statistics,
sizeof(struct linux_mib)) < 0)
goto err_net_mib;
if (snmp_mib_init((void **)net->mib.udp_statistics,
if (snmp_mib_init((void __percpu **)net->mib.udp_statistics,
sizeof(struct udp_mib)) < 0)
goto err_udp_mib;
if (snmp_mib_init((void **)net->mib.udplite_statistics,
if (snmp_mib_init((void __percpu **)net->mib.udplite_statistics,
sizeof(struct udp_mib)) < 0)
goto err_udplite_mib;
if (snmp_mib_init((void **)net->mib.icmp_statistics,
if (snmp_mib_init((void __percpu **)net->mib.icmp_statistics,
sizeof(struct icmp_mib)) < 0)
goto err_icmp_mib;
if (snmp_mib_init((void **)net->mib.icmpmsg_statistics,
if (snmp_mib_init((void __percpu **)net->mib.icmpmsg_statistics,
sizeof(struct icmpmsg_mib)) < 0)
goto err_icmpmsg_mib;

tcp_mib_init(net);
return 0;

err_icmpmsg_mib:
snmp_mib_free((void **)net->mib.icmp_statistics);
snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
err_icmp_mib:
snmp_mib_free((void **)net->mib.udplite_statistics);
snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
err_udplite_mib:
snmp_mib_free((void **)net->mib.udp_statistics);
snmp_mib_free((void __percpu **)net->mib.udp_statistics);
err_udp_mib:
snmp_mib_free((void **)net->mib.net_statistics);
snmp_mib_free((void __percpu **)net->mib.net_statistics);
err_net_mib:
snmp_mib_free((void **)net->mib.ip_statistics);
snmp_mib_free((void __percpu **)net->mib.ip_statistics);
err_ip_mib:
snmp_mib_free((void **)net->mib.tcp_statistics);
snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
err_tcp_mib:
return -ENOMEM;
}

static __net_exit void ipv4_mib_exit_net(struct net *net)
{
snmp_mib_free((void **)net->mib.icmpmsg_statistics);
snmp_mib_free((void **)net->mib.icmp_statistics);
snmp_mib_free((void **)net->mib.udplite_statistics);
snmp_mib_free((void **)net->mib.udp_statistics);
snmp_mib_free((void **)net->mib.net_statistics);
snmp_mib_free((void **)net->mib.ip_statistics);
snmp_mib_free((void **)net->mib.tcp_statistics);
snmp_mib_free((void __percpu **)net->mib.icmpmsg_statistics);
snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
snmp_mib_free((void __percpu **)net->mib.udp_statistics);
snmp_mib_free((void __percpu **)net->mib.net_statistics);
snmp_mib_free((void __percpu **)net->mib.ip_statistics);
snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
}

static __net_initdata struct pernet_operations ipv4_mib_ops = {
Expand Down
28 changes: 14 additions & 14 deletions net/ipv4/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ static void icmpmsg_put(struct seq_file *seq)

count = 0;
for (i = 0; i < ICMPMSG_MIB_MAX; i++) {
val = snmp_fold_field((void **) net->mib.icmpmsg_statistics, i);
val = snmp_fold_field((void __percpu **) net->mib.icmpmsg_statistics, i);
if (val) {
type[count] = i;
vals[count++] = val;
Expand All @@ -307,18 +307,18 @@ static void icmp_put(struct seq_file *seq)
for (i=0; icmpmibmap[i].name != NULL; i++)
seq_printf(seq, " Out%s", icmpmibmap[i].name);
seq_printf(seq, "\nIcmp: %lu %lu",
snmp_fold_field((void **) net->mib.icmp_statistics, ICMP_MIB_INMSGS),
snmp_fold_field((void **) net->mib.icmp_statistics, ICMP_MIB_INERRORS));
snmp_fold_field((void __percpu **) net->mib.icmp_statistics, ICMP_MIB_INMSGS),
snmp_fold_field((void __percpu **) net->mib.icmp_statistics, ICMP_MIB_INERRORS));
for (i=0; icmpmibmap[i].name != NULL; i++)
seq_printf(seq, " %lu",
snmp_fold_field((void **) net->mib.icmpmsg_statistics,
snmp_fold_field((void __percpu **) net->mib.icmpmsg_statistics,
icmpmibmap[i].index));
seq_printf(seq, " %lu %lu",
snmp_fold_field((void **) net->mib.icmp_statistics, ICMP_MIB_OUTMSGS),
snmp_fold_field((void **) net->mib.icmp_statistics, ICMP_MIB_OUTERRORS));
snmp_fold_field((void __percpu **) net->mib.icmp_statistics, ICMP_MIB_OUTMSGS),
snmp_fold_field((void __percpu **) net->mib.icmp_statistics, ICMP_MIB_OUTERRORS));
for (i=0; icmpmibmap[i].name != NULL; i++)
seq_printf(seq, " %lu",
snmp_fold_field((void **) net->mib.icmpmsg_statistics,
snmp_fold_field((void __percpu **) net->mib.icmpmsg_statistics,
icmpmibmap[i].index | 0x100));
}

Expand All @@ -341,7 +341,7 @@ static int snmp_seq_show(struct seq_file *seq, void *v)

for (i = 0; snmp4_ipstats_list[i].name != NULL; i++)
seq_printf(seq, " %lu",
snmp_fold_field((void **)net->mib.ip_statistics,
snmp_fold_field((void __percpu **)net->mib.ip_statistics,
snmp4_ipstats_list[i].entry));

icmp_put(seq); /* RFC 2011 compatibility */
Expand All @@ -356,11 +356,11 @@ static int snmp_seq_show(struct seq_file *seq, void *v)
/* MaxConn field is signed, RFC 2012 */
if (snmp4_tcp_list[i].entry == TCP_MIB_MAXCONN)
seq_printf(seq, " %ld",
snmp_fold_field((void **)net->mib.tcp_statistics,
snmp_fold_field((void __percpu **)net->mib.tcp_statistics,
snmp4_tcp_list[i].entry));
else
seq_printf(seq, " %lu",
snmp_fold_field((void **)net->mib.tcp_statistics,
snmp_fold_field((void __percpu **)net->mib.tcp_statistics,
snmp4_tcp_list[i].entry));
}

Expand All @@ -371,7 +371,7 @@ static int snmp_seq_show(struct seq_file *seq, void *v)
seq_puts(seq, "\nUdp:");
for (i = 0; snmp4_udp_list[i].name != NULL; i++)
seq_printf(seq, " %lu",
snmp_fold_field((void **)net->mib.udp_statistics,
snmp_fold_field((void __percpu **)net->mib.udp_statistics,
snmp4_udp_list[i].entry));

/* the UDP and UDP-Lite MIBs are the same */
Expand All @@ -382,7 +382,7 @@ static int snmp_seq_show(struct seq_file *seq, void *v)
seq_puts(seq, "\nUdpLite:");
for (i = 0; snmp4_udp_list[i].name != NULL; i++)
seq_printf(seq, " %lu",
snmp_fold_field((void **)net->mib.udplite_statistics,
snmp_fold_field((void __percpu **)net->mib.udplite_statistics,
snmp4_udp_list[i].entry));

seq_putc(seq, '\n');
Expand Down Expand Up @@ -419,7 +419,7 @@ static int netstat_seq_show(struct seq_file *seq, void *v)
seq_puts(seq, "\nTcpExt:");
for (i = 0; snmp4_net_list[i].name != NULL; i++)
seq_printf(seq, " %lu",
snmp_fold_field((void **)net->mib.net_statistics,
snmp_fold_field((void __percpu **)net->mib.net_statistics,
snmp4_net_list[i].entry));

seq_puts(seq, "\nIpExt:");
Expand All @@ -429,7 +429,7 @@ static int netstat_seq_show(struct seq_file *seq, void *v)
seq_puts(seq, "\nIpExt:");
for (i = 0; snmp4_ipextstats_list[i].name != NULL; i++)
seq_printf(seq, " %lu",
snmp_fold_field((void **)net->mib.ip_statistics,
snmp_fold_field((void __percpu **)net->mib.ip_statistics,
snmp4_ipextstats_list[i].entry));

seq_putc(seq, '\n');
Expand Down
2 changes: 1 addition & 1 deletion net/ipv4/route.c
Original file line number Diff line number Diff line change
Expand Up @@ -3334,7 +3334,7 @@ static __net_initdata struct pernet_operations rt_secret_timer_ops = {


#ifdef CONFIG_NET_CLS_ROUTE
struct ip_rt_acct *ip_rt_acct __read_mostly;
struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
#endif /* CONFIG_NET_CLS_ROUTE */

static __initdata unsigned long rhash_entries;
Expand Down
Loading

0 comments on commit 7d720c3

Please sign in to comment.