The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Zeng Heng' <zengheng4@huawei.com>,
	"mingo@redhat.com" <mingo@redhat.com>,
	"will@kernel.org" <will@kernel.org>,
	"peterz@infradead.org" <peterz@infradead.org>,
	"longman@redhat.com" <longman@redhat.com>,
	"boqun.feng@gmail.com" <boqun.feng@gmail.com>
Cc: "xiexiuqi@huawei.com" <xiexiuqi@huawei.com>,
	"liwei391@huawei.com" <liwei391@huawei.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH v2] locking/osq_lock: Avoid false sharing in optimistic_spin_node
Date: Sat, 23 Dec 2023 13:06:29 +0000	[thread overview]
Message-ID: <1ae13bccdff74cdd9a7cd7fc9442e2ad@AcuMS.aculab.com> (raw)
In-Reply-To: <5ac8c272-6887-5406-50e3-7b87b302498d@huawei.com>

From: Zeng Heng
> Sent: 23 December 2023 08:55
> 
> 在 2023/12/22 20:40, David Laight 写道:
> > From: Zeng Heng
> >> Sent: 22 December 2023 12:11
> >>
> >> Using the UnixBench test suite, we clearly find that osq_lock() cause
> >> extremely high overheads with perf tool in the File Copy items:
> >>
> >> Overhead  Shared Object            Symbol
> >>    94.25%  [kernel]                 [k] osq_lock
> >>     0.74%  [kernel]                 [k] rwsem_spin_on_owner
> >>     0.32%  [kernel]                 [k] filemap_get_read_batch
> >>
> >> In response to this, we conducted an analysis and made some gains:
> >>
> >> In the prologue of osq_lock(), it set `cpu` member of percpu struct
> >> optimistic_spin_node with the local cpu id, after that the value of the
> >> percpu struct would never change in fact. Based on that, we can regard
> >> the `cpu` member as a constant variable.
> >>
> > ...
> >> @@ -9,7 +11,13 @@
> >>   struct optimistic_spin_node {
> >>   	struct optimistic_spin_node *next, *prev;
> >>   	int locked; /* 1 if lock acquired */
> >> -	int cpu; /* encoded CPU # + 1 value */
> >> +
> >> +	CACHELINE_PADDING(_pad1_);
> >> +	/*
> >> +	 * Stores an encoded CPU # + 1 value.
> >> +	 * Only read by other cpus, so split into different cache lines.
> >> +	 */
> >> +	int cpu;
> >>   };
> > Isn't this structure embedded in every mutex and rwsem (etc)?
> > So that is a significant bloat especially on systems with
> > large cache lines.

This code is making my head hurt :-)
The 'spin_node' does only exist per-cpu.

> > Did you try just moving the initialisation of the per-cpu 'node'
> > below the first fast-path (uncontended) test in osq_lock()?

Reading more closely they do need to be valid before the fast-path
cmpxchg.
But I suspect the 'cache line dirty' could be done conditionally
or in the unlock/fail path.

I think the unlock fast-path always has node->next == NULL and it is
set to NULL in the slow path.
The lock-fail path calls osq_wait_next() - which also NULLs it.
So maybe it is always NULL on entry anyway?

node->locked is set by the slow-path lock code.
So could be cleared when checked or any time before the unlock returns.
Possibly unconditionally in the unlock slow path and conditionally
in the unlock fast path?

I think that would mean the assignment to node in osq_lock() could
be moved below the first xchg() (provided 'node' can be initialised).

I also wonder what the performance difference is between
smp_processor_id() and this_cpu_ptr(&osq_node)?

Bloating all the mutex/rwsem by 4 bytes (on 64bit) and changing
lock->tail to 'struct optimistic_spin_node *' (and moving it's
definition into the .c file) may well improve performance?


> >
> > OTOH if you really have multiple cpu spinning on the same rwsem
> > perhaps the test and/or filemap code are really at fault!
> >
> > 	David
> 
> Hi,
> 
> The File Copy items of UnixBench testsuite are using 1 read file and 1
> write file
> 
> for file read/write/copy test. In multi-parallel scenario, that would
> lead to high file lock contention.
> 
> That is just a performance test suite and has nothing to do with whether
> the user program design is correct or not.

But it might be stressing some code paths that don't usually happen.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

  reply	other threads:[~2023-12-23 13:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-22 12:10 [PATCH v2] locking/osq_lock: Avoid false sharing in optimistic_spin_node Zeng Heng
2023-12-22 12:40 ` David Laight
2023-12-23  8:54   ` Zeng Heng
2023-12-23 13:06     ` David Laight [this message]
2024-01-05 10:08 ` kernel test robot

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=1ae13bccdff74cdd9a7cd7fc9442e2ad@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=boqun.feng@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liwei391@huawei.com \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    --cc=xiexiuqi@huawei.com \
    --cc=zengheng4@huawei.com \
    /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