From: David Vrabel <david.vrabel@citrix.com>
To: xen-devel@lists.xenproject.org
Cc: Keir Fraser <keir@xen.org>,
Ian Campbell <ian.campbell@citrix.com>,
Andrew Cooper <andrew.cooper3@citrix.com>,
Tim Deegan <tim@xen.org>, David Vrabel <david.vrabel@citrix.com>,
Jan Beulich <jbeulich@suse.com>,
Jennifer Herbert <jennifer.herbert@citrix.com>
Subject: [PATCHv4 1/8] x86: provide arch_fetch_and_add()
Date: Thu, 30 Apr 2015 16:33:15 +0100 [thread overview]
Message-ID: <1430408002-30666-2-git-send-email-david.vrabel@citrix.com> (raw)
In-Reply-To: <1430408002-30666-1-git-send-email-david.vrabel@citrix.com>
arch_fetch_and_add() atomically adds a value and returns the previous
value.
This is needed to implement ticket locks.
Signed-off-by: David Vrabel <david.vrabel@citrix.com>
---
xen/include/asm-x86/system.h | 48 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/xen/include/asm-x86/system.h b/xen/include/asm-x86/system.h
index 7111329..47d4a70 100644
--- a/xen/include/asm-x86/system.h
+++ b/xen/include/asm-x86/system.h
@@ -118,6 +118,54 @@ static always_inline unsigned long __cmpxchg(
})
/*
+ * Undefined symbol to cause link failure if a wrong size is used with
+ * arch_fetch_and_add().
+ */
+extern unsigned long __bad_fetch_and_add_size(void);
+
+static always_inline unsigned long __xadd(
+ volatile void *ptr, unsigned long v, int size)
+{
+ switch ( size )
+ {
+ case 1:
+ asm volatile ( "lock; xaddb %b0,%1"
+ : "+r" (v), "+m" (*__xg((volatile void *)ptr))
+ :: "memory");
+ return v;
+ case 2:
+ asm volatile ( "lock; xaddw %w0,%1"
+ : "+r" (v), "+m" (*__xg((volatile void *)ptr))
+ :: "memory");
+ return v;
+ case 4:
+ asm volatile ( "lock; xaddl %k0,%1"
+ : "+r" (v), "+m" (*__xg((volatile void *)ptr))
+ :: "memory");
+ return v;
+ case 8:
+ asm volatile ( "lock; xaddq %q0,%1"
+ : "+r" (v), "+m" (*__xg((volatile void *)ptr))
+ :: "memory");
+
+ return v;
+ default:
+ return __bad_fetch_and_add_size();
+ }
+}
+
+/*
+ * Atomically add @v to the 1, 2, 4, or 8 byte value at @ptr. Returns
+ * the previous value.
+ *
+ * This is a full memory barrier.
+ */
+#define arch_fetch_and_add(ptr, v) \
+ ({ \
+ (typeof(*(ptr)))__xadd(ptr, (typeof(*(ptr)))(v), sizeof(*(ptr))); \
+ })
+
+/*
* Both Intel and AMD agree that, from a programmer's viewpoint:
* Loads cannot be reordered relative to other loads.
* Stores cannot be reordered relative to other stores.
--
1.7.10.4
next prev parent reply other threads:[~2015-04-30 15:38 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-30 15:33 [PATCHv4 0/8] Use ticket locks for spinlocks David Vrabel
2015-04-30 15:33 ` David Vrabel [this message]
2015-05-07 15:59 ` [PATCHv4 1/8] x86: provide arch_fetch_and_add() Jan Beulich
2015-04-30 15:33 ` [PATCHv4 2/8] arm: " David Vrabel
2015-04-30 15:33 ` [PATCHv4 3/8] x86: provide add_sized() David Vrabel
2015-05-07 16:05 ` Jan Beulich
2015-04-30 15:33 ` [PATCHv4 4/8] arm: " David Vrabel
2015-05-05 13:49 ` Ian Campbell
2015-05-05 13:54 ` Tim Deegan
2015-04-30 15:33 ` [PATCHv4 5/8] xen: use ticket locks for spin locks David Vrabel
2015-05-05 13:56 ` Ian Campbell
2015-05-05 13:58 ` David Vrabel
2015-05-08 9:36 ` Jan Beulich
2015-05-11 13:13 ` David Vrabel
2015-05-11 13:29 ` Tim Deegan
2015-04-30 15:33 ` [PATCHv4 6/8] x86, arm: remove asm/spinlock.h from all architectures David Vrabel
2015-05-05 13:49 ` Ian Campbell
2015-04-30 15:33 ` [PATCHv4 7/8] x86: reduce struct paging_domain size David Vrabel
2015-04-30 15:33 ` [PATCHv4 8/8] x86: reduce struct hvm_domain size David Vrabel
2015-05-08 9:47 ` Jan Beulich
2015-05-11 13:17 ` David Vrabel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1430408002-30666-2-git-send-email-david.vrabel@citrix.com \
--to=david.vrabel@citrix.com \
--cc=andrew.cooper3@citrix.com \
--cc=ian.campbell@citrix.com \
--cc=jbeulich@suse.com \
--cc=jennifer.herbert@citrix.com \
--cc=keir@xen.org \
--cc=tim@xen.org \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.