Skip to content

Commit be281e5

Browse files
Eric Dumazetdavem330
authored andcommitted
ipv6: reduce per device ICMP mib sizes
ipv6 has per device ICMP SNMP counters, taking too much space because they use percpu storage. needed size per device is : (512+4)*sizeof(long)*number_of_possible_cpus*2 On a 32bit kernel, 16 possible cpus, this wastes more than 64kbytes of memory per ipv6 enabled network device, taken in vmalloc pool. Since ICMP messages are rare, just use shared counters (atomic_long_t) Per network space ICMP counters are still using percpu memory, we might also convert them to shared counters in a future patch. Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> CC: Denys Fedoryshchenko <denys@visp.net.lb> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 75e308c commit be281e5

5 files changed

Lines changed: 68 additions & 33 deletions

File tree

‎include/net/if_inet6.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ struct ifacaddr6 {
154154
struct ipv6_devstat {
155155
struct proc_dir_entry *proc_dir_entry;
156156
DEFINE_SNMP_STAT(struct ipstats_mib, ipv6);
157-
DEFINE_SNMP_STAT(struct icmpv6_mib, icmpv6);
158-
DEFINE_SNMP_STAT(struct icmpv6msg_mib, icmpv6msg);
157+
DEFINE_SNMP_STAT_ATOMIC(struct icmpv6_mib_device, icmpv6dev);
158+
DEFINE_SNMP_STAT_ATOMIC(struct icmpv6msg_mib_device, icmpv6msgdev);
159159
};
160160

161161
struct inet6_dev {

‎include/net/ipv6.h‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ extern struct ctl_path net_ipv6_ctl_path[];
123123
SNMP_INC_STATS##modifier((net)->mib.statname##_statistics, (field));\
124124
})
125125

