[PATCH] seqlock: fix raw_read_seqcount_latch()

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] seqlock: fix raw_read_seqcount_latch()

Alexey Dobriyan-2
lockless_dereference() is supposed to take pointer not integer.

Signed-off-by: Alexey Dobriyan <[hidden email]>
---

 include/linux/seqlock.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--- a/include/linux/seqlock.h
+++ b/include/linux/seqlock.h
@@ -277,7 +277,7 @@ static inline void raw_write_seqcount_barrier(seqcount_t *s)
 
 static inline int raw_read_seqcount_latch(seqcount_t *s)
 {
- return lockless_dereference(s->sequence);
+ return lockless_dereference(s)->sequence;
 }
 
 /**
@@ -331,7 +331,7 @@ static inline int raw_read_seqcount_latch(seqcount_t *s)
  * unsigned seq, idx;
  *
  * do {
- * seq = lockless_dereference(latch->seq);
+ * seq = lockless_dereference(latch)->seq;
  *
  * idx = seq & 0x01;
  * entry = data_query(latch->data[idx], ...);
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] seqlock: fix raw_read_seqcount_latch()

Peter Zijlstra-5
On Sat, May 21, 2016 at 11:14:49PM +0300, Alexey Dobriyan wrote:
> lockless_dereference() is supposed to take pointer not integer.

Urgh :/

Is there any way we can make lockless_dereference() issue a warning if
we don't feed it a pointer?

Would something like so work? All pointer types should silently cast to
void * while integer (and others) should refuse to.

diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index b5ff9881bef8..8886de704d33 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -544,6 +544,7 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
  */
 #define lockless_dereference(p) \
 ({ \
+ __maybe_unused void * _________p2 = p; \
  typeof(p) _________p1 = READ_ONCE(p); \
  smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
  (_________p1); \
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] seqlock: fix raw_read_seqcount_latch()

Alexey Dobriyan-2
On Sun, May 22, 2016 at 12:48:27PM +0200, Peter Zijlstra wrote:

> On Sat, May 21, 2016 at 11:14:49PM +0300, Alexey Dobriyan wrote:
> > lockless_dereference() is supposed to take pointer not integer.
>
> Urgh :/
>
> Is there any way we can make lockless_dereference() issue a warning if
> we don't feed it a pointer?
>
> Would something like so work? All pointer types should silently cast to
> void * while integer (and others) should refuse to.

This works (and spammy enough in case of seqlock, which is good)
but not for "unsigned long":

        include/linux/percpu-refcount.h:146:36: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
          percpu_ptr = lockless_dereference(ref->percpu_count_ptr);

> --- a/include/linux/compiler.h
> +++ b/include/linux/compiler.h
> @@ -544,6 +544,7 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
>   */
>  #define lockless_dereference(p) \
>  ({ \
> + __maybe_unused void * _________p2 = p; \
>   typeof(p) _________p1 = READ_ONCE(p); \
>   smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
>   (_________p1); \
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] seqlock: fix raw_read_seqcount_latch()

Peter Zijlstra-5
On Sun, May 22, 2016 at 09:50:40PM +0300, Alexey Dobriyan wrote:

> On Sun, May 22, 2016 at 12:48:27PM +0200, Peter Zijlstra wrote:
> > On Sat, May 21, 2016 at 11:14:49PM +0300, Alexey Dobriyan wrote:
> > > lockless_dereference() is supposed to take pointer not integer.
> >
> > Urgh :/
> >
> > Is there any way we can make lockless_dereference() issue a warning if
> > we don't feed it a pointer?
> >
> > Would something like so work? All pointer types should silently cast to
> > void * while integer (and others) should refuse to.
>
> This works (and spammy enough in case of seqlock, which is good)
> but not for "unsigned long":
>
> include/linux/percpu-refcount.h:146:36: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
>  percpu_ptr = lockless_dereference(ref->percpu_count_ptr);

TJ; would you prefer casting or not using lockless_dereference() here?

>
> > --- a/include/linux/compiler.h
> > +++ b/include/linux/compiler.h
> > @@ -544,6 +544,7 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
> >   */
> >  #define lockless_dereference(p) \
> >  ({ \
> > + __maybe_unused void * _________p2 = p; \
> >   typeof(p) _________p1 = READ_ONCE(p); \
> >   smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
> >   (_________p1); \
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] seqlock: fix raw_read_seqcount_latch()

Tejun Heo-2
Hello,

On Mon, May 23, 2016 at 11:36:18AM +0200, Peter Zijlstra wrote:
> > include/linux/percpu-refcount.h:146:36: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
> >  percpu_ptr = lockless_dereference(ref->percpu_count_ptr);
>
> TJ; would you prefer casting or not using lockless_dereference() here?

Casting is nasty - *(unsigned long __percpu **)& - because the macro
expects an lvalue.  I think it'd be better to revert to opencoding
READ_ONCE() and barrier there.  It's a pretty special case anyway.

Thanks.

--
tejun
Reply | Threaded
Open this post in threaded view
|

[PATCH] percpu: Revert ("percpu: Replace smp_read_barrier_depends() with lockless_dereference()")

Tejun Heo-2
lockless_dereference() is planned to grow a sanity check to ensure
that the input parameter is a pointer.  __ref_is_percpu() passes in an
unsinged long value which is a combination of a pointer and a flag.
While it can be casted to a pointer lvalue, the casting looks messy
and it's a special case anyway.  Let's revert back to open-coding
READ_ONCE() and explicit barrier.

This doesn't cause any functional changes.

Signed-off-by: Tejun Heo <[hidden email]>
Link: http://lkml.kernel.org/g/20160522185040.GA23664@...
Cc: Pranith Kumar <[hidden email]>
Cc: Alexey Dobriyan <[hidden email]>
Cc: Peter Zijlstra <[hidden email]>
---
So, something like this.  Please feel free to include in the series.

Thanks.

 include/linux/percpu-refcount.h |   12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h
index 84f542d..1c7eec0 100644
--- a/include/linux/percpu-refcount.h
+++ b/include/linux/percpu-refcount.h
@@ -136,14 +136,12 @@ static inline bool __ref_is_percpu(struct percpu_ref *ref,
  * used as a pointer.  If the compiler generates a separate fetch
  * when using it as a pointer, __PERCPU_REF_ATOMIC may be set in
  * between contaminating the pointer value, meaning that
- * ACCESS_ONCE() is required when fetching it.
- *
- * Also, we need a data dependency barrier to be paired with
- * smp_store_release() in __percpu_ref_switch_to_percpu().
- *
- * Use lockless deref which contains both.
+ * READ_ONCE() is required when fetching it.
  */
- percpu_ptr = lockless_dereference(ref->percpu_count_ptr);
+ percpu_ptr = READ_ONCE(ref->percpu_count_ptr);
+
+ /* paired with smp_store_release() in __percpu_ref_switch_to_percpu() */
+ smp_read_barrier_depends();
 
  /*
  * Theoretically, the following could test just ATOMIC; however,