Skip to content

Commit

Permalink
locking/x86/xen: Use try_cmpxchg() in xen_alloc_p2m_entry()
Browse files Browse the repository at this point in the history
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 <ubizjak@gmail.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Link: https://lore.kernel.org/r/20240405083335.507471-1-ubizjak@gmail.com
Signed-off-by: Juergen Gross <jgross@suse.com>
  • Loading branch information
Uros Bizjak authored and Juergen Gross committed May 17, 2024
1 parent a38297e commit cdf9df0
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions arch/x86/xen/p2m.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ int xen_alloc_p2m_entry(unsigned long pfn)
/* Separately check the mid mfn level */
unsigned long missing_mfn;
unsigned long mid_mfn_mfn;
unsigned long old_mfn;

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

missing_mfn = virt_to_mfn(p2m_mid_missing_mfn);
mid_mfn_mfn = virt_to_mfn(mid_mfn);
old_mfn = cmpxchg(top_mfn_p, missing_mfn, mid_mfn_mfn);
if (old_mfn != missing_mfn) {
free_p2m_page(mid_mfn);
mid_mfn = mfn_to_virt(old_mfn);
} else {
/* try_cmpxchg() updates missing_mfn on failure. */
if (try_cmpxchg(top_mfn_p, &missing_mfn, mid_mfn_mfn)) {
p2m_top_mfn_p[topidx] = mid_mfn;
} else {
free_p2m_page(mid_mfn);
mid_mfn = mfn_to_virt(missing_mfn);
}
}
} else {
Expand Down

0 comments on commit cdf9df0

Please sign in to comment.