user: Use hashtable.h instead of manual hashtable implementation
PATCH 1/1 user: Use hashtable.h instead of manual hashtable implementation
Convert the UID hashtable in kernel/user.c to use the generic hashtable implementation from hashtable.h instead of the manual approach with hardcoded hlist_head array and custom hash functions.
This simplifies the code and makes it more maintainable by using the standard hashtable API defined in hashtable.h.
Signed-off-by Sasha Levin <[email protected]>
--- kernel/user.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-)
kernel/user.c +12 -21
@@ -20,6 +20,7 @@#include <linux/user_namespace.h> #include <linux/binfmts.h> #include <linux/proc_ns.h> +#include <linux/hashtable.h> #if IS_ENABLED (CONFIG_BINFMT_MISC ) struct binfmt_misc init_binfmt_misc = {
@@ -89,13 +90,9 @@ EXPORT_SYMBOL_GPL(init_user_ns);*/ #define UIDHASH_BITS (IS_ENABLED(CONFIG_BASE_SMALL) ? 3 : 7) -#define UIDHASH_SZ (1 << UIDHASH_BITS) -#define UIDHASH_MASK (UIDHASH_SZ - 1) -#define __uidhashfn (uid) (((uid >> UIDHASH_BITS) + uid) & UIDHASH_MASK) -#define uidhashentry (uid) (uidhash_table + __uidhashfn((__kuid_val(uid))))static struct kmem_cache *uid_cachep; -static struct hlist_head uidhash_table[UIDHASH_SZ ]; +static DEFINE_HASHTABLE(uidhash_table, UIDHASH_BITS);/* * The uidhash_lock is mostly taken from process context, but it is
@@ -118,21 +115,21 @@ struct user_struct root_user = {/* * These routines must be called with the uidhash spinlock held! */ -static void uid_hash_insert (struct user_struct *up,struct hlist_head *hashent) +static void uid_hash_insert (struct user_struct *up) { -hlist_add_head (&up->uidhash_node, hashent); +hash_add (uidhash_table, &up->uidhash_node,__kuid_val (up->uid)); }static void uid_hash_remove (struct user_struct *up) { -hlist_del_init (&up->uidhash_node); +hash_del (&up->uidhash_node); } -static struct user_struct *uid_hash_find (kuid_t uid,struct hlist_head *hashent) +static struct user_struct *uid_hash_find (kuid_t uid) {struct user_struct *user; -hlist_for_each_entry (user, hashent, uidhash_node) { +hash_for_each_possible (uidhash_table, user, uidhash_node,__kuid_val (uid)) {if (uid_eq (user->uid, uid)) {refcount_inc (&user->__count);return user;
@@ -183,7 +180,7 @@ struct user_struct *find_user(kuid_t uid)unsigned long flags;spin_lock_irqsave (&uidhash_lock, flags); - ret =uid_hash_find (uid,uidhashentry (uid)); + ret =uid_hash_find (uid);spin_unlock_irqrestore (&uidhash_lock, flags);return ret; }
@@ -202,11 +199,10 @@ EXPORT_SYMBOL_GPL(free_uid);struct user_struct *alloc_uid (kuid_t uid) { -struct hlist_head *hashent =uidhashentry (uid);struct user_struct *up, *new;spin_lock_irq (&uidhash_lock); - up =uid_hash_find (uid, hashent); + up =uid_hash_find (uid);spin_unlock_irq (&uidhash_lock);if (!up) {
@@ -228,12 +224,12 @@ struct user_struct *alloc_uid(kuid_t uid)* on adding the same user already.. */ spin_lock_irq (&uidhash_lock); - up =uid_hash_find (uid, hashent); + up =uid_hash_find (uid);if (up) {user_epoll_free (new);kmem_cache_free (uid_cachep, new); }else { -uid_hash_insert (new, hashent); +uid_hash_insert (new); up = new; }spin_unlock_irq (&uidhash_lock);
@@ -244,20 +240,15 @@ struct user_struct *alloc_uid(kuid_t uid)static int __init uid_cache_init (void ) { -int n; - uid_cachep =kmem_cache_create ("uid_cache" ,sizeof (struct user_struct ),0 ,SLAB_HWCACHE_ALIGN |SLAB_PANIC ,NULL ); -for (n =0 ; n <UIDHASH_SZ ; ++n) -INIT_HLIST_HEAD (uidhash_table + n); -if (user_epoll_alloc (&root_user))panic ("root_user epoll percpu counter alloc failed" );/* Insert the root user immediately (init already runs as root) */ spin_lock_irq (&uidhash_lock); -uid_hash_insert (&root_user,uidhashentry (GLOBAL_ROOT_UID )); +uid_hash_insert (&root_user);spin_unlock_irq (&uidhash_lock);return 0 ;
Signature · 1 line
2.39.5