public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] buffer: Avoid setting buffer bits that are already set
@ 2017-10-23 15:27 Kemi Wang
  2017-10-23 16:19 ` Jens Axboe
  0 siblings, 1 reply; 6+ messages in thread
From: Kemi Wang @ 2017-10-23 15:27 UTC (permalink / raw)
  To: Jan Kara, Jens Axboe, Darrick J Wong, Kemi Wang, Eric Biggers,
	Andreas Gruenbacher, Jeff Layton
  Cc: Dave, Andi Kleen, Tim Chen, Ying Huang, Aaron Lu, Linux Kernel

It's expensive to set buffer flags that are already set, because that
causes a costly cache line transition.

A common case is setting the "verified" flag during ext4 writes.
This patch checks for the flag being set first.

With the AIM7/creat-clo benchmark testing on a 48G ramdisk based-on ext4
file system, we see 3.3%(15431->15936) improvement of aim7.jobs-per-min on
a 2-sockets broadwell platform.

What the benchmark does is: it forks 3000 processes, and each  process do
the following:
a) open a new file
b) close the file
c) delete the file
until loop=100*1000 times.

The original patch is contributed by Andi Kleen.

Signed-off-by: Andi Kleen <ak@linux.intel.com>
Signed-off-by: Kemi Wang <kemi.wang@intel.com>
Tested-by: Kemi Wang <kemi.wang@intel.com>
---
 include/linux/buffer_head.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h
index c8dae55..e1799f7 100644
--- a/include/linux/buffer_head.h
+++ b/include/linux/buffer_head.h
@@ -84,7 +84,8 @@ struct buffer_head {
 #define BUFFER_FNS(bit, name)						\
 static __always_inline void set_buffer_##name(struct buffer_head *bh)	\
 {									\
-	set_bit(BH_##bit, &(bh)->b_state);				\
+	if (!test_bit(BH_##bit, &(bh)->b_state))			\
+		set_bit(BH_##bit, &(bh)->b_state);              \
 }									\
 static __always_inline void clear_buffer_##name(struct buffer_head *bh)	\
 {									\
-- 
2.7.4

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

* Re: [PATCH] buffer: Avoid setting buffer bits that are already set
  2017-10-23 15:27 [PATCH] buffer: Avoid setting buffer bits that are already set Kemi Wang
@ 2017-10-23 16:19 ` Jens Axboe
  2017-10-24  0:52   ` kemi
  0 siblings, 1 reply; 6+ messages in thread
From: Jens Axboe @ 2017-10-23 16:19 UTC (permalink / raw)
  To: Kemi Wang, Jan Kara, Darrick J Wong, Eric Biggers,
	Andreas Gruenbacher, Jeff Layton
  Cc: Dave, Andi Kleen, Tim Chen, Ying Huang, Aaron Lu, Linux Kernel

On 10/23/2017 10:27 AM, Kemi Wang wrote:
> It's expensive to set buffer flags that are already set, because that
> causes a costly cache line transition.
> 
> A common case is setting the "verified" flag during ext4 writes.
> This patch checks for the flag being set first.
> 
> With the AIM7/creat-clo benchmark testing on a 48G ramdisk based-on ext4
> file system, we see 3.3%(15431->15936) improvement of aim7.jobs-per-min on
> a 2-sockets broadwell platform.
> 
> What the benchmark does is: it forks 3000 processes, and each  process do
> the following:
> a) open a new file
> b) close the file
> c) delete the file
> until loop=100*1000 times.
> 
> The original patch is contributed by Andi Kleen.

We discussed this recently, in reference to this commit:

commit 7fcbbaf18392f0b17c95e2f033c8ccf87eecde1d
Author: Jens Axboe <axboe@fb.com>
Date:   Thu May 22 11:54:16 2014 -0700

    mm/filemap.c: avoid always dirtying mapping->flags on O_DIRECT

which made a massive difference, as the changelog details.

blk-mq uses this extensively as well, where possible. The problem is
that it always has to be explained, hence the recent discussion was
around perhaps adding

set_bit_if_not_set()
clear_bit_if_set()

or similar functions, to document in a single location why this matters.
Additionally, some archs may be able to implement that in an efficient
manner.

You can add my reviewed-by to the below, I'll see if I can find some
time to implement the above in a nice way. In the mean time, you may
want to consider adding a comment to the function explaining why you
have done it that way.

-- 
Jens Axboe

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

* Re: [PATCH] buffer: Avoid setting buffer bits that are already set
  2017-10-23 16:19 ` Jens Axboe
@ 2017-10-24  0:52   ` kemi
  2017-10-24  1:21     ` Andi Kleen
  0 siblings, 1 reply; 6+ messages in thread
From: kemi @ 2017-10-24  0:52 UTC (permalink / raw)
  To: Jens Axboe, Jan Kara, Darrick J Wong, Eric Biggers,
	Andreas Gruenbacher, Jeff Layton
  Cc: Dave, Andi Kleen, Tim Chen, Ying Huang, Aaron Lu, Linux Kernel



