Skip to content

Commit 77cd814

Browse files
ubizjakakpm00
authored andcommitted
mm/vmstat: use this_cpu_try_cmpxchg in mod_{zone,node}_state
Use this_cpu_try_cmpxchg instead of this_cpu_cmpxchg (*ptr, old, new) == old in mod_zone_state and mod_node_state. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg). Also, try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop. No functional change intended. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Uros Bizjak <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 91e79d2 commit 77cd814

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

mm/vmstat.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,10 @@ static inline void mod_zone_state(struct zone *zone,
559559
{
560560
struct per_cpu_zonestat __percpu *pcp = zone->per_cpu_zonestats;
561561
s8 __percpu *p = pcp->vm_stat_diff + item;
562-
long o, n, t, z;
562+
long n, t, z;
563+
s8 o;
563564

565+
o = this_cpu_read(*p);
564566
do {
565567
z = 0; /* overflow to zone counters */
566568

@@ -576,8 +578,7 @@ static inline void mod_zone_state(struct zone *zone,
576578
*/
577579
t = this_cpu_read(pcp->stat_threshold);
578580

579-
o = this_cpu_read(*p);
580-
n = delta + o;
581+
n = delta + (long)o;
581582

582583
if (abs(n) > t) {
583584
int os = overstep_mode * (t >> 1) ;
@@ -586,7 +587,7 @@ static inline void mod_zone_state(struct zone *zone,
586587
z = n + os;
587588
n = -os;
588589
}
589-
} while (this_cpu_cmpxchg(*p, o, n) != o);
590+
} while (!this_cpu_try_cmpxchg(*p, &o, n));
590591

591592
if (z)
592593
zone_page_state_add(z, zone, item);
@@ -616,7 +617,8 @@ static inline void mod_node_state(struct pglist_data *pgdat,
616617
{
617618
struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
618619
s8 __percpu *p = pcp->vm_node_stat_diff + item;
619-
long o, n, t, z;
620+
long n, t, z;
621+
s8 o;
620622

621623
if (vmstat_item_in_bytes(item)) {
622624
/*
@@ -629,6 +631,7 @@ static inline void mod_node_state(struct pglist_data *pgdat,
629631
delta >>= PAGE_SHIFT;
630632
}
631633

634+
o = this_cpu_read(*p);
632635
do {
633636
z = 0; /* overflow to node counters */
634637

@@ -644,8 +647,7 @@ static inline void mod_node_state(struct pglist_data *pgdat,
644647
*/
645648
t = this_cpu_read(pcp->stat_threshold);
646649

647-
o = this_cpu_read(*p);
648-
n = delta + o;
650+
n = delta + (long)o;
649651

650652
if (abs(n) > t) {
651653
int os = overstep_mode * (t >> 1) ;
@@ -654,7 +656,7 @@ static inline void mod_node_state(struct pglist_data *pgdat,
654656
z = n + os;
655657
n = -os;
656658
}
657-
} while (this_cpu_cmpxchg(*p, o, n) != o);
659+
} while (!this_cpu_try_cmpxchg(*p, &o, n));
658660

659661
if (z)
660662
node_page_state_add(z, pgdat, item);

0 commit comments

Comments
 (0)