[PATCH] vmstat: use preempt disable instead of try_cmpxchg
From: Shakeel Butt
Date: Wed Feb 04 2026 - 11:46:08 EST
Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
include/linux/mmzone.h | 2 +-
mm/vmstat.c | 58 ++++++++++++++++++------------------------
2 files changed, 26 insertions(+), 34 deletions(-)
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 3e51190a55e4..499cd53efdd6 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -776,7 +776,7 @@ struct per_cpu_zonestat {
struct per_cpu_nodestat {
s8 stat_threshold;
- s8 vm_node_stat_diff[NR_VM_NODE_STAT_ITEMS];
+ long vm_node_stat_diff[NR_VM_NODE_STAT_ITEMS];
};
#endif /* !__GENERATING_BOUNDS.H */
diff --git a/mm/vmstat.c b/mm/vmstat.c
index 86b14b0f77b5..0930695597bb 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -377,7 +377,7 @@ void __mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
long delta)
{
struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
- s8 __percpu *p = pcp->vm_node_stat_diff + item;
+ long __percpu *p = pcp->vm_node_stat_diff + item;
long x;
long t;
@@ -456,8 +456,8 @@ void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
{
struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
- s8 __percpu *p = pcp->vm_node_stat_diff + item;
- s8 v, t;
+ long __percpu *p = pcp->vm_node_stat_diff + item;
+ long v, t;
VM_WARN_ON_ONCE(vmstat_item_in_bytes(item));
@@ -467,7 +467,7 @@ void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
v = __this_cpu_inc_return(*p);
t = __this_cpu_read(pcp->stat_threshold);
if (unlikely(v > t)) {
- s8 overstep = t >> 1;
+ long overstep = t >> 1;
node_page_state_add(v + overstep, pgdat, item);
__this_cpu_write(*p, -overstep);
@@ -512,8 +512,8 @@ void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
{
struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
- s8 __percpu *p = pcp->vm_node_stat_diff + item;
- s8 v, t;
+ long __percpu *p = pcp->vm_node_stat_diff + item;
+ long v, t;
VM_WARN_ON_ONCE(vmstat_item_in_bytes(item));
@@ -523,7 +523,7 @@ void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
v = __this_cpu_dec_return(*p);
t = __this_cpu_read(pcp->stat_threshold);
if (unlikely(v < - t)) {
- s8 overstep = t >> 1;
+ long overstep = t >> 1;
node_page_state_add(v - overstep, pgdat, item);
__this_cpu_write(*p, overstep);
@@ -619,9 +619,8 @@ static inline void mod_node_state(struct pglist_data *pgdat,
enum node_stat_item item, int delta, int overstep_mode)
{
struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
- s8 __percpu *p = pcp->vm_node_stat_diff + item;
- long n, t, z;
- s8 o;
+ long __percpu *p = pcp->vm_node_stat_diff + item;
+ long o, n, t, z;
if (vmstat_item_in_bytes(item)) {
/*
@@ -634,32 +633,25 @@ static inline void mod_node_state(struct pglist_data *pgdat,
delta >>= PAGE_SHIFT;
}
+ preempt_disable();
+
o = this_cpu_read(*p);
- do {
- z = 0; /* overflow to node counters */
+ n = o + delta;
- /*
- * The fetching of the stat_threshold is racy. We may apply
- * a counter threshold to the wrong the cpu if we get
- * rescheduled while executing here. However, the next
- * counter update will apply the threshold again and
- * therefore bring the counter under the threshold again.
- *
- * Most of the time the thresholds are the same anyways
- * for all cpus in a node.
- */
- t = this_cpu_read(pcp->stat_threshold);
+ t = this_cpu_read(pcp->stat_threshold);
+ z = 0;
- n = delta + (long)o;
+ if (abs(n) > t) {
+ int os = overstep_mode * (t >> 1);
- if (abs(n) > t) {
- int os = overstep_mode * (t >> 1) ;
+ /* Overflow must be added to node counters */
+ z = n + os;
+ n = -os;
+ }
- /* Overflow must be added to node counters */
- z = n + os;
- n = -os;
- }
- } while (!this_cpu_try_cmpxchg(*p, &o, n));
+ this_cpu_add(*p, n - o);
+
+ preempt_enable();
if (z)
node_page_state_add(z, pgdat, item);
@@ -866,7 +858,7 @@ static bool refresh_cpu_vm_stats(bool do_pagesets)
struct per_cpu_nodestat __percpu *p = pgdat->per_cpu_nodestats;
for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
- int v;
+ long v;
v = this_cpu_xchg(p->vm_node_stat_diff[i], 0);
if (v) {
@@ -929,7 +921,7 @@ void cpu_vm_stats_fold(int cpu)
for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
if (p->vm_node_stat_diff[i]) {
- int v;
+ long v;
v = p->vm_node_stat_diff[i];
p->vm_node_stat_diff[i] = 0;
--
2.47.3