2023-09-13 18:10:35

by Oleg Nesterov

[permalink] [raw]
Subject: [PATCH 3/5] seqlock: introduce seqprop_lock/unlock

which can be used to take/release the corresponding lock.

Thanks to the previous patch, it is trivial to pass 2 arguments to
the new __seqprop_##lockname##_lock/unlock "methods", plus we do not
loose the type info and thus the new seqprop's are "type safe".

So for example

void func(seqcount_rwlock_t *s, rwlock_t *l)
{
seqprop_lock(s, l);
}

happily compiles, but this one

void func(seqcount_rwlock_t *s, spinlock_t *l)
{
seqprop_lock(s, l);
}

doesn't.

Signed-off-by: Oleg Nesterov <[email protected]>
---
include/linux/seqlock.h | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)

diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
index 41e36f8afad4..9831683a0102 100644
--- a/include/linux/seqlock.h
+++ b/include/linux/seqlock.h
@@ -241,6 +241,21 @@ static __always_inline void \
__seqprop_##lockname##_assert(const seqcount_##lockname##_t *s) \
{ \
__SEQ_LOCK(lockdep_assert_held(s->lock)); \
+} \
+ \
+static __always_inline void \
+__seqprop_##lockname##_lock(seqcount_##lockname##_t *s, \
+ locktype *lock) \
+{ \
+ __SEQ_LOCK(WARN_ON_ONCE(s->lock != lock)); \
+ lockbase##_lock(lock); \
+} \
+ \
+static __always_inline void \
+__seqprop_##lockname##_unlock(seqcount_##lockname##_t *s, \
+ locktype *lock) \
+{ \
+ lockbase##_unlock(lock); \
}

/*
@@ -306,6 +321,12 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
#define seqprop_preemptible(s) __seqprop(s, preemptible)(s)
#define seqprop_assert(s) __seqprop(s, assert)(s)

+/* seqcount_t doesn't have these methods */
+static inline void __seqprop_lock (seqcount_t *s, void *l) { BUILD_BUG(); }
+static inline void __seqprop_unlock (seqcount_t *s, void *l) { BUILD_BUG(); }
+#define seqprop_lock(s, l) __seqprop(s, lock)(s, l)
+#define seqprop_unlock(s, l) __seqprop(s, unlock)(s, l)
+
/**
* __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
--
2.25.1.362.g51ebf55


2023-09-15 18:30:33

by Alexey Gladkov

[permalink] [raw]
Subject: Re: [PATCH 3/5] seqlock: introduce seqprop_lock/unlock

On Wed, Sep 13, 2023 at 05:50:00PM +0200, Oleg Nesterov wrote:
> which can be used to take/release the corresponding lock.
>
> Thanks to the previous patch, it is trivial to pass 2 arguments to
> the new __seqprop_##lockname##_lock/unlock "methods", plus we do not
> loose the type info and thus the new seqprop's are "type safe".
>
> So for example
>
> void func(seqcount_rwlock_t *s, rwlock_t *l)
> {
> seqprop_lock(s, l);
> }
>
> happily compiles, but this one
>
> void func(seqcount_rwlock_t *s, spinlock_t *l)
> {
> seqprop_lock(s, l);
> }
>
> doesn't.
>
> Signed-off-by: Oleg Nesterov <[email protected]>
> ---
> include/linux/seqlock.h | 21 +++++++++++++++++++++
> 1 file changed, 21 insertions(+)
>
> diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
> index 41e36f8afad4..9831683a0102 100644
> --- a/include/linux/seqlock.h
> +++ b/include/linux/seqlock.h
> @@ -241,6 +241,21 @@ static __always_inline void \
> __seqprop_##lockname##_assert(const seqcount_##lockname##_t *s) \
> { \
> __SEQ_LOCK(lockdep_assert_held(s->lock)); \
> +} \
> + \
> +static __always_inline void \
> +__seqprop_##lockname##_lock(seqcount_##lockname##_t *s, \
> + locktype *lock) \
> +{ \
> + __SEQ_LOCK(WARN_ON_ONCE(s->lock != lock)); \
> + lockbase##_lock(lock); \
> +} \
> + \
> +static __always_inline void \
> +__seqprop_##lockname##_unlock(seqcount_##lockname##_t *s, \
> + locktype *lock) \
> +{ \
> + lockbase##_unlock(lock); \
> }

Why are you creating a new method with an unused argument s ?

>
> /*
> @@ -306,6 +321,12 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
> #define seqprop_preemptible(s) __seqprop(s, preemptible)(s)
> #define seqprop_assert(s) __seqprop(s, assert)(s)
>
> +/* seqcount_t doesn't have these methods */
> +static inline void __seqprop_lock (seqcount_t *s, void *l) { BUILD_BUG(); }
> +static inline void __seqprop_unlock (seqcount_t *s, void *l) { BUILD_BUG(); }
> +#define seqprop_lock(s, l) __seqprop(s, lock)(s, l)
> +#define seqprop_unlock(s, l) __seqprop(s, unlock)(s, l)
> +
> /**
> * __read_seqcount_begin() - begin a seqcount_t read section w/o barrier
> * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
> --
> 2.25.1.362.g51ebf55
>

--
Rgrds, legion

2023-09-16 06:10:18

by Oleg Nesterov

[permalink] [raw]
Subject: Re: [PATCH 3/5] seqlock: introduce seqprop_lock/unlock

On 09/15, Alexey Gladkov wrote:
>
> On Wed, Sep 13, 2023 at 05:50:00PM +0200, Oleg Nesterov wrote:
> > + \
> > +static __always_inline void \
> > +__seqprop_##lockname##_lock(seqcount_##lockname##_t *s, \
> > + locktype *lock) \
> > +{ \
> > + __SEQ_LOCK(WARN_ON_ONCE(s->lock != lock)); \
> > + lockbase##_lock(lock); \
> > +} \
> > + \
> > +static __always_inline void \
> > +__seqprop_##lockname##_unlock(seqcount_##lockname##_t *s, \
> > + locktype *lock) \
> > +{ \
> > + lockbase##_unlock(lock); \
> > }
>
> Why are you creating a new method with an unused argument s ?

To make it consistent/symmetrical with _lock() which does
__SEQ_LOCK(WARN_ON_ONCE(s->lock != lock)). _unlock() could do the
same check as well, but somehow I decided it would be too much.

And with other "methods". Say, __seqprop_##lockname##_preemptible(s)
doesn't use 's' too.

Otherwise they both do not need the 1st seqcount_##lockname##_t *s
argument.

Oleg.