This implements a get_cpu() and matching put_cpu() to safely hand out
the current CPU to avoid preempt races. Andrew and I have been bitching
about the need for such a method.
I also went ahead and replaced an example of current explicit
preempt-off with the new methods, as a case in point.
the previous value of smp_processor_id may not equal the current. You must
protect these situations by disabling preemption around them.
+You can also use put_cpu() and get_cpu(), which will disable preemption.
+
RULE #2: CPU state must be protected.
#define per_cpu(var, cpu) var
#define this_cpu(var) var
-#endif
-#endif
+#endif /* !SMP */
+
+#define get_cpu() ({ preempt_disable(); smp_processor_id(); })
+#define put_cpu() preempt_enable()
+
+#endif /* __LINUX_SMP_H */
} ____cacheline_aligned ratelimits[NR_CPUS];
int cpu;
- preempt_disable();
- cpu = smp_processor_id();
+ cpu = get_cpu();
if (ratelimits[cpu].count++ >= 1000) {
ratelimits[cpu].count = 0;
- preempt_enable();
+ put_cpu();
balance_dirty_pages(mapping);
return;
}
- preempt_enable();
+ put_cpu();
}
/*