Re: [PATCH RT 1/2] rwsem-rt: Do not allow readers to nest

2015-02-25 Thread Sebastian Andrzej Siewior
On 02/18/2015 09:13 PM, Steven Rostedt wrote:

>> Here the same thing but without cmpxchg(). _If_ after an increment the
>> value is negative then we take slowpath. Otherwise we have the lock.
> 
> OK, so I need to make it so it can nest with trylock. I have to look at
> the patch again because it has been a while.

I have reverted the patch and can confirm that cpufreq works again.

I did some testing on vanilla and -RT:
- down_read(l) + down_read(l)
  this triggers a lockdep warning about a possible deadlock the lock is
  obtained.

- down_read(l) + down_read_trylock()
  this passes without a warning.

So I think we good now.

> An RW sem must not do two down_read()s on the same lock (it's fine for
> a trylock if it has a fail safe for it). The reason is, the second
> down_read() will block if there's a writer waiting. Thus you are
> guaranteed a deadlock if you have the lock for read, a write comes in
> and waits, and you grab the RW sem again, because it will block, and
> the writer is waiting for the reader to release. Thus you have a
> deadlock.

I fully understand. However nesting is allowed according to the code in
vanilla and now again in -RT. Lockdep complains properly so we should
catch people doing it wrong in both trees.

> I'll have to revisit this. I also need to revisit the multi readers
> (although Thomas hates it, but he even admitted there's a better way to
> do it. Now only if I could remember what that was ;-)

Okay. For now I keep the revert since it looks sane and simple.

> 
> Thanks,
> 
> -- Steve

Sebastian
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH RT 1/2] rwsem-rt: Do not allow readers to nest

2015-02-19 Thread Jason Low
On Wed, Feb 18, 2015 at 12:13 PM, Steven Rostedt  wrote:
> On Wed, 18 Feb 2015 20:57:10 +0100
> Sebastian Andrzej Siewior  wrote:
>
>> * Steven Rostedt | 2014-04-08 22:47:01 [-0400]:
>>
>> >From: "Steven Rostedt (Red Hat)" 
>> >
>> >The readers of mainline rwsems are not allowed to nest, the rwsems in the
>> >PREEMPT_RT kernel should not nest either.
>>
>> I applied this and this is the reason why cpufreq isn't working. What I
>> see in cpufreq is:
>> | test.sh-788   [004] ...61.416288: store: down_read_try
>> | test.sh-788   [004] ...61.416296: cpufreq_cpu_get: 
>> down_read_try
>> | test.sh-788   [004] ...61.416301: cpufreq_cpu_put.part.6: 
>> up_read
>> | test.sh-788   [004] ...61.416332: store: up_read
>>
>> as you see, one code path takes the read path of rw_sema twice.
>>
>> Looking at the generic implementation, we have:
>> |#define RWSEM_UNLOCKED_VALUE0xL
>> |#define RWSEM_ACTIVE_BIAS   0x0001L
>> |#define RWSEM_WAITING_BIAS  (-RWSEM_ACTIVE_MASK-1)
>>
>> | static inline int __down_read_trylock(struct rw_semaphore *sem)
>> | {
>> | long tmp;
>> |
>> | while ((tmp = sem->count) >= 0) {
>> | if (tmp == cmpxchg(&sem->count, tmp,
>> |tmp + RWSEM_ACTIVE_READ_BIAS)) {
>> | return 1;
>> | }
>> | }
>> | return 0;
>> | }
>>
>> While sem->count is >= 0 we loop and take the semaphore. So we can have
>> five readers at once. The first writer would set count to a negative
>> value resulting in trylock failure.
>>
>> |static inline void __down_read(struct rw_semaphore *sem)
>> |{
>> |if (unlikely(atomic_long_inc_return((atomic_long_t*)&sem->count) <= 
>> 0))
>> |rwsem_down_read_failed(sem);
>> |}
>>
>> Here the same thing but without cmpxchg(). _If_ after an increment the
>> value is negative then we take slowpath. Otherwise we have the lock.
>
> OK, so I need to make it so it can nest with trylock. I have to look at
> the patch again because it has been a while.

When we reported this a few months ago, Thomas provided the following
patch to fix the issue (which essentially reverted the patch) and
appeared to be agreed on:

https://lkml.org/lkml/2014/11/5/844
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH RT 1/2] rwsem-rt: Do not allow readers to nest

2015-02-18 Thread Steven Rostedt
On Wed, 18 Feb 2015 20:57:10 +0100
Sebastian Andrzej Siewior  wrote:

