public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Matthew Wilcox <matthew@wil.cx>
To: Hirokazu Takata <takata@linux-m32r.org>
Cc: Andrew Morton <akpm@osdl.org>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] m32r: Revise __raw_read_trylock()
Date: Sun, 24 Sep 2006 00:20:36 -0600	[thread overview]
Message-ID: <20060924062036.GB30273@parisc-linux.org> (raw)
In-Reply-To: <swfzmcse7mm.wl%takata@linux-m32r.org>

On Fri, Sep 22, 2006 at 03:29:53PM +0900, Hirokazu Takata wrote:
>  
> -#define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
> +static inline int __raw_read_trylock(raw_rwlock_t *lock)
> +{
> +	atomic_t *count = (atomic_t*)lock;
> +	atomic_dec(count);
> +	if (atomic_read(count) >= 0)
> +		return 1;
> +	atomic_inc(count);
> +	return 0;
> +}
>  

Is there a race here between __raw_read_trylock and __raw_write_trylock?

CPU A			CPU B
__raw_read_trylock
atomic_dec(count);
			__raw_write_trylock
			atomic_sub_and_test(RW_LOCK_BIAS, count)
atomic_read(count)

It'd be fairly harmless as neither would manage to get the lock.  But
I think it's not too hard to fix.  Seems to me you want to do:

static inline int __raw_read_trylock(raw_rwlock_t *lock)
{
	atomic_t *count = (atomic_t*)lock;
	if (atomic_dec_return(count) >= 0)
		return 1;
	atomic_inc(count);
	return 0;
}

eliminating the race.

  parent reply	other threads:[~2006-09-24  6:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-22  6:29 [PATCH] m32r: Revise __raw_read_trylock() Hirokazu Takata
2006-09-22  7:48 ` Paul Mundt
2006-09-22 11:27   ` Matthew Wilcox
2006-09-25  6:09     ` Paul Mundt
2006-09-24  6:20 ` Matthew Wilcox [this message]
2006-09-25  7:47   ` Hirokazu Takata
2006-09-26 21:33     ` Andrew Morton
2006-09-26 22:29       ` Matthew Wilcox

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=20060924062036.GB30273@parisc-linux.org \
    --to=matthew@wil.cx \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=takata@linux-m32r.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