From: David Vrabel <david.vrabel@citrix.com>
To: xen-devel@lists.xenproject.org
Cc: Andrew Cooper <andrew.cooper3@citrix.com>,
Jennifer Herbert <jennifer.herbert@citrix.com>,
David Vrabel <david.vrabel@citrix.com>,
Jan Beulich <jbeulich@suse.com>,
Ian Campbell <ian.campbell@citrix.com>
Subject: [PATCHv2 1/4] atomic: replace atomic_compareandswap() with atomic_cmpxchg()
Date: Tue, 26 Jan 2016 16:25:10 +0000 [thread overview]
Message-ID: <1453825513-1611-2-git-send-email-david.vrabel@citrix.com> (raw)
In-Reply-To: <1453825513-1611-1-git-send-email-david.vrabel@citrix.com>
atomic_compareandswap() used atomic_t as the new, old and returned
values which is less convinient than using just int.
Signed-off-by: David Vrabel <david.vrabel@citrix.com>
---
v2:
- arm/arm64 already provided atomic_cmpxchg()
---
xen/common/domain.c | 5 +----
xen/include/asm-arm/atomic.h | 8 --------
xen/include/asm-x86/atomic.h | 24 ++++++++++++++++--------
xen/include/xen/sched.h | 9 ++++-----
4 files changed, 21 insertions(+), 25 deletions(-)
diff --git a/xen/common/domain.c b/xen/common/domain.c
index 2979c1b..93e77f5 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -857,14 +857,11 @@ static void complete_domain_destroy(struct rcu_head *head)
void domain_destroy(struct domain *d)
{
struct domain **pd;
- atomic_t old = ATOMIC_INIT(0);
- atomic_t new = ATOMIC_INIT(DOMAIN_DESTROYED);
BUG_ON(!d->is_dying);
/* May be already destroyed, or get_domain() can race us. */
- old = atomic_compareandswap(old, new, &d->refcnt);
- if ( _atomic_read(old) != 0 )
+ if ( atomic_cmpxchg(&d->refcnt, 0, DOMAIN_DESTROYED) != 0 )
return;
cpupool_rm_domain(d);
diff --git a/xen/include/asm-arm/atomic.h b/xen/include/asm-arm/atomic.h
index 5a38c67..29ab265 100644
--- a/xen/include/asm-arm/atomic.h
+++ b/xen/include/asm-arm/atomic.h
@@ -138,14 +138,6 @@ static inline void _atomic_set(atomic_t *v, int i)
# error "unknown ARM variant"
#endif
-static inline atomic_t atomic_compareandswap(
- atomic_t old, atomic_t new, atomic_t *v)
-{
- atomic_t rc;
- rc.counter = __cmpxchg(&v->counter, old.counter, new.counter, sizeof(int));
- return rc;
-}
-
#endif /* __ARCH_ARM_ATOMIC__ */
/*
* Local variables:
diff --git a/xen/include/asm-x86/atomic.h b/xen/include/asm-x86/atomic.h
index 2b8c877..d246b70 100644
--- a/xen/include/asm-x86/atomic.h
+++ b/xen/include/asm-x86/atomic.h
@@ -135,6 +135,10 @@ static inline void _atomic_set(atomic_t *v, int i)
v->counter = i;
}
+static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
+{
+ return cmpxchg(&v->counter, old, new);
+}
/**
* atomic_add - add integer to atomic variable
@@ -152,6 +156,18 @@ static inline void atomic_add(int i, atomic_t *v)
}
/**
+ * atomic_add_return - add integer and return
+ * @i: integer value to add
+ * @v: pointer of type atomic_t
+ *
+ * Atomically adds @i to @v and returns @i + @v
+ */
+static inline int atomic_add_return(int i, atomic_t *v)
+{
+ return i + arch_fetch_and_add(&v->counter, i);
+}
+
+/**
* atomic_sub - subtract the atomic variable
* @i: integer value to subtract
* @v: pointer of type atomic_t
@@ -272,12 +288,4 @@ static inline int atomic_add_negative(int i, atomic_t *v)
return c;
}
-static inline atomic_t atomic_compareandswap(
- atomic_t old, atomic_t new, atomic_t *v)
-{
- atomic_t rc;
- rc.counter = __cmpxchg(&v->counter, old.counter, new.counter, sizeof(int));
- return rc;
-}
-
#endif /* __ARCH_X86_ATOMIC__ */
diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
index 82b6dd1..5870745 100644
--- a/xen/include/xen/sched.h
+++ b/xen/include/xen/sched.h
@@ -483,16 +483,15 @@ extern struct vcpu *idle_vcpu[NR_CPUS];
*/
static always_inline int get_domain(struct domain *d)
{
- atomic_t old, new, seen = d->refcnt;
+ int old, seen = atomic_read(&d->refcnt);
do
{
old = seen;
- if ( unlikely(_atomic_read(old) & DOMAIN_DESTROYED) )
+ if ( unlikely(old & DOMAIN_DESTROYED) )
return 0;
- _atomic_set(&new, _atomic_read(old) + 1);
- seen = atomic_compareandswap(old, new, &d->refcnt);
+ seen = atomic_cmpxchg(&d->refcnt, old, old + 1);
}
- while ( unlikely(_atomic_read(seen) != _atomic_read(old)) );
+ while ( unlikely(seen != old) );
return 1;
}
--
2.1.4
next prev parent reply other threads:[~2016-01-26 16:25 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-01-26 16:25 [PATCHv2 0/4] spinlock: queue read-write locks David Vrabel
2016-01-26 16:25 ` David Vrabel [this message]
2016-01-26 17:09 ` [PATCHv2 1/4] atomic: replace atomic_compareandswap() with atomic_cmpxchg() Ian Campbell
2016-01-26 16:25 ` [PATCHv2 2/4] spinlock: shrink struct lock_debug David Vrabel
2016-01-26 16:25 ` [PATCHv2 3/4] spinlock: move rwlock API and per-cpu rwlocks into their own files David Vrabel
2016-01-26 16:25 ` [PATCHv2 4/4] spinlock: fair read-write locks 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=1453825513-1611-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=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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).