> * Steven Rostedt | 2014-04-08 22:47:01 [-0400]:
> 
> >From: "Steven Rostedt (Red Hat)" 
> >
> >The readers of mainline rwsems are not allowed to nest, the rwsems in the
> >PREEMPT_RT kernel should not nest either.
> 
> I applied this and this is the reason why cpufreq isn't working. What I
> see in cpufreq is:
> | test.sh-788   [004] ...61.416288: store: down_read_try
> | test.sh-788   [004] ...61.416296: cpufreq_cpu_get: 
> down_read_try
> | test.sh-788   [004] ...61.416301: cpufreq_cpu_put.part.6: 
> up_read
> | test.sh-788   [004] ...61.416332: store: up_read
> 
> as you see, one code path takes the read path of rw_sema twice.
> 
> Looking at the generic implementation, we have:
> |#define RWSEM_UNLOCKED_VALUE0xL
> |#define RWSEM_ACTIVE_BIAS   0x0001L
> |#define RWSEM_WAITING_BIAS  (-RWSEM_ACTIVE_MASK-1)
> 
> | static inline int __down_read_trylock(struct rw_semaphore *sem)
> | {
> | long tmp;
> | 
> | while ((tmp = sem->count) >= 0) {
> | if (tmp == cmpxchg(&sem->count, tmp,
> |tmp + RWSEM_ACTIVE_READ_BIAS)) {
> | return 1;
> | }
> | }
> | return 0;
> | }
> 
> While sem->count is >= 0 we loop and take the semaphore. So we can have
> five readers at once. The first writer would set count to a negative
> value resulting in trylock failure.
> 
> |static inline void __down_read(struct rw_semaphore *sem)
> |{
> |if (unlikely(atomic_long_inc_return((atomic_long_t*)&sem->count) <= 
> 0))
> |rwsem_down_read_failed(sem);
> |}
> 
> Here the same thing but without cmpxchg(). _If_ after an increment the
> value is negative then we take slowpath. Otherwise we have the lock.

OK, so I need to make it so it can nest with trylock. I have to look at
the patch again because it has been a while.

> 
> I think I'm going to revert this patch. Where is it written that
> multiple readers of a RW-semaphore can not nest? According to the code
> we can even have multiple readers without nesting (two+ processes may
> take a reader lock).

An RW sem must not do two down_read()s on the same lock (it's fine for
a trylock if it has a fail safe for it). The reason is, the second
down_read() will block if there's a writer waiting. Thus you are
guaranteed a deadlock if you have the lock for read, a write comes in
and waits, and you grab the RW sem again, because it will block, and
the writer is waiting for the reader to release. Thus you have a
deadlock.

I'll have to revisit this. I also need to revisit the multi readers
(although Thomas hates it, but he even admitted there's a better way to
do it. Now only if I could remember what that was ;-)

Thanks,

-- Steve
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH RT 1/2] rwsem-rt: Do not allow readers to nest

2015-02-18 Thread Sebastian Andrzej Siewior
* Steven Rostedt | 2014-04-08 22:47:01 [-0400]:

>From: "Steven Rostedt (Red Hat)" 
>
>The readers of mainline rwsems are not allowed to nest, the rwsems in the
>PREEMPT_RT kernel should not nest either.

I applied this and this is the reason why cpufreq isn't working. What I
see in cpufreq is:
| test.sh-788   [004] ...61.416288: store: down_read_try
| test.sh-788   [004] ...61.416296: cpufreq_cpu_get: 
down_read_try
| test.sh-788   [004] ...61.416301: cpufreq_cpu_put.part.6: 
up_read
| test.sh-788   [004] ...61.416332: store: up_read

as you see, one code path takes the read path of rw_sema twice.

Looking at the generic implementation, we have:
|#define RWSEM_UNLOCKED_VALUE0xL
|#define RWSEM_ACTIVE_BIAS   0x0001L
|#define RWSEM_WAITING_BIAS  (-RWSEM_ACTIVE_MASK-1)

| static inline int __down_read_trylock(struct rw_semaphore *sem)
| {
| long tmp;
| 
| while ((tmp = sem->count) >= 0) {
| if (tmp == cmpxchg(&sem->count, tmp,
|tmp + RWSEM_ACTIVE_READ_BIAS)) {
| return 1;
| }
| }
| return 0;
| }

While sem->count is >= 0 we loop and take the semaphore. So we can have
five readers at once. The first writer would set count to a negative
value resulting in trylock failure.

|static inline void __down_read(struct rw_semaphore *sem)
|{
|if (unlikely(atomic_long_inc_return((atomic_long_t*)&sem->count) <= 0))
|rwsem_down_read_failed(sem);
|}

Here the same thing but without cmpxchg(). _If_ after an increment the
value is negative then we take slowpath. Otherwise we have the lock.

I think I'm going to revert this patch. Where is it written that
multiple readers of a RW-semaphore can not nest? According to the code
we can even have multiple readers without nesting (two+ processes may
take a reader lock).

Sebastian
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH RT 1/2] rwsem-rt: Do not allow readers to nest

2014-05-02 Thread Sebastian Andrzej Siewior
* Steven Rostedt | 2014-04-08 22:47:01 [-0400]:

>From: "Steven Rostedt (Red Hat)" 
>
>The readers of mainline rwsems are not allowed to nest, the rwsems in the
>PREEMPT_RT kernel should not nest either.
>
>Signed-off-by: Steven Rostedt 

Applied

Sebastian
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/