Skip to content

Commit d647b36

Browse files
herbertxdavem330
authored andcommitted
[SNMP]: Fix SNMP counters with PREEMPT
The SNMP macros use raw_smp_processor_id() in process context which is illegal because the process may be preempted and then migrated to another CPU. This patch makes it use get_cpu/put_cpu to disable preemption. Signed-off-by: Herbert Xu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 2caf62f commit d647b36

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

include/net/snmp.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <linux/cache.h>
2525
#include <linux/snmp.h>
26+
#include <linux/smp.h>
2627

2728
/*
2829
* Mibs are stored in array of unsigned long.
@@ -135,14 +136,26 @@ struct linux_mib {
135136
#define SNMP_INC_STATS_BH(mib, field) \
136137
(per_cpu_ptr(mib[0], raw_smp_processor_id())->mibs[field]++)
137138
#define SNMP_INC_STATS_USER(mib, field) \
138-
(per_cpu_ptr(mib[1], raw_smp_processor_id())->mibs[field]++)
139+
do { \
140+
per_cpu_ptr(mib[1], get_cpu())->mibs[field]++; \
141+
put_cpu(); \
142+
} while (0)
139143
#define SNMP_INC_STATS(mib, field) \
140-
(per_cpu_ptr(mib[!in_softirq()], raw_smp_processor_id())->mibs[field]++)
144+
do { \
145+
per_cpu_ptr(mib[!in_softirq()], get_cpu())->mibs[field]++; \
146+
put_cpu(); \
147+
} while (0)
141148
#define SNMP_DEC_STATS(mib, field) \
142-
(per_cpu_ptr(mib[!in_softirq()], raw_smp_processor_id())->mibs[field]--)
149+
do { \
150+
per_cpu_ptr(mib[!in_softirq()], get_cpu())->mibs[field]--; \
151+
put_cpu(); \
152+
} while (0)
143153
#define SNMP_ADD_STATS_BH(mib, field, addend) \
144154
(per_cpu_ptr(mib[0], raw_smp_processor_id())->mibs[field] += addend)
145155
#define SNMP_ADD_STATS_USER(mib, field, addend) \
146-
(per_cpu_ptr(mib[1], raw_smp_processor_id())->mibs[field] += addend)
156+
do { \
157+
per_cpu_ptr(mib[1], get_cpu())->mibs[field] += addend; \
158+
put_cpu(); \
159+
} while (0)
147160

148161
#endif

0 commit comments

Comments
 (0)