public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@arndb.de>
To: Michael Schnell <mschnell@lumino.de>
Cc: David Miller <davem@davemloft.net>,
	alan@lxorguk.ukuu.org.uk, linux-kernel@vger.kernel.org,
	nios2-dev@sopc.et.ntust.edu.tw
Subject: Re: atomic RAM ?
Date: Thu, 8 Apr 2010 16:15:00 +0200	[thread overview]
Message-ID: <201004081615.01151.arnd@arndb.de> (raw)
In-Reply-To: <4BBDCC6E.3060702@lumino.de>

On Thursday 08 April 2010, Michael Schnell wrote:
> On 04/08/2010 02:14 PM, David Miller wrote:
> > Using the spinlock array idea also doesn't work in userspace
> > because any signal handler that tries to do an atomic on the
> > same object will deadlock on the spinlock.
> >   
> Yep. I was beeing afraid of signal issues when thinking about this stuff
> (on and off for several months :) ), too.
> 
> That is why I finally think that a completely hardware based solution
> for each necessary atomic operation is necessary, as well to do Futex
> (if not using said "atomic region" workaround for non-SMP), as to do SMP.

One really expensive but safe way to do atomic operations is to always
have them done on one CPU only, and provide a mechanism for other CPUs
to ask for an atomic operation using an inter-processor-interrupt.

> I finally think that this might be possible in a decent way with custom
> instructions using a - say - 1K Word internal FPGA memory space. But
> this might need some changes in the non-arch dependent Kernel and/or
> library code as the atomic macros would work on "handles" instead of
> pointers (of course these handles would be the old pointers with
> "normal" archs) and the words used by the macros would need to be
> explicitly allocated and deallocated instead of potentially being just
> static variables - even though the "atomic_allocate" macro would just
> create a static variable for "normal archs" and return it's address.

Why can't you do a hash by memory address for this?

I would guess you can define an instruction to atomically set and check
a bit in a shared array of implementation-specific size, by passing
a token in that by convention is the memory address you want to lock.

Given two priviledged instructions

/* returns one if we got the lock, zero if someone else holds it */
bool hashlock_addr(volatile void *addr);
void hashunlock_addr(volatile void *addr);

you can do

int atomic_add_return(int i, atomic_t *v)
{
	int temp;

	while (!hashlock_addr(v))
		;
	smp_rmb();
	temp = v->counter;
	temp += i;
	v->counter = temp;
	smp_wmb();
	hashunlock_addr(v);
}

static inline unsigned long __cmpxchg(volatile unsigned long *m,
                                      unsigned long old, unsigned long new)
{
        unsigned long retval;
        unsigned long flags;

        while (!hashlock_addr(m))
		;
	smp_rmb()
        retval = *m;
        if (retval == old) {
                *m = new;
		smp_wmb();
	}
        hashunlock_addr(m);
        return retval;
}

Anything else you can build on top of these two, including the system calls
that are used from user applications. Since you never hold that bit lock for
more than a few cycles, you could do with much less than 1K bits, in theory
a single global mutex (ignoring the address entirely) would be enough.

That said, a real load-locked/store-conditional would be much more powerful,
in particular because it can also be used from user space, and it is typically
more efficient because it uses the same mechanisms as the cache coherency
protocol.

	Arnd

  reply	other threads:[~2010-04-08 14:15 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-08  7:32 atomic RAM ? Michael Schnell
     [not found] ` <4BBDA1CB.3070204@davidnewall.com>
2010-04-08  9:52   ` Michael Schnell
2010-04-12 12:54     ` Pavel Machek
2010-04-14  8:42       ` Michael Schnell
     [not found] ` <k2gbca41b4c1004080243nc45a10aereaae18557808c943@mail.gmail.com>
2010-04-08 10:12   ` Michael Schnell
2010-04-08 10:45 ` Alan Cox
2010-04-08 12:11   ` Michael Schnell
2010-04-08 12:14     ` David Miller
2010-04-08 12:30       ` Michael Schnell
2010-04-08 14:15         ` Arnd Bergmann [this message]
2010-04-09 10:54           ` Michael Schnell
     [not found]           ` <4BBEECC8.10005@lumino.de>
     [not found]             ` <201004091714.04990.arnd@arndb.de>
2010-04-12  9:58               ` Michael Schnell
2010-04-12 15:02                 ` Arnd Bergmann
2010-04-13 10:11                   ` Michael Schnell
     [not found]                 ` <u2j42b5547a1004120845x87d7f2f7wd4acea1144153dd6@mail.gmail.com>
2010-04-13  8:39                   ` [Nios2-dev] " Michael Schnell
2010-04-08 13:37     ` Alan Cox
2010-04-09 10:55       ` Michael Schnell
2010-04-09 11:54         ` Alan Cox
2010-04-09 12:53           ` Michael Schnell
2010-04-09 13:15             ` Alan Cox
2010-04-09 13:14               ` Michael Schnell
2010-04-09 13:32               ` Michael Schnell
2010-04-14 12:46       ` Michael Schnell
2010-04-14 12:57         ` Alan Cox
2010-04-14 14:38           ` Michael Schnell
2010-04-09  1:36 ` Mike Frysinger
2010-04-09  9:23   ` Michael Schnell

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=201004081615.01151.arnd@arndb.de \
    --to=arnd@arndb.de \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mschnell@lumino.de \
    --cc=nios2-dev@sopc.et.ntust.edu.tw \
    /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