From: Uros Bizjak <ubizjak@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: "Uros Bizjak" <ubizjak@gmail.com>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Ingo Molnar" <mingo@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Darren Hart" <dvhart@infradead.org>,
"Davidlohr Bueso" <dave@stgolabs.net>,
"André Almeida" <andrealmeid@igalia.com>
Subject: [PATCH v2] futex: Rewrite get_inode_sequence_number() to make code simpler
Date: Fri, 4 Oct 2024 10:52:32 +0200 [thread overview]
Message-ID: <20241004085257.10908-1-ubizjak@gmail.com> (raw)
Rewrite get_inode_sequence_number() to make code simpler:
a) Rewrite FOR loop to a DO-WHILE loop with returns moved
out of the loop.
b) Use atomic64_inc_return() instead of atomic64_add_return().
c) Use !atomic64_try_cmpxchg_relaxed(*ptr, &old, new) instead of
atomic64_cmpxchg_relaxed (*ptr, old, new) != old. x86 CMPXCHG
instruction returns success in ZF flag, so this change also saves
a compare instruction after CMPXCHG.
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: "André Almeida" <andrealmeid@igalia.com>
---
v2: Explicitly initialize "old" to zero before the call to
atomic64_try_cmpxchg_relaxed(). Rewrite commit message to
state the motivation for the patch.
---
kernel/futex/core.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/kernel/futex/core.c b/kernel/futex/core.c
index 136768ae2637..ac650f7ed56c 100644
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -173,23 +173,21 @@ futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout,
static u64 get_inode_sequence_number(struct inode *inode)
{
static atomic64_t i_seq;
- u64 old;
+ u64 old, new;
/* Does the inode already have a sequence number? */
old = atomic64_read(&inode->i_sequence);
if (likely(old))
return old;
- for (;;) {
- u64 new = atomic64_add_return(1, &i_seq);
- if (WARN_ON_ONCE(!new))
- continue;
+ do {
+ new = atomic64_inc_return(&i_seq);
+ } while (WARN_ON_ONCE(!new));
- old = atomic64_cmpxchg_relaxed(&inode->i_sequence, 0, new);
- if (old)
- return old;
- return new;
- }
+ old = 0;
+ if (!atomic64_try_cmpxchg_relaxed(&inode->i_sequence, &old, new))
+ return old;
+ return new;
}
/**
--
2.46.2
next reply other threads:[~2024-10-04 8:53 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-04 8:52 Uros Bizjak [this message]
2024-10-09 19:43 ` [PATCH v2] futex: Rewrite get_inode_sequence_number() to make code simpler André Almeida
2024-10-10 1:47 ` Thomas Gleixner
2024-10-10 6:20 ` Uros Bizjak
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=20241004085257.10908-1-ubizjak@gmail.com \
--to=ubizjak@gmail.com \
--cc=andrealmeid@igalia.com \
--cc=dave@stgolabs.net \
--cc=dvhart@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
/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