fs: buffer: do not use unnecessary atomic operations when discarding buffers
authorMel Gorman <mgorman@suse.de>
Wed, 4 Jun 2014 23:10:29 +0000 (16:10 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 4 Jun 2014 23:54:10 +0000 (16:54 -0700)
Discarding buffers uses a bunch of atomic operations when discarding
buffers because ......  I can't think of a reason.  Use a cmpxchg loop to
clear all the necessary flags.  In most (all?) cases this will be a single
atomic operations.

[akpm@linux-foundation.org: move BUFFER_FLAGS_DISCARD into the .c file]
Signed-off-by: Mel Gorman <mgorman@suse.de>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Jan Kara <jack@suse.cz>
Cc: Michal Hocko <mhocko@suse.cz>
Cc: Hugh Dickins <hughd@google.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/buffer.c

index e33f8d5452ad827eb873bdb6fa446b92ae1e0c61..0d3e8d5a2299127a336d788a19646f9bf9dd86c1 100644 (file)
@@ -1483,16 +1483,27 @@ EXPORT_SYMBOL(set_bh_page);
 /*
  * Called when truncating a buffer on a page completely.
  */
+
+/* Bits that are cleared during an invalidate */
+#define BUFFER_FLAGS_DISCARD \
+       (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
+        1 << BH_Delay | 1 << BH_Unwritten)
+
 static void discard_buffer(struct buffer_head * bh)
 {
+       unsigned long b_state, b_state_old;
+
        lock_buffer(bh);
        clear_buffer_dirty(bh);
        bh->b_bdev = NULL;
-       clear_buffer_mapped(bh);
-       clear_buffer_req(bh);
-       clear_buffer_new(bh);
-       clear_buffer_delay(bh);
-       clear_buffer_unwritten(bh);
+       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;
+       }
        unlock_buffer(bh);
 }