linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] random: Use try_cmpxchg in _credit_init_bits
@ 2022-07-14 18:28 Uros Bizjak
  2022-07-16 18:29 ` Jason A. Donenfeld
  0 siblings, 1 reply; 2+ messages in thread
From: Uros Bizjak @ 2022-07-14 18:28 UTC (permalink / raw)
  To: linux-kernel; +Cc: Uros Bizjak, Theodore Ts'o, Jason A. Donenfeld

Use try_cmpxchg instead of cmpxchg (*ptr, old, new) == old in
_credit_init_bits. 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, 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: "Theodore Ts'o" <tytso@mit.edu>
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
---
 drivers/char/random.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index e3dd1dd3dd22..78e690f0f9f6 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -643,10 +643,10 @@ static void __cold _credit_init_bits(size_t bits)
 
 	add = min_t(size_t, bits, POOL_BITS);
 
+	orig = READ_ONCE(input_pool.init_bits);
 	do {
-		orig = READ_ONCE(input_pool.init_bits);
 		new = min_t(unsigned int, POOL_BITS, orig + add);
-	} while (cmpxchg(&input_pool.init_bits, orig, new) != orig);
+	} while (!try_cmpxchg(&input_pool.init_bits, &orig, new));
 
 	if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) {
 		crng_reseed(); /* Sets crng_init to CRNG_READY under base_crng.lock. */
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] random: Use try_cmpxchg in _credit_init_bits
  2022-07-14 18:28 [PATCH] random: Use try_cmpxchg in _credit_init_bits Uros Bizjak
@ 2022-07-16 18:29 ` Jason A. Donenfeld
  0 siblings, 0 replies; 2+ messages in thread
From: Jason A. Donenfeld @ 2022-07-16 18:29 UTC (permalink / raw)
  To: Uros Bizjak; +Cc: linux-kernel, Theodore Ts'o

Hi Uros,

On Thu, Jul 14, 2022 at 08:28:22PM +0200, Uros Bizjak wrote:
> Use try_cmpxchg instead of cmpxchg (*ptr, old, new) == old in
> _credit_init_bits. 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, try_cmpxchg implicitly assigns old *ptr value to "old"
> when cmpxchg fails, enabling further code simplifications.
> 
> No functional change intended.

Applied as:
  https://git.kernel.org/pub/scm/linux/kernel/git/crng/random.git/commit/?id=6a66e096619e3a737297eb2f9f535a287af43a95

Thanks for the patch.

Jason

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-07-16 18:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-14 18:28 [PATCH] random: Use try_cmpxchg in _credit_init_bits Uros Bizjak
2022-07-16 18:29 ` Jason A. Donenfeld

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).