summaryrefslogtreecommitdiffstats
path: root/fs/buffer.c
diff options
context:
space:
mode:
authorUros Bizjak <ubizjak@gmail.com>2022-07-14 19:16:53 +0200
committerAndrew Morton <akpm@linux-foundation.org>2022-09-11 21:55:07 -0700
commitb0192296b45232872720d969366449c001ab1f4a (patch)
treee4c722c8935747ded01af079f6e1c8302264c2b5 /fs/buffer.c
parent693fc06e98514c2d5951ead4aca40cf8b21100b1 (diff)
downloadlinux-stable-b0192296b45232872720d969366449c001ab1f4a.tar.gz
linux-stable-b0192296b45232872720d969366449c001ab1f4a.tar.bz2
linux-stable-b0192296b45232872720d969366449c001ab1f4a.zip
buffer: use try_cmpxchg in discard_buffer
Use try_cmpxchg instead of cmpxchg (*ptr, old, new) == old in discard_buffer. 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. Note that the value from *ptr should be read using READ_ONCE to prevent the compiler from merging, refetching or reordering the read. No functional change intended. Link: https://lkml.kernel.org/r/20220714171653.12128-1-ubizjak@gmail.com Signed-off-by: Uros Bizjak <ubizjak@gmail.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'fs/buffer.c')
-rw-r--r--fs/buffer.c14
1 files changed, 5 insertions, 9 deletions
diff --git a/fs/buffer.c b/fs/buffer.c
index 55e762a58eb6..2fd98bb7d74e 100644
--- a/fs/buffer.c
+++ b/fs/buffer.c
@@ -1464,19 +1464,15 @@ EXPORT_SYMBOL(set_bh_page);
static void discard_buffer(struct buffer_head * bh)
{
- unsigned long b_state, b_state_old;
+ unsigned long b_state;
lock_buffer(bh);
clear_buffer_dirty(bh);
bh->b_bdev = NULL;
- b_state = bh->b_state;
- for (;;) {
- b_state_old = cmpxchg(&bh->b_state, b_state,
- (b_state & ~BUFFER_FLAGS_DISCARD));
- if (b_state_old == b_state)
- break;
- b_state = b_state_old;
- }
+ b_state = READ_ONCE(bh->b_state);
+ do {
+ } while (!try_cmpxchg(&bh->b_state, &b_state,
+ b_state & ~BUFFER_FLAGS_DISCARD));
unlock_buffer(bh);
}