126+
/* per device counters are atomic_long_t */
127+
#define _DEVINCATOMIC(net, statname, modifier, idev, field) \
128+
({ \
129+
struct inet6_dev *_idev = (idev); \
130+
if (likely(_idev != NULL)) \
131+
SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
132+
SNMP_INC_STATS##modifier((net)->mib.statname##_statistics, (field));\
133+
})
134+
126135
#define _DEVADD(net, statname, modifier, idev, field, val) \
127136
({ \
128137
struct inet6_dev *_idev = (idev); \
@@ -154,16 +163,16 @@ extern struct ctl_path net_ipv6_ctl_path[];
154163
#define IP6_UPD_PO_STATS_BH(net, idev,field,val) \
155164
_DEVUPD(net, ipv6, 64_BH, idev, field, val)
156165
#define ICMP6_INC_STATS(net, idev, field) \
157-
_DEVINC(net, icmpv6, , idev, field)
166+
_DEVINCATOMIC(net, icmpv6, , idev, field)
158167
#define ICMP6_INC_STATS_BH(net, idev, field) \
159-
_DEVINC(net, icmpv6, _BH, idev, field)
168+
_DEVINCATOMIC(net, icmpv6, _BH, idev, field)
160169

161170
#define ICMP6MSGOUT_INC_STATS(net, idev, field) \
162-
_DEVINC(net, icmpv6msg, , idev, field +256)
171+
_DEVINCATOMIC(net, icmpv6msg, , idev, field +256)
163172
#define ICMP6MSGOUT_INC_STATS_BH(net, idev, field) \
164-
_DEVINC(net, icmpv6msg, _BH, idev, field +256)
173+
_DEVINCATOMIC(net, icmpv6msg, _BH, idev, field +256)
165174
#define ICMP6MSGIN_INC_STATS_BH(net, idev, field) \
166-
_DEVINC(net, icmpv6msg, _BH, idev, field)
175+
_DEVINCATOMIC(net, icmpv6msg, _BH, idev, field)
167176

168177
struct ip6_ra_chain {
169178
struct ip6_ra_chain *next;

‎include/net/snmp.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,24 @@ struct icmpmsg_mib {
7272

7373
/* ICMP6 (IPv6-ICMP) */
7474
#define ICMP6_MIB_MAX __ICMP6_MIB_MAX
75+
/* per network ns counters */
7576
struct icmpv6_mib {
7677
unsigned long mibs[ICMP6_MIB_MAX];
7778
};
79+
/* per device counters, (shared on all cpus) */
80+
struct icmpv6_mib_device {
81+
atomic_long_t mibs[ICMP6_MIB_MAX];
82+
};
7883

7984
#define ICMP6MSG_MIB_MAX __ICMP6MSG_MIB_MAX
85+
/* per network ns counters */
8086
struct icmpv6msg_mib {
8187
unsigned long mibs[ICMP6MSG_MIB_MAX];
8288
};
89+
/* per device counters, (shared on all cpus) */
90+
struct icmpv6msg_mib_device {
91+
atomic_long_t mibs[ICMP6MSG_MIB_MAX];
92+
};
8393

8494

8595
/* TCP */
@@ -114,6 +124,8 @@ struct linux_xfrm_mib {
114124
*/
115125
#define DEFINE_SNMP_STAT(type, name) \
116126
__typeof__(type) __percpu *name[2]
127+
#define DEFINE_SNMP_STAT_ATOMIC(type, name) \
128+
__typeof__(type) *name
117129
#define DECLARE_SNMP_STAT(type, name) \
118130
extern __typeof__(type) __percpu *name[2]
119131

@@ -124,6 +136,8 @@ struct linux_xfrm_mib {
124136
__this_cpu_inc(mib[0]->mibs[field])
125137
#define SNMP_INC_STATS_USER(mib, field) \
126138
this_cpu_inc(mib[1]->mibs[field])
139+
#define SNMP_INC_STATS_ATOMIC_LONG(mib, field) \
140+
atomic_long_inc(&mib->mibs[field])
127141
#define SNMP_INC_STATS(mib, field) \
128142
this_cpu_inc(mib[!in_softirq()]->mibs[field])
129143
#define SNMP_DEC_STATS(mib, field) \

‎net/ipv6/addrconf.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,19 +289,19 @@ static int snmp6_alloc_dev(struct inet6_dev *idev)
289289
sizeof(struct ipstats_mib),
290290
__alignof__(struct ipstats_mib)) < 0)
291291
goto err_ip;
292-
if (snmp_mib_init((void __percpu **)idev->stats.icmpv6,
293-
sizeof(struct icmpv6_mib),
294-
__alignof__(struct icmpv6_mib)) < 0)
292+
idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
293+
GFP_KERNEL);
294+
if (!idev->stats.icmpv6dev)
295295
goto err_icmp;
296-
if (snmp_mib_init((void __percpu **)idev->stats.icmpv6msg,
297-
sizeof(struct icmpv6msg_mib),
298-
__alignof__(struct icmpv6msg_mib)) < 0)
296+
idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
297+
GFP_KERNEL);
298+
if (!idev->stats.icmpv6msgdev)
299299
goto err_icmpmsg;
300300

301301
return 0;
302302

303303
err_icmpmsg:
304-
snmp_mib_free((void __percpu **)idev->stats.icmpv6);
304+
kfree(idev->stats.icmpv6dev);
305305
err_icmp:
306306
snmp_mib_free((void __percpu **)idev->stats.ipv6);
307307
err_ip:
@@ -310,8 +310,8 @@ static int snmp6_alloc_dev(struct inet6_dev *idev)
310310

311311
static void snmp6_free_dev(struct inet6_dev *idev)
312312
{
313-
snmp_mib_free((void __percpu **)idev->stats.icmpv6msg);
314-
snmp_mib_free((void __percpu **)idev->stats.icmpv6);
313+
kfree(idev->stats.icmpv6msgdev);
314+
kfree(idev->stats.icmpv6dev);
315315
snmp_mib_free((void __percpu **)idev->stats.ipv6);
316316
}
317317

@@ -3838,7 +3838,7 @@ static inline size_t inet6_if_nlmsg_size(void)
38383838
+ nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
38393839
}
38403840

3841-
static inline void __snmp6_fill_stats(u64 *stats, void __percpu **mib,
3841+
static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
38423842
int items, int bytes)
38433843
{
38443844
int i;
@@ -3848,7 +3848,7 @@ static inline void __snmp6_fill_stats(u64 *stats, void __percpu **mib,
38483848
/* Use put_unaligned() because stats may not be aligned for u64. */
38493849
put_unaligned(items, &stats[0]);
38503850
for (i = 1; i < items; i++)
3851-
put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3851+
put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
38523852

38533853
memset(&stats[items], 0, pad);
38543854
}
@@ -3877,7 +3877,7 @@ static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
38773877
IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
38783878
break;
38793879
case IFLA_INET6_ICMP6STATS:
3880-
__snmp6_fill_stats(stats, (void __percpu **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3880+
__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
38813881
break;
38823882
}
38833883
}

‎net/ipv6/proc.c‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ static const struct snmp_mib snmp6_udplite6_list[] = {
141141
SNMP_MIB_SENTINEL
142142
};
143143

144-
static void snmp6_seq_show_icmpv6msg(struct seq_file *seq, void __percpu **mib)
144+
/* can be called either with percpu mib (pcpumib != NULL),
145+
* or shared one (smib != NULL)
146+
*/
147+
static void snmp6_seq_show_icmpv6msg(struct seq_file *seq, void __percpu **pcpumib,
148+
atomic_long_t *smib)
145149
{
146150
char name[32];
147151
int i;
@@ -158,14 +162,14 @@ static void snmp6_seq_show_icmpv6msg(struct seq_file *seq, void __percpu **mib)
158162
snprintf(name, sizeof(name), "Icmp6%s%s",
159163
i & 0x100 ? "Out" : "In", p);
160164
seq_printf(seq, "%-32s\t%lu\n", name,
161-
snmp_fold_field(mib, i));
165+
pcpumib ? snmp_fold_field(pcpumib, i) : atomic_long_read(smib + i));
162166
}
163167

164168
/* print by number (nonzero only) - ICMPMsgStat format */
165169
for (i = 0; i < ICMP6MSG_MIB_MAX; i++) {
166170
unsigned long val;
167171

168-
val = snmp_fold_field(mib, i);
172+
val = pcpumib ? snmp_fold_field(pcpumib, i) : atomic_long_read(smib + i);
169173
if (!val)
170174
continue;
171175
snprintf(name, sizeof(name), "Icmp6%sType%u",
@@ -174,14 +178,22 @@ static void snmp6_seq_show_icmpv6msg(struct seq_file *seq, void __percpu **mib)
174178
}
175179
}
176180

177-
static void snmp6_seq_show_item(struct seq_file *seq, void __percpu **mib,
181+
/* can be called either with percpu mib (pcpumib != NULL),
182+
* or shared one (smib != NULL)
183+
*/
184+
static void snmp6_seq_show_item(struct seq_file *seq, void __percpu **pcpumib,
185+
atomic_long_t *smib,
178186
const struct snmp_mib *itemlist)
179187
{
180188
int i;
189+
unsigned long val;
181190

182-
for (i = 0; itemlist[i].name; i++)
183-
seq_printf(seq, "%-32s\t%lu\n", itemlist[i].name,
184-
snmp_fold_field(mib, itemlist[i].entry));
191+
for (i = 0; itemlist[i].name; i++) {
192+
val = pcpumib ?
193+
snmp_fold_field(pcpumib, itemlist[i].entry) :
194+
atomic_long_read(smib + itemlist[i].entry);
195+
seq_printf(seq, "%-32s\t%lu\n", itemlist[i].name, val);
196+
}
185197
}
186198

187199
static void snmp6_seq_show_item64(struct seq_file *seq, void __percpu **mib,
@@ -201,13 +213,13 @@ static int snmp6_seq_show(struct seq_file *seq, void *v)
201213
snmp6_seq_show_item64(seq, (void __percpu **)net->mib.ipv6_statistics,
202214
snmp6_ipstats_list, offsetof(struct ipstats_mib, syncp));
203215
snmp6_seq_show_item(seq, (void __percpu **)net->mib.icmpv6_statistics,
204-
snmp6_icmp6_list);
216+
NULL, snmp6_icmp6_list);
205217
snmp6_seq_show_icmpv6msg(seq,
206-
(void __percpu **)net->mib.icmpv6msg_statistics);
218+
(void __percpu **)net->mib.icmpv6msg_statistics, NULL);
207219
snmp6_seq_show_item(seq, (void __percpu **)net->mib.udp_stats_in6,
208-
snmp6_udp6_list);
220+
NULL, snmp6_udp6_list);
209221
snmp6_seq_show_item(seq, (void __percpu **)net->mib.udplite_stats_in6,
210-
snmp6_udplite6_list);
222+
NULL, snmp6_udplite6_list);
211223
return 0;
212224
}
213225

@@ -229,11 +241,11 @@ static int snmp6_dev_seq_show(struct seq_file *seq, void *v)
229241
struct inet6_dev *idev = (struct inet6_dev *)seq->private;
230242

231243
seq_printf(seq, "%-32s\t%u\n", "ifIndex", idev->dev->ifindex);
232-
snmp6_seq_show_item(seq, (void __percpu **)idev->stats.ipv6,
244+
snmp6_seq_show_item(seq, (void __percpu **)idev->stats.ipv6, NULL,
233245
snmp6_ipstats_list);
234-
snmp6_seq_show_item(seq, (void __percpu **)idev->stats.icmpv6,
246+
snmp6_seq_show_item(seq, NULL, idev->stats.icmpv6dev->mibs,
235247
snmp6_icmp6_list);
236-
snmp6_seq_show_icmpv6msg(seq, (void __percpu **)idev->stats.icmpv6msg);
248+
snmp6_seq_show_icmpv6msg(seq, NULL, idev->stats.icmpv6msgdev->mibs);
237249
return 0;
238250
}
239251

0 commit comments

Comments
 (0)