mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Miquel van Smoorenburg <miquels@cistron.nl>
Cc: Ingo Molnar <mingo@elte.hu>, linux-kernel@vger.kernel.org
Subject: Re: spinlock which can morph into a mutex
Date: Fri, 18 Dec 2009 18:14:29 +0100 (CET)	[thread overview]
Message-ID: <alpine.LFD.2.00.0912181804360.2755@localhost.localdomain> (raw)
In-Reply-To: <20091218143032.GA16595@xs4all.net>

On Fri, 18 Dec 2009, Miquel van Smoorenburg wrote:

> I'm trying to implement a dynamically resizable hashtable, and
> I have found that after resizing the table I need to call
> synchronize_rcu() and finish up before letting other writers
> (inserts, deletes) access the table.
> 
> Ofcourse during the hashtable update a spinlock is held to
> exclude the other writers. But I cannot hold this spinlock over
> synchronize_rcu(), yet the other writers still need to be excluded.
> 
> So I probably need a mutex instead of a spinlock, but I want to
> keep minimal overhead for the common case (when no resizing is in
> progress).  I think I need a spinlock that can morph into a mutex ..

Is the writer frequency and the possible contention so high that you
need a spinlock at all ?
 
> I was thinking about using something like the code below.
> It is sortof like a spinlock, but it's ofcourse less fair
> than actual ticketed spinlocks.
> 
> I'm working off 2.6.27 at the moment, but I noticed that in
> 2.6.28 adaptive spinning was introduced for mutexes. Is the
> approach below still worth it with adaptive spinning or could
> I just convert the spinlocks to mutexes with minimal extra overhead ?

Test it :)

If the mutex is still to heavy weight for you, then you can solve it
without implementing another weird concurrency control:

writer side:

       spin_lock(&hash_lock);

       if (unlikely(hash_update_active)) {
       	  spin_unlock(&hash_lock);
       	  wait_event_(un)interruptible(&hash_wq, !hash_update_active);
	  spin_lock(&hash_lock);
       }

resize side:

       spin_lock(&hash_lock);
       hash_update_active = 1;
       ....
       spin_unlock(&hash_lock);
       synchronize_rcu();
       hash_update_active = 0;
       wake_up(&hash_wq);

Thanks,

	tglx

  parent reply	other threads:[~2009-12-18 17:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-18 14:30 Miquel van Smoorenburg
2009-12-18 15:26 ` Andi Kleen
2009-12-18 15:43 ` Peter Zijlstra
2009-12-18 17:14 ` Thomas Gleixner [this message]
2009-12-18 18:19   ` Miquel van Smoorenburg

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=alpine.LFD.2.00.0912181804360.2755@localhost.localdomain \
    --to=tglx@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=miquels@cistron.nl \
    /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