[RESEND,2/2] ucount: Use atomic_long_try_cmpxchg in atomic_long_inc_below

Message ID 20221017144049.3404-3-ubizjak@gmail.com
State New
Headers
Series ucount: Fix and improve atomic_long_inc_below |

Commit Message

Uros Bizjak Oct. 17, 2022, 2:40 p.m. UTC
  Use atomic_long_try_cmpxchg instead of
atomic_long_cmpxchg (*ptr, old, new) == old in atomic_long_inc_below.
x86 CMPXCHG instruction returns success in ZF flag, so this change saves
a compare after cmpxchg (and related move instruction in front of cmpxchg).

Also, atomic_long_try_cmpxchg implicitly assigns old *ptr value to "old"
when cmpxchg fails, enabling further code simplifications.

No functional change intended.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
---
 kernel/ucount.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
  

Comments

Alexey Gladkov Oct. 31, 2022, 7:02 p.m. UTC | #1
On Mon, Oct 17, 2022 at 04:40:49PM +0200, Uros Bizjak wrote:
> Use atomic_long_try_cmpxchg instead of
> atomic_long_cmpxchg (*ptr, old, new) == old in atomic_long_inc_below.
> x86 CMPXCHG instruction returns success in ZF flag, so this change saves
> a compare after cmpxchg (and related move instruction in front of cmpxchg).
> 
> Also, atomic_long_try_cmpxchg implicitly assigns old *ptr value to "old"
> when cmpxchg fails, enabling further code simplifications.
> 
> No functional change intended.
> 
> Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
> Cc: "Eric W. Biederman" <ebiederm@xmission.com>

Reviewed-by: Alexey Gladkov <legion@kernel.org>

Eric, I think the patch looks good. You already reviewed the previous
version [1]. What do you think about this version?

[1] https://lore.kernel.org/lkml/87pmi6z143.fsf@email.froward.int.ebiederm.org/

> ---
>  kernel/ucount.c | 14 ++++++--------
>  1 file changed, 6 insertions(+), 8 deletions(-)
> 
> diff --git a/kernel/ucount.c b/kernel/ucount.c
> index 74f5b4959feb..2c04589a61ff 100644
> --- a/kernel/ucount.c
> +++ b/kernel/ucount.c
> @@ -214,16 +214,14 @@ void put_ucounts(struct ucounts *ucounts)
>  
>  static inline bool atomic_long_inc_below(atomic_long_t *v, long u)
>  {
> -	long c, old;
> -	c = atomic_long_read(v);
> -	for (;;) {
> +	long c = atomic_long_read(v);
> +
> +	do {
>  		if (unlikely(c >= u))
>  			return false;
> -		old = atomic_long_cmpxchg(v, c, c+1);
> -		if (likely(old == c))
> -			return true;
> -		c = old;
> -	}
> +	} while (!atomic_long_try_cmpxchg(v, &c, c+1));
> +
> +	return true;
>  }
>  
>  struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,
> -- 
> 2.37.3
>
  

Patch

diff --git a/kernel/ucount.c b/kernel/ucount.c
index 74f5b4959feb..2c04589a61ff 100644
--- a/kernel/ucount.c
+++ b/kernel/ucount.c
@@ -214,16 +214,14 @@  void put_ucounts(struct ucounts *ucounts)
 
 static inline bool atomic_long_inc_below(atomic_long_t *v, long u)
 {
-	long c, old;
-	c = atomic_long_read(v);
-	for (;;) {
+	long c = atomic_long_read(v);
+
+	do {
 		if (unlikely(c >= u))
 			return false;
-		old = atomic_long_cmpxchg(v, c, c+1);
-		if (likely(old == c))
-			return true;
-		c = old;
-	}
+	} while (!atomic_long_try_cmpxchg(v, &c, c+1));
+
+	return true;
 }
 
 struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,