On 2017年10月24日 00:19, Jens Axboe wrote:
> On 10/23/2017 10:27 AM, Kemi Wang wrote:
>> It's expensive to set buffer flags that are already set, because that
>> causes a costly cache line transition.
>>
>> A common case is setting the "verified" flag during ext4 writes.
>> This patch checks for the flag being set first.
>>
>> With the AIM7/creat-clo benchmark testing on a 48G ramdisk based-on ext4
>> file system, we see 3.3%(15431->15936) improvement of aim7.jobs-per-min on
>> a 2-sockets broadwell platform.
>>
>> What the benchmark does is: it forks 3000 processes, and each  process do
>> the following:
>> a) open a new file
>> b) close the file
>> c) delete the file
>> until loop=100*1000 times.
>>
>> The original patch is contributed by Andi Kleen.
> 
> We discussed this recently, in reference to this commit:
> 
> commit 7fcbbaf18392f0b17c95e2f033c8ccf87eecde1d
> Author: Jens Axboe <axboe@fb.com>
> Date:   Thu May 22 11:54:16 2014 -0700
> 
>     mm/filemap.c: avoid always dirtying mapping->flags on O_DIRECT
> 
> which made a massive difference, as the changelog details.
> 
> blk-mq uses this extensively as well, where possible. The problem is
> that it always has to be explained, hence the recent discussion was
> around perhaps adding
> 
> set_bit_if_not_set()
> clear_bit_if_set()
> 
> or similar functions, to document in a single location why this matters.
> Additionally, some archs may be able to implement that in an efficient
> manner.
> 
> You can add my reviewed-by to the below, 

Thanks.

I'll see if I can find some
> time to implement the above in a nice way.

Agree. Maybe something like test_and_set_bit() would be more suitable.

 In the mean time, you may
> want to consider adding a comment to the function explaining why you
> have done it that way.
> 

Sure.

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

* Re: [PATCH] buffer: Avoid setting buffer bits that are already set
  2017-10-24  0:52   ` kemi
@ 2017-10-24  1:21     ` Andi Kleen
  2017-10-24  1:25       ` kemi
  0 siblings, 1 reply; 6+ messages in thread
From: Andi Kleen @ 2017-10-24  1:21 UTC (permalink / raw)
  To: kemi
  Cc: Jens Axboe, Jan Kara, Darrick J Wong, Eric Biggers,
	Andreas Gruenbacher, Jeff Layton, Dave, Andi Kleen, Tim Chen,
	Ying Huang, Aaron Lu, Linux Kernel

kemi <kemi.wang@intel.com> writes:
>
> I'll see if I can find some
>> time to implement the above in a nice way.
>
> Agree. Maybe something like test_and_set_bit() would be more suitable.

test_and_set_bit is a very different operation for the CPU because
it is atomic for both. But we want the initial read to not
be atomic.

If you add special functions use a different variant that is only
atomic for the set.

-Andi

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

* Re: [PATCH] buffer: Avoid setting buffer bits that are already set
  2017-10-24  1:21     ` Andi Kleen
@ 2017-10-24  1:25       ` kemi
  2017-10-24 13:50         ` Jens Axboe
  0 siblings, 1 reply; 6+ messages in thread
From: kemi @ 2017-10-24  1:25 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Jens Axboe, Jan Kara, Darrick J Wong, Eric Biggers,
	Andreas Gruenbacher, Jeff Layton, Dave, Andi Kleen, Tim Chen,
	Ying Huang, Aaron Lu, Linux Kernel



On 2017年10月24日 09:21, Andi Kleen wrote:
> kemi <kemi.wang@intel.com> writes:
>>
>> I'll see if I can find some
>>> time to implement the above in a nice way.
>>
>> Agree. Maybe something like test_and_set_bit() would be more suitable.
> 
> test_and_set_bit is a very different operation for the CPU because
> it is atomic for both. But we want the initial read to not
> be atomic.
> 

I meant to express the meaning of test before setting bit.
Apologize to make you confused.

> If you add special functions use a different variant that is only
> atomic for the set.
> 
> -Andi
> 

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

* Re: [PATCH] buffer: Avoid setting buffer bits that are already set
  2017-10-24  1:25       ` kemi
@ 2017-10-24 13:50         ` Jens Axboe
  0 siblings, 0 replies; 6+ messages in thread
From: Jens Axboe @ 2017-10-24 13:50 UTC (permalink / raw)
  To: kemi, Andi Kleen
  Cc: Jan Kara, Darrick J Wong, Eric Biggers, Andreas Gruenbacher,
	Jeff Layton, Dave, Andi Kleen, Tim Chen, Ying Huang, Aaron Lu,
	Linux Kernel

On 10/23/2017 07:25 PM, kemi wrote:
> 
> 
> On 2017年10月24日 09:21, Andi Kleen wrote:
>> kemi <kemi.wang@intel.com> writes:
>>>
>>> I'll see if I can find some
>>>> time to implement the above in a nice way.
>>>
>>> Agree. Maybe something like test_and_set_bit() would be more suitable.
>>
>> test_and_set_bit is a very different operation for the CPU because
>> it is atomic for both. But we want the initial read to not
>> be atomic.
>>
> 
> I meant to express the meaning of test before setting bit.
> Apologize to make you confused.

That's why I suggested something like set_bit_if_not_set(),
test_and_set_bit() is both already used and has entirely
different semantics.

-- 
Jens Axboe

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

end of thread, other threads:[~2017-10-24 13:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-23 15:27 [PATCH] buffer: Avoid setting buffer bits that are already set Kemi Wang
2017-10-23 16:19 ` Jens Axboe
2017-10-24  0:52   ` kemi
2017-10-24  1:21     ` Andi Kleen
2017-10-24  1:25       ` kemi
2017-10-24 13:50         ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox