From: Waiman Long <waiman.long@hpe.com>
To: Andy Lutomirski <luto@amacapital.net>
Cc: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
Jonathan Corbet <corbet@lwn.net>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
<linux-doc@kernel.org>, X86 ML <x86@kernel.org>,
Jiang Liu <jiang.liu@linux.intel.com>,
Borislav Petkov <bp@suse.de>, Andy Lutomirski <luto@kernel.org>,
Scott J Norton <scott.norton@hpe.com>,
Douglas Hatch <doug.hatch@hpe.com>,
Randy Wright <rwright@hpe.com>
Subject: Re: [PATCH v3] x86/hpet: Reduce HPET counter read contention
Date: Tue, 12 Apr 2016 14:55:13 -0400 [thread overview]
Message-ID: <570D4491.3020408@hpe.com> (raw)
In-Reply-To: <570D245A.3020306@hpe.com>
On 04/12/2016 12:37 PM, Waiman Long wrote:
> On 04/11/2016 04:21 PM, Andy Lutomirski wrote:
>>
>>> +
>>> + /* Unlock */
>>> + smp_store_release(&hpet_save.seq, new + 1);
>>> + local_irq_restore(flags);
>>> + return (cycle_t)time;
>>> + }
>>> + local_irq_restore(flags);
>>> + seq = new;
>>> + }
>>> +
>>> + /*
>>> + * Wait until the locked sequence number changes which
>>> indicates
>>> + * that the saved HPET value is up-to-date.
>>> + */
>>> + while (READ_ONCE(hpet_save.seq) == seq) {
>>> + /*
>>> + * Since reading the HPET is much slower than a single
>>> + * cpu_relax() instruction, we use two here in an
>>> attempt
>>> + * to reduce the amount of cacheline contention in the
>>> + * hpet_save.seq cacheline.
>>> + */
>>> + cpu_relax();
>>> + cpu_relax();
>>> + }
>>> +
>>> + return (cycle_t)READ_ONCE(hpet_save.hpet);
>>> +}
>> I wonder if this could be simplified. Pseudocode:
>>
>> u32 time;
>> unsigned long flags;
>>
>> local_irq_save(flags);
>>
>> if (spin_trylock(&hpet_lock)) {
>> time = hpet_readl(HPET_COUNTER);
>> WRITE_ONCE(last_hpet_counter, time);
>
> You will need a spin_unlock(&hpet_lock) here.
>
>> } else {
>> spin_unlock_wait(&hpet_lock);
>> /* When this function started, hpet_lock was locked. Now it's
>> unlocked, which means that time is at least as new as whatever the
>> lock holder returned. */
>> time = READ_ONCE(last_hpet_counter);
>> }
>>
>> local_irq_restore(flags);
>> return time;
>>
>> Should be fasterunder heavy contention, too: spinlocks are very nicely
>> optimized.
>
> I don't think it will be faster. The current spinlock code isn't more
> optimized than what you can do with a cmpxchg and smp_store_release.
> In fact, it is what the spinlock code is actually doing. Other
> differences includes:
>
> 1) A CPU will not do local_irq_save/local_irq_restore when the lock is
> not free.
> 2) My patch also use a change a sequence number to indicate an updated
> time stamp is available. So there will be cases where CPUs running
> your code will have to wait while the those running my code can grab
> the time stamp and return immediately.
>
Moreover, if the timing is such that right after one CPU release the
lock, the next one get it immediately and there is a continuous stream
of incoming CPUs. It is possible that the ones that are waiting for the
lock to be free will see the lock being acquired for an indefinite
period of time. This is certainly not something we want to have and this
is what the sequence number is for.
Cheers,
Longman
next prev parent reply other threads:[~2016-04-12 19:11 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-11 20:09 [PATCH v3] x86/hpet: Reduce HPET counter read contention Waiman Long
2016-04-11 20:21 ` Andy Lutomirski
2016-04-12 16:37 ` Waiman Long
2016-04-12 18:55 ` Waiman Long [this message]
2016-04-11 20:22 ` kbuild 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=570D4491.3020408@hpe.com \
--to=waiman.long@hpe.com \
--cc=bp@suse.de \
--cc=corbet@lwn.net \
--cc=doug.hatch@hpe.com \
--cc=hpa@zytor.com \
--cc=jiang.liu@linux.intel.com \
--cc=linux-doc@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=rwright@hpe.com \
--cc=scott.norton@hpe.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/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