Skip to content

Commit cdf9df0

Browse files
ubizjakjgross1
authored andcommitted
locking/x86/xen: Use try_cmpxchg() in xen_alloc_p2m_entry()
Use try_cmpxchg() instead of cmpxchg(*ptr, old, new) == old. The x86 CMPXCHG instruction returns success in the ZF flag, so this change saves a compare after CMPXCHG. Also, try_cmpxchg() implicitly assigns old *ptr value to "old" when CMPXCHG fails. There is no need to explicitly assign old *ptr value to the temporary, which can simplify the surrounding source code. No functional change intended. Signed-off-by: Uros Bizjak <[email protected]> Reviewed-by: Juergen Gross <[email protected]> Cc: Juergen Gross <[email protected]> Cc: Boris Ostrovsky <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Dave Hansen <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Juergen Gross <[email protected]>
1 parent a38297e commit cdf9df0

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

arch/x86/xen/p2m.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,6 @@ int xen_alloc_p2m_entry(unsigned long pfn)
555555
/* Separately check the mid mfn level */
556556
unsigned long missing_mfn;
557557
unsigned long mid_mfn_mfn;
558-
unsigned long old_mfn;
559558

560559
mid_mfn = alloc_p2m_page();
561560
if (!mid_mfn)
@@ -565,12 +564,12 @@ int xen_alloc_p2m_entry(unsigned long pfn)
565564

566565
missing_mfn = virt_to_mfn(p2m_mid_missing_mfn);
567566
mid_mfn_mfn = virt_to_mfn(mid_mfn);
568-
old_mfn = cmpxchg(top_mfn_p, missing_mfn, mid_mfn_mfn);
569-
if (old_mfn != missing_mfn) {
570-
free_p2m_page(mid_mfn);
571-
mid_mfn = mfn_to_virt(old_mfn);
572-
} else {
567+
/* try_cmpxchg() updates missing_mfn on failure. */
568+
if (try_cmpxchg(top_mfn_p, &missing_mfn, mid_mfn_mfn)) {
573569
p2m_top_mfn_p[topidx] = mid_mfn;
570+
} else {
571+
free_p2m_page(mid_mfn);
572+
mid_mfn = mfn_to_virt(missing_mfn);
574573
}
575574
}
576575
} else {

0 commit comments

Comments
 (0)