refcount: introduce generic lockptr funcs
Subsystems (2)
RFC 1/2 refcount: introduce generic lockptr funcs
This patch introduce lockptr refcount operations. Currently refcount has a lot of refcount_dec_and_lock() functionality for most common used locktype. Those functions look mostly all the same and is duplicated inside the refcount implementation. Instead of introducing a new whole refcount_dec_and_lock() functionality e.g. for rwlock_t and their _bh variants this patch will introduce lockptr. A lockptr is just a void * and refers to the actual locking instance that can even be an own locking type. Over the passed callbacks for lock and unlock operations the void *lockptr becomes to the real thing by casting it and do the locktype specific lock operation.
--- include/linux/refcount.h | 15 +++++++ lib/refcount.c | 92 ++++++++++++++++++++++++++++------------ 2 files changed, 80 insertions(+), 27 deletions(-)
include/linux/refcount.h +15 -0
@@ -366,4 +366,19 @@ extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock) extern __must_check bool refcount_dec_and_lock_irqsave(refcount_t *r, spinlock_t *lock, unsigned long *flags) __cond_acquires(lock); +extern bool refcount_dec_and_lockptr(refcount_t *r, void (*lock)(void *lockptr), + void (*unlock)(void *lockptr), void *lockptr) __cond_acquires(lockptr); + +extern void lockptr_mutex_lock(void *lockptr) __acquires(lockptr); +extern void lockptr_mutex_unlock(void *lockptr) __releases(lockptr); +extern void lockptr_spin_lock(void *lockptr) __acquires(lockptr); +extern void lockptr_spin_unlock(void *lockptr) __releases(lockptr); + +struct lockptr_irqsave_data { +void *lockptr; +unsigned long *flags; +}; +extern void lockptr_irqsave(void *lockptr) __acquires(lockptr); +extern void lockptr_irqsave(void *lockptr) __releases(lockptr); +#endif /* _LINUX_REFCOUNT_H */
lib/refcount.c +65 -27
122 lines not on this page · raw message
Signature · 1 line
2.39.3
RFC 2/2 kref: introduce kref_put_lockptr() and use lockptr
This patch switches to make kref_put_lock() more locktype independend by introducing kref_put_lockptr() and using refcount_dec_and_lockptr(). The user can now pass a lockptr and do the specific locktype operation by parameters. The current kref_put_mutex() and kref_put_lock() has been adapted to use the new kref_put_lockptr() implementation for existing users.
--- include/linux/kref.h | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
include/linux/kref.h +16 -9
@@ -68,26 +68,33 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)return 0 ; } -static inline int kref_put_mutex (struct kref *kref, -void (*release)(struct kref *kref), -struct mutex *lock) +static inline int kref_put_lockptr (struct kref *kref, +void (*release)(struct kref *kref), +void (*lock)(void *lockptr), +void (*unlock)(void *lockptr), +void *lockptr) { -if (refcount_dec_and_mutex_lock (&kref->refcount, lock)) { +if (refcount_dec_and_lockptr (&kref->refcount, lock, unlock, lockptr)) {release (kref);return 1 ; }return 0 ; } +static inline int kref_put_mutex (struct kref *kref, +void (*release)(struct kref *kref), +struct mutex *lock) +{ +return kref_put_lockptr (kref, release, lockptr_mutex_lock, + lockptr_mutex_unlock, lock); +} +static inline int kref_put_lock (struct kref *kref,void (*release)(struct kref *kref),spinlock_t *lock) { -if (refcount_dec_and_lock (&kref->refcount, lock)) { -release (kref); -return 1 ; - } -return 0 ; +return kref_put_lockptr (kref, release, lockptr_spin_lock, + lockptr_spin_unlock, lock); }/**
Signature · 1 line
2.39.3
Peter Zijlstra
maintainerQuoted 13 lines · Alexander Aring
lib/refcount.c +5
-0@@ -94,6 +94,34 @@ bool refcount_dec_not_one(refcount_t *r) }EXPORT_SYMBOL (refcount_dec_not_one); +bool refcount_dec_and_lockptr (refcount_t *r,void (*lock)(void *lockptr), +void (*unlock)(void *lockptr),void *lockptr) +{ +if (refcount_dec_not_one (r)) +return false;
lib/refcount.c bool refcount_dec_not_one(refcount_t *r) · line 111
Quoted 10 lines
lib/refcount.c +10
-0+ +lock (lockptr); +if (!refcount_dec_and_test (r)) { +unlock (lockptr); +return false; + } + +return true; +} +EXPORT_SYMBOL (refcount_dec_and_lockptr);
This is terrible, you're forcing indirect calls on everything.
Hi,
Quoted 20 lines · Peter Zijlstra
Quoted 17 lines · Alexander Aring
lib/refcount.c +9
-0@@ -94,6 +94,34 @@ bool refcount_dec_not_one(refcount_t *r) }EXPORT_SYMBOL (refcount_dec_not_one); +bool refcount_dec_and_lockptr (refcount_t *r,void (*lock)(void *lockptr), +void (*unlock)(void *lockptr),void *lockptr) +{ +if (refcount_dec_not_one (r)) +return false; + +lock (lockptr); +if (!refcount_dec_and_test (r)) { +unlock (lockptr);
Quoted 9 lines
lib/refcount.c bool refcount_dec_not_one(refcount_t *r) · line 111
Quoted 6 lines
lib/refcount.c +6
-0+return false; + } + +return true; +} +EXPORT_SYMBOL (refcount_dec_and_lockptr);This is terrible, you're forcing indirect calls on everything.
Okay, I see. How about introducing a macro producing all the code at preprocessor time?
- Alex
Peter Zijlstra
maintainerQuoted 33 lines · Alexander Aring
Hi,
Quoted 20 lines · Peter Zijlstra
Quoted 17 lines · Alexander Aring
lib/refcount.c +9
-0@@ -94,6 +94,34 @@ bool refcount_dec_not_one(refcount_t *r) }EXPORT_SYMBOL (refcount_dec_not_one); +bool refcount_dec_and_lockptr (refcount_t *r,void (*lock)(void *lockptr), +void (*unlock)(void *lockptr),void *lockptr) +{ +if (refcount_dec_not_one (r)) +return false; + +lock (lockptr); +if (!refcount_dec_and_test (r)) { +unlock (lockptr);Quoted 9 lines
lib/refcount.c bool refcount_dec_not_one(refcount_t *r) · line 111
Quoted 6 lines
lib/refcount.c +6
-0+return false; + } + +return true; +} +EXPORT_SYMBOL (refcount_dec_and_lockptr);This is terrible, you're forcing indirect calls on everything.
Quoted 2 lines
Okay, I see. How about introducing a macro producing all the code at preprocessor time?
__always_inline should work, then you get constant propagation for the function pointer.
But indeed, perhaps a macro is more convenient vs the irq flags argument. You'll then end up with something like:
#define __refcount_dec_and_lock(_ref, _lock, _unlock) \
({ bool _ret = false; \
if (!refcount_dec_not_one(_ref)) { \
_lock; \
if (!refcount_dec_and_test(_ref)) { \
_unlock; \
} else { \
_ret = true; \
} \
} \
_ret; \
})bool refcount_dec_and_spinlock_irqsave(refcount_t *r, spinlock_t *lock,
unsigned long *flags)
{
return __refcount_dec_and_lock(r, spin_lock_irqsave(*lock, *flags),
spin_unlock_irqrestore(*lock, *flags));
}Hi,
Quoted 35 lines · Peter Zijlstra
Quoted 33 lines · Alexander Aring
Hi,
Quoted 29 lines · Peter Zijlstra
Quoted 13 lines · Alexander Aring
lib/refcount.c +5
-0@@ -94,6 +94,34 @@ bool refcount_dec_not_one(refcount_t *r) }EXPORT_SYMBOL (refcount_dec_not_one); +bool refcount_dec_and_lockptr (refcount_t *r,void (*lock)(void *lockptr), +void (*unlock)(void *lockptr),void *lockptr) +{ +if (refcount_dec_not_one (r)) +return false;lib/refcount.c bool refcount_dec_not_one(refcount_t *r) · line 111
Quoted 10 lines
lib/refcount.c +10
-0+ +lock (lockptr); +if (!refcount_dec_and_test (r)) { +unlock (lockptr); +return false; + } + +return true; +} +EXPORT_SYMBOL (refcount_dec_and_lockptr);This is terrible, you're forcing indirect calls on everything.
Quoted 6 lines
Quoted 2 lines
Okay, I see. How about introducing a macro producing all the code at preprocessor time?
__always_inlineshould work, then you get constant propagation for the function pointer.
Thanks, it is always good to learn something new.
Quoted 17 lines
But indeed, perhaps a macro is more convenient vs the irq flags argument. You'll then end up with something like:
#define
__refcount_dec_and_lock(_ref, _lock, _unlock)\({ bool _ret = false; \ if (!refcount_dec_not_one(_ref)) { \ _lock; \ if (!refcount_dec_and_test(_ref)) { \ _unlock; \ } else { \ _ret = true; \ } \ } \ _ret; \ })
Quoted 6 lines
bool refcount_dec_and_spinlock_irqsave(refcount_t *r,
spinlock_t*lock,
unsigned long *flags)
{
return __refcount_dec_and_lock(r,spin_lock_irqsave(*lock, *flags),
spin_unlock_irqrestore(*lock, *flags));
}
I was thinking of solving the additional flags parameter with prototype and argos macros e.g. TRACE_EVENT() is doing it, but this version looks much better.
I will send a patch and do similar things with _kref_put_lock().
- Alex
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Alexander,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on linus/master]
[also build test WARNING on v6.6 next-20231103]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Alexander-Aring/kref-introduce-kref_put_lockptr-and-use-lockptr/20231104-002152 base: linus/master patch link: https://lore.kernel.org/r/20231103161635.1902667-1-aahringo%40redhat.com patch subject: [RFC 1/2] refcount: introduce generic lockptr funcs config: arc-randconfig-001-20231104 (https://download.01.org/0day-ci/archive/20231104/[email protected]/config) compiler: arc-elf-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231104/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> lib/refcount.c:183:6: warning: no previous prototype for 'lockptr_lock_irqsave' [-Wmissing-prototypes]
183 | void lockptr_lock_irqsave(void *lockptr)
| ^~~~~~~~~~~~~~~~~~~~
>> lib/refcount.c:191:6: warning: no previous prototype for 'lockptr_unlock_irqsave' [-Wmissing-prototypes] 191 | void lockptr_unlock_irqsave(void *lockptr)
| ^~~~~~~~~~~~~~~~~~~~~~
vim +/lockptr_lock_irqsave +183 lib/refcount.c
182
> 183 void lockptr_lock_irqsave(void *lockptr)
184 {
185 struct lockptr_irqsave_data *d = lockptr;
186
187 spin_lock_irqsave(d->lockptr, *d->flags);
188 }
189 EXPORT_SYMBOL(lockptr_lock_irqsave);
190
> 191 void lockptr_unlock_irqsave(void *lockptr) 192 {
193 struct lockptr_irqsave_data *d = lockptr;
194
195 spin_unlock_irqrestore(d->lockptr, *d->flags);
196 }
197 EXPORT_SYMBOL(lockptr_unlock_irqsave);
198
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Alexander,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on linus/master]
[also build test WARNING on v6.6 next-20231103]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Alexander-Aring/kref-introduce-kref_put_lockptr-and-use-lockptr/20231104-002152 base: linus/master patch link: https://lore.kernel.org/r/20231103161635.1902667-1-aahringo%40redhat.com patch subject: [RFC 1/2] refcount: introduce generic lockptr funcs config: um-allnoconfig (https://download.01.org/0day-ci/archive/20231104/[email protected]/config) compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231104/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> lib/refcount.c:183:6: warning: no previous prototype for function 'lockptr_lock_irqsave' [-Wmissing-prototypes]
183 | void lockptr_lock_irqsave(void *lockptr)
| ^
lib/refcount.c:183:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
183 | void lockptr_lock_irqsave(void *lockptr)
| ^
| static
>> lib/refcount.c:191:6: warning: no previous prototype for function 'lockptr_unlock_irqsave' [-Wmissing-prototypes]
191 | void lockptr_unlock_irqsave(void *lockptr)
| ^
lib/refcount.c:191:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
191 | void lockptr_unlock_irqsave(void *lockptr)
| ^
| static 2 warnings generated.
vim +/lockptr_lock_irqsave +183 lib/refcount.c
182
> 183 void lockptr_lock_irqsave(void *lockptr)
184 {
185 struct lockptr_irqsave_data *d = lockptr;
186
187 spin_lock_irqsave(d->lockptr, *d->flags);
188 }
189 EXPORT_SYMBOL(lockptr_lock_irqsave);
190
> 191 void lockptr_unlock_irqsave(void *lockptr) 192 {
193 struct lockptr_irqsave_data *d = lockptr;
194
195 spin_unlock_irqrestore(d->lockptr, *d->flags);
196 }
197 EXPORT_SYMBOL(lockptr_unlock_irqsave);
198
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki