kref: remove kref_set
Subsystems (4)
PATCH 1/1 kref: remove kref_set
Of the three uses of kref_set in the kernel:
One really should be kref_put as the code is letting go of a
reference,
Two really should be kref_init because the kref is being
initialised.
This suggests that making kref_set available encourages bad code. So fix the three uses and remove kref_set completely.
--
The two 'kref_init' calls in ima_iint.c could equally be a single call after the kmem_cache_alloc in ima_inode_alloc, but this version was the minimal code change.
include/linux/kref.h +0 -1
@@ -21,7 +21,6 @@ struct kref {atomic_t refcount; }; -void kref_set (struct kref *kref,int num);void kref_init (struct kref *kref);void kref_get (struct kref *kref);int kref_put (struct kref *kref,void (*release) (struct kref *kref));
kernel/user_namespace.c +2 -2
@@ -54,8 +54,8 @@ int create_user_ns(struct cred *new)#endif /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ -/* alloc_uid() incremented the userns refcount. Just set it to 1 */ -kref_set (&ns->kref,1 ); +/* root_user holds a reference to ns, our reference can be dropped */ +put_user_ns (ns);return 0 ; }
lib/kref.c +2 -13
@@ -15,23 +15,13 @@#include <linux/module.h> /** -* kref_set - initialize object and set refcount to requested number. -* @kref: object in question. -* @num: initial reference counter -*/ -void kref_set (struct kref *kref,int num) -{ -atomic_set (&kref->refcount, num); -smp_mb (); -} - -/** * kref_init - initialize object. * @kref: object in question. */ void kref_init (struct kref *kref) { -kref_set (kref,1 ); +atomic_set (&kref->refcount,1 ); +smp_mb (); }/**
@@ -71,7 +61,6 @@ int kref_put(struct kref *kref, void (*release)(struct kref *kref))return 0 ; } -EXPORT_SYMBOL (kref_set);EXPORT_SYMBOL (kref_init);EXPORT_SYMBOL (kref_get);EXPORT_SYMBOL (kref_put);
security/integrity/ima/ima_iint.c +2 -2
@@ -93,7 +93,7 @@ void iint_free(struct kref *kref) iint->opencount); iint->opencount =0 ; } -kref_set (&iint->refcount,1 ); +kref_init (&iint->refcount);kmem_cache_free (iint_cache, iint); }
@@ -132,7 +132,7 @@ static void init_once(void *foo) iint->readcount =0 ; iint->writecount =0 ; iint->opencount =0 ; -kref_set (&iint->refcount,1 ); +kref_init (&iint->refcount); }static int __init ima_iintcache_init (void )
Quoted 9 lines · NeilBrown
Of the three uses of
kref_setin the kernel:One really should be
kref_putas the code is letting go of a
reference,
Two really should bekref_initbecause the kref is being
initialised.
Quoted 4 lines
This suggests that making
kref_setavailable encourages bad code. So fix the three uses and removekref_setcompletely.Signed-off-by NeilBrown <[email protected]>
No objection to the user_namespace.c hunk - haven't tested it but it certainly had better work with what you have.
thanks,
-serge
Quoted 94 lines
--
The two '
kref_init' calls inima_iint.ccould equally be a single call after thekmem_cache_allocinima_inode_alloc, but this version was the minimal code change.include/linux/kref.h +0
-1@@ -21,7 +21,6 @@ struct kref {atomic_t refcount; }; -void kref_set (struct kref *kref,int num);void kref_init (struct kref *kref);void kref_get (struct kref *kref);int kref_put (struct kref *kref,void (*release) (struct kref *kref));kernel/user_namespace.c +2
-2@@ -54,8 +54,8 @@ int create_user_ns(struct cred *new)#endif /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ -/* alloc_uid() incremented the userns refcount. Just set it to 1 */ -kref_set (&ns->kref,1 ); +/* root_user holds a reference to ns, our reference can be dropped */ +put_user_ns (ns);return 0 ; }lib/kref.c +2
-13@@ -15,23 +15,13 @@#include <linux/module.h> /** -* kref_set - initialize object and set refcount to requested number. -* @kref: object in question. -* @num: initial reference counter -*/ -void kref_set (struct kref *kref,int num) -{ -atomic_set (&kref->refcount, num); -smp_mb (); -} - -/** * kref_init - initialize object. * @kref: object in question. */ void kref_init (struct kref *kref) { -kref_set (kref,1 ); +atomic_set (&kref->refcount,1 ); +smp_mb (); }/** @@ -71,7 +61,6 @@ int kref_put(struct kref *kref, void (*release)(struct kref *kref))return 0 ; } -EXPORT_SYMBOL (kref_set);EXPORT_SYMBOL (kref_init);EXPORT_SYMBOL (kref_get);EXPORT_SYMBOL (kref_put);security/integrity/ima/ima_iint.c +2
-2@@ -93,7 +93,7 @@ void iint_free(struct kref *kref) iint->opencount); iint->opencount =0 ; } -kref_set (&iint->refcount,1 ); +kref_init (&iint->refcount);kmem_cache_free (iint_cache, iint); }@@ -132,7 +132,7 @@ static void init_once(void *foo) iint->readcount =0 ; iint->writecount =0 ; iint->opencount =0 ; -kref_set (&iint->refcount,1 ); +kref_init (&iint->refcount); }static int __init ima_iintcache_init (void )
Quoted 8 lines · NeilBrown
Of the three uses of
kref_setin the kernel:One really should be
kref_putas the code is letting go of a
reference,
Two really should bekref_initbecause the kref is being
initialised.
Quoted 4 lines
This suggests that making
kref_setavailable encourages bad code. So fix the three uses and removekref_setcompletely.Signed-off-by NeilBrown <[email protected]>
Tested the IMA section. Looks good.
Thanks!
Mimi
Quoted 94 lines
--
The two '
kref_init' calls inima_iint.ccould equally be a single call after thekmem_cache_allocinima_inode_alloc, but this version was the minimal code change.include/linux/kref.h +0
-1@@ -21,7 +21,6 @@ struct kref {atomic_t refcount; }; -void kref_set (struct kref *kref,int num);void kref_init (struct kref *kref);void kref_get (struct kref *kref);int kref_put (struct kref *kref,void (*release) (struct kref *kref));kernel/user_namespace.c +2
-2@@ -54,8 +54,8 @@ int create_user_ns(struct cred *new)#endif /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ -/* alloc_uid() incremented the userns refcount. Just set it to 1 */ -kref_set (&ns->kref,1 ); +/* root_user holds a reference to ns, our reference can be dropped */ +put_user_ns (ns);return 0 ; }lib/kref.c +2
-13@@ -15,23 +15,13 @@#include <linux/module.h> /** -* kref_set - initialize object and set refcount to requested number. -* @kref: object in question. -* @num: initial reference counter -*/ -void kref_set (struct kref *kref,int num) -{ -atomic_set (&kref->refcount, num); -smp_mb (); -} - -/** * kref_init - initialize object. * @kref: object in question. */ void kref_init (struct kref *kref) { -kref_set (kref,1 ); +atomic_set (&kref->refcount,1 ); +smp_mb (); }/** @@ -71,7 +61,6 @@ int kref_put(struct kref *kref, void (*release)(struct kref *kref))return 0 ; } -EXPORT_SYMBOL (kref_set);EXPORT_SYMBOL (kref_init);EXPORT_SYMBOL (kref_get);EXPORT_SYMBOL (kref_put);security/integrity/ima/ima_iint.c +2
-2@@ -93,7 +93,7 @@ void iint_free(struct kref *kref) iint->opencount); iint->opencount =0 ; } -kref_set (&iint->refcount,1 ); +kref_init (&iint->refcount);kmem_cache_free (iint_cache, iint); }@@ -132,7 +132,7 @@ static void init_once(void *foo) iint->readcount =0 ; iint->writecount =0 ; iint->opencount =0 ; -kref_set (&iint->refcount,1 ); +kref_init (&iint->refcount); }static int __init ima_iintcache_init (void )
Quoted 11 lines · NeilBrown
Of the three uses of
kref_setin the kernel:One really should be
kref_putas the code is letting go of a
reference,
Two really should bekref_initbecause the kref is being
initialised.This suggests that making
kref_setavailable encourages bad code. So fix the three uses and removekref_setcompletely.
Yeah!
I really didn't like kref_set at all anyway, and I thought that someone "required it" which is why it was added. Oh well, I'm glad it is now gone, thanks so much for this patch.
greg k-h