From patchwork Fri Jul 15 06:50:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corneliu ZUZU X-Patchwork-Id: 9231209 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4D73D608A6 for ; Fri, 15 Jul 2016 06:52:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3A4032819A for ; Fri, 15 Jul 2016 06:52:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2E9BC28319; Fri, 15 Jul 2016 06:52:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1B1F62819A for ; Fri, 15 Jul 2016 06:52:58 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bNwxB-00006j-5p; Fri, 15 Jul 2016 06:50:29 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bNwx9-00006U-PQ for xen-devel@lists.xen.org; Fri, 15 Jul 2016 06:50:27 +0000 Received: from [193.109.254.147] by server-9.bemta-14.messagelabs.com id 00/B2-32749-3B788875; Fri, 15 Jul 2016 06:50:27 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrKIsWRWlGSWpSXmKPExsUSfTxjoe6m9o5 wg+UnmS2WfFzM4sDocXT3b6YAxijWzLyk/IoE1ow/70+xFewtrvi39A1LA+OE2C5GTg4hAQ+J vjeX2LsYuYDstYwSJ24vYIZwTjJKnPq0hRWmqqW/mxEisZpR4k5nJxtIgk1AW+LcoXtMILaIg LTEtc+XwYqYBRYzShy518EMkhAW8Jb40zETqIiDg0VAVaLviT1ImFfAVeLW2StsIGEJATmJBR fSQUxOATeJ06cLIda6SmzcewJsiIRAjsTew1+YIKqlJP63KoEskhDYyCLxrmMvVI2MxKOJN9k mMAotYGRYxahenFpUllqka6GXVJSZnlGSm5iZo2toaKKXm1pcnJiempOYVKyXnJ+7iREYhAxA sINx4yTnQ4ySHExKorwbUzrChfiS8lMqMxKLM+KLSnNSiw8xynBwKEnw7m0DygkWpaanVqRl5 gDjASYtwcGjJMK7HCTNW1yQmFucmQ6ROsWoy3Fs7o21TEIsefl5qVLivNkgRQIgRRmleXAjYL F5iVFWSpiXEegoIZ6C1KLczBJU+VeM4hyMSsK8C0Cm8GTmlcBtegV0BBPQEdbm7SBHlCQipKQ aGPNWy05dGlemt+yqDOPlnvoyo3ytOpn87XeaV96+0bTIkLn4R0D7zfx7+s+iHlcGawuetsxq 9H63Z1F7W3px1dWPL90XvTXNNNt6I1bPvkenm8n8ZLuts5P1qSMpG6Wufz1m72Obxt4t6/6m/ Nc0zdyVTw79L950d/bF+T7KKcFy2wo4WuROKrEUZyQaajEXFScCADUng0TIAgAA X-Env-Sender: czuzu@bitdefender.com X-Msg-Ref: server-6.tower-27.messagelabs.com!1468565425!54094252!1 X-Originating-IP: [91.199.104.161] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.77; banners=-,-,- X-VirusChecked: Checked Received: (qmail 4873 invoked from network); 15 Jul 2016 06:50:25 -0000 Received: from mx01.bbu.dsd.mx.bitdefender.com (HELO mx01.bbu.dsd.mx.bitdefender.com) (91.199.104.161) by server-6.tower-27.messagelabs.com with DHE-RSA-AES128-GCM-SHA256 encrypted SMTP; 15 Jul 2016 06:50:25 -0000 Received: (qmail 13215 invoked from network); 15 Jul 2016 09:50:24 +0300 Received: from unknown (HELO mx-sr.buh.bitdefender.com) (10.17.80.103) by mx01.bbu.dsd.mx.bitdefender.com with AES256-GCM-SHA384 encrypted SMTP; 15 Jul 2016 09:50:24 +0300 Received: from smtp01.buh.bitdefender.com (unknown [10.17.80.75]) by mx-sr.buh.bitdefender.com (Postfix) with ESMTP id A960D7FC05 for ; Fri, 15 Jul 2016 09:50:24 +0300 (EEST) Received: (qmail 10780 invoked from network); 15 Jul 2016 09:50:24 +0300 Received: from unknown (HELO czuzu-dev-vm.clj.bitdefender.biz) (czuzu@bitdefender.com@91.199.104.243) by smtp01.buh.bitdefender.com with SMTP; 15 Jul 2016 09:50:24 +0300 From: Corneliu ZUZU To: xen-devel@lists.xen.org Date: Fri, 15 Jul 2016 09:50:13 +0300 Message-Id: <1468565413-32716-1-git-send-email-czuzu@bitdefender.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1468565278-32535-1-git-send-email-czuzu@bitdefender.com> References: <1468565278-32535-1-git-send-email-czuzu@bitdefender.com> X-BitDefender-Scanner: Clean, Agent: BitDefender qmail 3.1.6 on smtp01.buh.bitdefender.com, sigver: 7.66328 X-BitDefender-Spam: No (0) X-BitDefender-SpamStamp: Build: [Engines: 2.15.6.911, Dats: 426550, Stamp: 3], Multi: [Enabled, t: (0.000013, 0.010970)], BW: [Enabled, t: (0.000007,0.000001)], RBL DNSBL: [Disabled], APM: [Enabled, Score: 500, t: (0.009951), Flags: 85D2ED72; NN_TP_TAG_HTTP; NN_SAMPLE; NN_NO_CONTENT_TYPE; NN_LEGIT_SUMM_400_WORDS; NN_LEGIT_BITDEFENDER; NN_LEGIT_S_SQARE_BRACKETS; NN_LEGIT_MAILING_LIST_TO], SGN: [Enabled, t: (0.019812)], URL: [Enabled, t: (0.000014)], RTDA: [Enabled, t: (0.014513), Hit: No, Details: v2.3.10; Id: 2m1ghmj.1amgkr0nl.1rm1g], total: 0(775) X-BitDefender-CF-Stamp: none Cc: Andrew Cooper , Julien Grall , Stefano Stabellini , Jan Beulich Subject: [Xen-devel] [PATCH v4 4/7] asm/atomic.h: common prototyping (add xen/atomic.h) X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Create a common-side to establish, among others, prototypes of atomic functions called from common-code. Done to avoid introducing inconsistencies between arch-side headers when we make subtle changes to one of them. Some arm-side macros had to be turned into inline functions in the process (also updated README.LinuxPrimitives file). Removed outdated comment ("NB. I've [...]"). Signed-off-by: Corneliu ZUZU Suggested-by: Andrew Cooper Reviewed-by: Andrew Cooper Reviewed-by: Stefano Stabellini Acked-by: Julien Grall --- Changed since v3: * update README.LinuxPrimitives file --- xen/arch/arm/README.LinuxPrimitives | 14 +-- xen/include/asm-arm/atomic.h | 45 ++++++---- xen/include/asm-x86/atomic.h | 103 +--------------------- xen/include/xen/atomic.h | 171 ++++++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 123 deletions(-) create mode 100644 xen/include/xen/atomic.h diff --git a/xen/arch/arm/README.LinuxPrimitives b/xen/arch/arm/README.LinuxPrimitives index 4906593..2fcdfa4 100644 --- a/xen/arch/arm/README.LinuxPrimitives +++ b/xen/arch/arm/README.LinuxPrimitives @@ -27,10 +27,11 @@ The following functions were taken from Linux: atomic_add(), atomic_add_return(), atomic_sub(), atomic_sub_return(), atomic_cmpxchg(), __atomic_add_unless() -Also, the following macros which were in the meantime moved to asm-arm/atomic.h: - atomic_xchg(v, new), atomic_inc(v), atomic_dec(v), +Also, the following macros which were in the meantime moved to asm-arm/atomic.h +and most of them turned to inline functions: + atomic_xchg(v, new) [still macro], atomic_inc(v), atomic_dec(v), atomic_inc_and_test(v), atomic_dec_and_test(v), - atomic_inc_return(v), atomic_dec_return(v), + atomic_inc_return(v) [still macro], atomic_dec_return(v) [still macro], atomic_sub_and_test(i, v), atomic_add_negative(i,v) --------------------------------------------------------------------- @@ -105,10 +106,11 @@ The following functions were taken from Linux: atomic_add(), atomic_add_return(), atomic_sub(), atomic_sub_return(), atomic_cmpxchg(), __atomic_add_unless() -Also, the following macros which were in the meantime moved to asm-arm/atomic.h: - atomic_xchg(v, new), atomic_inc(v), atomic_dec(v), +Also, the following macros which were in the meantime moved to asm-arm/atomic.h +and most of them turned to inline functions: + atomic_xchg(v, new) [still macro], atomic_inc(v), atomic_dec(v), atomic_inc_and_test(v), atomic_dec_and_test(v), - atomic_inc_return(v), atomic_dec_return(v), + atomic_inc_return(v) [still macro], atomic_dec_return(v) [still macro], atomic_sub_and_test(i, v), atomic_add_negative(i,v) --------------------------------------------------------------------- diff --git a/xen/include/asm-arm/atomic.h b/xen/include/asm-arm/atomic.h index 620c636..a79420a 100644 --- a/xen/include/asm-arm/atomic.h +++ b/xen/include/asm-arm/atomic.h @@ -2,6 +2,7 @@ #define __ARCH_ARM_ATOMIC__ #include +#include #include #include @@ -95,15 +96,6 @@ void __bad_atomic_size(void); default: __bad_atomic_size(); break; \ } \ }) - -/* - * NB. I've pushed the volatile qualifier into the operations. This allows - * fast accessors such as _atomic_read() and _atomic_set() which don't give - * the compiler a fit. - */ -typedef struct { int counter; } atomic_t; - -#define ATOMIC_INIT(i) { (i) } /* * On ARM, ordinary assignment (str instruction) doesn't clear the local @@ -141,12 +133,35 @@ static inline void _atomic_set(atomic_t *v, int i) #define atomic_inc_return(v) (atomic_add_return(1, v)) #define atomic_dec_return(v) (atomic_sub_return(1, v)) -#define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0) -#define atomic_inc(v) atomic_add(1, v) -#define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0) -#define atomic_dec(v) atomic_sub(1, v) -#define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0) -#define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0) +static inline int atomic_sub_and_test(int i, atomic_t *v) +{ + return atomic_sub_return(i, v) == 0; +} + +static inline void atomic_inc(atomic_t *v) +{ + atomic_add(1, v); +} + +static inline int atomic_inc_and_test(atomic_t *v) +{ + return atomic_add_return(1, v) == 0; +} + +static inline void atomic_dec(atomic_t *v) +{ + atomic_sub(1, v); +} + +static inline int atomic_dec_and_test(atomic_t *v) +{ + return atomic_sub_return(1, v) == 0; +} + +static inline int atomic_add_negative(int i, atomic_t *v) +{ + return atomic_add_return(i, v) < 0; +} #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) diff --git a/xen/include/asm-x86/atomic.h b/xen/include/asm-x86/atomic.h index 5f9f2dd..3e99b03 100644 --- a/xen/include/asm-x86/atomic.h +++ b/xen/include/asm-x86/atomic.h @@ -2,6 +2,7 @@ #define __ARCH_X86_ATOMIC__ #include +#include #include #define build_read_atomic(name, size, type, reg, barrier) \ @@ -79,56 +80,21 @@ void __bad_atomic_size(void); } \ }) -/* - * NB. I've pushed the volatile qualifier into the operations. This allows - * fast accessors such as _atomic_read() and _atomic_set() which don't give - * the compiler a fit. - */ -typedef struct { int counter; } atomic_t; - -#define ATOMIC_INIT(i) { (i) } - -/** - * atomic_read - read atomic variable - * @v: pointer of type atomic_t - * - * Atomically reads the value of @v. - */ static inline int atomic_read(atomic_t *v) { return read_atomic(&v->counter); } -/** - * _atomic_read - read atomic variable non-atomically - * @v atomic_t - * - * Non-atomically reads the value of @v - */ static inline int _atomic_read(atomic_t v) { return v.counter; } -/** - * atomic_set - set atomic variable - * @v: pointer of type atomic_t - * @i: required value - * - * Atomically sets the value of @v to @i. - */ static inline void atomic_set(atomic_t *v, int i) { write_atomic(&v->counter, i); } -/** - * _atomic_set - set atomic variable non-atomically - * @v: pointer of type atomic_t - * @i: required value - * - * Non-atomically sets the value of @v to @i. - */ static inline void _atomic_set(atomic_t *v, int i) { v->counter = i; @@ -139,13 +105,6 @@ 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 - * @i: integer value to add - * @v: pointer of type atomic_t - * - * Atomically adds @i to @v. - */ static inline void atomic_add(int i, atomic_t *v) { asm volatile ( @@ -154,25 +113,11 @@ static inline void atomic_add(int i, atomic_t *v) : "ir" (i), "m" (*(volatile int *)&v->counter) ); } -/** - * 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 - * - * Atomically subtracts @i from @v. - */ static inline void atomic_sub(int i, atomic_t *v) { asm volatile ( @@ -181,15 +126,6 @@ static inline void atomic_sub(int i, atomic_t *v) : "ir" (i), "m" (*(volatile int *)&v->counter) ); } -/** - * atomic_sub_and_test - subtract value from variable and test result - * @i: integer value to subtract - * @v: pointer of type atomic_t - * - * Atomically subtracts @i from @v and returns - * true if the result is zero, or false for all - * other cases. - */ static inline int atomic_sub_and_test(int i, atomic_t *v) { unsigned char c; @@ -201,12 +137,6 @@ static inline int atomic_sub_and_test(int i, atomic_t *v) return c; } -/** - * atomic_inc - increment atomic variable - * @v: pointer of type atomic_t - * - * Atomically increments @v by 1. - */ static inline void atomic_inc(atomic_t *v) { asm volatile ( @@ -215,14 +145,6 @@ static inline void atomic_inc(atomic_t *v) : "m" (*(volatile int *)&v->counter) ); } -/** - * atomic_inc_and_test - increment and test - * @v: pointer of type atomic_t - * - * Atomically increments @v by 1 - * and returns true if the result is zero, or false for all - * other cases. - */ static inline int atomic_inc_and_test(atomic_t *v) { unsigned char c; @@ -234,12 +156,6 @@ static inline int atomic_inc_and_test(atomic_t *v) return c != 0; } -/** - * atomic_dec - decrement atomic variable - * @v: pointer of type atomic_t - * - * Atomically decrements @v by 1. - */ static inline void atomic_dec(atomic_t *v) { asm volatile ( @@ -248,14 +164,6 @@ static inline void atomic_dec(atomic_t *v) : "m" (*(volatile int *)&v->counter) ); } -/** - * atomic_dec_and_test - decrement and test - * @v: pointer of type atomic_t - * - * Atomically decrements @v by 1 and - * returns true if the result is 0, or false for all other - * cases. - */ static inline int atomic_dec_and_test(atomic_t *v) { unsigned char c; @@ -267,15 +175,6 @@ static inline int atomic_dec_and_test(atomic_t *v) return c != 0; } -/** - * atomic_add_negative - add and test if negative - * @v: pointer of type atomic_t - * @i: integer value to add - * - * Atomically adds @i to @v and returns true - * if the result is negative, or false when - * result is greater than or equal to zero. - */ static inline int atomic_add_negative(int i, atomic_t *v) { unsigned char c; diff --git a/xen/include/xen/atomic.h b/xen/include/xen/atomic.h new file mode 100644 index 0000000..d072912 --- /dev/null +++ b/xen/include/xen/atomic.h @@ -0,0 +1,171 @@ +/* + * include/xen/atomic.h + * + * Common atomic operations entities (atomic_t, function prototypes). + * Include _from_ arch-side . + * + * Copyright (c) 2016 Bitdefender S.R.L. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; If not, see . + */ + +#ifndef __XEN_ATOMIC_H__ +#define __XEN_ATOMIC_H__ + +typedef struct { int counter; } atomic_t; + +#define ATOMIC_INIT(i) { (i) } + +/** + * atomic_read - read atomic variable + * @v: pointer of type atomic_t + * + * Atomically reads the value of @v. + */ +static inline int atomic_read(atomic_t *v); + +/** + * _atomic_read - read atomic variable non-atomically + * @v atomic_t + * + * Non-atomically reads the value of @v + */ +static inline int _atomic_read(atomic_t v); + +/** + * atomic_set - set atomic variable + * @v: pointer of type atomic_t + * @i: required value + * + * Atomically sets the value of @v to @i. + */ +static inline void atomic_set(atomic_t *v, int i); + +/** + * _atomic_set - set atomic variable non-atomically + * @v: pointer of type atomic_t + * @i: required value + * + * Non-atomically sets the value of @v to @i. + */ +static inline void _atomic_set(atomic_t *v, int i); + +/** + * atomic_cmpxchg - compare and exchange an atomic variable + * @v: pointer of type atomic_t + * @old: old value + * @new: new value + * + * Before calling, @old should be set to @v. + * Succeeds if @old == @v (likely), in which case stores @new in @v. + * Returns the initial value in @v, hence succeeds when the return value + * matches that of @old. + * + * Sample (tries atomic increment of v until the operation succeeds): + * + * while(1) + * { + * int old = atomic_read(&v); + * int new = old + 1; + * if ( likely(old == atomic_cmpxchg(&v, old, new)) ) + * break; // success! + * } + */ +static inline int atomic_cmpxchg(atomic_t *v, int old, int new); + +/** + * atomic_add - add integer to atomic variable + * @i: integer value to add + * @v: pointer of type atomic_t + * + * Atomically adds @i to @v. + */ +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); + +/** + * atomic_sub - subtract the atomic variable + * @i: integer value to subtract + * @v: pointer of type atomic_t + * + * Atomically subtracts @i from @v. + */ +static inline void atomic_sub(int i, atomic_t *v); + +/** + * atomic_sub_and_test - subtract value from variable and test result + * @i: integer value to subtract + * @v: pointer of type atomic_t + * + * Atomically subtracts @i from @v and returns + * true if the result is zero, or false for all + * other cases. + */ +static inline int atomic_sub_and_test(int i, atomic_t *v); + +/** + * atomic_inc - increment atomic variable + * @v: pointer of type atomic_t + * + * Atomically increments @v by 1. + */ +static inline void atomic_inc(atomic_t *v); + +/** + * atomic_inc_and_test - increment and test + * @v: pointer of type atomic_t + * + * Atomically increments @v by 1 + * and returns true if the result is zero, or false for all + * other cases. + */ +static inline int atomic_inc_and_test(atomic_t *v); + +/** + * atomic_dec - decrement atomic variable + * @v: pointer of type atomic_t + * + * Atomically decrements @v by 1. + */ +static inline void atomic_dec(atomic_t *v); + +/** + * atomic_dec_and_test - decrement and test + * @v: pointer of type atomic_t + * + * Atomically decrements @v by 1 and + * returns true if the result is 0, or false for all other + * cases. + */ +static inline int atomic_dec_and_test(atomic_t *v); + +/** + * atomic_add_negative - add and test if negative + * @v: pointer of type atomic_t + * @i: integer value to add + * + * Atomically adds @i to @v and returns true + * if the result is negative, or false when + * result is greater than or equal to zero. + */ +static inline int atomic_add_negative(int i, atomic_t *v); + +#endif /* __XEN_ATOMIC_H__ */