Skip to content

Commit 50fd4d5

Browse files
ubizjakIngo Molnar
authored andcommitted
x86/PAT: Use try_cmpxchg() in set_page_memtype()
Use try_cmpxchg instead of cmpxchg (*ptr, old, new) == old in set_page_memtype. 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. Note that the value from *ptr should be read using READ_ONCE to prevent the compiler from merging, refetching or reordering the read. No functional change intended. Signed-off-by: Uros Bizjak <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 1d61659 commit 50fd4d5

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

arch/x86/mm/pat/memtype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ static inline void set_page_memtype(struct page *pg,
159159
break;
160160
}
161161

162+
old_flags = READ_ONCE(pg->flags);
162163
do {
163-
old_flags = pg->flags;
164164
new_flags = (old_flags & _PGMT_CLEAR_MASK) | memtype_flags;
165-
} while (cmpxchg(&pg->flags, old_flags, new_flags) != old_flags);
165+
} while (!try_cmpxchg(&pg->flags, &old_flags, new_flags));
166166
}
167167
#else
168168
static inline enum page_cache_mode get_page_memtype(struct page *pg)

0 commit comments

Comments
 (0)