mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Waiman Long <waiman.long@hpe.com>
To: Andy Lutomirski <luto@amacapital.net>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	John Stultz <john.stultz@linaro.org>,
	Ingo Molnar <mingo@redhat.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Borislav Petkov <bp@suse.de>,
	Jiang Liu <jiang.liu@linux.intel.com>,
	Randy Wright <rwright@hpe.com>,
	Scott J Norton <scott.norton@hpe.com>,
	Douglas Hatch <doug.hatch@hpe.com>,
	Dave Hansen <dave.hansen@intel.com>,
	Prarit Bhargava <prarit@redhat.com>, X86 ML <x86@kernel.org>,
	"H. Peter Anvin" <hpa@zytor.com>
Subject: Re: [RESEND PATCH v4] x86/hpet: Reduce HPET counter read contention
Date: Fri, 12 Aug 2016 17:10:57 -0400	[thread overview]
Message-ID: <57AE3B61.5010202@hpe.com> (raw)
In-Reply-To: <CALCETrWaVFPkP=aP+KW_sQQ+6M0gOtVbgg2awJqck93PFJHJsw@mail.gmail.com>

On 08/12/2016 04:18 PM, Andy Lutomirski wrote:
> On Aug 12, 2016 9:31 PM, "Waiman Long"<waiman.long@hpe.com>  wrote:
>> On 08/12/2016 01:16 PM, Dave Hansen wrote:
>>> On 08/12/2016 10:01 AM, Waiman Long wrote:
>>>> The reason for using a special lock is that I want both sequence number
>>>> update and locking to be done together atomically. They can be made
>>>> separate as is in the seqlock. However, that will make the code more
>>>> complex to make sure that all the threads see a consistent set of lock
>>>> state and sequence number.
>>> Why do we need a sequence number?  The "cached" HPET itself could be used.
>>>
>>> I'm thinking something like below could use a spinlock instead of the
>>> doing a custom cmpxchg sequence.  The spin_is_locked() should allow the
>>> contended "readers" to avoid using atomics.
>>>
>>> spinlock_t hpet_lock;
>>> u32 hpet_value;
>>> ...
>>> {
>>>          u32 old_hpet = READ_ONCE(hpet_value);
>>>          u32 new_hpet;
>>>
>>>          // need to ensure that the spin_is_locked() is ordered after
>>>          // the READ_ONCE().
>>>          smp_rmb();
>>>          // spin_is_locked() doesn't do atomics
>>>          if (!spin_is_locked(&hpet_lock)&&   spin_trylock(&hpet_lock)) {
>>>
>>>                  WRITE_ONCE(hpet_value, real_read_hpet());
>>>                  spin_unlock(&hpet_lock);
>>>                  return hpet_value;
>>>          }
>>>          // Contended case.  We spin here waiting for the guy who holds
>>>          // the lock to write a new value to 'hpet_value'.
>>>          //
>>>          // We know that our old_hpet is older than our check for the
>>>          // spinlock being locked. So, someone must either have already
>>>          // updated it or be updating it.
>>>          do {
>>>                  cpu_relax();
>>>                  // We do not do a rmb() here.  We don't need a guarantee
>>>                  // that this read is up-to-date, just that it will
>>>                  // _eventually_ see an up-to-date value.
>>>                  new_hpet = READ_ONCE(hpet_value);
>>>          } while (old_hpet == new_hpet);
>>>          return new_hpet;
>>> }
>>
>> Yes, I think that work too. I will update my patch accordingly. Thanks for the input.
> Why is Dave more convincing than I was a couple months ago when I
> asked a similar question? :)

I thought I made some changes in accordance with your comments 
previously. Maybe I missed something:-[

> I don't think this is right.  If the HPET ever returns the same value
> twice in a row (unlikely because it's generally too slow to read, but
> it's plausible that someone will make a fast HPET some day), then this
> could deadlock.

What is the deadlock scenario you are talking about?

>
> Also, does this code need to be NMI-safe?  This implementation is
> deadlocky if it's called from an NMI.

The code isn't NMI-safe as it is not maskable. So is the original code. 
We can check the interrupt state and read HPET directly if in NMI.


>
> The original code was wait-free, right?  That was a nice property, too.
>
> --Andy

The v4 patch isn't wait-free as need to make sure that we read the new 
HPET value instead of the stale one.

Cheers,
Longman

  reply	other threads:[~2016-08-12 21:11 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-10 18:29 Waiman Long
2016-08-10 18:37 ` Long, Wai Man
2016-08-10 19:01   ` Prarit Bhargava
2016-08-11 19:32 ` Dave Hansen
2016-08-11 23:22   ` Waiman Long
2016-08-12  0:31     ` Dave Hansen
2016-08-12 17:01       ` Waiman Long
2016-08-12 17:16         ` Dave Hansen
2016-08-12 18:31           ` Waiman Long
2016-08-12 20:18             ` Andy Lutomirski
2016-08-12 21:10               ` Waiman Long [this message]
2016-08-12 21:20                 ` Dave Hansen
2016-08-12 21:32                   ` Waiman Long
2016-08-12 21:16               ` Dave Hansen
2016-08-12 21:32                 ` Waiman Long
  -- strict thread matches above, loose matches on Subject: below --
2016-06-17 20:20 Waiman Long
2016-07-13 15:02 ` Waiman Long

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=57AE3B61.5010202@hpe.com \
    --to=waiman.long@hpe.com \
    --cc=bp@suse.de \
    --cc=dave.hansen@intel.com \
    --cc=doug.hatch@hpe.com \
    --cc=hpa@zytor.com \
    --cc=jiang.liu@linux.intel.com \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mingo@redhat.com \
    --cc=prarit@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

Powered by JetHome