Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1169894 > unrolled thread

[RFC][PATCH 06/13] percpu-rwsem: Provide percpu_down_read_trylock()

Started byPeter Zijlstra <peterz@infradead.org>
First post2015-06-22 14:30 +0200
Last post2015-06-23 01:20 +0200
Articles 2 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [RFC][PATCH 06/13] percpu-rwsem: Provide percpu_down_read_trylock() Peter Zijlstra <peterz@infradead.org> - 2015-06-22 14:30 +0200
    Re: [RFC][PATCH 06/13] percpu-rwsem: Provide  percpu_down_read_trylock() Oleg Nesterov <oleg@redhat.com> - 2015-06-23 01:20 +0200

#1169894 — [RFC][PATCH 06/13] percpu-rwsem: Provide percpu_down_read_trylock()

FromPeter Zijlstra <peterz@infradead.org>
Date2015-06-22 14:30 +0200
Subject[RFC][PATCH 06/13] percpu-rwsem: Provide percpu_down_read_trylock()
Message-ID<pE91o-3zX-19@gated-at.bofh.it>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 include/linux/percpu-rwsem.h  |   17 +++++++++++++++++
 kernel/locking/percpu-rwsem.c |   12 ++++++++++++
 2 files changed, 29 insertions(+)

--- a/include/linux/percpu-rwsem.h
+++ b/include/linux/percpu-rwsem.h
@@ -17,6 +17,7 @@ struct percpu_rw_semaphore {
 };
 
 extern void __percpu_down_read(struct percpu_rw_semaphore *);
+extern bool __percpu_down_read_trylock(struct percpu_rw_semaphore *);
 extern void __percpu_up_read(struct percpu_rw_semaphore *);
 
 static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
@@ -45,6 +46,22 @@ static inline void percpu_down_read(stru
 	 */
 }
 
+static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
+{
+	bool ret = true;
+
+	preempt_disable();
+	__this_cpu_inc(*sem->refcount);
+	if (unlikely(!rcu_sync_is_idle(&sem->rss)))
+		ret = __percpu_down_read_trylock(sem);
+	preempt_enable();
+
+	if (ret)
+		rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 1, _RET_IP_);
+
+	return ret;
+}
+
 static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
 {
 	/*
--- a/kernel/locking/percpu-rwsem.c
+++ b/kernel/locking/percpu-rwsem.c
@@ -80,6 +80,18 @@ void __percpu_down_read(struct percpu_rw
 	preempt_disable();
 }
 
+bool __percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
+{
+	smp_mb(); /* A matches D */
+
+	if (likely(smp_load_acquire(&sem->state) != readers_block))
+		return true;
+
+	__percpu_up_read(sem);
+
+	return false;
+}
+
 void __percpu_up_read(struct percpu_rw_semaphore *sem)
 {
 	smp_mb(); /* B matches C */


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1170286 — Re: [RFC][PATCH 06/13] percpu-rwsem: Provide percpu_down_read_trylock()

FromOleg Nesterov <oleg@redhat.com>
Date2015-06-23 01:20 +0200
SubjectRe: [RFC][PATCH 06/13] percpu-rwsem: Provide percpu_down_read_trylock()
Message-ID<pEjap-1zV-3@gated-at.bofh.it>
In reply to#1169894
On 06/22, Peter Zijlstra wrote:
>
> +static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
> +{
> +	bool ret = true;
> +
> +	preempt_disable();
> +	__this_cpu_inc(*sem->refcount);
> +	if (unlikely(!rcu_sync_is_idle(&sem->rss)))
> +		ret = __percpu_down_read_trylock(sem);
> +	preempt_enable();
> +
> +	if (ret)
> +		rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 1, _RET_IP_);
> +
> +	return ret;
> +}
...
> +bool __percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
> +{
> +	smp_mb(); /* A matches D */
> +
> +	if (likely(smp_load_acquire(&sem->state) != readers_block))
> +		return true;
> +
> +	__percpu_up_read(sem);
> +
> +	return false;
> +}

Looks like we can slightly refactor this code to avoid the code
duplication. But this is minor too and we can do this later.

Reviewed-by: Oleg Nesterov <oleg@redhat.com>

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web